blob: c26de6673bcdde556fabfc2c9b3da190acd2868b [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
Dan Stozacf3834d2015-03-11 14:04:22 -070030#include <gui/BufferItem.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070031#include <gui/IGraphicBufferAlloc.h>
32#include <gui/ISurfaceComposer.h>
33#include <gui/SurfaceComposerClient.h>
34#include <gui/ConsumerBase.h>
35
36#include <private/gui/ComposerService.h>
37
38#include <utils/Log.h>
39#include <utils/String8.h>
40#include <utils/Trace.h>
41
42// Macros for including the ConsumerBase name in log messages
Dan Albert8b491252014-09-08 18:53:39 -070043#define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
Dan Stoza3be1c6b2014-11-18 10:24:03 -080044//#define CB_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
45//#define CB_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
46//#define CB_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
Dan Albert8b491252014-09-08 18:53:39 -070047#define CB_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Jamie Gennis1a4d8832012-08-02 20:11:05 -070048
49namespace android {
50
51// Get an ID that's unique within this process.
52static int32_t createProcessUniqueId() {
53 static volatile int32_t globalCounter = 0;
54 return android_atomic_inc(&globalCounter);
55}
56
Mathias Agopiandb89edc2013-08-02 01:40:18 -070057ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070058 mAbandoned(false),
Brian Anderson3546a3f2016-07-14 11:51:14 -070059 mConsumer(bufferQueue),
60 mPrevFinalReleaseFence(Fence::NO_FENCE) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070061 // Choose a name using the PID and a process-unique ID.
62 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
63
64 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
65 // reference once the ctor ends, as that would cause the refcount of 'this'
66 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
67 // that's what we create.
Mathias Agopiana4e19522013-07-31 20:09:53 -070068 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
69 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070070
Mathias Agopiandb89edc2013-08-02 01:40:18 -070071 status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070072 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -080073 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -070074 strerror(-err), err);
75 } else {
Mathias Agopiandb89edc2013-08-02 01:40:18 -070076 mConsumer->setConsumerName(mName);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070077 }
78}
79
80ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -070081 CB_LOGV("~ConsumerBase");
Pablo Ceballos22b57022016-02-19 17:41:54 -080082 Mutex::Autolock lock(mMutex);
Pablo Ceballose07e3e52016-03-15 15:07:54 -070083
Jamie Gennisad669b02013-04-05 16:41:27 -070084 // Verify that abandon() has been called before we get here. This should
85 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
86 // derived class to override that method and not call
87 // ConsumerBase::onLastStrongRef().
88 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
89 "consumer is not abandoned!", mName.string());
90}
91
Igor Murashkin7d2d1602013-11-12 18:02:20 -080092void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070093 abandon();
94}
95
96void ConsumerBase::freeBufferLocked(int slotIndex) {
97 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
98 mSlots[slotIndex].mGraphicBuffer = 0;
Jamie Gennis1df8c342012-12-20 14:05:45 -080099 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700100 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700101}
102
Dan Stoza8dc55392014-11-04 11:37:46 -0800103void ConsumerBase::onFrameAvailable(const BufferItem& item) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700104 CB_LOGV("onFrameAvailable");
105
106 sp<FrameAvailableListener> listener;
107 { // scope for the lock
108 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700109 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700110 }
111
112 if (listener != NULL) {
113 CB_LOGV("actually calling onFrameAvailable");
Dan Stoza8dc55392014-11-04 11:37:46 -0800114 listener->onFrameAvailable(item);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700115 }
116}
117
Dan Stozadc13c5b2015-05-11 15:33:01 -0700118void ConsumerBase::onFrameReplaced(const BufferItem &item) {
119 CB_LOGV("onFrameReplaced");
120
121 sp<FrameAvailableListener> listener;
122 {
123 Mutex::Autolock lock(mMutex);
124 listener = mFrameAvailableListener.promote();
125 }
126
127 if (listener != NULL) {
128 CB_LOGV("actually calling onFrameReplaced");
129 listener->onFrameReplaced(item);
130 }
131}
132
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700133void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800134 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700135
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800136 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700137
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800138 if (mAbandoned) {
139 // Nothing to do if we're already abandoned.
140 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700141 }
142
Dan Stozafebd4f42014-04-09 16:14:51 -0700143 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700144 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700145 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700146 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800147 freeBufferLocked(i);
148 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700149 }
150}
151
Jesse Hall399184a2014-03-03 15:42:54 -0800152void ConsumerBase::onSidebandStreamChanged() {
153}
154
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700155void ConsumerBase::abandon() {
156 CB_LOGV("abandon");
157 Mutex::Autolock lock(mMutex);
158
159 if (!mAbandoned) {
160 abandonLocked();
161 mAbandoned = true;
162 }
163}
164
165void ConsumerBase::abandonLocked() {
Brian Carlstrom83b1e682016-03-12 16:07:59 -0800166 CB_LOGV("abandonLocked");
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700167 if (mAbandoned) {
168 CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
169 return;
170 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700171 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
172 freeBufferLocked(i);
173 }
174 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700175 mConsumer->consumerDisconnect();
176 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700177}
178
John Recke4783052015-05-14 15:55:11 -0700179bool ConsumerBase::isAbandoned() {
180 Mutex::Autolock _l(mMutex);
181 return mAbandoned;
182}
183
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700184void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700185 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700186 CB_LOGV("setFrameAvailableListener");
187 Mutex::Autolock lock(mMutex);
188 mFrameAvailableListener = listener;
189}
190
Dan Stoza634f5ee2015-04-03 14:22:05 -0700191status_t ConsumerBase::detachBuffer(int slot) {
192 CB_LOGV("detachBuffer");
193 Mutex::Autolock lock(mMutex);
194
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700195 if (mAbandoned) {
196 CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
197 return NO_INIT;
198 }
199
Dan Stoza634f5ee2015-04-03 14:22:05 -0700200 status_t result = mConsumer->detachBuffer(slot);
201 if (result != NO_ERROR) {
202 CB_LOGE("Failed to detach buffer: %d", result);
203 return result;
204 }
205
206 freeBufferLocked(slot);
207
208 return result;
209}
210
Michael Lentine847f11e2015-05-18 13:41:23 -0700211status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
212 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700213 if (mAbandoned) {
214 CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
215 return NO_INIT;
216 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700217 return mConsumer->setDefaultBufferSize(width, height);
218}
219
220status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
221 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700222 if (mAbandoned) {
223 CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
224 return NO_INIT;
225 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700226 return mConsumer->setDefaultBufferFormat(defaultFormat);
227}
228
229status_t ConsumerBase::setDefaultBufferDataSpace(
230 android_dataspace defaultDataSpace) {
231 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700232 if (mAbandoned) {
233 CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
234 return NO_INIT;
235 }
Michael Lentine847f11e2015-05-18 13:41:23 -0700236 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
237}
238
Dan Stozae77c7662016-05-13 11:37:28 -0700239status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
240 std::vector<OccupancyTracker::Segment>* outHistory) {
241 Mutex::Autolock _l(mMutex);
242 if (mAbandoned) {
243 CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
244 return NO_INIT;
245 }
246 return mConsumer->getOccupancyHistory(forceFlush, outHistory);
247}
248
Eino-Ville Talvalabc2df652016-07-21 17:06:58 -0700249status_t ConsumerBase::discardFreeBuffers() {
250 Mutex::Autolock _l(mMutex);
251 if (mAbandoned) {
252 CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
253 return NO_INIT;
254 }
255 return mConsumer->discardFreeBuffers();
256}
257
Colin Crossdc782512016-09-26 18:10:16 -0700258void ConsumerBase::dumpState(String8& result) const {
259 dumpState(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700260}
261
Colin Crossdc782512016-09-26 18:10:16 -0700262void ConsumerBase::dumpState(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700263 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200264 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700265}
266
Mathias Agopian74d211a2013-04-22 16:55:35 +0200267void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
268 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700269
270 if (!mAbandoned) {
Colin Crossdc782512016-09-26 18:10:16 -0700271 mConsumer->dumpState(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700272 }
273}
274
Dan Stozacf3834d2015-03-11 14:04:22 -0700275status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700276 nsecs_t presentWhen, uint64_t maxFrameNumber) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700277 if (mAbandoned) {
278 CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
279 return NO_INIT;
280 }
281
Dan Stozaa4650a52015-05-12 12:56:16 -0700282 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700283 if (err != NO_ERROR) {
284 return err;
285 }
286
287 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700288 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700289 }
290
Pablo Ceballos47650f42015-08-04 16:38:17 -0700291 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
292 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700293
Mark Salyzyn91100452014-06-09 14:27:45 -0700294 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700295 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700296
297 return OK;
298}
299
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700300status_t ConsumerBase::addReleaseFence(int slot,
301 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700302 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700303 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700304}
305
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700306status_t ConsumerBase::addReleaseFenceLocked(int slot,
307 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700308 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700309
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700310 // If consumer no longer tracks this graphicBuffer, we can safely
311 // drop this fence, as it will never be received by the producer.
312 if (!stillTracking(slot, graphicBuffer)) {
313 return OK;
314 }
315
Jamie Gennisb2725412012-09-05 20:09:05 -0700316 if (!mSlots[slot].mFence.get()) {
317 mSlots[slot].mFence = fence;
Matthew Bouyack377c2032016-10-07 15:06:15 -0700318 return OK;
319 }
320
Dan Stozaa34320a2017-02-16 14:55:03 -0800321 auto status = mSlots[slot].mFence->getStatus();
Matthew Bouyack377c2032016-10-07 15:06:15 -0700322
Dan Stozaa34320a2017-02-16 14:55:03 -0800323 if (status == Fence::Status::Invalid) {
Matthew Bouyack377c2032016-10-07 15:06:15 -0700324 CB_LOGE("fence has invalid state");
325 return BAD_VALUE;
326 }
327
Dan Stozaa34320a2017-02-16 14:55:03 -0800328 if (status == Fence::Status::Signaled) {
Matthew Bouyack377c2032016-10-07 15:06:15 -0700329 mSlots[slot].mFence = fence;
Dan Stozaa34320a2017-02-16 14:55:03 -0800330 } else { // status == Fence::Status::Unsignaled
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700331 char fenceName[32] = {};
332 snprintf(fenceName, 32, "%.28s:%d", mName.string(), slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700333 sp<Fence> mergedFence = Fence::merge(
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700334 fenceName, mSlots[slot].mFence, fence);
Jamie Gennisb2725412012-09-05 20:09:05 -0700335 if (!mergedFence.get()) {
336 CB_LOGE("failed to merge release fences");
337 // synchronization is broken, the best we can do is hope fences
338 // signal in order so the new fence will act like a union
339 mSlots[slot].mFence = fence;
340 return BAD_VALUE;
341 }
342 mSlots[slot].mFence = mergedFence;
343 }
344
345 return OK;
346}
347
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700348status_t ConsumerBase::releaseBufferLocked(
349 int slot, const sp<GraphicBuffer> graphicBuffer,
350 EGLDisplay display, EGLSyncKHR eglFence) {
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700351 if (mAbandoned) {
352 CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
353 return NO_INIT;
354 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700355 // If consumer no longer tracks this graphicBuffer (we received a new
356 // buffer on the same slot), the buffer producer is definitely no longer
357 // tracking it.
358 if (!stillTracking(slot, graphicBuffer)) {
359 return OK;
360 }
361
Mark Salyzyn91100452014-06-09 14:27:45 -0700362 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700363 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700364 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700365 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800366 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700367 freeBufferLocked(slot);
368 }
369
Brian Anderson3546a3f2016-07-14 11:51:14 -0700370 mPrevFinalReleaseFence = mSlots[slot].mFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800371 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700372
373 return err;
374}
375
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700376bool ConsumerBase::stillTracking(int slot,
377 const sp<GraphicBuffer> graphicBuffer) {
378 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
379 return false;
380 }
381 return (mSlots[slot].mGraphicBuffer != NULL &&
382 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
383}
384
Andy McFadden2adaf042012-12-18 09:49:45 -0800385} // namespace android