Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "BufferItemConsumer" |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 19 | //#define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 20 | #include <utils/Log.h> |
| 21 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 24 | #include <com_android_graphics_libgui_flags.h> |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame] | 25 | #include <gui/BufferItem.h> |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 26 | #include <gui/BufferItemConsumer.h> |
Jim Shargo | f6a8037 | 2025-02-21 21:07:04 +0000 | [diff] [blame] | 27 | #include <gui/Surface.h> |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 28 | #include <ui/BufferQueueDefs.h> |
| 29 | #include <ui/GraphicBuffer.h> |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 30 | |
Tomasz Wasilczyk | b19fe0c | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 31 | #define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 32 | // #define BI_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 33 | // #define BI_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 34 | // #define BI_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 35 | #define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
Jim Shargo | f6a8037 | 2025-02-21 21:07:04 +0000 | [diff] [blame] | 39 | std::tuple<sp<BufferItemConsumer>, sp<Surface>> BufferItemConsumer::create( |
| 40 | uint64_t consumerUsage, int bufferCount, bool controlledByApp, |
| 41 | bool isConsumerSurfaceFlinger) { |
| 42 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 43 | sp<BufferItemConsumer> bufferItemConsumer = |
| 44 | sp<BufferItemConsumer>::make(consumerUsage, bufferCount, controlledByApp, |
| 45 | isConsumerSurfaceFlinger); |
| 46 | return {bufferItemConsumer, bufferItemConsumer->getSurface()}; |
| 47 | #else |
| 48 | sp<IGraphicBufferProducer> igbp; |
| 49 | sp<IGraphicBufferConsumer> igbc; |
| 50 | BufferQueue::createBufferQueue(&igbp, &igbc, isConsumerSurfaceFlinger); |
| 51 | sp<BufferItemConsumer> bufferItemConsumer = |
| 52 | sp<BufferItemConsumer>::make(igbc, consumerUsage, bufferCount, controlledByApp); |
| 53 | return {bufferItemConsumer, sp<Surface>::make(igbp, controlledByApp)}; |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | sp<BufferItemConsumer> BufferItemConsumer::create(const sp<IGraphicBufferConsumer>& consumer, |
| 58 | uint64_t consumerUsage, int bufferCount, |
| 59 | bool controlledByApp) { |
| 60 | return sp<BufferItemConsumer>::make(consumer, consumerUsage, bufferCount, controlledByApp); |
| 61 | } |
| 62 | |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 63 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 64 | BufferItemConsumer::BufferItemConsumer(uint64_t consumerUsage, int bufferCount, |
| 65 | bool controlledByApp, bool isConsumerSurfaceFlinger) |
| 66 | : ConsumerBase(controlledByApp, isConsumerSurfaceFlinger) { |
| 67 | initialize(consumerUsage, bufferCount); |
| 68 | } |
| 69 | |
| 70 | BufferItemConsumer::BufferItemConsumer(const sp<IGraphicBufferProducer>& producer, |
| 71 | const sp<IGraphicBufferConsumer>& consumer, |
| 72 | uint64_t consumerUsage, int bufferCount, |
| 73 | bool controlledByApp) |
| 74 | : ConsumerBase(producer, consumer, controlledByApp) { |
| 75 | initialize(consumerUsage, bufferCount); |
| 76 | } |
| 77 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 78 | |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 79 | BufferItemConsumer::BufferItemConsumer( |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 80 | const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 81 | int bufferCount, bool controlledByApp) : |
| 82 | ConsumerBase(consumer, controlledByApp) |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 83 | { |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 84 | initialize(consumerUsage, bufferCount); |
| 85 | } |
| 86 | |
| 87 | void BufferItemConsumer::initialize(uint64_t consumerUsage, int bufferCount) { |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 88 | status_t err = mConsumer->setConsumerUsageBits(consumerUsage); |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set consumer usage bits to %#" PRIx64, consumerUsage); |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 90 | if (bufferCount != DEFAULT_MAX_BUFFERS) { |
| 91 | err = mConsumer->setMaxAcquiredBufferCount(bufferCount); |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 92 | LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set max acquired buffer count to %d", |
| 93 | bufferCount); |
Igor Murashkin | 4be18a3 | 2013-11-18 12:26:56 -0800 | [diff] [blame] | 94 | } |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 97 | BufferItemConsumer::~BufferItemConsumer() {} |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 98 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 99 | void BufferItemConsumer::setBufferFreedListener( |
| 100 | const wp<BufferFreedListener>& listener) { |
| 101 | Mutex::Autolock _l(mMutex); |
| 102 | mBufferFreedListener = listener; |
| 103 | } |
| 104 | |
Dan Stoza | 5471631 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 105 | status_t BufferItemConsumer::acquireBuffer(BufferItem *item, |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 106 | nsecs_t presentWhen, bool waitForFence) { |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 107 | status_t err; |
| 108 | |
| 109 | if (!item) return BAD_VALUE; |
| 110 | |
| 111 | Mutex::Autolock _l(mMutex); |
| 112 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 113 | err = acquireBufferLocked(item, presentWhen); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 114 | if (err != OK) { |
| 115 | if (err != NO_BUFFER_AVAILABLE) { |
| 116 | BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
| 117 | } |
| 118 | return err; |
| 119 | } |
| 120 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 121 | if (waitForFence) { |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 122 | err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer"); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 123 | if (err != OK) { |
| 124 | BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
| 125 | strerror(-err), err); |
| 126 | return err; |
| 127 | } |
| 128 | } |
| 129 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 130 | item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer; |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 131 | |
| 132 | return OK; |
| 133 | } |
| 134 | |
| 135 | status_t BufferItemConsumer::releaseBuffer(const BufferItem &item, |
| 136 | const sp<Fence>& releaseFence) { |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 137 | Mutex::Autolock _l(mMutex); |
| 138 | return releaseBufferSlotLocked(item.mSlot, item.mGraphicBuffer, releaseFence); |
| 139 | } |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 140 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 141 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS) |
| 142 | status_t BufferItemConsumer::releaseBuffer(const sp<GraphicBuffer>& buffer, |
| 143 | const sp<Fence>& releaseFence) { |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 144 | Mutex::Autolock _l(mMutex); |
| 145 | |
Jim Shargo | 28e9d65 | 2024-07-10 23:50:00 +0000 | [diff] [blame] | 146 | if (buffer == nullptr) { |
| 147 | return BAD_VALUE; |
| 148 | } |
| 149 | |
| 150 | int slotIndex = getSlotForBufferLocked(buffer); |
| 151 | if (slotIndex == INVALID_BUFFER_SLOT) { |
| 152 | return BAD_VALUE; |
| 153 | } |
| 154 | |
| 155 | return releaseBufferSlotLocked(slotIndex, buffer, releaseFence); |
| 156 | } |
| 157 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS) |
| 158 | |
| 159 | status_t BufferItemConsumer::releaseBufferSlotLocked(int slotIndex, const sp<GraphicBuffer>& buffer, |
| 160 | const sp<Fence>& releaseFence) { |
| 161 | status_t err; |
| 162 | |
| 163 | err = addReleaseFenceLocked(slotIndex, buffer, releaseFence); |
John Reck | 82607f3 | 2018-04-27 17:01:45 -0700 | [diff] [blame] | 164 | if (err != OK) { |
| 165 | BI_LOGE("Failed to addReleaseFenceLocked"); |
| 166 | } |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 167 | |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 168 | err = releaseBufferLocked(slotIndex, buffer); |
John Reck | 82607f3 | 2018-04-27 17:01:45 -0700 | [diff] [blame] | 169 | if (err != OK && err != IGraphicBufferConsumer::STALE_BUFFER_SLOT) { |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 170 | BI_LOGE("Failed to release buffer: %s (%d)", |
| 171 | strerror(-err), err); |
| 172 | } |
| 173 | return err; |
| 174 | } |
| 175 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 176 | void BufferItemConsumer::freeBufferLocked(int slotIndex) { |
| 177 | sp<BufferFreedListener> listener = mBufferFreedListener.promote(); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 178 | if (listener != nullptr && mSlots[slotIndex].mGraphicBuffer != nullptr) { |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 179 | // Fire callback if we have a listener registered and the buffer being freed is valid. |
| 180 | BI_LOGV("actually calling onBufferFreed"); |
| 181 | listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer); |
| 182 | } |
| 183 | ConsumerBase::freeBufferLocked(slotIndex); |
| 184 | } |
| 185 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 186 | } // namespace android |