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> |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 23 | #include <GLES2/gl2ext.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 24 | |
| 25 | #include <gui/ISurfaceTexture.h> |
| 26 | |
| 27 | #include <ui/GraphicBuffer.h> |
| 28 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 29 | #include <utils/String8.h> |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 30 | #include <utils/Vector.h> |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 31 | #include <utils/threads.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 32 | |
| 33 | #define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture" |
| 34 | |
| 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 38 | class IGraphicBufferAlloc; |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 39 | class String8; |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 40 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 41 | class SurfaceTexture : public BnSurfaceTexture { |
| 42 | public: |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 43 | enum { MIN_UNDEQUEUED_BUFFERS = 2 }; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 44 | enum { |
| 45 | MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1, |
| 46 | MIN_SYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS |
| 47 | }; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 48 | enum { NUM_BUFFER_SLOTS = 32 }; |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 49 | enum { NO_CONNECTED_API = 0 }; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 50 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 51 | struct FrameAvailableListener : public virtual RefBase { |
Jamie Gennis | 3d8063b | 2011-06-26 18:27:47 -0700 | [diff] [blame] | 52 | // onFrameAvailable() is called from queueBuffer() each time an |
| 53 | // additional frame becomes available for consumption. This means that |
| 54 | // frames that are queued while in asynchronous mode only trigger the |
| 55 | // callback if no previous frames are pending. Frames queued while in |
| 56 | // synchronous mode always trigger the callback. |
| 57 | // |
| 58 | // This is called without any lock held and can be called concurrently |
| 59 | // by multiple threads. |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 60 | virtual void onFrameAvailable() = 0; |
| 61 | }; |
| 62 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 63 | // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the |
| 64 | // name of the OpenGL ES texture to which images are to be streamed. This |
| 65 | // texture name cannot be changed once the SurfaceTexture is created. |
| 66 | // allowSynchronousMode specifies whether or not synchronous mode can be |
| 67 | // enabled. texTarget specifies the OpenGL ES texture target to which the |
| 68 | // texture will be bound in updateTexImage. useFenceSync specifies whether |
| 69 | // fences should be used to synchronize access to buffers if that behavior |
| 70 | // is enabled at compile-time. |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 71 | SurfaceTexture(GLuint tex, bool allowSynchronousMode = true, |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 72 | GLenum texTarget = GL_TEXTURE_EXTERNAL_OES, bool useFenceSync = true); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 73 | |
| 74 | virtual ~SurfaceTexture(); |
| 75 | |
| 76 | // setBufferCount updates the number of available buffer slots. After |
| 77 | // calling this all buffer slots are both unallocated and owned by the |
| 78 | // SurfaceTexture object (i.e. they are not owned by the client). |
| 79 | virtual status_t setBufferCount(int bufferCount); |
| 80 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 81 | virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 82 | |
| 83 | // dequeueBuffer gets the next buffer slot index for the client to use. If a |
| 84 | // buffer slot is available then that slot index is written to the location |
| 85 | // pointed to by the buf argument and a status of OK is returned. If no |
| 86 | // slot is available then a status of -EBUSY is returned and buf is |
| 87 | // unmodified. |
Mathias Agopian | 194c76c | 2011-11-10 14:34:26 -0800 | [diff] [blame] | 88 | // The width and height parameters must be no greater than the minimum of |
| 89 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
| 90 | // An error due to invalid dimensions might not be reported until |
| 91 | // updateTexImage() is called. |
| 92 | virtual status_t dequeueBuffer(int *buf, uint32_t width, uint32_t height, |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 93 | uint32_t format, uint32_t usage); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 94 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 95 | // queueBuffer returns a filled buffer to the SurfaceTexture. In addition, a |
| 96 | // timestamp must be provided for the buffer. The timestamp is in |
| 97 | // nanoseconds, and must be monotonically increasing. Its other semantics |
| 98 | // (zero point, etc) are client-dependent and should be documented by the |
| 99 | // client. |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 100 | virtual status_t queueBuffer(int buf, int64_t timestamp, |
| 101 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 102 | virtual void cancelBuffer(int buf); |
| 103 | virtual status_t setCrop(const Rect& reg); |
| 104 | virtual status_t setTransform(uint32_t transform); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 105 | virtual status_t setScalingMode(int mode); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 106 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 107 | virtual int query(int what, int* value); |
| 108 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 109 | // setSynchronousMode set whether dequeueBuffer is synchronous or |
| 110 | // asynchronous. In synchronous mode, dequeueBuffer blocks until |
| 111 | // a buffer is available, the currently bound buffer can be dequeued and |
| 112 | // queued buffers will be retired in order. |
| 113 | // The default mode is asynchronous. |
| 114 | virtual status_t setSynchronousMode(bool enabled); |
| 115 | |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 116 | // connect attempts to connect a client API to the SurfaceTexture. This |
| 117 | // must be called before any other ISurfaceTexture methods are called except |
| 118 | // for getAllocator. |
| 119 | // |
| 120 | // This method will fail if the connect was previously called on the |
| 121 | // SurfaceTexture and no corresponding disconnect call was made. |
Mathias Agopian | 5bfc245 | 2011-08-08 19:14:03 -0700 | [diff] [blame] | 122 | virtual status_t connect(int api, |
| 123 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 124 | |
| 125 | // disconnect attempts to disconnect a client API from the SurfaceTexture. |
| 126 | // Calling this method will cause any subsequent calls to other |
| 127 | // ISurfaceTexture methods to fail except for getAllocator and connect. |
| 128 | // Successfully calling connect after this will allow the other methods to |
| 129 | // succeed again. |
| 130 | // |
| 131 | // This method will fail if the the SurfaceTexture is not currently |
| 132 | // connected to the specified client API. |
| 133 | virtual status_t disconnect(int api); |
| 134 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 135 | // updateTexImage sets the image contents of the target texture to that of |
| 136 | // the most recently queued buffer. |
| 137 | // |
| 138 | // This call may only be made while the OpenGL ES context to which the |
| 139 | // target texture belongs is bound to the calling thread. |
| 140 | status_t updateTexImage(); |
| 141 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 142 | // setBufferCountServer set the buffer count. If the client has requested |
| 143 | // a buffer count using setBufferCount, the server-buffer count will |
| 144 | // take effect once the client sets the count back to zero. |
| 145 | status_t setBufferCountServer(int bufferCount); |
| 146 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 147 | // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix |
| 148 | // associated with the texture image set by the most recent call to |
| 149 | // updateTexImage. |
| 150 | // |
| 151 | // This transform matrix maps 2D homogeneous texture coordinates of the form |
| 152 | // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture |
| 153 | // coordinate that should be used to sample that location from the texture. |
| 154 | // Sampling the texture outside of the range of this transform is undefined. |
| 155 | // |
| 156 | // This transform is necessary to compensate for transforms that the stream |
| 157 | // content producer may implicitly apply to the content. By forcing users of |
| 158 | // a SurfaceTexture to apply this transform we avoid performing an extra |
| 159 | // copy of the data that would be needed to hide the transform from the |
| 160 | // user. |
| 161 | // |
| 162 | // The matrix is stored in column-major order so that it may be passed |
| 163 | // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv |
| 164 | // functions. |
| 165 | void getTransformMatrix(float mtx[16]); |
| 166 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 167 | // getTimestamp retrieves the timestamp associated with the texture image |
| 168 | // set by the most recent call to updateTexImage. |
| 169 | // |
| 170 | // The timestamp is in nanoseconds, and is monotonically increasing. Its |
| 171 | // other semantics (zero point, etc) are source-dependent and should be |
| 172 | // documented by the source. |
| 173 | int64_t getTimestamp(); |
| 174 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 175 | // setFrameAvailableListener sets the listener object that will be notified |
| 176 | // when a new frame becomes available. |
Pannag Sanketi | 292a31a | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 177 | void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 178 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 179 | // getAllocator retrieves the binder object that must be referenced as long |
| 180 | // as the GraphicBuffers dequeued from this SurfaceTexture are referenced. |
| 181 | // Holding this binder reference prevents SurfaceFlinger from freeing the |
| 182 | // buffers before the client is done with them. |
| 183 | sp<IBinder> getAllocator(); |
| 184 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 185 | // setDefaultBufferSize is used to set the size of buffers returned by |
| 186 | // requestBuffers when a with and height of zero is requested. |
| 187 | // A call to setDefaultBufferSize() may trigger requestBuffers() to |
| 188 | // be called from the client. |
Mathias Agopian | 194c76c | 2011-11-10 14:34:26 -0800 | [diff] [blame] | 189 | // The width and height parameters must be no greater than the minimum of |
| 190 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
| 191 | // An error due to invalid dimensions might not be reported until |
| 192 | // updateTexImage() is called. |
| 193 | status_t setDefaultBufferSize(uint32_t width, uint32_t height); |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 194 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 195 | // getCurrentBuffer returns the buffer associated with the current image. |
| 196 | sp<GraphicBuffer> getCurrentBuffer() const; |
| 197 | |
| 198 | // getCurrentTextureTarget returns the texture target of the current |
| 199 | // texture as returned by updateTexImage(). |
| 200 | GLenum getCurrentTextureTarget() const; |
| 201 | |
| 202 | // getCurrentCrop returns the cropping rectangle of the current buffer |
| 203 | Rect getCurrentCrop() const; |
| 204 | |
| 205 | // getCurrentTransform returns the transform of the current buffer |
| 206 | uint32_t getCurrentTransform() const; |
| 207 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 208 | // getCurrentScalingMode returns the scaling mode of the current buffer |
| 209 | uint32_t getCurrentScalingMode() const; |
| 210 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 211 | // isSynchronousMode returns whether the SurfaceTexture is currently in |
| 212 | // synchronous mode. |
| 213 | bool isSynchronousMode() const; |
| 214 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 215 | // abandon frees all the buffers and puts the SurfaceTexture into the |
| 216 | // 'abandoned' state. Once put in this state the SurfaceTexture can never |
| 217 | // leave it. When in the 'abandoned' state, all methods of the |
| 218 | // ISurfaceTexture interface will fail with the NO_INIT error. |
| 219 | // |
| 220 | // Note that while calling this method causes all the buffers to be freed |
| 221 | // from the perspective of the the SurfaceTexture, if there are additional |
| 222 | // references on the buffers (e.g. if a buffer is referenced by a client or |
| 223 | // by OpenGL ES as a texture) then those buffer will remain allocated. |
| 224 | void abandon(); |
| 225 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 226 | // set the name of the SurfaceTexture that will be used to identify it in |
| 227 | // log messages. |
| 228 | void setName(const String8& name); |
| 229 | |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 230 | // dump our state in a String |
| 231 | void dump(String8& result) const; |
| 232 | void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const; |
| 233 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 234 | protected: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 235 | |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 236 | // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage) |
| 237 | // for the given slot. |
| 238 | void freeBufferLocked(int index); |
| 239 | |
| 240 | // freeAllBuffersLocked frees the resources (both GraphicBuffer and |
| 241 | // EGLImage) for all slots. |
Mathias Agopian | ef51b99 | 2011-08-10 15:28:58 -0700 | [diff] [blame] | 242 | void freeAllBuffersLocked(); |
Mathias Agopian | 2560d14 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 243 | |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 244 | // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer |
| 245 | // and EGLImage) for all slots except the head of mQueue |
| 246 | void freeAllBuffersExceptHeadLocked(); |
| 247 | |
Mathias Agopian | 2560d14 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 248 | // drainQueueLocked drains the buffer queue if we're in synchronous mode |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 249 | // returns immediately otherwise. return NO_INIT if SurfaceTexture |
| 250 | // became abandoned or disconnected during this call. |
| 251 | status_t drainQueueLocked(); |
| 252 | |
| 253 | // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in |
| 254 | // synchronous mode and free all buffers. In asynchronous mode, all buffers |
| 255 | // are freed except the current buffer. |
| 256 | status_t drainQueueAndFreeBuffersLocked(); |
Mathias Agopian | 2560d14 | 2011-08-10 16:33:23 -0700 | [diff] [blame] | 257 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 258 | static bool isExternalFormat(uint32_t format); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 259 | |
| 260 | private: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 261 | |
| 262 | // createImage creates a new EGLImage from a GraphicBuffer. |
| 263 | EGLImageKHR createImage(EGLDisplay dpy, |
| 264 | const sp<GraphicBuffer>& graphicBuffer); |
| 265 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 266 | status_t setBufferCountServerLocked(int bufferCount); |
| 267 | |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 268 | // computeCurrentTransformMatrix computes the transform matrix for the |
| 269 | // current texture. It uses mCurrentTransform and the current GraphicBuffer |
| 270 | // to compute this matrix and stores it in mCurrentTransformMatrix. |
| 271 | void computeCurrentTransformMatrix(); |
| 272 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 273 | enum { INVALID_BUFFER_SLOT = -1 }; |
| 274 | |
| 275 | struct BufferSlot { |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 276 | |
| 277 | BufferSlot() |
| 278 | : mEglImage(EGL_NO_IMAGE_KHR), |
| 279 | mEglDisplay(EGL_NO_DISPLAY), |
| 280 | mBufferState(BufferSlot::FREE), |
| 281 | mRequestBufferCalled(false), |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 282 | mTransform(0), |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 283 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
Sunita Nadampalli | a929748 | 2011-11-09 18:23:41 -0600 | [diff] [blame] | 284 | mTimestamp(0), |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 285 | mFrameNumber(0), |
| 286 | mFence(EGL_NO_SYNC_KHR) { |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 287 | mCrop.makeInvalid(); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 290 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
| 291 | // if no buffer has been allocated. |
| 292 | sp<GraphicBuffer> mGraphicBuffer; |
| 293 | |
| 294 | // mEglImage is the EGLImage created from mGraphicBuffer. |
| 295 | EGLImageKHR mEglImage; |
| 296 | |
| 297 | // mEglDisplay is the EGLDisplay used to create mEglImage. |
| 298 | EGLDisplay mEglDisplay; |
| 299 | |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 300 | // BufferState represents the different states in which a buffer slot |
| 301 | // can be. |
| 302 | enum BufferState { |
| 303 | // FREE indicates that the buffer is not currently being used and |
| 304 | // will not be used in the future until it gets dequeued and |
Mathias Agopian | 29b5762 | 2011-08-17 15:42:04 -0700 | [diff] [blame] | 305 | // subsequently queued by the client. |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 306 | FREE = 0, |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 307 | |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 308 | // DEQUEUED indicates that the buffer has been dequeued by the |
| 309 | // client, but has not yet been queued or canceled. The buffer is |
| 310 | // considered 'owned' by the client, and the server should not use |
| 311 | // it for anything. |
| 312 | // |
| 313 | // Note that when in synchronous-mode (mSynchronousMode == true), |
| 314 | // the buffer that's currently attached to the texture may be |
| 315 | // dequeued by the client. That means that the current buffer can |
| 316 | // be in either the DEQUEUED or QUEUED state. In asynchronous mode, |
| 317 | // however, the current buffer is always in the QUEUED state. |
| 318 | DEQUEUED = 1, |
| 319 | |
| 320 | // QUEUED indicates that the buffer has been queued by the client, |
| 321 | // and has not since been made available for the client to dequeue. |
| 322 | // Attaching the buffer to the texture does NOT transition the |
| 323 | // buffer away from the QUEUED state. However, in Synchronous mode |
| 324 | // the current buffer may be dequeued by the client under some |
| 325 | // circumstances. See the note about the current buffer in the |
| 326 | // documentation for DEQUEUED. |
| 327 | QUEUED = 2, |
| 328 | }; |
| 329 | |
| 330 | // mBufferState is the current state of this buffer slot. |
| 331 | BufferState mBufferState; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 332 | |
| 333 | // mRequestBufferCalled is used for validating that the client did |
| 334 | // call requestBuffer() when told to do so. Technically this is not |
| 335 | // needed but useful for debugging and catching client bugs. |
| 336 | bool mRequestBufferCalled; |
| 337 | |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 338 | // mCrop is the current crop rectangle for this buffer slot. This gets |
| 339 | // set to mNextCrop each time queueBuffer gets called for this buffer. |
| 340 | Rect mCrop; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 341 | |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 342 | // mTransform is the current transform flags for this buffer slot. This |
| 343 | // gets set to mNextTransform each time queueBuffer gets called for this |
| 344 | // slot. |
| 345 | uint32_t mTransform; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 346 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 347 | // mScalingMode is the current scaling mode for this buffer slot. This |
| 348 | // gets set to mNextScalingMode each time queueBuffer gets called for |
| 349 | // this slot. |
| 350 | uint32_t mScalingMode; |
| 351 | |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 352 | // mTimestamp is the current timestamp for this buffer slot. This gets |
| 353 | // to set by queueBuffer each time this slot is queued. |
| 354 | int64_t mTimestamp; |
Sunita Nadampalli | a929748 | 2011-11-09 18:23:41 -0600 | [diff] [blame] | 355 | |
| 356 | // mFrameNumber is the number of the queued frame for this slot. |
| 357 | uint64_t mFrameNumber; |
| 358 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 359 | // mFence is the EGL sync object that must signal before the buffer |
| 360 | // associated with this buffer slot may be dequeued. It is initialized |
| 361 | // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based |
| 362 | // on a compile-time option) set to a new sync object in updateTexImage. |
| 363 | EGLSyncKHR mFence; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | // mSlots is the array of buffer slots that must be mirrored on the client |
| 367 | // side. This allows buffer ownership to be transferred between the client |
| 368 | // and server without sending a GraphicBuffer over binder. The entire array |
| 369 | // is initialized to NULL at construction time, and buffers are allocated |
| 370 | // for a slot when requestBuffer is called with that slot's index. |
| 371 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
| 372 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 373 | // mDefaultWidth holds the default width of allocated buffers. It is used |
| 374 | // in requestBuffers() if a width and height of zero is specified. |
| 375 | uint32_t mDefaultWidth; |
| 376 | |
| 377 | // mDefaultHeight holds the default height of allocated buffers. It is used |
| 378 | // in requestBuffers() if a width and height of zero is specified. |
| 379 | uint32_t mDefaultHeight; |
| 380 | |
| 381 | // mPixelFormat holds the pixel format of allocated buffers. It is used |
| 382 | // in requestBuffers() if a format of zero is specified. |
| 383 | uint32_t mPixelFormat; |
| 384 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 385 | // mBufferCount is the number of buffer slots that the client and server |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 386 | // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed |
| 387 | // by calling setBufferCount or setBufferCountServer |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 388 | int mBufferCount; |
| 389 | |
Jamie Gennis | ae468f4 | 2011-06-12 14:11:39 -0700 | [diff] [blame] | 390 | // mClientBufferCount is the number of buffer slots requested by the client. |
| 391 | // The default is zero, which means the client doesn't care how many buffers |
| 392 | // there is. |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 393 | int mClientBufferCount; |
| 394 | |
| 395 | // mServerBufferCount buffer count requested by the server-side |
| 396 | int mServerBufferCount; |
| 397 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 398 | // mCurrentTexture is the buffer slot index of the buffer that is currently |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 399 | // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, |
| 400 | // indicating that no buffer slot is currently bound to the texture. Note, |
| 401 | // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean |
| 402 | // that no buffer is bound to the texture. A call to setBufferCount will |
| 403 | // reset mCurrentTexture to INVALID_BUFFER_SLOT. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 404 | int mCurrentTexture; |
| 405 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 406 | // mCurrentTextureBuf is the graphic buffer of the current texture. It's |
| 407 | // possible that this buffer is not associated with any buffer slot, so we |
Jamie Gennis | 29c8702 | 2011-07-19 12:11:52 -0700 | [diff] [blame] | 408 | // must track it separately in order to support the getCurrentBuffer method. |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 409 | sp<GraphicBuffer> mCurrentTextureBuf; |
| 410 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 411 | // mCurrentCrop is the crop rectangle that applies to the current texture. |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 412 | // It gets set each time updateTexImage is called. |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 413 | Rect mCurrentCrop; |
| 414 | |
| 415 | // mCurrentTransform is the transform identifier for the current texture. It |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 416 | // gets set each time updateTexImage is called. |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 417 | uint32_t mCurrentTransform; |
| 418 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 419 | // mCurrentScalingMode is the scaling mode for the current texture. It gets |
| 420 | // set to each time updateTexImage is called. |
| 421 | uint32_t mCurrentScalingMode; |
| 422 | |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 423 | // mCurrentTransformMatrix is the transform matrix for the current texture. |
| 424 | // It gets computed by computeTransformMatrix each time updateTexImage is |
| 425 | // called. |
| 426 | float mCurrentTransformMatrix[16]; |
| 427 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 428 | // mCurrentTimestamp is the timestamp for the current texture. It |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 429 | // gets set each time updateTexImage is called. |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 430 | int64_t mCurrentTimestamp; |
| 431 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 432 | // mNextCrop is the crop rectangle that will be used for the next buffer |
| 433 | // that gets queued. It is set by calling setCrop. |
| 434 | Rect mNextCrop; |
| 435 | |
| 436 | // mNextTransform is the transform identifier that will be used for the next |
| 437 | // buffer that gets queued. It is set by calling setTransform. |
| 438 | uint32_t mNextTransform; |
| 439 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 440 | // mNextScalingMode is the scaling mode that will be used for the next |
| 441 | // buffers that get queued. It is set by calling setScalingMode. |
| 442 | int mNextScalingMode; |
| 443 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 444 | // mTexName is the name of the OpenGL texture to which streamed images will |
Pannag Sanketi | 292a31a | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 445 | // be bound when updateTexImage is called. It is set at construction time |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 446 | // changed with a call to setTexName. |
| 447 | const GLuint mTexName; |
| 448 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 449 | // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to |
| 450 | // allocate new GraphicBuffer objects. |
| 451 | sp<IGraphicBufferAlloc> mGraphicBufferAlloc; |
| 452 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 453 | // mFrameAvailableListener is the listener object that will be called when a |
| 454 | // new frame becomes available. If it is not NULL it will be called from |
| 455 | // queueBuffer. |
| 456 | sp<FrameAvailableListener> mFrameAvailableListener; |
| 457 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 458 | // mSynchronousMode whether we're in synchronous mode or not |
| 459 | bool mSynchronousMode; |
| 460 | |
Grace Kloba | 14a0e58 | 2011-06-23 21:21:47 -0700 | [diff] [blame] | 461 | // mAllowSynchronousMode whether we allow synchronous mode or not |
| 462 | const bool mAllowSynchronousMode; |
| 463 | |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 464 | // mConnectedApi indicates the API that is currently connected to this |
| 465 | // SurfaceTexture. It defaults to NO_CONNECTED_API (= 0), and gets updated |
| 466 | // by the connect and disconnect methods. |
| 467 | int mConnectedApi; |
| 468 | |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 469 | // mDequeueCondition condition used for dequeueBuffer in synchronous mode |
| 470 | mutable Condition mDequeueCondition; |
| 471 | |
| 472 | // mQueue is a FIFO of queued buffers used in synchronous mode |
| 473 | typedef Vector<int> Fifo; |
| 474 | Fifo mQueue; |
| 475 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 476 | // mAbandoned indicates that the SurfaceTexture will no longer be used to |
| 477 | // consume images buffers pushed to it using the ISurfaceTexture interface. |
| 478 | // It is initialized to false, and set to true in the abandon method. A |
| 479 | // SurfaceTexture that has been abandoned will return the NO_INIT error from |
| 480 | // all ISurfaceTexture methods capable of returning an error. |
| 481 | bool mAbandoned; |
| 482 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 483 | // mName is a string used to identify the SurfaceTexture in log messages. |
| 484 | // It is set by the setName method. |
| 485 | String8 mName; |
| 486 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 487 | // mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync |
| 488 | // extension should be used to prevent buffers from being dequeued before |
| 489 | // it's safe for them to be written. It gets set at construction time and |
| 490 | // never changes. |
| 491 | const bool mUseFenceSync; |
| 492 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 493 | // mMutex is the mutex used to prevent concurrent access to the member |
| 494 | // variables of SurfaceTexture objects. It must be locked whenever the |
| 495 | // member variables are accessed. |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 496 | mutable Mutex mMutex; |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 497 | |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 498 | // mTexTarget is the GL texture target with which the GL texture object is |
| 499 | // associated. It is set in the constructor and never changed. It is |
| 500 | // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android |
| 501 | // Browser. In that case it is set to GL_TEXTURE_2D to allow |
| 502 | // glCopyTexSubImage to read from the texture. This is a hack to work |
| 503 | // around a GL driver limitation on the number of FBO attachments, which the |
| 504 | // browser's tile cache exceeds. |
| 505 | const GLenum mTexTarget; |
Sunita Nadampalli | a929748 | 2011-11-09 18:23:41 -0600 | [diff] [blame] | 506 | |
| 507 | // mFrameCounter is the free running counter, incremented for every buffer queued |
| 508 | // with the surface Texture. |
| 509 | uint64_t mFrameCounter; |
| 510 | |
| 511 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | // ---------------------------------------------------------------------------- |
| 515 | }; // namespace android |
| 516 | |
| 517 | #endif // ANDROID_GUI_SURFACETEXTURE_H |