blob: 62c3a4a811ce09c4ce3ff51fa378bb024ccb2e45 [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>
23
24#include <gui/ISurfaceTexture.h>
25
26#include <ui/GraphicBuffer.h>
27
28#include <utils/threads.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080029#include <utils/Vector.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080030
31#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
32
33namespace android {
34// ----------------------------------------------------------------------------
35
Jamie Gennis9a78c902011-01-12 18:30:40 -080036class IGraphicBufferAlloc;
Mathias Agopian68c77942011-05-09 19:08:33 -070037class String8;
Jamie Gennis9a78c902011-01-12 18:30:40 -080038
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080039class SurfaceTexture : public BnSurfaceTexture {
40public:
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080041 enum { MIN_UNDEQUEUED_BUFFERS = 2 };
Mathias Agopian80727112011-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 Gennis8ba32fa2010-12-20 11:27:26 -080046 enum { NUM_BUFFER_SLOTS = 32 };
47
Jamie Gennisc4d4aea2011-01-13 14:43:36 -080048 struct FrameAvailableListener : public virtual RefBase {
Mathias Agopiancf46eb92011-05-11 15:05:29 -070049 // onFrameAvailable() is called from queueBuffer() is the FIFO is
50 // empty. You can use SurfaceTexture::getQueuedCount() to
51 // figure out if there are more frames waiting.
52 // This is called without any lock held can be called concurrently by
53 // multiple threads.
Jamie Gennisc4d4aea2011-01-13 14:43:36 -080054 virtual void onFrameAvailable() = 0;
55 };
56
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057 // tex indicates the name OpenGL texture to which images are to be streamed.
58 // This texture name cannot be changed once the SurfaceTexture is created.
59 SurfaceTexture(GLuint tex);
60
61 virtual ~SurfaceTexture();
62
63 // setBufferCount updates the number of available buffer slots. After
64 // calling this all buffer slots are both unallocated and owned by the
65 // SurfaceTexture object (i.e. they are not owned by the client).
66 virtual status_t setBufferCount(int bufferCount);
67
Mathias Agopianc04f1532011-04-25 20:22:14 -070068 virtual sp<GraphicBuffer> requestBuffer(int buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069
70 // dequeueBuffer gets the next buffer slot index for the client to use. If a
71 // buffer slot is available then that slot index is written to the location
72 // pointed to by the buf argument and a status of OK is returned. If no
73 // slot is available then a status of -EBUSY is returned and buf is
74 // unmodified.
Mathias Agopianc04f1532011-04-25 20:22:14 -070075 virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h,
76 uint32_t format, uint32_t usage);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080077
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080078 // queueBuffer returns a filled buffer to the SurfaceTexture. In addition, a
79 // timestamp must be provided for the buffer. The timestamp is in
80 // nanoseconds, and must be monotonically increasing. Its other semantics
81 // (zero point, etc) are client-dependent and should be documented by the
82 // client.
83 virtual status_t queueBuffer(int buf, int64_t timestamp);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080084 virtual void cancelBuffer(int buf);
85 virtual status_t setCrop(const Rect& reg);
86 virtual status_t setTransform(uint32_t transform);
87
Mathias Agopianeafabcd2011-04-20 14:20:59 -070088 virtual int query(int what, int* value);
89
Mathias Agopian80727112011-05-02 19:51:12 -070090 // setSynchronousMode set whether dequeueBuffer is synchronous or
91 // asynchronous. In synchronous mode, dequeueBuffer blocks until
92 // a buffer is available, the currently bound buffer can be dequeued and
93 // queued buffers will be retired in order.
94 // The default mode is asynchronous.
95 virtual status_t setSynchronousMode(bool enabled);
96
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080097 // updateTexImage sets the image contents of the target texture to that of
98 // the most recently queued buffer.
99 //
100 // This call may only be made while the OpenGL ES context to which the
101 // target texture belongs is bound to the calling thread.
102 status_t updateTexImage();
103
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700104 // getqueuedCount returns the number of queued frames waiting in the
105 // FIFO. In asynchronous mode, this always returns 0 or 1 since
106 // frames are not accumulating in the FIFO.
107 size_t getQueuedCount() const;
108
Mathias Agopian80727112011-05-02 19:51:12 -0700109 // setBufferCountServer set the buffer count. If the client has requested
110 // a buffer count using setBufferCount, the server-buffer count will
111 // take effect once the client sets the count back to zero.
112 status_t setBufferCountServer(int bufferCount);
113
Jamie Gennisf238e282011-01-09 16:33:17 -0800114 // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
115 // associated with the texture image set by the most recent call to
116 // updateTexImage.
117 //
118 // This transform matrix maps 2D homogeneous texture coordinates of the form
119 // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
120 // coordinate that should be used to sample that location from the texture.
121 // Sampling the texture outside of the range of this transform is undefined.
122 //
123 // This transform is necessary to compensate for transforms that the stream
124 // content producer may implicitly apply to the content. By forcing users of
125 // a SurfaceTexture to apply this transform we avoid performing an extra
126 // copy of the data that would be needed to hide the transform from the
127 // user.
128 //
129 // The matrix is stored in column-major order so that it may be passed
130 // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
131 // functions.
132 void getTransformMatrix(float mtx[16]);
133
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800134 // getTimestamp retrieves the timestamp associated with the texture image
135 // set by the most recent call to updateTexImage.
136 //
137 // The timestamp is in nanoseconds, and is monotonically increasing. Its
138 // other semantics (zero point, etc) are source-dependent and should be
139 // documented by the source.
140 int64_t getTimestamp();
141
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800142 // setFrameAvailableListener sets the listener object that will be notified
143 // when a new frame becomes available.
144 void setFrameAvailableListener(const sp<FrameAvailableListener>& l);
145
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800146 // getAllocator retrieves the binder object that must be referenced as long
147 // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
148 // Holding this binder reference prevents SurfaceFlinger from freeing the
149 // buffers before the client is done with them.
150 sp<IBinder> getAllocator();
151
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700152 // setDefaultBufferSize is used to set the size of buffers returned by
153 // requestBuffers when a with and height of zero is requested.
154 // A call to setDefaultBufferSize() may trigger requestBuffers() to
155 // be called from the client.
156 status_t setDefaultBufferSize(uint32_t w, uint32_t h);
157
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700158 // getCurrentBuffer returns the buffer associated with the current image.
159 sp<GraphicBuffer> getCurrentBuffer() const;
160
161 // getCurrentTextureTarget returns the texture target of the current
162 // texture as returned by updateTexImage().
163 GLenum getCurrentTextureTarget() const;
164
165 // getCurrentCrop returns the cropping rectangle of the current buffer
166 Rect getCurrentCrop() const;
167
168 // getCurrentTransform returns the transform of the current buffer
169 uint32_t getCurrentTransform() const;
170
Mathias Agopian68c77942011-05-09 19:08:33 -0700171 // dump our state in a String
172 void dump(String8& result) const;
173 void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
174
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700175protected:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176
177 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
178 // all slots.
179 void freeAllBuffers();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700180 static bool isExternalFormat(uint32_t format);
181 static GLenum getTextureTarget(uint32_t format);
182
183private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800184
185 // createImage creates a new EGLImage from a GraphicBuffer.
186 EGLImageKHR createImage(EGLDisplay dpy,
187 const sp<GraphicBuffer>& graphicBuffer);
188
Mathias Agopian80727112011-05-02 19:51:12 -0700189 status_t setBufferCountServerLocked(int bufferCount);
190
Jamie Gennis736aa952011-06-12 17:03:06 -0700191 // computeCurrentTransformMatrix computes the transform matrix for the
192 // current texture. It uses mCurrentTransform and the current GraphicBuffer
193 // to compute this matrix and stores it in mCurrentTransformMatrix.
194 void computeCurrentTransformMatrix();
195
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800196 enum { INVALID_BUFFER_SLOT = -1 };
197
198 struct BufferSlot {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700199
200 BufferSlot()
201 : mEglImage(EGL_NO_IMAGE_KHR),
202 mEglDisplay(EGL_NO_DISPLAY),
203 mBufferState(BufferSlot::FREE),
204 mRequestBufferCalled(false),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700205 mTransform(0),
206 mTimestamp(0) {
207 mCrop.makeInvalid();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700208 }
209
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
211 // if no buffer has been allocated.
212 sp<GraphicBuffer> mGraphicBuffer;
213
214 // mEglImage is the EGLImage created from mGraphicBuffer.
215 EGLImageKHR mEglImage;
216
217 // mEglDisplay is the EGLDisplay used to create mEglImage.
218 EGLDisplay mEglDisplay;
219
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700220 // BufferState represents the different states in which a buffer slot
221 // can be.
222 enum BufferState {
223 // FREE indicates that the buffer is not currently being used and
224 // will not be used in the future until it gets dequeued and
225 // subseqently queued by the client.
226 FREE = 0,
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700227
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700228 // DEQUEUED indicates that the buffer has been dequeued by the
229 // client, but has not yet been queued or canceled. The buffer is
230 // considered 'owned' by the client, and the server should not use
231 // it for anything.
232 //
233 // Note that when in synchronous-mode (mSynchronousMode == true),
234 // the buffer that's currently attached to the texture may be
235 // dequeued by the client. That means that the current buffer can
236 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
237 // however, the current buffer is always in the QUEUED state.
238 DEQUEUED = 1,
239
240 // QUEUED indicates that the buffer has been queued by the client,
241 // and has not since been made available for the client to dequeue.
242 // Attaching the buffer to the texture does NOT transition the
243 // buffer away from the QUEUED state. However, in Synchronous mode
244 // the current buffer may be dequeued by the client under some
245 // circumstances. See the note about the current buffer in the
246 // documentation for DEQUEUED.
247 QUEUED = 2,
248 };
249
250 // mBufferState is the current state of this buffer slot.
251 BufferState mBufferState;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700252
253 // mRequestBufferCalled is used for validating that the client did
254 // call requestBuffer() when told to do so. Technically this is not
255 // needed but useful for debugging and catching client bugs.
256 bool mRequestBufferCalled;
257
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700258 // mCrop is the current crop rectangle for this buffer slot. This gets
259 // set to mNextCrop each time queueBuffer gets called for this buffer.
260 Rect mCrop;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700261
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700262 // mTransform is the current transform flags for this buffer slot. This
263 // gets set to mNextTransform each time queueBuffer gets called for this
264 // slot.
265 uint32_t mTransform;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700266
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700267 // mTimestamp is the current timestamp for this buffer slot. This gets
268 // to set by queueBuffer each time this slot is queued.
269 int64_t mTimestamp;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800270 };
271
272 // mSlots is the array of buffer slots that must be mirrored on the client
273 // side. This allows buffer ownership to be transferred between the client
274 // and server without sending a GraphicBuffer over binder. The entire array
275 // is initialized to NULL at construction time, and buffers are allocated
276 // for a slot when requestBuffer is called with that slot's index.
277 BufferSlot mSlots[NUM_BUFFER_SLOTS];
278
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700279 // mDefaultWidth holds the default width of allocated buffers. It is used
280 // in requestBuffers() if a width and height of zero is specified.
281 uint32_t mDefaultWidth;
282
283 // mDefaultHeight holds the default height of allocated buffers. It is used
284 // in requestBuffers() if a width and height of zero is specified.
285 uint32_t mDefaultHeight;
286
287 // mPixelFormat holds the pixel format of allocated buffers. It is used
288 // in requestBuffers() if a format of zero is specified.
289 uint32_t mPixelFormat;
290
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800291 // mBufferCount is the number of buffer slots that the client and server
Mathias Agopian80727112011-05-02 19:51:12 -0700292 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
293 // by calling setBufferCount or setBufferCountServer
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800294 int mBufferCount;
295
Mathias Agopian80727112011-05-02 19:51:12 -0700296 // mRequestedBufferCount is the number of buffer slots requested by the
297 // client. The default is zero, which means the client doesn't care how
298 // many buffers there is.
299 int mClientBufferCount;
300
301 // mServerBufferCount buffer count requested by the server-side
302 int mServerBufferCount;
303
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304 // mCurrentTexture is the buffer slot index of the buffer that is currently
Jamie Gennis67eedd72011-01-09 13:25:39 -0800305 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
306 // indicating that no buffer slot is currently bound to the texture. Note,
307 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
308 // that no buffer is bound to the texture. A call to setBufferCount will
309 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800310 int mCurrentTexture;
311
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700312 // mCurrentTextureTarget is the GLES texture target to be used with the
313 // current texture.
314 GLenum mCurrentTextureTarget;
315
Jamie Gennis9a78c902011-01-12 18:30:40 -0800316 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
317 // possible that this buffer is not associated with any buffer slot, so we
318 // must track it separately in order to properly use
319 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
320 sp<GraphicBuffer> mCurrentTextureBuf;
321
Jamie Gennisf238e282011-01-09 16:33:17 -0800322 // mCurrentCrop is the crop rectangle that applies to the current texture.
323 // It gets set to mLastQueuedCrop each time updateTexImage is called.
324 Rect mCurrentCrop;
325
326 // mCurrentTransform is the transform identifier for the current texture. It
327 // gets set to mLastQueuedTransform each time updateTexImage is called.
328 uint32_t mCurrentTransform;
329
Jamie Gennis736aa952011-06-12 17:03:06 -0700330 // mCurrentTransformMatrix is the transform matrix for the current texture.
331 // It gets computed by computeTransformMatrix each time updateTexImage is
332 // called.
333 float mCurrentTransformMatrix[16];
334
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800335 // mCurrentTimestamp is the timestamp for the current texture. It
336 // gets set to mLastQueuedTimestamp each time updateTexImage is called.
337 int64_t mCurrentTimestamp;
338
Jamie Gennisf238e282011-01-09 16:33:17 -0800339 // mNextCrop is the crop rectangle that will be used for the next buffer
340 // that gets queued. It is set by calling setCrop.
341 Rect mNextCrop;
342
343 // mNextTransform is the transform identifier that will be used for the next
344 // buffer that gets queued. It is set by calling setTransform.
345 uint32_t mNextTransform;
346
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800347 // mTexName is the name of the OpenGL texture to which streamed images will
348 // be bound when updateTexImage is called. It is set at construction time
349 // changed with a call to setTexName.
350 const GLuint mTexName;
351
Jamie Gennis9a78c902011-01-12 18:30:40 -0800352 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
353 // allocate new GraphicBuffer objects.
354 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
355
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800356 // mFrameAvailableListener is the listener object that will be called when a
357 // new frame becomes available. If it is not NULL it will be called from
358 // queueBuffer.
359 sp<FrameAvailableListener> mFrameAvailableListener;
360
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700361 // mSynchronousMode whether we're in synchronous mode or not
362 bool mSynchronousMode;
363
364 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
365 mutable Condition mDequeueCondition;
366
367 // mQueue is a FIFO of queued buffers used in synchronous mode
368 typedef Vector<int> Fifo;
369 Fifo mQueue;
370
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800371 // mMutex is the mutex used to prevent concurrent access to the member
372 // variables of SurfaceTexture objects. It must be locked whenever the
373 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700374 mutable Mutex mMutex;
Jamie Gennis736aa952011-06-12 17:03:06 -0700375
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800376};
377
378// ----------------------------------------------------------------------------
379}; // namespace android
380
381#endif // ANDROID_GUI_SURFACETEXTURE_H