blob: 1585aae45c32fa56b1eee4395b25f9e0f6081e39 [file] [log] [blame]
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -07001/*
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 Stozad723bd72014-11-18 10:24:03 -080019//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070020#include <utils/Log.h>
21
Chia-I Wue2786ea2017-08-07 10:36:08 -070022#include <inttypes.h>
23
Jim Shargo28e9d652024-07-10 23:50:00 +000024#include <com_android_graphics_libgui_flags.h>
Dan Stozadd264162015-03-12 13:58:47 -070025#include <gui/BufferItem.h>
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070026#include <gui/BufferItemConsumer.h>
Jim Shargof6a80372025-02-21 21:07:04 +000027#include <gui/Surface.h>
Jim Shargo28e9d652024-07-10 23:50:00 +000028#include <ui/BufferQueueDefs.h>
29#include <ui/GraphicBuffer.h>
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070030
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +000031#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 Talvalae232fdc2012-08-21 13:37:35 -070036
37namespace android {
38
Jim Shargof6a80372025-02-21 21:07:04 +000039std::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
57sp<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 Shargod30823a2024-07-27 02:49:39 +000063#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
64BufferItemConsumer::BufferItemConsumer(uint64_t consumerUsage, int bufferCount,
65 bool controlledByApp, bool isConsumerSurfaceFlinger)
66 : ConsumerBase(controlledByApp, isConsumerSurfaceFlinger) {
67 initialize(consumerUsage, bufferCount);
68}
69
70BufferItemConsumer::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 Stozafe50d2a2014-03-12 14:32:29 -070079BufferItemConsumer::BufferItemConsumer(
Chia-I Wue2786ea2017-08-07 10:36:08 -070080 const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
Dan Stozafe50d2a2014-03-12 14:32:29 -070081 int bufferCount, bool controlledByApp) :
82 ConsumerBase(consumer, controlledByApp)
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070083{
Jim Shargod30823a2024-07-27 02:49:39 +000084 initialize(consumerUsage, bufferCount);
85}
86
87void BufferItemConsumer::initialize(uint64_t consumerUsage, int bufferCount) {
Dan Stozafe50d2a2014-03-12 14:32:29 -070088 status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
Jim Shargod30823a2024-07-27 02:49:39 +000089 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
Dan Stozafe50d2a2014-03-12 14:32:29 -070090 if (bufferCount != DEFAULT_MAX_BUFFERS) {
91 err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
Jim Shargod30823a2024-07-27 02:49:39 +000092 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set max acquired buffer count to %d",
93 bufferCount);
Igor Murashkin4be18a32013-11-18 12:26:56 -080094 }
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070095}
96
Dan Stozad723bd72014-11-18 10:24:03 -080097BufferItemConsumer::~BufferItemConsumer() {}
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070098
Jiwen 'Steve' Cai021d92e2017-03-16 21:07:00 -070099void BufferItemConsumer::setBufferFreedListener(
100 const wp<BufferFreedListener>& listener) {
101 Mutex::Autolock _l(mMutex);
102 mBufferFreedListener = listener;
103}
104
Dan Stoza54716312015-03-13 14:40:34 -0700105status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700106 nsecs_t presentWhen, bool waitForFence) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700107 status_t err;
108
109 if (!item) return BAD_VALUE;
110
111 Mutex::Autolock _l(mMutex);
112
Andy McFadden1585c4d2013-06-28 13:52:40 -0700113 err = acquireBufferLocked(item, presentWhen);
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700114 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 Gennis1df8c342012-12-20 14:05:45 -0800121 if (waitForFence) {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700122 err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700123 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 Ceballos47650f42015-08-04 16:38:17 -0700130 item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer;
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700131
132 return OK;
133}
134
135status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
136 const sp<Fence>& releaseFence) {
Jim Shargo28e9d652024-07-10 23:50:00 +0000137 Mutex::Autolock _l(mMutex);
138 return releaseBufferSlotLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
139}
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700140
Jim Shargo28e9d652024-07-10 23:50:00 +0000141#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
142status_t BufferItemConsumer::releaseBuffer(const sp<GraphicBuffer>& buffer,
143 const sp<Fence>& releaseFence) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700144 Mutex::Autolock _l(mMutex);
145
Jim Shargo28e9d652024-07-10 23:50:00 +0000146 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
159status_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 Reck82607f32018-04-27 17:01:45 -0700164 if (err != OK) {
165 BI_LOGE("Failed to addReleaseFenceLocked");
166 }
Jamie Gennisb2725412012-09-05 20:09:05 -0700167
Jim Shargo52108082024-11-15 16:53:57 +0000168 err = releaseBufferLocked(slotIndex, buffer);
John Reck82607f32018-04-27 17:01:45 -0700169 if (err != OK && err != IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700170 BI_LOGE("Failed to release buffer: %s (%d)",
171 strerror(-err), err);
172 }
173 return err;
174}
175
Jiwen 'Steve' Cai021d92e2017-03-16 21:07:00 -0700176void BufferItemConsumer::freeBufferLocked(int slotIndex) {
177 sp<BufferFreedListener> listener = mBufferFreedListener.promote();
Yi Kong48a619f2018-06-05 16:34:59 -0700178 if (listener != nullptr && mSlots[slotIndex].mGraphicBuffer != nullptr) {
Jiwen 'Steve' Cai021d92e2017-03-16 21:07:00 -0700179 // 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 Talvalae232fdc2012-08-21 13:37:35 -0700186} // namespace android