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> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 26 | #include <gui/BufferQueue.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 27 | |
| 28 | #include <ui/GraphicBuffer.h> |
| 29 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 30 | #include <utils/String8.h> |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 31 | #include <utils/Vector.h> |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 32 | #include <utils/threads.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 33 | |
| 34 | #define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture" |
| 35 | |
| 36 | namespace android { |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 39 | |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 40 | class String8; |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 41 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 42 | class SurfaceTexture : public virtual RefBase { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 43 | public: |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 44 | // This typedef allows external code to continue referencing |
| 45 | // SurfaceTexture::FrameAvailableListener during refactoring |
| 46 | typedef BufferQueue::FrameAvailableListener FrameAvailableListener; |
| 47 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 48 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 49 | // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the |
| 50 | // name of the OpenGL ES texture to which images are to be streamed. This |
| 51 | // texture name cannot be changed once the SurfaceTexture is created. |
| 52 | // allowSynchronousMode specifies whether or not synchronous mode can be |
| 53 | // enabled. texTarget specifies the OpenGL ES texture target to which the |
| 54 | // texture will be bound in updateTexImage. useFenceSync specifies whether |
| 55 | // fences should be used to synchronize access to buffers if that behavior |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 56 | // is enabled at compile-time. A custom bufferQueue can be specified |
| 57 | // if behavior for queue/dequeue/connect etc needs to be customized. |
| 58 | // Otherwise a default BufferQueue will be created and used. |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 59 | SurfaceTexture(GLuint tex, bool allowSynchronousMode = true, |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 60 | GLenum texTarget = GL_TEXTURE_EXTERNAL_OES, bool useFenceSync = true, |
| 61 | const sp<BufferQueue> &bufferQueue = 0); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 62 | |
| 63 | virtual ~SurfaceTexture(); |
| 64 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 65 | // updateTexImage sets the image contents of the target texture to that of |
| 66 | // the most recently queued buffer. |
| 67 | // |
| 68 | // This call may only be made while the OpenGL ES context to which the |
| 69 | // target texture belongs is bound to the calling thread. |
| 70 | status_t updateTexImage(); |
| 71 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 72 | // setBufferCountServer set the buffer count. If the client has requested |
| 73 | // a buffer count using setBufferCount, the server-buffer count will |
| 74 | // take effect once the client sets the count back to zero. |
| 75 | status_t setBufferCountServer(int bufferCount); |
| 76 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 77 | // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix |
| 78 | // associated with the texture image set by the most recent call to |
| 79 | // updateTexImage. |
| 80 | // |
| 81 | // This transform matrix maps 2D homogeneous texture coordinates of the form |
| 82 | // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture |
| 83 | // coordinate that should be used to sample that location from the texture. |
| 84 | // Sampling the texture outside of the range of this transform is undefined. |
| 85 | // |
| 86 | // This transform is necessary to compensate for transforms that the stream |
| 87 | // content producer may implicitly apply to the content. By forcing users of |
| 88 | // a SurfaceTexture to apply this transform we avoid performing an extra |
| 89 | // copy of the data that would be needed to hide the transform from the |
| 90 | // user. |
| 91 | // |
| 92 | // The matrix is stored in column-major order so that it may be passed |
| 93 | // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv |
| 94 | // functions. |
| 95 | void getTransformMatrix(float mtx[16]); |
| 96 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 97 | // getTimestamp retrieves the timestamp associated with the texture image |
| 98 | // set by the most recent call to updateTexImage. |
| 99 | // |
| 100 | // The timestamp is in nanoseconds, and is monotonically increasing. Its |
| 101 | // other semantics (zero point, etc) are source-dependent and should be |
| 102 | // documented by the source. |
| 103 | int64_t getTimestamp(); |
| 104 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 105 | // setFrameAvailableListener sets the listener object that will be notified |
| 106 | // when a new frame becomes available. |
Pannag Sanketi | 292a31a | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 107 | void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 108 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 109 | // getAllocator retrieves the binder object that must be referenced as long |
| 110 | // as the GraphicBuffers dequeued from this SurfaceTexture are referenced. |
| 111 | // Holding this binder reference prevents SurfaceFlinger from freeing the |
| 112 | // buffers before the client is done with them. |
| 113 | sp<IBinder> getAllocator(); |
| 114 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 115 | // setDefaultBufferSize is used to set the size of buffers returned by |
| 116 | // requestBuffers when a with and height of zero is requested. |
| 117 | // A call to setDefaultBufferSize() may trigger requestBuffers() to |
| 118 | // be called from the client. |
Mathias Agopian | 194c76c | 2011-11-10 14:34:26 -0800 | [diff] [blame] | 119 | // The width and height parameters must be no greater than the minimum of |
| 120 | // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). |
| 121 | // An error due to invalid dimensions might not be reported until |
| 122 | // updateTexImage() is called. |
| 123 | status_t setDefaultBufferSize(uint32_t width, uint32_t height); |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 124 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 125 | // getCurrentBuffer returns the buffer associated with the current image. |
| 126 | sp<GraphicBuffer> getCurrentBuffer() const; |
| 127 | |
| 128 | // getCurrentTextureTarget returns the texture target of the current |
| 129 | // texture as returned by updateTexImage(). |
| 130 | GLenum getCurrentTextureTarget() const; |
| 131 | |
| 132 | // getCurrentCrop returns the cropping rectangle of the current buffer |
| 133 | Rect getCurrentCrop() const; |
| 134 | |
| 135 | // getCurrentTransform returns the transform of the current buffer |
| 136 | uint32_t getCurrentTransform() const; |
| 137 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 138 | // getCurrentScalingMode returns the scaling mode of the current buffer |
| 139 | uint32_t getCurrentScalingMode() const; |
| 140 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 141 | // isSynchronousMode returns whether the SurfaceTexture is currently in |
| 142 | // synchronous mode. |
| 143 | bool isSynchronousMode() const; |
| 144 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 145 | // abandon frees all the buffers and puts the SurfaceTexture into the |
| 146 | // 'abandoned' state. Once put in this state the SurfaceTexture can never |
| 147 | // leave it. When in the 'abandoned' state, all methods of the |
| 148 | // ISurfaceTexture interface will fail with the NO_INIT error. |
| 149 | // |
| 150 | // Note that while calling this method causes all the buffers to be freed |
| 151 | // from the perspective of the the SurfaceTexture, if there are additional |
| 152 | // references on the buffers (e.g. if a buffer is referenced by a client or |
| 153 | // by OpenGL ES as a texture) then those buffer will remain allocated. |
| 154 | void abandon(); |
| 155 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 156 | // set the name of the SurfaceTexture that will be used to identify it in |
| 157 | // log messages. |
| 158 | void setName(const String8& name); |
| 159 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 160 | // These functions call the corresponding BufferQueue implementation |
| 161 | // so the refactoring can proceed smoothly |
| 162 | status_t setDefaultBufferFormat(uint32_t defaultFormat); |
| 163 | status_t setConsumerUsageBits(uint32_t usage); |
| 164 | status_t setTransformHint(uint32_t hint); |
| 165 | virtual status_t setSynchronousMode(bool enabled); |
| 166 | virtual status_t setBufferCount(int bufferCount); |
| 167 | virtual status_t connect(int api, |
| 168 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); |
| 169 | |
| 170 | sp<BufferQueue> getBufferQueue() const; |
| 171 | |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 172 | // dump our state in a String |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 173 | virtual void dump(String8& result) const; |
| 174 | virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const; |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 175 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 176 | protected: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 177 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 178 | static bool isExternalFormat(uint32_t format); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 179 | |
| 180 | private: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 181 | |
| 182 | // createImage creates a new EGLImage from a GraphicBuffer. |
| 183 | EGLImageKHR createImage(EGLDisplay dpy, |
| 184 | const sp<GraphicBuffer>& graphicBuffer); |
| 185 | |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 186 | // computeCurrentTransformMatrix computes the transform matrix for the |
| 187 | // current texture. It uses mCurrentTransform and the current GraphicBuffer |
| 188 | // to compute this matrix and stores it in mCurrentTransformMatrix. |
| 189 | void computeCurrentTransformMatrix(); |
| 190 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 191 | // mCurrentTextureBuf is the graphic buffer of the current texture. It's |
| 192 | // 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] | 193 | // must track it separately in order to support the getCurrentBuffer method. |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 194 | sp<GraphicBuffer> mCurrentTextureBuf; |
| 195 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 196 | // mCurrentCrop is the crop rectangle that applies to the current texture. |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 197 | // It gets set each time updateTexImage is called. |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 198 | Rect mCurrentCrop; |
| 199 | |
| 200 | // mCurrentTransform is the transform identifier for the current texture. It |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 201 | // gets set each time updateTexImage is called. |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 202 | uint32_t mCurrentTransform; |
| 203 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 204 | // mCurrentScalingMode is the scaling mode for the current texture. It gets |
| 205 | // set to each time updateTexImage is called. |
| 206 | uint32_t mCurrentScalingMode; |
| 207 | |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 208 | // mCurrentTransformMatrix is the transform matrix for the current texture. |
| 209 | // It gets computed by computeTransformMatrix each time updateTexImage is |
| 210 | // called. |
| 211 | float mCurrentTransformMatrix[16]; |
| 212 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 213 | // mCurrentTimestamp is the timestamp for the current texture. It |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 214 | // gets set each time updateTexImage is called. |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 215 | int64_t mCurrentTimestamp; |
| 216 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 217 | // 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] | 218 | // be bound when updateTexImage is called. It is set at construction time |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 219 | // changed with a call to setTexName. |
| 220 | const GLuint mTexName; |
| 221 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 222 | // mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync |
| 223 | // extension should be used to prevent buffers from being dequeued before |
| 224 | // it's safe for them to be written. It gets set at construction time and |
| 225 | // never changes. |
| 226 | const bool mUseFenceSync; |
| 227 | |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 228 | // mTexTarget is the GL texture target with which the GL texture object is |
| 229 | // associated. It is set in the constructor and never changed. It is |
| 230 | // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android |
| 231 | // Browser. In that case it is set to GL_TEXTURE_2D to allow |
| 232 | // glCopyTexSubImage to read from the texture. This is a hack to work |
| 233 | // around a GL driver limitation on the number of FBO attachments, which the |
| 234 | // browser's tile cache exceeds. |
| 235 | const GLenum mTexTarget; |
Sunita Nadampalli | a929748 | 2011-11-09 18:23:41 -0600 | [diff] [blame] | 236 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 237 | // SurfaceTexture maintains EGL information about GraphicBuffers that corresponds |
| 238 | // directly with BufferQueue's buffers |
| 239 | struct EGLSlot { |
| 240 | EGLSlot() |
| 241 | : mEglImage(EGL_NO_IMAGE_KHR), |
| 242 | mEglDisplay(EGL_NO_DISPLAY), |
| 243 | mFence(EGL_NO_SYNC_KHR) { |
| 244 | } |
| 245 | |
| 246 | sp<GraphicBuffer> mGraphicBuffer; |
| 247 | |
| 248 | // mEglImage is the EGLImage created from mGraphicBuffer. |
| 249 | EGLImageKHR mEglImage; |
| 250 | |
| 251 | // mEglDisplay is the EGLDisplay used to create mEglImage. |
| 252 | EGLDisplay mEglDisplay; |
| 253 | |
| 254 | // mFence is the EGL sync object that must signal before the buffer |
| 255 | // associated with this buffer slot may be dequeued. It is initialized |
| 256 | // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based |
| 257 | // on a compile-time option) set to a new sync object in updateTexImage. |
| 258 | EGLSyncKHR mFence; |
| 259 | }; |
| 260 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 261 | EGLSlot mEGLSlots[BufferQueue::NUM_BUFFER_SLOTS]; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 262 | |
| 263 | // mAbandoned indicates that the BufferQueue will no longer be used to |
| 264 | // consume images buffers pushed to it using the ISurfaceTexture interface. |
| 265 | // It is initialized to false, and set to true in the abandon method. A |
| 266 | // BufferQueue that has been abandoned will return the NO_INIT error from |
| 267 | // all ISurfaceTexture methods capable of returning an error. |
| 268 | bool mAbandoned; |
| 269 | |
| 270 | // mName is a string used to identify the SurfaceTexture in log messages. |
| 271 | // It can be set by the setName method. |
| 272 | String8 mName; |
| 273 | |
| 274 | // mMutex is the mutex used to prevent concurrent access to the member |
| 275 | // variables of SurfaceTexture objects. It must be locked whenever the |
| 276 | // member variables are accessed. |
| 277 | mutable Mutex mMutex; |
| 278 | |
| 279 | // mCurrentTexture is the buffer slot index of the buffer that is currently |
| 280 | // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, |
| 281 | // indicating that no buffer slot is currently bound to the texture. Note, |
| 282 | // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean |
| 283 | // that no buffer is bound to the texture. A call to setBufferCount will |
| 284 | // reset mCurrentTexture to INVALID_BUFFER_SLOT. |
| 285 | int mCurrentTexture; |
| 286 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame^] | 287 | // The SurfaceTexture has-a BufferQueue and is responsible for creating this object |
| 288 | // if none is supplied |
| 289 | sp<BufferQueue> mBufferQueue; |
| 290 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | // ---------------------------------------------------------------------------- |
| 294 | }; // namespace android |
| 295 | |
| 296 | #endif // ANDROID_GUI_SURFACETEXTURE_H |