blob: 17e93c858f9e94fb346788510d9d389f1e9cac07 [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 Agopian90ac7992012-02-25 18:48:35 -080028#include <gui/ISurfaceComposer.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080029#include <private/gui/ComposerService.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080030
31#include <utils/Log.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080032#include <utils/Trace.h>
Mathias Agopian7cdd7862013-07-18 22:10:56 -070033#include <utils/CallStack.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
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700141 sp<ConsumerListener> listener;
142 {
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;
Daniel Lamb8560522012-01-30 15:51:27 -0800220 default:
221 return BAD_VALUE;
222 }
223 outValue[0] = value;
224 return NO_ERROR;
225}
226
Daniel Lam6b091c52012-01-22 15:26:27 -0800227status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800228 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800229 ST_LOGV("requestBuffer: slot=%d", slot);
230 Mutex::Autolock lock(mMutex);
231 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800232 ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800233 return NO_INIT;
234 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700235 if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800236 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700237 NUM_BUFFER_SLOTS, slot);
Jamie Gennise191e6c2012-08-24 20:26:34 -0700238 return BAD_VALUE;
239 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennise191e6c2012-08-24 20:26:34 -0700240 ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
241 slot, mSlots[slot].mBufferState);
Daniel Lam6b091c52012-01-22 15:26:27 -0800242 return BAD_VALUE;
243 }
244 mSlots[slot].mRequestBufferCalled = true;
245 *buf = mSlots[slot].mGraphicBuffer;
246 return NO_ERROR;
247}
248
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700249status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700250 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800251 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800252 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
253
254 if ((w && !h) || (!w && h)) {
255 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
256 return BAD_VALUE;
257 }
258
259 status_t returnFlags(OK);
260 EGLDisplay dpy = EGL_NO_DISPLAY;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700261 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800262
263 { // Scope for the lock
264 Mutex::Autolock lock(mMutex);
265
Daniel Lamb2675792012-02-23 14:35:13 -0800266 if (format == 0) {
267 format = mDefaultBufferFormat;
268 }
269 // turn on usage bits the consumer requested
270 usage |= mConsumerUsageBits;
271
Daniel Lam6b091c52012-01-22 15:26:27 -0800272 int found = -1;
Daniel Lam6b091c52012-01-22 15:26:27 -0800273 bool tryAgain = true;
274 while (tryAgain) {
275 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800276 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800277 return NO_INIT;
278 }
279
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700280 const int maxBufferCount = getMaxBufferCountLocked(async);
281 if (async && mOverrideMaxBufferCount) {
282 // FIXME: some drivers are manually setting the buffer-count (which they
283 // shouldn't), so we do this extra test here to handle that case.
284 // This is TEMPORARY, until we get this fixed.
285 if (mOverrideMaxBufferCount < maxBufferCount) {
286 ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
287 return BAD_VALUE;
288 }
289 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800290
Jamie Gennise191e6c2012-08-24 20:26:34 -0700291 // Free up any buffers that are in slots beyond the max buffer
292 // count.
293 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
294 assert(mSlots[i].mBufferState == BufferSlot::FREE);
295 if (mSlots[i].mGraphicBuffer != NULL) {
296 freeBufferLocked(i);
Andy McFadden2adaf042012-12-18 09:49:45 -0800297 returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700298 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800299 }
300
301 // look for a free buffer to give to the client
302 found = INVALID_BUFFER_SLOT;
Mathias Agopian6bac3632013-07-23 21:50:20 -0700303 int dequeuedCount = 0;
304 int acquiredCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700305 for (int i = 0; i < maxBufferCount; i++) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800306 const int state = mSlots[i].mBufferState;
Mathias Agopian6bac3632013-07-23 21:50:20 -0700307 switch (state) {
308 case BufferSlot::DEQUEUED:
309 dequeuedCount++;
310 break;
311 case BufferSlot::ACQUIRED:
312 acquiredCount++;
313 break;
314 case BufferSlot::FREE:
315 /* We return the oldest of the free buffers to avoid
316 * stalling the producer if possible. This is because
317 * the consumer may still have pending reads of the
318 * buffers in flight.
319 */
320 if ((found < 0) ||
321 mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
322 found = i;
323 }
324 break;
Daniel Lam6b091c52012-01-22 15:26:27 -0800325 }
326 }
327
328 // clients are not allowed to dequeue more than one buffer
329 // if they didn't set a buffer count.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700330 if (!mOverrideMaxBufferCount && dequeuedCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800331 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
332 "setting the buffer count");
333 return -EINVAL;
334 }
335
336 // See whether a buffer has been queued since the last
Jamie Gennis72f096f2012-08-27 18:48:37 -0700337 // setBufferCount so we know whether to perform the min undequeued
338 // buffers check below.
Daniel Lameae59d22012-01-22 15:26:27 -0800339 if (mBufferHasBeenQueued) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800340 // make sure the client is not trying to dequeue more buffers
341 // than allowed.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700342 const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700343 const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700344 if (newUndequeuedCount < minUndequeuedCount) {
345 ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
346 "exceeded (dequeued=%d undequeudCount=%d)",
347 minUndequeuedCount, dequeuedCount,
348 newUndequeuedCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800349 return -EBUSY;
350 }
351 }
352
Jamie Gennise191e6c2012-08-24 20:26:34 -0700353 // If no buffer is found, wait for a buffer to be released or for
354 // the max buffer count to change.
Daniel Lamc2c1f2f2012-03-07 14:11:29 -0800355 tryAgain = found == INVALID_BUFFER_SLOT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800356 if (tryAgain) {
Mathias Agopian6bac3632013-07-23 21:50:20 -0700357 // return an error if we're in "cannot block" mode (producer and consumer
358 // are controlled by the application) -- however, the consumer is allowed
359 // to acquire briefly an extra buffer (which could cause us to have to wait here)
360 // and that's okay because we know the wait will be brief (it happens
361 // if we dequeue a buffer while the consumer has acquired one but not released
362 // the old one yet -- for e.g.: see GLConsumer::updateTexImage()).
363 if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) {
Mathias Agopian595264f2013-07-16 22:56:09 -0700364 ST_LOGE("dequeueBuffer: would block! returning an error instead.");
365 return WOULD_BLOCK;
366 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800367 mDequeueCondition.wait(mMutex);
368 }
369 }
370
Daniel Lam6b091c52012-01-22 15:26:27 -0800371
372 if (found == INVALID_BUFFER_SLOT) {
373 // This should not happen.
374 ST_LOGE("dequeueBuffer: no available buffer slots");
375 return -EBUSY;
376 }
377
378 const int buf = found;
379 *outBuf = found;
380
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800381 ATRACE_BUFFER_INDEX(buf);
382
Daniel Lam6b091c52012-01-22 15:26:27 -0800383 const bool useDefaultSize = !w && !h;
384 if (useDefaultSize) {
385 // use the default size
386 w = mDefaultWidth;
387 h = mDefaultHeight;
388 }
389
Daniel Lam6b091c52012-01-22 15:26:27 -0800390 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
391
392 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
393 if ((buffer == NULL) ||
394 (uint32_t(buffer->width) != w) ||
395 (uint32_t(buffer->height) != h) ||
396 (uint32_t(buffer->format) != format) ||
397 ((uint32_t(buffer->usage) & usage) != usage))
398 {
Daniel Lameae59d22012-01-22 15:26:27 -0800399 mSlots[buf].mAcquireCalled = false;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700400 mSlots[buf].mGraphicBuffer = NULL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800401 mSlots[buf].mRequestBufferCalled = false;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700402 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800403 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800404 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
405
Andy McFadden2adaf042012-12-18 09:49:45 -0800406 returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
Daniel Lam6b091c52012-01-22 15:26:27 -0800407 }
408
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700409
410 if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) {
411 ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. "
412 "buf=%d, w=%d, h=%d, format=%d",
413 buf, buffer->width, buffer->height, buffer->format);
414 }
415
Daniel Lam6b091c52012-01-22 15:26:27 -0800416 dpy = mSlots[buf].mEglDisplay;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700417 eglFence = mSlots[buf].mEglFence;
Jesse Hall4c00cc12013-03-15 21:34:30 -0700418 *outFence = mSlots[buf].mFence;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700419 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800420 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800421 } // end lock scope
Daniel Lam6b091c52012-01-22 15:26:27 -0800422
Andy McFadden2adaf042012-12-18 09:49:45 -0800423 if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
Jamie Gennis1efe0992012-10-04 18:34:01 -0700424 status_t error;
425 sp<GraphicBuffer> graphicBuffer(
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700426 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error));
Jamie Gennis1efe0992012-10-04 18:34:01 -0700427 if (graphicBuffer == 0) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700428 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700429 return error;
430 }
431
432 { // Scope for the lock
433 Mutex::Autolock lock(mMutex);
434
435 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800436 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700437 return NO_INIT;
438 }
439
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700440 mSlots[*outBuf].mFrameNumber = ~0;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700441 mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
442 }
443 }
444
Jesse Hallc777b0b2012-06-28 12:52:05 -0700445 if (eglFence != EGL_NO_SYNC_KHR) {
446 EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
Daniel Lam6b091c52012-01-22 15:26:27 -0800447 // If something goes wrong, log the error, but return the buffer without
448 // synchronizing access to it. It's too late at this point to abort the
449 // dequeue operation.
450 if (result == EGL_FALSE) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700451 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
Daniel Lam6b091c52012-01-22 15:26:27 -0800452 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700453 ST_LOGE("dequeueBuffer: timeout waiting for fence");
Daniel Lam6b091c52012-01-22 15:26:27 -0800454 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700455 eglDestroySyncKHR(dpy, eglFence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800456 }
457
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700458 ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
459 mSlots[*outBuf].mFrameNumber,
Daniel Lam6b091c52012-01-22 15:26:27 -0800460 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
461
462 return returnFlags;
463}
464
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700465status_t BufferQueue::queueBuffer(int buf,
466 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800467 ATRACE_CALL();
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800468 ATRACE_BUFFER_INDEX(buf);
469
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700470 Rect crop;
471 uint32_t transform;
472 int scalingMode;
473 int64_t timestamp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700474 bool async;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700475 sp<Fence> fence;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700476
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700477 input.deflate(&timestamp, &crop, &scalingMode, &transform, &async, &fence);
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700478
Jamie Gennis1df8c342012-12-20 14:05:45 -0800479 if (fence == NULL) {
480 ST_LOGE("queueBuffer: fence is NULL");
481 return BAD_VALUE;
482 }
483
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700484 switch (scalingMode) {
485 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
486 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
487 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
488 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
489 break;
490 default:
491 ST_LOGE("unknown scaling mode: %d", scalingMode);
492 return -EINVAL;
493 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800494
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700495 sp<ConsumerListener> listener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800496
497 { // scope for the lock
498 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700499
Daniel Lam6b091c52012-01-22 15:26:27 -0800500 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800501 ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800502 return NO_INIT;
503 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700504
505 const int maxBufferCount = getMaxBufferCountLocked(async);
506 if (async && mOverrideMaxBufferCount) {
507 // FIXME: some drivers are manually setting the buffer-count (which they
508 // shouldn't), so we do this extra test here to handle that case.
509 // This is TEMPORARY, until we get this fixed.
510 if (mOverrideMaxBufferCount < maxBufferCount) {
511 ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
512 return BAD_VALUE;
513 }
514 }
Jamie Gennise191e6c2012-08-24 20:26:34 -0700515 if (buf < 0 || buf >= maxBufferCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800516 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennise191e6c2012-08-24 20:26:34 -0700517 maxBufferCount, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800518 return -EINVAL;
519 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
520 ST_LOGE("queueBuffer: slot %d is not owned by the client "
521 "(state=%d)", buf, mSlots[buf].mBufferState);
522 return -EINVAL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800523 } else if (!mSlots[buf].mRequestBufferCalled) {
524 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
525 "buffer", buf);
526 return -EINVAL;
527 }
528
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700529 ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
530 "tr=%#x scale=%s",
531 buf, mFrameCounter + 1, timestamp,
532 crop.left, crop.top, crop.right, crop.bottom,
533 transform, scalingModeName(scalingMode));
534
Jamie Gennisd72f2332012-05-07 13:50:11 -0700535 const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
536 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
537 Rect croppedCrop;
538 crop.intersect(bufferRect, &croppedCrop);
539 if (croppedCrop != crop) {
540 ST_LOGE("queueBuffer: crop rect is not contained within the "
541 "buffer in slot %d", buf);
542 return -EINVAL;
543 }
544
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700545 mSlots[buf].mFence = fence;
546 mSlots[buf].mBufferState = BufferSlot::QUEUED;
547 mFrameCounter++;
548 mSlots[buf].mFrameNumber = mFrameCounter;
549
550 BufferItem item;
551 item.mAcquireCalled = mSlots[buf].mAcquireCalled;
552 item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
553 item.mCrop = crop;
554 item.mTransform = transform;
555 item.mScalingMode = scalingMode;
556 item.mTimestamp = timestamp;
557 item.mFrameNumber = mFrameCounter;
558 item.mBuf = buf;
559 item.mFence = fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700560 item.mIsDroppable = mDequeueBufferCannotBlock || async;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700561
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700562 if (mQueue.empty()) {
563 // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
564 // simply queue this buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700565 mQueue.push_back(item);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700566 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800567 } else {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700568 // when the queue is not empty, we need to look at the front buffer
569 // state and see if we need to replace it.
570 Fifo::iterator front(mQueue.begin());
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700571 if (front->mIsDroppable) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700572 // buffer slot currently queued is marked free if still tracked
573 if (stillTracking(front)) {
574 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
Mathias Agopian26a6f372013-07-18 22:25:55 -0700575 // reset the frame number of the freed buffer so that it is the first in
576 // line to be dequeued again.
577 mSlots[front->mBuf].mFrameNumber = 0;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700578 }
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700579 // and we record the new buffer in the queued list
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700580 *front = item;
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700581 } else {
582 mQueue.push_back(item);
583 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800584 }
585 }
586
Daniel Lameae59d22012-01-22 15:26:27 -0800587 mBufferHasBeenQueued = true;
Dave Burke74ff8c22012-03-12 21:49:41 -0700588 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800589
Mathias Agopian2488b202012-04-20 17:19:28 -0700590 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
591 mQueue.size());
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800592
593 ATRACE_INT(mConsumerName.string(), mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800594 } // scope for the lock
595
596 // call back without lock held
597 if (listener != 0) {
598 listener->onFrameAvailable();
599 }
Andy McFadden753e3412013-04-04 17:09:03 -0700600 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800601}
602
Jesse Hall4c00cc12013-03-15 21:34:30 -0700603void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800604 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800605 ST_LOGV("cancelBuffer: slot=%d", buf);
606 Mutex::Autolock lock(mMutex);
607
608 if (mAbandoned) {
609 ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
610 return;
611 }
612
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700613 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800614 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700615 NUM_BUFFER_SLOTS, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800616 return;
617 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
618 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
619 buf, mSlots[buf].mBufferState);
620 return;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800621 } else if (fence == NULL) {
622 ST_LOGE("cancelBuffer: fence is NULL");
623 return;
Daniel Lam6b091c52012-01-22 15:26:27 -0800624 }
625 mSlots[buf].mBufferState = BufferSlot::FREE;
626 mSlots[buf].mFrameNumber = 0;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700627 mSlots[buf].mFence = fence;
Dave Burke74ff8c22012-03-12 21:49:41 -0700628 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800629}
630
Mathias Agopian595264f2013-07-16 22:56:09 -0700631status_t BufferQueue::connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800632 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800633 ST_LOGV("connect: api=%d", api);
634 Mutex::Autolock lock(mMutex);
635
636 if (mAbandoned) {
637 ST_LOGE("connect: BufferQueue has been abandoned!");
638 return NO_INIT;
639 }
640
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700641 if (mConsumerListener == NULL) {
642 ST_LOGE("connect: BufferQueue has no consumer!");
643 return NO_INIT;
644 }
645
Daniel Lam6b091c52012-01-22 15:26:27 -0800646 int err = NO_ERROR;
647 switch (api) {
648 case NATIVE_WINDOW_API_EGL:
649 case NATIVE_WINDOW_API_CPU:
650 case NATIVE_WINDOW_API_MEDIA:
651 case NATIVE_WINDOW_API_CAMERA:
652 if (mConnectedApi != NO_CONNECTED_API) {
653 ST_LOGE("connect: already connected (cur=%d, req=%d)",
654 mConnectedApi, api);
655 err = -EINVAL;
656 } else {
657 mConnectedApi = api;
Mathias Agopian2488b202012-04-20 17:19:28 -0700658 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
659 mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800660 }
661 break;
662 default:
663 err = -EINVAL;
664 break;
665 }
Daniel Lameae59d22012-01-22 15:26:27 -0800666
667 mBufferHasBeenQueued = false;
Mathias Agopian595264f2013-07-16 22:56:09 -0700668 mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
Daniel Lameae59d22012-01-22 15:26:27 -0800669
Daniel Lam6b091c52012-01-22 15:26:27 -0800670 return err;
671}
672
673status_t BufferQueue::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800674 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800675 ST_LOGV("disconnect: api=%d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800676
677 int err = NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700678 sp<ConsumerListener> listener;
679
680 { // Scope for the lock
681 Mutex::Autolock lock(mMutex);
682
683 if (mAbandoned) {
684 // it is not really an error to disconnect after the surface
685 // has been abandoned, it should just be a no-op.
686 return NO_ERROR;
687 }
688
689 switch (api) {
690 case NATIVE_WINDOW_API_EGL:
691 case NATIVE_WINDOW_API_CPU:
692 case NATIVE_WINDOW_API_MEDIA:
693 case NATIVE_WINDOW_API_CAMERA:
694 if (mConnectedApi == api) {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700695 freeAllBuffersLocked();
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700696 mConnectedApi = NO_CONNECTED_API;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700697 mDequeueCondition.broadcast();
698 listener = mConsumerListener;
699 } else {
700 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
701 mConnectedApi, api);
702 err = -EINVAL;
703 }
704 break;
705 default:
706 ST_LOGE("disconnect: unknown API %d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800707 err = -EINVAL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700708 break;
709 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800710 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700711
712 if (listener != NULL) {
713 listener->onBuffersReleased();
714 }
715
Daniel Lam6b091c52012-01-22 15:26:27 -0800716 return err;
717}
718
Mathias Agopian74d211a2013-04-22 16:55:35 +0200719void BufferQueue::dump(String8& result) const {
720 BufferQueue::dump(result, "");
Daniel Lameae59d22012-01-22 15:26:27 -0800721}
722
Mathias Agopian74d211a2013-04-22 16:55:35 +0200723void BufferQueue::dump(String8& result, const char* prefix) const {
Daniel Lameae59d22012-01-22 15:26:27 -0800724 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800725
726 String8 fifo;
727 int fifoSize = 0;
728 Fifo::const_iterator i(mQueue.begin());
729 while (i != mQueue.end()) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700730 fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
731 "xform=0x%02x, time=%#llx, scale=%s\n",
732 i->mBuf, i->mGraphicBuffer.get(),
733 i->mCrop.left, i->mCrop.top, i->mCrop.right,
734 i->mCrop.bottom, i->mTransform, i->mTimestamp,
735 scalingModeName(i->mScalingMode)
736 );
737 i++;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200738 fifoSize++;
Daniel Lameae59d22012-01-22 15:26:27 -0800739 }
740
Jamie Gennise191e6c2012-08-24 20:26:34 -0700741
Mathias Agopian74d211a2013-04-22 16:55:35 +0200742 result.appendFormat(
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700743 "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
Andy McFadden69052052012-09-14 16:10:11 -0700744 "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700745 prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
Andy McFadden69052052012-09-14 16:10:11 -0700746 mDefaultHeight, mDefaultBufferFormat, mTransformHint,
747 fifoSize, fifo.string());
Daniel Lameae59d22012-01-22 15:26:27 -0800748
749 struct {
750 const char * operator()(int state) const {
751 switch (state) {
752 case BufferSlot::DEQUEUED: return "DEQUEUED";
753 case BufferSlot::QUEUED: return "QUEUED";
754 case BufferSlot::FREE: return "FREE";
755 case BufferSlot::ACQUIRED: return "ACQUIRED";
756 default: return "Unknown";
757 }
758 }
759 } stateName;
760
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700761 // just trim the free buffers to not spam the dump
762 int maxBufferCount = 0;
763 for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
764 const BufferSlot& slot(mSlots[i]);
765 if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
766 maxBufferCount = i+1;
767 break;
768 }
769 }
770
Jamie Gennise191e6c2012-08-24 20:26:34 -0700771 for (int i=0 ; i<maxBufferCount ; i++) {
Daniel Lameae59d22012-01-22 15:26:27 -0800772 const BufferSlot& slot(mSlots[i]);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700773 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200774 result.appendFormat(
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700775 "%s%s[%02d:%p] state=%-8s",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700776 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700777 stateName(slot.mBufferState)
Daniel Lameae59d22012-01-22 15:26:27 -0800778 );
Daniel Lameae59d22012-01-22 15:26:27 -0800779
Daniel Lameae59d22012-01-22 15:26:27 -0800780 if (buf != NULL) {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200781 result.appendFormat(
Daniel Lameae59d22012-01-22 15:26:27 -0800782 ", %p [%4ux%4u:%4u,%3X]",
783 buf->handle, buf->width, buf->height, buf->stride,
784 buf->format);
Daniel Lameae59d22012-01-22 15:26:27 -0800785 }
786 result.append("\n");
787 }
788}
789
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700790void BufferQueue::freeBufferLocked(int slot) {
791 ST_LOGV("freeBufferLocked: slot=%d", slot);
792 mSlots[slot].mGraphicBuffer = 0;
793 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
794 mSlots[slot].mNeedsCleanupOnRelease = true;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700795 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700796 mSlots[slot].mBufferState = BufferSlot::FREE;
797 mSlots[slot].mFrameNumber = 0;
798 mSlots[slot].mAcquireCalled = false;
Daniel Lameae59d22012-01-22 15:26:27 -0800799
800 // destroy fence as BufferQueue now takes ownership
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700801 if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
802 eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
803 mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800804 }
Jamie Gennis1df8c342012-12-20 14:05:45 -0800805 mSlots[slot].mFence = Fence::NO_FENCE;
Daniel Lam6b091c52012-01-22 15:26:27 -0800806}
807
808void BufferQueue::freeAllBuffersLocked() {
Daniel Lameae59d22012-01-22 15:26:27 -0800809 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800810 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
811 freeBufferLocked(i);
812 }
813}
814
Andy McFadden14fab7d2013-08-01 13:37:42 -0700815status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800816 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -0800817 Mutex::Autolock _l(mMutex);
Jamie Gennis5e5efde2012-08-28 17:18:50 -0700818
819 // Check that the consumer doesn't currently have the maximum number of
820 // buffers acquired. We allow the max buffer count to be exceeded by one
821 // buffer, so that the consumer can successfully set up the newly acquired
822 // buffer before releasing the old one.
823 int numAcquiredBuffers = 0;
824 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
825 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
826 numAcquiredBuffers++;
827 }
828 }
829 if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
830 ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
831 numAcquiredBuffers, mMaxAcquiredBufferCount);
832 return INVALID_OPERATION;
833 }
834
Daniel Lameae59d22012-01-22 15:26:27 -0800835 // check if queue is empty
836 // In asynchronous mode the list is guaranteed to be one buffer
837 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700838 if (mQueue.empty()) {
Daniel Lamfbcda932012-04-09 22:51:52 -0700839 return NO_BUFFER_AVAILABLE;
Daniel Lameae59d22012-01-22 15:26:27 -0800840 }
841
Andy McFadden1585c4d2013-06-28 13:52:40 -0700842 Fifo::iterator front(mQueue.begin());
Andy McFadden1585c4d2013-06-28 13:52:40 -0700843
Andy McFadden14fab7d2013-08-01 13:37:42 -0700844 // If expectedPresent is specified, we may not want to return a buffer yet.
845 // If it's specified and there's more than one buffer queued, we may
846 // want to drop a buffer.
847 if (expectedPresent != 0) {
848 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
849
850 // The "expectedPresent" argument indicates when the buffer is expected
851 // to be presented on-screen. If the buffer's desired-present time
852 // is earlier (less) than expectedPresent, meaning it'll be displayed
853 // on time or possibly late if we show it ASAP, we acquire and return
854 // it. If we don't want to display it until after the expectedPresent
855 // time, we return PRESENT_LATER without acquiring it.
856 //
857 // To be safe, we don't defer acquisition if expectedPresent is
858 // more than one second in the future beyond the desired present time
859 // (i.e. we'd be holding the buffer for a long time).
860 //
861 // NOTE: code assumes monotonic time values from the system clock are
862 // positive.
863 while (mQueue.size() > 1) {
864 // If entry[1] is timely, drop entry[0] (and repeat). We apply
865 // an additional criteria here: we only drop the earlier buffer if
866 // our desiredPresent falls within +/- 1 second of the expected
867 // present. Otherwise, bogus desiredPresent times (e.g. 0 or
868 // a small relative timestamp), which normally mean "ignore the
869 // timestamp and acquire immediately", would cause us to drop
870 // frames.
871 //
872 // We may want to add an additional criteria: don't drop the
873 // earlier buffer if entry[1]'s fence hasn't signaled yet.
874 //
875 // (Vector front is [0], back is [size()-1])
876 const BufferItem& bi(mQueue[1]);
877 nsecs_t desiredPresent = bi.mTimestamp;
878 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
879 desiredPresent > expectedPresent) {
880 // This buffer is set to display in the near future, or
881 // desiredPresent is garbage. Either way we don't want to
882 // drop the previous buffer just to get this on screen sooner.
883 ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld",
884 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
885 systemTime(CLOCK_MONOTONIC));
886 break;
887 }
888 ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d",
889 desiredPresent, expectedPresent, mQueue.size());
890 if (stillTracking(front)) {
891 // front buffer is still in mSlots, so mark the slot as free
892 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
893 }
894 mQueue.erase(front);
895 front = mQueue.begin();
896 }
897
898 // See if the front buffer is due.
899 nsecs_t desiredPresent = front->mTimestamp;
900 if (desiredPresent > expectedPresent &&
901 desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
902 ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld",
903 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
904 systemTime(CLOCK_MONOTONIC));
905 return PRESENT_LATER;
906 }
907
908 ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld",
909 desiredPresent, expectedPresent, desiredPresent - expectedPresent,
Andy McFadden1585c4d2013-06-28 13:52:40 -0700910 systemTime(CLOCK_MONOTONIC));
Andy McFadden1585c4d2013-06-28 13:52:40 -0700911 }
912
Andy McFadden14fab7d2013-08-01 13:37:42 -0700913 int buf = front->mBuf;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700914 *buffer = *front;
915 ATRACE_BUFFER_INDEX(buf);
916
917 ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
918 front->mBuf, front->mFrameNumber,
919 front->mGraphicBuffer->handle);
920 // if front buffer still being tracked update slot state
921 if (stillTracking(front)) {
922 mSlots[buf].mAcquireCalled = true;
923 mSlots[buf].mNeedsCleanupOnRelease = false;
924 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
925 mSlots[buf].mFence = Fence::NO_FENCE;
926 }
927
928 // If the buffer has previously been acquired by the consumer, set
929 // mGraphicBuffer to NULL to avoid unnecessarily remapping this
930 // buffer on the consumer side.
931 if (buffer->mAcquireCalled) {
932 buffer->mGraphicBuffer = NULL;
933 }
934
935 mQueue.erase(front);
936 mDequeueCondition.broadcast();
937
938 ATRACE_INT(mConsumerName.string(), mQueue.size());
939
Andy McFadden753e3412013-04-04 17:09:03 -0700940 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800941}
942
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700943status_t BufferQueue::releaseBuffer(
944 int buf, uint64_t frameNumber, EGLDisplay display,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700945 EGLSyncKHR eglFence, const sp<Fence>& fence) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800946 ATRACE_CALL();
947 ATRACE_BUFFER_INDEX(buf);
948
Jamie Gennis1df8c342012-12-20 14:05:45 -0800949 if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
950 return BAD_VALUE;
Daniel Lameae59d22012-01-22 15:26:27 -0800951 }
952
Mathias Agopian207c1e22013-07-22 18:00:53 -0700953 Mutex::Autolock _l(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700954
955 // If the frame number has changed because buffer has been reallocated,
956 // we can ignore this releaseBuffer for the old buffer.
957 if (frameNumber != mSlots[buf].mFrameNumber) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700958 return STALE_BUFFER_SLOT;
959 }
Mathias Agopian207c1e22013-07-22 18:00:53 -0700960
961
962 // Internal state consistency checks:
963 // Make sure this buffers hasn't been queued while we were owning it (acquired)
964 Fifo::iterator front(mQueue.begin());
965 Fifo::const_iterator const end(mQueue.end());
966 while (front != end) {
967 if (front->mBuf == buf) {
968 LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
969 "acquired", mConsumerName.string(), frameNumber, buf);
970 break; // never reached
971 }
972 front++;
973 }
Daniel Lameae59d22012-01-22 15:26:27 -0800974
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700975 // The buffer can now only be released if its in the acquired state
976 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700977 mSlots[buf].mEglDisplay = display;
978 mSlots[buf].mEglFence = eglFence;
979 mSlots[buf].mFence = fence;
Daniel Lameae59d22012-01-22 15:26:27 -0800980 mSlots[buf].mBufferState = BufferSlot::FREE;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700981 } else if (mSlots[buf].mNeedsCleanupOnRelease) {
982 ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
983 mSlots[buf].mNeedsCleanupOnRelease = false;
984 return STALE_BUFFER_SLOT;
985 } else {
986 ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
987 return -EINVAL;
Daniel Lameae59d22012-01-22 15:26:27 -0800988 }
Daniel Lameae59d22012-01-22 15:26:27 -0800989
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700990 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -0700991 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800992}
993
Mathias Agopian595264f2013-07-16 22:56:09 -0700994status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener,
995 bool controlledByApp) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700996 ST_LOGV("consumerConnect");
Daniel Lameae59d22012-01-22 15:26:27 -0800997 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800998
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700999 if (mAbandoned) {
1000 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
1001 return NO_INIT;
1002 }
Andy McFadden753e3412013-04-04 17:09:03 -07001003 if (consumerListener == NULL) {
1004 ST_LOGE("consumerConnect: consumerListener may not be NULL");
1005 return BAD_VALUE;
1006 }
Daniel Lamb2675792012-02-23 14:35:13 -08001007
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001008 mConsumerListener = consumerListener;
Mathias Agopian595264f2013-07-16 22:56:09 -07001009 mConsumerControlledByApp = controlledByApp;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001010
Andy McFadden753e3412013-04-04 17:09:03 -07001011 return NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001012}
1013
1014status_t BufferQueue::consumerDisconnect() {
1015 ST_LOGV("consumerDisconnect");
1016 Mutex::Autolock lock(mMutex);
1017
1018 if (mConsumerListener == NULL) {
1019 ST_LOGE("consumerDisconnect: No consumer is connected!");
1020 return -EINVAL;
1021 }
1022
1023 mAbandoned = true;
1024 mConsumerListener = NULL;
Daniel Lamb2675792012-02-23 14:35:13 -08001025 mQueue.clear();
Daniel Lameae59d22012-01-22 15:26:27 -08001026 freeAllBuffersLocked();
Dave Burke74ff8c22012-03-12 21:49:41 -07001027 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -07001028 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001029}
1030
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001031status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
1032 ST_LOGV("getReleasedBuffers");
1033 Mutex::Autolock lock(mMutex);
1034
1035 if (mAbandoned) {
1036 ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
1037 return NO_INIT;
1038 }
1039
1040 uint32_t mask = 0;
1041 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1042 if (!mSlots[i].mAcquireCalled) {
1043 mask |= 1 << i;
1044 }
1045 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001046
1047 // Remove buffers in flight (on the queue) from the mask where acquire has
1048 // been called, as the consumer will not receive the buffer address, so
1049 // it should not free these slots.
1050 Fifo::iterator front(mQueue.begin());
1051 while (front != mQueue.end()) {
1052 if (front->mAcquireCalled)
1053 mask &= ~(1 << front->mBuf);
1054 front++;
1055 }
1056
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001057 *slotMask = mask;
1058
1059 ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1060 return NO_ERROR;
1061}
1062
Mathias Agopian207c1e22013-07-22 18:00:53 -07001063status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
Daniel Lameae59d22012-01-22 15:26:27 -08001064 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1065 if (!w || !h) {
1066 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1067 w, h);
1068 return BAD_VALUE;
1069 }
1070
1071 Mutex::Autolock lock(mMutex);
1072 mDefaultWidth = w;
1073 mDefaultHeight = h;
Andy McFadden753e3412013-04-04 17:09:03 -07001074 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001075}
1076
Jamie Gennis31a353d2012-08-24 17:25:13 -07001077status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001078 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -08001079 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -07001080 return setDefaultMaxBufferCountLocked(bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -08001081}
1082
Mathias Agopianad678e12013-07-23 17:28:53 -07001083status_t BufferQueue::disableAsyncBuffer() {
1084 ATRACE_CALL();
1085 Mutex::Autolock lock(mMutex);
1086 if (mConsumerListener != NULL) {
1087 ST_LOGE("disableAsyncBuffer: consumer already connected!");
1088 return INVALID_OPERATION;
1089 }
1090 mUseAsyncBuffer = false;
1091 return NO_ERROR;
1092}
1093
Jamie Gennis72f096f2012-08-27 18:48:37 -07001094status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1095 ATRACE_CALL();
1096 Mutex::Autolock lock(mMutex);
Jamie Gennisc68f2ec2012-08-30 18:36:22 -07001097 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1098 ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1099 maxAcquiredBuffers);
1100 return BAD_VALUE;
1101 }
Jamie Gennis72f096f2012-08-27 18:48:37 -07001102 if (mConnectedApi != NO_CONNECTED_API) {
1103 return INVALID_OPERATION;
1104 }
1105 mMaxAcquiredBufferCount = maxAcquiredBuffers;
Andy McFadden753e3412013-04-04 17:09:03 -07001106 return NO_ERROR;
Jamie Gennis72f096f2012-08-27 18:48:37 -07001107}
1108
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001109int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
Mathias Agopianad678e12013-07-23 17:28:53 -07001110 // if dequeueBuffer is allowed to error out, we don't have to
1111 // add an extra buffer.
1112 if (!mUseAsyncBuffer)
1113 return mMaxAcquiredBufferCount;
1114
1115 // we're in async mode, or we want to prevent the app to
1116 // deadlock itself, we throw-in an extra buffer to guarantee it.
1117 if (mDequeueBufferCannotBlock || async)
1118 return mMaxAcquiredBufferCount+1;
1119
1120 return mMaxAcquiredBufferCount;
Jamie Gennis31a353d2012-08-24 17:25:13 -07001121}
1122
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001123int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
1124 return getMinUndequeuedBufferCount(async) + 1;
1125}
1126
1127int BufferQueue::getMaxBufferCountLocked(bool async) const {
1128 int minMaxBufferCount = getMinMaxBufferCountLocked(async);
Jamie Gennise191e6c2012-08-24 20:26:34 -07001129
1130 int maxBufferCount = mDefaultMaxBufferCount;
1131 if (maxBufferCount < minMaxBufferCount) {
1132 maxBufferCount = minMaxBufferCount;
1133 }
1134 if (mOverrideMaxBufferCount != 0) {
1135 assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1136 maxBufferCount = mOverrideMaxBufferCount;
1137 }
1138
1139 // Any buffers that are dequeued by the producer or sitting in the queue
1140 // waiting to be consumed need to have their slots preserved. Such
1141 // buffers will temporarily keep the max buffer count up until the slots
1142 // no longer need to be preserved.
1143 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1144 BufferSlot::BufferState state = mSlots[i].mBufferState;
1145 if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1146 maxBufferCount = i + 1;
1147 }
1148 }
1149
1150 return maxBufferCount;
1151}
1152
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001153bool BufferQueue::stillTracking(const BufferItem *item) const {
1154 const BufferSlot &slot = mSlots[item->mBuf];
1155
1156 ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1157 "slot: { slot=%d/%llu, buffer=%p }",
1158 item->mBuf, item->mFrameNumber,
1159 (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1160 item->mBuf, slot.mFrameNumber,
1161 (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1162
1163 // Compare item with its original buffer slot. We can check the slot
1164 // as the buffer would not be moved to a different slot by the producer.
1165 return (slot.mGraphicBuffer != NULL &&
1166 item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1167}
1168
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001169BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1170 const wp<BufferQueue::ConsumerListener>& consumerListener):
1171 mConsumerListener(consumerListener) {}
1172
1173BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1174
1175void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1176 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1177 if (listener != NULL) {
1178 listener->onFrameAvailable();
1179 }
1180}
1181
1182void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1183 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1184 if (listener != NULL) {
1185 listener->onBuffersReleased();
1186 }
1187}
1188
Daniel Lam6b091c52012-01-22 15:26:27 -08001189}; // namespace android