• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 #include <inttypes.h>
18 
19 #define LOG_TAG "BufferQueueProducer"
20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21 //#define LOG_NDEBUG 0
22 
23 #if DEBUG_ONLY_CODE
24 #define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0)
25 #else
26 #define VALIDATE_CONSISTENCY()
27 #endif
28 
29 #define EGL_EGLEXT_PROTOTYPES
30 
31 #include <binder/IPCThreadState.h>
32 #include <gui/BufferItem.h>
33 #include <gui/BufferQueueCore.h>
34 #include <gui/BufferQueueProducer.h>
35 
36 #include <gui/FrameRateUtils.h>
37 #include <gui/GLConsumer.h>
38 #include <gui/IConsumerListener.h>
39 #include <gui/IProducerListener.h>
40 #include <gui/TraceUtils.h>
41 #include <private/gui/BufferQueueThreadState.h>
42 
43 #include <utils/Errors.h>
44 #include <utils/Log.h>
45 #include <utils/Trace.h>
46 
47 #include <system/window.h>
48 
49 #include <com_android_graphics_libgui_flags.h>
50 
51 namespace android {
52 using namespace com::android::graphics::libgui;
53 
54 // Macros for include BufferQueueCore information in log messages
55 #define BQ_LOGV(x, ...)                                                                           \
56     ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(),             \
57           mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
58           ##__VA_ARGS__)
59 #define BQ_LOGD(x, ...)                                                                           \
60     ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(),             \
61           mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
62           ##__VA_ARGS__)
63 #define BQ_LOGI(x, ...)                                                                           \
64     ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(),             \
65           mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
66           ##__VA_ARGS__)
67 #define BQ_LOGW(x, ...)                                                                           \
68     ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(),             \
69           mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
70           ##__VA_ARGS__)
71 #define BQ_LOGE(x, ...)                                                                           \
72     ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(),             \
73           mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \
74           ##__VA_ARGS__)
75 
76 static constexpr uint32_t BQ_LAYER_COUNT = 1;
77 ProducerListener::~ProducerListener() = default;
78 
BufferQueueProducer(const sp<BufferQueueCore> & core,bool consumerIsSurfaceFlinger)79 BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
80         bool consumerIsSurfaceFlinger) :
81     mCore(core),
82     mSlots(core->mSlots),
83     mConsumerName(),
84     mStickyTransform(0),
85     mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger),
86     mLastQueueBufferFence(Fence::NO_FENCE),
87     mLastQueuedTransform(0),
88     mCallbackMutex(),
89     mNextCallbackTicket(0),
90     mCurrentCallbackTicket(0),
91     mCallbackCondition(),
92     mDequeueTimeout(-1),
93     mDequeueWaitingForAllocation(false) {}
94 
~BufferQueueProducer()95 BufferQueueProducer::~BufferQueueProducer() {}
96 
requestBuffer(int slot,sp<GraphicBuffer> * buf)97 status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
98     ATRACE_CALL();
99     BQ_LOGV("requestBuffer: slot %d", slot);
100     std::lock_guard<std::mutex> lock(mCore->mMutex);
101 
102     if (mCore->mIsAbandoned) {
103         BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
104         return NO_INIT;
105     }
106 
107     if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
108         BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
109         return NO_INIT;
110     }
111 
112     int maxSlot = mCore->getTotalSlotCountLocked();
113     if (slot < 0 || slot >= maxSlot) {
114         BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)", slot, maxSlot);
115         return BAD_VALUE;
116     } else if (!mSlots[slot].mBufferState.isDequeued()) {
117         BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
118                 "(state = %s)", slot, mSlots[slot].mBufferState.string());
119         return BAD_VALUE;
120     }
121 
122     mSlots[slot].mRequestBufferCalled = true;
123     *buf = mSlots[slot].mGraphicBuffer;
124     return NO_ERROR;
125 }
126 
127 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
extendSlotCount(int size)128 status_t BufferQueueProducer::extendSlotCount(int size) {
129     ATRACE_CALL();
130 
131     sp<IConsumerListener> listener;
132     {
133         std::lock_guard<std::mutex> lock(mCore->mMutex);
134         BQ_LOGV("extendSlotCount: size %d", size);
135 
136         if (mCore->mIsAbandoned) {
137             BQ_LOGE("extendSlotCount: BufferQueue has been abandoned");
138             return NO_INIT;
139         }
140 
141         if (!mCore->mAllowExtendedSlotCount) {
142             BQ_LOGE("extendSlotCount: Consumer did not allow unlimited slots");
143             return INVALID_OPERATION;
144         }
145 
146         int maxBeforeExtension = mCore->mMaxBufferCount;
147 
148         if (size == maxBeforeExtension) {
149             return NO_ERROR;
150         }
151 
152         if (size < maxBeforeExtension) {
153             return BAD_VALUE;
154         }
155 
156         if (status_t ret = mCore->extendSlotCountLocked(size); ret != OK) {
157             return ret;
158         }
159         listener = mCore->mConsumerListener;
160     }
161 
162     if (listener) {
163         listener->onSlotCountChanged(size);
164     }
165 
166     return NO_ERROR;
167 }
168 #endif
169 
setMaxDequeuedBufferCount(int maxDequeuedBuffers)170 status_t BufferQueueProducer::setMaxDequeuedBufferCount(
171         int maxDequeuedBuffers) {
172     int maxBufferCount;
173     return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount);
174 }
175 
setMaxDequeuedBufferCount(int maxDequeuedBuffers,int * maxBufferCount)176 status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers,
177                                                         int* maxBufferCount) {
178     ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers);
179     BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d",
180             maxDequeuedBuffers);
181 
182     sp<IConsumerListener> listener;
183     { // Autolock scope
184         std::unique_lock<std::mutex> lock(mCore->mMutex);
185         mCore->waitWhileAllocatingLocked(lock);
186 
187         if (mCore->mIsAbandoned) {
188             BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been "
189                     "abandoned");
190             return NO_INIT;
191         }
192 
193         *maxBufferCount = mCore->getMaxBufferCountLocked();
194 
195         if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) {
196             return NO_ERROR;
197         }
198 
199         // The new maxDequeuedBuffer count should not be violated by the number
200         // of currently dequeued buffers
201         int dequeuedCount = 0;
202         for (int s : mCore->mActiveBuffers) {
203             if (mSlots[s].mBufferState.isDequeued()) {
204                 dequeuedCount++;
205             }
206         }
207         if (dequeuedCount > maxDequeuedBuffers) {
208             BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer"
209                     "count (%d) exceeds the current dequeued buffer count (%d)",
210                     maxDequeuedBuffers, dequeuedCount);
211             return BAD_VALUE;
212         }
213 
214         int bufferCount = mCore->getMinUndequeuedBufferCountLocked();
215         bufferCount += maxDequeuedBuffers;
216 
217         if (bufferCount > mCore->getTotalSlotCountLocked()) {
218             BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large "
219                     "(max %d)",
220                     bufferCount, mCore->getTotalSlotCountLocked());
221             return BAD_VALUE;
222         }
223 
224         const int minBufferSlots = mCore->getMinMaxBufferCountLocked();
225         if (bufferCount < minBufferSlots) {
226             BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is "
227                     "less than minimum %d", bufferCount, minBufferSlots);
228             return BAD_VALUE;
229         }
230 
231         if (bufferCount > mCore->mMaxBufferCount) {
232             BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would "
233                     "exceed the maxBufferCount (%d) (maxAcquired %d async %d "
234                     "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers,
235                     mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
236                     mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock);
237             return BAD_VALUE;
238         }
239 
240         int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount;
241         if (!mCore->adjustAvailableSlotsLocked(delta)) {
242             return BAD_VALUE;
243         }
244         mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers;
245         *maxBufferCount = mCore->getMaxBufferCountLocked();
246         VALIDATE_CONSISTENCY();
247         if (delta < 0) {
248             listener = mCore->mConsumerListener;
249         }
250 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
251         mCore->notifyBufferReleased();
252 #else
253         mCore->mDequeueCondition.notify_all();
254 #endif
255     } // Autolock scope
256 
257     // Call back without lock held
258     if (listener != nullptr) {
259         listener->onBuffersReleased();
260     }
261 
262     return NO_ERROR;
263 }
264 
setAsyncMode(bool async)265 status_t BufferQueueProducer::setAsyncMode(bool async) {
266     ATRACE_CALL();
267     BQ_LOGV("setAsyncMode: async = %d", async);
268 
269     sp<IConsumerListener> listener;
270     { // Autolock scope
271         std::unique_lock<std::mutex> lock(mCore->mMutex);
272         mCore->waitWhileAllocatingLocked(lock);
273 
274         if (mCore->mIsAbandoned) {
275             BQ_LOGE("setAsyncMode: BufferQueue has been abandoned");
276             return NO_INIT;
277         }
278 
279         if (async == mCore->mAsyncMode) {
280             return NO_ERROR;
281         }
282 
283         if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount +
284                 (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) >
285                 mCore->mMaxBufferCount) {
286             BQ_LOGE("setAsyncMode(%d): this call would cause the "
287                     "maxBufferCount (%d) to be exceeded (maxAcquired %d "
288                     "maxDequeued %d mDequeueBufferCannotBlock %d)", async,
289                     mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount,
290                     mCore->mMaxDequeuedBufferCount,
291                     mCore->mDequeueBufferCannotBlock);
292             return BAD_VALUE;
293         }
294 
295         int delta = mCore->getMaxBufferCountLocked(async,
296                 mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount)
297                 - mCore->getMaxBufferCountLocked();
298 
299         if (!mCore->adjustAvailableSlotsLocked(delta)) {
300             BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of "
301                     "available slots. Delta = %d", delta);
302             return BAD_VALUE;
303         }
304         mCore->mAsyncMode = async;
305         VALIDATE_CONSISTENCY();
306 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
307         mCore->notifyBufferReleased();
308 #else
309         mCore->mDequeueCondition.notify_all();
310 #endif
311 
312         if (delta < 0) {
313             listener = mCore->mConsumerListener;
314         }
315     } // Autolock scope
316 
317     // Call back without lock held
318     if (listener != nullptr) {
319         listener->onBuffersReleased();
320     }
321     return NO_ERROR;
322 }
323 
getFreeBufferLocked() const324 int BufferQueueProducer::getFreeBufferLocked() const {
325     if (mCore->mFreeBuffers.empty()) {
326         return BufferQueueCore::INVALID_BUFFER_SLOT;
327     }
328     int slot = mCore->mFreeBuffers.front();
329     mCore->mFreeBuffers.pop_front();
330     return slot;
331 }
332 
getFreeSlotLocked() const333 int BufferQueueProducer::getFreeSlotLocked() const {
334     if (mCore->mFreeSlots.empty()) {
335         return BufferQueueCore::INVALID_BUFFER_SLOT;
336     }
337     int slot = *(mCore->mFreeSlots.begin());
338     mCore->mFreeSlots.erase(slot);
339     return slot;
340 }
341 
waitForFreeSlotThenRelock(FreeSlotCaller caller,std::unique_lock<std::mutex> & lock,int * found) const342 status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller,
343         std::unique_lock<std::mutex>& lock, int* found) const {
344     auto callerString = (caller == FreeSlotCaller::Dequeue) ?
345             "dequeueBuffer" : "attachBuffer";
346     bool tryAgain = true;
347     while (tryAgain) {
348         if (mCore->mIsAbandoned) {
349             BQ_LOGE("%s: BufferQueue has been abandoned", callerString);
350             return NO_INIT;
351         }
352 
353         int dequeuedCount = 0;
354         int acquiredCount = 0;
355         for (int s : mCore->mActiveBuffers) {
356             if (mSlots[s].mBufferState.isDequeued()) {
357                 ++dequeuedCount;
358             }
359             if (mSlots[s].mBufferState.isAcquired()) {
360                 ++acquiredCount;
361             }
362         }
363 
364         // Producers are not allowed to dequeue more than
365         // mMaxDequeuedBufferCount buffers.
366         // This check is only done if a buffer has already been queued
367         using namespace com::android::graphics::libgui::flags;
368         bool flagGatedBufferHasBeenQueued =
369                 bq_always_use_max_dequeued_buffer_count() || mCore->mBufferHasBeenQueued;
370         if (flagGatedBufferHasBeenQueued && dequeuedCount >= mCore->mMaxDequeuedBufferCount) {
371             // Supress error logs when timeout is non-negative.
372             if (mDequeueTimeout < 0) {
373                 BQ_LOGE("%s: attempting to exceed the max dequeued buffer "
374                         "count (%d)", callerString,
375                         mCore->mMaxDequeuedBufferCount);
376             }
377             return INVALID_OPERATION;
378         }
379 
380         *found = BufferQueueCore::INVALID_BUFFER_SLOT;
381 
382         // If we disconnect and reconnect quickly, we can be in a state where
383         // our slots are empty but we have many buffers in the queue. This can
384         // cause us to run out of memory if we outrun the consumer. Wait here if
385         // it looks like we have too many buffers queued up.
386         const int maxBufferCount = mCore->getMaxBufferCountLocked();
387         bool tooManyBuffers = mCore->mQueue.size()
388                             > static_cast<size_t>(maxBufferCount);
389         if (tooManyBuffers) {
390             BQ_LOGV("%s: queue size is %zu, waiting", callerString,
391                     mCore->mQueue.size());
392         } else {
393             // If in shared buffer mode and a shared buffer exists, always
394             // return it.
395             if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot !=
396                     BufferQueueCore::INVALID_BUFFER_SLOT) {
397                 *found = mCore->mSharedBufferSlot;
398             } else {
399                 if (caller == FreeSlotCaller::Dequeue) {
400                     // If we're calling this from dequeue, prefer free buffers
401                     int slot = getFreeBufferLocked();
402                     if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
403                         *found = slot;
404                     } else if (mCore->mAllowAllocation) {
405                         *found = getFreeSlotLocked();
406                     }
407                 } else {
408                     // If we're calling this from attach, prefer free slots
409                     int slot = getFreeSlotLocked();
410                     if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) {
411                         *found = slot;
412                     } else {
413                         *found = getFreeBufferLocked();
414                     }
415                 }
416             }
417         }
418 
419         // If no buffer is found, or if the queue has too many buffers
420         // outstanding, wait for a buffer to be acquired or released, or for the
421         // max buffer count to change.
422         tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) ||
423                    tooManyBuffers;
424         if (tryAgain) {
425             // Return an error if we're in non-blocking mode (producer and
426             // consumer are controlled by the application).
427             // However, the consumer is allowed to briefly acquire an extra
428             // buffer (which could cause us to have to wait here), which is
429             // okay, since it is only used to implement an atomic acquire +
430             // release (e.g., in GLConsumer::updateTexImage())
431             if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) &&
432                     (acquiredCount <= mCore->mMaxAcquiredBufferCount)) {
433                 return WOULD_BLOCK;
434             }
435 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
436             if (status_t status = waitForBufferRelease(lock, mDequeueTimeout);
437                 status == TIMED_OUT) {
438                 return TIMED_OUT;
439             }
440 #else
441             if (mDequeueTimeout >= 0) {
442                 std::cv_status result = mCore->mDequeueCondition.wait_for(lock,
443                         std::chrono::nanoseconds(mDequeueTimeout));
444                 if (result == std::cv_status::timeout) {
445                     return TIMED_OUT;
446                 }
447             } else {
448                 mCore->mDequeueCondition.wait(lock);
449             }
450 #endif
451         }
452     } // while (tryAgain)
453 
454     return NO_ERROR;
455 }
456 
457 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
waitForBufferRelease(std::unique_lock<std::mutex> & lock,nsecs_t timeout) const458 status_t BufferQueueProducer::waitForBufferRelease(std::unique_lock<std::mutex>& lock,
459                                                    nsecs_t timeout) const {
460     if (mDequeueTimeout >= 0) {
461         std::cv_status result =
462                 mCore->mDequeueCondition.wait_for(lock, std::chrono::nanoseconds(timeout));
463         if (result == std::cv_status::timeout) {
464             return TIMED_OUT;
465         }
466     } else {
467         mCore->mDequeueCondition.wait(lock);
468     }
469     return OK;
470 }
471 #endif
472 
dequeueBuffer(int * outSlot,sp<android::Fence> * outFence,uint32_t width,uint32_t height,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)473 status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence,
474                                             uint32_t width, uint32_t height, PixelFormat format,
475                                             uint64_t usage, uint64_t* outBufferAge,
476                                             FrameEventHistoryDelta* outTimestamps) {
477     ATRACE_CALL();
478     { // Autolock scope
479         std::lock_guard<std::mutex> lock(mCore->mMutex);
480         mConsumerName = mCore->mConsumerName;
481 
482         if (mCore->mIsAbandoned) {
483             BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
484             return NO_INIT;
485         }
486 
487         if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
488             BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer");
489             return NO_INIT;
490         }
491     } // Autolock scope
492 
493     BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage);
494 
495     if ((width && !height) || (!width && height)) {
496         BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height);
497         return BAD_VALUE;
498     }
499 
500     status_t returnFlags = NO_ERROR;
501 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
502     EGLDisplay eglDisplay = EGL_NO_DISPLAY;
503     EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
504 #endif
505     bool attachedByConsumer = false;
506 
507     sp<IConsumerListener> listener;
508     bool callOnFrameDequeued = false;
509     uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true
510 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
511     std::vector<gui::AdditionalOptions> allocOptions;
512     uint32_t allocOptionsGenId = 0;
513 #endif
514 
515     { // Autolock scope
516         std::unique_lock<std::mutex> lock(mCore->mMutex);
517 
518         // If we don't have a free buffer, but we are currently allocating, we wait until allocation
519         // is finished such that we don't allocate in parallel.
520         if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) {
521             mDequeueWaitingForAllocation = true;
522             mCore->waitWhileAllocatingLocked(lock);
523             mDequeueWaitingForAllocation = false;
524             mDequeueWaitingForAllocationCondition.notify_all();
525         }
526 
527         if (format == 0) {
528             format = mCore->mDefaultBufferFormat;
529         }
530 
531         // Enable the usage bits the consumer requested
532         usage |= mCore->mConsumerUsageBits;
533 
534         const bool useDefaultSize = !width && !height;
535         if (useDefaultSize) {
536             width = mCore->mDefaultWidth;
537             height = mCore->mDefaultHeight;
538             if (mCore->mAutoPrerotation &&
539                 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
540                 std::swap(width, height);
541             }
542         }
543 
544         int found = BufferItem::INVALID_BUFFER_SLOT;
545         while (found == BufferItem::INVALID_BUFFER_SLOT) {
546             status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found);
547             if (status != NO_ERROR) {
548                 return status;
549             }
550 
551             // This should not happen
552             if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
553                 BQ_LOGE("dequeueBuffer: no available buffer slots");
554                 return -EBUSY;
555             }
556 
557             const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
558 
559             // If we are not allowed to allocate new buffers,
560             // waitForFreeSlotThenRelock must have returned a slot containing a
561             // buffer. If this buffer would require reallocation to meet the
562             // requested attributes, we free it and attempt to get another one.
563             if (!mCore->mAllowAllocation) {
564                 if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) {
565                     if (mCore->mSharedBufferSlot == found) {
566                         BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer");
567                         return BAD_VALUE;
568                     }
569                     mCore->mFreeSlots.insert(found);
570                     mCore->clearBufferSlotLocked(found);
571                     found = BufferItem::INVALID_BUFFER_SLOT;
572                     continue;
573                 }
574             }
575         }
576 
577         const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
578 
579         bool needsReallocation = buffer == nullptr ||
580                 buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage);
581 
582 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
583         needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId !=
584                 mCore->mAdditionalOptionsGenerationId;
585 #endif
586 
587         if (mCore->mSharedBufferSlot == found && needsReallocation) {
588             BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer");
589             return BAD_VALUE;
590         }
591 
592         if (mCore->mSharedBufferSlot != found) {
593             mCore->mActiveBuffers.insert(found);
594         }
595         *outSlot = found;
596         ATRACE_BUFFER_INDEX(found);
597 
598         attachedByConsumer = mSlots[found].mNeedsReallocation;
599         mSlots[found].mNeedsReallocation = false;
600 
601         mSlots[found].mBufferState.dequeue();
602 
603         if (needsReallocation) {
604             if (CC_UNLIKELY(ATRACE_ENABLED())) {
605                 if (buffer == nullptr) {
606                     ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str());
607                 } else {
608                     ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d "
609                                           "layerCount:%d "
610                                           "usage:%d requested: %dx%d format:%d layerCount:%d "
611                                           "usage:%d ",
612                                           mConsumerName.c_str(), width, height, format,
613                                           BQ_LAYER_COUNT, usage, buffer->getWidth(),
614                                           buffer->getHeight(), buffer->getPixelFormat(),
615                                           buffer->getLayerCount(), buffer->getUsage());
616                 }
617             }
618             mSlots[found].mAcquireCalled = false;
619             mSlots[found].mGraphicBuffer = nullptr;
620             mSlots[found].mRequestBufferCalled = false;
621 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
622             mSlots[found].mEglDisplay = EGL_NO_DISPLAY;
623             mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
624 #endif
625             mSlots[found].mFence = Fence::NO_FENCE;
626             mCore->mBufferAge = 0;
627             mCore->mIsAllocating = true;
628 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
629             allocOptions = mCore->mAdditionalOptions;
630             allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
631 #endif
632 
633             returnFlags |= BUFFER_NEEDS_REALLOCATION;
634         } else {
635             // We add 1 because that will be the frame number when this buffer
636             // is queued
637             mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber;
638         }
639 
640         BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64,
641                 mCore->mBufferAge);
642 
643         if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) {
644             BQ_LOGE("dequeueBuffer: about to return a NULL fence - "
645                     "slot=%d w=%d h=%d format=%u",
646                     found, buffer->width, buffer->height, buffer->format);
647         }
648 
649 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
650         eglDisplay = mSlots[found].mEglDisplay;
651         eglFence = mSlots[found].mEglFence;
652 #endif
653         // Don't return a fence in shared buffer mode, except for the first
654         // frame.
655         *outFence = (mCore->mSharedBufferMode &&
656                 mCore->mSharedBufferSlot == found) ?
657                 Fence::NO_FENCE : mSlots[found].mFence;
658 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
659         mSlots[found].mEglFence = EGL_NO_SYNC_KHR;
660 #endif
661         mSlots[found].mFence = Fence::NO_FENCE;
662 
663         // If shared buffer mode has just been enabled, cache the slot of the
664         // first buffer that is dequeued and mark it as the shared buffer.
665         if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
666                 BufferQueueCore::INVALID_BUFFER_SLOT) {
667             mCore->mSharedBufferSlot = found;
668             mSlots[found].mBufferState.mShared = true;
669         }
670 
671         if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) {
672             callOnFrameDequeued = true;
673             bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
674         }
675 
676         listener = mCore->mConsumerListener;
677     } // Autolock scope
678 
679     if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
680         BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot);
681 
682 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
683         std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
684         tempOptions.reserve(allocOptions.size());
685         for (const auto& it : allocOptions) {
686             tempOptions.emplace_back(it.name.c_str(), it.value);
687         }
688         const GraphicBufferAllocator::AllocationRequest allocRequest = {
689                 .importBuffer = true,
690                 .width = width,
691                 .height = height,
692                 .format = format,
693                 .layerCount = BQ_LAYER_COUNT,
694                 .usage = usage,
695                 .requestorName = {mConsumerName.c_str(), mConsumerName.size()},
696                 .extras = std::move(tempOptions),
697         };
698         sp<GraphicBuffer> graphicBuffer = sp<GraphicBuffer>::make(allocRequest);
699 #else
700         sp<GraphicBuffer> graphicBuffer =
701                 sp<GraphicBuffer>::make(width, height, format, BQ_LAYER_COUNT, usage,
702                                         std::string{mConsumerName.c_str(), mConsumerName.size()});
703 #endif
704 
705         status_t error = graphicBuffer->initCheck();
706 
707         { // Autolock scope
708             std::lock_guard<std::mutex> lock(mCore->mMutex);
709 
710             if (error == NO_ERROR && !mCore->mIsAbandoned) {
711                 graphicBuffer->setGenerationNumber(mCore->mGenerationNumber);
712                 mSlots[*outSlot].mGraphicBuffer = graphicBuffer;
713 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
714                 mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId;
715 #endif
716                 callOnFrameDequeued = true;
717                 bufferId = mSlots[*outSlot].mGraphicBuffer->getId();
718             }
719 
720             mCore->mIsAllocating = false;
721             mCore->mIsAllocatingCondition.notify_all();
722 
723             if (error != NO_ERROR) {
724                 mCore->mFreeSlots.insert(*outSlot);
725                 mCore->clearBufferSlotLocked(*outSlot);
726                 BQ_LOGE("dequeueBuffer: createGraphicBuffer failed");
727                 return error;
728             }
729 
730             if (mCore->mIsAbandoned) {
731                 mCore->mFreeSlots.insert(*outSlot);
732                 mCore->clearBufferSlotLocked(*outSlot);
733                 BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned");
734                 return NO_INIT;
735             }
736 
737             VALIDATE_CONSISTENCY();
738         } // Autolock scope
739     }
740 
741     if (listener != nullptr && callOnFrameDequeued) {
742         listener->onFrameDequeued(bufferId);
743     }
744 
745     if (attachedByConsumer) {
746         returnFlags |= BUFFER_NEEDS_REALLOCATION;
747     }
748 
749 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
750     if (eglFence != EGL_NO_SYNC_KHR) {
751         EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0,
752                 1000000000);
753         // If something goes wrong, log the error, but return the buffer without
754         // synchronizing access to it. It's too late at this point to abort the
755         // dequeue operation.
756         if (result == EGL_FALSE) {
757             BQ_LOGE("dequeueBuffer: error %#x waiting for fence",
758                     eglGetError());
759         } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
760             BQ_LOGE("dequeueBuffer: timeout waiting for fence");
761         }
762         eglDestroySyncKHR(eglDisplay, eglFence);
763     }
764 #endif
765 
766     BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x",
767             *outSlot,
768             mSlots[*outSlot].mFrameNumber,
769             mSlots[*outSlot].mGraphicBuffer != nullptr ?
770             mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags);
771 
772     if (outBufferAge) {
773         *outBufferAge = mCore->mBufferAge;
774     }
775     addAndGetFrameTimestamps(nullptr, outTimestamps);
776 
777     return returnFlags;
778 }
779 
detachBuffer(int slot)780 status_t BufferQueueProducer::detachBuffer(int slot) {
781     ATRACE_CALL();
782     ATRACE_BUFFER_INDEX(slot);
783     BQ_LOGV("detachBuffer: slot %d", slot);
784 
785     sp<IConsumerListener> listener;
786     bool callOnFrameDetached = false;
787     uint64_t bufferId = 0; // Only used if callOnFrameDetached is true
788     {
789         std::lock_guard<std::mutex> lock(mCore->mMutex);
790 
791         if (mCore->mIsAbandoned) {
792             BQ_LOGE("detachBuffer: BufferQueue has been abandoned");
793             return NO_INIT;
794         }
795 
796         if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
797             BQ_LOGE("detachBuffer: BufferQueue has no connected producer");
798             return NO_INIT;
799         }
800 
801         if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) {
802             BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode");
803             return BAD_VALUE;
804         }
805 
806         const int totalSlotCount = mCore->getTotalSlotCountLocked();
807         if (slot < 0 || slot >= totalSlotCount) {
808             BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
809             return BAD_VALUE;
810         } else if (!mSlots[slot].mBufferState.isDequeued()) {
811             // TODO(http://b/140581935): This message is BQ_LOGW because it
812             // often logs when no actionable errors are present. Return to
813             // using BQ_LOGE after ensuring this only logs during errors.
814             BQ_LOGW("detachBuffer: slot %d is not owned by the producer "
815                     "(state = %s)", slot, mSlots[slot].mBufferState.string());
816             return BAD_VALUE;
817         } else if (!mSlots[slot].mRequestBufferCalled) {
818             BQ_LOGE("detachBuffer: buffer in slot %d has not been requested",
819                     slot);
820             return BAD_VALUE;
821         }
822 
823         listener = mCore->mConsumerListener;
824         auto gb = mSlots[slot].mGraphicBuffer;
825         if (gb != nullptr) {
826             callOnFrameDetached = true;
827             bufferId = gb->getId();
828         }
829         mSlots[slot].mBufferState.detachProducer();
830         mCore->mActiveBuffers.erase(slot);
831         mCore->mFreeSlots.insert(slot);
832         mCore->clearBufferSlotLocked(slot);
833 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
834         mCore->notifyBufferReleased();
835 #else
836         mCore->mDequeueCondition.notify_all();
837 #endif
838         VALIDATE_CONSISTENCY();
839     }
840 
841     if (listener != nullptr && callOnFrameDetached) {
842         listener->onFrameDetached(bufferId);
843     }
844 
845     if (listener != nullptr) {
846         listener->onBuffersReleased();
847     }
848 
849     return NO_ERROR;
850 }
851 
detachNextBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)852 status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
853         sp<Fence>* outFence) {
854     ATRACE_CALL();
855 
856     if (outBuffer == nullptr) {
857         BQ_LOGE("detachNextBuffer: outBuffer must not be NULL");
858         return BAD_VALUE;
859     } else if (outFence == nullptr) {
860         BQ_LOGE("detachNextBuffer: outFence must not be NULL");
861         return BAD_VALUE;
862     }
863 
864     sp<IConsumerListener> listener;
865     {
866         std::unique_lock<std::mutex> lock(mCore->mMutex);
867 
868         if (mCore->mIsAbandoned) {
869             BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned");
870             return NO_INIT;
871         }
872 
873         if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
874             BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer");
875             return NO_INIT;
876         }
877 
878         if (mCore->mSharedBufferMode) {
879             BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer "
880                     "mode");
881             return BAD_VALUE;
882         }
883 
884         mCore->waitWhileAllocatingLocked(lock);
885 
886         if (mCore->mFreeBuffers.empty()) {
887             return NO_MEMORY;
888         }
889 
890         int found = mCore->mFreeBuffers.front();
891         mCore->mFreeBuffers.remove(found);
892         mCore->mFreeSlots.insert(found);
893 
894         BQ_LOGV("detachNextBuffer detached slot %d", found);
895 
896         *outBuffer = mSlots[found].mGraphicBuffer;
897         *outFence = mSlots[found].mFence;
898         mCore->clearBufferSlotLocked(found);
899         VALIDATE_CONSISTENCY();
900         listener = mCore->mConsumerListener;
901     }
902 
903     if (listener != nullptr) {
904         listener->onBuffersReleased();
905     }
906 
907     return NO_ERROR;
908 }
909 
attachBuffer(int * outSlot,const sp<android::GraphicBuffer> & buffer)910 status_t BufferQueueProducer::attachBuffer(int* outSlot,
911         const sp<android::GraphicBuffer>& buffer) {
912     ATRACE_CALL();
913 
914     if (outSlot == nullptr) {
915         BQ_LOGE("attachBuffer: outSlot must not be NULL");
916         return BAD_VALUE;
917     } else if (buffer == nullptr) {
918         BQ_LOGE("attachBuffer: cannot attach NULL buffer");
919         return BAD_VALUE;
920     }
921 
922     std::unique_lock<std::mutex> lock(mCore->mMutex);
923 
924     if (mCore->mIsAbandoned) {
925         BQ_LOGE("attachBuffer: BufferQueue has been abandoned");
926         return NO_INIT;
927     }
928 
929     if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
930         BQ_LOGE("attachBuffer: BufferQueue has no connected producer");
931         return NO_INIT;
932     }
933 
934     if (mCore->mSharedBufferMode) {
935         BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode");
936         return BAD_VALUE;
937     }
938 
939     if (buffer->getGenerationNumber() != mCore->mGenerationNumber) {
940         BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] "
941                 "[queue %u]", buffer->getGenerationNumber(),
942                 mCore->mGenerationNumber);
943         return BAD_VALUE;
944     }
945 
946     mCore->waitWhileAllocatingLocked(lock);
947 
948     status_t returnFlags = NO_ERROR;
949     int found;
950     status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found);
951     if (status != NO_ERROR) {
952         return status;
953     }
954 
955     // This should not happen
956     if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
957         BQ_LOGE("attachBuffer: no available buffer slots");
958         return -EBUSY;
959     }
960 
961     *outSlot = found;
962     ATRACE_BUFFER_INDEX(*outSlot);
963     BQ_LOGV("attachBuffer: returning slot %d flags=%#x",
964             *outSlot, returnFlags);
965 
966     mSlots[*outSlot].mGraphicBuffer = buffer;
967     mSlots[*outSlot].mBufferState.attachProducer();
968 #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP)
969     mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR;
970 #endif
971     mSlots[*outSlot].mFence = Fence::NO_FENCE;
972     mSlots[*outSlot].mRequestBufferCalled = true;
973     mSlots[*outSlot].mAcquireCalled = false;
974     mSlots[*outSlot].mNeedsReallocation = false;
975     mCore->mActiveBuffers.insert(found);
976     VALIDATE_CONSISTENCY();
977 
978     return returnFlags;
979 }
980 
queueBuffer(int slot,const QueueBufferInput & input,QueueBufferOutput * output)981 status_t BufferQueueProducer::queueBuffer(int slot,
982         const QueueBufferInput &input, QueueBufferOutput *output) {
983     ATRACE_CALL();
984     ATRACE_BUFFER_INDEX(slot);
985 
986     int64_t requestedPresentTimestamp;
987     bool isAutoTimestamp;
988     android_dataspace dataSpace;
989     Rect crop(Rect::EMPTY_RECT);
990     int scalingMode;
991     uint32_t transform;
992     uint32_t stickyTransform;
993     sp<Fence> acquireFence;
994     bool getFrameTimestamps = false;
995     input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace,
996             &crop, &scalingMode, &transform, &acquireFence, &stickyTransform,
997             &getFrameTimestamps);
998     const Region& surfaceDamage = input.getSurfaceDamage();
999     const HdrMetadata& hdrMetadata = input.getHdrMetadata();
1000     const std::optional<PictureProfileHandle>& pictureProfileHandle =
1001             input.getPictureProfileHandle();
1002 
1003     if (acquireFence == nullptr) {
1004         BQ_LOGE("queueBuffer: fence is NULL");
1005         return BAD_VALUE;
1006     }
1007 
1008     auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence);
1009 
1010     switch (scalingMode) {
1011         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1012         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1013         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1014         case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1015             break;
1016         default:
1017             BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode);
1018             return BAD_VALUE;
1019     }
1020 
1021     sp<IConsumerListener> frameAvailableListener;
1022     sp<IConsumerListener> frameReplacedListener;
1023     int callbackTicket = 0;
1024     uint64_t currentFrameNumber = 0;
1025     BufferItem item;
1026     int connectedApi;
1027     bool enableEglCpuThrottling = true;
1028     sp<Fence> lastQueuedFence;
1029 
1030     { // Autolock scope
1031         std::lock_guard<std::mutex> lock(mCore->mMutex);
1032 
1033         if (mCore->mIsAbandoned) {
1034             BQ_LOGE("queueBuffer: BufferQueue has been abandoned");
1035             return NO_INIT;
1036         }
1037 
1038         if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1039             BQ_LOGE("queueBuffer: BufferQueue has no connected producer");
1040             return NO_INIT;
1041         }
1042 
1043         const int totalSlotCount = mCore->getTotalSlotCountLocked();
1044         if (slot < 0 || slot >= totalSlotCount) {
1045             BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
1046             return BAD_VALUE;
1047         } else if (!mSlots[slot].mBufferState.isDequeued()) {
1048             BQ_LOGE("queueBuffer: slot %d is not owned by the producer "
1049                     "(state = %s)", slot, mSlots[slot].mBufferState.string());
1050             return BAD_VALUE;
1051         } else if (!mSlots[slot].mRequestBufferCalled) {
1052             BQ_LOGE("queueBuffer: slot %d was queued without requesting "
1053                     "a buffer", slot);
1054             return BAD_VALUE;
1055         }
1056 
1057         // If shared buffer mode has just been enabled, cache the slot of the
1058         // first buffer that is queued and mark it as the shared buffer.
1059         if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot ==
1060                 BufferQueueCore::INVALID_BUFFER_SLOT) {
1061             mCore->mSharedBufferSlot = slot;
1062             mSlots[slot].mBufferState.mShared = true;
1063         }
1064 
1065         BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d"
1066                 " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s",
1067                 slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace,
1068                 hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom,
1069                 transform,
1070                 BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode)));
1071 
1072         const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer);
1073         Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
1074         Rect croppedRect(Rect::EMPTY_RECT);
1075         crop.intersect(bufferRect, &croppedRect);
1076         if (croppedRect != crop) {
1077             BQ_LOGE("queueBuffer: crop rect is not contained within the "
1078                     "buffer in slot %d", slot);
1079             return BAD_VALUE;
1080         }
1081 
1082         // Override UNKNOWN dataspace with consumer default
1083         if (dataSpace == HAL_DATASPACE_UNKNOWN) {
1084             dataSpace = mCore->mDefaultBufferDataSpace;
1085         }
1086 
1087         mSlots[slot].mFence = acquireFence;
1088         mSlots[slot].mBufferState.queue();
1089 
1090         // Increment the frame counter and store a local version of it
1091         // for use outside the lock on mCore->mMutex.
1092         ++mCore->mFrameCounter;
1093         currentFrameNumber = mCore->mFrameCounter;
1094         mSlots[slot].mFrameNumber = currentFrameNumber;
1095 
1096         item.mAcquireCalled = mSlots[slot].mAcquireCalled;
1097         item.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
1098         item.mCrop = crop;
1099         item.mTransform = transform &
1100                 ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
1101         item.mTransformToDisplayInverse =
1102                 (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0;
1103         item.mScalingMode = static_cast<uint32_t>(scalingMode);
1104         item.mTimestamp = requestedPresentTimestamp;
1105         item.mIsAutoTimestamp = isAutoTimestamp;
1106         item.mDataSpace = dataSpace;
1107         item.mHdrMetadata = hdrMetadata;
1108         item.mPictureProfileHandle = pictureProfileHandle;
1109         item.mFrameNumber = currentFrameNumber;
1110         item.mSlot = slot;
1111         item.mFence = acquireFence;
1112         item.mFenceTime = acquireFenceTime;
1113         item.mIsDroppable = mCore->mAsyncMode ||
1114                 (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) ||
1115                 (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) ||
1116                 (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot);
1117         item.mSurfaceDamage = surfaceDamage;
1118         item.mQueuedBuffer = true;
1119         item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh;
1120         item.mApi = mCore->mConnectedApi;
1121 
1122         mStickyTransform = stickyTransform;
1123 
1124         // Cache the shared buffer data so that the BufferItem can be recreated.
1125         if (mCore->mSharedBufferMode) {
1126             mCore->mSharedBufferCache.crop = crop;
1127             mCore->mSharedBufferCache.transform = transform;
1128             mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>(
1129                     scalingMode);
1130             mCore->mSharedBufferCache.dataspace = dataSpace;
1131         }
1132 
1133         output->bufferReplaced = false;
1134         if (mCore->mQueue.empty()) {
1135             // When the queue is empty, we can ignore mDequeueBufferCannotBlock
1136             // and simply queue this buffer
1137             mCore->mQueue.push_back(item);
1138             frameAvailableListener = mCore->mConsumerListener;
1139         } else {
1140             // When the queue is not empty, we need to look at the last buffer
1141             // in the queue to see if we need to replace it
1142             const BufferItem& last = mCore->mQueue.itemAt(
1143                     mCore->mQueue.size() - 1);
1144             if (last.mIsDroppable) {
1145 
1146                 if (!last.mIsStale) {
1147                     mSlots[last.mSlot].mBufferState.freeQueued();
1148 
1149                     // After leaving shared buffer mode, the shared buffer will
1150                     // still be around. Mark it as no longer shared if this
1151                     // operation causes it to be free.
1152                     if (!mCore->mSharedBufferMode &&
1153                             mSlots[last.mSlot].mBufferState.isFree()) {
1154                         mSlots[last.mSlot].mBufferState.mShared = false;
1155                     }
1156                     // Don't put the shared buffer on the free list.
1157                     if (!mSlots[last.mSlot].mBufferState.isShared()) {
1158                         mCore->mActiveBuffers.erase(last.mSlot);
1159                         mCore->mFreeBuffers.push_back(last.mSlot);
1160                         output->bufferReplaced = true;
1161                     }
1162                 }
1163 
1164                 // Make sure to merge the damage rect from the frame we're about
1165                 // to drop into the new frame's damage rect.
1166                 if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT ||
1167                     item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) {
1168                     item.mSurfaceDamage = Region::INVALID_REGION;
1169                 } else {
1170                     item.mSurfaceDamage |= last.mSurfaceDamage;
1171                 }
1172 
1173                 // Overwrite the droppable buffer with the incoming one
1174                 mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item;
1175                 frameReplacedListener = mCore->mConsumerListener;
1176             } else {
1177                 mCore->mQueue.push_back(item);
1178                 frameAvailableListener = mCore->mConsumerListener;
1179             }
1180         }
1181 
1182         mCore->mBufferHasBeenQueued = true;
1183 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1184         mCore->notifyBufferReleased();
1185 #else
1186         mCore->mDequeueCondition.notify_all();
1187 #endif
1188         mCore->mLastQueuedSlot = slot;
1189 
1190         output->width = mCore->mDefaultWidth;
1191         output->height = mCore->mDefaultHeight;
1192         output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
1193         output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size());
1194         output->nextFrameNumber = mCore->mFrameCounter + 1;
1195 
1196         ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size()));
1197 #ifndef NO_BINDER
1198         mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size());
1199 #endif
1200         // Take a ticket for the callback functions
1201         callbackTicket = mNextCallbackTicket++;
1202 
1203         VALIDATE_CONSISTENCY();
1204 
1205         connectedApi = mCore->mConnectedApi;
1206         if (flags::bq_producer_throttles_only_async_mode()) {
1207             enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock;
1208         }
1209         lastQueuedFence = std::move(mLastQueueBufferFence);
1210 
1211         mLastQueueBufferFence = std::move(acquireFence);
1212         mLastQueuedCrop = item.mCrop;
1213         mLastQueuedTransform = item.mTransform;
1214     } // Autolock scope
1215 
1216     // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because
1217     // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and
1218     // there will be no Binder call
1219     if (!mConsumerIsSurfaceFlinger) {
1220         item.mGraphicBuffer.clear();
1221     }
1222 
1223     // Update and get FrameEventHistory.
1224     nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC);
1225     NewFrameEventsEntry newFrameEventsEntry = {
1226         currentFrameNumber,
1227         postedTime,
1228         requestedPresentTimestamp,
1229         std::move(acquireFenceTime)
1230     };
1231     addAndGetFrameTimestamps(&newFrameEventsEntry,
1232             getFrameTimestamps ? &output->frameTimestamps : nullptr);
1233 
1234     // Call back without the main BufferQueue lock held, but with the callback
1235     // lock held so we can ensure that callbacks occur in order
1236 
1237     { // scope for the lock
1238         std::unique_lock<std::mutex> lock(mCallbackMutex);
1239         while (callbackTicket != mCurrentCallbackTicket) {
1240             mCallbackCondition.wait(lock);
1241         }
1242 
1243         if (frameAvailableListener != nullptr) {
1244             frameAvailableListener->onFrameAvailable(item);
1245         } else if (frameReplacedListener != nullptr) {
1246             frameReplacedListener->onFrameReplaced(item);
1247         }
1248 
1249         ++mCurrentCallbackTicket;
1250         mCallbackCondition.notify_all();
1251     }
1252 
1253     // Wait without lock held
1254     if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) {
1255         // Waiting here allows for two full buffers to be queued but not a
1256         // third. In the event that frames take varying time, this makes a
1257         // small trade-off in favor of latency rather than throughput.
1258         lastQueuedFence->waitForever("Throttling EGL Production");
1259     }
1260 
1261     return NO_ERROR;
1262 }
1263 
cancelBuffer(int slot,const sp<Fence> & fence)1264 status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
1265     ATRACE_CALL();
1266     BQ_LOGV("cancelBuffer: slot %d", slot);
1267 
1268     sp<IConsumerListener> listener;
1269     bool callOnFrameCancelled = false;
1270     uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true
1271     {
1272         std::lock_guard<std::mutex> lock(mCore->mMutex);
1273 
1274         if (mCore->mIsAbandoned) {
1275             BQ_LOGE("cancelBuffer: BufferQueue has been abandoned");
1276             return NO_INIT;
1277         }
1278 
1279         if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1280             BQ_LOGE("cancelBuffer: BufferQueue has no connected producer");
1281             return NO_INIT;
1282         }
1283 
1284         if (mCore->mSharedBufferMode) {
1285             BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode");
1286             return BAD_VALUE;
1287         }
1288 
1289         const int totalSlotCount = mCore->getTotalSlotCountLocked();
1290         if (slot < 0 || slot >= totalSlotCount) {
1291             BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot, totalSlotCount);
1292             return BAD_VALUE;
1293         } else if (!mSlots[slot].mBufferState.isDequeued()) {
1294             BQ_LOGE("cancelBuffer: slot %d is not owned by the producer "
1295                     "(state = %s)",
1296                     slot, mSlots[slot].mBufferState.string());
1297             return BAD_VALUE;
1298         } else if (fence == nullptr) {
1299             BQ_LOGE("cancelBuffer: fence is NULL");
1300             return BAD_VALUE;
1301         }
1302 
1303         mSlots[slot].mBufferState.cancel();
1304 
1305         // After leaving shared buffer mode, the shared buffer will still be around.
1306         // Mark it as no longer shared if this operation causes it to be free.
1307         if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) {
1308             mSlots[slot].mBufferState.mShared = false;
1309         }
1310 
1311         // Don't put the shared buffer on the free list.
1312         if (!mSlots[slot].mBufferState.isShared()) {
1313             mCore->mActiveBuffers.erase(slot);
1314             mCore->mFreeBuffers.push_back(slot);
1315         }
1316 
1317         auto gb = mSlots[slot].mGraphicBuffer;
1318         if (gb != nullptr) {
1319             callOnFrameCancelled = true;
1320             bufferId = gb->getId();
1321         }
1322         mSlots[slot].mFence = fence;
1323 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1324         mCore->notifyBufferReleased();
1325 #else
1326         mCore->mDequeueCondition.notify_all();
1327 #endif
1328         listener = mCore->mConsumerListener;
1329         VALIDATE_CONSISTENCY();
1330     }
1331 
1332     if (listener != nullptr && callOnFrameCancelled) {
1333         listener->onFrameCancelled(bufferId);
1334     }
1335 
1336     return NO_ERROR;
1337 }
1338 
query(int what,int * outValue)1339 int BufferQueueProducer::query(int what, int *outValue) {
1340     ATRACE_CALL();
1341     std::lock_guard<std::mutex> lock(mCore->mMutex);
1342 
1343     if (outValue == nullptr) {
1344         BQ_LOGE("query: outValue was NULL");
1345         return BAD_VALUE;
1346     }
1347 
1348     if (mCore->mIsAbandoned) {
1349         BQ_LOGE("query: BufferQueue has been abandoned");
1350         return NO_INIT;
1351     }
1352 
1353     int value;
1354     switch (what) {
1355         case NATIVE_WINDOW_WIDTH:
1356             value = static_cast<int32_t>(mCore->mDefaultWidth);
1357             break;
1358         case NATIVE_WINDOW_HEIGHT:
1359             value = static_cast<int32_t>(mCore->mDefaultHeight);
1360             break;
1361         case NATIVE_WINDOW_FORMAT:
1362             value = static_cast<int32_t>(mCore->mDefaultBufferFormat);
1363             break;
1364         case NATIVE_WINDOW_LAYER_COUNT:
1365             // All BufferQueue buffers have a single layer.
1366             value = BQ_LAYER_COUNT;
1367             break;
1368         case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1369             value = mCore->getMinUndequeuedBufferCountLocked();
1370             break;
1371         case NATIVE_WINDOW_STICKY_TRANSFORM:
1372             value = static_cast<int32_t>(mStickyTransform);
1373             break;
1374         case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
1375             value = (mCore->mQueue.size() > 1);
1376             break;
1377         case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
1378             // deprecated; higher 32 bits are truncated
1379             value = static_cast<int32_t>(mCore->mConsumerUsageBits);
1380             break;
1381         case NATIVE_WINDOW_DEFAULT_DATASPACE:
1382             value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace);
1383             break;
1384         case NATIVE_WINDOW_BUFFER_AGE:
1385             if (mCore->mBufferAge > INT32_MAX) {
1386                 value = 0;
1387             } else {
1388                 value = static_cast<int32_t>(mCore->mBufferAge);
1389             }
1390             break;
1391         case NATIVE_WINDOW_CONSUMER_IS_PROTECTED:
1392             value = static_cast<int32_t>(mCore->mConsumerIsProtected);
1393             break;
1394         default:
1395             return BAD_VALUE;
1396     }
1397 
1398     BQ_LOGV("query: %d? %d", what, value);
1399     *outValue = value;
1400     return NO_ERROR;
1401 }
1402 
connect(const sp<IProducerListener> & listener,int api,bool producerControlledByApp,QueueBufferOutput * output)1403 status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener,
1404         int api, bool producerControlledByApp, QueueBufferOutput *output) {
1405     ATRACE_CALL();
1406     std::lock_guard<std::mutex> lock(mCore->mMutex);
1407     mConsumerName = mCore->mConsumerName;
1408     BQ_LOGV("connect: api=%d producerControlledByApp=%s", api,
1409             producerControlledByApp ? "true" : "false");
1410 
1411     if (mCore->mIsAbandoned) {
1412         BQ_LOGE("connect: BufferQueue has been abandoned");
1413         return NO_INIT;
1414     }
1415 
1416     if (mCore->mConsumerListener == nullptr) {
1417         BQ_LOGE("connect: BufferQueue has no consumer");
1418         return NO_INIT;
1419     }
1420 
1421     if (output == nullptr) {
1422         BQ_LOGE("connect: output was NULL");
1423         return BAD_VALUE;
1424     }
1425 
1426     if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
1427         BQ_LOGE("connect: already connected (cur=%d req=%d)",
1428                 mCore->mConnectedApi, api);
1429         return BAD_VALUE;
1430     }
1431 
1432     int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode,
1433             mDequeueTimeout < 0 ?
1434             mCore->mConsumerControlledByApp && producerControlledByApp : false,
1435             mCore->mMaxBufferCount) -
1436             mCore->getMaxBufferCountLocked();
1437     if (!mCore->adjustAvailableSlotsLocked(delta)) {
1438         BQ_LOGE("connect: BufferQueue failed to adjust the number of available "
1439                 "slots. Delta = %d", delta);
1440         return BAD_VALUE;
1441     }
1442 
1443     int status = NO_ERROR;
1444     switch (api) {
1445         case NATIVE_WINDOW_API_EGL:
1446         case NATIVE_WINDOW_API_CPU:
1447         case NATIVE_WINDOW_API_MEDIA:
1448         case NATIVE_WINDOW_API_CAMERA:
1449             mCore->mConnectedApi = api;
1450 
1451             output->width = mCore->mDefaultWidth;
1452             output->height = mCore->mDefaultHeight;
1453             output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint;
1454             output->numPendingBuffers =
1455                     static_cast<uint32_t>(mCore->mQueue.size());
1456             output->nextFrameNumber = mCore->mFrameCounter + 1;
1457             output->bufferReplaced = false;
1458             output->maxBufferCount = mCore->mMaxBufferCount;
1459 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS)
1460             output->isSlotExpansionAllowed = mCore->mAllowExtendedSlotCount;
1461 #endif
1462 
1463             if (listener != nullptr) {
1464                 // Set up a death notification so that we can disconnect
1465                 // automatically if the remote producer dies
1466 #ifndef NO_BINDER
1467                 if (IInterface::asBinder(listener)->remoteBinder() != nullptr) {
1468                     status = IInterface::asBinder(listener)->linkToDeath(
1469                             sp<IBinder::DeathRecipient>::fromExisting(this));
1470                     if (status != NO_ERROR) {
1471                         BQ_LOGE("connect: linkToDeath failed: %s (%d)",
1472                                 strerror(-status), status);
1473                     }
1474                     mCore->mLinkedToDeath = listener;
1475                 }
1476 #endif
1477                 mCore->mConnectedProducerListener = listener;
1478                 mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify();
1479 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1480                 mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify();
1481 #endif
1482             }
1483             break;
1484         default:
1485             BQ_LOGE("connect: unknown API %d", api);
1486             status = BAD_VALUE;
1487             break;
1488     }
1489     mCore->mConnectedPid = BufferQueueThreadState::getCallingPid();
1490     mCore->mBufferHasBeenQueued = false;
1491     mCore->mDequeueBufferCannotBlock = false;
1492     mCore->mQueueBufferCanDrop = false;
1493     mCore->mLegacyBufferDrop = true;
1494     if (mCore->mConsumerControlledByApp && producerControlledByApp) {
1495         mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0;
1496         mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0;
1497     }
1498 
1499     mCore->mAllowAllocation = true;
1500 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1501     mCore->mAdditionalOptions.clear();
1502 #endif
1503     VALIDATE_CONSISTENCY();
1504     return status;
1505 }
1506 
disconnect(int api,DisconnectMode mode)1507 status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) {
1508     ATRACE_CALL();
1509     BQ_LOGV("disconnect: api %d", api);
1510 
1511     int status = NO_ERROR;
1512     sp<IConsumerListener> listener;
1513     { // Autolock scope
1514         std::unique_lock<std::mutex> lock(mCore->mMutex);
1515 
1516         if (mode == DisconnectMode::AllLocal) {
1517             if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) {
1518                 return NO_ERROR;
1519             }
1520             api = BufferQueueCore::CURRENTLY_CONNECTED_API;
1521         }
1522 
1523         mCore->waitWhileAllocatingLocked(lock);
1524 
1525         if (mCore->mIsAbandoned) {
1526             // It's not really an error to disconnect after the surface has
1527             // been abandoned; it should just be a no-op.
1528             return NO_ERROR;
1529         }
1530 
1531         if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) {
1532             if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) {
1533                 ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode);
1534             }
1535             api = mCore->mConnectedApi;
1536             // If we're asked to disconnect the currently connected api but
1537             // nobody is connected, it's not really an error.
1538             if (api == BufferQueueCore::NO_CONNECTED_API) {
1539                 return NO_ERROR;
1540             }
1541         }
1542 
1543         switch (api) {
1544             case NATIVE_WINDOW_API_EGL:
1545             case NATIVE_WINDOW_API_CPU:
1546             case NATIVE_WINDOW_API_MEDIA:
1547             case NATIVE_WINDOW_API_CAMERA:
1548                 if (mCore->mConnectedApi == api) {
1549                     mCore->freeAllBuffersLocked();
1550 
1551 #ifndef NO_BINDER
1552                     // Remove our death notification callback if we have one
1553                     if (mCore->mLinkedToDeath != nullptr) {
1554                         sp<IBinder> token =
1555                                 IInterface::asBinder(mCore->mLinkedToDeath);
1556                         // This can fail if we're here because of the death
1557                         // notification, but we just ignore it
1558                         token->unlinkToDeath(static_cast<IBinder::DeathRecipient*>(this));
1559                     }
1560 #endif
1561                     mCore->mSharedBufferSlot =
1562                             BufferQueueCore::INVALID_BUFFER_SLOT;
1563                     mCore->mLinkedToDeath = nullptr;
1564                     mCore->mConnectedProducerListener = nullptr;
1565                     mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API;
1566                     mCore->mConnectedPid = -1;
1567                     mCore->mSidebandStream.clear();
1568 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1569                     mCore->notifyBufferReleased();
1570 #else
1571                     mCore->mDequeueCondition.notify_all();
1572 #endif
1573                     mCore->mAutoPrerotation = false;
1574 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1575                     mCore->mAdditionalOptions.clear();
1576 #endif
1577                     listener = mCore->mConsumerListener;
1578                 } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1579                     BQ_LOGE("disconnect: not connected (req=%d)", api);
1580                     status = NO_INIT;
1581                 } else {
1582                     BQ_LOGE("disconnect: still connected to another API "
1583                             "(cur=%d req=%d)", mCore->mConnectedApi, api);
1584                     status = BAD_VALUE;
1585                 }
1586                 break;
1587             default:
1588                 BQ_LOGE("disconnect: unknown API %d", api);
1589                 status = BAD_VALUE;
1590                 break;
1591         }
1592     } // Autolock scope
1593 
1594     // Call back without lock held
1595     if (listener != nullptr) {
1596         listener->onBuffersReleased();
1597         listener->onDisconnect();
1598     }
1599 
1600     return status;
1601 }
1602 
setSidebandStream(const sp<NativeHandle> & stream)1603 status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) {
1604     sp<IConsumerListener> listener;
1605     { // Autolock scope
1606         std::lock_guard<std::mutex> _l(mCore->mMutex);
1607         mCore->mSidebandStream = stream;
1608         listener = mCore->mConsumerListener;
1609     } // Autolock scope
1610 
1611     if (listener != nullptr) {
1612         listener->onSidebandStreamChanged();
1613     }
1614     return NO_ERROR;
1615 }
1616 
allocateBuffers(uint32_t width,uint32_t height,PixelFormat format,uint64_t usage)1617 void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height,
1618         PixelFormat format, uint64_t usage) {
1619     ATRACE_CALL();
1620 
1621     const bool useDefaultSize = !width && !height;
1622     while (true) {
1623         uint32_t allocWidth = 0;
1624         uint32_t allocHeight = 0;
1625         PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN;
1626         uint64_t allocUsage = 0;
1627         std::string allocName;
1628 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1629         std::vector<gui::AdditionalOptions> allocOptions;
1630         uint32_t allocOptionsGenId = 0;
1631 #endif
1632         { // Autolock scope
1633             std::unique_lock<std::mutex> lock(mCore->mMutex);
1634             mCore->waitWhileAllocatingLocked(lock);
1635 
1636             if (!mCore->mAllowAllocation) {
1637                 BQ_LOGE("allocateBuffers: allocation is not allowed for this "
1638                         "BufferQueue");
1639                 return;
1640             }
1641 
1642             // Only allocate one buffer at a time to reduce risks of overlapping an allocation from
1643             // both allocateBuffers and dequeueBuffer.
1644             if (mCore->mFreeSlots.empty()) {
1645                 BQ_LOGV("allocateBuffers: a slot was occupied while "
1646                         "allocating. Dropping allocated buffer.");
1647                 return;
1648             }
1649 
1650             allocWidth = width > 0 ? width : mCore->mDefaultWidth;
1651             allocHeight = height > 0 ? height : mCore->mDefaultHeight;
1652             if (useDefaultSize && mCore->mAutoPrerotation &&
1653                 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1654                 std::swap(allocWidth, allocHeight);
1655             }
1656 
1657             allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat;
1658             allocUsage = usage | mCore->mConsumerUsageBits;
1659             allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size());
1660 
1661 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1662             allocOptions = mCore->mAdditionalOptions;
1663             allocOptionsGenId = mCore->mAdditionalOptionsGenerationId;
1664 #endif
1665 
1666             mCore->mIsAllocating = true;
1667 
1668         } // Autolock scope
1669 
1670 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1671         std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions;
1672         tempOptions.reserve(allocOptions.size());
1673         for (const auto& it : allocOptions) {
1674             tempOptions.emplace_back(it.name.c_str(), it.value);
1675         }
1676         const GraphicBufferAllocator::AllocationRequest allocRequest = {
1677                 .importBuffer = true,
1678                 .width = allocWidth,
1679                 .height = allocHeight,
1680                 .format = allocFormat,
1681                 .layerCount = BQ_LAYER_COUNT,
1682                 .usage = allocUsage,
1683                 .requestorName = allocName,
1684                 .extras = std::move(tempOptions),
1685         };
1686 #endif
1687 
1688 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1689         sp<GraphicBuffer> graphicBuffer = sp<GraphicBuffer>::make(allocRequest);
1690 #else
1691         sp<GraphicBuffer> graphicBuffer =
1692                 sp<GraphicBuffer>::make(allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT,
1693                                         allocUsage, allocName);
1694 #endif
1695 
1696         status_t result = graphicBuffer->initCheck();
1697 
1698         if (result != NO_ERROR) {
1699             BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format"
1700                     " %u, usage %#" PRIx64 ")", width, height, format, usage);
1701             std::lock_guard<std::mutex> lock(mCore->mMutex);
1702             mCore->mIsAllocating = false;
1703             mCore->mIsAllocatingCondition.notify_all();
1704             return;
1705         }
1706 
1707         { // Autolock scope
1708             std::unique_lock<std::mutex> lock(mCore->mMutex);
1709             uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth;
1710             uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight;
1711             if (useDefaultSize && mCore->mAutoPrerotation &&
1712                 (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) {
1713                 std::swap(checkWidth, checkHeight);
1714             }
1715 
1716             PixelFormat checkFormat = format != 0 ?
1717                     format : mCore->mDefaultBufferFormat;
1718             uint64_t checkUsage = usage | mCore->mConsumerUsageBits;
1719             bool allocOptionsChanged = false;
1720 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1721             allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId;
1722 #endif
1723             if (checkWidth != allocWidth || checkHeight != allocHeight ||
1724                 checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) {
1725                 // Something changed while we released the lock. Retry.
1726                 BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying.");
1727                 mCore->mIsAllocating = false;
1728                 mCore->mIsAllocatingCondition.notify_all();
1729                 continue;
1730             }
1731 
1732             if (mCore->mFreeSlots.empty()) {
1733                 BQ_LOGV("allocateBuffers: a slot was occupied while "
1734                         "allocating. Dropping allocated buffer.");
1735             } else {
1736                 auto slot = mCore->mFreeSlots.begin();
1737                 mCore->clearBufferSlotLocked(*slot); // Clean up the slot first
1738                 mSlots[*slot].mGraphicBuffer = graphicBuffer;
1739                 mSlots[*slot].mFence = Fence::NO_FENCE;
1740 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
1741                 mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId;
1742 #endif
1743 
1744                 // freeBufferLocked puts this slot on the free slots list. Since
1745                 // we then attached a buffer, move the slot to free buffer list.
1746                 mCore->mFreeBuffers.push_front(*slot);
1747 
1748                 BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d",
1749                         *slot);
1750 
1751                 // Make sure the erase is done after all uses of the slot
1752                 // iterator since it will be invalid after this point.
1753                 mCore->mFreeSlots.erase(slot);
1754             }
1755 
1756             mCore->mIsAllocating = false;
1757             mCore->mIsAllocatingCondition.notify_all();
1758             VALIDATE_CONSISTENCY();
1759 
1760             // If dequeue is waiting for to allocate a buffer, release the lock until it's not
1761             // waiting anymore so it can use the buffer we just allocated.
1762             while (mDequeueWaitingForAllocation) {
1763                 mDequeueWaitingForAllocationCondition.wait(lock);
1764             }
1765         } // Autolock scope
1766     }
1767 }
1768 
allowAllocation(bool allow)1769 status_t BufferQueueProducer::allowAllocation(bool allow) {
1770     ATRACE_CALL();
1771     BQ_LOGV("allowAllocation: %s", allow ? "true" : "false");
1772 
1773     std::lock_guard<std::mutex> lock(mCore->mMutex);
1774     mCore->mAllowAllocation = allow;
1775     return NO_ERROR;
1776 }
1777 
setGenerationNumber(uint32_t generationNumber)1778 status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) {
1779     ATRACE_CALL();
1780     BQ_LOGV("setGenerationNumber: %u", generationNumber);
1781 
1782     std::lock_guard<std::mutex> lock(mCore->mMutex);
1783     mCore->mGenerationNumber = generationNumber;
1784     return NO_ERROR;
1785 }
1786 
getConsumerName() const1787 String8 BufferQueueProducer::getConsumerName() const {
1788     ATRACE_CALL();
1789     std::lock_guard<std::mutex> lock(mCore->mMutex);
1790     BQ_LOGV("getConsumerName: %s", mConsumerName.c_str());
1791     return mConsumerName;
1792 }
1793 
setSharedBufferMode(bool sharedBufferMode)1794 status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) {
1795     ATRACE_CALL();
1796     BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode);
1797 
1798     std::lock_guard<std::mutex> lock(mCore->mMutex);
1799     if (!sharedBufferMode) {
1800         mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT;
1801     }
1802     mCore->mSharedBufferMode = sharedBufferMode;
1803     return NO_ERROR;
1804 }
1805 
setAutoRefresh(bool autoRefresh)1806 status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) {
1807     ATRACE_CALL();
1808     BQ_LOGV("setAutoRefresh: %d", autoRefresh);
1809 
1810     std::lock_guard<std::mutex> lock(mCore->mMutex);
1811 
1812     mCore->mAutoRefresh = autoRefresh;
1813     return NO_ERROR;
1814 }
1815 
setDequeueTimeout(nsecs_t timeout)1816 status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) {
1817     ATRACE_CALL();
1818     BQ_LOGV("setDequeueTimeout: %" PRId64, timeout);
1819 
1820     std::lock_guard<std::mutex> lock(mCore->mMutex);
1821     bool dequeueBufferCannotBlock =
1822             timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock;
1823     int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock,
1824             mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked();
1825     if (!mCore->adjustAvailableSlotsLocked(delta)) {
1826         BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of "
1827                 "available slots. Delta = %d", delta);
1828         return BAD_VALUE;
1829     }
1830 
1831     mDequeueTimeout = timeout;
1832     mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock;
1833     if (timeout > 0) {
1834         mCore->mQueueBufferCanDrop = false;
1835     }
1836 
1837     VALIDATE_CONSISTENCY();
1838     return NO_ERROR;
1839 }
1840 
setLegacyBufferDrop(bool drop)1841 status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) {
1842     ATRACE_CALL();
1843     BQ_LOGV("setLegacyBufferDrop: drop = %d", drop);
1844 
1845     std::lock_guard<std::mutex> lock(mCore->mMutex);
1846     mCore->mLegacyBufferDrop = drop;
1847     return NO_ERROR;
1848 }
1849 
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,float outTransformMatrix[16])1850 status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
1851         sp<Fence>* outFence, float outTransformMatrix[16]) {
1852     ATRACE_CALL();
1853 
1854     std::lock_guard<std::mutex> lock(mCore->mMutex);
1855     BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1856 
1857     if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) {
1858         *outBuffer = nullptr;
1859         *outFence = Fence::NO_FENCE;
1860         return NO_ERROR;
1861     }
1862 
1863     *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1864     *outFence = mLastQueueBufferFence;
1865 
1866     // Currently only SurfaceFlinger internally ever changes
1867     // GLConsumer's filtering mode, so we just use 'true' here as
1868     // this is slightly specialized for the current client of this API,
1869     // which does want filtering.
1870     GLConsumer::computeTransformMatrix(outTransformMatrix,
1871             mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop,
1872             mLastQueuedTransform, true /* filter */);
1873 
1874     return NO_ERROR;
1875 }
1876 
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,Rect * outRect,uint32_t * outTransform)1877 status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence,
1878                                                   Rect* outRect, uint32_t* outTransform) {
1879     ATRACE_CALL();
1880 
1881     std::lock_guard<std::mutex> lock(mCore->mMutex);
1882     BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot);
1883     if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT ||
1884         mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) {
1885         *outBuffer = nullptr;
1886         *outFence = Fence::NO_FENCE;
1887         return NO_ERROR;
1888     }
1889 
1890     *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer;
1891     *outFence = mLastQueueBufferFence;
1892     *outRect = mLastQueuedCrop;
1893     *outTransform = mLastQueuedTransform;
1894 
1895     return NO_ERROR;
1896 }
1897 
getFrameTimestamps(FrameEventHistoryDelta * outDelta)1898 void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
1899     addAndGetFrameTimestamps(nullptr, outDelta);
1900 }
1901 
addAndGetFrameTimestamps(const NewFrameEventsEntry * newTimestamps,FrameEventHistoryDelta * outDelta)1902 void BufferQueueProducer::addAndGetFrameTimestamps(
1903         const NewFrameEventsEntry* newTimestamps,
1904         FrameEventHistoryDelta* outDelta) {
1905     if (newTimestamps == nullptr && outDelta == nullptr) {
1906         return;
1907     }
1908 
1909     ATRACE_CALL();
1910     BQ_LOGV("addAndGetFrameTimestamps");
1911     sp<IConsumerListener> listener;
1912     {
1913         std::lock_guard<std::mutex> lock(mCore->mMutex);
1914         listener = mCore->mConsumerListener;
1915     }
1916     if (listener != nullptr) {
1917         listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
1918     }
1919 }
1920 
binderDied(const wp<android::IBinder> &)1921 void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) {
1922     // If we're here, it means that a producer we were connected to died.
1923     // We're guaranteed that we are still connected to it because we remove
1924     // this callback upon disconnect. It's therefore safe to read mConnectedApi
1925     // without synchronization here.
1926     int api = mCore->mConnectedApi;
1927     disconnect(api);
1928 }
1929 
getUniqueId(uint64_t * outId) const1930 status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const {
1931     BQ_LOGV("getUniqueId");
1932 
1933     *outId = mCore->mUniqueId;
1934     return NO_ERROR;
1935 }
1936 
getConsumerUsage(uint64_t * outUsage) const1937 status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const {
1938     BQ_LOGV("getConsumerUsage");
1939 
1940     std::lock_guard<std::mutex> lock(mCore->mMutex);
1941     *outUsage = mCore->mConsumerUsageBits;
1942     return NO_ERROR;
1943 }
1944 
setAutoPrerotation(bool autoPrerotation)1945 status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) {
1946     ATRACE_CALL();
1947     BQ_LOGV("setAutoPrerotation: %d", autoPrerotation);
1948 
1949     std::lock_guard<std::mutex> lock(mCore->mMutex);
1950 
1951     mCore->mAutoPrerotation = autoPrerotation;
1952     return NO_ERROR;
1953 }
1954 
1955 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
setFrameRate(float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy)1956 status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility,
1957                                            int8_t changeFrameRateStrategy) {
1958     ATRACE_CALL();
1959     BQ_LOGV("setFrameRate: %.2f", frameRate);
1960 
1961     if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
1962                            "BufferQueueProducer::setFrameRate")) {
1963         return BAD_VALUE;
1964     }
1965 
1966     sp<IConsumerListener> listener;
1967     {
1968         std::lock_guard<std::mutex> lock(mCore->mMutex);
1969         listener = mCore->mConsumerListener;
1970     }
1971     if (listener != nullptr) {
1972         listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy);
1973     }
1974     return NO_ERROR;
1975 }
1976 #endif
1977 
1978 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE)
setAdditionalOptions(const std::vector<gui::AdditionalOptions> & options)1979 status_t BufferQueueProducer::setAdditionalOptions(
1980         const std::vector<gui::AdditionalOptions>& options) {
1981     ATRACE_CALL();
1982     BQ_LOGV("setAdditionalOptions, size = %zu", options.size());
1983 
1984     if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) {
1985         return INVALID_OPERATION;
1986     }
1987 
1988     std::lock_guard<std::mutex> lock(mCore->mMutex);
1989 
1990     if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
1991         BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options");
1992         return NO_INIT;
1993     }
1994 
1995     if (mCore->mAdditionalOptions != options) {
1996         mCore->mAdditionalOptions = options;
1997         mCore->mAdditionalOptionsGenerationId++;
1998     }
1999     return NO_ERROR;
2000 }
2001 #endif
2002 
2003 } // namespace android
2004