blob: ee813bf3409ab6345804c95e24f9f7a68f1956fc [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
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
Alec Mouria90a5702021-04-16 16:36:21 +000017#include <SurfaceFlingerProperties.sysprop.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070018#include <android-base/stringprintf.h>
Melody Hsu793f8362024-01-08 20:00:35 +000019#include <common/FlagManager.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000020#include <common/trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070021#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080022#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070023#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080024#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070025#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070026#include <compositionengine/RenderSurface.h>
Vishnu Naira3140382022-02-24 14:07:11 -080027#include <compositionengine/impl/HwcAsyncWorker.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070028#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080030#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070031#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080032#include <compositionengine/impl/planner/Planner.h>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050033#include <ftl/algorithm.h>
Sally Qi59a9f502021-10-12 18:53:23 +000034#include <ftl/future.h>
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060035#include <ftl/optional.h>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050036#include <scheduler/FrameTargeter.h>
37#include <scheduler/Time.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080038
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060039#include <com_android_graphics_libgui_flags.h>
40
Chavi Weingarten545da0e2023-02-09 14:55:57 +000041#include <optional>
Alec Mouria90a5702021-04-16 16:36:21 +000042#include <thread>
43
44#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080045
46// TODO(b/129481165): remove the #pragma below and fix conversion issues
47#pragma clang diagnostic push
48#pragma clang diagnostic ignored "-Wconversion"
49
Lloyd Pique688abd42019-02-15 15:42:24 -080050#include <renderengine/DisplaySettings.h>
51#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080052
53// TODO(b/129481165): remove the #pragma below and fix conversion issues
54#pragma clang diagnostic pop // ignored "-Wconversion"
55
Dan Stoza269dc4d2021-01-15 15:07:43 -080056#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070057#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080058#include <ui/HdrCapabilities.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070059
Lloyd Pique688abd42019-02-15 15:42:24 -080060#include "TracedOrdinal.h"
61
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050062using aidl::android::hardware::graphics::composer3::Composition;
63
Lloyd Piquefeb73d72018-12-04 17:23:44 -080064namespace android::compositionengine {
65
66Output::~Output() = default;
67
68namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080069using CompositionStrategyPredictionState =
70 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080071namespace {
72
73template <typename T>
74class Reversed {
75public:
76 explicit Reversed(const T& container) : mContainer(container) {}
77 auto begin() { return mContainer.rbegin(); }
78 auto end() { return mContainer.rend(); }
79
80private:
81 const T& mContainer;
82};
83
84// Helper for enumerating over a container in reverse order
85template <typename T>
86Reversed<T> reversed(const T& c) {
87 return Reversed<T>(c);
88}
89
Marin Shalamanovb15d2272020-09-17 21:41:52 +020090struct ScaleVector {
91 float x;
92 float y;
93};
94
95// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
96// where to' will have the same size as "to". In the case where "from" and "to"
97// start at the origin to'=to.
98ScaleVector getScale(const Rect& from, const Rect& to) {
99 return {.x = static_cast<float>(to.width()) / from.width(),
100 .y = static_cast<float>(to.height()) / from.height()};
101}
102
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800103} // namespace
104
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700105std::shared_ptr<Output> createOutput(
106 const compositionengine::CompositionEngine& compositionEngine) {
107 return createOutputTemplated<Output>(compositionEngine);
108}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109
110Output::~Output() = default;
111
Lloyd Pique32cbe282018-10-19 13:09:22 -0700112bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700113 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
114 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700115}
116
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600117ftl::Optional<DisplayId> Output::getDisplayId() const {
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700118 return {};
119}
120
Lloyd Pique32cbe282018-10-19 13:09:22 -0700121const std::string& Output::getName() const {
122 return mName;
123}
124
125void Output::setName(const std::string& name) {
126 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400127 auto displayIdOpt = getDisplayId();
Leon Scroggins IIIc03d4652023-01-05 13:03:53 -0500128 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
129 to_string(*displayIdOpt).c_str())
130 : mName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700131}
132
133void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700134 auto& outputState = editState();
135 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700136 return;
137 }
138
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700139 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700140 dirtyEntireOutput();
141}
142
Alec Mouri023c1882021-05-08 16:36:33 -0700143void Output::setLayerCachingEnabled(bool enabled) {
144 if (enabled == (mPlanner != nullptr)) {
145 return;
146 }
147
148 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700149 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700150 if (mRenderSurface) {
151 mPlanner->setDisplaySize(mRenderSurface->getSize());
152 }
153 } else {
154 mPlanner.reset();
155 }
Alec Mouric773472b2021-05-19 14:29:05 -0700156
157 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
158 if (!outputLayer) {
159 continue;
160 }
161
162 outputLayer->editState().overrideInfo = {};
163 }
Alec Mouri023c1882021-05-08 16:36:33 -0700164}
165
Ady Abrahamdb036a82021-07-16 14:18:34 -0700166void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
167 if (mPlanner) {
168 mPlanner->setTexturePoolEnabled(enabled);
169 }
170}
171
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200172void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
173 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700174 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200175
Angel Aguayob084e0c2021-08-04 23:27:28 +0000176 outputState.displaySpace.setOrientation(orientation);
177 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200178 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200179
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000181 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200182 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200183 std::swap(orientedSize.width, orientedSize.height);
184 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000185 outputState.orientedDisplaySpace.setBounds(orientedSize);
186 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200187
188 // Compute displaySpace.content
189 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
190 ui::Transform rotation;
191 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000192 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200193 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
194 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000195 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200196
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200197 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000198 outputState.framebufferSpace.setOrientation(orientation);
199 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200200 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000201 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
202 outputState.framebufferSpace.getBoundsAsRect());
203 outputState.framebufferSpace.setContent(
204 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200205
206 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000207 outputState.layerStackSpace.setContent(layerStackSpaceRect);
208 outputState.layerStackSpace.setBounds(
209 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200210
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200211 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
212 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700213 dirtyEntireOutput();
214}
215
Alec Mouricdf16792021-12-10 13:16:06 -0800216void Output::setNextBrightness(float brightness) {
217 editState().displayBrightness = brightness;
218}
219
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200220void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700221 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200222
223 auto& state = editState();
224
225 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000226 const ui::Size newBounds(size);
227 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200228
229 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000230 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200231 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
232
233 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000234 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200235 ui::Size orientedSize = size;
236 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
237 std::swap(orientedSize.width, orientedSize.height);
238 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000239 const ui::Size newOrientedBounds(orientedSize);
240 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700241
Dan Stoza6166c312021-01-15 16:34:05 -0800242 if (mPlanner) {
243 mPlanner->setDisplaySize(size);
244 }
245
Lloyd Pique32cbe282018-10-19 13:09:22 -0700246 dirtyEntireOutput();
247}
248
Garfield Tan54edd912020-10-21 16:31:41 -0700249ui::Transform::RotationFlags Output::getTransformHint() const {
250 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
251}
252
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700253void Output::setLayerFilter(ui::LayerFilter filter) {
254 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255 dirtyEntireOutput();
256}
257
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800258void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700259 auto& colorTransformMatrix = editState().colorTransformMatrix;
260 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700261 return;
262 }
263
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700264 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800265
266 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700267}
268
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800269void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700270 auto& outputState = editState();
271 if (outputState.colorMode == colorProfile.mode &&
272 outputState.dataspace == colorProfile.dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000273 outputState.renderIntent == colorProfile.renderIntent) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800274 return;
275 }
276
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700277 outputState.colorMode = colorProfile.mode;
278 outputState.dataspace = colorProfile.dataspace;
279 outputState.renderIntent = colorProfile.renderIntent;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700280
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800281 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700282
Lloyd Pique32cbe282018-10-19 13:09:22 -0700283 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800284 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
285 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800286
287 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700288}
289
John Reckac09e452021-04-07 16:35:37 -0400290void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
291 auto& outputState = editState();
292 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
293 outputState.displayBrightnessNits == displayBrightnessNits) {
294 // Nothing changed
295 return;
296 }
297 outputState.sdrWhitePointNits = sdrWhitePointNits;
298 outputState.displayBrightnessNits = displayBrightnessNits;
299 dirtyEntireOutput();
300}
301
Lloyd Pique32cbe282018-10-19 13:09:22 -0700302void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700303 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
304 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700305
306 dumpBase(out);
307}
308
309void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700311 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700312
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700313 if (mDisplayColorProfile) {
314 mDisplayColorProfile->dump(out);
315 } else {
316 out.append(" No display color profile!\n");
317 }
318
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700319 out += '\n';
320
Lloyd Pique31cb2942018-10-19 17:23:03 -0700321 if (mRenderSurface) {
322 mRenderSurface->dump(out);
323 } else {
324 out.append(" No render surface!\n");
325 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800326
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700327 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700328 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800329 if (!outputLayer) {
330 continue;
331 }
332 outputLayer->dump(out);
333 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700334}
335
Dan Stoza269dc4d2021-01-15 15:07:43 -0800336void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
337 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700338 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800339 return;
340 }
341 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
342 mPlanner->dump(args, out);
343}
344
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700345compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
346 return mDisplayColorProfile.get();
347}
348
349void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
350 mDisplayColorProfile = std::move(mode);
351}
352
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800353const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
354 return mReleasedLayers;
355}
356
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700357void Output::setDisplayColorProfileForTest(
358 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
359 mDisplayColorProfile = std::move(mode);
360}
361
Lloyd Pique31cb2942018-10-19 17:23:03 -0700362compositionengine::RenderSurface* Output::getRenderSurface() const {
363 return mRenderSurface.get();
364}
365
366void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
367 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800368 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000369 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800370 if (mPlanner) {
371 mPlanner->setDisplaySize(size);
372 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700373 dirtyEntireOutput();
374}
375
Vishnu Nair9b079a22020-01-21 14:36:08 -0800376void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
377 if (cacheSize == 0) {
378 mClientCompositionRequestCache.reset();
379 } else {
380 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
381 }
382};
383
Lloyd Pique31cb2942018-10-19 17:23:03 -0700384void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
385 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700386}
387
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700388Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700389 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000390 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700391}
392
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700393bool Output::includesLayer(ui::LayerFilter filter) const {
394 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700395}
396
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700397bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800398 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700399 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800400}
401
Lloyd Piquedf336d92019-03-07 21:38:42 -0800402std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800403 const sp<LayerFE>& layerFE) const {
404 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800405}
406
Lloyd Piquede196652020-01-22 17:29:58 -0800407compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
408 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700409 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800410}
411
Lloyd Pique01c77c12019-04-17 12:48:32 -0700412std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800413 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700414 for (size_t i = 0; i < getOutputLayerCount(); i++) {
415 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800416 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700417 return i;
418 }
419 }
420 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800421}
422
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800423void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
424 mReleasedLayers = std::move(layers);
425}
426
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800427void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
428 LayerFESet& geomSnapshots) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000429 SFTRACE_CALL();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800430 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800431
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800432 rebuildLayerStacks(refreshArgs, geomSnapshots);
Brian Lindahl439afad2022-11-14 11:16:55 -0700433 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800434}
435
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400436ftl::Future<std::monostate> Output::present(
437 const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500438 const auto stringifyExpectedPresentTime = [this, &refreshArgs]() -> std::string {
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600439 return getDisplayId()
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500440 .and_then(PhysicalDisplayId::tryCast)
441 .and_then([&refreshArgs](PhysicalDisplayId id) {
442 return refreshArgs.frameTargets.get(id);
443 })
444 .transform([](const auto& frameTargetPtr) {
445 return frameTargetPtr.get()->expectedPresentTime();
446 })
447 .transform([](TimePoint expectedPresentTime) {
448 return base::StringPrintf(" vsyncIn %.2fms",
449 ticks<std::milli, float>(expectedPresentTime -
450 TimePoint::now()));
451 })
452 .or_else([] {
453 // There is no vsync for this output.
454 return std::make_optional(std::string());
455 })
456 .value();
457 };
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000458 SFTRACE_FORMAT("%s for %s%s", __func__, mNamePlusId.c_str(),
459 stringifyExpectedPresentTime().c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800460 ALOGV(__FUNCTION__);
461
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800462 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800463 updateCompositionState(refreshArgs);
464 planComposition();
465 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800466 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800467 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800468
Xiang Wangaab31162024-03-12 19:48:08 -0700469 if (isPowerHintSessionEnabled()) {
470 // always reset the flag before the composition prediction
471 setHintSessionRequiresRenderEngine(false);
472 }
Vishnu Naira3140382022-02-24 14:07:11 -0800473 GpuCompositionResult result;
474 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
475 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000476 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800477 } else {
478 prepareFrame();
479 }
480
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800481 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000482 finishFrame(std::move(result));
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400483 ftl::Future<std::monostate> future;
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400484 const bool flushEvenWhenDisabled = !refreshArgs.bufferIdsToUncache.empty();
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400485 if (mOffloadPresent) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400486 future = presentFrameAndReleaseLayersAsync(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400487
488 // Only offload for this frame. The next frame will determine whether it
489 // needs to be offloaded. Leave the HwcAsyncWorker in place. For one thing,
490 // it is currently presenting. Further, it may be needed next frame, and
491 // we don't want to churn.
492 mOffloadPresent = false;
493 } else {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400494 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400495 future = ftl::yield<std::monostate>({});
496 }
Alec Mouriaa831582021-06-07 16:23:01 -0700497 renderCachedSets(refreshArgs);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400498 return future;
499}
500
501void Output::offloadPresentNextFrame() {
502 mOffloadPresent = true;
503 updateHwcAsyncWorker();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800504}
505
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800506void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
507 LayerFESet& layerFESet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700508 auto& outputState = editState();
509
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800510 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700511 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800512 return;
513 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000514 SFTRACE_CALL();
Vishnu Naird9a640b2023-07-21 14:20:27 +0000515 ALOGV(__FUNCTION__);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800516
517 // Process the layers to determine visibility and coverage
518 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000519 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
520 ? std::make_optional<Region>()
521 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800522 collectVisibleLayers(refreshArgs, coverage);
523
524 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700525 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000526 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800527 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
528
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700529 outputState.undefinedRegion = undefinedRegion;
530 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800531}
532
533void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
534 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800535 // Evaluate the layers from front to back to determine what is visible. This
536 // also incrementally calculates the coverage information for each layer as
537 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800538 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700539 // Incrementally process the coverage for each layer
540 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800541
542 // TODO(b/121291683): Stop early if the output is completely covered and
543 // no more layers could even be visible underneath the ones on top.
544 }
545
Lloyd Pique01c77c12019-04-17 12:48:32 -0700546 setReleasedLayers(refreshArgs);
547
548 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549}
550
Lloyd Piquede196652020-01-22 17:29:58 -0800551void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700552 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800553 // Ensure we have a snapshot of the basic geometry layer state. Limit the
554 // snapshots to once per frame for each candidate layer, as layers may
555 // appear on multiple outputs.
556 if (!coverage.latchedLayers.count(layerFE)) {
557 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800558 }
559
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700560 // Only consider the layers on this output
561 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800562 return;
563 }
564
565 // Obtain a read-only pointer to the front-end layer state
566 const auto* layerFEState = layerFE->getCompositionState();
567 if (CC_UNLIKELY(!layerFEState)) {
568 return;
569 }
570
571 // handle hidden surfaces by setting the visible region to empty
572 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700573 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800574 }
575
Vishnu Naird47bcee2023-02-24 18:08:51 +0000576 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
577 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000578
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800579 /*
580 * opaqueRegion: area of a surface that is fully opaque.
581 */
582 Region opaqueRegion;
583
584 /*
585 * visibleRegion: area of a surface that is visible on screen and not fully
586 * transparent. This is essentially the layer's footprint minus the opaque
587 * regions above it. Areas covered by a translucent surface are considered
588 * visible.
589 */
590 Region visibleRegion;
591
592 /*
593 * coveredRegion: area of a surface that is covered by all visible regions
594 * above it (which includes the translucent areas).
595 */
596 Region coveredRegion;
597
598 /*
599 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500600 * transparent.
601 * This is used to tell when the layer has no visible non-transparent
602 * regions and can be removed from the layer list. It does not affect the
603 * visibleRegion of this layer or any layers beneath it. The hint may not
604 * be correct if apps don't respect the SurfaceView restrictions (which,
605 * sadly, some don't).
606 *
607 * In addition, it is used on DISPLAY_DECORATION layers to specify the
608 * blockingRegion, allowing the DPU to skip it to save power. Once we have
609 * hardware that supports a blockingRegion on frames with AFBC, it may be
610 * useful to use this for other layers, too, so long as we can prevent
611 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800612 */
613 Region transparentRegion;
614
Vishnu Naira483b4a2019-12-12 15:07:52 -0800615 /*
616 * shadowRegion: Region cast by the layer's shadow.
617 */
618 Region shadowRegion;
619
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000620 /**
621 * covered region above excluding internal display overlay layers
622 */
623 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
624
Lloyd Piquede196652020-01-22 17:29:58 -0800625 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800626
627 // Get the visible region
628 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
629 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800630 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800631 visibleRegion.set(visibleRect);
632
Vishnu Naird9e4f462023-10-06 04:05:45 +0000633 if (layerFEState->shadowSettings.length > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800634 // if the layer casts a shadow, offset the layers visible region and
635 // calculate the shadow region.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000636 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowSettings.length) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800637 Rect visibleRectWithShadows(visibleRect);
638 visibleRectWithShadows.inset(inset, inset, inset, inset);
639 visibleRegion.set(visibleRectWithShadows);
640 shadowRegion = visibleRegion.subtract(visibleRect);
641 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800642
643 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700644 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800645 }
646
647 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800648 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800649 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000650 // Clip the transparent region to geomLayerBounds first
651 // The transparent region may be influenced by applications, for
652 // instance, by overriding ViewGroup#gatherTransparentRegion with a
653 // custom view. Once the layer stack -> display mapping is known, we
654 // must guard against very wrong inputs to prevent underflow or
655 // overflow errors. We do this here by constraining the transparent
656 // region to be within the pre-transform layer bounds, since the
657 // layer bounds are expected to play nicely with the full
658 // transform.
659 const Region clippedTransparentRegionHint =
660 layerFEState->transparentRegionHint.intersect(
661 Rect(layerFEState->geomLayerBounds));
662
663 if (clippedTransparentRegionHint.isEmpty()) {
664 if (!layerFEState->transparentRegionHint.isEmpty()) {
665 ALOGD("Layer: %s had an out of bounds transparent region",
666 layerFE->getDebugName());
667 layerFEState->transparentRegionHint.dump("transparentRegionHint");
668 }
669 transparentRegion.clear();
670 } else {
671 transparentRegion = tr.transform(clippedTransparentRegionHint);
672 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800673 } else {
674 // transformation too complex, can't do the
675 // transparent region optimization.
676 transparentRegion.clear();
677 }
678 }
679
680 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800681 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800682 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800683 // If we one of the simple category of transforms (0/90/180/270 rotation
684 // + any flip), then the opaque region is the layer's footprint.
685 // Otherwise we don't try and compute the opaque region since there may
686 // be errors at the edges, and we treat the entire layer as
687 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800688 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800689 }
690
691 // Clip the covered region to the visible region
692 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
693
694 // Update accumAboveCoveredLayers for next (lower) layer
695 coverage.aboveCoveredLayers.orSelf(visibleRegion);
696
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000697 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
698 coveredRegionExcludingDisplayOverlays =
699 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
700 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
701 }
702
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800703 // subtract the opaque region covered by the layers above us
704 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
705
706 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700707 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800708 }
709
710 // Get coverage information for the layer as previously displayed,
711 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800712 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700713 auto prevOutputLayer =
714 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800715
716 // Get coverage information for the layer as previously displayed
717 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
718 const Region kEmptyRegion;
719 const Region& oldVisibleRegion =
720 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
721 const Region& oldCoveredRegion =
722 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
723
724 // compute this layer's dirty region
725 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800726 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800727 // we need to invalidate the whole region
728 dirty = visibleRegion;
729 // as well, as the old visible region
730 dirty.orSelf(oldVisibleRegion);
731 } else {
732 /* compute the exposed region:
733 * the exposed region consists of two components:
734 * 1) what's VISIBLE now and was COVERED before
735 * 2) what's EXPOSED now less what was EXPOSED before
736 *
737 * note that (1) is conservative, we start with the whole visible region
738 * but only keep what used to be covered by something -- which mean it
739 * may have been exposed.
740 *
741 * (2) handles areas that were not covered by anything but got exposed
742 * because of a resize.
743 *
744 */
745 const Region newExposed = visibleRegion - coveredRegion;
746 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
747 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
748 }
749 dirty.subtractSelf(coverage.aboveOpaqueLayers);
750
751 // accumulate to the screen dirty region
752 coverage.dirtyRegion.orSelf(dirty);
753
754 // Update accumAboveOpaqueLayers for next (lower) layer
755 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
756
757 // Compute the visible non-transparent region
758 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
759
Vishnu Naira483b4a2019-12-12 15:07:52 -0800760 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800761 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700762 const auto& outputState = getState();
763 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000764 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800765 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700766 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800767 }
768
Vishnu Naira483b4a2019-12-12 15:07:52 -0800769 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
770
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800771 // The layer is visible. Either reuse the existing outputLayer if we have
772 // one, or create a new one if we do not.
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600773 auto outputLayer = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800774
775 // Store the layer coverage information into the layer state as some of it
776 // is useful later.
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600777 auto& outputLayerState = outputLayer->editState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800778 outputLayerState.visibleRegion = visibleRegion;
779 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
780 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200781 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000782 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800783 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500784 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400785 layerFEState->compositionType == Composition::DISPLAY_DECORATION
786 ? outputState.transform.transform(
787 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
788 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000789 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
790 outputLayerState.coveredRegionExcludingDisplayOverlays =
791 std::move(coveredRegionExcludingDisplayOverlays);
792 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800793}
794
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600795void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
796 if (bufferIdsToUncache.empty()) {
797 return;
798 }
799 for (auto outputLayer : getOutputLayersOrderedByZ()) {
800 outputLayer->uncacheBuffers(bufferIdsToUncache);
801 }
802}
803
804void Output::commitPictureProfilesToCompositionState() {
805 if (!com_android_graphics_libgui_flags_apply_picture_profiles()) {
806 return;
807 }
808 if (!hasPictureProcessing()) {
809 return;
810 }
811 auto compare = [](const ::android::compositionengine::OutputLayer* lhs,
812 const ::android::compositionengine::OutputLayer* rhs) {
813 return lhs->getPictureProfilePriority() > rhs->getPictureProfilePriority();
814 };
815 std::priority_queue<::android::compositionengine::OutputLayer*,
816 std::vector<::android::compositionengine::OutputLayer*>, decltype(compare)>
817 layersWithProfiles;
818 for (auto outputLayer : getOutputLayersOrderedByZ()) {
819 if (outputLayer->getPictureProfileHandle()) {
820 layersWithProfiles.push(outputLayer);
821 }
822 }
823
824 // TODO(b/337330263): Use the default display picture profile from SurfaceFlinger
825 editState().pictureProfileHandle = PictureProfileHandle::NONE;
826
827 // When layer-specific picture processing is supported, apply as many high priority profiles as
828 // possible to the layers, and ignore the low priority layers.
829 if (getMaxLayerPictureProfiles() > 0) {
830 for (int i = 0; i < getMaxLayerPictureProfiles() && !layersWithProfiles.empty();
831 layersWithProfiles.pop(), ++i) {
832 layersWithProfiles.top()->commitPictureProfileToCompositionState();
833 }
834 // No layer-specific picture processing, so apply the highest priority picture profile to
835 // the entire display.
836 } else if (!layersWithProfiles.empty()) {
837 editState().pictureProfileHandle = layersWithProfiles.top()->getPictureProfileHandle();
838 }
839}
840
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800841void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
842 // The base class does nothing with this call.
843}
844
Dan Stoza269dc4d2021-01-15 15:07:43 -0800845void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000846 SFTRACE_CALL();
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800847 ALOGV(__FUNCTION__);
848
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800849 if (!getState().isEnabled) {
850 return;
851 }
852
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800853 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
854 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
855
Sally Qi0abc4a52024-09-26 16:13:06 -0700856 auto* properties = getOverlaySupport();
857
Lloyd Pique01c77c12019-04-17 12:48:32 -0700858 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700859 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800860 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200861 forceClientComposition,
Sally Qi0abc4a52024-09-26 16:13:06 -0700862 refreshArgs.internalDisplayRotationFlags,
863 properties ? properties->lutProperties : std::nullopt);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800864
865 if (mLayerRequestingBackgroundBlur == layer) {
866 forceClientComposition = false;
867 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800868 }
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600869 commitPictureProfilesToCompositionState();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800870}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800871
Dan Stoza269dc4d2021-01-15 15:07:43 -0800872void Output::planComposition() {
873 if (!mPlanner || !getState().isEnabled) {
874 return;
875 }
876
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000877 SFTRACE_CALL();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800878 ALOGV(__FUNCTION__);
879
880 mPlanner->plan(getOutputLayersOrderedByZ());
881}
882
883void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000884 SFTRACE_CALL();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800885 ALOGV(__FUNCTION__);
886
887 if (!getState().isEnabled) {
888 return;
889 }
890
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600891 if (auto frameTargetPtrOpt = getDisplayId()
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500892 .and_then(PhysicalDisplayId::tryCast)
893 .and_then([&refreshArgs](PhysicalDisplayId id) {
894 return refreshArgs.frameTargets.get(id);
895 })) {
896 editState().earliestPresentTime = frameTargetPtrOpt->get()->earliestPresentTime();
897 editState().expectedPresentTime = frameTargetPtrOpt->get()->expectedPresentTime().ns();
Ady Abraham708ebfb2024-11-14 14:42:49 -0800898 const auto debugPresentDelay = frameTargetPtrOpt->get()->debugPresentDelay();
899 if (debugPresentDelay) {
900 SFTRACE_FORMAT_INSTANT("DEBUG delaying presentation by %.2fms",
901 debugPresentDelay->ns() / 1e6f);
902 editState().expectedPresentTime += debugPresentDelay->ns();
903 }
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500904 }
ramindani4aac32c2023-10-30 14:13:30 -0700905 editState().frameInterval = refreshArgs.frameInterval;
jimmyshiu4e211772023-06-15 15:18:38 +0000906 editState().powerCallback = refreshArgs.powerCallback;
Ady Abraham3645e642021-04-20 18:39:00 -0700907
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600908 applyPictureProfile();
909
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400910 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800911 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400912 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400913 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400914 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700915 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800916 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400917 if (layer == peekThroughLayer) {
918 // No longer needed, although it should not show up again, so
919 // resetting it is not truly needed either.
920 peekThroughLayer = nullptr;
921
922 // peekThroughLayer was already drawn ahead of its z order.
923 continue;
924 }
Dan Stoza6166c312021-01-15 16:34:05 -0800925 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400926 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400927 if (overrideInfo.buffer != nullptr) {
928 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800929 ALOGV("Skipping redundant buffer");
930 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400931 } else {
932 // First layer with the override buffer.
933 if (overrideInfo.peekThroughLayer) {
934 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400935
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400936 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400937 overrideZ = true;
938 includeGeometry = true;
939 constexpr bool isPeekingThrough = true;
940 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
941 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700942 outputLayerHash ^= android::hashCombine(
943 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
944 z, includeGeometry, overrideZ, isPeekingThrough,
945 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400946 }
947
948 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800949 }
Dan Stoza6166c312021-01-15 16:34:05 -0800950 }
951
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400952 constexpr bool isPeekingThrough = false;
953 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700954 if (!skipLayer) {
955 outputLayerHash ^= android::hashCombine(
956 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
957 z, includeGeometry, overrideZ, isPeekingThrough,
958 layer->requiresClientComposition());
959 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800960 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700961 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800962}
963
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800964compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
965 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
966 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400967 const auto* compState = layer->getLayerFE().getCompositionState();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100968
969 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
970 // want to force client composition because of the blur.
971 if (compState->sidebandStream != nullptr) {
972 return nullptr;
973 }
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400974
975 // If RenderEngine cannot render protected content, we cannot blur.
976 if (compState->hasProtectedContent &&
977 !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
978 return nullptr;
979 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000980 if (compState->isOpaque) {
981 continue;
982 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100983 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800984 layerRequestingBgComposition = layer;
985 }
986 }
987 return layerRequestingBgComposition;
988}
989
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800990void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
991 setColorProfile(pickColorProfile(refreshArgs));
992}
993
994// Returns a data space that fits all visible layers. The returned data space
995// can only be one of
996// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
997// - Dataspace::DISPLAY_P3
998// - Dataspace::DISPLAY_BT2020
999// The returned HDR data space is one of
1000// - Dataspace::UNKNOWN
1001// - Dataspace::BT2020_HLG
1002// - Dataspace::BT2020_PQ
1003ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
1004 bool* outIsHdrClientComposition) const {
1005 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
1006 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
1007
Vishnu Naire14c6b32022-08-06 04:20:15 +00001008 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
1009 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
1010 // properties. Return a default dataspace value in this case.
1011 if (!getState().isEnabled) {
1012 return ui::Dataspace::V0_SRGB;
1013 }
1014
Lloyd Pique01c77c12019-04-17 12:48:32 -07001015 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -08001016 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001017 case ui::Dataspace::V0_SCRGB:
1018 case ui::Dataspace::V0_SCRGB_LINEAR:
1019 case ui::Dataspace::BT2020:
1020 case ui::Dataspace::BT2020_ITU:
1021 case ui::Dataspace::BT2020_LINEAR:
1022 case ui::Dataspace::DISPLAY_BT2020:
1023 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
1024 break;
1025 case ui::Dataspace::DISPLAY_P3:
1026 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1027 break;
1028 case ui::Dataspace::BT2020_PQ:
1029 case ui::Dataspace::BT2020_ITU_PQ:
1030 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1031 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -08001032 *outIsHdrClientComposition =
1033 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001034 break;
1035 case ui::Dataspace::BT2020_HLG:
1036 case ui::Dataspace::BT2020_ITU_HLG:
1037 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1038 // When there's mixed PQ content and HLG content, we set the HDR
Sally Qi37d07c02023-10-05 17:32:32 +00001039 // data space to be BT2020_HLG and convert PQ to HLG.
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001040 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
1041 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
1042 }
1043 break;
1044 default:
1045 break;
1046 }
1047 }
1048
1049 return bestDataSpace;
1050}
1051
1052compositionengine::Output::ColorProfile Output::pickColorProfile(
1053 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
1054 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
1055 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Alec Mouri88790f32023-07-21 01:25:14 +00001056 ui::RenderIntent::COLORIMETRIC};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001057 }
1058
1059 ui::Dataspace hdrDataSpace;
1060 bool isHdrClientComposition = false;
1061 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
1062
1063 switch (refreshArgs.forceOutputColorMode) {
1064 case ui::ColorMode::SRGB:
1065 bestDataSpace = ui::Dataspace::V0_SRGB;
1066 break;
1067 case ui::ColorMode::DISPLAY_P3:
1068 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1069 break;
1070 default:
1071 break;
1072 }
1073
1074 // respect hdrDataSpace only when there is no legacy HDR support
1075 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1076 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1077 if (isHdr) {
1078 bestDataSpace = hdrDataSpace;
1079 }
1080
1081 ui::RenderIntent intent;
1082 switch (refreshArgs.outputColorSetting) {
1083 case OutputColorSetting::kManaged:
1084 case OutputColorSetting::kUnmanaged:
1085 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1086 : ui::RenderIntent::COLORIMETRIC;
1087 break;
1088 case OutputColorSetting::kEnhanced:
1089 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1090 break;
1091 default: // vendor display color setting
1092 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1093 break;
1094 }
1095
1096 ui::ColorMode outMode;
1097 ui::Dataspace outDataSpace;
1098 ui::RenderIntent outRenderIntent;
1099 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1100 &outRenderIntent);
1101
Alec Mouri88790f32023-07-21 01:25:14 +00001102 return ColorProfile{outMode, outDataSpace, outRenderIntent};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001103}
1104
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001105void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001106 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001107 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001108 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001109 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001110
1111 // If nothing has changed (!dirty), don't recompose.
1112 // If something changed, but we don't currently have any visible layers,
1113 // and didn't when we last did a composition, then skip it this time.
1114 // The second rule does two things:
1115 // - When all layers are removed from a display, we'll emit one black
1116 // frame, then nothing more until we get new layers.
1117 // - When a display is created with a private layer stack, we won't
1118 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001119 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001120
1121 const char flagPrefix[] = {'-', '+'};
1122 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001123 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1124 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1125 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001126
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001127 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001128
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001129 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001130 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001131 }
1132}
1133
Lloyd Pique66d68602019-02-13 14:23:31 -08001134void Output::prepareFrame() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001135 SFTRACE_CALL();
Lloyd Pique66d68602019-02-13 14:23:31 -08001136 ALOGV(__FUNCTION__);
1137
Vishnu Naira3140382022-02-24 14:07:11 -08001138 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001139 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001140 return;
1141 }
1142
Vishnu Naira3140382022-02-24 14:07:11 -08001143 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1144 bool success = chooseCompositionStrategy(&changes);
1145 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001146 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001147 outputState.previousDeviceRequestedChanges = changes;
1148 outputState.previousDeviceRequestedSuccess = success;
1149 if (success) {
1150 applyCompositionStrategy(changes);
1151 }
1152 finishPrepareFrame();
1153}
Lloyd Pique66d68602019-02-13 14:23:31 -08001154
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001155ftl::Future<std::monostate> Output::presentFrameAndReleaseLayersAsync(bool flushEvenWhenDisabled) {
Yi Kong9dce90f2024-08-14 07:06:52 +08001156 return ftl::Future<bool>(mHwComposerAsyncWorker->send([this, flushEvenWhenDisabled]() {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001157 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001158 return true;
Yi Kong9dce90f2024-08-14 07:06:52 +08001159 }))
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001160 .then([](bool) { return std::monostate{}; });
1161}
1162
Vishnu Naira3140382022-02-24 14:07:11 -08001163std::future<bool> Output::chooseCompositionStrategyAsync(
1164 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1165 return mHwComposerAsyncWorker->send(
1166 [&, changes]() { return chooseCompositionStrategy(changes); });
1167}
1168
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001169GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001170 SFTRACE_CALL();
Vishnu Naira3140382022-02-24 14:07:11 -08001171 ALOGV(__FUNCTION__);
1172 auto& state = editState();
1173 const auto& previousChanges = state.previousDeviceRequestedChanges;
1174 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1175 resetCompositionStrategy();
1176 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1177 if (state.previousDeviceRequestedSuccess) {
1178 applyCompositionStrategy(previousChanges);
1179 }
1180 finishPrepareFrame();
1181
1182 base::unique_fd bufferFence;
1183 std::shared_ptr<renderengine::ExternalTexture> buffer;
1184 updateProtectedContentState();
1185 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1186 GpuCompositionResult compositionResult;
1187 if (dequeueSucceeded) {
1188 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001189 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001190 if (optFd) {
1191 compositionResult.fence = std::move(*optFd);
1192 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001193 }
1194
Vishnu Naira3140382022-02-24 14:07:11 -08001195 auto chooseCompositionSuccess = hwcResult.get();
1196 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001197 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1198 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001199 if (!predictionSucceeded) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001200 SFTRACE_NAME("CompositionStrategyPredictionMiss");
Vishnu Naira3140382022-02-24 14:07:11 -08001201 resetCompositionStrategy();
1202 if (chooseCompositionSuccess) {
1203 applyCompositionStrategy(changes);
1204 }
1205 finishPrepareFrame();
1206 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1207 compositionResult.buffer = buffer;
1208 } else {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001209 SFTRACE_NAME("CompositionStrategyPredictionHit");
Vishnu Naira3140382022-02-24 14:07:11 -08001210 }
1211 state.previousDeviceRequestedChanges = std::move(changes);
1212 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1213 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001214}
1215
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001216void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1217 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1218 return;
1219 }
1220
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001221 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001222 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001223 base::unique_fd bufferFence;
1224 std::shared_ptr<renderengine::ExternalTexture> buffer;
1225 updateProtectedContentState();
1226 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001227 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Alec Mourif97df4d2023-09-06 02:10:05 +00001228 mRenderSurface->queueBuffer(base::unique_fd(), getHdrSdrRatio(buffer));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001229 }
1230 }
1231
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001232 constexpr bool kFlushEvenWhenDisabled = false;
1233 presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001234
1235 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1236
1237 prepareFrame();
1238}
1239
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001240void Output::finishFrame(GpuCompositionResult&& result) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001241 SFTRACE_CALL();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001242 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001243 const auto& outputState = getState();
1244 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001245 return;
1246 }
1247
Vishnu Naira3140382022-02-24 14:07:11 -08001248 std::optional<base::unique_fd> optReadyFence;
1249 std::shared_ptr<renderengine::ExternalTexture> buffer;
1250 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001251 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001252 optReadyFence = std::move(result.fence);
1253 } else {
1254 if (result.bufferAvailable()) {
1255 buffer = std::move(result.buffer);
1256 bufferFence = std::move(result.fence);
1257 } else {
1258 updateProtectedContentState();
1259 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1260 return;
1261 }
1262 }
1263 // Repaint the framebuffer (if needed), getting the optional fence for when
1264 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001265 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001266 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001267 if (!optReadyFence) {
1268 return;
1269 }
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001270 if (isPowerHintSessionEnabled() && !isPowerHintSessionGpuReportingEnabled()) {
Matt Buckley50c44062022-01-17 20:48:10 +00001271 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001272 setHintSessionGpuFence(
1273 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001274 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001275 // swap buffers (presentation)
Alec Mourif97df4d2023-09-06 02:10:05 +00001276 mRenderSurface->queueBuffer(std::move(*optReadyFence), getHdrSdrRatio(buffer));
Lloyd Piqued3d69882019-02-28 16:03:46 -08001277}
1278
Vishnu Naira3140382022-02-24 14:07:11 -08001279void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001280 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001281 auto& renderEngine = getCompositionEngine().getRenderEngine();
1282 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1283
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001284 bool isProtected;
1285 if (FlagManager::getInstance().display_protected()) {
1286 isProtected = outputState.isProtected;
1287 } else {
1288 isProtected = outputState.isSecure;
1289 }
1290
1291 // We need to set the render surface as protected (DRM) if all the following conditions are met:
1292 // 1. The display is protected (in legacy, check if the display is secure)
1293 // 2. Protected content is supported
1294 // 3. At least one layer has protected content.
1295 if (isProtected && supportsProtectedContent) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001296 auto layers = getOutputLayersOrderedByZ();
1297 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
Eason Chiu45099662023-10-23 08:55:48 +08001298 return layer->getLayerFE().getCompositionState()->hasProtectedContent &&
1299 (!FlagManager::getInstance().protected_if_client() ||
1300 layer->requiresClientComposition());
Lloyd Piquee9eff972020-05-05 12:36:44 -07001301 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001302 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001303 mRenderSurface->setProtected(needsProtected);
1304 }
1305 }
Vishnu Naira3140382022-02-24 14:07:11 -08001306}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001307
Vishnu Naira3140382022-02-24 14:07:11 -08001308bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1309 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1310 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001311
1312 // If we aren't doing client composition on this output, but do have a
1313 // flipClientTarget request for this frame on this output, we still need to
1314 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001315 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1316 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1317 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001318 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1319 "client composition for this frame",
1320 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001321 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001322 }
1323 }
Vishnu Naira3140382022-02-24 14:07:11 -08001324 return true;
1325}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001326
Vishnu Naira3140382022-02-24 14:07:11 -08001327std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001328 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1329 base::unique_fd& fd) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001330 SFTRACE_CALL();
Vishnu Naira3140382022-02-24 14:07:11 -08001331 ALOGV(__FUNCTION__);
1332
1333 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001334 const TracedOrdinal<bool> hasClientComposition = {
1335 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1336 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001337 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001338 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001339 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001340 }
1341
Vishnu Naira3140382022-02-24 14:07:11 -08001342 if (tex == nullptr) {
1343 ALOGW("Buffer not valid for display [%s], bailing out of "
1344 "client composition for this frame",
1345 mName.c_str());
1346 return {};
1347 }
1348
Lloyd Pique688abd42019-02-15 15:42:24 -08001349 ALOGV("hasClientComposition");
1350
Patrick Williams7584c6a2022-10-29 02:10:58 +00001351 renderengine::DisplaySettings clientCompositionDisplay =
Alec Mourif97df4d2023-09-06 02:10:05 +00001352 generateClientCompositionDisplaySettings(tex);
Lloyd Pique688abd42019-02-15 15:42:24 -08001353
Lloyd Pique688abd42019-02-15 15:42:24 -08001354 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001355 auto& renderEngine = getCompositionEngine().getRenderEngine();
1356 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001357 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001358 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001359 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001360 clientCompositionDisplay.outputDataspace,
1361 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001362 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1363
Vishnu Naira3140382022-02-24 14:07:11 -08001364 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001365 // Check if the client composition requests were rendered into the provided graphic buffer. If
1366 // so, we can reuse the buffer and avoid client composition.
1367 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001368 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1369 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001370 clientCompositionLayers)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001371 SFTRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001372 outputCompositionState.reusedClientComposition = true;
1373 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001374 // b/239944175 pass the fence associated with the buffer.
1375 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001376 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001377 SFTRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001378 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001379 clientCompositionLayers);
1380 }
1381
Lloyd Pique688abd42019-02-15 15:42:24 -08001382 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001383 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1384 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1385 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001386 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001387 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1388 [outputDataspace =
1389 clientCompositionDisplay.outputDataspace](const auto& layer) {
1390 return layer.sourceDataspace != outputDataspace;
1391 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001392 if (expensiveRenderingExpected) {
1393 setExpensiveRenderingExpected(true);
1394 }
1395
Sally Qi59a9f502021-10-12 18:53:23 +00001396 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1397 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001398 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001399 std::back_inserter(clientRenderEngineLayers),
1400 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1401 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001402 });
1403
Alec Mourie4034bb2019-11-19 12:45:54 -08001404 const nsecs_t renderEngineStart = systemTime();
Patrick Williams2e9748f2022-08-09 22:48:18 +00001405 auto fenceResult = renderEngine
1406 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Alec Mourif29700f2023-08-17 21:53:31 +00001407 std::move(fd))
Patrick Williams2e9748f2022-08-09 22:48:18 +00001408 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001409
1410 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001411 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001412 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001413 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001414 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
Xiang Wangaab31162024-03-12 19:48:08 -07001415 if (isPowerHintSessionEnabled()) {
1416 if (fence != Fence::NO_FENCE && fence->isValid() &&
1417 !outputCompositionState.reusedClientComposition) {
1418 setHintSessionRequiresRenderEngine(true);
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001419 if (isPowerHintSessionGpuReportingEnabled()) {
Xiang Wangaab31162024-03-12 19:48:08 -07001420 // the order of the two calls here matters as we should check if the previously
1421 // tracked fence has signaled first and archive the previous start time
1422 setHintSessionGpuStart(TimePoint::now());
1423 setHintSessionGpuFence(
1424 std::make_unique<FenceTime>(sp<Fence>::make(dup(fence->get()))));
1425 }
1426 }
1427 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001428
Patrick Williams74c0bf62022-11-02 23:59:26 +00001429 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1430 if (fence->isValid()) {
1431 timeStats->recordRenderEngineDuration(renderEngineStart,
1432 std::make_shared<FenceTime>(fence));
1433 } else {
1434 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1435 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001436 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001437
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001438 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1439 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001440 }
1441
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001442 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001443}
1444
Alec Mourif97df4d2023-09-06 02:10:05 +00001445renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings(
1446 const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
Patrick Williams7584c6a2022-10-29 02:10:58 +00001447 const auto& outputState = getState();
1448
1449 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001450 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001451 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1452 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1453 clientCompositionDisplay.orientation =
1454 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1455 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1456 ? outputState.dataspace
1457 : ui::Dataspace::UNKNOWN;
1458
1459 // If we have a valid current display brightness use that, otherwise fall back to the
1460 // display's max desired
1461 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1462 ? outputState.displayBrightnessNits
1463 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1464 clientCompositionDisplay.maxLuminance =
1465 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif97df4d2023-09-06 02:10:05 +00001466
1467 float hdrSdrRatioMultiplier = 1.0f / getHdrSdrRatio(buffer);
1468 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetBrightness *
1469 outputState.displayBrightnessNits * hdrSdrRatioMultiplier;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001470 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1471 clientCompositionDisplay.renderIntent =
1472 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1473 outputState.renderIntent);
1474
1475 // Compute the global color transform matrix.
1476 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001477 clientCompositionDisplay.deviceHandlesColorTransform =
1478 outputState.usesDeviceComposition || getSkipColorTransform();
1479 return clientCompositionDisplay;
1480}
1481
Vishnu Nair9b079a22020-01-21 14:36:08 -08001482std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001483 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001484 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001485 ALOGV("Rendering client layers");
1486
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001487 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001488 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001489 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001490
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001491 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001492 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001493
Lloyd Pique01c77c12019-04-17 12:48:32 -07001494 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001495 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001496 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001497 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001498 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001499
Lloyd Piquea2468662019-03-07 21:31:06 -08001500 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001501 ALOGV("Layer: %s", layerFE.getDebugName());
1502 if (clip.isEmpty()) {
1503 ALOGV(" Skipping for empty clip");
1504 firstLayer = false;
1505 continue;
1506 }
1507
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001508 disableBlurs |= layerFEState->sidebandStream != nullptr;
1509
Vishnu Naira483b4a2019-12-12 15:07:52 -08001510 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001511
1512 // We clear the client target for non-client composed layers if
1513 // requested by the HWC. We skip this if the layer is not an opaque
1514 // rectangle, as by definition the layer must blend with whatever is
1515 // underneath. We also skip the first layer as the buffer target is
1516 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001517 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001518 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001519
1520 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1521
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001522 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1523 // composing the non-shadow content and only draw the shadows.
1524 const bool realContentIsVisible = clientComposition &&
1525 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1526
Lloyd Pique688abd42019-02-15 15:42:24 -08001527 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001528 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1529 if (overrideSettings->bufferId != previousOverrideBufferId) {
1530 previousOverrideBufferId = overrideSettings->bufferId;
1531 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001532 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1533 } else {
1534 ALOGV("Skipping redundant override buffer for [%s] in RE",
1535 layer->getLayerFE().getDebugName());
1536 }
Dan Stoza6166c312021-01-15 16:34:05 -08001537 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001538 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1539 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1540 : (layer->getState().overrideInfo.disableBackgroundBlur
1541 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1542 BlurRegionsOnly
1543 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1544 Enabled);
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001545 bool isProtected = supportsProtectedContent;
1546 if (FlagManager::getInstance().display_protected()) {
1547 isProtected = outputState.isProtected && supportsProtectedContent;
1548 }
Alec Mourif54453c2021-05-13 16:28:28 -07001549 compositionengine::LayerFE::ClientCompositionTargetSettings
1550 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001551 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001552 outputState.needsFiltering,
1553 .isSecure = outputState.isSecure,
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001554 .isProtected = isProtected,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001555 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001556 .dataspace = outputDataspace,
1557 .realContentIsVisible = realContentIsVisible,
1558 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001559 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001560 .whitePointNits = layerState.whitePointNits,
1561 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001562 if (auto clientCompositionSettings =
1563 layerFE.prepareClientComposition(targetSettings)) {
1564 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1565 if (realContentIsVisible) {
1566 layer->editState().clientCompositionTimestamp = systemTime();
1567 }
Dan Stoza6166c312021-01-15 16:34:05 -08001568 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001569 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001570
Tianhua Sunf91f1402022-05-09 05:45:46 +00001571 if (clientComposition) {
1572 outLayerFEs.push_back(&layerFE);
1573 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001574 }
1575
1576 firstLayer = false;
1577 }
1578
1579 return clientCompositionLayers;
1580}
1581
1582void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001583 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001584 if (flashRegion.isEmpty()) {
1585 return;
1586 }
1587
Vishnu Nair9b079a22020-01-21 14:36:08 -08001588 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001589 layerSettings.source.buffer.buffer = nullptr;
1590 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1591 layerSettings.alpha = half(1.0);
1592
1593 for (const auto& rect : flashRegion) {
1594 layerSettings.geometry.boundaries = rect.toFloatRect();
1595 clientCompositionLayers.push_back(layerSettings);
1596 }
1597}
1598
1599void Output::setExpensiveRenderingExpected(bool) {
1600 // The base class does nothing with this call.
1601}
1602
Xiang Wangaab31162024-03-12 19:48:08 -07001603void Output::setHintSessionGpuStart(TimePoint) {
1604 // The base class does nothing with this call.
1605}
1606
Matt Buckley50c44062022-01-17 20:48:10 +00001607void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1608 // The base class does nothing with this call.
1609}
1610
Xiang Wangaab31162024-03-12 19:48:08 -07001611void Output::setHintSessionRequiresRenderEngine(bool) {
1612 // The base class does nothing with this call.
1613}
1614
Matt Buckley50c44062022-01-17 20:48:10 +00001615bool Output::isPowerHintSessionEnabled() {
1616 return false;
1617}
1618
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001619bool Output::isPowerHintSessionGpuReportingEnabled() {
1620 return false;
1621}
1622
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001623void Output::presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001624 SFTRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001625 ALOGV(__FUNCTION__);
1626
1627 if (!getState().isEnabled) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001628 if (flushEvenWhenDisabled && FlagManager::getInstance().flush_buffer_slots_to_uncache()) {
1629 // Some commands, like clearing buffer slots, should still be executed
1630 // even if the display is not enabled.
1631 executeCommands();
1632 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001633 return;
1634 }
1635
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001636 auto& outputState = editState();
1637 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001638
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001639 auto frame = presentFrame();
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001640
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001641 mRenderSurface->onPresentDisplayCompleted();
1642
Lloyd Pique01c77c12019-04-17 12:48:32 -07001643 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001644 // The layer buffer from the previous frame (if any) is released
1645 // by HWC only when the release fence from this frame (if any) is
1646 // signaled. Always get the release fence from HWC first.
1647 sp<Fence> releaseFence = Fence::NO_FENCE;
1648
1649 if (auto hwcLayer = layer->getHwcLayer()) {
1650 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1651 releaseFence = f->second;
1652 }
1653 }
1654
1655 // If the layer was client composited in the previous frame, we
1656 // need to merge with the previous client target acquire fence.
1657 // Since we do not track that, always merge with the current
1658 // client target acquire fence when it is available, even though
1659 // this is suboptimal.
1660 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001661 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001662 releaseFence =
1663 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1664 }
Melody Hsu0077fde2024-10-17 21:42:50 +00001665 layer->getLayerFE().setReleaseFence(releaseFence);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001666 }
1667
1668 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001669 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001670 // supply them with the present fence.
1671 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001672 if (const auto layer = weakLayer.promote()) {
Melody Hsu0077fde2024-10-17 21:42:50 +00001673 layer->setReleaseFence(frame.presentFence);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001674 }
1675 }
1676
1677 // Clear out the released layers now that we're done with them.
1678 mReleasedLayers.clear();
1679}
1680
Alec Mouriaa831582021-06-07 16:23:01 -07001681void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III43b5d522023-04-10 15:53:45 -04001682 const auto& outputState = getState();
1683 if (mPlanner && outputState.isEnabled) {
1684 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1685 outputState.usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001686 }
1687}
1688
Lloyd Pique32cbe282018-10-19 13:09:22 -07001689void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001690 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001691 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001692}
1693
Vishnu Naira3140382022-02-24 14:07:11 -08001694void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001695 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001696 auto& outputState = editState();
1697 outputState.usesClientComposition = true;
1698 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001699 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001700}
1701
Lloyd Pique688abd42019-02-15 15:42:24 -08001702bool Output::getSkipColorTransform() const {
1703 return true;
1704}
1705
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001706compositionengine::Output::FrameFences Output::presentFrame() {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001707 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001708 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001709 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1710 }
1711 return result;
1712}
1713
Vishnu Naira3140382022-02-24 14:07:11 -08001714void Output::setPredictCompositionStrategy(bool predict) {
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001715 mPredictCompositionStrategy = predict;
1716 updateHwcAsyncWorker();
1717}
1718
1719void Output::updateHwcAsyncWorker() {
1720 if (mPredictCompositionStrategy || mOffloadPresent) {
1721 if (!mHwComposerAsyncWorker) {
1722 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1723 }
Vishnu Naira3140382022-02-24 14:07:11 -08001724 } else {
1725 mHwComposerAsyncWorker.reset(nullptr);
1726 }
1727}
1728
Alec Mouridda07d92022-04-25 22:39:25 +00001729void Output::setTreat170mAsSrgb(bool enable) {
1730 editState().treat170mAsSrgb = enable;
1731}
1732
Sally Qi0abc4a52024-09-26 16:13:06 -07001733const aidl::android::hardware::graphics::composer3::OverlayProperties* Output::getOverlaySupport() {
1734 return nullptr;
1735}
1736
Vishnu Naira3140382022-02-24 14:07:11 -08001737bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001738 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1739 uint64_t outputLayerHash = getState().outputLayerHash;
1740 editState().lastOutputLayerHash = outputLayerHash;
1741
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001742 if (!getState().isEnabled || !mPredictCompositionStrategy) {
Vishnu Naira3140382022-02-24 14:07:11 -08001743 ALOGV("canPredictCompositionStrategy disabled");
1744 return false;
1745 }
1746
1747 if (!getState().previousDeviceRequestedChanges) {
1748 ALOGV("canPredictCompositionStrategy previous changes not available");
1749 return false;
1750 }
1751
1752 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1753 ALOGV("canPredictCompositionStrategy surface does not support");
1754 return false;
1755 }
1756
1757 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1758 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1759 return false;
1760 }
1761
Robert Carrec8ccca2022-05-04 09:36:14 -07001762 if (lastOutputLayerHash != outputLayerHash) {
1763 ALOGV("canPredictCompositionStrategy output layers changed");
1764 return false;
1765 }
1766
Vishnu Naira3140382022-02-24 14:07:11 -08001767 // If no layer uses clientComposition, then don't predict composition strategy
1768 // because we have less work to do in parallel.
1769 if (!anyLayersRequireClientComposition()) {
1770 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1771 return false;
1772 }
1773
Robert Carrec8ccca2022-05-04 09:36:14 -07001774 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001775}
1776
1777bool Output::anyLayersRequireClientComposition() const {
1778 const auto layers = getOutputLayersOrderedByZ();
1779 return std::any_of(layers.begin(), layers.end(),
1780 [](const auto& layer) { return layer->requiresClientComposition(); });
1781}
1782
1783void Output::finishPrepareFrame() {
1784 const auto& state = getState();
1785 if (mPlanner) {
1786 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1787 }
1788 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1789}
1790
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001791bool Output::mustRecompose() const {
1792 return mMustRecompose;
1793}
1794
Alec Mourif97df4d2023-09-06 02:10:05 +00001795float Output::getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
1796 if (buffer == nullptr) {
1797 return 1.0f;
1798 }
1799
1800 if (!FlagManager::getInstance().fp16_client_target()) {
1801 return 1.0f;
1802 }
1803
1804 if (getState().displayBrightnessNits < 0.0f || getState().sdrWhitePointNits <= 0.0f ||
1805 buffer->getPixelFormat() != PIXEL_FORMAT_RGBA_FP16 ||
1806 (static_cast<int32_t>(getState().dataspace) &
1807 static_cast<int32_t>(ui::Dataspace::RANGE_MASK)) !=
1808 static_cast<int32_t>(ui::Dataspace::RANGE_EXTENDED)) {
1809 return 1.0f;
1810 }
1811
1812 return getState().displayBrightnessNits / getState().sdrWhitePointNits;
1813}
1814
Brian Lindahl1a4ffd82024-10-30 11:00:37 -06001815bool Output::hasPictureProcessing() const {
1816 return false;
1817}
1818
1819int32_t Output::getMaxLayerPictureProfiles() const {
1820 return 0;
1821}
1822
1823void Output::applyPictureProfile() {
1824 if (!com_android_graphics_libgui_flags_apply_picture_profiles()) {
1825 return;
1826 }
1827
1828 // TODO(b/337330263): Move this into the Display class and add a Display unit test.
1829 if (!getState().pictureProfileHandle) {
1830 return;
1831 }
1832 if (!getDisplayId()) {
1833 return;
1834 }
1835 if (auto displayId = PhysicalDisplayId::tryCast(*getDisplayId())) {
1836 auto& hwc = getCompositionEngine().getHwComposer();
1837 const status_t error =
1838 hwc.setDisplayPictureProfileHandle(*displayId, getState().pictureProfileHandle);
1839 ALOGE_IF(error, "setDisplayPictureProfileHandle failed for %s: %d, (%s)", getName().c_str(),
1840 error, strerror(-error));
1841 }
1842}
1843
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001844} // namespace impl
1845} // namespace android::compositionengine