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 defaultMaxMasteringLuminance = 1000.0;
62 static constexpr float defaultMaxContentLuminance = 1000.0;
63 
BufferLayer(const LayerCreationArgs & args)64 BufferLayer::BufferLayer(const LayerCreationArgs& args)
65       : Layer(args),
66         mTextureName(args.textureName),
67         mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {
68     ALOGV("Creating Layer %s", getDebugName());
69 
70     mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
71 
72     mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
73     mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
74 }
75 
~BufferLayer()76 BufferLayer::~BufferLayer() {
77     if (!isClone()) {
78         // The original layer and the clone layer share the same texture. Therefore, only one of
79         // the layers, in this case the original layer, needs to handle the deletion. The original
80         // layer and the clone should be removed at the same time so there shouldn't be any issue
81         // with the clone layer trying to use the deleted texture.
82         mFlinger->deleteTextureAsync(mTextureName);
83     }
84     const int32_t layerId = getSequence();
85     mFlinger->mTimeStats->onDestroy(layerId);
86     mFlinger->mFrameTracer->onDestroy(layerId);
87 }
88 
useSurfaceDamage()89 void BufferLayer::useSurfaceDamage() {
90     if (mFlinger->mForceFullDamage) {
91         surfaceDamageRegion = Region::INVALID_REGION;
92     } else {
93         surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
94     }
95 }
96 
useEmptyDamage()97 void BufferLayer::useEmptyDamage() {
98     surfaceDamageRegion.clear();
99 }
100 
isOpaque(const Layer::State & s) const101 bool BufferLayer::isOpaque(const Layer::State& s) const {
102     // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
103     // layer's opaque flag.
104     if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
105         return false;
106     }
107 
108     // if the layer has the opaque flag, then we're always opaque,
109     // otherwise we use the current buffer's format.
110     return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
111 }
112 
isVisible() const113 bool BufferLayer::isVisible() const {
114     return !isHiddenByPolicy() && getAlpha() > 0.0f &&
115             (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
116 }
117 
isFixedSize() const118 bool BufferLayer::isFixedSize() const {
119     return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
120 }
121 
usesSourceCrop() const122 bool BufferLayer::usesSourceCrop() const {
123     return true;
124 }
125 
inverseOrientation(uint32_t transform)126 static constexpr mat4 inverseOrientation(uint32_t transform) {
127     const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
128     const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
129     const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
130     mat4 tr;
131 
132     if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
133         tr = tr * rot90;
134     }
135     if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
136         tr = tr * flipH;
137     }
138     if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
139         tr = tr * flipV;
140     }
141     return inverse(tr);
142 }
143 
prepareClientComposition(compositionengine::LayerFE::ClientCompositionTargetSettings & targetSettings)144 std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition(
145         compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
146     ATRACE_CALL();
147 
148     std::optional<compositionengine::LayerFE::LayerSettings> result =
149             Layer::prepareClientComposition(targetSettings);
150     if (!result) {
151         return result;
152     }
153 
154     if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
155         // the texture has not been created yet, this Layer has
156         // in fact never been drawn into. This happens frequently with
157         // SurfaceView because the WindowManager can't know when the client
158         // has drawn the first time.
159 
160         // If there is nothing under us, we paint the screen in black, otherwise
161         // we just skip this update.
162 
163         // figure out if there is something below us
164         Region under;
165         bool finished = false;
166         mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
167             if (finished || layer == static_cast<BufferLayer const*>(this)) {
168                 finished = true;
169                 return;
170             }
171 
172             under.orSelf(layer->getScreenBounds());
173         });
174         // if not everything below us is covered, we plug the holes!
175         Region holes(targetSettings.clip.subtract(under));
176         if (!holes.isEmpty()) {
177             targetSettings.clearRegion.orSelf(holes);
178         }
179         return std::nullopt;
180     }
181     bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
182             (isSecure() && !targetSettings.isSecure);
183     compositionengine::LayerFE::LayerSettings& layer = *result;
184     if (blackOutLayer) {
185         prepareClearClientComposition(layer, true /* blackout */);
186         return layer;
187     }
188 
189     const State& s(getDrawingState());
190     layer.source.buffer.buffer = mBufferInfo.mBuffer;
191     layer.source.buffer.isOpaque = isOpaque(s);
192     layer.source.buffer.fence = mBufferInfo.mFence;
193     layer.source.buffer.textureName = mTextureName;
194     layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
195     layer.source.buffer.isY410BT2020 = isHdrY410();
196     bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
197     bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
198     layer.source.buffer.maxMasteringLuminance = hasSmpte2086
199             ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance
200             : defaultMaxMasteringLuminance;
201     layer.source.buffer.maxContentLuminance = hasCta861_3
202             ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel
203             : defaultMaxContentLuminance;
204     layer.frameNumber = mCurrentFrameNumber;
205     layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
206 
207     // TODO: we could be more subtle with isFixedSize()
208     const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
209 
210     // Query the texture matrix given our current filtering mode.
211     float textureMatrix[16];
212     getDrawingTransformMatrix(useFiltering, textureMatrix);
213 
214     if (getTransformToDisplayInverse()) {
215         /*
216          * the code below applies the primary display's inverse transform to
217          * the texture transform
218          */
219         uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
220         mat4 tr = inverseOrientation(transform);
221 
222         /**
223          * TODO(b/36727915): This is basically a hack.
224          *
225          * Ensure that regardless of the parent transformation,
226          * this buffer is always transformed from native display
227          * orientation to display orientation. For example, in the case
228          * of a camera where the buffer remains in native orientation,
229          * we want the pixels to always be upright.
230          */
231         sp<Layer> p = mDrawingParent.promote();
232         if (p != nullptr) {
233             const auto parentTransform = p->getTransform();
234             tr = tr * inverseOrientation(parentTransform.getOrientation());
235         }
236 
237         // and finally apply it to the original texture matrix
238         const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
239         memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
240     }
241 
242     const Rect win{getBounds()};
243     float bufferWidth = getBufferSize(s).getWidth();
244     float bufferHeight = getBufferSize(s).getHeight();
245 
246     // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
247     // been set and there is no parent layer bounds. In that case, the scale is meaningless so
248     // ignore them.
249     if (!getBufferSize(s).isValid()) {
250         bufferWidth = float(win.right) - float(win.left);
251         bufferHeight = float(win.bottom) - float(win.top);
252     }
253 
254     const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
255     const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
256     const float translateY = float(win.top) / bufferHeight;
257     const float translateX = float(win.left) / bufferWidth;
258 
259     // Flip y-coordinates because GLConsumer expects OpenGL convention.
260     mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
261             mat4::translate(vec4(-.5, -.5, 0, 1)) *
262             mat4::translate(vec4(translateX, translateY, 0, 1)) *
263             mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
264 
265     layer.source.buffer.useTextureFiltering = useFiltering;
266     layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
267 
268     return layer;
269 }
270 
isHdrY410() const271 bool BufferLayer::isHdrY410() const {
272     // pixel format is HDR Y410 masquerading as RGBA_1010102
273     return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
274             mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
275             mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
276 }
277 
getCompositionEngineLayerFE() const278 sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
279     return asLayerFE();
280 }
281 
editCompositionState()282 compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
283     return mCompositionState.get();
284 }
285 
getCompositionState() const286 const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
287     return mCompositionState.get();
288 }
289 
preparePerFrameCompositionState()290 void BufferLayer::preparePerFrameCompositionState() {
291     Layer::preparePerFrameCompositionState();
292 
293     // Sideband layers
294     auto* compositionState = editCompositionState();
295     if (compositionState->sidebandStream.get()) {
296         compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
297         return;
298     } else {
299         // Normal buffer layers
300         compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
301         compositionState->compositionType = mPotentialCursor
302                 ? Hwc2::IComposerClient::Composition::CURSOR
303                 : Hwc2::IComposerClient::Composition::DEVICE;
304     }
305 
306     compositionState->buffer = mBufferInfo.mBuffer;
307     compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
308             ? 0
309             : mBufferInfo.mBufferSlot;
310     compositionState->acquireFence = mBufferInfo.mFence;
311 }
312 
onPreComposition(nsecs_t refreshStartTime)313 bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
314     if (mBufferInfo.mBuffer != nullptr) {
315         Mutex::Autolock lock(mFrameEventHistoryMutex);
316         mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
317     }
318     mRefreshPending = false;
319     return hasReadyFrame();
320 }
321 
onPostComposition(const DisplayDevice * display,const std::shared_ptr<FenceTime> & glDoneFence,const std::shared_ptr<FenceTime> & presentFence,const CompositorTiming & compositorTiming)322 bool BufferLayer::onPostComposition(const DisplayDevice* display,
323                                     const std::shared_ptr<FenceTime>& glDoneFence,
324                                     const std::shared_ptr<FenceTime>& presentFence,
325                                     const CompositorTiming& compositorTiming) {
326     // mFrameLatencyNeeded is true when a new frame was latched for the
327     // composition.
328     if (!mBufferInfo.mFrameLatencyNeeded) return false;
329 
330     // Update mFrameEventHistory.
331     {
332         Mutex::Autolock lock(mFrameEventHistoryMutex);
333         mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
334                                               compositorTiming);
335         finalizeFrameEventHistory(glDoneFence, compositorTiming);
336     }
337 
338     // Update mFrameTracker.
339     nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
340     mFrameTracker.setDesiredPresentTime(desiredPresentTime);
341 
342     const int32_t layerId = getSequence();
343     mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
344 
345     const auto outputLayer = findOutputLayerForDisplay(display);
346     if (outputLayer && outputLayer->requiresClientComposition()) {
347         nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
348         mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
349                                                clientCompositionTimestamp,
350                                                FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
351     }
352 
353     std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
354     if (frameReadyFence->isValid()) {
355         mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
356     } else {
357         // There was no fence for this frame, so assume that it was ready
358         // to be presented at the desired present time.
359         mFrameTracker.setFrameReadyTime(desiredPresentTime);
360     }
361 
362     if (presentFence->isValid()) {
363         mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence);
364         mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
365                                            presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
366         mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
367     } else if (!display) {
368         // Do nothing.
369     } else if (const auto displayId = display->getId();
370                displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
371         // The HWC doesn't support present fences, so use the refresh
372         // timestamp instead.
373         const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
374         mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime);
375         mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
376                                                actualPresentTime,
377                                                FrameTracer::FrameEvent::PRESENT_FENCE);
378         mFrameTracker.setActualPresentTime(actualPresentTime);
379     }
380 
381     mFrameTracker.advanceFrame();
382     mBufferInfo.mFrameLatencyNeeded = false;
383     return true;
384 }
385 
gatherBufferInfo()386 void BufferLayer::gatherBufferInfo() {
387     mBufferInfo.mPixelFormat =
388             !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
389     mBufferInfo.mFrameLatencyNeeded = true;
390 }
391 
latchBuffer(bool & recomputeVisibleRegions,nsecs_t latchTime,nsecs_t expectedPresentTime)392 bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
393                               nsecs_t expectedPresentTime) {
394     ATRACE_CALL();
395 
396     bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
397 
398     if (refreshRequired) {
399         return refreshRequired;
400     }
401 
402     if (!hasReadyFrame()) {
403         return false;
404     }
405 
406     // if we've already called updateTexImage() without going through
407     // a composition step, we have to skip this layer at this point
408     // because we cannot call updateTeximage() without a corresponding
409     // compositionComplete() call.
410     // we'll trigger an update in onPreComposition().
411     if (mRefreshPending) {
412         return false;
413     }
414 
415     // If the head buffer's acquire fence hasn't signaled yet, return and
416     // try again later
417     if (!fenceHasSignaled()) {
418         ATRACE_NAME("!fenceHasSignaled()");
419         mFlinger->signalLayerUpdate();
420         return false;
421     }
422 
423     // Capture the old state of the layer for comparisons later
424     const State& s(getDrawingState());
425     const bool oldOpacity = isOpaque(s);
426 
427     BufferInfo oldBufferInfo = mBufferInfo;
428 
429     if (!allTransactionsSignaled(expectedPresentTime)) {
430         mFlinger->setTransactionFlags(eTraversalNeeded);
431         return false;
432     }
433 
434     status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
435     if (err != NO_ERROR) {
436         return false;
437     }
438 
439     err = updateActiveBuffer();
440     if (err != NO_ERROR) {
441         return false;
442     }
443 
444     err = updateFrameNumber(latchTime);
445     if (err != NO_ERROR) {
446         return false;
447     }
448 
449     gatherBufferInfo();
450 
451     mRefreshPending = true;
452     if (oldBufferInfo.mBuffer == nullptr) {
453         // the first time we receive a buffer, we need to trigger a
454         // geometry invalidation.
455         recomputeVisibleRegions = true;
456     }
457 
458     if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
459         (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
460         (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
461         (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
462         recomputeVisibleRegions = true;
463     }
464 
465     if (oldBufferInfo.mBuffer != nullptr) {
466         uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
467         uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
468         if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
469             bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
470             recomputeVisibleRegions = true;
471         }
472     }
473 
474     if (oldOpacity != isOpaque(s)) {
475         recomputeVisibleRegions = true;
476     }
477 
478     // Remove any sync points corresponding to the buffer which was just
479     // latched
480     {
481         Mutex::Autolock lock(mLocalSyncPointMutex);
482         auto point = mLocalSyncPoints.begin();
483         while (point != mLocalSyncPoints.end()) {
484             if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
485                 // This sync point must have been added since we started
486                 // latching. Don't drop it yet.
487                 ++point;
488                 continue;
489             }
490 
491             if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
492                 std::stringstream ss;
493                 ss << "Dropping sync point " << (*point)->getFrameNumber();
494                 ATRACE_NAME(ss.str().c_str());
495                 point = mLocalSyncPoints.erase(point);
496             } else {
497                 ++point;
498             }
499         }
500     }
501 
502     return true;
503 }
504 
505 // transaction
notifyAvailableFrames(nsecs_t expectedPresentTime)506 void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
507     const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
508     const bool headFenceSignaled = fenceHasSignaled();
509     const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
510     Mutex::Autolock lock(mLocalSyncPointMutex);
511     for (auto& point : mLocalSyncPoints) {
512         if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
513             presentTimeIsCurrent) {
514             point->setFrameAvailable();
515             sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
516             if (requestedSyncLayer) {
517                 // Need to update the transaction flag to ensure the layer's pending transaction
518                 // gets applied.
519                 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
520             }
521         }
522     }
523 }
524 
hasReadyFrame() const525 bool BufferLayer::hasReadyFrame() const {
526     return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
527 }
528 
getEffectiveScalingMode() const529 uint32_t BufferLayer::getEffectiveScalingMode() const {
530     if (mOverrideScalingMode >= 0) {
531         return mOverrideScalingMode;
532     }
533 
534     return mBufferInfo.mScaleMode;
535 }
536 
isProtected() const537 bool BufferLayer::isProtected() const {
538     const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
539     return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
540 }
541 
latchUnsignaledBuffers()542 bool BufferLayer::latchUnsignaledBuffers() {
543     static bool propertyLoaded = false;
544     static bool latch = false;
545     static std::mutex mutex;
546     std::lock_guard<std::mutex> lock(mutex);
547     if (!propertyLoaded) {
548         char value[PROPERTY_VALUE_MAX] = {};
549         property_get("debug.sf.latch_unsignaled", value, "0");
550         latch = atoi(value);
551         propertyLoaded = true;
552     }
553     return latch;
554 }
555 
556 // h/w composer set-up
allTransactionsSignaled(nsecs_t expectedPresentTime)557 bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
558     const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
559     bool matchingFramesFound = false;
560     bool allTransactionsApplied = true;
561     Mutex::Autolock lock(mLocalSyncPointMutex);
562 
563     for (auto& point : mLocalSyncPoints) {
564         if (point->getFrameNumber() > headFrameNumber) {
565             break;
566         }
567         matchingFramesFound = true;
568 
569         if (!point->frameIsAvailable()) {
570             // We haven't notified the remote layer that the frame for
571             // this point is available yet. Notify it now, and then
572             // abort this attempt to latch.
573             point->setFrameAvailable();
574             allTransactionsApplied = false;
575             break;
576         }
577 
578         allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
579     }
580     return !matchingFramesFound || allTransactionsApplied;
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->getWidth();
668     uint32_t bufHeight = mBufferInfo.mBuffer->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->getWidth();
700     uint32_t bufHeight = mBufferInfo.mBuffer->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->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;
788 }
789 
getDrawingTransformMatrix(bool filteringEnabled,float outMatrix[16])790 void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
791     GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
792                                        mBufferInfo.mTransform, filteringEnabled);
793 }
794 
setInitialValuesForClone(const sp<Layer> & clonedFrom)795 void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
796     Layer::setInitialValuesForClone(clonedFrom);
797 
798     sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
799     mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
800     mPotentialCursor = bufferClonedFrom->mPotentialCursor;
801     mProtectedByApp = bufferClonedFrom->mProtectedByApp;
802 
803     updateCloneBufferInfo();
804 }
805 
updateCloneBufferInfo()806 void BufferLayer::updateCloneBufferInfo() {
807     if (!isClone() || !isClonedFromAlive()) {
808         return;
809     }
810 
811     sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
812     mBufferInfo = clonedFrom->mBufferInfo;
813     mSidebandStream = clonedFrom->mSidebandStream;
814     surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
815     mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
816     mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
817 
818     // After buffer info is updated, the drawingState from the real layer needs to be copied into
819     // the cloned. This is because some properties of drawingState can change when latchBuffer is
820     // called. However, copying the drawingState would also overwrite the cloned layer's relatives
821     // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
822     // the cloned drawingState again.
823     wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
824     SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
825     wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
826     InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
827 
828     mDrawingState = clonedFrom->mDrawingState;
829 
830     mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
831     mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
832     mDrawingState.zOrderRelatives = tmpZOrderRelatives;
833     mDrawingState.inputInfo = tmpInputInfo;
834 }
835 
setTransformHint(ui::Transform::RotationFlags displayTransformHint)836 void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
837     mTransformHint = getFixedTransformHint();
838     if (mTransformHint == ui::Transform::ROT_INVALID) {
839         mTransformHint = displayTransformHint;
840     }
841 }
842 
843 } // namespace android
844 
845 #if defined(__gl_h_)
846 #error "don't include gl/gl.h in this file"
847 #endif
848 
849 #if defined(__gl2_h_)
850 #error "don't include gl2/gl2.h in this file"
851 #endif
852 
853 // TODO(b/129481165): remove the #pragma below and fix conversion issues
854 #pragma clang diagnostic pop // ignored "-Wconversion"
855