blob: 43b2fa92ddeca85919fbc034c0462073bc148928 [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 {
Mathias Agopiane845c352011-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 Gennis376590d2011-01-13 14:43:36 -080054 virtual void onFrameAvailable() = 0;
55 };
56
Jamie Gennis68e4a7a2010-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 Agopian0297dca2011-04-25 20:22:14 -070068 virtual sp<GraphicBuffer> requestBuffer(int buf);
Jamie Gennis68e4a7a2010-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 Agopian0297dca2011-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 Gennis68e4a7a2010-12-20 11:27:26 -080077
Eino-Ville Talvalac5f94d82011-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 Gennis68e4a7a2010-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 Agopianed3894c2011-04-20 14:20:59 -070088 virtual int query(int what, int* value);
89
Mathias Agopian402ff242011-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 Gennis68e4a7a2010-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 Agopiane845c352011-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 Agopian402ff242011-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 Gennisb598fb92011-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 Talvalac5f94d82011-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 Gennis376590d2011-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 Gennis83bac212011-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 Agopiane5a1bff2011-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 Agopian27cd07c2011-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 Agopian7f3289c2011-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 Agopian27cd07c2011-04-11 21:19:55 -0700175protected:
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800176
177 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
178 // all slots.
179 void freeAllBuffers();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700180 static bool isExternalFormat(uint32_t format);
181 static GLenum getTextureTarget(uint32_t format);
182
183private:
Jamie Gennis68e4a7a2010-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 Agopian402ff242011-05-02 19:51:12 -0700189 status_t setBufferCountServerLocked(int bufferCount);
190
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800191 enum { INVALID_BUFFER_SLOT = -1 };
192
193 struct BufferSlot {
Mathias Agopian5bbb1cf2011-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),
200 mLastQueuedTransform(0),
201 mLastQueuedTimestamp(0) {
202 }
203
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800204 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
205 // if no buffer has been allocated.
206 sp<GraphicBuffer> mGraphicBuffer;
207
208 // mEglImage is the EGLImage created from mGraphicBuffer.
209 EGLImageKHR mEglImage;
210
211 // mEglDisplay is the EGLDisplay used to create mEglImage.
212 EGLDisplay mEglDisplay;
213
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700214 // mBufferState indicates whether the slot is currently accessible to a
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800215 // client and should not be used by the SurfaceTexture object. It gets
216 // set to true when dequeueBuffer returns the slot and is reset to false
217 // when the client calls either queueBuffer or cancelBuffer on the slot.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700218 enum { DEQUEUED=-2, FREE=-1, QUEUED=0 };
219 int8_t mBufferState;
220
221
222 // mRequestBufferCalled is used for validating that the client did
223 // call requestBuffer() when told to do so. Technically this is not
224 // needed but useful for debugging and catching client bugs.
225 bool mRequestBufferCalled;
226
227 // mLastQueuedCrop is the crop rectangle for the buffer that was most
228 // recently queued. This gets set to mNextCrop each time queueBuffer gets
229 // called.
230 Rect mLastQueuedCrop;
231
232 // mLastQueuedTransform is the transform identifier for the buffer that was
233 // most recently queued. This gets set to mNextTransform each time
234 // queueBuffer gets called.
235 uint32_t mLastQueuedTransform;
236
237 // mLastQueuedTimestamp is the timestamp for the buffer that was most
238 // recently queued. This gets set by queueBuffer.
239 int64_t mLastQueuedTimestamp;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800240 };
241
242 // mSlots is the array of buffer slots that must be mirrored on the client
243 // side. This allows buffer ownership to be transferred between the client
244 // and server without sending a GraphicBuffer over binder. The entire array
245 // is initialized to NULL at construction time, and buffers are allocated
246 // for a slot when requestBuffer is called with that slot's index.
247 BufferSlot mSlots[NUM_BUFFER_SLOTS];
248
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700249 // mDefaultWidth holds the default width of allocated buffers. It is used
250 // in requestBuffers() if a width and height of zero is specified.
251 uint32_t mDefaultWidth;
252
253 // mDefaultHeight holds the default height of allocated buffers. It is used
254 // in requestBuffers() if a width and height of zero is specified.
255 uint32_t mDefaultHeight;
256
257 // mPixelFormat holds the pixel format of allocated buffers. It is used
258 // in requestBuffers() if a format of zero is specified.
259 uint32_t mPixelFormat;
260
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800261 // mBufferCount is the number of buffer slots that the client and server
Mathias Agopian402ff242011-05-02 19:51:12 -0700262 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
263 // by calling setBufferCount or setBufferCountServer
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800264 int mBufferCount;
265
Mathias Agopian402ff242011-05-02 19:51:12 -0700266 // mRequestedBufferCount is the number of buffer slots requested by the
267 // client. The default is zero, which means the client doesn't care how
268 // many buffers there is.
269 int mClientBufferCount;
270
271 // mServerBufferCount buffer count requested by the server-side
272 int mServerBufferCount;
273
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800274 // mCurrentTexture is the buffer slot index of the buffer that is currently
Jamie Gennisd369dc42011-01-09 13:25:39 -0800275 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
276 // indicating that no buffer slot is currently bound to the texture. Note,
277 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
278 // that no buffer is bound to the texture. A call to setBufferCount will
279 // reset mCurrentTexture to INVALID_BUFFER_SLOT.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800280 int mCurrentTexture;
281
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700282 // mCurrentTextureTarget is the GLES texture target to be used with the
283 // current texture.
284 GLenum mCurrentTextureTarget;
285
Jamie Gennisf7acf162011-01-12 18:30:40 -0800286 // mCurrentTextureBuf is the graphic buffer of the current texture. It's
287 // possible that this buffer is not associated with any buffer slot, so we
288 // must track it separately in order to properly use
289 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
290 sp<GraphicBuffer> mCurrentTextureBuf;
291
Jamie Gennisb598fb92011-01-09 16:33:17 -0800292 // mCurrentCrop is the crop rectangle that applies to the current texture.
293 // It gets set to mLastQueuedCrop each time updateTexImage is called.
294 Rect mCurrentCrop;
295
296 // mCurrentTransform is the transform identifier for the current texture. It
297 // gets set to mLastQueuedTransform each time updateTexImage is called.
298 uint32_t mCurrentTransform;
299
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800300 // mCurrentTimestamp is the timestamp for the current texture. It
301 // gets set to mLastQueuedTimestamp each time updateTexImage is called.
302 int64_t mCurrentTimestamp;
303
Jamie Gennisb598fb92011-01-09 16:33:17 -0800304 // mNextCrop is the crop rectangle that will be used for the next buffer
305 // that gets queued. It is set by calling setCrop.
306 Rect mNextCrop;
307
308 // mNextTransform is the transform identifier that will be used for the next
309 // buffer that gets queued. It is set by calling setTransform.
310 uint32_t mNextTransform;
311
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800312 // mTexName is the name of the OpenGL texture to which streamed images will
313 // be bound when updateTexImage is called. It is set at construction time
314 // changed with a call to setTexName.
315 const GLuint mTexName;
316
Jamie Gennisf7acf162011-01-12 18:30:40 -0800317 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
318 // allocate new GraphicBuffer objects.
319 sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
320
Jamie Gennis376590d2011-01-13 14:43:36 -0800321 // mFrameAvailableListener is the listener object that will be called when a
322 // new frame becomes available. If it is not NULL it will be called from
323 // queueBuffer.
324 sp<FrameAvailableListener> mFrameAvailableListener;
325
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700326 // mSynchronousMode whether we're in synchronous mode or not
327 bool mSynchronousMode;
328
329 // mDequeueCondition condition used for dequeueBuffer in synchronous mode
330 mutable Condition mDequeueCondition;
331
332 // mQueue is a FIFO of queued buffers used in synchronous mode
333 typedef Vector<int> Fifo;
334 Fifo mQueue;
335
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800336 // mMutex is the mutex used to prevent concurrent access to the member
337 // variables of SurfaceTexture objects. It must be locked whenever the
338 // member variables are accessed.
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700339 mutable Mutex mMutex;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800340};
341
342// ----------------------------------------------------------------------------
343}; // namespace android
344
345#endif // ANDROID_GUI_SURFACETEXTURE_H