• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #pragma clang diagnostic ignored "-Wextra"
21 
22 //#define LOG_NDEBUG 0
23 #undef LOG_TAG
24 #define LOG_TAG "BufferStateLayer"
25 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
26 
27 #include "BufferStateLayer.h"
28 
29 #include <limits>
30 
31 #include <FrameTimeline/FrameTimeline.h>
32 #include <compositionengine/LayerFECompositionState.h>
33 #include <gui/BufferQueue.h>
34 #include <private/gui/SyncFeatures.h>
35 #include <renderengine/Image.h>
36 #include "TunnelModeEnabledReporter.h"
37 
38 #include "EffectLayer.h"
39 #include "FrameTracer/FrameTracer.h"
40 #include "TimeStats/TimeStats.h"
41 
42 namespace android {
43 
44 using PresentState = frametimeline::SurfaceFrame::PresentState;
45 namespace {
callReleaseBufferCallback(const sp<ITransactionCompletedListener> & listener,const sp<GraphicBuffer> & buffer,uint64_t framenumber,const sp<Fence> & releaseFence,uint32_t transformHint,uint32_t currentMaxAcquiredBufferCount)46 void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
47                                const sp<GraphicBuffer>& buffer, uint64_t framenumber,
48                                const sp<Fence>& releaseFence, uint32_t transformHint,
49                                uint32_t currentMaxAcquiredBufferCount) {
50     if (!listener) {
51         return;
52     }
53     listener->onReleaseBuffer({buffer->getId(), framenumber},
54                               releaseFence ? releaseFence : Fence::NO_FENCE, transformHint,
55                               currentMaxAcquiredBufferCount);
56 }
57 } // namespace
58 
BufferStateLayer(const LayerCreationArgs & args)59 BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
60       : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
61     mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
62 }
63 
~BufferStateLayer()64 BufferStateLayer::~BufferStateLayer() {
65     // The original layer and the clone layer share the same texture and buffer. Therefore, only
66     // one of the layers, in this case the original layer, needs to handle the deletion. The
67     // original layer and the clone should be removed at the same time so there shouldn't be any
68     // issue with the clone layer trying to use the texture.
69     if (mBufferInfo.mBuffer != nullptr && !isClone()) {
70         callReleaseBufferCallback(mDrawingState.releaseBufferListener,
71                                   mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
72                                   mBufferInfo.mFence, mTransformHint,
73                                   mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
74                                           mOwnerUid));
75     }
76 }
77 
addReleaseFence(const sp<CallbackHandle> & ch,const sp<Fence> & fence)78 status_t BufferStateLayer::addReleaseFence(const sp<CallbackHandle>& ch,
79                                            const sp<Fence>& fence) {
80     if (ch == nullptr) {
81         return OK;
82     }
83     ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
84     if (!ch->previousReleaseFence.get()) {
85         ch->previousReleaseFence = fence;
86         return OK;
87     }
88 
89     // Below logic is lifted from ConsumerBase.cpp:
90     // Check status of fences first because merging is expensive.
91     // Merging an invalid fence with any other fence results in an
92     // invalid fence.
93     auto currentStatus = ch->previousReleaseFence->getStatus();
94     if (currentStatus == Fence::Status::Invalid) {
95         ALOGE("Existing fence has invalid state, layer: %s", mName.c_str());
96         return BAD_VALUE;
97     }
98 
99     auto incomingStatus = fence->getStatus();
100     if (incomingStatus == Fence::Status::Invalid) {
101         ALOGE("New fence has invalid state, layer: %s", mName.c_str());
102         ch->previousReleaseFence = fence;
103         return BAD_VALUE;
104     }
105 
106     // If both fences are signaled or both are unsignaled, we need to merge
107     // them to get an accurate timestamp.
108     if (currentStatus == incomingStatus) {
109         char fenceName[32] = {};
110         snprintf(fenceName, 32, "%.28s", mName.c_str());
111         sp<Fence> mergedFence = Fence::merge(
112                 fenceName, ch->previousReleaseFence, fence);
113         if (!mergedFence.get()) {
114             ALOGE("failed to merge release fences, layer: %s", mName.c_str());
115             // synchronization is broken, the best we can do is hope fences
116             // signal in order so the new fence will act like a union
117             ch->previousReleaseFence = fence;
118             return BAD_VALUE;
119         }
120         ch->previousReleaseFence = mergedFence;
121     } else if (incomingStatus == Fence::Status::Unsignaled) {
122         // If one fence has signaled and the other hasn't, the unsignaled
123         // fence will approximately correspond with the correct timestamp.
124         // There's a small race if both fences signal at about the same time
125         // and their statuses are retrieved with unfortunate timing. However,
126         // by this point, they will have both signaled and only the timestamp
127         // will be slightly off; any dependencies after this point will
128         // already have been met.
129         ch->previousReleaseFence = fence;
130     }
131     // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
132 
133     return OK;
134 }
135 
136 // -----------------------------------------------------------------------
137 // Interface implementation for Layer
138 // -----------------------------------------------------------------------
onLayerDisplayed(const sp<Fence> & releaseFence)139 void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
140     if (!releaseFence->isValid()) {
141         return;
142     }
143     // The previous release fence notifies the client that SurfaceFlinger is done with the previous
144     // buffer that was presented on this layer. The first transaction that came in this frame that
145     // replaced the previous buffer on this layer needs this release fence, because the fence will
146     // let the client know when that previous buffer is removed from the screen.
147     //
148     // Every other transaction on this layer does not need a release fence because no other
149     // Transactions that were set on this layer this frame are going to have their preceeding buffer
150     // removed from the display this frame.
151     //
152     // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
153     // buffer so it doesn't need a previous release fence because the layer still needs the previous
154     // buffer. The second transaction contains a buffer so it needs a previous release fence because
155     // the previous buffer will be released this frame. The third transaction also contains a
156     // buffer. It replaces the buffer in the second transaction. The buffer in the second
157     // transaction will now no longer be presented so it is released immediately and the third
158     // transaction doesn't need a previous release fence.
159     sp<CallbackHandle> ch;
160     for (auto& handle : mDrawingState.callbackHandles) {
161         if (handle->releasePreviousBuffer) {
162             ch = handle;
163             break;
164         }
165     }
166     auto status = addReleaseFence(ch, releaseFence);
167     if (status != OK) {
168         ALOGE("Failed to add release fence for layer %s", getName().c_str());
169     }
170 
171     mPreviousReleaseFence = releaseFence;
172 
173     // Prevent tracing the same release multiple times.
174     if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
175         mPreviousReleasedFrameNumber = mPreviousFrameNumber;
176     }
177 }
178 
onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame> & surfaceFrame)179 void BufferStateLayer::onSurfaceFrameCreated(
180         const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
181     while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
182         // Too many SurfaceFrames pending classification. The front of the deque is probably not
183         // tracked by FrameTimeline and will never be presented. This will only result in a memory
184         // leak.
185         ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
186               mName.c_str());
187         std::string miniDump = mPendingJankClassifications.front()->miniDump();
188         ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
189         mPendingJankClassifications.pop_front();
190     }
191     mPendingJankClassifications.emplace_back(surfaceFrame);
192 }
193 
releasePendingBuffer(nsecs_t dequeueReadyTime)194 void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
195     for (const auto& handle : mDrawingState.callbackHandles) {
196         handle->transformHint = mTransformHint;
197         handle->dequeueReadyTime = dequeueReadyTime;
198         handle->currentMaxAcquiredBufferCount =
199                 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
200     }
201 
202     // If there are multiple transactions in this frame, set the previous id on the earliest
203     // transacton. We don't need to pass in the released buffer id to multiple transactions.
204     // The buffer id does not have to correspond to any particular transaction as long as the
205     // listening end point is the same but the client expects the first transaction callback that
206     // replaces the presented buffer to contain the release fence. This follows the same logic.
207     // see BufferStateLayer::onLayerDisplayed.
208     for (auto& handle : mDrawingState.callbackHandles) {
209         if (handle->releasePreviousBuffer) {
210             handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
211             break;
212         }
213     }
214 
215     std::vector<JankData> jankData;
216     jankData.reserve(mPendingJankClassifications.size());
217     while (!mPendingJankClassifications.empty()
218             && mPendingJankClassifications.front()->getJankType()) {
219         std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
220                 mPendingJankClassifications.front();
221         mPendingJankClassifications.pop_front();
222         jankData.emplace_back(
223                 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
224     }
225 
226     mFlinger->getTransactionCallbackInvoker().finalizePendingCallbackHandles(
227             mDrawingState.callbackHandles, jankData);
228 
229     mDrawingState.callbackHandles = {};
230 
231     const sp<Fence>& releaseFence(mPreviousReleaseFence);
232     std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
233     {
234         Mutex::Autolock lock(mFrameEventHistoryMutex);
235         if (mPreviousFrameNumber != 0) {
236             mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
237                                           std::move(releaseFenceTime));
238         }
239     }
240 }
241 
finalizeFrameEventHistory(const std::shared_ptr<FenceTime> & glDoneFence,const CompositorTiming & compositorTiming)242 void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
243                                                  const CompositorTiming& compositorTiming) {
244     for (const auto& handle : mDrawingState.callbackHandles) {
245         handle->gpuCompositionDoneFence = glDoneFence;
246         handle->compositorTiming = compositorTiming;
247     }
248 }
249 
willPresentCurrentTransaction() const250 bool BufferStateLayer::willPresentCurrentTransaction() const {
251     // Returns true if the most recent Transaction applied to CurrentState will be presented.
252     return (getSidebandStreamChanged() || getAutoRefresh() ||
253             (mDrawingState.modified &&
254              (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
255 }
256 
getCrop(const Layer::State & s) const257 Rect BufferStateLayer::getCrop(const Layer::State& s) const {
258     return s.crop;
259 }
260 
setTransform(uint32_t transform)261 bool BufferStateLayer::setTransform(uint32_t transform) {
262     if (mDrawingState.bufferTransform == transform) return false;
263     mDrawingState.bufferTransform = transform;
264     mDrawingState.modified = true;
265     setTransactionFlags(eTransactionNeeded);
266     return true;
267 }
268 
setTransformToDisplayInverse(bool transformToDisplayInverse)269 bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
270     if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
271     mDrawingState.sequence++;
272     mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
273     mDrawingState.modified = true;
274     setTransactionFlags(eTransactionNeeded);
275     return true;
276 }
277 
setCrop(const Rect & crop)278 bool BufferStateLayer::setCrop(const Rect& crop) {
279     if (mDrawingState.crop == crop) return false;
280     mDrawingState.sequence++;
281     mDrawingState.crop = crop;
282 
283     mDrawingState.modified = true;
284     setTransactionFlags(eTransactionNeeded);
285     return true;
286 }
287 
setBufferCrop(const Rect & bufferCrop)288 bool BufferStateLayer::setBufferCrop(const Rect& bufferCrop) {
289     if (mDrawingState.bufferCrop == bufferCrop) return false;
290 
291     mDrawingState.sequence++;
292     mDrawingState.bufferCrop = bufferCrop;
293 
294     mDrawingState.modified = true;
295     setTransactionFlags(eTransactionNeeded);
296     return true;
297 }
298 
setDestinationFrame(const Rect & destinationFrame)299 bool BufferStateLayer::setDestinationFrame(const Rect& destinationFrame) {
300     if (mDrawingState.destinationFrame == destinationFrame) return false;
301 
302     mDrawingState.sequence++;
303     mDrawingState.destinationFrame = destinationFrame;
304 
305     mDrawingState.modified = true;
306     setTransactionFlags(eTransactionNeeded);
307     return true;
308 }
309 
assignTransform(ui::Transform * dst,ui::Transform & from)310 static bool assignTransform(ui::Transform* dst, ui::Transform& from) {
311     if (*dst == from) {
312         return false;
313     }
314     *dst = from;
315     return true;
316 }
317 
318 // Translate destination frame into scale and position. If a destination frame is not set, use the
319 // provided scale and position
updateGeometry()320 bool BufferStateLayer::updateGeometry() {
321     if (mDrawingState.destinationFrame.isEmpty()) {
322         // If destination frame is not set, use the requested transform set via
323         // BufferStateLayer::setPosition and BufferStateLayer::setMatrix.
324         return assignTransform(&mDrawingState.transform, mRequestedTransform);
325     }
326 
327     Rect destRect = mDrawingState.destinationFrame;
328     int32_t destW = destRect.width();
329     int32_t destH = destRect.height();
330     if (destRect.left < 0) {
331         destRect.left = 0;
332         destRect.right = destW;
333     }
334     if (destRect.top < 0) {
335         destRect.top = 0;
336         destRect.bottom = destH;
337     }
338 
339     if (!mDrawingState.buffer) {
340         ui::Transform t;
341         t.set(destRect.left, destRect.top);
342         return assignTransform(&mDrawingState.transform, t);
343     }
344 
345     uint32_t bufferWidth = mDrawingState.buffer->getBuffer()->getWidth();
346     uint32_t bufferHeight = mDrawingState.buffer->getBuffer()->getHeight();
347     // Undo any transformations on the buffer.
348     if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
349         std::swap(bufferWidth, bufferHeight);
350     }
351     uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
352     if (mDrawingState.transformToDisplayInverse) {
353         if (invTransform & ui::Transform::ROT_90) {
354             std::swap(bufferWidth, bufferHeight);
355         }
356     }
357 
358     float sx = destW / static_cast<float>(bufferWidth);
359     float sy = destH / static_cast<float>(bufferHeight);
360     ui::Transform t;
361     t.set(sx, 0, 0, sy);
362     t.set(destRect.left, destRect.top);
363     return assignTransform(&mDrawingState.transform, t);
364 }
365 
setMatrix(const layer_state_t::matrix22_t & matrix,bool allowNonRectPreservingTransforms)366 bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix,
367                                  bool allowNonRectPreservingTransforms) {
368     if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
369         mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
370         return false;
371     }
372 
373     ui::Transform t;
374     t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
375 
376     if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
377         ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
378               "ROTATE_SURFACE_FLINGER ignored");
379         return false;
380     }
381 
382     mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
383 
384     mDrawingState.sequence++;
385     mDrawingState.modified = true;
386     setTransactionFlags(eTransactionNeeded);
387 
388     return true;
389 }
390 
setPosition(float x,float y)391 bool BufferStateLayer::setPosition(float x, float y) {
392     if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
393         return false;
394     }
395 
396     mRequestedTransform.set(x, y);
397 
398     mDrawingState.sequence++;
399     mDrawingState.modified = true;
400     setTransactionFlags(eTransactionNeeded);
401 
402     return true;
403 }
404 
addFrameEvent(const sp<Fence> & acquireFence,nsecs_t postedTime,nsecs_t desiredPresentTime)405 bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
406                                      nsecs_t desiredPresentTime) {
407     Mutex::Autolock lock(mFrameEventHistoryMutex);
408     mAcquireTimeline.updateSignalTimes();
409     std::shared_ptr<FenceTime> acquireFenceTime =
410             std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
411     NewFrameEventsEntry newTimestamps = {mDrawingState.frameNumber, postedTime, desiredPresentTime,
412                                          acquireFenceTime};
413     mFrameEventHistory.setProducerWantsEvents();
414     mFrameEventHistory.addQueue(newTimestamps);
415     return true;
416 }
417 
setBuffer(const std::shared_ptr<renderengine::ExternalTexture> & buffer,const sp<Fence> & acquireFence,nsecs_t postTime,nsecs_t desiredPresentTime,bool isAutoTimestamp,const client_cache_t & clientCacheId,uint64_t frameNumber,std::optional<nsecs_t> dequeueTime,const FrameTimelineInfo & info,const sp<ITransactionCompletedListener> & releaseBufferListener)418 bool BufferStateLayer::setBuffer(const std::shared_ptr<renderengine::ExternalTexture>& buffer,
419                                  const sp<Fence>& acquireFence, nsecs_t postTime,
420                                  nsecs_t desiredPresentTime, bool isAutoTimestamp,
421                                  const client_cache_t& clientCacheId, uint64_t frameNumber,
422                                  std::optional<nsecs_t> dequeueTime, const FrameTimelineInfo& info,
423                                  const sp<ITransactionCompletedListener>& releaseBufferListener) {
424     ATRACE_CALL();
425 
426     if (mDrawingState.buffer) {
427         mReleasePreviousBuffer = true;
428         if (mDrawingState.buffer != mBufferInfo.mBuffer ||
429             mDrawingState.frameNumber != mBufferInfo.mFrameNumber) {
430             // If mDrawingState has a buffer, and we are about to update again
431             // before swapping to drawing state, then the first buffer will be
432             // dropped and we should decrement the pending buffer count and
433             // call any release buffer callbacks if set.
434             callReleaseBufferCallback(mDrawingState.releaseBufferListener,
435                                       mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
436                                       mDrawingState.acquireFence, mTransformHint,
437                                       mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
438                                               mOwnerUid));
439             decrementPendingBufferCount();
440             if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
441                 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
442               addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
443               mDrawingState.bufferSurfaceFrameTX.reset();
444             }
445         }
446     }
447 
448     mDrawingState.frameNumber = frameNumber;
449     mDrawingState.releaseBufferListener = releaseBufferListener;
450     mDrawingState.buffer = buffer;
451     mDrawingState.clientCacheId = clientCacheId;
452     mDrawingState.modified = true;
453     setTransactionFlags(eTransactionNeeded);
454 
455     const int32_t layerId = getSequence();
456     mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
457                                       mOwnerUid, postTime, getGameMode());
458     mDrawingState.desiredPresentTime = desiredPresentTime;
459     mDrawingState.isAutoTimestamp = isAutoTimestamp;
460 
461     const nsecs_t presentTime = [&] {
462         if (!isAutoTimestamp) return desiredPresentTime;
463 
464         const auto prediction =
465                 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
466         if (prediction.has_value()) return prediction->presentTime;
467 
468         return static_cast<nsecs_t>(0);
469     }();
470     mFlinger->mScheduler->recordLayerHistory(this, presentTime,
471                                              LayerHistory::LayerUpdateType::Buffer);
472 
473     addFrameEvent(acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
474 
475     setFrameTimelineVsyncForBufferTransaction(info, postTime);
476 
477     if (buffer && dequeueTime && *dequeueTime != 0) {
478         const uint64_t bufferId = buffer->getBuffer()->getId();
479         mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
480         mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
481                                                FrameTracer::FrameEvent::DEQUEUE);
482         mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
483                                                FrameTracer::FrameEvent::QUEUE);
484     }
485 
486     mDrawingState.width = mDrawingState.buffer->getBuffer()->getWidth();
487     mDrawingState.height = mDrawingState.buffer->getBuffer()->getHeight();
488 
489     return true;
490 }
491 
setAcquireFence(const sp<Fence> & fence)492 bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
493     mDrawingState.acquireFence = fence;
494     mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(fence);
495 
496     // The acquire fences of BufferStateLayers have already signaled before they are set
497     mCallbackHandleAcquireTime = mDrawingState.acquireFenceTime->getSignalTime();
498 
499     mDrawingState.modified = true;
500     setTransactionFlags(eTransactionNeeded);
501     return true;
502 }
503 
setDataspace(ui::Dataspace dataspace)504 bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
505     if (mDrawingState.dataspace == dataspace) return false;
506     mDrawingState.dataspace = dataspace;
507     mDrawingState.modified = true;
508     setTransactionFlags(eTransactionNeeded);
509     return true;
510 }
511 
setHdrMetadata(const HdrMetadata & hdrMetadata)512 bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
513     if (mDrawingState.hdrMetadata == hdrMetadata) return false;
514     mDrawingState.hdrMetadata = hdrMetadata;
515     mDrawingState.modified = true;
516     setTransactionFlags(eTransactionNeeded);
517     return true;
518 }
519 
setSurfaceDamageRegion(const Region & surfaceDamage)520 bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
521     mDrawingState.surfaceDamageRegion = surfaceDamage;
522     mDrawingState.modified = true;
523     setTransactionFlags(eTransactionNeeded);
524     return true;
525 }
526 
setApi(int32_t api)527 bool BufferStateLayer::setApi(int32_t api) {
528     if (mDrawingState.api == api) return false;
529     mDrawingState.api = api;
530     mDrawingState.modified = true;
531     setTransactionFlags(eTransactionNeeded);
532     return true;
533 }
534 
setSidebandStream(const sp<NativeHandle> & sidebandStream)535 bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
536     if (mDrawingState.sidebandStream == sidebandStream) return false;
537 
538     if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
539         mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
540     } else if (sidebandStream != nullptr) {
541         mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
542     }
543 
544     mDrawingState.sidebandStream = sidebandStream;
545     mDrawingState.modified = true;
546     setTransactionFlags(eTransactionNeeded);
547     if (!mSidebandStreamChanged.exchange(true)) {
548         // mSidebandStreamChanged was false
549         mFlinger->signalLayerUpdate();
550     }
551     return true;
552 }
553 
setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>> & handles)554 bool BufferStateLayer::setTransactionCompletedListeners(
555         const std::vector<sp<CallbackHandle>>& handles) {
556     // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
557     if (handles.empty()) {
558         mReleasePreviousBuffer = false;
559         return false;
560     }
561 
562     const bool willPresent = willPresentCurrentTransaction();
563 
564     for (const auto& handle : handles) {
565         // If this transaction set a buffer on this layer, release its previous buffer
566         handle->releasePreviousBuffer = mReleasePreviousBuffer;
567 
568         // If this layer will be presented in this frame
569         if (willPresent) {
570             // If this transaction set an acquire fence on this layer, set its acquire time
571             handle->acquireTime = mCallbackHandleAcquireTime;
572             handle->frameNumber = mDrawingState.frameNumber;
573 
574             // Notify the transaction completed thread that there is a pending latched callback
575             // handle
576             mFlinger->getTransactionCallbackInvoker().registerPendingCallbackHandle(handle);
577 
578             // Store so latched time and release fence can be set
579             mDrawingState.callbackHandles.push_back(handle);
580 
581         } else { // If this layer will NOT need to be relatched and presented this frame
582             // Notify the transaction completed thread this handle is done
583             mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
584         }
585     }
586 
587     mReleasePreviousBuffer = false;
588     mCallbackHandleAcquireTime = -1;
589 
590     return willPresent;
591 }
592 
setTransparentRegionHint(const Region & transparent)593 bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
594     mDrawingState.sequence++;
595     mDrawingState.transparentRegionHint = transparent;
596     mDrawingState.modified = true;
597     setTransactionFlags(eTransactionNeeded);
598     return true;
599 }
600 
getBufferSize(const State & s) const601 Rect BufferStateLayer::getBufferSize(const State& s) const {
602     // for buffer state layers we use the display frame size as the buffer size.
603 
604     if (mBufferInfo.mBuffer == nullptr) {
605         return Rect::INVALID_RECT;
606     }
607 
608     uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
609     uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
610 
611     // Undo any transformations on the buffer and return the result.
612     if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
613         std::swap(bufWidth, bufHeight);
614     }
615 
616     if (getTransformToDisplayInverse()) {
617         uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
618         if (invTransform & ui::Transform::ROT_90) {
619             std::swap(bufWidth, bufHeight);
620         }
621     }
622 
623     return Rect(0, 0, bufWidth, bufHeight);
624 }
625 
computeSourceBounds(const FloatRect & parentBounds) const626 FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
627     if (mBufferInfo.mBuffer == nullptr) {
628         return parentBounds;
629     }
630 
631     return getBufferSize(getDrawingState()).toFloatRect();
632 }
633 
634 // -----------------------------------------------------------------------
635 
636 // -----------------------------------------------------------------------
637 // Interface implementation for BufferLayer
638 // -----------------------------------------------------------------------
fenceHasSignaled() const639 bool BufferStateLayer::fenceHasSignaled() const {
640     if (SurfaceFlinger::enableLatchUnsignaled) {
641         return true;
642     }
643 
644     const bool fenceSignaled =
645             getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
646     if (!fenceSignaled) {
647         mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
648                                                     TimeStats::LatchSkipReason::LateAcquire);
649     }
650 
651     return fenceSignaled;
652 }
653 
framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const654 bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
655     if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
656         return true;
657     }
658 
659     return mDrawingState.isAutoTimestamp || mDrawingState.desiredPresentTime <= expectedPresentTime;
660 }
661 
onPreComposition(nsecs_t refreshStartTime)662 bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
663     for (const auto& handle : mDrawingState.callbackHandles) {
664         handle->refreshStartTime = refreshStartTime;
665     }
666     return BufferLayer::onPreComposition(refreshStartTime);
667 }
668 
getFrameNumber(nsecs_t) const669 uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
670     return mDrawingState.frameNumber;
671 }
672 
673 /**
674  * This is the frameNumber used for deferred transaction signalling. We need to use this because
675  * of cases where we defer a transaction for a surface to itself. In the BLAST world this
676  * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
677  * deferred transaction?) but this is an important legacy use case, for example moving
678  * a window at the same time it draws makes use of this kind of technique. So anyway
679  * imagine we have something like this:
680  *
681  * Transaction { // containing
682  *     Buffer -> frameNumber = 2
683  *     DeferTransactionUntil -> frameNumber = 2
684  *     Random other stuff
685  *  }
686  * Now imagine mFrameNumber returned mDrawingState.frameNumber (or mCurrentFrameNumber).
687  * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
688  * haven't swapped mDrawingState to mDrawingState yet we will think the sync point
689  * is not ready. So we will return false from applyPendingState and not swap
690  * current state to drawing state. But because we don't swap current state
691  * to drawing state the number will never update and we will be stuck. This way
692  * we can see we need to return the frame number for the buffer we are about
693  * to apply.
694  */
getHeadFrameNumber(nsecs_t) const695 uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
696     return mDrawingState.frameNumber;
697 }
698 
setAutoRefresh(bool autoRefresh)699 void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
700     if (!mAutoRefresh.exchange(autoRefresh)) {
701         mFlinger->signalLayerUpdate();
702     }
703 }
704 
latchSidebandStream(bool & recomputeVisibleRegions)705 bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
706     // We need to update the sideband stream if the layer has both a buffer and a sideband stream.
707     const bool updateSidebandStream = hasFrameUpdate() && mSidebandStream.get();
708 
709     if (mSidebandStreamChanged.exchange(false) || updateSidebandStream) {
710         const State& s(getDrawingState());
711         // mSidebandStreamChanged was true
712         mSidebandStream = s.sidebandStream;
713         editCompositionState()->sidebandStream = mSidebandStream;
714         if (mSidebandStream != nullptr) {
715             setTransactionFlags(eTransactionNeeded);
716             mFlinger->setTransactionFlags(eTraversalNeeded);
717         }
718         recomputeVisibleRegions = true;
719 
720         return true;
721     }
722     return false;
723 }
724 
hasFrameUpdate() const725 bool BufferStateLayer::hasFrameUpdate() const {
726     const State& c(getDrawingState());
727     return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
728 }
729 
updateTexImage(bool &,nsecs_t latchTime,nsecs_t)730 status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
731                                           nsecs_t /*expectedPresentTime*/) {
732     const State& s(getDrawingState());
733 
734     if (!s.buffer) {
735         if (s.bgColorLayer) {
736             for (auto& handle : mDrawingState.callbackHandles) {
737                 handle->latchTime = latchTime;
738             }
739         }
740         return NO_ERROR;
741     }
742 
743     for (auto& handle : mDrawingState.callbackHandles) {
744         if (handle->frameNumber == mDrawingState.frameNumber) {
745             handle->latchTime = latchTime;
746         }
747     }
748 
749     const int32_t layerId = getSequence();
750     const uint64_t bufferId = mDrawingState.buffer->getBuffer()->getId();
751     const uint64_t frameNumber = mDrawingState.frameNumber;
752     const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
753     mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
754     mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
755 
756     mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
757                                        FrameTracer::FrameEvent::ACQUIRE_FENCE);
758     mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
759                                            FrameTracer::FrameEvent::LATCH);
760 
761     auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
762     if (bufferSurfaceFrame != nullptr &&
763         bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
764         // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
765         // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
766         // are processing the next state.
767         addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
768                                           mDrawingState.acquireFenceTime->getSignalTime(),
769                                           latchTime);
770         mDrawingState.bufferSurfaceFrameTX.reset();
771     }
772 
773     std::deque<sp<CallbackHandle>> remainingHandles;
774     mFlinger->getTransactionCallbackInvoker()
775             .finalizeOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
776     mDrawingState.callbackHandles = remainingHandles;
777 
778     mDrawingStateModified = false;
779 
780     return NO_ERROR;
781 }
782 
updateActiveBuffer()783 status_t BufferStateLayer::updateActiveBuffer() {
784     const State& s(getDrawingState());
785 
786     if (s.buffer == nullptr) {
787         return BAD_VALUE;
788     }
789 
790     if (!mBufferInfo.mBuffer || s.buffer->getBuffer() != mBufferInfo.mBuffer->getBuffer()) {
791         decrementPendingBufferCount();
792     }
793 
794     mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
795     mBufferInfo.mBuffer = s.buffer;
796     mBufferInfo.mFence = s.acquireFence;
797     mBufferInfo.mFrameNumber = s.frameNumber;
798 
799     return NO_ERROR;
800 }
801 
updateFrameNumber(nsecs_t latchTime)802 status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
803     // TODO(marissaw): support frame history events
804     mPreviousFrameNumber = mCurrentFrameNumber;
805     mCurrentFrameNumber = mDrawingState.frameNumber;
806     {
807         Mutex::Autolock lock(mFrameEventHistoryMutex);
808         mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
809     }
810     return NO_ERROR;
811 }
812 
bufferErased(const client_cache_t & clientCacheId)813 void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
814     std::lock_guard lock(mMutex);
815     if (!clientCacheId.isValid()) {
816         ALOGE("invalid process, failed to erase buffer");
817         return;
818     }
819     eraseBufferLocked(clientCacheId);
820 }
821 
getHwcCacheSlot(const client_cache_t & clientCacheId)822 uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
823     std::lock_guard<std::mutex> lock(mMutex);
824     auto itr = mCachedBuffers.find(clientCacheId);
825     if (itr == mCachedBuffers.end()) {
826         return addCachedBuffer(clientCacheId);
827     }
828     auto& [hwcCacheSlot, counter] = itr->second;
829     counter = mCounter++;
830     return hwcCacheSlot;
831 }
832 
addCachedBuffer(const client_cache_t & clientCacheId)833 uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
834         REQUIRES(mMutex) {
835     if (!clientCacheId.isValid()) {
836         ALOGE("invalid process, returning invalid slot");
837         return BufferQueue::INVALID_BUFFER_SLOT;
838     }
839 
840     ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
841 
842     uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
843     mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
844     return hwcCacheSlot;
845 }
846 
getFreeHwcCacheSlot()847 uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
848     if (mFreeHwcCacheSlots.empty()) {
849         evictLeastRecentlyUsed();
850     }
851 
852     uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
853     mFreeHwcCacheSlots.pop();
854     return hwcCacheSlot;
855 }
856 
evictLeastRecentlyUsed()857 void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
858     uint64_t minCounter = UINT_MAX;
859     client_cache_t minClientCacheId = {};
860     for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
861         const auto& [hwcCacheSlot, counter] = slotCounter;
862         if (counter < minCounter) {
863             minCounter = counter;
864             minClientCacheId = clientCacheId;
865         }
866     }
867     eraseBufferLocked(minClientCacheId);
868 
869     ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
870 }
871 
eraseBufferLocked(const client_cache_t & clientCacheId)872 void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
873         REQUIRES(mMutex) {
874     auto itr = mCachedBuffers.find(clientCacheId);
875     if (itr == mCachedBuffers.end()) {
876         return;
877     }
878     auto& [hwcCacheSlot, counter] = itr->second;
879 
880     // TODO send to hwc cache and resources
881 
882     mFreeHwcCacheSlots.push(hwcCacheSlot);
883     mCachedBuffers.erase(clientCacheId);
884 }
885 
gatherBufferInfo()886 void BufferStateLayer::gatherBufferInfo() {
887     BufferLayer::gatherBufferInfo();
888 
889     const State& s(getDrawingState());
890     mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
891     mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
892     mBufferInfo.mFence = s.acquireFence;
893     mBufferInfo.mTransform = s.bufferTransform;
894     auto lastDataspace = mBufferInfo.mDataspace;
895     mBufferInfo.mDataspace = translateDataspace(s.dataspace);
896     if (lastDataspace != mBufferInfo.mDataspace) {
897         mFlinger->mSomeDataspaceChanged = true;
898     }
899     mBufferInfo.mCrop = computeBufferCrop(s);
900     mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
901     mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
902     mBufferInfo.mHdrMetadata = s.hdrMetadata;
903     mBufferInfo.mApi = s.api;
904     mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
905     mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
906 }
907 
getEffectiveScalingMode() const908 uint32_t BufferStateLayer::getEffectiveScalingMode() const {
909    return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
910 }
911 
computeBufferCrop(const State & s)912 Rect BufferStateLayer::computeBufferCrop(const State& s) {
913     if (s.buffer && !s.bufferCrop.isEmpty()) {
914         Rect bufferCrop;
915         s.buffer->getBuffer()->getBounds().intersect(s.bufferCrop, &bufferCrop);
916         return bufferCrop;
917     } else if (s.buffer) {
918         return s.buffer->getBuffer()->getBounds();
919     } else {
920         return s.bufferCrop;
921     }
922 }
923 
createClone()924 sp<Layer> BufferStateLayer::createClone() {
925     LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
926     args.textureName = mTextureName;
927     sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
928     layer->mHwcSlotGenerator = mHwcSlotGenerator;
929     layer->setInitialValuesForClone(this);
930     return layer;
931 }
932 
bufferNeedsFiltering() const933 bool BufferStateLayer::bufferNeedsFiltering() const {
934     const State& s(getDrawingState());
935     if (!s.buffer) {
936         return false;
937     }
938 
939     uint32_t bufferWidth = s.buffer->getBuffer()->width;
940     uint32_t bufferHeight = s.buffer->getBuffer()->height;
941 
942     // Undo any transformations on the buffer and return the result.
943     if (s.bufferTransform & ui::Transform::ROT_90) {
944         std::swap(bufferWidth, bufferHeight);
945     }
946 
947     if (s.transformToDisplayInverse) {
948         uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
949         if (invTransform & ui::Transform::ROT_90) {
950             std::swap(bufferWidth, bufferHeight);
951         }
952     }
953 
954     const Rect layerSize{getBounds()};
955     return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
956 }
957 
decrementPendingBufferCount()958 void BufferStateLayer::decrementPendingBufferCount() {
959     int32_t pendingBuffers = --mPendingBufferTransactions;
960     tracePendingBufferCount(pendingBuffers);
961 }
962 
tracePendingBufferCount(int32_t pendingBuffers)963 void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
964     ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
965 }
966 
967 
968 /*
969  * We don't want to send the layer's transform to input, but rather the
970  * parent's transform. This is because BufferStateLayer's transform is
971  * information about how the buffer is placed on screen. The parent's
972  * transform makes more sense to send since it's information about how the
973  * layer is placed on screen. This transform is used by input to determine
974  * how to go from screen space back to window space.
975  */
getInputTransform() const976 ui::Transform BufferStateLayer::getInputTransform() const {
977     sp<Layer> parent = mDrawingParent.promote();
978     if (parent == nullptr) {
979         return ui::Transform();
980     }
981 
982     return parent->getTransform();
983 }
984 
985 /**
986  * Similar to getInputTransform, we need to update the bounds to include the transform.
987  * This is because bounds for BSL doesn't include buffer transform, where the input assumes
988  * that's already included.
989  */
getInputBounds() const990 Rect BufferStateLayer::getInputBounds() const {
991     Rect bufferBounds = getCroppedBufferSize(getDrawingState());
992     if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
993         return bufferBounds;
994     }
995     return mDrawingState.transform.transform(bufferBounds);
996 }
997 
998 } // namespace android
999 
1000 // TODO(b/129481165): remove the #pragma below and fix conversion issues
1001 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
1002