SurfaceTexture.h revision 1b20cde313b5ef8acdace742328df867956d24cb
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; 37 38class SurfaceTexture : public BnSurfaceTexture { 39public: 40 enum { MIN_BUFFER_SLOTS = 3 }; 41 enum { NUM_BUFFER_SLOTS = 32 }; 42 43 struct FrameAvailableListener : public virtual RefBase { 44 virtual void onFrameAvailable() = 0; 45 }; 46 47 // tex indicates the name OpenGL texture to which images are to be streamed. 48 // This texture name cannot be changed once the SurfaceTexture is created. 49 SurfaceTexture(GLuint tex); 50 51 virtual ~SurfaceTexture(); 52 53 // setBufferCount updates the number of available buffer slots. After 54 // calling this all buffer slots are both unallocated and owned by the 55 // SurfaceTexture object (i.e. they are not owned by the client). 56 virtual status_t setBufferCount(int bufferCount); 57 58 virtual sp<GraphicBuffer> requestBuffer(int buf, uint32_t w, uint32_t h, 59 uint32_t format, uint32_t usage); 60 61 // dequeueBuffer gets the next buffer slot index for the client to use. If a 62 // buffer slot is available then that slot index is written to the location 63 // pointed to by the buf argument and a status of OK is returned. If no 64 // slot is available then a status of -EBUSY is returned and buf is 65 // unmodified. 66 virtual status_t dequeueBuffer(int *buf); 67 68 virtual status_t queueBuffer(int buf); 69 virtual void cancelBuffer(int buf); 70 virtual status_t setCrop(const Rect& reg); 71 virtual status_t setTransform(uint32_t transform); 72 73 // updateTexImage sets the image contents of the target texture to that of 74 // the most recently queued buffer. 75 // 76 // This call may only be made while the OpenGL ES context to which the 77 // target texture belongs is bound to the calling thread. 78 status_t updateTexImage(); 79 80 // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix 81 // associated with the texture image set by the most recent call to 82 // updateTexImage. 83 // 84 // This transform matrix maps 2D homogeneous texture coordinates of the form 85 // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture 86 // coordinate that should be used to sample that location from the texture. 87 // Sampling the texture outside of the range of this transform is undefined. 88 // 89 // This transform is necessary to compensate for transforms that the stream 90 // content producer may implicitly apply to the content. By forcing users of 91 // a SurfaceTexture to apply this transform we avoid performing an extra 92 // copy of the data that would be needed to hide the transform from the 93 // user. 94 // 95 // The matrix is stored in column-major order so that it may be passed 96 // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv 97 // functions. 98 void getTransformMatrix(float mtx[16]); 99 100 // setFrameAvailableListener sets the listener object that will be notified 101 // when a new frame becomes available. 102 void setFrameAvailableListener(const sp<FrameAvailableListener>& l); 103 104 // getAllocator retrieves the binder object that must be referenced as long 105 // as the GraphicBuffers dequeued from this SurfaceTexture are referenced. 106 // Holding this binder reference prevents SurfaceFlinger from freeing the 107 // buffers before the client is done with them. 108 sp<IBinder> getAllocator(); 109 110private: 111 112 // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for 113 // all slots. 114 void freeAllBuffers(); 115 116 // createImage creates a new EGLImage from a GraphicBuffer. 117 EGLImageKHR createImage(EGLDisplay dpy, 118 const sp<GraphicBuffer>& graphicBuffer); 119 120 enum { INVALID_BUFFER_SLOT = -1 }; 121 122 struct BufferSlot { 123 // mGraphicBuffer points to the buffer allocated for this slot or is NULL 124 // if no buffer has been allocated. 125 sp<GraphicBuffer> mGraphicBuffer; 126 127 // mEglImage is the EGLImage created from mGraphicBuffer. 128 EGLImageKHR mEglImage; 129 130 // mEglDisplay is the EGLDisplay used to create mEglImage. 131 EGLDisplay mEglDisplay; 132 133 // mOwnedByClient indicates whether the slot is currently accessible to a 134 // client and should not be used by the SurfaceTexture object. It gets 135 // set to true when dequeueBuffer returns the slot and is reset to false 136 // when the client calls either queueBuffer or cancelBuffer on the slot. 137 bool mOwnedByClient; 138 }; 139 140 // mSlots is the array of buffer slots that must be mirrored on the client 141 // side. This allows buffer ownership to be transferred between the client 142 // and server without sending a GraphicBuffer over binder. The entire array 143 // is initialized to NULL at construction time, and buffers are allocated 144 // for a slot when requestBuffer is called with that slot's index. 145 BufferSlot mSlots[NUM_BUFFER_SLOTS]; 146 147 // mBufferCount is the number of buffer slots that the client and server 148 // must maintain. It defaults to MIN_BUFFER_SLOTS and can be changed by 149 // calling setBufferCount. 150 int mBufferCount; 151 152 // mCurrentTexture is the buffer slot index of the buffer that is currently 153 // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, 154 // indicating that no buffer slot is currently bound to the texture. Note, 155 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean 156 // that no buffer is bound to the texture. A call to setBufferCount will 157 // reset mCurrentTexture to INVALID_BUFFER_SLOT. 158 int mCurrentTexture; 159 160 // mCurrentTextureBuf is the graphic buffer of the current texture. It's 161 // possible that this buffer is not associated with any buffer slot, so we 162 // must track it separately in order to properly use 163 // IGraphicBufferAlloc::freeAllGraphicBuffersExcept. 164 sp<GraphicBuffer> mCurrentTextureBuf; 165 166 // mCurrentCrop is the crop rectangle that applies to the current texture. 167 // It gets set to mLastQueuedCrop each time updateTexImage is called. 168 Rect mCurrentCrop; 169 170 // mCurrentTransform is the transform identifier for the current texture. It 171 // gets set to mLastQueuedTransform each time updateTexImage is called. 172 uint32_t mCurrentTransform; 173 174 // mLastQueued is the buffer slot index of the most recently enqueued buffer. 175 // At construction time it is initialized to INVALID_BUFFER_SLOT, and is 176 // updated each time queueBuffer is called. 177 int mLastQueued; 178 179 // mLastQueuedCrop is the crop rectangle for the buffer that was most 180 // recently queued. This gets set to mNextCrop each time queueBuffer gets 181 // called. 182 Rect mLastQueuedCrop; 183 184 // mLastQueuedTransform is the transform identifier for the buffer that was 185 // most recently queued. This gets set to mNextTransform each time 186 // queueBuffer gets called. 187 uint32_t mLastQueuedTransform; 188 189 // mNextCrop is the crop rectangle that will be used for the next buffer 190 // that gets queued. It is set by calling setCrop. 191 Rect mNextCrop; 192 193 // mNextTransform is the transform identifier that will be used for the next 194 // buffer that gets queued. It is set by calling setTransform. 195 uint32_t mNextTransform; 196 197 // mTexName is the name of the OpenGL texture to which streamed images will 198 // be bound when updateTexImage is called. It is set at construction time 199 // changed with a call to setTexName. 200 const GLuint mTexName; 201 202 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to 203 // allocate new GraphicBuffer objects. 204 sp<IGraphicBufferAlloc> mGraphicBufferAlloc; 205 206 // mAllocdBuffers is mirror of the list of buffers that SurfaceFlinger is 207 // referencing. This is kept so that gralloc implementations do not need to 208 // properly handle the case where SurfaceFlinger no longer holds a reference 209 // to a buffer, but other processes do. 210 Vector<sp<GraphicBuffer> > mAllocdBuffers; 211 212 // mFrameAvailableListener is the listener object that will be called when a 213 // new frame becomes available. If it is not NULL it will be called from 214 // queueBuffer. 215 sp<FrameAvailableListener> mFrameAvailableListener; 216 217 // mMutex is the mutex used to prevent concurrent access to the member 218 // variables of SurfaceTexture objects. It must be locked whenever the 219 // member variables are accessed. 220 Mutex mMutex; 221}; 222 223// ---------------------------------------------------------------------------- 224}; // namespace android 225 226#endif // ANDROID_GUI_SURFACETEXTURE_H 227