blob: 4926ceb619f62c99989aa3d2395127beabbb2950 [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
Jim Shargof3b00122025-03-03 22:10:37 +000020#include <utils/Errors.h>
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070021#include <utils/Log.h>
22
Chia-I Wue2786ea2017-08-07 10:36:08 -070023#include <inttypes.h>
24
Jim Shargo28e9d652024-07-10 23:50:00 +000025#include <com_android_graphics_libgui_flags.h>
Dan Stozadd264162015-03-12 13:58:47 -070026#include <gui/BufferItem.h>
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070027#include <gui/BufferItemConsumer.h>
Jim Shargof6a80372025-02-21 21:07:04 +000028#include <gui/Surface.h>
Jim Shargo28e9d652024-07-10 23:50:00 +000029#include <ui/BufferQueueDefs.h>
30#include <ui/GraphicBuffer.h>
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070031
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +000032#define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
33// #define BI_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__)
34// #define BI_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__)
35// #define BI_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__)
36#define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__)
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070037
38namespace android {
39
Jim Shargof6a80372025-02-21 21:07:04 +000040std::tuple<sp<BufferItemConsumer>, sp<Surface>> BufferItemConsumer::create(
41 uint64_t consumerUsage, int bufferCount, bool controlledByApp,
42 bool isConsumerSurfaceFlinger) {
43#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
44 sp<BufferItemConsumer> bufferItemConsumer =
45 sp<BufferItemConsumer>::make(consumerUsage, bufferCount, controlledByApp,
46 isConsumerSurfaceFlinger);
47 return {bufferItemConsumer, bufferItemConsumer->getSurface()};
48#else
49 sp<IGraphicBufferProducer> igbp;
50 sp<IGraphicBufferConsumer> igbc;
51 BufferQueue::createBufferQueue(&igbp, &igbc, isConsumerSurfaceFlinger);
52 sp<BufferItemConsumer> bufferItemConsumer =
53 sp<BufferItemConsumer>::make(igbc, consumerUsage, bufferCount, controlledByApp);
54 return {bufferItemConsumer, sp<Surface>::make(igbp, controlledByApp)};
55#endif
56}
57
58sp<BufferItemConsumer> BufferItemConsumer::create(const sp<IGraphicBufferConsumer>& consumer,
59 uint64_t consumerUsage, int bufferCount,
60 bool controlledByApp) {
61 return sp<BufferItemConsumer>::make(consumer, consumerUsage, bufferCount, controlledByApp);
62}
63
Jim Shargod30823a2024-07-27 02:49:39 +000064#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
65BufferItemConsumer::BufferItemConsumer(uint64_t consumerUsage, int bufferCount,
66 bool controlledByApp, bool isConsumerSurfaceFlinger)
67 : ConsumerBase(controlledByApp, isConsumerSurfaceFlinger) {
68 initialize(consumerUsage, bufferCount);
69}
70
71BufferItemConsumer::BufferItemConsumer(const sp<IGraphicBufferProducer>& producer,
72 const sp<IGraphicBufferConsumer>& consumer,
73 uint64_t consumerUsage, int bufferCount,
74 bool controlledByApp)
75 : ConsumerBase(producer, consumer, controlledByApp) {
76 initialize(consumerUsage, bufferCount);
77}
78#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
79
Dan Stozafe50d2a2014-03-12 14:32:29 -070080BufferItemConsumer::BufferItemConsumer(
Chia-I Wue2786ea2017-08-07 10:36:08 -070081 const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
Dan Stozafe50d2a2014-03-12 14:32:29 -070082 int bufferCount, bool controlledByApp) :
83 ConsumerBase(consumer, controlledByApp)
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070084{
Jim Shargod30823a2024-07-27 02:49:39 +000085 initialize(consumerUsage, bufferCount);
86}
87
88void BufferItemConsumer::initialize(uint64_t consumerUsage, int bufferCount) {
Dan Stozafe50d2a2014-03-12 14:32:29 -070089 status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
Jim Shargod30823a2024-07-27 02:49:39 +000090 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
Dan Stozafe50d2a2014-03-12 14:32:29 -070091 if (bufferCount != DEFAULT_MAX_BUFFERS) {
92 err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
Jim Shargod30823a2024-07-27 02:49:39 +000093 LOG_ALWAYS_FATAL_IF(err != OK, "Failed to set max acquired buffer count to %d",
94 bufferCount);
Igor Murashkin4be18a32013-11-18 12:26:56 -080095 }
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070096}
97
Dan Stozad723bd72014-11-18 10:24:03 -080098BufferItemConsumer::~BufferItemConsumer() {}
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070099
Jiwen 'Steve' Cai021d92e2017-03-16 21:07:00 -0700100void BufferItemConsumer::setBufferFreedListener(
101 const wp<BufferFreedListener>& listener) {
102 Mutex::Autolock _l(mMutex);
103 mBufferFreedListener = listener;
104}
105
Dan Stoza54716312015-03-13 14:40:34 -0700106status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700107 nsecs_t presentWhen, bool waitForFence) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700108 status_t err;
109
110 if (!item) return BAD_VALUE;
111
112 Mutex::Autolock _l(mMutex);
113
Andy McFadden1585c4d2013-06-28 13:52:40 -0700114 err = acquireBufferLocked(item, presentWhen);
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700115 if (err != OK) {
116 if (err != NO_BUFFER_AVAILABLE) {
117 BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
118 }
119 return err;
120 }
121
Jamie Gennis1df8c342012-12-20 14:05:45 -0800122 if (waitForFence) {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700123 err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700124 if (err != OK) {
125 BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
126 strerror(-err), err);
127 return err;
128 }
129 }
130
Pablo Ceballos47650f42015-08-04 16:38:17 -0700131 item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer;
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700132
133 return OK;
134}
135
Jim Shargof3b00122025-03-03 22:10:37 +0000136status_t BufferItemConsumer::attachBuffer(const sp<GraphicBuffer>& buffer) {
137 if (!buffer) {
138 BI_LOGE("BufferItemConsumer::attachBuffer no input buffer specified.");
139 return BAD_VALUE;
140 }
141
142 Mutex::Autolock _l(mMutex);
143
144 int slot = INVALID_BUFFER_SLOT;
145 status_t status = mConsumer->attachBuffer(&slot, buffer);
146 if (status != OK) {
147 BI_LOGE("BufferItemConsumer::attachBuffer unable to attach buffer %d", status);
148 return status;
149 }
150
151 mSlots[slot] = {
152 .mGraphicBuffer = buffer,
153 .mFence = nullptr,
154 .mFrameNumber = 0,
155 };
156
157 return OK;
158}
159
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700160status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
161 const sp<Fence>& releaseFence) {
Jim Shargo28e9d652024-07-10 23:50:00 +0000162 Mutex::Autolock _l(mMutex);
163 return releaseBufferSlotLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
164}
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700165
Jim Shargo28e9d652024-07-10 23:50:00 +0000166status_t BufferItemConsumer::releaseBuffer(const sp<GraphicBuffer>& buffer,
167 const sp<Fence>& releaseFence) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700168 Mutex::Autolock _l(mMutex);
169
Jim Shargo28e9d652024-07-10 23:50:00 +0000170 if (buffer == nullptr) {
171 return BAD_VALUE;
172 }
173
174 int slotIndex = getSlotForBufferLocked(buffer);
175 if (slotIndex == INVALID_BUFFER_SLOT) {
176 return BAD_VALUE;
177 }
178
179 return releaseBufferSlotLocked(slotIndex, buffer, releaseFence);
180}
Jim Shargo28e9d652024-07-10 23:50:00 +0000181
182status_t BufferItemConsumer::releaseBufferSlotLocked(int slotIndex, const sp<GraphicBuffer>& buffer,
183 const sp<Fence>& releaseFence) {
184 status_t err;
185
186 err = addReleaseFenceLocked(slotIndex, buffer, releaseFence);
John Reck82607f32018-04-27 17:01:45 -0700187 if (err != OK) {
188 BI_LOGE("Failed to addReleaseFenceLocked");
189 }
Jamie Gennisb2725412012-09-05 20:09:05 -0700190
Jim Shargo52108082024-11-15 16:53:57 +0000191 err = releaseBufferLocked(slotIndex, buffer);
John Reck82607f32018-04-27 17:01:45 -0700192 if (err != OK && err != IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700193 BI_LOGE("Failed to release buffer: %s (%d)",
194 strerror(-err), err);
195 }
196 return err;
197}
198
Jiwen 'Steve' Cai021d92e2017-03-16 21:07:00 -0700199void BufferItemConsumer::freeBufferLocked(int slotIndex) {
200 sp<BufferFreedListener> listener = mBufferFreedListener.promote();
Yi Kong48a619f2018-06-05 16:34:59 -0700201 if (listener != nullptr && mSlots[slotIndex].mGraphicBuffer != nullptr) {
Jiwen 'Steve' Cai021d92e2017-03-16 21:07:00 -0700202 // Fire callback if we have a listener registered and the buffer being freed is valid.
203 BI_LOGV("actually calling onBufferFreed");
204 listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer);
205 }
206 ConsumerBase::freeBufferLocked(slotIndex);
207}
208
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700209} // namespace android