blob: 28d26536a868918f89c0f0830f0ec0bbe54f4847 [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 Piqued3d69882019-02-28 16:03:46 -0800791 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800792 }
793
794 ALOGV("hasClientComposition");
795
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700796 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800797 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
798
799 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700800 clientCompositionDisplay.physicalDisplay = outputState.scissor;
801 clientCompositionDisplay.clip = outputState.scissor;
802 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
803 clientCompositionDisplay.orientation = outputState.orientation;
804 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
805 ? outputState.dataspace
806 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800807 clientCompositionDisplay.maxLuminance =
808 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
809
810 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700811 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
812 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800813 }
814
815 // Note: Updated by generateClientCompositionRequests
816 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
817
818 // Generate the client composition requests for the layers on this output.
819 std::vector<renderengine::LayerSettings> clientCompositionLayers =
820 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800821 clientCompositionDisplay.clearRegion,
822 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800823 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
824
825 // If we the display is secure, protected content support is enabled, and at
826 // least one layer has protected content, we need to use a secure back
827 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700828 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700829 auto layers = getOutputLayersOrderedByZ();
830 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
831 return layer->getLayer().getFEState().hasProtectedContent;
832 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800833 if (needsProtected != renderEngine.isProtected()) {
834 renderEngine.useProtectedContext(needsProtected);
835 }
836 if (needsProtected != mRenderSurface->isProtected() &&
837 needsProtected == renderEngine.isProtected()) {
838 mRenderSurface->setProtected(needsProtected);
839 }
840 }
841
842 base::unique_fd fd;
843 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
844 if (buf == nullptr) {
845 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
846 "client composition for this frame",
847 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800848 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800849 }
850
851 // We boost GPU frequency here because there will be color spaces conversion
852 // and it's expensive. We boost the GPU frequency so that GPU composition can
853 // finish in time. We must reset GPU frequency afterwards, because high frequency
854 // consumes extra battery.
855 const bool expensiveRenderingExpected =
856 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
857 if (expensiveRenderingExpected) {
858 setExpensiveRenderingExpected(true);
859 }
860
Alec Mourie4034bb2019-11-19 12:45:54 -0800861 const nsecs_t renderEngineStart = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -0800862 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
863 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800864 &readyFence);
Alec Mourie4034bb2019-11-19 12:45:54 -0800865 auto& timeStats = getCompositionEngine().getTimeStats();
866 if (readyFence.get() < 0) {
867 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
868 } else {
869 timeStats.recordRenderEngineDuration(renderEngineStart,
870 std::make_shared<FenceTime>(
871 new Fence(dup(readyFence.get()))));
872 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800873
874 if (expensiveRenderingExpected) {
875 setExpensiveRenderingExpected(false);
876 }
877
Lloyd Piqued3d69882019-02-28 16:03:46 -0800878 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800879}
880
881std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800882 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800883 std::vector<renderengine::LayerSettings> clientCompositionLayers;
884 ALOGV("Rendering client layers");
885
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700886 const auto& outputState = getState();
887 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800888 const bool useIdentityTransform = false;
889 bool firstLayer = true;
890 // Used when a layer clears part of the buffer.
891 Region dummyRegion;
892
Lloyd Pique01c77c12019-04-17 12:48:32 -0700893 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800894 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700895 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800896 auto& layerFE = layer->getLayerFE();
897
Lloyd Piquea2468662019-03-07 21:31:06 -0800898 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800899 ALOGV("Layer: %s", layerFE.getDebugName());
900 if (clip.isEmpty()) {
901 ALOGV(" Skipping for empty clip");
902 firstLayer = false;
903 continue;
904 }
905
Vishnu Naira483b4a2019-12-12 15:07:52 -0800906 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800907
908 // We clear the client target for non-client composed layers if
909 // requested by the HWC. We skip this if the layer is not an opaque
910 // rectangle, as by definition the layer must blend with whatever is
911 // underneath. We also skip the first layer as the buffer target is
912 // guaranteed to start out cleared.
913 bool clearClientComposition =
914 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
915
916 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
917
918 if (clientComposition || clearClientComposition) {
919 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
920 clip,
921 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700922 layer->needsFiltering() || outputState.needsFiltering,
923 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800924 supportsProtectedContent,
925 clientComposition ? clearRegion : dummyRegion,
926 };
927 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700928 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800929 auto& layerSettings = *result;
930 layerSettings.source.buffer.buffer = nullptr;
931 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
932 layerSettings.alpha = half(0.0);
933 layerSettings.disableBlending = true;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800934 } else {
935 std::optional<renderengine::LayerSettings> shadowLayer =
936 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
937 outputDataspace);
938 if (shadowLayer) {
939 clientCompositionLayers.push_back(*shadowLayer);
940 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800941 }
942
Vishnu Naira483b4a2019-12-12 15:07:52 -0800943 // If the layer casts a shadow but the content casting the shadow is occluded, skip
944 // composing the non-shadow content and only draw the shadows.
945 const bool skipNonShadowContentComposition = clientComposition &&
946 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
947
948 if (!skipNonShadowContentComposition) {
949 layer->editState().clientCompositionTimestamp = systemTime();
950 clientCompositionLayers.push_back(*result);
951 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800952 }
953 }
954
955 firstLayer = false;
956 }
957
958 return clientCompositionLayers;
959}
960
961void Output::appendRegionFlashRequests(
962 const Region& flashRegion,
963 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
964 if (flashRegion.isEmpty()) {
965 return;
966 }
967
968 renderengine::LayerSettings layerSettings;
969 layerSettings.source.buffer.buffer = nullptr;
970 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
971 layerSettings.alpha = half(1.0);
972
973 for (const auto& rect : flashRegion) {
974 layerSettings.geometry.boundaries = rect.toFloatRect();
975 clientCompositionLayers.push_back(layerSettings);
976 }
977}
978
979void Output::setExpensiveRenderingExpected(bool) {
980 // The base class does nothing with this call.
981}
982
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800983void Output::postFramebuffer() {
984 ATRACE_CALL();
985 ALOGV(__FUNCTION__);
986
987 if (!getState().isEnabled) {
988 return;
989 }
990
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700991 auto& outputState = editState();
992 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -0800993 mRenderSurface->flip();
994
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800995 auto frame = presentAndGetFrameFences();
996
Lloyd Pique7d90ba52019-08-08 11:57:53 -0700997 mRenderSurface->onPresentDisplayCompleted();
998
Lloyd Pique01c77c12019-04-17 12:48:32 -0700999 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001000 // The layer buffer from the previous frame (if any) is released
1001 // by HWC only when the release fence from this frame (if any) is
1002 // signaled. Always get the release fence from HWC first.
1003 sp<Fence> releaseFence = Fence::NO_FENCE;
1004
1005 if (auto hwcLayer = layer->getHwcLayer()) {
1006 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1007 releaseFence = f->second;
1008 }
1009 }
1010
1011 // If the layer was client composited in the previous frame, we
1012 // need to merge with the previous client target acquire fence.
1013 // Since we do not track that, always merge with the current
1014 // client target acquire fence when it is available, even though
1015 // this is suboptimal.
1016 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001017 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001018 releaseFence =
1019 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1020 }
1021
1022 layer->getLayerFE().onLayerDisplayed(releaseFence);
1023 }
1024
1025 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001026 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001027 // supply them with the present fence.
1028 for (auto& weakLayer : mReleasedLayers) {
1029 if (auto layer = weakLayer.promote(); layer != nullptr) {
1030 layer->onLayerDisplayed(frame.presentFence);
1031 }
1032 }
1033
1034 // Clear out the released layers now that we're done with them.
1035 mReleasedLayers.clear();
1036}
1037
Lloyd Pique32cbe282018-10-19 13:09:22 -07001038void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001039 auto& outputState = editState();
1040 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001041}
1042
Lloyd Pique66d68602019-02-13 14:23:31 -08001043void Output::chooseCompositionStrategy() {
1044 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001045 auto& outputState = editState();
1046 outputState.usesClientComposition = true;
1047 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001048}
1049
Lloyd Pique688abd42019-02-15 15:42:24 -08001050bool Output::getSkipColorTransform() const {
1051 return true;
1052}
1053
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001054compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1055 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001056 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001057 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1058 }
1059 return result;
1060}
1061
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001062} // namespace impl
1063} // namespace android::compositionengine