blob: 175dd1d825bfe7bfbc730ac798b79170a9d7b524 [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 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800265 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
266 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800267
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700268 auto& outputState = editState();
269 if (outputState.colorMode == colorProfile.mode &&
270 outputState.dataspace == colorProfile.dataspace &&
271 outputState.renderIntent == colorProfile.renderIntent &&
272 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800273 return;
274 }
275
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700276 outputState.colorMode = colorProfile.mode;
277 outputState.dataspace = colorProfile.dataspace;
278 outputState.renderIntent = colorProfile.renderIntent;
279 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700280
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800281 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700282
Lloyd Pique32cbe282018-10-19 13:09:22 -0700283 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800284 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
285 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800286
287 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700288}
289
John Reckac09e452021-04-07 16:35:37 -0400290void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
291 auto& outputState = editState();
292 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
293 outputState.displayBrightnessNits == displayBrightnessNits) {
294 // Nothing changed
295 return;
296 }
297 outputState.sdrWhitePointNits = sdrWhitePointNits;
298 outputState.displayBrightnessNits = displayBrightnessNits;
299 dirtyEntireOutput();
300}
301
Lloyd Pique32cbe282018-10-19 13:09:22 -0700302void Output::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700303 base::StringAppendF(&out, "Output \"%s\"", mName.c_str());
304 out.append("\n Composition Output State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700305
306 dumpBase(out);
307}
308
309void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 dumpState(out);
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700311 out += '\n';
Lloyd Pique31cb2942018-10-19 17:23:03 -0700312
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700313 if (mDisplayColorProfile) {
314 mDisplayColorProfile->dump(out);
315 } else {
316 out.append(" No display color profile!\n");
317 }
318
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700319 out += '\n';
320
Lloyd Pique31cb2942018-10-19 17:23:03 -0700321 if (mRenderSurface) {
322 mRenderSurface->dump(out);
323 } else {
324 out.append(" No render surface!\n");
325 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800326
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700327 base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
Lloyd Pique01c77c12019-04-17 12:48:32 -0700328 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800329 if (!outputLayer) {
330 continue;
331 }
332 outputLayer->dump(out);
333 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700334}
335
Dan Stoza269dc4d2021-01-15 15:07:43 -0800336void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
337 if (!mPlanner) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700338 out.append("Planner is disabled\n");
Dan Stoza269dc4d2021-01-15 15:07:43 -0800339 return;
340 }
341 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
342 mPlanner->dump(args, out);
343}
344
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700345compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
346 return mDisplayColorProfile.get();
347}
348
349void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
350 mDisplayColorProfile = std::move(mode);
351}
352
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800353const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
354 return mReleasedLayers;
355}
356
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700357void Output::setDisplayColorProfileForTest(
358 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
359 mDisplayColorProfile = std::move(mode);
360}
361
Lloyd Pique31cb2942018-10-19 17:23:03 -0700362compositionengine::RenderSurface* Output::getRenderSurface() const {
363 return mRenderSurface.get();
364}
365
366void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
367 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800368 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000369 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800370 if (mPlanner) {
371 mPlanner->setDisplaySize(size);
372 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700373 dirtyEntireOutput();
374}
375
Vishnu Nair9b079a22020-01-21 14:36:08 -0800376void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
377 if (cacheSize == 0) {
378 mClientCompositionRequestCache.reset();
379 } else {
380 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
381 }
382};
383
Lloyd Pique31cb2942018-10-19 17:23:03 -0700384void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
385 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700386}
387
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700388Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700389 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000390 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700391}
392
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700393bool Output::includesLayer(ui::LayerFilter filter) const {
394 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700395}
396
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700397bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800398 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700399 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800400}
401
Lloyd Piquedf336d92019-03-07 21:38:42 -0800402std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800403 const sp<LayerFE>& layerFE) const {
404 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800405}
406
Lloyd Piquede196652020-01-22 17:29:58 -0800407compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
408 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700409 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800410}
411
Lloyd Pique01c77c12019-04-17 12:48:32 -0700412std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800413 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700414 for (size_t i = 0; i < getOutputLayerCount(); i++) {
415 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800416 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700417 return i;
418 }
419 }
420 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800421}
422
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800423void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
424 mReleasedLayers = std::move(layers);
425}
426
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800427void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
428 LayerFESet& geomSnapshots) {
429 ATRACE_CALL();
430 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800431
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800432 rebuildLayerStacks(refreshArgs, geomSnapshots);
Brian Lindahl439afad2022-11-14 11:16:55 -0700433 uncacheBuffers(refreshArgs.bufferIdsToUncache);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800434}
435
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800436void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400437 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800438 ALOGV(__FUNCTION__);
439
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800440 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800441 updateCompositionState(refreshArgs);
442 planComposition();
443 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800444 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800445 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800446
447 GpuCompositionResult result;
448 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
449 if (predictCompositionStrategy) {
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000450 result = prepareFrameAsync();
Vishnu Naira3140382022-02-24 14:07:11 -0800451 } else {
452 prepareFrame();
453 }
454
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800455 devOptRepaintFlash(refreshArgs);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000456 finishFrame(std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800457 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700458 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800459}
460
Brian Lindahl439afad2022-11-14 11:16:55 -0700461void Output::uncacheBuffers(std::vector<uint64_t> const& bufferIdsToUncache) {
462 if (bufferIdsToUncache.empty()) {
463 return;
464 }
465 for (auto outputLayer : getOutputLayersOrderedByZ()) {
466 outputLayer->uncacheBuffers(bufferIdsToUncache);
467 }
468}
469
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800470void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
471 LayerFESet& layerFESet) {
472 ATRACE_CALL();
473 ALOGV(__FUNCTION__);
474
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700475 auto& outputState = editState();
476
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700478 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800479 return;
480 }
481
482 // Process the layers to determine visibility and coverage
483 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000484 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
485 ? std::make_optional<Region>()
486 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487 collectVisibleLayers(refreshArgs, coverage);
488
489 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700490 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000491 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800492 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
493
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700494 outputState.undefinedRegion = undefinedRegion;
495 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800496}
497
498void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
499 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800500 // Evaluate the layers from front to back to determine what is visible. This
501 // also incrementally calculates the coverage information for each layer as
502 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800503 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700504 // Incrementally process the coverage for each layer
505 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800506
507 // TODO(b/121291683): Stop early if the output is completely covered and
508 // no more layers could even be visible underneath the ones on top.
509 }
510
Lloyd Pique01c77c12019-04-17 12:48:32 -0700511 setReleasedLayers(refreshArgs);
512
513 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800514}
515
Lloyd Piquede196652020-01-22 17:29:58 -0800516void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700517 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800518 // Ensure we have a snapshot of the basic geometry layer state. Limit the
519 // snapshots to once per frame for each candidate layer, as layers may
520 // appear on multiple outputs.
521 if (!coverage.latchedLayers.count(layerFE)) {
522 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800523 }
524
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700525 // Only consider the layers on this output
526 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800527 return;
528 }
529
530 // Obtain a read-only pointer to the front-end layer state
531 const auto* layerFEState = layerFE->getCompositionState();
532 if (CC_UNLIKELY(!layerFEState)) {
533 return;
534 }
535
536 // handle hidden surfaces by setting the visible region to empty
537 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700538 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800539 }
540
Vishnu Naird47bcee2023-02-24 18:08:51 +0000541 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
542 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000543
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800544 /*
545 * opaqueRegion: area of a surface that is fully opaque.
546 */
547 Region opaqueRegion;
548
549 /*
550 * visibleRegion: area of a surface that is visible on screen and not fully
551 * transparent. This is essentially the layer's footprint minus the opaque
552 * regions above it. Areas covered by a translucent surface are considered
553 * visible.
554 */
555 Region visibleRegion;
556
557 /*
558 * coveredRegion: area of a surface that is covered by all visible regions
559 * above it (which includes the translucent areas).
560 */
561 Region coveredRegion;
562
563 /*
564 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500565 * transparent.
566 * This is used to tell when the layer has no visible non-transparent
567 * regions and can be removed from the layer list. It does not affect the
568 * visibleRegion of this layer or any layers beneath it. The hint may not
569 * be correct if apps don't respect the SurfaceView restrictions (which,
570 * sadly, some don't).
571 *
572 * In addition, it is used on DISPLAY_DECORATION layers to specify the
573 * blockingRegion, allowing the DPU to skip it to save power. Once we have
574 * hardware that supports a blockingRegion on frames with AFBC, it may be
575 * useful to use this for other layers, too, so long as we can prevent
576 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800577 */
578 Region transparentRegion;
579
Vishnu Naira483b4a2019-12-12 15:07:52 -0800580 /*
581 * shadowRegion: Region cast by the layer's shadow.
582 */
583 Region shadowRegion;
584
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000585 /**
586 * covered region above excluding internal display overlay layers
587 */
588 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
589
Lloyd Piquede196652020-01-22 17:29:58 -0800590 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800591
592 // Get the visible region
593 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
594 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800595 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800596 visibleRegion.set(visibleRect);
597
Lloyd Piquede196652020-01-22 17:29:58 -0800598 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800599 // if the layer casts a shadow, offset the layers visible region and
600 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800601 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800602 Rect visibleRectWithShadows(visibleRect);
603 visibleRectWithShadows.inset(inset, inset, inset, inset);
604 visibleRegion.set(visibleRectWithShadows);
605 shadowRegion = visibleRegion.subtract(visibleRect);
606 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800607
608 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700609 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800610 }
611
612 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800613 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800614 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000615 // Clip the transparent region to geomLayerBounds first
616 // The transparent region may be influenced by applications, for
617 // instance, by overriding ViewGroup#gatherTransparentRegion with a
618 // custom view. Once the layer stack -> display mapping is known, we
619 // must guard against very wrong inputs to prevent underflow or
620 // overflow errors. We do this here by constraining the transparent
621 // region to be within the pre-transform layer bounds, since the
622 // layer bounds are expected to play nicely with the full
623 // transform.
624 const Region clippedTransparentRegionHint =
625 layerFEState->transparentRegionHint.intersect(
626 Rect(layerFEState->geomLayerBounds));
627
628 if (clippedTransparentRegionHint.isEmpty()) {
629 if (!layerFEState->transparentRegionHint.isEmpty()) {
630 ALOGD("Layer: %s had an out of bounds transparent region",
631 layerFE->getDebugName());
632 layerFEState->transparentRegionHint.dump("transparentRegionHint");
633 }
634 transparentRegion.clear();
635 } else {
636 transparentRegion = tr.transform(clippedTransparentRegionHint);
637 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800638 } else {
639 // transformation too complex, can't do the
640 // transparent region optimization.
641 transparentRegion.clear();
642 }
643 }
644
645 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800646 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800647 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800648 // If we one of the simple category of transforms (0/90/180/270 rotation
649 // + any flip), then the opaque region is the layer's footprint.
650 // Otherwise we don't try and compute the opaque region since there may
651 // be errors at the edges, and we treat the entire layer as
652 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800653 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800654 }
655
656 // Clip the covered region to the visible region
657 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
658
659 // Update accumAboveCoveredLayers for next (lower) layer
660 coverage.aboveCoveredLayers.orSelf(visibleRegion);
661
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000662 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
663 coveredRegionExcludingDisplayOverlays =
664 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
665 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
666 }
667
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800668 // subtract the opaque region covered by the layers above us
669 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
670
671 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700672 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800673 }
674
675 // Get coverage information for the layer as previously displayed,
676 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800677 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700678 auto prevOutputLayer =
679 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800680
681 // Get coverage information for the layer as previously displayed
682 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
683 const Region kEmptyRegion;
684 const Region& oldVisibleRegion =
685 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
686 const Region& oldCoveredRegion =
687 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
688
689 // compute this layer's dirty region
690 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800691 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800692 // we need to invalidate the whole region
693 dirty = visibleRegion;
694 // as well, as the old visible region
695 dirty.orSelf(oldVisibleRegion);
696 } else {
697 /* compute the exposed region:
698 * the exposed region consists of two components:
699 * 1) what's VISIBLE now and was COVERED before
700 * 2) what's EXPOSED now less what was EXPOSED before
701 *
702 * note that (1) is conservative, we start with the whole visible region
703 * but only keep what used to be covered by something -- which mean it
704 * may have been exposed.
705 *
706 * (2) handles areas that were not covered by anything but got exposed
707 * because of a resize.
708 *
709 */
710 const Region newExposed = visibleRegion - coveredRegion;
711 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
712 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
713 }
714 dirty.subtractSelf(coverage.aboveOpaqueLayers);
715
716 // accumulate to the screen dirty region
717 coverage.dirtyRegion.orSelf(dirty);
718
719 // Update accumAboveOpaqueLayers for next (lower) layer
720 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
721
722 // Compute the visible non-transparent region
723 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
724
Vishnu Naira483b4a2019-12-12 15:07:52 -0800725 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800726 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700727 const auto& outputState = getState();
728 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000729 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800730 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700731 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800732 }
733
Vishnu Naira483b4a2019-12-12 15:07:52 -0800734 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
735
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800736 // The layer is visible. Either reuse the existing outputLayer if we have
737 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800738 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800739
740 // Store the layer coverage information into the layer state as some of it
741 // is useful later.
742 auto& outputLayerState = result->editState();
743 outputLayerState.visibleRegion = visibleRegion;
744 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
745 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200746 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000747 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800748 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500749 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400750 layerFEState->compositionType == Composition::DISPLAY_DECORATION
751 ? outputState.transform.transform(
752 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
753 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000754 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
755 outputLayerState.coveredRegionExcludingDisplayOverlays =
756 std::move(coveredRegionExcludingDisplayOverlays);
757 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800758}
759
760void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
761 // The base class does nothing with this call.
762}
763
Dan Stoza269dc4d2021-01-15 15:07:43 -0800764void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800765 ATRACE_CALL();
766 ALOGV(__FUNCTION__);
767
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800768 if (!getState().isEnabled) {
769 return;
770 }
771
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800772 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
773 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
774
Lloyd Pique01c77c12019-04-17 12:48:32 -0700775 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700776 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800777 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200778 forceClientComposition,
779 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800780
781 if (mLayerRequestingBackgroundBlur == layer) {
782 forceClientComposition = false;
783 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800784 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000785
786 updateCompositionStateForBorder(refreshArgs);
787}
788
789void Output::updateCompositionStateForBorder(
790 const compositionengine::CompositionRefreshArgs& refreshArgs) {
791 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
792 // Store a map of layerId to their computed visible region.
793 for (auto* layer : getOutputLayersOrderedByZ()) {
794 int layerId = (layer->getLayerFE()).getSequence();
795 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
796 }
797 OutputCompositionState& outputCompositionState = editState();
798 outputCompositionState.borderInfoList.clear();
799 bool clientComposeTopLayer = false;
800 for (const auto& borderInfo : refreshArgs.borderInfoList) {
801 renderengine::BorderRenderInfo info;
802 for (const auto& id : borderInfo.layerIds) {
803 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
804 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000805
806 if (!info.combinedRegion.isEmpty()) {
807 info.width = borderInfo.width;
808 info.color = borderInfo.color;
809 outputCompositionState.borderInfoList.emplace_back(std::move(info));
810 clientComposeTopLayer = true;
811 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000812 }
813
814 // In this situation we must client compose the top layer instead of using hwc
815 // because we want to draw the border above all else.
816 // This could potentially cause a bit of a performance regression if the top
817 // layer would have been rendered using hwc originally.
818 // TODO(b/227656283): Measure system's performance before enabling the border feature
819 if (clientComposeTopLayer) {
820 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
821 (topLayer->editState()).forceClientComposition = true;
822 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800823}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800824
Dan Stoza269dc4d2021-01-15 15:07:43 -0800825void Output::planComposition() {
826 if (!mPlanner || !getState().isEnabled) {
827 return;
828 }
829
830 ATRACE_CALL();
831 ALOGV(__FUNCTION__);
832
833 mPlanner->plan(getOutputLayersOrderedByZ());
834}
835
836void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
837 ATRACE_CALL();
838 ALOGV(__FUNCTION__);
839
840 if (!getState().isEnabled) {
841 return;
842 }
843
Ady Abraham3645e642021-04-20 18:39:00 -0700844 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700845 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800846 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700847
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400848 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800849 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400850 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400851 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400852 bool overrideZ = false;
Robert Carrec8ccca2022-05-04 09:36:14 -0700853 uint64_t outputLayerHash = 0;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800854 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400855 if (layer == peekThroughLayer) {
856 // No longer needed, although it should not show up again, so
857 // resetting it is not truly needed either.
858 peekThroughLayer = nullptr;
859
860 // peekThroughLayer was already drawn ahead of its z order.
861 continue;
862 }
Dan Stoza6166c312021-01-15 16:34:05 -0800863 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400864 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400865 if (overrideInfo.buffer != nullptr) {
866 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800867 ALOGV("Skipping redundant buffer");
868 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400869 } else {
870 // First layer with the override buffer.
871 if (overrideInfo.peekThroughLayer) {
872 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400873
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400874 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400875 overrideZ = true;
876 includeGeometry = true;
877 constexpr bool isPeekingThrough = true;
878 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
879 isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700880 outputLayerHash ^= android::hashCombine(
881 reinterpret_cast<uint64_t>(&peekThroughLayer->getLayerFE()),
882 z, includeGeometry, overrideZ, isPeekingThrough,
883 peekThroughLayer->requiresClientComposition());
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400884 }
885
886 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800887 }
Dan Stoza6166c312021-01-15 16:34:05 -0800888 }
889
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400890 constexpr bool isPeekingThrough = false;
891 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Robert Carrec8ccca2022-05-04 09:36:14 -0700892 if (!skipLayer) {
893 outputLayerHash ^= android::hashCombine(
894 reinterpret_cast<uint64_t>(&layer->getLayerFE()),
895 z, includeGeometry, overrideZ, isPeekingThrough,
896 layer->requiresClientComposition());
897 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800898 }
Robert Carrec8ccca2022-05-04 09:36:14 -0700899 editState().outputLayerHash = outputLayerHash;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800900}
901
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800902compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
903 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
904 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100905 auto* compState = layer->getLayerFE().getCompositionState();
906
907 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
908 // want to force client composition because of the blur.
909 if (compState->sidebandStream != nullptr) {
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
971 // data space to be BT2020_PQ and convert HLG to PQ.
972 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,
988 ui::RenderIntent::COLORIMETRIC,
989 refreshArgs.colorSpaceAgnosticDataspace};
990 }
991
992 ui::Dataspace hdrDataSpace;
993 bool isHdrClientComposition = false;
994 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
995
996 switch (refreshArgs.forceOutputColorMode) {
997 case ui::ColorMode::SRGB:
998 bestDataSpace = ui::Dataspace::V0_SRGB;
999 break;
1000 case ui::ColorMode::DISPLAY_P3:
1001 bestDataSpace = ui::Dataspace::DISPLAY_P3;
1002 break;
1003 default:
1004 break;
1005 }
1006
1007 // respect hdrDataSpace only when there is no legacy HDR support
1008 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1009 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1010 if (isHdr) {
1011 bestDataSpace = hdrDataSpace;
1012 }
1013
1014 ui::RenderIntent intent;
1015 switch (refreshArgs.outputColorSetting) {
1016 case OutputColorSetting::kManaged:
1017 case OutputColorSetting::kUnmanaged:
1018 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1019 : ui::RenderIntent::COLORIMETRIC;
1020 break;
1021 case OutputColorSetting::kEnhanced:
1022 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1023 break;
1024 default: // vendor display color setting
1025 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1026 break;
1027 }
1028
1029 ui::ColorMode outMode;
1030 ui::Dataspace outDataSpace;
1031 ui::RenderIntent outRenderIntent;
1032 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1033 &outRenderIntent);
1034
1035 return ColorProfile{outMode, outDataSpace, outRenderIntent,
1036 refreshArgs.colorSpaceAgnosticDataspace};
1037}
1038
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001039void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001040 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001041 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001042 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001043 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001044
1045 // If nothing has changed (!dirty), don't recompose.
1046 // If something changed, but we don't currently have any visible layers,
1047 // and didn't when we last did a composition, then skip it this time.
1048 // The second rule does two things:
1049 // - When all layers are removed from a display, we'll emit one black
1050 // frame, then nothing more until we get new layers.
1051 // - When a display is created with a private layer stack, we won't
1052 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001053 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001054
1055 const char flagPrefix[] = {'-', '+'};
1056 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001057 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1058 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1059 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001060
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001061 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001062
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001063 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001064 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001065 }
1066}
1067
Lloyd Pique66d68602019-02-13 14:23:31 -08001068void Output::prepareFrame() {
1069 ATRACE_CALL();
1070 ALOGV(__FUNCTION__);
1071
Vishnu Naira3140382022-02-24 14:07:11 -08001072 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001073 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001074 return;
1075 }
1076
Vishnu Naira3140382022-02-24 14:07:11 -08001077 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1078 bool success = chooseCompositionStrategy(&changes);
1079 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001080 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001081 outputState.previousDeviceRequestedChanges = changes;
1082 outputState.previousDeviceRequestedSuccess = success;
1083 if (success) {
1084 applyCompositionStrategy(changes);
1085 }
1086 finishPrepareFrame();
1087}
Lloyd Pique66d68602019-02-13 14:23:31 -08001088
Vishnu Naira3140382022-02-24 14:07:11 -08001089std::future<bool> Output::chooseCompositionStrategyAsync(
1090 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1091 return mHwComposerAsyncWorker->send(
1092 [&, changes]() { return chooseCompositionStrategy(changes); });
1093}
1094
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001095GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Naira3140382022-02-24 14:07:11 -08001096 ATRACE_CALL();
1097 ALOGV(__FUNCTION__);
1098 auto& state = editState();
1099 const auto& previousChanges = state.previousDeviceRequestedChanges;
1100 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1101 resetCompositionStrategy();
1102 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1103 if (state.previousDeviceRequestedSuccess) {
1104 applyCompositionStrategy(previousChanges);
1105 }
1106 finishPrepareFrame();
1107
1108 base::unique_fd bufferFence;
1109 std::shared_ptr<renderengine::ExternalTexture> buffer;
1110 updateProtectedContentState();
1111 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1112 GpuCompositionResult compositionResult;
1113 if (dequeueSucceeded) {
1114 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001115 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001116 if (optFd) {
1117 compositionResult.fence = std::move(*optFd);
1118 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001119 }
1120
Vishnu Naira3140382022-02-24 14:07:11 -08001121 auto chooseCompositionSuccess = hwcResult.get();
1122 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001123 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1124 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001125 if (!predictionSucceeded) {
1126 ATRACE_NAME("CompositionStrategyPredictionMiss");
1127 resetCompositionStrategy();
1128 if (chooseCompositionSuccess) {
1129 applyCompositionStrategy(changes);
1130 }
1131 finishPrepareFrame();
1132 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1133 compositionResult.buffer = buffer;
1134 } else {
1135 ATRACE_NAME("CompositionStrategyPredictionHit");
1136 }
1137 state.previousDeviceRequestedChanges = std::move(changes);
1138 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1139 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001140}
1141
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001142void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1143 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1144 return;
1145 }
1146
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001147 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001148 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001149 base::unique_fd bufferFence;
1150 std::shared_ptr<renderengine::ExternalTexture> buffer;
1151 updateProtectedContentState();
1152 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001153 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001154 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001155 }
1156 }
1157
1158 postFramebuffer();
1159
1160 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1161
1162 prepareFrame();
1163}
1164
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001165void Output::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001166 ATRACE_CALL();
1167 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001168 const auto& outputState = getState();
1169 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001170 return;
1171 }
1172
Vishnu Naira3140382022-02-24 14:07:11 -08001173 std::optional<base::unique_fd> optReadyFence;
1174 std::shared_ptr<renderengine::ExternalTexture> buffer;
1175 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001176 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001177 optReadyFence = std::move(result.fence);
1178 } else {
1179 if (result.bufferAvailable()) {
1180 buffer = std::move(result.buffer);
1181 bufferFence = std::move(result.fence);
1182 } else {
1183 updateProtectedContentState();
1184 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1185 return;
1186 }
1187 }
1188 // Repaint the framebuffer (if needed), getting the optional fence for when
1189 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001190 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001191 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001192 if (!optReadyFence) {
1193 return;
1194 }
1195
Matt Buckley50c44062022-01-17 20:48:10 +00001196 if (isPowerHintSessionEnabled()) {
1197 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001198 setHintSessionGpuFence(
1199 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001200 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001201 // swap buffers (presentation)
1202 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1203}
1204
Vishnu Naira3140382022-02-24 14:07:11 -08001205void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001206 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001207 auto& renderEngine = getCompositionEngine().getRenderEngine();
1208 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1209
1210 // If we the display is secure, protected content support is enabled, and at
1211 // least one layer has protected content, we need to use a secure back
1212 // buffer.
1213 if (outputState.isSecure && supportsProtectedContent) {
1214 auto layers = getOutputLayersOrderedByZ();
1215 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1216 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1217 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001218 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001219 mRenderSurface->setProtected(needsProtected);
1220 }
1221 }
Vishnu Naira3140382022-02-24 14:07:11 -08001222}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001223
Vishnu Naira3140382022-02-24 14:07:11 -08001224bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1225 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1226 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001227
1228 // If we aren't doing client composition on this output, but do have a
1229 // flipClientTarget request for this frame on this output, we still need to
1230 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001231 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1232 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1233 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001234 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1235 "client composition for this frame",
1236 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001237 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001238 }
1239 }
Vishnu Naira3140382022-02-24 14:07:11 -08001240 return true;
1241}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001242
Vishnu Naira3140382022-02-24 14:07:11 -08001243std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001244 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1245 base::unique_fd& fd) {
Vishnu Naira3140382022-02-24 14:07:11 -08001246 ATRACE_CALL();
1247 ALOGV(__FUNCTION__);
1248
1249 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001250 const TracedOrdinal<bool> hasClientComposition = {
1251 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1252 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001253 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001254 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001255 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001256 }
1257
Vishnu Naira3140382022-02-24 14:07:11 -08001258 if (tex == nullptr) {
1259 ALOGW("Buffer not valid for display [%s], bailing out of "
1260 "client composition for this frame",
1261 mName.c_str());
1262 return {};
1263 }
1264
Lloyd Pique688abd42019-02-15 15:42:24 -08001265 ALOGV("hasClientComposition");
1266
Patrick Williams7584c6a2022-10-29 02:10:58 +00001267 renderengine::DisplaySettings clientCompositionDisplay =
1268 generateClientCompositionDisplaySettings();
Lloyd Pique688abd42019-02-15 15:42:24 -08001269
Lloyd Pique688abd42019-02-15 15:42:24 -08001270 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001271 auto& renderEngine = getCompositionEngine().getRenderEngine();
1272 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001273 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001274 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001275 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001276 clientCompositionDisplay.outputDataspace,
1277 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001278 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1279
Vishnu Naira3140382022-02-24 14:07:11 -08001280 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001281 // Check if the client composition requests were rendered into the provided graphic buffer. If
1282 // so, we can reuse the buffer and avoid client composition.
1283 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001284 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1285 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001286 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001287 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001288 outputCompositionState.reusedClientComposition = true;
1289 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001290 // b/239944175 pass the fence associated with the buffer.
1291 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001292 }
Vishnu Naira3140382022-02-24 14:07:11 -08001293 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001294 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001295 clientCompositionLayers);
1296 }
1297
Lloyd Pique688abd42019-02-15 15:42:24 -08001298 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001299 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1300 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1301 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001302 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001303 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1304 [outputDataspace =
1305 clientCompositionDisplay.outputDataspace](const auto& layer) {
1306 return layer.sourceDataspace != outputDataspace;
1307 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001308 if (expensiveRenderingExpected) {
1309 setExpensiveRenderingExpected(true);
1310 }
1311
Sally Qi59a9f502021-10-12 18:53:23 +00001312 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1313 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001314 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001315 std::back_inserter(clientRenderEngineLayers),
1316 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1317 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001318 });
1319
Alec Mourie4034bb2019-11-19 12:45:54 -08001320 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001321 // Only use the framebuffer cache when rendering to an internal display
1322 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1323 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1324 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1325 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1326 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001327 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001328
Patrick Williams2e9748f2022-08-09 22:48:18 +00001329 auto fenceResult = renderEngine
1330 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
1331 useFramebufferCache, std::move(fd))
1332 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001333
1334 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001335 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001336 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001337 }
1338
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001339 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1340
Patrick Williams74c0bf62022-11-02 23:59:26 +00001341 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1342 if (fence->isValid()) {
1343 timeStats->recordRenderEngineDuration(renderEngineStart,
1344 std::make_shared<FenceTime>(fence));
1345 } else {
1346 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1347 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001348 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001349
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001350 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1351 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001352 }
1353
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001354 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001355}
1356
Patrick Williams7584c6a2022-10-29 02:10:58 +00001357renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings() const {
1358 const auto& outputState = getState();
1359
1360 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001361 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001362 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1363 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1364 clientCompositionDisplay.orientation =
1365 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1366 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1367 ? outputState.dataspace
1368 : ui::Dataspace::UNKNOWN;
1369
1370 // If we have a valid current display brightness use that, otherwise fall back to the
1371 // display's max desired
1372 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1373 ? outputState.displayBrightnessNits
1374 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1375 clientCompositionDisplay.maxLuminance =
1376 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1377 clientCompositionDisplay.targetLuminanceNits =
1378 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
1379 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1380 clientCompositionDisplay.renderIntent =
1381 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1382 outputState.renderIntent);
1383
1384 // Compute the global color transform matrix.
1385 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1386 for (auto& info : outputState.borderInfoList) {
1387 renderengine::BorderRenderInfo borderInfo;
1388 borderInfo.width = info.width;
1389 borderInfo.color = info.color;
1390 borderInfo.combinedRegion = info.combinedRegion;
1391 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1392 }
1393 clientCompositionDisplay.deviceHandlesColorTransform =
1394 outputState.usesDeviceComposition || getSkipColorTransform();
1395 return clientCompositionDisplay;
1396}
1397
Vishnu Nair9b079a22020-01-21 14:36:08 -08001398std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001399 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001400 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001401 ALOGV("Rendering client layers");
1402
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001403 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001404 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001405 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001406
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001407 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001408 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001409
Lloyd Pique01c77c12019-04-17 12:48:32 -07001410 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001411 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001412 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001413 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001414 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001415
Lloyd Piquea2468662019-03-07 21:31:06 -08001416 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001417 ALOGV("Layer: %s", layerFE.getDebugName());
1418 if (clip.isEmpty()) {
1419 ALOGV(" Skipping for empty clip");
1420 firstLayer = false;
1421 continue;
1422 }
1423
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001424 disableBlurs |= layerFEState->sidebandStream != nullptr;
1425
Vishnu Naira483b4a2019-12-12 15:07:52 -08001426 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001427
1428 // We clear the client target for non-client composed layers if
1429 // requested by the HWC. We skip this if the layer is not an opaque
1430 // rectangle, as by definition the layer must blend with whatever is
1431 // underneath. We also skip the first layer as the buffer target is
1432 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001433 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001434 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001435
1436 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1437
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001438 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1439 // composing the non-shadow content and only draw the shadows.
1440 const bool realContentIsVisible = clientComposition &&
1441 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1442
Lloyd Pique688abd42019-02-15 15:42:24 -08001443 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001444 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1445 if (overrideSettings->bufferId != previousOverrideBufferId) {
1446 previousOverrideBufferId = overrideSettings->bufferId;
1447 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001448 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1449 } else {
1450 ALOGV("Skipping redundant override buffer for [%s] in RE",
1451 layer->getLayerFE().getDebugName());
1452 }
Dan Stoza6166c312021-01-15 16:34:05 -08001453 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001454 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1455 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1456 : (layer->getState().overrideInfo.disableBackgroundBlur
1457 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1458 BlurRegionsOnly
1459 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1460 Enabled);
1461 compositionengine::LayerFE::ClientCompositionTargetSettings
1462 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001463 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001464 outputState.needsFiltering,
1465 .isSecure = outputState.isSecure,
1466 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001467 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001468 .dataspace = outputDataspace,
1469 .realContentIsVisible = realContentIsVisible,
1470 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001471 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001472 .whitePointNits = layerState.whitePointNits,
1473 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001474 if (auto clientCompositionSettings =
1475 layerFE.prepareClientComposition(targetSettings)) {
1476 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1477 if (realContentIsVisible) {
1478 layer->editState().clientCompositionTimestamp = systemTime();
1479 }
Dan Stoza6166c312021-01-15 16:34:05 -08001480 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001481 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001482
Tianhua Sunf91f1402022-05-09 05:45:46 +00001483 if (clientComposition) {
1484 outLayerFEs.push_back(&layerFE);
1485 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001486 }
1487
1488 firstLayer = false;
1489 }
1490
1491 return clientCompositionLayers;
1492}
1493
1494void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001495 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001496 if (flashRegion.isEmpty()) {
1497 return;
1498 }
1499
Vishnu Nair9b079a22020-01-21 14:36:08 -08001500 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001501 layerSettings.source.buffer.buffer = nullptr;
1502 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1503 layerSettings.alpha = half(1.0);
1504
1505 for (const auto& rect : flashRegion) {
1506 layerSettings.geometry.boundaries = rect.toFloatRect();
1507 clientCompositionLayers.push_back(layerSettings);
1508 }
1509}
1510
1511void Output::setExpensiveRenderingExpected(bool) {
1512 // The base class does nothing with this call.
1513}
1514
Matt Buckley50c44062022-01-17 20:48:10 +00001515void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1516 // The base class does nothing with this call.
1517}
1518
1519bool Output::isPowerHintSessionEnabled() {
1520 return false;
1521}
1522
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001523void Output::postFramebuffer() {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001524 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001525 ALOGV(__FUNCTION__);
1526
1527 if (!getState().isEnabled) {
1528 return;
1529 }
1530
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001531 auto& outputState = editState();
1532 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001533
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001534 auto frame = presentAndGetFrameFences();
1535
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001536 mRenderSurface->onPresentDisplayCompleted();
1537
Lloyd Pique01c77c12019-04-17 12:48:32 -07001538 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001539 // The layer buffer from the previous frame (if any) is released
1540 // by HWC only when the release fence from this frame (if any) is
1541 // signaled. Always get the release fence from HWC first.
1542 sp<Fence> releaseFence = Fence::NO_FENCE;
1543
1544 if (auto hwcLayer = layer->getHwcLayer()) {
1545 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1546 releaseFence = f->second;
1547 }
1548 }
1549
1550 // If the layer was client composited in the previous frame, we
1551 // need to merge with the previous client target acquire fence.
1552 // Since we do not track that, always merge with the current
1553 // client target acquire fence when it is available, even though
1554 // this is suboptimal.
1555 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001556 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001557 releaseFence =
1558 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1559 }
Sally Qi59a9f502021-10-12 18:53:23 +00001560 layer->getLayerFE().onLayerDisplayed(
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001561 ftl::yield<FenceResult>(std::move(releaseFence)).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001562 }
1563
1564 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001565 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001566 // supply them with the present fence.
1567 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001568 if (const auto layer = weakLayer.promote()) {
1569 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001570 }
1571 }
1572
1573 // Clear out the released layers now that we're done with them.
1574 mReleasedLayers.clear();
1575}
1576
Alec Mouriaa831582021-06-07 16:23:01 -07001577void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001578 if (mPlanner) {
Brian Johnson869e28f2022-08-12 22:20:19 +00001579 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime,
1580 getState().usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001581 }
1582}
1583
Lloyd Pique32cbe282018-10-19 13:09:22 -07001584void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001585 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001586 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001587}
1588
Vishnu Naira3140382022-02-24 14:07:11 -08001589void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001590 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001591 auto& outputState = editState();
1592 outputState.usesClientComposition = true;
1593 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001594 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001595}
1596
Lloyd Pique688abd42019-02-15 15:42:24 -08001597bool Output::getSkipColorTransform() const {
1598 return true;
1599}
1600
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001601compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1602 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001603 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001604 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1605 }
1606 return result;
1607}
1608
Vishnu Naira3140382022-02-24 14:07:11 -08001609void Output::setPredictCompositionStrategy(bool predict) {
1610 if (predict) {
1611 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1612 } else {
1613 mHwComposerAsyncWorker.reset(nullptr);
1614 }
1615}
1616
Alec Mouridda07d92022-04-25 22:39:25 +00001617void Output::setTreat170mAsSrgb(bool enable) {
1618 editState().treat170mAsSrgb = enable;
1619}
1620
Vishnu Naira3140382022-02-24 14:07:11 -08001621bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001622 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1623 uint64_t outputLayerHash = getState().outputLayerHash;
1624 editState().lastOutputLayerHash = outputLayerHash;
1625
Vishnu Naira3140382022-02-24 14:07:11 -08001626 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1627 ALOGV("canPredictCompositionStrategy disabled");
1628 return false;
1629 }
1630
1631 if (!getState().previousDeviceRequestedChanges) {
1632 ALOGV("canPredictCompositionStrategy previous changes not available");
1633 return false;
1634 }
1635
1636 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1637 ALOGV("canPredictCompositionStrategy surface does not support");
1638 return false;
1639 }
1640
1641 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1642 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1643 return false;
1644 }
1645
Robert Carrec8ccca2022-05-04 09:36:14 -07001646 if (lastOutputLayerHash != outputLayerHash) {
1647 ALOGV("canPredictCompositionStrategy output layers changed");
1648 return false;
1649 }
1650
Vishnu Naira3140382022-02-24 14:07:11 -08001651 // If no layer uses clientComposition, then don't predict composition strategy
1652 // because we have less work to do in parallel.
1653 if (!anyLayersRequireClientComposition()) {
1654 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1655 return false;
1656 }
1657
Robert Carrec8ccca2022-05-04 09:36:14 -07001658 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001659}
1660
1661bool Output::anyLayersRequireClientComposition() const {
1662 const auto layers = getOutputLayersOrderedByZ();
1663 return std::any_of(layers.begin(), layers.end(),
1664 [](const auto& layer) { return layer->requiresClientComposition(); });
1665}
1666
1667void Output::finishPrepareFrame() {
1668 const auto& state = getState();
1669 if (mPlanner) {
1670 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1671 }
1672 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1673}
1674
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001675bool Output::mustRecompose() const {
1676 return mMustRecompose;
1677}
1678
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001679} // namespace impl
1680} // namespace android::compositionengine