blob: 162d84ef3135a832fa33f3096b5d6a684e00b3fe [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
Alec Mouricdf16792021-12-10 13:16:06 -0800200void Output::setNextBrightness(float brightness) {
201 editState().displayBrightness = brightness;
202}
203
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200204void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700205 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200206
207 auto& state = editState();
208
209 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000210 const ui::Size newBounds(size);
211 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200212
213 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000214 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200215 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
216
217 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000218 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200219 ui::Size orientedSize = size;
220 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
221 std::swap(orientedSize.width, orientedSize.height);
222 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000223 const ui::Size newOrientedBounds(orientedSize);
224 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700225
Dan Stoza6166c312021-01-15 16:34:05 -0800226 if (mPlanner) {
227 mPlanner->setDisplaySize(size);
228 }
229
Lloyd Pique32cbe282018-10-19 13:09:22 -0700230 dirtyEntireOutput();
231}
232
Garfield Tan54edd912020-10-21 16:31:41 -0700233ui::Transform::RotationFlags Output::getTransformHint() const {
234 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
235}
236
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700237void Output::setLayerFilter(ui::LayerFilter filter) {
238 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700239 dirtyEntireOutput();
240}
241
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800242void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700243 auto& colorTransformMatrix = editState().colorTransformMatrix;
244 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700245 return;
246 }
247
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700248 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800249
250 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700251}
252
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800253void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700254 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800255 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
256 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800257
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700258 auto& outputState = editState();
259 if (outputState.colorMode == colorProfile.mode &&
260 outputState.dataspace == colorProfile.dataspace &&
261 outputState.renderIntent == colorProfile.renderIntent &&
262 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800263 return;
264 }
265
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700266 outputState.colorMode = colorProfile.mode;
267 outputState.dataspace = colorProfile.dataspace;
268 outputState.renderIntent = colorProfile.renderIntent;
269 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700270
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800271 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700272
Lloyd Pique32cbe282018-10-19 13:09:22 -0700273 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800274 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
275 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800276
277 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700278}
279
John Reckac09e452021-04-07 16:35:37 -0400280void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
281 auto& outputState = editState();
282 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
283 outputState.displayBrightnessNits == displayBrightnessNits) {
284 // Nothing changed
285 return;
286 }
287 outputState.sdrWhitePointNits = sdrWhitePointNits;
288 outputState.displayBrightnessNits = displayBrightnessNits;
289 dirtyEntireOutput();
290}
291
Lloyd Pique32cbe282018-10-19 13:09:22 -0700292void Output::dump(std::string& out) const {
293 using android::base::StringAppendF;
294
295 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
296
297 out.append("\n ");
298
299 dumpBase(out);
300}
301
302void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700303 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700304
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700305 if (mDisplayColorProfile) {
306 mDisplayColorProfile->dump(out);
307 } else {
308 out.append(" No display color profile!\n");
309 }
310
Lloyd Pique31cb2942018-10-19 17:23:03 -0700311 if (mRenderSurface) {
312 mRenderSurface->dump(out);
313 } else {
314 out.append(" No render surface!\n");
315 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800316
Lloyd Pique01c77c12019-04-17 12:48:32 -0700317 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
318 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800319 if (!outputLayer) {
320 continue;
321 }
322 outputLayer->dump(out);
323 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700324}
325
Dan Stoza269dc4d2021-01-15 15:07:43 -0800326void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
327 if (!mPlanner) {
328 base::StringAppendF(&out, "Planner is disabled\n");
329 return;
330 }
331 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
332 mPlanner->dump(args, out);
333}
334
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700335compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
336 return mDisplayColorProfile.get();
337}
338
339void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
340 mDisplayColorProfile = std::move(mode);
341}
342
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800343const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
344 return mReleasedLayers;
345}
346
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700347void Output::setDisplayColorProfileForTest(
348 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
349 mDisplayColorProfile = std::move(mode);
350}
351
Lloyd Pique31cb2942018-10-19 17:23:03 -0700352compositionengine::RenderSurface* Output::getRenderSurface() const {
353 return mRenderSurface.get();
354}
355
356void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
357 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800358 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000359 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800360 if (mPlanner) {
361 mPlanner->setDisplaySize(size);
362 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700363 dirtyEntireOutput();
364}
365
Vishnu Nair9b079a22020-01-21 14:36:08 -0800366void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
367 if (cacheSize == 0) {
368 mClientCompositionRequestCache.reset();
369 } else {
370 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
371 }
372};
373
Lloyd Pique31cb2942018-10-19 17:23:03 -0700374void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
375 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700376}
377
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700378Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700379 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000380 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700381}
382
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700383bool Output::includesLayer(ui::LayerFilter filter) const {
384 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700385}
386
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700387bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800388 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700389 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800390}
391
Lloyd Piquedf336d92019-03-07 21:38:42 -0800392std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800393 const sp<LayerFE>& layerFE) const {
394 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800395}
396
Lloyd Piquede196652020-01-22 17:29:58 -0800397compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
398 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700399 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800400}
401
Lloyd Pique01c77c12019-04-17 12:48:32 -0700402std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800403 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700404 for (size_t i = 0; i < getOutputLayerCount(); i++) {
405 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800406 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700407 return i;
408 }
409 }
410 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800411}
412
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800413void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
414 mReleasedLayers = std::move(layers);
415}
416
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800417void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
418 LayerFESet& geomSnapshots) {
419 ATRACE_CALL();
420 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800421
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800422 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800423}
424
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800425void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800426 ATRACE_CALL();
427 ALOGV(__FUNCTION__);
428
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800429 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800430 updateCompositionState(refreshArgs);
431 planComposition();
432 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800433 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800434 beginFrame();
435 prepareFrame();
436 devOptRepaintFlash(refreshArgs);
437 finishFrame(refreshArgs);
438 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700439 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800440}
441
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800442void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
443 LayerFESet& layerFESet) {
444 ATRACE_CALL();
445 ALOGV(__FUNCTION__);
446
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700447 auto& outputState = editState();
448
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800449 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700450 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800451 return;
452 }
453
454 // Process the layers to determine visibility and coverage
455 compositionengine::Output::CoverageState coverage{layerFESet};
456 collectVisibleLayers(refreshArgs, coverage);
457
458 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700459 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000460 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
462
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700463 outputState.undefinedRegion = undefinedRegion;
464 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465}
466
467void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
468 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800469 // Evaluate the layers from front to back to determine what is visible. This
470 // also incrementally calculates the coverage information for each layer as
471 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800472 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700473 // Incrementally process the coverage for each layer
474 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800475
476 // TODO(b/121291683): Stop early if the output is completely covered and
477 // no more layers could even be visible underneath the ones on top.
478 }
479
Lloyd Pique01c77c12019-04-17 12:48:32 -0700480 setReleasedLayers(refreshArgs);
481
482 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800483}
484
Lloyd Piquede196652020-01-22 17:29:58 -0800485void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700486 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487 // Ensure we have a snapshot of the basic geometry layer state. Limit the
488 // snapshots to once per frame for each candidate layer, as layers may
489 // appear on multiple outputs.
490 if (!coverage.latchedLayers.count(layerFE)) {
491 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800492 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800493 }
494
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700495 // Only consider the layers on this output
496 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800497 return;
498 }
499
500 // Obtain a read-only pointer to the front-end layer state
501 const auto* layerFEState = layerFE->getCompositionState();
502 if (CC_UNLIKELY(!layerFEState)) {
503 return;
504 }
505
506 // handle hidden surfaces by setting the visible region to empty
507 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700508 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800509 }
510
511 /*
512 * opaqueRegion: area of a surface that is fully opaque.
513 */
514 Region opaqueRegion;
515
516 /*
517 * visibleRegion: area of a surface that is visible on screen and not fully
518 * transparent. This is essentially the layer's footprint minus the opaque
519 * regions above it. Areas covered by a translucent surface are considered
520 * visible.
521 */
522 Region visibleRegion;
523
524 /*
525 * coveredRegion: area of a surface that is covered by all visible regions
526 * above it (which includes the translucent areas).
527 */
528 Region coveredRegion;
529
530 /*
531 * transparentRegion: area of a surface that is hinted to be completely
532 * transparent. This is only used to tell when the layer has no visible non-
533 * transparent regions and can be removed from the layer list. It does not
534 * affect the visibleRegion of this layer or any layers beneath it. The hint
535 * may not be correct if apps don't respect the SurfaceView restrictions
536 * (which, sadly, some don't).
537 */
538 Region transparentRegion;
539
Vishnu Naira483b4a2019-12-12 15:07:52 -0800540 /*
541 * shadowRegion: Region cast by the layer's shadow.
542 */
543 Region shadowRegion;
544
Lloyd Piquede196652020-01-22 17:29:58 -0800545 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800546
547 // Get the visible region
548 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
549 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800550 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800551 visibleRegion.set(visibleRect);
552
Lloyd Piquede196652020-01-22 17:29:58 -0800553 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800554 // if the layer casts a shadow, offset the layers visible region and
555 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800556 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800557 Rect visibleRectWithShadows(visibleRect);
558 visibleRectWithShadows.inset(inset, inset, inset, inset);
559 visibleRegion.set(visibleRectWithShadows);
560 shadowRegion = visibleRegion.subtract(visibleRect);
561 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562
563 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700564 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800565 }
566
567 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800568 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800569 if (tr.preserveRects()) {
570 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800571 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800572 } else {
573 // transformation too complex, can't do the
574 // transparent region optimization.
575 transparentRegion.clear();
576 }
577 }
578
579 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800580 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800581 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800582 // If we one of the simple category of transforms (0/90/180/270 rotation
583 // + any flip), then the opaque region is the layer's footprint.
584 // Otherwise we don't try and compute the opaque region since there may
585 // be errors at the edges, and we treat the entire layer as
586 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800587 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800588 }
589
590 // Clip the covered region to the visible region
591 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
592
593 // Update accumAboveCoveredLayers for next (lower) layer
594 coverage.aboveCoveredLayers.orSelf(visibleRegion);
595
596 // subtract the opaque region covered by the layers above us
597 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
598
599 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700600 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800601 }
602
603 // Get coverage information for the layer as previously displayed,
604 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800605 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700606 auto prevOutputLayer =
607 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800608
609 // Get coverage information for the layer as previously displayed
610 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
611 const Region kEmptyRegion;
612 const Region& oldVisibleRegion =
613 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
614 const Region& oldCoveredRegion =
615 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
616
617 // compute this layer's dirty region
618 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800619 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800620 // we need to invalidate the whole region
621 dirty = visibleRegion;
622 // as well, as the old visible region
623 dirty.orSelf(oldVisibleRegion);
624 } else {
625 /* compute the exposed region:
626 * the exposed region consists of two components:
627 * 1) what's VISIBLE now and was COVERED before
628 * 2) what's EXPOSED now less what was EXPOSED before
629 *
630 * note that (1) is conservative, we start with the whole visible region
631 * but only keep what used to be covered by something -- which mean it
632 * may have been exposed.
633 *
634 * (2) handles areas that were not covered by anything but got exposed
635 * because of a resize.
636 *
637 */
638 const Region newExposed = visibleRegion - coveredRegion;
639 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
640 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
641 }
642 dirty.subtractSelf(coverage.aboveOpaqueLayers);
643
644 // accumulate to the screen dirty region
645 coverage.dirtyRegion.orSelf(dirty);
646
647 // Update accumAboveOpaqueLayers for next (lower) layer
648 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
649
650 // Compute the visible non-transparent region
651 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
652
Vishnu Naira483b4a2019-12-12 15:07:52 -0800653 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800654 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700655 const auto& outputState = getState();
656 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000657 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800658 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700659 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800660 }
661
Vishnu Naira483b4a2019-12-12 15:07:52 -0800662 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
663
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800664 // The layer is visible. Either reuse the existing outputLayer if we have
665 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800666 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800667
668 // Store the layer coverage information into the layer state as some of it
669 // is useful later.
670 auto& outputLayerState = result->editState();
671 outputLayerState.visibleRegion = visibleRegion;
672 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
673 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200674 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000675 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800676 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800677}
678
679void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
680 // The base class does nothing with this call.
681}
682
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800683void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700684 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800685 layer->getLayerFE().prepareCompositionState(
686 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
687 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800688 }
689}
690
Dan Stoza269dc4d2021-01-15 15:07:43 -0800691void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800692 ATRACE_CALL();
693 ALOGV(__FUNCTION__);
694
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800695 if (!getState().isEnabled) {
696 return;
697 }
698
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800699 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
700 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
701
Lloyd Pique01c77c12019-04-17 12:48:32 -0700702 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700703 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800704 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200705 forceClientComposition,
706 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800707
708 if (mLayerRequestingBackgroundBlur == layer) {
709 forceClientComposition = false;
710 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800711 }
712}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800713
Dan Stoza269dc4d2021-01-15 15:07:43 -0800714void Output::planComposition() {
715 if (!mPlanner || !getState().isEnabled) {
716 return;
717 }
718
719 ATRACE_CALL();
720 ALOGV(__FUNCTION__);
721
722 mPlanner->plan(getOutputLayersOrderedByZ());
723}
724
725void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
726 ATRACE_CALL();
727 ALOGV(__FUNCTION__);
728
729 if (!getState().isEnabled) {
730 return;
731 }
732
Ady Abraham3645e642021-04-20 18:39:00 -0700733 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700734 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham43065bd2021-12-10 17:22:15 -0800735 editState().expectedPresentTime = refreshArgs.expectedPresentTime;
Ady Abraham3645e642021-04-20 18:39:00 -0700736
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400737 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800738 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400739 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400740 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400741 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800742 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400743 if (layer == peekThroughLayer) {
744 // No longer needed, although it should not show up again, so
745 // resetting it is not truly needed either.
746 peekThroughLayer = nullptr;
747
748 // peekThroughLayer was already drawn ahead of its z order.
749 continue;
750 }
Dan Stoza6166c312021-01-15 16:34:05 -0800751 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400752 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400753 if (overrideInfo.buffer != nullptr) {
754 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800755 ALOGV("Skipping redundant buffer");
756 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400757 } else {
758 // First layer with the override buffer.
759 if (overrideInfo.peekThroughLayer) {
760 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400761
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400762 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400763 overrideZ = true;
764 includeGeometry = true;
765 constexpr bool isPeekingThrough = true;
766 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
767 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400768 }
769
770 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800771 }
Dan Stoza6166c312021-01-15 16:34:05 -0800772 }
773
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400774 constexpr bool isPeekingThrough = false;
775 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800776 }
777}
778
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800779compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
780 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
781 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100782 auto* compState = layer->getLayerFE().getCompositionState();
783
784 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
785 // want to force client composition because of the blur.
786 if (compState->sidebandStream != nullptr) {
787 return nullptr;
788 }
Lucas Dupin084a6d42021-08-26 22:10:29 +0000789 if (compState->isOpaque) {
790 continue;
791 }
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100792 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800793 layerRequestingBgComposition = layer;
794 }
795 }
796 return layerRequestingBgComposition;
797}
798
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800799void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
800 setColorProfile(pickColorProfile(refreshArgs));
801}
802
803// Returns a data space that fits all visible layers. The returned data space
804// can only be one of
805// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
806// - Dataspace::DISPLAY_P3
807// - Dataspace::DISPLAY_BT2020
808// The returned HDR data space is one of
809// - Dataspace::UNKNOWN
810// - Dataspace::BT2020_HLG
811// - Dataspace::BT2020_PQ
812ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
813 bool* outIsHdrClientComposition) const {
814 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
815 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
816
Lloyd Pique01c77c12019-04-17 12:48:32 -0700817 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800818 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800819 case ui::Dataspace::V0_SCRGB:
820 case ui::Dataspace::V0_SCRGB_LINEAR:
821 case ui::Dataspace::BT2020:
822 case ui::Dataspace::BT2020_ITU:
823 case ui::Dataspace::BT2020_LINEAR:
824 case ui::Dataspace::DISPLAY_BT2020:
825 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
826 break;
827 case ui::Dataspace::DISPLAY_P3:
828 bestDataSpace = ui::Dataspace::DISPLAY_P3;
829 break;
830 case ui::Dataspace::BT2020_PQ:
831 case ui::Dataspace::BT2020_ITU_PQ:
832 bestDataSpace = ui::Dataspace::DISPLAY_P3;
833 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800834 *outIsHdrClientComposition =
835 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800836 break;
837 case ui::Dataspace::BT2020_HLG:
838 case ui::Dataspace::BT2020_ITU_HLG:
839 bestDataSpace = ui::Dataspace::DISPLAY_P3;
840 // When there's mixed PQ content and HLG content, we set the HDR
841 // data space to be BT2020_PQ and convert HLG to PQ.
842 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
843 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
844 }
845 break;
846 default:
847 break;
848 }
849 }
850
851 return bestDataSpace;
852}
853
854compositionengine::Output::ColorProfile Output::pickColorProfile(
855 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
856 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
857 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
858 ui::RenderIntent::COLORIMETRIC,
859 refreshArgs.colorSpaceAgnosticDataspace};
860 }
861
862 ui::Dataspace hdrDataSpace;
863 bool isHdrClientComposition = false;
864 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
865
866 switch (refreshArgs.forceOutputColorMode) {
867 case ui::ColorMode::SRGB:
868 bestDataSpace = ui::Dataspace::V0_SRGB;
869 break;
870 case ui::ColorMode::DISPLAY_P3:
871 bestDataSpace = ui::Dataspace::DISPLAY_P3;
872 break;
873 default:
874 break;
875 }
876
877 // respect hdrDataSpace only when there is no legacy HDR support
878 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
879 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
880 if (isHdr) {
881 bestDataSpace = hdrDataSpace;
882 }
883
884 ui::RenderIntent intent;
885 switch (refreshArgs.outputColorSetting) {
886 case OutputColorSetting::kManaged:
887 case OutputColorSetting::kUnmanaged:
888 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
889 : ui::RenderIntent::COLORIMETRIC;
890 break;
891 case OutputColorSetting::kEnhanced:
892 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
893 break;
894 default: // vendor display color setting
895 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
896 break;
897 }
898
899 ui::ColorMode outMode;
900 ui::Dataspace outDataSpace;
901 ui::RenderIntent outRenderIntent;
902 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
903 &outRenderIntent);
904
905 return ColorProfile{outMode, outDataSpace, outRenderIntent,
906 refreshArgs.colorSpaceAgnosticDataspace};
907}
908
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800909void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700910 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700911 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700912 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700913 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800914
915 // If nothing has changed (!dirty), don't recompose.
916 // If something changed, but we don't currently have any visible layers,
917 // and didn't when we last did a composition, then skip it this time.
918 // The second rule does two things:
919 // - When all layers are removed from a display, we'll emit one black
920 // frame, then nothing more until we get new layers.
921 // - When a display is created with a private layer stack, we won't
922 // emit any black frames until a layer is added to the layer stack.
923 const bool mustRecompose = dirty && !(empty && wasEmpty);
924
925 const char flagPrefix[] = {'-', '+'};
926 static_cast<void>(flagPrefix);
927 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
928 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
929 flagPrefix[empty], flagPrefix[wasEmpty]);
930
931 mRenderSurface->beginFrame(mustRecompose);
932
933 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700934 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800935 }
936}
937
Lloyd Pique66d68602019-02-13 14:23:31 -0800938void Output::prepareFrame() {
939 ATRACE_CALL();
940 ALOGV(__FUNCTION__);
941
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700942 const auto& outputState = getState();
943 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800944 return;
945 }
946
947 chooseCompositionStrategy();
948
Dan Stoza47437bb2021-01-15 16:21:07 -0800949 if (mPlanner) {
950 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
951 }
952
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700953 mRenderSurface->prepareFrame(outputState.usesClientComposition,
954 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800955}
956
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800957void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
958 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
959 return;
960 }
961
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700962 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700963 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800964 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700965 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800966 }
967 }
968
969 postFramebuffer();
970
971 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
972
973 prepareFrame();
974}
975
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800976void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800977 ATRACE_CALL();
978 ALOGV(__FUNCTION__);
979
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700980 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800981 return;
982 }
983
984 // Repaint the framebuffer (if needed), getting the optional fence for when
985 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800986 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800987 if (!optReadyFence) {
988 return;
989 }
990
991 // swap buffers (presentation)
992 mRenderSurface->queueBuffer(std::move(*optReadyFence));
993}
994
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800995std::optional<base::unique_fd> Output::composeSurfaces(
996 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800997 ATRACE_CALL();
998 ALOGV(__FUNCTION__);
999
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001000 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001001 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001002 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001003 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -07001004
1005 auto& renderEngine = getCompositionEngine().getRenderEngine();
1006 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1007
1008 // If we the display is secure, protected content support is enabled, and at
1009 // least one layer has protected content, we need to use a secure back
1010 // buffer.
1011 if (outputState.isSecure && supportsProtectedContent) {
1012 auto layers = getOutputLayersOrderedByZ();
1013 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1014 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1015 });
1016 if (needsProtected != renderEngine.isProtected()) {
1017 renderEngine.useProtectedContext(needsProtected);
1018 }
1019 if (needsProtected != mRenderSurface->isProtected() &&
1020 needsProtected == renderEngine.isProtected()) {
1021 mRenderSurface->setProtected(needsProtected);
1022 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001023 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1024 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001025 }
1026
1027 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001028
1029 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001030
1031 // If we aren't doing client composition on this output, but do have a
1032 // flipClientTarget request for this frame on this output, we still need to
1033 // dequeue a buffer.
1034 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001035 tex = mRenderSurface->dequeueBuffer(&fd);
1036 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001037 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1038 "client composition for this frame",
1039 mName.c_str());
1040 return {};
1041 }
1042 }
1043
Lloyd Pique688abd42019-02-15 15:42:24 -08001044 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001045 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001046 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001047 }
1048
1049 ALOGV("hasClientComposition");
1050
Lloyd Pique688abd42019-02-15 15:42:24 -08001051 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001052 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1053 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001054 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001055 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001056 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1057 ? outputState.dataspace
1058 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001059
1060 // If we have a valid current display brightness use that, otherwise fall back to the
1061 // display's max desired
Alec Mourib21d94e2022-01-13 17:44:10 -08001062 clientCompositionDisplay.currentLuminanceNits = outputState.displayBrightnessNits > 0.f
John Reckac09e452021-04-07 16:35:37 -04001063 ? outputState.displayBrightnessNits
1064 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mourib21d94e2022-01-13 17:44:10 -08001065 clientCompositionDisplay.maxLuminance =
1066 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001067 clientCompositionDisplay.targetLuminanceNits = outputState.clientTargetWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001068
1069 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001070 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1071 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001072 }
1073
Lloyd Pique688abd42019-02-15 15:42:24 -08001074 // Generate the client composition requests for the layers on this output.
Robert Carrccab4242021-09-28 16:53:03 -07001075 std::vector<LayerFE*> clientCompositionLayersFE;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001076 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001077 generateClientCompositionRequests(supportsProtectedContent,
Robert Carrccab4242021-09-28 16:53:03 -07001078 clientCompositionDisplay.outputDataspace,
1079 clientCompositionLayersFE);
Lloyd Pique688abd42019-02-15 15:42:24 -08001080 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1081
Vishnu Nair9b079a22020-01-21 14:36:08 -08001082 // Check if the client composition requests were rendered into the provided graphic buffer. If
1083 // so, we can reuse the buffer and avoid client composition.
1084 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001085 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1086 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001087 clientCompositionLayers)) {
1088 outputCompositionState.reusedClientComposition = true;
1089 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001090 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001091 }
Alec Mouria90a5702021-04-16 16:36:21 +00001092 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001093 clientCompositionLayers);
1094 }
1095
Lloyd Pique688abd42019-02-15 15:42:24 -08001096 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001097 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1098 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1099 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001100 const bool expensiveBlurs =
1101 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001102 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001103 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001104 if (expensiveRenderingExpected) {
1105 setExpensiveRenderingExpected(true);
1106 }
1107
Sally Qi59a9f502021-10-12 18:53:23 +00001108 std::vector<renderengine::LayerSettings> clientRenderEngineLayers;
1109 clientRenderEngineLayers.reserve(clientCompositionLayers.size());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001110 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
Sally Qi59a9f502021-10-12 18:53:23 +00001111 std::back_inserter(clientRenderEngineLayers),
1112 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings {
1113 return settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001114 });
1115
Alec Mourie4034bb2019-11-19 12:45:54 -08001116 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001117 // Only use the framebuffer cache when rendering to an internal display
1118 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1119 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1120 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1121 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1122 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001123 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001124 auto [status, drawFence] =
1125 renderEngine
Sally Qi59a9f502021-10-12 18:53:23 +00001126 .drawLayers(clientCompositionDisplay, clientRenderEngineLayers, tex,
Sally Qi4cabdd02021-08-05 16:45:57 -07001127 useFramebufferCache, std::move(fd))
1128 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001129
1130 if (status != NO_ERROR && mClientCompositionRequestCache) {
1131 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001132 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001133 }
1134
Alec Mourie4034bb2019-11-19 12:45:54 -08001135 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001136 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001137 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1138 } else {
1139 timeStats.recordRenderEngineDuration(renderEngineStart,
1140 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001141 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001142 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001143
Robert Carrccab4242021-09-28 16:53:03 -07001144 if (clientCompositionLayersFE.size() > 0) {
1145 sp<Fence> clientCompFence = new Fence(dup(drawFence.get()));
1146 for (auto clientComposedLayer : clientCompositionLayersFE) {
1147 clientComposedLayer->setWasClientComposed(clientCompFence);
1148 }
1149 }
1150
Sally Qi4cabdd02021-08-05 16:45:57 -07001151 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001152}
1153
Vishnu Nair9b079a22020-01-21 14:36:08 -08001154std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Robert Carrccab4242021-09-28 16:53:03 -07001155 bool supportsProtectedContent, ui::Dataspace outputDataspace, std::vector<LayerFE*>& outLayerFEs) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001156 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001157 ALOGV("Rendering client layers");
1158
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001159 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001160 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001161 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001162
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001163 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001164 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001165
Lloyd Pique01c77c12019-04-17 12:48:32 -07001166 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001167 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001168 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001169 auto& layerFE = layer->getLayerFE();
1170
Lloyd Piquea2468662019-03-07 21:31:06 -08001171 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001172 ALOGV("Layer: %s", layerFE.getDebugName());
1173 if (clip.isEmpty()) {
1174 ALOGV(" Skipping for empty clip");
1175 firstLayer = false;
1176 continue;
1177 }
1178
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001179 disableBlurs |= layerFEState->sidebandStream != nullptr;
1180
Vishnu Naira483b4a2019-12-12 15:07:52 -08001181 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001182
1183 // We clear the client target for non-client composed layers if
1184 // requested by the HWC. We skip this if the layer is not an opaque
1185 // rectangle, as by definition the layer must blend with whatever is
1186 // underneath. We also skip the first layer as the buffer target is
1187 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001188 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001189 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001190
1191 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1192
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001193 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1194 // composing the non-shadow content and only draw the shadows.
1195 const bool realContentIsVisible = clientComposition &&
1196 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1197
Lloyd Pique688abd42019-02-15 15:42:24 -08001198 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001199 std::vector<LayerFE::LayerSettings> results;
1200 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001201 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001202 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001203 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001204 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1205 } else {
1206 ALOGV("Skipping redundant override buffer for [%s] in RE",
1207 layer->getLayerFE().getDebugName());
1208 }
Dan Stoza6166c312021-01-15 16:34:05 -08001209 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001210 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1211 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1212 : (layer->getState().overrideInfo.disableBackgroundBlur
1213 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1214 BlurRegionsOnly
1215 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1216 Enabled);
1217 compositionengine::LayerFE::ClientCompositionTargetSettings
1218 targetSettings{.clip = clip,
1219 .needsFiltering = layer->needsFiltering() ||
1220 outputState.needsFiltering,
1221 .isSecure = outputState.isSecure,
1222 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001223 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001224 .dataspace = outputDataspace,
1225 .realContentIsVisible = realContentIsVisible,
1226 .clearContent = !clientComposition,
Alec Mouricdf6cbc2021-11-01 17:21:15 -07001227 .blurSetting = blurSetting,
1228 .whitePointNits = layerState.whitePointNits};
Dan Stoza6166c312021-01-15 16:34:05 -08001229 results = layerFE.prepareClientCompositionList(targetSettings);
1230 if (realContentIsVisible && !results.empty()) {
1231 layer->editState().clientCompositionTimestamp = systemTime();
1232 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001233 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001234
Robert Carrccab4242021-09-28 16:53:03 -07001235 outLayerFEs.push_back(&layerFE);
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001236 clientCompositionLayers.insert(clientCompositionLayers.end(),
1237 std::make_move_iterator(results.begin()),
1238 std::make_move_iterator(results.end()));
1239 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001240 }
1241
1242 firstLayer = false;
1243 }
1244
1245 return clientCompositionLayers;
1246}
1247
1248void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001249 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001250 if (flashRegion.isEmpty()) {
1251 return;
1252 }
1253
Vishnu Nair9b079a22020-01-21 14:36:08 -08001254 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001255 layerSettings.source.buffer.buffer = nullptr;
1256 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1257 layerSettings.alpha = half(1.0);
1258
1259 for (const auto& rect : flashRegion) {
1260 layerSettings.geometry.boundaries = rect.toFloatRect();
1261 clientCompositionLayers.push_back(layerSettings);
1262 }
1263}
1264
1265void Output::setExpensiveRenderingExpected(bool) {
1266 // The base class does nothing with this call.
1267}
1268
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001269void Output::postFramebuffer() {
1270 ATRACE_CALL();
1271 ALOGV(__FUNCTION__);
1272
1273 if (!getState().isEnabled) {
1274 return;
1275 }
1276
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001277 auto& outputState = editState();
1278 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001279 mRenderSurface->flip();
1280
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001281 auto frame = presentAndGetFrameFences();
1282
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001283 mRenderSurface->onPresentDisplayCompleted();
1284
Lloyd Pique01c77c12019-04-17 12:48:32 -07001285 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001286 // The layer buffer from the previous frame (if any) is released
1287 // by HWC only when the release fence from this frame (if any) is
1288 // signaled. Always get the release fence from HWC first.
1289 sp<Fence> releaseFence = Fence::NO_FENCE;
1290
1291 if (auto hwcLayer = layer->getHwcLayer()) {
1292 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1293 releaseFence = f->second;
1294 }
1295 }
1296
1297 // If the layer was client composited in the previous frame, we
1298 // need to merge with the previous client target acquire fence.
1299 // Since we do not track that, always merge with the current
1300 // client target acquire fence when it is available, even though
1301 // this is suboptimal.
1302 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001303 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001304 releaseFence =
1305 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1306 }
Sally Qi59a9f502021-10-12 18:53:23 +00001307 layer->getLayerFE().onLayerDisplayed(
1308 ftl::yield<renderengine::RenderEngineResult>(
1309 {NO_ERROR, base::unique_fd(releaseFence->dup())})
1310 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001311 }
1312
1313 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001314 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001315 // supply them with the present fence.
1316 for (auto& weakLayer : mReleasedLayers) {
1317 if (auto layer = weakLayer.promote(); layer != nullptr) {
Sally Qi59a9f502021-10-12 18:53:23 +00001318 layer->onLayerDisplayed(ftl::yield<renderengine::RenderEngineResult>(
1319 {NO_ERROR, base::unique_fd(frame.presentFence->dup())})
1320 .share());
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001321 }
1322 }
1323
1324 // Clear out the released layers now that we're done with them.
1325 mReleasedLayers.clear();
1326}
1327
Alec Mouriaa831582021-06-07 16:23:01 -07001328void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001329 if (mPlanner) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -07001330 mPlanner->renderCachedSets(getState(), refreshArgs.scheduledFrameTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001331 }
1332}
1333
Lloyd Pique32cbe282018-10-19 13:09:22 -07001334void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001335 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001336 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001337}
1338
Lloyd Pique66d68602019-02-13 14:23:31 -08001339void Output::chooseCompositionStrategy() {
1340 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001341 auto& outputState = editState();
1342 outputState.usesClientComposition = true;
1343 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001344 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001345}
1346
Lloyd Pique688abd42019-02-15 15:42:24 -08001347bool Output::getSkipColorTransform() const {
1348 return true;
1349}
1350
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001351compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1352 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001353 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001354 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1355 }
1356 return result;
1357}
1358
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001359} // namespace impl
1360} // namespace android::compositionengine