SurfaceTexture.h revision 8cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09ef
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> 29#include <utils/Vector.h> 30 31#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture" 32 33namespace android { 34// ---------------------------------------------------------------------------- 35 36class IGraphicBufferAlloc; 37class String8; 38 39class SurfaceTexture : public BnSurfaceTexture { 40public: 41 enum { MIN_UNDEQUEUED_BUFFERS = 2 }; 42 enum { 43 MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1, 44 MIN_SYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS 45 }; 46 enum { NUM_BUFFER_SLOTS = 32 }; 47 48 struct FrameAvailableListener : public virtual RefBase { 49 // 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. 54 virtual void onFrameAvailable() = 0; 55 }; 56 57 // 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 68 virtual sp<GraphicBuffer> requestBuffer(int buf); 69 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. 75 virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h, 76 uint32_t format, uint32_t usage); 77 78 // 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); 84 virtual void cancelBuffer(int buf); 85 virtual status_t setCrop(const Rect& reg); 86 virtual status_t setTransform(uint32_t transform); 87 88 virtual int query(int what, int* value); 89 90 // 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 97 // 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 104 // 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 109 // 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 114 // 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 134 // 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 142 // setFrameAvailableListener sets the listener object that will be notified 143 // when a new frame becomes available. 144 void setFrameAvailableListener(const sp<FrameAvailableListener>& l); 145 146 // 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 152 // 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 158 // 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 171 // 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 175protected: 176 177 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for 178 // all slots. 179 void freeAllBuffers(); 180 static bool isExternalFormat(uint32_t format); 181 static GLenum getTextureTarget(uint32_t format); 182 183private: 184 185 // createImage creates a new EGLImage from a GraphicBuffer. 186 EGLImageKHR createImage(EGLDisplay dpy, 187 const sp<GraphicBuffer>& graphicBuffer); 188 189 status_t setBufferCountServerLocked(int bufferCount); 190 191 enum { INVALID_BUFFER_SLOT = -1 }; 192 193 struct BufferSlot { 194 195 BufferSlot() 196 : mEglImage(EGL_NO_IMAGE_KHR), 197 mEglDisplay(EGL_NO_DISPLAY), 198 mBufferState(BufferSlot::FREE), 199 mRequestBufferCalled(false), 200 mTransform(0), 201 mTimestamp(0) { 202 mCrop.makeInvalid(); 203 } 204 205 // 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 215 // 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, 222 223 // 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; 247 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 253 // 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; 256 257 // 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; 261 262 // 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; 265 }; 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 274 // 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 286 // mBufferCount is the number of buffer slots that the client and server 287 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed 288 // by calling setBufferCount or setBufferCountServer 289 int mBufferCount; 290 291 // 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 299 // mCurrentTexture is the buffer slot index of the buffer that is currently 300 // 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. 305 int mCurrentTexture; 306 307 // mCurrentTextureTarget is the GLES texture target to be used with the 308 // current texture. 309 GLenum mCurrentTextureTarget; 310 311 // 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 317 // 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 325 // mCurrentTimestamp is the timestamp for the current texture. It 326 // gets set to mLastQueuedTimestamp each time updateTexImage is called. 327 int64_t mCurrentTimestamp; 328 329 // 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 337 // 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 342 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to 343 // allocate new GraphicBuffer objects. 344 sp<IGraphicBufferAlloc> mGraphicBufferAlloc; 345 346 // 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 351 // 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 361 // 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. 364 mutable Mutex mMutex; 365}; 366 367// ---------------------------------------------------------------------------- 368}; // namespace android 369 370#endif // ANDROID_GUI_SURFACETEXTURE_H 371