blob: c5900aa5b19368816ce4b7a583fb129040daf4c8 [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 Agopiandb89edc2013-08-02 01:40:18 -070054ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
Jamie Gennis9fea3422012-08-07 18:03:04 -070055 mAbandoned(false),
Mathias Agopiandb89edc2013-08-02 01:40:18 -070056 mConsumer(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 Agopiandb89edc2013-08-02 01:40:18 -070067 status_t err = mConsumer->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 {
Mathias Agopiandb89edc2013-08-02 01:40:18 -070072 mConsumer->setConsumerName(mName);
Jamie Gennis1a4d8832012-08-02 20:11:05 -070073 }
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
Igor Murashkin7d2d1602013-11-12 18:02:20 -080088void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
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
Jamie Gennis1a4d8832012-08-02 20:11:05 -070099void ConsumerBase::onFrameAvailable() {
100 CB_LOGV("onFrameAvailable");
101
102 sp<FrameAvailableListener> listener;
103 { // scope for the lock
104 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700105 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700106 }
107
108 if (listener != NULL) {
109 CB_LOGV("actually calling onFrameAvailable");
110 listener->onFrameAvailable();
111 }
112}
113
114void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800115 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700116
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800117 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700118
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800119 if (mAbandoned) {
120 // Nothing to do if we're already abandoned.
121 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700122 }
123
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800124 uint32_t mask = 0;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700125 mConsumer->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700126 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800127 if (mask & (1 << i)) {
128 freeBufferLocked(i);
129 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700130 }
131}
132
133void ConsumerBase::abandon() {
134 CB_LOGV("abandon");
135 Mutex::Autolock lock(mMutex);
136
137 if (!mAbandoned) {
138 abandonLocked();
139 mAbandoned = true;
140 }
141}
142
143void ConsumerBase::abandonLocked() {
144 CB_LOGV("abandonLocked");
145 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
146 freeBufferLocked(i);
147 }
148 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700149 mConsumer->consumerDisconnect();
150 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700151}
152
153void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700154 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700155 CB_LOGV("setFrameAvailableListener");
156 Mutex::Autolock lock(mMutex);
157 mFrameAvailableListener = listener;
158}
159
160void ConsumerBase::dump(String8& result) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200161 dump(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700162}
163
Mathias Agopian74d211a2013-04-22 16:55:35 +0200164void ConsumerBase::dump(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700165 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200166 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700167}
168
Mathias Agopian74d211a2013-04-22 16:55:35 +0200169void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
170 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700171
172 if (!mAbandoned) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700173 mConsumer->dump(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700174 }
175}
176
Andy McFadden1585c4d2013-06-28 13:52:40 -0700177status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
178 nsecs_t presentWhen) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700179 status_t err = mConsumer->acquireBuffer(item, presentWhen);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700180 if (err != NO_ERROR) {
181 return err;
182 }
183
184 if (item->mGraphicBuffer != NULL) {
185 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
186 }
187
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700188 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
Jamie Gennisb2725412012-09-05 20:09:05 -0700189 mSlots[item->mBuf].mFence = item->mFence;
190
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700191 CB_LOGV("acquireBufferLocked: -> slot=%d/%llu",
192 item->mBuf, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700193
194 return OK;
195}
196
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700197status_t ConsumerBase::addReleaseFence(int slot,
198 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700199 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700200 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700201}
202
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700203status_t ConsumerBase::addReleaseFenceLocked(int slot,
204 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700205 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700206
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700207 // If consumer no longer tracks this graphicBuffer, we can safely
208 // drop this fence, as it will never be received by the producer.
209 if (!stillTracking(slot, graphicBuffer)) {
210 return OK;
211 }
212
Jamie Gennisb2725412012-09-05 20:09:05 -0700213 if (!mSlots[slot].mFence.get()) {
214 mSlots[slot].mFence = fence;
215 } else {
216 sp<Fence> mergedFence = Fence::merge(
Jamie Gennis7aff4a52012-09-24 12:25:15 -0700217 String8::format("%.28s:%d", mName.string(), slot),
Jamie Gennisb2725412012-09-05 20:09:05 -0700218 mSlots[slot].mFence, fence);
219 if (!mergedFence.get()) {
220 CB_LOGE("failed to merge release fences");
221 // synchronization is broken, the best we can do is hope fences
222 // signal in order so the new fence will act like a union
223 mSlots[slot].mFence = fence;
224 return BAD_VALUE;
225 }
226 mSlots[slot].mFence = mergedFence;
227 }
228
229 return OK;
230}
231
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700232status_t ConsumerBase::releaseBufferLocked(
233 int slot, const sp<GraphicBuffer> graphicBuffer,
234 EGLDisplay display, EGLSyncKHR eglFence) {
235 // If consumer no longer tracks this graphicBuffer (we received a new
236 // buffer on the same slot), the buffer producer is definitely no longer
237 // tracking it.
238 if (!stillTracking(slot, graphicBuffer)) {
239 return OK;
240 }
241
242 CB_LOGV("releaseBufferLocked: slot=%d/%llu",
243 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700244 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700245 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800246 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700247 freeBufferLocked(slot);
248 }
249
Jamie Gennis1df8c342012-12-20 14:05:45 -0800250 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700251
252 return err;
253}
254
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700255bool ConsumerBase::stillTracking(int slot,
256 const sp<GraphicBuffer> graphicBuffer) {
257 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
258 return false;
259 }
260 return (mSlots[slot].mGraphicBuffer != NULL &&
261 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
262}
263
Andy McFadden2adaf042012-12-18 09:49:45 -0800264} // namespace android