1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Copyright (C) 2010 The Android Open Source Project
* Copyright (C) 2013 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "GonkConsumerBase"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
//#define LOG_NDEBUG 0
#define EGL_EGLEXT_PROTOTYPES
#include <hardware/hardware.h>
#include <gui/IGraphicBufferAlloc.h>
#include <utils/Log.h>
#include <utils/String8.h>
#include "GonkConsumerBaseJB.h"
// Macros for including the GonkConsumerBase name in log messages
#define CB_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define CB_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define CB_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define CB_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define CB_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
namespace android {
// Get an ID that's unique within this process.
static int32_t createProcessUniqueId() {
static volatile int32_t globalCounter = 0;
return android_atomic_inc(&globalCounter);
}
GonkConsumerBase::GonkConsumerBase(const sp<GonkBufferQueue>& bufferQueue) :
mAbandoned(false),
mBufferQueue(bufferQueue) {
// Choose a name using the PID and a process-unique ID.
mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
// Note that we can't create an sp<...>(this) in a ctor that will not keep a
// reference once the ctor ends, as that would cause the refcount of 'this'
// dropping to 0 at the end of the ctor. Since all we need is a wp<...>
// that's what we create.
wp<GonkBufferQueue::ConsumerListener> listener;
sp<GonkBufferQueue::ConsumerListener> proxy;
listener = static_cast<GonkBufferQueue::ConsumerListener*>(this);
proxy = new GonkBufferQueue::ProxyConsumerListener(listener);
status_t err = mBufferQueue->consumerConnect(proxy);
if (err != NO_ERROR) {
CB_LOGE("GonkConsumerBase: error connecting to GonkBufferQueue: %s (%d)",
strerror(-err), err);
} else {
mBufferQueue->setConsumerName(mName);
}
}
GonkConsumerBase::~GonkConsumerBase() {
CB_LOGV("~GonkConsumerBase");
Mutex::Autolock lock(mMutex);
// Verify that abandon() has been called before we get here. This should
// be done by GonkConsumerBase::onLastStrongRef(), but it's possible for a
// derived class to override that method and not call
// GonkConsumerBase::onLastStrongRef().
LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~GonkConsumerBase was called, but the "
"consumer is not abandoned!", mName.string());
}
void GonkConsumerBase::onLastStrongRef(const void* id) {
abandon();
}
void GonkConsumerBase::freeBufferLocked(int slotIndex) {
CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
mSlots[slotIndex].mGraphicBuffer = 0;
mSlots[slotIndex].mFence = Fence::NO_FENCE;
}
// Used for refactoring, should not be in final interface
sp<GonkBufferQueue> GonkConsumerBase::getBufferQueue() const {
Mutex::Autolock lock(mMutex);
return mBufferQueue;
}
void GonkConsumerBase::onFrameAvailable() {
CB_LOGV("onFrameAvailable");
sp<FrameAvailableListener> listener;
{ // scope for the lock
Mutex::Autolock lock(mMutex);
#if ANDROID_VERSION == 17
listener = mFrameAvailableListener;
#else
listener = mFrameAvailableListener.promote();
#endif
}
if (listener != NULL) {
CB_LOGV("actually calling onFrameAvailable");
listener->onFrameAvailable();
}
}
void GonkConsumerBase::onBuffersReleased() {
Mutex::Autolock lock(mMutex);
CB_LOGV("onBuffersReleased");
if (mAbandoned) {
// Nothing to do if we're already abandoned.
return;
}
uint32_t mask = 0;
mBufferQueue->getReleasedBuffers(&mask);
for (int i = 0; i < GonkBufferQueue::NUM_BUFFER_SLOTS; i++) {
if (mask & (1 << i)) {
freeBufferLocked(i);
}
}
}
void GonkConsumerBase::abandon() {
CB_LOGV("abandon");
Mutex::Autolock lock(mMutex);
if (!mAbandoned) {
abandonLocked();
mAbandoned = true;
}
}
void GonkConsumerBase::abandonLocked() {
CB_LOGV("abandonLocked");
for (int i =0; i < GonkBufferQueue::NUM_BUFFER_SLOTS; i++) {
freeBufferLocked(i);
}
// disconnect from the GonkBufferQueue
mBufferQueue->consumerDisconnect();
mBufferQueue.clear();
}
void GonkConsumerBase::setFrameAvailableListener(
#if ANDROID_VERSION == 17
const sp<FrameAvailableListener>& listener) {
#else
const wp<FrameAvailableListener>& listener) {
#endif
CB_LOGV("setFrameAvailableListener");
Mutex::Autolock lock(mMutex);
mFrameAvailableListener = listener;
}
void GonkConsumerBase::dump(String8& result) const {
char buffer[1024];
dump(result, "", buffer, 1024);
}
void GonkConsumerBase::dump(String8& result, const char* prefix,
char* buffer, size_t size) const {
Mutex::Autolock _l(mMutex);
dumpLocked(result, prefix, buffer, size);
}
void GonkConsumerBase::dumpLocked(String8& result, const char* prefix,
char* buffer, size_t SIZE) const {
snprintf(buffer, SIZE, "%smAbandoned=%d\n", prefix, int(mAbandoned));
result.append(buffer);
if (!mAbandoned) {
mBufferQueue->dumpToString(result, prefix, buffer, SIZE);
}
}
status_t GonkConsumerBase::acquireBufferLocked(GonkBufferQueue::BufferItem *item) {
status_t err = mBufferQueue->acquireBuffer(item);
if (err != NO_ERROR) {
return err;
}
if (item->mGraphicBuffer != NULL) {
mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
}
mSlots[item->mBuf].mFence = item->mFence;
CB_LOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
return OK;
}
status_t GonkConsumerBase::addReleaseFence(int slot, const sp<Fence>& fence) {
Mutex::Autolock lock(mMutex);
return addReleaseFenceLocked(slot, fence);
}
status_t GonkConsumerBase::addReleaseFenceLocked(int slot, const sp<Fence>& fence) {
CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
if (!mSlots[slot].mFence.get()) {
mSlots[slot].mFence = fence;
} else {
sp<Fence> mergedFence = Fence::merge(
String8::format("%.28s:%d", mName.string(), slot),
mSlots[slot].mFence, fence);
if (!mergedFence.get()) {
CB_LOGE("failed to merge release fences");
// synchronization is broken, the best we can do is hope fences
// signal in order so the new fence will act like a union
mSlots[slot].mFence = fence;
return BAD_VALUE;
}
mSlots[slot].mFence = mergedFence;
}
return OK;
}
status_t GonkConsumerBase::releaseBufferLocked(int slot) {
CB_LOGV("releaseBufferLocked: slot=%d", slot);
status_t err = mBufferQueue->releaseBuffer(slot, mSlots[slot].mFence);
if (err == GonkBufferQueue::STALE_BUFFER_SLOT) {
freeBufferLocked(slot);
}
mSlots[slot].mFence = Fence::NO_FENCE;
return err;
}
} // namespace android
|