blob: 602bba8daba864205db97e3c83e0bc41ec0eeac4 [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
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700257void ConsumerBase::abandon() {
258 CB_LOGV("abandon");
259 Mutex::Autolock lock(mMutex);
260
261 if (!mAbandoned) {
262 abandonLocked();
263 mAbandoned = true;
264 }
265}
266
267void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800268 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700269 if (mAbandoned) {
270 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
271 return;
272 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700273 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
274 freeBufferLocked(i);
275 }
276 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700277 mConsumer->consumerDisconnect();
278 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700279}
280
John Recke4783052015-05-14 15:55:11 -0700281bool ConsumerBase::isAbandoned() {
282 Mutex::Autolock _l(mMutex);
283 return mAbandoned;
284}
285
Chia-I Wua81bc492017-11-27 10:16:00 -0800286void ConsumerBase::setName(const String8& name) {
287 Mutex::Autolock _l(mMutex);
288 if (mAbandoned) {
289 CB_LOGE("setName: ConsumerBase is abandoned!");
290 return;
291 }
292 mName = name;
293 mConsumer->setConsumerName(name);
294}
295
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700296void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700297 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700298 CB_LOGV("setFrameAvailableListener");
Dan Stoza95971c82017-06-26 14:27:18 -0700299 Mutex::Autolock lock(mFrameAvailableMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700300 mFrameAvailableListener = listener;
301}
302
Dan Stoza634f5ee2015-04-03 14:22:05 -0700303status_t ConsumerBase::detachBuffer(int slot) {
304 CB_LOGV("detachBuffer");
305 Mutex::Autolock lock(mMutex);
306
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700307 if (mAbandoned) {
308 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
309 return NO_INIT;
310 }
311
Jim Shargo28e9d652024-07-10 23:50:00 +0000312 return detachBufferLocked(slot);
313}
314
315#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
316status_t ConsumerBase::detachBuffer(const sp<GraphicBuffer>& buffer) {
317 CB_LOGV("detachBuffer");
318 Mutex::Autolock lock(mMutex);
319
320 if (mAbandoned) {
321 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
322 return NO_INIT;
323 }
324 if (buffer == nullptr) {
325 return BAD_VALUE;
Dan Stoza634f5ee2015-04-03 14:22:05 -0700326 }
327
Jim Shargo28e9d652024-07-10 23:50:00 +0000328 int slotIndex = getSlotForBufferLocked(buffer);
329 if (slotIndex == BufferQueue::INVALID_BUFFER_SLOT) {
330 return BAD_VALUE;
331 }
Dan Stoza634f5ee2015-04-03 14:22:05 -0700332
Jim Shargo28e9d652024-07-10 23:50:00 +0000333 return detachBufferLocked(slotIndex);
Dan Stoza634f5ee2015-04-03 14:22:05 -0700334}
Jim Shargo28e9d652024-07-10 23:50:00 +0000335#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_PLATFORM_API_IMPROVEMENTS)
Dan Stoza634f5ee2015-04-03 14:22:05 -0700336
Michael Lentine847f11e2015-05-18 13:41:23 -0700337status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
338 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700339 if (mAbandoned) {
340 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
341 return NO_INIT;
342 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700343 return mConsumer->setDefaultBufferSize(width, height);
344}
345
346status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
347 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700348 if (mAbandoned) {
349 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
350 return NO_INIT;
351 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700352 return mConsumer->setDefaultBufferFormat(defaultFormat);
353}
354
355status_t ConsumerBase::setDefaultBufferDataSpace(
356 android_dataspace defaultDataSpace) {
357 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700358 if (mAbandoned) {
359 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
360 return NO_INIT;
361 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700362 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
363}
364
Chia-I Wua81bc492017-11-27 10:16:00 -0800365status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
366 Mutex::Autolock lock(mMutex);
367 if (mAbandoned) {
368 CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
369 return NO_INIT;
370 }
371 return mConsumer->setConsumerUsageBits(usage);
372}
373
374status_t ConsumerBase::setTransformHint(uint32_t hint) {
375 Mutex::Autolock lock(mMutex);
376 if (mAbandoned) {
377 CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
378 return NO_INIT;
379 }
380 return mConsumer->setTransformHint(hint);
381}
382
Jim Shargod30823a2024-07-27 02:49:39 +0000383#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
384status_t ConsumerBase::setMaxBufferCount(int bufferCount) {
385 Mutex::Autolock lock(mMutex);
386 if (mAbandoned) {
387 CB_LOGE("setMaxBufferCount: ConsumerBase is abandoned!");
388 return NO_INIT;
389 }
390 return mConsumer->setMaxBufferCount(bufferCount);
391}
392#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
393
Chia-I Wua81bc492017-11-27 10:16:00 -0800394status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
395 Mutex::Autolock lock(mMutex);
396 if (mAbandoned) {
397 CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
398 return NO_INIT;
399 }
400 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
401}
402
Jim Shargod30823a2024-07-27 02:49:39 +0000403#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
404status_t ConsumerBase::setConsumerIsProtected(bool isProtected) {
405 Mutex::Autolock lock(mMutex);
406 if (mAbandoned) {
407 CB_LOGE("setConsumerIsProtected: ConsumerBase is abandoned!");
408 return NO_INIT;
409 }
410 return mConsumer->setConsumerIsProtected(isProtected);
411}
412#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
413
Chia-I Wua81bc492017-11-27 10:16:00 -0800414sp<NativeHandle> ConsumerBase::getSidebandStream() const {
415 Mutex::Autolock _l(mMutex);
416 if (mAbandoned) {
417 CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
418 return nullptr;
419 }
420
421 sp<NativeHandle> stream;
422 status_t err = mConsumer->getSidebandStream(&stream);
423 if (err != NO_ERROR) {
424 CB_LOGE("failed to get sideband stream: %d", err);
425 return nullptr;
426 }
427
428 return stream;
429}
430
Dan Stozae77c7662016-05-13 11:37:28 -0700431status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
432 std::vector<OccupancyTracker::Segment>* outHistory) {
433 Mutex::Autolock _l(mMutex);
434 if (mAbandoned) {
435 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
436 return NO_INIT;
437 }
438 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
439}
440
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700441status_t ConsumerBase::discardFreeBuffers() {
442 Mutex::Autolock _l(mMutex);
443 if (mAbandoned) {
444 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
445 return NO_INIT;
446 }
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700447 status_t err = mConsumer->discardFreeBuffers();
448 if (err != OK) {
449 return err;
450 }
451 uint64_t mask;
452 mConsumer->getReleasedBuffers(&mask);
453 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
454 if (mask & (1ULL << i)) {
455 freeBufferLocked(i);
456 }
457 }
458 return OK;
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700459}
460
Colin Crossdc782512016-09-26 18:10:16 -0700461void ConsumerBase::dumpState(String8& result) const {
462 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700463}
464
Colin Crossdc782512016-09-26 18:10:16 -0700465void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700466 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200467 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700468}
469
Mathias Agopian74d211a2013-04-22 16:55:35 +0200470void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
471 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700472
473 if (!mAbandoned) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700474 String8 consumerState;
475 mConsumer->dumpState(String8(prefix), &consumerState);
476 result.append(consumerState);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700477 }
478}
479
Jim Shargod30823a2024-07-27 02:49:39 +0000480#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
481sp<Surface> ConsumerBase::getSurface() const {
482 LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
483 "It's illegal to get the surface of a Consumer that does not own it. This "
484 "should be impossible once the old CTOR is removed.");
485 return mSurface;
486}
487
488sp<IGraphicBufferConsumer> ConsumerBase::getIGraphicBufferConsumer() const {
489 return mConsumer;
490}
491#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
492
Dan Stozacf3834d2015-03-11 14:04:22 -0700493status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700494 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700495 if (mAbandoned) {
496 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
497 return NO_INIT;
498 }
499
Dan Stozaa4650a52015-05-12 12:56:16 -0700500 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700501 if (err != NO_ERROR) {
502 return err;
503 }
504
Yi Kong48a619f2018-06-05 16:34:59 -0700505 if (item->mGraphicBuffer != nullptr) {
506 if (mSlots[item->mSlot].mGraphicBuffer != nullptr) {
Yin-Chia Yeh723c4892017-03-30 13:13:36 -0700507 freeBufferLocked(item->mSlot);
508 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700509 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700510 }
511
Pablo Ceballos47650f42015-08-04 16:38:17 -0700512 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
513 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700514
Mark Salyzyn91100452014-06-09 14:27:45 -0700515 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700516 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700517
518 return OK;
519}
520
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700521status_t ConsumerBase::addReleaseFence(int slot,
522 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700523 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700524 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700525}
526
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700527status_t ConsumerBase::addReleaseFenceLocked(int slot,
528 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700529 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700530
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700531 // If consumer no longer tracks this graphicBuffer, we can safely
532 // drop this fence, as it will never be received by the producer.
533 if (!stillTracking(slot, graphicBuffer)) {
534 return OK;
535 }
536
Jamie Gennisb2725412012-09-05 20:09:05 -0700537 if (!mSlots[slot].mFence.get()) {
538 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700539 return OK;
540 }
541
Brian Anderson7b097e22017-08-08 16:31:37 -0700542 // Check status of fences first because merging is expensive.
543 // Merging an invalid fence with any other fence results in an
544 // invalid fence.
545 auto currentStatus = mSlots[slot].mFence->getStatus();
546 if (currentStatus == Fence::Status::Invalid) {
547 CB_LOGE("Existing fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700548 return BAD_VALUE;
549 }
550
Brian Anderson7b097e22017-08-08 16:31:37 -0700551 auto incomingStatus = fence->getStatus();
552 if (incomingStatus == Fence::Status::Invalid) {
553 CB_LOGE("New fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700554 mSlots[slot].mFence = fence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700555 return BAD_VALUE;
556 }
557
558 // If both fences are signaled or both are unsignaled, we need to merge
559 // them to get an accurate timestamp.
560 if (currentStatus == incomingStatus) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700561 char fenceName[32] = {};
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000562 snprintf(fenceName, 32, "%.28s:%d", mName.c_str(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700563 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700564 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700565 if (!mergedFence.get()) {
566 CB_LOGE("failed to merge release fences");
567 // synchronization is broken, the best we can do is hope fences
568 // signal in order so the new fence will act like a union
569 mSlots[slot].mFence = fence;
570 return BAD_VALUE;
571 }
572 mSlots[slot].mFence = mergedFence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700573 } else if (incomingStatus == Fence::Status::Unsignaled) {
574 // If one fence has signaled and the other hasn't, the unsignaled
575 // fence will approximately correspond with the correct timestamp.
576 // There's a small race if both fences signal at about the same time
577 // and their statuses are retrieved with unfortunate timing. However,
578 // by this point, they will have both signaled and only the timestamp
579 // will be slightly off; any dependencies after this point will
580 // already have been met.
581 mSlots[slot].mFence = fence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700582 }
Brian Anderson7b097e22017-08-08 16:31:37 -0700583 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
Jamie Gennisb2725412012-09-05 20:09:05 -0700584
585 return OK;
586}
587
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700588status_t ConsumerBase::releaseBufferLocked(
589 int slot, const sp<GraphicBuffer> graphicBuffer,
590 EGLDisplay display, EGLSyncKHR eglFence) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700591 if (mAbandoned) {
592 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
593 return NO_INIT;
594 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700595 // If consumer no longer tracks this graphicBuffer (we received a new
596 // buffer on the same slot), the buffer producer is definitely no longer
597 // tracking it.
598 if (!stillTracking(slot, graphicBuffer)) {
599 return OK;
600 }
601
Mark Salyzyn91100452014-06-09 14:27:45 -0700602 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700603 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700604 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700605 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800606 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700607 freeBufferLocked(slot);
608 }
609
Brian Anderson3546a3f2016-07-14 11:51:14 -0700610 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800611 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700612
613 return err;
614}
615
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700616bool ConsumerBase::stillTracking(int slot,
617 const sp<GraphicBuffer> graphicBuffer) {
618 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
619 return false;
620 }
Yi Kong48a619f2018-06-05 16:34:59 -0700621 return (mSlots[slot].mGraphicBuffer != nullptr &&
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700622 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
623}
624
Andy McFadden2adaf042012-12-18 09:49:45 -0800625} // namespace android