blob: 73bd4888ba6edb8d5e1f0a0598e6f92cd13d9d84 [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),
Daniel Lam6b091c52012-01-22 15:26:27 -080075 mConnectedApi(NO_CONNECTED_API),
76 mAbandoned(false),
Daniel Lameae59d22012-01-22 15:26:27 -080077 mFrameCounter(0),
Daniel Lamb2675792012-02-23 14:35:13 -080078 mBufferHasBeenQueued(false),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070079 mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
Daniel Lamb2675792012-02-23 14:35:13 -080080 mConsumerUsageBits(0),
81 mTransformHint(0)
Daniel Lam6b091c52012-01-22 15:26:27 -080082{
83 // Choose a name using the PID and a process-unique ID.
Daniel Lameae59d22012-01-22 15:26:27 -080084 mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Daniel Lam6b091c52012-01-22 15:26:27 -080085
86 ST_LOGV("BufferQueue");
Mathias Agopian3e876012012-06-07 17:52:54 -070087 if (allocator == NULL) {
88 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
89 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
90 if (mGraphicBufferAlloc == 0) {
91 ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
92 }
93 } else {
94 mGraphicBufferAlloc = allocator;
Daniel Lamabe61bf2012-03-26 20:37:15 -070095 }
Daniel Lam6b091c52012-01-22 15:26:27 -080096}
97
98BufferQueue::~BufferQueue() {
99 ST_LOGV("~BufferQueue");
100}
101
Jamie Gennis31a353d2012-08-24 17:25:13 -0700102status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
Jamie Gennise191e6c2012-08-24 20:26:34 -0700103 if (count < 2 || count > NUM_BUFFER_SLOTS)
Daniel Lam6b091c52012-01-22 15:26:27 -0800104 return BAD_VALUE;
105
Jamie Gennis31a353d2012-08-24 17:25:13 -0700106 mDefaultMaxBufferCount = count;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700107 mDequeueCondition.broadcast();
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700108
Andy McFadden753e3412013-04-04 17:09:03 -0700109 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800110}
111
Daniel Lameae59d22012-01-22 15:26:27 -0800112void BufferQueue::setConsumerName(const String8& name) {
113 Mutex::Autolock lock(mMutex);
114 mConsumerName = name;
115}
116
Daniel Lamb2675792012-02-23 14:35:13 -0800117status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
118 Mutex::Autolock lock(mMutex);
119 mDefaultBufferFormat = defaultFormat;
Andy McFadden753e3412013-04-04 17:09:03 -0700120 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800121}
122
123status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
124 Mutex::Autolock lock(mMutex);
125 mConsumerUsageBits = usage;
Andy McFadden753e3412013-04-04 17:09:03 -0700126 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800127}
128
129status_t BufferQueue::setTransformHint(uint32_t hint) {
Andy McFadden69052052012-09-14 16:10:11 -0700130 ST_LOGV("setTransformHint: %02x", hint);
Daniel Lamb2675792012-02-23 14:35:13 -0800131 Mutex::Autolock lock(mMutex);
132 mTransformHint = hint;
Andy McFadden753e3412013-04-04 17:09:03 -0700133 return NO_ERROR;
Daniel Lamb2675792012-02-23 14:35:13 -0800134}
135
Daniel Lam6b091c52012-01-22 15:26:27 -0800136status_t BufferQueue::setBufferCount(int bufferCount) {
137 ST_LOGV("setBufferCount: count=%d", bufferCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800138
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700139 sp<ConsumerListener> listener;
140 {
141 Mutex::Autolock lock(mMutex);
Daniel Lam6b091c52012-01-22 15:26:27 -0800142
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700143 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800144 ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700145 return NO_INIT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800146 }
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700147 if (bufferCount > NUM_BUFFER_SLOTS) {
Andy McFadden753e3412013-04-04 17:09:03 -0700148 ST_LOGE("setBufferCount: bufferCount too large (max %d)",
149 NUM_BUFFER_SLOTS);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700150 return BAD_VALUE;
151 }
152
153 // Error out if the user has dequeued buffers
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700154 for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700155 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
156 ST_LOGE("setBufferCount: client owns some buffers");
157 return -EINVAL;
158 }
159 }
160
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700161 if (bufferCount == 0) {
Jamie Gennis31a353d2012-08-24 17:25:13 -0700162 mOverrideMaxBufferCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700163 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -0700164 return NO_ERROR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700165 }
166
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700167 // fine to assume async to false before we're setting the buffer count
168 const int minBufferSlots = getMinMaxBufferCountLocked(false);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700169 if (bufferCount < minBufferSlots) {
170 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
171 "minimum (%d)", bufferCount, minBufferSlots);
172 return BAD_VALUE;
173 }
174
175 // here we're guaranteed that the client doesn't have dequeued buffers
Lajos Molnar9e3cb552013-05-06 16:23:07 -0700176 // and will release all of its buffer references. We don't clear the
177 // queue, however, so currently queued buffers still get displayed.
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700178 freeAllBuffersLocked();
Jamie Gennis31a353d2012-08-24 17:25:13 -0700179 mOverrideMaxBufferCount = bufferCount;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700180 mDequeueCondition.broadcast();
181 listener = mConsumerListener;
182 } // scope for lock
183
184 if (listener != NULL) {
185 listener->onBuffersReleased();
Daniel Lam6b091c52012-01-22 15:26:27 -0800186 }
187
Andy McFadden753e3412013-04-04 17:09:03 -0700188 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800189}
190
Daniel Lamb8560522012-01-30 15:51:27 -0800191int BufferQueue::query(int what, int* outValue)
192{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800193 ATRACE_CALL();
Daniel Lamb8560522012-01-30 15:51:27 -0800194 Mutex::Autolock lock(mMutex);
195
196 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800197 ST_LOGE("query: BufferQueue has been abandoned!");
Daniel Lamb8560522012-01-30 15:51:27 -0800198 return NO_INIT;
199 }
200
201 int value;
202 switch (what) {
203 case NATIVE_WINDOW_WIDTH:
204 value = mDefaultWidth;
205 break;
206 case NATIVE_WINDOW_HEIGHT:
207 value = mDefaultHeight;
208 break;
209 case NATIVE_WINDOW_FORMAT:
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700210 value = mDefaultBufferFormat;
Daniel Lamb8560522012-01-30 15:51:27 -0800211 break;
212 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700213 value = getMinUndequeuedBufferCount(false);
Daniel Lamb8560522012-01-30 15:51:27 -0800214 break;
Mathias Agopian2488b202012-04-20 17:19:28 -0700215 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
216 value = (mQueue.size() >= 2);
217 break;
Daniel Lamb8560522012-01-30 15:51:27 -0800218 default:
219 return BAD_VALUE;
220 }
221 outValue[0] = value;
222 return NO_ERROR;
223}
224
Daniel Lam6b091c52012-01-22 15:26:27 -0800225status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800226 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800227 ST_LOGV("requestBuffer: slot=%d", slot);
228 Mutex::Autolock lock(mMutex);
229 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800230 ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800231 return NO_INIT;
232 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700233 if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800234 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700235 NUM_BUFFER_SLOTS, slot);
Jamie Gennise191e6c2012-08-24 20:26:34 -0700236 return BAD_VALUE;
237 } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennise191e6c2012-08-24 20:26:34 -0700238 ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
239 slot, mSlots[slot].mBufferState);
Daniel Lam6b091c52012-01-22 15:26:27 -0800240 return BAD_VALUE;
241 }
242 mSlots[slot].mRequestBufferCalled = true;
243 *buf = mSlots[slot].mGraphicBuffer;
244 return NO_ERROR;
245}
246
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700247status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700248 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800249 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800250 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
251
252 if ((w && !h) || (!w && h)) {
253 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
254 return BAD_VALUE;
255 }
256
257 status_t returnFlags(OK);
258 EGLDisplay dpy = EGL_NO_DISPLAY;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700259 EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800260
261 { // Scope for the lock
262 Mutex::Autolock lock(mMutex);
263
Daniel Lamb2675792012-02-23 14:35:13 -0800264 if (format == 0) {
265 format = mDefaultBufferFormat;
266 }
267 // turn on usage bits the consumer requested
268 usage |= mConsumerUsageBits;
269
Daniel Lam6b091c52012-01-22 15:26:27 -0800270 int found = -1;
Daniel Lam6b091c52012-01-22 15:26:27 -0800271 int dequeuedCount = 0;
272 bool tryAgain = true;
273 while (tryAgain) {
274 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800275 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800276 return NO_INIT;
277 }
278
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700279 const int maxBufferCount = getMaxBufferCountLocked(async);
280 if (async && mOverrideMaxBufferCount) {
281 // FIXME: some drivers are manually setting the buffer-count (which they
282 // shouldn't), so we do this extra test here to handle that case.
283 // This is TEMPORARY, until we get this fixed.
284 if (mOverrideMaxBufferCount < maxBufferCount) {
285 ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
286 return BAD_VALUE;
287 }
288 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800289
Jamie Gennise191e6c2012-08-24 20:26:34 -0700290 // Free up any buffers that are in slots beyond the max buffer
291 // count.
292 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
293 assert(mSlots[i].mBufferState == BufferSlot::FREE);
294 if (mSlots[i].mGraphicBuffer != NULL) {
295 freeBufferLocked(i);
Andy McFadden2adaf042012-12-18 09:49:45 -0800296 returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700297 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800298 }
299
300 // look for a free buffer to give to the client
301 found = INVALID_BUFFER_SLOT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800302 dequeuedCount = 0;
Jamie Gennise191e6c2012-08-24 20:26:34 -0700303 for (int i = 0; i < maxBufferCount; i++) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800304 const int state = mSlots[i].mBufferState;
305 if (state == BufferSlot::DEQUEUED) {
306 dequeuedCount++;
307 }
308
Jamie Gennis3fd12e42012-10-02 16:40:30 -0700309 if (state == BufferSlot::FREE) {
310 /* We return the oldest of the free buffers to avoid
311 * stalling the producer if possible. This is because
312 * the consumer may still have pending reads of the
313 * buffers in flight.
314 */
Mathias Agopian73d56622012-11-19 16:50:24 -0800315 if ((found < 0) ||
316 mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
317 found = i;
Daniel Lam6b091c52012-01-22 15:26:27 -0800318 }
319 }
320 }
321
322 // clients are not allowed to dequeue more than one buffer
323 // if they didn't set a buffer count.
Jamie Gennis31a353d2012-08-24 17:25:13 -0700324 if (!mOverrideMaxBufferCount && dequeuedCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800325 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
326 "setting the buffer count");
327 return -EINVAL;
328 }
329
330 // See whether a buffer has been queued since the last
Jamie Gennis72f096f2012-08-27 18:48:37 -0700331 // setBufferCount so we know whether to perform the min undequeued
332 // buffers check below.
Daniel Lameae59d22012-01-22 15:26:27 -0800333 if (mBufferHasBeenQueued) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800334 // make sure the client is not trying to dequeue more buffers
335 // than allowed.
Jamie Gennis72f096f2012-08-27 18:48:37 -0700336 const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700337 const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
Jamie Gennis72f096f2012-08-27 18:48:37 -0700338 if (newUndequeuedCount < minUndequeuedCount) {
339 ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
340 "exceeded (dequeued=%d undequeudCount=%d)",
341 minUndequeuedCount, dequeuedCount,
342 newUndequeuedCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800343 return -EBUSY;
344 }
345 }
346
Jamie Gennise191e6c2012-08-24 20:26:34 -0700347 // If no buffer is found, wait for a buffer to be released or for
348 // the max buffer count to change.
Daniel Lamc2c1f2f2012-03-07 14:11:29 -0800349 tryAgain = found == INVALID_BUFFER_SLOT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800350 if (tryAgain) {
Mathias Agopian595264f2013-07-16 22:56:09 -0700351 if (mDequeueBufferCannotBlock) {
352 ST_LOGE("dequeueBuffer: would block! returning an error instead.");
353 return WOULD_BLOCK;
354 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800355 mDequeueCondition.wait(mMutex);
356 }
357 }
358
Daniel Lam6b091c52012-01-22 15:26:27 -0800359
360 if (found == INVALID_BUFFER_SLOT) {
361 // This should not happen.
362 ST_LOGE("dequeueBuffer: no available buffer slots");
363 return -EBUSY;
364 }
365
366 const int buf = found;
367 *outBuf = found;
368
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800369 ATRACE_BUFFER_INDEX(buf);
370
Daniel Lam6b091c52012-01-22 15:26:27 -0800371 const bool useDefaultSize = !w && !h;
372 if (useDefaultSize) {
373 // use the default size
374 w = mDefaultWidth;
375 h = mDefaultHeight;
376 }
377
Daniel Lam6b091c52012-01-22 15:26:27 -0800378 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
379
380 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
381 if ((buffer == NULL) ||
382 (uint32_t(buffer->width) != w) ||
383 (uint32_t(buffer->height) != h) ||
384 (uint32_t(buffer->format) != format) ||
385 ((uint32_t(buffer->usage) & usage) != usage))
386 {
Daniel Lameae59d22012-01-22 15:26:27 -0800387 mSlots[buf].mAcquireCalled = false;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700388 mSlots[buf].mGraphicBuffer = NULL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800389 mSlots[buf].mRequestBufferCalled = false;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700390 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800391 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800392 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
393
Andy McFadden2adaf042012-12-18 09:49:45 -0800394 returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
Daniel Lam6b091c52012-01-22 15:26:27 -0800395 }
396
397 dpy = mSlots[buf].mEglDisplay;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700398 eglFence = mSlots[buf].mEglFence;
Jesse Hall4c00cc12013-03-15 21:34:30 -0700399 *outFence = mSlots[buf].mFence;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700400 mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800401 mSlots[buf].mFence = Fence::NO_FENCE;
Daniel Lameae59d22012-01-22 15:26:27 -0800402 } // end lock scope
Daniel Lam6b091c52012-01-22 15:26:27 -0800403
Andy McFadden2adaf042012-12-18 09:49:45 -0800404 if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
Jamie Gennis1efe0992012-10-04 18:34:01 -0700405 status_t error;
406 sp<GraphicBuffer> graphicBuffer(
407 mGraphicBufferAlloc->createGraphicBuffer(
408 w, h, format, usage, &error));
409 if (graphicBuffer == 0) {
410 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
411 "failed");
412 return error;
413 }
414
415 { // Scope for the lock
416 Mutex::Autolock lock(mMutex);
417
418 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800419 ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
Jamie Gennis1efe0992012-10-04 18:34:01 -0700420 return NO_INIT;
421 }
422
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700423 mSlots[*outBuf].mFrameNumber = ~0;
Jamie Gennis1efe0992012-10-04 18:34:01 -0700424 mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
425 }
426 }
427
Jesse Hallc777b0b2012-06-28 12:52:05 -0700428 if (eglFence != EGL_NO_SYNC_KHR) {
429 EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
Daniel Lam6b091c52012-01-22 15:26:27 -0800430 // If something goes wrong, log the error, but return the buffer without
431 // synchronizing access to it. It's too late at this point to abort the
432 // dequeue operation.
433 if (result == EGL_FALSE) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700434 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
Daniel Lam6b091c52012-01-22 15:26:27 -0800435 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700436 ST_LOGE("dequeueBuffer: timeout waiting for fence");
Daniel Lam6b091c52012-01-22 15:26:27 -0800437 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700438 eglDestroySyncKHR(dpy, eglFence);
Daniel Lam6b091c52012-01-22 15:26:27 -0800439 }
440
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700441 ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
442 mSlots[*outBuf].mFrameNumber,
Daniel Lam6b091c52012-01-22 15:26:27 -0800443 mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
444
445 return returnFlags;
446}
447
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700448status_t BufferQueue::queueBuffer(int buf,
449 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800450 ATRACE_CALL();
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800451 ATRACE_BUFFER_INDEX(buf);
452
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700453 Rect crop;
454 uint32_t transform;
455 int scalingMode;
456 int64_t timestamp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700457 bool async;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700458 sp<Fence> fence;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700459
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700460 input.deflate(&timestamp, &crop, &scalingMode, &transform, &async, &fence);
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700461
Jamie Gennis1df8c342012-12-20 14:05:45 -0800462 if (fence == NULL) {
463 ST_LOGE("queueBuffer: fence is NULL");
464 return BAD_VALUE;
465 }
466
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700467 switch (scalingMode) {
468 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
469 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
470 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
471 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
472 break;
473 default:
474 ST_LOGE("unknown scaling mode: %d", scalingMode);
475 return -EINVAL;
476 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800477
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700478 sp<ConsumerListener> listener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800479
480 { // scope for the lock
481 Mutex::Autolock lock(mMutex);
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700482
Daniel Lam6b091c52012-01-22 15:26:27 -0800483 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800484 ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
Daniel Lam6b091c52012-01-22 15:26:27 -0800485 return NO_INIT;
486 }
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700487
488 const int maxBufferCount = getMaxBufferCountLocked(async);
489 if (async && mOverrideMaxBufferCount) {
490 // FIXME: some drivers are manually setting the buffer-count (which they
491 // shouldn't), so we do this extra test here to handle that case.
492 // This is TEMPORARY, until we get this fixed.
493 if (mOverrideMaxBufferCount < maxBufferCount) {
494 ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
495 return BAD_VALUE;
496 }
497 }
Jamie Gennise191e6c2012-08-24 20:26:34 -0700498 if (buf < 0 || buf >= maxBufferCount) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800499 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennise191e6c2012-08-24 20:26:34 -0700500 maxBufferCount, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800501 return -EINVAL;
502 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
503 ST_LOGE("queueBuffer: slot %d is not owned by the client "
504 "(state=%d)", buf, mSlots[buf].mBufferState);
505 return -EINVAL;
Daniel Lam6b091c52012-01-22 15:26:27 -0800506 } else if (!mSlots[buf].mRequestBufferCalled) {
507 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
508 "buffer", buf);
509 return -EINVAL;
510 }
511
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700512 ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
513 "tr=%#x scale=%s",
514 buf, mFrameCounter + 1, timestamp,
515 crop.left, crop.top, crop.right, crop.bottom,
516 transform, scalingModeName(scalingMode));
517
Jamie Gennisd72f2332012-05-07 13:50:11 -0700518 const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
519 Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
520 Rect croppedCrop;
521 crop.intersect(bufferRect, &croppedCrop);
522 if (croppedCrop != crop) {
523 ST_LOGE("queueBuffer: crop rect is not contained within the "
524 "buffer in slot %d", buf);
525 return -EINVAL;
526 }
527
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700528 mSlots[buf].mFence = fence;
529 mSlots[buf].mBufferState = BufferSlot::QUEUED;
530 mFrameCounter++;
531 mSlots[buf].mFrameNumber = mFrameCounter;
532
533 BufferItem item;
534 item.mAcquireCalled = mSlots[buf].mAcquireCalled;
535 item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
536 item.mCrop = crop;
537 item.mTransform = transform;
538 item.mScalingMode = scalingMode;
539 item.mTimestamp = timestamp;
540 item.mFrameNumber = mFrameCounter;
541 item.mBuf = buf;
542 item.mFence = fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700543 item.mIsDroppable = mDequeueBufferCannotBlock || async;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700544
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700545 if (mQueue.empty()) {
546 // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
547 // simply queue this buffer.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700548 mQueue.push_back(item);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700549 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800550 } else {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700551 // when the queue is not empty, we need to look at the front buffer
552 // state and see if we need to replace it.
553 Fifo::iterator front(mQueue.begin());
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700554 if (front->mIsDroppable) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700555 // buffer slot currently queued is marked free if still tracked
556 if (stillTracking(front)) {
557 mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
Mathias Agopian26a6f372013-07-18 22:25:55 -0700558 // reset the frame number of the freed buffer so that it is the first in
559 // line to be dequeued again.
560 mSlots[front->mBuf].mFrameNumber = 0;
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700561 }
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700562 // and we record the new buffer in the queued list
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700563 *front = item;
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700564 } else {
565 mQueue.push_back(item);
566 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800567 }
568 }
569
Daniel Lameae59d22012-01-22 15:26:27 -0800570 mBufferHasBeenQueued = true;
Dave Burke74ff8c22012-03-12 21:49:41 -0700571 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800572
Mathias Agopian2488b202012-04-20 17:19:28 -0700573 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
574 mQueue.size());
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800575
576 ATRACE_INT(mConsumerName.string(), mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800577 } // scope for the lock
578
579 // call back without lock held
580 if (listener != 0) {
581 listener->onFrameAvailable();
582 }
Andy McFadden753e3412013-04-04 17:09:03 -0700583 return NO_ERROR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800584}
585
Jesse Hall4c00cc12013-03-15 21:34:30 -0700586void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800587 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800588 ST_LOGV("cancelBuffer: slot=%d", buf);
589 Mutex::Autolock lock(mMutex);
590
591 if (mAbandoned) {
592 ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
593 return;
594 }
595
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700596 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800597 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700598 NUM_BUFFER_SLOTS, buf);
Daniel Lam6b091c52012-01-22 15:26:27 -0800599 return;
600 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
601 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
602 buf, mSlots[buf].mBufferState);
603 return;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800604 } else if (fence == NULL) {
605 ST_LOGE("cancelBuffer: fence is NULL");
606 return;
Daniel Lam6b091c52012-01-22 15:26:27 -0800607 }
608 mSlots[buf].mBufferState = BufferSlot::FREE;
609 mSlots[buf].mFrameNumber = 0;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700610 mSlots[buf].mFence = fence;
Dave Burke74ff8c22012-03-12 21:49:41 -0700611 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800612}
613
Mathias Agopian595264f2013-07-16 22:56:09 -0700614status_t BufferQueue::connect(int api, bool producerControlledByApp, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800615 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800616 ST_LOGV("connect: api=%d", api);
617 Mutex::Autolock lock(mMutex);
618
619 if (mAbandoned) {
620 ST_LOGE("connect: BufferQueue has been abandoned!");
621 return NO_INIT;
622 }
623
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700624 if (mConsumerListener == NULL) {
625 ST_LOGE("connect: BufferQueue has no consumer!");
626 return NO_INIT;
627 }
628
Daniel Lam6b091c52012-01-22 15:26:27 -0800629 int err = NO_ERROR;
630 switch (api) {
631 case NATIVE_WINDOW_API_EGL:
632 case NATIVE_WINDOW_API_CPU:
633 case NATIVE_WINDOW_API_MEDIA:
634 case NATIVE_WINDOW_API_CAMERA:
635 if (mConnectedApi != NO_CONNECTED_API) {
636 ST_LOGE("connect: already connected (cur=%d, req=%d)",
637 mConnectedApi, api);
638 err = -EINVAL;
639 } else {
640 mConnectedApi = api;
Mathias Agopian2488b202012-04-20 17:19:28 -0700641 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
642 mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800643 }
644 break;
645 default:
646 err = -EINVAL;
647 break;
648 }
Daniel Lameae59d22012-01-22 15:26:27 -0800649
650 mBufferHasBeenQueued = false;
Mathias Agopian595264f2013-07-16 22:56:09 -0700651 mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
Daniel Lameae59d22012-01-22 15:26:27 -0800652
Daniel Lam6b091c52012-01-22 15:26:27 -0800653 return err;
654}
655
656status_t BufferQueue::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800657 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800658 ST_LOGV("disconnect: api=%d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800659
660 int err = NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700661 sp<ConsumerListener> listener;
662
663 { // Scope for the lock
664 Mutex::Autolock lock(mMutex);
665
666 if (mAbandoned) {
667 // it is not really an error to disconnect after the surface
668 // has been abandoned, it should just be a no-op.
669 return NO_ERROR;
670 }
671
672 switch (api) {
673 case NATIVE_WINDOW_API_EGL:
674 case NATIVE_WINDOW_API_CPU:
675 case NATIVE_WINDOW_API_MEDIA:
676 case NATIVE_WINDOW_API_CAMERA:
677 if (mConnectedApi == api) {
Mathias Agopiana3fbda32013-07-18 15:55:03 -0700678 freeAllBuffersLocked();
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700679 mConnectedApi = NO_CONNECTED_API;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700680 mDequeueCondition.broadcast();
681 listener = mConsumerListener;
682 } else {
683 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
684 mConnectedApi, api);
685 err = -EINVAL;
686 }
687 break;
688 default:
689 ST_LOGE("disconnect: unknown API %d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800690 err = -EINVAL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700691 break;
692 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800693 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700694
695 if (listener != NULL) {
696 listener->onBuffersReleased();
697 }
698
Daniel Lam6b091c52012-01-22 15:26:27 -0800699 return err;
700}
701
Mathias Agopian74d211a2013-04-22 16:55:35 +0200702void BufferQueue::dump(String8& result) const {
703 BufferQueue::dump(result, "");
Daniel Lameae59d22012-01-22 15:26:27 -0800704}
705
Mathias Agopian74d211a2013-04-22 16:55:35 +0200706void BufferQueue::dump(String8& result, const char* prefix) const {
Daniel Lameae59d22012-01-22 15:26:27 -0800707 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800708
709 String8 fifo;
710 int fifoSize = 0;
711 Fifo::const_iterator i(mQueue.begin());
712 while (i != mQueue.end()) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700713 fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
714 "xform=0x%02x, time=%#llx, scale=%s\n",
715 i->mBuf, i->mGraphicBuffer.get(),
716 i->mCrop.left, i->mCrop.top, i->mCrop.right,
717 i->mCrop.bottom, i->mTransform, i->mTimestamp,
718 scalingModeName(i->mScalingMode)
719 );
720 i++;
Mathias Agopian74d211a2013-04-22 16:55:35 +0200721 fifoSize++;
Daniel Lameae59d22012-01-22 15:26:27 -0800722 }
723
Jamie Gennise191e6c2012-08-24 20:26:34 -0700724
Mathias Agopian74d211a2013-04-22 16:55:35 +0200725 result.appendFormat(
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700726 "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
Andy McFadden69052052012-09-14 16:10:11 -0700727 "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700728 prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
Andy McFadden69052052012-09-14 16:10:11 -0700729 mDefaultHeight, mDefaultBufferFormat, mTransformHint,
730 fifoSize, fifo.string());
Daniel Lameae59d22012-01-22 15:26:27 -0800731
732 struct {
733 const char * operator()(int state) const {
734 switch (state) {
735 case BufferSlot::DEQUEUED: return "DEQUEUED";
736 case BufferSlot::QUEUED: return "QUEUED";
737 case BufferSlot::FREE: return "FREE";
738 case BufferSlot::ACQUIRED: return "ACQUIRED";
739 default: return "Unknown";
740 }
741 }
742 } stateName;
743
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700744 // just trim the free buffers to not spam the dump
745 int maxBufferCount = 0;
746 for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
747 const BufferSlot& slot(mSlots[i]);
748 if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
749 maxBufferCount = i+1;
750 break;
751 }
752 }
753
Jamie Gennise191e6c2012-08-24 20:26:34 -0700754 for (int i=0 ; i<maxBufferCount ; i++) {
Daniel Lameae59d22012-01-22 15:26:27 -0800755 const BufferSlot& slot(mSlots[i]);
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700756 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200757 result.appendFormat(
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700758 "%s%s[%02d:%p] state=%-8s",
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700759 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700760 stateName(slot.mBufferState)
Daniel Lameae59d22012-01-22 15:26:27 -0800761 );
Daniel Lameae59d22012-01-22 15:26:27 -0800762
Daniel Lameae59d22012-01-22 15:26:27 -0800763 if (buf != NULL) {
Mathias Agopian74d211a2013-04-22 16:55:35 +0200764 result.appendFormat(
Daniel Lameae59d22012-01-22 15:26:27 -0800765 ", %p [%4ux%4u:%4u,%3X]",
766 buf->handle, buf->width, buf->height, buf->stride,
767 buf->format);
Daniel Lameae59d22012-01-22 15:26:27 -0800768 }
769 result.append("\n");
770 }
771}
772
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700773void BufferQueue::freeBufferLocked(int slot) {
774 ST_LOGV("freeBufferLocked: slot=%d", slot);
775 mSlots[slot].mGraphicBuffer = 0;
776 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
777 mSlots[slot].mNeedsCleanupOnRelease = true;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700778 }
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700779 mSlots[slot].mBufferState = BufferSlot::FREE;
780 mSlots[slot].mFrameNumber = 0;
781 mSlots[slot].mAcquireCalled = false;
Daniel Lameae59d22012-01-22 15:26:27 -0800782
783 // destroy fence as BufferQueue now takes ownership
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700784 if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
785 eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
786 mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
Daniel Lam6b091c52012-01-22 15:26:27 -0800787 }
Jamie Gennis1df8c342012-12-20 14:05:45 -0800788 mSlots[slot].mFence = Fence::NO_FENCE;
Daniel Lam6b091c52012-01-22 15:26:27 -0800789}
790
791void BufferQueue::freeAllBuffersLocked() {
Daniel Lameae59d22012-01-22 15:26:27 -0800792 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800793 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
794 freeBufferLocked(i);
795 }
796}
797
Andy McFadden1585c4d2013-06-28 13:52:40 -0700798status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t presentWhen) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800799 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -0800800 Mutex::Autolock _l(mMutex);
Jamie Gennis5e5efde2012-08-28 17:18:50 -0700801
802 // Check that the consumer doesn't currently have the maximum number of
803 // buffers acquired. We allow the max buffer count to be exceeded by one
804 // buffer, so that the consumer can successfully set up the newly acquired
805 // buffer before releasing the old one.
806 int numAcquiredBuffers = 0;
807 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
808 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
809 numAcquiredBuffers++;
810 }
811 }
812 if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
813 ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
814 numAcquiredBuffers, mMaxAcquiredBufferCount);
815 return INVALID_OPERATION;
816 }
817
Daniel Lameae59d22012-01-22 15:26:27 -0800818 // check if queue is empty
819 // In asynchronous mode the list is guaranteed to be one buffer
820 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700821 if (mQueue.empty()) {
Daniel Lamfbcda932012-04-09 22:51:52 -0700822 return NO_BUFFER_AVAILABLE;
Daniel Lameae59d22012-01-22 15:26:27 -0800823 }
824
Andy McFadden1585c4d2013-06-28 13:52:40 -0700825 Fifo::iterator front(mQueue.begin());
826 int buf = front->mBuf;
827
828 // Compare the buffer's desired presentation time to the predicted
829 // actual display time.
830 //
831 // The "presentWhen" argument indicates when the buffer is expected
832 // to be presented on-screen. If the buffer's desired-present time
833 // is earlier (less) than presentWhen, meaning it'll be displayed
834 // on time or possibly late, we acquire and return it. If we don't want
835 // to display it until after the presentWhen time, we return PRESENT_LATER
836 // without acquiring it.
837 //
838 // To be safe, we don't refuse to acquire the buffer if presentWhen is
839 // more than one second in the future beyond the desired present time
840 // (i.e. we'd be holding the buffer for a really long time).
841 const int MAX_FUTURE_NSEC = 1000000000ULL;
842 nsecs_t desiredPresent = front->mTimestamp;
843 if (presentWhen != 0 && desiredPresent > presentWhen &&
844 desiredPresent - presentWhen < MAX_FUTURE_NSEC)
845 {
846 ALOGV("pts defer: des=%lld when=%lld (%lld) now=%lld",
847 desiredPresent, presentWhen, desiredPresent - presentWhen,
848 systemTime(CLOCK_MONOTONIC));
849 return PRESENT_LATER;
850 }
851 if (presentWhen != 0) {
852 ALOGV("pts accept: %p[%d] sig=%lld des=%lld when=%lld (%lld)",
853 mSlots, buf, mSlots[buf].mFence->getSignalTime(),
854 desiredPresent, presentWhen, desiredPresent - presentWhen);
855 }
856
857 *buffer = *front;
858 ATRACE_BUFFER_INDEX(buf);
859
860 ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
861 front->mBuf, front->mFrameNumber,
862 front->mGraphicBuffer->handle);
863 // if front buffer still being tracked update slot state
864 if (stillTracking(front)) {
865 mSlots[buf].mAcquireCalled = true;
866 mSlots[buf].mNeedsCleanupOnRelease = false;
867 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
868 mSlots[buf].mFence = Fence::NO_FENCE;
869 }
870
871 // If the buffer has previously been acquired by the consumer, set
872 // mGraphicBuffer to NULL to avoid unnecessarily remapping this
873 // buffer on the consumer side.
874 if (buffer->mAcquireCalled) {
875 buffer->mGraphicBuffer = NULL;
876 }
877
878 mQueue.erase(front);
879 mDequeueCondition.broadcast();
880
881 ATRACE_INT(mConsumerName.string(), mQueue.size());
882
Andy McFadden753e3412013-04-04 17:09:03 -0700883 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800884}
885
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700886status_t BufferQueue::releaseBuffer(
887 int buf, uint64_t frameNumber, EGLDisplay display,
Jesse Hallc777b0b2012-06-28 12:52:05 -0700888 EGLSyncKHR eglFence, const sp<Fence>& fence) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800889 ATRACE_CALL();
890 ATRACE_BUFFER_INDEX(buf);
891
Daniel Lameae59d22012-01-22 15:26:27 -0800892 Mutex::Autolock _l(mMutex);
893
Jamie Gennis1df8c342012-12-20 14:05:45 -0800894 if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
895 return BAD_VALUE;
Daniel Lameae59d22012-01-22 15:26:27 -0800896 }
897
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700898 // Check if this buffer slot is on the queue
899 bool slotQueued = false;
900 Fifo::iterator front(mQueue.begin());
901 while (front != mQueue.end() && !slotQueued) {
902 if (front->mBuf == buf)
903 slotQueued = true;
904 front++;
905 }
906
907 // If the frame number has changed because buffer has been reallocated,
908 // we can ignore this releaseBuffer for the old buffer.
909 if (frameNumber != mSlots[buf].mFrameNumber) {
910 // This should only occur if new buffer is still in the queue
911 ALOGE_IF(!slotQueued,
912 "received old buffer(#%lld) after new buffer(#%lld) on same "
913 "slot #%d already acquired", frameNumber,
914 mSlots[buf].mFrameNumber, buf);
915 return STALE_BUFFER_SLOT;
916 }
917 // this should never happen
918 ALOGE_IF(slotQueued,
919 "received new buffer(#%lld) on slot #%d that has not yet been "
920 "acquired", frameNumber, buf);
Daniel Lameae59d22012-01-22 15:26:27 -0800921
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700922 // The buffer can now only be released if its in the acquired state
923 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700924 mSlots[buf].mEglDisplay = display;
925 mSlots[buf].mEglFence = eglFence;
926 mSlots[buf].mFence = fence;
Daniel Lameae59d22012-01-22 15:26:27 -0800927 mSlots[buf].mBufferState = BufferSlot::FREE;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700928 } else if (mSlots[buf].mNeedsCleanupOnRelease) {
929 ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
930 mSlots[buf].mNeedsCleanupOnRelease = false;
931 return STALE_BUFFER_SLOT;
932 } else {
933 ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
934 return -EINVAL;
Daniel Lameae59d22012-01-22 15:26:27 -0800935 }
Daniel Lameae59d22012-01-22 15:26:27 -0800936
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700937 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -0700938 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800939}
940
Mathias Agopian595264f2013-07-16 22:56:09 -0700941status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener,
942 bool controlledByApp) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700943 ST_LOGV("consumerConnect");
Daniel Lameae59d22012-01-22 15:26:27 -0800944 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800945
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700946 if (mAbandoned) {
947 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
948 return NO_INIT;
949 }
Andy McFadden753e3412013-04-04 17:09:03 -0700950 if (consumerListener == NULL) {
951 ST_LOGE("consumerConnect: consumerListener may not be NULL");
952 return BAD_VALUE;
953 }
Daniel Lamb2675792012-02-23 14:35:13 -0800954
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700955 mConsumerListener = consumerListener;
Mathias Agopian595264f2013-07-16 22:56:09 -0700956 mConsumerControlledByApp = controlledByApp;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700957
Andy McFadden753e3412013-04-04 17:09:03 -0700958 return NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700959}
960
961status_t BufferQueue::consumerDisconnect() {
962 ST_LOGV("consumerDisconnect");
963 Mutex::Autolock lock(mMutex);
964
965 if (mConsumerListener == NULL) {
966 ST_LOGE("consumerDisconnect: No consumer is connected!");
967 return -EINVAL;
968 }
969
970 mAbandoned = true;
971 mConsumerListener = NULL;
Daniel Lamb2675792012-02-23 14:35:13 -0800972 mQueue.clear();
Daniel Lameae59d22012-01-22 15:26:27 -0800973 freeAllBuffersLocked();
Dave Burke74ff8c22012-03-12 21:49:41 -0700974 mDequeueCondition.broadcast();
Andy McFadden753e3412013-04-04 17:09:03 -0700975 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -0800976}
977
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700978status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
979 ST_LOGV("getReleasedBuffers");
980 Mutex::Autolock lock(mMutex);
981
982 if (mAbandoned) {
983 ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
984 return NO_INIT;
985 }
986
987 uint32_t mask = 0;
988 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
989 if (!mSlots[i].mAcquireCalled) {
990 mask |= 1 << i;
991 }
992 }
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700993
994 // Remove buffers in flight (on the queue) from the mask where acquire has
995 // been called, as the consumer will not receive the buffer address, so
996 // it should not free these slots.
997 Fifo::iterator front(mQueue.begin());
998 while (front != mQueue.end()) {
999 if (front->mAcquireCalled)
1000 mask &= ~(1 << front->mBuf);
1001 front++;
1002 }
1003
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001004 *slotMask = mask;
1005
1006 ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
1007 return NO_ERROR;
1008}
1009
Daniel Lameae59d22012-01-22 15:26:27 -08001010status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
1011{
1012 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
1013 if (!w || !h) {
1014 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
1015 w, h);
1016 return BAD_VALUE;
1017 }
1018
1019 Mutex::Autolock lock(mMutex);
1020 mDefaultWidth = w;
1021 mDefaultHeight = h;
Andy McFadden753e3412013-04-04 17:09:03 -07001022 return NO_ERROR;
Daniel Lameae59d22012-01-22 15:26:27 -08001023}
1024
Jamie Gennis31a353d2012-08-24 17:25:13 -07001025status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001026 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -08001027 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -07001028 return setDefaultMaxBufferCountLocked(bufferCount);
Daniel Lameae59d22012-01-22 15:26:27 -08001029}
1030
Jamie Gennis72f096f2012-08-27 18:48:37 -07001031status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1032 ATRACE_CALL();
1033 Mutex::Autolock lock(mMutex);
Jamie Gennisc68f2ec2012-08-30 18:36:22 -07001034 if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
1035 ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
1036 maxAcquiredBuffers);
1037 return BAD_VALUE;
1038 }
Jamie Gennis72f096f2012-08-27 18:48:37 -07001039 if (mConnectedApi != NO_CONNECTED_API) {
1040 return INVALID_OPERATION;
1041 }
1042 mMaxAcquiredBufferCount = maxAcquiredBuffers;
Andy McFadden753e3412013-04-04 17:09:03 -07001043 return NO_ERROR;
Jamie Gennis72f096f2012-08-27 18:48:37 -07001044}
1045
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001046int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
1047 return (mDequeueBufferCannotBlock || async) ?
1048 mMaxAcquiredBufferCount+1 : mMaxAcquiredBufferCount;
Jamie Gennis31a353d2012-08-24 17:25:13 -07001049}
1050
Mathias Agopian7cdd7862013-07-18 22:10:56 -07001051int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
1052 return getMinUndequeuedBufferCount(async) + 1;
1053}
1054
1055int BufferQueue::getMaxBufferCountLocked(bool async) const {
1056 int minMaxBufferCount = getMinMaxBufferCountLocked(async);
Jamie Gennise191e6c2012-08-24 20:26:34 -07001057
1058 int maxBufferCount = mDefaultMaxBufferCount;
1059 if (maxBufferCount < minMaxBufferCount) {
1060 maxBufferCount = minMaxBufferCount;
1061 }
1062 if (mOverrideMaxBufferCount != 0) {
1063 assert(mOverrideMaxBufferCount >= minMaxBufferCount);
1064 maxBufferCount = mOverrideMaxBufferCount;
1065 }
1066
1067 // Any buffers that are dequeued by the producer or sitting in the queue
1068 // waiting to be consumed need to have their slots preserved. Such
1069 // buffers will temporarily keep the max buffer count up until the slots
1070 // no longer need to be preserved.
1071 for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
1072 BufferSlot::BufferState state = mSlots[i].mBufferState;
1073 if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
1074 maxBufferCount = i + 1;
1075 }
1076 }
1077
1078 return maxBufferCount;
1079}
1080
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -07001081bool BufferQueue::stillTracking(const BufferItem *item) const {
1082 const BufferSlot &slot = mSlots[item->mBuf];
1083
1084 ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
1085 "slot: { slot=%d/%llu, buffer=%p }",
1086 item->mBuf, item->mFrameNumber,
1087 (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
1088 item->mBuf, slot.mFrameNumber,
1089 (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
1090
1091 // Compare item with its original buffer slot. We can check the slot
1092 // as the buffer would not be moved to a different slot by the producer.
1093 return (slot.mGraphicBuffer != NULL &&
1094 item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
1095}
1096
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001097BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1098 const wp<BufferQueue::ConsumerListener>& consumerListener):
1099 mConsumerListener(consumerListener) {}
1100
1101BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1102
1103void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1104 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1105 if (listener != NULL) {
1106 listener->onFrameAvailable();
1107 }
1108}
1109
1110void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1111 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1112 if (listener != NULL) {
1113 listener->onBuffersReleased();
1114 }
1115}
1116
Daniel Lam6b091c52012-01-22 15:26:27 -08001117}; // namespace android