BufferQueueCore.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #define LOG_TAG "BufferQueueCore"
  17. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  18. //#define LOG_NDEBUG 0
  19. #define EGL_EGLEXT_PROTOTYPES
  20. #include <inttypes.h>
  21. #include <gui/BufferItem.h>
  22. #include <gui/BufferQueueCore.h>
  23. #include <gui/IConsumerListener.h>
  24. #include <gui/IGraphicBufferAlloc.h>
  25. #include <gui/IProducerListener.h>
  26. #include <gui/ISurfaceComposer.h>
  27. #include <private/gui/ComposerService.h>
  28. template <typename T>
  29. static inline T max(T a, T b) { return a > b ? a : b; }
  30. namespace android {
  31. static String8 getUniqueName() {
  32. static volatile int32_t counter = 0;
  33. return String8::format("unnamed-%d-%d", getpid(),
  34. android_atomic_inc(&counter));
  35. }
  36. BufferQueueCore::BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator) :
  37. mAllocator(allocator),
  38. mMutex(),
  39. mIsAbandoned(false),
  40. mConsumerControlledByApp(false),
  41. mConsumerName(getUniqueName()),
  42. mConsumerListener(),
  43. mConsumerUsageBits(0),
  44. mConnectedApi(NO_CONNECTED_API),
  45. mConnectedProducerListener(),
  46. mSlots(),
  47. mQueue(),
  48. mFreeSlots(),
  49. mFreeBuffers(),
  50. mOverrideMaxBufferCount(0),
  51. mDequeueCondition(),
  52. mUseAsyncBuffer(true),
  53. mDequeueBufferCannotBlock(false),
  54. mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
  55. mDefaultWidth(1),
  56. mDefaultHeight(1),
  57. mDefaultBufferDataSpace(HAL_DATASPACE_UNKNOWN),
  58. mDefaultMaxBufferCount(2),
  59. mMaxAcquiredBufferCount(1),
  60. mBufferHasBeenQueued(false),
  61. mFrameCounter(0),
  62. mTransformHint(0),
  63. mIsAllocating(false),
  64. mIsAllocatingCondition(),
  65. mAllowAllocation(true),
  66. mBufferAge(0),
  67. mGenerationNumber(0)
  68. {
  69. if (allocator == NULL) {
  70. sp<ISurfaceComposer> composer(ComposerService::getComposerService());
  71. mAllocator = composer->createGraphicBufferAlloc();
  72. if (mAllocator == NULL) {
  73. BQ_LOGE("createGraphicBufferAlloc failed");
  74. }
  75. }
  76. for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
  77. mFreeSlots.insert(slot);
  78. }
  79. }
  80. BufferQueueCore::~BufferQueueCore() {}
  81. void BufferQueueCore::dump(String8& result, const char* prefix) const {
  82. Mutex::Autolock lock(mMutex);
  83. String8 fifo;
  84. Fifo::const_iterator current(mQueue.begin());
  85. while (current != mQueue.end()) {
  86. fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
  87. "xform=0x%02x, time=%#" PRIx64 ", scale=%s\n",
  88. current->mSlot, current->mGraphicBuffer.get(),
  89. current->mCrop.left, current->mCrop.top, current->mCrop.right,
  90. current->mCrop.bottom, current->mTransform, current->mTimestamp,
  91. BufferItem::scalingModeName(current->mScalingMode));
  92. ++current;
  93. }
  94. result.appendFormat("%s-BufferQueue mMaxAcquiredBufferCount=%d, "
  95. "mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
  96. "default-format=%d, transform-hint=%02x, FIFO(%zu)={%s}\n",
  97. prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock,
  98. mDefaultWidth, mDefaultHeight, mDefaultBufferFormat, mTransformHint,
  99. mQueue.size(), fifo.string());
  100. // Trim the free buffers so as to not spam the dump
  101. int maxBufferCount = 0;
  102. for (int s = BufferQueueDefs::NUM_BUFFER_SLOTS - 1; s >= 0; --s) {
  103. const BufferSlot& slot(mSlots[s]);
  104. if (slot.mBufferState != BufferSlot::FREE ||
  105. slot.mGraphicBuffer != NULL) {
  106. maxBufferCount = s + 1;
  107. break;
  108. }
  109. }
  110. for (int s = 0; s < maxBufferCount; ++s) {
  111. const BufferSlot& slot(mSlots[s]);
  112. const sp<GraphicBuffer>& buffer(slot.mGraphicBuffer);
  113. result.appendFormat("%s%s[%02d:%p] state=%-8s", prefix,
  114. (slot.mBufferState == BufferSlot::ACQUIRED) ? ">" : " ",
  115. s, buffer.get(),
  116. BufferSlot::bufferStateName(slot.mBufferState));
  117. if (buffer != NULL) {
  118. result.appendFormat(", %p [%4ux%4u:%4u,%3X]", buffer->handle,
  119. buffer->width, buffer->height, buffer->stride,
  120. buffer->format);
  121. }
  122. result.append("\n");
  123. }
  124. }
  125. int BufferQueueCore::getMinUndequeuedBufferCountLocked(bool async) const {
  126. // If dequeueBuffer is allowed to error out, we don't have to add an
  127. // extra buffer.
  128. if (!mUseAsyncBuffer) {
  129. return mMaxAcquiredBufferCount;
  130. }
  131. if (mDequeueBufferCannotBlock || async) {
  132. return mMaxAcquiredBufferCount + 1;
  133. }
  134. return mMaxAcquiredBufferCount;
  135. }
  136. int BufferQueueCore::getMinMaxBufferCountLocked(bool async) const {
  137. return getMinUndequeuedBufferCountLocked(async) + 1;
  138. }
  139. int BufferQueueCore::getMaxBufferCountLocked(bool async) const {
  140. int minMaxBufferCount = getMinMaxBufferCountLocked(async);
  141. int maxBufferCount = max(mDefaultMaxBufferCount, minMaxBufferCount);
  142. if (mOverrideMaxBufferCount != 0) {
  143. assert(mOverrideMaxBufferCount >= minMaxBufferCount);
  144. maxBufferCount = mOverrideMaxBufferCount;
  145. }
  146. // Any buffers that are dequeued by the producer or sitting in the queue
  147. // waiting to be consumed need to have their slots preserved. Such buffers
  148. // will temporarily keep the max buffer count up until the slots no longer
  149. // need to be preserved.
  150. for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
  151. BufferSlot::BufferState state = mSlots[s].mBufferState;
  152. if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
  153. maxBufferCount = s + 1;
  154. }
  155. }
  156. return maxBufferCount;
  157. }
  158. status_t BufferQueueCore::setDefaultMaxBufferCountLocked(int count) {
  159. const int minBufferCount = mUseAsyncBuffer ? 2 : 1;
  160. if (count < minBufferCount || count > BufferQueueDefs::NUM_BUFFER_SLOTS) {
  161. BQ_LOGV("setDefaultMaxBufferCount: invalid count %d, should be in "
  162. "[%d, %d]",
  163. count, minBufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS);
  164. return BAD_VALUE;
  165. }
  166. BQ_LOGV("setDefaultMaxBufferCount: setting count to %d", count);
  167. mDefaultMaxBufferCount = count;
  168. mDequeueCondition.broadcast();
  169. return NO_ERROR;
  170. }
  171. void BufferQueueCore::freeBufferLocked(int slot) {
  172. BQ_LOGV("freeBufferLocked: slot %d", slot);
  173. bool hadBuffer = mSlots[slot].mGraphicBuffer != NULL;
  174. mSlots[slot].mGraphicBuffer.clear();
  175. if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
  176. mSlots[slot].mNeedsCleanupOnRelease = true;
  177. }
  178. if (mSlots[slot].mBufferState != BufferSlot::FREE) {
  179. mFreeSlots.insert(slot);
  180. } else if (hadBuffer) {
  181. // If the slot was FREE, but we had a buffer, we need to move this slot
  182. // from the free buffers list to the the free slots list
  183. mFreeBuffers.remove(slot);
  184. mFreeSlots.insert(slot);
  185. }
  186. mSlots[slot].mBufferState = BufferSlot::FREE;
  187. mSlots[slot].mAcquireCalled = false;
  188. mSlots[slot].mFrameNumber = 0;
  189. // Destroy fence as BufferQueue now takes ownership
  190. if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
  191. eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
  192. mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
  193. }
  194. mSlots[slot].mFence = Fence::NO_FENCE;
  195. validateConsistencyLocked();
  196. }
  197. void BufferQueueCore::freeAllBuffersLocked() {
  198. mBufferHasBeenQueued = false;
  199. for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
  200. freeBufferLocked(s);
  201. }
  202. }
  203. bool BufferQueueCore::stillTracking(const BufferItem* item) const {
  204. const BufferSlot& slot = mSlots[item->mSlot];
  205. BQ_LOGV("stillTracking: item { slot=%d/%" PRIu64 " buffer=%p } "
  206. "slot { slot=%d/%" PRIu64 " buffer=%p }",
  207. item->mSlot, item->mFrameNumber,
  208. (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
  209. item->mSlot, slot.mFrameNumber,
  210. (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
  211. // Compare item with its original buffer slot. We can check the slot as
  212. // the buffer would not be moved to a different slot by the producer.
  213. return (slot.mGraphicBuffer != NULL) &&
  214. (item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
  215. }
  216. void BufferQueueCore::waitWhileAllocatingLocked() const {
  217. ATRACE_CALL();
  218. while (mIsAllocating) {
  219. mIsAllocatingCondition.wait(mMutex);
  220. }
  221. }
  222. void BufferQueueCore::validateConsistencyLocked() const {
  223. static const useconds_t PAUSE_TIME = 0;
  224. for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
  225. bool isInFreeSlots = mFreeSlots.count(slot) != 0;
  226. bool isInFreeBuffers =
  227. std::find(mFreeBuffers.cbegin(), mFreeBuffers.cend(), slot) !=
  228. mFreeBuffers.cend();
  229. if (mSlots[slot].mBufferState == BufferSlot::FREE) {
  230. if (mSlots[slot].mGraphicBuffer == NULL) {
  231. if (!isInFreeSlots) {
  232. BQ_LOGE("Slot %d is FREE but is not in mFreeSlots", slot);
  233. usleep(PAUSE_TIME);
  234. }
  235. if (isInFreeBuffers) {
  236. BQ_LOGE("Slot %d is in mFreeSlots "
  237. "but is also in mFreeBuffers", slot);
  238. usleep(PAUSE_TIME);
  239. }
  240. } else {
  241. if (!isInFreeBuffers) {
  242. BQ_LOGE("Slot %d is FREE but is not in mFreeBuffers", slot);
  243. usleep(PAUSE_TIME);
  244. }
  245. if (isInFreeSlots) {
  246. BQ_LOGE("Slot %d is in mFreeBuffers "
  247. "but is also in mFreeSlots", slot);
  248. usleep(PAUSE_TIME);
  249. }
  250. }
  251. } else {
  252. if (isInFreeSlots) {
  253. BQ_LOGE("Slot %d is in mFreeSlots but is not FREE (%d)",
  254. slot, mSlots[slot].mBufferState);
  255. usleep(PAUSE_TIME);
  256. }
  257. if (isInFreeBuffers) {
  258. BQ_LOGE("Slot %d is in mFreeBuffers but is not FREE (%d)",
  259. slot, mSlots[slot].mBufferState);
  260. usleep(PAUSE_TIME);
  261. }
  262. }
  263. }
  264. }
  265. } // namespace android