blob: 4937b1734c609bb57c6562d9451db6b591ccfa21 [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;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070098}
99
100// Used for refactoring, should not be in final interface
101sp<BufferQueue> ConsumerBase::getBufferQueue() const {
102 Mutex::Autolock lock(mMutex);
103 return mBufferQueue;
104}
105
106void ConsumerBase::onFrameAvailable() {
107 CB_LOGV("onFrameAvailable");
108
109 sp<FrameAvailableListener> listener;
110 { // scope for the lock
111 Mutex::Autolock lock(mMutex);
Igor Murashkina4a31492012-10-29 13:36:11 -0700112 listener = mFrameAvailableListener.promote();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700113 }
114
115 if (listener != NULL) {
116 CB_LOGV("actually calling onFrameAvailable");
117 listener->onFrameAvailable();
118 }
119}
120
121void ConsumerBase::onBuffersReleased() {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800122 Mutex::Autolock lock(mMutex);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700123
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800124 CB_LOGV("onBuffersReleased");
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700125
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800126 if (mAbandoned) {
127 // Nothing to do if we're already abandoned.
128 return;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700129 }
130
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800131 uint32_t mask = 0;
132 mBufferQueue->getReleasedBuffers(&mask);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700133 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis72c3f7d2012-12-07 00:41:56 -0800134 if (mask & (1 << i)) {
135 freeBufferLocked(i);
136 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700137 }
138}
139
140void ConsumerBase::abandon() {
141 CB_LOGV("abandon");
142 Mutex::Autolock lock(mMutex);
143
144 if (!mAbandoned) {
145 abandonLocked();
146 mAbandoned = true;
147 }
148}
149
150void ConsumerBase::abandonLocked() {
151 CB_LOGV("abandonLocked");
152 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
153 freeBufferLocked(i);
154 }
155 // disconnect from the BufferQueue
156 mBufferQueue->consumerDisconnect();
157 mBufferQueue.clear();
158}
159
160void ConsumerBase::setFrameAvailableListener(
Igor Murashkina4a31492012-10-29 13:36:11 -0700161 const wp<FrameAvailableListener>& listener) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700162 CB_LOGV("setFrameAvailableListener");
163 Mutex::Autolock lock(mMutex);
164 mFrameAvailableListener = listener;
165}
166
167void ConsumerBase::dump(String8& result) const {
168 char buffer[1024];
169 dump(result, "", buffer, 1024);
170}
171
172void ConsumerBase::dump(String8& result, const char* prefix,
173 char* buffer, size_t size) const {
174 Mutex::Autolock _l(mMutex);
175 dumpLocked(result, prefix, buffer, size);
176}
177
178void ConsumerBase::dumpLocked(String8& result, const char* prefix,
179 char* buffer, size_t SIZE) const {
180 snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned));
181 result.append(buffer);
182
183 if (!mAbandoned) {
184 mBufferQueue->dump(result, prefix, buffer, SIZE);
185 }
186}
187
188status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item) {
189 status_t err = mBufferQueue->acquireBuffer(item);
190 if (err != NO_ERROR) {
191 return err;
192 }
193
194 if (item->mGraphicBuffer != NULL) {
195 mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
196 }
197
Jamie Gennisb2725412012-09-05 20:09:05 -0700198 mSlots[item->mBuf].mFence = item->mFence;
199
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700200 CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
201
202 return OK;
203}
204
Jamie Gennisb2725412012-09-05 20:09:05 -0700205status_t ConsumerBase::addReleaseFence(int slot, const sp<Fence>& fence) {
Jesse Hall9504eb92012-10-05 14:34:21 -0700206 Mutex::Autolock lock(mMutex);
207 return addReleaseFenceLocked(slot, fence);
208}
209
210status_t ConsumerBase::addReleaseFenceLocked(int slot, const sp<Fence>& fence) {
211 CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700212
213 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
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700232status_t ConsumerBase::releaseBufferLocked(int slot, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700233 EGLSyncKHR eglFence) {
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700234 CB_LOGV("releaseBufferLocked: slot=%d", slot);
Jamie Gennisb2725412012-09-05 20:09:05 -0700235 status_t err = mBufferQueue->releaseBuffer(slot, display, eglFence,
236 mSlots[slot].mFence);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700237 if (err == BufferQueue::STALE_BUFFER_SLOT) {
238 freeBufferLocked(slot);
239 }
240
Jamie Gennis1df8c342012-12-20 14:05:45 -0800241 mSlots[slot].mFence = Fence::NO_FENCE;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700242
243 return err;
244}
245
Andy McFadden2adaf042012-12-18 09:49:45 -0800246} // namespace android