blob: e42e27dfbba848cb2bb58e5184dc917bd770f5cd [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) {
466 ATRACE_CALL();
467 ALOGV(__FUNCTION__);
468
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700469 auto& outputState = editState();
470
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800471 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700472 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 return;
474 }
475
476 // Process the layers to determine visibility and coverage
477 compositionengine::Output::CoverageState coverage{layerFESet};
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000478 coverage.aboveCoveredLayersExcludingOverlays = refreshArgs.hasTrustedPresentationListener
479 ? std::make_optional<Region>()
480 : std::nullopt;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800481 collectVisibleLayers(refreshArgs, coverage);
482
483 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700484 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000485 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800486 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
487
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700488 outputState.undefinedRegion = undefinedRegion;
489 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800490}
491
492void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
493 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800494 // Evaluate the layers from front to back to determine what is visible. This
495 // also incrementally calculates the coverage information for each layer as
496 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800497 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700498 // Incrementally process the coverage for each layer
499 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800500
501 // TODO(b/121291683): Stop early if the output is completely covered and
502 // no more layers could even be visible underneath the ones on top.
503 }
504
Lloyd Pique01c77c12019-04-17 12:48:32 -0700505 setReleasedLayers(refreshArgs);
506
507 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800508}
509
Lloyd Piquede196652020-01-22 17:29:58 -0800510void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700511 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800512 // Ensure we have a snapshot of the basic geometry layer state. Limit the
513 // snapshots to once per frame for each candidate layer, as layers may
514 // appear on multiple outputs.
515 if (!coverage.latchedLayers.count(layerFE)) {
516 coverage.latchedLayers.insert(layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800517 }
518
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700519 // Only consider the layers on this output
520 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800521 return;
522 }
523
524 // Obtain a read-only pointer to the front-end layer state
525 const auto* layerFEState = layerFE->getCompositionState();
526 if (CC_UNLIKELY(!layerFEState)) {
527 return;
528 }
529
530 // handle hidden surfaces by setting the visible region to empty
531 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700532 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800533 }
534
Vishnu Naird47bcee2023-02-24 18:08:51 +0000535 bool computeAboveCoveredExcludingOverlays = coverage.aboveCoveredLayersExcludingOverlays &&
536 !layerFEState->outputFilter.toInternalDisplay;
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000537
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800538 /*
539 * opaqueRegion: area of a surface that is fully opaque.
540 */
541 Region opaqueRegion;
542
543 /*
544 * visibleRegion: area of a surface that is visible on screen and not fully
545 * transparent. This is essentially the layer's footprint minus the opaque
546 * regions above it. Areas covered by a translucent surface are considered
547 * visible.
548 */
549 Region visibleRegion;
550
551 /*
552 * coveredRegion: area of a surface that is covered by all visible regions
553 * above it (which includes the translucent areas).
554 */
555 Region coveredRegion;
556
557 /*
558 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500559 * transparent.
560 * This is used to tell when the layer has no visible non-transparent
561 * regions and can be removed from the layer list. It does not affect the
562 * visibleRegion of this layer or any layers beneath it. The hint may not
563 * be correct if apps don't respect the SurfaceView restrictions (which,
564 * sadly, some don't).
565 *
566 * In addition, it is used on DISPLAY_DECORATION layers to specify the
567 * blockingRegion, allowing the DPU to skip it to save power. Once we have
568 * hardware that supports a blockingRegion on frames with AFBC, it may be
569 * useful to use this for other layers, too, so long as we can prevent
570 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800571 */
572 Region transparentRegion;
573
Vishnu Naira483b4a2019-12-12 15:07:52 -0800574 /*
575 * shadowRegion: Region cast by the layer's shadow.
576 */
577 Region shadowRegion;
578
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000579 /**
580 * covered region above excluding internal display overlay layers
581 */
582 std::optional<Region> coveredRegionExcludingDisplayOverlays = std::nullopt;
583
Lloyd Piquede196652020-01-22 17:29:58 -0800584 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800585
586 // Get the visible region
587 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
588 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800589 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800590 visibleRegion.set(visibleRect);
591
Lloyd Piquede196652020-01-22 17:29:58 -0800592 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800593 // if the layer casts a shadow, offset the layers visible region and
594 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800595 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800596 Rect visibleRectWithShadows(visibleRect);
597 visibleRectWithShadows.inset(inset, inset, inset, inset);
598 visibleRegion.set(visibleRectWithShadows);
599 shadowRegion = visibleRegion.subtract(visibleRect);
600 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800601
602 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700603 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800604 }
605
606 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800607 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800608 if (tr.preserveRects()) {
Alec Mourie60f0b92022-06-10 19:15:20 +0000609 // Clip the transparent region to geomLayerBounds first
610 // The transparent region may be influenced by applications, for
611 // instance, by overriding ViewGroup#gatherTransparentRegion with a
612 // custom view. Once the layer stack -> display mapping is known, we
613 // must guard against very wrong inputs to prevent underflow or
614 // overflow errors. We do this here by constraining the transparent
615 // region to be within the pre-transform layer bounds, since the
616 // layer bounds are expected to play nicely with the full
617 // transform.
618 const Region clippedTransparentRegionHint =
619 layerFEState->transparentRegionHint.intersect(
620 Rect(layerFEState->geomLayerBounds));
621
622 if (clippedTransparentRegionHint.isEmpty()) {
623 if (!layerFEState->transparentRegionHint.isEmpty()) {
624 ALOGD("Layer: %s had an out of bounds transparent region",
625 layerFE->getDebugName());
626 layerFEState->transparentRegionHint.dump("transparentRegionHint");
627 }
628 transparentRegion.clear();
629 } else {
630 transparentRegion = tr.transform(clippedTransparentRegionHint);
631 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800632 } else {
633 // transformation too complex, can't do the
634 // transparent region optimization.
635 transparentRegion.clear();
636 }
637 }
638
639 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800640 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800641 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800642 // If we one of the simple category of transforms (0/90/180/270 rotation
643 // + any flip), then the opaque region is the layer's footprint.
644 // Otherwise we don't try and compute the opaque region since there may
645 // be errors at the edges, and we treat the entire layer as
646 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800647 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800648 }
649
650 // Clip the covered region to the visible region
651 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
652
653 // Update accumAboveCoveredLayers for next (lower) layer
654 coverage.aboveCoveredLayers.orSelf(visibleRegion);
655
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000656 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
657 coveredRegionExcludingDisplayOverlays =
658 coverage.aboveCoveredLayersExcludingOverlays->intersect(visibleRegion);
659 coverage.aboveCoveredLayersExcludingOverlays->orSelf(visibleRegion);
660 }
661
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800662 // subtract the opaque region covered by the layers above us
663 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
664
665 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700666 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800667 }
668
669 // Get coverage information for the layer as previously displayed,
670 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800671 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700672 auto prevOutputLayer =
673 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800674
675 // Get coverage information for the layer as previously displayed
676 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
677 const Region kEmptyRegion;
678 const Region& oldVisibleRegion =
679 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
680 const Region& oldCoveredRegion =
681 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
682
683 // compute this layer's dirty region
684 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800685 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800686 // we need to invalidate the whole region
687 dirty = visibleRegion;
688 // as well, as the old visible region
689 dirty.orSelf(oldVisibleRegion);
690 } else {
691 /* compute the exposed region:
692 * the exposed region consists of two components:
693 * 1) what's VISIBLE now and was COVERED before
694 * 2) what's EXPOSED now less what was EXPOSED before
695 *
696 * note that (1) is conservative, we start with the whole visible region
697 * but only keep what used to be covered by something -- which mean it
698 * may have been exposed.
699 *
700 * (2) handles areas that were not covered by anything but got exposed
701 * because of a resize.
702 *
703 */
704 const Region newExposed = visibleRegion - coveredRegion;
705 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
706 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
707 }
708 dirty.subtractSelf(coverage.aboveOpaqueLayers);
709
710 // accumulate to the screen dirty region
711 coverage.dirtyRegion.orSelf(dirty);
712
713 // Update accumAboveOpaqueLayers for next (lower) layer
714 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
715
716 // Compute the visible non-transparent region
717 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
718
Vishnu Naira483b4a2019-12-12 15:07:52 -0800719 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800720 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700721 const auto& outputState = getState();
722 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000723 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800724 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700725 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800726 }
727
Vishnu Naira483b4a2019-12-12 15:07:52 -0800728 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
729
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800730 // The layer is visible. Either reuse the existing outputLayer if we have
731 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800732 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800733
734 // Store the layer coverage information into the layer state as some of it
735 // is useful later.
736 auto& outputLayerState = result->editState();
737 outputLayerState.visibleRegion = visibleRegion;
738 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
739 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200740 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000741 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800742 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500743 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400744 layerFEState->compositionType == Composition::DISPLAY_DECORATION
745 ? outputState.transform.transform(
746 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
747 : Region();
Chavi Weingarten545da0e2023-02-09 14:55:57 +0000748 if (CC_UNLIKELY(computeAboveCoveredExcludingOverlays)) {
749 outputLayerState.coveredRegionExcludingDisplayOverlays =
750 std::move(coveredRegionExcludingDisplayOverlays);
751 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800752}
753
754void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
755 // The base class does nothing with this call.
756}
757
Dan Stoza269dc4d2021-01-15 15:07:43 -0800758void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800759 ATRACE_CALL();
760 ALOGV(__FUNCTION__);
761
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800762 if (!getState().isEnabled) {
763 return;
764 }
765
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800766 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
767 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
768
Lloyd Pique01c77c12019-04-17 12:48:32 -0700769 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700770 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800771 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200772 forceClientComposition,
773 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800774
775 if (mLayerRequestingBackgroundBlur == layer) {
776 forceClientComposition = false;
777 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800778 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000779
780 updateCompositionStateForBorder(refreshArgs);
781}
782
783void Output::updateCompositionStateForBorder(
784 const compositionengine::CompositionRefreshArgs& refreshArgs) {
785 std::unordered_map<int32_t, const Region*> layerVisibleRegionMap;
786 // Store a map of layerId to their computed visible region.
787 for (auto* layer : getOutputLayersOrderedByZ()) {
788 int layerId = (layer->getLayerFE()).getSequence();
789 layerVisibleRegionMap[layerId] = &((layer->getState()).visibleRegion);
790 }
791 OutputCompositionState& outputCompositionState = editState();
792 outputCompositionState.borderInfoList.clear();
793 bool clientComposeTopLayer = false;
794 for (const auto& borderInfo : refreshArgs.borderInfoList) {
795 renderengine::BorderRenderInfo info;
796 for (const auto& id : borderInfo.layerIds) {
797 info.combinedRegion.orSelf(*(layerVisibleRegionMap[id]));
798 }
Tianhao Yao10cea3c2022-03-30 01:37:22 +0000799
800 if (!info.combinedRegion.isEmpty()) {
801 info.width = borderInfo.width;
802 info.color = borderInfo.color;
803 outputCompositionState.borderInfoList.emplace_back(std::move(info));
804 clientComposeTopLayer = true;
805 }
Tianhao Yao67dd7122022-02-22 17:48:33 +0000806 }
807
808 // In this situation we must client compose the top layer instead of using hwc
809 // because we want to draw the border above all else.
810 // This could potentially cause a bit of a performance regression if the top
811 // layer would have been rendered using hwc originally.
812 // TODO(b/227656283): Measure system's performance before enabling the border feature
813 if (clientComposeTopLayer) {
814 auto topLayer = getOutputLayerOrderedByZByIndex(getOutputLayerCount() - 1);
815 (topLayer->editState()).forceClientComposition = true;
816 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800817}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800818
Dan Stoza269dc4d2021-01-15 15:07:43 -0800819void Output::planComposition() {
820 if (!mPlanner || !getState().isEnabled) {
821 return;
822 }
823
824 ATRACE_CALL();
825 ALOGV(__FUNCTION__);
826
827 mPlanner->plan(getOutputLayersOrderedByZ());
828}
829
830void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
831 ATRACE_CALL();
832 ALOGV(__FUNCTION__);
833
834 if (!getState().isEnabled) {
835 return;
836 }
837
Ady Abraham3645e642021-04-20 18:39:00 -0700838 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abraham43065bd2021-12-10 17:22:15 -0800839 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
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()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100899 auto* compState = layer->getLayerFE().getCompositionState();
900
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 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000906 if (compState->isOpaque) {
907 continue;
908 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100909 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800910 layerRequestingBgComposition = layer;
911 }
912 }
913 return layerRequestingBgComposition;
914}
915
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800916void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
917 setColorProfile(pickColorProfile(refreshArgs));
918}
919
920// Returns a data space that fits all visible layers. The returned data space
921// can only be one of
922// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
923// - Dataspace::DISPLAY_P3
924// - Dataspace::DISPLAY_BT2020
925// The returned HDR data space is one of
926// - Dataspace::UNKNOWN
927// - Dataspace::BT2020_HLG
928// - Dataspace::BT2020_PQ
929ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
930 bool* outIsHdrClientComposition) const {
931 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
932 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
933
Vishnu Naire14c6b32022-08-06 04:20:15 +0000934 // An Output's layers may be stale when it is disabled. As a consequence, the layers returned by
935 // getOutputLayersOrderedByZ may not be in a valid state and it is not safe to access their
936 // properties. Return a default dataspace value in this case.
937 if (!getState().isEnabled) {
938 return ui::Dataspace::V0_SRGB;
939 }
940
Lloyd Pique01c77c12019-04-17 12:48:32 -0700941 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800942 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800943 case ui::Dataspace::V0_SCRGB:
944 case ui::Dataspace::V0_SCRGB_LINEAR:
945 case ui::Dataspace::BT2020:
946 case ui::Dataspace::BT2020_ITU:
947 case ui::Dataspace::BT2020_LINEAR:
948 case ui::Dataspace::DISPLAY_BT2020:
949 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
950 break;
951 case ui::Dataspace::DISPLAY_P3:
952 bestDataSpace = ui::Dataspace::DISPLAY_P3;
953 break;
954 case ui::Dataspace::BT2020_PQ:
955 case ui::Dataspace::BT2020_ITU_PQ:
956 bestDataSpace = ui::Dataspace::DISPLAY_P3;
957 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800958 *outIsHdrClientComposition =
959 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800960 break;
961 case ui::Dataspace::BT2020_HLG:
962 case ui::Dataspace::BT2020_ITU_HLG:
963 bestDataSpace = ui::Dataspace::DISPLAY_P3;
964 // When there's mixed PQ content and HLG content, we set the HDR
965 // data space to be BT2020_PQ and convert HLG to PQ.
966 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
967 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
968 }
969 break;
970 default:
971 break;
972 }
973 }
974
975 return bestDataSpace;
976}
977
978compositionengine::Output::ColorProfile Output::pickColorProfile(
979 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
980 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
981 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
Alec Mouri88790f32023-07-21 01:25:14 +0000982 ui::RenderIntent::COLORIMETRIC};
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800983 }
984
985 ui::Dataspace hdrDataSpace;
986 bool isHdrClientComposition = false;
987 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
988
989 switch (refreshArgs.forceOutputColorMode) {
990 case ui::ColorMode::SRGB:
991 bestDataSpace = ui::Dataspace::V0_SRGB;
992 break;
993 case ui::ColorMode::DISPLAY_P3:
994 bestDataSpace = ui::Dataspace::DISPLAY_P3;
995 break;
996 default:
997 break;
998 }
999
1000 // respect hdrDataSpace only when there is no legacy HDR support
1001 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
1002 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
1003 if (isHdr) {
1004 bestDataSpace = hdrDataSpace;
1005 }
1006
1007 ui::RenderIntent intent;
1008 switch (refreshArgs.outputColorSetting) {
1009 case OutputColorSetting::kManaged:
1010 case OutputColorSetting::kUnmanaged:
1011 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
1012 : ui::RenderIntent::COLORIMETRIC;
1013 break;
1014 case OutputColorSetting::kEnhanced:
1015 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
1016 break;
1017 default: // vendor display color setting
1018 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
1019 break;
1020 }
1021
1022 ui::ColorMode outMode;
1023 ui::Dataspace outDataSpace;
1024 ui::RenderIntent outRenderIntent;
1025 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
1026 &outRenderIntent);
1027
Alec Mouri88790f32023-07-21 01:25:14 +00001028 return ColorProfile{outMode, outDataSpace, outRenderIntent};
Lloyd Pique6a3b4462019-03-07 20:58:12 -08001029}
1030
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001031void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001032 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001033 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -07001034 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001035 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001036
1037 // If nothing has changed (!dirty), don't recompose.
1038 // If something changed, but we don't currently have any visible layers,
1039 // and didn't when we last did a composition, then skip it this time.
1040 // The second rule does two things:
1041 // - When all layers are removed from a display, we'll emit one black
1042 // frame, then nothing more until we get new layers.
1043 // - When a display is created with a private layer stack, we won't
1044 // emit any black frames until a layer is added to the layer stack.
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001045 mMustRecompose = dirty && !(empty && wasEmpty);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001046
1047 const char flagPrefix[] = {'-', '+'};
1048 static_cast<void>(flagPrefix);
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001049 ALOGV("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __func__,
1050 mMustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
1051 flagPrefix[empty], flagPrefix[wasEmpty]);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001052
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001053 mRenderSurface->beginFrame(mMustRecompose);
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001054
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001055 if (mMustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001056 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -08001057 }
1058}
1059
Lloyd Pique66d68602019-02-13 14:23:31 -08001060void Output::prepareFrame() {
1061 ATRACE_CALL();
1062 ALOGV(__FUNCTION__);
1063
Vishnu Naira3140382022-02-24 14:07:11 -08001064 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001065 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -08001066 return;
1067 }
1068
Vishnu Naira3140382022-02-24 14:07:11 -08001069 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1070 bool success = chooseCompositionStrategy(&changes);
1071 resetCompositionStrategy();
Vishnu Nair9cf89262022-02-26 09:17:49 -08001072 outputState.strategyPrediction = CompositionStrategyPredictionState::DISABLED;
Vishnu Naira3140382022-02-24 14:07:11 -08001073 outputState.previousDeviceRequestedChanges = changes;
1074 outputState.previousDeviceRequestedSuccess = success;
1075 if (success) {
1076 applyCompositionStrategy(changes);
1077 }
1078 finishPrepareFrame();
1079}
Lloyd Pique66d68602019-02-13 14:23:31 -08001080
Vishnu Naira3140382022-02-24 14:07:11 -08001081std::future<bool> Output::chooseCompositionStrategyAsync(
1082 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
1083 return mHwComposerAsyncWorker->send(
1084 [&, changes]() { return chooseCompositionStrategy(changes); });
1085}
1086
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001087GpuCompositionResult Output::prepareFrameAsync() {
Vishnu Naira3140382022-02-24 14:07:11 -08001088 ATRACE_CALL();
1089 ALOGV(__FUNCTION__);
1090 auto& state = editState();
1091 const auto& previousChanges = state.previousDeviceRequestedChanges;
1092 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
1093 resetCompositionStrategy();
1094 auto hwcResult = chooseCompositionStrategyAsync(&changes);
1095 if (state.previousDeviceRequestedSuccess) {
1096 applyCompositionStrategy(previousChanges);
1097 }
1098 finishPrepareFrame();
1099
1100 base::unique_fd bufferFence;
1101 std::shared_ptr<renderengine::ExternalTexture> buffer;
1102 updateProtectedContentState();
1103 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1104 GpuCompositionResult compositionResult;
1105 if (dequeueSucceeded) {
1106 std::optional<base::unique_fd> optFd =
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001107 composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001108 if (optFd) {
1109 compositionResult.fence = std::move(*optFd);
1110 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001111 }
1112
Vishnu Naira3140382022-02-24 14:07:11 -08001113 auto chooseCompositionSuccess = hwcResult.get();
1114 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001115 state.strategyPrediction = predictionSucceeded ? CompositionStrategyPredictionState::SUCCESS
1116 : CompositionStrategyPredictionState::FAIL;
Vishnu Naira3140382022-02-24 14:07:11 -08001117 if (!predictionSucceeded) {
1118 ATRACE_NAME("CompositionStrategyPredictionMiss");
1119 resetCompositionStrategy();
1120 if (chooseCompositionSuccess) {
1121 applyCompositionStrategy(changes);
1122 }
1123 finishPrepareFrame();
1124 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1125 compositionResult.buffer = buffer;
1126 } else {
1127 ATRACE_NAME("CompositionStrategyPredictionHit");
1128 }
1129 state.previousDeviceRequestedChanges = std::move(changes);
1130 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1131 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001132}
1133
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001134void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1135 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1136 return;
1137 }
1138
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001139 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001140 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001141 base::unique_fd bufferFence;
1142 std::shared_ptr<renderengine::ExternalTexture> buffer;
1143 updateProtectedContentState();
1144 dequeueRenderBuffer(&bufferFence, &buffer);
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001145 static_cast<void>(composeSurfaces(dirtyRegion, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001146 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001147 }
1148 }
1149
1150 postFramebuffer();
1151
1152 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1153
1154 prepareFrame();
1155}
1156
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001157void Output::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001158 ATRACE_CALL();
1159 ALOGV(__FUNCTION__);
Vishnu Nair9cf89262022-02-26 09:17:49 -08001160 const auto& outputState = getState();
1161 if (!outputState.isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001162 return;
1163 }
1164
Vishnu Naira3140382022-02-24 14:07:11 -08001165 std::optional<base::unique_fd> optReadyFence;
1166 std::shared_ptr<renderengine::ExternalTexture> buffer;
1167 base::unique_fd bufferFence;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001168 if (outputState.strategyPrediction == CompositionStrategyPredictionState::SUCCESS) {
Vishnu Naira3140382022-02-24 14:07:11 -08001169 optReadyFence = std::move(result.fence);
1170 } else {
1171 if (result.bufferAvailable()) {
1172 buffer = std::move(result.buffer);
1173 bufferFence = std::move(result.fence);
1174 } else {
1175 updateProtectedContentState();
1176 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1177 return;
1178 }
1179 }
1180 // Repaint the framebuffer (if needed), getting the optional fence for when
1181 // the composition completes.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001182 optReadyFence = composeSurfaces(Region::INVALID_REGION, buffer, bufferFence);
Vishnu Naira3140382022-02-24 14:07:11 -08001183 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001184 if (!optReadyFence) {
1185 return;
1186 }
1187
Matt Buckley50c44062022-01-17 20:48:10 +00001188 if (isPowerHintSessionEnabled()) {
1189 // get fence end time to know when gpu is complete in display
Ady Abrahamd11bade2022-08-01 16:18:03 -07001190 setHintSessionGpuFence(
1191 std::make_unique<FenceTime>(sp<Fence>::make(dup(optReadyFence->get()))));
Matt Buckley50c44062022-01-17 20:48:10 +00001192 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001193 // swap buffers (presentation)
1194 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1195}
1196
Vishnu Naira3140382022-02-24 14:07:11 -08001197void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001198 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001199 auto& renderEngine = getCompositionEngine().getRenderEngine();
1200 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1201
1202 // If we the display is secure, protected content support is enabled, and at
1203 // least one layer has protected content, we need to use a secure back
1204 // buffer.
1205 if (outputState.isSecure && supportsProtectedContent) {
1206 auto layers = getOutputLayersOrderedByZ();
1207 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1208 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1209 });
Patrick Williams8aed5d22022-10-31 22:18:10 +00001210 if (needsProtected != mRenderSurface->isProtected()) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001211 mRenderSurface->setProtected(needsProtected);
1212 }
1213 }
Vishnu Naira3140382022-02-24 14:07:11 -08001214}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001215
Vishnu Naira3140382022-02-24 14:07:11 -08001216bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1217 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1218 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001219
1220 // If we aren't doing client composition on this output, but do have a
1221 // flipClientTarget request for this frame on this output, we still need to
1222 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001223 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1224 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1225 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001226 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1227 "client composition for this frame",
1228 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001229 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001230 }
1231 }
Vishnu Naira3140382022-02-24 14:07:11 -08001232 return true;
1233}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001234
Vishnu Naira3140382022-02-24 14:07:11 -08001235std::optional<base::unique_fd> Output::composeSurfaces(
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001236 const Region& debugRegion, std::shared_ptr<renderengine::ExternalTexture> tex,
1237 base::unique_fd& fd) {
Vishnu Naira3140382022-02-24 14:07:11 -08001238 ATRACE_CALL();
1239 ALOGV(__FUNCTION__);
1240
1241 const auto& outputState = getState();
Leon Scroggins III042fdba2023-01-04 10:53:07 -05001242 const TracedOrdinal<bool> hasClientComposition = {
1243 base::StringPrintf("hasClientComposition %s", mNamePlusId.c_str()),
1244 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001245 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001246 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001247 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001248 }
1249
Vishnu Naira3140382022-02-24 14:07:11 -08001250 if (tex == nullptr) {
1251 ALOGW("Buffer not valid for display [%s], bailing out of "
1252 "client composition for this frame",
1253 mName.c_str());
1254 return {};
1255 }
1256
Lloyd Pique688abd42019-02-15 15:42:24 -08001257 ALOGV("hasClientComposition");
1258
Patrick Williams7584c6a2022-10-29 02:10:58 +00001259 renderengine::DisplaySettings clientCompositionDisplay =
1260 generateClientCompositionDisplaySettings();
Lloyd Pique688abd42019-02-15 15:42:24 -08001261
Lloyd Pique688abd42019-02-15 15:42:24 -08001262 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001263 auto& renderEngine = getCompositionEngine().getRenderEngine();
1264 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001265 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001266 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001267 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001268 clientCompositionDisplay.outputDataspace,
1269 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001270 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1271
Vishnu Naira3140382022-02-24 14:07:11 -08001272 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001273 // Check if the client composition requests were rendered into the provided graphic buffer. If
1274 // so, we can reuse the buffer and avoid client composition.
1275 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001276 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1277 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001278 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001279 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001280 outputCompositionState.reusedClientComposition = true;
1281 setExpensiveRenderingExpected(false);
Vishnu Nair3a49f0a2022-07-29 21:52:53 +00001282 // b/239944175 pass the fence associated with the buffer.
1283 return base::unique_fd(std::move(fd));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001284 }
Vishnu Naira3140382022-02-24 14:07:11 -08001285 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001286 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001287 clientCompositionLayers);
1288 }
1289
Lloyd Pique688abd42019-02-15 15:42:24 -08001290 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001291 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1292 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1293 // because high frequency consumes extra battery.
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +00001294 const bool expensiveRenderingExpected =
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001295 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1296 [outputDataspace =
1297 clientCompositionDisplay.outputDataspace](const auto& layer) {
1298 return layer.sourceDataspace != outputDataspace;
1299 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001300 if (expensiveRenderingExpected) {
1301 setExpensiveRenderingExpected(true);
1302 }
1303
Sally Qi59a9f502021-10-12 18:53:23 +00001304 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1305 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001306 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001307 std::back_inserter(clientRenderEngineLayers),
1308 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1309 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001310 });
1311
Alec Mourie4034bb2019-11-19 12:45:54 -08001312 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001313 // Only use the framebuffer cache when rendering to an internal display
1314 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1315 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1316 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1317 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1318 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001319 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001320
Patrick Williams2e9748f2022-08-09 22:48:18 +00001321 auto fenceResult = renderEngine
1322 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
1323 useFramebufferCache, std::move(fd))
1324 .get();
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001325
1326 if (mClientCompositionRequestCache && fenceStatus(fenceResult) != NO_ERROR) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001327 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001328 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001329 }
1330
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001331 const auto fence = std::move(fenceResult).value_or(Fence::NO_FENCE);
1332
Patrick Williams74c0bf62022-11-02 23:59:26 +00001333 if (auto timeStats = getCompositionEngine().getTimeStats()) {
1334 if (fence->isValid()) {
1335 timeStats->recordRenderEngineDuration(renderEngineStart,
1336 std::make_shared<FenceTime>(fence));
1337 } else {
1338 timeStats->recordRenderEngineDuration(renderEngineStart, systemTime());
1339 }
Alec Mourie4034bb2019-11-19 12:45:54 -08001340 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001341
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001342 for (auto* clientComposedLayer : clientCompositionLayersFE) {
1343 clientComposedLayer->setWasClientComposed(fence);
Robert Carrccab4242021-09-28 16:53:03 -07001344 }
1345
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001346 return base::unique_fd(fence->dup());
Lloyd Pique688abd42019-02-15 15:42:24 -08001347}
1348
Patrick Williams7584c6a2022-10-29 02:10:58 +00001349renderengine::DisplaySettings Output::generateClientCompositionDisplaySettings() const {
1350 const auto& outputState = getState();
1351
1352 renderengine::DisplaySettings clientCompositionDisplay;
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001353 clientCompositionDisplay.namePlusId = mNamePlusId;
Patrick Williams7584c6a2022-10-29 02:10:58 +00001354 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1355 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
1356 clientCompositionDisplay.orientation =
1357 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
1358 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1359 ? outputState.dataspace
1360 : ui::Dataspace::UNKNOWN;
1361
1362 // If we have a valid current display brightness use that, otherwise fall back to the
1363 // display's max desired
1364 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
1365 ? outputState.displayBrightnessNits
1366 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1367 clientCompositionDisplay.maxLuminance =
1368 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1369 clientCompositionDisplay.targetLuminanceNits =
1370 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
1371 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
1372 clientCompositionDisplay.renderIntent =
1373 static_cast<aidl::android::hardware::graphics::composer3::RenderIntent>(
1374 outputState.renderIntent);
1375
1376 // Compute the global color transform matrix.
1377 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1378 for (auto& info : outputState.borderInfoList) {
1379 renderengine::BorderRenderInfo borderInfo;
1380 borderInfo.width = info.width;
1381 borderInfo.color = info.color;
1382 borderInfo.combinedRegion = info.combinedRegion;
1383 clientCompositionDisplay.borderInfoList.emplace_back(std::move(borderInfo));
1384 }
1385 clientCompositionDisplay.deviceHandlesColorTransform =
1386 outputState.usesDeviceComposition || getSkipColorTransform();
1387 return clientCompositionDisplay;
1388}
1389
Vishnu Nair9b079a22020-01-21 14:36:08 -08001390std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001391 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001392 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001393 ALOGV("Rendering client layers");
1394
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001395 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001396 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001397 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001398
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001399 bool disableBlurs = false;
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001400 uint64_t previousOverrideBufferId = 0;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001401
Lloyd Pique01c77c12019-04-17 12:48:32 -07001402 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001403 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001404 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001405 auto& layerFE = layer->getLayerFE();
Robert Carr05da0082022-05-25 23:29:34 -07001406 layerFE.setWasClientComposed(nullptr);
Lloyd Pique688abd42019-02-15 15:42:24 -08001407
Lloyd Piquea2468662019-03-07 21:31:06 -08001408 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001409 ALOGV("Layer: %s", layerFE.getDebugName());
1410 if (clip.isEmpty()) {
1411 ALOGV(" Skipping for empty clip");
1412 firstLayer = false;
1413 continue;
1414 }
1415
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001416 disableBlurs |= layerFEState->sidebandStream != nullptr;
1417
Vishnu Naira483b4a2019-12-12 15:07:52 -08001418 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001419
1420 // We clear the client target for non-client composed layers if
1421 // requested by the HWC. We skip this if the layer is not an opaque
1422 // rectangle, as by definition the layer must blend with whatever is
1423 // underneath. We also skip the first layer as the buffer target is
1424 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001425 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001426 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001427
1428 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1429
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001430 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1431 // composing the non-shadow content and only draw the shadows.
1432 const bool realContentIsVisible = clientComposition &&
1433 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1434
Lloyd Pique688abd42019-02-15 15:42:24 -08001435 if (clientComposition || clearClientComposition) {
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001436 if (auto overrideSettings = layer->getOverrideCompositionSettings()) {
1437 if (overrideSettings->bufferId != previousOverrideBufferId) {
1438 previousOverrideBufferId = overrideSettings->bufferId;
1439 clientCompositionLayers.push_back(std::move(*overrideSettings));
Huihong Luo91ac3b52021-04-08 11:07:41 -07001440 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1441 } else {
1442 ALOGV("Skipping redundant override buffer for [%s] in RE",
1443 layer->getLayerFE().getDebugName());
1444 }
Dan Stoza6166c312021-01-15 16:34:05 -08001445 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001446 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1447 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1448 : (layer->getState().overrideInfo.disableBackgroundBlur
1449 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1450 BlurRegionsOnly
1451 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1452 Enabled);
1453 compositionengine::LayerFE::ClientCompositionTargetSettings
1454 targetSettings{.clip = clip,
Patrick Williams278a88f2023-01-27 16:52:40 -06001455 .needsFiltering = layer->needsFiltering() ||
Alec Mourif54453c2021-05-13 16:28:28 -07001456 outputState.needsFiltering,
1457 .isSecure = outputState.isSecure,
1458 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001459 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001460 .dataspace = outputDataspace,
1461 .realContentIsVisible = realContentIsVisible,
1462 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001463 .blurSetting = blurSetting,
Vishnu Naire14c6b32022-08-06 04:20:15 +00001464 .whitePointNits = layerState.whitePointNits,
1465 .treat170mAsSrgb = outputState.treat170mAsSrgb};
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001466 if (auto clientCompositionSettings =
1467 layerFE.prepareClientComposition(targetSettings)) {
1468 clientCompositionLayers.push_back(std::move(*clientCompositionSettings));
1469 if (realContentIsVisible) {
1470 layer->editState().clientCompositionTimestamp = systemTime();
1471 }
Dan Stoza6166c312021-01-15 16:34:05 -08001472 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001473 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001474
Tianhua Sunf91f1402022-05-09 05:45:46 +00001475 if (clientComposition) {
1476 outLayerFEs.push_back(&layerFE);
1477 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001478 }
1479
1480 firstLayer = false;
1481 }
1482
1483 return clientCompositionLayers;
1484}
1485
1486void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001487 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001488 if (flashRegion.isEmpty()) {
1489 return;
1490 }
1491
Vishnu Nair9b079a22020-01-21 14:36:08 -08001492 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001493 layerSettings.source.buffer.buffer = nullptr;
1494 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1495 layerSettings.alpha = half(1.0);
1496
1497 for (const auto& rect : flashRegion) {
1498 layerSettings.geometry.boundaries = rect.toFloatRect();
1499 clientCompositionLayers.push_back(layerSettings);
1500 }
1501}
1502
1503void Output::setExpensiveRenderingExpected(bool) {
1504 // The base class does nothing with this call.
1505}
1506
Matt Buckley50c44062022-01-17 20:48:10 +00001507void Output::setHintSessionGpuFence(std::unique_ptr<FenceTime>&&) {
1508 // The base class does nothing with this call.
1509}
1510
1511bool Output::isPowerHintSessionEnabled() {
1512 return false;
1513}
1514
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001515void Output::postFramebuffer() {
Leon Scroggins III5a655b82022-09-07 13:17:09 -04001516 ATRACE_FORMAT("%s for %s", __func__, mNamePlusId.c_str());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001517 ALOGV(__FUNCTION__);
1518
1519 if (!getState().isEnabled) {
1520 return;
1521 }
1522
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001523 auto& outputState = editState();
1524 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001525
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001526 auto frame = presentAndGetFrameFences();
1527
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001528 mRenderSurface->onPresentDisplayCompleted();
1529
Lloyd Pique01c77c12019-04-17 12:48:32 -07001530 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001531 // The layer buffer from the previous frame (if any) is released
1532 // by HWC only when the release fence from this frame (if any) is
1533 // signaled. Always get the release fence from HWC first.
1534 sp<Fence> releaseFence = Fence::NO_FENCE;
1535
1536 if (auto hwcLayer = layer->getHwcLayer()) {
1537 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1538 releaseFence = f->second;
1539 }
1540 }
1541
1542 // If the layer was client composited in the previous frame, we
1543 // need to merge with the previous client target acquire fence.
1544 // Since we do not track that, always merge with the current
1545 // client target acquire fence when it is available, even though
1546 // this is suboptimal.
1547 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001548 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001549 releaseFence =
1550 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1551 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001552 layer->getLayerFE()
1553 .onLayerDisplayed(ftl::yield<FenceResult>(std::move(releaseFence)).share(),
1554 outputState.layerFilter.layerStack);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001555 }
1556
1557 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001558 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001559 // supply them with the present fence.
1560 for (auto& weakLayer : mReleasedLayers) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -07001561 if (const auto layer = weakLayer.promote()) {
Vishnu Nair7ee4f462023-04-19 09:54:09 -07001562 layer->onLayerDisplayed(ftl::yield<FenceResult>(frame.presentFence).share(),
1563 outputState.layerFilter.layerStack);
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001564 }
1565 }
1566
1567 // Clear out the released layers now that we're done with them.
1568 mReleasedLayers.clear();
1569}
1570
Alec Mouriaa831582021-06-07 16:23:01 -07001571void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Leon Scroggins III43b5d522023-04-10 15:53:45 -04001572 const auto& outputState = getState();
1573 if (mPlanner && outputState.isEnabled) {
1574 mPlanner->renderCachedSets(outputState, refreshArgs.scheduledFrameTime,
1575 outputState.usesDeviceComposition || getSkipColorTransform());
Dan Stoza6166c312021-01-15 16:34:05 -08001576 }
1577}
1578
Lloyd Pique32cbe282018-10-19 13:09:22 -07001579void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001580 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001581 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001582}
1583
Vishnu Naira3140382022-02-24 14:07:11 -08001584void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001585 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001586 auto& outputState = editState();
1587 outputState.usesClientComposition = true;
1588 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001589 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001590}
1591
Lloyd Pique688abd42019-02-15 15:42:24 -08001592bool Output::getSkipColorTransform() const {
1593 return true;
1594}
1595
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001596compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1597 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001598 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001599 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1600 }
1601 return result;
1602}
1603
Vishnu Naira3140382022-02-24 14:07:11 -08001604void Output::setPredictCompositionStrategy(bool predict) {
1605 if (predict) {
1606 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1607 } else {
1608 mHwComposerAsyncWorker.reset(nullptr);
1609 }
1610}
1611
Alec Mouridda07d92022-04-25 22:39:25 +00001612void Output::setTreat170mAsSrgb(bool enable) {
1613 editState().treat170mAsSrgb = enable;
1614}
1615
Vishnu Naira3140382022-02-24 14:07:11 -08001616bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
Robert Carrec8ccca2022-05-04 09:36:14 -07001617 uint64_t lastOutputLayerHash = getState().lastOutputLayerHash;
1618 uint64_t outputLayerHash = getState().outputLayerHash;
1619 editState().lastOutputLayerHash = outputLayerHash;
1620
Vishnu Naira3140382022-02-24 14:07:11 -08001621 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1622 ALOGV("canPredictCompositionStrategy disabled");
1623 return false;
1624 }
1625
1626 if (!getState().previousDeviceRequestedChanges) {
1627 ALOGV("canPredictCompositionStrategy previous changes not available");
1628 return false;
1629 }
1630
1631 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1632 ALOGV("canPredictCompositionStrategy surface does not support");
1633 return false;
1634 }
1635
1636 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1637 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1638 return false;
1639 }
1640
Robert Carrec8ccca2022-05-04 09:36:14 -07001641 if (lastOutputLayerHash != outputLayerHash) {
1642 ALOGV("canPredictCompositionStrategy output layers changed");
1643 return false;
1644 }
1645
Vishnu Naira3140382022-02-24 14:07:11 -08001646 // If no layer uses clientComposition, then don't predict composition strategy
1647 // because we have less work to do in parallel.
1648 if (!anyLayersRequireClientComposition()) {
1649 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1650 return false;
1651 }
1652
Robert Carrec8ccca2022-05-04 09:36:14 -07001653 return true;
Vishnu Naira3140382022-02-24 14:07:11 -08001654}
1655
1656bool Output::anyLayersRequireClientComposition() const {
1657 const auto layers = getOutputLayersOrderedByZ();
1658 return std::any_of(layers.begin(), layers.end(),
1659 [](const auto& layer) { return layer->requiresClientComposition(); });
1660}
1661
1662void Output::finishPrepareFrame() {
1663 const auto& state = getState();
1664 if (mPlanner) {
1665 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1666 }
1667 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1668}
1669
Chavi Weingarten09fa1d62022-08-17 21:57:04 +00001670bool Output::mustRecompose() const {
1671 return mMustRecompose;
1672}
1673
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001674} // namespace impl
1675} // namespace android::compositionengine