blob: e772f44a4ff4dceca50e96791d21a4971dadd956 [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 Shargod30823a2024-07-27 02:49:39 +000040#include <log/log.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <utils/Log.h>
42#include <utils/String8.h>
43#include <utils/Trace.h>
44
Jim Shargod30823a2024-07-27 02:49:39 +000045#include <inttypes.h>
Jim Shargo28e9d652024-07-10 23:50:00 +000046
Jamie Gennis1a4d8832012-08-02 20:11:05 -070047// Macros for including the ConsumerBase name in log messages
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +000048#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
49// #define CB_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__)
50// #define CB_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__)
51// #define CB_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__)
52#define CB_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__)
Jamie Gennis1a4d8832012-08-02 20:11:05 -070053
54namespace android {
55
56// Get an ID that's unique within this process.
57static int32_t createProcessUniqueId() {
58 static volatile int32_t globalCounter = 0;
59 return android_atomic_inc(&globalCounter);
60}
61
Mathias Agopiandb89edc2013-08-02 01:40:18 -070062ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070063 mAbandoned(false),
Brian Anderson3546a3f2016-07-14 11:51:14 -070064 mConsumer(bufferQueue),
65 mPrevFinalReleaseFence(Fence::NO_FENCE) {
Jim Shargod30823a2024-07-27 02:49:39 +000066 initialize(controlledByApp);
67}
68
69#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
70ConsumerBase::ConsumerBase(bool controlledByApp, bool consumerIsSurfaceFlinger)
71 : mAbandoned(false), mPrevFinalReleaseFence(Fence::NO_FENCE) {
72 sp<IGraphicBufferProducer> producer;
73 BufferQueue::createBufferQueue(&producer, &mConsumer, consumerIsSurfaceFlinger);
74 mSurface = sp<Surface>::make(producer, controlledByApp);
75 initialize(controlledByApp);
76}
77
78ConsumerBase::ConsumerBase(const sp<IGraphicBufferProducer>& producer,
79 const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp)
80 : mAbandoned(false),
81 mConsumer(consumer),
82 mSurface(sp<Surface>::make(producer, controlledByApp)),
83 mPrevFinalReleaseFence(Fence::NO_FENCE) {
84 initialize(controlledByApp);
85}
86
87#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
88
89void ConsumerBase::initialize(bool controlledByApp) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070090 // Choose a name using the PID and a process-unique ID.
91 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
92
93 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
94 // reference once the ctor ends, as that would cause the refcount of 'this'
95 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
96 // that's what we create.
Mathias Agopiana4e19522013-07-31 20:09:53 -070097 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
98 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070099
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700100 status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700101 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800102 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700103 strerror(-err), err);
104 } else {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700105 mConsumer->setConsumerName(mName);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700106 }
107}
108
109ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -0700110 CB_LOGV("~ConsumerBase");
Pablo Ceballos22b57022016-02-19 17:41:54 -0800111 Mutex::Autolock lock(mMutex);
Pablo Ceballose07e3e52016-03-15 15:07:54 -0700112
Jamie Gennisad669b02013-04-05 16:41:27 -0700113 // Verify that abandon() has been called before we get here. This should
114 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
115 // derived class to override that method and not call
116 // ConsumerBase::onLastStrongRef().
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000117 LOG_ALWAYS_FATAL_IF(!mAbandoned,
118 "[%s] ~ConsumerBase was called, but the "
119 "consumer is not abandoned!",
120 mName.c_str());
Jamie Gennisad669b02013-04-05 16:41:27 -0700121}
122
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800123void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700124 abandon();
125}
126
Jim Shargo28e9d652024-07-10 23:50:00 +0000127int ConsumerBase::getSlotForBufferLocked(const sp<GraphicBuffer>& buffer) {
128 if (!buffer) {
129 return BufferQueue::INVALID_BUFFER_SLOT;
130 }
131
132 uint64_t id = buffer->getId();
133 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; i++) {
134 auto& slot = mSlots[i];
135 if (slot.mGraphicBuffer && slot.mGraphicBuffer->getId() == id) {
136 return i;
137 }
138 }
139
140 return BufferQueue::INVALID_BUFFER_SLOT;
141}
142
143status_t ConsumerBase::detachBufferLocked(int slotIndex) {
144 status_t result = mConsumer->detachBuffer(slotIndex);
145
146 if (result != NO_ERROR) {
147 CB_LOGE("Failed to detach buffer: %d", result);
148 return result;
149 }
150
151 freeBufferLocked(slotIndex);
152
153 return result;
154}
155
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700156void ConsumerBase::freeBufferLocked(int slotIndex) {
157 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Yi Kong48a619f2018-06-05 16:34:59 -0700158 mSlots[slotIndex].mGraphicBuffer = nullptr;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800159 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700160 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700161}
162
Adithya Srinivasan2e434382019-10-09 11:43:01 -0700163void ConsumerBase::onFrameDequeued(const uint64_t bufferId) {
164 CB_LOGV("onFrameDequeued");
165
166 sp<FrameAvailableListener> listener;
167 {
168 Mutex::Autolock lock(mFrameAvailableMutex);
169 listener = mFrameAvailableListener.promote();
170 }
171
172 if (listener != nullptr) {
173 listener->onFrameDequeued(bufferId);
174 }
175}
176
177void ConsumerBase::onFrameCancelled(const uint64_t bufferId) {
178 CB_LOGV("onFrameCancelled");
179
180 sp<FrameAvailableListener> listener;
181 {
182 Mutex::Autolock lock(mFrameAvailableMutex);
183 listener = mFrameAvailableListener.promote();
184 }
185
186 if (listener != nullptr) {
187 listener->onFrameCancelled(bufferId);
188 }
189}
190
191void ConsumerBase::onFrameDetached(const uint64_t bufferId) {
192 CB_LOGV("onFrameDetached");
193
194 sp<FrameAvailableListener> listener;
195 {
196 Mutex::Autolock lock(mFrameAvailableMutex);
197 listener = mFrameAvailableListener.promote();
198 }
199
200 if (listener != nullptr) {
201 listener->onFrameDetached(bufferId);
202 }
203}
204
Dan Stoza8dc55392014-11-04 11:37:46 -0800205void ConsumerBase::onFrameAvailable(const BufferItem& item) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700206 CB_LOGV("onFrameAvailable");
207
208 sp<FrameAvailableListener> listener;
209 { // scope for the lock
Dan Stoza95971c82017-06-26 14:27:18 -0700210 Mutex::Autolock lock(mFrameAvailableMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700211 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700212 }
213
Yi Kong48a619f2018-06-05 16:34:59 -0700214 if (listener != nullptr) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700215 CB_LOGV("actually calling onFrameAvailable");
Dan Stoza8dc55392014-11-04 11:37:46 -0800216 listener->onFrameAvailable(item);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700217 }
218}
219
Dan Stozadc13c5b2015-05-11 15:33:01 -0700220void ConsumerBase::onFrameReplaced(const BufferItem &item) {
221 CB_LOGV("onFrameReplaced");
222
223 sp<FrameAvailableListener> listener;
224 {
Dan Stoza95971c82017-06-26 14:27:18 -0700225 Mutex::Autolock lock(mFrameAvailableMutex);
Dan Stozadc13c5b2015-05-11 15:33:01 -0700226 listener = mFrameAvailableListener.promote();
227 }
228
Yi Kong48a619f2018-06-05 16:34:59 -0700229 if (listener != nullptr) {
Dan Stozadc13c5b2015-05-11 15:33:01 -0700230 CB_LOGV("actually calling onFrameReplaced");
231 listener->onFrameReplaced(item);
232 }
233}
234
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700235void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800236 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700237
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800238 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700239
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800240 if (mAbandoned) {
241 // Nothing to do if we're already abandoned.
242 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700243 }
244
Dan Stozafebd4f42014-04-09 16:14:51 -0700245 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700246 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700247 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700248 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800249 freeBufferLocked(i);
250 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700251 }
252}
253
Jesse Hall399184a2014-03-03 15:42:54 -0800254void ConsumerBase::onSidebandStreamChanged() {
255}
256
Jim Shargo2e614a42024-10-02 19:31:53 +0000257#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
258void ConsumerBase::onSlotCountChanged(int slotCount) {
259 CB_LOGV("onSlotCountChanged: %d", slotCount);
260 Mutex::Autolock lock(mMutex);
261
262 if (slotCount > (int)mSlots.size()) {
263 mSlots.resize(slotCount);
264 }
265}
266#endif
267
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700268void ConsumerBase::abandon() {
269 CB_LOGV("abandon");
270 Mutex::Autolock lock(mMutex);
271
272 if (!mAbandoned) {
273 abandonLocked();
274 mAbandoned = true;
275 }
276}
277
278void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800279 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700280 if (mAbandoned) {
281 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
282 return;
283 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700284 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
285 freeBufferLocked(i);
286 }
287 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700288 mConsumer->consumerDisconnect();
289 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700290}
291
John Recke4783052015-05-14 15:55:11 -0700292bool ConsumerBase::isAbandoned() {
293 Mutex::Autolock _l(mMutex);
294 return mAbandoned;
295}
296
Chia-I Wua81bc492017-11-27 10:16:00 -0800297void ConsumerBase::setName(const String8& name) {
298 Mutex::Autolock _l(mMutex);
299 if (mAbandoned) {
300 CB_LOGE("setName: ConsumerBase is abandoned!");
301 return;
302 }
303 mName = name;
304 mConsumer->setConsumerName(name);
305}
306
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700307void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700308 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700309 CB_LOGV("setFrameAvailableListener");
Dan Stoza95971c82017-06-26 14:27:18 -0700310 Mutex::Autolock lock(mFrameAvailableMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700311 mFrameAvailableListener = listener;
312}
313
Dan Stoza634f5ee2015-04-03 14:22:05 -0700314status_t ConsumerBase::detachBuffer(int slot) {
315 CB_LOGV("detachBuffer");
316 Mutex::Autolock lock(mMutex);
317
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700318 if (mAbandoned) {
319 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
320 return NO_INIT;
321 }
322
Jim Shargo28e9d652024-07-10 23:50:00 +0000323 return detachBufferLocked(slot);
324}
325
326#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
327status_t ConsumerBase::detachBuffer(const sp<GraphicBuffer>& buffer) {
328 CB_LOGV("detachBuffer");
329 Mutex::Autolock lock(mMutex);
330
331 if (mAbandoned) {
332 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
333 return NO_INIT;
334 }
335 if (buffer == nullptr) {
336 return BAD_VALUE;
Dan Stoza634f5ee2015-04-03 14:22:05 -0700337 }
338
Jim Shargo28e9d652024-07-10 23:50:00 +0000339 int slotIndex = getSlotForBufferLocked(buffer);
340 if (slotIndex == BufferQueue::INVALID_BUFFER_SLOT) {
341 return BAD_VALUE;
342 }
Dan Stoza634f5ee2015-04-03 14:22:05 -0700343
Jim Shargo28e9d652024-07-10 23:50:00 +0000344 return detachBufferLocked(slotIndex);
Dan Stoza634f5ee2015-04-03 14:22:05 -0700345}
Jim Shargo28e9d652024-07-10 23:50:00 +0000346#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
Dan Stoza634f5ee2015-04-03 14:22:05 -0700347
Michael Lentine847f11e2015-05-18 13:41:23 -0700348status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
349 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700350 if (mAbandoned) {
351 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
352 return NO_INIT;
353 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700354 return mConsumer->setDefaultBufferSize(width, height);
355}
356
357status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
358 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700359 if (mAbandoned) {
360 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
361 return NO_INIT;
362 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700363 return mConsumer->setDefaultBufferFormat(defaultFormat);
364}
365
366status_t ConsumerBase::setDefaultBufferDataSpace(
367 android_dataspace defaultDataSpace) {
368 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700369 if (mAbandoned) {
370 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
371 return NO_INIT;
372 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700373 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
374}
375
Chia-I Wua81bc492017-11-27 10:16:00 -0800376status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
377 Mutex::Autolock lock(mMutex);
378 if (mAbandoned) {
379 CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
380 return NO_INIT;
381 }
382 return mConsumer->setConsumerUsageBits(usage);
383}
384
385status_t ConsumerBase::setTransformHint(uint32_t hint) {
386 Mutex::Autolock lock(mMutex);
387 if (mAbandoned) {
388 CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
389 return NO_INIT;
390 }
391 return mConsumer->setTransformHint(hint);
392}
393
Jim Shargod30823a2024-07-27 02:49:39 +0000394#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
395status_t ConsumerBase::setMaxBufferCount(int bufferCount) {
396 Mutex::Autolock lock(mMutex);
397 if (mAbandoned) {
398 CB_LOGE("setMaxBufferCount: ConsumerBase is abandoned!");
399 return NO_INIT;
400 }
401 return mConsumer->setMaxBufferCount(bufferCount);
402}
403#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
404
Chia-I Wua81bc492017-11-27 10:16:00 -0800405status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
406 Mutex::Autolock lock(mMutex);
407 if (mAbandoned) {
408 CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
409 return NO_INIT;
410 }
411 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
412}
413
Jim Shargod30823a2024-07-27 02:49:39 +0000414#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
415status_t ConsumerBase::setConsumerIsProtected(bool isProtected) {
416 Mutex::Autolock lock(mMutex);
417 if (mAbandoned) {
418 CB_LOGE("setConsumerIsProtected: ConsumerBase is abandoned!");
419 return NO_INIT;
420 }
421 return mConsumer->setConsumerIsProtected(isProtected);
422}
423#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
424
Chia-I Wua81bc492017-11-27 10:16:00 -0800425sp<NativeHandle> ConsumerBase::getSidebandStream() const {
426 Mutex::Autolock _l(mMutex);
427 if (mAbandoned) {
428 CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
429 return nullptr;
430 }
431
432 sp<NativeHandle> stream;
433 status_t err = mConsumer->getSidebandStream(&stream);
434 if (err != NO_ERROR) {
435 CB_LOGE("failed to get sideband stream: %d", err);
436 return nullptr;
437 }
438
439 return stream;
440}
441
Dan Stozae77c7662016-05-13 11:37:28 -0700442status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
443 std::vector<OccupancyTracker::Segment>* outHistory) {
444 Mutex::Autolock _l(mMutex);
445 if (mAbandoned) {
446 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
447 return NO_INIT;
448 }
449 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
450}
451
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700452status_t ConsumerBase::discardFreeBuffers() {
453 Mutex::Autolock _l(mMutex);
454 if (mAbandoned) {
455 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
456 return NO_INIT;
457 }
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700458 status_t err = mConsumer->discardFreeBuffers();
459 if (err != OK) {
460 return err;
461 }
462 uint64_t mask;
463 mConsumer->getReleasedBuffers(&mask);
464 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
465 if (mask & (1ULL << i)) {
466 freeBufferLocked(i);
467 }
468 }
469 return OK;
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700470}
471
Colin Crossdc782512016-09-26 18:10:16 -0700472void ConsumerBase::dumpState(String8& result) const {
473 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700474}
475
Colin Crossdc782512016-09-26 18:10:16 -0700476void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700477 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200478 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700479}
480
Mathias Agopian74d211a2013-04-22 16:55:35 +0200481void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
482 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700483
484 if (!mAbandoned) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700485 String8 consumerState;
486 mConsumer->dumpState(String8(prefix), &consumerState);
487 result.append(consumerState);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700488 }
489}
490
Jim Shargod30823a2024-07-27 02:49:39 +0000491#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
492sp<Surface> ConsumerBase::getSurface() const {
493 LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
494 "It's illegal to get the surface of a Consumer that does not own it. This "
495 "should be impossible once the old CTOR is removed.");
496 return mSurface;
497}
498
499sp<IGraphicBufferConsumer> ConsumerBase::getIGraphicBufferConsumer() const {
500 return mConsumer;
501}
502#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
503
Dan Stozacf3834d2015-03-11 14:04:22 -0700504status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700505 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700506 if (mAbandoned) {
507 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
508 return NO_INIT;
509 }
510
Dan Stozaa4650a52015-05-12 12:56:16 -0700511 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700512 if (err != NO_ERROR) {
513 return err;
514 }
515
Yi Kong48a619f2018-06-05 16:34:59 -0700516 if (item->mGraphicBuffer != nullptr) {
517 if (mSlots[item->mSlot].mGraphicBuffer != nullptr) {
Yin-Chia Yeh723c4892017-03-30 13:13:36 -0700518 freeBufferLocked(item->mSlot);
519 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700520 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700521 }
522
Pablo Ceballos47650f42015-08-04 16:38:17 -0700523 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
524 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700525
Mark Salyzyn91100452014-06-09 14:27:45 -0700526 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700527 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700528
529 return OK;
530}
531
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700532status_t ConsumerBase::addReleaseFence(int slot,
533 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700534 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700535 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700536}
537
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700538status_t ConsumerBase::addReleaseFenceLocked(int slot,
539 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700540 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700541
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700542 // If consumer no longer tracks this graphicBuffer, we can safely
543 // drop this fence, as it will never be received by the producer.
544 if (!stillTracking(slot, graphicBuffer)) {
545 return OK;
546 }
547
Jamie Gennisb2725412012-09-05 20:09:05 -0700548 if (!mSlots[slot].mFence.get()) {
549 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700550 return OK;
551 }
552
Brian Anderson7b097e22017-08-08 16:31:37 -0700553 // Check status of fences first because merging is expensive.
554 // Merging an invalid fence with any other fence results in an
555 // invalid fence.
556 auto currentStatus = mSlots[slot].mFence->getStatus();
557 if (currentStatus == Fence::Status::Invalid) {
558 CB_LOGE("Existing fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700559 return BAD_VALUE;
560 }
561
Brian Anderson7b097e22017-08-08 16:31:37 -0700562 auto incomingStatus = fence->getStatus();
563 if (incomingStatus == Fence::Status::Invalid) {
564 CB_LOGE("New fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700565 mSlots[slot].mFence = fence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700566 return BAD_VALUE;
567 }
568
569 // If both fences are signaled or both are unsignaled, we need to merge
570 // them to get an accurate timestamp.
571 if (currentStatus == incomingStatus) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700572 char fenceName[32] = {};
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000573 snprintf(fenceName, 32, "%.28s:%d", mName.c_str(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700574 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700575 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700576 if (!mergedFence.get()) {
577 CB_LOGE("failed to merge release fences");
578 // synchronization is broken, the best we can do is hope fences
579 // signal in order so the new fence will act like a union
580 mSlots[slot].mFence = fence;
581 return BAD_VALUE;
582 }
583 mSlots[slot].mFence = mergedFence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700584 } else if (incomingStatus == Fence::Status::Unsignaled) {
585 // If one fence has signaled and the other hasn't, the unsignaled
586 // fence will approximately correspond with the correct timestamp.
587 // There's a small race if both fences signal at about the same time
588 // and their statuses are retrieved with unfortunate timing. However,
589 // by this point, they will have both signaled and only the timestamp
590 // will be slightly off; any dependencies after this point will
591 // already have been met.
592 mSlots[slot].mFence = fence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700593 }
Brian Anderson7b097e22017-08-08 16:31:37 -0700594 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
Jamie Gennisb2725412012-09-05 20:09:05 -0700595
596 return OK;
597}
598
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700599status_t ConsumerBase::releaseBufferLocked(
600 int slot, const sp<GraphicBuffer> graphicBuffer,
601 EGLDisplay display, EGLSyncKHR eglFence) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700602 if (mAbandoned) {
603 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
604 return NO_INIT;
605 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700606 // If consumer no longer tracks this graphicBuffer (we received a new
607 // buffer on the same slot), the buffer producer is definitely no longer
608 // tracking it.
609 if (!stillTracking(slot, graphicBuffer)) {
610 return OK;
611 }
612
Mark Salyzyn91100452014-06-09 14:27:45 -0700613 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700614 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700615 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700616 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800617 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700618 freeBufferLocked(slot);
619 }
620
Brian Anderson3546a3f2016-07-14 11:51:14 -0700621 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800622 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700623
624 return err;
625}
626
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700627bool ConsumerBase::stillTracking(int slot,
628 const sp<GraphicBuffer> graphicBuffer) {
629 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
630 return false;
631 }
Yi Kong48a619f2018-06-05 16:34:59 -0700632 return (mSlots[slot].mGraphicBuffer != nullptr &&
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700633 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
634}
635
Andy McFadden2adaf042012-12-18 09:49:45 -0800636} // namespace android