blob: 4e67a63c40aed1601747453d558295e569b4930a [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>
Lloyd Pique32cbe282018-10-19 13:09:22 -070025#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070026#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080027#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070028#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080029#include <compositionengine/impl/planner/Planner.h>
Sally Qi59a9f502021-10-12 18:53:23 +000030#include <ftl/future.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080031
Alec Mouria90a5702021-04-16 16:36:21 +000032#include <thread>
33
34#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080035
36// TODO(b/129481165): remove the #pragma below and fix conversion issues
37#pragma clang diagnostic push
38#pragma clang diagnostic ignored "-Wconversion"
39
Lloyd Pique688abd42019-02-15 15:42:24 -080040#include <renderengine/DisplaySettings.h>
41#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080042
43// TODO(b/129481165): remove the #pragma below and fix conversion issues
44#pragma clang diagnostic pop // ignored "-Wconversion"
45
Dan Stoza269dc4d2021-01-15 15:07:43 -080046#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070047#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080048#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080049#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070050
Lloyd Pique688abd42019-02-15 15:42:24 -080051#include "TracedOrdinal.h"
52
Leon Scroggins III9a0afda2022-01-11 16:53:09 -050053using aidl::android::hardware::graphics::composer3::Composition;
54
Lloyd Piquefeb73d72018-12-04 17:23:44 -080055namespace android::compositionengine {
56
57Output::~Output() = default;
58
59namespace impl {
Robert Carra00eb142022-03-09 13:49:30 -080060
Lloyd Piquec29e4c62019-03-07 21:48:19 -080061namespace {
62
63template <typename T>
64class Reversed {
65public:
66 explicit Reversed(const T& container) : mContainer(container) {}
67 auto begin() { return mContainer.rbegin(); }
68 auto end() { return mContainer.rend(); }
69
70private:
71 const T& mContainer;
72};
73
74// Helper for enumerating over a container in reverse order
75template <typename T>
76Reversed<T> reversed(const T& c) {
77 return Reversed<T>(c);
78}
79
Marin Shalamanovb15d2272020-09-17 21:41:52 +020080struct ScaleVector {
81 float x;
82 float y;
83};
84
85// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
86// where to' will have the same size as "to". In the case where "from" and "to"
87// start at the origin to'=to.
88ScaleVector getScale(const Rect& from, const Rect& to) {
89 return {.x = static_cast<float>(to.width()) / from.width(),
90 .y = static_cast<float>(to.height()) / from.height()};
91}
92
Lloyd Piquec29e4c62019-03-07 21:48:19 -080093} // namespace
94
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070095std::shared_ptr<Output> createOutput(
96 const compositionengine::CompositionEngine& compositionEngine) {
97 return createOutputTemplated<Output>(compositionEngine);
98}
Lloyd Pique32cbe282018-10-19 13:09:22 -070099
100Output::~Output() = default;
101
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700103 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
104 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105}
106
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700107std::optional<DisplayId> Output::getDisplayId() const {
108 return {};
109}
110
Lloyd Pique32cbe282018-10-19 13:09:22 -0700111const std::string& Output::getName() const {
112 return mName;
113}
114
115void Output::setName(const std::string& name) {
116 mName = name;
117}
118
119void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700120 auto& outputState = editState();
121 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700122 return;
123 }
124
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700125 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700126 dirtyEntireOutput();
127}
128
Alec Mouri023c1882021-05-08 16:36:33 -0700129void Output::setLayerCachingEnabled(bool enabled) {
130 if (enabled == (mPlanner != nullptr)) {
131 return;
132 }
133
134 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700135 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700136 if (mRenderSurface) {
137 mPlanner->setDisplaySize(mRenderSurface->getSize());
138 }
139 } else {
140 mPlanner.reset();
141 }
Alec Mouric773472b2021-05-19 14:29:05 -0700142
143 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
144 if (!outputLayer) {
145 continue;
146 }
147
148 outputLayer->editState().overrideInfo = {};
149 }
Alec Mouri023c1882021-05-08 16:36:33 -0700150}
151
Ady Abrahamdb036a82021-07-16 14:18:34 -0700152void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
153 if (mPlanner) {
154 mPlanner->setTexturePoolEnabled(enabled);
155 }
156}
157
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200158void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
159 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700160 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200161
Angel Aguayob084e0c2021-08-04 23:27:28 +0000162 outputState.displaySpace.setOrientation(orientation);
163 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200164 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200165
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200166 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000167 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200168 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200169 std::swap(orientedSize.width, orientedSize.height);
170 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000171 outputState.orientedDisplaySpace.setBounds(orientedSize);
172 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200173
174 // Compute displaySpace.content
175 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
176 ui::Transform rotation;
177 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000178 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200179 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
180 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000181 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200182
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200183 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000184 outputState.framebufferSpace.setOrientation(orientation);
185 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200186 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000187 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
188 outputState.framebufferSpace.getBoundsAsRect());
189 outputState.framebufferSpace.setContent(
190 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191
192 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000193 outputState.layerStackSpace.setContent(layerStackSpaceRect);
194 outputState.layerStackSpace.setBounds(
195 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200196
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200197 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
198 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700199 dirtyEntireOutput();
200}
201
Alec Mouricdf16792021-12-10 13:16:06 -0800202void Output::setNextBrightness(float brightness) {
203 editState().displayBrightness = brightness;
204}
205
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200206void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700207 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200208
209 auto& state = editState();
210
211 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000212 const ui::Size newBounds(size);
213 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200214
215 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000216 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200217 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
218
219 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000220 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200221 ui::Size orientedSize = size;
222 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
223 std::swap(orientedSize.width, orientedSize.height);
224 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000225 const ui::Size newOrientedBounds(orientedSize);
226 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700227
Dan Stoza6166c312021-01-15 16:34:05 -0800228 if (mPlanner) {
229 mPlanner->setDisplaySize(size);
230 }
231
Lloyd Pique32cbe282018-10-19 13:09:22 -0700232 dirtyEntireOutput();
233}
234
Garfield Tan54edd912020-10-21 16:31:41 -0700235ui::Transform::RotationFlags Output::getTransformHint() const {
236 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
237}
238
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700239void Output::setLayerFilter(ui::LayerFilter filter) {
240 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700241 dirtyEntireOutput();
242}
243
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800244void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700245 auto& colorTransformMatrix = editState().colorTransformMatrix;
246 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700247 return;
248 }
249
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700250 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800251
252 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700253}
254
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800255void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700256 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800257 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
258 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800259
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700260 auto& outputState = editState();
261 if (outputState.colorMode == colorProfile.mode &&
262 outputState.dataspace == colorProfile.dataspace &&
263 outputState.renderIntent == colorProfile.renderIntent &&
264 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800265 return;
266 }
267
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700268 outputState.colorMode = colorProfile.mode;
269 outputState.dataspace = colorProfile.dataspace;
270 outputState.renderIntent = colorProfile.renderIntent;
271 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700272
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800273 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700274
Lloyd Pique32cbe282018-10-19 13:09:22 -0700275 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800276 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
277 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800278
279 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700280}
281
John Reckac09e452021-04-07 16:35:37 -0400282void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
283 auto& outputState = editState();
284 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
285 outputState.displayBrightnessNits == displayBrightnessNits) {
286 // Nothing changed
287 return;
288 }
289 outputState.sdrWhitePointNits = sdrWhitePointNits;
290 outputState.displayBrightnessNits = displayBrightnessNits;
291 dirtyEntireOutput();
292}
293
Lloyd Pique32cbe282018-10-19 13:09:22 -0700294void Output::dump(std::string& out) const {
295 using android::base::StringAppendF;
296
297 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
298
299 out.append("\n ");
300
301 dumpBase(out);
302}
303
304void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700305 dumpState(out);
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
Lloyd Pique31cb2942018-10-19 17:23:03 -0700313 if (mRenderSurface) {
314 mRenderSurface->dump(out);
315 } else {
316 out.append(" No render surface!\n");
317 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800318
Lloyd Pique01c77c12019-04-17 12:48:32 -0700319 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
320 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800321 if (!outputLayer) {
322 continue;
323 }
324 outputLayer->dump(out);
325 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700326}
327
Dan Stoza269dc4d2021-01-15 15:07:43 -0800328void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
329 if (!mPlanner) {
330 base::StringAppendF(&out, "Planner is disabled\n");
331 return;
332 }
333 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
334 mPlanner->dump(args, out);
335}
336
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700337compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
338 return mDisplayColorProfile.get();
339}
340
341void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
342 mDisplayColorProfile = std::move(mode);
343}
344
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800345const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
346 return mReleasedLayers;
347}
348
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700349void Output::setDisplayColorProfileForTest(
350 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
351 mDisplayColorProfile = std::move(mode);
352}
353
Lloyd Pique31cb2942018-10-19 17:23:03 -0700354compositionengine::RenderSurface* Output::getRenderSurface() const {
355 return mRenderSurface.get();
356}
357
358void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
359 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800360 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000361 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800362 if (mPlanner) {
363 mPlanner->setDisplaySize(size);
364 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700365 dirtyEntireOutput();
366}
367
Vishnu Nair9b079a22020-01-21 14:36:08 -0800368void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
369 if (cacheSize == 0) {
370 mClientCompositionRequestCache.reset();
371 } else {
372 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
373 }
374};
375
Lloyd Pique31cb2942018-10-19 17:23:03 -0700376void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
377 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700378}
379
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700380Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700381 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000382 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700383}
384
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700385bool Output::includesLayer(ui::LayerFilter filter) const {
386 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700387}
388
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700389bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800390 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700391 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800392}
393
Lloyd Piquedf336d92019-03-07 21:38:42 -0800394std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800395 const sp<LayerFE>& layerFE) const {
396 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800397}
398
Lloyd Piquede196652020-01-22 17:29:58 -0800399compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
400 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700401 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800402}
403
Lloyd Pique01c77c12019-04-17 12:48:32 -0700404std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800405 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700406 for (size_t i = 0; i < getOutputLayerCount(); i++) {
407 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800408 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700409 return i;
410 }
411 }
412 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800413}
414
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800415void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
416 mReleasedLayers = std::move(layers);
417}
418
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800419void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
420 LayerFESet& geomSnapshots) {
421 ATRACE_CALL();
422 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800423
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800424 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800425}
426
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800427void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800428 ATRACE_CALL();
429 ALOGV(__FUNCTION__);
430
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800431 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800432 updateCompositionState(refreshArgs);
433 planComposition();
434 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800435 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800436 beginFrame();
Robert Carr6fe2bef2022-03-09 13:49:41 -0800437 prepareFrame();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800438 devOptRepaintFlash(refreshArgs);
Robert Carr6fe2bef2022-03-09 13:49:41 -0800439 finishFrame(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800440 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700441 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800442}
443
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800444void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
445 LayerFESet& layerFESet) {
446 ATRACE_CALL();
447 ALOGV(__FUNCTION__);
448
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700449 auto& outputState = editState();
450
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800451 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700452 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800453 return;
454 }
455
456 // Process the layers to determine visibility and coverage
457 compositionengine::Output::CoverageState coverage{layerFESet};
458 collectVisibleLayers(refreshArgs, coverage);
459
460 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700461 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000462 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800463 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
464
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700465 outputState.undefinedRegion = undefinedRegion;
466 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800467}
468
469void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
470 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800471 // Evaluate the layers from front to back to determine what is visible. This
472 // also incrementally calculates the coverage information for each layer as
473 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800474 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700475 // Incrementally process the coverage for each layer
476 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477
478 // TODO(b/121291683): Stop early if the output is completely covered and
479 // no more layers could even be visible underneath the ones on top.
480 }
481
Lloyd Pique01c77c12019-04-17 12:48:32 -0700482 setReleasedLayers(refreshArgs);
483
484 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800485}
486
Lloyd Piquede196652020-01-22 17:29:58 -0800487void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700488 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800489 // Ensure we have a snapshot of the basic geometry layer state. Limit the
490 // snapshots to once per frame for each candidate layer, as layers may
491 // appear on multiple outputs.
492 if (!coverage.latchedLayers.count(layerFE)) {
493 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800494 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800495 }
496
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700497 // Only consider the layers on this output
498 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800499 return;
500 }
501
502 // Obtain a read-only pointer to the front-end layer state
503 const auto* layerFEState = layerFE->getCompositionState();
504 if (CC_UNLIKELY(!layerFEState)) {
505 return;
506 }
507
508 // handle hidden surfaces by setting the visible region to empty
509 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700510 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800511 }
512
513 /*
514 * opaqueRegion: area of a surface that is fully opaque.
515 */
516 Region opaqueRegion;
517
518 /*
519 * visibleRegion: area of a surface that is visible on screen and not fully
520 * transparent. This is essentially the layer's footprint minus the opaque
521 * regions above it. Areas covered by a translucent surface are considered
522 * visible.
523 */
524 Region visibleRegion;
525
526 /*
527 * coveredRegion: area of a surface that is covered by all visible regions
528 * above it (which includes the translucent areas).
529 */
530 Region coveredRegion;
531
532 /*
533 * transparentRegion: area of a surface that is hinted to be completely
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500534 * transparent.
535 * This is used to tell when the layer has no visible non-transparent
536 * regions and can be removed from the layer list. It does not affect the
537 * visibleRegion of this layer or any layers beneath it. The hint may not
538 * be correct if apps don't respect the SurfaceView restrictions (which,
539 * sadly, some don't).
540 *
541 * In addition, it is used on DISPLAY_DECORATION layers to specify the
542 * blockingRegion, allowing the DPU to skip it to save power. Once we have
543 * hardware that supports a blockingRegion on frames with AFBC, it may be
544 * useful to use this for other layers, too, so long as we can prevent
545 * regressions on b/7179570.
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800546 */
547 Region transparentRegion;
548
Vishnu Naira483b4a2019-12-12 15:07:52 -0800549 /*
550 * shadowRegion: Region cast by the layer's shadow.
551 */
552 Region shadowRegion;
553
Lloyd Piquede196652020-01-22 17:29:58 -0800554 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800555
556 // Get the visible region
557 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
558 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800559 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800560 visibleRegion.set(visibleRect);
561
Lloyd Piquede196652020-01-22 17:29:58 -0800562 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800563 // if the layer casts a shadow, offset the layers visible region and
564 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800565 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800566 Rect visibleRectWithShadows(visibleRect);
567 visibleRectWithShadows.inset(inset, inset, inset, inset);
568 visibleRegion.set(visibleRectWithShadows);
569 shadowRegion = visibleRegion.subtract(visibleRect);
570 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800571
572 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700573 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800574 }
575
576 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800577 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800578 if (tr.preserveRects()) {
579 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800580 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800581 } else {
582 // transformation too complex, can't do the
583 // transparent region optimization.
584 transparentRegion.clear();
585 }
586 }
587
588 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800589 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800590 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800591 // If we one of the simple category of transforms (0/90/180/270 rotation
592 // + any flip), then the opaque region is the layer's footprint.
593 // Otherwise we don't try and compute the opaque region since there may
594 // be errors at the edges, and we treat the entire layer as
595 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800596 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800597 }
598
599 // Clip the covered region to the visible region
600 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
601
602 // Update accumAboveCoveredLayers for next (lower) layer
603 coverage.aboveCoveredLayers.orSelf(visibleRegion);
604
605 // subtract the opaque region covered by the layers above us
606 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
607
608 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700609 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800610 }
611
612 // Get coverage information for the layer as previously displayed,
613 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800614 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700615 auto prevOutputLayer =
616 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800617
618 // Get coverage information for the layer as previously displayed
619 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
620 const Region kEmptyRegion;
621 const Region& oldVisibleRegion =
622 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
623 const Region& oldCoveredRegion =
624 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
625
626 // compute this layer's dirty region
627 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800628 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800629 // we need to invalidate the whole region
630 dirty = visibleRegion;
631 // as well, as the old visible region
632 dirty.orSelf(oldVisibleRegion);
633 } else {
634 /* compute the exposed region:
635 * the exposed region consists of two components:
636 * 1) what's VISIBLE now and was COVERED before
637 * 2) what's EXPOSED now less what was EXPOSED before
638 *
639 * note that (1) is conservative, we start with the whole visible region
640 * but only keep what used to be covered by something -- which mean it
641 * may have been exposed.
642 *
643 * (2) handles areas that were not covered by anything but got exposed
644 * because of a resize.
645 *
646 */
647 const Region newExposed = visibleRegion - coveredRegion;
648 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
649 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
650 }
651 dirty.subtractSelf(coverage.aboveOpaqueLayers);
652
653 // accumulate to the screen dirty region
654 coverage.dirtyRegion.orSelf(dirty);
655
656 // Update accumAboveOpaqueLayers for next (lower) layer
657 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
658
659 // Compute the visible non-transparent region
660 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
661
Vishnu Naira483b4a2019-12-12 15:07:52 -0800662 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800663 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700664 const auto& outputState = getState();
665 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000666 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800667 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700668 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800669 }
670
Vishnu Naira483b4a2019-12-12 15:07:52 -0800671 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
672
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800673 // The layer is visible. Either reuse the existing outputLayer if we have
674 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800675 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800676
677 // Store the layer coverage information into the layer state as some of it
678 // is useful later.
679 auto& outputLayerState = result->editState();
680 outputLayerState.visibleRegion = visibleRegion;
681 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
682 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200683 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000684 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800685 outputLayerState.shadowRegion = shadowRegion;
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500686 outputLayerState.outputSpaceBlockingRegionHint =
Leon Scroggins III7f7ad2c2022-03-17 17:06:20 -0400687 layerFEState->compositionType == Composition::DISPLAY_DECORATION
688 ? outputState.transform.transform(
689 transparentRegion.intersect(outputState.layerStackSpace.getContent()))
690 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800691}
692
693void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
694 // The base class does nothing with this call.
695}
696
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800697void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700698 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800699 layer->getLayerFE().prepareCompositionState(
700 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
701 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800702 }
703}
704
Dan Stoza269dc4d2021-01-15 15:07:43 -0800705void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800706 ATRACE_CALL();
707 ALOGV(__FUNCTION__);
708
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800709 if (!getState().isEnabled) {
710 return;
711 }
712
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800713 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
714 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
715
Lloyd Pique01c77c12019-04-17 12:48:32 -0700716 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700717 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800718 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200719 forceClientComposition,
720 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800721
722 if (mLayerRequestingBackgroundBlur == layer) {
723 forceClientComposition = false;
724 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800725 }
726}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800727
Dan Stoza269dc4d2021-01-15 15:07:43 -0800728void Output::planComposition() {
729 if (!mPlanner || !getState().isEnabled) {
730 return;
731 }
732
733 ATRACE_CALL();
734 ALOGV(__FUNCTION__);
735
736 mPlanner->plan(getOutputLayersOrderedByZ());
737}
738
739void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
740 ATRACE_CALL();
741 ALOGV(__FUNCTION__);
742
743 if (!getState().isEnabled) {
744 return;
745 }
746
Ady Abraham3645e642021-04-20 18:39:00 -0700747 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700748 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800749 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700750
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400751 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800752 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400753 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400754 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400755 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800756 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400757 if (layer == peekThroughLayer) {
758 // No longer needed, although it should not show up again, so
759 // resetting it is not truly needed either.
760 peekThroughLayer = nullptr;
761
762 // peekThroughLayer was already drawn ahead of its z order.
763 continue;
764 }
Dan Stoza6166c312021-01-15 16:34:05 -0800765 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400766 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400767 if (overrideInfo.buffer != nullptr) {
768 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800769 ALOGV("Skipping redundant buffer");
770 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400771 } else {
772 // First layer with the override buffer.
773 if (overrideInfo.peekThroughLayer) {
774 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400775
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400776 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400777 overrideZ = true;
778 includeGeometry = true;
779 constexpr bool isPeekingThrough = true;
780 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
781 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400782 }
783
784 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800785 }
Dan Stoza6166c312021-01-15 16:34:05 -0800786 }
787
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400788 constexpr bool isPeekingThrough = false;
789 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800790 }
791}
792
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800793compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
794 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
795 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100796 auto* compState = layer->getLayerFE().getCompositionState();
797
798 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
799 // want to force client composition because of the blur.
800 if (compState->sidebandStream != nullptr) {
801 return nullptr;
802 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000803 if (compState->isOpaque) {
804 continue;
805 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100806 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800807 layerRequestingBgComposition = layer;
808 }
809 }
810 return layerRequestingBgComposition;
811}
812
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800813void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
814 setColorProfile(pickColorProfile(refreshArgs));
815}
816
817// Returns a data space that fits all visible layers. The returned data space
818// can only be one of
819// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
820// - Dataspace::DISPLAY_P3
821// - Dataspace::DISPLAY_BT2020
822// The returned HDR data space is one of
823// - Dataspace::UNKNOWN
824// - Dataspace::BT2020_HLG
825// - Dataspace::BT2020_PQ
826ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
827 bool* outIsHdrClientComposition) const {
828 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
829 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
830
Lloyd Pique01c77c12019-04-17 12:48:32 -0700831 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800832 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800833 case ui::Dataspace::V0_SCRGB:
834 case ui::Dataspace::V0_SCRGB_LINEAR:
835 case ui::Dataspace::BT2020:
836 case ui::Dataspace::BT2020_ITU:
837 case ui::Dataspace::BT2020_LINEAR:
838 case ui::Dataspace::DISPLAY_BT2020:
839 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
840 break;
841 case ui::Dataspace::DISPLAY_P3:
842 bestDataSpace = ui::Dataspace::DISPLAY_P3;
843 break;
844 case ui::Dataspace::BT2020_PQ:
845 case ui::Dataspace::BT2020_ITU_PQ:
846 bestDataSpace = ui::Dataspace::DISPLAY_P3;
847 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800848 *outIsHdrClientComposition =
849 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800850 break;
851 case ui::Dataspace::BT2020_HLG:
852 case ui::Dataspace::BT2020_ITU_HLG:
853 bestDataSpace = ui::Dataspace::DISPLAY_P3;
854 // When there's mixed PQ content and HLG content, we set the HDR
855 // data space to be BT2020_PQ and convert HLG to PQ.
856 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
857 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
858 }
859 break;
860 default:
861 break;
862 }
863 }
864
865 return bestDataSpace;
866}
867
868compositionengine::Output::ColorProfile Output::pickColorProfile(
869 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
870 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
871 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
872 ui::RenderIntent::COLORIMETRIC,
873 refreshArgs.colorSpaceAgnosticDataspace};
874 }
875
876 ui::Dataspace hdrDataSpace;
877 bool isHdrClientComposition = false;
878 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
879
880 switch (refreshArgs.forceOutputColorMode) {
881 case ui::ColorMode::SRGB:
882 bestDataSpace = ui::Dataspace::V0_SRGB;
883 break;
884 case ui::ColorMode::DISPLAY_P3:
885 bestDataSpace = ui::Dataspace::DISPLAY_P3;
886 break;
887 default:
888 break;
889 }
890
891 // respect hdrDataSpace only when there is no legacy HDR support
892 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
893 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
894 if (isHdr) {
895 bestDataSpace = hdrDataSpace;
896 }
897
898 ui::RenderIntent intent;
899 switch (refreshArgs.outputColorSetting) {
900 case OutputColorSetting::kManaged:
901 case OutputColorSetting::kUnmanaged:
902 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
903 : ui::RenderIntent::COLORIMETRIC;
904 break;
905 case OutputColorSetting::kEnhanced:
906 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
907 break;
908 default: // vendor display color setting
909 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
910 break;
911 }
912
913 ui::ColorMode outMode;
914 ui::Dataspace outDataSpace;
915 ui::RenderIntent outRenderIntent;
916 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
917 &outRenderIntent);
918
919 return ColorProfile{outMode, outDataSpace, outRenderIntent,
920 refreshArgs.colorSpaceAgnosticDataspace};
921}
922
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800923void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700924 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700925 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700926 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700927 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800928
929 // If nothing has changed (!dirty), don't recompose.
930 // If something changed, but we don't currently have any visible layers,
931 // and didn't when we last did a composition, then skip it this time.
932 // The second rule does two things:
933 // - When all layers are removed from a display, we'll emit one black
934 // frame, then nothing more until we get new layers.
935 // - When a display is created with a private layer stack, we won't
936 // emit any black frames until a layer is added to the layer stack.
937 const bool mustRecompose = dirty && !(empty && wasEmpty);
938
939 const char flagPrefix[] = {'-', '+'};
940 static_cast<void>(flagPrefix);
941 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
942 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
943 flagPrefix[empty], flagPrefix[wasEmpty]);
944
945 mRenderSurface->beginFrame(mustRecompose);
946
947 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700948 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800949 }
950}
951
Lloyd Pique66d68602019-02-13 14:23:31 -0800952void Output::prepareFrame() {
953 ATRACE_CALL();
954 ALOGV(__FUNCTION__);
955
Robert Carr6fe2bef2022-03-09 13:49:41 -0800956 const auto& outputState = getState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700957 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800958 return;
959 }
960
Robert Carr6fe2bef2022-03-09 13:49:41 -0800961 chooseCompositionStrategy();
Lloyd Pique66d68602019-02-13 14:23:31 -0800962
Robert Carr6fe2bef2022-03-09 13:49:41 -0800963 if (mPlanner) {
964 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
Dan Stoza47437bb2021-01-15 16:21:07 -0800965 }
966
Robert Carr6fe2bef2022-03-09 13:49:41 -0800967 mRenderSurface->prepareFrame(outputState.usesClientComposition,
968 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800969}
970
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800971void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
972 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
973 return;
974 }
975
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700976 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700977 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Robert Carr6fe2bef2022-03-09 13:49:41 -0800978 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700979 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800980 }
981 }
982
983 postFramebuffer();
984
985 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
986
987 prepareFrame();
988}
989
Robert Carr6fe2bef2022-03-09 13:49:41 -0800990void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800991 ATRACE_CALL();
992 ALOGV(__FUNCTION__);
Robert Carra00eb142022-03-09 13:49:30 -0800993
994 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800995 return;
996 }
997
Robert Carr6fe2bef2022-03-09 13:49:41 -0800998 // Repaint the framebuffer (if needed), getting the optional fence for when
999 // the composition completes.
1000 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001001 if (!optReadyFence) {
1002 return;
1003 }
1004
1005 // swap buffers (presentation)
1006 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1007}
1008
Robert Carr6fe2bef2022-03-09 13:49:41 -08001009std::optional<base::unique_fd> Output::composeSurfaces(
1010 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
1011 ATRACE_CALL();
1012 ALOGV(__FUNCTION__);
1013
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001014 const auto& outputState = getState();
Robert Carr6fe2bef2022-03-09 13:49:41 -08001015 OutputCompositionState& outputCompositionState = editState();
1016 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
1017 outputState.usesClientComposition};
1018
Lloyd Piquee9eff972020-05-05 12:36:44 -07001019 auto& renderEngine = getCompositionEngine().getRenderEngine();
1020 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1021
1022 // If we the display is secure, protected content support is enabled, and at
1023 // least one layer has protected content, we need to use a secure back
1024 // buffer.
1025 if (outputState.isSecure && supportsProtectedContent) {
1026 auto layers = getOutputLayersOrderedByZ();
1027 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1028 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1029 });
1030 if (needsProtected != renderEngine.isProtected()) {
1031 renderEngine.useProtectedContext(needsProtected);
1032 }
1033 if (needsProtected != mRenderSurface->isProtected() &&
1034 needsProtected == renderEngine.isProtected()) {
1035 mRenderSurface->setProtected(needsProtected);
1036 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001037 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1038 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001039 }
1040
Robert Carr6fe2bef2022-03-09 13:49:41 -08001041 base::unique_fd fd;
1042
1043 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001044
1045 // If we aren't doing client composition on this output, but do have a
1046 // flipClientTarget request for this frame on this output, we still need to
1047 // dequeue a buffer.
Robert Carr6fe2bef2022-03-09 13:49:41 -08001048 if (hasClientComposition || outputState.flipClientTarget) {
1049 tex = mRenderSurface->dequeueBuffer(&fd);
1050 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001051 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1052 "client composition for this frame",
1053 mName.c_str());
Robert Carr6fe2bef2022-03-09 13:49:41 -08001054 return {};
Lloyd Piquee9eff972020-05-05 12:36:44 -07001055 }
1056 }
1057
Lloyd Pique688abd42019-02-15 15:42:24 -08001058 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001059 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001060 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001061 }
1062
1063 ALOGV("hasClientComposition");
1064
Lloyd Pique688abd42019-02-15 15:42:24 -08001065 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001066 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1067 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001068 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001069 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001070 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1071 ? outputState.dataspace
1072 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001073
1074 // If we have a valid current display brightness use that, otherwise fall back to the
1075 // display's max desired
Alec Mourib21d94e2022-01-13 17:44:10 -08001076 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
John Reckac09e452021-04-07 16:35:37 -04001077 ? outputState.displayBrightnessNits
1078 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourib21d94e2022-01-13 17:44:10 -08001079 clientCompositionDisplay.maxLuminance =
1080 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourif8d093d2022-02-10 15:16:59 -08001081 clientCompositionDisplay.targetLuminanceNits =
1082 outputState.clientTargetBrightness * outputState.displayBrightnessNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001083
1084 // Compute the global color transform matrix.
Leon Scroggins III745dcaa2022-01-26 11:55:58 -05001085 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1086 clientCompositionDisplay.deviceHandlesColorTransform =
1087 outputState.usesDeviceComposition || getSkipColorTransform();
Lloyd Pique688abd42019-02-15 15:42:24 -08001088
Lloyd Pique688abd42019-02-15 15:42:24 -08001089 // Generate the client composition requests for the layers on this output.
Robert Carrccab4242021-09-28 16:53:03 -07001090 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001091 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001092 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001093 clientCompositionDisplay.outputDataspace,
1094 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001095 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1096
Vishnu Nair9b079a22020-01-21 14:36:08 -08001097 // Check if the client composition requests were rendered into the provided graphic buffer. If
1098 // so, we can reuse the buffer and avoid client composition.
1099 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001100 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1101 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001102 clientCompositionLayers)) {
1103 outputCompositionState.reusedClientComposition = true;
1104 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001105 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001106 }
Alec Mouria90a5702021-04-16 16:36:21 +00001107 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001108 clientCompositionLayers);
1109 }
1110
Lloyd Pique688abd42019-02-15 15:42:24 -08001111 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001112 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1113 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1114 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001115 const bool expensiveBlurs =
1116 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Leon Scroggins IIIcf17ebc2022-03-03 14:54:00 -05001117 const bool expensiveRenderingExpected = expensiveBlurs ||
1118 std::any_of(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1119 [outputDataspace =
1120 clientCompositionDisplay.outputDataspace](const auto& layer) {
1121 return layer.sourceDataspace != outputDataspace;
1122 });
Lloyd Pique688abd42019-02-15 15:42:24 -08001123 if (expensiveRenderingExpected) {
1124 setExpensiveRenderingExpected(true);
1125 }
1126
Sally Qi59a9f502021-10-12 18:53:23 +00001127 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1128 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001129 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001130 std::back_inserter(clientRenderEngineLayers),
1131 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1132 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001133 });
1134
Alec Mourie4034bb2019-11-19 12:45:54 -08001135 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001136 // Only use the framebuffer cache when rendering to an internal display
1137 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1138 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1139 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1140 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1141 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001142 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001143 auto [status, drawFence] =
1144 renderEngine
Sally Qi59a9f502021-10-12 18:53:23 +00001145 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Sally Qi4cabdd02021-08-05 16:45:57 -07001146 useFramebufferCache, std::move(fd))
1147 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001148
1149 if (status != NO_ERROR && mClientCompositionRequestCache) {
1150 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001151 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001152 }
1153
Alec Mourie4034bb2019-11-19 12:45:54 -08001154 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001155 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001156 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1157 } else {
1158 timeStats.recordRenderEngineDuration(renderEngineStart,
1159 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001160 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001161 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001162
Robert Carrccab4242021-09-28 16:53:03 -07001163 if (clientCompositionLayersFE.size() > 0) {
1164 sp<Fence> clientCompFence = new Fence(dup(drawFence.get()));
1165 for (auto clientComposedLayer : clientCompositionLayersFE) {
1166 clientComposedLayer->setWasClientComposed(clientCompFence);
1167 }
1168 }
1169
Sally Qi4cabdd02021-08-05 16:45:57 -07001170 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001171}
1172
Vishnu Nair9b079a22020-01-21 14:36:08 -08001173std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001174 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001175 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001176 ALOGV("Rendering client layers");
1177
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001178 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001179 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001180 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001181
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001182 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001183 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001184
Lloyd Pique01c77c12019-04-17 12:48:32 -07001185 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001186 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001187 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001188 auto& layerFE = layer->getLayerFE();
1189
Lloyd Piquea2468662019-03-07 21:31:06 -08001190 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 ALOGV("Layer: %s", layerFE.getDebugName());
1192 if (clip.isEmpty()) {
1193 ALOGV(" Skipping for empty clip");
1194 firstLayer = false;
1195 continue;
1196 }
1197
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001198 disableBlurs |= layerFEState->sidebandStream != nullptr;
1199
Vishnu Naira483b4a2019-12-12 15:07:52 -08001200 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001201
1202 // We clear the client target for non-client composed layers if
1203 // requested by the HWC. We skip this if the layer is not an opaque
1204 // rectangle, as by definition the layer must blend with whatever is
1205 // underneath. We also skip the first layer as the buffer target is
1206 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001207 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001208 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001209
1210 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1211
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001212 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1213 // composing the non-shadow content and only draw the shadows.
1214 const bool realContentIsVisible = clientComposition &&
1215 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1216
Lloyd Pique688abd42019-02-15 15:42:24 -08001217 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001218 std::vector<LayerFE::LayerSettings> results;
1219 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001220 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001221 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001222 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001223 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1224 } else {
1225 ALOGV("Skipping redundant override buffer for [%s] in RE",
1226 layer->getLayerFE().getDebugName());
1227 }
Dan Stoza6166c312021-01-15 16:34:05 -08001228 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001229 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1230 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1231 : (layer->getState().overrideInfo.disableBackgroundBlur
1232 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1233 BlurRegionsOnly
1234 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1235 Enabled);
1236 compositionengine::LayerFE::ClientCompositionTargetSettings
1237 targetSettings{.clip = clip,
1238 .needsFiltering = layer->needsFiltering() ||
1239 outputState.needsFiltering,
1240 .isSecure = outputState.isSecure,
1241 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001242 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001243 .dataspace = outputDataspace,
1244 .realContentIsVisible = realContentIsVisible,
1245 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001246 .blurSetting = blurSetting,
1247 .whitePointNits = layerState.whitePointNits};
Dan Stoza6166c312021-01-15 16:34:05 -08001248 results = layerFE.prepareClientCompositionList(targetSettings);
1249 if (realContentIsVisible && !results.empty()) {
1250 layer->editState().clientCompositionTimestamp = systemTime();
1251 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001252 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001253
Robert Carrccab4242021-09-28 16:53:03 -07001254 outLayerFEs.push_back(&layerFE);
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001255 clientCompositionLayers.insert(clientCompositionLayers.end(),
1256 std::make_move_iterator(results.begin()),
1257 std::make_move_iterator(results.end()));
1258 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001259 }
1260
1261 firstLayer = false;
1262 }
1263
1264 return clientCompositionLayers;
1265}
1266
1267void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001268 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001269 if (flashRegion.isEmpty()) {
1270 return;
1271 }
1272
Vishnu Nair9b079a22020-01-21 14:36:08 -08001273 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001274 layerSettings.source.buffer.buffer = nullptr;
1275 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1276 layerSettings.alpha = half(1.0);
1277
1278 for (const auto& rect : flashRegion) {
1279 layerSettings.geometry.boundaries = rect.toFloatRect();
1280 clientCompositionLayers.push_back(layerSettings);
1281 }
1282}
1283
1284void Output::setExpensiveRenderingExpected(bool) {
1285 // The base class does nothing with this call.
1286}
1287
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001288void Output::postFramebuffer() {
1289 ATRACE_CALL();
1290 ALOGV(__FUNCTION__);
1291
1292 if (!getState().isEnabled) {
1293 return;
1294 }
1295
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001296 auto& outputState = editState();
1297 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001298 mRenderSurface->flip();
1299
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001300 auto frame = presentAndGetFrameFences();
1301
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001302 mRenderSurface->onPresentDisplayCompleted();
1303
Lloyd Pique01c77c12019-04-17 12:48:32 -07001304 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001305 // The layer buffer from the previous frame (if any) is released
1306 // by HWC only when the release fence from this frame (if any) is
1307 // signaled. Always get the release fence from HWC first.
1308 sp<Fence> releaseFence = Fence::NO_FENCE;
1309
1310 if (auto hwcLayer = layer->getHwcLayer()) {
1311 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1312 releaseFence = f->second;
1313 }
1314 }
1315
1316 // If the layer was client composited in the previous frame, we
1317 // need to merge with the previous client target acquire fence.
1318 // Since we do not track that, always merge with the current
1319 // client target acquire fence when it is available, even though
1320 // this is suboptimal.
1321 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001322 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001323 releaseFence =
1324 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1325 }
Sally Qi59a9f502021-10-12 18:53:23 +00001326 layer->getLayerFE().onLayerDisplayed(
1327 ftl::yield<renderengine::RenderEngineResult>(
1328 {NO_ERROR, base::unique_fd(releaseFence->dup())})
1329 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001330 }
1331
1332 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001333 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001334 // supply them with the present fence.
1335 for (auto& weakLayer : mReleasedLayers) {
1336 if (auto layer = weakLayer.promote(); layer != nullptr) {
Sally Qi59a9f502021-10-12 18:53:23 +00001337 layer->onLayerDisplayed(ftl::yield<renderengine::RenderEngineResult>(
1338 {NO_ERROR, base::unique_fd(frame.presentFence->dup())})
1339 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001340 }
1341 }
1342
1343 // Clear out the released layers now that we're done with them.
1344 mReleasedLayers.clear();
1345}
1346
Alec Mouriaa831582021-06-07 16:23:01 -07001347void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001348 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001349 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001350 }
1351}
1352
Lloyd Pique32cbe282018-10-19 13:09:22 -07001353void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001354 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001355 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001356}
1357
Robert Carr6fe2bef2022-03-09 13:49:41 -08001358void Output::chooseCompositionStrategy() {
Lloyd Pique66d68602019-02-13 14:23:31 -08001359 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001360 auto& outputState = editState();
1361 outputState.usesClientComposition = true;
1362 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001363 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001364}
1365
Lloyd Pique688abd42019-02-15 15:42:24 -08001366bool Output::getSkipColorTransform() const {
1367 return true;
1368}
1369
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001370compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1371 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001372 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001373 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1374 }
1375 return result;
1376}
1377
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001378} // namespace impl
1379} // namespace android::compositionengine