IGraphicBufferProducer.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_IGRAPHICBUFFERPRODUCER_H
  17. #define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <utils/Errors.h>
  21. #include <utils/RefBase.h>
  22. #include <binder/IInterface.h>
  23. #include <ui/Fence.h>
  24. #include <ui/GraphicBuffer.h>
  25. #include <ui/Rect.h>
  26. #include <ui/Region.h>
  27. namespace android {
  28. // ----------------------------------------------------------------------------
  29. class IProducerListener;
  30. class NativeHandle;
  31. class Surface;
  32. /*
  33. * This class defines the Binder IPC interface for the producer side of
  34. * a queue of graphics buffers. It's used to send graphics data from one
  35. * component to another. For example, a class that decodes video for
  36. * playback might use this to provide frames. This is typically done
  37. * indirectly, through Surface.
  38. *
  39. * The underlying mechanism is a BufferQueue, which implements
  40. * BnGraphicBufferProducer. In normal operation, the producer calls
  41. * dequeueBuffer() to get an empty buffer, fills it with data, then
  42. * calls queueBuffer() to make it available to the consumer.
  43. *
  44. * This class was previously called ISurfaceTexture.
  45. */
  46. class IGraphicBufferProducer : public IInterface
  47. {
  48. public:
  49. DECLARE_META_INTERFACE(GraphicBufferProducer);
  50. enum {
  51. // A flag returned by dequeueBuffer when the client needs to call
  52. // requestBuffer immediately thereafter.
  53. BUFFER_NEEDS_REALLOCATION = 0x1,
  54. // A flag returned by dequeueBuffer when all mirrored slots should be
  55. // released by the client. This flag should always be processed first.
  56. RELEASE_ALL_BUFFERS = 0x2,
  57. };
  58. // requestBuffer requests a new buffer for the given index. The server (i.e.
  59. // the IGraphicBufferProducer implementation) assigns the newly created
  60. // buffer to the given slot index, and the client is expected to mirror the
  61. // slot->buffer mapping so that it's not necessary to transfer a
  62. // GraphicBuffer for every dequeue operation.
  63. //
  64. // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
  65. //
  66. // Return of a value other than NO_ERROR means an error has occurred:
  67. // * NO_INIT - the buffer queue has been abandoned.
  68. // * BAD_VALUE - one of the two conditions occurred:
  69. // * slot was out of range (see above)
  70. // * buffer specified by the slot is not dequeued
  71. virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
  72. // setBufferCount sets the number of buffer slots available. Calling this
  73. // will also cause all buffer slots to be emptied. The caller should empty
  74. // its mirrored copy of the buffer slots when calling this method.
  75. //
  76. // This function should not be called when there are any dequeued buffer
  77. // slots, doing so will result in a BAD_VALUE error returned.
  78. //
  79. // The buffer count should be at most NUM_BUFFER_SLOTS (inclusive), but at least
  80. // the minimum undequeued buffer count (exclusive). The minimum value
  81. // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS).
  82. // In particular the range is (minUndequeudBuffers, NUM_BUFFER_SLOTS].
  83. //
  84. // The buffer count may also be set to 0 (the default), to indicate that
  85. // the producer does not wish to set a value.
  86. //
  87. // Return of a value other than NO_ERROR means an error has occurred:
  88. // * NO_INIT - the buffer queue has been abandoned.
  89. // * BAD_VALUE - one of the below conditions occurred:
  90. // * bufferCount was out of range (see above)
  91. // * client has one or more buffers dequeued
  92. virtual status_t setBufferCount(int bufferCount) = 0;
  93. // dequeueBuffer requests a new buffer slot for the client to use. Ownership
  94. // of the slot is transfered to the client, meaning that the server will not
  95. // use the contents of the buffer associated with that slot.
  96. //
  97. // The slot index returned may or may not contain a buffer (client-side).
  98. // If the slot is empty the client should call requestBuffer to assign a new
  99. // buffer to that slot.
  100. //
  101. // Once the client is done filling this buffer, it is expected to transfer
  102. // buffer ownership back to the server with either cancelBuffer on
  103. // the dequeued slot or to fill in the contents of its associated buffer
  104. // contents and call queueBuffer.
  105. //
  106. // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is
  107. // expected to call requestBuffer immediately.
  108. //
  109. // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is
  110. // expected to release all of the mirrored slot->buffer mappings.
  111. //
  112. // The fence parameter will be updated to hold the fence associated with
  113. // the buffer. The contents of the buffer must not be overwritten until the
  114. // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written
  115. // immediately.
  116. //
  117. // The async parameter sets whether we're in asynchronous mode for this
  118. // dequeueBuffer() call.
  119. //
  120. // The width and height parameters must be no greater than the minimum of
  121. // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
  122. // An error due to invalid dimensions might not be reported until
  123. // updateTexImage() is called. If width and height are both zero, the
  124. // default values specified by setDefaultBufferSize() are used instead.
  125. //
  126. // If the format is 0, the default format will be used.
  127. //
  128. // The usage argument specifies gralloc buffer usage flags. The values
  129. // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER. These
  130. // will be merged with the usage flags specified by
  131. // IGraphicBufferConsumer::setConsumerUsageBits.
  132. //
  133. // This call will block until a buffer is available to be dequeued. If
  134. // both the producer and consumer are controlled by the app, then this call
  135. // can never block and will return WOULD_BLOCK if no buffer is available.
  136. //
  137. // A non-negative value with flags set (see above) will be returned upon
  138. // success.
  139. //
  140. // Return of a negative means an error has occurred:
  141. // * NO_INIT - the buffer queue has been abandoned.
  142. // * BAD_VALUE - both in async mode and buffer count was less than the
  143. // max numbers of buffers that can be allocated at once.
  144. // * INVALID_OPERATION - cannot attach the buffer because it would cause
  145. // too many buffers to be dequeued, either because
  146. // the producer already has a single buffer dequeued
  147. // and did not set a buffer count, or because a
  148. // buffer count was set and this call would cause
  149. // it to be exceeded.
  150. // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled
  151. // since both the producer/consumer are controlled by app
  152. // * NO_MEMORY - out of memory, cannot allocate the graphics buffer.
  153. //
  154. // All other negative values are an unknown error returned downstream
  155. // from the graphics allocator (typically errno).
  156. virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, bool async,
  157. uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) = 0;
  158. // detachBuffer attempts to remove all ownership of the buffer in the given
  159. // slot from the buffer queue. If this call succeeds, the slot will be
  160. // freed, and there will be no way to obtain the buffer from this interface.
  161. // The freed slot will remain unallocated until either it is selected to
  162. // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
  163. // to the slot. The buffer must have already been dequeued, and the caller
  164. // must already possesses the sp<GraphicBuffer> (i.e., must have called
  165. // requestBuffer).
  166. //
  167. // Return of a value other than NO_ERROR means an error has occurred:
  168. // * NO_INIT - the buffer queue has been abandoned.
  169. // * BAD_VALUE - the given slot number is invalid, either because it is
  170. // out of the range [0, NUM_BUFFER_SLOTS), or because the slot
  171. // it refers to is not currently dequeued and requested.
  172. virtual status_t detachBuffer(int slot) = 0;
  173. // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer,
  174. // and detachBuffer in sequence, except for two things:
  175. //
  176. // 1) It is unnecessary to know the dimensions, format, or usage of the
  177. // next buffer.
  178. // 2) It will not block, since if it cannot find an appropriate buffer to
  179. // return, it will return an error instead.
  180. //
  181. // Only slots that are free but still contain a GraphicBuffer will be
  182. // considered, and the oldest of those will be returned. outBuffer is
  183. // equivalent to outBuffer from the requestBuffer call, and outFence is
  184. // equivalent to fence from the dequeueBuffer call.
  185. //
  186. // Return of a value other than NO_ERROR means an error has occurred:
  187. // * NO_INIT - the buffer queue has been abandoned.
  188. // * BAD_VALUE - either outBuffer or outFence were NULL.
  189. // * NO_MEMORY - no slots were found that were both free and contained a
  190. // GraphicBuffer.
  191. virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
  192. sp<Fence>* outFence) = 0;
  193. // attachBuffer attempts to transfer ownership of a buffer to the buffer
  194. // queue. If this call succeeds, it will be as if this buffer was dequeued
  195. // from the returned slot number. As such, this call will fail if attaching
  196. // this buffer would cause too many buffers to be simultaneously dequeued.
  197. //
  198. // If attachBuffer returns the RELEASE_ALL_BUFFERS flag, the caller is
  199. // expected to release all of the mirrored slot->buffer mappings.
  200. //
  201. // A non-negative value with flags set (see above) will be returned upon
  202. // success.
  203. //
  204. // Return of a negative value means an error has occurred:
  205. // * NO_INIT - the buffer queue has been abandoned.
  206. // * BAD_VALUE - outSlot or buffer were NULL, invalid combination of
  207. // async mode and buffer count override, or the generation
  208. // number of the buffer did not match the buffer queue.
  209. // * INVALID_OPERATION - cannot attach the buffer because it would cause
  210. // too many buffers to be dequeued, either because
  211. // the producer already has a single buffer dequeued
  212. // and did not set a buffer count, or because a
  213. // buffer count was set and this call would cause
  214. // it to be exceeded.
  215. // * WOULD_BLOCK - no buffer slot is currently available, and blocking is
  216. // disabled since both the producer/consumer are
  217. // controlled by the app.
  218. virtual status_t attachBuffer(int* outSlot,
  219. const sp<GraphicBuffer>& buffer) = 0;
  220. // queueBuffer indicates that the client has finished filling in the
  221. // contents of the buffer associated with slot and transfers ownership of
  222. // that slot back to the server.
  223. //
  224. // It is not valid to call queueBuffer on a slot that is not owned
  225. // by the client or one for which a buffer associated via requestBuffer
  226. // (an attempt to do so will fail with a return value of BAD_VALUE).
  227. //
  228. // In addition, the input must be described by the client (as documented
  229. // below). Any other properties (zero point, etc)
  230. // are client-dependent, and should be documented by the client.
  231. //
  232. // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
  233. //
  234. // Upon success, the output will be filled with meaningful values
  235. // (refer to the documentation below).
  236. //
  237. // Return of a value other than NO_ERROR means an error has occurred:
  238. // * NO_INIT - the buffer queue has been abandoned.
  239. // * BAD_VALUE - one of the below conditions occurred:
  240. // * fence was NULL
  241. // * scaling mode was unknown
  242. // * both in async mode and buffer count was less than the
  243. // max numbers of buffers that can be allocated at once
  244. // * slot index was out of range (see above).
  245. // * the slot was not in the dequeued state
  246. // * the slot was enqueued without requesting a buffer
  247. // * crop rect is out of bounds of the buffer dimensions
  248. struct QueueBufferInput : public Flattenable<QueueBufferInput> {
  249. friend class Flattenable<QueueBufferInput>;
  250. inline QueueBufferInput(const Parcel& parcel);
  251. // timestamp - a monotonically increasing value in nanoseconds
  252. // isAutoTimestamp - if the timestamp was synthesized at queue time
  253. // dataSpace - description of the contents, interpretation depends on format
  254. // crop - a crop rectangle that's used as a hint to the consumer
  255. // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h>
  256. // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h>
  257. // async - if the buffer is queued in asynchronous mode
  258. // fence - a fence that the consumer must wait on before reading the buffer,
  259. // set this to Fence::NO_FENCE if the buffer is ready immediately
  260. // sticky - the sticky transform set in Surface (only used by the LEGACY
  261. // camera mode).
  262. inline QueueBufferInput(int64_t timestamp, bool isAutoTimestamp,
  263. android_dataspace dataSpace, const Rect& crop, int scalingMode,
  264. uint32_t transform, bool async, const sp<Fence>& fence,
  265. uint32_t sticky = 0)
  266. : timestamp(timestamp), isAutoTimestamp(isAutoTimestamp),
  267. dataSpace(dataSpace), crop(crop), scalingMode(scalingMode),
  268. transform(transform), stickyTransform(sticky),
  269. async(async), fence(fence), surfaceDamage() { }
  270. inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp,
  271. android_dataspace* outDataSpace,
  272. Rect* outCrop, int* outScalingMode,
  273. uint32_t* outTransform, bool* outAsync, sp<Fence>* outFence,
  274. uint32_t* outStickyTransform = NULL) const {
  275. *outTimestamp = timestamp;
  276. *outIsAutoTimestamp = bool(isAutoTimestamp);
  277. *outDataSpace = dataSpace;
  278. *outCrop = crop;
  279. *outScalingMode = scalingMode;
  280. *outTransform = transform;
  281. *outAsync = bool(async);
  282. *outFence = fence;
  283. if (outStickyTransform != NULL) {
  284. *outStickyTransform = stickyTransform;
  285. }
  286. }
  287. // Flattenable protocol
  288. size_t getFlattenedSize() const;
  289. size_t getFdCount() const;
  290. status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
  291. status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
  292. const Region& getSurfaceDamage() const { return surfaceDamage; }
  293. void setSurfaceDamage(const Region& damage) { surfaceDamage = damage; }
  294. private:
  295. int64_t timestamp;
  296. int isAutoTimestamp;
  297. android_dataspace dataSpace;
  298. Rect crop;
  299. int scalingMode;
  300. uint32_t transform;
  301. uint32_t stickyTransform;
  302. int async;
  303. sp<Fence> fence;
  304. Region surfaceDamage;
  305. };
  306. // QueueBufferOutput must be a POD structure
  307. struct __attribute__ ((__packed__)) QueueBufferOutput {
  308. inline QueueBufferOutput() { }
  309. // outWidth - filled with default width applied to the buffer
  310. // outHeight - filled with default height applied to the buffer
  311. // outTransformHint - filled with default transform applied to the buffer
  312. // outNumPendingBuffers - num buffers queued that haven't yet been acquired
  313. // (counting the currently queued buffer)
  314. inline void deflate(uint32_t* outWidth,
  315. uint32_t* outHeight,
  316. uint32_t* outTransformHint,
  317. uint32_t* outNumPendingBuffers) const {
  318. *outWidth = width;
  319. *outHeight = height;
  320. *outTransformHint = transformHint;
  321. *outNumPendingBuffers = numPendingBuffers;
  322. }
  323. inline void inflate(uint32_t inWidth, uint32_t inHeight,
  324. uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
  325. width = inWidth;
  326. height = inHeight;
  327. transformHint = inTransformHint;
  328. numPendingBuffers = inNumPendingBuffers;
  329. }
  330. private:
  331. uint32_t width;
  332. uint32_t height;
  333. uint32_t transformHint;
  334. uint32_t numPendingBuffers;
  335. };
  336. virtual status_t queueBuffer(int slot,
  337. const QueueBufferInput& input, QueueBufferOutput* output) = 0;
  338. // cancelBuffer indicates that the client does not wish to fill in the
  339. // buffer associated with slot and transfers ownership of the slot back to
  340. // the server.
  341. //
  342. // The buffer is not queued for use by the consumer.
  343. //
  344. // The buffer will not be overwritten until the fence signals. The fence
  345. // will usually be the one obtained from dequeueBuffer.
  346. virtual void cancelBuffer(int slot, const sp<Fence>& fence) = 0;
  347. // query retrieves some information for this surface
  348. // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h>
  349. //
  350. // Return of a value other than NO_ERROR means an error has occurred:
  351. // * NO_INIT - the buffer queue has been abandoned.
  352. // * BAD_VALUE - what was out of range
  353. virtual int query(int what, int* value) = 0;
  354. // connect attempts to connect a client API to the IGraphicBufferProducer.
  355. // This must be called before any other IGraphicBufferProducer methods are
  356. // called except for getAllocator. A consumer must be already connected.
  357. //
  358. // This method will fail if the connect was previously called on the
  359. // IGraphicBufferProducer and no corresponding disconnect call was made.
  360. //
  361. // The listener is an optional binder callback object that can be used if
  362. // the producer wants to be notified when the consumer releases a buffer
  363. // back to the BufferQueue. It is also used to detect the death of the
  364. // producer. If only the latter functionality is desired, there is a
  365. // DummyProducerListener class in IProducerListener.h that can be used.
  366. //
  367. // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
  368. //
  369. // The producerControlledByApp should be set to true if the producer is hosted
  370. // by an untrusted process (typically app_process-forked processes). If both
  371. // the producer and the consumer are app-controlled then all buffer queues
  372. // will operate in async mode regardless of the async flag.
  373. //
  374. // Upon success, the output will be filled with meaningful data
  375. // (refer to QueueBufferOutput documentation above).
  376. //
  377. // Return of a value other than NO_ERROR means an error has occurred:
  378. // * NO_INIT - one of the following occurred:
  379. // * the buffer queue was abandoned
  380. // * no consumer has yet connected
  381. // * BAD_VALUE - one of the following has occurred:
  382. // * the producer is already connected
  383. // * api was out of range (see above).
  384. // * output was NULL.
  385. // * DEAD_OBJECT - the token is hosted by an already-dead process
  386. //
  387. // Additional negative errors may be returned by the internals, they
  388. // should be treated as opaque fatal unrecoverable errors.
  389. virtual status_t connect(const sp<IProducerListener>& listener,
  390. int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
  391. // disconnect attempts to disconnect a client API from the
  392. // IGraphicBufferProducer. Calling this method will cause any subsequent
  393. // calls to other IGraphicBufferProducer methods to fail except for
  394. // getAllocator and connect. Successfully calling connect after this will
  395. // allow the other methods to succeed again.
  396. //
  397. // This method will fail if the the IGraphicBufferProducer is not currently
  398. // connected to the specified client API.
  399. //
  400. // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
  401. //
  402. // Disconnecting from an abandoned IGraphicBufferProducer is legal and
  403. // is considered a no-op.
  404. //
  405. // Return of a value other than NO_ERROR means an error has occurred:
  406. // * BAD_VALUE - one of the following has occurred:
  407. // * the api specified does not match the one that was connected
  408. // * api was out of range (see above).
  409. // * DEAD_OBJECT - the token is hosted by an already-dead process
  410. virtual status_t disconnect(int api) = 0;
  411. // Attaches a sideband buffer stream to the IGraphicBufferProducer.
  412. //
  413. // A sideband stream is a device-specific mechanism for passing buffers
  414. // from the producer to the consumer without using dequeueBuffer/
  415. // queueBuffer. If a sideband stream is present, the consumer can choose
  416. // whether to acquire buffers from the sideband stream or from the queued
  417. // buffers.
  418. //
  419. // Passing NULL or a different stream handle will detach the previous
  420. // handle if any.
  421. virtual status_t setSidebandStream(const sp<NativeHandle>& stream) = 0;
  422. // Allocates buffers based on the given dimensions/format.
  423. //
  424. // This function will allocate up to the maximum number of buffers
  425. // permitted by the current BufferQueue configuration. It will use the
  426. // given format, dimensions, and usage bits, which are interpreted in the
  427. // same way as for dequeueBuffer, and the async flag must be set the same
  428. // way as for dequeueBuffer to ensure that the correct number of buffers are
  429. // allocated. This is most useful to avoid an allocation delay during
  430. // dequeueBuffer. If there are already the maximum number of buffers
  431. // allocated, this function has no effect.
  432. virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
  433. PixelFormat format, uint32_t usage) = 0;
  434. // Sets whether dequeueBuffer is allowed to allocate new buffers.
  435. //
  436. // Normally dequeueBuffer does not discriminate between free slots which
  437. // already have an allocated buffer and those which do not, and will
  438. // allocate a new buffer if the slot doesn't have a buffer or if the slot's
  439. // buffer doesn't match the requested size, format, or usage. This method
  440. // allows the producer to restrict the eligible slots to those which already
  441. // have an allocated buffer of the correct size, format, and usage. If no
  442. // eligible slot is available, dequeueBuffer will block or return an error
  443. // as usual.
  444. virtual status_t allowAllocation(bool allow) = 0;
  445. // Sets the current generation number of the BufferQueue.
  446. //
  447. // This generation number will be inserted into any buffers allocated by the
  448. // BufferQueue, and any attempts to attach a buffer with a different
  449. // generation number will fail. Buffers already in the queue are not
  450. // affected and will retain their current generation number. The generation
  451. // number defaults to 0.
  452. virtual status_t setGenerationNumber(uint32_t generationNumber) = 0;
  453. // Returns the name of the connected consumer.
  454. virtual String8 getConsumerName() const = 0;
  455. };
  456. // ----------------------------------------------------------------------------
  457. class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
  458. {
  459. public:
  460. virtual status_t onTransact( uint32_t code,
  461. const Parcel& data,
  462. Parcel* reply,
  463. uint32_t flags = 0);
  464. };
  465. // ----------------------------------------------------------------------------
  466. }; // namespace android
  467. #endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H