blob: ac252aaa454de1f39e4de50b40d1c328e9b37bc8 [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>
daniml39d6a2162021-05-19 15:56:21 +020027#include <compositionengine/UdfpsExtension.h>
Vishnu Naira3140382022-02-24 14:07:11 -080028#include <compositionengine/impl/HwcAsyncWorker.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070029#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070030#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080031#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070032#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080033#include <compositionengine/impl/planner/Planner.h>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050034#include <ftl/algorithm.h>
Sally Qi59a9f502021-10-12 18:53:23 +000035#include <ftl/future.h>
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060036#include <ftl/optional.h>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050037#include <scheduler/FrameTargeter.h>
38#include <scheduler/Time.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080039
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060040#include <com_android_graphics_libgui_flags.h>
41
Chavi Weingarten545da0e2023-02-09 14:55:57 +000042#include <optional>
Alec Mouria90a5702021-04-16 16:36:21 +000043#include <thread>
44
45#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080046
47// TODO(b/129481165): remove the #pragma below and fix conversion issues
48#pragma clang diagnostic push
49#pragma clang diagnostic ignored "-Wconversion"
50
Lloyd Pique688abd42019-02-15 15:42:24 -080051#include <renderengine/DisplaySettings.h>
52#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080053
54// TODO(b/129481165): remove the #pragma below and fix conversion issues
55#pragma clang diagnostic pop // ignored "-Wconversion"
56
Dan Stoza269dc4d2021-01-15 15:07:43 -080057#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070058#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080059#include <ui/HdrCapabilities.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070060
Lloyd Pique688abd42019-02-15 15:42:24 -080061#include "TracedOrdinal.h"
62
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050063using aidl::android::hardware::graphics::composer3::Composition;
64
Lloyd Piquefeb73d72018-12-04 17:23:44 -080065namespace android::compositionengine {
66
67Output::~Output() = default;
68
69namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080070using CompositionStrategyPredictionState =
71 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080072namespace {
73
74template <typename T>
75class Reversed {
76public:
77 explicit Reversed(const T& container) : mContainer(container) {}
78 auto begin() { return mContainer.rbegin(); }
79 auto end() { return mContainer.rend(); }
80
81private:
82 const T& mContainer;
83};
84
85// Helper for enumerating over a container in reverse order
86template <typename T>
87Reversed<T> reversed(const T& c) {
88 return Reversed<T>(c);
89}
90
Marin Shalamanovb15d2272020-09-17 21:41:52 +020091struct ScaleVector {
92 float x;
93 float y;
94};
95
96// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
97// where to' will have the same size as "to". In the case where "from" and "to"
98// start at the origin to'=to.
99ScaleVector getScale(const Rect& from, const Rect& to) {
100 return {.x = static_cast<float>(to.width()) / from.width(),
101 .y = static_cast<float>(to.height()) / from.height()};
102}
103
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800104} // namespace
105
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700106std::shared_ptr<Output> createOutput(
107 const compositionengine::CompositionEngine& compositionEngine) {
108 return createOutputTemplated<Output>(compositionEngine);
109}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110
111Output::~Output() = default;
112
Lloyd Pique32cbe282018-10-19 13:09:22 -0700113bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700114 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
115 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700116}
117
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600118ftl::Optional<DisplayId> Output::getDisplayId() const {
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700119 return {};
120}
121
Lloyd Pique32cbe282018-10-19 13:09:22 -0700122const std::string& Output::getName() const {
123 return mName;
124}
125
126void Output::setName(const std::string& name) {
127 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400128 auto displayIdOpt = getDisplayId();
Leon Scroggins IIIc03d4652023-01-05 13:03:53 -0500129 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
130 to_string(*displayIdOpt).c_str())
131 : mName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700132}
133
134void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700135 auto& outputState = editState();
136 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700137 return;
138 }
139
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700141 dirtyEntireOutput();
142}
143
Alec Mouri023c1882021-05-08 16:36:33 -0700144void Output::setLayerCachingEnabled(bool enabled) {
145 if (enabled == (mPlanner != nullptr)) {
146 return;
147 }
148
149 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700150 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700151 if (mRenderSurface) {
152 mPlanner->setDisplaySize(mRenderSurface->getSize());
153 }
154 } else {
155 mPlanner.reset();
156 }
Alec Mouric773472b2021-05-19 14:29:05 -0700157
158 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
159 if (!outputLayer) {
160 continue;
161 }
162
163 outputLayer->editState().overrideInfo = {};
164 }
Alec Mouri023c1882021-05-08 16:36:33 -0700165}
166
Ady Abrahamdb036a82021-07-16 14:18:34 -0700167void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
168 if (mPlanner) {
169 mPlanner->setTexturePoolEnabled(enabled);
170 }
171}
172
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200173void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
174 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700175 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200176
Angel Aguayob084e0c2021-08-04 23:27:28 +0000177 outputState.displaySpace.setOrientation(orientation);
178 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200179 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200180
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200181 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000182 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200183 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200184 std::swap(orientedSize.width, orientedSize.height);
185 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000186 outputState.orientedDisplaySpace.setBounds(orientedSize);
187 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200188
189 // Compute displaySpace.content
190 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
191 ui::Transform rotation;
192 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000193 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200194 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
195 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000196 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200197
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200198 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000199 outputState.framebufferSpace.setOrientation(orientation);
200 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200201 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000202 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
203 outputState.framebufferSpace.getBoundsAsRect());
204 outputState.framebufferSpace.setContent(
205 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200206
207 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000208 outputState.layerStackSpace.setContent(layerStackSpaceRect);
209 outputState.layerStackSpace.setBounds(
210 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200211
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200212 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
213 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700214 dirtyEntireOutput();
215}
216
Alec Mouricdf16792021-12-10 13:16:06 -0800217void Output::setNextBrightness(float brightness) {
218 editState().displayBrightness = brightness;
219}
220
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200221void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700222 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200223
224 auto& state = editState();
225
226 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000227 const ui::Size newBounds(size);
228 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200229
230 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000231 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200232 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
233
234 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000235 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200236 ui::Size orientedSize = size;
237 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
238 std::swap(orientedSize.width, orientedSize.height);
239 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000240 const ui::Size newOrientedBounds(orientedSize);
241 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700242
Dan Stoza6166c312021-01-15 16:34:05 -0800243 if (mPlanner) {
244 mPlanner->setDisplaySize(size);
245 }
246
Lloyd Pique32cbe282018-10-19 13:09:22 -0700247 dirtyEntireOutput();
248}
249
Garfield Tan54edd912020-10-21 16:31:41 -0700250ui::Transform::RotationFlags Output::getTransformHint() const {
251 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
252}
253
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700254void Output::setLayerFilter(ui::LayerFilter filter) {
255 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700256 dirtyEntireOutput();
257}
258
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800259void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700260 auto& colorTransformMatrix = editState().colorTransformMatrix;
261 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700262 return;
263 }
264
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700265 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800266
267 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700268}
269
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800270void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700271 auto& outputState = editState();
272 if (outputState.colorMode == colorProfile.mode &&
273 outputState.dataspace == colorProfile.dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000274 outputState.renderIntent == colorProfile.renderIntent) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800275 return;
276 }
277
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700278 outputState.colorMode = colorProfile.mode;
279 outputState.dataspace = colorProfile.dataspace;
280 outputState.renderIntent = colorProfile.renderIntent;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700281
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800282 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700283
Lloyd Pique32cbe282018-10-19 13:09:22 -0700284 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800285 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
286 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800287
288 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700289}
290
John Reckac09e452021-04-07 16:35:37 -0400291void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
292 auto& outputState = editState();
293 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
294 outputState.displayBrightnessNits == displayBrightnessNits) {
295 // Nothing changed
296 return;
297 }
298 outputState.sdrWhitePointNits = sdrWhitePointNits;
299 outputState.displayBrightnessNits = displayBrightnessNits;
300 dirtyEntireOutput();
301}
302
Lloyd Pique32cbe282018-10-19 13:09:22 -0700303void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700304 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
305 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700306
307 dumpBase(out);
308}
309
310void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700311 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700312 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700313
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700314 if (mDisplayColorProfile) {
315 mDisplayColorProfile->dump(out);
316 } else {
317 out.append(" No display color profile!\n");
318 }
319
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700320 out += '\n';
321
Lloyd Pique31cb2942018-10-19 17:23:03 -0700322 if (mRenderSurface) {
323 mRenderSurface->dump(out);
324 } else {
325 out.append(" No render surface!\n");
326 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800327
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700328 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700329 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800330 if (!outputLayer) {
331 continue;
332 }
333 outputLayer->dump(out);
334 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700335}
336
Dan Stoza269dc4d2021-01-15 15:07:43 -0800337void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
338 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700339 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800340 return;
341 }
342 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
343 mPlanner->dump(args, out);
344}
345
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700346compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
347 return mDisplayColorProfile.get();
348}
349
350void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
351 mDisplayColorProfile = std::move(mode);
352}
353
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800354const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
355 return mReleasedLayers;
356}
357
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700358void Output::setDisplayColorProfileForTest(
359 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
360 mDisplayColorProfile = std::move(mode);
361}
362
Lloyd Pique31cb2942018-10-19 17:23:03 -0700363compositionengine::RenderSurface* Output::getRenderSurface() const {
364 return mRenderSurface.get();
365}
366
367void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
368 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800369 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000370 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800371 if (mPlanner) {
372 mPlanner->setDisplaySize(size);
373 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700374 dirtyEntireOutput();
375}
376
Vishnu Nair9b079a22020-01-21 14:36:08 -0800377void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
378 if (cacheSize == 0) {
379 mClientCompositionRequestCache.reset();
380 } else {
381 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
382 }
383};
384
Lloyd Pique31cb2942018-10-19 17:23:03 -0700385void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
386 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700387}
388
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700389Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700390 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000391 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700392}
393
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700394bool Output::includesLayer(ui::LayerFilter filter) const {
395 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700396}
397
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700398bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800399 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700400 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800401}
402
Lloyd Piquedf336d92019-03-07 21:38:42 -0800403std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800404 const sp<LayerFE>& layerFE) const {
405 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800406}
407
Lloyd Piquede196652020-01-22 17:29:58 -0800408compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
409 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700410 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800411}
412
Lloyd Pique01c77c12019-04-17 12:48:32 -0700413std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800414 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700415 for (size_t i = 0; i < getOutputLayerCount(); i++) {
416 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800417 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700418 return i;
419 }
420 }
421 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800422}
423
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800424void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
425 mReleasedLayers = std::move(layers);
426}
427
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800428void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
429 LayerFESet& geomSnapshots) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000430 SFTRACE_CALL();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800431 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800432
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800433 rebuildLayerStacks(refreshArgs, geomSnapshots);
Brian Lindahl439afad2022-11-14 11:16:55 -0700434 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800435}
436
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400437ftl::Future<std::monostate> Output::present(
438 const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500439 const auto stringifyExpectedPresentTime = [this, &refreshArgs]() -> std::string {
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600440 return getDisplayId()
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500441 .and_then(PhysicalDisplayId::tryCast)
442 .and_then([&refreshArgs](PhysicalDisplayId id) {
443 return refreshArgs.frameTargets.get(id);
444 })
445 .transform([](const auto& frameTargetPtr) {
446 return frameTargetPtr.get()->expectedPresentTime();
447 })
448 .transform([](TimePoint expectedPresentTime) {
449 return base::StringPrintf(" vsyncIn %.2fms",
450 ticks<std::milli, float>(expectedPresentTime -
451 TimePoint::now()));
452 })
453 .or_else([] {
454 // There is no vsync for this output.
455 return std::make_optional(std::string());
456 })
457 .value();
458 };
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000459 SFTRACE_FORMAT("%s for %s%s", __func__, mNamePlusId.c_str(),
460 stringifyExpectedPresentTime().c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461 ALOGV(__FUNCTION__);
462
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800463 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800464 updateCompositionState(refreshArgs);
465 planComposition();
466 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800467 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800468 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800469
Xiang Wangaab31162024-03-12 19:48:08 -0700470 if (isPowerHintSessionEnabled()) {
471 // always reset the flag before the composition prediction
472 setHintSessionRequiresRenderEngine(false);
473 }
Vishnu Naira3140382022-02-24 14:07:11 -0800474 GpuCompositionResult result;
475 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
476 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000477 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800478 } else {
479 prepareFrame();
480 }
481
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800482 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000483 finishFrame(std::move(result));
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400484 ftl::Future<std::monostate> future;
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400485 const bool flushEvenWhenDisabled = !refreshArgs.bufferIdsToUncache.empty();
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400486 if (mOffloadPresent) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400487 future = presentFrameAndReleaseLayersAsync(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400488
489 // Only offload for this frame. The next frame will determine whether it
490 // needs to be offloaded. Leave the HwcAsyncWorker in place. For one thing,
491 // it is currently presenting. Further, it may be needed next frame, and
492 // we don't want to churn.
493 mOffloadPresent = false;
494 } else {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400495 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400496 future = ftl::yield<std::monostate>({});
497 }
Alec Mouriaa831582021-06-07 16:23:01 -0700498 renderCachedSets(refreshArgs);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400499 return future;
500}
501
502void Output::offloadPresentNextFrame() {
503 mOffloadPresent = true;
504 updateHwcAsyncWorker();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800505}
506
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800507void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
508 LayerFESet& layerFESet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700509 auto& outputState = editState();
510
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800511 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700512 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800513 return;
514 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000515 SFTRACE_CALL();
Vishnu Naird9a640b2023-07-21 14:20:27 +0000516 ALOGV(__FUNCTION__);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800517
518 // Process the layers to determine visibility and coverage
519 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000520 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
521 ? std::make_optional<Region>()
522 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800523 collectVisibleLayers(refreshArgs, coverage);
524
525 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700526 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000527 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800528 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
529
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700530 outputState.undefinedRegion = undefinedRegion;
531 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800532}
533
534void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
535 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800536 // Evaluate the layers from front to back to determine what is visible. This
537 // also incrementally calculates the coverage information for each layer as
538 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800539 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700540 // Incrementally process the coverage for each layer
541 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800542
543 // TODO(b/121291683): Stop early if the output is completely covered and
544 // no more layers could even be visible underneath the ones on top.
545 }
546
Lloyd Pique01c77c12019-04-17 12:48:32 -0700547 setReleasedLayers(refreshArgs);
548
549 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800550}
551
Lloyd Piquede196652020-01-22 17:29:58 -0800552void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700553 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800554 // Ensure we have a snapshot of the basic geometry layer state. Limit the
555 // snapshots to once per frame for each candidate layer, as layers may
556 // appear on multiple outputs.
557 if (!coverage.latchedLayers.count(layerFE)) {
558 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800559 }
560
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700561 // Only consider the layers on this output
562 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800563 return;
564 }
565
566 // Obtain a read-only pointer to the front-end layer state
567 const auto* layerFEState = layerFE->getCompositionState();
568 if (CC_UNLIKELY(!layerFEState)) {
569 return;
570 }
571
572 // handle hidden surfaces by setting the visible region to empty
573 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700574 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800575 }
576
Vishnu Naird47bcee2023-02-24 18:08:51 +0000577 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
578 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000579
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800580 /*
581 * opaqueRegion: area of a surface that is fully opaque.
582 */
583 Region opaqueRegion;
584
585 /*
586 * visibleRegion: area of a surface that is visible on screen and not fully
587 * transparent. This is essentially the layer's footprint minus the opaque
588 * regions above it. Areas covered by a translucent surface are considered
589 * visible.
590 */
591 Region visibleRegion;
592
593 /*
594 * coveredRegion: area of a surface that is covered by all visible regions
595 * above it (which includes the translucent areas).
596 */
597 Region coveredRegion;
598
599 /*
600 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500601 * transparent.
602 * This is used to tell when the layer has no visible non-transparent
603 * regions and can be removed from the layer list. It does not affect the
604 * visibleRegion of this layer or any layers beneath it. The hint may not
605 * be correct if apps don't respect the SurfaceView restrictions (which,
606 * sadly, some don't).
607 *
608 * In addition, it is used on DISPLAY_DECORATION layers to specify the
609 * blockingRegion, allowing the DPU to skip it to save power. Once we have
610 * hardware that supports a blockingRegion on frames with AFBC, it may be
611 * useful to use this for other layers, too, so long as we can prevent
612 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800613 */
614 Region transparentRegion;
615
Vishnu Naira483b4a2019-12-12 15:07:52 -0800616 /*
617 * shadowRegion: Region cast by the layer's shadow.
618 */
619 Region shadowRegion;
620
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000621 /**
622 * covered region above excluding internal display overlay layers
623 */
624 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
625
Lloyd Piquede196652020-01-22 17:29:58 -0800626 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800627
628 // Get the visible region
629 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
630 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800631 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800632 visibleRegion.set(visibleRect);
633
Vishnu Naird9e4f462023-10-06 04:05:45 +0000634 if (layerFEState->shadowSettings.length > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800635 // if the layer casts a shadow, offset the layers visible region and
636 // calculate the shadow region.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000637 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowSettings.length) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800638 Rect visibleRectWithShadows(visibleRect);
639 visibleRectWithShadows.inset(inset, inset, inset, inset);
640 visibleRegion.set(visibleRectWithShadows);
641 shadowRegion = visibleRegion.subtract(visibleRect);
642 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800643
644 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700645 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800646 }
647
648 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800649 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800650 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000651 // Clip the transparent region to geomLayerBounds first
652 // The transparent region may be influenced by applications, for
653 // instance, by overriding ViewGroup#gatherTransparentRegion with a
654 // custom view. Once the layer stack -> display mapping is known, we
655 // must guard against very wrong inputs to prevent underflow or
656 // overflow errors. We do this here by constraining the transparent
657 // region to be within the pre-transform layer bounds, since the
658 // layer bounds are expected to play nicely with the full
659 // transform.
660 const Region clippedTransparentRegionHint =
661 layerFEState->transparentRegionHint.intersect(
662 Rect(layerFEState->geomLayerBounds));
663
664 if (clippedTransparentRegionHint.isEmpty()) {
665 if (!layerFEState->transparentRegionHint.isEmpty()) {
666 ALOGD("Layer: %s had an out of bounds transparent region",
667 layerFE->getDebugName());
668 layerFEState->transparentRegionHint.dump("transparentRegionHint");
669 }
670 transparentRegion.clear();
671 } else {
672 transparentRegion = tr.transform(clippedTransparentRegionHint);
673 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800674 } else {
675 // transformation too complex, can't do the
676 // transparent region optimization.
677 transparentRegion.clear();
678 }
679 }
680
681 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800682 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800683 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800684 // If we one of the simple category of transforms (0/90/180/270 rotation
685 // + any flip), then the opaque region is the layer's footprint.
686 // Otherwise we don't try and compute the opaque region since there may
687 // be errors at the edges, and we treat the entire layer as
688 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800689 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800690 }
691
692 // Clip the covered region to the visible region
693 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
694
695 // Update accumAboveCoveredLayers for next (lower) layer
696 coverage.aboveCoveredLayers.orSelf(visibleRegion);
697
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000698 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
699 coveredRegionExcludingDisplayOverlays =
700 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
701 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
702 }
703
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800704 // subtract the opaque region covered by the layers above us
705 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
706
707 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700708 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800709 }
710
711 // Get coverage information for the layer as previously displayed,
712 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800713 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700714 auto prevOutputLayer =
715 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800716
717 // Get coverage information for the layer as previously displayed
718 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
719 const Region kEmptyRegion;
720 const Region& oldVisibleRegion =
721 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
722 const Region& oldCoveredRegion =
723 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
724
725 // compute this layer's dirty region
726 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800727 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800728 // we need to invalidate the whole region
729 dirty = visibleRegion;
730 // as well, as the old visible region
731 dirty.orSelf(oldVisibleRegion);
732 } else {
733 /* compute the exposed region:
734 * the exposed region consists of two components:
735 * 1) what's VISIBLE now and was COVERED before
736 * 2) what's EXPOSED now less what was EXPOSED before
737 *
738 * note that (1) is conservative, we start with the whole visible region
739 * but only keep what used to be covered by something -- which mean it
740 * may have been exposed.
741 *
742 * (2) handles areas that were not covered by anything but got exposed
743 * because of a resize.
744 *
745 */
746 const Region newExposed = visibleRegion - coveredRegion;
747 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
748 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
749 }
750 dirty.subtractSelf(coverage.aboveOpaqueLayers);
751
752 // accumulate to the screen dirty region
753 coverage.dirtyRegion.orSelf(dirty);
754
755 // Update accumAboveOpaqueLayers for next (lower) layer
756 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
757
758 // Compute the visible non-transparent region
759 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
760
Vishnu Naira483b4a2019-12-12 15:07:52 -0800761 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800762 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700763 const auto& outputState = getState();
764 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000765 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800766 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700767 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800768 }
769
Vishnu Naira483b4a2019-12-12 15:07:52 -0800770 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
771
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800772 // The layer is visible. Either reuse the existing outputLayer if we have
773 // one, or create a new one if we do not.
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600774 auto outputLayer = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800775
776 // Store the layer coverage information into the layer state as some of it
777 // is useful later.
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600778 auto& outputLayerState = outputLayer->editState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800779 outputLayerState.visibleRegion = visibleRegion;
780 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
781 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200782 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000783 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800784 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500785 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400786 layerFEState->compositionType == Composition::DISPLAY_DECORATION
787 ? outputState.transform.transform(
788 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
789 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000790 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
791 outputLayerState.coveredRegionExcludingDisplayOverlays =
792 std::move(coveredRegionExcludingDisplayOverlays);
793 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800794}
795
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600796void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
797 if (bufferIdsToUncache.empty()) {
798 return;
799 }
800 for (auto outputLayer : getOutputLayersOrderedByZ()) {
801 outputLayer->uncacheBuffers(bufferIdsToUncache);
802 }
803}
804
805void Output::commitPictureProfilesToCompositionState() {
806 if (!com_android_graphics_libgui_flags_apply_picture_profiles()) {
807 return;
808 }
809 if (!hasPictureProcessing()) {
810 return;
811 }
812 auto compare = [](const ::android::compositionengine::OutputLayer* lhs,
813 const ::android::compositionengine::OutputLayer* rhs) {
814 return lhs->getPictureProfilePriority() > rhs->getPictureProfilePriority();
815 };
816 std::priority_queue<::android::compositionengine::OutputLayer*,
817 std::vector<::android::compositionengine::OutputLayer*>, decltype(compare)>
818 layersWithProfiles;
819 for (auto outputLayer : getOutputLayersOrderedByZ()) {
820 if (outputLayer->getPictureProfileHandle()) {
821 layersWithProfiles.push(outputLayer);
822 }
823 }
824
825 // TODO(b/337330263): Use the default display picture profile from SurfaceFlinger
826 editState().pictureProfileHandle = PictureProfileHandle::NONE;
827
828 // When layer-specific picture processing is supported, apply as many high priority profiles as
829 // possible to the layers, and ignore the low priority layers.
830 if (getMaxLayerPictureProfiles() > 0) {
831 for (int i = 0; i < getMaxLayerPictureProfiles() && !layersWithProfiles.empty();
832 layersWithProfiles.pop(), ++i) {
833 layersWithProfiles.top()->commitPictureProfileToCompositionState();
Brian Lindahlf5fdff82024-11-01 09:28:47 -0600834 layersWithProfiles.top()->getLayerFE().onPictureProfileCommitted();
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600835 }
836 // No layer-specific picture processing, so apply the highest priority picture profile to
837 // the entire display.
838 } else if (!layersWithProfiles.empty()) {
839 editState().pictureProfileHandle = layersWithProfiles.top()->getPictureProfileHandle();
Brian Lindahlf5fdff82024-11-01 09:28:47 -0600840 layersWithProfiles.top()->getLayerFE().onPictureProfileCommitted();
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600841 }
842}
843
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800844void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
845 // The base class does nothing with this call.
846}
847
Dan Stoza269dc4d2021-01-15 15:07:43 -0800848void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000849 SFTRACE_CALL();
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800850 ALOGV(__FUNCTION__);
851
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800852 if (!getState().isEnabled) {
853 return;
854 }
855
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800856 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
857 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
858
Sally Qi0abc4a52024-09-26 16:13:06 -0700859 auto* properties = getOverlaySupport();
860
Lloyd Pique01c77c12019-04-17 12:48:32 -0700861 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700862 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800863 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200864 forceClientComposition,
Sally Qi0abc4a52024-09-26 16:13:06 -0700865 refreshArgs.internalDisplayRotationFlags,
866 properties ? properties->lutProperties : std::nullopt);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800867
868 if (mLayerRequestingBackgroundBlur == layer) {
869 forceClientComposition = false;
870 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800871 }
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600872 commitPictureProfilesToCompositionState();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800873}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800874
Dan Stoza269dc4d2021-01-15 15:07:43 -0800875void Output::planComposition() {
876 if (!mPlanner || !getState().isEnabled) {
877 return;
878 }
879
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000880 SFTRACE_CALL();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800881 ALOGV(__FUNCTION__);
882
883 mPlanner->plan(getOutputLayersOrderedByZ());
884}
885
886void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000887 SFTRACE_CALL();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800888 ALOGV(__FUNCTION__);
889
890 if (!getState().isEnabled) {
891 return;
892 }
893
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600894 if (auto frameTargetPtrOpt = getDisplayId()
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500895 .and_then(PhysicalDisplayId::tryCast)
896 .and_then([&refreshArgs](PhysicalDisplayId id) {
897 return refreshArgs.frameTargets.get(id);
898 })) {
899 editState().earliestPresentTime = frameTargetPtrOpt->get()->earliestPresentTime();
900 editState().expectedPresentTime = frameTargetPtrOpt->get()->expectedPresentTime().ns();
Ady Abraham708ebfb2024-11-14 14:42:49 -0800901 const auto debugPresentDelay = frameTargetPtrOpt->get()->debugPresentDelay();
902 if (debugPresentDelay) {
903 SFTRACE_FORMAT_INSTANT("DEBUG delaying presentation by %.2fms",
904 debugPresentDelay->ns() / 1e6f);
905 editState().expectedPresentTime += debugPresentDelay->ns();
906 }
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500907 }
ramindani4aac32c2023-10-30 14:13:30 -0700908 editState().frameInterval = refreshArgs.frameInterval;
jimmyshiu4e211772023-06-15 15:18:38 +0000909 editState().powerCallback = refreshArgs.powerCallback;
Ady Abraham3645e642021-04-20 18:39:00 -0700910
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600911 applyPictureProfile();
912
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400913 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800914 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400915 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400916 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400917 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700918 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800919 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400920 if (layer == peekThroughLayer) {
921 // No longer needed, although it should not show up again, so
922 // resetting it is not truly needed either.
923 peekThroughLayer = nullptr;
924
925 // peekThroughLayer was already drawn ahead of its z order.
926 continue;
927 }
Dan Stoza6166c312021-01-15 16:34:05 -0800928 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400929 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400930 if (overrideInfo.buffer != nullptr) {
931 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800932 ALOGV("Skipping redundant buffer");
933 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400934 } else {
935 // First layer with the override buffer.
936 if (overrideInfo.peekThroughLayer) {
937 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400938
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400939 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400940 overrideZ = true;
941 includeGeometry = true;
942 constexpr bool isPeekingThrough = true;
943 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
944 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700945 outputLayerHash ^= android::hashCombine(
946 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
947 z, includeGeometry, overrideZ, isPeekingThrough,
948 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400949 }
950
951 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800952 }
Dan Stoza6166c312021-01-15 16:34:05 -0800953 }
954
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400955 constexpr bool isPeekingThrough = false;
956 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700957 if (!skipLayer) {
958 outputLayerHash ^= android::hashCombine(
959 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
960 z, includeGeometry, overrideZ, isPeekingThrough,
961 layer->requiresClientComposition());
962 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800963 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700964 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800965}
966
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800967compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
968 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
daniml39d6a2162021-05-19 15:56:21 +0200969 for (size_t i = 0; i < getOutputLayerCount(); i++) {
970 compositionengine::OutputLayer* layer = getOutputLayerOrderedByZByIndex(i);
971 compositionengine::OutputLayer* nextLayer = getOutputLayerOrderedByZByIndex(i + 1);
972
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400973 const auto* compState = layer->getLayerFE().getCompositionState();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100974
975 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
976 // want to force client composition because of the blur.
977 if (compState->sidebandStream != nullptr) {
978 return nullptr;
979 }
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400980
981 // If RenderEngine cannot render protected content, we cannot blur.
982 if (compState->hasProtectedContent &&
983 !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
984 return nullptr;
985 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000986 if (compState->isOpaque) {
987 continue;
988 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100989 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800990 layerRequestingBgComposition = layer;
991 }
daniml39d6a2162021-05-19 15:56:21 +0200992
993 // If the next layer is the Udfps touched layer, enable client composition for it
994 // because that somehow leads to the Udfps touched layer getting device composition
995 // consistently.
996 if ((nextLayer != nullptr && layerRequestingBgComposition == nullptr) &&
997 (strncmp(nextLayer->getLayerFE().getDebugName(), UDFPS_TOUCHED_LAYER_NAME,
998 strlen(UDFPS_TOUCHED_LAYER_NAME)) == 0)) {
999 layerRequestingBgComposition = layer;
1000 break;
1001 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001002 }
1003 return layerRequestingBgComposition;
1004}
1005
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001006void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1007 setColorProfile(pickColorProfile(refreshArgs));
1008}
1009
1010// Returns a data space that fits all visible layers. The returned data space
1011// can only be one of
1012// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
1013// - Dataspace::DISPLAY_P3
1014// - Dataspace::DISPLAY_BT2020
1015// The returned HDR data space is one of
1016// - Dataspace::UNKNOWN
1017// - Dataspace::BT2020_HLG
1018// - Dataspace::BT2020_PQ
1019ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
1020 bool* outIsHdrClientComposition) const {
1021 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
1022 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
1023
Vishnu Naire14c6b32022-08-06 04:20:15 +00001024 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
1025 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
1026 // properties. Return a default dataspace value in this case.
1027 if (!getState().isEnabled) {
1028 return ui::Dataspace::V0_SRGB;
1029 }
1030
Lloyd Pique01c77c12019-04-17 12:48:32 -07001031 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -08001032 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001033 case ui::Dataspace::V0_SCRGB:
1034 case ui::Dataspace::V0_SCRGB_LINEAR:
1035 case ui::Dataspace::BT2020:
1036 case ui::Dataspace::BT2020_ITU:
1037 case ui::Dataspace::BT2020_LINEAR:
1038 case ui::Dataspace::DISPLAY_BT2020:
1039 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
1040 break;
1041 case ui::Dataspace::DISPLAY_P3:
1042 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1043 break;
1044 case ui::Dataspace::BT2020_PQ:
1045 case ui::Dataspace::BT2020_ITU_PQ:
1046 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1047 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -08001048 *outIsHdrClientComposition =
1049 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001050 break;
1051 case ui::Dataspace::BT2020_HLG:
1052 case ui::Dataspace::BT2020_ITU_HLG:
1053 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1054 // When there's mixed PQ content and HLG content, we set the HDR
Sally Qi37d07c02023-10-05 17:32:32 +00001055 // data space to be BT2020_HLG and convert PQ to HLG.
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001056 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
1057 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
1058 }
1059 break;
1060 default:
1061 break;
1062 }
1063 }
1064
1065 return bestDataSpace;
1066}
1067
1068compositionengine::Output::ColorProfile Output::pickColorProfile(
1069 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
1070 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
1071 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Alec Mouri88790f32023-07-21 01:25:14 +00001072 ui::RenderIntent::COLORIMETRIC};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001073 }
1074
1075 ui::Dataspace hdrDataSpace;
1076 bool isHdrClientComposition = false;
1077 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
1078
1079 switch (refreshArgs.forceOutputColorMode) {
1080 case ui::ColorMode::SRGB:
1081 bestDataSpace = ui::Dataspace::V0_SRGB;
1082 break;
1083 case ui::ColorMode::DISPLAY_P3:
1084 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1085 break;
1086 default:
1087 break;
1088 }
1089
1090 // respect hdrDataSpace only when there is no legacy HDR support
1091 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1092 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1093 if (isHdr) {
1094 bestDataSpace = hdrDataSpace;
1095 }
1096
1097 ui::RenderIntent intent;
1098 switch (refreshArgs.outputColorSetting) {
1099 case OutputColorSetting::kManaged:
1100 case OutputColorSetting::kUnmanaged:
1101 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1102 : ui::RenderIntent::COLORIMETRIC;
1103 break;
1104 case OutputColorSetting::kEnhanced:
1105 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1106 break;
1107 default: // vendor display color setting
1108 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1109 break;
1110 }
1111
1112 ui::ColorMode outMode;
1113 ui::Dataspace outDataSpace;
1114 ui::RenderIntent outRenderIntent;
1115 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1116 &outRenderIntent);
1117
Alec Mouri88790f32023-07-21 01:25:14 +00001118 return ColorProfile{outMode, outDataSpace, outRenderIntent};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001119}
1120
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001121void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001122 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001123 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001124 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001125 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001126
1127 // If nothing has changed (!dirty), don't recompose.
1128 // If something changed, but we don't currently have any visible layers,
1129 // and didn't when we last did a composition, then skip it this time.
1130 // The second rule does two things:
1131 // - When all layers are removed from a display, we'll emit one black
1132 // frame, then nothing more until we get new layers.
1133 // - When a display is created with a private layer stack, we won't
1134 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001135 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001136
1137 const char flagPrefix[] = {'-', '+'};
1138 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001139 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1140 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1141 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001142
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001143 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001144
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001145 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001146 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001147 }
1148}
1149
Lloyd Pique66d68602019-02-13 14:23:31 -08001150void Output::prepareFrame() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001151 SFTRACE_CALL();
Lloyd Pique66d68602019-02-13 14:23:31 -08001152 ALOGV(__FUNCTION__);
1153
Vishnu Naira3140382022-02-24 14:07:11 -08001154 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001155 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001156 return;
1157 }
1158
Vishnu Naira3140382022-02-24 14:07:11 -08001159 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1160 bool success = chooseCompositionStrategy(&changes);
1161 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001162 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001163 outputState.previousDeviceRequestedChanges = changes;
1164 outputState.previousDeviceRequestedSuccess = success;
1165 if (success) {
1166 applyCompositionStrategy(changes);
1167 }
1168 finishPrepareFrame();
1169}
Lloyd Pique66d68602019-02-13 14:23:31 -08001170
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001171ftl::Future<std::monostate> Output::presentFrameAndReleaseLayersAsync(bool flushEvenWhenDisabled) {
Yi Kong9dce90f2024-08-14 07:06:52 +08001172 return ftl::Future<bool>(mHwComposerAsyncWorker->send([this, flushEvenWhenDisabled]() {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001173 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001174 return true;
Yi Kong9dce90f2024-08-14 07:06:52 +08001175 }))
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001176 .then([](bool) { return std::monostate{}; });
1177}
1178
Vishnu Naira3140382022-02-24 14:07:11 -08001179std::future<bool> Output::chooseCompositionStrategyAsync(
1180 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1181 return mHwComposerAsyncWorker->send(
1182 [&, changes]() { return chooseCompositionStrategy(changes); });
1183}
1184
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001185GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001186 SFTRACE_CALL();
Vishnu Naira3140382022-02-24 14:07:11 -08001187 ALOGV(__FUNCTION__);
1188 auto& state = editState();
1189 const auto& previousChanges = state.previousDeviceRequestedChanges;
1190 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1191 resetCompositionStrategy();
1192 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1193 if (state.previousDeviceRequestedSuccess) {
1194 applyCompositionStrategy(previousChanges);
1195 }
1196 finishPrepareFrame();
1197
1198 base::unique_fd bufferFence;
1199 std::shared_ptr<renderengine::ExternalTexture> buffer;
1200 updateProtectedContentState();
1201 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1202 GpuCompositionResult compositionResult;
1203 if (dequeueSucceeded) {
1204 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001205 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001206 if (optFd) {
1207 compositionResult.fence = std::move(*optFd);
1208 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001209 }
1210
Vishnu Naira3140382022-02-24 14:07:11 -08001211 auto chooseCompositionSuccess = hwcResult.get();
1212 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001213 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1214 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001215 if (!predictionSucceeded) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001216 SFTRACE_NAME("CompositionStrategyPredictionMiss");
Vishnu Naira3140382022-02-24 14:07:11 -08001217 resetCompositionStrategy();
1218 if (chooseCompositionSuccess) {
1219 applyCompositionStrategy(changes);
1220 }
1221 finishPrepareFrame();
1222 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1223 compositionResult.buffer = buffer;
1224 } else {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001225 SFTRACE_NAME("CompositionStrategyPredictionHit");
Vishnu Naira3140382022-02-24 14:07:11 -08001226 }
1227 state.previousDeviceRequestedChanges = std::move(changes);
1228 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1229 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001230}
1231
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001232void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1233 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1234 return;
1235 }
1236
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001237 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001238 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001239 base::unique_fd bufferFence;
1240 std::shared_ptr<renderengine::ExternalTexture> buffer;
1241 updateProtectedContentState();
1242 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001243 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Alec Mourif97df4d2023-09-06 02:10:05 +00001244 mRenderSurface->queueBuffer(base::unique_fd(), getHdrSdrRatio(buffer));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001245 }
1246 }
1247
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001248 constexpr bool kFlushEvenWhenDisabled = false;
1249 presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001250
1251 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1252
1253 prepareFrame();
1254}
1255
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001256void Output::finishFrame(GpuCompositionResult&& result) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001257 SFTRACE_CALL();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001258 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001259 const auto& outputState = getState();
1260 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001261 return;
1262 }
1263
Vishnu Naira3140382022-02-24 14:07:11 -08001264 std::optional<base::unique_fd> optReadyFence;
1265 std::shared_ptr<renderengine::ExternalTexture> buffer;
1266 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001267 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001268 optReadyFence = std::move(result.fence);
1269 } else {
1270 if (result.bufferAvailable()) {
1271 buffer = std::move(result.buffer);
1272 bufferFence = std::move(result.fence);
1273 } else {
1274 updateProtectedContentState();
1275 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1276 return;
1277 }
1278 }
1279 // Repaint the framebuffer (if needed), getting the optional fence for when
1280 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001281 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001282 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001283 if (!optReadyFence) {
1284 return;
1285 }
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001286 if (isPowerHintSessionEnabled() && !isPowerHintSessionGpuReportingEnabled()) {
Matt Buckley50c44062022-01-17 20:48:10 +00001287 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001288 setHintSessionGpuFence(
1289 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001290 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001291 // swap buffers (presentation)
Alec Mourif97df4d2023-09-06 02:10:05 +00001292 mRenderSurface->queueBuffer(std::move(*optReadyFence), getHdrSdrRatio(buffer));
Lloyd Piqued3d69882019-02-28 16:03:46 -08001293}
1294
Vishnu Naira3140382022-02-24 14:07:11 -08001295void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001296 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001297 auto& renderEngine = getCompositionEngine().getRenderEngine();
1298 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1299
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001300 bool isProtected;
1301 if (FlagManager::getInstance().display_protected()) {
1302 isProtected = outputState.isProtected;
1303 } else {
1304 isProtected = outputState.isSecure;
1305 }
1306
1307 // We need to set the render surface as protected (DRM) if all the following conditions are met:
1308 // 1. The display is protected (in legacy, check if the display is secure)
1309 // 2. Protected content is supported
1310 // 3. At least one layer has protected content.
1311 if (isProtected && supportsProtectedContent) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001312 auto layers = getOutputLayersOrderedByZ();
1313 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
Eason Chiu45099662023-10-23 08:55:48 +08001314 return layer->getLayerFE().getCompositionState()->hasProtectedContent &&
1315 (!FlagManager::getInstance().protected_if_client() ||
1316 layer->requiresClientComposition());
Lloyd Piquee9eff972020-05-05 12:36:44 -07001317 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001318 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001319 mRenderSurface->setProtected(needsProtected);
1320 }
1321 }
Vishnu Naira3140382022-02-24 14:07:11 -08001322}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001323
Vishnu Naira3140382022-02-24 14:07:11 -08001324bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1325 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1326 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001327
1328 // If we aren't doing client composition on this output, but do have a
1329 // flipClientTarget request for this frame on this output, we still need to
1330 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001331 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1332 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1333 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001334 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1335 "client composition for this frame",
1336 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001337 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001338 }
1339 }
Vishnu Naira3140382022-02-24 14:07:11 -08001340 return true;
1341}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001342
Vishnu Naira3140382022-02-24 14:07:11 -08001343std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001344 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1345 base::unique_fd& fd) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001346 SFTRACE_CALL();
Vishnu Naira3140382022-02-24 14:07:11 -08001347 ALOGV(__FUNCTION__);
1348
1349 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001350 const TracedOrdinal<bool> hasClientComposition = {
1351 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1352 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001353 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001354 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001355 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001356 }
1357
Vishnu Naira3140382022-02-24 14:07:11 -08001358 if (tex == nullptr) {
1359 ALOGW("Buffer not valid for display [%s], bailing out of "
1360 "client composition for this frame",
1361 mName.c_str());
1362 return {};
1363 }
1364
Lloyd Pique688abd42019-02-15 15:42:24 -08001365 ALOGV("hasClientComposition");
1366
Patrick Williams7584c6a2022-10-29 02:10:58 +00001367 renderengine::DisplaySettings clientCompositionDisplay =
Alec Mourif97df4d2023-09-06 02:10:05 +00001368 generateClientCompositionDisplaySettings(tex);
Lloyd Pique688abd42019-02-15 15:42:24 -08001369
Lloyd Pique688abd42019-02-15 15:42:24 -08001370 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001371 auto& renderEngine = getCompositionEngine().getRenderEngine();
1372 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001373 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001374 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001375 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001376 clientCompositionDisplay.outputDataspace,
1377 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001378 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1379
Vishnu Naira3140382022-02-24 14:07:11 -08001380 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001381 // Check if the client composition requests were rendered into the provided graphic buffer. If
1382 // so, we can reuse the buffer and avoid client composition.
1383 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001384 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1385 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001386 clientCompositionLayers)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001387 SFTRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001388 outputCompositionState.reusedClientComposition = true;
1389 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001390 // b/239944175 pass the fence associated with the buffer.
1391 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001392 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001393 SFTRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001394 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001395 clientCompositionLayers);
1396 }
1397
Lloyd Pique688abd42019-02-15 15:42:24 -08001398 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001399 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1400 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1401 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001402 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001403 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1404 [outputDataspace =
1405 clientCompositionDisplay.outputDataspace](const auto& layer) {
1406 return layer.sourceDataspace != outputDataspace;
1407 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001408 if (expensiveRenderingExpected) {
1409 setExpensiveRenderingExpected(true);
1410 }
1411
Sally Qi59a9f502021-10-12 18:53:23 +00001412 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1413 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001414 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001415 std::back_inserter(clientRenderEngineLayers),
1416 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1417 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001418 });
1419
Alec Mourie4034bb2019-11-19 12:45:54 -08001420 const nsecs_t renderEngineStart = systemTime();
Patrick Williams2e9748f2022-08-09 22:48:18 +00001421 auto fenceResult = renderEngine
1422 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Alec Mourif29700f2023-08-17 21:53:31 +00001423 std::move(fd))
Patrick Williams2e9748f2022-08-09 22:48:18 +00001424 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001425
1426 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001427 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001428 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001429 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001430 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
Xiang Wangaab31162024-03-12 19:48:08 -07001431 if (isPowerHintSessionEnabled()) {
1432 if (fence != Fence::NO_FENCE && fence->isValid() &&
1433 !outputCompositionState.reusedClientComposition) {
1434 setHintSessionRequiresRenderEngine(true);
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001435 if (isPowerHintSessionGpuReportingEnabled()) {
Xiang Wangaab31162024-03-12 19:48:08 -07001436 // the order of the two calls here matters as we should check if the previously
1437 // tracked fence has signaled first and archive the previous start time
1438 setHintSessionGpuStart(TimePoint::now());
1439 setHintSessionGpuFence(
1440 std::make_unique<FenceTime>(sp<Fence>::make(dup(fence->get()))));
1441 }
1442 }
1443 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001444
Patrick Williams74c0bf62022-11-02 23:59:26 +00001445 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1446 if (fence->isValid()) {
1447 timeStats->recordRenderEngineDuration(renderEngineStart,
1448 std::make_shared<FenceTime>(fence));
1449 } else {
1450 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1451 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001452 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001453
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001454 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1455 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001456 }
1457
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001458 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001459}
1460
Alec Mourif97df4d2023-09-06 02:10:05 +00001461renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings(
1462 const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
Patrick Williams7584c6a2022-10-29 02:10:58 +00001463 const auto& outputState = getState();
1464
1465 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001466 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001467 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1468 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1469 clientCompositionDisplay.orientation =
1470 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1471 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1472 ? outputState.dataspace
1473 : ui::Dataspace::UNKNOWN;
1474
1475 // If we have a valid current display brightness use that, otherwise fall back to the
1476 // display's max desired
1477 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1478 ? outputState.displayBrightnessNits
1479 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1480 clientCompositionDisplay.maxLuminance =
1481 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif97df4d2023-09-06 02:10:05 +00001482
1483 float hdrSdrRatioMultiplier = 1.0f / getHdrSdrRatio(buffer);
1484 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetBrightness *
1485 outputState.displayBrightnessNits * hdrSdrRatioMultiplier;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001486 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1487 clientCompositionDisplay.renderIntent =
1488 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1489 outputState.renderIntent);
1490
1491 // Compute the global color transform matrix.
1492 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001493 clientCompositionDisplay.deviceHandlesColorTransform =
1494 outputState.usesDeviceComposition || getSkipColorTransform();
1495 return clientCompositionDisplay;
1496}
1497
Vishnu Nair9b079a22020-01-21 14:36:08 -08001498std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001499 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001500 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001501 ALOGV("Rendering client layers");
1502
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001503 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001504 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001505 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001506
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001507 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001508 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001509
Lloyd Pique01c77c12019-04-17 12:48:32 -07001510 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001511 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001512 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001513 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001514 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001515
Lloyd Piquea2468662019-03-07 21:31:06 -08001516 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001517 ALOGV("Layer: %s", layerFE.getDebugName());
1518 if (clip.isEmpty()) {
1519 ALOGV(" Skipping for empty clip");
1520 firstLayer = false;
1521 continue;
1522 }
1523
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001524 disableBlurs |= layerFEState->sidebandStream != nullptr;
1525
Vishnu Naira483b4a2019-12-12 15:07:52 -08001526 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001527
1528 // We clear the client target for non-client composed layers if
1529 // requested by the HWC. We skip this if the layer is not an opaque
1530 // rectangle, as by definition the layer must blend with whatever is
1531 // underneath. We also skip the first layer as the buffer target is
1532 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001533 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001534 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001535
1536 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1537
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001538 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1539 // composing the non-shadow content and only draw the shadows.
1540 const bool realContentIsVisible = clientComposition &&
1541 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1542
Lloyd Pique688abd42019-02-15 15:42:24 -08001543 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001544 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1545 if (overrideSettings->bufferId != previousOverrideBufferId) {
1546 previousOverrideBufferId = overrideSettings->bufferId;
1547 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001548 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1549 } else {
1550 ALOGV("Skipping redundant override buffer for [%s] in RE",
1551 layer->getLayerFE().getDebugName());
1552 }
Dan Stoza6166c312021-01-15 16:34:05 -08001553 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001554 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1555 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1556 : (layer->getState().overrideInfo.disableBackgroundBlur
1557 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1558 BlurRegionsOnly
1559 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1560 Enabled);
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001561 bool isProtected = supportsProtectedContent;
1562 if (FlagManager::getInstance().display_protected()) {
1563 isProtected = outputState.isProtected && supportsProtectedContent;
1564 }
Alec Mourif54453c2021-05-13 16:28:28 -07001565 compositionengine::LayerFE::ClientCompositionTargetSettings
1566 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001567 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001568 outputState.needsFiltering,
1569 .isSecure = outputState.isSecure,
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001570 .isProtected = isProtected,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001571 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001572 .dataspace = outputDataspace,
1573 .realContentIsVisible = realContentIsVisible,
1574 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001575 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001576 .whitePointNits = layerState.whitePointNits,
1577 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001578 if (auto clientCompositionSettings =
1579 layerFE.prepareClientComposition(targetSettings)) {
1580 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1581 if (realContentIsVisible) {
1582 layer->editState().clientCompositionTimestamp = systemTime();
1583 }
Dan Stoza6166c312021-01-15 16:34:05 -08001584 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001585 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001586
Tianhua Sunf91f1402022-05-09 05:45:46 +00001587 if (clientComposition) {
1588 outLayerFEs.push_back(&layerFE);
1589 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001590 }
1591
1592 firstLayer = false;
1593 }
1594
1595 return clientCompositionLayers;
1596}
1597
1598void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001599 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001600 if (flashRegion.isEmpty()) {
1601 return;
1602 }
1603
Vishnu Nair9b079a22020-01-21 14:36:08 -08001604 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001605 layerSettings.source.buffer.buffer = nullptr;
1606 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1607 layerSettings.alpha = half(1.0);
1608
1609 for (const auto& rect : flashRegion) {
1610 layerSettings.geometry.boundaries = rect.toFloatRect();
1611 clientCompositionLayers.push_back(layerSettings);
1612 }
1613}
1614
1615void Output::setExpensiveRenderingExpected(bool) {
1616 // The base class does nothing with this call.
1617}
1618
Xiang Wangaab31162024-03-12 19:48:08 -07001619void Output::setHintSessionGpuStart(TimePoint) {
1620 // The base class does nothing with this call.
1621}
1622
Matt Buckley50c44062022-01-17 20:48:10 +00001623void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1624 // The base class does nothing with this call.
1625}
1626
Xiang Wangaab31162024-03-12 19:48:08 -07001627void Output::setHintSessionRequiresRenderEngine(bool) {
1628 // The base class does nothing with this call.
1629}
1630
Matt Buckley50c44062022-01-17 20:48:10 +00001631bool Output::isPowerHintSessionEnabled() {
1632 return false;
1633}
1634
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001635bool Output::isPowerHintSessionGpuReportingEnabled() {
1636 return false;
1637}
1638
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001639void Output::presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001640 SFTRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001641 ALOGV(__FUNCTION__);
1642
1643 if (!getState().isEnabled) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001644 if (flushEvenWhenDisabled && FlagManager::getInstance().flush_buffer_slots_to_uncache()) {
1645 // Some commands, like clearing buffer slots, should still be executed
1646 // even if the display is not enabled.
1647 executeCommands();
1648 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001649 return;
1650 }
1651
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001652 auto& outputState = editState();
1653 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001654
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001655 auto frame = presentFrame();
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001656
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001657 mRenderSurface->onPresentDisplayCompleted();
1658
Lloyd Pique01c77c12019-04-17 12:48:32 -07001659 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001660 // The layer buffer from the previous frame (if any) is released
1661 // by HWC only when the release fence from this frame (if any) is
1662 // signaled. Always get the release fence from HWC first.
1663 sp<Fence> releaseFence = Fence::NO_FENCE;
1664
1665 if (auto hwcLayer = layer->getHwcLayer()) {
1666 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1667 releaseFence = f->second;
1668 }
1669 }
1670
1671 // If the layer was client composited in the previous frame, we
1672 // need to merge with the previous client target acquire fence.
1673 // Since we do not track that, always merge with the current
1674 // client target acquire fence when it is available, even though
1675 // this is suboptimal.
1676 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001677 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001678 releaseFence =
1679 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1680 }
Melody Hsu0077fde2024-10-17 21:42:50 +00001681 layer->getLayerFE().setReleaseFence(releaseFence);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001682 }
1683
1684 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001685 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001686 // supply them with the present fence.
1687 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001688 if (const auto layer = weakLayer.promote()) {
Melody Hsu0077fde2024-10-17 21:42:50 +00001689 layer->setReleaseFence(frame.presentFence);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001690 }
1691 }
1692
1693 // Clear out the released layers now that we're done with them.
1694 mReleasedLayers.clear();
1695}
1696
Alec Mouriaa831582021-06-07 16:23:01 -07001697void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III43b5d522023-04-10 15:53:45 -04001698 const auto& outputState = getState();
1699 if (mPlanner && outputState.isEnabled) {
1700 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1701 outputState.usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001702 }
1703}
1704
Lloyd Pique32cbe282018-10-19 13:09:22 -07001705void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001706 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001707 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001708}
1709
Vishnu Naira3140382022-02-24 14:07:11 -08001710void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001711 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001712 auto& outputState = editState();
1713 outputState.usesClientComposition = true;
1714 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001715 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001716}
1717
Lloyd Pique688abd42019-02-15 15:42:24 -08001718bool Output::getSkipColorTransform() const {
1719 return true;
1720}
1721
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001722compositionengine::Output::FrameFences Output::presentFrame() {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001723 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001724 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001725 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1726 }
1727 return result;
1728}
1729
Vishnu Naira3140382022-02-24 14:07:11 -08001730void Output::setPredictCompositionStrategy(bool predict) {
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001731 mPredictCompositionStrategy = predict;
1732 updateHwcAsyncWorker();
1733}
1734
1735void Output::updateHwcAsyncWorker() {
1736 if (mPredictCompositionStrategy || mOffloadPresent) {
1737 if (!mHwComposerAsyncWorker) {
1738 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1739 }
Vishnu Naira3140382022-02-24 14:07:11 -08001740 } else {
1741 mHwComposerAsyncWorker.reset(nullptr);
1742 }
1743}
1744
Alec Mouridda07d92022-04-25 22:39:25 +00001745void Output::setTreat170mAsSrgb(bool enable) {
1746 editState().treat170mAsSrgb = enable;
1747}
1748
Sally Qi0abc4a52024-09-26 16:13:06 -07001749const aidl::android::hardware::graphics::composer3::OverlayProperties* Output::getOverlaySupport() {
1750 return nullptr;
1751}
1752
Vishnu Naira3140382022-02-24 14:07:11 -08001753bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001754 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1755 uint64_t outputLayerHash = getState().outputLayerHash;
1756 editState().lastOutputLayerHash = outputLayerHash;
1757
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001758 if (!getState().isEnabled || !mPredictCompositionStrategy) {
Vishnu Naira3140382022-02-24 14:07:11 -08001759 ALOGV("canPredictCompositionStrategy disabled");
1760 return false;
1761 }
1762
1763 if (!getState().previousDeviceRequestedChanges) {
1764 ALOGV("canPredictCompositionStrategy previous changes not available");
1765 return false;
1766 }
1767
1768 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1769 ALOGV("canPredictCompositionStrategy surface does not support");
1770 return false;
1771 }
1772
1773 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1774 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1775 return false;
1776 }
1777
Robert Carrec8ccca2022-05-04 09:36:14 -07001778 if (lastOutputLayerHash != outputLayerHash) {
1779 ALOGV("canPredictCompositionStrategy output layers changed");
1780 return false;
1781 }
1782
Vishnu Naira3140382022-02-24 14:07:11 -08001783 // If no layer uses clientComposition, then don't predict composition strategy
1784 // because we have less work to do in parallel.
1785 if (!anyLayersRequireClientComposition()) {
1786 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1787 return false;
1788 }
1789
Robert Carrec8ccca2022-05-04 09:36:14 -07001790 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001791}
1792
1793bool Output::anyLayersRequireClientComposition() const {
1794 const auto layers = getOutputLayersOrderedByZ();
1795 return std::any_of(layers.begin(), layers.end(),
1796 [](const auto& layer) { return layer->requiresClientComposition(); });
1797}
1798
1799void Output::finishPrepareFrame() {
1800 const auto& state = getState();
1801 if (mPlanner) {
1802 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1803 }
1804 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1805}
1806
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001807bool Output::mustRecompose() const {
1808 return mMustRecompose;
1809}
1810
Alec Mourif97df4d2023-09-06 02:10:05 +00001811float Output::getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
1812 if (buffer == nullptr) {
1813 return 1.0f;
1814 }
1815
1816 if (!FlagManager::getInstance().fp16_client_target()) {
1817 return 1.0f;
1818 }
1819
1820 if (getState().displayBrightnessNits < 0.0f || getState().sdrWhitePointNits <= 0.0f ||
1821 buffer->getPixelFormat() != PIXEL_FORMAT_RGBA_FP16 ||
1822 (static_cast<int32_t>(getState().dataspace) &
1823 static_cast<int32_t>(ui::Dataspace::RANGE_MASK)) !=
1824 static_cast<int32_t>(ui::Dataspace::RANGE_EXTENDED)) {
1825 return 1.0f;
1826 }
1827
1828 return getState().displayBrightnessNits / getState().sdrWhitePointNits;
1829}
1830
Brian Lindahl1a4ffd82024-10-30 11:00:37 -06001831bool Output::hasPictureProcessing() const {
1832 return false;
1833}
1834
1835int32_t Output::getMaxLayerPictureProfiles() const {
1836 return 0;
1837}
1838
1839void Output::applyPictureProfile() {
1840 if (!com_android_graphics_libgui_flags_apply_picture_profiles()) {
1841 return;
1842 }
1843
1844 // TODO(b/337330263): Move this into the Display class and add a Display unit test.
1845 if (!getState().pictureProfileHandle) {
1846 return;
1847 }
1848 if (!getDisplayId()) {
1849 return;
1850 }
1851 if (auto displayId = PhysicalDisplayId::tryCast(*getDisplayId())) {
1852 auto& hwc = getCompositionEngine().getHwComposer();
1853 const status_t error =
1854 hwc.setDisplayPictureProfileHandle(*displayId, getState().pictureProfileHandle);
1855 ALOGE_IF(error, "setDisplayPictureProfileHandle failed for %s: %d, (%s)", getName().c_str(),
1856 error, strerror(-error));
1857 }
1858}
1859
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001860} // namespace impl
1861} // namespace android::compositionengine