blob: 4f99495ff22def13f8e5c6082181feb331fe70b8 [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
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080017#include <thread>
18
Lloyd Pique32cbe282018-10-19 13:09:22 -070019#include <android-base/stringprintf.h>
20#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070024#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080030
31// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
34
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include <renderengine/DisplaySettings.h>
36#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic pop // ignored "-Wconversion"
40
Lloyd Pique32cbe282018-10-19 13:09:22 -070041#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080042#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080043#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070044
Lloyd Pique688abd42019-02-15 15:42:24 -080045#include "TracedOrdinal.h"
46
Lloyd Piquefeb73d72018-12-04 17:23:44 -080047namespace android::compositionengine {
48
49Output::~Output() = default;
50
51namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070052
Lloyd Piquec29e4c62019-03-07 21:48:19 -080053namespace {
54
55template <typename T>
56class Reversed {
57public:
58 explicit Reversed(const T& container) : mContainer(container) {}
59 auto begin() { return mContainer.rbegin(); }
60 auto end() { return mContainer.rend(); }
61
62private:
63 const T& mContainer;
64};
65
66// Helper for enumerating over a container in reverse order
67template <typename T>
68Reversed<T> reversed(const T& c) {
69 return Reversed<T>(c);
70}
71
Marin Shalamanovb15d2272020-09-17 21:41:52 +020072struct ScaleVector {
73 float x;
74 float y;
75};
76
77// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
78// where to' will have the same size as "to". In the case where "from" and "to"
79// start at the origin to'=to.
80ScaleVector getScale(const Rect& from, const Rect& to) {
81 return {.x = static_cast<float>(to.width()) / from.width(),
82 .y = static_cast<float>(to.height()) / from.height()};
83}
84
Lloyd Piquec29e4c62019-03-07 21:48:19 -080085} // namespace
86
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070087std::shared_ptr<Output> createOutput(
88 const compositionengine::CompositionEngine& compositionEngine) {
89 return createOutputTemplated<Output>(compositionEngine);
90}
Lloyd Pique32cbe282018-10-19 13:09:22 -070091
92Output::~Output() = default;
93
Lloyd Pique32cbe282018-10-19 13:09:22 -070094bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070095 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
96 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070097}
98
Lloyd Pique6c564cf2019-05-17 17:31:36 -070099std::optional<DisplayId> Output::getDisplayId() const {
100 return {};
101}
102
Lloyd Pique32cbe282018-10-19 13:09:22 -0700103const std::string& Output::getName() const {
104 return mName;
105}
106
107void Output::setName(const std::string& name) {
108 mName = name;
109}
110
111void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700112 auto& outputState = editState();
113 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114 return;
115 }
116
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700117 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700118 dirtyEntireOutput();
119}
120
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200121void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
122 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700123 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200124
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200125 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200126 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
127 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200128
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200129 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200130 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200131 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200132 std::swap(orientedSize.width, orientedSize.height);
133 }
134 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200135 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
136
137 // Compute displaySpace.content
138 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
139 ui::Transform rotation;
140 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
141 const auto displaySize = outputState.displaySpace.bounds;
142 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
143 }
144 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200145
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200146 // Compute framebufferSpace
147 outputState.framebufferSpace.orientation = orientation;
148 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
149 "The framebuffer bounds are unknown.");
150 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200151 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200152 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
153
154 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200155 outputState.layerStackSpace.content = layerStackSpaceRect;
156 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200157
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200158 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
159 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700160 dirtyEntireOutput();
161}
162
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200163void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700164 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200165
166 auto& state = editState();
167
168 // Update framebuffer space
169 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200170 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200171
172 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200173 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200174 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
175
176 // Update oriented display space
177 const auto orientation = state.displaySpace.orientation;
178 ui::Size orientedSize = size;
179 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
180 std::swap(orientedSize.width, orientedSize.height);
181 }
182 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200183 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700184
185 dirtyEntireOutput();
186}
187
Garfield Tan54edd912020-10-21 16:31:41 -0700188ui::Transform::RotationFlags Output::getTransformHint() const {
189 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
190}
191
Lloyd Piqueef36b002019-01-23 17:52:04 -0800192void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700193 auto& outputState = editState();
194 outputState.layerStackId = layerStackId;
195 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700196
197 dirtyEntireOutput();
198}
199
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800200void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700201 auto& colorTransformMatrix = editState().colorTransformMatrix;
202 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700203 return;
204 }
205
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700206 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800207
208 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700209}
210
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800211void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700212 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800213 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
214 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800215
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700216 auto& outputState = editState();
217 if (outputState.colorMode == colorProfile.mode &&
218 outputState.dataspace == colorProfile.dataspace &&
219 outputState.renderIntent == colorProfile.renderIntent &&
220 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800221 return;
222 }
223
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700224 outputState.colorMode = colorProfile.mode;
225 outputState.dataspace = colorProfile.dataspace;
226 outputState.renderIntent = colorProfile.renderIntent;
227 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700228
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800229 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700230
Lloyd Pique32cbe282018-10-19 13:09:22 -0700231 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800232 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
233 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800234
235 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700236}
237
238void Output::dump(std::string& out) const {
239 using android::base::StringAppendF;
240
241 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
242
243 out.append("\n ");
244
245 dumpBase(out);
246}
247
248void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700249 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700250
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700251 if (mDisplayColorProfile) {
252 mDisplayColorProfile->dump(out);
253 } else {
254 out.append(" No display color profile!\n");
255 }
256
Lloyd Pique31cb2942018-10-19 17:23:03 -0700257 if (mRenderSurface) {
258 mRenderSurface->dump(out);
259 } else {
260 out.append(" No render surface!\n");
261 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800262
Lloyd Pique01c77c12019-04-17 12:48:32 -0700263 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
264 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800265 if (!outputLayer) {
266 continue;
267 }
268 outputLayer->dump(out);
269 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700270}
271
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700272compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
273 return mDisplayColorProfile.get();
274}
275
276void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
277 mDisplayColorProfile = std::move(mode);
278}
279
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800280const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
281 return mReleasedLayers;
282}
283
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700284void Output::setDisplayColorProfileForTest(
285 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
286 mDisplayColorProfile = std::move(mode);
287}
288
Lloyd Pique31cb2942018-10-19 17:23:03 -0700289compositionengine::RenderSurface* Output::getRenderSurface() const {
290 return mRenderSurface.get();
291}
292
293void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
294 mRenderSurface = std::move(surface);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200295 editState().framebufferSpace.bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700296 dirtyEntireOutput();
297}
298
Vishnu Nair9b079a22020-01-21 14:36:08 -0800299void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
300 if (cacheSize == 0) {
301 mClientCompositionRequestCache.reset();
302 } else {
303 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
304 }
305};
306
Lloyd Pique31cb2942018-10-19 17:23:03 -0700307void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
308 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700309}
310
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000311Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700312 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200313 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000314 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700315 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700316 }
317 return dirty;
318}
319
Lloyd Piquec6687342019-03-07 21:34:57 -0800320bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800321 // The layerStackId's must match, and also the layer must not be internal
322 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700323 const auto& outputState = getState();
324 return layerStackId && (*layerStackId == outputState.layerStackId) &&
325 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700326}
327
Lloyd Piquede196652020-01-22 17:29:58 -0800328bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
329 const auto* layerFEState = layerFE->getCompositionState();
330 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800331}
332
Lloyd Piquedf336d92019-03-07 21:38:42 -0800333std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800334 const sp<LayerFE>& layerFE) const {
335 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800336}
337
Lloyd Piquede196652020-01-22 17:29:58 -0800338compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
339 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700340 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800341}
342
Lloyd Pique01c77c12019-04-17 12:48:32 -0700343std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800344 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700345 for (size_t i = 0; i < getOutputLayerCount(); i++) {
346 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800347 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700348 return i;
349 }
350 }
351 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800352}
353
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800354void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
355 mReleasedLayers = std::move(layers);
356}
357
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800358void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
359 LayerFESet& geomSnapshots) {
360 ATRACE_CALL();
361 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800362
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800363 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800364}
365
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800366void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800367 ATRACE_CALL();
368 ALOGV(__FUNCTION__);
369
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800370 updateColorProfile(refreshArgs);
371 updateAndWriteCompositionState(refreshArgs);
372 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800373 beginFrame();
374 prepareFrame();
375 devOptRepaintFlash(refreshArgs);
376 finishFrame(refreshArgs);
377 postFramebuffer();
378}
379
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
381 LayerFESet& layerFESet) {
382 ATRACE_CALL();
383 ALOGV(__FUNCTION__);
384
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700385 auto& outputState = editState();
386
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800387 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700388 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800389 return;
390 }
391
392 // Process the layers to determine visibility and coverage
393 compositionengine::Output::CoverageState coverage{layerFESet};
394 collectVisibleLayers(refreshArgs, coverage);
395
396 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700397 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200398 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800399 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
400
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700401 outputState.undefinedRegion = undefinedRegion;
402 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800403}
404
405void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
406 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800407 // Evaluate the layers from front to back to determine what is visible. This
408 // also incrementally calculates the coverage information for each layer as
409 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800410 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700411 // Incrementally process the coverage for each layer
412 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800413
414 // TODO(b/121291683): Stop early if the output is completely covered and
415 // no more layers could even be visible underneath the ones on top.
416 }
417
Lloyd Pique01c77c12019-04-17 12:48:32 -0700418 setReleasedLayers(refreshArgs);
419
420 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800421
422 // Generate a simple Z-order values to each visible output layer
423 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700424 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425 outputLayer->editState().z = zOrder++;
426 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800427}
428
Lloyd Piquede196652020-01-22 17:29:58 -0800429void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700430 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800431 // Ensure we have a snapshot of the basic geometry layer state. Limit the
432 // snapshots to once per frame for each candidate layer, as layers may
433 // appear on multiple outputs.
434 if (!coverage.latchedLayers.count(layerFE)) {
435 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800436 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800437 }
438
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800439 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800440 if (!belongsInOutput(layerFE)) {
441 return;
442 }
443
444 // Obtain a read-only pointer to the front-end layer state
445 const auto* layerFEState = layerFE->getCompositionState();
446 if (CC_UNLIKELY(!layerFEState)) {
447 return;
448 }
449
450 // handle hidden surfaces by setting the visible region to empty
451 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700452 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800453 }
454
455 /*
456 * opaqueRegion: area of a surface that is fully opaque.
457 */
458 Region opaqueRegion;
459
460 /*
461 * visibleRegion: area of a surface that is visible on screen and not fully
462 * transparent. This is essentially the layer's footprint minus the opaque
463 * regions above it. Areas covered by a translucent surface are considered
464 * visible.
465 */
466 Region visibleRegion;
467
468 /*
469 * coveredRegion: area of a surface that is covered by all visible regions
470 * above it (which includes the translucent areas).
471 */
472 Region coveredRegion;
473
474 /*
475 * transparentRegion: area of a surface that is hinted to be completely
476 * transparent. This is only used to tell when the layer has no visible non-
477 * transparent regions and can be removed from the layer list. It does not
478 * affect the visibleRegion of this layer or any layers beneath it. The hint
479 * may not be correct if apps don't respect the SurfaceView restrictions
480 * (which, sadly, some don't).
481 */
482 Region transparentRegion;
483
Vishnu Naira483b4a2019-12-12 15:07:52 -0800484 /*
485 * shadowRegion: Region cast by the layer's shadow.
486 */
487 Region shadowRegion;
488
Lloyd Piquede196652020-01-22 17:29:58 -0800489 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800490
491 // Get the visible region
492 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
493 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800494 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800495 visibleRegion.set(visibleRect);
496
Lloyd Piquede196652020-01-22 17:29:58 -0800497 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800498 // if the layer casts a shadow, offset the layers visible region and
499 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800500 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800501 Rect visibleRectWithShadows(visibleRect);
502 visibleRectWithShadows.inset(inset, inset, inset, inset);
503 visibleRegion.set(visibleRectWithShadows);
504 shadowRegion = visibleRegion.subtract(visibleRect);
505 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800506
507 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700508 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800509 }
510
511 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800512 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800513 if (tr.preserveRects()) {
514 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800515 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800516 } else {
517 // transformation too complex, can't do the
518 // transparent region optimization.
519 transparentRegion.clear();
520 }
521 }
522
523 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800524 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800525 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800526 // If we one of the simple category of transforms (0/90/180/270 rotation
527 // + any flip), then the opaque region is the layer's footprint.
528 // Otherwise we don't try and compute the opaque region since there may
529 // be errors at the edges, and we treat the entire layer as
530 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800531 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800532 }
533
534 // Clip the covered region to the visible region
535 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
536
537 // Update accumAboveCoveredLayers for next (lower) layer
538 coverage.aboveCoveredLayers.orSelf(visibleRegion);
539
540 // subtract the opaque region covered by the layers above us
541 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
542
543 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700544 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800545 }
546
547 // Get coverage information for the layer as previously displayed,
548 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800549 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700550 auto prevOutputLayer =
551 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552
553 // Get coverage information for the layer as previously displayed
554 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
555 const Region kEmptyRegion;
556 const Region& oldVisibleRegion =
557 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
558 const Region& oldCoveredRegion =
559 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
560
561 // compute this layer's dirty region
562 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800563 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800564 // we need to invalidate the whole region
565 dirty = visibleRegion;
566 // as well, as the old visible region
567 dirty.orSelf(oldVisibleRegion);
568 } else {
569 /* compute the exposed region:
570 * the exposed region consists of two components:
571 * 1) what's VISIBLE now and was COVERED before
572 * 2) what's EXPOSED now less what was EXPOSED before
573 *
574 * note that (1) is conservative, we start with the whole visible region
575 * but only keep what used to be covered by something -- which mean it
576 * may have been exposed.
577 *
578 * (2) handles areas that were not covered by anything but got exposed
579 * because of a resize.
580 *
581 */
582 const Region newExposed = visibleRegion - coveredRegion;
583 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
584 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
585 }
586 dirty.subtractSelf(coverage.aboveOpaqueLayers);
587
588 // accumulate to the screen dirty region
589 coverage.dirtyRegion.orSelf(dirty);
590
591 // Update accumAboveOpaqueLayers for next (lower) layer
592 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
593
594 // Compute the visible non-transparent region
595 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
596
Vishnu Naira483b4a2019-12-12 15:07:52 -0800597 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800598 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700599 const auto& outputState = getState();
600 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200601 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800602 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700603 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800604 }
605
Vishnu Naira483b4a2019-12-12 15:07:52 -0800606 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
607
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800608 // The layer is visible. Either reuse the existing outputLayer if we have
609 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800610 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800611
612 // Store the layer coverage information into the layer state as some of it
613 // is useful later.
614 auto& outputLayerState = result->editState();
615 outputLayerState.visibleRegion = visibleRegion;
616 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
617 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200618 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
619 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800620 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800621}
622
623void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
624 // The base class does nothing with this call.
625}
626
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800627void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700628 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800629 layer->getLayerFE().prepareCompositionState(
630 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
631 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800632 }
633}
634
635void Output::updateAndWriteCompositionState(
636 const compositionengine::CompositionRefreshArgs& refreshArgs) {
637 ATRACE_CALL();
638 ALOGV(__FUNCTION__);
639
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800640 if (!getState().isEnabled) {
641 return;
642 }
643
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800644 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
645 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
646
Lloyd Pique01c77c12019-04-17 12:48:32 -0700647 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700648 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800649 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200650 forceClientComposition,
651 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800652
653 if (mLayerRequestingBackgroundBlur == layer) {
654 forceClientComposition = false;
655 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800656
657 // Send the updated state to the HWC, if appropriate.
658 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
659 }
660}
661
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800662compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
663 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
664 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100665 auto* compState = layer->getLayerFE().getCompositionState();
666
667 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
668 // want to force client composition because of the blur.
669 if (compState->sidebandStream != nullptr) {
670 return nullptr;
671 }
672 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800673 layerRequestingBgComposition = layer;
674 }
675 }
676 return layerRequestingBgComposition;
677}
678
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800679void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
680 setColorProfile(pickColorProfile(refreshArgs));
681}
682
683// Returns a data space that fits all visible layers. The returned data space
684// can only be one of
685// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
686// - Dataspace::DISPLAY_P3
687// - Dataspace::DISPLAY_BT2020
688// The returned HDR data space is one of
689// - Dataspace::UNKNOWN
690// - Dataspace::BT2020_HLG
691// - Dataspace::BT2020_PQ
692ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
693 bool* outIsHdrClientComposition) const {
694 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
695 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
696
Lloyd Pique01c77c12019-04-17 12:48:32 -0700697 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800698 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800699 case ui::Dataspace::V0_SCRGB:
700 case ui::Dataspace::V0_SCRGB_LINEAR:
701 case ui::Dataspace::BT2020:
702 case ui::Dataspace::BT2020_ITU:
703 case ui::Dataspace::BT2020_LINEAR:
704 case ui::Dataspace::DISPLAY_BT2020:
705 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
706 break;
707 case ui::Dataspace::DISPLAY_P3:
708 bestDataSpace = ui::Dataspace::DISPLAY_P3;
709 break;
710 case ui::Dataspace::BT2020_PQ:
711 case ui::Dataspace::BT2020_ITU_PQ:
712 bestDataSpace = ui::Dataspace::DISPLAY_P3;
713 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800714 *outIsHdrClientComposition =
715 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800716 break;
717 case ui::Dataspace::BT2020_HLG:
718 case ui::Dataspace::BT2020_ITU_HLG:
719 bestDataSpace = ui::Dataspace::DISPLAY_P3;
720 // When there's mixed PQ content and HLG content, we set the HDR
721 // data space to be BT2020_PQ and convert HLG to PQ.
722 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
723 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
724 }
725 break;
726 default:
727 break;
728 }
729 }
730
731 return bestDataSpace;
732}
733
734compositionengine::Output::ColorProfile Output::pickColorProfile(
735 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
736 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
737 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
738 ui::RenderIntent::COLORIMETRIC,
739 refreshArgs.colorSpaceAgnosticDataspace};
740 }
741
742 ui::Dataspace hdrDataSpace;
743 bool isHdrClientComposition = false;
744 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
745
746 switch (refreshArgs.forceOutputColorMode) {
747 case ui::ColorMode::SRGB:
748 bestDataSpace = ui::Dataspace::V0_SRGB;
749 break;
750 case ui::ColorMode::DISPLAY_P3:
751 bestDataSpace = ui::Dataspace::DISPLAY_P3;
752 break;
753 default:
754 break;
755 }
756
757 // respect hdrDataSpace only when there is no legacy HDR support
758 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
759 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
760 if (isHdr) {
761 bestDataSpace = hdrDataSpace;
762 }
763
764 ui::RenderIntent intent;
765 switch (refreshArgs.outputColorSetting) {
766 case OutputColorSetting::kManaged:
767 case OutputColorSetting::kUnmanaged:
768 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
769 : ui::RenderIntent::COLORIMETRIC;
770 break;
771 case OutputColorSetting::kEnhanced:
772 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
773 break;
774 default: // vendor display color setting
775 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
776 break;
777 }
778
779 ui::ColorMode outMode;
780 ui::Dataspace outDataSpace;
781 ui::RenderIntent outRenderIntent;
782 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
783 &outRenderIntent);
784
785 return ColorProfile{outMode, outDataSpace, outRenderIntent,
786 refreshArgs.colorSpaceAgnosticDataspace};
787}
788
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800789void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700790 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800791 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700792 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700793 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800794
795 // If nothing has changed (!dirty), don't recompose.
796 // If something changed, but we don't currently have any visible layers,
797 // and didn't when we last did a composition, then skip it this time.
798 // The second rule does two things:
799 // - When all layers are removed from a display, we'll emit one black
800 // frame, then nothing more until we get new layers.
801 // - When a display is created with a private layer stack, we won't
802 // emit any black frames until a layer is added to the layer stack.
803 const bool mustRecompose = dirty && !(empty && wasEmpty);
804
805 const char flagPrefix[] = {'-', '+'};
806 static_cast<void>(flagPrefix);
807 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
808 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
809 flagPrefix[empty], flagPrefix[wasEmpty]);
810
811 mRenderSurface->beginFrame(mustRecompose);
812
813 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700814 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800815 }
816}
817
Lloyd Pique66d68602019-02-13 14:23:31 -0800818void Output::prepareFrame() {
819 ATRACE_CALL();
820 ALOGV(__FUNCTION__);
821
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700822 const auto& outputState = getState();
823 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800824 return;
825 }
826
827 chooseCompositionStrategy();
828
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700829 mRenderSurface->prepareFrame(outputState.usesClientComposition,
830 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800831}
832
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800833void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
834 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
835 return;
836 }
837
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700838 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800839 // transform the dirty region into this screen's coordinate space
840 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
841 if (!dirtyRegion.isEmpty()) {
842 base::unique_fd readyFence;
843 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800844 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800845
846 mRenderSurface->queueBuffer(std::move(readyFence));
847 }
848 }
849
850 postFramebuffer();
851
852 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
853
854 prepareFrame();
855}
856
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800857void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800858 ATRACE_CALL();
859 ALOGV(__FUNCTION__);
860
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700861 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800862 return;
863 }
864
865 // Repaint the framebuffer (if needed), getting the optional fence for when
866 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800867 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800868 if (!optReadyFence) {
869 return;
870 }
871
872 // swap buffers (presentation)
873 mRenderSurface->queueBuffer(std::move(*optReadyFence));
874}
875
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800876std::optional<base::unique_fd> Output::composeSurfaces(
877 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800878 ATRACE_CALL();
879 ALOGV(__FUNCTION__);
880
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700881 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800882 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800883 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700884 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700885
886 auto& renderEngine = getCompositionEngine().getRenderEngine();
887 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
888
889 // If we the display is secure, protected content support is enabled, and at
890 // least one layer has protected content, we need to use a secure back
891 // buffer.
892 if (outputState.isSecure && supportsProtectedContent) {
893 auto layers = getOutputLayersOrderedByZ();
894 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
895 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
896 });
897 if (needsProtected != renderEngine.isProtected()) {
898 renderEngine.useProtectedContext(needsProtected);
899 }
900 if (needsProtected != mRenderSurface->isProtected() &&
901 needsProtected == renderEngine.isProtected()) {
902 mRenderSurface->setProtected(needsProtected);
903 }
Peiyong Lin09f910f2020-09-25 10:54:13 -0700904 } else if (!outputState.isSecure && renderEngine.isProtected()) {
905 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -0700906 }
907
908 base::unique_fd fd;
909 sp<GraphicBuffer> buf;
910
911 // If we aren't doing client composition on this output, but do have a
912 // flipClientTarget request for this frame on this output, we still need to
913 // dequeue a buffer.
914 if (hasClientComposition || outputState.flipClientTarget) {
915 buf = mRenderSurface->dequeueBuffer(&fd);
916 if (buf == nullptr) {
917 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
918 "client composition for this frame",
919 mName.c_str());
920 return {};
921 }
922 }
923
Lloyd Piqued3d69882019-02-28 16:03:46 -0800924 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800925 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800926 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800927 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800928 }
929
930 ALOGV("hasClientComposition");
931
Lloyd Pique688abd42019-02-15 15:42:24 -0800932 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200933 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200934 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200935 clientCompositionDisplay.orientation =
936 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700937 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
938 ? outputState.dataspace
939 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800940 clientCompositionDisplay.maxLuminance =
941 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
942
943 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700944 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
945 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800946 }
947
948 // Note: Updated by generateClientCompositionRequests
949 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
950
951 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800952 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800953 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800954 clientCompositionDisplay.clearRegion,
955 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800956 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
957
Vishnu Nair9b079a22020-01-21 14:36:08 -0800958 // Check if the client composition requests were rendered into the provided graphic buffer. If
959 // so, we can reuse the buffer and avoid client composition.
960 if (mClientCompositionRequestCache) {
961 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
962 clientCompositionLayers)) {
963 outputCompositionState.reusedClientComposition = true;
964 setExpensiveRenderingExpected(false);
965 return readyFence;
966 }
967 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
968 clientCompositionLayers);
969 }
970
Lloyd Pique688abd42019-02-15 15:42:24 -0800971 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800972 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
973 // GPU composition can finish in time. We must reset GPU frequency afterwards,
974 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800975 const bool expensiveBlurs =
976 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800977 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800978 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -0800979 if (expensiveRenderingExpected) {
980 setExpensiveRenderingExpected(true);
981 }
982
Vishnu Nair9b079a22020-01-21 14:36:08 -0800983 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
984 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
985 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
986 std::back_inserter(clientCompositionLayerPointers),
987 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
988 return &settings;
989 });
990
Alec Mourie4034bb2019-11-19 12:45:54 -0800991 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800992 status_t status =
Ana Krulecfc874ae2020-02-22 15:39:32 -0800993 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, buf,
994 /*useFramebufferCache=*/true, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800995
996 if (status != NO_ERROR && mClientCompositionRequestCache) {
997 // If rendering was not successful, remove the request from the cache.
998 mClientCompositionRequestCache->remove(buf->getId());
999 }
1000
Alec Mourie4034bb2019-11-19 12:45:54 -08001001 auto& timeStats = getCompositionEngine().getTimeStats();
1002 if (readyFence.get() < 0) {
1003 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1004 } else {
1005 timeStats.recordRenderEngineDuration(renderEngineStart,
1006 std::make_shared<FenceTime>(
1007 new Fence(dup(readyFence.get()))));
1008 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001009
Lloyd Piqued3d69882019-02-28 16:03:46 -08001010 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001011}
1012
Vishnu Nair9b079a22020-01-21 14:36:08 -08001013std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001014 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001015 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001016 ALOGV("Rendering client layers");
1017
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001018 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001019 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001020 bool firstLayer = true;
1021 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001022 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001023
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001024 bool disableBlurs = false;
1025
Lloyd Pique01c77c12019-04-17 12:48:32 -07001026 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001027 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001028 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001029 auto& layerFE = layer->getLayerFE();
1030
Lloyd Piquea2468662019-03-07 21:31:06 -08001031 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001032 ALOGV("Layer: %s", layerFE.getDebugName());
1033 if (clip.isEmpty()) {
1034 ALOGV(" Skipping for empty clip");
1035 firstLayer = false;
1036 continue;
1037 }
1038
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001039 disableBlurs |= layerFEState->sidebandStream != nullptr;
1040
Vishnu Naira483b4a2019-12-12 15:07:52 -08001041 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001042
1043 // We clear the client target for non-client composed layers if
1044 // requested by the HWC. We skip this if the layer is not an opaque
1045 // rectangle, as by definition the layer must blend with whatever is
1046 // underneath. We also skip the first layer as the buffer target is
1047 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001048 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001049 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001050
1051 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1052
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001053 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1054 // composing the non-shadow content and only draw the shadows.
1055 const bool realContentIsVisible = clientComposition &&
1056 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1057
Lloyd Pique688abd42019-02-15 15:42:24 -08001058 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001059 compositionengine::LayerFE::ClientCompositionTargetSettings
1060 targetSettings{.clip = clip,
1061 .needsFiltering =
1062 layer->needsFiltering() || outputState.needsFiltering,
1063 .isSecure = outputState.isSecure,
1064 .supportsProtectedContent = supportsProtectedContent,
1065 .clearRegion = clientComposition ? clearRegion : stubRegion,
1066 .viewport = outputState.layerStackSpace.content,
1067 .dataspace = outputDataspace,
1068 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001069 .clearContent = !clientComposition,
1070 .disableBlurs = disableBlurs};
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001071 std::vector<LayerFE::LayerSettings> results =
1072 layerFE.prepareClientCompositionList(targetSettings);
1073 if (realContentIsVisible && !results.empty()) {
1074 layer->editState().clientCompositionTimestamp = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -08001075 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001076
1077 clientCompositionLayers.insert(clientCompositionLayers.end(),
1078 std::make_move_iterator(results.begin()),
1079 std::make_move_iterator(results.end()));
1080 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001081 }
1082
1083 firstLayer = false;
1084 }
1085
1086 return clientCompositionLayers;
1087}
1088
1089void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001090 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001091 if (flashRegion.isEmpty()) {
1092 return;
1093 }
1094
Vishnu Nair9b079a22020-01-21 14:36:08 -08001095 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001096 layerSettings.source.buffer.buffer = nullptr;
1097 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1098 layerSettings.alpha = half(1.0);
1099
1100 for (const auto& rect : flashRegion) {
1101 layerSettings.geometry.boundaries = rect.toFloatRect();
1102 clientCompositionLayers.push_back(layerSettings);
1103 }
1104}
1105
1106void Output::setExpensiveRenderingExpected(bool) {
1107 // The base class does nothing with this call.
1108}
1109
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001110void Output::postFramebuffer() {
1111 ATRACE_CALL();
1112 ALOGV(__FUNCTION__);
1113
1114 if (!getState().isEnabled) {
1115 return;
1116 }
1117
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001118 auto& outputState = editState();
1119 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001120 mRenderSurface->flip();
1121
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001122 auto frame = presentAndGetFrameFences();
1123
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001124 mRenderSurface->onPresentDisplayCompleted();
1125
Lloyd Pique01c77c12019-04-17 12:48:32 -07001126 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001127 // The layer buffer from the previous frame (if any) is released
1128 // by HWC only when the release fence from this frame (if any) is
1129 // signaled. Always get the release fence from HWC first.
1130 sp<Fence> releaseFence = Fence::NO_FENCE;
1131
1132 if (auto hwcLayer = layer->getHwcLayer()) {
1133 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1134 releaseFence = f->second;
1135 }
1136 }
1137
1138 // If the layer was client composited in the previous frame, we
1139 // need to merge with the previous client target acquire fence.
1140 // Since we do not track that, always merge with the current
1141 // client target acquire fence when it is available, even though
1142 // this is suboptimal.
1143 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001144 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001145 releaseFence =
1146 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1147 }
1148
1149 layer->getLayerFE().onLayerDisplayed(releaseFence);
1150 }
1151
1152 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001153 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001154 // supply them with the present fence.
1155 for (auto& weakLayer : mReleasedLayers) {
1156 if (auto layer = weakLayer.promote(); layer != nullptr) {
1157 layer->onLayerDisplayed(frame.presentFence);
1158 }
1159 }
1160
1161 // Clear out the released layers now that we're done with them.
1162 mReleasedLayers.clear();
1163}
1164
Lloyd Pique32cbe282018-10-19 13:09:22 -07001165void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001166 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001167 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001168}
1169
Lloyd Pique66d68602019-02-13 14:23:31 -08001170void Output::chooseCompositionStrategy() {
1171 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001172 auto& outputState = editState();
1173 outputState.usesClientComposition = true;
1174 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001175 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001176}
1177
Lloyd Pique688abd42019-02-15 15:42:24 -08001178bool Output::getSkipColorTransform() const {
1179 return true;
1180}
1181
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001182compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1183 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001184 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001185 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1186 }
1187 return result;
1188}
1189
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001190} // namespace impl
1191} // namespace android::compositionengine