CpuConsumer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2012 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_NDEBUG 0
  17. #define LOG_TAG "CpuConsumer"
  18. //#define ATRACE_TAG ATRACE_TAG_GRAPHICS
  19. #include <cutils/compiler.h>
  20. #include <utils/Log.h>
  21. #include <gui/BufferItem.h>
  22. #include <gui/CpuConsumer.h>
  23. #define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
  24. //#define CC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
  25. //#define CC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
  26. #define CC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
  27. #define CC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
  28. namespace android {
  29. CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
  30. size_t maxLockedBuffers, bool controlledByApp) :
  31. ConsumerBase(bq, controlledByApp),
  32. mMaxLockedBuffers(maxLockedBuffers),
  33. mCurrentLockedBuffers(0)
  34. {
  35. // Create tracking entries for locked buffers
  36. mAcquiredBuffers.insertAt(0, maxLockedBuffers);
  37. mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
  38. mConsumer->setMaxAcquiredBufferCount(static_cast<int32_t>(maxLockedBuffers));
  39. }
  40. CpuConsumer::~CpuConsumer() {
  41. // ConsumerBase destructor does all the work.
  42. }
  43. void CpuConsumer::setName(const String8& name) {
  44. Mutex::Autolock _l(mMutex);
  45. mName = name;
  46. mConsumer->setConsumerName(name);
  47. }
  48. static bool isPossiblyYUV(PixelFormat format) {
  49. switch (static_cast<int>(format)) {
  50. case HAL_PIXEL_FORMAT_RGBA_8888:
  51. case HAL_PIXEL_FORMAT_RGBX_8888:
  52. case HAL_PIXEL_FORMAT_RGB_888:
  53. case HAL_PIXEL_FORMAT_RGB_565:
  54. case HAL_PIXEL_FORMAT_BGRA_8888:
  55. case HAL_PIXEL_FORMAT_Y8:
  56. case HAL_PIXEL_FORMAT_Y16:
  57. case HAL_PIXEL_FORMAT_RAW16:
  58. case HAL_PIXEL_FORMAT_RAW10:
  59. case HAL_PIXEL_FORMAT_RAW_OPAQUE:
  60. case HAL_PIXEL_FORMAT_BLOB:
  61. case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
  62. return false;
  63. case HAL_PIXEL_FORMAT_YV12:
  64. case HAL_PIXEL_FORMAT_YCbCr_420_888:
  65. case HAL_PIXEL_FORMAT_YCbCr_422_SP:
  66. case HAL_PIXEL_FORMAT_YCrCb_420_SP:
  67. case HAL_PIXEL_FORMAT_YCbCr_422_I:
  68. default:
  69. return true;
  70. }
  71. }
  72. status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
  73. status_t err;
  74. if (!nativeBuffer) return BAD_VALUE;
  75. if (mCurrentLockedBuffers == mMaxLockedBuffers) {
  76. CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
  77. mMaxLockedBuffers);
  78. return NOT_ENOUGH_DATA;
  79. }
  80. BufferItem b;
  81. Mutex::Autolock _l(mMutex);
  82. err = acquireBufferLocked(&b, 0);
  83. if (err != OK) {
  84. if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
  85. return BAD_VALUE;
  86. } else {
  87. CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
  88. return err;
  89. }
  90. }
  91. int buf = b.mBuf;
  92. void *bufferPointer = NULL;
  93. android_ycbcr ycbcr = android_ycbcr();
  94. PixelFormat format = mSlots[buf].mGraphicBuffer->getPixelFormat();
  95. PixelFormat flexFormat = format;
  96. if (isPossiblyYUV(format)) {
  97. if (b.mFence.get()) {
  98. err = mSlots[buf].mGraphicBuffer->lockAsyncYCbCr(
  99. GraphicBuffer::USAGE_SW_READ_OFTEN,
  100. b.mCrop,
  101. &ycbcr,
  102. b.mFence->dup());
  103. } else {
  104. err = mSlots[buf].mGraphicBuffer->lockYCbCr(
  105. GraphicBuffer::USAGE_SW_READ_OFTEN,
  106. b.mCrop,
  107. &ycbcr);
  108. }
  109. if (err == OK) {
  110. bufferPointer = ycbcr.y;
  111. flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
  112. if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
  113. CC_LOGV("locking buffer of format %#x as flex YUV", format);
  114. }
  115. } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
  116. CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
  117. strerror(-err), err);
  118. return err;
  119. }
  120. }
  121. if (bufferPointer == NULL) { // not flexible YUV
  122. if (b.mFence.get()) {
  123. err = mSlots[buf].mGraphicBuffer->lockAsync(
  124. GraphicBuffer::USAGE_SW_READ_OFTEN,
  125. b.mCrop,
  126. &bufferPointer,
  127. b.mFence->dup());
  128. } else {
  129. err = mSlots[buf].mGraphicBuffer->lock(
  130. GraphicBuffer::USAGE_SW_READ_OFTEN,
  131. b.mCrop,
  132. &bufferPointer);
  133. }
  134. if (err != OK) {
  135. CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
  136. strerror(-err), err);
  137. return err;
  138. }
  139. }
  140. size_t lockedIdx = 0;
  141. for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
  142. if (mAcquiredBuffers[lockedIdx].mSlot ==
  143. BufferQueue::INVALID_BUFFER_SLOT) {
  144. break;
  145. }
  146. }
  147. assert(lockedIdx < mMaxLockedBuffers);
  148. AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
  149. ab.mSlot = buf;
  150. ab.mBufferPointer = bufferPointer;
  151. ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
  152. nativeBuffer->data =
  153. reinterpret_cast<uint8_t*>(bufferPointer);
  154. nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth();
  155. nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight();
  156. nativeBuffer->format = format;
  157. nativeBuffer->flexFormat = flexFormat;
  158. nativeBuffer->stride = (ycbcr.y != NULL) ?
  159. static_cast<uint32_t>(ycbcr.ystride) :
  160. mSlots[buf].mGraphicBuffer->getStride();
  161. nativeBuffer->crop = b.mCrop;
  162. nativeBuffer->transform = b.mTransform;
  163. nativeBuffer->scalingMode = b.mScalingMode;
  164. nativeBuffer->timestamp = b.mTimestamp;
  165. nativeBuffer->dataSpace = b.mDataSpace;
  166. nativeBuffer->frameNumber = b.mFrameNumber;
  167. nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
  168. nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
  169. nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
  170. nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
  171. mCurrentLockedBuffers++;
  172. return OK;
  173. }
  174. status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
  175. Mutex::Autolock _l(mMutex);
  176. size_t lockedIdx = 0;
  177. void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
  178. for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
  179. if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
  180. }
  181. if (lockedIdx == mMaxLockedBuffers) {
  182. CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
  183. return BAD_VALUE;
  184. }
  185. return releaseAcquiredBufferLocked(lockedIdx);
  186. }
  187. status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
  188. status_t err;
  189. int fd = -1;
  190. err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
  191. if (err != OK) {
  192. CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
  193. lockedIdx);
  194. return err;
  195. }
  196. int buf = mAcquiredBuffers[lockedIdx].mSlot;
  197. if (CC_LIKELY(fd != -1)) {
  198. sp<Fence> fence(new Fence(fd));
  199. addReleaseFenceLocked(
  200. mAcquiredBuffers[lockedIdx].mSlot,
  201. mSlots[buf].mGraphicBuffer,
  202. fence);
  203. }
  204. // release the buffer if it hasn't already been freed by the BufferQueue.
  205. // This can happen, for example, when the producer of this buffer
  206. // disconnected after this buffer was acquired.
  207. if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
  208. mSlots[buf].mGraphicBuffer)) {
  209. releaseBufferLocked(
  210. buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
  211. EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
  212. }
  213. AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
  214. ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
  215. ab.mBufferPointer = NULL;
  216. ab.mGraphicBuffer.clear();
  217. mCurrentLockedBuffers--;
  218. return OK;
  219. }
  220. void CpuConsumer::freeBufferLocked(int slotIndex) {
  221. ConsumerBase::freeBufferLocked(slotIndex);
  222. }
  223. } // namespace android