IGraphicBufferConsumer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2013 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_IGRAPHICBUFFERCONSUMER_H
  17. #define ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Errors.h>
  21. #include <utils/RefBase.h>
  22. #include <utils/Timers.h>
  23. #include <binder/IInterface.h>
  24. #include <ui/PixelFormat.h>
  25. #include <ui/Rect.h>
  26. #include <EGL/egl.h>
  27. #include <EGL/eglext.h>
  28. namespace android {
  29. // ----------------------------------------------------------------------------
  30. class BufferItem;
  31. class Fence;
  32. class GraphicBuffer;
  33. class IConsumerListener;
  34. class NativeHandle;
  35. class IGraphicBufferConsumer : public IInterface {
  36. public:
  37. enum {
  38. // Returned by releaseBuffer, after which the consumer must
  39. // free any references to the just-released buffer that it might have.
  40. STALE_BUFFER_SLOT = 1,
  41. // Returned by dequeueBuffer if there are no pending buffers available.
  42. NO_BUFFER_AVAILABLE,
  43. // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
  44. PRESENT_LATER,
  45. };
  46. // acquireBuffer attempts to acquire ownership of the next pending buffer in
  47. // the BufferQueue. If no buffer is pending then it returns
  48. // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
  49. // information about the buffer is returned in BufferItem.
  50. //
  51. // If the buffer returned had previously been
  52. // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
  53. // NULL and it is assumed that the consumer still holds a reference to the
  54. // buffer.
  55. //
  56. // If presentWhen is non-zero, it indicates the time when the buffer will
  57. // be displayed on screen. If the buffer's timestamp is farther in the
  58. // future, the buffer won't be acquired, and PRESENT_LATER will be
  59. // returned. The presentation time is in nanoseconds, and the time base
  60. // is CLOCK_MONOTONIC.
  61. //
  62. // If maxFrameNumber is non-zero, it indicates that acquireBuffer should
  63. // only return a buffer with a frame number less than or equal to
  64. // maxFrameNumber. If no such frame is available (such as when a buffer has
  65. // been replaced but the consumer has not received the onFrameReplaced
  66. // callback), then PRESENT_LATER will be returned.
  67. //
  68. // Return of NO_ERROR means the operation completed as normal.
  69. //
  70. // Return of a positive value means the operation could not be completed
  71. // at this time, but the user should try again later:
  72. // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
  73. // * PRESENT_LATER - the buffer's timestamp is farther in the future
  74. //
  75. // Return of a negative value means an error has occurred:
  76. // * INVALID_OPERATION - too many buffers have been acquired
  77. virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
  78. uint64_t maxFrameNumber = 0) = 0;
  79. // detachBuffer attempts to remove all ownership of the buffer in the given
  80. // slot from the buffer queue. If this call succeeds, the slot will be
  81. // freed, and there will be no way to obtain the buffer from this interface.
  82. // The freed slot will remain unallocated until either it is selected to
  83. // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
  84. // to the slot. The buffer must have already been acquired.
  85. //
  86. // Return of a value other than NO_ERROR means an error has occurred:
  87. // * BAD_VALUE - the given slot number is invalid, either because it is
  88. // out of the range [0, NUM_BUFFER_SLOTS) or because the slot
  89. // it refers to is not currently acquired.
  90. virtual status_t detachBuffer(int slot) = 0;
  91. // attachBuffer attempts to transfer ownership of a buffer to the buffer
  92. // queue. If this call succeeds, it will be as if this buffer was acquired
  93. // from the returned slot number. As such, this call will fail if attaching
  94. // this buffer would cause too many buffers to be simultaneously acquired.
  95. //
  96. // If the buffer is successfully attached, its frameNumber is initialized
  97. // to 0. This must be passed into the releaseBuffer call or else the buffer
  98. // will be deallocated as stale.
  99. //
  100. // Return of a value other than NO_ERROR means an error has occurred:
  101. // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of
  102. // the buffer did not match the buffer queue.
  103. // * INVALID_OPERATION - cannot attach the buffer because it would cause too
  104. // many buffers to be acquired.
  105. // * NO_MEMORY - no free slots available
  106. virtual status_t attachBuffer(int *outSlot,
  107. const sp<GraphicBuffer>& buffer) = 0;
  108. // releaseBuffer releases a buffer slot from the consumer back to the
  109. // BufferQueue. This may be done while the buffer's contents are still
  110. // being accessed. The fence will signal when the buffer is no longer
  111. // in use. frameNumber is used to indentify the exact buffer returned.
  112. //
  113. // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
  114. // any references to the just-released buffer that it might have, as if it
  115. // had received a onBuffersReleased() call with a mask set for the released
  116. // buffer.
  117. //
  118. // Note that the dependencies on EGL will be removed once we switch to using
  119. // the Android HW Sync HAL.
  120. //
  121. // Return of NO_ERROR means the operation completed as normal.
  122. //
  123. // Return of a positive value means the operation could not be completed
  124. // at this time, but the user should try again later:
  125. // * STALE_BUFFER_SLOT - see above (second paragraph)
  126. //
  127. // Return of a negative value means an error has occurred:
  128. // * BAD_VALUE - one of the following could've happened:
  129. // * the buffer slot was invalid
  130. // * the fence was NULL
  131. // * the buffer slot specified is not in the acquired state
  132. virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
  133. EGLDisplay display, EGLSyncKHR fence,
  134. const sp<Fence>& releaseFence) = 0;
  135. // consumerConnect connects a consumer to the BufferQueue. Only one
  136. // consumer may be connected, and when that consumer disconnects the
  137. // BufferQueue is placed into the "abandoned" state, causing most
  138. // interactions with the BufferQueue by the producer to fail.
  139. // controlledByApp indicates whether the consumer is controlled by
  140. // the application.
  141. //
  142. // consumer may not be NULL.
  143. //
  144. // Return of a value other than NO_ERROR means an error has occurred:
  145. // * NO_INIT - the buffer queue has been abandoned
  146. // * BAD_VALUE - a NULL consumer was provided
  147. virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
  148. // consumerDisconnect disconnects a consumer from the BufferQueue. All
  149. // buffers will be freed and the BufferQueue is placed in the "abandoned"
  150. // state, causing most interactions with the BufferQueue by the producer to
  151. // fail.
  152. //
  153. // Return of a value other than NO_ERROR means an error has occurred:
  154. // * BAD_VALUE - no consumer is currently connected
  155. virtual status_t consumerDisconnect() = 0;
  156. // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
  157. // Each bit index with a 1 corresponds to a released buffer slot with that
  158. // index value. In particular, a released buffer is one that has
  159. // been released by the BufferQueue but have not yet been released by the consumer.
  160. //
  161. // This should be called from the onBuffersReleased() callback.
  162. //
  163. // Return of a value other than NO_ERROR means an error has occurred:
  164. // * NO_INIT - the buffer queue has been abandoned.
  165. virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
  166. // setDefaultBufferSize is used to set the size of buffers returned by
  167. // dequeueBuffer when a width and height of zero is requested. Default
  168. // is 1x1.
  169. //
  170. // Return of a value other than NO_ERROR means an error has occurred:
  171. // * BAD_VALUE - either w or h was zero
  172. virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
  173. // setDefaultMaxBufferCount sets the default value for the maximum buffer
  174. // count (the initial default is 2). If the producer has requested a
  175. // buffer count using setBufferCount, the default buffer count will only
  176. // take effect if the producer sets the count back to zero.
  177. //
  178. // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
  179. //
  180. // Return of a value other than NO_ERROR means an error has occurred:
  181. // * BAD_VALUE - bufferCount was out of range (see above).
  182. virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
  183. // disableAsyncBuffer disables the extra buffer used in async mode
  184. // (when both producer and consumer have set their "isControlledByApp"
  185. // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
  186. //
  187. // This can only be called before consumerConnect().
  188. //
  189. // Return of a value other than NO_ERROR means an error has occurred:
  190. // * INVALID_OPERATION - attempting to call this after consumerConnect.
  191. virtual status_t disableAsyncBuffer() = 0;
  192. // setMaxAcquiredBufferCount sets the maximum number of buffers that can
  193. // be acquired by the consumer at one time (default 1). This call will
  194. // fail if a producer is connected to the BufferQueue.
  195. //
  196. // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
  197. //
  198. // Return of a value other than NO_ERROR means an error has occurred:
  199. // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
  200. // * INVALID_OPERATION - attempting to call this after a producer connected.
  201. virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
  202. // setConsumerName sets the name used in logging
  203. virtual void setConsumerName(const String8& name) = 0;
  204. // setDefaultBufferFormat allows the BufferQueue to create
  205. // GraphicBuffers of a defaultFormat if no format is specified
  206. // in dequeueBuffer.
  207. // The initial default is PIXEL_FORMAT_RGBA_8888.
  208. //
  209. // Return of a value other than NO_ERROR means an unknown error has occurred.
  210. virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
  211. // setDefaultBufferDataSpace is a request to the producer to provide buffers
  212. // of the indicated dataSpace. The producer may ignore this request.
  213. // The initial default is HAL_DATASPACE_UNKNOWN.
  214. //
  215. // Return of a value other than NO_ERROR means an unknown error has occurred.
  216. virtual status_t setDefaultBufferDataSpace(
  217. android_dataspace defaultDataSpace) = 0;
  218. // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
  219. // These are merged with the bits passed to dequeueBuffer. The values are
  220. // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
  221. //
  222. // Return of a value other than NO_ERROR means an unknown error has occurred.
  223. virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
  224. // setTransformHint bakes in rotation to buffers so overlays can be used.
  225. // The values are enumerated in window.h, e.g.
  226. // NATIVE_WINDOW_TRANSFORM_ROT_90. The default is 0 (no transform).
  227. //
  228. // Return of a value other than NO_ERROR means an unknown error has occurred.
  229. virtual status_t setTransformHint(uint32_t hint) = 0;
  230. // Retrieve the sideband buffer stream, if any.
  231. virtual sp<NativeHandle> getSidebandStream() const = 0;
  232. // dump state into a string
  233. virtual void dump(String8& result, const char* prefix) const = 0;
  234. public:
  235. DECLARE_META_INTERFACE(GraphicBufferConsumer);
  236. };
  237. // ----------------------------------------------------------------------------
  238. class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
  239. {
  240. public:
  241. virtual status_t onTransact( uint32_t code,
  242. const Parcel& data,
  243. Parcel* reply,
  244. uint32_t flags = 0);
  245. };
  246. // ----------------------------------------------------------------------------
  247. }; // namespace android
  248. #endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H