blob: d748786d98f64caf5bdd7144ffe1143233ead7d5 [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
Mathias Agopian595264f2013-07-16 22:56:09 -070054ConsumerBase::ConsumerBase(const sp<BufferQueue>& bufferQueue, bool controlledByApp) :
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.
Mathias Agopiana4e19522013-07-31 20:09:53 -070064 wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
65 sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070066
Mathias Agopian595264f2013-07-16 22:56:09 -070067 status_t err = mBufferQueue->consumerConnect(proxy, controlledByApp);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070068 if (err != NO_ERROR) {
Andy McFadden2adaf042012-12-18 09:49:45 -080069 CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
Jamie Gennis1a4d8832012-08-02 20:11:05 -070070 strerror(-err), err);
71 } else {
72 mBufferQueue->setConsumerName(mName);
73 }
74}
75
76ConsumerBase::~ConsumerBase() {
Jamie Gennisad669b02013-04-05 16:41:27 -070077 CB_LOGV("~ConsumerBase");
78 Mutex::Autolock lock(mMutex);
79
80 // Verify that abandon() has been called before we get here. This should
81 // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
82 // derived class to override that method and not call
83 // ConsumerBase::onLastStrongRef().
84 LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
85 "consumer is not abandoned!", mName.string());
86}
87
88void ConsumerBase::onLastStrongRef(const void* id) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -070089 abandon();
90}
91
92void ConsumerBase::freeBufferLocked(int slotIndex) {
93 CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
94 mSlots[slotIndex].mGraphicBuffer = 0;
Jamie Gennis1df8c342012-12-20 14:05:45 -080095 mSlots[slotIndex].mFence = Fence::NO_FENCE;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -070096 mSlots[slotIndex].mFrameNumber = 0;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070097}
98
99// Used for refactoring, should not be in final interface
100sp<BufferQueue> ConsumerBase::getBufferQueue() const {
101 Mutex::Autolock lock(mMutex);
102 return mBufferQueue;
103}
104
105void ConsumerBase::onFrameAvailable() {
106 CB_LOGV("onFrameAvailable");
107
108 sp<FrameAvailableListener> listener;
109 { // scope for the lock
110 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700111 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700112 }
113
114 if (listener != NULL) {
115 CB_LOGV("actually calling onFrameAvailable");
116 listener->onFrameAvailable();
117 }
118}
119
120void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800121 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700122
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800123 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700124
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800125 if (mAbandoned) {
126 // Nothing to do if we're already abandoned.
127 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700128 }
129
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800130 uint32_t mask = 0;
131 mBufferQueue->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800133 if (mask & (1 << i)) {
134 freeBufferLocked(i);
135 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136 }
137}
138
139void ConsumerBase::abandon() {
140 CB_LOGV("abandon");
141 Mutex::Autolock lock(mMutex);
142
143 if (!mAbandoned) {
144 abandonLocked();
145 mAbandoned = true;
146 }
147}
148
149void ConsumerBase::abandonLocked() {
150 CB_LOGV("abandonLocked");
151 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
152 freeBufferLocked(i);
153 }
154 // disconnect from the BufferQueue
155 mBufferQueue->consumerDisconnect();
156 mBufferQueue.clear();
157}
158
159void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700160 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700161 CB_LOGV("setFrameAvailableListener");
162 Mutex::Autolock lock(mMutex);
163 mFrameAvailableListener = listener;
164}
165
166void ConsumerBase::dump(String8& result) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200167 dump(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700168}
169
Mathias Agopian74d211a2013-04-22 16:55:35 +0200170void ConsumerBase::dump(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700171 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200172 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700173}
174
Mathias Agopian74d211a2013-04-22 16:55:35 +0200175void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
176 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700177
178 if (!mAbandoned) {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200179 mBufferQueue->dump(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700180 }
181}
182
Andy McFadden1585c4d2013-06-28 13:52:40 -0700183status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
184 nsecs_t presentWhen) {
185 status_t err = mBufferQueue->acquireBuffer(item, presentWhen);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700186 if (err != NO_ERROR) {
187 return err;
188 }
189
190 if (item->mGraphicBuffer != NULL) {
191 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
192 }
193
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700194 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
Jamie Gennisb2725412012-09-05 20:09:05 -0700195 mSlots[item->mBuf].mFence = item->mFence;
196
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700197 CB_LOGV("acquireBufferLocked: -> slot=%d/%llu",
198 item->mBuf, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700199
200 return OK;
201}
202
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700203status_t ConsumerBase::addReleaseFence(int slot,
204 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700205 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700206 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700207}
208
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700209status_t ConsumerBase::addReleaseFenceLocked(int slot,
210 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700211 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700212
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700213 // If consumer no longer tracks this graphicBuffer, we can safely
214 // drop this fence, as it will never be received by the producer.
215 if (!stillTracking(slot, graphicBuffer)) {
216 return OK;
217 }
218
Jamie Gennisb2725412012-09-05 20:09:05 -0700219 if (!mSlots[slot].mFence.get()) {
220 mSlots[slot].mFence = fence;
221 } else {
222 sp<Fence> mergedFence = Fence::merge(
Jamie Gennis7aff4a52012-09-24 12:25:15 -0700223 String8::format("%.28s:%d", mName.string(), slot),
Jamie Gennisb2725412012-09-05 20:09:05 -0700224 mSlots[slot].mFence, fence);
225 if (!mergedFence.get()) {
226 CB_LOGE("failed to merge release fences");
227 // synchronization is broken, the best we can do is hope fences
228 // signal in order so the new fence will act like a union
229 mSlots[slot].mFence = fence;
230 return BAD_VALUE;
231 }
232 mSlots[slot].mFence = mergedFence;
233 }
234
235 return OK;
236}
237
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700238status_t ConsumerBase::releaseBufferLocked(
239 int slot, const sp<GraphicBuffer> graphicBuffer,
240 EGLDisplay display, EGLSyncKHR eglFence) {
241 // If consumer no longer tracks this graphicBuffer (we received a new
242 // buffer on the same slot), the buffer producer is definitely no longer
243 // tracking it.
244 if (!stillTracking(slot, graphicBuffer)) {
245 return OK;
246 }
247
248 CB_LOGV("releaseBufferLocked: slot=%d/%llu",
249 slot, mSlots[slot].mFrameNumber);
250 status_t err = mBufferQueue->releaseBuffer(slot, mSlots[slot].mFrameNumber,
251 display, eglFence, mSlots[slot].mFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700252 if (err == BufferQueue::STALE_BUFFER_SLOT) {
253 freeBufferLocked(slot);
254 }
255
Jamie Gennis1df8c342012-12-20 14:05:45 -0800256 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700257
258 return err;
259}
260
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700261bool ConsumerBase::stillTracking(int slot,
262 const sp<GraphicBuffer> graphicBuffer) {
263 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
264 return false;
265 }
266 return (mSlots[slot].mGraphicBuffer != NULL &&
267 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
268}
269
Andy McFadden2adaf042012-12-18 09:49:45 -0800270} // namespace android