blob: 6b0fc99897455511a421f73a83150345b2322a0e [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>
19#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080020#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070021#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080022#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070023#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070024#include <compositionengine/RenderSurface.h>
Vishnu Naira3140382022-02-24 14:07:11 -080025#include <compositionengine/impl/HwcAsyncWorker.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080030#include <compositionengine/impl/planner/Planner.h>
Sally Qi59a9f502021-10-12 18:53:23 +000031#include <ftl/future.h>
Leon Scroggins III5a655b82022-09-07 13:17:09 -040032#include <gui/TraceUtils.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080033
Chavi Weingarten545da0e2023-02-09 14:55:57 +000034#include <optional>
Alec Mouria90a5702021-04-16 16:36:21 +000035#include <thread>
36
37#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080038
39// TODO(b/129481165): remove the #pragma below and fix conversion issues
40#pragma clang diagnostic push
41#pragma clang diagnostic ignored "-Wconversion"
42
Lloyd Pique688abd42019-02-15 15:42:24 -080043#include <renderengine/DisplaySettings.h>
44#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080045
46// TODO(b/129481165): remove the #pragma below and fix conversion issues
47#pragma clang diagnostic pop // ignored "-Wconversion"
48
Dan Stoza269dc4d2021-01-15 15:07:43 -080049#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070050#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080051#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080052#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070053
Lloyd Pique688abd42019-02-15 15:42:24 -080054#include "TracedOrdinal.h"
55
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050056using aidl::android::hardware::graphics::composer3::Composition;
57
Lloyd Piquefeb73d72018-12-04 17:23:44 -080058namespace android::compositionengine {
59
60Output::~Output() = default;
61
62namespace impl {
Vishnu Nair9cf89262022-02-26 09:17:49 -080063using CompositionStrategyPredictionState =
64 OutputCompositionState::CompositionStrategyPredictionState;
Lloyd Piquec29e4c62019-03-07 21:48:19 -080065namespace {
66
67template <typename T>
68class Reversed {
69public:
70 explicit Reversed(const T& container) : mContainer(container) {}
71 auto begin() { return mContainer.rbegin(); }
72 auto end() { return mContainer.rend(); }
73
74private:
75 const T& mContainer;
76};
77
78// Helper for enumerating over a container in reverse order
79template <typename T>
80Reversed<T> reversed(const T& c) {
81 return Reversed<T>(c);
82}
83
Marin Shalamanovb15d2272020-09-17 21:41:52 +020084struct ScaleVector {
85 float x;
86 float y;
87};
88
89// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
90// where to' will have the same size as "to". In the case where "from" and "to"
91// start at the origin to'=to.
92ScaleVector getScale(const Rect& from, const Rect& to) {
93 return {.x = static_cast<float>(to.width()) / from.width(),
94 .y = static_cast<float>(to.height()) / from.height()};
95}
96
Lloyd Piquec29e4c62019-03-07 21:48:19 -080097} // namespace
98
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070099std::shared_ptr<Output> createOutput(
100 const compositionengine::CompositionEngine& compositionEngine) {
101 return createOutputTemplated<Output>(compositionEngine);
102}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103
104Output::~Output() = default;
105
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700107 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
108 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109}
110
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700111std::optional<DisplayId> Output::getDisplayId() const {
112 return {};
113}
114
Lloyd Pique32cbe282018-10-19 13:09:22 -0700115const std::string& Output::getName() const {
116 return mName;
117}
118
119void Output::setName(const std::string& name) {
120 mName = name;
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400121 auto displayIdOpt = getDisplayId();
Leon Scroggins IIIc03d4652023-01-05 13:03:53 -0500122 mNamePlusId = displayIdOpt ? base::StringPrintf("%s (%s)", mName.c_str(),
123 to_string(*displayIdOpt).c_str())
124 : mName;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700125}
126
127void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700128 auto& outputState = editState();
129 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130 return;
131 }
132
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700133 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700134 dirtyEntireOutput();
135}
136
Alec Mouri023c1882021-05-08 16:36:33 -0700137void Output::setLayerCachingEnabled(bool enabled) {
138 if (enabled == (mPlanner != nullptr)) {
139 return;
140 }
141
142 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700143 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700144 if (mRenderSurface) {
145 mPlanner->setDisplaySize(mRenderSurface->getSize());
146 }
147 } else {
148 mPlanner.reset();
149 }
Alec Mouric773472b2021-05-19 14:29:05 -0700150
151 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
152 if (!outputLayer) {
153 continue;
154 }
155
156 outputLayer->editState().overrideInfo = {};
157 }
Alec Mouri023c1882021-05-08 16:36:33 -0700158}
159
Ady Abrahamdb036a82021-07-16 14:18:34 -0700160void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
161 if (mPlanner) {
162 mPlanner->setTexturePoolEnabled(enabled);
163 }
164}
165
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200166void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
167 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700168 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200169
Angel Aguayob084e0c2021-08-04 23:27:28 +0000170 outputState.displaySpace.setOrientation(orientation);
171 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200172 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200173
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200174 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000175 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200176 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200177 std::swap(orientedSize.width, orientedSize.height);
178 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000179 outputState.orientedDisplaySpace.setBounds(orientedSize);
180 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200181
182 // Compute displaySpace.content
183 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
184 ui::Transform rotation;
185 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000186 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200187 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
188 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000189 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200190
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000192 outputState.framebufferSpace.setOrientation(orientation);
193 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200194 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000195 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
196 outputState.framebufferSpace.getBoundsAsRect());
197 outputState.framebufferSpace.setContent(
198 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199
200 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000201 outputState.layerStackSpace.setContent(layerStackSpaceRect);
202 outputState.layerStackSpace.setBounds(
203 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200204
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200205 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
206 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700207 dirtyEntireOutput();
208}
209
Alec Mouricdf16792021-12-10 13:16:06 -0800210void Output::setNextBrightness(float brightness) {
211 editState().displayBrightness = brightness;
212}
213
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200214void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700215 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200216
217 auto& state = editState();
218
219 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000220 const ui::Size newBounds(size);
221 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200222
223 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000224 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200225 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
226
227 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000228 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200229 ui::Size orientedSize = size;
230 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
231 std::swap(orientedSize.width, orientedSize.height);
232 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000233 const ui::Size newOrientedBounds(orientedSize);
234 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700235
Dan Stoza6166c312021-01-15 16:34:05 -0800236 if (mPlanner) {
237 mPlanner->setDisplaySize(size);
238 }
239
Lloyd Pique32cbe282018-10-19 13:09:22 -0700240 dirtyEntireOutput();
241}
242
Garfield Tan54edd912020-10-21 16:31:41 -0700243ui::Transform::RotationFlags Output::getTransformHint() const {
244 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
245}
246
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700247void Output::setLayerFilter(ui::LayerFilter filter) {
248 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249 dirtyEntireOutput();
250}
251
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800252void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700253 auto& colorTransformMatrix = editState().colorTransformMatrix;
254 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700255 return;
256 }
257
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700258 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800259
260 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700261}
262
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800263void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700264 auto& outputState = editState();
265 if (outputState.colorMode == colorProfile.mode &&
266 outputState.dataspace == colorProfile.dataspace &&
Alec Mouri88790f32023-07-21 01:25:14 +0000267 outputState.renderIntent == colorProfile.renderIntent) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800268 return;
269 }
270
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700271 outputState.colorMode = colorProfile.mode;
272 outputState.dataspace = colorProfile.dataspace;
273 outputState.renderIntent = colorProfile.renderIntent;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700274
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800275 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700276
Lloyd Pique32cbe282018-10-19 13:09:22 -0700277 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800278 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
279 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800280
281 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700282}
283
John Reckac09e452021-04-07 16:35:37 -0400284void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
285 auto& outputState = editState();
286 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
287 outputState.displayBrightnessNits == displayBrightnessNits) {
288 // Nothing changed
289 return;
290 }
291 outputState.sdrWhitePointNits = sdrWhitePointNits;
292 outputState.displayBrightnessNits = displayBrightnessNits;
293 dirtyEntireOutput();
294}
295
Lloyd Pique32cbe282018-10-19 13:09:22 -0700296void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700297 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
298 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700299
300 dumpBase(out);
301}
302
303void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700305 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700306
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700307 if (mDisplayColorProfile) {
308 mDisplayColorProfile->dump(out);
309 } else {
310 out.append(" No display color profile!\n");
311 }
312
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700313 out += '\n';
314
Lloyd Pique31cb2942018-10-19 17:23:03 -0700315 if (mRenderSurface) {
316 mRenderSurface->dump(out);
317 } else {
318 out.append(" No render surface!\n");
319 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800320
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700321 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700322 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800323 if (!outputLayer) {
324 continue;
325 }
326 outputLayer->dump(out);
327 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700328}
329
Dan Stoza269dc4d2021-01-15 15:07:43 -0800330void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
331 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700332 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800333 return;
334 }
335 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
336 mPlanner->dump(args, out);
337}
338
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700339compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
340 return mDisplayColorProfile.get();
341}
342
343void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
344 mDisplayColorProfile = std::move(mode);
345}
346
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800347const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
348 return mReleasedLayers;
349}
350
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700351void Output::setDisplayColorProfileForTest(
352 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
353 mDisplayColorProfile = std::move(mode);
354}
355
Lloyd Pique31cb2942018-10-19 17:23:03 -0700356compositionengine::RenderSurface* Output::getRenderSurface() const {
357 return mRenderSurface.get();
358}
359
360void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
361 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800362 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000363 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800364 if (mPlanner) {
365 mPlanner->setDisplaySize(size);
366 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700367 dirtyEntireOutput();
368}
369
Vishnu Nair9b079a22020-01-21 14:36:08 -0800370void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
371 if (cacheSize == 0) {
372 mClientCompositionRequestCache.reset();
373 } else {
374 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
375 }
376};
377
Lloyd Pique31cb2942018-10-19 17:23:03 -0700378void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
379 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700380}
381
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700382Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700383 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000384 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700385}
386
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700387bool Output::includesLayer(ui::LayerFilter filter) const {
388 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700389}
390
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700391bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800392 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700393 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800394}
395
Lloyd Piquedf336d92019-03-07 21:38:42 -0800396std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800397 const sp<LayerFE>& layerFE) const {
398 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800399}
400
Lloyd Piquede196652020-01-22 17:29:58 -0800401compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
402 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700403 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800404}
405
Lloyd Pique01c77c12019-04-17 12:48:32 -0700406std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800407 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700408 for (size_t i = 0; i < getOutputLayerCount(); i++) {
409 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800410 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700411 return i;
412 }
413 }
414 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800415}
416
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800417void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
418 mReleasedLayers = std::move(layers);
419}
420
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800421void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
422 LayerFESet& geomSnapshots) {
423 ATRACE_CALL();
424 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800425
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800426 rebuildLayerStacks(refreshArgs, geomSnapshots);
Brian Lindahl439afad2022-11-14 11:16:55 -0700427 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800428}
429
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800430void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400431 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800432 ALOGV(__FUNCTION__);
433
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800434 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800435 updateCompositionState(refreshArgs);
436 planComposition();
437 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800438 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800439 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800440
441 GpuCompositionResult result;
442 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
443 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000444 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800445 } else {
446 prepareFrame();
447 }
448
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800449 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000450 finishFrame(std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800451 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700452 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800453}
454
Brian Lindahl439afad2022-11-14 11:16:55 -0700455void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
456 if (bufferIdsToUncache.empty()) {
457 return;
458 }
459 for (auto outputLayer : getOutputLayersOrderedByZ()) {
460 outputLayer->uncacheBuffers(bufferIdsToUncache);
461 }
462}
463
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800464void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
465 LayerFESet& layerFESet) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700466 auto& outputState = editState();
467
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700469 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800470 return;
471 }
Vishnu Naird9a640b2023-07-21 14:20:27 +0000472 ATRACE_CALL();
473 ALOGV(__FUNCTION__);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800474
475 // Process the layers to determine visibility and coverage
476 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000477 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
478 ? std::make_optional<Region>()
479 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800480 collectVisibleLayers(refreshArgs, coverage);
481
482 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700483 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000484 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800485 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
486
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700487 outputState.undefinedRegion = undefinedRegion;
488 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800489}
490
491void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
492 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800493 // Evaluate the layers from front to back to determine what is visible. This
494 // also incrementally calculates the coverage information for each layer as
495 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800496 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700497 // Incrementally process the coverage for each layer
498 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800499
500 // TODO(b/121291683): Stop early if the output is completely covered and
501 // no more layers could even be visible underneath the ones on top.
502 }
503
Lloyd Pique01c77c12019-04-17 12:48:32 -0700504 setReleasedLayers(refreshArgs);
505
506 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800507}
508
Lloyd Piquede196652020-01-22 17:29:58 -0800509void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700510 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800511 // Ensure we have a snapshot of the basic geometry layer state. Limit the
512 // snapshots to once per frame for each candidate layer, as layers may
513 // appear on multiple outputs.
514 if (!coverage.latchedLayers.count(layerFE)) {
515 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800516 }
517
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700518 // Only consider the layers on this output
519 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800520 return;
521 }
522
523 // Obtain a read-only pointer to the front-end layer state
524 const auto* layerFEState = layerFE->getCompositionState();
525 if (CC_UNLIKELY(!layerFEState)) {
526 return;
527 }
528
529 // handle hidden surfaces by setting the visible region to empty
530 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700531 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800532 }
533
Vishnu Naird47bcee2023-02-24 18:08:51 +0000534 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
535 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000536
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800537 /*
538 * opaqueRegion: area of a surface that is fully opaque.
539 */
540 Region opaqueRegion;
541
542 /*
543 * visibleRegion: area of a surface that is visible on screen and not fully
544 * transparent. This is essentially the layer's footprint minus the opaque
545 * regions above it. Areas covered by a translucent surface are considered
546 * visible.
547 */
548 Region visibleRegion;
549
550 /*
551 * coveredRegion: area of a surface that is covered by all visible regions
552 * above it (which includes the translucent areas).
553 */
554 Region coveredRegion;
555
556 /*
557 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500558 * transparent.
559 * This is used to tell when the layer has no visible non-transparent
560 * regions and can be removed from the layer list. It does not affect the
561 * visibleRegion of this layer or any layers beneath it. The hint may not
562 * be correct if apps don't respect the SurfaceView restrictions (which,
563 * sadly, some don't).
564 *
565 * In addition, it is used on DISPLAY_DECORATION layers to specify the
566 * blockingRegion, allowing the DPU to skip it to save power. Once we have
567 * hardware that supports a blockingRegion on frames with AFBC, it may be
568 * useful to use this for other layers, too, so long as we can prevent
569 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800570 */
571 Region transparentRegion;
572
Vishnu Naira483b4a2019-12-12 15:07:52 -0800573 /*
574 * shadowRegion: Region cast by the layer's shadow.
575 */
576 Region shadowRegion;
577
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000578 /**
579 * covered region above excluding internal display overlay layers
580 */
581 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
582
Lloyd Piquede196652020-01-22 17:29:58 -0800583 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800584
585 // Get the visible region
586 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
587 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800588 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800589 visibleRegion.set(visibleRect);
590
Vishnu Naird9e4f462023-10-06 04:05:45 +0000591 if (layerFEState->shadowSettings.length > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800592 // if the layer casts a shadow, offset the layers visible region and
593 // calculate the shadow region.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000594 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowSettings.length) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800595 Rect visibleRectWithShadows(visibleRect);
596 visibleRectWithShadows.inset(inset, inset, inset, inset);
597 visibleRegion.set(visibleRectWithShadows);
598 shadowRegion = visibleRegion.subtract(visibleRect);
599 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800600
601 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700602 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800603 }
604
605 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800606 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800607 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000608 // Clip the transparent region to geomLayerBounds first
609 // The transparent region may be influenced by applications, for
610 // instance, by overriding ViewGroup#gatherTransparentRegion with a
611 // custom view. Once the layer stack -> display mapping is known, we
612 // must guard against very wrong inputs to prevent underflow or
613 // overflow errors. We do this here by constraining the transparent
614 // region to be within the pre-transform layer bounds, since the
615 // layer bounds are expected to play nicely with the full
616 // transform.
617 const Region clippedTransparentRegionHint =
618 layerFEState->transparentRegionHint.intersect(
619 Rect(layerFEState->geomLayerBounds));
620
621 if (clippedTransparentRegionHint.isEmpty()) {
622 if (!layerFEState->transparentRegionHint.isEmpty()) {
623 ALOGD("Layer: %s had an out of bounds transparent region",
624 layerFE->getDebugName());
625 layerFEState->transparentRegionHint.dump("transparentRegionHint");
626 }
627 transparentRegion.clear();
628 } else {
629 transparentRegion = tr.transform(clippedTransparentRegionHint);
630 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800631 } else {
632 // transformation too complex, can't do the
633 // transparent region optimization.
634 transparentRegion.clear();
635 }
636 }
637
638 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800639 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800640 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800641 // If we one of the simple category of transforms (0/90/180/270 rotation
642 // + any flip), then the opaque region is the layer's footprint.
643 // Otherwise we don't try and compute the opaque region since there may
644 // be errors at the edges, and we treat the entire layer as
645 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800646 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800647 }
648
649 // Clip the covered region to the visible region
650 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
651
652 // Update accumAboveCoveredLayers for next (lower) layer
653 coverage.aboveCoveredLayers.orSelf(visibleRegion);
654
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000655 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
656 coveredRegionExcludingDisplayOverlays =
657 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
658 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
659 }
660
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800661 // subtract the opaque region covered by the layers above us
662 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
663
664 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700665 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800666 }
667
668 // Get coverage information for the layer as previously displayed,
669 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800670 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700671 auto prevOutputLayer =
672 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800673
674 // Get coverage information for the layer as previously displayed
675 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
676 const Region kEmptyRegion;
677 const Region& oldVisibleRegion =
678 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
679 const Region& oldCoveredRegion =
680 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
681
682 // compute this layer's dirty region
683 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800684 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800685 // we need to invalidate the whole region
686 dirty = visibleRegion;
687 // as well, as the old visible region
688 dirty.orSelf(oldVisibleRegion);
689 } else {
690 /* compute the exposed region:
691 * the exposed region consists of two components:
692 * 1) what's VISIBLE now and was COVERED before
693 * 2) what's EXPOSED now less what was EXPOSED before
694 *
695 * note that (1) is conservative, we start with the whole visible region
696 * but only keep what used to be covered by something -- which mean it
697 * may have been exposed.
698 *
699 * (2) handles areas that were not covered by anything but got exposed
700 * because of a resize.
701 *
702 */
703 const Region newExposed = visibleRegion - coveredRegion;
704 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
705 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
706 }
707 dirty.subtractSelf(coverage.aboveOpaqueLayers);
708
709 // accumulate to the screen dirty region
710 coverage.dirtyRegion.orSelf(dirty);
711
712 // Update accumAboveOpaqueLayers for next (lower) layer
713 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
714
715 // Compute the visible non-transparent region
716 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
717
Vishnu Naira483b4a2019-12-12 15:07:52 -0800718 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800719 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700720 const auto& outputState = getState();
721 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000722 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800723 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700724 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800725 }
726
Vishnu Naira483b4a2019-12-12 15:07:52 -0800727 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
728
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800729 // The layer is visible. Either reuse the existing outputLayer if we have
730 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800731 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800732
733 // Store the layer coverage information into the layer state as some of it
734 // is useful later.
735 auto& outputLayerState = result->editState();
736 outputLayerState.visibleRegion = visibleRegion;
737 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
738 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200739 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000740 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800741 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500742 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400743 layerFEState->compositionType == Composition::DISPLAY_DECORATION
744 ? outputState.transform.transform(
745 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
746 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000747 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
748 outputLayerState.coveredRegionExcludingDisplayOverlays =
749 std::move(coveredRegionExcludingDisplayOverlays);
750 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800751}
752
753void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
754 // The base class does nothing with this call.
755}
756
Dan Stoza269dc4d2021-01-15 15:07:43 -0800757void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800758 ATRACE_CALL();
759 ALOGV(__FUNCTION__);
760
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800761 if (!getState().isEnabled) {
762 return;
763 }
764
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800765 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
766 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
767
Lloyd Pique01c77c12019-04-17 12:48:32 -0700768 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700769 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800770 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200771 forceClientComposition,
772 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800773
774 if (mLayerRequestingBackgroundBlur == layer) {
775 forceClientComposition = false;
776 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800777 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000778
779 updateCompositionStateForBorder(refreshArgs);
780}
781
782void Output::updateCompositionStateForBorder(
783 const compositionengine::CompositionRefreshArgs& refreshArgs) {
784 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
785 // Store a map of layerId to their computed visible region.
786 for (auto* layer : getOutputLayersOrderedByZ()) {
787 int layerId = (layer->getLayerFE()).getSequence();
788 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
789 }
790 OutputCompositionState& outputCompositionState = editState();
791 outputCompositionState.borderInfoList.clear();
792 bool clientComposeTopLayer = false;
793 for (const auto& borderInfo : refreshArgs.borderInfoList) {
794 renderengine::BorderRenderInfo info;
795 for (const auto& id : borderInfo.layerIds) {
796 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
797 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000798
799 if (!info.combinedRegion.isEmpty()) {
800 info.width = borderInfo.width;
801 info.color = borderInfo.color;
802 outputCompositionState.borderInfoList.emplace_back(std::move(info));
803 clientComposeTopLayer = true;
804 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000805 }
806
807 // In this situation we must client compose the top layer instead of using hwc
808 // because we want to draw the border above all else.
809 // This could potentially cause a bit of a performance regression if the top
810 // layer would have been rendered using hwc originally.
811 // TODO(b/227656283): Measure system's performance before enabling the border feature
812 if (clientComposeTopLayer) {
813 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
814 (topLayer->editState()).forceClientComposition = true;
815 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800816}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800817
Dan Stoza269dc4d2021-01-15 15:07:43 -0800818void Output::planComposition() {
819 if (!mPlanner || !getState().isEnabled) {
820 return;
821 }
822
823 ATRACE_CALL();
824 ALOGV(__FUNCTION__);
825
826 mPlanner->plan(getOutputLayersOrderedByZ());
827}
828
829void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
830 ATRACE_CALL();
831 ALOGV(__FUNCTION__);
832
833 if (!getState().isEnabled) {
834 return;
835 }
836
Ady Abraham3645e642021-04-20 18:39:00 -0700837 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abraham43065bd2021-12-10 17:22:15 -0800838 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
ramindani4aac32c2023-10-30 14:13:30 -0700839 editState().frameInterval = refreshArgs.frameInterval;
jimmyshiu4e211772023-06-15 15:18:38 +0000840 editState().powerCallback = refreshArgs.powerCallback;
Ady Abraham3645e642021-04-20 18:39:00 -0700841
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400842 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800843 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400844 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400845 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400846 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700847 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800848 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400849 if (layer == peekThroughLayer) {
850 // No longer needed, although it should not show up again, so
851 // resetting it is not truly needed either.
852 peekThroughLayer = nullptr;
853
854 // peekThroughLayer was already drawn ahead of its z order.
855 continue;
856 }
Dan Stoza6166c312021-01-15 16:34:05 -0800857 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400858 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400859 if (overrideInfo.buffer != nullptr) {
860 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800861 ALOGV("Skipping redundant buffer");
862 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400863 } else {
864 // First layer with the override buffer.
865 if (overrideInfo.peekThroughLayer) {
866 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400867
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400868 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400869 overrideZ = true;
870 includeGeometry = true;
871 constexpr bool isPeekingThrough = true;
872 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
873 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700874 outputLayerHash ^= android::hashCombine(
875 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
876 z, includeGeometry, overrideZ, isPeekingThrough,
877 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400878 }
879
880 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800881 }
Dan Stoza6166c312021-01-15 16:34:05 -0800882 }
883
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400884 constexpr bool isPeekingThrough = false;
885 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700886 if (!skipLayer) {
887 outputLayerHash ^= android::hashCombine(
888 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
889 z, includeGeometry, overrideZ, isPeekingThrough,
890 layer->requiresClientComposition());
891 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800892 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700893 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800894}
895
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800896compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
897 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
898 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400899 const auto* compState = layer->getLayerFE().getCompositionState();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100900
901 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
902 // want to force client composition because of the blur.
903 if (compState->sidebandStream != nullptr) {
904 return nullptr;
905 }
Leon Scroggins IIIc1dbfcb2022-03-21 16:48:10 -0400906
907 // If RenderEngine cannot render protected content, we cannot blur.
908 if (compState->hasProtectedContent &&
909 !getCompositionEngine().getRenderEngine().supportsProtectedContent()) {
910 return nullptr;
911 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000912 if (compState->isOpaque) {
913 continue;
914 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100915 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800916 layerRequestingBgComposition = layer;
917 }
918 }
919 return layerRequestingBgComposition;
920}
921
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800922void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
923 setColorProfile(pickColorProfile(refreshArgs));
924}
925
926// Returns a data space that fits all visible layers. The returned data space
927// can only be one of
928// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
929// - Dataspace::DISPLAY_P3
930// - Dataspace::DISPLAY_BT2020
931// The returned HDR data space is one of
932// - Dataspace::UNKNOWN
933// - Dataspace::BT2020_HLG
934// - Dataspace::BT2020_PQ
935ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
936 bool* outIsHdrClientComposition) const {
937 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
938 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
939
Vishnu Naire14c6b32022-08-06 04:20:15 +0000940 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
941 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
942 // properties. Return a default dataspace value in this case.
943 if (!getState().isEnabled) {
944 return ui::Dataspace::V0_SRGB;
945 }
946
Lloyd Pique01c77c12019-04-17 12:48:32 -0700947 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800948 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800949 case ui::Dataspace::V0_SCRGB:
950 case ui::Dataspace::V0_SCRGB_LINEAR:
951 case ui::Dataspace::BT2020:
952 case ui::Dataspace::BT2020_ITU:
953 case ui::Dataspace::BT2020_LINEAR:
954 case ui::Dataspace::DISPLAY_BT2020:
955 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
956 break;
957 case ui::Dataspace::DISPLAY_P3:
958 bestDataSpace = ui::Dataspace::DISPLAY_P3;
959 break;
960 case ui::Dataspace::BT2020_PQ:
961 case ui::Dataspace::BT2020_ITU_PQ:
962 bestDataSpace = ui::Dataspace::DISPLAY_P3;
963 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800964 *outIsHdrClientComposition =
965 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800966 break;
967 case ui::Dataspace::BT2020_HLG:
968 case ui::Dataspace::BT2020_ITU_HLG:
969 bestDataSpace = ui::Dataspace::DISPLAY_P3;
970 // When there's mixed PQ content and HLG content, we set the HDR
Sally Qi37d07c02023-10-05 17:32:32 +0000971 // data space to be BT2020_HLG and convert PQ to HLG.
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800972 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
973 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
974 }
975 break;
976 default:
977 break;
978 }
979 }
980
981 return bestDataSpace;
982}
983
984compositionengine::Output::ColorProfile Output::pickColorProfile(
985 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
986 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
987 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Alec Mouri88790f32023-07-21 01:25:14 +0000988 ui::RenderIntent::COLORIMETRIC};
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800989 }
990
991 ui::Dataspace hdrDataSpace;
992 bool isHdrClientComposition = false;
993 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
994
995 switch (refreshArgs.forceOutputColorMode) {
996 case ui::ColorMode::SRGB:
997 bestDataSpace = ui::Dataspace::V0_SRGB;
998 break;
999 case ui::ColorMode::DISPLAY_P3:
1000 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1001 break;
1002 default:
1003 break;
1004 }
1005
1006 // respect hdrDataSpace only when there is no legacy HDR support
1007 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1008 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1009 if (isHdr) {
1010 bestDataSpace = hdrDataSpace;
1011 }
1012
1013 ui::RenderIntent intent;
1014 switch (refreshArgs.outputColorSetting) {
1015 case OutputColorSetting::kManaged:
1016 case OutputColorSetting::kUnmanaged:
1017 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1018 : ui::RenderIntent::COLORIMETRIC;
1019 break;
1020 case OutputColorSetting::kEnhanced:
1021 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1022 break;
1023 default: // vendor display color setting
1024 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1025 break;
1026 }
1027
1028 ui::ColorMode outMode;
1029 ui::Dataspace outDataSpace;
1030 ui::RenderIntent outRenderIntent;
1031 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1032 &outRenderIntent);
1033
Alec Mouri88790f32023-07-21 01:25:14 +00001034 return ColorProfile{outMode, outDataSpace, outRenderIntent};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001035}
1036
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001037void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001038 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001039 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001040 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001041 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001042
1043 // If nothing has changed (!dirty), don't recompose.
1044 // If something changed, but we don't currently have any visible layers,
1045 // and didn't when we last did a composition, then skip it this time.
1046 // The second rule does two things:
1047 // - When all layers are removed from a display, we'll emit one black
1048 // frame, then nothing more until we get new layers.
1049 // - When a display is created with a private layer stack, we won't
1050 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001051 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001052
1053 const char flagPrefix[] = {'-', '+'};
1054 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001055 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1056 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1057 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001058
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001059 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001060
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001061 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001062 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001063 }
1064}
1065
Lloyd Pique66d68602019-02-13 14:23:31 -08001066void Output::prepareFrame() {
1067 ATRACE_CALL();
1068 ALOGV(__FUNCTION__);
1069
Vishnu Naira3140382022-02-24 14:07:11 -08001070 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001071 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001072 return;
1073 }
1074
Vishnu Naira3140382022-02-24 14:07:11 -08001075 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1076 bool success = chooseCompositionStrategy(&changes);
1077 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001078 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001079 outputState.previousDeviceRequestedChanges = changes;
1080 outputState.previousDeviceRequestedSuccess = success;
1081 if (success) {
1082 applyCompositionStrategy(changes);
1083 }
1084 finishPrepareFrame();
1085}
Lloyd Pique66d68602019-02-13 14:23:31 -08001086
Vishnu Naira3140382022-02-24 14:07:11 -08001087std::future<bool> Output::chooseCompositionStrategyAsync(
1088 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1089 return mHwComposerAsyncWorker->send(
1090 [&, changes]() { return chooseCompositionStrategy(changes); });
1091}
1092
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001093GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Naira3140382022-02-24 14:07:11 -08001094 ATRACE_CALL();
1095 ALOGV(__FUNCTION__);
1096 auto& state = editState();
1097 const auto& previousChanges = state.previousDeviceRequestedChanges;
1098 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1099 resetCompositionStrategy();
1100 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1101 if (state.previousDeviceRequestedSuccess) {
1102 applyCompositionStrategy(previousChanges);
1103 }
1104 finishPrepareFrame();
1105
1106 base::unique_fd bufferFence;
1107 std::shared_ptr<renderengine::ExternalTexture> buffer;
1108 updateProtectedContentState();
1109 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1110 GpuCompositionResult compositionResult;
1111 if (dequeueSucceeded) {
1112 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001113 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001114 if (optFd) {
1115 compositionResult.fence = std::move(*optFd);
1116 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001117 }
1118
Vishnu Naira3140382022-02-24 14:07:11 -08001119 auto chooseCompositionSuccess = hwcResult.get();
1120 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001121 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1122 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001123 if (!predictionSucceeded) {
1124 ATRACE_NAME("CompositionStrategyPredictionMiss");
1125 resetCompositionStrategy();
1126 if (chooseCompositionSuccess) {
1127 applyCompositionStrategy(changes);
1128 }
1129 finishPrepareFrame();
1130 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1131 compositionResult.buffer = buffer;
1132 } else {
1133 ATRACE_NAME("CompositionStrategyPredictionHit");
1134 }
1135 state.previousDeviceRequestedChanges = std::move(changes);
1136 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1137 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001138}
1139
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001140void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1141 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1142 return;
1143 }
1144
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001145 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001146 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001147 base::unique_fd bufferFence;
1148 std::shared_ptr<renderengine::ExternalTexture> buffer;
1149 updateProtectedContentState();
1150 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001151 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001152 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001153 }
1154 }
1155
1156 postFramebuffer();
1157
1158 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1159
1160 prepareFrame();
1161}
1162
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001163void Output::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001164 ATRACE_CALL();
1165 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001166 const auto& outputState = getState();
1167 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001168 return;
1169 }
1170
Vishnu Naira3140382022-02-24 14:07:11 -08001171 std::optional<base::unique_fd> optReadyFence;
1172 std::shared_ptr<renderengine::ExternalTexture> buffer;
1173 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001174 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001175 optReadyFence = std::move(result.fence);
1176 } else {
1177 if (result.bufferAvailable()) {
1178 buffer = std::move(result.buffer);
1179 bufferFence = std::move(result.fence);
1180 } else {
1181 updateProtectedContentState();
1182 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1183 return;
1184 }
1185 }
1186 // Repaint the framebuffer (if needed), getting the optional fence for when
1187 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001188 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001189 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001190 if (!optReadyFence) {
1191 return;
1192 }
1193
Matt Buckley50c44062022-01-17 20:48:10 +00001194 if (isPowerHintSessionEnabled()) {
1195 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001196 setHintSessionGpuFence(
1197 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001198 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001199 // swap buffers (presentation)
1200 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1201}
1202
Vishnu Naira3140382022-02-24 14:07:11 -08001203void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001204 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001205 auto& renderEngine = getCompositionEngine().getRenderEngine();
1206 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1207
1208 // If we the display is secure, protected content support is enabled, and at
1209 // least one layer has protected content, we need to use a secure back
1210 // buffer.
1211 if (outputState.isSecure && supportsProtectedContent) {
1212 auto layers = getOutputLayersOrderedByZ();
1213 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1214 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1215 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001216 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001217 mRenderSurface->setProtected(needsProtected);
1218 }
1219 }
Vishnu Naira3140382022-02-24 14:07:11 -08001220}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001221
Vishnu Naira3140382022-02-24 14:07:11 -08001222bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1223 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1224 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001225
1226 // If we aren't doing client composition on this output, but do have a
1227 // flipClientTarget request for this frame on this output, we still need to
1228 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001229 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1230 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1231 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001232 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1233 "client composition for this frame",
1234 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001235 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001236 }
1237 }
Vishnu Naira3140382022-02-24 14:07:11 -08001238 return true;
1239}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001240
Vishnu Naira3140382022-02-24 14:07:11 -08001241std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001242 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1243 base::unique_fd& fd) {
Vishnu Naira3140382022-02-24 14:07:11 -08001244 ATRACE_CALL();
1245 ALOGV(__FUNCTION__);
1246
1247 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001248 const TracedOrdinal<bool> hasClientComposition = {
1249 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1250 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001251 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001252 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001253 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001254 }
1255
Vishnu Naira3140382022-02-24 14:07:11 -08001256 if (tex == nullptr) {
1257 ALOGW("Buffer not valid for display [%s], bailing out of "
1258 "client composition for this frame",
1259 mName.c_str());
1260 return {};
1261 }
1262
Lloyd Pique688abd42019-02-15 15:42:24 -08001263 ALOGV("hasClientComposition");
1264
Patrick Williams7584c6a2022-10-29 02:10:58 +00001265 renderengine::DisplaySettings clientCompositionDisplay =
1266 generateClientCompositionDisplaySettings();
Lloyd Pique688abd42019-02-15 15:42:24 -08001267
Lloyd Pique688abd42019-02-15 15:42:24 -08001268 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001269 auto& renderEngine = getCompositionEngine().getRenderEngine();
1270 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001271 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001272 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001273 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001274 clientCompositionDisplay.outputDataspace,
1275 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001276 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1277
Vishnu Naira3140382022-02-24 14:07:11 -08001278 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001279 // Check if the client composition requests were rendered into the provided graphic buffer. If
1280 // so, we can reuse the buffer and avoid client composition.
1281 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001282 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1283 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001284 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001285 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001286 outputCompositionState.reusedClientComposition = true;
1287 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001288 // b/239944175 pass the fence associated with the buffer.
1289 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001290 }
Vishnu Naira3140382022-02-24 14:07:11 -08001291 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001292 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001293 clientCompositionLayers);
1294 }
1295
Lloyd Pique688abd42019-02-15 15:42:24 -08001296 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001297 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1298 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1299 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001300 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001301 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1302 [outputDataspace =
1303 clientCompositionDisplay.outputDataspace](const auto& layer) {
1304 return layer.sourceDataspace != outputDataspace;
1305 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001306 if (expensiveRenderingExpected) {
1307 setExpensiveRenderingExpected(true);
1308 }
1309
Sally Qi59a9f502021-10-12 18:53:23 +00001310 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1311 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001312 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001313 std::back_inserter(clientRenderEngineLayers),
1314 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1315 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001316 });
1317
Alec Mourie4034bb2019-11-19 12:45:54 -08001318 const nsecs_t renderEngineStart = systemTime();
Patrick Williams2e9748f2022-08-09 22:48:18 +00001319 auto fenceResult = renderEngine
1320 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Alec Mourif29700f2023-08-17 21:53:31 +00001321 std::move(fd))
Patrick Williams2e9748f2022-08-09 22:48:18 +00001322 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001323
1324 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001325 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001326 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001327 }
1328
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001329 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1330
Patrick Williams74c0bf62022-11-02 23:59:26 +00001331 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1332 if (fence->isValid()) {
1333 timeStats->recordRenderEngineDuration(renderEngineStart,
1334 std::make_shared<FenceTime>(fence));
1335 } else {
1336 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1337 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001338 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001339
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001340 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1341 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001342 }
1343
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001344 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001345}
1346
Patrick Williams7584c6a2022-10-29 02:10:58 +00001347renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings() const {
1348 const auto& outputState = getState();
1349
1350 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001351 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001352 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1353 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1354 clientCompositionDisplay.orientation =
1355 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1356 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1357 ? outputState.dataspace
1358 : ui::Dataspace::UNKNOWN;
1359
1360 // If we have a valid current display brightness use that, otherwise fall back to the
1361 // display's max desired
1362 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1363 ? outputState.displayBrightnessNits
1364 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1365 clientCompositionDisplay.maxLuminance =
1366 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1367 clientCompositionDisplay.targetLuminanceNits =
1368 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
1369 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1370 clientCompositionDisplay.renderIntent =
1371 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1372 outputState.renderIntent);
1373
1374 // Compute the global color transform matrix.
1375 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1376 for (auto& info : outputState.borderInfoList) {
1377 renderengine::BorderRenderInfo borderInfo;
1378 borderInfo.width = info.width;
1379 borderInfo.color = info.color;
1380 borderInfo.combinedRegion = info.combinedRegion;
1381 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1382 }
1383 clientCompositionDisplay.deviceHandlesColorTransform =
1384 outputState.usesDeviceComposition || getSkipColorTransform();
1385 return clientCompositionDisplay;
1386}
1387
Vishnu Nair9b079a22020-01-21 14:36:08 -08001388std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001389 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001390 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001391 ALOGV("Rendering client layers");
1392
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001393 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001394 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001395 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001396
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001397 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001398 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001399
Lloyd Pique01c77c12019-04-17 12:48:32 -07001400 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001401 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001402 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001403 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001404 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001405
Lloyd Piquea2468662019-03-07 21:31:06 -08001406 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001407 ALOGV("Layer: %s", layerFE.getDebugName());
1408 if (clip.isEmpty()) {
1409 ALOGV(" Skipping for empty clip");
1410 firstLayer = false;
1411 continue;
1412 }
1413
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001414 disableBlurs |= layerFEState->sidebandStream != nullptr;
1415
Vishnu Naira483b4a2019-12-12 15:07:52 -08001416 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001417
1418 // We clear the client target for non-client composed layers if
1419 // requested by the HWC. We skip this if the layer is not an opaque
1420 // rectangle, as by definition the layer must blend with whatever is
1421 // underneath. We also skip the first layer as the buffer target is
1422 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001423 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001424 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001425
1426 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1427
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001428 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1429 // composing the non-shadow content and only draw the shadows.
1430 const bool realContentIsVisible = clientComposition &&
1431 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1432
Lloyd Pique688abd42019-02-15 15:42:24 -08001433 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001434 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1435 if (overrideSettings->bufferId != previousOverrideBufferId) {
1436 previousOverrideBufferId = overrideSettings->bufferId;
1437 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001438 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1439 } else {
1440 ALOGV("Skipping redundant override buffer for [%s] in RE",
1441 layer->getLayerFE().getDebugName());
1442 }
Dan Stoza6166c312021-01-15 16:34:05 -08001443 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001444 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1445 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1446 : (layer->getState().overrideInfo.disableBackgroundBlur
1447 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1448 BlurRegionsOnly
1449 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1450 Enabled);
1451 compositionengine::LayerFE::ClientCompositionTargetSettings
1452 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001453 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001454 outputState.needsFiltering,
1455 .isSecure = outputState.isSecure,
1456 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001457 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001458 .dataspace = outputDataspace,
1459 .realContentIsVisible = realContentIsVisible,
1460 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001461 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001462 .whitePointNits = layerState.whitePointNits,
1463 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001464 if (auto clientCompositionSettings =
1465 layerFE.prepareClientComposition(targetSettings)) {
1466 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1467 if (realContentIsVisible) {
1468 layer->editState().clientCompositionTimestamp = systemTime();
1469 }
Dan Stoza6166c312021-01-15 16:34:05 -08001470 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001471 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001472
Tianhua Sunf91f1402022-05-09 05:45:46 +00001473 if (clientComposition) {
1474 outLayerFEs.push_back(&layerFE);
1475 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001476 }
1477
1478 firstLayer = false;
1479 }
1480
1481 return clientCompositionLayers;
1482}
1483
1484void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001485 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001486 if (flashRegion.isEmpty()) {
1487 return;
1488 }
1489
Vishnu Nair9b079a22020-01-21 14:36:08 -08001490 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001491 layerSettings.source.buffer.buffer = nullptr;
1492 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1493 layerSettings.alpha = half(1.0);
1494
1495 for (const auto& rect : flashRegion) {
1496 layerSettings.geometry.boundaries = rect.toFloatRect();
1497 clientCompositionLayers.push_back(layerSettings);
1498 }
1499}
1500
1501void Output::setExpensiveRenderingExpected(bool) {
1502 // The base class does nothing with this call.
1503}
1504
Matt Buckley50c44062022-01-17 20:48:10 +00001505void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1506 // The base class does nothing with this call.
1507}
1508
1509bool Output::isPowerHintSessionEnabled() {
1510 return false;
1511}
1512
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001513void Output::postFramebuffer() {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001514 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001515 ALOGV(__FUNCTION__);
1516
1517 if (!getState().isEnabled) {
1518 return;
1519 }
1520
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001521 auto& outputState = editState();
1522 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001523
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001524 auto frame = presentAndGetFrameFences();
1525
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001526 mRenderSurface->onPresentDisplayCompleted();
1527
Lloyd Pique01c77c12019-04-17 12:48:32 -07001528 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001529 // The layer buffer from the previous frame (if any) is released
1530 // by HWC only when the release fence from this frame (if any) is
1531 // signaled. Always get the release fence from HWC first.
1532 sp<Fence> releaseFence = Fence::NO_FENCE;
1533
1534 if (auto hwcLayer = layer->getHwcLayer()) {
1535 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1536 releaseFence = f->second;
1537 }
1538 }
1539
1540 // If the layer was client composited in the previous frame, we
1541 // need to merge with the previous client target acquire fence.
1542 // Since we do not track that, always merge with the current
1543 // client target acquire fence when it is available, even though
1544 // this is suboptimal.
1545 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001546 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001547 releaseFence =
1548 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1549 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001550 layer->getLayerFE()
1551 .onLayerDisplayed(ftl::yield<FenceResult>(std::move(releaseFence)).share(),
1552 outputState.layerFilter.layerStack);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001553 }
1554
1555 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001556 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001557 // supply them with the present fence.
1558 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001559 if (const auto layer = weakLayer.promote()) {
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001560 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share(),
1561 outputState.layerFilter.layerStack);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001562 }
1563 }
1564
1565 // Clear out the released layers now that we're done with them.
1566 mReleasedLayers.clear();
1567}
1568
Alec Mouriaa831582021-06-07 16:23:01 -07001569void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III43b5d522023-04-10 15:53:45 -04001570 const auto& outputState = getState();
1571 if (mPlanner && outputState.isEnabled) {
1572 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1573 outputState.usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001574 }
1575}
1576
Lloyd Pique32cbe282018-10-19 13:09:22 -07001577void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001578 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001579 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001580}
1581
Vishnu Naira3140382022-02-24 14:07:11 -08001582void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001583 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001584 auto& outputState = editState();
1585 outputState.usesClientComposition = true;
1586 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001587 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001588}
1589
Lloyd Pique688abd42019-02-15 15:42:24 -08001590bool Output::getSkipColorTransform() const {
1591 return true;
1592}
1593
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001594compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1595 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001596 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001597 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1598 }
1599 return result;
1600}
1601
Vishnu Naira3140382022-02-24 14:07:11 -08001602void Output::setPredictCompositionStrategy(bool predict) {
1603 if (predict) {
1604 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1605 } else {
1606 mHwComposerAsyncWorker.reset(nullptr);
1607 }
1608}
1609
Alec Mouridda07d92022-04-25 22:39:25 +00001610void Output::setTreat170mAsSrgb(bool enable) {
1611 editState().treat170mAsSrgb = enable;
1612}
1613
Vishnu Naira3140382022-02-24 14:07:11 -08001614bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001615 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1616 uint64_t outputLayerHash = getState().outputLayerHash;
1617 editState().lastOutputLayerHash = outputLayerHash;
1618
Vishnu Naira3140382022-02-24 14:07:11 -08001619 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1620 ALOGV("canPredictCompositionStrategy disabled");
1621 return false;
1622 }
1623
1624 if (!getState().previousDeviceRequestedChanges) {
1625 ALOGV("canPredictCompositionStrategy previous changes not available");
1626 return false;
1627 }
1628
1629 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1630 ALOGV("canPredictCompositionStrategy surface does not support");
1631 return false;
1632 }
1633
1634 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1635 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1636 return false;
1637 }
1638
Robert Carrec8ccca2022-05-04 09:36:14 -07001639 if (lastOutputLayerHash != outputLayerHash) {
1640 ALOGV("canPredictCompositionStrategy output layers changed");
1641 return false;
1642 }
1643
Vishnu Naira3140382022-02-24 14:07:11 -08001644 // If no layer uses clientComposition, then don't predict composition strategy
1645 // because we have less work to do in parallel.
1646 if (!anyLayersRequireClientComposition()) {
1647 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1648 return false;
1649 }
1650
Robert Carrec8ccca2022-05-04 09:36:14 -07001651 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001652}
1653
1654bool Output::anyLayersRequireClientComposition() const {
1655 const auto layers = getOutputLayersOrderedByZ();
1656 return std::any_of(layers.begin(), layers.end(),
1657 [](const auto& layer) { return layer->requiresClientComposition(); });
1658}
1659
1660void Output::finishPrepareFrame() {
1661 const auto& state = getState();
1662 if (mPlanner) {
1663 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1664 }
1665 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1666}
1667
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001668bool Output::mustRecompose() const {
1669 return mMustRecompose;
1670}
1671
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001672} // namespace impl
1673} // namespace android::compositionengine