blob: b6adc54d7135e47ae3ae2bf0acd7aac2131b0e03 [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
Jesse Hall399184a2014-03-03 15:42:54 -0800133void ConsumerBase::onSidebandStreamChanged() {
134}
135
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700136void ConsumerBase::abandon() {
137 CB_LOGV("abandon");
138 Mutex::Autolock lock(mMutex);
139
140 if (!mAbandoned) {
141 abandonLocked();
142 mAbandoned = true;
143 }
144}
145
146void ConsumerBase::abandonLocked() {
147 CB_LOGV("abandonLocked");
148 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
149 freeBufferLocked(i);
150 }
151 // disconnect from the BufferQueue
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700152 mConsumer->consumerDisconnect();
153 mConsumer.clear();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700154}
155
156void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700157 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700158 CB_LOGV("setFrameAvailableListener");
159 Mutex::Autolock lock(mMutex);
160 mFrameAvailableListener = listener;
161}
162
163void ConsumerBase::dump(String8& result) const {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200164 dump(result, "");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700165}
166
Mathias Agopian74d211a2013-04-22 16:55:35 +0200167void ConsumerBase::dump(String8& result, const char* prefix) const {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700168 Mutex::Autolock _l(mMutex);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200169 dumpLocked(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700170}
171
Mathias Agopian74d211a2013-04-22 16:55:35 +0200172void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
173 result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700174
175 if (!mAbandoned) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700176 mConsumer->dump(result, prefix);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700177 }
178}
179
Andy McFadden1585c4d2013-06-28 13:52:40 -0700180status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
181 nsecs_t presentWhen) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700182 status_t err = mConsumer->acquireBuffer(item, presentWhen);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700183 if (err != NO_ERROR) {
184 return err;
185 }
186
187 if (item->mGraphicBuffer != NULL) {
188 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
189 }
190
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700191 mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
Jamie Gennisb2725412012-09-05 20:09:05 -0700192 mSlots[item->mBuf].mFence = item->mFence;
193
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700194 CB_LOGV("acquireBufferLocked: -> slot=%d/%llu",
195 item->mBuf, item->mFrameNumber);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700196
197 return OK;
198}
199
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700200status_t ConsumerBase::addReleaseFence(int slot,
201 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700202 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700203 return addReleaseFenceLocked(slot, graphicBuffer, fence);
Jesse Hall9504eb92012-10-05 14:34:21 -0700204}
205
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700206status_t ConsumerBase::addReleaseFenceLocked(int slot,
207 const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700208 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700209
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700210 // If consumer no longer tracks this graphicBuffer, we can safely
211 // drop this fence, as it will never be received by the producer.
212 if (!stillTracking(slot, graphicBuffer)) {
213 return OK;
214 }
215
Jamie Gennisb2725412012-09-05 20:09:05 -0700216 if (!mSlots[slot].mFence.get()) {
217 mSlots[slot].mFence = fence;
218 } else {
219 sp<Fence> mergedFence = Fence::merge(
Jamie Gennis7aff4a52012-09-24 12:25:15 -0700220 String8::format("%.28s:%d", mName.string(), slot),
Jamie Gennisb2725412012-09-05 20:09:05 -0700221 mSlots[slot].mFence, fence);
222 if (!mergedFence.get()) {
223 CB_LOGE("failed to merge release fences");
224 // synchronization is broken, the best we can do is hope fences
225 // signal in order so the new fence will act like a union
226 mSlots[slot].mFence = fence;
227 return BAD_VALUE;
228 }
229 mSlots[slot].mFence = mergedFence;
230 }
231
232 return OK;
233}
234
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700235status_t ConsumerBase::releaseBufferLocked(
236 int slot, const sp<GraphicBuffer> graphicBuffer,
237 EGLDisplay display, EGLSyncKHR eglFence) {
238 // If consumer no longer tracks this graphicBuffer (we received a new
239 // buffer on the same slot), the buffer producer is definitely no longer
240 // tracking it.
241 if (!stillTracking(slot, graphicBuffer)) {
242 return OK;
243 }
244
245 CB_LOGV("releaseBufferLocked: slot=%d/%llu",
246 slot, mSlots[slot].mFrameNumber);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700247 status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700248 display, eglFence, mSlots[slot].mFence);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800249 if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700250 freeBufferLocked(slot);
251 }
252
Jamie Gennis1df8c342012-12-20 14:05:45 -0800253 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700254
255 return err;
256}
257
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700258bool ConsumerBase::stillTracking(int slot,
259 const sp<GraphicBuffer> graphicBuffer) {
260 if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
261 return false;
262 }
263 return (mSlots[slot].mGraphicBuffer != NULL &&
264 mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
265}
266
Andy McFadden2adaf042012-12-18 09:49:45 -0800267} // namespace android