1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <thread>
18 
19 #include <android-base/stringprintf.h>
20 #include <compositionengine/CompositionEngine.h>
21 #include <compositionengine/CompositionRefreshArgs.h>
22 #include <compositionengine/DisplayColorProfile.h>
23 #include <compositionengine/LayerFE.h>
24 #include <compositionengine/LayerFECompositionState.h>
25 #include <compositionengine/RenderSurface.h>
26 #include <compositionengine/impl/Output.h>
27 #include <compositionengine/impl/OutputCompositionState.h>
28 #include <compositionengine/impl/OutputLayer.h>
29 #include <compositionengine/impl/OutputLayerCompositionState.h>
30 
31 // TODO(b/129481165): remove the #pragma below and fix conversion issues
32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Wconversion"
34 
35 #include <renderengine/DisplaySettings.h>
36 #include <renderengine/RenderEngine.h>
37 
38 // TODO(b/129481165): remove the #pragma below and fix conversion issues
39 #pragma clang diagnostic pop // ignored "-Wconversion"
40 
41 #include <ui/DebugUtils.h>
42 #include <ui/HdrCapabilities.h>
43 #include <utils/Trace.h>
44 
45 #include "TracedOrdinal.h"
46 
47 namespace android::compositionengine {
48 
49 Output::~Output() = default;
50 
51 namespace impl {
52 
53 namespace {
54 
55 template <typename T>
56 class Reversed {
57 public:
Reversed(const T & container)58     explicit Reversed(const T& container) : mContainer(container) {}
begin()59     auto begin() { return mContainer.rbegin(); }
end()60     auto end() { return mContainer.rend(); }
61 
62 private:
63     const T& mContainer;
64 };
65 
66 // Helper for enumerating over a container in reverse order
67 template <typename T>
reversed(const T & c)68 Reversed<T> reversed(const T& c) {
69     return Reversed<T>(c);
70 }
71 
72 } // namespace
73 
createOutput(const compositionengine::CompositionEngine & compositionEngine)74 std::shared_ptr<Output> createOutput(
75         const compositionengine::CompositionEngine& compositionEngine) {
76     return createOutputTemplated<Output>(compositionEngine);
77 }
78 
79 Output::~Output() = default;
80 
isValid() const81 bool Output::isValid() const {
82     return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
83             mRenderSurface->isValid();
84 }
85 
getDisplayId() const86 std::optional<DisplayId> Output::getDisplayId() const {
87     return {};
88 }
89 
getName() const90 const std::string& Output::getName() const {
91     return mName;
92 }
93 
setName(const std::string & name)94 void Output::setName(const std::string& name) {
95     mName = name;
96 }
97 
setCompositionEnabled(bool enabled)98 void Output::setCompositionEnabled(bool enabled) {
99     auto& outputState = editState();
100     if (outputState.isEnabled == enabled) {
101         return;
102     }
103 
104     outputState.isEnabled = enabled;
105     dirtyEntireOutput();
106 }
107 
setProjection(const ui::Transform & transform,uint32_t orientation,const Rect & frame,const Rect & viewport,const Rect & sourceClip,const Rect & destinationClip,bool needsFiltering)108 void Output::setProjection(const ui::Transform& transform, uint32_t orientation, const Rect& frame,
109                            const Rect& viewport, const Rect& sourceClip,
110                            const Rect& destinationClip, bool needsFiltering) {
111     auto& outputState = editState();
112     outputState.transform = transform;
113     outputState.orientation = orientation;
114     outputState.sourceClip = sourceClip;
115     outputState.destinationClip = destinationClip;
116     outputState.frame = frame;
117     outputState.viewport = viewport;
118     outputState.needsFiltering = needsFiltering;
119 
120     dirtyEntireOutput();
121 }
122 
123 // TODO(b/121291683): Rename setSize() once more is moved.
setBounds(const ui::Size & size)124 void Output::setBounds(const ui::Size& size) {
125     mRenderSurface->setDisplaySize(size);
126     // TODO(b/121291683): Rename outputState.size once more is moved.
127     editState().bounds = Rect(mRenderSurface->getSize());
128 
129     dirtyEntireOutput();
130 }
131 
setLayerStackFilter(uint32_t layerStackId,bool isInternal)132 void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
133     auto& outputState = editState();
134     outputState.layerStackId = layerStackId;
135     outputState.layerStackInternal = isInternal;
136 
137     dirtyEntireOutput();
138 }
139 
setColorTransform(const compositionengine::CompositionRefreshArgs & args)140 void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
141     auto& colorTransformMatrix = editState().colorTransformMatrix;
142     if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
143         return;
144     }
145 
146     colorTransformMatrix = *args.colorTransformMatrix;
147 
148     dirtyEntireOutput();
149 }
150 
setColorProfile(const ColorProfile & colorProfile)151 void Output::setColorProfile(const ColorProfile& colorProfile) {
152     ui::Dataspace targetDataspace =
153             getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
154                                                          colorProfile.colorSpaceAgnosticDataspace);
155 
156     auto& outputState = editState();
157     if (outputState.colorMode == colorProfile.mode &&
158         outputState.dataspace == colorProfile.dataspace &&
159         outputState.renderIntent == colorProfile.renderIntent &&
160         outputState.targetDataspace == targetDataspace) {
161         return;
162     }
163 
164     outputState.colorMode = colorProfile.mode;
165     outputState.dataspace = colorProfile.dataspace;
166     outputState.renderIntent = colorProfile.renderIntent;
167     outputState.targetDataspace = targetDataspace;
168 
169     mRenderSurface->setBufferDataspace(colorProfile.dataspace);
170 
171     ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
172           decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
173           decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
174 
175     dirtyEntireOutput();
176 }
177 
dump(std::string & out) const178 void Output::dump(std::string& out) const {
179     using android::base::StringAppendF;
180 
181     StringAppendF(&out, "   Composition Output State: [\"%s\"]", mName.c_str());
182 
183     out.append("\n   ");
184 
185     dumpBase(out);
186 }
187 
dumpBase(std::string & out) const188 void Output::dumpBase(std::string& out) const {
189     dumpState(out);
190 
191     if (mDisplayColorProfile) {
192         mDisplayColorProfile->dump(out);
193     } else {
194         out.append("    No display color profile!\n");
195     }
196 
197     if (mRenderSurface) {
198         mRenderSurface->dump(out);
199     } else {
200         out.append("    No render surface!\n");
201     }
202 
203     android::base::StringAppendF(&out, "\n   %zu Layers\n", getOutputLayerCount());
204     for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
205         if (!outputLayer) {
206             continue;
207         }
208         outputLayer->dump(out);
209     }
210 }
211 
getDisplayColorProfile() const212 compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
213     return mDisplayColorProfile.get();
214 }
215 
setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode)216 void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
217     mDisplayColorProfile = std::move(mode);
218 }
219 
getReleasedLayersForTest() const220 const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
221     return mReleasedLayers;
222 }
223 
setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile> mode)224 void Output::setDisplayColorProfileForTest(
225         std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
226     mDisplayColorProfile = std::move(mode);
227 }
228 
getRenderSurface() const229 compositionengine::RenderSurface* Output::getRenderSurface() const {
230     return mRenderSurface.get();
231 }
232 
setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface)233 void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
234     mRenderSurface = std::move(surface);
235     editState().bounds = Rect(mRenderSurface->getSize());
236 
237     dirtyEntireOutput();
238 }
239 
cacheClientCompositionRequests(uint32_t cacheSize)240 void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
241     if (cacheSize == 0) {
242         mClientCompositionRequestCache.reset();
243     } else {
244         mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
245     }
246 };
247 
setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface)248 void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
249     mRenderSurface = std::move(surface);
250 }
251 
getDirtyRegion(bool repaintEverything) const252 Region Output::getDirtyRegion(bool repaintEverything) const {
253     const auto& outputState = getState();
254     Region dirty(outputState.viewport);
255     if (!repaintEverything) {
256         dirty.andSelf(outputState.dirtyRegion);
257     }
258     return dirty;
259 }
260 
belongsInOutput(std::optional<uint32_t> layerStackId,bool internalOnly) const261 bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
262     // The layerStackId's must match, and also the layer must not be internal
263     // only when not on an internal output.
264     const auto& outputState = getState();
265     return layerStackId && (*layerStackId == outputState.layerStackId) &&
266             (!internalOnly || outputState.layerStackInternal);
267 }
268 
belongsInOutput(const sp<compositionengine::LayerFE> & layerFE) const269 bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
270     const auto* layerFEState = layerFE->getCompositionState();
271     return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
272 }
273 
createOutputLayer(const sp<LayerFE> & layerFE) const274 std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
275         const sp<LayerFE>& layerFE) const {
276     return impl::createOutputLayer(*this, layerFE);
277 }
278 
getOutputLayerForLayer(const sp<LayerFE> & layerFE) const279 compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
280     auto index = findCurrentOutputLayerForLayer(layerFE);
281     return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
282 }
283 
findCurrentOutputLayerForLayer(const sp<compositionengine::LayerFE> & layer) const284 std::optional<size_t> Output::findCurrentOutputLayerForLayer(
285         const sp<compositionengine::LayerFE>& layer) const {
286     for (size_t i = 0; i < getOutputLayerCount(); i++) {
287         auto outputLayer = getOutputLayerOrderedByZByIndex(i);
288         if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
289             return i;
290         }
291     }
292     return std::nullopt;
293 }
294 
setReleasedLayers(Output::ReleasedLayers && layers)295 void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
296     mReleasedLayers = std::move(layers);
297 }
298 
prepare(const compositionengine::CompositionRefreshArgs & refreshArgs,LayerFESet & geomSnapshots)299 void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
300                      LayerFESet& geomSnapshots) {
301     ATRACE_CALL();
302     ALOGV(__FUNCTION__);
303 
304     rebuildLayerStacks(refreshArgs, geomSnapshots);
305 }
306 
present(const compositionengine::CompositionRefreshArgs & refreshArgs)307 void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
308     ATRACE_CALL();
309     ALOGV(__FUNCTION__);
310 
311     updateColorProfile(refreshArgs);
312     updateAndWriteCompositionState(refreshArgs);
313     setColorTransform(refreshArgs);
314     beginFrame();
315     prepareFrame();
316     devOptRepaintFlash(refreshArgs);
317     finishFrame(refreshArgs);
318     postFramebuffer();
319 }
320 
rebuildLayerStacks(const compositionengine::CompositionRefreshArgs & refreshArgs,LayerFESet & layerFESet)321 void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
322                                 LayerFESet& layerFESet) {
323     ATRACE_CALL();
324     ALOGV(__FUNCTION__);
325 
326     auto& outputState = editState();
327 
328     // Do nothing if this output is not enabled or there is no need to perform this update
329     if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
330         return;
331     }
332 
333     // Process the layers to determine visibility and coverage
334     compositionengine::Output::CoverageState coverage{layerFESet};
335     collectVisibleLayers(refreshArgs, coverage);
336 
337     // Compute the resulting coverage for this output, and store it for later
338     const ui::Transform& tr = outputState.transform;
339     Region undefinedRegion{outputState.bounds};
340     undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
341 
342     outputState.undefinedRegion = undefinedRegion;
343     outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
344 }
345 
collectVisibleLayers(const compositionengine::CompositionRefreshArgs & refreshArgs,compositionengine::Output::CoverageState & coverage)346 void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
347                                   compositionengine::Output::CoverageState& coverage) {
348     // Evaluate the layers from front to back to determine what is visible. This
349     // also incrementally calculates the coverage information for each layer as
350     // well as the entire output.
351     for (auto layer : reversed(refreshArgs.layers)) {
352         // Incrementally process the coverage for each layer
353         ensureOutputLayerIfVisible(layer, coverage);
354 
355         // TODO(b/121291683): Stop early if the output is completely covered and
356         // no more layers could even be visible underneath the ones on top.
357     }
358 
359     setReleasedLayers(refreshArgs);
360 
361     finalizePendingOutputLayers();
362 
363     // Generate a simple Z-order values to each visible output layer
364     uint32_t zOrder = 0;
365     for (auto* outputLayer : getOutputLayersOrderedByZ()) {
366         outputLayer->editState().z = zOrder++;
367     }
368 }
369 
ensureOutputLayerIfVisible(sp<compositionengine::LayerFE> & layerFE,compositionengine::Output::CoverageState & coverage)370 void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
371                                         compositionengine::Output::CoverageState& coverage) {
372     // Ensure we have a snapshot of the basic geometry layer state. Limit the
373     // snapshots to once per frame for each candidate layer, as layers may
374     // appear on multiple outputs.
375     if (!coverage.latchedLayers.count(layerFE)) {
376         coverage.latchedLayers.insert(layerFE);
377         layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
378     }
379 
380     // Only consider the layers on the given layer stack
381     if (!belongsInOutput(layerFE)) {
382         return;
383     }
384 
385     // Obtain a read-only pointer to the front-end layer state
386     const auto* layerFEState = layerFE->getCompositionState();
387     if (CC_UNLIKELY(!layerFEState)) {
388         return;
389     }
390 
391     // handle hidden surfaces by setting the visible region to empty
392     if (CC_UNLIKELY(!layerFEState->isVisible)) {
393         return;
394     }
395 
396     /*
397      * opaqueRegion: area of a surface that is fully opaque.
398      */
399     Region opaqueRegion;
400 
401     /*
402      * visibleRegion: area of a surface that is visible on screen and not fully
403      * transparent. This is essentially the layer's footprint minus the opaque
404      * regions above it. Areas covered by a translucent surface are considered
405      * visible.
406      */
407     Region visibleRegion;
408 
409     /*
410      * coveredRegion: area of a surface that is covered by all visible regions
411      * above it (which includes the translucent areas).
412      */
413     Region coveredRegion;
414 
415     /*
416      * transparentRegion: area of a surface that is hinted to be completely
417      * transparent. This is only used to tell when the layer has no visible non-
418      * transparent regions and can be removed from the layer list. It does not
419      * affect the visibleRegion of this layer or any layers beneath it. The hint
420      * may not be correct if apps don't respect the SurfaceView restrictions
421      * (which, sadly, some don't).
422      */
423     Region transparentRegion;
424 
425     /*
426      * shadowRegion: Region cast by the layer's shadow.
427      */
428     Region shadowRegion;
429 
430     const ui::Transform& tr = layerFEState->geomLayerTransform;
431 
432     // Get the visible region
433     // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
434     // for computations like this?
435     const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
436     visibleRegion.set(visibleRect);
437 
438     if (layerFEState->shadowRadius > 0.0f) {
439         // if the layer casts a shadow, offset the layers visible region and
440         // calculate the shadow region.
441         const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
442         Rect visibleRectWithShadows(visibleRect);
443         visibleRectWithShadows.inset(inset, inset, inset, inset);
444         visibleRegion.set(visibleRectWithShadows);
445         shadowRegion = visibleRegion.subtract(visibleRect);
446     }
447 
448     if (visibleRegion.isEmpty()) {
449         return;
450     }
451 
452     // Remove the transparent area from the visible region
453     if (!layerFEState->isOpaque) {
454         if (tr.preserveRects()) {
455             // transform the transparent region
456             transparentRegion = tr.transform(layerFEState->transparentRegionHint);
457         } else {
458             // transformation too complex, can't do the
459             // transparent region optimization.
460             transparentRegion.clear();
461         }
462     }
463 
464     // compute the opaque region
465     const auto layerOrientation = tr.getOrientation();
466     if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
467         // If we one of the simple category of transforms (0/90/180/270 rotation
468         // + any flip), then the opaque region is the layer's footprint.
469         // Otherwise we don't try and compute the opaque region since there may
470         // be errors at the edges, and we treat the entire layer as
471         // translucent.
472         opaqueRegion.set(visibleRect);
473     }
474 
475     // Clip the covered region to the visible region
476     coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
477 
478     // Update accumAboveCoveredLayers for next (lower) layer
479     coverage.aboveCoveredLayers.orSelf(visibleRegion);
480 
481     // subtract the opaque region covered by the layers above us
482     visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
483 
484     if (visibleRegion.isEmpty()) {
485         return;
486     }
487 
488     // Get coverage information for the layer as previously displayed,
489     // also taking over ownership from mOutputLayersorderedByZ.
490     auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
491     auto prevOutputLayer =
492             prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
493 
494     //  Get coverage information for the layer as previously displayed
495     // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
496     const Region kEmptyRegion;
497     const Region& oldVisibleRegion =
498             prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
499     const Region& oldCoveredRegion =
500             prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
501 
502     // compute this layer's dirty region
503     Region dirty;
504     if (layerFEState->contentDirty) {
505         // we need to invalidate the whole region
506         dirty = visibleRegion;
507         // as well, as the old visible region
508         dirty.orSelf(oldVisibleRegion);
509     } else {
510         /* compute the exposed region:
511          *   the exposed region consists of two components:
512          *   1) what's VISIBLE now and was COVERED before
513          *   2) what's EXPOSED now less what was EXPOSED before
514          *
515          * note that (1) is conservative, we start with the whole visible region
516          * but only keep what used to be covered by something -- which mean it
517          * may have been exposed.
518          *
519          * (2) handles areas that were not covered by anything but got exposed
520          * because of a resize.
521          *
522          */
523         const Region newExposed = visibleRegion - coveredRegion;
524         const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
525         dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
526     }
527     dirty.subtractSelf(coverage.aboveOpaqueLayers);
528 
529     // accumulate to the screen dirty region
530     coverage.dirtyRegion.orSelf(dirty);
531 
532     // Update accumAboveOpaqueLayers for next (lower) layer
533     coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
534 
535     // Compute the visible non-transparent region
536     Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
537 
538     // Perform the final check to see if this layer is visible on this output
539     // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
540     const auto& outputState = getState();
541     Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
542     drawRegion.andSelf(outputState.bounds);
543     if (drawRegion.isEmpty()) {
544         return;
545     }
546 
547     Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
548 
549     // The layer is visible. Either reuse the existing outputLayer if we have
550     // one, or create a new one if we do not.
551     auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
552 
553     // Store the layer coverage information into the layer state as some of it
554     // is useful later.
555     auto& outputLayerState = result->editState();
556     outputLayerState.visibleRegion = visibleRegion;
557     outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
558     outputLayerState.coveredRegion = coveredRegion;
559     outputLayerState.outputSpaceVisibleRegion =
560             outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
561     outputLayerState.shadowRegion = shadowRegion;
562 }
563 
setReleasedLayers(const compositionengine::CompositionRefreshArgs &)564 void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
565     // The base class does nothing with this call.
566 }
567 
updateLayerStateFromFE(const CompositionRefreshArgs & args) const568 void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
569     for (auto* layer : getOutputLayersOrderedByZ()) {
570         layer->getLayerFE().prepareCompositionState(
571                 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
572                                                : LayerFE::StateSubset::Content);
573     }
574 }
575 
updateAndWriteCompositionState(const compositionengine::CompositionRefreshArgs & refreshArgs)576 void Output::updateAndWriteCompositionState(
577         const compositionengine::CompositionRefreshArgs& refreshArgs) {
578     ATRACE_CALL();
579     ALOGV(__FUNCTION__);
580 
581     if (!getState().isEnabled) {
582         return;
583     }
584 
585     mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
586     bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
587 
588     for (auto* layer : getOutputLayersOrderedByZ()) {
589         layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
590                                       refreshArgs.devOptForceClientComposition ||
591                                               forceClientComposition,
592                                       refreshArgs.internalDisplayRotationFlags);
593 
594         if (mLayerRequestingBackgroundBlur == layer) {
595             forceClientComposition = false;
596         }
597 
598         // Send the updated state to the HWC, if appropriate.
599         layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
600     }
601 }
602 
findLayerRequestingBackgroundComposition() const603 compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
604     compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
605     for (auto* layer : getOutputLayersOrderedByZ()) {
606         if (layer->getLayerFE().getCompositionState()->backgroundBlurRadius > 0) {
607             layerRequestingBgComposition = layer;
608         }
609     }
610     return layerRequestingBgComposition;
611 }
612 
updateColorProfile(const compositionengine::CompositionRefreshArgs & refreshArgs)613 void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
614     setColorProfile(pickColorProfile(refreshArgs));
615 }
616 
617 // Returns a data space that fits all visible layers.  The returned data space
618 // can only be one of
619 //  - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
620 //  - Dataspace::DISPLAY_P3
621 //  - Dataspace::DISPLAY_BT2020
622 // The returned HDR data space is one of
623 //  - Dataspace::UNKNOWN
624 //  - Dataspace::BT2020_HLG
625 //  - Dataspace::BT2020_PQ
getBestDataspace(ui::Dataspace * outHdrDataSpace,bool * outIsHdrClientComposition) const626 ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
627                                        bool* outIsHdrClientComposition) const {
628     ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
629     *outHdrDataSpace = ui::Dataspace::UNKNOWN;
630 
631     for (const auto* layer : getOutputLayersOrderedByZ()) {
632         switch (layer->getLayerFE().getCompositionState()->dataspace) {
633             case ui::Dataspace::V0_SCRGB:
634             case ui::Dataspace::V0_SCRGB_LINEAR:
635             case ui::Dataspace::BT2020:
636             case ui::Dataspace::BT2020_ITU:
637             case ui::Dataspace::BT2020_LINEAR:
638             case ui::Dataspace::DISPLAY_BT2020:
639                 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
640                 break;
641             case ui::Dataspace::DISPLAY_P3:
642                 bestDataSpace = ui::Dataspace::DISPLAY_P3;
643                 break;
644             case ui::Dataspace::BT2020_PQ:
645             case ui::Dataspace::BT2020_ITU_PQ:
646                 bestDataSpace = ui::Dataspace::DISPLAY_P3;
647                 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
648                 *outIsHdrClientComposition =
649                         layer->getLayerFE().getCompositionState()->forceClientComposition;
650                 break;
651             case ui::Dataspace::BT2020_HLG:
652             case ui::Dataspace::BT2020_ITU_HLG:
653                 bestDataSpace = ui::Dataspace::DISPLAY_P3;
654                 // When there's mixed PQ content and HLG content, we set the HDR
655                 // data space to be BT2020_PQ and convert HLG to PQ.
656                 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
657                     *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
658                 }
659                 break;
660             default:
661                 break;
662         }
663     }
664 
665     return bestDataSpace;
666 }
667 
pickColorProfile(const compositionengine::CompositionRefreshArgs & refreshArgs) const668 compositionengine::Output::ColorProfile Output::pickColorProfile(
669         const compositionengine::CompositionRefreshArgs& refreshArgs) const {
670     if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
671         return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
672                             ui::RenderIntent::COLORIMETRIC,
673                             refreshArgs.colorSpaceAgnosticDataspace};
674     }
675 
676     ui::Dataspace hdrDataSpace;
677     bool isHdrClientComposition = false;
678     ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
679 
680     switch (refreshArgs.forceOutputColorMode) {
681         case ui::ColorMode::SRGB:
682             bestDataSpace = ui::Dataspace::V0_SRGB;
683             break;
684         case ui::ColorMode::DISPLAY_P3:
685             bestDataSpace = ui::Dataspace::DISPLAY_P3;
686             break;
687         default:
688             break;
689     }
690 
691     // respect hdrDataSpace only when there is no legacy HDR support
692     const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
693             !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
694     if (isHdr) {
695         bestDataSpace = hdrDataSpace;
696     }
697 
698     ui::RenderIntent intent;
699     switch (refreshArgs.outputColorSetting) {
700         case OutputColorSetting::kManaged:
701         case OutputColorSetting::kUnmanaged:
702             intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
703                            : ui::RenderIntent::COLORIMETRIC;
704             break;
705         case OutputColorSetting::kEnhanced:
706             intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
707             break;
708         default: // vendor display color setting
709             intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
710             break;
711     }
712 
713     ui::ColorMode outMode;
714     ui::Dataspace outDataSpace;
715     ui::RenderIntent outRenderIntent;
716     mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
717                                            &outRenderIntent);
718 
719     return ColorProfile{outMode, outDataSpace, outRenderIntent,
720                         refreshArgs.colorSpaceAgnosticDataspace};
721 }
722 
beginFrame()723 void Output::beginFrame() {
724     auto& outputState = editState();
725     const bool dirty = !getDirtyRegion(false).isEmpty();
726     const bool empty = getOutputLayerCount() == 0;
727     const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
728 
729     // If nothing has changed (!dirty), don't recompose.
730     // If something changed, but we don't currently have any visible layers,
731     //   and didn't when we last did a composition, then skip it this time.
732     // The second rule does two things:
733     // - When all layers are removed from a display, we'll emit one black
734     //   frame, then nothing more until we get new layers.
735     // - When a display is created with a private layer stack, we won't
736     //   emit any black frames until a layer is added to the layer stack.
737     const bool mustRecompose = dirty && !(empty && wasEmpty);
738 
739     const char flagPrefix[] = {'-', '+'};
740     static_cast<void>(flagPrefix);
741     ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
742              mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
743              flagPrefix[empty], flagPrefix[wasEmpty]);
744 
745     mRenderSurface->beginFrame(mustRecompose);
746 
747     if (mustRecompose) {
748         outputState.lastCompositionHadVisibleLayers = !empty;
749     }
750 }
751 
prepareFrame()752 void Output::prepareFrame() {
753     ATRACE_CALL();
754     ALOGV(__FUNCTION__);
755 
756     const auto& outputState = getState();
757     if (!outputState.isEnabled) {
758         return;
759     }
760 
761     chooseCompositionStrategy();
762 
763     mRenderSurface->prepareFrame(outputState.usesClientComposition,
764                                  outputState.usesDeviceComposition);
765 }
766 
devOptRepaintFlash(const compositionengine::CompositionRefreshArgs & refreshArgs)767 void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
768     if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
769         return;
770     }
771 
772     if (getState().isEnabled) {
773         // transform the dirty region into this screen's coordinate space
774         const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
775         if (!dirtyRegion.isEmpty()) {
776             base::unique_fd readyFence;
777             // redraw the whole screen
778             static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
779 
780             mRenderSurface->queueBuffer(std::move(readyFence));
781         }
782     }
783 
784     postFramebuffer();
785 
786     std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
787 
788     prepareFrame();
789 }
790 
finishFrame(const compositionengine::CompositionRefreshArgs & refreshArgs)791 void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
792     ATRACE_CALL();
793     ALOGV(__FUNCTION__);
794 
795     if (!getState().isEnabled) {
796         return;
797     }
798 
799     // Repaint the framebuffer (if needed), getting the optional fence for when
800     // the composition completes.
801     auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
802     if (!optReadyFence) {
803         return;
804     }
805 
806     // swap buffers (presentation)
807     mRenderSurface->queueBuffer(std::move(*optReadyFence));
808 }
809 
composeSurfaces(const Region & debugRegion,const compositionengine::CompositionRefreshArgs & refreshArgs)810 std::optional<base::unique_fd> Output::composeSurfaces(
811         const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
812     ATRACE_CALL();
813     ALOGV(__FUNCTION__);
814 
815     const auto& outputState = getState();
816     OutputCompositionState& outputCompositionState = editState();
817     const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
818                                                       outputState.usesClientComposition};
819 
820     auto& renderEngine = getCompositionEngine().getRenderEngine();
821     const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
822 
823     // If we the display is secure, protected content support is enabled, and at
824     // least one layer has protected content, we need to use a secure back
825     // buffer.
826     if (outputState.isSecure && supportsProtectedContent) {
827         auto layers = getOutputLayersOrderedByZ();
828         bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
829             return layer->getLayerFE().getCompositionState()->hasProtectedContent;
830         });
831         if (needsProtected != renderEngine.isProtected()) {
832             renderEngine.useProtectedContext(needsProtected);
833         }
834         if (needsProtected != mRenderSurface->isProtected() &&
835             needsProtected == renderEngine.isProtected()) {
836             mRenderSurface->setProtected(needsProtected);
837         }
838     }
839 
840     base::unique_fd fd;
841     sp<GraphicBuffer> buf;
842 
843     // If we aren't doing client composition on this output, but do have a
844     // flipClientTarget request for this frame on this output, we still need to
845     // dequeue a buffer.
846     if (hasClientComposition || outputState.flipClientTarget) {
847         buf = mRenderSurface->dequeueBuffer(&fd);
848         if (buf == nullptr) {
849             ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
850                   "client composition for this frame",
851                   mName.c_str());
852             return {};
853         }
854     }
855 
856     base::unique_fd readyFence;
857     if (!hasClientComposition) {
858         setExpensiveRenderingExpected(false);
859         return readyFence;
860     }
861 
862     ALOGV("hasClientComposition");
863 
864     renderengine::DisplaySettings clientCompositionDisplay;
865     clientCompositionDisplay.physicalDisplay = outputState.destinationClip;
866     clientCompositionDisplay.clip = outputState.sourceClip;
867     clientCompositionDisplay.orientation = outputState.orientation;
868     clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
869             ? outputState.dataspace
870             : ui::Dataspace::UNKNOWN;
871     clientCompositionDisplay.maxLuminance =
872             mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
873 
874     // Compute the global color transform matrix.
875     if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
876         clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
877     }
878 
879     // Note: Updated by generateClientCompositionRequests
880     clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
881 
882     // Generate the client composition requests for the layers on this output.
883     std::vector<LayerFE::LayerSettings> clientCompositionLayers =
884             generateClientCompositionRequests(supportsProtectedContent,
885                                               clientCompositionDisplay.clearRegion,
886                                               clientCompositionDisplay.outputDataspace);
887     appendRegionFlashRequests(debugRegion, clientCompositionLayers);
888 
889     // Check if the client composition requests were rendered into the provided graphic buffer. If
890     // so, we can reuse the buffer and avoid client composition.
891     if (mClientCompositionRequestCache) {
892         if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
893                                                    clientCompositionLayers)) {
894             outputCompositionState.reusedClientComposition = true;
895             setExpensiveRenderingExpected(false);
896             return readyFence;
897         }
898         mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
899                                             clientCompositionLayers);
900     }
901 
902     // We boost GPU frequency here because there will be color spaces conversion
903     // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
904     // GPU composition can finish in time. We must reset GPU frequency afterwards,
905     // because high frequency consumes extra battery.
906     const bool expensiveBlurs =
907             refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
908     const bool expensiveRenderingExpected =
909             clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
910     if (expensiveRenderingExpected) {
911         setExpensiveRenderingExpected(true);
912     }
913 
914     std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
915     clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
916     std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
917                    std::back_inserter(clientCompositionLayerPointers),
918                    [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
919                        return &settings;
920                    });
921 
922     const nsecs_t renderEngineStart = systemTime();
923     status_t status =
924             renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers,
925                                     buf->getNativeBuffer(), /*useFramebufferCache=*/true,
926                                     std::move(fd), &readyFence);
927 
928     if (status != NO_ERROR && mClientCompositionRequestCache) {
929         // If rendering was not successful, remove the request from the cache.
930         mClientCompositionRequestCache->remove(buf->getId());
931     }
932 
933     auto& timeStats = getCompositionEngine().getTimeStats();
934     if (readyFence.get() < 0) {
935         timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
936     } else {
937         timeStats.recordRenderEngineDuration(renderEngineStart,
938                                              std::make_shared<FenceTime>(
939                                                      new Fence(dup(readyFence.get()))));
940     }
941 
942     return readyFence;
943 }
944 
generateClientCompositionRequests(bool supportsProtectedContent,Region & clearRegion,ui::Dataspace outputDataspace)945 std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
946         bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
947     std::vector<LayerFE::LayerSettings> clientCompositionLayers;
948     ALOGV("Rendering client layers");
949 
950     const auto& outputState = getState();
951     const Region viewportRegion(outputState.viewport);
952     const bool useIdentityTransform = false;
953     bool firstLayer = true;
954     // Used when a layer clears part of the buffer.
955     Region dummyRegion;
956 
957     for (auto* layer : getOutputLayersOrderedByZ()) {
958         const auto& layerState = layer->getState();
959         const auto* layerFEState = layer->getLayerFE().getCompositionState();
960         auto& layerFE = layer->getLayerFE();
961 
962         const Region clip(viewportRegion.intersect(layerState.visibleRegion));
963         ALOGV("Layer: %s", layerFE.getDebugName());
964         if (clip.isEmpty()) {
965             ALOGV("  Skipping for empty clip");
966             firstLayer = false;
967             continue;
968         }
969 
970         const bool clientComposition = layer->requiresClientComposition();
971 
972         // We clear the client target for non-client composed layers if
973         // requested by the HWC. We skip this if the layer is not an opaque
974         // rectangle, as by definition the layer must blend with whatever is
975         // underneath. We also skip the first layer as the buffer target is
976         // guaranteed to start out cleared.
977         const bool clearClientComposition =
978                 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
979 
980         ALOGV("  Composition type: client %d clear %d", clientComposition, clearClientComposition);
981 
982         // If the layer casts a shadow but the content casting the shadow is occluded, skip
983         // composing the non-shadow content and only draw the shadows.
984         const bool realContentIsVisible = clientComposition &&
985                 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
986 
987         if (clientComposition || clearClientComposition) {
988             compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
989                     clip,
990                     useIdentityTransform,
991                     layer->needsFiltering() || outputState.needsFiltering,
992                     outputState.isSecure,
993                     supportsProtectedContent,
994                     clientComposition ? clearRegion : dummyRegion,
995                     outputState.viewport,
996                     outputDataspace,
997                     realContentIsVisible,
998                     !clientComposition, /* clearContent  */
999             };
1000             std::vector<LayerFE::LayerSettings> results =
1001                     layerFE.prepareClientCompositionList(targetSettings);
1002             if (realContentIsVisible && !results.empty()) {
1003                 layer->editState().clientCompositionTimestamp = systemTime();
1004             }
1005 
1006             clientCompositionLayers.insert(clientCompositionLayers.end(),
1007                                            std::make_move_iterator(results.begin()),
1008                                            std::make_move_iterator(results.end()));
1009             results.clear();
1010         }
1011 
1012         firstLayer = false;
1013     }
1014 
1015     return clientCompositionLayers;
1016 }
1017 
appendRegionFlashRequests(const Region & flashRegion,std::vector<LayerFE::LayerSettings> & clientCompositionLayers)1018 void Output::appendRegionFlashRequests(
1019         const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
1020     if (flashRegion.isEmpty()) {
1021         return;
1022     }
1023 
1024     LayerFE::LayerSettings layerSettings;
1025     layerSettings.source.buffer.buffer = nullptr;
1026     layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1027     layerSettings.alpha = half(1.0);
1028 
1029     for (const auto& rect : flashRegion) {
1030         layerSettings.geometry.boundaries = rect.toFloatRect();
1031         clientCompositionLayers.push_back(layerSettings);
1032     }
1033 }
1034 
setExpensiveRenderingExpected(bool)1035 void Output::setExpensiveRenderingExpected(bool) {
1036     // The base class does nothing with this call.
1037 }
1038 
postFramebuffer()1039 void Output::postFramebuffer() {
1040     ATRACE_CALL();
1041     ALOGV(__FUNCTION__);
1042 
1043     if (!getState().isEnabled) {
1044         return;
1045     }
1046 
1047     auto& outputState = editState();
1048     outputState.dirtyRegion.clear();
1049     mRenderSurface->flip();
1050 
1051     auto frame = presentAndGetFrameFences();
1052 
1053     mRenderSurface->onPresentDisplayCompleted();
1054 
1055     for (auto* layer : getOutputLayersOrderedByZ()) {
1056         // The layer buffer from the previous frame (if any) is released
1057         // by HWC only when the release fence from this frame (if any) is
1058         // signaled.  Always get the release fence from HWC first.
1059         sp<Fence> releaseFence = Fence::NO_FENCE;
1060 
1061         if (auto hwcLayer = layer->getHwcLayer()) {
1062             if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1063                 releaseFence = f->second;
1064             }
1065         }
1066 
1067         // If the layer was client composited in the previous frame, we
1068         // need to merge with the previous client target acquire fence.
1069         // Since we do not track that, always merge with the current
1070         // client target acquire fence when it is available, even though
1071         // this is suboptimal.
1072         // TODO(b/121291683): Track previous frame client target acquire fence.
1073         if (outputState.usesClientComposition) {
1074             releaseFence =
1075                     Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1076         }
1077 
1078         layer->getLayerFE().onLayerDisplayed(releaseFence);
1079     }
1080 
1081     // We've got a list of layers needing fences, that are disjoint with
1082     // OutputLayersOrderedByZ.  The best we can do is to
1083     // supply them with the present fence.
1084     for (auto& weakLayer : mReleasedLayers) {
1085         if (auto layer = weakLayer.promote(); layer != nullptr) {
1086             layer->onLayerDisplayed(frame.presentFence);
1087         }
1088     }
1089 
1090     // Clear out the released layers now that we're done with them.
1091     mReleasedLayers.clear();
1092 }
1093 
dirtyEntireOutput()1094 void Output::dirtyEntireOutput() {
1095     auto& outputState = editState();
1096     outputState.dirtyRegion.set(outputState.bounds);
1097 }
1098 
chooseCompositionStrategy()1099 void Output::chooseCompositionStrategy() {
1100     // The base output implementation can only do client composition
1101     auto& outputState = editState();
1102     outputState.usesClientComposition = true;
1103     outputState.usesDeviceComposition = false;
1104     outputState.reusedClientComposition = false;
1105 }
1106 
getSkipColorTransform() const1107 bool Output::getSkipColorTransform() const {
1108     return true;
1109 }
1110 
presentAndGetFrameFences()1111 compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1112     compositionengine::Output::FrameFences result;
1113     if (getState().usesClientComposition) {
1114         result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1115     }
1116     return result;
1117 }
1118 
1119 } // namespace impl
1120 } // namespace android::compositionengine
1121