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