blob: 2b314621abab87c6dde42348a7d087d8ff6efe82 [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 Gennis8ba32fa2010-12-20 11:27:26 -0800191 enum { INVALID_BUFFER_SLOT = -1 };
192
193 struct BufferSlot {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700194
195 BufferSlot()
196 : mEglImage(EGL_NO_IMAGE_KHR),
197 mEglDisplay(EGL_NO_DISPLAY),
198 mBufferState(BufferSlot::FREE),
199 mRequestBufferCalled(false),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700200 mTransform(0),
201 mTimestamp(0) {
202 mCrop.makeInvalid();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700203 }
204
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800205 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
206 // if no buffer has been allocated.
207 sp<GraphicBuffer> mGraphicBuffer;
208
209 // mEglImage is the EGLImage created from mGraphicBuffer.
210 EGLImageKHR mEglImage;
211
212 // mEglDisplay is the EGLDisplay used to create mEglImage.
213 EGLDisplay mEglDisplay;
214
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700215 // BufferState represents the different states in which a buffer slot
216 // can be.
217 enum BufferState {
218 // FREE indicates that the buffer is not currently being used and
219 // will not be used in the future until it gets dequeued and
220 // subseqently queued by the client.
221 FREE = 0,
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700222
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700223 // DEQUEUED indicates that the buffer has been dequeued by the
224 // client, but has not yet been queued or canceled. The buffer is
225 // considered 'owned' by the client, and the server should not use
226 // it for anything.
227 //
228 // Note that when in synchronous-mode (mSynchronousMode == true),
229 // the buffer that's currently attached to the texture may be
230 // dequeued by the client. That means that the current buffer can
231 // be in either the DEQUEUED or QUEUED state. In asynchronous mode,
232 // however, the current buffer is always in the QUEUED state.
233 DEQUEUED = 1,
234
235 // QUEUED indicates that the buffer has been queued by the client,
236 // and has not since been made available for the client to dequeue.
237 // Attaching the buffer to the texture does NOT transition the
238 // buffer away from the QUEUED state. However, in Synchronous mode
239 // the current buffer may be dequeued by the client under some
240 // circumstances. See the note about the current buffer in the
241 // documentation for DEQUEUED.
242 QUEUED = 2,
243 };
244
245 // mBufferState is the current state of this buffer slot.
246 BufferState mBufferState;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700247
248 // mRequestBufferCalled is used for validating that the client did
249 // call requestBuffer() when told to do so. Technically this is not
250 // needed but useful for debugging and catching client bugs.
251 bool mRequestBufferCalled;
252
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700253 // mCrop is the current crop rectangle for this buffer slot. This gets
254 // set to mNextCrop each time queueBuffer gets called for this buffer.
255 Rect mCrop;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700256
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700257 // mTransform is the current transform flags for this buffer slot. This
258 // gets set to mNextTransform each time queueBuffer gets called for this
259 // slot.
260 uint32_t mTransform;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700261
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700262 // mTimestamp is the current timestamp for this buffer slot. This gets
263 // to set by queueBuffer each time this slot is queued.
264 int64_t mTimestamp;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800265 };
266
267 // mSlots is the array of buffer slots that must be mirrored on the client
268 // side. This allows buffer ownership to be transferred between the client
269 // and server without sending a GraphicBuffer over binder. The entire array
270 // is initialized to NULL at construction time, and buffers are allocated
271 // for a slot when requestBuffer is called with that slot's index.
272 BufferSlot mSlots[NUM_BUFFER_SLOTS];
273
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700274 // mDefaultWidth holds the default width of allocated buffers. It is used
275 // in requestBuffers() if a width and height of zero is specified.
276 uint32_t mDefaultWidth;
277
278 // mDefaultHeight holds the default height of allocated buffers. It is used
279 // in requestBuffers() if a width and height of zero is specified.
280 uint32_t mDefaultHeight;
281
282 // mPixelFormat holds the pixel format of allocated buffers. It is used
283 // in requestBuffers() if a format of zero is specified.
284 uint32_t mPixelFormat;
285
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800286 // mBufferCount is the number of buffer slots that the client and server
Mathias Agopian80727112011-05-02 19:51:12 -0700287 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
288 // by calling setBufferCount or setBufferCountServer
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800289 int mBufferCount;
290
Mathias Agopian80727112011-05-02 19:51:12 -0700291 // mRequestedBufferCount is the number of buffer slots requested by the
292 // client. The default is zero, which means the client doesn't care how
293 // many buffers there is.
294 int mClientBufferCount;
295
296 // mServerBufferCount buffer count requested by the server-side
297 int mServerBufferCount;
298
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800299 // mCurrentTexture is the buffer slot index of the buffer that is currently
Jamie Gennis67eedd72011-01-09 13:25:39 -0800300 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
301 // indicating that no buffer slot is currently bound to the texture. Note,
302 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
303 // that no buffer is bound to the texture. A call to setBufferCount will
304 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800305 int mCurrentTexture;
306
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700307 // mCurrentTextureTarget is the GLES texture target to be used with the
308 // current texture.
309 GLenum mCurrentTextureTarget;
310
Jamie Gennis9a78c902011-01-12 18:30:40 -0800311 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
312 // possible that this buffer is not associated with any buffer slot, so we
313 // must track it separately in order to properly use
314 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
315 sp<GraphicBuffer> mCurrentTextureBuf;
316
Jamie Gennisf238e282011-01-09 16:33:17 -0800317 // mCurrentCrop is the crop rectangle that applies to the current texture.
318 // It gets set to mLastQueuedCrop each time updateTexImage is called.
319 Rect mCurrentCrop;
320
321 // mCurrentTransform is the transform identifier for the current texture. It
322 // gets set to mLastQueuedTransform each time updateTexImage is called.
323 uint32_t mCurrentTransform;
324
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800325 // mCurrentTimestamp is the timestamp for the current texture. It
326 // gets set to mLastQueuedTimestamp each time updateTexImage is called.
327 int64_t mCurrentTimestamp;
328
Jamie Gennisf238e282011-01-09 16:33:17 -0800329 // mNextCrop is the crop rectangle that will be used for the next buffer
330 // that gets queued. It is set by calling setCrop.
331 Rect mNextCrop;
332
333 // mNextTransform is the transform identifier that will be used for the next
334 // buffer that gets queued. It is set by calling setTransform.
335 uint32_t mNextTransform;
336
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800337 // mTexName is the name of the OpenGL texture to which streamed images will
338 // be bound when updateTexImage is called. It is set at construction time
339 // changed with a call to setTexName.
340 const GLuint mTexName;
341
Jamie Gennis9a78c902011-01-12 18:30:40 -0800342 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
343 // allocate new GraphicBuffer objects.
344 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
345
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800346 // mFrameAvailableListener is the listener object that will be called when a
347 // new frame becomes available. If it is not NULL it will be called from
348 // queueBuffer.
349 sp<FrameAvailableListener> mFrameAvailableListener;
350
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700351 // mSynchronousMode whether we're in synchronous mode or not
352 bool mSynchronousMode;
353
354 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
355 mutable Condition mDequeueCondition;
356
357 // mQueue is a FIFO of queued buffers used in synchronous mode
358 typedef Vector<int> Fifo;
359 Fifo mQueue;
360
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800361 // mMutex is the mutex used to prevent concurrent access to the member
362 // variables of SurfaceTexture objects. It must be locked whenever the
363 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700364 mutable Mutex mMutex;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800365};
366
367// ----------------------------------------------------------------------------
368}; // namespace android
369
370#endif // ANDROID_GUI_SURFACETEXTURE_H