blob: 9c145b64540546b36cc6b53f703ca91b9714f01c [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>
Leon Scroggins III370b8b52022-12-08 13:20:45 -050036#include <scheduler/FrameTargeter.h>
37#include <scheduler/Time.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080038
Chavi Weingarten545da0e2023-02-09 14:55:57 +000039#include <optional>
Alec Mouria90a5702021-04-16 16:36:21 +000040#include <thread>
41
42#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080043
44// TODO(b/129481165): remove the #pragma below and fix conversion issues
45#pragma clang diagnostic push
46#pragma clang diagnostic ignored "-Wconversion"
47
Lloyd Pique688abd42019-02-15 15:42:24 -080048#include <renderengine/DisplaySettings.h>
49#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080050
51// TODO(b/129481165): remove the #pragma below and fix conversion issues
52#pragma clang diagnostic pop // ignored "-Wconversion"
53
Dan Stoza269dc4d2021-01-15 15:07:43 -080054#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070055#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080056#include <ui/HdrCapabilities.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070057
Lloyd Pique688abd42019-02-15 15:42:24 -080058#include "TracedOrdinal.h"
59
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050060using aidl::android::hardware::graphics::composer3::Composition;
61
Lloyd Piquefeb73d72018-12-04 17:23:44 -080062namespace android::compositionengine {
63
64Output::~Output() = default;
65
66namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080067using CompositionStrategyPredictionState =
68 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080069namespace {
70
71template <typename T>
72class Reversed {
73public:
74 explicit Reversed(const T& container) : mContainer(container) {}
75 auto begin() { return mContainer.rbegin(); }
76 auto end() { return mContainer.rend(); }
77
78private:
79 const T& mContainer;
80};
81
82// Helper for enumerating over a container in reverse order
83template <typename T>
84Reversed<T> reversed(const T& c) {
85 return Reversed<T>(c);
86}
87
Marin Shalamanovb15d2272020-09-17 21:41:52 +020088struct ScaleVector {
89 float x;
90 float y;
91};
92
93// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
94// where to' will have the same size as "to". In the case where "from" and "to"
95// start at the origin to'=to.
96ScaleVector getScale(const Rect& from, const Rect& to) {
97 return {.x = static_cast<float>(to.width()) / from.width(),
98 .y = static_cast<float>(to.height()) / from.height()};
99}
100
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800101} // namespace
102
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700103std::shared_ptr<Output> createOutput(
104 const compositionengine::CompositionEngine& compositionEngine) {
105 return createOutputTemplated<Output>(compositionEngine);
106}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107
108Output::~Output() = default;
109
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700111 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
112 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700113}
114
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700115std::optional<DisplayId> Output::getDisplayId() const {
116 return {};
117}
118
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119const std::string& Output::getName() const {
120 return mName;
121}
122
123void Output::setName(const std::string& name) {
124 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400125 auto displayIdOpt = getDisplayId();
Leon Scroggins IIIc03d4652023-01-05 13:03:53 -0500126 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
127 to_string(*displayIdOpt).c_str())
128 : mName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700129}
130
131void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700132 auto& outputState = editState();
133 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700134 return;
135 }
136
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700137 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700138 dirtyEntireOutput();
139}
140
Alec Mouri023c1882021-05-08 16:36:33 -0700141void Output::setLayerCachingEnabled(bool enabled) {
142 if (enabled == (mPlanner != nullptr)) {
143 return;
144 }
145
146 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700147 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700148 if (mRenderSurface) {
149 mPlanner->setDisplaySize(mRenderSurface->getSize());
150 }
151 } else {
152 mPlanner.reset();
153 }
Alec Mouric773472b2021-05-19 14:29:05 -0700154
155 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
156 if (!outputLayer) {
157 continue;
158 }
159
160 outputLayer->editState().overrideInfo = {};
161 }
Alec Mouri023c1882021-05-08 16:36:33 -0700162}
163
Ady Abrahamdb036a82021-07-16 14:18:34 -0700164void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
165 if (mPlanner) {
166 mPlanner->setTexturePoolEnabled(enabled);
167 }
168}
169
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200170void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
171 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700172 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200173
Angel Aguayob084e0c2021-08-04 23:27:28 +0000174 outputState.displaySpace.setOrientation(orientation);
175 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200176 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200177
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200178 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000179 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200180 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200181 std::swap(orientedSize.width, orientedSize.height);
182 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000183 outputState.orientedDisplaySpace.setBounds(orientedSize);
184 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200185
186 // Compute displaySpace.content
187 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
188 ui::Transform rotation;
189 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000190 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200191 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
192 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000193 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200194
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200195 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000196 outputState.framebufferSpace.setOrientation(orientation);
197 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200198 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000199 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
200 outputState.framebufferSpace.getBoundsAsRect());
201 outputState.framebufferSpace.setContent(
202 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200203
204 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000205 outputState.layerStackSpace.setContent(layerStackSpaceRect);
206 outputState.layerStackSpace.setBounds(
207 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200208
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200209 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
210 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700211 dirtyEntireOutput();
212}
213
Alec Mouricdf16792021-12-10 13:16:06 -0800214void Output::setNextBrightness(float brightness) {
215 editState().displayBrightness = brightness;
216}
217
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200218void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700219 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200220
221 auto& state = editState();
222
223 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000224 const ui::Size newBounds(size);
225 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200226
227 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000228 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200229 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
230
231 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000232 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200233 ui::Size orientedSize = size;
234 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
235 std::swap(orientedSize.width, orientedSize.height);
236 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000237 const ui::Size newOrientedBounds(orientedSize);
238 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700239
Dan Stoza6166c312021-01-15 16:34:05 -0800240 if (mPlanner) {
241 mPlanner->setDisplaySize(size);
242 }
243
Lloyd Pique32cbe282018-10-19 13:09:22 -0700244 dirtyEntireOutput();
245}
246
Garfield Tan54edd912020-10-21 16:31:41 -0700247ui::Transform::RotationFlags Output::getTransformHint() const {
248 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
249}
250
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700251void Output::setLayerFilter(ui::LayerFilter filter) {
252 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700253 dirtyEntireOutput();
254}
255
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800256void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700257 auto& colorTransformMatrix = editState().colorTransformMatrix;
258 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700259 return;
260 }
261
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800263
264 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700265}
266
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800267void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700268 auto& outputState = editState();
269 if (outputState.colorMode == colorProfile.mode &&
270 outputState.dataspace == colorProfile.dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000271 outputState.renderIntent == colorProfile.renderIntent) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800272 return;
273 }
274
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700275 outputState.colorMode = colorProfile.mode;
276 outputState.dataspace = colorProfile.dataspace;
277 outputState.renderIntent = colorProfile.renderIntent;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700278
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800279 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700280
Lloyd Pique32cbe282018-10-19 13:09:22 -0700281 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800282 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
283 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800284
285 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700286}
287
John Reckac09e452021-04-07 16:35:37 -0400288void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
289 auto& outputState = editState();
290 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
291 outputState.displayBrightnessNits == displayBrightnessNits) {
292 // Nothing changed
293 return;
294 }
295 outputState.sdrWhitePointNits = sdrWhitePointNits;
296 outputState.displayBrightnessNits = displayBrightnessNits;
297 dirtyEntireOutput();
298}
299
Lloyd Pique32cbe282018-10-19 13:09:22 -0700300void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700301 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
302 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700303
304 dumpBase(out);
305}
306
307void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700308 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700309 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700310
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700311 if (mDisplayColorProfile) {
312 mDisplayColorProfile->dump(out);
313 } else {
314 out.append(" No display color profile!\n");
315 }
316
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700317 out += '\n';
318
Lloyd Pique31cb2942018-10-19 17:23:03 -0700319 if (mRenderSurface) {
320 mRenderSurface->dump(out);
321 } else {
322 out.append(" No render surface!\n");
323 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800324
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700325 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700326 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800327 if (!outputLayer) {
328 continue;
329 }
330 outputLayer->dump(out);
331 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700332}
333
Dan Stoza269dc4d2021-01-15 15:07:43 -0800334void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
335 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700336 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800337 return;
338 }
339 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
340 mPlanner->dump(args, out);
341}
342
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700343compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
344 return mDisplayColorProfile.get();
345}
346
347void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
348 mDisplayColorProfile = std::move(mode);
349}
350
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800351const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
352 return mReleasedLayers;
353}
354
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700355void Output::setDisplayColorProfileForTest(
356 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
357 mDisplayColorProfile = std::move(mode);
358}
359
Lloyd Pique31cb2942018-10-19 17:23:03 -0700360compositionengine::RenderSurface* Output::getRenderSurface() const {
361 return mRenderSurface.get();
362}
363
364void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
365 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800366 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000367 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800368 if (mPlanner) {
369 mPlanner->setDisplaySize(size);
370 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700371 dirtyEntireOutput();
372}
373
Vishnu Nair9b079a22020-01-21 14:36:08 -0800374void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
375 if (cacheSize == 0) {
376 mClientCompositionRequestCache.reset();
377 } else {
378 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
379 }
380};
381
Lloyd Pique31cb2942018-10-19 17:23:03 -0700382void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
383 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700384}
385
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700386Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700387 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000388 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700389}
390
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700391bool Output::includesLayer(ui::LayerFilter filter) const {
392 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700393}
394
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700395bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800396 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700397 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800398}
399
Lloyd Piquedf336d92019-03-07 21:38:42 -0800400std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800401 const sp<LayerFE>& layerFE) const {
402 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800403}
404
Lloyd Piquede196652020-01-22 17:29:58 -0800405compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
406 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700407 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800408}
409
Lloyd Pique01c77c12019-04-17 12:48:32 -0700410std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800411 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700412 for (size_t i = 0; i < getOutputLayerCount(); i++) {
413 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800414 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700415 return i;
416 }
417 }
418 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800419}
420
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800421void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
422 mReleasedLayers = std::move(layers);
423}
424
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
426 LayerFESet& geomSnapshots) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000427 SFTRACE_CALL();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800428 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800429
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800430 rebuildLayerStacks(refreshArgs, geomSnapshots);
Brian Lindahl439afad2022-11-14 11:16:55 -0700431 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800432}
433
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400434ftl::Future<std::monostate> Output::present(
435 const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500436 const auto stringifyExpectedPresentTime = [this, &refreshArgs]() -> std::string {
437 return ftl::Optional(getDisplayId())
438 .and_then(PhysicalDisplayId::tryCast)
439 .and_then([&refreshArgs](PhysicalDisplayId id) {
440 return refreshArgs.frameTargets.get(id);
441 })
442 .transform([](const auto& frameTargetPtr) {
443 return frameTargetPtr.get()->expectedPresentTime();
444 })
445 .transform([](TimePoint expectedPresentTime) {
446 return base::StringPrintf(" vsyncIn %.2fms",
447 ticks<std::milli, float>(expectedPresentTime -
448 TimePoint::now()));
449 })
450 .or_else([] {
451 // There is no vsync for this output.
452 return std::make_optional(std::string());
453 })
454 .value();
455 };
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000456 SFTRACE_FORMAT("%s for %s%s", __func__, mNamePlusId.c_str(),
457 stringifyExpectedPresentTime().c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800458 ALOGV(__FUNCTION__);
459
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800460 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800461 updateCompositionState(refreshArgs);
462 planComposition();
463 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800464 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800465 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800466
Xiang Wangaab31162024-03-12 19:48:08 -0700467 if (isPowerHintSessionEnabled()) {
468 // always reset the flag before the composition prediction
469 setHintSessionRequiresRenderEngine(false);
470 }
Vishnu Naira3140382022-02-24 14:07:11 -0800471 GpuCompositionResult result;
472 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
473 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000474 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800475 } else {
476 prepareFrame();
477 }
478
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800479 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000480 finishFrame(std::move(result));
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400481 ftl::Future<std::monostate> future;
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400482 const bool flushEvenWhenDisabled = !refreshArgs.bufferIdsToUncache.empty();
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400483 if (mOffloadPresent) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400484 future = presentFrameAndReleaseLayersAsync(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400485
486 // Only offload for this frame. The next frame will determine whether it
487 // needs to be offloaded. Leave the HwcAsyncWorker in place. For one thing,
488 // it is currently presenting. Further, it may be needed next frame, and
489 // we don't want to churn.
490 mOffloadPresent = false;
491 } else {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -0400492 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400493 future = ftl::yield<std::monostate>({});
494 }
Alec Mouriaa831582021-06-07 16:23:01 -0700495 renderCachedSets(refreshArgs);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400496 return future;
497}
498
499void Output::offloadPresentNextFrame() {
500 mOffloadPresent = true;
501 updateHwcAsyncWorker();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800502}
503
Brian Lindahl439afad2022-11-14 11:16:55 -0700504void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
505 if (bufferIdsToUncache.empty()) {
506 return;
507 }
508 for (auto outputLayer : getOutputLayersOrderedByZ()) {
509 outputLayer->uncacheBuffers(bufferIdsToUncache);
510 }
511}
512
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800513void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
514 LayerFESet& layerFESet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700515 auto& outputState = editState();
516
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800517 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700518 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800519 return;
520 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000521 SFTRACE_CALL();
Vishnu Naird9a640b2023-07-21 14:20:27 +0000522 ALOGV(__FUNCTION__);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800523
524 // Process the layers to determine visibility and coverage
525 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000526 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
527 ? std::make_optional<Region>()
528 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800529 collectVisibleLayers(refreshArgs, coverage);
530
531 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700532 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000533 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800534 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
535
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700536 outputState.undefinedRegion = undefinedRegion;
537 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800538}
539
540void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
541 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800542 // Evaluate the layers from front to back to determine what is visible. This
543 // also incrementally calculates the coverage information for each layer as
544 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800545 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700546 // Incrementally process the coverage for each layer
547 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800548
549 // TODO(b/121291683): Stop early if the output is completely covered and
550 // no more layers could even be visible underneath the ones on top.
551 }
552
Lloyd Pique01c77c12019-04-17 12:48:32 -0700553 setReleasedLayers(refreshArgs);
554
555 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800556}
557
Lloyd Piquede196652020-01-22 17:29:58 -0800558void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700559 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560 // Ensure we have a snapshot of the basic geometry layer state. Limit the
561 // snapshots to once per frame for each candidate layer, as layers may
562 // appear on multiple outputs.
563 if (!coverage.latchedLayers.count(layerFE)) {
564 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800565 }
566
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700567 // Only consider the layers on this output
568 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800569 return;
570 }
571
572 // Obtain a read-only pointer to the front-end layer state
573 const auto* layerFEState = layerFE->getCompositionState();
574 if (CC_UNLIKELY(!layerFEState)) {
575 return;
576 }
577
578 // handle hidden surfaces by setting the visible region to empty
579 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700580 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800581 }
582
Vishnu Naird47bcee2023-02-24 18:08:51 +0000583 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
584 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000585
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800586 /*
587 * opaqueRegion: area of a surface that is fully opaque.
588 */
589 Region opaqueRegion;
590
591 /*
592 * visibleRegion: area of a surface that is visible on screen and not fully
593 * transparent. This is essentially the layer's footprint minus the opaque
594 * regions above it. Areas covered by a translucent surface are considered
595 * visible.
596 */
597 Region visibleRegion;
598
599 /*
600 * coveredRegion: area of a surface that is covered by all visible regions
601 * above it (which includes the translucent areas).
602 */
603 Region coveredRegion;
604
605 /*
606 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500607 * transparent.
608 * This is used to tell when the layer has no visible non-transparent
609 * regions and can be removed from the layer list. It does not affect the
610 * visibleRegion of this layer or any layers beneath it. The hint may not
611 * be correct if apps don't respect the SurfaceView restrictions (which,
612 * sadly, some don't).
613 *
614 * In addition, it is used on DISPLAY_DECORATION layers to specify the
615 * blockingRegion, allowing the DPU to skip it to save power. Once we have
616 * hardware that supports a blockingRegion on frames with AFBC, it may be
617 * useful to use this for other layers, too, so long as we can prevent
618 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800619 */
620 Region transparentRegion;
621
Vishnu Naira483b4a2019-12-12 15:07:52 -0800622 /*
623 * shadowRegion: Region cast by the layer's shadow.
624 */
625 Region shadowRegion;
626
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000627 /**
628 * covered region above excluding internal display overlay layers
629 */
630 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
631
Lloyd Piquede196652020-01-22 17:29:58 -0800632 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800633
634 // Get the visible region
635 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
636 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800637 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800638 visibleRegion.set(visibleRect);
639
Vishnu Naird9e4f462023-10-06 04:05:45 +0000640 if (layerFEState->shadowSettings.length > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800641 // if the layer casts a shadow, offset the layers visible region and
642 // calculate the shadow region.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000643 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowSettings.length) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800644 Rect visibleRectWithShadows(visibleRect);
645 visibleRectWithShadows.inset(inset, inset, inset, inset);
646 visibleRegion.set(visibleRectWithShadows);
647 shadowRegion = visibleRegion.subtract(visibleRect);
648 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800649
650 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700651 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800652 }
653
654 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800655 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800656 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000657 // Clip the transparent region to geomLayerBounds first
658 // The transparent region may be influenced by applications, for
659 // instance, by overriding ViewGroup#gatherTransparentRegion with a
660 // custom view. Once the layer stack -> display mapping is known, we
661 // must guard against very wrong inputs to prevent underflow or
662 // overflow errors. We do this here by constraining the transparent
663 // region to be within the pre-transform layer bounds, since the
664 // layer bounds are expected to play nicely with the full
665 // transform.
666 const Region clippedTransparentRegionHint =
667 layerFEState->transparentRegionHint.intersect(
668 Rect(layerFEState->geomLayerBounds));
669
670 if (clippedTransparentRegionHint.isEmpty()) {
671 if (!layerFEState->transparentRegionHint.isEmpty()) {
672 ALOGD("Layer: %s had an out of bounds transparent region",
673 layerFE->getDebugName());
674 layerFEState->transparentRegionHint.dump("transparentRegionHint");
675 }
676 transparentRegion.clear();
677 } else {
678 transparentRegion = tr.transform(clippedTransparentRegionHint);
679 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800680 } else {
681 // transformation too complex, can't do the
682 // transparent region optimization.
683 transparentRegion.clear();
684 }
685 }
686
687 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800688 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800689 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800690 // If we one of the simple category of transforms (0/90/180/270 rotation
691 // + any flip), then the opaque region is the layer's footprint.
692 // Otherwise we don't try and compute the opaque region since there may
693 // be errors at the edges, and we treat the entire layer as
694 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800695 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800696 }
697
698 // Clip the covered region to the visible region
699 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
700
701 // Update accumAboveCoveredLayers for next (lower) layer
702 coverage.aboveCoveredLayers.orSelf(visibleRegion);
703
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000704 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
705 coveredRegionExcludingDisplayOverlays =
706 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
707 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
708 }
709
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800710 // subtract the opaque region covered by the layers above us
711 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
712
713 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700714 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800715 }
716
717 // Get coverage information for the layer as previously displayed,
718 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800719 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700720 auto prevOutputLayer =
721 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800722
723 // Get coverage information for the layer as previously displayed
724 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
725 const Region kEmptyRegion;
726 const Region& oldVisibleRegion =
727 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
728 const Region& oldCoveredRegion =
729 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
730
731 // compute this layer's dirty region
732 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800733 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800734 // we need to invalidate the whole region
735 dirty = visibleRegion;
736 // as well, as the old visible region
737 dirty.orSelf(oldVisibleRegion);
738 } else {
739 /* compute the exposed region:
740 * the exposed region consists of two components:
741 * 1) what's VISIBLE now and was COVERED before
742 * 2) what's EXPOSED now less what was EXPOSED before
743 *
744 * note that (1) is conservative, we start with the whole visible region
745 * but only keep what used to be covered by something -- which mean it
746 * may have been exposed.
747 *
748 * (2) handles areas that were not covered by anything but got exposed
749 * because of a resize.
750 *
751 */
752 const Region newExposed = visibleRegion - coveredRegion;
753 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
754 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
755 }
756 dirty.subtractSelf(coverage.aboveOpaqueLayers);
757
758 // accumulate to the screen dirty region
759 coverage.dirtyRegion.orSelf(dirty);
760
761 // Update accumAboveOpaqueLayers for next (lower) layer
762 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
763
764 // Compute the visible non-transparent region
765 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
766
Vishnu Naira483b4a2019-12-12 15:07:52 -0800767 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800768 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700769 const auto& outputState = getState();
770 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000771 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800772 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700773 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800774 }
775
Vishnu Naira483b4a2019-12-12 15:07:52 -0800776 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
777
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800778 // The layer is visible. Either reuse the existing outputLayer if we have
779 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800780 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800781
782 // Store the layer coverage information into the layer state as some of it
783 // is useful later.
784 auto& outputLayerState = result->editState();
785 outputLayerState.visibleRegion = visibleRegion;
786 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
787 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200788 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000789 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800790 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500791 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400792 layerFEState->compositionType == Composition::DISPLAY_DECORATION
793 ? outputState.transform.transform(
794 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
795 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000796 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
797 outputLayerState.coveredRegionExcludingDisplayOverlays =
798 std::move(coveredRegionExcludingDisplayOverlays);
799 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800800}
801
802void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
803 // The base class does nothing with this call.
804}
805
Dan Stoza269dc4d2021-01-15 15:07:43 -0800806void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000807 SFTRACE_CALL();
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800808 ALOGV(__FUNCTION__);
809
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800810 if (!getState().isEnabled) {
811 return;
812 }
813
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800814 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
815 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
816
Lloyd Pique01c77c12019-04-17 12:48:32 -0700817 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700818 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800819 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200820 forceClientComposition,
821 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800822
823 if (mLayerRequestingBackgroundBlur == layer) {
824 forceClientComposition = false;
825 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800826 }
827}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800828
Dan Stoza269dc4d2021-01-15 15:07:43 -0800829void Output::planComposition() {
830 if (!mPlanner || !getState().isEnabled) {
831 return;
832 }
833
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000834 SFTRACE_CALL();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800835 ALOGV(__FUNCTION__);
836
837 mPlanner->plan(getOutputLayersOrderedByZ());
838}
839
840void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000841 SFTRACE_CALL();
Dan Stoza269dc4d2021-01-15 15:07:43 -0800842 ALOGV(__FUNCTION__);
843
844 if (!getState().isEnabled) {
845 return;
846 }
847
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500848 if (auto frameTargetPtrOpt = ftl::Optional(getDisplayId())
849 .and_then(PhysicalDisplayId::tryCast)
850 .and_then([&refreshArgs](PhysicalDisplayId id) {
851 return refreshArgs.frameTargets.get(id);
852 })) {
853 editState().earliestPresentTime = frameTargetPtrOpt->get()->earliestPresentTime();
854 editState().expectedPresentTime = frameTargetPtrOpt->get()->expectedPresentTime().ns();
855 }
ramindani4aac32c2023-10-30 14:13:30 -0700856 editState().frameInterval = refreshArgs.frameInterval;
jimmyshiu4e211772023-06-15 15:18:38 +0000857 editState().powerCallback = refreshArgs.powerCallback;
Ady Abraham3645e642021-04-20 18:39:00 -0700858
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400859 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800860 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400861 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400862 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400863 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700864 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800865 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400866 if (layer == peekThroughLayer) {
867 // No longer needed, although it should not show up again, so
868 // resetting it is not truly needed either.
869 peekThroughLayer = nullptr;
870
871 // peekThroughLayer was already drawn ahead of its z order.
872 continue;
873 }
Dan Stoza6166c312021-01-15 16:34:05 -0800874 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400875 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400876 if (overrideInfo.buffer != nullptr) {
877 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800878 ALOGV("Skipping redundant buffer");
879 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400880 } else {
881 // First layer with the override buffer.
882 if (overrideInfo.peekThroughLayer) {
883 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400884
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400885 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400886 overrideZ = true;
887 includeGeometry = true;
888 constexpr bool isPeekingThrough = true;
889 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
890 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700891 outputLayerHash ^= android::hashCombine(
892 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
893 z, includeGeometry, overrideZ, isPeekingThrough,
894 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400895 }
896
897 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800898 }
Dan Stoza6166c312021-01-15 16:34:05 -0800899 }
900
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400901 constexpr bool isPeekingThrough = false;
902 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700903 if (!skipLayer) {
904 outputLayerHash ^= android::hashCombine(
905 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
906 z, includeGeometry, overrideZ, isPeekingThrough,
907 layer->requiresClientComposition());
908 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800909 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700910 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800911}
912
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800913compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
914 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
daniml39d6a2162021-05-19 15:56:21 +0200915 for (size_t i = 0; i < getOutputLayerCount(); i++) {
916 compositionengine::OutputLayer* layer = getOutputLayerOrderedByZByIndex(i);
917 compositionengine::OutputLayer* nextLayer = getOutputLayerOrderedByZByIndex(i + 1);
918
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400919 const auto* compState = layer->getLayerFE().getCompositionState();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100920
921 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
922 // want to force client composition because of the blur.
923 if (compState->sidebandStream != nullptr) {
924 return nullptr;
925 }
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400926
927 // If RenderEngine cannot render protected content, we cannot blur.
928 if (compState->hasProtectedContent &&
929 !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
930 return nullptr;
931 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000932 if (compState->isOpaque) {
933 continue;
934 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100935 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800936 layerRequestingBgComposition = layer;
937 }
daniml39d6a2162021-05-19 15:56:21 +0200938
939 // If the next layer is the Udfps touched layer, enable client composition for it
940 // because that somehow leads to the Udfps touched layer getting device composition
941 // consistently.
942 if ((nextLayer != nullptr && layerRequestingBgComposition == nullptr) &&
943 (strncmp(nextLayer->getLayerFE().getDebugName(), UDFPS_TOUCHED_LAYER_NAME,
944 strlen(UDFPS_TOUCHED_LAYER_NAME)) == 0)) {
945 layerRequestingBgComposition = layer;
946 break;
947 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800948 }
949 return layerRequestingBgComposition;
950}
951
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800952void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
953 setColorProfile(pickColorProfile(refreshArgs));
954}
955
956// Returns a data space that fits all visible layers. The returned data space
957// can only be one of
958// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
959// - Dataspace::DISPLAY_P3
960// - Dataspace::DISPLAY_BT2020
961// The returned HDR data space is one of
962// - Dataspace::UNKNOWN
963// - Dataspace::BT2020_HLG
964// - Dataspace::BT2020_PQ
965ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
966 bool* outIsHdrClientComposition) const {
967 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
968 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
969
Vishnu Naire14c6b32022-08-06 04:20:15 +0000970 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
971 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
972 // properties. Return a default dataspace value in this case.
973 if (!getState().isEnabled) {
974 return ui::Dataspace::V0_SRGB;
975 }
976
Lloyd Pique01c77c12019-04-17 12:48:32 -0700977 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800978 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800979 case ui::Dataspace::V0_SCRGB:
980 case ui::Dataspace::V0_SCRGB_LINEAR:
981 case ui::Dataspace::BT2020:
982 case ui::Dataspace::BT2020_ITU:
983 case ui::Dataspace::BT2020_LINEAR:
984 case ui::Dataspace::DISPLAY_BT2020:
985 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
986 break;
987 case ui::Dataspace::DISPLAY_P3:
988 bestDataSpace = ui::Dataspace::DISPLAY_P3;
989 break;
990 case ui::Dataspace::BT2020_PQ:
991 case ui::Dataspace::BT2020_ITU_PQ:
992 bestDataSpace = ui::Dataspace::DISPLAY_P3;
993 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800994 *outIsHdrClientComposition =
995 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800996 break;
997 case ui::Dataspace::BT2020_HLG:
998 case ui::Dataspace::BT2020_ITU_HLG:
999 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1000 // When there's mixed PQ content and HLG content, we set the HDR
Sally Qi37d07c02023-10-05 17:32:32 +00001001 // data space to be BT2020_HLG and convert PQ to HLG.
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001002 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
1003 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
1004 }
1005 break;
1006 default:
1007 break;
1008 }
1009 }
1010
1011 return bestDataSpace;
1012}
1013
1014compositionengine::Output::ColorProfile Output::pickColorProfile(
1015 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
1016 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
1017 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Alec Mouri88790f32023-07-21 01:25:14 +00001018 ui::RenderIntent::COLORIMETRIC};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001019 }
1020
1021 ui::Dataspace hdrDataSpace;
1022 bool isHdrClientComposition = false;
1023 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
1024
1025 switch (refreshArgs.forceOutputColorMode) {
1026 case ui::ColorMode::SRGB:
1027 bestDataSpace = ui::Dataspace::V0_SRGB;
1028 break;
1029 case ui::ColorMode::DISPLAY_P3:
1030 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1031 break;
1032 default:
1033 break;
1034 }
1035
1036 // respect hdrDataSpace only when there is no legacy HDR support
1037 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1038 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1039 if (isHdr) {
1040 bestDataSpace = hdrDataSpace;
1041 }
1042
1043 ui::RenderIntent intent;
1044 switch (refreshArgs.outputColorSetting) {
1045 case OutputColorSetting::kManaged:
1046 case OutputColorSetting::kUnmanaged:
1047 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1048 : ui::RenderIntent::COLORIMETRIC;
1049 break;
1050 case OutputColorSetting::kEnhanced:
1051 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1052 break;
1053 default: // vendor display color setting
1054 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1055 break;
1056 }
1057
1058 ui::ColorMode outMode;
1059 ui::Dataspace outDataSpace;
1060 ui::RenderIntent outRenderIntent;
1061 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1062 &outRenderIntent);
1063
Alec Mouri88790f32023-07-21 01:25:14 +00001064 return ColorProfile{outMode, outDataSpace, outRenderIntent};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001065}
1066
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001067void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001068 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001069 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001070 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001071 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001072
1073 // If nothing has changed (!dirty), don't recompose.
1074 // If something changed, but we don't currently have any visible layers,
1075 // and didn't when we last did a composition, then skip it this time.
1076 // The second rule does two things:
1077 // - When all layers are removed from a display, we'll emit one black
1078 // frame, then nothing more until we get new layers.
1079 // - When a display is created with a private layer stack, we won't
1080 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001081 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001082
1083 const char flagPrefix[] = {'-', '+'};
1084 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001085 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1086 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1087 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001088
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001089 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001090
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001091 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001092 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001093 }
1094}
1095
Lloyd Pique66d68602019-02-13 14:23:31 -08001096void Output::prepareFrame() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001097 SFTRACE_CALL();
Lloyd Pique66d68602019-02-13 14:23:31 -08001098 ALOGV(__FUNCTION__);
1099
Vishnu Naira3140382022-02-24 14:07:11 -08001100 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001101 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001102 return;
1103 }
1104
Vishnu Naira3140382022-02-24 14:07:11 -08001105 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1106 bool success = chooseCompositionStrategy(&changes);
1107 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001108 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001109 outputState.previousDeviceRequestedChanges = changes;
1110 outputState.previousDeviceRequestedSuccess = success;
1111 if (success) {
1112 applyCompositionStrategy(changes);
1113 }
1114 finishPrepareFrame();
1115}
Lloyd Pique66d68602019-02-13 14:23:31 -08001116
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001117ftl::Future<std::monostate> Output::presentFrameAndReleaseLayersAsync(bool flushEvenWhenDisabled) {
Yi Kong9dce90f2024-08-14 07:06:52 +08001118 return ftl::Future<bool>(mHwComposerAsyncWorker->send([this, flushEvenWhenDisabled]() {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001119 presentFrameAndReleaseLayers(flushEvenWhenDisabled);
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001120 return true;
Yi Kong9dce90f2024-08-14 07:06:52 +08001121 }))
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001122 .then([](bool) { return std::monostate{}; });
1123}
1124
Vishnu Naira3140382022-02-24 14:07:11 -08001125std::future<bool> Output::chooseCompositionStrategyAsync(
1126 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1127 return mHwComposerAsyncWorker->send(
1128 [&, changes]() { return chooseCompositionStrategy(changes); });
1129}
1130
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001131GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001132 SFTRACE_CALL();
Vishnu Naira3140382022-02-24 14:07:11 -08001133 ALOGV(__FUNCTION__);
1134 auto& state = editState();
1135 const auto& previousChanges = state.previousDeviceRequestedChanges;
1136 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1137 resetCompositionStrategy();
1138 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1139 if (state.previousDeviceRequestedSuccess) {
1140 applyCompositionStrategy(previousChanges);
1141 }
1142 finishPrepareFrame();
1143
1144 base::unique_fd bufferFence;
1145 std::shared_ptr<renderengine::ExternalTexture> buffer;
1146 updateProtectedContentState();
1147 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1148 GpuCompositionResult compositionResult;
1149 if (dequeueSucceeded) {
1150 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001151 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001152 if (optFd) {
1153 compositionResult.fence = std::move(*optFd);
1154 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001155 }
1156
Vishnu Naira3140382022-02-24 14:07:11 -08001157 auto chooseCompositionSuccess = hwcResult.get();
1158 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001159 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1160 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001161 if (!predictionSucceeded) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001162 SFTRACE_NAME("CompositionStrategyPredictionMiss");
Vishnu Naira3140382022-02-24 14:07:11 -08001163 resetCompositionStrategy();
1164 if (chooseCompositionSuccess) {
1165 applyCompositionStrategy(changes);
1166 }
1167 finishPrepareFrame();
1168 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1169 compositionResult.buffer = buffer;
1170 } else {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001171 SFTRACE_NAME("CompositionStrategyPredictionHit");
Vishnu Naira3140382022-02-24 14:07:11 -08001172 }
1173 state.previousDeviceRequestedChanges = std::move(changes);
1174 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1175 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001176}
1177
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001178void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1179 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1180 return;
1181 }
1182
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001183 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001184 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001185 base::unique_fd bufferFence;
1186 std::shared_ptr<renderengine::ExternalTexture> buffer;
1187 updateProtectedContentState();
1188 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001189 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Alec Mourif97df4d2023-09-06 02:10:05 +00001190 mRenderSurface->queueBuffer(base::unique_fd(), getHdrSdrRatio(buffer));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001191 }
1192 }
1193
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001194 constexpr bool kFlushEvenWhenDisabled = false;
1195 presentFrameAndReleaseLayers(kFlushEvenWhenDisabled);
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001196
1197 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1198
1199 prepareFrame();
1200}
1201
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001202void Output::finishFrame(GpuCompositionResult&& result) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001203 SFTRACE_CALL();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001204 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001205 const auto& outputState = getState();
1206 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001207 return;
1208 }
1209
Vishnu Naira3140382022-02-24 14:07:11 -08001210 std::optional<base::unique_fd> optReadyFence;
1211 std::shared_ptr<renderengine::ExternalTexture> buffer;
1212 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001213 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001214 optReadyFence = std::move(result.fence);
1215 } else {
1216 if (result.bufferAvailable()) {
1217 buffer = std::move(result.buffer);
1218 bufferFence = std::move(result.fence);
1219 } else {
1220 updateProtectedContentState();
1221 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1222 return;
1223 }
1224 }
1225 // Repaint the framebuffer (if needed), getting the optional fence for when
1226 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001227 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001228 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001229 if (!optReadyFence) {
1230 return;
1231 }
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001232 if (isPowerHintSessionEnabled() && !isPowerHintSessionGpuReportingEnabled()) {
Matt Buckley50c44062022-01-17 20:48:10 +00001233 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001234 setHintSessionGpuFence(
1235 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001236 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001237 // swap buffers (presentation)
Alec Mourif97df4d2023-09-06 02:10:05 +00001238 mRenderSurface->queueBuffer(std::move(*optReadyFence), getHdrSdrRatio(buffer));
Lloyd Piqued3d69882019-02-28 16:03:46 -08001239}
1240
Vishnu Naira3140382022-02-24 14:07:11 -08001241void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001242 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001243 auto& renderEngine = getCompositionEngine().getRenderEngine();
1244 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1245
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001246 bool isProtected;
1247 if (FlagManager::getInstance().display_protected()) {
1248 isProtected = outputState.isProtected;
1249 } else {
1250 isProtected = outputState.isSecure;
1251 }
1252
1253 // We need to set the render surface as protected (DRM) if all the following conditions are met:
1254 // 1. The display is protected (in legacy, check if the display is secure)
1255 // 2. Protected content is supported
1256 // 3. At least one layer has protected content.
1257 if (isProtected && supportsProtectedContent) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001258 auto layers = getOutputLayersOrderedByZ();
1259 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
Eason Chiu45099662023-10-23 08:55:48 +08001260 return layer->getLayerFE().getCompositionState()->hasProtectedContent &&
1261 (!FlagManager::getInstance().protected_if_client() ||
1262 layer->requiresClientComposition());
Lloyd Piquee9eff972020-05-05 12:36:44 -07001263 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001264 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001265 mRenderSurface->setProtected(needsProtected);
1266 }
1267 }
Vishnu Naira3140382022-02-24 14:07:11 -08001268}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001269
Vishnu Naira3140382022-02-24 14:07:11 -08001270bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1271 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1272 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001273
1274 // If we aren't doing client composition on this output, but do have a
1275 // flipClientTarget request for this frame on this output, we still need to
1276 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001277 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1278 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1279 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001280 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1281 "client composition for this frame",
1282 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001283 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001284 }
1285 }
Vishnu Naira3140382022-02-24 14:07:11 -08001286 return true;
1287}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001288
Vishnu Naira3140382022-02-24 14:07:11 -08001289std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001290 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1291 base::unique_fd& fd) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001292 SFTRACE_CALL();
Vishnu Naira3140382022-02-24 14:07:11 -08001293 ALOGV(__FUNCTION__);
1294
1295 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001296 const TracedOrdinal<bool> hasClientComposition = {
1297 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1298 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001299 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001300 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001301 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001302 }
1303
Vishnu Naira3140382022-02-24 14:07:11 -08001304 if (tex == nullptr) {
1305 ALOGW("Buffer not valid for display [%s], bailing out of "
1306 "client composition for this frame",
1307 mName.c_str());
1308 return {};
1309 }
1310
Lloyd Pique688abd42019-02-15 15:42:24 -08001311 ALOGV("hasClientComposition");
1312
Patrick Williams7584c6a2022-10-29 02:10:58 +00001313 renderengine::DisplaySettings clientCompositionDisplay =
Alec Mourif97df4d2023-09-06 02:10:05 +00001314 generateClientCompositionDisplaySettings(tex);
Lloyd Pique688abd42019-02-15 15:42:24 -08001315
Lloyd Pique688abd42019-02-15 15:42:24 -08001316 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001317 auto& renderEngine = getCompositionEngine().getRenderEngine();
1318 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001319 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001320 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001321 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001322 clientCompositionDisplay.outputDataspace,
1323 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001324 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1325
Vishnu Naira3140382022-02-24 14:07:11 -08001326 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001327 // Check if the client composition requests were rendered into the provided graphic buffer. If
1328 // so, we can reuse the buffer and avoid client composition.
1329 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001330 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1331 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001332 clientCompositionLayers)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001333 SFTRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001334 outputCompositionState.reusedClientComposition = true;
1335 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001336 // b/239944175 pass the fence associated with the buffer.
1337 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001338 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001339 SFTRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001340 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001341 clientCompositionLayers);
1342 }
1343
Lloyd Pique688abd42019-02-15 15:42:24 -08001344 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001345 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1346 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1347 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001348 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001349 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1350 [outputDataspace =
1351 clientCompositionDisplay.outputDataspace](const auto& layer) {
1352 return layer.sourceDataspace != outputDataspace;
1353 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001354 if (expensiveRenderingExpected) {
1355 setExpensiveRenderingExpected(true);
1356 }
1357
Sally Qi59a9f502021-10-12 18:53:23 +00001358 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1359 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001360 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001361 std::back_inserter(clientRenderEngineLayers),
1362 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1363 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001364 });
1365
Alec Mourie4034bb2019-11-19 12:45:54 -08001366 const nsecs_t renderEngineStart = systemTime();
Patrick Williams2e9748f2022-08-09 22:48:18 +00001367 auto fenceResult = renderEngine
1368 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Alec Mourif29700f2023-08-17 21:53:31 +00001369 std::move(fd))
Patrick Williams2e9748f2022-08-09 22:48:18 +00001370 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001371
1372 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001373 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001374 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001375 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001376 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
Xiang Wangaab31162024-03-12 19:48:08 -07001377 if (isPowerHintSessionEnabled()) {
1378 if (fence != Fence::NO_FENCE && fence->isValid() &&
1379 !outputCompositionState.reusedClientComposition) {
1380 setHintSessionRequiresRenderEngine(true);
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001381 if (isPowerHintSessionGpuReportingEnabled()) {
Xiang Wangaab31162024-03-12 19:48:08 -07001382 // the order of the two calls here matters as we should check if the previously
1383 // tracked fence has signaled first and archive the previous start time
1384 setHintSessionGpuStart(TimePoint::now());
1385 setHintSessionGpuFence(
1386 std::make_unique<FenceTime>(sp<Fence>::make(dup(fence->get()))));
1387 }
1388 }
1389 }
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001390
Patrick Williams74c0bf62022-11-02 23:59:26 +00001391 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1392 if (fence->isValid()) {
1393 timeStats->recordRenderEngineDuration(renderEngineStart,
1394 std::make_shared<FenceTime>(fence));
1395 } else {
1396 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1397 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001398 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001399
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001400 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1401 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001402 }
1403
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001404 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001405}
1406
Alec Mourif97df4d2023-09-06 02:10:05 +00001407renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings(
1408 const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
Patrick Williams7584c6a2022-10-29 02:10:58 +00001409 const auto& outputState = getState();
1410
1411 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001412 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001413 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1414 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1415 clientCompositionDisplay.orientation =
1416 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1417 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1418 ? outputState.dataspace
1419 : ui::Dataspace::UNKNOWN;
1420
1421 // If we have a valid current display brightness use that, otherwise fall back to the
1422 // display's max desired
1423 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1424 ? outputState.displayBrightnessNits
1425 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1426 clientCompositionDisplay.maxLuminance =
1427 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif97df4d2023-09-06 02:10:05 +00001428
1429 float hdrSdrRatioMultiplier = 1.0f / getHdrSdrRatio(buffer);
1430 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetBrightness *
1431 outputState.displayBrightnessNits * hdrSdrRatioMultiplier;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001432 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1433 clientCompositionDisplay.renderIntent =
1434 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1435 outputState.renderIntent);
1436
1437 // Compute the global color transform matrix.
1438 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001439 clientCompositionDisplay.deviceHandlesColorTransform =
1440 outputState.usesDeviceComposition || getSkipColorTransform();
1441 return clientCompositionDisplay;
1442}
1443
Vishnu Nair9b079a22020-01-21 14:36:08 -08001444std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001445 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001446 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001447 ALOGV("Rendering client layers");
1448
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001449 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001450 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001451 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001452
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001453 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001454 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001455
Lloyd Pique01c77c12019-04-17 12:48:32 -07001456 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001457 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001458 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001459 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001460 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001461
Lloyd Piquea2468662019-03-07 21:31:06 -08001462 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001463 ALOGV("Layer: %s", layerFE.getDebugName());
1464 if (clip.isEmpty()) {
1465 ALOGV(" Skipping for empty clip");
1466 firstLayer = false;
1467 continue;
1468 }
1469
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001470 disableBlurs |= layerFEState->sidebandStream != nullptr;
1471
Vishnu Naira483b4a2019-12-12 15:07:52 -08001472 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001473
1474 // We clear the client target for non-client composed layers if
1475 // requested by the HWC. We skip this if the layer is not an opaque
1476 // rectangle, as by definition the layer must blend with whatever is
1477 // underneath. We also skip the first layer as the buffer target is
1478 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001479 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001480 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001481
1482 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1483
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001484 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1485 // composing the non-shadow content and only draw the shadows.
1486 const bool realContentIsVisible = clientComposition &&
1487 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1488
Lloyd Pique688abd42019-02-15 15:42:24 -08001489 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001490 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1491 if (overrideSettings->bufferId != previousOverrideBufferId) {
1492 previousOverrideBufferId = overrideSettings->bufferId;
1493 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001494 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1495 } else {
1496 ALOGV("Skipping redundant override buffer for [%s] in RE",
1497 layer->getLayerFE().getDebugName());
1498 }
Dan Stoza6166c312021-01-15 16:34:05 -08001499 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001500 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1501 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1502 : (layer->getState().overrideInfo.disableBackgroundBlur
1503 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1504 BlurRegionsOnly
1505 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1506 Enabled);
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001507 bool isProtected = supportsProtectedContent;
1508 if (FlagManager::getInstance().display_protected()) {
1509 isProtected = outputState.isProtected && supportsProtectedContent;
1510 }
Alec Mourif54453c2021-05-13 16:28:28 -07001511 compositionengine::LayerFE::ClientCompositionTargetSettings
1512 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001513 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001514 outputState.needsFiltering,
1515 .isSecure = outputState.isSecure,
Chavi Weingarten18fa7c62023-11-28 21:16:03 +00001516 .isProtected = isProtected,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001517 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001518 .dataspace = outputDataspace,
1519 .realContentIsVisible = realContentIsVisible,
1520 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001521 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001522 .whitePointNits = layerState.whitePointNits,
1523 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001524 if (auto clientCompositionSettings =
1525 layerFE.prepareClientComposition(targetSettings)) {
1526 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1527 if (realContentIsVisible) {
1528 layer->editState().clientCompositionTimestamp = systemTime();
1529 }
Dan Stoza6166c312021-01-15 16:34:05 -08001530 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001531 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001532
Tianhua Sunf91f1402022-05-09 05:45:46 +00001533 if (clientComposition) {
1534 outLayerFEs.push_back(&layerFE);
1535 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001536 }
1537
1538 firstLayer = false;
1539 }
1540
1541 return clientCompositionLayers;
1542}
1543
1544void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001545 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001546 if (flashRegion.isEmpty()) {
1547 return;
1548 }
1549
Vishnu Nair9b079a22020-01-21 14:36:08 -08001550 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001551 layerSettings.source.buffer.buffer = nullptr;
1552 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1553 layerSettings.alpha = half(1.0);
1554
1555 for (const auto& rect : flashRegion) {
1556 layerSettings.geometry.boundaries = rect.toFloatRect();
1557 clientCompositionLayers.push_back(layerSettings);
1558 }
1559}
1560
1561void Output::setExpensiveRenderingExpected(bool) {
1562 // The base class does nothing with this call.
1563}
1564
Xiang Wangaab31162024-03-12 19:48:08 -07001565void Output::setHintSessionGpuStart(TimePoint) {
1566 // The base class does nothing with this call.
1567}
1568
Matt Buckley50c44062022-01-17 20:48:10 +00001569void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1570 // The base class does nothing with this call.
1571}
1572
Xiang Wangaab31162024-03-12 19:48:08 -07001573void Output::setHintSessionRequiresRenderEngine(bool) {
1574 // The base class does nothing with this call.
1575}
1576
Matt Buckley50c44062022-01-17 20:48:10 +00001577bool Output::isPowerHintSessionEnabled() {
1578 return false;
1579}
1580
Xiang Wangcb50bbd2024-04-18 16:57:54 -07001581bool Output::isPowerHintSessionGpuReportingEnabled() {
1582 return false;
1583}
1584
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001585void Output::presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001586 SFTRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001587 ALOGV(__FUNCTION__);
1588
1589 if (!getState().isEnabled) {
Leon Scroggins IIIa3ba7fa2024-05-22 16:34:52 -04001590 if (flushEvenWhenDisabled && FlagManager::getInstance().flush_buffer_slots_to_uncache()) {
1591 // Some commands, like clearing buffer slots, should still be executed
1592 // even if the display is not enabled.
1593 executeCommands();
1594 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001595 return;
1596 }
1597
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001598 auto& outputState = editState();
1599 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001600
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001601 auto frame = presentFrame();
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001602
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001603 mRenderSurface->onPresentDisplayCompleted();
1604
Lloyd Pique01c77c12019-04-17 12:48:32 -07001605 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001606 // The layer buffer from the previous frame (if any) is released
1607 // by HWC only when the release fence from this frame (if any) is
1608 // signaled. Always get the release fence from HWC first.
1609 sp<Fence> releaseFence = Fence::NO_FENCE;
1610
1611 if (auto hwcLayer = layer->getHwcLayer()) {
1612 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1613 releaseFence = f->second;
1614 }
1615 }
1616
1617 // If the layer was client composited in the previous frame, we
1618 // need to merge with the previous client target acquire fence.
1619 // Since we do not track that, always merge with the current
1620 // client target acquire fence when it is available, even though
1621 // this is suboptimal.
1622 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001623 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001624 releaseFence =
1625 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1626 }
Melody Hsu793f8362024-01-08 20:00:35 +00001627 if (FlagManager::getInstance().ce_fence_promise()) {
1628 layer->getLayerFE().setReleaseFence(releaseFence);
1629 } else {
1630 layer->getLayerFE()
1631 .onLayerDisplayed(ftl::yield<FenceResult>(std::move(releaseFence)).share(),
1632 outputState.layerFilter.layerStack);
1633 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001634 }
1635
1636 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001637 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001638 // supply them with the present fence.
1639 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001640 if (const auto layer = weakLayer.promote()) {
Melody Hsu793f8362024-01-08 20:00:35 +00001641 if (FlagManager::getInstance().ce_fence_promise()) {
1642 layer->setReleaseFence(frame.presentFence);
1643 } else {
1644 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share(),
1645 outputState.layerFilter.layerStack);
1646 }
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001647 }
1648 }
1649
1650 // Clear out the released layers now that we're done with them.
1651 mReleasedLayers.clear();
1652}
1653
Alec Mouriaa831582021-06-07 16:23:01 -07001654void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III43b5d522023-04-10 15:53:45 -04001655 const auto& outputState = getState();
1656 if (mPlanner && outputState.isEnabled) {
1657 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1658 outputState.usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001659 }
1660}
1661
Lloyd Pique32cbe282018-10-19 13:09:22 -07001662void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001663 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001664 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001665}
1666
Vishnu Naira3140382022-02-24 14:07:11 -08001667void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001668 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001669 auto& outputState = editState();
1670 outputState.usesClientComposition = true;
1671 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001672 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001673}
1674
Lloyd Pique688abd42019-02-15 15:42:24 -08001675bool Output::getSkipColorTransform() const {
1676 return true;
1677}
1678
Leon Scroggins IIIc1623d12023-11-06 15:31:05 -05001679compositionengine::Output::FrameFences Output::presentFrame() {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001680 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001681 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001682 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1683 }
1684 return result;
1685}
1686
Vishnu Naira3140382022-02-24 14:07:11 -08001687void Output::setPredictCompositionStrategy(bool predict) {
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001688 mPredictCompositionStrategy = predict;
1689 updateHwcAsyncWorker();
1690}
1691
1692void Output::updateHwcAsyncWorker() {
1693 if (mPredictCompositionStrategy || mOffloadPresent) {
1694 if (!mHwComposerAsyncWorker) {
1695 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1696 }
Vishnu Naira3140382022-02-24 14:07:11 -08001697 } else {
1698 mHwComposerAsyncWorker.reset(nullptr);
1699 }
1700}
1701
Alec Mouridda07d92022-04-25 22:39:25 +00001702void Output::setTreat170mAsSrgb(bool enable) {
1703 editState().treat170mAsSrgb = enable;
1704}
1705
Vishnu Naira3140382022-02-24 14:07:11 -08001706bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001707 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1708 uint64_t outputLayerHash = getState().outputLayerHash;
1709 editState().lastOutputLayerHash = outputLayerHash;
1710
Leon Scroggins III2f60d732022-09-12 14:42:38 -04001711 if (!getState().isEnabled || !mPredictCompositionStrategy) {
Vishnu Naira3140382022-02-24 14:07:11 -08001712 ALOGV("canPredictCompositionStrategy disabled");
1713 return false;
1714 }
1715
1716 if (!getState().previousDeviceRequestedChanges) {
1717 ALOGV("canPredictCompositionStrategy previous changes not available");
1718 return false;
1719 }
1720
1721 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1722 ALOGV("canPredictCompositionStrategy surface does not support");
1723 return false;
1724 }
1725
1726 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1727 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1728 return false;
1729 }
1730
Robert Carrec8ccca2022-05-04 09:36:14 -07001731 if (lastOutputLayerHash != outputLayerHash) {
1732 ALOGV("canPredictCompositionStrategy output layers changed");
1733 return false;
1734 }
1735
Vishnu Naira3140382022-02-24 14:07:11 -08001736 // If no layer uses clientComposition, then don't predict composition strategy
1737 // because we have less work to do in parallel.
1738 if (!anyLayersRequireClientComposition()) {
1739 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1740 return false;
1741 }
1742
Robert Carrec8ccca2022-05-04 09:36:14 -07001743 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001744}
1745
1746bool Output::anyLayersRequireClientComposition() const {
1747 const auto layers = getOutputLayersOrderedByZ();
1748 return std::any_of(layers.begin(), layers.end(),
1749 [](const auto& layer) { return layer->requiresClientComposition(); });
1750}
1751
1752void Output::finishPrepareFrame() {
1753 const auto& state = getState();
1754 if (mPlanner) {
1755 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1756 }
1757 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1758}
1759
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001760bool Output::mustRecompose() const {
1761 return mMustRecompose;
1762}
1763
Alec Mourif97df4d2023-09-06 02:10:05 +00001764float Output::getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
1765 if (buffer == nullptr) {
1766 return 1.0f;
1767 }
1768
1769 if (!FlagManager::getInstance().fp16_client_target()) {
1770 return 1.0f;
1771 }
1772
1773 if (getState().displayBrightnessNits < 0.0f || getState().sdrWhitePointNits <= 0.0f ||
1774 buffer->getPixelFormat() != PIXEL_FORMAT_RGBA_FP16 ||
1775 (static_cast<int32_t>(getState().dataspace) &
1776 static_cast<int32_t>(ui::Dataspace::RANGE_MASK)) !=
1777 static_cast<int32_t>(ui::Dataspace::RANGE_EXTENDED)) {
1778 return 1.0f;
1779 }
1780
1781 return getState().displayBrightnessNits / getState().sdrWhitePointNits;
1782}
1783
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001784} // namespace impl
1785} // namespace android::compositionengine