blob: f9e292e19920705df9186bfdd9f97272f68b70d2 [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);
99 mSlots[slotIndex].mGraphicBuffer = 0;
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
113 if (listener != NULL) {
114 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
128 if (listener != NULL) {
129 CB_LOGV("actually calling onFrameReplaced");
130 listener->onFrameReplaced(item);
131 }
132}
133
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800135 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800137 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700138
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800139 if (mAbandoned) {
140 // Nothing to do if we're already abandoned.
141 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700142 }
143
Dan Stozafebd4f42014-04-09 16:14:51 -0700144 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700145 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700146 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700147 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800148 freeBufferLocked(i);
149 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700150 }
151}
152
Jesse Hall399184a2014-03-03 15:42:54 -0800153void ConsumerBase::onSidebandStreamChanged() {
154}
155
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700156void ConsumerBase::abandon() {
157 CB_LOGV("abandon");
158 Mutex::Autolock lock(mMutex);
159
160 if (!mAbandoned) {
161 abandonLocked();
162 mAbandoned = true;
163 }
164}
165
166void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800167 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700168 if (mAbandoned) {
169 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
170 return;
171 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700172 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
173 freeBufferLocked(i);
174 }
175 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700176 mConsumer->consumerDisconnect();
177 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700178}
179
John Recke4783052015-05-14 15:55:11 -0700180bool ConsumerBase::isAbandoned() {
181 Mutex::Autolock _l(mMutex);
182 return mAbandoned;
183}
184
Chia-I Wua81bc492017-11-27 10:16:00 -0800185void ConsumerBase::setName(const String8& name) {
186 Mutex::Autolock _l(mMutex);
187 if (mAbandoned) {
188 CB_LOGE("setName: ConsumerBase is abandoned!");
189 return;
190 }
191 mName = name;
192 mConsumer->setConsumerName(name);
193}
194
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700195void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700196 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700197 CB_LOGV("setFrameAvailableListener");
Dan Stoza95971c82017-06-26 14:27:18 -0700198 Mutex::Autolock lock(mFrameAvailableMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700199 mFrameAvailableListener = listener;
200}
201
Dan Stoza634f5ee2015-04-03 14:22:05 -0700202status_t ConsumerBase::detachBuffer(int slot) {
203 CB_LOGV("detachBuffer");
204 Mutex::Autolock lock(mMutex);
205
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700206 if (mAbandoned) {
207 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
208 return NO_INIT;
209 }
210
Dan Stoza634f5ee2015-04-03 14:22:05 -0700211 status_t result = mConsumer->detachBuffer(slot);
212 if (result != NO_ERROR) {
213 CB_LOGE("Failed to detach buffer: %d", result);
214 return result;
215 }
216
217 freeBufferLocked(slot);
218
219 return result;
220}
221
Michael Lentine847f11e2015-05-18 13:41:23 -0700222status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
223 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700224 if (mAbandoned) {
225 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
226 return NO_INIT;
227 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700228 return mConsumer->setDefaultBufferSize(width, height);
229}
230
231status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
232 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700233 if (mAbandoned) {
234 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
235 return NO_INIT;
236 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700237 return mConsumer->setDefaultBufferFormat(defaultFormat);
238}
239
240status_t ConsumerBase::setDefaultBufferDataSpace(
241 android_dataspace defaultDataSpace) {
242 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700243 if (mAbandoned) {
244 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
245 return NO_INIT;
246 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700247 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
248}
249
Chia-I Wua81bc492017-11-27 10:16:00 -0800250status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
251 Mutex::Autolock lock(mMutex);
252 if (mAbandoned) {
253 CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
254 return NO_INIT;
255 }
256 return mConsumer->setConsumerUsageBits(usage);
257}
258
259status_t ConsumerBase::setTransformHint(uint32_t hint) {
260 Mutex::Autolock lock(mMutex);
261 if (mAbandoned) {
262 CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
263 return NO_INIT;
264 }
265 return mConsumer->setTransformHint(hint);
266}
267
268status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
269 Mutex::Autolock lock(mMutex);
270 if (mAbandoned) {
271 CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
272 return NO_INIT;
273 }
274 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
275}
276
277sp<NativeHandle> ConsumerBase::getSidebandStream() const {
278 Mutex::Autolock _l(mMutex);
279 if (mAbandoned) {
280 CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
281 return nullptr;
282 }
283
284 sp<NativeHandle> stream;
285 status_t err = mConsumer->getSidebandStream(&stream);
286 if (err != NO_ERROR) {
287 CB_LOGE("failed to get sideband stream: %d", err);
288 return nullptr;
289 }
290
291 return stream;
292}
293
Dan Stozae77c7662016-05-13 11:37:28 -0700294status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
295 std::vector<OccupancyTracker::Segment>* outHistory) {
296 Mutex::Autolock _l(mMutex);
297 if (mAbandoned) {
298 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
299 return NO_INIT;
300 }
301 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
302}
303
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700304status_t ConsumerBase::discardFreeBuffers() {
305 Mutex::Autolock _l(mMutex);
306 if (mAbandoned) {
307 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
308 return NO_INIT;
309 }
Eino-Ville Talvalac6ff7982017-06-13 17:09:11 -0700310 status_t err = mConsumer->discardFreeBuffers();
311 if (err != OK) {
312 return err;
313 }
314 uint64_t mask;
315 mConsumer->getReleasedBuffers(&mask);
316 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
317 if (mask & (1ULL << i)) {
318 freeBufferLocked(i);
319 }
320 }
321 return OK;
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700322}
323
Colin Crossdc782512016-09-26 18:10:16 -0700324void ConsumerBase::dumpState(String8& result) const {
325 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700326}
327
Colin Crossdc782512016-09-26 18:10:16 -0700328void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700329 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200330 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700331}
332
Mathias Agopian74d211a2013-04-22 16:55:35 +0200333void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
334 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700335
336 if (!mAbandoned) {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700337 String8 consumerState;
338 mConsumer->dumpState(String8(prefix), &consumerState);
339 result.append(consumerState);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700340 }
341}
342
Dan Stozacf3834d2015-03-11 14:04:22 -0700343status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700344 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700345 if (mAbandoned) {
346 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
347 return NO_INIT;
348 }
349
Dan Stozaa4650a52015-05-12 12:56:16 -0700350 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700351 if (err != NO_ERROR) {
352 return err;
353 }
354
355 if (item->mGraphicBuffer != NULL) {
Yin-Chia Yeh723c4892017-03-30 13:13:36 -0700356 if (mSlots[item->mSlot].mGraphicBuffer != NULL) {
357 freeBufferLocked(item->mSlot);
358 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700359 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700360 }
361
Pablo Ceballos47650f42015-08-04 16:38:17 -0700362 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
363 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700364
Mark Salyzyn91100452014-06-09 14:27:45 -0700365 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700366 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700367
368 return OK;
369}
370
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700371status_t ConsumerBase::addReleaseFence(int slot,
372 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700373 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700374 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700375}
376
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700377status_t ConsumerBase::addReleaseFenceLocked(int slot,
378 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700379 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700380
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700381 // If consumer no longer tracks this graphicBuffer, we can safely
382 // drop this fence, as it will never be received by the producer.
383 if (!stillTracking(slot, graphicBuffer)) {
384 return OK;
385 }
386
Jamie Gennisb2725412012-09-05 20:09:05 -0700387 if (!mSlots[slot].mFence.get()) {
388 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700389 return OK;
390 }
391
Brian Anderson7b097e22017-08-08 16:31:37 -0700392 // Check status of fences first because merging is expensive.
393 // Merging an invalid fence with any other fence results in an
394 // invalid fence.
395 auto currentStatus = mSlots[slot].mFence->getStatus();
396 if (currentStatus == Fence::Status::Invalid) {
397 CB_LOGE("Existing fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700398 return BAD_VALUE;
399 }
400
Brian Anderson7b097e22017-08-08 16:31:37 -0700401 auto incomingStatus = fence->getStatus();
402 if (incomingStatus == Fence::Status::Invalid) {
403 CB_LOGE("New fence has invalid state");
Matthew Bouyack377c2032016-10-07 15:06:15 -0700404 mSlots[slot].mFence = fence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700405 return BAD_VALUE;
406 }
407
408 // If both fences are signaled or both are unsignaled, we need to merge
409 // them to get an accurate timestamp.
410 if (currentStatus == incomingStatus) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700411 char fenceName[32] = {};
412 snprintf(fenceName, 32, "%.28s:%d", mName.string(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700413 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700414 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700415 if (!mergedFence.get()) {
416 CB_LOGE("failed to merge release fences");
417 // synchronization is broken, the best we can do is hope fences
418 // signal in order so the new fence will act like a union
419 mSlots[slot].mFence = fence;
420 return BAD_VALUE;
421 }
422 mSlots[slot].mFence = mergedFence;
Brian Anderson7b097e22017-08-08 16:31:37 -0700423 } else if (incomingStatus == Fence::Status::Unsignaled) {
424 // If one fence has signaled and the other hasn't, the unsignaled
425 // fence will approximately correspond with the correct timestamp.
426 // There's a small race if both fences signal at about the same time
427 // and their statuses are retrieved with unfortunate timing. However,
428 // by this point, they will have both signaled and only the timestamp
429 // will be slightly off; any dependencies after this point will
430 // already have been met.
431 mSlots[slot].mFence = fence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700432 }
Brian Anderson7b097e22017-08-08 16:31:37 -0700433 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
Jamie Gennisb2725412012-09-05 20:09:05 -0700434
435 return OK;
436}
437
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700438status_t ConsumerBase::releaseBufferLocked(
439 int slot, const sp<GraphicBuffer> graphicBuffer,
440 EGLDisplay display, EGLSyncKHR eglFence) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700441 if (mAbandoned) {
442 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
443 return NO_INIT;
444 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700445 // If consumer no longer tracks this graphicBuffer (we received a new
446 // buffer on the same slot), the buffer producer is definitely no longer
447 // tracking it.
448 if (!stillTracking(slot, graphicBuffer)) {
449 return OK;
450 }
451
Mark Salyzyn91100452014-06-09 14:27:45 -0700452 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700453 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700454 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700455 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800456 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700457 freeBufferLocked(slot);
458 }
459
Brian Anderson3546a3f2016-07-14 11:51:14 -0700460 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800461 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700462
463 return err;
464}
465
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700466bool ConsumerBase::stillTracking(int slot,
467 const sp<GraphicBuffer> graphicBuffer) {
468 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
469 return false;
470 }
471 return (mSlots[slot].mGraphicBuffer != NULL &&
472 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
473}
474
Andy McFadden2adaf042012-12-18 09:49:45 -0800475} // namespace android