blob: e0362521eb2481d1637e88950a123e611904913b [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 {
Lloyd Pique32cbe282018-10-19 13:09:22 -070060
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();
437 prepareFrame();
438 devOptRepaintFlash(refreshArgs);
439 finishFrame(refreshArgs);
440 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 =
687 layerFEState->compositionType == Composition::DISPLAY_DECORATION ? transparentRegion
688 : Region();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800689}
690
691void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
692 // The base class does nothing with this call.
693}
694
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800695void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700696 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800697 layer->getLayerFE().prepareCompositionState(
698 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
699 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800700 }
701}
702
Dan Stoza269dc4d2021-01-15 15:07:43 -0800703void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800704 ATRACE_CALL();
705 ALOGV(__FUNCTION__);
706
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800707 if (!getState().isEnabled) {
708 return;
709 }
710
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800711 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
712 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
713
Lloyd Pique01c77c12019-04-17 12:48:32 -0700714 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700715 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800716 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200717 forceClientComposition,
718 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800719
720 if (mLayerRequestingBackgroundBlur == layer) {
721 forceClientComposition = false;
722 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800723 }
724}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800725
Dan Stoza269dc4d2021-01-15 15:07:43 -0800726void Output::planComposition() {
727 if (!mPlanner || !getState().isEnabled) {
728 return;
729 }
730
731 ATRACE_CALL();
732 ALOGV(__FUNCTION__);
733
734 mPlanner->plan(getOutputLayersOrderedByZ());
735}
736
737void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
738 ATRACE_CALL();
739 ALOGV(__FUNCTION__);
740
741 if (!getState().isEnabled) {
742 return;
743 }
744
Ady Abraham3645e642021-04-20 18:39:00 -0700745 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700746 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800747 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700748
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400749 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800750 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400751 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400752 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400753 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800754 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400755 if (layer == peekThroughLayer) {
756 // No longer needed, although it should not show up again, so
757 // resetting it is not truly needed either.
758 peekThroughLayer = nullptr;
759
760 // peekThroughLayer was already drawn ahead of its z order.
761 continue;
762 }
Dan Stoza6166c312021-01-15 16:34:05 -0800763 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400764 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400765 if (overrideInfo.buffer != nullptr) {
766 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800767 ALOGV("Skipping redundant buffer");
768 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400769 } else {
770 // First layer with the override buffer.
771 if (overrideInfo.peekThroughLayer) {
772 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400773
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400774 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400775 overrideZ = true;
776 includeGeometry = true;
777 constexpr bool isPeekingThrough = true;
778 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
779 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400780 }
781
782 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800783 }
Dan Stoza6166c312021-01-15 16:34:05 -0800784 }
785
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400786 constexpr bool isPeekingThrough = false;
787 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800788 }
789}
790
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800791compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
792 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
793 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100794 auto* compState = layer->getLayerFE().getCompositionState();
795
796 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
797 // want to force client composition because of the blur.
798 if (compState->sidebandStream != nullptr) {
799 return nullptr;
800 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000801 if (compState->isOpaque) {
802 continue;
803 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100804 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800805 layerRequestingBgComposition = layer;
806 }
807 }
808 return layerRequestingBgComposition;
809}
810
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800811void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
812 setColorProfile(pickColorProfile(refreshArgs));
813}
814
815// Returns a data space that fits all visible layers. The returned data space
816// can only be one of
817// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
818// - Dataspace::DISPLAY_P3
819// - Dataspace::DISPLAY_BT2020
820// The returned HDR data space is one of
821// - Dataspace::UNKNOWN
822// - Dataspace::BT2020_HLG
823// - Dataspace::BT2020_PQ
824ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
825 bool* outIsHdrClientComposition) const {
826 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
827 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
828
Lloyd Pique01c77c12019-04-17 12:48:32 -0700829 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800830 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800831 case ui::Dataspace::V0_SCRGB:
832 case ui::Dataspace::V0_SCRGB_LINEAR:
833 case ui::Dataspace::BT2020:
834 case ui::Dataspace::BT2020_ITU:
835 case ui::Dataspace::BT2020_LINEAR:
836 case ui::Dataspace::DISPLAY_BT2020:
837 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
838 break;
839 case ui::Dataspace::DISPLAY_P3:
840 bestDataSpace = ui::Dataspace::DISPLAY_P3;
841 break;
842 case ui::Dataspace::BT2020_PQ:
843 case ui::Dataspace::BT2020_ITU_PQ:
844 bestDataSpace = ui::Dataspace::DISPLAY_P3;
845 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800846 *outIsHdrClientComposition =
847 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800848 break;
849 case ui::Dataspace::BT2020_HLG:
850 case ui::Dataspace::BT2020_ITU_HLG:
851 bestDataSpace = ui::Dataspace::DISPLAY_P3;
852 // When there's mixed PQ content and HLG content, we set the HDR
853 // data space to be BT2020_PQ and convert HLG to PQ.
854 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
855 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
856 }
857 break;
858 default:
859 break;
860 }
861 }
862
863 return bestDataSpace;
864}
865
866compositionengine::Output::ColorProfile Output::pickColorProfile(
867 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
868 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
869 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
870 ui::RenderIntent::COLORIMETRIC,
871 refreshArgs.colorSpaceAgnosticDataspace};
872 }
873
874 ui::Dataspace hdrDataSpace;
875 bool isHdrClientComposition = false;
876 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
877
878 switch (refreshArgs.forceOutputColorMode) {
879 case ui::ColorMode::SRGB:
880 bestDataSpace = ui::Dataspace::V0_SRGB;
881 break;
882 case ui::ColorMode::DISPLAY_P3:
883 bestDataSpace = ui::Dataspace::DISPLAY_P3;
884 break;
885 default:
886 break;
887 }
888
889 // respect hdrDataSpace only when there is no legacy HDR support
890 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
891 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
892 if (isHdr) {
893 bestDataSpace = hdrDataSpace;
894 }
895
896 ui::RenderIntent intent;
897 switch (refreshArgs.outputColorSetting) {
898 case OutputColorSetting::kManaged:
899 case OutputColorSetting::kUnmanaged:
900 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
901 : ui::RenderIntent::COLORIMETRIC;
902 break;
903 case OutputColorSetting::kEnhanced:
904 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
905 break;
906 default: // vendor display color setting
907 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
908 break;
909 }
910
911 ui::ColorMode outMode;
912 ui::Dataspace outDataSpace;
913 ui::RenderIntent outRenderIntent;
914 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
915 &outRenderIntent);
916
917 return ColorProfile{outMode, outDataSpace, outRenderIntent,
918 refreshArgs.colorSpaceAgnosticDataspace};
919}
920
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800921void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700922 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700923 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700924 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700925 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800926
927 // If nothing has changed (!dirty), don't recompose.
928 // If something changed, but we don't currently have any visible layers,
929 // and didn't when we last did a composition, then skip it this time.
930 // The second rule does two things:
931 // - When all layers are removed from a display, we'll emit one black
932 // frame, then nothing more until we get new layers.
933 // - When a display is created with a private layer stack, we won't
934 // emit any black frames until a layer is added to the layer stack.
935 const bool mustRecompose = dirty && !(empty && wasEmpty);
936
937 const char flagPrefix[] = {'-', '+'};
938 static_cast<void>(flagPrefix);
939 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
940 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
941 flagPrefix[empty], flagPrefix[wasEmpty]);
942
943 mRenderSurface->beginFrame(mustRecompose);
944
945 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700946 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800947 }
948}
949
Lloyd Pique66d68602019-02-13 14:23:31 -0800950void Output::prepareFrame() {
951 ATRACE_CALL();
952 ALOGV(__FUNCTION__);
953
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700954 const auto& outputState = getState();
955 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800956 return;
957 }
958
959 chooseCompositionStrategy();
960
Dan Stoza47437bb2021-01-15 16:21:07 -0800961 if (mPlanner) {
962 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
963 }
964
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700965 mRenderSurface->prepareFrame(outputState.usesClientComposition,
966 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800967}
968
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800969void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
970 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
971 return;
972 }
973
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700974 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700975 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800976 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700977 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800978 }
979 }
980
981 postFramebuffer();
982
983 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
984
985 prepareFrame();
986}
987
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800988void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800989 ATRACE_CALL();
990 ALOGV(__FUNCTION__);
991
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700992 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800993 return;
994 }
995
996 // Repaint the framebuffer (if needed), getting the optional fence for when
997 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800998 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800999 if (!optReadyFence) {
1000 return;
1001 }
1002
1003 // swap buffers (presentation)
1004 mRenderSurface->queueBuffer(std::move(*optReadyFence));
1005}
1006
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001007std::optional<base::unique_fd> Output::composeSurfaces(
1008 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001009 ATRACE_CALL();
1010 ALOGV(__FUNCTION__);
1011
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001012 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001013 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001014 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001015 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -07001016
1017 auto& renderEngine = getCompositionEngine().getRenderEngine();
1018 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1019
1020 // If we the display is secure, protected content support is enabled, and at
1021 // least one layer has protected content, we need to use a secure back
1022 // buffer.
1023 if (outputState.isSecure && supportsProtectedContent) {
1024 auto layers = getOutputLayersOrderedByZ();
1025 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1026 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1027 });
1028 if (needsProtected != renderEngine.isProtected()) {
1029 renderEngine.useProtectedContext(needsProtected);
1030 }
1031 if (needsProtected != mRenderSurface->isProtected() &&
1032 needsProtected == renderEngine.isProtected()) {
1033 mRenderSurface->setProtected(needsProtected);
1034 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001035 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1036 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001037 }
1038
1039 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001040
1041 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001042
1043 // If we aren't doing client composition on this output, but do have a
1044 // flipClientTarget request for this frame on this output, we still need to
1045 // dequeue a buffer.
1046 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001047 tex = mRenderSurface->dequeueBuffer(&fd);
1048 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001049 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1050 "client composition for this frame",
1051 mName.c_str());
1052 return {};
1053 }
1054 }
1055
Lloyd Pique688abd42019-02-15 15:42:24 -08001056 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001057 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001058 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001059 }
1060
1061 ALOGV("hasClientComposition");
1062
Lloyd Pique688abd42019-02-15 15:42:24 -08001063 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001064 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1065 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001066 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001067 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001068 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1069 ? outputState.dataspace
1070 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001071
1072 // If we have a valid current display brightness use that, otherwise fall back to the
1073 // display's max desired
Alec Mourib21d94e2022-01-13 17:44:10 -08001074 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
John Reckac09e452021-04-07 16:35:37 -04001075 ? outputState.displayBrightnessNits
1076 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourib21d94e2022-01-13 17:44:10 -08001077 clientCompositionDisplay.maxLuminance =
1078 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001079 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001080
1081 // Compute the global color transform matrix.
Leon Scroggins III745dcaa2022-01-26 11:55:58 -05001082 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
1083 clientCompositionDisplay.deviceHandlesColorTransform =
1084 outputState.usesDeviceComposition || getSkipColorTransform();
Lloyd Pique688abd42019-02-15 15:42:24 -08001085
Lloyd Pique688abd42019-02-15 15:42:24 -08001086 // Generate the client composition requests for the layers on this output.
Robert Carrccab4242021-09-28 16:53:03 -07001087 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001088 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001089 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001090 clientCompositionDisplay.outputDataspace,
1091 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001092 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1093
Vishnu Nair9b079a22020-01-21 14:36:08 -08001094 // Check if the client composition requests were rendered into the provided graphic buffer. If
1095 // so, we can reuse the buffer and avoid client composition.
1096 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001097 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1098 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001099 clientCompositionLayers)) {
1100 outputCompositionState.reusedClientComposition = true;
1101 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001102 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001103 }
Alec Mouria90a5702021-04-16 16:36:21 +00001104 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001105 clientCompositionLayers);
1106 }
1107
Lloyd Pique688abd42019-02-15 15:42:24 -08001108 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001109 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1110 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1111 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001112 const bool expensiveBlurs =
1113 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001114 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001115 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001116 if (expensiveRenderingExpected) {
1117 setExpensiveRenderingExpected(true);
1118 }
1119
Sally Qi59a9f502021-10-12 18:53:23 +00001120 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1121 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001122 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001123 std::back_inserter(clientRenderEngineLayers),
1124 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1125 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001126 });
1127
Alec Mourie4034bb2019-11-19 12:45:54 -08001128 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001129 // Only use the framebuffer cache when rendering to an internal display
1130 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1131 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1132 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1133 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1134 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001135 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001136 auto [status, drawFence] =
1137 renderEngine
Sally Qi59a9f502021-10-12 18:53:23 +00001138 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Sally Qi4cabdd02021-08-05 16:45:57 -07001139 useFramebufferCache, std::move(fd))
1140 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001141
1142 if (status != NO_ERROR && mClientCompositionRequestCache) {
1143 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001144 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001145 }
1146
Alec Mourie4034bb2019-11-19 12:45:54 -08001147 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001148 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001149 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1150 } else {
1151 timeStats.recordRenderEngineDuration(renderEngineStart,
1152 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001153 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001154 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001155
Robert Carrccab4242021-09-28 16:53:03 -07001156 if (clientCompositionLayersFE.size() > 0) {
1157 sp<Fence> clientCompFence = new Fence(dup(drawFence.get()));
1158 for (auto clientComposedLayer : clientCompositionLayersFE) {
1159 clientComposedLayer->setWasClientComposed(clientCompFence);
1160 }
1161 }
1162
Sally Qi4cabdd02021-08-05 16:45:57 -07001163 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001164}
1165
Vishnu Nair9b079a22020-01-21 14:36:08 -08001166std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001167 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001168 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001169 ALOGV("Rendering client layers");
1170
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001171 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001172 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001173 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001174
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001175 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001176 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001177
Lloyd Pique01c77c12019-04-17 12:48:32 -07001178 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001179 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001180 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001181 auto& layerFE = layer->getLayerFE();
1182
Lloyd Piquea2468662019-03-07 21:31:06 -08001183 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001184 ALOGV("Layer: %s", layerFE.getDebugName());
1185 if (clip.isEmpty()) {
1186 ALOGV(" Skipping for empty clip");
1187 firstLayer = false;
1188 continue;
1189 }
1190
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001191 disableBlurs |= layerFEState->sidebandStream != nullptr;
1192
Vishnu Naira483b4a2019-12-12 15:07:52 -08001193 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001194
1195 // We clear the client target for non-client composed layers if
1196 // requested by the HWC. We skip this if the layer is not an opaque
1197 // rectangle, as by definition the layer must blend with whatever is
1198 // underneath. We also skip the first layer as the buffer target is
1199 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001200 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001201 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001202
1203 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1204
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001205 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1206 // composing the non-shadow content and only draw the shadows.
1207 const bool realContentIsVisible = clientComposition &&
1208 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1209
Lloyd Pique688abd42019-02-15 15:42:24 -08001210 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001211 std::vector<LayerFE::LayerSettings> results;
1212 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001213 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001214 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001215 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001216 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1217 } else {
1218 ALOGV("Skipping redundant override buffer for [%s] in RE",
1219 layer->getLayerFE().getDebugName());
1220 }
Dan Stoza6166c312021-01-15 16:34:05 -08001221 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001222 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1223 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1224 : (layer->getState().overrideInfo.disableBackgroundBlur
1225 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1226 BlurRegionsOnly
1227 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1228 Enabled);
1229 compositionengine::LayerFE::ClientCompositionTargetSettings
1230 targetSettings{.clip = clip,
1231 .needsFiltering = layer->needsFiltering() ||
1232 outputState.needsFiltering,
1233 .isSecure = outputState.isSecure,
1234 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001235 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001236 .dataspace = outputDataspace,
1237 .realContentIsVisible = realContentIsVisible,
1238 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001239 .blurSetting = blurSetting,
1240 .whitePointNits = layerState.whitePointNits};
Dan Stoza6166c312021-01-15 16:34:05 -08001241 results = layerFE.prepareClientCompositionList(targetSettings);
1242 if (realContentIsVisible && !results.empty()) {
1243 layer->editState().clientCompositionTimestamp = systemTime();
1244 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001245 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001246
Robert Carrccab4242021-09-28 16:53:03 -07001247 outLayerFEs.push_back(&layerFE);
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001248 clientCompositionLayers.insert(clientCompositionLayers.end(),
1249 std::make_move_iterator(results.begin()),
1250 std::make_move_iterator(results.end()));
1251 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001252 }
1253
1254 firstLayer = false;
1255 }
1256
1257 return clientCompositionLayers;
1258}
1259
1260void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001261 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001262 if (flashRegion.isEmpty()) {
1263 return;
1264 }
1265
Vishnu Nair9b079a22020-01-21 14:36:08 -08001266 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001267 layerSettings.source.buffer.buffer = nullptr;
1268 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1269 layerSettings.alpha = half(1.0);
1270
1271 for (const auto& rect : flashRegion) {
1272 layerSettings.geometry.boundaries = rect.toFloatRect();
1273 clientCompositionLayers.push_back(layerSettings);
1274 }
1275}
1276
1277void Output::setExpensiveRenderingExpected(bool) {
1278 // The base class does nothing with this call.
1279}
1280
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001281void Output::postFramebuffer() {
1282 ATRACE_CALL();
1283 ALOGV(__FUNCTION__);
1284
1285 if (!getState().isEnabled) {
1286 return;
1287 }
1288
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001289 auto& outputState = editState();
1290 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001291 mRenderSurface->flip();
1292
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001293 auto frame = presentAndGetFrameFences();
1294
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001295 mRenderSurface->onPresentDisplayCompleted();
1296
Lloyd Pique01c77c12019-04-17 12:48:32 -07001297 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001298 // The layer buffer from the previous frame (if any) is released
1299 // by HWC only when the release fence from this frame (if any) is
1300 // signaled. Always get the release fence from HWC first.
1301 sp<Fence> releaseFence = Fence::NO_FENCE;
1302
1303 if (auto hwcLayer = layer->getHwcLayer()) {
1304 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1305 releaseFence = f->second;
1306 }
1307 }
1308
1309 // If the layer was client composited in the previous frame, we
1310 // need to merge with the previous client target acquire fence.
1311 // Since we do not track that, always merge with the current
1312 // client target acquire fence when it is available, even though
1313 // this is suboptimal.
1314 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001315 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001316 releaseFence =
1317 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1318 }
Sally Qi59a9f502021-10-12 18:53:23 +00001319 layer->getLayerFE().onLayerDisplayed(
1320 ftl::yield<renderengine::RenderEngineResult>(
1321 {NO_ERROR, base::unique_fd(releaseFence->dup())})
1322 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001323 }
1324
1325 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001326 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001327 // supply them with the present fence.
1328 for (auto& weakLayer : mReleasedLayers) {
1329 if (auto layer = weakLayer.promote(); layer != nullptr) {
Sally Qi59a9f502021-10-12 18:53:23 +00001330 layer->onLayerDisplayed(ftl::yield<renderengine::RenderEngineResult>(
1331 {NO_ERROR, base::unique_fd(frame.presentFence->dup())})
1332 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001333 }
1334 }
1335
1336 // Clear out the released layers now that we're done with them.
1337 mReleasedLayers.clear();
1338}
1339
Alec Mouriaa831582021-06-07 16:23:01 -07001340void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001341 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001342 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001343 }
1344}
1345
Lloyd Pique32cbe282018-10-19 13:09:22 -07001346void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001347 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001348 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001349}
1350
Lloyd Pique66d68602019-02-13 14:23:31 -08001351void Output::chooseCompositionStrategy() {
1352 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001353 auto& outputState = editState();
1354 outputState.usesClientComposition = true;
1355 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001356 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001357}
1358
Lloyd Pique688abd42019-02-15 15:42:24 -08001359bool Output::getSkipColorTransform() const {
1360 return true;
1361}
1362
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001363compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1364 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001365 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001366 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1367 }
1368 return result;
1369}
1370
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001371} // namespace impl
1372} // namespace android::compositionengine