blob: 6e2816715f5b2b6d8896569ad283ec8d9a39bb9e [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 Pique688abd42019-02-15 15:42:24 -080031#include <renderengine/DisplaySettings.h>
32#include <renderengine/RenderEngine.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070033#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080034#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080035#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070036
Lloyd Pique688abd42019-02-15 15:42:24 -080037#include "TracedOrdinal.h"
38
Lloyd Piquefeb73d72018-12-04 17:23:44 -080039namespace android::compositionengine {
40
41Output::~Output() = default;
42
43namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070044
Lloyd Piquec29e4c62019-03-07 21:48:19 -080045namespace {
46
47template <typename T>
48class Reversed {
49public:
50 explicit Reversed(const T& container) : mContainer(container) {}
51 auto begin() { return mContainer.rbegin(); }
52 auto end() { return mContainer.rend(); }
53
54private:
55 const T& mContainer;
56};
57
58// Helper for enumerating over a container in reverse order
59template <typename T>
60Reversed<T> reversed(const T& c) {
61 return Reversed<T>(c);
62}
63
64} // namespace
65
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070066std::shared_ptr<Output> createOutput(
67 const compositionengine::CompositionEngine& compositionEngine) {
68 return createOutputTemplated<Output>(compositionEngine);
69}
Lloyd Pique32cbe282018-10-19 13:09:22 -070070
71Output::~Output() = default;
72
Lloyd Pique32cbe282018-10-19 13:09:22 -070073bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070074 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
75 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070076}
77
Lloyd Pique6c564cf2019-05-17 17:31:36 -070078std::optional<DisplayId> Output::getDisplayId() const {
79 return {};
80}
81
Lloyd Pique32cbe282018-10-19 13:09:22 -070082const std::string& Output::getName() const {
83 return mName;
84}
85
86void Output::setName(const std::string& name) {
87 mName = name;
88}
89
90void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070091 auto& outputState = editState();
92 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070093 return;
94 }
95
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070096 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -070097 dirtyEntireOutput();
98}
99
100void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame,
101 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700102 auto& outputState = editState();
103 outputState.transform = transform;
104 outputState.orientation = orientation;
105 outputState.scissor = scissor;
106 outputState.frame = frame;
107 outputState.viewport = viewport;
108 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109
110 dirtyEntireOutput();
111}
112
Lloyd Pique688abd42019-02-15 15:42:24 -0800113// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114void Output::setBounds(const ui::Size& size) {
115 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700116 // TODO(b/121291683): Rename outputState.size once more is moved.
117 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700118
119 dirtyEntireOutput();
120}
121
Lloyd Piqueef36b002019-01-23 17:52:04 -0800122void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700123 auto& outputState = editState();
124 outputState.layerStackId = layerStackId;
125 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700126
127 dirtyEntireOutput();
128}
129
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800130void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131 auto& colorTransformMatrix = editState().colorTransformMatrix;
132 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700133 return;
134 }
135
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700136 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800137
138 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700139}
140
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800141void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700142 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800143 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
144 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800145
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700146 auto& outputState = editState();
147 if (outputState.colorMode == colorProfile.mode &&
148 outputState.dataspace == colorProfile.dataspace &&
149 outputState.renderIntent == colorProfile.renderIntent &&
150 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800151 return;
152 }
153
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700154 outputState.colorMode = colorProfile.mode;
155 outputState.dataspace = colorProfile.dataspace;
156 outputState.renderIntent = colorProfile.renderIntent;
157 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700158
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800159 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700160
Lloyd Pique32cbe282018-10-19 13:09:22 -0700161 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800162 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
163 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800164
165 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700166}
167
168void Output::dump(std::string& out) const {
169 using android::base::StringAppendF;
170
171 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
172
173 out.append("\n ");
174
175 dumpBase(out);
176}
177
178void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700179 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700180
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700181 if (mDisplayColorProfile) {
182 mDisplayColorProfile->dump(out);
183 } else {
184 out.append(" No display color profile!\n");
185 }
186
Lloyd Pique31cb2942018-10-19 17:23:03 -0700187 if (mRenderSurface) {
188 mRenderSurface->dump(out);
189 } else {
190 out.append(" No render surface!\n");
191 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800192
Lloyd Pique01c77c12019-04-17 12:48:32 -0700193 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
194 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800195 if (!outputLayer) {
196 continue;
197 }
198 outputLayer->dump(out);
199 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200}
201
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700202compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
203 return mDisplayColorProfile.get();
204}
205
206void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
207 mDisplayColorProfile = std::move(mode);
208}
209
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800210const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
211 return mReleasedLayers;
212}
213
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700214void Output::setDisplayColorProfileForTest(
215 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
216 mDisplayColorProfile = std::move(mode);
217}
218
Lloyd Pique31cb2942018-10-19 17:23:03 -0700219compositionengine::RenderSurface* Output::getRenderSurface() const {
220 return mRenderSurface.get();
221}
222
223void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
224 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700225 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700226
227 dirtyEntireOutput();
228}
229
230void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
231 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700232}
233
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000234Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700235 const auto& outputState = getState();
236 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000237 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700238 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700239 }
240 return dirty;
241}
242
Lloyd Piquec6687342019-03-07 21:34:57 -0800243bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800244 // The layerStackId's must match, and also the layer must not be internal
245 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700246 const auto& outputState = getState();
247 return layerStackId && (*layerStackId == outputState.layerStackId) &&
248 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249}
250
Lloyd Pique66c20c42019-03-07 21:44:02 -0800251bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
252 if (!layer) {
253 return false;
254 }
255
Lloyd Pique9755fb72019-03-26 14:44:40 -0700256 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800257 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
258}
259
Lloyd Piquedf336d92019-03-07 21:38:42 -0800260std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700261 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800262 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800263}
264
Lloyd Pique01c77c12019-04-17 12:48:32 -0700265compositionengine::OutputLayer* Output::getOutputLayerForLayer(
266 compositionengine::Layer* layer) const {
267 auto index = findCurrentOutputLayerForLayer(layer);
268 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800269}
270
Lloyd Pique01c77c12019-04-17 12:48:32 -0700271std::optional<size_t> Output::findCurrentOutputLayerForLayer(
272 compositionengine::Layer* layer) const {
273 for (size_t i = 0; i < getOutputLayerCount(); i++) {
274 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
275 if (outputLayer && &outputLayer->getLayer() == layer) {
276 return i;
277 }
278 }
279 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800280}
281
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800282void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
283 mReleasedLayers = std::move(layers);
284}
285
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800286void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
287 LayerFESet& geomSnapshots) {
288 ATRACE_CALL();
289 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800290
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800291 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800292}
293
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800294void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800295 ATRACE_CALL();
296 ALOGV(__FUNCTION__);
297
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800298 updateColorProfile(refreshArgs);
299 updateAndWriteCompositionState(refreshArgs);
300 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800301 beginFrame();
302 prepareFrame();
303 devOptRepaintFlash(refreshArgs);
304 finishFrame(refreshArgs);
305 postFramebuffer();
306}
307
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800308void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
309 LayerFESet& layerFESet) {
310 ATRACE_CALL();
311 ALOGV(__FUNCTION__);
312
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700313 auto& outputState = editState();
314
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800315 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700316 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800317 return;
318 }
319
320 // Process the layers to determine visibility and coverage
321 compositionengine::Output::CoverageState coverage{layerFESet};
322 collectVisibleLayers(refreshArgs, coverage);
323
324 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700325 const ui::Transform& tr = outputState.transform;
326 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800327 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
328
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700329 outputState.undefinedRegion = undefinedRegion;
330 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800331}
332
333void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
334 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800335 // Evaluate the layers from front to back to determine what is visible. This
336 // also incrementally calculates the coverage information for each layer as
337 // well as the entire output.
338 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700339 // Incrementally process the coverage for each layer
340 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800341
342 // TODO(b/121291683): Stop early if the output is completely covered and
343 // no more layers could even be visible underneath the ones on top.
344 }
345
Lloyd Pique01c77c12019-04-17 12:48:32 -0700346 setReleasedLayers(refreshArgs);
347
348 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800349
350 // Generate a simple Z-order values to each visible output layer
351 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700352 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800353 outputLayer->editState().z = zOrder++;
354 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800355}
356
Lloyd Pique01c77c12019-04-17 12:48:32 -0700357void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
358 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800359 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
360 auto layerFE = layer->getLayerFE();
361 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700362 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800363 }
364
365 // Ensure we have a snapshot of the basic geometry layer state. Limit the
366 // snapshots to once per frame for each candidate layer, as layers may
367 // appear on multiple outputs.
368 if (!coverage.latchedLayers.count(layerFE)) {
369 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700370 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800371 compositionengine::LayerFE::StateSubset::BasicGeometry);
372 }
373
374 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700375 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800376
377 // Only consider the layers on the given layer stack
378 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700379 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380 }
381
382 /*
383 * opaqueRegion: area of a surface that is fully opaque.
384 */
385 Region opaqueRegion;
386
387 /*
388 * visibleRegion: area of a surface that is visible on screen and not fully
389 * transparent. This is essentially the layer's footprint minus the opaque
390 * regions above it. Areas covered by a translucent surface are considered
391 * visible.
392 */
393 Region visibleRegion;
394
395 /*
396 * coveredRegion: area of a surface that is covered by all visible regions
397 * above it (which includes the translucent areas).
398 */
399 Region coveredRegion;
400
401 /*
402 * transparentRegion: area of a surface that is hinted to be completely
403 * transparent. This is only used to tell when the layer has no visible non-
404 * transparent regions and can be removed from the layer list. It does not
405 * affect the visibleRegion of this layer or any layers beneath it. The hint
406 * may not be correct if apps don't respect the SurfaceView restrictions
407 * (which, sadly, some don't).
408 */
409 Region transparentRegion;
410
Vishnu Naira483b4a2019-12-12 15:07:52 -0800411 /*
412 * shadowRegion: Region cast by the layer's shadow.
413 */
414 Region shadowRegion;
415
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800416 // handle hidden surfaces by setting the visible region to empty
417 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700418 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800419 }
420
421 const ui::Transform& tr = layerFEState.geomLayerTransform;
422
423 // Get the visible region
424 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
425 // for computations like this?
Vishnu Naira483b4a2019-12-12 15:07:52 -0800426 const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
427 visibleRegion.set(visibleRect);
428
429 if (layerFEState.shadowRadius > 0.0f) {
430 // if the layer casts a shadow, offset the layers visible region and
431 // calculate the shadow region.
432 const int32_t inset = layerFEState.shadowRadius * -1.0f;
433 Rect visibleRectWithShadows(visibleRect);
434 visibleRectWithShadows.inset(inset, inset, inset, inset);
435 visibleRegion.set(visibleRectWithShadows);
436 shadowRegion = visibleRegion.subtract(visibleRect);
437 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800438
439 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700440 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800441 }
442
443 // Remove the transparent area from the visible region
444 if (!layerFEState.isOpaque) {
445 if (tr.preserveRects()) {
446 // transform the transparent region
447 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
448 } else {
449 // transformation too complex, can't do the
450 // transparent region optimization.
451 transparentRegion.clear();
452 }
453 }
454
455 // compute the opaque region
456 const int32_t layerOrientation = tr.getOrientation();
457 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
458 // If we one of the simple category of transforms (0/90/180/270 rotation
459 // + any flip), then the opaque region is the layer's footprint.
460 // Otherwise we don't try and compute the opaque region since there may
461 // be errors at the edges, and we treat the entire layer as
462 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800463 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800464 }
465
466 // Clip the covered region to the visible region
467 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
468
469 // Update accumAboveCoveredLayers for next (lower) layer
470 coverage.aboveCoveredLayers.orSelf(visibleRegion);
471
472 // subtract the opaque region covered by the layers above us
473 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
474
475 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700476 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800477 }
478
479 // Get coverage information for the layer as previously displayed,
480 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700481 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
482 auto prevOutputLayer =
483 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800484
485 // Get coverage information for the layer as previously displayed
486 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
487 const Region kEmptyRegion;
488 const Region& oldVisibleRegion =
489 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
490 const Region& oldCoveredRegion =
491 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
492
493 // compute this layer's dirty region
494 Region dirty;
495 if (layerFEState.contentDirty) {
496 // we need to invalidate the whole region
497 dirty = visibleRegion;
498 // as well, as the old visible region
499 dirty.orSelf(oldVisibleRegion);
500 } else {
501 /* compute the exposed region:
502 * the exposed region consists of two components:
503 * 1) what's VISIBLE now and was COVERED before
504 * 2) what's EXPOSED now less what was EXPOSED before
505 *
506 * note that (1) is conservative, we start with the whole visible region
507 * but only keep what used to be covered by something -- which mean it
508 * may have been exposed.
509 *
510 * (2) handles areas that were not covered by anything but got exposed
511 * because of a resize.
512 *
513 */
514 const Region newExposed = visibleRegion - coveredRegion;
515 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
516 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
517 }
518 dirty.subtractSelf(coverage.aboveOpaqueLayers);
519
520 // accumulate to the screen dirty region
521 coverage.dirtyRegion.orSelf(dirty);
522
523 // Update accumAboveOpaqueLayers for next (lower) layer
524 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
525
526 // Compute the visible non-transparent region
527 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
528
Vishnu Naira483b4a2019-12-12 15:07:52 -0800529 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800530 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700531 const auto& outputState = getState();
532 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
533 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800534 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700535 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800536 }
537
Vishnu Naira483b4a2019-12-12 15:07:52 -0800538 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
539
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800540 // The layer is visible. Either reuse the existing outputLayer if we have
541 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700542 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543
544 // Store the layer coverage information into the layer state as some of it
545 // is useful later.
546 auto& outputLayerState = result->editState();
547 outputLayerState.visibleRegion = visibleRegion;
548 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
549 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800550 outputLayerState.outputSpaceVisibleRegion =
551 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
552 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800553}
554
555void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
556 // The base class does nothing with this call.
557}
558
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800559void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700560 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700561 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800562 args.updatingGeometryThisFrame
563 ? LayerFE::StateSubset::GeometryAndContent
564 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800565 }
566}
567
568void Output::updateAndWriteCompositionState(
569 const compositionengine::CompositionRefreshArgs& refreshArgs) {
570 ATRACE_CALL();
571 ALOGV(__FUNCTION__);
572
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800573 if (!getState().isEnabled) {
574 return;
575 }
576
Lloyd Pique01c77c12019-04-17 12:48:32 -0700577 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700578 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
579 refreshArgs.devOptForceClientComposition);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800580
581 // Send the updated state to the HWC, if appropriate.
582 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
583 }
584}
585
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800586void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
587 setColorProfile(pickColorProfile(refreshArgs));
588}
589
590// Returns a data space that fits all visible layers. The returned data space
591// can only be one of
592// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
593// - Dataspace::DISPLAY_P3
594// - Dataspace::DISPLAY_BT2020
595// The returned HDR data space is one of
596// - Dataspace::UNKNOWN
597// - Dataspace::BT2020_HLG
598// - Dataspace::BT2020_PQ
599ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
600 bool* outIsHdrClientComposition) const {
601 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
602 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
603
Lloyd Pique01c77c12019-04-17 12:48:32 -0700604 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700605 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800606 case ui::Dataspace::V0_SCRGB:
607 case ui::Dataspace::V0_SCRGB_LINEAR:
608 case ui::Dataspace::BT2020:
609 case ui::Dataspace::BT2020_ITU:
610 case ui::Dataspace::BT2020_LINEAR:
611 case ui::Dataspace::DISPLAY_BT2020:
612 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
613 break;
614 case ui::Dataspace::DISPLAY_P3:
615 bestDataSpace = ui::Dataspace::DISPLAY_P3;
616 break;
617 case ui::Dataspace::BT2020_PQ:
618 case ui::Dataspace::BT2020_ITU_PQ:
619 bestDataSpace = ui::Dataspace::DISPLAY_P3;
620 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700621 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800622 break;
623 case ui::Dataspace::BT2020_HLG:
624 case ui::Dataspace::BT2020_ITU_HLG:
625 bestDataSpace = ui::Dataspace::DISPLAY_P3;
626 // When there's mixed PQ content and HLG content, we set the HDR
627 // data space to be BT2020_PQ and convert HLG to PQ.
628 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
629 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
630 }
631 break;
632 default:
633 break;
634 }
635 }
636
637 return bestDataSpace;
638}
639
640compositionengine::Output::ColorProfile Output::pickColorProfile(
641 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
642 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
643 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
644 ui::RenderIntent::COLORIMETRIC,
645 refreshArgs.colorSpaceAgnosticDataspace};
646 }
647
648 ui::Dataspace hdrDataSpace;
649 bool isHdrClientComposition = false;
650 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
651
652 switch (refreshArgs.forceOutputColorMode) {
653 case ui::ColorMode::SRGB:
654 bestDataSpace = ui::Dataspace::V0_SRGB;
655 break;
656 case ui::ColorMode::DISPLAY_P3:
657 bestDataSpace = ui::Dataspace::DISPLAY_P3;
658 break;
659 default:
660 break;
661 }
662
663 // respect hdrDataSpace only when there is no legacy HDR support
664 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
665 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
666 if (isHdr) {
667 bestDataSpace = hdrDataSpace;
668 }
669
670 ui::RenderIntent intent;
671 switch (refreshArgs.outputColorSetting) {
672 case OutputColorSetting::kManaged:
673 case OutputColorSetting::kUnmanaged:
674 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
675 : ui::RenderIntent::COLORIMETRIC;
676 break;
677 case OutputColorSetting::kEnhanced:
678 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
679 break;
680 default: // vendor display color setting
681 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
682 break;
683 }
684
685 ui::ColorMode outMode;
686 ui::Dataspace outDataSpace;
687 ui::RenderIntent outRenderIntent;
688 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
689 &outRenderIntent);
690
691 return ColorProfile{outMode, outDataSpace, outRenderIntent,
692 refreshArgs.colorSpaceAgnosticDataspace};
693}
694
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800695void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700696 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800697 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700698 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700699 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800700
701 // If nothing has changed (!dirty), don't recompose.
702 // If something changed, but we don't currently have any visible layers,
703 // and didn't when we last did a composition, then skip it this time.
704 // The second rule does two things:
705 // - When all layers are removed from a display, we'll emit one black
706 // frame, then nothing more until we get new layers.
707 // - When a display is created with a private layer stack, we won't
708 // emit any black frames until a layer is added to the layer stack.
709 const bool mustRecompose = dirty && !(empty && wasEmpty);
710
711 const char flagPrefix[] = {'-', '+'};
712 static_cast<void>(flagPrefix);
713 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
714 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
715 flagPrefix[empty], flagPrefix[wasEmpty]);
716
717 mRenderSurface->beginFrame(mustRecompose);
718
719 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700720 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800721 }
722}
723
Lloyd Pique66d68602019-02-13 14:23:31 -0800724void Output::prepareFrame() {
725 ATRACE_CALL();
726 ALOGV(__FUNCTION__);
727
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700728 const auto& outputState = getState();
729 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800730 return;
731 }
732
733 chooseCompositionStrategy();
734
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700735 mRenderSurface->prepareFrame(outputState.usesClientComposition,
736 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800737}
738
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800739void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
740 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
741 return;
742 }
743
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700744 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800745 // transform the dirty region into this screen's coordinate space
746 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
747 if (!dirtyRegion.isEmpty()) {
748 base::unique_fd readyFence;
749 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800750 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800751
752 mRenderSurface->queueBuffer(std::move(readyFence));
753 }
754 }
755
756 postFramebuffer();
757
758 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
759
760 prepareFrame();
761}
762
Lloyd Piqued3d69882019-02-28 16:03:46 -0800763void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
764 ATRACE_CALL();
765 ALOGV(__FUNCTION__);
766
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700767 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800768 return;
769 }
770
771 // Repaint the framebuffer (if needed), getting the optional fence for when
772 // the composition completes.
773 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
774 if (!optReadyFence) {
775 return;
776 }
777
778 // swap buffers (presentation)
779 mRenderSurface->queueBuffer(std::move(*optReadyFence));
780}
781
782std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800783 ATRACE_CALL();
784 ALOGV(__FUNCTION__);
785
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700786 const auto& outputState = getState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800787 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700788 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800789 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800790 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800791 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800792 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800793 }
794
795 ALOGV("hasClientComposition");
796
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700797 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800798 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
799
800 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700801 clientCompositionDisplay.physicalDisplay = outputState.scissor;
802 clientCompositionDisplay.clip = outputState.scissor;
803 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
804 clientCompositionDisplay.orientation = outputState.orientation;
805 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
806 ? outputState.dataspace
807 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800808 clientCompositionDisplay.maxLuminance =
809 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
810
811 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700812 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
813 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800814 }
815
816 // Note: Updated by generateClientCompositionRequests
817 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
818
819 // Generate the client composition requests for the layers on this output.
820 std::vector<renderengine::LayerSettings> clientCompositionLayers =
821 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800822 clientCompositionDisplay.clearRegion,
823 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800824 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
825
826 // If we the display is secure, protected content support is enabled, and at
827 // least one layer has protected content, we need to use a secure back
828 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700829 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700830 auto layers = getOutputLayersOrderedByZ();
831 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
832 return layer->getLayer().getFEState().hasProtectedContent;
833 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800834 if (needsProtected != renderEngine.isProtected()) {
835 renderEngine.useProtectedContext(needsProtected);
836 }
837 if (needsProtected != mRenderSurface->isProtected() &&
838 needsProtected == renderEngine.isProtected()) {
839 mRenderSurface->setProtected(needsProtected);
840 }
841 }
842
843 base::unique_fd fd;
844 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
845 if (buf == nullptr) {
846 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
847 "client composition for this frame",
848 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800849 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800850 }
851
852 // We boost GPU frequency here because there will be color spaces conversion
853 // and it's expensive. We boost the GPU frequency so that GPU composition can
854 // finish in time. We must reset GPU frequency afterwards, because high frequency
855 // consumes extra battery.
856 const bool expensiveRenderingExpected =
857 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
858 if (expensiveRenderingExpected) {
859 setExpensiveRenderingExpected(true);
860 }
861
Alec Mourie4034bb2019-11-19 12:45:54 -0800862 const nsecs_t renderEngineStart = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -0800863 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
864 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800865 &readyFence);
Alec Mourie4034bb2019-11-19 12:45:54 -0800866 auto& timeStats = getCompositionEngine().getTimeStats();
867 if (readyFence.get() < 0) {
868 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
869 } else {
870 timeStats.recordRenderEngineDuration(renderEngineStart,
871 std::make_shared<FenceTime>(
872 new Fence(dup(readyFence.get()))));
873 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800874
Lloyd Piqued3d69882019-02-28 16:03:46 -0800875 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800876}
877
878std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800879 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800880 std::vector<renderengine::LayerSettings> clientCompositionLayers;
881 ALOGV("Rendering client layers");
882
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700883 const auto& outputState = getState();
884 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800885 const bool useIdentityTransform = false;
886 bool firstLayer = true;
887 // Used when a layer clears part of the buffer.
888 Region dummyRegion;
889
Lloyd Pique01c77c12019-04-17 12:48:32 -0700890 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800891 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700892 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800893 auto& layerFE = layer->getLayerFE();
894
Lloyd Piquea2468662019-03-07 21:31:06 -0800895 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800896 ALOGV("Layer: %s", layerFE.getDebugName());
897 if (clip.isEmpty()) {
898 ALOGV(" Skipping for empty clip");
899 firstLayer = false;
900 continue;
901 }
902
Vishnu Naira483b4a2019-12-12 15:07:52 -0800903 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800904
905 // We clear the client target for non-client composed layers if
906 // requested by the HWC. We skip this if the layer is not an opaque
907 // rectangle, as by definition the layer must blend with whatever is
908 // underneath. We also skip the first layer as the buffer target is
909 // guaranteed to start out cleared.
910 bool clearClientComposition =
911 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
912
913 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
914
915 if (clientComposition || clearClientComposition) {
916 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
917 clip,
918 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700919 layer->needsFiltering() || outputState.needsFiltering,
920 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800921 supportsProtectedContent,
922 clientComposition ? clearRegion : dummyRegion,
923 };
924 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700925 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800926 auto& layerSettings = *result;
927 layerSettings.source.buffer.buffer = nullptr;
928 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
929 layerSettings.alpha = half(0.0);
930 layerSettings.disableBlending = true;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800931 } else {
932 std::optional<renderengine::LayerSettings> shadowLayer =
933 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
934 outputDataspace);
935 if (shadowLayer) {
936 clientCompositionLayers.push_back(*shadowLayer);
937 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800938 }
939
Vishnu Naira483b4a2019-12-12 15:07:52 -0800940 // If the layer casts a shadow but the content casting the shadow is occluded, skip
941 // composing the non-shadow content and only draw the shadows.
942 const bool skipNonShadowContentComposition = clientComposition &&
943 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
944
945 if (!skipNonShadowContentComposition) {
946 layer->editState().clientCompositionTimestamp = systemTime();
947 clientCompositionLayers.push_back(*result);
948 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800949 }
950 }
951
952 firstLayer = false;
953 }
954
955 return clientCompositionLayers;
956}
957
958void Output::appendRegionFlashRequests(
959 const Region& flashRegion,
960 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
961 if (flashRegion.isEmpty()) {
962 return;
963 }
964
965 renderengine::LayerSettings layerSettings;
966 layerSettings.source.buffer.buffer = nullptr;
967 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
968 layerSettings.alpha = half(1.0);
969
970 for (const auto& rect : flashRegion) {
971 layerSettings.geometry.boundaries = rect.toFloatRect();
972 clientCompositionLayers.push_back(layerSettings);
973 }
974}
975
976void Output::setExpensiveRenderingExpected(bool) {
977 // The base class does nothing with this call.
978}
979
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800980void Output::postFramebuffer() {
981 ATRACE_CALL();
982 ALOGV(__FUNCTION__);
983
984 if (!getState().isEnabled) {
985 return;
986 }
987
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700988 auto& outputState = editState();
989 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -0800990 mRenderSurface->flip();
991
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800992 auto frame = presentAndGetFrameFences();
993
Lloyd Pique7d90ba52019-08-08 11:57:53 -0700994 mRenderSurface->onPresentDisplayCompleted();
995
Lloyd Pique01c77c12019-04-17 12:48:32 -0700996 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800997 // The layer buffer from the previous frame (if any) is released
998 // by HWC only when the release fence from this frame (if any) is
999 // signaled. Always get the release fence from HWC first.
1000 sp<Fence> releaseFence = Fence::NO_FENCE;
1001
1002 if (auto hwcLayer = layer->getHwcLayer()) {
1003 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1004 releaseFence = f->second;
1005 }
1006 }
1007
1008 // If the layer was client composited in the previous frame, we
1009 // need to merge with the previous client target acquire fence.
1010 // Since we do not track that, always merge with the current
1011 // client target acquire fence when it is available, even though
1012 // this is suboptimal.
1013 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001014 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001015 releaseFence =
1016 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1017 }
1018
1019 layer->getLayerFE().onLayerDisplayed(releaseFence);
1020 }
1021
1022 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001023 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001024 // supply them with the present fence.
1025 for (auto& weakLayer : mReleasedLayers) {
1026 if (auto layer = weakLayer.promote(); layer != nullptr) {
1027 layer->onLayerDisplayed(frame.presentFence);
1028 }
1029 }
1030
1031 // Clear out the released layers now that we're done with them.
1032 mReleasedLayers.clear();
1033}
1034
Lloyd Pique32cbe282018-10-19 13:09:22 -07001035void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001036 auto& outputState = editState();
1037 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001038}
1039
Lloyd Pique66d68602019-02-13 14:23:31 -08001040void Output::chooseCompositionStrategy() {
1041 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001042 auto& outputState = editState();
1043 outputState.usesClientComposition = true;
1044 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001045}
1046
Lloyd Pique688abd42019-02-15 15:42:24 -08001047bool Output::getSkipColorTransform() const {
1048 return true;
1049}
1050
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001051compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1052 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001053 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001054 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1055 }
1056 return result;
1057}
1058
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001059} // namespace impl
1060} // namespace android::compositionengine