Surface.h revision 3be1c6b60a188dc10025e2ce156c11fac050625d
1012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)/* 2012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * Copyright (C) 2010 The Android Open Source Project 341fd29532b52bb66e3e1f4620053713fd0299f6eDimitry Ivanov * 4012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License"); 5012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * you may not use this file except in compliance with the License. 6012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * You may obtain a copy of the License at 7012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * 8012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * http://www.apache.org/licenses/LICENSE-2.0 9012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * 10012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software 11012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS, 12012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * See the License for the specific language governing permissions and 14012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) * limitations under the License. 15012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) */ 16012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) 17012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#ifndef ANDROID_GUI_SURFACE_H 18012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#define ANDROID_GUI_SURFACE_H 19012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) 202675a9e343fa749aba57b1bfcc8f7f1f5db48c72Josh Gao#include <gui/IGraphicBufferProducer.h> 21012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#include <gui/BufferQueue.h> 22aad1a39dfbdbb3cd2f9b1ba7f5c65fe68f179065David 'Digit' Turner 23012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#include <ui/ANativeObjectBase.h> 24aad1a39dfbdbb3cd2f9b1ba7f5c65fe68f179065David 'Digit' Turner#include <ui/Region.h> 25012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) 26012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#include <utils/RefBase.h> 27012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#include <utils/threads.h> 28012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)#include <utils/KeyedVector.h> 29012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) 3012bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)struct ANativeWindow_Buffer; 3112bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) 3212bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)namespace android { 3312bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) 3412bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)/* 3512bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * An implementation of ANativeWindow that feeds graphics buffers into a 3612bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * BufferQueue. 3712bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * 3812bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * This is typically used by programs that want to render frames through 3912bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * some means (maybe OpenGL, a software renderer, or a hardware decoder) 4012bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * and have the frames they create forwarded to SurfaceFlinger for 4112bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) * compositing. For example, a video decoder could render a frame and call 42183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by 43183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) * Surface. Surface then forwards the buffers through Binder IPC 44183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) * to the BufferQueue's producer interface, providing the new frame to a 45183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) * consumer such as GLConsumer. 46183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) */ 47183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)class Surface 48183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) : public ANativeObjectBase<ANativeWindow, Surface, RefBase> 49183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles){ 50183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)public: 51183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) 52183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) /* 53183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) * creates a Surface from the given IGraphicBufferProducer (which concrete 54183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) * implementation is a BufferQueue). 5504dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov * 5604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov * Surface is mainly state-less while it's disconnected, it can be 5704dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov * viewed as a glorified IGraphicBufferProducer holder. It's therefore 5804dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov * safe to create other Surfaces from the same IGraphicBufferProducer. 5904dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov * 60a6c1279098f24a675d0df74ce1946f5d534b425eDmitriy Ivanov * However, once a Surface is connected, it'll prevent other Surfaces 6107e5bc152d8a3ad4c50808bb86f3c0f2c5e2f514Dmitriy Ivanov * referring to the same IGraphicBufferProducer to become connected and 6207e5bc152d8a3ad4c50808bb86f3c0f2c5e2f514Dmitriy Ivanov * therefore prevent them to be used as actual producers of buffers. 63a6c1279098f24a675d0df74ce1946f5d534b425eDmitriy Ivanov * 6407e5bc152d8a3ad4c50808bb86f3c0f2c5e2f514Dmitriy Ivanov * the controlledByApp flag indicates that this Surface (producer) is 659b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov * controlled by the application. This flag is used at connect time. 669b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov */ 679b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false); 689b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov 699b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this 709b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov * Surface was created with. Usually it's an error to use the 719b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov * IGraphicBufferProducer while the Surface is connected. 729b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov */ 739b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov sp<IGraphicBufferProducer> getIGraphicBufferProducer() const; 749b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov 759b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov /* convenience function to check that the given surface is non NULL as 769b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov * well as its IGraphicBufferProducer */ 778a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov static bool isValid(const sp<Surface>& surface) { 788a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov return surface != NULL && surface->getIGraphicBufferProducer() != NULL; 798a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov } 808a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov 818a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov /* Attaches a sideband buffer stream to the Surface's IGraphicBufferProducer. 828a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov * 838a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov * A sideband stream is a device-specific mechanism for passing buffers 84126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * from the producer to the consumer without using dequeueBuffer/ 85126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * queueBuffer. If a sideband stream is present, the consumer can choose 86126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * whether to acquire buffers from the sideband stream or from the queued 87126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * buffers. 88126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * 89126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * Passing NULL or a different stream handle will detach the previous 90126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * handle if any. 91126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov */ 92126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov void setSidebandStream(const sp<NativeHandle>& stream); 93126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov 94126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov /* Allocates buffers based on the current dimensions/format. 95126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * 96126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * This function will allocate up to the maximum number of buffers 97126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * permitted by the current BufferQueue configuration. It will use the 98126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * default format and dimensions. This is most useful to avoid an allocation 99126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * delay during dequeueBuffer. If there are already the maximum number of 100126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov * buffers allocated, this function has no effect. 101126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov */ 10242d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov void allocateBuffers(); 10342d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov 10442d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanovprotected: 10542d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov virtual ~Surface(); 10642d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov 107012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)private: 10812bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) // can't be copied 109183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) Surface& operator = (const Surface& rhs); 110183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) Surface(const Surface& rhs); 11104dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov 11207e5bc152d8a3ad4c50808bb86f3c0f2c5e2f514Dmitriy Ivanov // ANativeWindow hooks 1139b82136b987bc01224e3b42732334ea27c97d188Dmitriy Ivanov static int hook_cancelBuffer(ANativeWindow* window, 1148a1162835597be38705b381ac34c07da17932568Dmitriy Ivanov ANativeWindowBuffer* buffer, int fenceFd); 115126af757c6d6a6447c19236df8d98ba07f21996fDmitriy Ivanov static int hook_dequeueBuffer(ANativeWindow* window, 11642d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov ANativeWindowBuffer** buffer, int* fenceFd); 11742d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov static int hook_perform(ANativeWindow* window, int operation, ...); 118012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) static int hook_query(const ANativeWindow* window, int what, int* value); 119012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) static int hook_queueBuffer(ANativeWindow* window, 12042d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov ANativeWindowBuffer* buffer, int fenceFd); 12142d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov static int hook_setSwapInterval(ANativeWindow* window, int interval); 122012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) 1233a8646fc5d9f1a9304251a1d1c8ff9e523d81b40Dmitriy Ivanov static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, 12412bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) ANativeWindowBuffer* buffer); 12512bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, 126183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) ANativeWindowBuffer** buffer); 12704dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, 128a6c1279098f24a675d0df74ce1946f5d534b425eDmitriy Ivanov ANativeWindowBuffer* buffer); 12942d5fcb9f494eb45de3b6bf759f4a18076e84728Dmitriy Ivanov static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, 130012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) ANativeWindowBuffer* buffer); 131012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) 1323b2096a9d6edd74622b94cacc76b77100f919b84Elliott Hughes int dispatchConnect(va_list args); 13314adff1cfa06a3d4d3281a9bf7848b556d84c20dJosh Gao int dispatchDisconnect(va_list args); 134012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) int dispatchSetBufferCount(va_list args); 135012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) int dispatchSetBuffersGeometry(va_list args); 136012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) int dispatchSetBuffersDimensions(va_list args); 137012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) int dispatchSetBuffersUserDimensions(va_list args); 138 int dispatchSetBuffersFormat(va_list args); 139 int dispatchSetScalingMode(va_list args); 140 int dispatchSetBuffersTransform(va_list args); 141 int dispatchSetBuffersStickyTransform(va_list args); 142 int dispatchSetBuffersTimestamp(va_list args); 143 int dispatchSetCrop(va_list args); 144 int dispatchSetPostTransformCrop(va_list args); 145 int dispatchSetUsage(va_list args); 146 int dispatchLock(va_list args); 147 int dispatchUnlockAndPost(va_list args); 148 int dispatchSetSidebandStream(va_list args); 149 150protected: 151 virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); 152 virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); 153 virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); 154 virtual int perform(int operation, va_list args); 155 virtual int query(int what, int* value) const; 156 virtual int setSwapInterval(int interval); 157 158 virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer); 159 160 virtual int connect(int api); 161 virtual int disconnect(int api); 162 virtual int setBufferCount(int bufferCount); 163 virtual int setBuffersDimensions(uint32_t width, uint32_t height); 164 virtual int setBuffersUserDimensions(uint32_t width, uint32_t height); 165 virtual int setBuffersFormat(PixelFormat format); 166 virtual int setScalingMode(int mode); 167 virtual int setBuffersTransform(uint32_t transform); 168 virtual int setBuffersStickyTransform(uint32_t transform); 169 virtual int setBuffersTimestamp(int64_t timestamp); 170 virtual int setCrop(Rect const* rect); 171 virtual int setUsage(uint32_t reqUsage); 172 173public: 174 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); 175 virtual int unlockAndPost(); 176 177protected: 178 enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS }; 179 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; 180 181private: 182 void freeAllBuffers(); 183 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; 184 185 struct BufferSlot { 186 sp<GraphicBuffer> buffer; 187 Region dirtyRegion; 188 }; 189 190 // mSurfaceTexture is the interface to the surface texture server. All 191 // operations on the surface texture client ultimately translate into 192 // interactions with the server using this interface. 193 // TODO: rename to mBufferProducer 194 sp<IGraphicBufferProducer> mGraphicBufferProducer; 195 196 // mSlots stores the buffers that have been allocated for each buffer slot. 197 // It is initialized to null pointers, and gets filled in with the result of 198 // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a 199 // slot that has not yet been used. The buffer allocated to a slot will also 200 // be replaced if the requested buffer usage or geometry differs from that 201 // of the buffer allocated to a slot. 202 BufferSlot mSlots[NUM_BUFFER_SLOTS]; 203 204 // mReqWidth is the buffer width that will be requested at the next dequeue 205 // operation. It is initialized to 1. 206 uint32_t mReqWidth; 207 208 // mReqHeight is the buffer height that will be requested at the next 209 // dequeue operation. It is initialized to 1. 210 uint32_t mReqHeight; 211 212 // mReqFormat is the buffer pixel format that will be requested at the next 213 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. 214 PixelFormat mReqFormat; 215 216 // mReqUsage is the set of buffer usage flags that will be requested 217 // at the next deuque operation. It is initialized to 0. 218 uint32_t mReqUsage; 219 220 // mTimestamp is the timestamp that will be used for the next buffer queue 221 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that 222 // a timestamp is auto-generated when queueBuffer is called. 223 int64_t mTimestamp; 224 225 // mCrop is the crop rectangle that will be used for the next buffer 226 // that gets queued. It is set by calling setCrop. 227 Rect mCrop; 228 229 // mScalingMode is the scaling mode that will be used for the next 230 // buffers that get queued. It is set by calling setScalingMode. 231 int mScalingMode; 232 233 // mTransform is the transform identifier that will be used for the next 234 // buffer that gets queued. It is set by calling setTransform. 235 uint32_t mTransform; 236 237 // mStickyTransform is a transform that is applied on top of mTransform 238 // in each buffer that is queued. This is typically used to force the 239 // compositor to apply a transform, and will prevent the transform hint 240 // from being set by the compositor. 241 uint32_t mStickyTransform; 242 243 // mDefaultWidth is default width of the buffers, regardless of the 244 // native_window_set_buffers_dimensions call. 245 uint32_t mDefaultWidth; 246 247 // mDefaultHeight is default height of the buffers, regardless of the 248 // native_window_set_buffers_dimensions call. 249 uint32_t mDefaultHeight; 250 251 // mUserWidth, if non-zero, is an application-specified override 252 // of mDefaultWidth. This is lower priority than the width set by 253 // native_window_set_buffers_dimensions. 254 uint32_t mUserWidth; 255 256 // mUserHeight, if non-zero, is an application-specified override 257 // of mDefaultHeight. This is lower priority than the height set 258 // by native_window_set_buffers_dimensions. 259 uint32_t mUserHeight; 260 261 // mTransformHint is the transform probably applied to buffers of this 262 // window. this is only a hint, actual transform may differ. 263 uint32_t mTransformHint; 264 265 // mProducerControlledByApp whether this buffer producer is controlled 266 // by the application 267 bool mProducerControlledByApp; 268 269 // mSwapIntervalZero set if we should drop buffers at queue() time to 270 // achieve an asynchronous swap interval 271 bool mSwapIntervalZero; 272 273 // mConsumerRunningBehind whether the consumer is running more than 274 // one buffer behind the producer. 275 mutable bool mConsumerRunningBehind; 276 277 // mMutex is the mutex used to prevent concurrent access to the member 278 // variables of Surface objects. It must be locked whenever the 279 // member variables are accessed. 280 mutable Mutex mMutex; 281 282 // must be used from the lock/unlock thread 283 sp<GraphicBuffer> mLockedBuffer; 284 sp<GraphicBuffer> mPostedBuffer; 285 bool mConnectedToCpu; 286 287 // must be accessed from lock/unlock thread only 288 Region mDirtyRegion; 289}; 290 291}; // namespace android 292 293#endif // ANDROID_GUI_SURFACE_H 294