blob: 6595ed3fe53d77e89cd2576574ba7da039ca3eba [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>
Dan Stoza269dc4d2021-01-15 15:07:43 -080032
Alec Mouria90a5702021-04-16 16:36:21 +000033#include <thread>
34
35#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080036
37// TODO(b/129481165): remove the #pragma below and fix conversion issues
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wconversion"
40
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include <renderengine/DisplaySettings.h>
42#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080043
44// TODO(b/129481165): remove the #pragma below and fix conversion issues
45#pragma clang diagnostic pop // ignored "-Wconversion"
46
Dan Stoza269dc4d2021-01-15 15:07:43 -080047#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070048#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080049#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080050#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070051
Lloyd Pique688abd42019-02-15 15:42:24 -080052#include "TracedOrdinal.h"
53
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050054using aidl::android::hardware::graphics::composer3::Composition;
55
Lloyd Piquefeb73d72018-12-04 17:23:44 -080056namespace android::compositionengine {
57
58Output::~Output() = default;
59
60namespace impl {
Robert Carra00eb142022-03-09 13:49:30 -080061
Lloyd Piquec29e4c62019-03-07 21:48:19 -080062namespace {
63
64template <typename T>
65class Reversed {
66public:
67 explicit Reversed(const T& container) : mContainer(container) {}
68 auto begin() { return mContainer.rbegin(); }
69 auto end() { return mContainer.rend(); }
70
71private:
72 const T& mContainer;
73};
74
75// Helper for enumerating over a container in reverse order
76template <typename T>
77Reversed<T> reversed(const T& c) {
78 return Reversed<T>(c);
79}
80
Marin Shalamanovb15d2272020-09-17 21:41:52 +020081struct ScaleVector {
82 float x;
83 float y;
84};
85
86// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
87// where to' will have the same size as "to". In the case where "from" and "to"
88// start at the origin to'=to.
89ScaleVector getScale(const Rect& from, const Rect& to) {
90 return {.x = static_cast<float>(to.width()) / from.width(),
91 .y = static_cast<float>(to.height()) / from.height()};
92}
93
Lloyd Piquec29e4c62019-03-07 21:48:19 -080094} // namespace
95
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070096std::shared_ptr<Output> createOutput(
97 const compositionengine::CompositionEngine& compositionEngine) {
98 return createOutputTemplated<Output>(compositionEngine);
99}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700100
101Output::~Output() = default;
102
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700104 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
105 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106}
107
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700108std::optional<DisplayId> Output::getDisplayId() const {
109 return {};
110}
111
Lloyd Pique32cbe282018-10-19 13:09:22 -0700112const std::string& Output::getName() const {
113 return mName;
114}
115
116void Output::setName(const std::string& name) {
117 mName = name;
118}
119
120void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700121 auto& outputState = editState();
122 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123 return;
124 }
125
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700126 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700127 dirtyEntireOutput();
128}
129
Alec Mouri023c1882021-05-08 16:36:33 -0700130void Output::setLayerCachingEnabled(bool enabled) {
131 if (enabled == (mPlanner != nullptr)) {
132 return;
133 }
134
135 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700136 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700137 if (mRenderSurface) {
138 mPlanner->setDisplaySize(mRenderSurface->getSize());
139 }
140 } else {
141 mPlanner.reset();
142 }
Alec Mouric773472b2021-05-19 14:29:05 -0700143
144 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
145 if (!outputLayer) {
146 continue;
147 }
148
149 outputLayer->editState().overrideInfo = {};
150 }
Alec Mouri023c1882021-05-08 16:36:33 -0700151}
152
Ady Abrahamdb036a82021-07-16 14:18:34 -0700153void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
154 if (mPlanner) {
155 mPlanner->setTexturePoolEnabled(enabled);
156 }
157}
158
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200159void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
160 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700161 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200162
Angel Aguayob084e0c2021-08-04 23:27:28 +0000163 outputState.displaySpace.setOrientation(orientation);
164 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200165 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200166
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200167 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000168 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200169 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200170 std::swap(orientedSize.width, orientedSize.height);
171 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000172 outputState.orientedDisplaySpace.setBounds(orientedSize);
173 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200174
175 // Compute displaySpace.content
176 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
177 ui::Transform rotation;
178 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000179 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200180 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
181 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000182 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200183
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200184 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000185 outputState.framebufferSpace.setOrientation(orientation);
186 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200187 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000188 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
189 outputState.framebufferSpace.getBoundsAsRect());
190 outputState.framebufferSpace.setContent(
191 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200192
193 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000194 outputState.layerStackSpace.setContent(layerStackSpaceRect);
195 outputState.layerStackSpace.setBounds(
196 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200197
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200198 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
199 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700200 dirtyEntireOutput();
201}
202
Alec Mouricdf16792021-12-10 13:16:06 -0800203void Output::setNextBrightness(float brightness) {
204 editState().displayBrightness = brightness;
205}
206
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200207void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700208 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200209
210 auto& state = editState();
211
212 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000213 const ui::Size newBounds(size);
214 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200215
216 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000217 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200218 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
219
220 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000221 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200222 ui::Size orientedSize = size;
223 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
224 std::swap(orientedSize.width, orientedSize.height);
225 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000226 const ui::Size newOrientedBounds(orientedSize);
227 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700228
Dan Stoza6166c312021-01-15 16:34:05 -0800229 if (mPlanner) {
230 mPlanner->setDisplaySize(size);
231 }
232
Lloyd Pique32cbe282018-10-19 13:09:22 -0700233 dirtyEntireOutput();
234}
235
Garfield Tan54edd912020-10-21 16:31:41 -0700236ui::Transform::RotationFlags Output::getTransformHint() const {
237 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
238}
239
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700240void Output::setLayerFilter(ui::LayerFilter filter) {
241 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700242 dirtyEntireOutput();
243}
244
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800245void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700246 auto& colorTransformMatrix = editState().colorTransformMatrix;
247 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700248 return;
249 }
250
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800252
253 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700254}
255
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800256void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700257 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800258 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
259 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800260
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700261 auto& outputState = editState();
262 if (outputState.colorMode == colorProfile.mode &&
263 outputState.dataspace == colorProfile.dataspace &&
264 outputState.renderIntent == colorProfile.renderIntent &&
265 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800266 return;
267 }
268
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700269 outputState.colorMode = colorProfile.mode;
270 outputState.dataspace = colorProfile.dataspace;
271 outputState.renderIntent = colorProfile.renderIntent;
272 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700273
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800274 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700275
Lloyd Pique32cbe282018-10-19 13:09:22 -0700276 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800277 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
278 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800279
280 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700281}
282
John Reckac09e452021-04-07 16:35:37 -0400283void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
284 auto& outputState = editState();
285 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
286 outputState.displayBrightnessNits == displayBrightnessNits) {
287 // Nothing changed
288 return;
289 }
290 outputState.sdrWhitePointNits = sdrWhitePointNits;
291 outputState.displayBrightnessNits = displayBrightnessNits;
292 dirtyEntireOutput();
293}
294
Lloyd Pique32cbe282018-10-19 13:09:22 -0700295void Output::dump(std::string& out) const {
296 using android::base::StringAppendF;
297
298 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
299
300 out.append("\n ");
301
302 dumpBase(out);
303}
304
305void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700306 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700307
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700308 if (mDisplayColorProfile) {
309 mDisplayColorProfile->dump(out);
310 } else {
311 out.append(" No display color profile!\n");
312 }
313
Lloyd Pique31cb2942018-10-19 17:23:03 -0700314 if (mRenderSurface) {
315 mRenderSurface->dump(out);
316 } else {
317 out.append(" No render surface!\n");
318 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800319
Lloyd Pique01c77c12019-04-17 12:48:32 -0700320 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
321 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800322 if (!outputLayer) {
323 continue;
324 }
325 outputLayer->dump(out);
326 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700327}
328
Dan Stoza269dc4d2021-01-15 15:07:43 -0800329void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
330 if (!mPlanner) {
331 base::StringAppendF(&out, "Planner is disabled\n");
332 return;
333 }
334 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
335 mPlanner->dump(args, out);
336}
337
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700338compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
339 return mDisplayColorProfile.get();
340}
341
342void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
343 mDisplayColorProfile = std::move(mode);
344}
345
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800346const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
347 return mReleasedLayers;
348}
349
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700350void Output::setDisplayColorProfileForTest(
351 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
352 mDisplayColorProfile = std::move(mode);
353}
354
Lloyd Pique31cb2942018-10-19 17:23:03 -0700355compositionengine::RenderSurface* Output::getRenderSurface() const {
356 return mRenderSurface.get();
357}
358
359void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
360 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800361 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000362 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800363 if (mPlanner) {
364 mPlanner->setDisplaySize(size);
365 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700366 dirtyEntireOutput();
367}
368
Vishnu Nair9b079a22020-01-21 14:36:08 -0800369void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
370 if (cacheSize == 0) {
371 mClientCompositionRequestCache.reset();
372 } else {
373 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
374 }
375};
376
Lloyd Pique31cb2942018-10-19 17:23:03 -0700377void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
378 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700379}
380
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700381Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700382 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000383 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700384}
385
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700386bool Output::includesLayer(ui::LayerFilter filter) const {
387 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700388}
389
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700390bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800391 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700392 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800393}
394
Lloyd Piquedf336d92019-03-07 21:38:42 -0800395std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800396 const sp<LayerFE>& layerFE) const {
397 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800398}
399
Lloyd Piquede196652020-01-22 17:29:58 -0800400compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
401 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700402 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800403}
404
Lloyd Pique01c77c12019-04-17 12:48:32 -0700405std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800406 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700407 for (size_t i = 0; i < getOutputLayerCount(); i++) {
408 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800409 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700410 return i;
411 }
412 }
413 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800414}
415
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800416void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
417 mReleasedLayers = std::move(layers);
418}
419
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800420void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
421 LayerFESet& geomSnapshots) {
422 ATRACE_CALL();
423 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800424
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800426}
427
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800428void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800429 ATRACE_CALL();
430 ALOGV(__FUNCTION__);
431
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800432 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800433 updateCompositionState(refreshArgs);
434 planComposition();
435 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800436 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800437 beginFrame();
Vishnu Naira3140382022-02-24 14:07:11 -0800438
439 GpuCompositionResult result;
440 const bool predictCompositionStrategy = canPredictCompositionStrategy(refreshArgs);
441 if (predictCompositionStrategy) {
442 result = prepareFrameAsync(refreshArgs);
443 } else {
444 prepareFrame();
445 }
446
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800447 devOptRepaintFlash(refreshArgs);
Vishnu Naira3140382022-02-24 14:07:11 -0800448 finishFrame(refreshArgs, std::move(result));
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800449 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700450 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800451}
452
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800453void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
454 LayerFESet& layerFESet) {
455 ATRACE_CALL();
456 ALOGV(__FUNCTION__);
457
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700458 auto& outputState = editState();
459
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800460 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700461 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462 return;
463 }
464
465 // Process the layers to determine visibility and coverage
466 compositionengine::Output::CoverageState coverage{layerFESet};
467 collectVisibleLayers(refreshArgs, coverage);
468
469 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700470 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000471 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800472 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
473
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700474 outputState.undefinedRegion = undefinedRegion;
475 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800476}
477
478void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
479 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800480 // Evaluate the layers from front to back to determine what is visible. This
481 // also incrementally calculates the coverage information for each layer as
482 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800483 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700484 // Incrementally process the coverage for each layer
485 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800486
487 // TODO(b/121291683): Stop early if the output is completely covered and
488 // no more layers could even be visible underneath the ones on top.
489 }
490
Lloyd Pique01c77c12019-04-17 12:48:32 -0700491 setReleasedLayers(refreshArgs);
492
493 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800494}
495
Lloyd Piquede196652020-01-22 17:29:58 -0800496void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700497 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800498 // Ensure we have a snapshot of the basic geometry layer state. Limit the
499 // snapshots to once per frame for each candidate layer, as layers may
500 // appear on multiple outputs.
501 if (!coverage.latchedLayers.count(layerFE)) {
502 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800503 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800504 }
505
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700506 // Only consider the layers on this output
507 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800508 return;
509 }
510
511 // Obtain a read-only pointer to the front-end layer state
512 const auto* layerFEState = layerFE->getCompositionState();
513 if (CC_UNLIKELY(!layerFEState)) {
514 return;
515 }
516
517 // handle hidden surfaces by setting the visible region to empty
518 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700519 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800520 }
521
522 /*
523 * opaqueRegion: area of a surface that is fully opaque.
524 */
525 Region opaqueRegion;
526
527 /*
528 * visibleRegion: area of a surface that is visible on screen and not fully
529 * transparent. This is essentially the layer's footprint minus the opaque
530 * regions above it. Areas covered by a translucent surface are considered
531 * visible.
532 */
533 Region visibleRegion;
534
535 /*
536 * coveredRegion: area of a surface that is covered by all visible regions
537 * above it (which includes the translucent areas).
538 */
539 Region coveredRegion;
540
541 /*
542 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500543 * transparent.
544 * This is used to tell when the layer has no visible non-transparent
545 * regions and can be removed from the layer list. It does not affect the
546 * visibleRegion of this layer or any layers beneath it. The hint may not
547 * be correct if apps don't respect the SurfaceView restrictions (which,
548 * sadly, some don't).
549 *
550 * In addition, it is used on DISPLAY_DECORATION layers to specify the
551 * blockingRegion, allowing the DPU to skip it to save power. Once we have
552 * hardware that supports a blockingRegion on frames with AFBC, it may be
553 * useful to use this for other layers, too, so long as we can prevent
554 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800555 */
556 Region transparentRegion;
557
Vishnu Naira483b4a2019-12-12 15:07:52 -0800558 /*
559 * shadowRegion: Region cast by the layer's shadow.
560 */
561 Region shadowRegion;
562
Lloyd Piquede196652020-01-22 17:29:58 -0800563 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800564
565 // Get the visible region
566 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
567 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800568 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800569 visibleRegion.set(visibleRect);
570
Lloyd Piquede196652020-01-22 17:29:58 -0800571 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800572 // if the layer casts a shadow, offset the layers visible region and
573 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800574 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800575 Rect visibleRectWithShadows(visibleRect);
576 visibleRectWithShadows.inset(inset, inset, inset, inset);
577 visibleRegion.set(visibleRectWithShadows);
578 shadowRegion = visibleRegion.subtract(visibleRect);
579 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800580
581 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700582 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800583 }
584
585 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800586 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800587 if (tr.preserveRects()) {
588 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800589 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800590 } else {
591 // transformation too complex, can't do the
592 // transparent region optimization.
593 transparentRegion.clear();
594 }
595 }
596
597 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800598 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800599 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800600 // If we one of the simple category of transforms (0/90/180/270 rotation
601 // + any flip), then the opaque region is the layer's footprint.
602 // Otherwise we don't try and compute the opaque region since there may
603 // be errors at the edges, and we treat the entire layer as
604 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800605 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800606 }
607
608 // Clip the covered region to the visible region
609 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
610
611 // Update accumAboveCoveredLayers for next (lower) layer
612 coverage.aboveCoveredLayers.orSelf(visibleRegion);
613
614 // subtract the opaque region covered by the layers above us
615 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
616
617 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700618 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800619 }
620
621 // Get coverage information for the layer as previously displayed,
622 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800623 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700624 auto prevOutputLayer =
625 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800626
627 // Get coverage information for the layer as previously displayed
628 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
629 const Region kEmptyRegion;
630 const Region& oldVisibleRegion =
631 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
632 const Region& oldCoveredRegion =
633 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
634
635 // compute this layer's dirty region
636 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800637 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800638 // we need to invalidate the whole region
639 dirty = visibleRegion;
640 // as well, as the old visible region
641 dirty.orSelf(oldVisibleRegion);
642 } else {
643 /* compute the exposed region:
644 * the exposed region consists of two components:
645 * 1) what's VISIBLE now and was COVERED before
646 * 2) what's EXPOSED now less what was EXPOSED before
647 *
648 * note that (1) is conservative, we start with the whole visible region
649 * but only keep what used to be covered by something -- which mean it
650 * may have been exposed.
651 *
652 * (2) handles areas that were not covered by anything but got exposed
653 * because of a resize.
654 *
655 */
656 const Region newExposed = visibleRegion - coveredRegion;
657 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
658 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
659 }
660 dirty.subtractSelf(coverage.aboveOpaqueLayers);
661
662 // accumulate to the screen dirty region
663 coverage.dirtyRegion.orSelf(dirty);
664
665 // Update accumAboveOpaqueLayers for next (lower) layer
666 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
667
668 // Compute the visible non-transparent region
669 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
670
Vishnu Naira483b4a2019-12-12 15:07:52 -0800671 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800672 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700673 const auto& outputState = getState();
674 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000675 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800676 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700677 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800678 }
679
Vishnu Naira483b4a2019-12-12 15:07:52 -0800680 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
681
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800682 // The layer is visible. Either reuse the existing outputLayer if we have
683 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800684 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800685
686 // Store the layer coverage information into the layer state as some of it
687 // is useful later.
688 auto& outputLayerState = result->editState();
689 outputLayerState.visibleRegion = visibleRegion;
690 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
691 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200692 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000693 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800694 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500695 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400696 layerFEState->compositionType == Composition::DISPLAY_DECORATION
697 ? outputState.transform.transform(
698 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
699 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800700}
701
702void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
703 // The base class does nothing with this call.
704}
705
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800706void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700707 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800708 layer->getLayerFE().prepareCompositionState(
709 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
710 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800711 }
712}
713
Dan Stoza269dc4d2021-01-15 15:07:43 -0800714void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800715 ATRACE_CALL();
716 ALOGV(__FUNCTION__);
717
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800718 if (!getState().isEnabled) {
719 return;
720 }
721
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800722 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
723 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
724
Lloyd Pique01c77c12019-04-17 12:48:32 -0700725 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700726 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800727 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200728 forceClientComposition,
729 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800730
731 if (mLayerRequestingBackgroundBlur == layer) {
732 forceClientComposition = false;
733 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800734 }
735}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800736
Dan Stoza269dc4d2021-01-15 15:07:43 -0800737void Output::planComposition() {
738 if (!mPlanner || !getState().isEnabled) {
739 return;
740 }
741
742 ATRACE_CALL();
743 ALOGV(__FUNCTION__);
744
745 mPlanner->plan(getOutputLayersOrderedByZ());
746}
747
748void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
749 ATRACE_CALL();
750 ALOGV(__FUNCTION__);
751
752 if (!getState().isEnabled) {
753 return;
754 }
755
Ady Abraham3645e642021-04-20 18:39:00 -0700756 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700757 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800758 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700759
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400760 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800761 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400762 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400763 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400764 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800765 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400766 if (layer == peekThroughLayer) {
767 // No longer needed, although it should not show up again, so
768 // resetting it is not truly needed either.
769 peekThroughLayer = nullptr;
770
771 // peekThroughLayer was already drawn ahead of its z order.
772 continue;
773 }
Dan Stoza6166c312021-01-15 16:34:05 -0800774 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400775 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400776 if (overrideInfo.buffer != nullptr) {
777 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800778 ALOGV("Skipping redundant buffer");
779 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400780 } else {
781 // First layer with the override buffer.
782 if (overrideInfo.peekThroughLayer) {
783 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400784
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400785 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400786 overrideZ = true;
787 includeGeometry = true;
788 constexpr bool isPeekingThrough = true;
789 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
790 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400791 }
792
793 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800794 }
Dan Stoza6166c312021-01-15 16:34:05 -0800795 }
796
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400797 constexpr bool isPeekingThrough = false;
798 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800799 }
800}
801
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800802compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
803 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
804 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100805 auto* compState = layer->getLayerFE().getCompositionState();
806
807 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
808 // want to force client composition because of the blur.
809 if (compState->sidebandStream != nullptr) {
810 return nullptr;
811 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000812 if (compState->isOpaque) {
813 continue;
814 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100815 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800816 layerRequestingBgComposition = layer;
817 }
818 }
819 return layerRequestingBgComposition;
820}
821
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800822void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
823 setColorProfile(pickColorProfile(refreshArgs));
824}
825
826// Returns a data space that fits all visible layers. The returned data space
827// can only be one of
828// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
829// - Dataspace::DISPLAY_P3
830// - Dataspace::DISPLAY_BT2020
831// The returned HDR data space is one of
832// - Dataspace::UNKNOWN
833// - Dataspace::BT2020_HLG
834// - Dataspace::BT2020_PQ
835ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
836 bool* outIsHdrClientComposition) const {
837 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
838 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
839
Lloyd Pique01c77c12019-04-17 12:48:32 -0700840 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800841 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800842 case ui::Dataspace::V0_SCRGB:
843 case ui::Dataspace::V0_SCRGB_LINEAR:
844 case ui::Dataspace::BT2020:
845 case ui::Dataspace::BT2020_ITU:
846 case ui::Dataspace::BT2020_LINEAR:
847 case ui::Dataspace::DISPLAY_BT2020:
848 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
849 break;
850 case ui::Dataspace::DISPLAY_P3:
851 bestDataSpace = ui::Dataspace::DISPLAY_P3;
852 break;
853 case ui::Dataspace::BT2020_PQ:
854 case ui::Dataspace::BT2020_ITU_PQ:
855 bestDataSpace = ui::Dataspace::DISPLAY_P3;
856 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800857 *outIsHdrClientComposition =
858 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800859 break;
860 case ui::Dataspace::BT2020_HLG:
861 case ui::Dataspace::BT2020_ITU_HLG:
862 bestDataSpace = ui::Dataspace::DISPLAY_P3;
863 // When there's mixed PQ content and HLG content, we set the HDR
864 // data space to be BT2020_PQ and convert HLG to PQ.
865 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
866 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
867 }
868 break;
869 default:
870 break;
871 }
872 }
873
874 return bestDataSpace;
875}
876
877compositionengine::Output::ColorProfile Output::pickColorProfile(
878 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
879 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
880 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
881 ui::RenderIntent::COLORIMETRIC,
882 refreshArgs.colorSpaceAgnosticDataspace};
883 }
884
885 ui::Dataspace hdrDataSpace;
886 bool isHdrClientComposition = false;
887 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
888
889 switch (refreshArgs.forceOutputColorMode) {
890 case ui::ColorMode::SRGB:
891 bestDataSpace = ui::Dataspace::V0_SRGB;
892 break;
893 case ui::ColorMode::DISPLAY_P3:
894 bestDataSpace = ui::Dataspace::DISPLAY_P3;
895 break;
896 default:
897 break;
898 }
899
900 // respect hdrDataSpace only when there is no legacy HDR support
901 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
902 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
903 if (isHdr) {
904 bestDataSpace = hdrDataSpace;
905 }
906
907 ui::RenderIntent intent;
908 switch (refreshArgs.outputColorSetting) {
909 case OutputColorSetting::kManaged:
910 case OutputColorSetting::kUnmanaged:
911 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
912 : ui::RenderIntent::COLORIMETRIC;
913 break;
914 case OutputColorSetting::kEnhanced:
915 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
916 break;
917 default: // vendor display color setting
918 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
919 break;
920 }
921
922 ui::ColorMode outMode;
923 ui::Dataspace outDataSpace;
924 ui::RenderIntent outRenderIntent;
925 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
926 &outRenderIntent);
927
928 return ColorProfile{outMode, outDataSpace, outRenderIntent,
929 refreshArgs.colorSpaceAgnosticDataspace};
930}
931
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800932void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700933 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700934 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700935 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700936 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800937
938 // If nothing has changed (!dirty), don't recompose.
939 // If something changed, but we don't currently have any visible layers,
940 // and didn't when we last did a composition, then skip it this time.
941 // The second rule does two things:
942 // - When all layers are removed from a display, we'll emit one black
943 // frame, then nothing more until we get new layers.
944 // - When a display is created with a private layer stack, we won't
945 // emit any black frames until a layer is added to the layer stack.
946 const bool mustRecompose = dirty && !(empty && wasEmpty);
947
948 const char flagPrefix[] = {'-', '+'};
949 static_cast<void>(flagPrefix);
950 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
951 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
952 flagPrefix[empty], flagPrefix[wasEmpty]);
953
954 mRenderSurface->beginFrame(mustRecompose);
955
956 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700957 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800958 }
959}
960
Lloyd Pique66d68602019-02-13 14:23:31 -0800961void Output::prepareFrame() {
962 ATRACE_CALL();
963 ALOGV(__FUNCTION__);
964
Vishnu Naira3140382022-02-24 14:07:11 -0800965 auto& outputState = editState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700966 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800967 return;
968 }
969
Vishnu Naira3140382022-02-24 14:07:11 -0800970 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
971 bool success = chooseCompositionStrategy(&changes);
972 resetCompositionStrategy();
973 outputState.previousDeviceRequestedChanges = changes;
974 outputState.previousDeviceRequestedSuccess = success;
975 if (success) {
976 applyCompositionStrategy(changes);
977 }
978 finishPrepareFrame();
979}
Lloyd Pique66d68602019-02-13 14:23:31 -0800980
Vishnu Naira3140382022-02-24 14:07:11 -0800981std::future<bool> Output::chooseCompositionStrategyAsync(
982 std::optional<android::HWComposer::DeviceRequestedChanges>* changes) {
983 return mHwComposerAsyncWorker->send(
984 [&, changes]() { return chooseCompositionStrategy(changes); });
985}
986
987GpuCompositionResult Output::prepareFrameAsync(const CompositionRefreshArgs& refreshArgs) {
988 ATRACE_CALL();
989 ALOGV(__FUNCTION__);
990 auto& state = editState();
991 const auto& previousChanges = state.previousDeviceRequestedChanges;
992 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
993 resetCompositionStrategy();
994 auto hwcResult = chooseCompositionStrategyAsync(&changes);
995 if (state.previousDeviceRequestedSuccess) {
996 applyCompositionStrategy(previousChanges);
997 }
998 finishPrepareFrame();
999
1000 base::unique_fd bufferFence;
1001 std::shared_ptr<renderengine::ExternalTexture> buffer;
1002 updateProtectedContentState();
1003 const bool dequeueSucceeded = dequeueRenderBuffer(&bufferFence, &buffer);
1004 GpuCompositionResult compositionResult;
1005 if (dequeueSucceeded) {
1006 std::optional<base::unique_fd> optFd =
1007 composeSurfaces(Region::INVALID_REGION, refreshArgs, buffer, bufferFence);
1008 if (optFd) {
1009 compositionResult.fence = std::move(*optFd);
1010 }
Dan Stoza47437bb2021-01-15 16:21:07 -08001011 }
1012
Vishnu Naira3140382022-02-24 14:07:11 -08001013 auto chooseCompositionSuccess = hwcResult.get();
1014 const bool predictionSucceeded = dequeueSucceeded && changes == previousChanges;
1015 compositionResult.succeeded = predictionSucceeded;
1016 if (!predictionSucceeded) {
1017 ATRACE_NAME("CompositionStrategyPredictionMiss");
1018 resetCompositionStrategy();
1019 if (chooseCompositionSuccess) {
1020 applyCompositionStrategy(changes);
1021 }
1022 finishPrepareFrame();
1023 // Track the dequeued buffer to reuse so we don't need to dequeue another one.
1024 compositionResult.buffer = buffer;
1025 } else {
1026 ATRACE_NAME("CompositionStrategyPredictionHit");
1027 }
1028 state.previousDeviceRequestedChanges = std::move(changes);
1029 state.previousDeviceRequestedSuccess = chooseCompositionSuccess;
1030 return compositionResult;
Lloyd Pique66d68602019-02-13 14:23:31 -08001031}
1032
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001033void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
1034 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
1035 return;
1036 }
1037
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001038 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001039 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Vishnu Naira3140382022-02-24 14:07:11 -08001040 base::unique_fd bufferFence;
1041 std::shared_ptr<renderengine::ExternalTexture> buffer;
1042 updateProtectedContentState();
1043 dequeueRenderBuffer(&bufferFence, &buffer);
1044 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs, buffer, bufferFence));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001045 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -08001046 }
1047 }
1048
1049 postFramebuffer();
1050
1051 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
1052
1053 prepareFrame();
1054}
1055
Vishnu Naira3140382022-02-24 14:07:11 -08001056void Output::finishFrame(const CompositionRefreshArgs& refreshArgs, GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001057 ATRACE_CALL();
1058 ALOGV(__FUNCTION__);
Robert Carra00eb142022-03-09 13:49:30 -08001059
1060 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -08001061 return;
1062 }
1063
Vishnu Naira3140382022-02-24 14:07:11 -08001064 std::optional<base::unique_fd> optReadyFence;
1065 std::shared_ptr<renderengine::ExternalTexture> buffer;
1066 base::unique_fd bufferFence;
1067 if (result.succeeded) {
1068 optReadyFence = std::move(result.fence);
1069 } else {
1070 if (result.bufferAvailable()) {
1071 buffer = std::move(result.buffer);
1072 bufferFence = std::move(result.fence);
1073 } else {
1074 updateProtectedContentState();
1075 if (!dequeueRenderBuffer(&bufferFence, &buffer)) {
1076 return;
1077 }
1078 }
1079 // Repaint the framebuffer (if needed), getting the optional fence for when
1080 // the composition completes.
1081 optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs, buffer, bufferFence);
1082 }
Lloyd Piqued3d69882019-02-28 16:03:46 -08001083 if (!optReadyFence) {
1084 return;
1085 }
1086
1087 // swap buffers (presentation)
1088 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1089}
1090
Vishnu Naira3140382022-02-24 14:07:11 -08001091void Output::updateProtectedContentState() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001092 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001093 auto& renderEngine = getCompositionEngine().getRenderEngine();
1094 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1095
1096 // If we the display is secure, protected content support is enabled, and at
1097 // least one layer has protected content, we need to use a secure back
1098 // buffer.
1099 if (outputState.isSecure && supportsProtectedContent) {
1100 auto layers = getOutputLayersOrderedByZ();
1101 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1102 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1103 });
1104 if (needsProtected != renderEngine.isProtected()) {
1105 renderEngine.useProtectedContext(needsProtected);
1106 }
1107 if (needsProtected != mRenderSurface->isProtected() &&
1108 needsProtected == renderEngine.isProtected()) {
1109 mRenderSurface->setProtected(needsProtected);
1110 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001111 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1112 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001113 }
Vishnu Naira3140382022-02-24 14:07:11 -08001114}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001115
Vishnu Naira3140382022-02-24 14:07:11 -08001116bool Output::dequeueRenderBuffer(base::unique_fd* bufferFence,
1117 std::shared_ptr<renderengine::ExternalTexture>* tex) {
1118 const auto& outputState = getState();
Lloyd Piquee9eff972020-05-05 12:36:44 -07001119
1120 // If we aren't doing client composition on this output, but do have a
1121 // flipClientTarget request for this frame on this output, we still need to
1122 // dequeue a buffer.
Vishnu Naira3140382022-02-24 14:07:11 -08001123 if (outputState.usesClientComposition || outputState.flipClientTarget) {
1124 *tex = mRenderSurface->dequeueBuffer(bufferFence);
1125 if (*tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001126 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1127 "client composition for this frame",
1128 mName.c_str());
Vishnu Naira3140382022-02-24 14:07:11 -08001129 return false;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001130 }
1131 }
Vishnu Naira3140382022-02-24 14:07:11 -08001132 return true;
1133}
Lloyd Piquee9eff972020-05-05 12:36:44 -07001134
Vishnu Naira3140382022-02-24 14:07:11 -08001135std::optional<base::unique_fd> Output::composeSurfaces(
1136 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs,
1137 std::shared_ptr<renderengine::ExternalTexture> tex, base::unique_fd& fd) {
1138 ATRACE_CALL();
1139 ALOGV(__FUNCTION__);
1140
1141 const auto& outputState = getState();
1142 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
1143 outputState.usesClientComposition};
Lloyd Pique688abd42019-02-15 15:42:24 -08001144 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001145 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001146 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001147 }
1148
Vishnu Naira3140382022-02-24 14:07:11 -08001149 if (tex == nullptr) {
1150 ALOGW("Buffer not valid for display [%s], bailing out of "
1151 "client composition for this frame",
1152 mName.c_str());
1153 return {};
1154 }
1155
Lloyd Pique688abd42019-02-15 15:42:24 -08001156 ALOGV("hasClientComposition");
1157
Lloyd Pique688abd42019-02-15 15:42:24 -08001158 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001159 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1160 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001161 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001162 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001163 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1164 ? outputState.dataspace
1165 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001166
1167 // If we have a valid current display brightness use that, otherwise fall back to the
1168 // display's max desired
Alec Mourib21d94e2022-01-13 17:44:10 -08001169 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
John Reckac09e452021-04-07 16:35:37 -04001170 ? outputState.displayBrightnessNits
1171 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourib21d94e2022-01-13 17:44:10 -08001172 clientCompositionDisplay.maxLuminance =
1173 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif8d093d2022-02-10 15:16:59 -08001174 clientCompositionDisplay.targetLuminanceNits =
1175 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
Alec Mouri85065692022-03-18 00:58:26 +00001176 clientCompositionDisplay.dimmingStage = outputState.clientTargetDimmingStage;
Lloyd Pique688abd42019-02-15 15:42:24 -08001177
1178 // Compute the global color transform matrix.
Leon Scroggins III745dcaa2022-01-26 11:55:58 -05001179 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1180 clientCompositionDisplay.deviceHandlesColorTransform =
1181 outputState.usesDeviceComposition || getSkipColorTransform();
Lloyd Pique688abd42019-02-15 15:42:24 -08001182
Lloyd Pique688abd42019-02-15 15:42:24 -08001183 // Generate the client composition requests for the layers on this output.
Vishnu Naira3140382022-02-24 14:07:11 -08001184 auto& renderEngine = getCompositionEngine().getRenderEngine();
1185 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
Robert Carrccab4242021-09-28 16:53:03 -07001186 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001187 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001188 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001189 clientCompositionDisplay.outputDataspace,
1190 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1192
Vishnu Naira3140382022-02-24 14:07:11 -08001193 OutputCompositionState& outputCompositionState = editState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001194 // Check if the client composition requests were rendered into the provided graphic buffer. If
1195 // so, we can reuse the buffer and avoid client composition.
1196 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001197 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1198 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001199 clientCompositionLayers)) {
Vishnu Naira3140382022-02-24 14:07:11 -08001200 ATRACE_NAME("ClientCompositionCacheHit");
Vishnu Nair9b079a22020-01-21 14:36:08 -08001201 outputCompositionState.reusedClientComposition = true;
1202 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001203 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001204 }
Vishnu Naira3140382022-02-24 14:07:11 -08001205 ATRACE_NAME("ClientCompositionCacheMiss");
Alec Mouria90a5702021-04-16 16:36:21 +00001206 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001207 clientCompositionLayers);
1208 }
1209
Lloyd Pique688abd42019-02-15 15:42:24 -08001210 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001211 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1212 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1213 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001214 const bool expensiveBlurs =
1215 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001216 const bool expensiveRenderingExpected = expensiveBlurs ||
1217 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1218 [outputDataspace =
1219 clientCompositionDisplay.outputDataspace](const auto& layer) {
1220 return layer.sourceDataspace != outputDataspace;
1221 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001222 if (expensiveRenderingExpected) {
1223 setExpensiveRenderingExpected(true);
1224 }
1225
Sally Qi59a9f502021-10-12 18:53:23 +00001226 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1227 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001228 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001229 std::back_inserter(clientRenderEngineLayers),
1230 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1231 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001232 });
1233
Alec Mourie4034bb2019-11-19 12:45:54 -08001234 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001235 // Only use the framebuffer cache when rendering to an internal display
1236 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1237 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1238 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1239 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1240 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001241 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001242 auto [status, drawFence] =
1243 renderEngine
Sally Qi59a9f502021-10-12 18:53:23 +00001244 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Sally Qi4cabdd02021-08-05 16:45:57 -07001245 useFramebufferCache, std::move(fd))
1246 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001247
1248 if (status != NO_ERROR && mClientCompositionRequestCache) {
1249 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001250 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001251 }
1252
Alec Mourie4034bb2019-11-19 12:45:54 -08001253 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001254 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001255 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1256 } else {
1257 timeStats.recordRenderEngineDuration(renderEngineStart,
1258 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001259 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001260 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001261
Robert Carrccab4242021-09-28 16:53:03 -07001262 if (clientCompositionLayersFE.size() > 0) {
1263 sp<Fence> clientCompFence = new Fence(dup(drawFence.get()));
1264 for (auto clientComposedLayer : clientCompositionLayersFE) {
1265 clientComposedLayer->setWasClientComposed(clientCompFence);
1266 }
1267 }
1268
Sally Qi4cabdd02021-08-05 16:45:57 -07001269 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001270}
1271
Vishnu Nair9b079a22020-01-21 14:36:08 -08001272std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001273 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001274 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001275 ALOGV("Rendering client layers");
1276
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001277 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001278 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001279 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001280
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001281 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001282 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001283
Lloyd Pique01c77c12019-04-17 12:48:32 -07001284 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001285 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001286 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001287 auto& layerFE = layer->getLayerFE();
1288
Lloyd Piquea2468662019-03-07 21:31:06 -08001289 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001290 ALOGV("Layer: %s", layerFE.getDebugName());
1291 if (clip.isEmpty()) {
1292 ALOGV(" Skipping for empty clip");
1293 firstLayer = false;
1294 continue;
1295 }
1296
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001297 disableBlurs |= layerFEState->sidebandStream != nullptr;
1298
Vishnu Naira483b4a2019-12-12 15:07:52 -08001299 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001300
1301 // We clear the client target for non-client composed layers if
1302 // requested by the HWC. We skip this if the layer is not an opaque
1303 // rectangle, as by definition the layer must blend with whatever is
1304 // underneath. We also skip the first layer as the buffer target is
1305 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001306 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001307 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001308
1309 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1310
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001311 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1312 // composing the non-shadow content and only draw the shadows.
1313 const bool realContentIsVisible = clientComposition &&
1314 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1315
Lloyd Pique688abd42019-02-15 15:42:24 -08001316 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001317 std::vector<LayerFE::LayerSettings> results;
1318 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001319 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001320 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001321 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001322 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1323 } else {
1324 ALOGV("Skipping redundant override buffer for [%s] in RE",
1325 layer->getLayerFE().getDebugName());
1326 }
Dan Stoza6166c312021-01-15 16:34:05 -08001327 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001328 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1329 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1330 : (layer->getState().overrideInfo.disableBackgroundBlur
1331 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1332 BlurRegionsOnly
1333 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1334 Enabled);
1335 compositionengine::LayerFE::ClientCompositionTargetSettings
1336 targetSettings{.clip = clip,
1337 .needsFiltering = layer->needsFiltering() ||
1338 outputState.needsFiltering,
1339 .isSecure = outputState.isSecure,
1340 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001341 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001342 .dataspace = outputDataspace,
1343 .realContentIsVisible = realContentIsVisible,
1344 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001345 .blurSetting = blurSetting,
1346 .whitePointNits = layerState.whitePointNits};
Dan Stoza6166c312021-01-15 16:34:05 -08001347 results = layerFE.prepareClientCompositionList(targetSettings);
1348 if (realContentIsVisible && !results.empty()) {
1349 layer->editState().clientCompositionTimestamp = systemTime();
1350 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001351 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001352
Robert Carrccab4242021-09-28 16:53:03 -07001353 outLayerFEs.push_back(&layerFE);
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001354 clientCompositionLayers.insert(clientCompositionLayers.end(),
1355 std::make_move_iterator(results.begin()),
1356 std::make_move_iterator(results.end()));
1357 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001358 }
1359
1360 firstLayer = false;
1361 }
1362
1363 return clientCompositionLayers;
1364}
1365
1366void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001367 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001368 if (flashRegion.isEmpty()) {
1369 return;
1370 }
1371
Vishnu Nair9b079a22020-01-21 14:36:08 -08001372 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001373 layerSettings.source.buffer.buffer = nullptr;
1374 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1375 layerSettings.alpha = half(1.0);
1376
1377 for (const auto& rect : flashRegion) {
1378 layerSettings.geometry.boundaries = rect.toFloatRect();
1379 clientCompositionLayers.push_back(layerSettings);
1380 }
1381}
1382
1383void Output::setExpensiveRenderingExpected(bool) {
1384 // The base class does nothing with this call.
1385}
1386
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001387void Output::postFramebuffer() {
1388 ATRACE_CALL();
1389 ALOGV(__FUNCTION__);
1390
1391 if (!getState().isEnabled) {
1392 return;
1393 }
1394
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001395 auto& outputState = editState();
1396 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001397 mRenderSurface->flip();
1398
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001399 auto frame = presentAndGetFrameFences();
1400
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001401 mRenderSurface->onPresentDisplayCompleted();
1402
Lloyd Pique01c77c12019-04-17 12:48:32 -07001403 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001404 // The layer buffer from the previous frame (if any) is released
1405 // by HWC only when the release fence from this frame (if any) is
1406 // signaled. Always get the release fence from HWC first.
1407 sp<Fence> releaseFence = Fence::NO_FENCE;
1408
1409 if (auto hwcLayer = layer->getHwcLayer()) {
1410 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1411 releaseFence = f->second;
1412 }
1413 }
1414
1415 // If the layer was client composited in the previous frame, we
1416 // need to merge with the previous client target acquire fence.
1417 // Since we do not track that, always merge with the current
1418 // client target acquire fence when it is available, even though
1419 // this is suboptimal.
1420 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001421 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001422 releaseFence =
1423 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1424 }
Sally Qi59a9f502021-10-12 18:53:23 +00001425 layer->getLayerFE().onLayerDisplayed(
1426 ftl::yield<renderengine::RenderEngineResult>(
1427 {NO_ERROR, base::unique_fd(releaseFence->dup())})
1428 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001429 }
1430
1431 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001432 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001433 // supply them with the present fence.
1434 for (auto& weakLayer : mReleasedLayers) {
1435 if (auto layer = weakLayer.promote(); layer != nullptr) {
Sally Qi59a9f502021-10-12 18:53:23 +00001436 layer->onLayerDisplayed(ftl::yield<renderengine::RenderEngineResult>(
1437 {NO_ERROR, base::unique_fd(frame.presentFence->dup())})
1438 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001439 }
1440 }
1441
1442 // Clear out the released layers now that we're done with them.
1443 mReleasedLayers.clear();
1444}
1445
Alec Mouriaa831582021-06-07 16:23:01 -07001446void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001447 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001448 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001449 }
1450}
1451
Lloyd Pique32cbe282018-10-19 13:09:22 -07001452void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001453 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001454 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001455}
1456
Vishnu Naira3140382022-02-24 14:07:11 -08001457void Output::resetCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001458 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001459 auto& outputState = editState();
1460 outputState.usesClientComposition = true;
1461 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001462 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001463}
1464
Lloyd Pique688abd42019-02-15 15:42:24 -08001465bool Output::getSkipColorTransform() const {
1466 return true;
1467}
1468
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001469compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1470 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001471 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001472 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1473 }
1474 return result;
1475}
1476
Vishnu Naira3140382022-02-24 14:07:11 -08001477void Output::setPredictCompositionStrategy(bool predict) {
1478 if (predict) {
1479 mHwComposerAsyncWorker = std::make_unique<HwcAsyncWorker>();
1480 } else {
1481 mHwComposerAsyncWorker.reset(nullptr);
1482 }
1483}
1484
1485bool Output::canPredictCompositionStrategy(const CompositionRefreshArgs& refreshArgs) {
1486 if (!getState().isEnabled || !mHwComposerAsyncWorker) {
1487 ALOGV("canPredictCompositionStrategy disabled");
1488 return false;
1489 }
1490
1491 if (!getState().previousDeviceRequestedChanges) {
1492 ALOGV("canPredictCompositionStrategy previous changes not available");
1493 return false;
1494 }
1495
1496 if (!mRenderSurface->supportsCompositionStrategyPrediction()) {
1497 ALOGV("canPredictCompositionStrategy surface does not support");
1498 return false;
1499 }
1500
1501 if (refreshArgs.devOptFlashDirtyRegionsDelay) {
1502 ALOGV("canPredictCompositionStrategy devOptFlashDirtyRegionsDelay");
1503 return false;
1504 }
1505
1506 // If no layer uses clientComposition, then don't predict composition strategy
1507 // because we have less work to do in parallel.
1508 if (!anyLayersRequireClientComposition()) {
1509 ALOGV("canPredictCompositionStrategy no layer uses clientComposition");
1510 return false;
1511 }
1512
1513 if (!refreshArgs.updatingOutputGeometryThisFrame) {
1514 return true;
1515 }
1516
1517 ALOGV("canPredictCompositionStrategy updatingOutputGeometryThisFrame");
1518 return false;
1519}
1520
1521bool Output::anyLayersRequireClientComposition() const {
1522 const auto layers = getOutputLayersOrderedByZ();
1523 return std::any_of(layers.begin(), layers.end(),
1524 [](const auto& layer) { return layer->requiresClientComposition(); });
1525}
1526
1527void Output::finishPrepareFrame() {
1528 const auto& state = getState();
1529 if (mPlanner) {
1530 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
1531 }
1532 mRenderSurface->prepareFrame(state.usesClientComposition, state.usesDeviceComposition);
1533}
1534
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001535} // namespace impl
1536} // namespace android::compositionengine