blob: d01187fbbf2d0a4adabaeac351d1d61f1742a189 [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),
Mathias Agopiandb89edc2013-08-02 01:40:18 -070059 mConsumer(bufferQueue) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070060 // Choose a name using the PID and a process-unique ID.
61 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
62
63 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
64 // reference once the ctor ends, as that would cause the refcount of 'this'
65 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
66 // that's what we create.
Mathias Agopiana4e19522013-07-31 20:09:53 -070067 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
68 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070069
Mathias Agopiandb89edc2013-08-02 01:40:18 -070070 status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070071 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -080072 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -070073 strerror(-err), err);
74 } else {
Mathias Agopiandb89edc2013-08-02 01:40:18 -070075 mConsumer->setConsumerName(mName);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070076 }
77}
78
79ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -070080 CB_LOGV("~ConsumerBase");
81 Mutex::Autolock lock(mMutex);
82
83 // Verify that abandon() has been called before we get here. This should
84 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
85 // derived class to override that method and not call
86 // ConsumerBase::onLastStrongRef().
87 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
88 "consumer is not abandoned!", mName.string());
89}
90
Igor Murashkin7d2d1602013-11-12 18:02:20 -080091void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070092 abandon();
93}
94
95void ConsumerBase::freeBufferLocked(int slotIndex) {
96 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
97 mSlots[slotIndex].mGraphicBuffer = 0;
Jamie Gennis1df8c342012-12-20 14:05:45 -080098 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -070099 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700100}
101
Dan Stoza8dc55392014-11-04 11:37:46 -0800102void ConsumerBase::onFrameAvailable(const BufferItem& item) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700103 CB_LOGV("onFrameAvailable");
104
105 sp<FrameAvailableListener> listener;
106 { // scope for the lock
107 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700108 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700109 }
110
111 if (listener != NULL) {
112 CB_LOGV("actually calling onFrameAvailable");
Dan Stoza8dc55392014-11-04 11:37:46 -0800113 listener->onFrameAvailable(item);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700114 }
115}
116
Dan Stozadc13c5b2015-05-11 15:33:01 -0700117void ConsumerBase::onFrameReplaced(const BufferItem &item) {
118 CB_LOGV("onFrameReplaced");
119
120 sp<FrameAvailableListener> listener;
121 {
122 Mutex::Autolock lock(mMutex);
123 listener = mFrameAvailableListener.promote();
124 }
125
126 if (listener != NULL) {
127 CB_LOGV("actually calling onFrameReplaced");
128 listener->onFrameReplaced(item);
129 }
130}
131
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800133 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800135 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800137 if (mAbandoned) {
138 // Nothing to do if we're already abandoned.
139 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700140 }
141
Dan Stozafebd4f42014-04-09 16:14:51 -0700142 uint64_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700143 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700144 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Dan Stozafebd4f42014-04-09 16:14:51 -0700145 if (mask & (1ULL << i)) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800146 freeBufferLocked(i);
147 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700148 }
149}
150
Jesse Hall399184a2014-03-03 15:42:54 -0800151void ConsumerBase::onSidebandStreamChanged() {
152}
153
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700154void ConsumerBase::abandon() {
155 CB_LOGV("abandon");
156 Mutex::Autolock lock(mMutex);
157
158 if (!mAbandoned) {
159 abandonLocked();
160 mAbandoned = true;
161 }
162}
163
164void ConsumerBase::abandonLocked() {
165 CB_LOGV("abandonLocked");
166 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
167 freeBufferLocked(i);
168 }
169 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700170 mConsumer->consumerDisconnect();
171 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700172}
173
John Recke4783052015-05-14 15:55:11 -0700174bool ConsumerBase::isAbandoned() {
175 Mutex::Autolock _l(mMutex);
176 return mAbandoned;
177}
178
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700179void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700180 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700181 CB_LOGV("setFrameAvailableListener");
182 Mutex::Autolock lock(mMutex);
183 mFrameAvailableListener = listener;
184}
185
Dan Stoza634f5ee2015-04-03 14:22:05 -0700186status_t ConsumerBase::detachBuffer(int slot) {
187 CB_LOGV("detachBuffer");
188 Mutex::Autolock lock(mMutex);
189
190 status_t result = mConsumer->detachBuffer(slot);
191 if (result != NO_ERROR) {
192 CB_LOGE("Failed to detach buffer: %d", result);
193 return result;
194 }
195
196 freeBufferLocked(slot);
197
198 return result;
199}
200
Michael Lentine847f11e2015-05-18 13:41:23 -0700201status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
202 Mutex::Autolock _l(mMutex);
203 return mConsumer->setDefaultBufferSize(width, height);
204}
205
206status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
207 Mutex::Autolock _l(mMutex);
208 return mConsumer->setDefaultBufferFormat(defaultFormat);
209}
210
211status_t ConsumerBase::setDefaultBufferDataSpace(
212 android_dataspace defaultDataSpace) {
213 Mutex::Autolock _l(mMutex);
214 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
215}
216
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700217void ConsumerBase::dump(String8& result) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200218 dump(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700219}
220
Mathias Agopian74d211a2013-04-22 16:55:35 +0200221void ConsumerBase::dump(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700222 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200223 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700224}
225
Mathias Agopian74d211a2013-04-22 16:55:35 +0200226void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
227 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700228
229 if (!mAbandoned) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700230 mConsumer->dump(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700231 }
232}
233
Dan Stozacf3834d2015-03-11 14:04:22 -0700234status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700235 nsecs_t presentWhen, uint64_t maxFrameNumber) {
236 status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700237 if (err != NO_ERROR) {
238 return err;
239 }
240
241 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700242 mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700243 }
244
Pablo Ceballos47650f42015-08-04 16:38:17 -0700245 mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
246 mSlots[item->mSlot].mFence = item->mFence;
Jamie Gennisb2725412012-09-05 20:09:05 -0700247
Mark Salyzyn91100452014-06-09 14:27:45 -0700248 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700249 item->mSlot, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700250
251 return OK;
252}
253
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700254status_t ConsumerBase::addReleaseFence(int slot,
255 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700256 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700257 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700258}
259
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700260status_t ConsumerBase::addReleaseFenceLocked(int slot,
261 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700262 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700263
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700264 // If consumer no longer tracks this graphicBuffer, we can safely
265 // drop this fence, as it will never be received by the producer.
266 if (!stillTracking(slot, graphicBuffer)) {
267 return OK;
268 }
269
Jamie Gennisb2725412012-09-05 20:09:05 -0700270 if (!mSlots[slot].mFence.get()) {
271 mSlots[slot].mFence = fence;
272 } else {
273 sp<Fence> mergedFence = Fence::merge(
Jamie Gennis7aff4a52012-09-24 12:25:15 -0700274 String8::format("%.28s:%d", mName.string(), slot),
Jamie Gennisb2725412012-09-05 20:09:05 -0700275 mSlots[slot].mFence, fence);
276 if (!mergedFence.get()) {
277 CB_LOGE("failed to merge release fences");
278 // synchronization is broken, the best we can do is hope fences
279 // signal in order so the new fence will act like a union
280 mSlots[slot].mFence = fence;
281 return BAD_VALUE;
282 }
283 mSlots[slot].mFence = mergedFence;
284 }
285
286 return OK;
287}
288
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700289status_t ConsumerBase::releaseBufferLocked(
290 int slot, const sp<GraphicBuffer> graphicBuffer,
291 EGLDisplay display, EGLSyncKHR eglFence) {
292 // If consumer no longer tracks this graphicBuffer (we received a new
293 // buffer on the same slot), the buffer producer is definitely no longer
294 // tracking it.
295 if (!stillTracking(slot, graphicBuffer)) {
296 return OK;
297 }
298
Mark Salyzyn91100452014-06-09 14:27:45 -0700299 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700300 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700301 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700302 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800303 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700304 freeBufferLocked(slot);
305 }
306
Jamie Gennis1df8c342012-12-20 14:05:45 -0800307 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700308
309 return err;
310}
311
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700312bool ConsumerBase::stillTracking(int slot,
313 const sp<GraphicBuffer> graphicBuffer) {
314 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
315 return false;
316 }
317 return (mSlots[slot].mGraphicBuffer != NULL &&
318 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
319}
320
Andy McFadden2adaf042012-12-18 09:49:45 -0800321} // namespace android