blob: 674ee5acc0407eb3a922783b3f533805a5b5fa22 [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
30#include <gui/IGraphicBufferAlloc.h>
31#include <gui/ISurfaceComposer.h>
32#include <gui/SurfaceComposerClient.h>
33#include <gui/ConsumerBase.h>
34
35#include <private/gui/ComposerService.h>
36
37#include <utils/Log.h>
38#include <utils/String8.h>
39#include <utils/Trace.h>
40
41// Macros for including the ConsumerBase name in log messages
42#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
43#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
45#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
46#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
47
48namespace android {
49
50// Get an ID that's unique within this process.
51static int32_t createProcessUniqueId() {
52 static volatile int32_t globalCounter = 0;
53 return android_atomic_inc(&globalCounter);
54}
55
Mathias Agopiandb89edc2013-08-02 01:40:18 -070056ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070057 mAbandoned(false),
Mathias Agopiandb89edc2013-08-02 01:40:18 -070058 mConsumer(bufferQueue) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070059 // Choose a name using the PID and a process-unique ID.
60 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
61
62 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
63 // reference once the ctor ends, as that would cause the refcount of 'this'
64 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
65 // that's what we create.
Mathias Agopiana4e19522013-07-31 20:09:53 -070066 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
67 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070068
Mathias Agopiandb89edc2013-08-02 01:40:18 -070069 status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070070 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -080071 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -070072 strerror(-err), err);
73 } else {
Mathias Agopiandb89edc2013-08-02 01:40:18 -070074 mConsumer->setConsumerName(mName);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070075 }
76}
77
78ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -070079 CB_LOGV("~ConsumerBase");
80 Mutex::Autolock lock(mMutex);
81
82 // Verify that abandon() has been called before we get here. This should
83 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
84 // derived class to override that method and not call
85 // ConsumerBase::onLastStrongRef().
86 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
87 "consumer is not abandoned!", mName.string());
88}
89
90void ConsumerBase::onLastStrongRef(const void* id) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070091 abandon();
92}
93
94void ConsumerBase::freeBufferLocked(int slotIndex) {
95 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
96 mSlots[slotIndex].mGraphicBuffer = 0;
Jamie Gennis1df8c342012-12-20 14:05:45 -080097 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -070098 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070099}
100
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700101void ConsumerBase::onFrameAvailable() {
102 CB_LOGV("onFrameAvailable");
103
104 sp<FrameAvailableListener> listener;
105 { // scope for the lock
106 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700107 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700108 }
109
110 if (listener != NULL) {
111 CB_LOGV("actually calling onFrameAvailable");
112 listener->onFrameAvailable();
113 }
114}
115
116void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800117 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700118
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800119 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700120
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800121 if (mAbandoned) {
122 // Nothing to do if we're already abandoned.
123 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700124 }
125
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800126 uint32_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700127 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700128 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800129 if (mask & (1 << i)) {
130 freeBufferLocked(i);
131 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 }
133}
134
135void ConsumerBase::abandon() {
136 CB_LOGV("abandon");
137 Mutex::Autolock lock(mMutex);
138
139 if (!mAbandoned) {
140 abandonLocked();
141 mAbandoned = true;
142 }
143}
144
145void ConsumerBase::abandonLocked() {
146 CB_LOGV("abandonLocked");
147 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
148 freeBufferLocked(i);
149 }
150 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700151 mConsumer->consumerDisconnect();
152 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700153}
154
155void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700156 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700157 CB_LOGV("setFrameAvailableListener");
158 Mutex::Autolock lock(mMutex);
159 mFrameAvailableListener = listener;
160}
161
162void ConsumerBase::dump(String8& result) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200163 dump(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700164}
165
Mathias Agopian74d211a2013-04-22 16:55:35 +0200166void ConsumerBase::dump(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700167 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200168 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700169}
170
Mathias Agopian74d211a2013-04-22 16:55:35 +0200171void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
172 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700173
174 if (!mAbandoned) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700175 mConsumer->dump(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700176 }
177}
178
Andy McFadden1585c4d2013-06-28 13:52:40 -0700179status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
180 nsecs_t presentWhen) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700181 status_t err = mConsumer->acquireBuffer(item, presentWhen);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700182 if (err != NO_ERROR) {
183 return err;
184 }
185
186 if (item->mGraphicBuffer != NULL) {
187 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
188 }
189
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700190 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
Jamie Gennisb2725412012-09-05 20:09:05 -0700191 mSlots[item->mBuf].mFence = item->mFence;
192
Mark Salyzyn91100452014-06-09 14:27:45 -0700193 CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700194 item->mBuf, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700195
196 return OK;
197}
198
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700199status_t ConsumerBase::addReleaseFence(int slot,
200 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700201 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700202 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700203}
204
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700205status_t ConsumerBase::addReleaseFenceLocked(int slot,
206 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700207 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700208
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700209 // If consumer no longer tracks this graphicBuffer, we can safely
210 // drop this fence, as it will never be received by the producer.
211 if (!stillTracking(slot, graphicBuffer)) {
212 return OK;
213 }
214
Jamie Gennisb2725412012-09-05 20:09:05 -0700215 if (!mSlots[slot].mFence.get()) {
216 mSlots[slot].mFence = fence;
217 } else {
218 sp<Fence> mergedFence = Fence::merge(
Jamie Gennis7aff4a52012-09-24 12:25:15 -0700219 String8::format("%.28s:%d", mName.string(), slot),
Jamie Gennisb2725412012-09-05 20:09:05 -0700220 mSlots[slot].mFence, fence);
221 if (!mergedFence.get()) {
222 CB_LOGE("failed to merge release fences");
223 // synchronization is broken, the best we can do is hope fences
224 // signal in order so the new fence will act like a union
225 mSlots[slot].mFence = fence;
226 return BAD_VALUE;
227 }
228 mSlots[slot].mFence = mergedFence;
229 }
230
231 return OK;
232}
233
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700234status_t ConsumerBase::releaseBufferLocked(
235 int slot, const sp<GraphicBuffer> graphicBuffer,
236 EGLDisplay display, EGLSyncKHR eglFence) {
237 // If consumer no longer tracks this graphicBuffer (we received a new
238 // buffer on the same slot), the buffer producer is definitely no longer
239 // tracking it.
240 if (!stillTracking(slot, graphicBuffer)) {
241 return OK;
242 }
243
Mark Salyzyn91100452014-06-09 14:27:45 -0700244 CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700245 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700246 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700247 display, eglFence, mSlots[slot].mFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700248 if (err == BufferQueue::STALE_BUFFER_SLOT) {
249 freeBufferLocked(slot);
250 }
251
Jamie Gennis1df8c342012-12-20 14:05:45 -0800252 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700253
254 return err;
255}
256
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700257bool ConsumerBase::stillTracking(int slot,
258 const sp<GraphicBuffer> graphicBuffer) {
259 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
260 return false;
261 }
262 return (mSlots[slot].mGraphicBuffer != NULL &&
263 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
264}
265
Andy McFadden2adaf042012-12-18 09:49:45 -0800266} // namespace android