• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <SurfaceFlingerProperties.sysprop.h>
18 #include <android-base/stringprintf.h>
19 #include <compositionengine/CompositionEngine.h>
20 #include <compositionengine/CompositionRefreshArgs.h>
21 #include <compositionengine/DisplayColorProfile.h>
22 #include <compositionengine/LayerFE.h>
23 #include <compositionengine/LayerFECompositionState.h>
24 #include <compositionengine/RenderSurface.h>
25 #include <compositionengine/impl/Output.h>
26 #include <compositionengine/impl/OutputCompositionState.h>
27 #include <compositionengine/impl/OutputLayer.h>
28 #include <compositionengine/impl/OutputLayerCompositionState.h>
29 #include <compositionengine/impl/planner/Planner.h>
30 
31 #include <thread>
32 
33 #include "renderengine/ExternalTexture.h"
34 
35 // TODO(b/129481165): remove the #pragma below and fix conversion issues
36 #pragma clang diagnostic push
37 #pragma clang diagnostic ignored "-Wconversion"
38 
39 #include <renderengine/DisplaySettings.h>
40 #include <renderengine/RenderEngine.h>
41 
42 // TODO(b/129481165): remove the #pragma below and fix conversion issues
43 #pragma clang diagnostic pop // ignored "-Wconversion"
44 
45 #include <android-base/properties.h>
46 #include <ui/DebugUtils.h>
47 #include <ui/HdrCapabilities.h>
48 #include <utils/Trace.h>
49 
50 #include "TracedOrdinal.h"
51 
52 namespace android::compositionengine {
53 
54 Output::~Output() = default;
55 
56 namespace impl {
57 
58 namespace {
59 
60 template <typename T>
61 class Reversed {
62 public:
Reversed(const T & container)63     explicit Reversed(const T& container) : mContainer(container) {}
begin()64     auto begin() { return mContainer.rbegin(); }
end()65     auto end() { return mContainer.rend(); }
66 
67 private:
68     const T& mContainer;
69 };
70 
71 // Helper for enumerating over a container in reverse order
72 template <typename T>
reversed(const T & c)73 Reversed<T> reversed(const T& c) {
74     return Reversed<T>(c);
75 }
76 
77 struct ScaleVector {
78     float x;
79     float y;
80 };
81 
82 // Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
83 // where to' will have the same size as "to". In the case where "from" and "to"
84 // start at the origin to'=to.
getScale(const Rect & from,const Rect & to)85 ScaleVector getScale(const Rect& from, const Rect& to) {
86     return {.x = static_cast<float>(to.width()) / from.width(),
87             .y = static_cast<float>(to.height()) / from.height()};
88 }
89 
90 } // namespace
91 
createOutput(const compositionengine::CompositionEngine & compositionEngine)92 std::shared_ptr<Output> createOutput(
93         const compositionengine::CompositionEngine& compositionEngine) {
94     return createOutputTemplated<Output>(compositionEngine);
95 }
96 
97 Output::~Output() = default;
98 
isValid() const99 bool Output::isValid() const {
100     return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
101             mRenderSurface->isValid();
102 }
103 
getDisplayId() const104 std::optional<DisplayId> Output::getDisplayId() const {
105     return {};
106 }
107 
getName() const108 const std::string& Output::getName() const {
109     return mName;
110 }
111 
setName(const std::string & name)112 void Output::setName(const std::string& name) {
113     mName = name;
114 }
115 
setCompositionEnabled(bool enabled)116 void Output::setCompositionEnabled(bool enabled) {
117     auto& outputState = editState();
118     if (outputState.isEnabled == enabled) {
119         return;
120     }
121 
122     outputState.isEnabled = enabled;
123     dirtyEntireOutput();
124 }
125 
setLayerCachingEnabled(bool enabled)126 void Output::setLayerCachingEnabled(bool enabled) {
127     if (enabled == (mPlanner != nullptr)) {
128         return;
129     }
130 
131     if (enabled) {
132         mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
133         if (mRenderSurface) {
134             mPlanner->setDisplaySize(mRenderSurface->getSize());
135         }
136     } else {
137         mPlanner.reset();
138     }
139 
140     for (auto* outputLayer : getOutputLayersOrderedByZ()) {
141         if (!outputLayer) {
142             continue;
143         }
144 
145         outputLayer->editState().overrideInfo = {};
146     }
147 }
148 
setProjection(ui::Rotation orientation,const Rect & layerStackSpaceRect,const Rect & orientedDisplaySpaceRect)149 void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
150                            const Rect& orientedDisplaySpaceRect) {
151     auto& outputState = editState();
152 
153     outputState.displaySpace.orientation = orientation;
154     LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
155                  "The display bounds are unknown.");
156 
157     // Compute orientedDisplaySpace
158     ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
159     if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
160         std::swap(orientedSize.width, orientedSize.height);
161     }
162     outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
163     outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
164 
165     // Compute displaySpace.content
166     const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
167     ui::Transform rotation;
168     if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
169         const auto displaySize = outputState.displaySpace.bounds;
170         rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
171     }
172     outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
173 
174     // Compute framebufferSpace
175     outputState.framebufferSpace.orientation = orientation;
176     LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
177                  "The framebuffer bounds are unknown.");
178     const auto scale =
179             getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
180     outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
181 
182     // Compute layerStackSpace
183     outputState.layerStackSpace.content = layerStackSpaceRect;
184     outputState.layerStackSpace.bounds = layerStackSpaceRect;
185 
186     outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
187     outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
188     dirtyEntireOutput();
189 }
190 
setDisplaySize(const ui::Size & size)191 void Output::setDisplaySize(const ui::Size& size) {
192     mRenderSurface->setDisplaySize(size);
193 
194     auto& state = editState();
195 
196     // Update framebuffer space
197     const Rect newBounds(size);
198     state.framebufferSpace.bounds = newBounds;
199 
200     // Update display space
201     state.displaySpace.bounds = newBounds;
202     state.transform = state.layerStackSpace.getTransform(state.displaySpace);
203 
204     // Update oriented display space
205     const auto orientation = state.displaySpace.orientation;
206     ui::Size orientedSize = size;
207     if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
208         std::swap(orientedSize.width, orientedSize.height);
209     }
210     const Rect newOrientedBounds(orientedSize);
211     state.orientedDisplaySpace.bounds = newOrientedBounds;
212 
213     if (mPlanner) {
214         mPlanner->setDisplaySize(size);
215     }
216 
217     dirtyEntireOutput();
218 }
219 
getTransformHint() const220 ui::Transform::RotationFlags Output::getTransformHint() const {
221     return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
222 }
223 
setLayerStackFilter(uint32_t layerStackId,bool isInternal)224 void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
225     auto& outputState = editState();
226     outputState.layerStackId = layerStackId;
227     outputState.layerStackInternal = isInternal;
228 
229     dirtyEntireOutput();
230 }
231 
setColorTransform(const compositionengine::CompositionRefreshArgs & args)232 void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
233     auto& colorTransformMatrix = editState().colorTransformMatrix;
234     if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
235         return;
236     }
237 
238     colorTransformMatrix = *args.colorTransformMatrix;
239 
240     dirtyEntireOutput();
241 }
242 
setColorProfile(const ColorProfile & colorProfile)243 void Output::setColorProfile(const ColorProfile& colorProfile) {
244     ui::Dataspace targetDataspace =
245             getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
246                                                          colorProfile.colorSpaceAgnosticDataspace);
247 
248     auto& outputState = editState();
249     if (outputState.colorMode == colorProfile.mode &&
250         outputState.dataspace == colorProfile.dataspace &&
251         outputState.renderIntent == colorProfile.renderIntent &&
252         outputState.targetDataspace == targetDataspace) {
253         return;
254     }
255 
256     outputState.colorMode = colorProfile.mode;
257     outputState.dataspace = colorProfile.dataspace;
258     outputState.renderIntent = colorProfile.renderIntent;
259     outputState.targetDataspace = targetDataspace;
260 
261     mRenderSurface->setBufferDataspace(colorProfile.dataspace);
262 
263     ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
264           decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
265           decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
266 
267     dirtyEntireOutput();
268 }
269 
setDisplayBrightness(float sdrWhitePointNits,float displayBrightnessNits)270 void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
271     auto& outputState = editState();
272     if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
273         outputState.displayBrightnessNits == displayBrightnessNits) {
274         // Nothing changed
275         return;
276     }
277     outputState.sdrWhitePointNits = sdrWhitePointNits;
278     outputState.displayBrightnessNits = displayBrightnessNits;
279     dirtyEntireOutput();
280 }
281 
dump(std::string & out) const282 void Output::dump(std::string& out) const {
283     using android::base::StringAppendF;
284 
285     StringAppendF(&out, "   Composition Output State: [\"%s\"]", mName.c_str());
286 
287     out.append("\n   ");
288 
289     dumpBase(out);
290 }
291 
dumpBase(std::string & out) const292 void Output::dumpBase(std::string& out) const {
293     dumpState(out);
294 
295     if (mDisplayColorProfile) {
296         mDisplayColorProfile->dump(out);
297     } else {
298         out.append("    No display color profile!\n");
299     }
300 
301     if (mRenderSurface) {
302         mRenderSurface->dump(out);
303     } else {
304         out.append("    No render surface!\n");
305     }
306 
307     android::base::StringAppendF(&out, "\n   %zu Layers\n", getOutputLayerCount());
308     for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
309         if (!outputLayer) {
310             continue;
311         }
312         outputLayer->dump(out);
313     }
314 }
315 
dumpPlannerInfo(const Vector<String16> & args,std::string & out) const316 void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
317     if (!mPlanner) {
318         base::StringAppendF(&out, "Planner is disabled\n");
319         return;
320     }
321     base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
322     mPlanner->dump(args, out);
323 }
324 
getDisplayColorProfile() const325 compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
326     return mDisplayColorProfile.get();
327 }
328 
setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode)329 void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
330     mDisplayColorProfile = std::move(mode);
331 }
332 
getReleasedLayersForTest() const333 const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
334     return mReleasedLayers;
335 }
336 
setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile> mode)337 void Output::setDisplayColorProfileForTest(
338         std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
339     mDisplayColorProfile = std::move(mode);
340 }
341 
getRenderSurface() const342 compositionengine::RenderSurface* Output::getRenderSurface() const {
343     return mRenderSurface.get();
344 }
345 
setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface)346 void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
347     mRenderSurface = std::move(surface);
348     const auto size = mRenderSurface->getSize();
349     editState().framebufferSpace.bounds = Rect(size);
350     if (mPlanner) {
351         mPlanner->setDisplaySize(size);
352     }
353     dirtyEntireOutput();
354 }
355 
cacheClientCompositionRequests(uint32_t cacheSize)356 void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
357     if (cacheSize == 0) {
358         mClientCompositionRequestCache.reset();
359     } else {
360         mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
361     }
362 };
363 
setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface)364 void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
365     mRenderSurface = std::move(surface);
366 }
367 
getDirtyRegion(bool repaintEverything) const368 Region Output::getDirtyRegion(bool repaintEverything) const {
369     const auto& outputState = getState();
370     Region dirty(outputState.layerStackSpace.content);
371     if (!repaintEverything) {
372         dirty.andSelf(outputState.dirtyRegion);
373     }
374     return dirty;
375 }
376 
belongsInOutput(std::optional<uint32_t> layerStackId,bool internalOnly) const377 bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
378     // The layerStackId's must match, and also the layer must not be internal
379     // only when not on an internal output.
380     const auto& outputState = getState();
381     return layerStackId && (*layerStackId == outputState.layerStackId) &&
382             (!internalOnly || outputState.layerStackInternal);
383 }
384 
belongsInOutput(const sp<compositionengine::LayerFE> & layerFE) const385 bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
386     const auto* layerFEState = layerFE->getCompositionState();
387     return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
388 }
389 
createOutputLayer(const sp<LayerFE> & layerFE) const390 std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
391         const sp<LayerFE>& layerFE) const {
392     return impl::createOutputLayer(*this, layerFE);
393 }
394 
getOutputLayerForLayer(const sp<LayerFE> & layerFE) const395 compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
396     auto index = findCurrentOutputLayerForLayer(layerFE);
397     return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
398 }
399 
findCurrentOutputLayerForLayer(const sp<compositionengine::LayerFE> & layer) const400 std::optional<size_t> Output::findCurrentOutputLayerForLayer(
401         const sp<compositionengine::LayerFE>& layer) const {
402     for (size_t i = 0; i < getOutputLayerCount(); i++) {
403         auto outputLayer = getOutputLayerOrderedByZByIndex(i);
404         if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
405             return i;
406         }
407     }
408     return std::nullopt;
409 }
410 
setReleasedLayers(Output::ReleasedLayers && layers)411 void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
412     mReleasedLayers = std::move(layers);
413 }
414 
prepare(const compositionengine::CompositionRefreshArgs & refreshArgs,LayerFESet & geomSnapshots)415 void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
416                      LayerFESet& geomSnapshots) {
417     ATRACE_CALL();
418     ALOGV(__FUNCTION__);
419 
420     rebuildLayerStacks(refreshArgs, geomSnapshots);
421 }
422 
present(const compositionengine::CompositionRefreshArgs & refreshArgs)423 void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
424     ATRACE_CALL();
425     ALOGV(__FUNCTION__);
426 
427     updateColorProfile(refreshArgs);
428     updateCompositionState(refreshArgs);
429     planComposition();
430     writeCompositionState(refreshArgs);
431     setColorTransform(refreshArgs);
432     beginFrame();
433     prepareFrame();
434     devOptRepaintFlash(refreshArgs);
435     finishFrame(refreshArgs);
436     postFramebuffer();
437     renderCachedSets(refreshArgs);
438 }
439 
rebuildLayerStacks(const compositionengine::CompositionRefreshArgs & refreshArgs,LayerFESet & layerFESet)440 void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
441                                 LayerFESet& layerFESet) {
442     ATRACE_CALL();
443     ALOGV(__FUNCTION__);
444 
445     auto& outputState = editState();
446 
447     // Do nothing if this output is not enabled or there is no need to perform this update
448     if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
449         return;
450     }
451 
452     // Process the layers to determine visibility and coverage
453     compositionengine::Output::CoverageState coverage{layerFESet};
454     collectVisibleLayers(refreshArgs, coverage);
455 
456     // Compute the resulting coverage for this output, and store it for later
457     const ui::Transform& tr = outputState.transform;
458     Region undefinedRegion{outputState.displaySpace.bounds};
459     undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
460 
461     outputState.undefinedRegion = undefinedRegion;
462     outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
463 }
464 
collectVisibleLayers(const compositionengine::CompositionRefreshArgs & refreshArgs,compositionengine::Output::CoverageState & coverage)465 void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
466                                   compositionengine::Output::CoverageState& coverage) {
467     // Evaluate the layers from front to back to determine what is visible. This
468     // also incrementally calculates the coverage information for each layer as
469     // well as the entire output.
470     for (auto layer : reversed(refreshArgs.layers)) {
471         // Incrementally process the coverage for each layer
472         ensureOutputLayerIfVisible(layer, coverage);
473 
474         // TODO(b/121291683): Stop early if the output is completely covered and
475         // no more layers could even be visible underneath the ones on top.
476     }
477 
478     setReleasedLayers(refreshArgs);
479 
480     finalizePendingOutputLayers();
481 }
482 
ensureOutputLayerIfVisible(sp<compositionengine::LayerFE> & layerFE,compositionengine::Output::CoverageState & coverage)483 void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
484                                         compositionengine::Output::CoverageState& coverage) {
485     // Ensure we have a snapshot of the basic geometry layer state. Limit the
486     // snapshots to once per frame for each candidate layer, as layers may
487     // appear on multiple outputs.
488     if (!coverage.latchedLayers.count(layerFE)) {
489         coverage.latchedLayers.insert(layerFE);
490         layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
491     }
492 
493     // Only consider the layers on the given layer stack
494     if (!belongsInOutput(layerFE)) {
495         return;
496     }
497 
498     // Obtain a read-only pointer to the front-end layer state
499     const auto* layerFEState = layerFE->getCompositionState();
500     if (CC_UNLIKELY(!layerFEState)) {
501         return;
502     }
503 
504     // handle hidden surfaces by setting the visible region to empty
505     if (CC_UNLIKELY(!layerFEState->isVisible)) {
506         return;
507     }
508 
509     /*
510      * opaqueRegion: area of a surface that is fully opaque.
511      */
512     Region opaqueRegion;
513 
514     /*
515      * visibleRegion: area of a surface that is visible on screen and not fully
516      * transparent. This is essentially the layer's footprint minus the opaque
517      * regions above it. Areas covered by a translucent surface are considered
518      * visible.
519      */
520     Region visibleRegion;
521 
522     /*
523      * coveredRegion: area of a surface that is covered by all visible regions
524      * above it (which includes the translucent areas).
525      */
526     Region coveredRegion;
527 
528     /*
529      * transparentRegion: area of a surface that is hinted to be completely
530      * transparent. This is only used to tell when the layer has no visible non-
531      * transparent regions and can be removed from the layer list. It does not
532      * affect the visibleRegion of this layer or any layers beneath it. The hint
533      * may not be correct if apps don't respect the SurfaceView restrictions
534      * (which, sadly, some don't).
535      */
536     Region transparentRegion;
537 
538     /*
539      * shadowRegion: Region cast by the layer's shadow.
540      */
541     Region shadowRegion;
542 
543     const ui::Transform& tr = layerFEState->geomLayerTransform;
544 
545     // Get the visible region
546     // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
547     // for computations like this?
548     const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
549     visibleRegion.set(visibleRect);
550 
551     if (layerFEState->shadowRadius > 0.0f) {
552         // if the layer casts a shadow, offset the layers visible region and
553         // calculate the shadow region.
554         const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
555         Rect visibleRectWithShadows(visibleRect);
556         visibleRectWithShadows.inset(inset, inset, inset, inset);
557         visibleRegion.set(visibleRectWithShadows);
558         shadowRegion = visibleRegion.subtract(visibleRect);
559     }
560 
561     if (visibleRegion.isEmpty()) {
562         return;
563     }
564 
565     // Remove the transparent area from the visible region
566     if (!layerFEState->isOpaque) {
567         if (tr.preserveRects()) {
568             // transform the transparent region
569             transparentRegion = tr.transform(layerFEState->transparentRegionHint);
570         } else {
571             // transformation too complex, can't do the
572             // transparent region optimization.
573             transparentRegion.clear();
574         }
575     }
576 
577     // compute the opaque region
578     const auto layerOrientation = tr.getOrientation();
579     if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
580         // If we one of the simple category of transforms (0/90/180/270 rotation
581         // + any flip), then the opaque region is the layer's footprint.
582         // Otherwise we don't try and compute the opaque region since there may
583         // be errors at the edges, and we treat the entire layer as
584         // translucent.
585         opaqueRegion.set(visibleRect);
586     }
587 
588     // Clip the covered region to the visible region
589     coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
590 
591     // Update accumAboveCoveredLayers for next (lower) layer
592     coverage.aboveCoveredLayers.orSelf(visibleRegion);
593 
594     // subtract the opaque region covered by the layers above us
595     visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
596 
597     if (visibleRegion.isEmpty()) {
598         return;
599     }
600 
601     // Get coverage information for the layer as previously displayed,
602     // also taking over ownership from mOutputLayersorderedByZ.
603     auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
604     auto prevOutputLayer =
605             prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
606 
607     //  Get coverage information for the layer as previously displayed
608     // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
609     const Region kEmptyRegion;
610     const Region& oldVisibleRegion =
611             prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
612     const Region& oldCoveredRegion =
613             prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
614 
615     // compute this layer's dirty region
616     Region dirty;
617     if (layerFEState->contentDirty) {
618         // we need to invalidate the whole region
619         dirty = visibleRegion;
620         // as well, as the old visible region
621         dirty.orSelf(oldVisibleRegion);
622     } else {
623         /* compute the exposed region:
624          *   the exposed region consists of two components:
625          *   1) what's VISIBLE now and was COVERED before
626          *   2) what's EXPOSED now less what was EXPOSED before
627          *
628          * note that (1) is conservative, we start with the whole visible region
629          * but only keep what used to be covered by something -- which mean it
630          * may have been exposed.
631          *
632          * (2) handles areas that were not covered by anything but got exposed
633          * because of a resize.
634          *
635          */
636         const Region newExposed = visibleRegion - coveredRegion;
637         const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
638         dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
639     }
640     dirty.subtractSelf(coverage.aboveOpaqueLayers);
641 
642     // accumulate to the screen dirty region
643     coverage.dirtyRegion.orSelf(dirty);
644 
645     // Update accumAboveOpaqueLayers for next (lower) layer
646     coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
647 
648     // Compute the visible non-transparent region
649     Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
650 
651     // Perform the final check to see if this layer is visible on this output
652     // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
653     const auto& outputState = getState();
654     Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
655     drawRegion.andSelf(outputState.displaySpace.bounds);
656     if (drawRegion.isEmpty()) {
657         return;
658     }
659 
660     Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
661 
662     // The layer is visible. Either reuse the existing outputLayer if we have
663     // one, or create a new one if we do not.
664     auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
665 
666     // Store the layer coverage information into the layer state as some of it
667     // is useful later.
668     auto& outputLayerState = result->editState();
669     outputLayerState.visibleRegion = visibleRegion;
670     outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
671     outputLayerState.coveredRegion = coveredRegion;
672     outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
673             visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
674     outputLayerState.shadowRegion = shadowRegion;
675 }
676 
setReleasedLayers(const compositionengine::CompositionRefreshArgs &)677 void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
678     // The base class does nothing with this call.
679 }
680 
updateLayerStateFromFE(const CompositionRefreshArgs & args) const681 void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
682     for (auto* layer : getOutputLayersOrderedByZ()) {
683         layer->getLayerFE().prepareCompositionState(
684                 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
685                                                : LayerFE::StateSubset::Content);
686     }
687 }
688 
updateCompositionState(const compositionengine::CompositionRefreshArgs & refreshArgs)689 void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
690     ATRACE_CALL();
691     ALOGV(__FUNCTION__);
692 
693     if (!getState().isEnabled) {
694         return;
695     }
696 
697     mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
698     bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
699 
700     for (auto* layer : getOutputLayersOrderedByZ()) {
701         layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
702                                       refreshArgs.devOptForceClientComposition ||
703                                               forceClientComposition,
704                                       refreshArgs.internalDisplayRotationFlags);
705 
706         if (mLayerRequestingBackgroundBlur == layer) {
707             forceClientComposition = false;
708         }
709     }
710 }
711 
planComposition()712 void Output::planComposition() {
713     if (!mPlanner || !getState().isEnabled) {
714         return;
715     }
716 
717     ATRACE_CALL();
718     ALOGV(__FUNCTION__);
719 
720     mPlanner->plan(getOutputLayersOrderedByZ());
721 }
722 
writeCompositionState(const compositionengine::CompositionRefreshArgs & refreshArgs)723 void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
724     ATRACE_CALL();
725     ALOGV(__FUNCTION__);
726 
727     if (!getState().isEnabled) {
728         return;
729     }
730 
731     editState().earliestPresentTime = refreshArgs.earliestPresentTime;
732     editState().previousPresentFence = refreshArgs.previousPresentFence;
733 
734     compositionengine::OutputLayer* peekThroughLayer = nullptr;
735     sp<GraphicBuffer> previousOverride = nullptr;
736     bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
737     uint32_t z = 0;
738     bool overrideZ = false;
739     for (auto* layer : getOutputLayersOrderedByZ()) {
740         if (layer == peekThroughLayer) {
741             // No longer needed, although it should not show up again, so
742             // resetting it is not truly needed either.
743             peekThroughLayer = nullptr;
744 
745             // peekThroughLayer was already drawn ahead of its z order.
746             continue;
747         }
748         bool skipLayer = false;
749         const auto& overrideInfo = layer->getState().overrideInfo;
750         if (overrideInfo.buffer != nullptr) {
751             if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
752                 ALOGV("Skipping redundant buffer");
753                 skipLayer = true;
754             } else {
755                 // First layer with the override buffer.
756                 if (overrideInfo.peekThroughLayer) {
757                     peekThroughLayer = overrideInfo.peekThroughLayer;
758 
759                     // Draw peekThroughLayer first.
760                     overrideZ = true;
761                     includeGeometry = true;
762                     constexpr bool isPeekingThrough = true;
763                     peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
764                                                       isPeekingThrough);
765                 }
766 
767                 previousOverride = overrideInfo.buffer->getBuffer();
768             }
769         }
770 
771         constexpr bool isPeekingThrough = false;
772         layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
773     }
774 }
775 
findLayerRequestingBackgroundComposition() const776 compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
777     compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
778     for (auto* layer : getOutputLayersOrderedByZ()) {
779         auto* compState = layer->getLayerFE().getCompositionState();
780 
781         // If any layer has a sideband stream, we will disable blurs. In that case, we don't
782         // want to force client composition because of the blur.
783         if (compState->sidebandStream != nullptr) {
784             return nullptr;
785         }
786         if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
787             layerRequestingBgComposition = layer;
788         }
789     }
790     return layerRequestingBgComposition;
791 }
792 
updateColorProfile(const compositionengine::CompositionRefreshArgs & refreshArgs)793 void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
794     setColorProfile(pickColorProfile(refreshArgs));
795 }
796 
797 // Returns a data space that fits all visible layers.  The returned data space
798 // can only be one of
799 //  - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
800 //  - Dataspace::DISPLAY_P3
801 //  - Dataspace::DISPLAY_BT2020
802 // The returned HDR data space is one of
803 //  - Dataspace::UNKNOWN
804 //  - Dataspace::BT2020_HLG
805 //  - Dataspace::BT2020_PQ
getBestDataspace(ui::Dataspace * outHdrDataSpace,bool * outIsHdrClientComposition) const806 ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
807                                        bool* outIsHdrClientComposition) const {
808     ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
809     *outHdrDataSpace = ui::Dataspace::UNKNOWN;
810 
811     for (const auto* layer : getOutputLayersOrderedByZ()) {
812         switch (layer->getLayerFE().getCompositionState()->dataspace) {
813             case ui::Dataspace::V0_SCRGB:
814             case ui::Dataspace::V0_SCRGB_LINEAR:
815             case ui::Dataspace::BT2020:
816             case ui::Dataspace::BT2020_ITU:
817             case ui::Dataspace::BT2020_LINEAR:
818             case ui::Dataspace::DISPLAY_BT2020:
819                 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
820                 break;
821             case ui::Dataspace::DISPLAY_P3:
822                 bestDataSpace = ui::Dataspace::DISPLAY_P3;
823                 break;
824             case ui::Dataspace::BT2020_PQ:
825             case ui::Dataspace::BT2020_ITU_PQ:
826                 bestDataSpace = ui::Dataspace::DISPLAY_P3;
827                 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
828                 *outIsHdrClientComposition =
829                         layer->getLayerFE().getCompositionState()->forceClientComposition;
830                 break;
831             case ui::Dataspace::BT2020_HLG:
832             case ui::Dataspace::BT2020_ITU_HLG:
833                 bestDataSpace = ui::Dataspace::DISPLAY_P3;
834                 // When there's mixed PQ content and HLG content, we set the HDR
835                 // data space to be BT2020_PQ and convert HLG to PQ.
836                 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
837                     *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
838                 }
839                 break;
840             default:
841                 break;
842         }
843     }
844 
845     return bestDataSpace;
846 }
847 
pickColorProfile(const compositionengine::CompositionRefreshArgs & refreshArgs) const848 compositionengine::Output::ColorProfile Output::pickColorProfile(
849         const compositionengine::CompositionRefreshArgs& refreshArgs) const {
850     if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
851         return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
852                             ui::RenderIntent::COLORIMETRIC,
853                             refreshArgs.colorSpaceAgnosticDataspace};
854     }
855 
856     ui::Dataspace hdrDataSpace;
857     bool isHdrClientComposition = false;
858     ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
859 
860     switch (refreshArgs.forceOutputColorMode) {
861         case ui::ColorMode::SRGB:
862             bestDataSpace = ui::Dataspace::V0_SRGB;
863             break;
864         case ui::ColorMode::DISPLAY_P3:
865             bestDataSpace = ui::Dataspace::DISPLAY_P3;
866             break;
867         default:
868             break;
869     }
870 
871     // respect hdrDataSpace only when there is no legacy HDR support
872     const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
873             !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
874     if (isHdr) {
875         bestDataSpace = hdrDataSpace;
876     }
877 
878     ui::RenderIntent intent;
879     switch (refreshArgs.outputColorSetting) {
880         case OutputColorSetting::kManaged:
881         case OutputColorSetting::kUnmanaged:
882             intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
883                            : ui::RenderIntent::COLORIMETRIC;
884             break;
885         case OutputColorSetting::kEnhanced:
886             intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
887             break;
888         default: // vendor display color setting
889             intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
890             break;
891     }
892 
893     ui::ColorMode outMode;
894     ui::Dataspace outDataSpace;
895     ui::RenderIntent outRenderIntent;
896     mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
897                                            &outRenderIntent);
898 
899     return ColorProfile{outMode, outDataSpace, outRenderIntent,
900                         refreshArgs.colorSpaceAgnosticDataspace};
901 }
902 
beginFrame()903 void Output::beginFrame() {
904     auto& outputState = editState();
905     const bool dirty = !getDirtyRegion(false).isEmpty();
906     const bool empty = getOutputLayerCount() == 0;
907     const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
908 
909     // If nothing has changed (!dirty), don't recompose.
910     // If something changed, but we don't currently have any visible layers,
911     //   and didn't when we last did a composition, then skip it this time.
912     // The second rule does two things:
913     // - When all layers are removed from a display, we'll emit one black
914     //   frame, then nothing more until we get new layers.
915     // - When a display is created with a private layer stack, we won't
916     //   emit any black frames until a layer is added to the layer stack.
917     const bool mustRecompose = dirty && !(empty && wasEmpty);
918 
919     const char flagPrefix[] = {'-', '+'};
920     static_cast<void>(flagPrefix);
921     ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
922              mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
923              flagPrefix[empty], flagPrefix[wasEmpty]);
924 
925     mRenderSurface->beginFrame(mustRecompose);
926 
927     if (mustRecompose) {
928         outputState.lastCompositionHadVisibleLayers = !empty;
929     }
930 }
931 
prepareFrame()932 void Output::prepareFrame() {
933     ATRACE_CALL();
934     ALOGV(__FUNCTION__);
935 
936     const auto& outputState = getState();
937     if (!outputState.isEnabled) {
938         return;
939     }
940 
941     chooseCompositionStrategy();
942 
943     if (mPlanner) {
944         mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
945     }
946 
947     mRenderSurface->prepareFrame(outputState.usesClientComposition,
948                                  outputState.usesDeviceComposition);
949 }
950 
devOptRepaintFlash(const compositionengine::CompositionRefreshArgs & refreshArgs)951 void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
952     if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
953         return;
954     }
955 
956     if (getState().isEnabled) {
957         // transform the dirty region into this screen's coordinate space
958         const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
959         if (!dirtyRegion.isEmpty()) {
960             base::unique_fd readyFence;
961             // redraw the whole screen
962             static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
963 
964             mRenderSurface->queueBuffer(std::move(readyFence));
965         }
966     }
967 
968     postFramebuffer();
969 
970     std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
971 
972     prepareFrame();
973 }
974 
finishFrame(const compositionengine::CompositionRefreshArgs & refreshArgs)975 void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
976     ATRACE_CALL();
977     ALOGV(__FUNCTION__);
978 
979     if (!getState().isEnabled) {
980         return;
981     }
982 
983     // Repaint the framebuffer (if needed), getting the optional fence for when
984     // the composition completes.
985     auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
986     if (!optReadyFence) {
987         return;
988     }
989 
990     // swap buffers (presentation)
991     mRenderSurface->queueBuffer(std::move(*optReadyFence));
992 }
993 
composeSurfaces(const Region & debugRegion,const compositionengine::CompositionRefreshArgs & refreshArgs)994 std::optional<base::unique_fd> Output::composeSurfaces(
995         const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
996     ATRACE_CALL();
997     ALOGV(__FUNCTION__);
998 
999     const auto& outputState = getState();
1000     OutputCompositionState& outputCompositionState = editState();
1001     const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
1002                                                       outputState.usesClientComposition};
1003 
1004     auto& renderEngine = getCompositionEngine().getRenderEngine();
1005     const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1006 
1007     // If we the display is secure, protected content support is enabled, and at
1008     // least one layer has protected content, we need to use a secure back
1009     // buffer.
1010     if (outputState.isSecure && supportsProtectedContent) {
1011         auto layers = getOutputLayersOrderedByZ();
1012         bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1013             return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1014         });
1015         if (needsProtected != renderEngine.isProtected()) {
1016             renderEngine.useProtectedContext(needsProtected);
1017         }
1018         if (needsProtected != mRenderSurface->isProtected() &&
1019             needsProtected == renderEngine.isProtected()) {
1020             mRenderSurface->setProtected(needsProtected);
1021         }
1022     } else if (!outputState.isSecure && renderEngine.isProtected()) {
1023         renderEngine.useProtectedContext(false);
1024     }
1025 
1026     base::unique_fd fd;
1027 
1028     std::shared_ptr<renderengine::ExternalTexture> tex;
1029 
1030     // If we aren't doing client composition on this output, but do have a
1031     // flipClientTarget request for this frame on this output, we still need to
1032     // dequeue a buffer.
1033     if (hasClientComposition || outputState.flipClientTarget) {
1034         tex = mRenderSurface->dequeueBuffer(&fd);
1035         if (tex == nullptr) {
1036             ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1037                   "client composition for this frame",
1038                   mName.c_str());
1039             return {};
1040         }
1041     }
1042 
1043     base::unique_fd readyFence;
1044     if (!hasClientComposition) {
1045         setExpensiveRenderingExpected(false);
1046         return readyFence;
1047     }
1048 
1049     ALOGV("hasClientComposition");
1050 
1051     renderengine::DisplaySettings clientCompositionDisplay;
1052     clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
1053     clientCompositionDisplay.clip = outputState.layerStackSpace.content;
1054     clientCompositionDisplay.orientation =
1055             ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
1056     clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1057             ? outputState.dataspace
1058             : ui::Dataspace::UNKNOWN;
1059 
1060     // If we have a valid current display brightness use that, otherwise fall back to the
1061     // display's max desired
1062     clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1063             ? outputState.displayBrightnessNits
1064             : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1065     clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
1066 
1067     // Compute the global color transform matrix.
1068     if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1069         clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1070     }
1071 
1072     // Note: Updated by generateClientCompositionRequests
1073     clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1074 
1075     // Generate the client composition requests for the layers on this output.
1076     std::vector<LayerFE::LayerSettings> clientCompositionLayers =
1077             generateClientCompositionRequests(supportsProtectedContent,
1078                                               clientCompositionDisplay.clearRegion,
1079                                               clientCompositionDisplay.outputDataspace);
1080     appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1081 
1082     // Check if the client composition requests were rendered into the provided graphic buffer. If
1083     // so, we can reuse the buffer and avoid client composition.
1084     if (mClientCompositionRequestCache) {
1085         if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1086                                                    clientCompositionDisplay,
1087                                                    clientCompositionLayers)) {
1088             outputCompositionState.reusedClientComposition = true;
1089             setExpensiveRenderingExpected(false);
1090             return readyFence;
1091         }
1092         mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
1093                                             clientCompositionLayers);
1094     }
1095 
1096     // We boost GPU frequency here because there will be color spaces conversion
1097     // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1098     // GPU composition can finish in time. We must reset GPU frequency afterwards,
1099     // because high frequency consumes extra battery.
1100     const bool expensiveBlurs =
1101             refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
1102     const bool expensiveRenderingExpected =
1103             clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
1104     if (expensiveRenderingExpected) {
1105         setExpensiveRenderingExpected(true);
1106     }
1107 
1108     std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1109     clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1110     std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1111                    std::back_inserter(clientCompositionLayerPointers),
1112                    [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1113                        return &settings;
1114                    });
1115 
1116     const nsecs_t renderEngineStart = systemTime();
1117     // Only use the framebuffer cache when rendering to an internal display
1118     // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1119     // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1120     // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1121     // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1122     // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1123     const bool useFramebufferCache = outputState.layerStackInternal;
1124     status_t status =
1125             renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
1126                                     useFramebufferCache, std::move(fd), &readyFence);
1127 
1128     if (status != NO_ERROR && mClientCompositionRequestCache) {
1129         // If rendering was not successful, remove the request from the cache.
1130         mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
1131     }
1132 
1133     auto& timeStats = getCompositionEngine().getTimeStats();
1134     if (readyFence.get() < 0) {
1135         timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1136     } else {
1137         timeStats.recordRenderEngineDuration(renderEngineStart,
1138                                              std::make_shared<FenceTime>(
1139                                                      new Fence(dup(readyFence.get()))));
1140     }
1141 
1142     return readyFence;
1143 }
1144 
generateClientCompositionRequests(bool supportsProtectedContent,Region & clearRegion,ui::Dataspace outputDataspace)1145 std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
1146         bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
1147     std::vector<LayerFE::LayerSettings> clientCompositionLayers;
1148     ALOGV("Rendering client layers");
1149 
1150     const auto& outputState = getState();
1151     const Region viewportRegion(outputState.layerStackSpace.content);
1152     bool firstLayer = true;
1153     // Used when a layer clears part of the buffer.
1154     Region stubRegion;
1155 
1156     bool disableBlurs = false;
1157     sp<GraphicBuffer> previousOverrideBuffer = nullptr;
1158 
1159     for (auto* layer : getOutputLayersOrderedByZ()) {
1160         const auto& layerState = layer->getState();
1161         const auto* layerFEState = layer->getLayerFE().getCompositionState();
1162         auto& layerFE = layer->getLayerFE();
1163 
1164         const Region clip(viewportRegion.intersect(layerState.visibleRegion));
1165         ALOGV("Layer: %s", layerFE.getDebugName());
1166         if (clip.isEmpty()) {
1167             ALOGV("  Skipping for empty clip");
1168             firstLayer = false;
1169             continue;
1170         }
1171 
1172         disableBlurs |= layerFEState->sidebandStream != nullptr;
1173 
1174         const bool clientComposition = layer->requiresClientComposition();
1175 
1176         // We clear the client target for non-client composed layers if
1177         // requested by the HWC. We skip this if the layer is not an opaque
1178         // rectangle, as by definition the layer must blend with whatever is
1179         // underneath. We also skip the first layer as the buffer target is
1180         // guaranteed to start out cleared.
1181         const bool clearClientComposition =
1182                 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
1183 
1184         ALOGV("  Composition type: client %d clear %d", clientComposition, clearClientComposition);
1185 
1186         // If the layer casts a shadow but the content casting the shadow is occluded, skip
1187         // composing the non-shadow content and only draw the shadows.
1188         const bool realContentIsVisible = clientComposition &&
1189                 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1190 
1191         if (clientComposition || clearClientComposition) {
1192             std::vector<LayerFE::LayerSettings> results;
1193             if (layer->getState().overrideInfo.buffer != nullptr) {
1194                 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
1195                     results = layer->getOverrideCompositionList();
1196                     previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
1197                     ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1198                 } else {
1199                     ALOGV("Skipping redundant override buffer for [%s] in RE",
1200                           layer->getLayerFE().getDebugName());
1201                 }
1202             } else {
1203                 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1204                         ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1205                         : (layer->getState().overrideInfo.disableBackgroundBlur
1206                                    ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1207                                              BlurRegionsOnly
1208                                    : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1209                                              Enabled);
1210                 compositionengine::LayerFE::ClientCompositionTargetSettings
1211                         targetSettings{.clip = clip,
1212                                        .needsFiltering = layer->needsFiltering() ||
1213                                                outputState.needsFiltering,
1214                                        .isSecure = outputState.isSecure,
1215                                        .supportsProtectedContent = supportsProtectedContent,
1216                                        .clearRegion = clientComposition ? clearRegion : stubRegion,
1217                                        .viewport = outputState.layerStackSpace.content,
1218                                        .dataspace = outputDataspace,
1219                                        .realContentIsVisible = realContentIsVisible,
1220                                        .clearContent = !clientComposition,
1221                                        .blurSetting = blurSetting};
1222                 results = layerFE.prepareClientCompositionList(targetSettings);
1223                 if (realContentIsVisible && !results.empty()) {
1224                     layer->editState().clientCompositionTimestamp = systemTime();
1225                 }
1226             }
1227 
1228             clientCompositionLayers.insert(clientCompositionLayers.end(),
1229                                            std::make_move_iterator(results.begin()),
1230                                            std::make_move_iterator(results.end()));
1231             results.clear();
1232         }
1233 
1234         firstLayer = false;
1235     }
1236 
1237     return clientCompositionLayers;
1238 }
1239 
appendRegionFlashRequests(const Region & flashRegion,std::vector<LayerFE::LayerSettings> & clientCompositionLayers)1240 void Output::appendRegionFlashRequests(
1241         const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
1242     if (flashRegion.isEmpty()) {
1243         return;
1244     }
1245 
1246     LayerFE::LayerSettings layerSettings;
1247     layerSettings.source.buffer.buffer = nullptr;
1248     layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1249     layerSettings.alpha = half(1.0);
1250 
1251     for (const auto& rect : flashRegion) {
1252         layerSettings.geometry.boundaries = rect.toFloatRect();
1253         clientCompositionLayers.push_back(layerSettings);
1254     }
1255 }
1256 
setExpensiveRenderingExpected(bool)1257 void Output::setExpensiveRenderingExpected(bool) {
1258     // The base class does nothing with this call.
1259 }
1260 
postFramebuffer()1261 void Output::postFramebuffer() {
1262     ATRACE_CALL();
1263     ALOGV(__FUNCTION__);
1264 
1265     if (!getState().isEnabled) {
1266         return;
1267     }
1268 
1269     auto& outputState = editState();
1270     outputState.dirtyRegion.clear();
1271     mRenderSurface->flip();
1272 
1273     auto frame = presentAndGetFrameFences();
1274 
1275     mRenderSurface->onPresentDisplayCompleted();
1276 
1277     for (auto* layer : getOutputLayersOrderedByZ()) {
1278         // The layer buffer from the previous frame (if any) is released
1279         // by HWC only when the release fence from this frame (if any) is
1280         // signaled.  Always get the release fence from HWC first.
1281         sp<Fence> releaseFence = Fence::NO_FENCE;
1282 
1283         if (auto hwcLayer = layer->getHwcLayer()) {
1284             if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1285                 releaseFence = f->second;
1286             }
1287         }
1288 
1289         // If the layer was client composited in the previous frame, we
1290         // need to merge with the previous client target acquire fence.
1291         // Since we do not track that, always merge with the current
1292         // client target acquire fence when it is available, even though
1293         // this is suboptimal.
1294         // TODO(b/121291683): Track previous frame client target acquire fence.
1295         if (outputState.usesClientComposition) {
1296             releaseFence =
1297                     Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1298         }
1299 
1300         layer->getLayerFE().onLayerDisplayed(releaseFence);
1301     }
1302 
1303     // We've got a list of layers needing fences, that are disjoint with
1304     // OutputLayersOrderedByZ.  The best we can do is to
1305     // supply them with the present fence.
1306     for (auto& weakLayer : mReleasedLayers) {
1307         if (auto layer = weakLayer.promote(); layer != nullptr) {
1308             layer->onLayerDisplayed(frame.presentFence);
1309         }
1310     }
1311 
1312     // Clear out the released layers now that we're done with them.
1313     mReleasedLayers.clear();
1314 }
1315 
renderCachedSets(const CompositionRefreshArgs & refreshArgs)1316 void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
1317     if (mPlanner) {
1318         mPlanner->renderCachedSets(getState(), refreshArgs.nextInvalidateTime);
1319     }
1320 }
1321 
dirtyEntireOutput()1322 void Output::dirtyEntireOutput() {
1323     auto& outputState = editState();
1324     outputState.dirtyRegion.set(outputState.displaySpace.bounds);
1325 }
1326 
chooseCompositionStrategy()1327 void Output::chooseCompositionStrategy() {
1328     // The base output implementation can only do client composition
1329     auto& outputState = editState();
1330     outputState.usesClientComposition = true;
1331     outputState.usesDeviceComposition = false;
1332     outputState.reusedClientComposition = false;
1333 }
1334 
getSkipColorTransform() const1335 bool Output::getSkipColorTransform() const {
1336     return true;
1337 }
1338 
presentAndGetFrameFences()1339 compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1340     compositionengine::Output::FrameFences result;
1341     if (getState().usesClientComposition) {
1342         result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1343     }
1344     return result;
1345 }
1346 
1347 } // namespace impl
1348 } // namespace android::compositionengine
1349