blob: fd9d15365bc00f5fabcc9daf9a671dae57eeddaf [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
17#define LOG_TAG "ConsumerBase"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
Jamie Gennis1a4d8832012-08-02 20:11:05 -070021#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
26#include <hardware/hardware.h>
27
28#include <gui/IGraphicBufferAlloc.h>
29#include <gui/ISurfaceComposer.h>
30#include <gui/SurfaceComposerClient.h>
31#include <gui/ConsumerBase.h>
32
33#include <private/gui/ComposerService.h>
34
35#include <utils/Log.h>
36#include <utils/String8.h>
37#include <utils/Trace.h>
38
39// Macros for including the ConsumerBase name in log messages
40#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
41#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
42#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
43#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
45
46namespace android {
47
48// Get an ID that's unique within this process.
49static int32_t createProcessUniqueId() {
50 static volatile int32_t globalCounter = 0;
51 return android_atomic_inc(&globalCounter);
52}
53
54ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070055 mAbandoned(false),
56 mBufferQueue(bufferQueue) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070057 // Choose a name using the PID and a process-unique ID.
58 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
59
60 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
61 // reference once the ctor ends, as that would cause the refcount of 'this'
62 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
63 // that's what we create.
64 wp<BufferQueue::ConsumerListener> listener;
65 sp<BufferQueue::ConsumerListener> proxy;
66 listener = static_cast<BufferQueue::ConsumerListener*>(this);
67 proxy = new BufferQueue::ProxyConsumerListener(listener);
68
69 status_t err = mBufferQueue->consumerConnect(proxy);
70 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 {
74 mBufferQueue->setConsumerName(mName);
75 }
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
101// Used for refactoring, should not be in final interface
102sp<BufferQueue> ConsumerBase::getBufferQueue() const {
103 Mutex::Autolock lock(mMutex);
104 return mBufferQueue;
105}
106
107void ConsumerBase::onFrameAvailable() {
108 CB_LOGV("onFrameAvailable");
109
110 sp<FrameAvailableListener> listener;
111 { // scope for the lock
112 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700113 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700114 }
115
116 if (listener != NULL) {
117 CB_LOGV("actually calling onFrameAvailable");
118 listener->onFrameAvailable();
119 }
120}
121
122void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800123 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700124
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800125 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700126
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800127 if (mAbandoned) {
128 // Nothing to do if we're already abandoned.
129 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700130 }
131
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800132 uint32_t mask = 0;
133 mBufferQueue->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700134 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800135 if (mask & (1 << i)) {
136 freeBufferLocked(i);
137 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700138 }
139}
140
141void ConsumerBase::abandon() {
142 CB_LOGV("abandon");
143 Mutex::Autolock lock(mMutex);
144
145 if (!mAbandoned) {
146 abandonLocked();
147 mAbandoned = true;
148 }
149}
150
151void ConsumerBase::abandonLocked() {
152 CB_LOGV("abandonLocked");
153 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
154 freeBufferLocked(i);
155 }
156 // disconnect from the BufferQueue
157 mBufferQueue->consumerDisconnect();
158 mBufferQueue.clear();
159}
160
161void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700162 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700163 CB_LOGV("setFrameAvailableListener");
164 Mutex::Autolock lock(mMutex);
165 mFrameAvailableListener = listener;
166}
167
168void ConsumerBase::dump(String8& result) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200169 dump(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700170}
171
Mathias Agopian74d211a2013-04-22 16:55:35 +0200172void ConsumerBase::dump(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700173 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200174 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700175}
176
Mathias Agopian74d211a2013-04-22 16:55:35 +0200177void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
178 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700179
180 if (!mAbandoned) {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200181 mBufferQueue->dump(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700182 }
183}
184
185status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) {
186 status_t err = mBufferQueue->acquireBuffer(item);
187 if (err != NO_ERROR) {
188 return err;
189 }
190
191 if (item->mGraphicBuffer != NULL) {
192 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
193 }
194
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700195 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
Jamie Gennisb2725412012-09-05 20:09:05 -0700196 mSlots[item->mBuf].mFence = item->mFence;
197
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700198 CB_LOGV("acquireBufferLocked: -> slot=%d/%llu",
199 item->mBuf, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700200
201 return OK;
202}
203
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700204status_t ConsumerBase::addReleaseFence(int slot,
205 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700206 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700207 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700208}
209
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700210status_t ConsumerBase::addReleaseFenceLocked(int slot,
211 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700212 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700213
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700214 // If consumer no longer tracks this graphicBuffer, we can safely
215 // drop this fence, as it will never be received by the producer.
216 if (!stillTracking(slot, graphicBuffer)) {
217 return OK;
218 }
219
Jamie Gennisb2725412012-09-05 20:09:05 -0700220 if (!mSlots[slot].mFence.get()) {
221 mSlots[slot].mFence = fence;
222 } else {
223 sp<Fence> mergedFence = Fence::merge(
Jamie Gennis7aff4a52012-09-24 12:25:15 -0700224 String8::format("%.28s:%d", mName.string(), slot),
Jamie Gennisb2725412012-09-05 20:09:05 -0700225 mSlots[slot].mFence, fence);
226 if (!mergedFence.get()) {
227 CB_LOGE("failed to merge release fences");
228 // synchronization is broken, the best we can do is hope fences
229 // signal in order so the new fence will act like a union
230 mSlots[slot].mFence = fence;
231 return BAD_VALUE;
232 }
233 mSlots[slot].mFence = mergedFence;
234 }
235
236 return OK;
237}
238
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700239status_t ConsumerBase::releaseBufferLocked(
240 int slot, const sp<GraphicBuffer> graphicBuffer,
241 EGLDisplay display, EGLSyncKHR eglFence) {
242 // If consumer no longer tracks this graphicBuffer (we received a new
243 // buffer on the same slot), the buffer producer is definitely no longer
244 // tracking it.
245 if (!stillTracking(slot, graphicBuffer)) {
246 return OK;
247 }
248
249 CB_LOGV("releaseBufferLocked: slot=%d/%llu",
250 slot, mSlots[slot].mFrameNumber);
251 status_t err = mBufferQueue->releaseBuffer(slot, mSlots[slot].mFrameNumber,
252 display, eglFence, mSlots[slot].mFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700253 if (err == BufferQueue::STALE_BUFFER_SLOT) {
254 freeBufferLocked(slot);
255 }
256
Jamie Gennis1df8c342012-12-20 14:05:45 -0800257 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700258
259 return err;
260}
261
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700262bool ConsumerBase::stillTracking(int slot,
263 const sp<GraphicBuffer> graphicBuffer) {
264 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
265 return false;
266 }
267 return (mSlots[slot].mGraphicBuffer != NULL &&
268 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
269}
270
Andy McFadden2adaf042012-12-18 09:49:45 -0800271} // namespace android