xref: /aosp_15_r20/frameworks/native/include/android/surface_texture.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker /**
18*38e8c45fSAndroid Build Coastguard Worker  * @addtogroup SurfaceTexture
19*38e8c45fSAndroid Build Coastguard Worker  * @{
20*38e8c45fSAndroid Build Coastguard Worker  */
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker /**
23*38e8c45fSAndroid Build Coastguard Worker  * @file surface_texture.h
24*38e8c45fSAndroid Build Coastguard Worker  */
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker #ifndef ANDROID_NATIVE_SURFACE_TEXTURE_H
27*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_NATIVE_SURFACE_TEXTURE_H
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker /******************************************************************
30*38e8c45fSAndroid Build Coastguard Worker  *
31*38e8c45fSAndroid Build Coastguard Worker  * IMPORTANT NOTICE:
32*38e8c45fSAndroid Build Coastguard Worker  *
33*38e8c45fSAndroid Build Coastguard Worker  *   This file is part of Android's set of stable system headers
34*38e8c45fSAndroid Build Coastguard Worker  *   exposed by the Android NDK (Native Development Kit).
35*38e8c45fSAndroid Build Coastguard Worker  *
36*38e8c45fSAndroid Build Coastguard Worker  *   Third-party source AND binary code relies on the definitions
37*38e8c45fSAndroid Build Coastguard Worker  *   here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
38*38e8c45fSAndroid Build Coastguard Worker  *
39*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
40*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
41*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
42*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
43*38e8c45fSAndroid Build Coastguard Worker  */
44*38e8c45fSAndroid Build Coastguard Worker 
45*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
46*38e8c45fSAndroid Build Coastguard Worker #include <sys/cdefs.h>
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker #include <android/native_window.h>
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker __BEGIN_DECLS
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker struct ASurfaceTexture;
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker /**
55*38e8c45fSAndroid Build Coastguard Worker  * {@link ASurfaceTexture} is an opaque type to manage SurfaceTexture from native code
56*38e8c45fSAndroid Build Coastguard Worker  *
57*38e8c45fSAndroid Build Coastguard Worker  * {@link ASurfaceTexture} can be obtained from an android.graphics.SurfaceTexture object using
58*38e8c45fSAndroid Build Coastguard Worker  * ASurfaceTexture_fromSurfaceTexture().
59*38e8c45fSAndroid Build Coastguard Worker  */
60*38e8c45fSAndroid Build Coastguard Worker typedef struct ASurfaceTexture ASurfaceTexture;
61*38e8c45fSAndroid Build Coastguard Worker 
62*38e8c45fSAndroid Build Coastguard Worker /**
63*38e8c45fSAndroid Build Coastguard Worker  * Release the reference to the native ASurfaceTexture acquired with
64*38e8c45fSAndroid Build Coastguard Worker  * ASurfaceTexture_fromSurfaceTexture().
65*38e8c45fSAndroid Build Coastguard Worker  * Failing to do so will result in leaked memory and graphic resources.
66*38e8c45fSAndroid Build Coastguard Worker  *
67*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
68*38e8c45fSAndroid Build Coastguard Worker  *
69*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
70*38e8c45fSAndroid Build Coastguard Worker  */
71*38e8c45fSAndroid Build Coastguard Worker void ASurfaceTexture_release(ASurfaceTexture* st) __INTRODUCED_IN(28);
72*38e8c45fSAndroid Build Coastguard Worker 
73*38e8c45fSAndroid Build Coastguard Worker /**
74*38e8c45fSAndroid Build Coastguard Worker  * Returns a reference to an ANativeWindow (i.e. the Producer) for this SurfaceTexture.
75*38e8c45fSAndroid Build Coastguard Worker  * This is equivalent to Java's: Surface sur = new Surface(surfaceTexture);
76*38e8c45fSAndroid Build Coastguard Worker  *
77*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
78*38e8c45fSAndroid Build Coastguard Worker  *
79*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
80*38e8c45fSAndroid Build Coastguard Worker  * @return A reference to an ANativeWindow. This reference MUST BE released when no longer needed
81*38e8c45fSAndroid Build Coastguard Worker  * using ANativeWindow_release(). Failing to do so will result in leaked resources. nullptr is
82*38e8c45fSAndroid Build Coastguard Worker  * returned if \p st is null or if it's not an instance of android.graphics.SurfaceTexture
83*38e8c45fSAndroid Build Coastguard Worker  */
84*38e8c45fSAndroid Build Coastguard Worker ANativeWindow* ASurfaceTexture_acquireANativeWindow(ASurfaceTexture* st) __INTRODUCED_IN(28);
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker /**
87*38e8c45fSAndroid Build Coastguard Worker  * Attach the SurfaceTexture to the OpenGL ES context that is current on the calling thread.  A
88*38e8c45fSAndroid Build Coastguard Worker  * new OpenGL ES texture object is created and populated with the SurfaceTexture image frame
89*38e8c45fSAndroid Build Coastguard Worker  * that was current at the time of the last call to {@link ASurfaceTexture_detachFromGLContext}.
90*38e8c45fSAndroid Build Coastguard Worker  * This new texture is bound to the GL_TEXTURE_EXTERNAL_OES texture target.
91*38e8c45fSAndroid Build Coastguard Worker  *
92*38e8c45fSAndroid Build Coastguard Worker  * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES
93*38e8c45fSAndroid Build Coastguard Worker  * contexts.  Note, however, that the image contents are only accessible from one OpenGL ES
94*38e8c45fSAndroid Build Coastguard Worker  * context at a time.
95*38e8c45fSAndroid Build Coastguard Worker  *
96*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
97*38e8c45fSAndroid Build Coastguard Worker  *
98*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
99*38e8c45fSAndroid Build Coastguard Worker  * \param texName The name of the OpenGL ES texture that will be created.  This texture name
100*38e8c45fSAndroid Build Coastguard Worker  * must be unusued in the OpenGL ES context that is current on the calling thread.
101*38e8c45fSAndroid Build Coastguard Worker  * \return 0 on success, negative posix error code otherwise (see <errno.h>)
102*38e8c45fSAndroid Build Coastguard Worker  */
103*38e8c45fSAndroid Build Coastguard Worker int ASurfaceTexture_attachToGLContext(ASurfaceTexture* st, uint32_t texName) __INTRODUCED_IN(28);
104*38e8c45fSAndroid Build Coastguard Worker 
105*38e8c45fSAndroid Build Coastguard Worker /**
106*38e8c45fSAndroid Build Coastguard Worker  * Detach the SurfaceTexture from the OpenGL ES context that owns the OpenGL ES texture object.
107*38e8c45fSAndroid Build Coastguard Worker  * This call must be made with the OpenGL ES context current on the calling thread.  The OpenGL
108*38e8c45fSAndroid Build Coastguard Worker  * ES texture object will be deleted as a result of this call.  After calling this method all
109*38e8c45fSAndroid Build Coastguard Worker  * calls to {@link ASurfaceTexture_updateTexImage} will fail until a successful call to
110*38e8c45fSAndroid Build Coastguard Worker  * {@link ASurfaceTexture_attachToGLContext} is made.
111*38e8c45fSAndroid Build Coastguard Worker  *
112*38e8c45fSAndroid Build Coastguard Worker  * This can be used to access the SurfaceTexture image contents from multiple OpenGL ES
113*38e8c45fSAndroid Build Coastguard Worker  * contexts.  Note, however, that the image contents are only accessible from one OpenGL ES
114*38e8c45fSAndroid Build Coastguard Worker  * context at a time.
115*38e8c45fSAndroid Build Coastguard Worker  *
116*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
117*38e8c45fSAndroid Build Coastguard Worker  *
118*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
119*38e8c45fSAndroid Build Coastguard Worker  * \return 0 on success, negative posix error code otherwise (see <errno.h>)
120*38e8c45fSAndroid Build Coastguard Worker  */
121*38e8c45fSAndroid Build Coastguard Worker int ASurfaceTexture_detachFromGLContext(ASurfaceTexture* st) __INTRODUCED_IN(28);
122*38e8c45fSAndroid Build Coastguard Worker 
123*38e8c45fSAndroid Build Coastguard Worker /**
124*38e8c45fSAndroid Build Coastguard Worker  * Update the texture image to the most recent frame from the image stream.  This may only be
125*38e8c45fSAndroid Build Coastguard Worker  * called while the OpenGL ES context that owns the texture is current on the calling thread.
126*38e8c45fSAndroid Build Coastguard Worker  * It will implicitly bind its texture to the GL_TEXTURE_EXTERNAL_OES texture target.
127*38e8c45fSAndroid Build Coastguard Worker  *
128*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
129*38e8c45fSAndroid Build Coastguard Worker  *
130*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
131*38e8c45fSAndroid Build Coastguard Worker  * \return 0 on success, negative posix error code otherwise (see <errno.h>)
132*38e8c45fSAndroid Build Coastguard Worker  */
133*38e8c45fSAndroid Build Coastguard Worker int ASurfaceTexture_updateTexImage(ASurfaceTexture* st) __INTRODUCED_IN(28);
134*38e8c45fSAndroid Build Coastguard Worker 
135*38e8c45fSAndroid Build Coastguard Worker /**
136*38e8c45fSAndroid Build Coastguard Worker  * Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set by
137*38e8c45fSAndroid Build Coastguard Worker  * the most recent call to updateTexImage.
138*38e8c45fSAndroid Build Coastguard Worker  *
139*38e8c45fSAndroid Build Coastguard Worker  * This transform matrix maps 2D homogeneous texture coordinates of the form (s, t, 0, 1) with s
140*38e8c45fSAndroid Build Coastguard Worker  * and t in the inclusive range [0, 1] to the texture coordinate that should be used to sample
141*38e8c45fSAndroid Build Coastguard Worker  * that location from the texture.  Sampling the texture outside of the range of this transform
142*38e8c45fSAndroid Build Coastguard Worker  * is undefined.
143*38e8c45fSAndroid Build Coastguard Worker  *
144*38e8c45fSAndroid Build Coastguard Worker  * The matrix is stored in column-major order so that it may be passed directly to OpenGL ES via
145*38e8c45fSAndroid Build Coastguard Worker  * the glLoadMatrixf or glUniformMatrix4fv functions.
146*38e8c45fSAndroid Build Coastguard Worker  *
147*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
148*38e8c45fSAndroid Build Coastguard Worker  *
149*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
150*38e8c45fSAndroid Build Coastguard Worker  * \param mtx the array into which the 4x4 matrix will be stored.  The array must have exactly
151*38e8c45fSAndroid Build Coastguard Worker  *     16 elements.
152*38e8c45fSAndroid Build Coastguard Worker  */
153*38e8c45fSAndroid Build Coastguard Worker void ASurfaceTexture_getTransformMatrix(ASurfaceTexture* st, float mtx[16]) __INTRODUCED_IN(28);
154*38e8c45fSAndroid Build Coastguard Worker 
155*38e8c45fSAndroid Build Coastguard Worker /**
156*38e8c45fSAndroid Build Coastguard Worker  * Retrieve the timestamp associated with the texture image set by the most recent call to
157*38e8c45fSAndroid Build Coastguard Worker  * updateTexImage.
158*38e8c45fSAndroid Build Coastguard Worker  *
159*38e8c45fSAndroid Build Coastguard Worker  * This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp
160*38e8c45fSAndroid Build Coastguard Worker  * should be unaffected by time-of-day adjustments, and for a camera should be strictly
161*38e8c45fSAndroid Build Coastguard Worker  * monotonic but for a MediaPlayer may be reset when the position is set.  The
162*38e8c45fSAndroid Build Coastguard Worker  * specific meaning and zero point of the timestamp depends on the source providing images to
163*38e8c45fSAndroid Build Coastguard Worker  * the SurfaceTexture. Unless otherwise specified by the image source, timestamps cannot
164*38e8c45fSAndroid Build Coastguard Worker  * generally be compared across SurfaceTexture instances, or across multiple program
165*38e8c45fSAndroid Build Coastguard Worker  * invocations. It is mostly useful for determining time offsets between subsequent frames.
166*38e8c45fSAndroid Build Coastguard Worker  *
167*38e8c45fSAndroid Build Coastguard Worker  * For EGL/Vulkan producers, this timestamp is the desired present time set with the
168*38e8c45fSAndroid Build Coastguard Worker  * EGL_ANDROID_presentation_time or VK_GOOGLE_display_timing extensions
169*38e8c45fSAndroid Build Coastguard Worker  *
170*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 28.
171*38e8c45fSAndroid Build Coastguard Worker  *
172*38e8c45fSAndroid Build Coastguard Worker  * \param st A ASurfaceTexture reference acquired with ASurfaceTexture_fromSurfaceTexture()
173*38e8c45fSAndroid Build Coastguard Worker  */
174*38e8c45fSAndroid Build Coastguard Worker int64_t ASurfaceTexture_getTimestamp(ASurfaceTexture* st) __INTRODUCED_IN(28);
175*38e8c45fSAndroid Build Coastguard Worker 
176*38e8c45fSAndroid Build Coastguard Worker __END_DECLS
177*38e8c45fSAndroid Build Coastguard Worker 
178*38e8c45fSAndroid Build Coastguard Worker #endif /* ANDROID_NATIVE_SURFACE_TEXTURE_H */
179*38e8c45fSAndroid Build Coastguard Worker 
180*38e8c45fSAndroid Build Coastguard Worker /** @} */
181