blob: 6833584e713d1bbeeb82e0ecb29defa2183c7a70 [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 Abraham43065bd2021-12-10 17:22:15 -0800731 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700732
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400733 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800734 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400735 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400736 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400737 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800738 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400739 if (layer == peekThroughLayer) {
740 // No longer needed, although it should not show up again, so
741 // resetting it is not truly needed either.
742 peekThroughLayer = nullptr;
743
744 // peekThroughLayer was already drawn ahead of its z order.
745 continue;
746 }
Dan Stoza6166c312021-01-15 16:34:05 -0800747 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400748 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400749 if (overrideInfo.buffer != nullptr) {
750 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800751 ALOGV("Skipping redundant buffer");
752 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400753 } else {
754 // First layer with the override buffer.
755 if (overrideInfo.peekThroughLayer) {
756 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400757
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400758 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400759 overrideZ = true;
760 includeGeometry = true;
761 constexpr bool isPeekingThrough = true;
762 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
763 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400764 }
765
766 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800767 }
Dan Stoza6166c312021-01-15 16:34:05 -0800768 }
769
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400770 constexpr bool isPeekingThrough = false;
771 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800772 }
773}
774
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800775compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
776 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
777 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100778 auto* compState = layer->getLayerFE().getCompositionState();
779
780 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
781 // want to force client composition because of the blur.
782 if (compState->sidebandStream != nullptr) {
783 return nullptr;
784 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000785 if (compState->isOpaque) {
786 continue;
787 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100788 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800789 layerRequestingBgComposition = layer;
790 }
791 }
792 return layerRequestingBgComposition;
793}
794
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800795void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
796 setColorProfile(pickColorProfile(refreshArgs));
797}
798
799// Returns a data space that fits all visible layers. The returned data space
800// can only be one of
801// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
802// - Dataspace::DISPLAY_P3
803// - Dataspace::DISPLAY_BT2020
804// The returned HDR data space is one of
805// - Dataspace::UNKNOWN
806// - Dataspace::BT2020_HLG
807// - Dataspace::BT2020_PQ
808ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
809 bool* outIsHdrClientComposition) const {
810 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
811 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
812
Lloyd Pique01c77c12019-04-17 12:48:32 -0700813 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800814 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800815 case ui::Dataspace::V0_SCRGB:
816 case ui::Dataspace::V0_SCRGB_LINEAR:
817 case ui::Dataspace::BT2020:
818 case ui::Dataspace::BT2020_ITU:
819 case ui::Dataspace::BT2020_LINEAR:
820 case ui::Dataspace::DISPLAY_BT2020:
821 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
822 break;
823 case ui::Dataspace::DISPLAY_P3:
824 bestDataSpace = ui::Dataspace::DISPLAY_P3;
825 break;
826 case ui::Dataspace::BT2020_PQ:
827 case ui::Dataspace::BT2020_ITU_PQ:
828 bestDataSpace = ui::Dataspace::DISPLAY_P3;
829 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800830 *outIsHdrClientComposition =
831 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800832 break;
833 case ui::Dataspace::BT2020_HLG:
834 case ui::Dataspace::BT2020_ITU_HLG:
835 bestDataSpace = ui::Dataspace::DISPLAY_P3;
836 // When there's mixed PQ content and HLG content, we set the HDR
837 // data space to be BT2020_PQ and convert HLG to PQ.
838 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
839 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
840 }
841 break;
842 default:
843 break;
844 }
845 }
846
847 return bestDataSpace;
848}
849
850compositionengine::Output::ColorProfile Output::pickColorProfile(
851 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
852 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
853 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
854 ui::RenderIntent::COLORIMETRIC,
855 refreshArgs.colorSpaceAgnosticDataspace};
856 }
857
858 ui::Dataspace hdrDataSpace;
859 bool isHdrClientComposition = false;
860 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
861
862 switch (refreshArgs.forceOutputColorMode) {
863 case ui::ColorMode::SRGB:
864 bestDataSpace = ui::Dataspace::V0_SRGB;
865 break;
866 case ui::ColorMode::DISPLAY_P3:
867 bestDataSpace = ui::Dataspace::DISPLAY_P3;
868 break;
869 default:
870 break;
871 }
872
873 // respect hdrDataSpace only when there is no legacy HDR support
874 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
875 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
876 if (isHdr) {
877 bestDataSpace = hdrDataSpace;
878 }
879
880 ui::RenderIntent intent;
881 switch (refreshArgs.outputColorSetting) {
882 case OutputColorSetting::kManaged:
883 case OutputColorSetting::kUnmanaged:
884 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
885 : ui::RenderIntent::COLORIMETRIC;
886 break;
887 case OutputColorSetting::kEnhanced:
888 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
889 break;
890 default: // vendor display color setting
891 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
892 break;
893 }
894
895 ui::ColorMode outMode;
896 ui::Dataspace outDataSpace;
897 ui::RenderIntent outRenderIntent;
898 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
899 &outRenderIntent);
900
901 return ColorProfile{outMode, outDataSpace, outRenderIntent,
902 refreshArgs.colorSpaceAgnosticDataspace};
903}
904
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800905void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700906 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700907 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700908 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700909 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800910
911 // If nothing has changed (!dirty), don't recompose.
912 // If something changed, but we don't currently have any visible layers,
913 // and didn't when we last did a composition, then skip it this time.
914 // The second rule does two things:
915 // - When all layers are removed from a display, we'll emit one black
916 // frame, then nothing more until we get new layers.
917 // - When a display is created with a private layer stack, we won't
918 // emit any black frames until a layer is added to the layer stack.
919 const bool mustRecompose = dirty && !(empty && wasEmpty);
920
921 const char flagPrefix[] = {'-', '+'};
922 static_cast<void>(flagPrefix);
923 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
924 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
925 flagPrefix[empty], flagPrefix[wasEmpty]);
926
927 mRenderSurface->beginFrame(mustRecompose);
928
929 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700930 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800931 }
932}
933
Lloyd Pique66d68602019-02-13 14:23:31 -0800934void Output::prepareFrame() {
935 ATRACE_CALL();
936 ALOGV(__FUNCTION__);
937
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700938 const auto& outputState = getState();
939 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800940 return;
941 }
942
943 chooseCompositionStrategy();
944
Dan Stoza47437bb2021-01-15 16:21:07 -0800945 if (mPlanner) {
946 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
947 }
948
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700949 mRenderSurface->prepareFrame(outputState.usesClientComposition,
950 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800951}
952
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800953void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
954 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
955 return;
956 }
957
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700958 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700959 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800960 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700961 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800962 }
963 }
964
965 postFramebuffer();
966
967 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
968
969 prepareFrame();
970}
971
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800972void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800973 ATRACE_CALL();
974 ALOGV(__FUNCTION__);
975
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700976 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800977 return;
978 }
979
980 // Repaint the framebuffer (if needed), getting the optional fence for when
981 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800982 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800983 if (!optReadyFence) {
984 return;
985 }
986
987 // swap buffers (presentation)
988 mRenderSurface->queueBuffer(std::move(*optReadyFence));
989}
990
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800991std::optional<base::unique_fd> Output::composeSurfaces(
992 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800993 ATRACE_CALL();
994 ALOGV(__FUNCTION__);
995
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700996 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800997 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800998 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700999 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -07001000
1001 auto& renderEngine = getCompositionEngine().getRenderEngine();
1002 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1003
1004 // If we the display is secure, protected content support is enabled, and at
1005 // least one layer has protected content, we need to use a secure back
1006 // buffer.
1007 if (outputState.isSecure && supportsProtectedContent) {
1008 auto layers = getOutputLayersOrderedByZ();
1009 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1010 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1011 });
1012 if (needsProtected != renderEngine.isProtected()) {
1013 renderEngine.useProtectedContext(needsProtected);
1014 }
1015 if (needsProtected != mRenderSurface->isProtected() &&
1016 needsProtected == renderEngine.isProtected()) {
1017 mRenderSurface->setProtected(needsProtected);
1018 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001019 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1020 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001021 }
1022
1023 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001024
1025 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001026
1027 // If we aren't doing client composition on this output, but do have a
1028 // flipClientTarget request for this frame on this output, we still need to
1029 // dequeue a buffer.
1030 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001031 tex = mRenderSurface->dequeueBuffer(&fd);
1032 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001033 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1034 "client composition for this frame",
1035 mName.c_str());
1036 return {};
1037 }
1038 }
1039
Lloyd Pique688abd42019-02-15 15:42:24 -08001040 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001041 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001042 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001043 }
1044
1045 ALOGV("hasClientComposition");
1046
Lloyd Pique688abd42019-02-15 15:42:24 -08001047 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001048 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1049 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001050 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001051 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001052 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1053 ? outputState.dataspace
1054 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001055
1056 // If we have a valid current display brightness use that, otherwise fall back to the
1057 // display's max desired
1058 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1059 ? outputState.displayBrightnessNits
1060 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001061 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001062
1063 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001064 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1065 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001066 }
1067
Lloyd Pique688abd42019-02-15 15:42:24 -08001068 // Generate the client composition requests for the layers on this output.
Robert Carrccab4242021-09-28 16:53:03 -07001069 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001070 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001071 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001072 clientCompositionDisplay.outputDataspace,
1073 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001074 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1075
Vishnu Nair9b079a22020-01-21 14:36:08 -08001076 // Check if the client composition requests were rendered into the provided graphic buffer. If
1077 // so, we can reuse the buffer and avoid client composition.
1078 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001079 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1080 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001081 clientCompositionLayers)) {
1082 outputCompositionState.reusedClientComposition = true;
1083 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001084 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001085 }
Alec Mouria90a5702021-04-16 16:36:21 +00001086 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001087 clientCompositionLayers);
1088 }
1089
Lloyd Pique688abd42019-02-15 15:42:24 -08001090 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001091 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1092 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1093 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001094 const bool expensiveBlurs =
1095 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001096 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001097 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001098 if (expensiveRenderingExpected) {
1099 setExpensiveRenderingExpected(true);
1100 }
1101
Sally Qi59a9f502021-10-12 18:53:23 +00001102 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1103 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001104 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001105 std::back_inserter(clientRenderEngineLayers),
1106 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1107 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001108 });
1109
Alec Mourie4034bb2019-11-19 12:45:54 -08001110 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001111 // Only use the framebuffer cache when rendering to an internal display
1112 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1113 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1114 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1115 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1116 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001117 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001118 auto [status, drawFence] =
1119 renderEngine
Sally Qi59a9f502021-10-12 18:53:23 +00001120 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Sally Qi4cabdd02021-08-05 16:45:57 -07001121 useFramebufferCache, std::move(fd))
1122 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001123
1124 if (status != NO_ERROR && mClientCompositionRequestCache) {
1125 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001126 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001127 }
1128
Alec Mourie4034bb2019-11-19 12:45:54 -08001129 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001130 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001131 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1132 } else {
1133 timeStats.recordRenderEngineDuration(renderEngineStart,
1134 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001135 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001136 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001137
Robert Carrccab4242021-09-28 16:53:03 -07001138 if (clientCompositionLayersFE.size() > 0) {
1139 sp<Fence> clientCompFence = new Fence(dup(drawFence.get()));
1140 for (auto clientComposedLayer : clientCompositionLayersFE) {
1141 clientComposedLayer->setWasClientComposed(clientCompFence);
1142 }
1143 }
1144
Sally Qi4cabdd02021-08-05 16:45:57 -07001145 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001146}
1147
Vishnu Nair9b079a22020-01-21 14:36:08 -08001148std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001149 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001150 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001151 ALOGV("Rendering client layers");
1152
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001153 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001154 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001155 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001156
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001157 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001158 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001159
Lloyd Pique01c77c12019-04-17 12:48:32 -07001160 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001161 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001162 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001163 auto& layerFE = layer->getLayerFE();
1164
Lloyd Piquea2468662019-03-07 21:31:06 -08001165 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001166 ALOGV("Layer: %s", layerFE.getDebugName());
1167 if (clip.isEmpty()) {
1168 ALOGV(" Skipping for empty clip");
1169 firstLayer = false;
1170 continue;
1171 }
1172
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001173 disableBlurs |= layerFEState->sidebandStream != nullptr;
1174
Vishnu Naira483b4a2019-12-12 15:07:52 -08001175 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001176
1177 // We clear the client target for non-client composed layers if
1178 // requested by the HWC. We skip this if the layer is not an opaque
1179 // rectangle, as by definition the layer must blend with whatever is
1180 // underneath. We also skip the first layer as the buffer target is
1181 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001182 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001183 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001184
1185 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1186
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001187 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1188 // composing the non-shadow content and only draw the shadows.
1189 const bool realContentIsVisible = clientComposition &&
1190 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1191
Lloyd Pique688abd42019-02-15 15:42:24 -08001192 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001193 std::vector<LayerFE::LayerSettings> results;
1194 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001195 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001196 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001197 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001198 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1199 } else {
1200 ALOGV("Skipping redundant override buffer for [%s] in RE",
1201 layer->getLayerFE().getDebugName());
1202 }
Dan Stoza6166c312021-01-15 16:34:05 -08001203 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001204 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1205 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1206 : (layer->getState().overrideInfo.disableBackgroundBlur
1207 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1208 BlurRegionsOnly
1209 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1210 Enabled);
1211 compositionengine::LayerFE::ClientCompositionTargetSettings
1212 targetSettings{.clip = clip,
1213 .needsFiltering = layer->needsFiltering() ||
1214 outputState.needsFiltering,
1215 .isSecure = outputState.isSecure,
1216 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001217 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001218 .dataspace = outputDataspace,
1219 .realContentIsVisible = realContentIsVisible,
1220 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001221 .blurSetting = blurSetting,
1222 .whitePointNits = layerState.whitePointNits};
Dan Stoza6166c312021-01-15 16:34:05 -08001223 results = layerFE.prepareClientCompositionList(targetSettings);
1224 if (realContentIsVisible && !results.empty()) {
1225 layer->editState().clientCompositionTimestamp = systemTime();
1226 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001227 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001228
Robert Carrccab4242021-09-28 16:53:03 -07001229 outLayerFEs.push_back(&layerFE);
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001230 clientCompositionLayers.insert(clientCompositionLayers.end(),
1231 std::make_move_iterator(results.begin()),
1232 std::make_move_iterator(results.end()));
1233 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001234 }
1235
1236 firstLayer = false;
1237 }
1238
1239 return clientCompositionLayers;
1240}
1241
1242void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001243 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001244 if (flashRegion.isEmpty()) {
1245 return;
1246 }
1247
Vishnu Nair9b079a22020-01-21 14:36:08 -08001248 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001249 layerSettings.source.buffer.buffer = nullptr;
1250 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1251 layerSettings.alpha = half(1.0);
1252
1253 for (const auto& rect : flashRegion) {
1254 layerSettings.geometry.boundaries = rect.toFloatRect();
1255 clientCompositionLayers.push_back(layerSettings);
1256 }
1257}
1258
1259void Output::setExpensiveRenderingExpected(bool) {
1260 // The base class does nothing with this call.
1261}
1262
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001263void Output::postFramebuffer() {
1264 ATRACE_CALL();
1265 ALOGV(__FUNCTION__);
1266
1267 if (!getState().isEnabled) {
1268 return;
1269 }
1270
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001271 auto& outputState = editState();
1272 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001273 mRenderSurface->flip();
1274
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001275 auto frame = presentAndGetFrameFences();
1276
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001277 mRenderSurface->onPresentDisplayCompleted();
1278
Lloyd Pique01c77c12019-04-17 12:48:32 -07001279 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001280 // The layer buffer from the previous frame (if any) is released
1281 // by HWC only when the release fence from this frame (if any) is
1282 // signaled. Always get the release fence from HWC first.
1283 sp<Fence> releaseFence = Fence::NO_FENCE;
1284
1285 if (auto hwcLayer = layer->getHwcLayer()) {
1286 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1287 releaseFence = f->second;
1288 }
1289 }
1290
1291 // If the layer was client composited in the previous frame, we
1292 // need to merge with the previous client target acquire fence.
1293 // Since we do not track that, always merge with the current
1294 // client target acquire fence when it is available, even though
1295 // this is suboptimal.
1296 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001297 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001298 releaseFence =
1299 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1300 }
Sally Qi59a9f502021-10-12 18:53:23 +00001301 layer->getLayerFE().onLayerDisplayed(
1302 ftl::yield<renderengine::RenderEngineResult>(
1303 {NO_ERROR, base::unique_fd(releaseFence->dup())})
1304 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001305 }
1306
1307 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001308 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001309 // supply them with the present fence.
1310 for (auto& weakLayer : mReleasedLayers) {
1311 if (auto layer = weakLayer.promote(); layer != nullptr) {
Sally Qi59a9f502021-10-12 18:53:23 +00001312 layer->onLayerDisplayed(ftl::yield<renderengine::RenderEngineResult>(
1313 {NO_ERROR, base::unique_fd(frame.presentFence->dup())})
1314 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001315 }
1316 }
1317
1318 // Clear out the released layers now that we're done with them.
1319 mReleasedLayers.clear();
1320}
1321
Alec Mouriaa831582021-06-07 16:23:01 -07001322void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001323 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001324 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001325 }
1326}
1327
Lloyd Pique32cbe282018-10-19 13:09:22 -07001328void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001329 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001330 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001331}
1332
Lloyd Pique66d68602019-02-13 14:23:31 -08001333void Output::chooseCompositionStrategy() {
1334 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001335 auto& outputState = editState();
1336 outputState.usesClientComposition = true;
1337 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001338 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001339}
1340
Lloyd Pique688abd42019-02-15 15:42:24 -08001341bool Output::getSkipColorTransform() const {
1342 return true;
1343}
1344
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001345compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1346 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001347 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001348 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1349 }
1350 return result;
1351}
1352
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001353} // namespace impl
1354} // namespace android::compositionengine