Surface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #ifndef ANDROID_GUI_SURFACE_H
  17. #define ANDROID_GUI_SURFACE_H
  18. #include <gui/IGraphicBufferProducer.h>
  19. #include <gui/BufferQueue.h>
  20. #include <ui/ANativeObjectBase.h>
  21. #include <ui/Region.h>
  22. #include <utils/RefBase.h>
  23. #include <utils/threads.h>
  24. #include <utils/KeyedVector.h>
  25. struct ANativeWindow_Buffer;
  26. namespace android {
  27. /*
  28. * An implementation of ANativeWindow that feeds graphics buffers into a
  29. * BufferQueue.
  30. *
  31. * This is typically used by programs that want to render frames through
  32. * some means (maybe OpenGL, a software renderer, or a hardware decoder)
  33. * and have the frames they create forwarded to SurfaceFlinger for
  34. * compositing. For example, a video decoder could render a frame and call
  35. * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
  36. * Surface. Surface then forwards the buffers through Binder IPC
  37. * to the BufferQueue's producer interface, providing the new frame to a
  38. * consumer such as GLConsumer.
  39. */
  40. class Surface
  41. : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
  42. {
  43. public:
  44. /*
  45. * creates a Surface from the given IGraphicBufferProducer (which concrete
  46. * implementation is a BufferQueue).
  47. *
  48. * Surface is mainly state-less while it's disconnected, it can be
  49. * viewed as a glorified IGraphicBufferProducer holder. It's therefore
  50. * safe to create other Surfaces from the same IGraphicBufferProducer.
  51. *
  52. * However, once a Surface is connected, it'll prevent other Surfaces
  53. * referring to the same IGraphicBufferProducer to become connected and
  54. * therefore prevent them to be used as actual producers of buffers.
  55. *
  56. * the controlledByApp flag indicates that this Surface (producer) is
  57. * controlled by the application. This flag is used at connect time.
  58. */
  59. Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false);
  60. /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this
  61. * Surface was created with. Usually it's an error to use the
  62. * IGraphicBufferProducer while the Surface is connected.
  63. */
  64. sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
  65. /* convenience function to check that the given surface is non NULL as
  66. * well as its IGraphicBufferProducer */
  67. static bool isValid(const sp<Surface>& surface) {
  68. return surface != NULL && surface->getIGraphicBufferProducer() != NULL;
  69. }
  70. /* Attaches a sideband buffer stream to the Surface's IGraphicBufferProducer.
  71. *
  72. * A sideband stream is a device-specific mechanism for passing buffers
  73. * from the producer to the consumer without using dequeueBuffer/
  74. * queueBuffer. If a sideband stream is present, the consumer can choose
  75. * whether to acquire buffers from the sideband stream or from the queued
  76. * buffers.
  77. *
  78. * Passing NULL or a different stream handle will detach the previous
  79. * handle if any.
  80. */
  81. void setSidebandStream(const sp<NativeHandle>& stream);
  82. /* Allocates buffers based on the current dimensions/format.
  83. *
  84. * This function will allocate up to the maximum number of buffers
  85. * permitted by the current BufferQueue configuration. It will use the
  86. * default format and dimensions. This is most useful to avoid an allocation
  87. * delay during dequeueBuffer. If there are already the maximum number of
  88. * buffers allocated, this function has no effect.
  89. */
  90. void allocateBuffers();
  91. /* Sets the generation number on the IGraphicBufferProducer and updates the
  92. * generation number on any buffers attached to the Surface after this call.
  93. * See IGBP::setGenerationNumber for more information. */
  94. status_t setGenerationNumber(uint32_t generationNumber);
  95. // See IGraphicBufferProducer::getConsumerName
  96. String8 getConsumerName() const;
  97. protected:
  98. virtual ~Surface();
  99. private:
  100. // can't be copied
  101. Surface& operator = (const Surface& rhs);
  102. Surface(const Surface& rhs);
  103. // ANativeWindow hooks
  104. static int hook_cancelBuffer(ANativeWindow* window,
  105. ANativeWindowBuffer* buffer, int fenceFd);
  106. static int hook_dequeueBuffer(ANativeWindow* window,
  107. ANativeWindowBuffer** buffer, int* fenceFd);
  108. static int hook_perform(ANativeWindow* window, int operation, ...);
  109. static int hook_query(const ANativeWindow* window, int what, int* value);
  110. static int hook_queueBuffer(ANativeWindow* window,
  111. ANativeWindowBuffer* buffer, int fenceFd);
  112. static int hook_setSwapInterval(ANativeWindow* window, int interval);
  113. static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
  114. ANativeWindowBuffer* buffer);
  115. static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
  116. ANativeWindowBuffer** buffer);
  117. static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
  118. ANativeWindowBuffer* buffer);
  119. static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
  120. ANativeWindowBuffer* buffer);
  121. int dispatchConnect(va_list args);
  122. int dispatchDisconnect(va_list args);
  123. int dispatchSetBufferCount(va_list args);
  124. int dispatchSetBuffersGeometry(va_list args);
  125. int dispatchSetBuffersDimensions(va_list args);
  126. int dispatchSetBuffersUserDimensions(va_list args);
  127. int dispatchSetBuffersFormat(va_list args);
  128. int dispatchSetScalingMode(va_list args);
  129. int dispatchSetBuffersTransform(va_list args);
  130. int dispatchSetBuffersStickyTransform(va_list args);
  131. int dispatchSetBuffersTimestamp(va_list args);
  132. int dispatchSetCrop(va_list args);
  133. int dispatchSetPostTransformCrop(va_list args);
  134. int dispatchSetUsage(va_list args);
  135. int dispatchLock(va_list args);
  136. int dispatchUnlockAndPost(va_list args);
  137. int dispatchSetSidebandStream(va_list args);
  138. int dispatchSetBuffersDataSpace(va_list args);
  139. int dispatchSetSurfaceDamage(va_list args);
  140. protected:
  141. virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
  142. virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
  143. virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
  144. virtual int perform(int operation, va_list args);
  145. virtual int query(int what, int* value) const;
  146. virtual int setSwapInterval(int interval);
  147. virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
  148. virtual int connect(int api);
  149. virtual int disconnect(int api);
  150. virtual int setBufferCount(int bufferCount);
  151. virtual int setBuffersDimensions(uint32_t width, uint32_t height);
  152. virtual int setBuffersUserDimensions(uint32_t width, uint32_t height);
  153. virtual int setBuffersFormat(PixelFormat format);
  154. virtual int setScalingMode(int mode);
  155. virtual int setBuffersTransform(uint32_t transform);
  156. virtual int setBuffersStickyTransform(uint32_t transform);
  157. virtual int setBuffersTimestamp(int64_t timestamp);
  158. virtual int setBuffersDataSpace(android_dataspace dataSpace);
  159. virtual int setCrop(Rect const* rect);
  160. virtual int setUsage(uint32_t reqUsage);
  161. virtual void setSurfaceDamage(android_native_rect_t* rects, size_t numRects);
  162. public:
  163. virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
  164. virtual int unlockAndPost();
  165. virtual int connect(int api, const sp<IProducerListener>& listener);
  166. virtual int detachNextBuffer(sp<GraphicBuffer>* outBuffer,
  167. sp<Fence>* outFence);
  168. virtual int attachBuffer(ANativeWindowBuffer*);
  169. protected:
  170. enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
  171. enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
  172. private:
  173. void freeAllBuffers();
  174. int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
  175. struct BufferSlot {
  176. sp<GraphicBuffer> buffer;
  177. Region dirtyRegion;
  178. };
  179. // mSurfaceTexture is the interface to the surface texture server. All
  180. // operations on the surface texture client ultimately translate into
  181. // interactions with the server using this interface.
  182. // TODO: rename to mBufferProducer
  183. sp<IGraphicBufferProducer> mGraphicBufferProducer;
  184. // mSlots stores the buffers that have been allocated for each buffer slot.
  185. // It is initialized to null pointers, and gets filled in with the result of
  186. // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
  187. // slot that has not yet been used. The buffer allocated to a slot will also
  188. // be replaced if the requested buffer usage or geometry differs from that
  189. // of the buffer allocated to a slot.
  190. BufferSlot mSlots[NUM_BUFFER_SLOTS];
  191. // mReqWidth is the buffer width that will be requested at the next dequeue
  192. // operation. It is initialized to 1.
  193. uint32_t mReqWidth;
  194. // mReqHeight is the buffer height that will be requested at the next
  195. // dequeue operation. It is initialized to 1.
  196. uint32_t mReqHeight;
  197. // mReqFormat is the buffer pixel format that will be requested at the next
  198. // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
  199. PixelFormat mReqFormat;
  200. // mReqUsage is the set of buffer usage flags that will be requested
  201. // at the next deuque operation. It is initialized to 0.
  202. uint32_t mReqUsage;
  203. // mTimestamp is the timestamp that will be used for the next buffer queue
  204. // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
  205. // a timestamp is auto-generated when queueBuffer is called.
  206. int64_t mTimestamp;
  207. // mDataSpace is the buffer dataSpace that will be used for the next buffer
  208. // queue operation. It defaults to HAL_DATASPACE_UNKNOWN, which
  209. // means that the buffer contains some type of color data.
  210. android_dataspace mDataSpace;
  211. // mCrop is the crop rectangle that will be used for the next buffer
  212. // that gets queued. It is set by calling setCrop.
  213. Rect mCrop;
  214. // mScalingMode is the scaling mode that will be used for the next
  215. // buffers that get queued. It is set by calling setScalingMode.
  216. int mScalingMode;
  217. // mTransform is the transform identifier that will be used for the next
  218. // buffer that gets queued. It is set by calling setTransform.
  219. uint32_t mTransform;
  220. // mStickyTransform is a transform that is applied on top of mTransform
  221. // in each buffer that is queued. This is typically used to force the
  222. // compositor to apply a transform, and will prevent the transform hint
  223. // from being set by the compositor.
  224. uint32_t mStickyTransform;
  225. // mDefaultWidth is default width of the buffers, regardless of the
  226. // native_window_set_buffers_dimensions call.
  227. uint32_t mDefaultWidth;
  228. // mDefaultHeight is default height of the buffers, regardless of the
  229. // native_window_set_buffers_dimensions call.
  230. uint32_t mDefaultHeight;
  231. // mUserWidth, if non-zero, is an application-specified override
  232. // of mDefaultWidth. This is lower priority than the width set by
  233. // native_window_set_buffers_dimensions.
  234. uint32_t mUserWidth;
  235. // mUserHeight, if non-zero, is an application-specified override
  236. // of mDefaultHeight. This is lower priority than the height set
  237. // by native_window_set_buffers_dimensions.
  238. uint32_t mUserHeight;
  239. // mTransformHint is the transform probably applied to buffers of this
  240. // window. this is only a hint, actual transform may differ.
  241. uint32_t mTransformHint;
  242. // mProducerControlledByApp whether this buffer producer is controlled
  243. // by the application
  244. bool mProducerControlledByApp;
  245. // mSwapIntervalZero set if we should drop buffers at queue() time to
  246. // achieve an asynchronous swap interval
  247. bool mSwapIntervalZero;
  248. // mConsumerRunningBehind whether the consumer is running more than
  249. // one buffer behind the producer.
  250. mutable bool mConsumerRunningBehind;
  251. // mMutex is the mutex used to prevent concurrent access to the member
  252. // variables of Surface objects. It must be locked whenever the
  253. // member variables are accessed.
  254. mutable Mutex mMutex;
  255. // must be used from the lock/unlock thread
  256. sp<GraphicBuffer> mLockedBuffer;
  257. sp<GraphicBuffer> mPostedBuffer;
  258. bool mConnectedToCpu;
  259. // When a CPU producer is attached, this reflects the region that the
  260. // producer wished to update as well as whether the Surface was able to copy
  261. // the previous buffer back to allow a partial update.
  262. //
  263. // When a non-CPU producer is attached, this reflects the surface damage
  264. // (the change since the previous frame) passed in by the producer.
  265. Region mDirtyRegion;
  266. // Stores the current generation number. See setGenerationNumber and
  267. // IGraphicBufferProducer::setGenerationNumber for more information.
  268. uint32_t mGenerationNumber;
  269. };
  270. }; // namespace android
  271. #endif // ANDROID_GUI_SURFACE_H