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 <common/FlagManager.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/HwcAsyncWorker.h>
27 #include <compositionengine/impl/Output.h>
28 #include <compositionengine/impl/OutputCompositionState.h>
29 #include <compositionengine/impl/OutputLayer.h>
30 #include <compositionengine/impl/OutputLayerCompositionState.h>
31 #include <compositionengine/impl/planner/Planner.h>
32 #include <ftl/algorithm.h>
33 #include <ftl/future.h>
34 #include <gui/TraceUtils.h>
35 #include <scheduler/FrameTargeter.h>
36 #include <scheduler/Time.h>
37
38 #include <optional>
39 #include <thread>
40
41 #include "renderengine/ExternalTexture.h"
42
43 // TODO(b/129481165): remove the #pragma below and fix conversion issues
44 #pragma clang diagnostic push
45 #pragma clang diagnostic ignored "-Wconversion"
46
47 #include <renderengine/DisplaySettings.h>
48 #include <renderengine/RenderEngine.h>
49
50 // TODO(b/129481165): remove the #pragma below and fix conversion issues
51 #pragma clang diagnostic pop // ignored "-Wconversion"
52
53 #include <android-base/properties.h>
54 #include <ui/DebugUtils.h>
55 #include <ui/HdrCapabilities.h>
56 #include <utils/Trace.h>
57
58 #include "TracedOrdinal.h"
59
60 using aidl::android::hardware::graphics::composer3::Composition;
61
62 namespace android::compositionengine {
63
64 Output::~Output() = default;
65
66 namespace impl {
67 using CompositionStrategyPredictionState =
68 OutputCompositionState::CompositionStrategyPredictionState;
69 namespace {
70
71 template <typename T>
72 class Reversed {
73 public:
Reversed(const T & container)74 explicit Reversed(const T& container) : mContainer(container) {}
begin()75 auto begin() { return mContainer.rbegin(); }
end()76 auto end() { return mContainer.rend(); }
77
78 private:
79 const T& mContainer;
80 };
81
82 // Helper for enumerating over a container in reverse order
83 template <typename T>
reversed(const T & c)84 Reversed<T> reversed(const T& c) {
85 return Reversed<T>(c);
86 }
87
88 struct ScaleVector {
89 float x;
90 float y;
91 };
92
93 // Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
94 // where to' will have the same size as "to". In the case where "from" and "to"
95 // start at the origin to'=to.
getScale(const Rect & from,const Rect & to)96 ScaleVector getScale(const Rect& from, const Rect& to) {
97 return {.x = static_cast<float>(to.width()) / from.width(),
98 .y = static_cast<float>(to.height()) / from.height()};
99 }
100
101 } // namespace
102
createOutput(const compositionengine::CompositionEngine & compositionEngine)103 std::shared_ptr<Output> createOutput(
104 const compositionengine::CompositionEngine& compositionEngine) {
105 return createOutputTemplated<Output>(compositionEngine);
106 }
107
108 Output::~Output() = default;
109
isValid() const110 bool Output::isValid() const {
111 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
112 mRenderSurface->isValid();
113 }
114
getDisplayId() const115 std::optional<DisplayId> Output::getDisplayId() const {
116 return {};
117 }
118
getName() const119 const std::string& Output::getName() const {
120 return mName;
121 }
122
setName(const std::string & name)123 void Output::setName(const std::string& name) {
124 mName = name;
125 auto displayIdOpt = getDisplayId();
126 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
127 to_string(*displayIdOpt).c_str())
128 : mName;
129 }
130
setCompositionEnabled(bool enabled)131 void Output::setCompositionEnabled(bool enabled) {
132 auto& outputState = editState();
133 if (outputState.isEnabled == enabled) {
134 return;
135 }
136
137 outputState.isEnabled = enabled;
138 dirtyEntireOutput();
139 }
140
setLayerCachingEnabled(bool enabled)141 void Output::setLayerCachingEnabled(bool enabled) {
142 if (enabled == (mPlanner != nullptr)) {
143 return;
144 }
145
146 if (enabled) {
147 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
148 if (mRenderSurface) {
149 mPlanner->setDisplaySize(mRenderSurface->getSize());
150 }
151 } else {
152 mPlanner.reset();
153 }
154
155 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
156 if (!outputLayer) {
157 continue;
158 }
159
160 outputLayer->editState().overrideInfo = {};
161 }
162 }
163
setLayerCachingTexturePoolEnabled(bool enabled)164 void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
165 if (mPlanner) {
166 mPlanner->setTexturePoolEnabled(enabled);
167 }
168 }
169
setProjection(ui::Rotation orientation,const Rect & layerStackSpaceRect,const Rect & orientedDisplaySpaceRect)170 void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
171 const Rect& orientedDisplaySpaceRect) {
172 auto& outputState = editState();
173
174 outputState.displaySpace.setOrientation(orientation);
175 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
176 "The display bounds are unknown.");
177
178 // Compute orientedDisplaySpace
179 ui::Size orientedSize = outputState.displaySpace.getBounds();
180 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
181 std::swap(orientedSize.width, orientedSize.height);
182 }
183 outputState.orientedDisplaySpace.setBounds(orientedSize);
184 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
185
186 // Compute displaySpace.content
187 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
188 ui::Transform rotation;
189 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
190 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
191 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
192 }
193 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
194
195 // Compute framebufferSpace
196 outputState.framebufferSpace.setOrientation(orientation);
197 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
198 "The framebuffer bounds are unknown.");
199 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
200 outputState.framebufferSpace.getBoundsAsRect());
201 outputState.framebufferSpace.setContent(
202 outputState.displaySpace.getContent().scale(scale.x, scale.y));
203
204 // Compute layerStackSpace
205 outputState.layerStackSpace.setContent(layerStackSpaceRect);
206 outputState.layerStackSpace.setBounds(
207 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
208
209 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
210 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
211 dirtyEntireOutput();
212 }
213
setNextBrightness(float brightness)214 void Output::setNextBrightness(float brightness) {
215 editState().displayBrightness = brightness;
216 }
217
setDisplaySize(const ui::Size & size)218 void Output::setDisplaySize(const ui::Size& size) {
219 mRenderSurface->setDisplaySize(size);
220
221 auto& state = editState();
222
223 // Update framebuffer space
224 const ui::Size newBounds(size);
225 state.framebufferSpace.setBounds(newBounds);
226
227 // Update display space
228 state.displaySpace.setBounds(newBounds);
229 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
230
231 // Update oriented display space
232 const auto orientation = state.displaySpace.getOrientation();
233 ui::Size orientedSize = size;
234 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
235 std::swap(orientedSize.width, orientedSize.height);
236 }
237 const ui::Size newOrientedBounds(orientedSize);
238 state.orientedDisplaySpace.setBounds(newOrientedBounds);
239
240 if (mPlanner) {
241 mPlanner->setDisplaySize(size);
242 }
243
244 dirtyEntireOutput();
245 }
246
getTransformHint() const247 ui::Transform::RotationFlags Output::getTransformHint() const {
248 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
249 }
250
setLayerFilter(ui::LayerFilter filter)251 void Output::setLayerFilter(ui::LayerFilter filter) {
252 editState().layerFilter = filter;
253 dirtyEntireOutput();
254 }
255
setColorTransform(const compositionengine::CompositionRefreshArgs & args)256 void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
257 auto& colorTransformMatrix = editState().colorTransformMatrix;
258 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
259 return;
260 }
261
262 colorTransformMatrix = *args.colorTransformMatrix;
263
264 dirtyEntireOutput();
265 }
266
setColorProfile(const ColorProfile & colorProfile)267 void Output::setColorProfile(const ColorProfile& colorProfile) {
268 auto& outputState = editState();
269 if (outputState.colorMode == colorProfile.mode &&
270 outputState.dataspace == colorProfile.dataspace &&
271 outputState.renderIntent == colorProfile.renderIntent) {
272 return;
273 }
274
275 outputState.colorMode = colorProfile.mode;
276 outputState.dataspace = colorProfile.dataspace;
277 outputState.renderIntent = colorProfile.renderIntent;
278
279 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
280
281 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
282 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
283 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
284
285 dirtyEntireOutput();
286 }
287
setDisplayBrightness(float sdrWhitePointNits,float displayBrightnessNits)288 void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
289 auto& outputState = editState();
290 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
291 outputState.displayBrightnessNits == displayBrightnessNits) {
292 // Nothing changed
293 return;
294 }
295 outputState.sdrWhitePointNits = sdrWhitePointNits;
296 outputState.displayBrightnessNits = displayBrightnessNits;
297 dirtyEntireOutput();
298 }
299
dump(std::string & out) const300 void Output::dump(std::string& out) const {
301 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
302 out.append("\n Composition Output State:\n");
303
304 dumpBase(out);
305 }
306
dumpBase(std::string & out) const307 void Output::dumpBase(std::string& out) const {
308 dumpState(out);
309 out += '\n';
310
311 if (mDisplayColorProfile) {
312 mDisplayColorProfile->dump(out);
313 } else {
314 out.append(" No display color profile!\n");
315 }
316
317 out += '\n';
318
319 if (mRenderSurface) {
320 mRenderSurface->dump(out);
321 } else {
322 out.append(" No render surface!\n");
323 }
324
325 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
326 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
327 if (!outputLayer) {
328 continue;
329 }
330 outputLayer->dump(out);
331 }
332 }
333
dumpPlannerInfo(const Vector<String16> & args,std::string & out) const334 void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
335 if (!mPlanner) {
336 out.append("Planner is disabled\n");
337 return;
338 }
339 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
340 mPlanner->dump(args, out);
341 }
342
getDisplayColorProfile() const343 compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
344 return mDisplayColorProfile.get();
345 }
346
setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode)347 void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
348 mDisplayColorProfile = std::move(mode);
349 }
350
getReleasedLayersForTest() const351 const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
352 return mReleasedLayers;
353 }
354
setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile> mode)355 void Output::setDisplayColorProfileForTest(
356 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
357 mDisplayColorProfile = std::move(mode);
358 }
359
getRenderSurface() const360 compositionengine::RenderSurface* Output::getRenderSurface() const {
361 return mRenderSurface.get();
362 }
363
setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface)364 void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
365 mRenderSurface = std::move(surface);
366 const auto size = mRenderSurface->getSize();
367 editState().framebufferSpace.setBounds(size);
368 if (mPlanner) {
369 mPlanner->setDisplaySize(size);
370 }
371 dirtyEntireOutput();
372 }
373
cacheClientCompositionRequests(uint32_t cacheSize)374 void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
375 if (cacheSize == 0) {
376 mClientCompositionRequestCache.reset();
377 } else {
378 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
379 }
380 };
381
setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface)382 void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
383 mRenderSurface = std::move(surface);
384 }
385
getDirtyRegion() const386 Region Output::getDirtyRegion() const {
387 const auto& outputState = getState();
388 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
389 }
390
includesLayer(ui::LayerFilter filter) const391 bool Output::includesLayer(ui::LayerFilter filter) const {
392 return getState().layerFilter.includes(filter);
393 }
394
includesLayer(const sp<LayerFE> & layerFE) const395 bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
396 const auto* layerFEState = layerFE->getCompositionState();
397 return layerFEState && includesLayer(layerFEState->outputFilter);
398 }
399
createOutputLayer(const sp<LayerFE> & layerFE) const400 std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
401 const sp<LayerFE>& layerFE) const {
402 return impl::createOutputLayer(*this, layerFE);
403 }
404
getOutputLayerForLayer(const sp<LayerFE> & layerFE) const405 compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
406 auto index = findCurrentOutputLayerForLayer(layerFE);
407 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
408 }
409
findCurrentOutputLayerForLayer(const sp<compositionengine::LayerFE> & layer) const410 std::optional<size_t> Output::findCurrentOutputLayerForLayer(
411 const sp<compositionengine::LayerFE>& layer) const {
412 for (size_t i = 0; i < getOutputLayerCount(); i++) {
413 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
414 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
415 return i;
416 }
417 }
418 return std::nullopt;
419 }
420
setReleasedLayers(Output::ReleasedLayers && layers)421 void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
422 mReleasedLayers = std::move(layers);
423 }
424
prepare(const compositionengine::CompositionRefreshArgs & refreshArgs,LayerFESet & geomSnapshots)425 void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
426 LayerFESet& geomSnapshots) {
427 ATRACE_CALL();
428 ALOGV(__FUNCTION__);
429
430 rebuildLayerStacks(refreshArgs, geomSnapshots);
431 uncacheBuffers(refreshArgs.bufferIdsToUncache);
432 }
433
present(const compositionengine::CompositionRefreshArgs & refreshArgs)434 ftl::Future<std::monostate> Output::present(
435 const compositionengine::CompositionRefreshArgs& refreshArgs) {
436 const auto stringifyExpectedPresentTime = [this, &refreshArgs]() -> std::string {
437 return ftl::Optional(getDisplayId())
438 .and_then(PhysicalDisplayId::tryCast)
439 .and_then([&refreshArgs](PhysicalDisplayId id) {
440 return refreshArgs.frameTargets.get(id);
441 })
442 .transform([](const auto& frameTargetPtr) {
443 return frameTargetPtr.get()->expectedPresentTime();
444 })
445 .transform([](TimePoint expectedPresentTime) {
446 return base::StringPrintf(" vsyncIn %.2fms",
447 ticks<std::milli, float>(expectedPresentTime -
448 TimePoint::now()));
449 })
450 .or_else([] {
451 // There is no vsync for this output.
452 return std::make_optional(std::string());
453 })
454 .value();
455 };
456 ATRACE_FORMAT("%s for %s%s", __func__, mNamePlusId.c_str(),
457 stringifyExpectedPresentTime().c_str());
458 ALOGV(__FUNCTION__);
459
460 updateColorProfile(refreshArgs);
461 updateCompositionState(refreshArgs);
462 planComposition();
463 writeCompositionState(refreshArgs);
464 setColorTransform(refreshArgs);
465 beginFrame();
466
467 if (isPowerHintSessionEnabled()) {
468 // always reset the flag before the composition prediction
469 setHintSessionRequiresRenderEngine(false);
470 }
471 GpuCompositionResult result;
472 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
473 if (predictCompositionStrategy) {
474 result = prepareFrameAsync();
475 } else {
476 prepareFrame();
477 }
478
479 devOptRepaintFlash(refreshArgs);
480 finishFrame(std::move(result));
481 ftl::Future<std::monostate> future;
482 const bool flushEvenWhenDisabled = !refreshArgs.bufferIdsToUncache.empty();
483 if (mOffloadPresent) {
484 future = presentFrameAndReleaseLayersAsync(flushEvenWhenDisabled);
485
486 // Only offload for this frame. The next frame will determine whether it
487 // needs to be offloaded. Leave the HwcAsyncWorker in place. For one thing,
488 // it is currently presenting. Further, it may be needed next frame, and
489 // we don't want to churn.
490 mOffloadPresent = false;
491 } else {
492 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
493 future = ftl::yield<std::monostate>({});
494 }
495 renderCachedSets(refreshArgs);
496 return future;
497 }
498
offloadPresentNextFrame()499 void Output::offloadPresentNextFrame() {
500 mOffloadPresent = true;
501 updateHwcAsyncWorker();
502 }
503
uncacheBuffers(std::vector<uint64_t> const & bufferIdsToUncache)504 void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
505 if (bufferIdsToUncache.empty()) {
506 return;
507 }
508 for (auto outputLayer : getOutputLayersOrderedByZ()) {
509 outputLayer->uncacheBuffers(bufferIdsToUncache);
510 }
511 }
512
rebuildLayerStacks(const compositionengine::CompositionRefreshArgs & refreshArgs,LayerFESet & layerFESet)513 void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
514 LayerFESet& layerFESet) {
515 auto& outputState = editState();
516
517 // Do nothing if this output is not enabled or there is no need to perform this update
518 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
519 return;
520 }
521 ATRACE_CALL();
522 ALOGV(__FUNCTION__);
523
524 // Process the layers to determine visibility and coverage
525 compositionengine::Output::CoverageState coverage{layerFESet};
526 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
527 ? std::make_optional<Region>()
528 : std::nullopt;
529 collectVisibleLayers(refreshArgs, coverage);
530
531 // Compute the resulting coverage for this output, and store it for later
532 const ui::Transform& tr = outputState.transform;
533 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
534 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
535
536 outputState.undefinedRegion = undefinedRegion;
537 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
538 }
539
collectVisibleLayers(const compositionengine::CompositionRefreshArgs & refreshArgs,compositionengine::Output::CoverageState & coverage)540 void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
541 compositionengine::Output::CoverageState& coverage) {
542 // Evaluate the layers from front to back to determine what is visible. This
543 // also incrementally calculates the coverage information for each layer as
544 // well as the entire output.
545 for (auto layer : reversed(refreshArgs.layers)) {
546 // Incrementally process the coverage for each layer
547 ensureOutputLayerIfVisible(layer, coverage);
548
549 // TODO(b/121291683): Stop early if the output is completely covered and
550 // no more layers could even be visible underneath the ones on top.
551 }
552
553 setReleasedLayers(refreshArgs);
554
555 finalizePendingOutputLayers();
556 }
557
ensureOutputLayerIfVisible(sp<compositionengine::LayerFE> & layerFE,compositionengine::Output::CoverageState & coverage)558 void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
559 compositionengine::Output::CoverageState& coverage) {
560 // Ensure we have a snapshot of the basic geometry layer state. Limit the
561 // snapshots to once per frame for each candidate layer, as layers may
562 // appear on multiple outputs.
563 if (!coverage.latchedLayers.count(layerFE)) {
564 coverage.latchedLayers.insert(layerFE);
565 }
566
567 // Only consider the layers on this output
568 if (!includesLayer(layerFE)) {
569 return;
570 }
571
572 // Obtain a read-only pointer to the front-end layer state
573 const auto* layerFEState = layerFE->getCompositionState();
574 if (CC_UNLIKELY(!layerFEState)) {
575 return;
576 }
577
578 // handle hidden surfaces by setting the visible region to empty
579 if (CC_UNLIKELY(!layerFEState->isVisible)) {
580 return;
581 }
582
583 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
584 !layerFEState->outputFilter.toInternalDisplay;
585
586 /*
587 * opaqueRegion: area of a surface that is fully opaque.
588 */
589 Region opaqueRegion;
590
591 /*
592 * visibleRegion: area of a surface that is visible on screen and not fully
593 * transparent. This is essentially the layer's footprint minus the opaque
594 * regions above it. Areas covered by a translucent surface are considered
595 * visible.
596 */
597 Region visibleRegion;
598
599 /*
600 * coveredRegion: area of a surface that is covered by all visible regions
601 * above it (which includes the translucent areas).
602 */
603 Region coveredRegion;
604
605 /*
606 * transparentRegion: area of a surface that is hinted to be completely
607 * transparent.
608 * This is used to tell when the layer has no visible non-transparent
609 * regions and can be removed from the layer list. It does not affect the
610 * visibleRegion of this layer or any layers beneath it. The hint may not
611 * be correct if apps don't respect the SurfaceView restrictions (which,
612 * sadly, some don't).
613 *
614 * In addition, it is used on DISPLAY_DECORATION layers to specify the
615 * blockingRegion, allowing the DPU to skip it to save power. Once we have
616 * hardware that supports a blockingRegion on frames with AFBC, it may be
617 * useful to use this for other layers, too, so long as we can prevent
618 * regressions on b/7179570.
619 */
620 Region transparentRegion;
621
622 /*
623 * shadowRegion: Region cast by the layer's shadow.
624 */
625 Region shadowRegion;
626
627 /**
628 * covered region above excluding internal display overlay layers
629 */
630 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
631
632 const ui::Transform& tr = layerFEState->geomLayerTransform;
633
634 // Get the visible region
635 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
636 // for computations like this?
637 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
638 visibleRegion.set(visibleRect);
639
640 if (layerFEState->shadowSettings.length > 0.0f) {
641 // if the layer casts a shadow, offset the layers visible region and
642 // calculate the shadow region.
643 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowSettings.length) * -1.0f);
644 Rect visibleRectWithShadows(visibleRect);
645 visibleRectWithShadows.inset(inset, inset, inset, inset);
646 visibleRegion.set(visibleRectWithShadows);
647 shadowRegion = visibleRegion.subtract(visibleRect);
648 }
649
650 if (visibleRegion.isEmpty()) {
651 return;
652 }
653
654 // Remove the transparent area from the visible region
655 if (!layerFEState->isOpaque) {
656 if (tr.preserveRects()) {
657 // Clip the transparent region to geomLayerBounds first
658 // The transparent region may be influenced by applications, for
659 // instance, by overriding ViewGroup#gatherTransparentRegion with a
660 // custom view. Once the layer stack -> display mapping is known, we
661 // must guard against very wrong inputs to prevent underflow or
662 // overflow errors. We do this here by constraining the transparent
663 // region to be within the pre-transform layer bounds, since the
664 // layer bounds are expected to play nicely with the full
665 // transform.
666 const Region clippedTransparentRegionHint =
667 layerFEState->transparentRegionHint.intersect(
668 Rect(layerFEState->geomLayerBounds));
669
670 if (clippedTransparentRegionHint.isEmpty()) {
671 if (!layerFEState->transparentRegionHint.isEmpty()) {
672 ALOGD("Layer: %s had an out of bounds transparent region",
673 layerFE->getDebugName());
674 layerFEState->transparentRegionHint.dump("transparentRegionHint");
675 }
676 transparentRegion.clear();
677 } else {
678 transparentRegion = tr.transform(clippedTransparentRegionHint);
679 }
680 } else {
681 // transformation too complex, can't do the
682 // transparent region optimization.
683 transparentRegion.clear();
684 }
685 }
686
687 // compute the opaque region
688 const auto layerOrientation = tr.getOrientation();
689 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
690 // If we one of the simple category of transforms (0/90/180/270 rotation
691 // + any flip), then the opaque region is the layer's footprint.
692 // Otherwise we don't try and compute the opaque region since there may
693 // be errors at the edges, and we treat the entire layer as
694 // translucent.
695 opaqueRegion.set(visibleRect);
696 }
697
698 // Clip the covered region to the visible region
699 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
700
701 // Update accumAboveCoveredLayers for next (lower) layer
702 coverage.aboveCoveredLayers.orSelf(visibleRegion);
703
704 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
705 coveredRegionExcludingDisplayOverlays =
706 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
707 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
708 }
709
710 // subtract the opaque region covered by the layers above us
711 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
712
713 if (visibleRegion.isEmpty()) {
714 return;
715 }
716
717 // Get coverage information for the layer as previously displayed,
718 // also taking over ownership from mOutputLayersorderedByZ.
719 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
720 auto prevOutputLayer =
721 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
722
723 // Get coverage information for the layer as previously displayed
724 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
725 const Region kEmptyRegion;
726 const Region& oldVisibleRegion =
727 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
728 const Region& oldCoveredRegion =
729 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
730
731 // compute this layer's dirty region
732 Region dirty;
733 if (layerFEState->contentDirty) {
734 // we need to invalidate the whole region
735 dirty = visibleRegion;
736 // as well, as the old visible region
737 dirty.orSelf(oldVisibleRegion);
738 } else {
739 /* compute the exposed region:
740 * the exposed region consists of two components:
741 * 1) what's VISIBLE now and was COVERED before
742 * 2) what's EXPOSED now less what was EXPOSED before
743 *
744 * note that (1) is conservative, we start with the whole visible region
745 * but only keep what used to be covered by something -- which mean it
746 * may have been exposed.
747 *
748 * (2) handles areas that were not covered by anything but got exposed
749 * because of a resize.
750 *
751 */
752 const Region newExposed = visibleRegion - coveredRegion;
753 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
754 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
755 }
756 dirty.subtractSelf(coverage.aboveOpaqueLayers);
757
758 // accumulate to the screen dirty region
759 coverage.dirtyRegion.orSelf(dirty);
760
761 // Update accumAboveOpaqueLayers for next (lower) layer
762 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
763
764 // Compute the visible non-transparent region
765 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
766
767 // Perform the final check to see if this layer is visible on this output
768 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
769 const auto& outputState = getState();
770 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
771 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
772 if (drawRegion.isEmpty()) {
773 return;
774 }
775
776 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
777
778 // The layer is visible. Either reuse the existing outputLayer if we have
779 // one, or create a new one if we do not.
780 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
781
782 // Store the layer coverage information into the layer state as some of it
783 // is useful later.
784 auto& outputLayerState = result->editState();
785 outputLayerState.visibleRegion = visibleRegion;
786 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
787 outputLayerState.coveredRegion = coveredRegion;
788 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
789 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
790 outputLayerState.shadowRegion = shadowRegion;
791 outputLayerState.outputSpaceBlockingRegionHint =
792 layerFEState->compositionType == Composition::DISPLAY_DECORATION
793 ? outputState.transform.transform(
794 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
795 : Region();
796 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
797 outputLayerState.coveredRegionExcludingDisplayOverlays =
798 std::move(coveredRegionExcludingDisplayOverlays);
799 }
800 }
801
setReleasedLayers(const compositionengine::CompositionRefreshArgs &)802 void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
803 // The base class does nothing with this call.
804 }
805
updateCompositionState(const compositionengine::CompositionRefreshArgs & refreshArgs)806 void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
807 ATRACE_CALL();
808 ALOGV(__FUNCTION__);
809
810 if (!getState().isEnabled) {
811 return;
812 }
813
814 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
815 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
816
817 for (auto* layer : getOutputLayersOrderedByZ()) {
818 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
819 refreshArgs.devOptForceClientComposition ||
820 forceClientComposition,
821 refreshArgs.internalDisplayRotationFlags);
822
823 if (mLayerRequestingBackgroundBlur == layer) {
824 forceClientComposition = false;
825 }
826 }
827 }
828
planComposition()829 void Output::planComposition() {
830 if (!mPlanner || !getState().isEnabled) {
831 return;
832 }
833
834 ATRACE_CALL();
835 ALOGV(__FUNCTION__);
836
837 mPlanner->plan(getOutputLayersOrderedByZ());
838 }
839
writeCompositionState(const compositionengine::CompositionRefreshArgs & refreshArgs)840 void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
841 ATRACE_CALL();
842 ALOGV(__FUNCTION__);
843
844 if (!getState().isEnabled) {
845 return;
846 }
847
848 if (auto frameTargetPtrOpt = ftl::Optional(getDisplayId())
849 .and_then(PhysicalDisplayId::tryCast)
850 .and_then([&refreshArgs](PhysicalDisplayId id) {
851 return refreshArgs.frameTargets.get(id);
852 })) {
853 editState().earliestPresentTime = frameTargetPtrOpt->get()->earliestPresentTime();
854 editState().expectedPresentTime = frameTargetPtrOpt->get()->expectedPresentTime().ns();
855 }
856 editState().frameInterval = refreshArgs.frameInterval;
857 editState().powerCallback = refreshArgs.powerCallback;
858
859 compositionengine::OutputLayer* peekThroughLayer = nullptr;
860 sp<GraphicBuffer> previousOverride = nullptr;
861 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
862 uint32_t z = 0;
863 bool overrideZ = false;
864 uint64_t outputLayerHash = 0;
865 for (auto* layer : getOutputLayersOrderedByZ()) {
866 if (layer == peekThroughLayer) {
867 // No longer needed, although it should not show up again, so
868 // resetting it is not truly needed either.
869 peekThroughLayer = nullptr;
870
871 // peekThroughLayer was already drawn ahead of its z order.
872 continue;
873 }
874 bool skipLayer = false;
875 const auto& overrideInfo = layer->getState().overrideInfo;
876 if (overrideInfo.buffer != nullptr) {
877 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
878 ALOGV("Skipping redundant buffer");
879 skipLayer = true;
880 } else {
881 // First layer with the override buffer.
882 if (overrideInfo.peekThroughLayer) {
883 peekThroughLayer = overrideInfo.peekThroughLayer;
884
885 // Draw peekThroughLayer first.
886 overrideZ = true;
887 includeGeometry = true;
888 constexpr bool isPeekingThrough = true;
889 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
890 isPeekingThrough);
891 outputLayerHash ^= android::hashCombine(
892 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
893 z, includeGeometry, overrideZ, isPeekingThrough,
894 peekThroughLayer->requiresClientComposition());
895 }
896
897 previousOverride = overrideInfo.buffer->getBuffer();
898 }
899 }
900
901 constexpr bool isPeekingThrough = false;
902 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
903 if (!skipLayer) {
904 outputLayerHash ^= android::hashCombine(
905 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
906 z, includeGeometry, overrideZ, isPeekingThrough,
907 layer->requiresClientComposition());
908 }
909 }
910 editState().outputLayerHash = outputLayerHash;
911 }
912
findLayerRequestingBackgroundComposition() const913 compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
914 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
915 for (auto* layer : getOutputLayersOrderedByZ()) {
916 const auto* compState = layer->getLayerFE().getCompositionState();
917
918 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
919 // want to force client composition because of the blur.
920 if (compState->sidebandStream != nullptr) {
921 return nullptr;
922 }
923
924 // If RenderEngine cannot render protected content, we cannot blur.
925 if (compState->hasProtectedContent &&
926 !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
927 return nullptr;
928 }
929 if (compState->isOpaque) {
930 continue;
931 }
932 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
933 layerRequestingBgComposition = layer;
934 }
935 }
936 return layerRequestingBgComposition;
937 }
938
updateColorProfile(const compositionengine::CompositionRefreshArgs & refreshArgs)939 void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
940 setColorProfile(pickColorProfile(refreshArgs));
941 }
942
943 // Returns a data space that fits all visible layers. The returned data space
944 // can only be one of
945 // - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
946 // - Dataspace::DISPLAY_P3
947 // - Dataspace::DISPLAY_BT2020
948 // The returned HDR data space is one of
949 // - Dataspace::UNKNOWN
950 // - Dataspace::BT2020_HLG
951 // - Dataspace::BT2020_PQ
getBestDataspace(ui::Dataspace * outHdrDataSpace,bool * outIsHdrClientComposition) const952 ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
953 bool* outIsHdrClientComposition) const {
954 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
955 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
956
957 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
958 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
959 // properties. Return a default dataspace value in this case.
960 if (!getState().isEnabled) {
961 return ui::Dataspace::V0_SRGB;
962 }
963
964 for (const auto* layer : getOutputLayersOrderedByZ()) {
965 switch (layer->getLayerFE().getCompositionState()->dataspace) {
966 case ui::Dataspace::V0_SCRGB:
967 case ui::Dataspace::V0_SCRGB_LINEAR:
968 case ui::Dataspace::BT2020:
969 case ui::Dataspace::BT2020_ITU:
970 case ui::Dataspace::BT2020_LINEAR:
971 case ui::Dataspace::DISPLAY_BT2020:
972 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
973 break;
974 case ui::Dataspace::DISPLAY_P3:
975 bestDataSpace = ui::Dataspace::DISPLAY_P3;
976 break;
977 case ui::Dataspace::BT2020_PQ:
978 case ui::Dataspace::BT2020_ITU_PQ:
979 bestDataSpace = ui::Dataspace::DISPLAY_P3;
980 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
981 *outIsHdrClientComposition =
982 layer->getLayerFE().getCompositionState()->forceClientComposition;
983 break;
984 case ui::Dataspace::BT2020_HLG:
985 case ui::Dataspace::BT2020_ITU_HLG:
986 bestDataSpace = ui::Dataspace::DISPLAY_P3;
987 // When there's mixed PQ content and HLG content, we set the HDR
988 // data space to be BT2020_HLG and convert PQ to HLG.
989 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
990 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
991 }
992 break;
993 default:
994 break;
995 }
996 }
997
998 return bestDataSpace;
999 }
1000
pickColorProfile(const compositionengine::CompositionRefreshArgs & refreshArgs) const1001 compositionengine::Output::ColorProfile Output::pickColorProfile(
1002 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
1003 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
1004 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
1005 ui::RenderIntent::COLORIMETRIC};
1006 }
1007
1008 ui::Dataspace hdrDataSpace;
1009 bool isHdrClientComposition = false;
1010 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
1011
1012 switch (refreshArgs.forceOutputColorMode) {
1013 case ui::ColorMode::SRGB:
1014 bestDataSpace = ui::Dataspace::V0_SRGB;
1015 break;
1016 case ui::ColorMode::DISPLAY_P3:
1017 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1018 break;
1019 default:
1020 break;
1021 }
1022
1023 // respect hdrDataSpace only when there is no legacy HDR support
1024 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1025 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1026 if (isHdr) {
1027 bestDataSpace = hdrDataSpace;
1028 }
1029
1030 ui::RenderIntent intent;
1031 switch (refreshArgs.outputColorSetting) {
1032 case OutputColorSetting::kManaged:
1033 case OutputColorSetting::kUnmanaged:
1034 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1035 : ui::RenderIntent::COLORIMETRIC;
1036 break;
1037 case OutputColorSetting::kEnhanced:
1038 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1039 break;
1040 default: // vendor display color setting
1041 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1042 break;
1043 }
1044
1045 ui::ColorMode outMode;
1046 ui::Dataspace outDataSpace;
1047 ui::RenderIntent outRenderIntent;
1048 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1049 &outRenderIntent);
1050
1051 return ColorProfile{outMode, outDataSpace, outRenderIntent};
1052 }
1053
beginFrame()1054 void Output::beginFrame() {
1055 auto& outputState = editState();
1056 const bool dirty = !getDirtyRegion().isEmpty();
1057 const bool empty = getOutputLayerCount() == 0;
1058 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
1059
1060 // If nothing has changed (!dirty), don't recompose.
1061 // If something changed, but we don't currently have any visible layers,
1062 // and didn't when we last did a composition, then skip it this time.
1063 // The second rule does two things:
1064 // - When all layers are removed from a display, we'll emit one black
1065 // frame, then nothing more until we get new layers.
1066 // - When a display is created with a private layer stack, we won't
1067 // emit any black frames until a layer is added to the layer stack.
1068 mMustRecompose = dirty && !(empty && wasEmpty);
1069
1070 const char flagPrefix[] = {'-', '+'};
1071 static_cast<void>(flagPrefix);
1072 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1073 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1074 flagPrefix[empty], flagPrefix[wasEmpty]);
1075
1076 mRenderSurface->beginFrame(mMustRecompose);
1077
1078 if (mMustRecompose) {
1079 outputState.lastCompositionHadVisibleLayers = !empty;
1080 }
1081 }
1082
prepareFrame()1083 void Output::prepareFrame() {
1084 ATRACE_CALL();
1085 ALOGV(__FUNCTION__);
1086
1087 auto& outputState = editState();
1088 if (!outputState.isEnabled) {
1089 return;
1090 }
1091
1092 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1093 bool success = chooseCompositionStrategy(&changes);
1094 resetCompositionStrategy();
1095 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
1096 outputState.previousDeviceRequestedChanges = changes;
1097 outputState.previousDeviceRequestedSuccess = success;
1098 if (success) {
1099 applyCompositionStrategy(changes);
1100 }
1101 finishPrepareFrame();
1102 }
1103
presentFrameAndReleaseLayersAsync(bool flushEvenWhenDisabled)1104 ftl::Future<std::monostate> Output::presentFrameAndReleaseLayersAsync(bool flushEvenWhenDisabled) {
1105 return ftl::Future<bool>(std::move(mHwComposerAsyncWorker->send([this, flushEvenWhenDisabled]() {
1106 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
1107 return true;
1108 })))
1109 .then([](bool) { return std::monostate{}; });
1110 }
1111
chooseCompositionStrategyAsync(std::optional<android::HWComposer::DeviceRequestedChanges> * changes)1112 std::future<bool> Output::chooseCompositionStrategyAsync(
1113 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1114 return mHwComposerAsyncWorker->send(
1115 [&, changes]() { return chooseCompositionStrategy(changes); });
1116 }
1117
prepareFrameAsync()1118 GpuCompositionResult Output::prepareFrameAsync() {
1119 ATRACE_CALL();
1120 ALOGV(__FUNCTION__);
1121 auto& state = editState();
1122 const auto& previousChanges = state.previousDeviceRequestedChanges;
1123 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1124 resetCompositionStrategy();
1125 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1126 if (state.previousDeviceRequestedSuccess) {
1127 applyCompositionStrategy(previousChanges);
1128 }
1129 finishPrepareFrame();
1130
1131 base::unique_fd bufferFence;
1132 std::shared_ptr<renderengine::ExternalTexture> buffer;
1133 updateProtectedContentState();
1134 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1135 GpuCompositionResult compositionResult;
1136 if (dequeueSucceeded) {
1137 std::optional<base::unique_fd> optFd =
1138 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
1139 if (optFd) {
1140 compositionResult.fence = std::move(*optFd);
1141 }
1142 }
1143
1144 auto chooseCompositionSuccess = hwcResult.get();
1145 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
1146 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1147 : CompositionStrategyPredictionState::FAIL;
1148 if (!predictionSucceeded) {
1149 ATRACE_NAME("CompositionStrategyPredictionMiss");
1150 resetCompositionStrategy();
1151 if (chooseCompositionSuccess) {
1152 applyCompositionStrategy(changes);
1153 }
1154 finishPrepareFrame();
1155 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1156 compositionResult.buffer = buffer;
1157 } else {
1158 ATRACE_NAME("CompositionStrategyPredictionHit");
1159 }
1160 state.previousDeviceRequestedChanges = std::move(changes);
1161 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1162 return compositionResult;
1163 }
1164
devOptRepaintFlash(const compositionengine::CompositionRefreshArgs & refreshArgs)1165 void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1166 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1167 return;
1168 }
1169
1170 if (getState().isEnabled) {
1171 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
1172 base::unique_fd bufferFence;
1173 std::shared_ptr<renderengine::ExternalTexture> buffer;
1174 updateProtectedContentState();
1175 dequeueRenderBuffer(&bufferFence, &buffer);
1176 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
1177 mRenderSurface->queueBuffer(base::unique_fd(), getHdrSdrRatio(buffer));
1178 }
1179 }
1180
1181 constexpr bool kFlushEvenWhenDisabled = false;
1182 presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
1183
1184 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1185
1186 prepareFrame();
1187 }
1188
finishFrame(GpuCompositionResult && result)1189 void Output::finishFrame(GpuCompositionResult&& result) {
1190 ATRACE_CALL();
1191 ALOGV(__FUNCTION__);
1192 const auto& outputState = getState();
1193 if (!outputState.isEnabled) {
1194 return;
1195 }
1196
1197 std::optional<base::unique_fd> optReadyFence;
1198 std::shared_ptr<renderengine::ExternalTexture> buffer;
1199 base::unique_fd bufferFence;
1200 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
1201 optReadyFence = std::move(result.fence);
1202 } else {
1203 if (result.bufferAvailable()) {
1204 buffer = std::move(result.buffer);
1205 bufferFence = std::move(result.fence);
1206 } else {
1207 updateProtectedContentState();
1208 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1209 return;
1210 }
1211 }
1212 // Repaint the framebuffer (if needed), getting the optional fence for when
1213 // the composition completes.
1214 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
1215 }
1216 if (!optReadyFence) {
1217 return;
1218 }
1219 if (isPowerHintSessionEnabled() && !isPowerHintSessionGpuReportingEnabled()) {
1220 // get fence end time to know when gpu is complete in display
1221 setHintSessionGpuFence(
1222 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
1223 }
1224 // swap buffers (presentation)
1225 mRenderSurface->queueBuffer(std::move(*optReadyFence), getHdrSdrRatio(buffer));
1226 }
1227
updateProtectedContentState()1228 void Output::updateProtectedContentState() {
1229 const auto& outputState = getState();
1230 auto& renderEngine = getCompositionEngine().getRenderEngine();
1231 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1232
1233 bool isProtected;
1234 if (FlagManager::getInstance().display_protected()) {
1235 isProtected = outputState.isProtected;
1236 } else {
1237 isProtected = outputState.isSecure;
1238 }
1239
1240 // We need to set the render surface as protected (DRM) if all the following conditions are met:
1241 // 1. The display is protected (in legacy, check if the display is secure)
1242 // 2. Protected content is supported
1243 // 3. At least one layer has protected content.
1244 if (isProtected && supportsProtectedContent) {
1245 auto layers = getOutputLayersOrderedByZ();
1246 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1247 return layer->getLayerFE().getCompositionState()->hasProtectedContent &&
1248 (!FlagManager::getInstance().protected_if_client() ||
1249 layer->requiresClientComposition());
1250 });
1251 if (needsProtected != mRenderSurface->isProtected()) {
1252 mRenderSurface->setProtected(needsProtected);
1253 }
1254 }
1255 }
1256
dequeueRenderBuffer(base::unique_fd * bufferFence,std::shared_ptr<renderengine::ExternalTexture> * tex)1257 bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1258 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1259 const auto& outputState = getState();
1260
1261 // If we aren't doing client composition on this output, but do have a
1262 // flipClientTarget request for this frame on this output, we still need to
1263 // dequeue a buffer.
1264 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1265 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1266 if (*tex == nullptr) {
1267 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1268 "client composition for this frame",
1269 mName.c_str());
1270 return false;
1271 }
1272 }
1273 return true;
1274 }
1275
composeSurfaces(const Region & debugRegion,std::shared_ptr<renderengine::ExternalTexture> tex,base::unique_fd & fd)1276 std::optional<base::unique_fd> Output::composeSurfaces(
1277 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1278 base::unique_fd& fd) {
1279 ATRACE_CALL();
1280 ALOGV(__FUNCTION__);
1281
1282 const auto& outputState = getState();
1283 const TracedOrdinal<bool> hasClientComposition = {
1284 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1285 outputState.usesClientComposition};
1286 if (!hasClientComposition) {
1287 setExpensiveRenderingExpected(false);
1288 return base::unique_fd();
1289 }
1290
1291 if (tex == nullptr) {
1292 ALOGW("Buffer not valid for display [%s], bailing out of "
1293 "client composition for this frame",
1294 mName.c_str());
1295 return {};
1296 }
1297
1298 ALOGV("hasClientComposition");
1299
1300 renderengine::DisplaySettings clientCompositionDisplay =
1301 generateClientCompositionDisplaySettings(tex);
1302
1303 // Generate the client composition requests for the layers on this output.
1304 auto& renderEngine = getCompositionEngine().getRenderEngine();
1305 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1306 std::vector<LayerFE*> clientCompositionLayersFE;
1307 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
1308 generateClientCompositionRequests(supportsProtectedContent,
1309 clientCompositionDisplay.outputDataspace,
1310 clientCompositionLayersFE);
1311 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1312
1313 OutputCompositionState& outputCompositionState = editState();
1314 // Check if the client composition requests were rendered into the provided graphic buffer. If
1315 // so, we can reuse the buffer and avoid client composition.
1316 if (mClientCompositionRequestCache) {
1317 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1318 clientCompositionDisplay,
1319 clientCompositionLayers)) {
1320 ATRACE_NAME("ClientCompositionCacheHit");
1321 outputCompositionState.reusedClientComposition = true;
1322 setExpensiveRenderingExpected(false);
1323 // b/239944175 pass the fence associated with the buffer.
1324 return base::unique_fd(std::move(fd));
1325 }
1326 ATRACE_NAME("ClientCompositionCacheMiss");
1327 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
1328 clientCompositionLayers);
1329 }
1330
1331 // We boost GPU frequency here because there will be color spaces conversion
1332 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1333 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1334 // because high frequency consumes extra battery.
1335 const bool expensiveRenderingExpected =
1336 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1337 [outputDataspace =
1338 clientCompositionDisplay.outputDataspace](const auto& layer) {
1339 return layer.sourceDataspace != outputDataspace;
1340 });
1341 if (expensiveRenderingExpected) {
1342 setExpensiveRenderingExpected(true);
1343 }
1344
1345 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1346 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
1347 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1348 std::back_inserter(clientRenderEngineLayers),
1349 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1350 return settings;
1351 });
1352
1353 const nsecs_t renderEngineStart = systemTime();
1354 auto fenceResult = renderEngine
1355 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
1356 std::move(fd))
1357 .get();
1358
1359 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
1360 // If rendering was not successful, remove the request from the cache.
1361 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
1362 }
1363 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1364 if (isPowerHintSessionEnabled()) {
1365 if (fence != Fence::NO_FENCE && fence->isValid() &&
1366 !outputCompositionState.reusedClientComposition) {
1367 setHintSessionRequiresRenderEngine(true);
1368 if (isPowerHintSessionGpuReportingEnabled()) {
1369 // the order of the two calls here matters as we should check if the previously
1370 // tracked fence has signaled first and archive the previous start time
1371 setHintSessionGpuStart(TimePoint::now());
1372 setHintSessionGpuFence(
1373 std::make_unique<FenceTime>(sp<Fence>::make(dup(fence->get()))));
1374 }
1375 }
1376 }
1377
1378 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1379 if (fence->isValid()) {
1380 timeStats->recordRenderEngineDuration(renderEngineStart,
1381 std::make_shared<FenceTime>(fence));
1382 } else {
1383 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1384 }
1385 }
1386
1387 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1388 clientComposedLayer->setWasClientComposed(fence);
1389 }
1390
1391 return base::unique_fd(fence->dup());
1392 }
1393
generateClientCompositionDisplaySettings(const std::shared_ptr<renderengine::ExternalTexture> & buffer) const1394 renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings(
1395 const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
1396 const auto& outputState = getState();
1397
1398 renderengine::DisplaySettings clientCompositionDisplay;
1399 clientCompositionDisplay.namePlusId = mNamePlusId;
1400 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1401 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1402 clientCompositionDisplay.orientation =
1403 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1404 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1405 ? outputState.dataspace
1406 : ui::Dataspace::UNKNOWN;
1407
1408 // If we have a valid current display brightness use that, otherwise fall back to the
1409 // display's max desired
1410 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1411 ? outputState.displayBrightnessNits
1412 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1413 clientCompositionDisplay.maxLuminance =
1414 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1415
1416 float hdrSdrRatioMultiplier = 1.0f / getHdrSdrRatio(buffer);
1417 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetBrightness *
1418 outputState.displayBrightnessNits * hdrSdrRatioMultiplier;
1419 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1420 clientCompositionDisplay.renderIntent =
1421 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1422 outputState.renderIntent);
1423
1424 // Compute the global color transform matrix.
1425 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1426 clientCompositionDisplay.deviceHandlesColorTransform =
1427 outputState.usesDeviceComposition || getSkipColorTransform();
1428 return clientCompositionDisplay;
1429 }
1430
generateClientCompositionRequests(bool supportsProtectedContent,ui::Dataspace outputDataspace,std::vector<LayerFE * > & outLayerFEs)1431 std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
1432 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
1433 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
1434 ALOGV("Rendering client layers");
1435
1436 const auto& outputState = getState();
1437 const Region viewportRegion(outputState.layerStackSpace.getContent());
1438 bool firstLayer = true;
1439
1440 bool disableBlurs = false;
1441 uint64_t previousOverrideBufferId = 0;
1442
1443 for (auto* layer : getOutputLayersOrderedByZ()) {
1444 const auto& layerState = layer->getState();
1445 const auto* layerFEState = layer->getLayerFE().getCompositionState();
1446 auto& layerFE = layer->getLayerFE();
1447 layerFE.setWasClientComposed(nullptr);
1448
1449 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
1450 ALOGV("Layer: %s", layerFE.getDebugName());
1451 if (clip.isEmpty()) {
1452 ALOGV(" Skipping for empty clip");
1453 firstLayer = false;
1454 continue;
1455 }
1456
1457 disableBlurs |= layerFEState->sidebandStream != nullptr;
1458
1459 const bool clientComposition = layer->requiresClientComposition();
1460
1461 // We clear the client target for non-client composed layers if
1462 // requested by the HWC. We skip this if the layer is not an opaque
1463 // rectangle, as by definition the layer must blend with whatever is
1464 // underneath. We also skip the first layer as the buffer target is
1465 // guaranteed to start out cleared.
1466 const bool clearClientComposition =
1467 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
1468
1469 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1470
1471 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1472 // composing the non-shadow content and only draw the shadows.
1473 const bool realContentIsVisible = clientComposition &&
1474 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1475
1476 if (clientComposition || clearClientComposition) {
1477 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1478 if (overrideSettings->bufferId != previousOverrideBufferId) {
1479 previousOverrideBufferId = overrideSettings->bufferId;
1480 clientCompositionLayers.push_back(std::move(*overrideSettings));
1481 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1482 } else {
1483 ALOGV("Skipping redundant override buffer for [%s] in RE",
1484 layer->getLayerFE().getDebugName());
1485 }
1486 } else {
1487 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1488 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1489 : (layer->getState().overrideInfo.disableBackgroundBlur
1490 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1491 BlurRegionsOnly
1492 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1493 Enabled);
1494 bool isProtected = supportsProtectedContent;
1495 if (FlagManager::getInstance().display_protected()) {
1496 isProtected = outputState.isProtected && supportsProtectedContent;
1497 }
1498 compositionengine::LayerFE::ClientCompositionTargetSettings
1499 targetSettings{.clip = clip,
1500 .needsFiltering = layer->needsFiltering() ||
1501 outputState.needsFiltering,
1502 .isSecure = outputState.isSecure,
1503 .isProtected = isProtected,
1504 .viewport = outputState.layerStackSpace.getContent(),
1505 .dataspace = outputDataspace,
1506 .realContentIsVisible = realContentIsVisible,
1507 .clearContent = !clientComposition,
1508 .blurSetting = blurSetting,
1509 .whitePointNits = layerState.whitePointNits,
1510 .treat170mAsSrgb = outputState.treat170mAsSrgb};
1511 if (auto clientCompositionSettings =
1512 layerFE.prepareClientComposition(targetSettings)) {
1513 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1514 if (realContentIsVisible) {
1515 layer->editState().clientCompositionTimestamp = systemTime();
1516 }
1517 }
1518 }
1519
1520 if (clientComposition) {
1521 outLayerFEs.push_back(&layerFE);
1522 }
1523 }
1524
1525 firstLayer = false;
1526 }
1527
1528 return clientCompositionLayers;
1529 }
1530
appendRegionFlashRequests(const Region & flashRegion,std::vector<LayerFE::LayerSettings> & clientCompositionLayers)1531 void Output::appendRegionFlashRequests(
1532 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
1533 if (flashRegion.isEmpty()) {
1534 return;
1535 }
1536
1537 LayerFE::LayerSettings layerSettings;
1538 layerSettings.source.buffer.buffer = nullptr;
1539 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1540 layerSettings.alpha = half(1.0);
1541
1542 for (const auto& rect : flashRegion) {
1543 layerSettings.geometry.boundaries = rect.toFloatRect();
1544 clientCompositionLayers.push_back(layerSettings);
1545 }
1546 }
1547
setExpensiveRenderingExpected(bool)1548 void Output::setExpensiveRenderingExpected(bool) {
1549 // The base class does nothing with this call.
1550 }
1551
setHintSessionGpuStart(TimePoint)1552 void Output::setHintSessionGpuStart(TimePoint) {
1553 // The base class does nothing with this call.
1554 }
1555
setHintSessionGpuFence(std::unique_ptr<FenceTime> &&)1556 void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1557 // The base class does nothing with this call.
1558 }
1559
setHintSessionRequiresRenderEngine(bool)1560 void Output::setHintSessionRequiresRenderEngine(bool) {
1561 // The base class does nothing with this call.
1562 }
1563
isPowerHintSessionEnabled()1564 bool Output::isPowerHintSessionEnabled() {
1565 return false;
1566 }
1567
isPowerHintSessionGpuReportingEnabled()1568 bool Output::isPowerHintSessionGpuReportingEnabled() {
1569 return false;
1570 }
1571
presentFrameAndReleaseLayers(bool flushEvenWhenDisabled)1572 void Output::presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) {
1573 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
1574 ALOGV(__FUNCTION__);
1575
1576 if (!getState().isEnabled) {
1577 if (flushEvenWhenDisabled && FlagManager::getInstance().flush_buffer_slots_to_uncache()) {
1578 // Some commands, like clearing buffer slots, should still be executed
1579 // even if the display is not enabled.
1580 executeCommands();
1581 }
1582 return;
1583 }
1584
1585 auto& outputState = editState();
1586 outputState.dirtyRegion.clear();
1587
1588 auto frame = presentFrame();
1589
1590 mRenderSurface->onPresentDisplayCompleted();
1591
1592 for (auto* layer : getOutputLayersOrderedByZ()) {
1593 // The layer buffer from the previous frame (if any) is released
1594 // by HWC only when the release fence from this frame (if any) is
1595 // signaled. Always get the release fence from HWC first.
1596 sp<Fence> releaseFence = Fence::NO_FENCE;
1597
1598 if (auto hwcLayer = layer->getHwcLayer()) {
1599 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1600 releaseFence = f->second;
1601 }
1602 }
1603
1604 // If the layer was client composited in the previous frame, we
1605 // need to merge with the previous client target acquire fence.
1606 // Since we do not track that, always merge with the current
1607 // client target acquire fence when it is available, even though
1608 // this is suboptimal.
1609 // TODO(b/121291683): Track previous frame client target acquire fence.
1610 if (outputState.usesClientComposition) {
1611 releaseFence =
1612 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1613 }
1614 if (FlagManager::getInstance().ce_fence_promise()) {
1615 layer->getLayerFE().setReleaseFence(releaseFence);
1616 } else {
1617 layer->getLayerFE()
1618 .onLayerDisplayed(ftl::yield<FenceResult>(std::move(releaseFence)).share(),
1619 outputState.layerFilter.layerStack);
1620 }
1621 }
1622
1623 // We've got a list of layers needing fences, that are disjoint with
1624 // OutputLayersOrderedByZ. The best we can do is to
1625 // supply them with the present fence.
1626 for (auto& weakLayer : mReleasedLayers) {
1627 if (const auto layer = weakLayer.promote()) {
1628 if (FlagManager::getInstance().ce_fence_promise()) {
1629 layer->setReleaseFence(frame.presentFence);
1630 } else {
1631 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share(),
1632 outputState.layerFilter.layerStack);
1633 }
1634 }
1635 }
1636
1637 // Clear out the released layers now that we're done with them.
1638 mReleasedLayers.clear();
1639 }
1640
renderCachedSets(const CompositionRefreshArgs & refreshArgs)1641 void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
1642 const auto& outputState = getState();
1643 if (mPlanner && outputState.isEnabled) {
1644 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1645 outputState.usesDeviceComposition || getSkipColorTransform());
1646 }
1647 }
1648
dirtyEntireOutput()1649 void Output::dirtyEntireOutput() {
1650 auto& outputState = editState();
1651 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
1652 }
1653
resetCompositionStrategy()1654 void Output::resetCompositionStrategy() {
1655 // The base output implementation can only do client composition
1656 auto& outputState = editState();
1657 outputState.usesClientComposition = true;
1658 outputState.usesDeviceComposition = false;
1659 outputState.reusedClientComposition = false;
1660 }
1661
getSkipColorTransform() const1662 bool Output::getSkipColorTransform() const {
1663 return true;
1664 }
1665
presentFrame()1666 compositionengine::Output::FrameFences Output::presentFrame() {
1667 compositionengine::Output::FrameFences result;
1668 if (getState().usesClientComposition) {
1669 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1670 }
1671 return result;
1672 }
1673
setPredictCompositionStrategy(bool predict)1674 void Output::setPredictCompositionStrategy(bool predict) {
1675 mPredictCompositionStrategy = predict;
1676 updateHwcAsyncWorker();
1677 }
1678
updateHwcAsyncWorker()1679 void Output::updateHwcAsyncWorker() {
1680 if (mPredictCompositionStrategy || mOffloadPresent) {
1681 if (!mHwComposerAsyncWorker) {
1682 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1683 }
1684 } else {
1685 mHwComposerAsyncWorker.reset(nullptr);
1686 }
1687 }
1688
setTreat170mAsSrgb(bool enable)1689 void Output::setTreat170mAsSrgb(bool enable) {
1690 editState().treat170mAsSrgb = enable;
1691 }
1692
canPredictCompositionStrategy(const CompositionRefreshArgs & refreshArgs)1693 bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
1694 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1695 uint64_t outputLayerHash = getState().outputLayerHash;
1696 editState().lastOutputLayerHash = outputLayerHash;
1697
1698 if (!getState().isEnabled || !mPredictCompositionStrategy) {
1699 ALOGV("canPredictCompositionStrategy disabled");
1700 return false;
1701 }
1702
1703 if (!getState().previousDeviceRequestedChanges) {
1704 ALOGV("canPredictCompositionStrategy previous changes not available");
1705 return false;
1706 }
1707
1708 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1709 ALOGV("canPredictCompositionStrategy surface does not support");
1710 return false;
1711 }
1712
1713 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1714 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1715 return false;
1716 }
1717
1718 if (lastOutputLayerHash != outputLayerHash) {
1719 ALOGV("canPredictCompositionStrategy output layers changed");
1720 return false;
1721 }
1722
1723 // If no layer uses clientComposition, then don't predict composition strategy
1724 // because we have less work to do in parallel.
1725 if (!anyLayersRequireClientComposition()) {
1726 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1727 return false;
1728 }
1729
1730 return true;
1731 }
1732
anyLayersRequireClientComposition() const1733 bool Output::anyLayersRequireClientComposition() const {
1734 const auto layers = getOutputLayersOrderedByZ();
1735 return std::any_of(layers.begin(), layers.end(),
1736 [](const auto& layer) { return layer->requiresClientComposition(); });
1737 }
1738
finishPrepareFrame()1739 void Output::finishPrepareFrame() {
1740 const auto& state = getState();
1741 if (mPlanner) {
1742 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1743 }
1744 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1745 }
1746
mustRecompose() const1747 bool Output::mustRecompose() const {
1748 return mMustRecompose;
1749 }
1750
getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture> & buffer) const1751 float Output::getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
1752 if (buffer == nullptr) {
1753 return 1.0f;
1754 }
1755
1756 if (!FlagManager::getInstance().fp16_client_target()) {
1757 return 1.0f;
1758 }
1759
1760 if (getState().displayBrightnessNits < 0.0f || getState().sdrWhitePointNits <= 0.0f ||
1761 buffer->getPixelFormat() != PIXEL_FORMAT_RGBA_FP16 ||
1762 (static_cast<int32_t>(getState().dataspace) &
1763 static_cast<int32_t>(ui::Dataspace::RANGE_MASK)) !=
1764 static_cast<int32_t>(ui::Dataspace::RANGE_EXTENDED)) {
1765 return 1.0f;
1766 }
1767
1768 return getState().displayBrightnessNits / getState().sdrWhitePointNits;
1769 }
1770
1771 } // namespace impl
1772 } // namespace android::compositionengine
1773