• 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 
21 //#define LOG_NDEBUG 0
22 #undef LOG_TAG
23 #define LOG_TAG "BufferLayer"
24 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
25 
26 #include "BufferLayer.h"
27 
28 #include <compositionengine/CompositionEngine.h>
29 #include <compositionengine/LayerFECompositionState.h>
30 #include <compositionengine/OutputLayer.h>
31 #include <compositionengine/impl/OutputLayerCompositionState.h>
32 #include <cutils/compiler.h>
33 #include <cutils/native_handle.h>
34 #include <cutils/properties.h>
35 #include <gui/BufferItem.h>
36 #include <gui/BufferQueue.h>
37 #include <gui/GLConsumer.h>
38 #include <gui/LayerDebugInfo.h>
39 #include <gui/Surface.h>
40 #include <renderengine/RenderEngine.h>
41 #include <ui/DebugUtils.h>
42 #include <utils/Errors.h>
43 #include <utils/Log.h>
44 #include <utils/NativeHandle.h>
45 #include <utils/StopWatch.h>
46 #include <utils/Trace.h>
47 
48 #include <cmath>
49 #include <cstdlib>
50 #include <mutex>
51 #include <sstream>
52 
53 #include "Colorizer.h"
54 #include "DisplayDevice.h"
55 #include "FrameTracer/FrameTracer.h"
56 #include "LayerRejecter.h"
57 #include "TimeStats/TimeStats.h"
58 
59 namespace android {
60 
61 static constexpr float defaultMaxLuminance = 1000.0;
62 
BufferLayer(const LayerCreationArgs & args)63 BufferLayer::BufferLayer(const LayerCreationArgs& args)
64       : Layer(args),
65         mTextureName(args.textureName),
66         mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {
67     ALOGV("Creating Layer %s", getDebugName());
68 
69     mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
70 
71     mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
72     mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
73 }
74 
~BufferLayer()75 BufferLayer::~BufferLayer() {
76     if (!isClone()) {
77         // The original layer and the clone layer share the same texture. Therefore, only one of
78         // the layers, in this case the original layer, needs to handle the deletion. The original
79         // layer and the clone should be removed at the same time so there shouldn't be any issue
80         // with the clone layer trying to use the deleted texture.
81         mFlinger->deleteTextureAsync(mTextureName);
82     }
83     const int32_t layerId = getSequence();
84     mFlinger->mTimeStats->onDestroy(layerId);
85     mFlinger->mFrameTracer->onDestroy(layerId);
86 }
87 
useSurfaceDamage()88 void BufferLayer::useSurfaceDamage() {
89     if (mFlinger->mForceFullDamage) {
90         surfaceDamageRegion = Region::INVALID_REGION;
91     } else {
92         surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
93     }
94 }
95 
useEmptyDamage()96 void BufferLayer::useEmptyDamage() {
97     surfaceDamageRegion.clear();
98 }
99 
isOpaque(const Layer::State & s) const100 bool BufferLayer::isOpaque(const Layer::State& s) const {
101     // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
102     // layer's opaque flag.
103     if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
104         return false;
105     }
106 
107     // if the layer has the opaque flag, then we're always opaque,
108     // otherwise we use the current buffer's format.
109     return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
110 }
111 
isVisible() const112 bool BufferLayer::isVisible() const {
113     return !isHiddenByPolicy() && getAlpha() > 0.0f &&
114             (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
115 }
116 
isFixedSize() const117 bool BufferLayer::isFixedSize() const {
118     return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
119 }
120 
usesSourceCrop() const121 bool BufferLayer::usesSourceCrop() const {
122     return true;
123 }
124 
inverseOrientation(uint32_t transform)125 static constexpr mat4 inverseOrientation(uint32_t transform) {
126     const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
127     const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
128     const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
129     mat4 tr;
130 
131     if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
132         tr = tr * rot90;
133     }
134     if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
135         tr = tr * flipH;
136     }
137     if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
138         tr = tr * flipV;
139     }
140     return inverse(tr);
141 }
142 
prepareClientComposition(compositionengine::LayerFE::ClientCompositionTargetSettings & targetSettings)143 std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition(
144         compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
145     ATRACE_CALL();
146 
147     std::optional<compositionengine::LayerFE::LayerSettings> result =
148             Layer::prepareClientComposition(targetSettings);
149     if (!result) {
150         return result;
151     }
152 
153     if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
154         // the texture has not been created yet, this Layer has
155         // in fact never been drawn into. This happens frequently with
156         // SurfaceView because the WindowManager can't know when the client
157         // has drawn the first time.
158 
159         // If there is nothing under us, we paint the screen in black, otherwise
160         // we just skip this update.
161 
162         // figure out if there is something below us
163         Region under;
164         bool finished = false;
165         mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
166             if (finished || layer == static_cast<BufferLayer const*>(this)) {
167                 finished = true;
168                 return;
169             }
170 
171             under.orSelf(layer->getScreenBounds());
172         });
173         // if not everything below us is covered, we plug the holes!
174         Region holes(targetSettings.clip.subtract(under));
175         if (!holes.isEmpty()) {
176             targetSettings.clearRegion.orSelf(holes);
177         }
178 
179         if (mSidebandStream != nullptr) {
180             // For surfaceview of tv sideband, there is no activeBuffer
181             // in bufferqueue, we need return LayerSettings.
182             return result;
183         } else {
184             return std::nullopt;
185         }
186     }
187     const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
188             ((isSecure() || isProtected()) && !targetSettings.isSecure);
189     const bool bufferCanBeUsedAsHwTexture =
190             mBufferInfo.mBuffer->getBuffer()->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
191     compositionengine::LayerFE::LayerSettings& layer = *result;
192     if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
193         ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
194                  mName.c_str());
195         prepareClearClientComposition(layer, true /* blackout */);
196         return layer;
197     }
198 
199     const State& s(getDrawingState());
200     layer.source.buffer.buffer = mBufferInfo.mBuffer;
201     layer.source.buffer.isOpaque = isOpaque(s);
202     layer.source.buffer.fence = mBufferInfo.mFence;
203     layer.source.buffer.textureName = mTextureName;
204     layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
205     layer.source.buffer.isY410BT2020 = isHdrY410();
206     bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
207     bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
208     float maxLuminance = 0.f;
209     if (hasSmpte2086 && hasCta861_3) {
210         maxLuminance = std::min(mBufferInfo.mHdrMetadata.smpte2086.maxLuminance,
211                                 mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel);
212     } else if (hasSmpte2086) {
213         maxLuminance = mBufferInfo.mHdrMetadata.smpte2086.maxLuminance;
214     } else if (hasCta861_3) {
215         maxLuminance = mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel;
216     } else {
217         switch (layer.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) {
218             case HAL_DATASPACE_TRANSFER_ST2084:
219             case HAL_DATASPACE_TRANSFER_HLG:
220                 // Behavior-match previous releases for HDR content
221                 maxLuminance = defaultMaxLuminance;
222                 break;
223         }
224     }
225     layer.source.buffer.maxLuminanceNits = maxLuminance;
226     layer.frameNumber = mCurrentFrameNumber;
227     layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()->getId() : 0;
228 
229     const bool useFiltering =
230             targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
231 
232     // Query the texture matrix given our current filtering mode.
233     float textureMatrix[16];
234     getDrawingTransformMatrix(useFiltering, textureMatrix);
235 
236     if (getTransformToDisplayInverse()) {
237         /*
238          * the code below applies the primary display's inverse transform to
239          * the texture transform
240          */
241         uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
242         mat4 tr = inverseOrientation(transform);
243 
244         /**
245          * TODO(b/36727915): This is basically a hack.
246          *
247          * Ensure that regardless of the parent transformation,
248          * this buffer is always transformed from native display
249          * orientation to display orientation. For example, in the case
250          * of a camera where the buffer remains in native orientation,
251          * we want the pixels to always be upright.
252          */
253         sp<Layer> p = mDrawingParent.promote();
254         if (p != nullptr) {
255             const auto parentTransform = p->getTransform();
256             tr = tr * inverseOrientation(parentTransform.getOrientation());
257         }
258 
259         // and finally apply it to the original texture matrix
260         const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
261         memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
262     }
263 
264     const Rect win{getBounds()};
265     float bufferWidth = getBufferSize(s).getWidth();
266     float bufferHeight = getBufferSize(s).getHeight();
267 
268     // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
269     // been set and there is no parent layer bounds. In that case, the scale is meaningless so
270     // ignore them.
271     if (!getBufferSize(s).isValid()) {
272         bufferWidth = float(win.right) - float(win.left);
273         bufferHeight = float(win.bottom) - float(win.top);
274     }
275 
276     const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
277     const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
278     const float translateY = float(win.top) / bufferHeight;
279     const float translateX = float(win.left) / bufferWidth;
280 
281     // Flip y-coordinates because GLConsumer expects OpenGL convention.
282     mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
283             mat4::translate(vec4(-.5, -.5, 0, 1)) *
284             mat4::translate(vec4(translateX, translateY, 0, 1)) *
285             mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
286 
287     layer.source.buffer.useTextureFiltering = useFiltering;
288     layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
289 
290     return layer;
291 }
292 
isHdrY410() const293 bool BufferLayer::isHdrY410() const {
294     // pixel format is HDR Y410 masquerading as RGBA_1010102
295     return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
296             mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
297             mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
298 }
299 
getCompositionEngineLayerFE() const300 sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
301     return asLayerFE();
302 }
303 
editCompositionState()304 compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
305     return mCompositionState.get();
306 }
307 
getCompositionState() const308 const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
309     return mCompositionState.get();
310 }
311 
preparePerFrameCompositionState()312 void BufferLayer::preparePerFrameCompositionState() {
313     Layer::preparePerFrameCompositionState();
314 
315     // Sideband layers
316     auto* compositionState = editCompositionState();
317     if (compositionState->sidebandStream.get()) {
318         compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
319         return;
320     } else {
321         // Normal buffer layers
322         compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
323         compositionState->compositionType = mPotentialCursor
324                 ? Hwc2::IComposerClient::Composition::CURSOR
325                 : Hwc2::IComposerClient::Composition::DEVICE;
326     }
327 
328     compositionState->buffer = mBufferInfo.mBuffer->getBuffer();
329     compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
330             ? 0
331             : mBufferInfo.mBufferSlot;
332     compositionState->acquireFence = mBufferInfo.mFence;
333 }
334 
onPreComposition(nsecs_t refreshStartTime)335 bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
336     if (mBufferInfo.mBuffer != nullptr) {
337         Mutex::Autolock lock(mFrameEventHistoryMutex);
338         mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
339     }
340     mRefreshPending = false;
341     return hasReadyFrame();
342 }
343 namespace {
frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate)344 TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
345     using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
346     using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
347     const auto frameRateCompatibility = [frameRate] {
348         switch (frameRate.type) {
349             case Layer::FrameRateCompatibility::Default:
350                 return FrameRateCompatibility::Default;
351             case Layer::FrameRateCompatibility::ExactOrMultiple:
352                 return FrameRateCompatibility::ExactOrMultiple;
353             default:
354                 return FrameRateCompatibility::Undefined;
355         }
356     }();
357 
358     const auto seamlessness = [frameRate] {
359         switch (frameRate.seamlessness) {
360             case scheduler::Seamlessness::OnlySeamless:
361                 return Seamlessness::ShouldBeSeamless;
362             case scheduler::Seamlessness::SeamedAndSeamless:
363                 return Seamlessness::NotRequired;
364             default:
365                 return Seamlessness::Undefined;
366         }
367     }();
368 
369     return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
370                                        .frameRateCompatibility = frameRateCompatibility,
371                                        .seamlessness = seamlessness};
372 }
373 } // namespace
374 
onPostComposition(const DisplayDevice * display,const std::shared_ptr<FenceTime> & glDoneFence,const std::shared_ptr<FenceTime> & presentFence,const CompositorTiming & compositorTiming)375 bool BufferLayer::onPostComposition(const DisplayDevice* display,
376                                     const std::shared_ptr<FenceTime>& glDoneFence,
377                                     const std::shared_ptr<FenceTime>& presentFence,
378                                     const CompositorTiming& compositorTiming) {
379     // mFrameLatencyNeeded is true when a new frame was latched for the
380     // composition.
381     if (!mBufferInfo.mFrameLatencyNeeded) return false;
382 
383     // Update mFrameEventHistory.
384     {
385         Mutex::Autolock lock(mFrameEventHistoryMutex);
386         mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
387                                               compositorTiming);
388         finalizeFrameEventHistory(glDoneFence, compositorTiming);
389     }
390 
391     // Update mFrameTracker.
392     nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
393     mFrameTracker.setDesiredPresentTime(desiredPresentTime);
394 
395     const int32_t layerId = getSequence();
396     mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
397 
398     const auto outputLayer = findOutputLayerForDisplay(display);
399     if (outputLayer && outputLayer->requiresClientComposition()) {
400         nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
401         mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
402                                                clientCompositionTimestamp,
403                                                FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
404         // Update the SurfaceFrames in the drawing state
405         if (mDrawingState.bufferSurfaceFrameTX) {
406             mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
407         }
408         for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
409             surfaceFrame->setGpuComposition();
410         }
411     }
412 
413     std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
414     if (frameReadyFence->isValid()) {
415         mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
416     } else {
417         // There was no fence for this frame, so assume that it was ready
418         // to be presented at the desired present time.
419         mFrameTracker.setFrameReadyTime(desiredPresentTime);
420     }
421 
422     const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
423     const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
424     if (presentFence->isValid()) {
425         mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
426                                               refreshRate, renderRate,
427                                               frameRateToSetFrameRateVotePayload(
428                                                       mDrawingState.frameRate),
429                                               getGameMode());
430         mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
431                                            presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
432         mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
433     } else if (!display) {
434         // Do nothing.
435     } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
436                displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
437         // The HWC doesn't support present fences, so use the refresh
438         // timestamp instead.
439         const nsecs_t actualPresentTime = display->getRefreshTimestamp();
440         mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
441                                              refreshRate, renderRate,
442                                              frameRateToSetFrameRateVotePayload(
443                                                      mDrawingState.frameRate),
444                                              getGameMode());
445         mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
446                                                actualPresentTime,
447                                                FrameTracer::FrameEvent::PRESENT_FENCE);
448         mFrameTracker.setActualPresentTime(actualPresentTime);
449     }
450 
451     mFrameTracker.advanceFrame();
452     mBufferInfo.mFrameLatencyNeeded = false;
453     return true;
454 }
455 
gatherBufferInfo()456 void BufferLayer::gatherBufferInfo() {
457     mBufferInfo.mPixelFormat =
458             !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getBuffer()->format;
459     mBufferInfo.mFrameLatencyNeeded = true;
460 }
461 
shouldPresentNow(nsecs_t expectedPresentTime) const462 bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
463     // If this is not a valid vsync for the layer's uid, return and try again later
464     const bool isVsyncValidForUid =
465             mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
466     if (!isVsyncValidForUid) {
467         ATRACE_NAME("!isVsyncValidForUid");
468         return false;
469     }
470 
471     // AutoRefresh layers and sideband streams should always be presented
472     if (getSidebandStreamChanged() || getAutoRefresh()) {
473         return true;
474     }
475 
476     // If this layer doesn't have a frame is shouldn't be presented
477     if (!hasFrameUpdate()) {
478         return false;
479     }
480 
481     // Defer to the derived class to decide whether the next buffer is due for
482     // presentation.
483     return isBufferDue(expectedPresentTime);
484 }
485 
latchBuffer(bool & recomputeVisibleRegions,nsecs_t latchTime,nsecs_t expectedPresentTime)486 bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
487                               nsecs_t expectedPresentTime) {
488     ATRACE_CALL();
489 
490     bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
491 
492     if (refreshRequired) {
493         return refreshRequired;
494     }
495 
496     if (!hasReadyFrame()) {
497         return false;
498     }
499 
500     // if we've already called updateTexImage() without going through
501     // a composition step, we have to skip this layer at this point
502     // because we cannot call updateTeximage() without a corresponding
503     // compositionComplete() call.
504     // we'll trigger an update in onPreComposition().
505     if (mRefreshPending) {
506         return false;
507     }
508 
509     // If the head buffer's acquire fence hasn't signaled yet, return and
510     // try again later
511     if (!fenceHasSignaled()) {
512         ATRACE_NAME("!fenceHasSignaled()");
513         mFlinger->signalLayerUpdate();
514         return false;
515     }
516 
517     // Capture the old state of the layer for comparisons later
518     const State& s(getDrawingState());
519     const bool oldOpacity = isOpaque(s);
520 
521     BufferInfo oldBufferInfo = mBufferInfo;
522 
523     status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
524     if (err != NO_ERROR) {
525         return false;
526     }
527 
528     err = updateActiveBuffer();
529     if (err != NO_ERROR) {
530         return false;
531     }
532 
533     err = updateFrameNumber(latchTime);
534     if (err != NO_ERROR) {
535         return false;
536     }
537 
538     gatherBufferInfo();
539 
540     mRefreshPending = true;
541     if (oldBufferInfo.mBuffer == nullptr) {
542         // the first time we receive a buffer, we need to trigger a
543         // geometry invalidation.
544         recomputeVisibleRegions = true;
545     }
546 
547     if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
548         (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
549         (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
550         (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
551         recomputeVisibleRegions = true;
552     }
553 
554     if (oldBufferInfo.mBuffer != nullptr) {
555         uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
556         uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
557         if (bufWidth != uint32_t(oldBufferInfo.mBuffer->getBuffer()->width) ||
558             bufHeight != uint32_t(oldBufferInfo.mBuffer->getBuffer()->height)) {
559             recomputeVisibleRegions = true;
560         }
561     }
562 
563     if (oldOpacity != isOpaque(s)) {
564         recomputeVisibleRegions = true;
565     }
566 
567     return true;
568 }
569 
hasReadyFrame() const570 bool BufferLayer::hasReadyFrame() const {
571     return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
572 }
573 
getEffectiveScalingMode() const574 uint32_t BufferLayer::getEffectiveScalingMode() const {
575     return mBufferInfo.mScaleMode;
576 }
577 
isProtected() const578 bool BufferLayer::isProtected() const {
579     return (mBufferInfo.mBuffer != nullptr) &&
580             (mBufferInfo.mBuffer->getBuffer()->getUsage() & GRALLOC_USAGE_PROTECTED);
581 }
582 
583 // As documented in libhardware header, formats in the range
584 // 0x100 - 0x1FF are specific to the HAL implementation, and
585 // are known to have no alpha channel
586 // TODO: move definition for device-specific range into
587 // hardware.h, instead of using hard-coded values here.
588 #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
589 
getOpacityForFormat(uint32_t format)590 bool BufferLayer::getOpacityForFormat(uint32_t format) {
591     if (HARDWARE_IS_DEVICE_FORMAT(format)) {
592         return true;
593     }
594     switch (format) {
595         case HAL_PIXEL_FORMAT_RGBA_8888:
596         case HAL_PIXEL_FORMAT_BGRA_8888:
597         case HAL_PIXEL_FORMAT_RGBA_FP16:
598         case HAL_PIXEL_FORMAT_RGBA_1010102:
599             return false;
600     }
601     // in all other case, we have no blending (also for unknown formats)
602     return true;
603 }
604 
needsFiltering(const DisplayDevice * display) const605 bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
606     const auto outputLayer = findOutputLayerForDisplay(display);
607     if (outputLayer == nullptr) {
608         return false;
609     }
610 
611     // We need filtering if the sourceCrop rectangle size does not match the
612     // displayframe rectangle size (not a 1:1 render)
613     const auto& compositionState = outputLayer->getState();
614     const auto displayFrame = compositionState.displayFrame;
615     const auto sourceCrop = compositionState.sourceCrop;
616     return sourceCrop.getHeight() != displayFrame.getHeight() ||
617             sourceCrop.getWidth() != displayFrame.getWidth();
618 }
619 
needsFilteringForScreenshots(const DisplayDevice * display,const ui::Transform & inverseParentTransform) const620 bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
621                                                const ui::Transform& inverseParentTransform) const {
622     const auto outputLayer = findOutputLayerForDisplay(display);
623     if (outputLayer == nullptr) {
624         return false;
625     }
626 
627     // We need filtering if the sourceCrop rectangle size does not match the
628     // viewport rectangle size (not a 1:1 render)
629     const auto& compositionState = outputLayer->getState();
630     const ui::Transform& displayTransform = display->getTransform();
631     const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
632     // Undo the transformation of the displayFrame so that we're back into
633     // layer-stack space.
634     const Rect frame = inverseTransform.transform(compositionState.displayFrame);
635     const FloatRect sourceCrop = compositionState.sourceCrop;
636 
637     int32_t frameHeight = frame.getHeight();
638     int32_t frameWidth = frame.getWidth();
639     // If the display transform had a rotational component then undo the
640     // rotation so that the orientation matches the source crop.
641     if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
642         std::swap(frameHeight, frameWidth);
643     }
644     return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
645 }
646 
getHeadFrameNumber(nsecs_t expectedPresentTime) const647 uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
648     if (hasFrameUpdate()) {
649         return getFrameNumber(expectedPresentTime);
650     } else {
651         return mCurrentFrameNumber;
652     }
653 }
654 
getBufferSize(const State & s) const655 Rect BufferLayer::getBufferSize(const State& s) const {
656     // If we have a sideband stream, or we are scaling the buffer then return the layer size since
657     // we cannot determine the buffer size.
658     if ((s.sidebandStream != nullptr) ||
659         (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
660         return Rect(getActiveWidth(s), getActiveHeight(s));
661     }
662 
663     if (mBufferInfo.mBuffer == nullptr) {
664         return Rect::INVALID_RECT;
665     }
666 
667     uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
668     uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
669 
670     // Undo any transformations on the buffer and return the result.
671     if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
672         std::swap(bufWidth, bufHeight);
673     }
674 
675     if (getTransformToDisplayInverse()) {
676         uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
677         if (invTransform & ui::Transform::ROT_90) {
678             std::swap(bufWidth, bufHeight);
679         }
680     }
681 
682     return Rect(bufWidth, bufHeight);
683 }
684 
computeSourceBounds(const FloatRect & parentBounds) const685 FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
686     const State& s(getDrawingState());
687 
688     // If we have a sideband stream, or we are scaling the buffer then return the layer size since
689     // we cannot determine the buffer size.
690     if ((s.sidebandStream != nullptr) ||
691         (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
692         return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
693     }
694 
695     if (mBufferInfo.mBuffer == nullptr) {
696         return parentBounds;
697     }
698 
699     uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
700     uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
701 
702     // Undo any transformations on the buffer and return the result.
703     if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
704         std::swap(bufWidth, bufHeight);
705     }
706 
707     if (getTransformToDisplayInverse()) {
708         uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
709         if (invTransform & ui::Transform::ROT_90) {
710             std::swap(bufWidth, bufHeight);
711         }
712     }
713 
714     return FloatRect(0, 0, bufWidth, bufHeight);
715 }
716 
latchAndReleaseBuffer()717 void BufferLayer::latchAndReleaseBuffer() {
718     mRefreshPending = false;
719     if (hasReadyFrame()) {
720         bool ignored = false;
721         latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
722     }
723     releasePendingBuffer(systemTime());
724 }
725 
getPixelFormat() const726 PixelFormat BufferLayer::getPixelFormat() const {
727     return mBufferInfo.mPixelFormat;
728 }
729 
getTransformToDisplayInverse() const730 bool BufferLayer::getTransformToDisplayInverse() const {
731     return mBufferInfo.mTransformToDisplayInverse;
732 }
733 
getBufferCrop() const734 Rect BufferLayer::getBufferCrop() const {
735     // this is the crop rectangle that applies to the buffer
736     // itself (as opposed to the window)
737     if (!mBufferInfo.mCrop.isEmpty()) {
738         // if the buffer crop is defined, we use that
739         return mBufferInfo.mCrop;
740     } else if (mBufferInfo.mBuffer != nullptr) {
741         // otherwise we use the whole buffer
742         return mBufferInfo.mBuffer->getBuffer()->getBounds();
743     } else {
744         // if we don't have a buffer yet, we use an empty/invalid crop
745         return Rect();
746     }
747 }
748 
getBufferTransform() const749 uint32_t BufferLayer::getBufferTransform() const {
750     return mBufferInfo.mTransform;
751 }
752 
getDataSpace() const753 ui::Dataspace BufferLayer::getDataSpace() const {
754     return mBufferInfo.mDataspace;
755 }
756 
translateDataspace(ui::Dataspace dataspace)757 ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
758     ui::Dataspace updatedDataspace = dataspace;
759     // translate legacy dataspaces to modern dataspaces
760     switch (dataspace) {
761         case ui::Dataspace::SRGB:
762             updatedDataspace = ui::Dataspace::V0_SRGB;
763             break;
764         case ui::Dataspace::SRGB_LINEAR:
765             updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
766             break;
767         case ui::Dataspace::JFIF:
768             updatedDataspace = ui::Dataspace::V0_JFIF;
769             break;
770         case ui::Dataspace::BT601_625:
771             updatedDataspace = ui::Dataspace::V0_BT601_625;
772             break;
773         case ui::Dataspace::BT601_525:
774             updatedDataspace = ui::Dataspace::V0_BT601_525;
775             break;
776         case ui::Dataspace::BT709:
777             updatedDataspace = ui::Dataspace::V0_BT709;
778             break;
779         default:
780             break;
781     }
782 
783     return updatedDataspace;
784 }
785 
getBuffer() const786 sp<GraphicBuffer> BufferLayer::getBuffer() const {
787     return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
788 }
789 
getDrawingTransformMatrix(bool filteringEnabled,float outMatrix[16])790 void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
791     GLConsumer::computeTransformMatrix(outMatrix,
792                                        mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()
793                                                            : nullptr,
794                                        mBufferInfo.mCrop, mBufferInfo.mTransform, filteringEnabled);
795 }
796 
setInitialValuesForClone(const sp<Layer> & clonedFrom)797 void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
798     Layer::setInitialValuesForClone(clonedFrom);
799 
800     sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
801     mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
802     mPotentialCursor = bufferClonedFrom->mPotentialCursor;
803     mProtectedByApp = bufferClonedFrom->mProtectedByApp;
804 
805     updateCloneBufferInfo();
806 }
807 
updateCloneBufferInfo()808 void BufferLayer::updateCloneBufferInfo() {
809     if (!isClone() || !isClonedFromAlive()) {
810         return;
811     }
812 
813     sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
814     mBufferInfo = clonedFrom->mBufferInfo;
815     mSidebandStream = clonedFrom->mSidebandStream;
816     surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
817     mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
818     mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
819 
820     // After buffer info is updated, the drawingState from the real layer needs to be copied into
821     // the cloned. This is because some properties of drawingState can change when latchBuffer is
822     // called. However, copying the drawingState would also overwrite the cloned layer's relatives
823     // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
824     // the cloned drawingState again.
825     wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
826     SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
827     wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
828     InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
829 
830     mDrawingState = clonedFrom->mDrawingState;
831 
832     mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
833     mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
834     mDrawingState.zOrderRelatives = tmpZOrderRelatives;
835     mDrawingState.inputInfo = tmpInputInfo;
836 }
837 
setTransformHint(ui::Transform::RotationFlags displayTransformHint)838 void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
839     mTransformHint = getFixedTransformHint();
840     if (mTransformHint == ui::Transform::ROT_INVALID) {
841         mTransformHint = displayTransformHint;
842     }
843 }
844 
bufferNeedsFiltering() const845 bool BufferLayer::bufferNeedsFiltering() const {
846     return isFixedSize();
847 }
848 
849 } // namespace android
850 
851 #if defined(__gl_h_)
852 #error "don't include gl/gl.h in this file"
853 #endif
854 
855 #if defined(__gl2_h_)
856 #error "don't include gl2/gl2.h in this file"
857 #endif
858 
859 // TODO(b/129481165): remove the #pragma below and fix conversion issues
860 #pragma clang diagnostic pop // ignored "-Wconversion"
861