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