BufferQueueCore.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_BUFFERQUEUECORE_H
  17. #define ANDROID_GUI_BUFFERQUEUECORE_H
  18. #include <gui/BufferItem.h>
  19. #include <gui/BufferQueueDefs.h>
  20. #include <gui/BufferSlot.h>
  21. #include <utils/Condition.h>
  22. #include <utils/Mutex.h>
  23. #include <utils/NativeHandle.h>
  24. #include <utils/RefBase.h>
  25. #include <utils/String8.h>
  26. #include <utils/StrongPointer.h>
  27. #include <utils/Trace.h>
  28. #include <utils/Vector.h>
  29. #include <list>
  30. #include <set>
  31. #define BQ_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
  32. #define BQ_LOGD(x, ...) ALOGD("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
  33. #define BQ_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
  34. #define BQ_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
  35. #define BQ_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__)
  36. #define ATRACE_BUFFER_INDEX(index) \
  37. if (ATRACE_ENABLED()) { \
  38. char ___traceBuf[1024]; \
  39. snprintf(___traceBuf, 1024, "%s: %d", \
  40. mCore->mConsumerName.string(), (index)); \
  41. android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
  42. }
  43. namespace android {
  44. class IConsumerListener;
  45. class IGraphicBufferAlloc;
  46. class IProducerListener;
  47. class BufferQueueCore : public virtual RefBase {
  48. friend class BufferQueueProducer;
  49. friend class BufferQueueConsumer;
  50. public:
  51. // Used as a placeholder slot number when the value isn't pointing to an
  52. // existing buffer.
  53. enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT };
  54. // We reserve two slots in order to guarantee that the producer and
  55. // consumer can run asynchronously.
  56. enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 };
  57. // The default API number used to indicate that no producer is connected
  58. enum { NO_CONNECTED_API = 0 };
  59. typedef Vector<BufferItem> Fifo;
  60. // BufferQueueCore manages a pool of gralloc memory slots to be used by
  61. // producers and consumers. allocator is used to allocate all the needed
  62. // gralloc buffers.
  63. BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL);
  64. virtual ~BufferQueueCore();
  65. private:
  66. // Dump our state in a string
  67. void dump(String8& result, const char* prefix) const;
  68. // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
  69. // that must remain in a state other than DEQUEUED. The async parameter
  70. // tells whether we're in asynchronous mode.
  71. int getMinUndequeuedBufferCountLocked(bool async) const;
  72. // getMinMaxBufferCountLocked returns the minimum number of buffers allowed
  73. // given the current BufferQueue state. The async parameter tells whether
  74. // we're in asynchonous mode.
  75. int getMinMaxBufferCountLocked(bool async) const;
  76. // getMaxBufferCountLocked returns the maximum number of buffers that can be
  77. // allocated at once. This value depends on the following member variables:
  78. //
  79. // mDequeueBufferCannotBlock
  80. // mMaxAcquiredBufferCount
  81. // mDefaultMaxBufferCount
  82. // mOverrideMaxBufferCount
  83. // async parameter
  84. //
  85. // Any time one of these member variables is changed while a producer is
  86. // connected, mDequeueCondition must be broadcast.
  87. int getMaxBufferCountLocked(bool async) const;
  88. // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
  89. // that will be used if the producer does not override the buffer slot
  90. // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. The
  91. // initial default is 2.
  92. status_t setDefaultMaxBufferCountLocked(int count);
  93. // freeBufferLocked frees the GraphicBuffer and sync resources for the
  94. // given slot.
  95. void freeBufferLocked(int slot);
  96. // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
  97. // all slots.
  98. void freeAllBuffersLocked();
  99. // stillTracking returns true iff the buffer item is still being tracked
  100. // in one of the slots.
  101. bool stillTracking(const BufferItem* item) const;
  102. // waitWhileAllocatingLocked blocks until mIsAllocating is false.
  103. void waitWhileAllocatingLocked() const;
  104. // validateConsistencyLocked ensures that the free lists are in sync with
  105. // the information stored in mSlots
  106. void validateConsistencyLocked() const;
  107. // mAllocator is the connection to SurfaceFlinger that is used to allocate
  108. // new GraphicBuffer objects.
  109. sp<IGraphicBufferAlloc> mAllocator;
  110. // mMutex is the mutex used to prevent concurrent access to the member
  111. // variables of BufferQueueCore objects. It must be locked whenever any
  112. // member variable is accessed.
  113. mutable Mutex mMutex;
  114. // mIsAbandoned indicates that the BufferQueue will no longer be used to
  115. // consume image buffers pushed to it using the IGraphicBufferProducer
  116. // interface. It is initialized to false, and set to true in the
  117. // consumerDisconnect method. A BufferQueue that is abandoned will return
  118. // the NO_INIT error from all IGraphicBufferProducer methods capable of
  119. // returning an error.
  120. bool mIsAbandoned;
  121. // mConsumerControlledByApp indicates whether the connected consumer is
  122. // controlled by the application.
  123. bool mConsumerControlledByApp;
  124. // mConsumerName is a string used to identify the BufferQueue in log
  125. // messages. It is set by the IGraphicBufferConsumer::setConsumerName
  126. // method.
  127. String8 mConsumerName;
  128. // mConsumerListener is used to notify the connected consumer of
  129. // asynchronous events that it may wish to react to. It is initially
  130. // set to NULL and is written by consumerConnect and consumerDisconnect.
  131. sp<IConsumerListener> mConsumerListener;
  132. // mConsumerUsageBits contains flags that the consumer wants for
  133. // GraphicBuffers.
  134. uint32_t mConsumerUsageBits;
  135. // mConnectedApi indicates the producer API that is currently connected
  136. // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
  137. // by the connect and disconnect methods.
  138. int mConnectedApi;
  139. // mConnectedProducerToken is used to set a binder death notification on
  140. // the producer.
  141. sp<IProducerListener> mConnectedProducerListener;
  142. // mSlots is an array of buffer slots that must be mirrored on the producer
  143. // side. This allows buffer ownership to be transferred between the producer
  144. // and consumer without sending a GraphicBuffer over Binder. The entire
  145. // array is initialized to NULL at construction time, and buffers are
  146. // allocated for a slot when requestBuffer is called with that slot's index.
  147. BufferQueueDefs::SlotsType mSlots;
  148. // mQueue is a FIFO of queued buffers used in synchronous mode.
  149. Fifo mQueue;
  150. // mFreeSlots contains all of the slots which are FREE and do not currently
  151. // have a buffer attached
  152. std::set<int> mFreeSlots;
  153. // mFreeBuffers contains all of the slots which are FREE and currently have
  154. // a buffer attached
  155. std::list<int> mFreeBuffers;
  156. // mOverrideMaxBufferCount is the limit on the number of buffers that will
  157. // be allocated at one time. This value is set by the producer by calling
  158. // setBufferCount. The default is 0, which means that the producer doesn't
  159. // care about the number of buffers in the pool. In that case,
  160. // mDefaultMaxBufferCount is used as the limit.
  161. int mOverrideMaxBufferCount;
  162. // mDequeueCondition is a condition variable used for dequeueBuffer in
  163. // synchronous mode.
  164. mutable Condition mDequeueCondition;
  165. // mUseAsyncBuffer indicates whether an extra buffer is used in async mode
  166. // to prevent dequeueBuffer from blocking.
  167. bool mUseAsyncBuffer;
  168. // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
  169. // block. This flag is set during connect when both the producer and
  170. // consumer are controlled by the application.
  171. bool mDequeueBufferCannotBlock;
  172. // mDefaultBufferFormat can be set so it will override the buffer format
  173. // when it isn't specified in dequeueBuffer.
  174. PixelFormat mDefaultBufferFormat;
  175. // mDefaultWidth holds the default width of allocated buffers. It is used
  176. // in dequeueBuffer if a width and height of 0 are specified.
  177. uint32_t mDefaultWidth;
  178. // mDefaultHeight holds the default height of allocated buffers. It is used
  179. // in dequeueBuffer if a width and height of 0 are specified.
  180. uint32_t mDefaultHeight;
  181. // mDefaultBufferDataSpace holds the default dataSpace of queued buffers.
  182. // It is used in queueBuffer if a dataspace of 0 (HAL_DATASPACE_UNKNOWN)
  183. // is specified.
  184. android_dataspace mDefaultBufferDataSpace;
  185. // mDefaultMaxBufferCount is the default limit on the number of buffers that
  186. // will be allocated at one time. This default limit is set by the consumer.
  187. // The limit (as opposed to the default limit) may be overriden by the
  188. // producer.
  189. int mDefaultMaxBufferCount;
  190. // mMaxAcquiredBufferCount is the number of buffers that the consumer may
  191. // acquire at one time. It defaults to 1, and can be changed by the consumer
  192. // via setMaxAcquiredBufferCount, but this may only be done while no
  193. // producer is connected to the BufferQueue. This value is used to derive
  194. // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
  195. int mMaxAcquiredBufferCount;
  196. // mBufferHasBeenQueued is true once a buffer has been queued. It is reset
  197. // when something causes all buffers to be freed (e.g., changing the buffer
  198. // count).
  199. bool mBufferHasBeenQueued;
  200. // mFrameCounter is the free running counter, incremented on every
  201. // successful queueBuffer call and buffer allocation.
  202. uint64_t mFrameCounter;
  203. // mTransformHint is used to optimize for screen rotations.
  204. uint32_t mTransformHint;
  205. // mSidebandStream is a handle to the sideband buffer stream, if any
  206. sp<NativeHandle> mSidebandStream;
  207. // mIsAllocating indicates whether a producer is currently trying to allocate buffers (which
  208. // releases mMutex while doing the allocation proper). Producers should not modify any of the
  209. // FREE slots while this is true. mIsAllocatingCondition is signaled when this value changes to
  210. // false.
  211. bool mIsAllocating;
  212. // mIsAllocatingCondition is a condition variable used by producers to wait until mIsAllocating
  213. // becomes false.
  214. mutable Condition mIsAllocatingCondition;
  215. // mAllowAllocation determines whether dequeueBuffer is allowed to allocate
  216. // new buffers
  217. bool mAllowAllocation;
  218. // mBufferAge tracks the age of the contents of the most recently dequeued
  219. // buffer as the number of frames that have elapsed since it was last queued
  220. uint64_t mBufferAge;
  221. // mGenerationNumber stores the current generation number of the attached
  222. // producer. Any attempt to attach a buffer with a different generation
  223. // number will fail.
  224. uint32_t mGenerationNumber;
  225. }; // class BufferQueueCore
  226. } // namespace android
  227. #endif