blob: 0266a3f2e05e8262e21833af1d34ac2a1bb4f850 [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),
71 mPrevFinalReleaseFence(Fence::NO_FENCE) {
Jim Shargod30823a2024-07-27 02:49:39 +000072 initialize(controlledByApp);
73}
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),
82 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);
86 initialize(controlledByApp);
87}
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)),
98 mPrevFinalReleaseFence(Fence::NO_FENCE) {
99 initialize(controlledByApp);
100}
101
102#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
103
104void 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
108 // 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.
Mathias Agopiana4e19522013-07-31 20:09:53 -0700112 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
113 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700114
Mathias Agopiandb89edc2013-08-02 01:40:18 -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);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700263
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800264 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700265
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800266 if (mAbandoned) {
267 // Nothing to do if we're already abandoned.
268 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700269 }
270
Jim Shargo4f9c2752024-10-22 20:29:12 +0000271#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
272 std::vector<bool> mask;
273 mConsumer->getReleasedBuffersExtended(&mask);
274 for (size_t i = 0; i < mSlots.size(); i++) {
275 if (mask[i]) {
276 freeBufferLocked(i);
277 }
278 }
279#else
Dan Stozafebd4f42014-04-09 16:14:51 -0700280 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700281 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700282 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700283 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800284 freeBufferLocked(i);
285 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700286 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000287#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700288}
289
Jesse Hall399184a2014-03-03 15:42:54 -0800290void ConsumerBase::onSidebandStreamChanged() {
291}
292
Jim Shargo2e614a42024-10-02 19:31:53 +0000293#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
294void ConsumerBase::onSlotCountChanged(int slotCount) {
295 CB_LOGV("onSlotCountChanged: %d", slotCount);
296 Mutex::Autolock lock(mMutex);
297
298 if (slotCount > (int)mSlots.size()) {
299 mSlots.resize(slotCount);
300 }
301}
302#endif
303
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700304void ConsumerBase::abandon() {
305 CB_LOGV("abandon");
306 Mutex::Autolock lock(mMutex);
307
308 if (!mAbandoned) {
309 abandonLocked();
310 mAbandoned = true;
311 }
312}
313
314void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800315 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700316 if (mAbandoned) {
317 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
318 return;
319 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000320#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
321 for (int i = 0; i < (int)mSlots.size(); ++i) {
322#else
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700323 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000324#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700325 freeBufferLocked(i);
326 }
327 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700328 mConsumer->consumerDisconnect();
329 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700330}
331
John Recke4783052015-05-14 15:55:11 -0700332bool ConsumerBase::isAbandoned() {
333 Mutex::Autolock _l(mMutex);
334 return mAbandoned;
335}
336
Chia-I Wua81bc492017-11-27 10:16:00 -0800337void ConsumerBase::setName(const String8& name) {
338 Mutex::Autolock _l(mMutex);
339 if (mAbandoned) {
340 CB_LOGE("setName: ConsumerBase is abandoned!");
341 return;
342 }
343 mName = name;
344 mConsumer->setConsumerName(name);
345}
346
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700347void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700348 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700349 CB_LOGV("setFrameAvailableListener");
Dan Stoza95971c82017-06-26 14:27:18 -0700350 Mutex::Autolock lock(mFrameAvailableMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700351 mFrameAvailableListener = listener;
352}
353
Dan Stoza634f5ee2015-04-03 14:22:05 -0700354status_t ConsumerBase::detachBuffer(int slot) {
355 CB_LOGV("detachBuffer");
356 Mutex::Autolock lock(mMutex);
357
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700358 if (mAbandoned) {
359 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
360 return NO_INIT;
361 }
362
Jim Shargo28e9d652024-07-10 23:50:00 +0000363 return detachBufferLocked(slot);
364}
365
366#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
367status_t ConsumerBase::detachBuffer(const sp<GraphicBuffer>& buffer) {
368 CB_LOGV("detachBuffer");
369 Mutex::Autolock lock(mMutex);
370
371 if (mAbandoned) {
372 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
373 return NO_INIT;
374 }
375 if (buffer == nullptr) {
376 return BAD_VALUE;
Dan Stoza634f5ee2015-04-03 14:22:05 -0700377 }
378
Jim Shargo28e9d652024-07-10 23:50:00 +0000379 int slotIndex = getSlotForBufferLocked(buffer);
380 if (slotIndex == BufferQueue::INVALID_BUFFER_SLOT) {
381 return BAD_VALUE;
382 }
Dan Stoza634f5ee2015-04-03 14:22:05 -0700383
Jim Shargo28e9d652024-07-10 23:50:00 +0000384 return detachBufferLocked(slotIndex);
Dan Stoza634f5ee2015-04-03 14:22:05 -0700385}
Jim Shargo28e9d652024-07-10 23:50:00 +0000386#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
Dan Stoza634f5ee2015-04-03 14:22:05 -0700387
Carlos Martinez Romero0c9ce842025-01-17 09:50:07 -0800388status_t ConsumerBase::addReleaseFence(const sp<GraphicBuffer> buffer, const sp<Fence>& fence) {
389 CB_LOGV("addReleaseFence");
390 Mutex::Autolock lock(mMutex);
391
392 if (mAbandoned) {
393 CB_LOGE("addReleaseFence: ConsumerBase is abandoned!");
394 return NO_INIT;
395 }
396 if (buffer == nullptr) {
397 return BAD_VALUE;
398 }
399
400 int slotIndex = getSlotForBufferLocked(buffer);
401 if (slotIndex == BufferQueue::INVALID_BUFFER_SLOT) {
402 return BAD_VALUE;
403 }
404
405 return addReleaseFenceLocked(slotIndex, buffer, fence);
406}
407
Michael Lentine847f11e2015-05-18 13:41:23 -0700408status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
409 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700410 if (mAbandoned) {
411 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
412 return NO_INIT;
413 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700414 return mConsumer->setDefaultBufferSize(width, height);
415}
416
417status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
418 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700419 if (mAbandoned) {
420 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
421 return NO_INIT;
422 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700423 return mConsumer->setDefaultBufferFormat(defaultFormat);
424}
425
426status_t ConsumerBase::setDefaultBufferDataSpace(
427 android_dataspace defaultDataSpace) {
428 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700429 if (mAbandoned) {
430 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
431 return NO_INIT;
432 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700433 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
434}
435
Chia-I Wua81bc492017-11-27 10:16:00 -0800436status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
437 Mutex::Autolock lock(mMutex);
438 if (mAbandoned) {
439 CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
440 return NO_INIT;
441 }
442 return mConsumer->setConsumerUsageBits(usage);
443}
444
445status_t ConsumerBase::setTransformHint(uint32_t hint) {
446 Mutex::Autolock lock(mMutex);
447 if (mAbandoned) {
448 CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
449 return NO_INIT;
450 }
451 return mConsumer->setTransformHint(hint);
452}
453
Jim Shargod30823a2024-07-27 02:49:39 +0000454#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
455status_t ConsumerBase::setMaxBufferCount(int bufferCount) {
456 Mutex::Autolock lock(mMutex);
457 if (mAbandoned) {
458 CB_LOGE("setMaxBufferCount: ConsumerBase is abandoned!");
459 return NO_INIT;
460 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000461
462#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
463 if (status_t err = mConsumer->allowUnlimitedSlots(false); err != NO_ERROR) {
464 CB_LOGE("ConsumerBase: error marking as not allowed to have unlimited slots: %s (%d)",
465 strerror(-err), err);
466 return err;
467 }
468#endif
469
Jim Shargod30823a2024-07-27 02:49:39 +0000470 return mConsumer->setMaxBufferCount(bufferCount);
471}
472#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
473
Chia-I Wua81bc492017-11-27 10:16:00 -0800474status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
475 Mutex::Autolock lock(mMutex);
476 if (mAbandoned) {
477 CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
478 return NO_INIT;
479 }
480 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
481}
482
Jim Shargod30823a2024-07-27 02:49:39 +0000483status_t ConsumerBase::setConsumerIsProtected(bool isProtected) {
484 Mutex::Autolock lock(mMutex);
485 if (mAbandoned) {
486 CB_LOGE("setConsumerIsProtected: ConsumerBase is abandoned!");
487 return NO_INIT;
488 }
489 return mConsumer->setConsumerIsProtected(isProtected);
490}
Jim Shargod30823a2024-07-27 02:49:39 +0000491
Chia-I Wua81bc492017-11-27 10:16:00 -0800492sp<NativeHandle> ConsumerBase::getSidebandStream() const {
493 Mutex::Autolock _l(mMutex);
494 if (mAbandoned) {
495 CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
496 return nullptr;
497 }
498
499 sp<NativeHandle> stream;
500 status_t err = mConsumer->getSidebandStream(&stream);
501 if (err != NO_ERROR) {
502 CB_LOGE("failed to get sideband stream: %d", err);
503 return nullptr;
504 }
505
506 return stream;
507}
508
Dan Stozae77c7662016-05-13 11:37:28 -0700509status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
510 std::vector<OccupancyTracker::Segment>* outHistory) {
511 Mutex::Autolock _l(mMutex);
512 if (mAbandoned) {
513 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
514 return NO_INIT;
515 }
516 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
517}
518
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700519status_t ConsumerBase::discardFreeBuffers() {
520 Mutex::Autolock _l(mMutex);
521 if (mAbandoned) {
522 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
523 return NO_INIT;
524 }
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700525 status_t err = mConsumer->discardFreeBuffers();
526 if (err != OK) {
527 return err;
528 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000529#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
530 std::vector<bool> mask;
531 mConsumer->getReleasedBuffersExtended(&mask);
532 for (int i = 0; i < (int)mSlots.size(); i++) {
533 if (mask[i]) {
534 freeBufferLocked(i);
535 }
536 }
537#else
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700538 uint64_t mask;
539 mConsumer->getReleasedBuffers(&mask);
540 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
541 if (mask & (1ULL << i)) {
542 freeBufferLocked(i);
543 }
544 }
Jim Shargo4f9c2752024-10-22 20:29:12 +0000545#endif
546
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700547 return OK;
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700548}
549
Colin Crossdc782512016-09-26 18:10:16 -0700550void ConsumerBase::dumpState(String8& result) const {
551 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700552}
553
Colin Crossdc782512016-09-26 18:10:16 -0700554void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700555 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200556 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700557}
558
Mathias Agopian74d211a2013-04-22 16:55:35 +0200559void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
560 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700561
562 if (!mAbandoned) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700563 String8 consumerState;
564 mConsumer->dumpState(String8(prefix), &consumerState);
565 result.append(consumerState);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700566 }
567}
568
Jim Shargod30823a2024-07-27 02:49:39 +0000569#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
570sp<Surface> ConsumerBase::getSurface() const {
571 LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
572 "It's illegal to get the surface of a Consumer that does not own it. This "
573 "should be impossible once the old CTOR is removed.");
574 return mSurface;
575}
576
577sp<IGraphicBufferConsumer> ConsumerBase::getIGraphicBufferConsumer() const {
578 return mConsumer;
579}
580#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
581
Dan Stozacf3834d2015-03-11 14:04:22 -0700582status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700583 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700584 if (mAbandoned) {
585 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
586 return NO_INIT;
587 }
588
Dan Stozaa4650a52015-05-12 12:56:16 -0700589 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700590 if (err != NO_ERROR) {
591 return err;
592 }
593
Yi Kong48a619f2018-06-05 16:34:59 -0700594 if (item->mGraphicBuffer != nullptr) {
595 if (mSlots[item->mSlot].mGraphicBuffer != nullptr) {
Yin-Chia Yeh723c4892017-03-30 13:13:36 -0700596 freeBufferLocked(item->mSlot);
597 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700598 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700599 }
600
Pablo Ceballos47650f42015-08-04 16:38:17 -0700601 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
602 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700603
Mark Salyzyn91100452014-06-09 14:27:45 -0700604 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700605 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700606
607 return OK;
608}
609
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700610status_t ConsumerBase::addReleaseFence(int slot,
611 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700612 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700613 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700614}
615
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700616status_t ConsumerBase::addReleaseFenceLocked(int slot,
617 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700618 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700619
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700620 // If consumer no longer tracks this graphicBuffer, we can safely
621 // drop this fence, as it will never be received by the producer.
622 if (!stillTracking(slot, graphicBuffer)) {
623 return OK;
624 }
625
Jamie Gennisb2725412012-09-05 20:09:05 -0700626 if (!mSlots[slot].mFence.get()) {
627 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700628 return OK;
629 }
630
Brian Anderson7b097e22017-08-08 16:31:37 -0700631 // Check status of fences first because merging is expensive.
632 // Merging an invalid fence with any other fence results in an
633 // invalid fence.
634 auto currentStatus = mSlots[slot].mFence->getStatus();
635 if (currentStatus == Fence::Status::Invalid) {
636 CB_LOGE("Existing fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700637 return BAD_VALUE;
638 }
639
Brian Anderson7b097e22017-08-08 16:31:37 -0700640 auto incomingStatus = fence->getStatus();
641 if (incomingStatus == Fence::Status::Invalid) {
642 CB_LOGE("New fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700643 mSlots[slot].mFence = fence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700644 return BAD_VALUE;
645 }
646
647 // If both fences are signaled or both are unsignaled, we need to merge
648 // them to get an accurate timestamp.
649 if (currentStatus == incomingStatus) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700650 char fenceName[32] = {};
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000651 snprintf(fenceName, 32, "%.28s:%d", mName.c_str(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700652 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700653 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700654 if (!mergedFence.get()) {
655 CB_LOGE("failed to merge release fences");
656 // synchronization is broken, the best we can do is hope fences
657 // signal in order so the new fence will act like a union
658 mSlots[slot].mFence = fence;
659 return BAD_VALUE;
660 }
661 mSlots[slot].mFence = mergedFence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700662 } else if (incomingStatus == Fence::Status::Unsignaled) {
663 // If one fence has signaled and the other hasn't, the unsignaled
664 // fence will approximately correspond with the correct timestamp.
665 // There's a small race if both fences signal at about the same time
666 // and their statuses are retrieved with unfortunate timing. However,
667 // by this point, they will have both signaled and only the timestamp
668 // will be slightly off; any dependencies after this point will
669 // already have been met.
670 mSlots[slot].mFence = fence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700671 }
Brian Anderson7b097e22017-08-08 16:31:37 -0700672 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
Jamie Gennisb2725412012-09-05 20:09:05 -0700673
674 return OK;
675}
676
Jim Shargodefc49e2024-11-15 01:06:41 +0000677#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
678status_t ConsumerBase::releaseBufferLocked(int slot, const sp<GraphicBuffer> graphicBuffer) {
679#else
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700680status_t ConsumerBase::releaseBufferLocked(
681 int slot, const sp<GraphicBuffer> graphicBuffer,
682 EGLDisplay display, EGLSyncKHR eglFence) {
Jim Shargodefc49e2024-11-15 01:06:41 +0000683#endif
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700684 if (mAbandoned) {
685 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
686 return NO_INIT;
687 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700688 // If consumer no longer tracks this graphicBuffer (we received a new
689 // buffer on the same slot), the buffer producer is definitely no longer
690 // tracking it.
691 if (!stillTracking(slot, graphicBuffer)) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000692 CB_LOGV("releaseBufferLocked: Not tracking, exiting without calling releaseBuffer for "
693 "slot=%d/%" PRIu64,
694 slot, mSlots[slot].mFrameNumber);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700695 return OK;
696 }
697
Mark Salyzyn91100452014-06-09 14:27:45 -0700698 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700699 slot, mSlots[slot].mFrameNumber);
Jim Shargodefc49e2024-11-15 01:06:41 +0000700#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
701 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber, mSlots[slot].mFence);
702#else
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700703 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700704 display, eglFence, mSlots[slot].mFence);
Jim Shargodefc49e2024-11-15 01:06:41 +0000705#endif
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800706 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700707 freeBufferLocked(slot);
708 }
709
Brian Anderson3546a3f2016-07-14 11:51:14 -0700710 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800711 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700712
713 return err;
714}
715
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700716bool ConsumerBase::stillTracking(int slot,
717 const sp<GraphicBuffer> graphicBuffer) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000718#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
719 if (slot < 0 || slot >= (int)mSlots.size()) {
720#else
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700721 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
Jim Shargo4f9c2752024-10-22 20:29:12 +0000722#endif
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700723 return false;
724 }
Yi Kong48a619f2018-06-05 16:34:59 -0700725 return (mSlots[slot].mGraphicBuffer != nullptr &&
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700726 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
727}
728
Andy McFadden2adaf042012-12-18 09:49:45 -0800729} // namespace android