BufferItemConsumer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <gui/BufferItem.h>
  21. #include <gui/BufferItemConsumer.h>
  22. //#define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
  23. //#define BI_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
  24. //#define BI_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
  25. //#define BI_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
  26. #define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
  27. namespace android {
  28. BufferItemConsumer::BufferItemConsumer(
  29. const sp<IGraphicBufferConsumer>& consumer, uint32_t consumerUsage,
  30. int bufferCount, bool controlledByApp) :
  31. ConsumerBase(consumer, controlledByApp)
  32. {
  33. status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
  34. LOG_ALWAYS_FATAL_IF(err != OK,
  35. "Failed to set consumer usage bits to %#x", consumerUsage);
  36. if (bufferCount != DEFAULT_MAX_BUFFERS) {
  37. err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
  38. LOG_ALWAYS_FATAL_IF(err != OK,
  39. "Failed to set max acquired buffer count to %d", bufferCount);
  40. }
  41. }
  42. BufferItemConsumer::~BufferItemConsumer() {}
  43. void BufferItemConsumer::setName(const String8& name) {
  44. Mutex::Autolock _l(mMutex);
  45. mName = name;
  46. mConsumer->setConsumerName(name);
  47. }
  48. status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
  49. nsecs_t presentWhen, bool waitForFence) {
  50. status_t err;
  51. if (!item) return BAD_VALUE;
  52. Mutex::Autolock _l(mMutex);
  53. err = acquireBufferLocked(item, presentWhen);
  54. if (err != OK) {
  55. if (err != NO_BUFFER_AVAILABLE) {
  56. BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
  57. }
  58. return err;
  59. }
  60. if (waitForFence) {
  61. err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
  62. if (err != OK) {
  63. BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
  64. strerror(-err), err);
  65. return err;
  66. }
  67. }
  68. item->mGraphicBuffer = mSlots[item->mBuf].mGraphicBuffer;
  69. return OK;
  70. }
  71. status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
  72. const sp<Fence>& releaseFence) {
  73. status_t err;
  74. Mutex::Autolock _l(mMutex);
  75. err = addReleaseFenceLocked(item.mBuf, item.mGraphicBuffer, releaseFence);
  76. err = releaseBufferLocked(item.mBuf, item.mGraphicBuffer, EGL_NO_DISPLAY,
  77. EGL_NO_SYNC_KHR);
  78. if (err != OK) {
  79. BI_LOGE("Failed to release buffer: %s (%d)",
  80. strerror(-err), err);
  81. }
  82. return err;
  83. }
  84. } // namespace android