BufferItemConsumer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "BufferItemConsumer"
  18. //#define ATRACE_TAG ATRACE_TAG_GRAPHICS
  19. #include <utils/Log.h>
  20. #include <inttypes.h>
  21. #include <gui/BufferItem.h>
  22. #include <gui/BufferItemConsumer.h>
  23. #define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
  24. //#define BI_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
  25. //#define BI_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
  26. //#define BI_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
  27. #define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
  28. namespace android {
  29. BufferItemConsumer::BufferItemConsumer(
  30. const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
  31. int bufferCount, bool controlledByApp) :
  32. ConsumerBase(consumer, controlledByApp)
  33. {
  34. status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
  35. LOG_ALWAYS_FATAL_IF(err != OK,
  36. "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
  37. if (bufferCount != DEFAULT_MAX_BUFFERS) {
  38. err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
  39. LOG_ALWAYS_FATAL_IF(err != OK,
  40. "Failed to set max acquired buffer count to %d", bufferCount);
  41. }
  42. }
  43. BufferItemConsumer::~BufferItemConsumer() {}
  44. void BufferItemConsumer::setBufferFreedListener(
  45. const wp<BufferFreedListener>& listener) {
  46. Mutex::Autolock _l(mMutex);
  47. mBufferFreedListener = listener;
  48. }
  49. status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
  50. nsecs_t presentWhen, bool waitForFence) {
  51. status_t err;
  52. if (!item) return BAD_VALUE;
  53. Mutex::Autolock _l(mMutex);
  54. err = acquireBufferLocked(item, presentWhen);
  55. if (err != OK) {
  56. if (err != NO_BUFFER_AVAILABLE) {
  57. BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
  58. }
  59. return err;
  60. }
  61. if (waitForFence) {
  62. err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
  63. if (err != OK) {
  64. BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
  65. strerror(-err), err);
  66. return err;
  67. }
  68. }
  69. item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer;
  70. return OK;
  71. }
  72. status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
  73. const sp<Fence>& releaseFence) {
  74. status_t err;
  75. Mutex::Autolock _l(mMutex);
  76. err = addReleaseFenceLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
  77. if (err != OK) {
  78. BI_LOGE("Failed to addReleaseFenceLocked");
  79. }
  80. err = releaseBufferLocked(item.mSlot, item.mGraphicBuffer, EGL_NO_DISPLAY,
  81. EGL_NO_SYNC_KHR);
  82. if (err != OK && err != IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
  83. BI_LOGE("Failed to release buffer: %s (%d)",
  84. strerror(-err), err);
  85. }
  86. return err;
  87. }
  88. void BufferItemConsumer::freeBufferLocked(int slotIndex) {
  89. sp<BufferFreedListener> listener = mBufferFreedListener.promote();
  90. if (listener != nullptr && mSlots[slotIndex].mGraphicBuffer != nullptr) {
  91. // Fire callback if we have a listener registered and the buffer being freed is valid.
  92. BI_LOGV("actually calling onBufferFreed");
  93. listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer);
  94. }
  95. ConsumerBase::freeBufferLocked(slotIndex);
  96. }
  97. } // namespace android