blob: 0e6fb5cce4a03d4d567662d479fe19cb97e8aab4 [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>
Daniel Lam6b091c52012-01-22 15:26:27 -080034
Daniel Lam6b091c52012-01-22 15:26:27 -080035// Macros for including the BufferQueue name in log messages
Daniel Lameae59d22012-01-22 15:26:27 -080036#define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
37#define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
38#define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
39#define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
40#define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
Daniel Lam6b091c52012-01-22 15:26:27 -080041
Mathias Agopian546ed2d2012-03-01 22:11:25 -080042#define ATRACE_BUFFER_INDEX(index) \
Jamie Gennis695e3312012-04-16 20:34:58 -070043 if (ATRACE_ENABLED()) { \
44 char ___traceBuf[1024]; \
45 snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), \
46 (index)); \
47 android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
48 }
Mathias Agopian546ed2d2012-03-01 22:11:25 -080049
Daniel Lam6b091c52012-01-22 15:26:27 -080050namespace android {
51
52// Get an ID that's unique within this process.
53static int32_t createProcessUniqueId() {
54 static volatile int32_t globalCounter = 0;
55 return android_atomic_inc(&globalCounter);
56}
57
Jamie Genniscd1806e2012-05-10 02:22:33 -070058static const char* scalingModeName(int scalingMode) {
59 switch (scalingMode) {
60 case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
61 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
62 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
63 default: return "Unknown";
64 }
65}
66
Mathias Agopian595264f2013-07-16 22:56:09 -070067BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) :
Daniel Lam6b091c52012-01-22 15:26:27 -080068 mDefaultWidth(1),
69 mDefaultHeight(1),
Jamie Gennis72f096f2012-08-27 18:48:37 -070070 mMaxAcquiredBufferCount(1),
71 mDefaultMaxBufferCount(2),
Jamie Gennis31a353d2012-08-24 17:25:13 -070072 mOverrideMaxBufferCount(0),
Mathias Agopian595264f2013-07-16 22:56:09 -070073 mConsumerControlledByApp(false),
74 mDequeueBufferCannotBlock(false),
Mathias Agopianad678e12013-07-23 17:28:53 -070075 mUseAsyncBuffer(true),
Daniel Lam6b091c52012-01-22 15:26:27 -080076 mConnectedApi(NO_CONNECTED_API),
77 mAbandoned(false),
Daniel Lameae59d22012-01-22 15:26:27 -080078 mFrameCounter(0),
Daniel Lamb2675792012-02-23 14:35:13 -080079 mBufferHasBeenQueued(false),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070080 mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
Daniel Lamb2675792012-02-23 14:35:13 -080081 mConsumerUsageBits(0),
82 mTransformHint(0)
Daniel Lam6b091c52012-01-22 15:26:27 -080083{
84 // Choose a name using the PID and a process-unique ID.
Daniel Lameae59d22012-01-22 15:26:27 -080085 mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Daniel Lam6b091c52012-01-22 15:26:27 -080086
87 ST_LOGV("BufferQueue");
Mathias Agopian3e876012012-06-07 17:52:54 -070088 if (allocator == NULL) {
89 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
90 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
91 if (mGraphicBufferAlloc == 0) {
92 ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
93 }
94 } else {
95 mGraphicBufferAlloc = allocator;
Daniel Lamabe61bf2012-03-26 20:37:15 -070096 }
Daniel Lam6b091c52012-01-22 15:26:27 -080097}
98
99BufferQueue::~BufferQueue() {
100 ST_LOGV("~BufferQueue");
101}
102
Jamie Gennis31a353d2012-08-24 17:25:13 -0700103status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700104 const int minBufferCount = mUseAsyncBuffer ? 2 : 1;
105 if (count < minBufferCount || count > NUM_BUFFER_SLOTS)
Daniel Lam6b091c52012-01-22 15:26:27 -0800106 return BAD_VALUE;
107
Jamie Gennis31a353d2012-08-24 17:25:13 -0700108 mDefaultMaxBufferCount = count;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700109 mDequeueCondition.broadcast();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700110
Andy McFadden753e3412013-04-04 17:09:03 -0700111 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800112}
113
Daniel Lameae59d22012-01-22 15:26:27 -0800114void BufferQueue::setConsumerName(const String8& name) {
115 Mutex::Autolock lock(mMutex);
116 mConsumerName = name;
117}
118
Daniel Lamb2675792012-02-23 14:35:13 -0800119status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
120 Mutex::Autolock lock(mMutex);
121 mDefaultBufferFormat = defaultFormat;
Andy McFadden753e3412013-04-04 17:09:03 -0700122 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800123}
124
125status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
126 Mutex::Autolock lock(mMutex);
127 mConsumerUsageBits = usage;
Andy McFadden753e3412013-04-04 17:09:03 -0700128 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800129}
130
131status_t BufferQueue::setTransformHint(uint32_t hint) {
Andy McFadden69052052012-09-14 16:10:11 -0700132 ST_LOGV("setTransformHint: %02x", hint);
Daniel Lamb2675792012-02-23 14:35:13 -0800133 Mutex::Autolock lock(mMutex);
134 mTransformHint = hint;
Andy McFadden753e3412013-04-04 17:09:03 -0700135 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800136}
137
Daniel Lam6b091c52012-01-22 15:26:27 -0800138status_t BufferQueue::setBufferCount(int bufferCount) {
139 ST_LOGV("setBufferCount: count=%d", bufferCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800140
Mathias Agopiana4e19522013-07-31 20:09:53 -0700141 sp<IConsumerListener> listener;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700142 {
143 Mutex::Autolock lock(mMutex);
Daniel Lam6b091c52012-01-22 15:26:27 -0800144
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700145 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800146 ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700147 return NO_INIT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800148 }
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700149 if (bufferCount > NUM_BUFFER_SLOTS) {
Andy McFadden753e3412013-04-04 17:09:03 -0700150 ST_LOGE("setBufferCount: bufferCount too large (max %d)",
151 NUM_BUFFER_SLOTS);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700152 return BAD_VALUE;
153 }
154
155 // Error out if the user has dequeued buffers
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700156 for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700157 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
158 ST_LOGE("setBufferCount: client owns some buffers");
159 return -EINVAL;
160 }
161 }
162
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700163 if (bufferCount == 0) {
Jamie Gennis31a353d2012-08-24 17:25:13 -0700164 mOverrideMaxBufferCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700165 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -0700166 return NO_ERROR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700167 }
168
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700169 // fine to assume async to false before we're setting the buffer count
170 const int minBufferSlots = getMinMaxBufferCountLocked(false);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700171 if (bufferCount < minBufferSlots) {
172 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
173 "minimum (%d)", bufferCount, minBufferSlots);
174 return BAD_VALUE;
175 }
176
177 // here we're guaranteed that the client doesn't have dequeued buffers
Lajos Molnar9e3cb552013-05-06 16:23:07 -0700178 // and will release all of its buffer references. We don't clear the
179 // queue, however, so currently queued buffers still get displayed.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700180 freeAllBuffersLocked();
Jamie Gennis31a353d2012-08-24 17:25:13 -0700181 mOverrideMaxBufferCount = bufferCount;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700182 mDequeueCondition.broadcast();
183 listener = mConsumerListener;
184 } // scope for lock
185
186 if (listener != NULL) {
187 listener->onBuffersReleased();
Daniel Lam6b091c52012-01-22 15:26:27 -0800188 }
189
Andy McFadden753e3412013-04-04 17:09:03 -0700190 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800191}
192
Daniel Lamb8560522012-01-30 15:51:27 -0800193int BufferQueue::query(int what, int* outValue)
194{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800195 ATRACE_CALL();
Daniel Lamb8560522012-01-30 15:51:27 -0800196 Mutex::Autolock lock(mMutex);
197
198 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800199 ST_LOGE("query: BufferQueue has been abandoned!");
Daniel Lamb8560522012-01-30 15:51:27 -0800200 return NO_INIT;
201 }
202
203 int value;
204 switch (what) {
205 case NATIVE_WINDOW_WIDTH:
206 value = mDefaultWidth;
207 break;
208 case NATIVE_WINDOW_HEIGHT:
209 value = mDefaultHeight;
210 break;
211 case NATIVE_WINDOW_FORMAT:
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700212 value = mDefaultBufferFormat;
Daniel Lamb8560522012-01-30 15:51:27 -0800213 break;
214 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700215 value = getMinUndequeuedBufferCount(false);
Daniel Lamb8560522012-01-30 15:51:27 -0800216 break;
Mathias Agopian2488b202012-04-20 17:19:28 -0700217 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
218 value = (mQueue.size() >= 2);
219 break;
Eino-Ville Talvalaf7c60872013-07-30 14:05:02 -0700220 case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
221 value = mConsumerUsageBits;
222 break;
Daniel Lamb8560522012-01-30 15:51:27 -0800223 default:
224 return BAD_VALUE;
225 }
226 outValue[0] = value;
227 return NO_ERROR;
228}
229
Daniel Lam6b091c52012-01-22 15:26:27 -0800230status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800231 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800232 ST_LOGV("requestBuffer: slot=%d", slot);
233 Mutex::Autolock lock(mMutex);
234 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800235 ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800236 return NO_INIT;
237 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700238 if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800239 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700240 NUM_BUFFER_SLOTS, slot);
Jamie Gennise191e6c2012-08-24 20:26:34 -0700241 return BAD_VALUE;
242 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennise191e6c2012-08-24 20:26:34 -0700243 ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
244 slot, mSlots[slot].mBufferState);
Daniel Lam6b091c52012-01-22 15:26:27 -0800245 return BAD_VALUE;
246 }
247 mSlots[slot].mRequestBufferCalled = true;
248 *buf = mSlots[slot].mGraphicBuffer;
249 return NO_ERROR;
250}
251
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700252status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700253 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800254 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800255 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
256
257 if ((w && !h) || (!w && h)) {
258 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
259 return BAD_VALUE;
260 }
261
262 status_t returnFlags(OK);
263 EGLDisplay dpy = EGL_NO_DISPLAY;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700264 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800265
266 { // Scope for the lock
267 Mutex::Autolock lock(mMutex);
268
Daniel Lamb2675792012-02-23 14:35:13 -0800269 if (format == 0) {
270 format = mDefaultBufferFormat;
271 }
272 // turn on usage bits the consumer requested
273 usage |= mConsumerUsageBits;
274
Daniel Lam6b091c52012-01-22 15:26:27 -0800275 int found = -1;
Daniel Lam6b091c52012-01-22 15:26:27 -0800276 bool tryAgain = true;
277 while (tryAgain) {
278 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800279 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800280 return NO_INIT;
281 }
282
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700283 const int maxBufferCount = getMaxBufferCountLocked(async);
284 if (async && mOverrideMaxBufferCount) {
285 // FIXME: some drivers are manually setting the buffer-count (which they
286 // shouldn't), so we do this extra test here to handle that case.
287 // This is TEMPORARY, until we get this fixed.
288 if (mOverrideMaxBufferCount < maxBufferCount) {
289 ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
290 return BAD_VALUE;
291 }
292 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800293
Jamie Gennise191e6c2012-08-24 20:26:34 -0700294 // Free up any buffers that are in slots beyond the max buffer
295 // count.
296 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
297 assert(mSlots[i].mBufferState == BufferSlot::FREE);
298 if (mSlots[i].mGraphicBuffer != NULL) {
299 freeBufferLocked(i);
Andy McFadden2adaf042012-12-18 09:49:45 -0800300 returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700301 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800302 }
303
304 // look for a free buffer to give to the client
305 found = INVALID_BUFFER_SLOT;
Mathias Agopian6bac3632013-07-23 21:50:20 -0700306 int dequeuedCount = 0;
307 int acquiredCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700308 for (int i = 0; i < maxBufferCount; i++) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800309 const int state = mSlots[i].mBufferState;
Mathias Agopian6bac3632013-07-23 21:50:20 -0700310 switch (state) {
311 case BufferSlot::DEQUEUED:
312 dequeuedCount++;
313 break;
314 case BufferSlot::ACQUIRED:
315 acquiredCount++;
316 break;
317 case BufferSlot::FREE:
318 /* We return the oldest of the free buffers to avoid
319 * stalling the producer if possible. This is because
320 * the consumer may still have pending reads of the
321 * buffers in flight.
322 */
323 if ((found < 0) ||
324 mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
325 found = i;
326 }
327 break;
Daniel Lam6b091c52012-01-22 15:26:27 -0800328 }
329 }
330
331 // clients are not allowed to dequeue more than one buffer
332 // if they didn't set a buffer count.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700333 if (!mOverrideMaxBufferCount && dequeuedCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800334 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
335 "setting the buffer count");
336 return -EINVAL;
337 }
338
339 // See whether a buffer has been queued since the last
Jamie Gennis72f096f2012-08-27 18:48:37 -0700340 // setBufferCount so we know whether to perform the min undequeued
341 // buffers check below.
Daniel Lameae59d22012-01-22 15:26:27 -0800342 if (mBufferHasBeenQueued) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800343 // make sure the client is not trying to dequeue more buffers
344 // than allowed.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700345 const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700346 const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700347 if (newUndequeuedCount < minUndequeuedCount) {
348 ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
349 "exceeded (dequeued=%d undequeudCount=%d)",
350 minUndequeuedCount, dequeuedCount,
351 newUndequeuedCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800352 return -EBUSY;
353 }
354 }
355
Jamie Gennise191e6c2012-08-24 20:26:34 -0700356 // If no buffer is found, wait for a buffer to be released or for
357 // the max buffer count to change.
Daniel Lamc2c1f2f2012-03-07 14:11:29 -0800358 tryAgain = found == INVALID_BUFFER_SLOT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800359 if (tryAgain) {
Mathias Agopian6bac3632013-07-23 21:50:20 -0700360 // return an error if we're in "cannot block" mode (producer and consumer
361 // are controlled by the application) -- however, the consumer is allowed
362 // to acquire briefly an extra buffer (which could cause us to have to wait here)
363 // and that's okay because we know the wait will be brief (it happens
364 // if we dequeue a buffer while the consumer has acquired one but not released
365 // the old one yet -- for e.g.: see GLConsumer::updateTexImage()).
366 if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) {
Mathias Agopian595264f2013-07-16 22:56:09 -0700367 ST_LOGE("dequeueBuffer: would block! returning an error instead.");
368 return WOULD_BLOCK;
369 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800370 mDequeueCondition.wait(mMutex);
371 }
372 }
373
Daniel Lam6b091c52012-01-22 15:26:27 -0800374
375 if (found == INVALID_BUFFER_SLOT) {
376 // This should not happen.
377 ST_LOGE("dequeueBuffer: no available buffer slots");
378 return -EBUSY;
379 }
380
381 const int buf = found;
382 *outBuf = found;
383
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800384 ATRACE_BUFFER_INDEX(buf);
385
Daniel Lam6b091c52012-01-22 15:26:27 -0800386 const bool useDefaultSize = !w && !h;
387 if (useDefaultSize) {
388 // use the default size
389 w = mDefaultWidth;
390 h = mDefaultHeight;
391 }
392
Daniel Lam6b091c52012-01-22 15:26:27 -0800393 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
394
395 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
396 if ((buffer == NULL) ||
397 (uint32_t(buffer->width) != w) ||
398 (uint32_t(buffer->height) != h) ||
399 (uint32_t(buffer->format) != format) ||
400 ((uint32_t(buffer->usage) & usage) != usage))
401 {
Daniel Lameae59d22012-01-22 15:26:27 -0800402 mSlots[buf].mAcquireCalled = false;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700403 mSlots[buf].mGraphicBuffer = NULL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800404 mSlots[buf].mRequestBufferCalled = false;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700405 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800406 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800407 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
408
Andy McFadden2adaf042012-12-18 09:49:45 -0800409 returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
Daniel Lam6b091c52012-01-22 15:26:27 -0800410 }
411
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700412
413 if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) {
414 ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. "
415 "buf=%d, w=%d, h=%d, format=%d",
416 buf, buffer->width, buffer->height, buffer->format);
417 }
418
Daniel Lam6b091c52012-01-22 15:26:27 -0800419 dpy = mSlots[buf].mEglDisplay;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700420 eglFence = mSlots[buf].mEglFence;
Jesse Hall4c00cc12013-03-15 21:34:30 -0700421 *outFence = mSlots[buf].mFence;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700422 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800423 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800424 } // end lock scope
Daniel Lam6b091c52012-01-22 15:26:27 -0800425
Andy McFadden2adaf042012-12-18 09:49:45 -0800426 if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
Jamie Gennis1efe0992012-10-04 18:34:01 -0700427 status_t error;
428 sp<GraphicBuffer> graphicBuffer(
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700429 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error));
Jamie Gennis1efe0992012-10-04 18:34:01 -0700430 if (graphicBuffer == 0) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700431 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700432 return error;
433 }
434
435 { // Scope for the lock
436 Mutex::Autolock lock(mMutex);
437
438 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800439 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700440 return NO_INIT;
441 }
442
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700443 mSlots[*outBuf].mFrameNumber = ~0;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700444 mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
445 }
446 }
447
Jesse Hallc777b0b2012-06-28 12:52:05 -0700448 if (eglFence != EGL_NO_SYNC_KHR) {
449 EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
Daniel Lam6b091c52012-01-22 15:26:27 -0800450 // If something goes wrong, log the error, but return the buffer without
451 // synchronizing access to it. It's too late at this point to abort the
452 // dequeue operation.
453 if (result == EGL_FALSE) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700454 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
Daniel Lam6b091c52012-01-22 15:26:27 -0800455 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700456 ST_LOGE("dequeueBuffer: timeout waiting for fence");
Daniel Lam6b091c52012-01-22 15:26:27 -0800457 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700458 eglDestroySyncKHR(dpy, eglFence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800459 }
460
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700461 ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
462 mSlots[*outBuf].mFrameNumber,
Daniel Lam6b091c52012-01-22 15:26:27 -0800463 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
464
465 return returnFlags;
466}
467
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700468status_t BufferQueue::queueBuffer(int buf,
469 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800470 ATRACE_CALL();
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800471 ATRACE_BUFFER_INDEX(buf);
472
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700473 Rect crop;
474 uint32_t transform;
475 int scalingMode;
476 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700477 bool isAutoTimestamp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700478 bool async;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700479 sp<Fence> fence;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700480
Andy McFadden3c256212013-08-16 14:55:39 -0700481 input.deflate(&timestamp, &isAutoTimestamp, &crop, &scalingMode, &transform,
482 &async, &fence);
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700483
Jamie Gennis1df8c342012-12-20 14:05:45 -0800484 if (fence == NULL) {
485 ST_LOGE("queueBuffer: fence is NULL");
486 return BAD_VALUE;
487 }
488
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700489 switch (scalingMode) {
490 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
491 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
492 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
493 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
494 break;
495 default:
496 ST_LOGE("unknown scaling mode: %d", scalingMode);
497 return -EINVAL;
498 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800499
Mathias Agopiana4e19522013-07-31 20:09:53 -0700500 sp<IConsumerListener> listener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800501
502 { // scope for the lock
503 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700504
Daniel Lam6b091c52012-01-22 15:26:27 -0800505 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800506 ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800507 return NO_INIT;
508 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700509
510 const int maxBufferCount = getMaxBufferCountLocked(async);
511 if (async && mOverrideMaxBufferCount) {
512 // FIXME: some drivers are manually setting the buffer-count (which they
513 // shouldn't), so we do this extra test here to handle that case.
514 // This is TEMPORARY, until we get this fixed.
515 if (mOverrideMaxBufferCount < maxBufferCount) {
516 ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
517 return BAD_VALUE;
518 }
519 }
Jamie Gennise191e6c2012-08-24 20:26:34 -0700520 if (buf < 0 || buf >= maxBufferCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800521 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennise191e6c2012-08-24 20:26:34 -0700522 maxBufferCount, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800523 return -EINVAL;
524 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
525 ST_LOGE("queueBuffer: slot %d is not owned by the client "
526 "(state=%d)", buf, mSlots[buf].mBufferState);
527 return -EINVAL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800528 } else if (!mSlots[buf].mRequestBufferCalled) {
529 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
530 "buffer", buf);
531 return -EINVAL;
532 }
533
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700534 ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
535 "tr=%#x scale=%s",
536 buf, mFrameCounter + 1, timestamp,
537 crop.left, crop.top, crop.right, crop.bottom,
538 transform, scalingModeName(scalingMode));
539
Jamie Gennisd72f2332012-05-07 13:50:11 -0700540 const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
541 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
542 Rect croppedCrop;
543 crop.intersect(bufferRect, &croppedCrop);
544 if (croppedCrop != crop) {
545 ST_LOGE("queueBuffer: crop rect is not contained within the "
546 "buffer in slot %d", buf);
547 return -EINVAL;
548 }
549
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700550 mSlots[buf].mFence = fence;
551 mSlots[buf].mBufferState = BufferSlot::QUEUED;
552 mFrameCounter++;
553 mSlots[buf].mFrameNumber = mFrameCounter;
554
555 BufferItem item;
556 item.mAcquireCalled = mSlots[buf].mAcquireCalled;
557 item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
558 item.mCrop = crop;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700559 item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
560 item.mTransformToDisplayInverse = bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700561 item.mScalingMode = scalingMode;
562 item.mTimestamp = timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700563 item.mIsAutoTimestamp = isAutoTimestamp;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700564 item.mFrameNumber = mFrameCounter;
565 item.mBuf = buf;
566 item.mFence = fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700567 item.mIsDroppable = mDequeueBufferCannotBlock || async;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700568
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700569 if (mQueue.empty()) {
570 // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
571 // simply queue this buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700572 mQueue.push_back(item);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700573 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800574 } else {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700575 // when the queue is not empty, we need to look at the front buffer
576 // state and see if we need to replace it.
577 Fifo::iterator front(mQueue.begin());
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700578 if (front->mIsDroppable) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700579 // buffer slot currently queued is marked free if still tracked
580 if (stillTracking(front)) {
581 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
Mathias Agopian26a6f372013-07-18 22:25:55 -0700582 // reset the frame number of the freed buffer so that it is the first in
583 // line to be dequeued again.
584 mSlots[front->mBuf].mFrameNumber = 0;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700585 }
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700586 // and we record the new buffer in the queued list
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700587 *front = item;
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700588 } else {
589 mQueue.push_back(item);
590 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800591 }
592 }
593
Daniel Lameae59d22012-01-22 15:26:27 -0800594 mBufferHasBeenQueued = true;
Dave Burke74ff8c22012-03-12 21:49:41 -0700595 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800596
Mathias Agopian2488b202012-04-20 17:19:28 -0700597 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
598 mQueue.size());
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800599
600 ATRACE_INT(mConsumerName.string(), mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800601 } // scope for the lock
602
603 // call back without lock held
604 if (listener != 0) {
605 listener->onFrameAvailable();
606 }
Andy McFadden753e3412013-04-04 17:09:03 -0700607 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800608}
609
Jesse Hall4c00cc12013-03-15 21:34:30 -0700610void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800611 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800612 ST_LOGV("cancelBuffer: slot=%d", buf);
613 Mutex::Autolock lock(mMutex);
614
615 if (mAbandoned) {
616 ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
617 return;
618 }
619
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700620 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800621 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700622 NUM_BUFFER_SLOTS, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800623 return;
624 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
625 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
626 buf, mSlots[buf].mBufferState);
627 return;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800628 } else if (fence == NULL) {
629 ST_LOGE("cancelBuffer: fence is NULL");
630 return;
Daniel Lam6b091c52012-01-22 15:26:27 -0800631 }
632 mSlots[buf].mBufferState = BufferSlot::FREE;
633 mSlots[buf].mFrameNumber = 0;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700634 mSlots[buf].mFence = fence;
Dave Burke74ff8c22012-03-12 21:49:41 -0700635 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800636}
637
Mathias Agopian365857d2013-09-11 19:35:45 -0700638
639status_t BufferQueue::connect(const sp<IBinder>& token,
640 int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800641 ATRACE_CALL();
Jesse Hall8db92552013-08-29 16:03:50 -0700642 ST_LOGV("connect: api=%d producerControlledByApp=%s", api,
643 producerControlledByApp ? "true" : "false");
Daniel Lam6b091c52012-01-22 15:26:27 -0800644 Mutex::Autolock lock(mMutex);
645
646 if (mAbandoned) {
647 ST_LOGE("connect: BufferQueue has been abandoned!");
648 return NO_INIT;
649 }
650
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700651 if (mConsumerListener == NULL) {
652 ST_LOGE("connect: BufferQueue has no consumer!");
653 return NO_INIT;
654 }
655
Daniel Lam6b091c52012-01-22 15:26:27 -0800656 int err = NO_ERROR;
657 switch (api) {
658 case NATIVE_WINDOW_API_EGL:
659 case NATIVE_WINDOW_API_CPU:
660 case NATIVE_WINDOW_API_MEDIA:
661 case NATIVE_WINDOW_API_CAMERA:
662 if (mConnectedApi != NO_CONNECTED_API) {
663 ST_LOGE("connect: already connected (cur=%d, req=%d)",
664 mConnectedApi, api);
665 err = -EINVAL;
666 } else {
667 mConnectedApi = api;
Mathias Agopian365857d2013-09-11 19:35:45 -0700668 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, mQueue.size());
669
670 // set-up a death notification so that we can disconnect automatically
671 // when/if the remote producer dies.
672 // This will fail with INVALID_OPERATION if the "token" is local to our process.
673 if (token->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)) == NO_ERROR) {
674 mConnectedProducerToken = token;
675 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800676 }
677 break;
678 default:
679 err = -EINVAL;
680 break;
681 }
Daniel Lameae59d22012-01-22 15:26:27 -0800682
683 mBufferHasBeenQueued = false;
Mathias Agopian595264f2013-07-16 22:56:09 -0700684 mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
Daniel Lameae59d22012-01-22 15:26:27 -0800685
Daniel Lam6b091c52012-01-22 15:26:27 -0800686 return err;
687}
688
Mathias Agopian365857d2013-09-11 19:35:45 -0700689void BufferQueue::binderDied(const wp<IBinder>& who) {
690 // If we're here, it means that a producer we were connected to died.
691 // We're GUARANTEED that we still are connected to it because it has no other way
692 // to get disconnected -- or -- we wouldn't be here because we're removing this
693 // callback upon disconnect. Therefore, it's okay to read mConnectedApi without
694 // synchronization here.
695 int api = mConnectedApi;
696 this->disconnect(api);
697}
698
Daniel Lam6b091c52012-01-22 15:26:27 -0800699status_t BufferQueue::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800700 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800701 ST_LOGV("disconnect: api=%d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800702
703 int err = NO_ERROR;
Mathias Agopiana4e19522013-07-31 20:09:53 -0700704 sp<IConsumerListener> listener;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700705
706 { // Scope for the lock
707 Mutex::Autolock lock(mMutex);
708
709 if (mAbandoned) {
710 // it is not really an error to disconnect after the surface
711 // has been abandoned, it should just be a no-op.
712 return NO_ERROR;
713 }
714
715 switch (api) {
716 case NATIVE_WINDOW_API_EGL:
717 case NATIVE_WINDOW_API_CPU:
718 case NATIVE_WINDOW_API_MEDIA:
719 case NATIVE_WINDOW_API_CAMERA:
720 if (mConnectedApi == api) {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700721 freeAllBuffersLocked();
Mathias Agopian365857d2013-09-11 19:35:45 -0700722 // remove our death notification callback if we have one
723 sp<IBinder> token = mConnectedProducerToken;
724 if (token != NULL) {
725 // this can fail if we're here because of the death notification
726 // either way, we just ignore.
727 token->unlinkToDeath(static_cast<IBinder::DeathRecipient*>(this));
728 }
729 mConnectedProducerToken = NULL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700730 mConnectedApi = NO_CONNECTED_API;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700731 mDequeueCondition.broadcast();
732 listener = mConsumerListener;
733 } else {
734 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
735 mConnectedApi, api);
736 err = -EINVAL;
737 }
738 break;
739 default:
740 ST_LOGE("disconnect: unknown API %d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800741 err = -EINVAL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700742 break;
743 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800744 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700745
746 if (listener != NULL) {
747 listener->onBuffersReleased();
748 }
749
Daniel Lam6b091c52012-01-22 15:26:27 -0800750 return err;
751}
752
Mathias Agopian74d211a2013-04-22 16:55:35 +0200753void BufferQueue::dump(String8& result, const char* prefix) const {
Daniel Lameae59d22012-01-22 15:26:27 -0800754 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800755
756 String8 fifo;
757 int fifoSize = 0;
758 Fifo::const_iterator i(mQueue.begin());
759 while (i != mQueue.end()) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700760 fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
761 "xform=0x%02x, time=%#llx, scale=%s\n",
762 i->mBuf, i->mGraphicBuffer.get(),
763 i->mCrop.left, i->mCrop.top, i->mCrop.right,
764 i->mCrop.bottom, i->mTransform, i->mTimestamp,
765 scalingModeName(i->mScalingMode)
766 );
767 i++;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200768 fifoSize++;
Daniel Lameae59d22012-01-22 15:26:27 -0800769 }
770
Jamie Gennise191e6c2012-08-24 20:26:34 -0700771
Mathias Agopian74d211a2013-04-22 16:55:35 +0200772 result.appendFormat(
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700773 "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
Andy McFadden69052052012-09-14 16:10:11 -0700774 "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700775 prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
Andy McFadden69052052012-09-14 16:10:11 -0700776 mDefaultHeight, mDefaultBufferFormat, mTransformHint,
777 fifoSize, fifo.string());
Daniel Lameae59d22012-01-22 15:26:27 -0800778
779 struct {
780 const char * operator()(int state) const {
781 switch (state) {
782 case BufferSlot::DEQUEUED: return "DEQUEUED";
783 case BufferSlot::QUEUED: return "QUEUED";
784 case BufferSlot::FREE: return "FREE";
785 case BufferSlot::ACQUIRED: return "ACQUIRED";
786 default: return "Unknown";
787 }
788 }
789 } stateName;
790
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700791 // just trim the free buffers to not spam the dump
792 int maxBufferCount = 0;
793 for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
794 const BufferSlot& slot(mSlots[i]);
795 if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
796 maxBufferCount = i+1;
797 break;
798 }
799 }
800
Jamie Gennise191e6c2012-08-24 20:26:34 -0700801 for (int i=0 ; i<maxBufferCount ; i++) {
Daniel Lameae59d22012-01-22 15:26:27 -0800802 const BufferSlot& slot(mSlots[i]);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700803 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200804 result.appendFormat(
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700805 "%s%s[%02d:%p] state=%-8s",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700806 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700807 stateName(slot.mBufferState)
Daniel Lameae59d22012-01-22 15:26:27 -0800808 );
Daniel Lameae59d22012-01-22 15:26:27 -0800809
Daniel Lameae59d22012-01-22 15:26:27 -0800810 if (buf != NULL) {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200811 result.appendFormat(
Daniel Lameae59d22012-01-22 15:26:27 -0800812 ", %p [%4ux%4u:%4u,%3X]",
813 buf->handle, buf->width, buf->height, buf->stride,
814 buf->format);
Daniel Lameae59d22012-01-22 15:26:27 -0800815 }
816 result.append("\n");
817 }
818}
819
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700820void BufferQueue::freeBufferLocked(int slot) {
821 ST_LOGV("freeBufferLocked: slot=%d", slot);
822 mSlots[slot].mGraphicBuffer = 0;
823 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
824 mSlots[slot].mNeedsCleanupOnRelease = true;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700825 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700826 mSlots[slot].mBufferState = BufferSlot::FREE;
827 mSlots[slot].mFrameNumber = 0;
828 mSlots[slot].mAcquireCalled = false;
Daniel Lameae59d22012-01-22 15:26:27 -0800829
830 // destroy fence as BufferQueue now takes ownership
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700831 if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
832 eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
833 mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800834 }
Jamie Gennis1df8c342012-12-20 14:05:45 -0800835 mSlots[slot].mFence = Fence::NO_FENCE;
Daniel Lam6b091c52012-01-22 15:26:27 -0800836}
837
838void BufferQueue::freeAllBuffersLocked() {
Daniel Lameae59d22012-01-22 15:26:27 -0800839 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800840 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
841 freeBufferLocked(i);
842 }
843}
844
Andy McFadden14fab7d2013-08-01 13:37:42 -0700845status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800846 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -0800847 Mutex::Autolock _l(mMutex);
Jamie Gennis5e5efde2012-08-28 17:18:50 -0700848
849 // Check that the consumer doesn't currently have the maximum number of
850 // buffers acquired. We allow the max buffer count to be exceeded by one
851 // buffer, so that the consumer can successfully set up the newly acquired
852 // buffer before releasing the old one.
853 int numAcquiredBuffers = 0;
854 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
855 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
856 numAcquiredBuffers++;
857 }
858 }
859 if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
860 ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
861 numAcquiredBuffers, mMaxAcquiredBufferCount);
862 return INVALID_OPERATION;
863 }
864
Daniel Lameae59d22012-01-22 15:26:27 -0800865 // check if queue is empty
866 // In asynchronous mode the list is guaranteed to be one buffer
867 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700868 if (mQueue.empty()) {
Daniel Lamfbcda932012-04-09 22:51:52 -0700869 return NO_BUFFER_AVAILABLE;
Daniel Lameae59d22012-01-22 15:26:27 -0800870 }
871
Andy McFadden1585c4d2013-06-28 13:52:40 -0700872 Fifo::iterator front(mQueue.begin());
Andy McFadden1585c4d2013-06-28 13:52:40 -0700873
Andy McFadden14fab7d2013-08-01 13:37:42 -0700874 // If expectedPresent is specified, we may not want to return a buffer yet.
875 // If it's specified and there's more than one buffer queued, we may
876 // want to drop a buffer.
877 if (expectedPresent != 0) {
878 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
879
880 // The "expectedPresent" argument indicates when the buffer is expected
881 // to be presented on-screen. If the buffer's desired-present time
882 // is earlier (less) than expectedPresent, meaning it'll be displayed
883 // on time or possibly late if we show it ASAP, we acquire and return
884 // it. If we don't want to display it until after the expectedPresent
885 // time, we return PRESENT_LATER without acquiring it.
886 //
887 // To be safe, we don't defer acquisition if expectedPresent is
888 // more than one second in the future beyond the desired present time
889 // (i.e. we'd be holding the buffer for a long time).
890 //
891 // NOTE: code assumes monotonic time values from the system clock are
892 // positive.
Andy McFadden3c256212013-08-16 14:55:39 -0700893
894 // Start by checking to see if we can drop frames. We skip this check
895 // if the timestamps are being auto-generated by Surface -- if the
896 // app isn't generating timestamps explicitly, they probably don't
897 // want frames to be discarded based on them.
898 while (mQueue.size() > 1 && !mQueue[0].mIsAutoTimestamp) {
Andy McFadden14fab7d2013-08-01 13:37:42 -0700899 // If entry[1] is timely, drop entry[0] (and repeat). We apply
900 // an additional criteria here: we only drop the earlier buffer if
901 // our desiredPresent falls within +/- 1 second of the expected
902 // present. Otherwise, bogus desiredPresent times (e.g. 0 or
903 // a small relative timestamp), which normally mean "ignore the
904 // timestamp and acquire immediately", would cause us to drop
905 // frames.
906 //
907 // We may want to add an additional criteria: don't drop the
908 // earlier buffer if entry[1]'s fence hasn't signaled yet.
909 //
910 // (Vector front is [0], back is [size()-1])
911 const BufferItem& bi(mQueue[1]);
912 nsecs_t desiredPresent = bi.mTimestamp;
913 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
914 desiredPresent > expectedPresent) {
915 // This buffer is set to display in the near future, or
916 // desiredPresent is garbage. Either way we don't want to
917 // drop the previous buffer just to get this on screen sooner.
918 ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld",
919 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
920 systemTime(CLOCK_MONOTONIC));
921 break;
922 }
923 ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d",
924 desiredPresent, expectedPresent, mQueue.size());
925 if (stillTracking(front)) {
926 // front buffer is still in mSlots, so mark the slot as free
927 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
928 }
929 mQueue.erase(front);
930 front = mQueue.begin();
931 }
932
933 // See if the front buffer is due.
934 nsecs_t desiredPresent = front->mTimestamp;
935 if (desiredPresent > expectedPresent &&
936 desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
937 ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld",
938 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
939 systemTime(CLOCK_MONOTONIC));
940 return PRESENT_LATER;
941 }
942
943 ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld",
944 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700945 systemTime(CLOCK_MONOTONIC));
Andy McFadden1585c4d2013-06-28 13:52:40 -0700946 }
947
Andy McFadden14fab7d2013-08-01 13:37:42 -0700948 int buf = front->mBuf;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700949 *buffer = *front;
950 ATRACE_BUFFER_INDEX(buf);
951
952 ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
953 front->mBuf, front->mFrameNumber,
954 front->mGraphicBuffer->handle);
955 // if front buffer still being tracked update slot state
956 if (stillTracking(front)) {
957 mSlots[buf].mAcquireCalled = true;
958 mSlots[buf].mNeedsCleanupOnRelease = false;
959 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
960 mSlots[buf].mFence = Fence::NO_FENCE;
961 }
962
963 // If the buffer has previously been acquired by the consumer, set
964 // mGraphicBuffer to NULL to avoid unnecessarily remapping this
965 // buffer on the consumer side.
966 if (buffer->mAcquireCalled) {
967 buffer->mGraphicBuffer = NULL;
968 }
969
970 mQueue.erase(front);
971 mDequeueCondition.broadcast();
972
973 ATRACE_INT(mConsumerName.string(), mQueue.size());
974
Andy McFadden753e3412013-04-04 17:09:03 -0700975 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800976}
977
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700978status_t BufferQueue::releaseBuffer(
979 int buf, uint64_t frameNumber, EGLDisplay display,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700980 EGLSyncKHR eglFence, const sp<Fence>& fence) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800981 ATRACE_CALL();
982 ATRACE_BUFFER_INDEX(buf);
983
Jamie Gennis1df8c342012-12-20 14:05:45 -0800984 if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
985 return BAD_VALUE;
Daniel Lameae59d22012-01-22 15:26:27 -0800986 }
987
Mathias Agopian207c1e22013-07-22 18:00:53 -0700988 Mutex::Autolock _l(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700989
990 // If the frame number has changed because buffer has been reallocated,
991 // we can ignore this releaseBuffer for the old buffer.
992 if (frameNumber != mSlots[buf].mFrameNumber) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700993 return STALE_BUFFER_SLOT;
994 }
Mathias Agopian207c1e22013-07-22 18:00:53 -0700995
996
997 // Internal state consistency checks:
998 // Make sure this buffers hasn't been queued while we were owning it (acquired)
999 Fifo::iterator front(mQueue.begin());
1000 Fifo::const_iterator const end(mQueue.end());
1001 while (front != end) {
1002 if (front->mBuf == buf) {
1003 LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
1004 "acquired", mConsumerName.string(), frameNumber, buf);
1005 break; // never reached
1006 }
1007 front++;
1008 }
Daniel Lameae59d22012-01-22 15:26:27 -08001009
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001010 // The buffer can now only be released if its in the acquired state
1011 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001012 mSlots[buf].mEglDisplay = display;
1013 mSlots[buf].mEglFence = eglFence;
1014 mSlots[buf].mFence = fence;
Daniel Lameae59d22012-01-22 15:26:27 -08001015 mSlots[buf].mBufferState = BufferSlot::FREE;
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001016 } else if (mSlots[buf].mNeedsCleanupOnRelease) {
1017 ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
1018 mSlots[buf].mNeedsCleanupOnRelease = false;
1019 return STALE_BUFFER_SLOT;
1020 } else {
1021 ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
1022 return -EINVAL;
Daniel Lameae59d22012-01-22 15:26:27 -08001023 }
Daniel Lameae59d22012-01-22 15:26:27 -08001024
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001025 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -07001026 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001027}
1028
Mathias Agopiana4e19522013-07-31 20:09:53 -07001029status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener,
Mathias Agopian595264f2013-07-16 22:56:09 -07001030 bool controlledByApp) {
Jesse Hall8db92552013-08-29 16:03:50 -07001031 ST_LOGV("consumerConnect controlledByApp=%s",
1032 controlledByApp ? "true" : "false");
Daniel Lameae59d22012-01-22 15:26:27 -08001033 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -08001034
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001035 if (mAbandoned) {
1036 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
1037 return NO_INIT;
1038 }
Andy McFadden753e3412013-04-04 17:09:03 -07001039 if (consumerListener == NULL) {
1040 ST_LOGE("consumerConnect: consumerListener may not be NULL");
1041 return BAD_VALUE;
1042 }
Daniel Lamb2675792012-02-23 14:35:13 -08001043
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001044 mConsumerListener = consumerListener;
Mathias Agopian595264f2013-07-16 22:56:09 -07001045 mConsumerControlledByApp = controlledByApp;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001046
Andy McFadden753e3412013-04-04 17:09:03 -07001047 return NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001048}
1049
1050status_t BufferQueue::consumerDisconnect() {
1051 ST_LOGV("consumerDisconnect");
1052 Mutex::Autolock lock(mMutex);
1053
1054 if (mConsumerListener == NULL) {
1055 ST_LOGE("consumerDisconnect: No consumer is connected!");
1056 return -EINVAL;
1057 }
1058
1059 mAbandoned = true;
1060 mConsumerListener = NULL;
Daniel Lamb2675792012-02-23 14:35:13 -08001061 mQueue.clear();
Daniel Lameae59d22012-01-22 15:26:27 -08001062 freeAllBuffersLocked();
Dave Burke74ff8c22012-03-12 21:49:41 -07001063 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -07001064 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001065}
1066
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001067status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
1068 ST_LOGV("getReleasedBuffers");
1069 Mutex::Autolock lock(mMutex);
1070
1071 if (mAbandoned) {
1072 ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
1073 return NO_INIT;
1074 }
1075
1076 uint32_t mask = 0;
1077 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1078 if (!mSlots[i].mAcquireCalled) {
1079 mask |= 1 << i;
1080 }
1081 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001082
1083 // Remove buffers in flight (on the queue) from the mask where acquire has
1084 // been called, as the consumer will not receive the buffer address, so
1085 // it should not free these slots.
1086 Fifo::iterator front(mQueue.begin());
1087 while (front != mQueue.end()) {
1088 if (front->mAcquireCalled)
1089 mask &= ~(1 << front->mBuf);
1090 front++;
1091 }
1092
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001093 *slotMask = mask;
1094
1095 ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1096 return NO_ERROR;
1097}
1098
Mathias Agopian207c1e22013-07-22 18:00:53 -07001099status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
Daniel Lameae59d22012-01-22 15:26:27 -08001100 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1101 if (!w || !h) {
1102 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1103 w, h);
1104 return BAD_VALUE;
1105 }
1106
1107 Mutex::Autolock lock(mMutex);
1108 mDefaultWidth = w;
1109 mDefaultHeight = h;
Andy McFadden753e3412013-04-04 17:09:03 -07001110 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001111}
1112
Jamie Gennis31a353d2012-08-24 17:25:13 -07001113status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001114 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -08001115 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -07001116 return setDefaultMaxBufferCountLocked(bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -08001117}
1118
Mathias Agopianad678e12013-07-23 17:28:53 -07001119status_t BufferQueue::disableAsyncBuffer() {
1120 ATRACE_CALL();
1121 Mutex::Autolock lock(mMutex);
1122 if (mConsumerListener != NULL) {
1123 ST_LOGE("disableAsyncBuffer: consumer already connected!");
1124 return INVALID_OPERATION;
1125 }
1126 mUseAsyncBuffer = false;
1127 return NO_ERROR;
1128}
1129
Jamie Gennis72f096f2012-08-27 18:48:37 -07001130status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1131 ATRACE_CALL();
1132 Mutex::Autolock lock(mMutex);
Jamie Gennisc68f2ec2012-08-30 18:36:22 -07001133 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1134 ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1135 maxAcquiredBuffers);
1136 return BAD_VALUE;
1137 }
Jamie Gennis72f096f2012-08-27 18:48:37 -07001138 if (mConnectedApi != NO_CONNECTED_API) {
1139 return INVALID_OPERATION;
1140 }
1141 mMaxAcquiredBufferCount = maxAcquiredBuffers;
Andy McFadden753e3412013-04-04 17:09:03 -07001142 return NO_ERROR;
Jamie Gennis72f096f2012-08-27 18:48:37 -07001143}
1144
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001145int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
Mathias Agopianad678e12013-07-23 17:28:53 -07001146 // if dequeueBuffer is allowed to error out, we don't have to
1147 // add an extra buffer.
1148 if (!mUseAsyncBuffer)
1149 return mMaxAcquiredBufferCount;
1150
1151 // we're in async mode, or we want to prevent the app to
1152 // deadlock itself, we throw-in an extra buffer to guarantee it.
1153 if (mDequeueBufferCannotBlock || async)
1154 return mMaxAcquiredBufferCount+1;
1155
1156 return mMaxAcquiredBufferCount;
Jamie Gennis31a353d2012-08-24 17:25:13 -07001157}
1158
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001159int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
1160 return getMinUndequeuedBufferCount(async) + 1;
1161}
1162
1163int BufferQueue::getMaxBufferCountLocked(bool async) const {
1164 int minMaxBufferCount = getMinMaxBufferCountLocked(async);
Jamie Gennise191e6c2012-08-24 20:26:34 -07001165
1166 int maxBufferCount = mDefaultMaxBufferCount;
1167 if (maxBufferCount < minMaxBufferCount) {
1168 maxBufferCount = minMaxBufferCount;
1169 }
1170 if (mOverrideMaxBufferCount != 0) {
1171 assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1172 maxBufferCount = mOverrideMaxBufferCount;
1173 }
1174
1175 // Any buffers that are dequeued by the producer or sitting in the queue
1176 // waiting to be consumed need to have their slots preserved. Such
1177 // buffers will temporarily keep the max buffer count up until the slots
1178 // no longer need to be preserved.
1179 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1180 BufferSlot::BufferState state = mSlots[i].mBufferState;
1181 if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1182 maxBufferCount = i + 1;
1183 }
1184 }
1185
1186 return maxBufferCount;
1187}
1188
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001189bool BufferQueue::stillTracking(const BufferItem *item) const {
1190 const BufferSlot &slot = mSlots[item->mBuf];
1191
1192 ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1193 "slot: { slot=%d/%llu, buffer=%p }",
1194 item->mBuf, item->mFrameNumber,
1195 (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1196 item->mBuf, slot.mFrameNumber,
1197 (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1198
1199 // Compare item with its original buffer slot. We can check the slot
1200 // as the buffer would not be moved to a different slot by the producer.
1201 return (slot.mGraphicBuffer != NULL &&
1202 item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1203}
1204
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001205BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
Mathias Agopiana4e19522013-07-31 20:09:53 -07001206 const wp<ConsumerListener>& consumerListener):
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001207 mConsumerListener(consumerListener) {}
1208
1209BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1210
1211void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
Mathias Agopiana4e19522013-07-31 20:09:53 -07001212 sp<ConsumerListener> listener(mConsumerListener.promote());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001213 if (listener != NULL) {
1214 listener->onFrameAvailable();
1215 }
1216}
1217
1218void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
Mathias Agopiana4e19522013-07-31 20:09:53 -07001219 sp<ConsumerListener> listener(mConsumerListener.promote());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001220 if (listener != NULL) {
1221 listener->onBuffersReleased();
1222 }
1223}
1224
Daniel Lam6b091c52012-01-22 15:26:27 -08001225}; // namespace android