blob: 8dce47bc0056ebb3b548b129ac29dbb273c165e6 [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
239void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
240 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700241}
242
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000243Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700244 const auto& outputState = getState();
245 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000246 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700247 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700248 }
249 return dirty;
250}
251
Lloyd Piquec6687342019-03-07 21:34:57 -0800252bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800253 // The layerStackId's must match, and also the layer must not be internal
254 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700255 const auto& outputState = getState();
256 return layerStackId && (*layerStackId == outputState.layerStackId) &&
257 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700258}
259
Lloyd Pique66c20c42019-03-07 21:44:02 -0800260bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
261 if (!layer) {
262 return false;
263 }
264
Lloyd Pique9755fb72019-03-26 14:44:40 -0700265 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800266 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
267}
268
Lloyd Piquedf336d92019-03-07 21:38:42 -0800269std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700270 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800271 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800272}
273
Lloyd Pique01c77c12019-04-17 12:48:32 -0700274compositionengine::OutputLayer* Output::getOutputLayerForLayer(
275 compositionengine::Layer* layer) const {
276 auto index = findCurrentOutputLayerForLayer(layer);
277 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800278}
279
Lloyd Pique01c77c12019-04-17 12:48:32 -0700280std::optional<size_t> Output::findCurrentOutputLayerForLayer(
281 compositionengine::Layer* layer) const {
282 for (size_t i = 0; i < getOutputLayerCount(); i++) {
283 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
284 if (outputLayer && &outputLayer->getLayer() == layer) {
285 return i;
286 }
287 }
288 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800289}
290
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800291void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
292 mReleasedLayers = std::move(layers);
293}
294
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800295void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
296 LayerFESet& geomSnapshots) {
297 ATRACE_CALL();
298 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800299
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800300 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800301}
302
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800303void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800304 ATRACE_CALL();
305 ALOGV(__FUNCTION__);
306
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800307 updateColorProfile(refreshArgs);
308 updateAndWriteCompositionState(refreshArgs);
309 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800310 beginFrame();
311 prepareFrame();
312 devOptRepaintFlash(refreshArgs);
313 finishFrame(refreshArgs);
314 postFramebuffer();
315}
316
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800317void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
318 LayerFESet& layerFESet) {
319 ATRACE_CALL();
320 ALOGV(__FUNCTION__);
321
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700322 auto& outputState = editState();
323
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800324 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700325 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800326 return;
327 }
328
329 // Process the layers to determine visibility and coverage
330 compositionengine::Output::CoverageState coverage{layerFESet};
331 collectVisibleLayers(refreshArgs, coverage);
332
333 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700334 const ui::Transform& tr = outputState.transform;
335 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800336 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
337
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700338 outputState.undefinedRegion = undefinedRegion;
339 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800340}
341
342void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
343 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800344 // Evaluate the layers from front to back to determine what is visible. This
345 // also incrementally calculates the coverage information for each layer as
346 // well as the entire output.
347 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700348 // Incrementally process the coverage for each layer
349 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800350
351 // TODO(b/121291683): Stop early if the output is completely covered and
352 // no more layers could even be visible underneath the ones on top.
353 }
354
Lloyd Pique01c77c12019-04-17 12:48:32 -0700355 setReleasedLayers(refreshArgs);
356
357 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800358
359 // Generate a simple Z-order values to each visible output layer
360 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700361 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800362 outputLayer->editState().z = zOrder++;
363 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800364}
365
Lloyd Pique01c77c12019-04-17 12:48:32 -0700366void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
367 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800368 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
369 auto layerFE = layer->getLayerFE();
370 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700371 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800372 }
373
374 // Ensure we have a snapshot of the basic geometry layer state. Limit the
375 // snapshots to once per frame for each candidate layer, as layers may
376 // appear on multiple outputs.
377 if (!coverage.latchedLayers.count(layerFE)) {
378 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700379 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380 compositionengine::LayerFE::StateSubset::BasicGeometry);
381 }
382
383 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700384 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800385
386 // Only consider the layers on the given layer stack
387 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700388 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800389 }
390
391 /*
392 * opaqueRegion: area of a surface that is fully opaque.
393 */
394 Region opaqueRegion;
395
396 /*
397 * visibleRegion: area of a surface that is visible on screen and not fully
398 * transparent. This is essentially the layer's footprint minus the opaque
399 * regions above it. Areas covered by a translucent surface are considered
400 * visible.
401 */
402 Region visibleRegion;
403
404 /*
405 * coveredRegion: area of a surface that is covered by all visible regions
406 * above it (which includes the translucent areas).
407 */
408 Region coveredRegion;
409
410 /*
411 * transparentRegion: area of a surface that is hinted to be completely
412 * transparent. This is only used to tell when the layer has no visible non-
413 * transparent regions and can be removed from the layer list. It does not
414 * affect the visibleRegion of this layer or any layers beneath it. The hint
415 * may not be correct if apps don't respect the SurfaceView restrictions
416 * (which, sadly, some don't).
417 */
418 Region transparentRegion;
419
Vishnu Naira483b4a2019-12-12 15:07:52 -0800420 /*
421 * shadowRegion: Region cast by the layer's shadow.
422 */
423 Region shadowRegion;
424
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425 // handle hidden surfaces by setting the visible region to empty
426 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700427 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800428 }
429
430 const ui::Transform& tr = layerFEState.geomLayerTransform;
431
432 // Get the visible region
433 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
434 // for computations like this?
Vishnu Naira483b4a2019-12-12 15:07:52 -0800435 const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
436 visibleRegion.set(visibleRect);
437
438 if (layerFEState.shadowRadius > 0.0f) {
439 // if the layer casts a shadow, offset the layers visible region and
440 // calculate the shadow region.
Lloyd Pique0a456232020-01-16 17:51:13 -0800441 const auto inset = static_cast<int32_t>(ceilf(layerFEState.shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800442 Rect visibleRectWithShadows(visibleRect);
443 visibleRectWithShadows.inset(inset, inset, inset, inset);
444 visibleRegion.set(visibleRectWithShadows);
445 shadowRegion = visibleRegion.subtract(visibleRect);
446 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800447
448 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700449 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800450 }
451
452 // Remove the transparent area from the visible region
453 if (!layerFEState.isOpaque) {
454 if (tr.preserveRects()) {
455 // transform the transparent region
456 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
457 } else {
458 // transformation too complex, can't do the
459 // transparent region optimization.
460 transparentRegion.clear();
461 }
462 }
463
464 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800465 const auto layerOrientation = tr.getOrientation();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800466 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
467 // If we one of the simple category of transforms (0/90/180/270 rotation
468 // + any flip), then the opaque region is the layer's footprint.
469 // Otherwise we don't try and compute the opaque region since there may
470 // be errors at the edges, and we treat the entire layer as
471 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800472 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 }
474
475 // Clip the covered region to the visible region
476 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
477
478 // Update accumAboveCoveredLayers for next (lower) layer
479 coverage.aboveCoveredLayers.orSelf(visibleRegion);
480
481 // subtract the opaque region covered by the layers above us
482 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
483
484 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700485 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800486 }
487
488 // Get coverage information for the layer as previously displayed,
489 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700490 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
491 auto prevOutputLayer =
492 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800493
494 // Get coverage information for the layer as previously displayed
495 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
496 const Region kEmptyRegion;
497 const Region& oldVisibleRegion =
498 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
499 const Region& oldCoveredRegion =
500 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
501
502 // compute this layer's dirty region
503 Region dirty;
504 if (layerFEState.contentDirty) {
505 // we need to invalidate the whole region
506 dirty = visibleRegion;
507 // as well, as the old visible region
508 dirty.orSelf(oldVisibleRegion);
509 } else {
510 /* compute the exposed region:
511 * the exposed region consists of two components:
512 * 1) what's VISIBLE now and was COVERED before
513 * 2) what's EXPOSED now less what was EXPOSED before
514 *
515 * note that (1) is conservative, we start with the whole visible region
516 * but only keep what used to be covered by something -- which mean it
517 * may have been exposed.
518 *
519 * (2) handles areas that were not covered by anything but got exposed
520 * because of a resize.
521 *
522 */
523 const Region newExposed = visibleRegion - coveredRegion;
524 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
525 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
526 }
527 dirty.subtractSelf(coverage.aboveOpaqueLayers);
528
529 // accumulate to the screen dirty region
530 coverage.dirtyRegion.orSelf(dirty);
531
532 // Update accumAboveOpaqueLayers for next (lower) layer
533 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
534
535 // Compute the visible non-transparent region
536 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
537
Vishnu Naira483b4a2019-12-12 15:07:52 -0800538 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800539 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700540 const auto& outputState = getState();
541 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
542 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700544 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800545 }
546
Vishnu Naira483b4a2019-12-12 15:07:52 -0800547 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
548
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549 // The layer is visible. Either reuse the existing outputLayer if we have
550 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700551 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552
553 // Store the layer coverage information into the layer state as some of it
554 // is useful later.
555 auto& outputLayerState = result->editState();
556 outputLayerState.visibleRegion = visibleRegion;
557 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
558 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800559 outputLayerState.outputSpaceVisibleRegion =
560 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
561 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562}
563
564void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
565 // The base class does nothing with this call.
566}
567
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800568void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700569 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700570 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800571 args.updatingGeometryThisFrame
572 ? LayerFE::StateSubset::GeometryAndContent
573 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800574 }
575}
576
577void Output::updateAndWriteCompositionState(
578 const compositionengine::CompositionRefreshArgs& refreshArgs) {
579 ATRACE_CALL();
580 ALOGV(__FUNCTION__);
581
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800582 if (!getState().isEnabled) {
583 return;
584 }
585
Lloyd Pique01c77c12019-04-17 12:48:32 -0700586 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700587 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
588 refreshArgs.devOptForceClientComposition);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800589
590 // Send the updated state to the HWC, if appropriate.
591 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
592 }
593}
594
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800595void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
596 setColorProfile(pickColorProfile(refreshArgs));
597}
598
599// Returns a data space that fits all visible layers. The returned data space
600// can only be one of
601// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
602// - Dataspace::DISPLAY_P3
603// - Dataspace::DISPLAY_BT2020
604// The returned HDR data space is one of
605// - Dataspace::UNKNOWN
606// - Dataspace::BT2020_HLG
607// - Dataspace::BT2020_PQ
608ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
609 bool* outIsHdrClientComposition) const {
610 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
611 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
612
Lloyd Pique01c77c12019-04-17 12:48:32 -0700613 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700614 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800615 case ui::Dataspace::V0_SCRGB:
616 case ui::Dataspace::V0_SCRGB_LINEAR:
617 case ui::Dataspace::BT2020:
618 case ui::Dataspace::BT2020_ITU:
619 case ui::Dataspace::BT2020_LINEAR:
620 case ui::Dataspace::DISPLAY_BT2020:
621 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
622 break;
623 case ui::Dataspace::DISPLAY_P3:
624 bestDataSpace = ui::Dataspace::DISPLAY_P3;
625 break;
626 case ui::Dataspace::BT2020_PQ:
627 case ui::Dataspace::BT2020_ITU_PQ:
628 bestDataSpace = ui::Dataspace::DISPLAY_P3;
629 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700630 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800631 break;
632 case ui::Dataspace::BT2020_HLG:
633 case ui::Dataspace::BT2020_ITU_HLG:
634 bestDataSpace = ui::Dataspace::DISPLAY_P3;
635 // When there's mixed PQ content and HLG content, we set the HDR
636 // data space to be BT2020_PQ and convert HLG to PQ.
637 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
638 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
639 }
640 break;
641 default:
642 break;
643 }
644 }
645
646 return bestDataSpace;
647}
648
649compositionengine::Output::ColorProfile Output::pickColorProfile(
650 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
651 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
652 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
653 ui::RenderIntent::COLORIMETRIC,
654 refreshArgs.colorSpaceAgnosticDataspace};
655 }
656
657 ui::Dataspace hdrDataSpace;
658 bool isHdrClientComposition = false;
659 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
660
661 switch (refreshArgs.forceOutputColorMode) {
662 case ui::ColorMode::SRGB:
663 bestDataSpace = ui::Dataspace::V0_SRGB;
664 break;
665 case ui::ColorMode::DISPLAY_P3:
666 bestDataSpace = ui::Dataspace::DISPLAY_P3;
667 break;
668 default:
669 break;
670 }
671
672 // respect hdrDataSpace only when there is no legacy HDR support
673 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
674 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
675 if (isHdr) {
676 bestDataSpace = hdrDataSpace;
677 }
678
679 ui::RenderIntent intent;
680 switch (refreshArgs.outputColorSetting) {
681 case OutputColorSetting::kManaged:
682 case OutputColorSetting::kUnmanaged:
683 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
684 : ui::RenderIntent::COLORIMETRIC;
685 break;
686 case OutputColorSetting::kEnhanced:
687 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
688 break;
689 default: // vendor display color setting
690 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
691 break;
692 }
693
694 ui::ColorMode outMode;
695 ui::Dataspace outDataSpace;
696 ui::RenderIntent outRenderIntent;
697 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
698 &outRenderIntent);
699
700 return ColorProfile{outMode, outDataSpace, outRenderIntent,
701 refreshArgs.colorSpaceAgnosticDataspace};
702}
703
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800704void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700705 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800706 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700707 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700708 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800709
710 // If nothing has changed (!dirty), don't recompose.
711 // If something changed, but we don't currently have any visible layers,
712 // and didn't when we last did a composition, then skip it this time.
713 // The second rule does two things:
714 // - When all layers are removed from a display, we'll emit one black
715 // frame, then nothing more until we get new layers.
716 // - When a display is created with a private layer stack, we won't
717 // emit any black frames until a layer is added to the layer stack.
718 const bool mustRecompose = dirty && !(empty && wasEmpty);
719
720 const char flagPrefix[] = {'-', '+'};
721 static_cast<void>(flagPrefix);
722 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
723 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
724 flagPrefix[empty], flagPrefix[wasEmpty]);
725
726 mRenderSurface->beginFrame(mustRecompose);
727
728 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700729 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800730 }
731}
732
Lloyd Pique66d68602019-02-13 14:23:31 -0800733void Output::prepareFrame() {
734 ATRACE_CALL();
735 ALOGV(__FUNCTION__);
736
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700737 const auto& outputState = getState();
738 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800739 return;
740 }
741
742 chooseCompositionStrategy();
743
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700744 mRenderSurface->prepareFrame(outputState.usesClientComposition,
745 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800746}
747
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800748void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
749 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
750 return;
751 }
752
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700753 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800754 // transform the dirty region into this screen's coordinate space
755 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
756 if (!dirtyRegion.isEmpty()) {
757 base::unique_fd readyFence;
758 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800759 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800760
761 mRenderSurface->queueBuffer(std::move(readyFence));
762 }
763 }
764
765 postFramebuffer();
766
767 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
768
769 prepareFrame();
770}
771
Lloyd Piqued3d69882019-02-28 16:03:46 -0800772void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
773 ATRACE_CALL();
774 ALOGV(__FUNCTION__);
775
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700776 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800777 return;
778 }
779
780 // Repaint the framebuffer (if needed), getting the optional fence for when
781 // the composition completes.
782 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
783 if (!optReadyFence) {
784 return;
785 }
786
787 // swap buffers (presentation)
788 mRenderSurface->queueBuffer(std::move(*optReadyFence));
789}
790
791std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800792 ATRACE_CALL();
793 ALOGV(__FUNCTION__);
794
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700795 const auto& outputState = getState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800796 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700797 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800798 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800799 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800800 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800801 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800802 }
803
804 ALOGV("hasClientComposition");
805
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700806 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800807 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
808
809 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700810 clientCompositionDisplay.physicalDisplay = outputState.scissor;
811 clientCompositionDisplay.clip = outputState.scissor;
812 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
813 clientCompositionDisplay.orientation = outputState.orientation;
814 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
815 ? outputState.dataspace
816 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800817 clientCompositionDisplay.maxLuminance =
818 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
819
820 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700821 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
822 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800823 }
824
825 // Note: Updated by generateClientCompositionRequests
826 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
827
828 // Generate the client composition requests for the layers on this output.
829 std::vector<renderengine::LayerSettings> clientCompositionLayers =
830 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800831 clientCompositionDisplay.clearRegion,
832 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800833 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
834
835 // If we the display is secure, protected content support is enabled, and at
836 // least one layer has protected content, we need to use a secure back
837 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700838 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700839 auto layers = getOutputLayersOrderedByZ();
840 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
841 return layer->getLayer().getFEState().hasProtectedContent;
842 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800843 if (needsProtected != renderEngine.isProtected()) {
844 renderEngine.useProtectedContext(needsProtected);
845 }
846 if (needsProtected != mRenderSurface->isProtected() &&
847 needsProtected == renderEngine.isProtected()) {
848 mRenderSurface->setProtected(needsProtected);
849 }
850 }
851
852 base::unique_fd fd;
853 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
854 if (buf == nullptr) {
855 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
856 "client composition for this frame",
857 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800858 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800859 }
860
861 // We boost GPU frequency here because there will be color spaces conversion
862 // and it's expensive. We boost the GPU frequency so that GPU composition can
863 // finish in time. We must reset GPU frequency afterwards, because high frequency
864 // consumes extra battery.
865 const bool expensiveRenderingExpected =
866 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
867 if (expensiveRenderingExpected) {
868 setExpensiveRenderingExpected(true);
869 }
870
Alec Mourie4034bb2019-11-19 12:45:54 -0800871 const nsecs_t renderEngineStart = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -0800872 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
873 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800874 &readyFence);
Alec Mourie4034bb2019-11-19 12:45:54 -0800875 auto& timeStats = getCompositionEngine().getTimeStats();
876 if (readyFence.get() < 0) {
877 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
878 } else {
879 timeStats.recordRenderEngineDuration(renderEngineStart,
880 std::make_shared<FenceTime>(
881 new Fence(dup(readyFence.get()))));
882 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800883
Lloyd Piqued3d69882019-02-28 16:03:46 -0800884 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800885}
886
887std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800888 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800889 std::vector<renderengine::LayerSettings> clientCompositionLayers;
890 ALOGV("Rendering client layers");
891
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700892 const auto& outputState = getState();
893 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800894 const bool useIdentityTransform = false;
895 bool firstLayer = true;
896 // Used when a layer clears part of the buffer.
897 Region dummyRegion;
898
Lloyd Pique01c77c12019-04-17 12:48:32 -0700899 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800900 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700901 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800902 auto& layerFE = layer->getLayerFE();
903
Lloyd Piquea2468662019-03-07 21:31:06 -0800904 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800905 ALOGV("Layer: %s", layerFE.getDebugName());
906 if (clip.isEmpty()) {
907 ALOGV(" Skipping for empty clip");
908 firstLayer = false;
909 continue;
910 }
911
Vishnu Naira483b4a2019-12-12 15:07:52 -0800912 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800913
914 // We clear the client target for non-client composed layers if
915 // requested by the HWC. We skip this if the layer is not an opaque
916 // rectangle, as by definition the layer must blend with whatever is
917 // underneath. We also skip the first layer as the buffer target is
918 // guaranteed to start out cleared.
919 bool clearClientComposition =
920 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
921
922 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
923
924 if (clientComposition || clearClientComposition) {
925 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
926 clip,
927 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700928 layer->needsFiltering() || outputState.needsFiltering,
929 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800930 supportsProtectedContent,
931 clientComposition ? clearRegion : dummyRegion,
932 };
933 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700934 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800935 auto& layerSettings = *result;
936 layerSettings.source.buffer.buffer = nullptr;
937 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
938 layerSettings.alpha = half(0.0);
939 layerSettings.disableBlending = true;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800940 } else {
941 std::optional<renderengine::LayerSettings> shadowLayer =
942 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
943 outputDataspace);
944 if (shadowLayer) {
945 clientCompositionLayers.push_back(*shadowLayer);
946 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800947 }
948
Vishnu Naira483b4a2019-12-12 15:07:52 -0800949 // If the layer casts a shadow but the content casting the shadow is occluded, skip
950 // composing the non-shadow content and only draw the shadows.
951 const bool skipNonShadowContentComposition = clientComposition &&
952 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
953
954 if (!skipNonShadowContentComposition) {
955 layer->editState().clientCompositionTimestamp = systemTime();
956 clientCompositionLayers.push_back(*result);
957 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800958 }
959 }
960
961 firstLayer = false;
962 }
963
964 return clientCompositionLayers;
965}
966
967void Output::appendRegionFlashRequests(
968 const Region& flashRegion,
969 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
970 if (flashRegion.isEmpty()) {
971 return;
972 }
973
974 renderengine::LayerSettings layerSettings;
975 layerSettings.source.buffer.buffer = nullptr;
976 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
977 layerSettings.alpha = half(1.0);
978
979 for (const auto& rect : flashRegion) {
980 layerSettings.geometry.boundaries = rect.toFloatRect();
981 clientCompositionLayers.push_back(layerSettings);
982 }
983}
984
985void Output::setExpensiveRenderingExpected(bool) {
986 // The base class does nothing with this call.
987}
988
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800989void Output::postFramebuffer() {
990 ATRACE_CALL();
991 ALOGV(__FUNCTION__);
992
993 if (!getState().isEnabled) {
994 return;
995 }
996
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700997 auto& outputState = editState();
998 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -0800999 mRenderSurface->flip();
1000
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001001 auto frame = presentAndGetFrameFences();
1002
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001003 mRenderSurface->onPresentDisplayCompleted();
1004
Lloyd Pique01c77c12019-04-17 12:48:32 -07001005 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001006 // The layer buffer from the previous frame (if any) is released
1007 // by HWC only when the release fence from this frame (if any) is
1008 // signaled. Always get the release fence from HWC first.
1009 sp<Fence> releaseFence = Fence::NO_FENCE;
1010
1011 if (auto hwcLayer = layer->getHwcLayer()) {
1012 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1013 releaseFence = f->second;
1014 }
1015 }
1016
1017 // If the layer was client composited in the previous frame, we
1018 // need to merge with the previous client target acquire fence.
1019 // Since we do not track that, always merge with the current
1020 // client target acquire fence when it is available, even though
1021 // this is suboptimal.
1022 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001023 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001024 releaseFence =
1025 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1026 }
1027
1028 layer->getLayerFE().onLayerDisplayed(releaseFence);
1029 }
1030
1031 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001032 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001033 // supply them with the present fence.
1034 for (auto& weakLayer : mReleasedLayers) {
1035 if (auto layer = weakLayer.promote(); layer != nullptr) {
1036 layer->onLayerDisplayed(frame.presentFence);
1037 }
1038 }
1039
1040 // Clear out the released layers now that we're done with them.
1041 mReleasedLayers.clear();
1042}
1043
Lloyd Pique32cbe282018-10-19 13:09:22 -07001044void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001045 auto& outputState = editState();
1046 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001047}
1048
Lloyd Pique66d68602019-02-13 14:23:31 -08001049void Output::chooseCompositionStrategy() {
1050 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001051 auto& outputState = editState();
1052 outputState.usesClientComposition = true;
1053 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001054}
1055
Lloyd Pique688abd42019-02-15 15:42:24 -08001056bool Output::getSkipColorTransform() const {
1057 return true;
1058}
1059
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001060compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1061 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001062 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001063 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1064 }
1065 return result;
1066}
1067
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001068} // namespace impl
1069} // namespace android::compositionengine