blob: e36360c167a85650c99053c9c4b4657a1ebd5c33 [file] [log] [blame]
Jamie Gennis68e4a7a2010-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>
23
24#include <gui/ISurfaceTexture.h>
25
26#include <ui/GraphicBuffer.h>
27
28#include <utils/threads.h>
Jamie Gennisf7acf162011-01-12 18:30:40 -080029#include <utils/Vector.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080030
31#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
32
33namespace android {
34// ----------------------------------------------------------------------------
35
Jamie Gennisf7acf162011-01-12 18:30:40 -080036class IGraphicBufferAlloc;
Mathias Agopian7f3289c2011-05-09 19:08:33 -070037class String8;
Jamie Gennisf7acf162011-01-12 18:30:40 -080038
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080039class SurfaceTexture : public BnSurfaceTexture {
40public:
Jamie Gennis96dcc972011-02-27 14:10:20 -080041 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
Mathias Agopian402ff242011-05-02 19:51:12 -070042 enum {
43 MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1,
44 MIN_SYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS
45 };
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080046 enum { NUM_BUFFER_SLOTS = 32 };
47
Jamie Gennis376590d2011-01-13 14:43:36 -080048 struct FrameAvailableListener : public virtual RefBase {
Jamie Gennisbd5404d2011-06-26 18:27:47 -070049 // onFrameAvailable() is called from queueBuffer() each time an
50 // additional frame becomes available for consumption. This means that
51 // frames that are queued while in asynchronous mode only trigger the
52 // callback if no previous frames are pending. Frames queued while in
53 // synchronous mode always trigger the callback.
54 //
55 // This is called without any lock held and can be called concurrently
56 // by multiple threads.
Jamie Gennis376590d2011-01-13 14:43:36 -080057 virtual void onFrameAvailable() = 0;
58 };
59
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080060 // tex indicates the name OpenGL texture to which images are to be streamed.
61 // This texture name cannot be changed once the SurfaceTexture is created.
Grace Kloba0904d0a2011-06-23 21:21:47 -070062 SurfaceTexture(GLuint tex, bool allowSynchronousMode = true);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080063
64 virtual ~SurfaceTexture();
65
66 // setBufferCount updates the number of available buffer slots. After
67 // calling this all buffer slots are both unallocated and owned by the
68 // SurfaceTexture object (i.e. they are not owned by the client).
69 virtual status_t setBufferCount(int bufferCount);
70
Mathias Agopian0297dca2011-04-25 20:22:14 -070071 virtual sp<GraphicBuffer> requestBuffer(int buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080072
73 // dequeueBuffer gets the next buffer slot index for the client to use. If a
74 // buffer slot is available then that slot index is written to the location
75 // pointed to by the buf argument and a status of OK is returned. If no
76 // slot is available then a status of -EBUSY is returned and buf is
77 // unmodified.
Mathias Agopian0297dca2011-04-25 20:22:14 -070078 virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h,
79 uint32_t format, uint32_t usage);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080080
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -080081 // queueBuffer returns a filled buffer to the SurfaceTexture. In addition, a
82 // timestamp must be provided for the buffer. The timestamp is in
83 // nanoseconds, and must be monotonically increasing. Its other semantics
84 // (zero point, etc) are client-dependent and should be documented by the
85 // client.
86 virtual status_t queueBuffer(int buf, int64_t timestamp);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080087 virtual void cancelBuffer(int buf);
88 virtual status_t setCrop(const Rect& reg);
89 virtual status_t setTransform(uint32_t transform);
90
Mathias Agopianed3894c2011-04-20 14:20:59 -070091 virtual int query(int what, int* value);
92
Mathias Agopian402ff242011-05-02 19:51:12 -070093 // setSynchronousMode set whether dequeueBuffer is synchronous or
94 // asynchronous. In synchronous mode, dequeueBuffer blocks until
95 // a buffer is available, the currently bound buffer can be dequeued and
96 // queued buffers will be retired in order.
97 // The default mode is asynchronous.
98 virtual status_t setSynchronousMode(bool enabled);
99
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800100 // updateTexImage sets the image contents of the target texture to that of
101 // the most recently queued buffer.
102 //
103 // This call may only be made while the OpenGL ES context to which the
104 // target texture belongs is bound to the calling thread.
105 status_t updateTexImage();
106
Mathias Agopian402ff242011-05-02 19:51:12 -0700107 // setBufferCountServer set the buffer count. If the client has requested
108 // a buffer count using setBufferCount, the server-buffer count will
109 // take effect once the client sets the count back to zero.
110 status_t setBufferCountServer(int bufferCount);
111
Jamie Gennisb598fb92011-01-09 16:33:17 -0800112 // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
113 // associated with the texture image set by the most recent call to
114 // updateTexImage.
115 //
116 // This transform matrix maps 2D homogeneous texture coordinates of the form
117 // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
118 // coordinate that should be used to sample that location from the texture.
119 // Sampling the texture outside of the range of this transform is undefined.
120 //
121 // This transform is necessary to compensate for transforms that the stream
122 // content producer may implicitly apply to the content. By forcing users of
123 // a SurfaceTexture to apply this transform we avoid performing an extra
124 // copy of the data that would be needed to hide the transform from the
125 // user.
126 //
127 // The matrix is stored in column-major order so that it may be passed
128 // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
129 // functions.
130 void getTransformMatrix(float mtx[16]);
131
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800132 // getTimestamp retrieves the timestamp associated with the texture image
133 // set by the most recent call to updateTexImage.
134 //
135 // The timestamp is in nanoseconds, and is monotonically increasing. Its
136 // other semantics (zero point, etc) are source-dependent and should be
137 // documented by the source.
138 int64_t getTimestamp();
139
Jamie Gennis376590d2011-01-13 14:43:36 -0800140 // setFrameAvailableListener sets the listener object that will be notified
141 // when a new frame becomes available.
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700142 void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
Jamie Gennis376590d2011-01-13 14:43:36 -0800143
Jamie Gennis83bac212011-02-02 15:31:47 -0800144 // getAllocator retrieves the binder object that must be referenced as long
145 // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
146 // Holding this binder reference prevents SurfaceFlinger from freeing the
147 // buffers before the client is done with them.
148 sp<IBinder> getAllocator();
149
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700150 // setDefaultBufferSize is used to set the size of buffers returned by
151 // requestBuffers when a with and height of zero is requested.
152 // A call to setDefaultBufferSize() may trigger requestBuffers() to
153 // be called from the client.
154 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
155
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700156 // getCurrentBuffer returns the buffer associated with the current image.
157 sp<GraphicBuffer> getCurrentBuffer() const;
158
159 // getCurrentTextureTarget returns the texture target of the current
160 // texture as returned by updateTexImage().
161 GLenum getCurrentTextureTarget() const;
162
163 // getCurrentCrop returns the cropping rectangle of the current buffer
164 Rect getCurrentCrop() const;
165
166 // getCurrentTransform returns the transform of the current buffer
167 uint32_t getCurrentTransform() const;
168
Mathias Agopian7f3289c2011-05-09 19:08:33 -0700169 // dump our state in a String
170 void dump(String8& result) const;
171 void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
172
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700173protected:
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800174
175 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
176 // all slots.
177 void freeAllBuffers();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700178 static bool isExternalFormat(uint32_t format);
179 static GLenum getTextureTarget(uint32_t format);
180
181private:
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800182
183 // createImage creates a new EGLImage from a GraphicBuffer.
184 EGLImageKHR createImage(EGLDisplay dpy,
185 const sp<GraphicBuffer>& graphicBuffer);
186
Mathias Agopian402ff242011-05-02 19:51:12 -0700187 status_t setBufferCountServerLocked(int bufferCount);
188
Jamie Genniseadfb672011-06-12 17:03:06 -0700189 // computeCurrentTransformMatrix computes the transform matrix for the
190 // current texture. It uses mCurrentTransform and the current GraphicBuffer
191 // to compute this matrix and stores it in mCurrentTransformMatrix.
192 void computeCurrentTransformMatrix();
193
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800194 enum { INVALID_BUFFER_SLOT = -1 };
195
196 struct BufferSlot {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700197
198 BufferSlot()
199 : mEglImage(EGL_NO_IMAGE_KHR),
200 mEglDisplay(EGL_NO_DISPLAY),
201 mBufferState(BufferSlot::FREE),
202 mRequestBufferCalled(false),
Jamie Gennisa2187152011-05-19 13:33:00 -0700203 mTransform(0),
204 mTimestamp(0) {
205 mCrop.makeInvalid();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700206 }
207
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800208 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
209 // if no buffer has been allocated.
210 sp<GraphicBuffer> mGraphicBuffer;
211
212 // mEglImage is the EGLImage created from mGraphicBuffer.
213 EGLImageKHR mEglImage;
214
215 // mEglDisplay is the EGLDisplay used to create mEglImage.
216 EGLDisplay mEglDisplay;
217
Jamie Gennisa2187152011-05-19 13:33:00 -0700218 // BufferState represents the different states in which a buffer slot
219 // can be.
220 enum BufferState {
221 // FREE indicates that the buffer is not currently being used and
222 // will not be used in the future until it gets dequeued and
223 // subseqently queued by the client.
224 FREE = 0,
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700225
Jamie Gennisa2187152011-05-19 13:33:00 -0700226 // DEQUEUED indicates that the buffer has been dequeued by the
227 // client, but has not yet been queued or canceled. The buffer is
228 // considered 'owned' by the client, and the server should not use
229 // it for anything.
230 //
231 // Note that when in synchronous-mode (mSynchronousMode == true),
232 // the buffer that's currently attached to the texture may be
233 // dequeued by the client. That means that the current buffer can
234 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
235 // however, the current buffer is always in the QUEUED state.
236 DEQUEUED = 1,
237
238 // QUEUED indicates that the buffer has been queued by the client,
239 // and has not since been made available for the client to dequeue.
240 // Attaching the buffer to the texture does NOT transition the
241 // buffer away from the QUEUED state. However, in Synchronous mode
242 // the current buffer may be dequeued by the client under some
243 // circumstances. See the note about the current buffer in the
244 // documentation for DEQUEUED.
245 QUEUED = 2,
246 };
247
248 // mBufferState is the current state of this buffer slot.
249 BufferState mBufferState;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700250
251 // mRequestBufferCalled is used for validating that the client did
252 // call requestBuffer() when told to do so. Technically this is not
253 // needed but useful for debugging and catching client bugs.
254 bool mRequestBufferCalled;
255
Jamie Gennisa2187152011-05-19 13:33:00 -0700256 // mCrop is the current crop rectangle for this buffer slot. This gets
257 // set to mNextCrop each time queueBuffer gets called for this buffer.
258 Rect mCrop;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700259
Jamie Gennisa2187152011-05-19 13:33:00 -0700260 // mTransform is the current transform flags for this buffer slot. This
261 // gets set to mNextTransform each time queueBuffer gets called for this
262 // slot.
263 uint32_t mTransform;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700264
Jamie Gennisa2187152011-05-19 13:33:00 -0700265 // mTimestamp is the current timestamp for this buffer slot. This gets
266 // to set by queueBuffer each time this slot is queued.
267 int64_t mTimestamp;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800268 };
269
270 // mSlots is the array of buffer slots that must be mirrored on the client
271 // side. This allows buffer ownership to be transferred between the client
272 // and server without sending a GraphicBuffer over binder. The entire array
273 // is initialized to NULL at construction time, and buffers are allocated
274 // for a slot when requestBuffer is called with that slot's index.
275 BufferSlot mSlots[NUM_BUFFER_SLOTS];
276
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700277 // mDefaultWidth holds the default width of allocated buffers. It is used
278 // in requestBuffers() if a width and height of zero is specified.
279 uint32_t mDefaultWidth;
280
281 // mDefaultHeight holds the default height of allocated buffers. It is used
282 // in requestBuffers() if a width and height of zero is specified.
283 uint32_t mDefaultHeight;
284
285 // mPixelFormat holds the pixel format of allocated buffers. It is used
286 // in requestBuffers() if a format of zero is specified.
287 uint32_t mPixelFormat;
288
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800289 // mBufferCount is the number of buffer slots that the client and server
Mathias Agopian402ff242011-05-02 19:51:12 -0700290 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
291 // by calling setBufferCount or setBufferCountServer
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800292 int mBufferCount;
293
Jamie Gennis4e03d322011-06-12 14:11:39 -0700294 // mClientBufferCount is the number of buffer slots requested by the client.
295 // The default is zero, which means the client doesn't care how many buffers
296 // there is.
Mathias Agopian402ff242011-05-02 19:51:12 -0700297 int mClientBufferCount;
298
299 // mServerBufferCount buffer count requested by the server-side
300 int mServerBufferCount;
301
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800302 // mCurrentTexture is the buffer slot index of the buffer that is currently
Jamie Gennisd369dc42011-01-09 13:25:39 -0800303 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
304 // indicating that no buffer slot is currently bound to the texture. Note,
305 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
306 // that no buffer is bound to the texture. A call to setBufferCount will
307 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800308 int mCurrentTexture;
309
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700310 // mCurrentTextureTarget is the GLES texture target to be used with the
311 // current texture.
312 GLenum mCurrentTextureTarget;
313
Jamie Gennisf7acf162011-01-12 18:30:40 -0800314 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
315 // possible that this buffer is not associated with any buffer slot, so we
316 // must track it separately in order to properly use
317 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
318 sp<GraphicBuffer> mCurrentTextureBuf;
319
Jamie Gennisb598fb92011-01-09 16:33:17 -0800320 // mCurrentCrop is the crop rectangle that applies to the current texture.
321 // It gets set to mLastQueuedCrop each time updateTexImage is called.
322 Rect mCurrentCrop;
323
324 // mCurrentTransform is the transform identifier for the current texture. It
325 // gets set to mLastQueuedTransform each time updateTexImage is called.
326 uint32_t mCurrentTransform;
327
Jamie Genniseadfb672011-06-12 17:03:06 -0700328 // mCurrentTransformMatrix is the transform matrix for the current texture.
329 // It gets computed by computeTransformMatrix each time updateTexImage is
330 // called.
331 float mCurrentTransformMatrix[16];
332
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800333 // mCurrentTimestamp is the timestamp for the current texture. It
334 // gets set to mLastQueuedTimestamp each time updateTexImage is called.
335 int64_t mCurrentTimestamp;
336
Jamie Gennisb598fb92011-01-09 16:33:17 -0800337 // mNextCrop is the crop rectangle that will be used for the next buffer
338 // that gets queued. It is set by calling setCrop.
339 Rect mNextCrop;
340
341 // mNextTransform is the transform identifier that will be used for the next
342 // buffer that gets queued. It is set by calling setTransform.
343 uint32_t mNextTransform;
344
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800345 // mTexName is the name of the OpenGL texture to which streamed images will
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700346 // be bound when updateTexImage is called. It is set at construction time
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800347 // changed with a call to setTexName.
348 const GLuint mTexName;
349
Jamie Gennisf7acf162011-01-12 18:30:40 -0800350 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
351 // allocate new GraphicBuffer objects.
352 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
353
Jamie Gennis376590d2011-01-13 14:43:36 -0800354 // mFrameAvailableListener is the listener object that will be called when a
355 // new frame becomes available. If it is not NULL it will be called from
356 // queueBuffer.
357 sp<FrameAvailableListener> mFrameAvailableListener;
358
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700359 // mSynchronousMode whether we're in synchronous mode or not
360 bool mSynchronousMode;
361
Grace Kloba0904d0a2011-06-23 21:21:47 -0700362 // mAllowSynchronousMode whether we allow synchronous mode or not
363 const bool mAllowSynchronousMode;
364
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700365 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
366 mutable Condition mDequeueCondition;
367
368 // mQueue is a FIFO of queued buffers used in synchronous mode
369 typedef Vector<int> Fifo;
370 Fifo mQueue;
371
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800372 // mMutex is the mutex used to prevent concurrent access to the member
373 // variables of SurfaceTexture objects. It must be locked whenever the
374 // member variables are accessed.
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700375 mutable Mutex mMutex;
Jamie Genniseadfb672011-06-12 17:03:06 -0700376
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800377};
378
379// ----------------------------------------------------------------------------
380}; // namespace android
381
382#endif // ANDROID_GUI_SURFACETEXTURE_H