blob: 6800004a90065f27e9e207dd417b79f0d3474dca [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
Lloyd Piquefeb73d72018-12-04 17:23:44 -080053namespace android::compositionengine {
54
55Output::~Output() = default;
56
57namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070058
Lloyd Piquec29e4c62019-03-07 21:48:19 -080059namespace {
60
61template <typename T>
62class Reversed {
63public:
64 explicit Reversed(const T& container) : mContainer(container) {}
65 auto begin() { return mContainer.rbegin(); }
66 auto end() { return mContainer.rend(); }
67
68private:
69 const T& mContainer;
70};
71
72// Helper for enumerating over a container in reverse order
73template <typename T>
74Reversed<T> reversed(const T& c) {
75 return Reversed<T>(c);
76}
77
Marin Shalamanovb15d2272020-09-17 21:41:52 +020078struct ScaleVector {
79 float x;
80 float y;
81};
82
83// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
84// where to' will have the same size as "to". In the case where "from" and "to"
85// start at the origin to'=to.
86ScaleVector getScale(const Rect& from, const Rect& to) {
87 return {.x = static_cast<float>(to.width()) / from.width(),
88 .y = static_cast<float>(to.height()) / from.height()};
89}
90
Lloyd Piquec29e4c62019-03-07 21:48:19 -080091} // namespace
92
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070093std::shared_ptr<Output> createOutput(
94 const compositionengine::CompositionEngine& compositionEngine) {
95 return createOutputTemplated<Output>(compositionEngine);
96}
Lloyd Pique32cbe282018-10-19 13:09:22 -070097
98Output::~Output() = default;
99
Lloyd Pique32cbe282018-10-19 13:09:22 -0700100bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700101 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
102 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103}
104
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700105std::optional<DisplayId> Output::getDisplayId() const {
106 return {};
107}
108
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109const std::string& Output::getName() const {
110 return mName;
111}
112
113void Output::setName(const std::string& name) {
114 mName = name;
115}
116
117void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700118 auto& outputState = editState();
119 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120 return;
121 }
122
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700123 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700124 dirtyEntireOutput();
125}
126
Alec Mouri023c1882021-05-08 16:36:33 -0700127void Output::setLayerCachingEnabled(bool enabled) {
128 if (enabled == (mPlanner != nullptr)) {
129 return;
130 }
131
132 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700133 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700134 if (mRenderSurface) {
135 mPlanner->setDisplaySize(mRenderSurface->getSize());
136 }
137 } else {
138 mPlanner.reset();
139 }
Alec Mouric773472b2021-05-19 14:29:05 -0700140
141 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
142 if (!outputLayer) {
143 continue;
144 }
145
146 outputLayer->editState().overrideInfo = {};
147 }
Alec Mouri023c1882021-05-08 16:36:33 -0700148}
149
Ady Abrahamdb036a82021-07-16 14:18:34 -0700150void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
151 if (mPlanner) {
152 mPlanner->setTexturePoolEnabled(enabled);
153 }
154}
155
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200156void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
157 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700158 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200159
Angel Aguayob084e0c2021-08-04 23:27:28 +0000160 outputState.displaySpace.setOrientation(orientation);
161 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200162 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200163
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200164 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000165 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200166 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200167 std::swap(orientedSize.width, orientedSize.height);
168 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000169 outputState.orientedDisplaySpace.setBounds(orientedSize);
170 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200171
172 // Compute displaySpace.content
173 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
174 ui::Transform rotation;
175 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000176 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200177 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
178 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000179 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200180
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200181 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000182 outputState.framebufferSpace.setOrientation(orientation);
183 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200184 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000185 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
186 outputState.framebufferSpace.getBoundsAsRect());
187 outputState.framebufferSpace.setContent(
188 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200189
190 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000191 outputState.layerStackSpace.setContent(layerStackSpaceRect);
192 outputState.layerStackSpace.setBounds(
193 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200194
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200195 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
196 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700197 dirtyEntireOutput();
198}
199
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200200void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700201 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200202
203 auto& state = editState();
204
205 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000206 const ui::Size newBounds(size);
207 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200208
209 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000210 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200211 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
212
213 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000214 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200215 ui::Size orientedSize = size;
216 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
217 std::swap(orientedSize.width, orientedSize.height);
218 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000219 const ui::Size newOrientedBounds(orientedSize);
220 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700221
Dan Stoza6166c312021-01-15 16:34:05 -0800222 if (mPlanner) {
223 mPlanner->setDisplaySize(size);
224 }
225
Lloyd Pique32cbe282018-10-19 13:09:22 -0700226 dirtyEntireOutput();
227}
228
Garfield Tan54edd912020-10-21 16:31:41 -0700229ui::Transform::RotationFlags Output::getTransformHint() const {
230 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
231}
232
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700233void Output::setLayerFilter(ui::LayerFilter filter) {
234 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700235 dirtyEntireOutput();
236}
237
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800238void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700239 auto& colorTransformMatrix = editState().colorTransformMatrix;
240 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700241 return;
242 }
243
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700244 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800245
246 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700247}
248
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800249void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700250 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800251 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
252 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800253
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700254 auto& outputState = editState();
255 if (outputState.colorMode == colorProfile.mode &&
256 outputState.dataspace == colorProfile.dataspace &&
257 outputState.renderIntent == colorProfile.renderIntent &&
258 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800259 return;
260 }
261
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 outputState.colorMode = colorProfile.mode;
263 outputState.dataspace = colorProfile.dataspace;
264 outputState.renderIntent = colorProfile.renderIntent;
265 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800267 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700268
Lloyd Pique32cbe282018-10-19 13:09:22 -0700269 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800270 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
271 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800272
273 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700274}
275
John Reckac09e452021-04-07 16:35:37 -0400276void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
277 auto& outputState = editState();
278 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
279 outputState.displayBrightnessNits == displayBrightnessNits) {
280 // Nothing changed
281 return;
282 }
283 outputState.sdrWhitePointNits = sdrWhitePointNits;
284 outputState.displayBrightnessNits = displayBrightnessNits;
285 dirtyEntireOutput();
286}
287
Lloyd Pique32cbe282018-10-19 13:09:22 -0700288void Output::dump(std::string& out) const {
289 using android::base::StringAppendF;
290
291 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
292
293 out.append("\n ");
294
295 dumpBase(out);
296}
297
298void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700299 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700300
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700301 if (mDisplayColorProfile) {
302 mDisplayColorProfile->dump(out);
303 } else {
304 out.append(" No display color profile!\n");
305 }
306
Lloyd Pique31cb2942018-10-19 17:23:03 -0700307 if (mRenderSurface) {
308 mRenderSurface->dump(out);
309 } else {
310 out.append(" No render surface!\n");
311 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800312
Lloyd Pique01c77c12019-04-17 12:48:32 -0700313 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
314 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800315 if (!outputLayer) {
316 continue;
317 }
318 outputLayer->dump(out);
319 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700320}
321
Dan Stoza269dc4d2021-01-15 15:07:43 -0800322void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
323 if (!mPlanner) {
324 base::StringAppendF(&out, "Planner is disabled\n");
325 return;
326 }
327 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
328 mPlanner->dump(args, out);
329}
330
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700331compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
332 return mDisplayColorProfile.get();
333}
334
335void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
336 mDisplayColorProfile = std::move(mode);
337}
338
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800339const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
340 return mReleasedLayers;
341}
342
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700343void Output::setDisplayColorProfileForTest(
344 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
345 mDisplayColorProfile = std::move(mode);
346}
347
Lloyd Pique31cb2942018-10-19 17:23:03 -0700348compositionengine::RenderSurface* Output::getRenderSurface() const {
349 return mRenderSurface.get();
350}
351
352void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
353 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800354 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000355 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800356 if (mPlanner) {
357 mPlanner->setDisplaySize(size);
358 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700359 dirtyEntireOutput();
360}
361
Vishnu Nair9b079a22020-01-21 14:36:08 -0800362void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
363 if (cacheSize == 0) {
364 mClientCompositionRequestCache.reset();
365 } else {
366 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
367 }
368};
369
Lloyd Pique31cb2942018-10-19 17:23:03 -0700370void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
371 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700372}
373
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700374Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700375 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000376 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700377}
378
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700379bool Output::includesLayer(ui::LayerFilter filter) const {
380 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700381}
382
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700383bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800384 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700385 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800386}
387
Lloyd Piquedf336d92019-03-07 21:38:42 -0800388std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800389 const sp<LayerFE>& layerFE) const {
390 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800391}
392
Lloyd Piquede196652020-01-22 17:29:58 -0800393compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
394 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700395 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800396}
397
Lloyd Pique01c77c12019-04-17 12:48:32 -0700398std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800399 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700400 for (size_t i = 0; i < getOutputLayerCount(); i++) {
401 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800402 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700403 return i;
404 }
405 }
406 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800407}
408
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800409void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
410 mReleasedLayers = std::move(layers);
411}
412
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800413void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
414 LayerFESet& geomSnapshots) {
415 ATRACE_CALL();
416 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800417
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800418 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800419}
420
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800421void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800422 ATRACE_CALL();
423 ALOGV(__FUNCTION__);
424
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800425 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800426 updateCompositionState(refreshArgs);
427 planComposition();
428 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800429 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800430 beginFrame();
431 prepareFrame();
432 devOptRepaintFlash(refreshArgs);
433 finishFrame(refreshArgs);
434 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700435 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800436}
437
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800438void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
439 LayerFESet& layerFESet) {
440 ATRACE_CALL();
441 ALOGV(__FUNCTION__);
442
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700443 auto& outputState = editState();
444
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800445 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700446 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800447 return;
448 }
449
450 // Process the layers to determine visibility and coverage
451 compositionengine::Output::CoverageState coverage{layerFESet};
452 collectVisibleLayers(refreshArgs, coverage);
453
454 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700455 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000456 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800457 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
458
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700459 outputState.undefinedRegion = undefinedRegion;
460 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461}
462
463void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
464 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465 // Evaluate the layers from front to back to determine what is visible. This
466 // also incrementally calculates the coverage information for each layer as
467 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800468 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700469 // Incrementally process the coverage for each layer
470 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800471
472 // TODO(b/121291683): Stop early if the output is completely covered and
473 // no more layers could even be visible underneath the ones on top.
474 }
475
Lloyd Pique01c77c12019-04-17 12:48:32 -0700476 setReleasedLayers(refreshArgs);
477
478 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800479}
480
Lloyd Piquede196652020-01-22 17:29:58 -0800481void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700482 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800483 // Ensure we have a snapshot of the basic geometry layer state. Limit the
484 // snapshots to once per frame for each candidate layer, as layers may
485 // appear on multiple outputs.
486 if (!coverage.latchedLayers.count(layerFE)) {
487 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800488 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800489 }
490
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700491 // Only consider the layers on this output
492 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800493 return;
494 }
495
496 // Obtain a read-only pointer to the front-end layer state
497 const auto* layerFEState = layerFE->getCompositionState();
498 if (CC_UNLIKELY(!layerFEState)) {
499 return;
500 }
501
502 // handle hidden surfaces by setting the visible region to empty
503 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700504 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800505 }
506
507 /*
508 * opaqueRegion: area of a surface that is fully opaque.
509 */
510 Region opaqueRegion;
511
512 /*
513 * visibleRegion: area of a surface that is visible on screen and not fully
514 * transparent. This is essentially the layer's footprint minus the opaque
515 * regions above it. Areas covered by a translucent surface are considered
516 * visible.
517 */
518 Region visibleRegion;
519
520 /*
521 * coveredRegion: area of a surface that is covered by all visible regions
522 * above it (which includes the translucent areas).
523 */
524 Region coveredRegion;
525
526 /*
527 * transparentRegion: area of a surface that is hinted to be completely
528 * transparent. This is only used to tell when the layer has no visible non-
529 * transparent regions and can be removed from the layer list. It does not
530 * affect the visibleRegion of this layer or any layers beneath it. The hint
531 * may not be correct if apps don't respect the SurfaceView restrictions
532 * (which, sadly, some don't).
533 */
534 Region transparentRegion;
535
Vishnu Naira483b4a2019-12-12 15:07:52 -0800536 /*
537 * shadowRegion: Region cast by the layer's shadow.
538 */
539 Region shadowRegion;
540
Lloyd Piquede196652020-01-22 17:29:58 -0800541 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800542
543 // Get the visible region
544 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
545 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800546 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800547 visibleRegion.set(visibleRect);
548
Lloyd Piquede196652020-01-22 17:29:58 -0800549 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800550 // if the layer casts a shadow, offset the layers visible region and
551 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800552 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800553 Rect visibleRectWithShadows(visibleRect);
554 visibleRectWithShadows.inset(inset, inset, inset, inset);
555 visibleRegion.set(visibleRectWithShadows);
556 shadowRegion = visibleRegion.subtract(visibleRect);
557 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800558
559 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700560 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800561 }
562
563 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800564 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800565 if (tr.preserveRects()) {
566 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800567 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800568 } else {
569 // transformation too complex, can't do the
570 // transparent region optimization.
571 transparentRegion.clear();
572 }
573 }
574
575 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800576 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800577 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800578 // If we one of the simple category of transforms (0/90/180/270 rotation
579 // + any flip), then the opaque region is the layer's footprint.
580 // Otherwise we don't try and compute the opaque region since there may
581 // be errors at the edges, and we treat the entire layer as
582 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800583 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800584 }
585
586 // Clip the covered region to the visible region
587 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
588
589 // Update accumAboveCoveredLayers for next (lower) layer
590 coverage.aboveCoveredLayers.orSelf(visibleRegion);
591
592 // subtract the opaque region covered by the layers above us
593 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
594
595 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700596 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800597 }
598
599 // Get coverage information for the layer as previously displayed,
600 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800601 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700602 auto prevOutputLayer =
603 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800604
605 // Get coverage information for the layer as previously displayed
606 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
607 const Region kEmptyRegion;
608 const Region& oldVisibleRegion =
609 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
610 const Region& oldCoveredRegion =
611 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
612
613 // compute this layer's dirty region
614 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800615 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800616 // we need to invalidate the whole region
617 dirty = visibleRegion;
618 // as well, as the old visible region
619 dirty.orSelf(oldVisibleRegion);
620 } else {
621 /* compute the exposed region:
622 * the exposed region consists of two components:
623 * 1) what's VISIBLE now and was COVERED before
624 * 2) what's EXPOSED now less what was EXPOSED before
625 *
626 * note that (1) is conservative, we start with the whole visible region
627 * but only keep what used to be covered by something -- which mean it
628 * may have been exposed.
629 *
630 * (2) handles areas that were not covered by anything but got exposed
631 * because of a resize.
632 *
633 */
634 const Region newExposed = visibleRegion - coveredRegion;
635 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
636 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
637 }
638 dirty.subtractSelf(coverage.aboveOpaqueLayers);
639
640 // accumulate to the screen dirty region
641 coverage.dirtyRegion.orSelf(dirty);
642
643 // Update accumAboveOpaqueLayers for next (lower) layer
644 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
645
646 // Compute the visible non-transparent region
647 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
648
Vishnu Naira483b4a2019-12-12 15:07:52 -0800649 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800650 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700651 const auto& outputState = getState();
652 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000653 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800654 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700655 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800656 }
657
Vishnu Naira483b4a2019-12-12 15:07:52 -0800658 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
659
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800660 // The layer is visible. Either reuse the existing outputLayer if we have
661 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800662 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800663
664 // Store the layer coverage information into the layer state as some of it
665 // is useful later.
666 auto& outputLayerState = result->editState();
667 outputLayerState.visibleRegion = visibleRegion;
668 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
669 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200670 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000671 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800672 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800673}
674
675void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
676 // The base class does nothing with this call.
677}
678
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800679void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700680 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800681 layer->getLayerFE().prepareCompositionState(
682 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
683 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800684 }
685}
686
Dan Stoza269dc4d2021-01-15 15:07:43 -0800687void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800688 ATRACE_CALL();
689 ALOGV(__FUNCTION__);
690
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800691 if (!getState().isEnabled) {
692 return;
693 }
694
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800695 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
696 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
697
Lloyd Pique01c77c12019-04-17 12:48:32 -0700698 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700699 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800700 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200701 forceClientComposition,
702 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800703
704 if (mLayerRequestingBackgroundBlur == layer) {
705 forceClientComposition = false;
706 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800707 }
708}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800709
Dan Stoza269dc4d2021-01-15 15:07:43 -0800710void Output::planComposition() {
711 if (!mPlanner || !getState().isEnabled) {
712 return;
713 }
714
715 ATRACE_CALL();
716 ALOGV(__FUNCTION__);
717
718 mPlanner->plan(getOutputLayersOrderedByZ());
719}
720
721void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
722 ATRACE_CALL();
723 ALOGV(__FUNCTION__);
724
725 if (!getState().isEnabled) {
726 return;
727 }
728
Ady Abraham3645e642021-04-20 18:39:00 -0700729 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700730 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham3645e642021-04-20 18:39:00 -0700731
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400732 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800733 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400734 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400735 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400736 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800737 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400738 if (layer == peekThroughLayer) {
739 // No longer needed, although it should not show up again, so
740 // resetting it is not truly needed either.
741 peekThroughLayer = nullptr;
742
743 // peekThroughLayer was already drawn ahead of its z order.
744 continue;
745 }
Dan Stoza6166c312021-01-15 16:34:05 -0800746 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400747 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400748 if (overrideInfo.buffer != nullptr) {
749 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800750 ALOGV("Skipping redundant buffer");
751 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400752 } else {
753 // First layer with the override buffer.
754 if (overrideInfo.peekThroughLayer) {
755 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400756
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400757 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400758 overrideZ = true;
759 includeGeometry = true;
760 constexpr bool isPeekingThrough = true;
761 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
762 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400763 }
764
765 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800766 }
Dan Stoza6166c312021-01-15 16:34:05 -0800767 }
768
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400769 constexpr bool isPeekingThrough = false;
770 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800771 }
772}
773
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800774compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
775 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
776 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100777 auto* compState = layer->getLayerFE().getCompositionState();
778
779 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
780 // want to force client composition because of the blur.
781 if (compState->sidebandStream != nullptr) {
782 return nullptr;
783 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000784 if (compState->isOpaque) {
785 continue;
786 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100787 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800788 layerRequestingBgComposition = layer;
789 }
790 }
791 return layerRequestingBgComposition;
792}
793
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800794void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
795 setColorProfile(pickColorProfile(refreshArgs));
796}
797
798// Returns a data space that fits all visible layers. The returned data space
799// can only be one of
800// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
801// - Dataspace::DISPLAY_P3
802// - Dataspace::DISPLAY_BT2020
803// The returned HDR data space is one of
804// - Dataspace::UNKNOWN
805// - Dataspace::BT2020_HLG
806// - Dataspace::BT2020_PQ
807ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
808 bool* outIsHdrClientComposition) const {
809 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
810 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
811
Lloyd Pique01c77c12019-04-17 12:48:32 -0700812 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800813 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800814 case ui::Dataspace::V0_SCRGB:
815 case ui::Dataspace::V0_SCRGB_LINEAR:
816 case ui::Dataspace::BT2020:
817 case ui::Dataspace::BT2020_ITU:
818 case ui::Dataspace::BT2020_LINEAR:
819 case ui::Dataspace::DISPLAY_BT2020:
820 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
821 break;
822 case ui::Dataspace::DISPLAY_P3:
823 bestDataSpace = ui::Dataspace::DISPLAY_P3;
824 break;
825 case ui::Dataspace::BT2020_PQ:
826 case ui::Dataspace::BT2020_ITU_PQ:
827 bestDataSpace = ui::Dataspace::DISPLAY_P3;
828 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800829 *outIsHdrClientComposition =
830 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800831 break;
832 case ui::Dataspace::BT2020_HLG:
833 case ui::Dataspace::BT2020_ITU_HLG:
834 bestDataSpace = ui::Dataspace::DISPLAY_P3;
835 // When there's mixed PQ content and HLG content, we set the HDR
836 // data space to be BT2020_PQ and convert HLG to PQ.
837 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
838 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
839 }
840 break;
841 default:
842 break;
843 }
844 }
845
846 return bestDataSpace;
847}
848
849compositionengine::Output::ColorProfile Output::pickColorProfile(
850 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
851 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
852 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
853 ui::RenderIntent::COLORIMETRIC,
854 refreshArgs.colorSpaceAgnosticDataspace};
855 }
856
857 ui::Dataspace hdrDataSpace;
858 bool isHdrClientComposition = false;
859 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
860
861 switch (refreshArgs.forceOutputColorMode) {
862 case ui::ColorMode::SRGB:
863 bestDataSpace = ui::Dataspace::V0_SRGB;
864 break;
865 case ui::ColorMode::DISPLAY_P3:
866 bestDataSpace = ui::Dataspace::DISPLAY_P3;
867 break;
868 default:
869 break;
870 }
871
872 // respect hdrDataSpace only when there is no legacy HDR support
873 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
874 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
875 if (isHdr) {
876 bestDataSpace = hdrDataSpace;
877 }
878
879 ui::RenderIntent intent;
880 switch (refreshArgs.outputColorSetting) {
881 case OutputColorSetting::kManaged:
882 case OutputColorSetting::kUnmanaged:
883 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
884 : ui::RenderIntent::COLORIMETRIC;
885 break;
886 case OutputColorSetting::kEnhanced:
887 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
888 break;
889 default: // vendor display color setting
890 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
891 break;
892 }
893
894 ui::ColorMode outMode;
895 ui::Dataspace outDataSpace;
896 ui::RenderIntent outRenderIntent;
897 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
898 &outRenderIntent);
899
900 return ColorProfile{outMode, outDataSpace, outRenderIntent,
901 refreshArgs.colorSpaceAgnosticDataspace};
902}
903
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800904void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700905 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700906 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700907 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700908 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800909
910 // If nothing has changed (!dirty), don't recompose.
911 // If something changed, but we don't currently have any visible layers,
912 // and didn't when we last did a composition, then skip it this time.
913 // The second rule does two things:
914 // - When all layers are removed from a display, we'll emit one black
915 // frame, then nothing more until we get new layers.
916 // - When a display is created with a private layer stack, we won't
917 // emit any black frames until a layer is added to the layer stack.
918 const bool mustRecompose = dirty && !(empty && wasEmpty);
919
920 const char flagPrefix[] = {'-', '+'};
921 static_cast<void>(flagPrefix);
922 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
923 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
924 flagPrefix[empty], flagPrefix[wasEmpty]);
925
926 mRenderSurface->beginFrame(mustRecompose);
927
928 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700929 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800930 }
931}
932
Lloyd Pique66d68602019-02-13 14:23:31 -0800933void Output::prepareFrame() {
934 ATRACE_CALL();
935 ALOGV(__FUNCTION__);
936
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700937 const auto& outputState = getState();
938 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800939 return;
940 }
941
942 chooseCompositionStrategy();
943
Dan Stoza47437bb2021-01-15 16:21:07 -0800944 if (mPlanner) {
945 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
946 }
947
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700948 mRenderSurface->prepareFrame(outputState.usesClientComposition,
949 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800950}
951
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800952void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
953 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
954 return;
955 }
956
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700957 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700958 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800959 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700960 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800961 }
962 }
963
964 postFramebuffer();
965
966 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
967
968 prepareFrame();
969}
970
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800971void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800972 ATRACE_CALL();
973 ALOGV(__FUNCTION__);
974
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700975 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800976 return;
977 }
978
979 // Repaint the framebuffer (if needed), getting the optional fence for when
980 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800981 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800982 if (!optReadyFence) {
983 return;
984 }
985
986 // swap buffers (presentation)
987 mRenderSurface->queueBuffer(std::move(*optReadyFence));
988}
989
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800990std::optional<base::unique_fd> Output::composeSurfaces(
991 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800992 ATRACE_CALL();
993 ALOGV(__FUNCTION__);
994
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700995 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800996 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800997 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700998 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700999
1000 auto& renderEngine = getCompositionEngine().getRenderEngine();
1001 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1002
1003 // If we the display is secure, protected content support is enabled, and at
1004 // least one layer has protected content, we need to use a secure back
1005 // buffer.
1006 if (outputState.isSecure && supportsProtectedContent) {
1007 auto layers = getOutputLayersOrderedByZ();
1008 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1009 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1010 });
1011 if (needsProtected != renderEngine.isProtected()) {
1012 renderEngine.useProtectedContext(needsProtected);
1013 }
1014 if (needsProtected != mRenderSurface->isProtected() &&
1015 needsProtected == renderEngine.isProtected()) {
1016 mRenderSurface->setProtected(needsProtected);
1017 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001018 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1019 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001020 }
1021
1022 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001023
1024 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001025
1026 // If we aren't doing client composition on this output, but do have a
1027 // flipClientTarget request for this frame on this output, we still need to
1028 // dequeue a buffer.
1029 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001030 tex = mRenderSurface->dequeueBuffer(&fd);
1031 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001032 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1033 "client composition for this frame",
1034 mName.c_str());
1035 return {};
1036 }
1037 }
1038
Lloyd Pique688abd42019-02-15 15:42:24 -08001039 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001040 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001041 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001042 }
1043
1044 ALOGV("hasClientComposition");
1045
Lloyd Pique688abd42019-02-15 15:42:24 -08001046 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001047 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1048 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001049 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001050 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001051 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1052 ? outputState.dataspace
1053 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001054
1055 // If we have a valid current display brightness use that, otherwise fall back to the
1056 // display's max desired
1057 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1058 ? outputState.displayBrightnessNits
1059 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1060 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001061
1062 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001063 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1064 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001065 }
1066
Lloyd Pique688abd42019-02-15 15:42:24 -08001067 // Generate the client composition requests for the layers on this output.
Robert Carrccab4242021-09-28 16:53:03 -07001068 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001069 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001070 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001071 clientCompositionDisplay.outputDataspace,
1072 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001073 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1074
Vishnu Nair9b079a22020-01-21 14:36:08 -08001075 // Check if the client composition requests were rendered into the provided graphic buffer. If
1076 // so, we can reuse the buffer and avoid client composition.
1077 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001078 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1079 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001080 clientCompositionLayers)) {
1081 outputCompositionState.reusedClientComposition = true;
1082 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001083 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001084 }
Alec Mouria90a5702021-04-16 16:36:21 +00001085 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001086 clientCompositionLayers);
1087 }
1088
Lloyd Pique688abd42019-02-15 15:42:24 -08001089 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001090 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1091 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1092 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001093 const bool expensiveBlurs =
1094 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001095 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001096 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001097 if (expensiveRenderingExpected) {
1098 setExpensiveRenderingExpected(true);
1099 }
1100
Sally Qi59a9f502021-10-12 18:53:23 +00001101 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1102 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001103 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001104 std::back_inserter(clientRenderEngineLayers),
1105 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1106 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001107 });
1108
Alec Mourie4034bb2019-11-19 12:45:54 -08001109 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001110 // Only use the framebuffer cache when rendering to an internal display
1111 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1112 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1113 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1114 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1115 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001116 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001117 auto [status, drawFence] =
1118 renderEngine
Sally Qi59a9f502021-10-12 18:53:23 +00001119 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Sally Qi4cabdd02021-08-05 16:45:57 -07001120 useFramebufferCache, std::move(fd))
1121 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001122
1123 if (status != NO_ERROR && mClientCompositionRequestCache) {
1124 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001125 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001126 }
1127
Alec Mourie4034bb2019-11-19 12:45:54 -08001128 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001129 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001130 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1131 } else {
1132 timeStats.recordRenderEngineDuration(renderEngineStart,
1133 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001134 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001135 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001136
Robert Carrccab4242021-09-28 16:53:03 -07001137 if (clientCompositionLayersFE.size() > 0) {
1138 sp<Fence> clientCompFence = new Fence(dup(drawFence.get()));
1139 for (auto clientComposedLayer : clientCompositionLayersFE) {
1140 clientComposedLayer->setWasClientComposed(clientCompFence);
1141 }
1142 }
1143
Sally Qi4cabdd02021-08-05 16:45:57 -07001144 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001145}
1146
Vishnu Nair9b079a22020-01-21 14:36:08 -08001147std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001148 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001149 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001150 ALOGV("Rendering client layers");
1151
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001152 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001153 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001154 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001155
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001156 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001157 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001158
Lloyd Pique01c77c12019-04-17 12:48:32 -07001159 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001160 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001161 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001162 auto& layerFE = layer->getLayerFE();
1163
Lloyd Piquea2468662019-03-07 21:31:06 -08001164 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001165 ALOGV("Layer: %s", layerFE.getDebugName());
1166 if (clip.isEmpty()) {
1167 ALOGV(" Skipping for empty clip");
1168 firstLayer = false;
1169 continue;
1170 }
1171
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001172 disableBlurs |= layerFEState->sidebandStream != nullptr;
1173
Vishnu Naira483b4a2019-12-12 15:07:52 -08001174 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001175
1176 // We clear the client target for non-client composed layers if
1177 // requested by the HWC. We skip this if the layer is not an opaque
1178 // rectangle, as by definition the layer must blend with whatever is
1179 // underneath. We also skip the first layer as the buffer target is
1180 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001181 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001182 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001183
1184 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1185
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001186 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1187 // composing the non-shadow content and only draw the shadows.
1188 const bool realContentIsVisible = clientComposition &&
1189 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1190
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001192 std::vector<LayerFE::LayerSettings> results;
1193 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001194 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001195 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001196 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001197 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1198 } else {
1199 ALOGV("Skipping redundant override buffer for [%s] in RE",
1200 layer->getLayerFE().getDebugName());
1201 }
Dan Stoza6166c312021-01-15 16:34:05 -08001202 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001203 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1204 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1205 : (layer->getState().overrideInfo.disableBackgroundBlur
1206 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1207 BlurRegionsOnly
1208 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1209 Enabled);
1210 compositionengine::LayerFE::ClientCompositionTargetSettings
1211 targetSettings{.clip = clip,
1212 .needsFiltering = layer->needsFiltering() ||
1213 outputState.needsFiltering,
1214 .isSecure = outputState.isSecure,
1215 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001216 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001217 .dataspace = outputDataspace,
1218 .realContentIsVisible = realContentIsVisible,
1219 .clearContent = !clientComposition,
1220 .blurSetting = blurSetting};
Dan Stoza6166c312021-01-15 16:34:05 -08001221 results = layerFE.prepareClientCompositionList(targetSettings);
1222 if (realContentIsVisible && !results.empty()) {
1223 layer->editState().clientCompositionTimestamp = systemTime();
1224 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001225 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001226
Robert Carrccab4242021-09-28 16:53:03 -07001227 outLayerFEs.push_back(&layerFE);
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001228 clientCompositionLayers.insert(clientCompositionLayers.end(),
1229 std::make_move_iterator(results.begin()),
1230 std::make_move_iterator(results.end()));
1231 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001232 }
1233
1234 firstLayer = false;
1235 }
1236
1237 return clientCompositionLayers;
1238}
1239
1240void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001241 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001242 if (flashRegion.isEmpty()) {
1243 return;
1244 }
1245
Vishnu Nair9b079a22020-01-21 14:36:08 -08001246 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001247 layerSettings.source.buffer.buffer = nullptr;
1248 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1249 layerSettings.alpha = half(1.0);
1250
1251 for (const auto& rect : flashRegion) {
1252 layerSettings.geometry.boundaries = rect.toFloatRect();
1253 clientCompositionLayers.push_back(layerSettings);
1254 }
1255}
1256
1257void Output::setExpensiveRenderingExpected(bool) {
1258 // The base class does nothing with this call.
1259}
1260
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001261void Output::postFramebuffer() {
1262 ATRACE_CALL();
1263 ALOGV(__FUNCTION__);
1264
1265 if (!getState().isEnabled) {
1266 return;
1267 }
1268
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001269 auto& outputState = editState();
1270 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001271 mRenderSurface->flip();
1272
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001273 auto frame = presentAndGetFrameFences();
1274
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001275 mRenderSurface->onPresentDisplayCompleted();
1276
Lloyd Pique01c77c12019-04-17 12:48:32 -07001277 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001278 // The layer buffer from the previous frame (if any) is released
1279 // by HWC only when the release fence from this frame (if any) is
1280 // signaled. Always get the release fence from HWC first.
1281 sp<Fence> releaseFence = Fence::NO_FENCE;
1282
1283 if (auto hwcLayer = layer->getHwcLayer()) {
1284 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1285 releaseFence = f->second;
1286 }
1287 }
1288
1289 // If the layer was client composited in the previous frame, we
1290 // need to merge with the previous client target acquire fence.
1291 // Since we do not track that, always merge with the current
1292 // client target acquire fence when it is available, even though
1293 // this is suboptimal.
1294 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001295 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001296 releaseFence =
1297 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1298 }
Sally Qi59a9f502021-10-12 18:53:23 +00001299 layer->getLayerFE().onLayerDisplayed(
1300 ftl::yield<renderengine::RenderEngineResult>(
1301 {NO_ERROR, base::unique_fd(releaseFence->dup())})
1302 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001303 }
1304
1305 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001306 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001307 // supply them with the present fence.
1308 for (auto& weakLayer : mReleasedLayers) {
1309 if (auto layer = weakLayer.promote(); layer != nullptr) {
Sally Qi59a9f502021-10-12 18:53:23 +00001310 layer->onLayerDisplayed(ftl::yield<renderengine::RenderEngineResult>(
1311 {NO_ERROR, base::unique_fd(frame.presentFence->dup())})
1312 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001313 }
1314 }
1315
1316 // Clear out the released layers now that we're done with them.
1317 mReleasedLayers.clear();
1318}
1319
Alec Mouriaa831582021-06-07 16:23:01 -07001320void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001321 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001322 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001323 }
1324}
1325
Lloyd Pique32cbe282018-10-19 13:09:22 -07001326void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001327 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001328 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001329}
1330
Lloyd Pique66d68602019-02-13 14:23:31 -08001331void Output::chooseCompositionStrategy() {
1332 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001333 auto& outputState = editState();
1334 outputState.usesClientComposition = true;
1335 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001336 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001337}
1338
Lloyd Pique688abd42019-02-15 15:42:24 -08001339bool Output::getSkipColorTransform() const {
1340 return true;
1341}
1342
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001343compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1344 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001345 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001346 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1347 }
1348 return result;
1349}
1350
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001351} // namespace impl
1352} // namespace android::compositionengine