Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 29 | #include <utils/Vector.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 30 | |
| 31 | #define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture" |
| 32 | |
| 33 | namespace android { |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 36 | class IGraphicBufferAlloc; |
| 37 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 38 | class SurfaceTexture : public BnSurfaceTexture { |
| 39 | public: |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 40 | enum { MIN_UNDEQUEUED_BUFFERS = 2 }; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 41 | enum { |
| 42 | MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1, |
| 43 | MIN_SYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS |
| 44 | }; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 45 | enum { NUM_BUFFER_SLOTS = 32 }; |
| 46 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 47 | struct FrameAvailableListener : public virtual RefBase { |
| 48 | virtual void onFrameAvailable() = 0; |
| 49 | }; |
| 50 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 51 | // tex indicates the name OpenGL texture to which images are to be streamed. |
| 52 | // This texture name cannot be changed once the SurfaceTexture is created. |
| 53 | SurfaceTexture(GLuint tex); |
| 54 | |
| 55 | virtual ~SurfaceTexture(); |
| 56 | |
| 57 | // setBufferCount updates the number of available buffer slots. After |
| 58 | // calling this all buffer slots are both unallocated and owned by the |
| 59 | // SurfaceTexture object (i.e. they are not owned by the client). |
| 60 | virtual status_t setBufferCount(int bufferCount); |
| 61 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 62 | virtual sp<GraphicBuffer> requestBuffer(int buf); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 63 | |
| 64 | // dequeueBuffer gets the next buffer slot index for the client to use. If a |
| 65 | // buffer slot is available then that slot index is written to the location |
| 66 | // pointed to by the buf argument and a status of OK is returned. If no |
| 67 | // slot is available then a status of -EBUSY is returned and buf is |
| 68 | // unmodified. |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 69 | virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h, |
| 70 | uint32_t format, uint32_t usage); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 71 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 72 | // queueBuffer returns a filled buffer to the SurfaceTexture. In addition, a |
| 73 | // timestamp must be provided for the buffer. The timestamp is in |
| 74 | // nanoseconds, and must be monotonically increasing. Its other semantics |
| 75 | // (zero point, etc) are client-dependent and should be documented by the |
| 76 | // client. |
| 77 | virtual status_t queueBuffer(int buf, int64_t timestamp); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 78 | virtual void cancelBuffer(int buf); |
| 79 | virtual status_t setCrop(const Rect& reg); |
| 80 | virtual status_t setTransform(uint32_t transform); |
| 81 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 82 | virtual int query(int what, int* value); |
| 83 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 84 | // setSynchronousMode set whether dequeueBuffer is synchronous or |
| 85 | // asynchronous. In synchronous mode, dequeueBuffer blocks until |
| 86 | // a buffer is available, the currently bound buffer can be dequeued and |
| 87 | // queued buffers will be retired in order. |
| 88 | // The default mode is asynchronous. |
| 89 | virtual status_t setSynchronousMode(bool enabled); |
| 90 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 91 | // updateTexImage sets the image contents of the target texture to that of |
| 92 | // the most recently queued buffer. |
| 93 | // |
| 94 | // This call may only be made while the OpenGL ES context to which the |
| 95 | // target texture belongs is bound to the calling thread. |
| 96 | status_t updateTexImage(); |
| 97 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 98 | // setBufferCountServer set the buffer count. If the client has requested |
| 99 | // a buffer count using setBufferCount, the server-buffer count will |
| 100 | // take effect once the client sets the count back to zero. |
| 101 | status_t setBufferCountServer(int bufferCount); |
| 102 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 103 | // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix |
| 104 | // associated with the texture image set by the most recent call to |
| 105 | // updateTexImage. |
| 106 | // |
| 107 | // This transform matrix maps 2D homogeneous texture coordinates of the form |
| 108 | // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture |
| 109 | // coordinate that should be used to sample that location from the texture. |
| 110 | // Sampling the texture outside of the range of this transform is undefined. |
| 111 | // |
| 112 | // This transform is necessary to compensate for transforms that the stream |
| 113 | // content producer may implicitly apply to the content. By forcing users of |
| 114 | // a SurfaceTexture to apply this transform we avoid performing an extra |
| 115 | // copy of the data that would be needed to hide the transform from the |
| 116 | // user. |
| 117 | // |
| 118 | // The matrix is stored in column-major order so that it may be passed |
| 119 | // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv |
| 120 | // functions. |
| 121 | void getTransformMatrix(float mtx[16]); |
| 122 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 123 | // getTimestamp retrieves the timestamp associated with the texture image |
| 124 | // set by the most recent call to updateTexImage. |
| 125 | // |
| 126 | // The timestamp is in nanoseconds, and is monotonically increasing. Its |
| 127 | // other semantics (zero point, etc) are source-dependent and should be |
| 128 | // documented by the source. |
| 129 | int64_t getTimestamp(); |
| 130 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 131 | // setFrameAvailableListener sets the listener object that will be notified |
| 132 | // when a new frame becomes available. |
| 133 | void setFrameAvailableListener(const sp<FrameAvailableListener>& l); |
| 134 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 135 | // getAllocator retrieves the binder object that must be referenced as long |
| 136 | // as the GraphicBuffers dequeued from this SurfaceTexture are referenced. |
| 137 | // Holding this binder reference prevents SurfaceFlinger from freeing the |
| 138 | // buffers before the client is done with them. |
| 139 | sp<IBinder> getAllocator(); |
| 140 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 141 | // setDefaultBufferSize is used to set the size of buffers returned by |
| 142 | // requestBuffers when a with and height of zero is requested. |
| 143 | // A call to setDefaultBufferSize() may trigger requestBuffers() to |
| 144 | // be called from the client. |
| 145 | status_t setDefaultBufferSize(uint32_t w, uint32_t h); |
| 146 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 147 | // getCurrentBuffer returns the buffer associated with the current image. |
| 148 | sp<GraphicBuffer> getCurrentBuffer() const; |
| 149 | |
| 150 | // getCurrentTextureTarget returns the texture target of the current |
| 151 | // texture as returned by updateTexImage(). |
| 152 | GLenum getCurrentTextureTarget() const; |
| 153 | |
| 154 | // getCurrentCrop returns the cropping rectangle of the current buffer |
| 155 | Rect getCurrentCrop() const; |
| 156 | |
| 157 | // getCurrentTransform returns the transform of the current buffer |
| 158 | uint32_t getCurrentTransform() const; |
| 159 | |
| 160 | protected: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 161 | |
| 162 | // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for |
| 163 | // all slots. |
| 164 | void freeAllBuffers(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 165 | static bool isExternalFormat(uint32_t format); |
| 166 | static GLenum getTextureTarget(uint32_t format); |
| 167 | |
| 168 | private: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 169 | |
| 170 | // createImage creates a new EGLImage from a GraphicBuffer. |
| 171 | EGLImageKHR createImage(EGLDisplay dpy, |
| 172 | const sp<GraphicBuffer>& graphicBuffer); |
| 173 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 174 | status_t setBufferCountServerLocked(int bufferCount); |
| 175 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 176 | enum { INVALID_BUFFER_SLOT = -1 }; |
| 177 | |
| 178 | struct BufferSlot { |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 179 | |
| 180 | BufferSlot() |
| 181 | : mEglImage(EGL_NO_IMAGE_KHR), |
| 182 | mEglDisplay(EGL_NO_DISPLAY), |
| 183 | mBufferState(BufferSlot::FREE), |
| 184 | mRequestBufferCalled(false), |
| 185 | mLastQueuedTransform(0), |
| 186 | mLastQueuedTimestamp(0) { |
| 187 | } |
| 188 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 189 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
| 190 | // if no buffer has been allocated. |
| 191 | sp<GraphicBuffer> mGraphicBuffer; |
| 192 | |
| 193 | // mEglImage is the EGLImage created from mGraphicBuffer. |
| 194 | EGLImageKHR mEglImage; |
| 195 | |
| 196 | // mEglDisplay is the EGLDisplay used to create mEglImage. |
| 197 | EGLDisplay mEglDisplay; |
| 198 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 199 | // mBufferState indicates whether the slot is currently accessible to a |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 200 | // client and should not be used by the SurfaceTexture object. It gets |
| 201 | // set to true when dequeueBuffer returns the slot and is reset to false |
| 202 | // when the client calls either queueBuffer or cancelBuffer on the slot. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 203 | enum { DEQUEUED=-2, FREE=-1, QUEUED=0 }; |
| 204 | int8_t mBufferState; |
| 205 | |
| 206 | |
| 207 | // mRequestBufferCalled is used for validating that the client did |
| 208 | // call requestBuffer() when told to do so. Technically this is not |
| 209 | // needed but useful for debugging and catching client bugs. |
| 210 | bool mRequestBufferCalled; |
| 211 | |
| 212 | // mLastQueuedCrop is the crop rectangle for the buffer that was most |
| 213 | // recently queued. This gets set to mNextCrop each time queueBuffer gets |
| 214 | // called. |
| 215 | Rect mLastQueuedCrop; |
| 216 | |
| 217 | // mLastQueuedTransform is the transform identifier for the buffer that was |
| 218 | // most recently queued. This gets set to mNextTransform each time |
| 219 | // queueBuffer gets called. |
| 220 | uint32_t mLastQueuedTransform; |
| 221 | |
| 222 | // mLastQueuedTimestamp is the timestamp for the buffer that was most |
| 223 | // recently queued. This gets set by queueBuffer. |
| 224 | int64_t mLastQueuedTimestamp; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | // mSlots is the array of buffer slots that must be mirrored on the client |
| 228 | // side. This allows buffer ownership to be transferred between the client |
| 229 | // and server without sending a GraphicBuffer over binder. The entire array |
| 230 | // is initialized to NULL at construction time, and buffers are allocated |
| 231 | // for a slot when requestBuffer is called with that slot's index. |
| 232 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
| 233 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 234 | // mDefaultWidth holds the default width of allocated buffers. It is used |
| 235 | // in requestBuffers() if a width and height of zero is specified. |
| 236 | uint32_t mDefaultWidth; |
| 237 | |
| 238 | // mDefaultHeight holds the default height of allocated buffers. It is used |
| 239 | // in requestBuffers() if a width and height of zero is specified. |
| 240 | uint32_t mDefaultHeight; |
| 241 | |
| 242 | // mPixelFormat holds the pixel format of allocated buffers. It is used |
| 243 | // in requestBuffers() if a format of zero is specified. |
| 244 | uint32_t mPixelFormat; |
| 245 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 246 | // mBufferCount is the number of buffer slots that the client and server |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 247 | // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed |
| 248 | // by calling setBufferCount or setBufferCountServer |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 249 | int mBufferCount; |
| 250 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame^] | 251 | // mRequestedBufferCount is the number of buffer slots requested by the |
| 252 | // client. The default is zero, which means the client doesn't care how |
| 253 | // many buffers there is. |
| 254 | int mClientBufferCount; |
| 255 | |
| 256 | // mServerBufferCount buffer count requested by the server-side |
| 257 | int mServerBufferCount; |
| 258 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 259 | // mCurrentTexture is the buffer slot index of the buffer that is currently |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 260 | // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, |
| 261 | // indicating that no buffer slot is currently bound to the texture. Note, |
| 262 | // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean |
| 263 | // that no buffer is bound to the texture. A call to setBufferCount will |
| 264 | // reset mCurrentTexture to INVALID_BUFFER_SLOT. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 265 | int mCurrentTexture; |
| 266 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 267 | // mCurrentTextureTarget is the GLES texture target to be used with the |
| 268 | // current texture. |
| 269 | GLenum mCurrentTextureTarget; |
| 270 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 271 | // mCurrentTextureBuf is the graphic buffer of the current texture. It's |
| 272 | // possible that this buffer is not associated with any buffer slot, so we |
| 273 | // must track it separately in order to properly use |
| 274 | // IGraphicBufferAlloc::freeAllGraphicBuffersExcept. |
| 275 | sp<GraphicBuffer> mCurrentTextureBuf; |
| 276 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 277 | // mCurrentCrop is the crop rectangle that applies to the current texture. |
| 278 | // It gets set to mLastQueuedCrop each time updateTexImage is called. |
| 279 | Rect mCurrentCrop; |
| 280 | |
| 281 | // mCurrentTransform is the transform identifier for the current texture. It |
| 282 | // gets set to mLastQueuedTransform each time updateTexImage is called. |
| 283 | uint32_t mCurrentTransform; |
| 284 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 285 | // mCurrentTimestamp is the timestamp for the current texture. It |
| 286 | // gets set to mLastQueuedTimestamp each time updateTexImage is called. |
| 287 | int64_t mCurrentTimestamp; |
| 288 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 289 | // mNextCrop is the crop rectangle that will be used for the next buffer |
| 290 | // that gets queued. It is set by calling setCrop. |
| 291 | Rect mNextCrop; |
| 292 | |
| 293 | // mNextTransform is the transform identifier that will be used for the next |
| 294 | // buffer that gets queued. It is set by calling setTransform. |
| 295 | uint32_t mNextTransform; |
| 296 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 297 | // mTexName is the name of the OpenGL texture to which streamed images will |
| 298 | // be bound when updateTexImage is called. It is set at construction time |
| 299 | // changed with a call to setTexName. |
| 300 | const GLuint mTexName; |
| 301 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 302 | // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to |
| 303 | // allocate new GraphicBuffer objects. |
| 304 | sp<IGraphicBufferAlloc> mGraphicBufferAlloc; |
| 305 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 306 | // mFrameAvailableListener is the listener object that will be called when a |
| 307 | // new frame becomes available. If it is not NULL it will be called from |
| 308 | // queueBuffer. |
| 309 | sp<FrameAvailableListener> mFrameAvailableListener; |
| 310 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 311 | // mSynchronousMode whether we're in synchronous mode or not |
| 312 | bool mSynchronousMode; |
| 313 | |
| 314 | // mDequeueCondition condition used for dequeueBuffer in synchronous mode |
| 315 | mutable Condition mDequeueCondition; |
| 316 | |
| 317 | // mQueue is a FIFO of queued buffers used in synchronous mode |
| 318 | typedef Vector<int> Fifo; |
| 319 | Fifo mQueue; |
| 320 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 321 | // mMutex is the mutex used to prevent concurrent access to the member |
| 322 | // variables of SurfaceTexture objects. It must be locked whenever the |
| 323 | // member variables are accessed. |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 324 | mutable Mutex mMutex; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | // ---------------------------------------------------------------------------- |
| 328 | }; // namespace android |
| 329 | |
| 330 | #endif // ANDROID_GUI_SURFACETEXTURE_H |