blob: 87d66e2b7f7a18bb8a3c24643354ef10d803f825 [file] [log] [blame]
Daniel Lam6b091c52012-01-22 15:26:27 -08001/*
2 * Copyright (C) 2012 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 "BufferQueue"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070019//#define LOG_NDEBUG 0
Daniel Lam6b091c52012-01-22 15:26:27 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26
27#include <gui/BufferQueue.h>
Mathias Agopiana4e19522013-07-31 20:09:53 -070028#include <gui/IConsumerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/ISurfaceComposer.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080030#include <private/gui/ComposerService.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080031
32#include <utils/Log.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080033#include <utils/Trace.h>
Mathias Agopian7cdd7862013-07-18 22:10:56 -070034#include <utils/CallStack.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080035
Daniel Lam6b091c52012-01-22 15:26:27 -080036// Macros for including the BufferQueue name in log messages
Daniel Lameae59d22012-01-22 15:26:27 -080037#define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
38#define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
39#define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
40#define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
41#define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
Daniel Lam6b091c52012-01-22 15:26:27 -080042
Mathias Agopian546ed2d2012-03-01 22:11:25 -080043#define ATRACE_BUFFER_INDEX(index) \
Jamie Gennis695e3312012-04-16 20:34:58 -070044 if (ATRACE_ENABLED()) { \
45 char ___traceBuf[1024]; \
46 snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), \
47 (index)); \
48 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
49 }
Mathias Agopian546ed2d2012-03-01 22:11:25 -080050
Daniel Lam6b091c52012-01-22 15:26:27 -080051namespace android {
52
53// Get an ID that's unique within this process.
54static int32_t createProcessUniqueId() {
55 static volatile int32_t globalCounter = 0;
56 return android_atomic_inc(&globalCounter);
57}
58
Jamie Genniscd1806e2012-05-10 02:22:33 -070059static const char* scalingModeName(int scalingMode) {
60 switch (scalingMode) {
61 case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
62 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
63 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
64 default: return "Unknown";
65 }
66}
67
Mathias Agopian595264f2013-07-16 22:56:09 -070068BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) :
Daniel Lam6b091c52012-01-22 15:26:27 -080069 mDefaultWidth(1),
70 mDefaultHeight(1),
Jamie Gennis72f096f2012-08-27 18:48:37 -070071 mMaxAcquiredBufferCount(1),
72 mDefaultMaxBufferCount(2),
Jamie Gennis31a353d2012-08-24 17:25:13 -070073 mOverrideMaxBufferCount(0),
Mathias Agopian595264f2013-07-16 22:56:09 -070074 mConsumerControlledByApp(false),
75 mDequeueBufferCannotBlock(false),
Mathias Agopianad678e12013-07-23 17:28:53 -070076 mUseAsyncBuffer(true),
Daniel Lam6b091c52012-01-22 15:26:27 -080077 mConnectedApi(NO_CONNECTED_API),
78 mAbandoned(false),
Daniel Lameae59d22012-01-22 15:26:27 -080079 mFrameCounter(0),
Daniel Lamb2675792012-02-23 14:35:13 -080080 mBufferHasBeenQueued(false),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070081 mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
Daniel Lamb2675792012-02-23 14:35:13 -080082 mConsumerUsageBits(0),
83 mTransformHint(0)
Daniel Lam6b091c52012-01-22 15:26:27 -080084{
85 // Choose a name using the PID and a process-unique ID.
Daniel Lameae59d22012-01-22 15:26:27 -080086 mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Daniel Lam6b091c52012-01-22 15:26:27 -080087
88 ST_LOGV("BufferQueue");
Mathias Agopian3e876012012-06-07 17:52:54 -070089 if (allocator == NULL) {
90 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
91 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
92 if (mGraphicBufferAlloc == 0) {
93 ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
94 }
95 } else {
96 mGraphicBufferAlloc = allocator;
Daniel Lamabe61bf2012-03-26 20:37:15 -070097 }
Daniel Lam6b091c52012-01-22 15:26:27 -080098}
99
100BufferQueue::~BufferQueue() {
101 ST_LOGV("~BufferQueue");
102}
103
Jamie Gennis31a353d2012-08-24 17:25:13 -0700104status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700105 const int minBufferCount = mUseAsyncBuffer ? 2 : 1;
106 if (count < minBufferCount || count > NUM_BUFFER_SLOTS)
Daniel Lam6b091c52012-01-22 15:26:27 -0800107 return BAD_VALUE;
108
Jamie Gennis31a353d2012-08-24 17:25:13 -0700109 mDefaultMaxBufferCount = count;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700110 mDequeueCondition.broadcast();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700111
Andy McFadden753e3412013-04-04 17:09:03 -0700112 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800113}
114
Daniel Lameae59d22012-01-22 15:26:27 -0800115void BufferQueue::setConsumerName(const String8& name) {
116 Mutex::Autolock lock(mMutex);
117 mConsumerName = name;
118}
119
Daniel Lamb2675792012-02-23 14:35:13 -0800120status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
121 Mutex::Autolock lock(mMutex);
122 mDefaultBufferFormat = defaultFormat;
Andy McFadden753e3412013-04-04 17:09:03 -0700123 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800124}
125
126status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
127 Mutex::Autolock lock(mMutex);
128 mConsumerUsageBits = usage;
Andy McFadden753e3412013-04-04 17:09:03 -0700129 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800130}
131
132status_t BufferQueue::setTransformHint(uint32_t hint) {
Andy McFadden69052052012-09-14 16:10:11 -0700133 ST_LOGV("setTransformHint: %02x", hint);
Daniel Lamb2675792012-02-23 14:35:13 -0800134 Mutex::Autolock lock(mMutex);
135 mTransformHint = hint;
Andy McFadden753e3412013-04-04 17:09:03 -0700136 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800137}
138
Daniel Lam6b091c52012-01-22 15:26:27 -0800139status_t BufferQueue::setBufferCount(int bufferCount) {
140 ST_LOGV("setBufferCount: count=%d", bufferCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800141
Mathias Agopiana4e19522013-07-31 20:09:53 -0700142 sp<IConsumerListener> listener;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700143 {
144 Mutex::Autolock lock(mMutex);
Daniel Lam6b091c52012-01-22 15:26:27 -0800145
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700146 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800147 ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700148 return NO_INIT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800149 }
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700150 if (bufferCount > NUM_BUFFER_SLOTS) {
Andy McFadden753e3412013-04-04 17:09:03 -0700151 ST_LOGE("setBufferCount: bufferCount too large (max %d)",
152 NUM_BUFFER_SLOTS);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700153 return BAD_VALUE;
154 }
155
156 // Error out if the user has dequeued buffers
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700157 for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700158 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
159 ST_LOGE("setBufferCount: client owns some buffers");
160 return -EINVAL;
161 }
162 }
163
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700164 if (bufferCount == 0) {
Jamie Gennis31a353d2012-08-24 17:25:13 -0700165 mOverrideMaxBufferCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700166 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -0700167 return NO_ERROR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700168 }
169
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700170 // fine to assume async to false before we're setting the buffer count
171 const int minBufferSlots = getMinMaxBufferCountLocked(false);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700172 if (bufferCount < minBufferSlots) {
173 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
174 "minimum (%d)", bufferCount, minBufferSlots);
175 return BAD_VALUE;
176 }
177
178 // here we're guaranteed that the client doesn't have dequeued buffers
Lajos Molnar9e3cb552013-05-06 16:23:07 -0700179 // and will release all of its buffer references. We don't clear the
180 // queue, however, so currently queued buffers still get displayed.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700181 freeAllBuffersLocked();
Jamie Gennis31a353d2012-08-24 17:25:13 -0700182 mOverrideMaxBufferCount = bufferCount;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700183 mDequeueCondition.broadcast();
184 listener = mConsumerListener;
185 } // scope for lock
186
187 if (listener != NULL) {
188 listener->onBuffersReleased();
Daniel Lam6b091c52012-01-22 15:26:27 -0800189 }
190
Andy McFadden753e3412013-04-04 17:09:03 -0700191 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800192}
193
Daniel Lamb8560522012-01-30 15:51:27 -0800194int BufferQueue::query(int what, int* outValue)
195{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800196 ATRACE_CALL();
Daniel Lamb8560522012-01-30 15:51:27 -0800197 Mutex::Autolock lock(mMutex);
198
199 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800200 ST_LOGE("query: BufferQueue has been abandoned!");
Daniel Lamb8560522012-01-30 15:51:27 -0800201 return NO_INIT;
202 }
203
204 int value;
205 switch (what) {
206 case NATIVE_WINDOW_WIDTH:
207 value = mDefaultWidth;
208 break;
209 case NATIVE_WINDOW_HEIGHT:
210 value = mDefaultHeight;
211 break;
212 case NATIVE_WINDOW_FORMAT:
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700213 value = mDefaultBufferFormat;
Daniel Lamb8560522012-01-30 15:51:27 -0800214 break;
215 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700216 value = getMinUndequeuedBufferCount(false);
Daniel Lamb8560522012-01-30 15:51:27 -0800217 break;
Mathias Agopian2488b202012-04-20 17:19:28 -0700218 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
219 value = (mQueue.size() >= 2);
220 break;
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700221 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
222 value = mConsumerUsageBits;
223 break;
Daniel Lamb8560522012-01-30 15:51:27 -0800224 default:
225 return BAD_VALUE;
226 }
227 outValue[0] = value;
228 return NO_ERROR;
229}
230
Daniel Lam6b091c52012-01-22 15:26:27 -0800231status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800232 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800233 ST_LOGV("requestBuffer: slot=%d", slot);
234 Mutex::Autolock lock(mMutex);
235 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800236 ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800237 return NO_INIT;
238 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700239 if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800240 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700241 NUM_BUFFER_SLOTS, slot);
Jamie Gennise191e6c2012-08-24 20:26:34 -0700242 return BAD_VALUE;
243 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennise191e6c2012-08-24 20:26:34 -0700244 ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
245 slot, mSlots[slot].mBufferState);
Daniel Lam6b091c52012-01-22 15:26:27 -0800246 return BAD_VALUE;
247 }
248 mSlots[slot].mRequestBufferCalled = true;
249 *buf = mSlots[slot].mGraphicBuffer;
250 return NO_ERROR;
251}
252
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700253status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700254 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800255 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800256 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
257
258 if ((w && !h) || (!w && h)) {
259 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
260 return BAD_VALUE;
261 }
262
263 status_t returnFlags(OK);
264 EGLDisplay dpy = EGL_NO_DISPLAY;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700265 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800266
267 { // Scope for the lock
268 Mutex::Autolock lock(mMutex);
269
Daniel Lamb2675792012-02-23 14:35:13 -0800270 if (format == 0) {
271 format = mDefaultBufferFormat;
272 }
273 // turn on usage bits the consumer requested
274 usage |= mConsumerUsageBits;
275
Daniel Lam6b091c52012-01-22 15:26:27 -0800276 int found = -1;
Daniel Lam6b091c52012-01-22 15:26:27 -0800277 bool tryAgain = true;
278 while (tryAgain) {
279 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800280 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800281 return NO_INIT;
282 }
283
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700284 const int maxBufferCount = getMaxBufferCountLocked(async);
285 if (async && mOverrideMaxBufferCount) {
286 // FIXME: some drivers are manually setting the buffer-count (which they
287 // shouldn't), so we do this extra test here to handle that case.
288 // This is TEMPORARY, until we get this fixed.
289 if (mOverrideMaxBufferCount < maxBufferCount) {
290 ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
291 return BAD_VALUE;
292 }
293 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800294
Jamie Gennise191e6c2012-08-24 20:26:34 -0700295 // Free up any buffers that are in slots beyond the max buffer
296 // count.
297 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
298 assert(mSlots[i].mBufferState == BufferSlot::FREE);
299 if (mSlots[i].mGraphicBuffer != NULL) {
300 freeBufferLocked(i);
Andy McFadden2adaf042012-12-18 09:49:45 -0800301 returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700302 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800303 }
304
305 // look for a free buffer to give to the client
306 found = INVALID_BUFFER_SLOT;
Mathias Agopian6bac3632013-07-23 21:50:20 -0700307 int dequeuedCount = 0;
308 int acquiredCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700309 for (int i = 0; i < maxBufferCount; i++) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800310 const int state = mSlots[i].mBufferState;
Mathias Agopian6bac3632013-07-23 21:50:20 -0700311 switch (state) {
312 case BufferSlot::DEQUEUED:
313 dequeuedCount++;
314 break;
315 case BufferSlot::ACQUIRED:
316 acquiredCount++;
317 break;
318 case BufferSlot::FREE:
319 /* We return the oldest of the free buffers to avoid
320 * stalling the producer if possible. This is because
321 * the consumer may still have pending reads of the
322 * buffers in flight.
323 */
324 if ((found < 0) ||
325 mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
326 found = i;
327 }
328 break;
Daniel Lam6b091c52012-01-22 15:26:27 -0800329 }
330 }
331
332 // clients are not allowed to dequeue more than one buffer
333 // if they didn't set a buffer count.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700334 if (!mOverrideMaxBufferCount && dequeuedCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800335 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
336 "setting the buffer count");
337 return -EINVAL;
338 }
339
340 // See whether a buffer has been queued since the last
Jamie Gennis72f096f2012-08-27 18:48:37 -0700341 // setBufferCount so we know whether to perform the min undequeued
342 // buffers check below.
Daniel Lameae59d22012-01-22 15:26:27 -0800343 if (mBufferHasBeenQueued) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800344 // make sure the client is not trying to dequeue more buffers
345 // than allowed.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700346 const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700347 const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700348 if (newUndequeuedCount < minUndequeuedCount) {
349 ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
350 "exceeded (dequeued=%d undequeudCount=%d)",
351 minUndequeuedCount, dequeuedCount,
352 newUndequeuedCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800353 return -EBUSY;
354 }
355 }
356
Jamie Gennise191e6c2012-08-24 20:26:34 -0700357 // If no buffer is found, wait for a buffer to be released or for
358 // the max buffer count to change.
Daniel Lamc2c1f2f2012-03-07 14:11:29 -0800359 tryAgain = found == INVALID_BUFFER_SLOT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800360 if (tryAgain) {
Mathias Agopian6bac3632013-07-23 21:50:20 -0700361 // return an error if we're in "cannot block" mode (producer and consumer
362 // are controlled by the application) -- however, the consumer is allowed
363 // to acquire briefly an extra buffer (which could cause us to have to wait here)
364 // and that's okay because we know the wait will be brief (it happens
365 // if we dequeue a buffer while the consumer has acquired one but not released
366 // the old one yet -- for e.g.: see GLConsumer::updateTexImage()).
367 if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) {
Mathias Agopian595264f2013-07-16 22:56:09 -0700368 ST_LOGE("dequeueBuffer: would block! returning an error instead.");
369 return WOULD_BLOCK;
370 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800371 mDequeueCondition.wait(mMutex);
372 }
373 }
374
Daniel Lam6b091c52012-01-22 15:26:27 -0800375
376 if (found == INVALID_BUFFER_SLOT) {
377 // This should not happen.
378 ST_LOGE("dequeueBuffer: no available buffer slots");
379 return -EBUSY;
380 }
381
382 const int buf = found;
383 *outBuf = found;
384
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800385 ATRACE_BUFFER_INDEX(buf);
386
Daniel Lam6b091c52012-01-22 15:26:27 -0800387 const bool useDefaultSize = !w && !h;
388 if (useDefaultSize) {
389 // use the default size
390 w = mDefaultWidth;
391 h = mDefaultHeight;
392 }
393
Daniel Lam6b091c52012-01-22 15:26:27 -0800394 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
395
396 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
397 if ((buffer == NULL) ||
398 (uint32_t(buffer->width) != w) ||
399 (uint32_t(buffer->height) != h) ||
400 (uint32_t(buffer->format) != format) ||
401 ((uint32_t(buffer->usage) & usage) != usage))
402 {
Daniel Lameae59d22012-01-22 15:26:27 -0800403 mSlots[buf].mAcquireCalled = false;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700404 mSlots[buf].mGraphicBuffer = NULL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800405 mSlots[buf].mRequestBufferCalled = false;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700406 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800407 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800408 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
409
Andy McFadden2adaf042012-12-18 09:49:45 -0800410 returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
Daniel Lam6b091c52012-01-22 15:26:27 -0800411 }
412
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700413
414 if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) {
415 ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. "
416 "buf=%d, w=%d, h=%d, format=%d",
417 buf, buffer->width, buffer->height, buffer->format);
418 }
419
Daniel Lam6b091c52012-01-22 15:26:27 -0800420 dpy = mSlots[buf].mEglDisplay;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700421 eglFence = mSlots[buf].mEglFence;
Jesse Hall4c00cc12013-03-15 21:34:30 -0700422 *outFence = mSlots[buf].mFence;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700423 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800424 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800425 } // end lock scope
Daniel Lam6b091c52012-01-22 15:26:27 -0800426
Andy McFadden2adaf042012-12-18 09:49:45 -0800427 if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
Jamie Gennis1efe0992012-10-04 18:34:01 -0700428 status_t error;
429 sp<GraphicBuffer> graphicBuffer(
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700430 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error));
Jamie Gennis1efe0992012-10-04 18:34:01 -0700431 if (graphicBuffer == 0) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700432 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700433 return error;
434 }
435
436 { // Scope for the lock
437 Mutex::Autolock lock(mMutex);
438
439 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800440 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700441 return NO_INIT;
442 }
443
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700444 mSlots[*outBuf].mFrameNumber = ~0;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700445 mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
446 }
447 }
448
Jesse Hallc777b0b2012-06-28 12:52:05 -0700449 if (eglFence != EGL_NO_SYNC_KHR) {
450 EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
Daniel Lam6b091c52012-01-22 15:26:27 -0800451 // If something goes wrong, log the error, but return the buffer without
452 // synchronizing access to it. It's too late at this point to abort the
453 // dequeue operation.
454 if (result == EGL_FALSE) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700455 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
Daniel Lam6b091c52012-01-22 15:26:27 -0800456 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700457 ST_LOGE("dequeueBuffer: timeout waiting for fence");
Daniel Lam6b091c52012-01-22 15:26:27 -0800458 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700459 eglDestroySyncKHR(dpy, eglFence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800460 }
461
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700462 ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
463 mSlots[*outBuf].mFrameNumber,
Daniel Lam6b091c52012-01-22 15:26:27 -0800464 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
465
466 return returnFlags;
467}
468
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700469status_t BufferQueue::queueBuffer(int buf,
470 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800471 ATRACE_CALL();
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800472 ATRACE_BUFFER_INDEX(buf);
473
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700474 Rect crop;
475 uint32_t transform;
476 int scalingMode;
477 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700478 bool isAutoTimestamp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700479 bool async;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700480 sp<Fence> fence;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700481
Andy McFadden3c256212013-08-16 14:55:39 -0700482 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode, &transform,
483 &async, &fence);
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700484
Jamie Gennis1df8c342012-12-20 14:05:45 -0800485 if (fence == NULL) {
486 ST_LOGE("queueBuffer: fence is NULL");
487 return BAD_VALUE;
488 }
489
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700490 switch (scalingMode) {
491 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
492 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
493 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
494 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
495 break;
496 default:
497 ST_LOGE("unknown scaling mode: %d", scalingMode);
498 return -EINVAL;
499 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800500
Mathias Agopiana4e19522013-07-31 20:09:53 -0700501 sp<IConsumerListener> listener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800502
503 { // scope for the lock
504 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700505
Daniel Lam6b091c52012-01-22 15:26:27 -0800506 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800507 ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800508 return NO_INIT;
509 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700510
511 const int maxBufferCount = getMaxBufferCountLocked(async);
512 if (async && mOverrideMaxBufferCount) {
513 // FIXME: some drivers are manually setting the buffer-count (which they
514 // shouldn't), so we do this extra test here to handle that case.
515 // This is TEMPORARY, until we get this fixed.
516 if (mOverrideMaxBufferCount < maxBufferCount) {
517 ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
518 return BAD_VALUE;
519 }
520 }
Jamie Gennise191e6c2012-08-24 20:26:34 -0700521 if (buf < 0 || buf >= maxBufferCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800522 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennise191e6c2012-08-24 20:26:34 -0700523 maxBufferCount, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800524 return -EINVAL;
525 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
526 ST_LOGE("queueBuffer: slot %d is not owned by the client "
527 "(state=%d)", buf, mSlots[buf].mBufferState);
528 return -EINVAL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800529 } else if (!mSlots[buf].mRequestBufferCalled) {
530 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
531 "buffer", buf);
532 return -EINVAL;
533 }
534
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700535 ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
536 "tr=%#x scale=%s",
537 buf, mFrameCounter + 1, timestamp,
538 crop.left, crop.top, crop.right, crop.bottom,
539 transform, scalingModeName(scalingMode));
540
Jamie Gennisd72f2332012-05-07 13:50:11 -0700541 const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
542 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
543 Rect croppedCrop;
544 crop.intersect(bufferRect, &croppedCrop);
545 if (croppedCrop != crop) {
546 ST_LOGE("queueBuffer: crop rect is not contained within the "
547 "buffer in slot %d", buf);
548 return -EINVAL;
549 }
550
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700551 mSlots[buf].mFence = fence;
552 mSlots[buf].mBufferState = BufferSlot::QUEUED;
553 mFrameCounter++;
554 mSlots[buf].mFrameNumber = mFrameCounter;
555
556 BufferItem item;
557 item.mAcquireCalled = mSlots[buf].mAcquireCalled;
558 item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
559 item.mCrop = crop;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700560 item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
561 item.mTransformToDisplayInverse = bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700562 item.mScalingMode = scalingMode;
563 item.mTimestamp = timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700564 item.mIsAutoTimestamp = isAutoTimestamp;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700565 item.mFrameNumber = mFrameCounter;
566 item.mBuf = buf;
567 item.mFence = fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700568 item.mIsDroppable = mDequeueBufferCannotBlock || async;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700569
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700570 if (mQueue.empty()) {
571 // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
572 // simply queue this buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700573 mQueue.push_back(item);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700574 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800575 } else {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700576 // when the queue is not empty, we need to look at the front buffer
577 // state and see if we need to replace it.
578 Fifo::iterator front(mQueue.begin());
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700579 if (front->mIsDroppable) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700580 // buffer slot currently queued is marked free if still tracked
581 if (stillTracking(front)) {
582 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
Mathias Agopian26a6f372013-07-18 22:25:55 -0700583 // reset the frame number of the freed buffer so that it is the first in
584 // line to be dequeued again.
585 mSlots[front->mBuf].mFrameNumber = 0;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700586 }
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700587 // and we record the new buffer in the queued list
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700588 *front = item;
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700589 } else {
590 mQueue.push_back(item);
591 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800592 }
593 }
594
Daniel Lameae59d22012-01-22 15:26:27 -0800595 mBufferHasBeenQueued = true;
Dave Burke74ff8c22012-03-12 21:49:41 -0700596 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800597
Mathias Agopian2488b202012-04-20 17:19:28 -0700598 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
599 mQueue.size());
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800600
601 ATRACE_INT(mConsumerName.string(), mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800602 } // scope for the lock
603
604 // call back without lock held
605 if (listener != 0) {
606 listener->onFrameAvailable();
607 }
Andy McFadden753e3412013-04-04 17:09:03 -0700608 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800609}
610
Jesse Hall4c00cc12013-03-15 21:34:30 -0700611void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800612 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800613 ST_LOGV("cancelBuffer: slot=%d", buf);
614 Mutex::Autolock lock(mMutex);
615
616 if (mAbandoned) {
617 ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
618 return;
619 }
620
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700621 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800622 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700623 NUM_BUFFER_SLOTS, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800624 return;
625 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
626 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
627 buf, mSlots[buf].mBufferState);
628 return;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800629 } else if (fence == NULL) {
630 ST_LOGE("cancelBuffer: fence is NULL");
631 return;
Daniel Lam6b091c52012-01-22 15:26:27 -0800632 }
633 mSlots[buf].mBufferState = BufferSlot::FREE;
634 mSlots[buf].mFrameNumber = 0;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700635 mSlots[buf].mFence = fence;
Dave Burke74ff8c22012-03-12 21:49:41 -0700636 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800637}
638
Mathias Agopian365857d2013-09-11 19:35:45 -0700639
640status_t BufferQueue::connect(const sp<IBinder>& token,
641 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800642 ATRACE_CALL();
Jesse Hall8db92552013-08-29 16:03:50 -0700643 ST_LOGV("connect: api=%d producerControlledByApp=%s", api,
644 producerControlledByApp ? "true" : "false");
Daniel Lam6b091c52012-01-22 15:26:27 -0800645 Mutex::Autolock lock(mMutex);
646
647 if (mAbandoned) {
648 ST_LOGE("connect: BufferQueue has been abandoned!");
649 return NO_INIT;
650 }
651
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700652 if (mConsumerListener == NULL) {
653 ST_LOGE("connect: BufferQueue has no consumer!");
654 return NO_INIT;
655 }
656
Daniel Lam6b091c52012-01-22 15:26:27 -0800657 int err = NO_ERROR;
658 switch (api) {
659 case NATIVE_WINDOW_API_EGL:
660 case NATIVE_WINDOW_API_CPU:
661 case NATIVE_WINDOW_API_MEDIA:
662 case NATIVE_WINDOW_API_CAMERA:
663 if (mConnectedApi != NO_CONNECTED_API) {
664 ST_LOGE("connect: already connected (cur=%d, req=%d)",
665 mConnectedApi, api);
666 err = -EINVAL;
667 } else {
668 mConnectedApi = api;
Mathias Agopian365857d2013-09-11 19:35:45 -0700669 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, mQueue.size());
670
671 // set-up a death notification so that we can disconnect automatically
672 // when/if the remote producer dies.
673 // This will fail with INVALID_OPERATION if the "token" is local to our process.
674 if (token->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)) == NO_ERROR) {
675 mConnectedProducerToken = token;
676 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800677 }
678 break;
679 default:
680 err = -EINVAL;
681 break;
682 }
Daniel Lameae59d22012-01-22 15:26:27 -0800683
684 mBufferHasBeenQueued = false;
Mathias Agopian595264f2013-07-16 22:56:09 -0700685 mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
Daniel Lameae59d22012-01-22 15:26:27 -0800686
Daniel Lam6b091c52012-01-22 15:26:27 -0800687 return err;
688}
689
Mathias Agopian365857d2013-09-11 19:35:45 -0700690void BufferQueue::binderDied(const wp<IBinder>& who) {
691 // If we're here, it means that a producer we were connected to died.
692 // We're GUARANTEED that we still are connected to it because it has no other way
693 // to get disconnected -- or -- we wouldn't be here because we're removing this
694 // callback upon disconnect. Therefore, it's okay to read mConnectedApi without
695 // synchronization here.
696 int api = mConnectedApi;
697 this->disconnect(api);
698}
699
Daniel Lam6b091c52012-01-22 15:26:27 -0800700status_t BufferQueue::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800701 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800702 ST_LOGV("disconnect: api=%d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800703
704 int err = NO_ERROR;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700705 sp<IConsumerListener> listener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700706
707 { // Scope for the lock
708 Mutex::Autolock lock(mMutex);
709
710 if (mAbandoned) {
711 // it is not really an error to disconnect after the surface
712 // has been abandoned, it should just be a no-op.
713 return NO_ERROR;
714 }
715
716 switch (api) {
717 case NATIVE_WINDOW_API_EGL:
718 case NATIVE_WINDOW_API_CPU:
719 case NATIVE_WINDOW_API_MEDIA:
720 case NATIVE_WINDOW_API_CAMERA:
721 if (mConnectedApi == api) {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700722 freeAllBuffersLocked();
Mathias Agopian365857d2013-09-11 19:35:45 -0700723 // remove our death notification callback if we have one
724 sp<IBinder> token = mConnectedProducerToken;
725 if (token != NULL) {
726 // this can fail if we're here because of the death notification
727 // either way, we just ignore.
728 token->unlinkToDeath(static_cast<IBinder::DeathRecipient*>(this));
729 }
730 mConnectedProducerToken = NULL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700731 mConnectedApi = NO_CONNECTED_API;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700732 mDequeueCondition.broadcast();
733 listener = mConsumerListener;
734 } else {
735 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
736 mConnectedApi, api);
737 err = -EINVAL;
738 }
739 break;
740 default:
741 ST_LOGE("disconnect: unknown API %d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800742 err = -EINVAL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700743 break;
744 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800745 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700746
747 if (listener != NULL) {
748 listener->onBuffersReleased();
749 }
750
Daniel Lam6b091c52012-01-22 15:26:27 -0800751 return err;
752}
753
Mathias Agopian74d211a2013-04-22 16:55:35 +0200754void BufferQueue::dump(String8& result, const char* prefix) const {
Daniel Lameae59d22012-01-22 15:26:27 -0800755 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800756
757 String8 fifo;
758 int fifoSize = 0;
759 Fifo::const_iterator i(mQueue.begin());
760 while (i != mQueue.end()) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700761 fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
762 "xform=0x%02x, time=%#llx, scale=%s\n",
763 i->mBuf, i->mGraphicBuffer.get(),
764 i->mCrop.left, i->mCrop.top, i->mCrop.right,
765 i->mCrop.bottom, i->mTransform, i->mTimestamp,
766 scalingModeName(i->mScalingMode)
767 );
768 i++;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200769 fifoSize++;
Daniel Lameae59d22012-01-22 15:26:27 -0800770 }
771
Jamie Gennise191e6c2012-08-24 20:26:34 -0700772
Mathias Agopian74d211a2013-04-22 16:55:35 +0200773 result.appendFormat(
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700774 "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
Andy McFadden69052052012-09-14 16:10:11 -0700775 "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700776 prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
Andy McFadden69052052012-09-14 16:10:11 -0700777 mDefaultHeight, mDefaultBufferFormat, mTransformHint,
778 fifoSize, fifo.string());
Daniel Lameae59d22012-01-22 15:26:27 -0800779
780 struct {
781 const char * operator()(int state) const {
782 switch (state) {
783 case BufferSlot::DEQUEUED: return "DEQUEUED";
784 case BufferSlot::QUEUED: return "QUEUED";
785 case BufferSlot::FREE: return "FREE";
786 case BufferSlot::ACQUIRED: return "ACQUIRED";
787 default: return "Unknown";
788 }
789 }
790 } stateName;
791
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700792 // just trim the free buffers to not spam the dump
793 int maxBufferCount = 0;
794 for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
795 const BufferSlot& slot(mSlots[i]);
796 if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
797 maxBufferCount = i+1;
798 break;
799 }
800 }
801
Jamie Gennise191e6c2012-08-24 20:26:34 -0700802 for (int i=0 ; i<maxBufferCount ; i++) {
Daniel Lameae59d22012-01-22 15:26:27 -0800803 const BufferSlot& slot(mSlots[i]);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700804 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200805 result.appendFormat(
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700806 "%s%s[%02d:%p] state=%-8s",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700807 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700808 stateName(slot.mBufferState)
Daniel Lameae59d22012-01-22 15:26:27 -0800809 );
Daniel Lameae59d22012-01-22 15:26:27 -0800810
Daniel Lameae59d22012-01-22 15:26:27 -0800811 if (buf != NULL) {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200812 result.appendFormat(
Daniel Lameae59d22012-01-22 15:26:27 -0800813 ", %p [%4ux%4u:%4u,%3X]",
814 buf->handle, buf->width, buf->height, buf->stride,
815 buf->format);
Daniel Lameae59d22012-01-22 15:26:27 -0800816 }
817 result.append("\n");
818 }
819}
820
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700821void BufferQueue::freeBufferLocked(int slot) {
822 ST_LOGV("freeBufferLocked: slot=%d", slot);
823 mSlots[slot].mGraphicBuffer = 0;
824 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
825 mSlots[slot].mNeedsCleanupOnRelease = true;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700826 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700827 mSlots[slot].mBufferState = BufferSlot::FREE;
828 mSlots[slot].mFrameNumber = 0;
829 mSlots[slot].mAcquireCalled = false;
Daniel Lameae59d22012-01-22 15:26:27 -0800830
831 // destroy fence as BufferQueue now takes ownership
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700832 if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
833 eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
834 mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800835 }
Jamie Gennis1df8c342012-12-20 14:05:45 -0800836 mSlots[slot].mFence = Fence::NO_FENCE;
Daniel Lam6b091c52012-01-22 15:26:27 -0800837}
838
839void BufferQueue::freeAllBuffersLocked() {
Daniel Lameae59d22012-01-22 15:26:27 -0800840 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800841 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
842 freeBufferLocked(i);
843 }
844}
845
Andy McFadden14fab7d2013-08-01 13:37:42 -0700846status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800847 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -0800848 Mutex::Autolock _l(mMutex);
Jamie Gennis5e5efde2012-08-28 17:18:50 -0700849
850 // Check that the consumer doesn't currently have the maximum number of
851 // buffers acquired. We allow the max buffer count to be exceeded by one
852 // buffer, so that the consumer can successfully set up the newly acquired
853 // buffer before releasing the old one.
854 int numAcquiredBuffers = 0;
855 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
856 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
857 numAcquiredBuffers++;
858 }
859 }
860 if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
861 ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
862 numAcquiredBuffers, mMaxAcquiredBufferCount);
863 return INVALID_OPERATION;
864 }
865
Daniel Lameae59d22012-01-22 15:26:27 -0800866 // check if queue is empty
867 // In asynchronous mode the list is guaranteed to be one buffer
868 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700869 if (mQueue.empty()) {
Daniel Lamfbcda932012-04-09 22:51:52 -0700870 return NO_BUFFER_AVAILABLE;
Daniel Lameae59d22012-01-22 15:26:27 -0800871 }
872
Andy McFadden1585c4d2013-06-28 13:52:40 -0700873 Fifo::iterator front(mQueue.begin());
Andy McFadden1585c4d2013-06-28 13:52:40 -0700874
Andy McFadden14fab7d2013-08-01 13:37:42 -0700875 // If expectedPresent is specified, we may not want to return a buffer yet.
876 // If it's specified and there's more than one buffer queued, we may
877 // want to drop a buffer.
878 if (expectedPresent != 0) {
879 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
880
881 // The "expectedPresent" argument indicates when the buffer is expected
882 // to be presented on-screen. If the buffer's desired-present time
883 // is earlier (less) than expectedPresent, meaning it'll be displayed
884 // on time or possibly late if we show it ASAP, we acquire and return
885 // it. If we don't want to display it until after the expectedPresent
886 // time, we return PRESENT_LATER without acquiring it.
887 //
888 // To be safe, we don't defer acquisition if expectedPresent is
889 // more than one second in the future beyond the desired present time
890 // (i.e. we'd be holding the buffer for a long time).
891 //
892 // NOTE: code assumes monotonic time values from the system clock are
893 // positive.
Andy McFadden3c256212013-08-16 14:55:39 -0700894
895 // Start by checking to see if we can drop frames. We skip this check
896 // if the timestamps are being auto-generated by Surface -- if the
897 // app isn't generating timestamps explicitly, they probably don't
898 // want frames to be discarded based on them.
899 while (mQueue.size() > 1 && !mQueue[0].mIsAutoTimestamp) {
Andy McFadden14fab7d2013-08-01 13:37:42 -0700900 // If entry[1] is timely, drop entry[0] (and repeat). We apply
901 // an additional criteria here: we only drop the earlier buffer if
902 // our desiredPresent falls within +/- 1 second of the expected
903 // present. Otherwise, bogus desiredPresent times (e.g. 0 or
904 // a small relative timestamp), which normally mean "ignore the
905 // timestamp and acquire immediately", would cause us to drop
906 // frames.
907 //
908 // We may want to add an additional criteria: don't drop the
909 // earlier buffer if entry[1]'s fence hasn't signaled yet.
910 //
911 // (Vector front is [0], back is [size()-1])
912 const BufferItem& bi(mQueue[1]);
913 nsecs_t desiredPresent = bi.mTimestamp;
914 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
915 desiredPresent > expectedPresent) {
916 // This buffer is set to display in the near future, or
917 // desiredPresent is garbage. Either way we don't want to
918 // drop the previous buffer just to get this on screen sooner.
919 ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld",
920 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
921 systemTime(CLOCK_MONOTONIC));
922 break;
923 }
924 ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d",
925 desiredPresent, expectedPresent, mQueue.size());
926 if (stillTracking(front)) {
927 // front buffer is still in mSlots, so mark the slot as free
928 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
929 }
930 mQueue.erase(front);
931 front = mQueue.begin();
932 }
933
934 // See if the front buffer is due.
935 nsecs_t desiredPresent = front->mTimestamp;
936 if (desiredPresent > expectedPresent &&
937 desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
938 ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld",
939 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
940 systemTime(CLOCK_MONOTONIC));
941 return PRESENT_LATER;
942 }
943
944 ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld",
945 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700946 systemTime(CLOCK_MONOTONIC));
Andy McFadden1585c4d2013-06-28 13:52:40 -0700947 }
948
Andy McFadden14fab7d2013-08-01 13:37:42 -0700949 int buf = front->mBuf;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700950 *buffer = *front;
951 ATRACE_BUFFER_INDEX(buf);
952
953 ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
954 front->mBuf, front->mFrameNumber,
955 front->mGraphicBuffer->handle);
956 // if front buffer still being tracked update slot state
957 if (stillTracking(front)) {
958 mSlots[buf].mAcquireCalled = true;
959 mSlots[buf].mNeedsCleanupOnRelease = false;
960 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
961 mSlots[buf].mFence = Fence::NO_FENCE;
962 }
963
964 // If the buffer has previously been acquired by the consumer, set
965 // mGraphicBuffer to NULL to avoid unnecessarily remapping this
966 // buffer on the consumer side.
967 if (buffer->mAcquireCalled) {
968 buffer->mGraphicBuffer = NULL;
969 }
970
971 mQueue.erase(front);
972 mDequeueCondition.broadcast();
973
974 ATRACE_INT(mConsumerName.string(), mQueue.size());
975
Andy McFadden753e3412013-04-04 17:09:03 -0700976 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800977}
978
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700979status_t BufferQueue::releaseBuffer(
980 int buf, uint64_t frameNumber, EGLDisplay display,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700981 EGLSyncKHR eglFence, const sp<Fence>& fence) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800982 ATRACE_CALL();
983 ATRACE_BUFFER_INDEX(buf);
984
Jamie Gennis1df8c342012-12-20 14:05:45 -0800985 if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
986 return BAD_VALUE;
Daniel Lameae59d22012-01-22 15:26:27 -0800987 }
988
Mathias Agopian207c1e22013-07-22 18:00:53 -0700989 Mutex::Autolock _l(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700990
991 // If the frame number has changed because buffer has been reallocated,
992 // we can ignore this releaseBuffer for the old buffer.
993 if (frameNumber != mSlots[buf].mFrameNumber) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700994 return STALE_BUFFER_SLOT;
995 }
Mathias Agopian207c1e22013-07-22 18:00:53 -0700996
997
998 // Internal state consistency checks:
999 // Make sure this buffers hasn't been queued while we were owning it (acquired)
1000 Fifo::iterator front(mQueue.begin());
1001 Fifo::const_iterator const end(mQueue.end());
1002 while (front != end) {
1003 if (front->mBuf == buf) {
1004 LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
1005 "acquired", mConsumerName.string(), frameNumber, buf);
1006 break; // never reached
1007 }
1008 front++;
1009 }
Daniel Lameae59d22012-01-22 15:26:27 -08001010
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001011 // The buffer can now only be released if its in the acquired state
1012 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001013 mSlots[buf].mEglDisplay = display;
1014 mSlots[buf].mEglFence = eglFence;
1015 mSlots[buf].mFence = fence;
Daniel Lameae59d22012-01-22 15:26:27 -08001016 mSlots[buf].mBufferState = BufferSlot::FREE;
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001017 } else if (mSlots[buf].mNeedsCleanupOnRelease) {
1018 ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
1019 mSlots[buf].mNeedsCleanupOnRelease = false;
1020 return STALE_BUFFER_SLOT;
1021 } else {
1022 ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
1023 return -EINVAL;
Daniel Lameae59d22012-01-22 15:26:27 -08001024 }
Daniel Lameae59d22012-01-22 15:26:27 -08001025
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001026 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -07001027 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001028}
1029
Mathias Agopiana4e19522013-07-31 20:09:53 -07001030status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener,
Mathias Agopian595264f2013-07-16 22:56:09 -07001031 bool controlledByApp) {
Jesse Hall8db92552013-08-29 16:03:50 -07001032 ST_LOGV("consumerConnect controlledByApp=%s",
1033 controlledByApp ? "true" : "false");
Daniel Lameae59d22012-01-22 15:26:27 -08001034 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -08001035
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001036 if (mAbandoned) {
1037 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
1038 return NO_INIT;
1039 }
Andy McFadden753e3412013-04-04 17:09:03 -07001040 if (consumerListener == NULL) {
1041 ST_LOGE("consumerConnect: consumerListener may not be NULL");
1042 return BAD_VALUE;
1043 }
Daniel Lamb2675792012-02-23 14:35:13 -08001044
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001045 mConsumerListener = consumerListener;
Mathias Agopian595264f2013-07-16 22:56:09 -07001046 mConsumerControlledByApp = controlledByApp;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001047
Andy McFadden753e3412013-04-04 17:09:03 -07001048 return NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001049}
1050
1051status_t BufferQueue::consumerDisconnect() {
1052 ST_LOGV("consumerDisconnect");
1053 Mutex::Autolock lock(mMutex);
1054
1055 if (mConsumerListener == NULL) {
1056 ST_LOGE("consumerDisconnect: No consumer is connected!");
1057 return -EINVAL;
1058 }
1059
1060 mAbandoned = true;
1061 mConsumerListener = NULL;
Daniel Lamb2675792012-02-23 14:35:13 -08001062 mQueue.clear();
Daniel Lameae59d22012-01-22 15:26:27 -08001063 freeAllBuffersLocked();
Dave Burke74ff8c22012-03-12 21:49:41 -07001064 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -07001065 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001066}
1067
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001068status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
1069 ST_LOGV("getReleasedBuffers");
1070 Mutex::Autolock lock(mMutex);
1071
1072 if (mAbandoned) {
1073 ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
1074 return NO_INIT;
1075 }
1076
1077 uint32_t mask = 0;
1078 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1079 if (!mSlots[i].mAcquireCalled) {
1080 mask |= 1 << i;
1081 }
1082 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001083
1084 // Remove buffers in flight (on the queue) from the mask where acquire has
1085 // been called, as the consumer will not receive the buffer address, so
1086 // it should not free these slots.
1087 Fifo::iterator front(mQueue.begin());
1088 while (front != mQueue.end()) {
1089 if (front->mAcquireCalled)
1090 mask &= ~(1 << front->mBuf);
1091 front++;
1092 }
1093
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001094 *slotMask = mask;
1095
1096 ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1097 return NO_ERROR;
1098}
1099
Mathias Agopian207c1e22013-07-22 18:00:53 -07001100status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
Daniel Lameae59d22012-01-22 15:26:27 -08001101 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1102 if (!w || !h) {
1103 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1104 w, h);
1105 return BAD_VALUE;
1106 }
1107
1108 Mutex::Autolock lock(mMutex);
1109 mDefaultWidth = w;
1110 mDefaultHeight = h;
Andy McFadden753e3412013-04-04 17:09:03 -07001111 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001112}
1113
Jamie Gennis31a353d2012-08-24 17:25:13 -07001114status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001115 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -08001116 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -07001117 return setDefaultMaxBufferCountLocked(bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -08001118}
1119
Mathias Agopianad678e12013-07-23 17:28:53 -07001120status_t BufferQueue::disableAsyncBuffer() {
1121 ATRACE_CALL();
1122 Mutex::Autolock lock(mMutex);
1123 if (mConsumerListener != NULL) {
1124 ST_LOGE("disableAsyncBuffer: consumer already connected!");
1125 return INVALID_OPERATION;
1126 }
1127 mUseAsyncBuffer = false;
1128 return NO_ERROR;
1129}
1130
Jamie Gennis72f096f2012-08-27 18:48:37 -07001131status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1132 ATRACE_CALL();
1133 Mutex::Autolock lock(mMutex);
Jamie Gennisc68f2ec2012-08-30 18:36:22 -07001134 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1135 ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1136 maxAcquiredBuffers);
1137 return BAD_VALUE;
1138 }
Jamie Gennis72f096f2012-08-27 18:48:37 -07001139 if (mConnectedApi != NO_CONNECTED_API) {
1140 return INVALID_OPERATION;
1141 }
1142 mMaxAcquiredBufferCount = maxAcquiredBuffers;
Andy McFadden753e3412013-04-04 17:09:03 -07001143 return NO_ERROR;
Jamie Gennis72f096f2012-08-27 18:48:37 -07001144}
1145
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001146int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
Mathias Agopianad678e12013-07-23 17:28:53 -07001147 // if dequeueBuffer is allowed to error out, we don't have to
1148 // add an extra buffer.
1149 if (!mUseAsyncBuffer)
1150 return mMaxAcquiredBufferCount;
1151
1152 // we're in async mode, or we want to prevent the app to
1153 // deadlock itself, we throw-in an extra buffer to guarantee it.
1154 if (mDequeueBufferCannotBlock || async)
1155 return mMaxAcquiredBufferCount+1;
1156
1157 return mMaxAcquiredBufferCount;
Jamie Gennis31a353d2012-08-24 17:25:13 -07001158}
1159
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001160int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
1161 return getMinUndequeuedBufferCount(async) + 1;
1162}
1163
1164int BufferQueue::getMaxBufferCountLocked(bool async) const {
1165 int minMaxBufferCount = getMinMaxBufferCountLocked(async);
Jamie Gennise191e6c2012-08-24 20:26:34 -07001166
1167 int maxBufferCount = mDefaultMaxBufferCount;
1168 if (maxBufferCount < minMaxBufferCount) {
1169 maxBufferCount = minMaxBufferCount;
1170 }
1171 if (mOverrideMaxBufferCount != 0) {
1172 assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1173 maxBufferCount = mOverrideMaxBufferCount;
1174 }
1175
1176 // Any buffers that are dequeued by the producer or sitting in the queue
1177 // waiting to be consumed need to have their slots preserved. Such
1178 // buffers will temporarily keep the max buffer count up until the slots
1179 // no longer need to be preserved.
1180 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1181 BufferSlot::BufferState state = mSlots[i].mBufferState;
1182 if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1183 maxBufferCount = i + 1;
1184 }
1185 }
1186
1187 return maxBufferCount;
1188}
1189
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001190bool BufferQueue::stillTracking(const BufferItem *item) const {
1191 const BufferSlot &slot = mSlots[item->mBuf];
1192
1193 ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1194 "slot: { slot=%d/%llu, buffer=%p }",
1195 item->mBuf, item->mFrameNumber,
1196 (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1197 item->mBuf, slot.mFrameNumber,
1198 (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1199
1200 // Compare item with its original buffer slot. We can check the slot
1201 // as the buffer would not be moved to a different slot by the producer.
1202 return (slot.mGraphicBuffer != NULL &&
1203 item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1204}
1205
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001206BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
Mathias Agopiana4e19522013-07-31 20:09:53 -07001207 const wp<ConsumerListener>& consumerListener):
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001208 mConsumerListener(consumerListener) {}
1209
1210BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1211
1212void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
Mathias Agopiana4e19522013-07-31 20:09:53 -07001213 sp<ConsumerListener> listener(mConsumerListener.promote());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001214 if (listener != NULL) {
1215 listener->onFrameAvailable();
1216 }
1217}
1218
1219void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
Mathias Agopiana4e19522013-07-31 20:09:53 -07001220 sp<ConsumerListener> listener(mConsumerListener.promote());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001221 if (listener != NULL) {
1222 listener->onBuffersReleased();
1223 }
1224}
1225
Daniel Lam6b091c52012-01-22 15:26:27 -08001226}; // namespace android