blob: 6ac488be29600f8d7130872424ebf4a1e91d8720 [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
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200159 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200160 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
161 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200162
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200163 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200164 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
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 }
168 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200169 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
170
171 // Compute displaySpace.content
172 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
173 ui::Transform rotation;
174 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
175 const auto displaySize = outputState.displaySpace.bounds;
176 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
177 }
178 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200179
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180 // Compute framebufferSpace
181 outputState.framebufferSpace.orientation = orientation;
182 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
183 "The framebuffer bounds are unknown.");
184 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200185 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200186 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
187
188 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200189 outputState.layerStackSpace.content = layerStackSpaceRect;
190 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200192 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
193 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700194 dirtyEntireOutput();
195}
196
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200197void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200199
200 auto& state = editState();
201
202 // Update framebuffer space
203 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200204 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200205
206 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200207 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200208 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
209
210 // Update oriented display space
211 const auto orientation = state.displaySpace.orientation;
212 ui::Size orientedSize = size;
213 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
214 std::swap(orientedSize.width, orientedSize.height);
215 }
216 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200217 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700218
Dan Stoza6166c312021-01-15 16:34:05 -0800219 if (mPlanner) {
220 mPlanner->setDisplaySize(size);
221 }
222
Lloyd Pique32cbe282018-10-19 13:09:22 -0700223 dirtyEntireOutput();
224}
225
Garfield Tan54edd912020-10-21 16:31:41 -0700226ui::Transform::RotationFlags Output::getTransformHint() const {
227 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
228}
229
Lloyd Piqueef36b002019-01-23 17:52:04 -0800230void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700231 auto& outputState = editState();
232 outputState.layerStackId = layerStackId;
233 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700234
235 dirtyEntireOutput();
236}
237
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800238void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700239 auto& colorTransformMatrix = editState().colorTransformMatrix;
240 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700241 return;
242 }
243
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700244 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800245
246 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700247}
248
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800249void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700250 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800251 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
252 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800253
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700254 auto& outputState = editState();
255 if (outputState.colorMode == colorProfile.mode &&
256 outputState.dataspace == colorProfile.dataspace &&
257 outputState.renderIntent == colorProfile.renderIntent &&
258 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800259 return;
260 }
261
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 outputState.colorMode = colorProfile.mode;
263 outputState.dataspace = colorProfile.dataspace;
264 outputState.renderIntent = colorProfile.renderIntent;
265 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800267 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700268
Lloyd Pique32cbe282018-10-19 13:09:22 -0700269 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800270 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
271 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800272
273 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700274}
275
John Reckac09e452021-04-07 16:35:37 -0400276void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
277 auto& outputState = editState();
278 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
279 outputState.displayBrightnessNits == displayBrightnessNits) {
280 // Nothing changed
281 return;
282 }
283 outputState.sdrWhitePointNits = sdrWhitePointNits;
284 outputState.displayBrightnessNits = displayBrightnessNits;
285 dirtyEntireOutput();
286}
287
Lloyd Pique32cbe282018-10-19 13:09:22 -0700288void Output::dump(std::string& out) const {
289 using android::base::StringAppendF;
290
291 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
292
293 out.append("\n ");
294
295 dumpBase(out);
296}
297
298void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700299 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700300
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700301 if (mDisplayColorProfile) {
302 mDisplayColorProfile->dump(out);
303 } else {
304 out.append(" No display color profile!\n");
305 }
306
Lloyd Pique31cb2942018-10-19 17:23:03 -0700307 if (mRenderSurface) {
308 mRenderSurface->dump(out);
309 } else {
310 out.append(" No render surface!\n");
311 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800312
Lloyd Pique01c77c12019-04-17 12:48:32 -0700313 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
314 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800315 if (!outputLayer) {
316 continue;
317 }
318 outputLayer->dump(out);
319 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700320}
321
Dan Stoza269dc4d2021-01-15 15:07:43 -0800322void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
323 if (!mPlanner) {
324 base::StringAppendF(&out, "Planner is disabled\n");
325 return;
326 }
327 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
328 mPlanner->dump(args, out);
329}
330
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700331compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
332 return mDisplayColorProfile.get();
333}
334
335void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
336 mDisplayColorProfile = std::move(mode);
337}
338
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800339const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
340 return mReleasedLayers;
341}
342
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700343void Output::setDisplayColorProfileForTest(
344 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
345 mDisplayColorProfile = std::move(mode);
346}
347
Lloyd Pique31cb2942018-10-19 17:23:03 -0700348compositionengine::RenderSurface* Output::getRenderSurface() const {
349 return mRenderSurface.get();
350}
351
352void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
353 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800354 const auto size = mRenderSurface->getSize();
355 editState().framebufferSpace.bounds = Rect(size);
356 if (mPlanner) {
357 mPlanner->setDisplaySize(size);
358 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700359 dirtyEntireOutput();
360}
361
Vishnu Nair9b079a22020-01-21 14:36:08 -0800362void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
363 if (cacheSize == 0) {
364 mClientCompositionRequestCache.reset();
365 } else {
366 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
367 }
368};
369
Lloyd Pique31cb2942018-10-19 17:23:03 -0700370void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
371 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700372}
373
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000374Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700375 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200376 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000377 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700378 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700379 }
380 return dirty;
381}
382
Lloyd Piquec6687342019-03-07 21:34:57 -0800383bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800384 // The layerStackId's must match, and also the layer must not be internal
385 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700386 const auto& outputState = getState();
387 return layerStackId && (*layerStackId == outputState.layerStackId) &&
388 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700389}
390
Lloyd Piquede196652020-01-22 17:29:58 -0800391bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
392 const auto* layerFEState = layerFE->getCompositionState();
393 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800394}
395
Lloyd Piquedf336d92019-03-07 21:38:42 -0800396std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800397 const sp<LayerFE>& layerFE) const {
398 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800399}
400
Lloyd Piquede196652020-01-22 17:29:58 -0800401compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
402 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700403 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800404}
405
Lloyd Pique01c77c12019-04-17 12:48:32 -0700406std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800407 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700408 for (size_t i = 0; i < getOutputLayerCount(); i++) {
409 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800410 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700411 return i;
412 }
413 }
414 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800415}
416
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800417void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
418 mReleasedLayers = std::move(layers);
419}
420
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800421void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
422 LayerFESet& geomSnapshots) {
423 ATRACE_CALL();
424 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800425
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800426 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800427}
428
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800429void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800430 ATRACE_CALL();
431 ALOGV(__FUNCTION__);
432
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800433 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800434 updateCompositionState(refreshArgs);
435 planComposition();
436 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800437 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800438 beginFrame();
439 prepareFrame();
440 devOptRepaintFlash(refreshArgs);
441 finishFrame(refreshArgs);
442 postFramebuffer();
Alec Mouriaa831582021-06-07 16:23:01 -0700443 renderCachedSets(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800444}
445
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800446void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
447 LayerFESet& layerFESet) {
448 ATRACE_CALL();
449 ALOGV(__FUNCTION__);
450
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700451 auto& outputState = editState();
452
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800453 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700454 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800455 return;
456 }
457
458 // Process the layers to determine visibility and coverage
459 compositionengine::Output::CoverageState coverage{layerFESet};
460 collectVisibleLayers(refreshArgs, coverage);
461
462 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700463 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200464 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
466
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700467 outputState.undefinedRegion = undefinedRegion;
468 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800469}
470
471void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
472 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 // Evaluate the layers from front to back to determine what is visible. This
474 // also incrementally calculates the coverage information for each layer as
475 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800476 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700477 // Incrementally process the coverage for each layer
478 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800479
480 // TODO(b/121291683): Stop early if the output is completely covered and
481 // no more layers could even be visible underneath the ones on top.
482 }
483
Lloyd Pique01c77c12019-04-17 12:48:32 -0700484 setReleasedLayers(refreshArgs);
485
486 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487}
488
Lloyd Piquede196652020-01-22 17:29:58 -0800489void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700490 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800491 // Ensure we have a snapshot of the basic geometry layer state. Limit the
492 // snapshots to once per frame for each candidate layer, as layers may
493 // appear on multiple outputs.
494 if (!coverage.latchedLayers.count(layerFE)) {
495 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800496 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800497 }
498
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800499 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800500 if (!belongsInOutput(layerFE)) {
501 return;
502 }
503
504 // Obtain a read-only pointer to the front-end layer state
505 const auto* layerFEState = layerFE->getCompositionState();
506 if (CC_UNLIKELY(!layerFEState)) {
507 return;
508 }
509
510 // handle hidden surfaces by setting the visible region to empty
511 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700512 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800513 }
514
515 /*
516 * opaqueRegion: area of a surface that is fully opaque.
517 */
518 Region opaqueRegion;
519
520 /*
521 * visibleRegion: area of a surface that is visible on screen and not fully
522 * transparent. This is essentially the layer's footprint minus the opaque
523 * regions above it. Areas covered by a translucent surface are considered
524 * visible.
525 */
526 Region visibleRegion;
527
528 /*
529 * coveredRegion: area of a surface that is covered by all visible regions
530 * above it (which includes the translucent areas).
531 */
532 Region coveredRegion;
533
534 /*
535 * transparentRegion: area of a surface that is hinted to be completely
536 * transparent. This is only used to tell when the layer has no visible non-
537 * transparent regions and can be removed from the layer list. It does not
538 * affect the visibleRegion of this layer or any layers beneath it. The hint
539 * may not be correct if apps don't respect the SurfaceView restrictions
540 * (which, sadly, some don't).
541 */
542 Region transparentRegion;
543
Vishnu Naira483b4a2019-12-12 15:07:52 -0800544 /*
545 * shadowRegion: Region cast by the layer's shadow.
546 */
547 Region shadowRegion;
548
Lloyd Piquede196652020-01-22 17:29:58 -0800549 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800550
551 // Get the visible region
552 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
553 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800554 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800555 visibleRegion.set(visibleRect);
556
Lloyd Piquede196652020-01-22 17:29:58 -0800557 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800558 // if the layer casts a shadow, offset the layers visible region and
559 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800560 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800561 Rect visibleRectWithShadows(visibleRect);
562 visibleRectWithShadows.inset(inset, inset, inset, inset);
563 visibleRegion.set(visibleRectWithShadows);
564 shadowRegion = visibleRegion.subtract(visibleRect);
565 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800566
567 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700568 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800569 }
570
571 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800572 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800573 if (tr.preserveRects()) {
574 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800575 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800576 } else {
577 // transformation too complex, can't do the
578 // transparent region optimization.
579 transparentRegion.clear();
580 }
581 }
582
583 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800584 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800585 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800586 // If we one of the simple category of transforms (0/90/180/270 rotation
587 // + any flip), then the opaque region is the layer's footprint.
588 // Otherwise we don't try and compute the opaque region since there may
589 // be errors at the edges, and we treat the entire layer as
590 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800591 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800592 }
593
594 // Clip the covered region to the visible region
595 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
596
597 // Update accumAboveCoveredLayers for next (lower) layer
598 coverage.aboveCoveredLayers.orSelf(visibleRegion);
599
600 // subtract the opaque region covered by the layers above us
601 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
602
603 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700604 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800605 }
606
607 // Get coverage information for the layer as previously displayed,
608 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800609 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700610 auto prevOutputLayer =
611 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800612
613 // Get coverage information for the layer as previously displayed
614 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
615 const Region kEmptyRegion;
616 const Region& oldVisibleRegion =
617 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
618 const Region& oldCoveredRegion =
619 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
620
621 // compute this layer's dirty region
622 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800623 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800624 // we need to invalidate the whole region
625 dirty = visibleRegion;
626 // as well, as the old visible region
627 dirty.orSelf(oldVisibleRegion);
628 } else {
629 /* compute the exposed region:
630 * the exposed region consists of two components:
631 * 1) what's VISIBLE now and was COVERED before
632 * 2) what's EXPOSED now less what was EXPOSED before
633 *
634 * note that (1) is conservative, we start with the whole visible region
635 * but only keep what used to be covered by something -- which mean it
636 * may have been exposed.
637 *
638 * (2) handles areas that were not covered by anything but got exposed
639 * because of a resize.
640 *
641 */
642 const Region newExposed = visibleRegion - coveredRegion;
643 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
644 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
645 }
646 dirty.subtractSelf(coverage.aboveOpaqueLayers);
647
648 // accumulate to the screen dirty region
649 coverage.dirtyRegion.orSelf(dirty);
650
651 // Update accumAboveOpaqueLayers for next (lower) layer
652 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
653
654 // Compute the visible non-transparent region
655 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
656
Vishnu Naira483b4a2019-12-12 15:07:52 -0800657 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800658 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700659 const auto& outputState = getState();
660 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200661 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800662 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700663 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800664 }
665
Vishnu Naira483b4a2019-12-12 15:07:52 -0800666 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
667
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800668 // The layer is visible. Either reuse the existing outputLayer if we have
669 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800670 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800671
672 // Store the layer coverage information into the layer state as some of it
673 // is useful later.
674 auto& outputLayerState = result->editState();
675 outputLayerState.visibleRegion = visibleRegion;
676 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
677 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200678 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
679 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800680 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800681}
682
683void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
684 // The base class does nothing with this call.
685}
686
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800687void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700688 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800689 layer->getLayerFE().prepareCompositionState(
690 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
691 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800692 }
693}
694
Dan Stoza269dc4d2021-01-15 15:07:43 -0800695void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800696 ATRACE_CALL();
697 ALOGV(__FUNCTION__);
698
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800699 if (!getState().isEnabled) {
700 return;
701 }
702
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800703 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
704 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
705
Lloyd Pique01c77c12019-04-17 12:48:32 -0700706 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700707 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800708 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200709 forceClientComposition,
710 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800711
712 if (mLayerRequestingBackgroundBlur == layer) {
713 forceClientComposition = false;
714 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800715 }
716}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800717
Dan Stoza269dc4d2021-01-15 15:07:43 -0800718void Output::planComposition() {
719 if (!mPlanner || !getState().isEnabled) {
720 return;
721 }
722
723 ATRACE_CALL();
724 ALOGV(__FUNCTION__);
725
726 mPlanner->plan(getOutputLayersOrderedByZ());
727}
728
729void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
730 ATRACE_CALL();
731 ALOGV(__FUNCTION__);
732
733 if (!getState().isEnabled) {
734 return;
735 }
736
Ady Abraham3645e642021-04-20 18:39:00 -0700737 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700738 editState().previousPresentFence = refreshArgs.previousPresentFence;
Ady Abraham3645e642021-04-20 18:39:00 -0700739
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400740 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800741 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400742 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400743 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400744 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800745 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400746 if (layer == peekThroughLayer) {
747 // No longer needed, although it should not show up again, so
748 // resetting it is not truly needed either.
749 peekThroughLayer = nullptr;
750
751 // peekThroughLayer was already drawn ahead of its z order.
752 continue;
753 }
Dan Stoza6166c312021-01-15 16:34:05 -0800754 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400755 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400756 if (overrideInfo.buffer != nullptr) {
757 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800758 ALOGV("Skipping redundant buffer");
759 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400760 } else {
761 // First layer with the override buffer.
762 if (overrideInfo.peekThroughLayer) {
763 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400764
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400765 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400766 overrideZ = true;
767 includeGeometry = true;
768 constexpr bool isPeekingThrough = true;
769 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
770 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400771 }
772
773 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800774 }
Dan Stoza6166c312021-01-15 16:34:05 -0800775 }
776
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400777 constexpr bool isPeekingThrough = false;
778 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800779 }
780}
781
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800782compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
783 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
784 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100785 auto* compState = layer->getLayerFE().getCompositionState();
786
787 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
788 // want to force client composition because of the blur.
789 if (compState->sidebandStream != nullptr) {
790 return nullptr;
791 }
792 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();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800911 const bool dirty = !getDirtyRegion(false).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) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800963 // transform the dirty region into this screen's coordinate space
964 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
965 if (!dirtyRegion.isEmpty()) {
966 base::unique_fd readyFence;
967 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800968 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800969
970 mRenderSurface->queueBuffer(std::move(readyFence));
971 }
972 }
973
974 postFramebuffer();
975
976 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
977
978 prepareFrame();
979}
980
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800981void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800982 ATRACE_CALL();
983 ALOGV(__FUNCTION__);
984
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700985 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800986 return;
987 }
988
989 // Repaint the framebuffer (if needed), getting the optional fence for when
990 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800991 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800992 if (!optReadyFence) {
993 return;
994 }
995
996 // swap buffers (presentation)
997 mRenderSurface->queueBuffer(std::move(*optReadyFence));
998}
999
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001000std::optional<base::unique_fd> Output::composeSurfaces(
1001 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001002 ATRACE_CALL();
1003 ALOGV(__FUNCTION__);
1004
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001005 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -08001006 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001007 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001008 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -07001009
1010 auto& renderEngine = getCompositionEngine().getRenderEngine();
1011 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
1012
1013 // If we the display is secure, protected content support is enabled, and at
1014 // least one layer has protected content, we need to use a secure back
1015 // buffer.
1016 if (outputState.isSecure && supportsProtectedContent) {
1017 auto layers = getOutputLayersOrderedByZ();
1018 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1019 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1020 });
1021 if (needsProtected != renderEngine.isProtected()) {
1022 renderEngine.useProtectedContext(needsProtected);
1023 }
1024 if (needsProtected != mRenderSurface->isProtected() &&
1025 needsProtected == renderEngine.isProtected()) {
1026 mRenderSurface->setProtected(needsProtected);
1027 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001028 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1029 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001030 }
1031
1032 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001033
1034 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001035
1036 // If we aren't doing client composition on this output, but do have a
1037 // flipClientTarget request for this frame on this output, we still need to
1038 // dequeue a buffer.
1039 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001040 tex = mRenderSurface->dequeueBuffer(&fd);
1041 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001042 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1043 "client composition for this frame",
1044 mName.c_str());
1045 return {};
1046 }
1047 }
1048
Lloyd Piqued3d69882019-02-28 16:03:46 -08001049 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001050 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001051 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001052 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001053 }
1054
1055 ALOGV("hasClientComposition");
1056
Lloyd Pique688abd42019-02-15 15:42:24 -08001057 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001058 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001059 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001060 clientCompositionDisplay.orientation =
1061 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001062 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1063 ? outputState.dataspace
1064 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001065
1066 // If we have a valid current display brightness use that, otherwise fall back to the
1067 // display's max desired
1068 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1069 ? outputState.displayBrightnessNits
1070 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1071 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001072
1073 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001074 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1075 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001076 }
1077
Lloyd Pique688abd42019-02-15 15:42:24 -08001078 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001079 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001080 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001081 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001082 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1083
Vishnu Nair9b079a22020-01-21 14:36:08 -08001084 // Check if the client composition requests were rendered into the provided graphic buffer. If
1085 // so, we can reuse the buffer and avoid client composition.
1086 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001087 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1088 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001089 clientCompositionLayers)) {
1090 outputCompositionState.reusedClientComposition = true;
1091 setExpensiveRenderingExpected(false);
1092 return readyFence;
1093 }
Alec Mouria90a5702021-04-16 16:36:21 +00001094 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001095 clientCompositionLayers);
1096 }
1097
Lloyd Pique688abd42019-02-15 15:42:24 -08001098 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001099 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1100 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1101 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001102 const bool expensiveBlurs =
1103 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001104 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001105 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001106 if (expensiveRenderingExpected) {
1107 setExpensiveRenderingExpected(true);
1108 }
1109
Vishnu Nair9b079a22020-01-21 14:36:08 -08001110 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1111 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1112 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1113 std::back_inserter(clientCompositionLayerPointers),
1114 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1115 return &settings;
1116 });
1117
Alec Mourie4034bb2019-11-19 12:45:54 -08001118 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001119 // Only use the framebuffer cache when rendering to an internal display
1120 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1121 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1122 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1123 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1124 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1125 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001126 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001127 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001128 useFramebufferCache, std::move(fd), &readyFence);
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();
1136 if (readyFence.get() < 0) {
1137 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1138 } else {
1139 timeStats.recordRenderEngineDuration(renderEngineStart,
1140 std::make_shared<FenceTime>(
1141 new Fence(dup(readyFence.get()))));
1142 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001143
Lloyd Piqued3d69882019-02-28 16:03:46 -08001144 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001145}
1146
Vishnu Nair9b079a22020-01-21 14:36:08 -08001147std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Sally Qidf3da512021-07-08 17:27:02 +00001148 bool supportsProtectedContent, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001149 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001150 ALOGV("Rendering client layers");
1151
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001152 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001153 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001154 bool firstLayer = true;
Lloyd Pique688abd42019-02-15 15:42:24 -08001155
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001156 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001157 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001158
Lloyd Pique01c77c12019-04-17 12:48:32 -07001159 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001160 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001161 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001162 auto& layerFE = layer->getLayerFE();
1163
Lloyd Piquea2468662019-03-07 21:31:06 -08001164 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001165 ALOGV("Layer: %s", layerFE.getDebugName());
1166 if (clip.isEmpty()) {
1167 ALOGV(" Skipping for empty clip");
1168 firstLayer = false;
1169 continue;
1170 }
1171
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001172 disableBlurs |= layerFEState->sidebandStream != nullptr;
1173
Vishnu Naira483b4a2019-12-12 15:07:52 -08001174 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001175
1176 // We clear the client target for non-client composed layers if
1177 // requested by the HWC. We skip this if the layer is not an opaque
1178 // rectangle, as by definition the layer must blend with whatever is
1179 // underneath. We also skip the first layer as the buffer target is
1180 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001181 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001182 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001183
1184 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1185
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001186 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1187 // composing the non-shadow content and only draw the shadows.
1188 const bool realContentIsVisible = clientComposition &&
1189 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1190
Lloyd Pique688abd42019-02-15 15:42:24 -08001191 if (clientComposition || clearClientComposition) {
Dan Stoza6166c312021-01-15 16:34:05 -08001192 std::vector<LayerFE::LayerSettings> results;
1193 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001194 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001195 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001196 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001197 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1198 } else {
1199 ALOGV("Skipping redundant override buffer for [%s] in RE",
1200 layer->getLayerFE().getDebugName());
1201 }
Dan Stoza6166c312021-01-15 16:34:05 -08001202 } else {
Alec Mourif54453c2021-05-13 16:28:28 -07001203 LayerFE::ClientCompositionTargetSettings::BlurSetting blurSetting = disableBlurs
1204 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled
1205 : (layer->getState().overrideInfo.disableBackgroundBlur
1206 ? LayerFE::ClientCompositionTargetSettings::BlurSetting::
1207 BlurRegionsOnly
1208 : LayerFE::ClientCompositionTargetSettings::BlurSetting::
1209 Enabled);
1210 compositionengine::LayerFE::ClientCompositionTargetSettings
1211 targetSettings{.clip = clip,
1212 .needsFiltering = layer->needsFiltering() ||
1213 outputState.needsFiltering,
1214 .isSecure = outputState.isSecure,
1215 .supportsProtectedContent = supportsProtectedContent,
Alec Mourif54453c2021-05-13 16:28:28 -07001216 .viewport = outputState.layerStackSpace.content,
1217 .dataspace = outputDataspace,
1218 .realContentIsVisible = realContentIsVisible,
1219 .clearContent = !clientComposition,
1220 .blurSetting = blurSetting};
Dan Stoza6166c312021-01-15 16:34:05 -08001221 results = layerFE.prepareClientCompositionList(targetSettings);
1222 if (realContentIsVisible && !results.empty()) {
1223 layer->editState().clientCompositionTimestamp = systemTime();
1224 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001225 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001226
1227 clientCompositionLayers.insert(clientCompositionLayers.end(),
1228 std::make_move_iterator(results.begin()),
1229 std::make_move_iterator(results.end()));
1230 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001231 }
1232
1233 firstLayer = false;
1234 }
1235
1236 return clientCompositionLayers;
1237}
1238
1239void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001240 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001241 if (flashRegion.isEmpty()) {
1242 return;
1243 }
1244
Vishnu Nair9b079a22020-01-21 14:36:08 -08001245 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001246 layerSettings.source.buffer.buffer = nullptr;
1247 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1248 layerSettings.alpha = half(1.0);
1249
1250 for (const auto& rect : flashRegion) {
1251 layerSettings.geometry.boundaries = rect.toFloatRect();
1252 clientCompositionLayers.push_back(layerSettings);
1253 }
1254}
1255
1256void Output::setExpensiveRenderingExpected(bool) {
1257 // The base class does nothing with this call.
1258}
1259
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001260void Output::postFramebuffer() {
1261 ATRACE_CALL();
1262 ALOGV(__FUNCTION__);
1263
1264 if (!getState().isEnabled) {
1265 return;
1266 }
1267
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001268 auto& outputState = editState();
1269 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001270 mRenderSurface->flip();
1271
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001272 auto frame = presentAndGetFrameFences();
1273
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001274 mRenderSurface->onPresentDisplayCompleted();
1275
Lloyd Pique01c77c12019-04-17 12:48:32 -07001276 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001277 // The layer buffer from the previous frame (if any) is released
1278 // by HWC only when the release fence from this frame (if any) is
1279 // signaled. Always get the release fence from HWC first.
1280 sp<Fence> releaseFence = Fence::NO_FENCE;
1281
1282 if (auto hwcLayer = layer->getHwcLayer()) {
1283 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1284 releaseFence = f->second;
1285 }
1286 }
1287
1288 // If the layer was client composited in the previous frame, we
1289 // need to merge with the previous client target acquire fence.
1290 // Since we do not track that, always merge with the current
1291 // client target acquire fence when it is available, even though
1292 // this is suboptimal.
1293 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001294 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001295 releaseFence =
1296 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1297 }
1298
1299 layer->getLayerFE().onLayerDisplayed(releaseFence);
1300 }
1301
1302 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001303 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001304 // supply them with the present fence.
1305 for (auto& weakLayer : mReleasedLayers) {
1306 if (auto layer = weakLayer.promote(); layer != nullptr) {
1307 layer->onLayerDisplayed(frame.presentFence);
1308 }
1309 }
1310
1311 // Clear out the released layers now that we're done with them.
1312 mReleasedLayers.clear();
1313}
1314
Alec Mouriaa831582021-06-07 16:23:01 -07001315void Output::renderCachedSets(const CompositionRefreshArgs& refreshArgs) {
Dan Stoza6166c312021-01-15 16:34:05 -08001316 if (mPlanner) {
Alec Mouriaa831582021-06-07 16:23:01 -07001317 mPlanner->renderCachedSets(getState(), refreshArgs.nextInvalidateTime);
Dan Stoza6166c312021-01-15 16:34:05 -08001318 }
1319}
1320
Lloyd Pique32cbe282018-10-19 13:09:22 -07001321void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001322 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001323 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001324}
1325
Lloyd Pique66d68602019-02-13 14:23:31 -08001326void Output::chooseCompositionStrategy() {
1327 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001328 auto& outputState = editState();
1329 outputState.usesClientComposition = true;
1330 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001331 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001332}
1333
Lloyd Pique688abd42019-02-15 15:42:24 -08001334bool Output::getSkipColorTransform() const {
1335 return true;
1336}
1337
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001338compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1339 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001340 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001341 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1342 }
1343 return result;
1344}
1345
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001346} // namespace impl
1347} // namespace android::compositionengine