blob: 535458e8f48f63befde17d8c1d610471582617e9 [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>
30
Alec Mouria90a5702021-04-16 16:36:21 +000031#include <thread>
32
33#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080034
35// TODO(b/129481165): remove the #pragma below and fix conversion issues
36#pragma clang diagnostic push
37#pragma clang diagnostic ignored "-Wconversion"
38
Lloyd Pique688abd42019-02-15 15:42:24 -080039#include <renderengine/DisplaySettings.h>
40#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080041
42// TODO(b/129481165): remove the #pragma below and fix conversion issues
43#pragma clang diagnostic pop // ignored "-Wconversion"
44
Dan Stoza269dc4d2021-01-15 15:07:43 -080045#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070046#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080047#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080048#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070049
Lloyd Pique688abd42019-02-15 15:42:24 -080050#include "TracedOrdinal.h"
51
Lloyd Piquefeb73d72018-12-04 17:23:44 -080052namespace android::compositionengine {
53
54Output::~Output() = default;
55
56namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070057
Lloyd Piquec29e4c62019-03-07 21:48:19 -080058namespace {
59
60template <typename T>
61class Reversed {
62public:
63 explicit Reversed(const T& container) : mContainer(container) {}
64 auto begin() { return mContainer.rbegin(); }
65 auto end() { return mContainer.rend(); }
66
67private:
68 const T& mContainer;
69};
70
71// Helper for enumerating over a container in reverse order
72template <typename T>
73Reversed<T> reversed(const T& c) {
74 return Reversed<T>(c);
75}
76
Marin Shalamanovb15d2272020-09-17 21:41:52 +020077struct ScaleVector {
78 float x;
79 float y;
80};
81
82// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
83// where to' will have the same size as "to". In the case where "from" and "to"
84// start at the origin to'=to.
85ScaleVector getScale(const Rect& from, const Rect& to) {
86 return {.x = static_cast<float>(to.width()) / from.width(),
87 .y = static_cast<float>(to.height()) / from.height()};
88}
89
Lloyd Piquec29e4c62019-03-07 21:48:19 -080090} // namespace
91
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070092std::shared_ptr<Output> createOutput(
93 const compositionengine::CompositionEngine& compositionEngine) {
94 return createOutputTemplated<Output>(compositionEngine);
95}
Lloyd Pique32cbe282018-10-19 13:09:22 -070096
97Output::~Output() = default;
98
Lloyd Pique32cbe282018-10-19 13:09:22 -070099bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700100 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
101 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102}
103
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700104std::optional<DisplayId> Output::getDisplayId() const {
105 return {};
106}
107
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108const std::string& Output::getName() const {
109 return mName;
110}
111
112void Output::setName(const std::string& name) {
113 mName = name;
114}
115
116void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700117 auto& outputState = editState();
118 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 return;
120 }
121
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700122 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123 dirtyEntireOutput();
124}
125
Alec Mouri023c1882021-05-08 16:36:33 -0700126void Output::setLayerCachingEnabled(bool enabled) {
127 if (enabled == (mPlanner != nullptr)) {
128 return;
129 }
130
131 if (enabled) {
Alec Mouridf6201b2021-06-01 16:20:42 -0700132 mPlanner = std::make_unique<planner::Planner>(getCompositionEngine().getRenderEngine());
Alec Mouri023c1882021-05-08 16:36:33 -0700133 if (mRenderSurface) {
134 mPlanner->setDisplaySize(mRenderSurface->getSize());
135 }
136 } else {
137 mPlanner.reset();
138 }
Alec Mouric773472b2021-05-19 14:29:05 -0700139
140 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
141 if (!outputLayer) {
142 continue;
143 }
144
145 outputLayer->editState().overrideInfo = {};
146 }
Alec Mouri023c1882021-05-08 16:36:33 -0700147}
148
Ady Abrahamdb036a82021-07-16 14:18:34 -0700149void Output::setLayerCachingTexturePoolEnabled(bool enabled) {
150 if (mPlanner) {
151 mPlanner->setTexturePoolEnabled(enabled);
152 }
153}
154
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200155void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
156 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700157 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200158
Angel Aguayob084e0c2021-08-04 23:27:28 +0000159 outputState.displaySpace.setOrientation(orientation);
160 LOG_FATAL_IF(outputState.displaySpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200161 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200162
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200163 // Compute orientedDisplaySpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000164 ui::Size orientedSize = outputState.displaySpace.getBounds();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200165 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200166 std::swap(orientedSize.width, orientedSize.height);
167 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000168 outputState.orientedDisplaySpace.setBounds(orientedSize);
169 outputState.orientedDisplaySpace.setContent(orientedDisplaySpaceRect);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200170
171 // Compute displaySpace.content
172 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
173 ui::Transform rotation;
174 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
Angel Aguayob084e0c2021-08-04 23:27:28 +0000175 const auto displaySize = outputState.displaySpace.getBoundsAsRect();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200176 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
177 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000178 outputState.displaySpace.setContent(rotation.transform(orientedDisplaySpaceRect));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200179
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180 // Compute framebufferSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000181 outputState.framebufferSpace.setOrientation(orientation);
182 LOG_FATAL_IF(outputState.framebufferSpace.getBoundsAsRect() == Rect::INVALID_RECT,
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200183 "The framebuffer bounds are unknown.");
Angel Aguayob084e0c2021-08-04 23:27:28 +0000184 const auto scale = getScale(outputState.displaySpace.getBoundsAsRect(),
185 outputState.framebufferSpace.getBoundsAsRect());
186 outputState.framebufferSpace.setContent(
187 outputState.displaySpace.getContent().scale(scale.x, scale.y));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200188
189 // Compute layerStackSpace
Angel Aguayob084e0c2021-08-04 23:27:28 +0000190 outputState.layerStackSpace.setContent(layerStackSpaceRect);
191 outputState.layerStackSpace.setBounds(
192 ui::Size(layerStackSpaceRect.getWidth(), layerStackSpaceRect.getHeight()));
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200193
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200194 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
195 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700196 dirtyEntireOutput();
197}
198
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200201
202 auto& state = editState();
203
204 // Update framebuffer space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000205 const ui::Size newBounds(size);
206 state.framebufferSpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200207
208 // Update display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000209 state.displaySpace.setBounds(newBounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200210 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
211
212 // Update oriented display space
Angel Aguayob084e0c2021-08-04 23:27:28 +0000213 const auto orientation = state.displaySpace.getOrientation();
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200214 ui::Size orientedSize = size;
215 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
216 std::swap(orientedSize.width, orientedSize.height);
217 }
Angel Aguayob084e0c2021-08-04 23:27:28 +0000218 const ui::Size newOrientedBounds(orientedSize);
219 state.orientedDisplaySpace.setBounds(newOrientedBounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700220
Dan Stoza6166c312021-01-15 16:34:05 -0800221 if (mPlanner) {
222 mPlanner->setDisplaySize(size);
223 }
224
Lloyd Pique32cbe282018-10-19 13:09:22 -0700225 dirtyEntireOutput();
226}
227
Garfield Tan54edd912020-10-21 16:31:41 -0700228ui::Transform::RotationFlags Output::getTransformHint() const {
229 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
230}
231
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700232void Output::setLayerFilter(ui::LayerFilter filter) {
233 editState().layerFilter = filter;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234 dirtyEntireOutput();
235}
236
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800237void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700238 auto& colorTransformMatrix = editState().colorTransformMatrix;
239 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700240 return;
241 }
242
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700243 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800244
245 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700246}
247
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800248void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700249 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800250 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
251 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800252
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700253 auto& outputState = editState();
254 if (outputState.colorMode == colorProfile.mode &&
255 outputState.dataspace == colorProfile.dataspace &&
256 outputState.renderIntent == colorProfile.renderIntent &&
257 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800258 return;
259 }
260
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700261 outputState.colorMode = colorProfile.mode;
262 outputState.dataspace = colorProfile.dataspace;
263 outputState.renderIntent = colorProfile.renderIntent;
264 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700265
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800266 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700267
Lloyd Pique32cbe282018-10-19 13:09:22 -0700268 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800269 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
270 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800271
272 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700273}
274
John Reckac09e452021-04-07 16:35:37 -0400275void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
276 auto& outputState = editState();
277 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
278 outputState.displayBrightnessNits == displayBrightnessNits) {
279 // Nothing changed
280 return;
281 }
282 outputState.sdrWhitePointNits = sdrWhitePointNits;
283 outputState.displayBrightnessNits = displayBrightnessNits;
284 dirtyEntireOutput();
285}
286
Lloyd Pique32cbe282018-10-19 13:09:22 -0700287void Output::dump(std::string& out) const {
288 using android::base::StringAppendF;
289
290 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
291
292 out.append("\n ");
293
294 dumpBase(out);
295}
296
297void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700298 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700299
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700300 if (mDisplayColorProfile) {
301 mDisplayColorProfile->dump(out);
302 } else {
303 out.append(" No display color profile!\n");
304 }
305
Lloyd Pique31cb2942018-10-19 17:23:03 -0700306 if (mRenderSurface) {
307 mRenderSurface->dump(out);
308 } else {
309 out.append(" No render surface!\n");
310 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800311
Lloyd Pique01c77c12019-04-17 12:48:32 -0700312 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
313 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800314 if (!outputLayer) {
315 continue;
316 }
317 outputLayer->dump(out);
318 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700319}
320
Dan Stoza269dc4d2021-01-15 15:07:43 -0800321void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
322 if (!mPlanner) {
323 base::StringAppendF(&out, "Planner is disabled\n");
324 return;
325 }
326 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
327 mPlanner->dump(args, out);
328}
329
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700330compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
331 return mDisplayColorProfile.get();
332}
333
334void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
335 mDisplayColorProfile = std::move(mode);
336}
337
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800338const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
339 return mReleasedLayers;
340}
341
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700342void Output::setDisplayColorProfileForTest(
343 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
344 mDisplayColorProfile = std::move(mode);
345}
346
Lloyd Pique31cb2942018-10-19 17:23:03 -0700347compositionengine::RenderSurface* Output::getRenderSurface() const {
348 return mRenderSurface.get();
349}
350
351void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
352 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800353 const auto size = mRenderSurface->getSize();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000354 editState().framebufferSpace.setBounds(size);
Dan Stoza6166c312021-01-15 16:34:05 -0800355 if (mPlanner) {
356 mPlanner->setDisplaySize(size);
357 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700358 dirtyEntireOutput();
359}
360
Vishnu Nair9b079a22020-01-21 14:36:08 -0800361void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
362 if (cacheSize == 0) {
363 mClientCompositionRequestCache.reset();
364 } else {
365 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
366 }
367};
368
Lloyd Pique31cb2942018-10-19 17:23:03 -0700369void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
370 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700371}
372
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700373Region Output::getDirtyRegion() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700374 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +0000375 return outputState.dirtyRegion.intersect(outputState.layerStackSpace.getContent());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700376}
377
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700378bool Output::includesLayer(ui::LayerFilter filter) const {
379 return getState().layerFilter.includes(filter);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700380}
381
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700382bool Output::includesLayer(const sp<LayerFE>& layerFE) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800383 const auto* layerFEState = layerFE->getCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700384 return layerFEState && includesLayer(layerFEState->outputFilter);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800385}
386
Lloyd Piquedf336d92019-03-07 21:38:42 -0800387std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800388 const sp<LayerFE>& layerFE) const {
389 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800390}
391
Lloyd Piquede196652020-01-22 17:29:58 -0800392compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
393 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700394 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800395}
396
Lloyd Pique01c77c12019-04-17 12:48:32 -0700397std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800398 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700399 for (size_t i = 0; i < getOutputLayerCount(); i++) {
400 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800401 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700402 return i;
403 }
404 }
405 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800406}
407
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800408void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
409 mReleasedLayers = std::move(layers);
410}
411
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800412void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
413 LayerFESet& geomSnapshots) {
414 ATRACE_CALL();
415 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800416
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800417 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800418}
419
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800420void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800421 ATRACE_CALL();
422 ALOGV(__FUNCTION__);
423
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800424 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800425 updateCompositionState(refreshArgs);
426 planComposition();
427 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800428 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800429 beginFrame();
430 prepareFrame();
431 devOptRepaintFlash(refreshArgs);
432 finishFrame(refreshArgs);
433 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700434 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800435}
436
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800437void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
438 LayerFESet& layerFESet) {
439 ATRACE_CALL();
440 ALOGV(__FUNCTION__);
441
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700442 auto& outputState = editState();
443
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800444 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700445 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800446 return;
447 }
448
449 // Process the layers to determine visibility and coverage
450 compositionengine::Output::CoverageState coverage{layerFESet};
451 collectVisibleLayers(refreshArgs, coverage);
452
453 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700454 const ui::Transform& tr = outputState.transform;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000455 Region undefinedRegion{outputState.displaySpace.getBoundsAsRect()};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800456 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
457
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700458 outputState.undefinedRegion = undefinedRegion;
459 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800460}
461
462void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
463 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800464 // Evaluate the layers from front to back to determine what is visible. This
465 // also incrementally calculates the coverage information for each layer as
466 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800467 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700468 // Incrementally process the coverage for each layer
469 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800470
471 // TODO(b/121291683): Stop early if the output is completely covered and
472 // no more layers could even be visible underneath the ones on top.
473 }
474
Lloyd Pique01c77c12019-04-17 12:48:32 -0700475 setReleasedLayers(refreshArgs);
476
477 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800478}
479
Lloyd Piquede196652020-01-22 17:29:58 -0800480void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700481 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800482 // Ensure we have a snapshot of the basic geometry layer state. Limit the
483 // snapshots to once per frame for each candidate layer, as layers may
484 // appear on multiple outputs.
485 if (!coverage.latchedLayers.count(layerFE)) {
486 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800487 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800488 }
489
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700490 // Only consider the layers on this output
491 if (!includesLayer(layerFE)) {
Lloyd Piquede196652020-01-22 17:29:58 -0800492 return;
493 }
494
495 // Obtain a read-only pointer to the front-end layer state
496 const auto* layerFEState = layerFE->getCompositionState();
497 if (CC_UNLIKELY(!layerFEState)) {
498 return;
499 }
500
501 // handle hidden surfaces by setting the visible region to empty
502 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700503 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800504 }
505
506 /*
507 * opaqueRegion: area of a surface that is fully opaque.
508 */
509 Region opaqueRegion;
510
511 /*
512 * visibleRegion: area of a surface that is visible on screen and not fully
513 * transparent. This is essentially the layer's footprint minus the opaque
514 * regions above it. Areas covered by a translucent surface are considered
515 * visible.
516 */
517 Region visibleRegion;
518
519 /*
520 * coveredRegion: area of a surface that is covered by all visible regions
521 * above it (which includes the translucent areas).
522 */
523 Region coveredRegion;
524
525 /*
526 * transparentRegion: area of a surface that is hinted to be completely
527 * transparent. This is only used to tell when the layer has no visible non-
528 * transparent regions and can be removed from the layer list. It does not
529 * affect the visibleRegion of this layer or any layers beneath it. The hint
530 * may not be correct if apps don't respect the SurfaceView restrictions
531 * (which, sadly, some don't).
532 */
533 Region transparentRegion;
534
Vishnu Naira483b4a2019-12-12 15:07:52 -0800535 /*
536 * shadowRegion: Region cast by the layer's shadow.
537 */
538 Region shadowRegion;
539
Lloyd Piquede196652020-01-22 17:29:58 -0800540 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800541
542 // Get the visible region
543 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
544 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800545 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800546 visibleRegion.set(visibleRect);
547
Lloyd Piquede196652020-01-22 17:29:58 -0800548 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800549 // if the layer casts a shadow, offset the layers visible region and
550 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800551 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800552 Rect visibleRectWithShadows(visibleRect);
553 visibleRectWithShadows.inset(inset, inset, inset, inset);
554 visibleRegion.set(visibleRectWithShadows);
555 shadowRegion = visibleRegion.subtract(visibleRect);
556 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800557
558 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700559 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560 }
561
562 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800563 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800564 if (tr.preserveRects()) {
565 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800566 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800567 } else {
568 // transformation too complex, can't do the
569 // transparent region optimization.
570 transparentRegion.clear();
571 }
572 }
573
574 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800575 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800576 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800577 // If we one of the simple category of transforms (0/90/180/270 rotation
578 // + any flip), then the opaque region is the layer's footprint.
579 // Otherwise we don't try and compute the opaque region since there may
580 // be errors at the edges, and we treat the entire layer as
581 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800582 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800583 }
584
585 // Clip the covered region to the visible region
586 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
587
588 // Update accumAboveCoveredLayers for next (lower) layer
589 coverage.aboveCoveredLayers.orSelf(visibleRegion);
590
591 // subtract the opaque region covered by the layers above us
592 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
593
594 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700595 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800596 }
597
598 // Get coverage information for the layer as previously displayed,
599 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800600 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700601 auto prevOutputLayer =
602 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800603
604 // Get coverage information for the layer as previously displayed
605 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
606 const Region kEmptyRegion;
607 const Region& oldVisibleRegion =
608 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
609 const Region& oldCoveredRegion =
610 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
611
612 // compute this layer's dirty region
613 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800614 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800615 // we need to invalidate the whole region
616 dirty = visibleRegion;
617 // as well, as the old visible region
618 dirty.orSelf(oldVisibleRegion);
619 } else {
620 /* compute the exposed region:
621 * the exposed region consists of two components:
622 * 1) what's VISIBLE now and was COVERED before
623 * 2) what's EXPOSED now less what was EXPOSED before
624 *
625 * note that (1) is conservative, we start with the whole visible region
626 * but only keep what used to be covered by something -- which mean it
627 * may have been exposed.
628 *
629 * (2) handles areas that were not covered by anything but got exposed
630 * because of a resize.
631 *
632 */
633 const Region newExposed = visibleRegion - coveredRegion;
634 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
635 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
636 }
637 dirty.subtractSelf(coverage.aboveOpaqueLayers);
638
639 // accumulate to the screen dirty region
640 coverage.dirtyRegion.orSelf(dirty);
641
642 // Update accumAboveOpaqueLayers for next (lower) layer
643 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
644
645 // Compute the visible non-transparent region
646 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
647
Vishnu Naira483b4a2019-12-12 15:07:52 -0800648 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800649 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700650 const auto& outputState = getState();
651 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Angel Aguayob084e0c2021-08-04 23:27:28 +0000652 drawRegion.andSelf(outputState.displaySpace.getBoundsAsRect());
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800653 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700654 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800655 }
656
Vishnu Naira483b4a2019-12-12 15:07:52 -0800657 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
658
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800659 // The layer is visible. Either reuse the existing outputLayer if we have
660 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800661 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800662
663 // Store the layer coverage information into the layer state as some of it
664 // is useful later.
665 auto& outputLayerState = result->editState();
666 outputLayerState.visibleRegion = visibleRegion;
667 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
668 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200669 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
Angel Aguayob084e0c2021-08-04 23:27:28 +0000670 visibleNonShadowRegion.intersect(outputState.layerStackSpace.getContent()));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800671 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800672}
673
674void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
675 // The base class does nothing with this call.
676}
677
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800678void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700679 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800680 layer->getLayerFE().prepareCompositionState(
681 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
682 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800683 }
684}
685
Dan Stoza269dc4d2021-01-15 15:07:43 -0800686void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800687 ATRACE_CALL();
688 ALOGV(__FUNCTION__);
689
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800690 if (!getState().isEnabled) {
691 return;
692 }
693
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800694 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
695 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
696
Lloyd Pique01c77c12019-04-17 12:48:32 -0700697 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700698 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800699 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200700 forceClientComposition,
701 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800702
703 if (mLayerRequestingBackgroundBlur == layer) {
704 forceClientComposition = false;
705 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800706 }
707}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800708
Dan Stoza269dc4d2021-01-15 15:07:43 -0800709void Output::planComposition() {
710 if (!mPlanner || !getState().isEnabled) {
711 return;
712 }
713
714 ATRACE_CALL();
715 ALOGV(__FUNCTION__);
716
717 mPlanner->plan(getOutputLayersOrderedByZ());
718}
719
720void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
721 ATRACE_CALL();
722 ALOGV(__FUNCTION__);
723
724 if (!getState().isEnabled) {
725 return;
726 }
727
Ady Abraham3645e642021-04-20 18:39:00 -0700728 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700729 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham3645e642021-04-20 18:39:00 -0700730
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400731 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800732 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400733 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400734 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400735 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800736 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400737 if (layer == peekThroughLayer) {
738 // No longer needed, although it should not show up again, so
739 // resetting it is not truly needed either.
740 peekThroughLayer = nullptr;
741
742 // peekThroughLayer was already drawn ahead of its z order.
743 continue;
744 }
Dan Stoza6166c312021-01-15 16:34:05 -0800745 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400746 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400747 if (overrideInfo.buffer != nullptr) {
748 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800749 ALOGV("Skipping redundant buffer");
750 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400751 } else {
752 // First layer with the override buffer.
753 if (overrideInfo.peekThroughLayer) {
754 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400755
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400756 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400757 overrideZ = true;
758 includeGeometry = true;
759 constexpr bool isPeekingThrough = true;
760 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
761 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400762 }
763
764 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800765 }
Dan Stoza6166c312021-01-15 16:34:05 -0800766 }
767
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400768 constexpr bool isPeekingThrough = false;
769 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800770 }
771}
772
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800773compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
774 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
775 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100776 auto* compState = layer->getLayerFE().getCompositionState();
777
778 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
779 // want to force client composition because of the blur.
780 if (compState->sidebandStream != nullptr) {
781 return nullptr;
782 }
783 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800784 layerRequestingBgComposition = layer;
785 }
786 }
787 return layerRequestingBgComposition;
788}
789
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800790void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
791 setColorProfile(pickColorProfile(refreshArgs));
792}
793
794// Returns a data space that fits all visible layers. The returned data space
795// can only be one of
796// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
797// - Dataspace::DISPLAY_P3
798// - Dataspace::DISPLAY_BT2020
799// The returned HDR data space is one of
800// - Dataspace::UNKNOWN
801// - Dataspace::BT2020_HLG
802// - Dataspace::BT2020_PQ
803ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
804 bool* outIsHdrClientComposition) const {
805 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
806 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
807
Lloyd Pique01c77c12019-04-17 12:48:32 -0700808 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800809 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800810 case ui::Dataspace::V0_SCRGB:
811 case ui::Dataspace::V0_SCRGB_LINEAR:
812 case ui::Dataspace::BT2020:
813 case ui::Dataspace::BT2020_ITU:
814 case ui::Dataspace::BT2020_LINEAR:
815 case ui::Dataspace::DISPLAY_BT2020:
816 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
817 break;
818 case ui::Dataspace::DISPLAY_P3:
819 bestDataSpace = ui::Dataspace::DISPLAY_P3;
820 break;
821 case ui::Dataspace::BT2020_PQ:
822 case ui::Dataspace::BT2020_ITU_PQ:
823 bestDataSpace = ui::Dataspace::DISPLAY_P3;
824 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800825 *outIsHdrClientComposition =
826 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800827 break;
828 case ui::Dataspace::BT2020_HLG:
829 case ui::Dataspace::BT2020_ITU_HLG:
830 bestDataSpace = ui::Dataspace::DISPLAY_P3;
831 // When there's mixed PQ content and HLG content, we set the HDR
832 // data space to be BT2020_PQ and convert HLG to PQ.
833 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
834 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
835 }
836 break;
837 default:
838 break;
839 }
840 }
841
842 return bestDataSpace;
843}
844
845compositionengine::Output::ColorProfile Output::pickColorProfile(
846 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
847 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
848 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
849 ui::RenderIntent::COLORIMETRIC,
850 refreshArgs.colorSpaceAgnosticDataspace};
851 }
852
853 ui::Dataspace hdrDataSpace;
854 bool isHdrClientComposition = false;
855 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
856
857 switch (refreshArgs.forceOutputColorMode) {
858 case ui::ColorMode::SRGB:
859 bestDataSpace = ui::Dataspace::V0_SRGB;
860 break;
861 case ui::ColorMode::DISPLAY_P3:
862 bestDataSpace = ui::Dataspace::DISPLAY_P3;
863 break;
864 default:
865 break;
866 }
867
868 // respect hdrDataSpace only when there is no legacy HDR support
869 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
870 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
871 if (isHdr) {
872 bestDataSpace = hdrDataSpace;
873 }
874
875 ui::RenderIntent intent;
876 switch (refreshArgs.outputColorSetting) {
877 case OutputColorSetting::kManaged:
878 case OutputColorSetting::kUnmanaged:
879 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
880 : ui::RenderIntent::COLORIMETRIC;
881 break;
882 case OutputColorSetting::kEnhanced:
883 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
884 break;
885 default: // vendor display color setting
886 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
887 break;
888 }
889
890 ui::ColorMode outMode;
891 ui::Dataspace outDataSpace;
892 ui::RenderIntent outRenderIntent;
893 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
894 &outRenderIntent);
895
896 return ColorProfile{outMode, outDataSpace, outRenderIntent,
897 refreshArgs.colorSpaceAgnosticDataspace};
898}
899
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800900void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700901 auto& outputState = editState();
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700902 const bool dirty = !getDirtyRegion().isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700903 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700904 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800905
906 // If nothing has changed (!dirty), don't recompose.
907 // If something changed, but we don't currently have any visible layers,
908 // and didn't when we last did a composition, then skip it this time.
909 // The second rule does two things:
910 // - When all layers are removed from a display, we'll emit one black
911 // frame, then nothing more until we get new layers.
912 // - When a display is created with a private layer stack, we won't
913 // emit any black frames until a layer is added to the layer stack.
914 const bool mustRecompose = dirty && !(empty && wasEmpty);
915
916 const char flagPrefix[] = {'-', '+'};
917 static_cast<void>(flagPrefix);
918 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
919 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
920 flagPrefix[empty], flagPrefix[wasEmpty]);
921
922 mRenderSurface->beginFrame(mustRecompose);
923
924 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700925 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800926 }
927}
928
Lloyd Pique66d68602019-02-13 14:23:31 -0800929void Output::prepareFrame() {
930 ATRACE_CALL();
931 ALOGV(__FUNCTION__);
932
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700933 const auto& outputState = getState();
934 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800935 return;
936 }
937
938 chooseCompositionStrategy();
939
Dan Stoza47437bb2021-01-15 16:21:07 -0800940 if (mPlanner) {
941 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
942 }
943
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700944 mRenderSurface->prepareFrame(outputState.usesClientComposition,
945 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800946}
947
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800948void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
949 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
950 return;
951 }
952
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700953 if (getState().isEnabled) {
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700954 if (const auto dirtyRegion = getDirtyRegion(); !dirtyRegion.isEmpty()) {
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800955 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700956 mRenderSurface->queueBuffer(base::unique_fd());
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800957 }
958 }
959
960 postFramebuffer();
961
962 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
963
964 prepareFrame();
965}
966
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800967void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800968 ATRACE_CALL();
969 ALOGV(__FUNCTION__);
970
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700971 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800972 return;
973 }
974
975 // Repaint the framebuffer (if needed), getting the optional fence for when
976 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800977 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800978 if (!optReadyFence) {
979 return;
980 }
981
982 // swap buffers (presentation)
983 mRenderSurface->queueBuffer(std::move(*optReadyFence));
984}
985
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800986std::optional<base::unique_fd> Output::composeSurfaces(
987 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800988 ATRACE_CALL();
989 ALOGV(__FUNCTION__);
990
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700991 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800992 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800993 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700994 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700995
996 auto& renderEngine = getCompositionEngine().getRenderEngine();
997 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
998
999 // If we the display is secure, protected content support is enabled, and at
1000 // least one layer has protected content, we need to use a secure back
1001 // buffer.
1002 if (outputState.isSecure && supportsProtectedContent) {
1003 auto layers = getOutputLayersOrderedByZ();
1004 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1005 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1006 });
1007 if (needsProtected != renderEngine.isProtected()) {
1008 renderEngine.useProtectedContext(needsProtected);
1009 }
1010 if (needsProtected != mRenderSurface->isProtected() &&
1011 needsProtected == renderEngine.isProtected()) {
1012 mRenderSurface->setProtected(needsProtected);
1013 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001014 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1015 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001016 }
1017
1018 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001019
1020 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001021
1022 // If we aren't doing client composition on this output, but do have a
1023 // flipClientTarget request for this frame on this output, we still need to
1024 // dequeue a buffer.
1025 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001026 tex = mRenderSurface->dequeueBuffer(&fd);
1027 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001028 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1029 "client composition for this frame",
1030 mName.c_str());
1031 return {};
1032 }
1033 }
1034
Lloyd Pique688abd42019-02-15 15:42:24 -08001035 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001036 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001037 return base::unique_fd();
Lloyd Pique688abd42019-02-15 15:42:24 -08001038 }
1039
1040 ALOGV("hasClientComposition");
1041
Lloyd Pique688abd42019-02-15 15:42:24 -08001042 renderengine::DisplaySettings clientCompositionDisplay;
Angel Aguayob084e0c2021-08-04 23:27:28 +00001043 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.getContent();
1044 clientCompositionDisplay.clip = outputState.layerStackSpace.getContent();
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001045 clientCompositionDisplay.orientation =
Angel Aguayob084e0c2021-08-04 23:27:28 +00001046 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001047 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1048 ? outputState.dataspace
1049 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001050
1051 // If we have a valid current display brightness use that, otherwise fall back to the
1052 // display's max desired
1053 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1054 ? outputState.displayBrightnessNits
1055 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1056 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001057
1058 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001059 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1060 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001061 }
1062
Lloyd Pique688abd42019-02-15 15:42:24 -08001063 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001064 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001065 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001066 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001067 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1068
Vishnu Nair9b079a22020-01-21 14:36:08 -08001069 // Check if the client composition requests were rendered into the provided graphic buffer. If
1070 // so, we can reuse the buffer and avoid client composition.
1071 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001072 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1073 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001074 clientCompositionLayers)) {
1075 outputCompositionState.reusedClientComposition = true;
1076 setExpensiveRenderingExpected(false);
Sally Qi4cabdd02021-08-05 16:45:57 -07001077 return base::unique_fd();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001078 }
Alec Mouria90a5702021-04-16 16:36:21 +00001079 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001080 clientCompositionLayers);
1081 }
1082
Lloyd Pique688abd42019-02-15 15:42:24 -08001083 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001084 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1085 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1086 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001087 const bool expensiveBlurs =
1088 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001089 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001090 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001091 if (expensiveRenderingExpected) {
1092 setExpensiveRenderingExpected(true);
1093 }
1094
Vishnu Nair9b079a22020-01-21 14:36:08 -08001095 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1096 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1097 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1098 std::back_inserter(clientCompositionLayerPointers),
1099 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1100 return &settings;
1101 });
1102
Alec Mourie4034bb2019-11-19 12:45:54 -08001103 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001104 // Only use the framebuffer cache when rendering to an internal display
1105 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1106 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1107 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1108 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1109 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001110 const bool useFramebufferCache = outputState.layerFilter.toInternalDisplay;
Sally Qi4cabdd02021-08-05 16:45:57 -07001111 auto [status, drawFence] =
1112 renderEngine
1113 .drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
1114 useFramebufferCache, std::move(fd))
1115 .get();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001116
1117 if (status != NO_ERROR && mClientCompositionRequestCache) {
1118 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001119 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001120 }
1121
Alec Mourie4034bb2019-11-19 12:45:54 -08001122 auto& timeStats = getCompositionEngine().getTimeStats();
Sally Qi4cabdd02021-08-05 16:45:57 -07001123 if (drawFence.get() < 0) {
Alec Mourie4034bb2019-11-19 12:45:54 -08001124 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1125 } else {
1126 timeStats.recordRenderEngineDuration(renderEngineStart,
1127 std::make_shared<FenceTime>(
Sally Qi4cabdd02021-08-05 16:45:57 -07001128 new Fence(dup(drawFence.get()))));
Alec Mourie4034bb2019-11-19 12:45:54 -08001129 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001130
Sally Qi4cabdd02021-08-05 16:45:57 -07001131 return std::move(drawFence);
Lloyd Pique688abd42019-02-15 15:42:24 -08001132}
1133
Vishnu Nair9b079a22020-01-21 14:36:08 -08001134std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Sally Qidf3da512021-07-08 17:27:02 +00001135 bool supportsProtectedContent, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001136 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001137 ALOGV("Rendering client layers");
1138
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001139 const auto& outputState = getState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001140 const Region viewportRegion(outputState.layerStackSpace.getContent());
Lloyd Pique688abd42019-02-15 15:42:24 -08001141 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001142
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001143 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001144 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001145
Lloyd Pique01c77c12019-04-17 12:48:32 -07001146 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001147 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001148 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001149 auto& layerFE = layer->getLayerFE();
1150
Lloyd Piquea2468662019-03-07 21:31:06 -08001151 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001152 ALOGV("Layer: %s", layerFE.getDebugName());
1153 if (clip.isEmpty()) {
1154 ALOGV(" Skipping for empty clip");
1155 firstLayer = false;
1156 continue;
1157 }
1158
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001159 disableBlurs |= layerFEState->sidebandStream != nullptr;
1160
Vishnu Naira483b4a2019-12-12 15:07:52 -08001161 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001162
1163 // We clear the client target for non-client composed layers if
1164 // requested by the HWC. We skip this if the layer is not an opaque
1165 // rectangle, as by definition the layer must blend with whatever is
1166 // underneath. We also skip the first layer as the buffer target is
1167 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001168 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001169 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001170
1171 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1172
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001173 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1174 // composing the non-shadow content and only draw the shadows.
1175 const bool realContentIsVisible = clientComposition &&
1176 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1177
Lloyd Pique688abd42019-02-15 15:42:24 -08001178 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001179 std::vector<LayerFE::LayerSettings> results;
1180 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001181 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001182 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001183 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001184 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1185 } else {
1186 ALOGV("Skipping redundant override buffer for [%s] in RE",
1187 layer->getLayerFE().getDebugName());
1188 }
Dan Stoza6166c312021-01-15 16:34:05 -08001189 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001190 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1191 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1192 : (layer->getState().overrideInfo.disableBackgroundBlur
1193 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1194 BlurRegionsOnly
1195 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1196 Enabled);
1197 compositionengine::LayerFE::ClientCompositionTargetSettings
1198 targetSettings{.clip = clip,
1199 .needsFiltering = layer->needsFiltering() ||
1200 outputState.needsFiltering,
1201 .isSecure = outputState.isSecure,
1202 .supportsProtectedContent = supportsProtectedContent,
Angel Aguayob084e0c2021-08-04 23:27:28 +00001203 .viewport = outputState.layerStackSpace.getContent(),
Alec Mourif54453c2021-05-13 16:28:28 -07001204 .dataspace = outputDataspace,
1205 .realContentIsVisible = realContentIsVisible,
1206 .clearContent = !clientComposition,
1207 .blurSetting = blurSetting};
Dan Stoza6166c312021-01-15 16:34:05 -08001208 results = layerFE.prepareClientCompositionList(targetSettings);
1209 if (realContentIsVisible && !results.empty()) {
1210 layer->editState().clientCompositionTimestamp = systemTime();
1211 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001212 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001213
1214 clientCompositionLayers.insert(clientCompositionLayers.end(),
1215 std::make_move_iterator(results.begin()),
1216 std::make_move_iterator(results.end()));
1217 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001218 }
1219
1220 firstLayer = false;
1221 }
1222
1223 return clientCompositionLayers;
1224}
1225
1226void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001227 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001228 if (flashRegion.isEmpty()) {
1229 return;
1230 }
1231
Vishnu Nair9b079a22020-01-21 14:36:08 -08001232 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001233 layerSettings.source.buffer.buffer = nullptr;
1234 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1235 layerSettings.alpha = half(1.0);
1236
1237 for (const auto& rect : flashRegion) {
1238 layerSettings.geometry.boundaries = rect.toFloatRect();
1239 clientCompositionLayers.push_back(layerSettings);
1240 }
1241}
1242
1243void Output::setExpensiveRenderingExpected(bool) {
1244 // The base class does nothing with this call.
1245}
1246
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001247void Output::postFramebuffer() {
1248 ATRACE_CALL();
1249 ALOGV(__FUNCTION__);
1250
1251 if (!getState().isEnabled) {
1252 return;
1253 }
1254
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001255 auto& outputState = editState();
1256 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001257 mRenderSurface->flip();
1258
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001259 auto frame = presentAndGetFrameFences();
1260
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001261 mRenderSurface->onPresentDisplayCompleted();
1262
Lloyd Pique01c77c12019-04-17 12:48:32 -07001263 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001264 // The layer buffer from the previous frame (if any) is released
1265 // by HWC only when the release fence from this frame (if any) is
1266 // signaled. Always get the release fence from HWC first.
1267 sp<Fence> releaseFence = Fence::NO_FENCE;
1268
1269 if (auto hwcLayer = layer->getHwcLayer()) {
1270 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1271 releaseFence = f->second;
1272 }
1273 }
1274
1275 // If the layer was client composited in the previous frame, we
1276 // need to merge with the previous client target acquire fence.
1277 // Since we do not track that, always merge with the current
1278 // client target acquire fence when it is available, even though
1279 // this is suboptimal.
1280 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001281 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001282 releaseFence =
1283 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1284 }
1285
1286 layer->getLayerFE().onLayerDisplayed(releaseFence);
1287 }
1288
1289 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001290 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001291 // supply them with the present fence.
1292 for (auto& weakLayer : mReleasedLayers) {
1293 if (auto layer = weakLayer.promote(); layer != nullptr) {
1294 layer->onLayerDisplayed(frame.presentFence);
1295 }
1296 }
1297
1298 // Clear out the released layers now that we're done with them.
1299 mReleasedLayers.clear();
1300}
1301
Alec Mouriaa831582021-06-07 16:23:01 -07001302void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001303 if (mPlanner) {
Alec Mouriaa831582021-06-07 16:23:01 -07001304 mPlanner->renderCachedSets(getState(), refreshArgs.nextInvalidateTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001305 }
1306}
1307
Lloyd Pique32cbe282018-10-19 13:09:22 -07001308void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001309 auto& outputState = editState();
Angel Aguayob084e0c2021-08-04 23:27:28 +00001310 outputState.dirtyRegion.set(outputState.displaySpace.getBoundsAsRect());
Lloyd Pique32cbe282018-10-19 13:09:22 -07001311}
1312
Lloyd Pique66d68602019-02-13 14:23:31 -08001313void Output::chooseCompositionStrategy() {
1314 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001315 auto& outputState = editState();
1316 outputState.usesClientComposition = true;
1317 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001318 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001319}
1320
Lloyd Pique688abd42019-02-15 15:42:24 -08001321bool Output::getSkipColorTransform() const {
1322 return true;
1323}
1324
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001325compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1326 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001327 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001328 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1329 }
1330 return result;
1331}
1332
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001333} // namespace impl
1334} // namespace android::compositionengine