BufferQueueProducer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2014 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_BUFFERQUEUEPRODUCER_H
  17. #define ANDROID_GUI_BUFFERQUEUEPRODUCER_H
  18. #include <gui/BufferQueueDefs.h>
  19. #include <gui/IGraphicBufferProducer.h>
  20. namespace android {
  21. class BufferSlot;
  22. class BufferQueueProducer : public BnGraphicBufferProducer,
  23. private IBinder::DeathRecipient {
  24. public:
  25. friend class BufferQueue; // Needed to access binderDied
  26. BufferQueueProducer(const sp<BufferQueueCore>& core);
  27. virtual ~BufferQueueProducer();
  28. // requestBuffer returns the GraphicBuffer for slot N.
  29. //
  30. // In normal operation, this is called the first time slot N is returned
  31. // by dequeueBuffer. It must be called again if dequeueBuffer returns
  32. // flags indicating that previously-returned buffers are no longer valid.
  33. virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
  34. // setBufferCount updates the number of available buffer slots. If this
  35. // method succeeds, buffer slots will be both unallocated and owned by
  36. // the BufferQueue object (i.e. they are not owned by the producer or
  37. // consumer).
  38. //
  39. // This will fail if the producer has dequeued any buffers, or if
  40. // bufferCount is invalid. bufferCount must generally be a value
  41. // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS
  42. // (inclusive). It may also be set to zero (the default) to indicate
  43. // that the producer does not wish to set a value. The minimum value
  44. // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
  45. // ...).
  46. //
  47. // This may only be called by the producer. The consumer will be told
  48. // to discard buffers through the onBuffersReleased callback.
  49. virtual status_t setBufferCount(int bufferCount);
  50. // dequeueBuffer gets the next buffer slot index for the producer to use.
  51. // If a buffer slot is available then that slot index is written to the
  52. // location pointed to by the buf argument and a status of OK is returned.
  53. // If no slot is available then a status of -EBUSY is returned and buf is
  54. // unmodified.
  55. //
  56. // The outFence parameter will be updated to hold the fence associated with
  57. // the buffer. The contents of the buffer must not be overwritten until the
  58. // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
  59. // written immediately.
  60. //
  61. // The width and height parameters must be no greater than the minimum of
  62. // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
  63. // An error due to invalid dimensions might not be reported until
  64. // updateTexImage() is called. If width and height are both zero, the
  65. // default values specified by setDefaultBufferSize() are used instead.
  66. //
  67. // If the format is 0, the default format will be used.
  68. //
  69. // The usage argument specifies gralloc buffer usage flags. The values
  70. // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
  71. // will be merged with the usage flags specified by setConsumerUsageBits.
  72. //
  73. // The return value may be a negative error value or a non-negative
  74. // collection of flags. If the flags are set, the return values are
  75. // valid, but additional actions must be performed.
  76. //
  77. // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
  78. // producer must discard cached GraphicBuffer references for the slot
  79. // returned in buf.
  80. // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
  81. // must discard cached GraphicBuffer references for all slots.
  82. //
  83. // In both cases, the producer will need to call requestBuffer to get a
  84. // GraphicBuffer handle for the returned slot.
  85. virtual status_t dequeueBuffer(int *outSlot, sp<Fence>* outFence,
  86. bool async, uint32_t width, uint32_t height, PixelFormat format,
  87. uint32_t usage);
  88. // See IGraphicBufferProducer::detachBuffer
  89. virtual status_t detachBuffer(int slot);
  90. // See IGraphicBufferProducer::detachNextBuffer
  91. virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
  92. sp<Fence>* outFence);
  93. // See IGraphicBufferProducer::attachBuffer
  94. virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer);
  95. // queueBuffer returns a filled buffer to the BufferQueue.
  96. //
  97. // Additional data is provided in the QueueBufferInput struct. Notably,
  98. // a timestamp must be provided for the buffer. The timestamp is in
  99. // nanoseconds, and must be monotonically increasing. Its other semantics
  100. // (zero point, etc) are producer-specific and should be documented by the
  101. // producer.
  102. //
  103. // The caller may provide a fence that signals when all rendering
  104. // operations have completed. Alternatively, NO_FENCE may be used,
  105. // indicating that the buffer is ready immediately.
  106. //
  107. // Some values are returned in the output struct: the current settings
  108. // for default width and height, the current transform hint, and the
  109. // number of queued buffers.
  110. virtual status_t queueBuffer(int slot,
  111. const QueueBufferInput& input, QueueBufferOutput* output);
  112. // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
  113. // queue it for use by the consumer.
  114. //
  115. // The buffer will not be overwritten until the fence signals. The fence
  116. // will usually be the one obtained from dequeueBuffer.
  117. virtual void cancelBuffer(int slot, const sp<Fence>& fence);
  118. // Query native window attributes. The "what" values are enumerated in
  119. // window.h (e.g. NATIVE_WINDOW_FORMAT).
  120. virtual int query(int what, int* outValue);
  121. // connect attempts to connect a producer API to the BufferQueue. This
  122. // must be called before any other IGraphicBufferProducer methods are
  123. // called except for getAllocator. A consumer must already be connected.
  124. //
  125. // This method will fail if connect was previously called on the
  126. // BufferQueue and no corresponding disconnect call was made (i.e. if
  127. // it's still connected to a producer).
  128. //
  129. // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
  130. virtual status_t connect(const sp<IProducerListener>& listener,
  131. int api, bool producerControlledByApp, QueueBufferOutput* output);
  132. // disconnect attempts to disconnect a producer API from the BufferQueue.
  133. // Calling this method will cause any subsequent calls to other
  134. // IGraphicBufferProducer methods to fail except for getAllocator and connect.
  135. // Successfully calling connect after this will allow the other methods to
  136. // succeed again.
  137. //
  138. // This method will fail if the the BufferQueue is not currently
  139. // connected to the specified producer API.
  140. virtual status_t disconnect(int api);
  141. // Attaches a sideband buffer stream to the IGraphicBufferProducer.
  142. //
  143. // A sideband stream is a device-specific mechanism for passing buffers
  144. // from the producer to the consumer without using dequeueBuffer/
  145. // queueBuffer. If a sideband stream is present, the consumer can choose
  146. // whether to acquire buffers from the sideband stream or from the queued
  147. // buffers.
  148. //
  149. // Passing NULL or a different stream handle will detach the previous
  150. // handle if any.
  151. virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
  152. // See IGraphicBufferProducer::allocateBuffers
  153. virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
  154. PixelFormat format, uint32_t usage);
  155. // See IGraphicBufferProducer::allowAllocation
  156. virtual status_t allowAllocation(bool allow);
  157. // See IGraphicBufferProducer::setGenerationNumber
  158. virtual status_t setGenerationNumber(uint32_t generationNumber);
  159. // See IGraphicBufferProducer::getConsumerName
  160. virtual String8 getConsumerName() const override;
  161. private:
  162. // This is required by the IBinder::DeathRecipient interface
  163. virtual void binderDied(const wp<IBinder>& who);
  164. // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may
  165. // block if there are no available slots and we are not in non-blocking
  166. // mode (producer and consumer controlled by the application). If it blocks,
  167. // it will release mCore->mMutex while blocked so that other operations on
  168. // the BufferQueue may succeed.
  169. status_t waitForFreeSlotThenRelock(const char* caller, bool async,
  170. int* found, status_t* returnFlags) const;
  171. sp<BufferQueueCore> mCore;
  172. // This references mCore->mSlots. Lock mCore->mMutex while accessing.
  173. BufferQueueDefs::SlotsType& mSlots;
  174. // This is a cached copy of the name stored in the BufferQueueCore.
  175. // It's updated during connect and dequeueBuffer (which should catch
  176. // most updates).
  177. String8 mConsumerName;
  178. uint32_t mStickyTransform;
  179. // This saves the fence from the last queueBuffer, such that the
  180. // next queueBuffer call can throttle buffer production. The prior
  181. // queueBuffer's fence is not nessessarily available elsewhere,
  182. // since the previous buffer might have already been acquired.
  183. sp<Fence> mLastQueueBufferFence;
  184. // Take-a-ticket system for ensuring that onFrame* callbacks are called in
  185. // the order that frames are queued. While the BufferQueue lock
  186. // (mCore->mMutex) is held, a ticket is retained by the producer. After
  187. // dropping the BufferQueue lock, the producer must wait on the condition
  188. // variable until the current callback ticket matches its retained ticket.
  189. Mutex mCallbackMutex;
  190. int mNextCallbackTicket; // Protected by mCore->mMutex
  191. int mCurrentCallbackTicket; // Protected by mCallbackMutex
  192. Condition mCallbackCondition;
  193. }; // class BufferQueueProducer
  194. } // namespace android
  195. #endif