blob: 55371dfef09851a3252813704da7bf306e48c5bd [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 Pique688abd42019-02-15 15:42:24 -080023#include <compositionengine/Layer.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080024#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070025#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070026#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070027#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070028#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080029#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070030#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080031
32// TODO(b/129481165): remove the #pragma below and fix conversion issues
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wconversion"
35
Lloyd Pique688abd42019-02-15 15:42:24 -080036#include <renderengine/DisplaySettings.h>
37#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080038
39// TODO(b/129481165): remove the #pragma below and fix conversion issues
40#pragma clang diagnostic pop // ignored "-Wconversion"
41
Lloyd Pique32cbe282018-10-19 13:09:22 -070042#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080043#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080044#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070045
Lloyd Pique688abd42019-02-15 15:42:24 -080046#include "TracedOrdinal.h"
47
Lloyd Piquefeb73d72018-12-04 17:23:44 -080048namespace android::compositionengine {
49
50Output::~Output() = default;
51
52namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070053
Lloyd Piquec29e4c62019-03-07 21:48:19 -080054namespace {
55
56template <typename T>
57class Reversed {
58public:
59 explicit Reversed(const T& container) : mContainer(container) {}
60 auto begin() { return mContainer.rbegin(); }
61 auto end() { return mContainer.rend(); }
62
63private:
64 const T& mContainer;
65};
66
67// Helper for enumerating over a container in reverse order
68template <typename T>
69Reversed<T> reversed(const T& c) {
70 return Reversed<T>(c);
71}
72
73} // namespace
74
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070075std::shared_ptr<Output> createOutput(
76 const compositionengine::CompositionEngine& compositionEngine) {
77 return createOutputTemplated<Output>(compositionEngine);
78}
Lloyd Pique32cbe282018-10-19 13:09:22 -070079
80Output::~Output() = default;
81
Lloyd Pique32cbe282018-10-19 13:09:22 -070082bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070083 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
84 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070085}
86
Lloyd Pique6c564cf2019-05-17 17:31:36 -070087std::optional<DisplayId> Output::getDisplayId() const {
88 return {};
89}
90
Lloyd Pique32cbe282018-10-19 13:09:22 -070091const std::string& Output::getName() const {
92 return mName;
93}
94
95void Output::setName(const std::string& name) {
96 mName = name;
97}
98
99void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700100 auto& outputState = editState();
101 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102 return;
103 }
104
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700105 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106 dirtyEntireOutput();
107}
108
Lloyd Pique0a456232020-01-16 17:51:13 -0800109void Output::setProjection(const ui::Transform& transform, uint32_t orientation, const Rect& frame,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700110 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700111 auto& outputState = editState();
112 outputState.transform = transform;
113 outputState.orientation = orientation;
114 outputState.scissor = scissor;
115 outputState.frame = frame;
116 outputState.viewport = viewport;
117 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700118
119 dirtyEntireOutput();
120}
121
Lloyd Pique688abd42019-02-15 15:42:24 -0800122// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700123void Output::setBounds(const ui::Size& size) {
124 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700125 // TODO(b/121291683): Rename outputState.size once more is moved.
126 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700127
128 dirtyEntireOutput();
129}
130
Lloyd Piqueef36b002019-01-23 17:52:04 -0800131void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700132 auto& outputState = editState();
133 outputState.layerStackId = layerStackId;
134 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700135
136 dirtyEntireOutput();
137}
138
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800139void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140 auto& colorTransformMatrix = editState().colorTransformMatrix;
141 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700142 return;
143 }
144
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700145 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800146
147 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700148}
149
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800150void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700151 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800152 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
153 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800154
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700155 auto& outputState = editState();
156 if (outputState.colorMode == colorProfile.mode &&
157 outputState.dataspace == colorProfile.dataspace &&
158 outputState.renderIntent == colorProfile.renderIntent &&
159 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800160 return;
161 }
162
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700163 outputState.colorMode = colorProfile.mode;
164 outputState.dataspace = colorProfile.dataspace;
165 outputState.renderIntent = colorProfile.renderIntent;
166 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700167
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800168 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700169
Lloyd Pique32cbe282018-10-19 13:09:22 -0700170 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800171 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
172 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800173
174 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700175}
176
177void Output::dump(std::string& out) const {
178 using android::base::StringAppendF;
179
180 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
181
182 out.append("\n ");
183
184 dumpBase(out);
185}
186
187void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700188 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700189
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700190 if (mDisplayColorProfile) {
191 mDisplayColorProfile->dump(out);
192 } else {
193 out.append(" No display color profile!\n");
194 }
195
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196 if (mRenderSurface) {
197 mRenderSurface->dump(out);
198 } else {
199 out.append(" No render surface!\n");
200 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800201
Lloyd Pique01c77c12019-04-17 12:48:32 -0700202 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
203 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800204 if (!outputLayer) {
205 continue;
206 }
207 outputLayer->dump(out);
208 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700209}
210
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700211compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
212 return mDisplayColorProfile.get();
213}
214
215void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
216 mDisplayColorProfile = std::move(mode);
217}
218
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800219const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
220 return mReleasedLayers;
221}
222
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700223void Output::setDisplayColorProfileForTest(
224 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
225 mDisplayColorProfile = std::move(mode);
226}
227
Lloyd Pique31cb2942018-10-19 17:23:03 -0700228compositionengine::RenderSurface* Output::getRenderSurface() const {
229 return mRenderSurface.get();
230}
231
232void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
233 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700234 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700235
236 dirtyEntireOutput();
237}
238
Vishnu Nair9b079a22020-01-21 14:36:08 -0800239void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
240 if (cacheSize == 0) {
241 mClientCompositionRequestCache.reset();
242 } else {
243 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
244 }
245};
246
Lloyd Pique31cb2942018-10-19 17:23:03 -0700247void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
248 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249}
250
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000251Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700252 const auto& outputState = getState();
253 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000254 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700255 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700256 }
257 return dirty;
258}
259
Lloyd Piquec6687342019-03-07 21:34:57 -0800260bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800261 // The layerStackId's must match, and also the layer must not be internal
262 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700263 const auto& outputState = getState();
264 return layerStackId && (*layerStackId == outputState.layerStackId) &&
265 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700266}
267
Lloyd Pique66c20c42019-03-07 21:44:02 -0800268bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
269 if (!layer) {
270 return false;
271 }
272
Lloyd Pique9755fb72019-03-26 14:44:40 -0700273 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800274 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
275}
276
Lloyd Piquedf336d92019-03-07 21:38:42 -0800277std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700278 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800279 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800280}
281
Lloyd Pique01c77c12019-04-17 12:48:32 -0700282compositionengine::OutputLayer* Output::getOutputLayerForLayer(
283 compositionengine::Layer* layer) const {
284 auto index = findCurrentOutputLayerForLayer(layer);
285 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800286}
287
Lloyd Pique01c77c12019-04-17 12:48:32 -0700288std::optional<size_t> Output::findCurrentOutputLayerForLayer(
289 compositionengine::Layer* layer) const {
290 for (size_t i = 0; i < getOutputLayerCount(); i++) {
291 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
292 if (outputLayer && &outputLayer->getLayer() == layer) {
293 return i;
294 }
295 }
296 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800297}
298
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800299void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
300 mReleasedLayers = std::move(layers);
301}
302
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800303void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
304 LayerFESet& geomSnapshots) {
305 ATRACE_CALL();
306 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800307
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800308 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800309}
310
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800311void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800312 ATRACE_CALL();
313 ALOGV(__FUNCTION__);
314
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800315 updateColorProfile(refreshArgs);
316 updateAndWriteCompositionState(refreshArgs);
317 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800318 beginFrame();
319 prepareFrame();
320 devOptRepaintFlash(refreshArgs);
321 finishFrame(refreshArgs);
322 postFramebuffer();
323}
324
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800325void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
326 LayerFESet& layerFESet) {
327 ATRACE_CALL();
328 ALOGV(__FUNCTION__);
329
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700330 auto& outputState = editState();
331
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800332 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700333 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800334 return;
335 }
336
337 // Process the layers to determine visibility and coverage
338 compositionengine::Output::CoverageState coverage{layerFESet};
339 collectVisibleLayers(refreshArgs, coverage);
340
341 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700342 const ui::Transform& tr = outputState.transform;
343 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800344 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
345
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700346 outputState.undefinedRegion = undefinedRegion;
347 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800348}
349
350void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
351 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800352 // Evaluate the layers from front to back to determine what is visible. This
353 // also incrementally calculates the coverage information for each layer as
354 // well as the entire output.
355 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700356 // Incrementally process the coverage for each layer
357 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800358
359 // TODO(b/121291683): Stop early if the output is completely covered and
360 // no more layers could even be visible underneath the ones on top.
361 }
362
Lloyd Pique01c77c12019-04-17 12:48:32 -0700363 setReleasedLayers(refreshArgs);
364
365 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800366
367 // Generate a simple Z-order values to each visible output layer
368 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700369 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800370 outputLayer->editState().z = zOrder++;
371 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800372}
373
Lloyd Pique01c77c12019-04-17 12:48:32 -0700374void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
375 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800376 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
377 auto layerFE = layer->getLayerFE();
378 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700379 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380 }
381
382 // Ensure we have a snapshot of the basic geometry layer state. Limit the
383 // snapshots to once per frame for each candidate layer, as layers may
384 // appear on multiple outputs.
385 if (!coverage.latchedLayers.count(layerFE)) {
386 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700387 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800388 compositionengine::LayerFE::StateSubset::BasicGeometry);
389 }
390
391 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700392 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800393
394 // Only consider the layers on the given layer stack
395 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700396 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800397 }
398
399 /*
400 * opaqueRegion: area of a surface that is fully opaque.
401 */
402 Region opaqueRegion;
403
404 /*
405 * visibleRegion: area of a surface that is visible on screen and not fully
406 * transparent. This is essentially the layer's footprint minus the opaque
407 * regions above it. Areas covered by a translucent surface are considered
408 * visible.
409 */
410 Region visibleRegion;
411
412 /*
413 * coveredRegion: area of a surface that is covered by all visible regions
414 * above it (which includes the translucent areas).
415 */
416 Region coveredRegion;
417
418 /*
419 * transparentRegion: area of a surface that is hinted to be completely
420 * transparent. This is only used to tell when the layer has no visible non-
421 * transparent regions and can be removed from the layer list. It does not
422 * affect the visibleRegion of this layer or any layers beneath it. The hint
423 * may not be correct if apps don't respect the SurfaceView restrictions
424 * (which, sadly, some don't).
425 */
426 Region transparentRegion;
427
Vishnu Naira483b4a2019-12-12 15:07:52 -0800428 /*
429 * shadowRegion: Region cast by the layer's shadow.
430 */
431 Region shadowRegion;
432
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800433 // handle hidden surfaces by setting the visible region to empty
434 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700435 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800436 }
437
438 const ui::Transform& tr = layerFEState.geomLayerTransform;
439
440 // Get the visible region
441 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
442 // for computations like this?
Vishnu Naira483b4a2019-12-12 15:07:52 -0800443 const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
444 visibleRegion.set(visibleRect);
445
446 if (layerFEState.shadowRadius > 0.0f) {
447 // if the layer casts a shadow, offset the layers visible region and
448 // calculate the shadow region.
Lloyd Pique0a456232020-01-16 17:51:13 -0800449 const auto inset = static_cast<int32_t>(ceilf(layerFEState.shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800450 Rect visibleRectWithShadows(visibleRect);
451 visibleRectWithShadows.inset(inset, inset, inset, inset);
452 visibleRegion.set(visibleRectWithShadows);
453 shadowRegion = visibleRegion.subtract(visibleRect);
454 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800455
456 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700457 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800458 }
459
460 // Remove the transparent area from the visible region
461 if (!layerFEState.isOpaque) {
462 if (tr.preserveRects()) {
463 // transform the transparent region
464 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
465 } else {
466 // transformation too complex, can't do the
467 // transparent region optimization.
468 transparentRegion.clear();
469 }
470 }
471
472 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800473 const auto layerOrientation = tr.getOrientation();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800474 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
475 // If we one of the simple category of transforms (0/90/180/270 rotation
476 // + any flip), then the opaque region is the layer's footprint.
477 // Otherwise we don't try and compute the opaque region since there may
478 // be errors at the edges, and we treat the entire layer as
479 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800480 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800481 }
482
483 // Clip the covered region to the visible region
484 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
485
486 // Update accumAboveCoveredLayers for next (lower) layer
487 coverage.aboveCoveredLayers.orSelf(visibleRegion);
488
489 // subtract the opaque region covered by the layers above us
490 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
491
492 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700493 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800494 }
495
496 // Get coverage information for the layer as previously displayed,
497 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700498 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
499 auto prevOutputLayer =
500 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800501
502 // Get coverage information for the layer as previously displayed
503 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
504 const Region kEmptyRegion;
505 const Region& oldVisibleRegion =
506 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
507 const Region& oldCoveredRegion =
508 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
509
510 // compute this layer's dirty region
511 Region dirty;
512 if (layerFEState.contentDirty) {
513 // we need to invalidate the whole region
514 dirty = visibleRegion;
515 // as well, as the old visible region
516 dirty.orSelf(oldVisibleRegion);
517 } else {
518 /* compute the exposed region:
519 * the exposed region consists of two components:
520 * 1) what's VISIBLE now and was COVERED before
521 * 2) what's EXPOSED now less what was EXPOSED before
522 *
523 * note that (1) is conservative, we start with the whole visible region
524 * but only keep what used to be covered by something -- which mean it
525 * may have been exposed.
526 *
527 * (2) handles areas that were not covered by anything but got exposed
528 * because of a resize.
529 *
530 */
531 const Region newExposed = visibleRegion - coveredRegion;
532 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
533 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
534 }
535 dirty.subtractSelf(coverage.aboveOpaqueLayers);
536
537 // accumulate to the screen dirty region
538 coverage.dirtyRegion.orSelf(dirty);
539
540 // Update accumAboveOpaqueLayers for next (lower) layer
541 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
542
543 // Compute the visible non-transparent region
544 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
545
Vishnu Naira483b4a2019-12-12 15:07:52 -0800546 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800547 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700548 const auto& outputState = getState();
549 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
550 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800551 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700552 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800553 }
554
Vishnu Naira483b4a2019-12-12 15:07:52 -0800555 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
556
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800557 // The layer is visible. Either reuse the existing outputLayer if we have
558 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700559 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560
561 // Store the layer coverage information into the layer state as some of it
562 // is useful later.
563 auto& outputLayerState = result->editState();
564 outputLayerState.visibleRegion = visibleRegion;
565 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
566 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800567 outputLayerState.outputSpaceVisibleRegion =
568 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
569 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800570}
571
572void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
573 // The base class does nothing with this call.
574}
575
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800576void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700577 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700578 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800579 args.updatingGeometryThisFrame
580 ? LayerFE::StateSubset::GeometryAndContent
581 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800582 }
583}
584
585void Output::updateAndWriteCompositionState(
586 const compositionengine::CompositionRefreshArgs& refreshArgs) {
587 ATRACE_CALL();
588 ALOGV(__FUNCTION__);
589
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800590 if (!getState().isEnabled) {
591 return;
592 }
593
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800594 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
595 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
596
Lloyd Pique01c77c12019-04-17 12:48:32 -0700597 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700598 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800599 refreshArgs.devOptForceClientComposition ||
600 forceClientComposition);
601
602 if (mLayerRequestingBackgroundBlur == layer) {
603 forceClientComposition = false;
604 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800605
606 // Send the updated state to the HWC, if appropriate.
607 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
608 }
609}
610
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800611compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
612 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
613 for (auto* layer : getOutputLayersOrderedByZ()) {
614 if (layer->getLayer().getFEState().backgroundBlurRadius > 0) {
615 layerRequestingBgComposition = layer;
616 }
617 }
618 return layerRequestingBgComposition;
619}
620
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800621void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
622 setColorProfile(pickColorProfile(refreshArgs));
623}
624
625// Returns a data space that fits all visible layers. The returned data space
626// can only be one of
627// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
628// - Dataspace::DISPLAY_P3
629// - Dataspace::DISPLAY_BT2020
630// The returned HDR data space is one of
631// - Dataspace::UNKNOWN
632// - Dataspace::BT2020_HLG
633// - Dataspace::BT2020_PQ
634ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
635 bool* outIsHdrClientComposition) const {
636 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
637 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
638
Lloyd Pique01c77c12019-04-17 12:48:32 -0700639 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700640 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800641 case ui::Dataspace::V0_SCRGB:
642 case ui::Dataspace::V0_SCRGB_LINEAR:
643 case ui::Dataspace::BT2020:
644 case ui::Dataspace::BT2020_ITU:
645 case ui::Dataspace::BT2020_LINEAR:
646 case ui::Dataspace::DISPLAY_BT2020:
647 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
648 break;
649 case ui::Dataspace::DISPLAY_P3:
650 bestDataSpace = ui::Dataspace::DISPLAY_P3;
651 break;
652 case ui::Dataspace::BT2020_PQ:
653 case ui::Dataspace::BT2020_ITU_PQ:
654 bestDataSpace = ui::Dataspace::DISPLAY_P3;
655 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700656 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800657 break;
658 case ui::Dataspace::BT2020_HLG:
659 case ui::Dataspace::BT2020_ITU_HLG:
660 bestDataSpace = ui::Dataspace::DISPLAY_P3;
661 // When there's mixed PQ content and HLG content, we set the HDR
662 // data space to be BT2020_PQ and convert HLG to PQ.
663 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
664 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
665 }
666 break;
667 default:
668 break;
669 }
670 }
671
672 return bestDataSpace;
673}
674
675compositionengine::Output::ColorProfile Output::pickColorProfile(
676 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
677 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
678 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
679 ui::RenderIntent::COLORIMETRIC,
680 refreshArgs.colorSpaceAgnosticDataspace};
681 }
682
683 ui::Dataspace hdrDataSpace;
684 bool isHdrClientComposition = false;
685 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
686
687 switch (refreshArgs.forceOutputColorMode) {
688 case ui::ColorMode::SRGB:
689 bestDataSpace = ui::Dataspace::V0_SRGB;
690 break;
691 case ui::ColorMode::DISPLAY_P3:
692 bestDataSpace = ui::Dataspace::DISPLAY_P3;
693 break;
694 default:
695 break;
696 }
697
698 // respect hdrDataSpace only when there is no legacy HDR support
699 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
700 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
701 if (isHdr) {
702 bestDataSpace = hdrDataSpace;
703 }
704
705 ui::RenderIntent intent;
706 switch (refreshArgs.outputColorSetting) {
707 case OutputColorSetting::kManaged:
708 case OutputColorSetting::kUnmanaged:
709 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
710 : ui::RenderIntent::COLORIMETRIC;
711 break;
712 case OutputColorSetting::kEnhanced:
713 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
714 break;
715 default: // vendor display color setting
716 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
717 break;
718 }
719
720 ui::ColorMode outMode;
721 ui::Dataspace outDataSpace;
722 ui::RenderIntent outRenderIntent;
723 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
724 &outRenderIntent);
725
726 return ColorProfile{outMode, outDataSpace, outRenderIntent,
727 refreshArgs.colorSpaceAgnosticDataspace};
728}
729
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800730void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700731 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800732 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700733 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700734 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800735
736 // If nothing has changed (!dirty), don't recompose.
737 // If something changed, but we don't currently have any visible layers,
738 // and didn't when we last did a composition, then skip it this time.
739 // The second rule does two things:
740 // - When all layers are removed from a display, we'll emit one black
741 // frame, then nothing more until we get new layers.
742 // - When a display is created with a private layer stack, we won't
743 // emit any black frames until a layer is added to the layer stack.
744 const bool mustRecompose = dirty && !(empty && wasEmpty);
745
746 const char flagPrefix[] = {'-', '+'};
747 static_cast<void>(flagPrefix);
748 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
749 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
750 flagPrefix[empty], flagPrefix[wasEmpty]);
751
752 mRenderSurface->beginFrame(mustRecompose);
753
754 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700755 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800756 }
757}
758
Lloyd Pique66d68602019-02-13 14:23:31 -0800759void Output::prepareFrame() {
760 ATRACE_CALL();
761 ALOGV(__FUNCTION__);
762
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700763 const auto& outputState = getState();
764 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800765 return;
766 }
767
768 chooseCompositionStrategy();
769
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700770 mRenderSurface->prepareFrame(outputState.usesClientComposition,
771 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800772}
773
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800774void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
775 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
776 return;
777 }
778
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700779 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800780 // transform the dirty region into this screen's coordinate space
781 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
782 if (!dirtyRegion.isEmpty()) {
783 base::unique_fd readyFence;
784 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800785 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800786
787 mRenderSurface->queueBuffer(std::move(readyFence));
788 }
789 }
790
791 postFramebuffer();
792
793 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
794
795 prepareFrame();
796}
797
Lloyd Piqued3d69882019-02-28 16:03:46 -0800798void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
799 ATRACE_CALL();
800 ALOGV(__FUNCTION__);
801
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700802 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800803 return;
804 }
805
806 // Repaint the framebuffer (if needed), getting the optional fence for when
807 // the composition completes.
808 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
809 if (!optReadyFence) {
810 return;
811 }
812
813 // swap buffers (presentation)
814 mRenderSurface->queueBuffer(std::move(*optReadyFence));
815}
816
817std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800818 ATRACE_CALL();
819 ALOGV(__FUNCTION__);
820
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700821 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800822 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800823 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700824 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800825 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800826 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800827 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800828 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800829 }
830
831 ALOGV("hasClientComposition");
832
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700833 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800834 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
835
836 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700837 clientCompositionDisplay.physicalDisplay = outputState.scissor;
838 clientCompositionDisplay.clip = outputState.scissor;
839 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
840 clientCompositionDisplay.orientation = outputState.orientation;
841 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
842 ? outputState.dataspace
843 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800844 clientCompositionDisplay.maxLuminance =
845 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
846
847 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700848 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
849 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800850 }
851
852 // Note: Updated by generateClientCompositionRequests
853 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
854
855 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800856 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800857 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800858 clientCompositionDisplay.clearRegion,
859 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800860 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
861
862 // If we the display is secure, protected content support is enabled, and at
863 // least one layer has protected content, we need to use a secure back
864 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700865 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700866 auto layers = getOutputLayersOrderedByZ();
867 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
868 return layer->getLayer().getFEState().hasProtectedContent;
869 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800870 if (needsProtected != renderEngine.isProtected()) {
871 renderEngine.useProtectedContext(needsProtected);
872 }
873 if (needsProtected != mRenderSurface->isProtected() &&
874 needsProtected == renderEngine.isProtected()) {
875 mRenderSurface->setProtected(needsProtected);
876 }
877 }
878
879 base::unique_fd fd;
880 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
881 if (buf == nullptr) {
882 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
883 "client composition for this frame",
884 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800885 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800886 }
887
Vishnu Nair9b079a22020-01-21 14:36:08 -0800888 // Check if the client composition requests were rendered into the provided graphic buffer. If
889 // so, we can reuse the buffer and avoid client composition.
890 if (mClientCompositionRequestCache) {
891 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
892 clientCompositionLayers)) {
893 outputCompositionState.reusedClientComposition = true;
894 setExpensiveRenderingExpected(false);
895 return readyFence;
896 }
897 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
898 clientCompositionLayers);
899 }
900
Lloyd Pique688abd42019-02-15 15:42:24 -0800901 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800902 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
903 // GPU composition can finish in time. We must reset GPU frequency afterwards,
904 // because high frequency consumes extra battery.
Lloyd Pique688abd42019-02-15 15:42:24 -0800905 const bool expensiveRenderingExpected =
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800906 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 ||
907 mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800908 if (expensiveRenderingExpected) {
909 setExpensiveRenderingExpected(true);
910 }
911
Vishnu Nair9b079a22020-01-21 14:36:08 -0800912 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
913 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
914 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
915 std::back_inserter(clientCompositionLayerPointers),
916 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
917 return &settings;
918 });
919
Alec Mourie4034bb2019-11-19 12:45:54 -0800920 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800921 status_t status =
922 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers,
923 buf->getNativeBuffer(), /*useFramebufferCache=*/true,
924 std::move(fd), &readyFence);
925
926 if (status != NO_ERROR && mClientCompositionRequestCache) {
927 // If rendering was not successful, remove the request from the cache.
928 mClientCompositionRequestCache->remove(buf->getId());
929 }
930
Alec Mourie4034bb2019-11-19 12:45:54 -0800931 auto& timeStats = getCompositionEngine().getTimeStats();
932 if (readyFence.get() < 0) {
933 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
934 } else {
935 timeStats.recordRenderEngineDuration(renderEngineStart,
936 std::make_shared<FenceTime>(
937 new Fence(dup(readyFence.get()))));
938 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800939
Lloyd Piqued3d69882019-02-28 16:03:46 -0800940 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800941}
942
Vishnu Nair9b079a22020-01-21 14:36:08 -0800943std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800944 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800945 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -0800946 ALOGV("Rendering client layers");
947
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700948 const auto& outputState = getState();
949 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800950 const bool useIdentityTransform = false;
951 bool firstLayer = true;
952 // Used when a layer clears part of the buffer.
953 Region dummyRegion;
954
Lloyd Pique01c77c12019-04-17 12:48:32 -0700955 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800956 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700957 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800958 auto& layerFE = layer->getLayerFE();
959
Lloyd Piquea2468662019-03-07 21:31:06 -0800960 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800961 ALOGV("Layer: %s", layerFE.getDebugName());
962 if (clip.isEmpty()) {
963 ALOGV(" Skipping for empty clip");
964 firstLayer = false;
965 continue;
966 }
967
Vishnu Naira483b4a2019-12-12 15:07:52 -0800968 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800969
970 // We clear the client target for non-client composed layers if
971 // requested by the HWC. We skip this if the layer is not an opaque
972 // rectangle, as by definition the layer must blend with whatever is
973 // underneath. We also skip the first layer as the buffer target is
974 // guaranteed to start out cleared.
975 bool clearClientComposition =
976 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
977
978 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
979
980 if (clientComposition || clearClientComposition) {
981 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
982 clip,
983 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700984 layer->needsFiltering() || outputState.needsFiltering,
985 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800986 supportsProtectedContent,
987 clientComposition ? clearRegion : dummyRegion,
988 };
Vishnu Nair9b079a22020-01-21 14:36:08 -0800989 if (std::optional<LayerFE::LayerSettings> result =
990 layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700991 if (!clientComposition) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800992 LayerFE::LayerSettings& layerSettings = *result;
Lloyd Pique688abd42019-02-15 15:42:24 -0800993 layerSettings.source.buffer.buffer = nullptr;
994 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
995 layerSettings.alpha = half(0.0);
996 layerSettings.disableBlending = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800997 layerSettings.frameNumber = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800998 } else {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800999 std::optional<LayerFE::LayerSettings> shadowLayer =
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001000 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
1001 outputDataspace);
1002 if (shadowLayer) {
1003 clientCompositionLayers.push_back(*shadowLayer);
1004 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001005 }
1006
Vishnu Naira483b4a2019-12-12 15:07:52 -08001007 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1008 // composing the non-shadow content and only draw the shadows.
1009 const bool skipNonShadowContentComposition = clientComposition &&
1010 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1011
1012 if (!skipNonShadowContentComposition) {
1013 layer->editState().clientCompositionTimestamp = systemTime();
1014 clientCompositionLayers.push_back(*result);
1015 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001016 }
1017 }
1018
1019 firstLayer = false;
1020 }
1021
1022 return clientCompositionLayers;
1023}
1024
1025void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001026 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001027 if (flashRegion.isEmpty()) {
1028 return;
1029 }
1030
Vishnu Nair9b079a22020-01-21 14:36:08 -08001031 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001032 layerSettings.source.buffer.buffer = nullptr;
1033 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1034 layerSettings.alpha = half(1.0);
1035
1036 for (const auto& rect : flashRegion) {
1037 layerSettings.geometry.boundaries = rect.toFloatRect();
1038 clientCompositionLayers.push_back(layerSettings);
1039 }
1040}
1041
1042void Output::setExpensiveRenderingExpected(bool) {
1043 // The base class does nothing with this call.
1044}
1045
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001046void Output::postFramebuffer() {
1047 ATRACE_CALL();
1048 ALOGV(__FUNCTION__);
1049
1050 if (!getState().isEnabled) {
1051 return;
1052 }
1053
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001054 auto& outputState = editState();
1055 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001056 mRenderSurface->flip();
1057
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001058 auto frame = presentAndGetFrameFences();
1059
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001060 mRenderSurface->onPresentDisplayCompleted();
1061
Lloyd Pique01c77c12019-04-17 12:48:32 -07001062 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001063 // The layer buffer from the previous frame (if any) is released
1064 // by HWC only when the release fence from this frame (if any) is
1065 // signaled. Always get the release fence from HWC first.
1066 sp<Fence> releaseFence = Fence::NO_FENCE;
1067
1068 if (auto hwcLayer = layer->getHwcLayer()) {
1069 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1070 releaseFence = f->second;
1071 }
1072 }
1073
1074 // If the layer was client composited in the previous frame, we
1075 // need to merge with the previous client target acquire fence.
1076 // Since we do not track that, always merge with the current
1077 // client target acquire fence when it is available, even though
1078 // this is suboptimal.
1079 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001080 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001081 releaseFence =
1082 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1083 }
1084
1085 layer->getLayerFE().onLayerDisplayed(releaseFence);
1086 }
1087
1088 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001089 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001090 // supply them with the present fence.
1091 for (auto& weakLayer : mReleasedLayers) {
1092 if (auto layer = weakLayer.promote(); layer != nullptr) {
1093 layer->onLayerDisplayed(frame.presentFence);
1094 }
1095 }
1096
1097 // Clear out the released layers now that we're done with them.
1098 mReleasedLayers.clear();
1099}
1100
Lloyd Pique32cbe282018-10-19 13:09:22 -07001101void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001102 auto& outputState = editState();
1103 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001104}
1105
Lloyd Pique66d68602019-02-13 14:23:31 -08001106void Output::chooseCompositionStrategy() {
1107 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001108 auto& outputState = editState();
1109 outputState.usesClientComposition = true;
1110 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001111 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001112}
1113
Lloyd Pique688abd42019-02-15 15:42:24 -08001114bool Output::getSkipColorTransform() const {
1115 return true;
1116}
1117
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001118compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1119 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001120 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001121 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1122 }
1123 return result;
1124}
1125
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001126} // namespace impl
1127} // namespace android::compositionengine