blob: f8dc3ace2b26b0e80f368ef33354edd8b3cf91c3 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_GUI_SURFACETEXTURE_H
18#define ANDROID_GUI_SURFACETEXTURE_H
19
20#include <EGL/egl.h>
21#include <EGL/eglext.h>
22#include <GLES2/gl2.h>
Jamie Gennisfb1b5a22011-09-28 12:13:31 -070023#include <GLES2/gl2ext.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080024
25#include <gui/ISurfaceTexture.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080026#include <gui/BufferQueue.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080027
28#include <ui/GraphicBuffer.h>
29
Jamie Gennisfa28c352011-09-16 17:30:26 -070030#include <utils/String8.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080031#include <utils/Vector.h>
Jamie Gennisfa28c352011-09-16 17:30:26 -070032#include <utils/threads.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080033
34#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
35
36namespace android {
37// ----------------------------------------------------------------------------
38
Daniel Lam6b091c52012-01-22 15:26:27 -080039
Mathias Agopian68c77942011-05-09 19:08:33 -070040class String8;
Jamie Gennis9a78c902011-01-12 18:30:40 -080041
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070042class SurfaceTexture : public virtual RefBase,
43 protected BufferQueue::ConsumerListener {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044public:
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070045 struct FrameAvailableListener : public virtual RefBase {
46 // onFrameAvailable() is called each time an additional frame becomes
47 // available for consumption. This means that frames that are queued
48 // while in asynchronous mode only trigger the callback if no previous
49 // frames are pending. Frames queued while in synchronous mode always
50 // trigger the callback.
51 //
52 // This is called without any lock held and can be called concurrently
53 // by multiple threads.
54 virtual void onFrameAvailable() = 0;
55 };
Jamie Gennisc4d4aea2011-01-13 14:43:36 -080056
Jamie Gennis86edf4f2011-11-14 14:51:01 -080057 // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the
58 // name of the OpenGL ES texture to which images are to be streamed. This
59 // texture name cannot be changed once the SurfaceTexture is created.
60 // allowSynchronousMode specifies whether or not synchronous mode can be
61 // enabled. texTarget specifies the OpenGL ES texture target to which the
62 // texture will be bound in updateTexImage. useFenceSync specifies whether
63 // fences should be used to synchronize access to buffers if that behavior
Daniel Lamb2675792012-02-23 14:35:13 -080064 // is enabled at compile-time. A custom bufferQueue can be specified
65 // if behavior for queue/dequeue/connect etc needs to be customized.
66 // Otherwise a default BufferQueue will be created and used.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -070067 SurfaceTexture(GLuint tex, bool allowSynchronousMode = true,
Daniel Lamb2675792012-02-23 14:35:13 -080068 GLenum texTarget = GL_TEXTURE_EXTERNAL_OES, bool useFenceSync = true,
69 const sp<BufferQueue> &bufferQueue = 0);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080070
71 virtual ~SurfaceTexture();
72
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073 // updateTexImage sets the image contents of the target texture to that of
74 // the most recently queued buffer.
75 //
76 // This call may only be made while the OpenGL ES context to which the
77 // target texture belongs is bound to the calling thread.
78 status_t updateTexImage();
79
Mathias Agopian80727112011-05-02 19:51:12 -070080 // setBufferCountServer set the buffer count. If the client has requested
81 // a buffer count using setBufferCount, the server-buffer count will
82 // take effect once the client sets the count back to zero.
83 status_t setBufferCountServer(int bufferCount);
84
Jamie Gennisf238e282011-01-09 16:33:17 -080085 // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
86 // associated with the texture image set by the most recent call to
87 // updateTexImage.
88 //
89 // This transform matrix maps 2D homogeneous texture coordinates of the form
90 // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
91 // coordinate that should be used to sample that location from the texture.
92 // Sampling the texture outside of the range of this transform is undefined.
93 //
94 // This transform is necessary to compensate for transforms that the stream
95 // content producer may implicitly apply to the content. By forcing users of
96 // a SurfaceTexture to apply this transform we avoid performing an extra
97 // copy of the data that would be needed to hide the transform from the
98 // user.
99 //
100 // The matrix is stored in column-major order so that it may be passed
101 // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
102 // functions.
103 void getTransformMatrix(float mtx[16]);
104
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800105 // getTimestamp retrieves the timestamp associated with the texture image
106 // set by the most recent call to updateTexImage.
107 //
108 // The timestamp is in nanoseconds, and is monotonically increasing. Its
109 // other semantics (zero point, etc) are source-dependent and should be
110 // documented by the source.
111 int64_t getTimestamp();
112
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800113 // setFrameAvailableListener sets the listener object that will be notified
114 // when a new frame becomes available.
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700115 void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800116
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800117 // getAllocator retrieves the binder object that must be referenced as long
118 // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
119 // Holding this binder reference prevents SurfaceFlinger from freeing the
120 // buffers before the client is done with them.
121 sp<IBinder> getAllocator();
122
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700123 // setDefaultBufferSize is used to set the size of buffers returned by
124 // requestBuffers when a with and height of zero is requested.
125 // A call to setDefaultBufferSize() may trigger requestBuffers() to
126 // be called from the client.
Mathias Agopian194c76c2011-11-10 14:34:26 -0800127 // The width and height parameters must be no greater than the minimum of
128 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
129 // An error due to invalid dimensions might not be reported until
130 // updateTexImage() is called.
131 status_t setDefaultBufferSize(uint32_t width, uint32_t height);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700132
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700133 // getCurrentBuffer returns the buffer associated with the current image.
134 sp<GraphicBuffer> getCurrentBuffer() const;
135
136 // getCurrentTextureTarget returns the texture target of the current
137 // texture as returned by updateTexImage().
138 GLenum getCurrentTextureTarget() const;
139
140 // getCurrentCrop returns the cropping rectangle of the current buffer
141 Rect getCurrentCrop() const;
142
143 // getCurrentTransform returns the transform of the current buffer
144 uint32_t getCurrentTransform() const;
145
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700146 // getCurrentScalingMode returns the scaling mode of the current buffer
147 uint32_t getCurrentScalingMode() const;
148
Jamie Gennis59769462011-11-19 18:04:43 -0800149 // isSynchronousMode returns whether the SurfaceTexture is currently in
150 // synchronous mode.
151 bool isSynchronousMode() const;
152
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700153 // abandon frees all the buffers and puts the SurfaceTexture into the
154 // 'abandoned' state. Once put in this state the SurfaceTexture can never
155 // leave it. When in the 'abandoned' state, all methods of the
156 // ISurfaceTexture interface will fail with the NO_INIT error.
157 //
158 // Note that while calling this method causes all the buffers to be freed
159 // from the perspective of the the SurfaceTexture, if there are additional
160 // references on the buffers (e.g. if a buffer is referenced by a client or
161 // by OpenGL ES as a texture) then those buffer will remain allocated.
162 void abandon();
163
Jamie Gennisfa28c352011-09-16 17:30:26 -0700164 // set the name of the SurfaceTexture that will be used to identify it in
165 // log messages.
166 void setName(const String8& name);
167
Daniel Lamb2675792012-02-23 14:35:13 -0800168 // These functions call the corresponding BufferQueue implementation
169 // so the refactoring can proceed smoothly
170 status_t setDefaultBufferFormat(uint32_t defaultFormat);
171 status_t setConsumerUsageBits(uint32_t usage);
172 status_t setTransformHint(uint32_t hint);
173 virtual status_t setSynchronousMode(bool enabled);
174 virtual status_t setBufferCount(int bufferCount);
175 virtual status_t connect(int api,
176 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
177
178 sp<BufferQueue> getBufferQueue() const;
179
Mathias Agopian68c77942011-05-09 19:08:33 -0700180 // dump our state in a String
Daniel Lameae59d22012-01-22 15:26:27 -0800181 virtual void dump(String8& result) const;
182 virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
Mathias Agopian68c77942011-05-09 19:08:33 -0700183
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700184protected:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800185
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700186 // Implementation of the BufferQueue::ConsumerListener interface. These
187 // calls are used to notify the SurfaceTexture of asynchronous events in the
188 // BufferQueue.
189 virtual void onFrameAvailable();
190 virtual void onBuffersReleased();
191
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700192 static bool isExternalFormat(uint32_t format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700193
194private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195
196 // createImage creates a new EGLImage from a GraphicBuffer.
197 EGLImageKHR createImage(EGLDisplay dpy,
198 const sp<GraphicBuffer>& graphicBuffer);
199
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700200 // freeBufferLocked frees up the given buffer slot. If the slot has been
201 // initialized this will release the reference to the GraphicBuffer in that
202 // slot and destroy the EGLImage in that slot. Otherwise it has no effect.
203 //
204 // This method must be called with mMutex locked.
205 void freeBufferLocked(int slotIndex);
206
Jamie Gennis736aa952011-06-12 17:03:06 -0700207 // computeCurrentTransformMatrix computes the transform matrix for the
208 // current texture. It uses mCurrentTransform and the current GraphicBuffer
209 // to compute this matrix and stores it in mCurrentTransformMatrix.
210 void computeCurrentTransformMatrix();
211
Jamie Gennis9a78c902011-01-12 18:30:40 -0800212 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
213 // possible that this buffer is not associated with any buffer slot, so we
Jamie Gennis29c87022011-07-19 12:11:52 -0700214 // must track it separately in order to support the getCurrentBuffer method.
Jamie Gennis9a78c902011-01-12 18:30:40 -0800215 sp<GraphicBuffer> mCurrentTextureBuf;
216
Jamie Gennisf238e282011-01-09 16:33:17 -0800217 // mCurrentCrop is the crop rectangle that applies to the current texture.
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700218 // It gets set each time updateTexImage is called.
Jamie Gennisf238e282011-01-09 16:33:17 -0800219 Rect mCurrentCrop;
220
221 // mCurrentTransform is the transform identifier for the current texture. It
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700222 // gets set each time updateTexImage is called.
Jamie Gennisf238e282011-01-09 16:33:17 -0800223 uint32_t mCurrentTransform;
224
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700225 // mCurrentScalingMode is the scaling mode for the current texture. It gets
226 // set to each time updateTexImage is called.
227 uint32_t mCurrentScalingMode;
228
Jamie Gennis736aa952011-06-12 17:03:06 -0700229 // mCurrentTransformMatrix is the transform matrix for the current texture.
230 // It gets computed by computeTransformMatrix each time updateTexImage is
231 // called.
232 float mCurrentTransformMatrix[16];
233
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800234 // mCurrentTimestamp is the timestamp for the current texture. It
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700235 // gets set each time updateTexImage is called.
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800236 int64_t mCurrentTimestamp;
237
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800238 // mTexName is the name of the OpenGL texture to which streamed images will
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700239 // be bound when updateTexImage is called. It is set at construction time
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800240 // changed with a call to setTexName.
241 const GLuint mTexName;
242
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800243 // mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync
244 // extension should be used to prevent buffers from being dequeued before
245 // it's safe for them to be written. It gets set at construction time and
246 // never changes.
247 const bool mUseFenceSync;
248
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700249 // mTexTarget is the GL texture target with which the GL texture object is
250 // associated. It is set in the constructor and never changed. It is
251 // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android
252 // Browser. In that case it is set to GL_TEXTURE_2D to allow
253 // glCopyTexSubImage to read from the texture. This is a hack to work
254 // around a GL driver limitation on the number of FBO attachments, which the
255 // browser's tile cache exceeds.
256 const GLenum mTexTarget;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600257
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700258 // EGLSlot contains the information and object references that
259 // SurfaceTexture maintains about a BufferQueue buffer slot.
Daniel Lameae59d22012-01-22 15:26:27 -0800260 struct EGLSlot {
261 EGLSlot()
262 : mEglImage(EGL_NO_IMAGE_KHR),
263 mEglDisplay(EGL_NO_DISPLAY),
264 mFence(EGL_NO_SYNC_KHR) {
265 }
266
267 sp<GraphicBuffer> mGraphicBuffer;
268
269 // mEglImage is the EGLImage created from mGraphicBuffer.
270 EGLImageKHR mEglImage;
271
272 // mEglDisplay is the EGLDisplay used to create mEglImage.
273 EGLDisplay mEglDisplay;
274
275 // mFence is the EGL sync object that must signal before the buffer
276 // associated with this buffer slot may be dequeued. It is initialized
277 // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
278 // on a compile-time option) set to a new sync object in updateTexImage.
279 EGLSyncKHR mFence;
280 };
281
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700282 // mEGLSlots stores the buffers that have been allocated by the BufferQueue
283 // for each buffer slot. It is initialized to null pointers, and gets
284 // filled in with the result of BufferQueue::acquire when the
285 // client dequeues a buffer from a
286 // slot that has not yet been used. The buffer allocated to a slot will also
287 // be replaced if the requested buffer usage or geometry differs from that
288 // of the buffer allocated to a slot.
Daniel Lamb2675792012-02-23 14:35:13 -0800289 EGLSlot mEGLSlots[BufferQueue::NUM_BUFFER_SLOTS];
Daniel Lameae59d22012-01-22 15:26:27 -0800290
291 // mAbandoned indicates that the BufferQueue will no longer be used to
292 // consume images buffers pushed to it using the ISurfaceTexture interface.
293 // It is initialized to false, and set to true in the abandon method. A
294 // BufferQueue that has been abandoned will return the NO_INIT error from
295 // all ISurfaceTexture methods capable of returning an error.
296 bool mAbandoned;
297
298 // mName is a string used to identify the SurfaceTexture in log messages.
299 // It can be set by the setName method.
300 String8 mName;
301
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700302 // mFrameAvailableListener is the listener object that will be called when a
303 // new frame becomes available. If it is not NULL it will be called from
304 // queueBuffer.
305 sp<FrameAvailableListener> mFrameAvailableListener;
Daniel Lameae59d22012-01-22 15:26:27 -0800306
307 // mCurrentTexture is the buffer slot index of the buffer that is currently
308 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
309 // indicating that no buffer slot is currently bound to the texture. Note,
310 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
311 // that no buffer is bound to the texture. A call to setBufferCount will
312 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
313 int mCurrentTexture;
314
Daniel Lamb2675792012-02-23 14:35:13 -0800315 // The SurfaceTexture has-a BufferQueue and is responsible for creating this object
316 // if none is supplied
317 sp<BufferQueue> mBufferQueue;
318
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700319 // mMutex is the mutex used to prevent concurrent access to the member
320 // variables of SurfaceTexture objects. It must be locked whenever the
321 // member variables are accessed.
322 mutable Mutex mMutex;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800323};
324
325// ----------------------------------------------------------------------------
326}; // namespace android
327
328#endif // ANDROID_GUI_SURFACETEXTURE_H