blob: 1a975de08d2e921ee7cb3d629919e17c063888e5 [file] [log] [blame]
Jamie Gennis1a4d8832012-08-02 20:11:05 -07001/*
2 * Copyright (C) 2010 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_TAG "ConsumerBase"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
Jamie Gennis1a4d8832012-08-02 20:11:05 -070021#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
26#include <hardware/hardware.h>
27
Mathias Agopiane9e9fe42017-02-28 16:25:16 -080028#include <cutils/atomic.h>
29
Jim Shargod30823a2024-07-27 02:49:39 +000030#include <com_android_graphics_libgui_flags.h>
Dan Stozacf3834d2015-03-11 14:04:22 -070031#include <gui/BufferItem.h>
Jim Shargod30823a2024-07-27 02:49:39 +000032#include <gui/BufferQueue.h>
Jim Shargo28e9d652024-07-10 23:50:00 +000033#include <gui/ConsumerBase.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070034#include <gui/ISurfaceComposer.h>
Jim Shargod30823a2024-07-27 02:49:39 +000035#include <gui/Surface.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070036#include <gui/SurfaceComposerClient.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070037
38#include <private/gui/ComposerService.h>
39
Jim Shargo4f9c2752024-10-22 20:29:12 +000040#include <ui/BufferQueueDefs.h>
41
Jim Shargod30823a2024-07-27 02:49:39 +000042#include <log/log.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <utils/Log.h>
44#include <utils/String8.h>
45#include <utils/Trace.h>
46
Jim Shargod30823a2024-07-27 02:49:39 +000047#include <inttypes.h>
Jim Shargo28e9d652024-07-10 23:50:00 +000048
Jamie Gennis1a4d8832012-08-02 20:11:05 -070049// Macros for including the ConsumerBase name in log messages
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +000050#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
51// #define CB_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__)
52// #define CB_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__)
53// #define CB_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__)
54#define CB_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__)
Jamie Gennis1a4d8832012-08-02 20:11:05 -070055
56namespace android {
57
58// Get an ID that's unique within this process.
59static int32_t createProcessUniqueId() {
60 static volatile int32_t globalCounter = 0;
61 return android_atomic_inc(&globalCounter);
62}
63
Jim Shargo4f9c2752024-10-22 20:29:12 +000064ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp)
65 :
66#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
67 mSlots(BufferQueueDefs::NUM_BUFFER_SLOTS),
68#endif
Jamie Gennis9fea3422012-08-07 18:03:04 -070069 mAbandoned(false),
Brian Anderson3546a3f2016-07-14 11:51:14 -070070 mConsumer(bufferQueue),
Anton Ivanov08511a82025-04-14 15:39:30 -070071 mPrevFinalReleaseFence(Fence::NO_FENCE) {
72 initialize(controlledByApp);
Jim Shargod30823a2024-07-27 02:49:39 +000073}
74
75#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
76ConsumerBase::ConsumerBase(bool controlledByApp, bool consumerIsSurfaceFlinger)
Jim Shargo4f9c2752024-10-22 20:29:12 +000077 :
78#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
79 mSlots(BufferQueueDefs::NUM_BUFFER_SLOTS),
80#endif
81 mAbandoned(false),
Anton Ivanov08511a82025-04-14 15:39:30 -070082 mPrevFinalReleaseFence(Fence::NO_FENCE) {
Jim Shargod30823a2024-07-27 02:49:39 +000083 sp<IGraphicBufferProducer> producer;
84 BufferQueue::createBufferQueue(&producer, &mConsumer, consumerIsSurfaceFlinger);
85 mSurface = sp<Surface>::make(producer, controlledByApp);
Anton Ivanov08511a82025-04-14 15:39:30 -070086 initialize(controlledByApp);
Jim Shargod30823a2024-07-27 02:49:39 +000087}
88
89ConsumerBase::ConsumerBase(const sp<IGraphicBufferProducer>& producer,
90 const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp)
Jim Shargo4f9c2752024-10-22 20:29:12 +000091 :
92#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
93 mSlots(BufferQueueDefs::NUM_BUFFER_SLOTS),
94#endif
95 mAbandoned(false),
Jim Shargod30823a2024-07-27 02:49:39 +000096 mConsumer(consumer),
97 mSurface(sp<Surface>::make(producer, controlledByApp)),
Anton Ivanov08511a82025-04-14 15:39:30 -070098 mPrevFinalReleaseFence(Fence::NO_FENCE) {
99 initialize(controlledByApp);
Jim Shargod30823a2024-07-27 02:49:39 +0000100}
101
102#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
103
Anton Ivanov08511a82025-04-14 15:39:30 -0700104void ConsumerBase::initialize(bool controlledByApp) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700105 // Choose a name using the PID and a process-unique ID.
106 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
107
Anton Ivanov08511a82025-04-14 15:39:30 -0700108 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
109 // reference once the ctor ends, as that would cause the refcount of 'this'
110 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
111 // that's what we create.
112 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
113 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700114
Anton Ivanov08511a82025-04-14 15:39:30 -0700115 status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700116 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800117 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700118 strerror(-err), err);
Jim Shargo4f9c2752024-10-22 20:29:12 +0000119 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700120 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000121
122 mConsumer->setConsumerName(mName);
123#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
124 if (err = mConsumer->allowUnlimitedSlots(true); err != NO_ERROR) {
125 CB_LOGE("ConsumerBase: error marking as allowed to have unlimited slots: %s (%d)",
126 strerror(-err), err);
127 }
128#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700129}
130
131ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -0700132 CB_LOGV("~ConsumerBase");
Pablo Ceballos22b57022016-02-19 17:41:54 -0800133 Mutex::Autolock lock(mMutex);
Pablo Ceballose07e3e52016-03-15 15:07:54 -0700134
Jamie Gennisad669b02013-04-05 16:41:27 -0700135 // Verify that abandon() has been called before we get here. This should
136 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
137 // derived class to override that method and not call
138 // ConsumerBase::onLastStrongRef().
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000139 LOG_ALWAYS_FATAL_IF(!mAbandoned,
140 "[%s] ~ConsumerBase was called, but the "
141 "consumer is not abandoned!",
142 mName.c_str());
Jamie Gennisad669b02013-04-05 16:41:27 -0700143}
144
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800145void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700146 abandon();
147}
148
Jim Shargo28e9d652024-07-10 23:50:00 +0000149int ConsumerBase::getSlotForBufferLocked(const sp<GraphicBuffer>& buffer) {
150 if (!buffer) {
151 return BufferQueue::INVALID_BUFFER_SLOT;
152 }
153
154 uint64_t id = buffer->getId();
Jim Shargo4f9c2752024-10-22 20:29:12 +0000155#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
156 for (int i = 0; i < (int)mSlots.size(); ++i) {
157#else
158 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
159#endif
Jim Shargo28e9d652024-07-10 23:50:00 +0000160 auto& slot = mSlots[i];
161 if (slot.mGraphicBuffer && slot.mGraphicBuffer->getId() == id) {
162 return i;
163 }
164 }
165
166 return BufferQueue::INVALID_BUFFER_SLOT;
167}
168
169status_t ConsumerBase::detachBufferLocked(int slotIndex) {
170 status_t result = mConsumer->detachBuffer(slotIndex);
171
172 if (result != NO_ERROR) {
173 CB_LOGE("Failed to detach buffer: %d", result);
174 return result;
175 }
176
177 freeBufferLocked(slotIndex);
178
179 return result;
180}
181
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700182void ConsumerBase::freeBufferLocked(int slotIndex) {
183 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Yi Kong48a619f2018-06-05 16:34:59 -0700184 mSlots[slotIndex].mGraphicBuffer = nullptr;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800185 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700186 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700187}
188
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700189void ConsumerBase::onFrameDequeued(const uint64_t bufferId) {
190 CB_LOGV("onFrameDequeued");
191
192 sp<FrameAvailableListener> listener;
193 {
194 Mutex::Autolock lock(mFrameAvailableMutex);
195 listener = mFrameAvailableListener.promote();
196 }
197
198 if (listener != nullptr) {
199 listener->onFrameDequeued(bufferId);
200 }
201}
202
203void ConsumerBase::onFrameCancelled(const uint64_t bufferId) {
204 CB_LOGV("onFrameCancelled");
205
206 sp<FrameAvailableListener> listener;
207 {
208 Mutex::Autolock lock(mFrameAvailableMutex);
209 listener = mFrameAvailableListener.promote();
210 }
211
212 if (listener != nullptr) {
213 listener->onFrameCancelled(bufferId);
214 }
215}
216
217void ConsumerBase::onFrameDetached(const uint64_t bufferId) {
218 CB_LOGV("onFrameDetached");
219
220 sp<FrameAvailableListener> listener;
221 {
222 Mutex::Autolock lock(mFrameAvailableMutex);
223 listener = mFrameAvailableListener.promote();
224 }
225
226 if (listener != nullptr) {
227 listener->onFrameDetached(bufferId);
228 }
229}
230
Dan Stoza8dc55392014-11-04 11:37:46 -0800231void ConsumerBase::onFrameAvailable(const BufferItem& item) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700232 CB_LOGV("onFrameAvailable");
233
234 sp<FrameAvailableListener> listener;
235 { // scope for the lock
Dan Stoza95971c82017-06-26 14:27:18 -0700236 Mutex::Autolock lock(mFrameAvailableMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700237 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700238 }
239
Yi Kong48a619f2018-06-05 16:34:59 -0700240 if (listener != nullptr) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700241 CB_LOGV("actually calling onFrameAvailable");
Dan Stoza8dc55392014-11-04 11:37:46 -0800242 listener->onFrameAvailable(item);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700243 }
244}
245
Dan Stozadc13c5b2015-05-11 15:33:01 -0700246void ConsumerBase::onFrameReplaced(const BufferItem &item) {
247 CB_LOGV("onFrameReplaced");
248
249 sp<FrameAvailableListener> listener;
250 {
Dan Stoza95971c82017-06-26 14:27:18 -0700251 Mutex::Autolock lock(mFrameAvailableMutex);
Dan Stozadc13c5b2015-05-11 15:33:01 -0700252 listener = mFrameAvailableListener.promote();
253 }
254
Yi Kong48a619f2018-06-05 16:34:59 -0700255 if (listener != nullptr) {
Dan Stozadc13c5b2015-05-11 15:33:01 -0700256 CB_LOGV("actually calling onFrameReplaced");
257 listener->onFrameReplaced(item);
258 }
259}
260
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700261void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800262 Mutex::Autolock lock(mMutex);
Jim Shargofd4edb22025-03-05 17:38:19 +0000263 onBuffersReleasedLocked();
264}
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700265
Jim Shargofd4edb22025-03-05 17:38:19 +0000266void ConsumerBase::onBuffersReleasedLocked() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800267 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700268
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800269 if (mAbandoned) {
270 // Nothing to do if we're already abandoned.
271 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700272 }
273
Jim Shargo4f9c2752024-10-22 20:29:12 +0000274#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
275 std::vector<bool> mask;
276 mConsumer->getReleasedBuffersExtended(&mask);
277 for (size_t i = 0; i < mSlots.size(); i++) {
278 if (mask[i]) {
279 freeBufferLocked(i);
280 }
281 }
282#else
Dan Stozafebd4f42014-04-09 16:14:51 -0700283 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700284 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700285 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700286 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800287 freeBufferLocked(i);
288 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700289 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000290#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700291}
292
Jesse Hall399184a2014-03-03 15:42:54 -0800293void ConsumerBase::onSidebandStreamChanged() {
294}
295
Jim Shargo2e614a42024-10-02 19:31:53 +0000296#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
297void ConsumerBase::onSlotCountChanged(int slotCount) {
298 CB_LOGV("onSlotCountChanged: %d", slotCount);
299 Mutex::Autolock lock(mMutex);
300
301 if (slotCount > (int)mSlots.size()) {
302 mSlots.resize(slotCount);
303 }
304}
305#endif
306
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700307void ConsumerBase::abandon() {
308 CB_LOGV("abandon");
309 Mutex::Autolock lock(mMutex);
310
311 if (!mAbandoned) {
312 abandonLocked();
313 mAbandoned = true;
314 }
315}
316
317void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800318 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700319 if (mAbandoned) {
320 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
321 return;
322 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000323#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
324 for (int i = 0; i < (int)mSlots.size(); ++i) {
325#else
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700326 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000327#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700328 freeBufferLocked(i);
329 }
330 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700331 mConsumer->consumerDisconnect();
332 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700333}
334
John Recke4783052015-05-14 15:55:11 -0700335bool ConsumerBase::isAbandoned() {
336 Mutex::Autolock _l(mMutex);
337 return mAbandoned;
338}
339
Chia-I Wua81bc492017-11-27 10:16:00 -0800340void ConsumerBase::setName(const String8& name) {
341 Mutex::Autolock _l(mMutex);
342 if (mAbandoned) {
343 CB_LOGE("setName: ConsumerBase is abandoned!");
344 return;
345 }
346 mName = name;
347 mConsumer->setConsumerName(name);
348}
349
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700350void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700351 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700352 CB_LOGV("setFrameAvailableListener");
Dan Stoza95971c82017-06-26 14:27:18 -0700353 Mutex::Autolock lock(mFrameAvailableMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700354 mFrameAvailableListener = listener;
355}
356
Dan Stoza634f5ee2015-04-03 14:22:05 -0700357status_t ConsumerBase::detachBuffer(int slot) {
358 CB_LOGV("detachBuffer");
359 Mutex::Autolock lock(mMutex);
360
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700361 if (mAbandoned) {
362 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
363 return NO_INIT;
364 }
365
Jim Shargo28e9d652024-07-10 23:50:00 +0000366 return detachBufferLocked(slot);
367}
368
369#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
370status_t ConsumerBase::detachBuffer(const sp<GraphicBuffer>& buffer) {
371 CB_LOGV("detachBuffer");
372 Mutex::Autolock lock(mMutex);
373
374 if (mAbandoned) {
375 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
376 return NO_INIT;
377 }
378 if (buffer == nullptr) {
379 return BAD_VALUE;
Dan Stoza634f5ee2015-04-03 14:22:05 -0700380 }
381
Jim Shargo28e9d652024-07-10 23:50:00 +0000382 int slotIndex = getSlotForBufferLocked(buffer);
383 if (slotIndex == BufferQueue::INVALID_BUFFER_SLOT) {
384 return BAD_VALUE;
385 }
Dan Stoza634f5ee2015-04-03 14:22:05 -0700386
Jim Shargo28e9d652024-07-10 23:50:00 +0000387 return detachBufferLocked(slotIndex);
Dan Stoza634f5ee2015-04-03 14:22:05 -0700388}
Jim Shargo28e9d652024-07-10 23:50:00 +0000389#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
Dan Stoza634f5ee2015-04-03 14:22:05 -0700390
Carlos Martinez Romero0c9ce842025-01-17 09:50:07 -0800391status_t ConsumerBase::addReleaseFence(const sp<GraphicBuffer> buffer, const sp<Fence>& fence) {
392 CB_LOGV("addReleaseFence");
393 Mutex::Autolock lock(mMutex);
394
395 if (mAbandoned) {
396 CB_LOGE("addReleaseFence: ConsumerBase is abandoned!");
397 return NO_INIT;
398 }
399 if (buffer == nullptr) {
400 return BAD_VALUE;
401 }
402
403 int slotIndex = getSlotForBufferLocked(buffer);
404 if (slotIndex == BufferQueue::INVALID_BUFFER_SLOT) {
405 return BAD_VALUE;
406 }
407
408 return addReleaseFenceLocked(slotIndex, buffer, fence);
409}
410
Michael Lentine847f11e2015-05-18 13:41:23 -0700411status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
412 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700413 if (mAbandoned) {
414 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
415 return NO_INIT;
416 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700417 return mConsumer->setDefaultBufferSize(width, height);
418}
419
420status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
421 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700422 if (mAbandoned) {
423 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
424 return NO_INIT;
425 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700426 return mConsumer->setDefaultBufferFormat(defaultFormat);
427}
428
429status_t ConsumerBase::setDefaultBufferDataSpace(
430 android_dataspace defaultDataSpace) {
431 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700432 if (mAbandoned) {
433 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
434 return NO_INIT;
435 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700436 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
437}
438
Chia-I Wua81bc492017-11-27 10:16:00 -0800439status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
440 Mutex::Autolock lock(mMutex);
441 if (mAbandoned) {
442 CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
443 return NO_INIT;
444 }
445 return mConsumer->setConsumerUsageBits(usage);
446}
447
448status_t ConsumerBase::setTransformHint(uint32_t hint) {
449 Mutex::Autolock lock(mMutex);
450 if (mAbandoned) {
451 CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
452 return NO_INIT;
453 }
454 return mConsumer->setTransformHint(hint);
455}
456
Jim Shargod30823a2024-07-27 02:49:39 +0000457#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
458status_t ConsumerBase::setMaxBufferCount(int bufferCount) {
459 Mutex::Autolock lock(mMutex);
460 if (mAbandoned) {
461 CB_LOGE("setMaxBufferCount: ConsumerBase is abandoned!");
462 return NO_INIT;
463 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000464
465#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
466 if (status_t err = mConsumer->allowUnlimitedSlots(false); err != NO_ERROR) {
467 CB_LOGE("ConsumerBase: error marking as not allowed to have unlimited slots: %s (%d)",
468 strerror(-err), err);
469 return err;
470 }
471#endif
472
Jim Shargod30823a2024-07-27 02:49:39 +0000473 return mConsumer->setMaxBufferCount(bufferCount);
474}
475#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
476
Chia-I Wua81bc492017-11-27 10:16:00 -0800477status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
478 Mutex::Autolock lock(mMutex);
479 if (mAbandoned) {
480 CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
481 return NO_INIT;
482 }
Jim Shargofd4edb22025-03-05 17:38:19 +0000483 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers,
484 {[this]() { onBuffersReleasedLocked(); }});
Chia-I Wua81bc492017-11-27 10:16:00 -0800485}
486
Jim Shargod30823a2024-07-27 02:49:39 +0000487status_t ConsumerBase::setConsumerIsProtected(bool isProtected) {
488 Mutex::Autolock lock(mMutex);
489 if (mAbandoned) {
490 CB_LOGE("setConsumerIsProtected: ConsumerBase is abandoned!");
491 return NO_INIT;
492 }
493 return mConsumer->setConsumerIsProtected(isProtected);
494}
Jim Shargod30823a2024-07-27 02:49:39 +0000495
Chia-I Wua81bc492017-11-27 10:16:00 -0800496sp<NativeHandle> ConsumerBase::getSidebandStream() const {
497 Mutex::Autolock _l(mMutex);
498 if (mAbandoned) {
499 CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
500 return nullptr;
501 }
502
503 sp<NativeHandle> stream;
504 status_t err = mConsumer->getSidebandStream(&stream);
505 if (err != NO_ERROR) {
506 CB_LOGE("failed to get sideband stream: %d", err);
507 return nullptr;
508 }
509
510 return stream;
511}
512
Dan Stozae77c7662016-05-13 11:37:28 -0700513status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
514 std::vector<OccupancyTracker::Segment>* outHistory) {
515 Mutex::Autolock _l(mMutex);
516 if (mAbandoned) {
517 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
518 return NO_INIT;
519 }
520 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
521}
522
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700523status_t ConsumerBase::discardFreeBuffers() {
524 Mutex::Autolock _l(mMutex);
525 if (mAbandoned) {
526 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
527 return NO_INIT;
528 }
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700529 status_t err = mConsumer->discardFreeBuffers();
530 if (err != OK) {
531 return err;
532 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000533#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
534 std::vector<bool> mask;
535 mConsumer->getReleasedBuffersExtended(&mask);
536 for (int i = 0; i < (int)mSlots.size(); i++) {
537 if (mask[i]) {
538 freeBufferLocked(i);
539 }
540 }
541#else
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700542 uint64_t mask;
543 mConsumer->getReleasedBuffers(&mask);
544 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
545 if (mask & (1ULL << i)) {
546 freeBufferLocked(i);
547 }
548 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000549#endif
550
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700551 return OK;
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700552}
553
Colin Crossdc782512016-09-26 18:10:16 -0700554void ConsumerBase::dumpState(String8& result) const {
555 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700556}
557
Colin Crossdc782512016-09-26 18:10:16 -0700558void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700559 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200560 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700561}
562
Mathias Agopian74d211a2013-04-22 16:55:35 +0200563void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
564 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700565
566 if (!mAbandoned) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700567 String8 consumerState;
568 mConsumer->dumpState(String8(prefix), &consumerState);
569 result.append(consumerState);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700570 }
571}
572
Jim Shargod30823a2024-07-27 02:49:39 +0000573#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
574sp<Surface> ConsumerBase::getSurface() const {
575 LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
576 "It's illegal to get the surface of a Consumer that does not own it. This "
577 "should be impossible once the old CTOR is removed.");
578 return mSurface;
579}
580
581sp<IGraphicBufferConsumer> ConsumerBase::getIGraphicBufferConsumer() const {
582 return mConsumer;
583}
584#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
585
Dan Stozacf3834d2015-03-11 14:04:22 -0700586status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700587 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700588 if (mAbandoned) {
589 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
590 return NO_INIT;
591 }
592
Dan Stozaa4650a52015-05-12 12:56:16 -0700593 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700594 if (err != NO_ERROR) {
595 return err;
596 }
597
Yi Kong48a619f2018-06-05 16:34:59 -0700598 if (item->mGraphicBuffer != nullptr) {
599 if (mSlots[item->mSlot].mGraphicBuffer != nullptr) {
Yin-Chia Yeh723c4892017-03-30 13:13:36 -0700600 freeBufferLocked(item->mSlot);
601 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700602 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700603 }
604
Pablo Ceballos47650f42015-08-04 16:38:17 -0700605 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
606 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700607
Mark Salyzyn91100452014-06-09 14:27:45 -0700608 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700609 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700610
611 return OK;
612}
613
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700614status_t ConsumerBase::addReleaseFence(int slot,
615 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700616 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700617 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700618}
619
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700620status_t ConsumerBase::addReleaseFenceLocked(int slot,
621 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700622 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700623
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700624 // If consumer no longer tracks this graphicBuffer, we can safely
625 // drop this fence, as it will never be received by the producer.
626 if (!stillTracking(slot, graphicBuffer)) {
627 return OK;
628 }
629
Jamie Gennisb2725412012-09-05 20:09:05 -0700630 if (!mSlots[slot].mFence.get()) {
631 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700632 return OK;
633 }
634
Brian Anderson7b097e22017-08-08 16:31:37 -0700635 // Check status of fences first because merging is expensive.
636 // Merging an invalid fence with any other fence results in an
637 // invalid fence.
638 auto currentStatus = mSlots[slot].mFence->getStatus();
639 if (currentStatus == Fence::Status::Invalid) {
640 CB_LOGE("Existing fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700641 return BAD_VALUE;
642 }
643
Brian Anderson7b097e22017-08-08 16:31:37 -0700644 auto incomingStatus = fence->getStatus();
645 if (incomingStatus == Fence::Status::Invalid) {
646 CB_LOGE("New fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700647 mSlots[slot].mFence = fence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700648 return BAD_VALUE;
649 }
650
651 // If both fences are signaled or both are unsignaled, we need to merge
652 // them to get an accurate timestamp.
653 if (currentStatus == incomingStatus) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700654 char fenceName[32] = {};
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000655 snprintf(fenceName, 32, "%.28s:%d", mName.c_str(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700656 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700657 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700658 if (!mergedFence.get()) {
659 CB_LOGE("failed to merge release fences");
660 // synchronization is broken, the best we can do is hope fences
661 // signal in order so the new fence will act like a union
662 mSlots[slot].mFence = fence;
663 return BAD_VALUE;
664 }
665 mSlots[slot].mFence = mergedFence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700666 } else if (incomingStatus == Fence::Status::Unsignaled) {
667 // If one fence has signaled and the other hasn't, the unsignaled
668 // fence will approximately correspond with the correct timestamp.
669 // There's a small race if both fences signal at about the same time
670 // and their statuses are retrieved with unfortunate timing. However,
671 // by this point, they will have both signaled and only the timestamp
672 // will be slightly off; any dependencies after this point will
673 // already have been met.
674 mSlots[slot].mFence = fence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700675 }
Brian Anderson7b097e22017-08-08 16:31:37 -0700676 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
Jamie Gennisb2725412012-09-05 20:09:05 -0700677
678 return OK;
679}
680
Jim Shargodefc49e2024-11-15 01:06:41 +0000681#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
682status_t ConsumerBase::releaseBufferLocked(int slot, const sp<GraphicBuffer> graphicBuffer) {
683#else
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700684status_t ConsumerBase::releaseBufferLocked(
685 int slot, const sp<GraphicBuffer> graphicBuffer,
686 EGLDisplay display, EGLSyncKHR eglFence) {
Jim Shargodefc49e2024-11-15 01:06:41 +0000687#endif
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700688 if (mAbandoned) {
689 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
690 return NO_INIT;
691 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700692 // If consumer no longer tracks this graphicBuffer (we received a new
693 // buffer on the same slot), the buffer producer is definitely no longer
694 // tracking it.
695 if (!stillTracking(slot, graphicBuffer)) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000696 CB_LOGV("releaseBufferLocked: Not tracking, exiting without calling releaseBuffer for "
697 "slot=%d/%" PRIu64,
698 slot, mSlots[slot].mFrameNumber);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700699 return OK;
700 }
701
Mark Salyzyn91100452014-06-09 14:27:45 -0700702 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700703 slot, mSlots[slot].mFrameNumber);
Jim Shargodefc49e2024-11-15 01:06:41 +0000704#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
705 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber, mSlots[slot].mFence);
706#else
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700707 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700708 display, eglFence, mSlots[slot].mFence);
Jim Shargodefc49e2024-11-15 01:06:41 +0000709#endif
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800710 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700711 freeBufferLocked(slot);
712 }
713
Brian Anderson3546a3f2016-07-14 11:51:14 -0700714 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800715 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700716
717 return err;
718}
719
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700720bool ConsumerBase::stillTracking(int slot,
721 const sp<GraphicBuffer> graphicBuffer) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000722#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
723 if (slot < 0 || slot >= (int)mSlots.size()) {
724#else
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700725 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000726#endif
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700727 return false;
728 }
Yi Kong48a619f2018-06-05 16:34:59 -0700729 return (mSlots[slot].mGraphicBuffer != nullptr &&
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700730 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
731}
732
Andy McFadden2adaf042012-12-18 09:49:45 -0800733} // namespace android