blob: 1e94cc13cda652becfc2907be3ececaa764703c5 [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
Mark Salyzyn91100452014-06-09 14:27:45 -070017#include <inttypes.h>
18
Jamie Gennis1a4d8832012-08-02 20:11:05 -070019#define LOG_TAG "ConsumerBase"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Jamie Gennis1a4d8832012-08-02 20:11:05 -070023#define EGL_EGLEXT_PROTOTYPES
24
25#include <EGL/egl.h>
26#include <EGL/eglext.h>
27
28#include <hardware/hardware.h>
29
Mathias Agopiane9e9fe42017-02-28 16:25:16 -080030#include <cutils/atomic.h>
31
Dan Stozacf3834d2015-03-11 14:04:22 -070032#include <gui/BufferItem.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070033#include <gui/ISurfaceComposer.h>
34#include <gui/SurfaceComposerClient.h>
35#include <gui/ConsumerBase.h>
36
37#include <private/gui/ComposerService.h>
38
39#include <utils/Log.h>
40#include <utils/String8.h>
41#include <utils/Trace.h>
42
43// Macros for including the ConsumerBase name in log messages
Dan Albert8b491252014-09-08 18:53:39 -070044#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
Dan Stoza3be1c6b2014-11-18 10:24:03 -080045//#define CB_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
46//#define CB_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
47//#define CB_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
Dan Albert8b491252014-09-08 18:53:39 -070048#define CB_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Jamie Gennis1a4d8832012-08-02 20:11:05 -070049
50namespace android {
51
52// Get an ID that's unique within this process.
53static int32_t createProcessUniqueId() {
54 static volatile int32_t globalCounter = 0;
55 return android_atomic_inc(&globalCounter);
56}
57
Mathias Agopiandb89edc2013-08-02 01:40:18 -070058ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070059 mAbandoned(false),
Brian Anderson3546a3f2016-07-14 11:51:14 -070060 mConsumer(bufferQueue),
61 mPrevFinalReleaseFence(Fence::NO_FENCE) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070062 // Choose a name using the PID and a process-unique ID.
63 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
64
65 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
66 // reference once the ctor ends, as that would cause the refcount of 'this'
67 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
68 // that's what we create.
Mathias Agopiana4e19522013-07-31 20:09:53 -070069 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
70 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070071
Mathias Agopiandb89edc2013-08-02 01:40:18 -070072 status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070073 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -080074 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -070075 strerror(-err), err);
76 } else {
Mathias Agopiandb89edc2013-08-02 01:40:18 -070077 mConsumer->setConsumerName(mName);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070078 }
79}
80
81ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -070082 CB_LOGV("~ConsumerBase");
Pablo Ceballos22b57022016-02-19 17:41:54 -080083 Mutex::Autolock lock(mMutex);
Pablo Ceballose07e3e52016-03-15 15:07:54 -070084
Jamie Gennisad669b02013-04-05 16:41:27 -070085 // Verify that abandon() has been called before we get here. This should
86 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
87 // derived class to override that method and not call
88 // ConsumerBase::onLastStrongRef().
89 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
90 "consumer is not abandoned!", mName.string());
91}
92
Igor Murashkin7d2d1602013-11-12 18:02:20 -080093void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070094 abandon();
95}
96
97void ConsumerBase::freeBufferLocked(int slotIndex) {
98 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Yi Kong48a619f2018-06-05 16:34:59 -070099 mSlots[slotIndex].mGraphicBuffer = nullptr;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800100 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700101 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700102}
103
Dan Stoza8dc55392014-11-04 11:37:46 -0800104void ConsumerBase::onFrameAvailable(const BufferItem& item) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700105 CB_LOGV("onFrameAvailable");
106
107 sp<FrameAvailableListener> listener;
108 { // scope for the lock
Dan Stoza95971c82017-06-26 14:27:18 -0700109 Mutex::Autolock lock(mFrameAvailableMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700110 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700111 }
112
Yi Kong48a619f2018-06-05 16:34:59 -0700113 if (listener != nullptr) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700114 CB_LOGV("actually calling onFrameAvailable");
Dan Stoza8dc55392014-11-04 11:37:46 -0800115 listener->onFrameAvailable(item);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700116 }
117}
118
Dan Stozadc13c5b2015-05-11 15:33:01 -0700119void ConsumerBase::onFrameReplaced(const BufferItem &item) {
120 CB_LOGV("onFrameReplaced");
121
122 sp<FrameAvailableListener> listener;
123 {
Dan Stoza95971c82017-06-26 14:27:18 -0700124 Mutex::Autolock lock(mFrameAvailableMutex);
Dan Stozadc13c5b2015-05-11 15:33:01 -0700125 listener = mFrameAvailableListener.promote();
126 }
127
Yi Kong48a619f2018-06-05 16:34:59 -0700128 if (listener != nullptr) {
Dan Stozadc13c5b2015-05-11 15:33:01 -0700129 CB_LOGV("actually calling onFrameReplaced");
130 listener->onFrameReplaced(item);
131 }
132}
133
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700134void ConsumerBase::onBufferAllocated(const BufferItem& /*item*/) {}
135
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800137 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700138
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800139 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700140
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800141 if (mAbandoned) {
142 // Nothing to do if we're already abandoned.
143 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700144 }
145
Dan Stozafebd4f42014-04-09 16:14:51 -0700146 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700147 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700148 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700149 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800150 freeBufferLocked(i);
151 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700152 }
153}
154
Jesse Hall399184a2014-03-03 15:42:54 -0800155void ConsumerBase::onSidebandStreamChanged() {
156}
157
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700158void ConsumerBase::abandon() {
159 CB_LOGV("abandon");
160 Mutex::Autolock lock(mMutex);
161
162 if (!mAbandoned) {
163 abandonLocked();
164 mAbandoned = true;
165 }
166}
167
168void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800169 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700170 if (mAbandoned) {
171 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
172 return;
173 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700174 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
175 freeBufferLocked(i);
176 }
177 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700178 mConsumer->consumerDisconnect();
179 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700180}
181
John Recke4783052015-05-14 15:55:11 -0700182bool ConsumerBase::isAbandoned() {
183 Mutex::Autolock _l(mMutex);
184 return mAbandoned;
185}
186
Chia-I Wua81bc492017-11-27 10:16:00 -0800187void ConsumerBase::setName(const String8& name) {
188 Mutex::Autolock _l(mMutex);
189 if (mAbandoned) {
190 CB_LOGE("setName: ConsumerBase is abandoned!");
191 return;
192 }
193 mName = name;
194 mConsumer->setConsumerName(name);
195}
196
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700197void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700198 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700199 CB_LOGV("setFrameAvailableListener");
Dan Stoza95971c82017-06-26 14:27:18 -0700200 Mutex::Autolock lock(mFrameAvailableMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700201 mFrameAvailableListener = listener;
202}
203
Dan Stoza634f5ee2015-04-03 14:22:05 -0700204status_t ConsumerBase::detachBuffer(int slot) {
205 CB_LOGV("detachBuffer");
206 Mutex::Autolock lock(mMutex);
207
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700208 if (mAbandoned) {
209 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
210 return NO_INIT;
211 }
212
Dan Stoza634f5ee2015-04-03 14:22:05 -0700213 status_t result = mConsumer->detachBuffer(slot);
214 if (result != NO_ERROR) {
215 CB_LOGE("Failed to detach buffer: %d", result);
216 return result;
217 }
218
219 freeBufferLocked(slot);
220
221 return result;
222}
223
Michael Lentine847f11e2015-05-18 13:41:23 -0700224status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
225 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700226 if (mAbandoned) {
227 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
228 return NO_INIT;
229 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700230 return mConsumer->setDefaultBufferSize(width, height);
231}
232
233status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
234 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700235 if (mAbandoned) {
236 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
237 return NO_INIT;
238 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700239 return mConsumer->setDefaultBufferFormat(defaultFormat);
240}
241
242status_t ConsumerBase::setDefaultBufferDataSpace(
243 android_dataspace defaultDataSpace) {
244 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700245 if (mAbandoned) {
246 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
247 return NO_INIT;
248 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700249 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
250}
251
Chia-I Wua81bc492017-11-27 10:16:00 -0800252status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
253 Mutex::Autolock lock(mMutex);
254 if (mAbandoned) {
255 CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
256 return NO_INIT;
257 }
258 return mConsumer->setConsumerUsageBits(usage);
259}
260
261status_t ConsumerBase::setTransformHint(uint32_t hint) {
262 Mutex::Autolock lock(mMutex);
263 if (mAbandoned) {
264 CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
265 return NO_INIT;
266 }
267 return mConsumer->setTransformHint(hint);
268}
269
270status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
271 Mutex::Autolock lock(mMutex);
272 if (mAbandoned) {
273 CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
274 return NO_INIT;
275 }
276 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
277}
278
279sp<NativeHandle> ConsumerBase::getSidebandStream() const {
280 Mutex::Autolock _l(mMutex);
281 if (mAbandoned) {
282 CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
283 return nullptr;
284 }
285
286 sp<NativeHandle> stream;
287 status_t err = mConsumer->getSidebandStream(&stream);
288 if (err != NO_ERROR) {
289 CB_LOGE("failed to get sideband stream: %d", err);
290 return nullptr;
291 }
292
293 return stream;
294}
295
Dan Stozae77c7662016-05-13 11:37:28 -0700296status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
297 std::vector<OccupancyTracker::Segment>* outHistory) {
298 Mutex::Autolock _l(mMutex);
299 if (mAbandoned) {
300 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
301 return NO_INIT;
302 }
303 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
304}
305
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700306status_t ConsumerBase::discardFreeBuffers() {
307 Mutex::Autolock _l(mMutex);
308 if (mAbandoned) {
309 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
310 return NO_INIT;
311 }
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700312 status_t err = mConsumer->discardFreeBuffers();
313 if (err != OK) {
314 return err;
315 }
316 uint64_t mask;
317 mConsumer->getReleasedBuffers(&mask);
318 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
319 if (mask & (1ULL << i)) {
320 freeBufferLocked(i);
321 }
322 }
323 return OK;
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700324}
325
Colin Crossdc782512016-09-26 18:10:16 -0700326void ConsumerBase::dumpState(String8& result) const {
327 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700328}
329
Colin Crossdc782512016-09-26 18:10:16 -0700330void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700331 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200332 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700333}
334
Mathias Agopian74d211a2013-04-22 16:55:35 +0200335void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
336 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700337
338 if (!mAbandoned) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700339 String8 consumerState;
340 mConsumer->dumpState(String8(prefix), &consumerState);
341 result.append(consumerState);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700342 }
343}
344
Dan Stozacf3834d2015-03-11 14:04:22 -0700345status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700346 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700347 if (mAbandoned) {
348 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
349 return NO_INIT;
350 }
351
Dan Stozaa4650a52015-05-12 12:56:16 -0700352 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700353 if (err != NO_ERROR) {
354 return err;
355 }
356
Yi Kong48a619f2018-06-05 16:34:59 -0700357 if (item->mGraphicBuffer != nullptr) {
358 if (mSlots[item->mSlot].mGraphicBuffer != nullptr) {
Yin-Chia Yeh723c4892017-03-30 13:13:36 -0700359 freeBufferLocked(item->mSlot);
360 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700361 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700362 }
363
Pablo Ceballos47650f42015-08-04 16:38:17 -0700364 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
365 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700366
Mark Salyzyn91100452014-06-09 14:27:45 -0700367 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700368 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700369
370 return OK;
371}
372
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700373status_t ConsumerBase::addReleaseFence(int slot,
374 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700375 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700376 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700377}
378
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700379status_t ConsumerBase::addReleaseFenceLocked(int slot,
380 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700381 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700382
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700383 // If consumer no longer tracks this graphicBuffer, we can safely
384 // drop this fence, as it will never be received by the producer.
385 if (!stillTracking(slot, graphicBuffer)) {
386 return OK;
387 }
388
Jamie Gennisb2725412012-09-05 20:09:05 -0700389 if (!mSlots[slot].mFence.get()) {
390 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700391 return OK;
392 }
393
Brian Anderson7b097e22017-08-08 16:31:37 -0700394 // Check status of fences first because merging is expensive.
395 // Merging an invalid fence with any other fence results in an
396 // invalid fence.
397 auto currentStatus = mSlots[slot].mFence->getStatus();
398 if (currentStatus == Fence::Status::Invalid) {
399 CB_LOGE("Existing fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700400 return BAD_VALUE;
401 }
402
Brian Anderson7b097e22017-08-08 16:31:37 -0700403 auto incomingStatus = fence->getStatus();
404 if (incomingStatus == Fence::Status::Invalid) {
405 CB_LOGE("New fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700406 mSlots[slot].mFence = fence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700407 return BAD_VALUE;
408 }
409
410 // If both fences are signaled or both are unsignaled, we need to merge
411 // them to get an accurate timestamp.
412 if (currentStatus == incomingStatus) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700413 char fenceName[32] = {};
414 snprintf(fenceName, 32, "%.28s:%d", mName.string(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700415 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700416 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700417 if (!mergedFence.get()) {
418 CB_LOGE("failed to merge release fences");
419 // synchronization is broken, the best we can do is hope fences
420 // signal in order so the new fence will act like a union
421 mSlots[slot].mFence = fence;
422 return BAD_VALUE;
423 }
424 mSlots[slot].mFence = mergedFence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700425 } else if (incomingStatus == Fence::Status::Unsignaled) {
426 // If one fence has signaled and the other hasn't, the unsignaled
427 // fence will approximately correspond with the correct timestamp.
428 // There's a small race if both fences signal at about the same time
429 // and their statuses are retrieved with unfortunate timing. However,
430 // by this point, they will have both signaled and only the timestamp
431 // will be slightly off; any dependencies after this point will
432 // already have been met.
433 mSlots[slot].mFence = fence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700434 }
Brian Anderson7b097e22017-08-08 16:31:37 -0700435 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
Jamie Gennisb2725412012-09-05 20:09:05 -0700436
437 return OK;
438}
439
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700440status_t ConsumerBase::releaseBufferLocked(
441 int slot, const sp<GraphicBuffer> graphicBuffer,
442 EGLDisplay display, EGLSyncKHR eglFence) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700443 if (mAbandoned) {
444 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
445 return NO_INIT;
446 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700447 // If consumer no longer tracks this graphicBuffer (we received a new
448 // buffer on the same slot), the buffer producer is definitely no longer
449 // tracking it.
450 if (!stillTracking(slot, graphicBuffer)) {
451 return OK;
452 }
453
Mark Salyzyn91100452014-06-09 14:27:45 -0700454 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700455 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700456 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700457 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800458 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700459 freeBufferLocked(slot);
460 }
461
Brian Anderson3546a3f2016-07-14 11:51:14 -0700462 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800463 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700464
465 return err;
466}
467
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700468bool ConsumerBase::stillTracking(int slot,
469 const sp<GraphicBuffer> graphicBuffer) {
470 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
471 return false;
472 }
Yi Kong48a619f2018-06-05 16:34:59 -0700473 return (mSlots[slot].mGraphicBuffer != nullptr &&
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700474 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
475}
476
Andy McFadden2adaf042012-12-18 09:49:45 -0800477} // namespace android