1 /*
2  * Copyright (C) 2018 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #undef LOG_TAG
22 #define LOG_TAG "BufferQueueLayer"
23 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
24 #include "BufferQueueLayer.h"
25 
26 #include <compositionengine/LayerFECompositionState.h>
27 #include <gui/BufferQueueConsumer.h>
28 #include <system/window.h>
29 
30 #include "LayerRejecter.h"
31 #include "SurfaceInterceptor.h"
32 
33 #include "FrameTracer/FrameTracer.h"
34 #include "Scheduler/LayerHistory.h"
35 #include "TimeStats/TimeStats.h"
36 
37 namespace android {
38 
BufferQueueLayer(const LayerCreationArgs & args)39 BufferQueueLayer::BufferQueueLayer(const LayerCreationArgs& args) : BufferLayer(args) {}
40 
~BufferQueueLayer()41 BufferQueueLayer::~BufferQueueLayer() {
42     mContentsChangedListener->abandon();
43     mConsumer->abandon();
44 }
45 
46 // -----------------------------------------------------------------------
47 // Interface implementation for Layer
48 // -----------------------------------------------------------------------
49 
onLayerDisplayed(const sp<Fence> & releaseFence)50 void BufferQueueLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
51     mConsumer->setReleaseFence(releaseFence);
52 
53     // Prevent tracing the same release multiple times.
54     if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
55         mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
56                                            std::make_shared<FenceTime>(releaseFence),
57                                            FrameTracer::FrameEvent::RELEASE_FENCE);
58         mPreviousReleasedFrameNumber = mPreviousFrameNumber;
59     }
60 }
61 
setTransformHint(ui::Transform::RotationFlags displayTransformHint)62 void BufferQueueLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
63     BufferLayer::setTransformHint(displayTransformHint);
64     mConsumer->setTransformHint(mTransformHint);
65 }
66 
getOccupancyHistory(bool forceFlush)67 std::vector<OccupancyTracker::Segment> BufferQueueLayer::getOccupancyHistory(bool forceFlush) {
68     std::vector<OccupancyTracker::Segment> history;
69     status_t result = mConsumer->getOccupancyHistory(forceFlush, &history);
70     if (result != NO_ERROR) {
71         ALOGW("[%s] Failed to obtain occupancy history (%d)", getDebugName(), result);
72         return {};
73     }
74     return history;
75 }
76 
releasePendingBuffer(nsecs_t dequeueReadyTime)77 void BufferQueueLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
78     if (!mConsumer->releasePendingBuffer()) {
79         return;
80     }
81 
82     auto releaseFenceTime = std::make_shared<FenceTime>(mConsumer->getPrevFinalReleaseFence());
83     mReleaseTimeline.updateSignalTimes();
84     mReleaseTimeline.push(releaseFenceTime);
85 
86     Mutex::Autolock lock(mFrameEventHistoryMutex);
87     if (mPreviousFrameNumber != 0) {
88         mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
89                                       std::move(releaseFenceTime));
90     }
91 }
92 
setDefaultBufferSize(uint32_t w,uint32_t h)93 void BufferQueueLayer::setDefaultBufferSize(uint32_t w, uint32_t h) {
94     mConsumer->setDefaultBufferSize(w, h);
95 }
96 
getQueuedFrameCount() const97 int32_t BufferQueueLayer::getQueuedFrameCount() const {
98     return mQueuedFrames;
99 }
100 
shouldPresentNow(nsecs_t expectedPresentTime) const101 bool BufferQueueLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
102     if (getSidebandStreamChanged() || getAutoRefresh()) {
103         return true;
104     }
105 
106     if (!hasFrameUpdate()) {
107         return false;
108     }
109 
110     Mutex::Autolock lock(mQueueItemLock);
111 
112     const int64_t addedTime = mQueueItems[0].mTimestamp;
113 
114     // Ignore timestamps more than a second in the future
115     const bool isPlausible = addedTime < (expectedPresentTime + s2ns(1));
116     ALOGW_IF(!isPlausible,
117              "[%s] Timestamp %" PRId64 " seems implausible "
118              "relative to expectedPresent %" PRId64,
119              getDebugName(), addedTime, expectedPresentTime);
120 
121     if (!isPlausible) {
122         mFlinger->mTimeStats->incrementBadDesiredPresent(getSequence());
123     }
124 
125     const bool isDue = addedTime < expectedPresentTime;
126     return isDue || !isPlausible;
127 }
128 
129 // -----------------------------------------------------------------------
130 // Interface implementation for BufferLayer
131 // -----------------------------------------------------------------------
132 
fenceHasSignaled() const133 bool BufferQueueLayer::fenceHasSignaled() const {
134     if (latchUnsignaledBuffers()) {
135         return true;
136     }
137 
138     if (!hasFrameUpdate()) {
139         return true;
140     }
141 
142     Mutex::Autolock lock(mQueueItemLock);
143     if (mQueueItems[0].mIsDroppable) {
144         // Even though this buffer's fence may not have signaled yet, it could
145         // be replaced by another buffer before it has a chance to, which means
146         // that it's possible to get into a situation where a buffer is never
147         // able to be latched. To avoid this, grab this buffer anyway.
148         return true;
149     }
150     const bool fenceSignaled =
151             mQueueItems[0].mFenceTime->getSignalTime() != Fence::SIGNAL_TIME_PENDING;
152     if (!fenceSignaled) {
153         mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
154                                                     TimeStats::LatchSkipReason::LateAcquire);
155     }
156 
157     return fenceSignaled;
158 }
159 
framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const160 bool BufferQueueLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
161     if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
162         return true;
163     }
164 
165     Mutex::Autolock lock(mQueueItemLock);
166     return mQueueItems[0].mTimestamp <= expectedPresentTime;
167 }
168 
getFrameNumber(nsecs_t expectedPresentTime) const169 uint64_t BufferQueueLayer::getFrameNumber(nsecs_t expectedPresentTime) const {
170     Mutex::Autolock lock(mQueueItemLock);
171     uint64_t frameNumber = mQueueItems[0].mFrameNumber;
172 
173     // The head of the queue will be dropped if there are signaled and timely frames behind it
174     if (isRemovedFromCurrentState()) {
175         expectedPresentTime = 0;
176     }
177 
178     for (int i = 1; i < mQueueItems.size(); i++) {
179         const bool fenceSignaled =
180                 mQueueItems[i].mFenceTime->getSignalTime() != Fence::SIGNAL_TIME_PENDING;
181         if (!fenceSignaled) {
182             break;
183         }
184 
185         // We don't drop frames without explicit timestamps
186         if (mQueueItems[i].mIsAutoTimestamp) {
187             break;
188         }
189 
190         const nsecs_t desiredPresent = mQueueItems[i].mTimestamp;
191         if (desiredPresent < expectedPresentTime - BufferQueueConsumer::MAX_REASONABLE_NSEC ||
192             desiredPresent > expectedPresentTime) {
193             break;
194         }
195 
196         frameNumber = mQueueItems[i].mFrameNumber;
197     }
198 
199     return frameNumber;
200 }
201 
getAutoRefresh() const202 bool BufferQueueLayer::getAutoRefresh() const {
203     return mAutoRefresh;
204 }
205 
getSidebandStreamChanged() const206 bool BufferQueueLayer::getSidebandStreamChanged() const {
207     return mSidebandStreamChanged;
208 }
209 
latchSidebandStream(bool & recomputeVisibleRegions)210 bool BufferQueueLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
211     bool sidebandStreamChanged = true;
212     if (mSidebandStreamChanged.compare_exchange_strong(sidebandStreamChanged, false)) {
213         // mSidebandStreamChanged was changed to false
214         mSidebandStream = mConsumer->getSidebandStream();
215         auto* layerCompositionState = editCompositionState();
216         layerCompositionState->sidebandStream = mSidebandStream;
217         if (layerCompositionState->sidebandStream != nullptr) {
218             setTransactionFlags(eTransactionNeeded);
219             mFlinger->setTransactionFlags(eTraversalNeeded);
220         }
221         recomputeVisibleRegions = true;
222 
223         return true;
224     }
225     return false;
226 }
227 
hasFrameUpdate() const228 bool BufferQueueLayer::hasFrameUpdate() const {
229     return mQueuedFrames > 0;
230 }
231 
bindTextureImage()232 status_t BufferQueueLayer::bindTextureImage() {
233     return mConsumer->bindTextureImage();
234 }
235 
updateTexImage(bool & recomputeVisibleRegions,nsecs_t latchTime,nsecs_t expectedPresentTime)236 status_t BufferQueueLayer::updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
237                                           nsecs_t expectedPresentTime) {
238     // This boolean is used to make sure that SurfaceFlinger's shadow copy
239     // of the buffer queue isn't modified when the buffer queue is returning
240     // BufferItem's that weren't actually queued. This can happen in shared
241     // buffer mode.
242     bool queuedBuffer = false;
243     const int32_t layerId = getSequence();
244     LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
245                     getProducerStickyTransform() != 0, mName, mOverrideScalingMode,
246                     getTransformToDisplayInverse());
247 
248     if (isRemovedFromCurrentState()) {
249         expectedPresentTime = 0;
250     }
251 
252     // updateTexImage() below might drop the some buffers at the head of the queue if there is a
253     // buffer behind them which is timely to be presented. However this buffer may not be signaled
254     // yet. The code below makes sure that this wouldn't happen by setting maxFrameNumber to the
255     // last buffer that was signaled.
256     uint64_t lastSignaledFrameNumber = mLastFrameNumberReceived;
257     {
258         Mutex::Autolock lock(mQueueItemLock);
259         for (int i = 0; i < mQueueItems.size(); i++) {
260             bool fenceSignaled =
261                     mQueueItems[i].mFenceTime->getSignalTime() != Fence::SIGNAL_TIME_PENDING;
262             if (!fenceSignaled) {
263                 break;
264             }
265             lastSignaledFrameNumber = mQueueItems[i].mFrameNumber;
266         }
267     }
268     const uint64_t maxFrameNumberToAcquire =
269             std::min(mLastFrameNumberReceived.load(), lastSignaledFrameNumber);
270 
271     status_t updateResult = mConsumer->updateTexImage(&r, expectedPresentTime, &mAutoRefresh,
272                                                       &queuedBuffer, maxFrameNumberToAcquire);
273     if (updateResult == BufferQueue::PRESENT_LATER) {
274         // Producer doesn't want buffer to be displayed yet.  Signal a
275         // layer update so we check again at the next opportunity.
276         mFlinger->signalLayerUpdate();
277         return BAD_VALUE;
278     } else if (updateResult == BufferLayerConsumer::BUFFER_REJECTED) {
279         // If the buffer has been rejected, remove it from the shadow queue
280         // and return early
281         if (queuedBuffer) {
282             Mutex::Autolock lock(mQueueItemLock);
283             mConsumer->mergeSurfaceDamage(mQueueItems[0].mSurfaceDamage);
284             mFlinger->mTimeStats->removeTimeRecord(layerId, mQueueItems[0].mFrameNumber);
285             mQueueItems.removeAt(0);
286             mQueuedFrames--;
287         }
288         return BAD_VALUE;
289     } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
290         // This can occur if something goes wrong when trying to create the
291         // EGLImage for this buffer. If this happens, the buffer has already
292         // been released, so we need to clean up the queue and bug out
293         // early.
294         if (queuedBuffer) {
295             Mutex::Autolock lock(mQueueItemLock);
296             mQueueItems.clear();
297             mQueuedFrames = 0;
298             mFlinger->mTimeStats->onDestroy(layerId);
299             mFlinger->mFrameTracer->onDestroy(layerId);
300         }
301 
302         // Once we have hit this state, the shadow queue may no longer
303         // correctly reflect the incoming BufferQueue's contents, so even if
304         // updateTexImage starts working, the only safe course of action is
305         // to continue to ignore updates.
306         mUpdateTexImageFailed = true;
307 
308         return BAD_VALUE;
309     }
310 
311     if (queuedBuffer) {
312         // Autolock scope
313         auto currentFrameNumber = mConsumer->getFrameNumber();
314 
315         Mutex::Autolock lock(mQueueItemLock);
316 
317         // Remove any stale buffers that have been dropped during
318         // updateTexImage
319         while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
320             mConsumer->mergeSurfaceDamage(mQueueItems[0].mSurfaceDamage);
321             mFlinger->mTimeStats->removeTimeRecord(layerId, mQueueItems[0].mFrameNumber);
322             mQueueItems.removeAt(0);
323             mQueuedFrames--;
324         }
325 
326         uint64_t bufferID = mQueueItems[0].mGraphicBuffer->getId();
327         mFlinger->mFrameTracer->traceFence(layerId, bufferID, currentFrameNumber,
328                                            mQueueItems[0].mFenceTime,
329                                            FrameTracer::FrameEvent::ACQUIRE_FENCE);
330         mFlinger->mTimeStats->setLatchTime(layerId, currentFrameNumber, latchTime);
331         mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, currentFrameNumber, latchTime,
332                                                FrameTracer::FrameEvent::LATCH);
333 
334         mQueueItems.removeAt(0);
335     }
336 
337     // Decrement the queued-frames count.  Signal another event if we
338     // have more frames pending.
339     if ((queuedBuffer && mQueuedFrames.fetch_sub(1) > 1) || mAutoRefresh) {
340         mFlinger->signalLayerUpdate();
341     }
342 
343     return NO_ERROR;
344 }
345 
updateActiveBuffer()346 status_t BufferQueueLayer::updateActiveBuffer() {
347     // update the active buffer
348     mPreviousBufferId = getCurrentBufferId();
349     mBufferInfo.mBuffer =
350             mConsumer->getCurrentBuffer(&mBufferInfo.mBufferSlot, &mBufferInfo.mFence);
351 
352     if (mBufferInfo.mBuffer == nullptr) {
353         // this can only happen if the very first buffer was rejected.
354         return BAD_VALUE;
355     }
356     return NO_ERROR;
357 }
358 
updateFrameNumber(nsecs_t latchTime)359 status_t BufferQueueLayer::updateFrameNumber(nsecs_t latchTime) {
360     mPreviousFrameNumber = mCurrentFrameNumber;
361     mCurrentFrameNumber = mConsumer->getFrameNumber();
362 
363     {
364         Mutex::Autolock lock(mFrameEventHistoryMutex);
365         mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
366     }
367     return NO_ERROR;
368 }
369 
370 // -----------------------------------------------------------------------
371 // Interface implementation for BufferLayerConsumer::ContentsChangedListener
372 // -----------------------------------------------------------------------
373 
onFrameDequeued(const uint64_t bufferId)374 void BufferQueueLayer::onFrameDequeued(const uint64_t bufferId) {
375     const int32_t layerId = getSequence();
376     mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
377     mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, FrameTracer::UNSPECIFIED_FRAME_NUMBER,
378                                            systemTime(), FrameTracer::FrameEvent::DEQUEUE);
379 }
380 
onFrameDetached(const uint64_t bufferId)381 void BufferQueueLayer::onFrameDetached(const uint64_t bufferId) {
382     const int32_t layerId = getSequence();
383     mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
384     mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, FrameTracer::UNSPECIFIED_FRAME_NUMBER,
385                                            systemTime(), FrameTracer::FrameEvent::DETACH);
386 }
387 
onFrameCancelled(const uint64_t bufferId)388 void BufferQueueLayer::onFrameCancelled(const uint64_t bufferId) {
389     const int32_t layerId = getSequence();
390     mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, FrameTracer::UNSPECIFIED_FRAME_NUMBER,
391                                            systemTime(), FrameTracer::FrameEvent::CANCEL);
392 }
393 
onFrameAvailable(const BufferItem & item)394 void BufferQueueLayer::onFrameAvailable(const BufferItem& item) {
395     const int32_t layerId = getSequence();
396     mFlinger->mFrameTracer->traceTimestamp(layerId, item.mGraphicBuffer->getId(), item.mFrameNumber,
397                                            systemTime(), FrameTracer::FrameEvent::QUEUE);
398 
399     ATRACE_CALL();
400     // Add this buffer from our internal queue tracker
401     { // Autolock scope
402         const nsecs_t presentTime = item.mIsAutoTimestamp ? 0 : item.mTimestamp;
403         mFlinger->mScheduler->recordLayerHistory(this, presentTime,
404                                                  LayerHistory::LayerUpdateType::Buffer);
405 
406         Mutex::Autolock lock(mQueueItemLock);
407         // Reset the frame number tracker when we receive the first buffer after
408         // a frame number reset
409         if (item.mFrameNumber == 1) {
410             mLastFrameNumberReceived = 0;
411         }
412 
413         // Ensure that callbacks are handled in order
414         while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
415             status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500));
416             if (result != NO_ERROR) {
417                 ALOGE("[%s] Timed out waiting on callback", getDebugName());
418                 break;
419             }
420         }
421 
422         mQueueItems.push_back(item);
423         mQueuedFrames++;
424 
425         // Wake up any pending callbacks
426         mLastFrameNumberReceived = item.mFrameNumber;
427         mQueueItemCondition.broadcast();
428     }
429 
430     mFlinger->mInterceptor->saveBufferUpdate(layerId, item.mGraphicBuffer->getWidth(),
431                                              item.mGraphicBuffer->getHeight(), item.mFrameNumber);
432 
433     mFlinger->signalLayerUpdate();
434     mConsumer->onBufferAvailable(item);
435 }
436 
onFrameReplaced(const BufferItem & item)437 void BufferQueueLayer::onFrameReplaced(const BufferItem& item) {
438     ATRACE_CALL();
439     { // Autolock scope
440         Mutex::Autolock lock(mQueueItemLock);
441 
442         // Ensure that callbacks are handled in order
443         while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
444             status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500));
445             if (result != NO_ERROR) {
446                 ALOGE("[%s] Timed out waiting on callback", getDebugName());
447                 break;
448             }
449         }
450 
451         if (!hasFrameUpdate()) {
452             ALOGE("Can't replace a frame on an empty queue");
453             return;
454         }
455         mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
456 
457         // Wake up any pending callbacks
458         mLastFrameNumberReceived = item.mFrameNumber;
459         mQueueItemCondition.broadcast();
460     }
461 
462     const int32_t layerId = getSequence();
463     mFlinger->mFrameTracer->traceTimestamp(layerId, item.mGraphicBuffer->getId(), item.mFrameNumber,
464                                            systemTime(), FrameTracer::FrameEvent::QUEUE);
465     mConsumer->onBufferAvailable(item);
466 }
467 
onSidebandStreamChanged()468 void BufferQueueLayer::onSidebandStreamChanged() {
469     bool sidebandStreamChanged = false;
470     if (mSidebandStreamChanged.compare_exchange_strong(sidebandStreamChanged, true)) {
471         // mSidebandStreamChanged was changed to true
472         mFlinger->signalLayerUpdate();
473     }
474 }
475 
476 // -----------------------------------------------------------------------
477 
onFirstRef()478 void BufferQueueLayer::onFirstRef() {
479     BufferLayer::onFirstRef();
480 
481     // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
482     sp<IGraphicBufferProducer> producer;
483     sp<IGraphicBufferConsumer> consumer;
484     mFlinger->getFactory().createBufferQueue(&producer, &consumer, true);
485     mProducer = mFlinger->getFactory().createMonitoredProducer(producer, mFlinger, this);
486     mConsumer =
487             mFlinger->getFactory().createBufferLayerConsumer(consumer, mFlinger->getRenderEngine(),
488                                                              mTextureName, this);
489     mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
490 
491     mContentsChangedListener = new ContentsChangedListener(this);
492     mConsumer->setContentsChangedListener(mContentsChangedListener);
493     mConsumer->setName(String8(mName.data(), mName.size()));
494 
495     // BufferQueueCore::mMaxDequeuedBufferCount is default to 1
496     if (!mFlinger->isLayerTripleBufferingDisabled()) {
497         mProducer->setMaxDequeuedBufferCount(2);
498     }
499 }
500 
setDefaultBufferProperties(uint32_t w,uint32_t h,PixelFormat format)501 status_t BufferQueueLayer::setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format) {
502     uint32_t const maxSurfaceDims =
503           std::min(mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
504 
505     // never allow a surface larger than what our underlying GL implementation
506     // can handle.
507     if ((uint32_t(w) > maxSurfaceDims) || (uint32_t(h) > maxSurfaceDims)) {
508         ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
509         return BAD_VALUE;
510     }
511 
512     setDefaultBufferSize(w, h);
513     mConsumer->setDefaultBufferFormat(format);
514     mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
515 
516     return NO_ERROR;
517 }
518 
getProducer() const519 sp<IGraphicBufferProducer> BufferQueueLayer::getProducer() const {
520     return mProducer;
521 }
522 
getProducerStickyTransform() const523 uint32_t BufferQueueLayer::getProducerStickyTransform() const {
524     int producerStickyTransform = 0;
525     int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
526     if (ret != OK) {
527         ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
528               strerror(-ret), ret);
529         return 0;
530     }
531     return static_cast<uint32_t>(producerStickyTransform);
532 }
533 
gatherBufferInfo()534 void BufferQueueLayer::gatherBufferInfo() {
535     BufferLayer::gatherBufferInfo();
536 
537     mBufferInfo.mDesiredPresentTime = mConsumer->getTimestamp();
538     mBufferInfo.mFenceTime = mConsumer->getCurrentFenceTime();
539     mBufferInfo.mFence = mConsumer->getCurrentFence();
540     mBufferInfo.mTransform = mConsumer->getCurrentTransform();
541     mBufferInfo.mDataspace = translateDataspace(mConsumer->getCurrentDataSpace());
542     mBufferInfo.mCrop = mConsumer->getCurrentCrop();
543     mBufferInfo.mScaleMode = mConsumer->getCurrentScalingMode();
544     mBufferInfo.mSurfaceDamage = mConsumer->getSurfaceDamage();
545     mBufferInfo.mHdrMetadata = mConsumer->getCurrentHdrMetadata();
546     mBufferInfo.mApi = mConsumer->getCurrentApi();
547     mBufferInfo.mTransformToDisplayInverse = mConsumer->getTransformToDisplayInverse();
548 }
549 
createClone()550 sp<Layer> BufferQueueLayer::createClone() {
551     LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
552     args.textureName = mTextureName;
553     sp<BufferQueueLayer> layer = mFlinger->getFactory().createBufferQueueLayer(args);
554     layer->setInitialValuesForClone(this);
555 
556     return layer;
557 }
558 
559 // -----------------------------------------------------------------------
560 // Interface implementation for BufferLayerConsumer::ContentsChangedListener
561 // -----------------------------------------------------------------------
562 
onFrameAvailable(const BufferItem & item)563 void BufferQueueLayer::ContentsChangedListener::onFrameAvailable(const BufferItem& item) {
564     Mutex::Autolock lock(mMutex);
565     if (mBufferQueueLayer != nullptr) {
566         mBufferQueueLayer->onFrameAvailable(item);
567     }
568 }
569 
onFrameReplaced(const BufferItem & item)570 void BufferQueueLayer::ContentsChangedListener::onFrameReplaced(const BufferItem& item) {
571     Mutex::Autolock lock(mMutex);
572     if (mBufferQueueLayer != nullptr) {
573         mBufferQueueLayer->onFrameReplaced(item);
574     }
575 }
576 
onSidebandStreamChanged()577 void BufferQueueLayer::ContentsChangedListener::onSidebandStreamChanged() {
578     Mutex::Autolock lock(mMutex);
579     if (mBufferQueueLayer != nullptr) {
580         mBufferQueueLayer->onSidebandStreamChanged();
581     }
582 }
583 
onFrameDequeued(const uint64_t bufferId)584 void BufferQueueLayer::ContentsChangedListener::onFrameDequeued(const uint64_t bufferId) {
585     Mutex::Autolock lock(mMutex);
586     if (mBufferQueueLayer != nullptr) {
587         mBufferQueueLayer->onFrameDequeued(bufferId);
588     }
589 }
590 
onFrameDetached(const uint64_t bufferId)591 void BufferQueueLayer::ContentsChangedListener::onFrameDetached(const uint64_t bufferId) {
592     Mutex::Autolock lock(mMutex);
593     if (mBufferQueueLayer != nullptr) {
594         mBufferQueueLayer->onFrameDetached(bufferId);
595     }
596 }
597 
onFrameCancelled(const uint64_t bufferId)598 void BufferQueueLayer::ContentsChangedListener::onFrameCancelled(const uint64_t bufferId) {
599     Mutex::Autolock lock(mMutex);
600     if (mBufferQueueLayer != nullptr) {
601         mBufferQueueLayer->onFrameCancelled(bufferId);
602     }
603 }
604 
abandon()605 void BufferQueueLayer::ContentsChangedListener::abandon() {
606     Mutex::Autolock lock(mMutex);
607     mBufferQueueLayer = nullptr;
608 }
609 
610 // -----------------------------------------------------------------------
611 
612 } // namespace android
613 
614 // TODO(b/129481165): remove the #pragma below and fix conversion issues
615 #pragma clang diagnostic pop // ignored "-Wconversion"
616