blob: df33cf0df1049b07708f77611e68df1b79af6244 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <thread>
22
Lloyd Pique32cbe282018-10-19 13:09:22 -070023#include <android-base/stringprintf.h>
24#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080025#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070026#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080027#include <compositionengine/Layer.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070029#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070030#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070031#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070032#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080033#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070034#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include <renderengine/DisplaySettings.h>
36#include <renderengine/RenderEngine.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070037#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080038#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080039#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070040
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include "TracedOrdinal.h"
42
Lloyd Piquefeb73d72018-12-04 17:23:44 -080043namespace android::compositionengine {
44
45Output::~Output() = default;
46
47namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070048
Lloyd Piquec29e4c62019-03-07 21:48:19 -080049namespace {
50
51template <typename T>
52class Reversed {
53public:
54 explicit Reversed(const T& container) : mContainer(container) {}
55 auto begin() { return mContainer.rbegin(); }
56 auto end() { return mContainer.rend(); }
57
58private:
59 const T& mContainer;
60};
61
62// Helper for enumerating over a container in reverse order
63template <typename T>
64Reversed<T> reversed(const T& c) {
65 return Reversed<T>(c);
66}
67
68} // namespace
69
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070070std::shared_ptr<Output> createOutput(
71 const compositionengine::CompositionEngine& compositionEngine) {
72 return createOutputTemplated<Output>(compositionEngine);
73}
Lloyd Pique32cbe282018-10-19 13:09:22 -070074
75Output::~Output() = default;
76
Lloyd Pique32cbe282018-10-19 13:09:22 -070077bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070078 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
79 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070080}
81
Lloyd Pique6c564cf2019-05-17 17:31:36 -070082std::optional<DisplayId> Output::getDisplayId() const {
83 return {};
84}
85
Lloyd Pique32cbe282018-10-19 13:09:22 -070086const std::string& Output::getName() const {
87 return mName;
88}
89
90void Output::setName(const std::string& name) {
91 mName = name;
92}
93
94void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070095 auto& outputState = editState();
96 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070097 return;
98 }
99
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700100 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101 dirtyEntireOutput();
102}
103
104void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame,
105 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700106 auto& outputState = editState();
107 outputState.transform = transform;
108 outputState.orientation = orientation;
109 outputState.scissor = scissor;
110 outputState.frame = frame;
111 outputState.viewport = viewport;
112 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700113
114 dirtyEntireOutput();
115}
116
Lloyd Pique688abd42019-02-15 15:42:24 -0800117// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700118void Output::setBounds(const ui::Size& size) {
119 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700120 // TODO(b/121291683): Rename outputState.size once more is moved.
121 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700122
123 dirtyEntireOutput();
124}
125
Lloyd Piqueef36b002019-01-23 17:52:04 -0800126void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127 auto& outputState = editState();
128 outputState.layerStackId = layerStackId;
129 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130
131 dirtyEntireOutput();
132}
133
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800134void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700135 auto& colorTransformMatrix = editState().colorTransformMatrix;
136 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700137 return;
138 }
139
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800141
142 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700143}
144
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800145void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700146 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800147 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
148 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800149
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700150 auto& outputState = editState();
151 if (outputState.colorMode == colorProfile.mode &&
152 outputState.dataspace == colorProfile.dataspace &&
153 outputState.renderIntent == colorProfile.renderIntent &&
154 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800155 return;
156 }
157
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700158 outputState.colorMode = colorProfile.mode;
159 outputState.dataspace = colorProfile.dataspace;
160 outputState.renderIntent = colorProfile.renderIntent;
161 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700162
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800163 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700164
Lloyd Pique32cbe282018-10-19 13:09:22 -0700165 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800166 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
167 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800168
169 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700170}
171
172void Output::dump(std::string& out) const {
173 using android::base::StringAppendF;
174
175 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
176
177 out.append("\n ");
178
179 dumpBase(out);
180}
181
182void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700183 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700185 if (mDisplayColorProfile) {
186 mDisplayColorProfile->dump(out);
187 } else {
188 out.append(" No display color profile!\n");
189 }
190
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191 if (mRenderSurface) {
192 mRenderSurface->dump(out);
193 } else {
194 out.append(" No render surface!\n");
195 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800196
Lloyd Pique01c77c12019-04-17 12:48:32 -0700197 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
198 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800199 if (!outputLayer) {
200 continue;
201 }
202 outputLayer->dump(out);
203 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700204}
205
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700206compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
207 return mDisplayColorProfile.get();
208}
209
210void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
211 mDisplayColorProfile = std::move(mode);
212}
213
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800214const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
215 return mReleasedLayers;
216}
217
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700218void Output::setDisplayColorProfileForTest(
219 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
220 mDisplayColorProfile = std::move(mode);
221}
222
Lloyd Pique31cb2942018-10-19 17:23:03 -0700223compositionengine::RenderSurface* Output::getRenderSurface() const {
224 return mRenderSurface.get();
225}
226
227void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
228 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700229 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700230
231 dirtyEntireOutput();
232}
233
Vishnu Nair9b079a22020-01-21 14:36:08 -0800234void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
235 if (cacheSize == 0) {
236 mClientCompositionRequestCache.reset();
237 } else {
238 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
239 }
240};
241
Lloyd Pique31cb2942018-10-19 17:23:03 -0700242void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
243 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700244}
245
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000246Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700247 const auto& outputState = getState();
248 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000249 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700250 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700251 }
252 return dirty;
253}
254
Lloyd Piquec6687342019-03-07 21:34:57 -0800255bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800256 // The layerStackId's must match, and also the layer must not be internal
257 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700258 const auto& outputState = getState();
259 return layerStackId && (*layerStackId == outputState.layerStackId) &&
260 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700261}
262
Lloyd Pique66c20c42019-03-07 21:44:02 -0800263bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
264 if (!layer) {
265 return false;
266 }
267
Lloyd Pique9755fb72019-03-26 14:44:40 -0700268 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800269 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
270}
271
Lloyd Piquedf336d92019-03-07 21:38:42 -0800272std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700273 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800274 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800275}
276
Lloyd Pique01c77c12019-04-17 12:48:32 -0700277compositionengine::OutputLayer* Output::getOutputLayerForLayer(
278 compositionengine::Layer* layer) const {
279 auto index = findCurrentOutputLayerForLayer(layer);
280 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800281}
282
Lloyd Pique01c77c12019-04-17 12:48:32 -0700283std::optional<size_t> Output::findCurrentOutputLayerForLayer(
284 compositionengine::Layer* layer) const {
285 for (size_t i = 0; i < getOutputLayerCount(); i++) {
286 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
287 if (outputLayer && &outputLayer->getLayer() == layer) {
288 return i;
289 }
290 }
291 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800292}
293
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800294void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
295 mReleasedLayers = std::move(layers);
296}
297
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800298void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
299 LayerFESet& geomSnapshots) {
300 ATRACE_CALL();
301 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800302
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800303 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800304}
305
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800306void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800307 ATRACE_CALL();
308 ALOGV(__FUNCTION__);
309
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800310 updateColorProfile(refreshArgs);
311 updateAndWriteCompositionState(refreshArgs);
312 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800313 beginFrame();
314 prepareFrame();
315 devOptRepaintFlash(refreshArgs);
316 finishFrame(refreshArgs);
317 postFramebuffer();
318}
319
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800320void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
321 LayerFESet& layerFESet) {
322 ATRACE_CALL();
323 ALOGV(__FUNCTION__);
324
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700325 auto& outputState = editState();
326
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800327 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700328 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800329 return;
330 }
331
332 // Process the layers to determine visibility and coverage
333 compositionengine::Output::CoverageState coverage{layerFESet};
334 collectVisibleLayers(refreshArgs, coverage);
335
336 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700337 const ui::Transform& tr = outputState.transform;
338 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800339 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
340
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700341 outputState.undefinedRegion = undefinedRegion;
342 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800343}
344
345void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
346 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800347 // Evaluate the layers from front to back to determine what is visible. This
348 // also incrementally calculates the coverage information for each layer as
349 // well as the entire output.
350 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700351 // Incrementally process the coverage for each layer
352 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800353
354 // TODO(b/121291683): Stop early if the output is completely covered and
355 // no more layers could even be visible underneath the ones on top.
356 }
357
Lloyd Pique01c77c12019-04-17 12:48:32 -0700358 setReleasedLayers(refreshArgs);
359
360 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800361
362 // Generate a simple Z-order values to each visible output layer
363 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700364 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800365 outputLayer->editState().z = zOrder++;
366 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800367}
368
Lloyd Pique01c77c12019-04-17 12:48:32 -0700369void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
370 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800371 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
372 auto layerFE = layer->getLayerFE();
373 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700374 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800375 }
376
377 // Ensure we have a snapshot of the basic geometry layer state. Limit the
378 // snapshots to once per frame for each candidate layer, as layers may
379 // appear on multiple outputs.
380 if (!coverage.latchedLayers.count(layerFE)) {
381 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700382 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800383 compositionengine::LayerFE::StateSubset::BasicGeometry);
384 }
385
386 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700387 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800388
389 // Only consider the layers on the given layer stack
390 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700391 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800392 }
393
394 /*
395 * opaqueRegion: area of a surface that is fully opaque.
396 */
397 Region opaqueRegion;
398
399 /*
400 * visibleRegion: area of a surface that is visible on screen and not fully
401 * transparent. This is essentially the layer's footprint minus the opaque
402 * regions above it. Areas covered by a translucent surface are considered
403 * visible.
404 */
405 Region visibleRegion;
406
407 /*
408 * coveredRegion: area of a surface that is covered by all visible regions
409 * above it (which includes the translucent areas).
410 */
411 Region coveredRegion;
412
413 /*
414 * transparentRegion: area of a surface that is hinted to be completely
415 * transparent. This is only used to tell when the layer has no visible non-
416 * transparent regions and can be removed from the layer list. It does not
417 * affect the visibleRegion of this layer or any layers beneath it. The hint
418 * may not be correct if apps don't respect the SurfaceView restrictions
419 * (which, sadly, some don't).
420 */
421 Region transparentRegion;
422
Vishnu Naira483b4a2019-12-12 15:07:52 -0800423 /*
424 * shadowRegion: Region cast by the layer's shadow.
425 */
426 Region shadowRegion;
427
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800428 // handle hidden surfaces by setting the visible region to empty
429 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700430 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800431 }
432
433 const ui::Transform& tr = layerFEState.geomLayerTransform;
434
435 // Get the visible region
436 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
437 // for computations like this?
Vishnu Naira483b4a2019-12-12 15:07:52 -0800438 const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
439 visibleRegion.set(visibleRect);
440
441 if (layerFEState.shadowRadius > 0.0f) {
442 // if the layer casts a shadow, offset the layers visible region and
443 // calculate the shadow region.
444 const int32_t inset = layerFEState.shadowRadius * -1.0f;
445 Rect visibleRectWithShadows(visibleRect);
446 visibleRectWithShadows.inset(inset, inset, inset, inset);
447 visibleRegion.set(visibleRectWithShadows);
448 shadowRegion = visibleRegion.subtract(visibleRect);
449 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800450
451 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700452 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800453 }
454
455 // Remove the transparent area from the visible region
456 if (!layerFEState.isOpaque) {
457 if (tr.preserveRects()) {
458 // transform the transparent region
459 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
460 } else {
461 // transformation too complex, can't do the
462 // transparent region optimization.
463 transparentRegion.clear();
464 }
465 }
466
467 // compute the opaque region
468 const int32_t layerOrientation = tr.getOrientation();
469 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
470 // If we one of the simple category of transforms (0/90/180/270 rotation
471 // + any flip), then the opaque region is the layer's footprint.
472 // Otherwise we don't try and compute the opaque region since there may
473 // be errors at the edges, and we treat the entire layer as
474 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800475 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800476 }
477
478 // Clip the covered region to the visible region
479 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
480
481 // Update accumAboveCoveredLayers for next (lower) layer
482 coverage.aboveCoveredLayers.orSelf(visibleRegion);
483
484 // subtract the opaque region covered by the layers above us
485 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
486
487 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700488 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800489 }
490
491 // Get coverage information for the layer as previously displayed,
492 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700493 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
494 auto prevOutputLayer =
495 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800496
497 // Get coverage information for the layer as previously displayed
498 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
499 const Region kEmptyRegion;
500 const Region& oldVisibleRegion =
501 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
502 const Region& oldCoveredRegion =
503 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
504
505 // compute this layer's dirty region
506 Region dirty;
507 if (layerFEState.contentDirty) {
508 // we need to invalidate the whole region
509 dirty = visibleRegion;
510 // as well, as the old visible region
511 dirty.orSelf(oldVisibleRegion);
512 } else {
513 /* compute the exposed region:
514 * the exposed region consists of two components:
515 * 1) what's VISIBLE now and was COVERED before
516 * 2) what's EXPOSED now less what was EXPOSED before
517 *
518 * note that (1) is conservative, we start with the whole visible region
519 * but only keep what used to be covered by something -- which mean it
520 * may have been exposed.
521 *
522 * (2) handles areas that were not covered by anything but got exposed
523 * because of a resize.
524 *
525 */
526 const Region newExposed = visibleRegion - coveredRegion;
527 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
528 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
529 }
530 dirty.subtractSelf(coverage.aboveOpaqueLayers);
531
532 // accumulate to the screen dirty region
533 coverage.dirtyRegion.orSelf(dirty);
534
535 // Update accumAboveOpaqueLayers for next (lower) layer
536 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
537
538 // Compute the visible non-transparent region
539 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
540
Vishnu Naira483b4a2019-12-12 15:07:52 -0800541 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800542 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700543 const auto& outputState = getState();
544 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
545 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800546 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700547 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800548 }
549
Vishnu Naira483b4a2019-12-12 15:07:52 -0800550 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
551
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552 // The layer is visible. Either reuse the existing outputLayer if we have
553 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700554 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800555
556 // Store the layer coverage information into the layer state as some of it
557 // is useful later.
558 auto& outputLayerState = result->editState();
559 outputLayerState.visibleRegion = visibleRegion;
560 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
561 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800562 outputLayerState.outputSpaceVisibleRegion =
563 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
564 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800565}
566
567void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
568 // The base class does nothing with this call.
569}
570
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800571void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700572 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700573 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800574 args.updatingGeometryThisFrame
575 ? LayerFE::StateSubset::GeometryAndContent
576 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800577 }
578}
579
580void Output::updateAndWriteCompositionState(
581 const compositionengine::CompositionRefreshArgs& refreshArgs) {
582 ATRACE_CALL();
583 ALOGV(__FUNCTION__);
584
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800585 if (!getState().isEnabled) {
586 return;
587 }
588
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800589 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
590 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
591
Lloyd Pique01c77c12019-04-17 12:48:32 -0700592 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700593 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800594 refreshArgs.devOptForceClientComposition ||
595 forceClientComposition);
596
597 if (mLayerRequestingBackgroundBlur == layer) {
598 forceClientComposition = false;
599 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800600
601 // Send the updated state to the HWC, if appropriate.
602 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
603 }
604}
605
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800606compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
607 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
608 for (auto* layer : getOutputLayersOrderedByZ()) {
609 if (layer->getLayer().getFEState().backgroundBlurRadius > 0) {
610 layerRequestingBgComposition = layer;
611 }
612 }
613 return layerRequestingBgComposition;
614}
615
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800616void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
617 setColorProfile(pickColorProfile(refreshArgs));
618}
619
620// Returns a data space that fits all visible layers. The returned data space
621// can only be one of
622// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
623// - Dataspace::DISPLAY_P3
624// - Dataspace::DISPLAY_BT2020
625// The returned HDR data space is one of
626// - Dataspace::UNKNOWN
627// - Dataspace::BT2020_HLG
628// - Dataspace::BT2020_PQ
629ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
630 bool* outIsHdrClientComposition) const {
631 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
632 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
633
Lloyd Pique01c77c12019-04-17 12:48:32 -0700634 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700635 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800636 case ui::Dataspace::V0_SCRGB:
637 case ui::Dataspace::V0_SCRGB_LINEAR:
638 case ui::Dataspace::BT2020:
639 case ui::Dataspace::BT2020_ITU:
640 case ui::Dataspace::BT2020_LINEAR:
641 case ui::Dataspace::DISPLAY_BT2020:
642 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
643 break;
644 case ui::Dataspace::DISPLAY_P3:
645 bestDataSpace = ui::Dataspace::DISPLAY_P3;
646 break;
647 case ui::Dataspace::BT2020_PQ:
648 case ui::Dataspace::BT2020_ITU_PQ:
649 bestDataSpace = ui::Dataspace::DISPLAY_P3;
650 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700651 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800652 break;
653 case ui::Dataspace::BT2020_HLG:
654 case ui::Dataspace::BT2020_ITU_HLG:
655 bestDataSpace = ui::Dataspace::DISPLAY_P3;
656 // When there's mixed PQ content and HLG content, we set the HDR
657 // data space to be BT2020_PQ and convert HLG to PQ.
658 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
659 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
660 }
661 break;
662 default:
663 break;
664 }
665 }
666
667 return bestDataSpace;
668}
669
670compositionengine::Output::ColorProfile Output::pickColorProfile(
671 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
672 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
673 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
674 ui::RenderIntent::COLORIMETRIC,
675 refreshArgs.colorSpaceAgnosticDataspace};
676 }
677
678 ui::Dataspace hdrDataSpace;
679 bool isHdrClientComposition = false;
680 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
681
682 switch (refreshArgs.forceOutputColorMode) {
683 case ui::ColorMode::SRGB:
684 bestDataSpace = ui::Dataspace::V0_SRGB;
685 break;
686 case ui::ColorMode::DISPLAY_P3:
687 bestDataSpace = ui::Dataspace::DISPLAY_P3;
688 break;
689 default:
690 break;
691 }
692
693 // respect hdrDataSpace only when there is no legacy HDR support
694 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
695 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
696 if (isHdr) {
697 bestDataSpace = hdrDataSpace;
698 }
699
700 ui::RenderIntent intent;
701 switch (refreshArgs.outputColorSetting) {
702 case OutputColorSetting::kManaged:
703 case OutputColorSetting::kUnmanaged:
704 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
705 : ui::RenderIntent::COLORIMETRIC;
706 break;
707 case OutputColorSetting::kEnhanced:
708 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
709 break;
710 default: // vendor display color setting
711 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
712 break;
713 }
714
715 ui::ColorMode outMode;
716 ui::Dataspace outDataSpace;
717 ui::RenderIntent outRenderIntent;
718 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
719 &outRenderIntent);
720
721 return ColorProfile{outMode, outDataSpace, outRenderIntent,
722 refreshArgs.colorSpaceAgnosticDataspace};
723}
724
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800725void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700726 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800727 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700728 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700729 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800730
731 // If nothing has changed (!dirty), don't recompose.
732 // If something changed, but we don't currently have any visible layers,
733 // and didn't when we last did a composition, then skip it this time.
734 // The second rule does two things:
735 // - When all layers are removed from a display, we'll emit one black
736 // frame, then nothing more until we get new layers.
737 // - When a display is created with a private layer stack, we won't
738 // emit any black frames until a layer is added to the layer stack.
739 const bool mustRecompose = dirty && !(empty && wasEmpty);
740
741 const char flagPrefix[] = {'-', '+'};
742 static_cast<void>(flagPrefix);
743 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
744 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
745 flagPrefix[empty], flagPrefix[wasEmpty]);
746
747 mRenderSurface->beginFrame(mustRecompose);
748
749 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700750 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800751 }
752}
753
Lloyd Pique66d68602019-02-13 14:23:31 -0800754void Output::prepareFrame() {
755 ATRACE_CALL();
756 ALOGV(__FUNCTION__);
757
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700758 const auto& outputState = getState();
759 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800760 return;
761 }
762
763 chooseCompositionStrategy();
764
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700765 mRenderSurface->prepareFrame(outputState.usesClientComposition,
766 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800767}
768
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800769void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
770 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
771 return;
772 }
773
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700774 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800775 // transform the dirty region into this screen's coordinate space
776 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
777 if (!dirtyRegion.isEmpty()) {
778 base::unique_fd readyFence;
779 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800780 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800781
782 mRenderSurface->queueBuffer(std::move(readyFence));
783 }
784 }
785
786 postFramebuffer();
787
788 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
789
790 prepareFrame();
791}
792
Lloyd Piqued3d69882019-02-28 16:03:46 -0800793void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
794 ATRACE_CALL();
795 ALOGV(__FUNCTION__);
796
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700797 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800798 return;
799 }
800
801 // Repaint the framebuffer (if needed), getting the optional fence for when
802 // the composition completes.
803 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
804 if (!optReadyFence) {
805 return;
806 }
807
808 // swap buffers (presentation)
809 mRenderSurface->queueBuffer(std::move(*optReadyFence));
810}
811
812std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800813 ATRACE_CALL();
814 ALOGV(__FUNCTION__);
815
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700816 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800817 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800818 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700819 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800820 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800821 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800822 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800823 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800824 }
825
826 ALOGV("hasClientComposition");
827
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700828 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800829 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
830
831 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700832 clientCompositionDisplay.physicalDisplay = outputState.scissor;
833 clientCompositionDisplay.clip = outputState.scissor;
834 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
835 clientCompositionDisplay.orientation = outputState.orientation;
836 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
837 ? outputState.dataspace
838 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800839 clientCompositionDisplay.maxLuminance =
840 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
841
842 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700843 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
844 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800845 }
846
847 // Note: Updated by generateClientCompositionRequests
848 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
849
850 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800851 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800852 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800853 clientCompositionDisplay.clearRegion,
854 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800855 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
856
857 // If we the display is secure, protected content support is enabled, and at
858 // least one layer has protected content, we need to use a secure back
859 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700860 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700861 auto layers = getOutputLayersOrderedByZ();
862 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
863 return layer->getLayer().getFEState().hasProtectedContent;
864 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800865 if (needsProtected != renderEngine.isProtected()) {
866 renderEngine.useProtectedContext(needsProtected);
867 }
868 if (needsProtected != mRenderSurface->isProtected() &&
869 needsProtected == renderEngine.isProtected()) {
870 mRenderSurface->setProtected(needsProtected);
871 }
872 }
873
874 base::unique_fd fd;
875 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
876 if (buf == nullptr) {
877 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
878 "client composition for this frame",
879 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800880 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800881 }
882
Vishnu Nair9b079a22020-01-21 14:36:08 -0800883 // Check if the client composition requests were rendered into the provided graphic buffer. If
884 // so, we can reuse the buffer and avoid client composition.
885 if (mClientCompositionRequestCache) {
886 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
887 clientCompositionLayers)) {
888 outputCompositionState.reusedClientComposition = true;
889 setExpensiveRenderingExpected(false);
890 return readyFence;
891 }
892 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
893 clientCompositionLayers);
894 }
895
Lloyd Pique688abd42019-02-15 15:42:24 -0800896 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800897 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
898 // GPU composition can finish in time. We must reset GPU frequency afterwards,
899 // because high frequency consumes extra battery.
Lloyd Pique688abd42019-02-15 15:42:24 -0800900 const bool expensiveRenderingExpected =
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800901 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 ||
902 mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800903 if (expensiveRenderingExpected) {
904 setExpensiveRenderingExpected(true);
905 }
906
Vishnu Nair9b079a22020-01-21 14:36:08 -0800907 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
908 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
909 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
910 std::back_inserter(clientCompositionLayerPointers),
911 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
912 return &settings;
913 });
914
Alec Mourie4034bb2019-11-19 12:45:54 -0800915 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800916 status_t status =
917 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers,
918 buf->getNativeBuffer(), /*useFramebufferCache=*/true,
919 std::move(fd), &readyFence);
920
921 if (status != NO_ERROR && mClientCompositionRequestCache) {
922 // If rendering was not successful, remove the request from the cache.
923 mClientCompositionRequestCache->remove(buf->getId());
924 }
925
Alec Mourie4034bb2019-11-19 12:45:54 -0800926 auto& timeStats = getCompositionEngine().getTimeStats();
927 if (readyFence.get() < 0) {
928 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
929 } else {
930 timeStats.recordRenderEngineDuration(renderEngineStart,
931 std::make_shared<FenceTime>(
932 new Fence(dup(readyFence.get()))));
933 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800934
Lloyd Piqued3d69882019-02-28 16:03:46 -0800935 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800936}
937
Vishnu Nair9b079a22020-01-21 14:36:08 -0800938std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800939 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800940 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -0800941 ALOGV("Rendering client layers");
942
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700943 const auto& outputState = getState();
944 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800945 const bool useIdentityTransform = false;
946 bool firstLayer = true;
947 // Used when a layer clears part of the buffer.
948 Region dummyRegion;
949
Lloyd Pique01c77c12019-04-17 12:48:32 -0700950 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800951 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700952 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800953 auto& layerFE = layer->getLayerFE();
954
Lloyd Piquea2468662019-03-07 21:31:06 -0800955 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800956 ALOGV("Layer: %s", layerFE.getDebugName());
957 if (clip.isEmpty()) {
958 ALOGV(" Skipping for empty clip");
959 firstLayer = false;
960 continue;
961 }
962
Vishnu Naira483b4a2019-12-12 15:07:52 -0800963 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800964
965 // We clear the client target for non-client composed layers if
966 // requested by the HWC. We skip this if the layer is not an opaque
967 // rectangle, as by definition the layer must blend with whatever is
968 // underneath. We also skip the first layer as the buffer target is
969 // guaranteed to start out cleared.
970 bool clearClientComposition =
971 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
972
973 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
974
975 if (clientComposition || clearClientComposition) {
976 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
977 clip,
978 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700979 layer->needsFiltering() || outputState.needsFiltering,
980 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800981 supportsProtectedContent,
982 clientComposition ? clearRegion : dummyRegion,
983 };
Vishnu Nair9b079a22020-01-21 14:36:08 -0800984 if (std::optional<LayerFE::LayerSettings> result =
985 layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700986 if (!clientComposition) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800987 LayerFE::LayerSettings& layerSettings = *result;
Lloyd Pique688abd42019-02-15 15:42:24 -0800988 layerSettings.source.buffer.buffer = nullptr;
989 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
990 layerSettings.alpha = half(0.0);
991 layerSettings.disableBlending = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800992 layerSettings.frameNumber = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800993 } else {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800994 std::optional<LayerFE::LayerSettings> shadowLayer =
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800995 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
996 outputDataspace);
997 if (shadowLayer) {
998 clientCompositionLayers.push_back(*shadowLayer);
999 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001000 }
1001
Vishnu Naira483b4a2019-12-12 15:07:52 -08001002 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1003 // composing the non-shadow content and only draw the shadows.
1004 const bool skipNonShadowContentComposition = clientComposition &&
1005 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1006
1007 if (!skipNonShadowContentComposition) {
1008 layer->editState().clientCompositionTimestamp = systemTime();
1009 clientCompositionLayers.push_back(*result);
1010 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001011 }
1012 }
1013
1014 firstLayer = false;
1015 }
1016
1017 return clientCompositionLayers;
1018}
1019
1020void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001021 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001022 if (flashRegion.isEmpty()) {
1023 return;
1024 }
1025
Vishnu Nair9b079a22020-01-21 14:36:08 -08001026 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001027 layerSettings.source.buffer.buffer = nullptr;
1028 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1029 layerSettings.alpha = half(1.0);
1030
1031 for (const auto& rect : flashRegion) {
1032 layerSettings.geometry.boundaries = rect.toFloatRect();
1033 clientCompositionLayers.push_back(layerSettings);
1034 }
1035}
1036
1037void Output::setExpensiveRenderingExpected(bool) {
1038 // The base class does nothing with this call.
1039}
1040
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001041void Output::postFramebuffer() {
1042 ATRACE_CALL();
1043 ALOGV(__FUNCTION__);
1044
1045 if (!getState().isEnabled) {
1046 return;
1047 }
1048
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001049 auto& outputState = editState();
1050 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001051 mRenderSurface->flip();
1052
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001053 auto frame = presentAndGetFrameFences();
1054
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001055 mRenderSurface->onPresentDisplayCompleted();
1056
Lloyd Pique01c77c12019-04-17 12:48:32 -07001057 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001058 // The layer buffer from the previous frame (if any) is released
1059 // by HWC only when the release fence from this frame (if any) is
1060 // signaled. Always get the release fence from HWC first.
1061 sp<Fence> releaseFence = Fence::NO_FENCE;
1062
1063 if (auto hwcLayer = layer->getHwcLayer()) {
1064 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1065 releaseFence = f->second;
1066 }
1067 }
1068
1069 // If the layer was client composited in the previous frame, we
1070 // need to merge with the previous client target acquire fence.
1071 // Since we do not track that, always merge with the current
1072 // client target acquire fence when it is available, even though
1073 // this is suboptimal.
1074 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001075 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001076 releaseFence =
1077 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1078 }
1079
1080 layer->getLayerFE().onLayerDisplayed(releaseFence);
1081 }
1082
1083 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001084 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001085 // supply them with the present fence.
1086 for (auto& weakLayer : mReleasedLayers) {
1087 if (auto layer = weakLayer.promote(); layer != nullptr) {
1088 layer->onLayerDisplayed(frame.presentFence);
1089 }
1090 }
1091
1092 // Clear out the released layers now that we're done with them.
1093 mReleasedLayers.clear();
1094}
1095
Lloyd Pique32cbe282018-10-19 13:09:22 -07001096void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001097 auto& outputState = editState();
1098 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001099}
1100
Lloyd Pique66d68602019-02-13 14:23:31 -08001101void Output::chooseCompositionStrategy() {
1102 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001103 auto& outputState = editState();
1104 outputState.usesClientComposition = true;
1105 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001106 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001107}
1108
Lloyd Pique688abd42019-02-15 15:42:24 -08001109bool Output::getSkipColorTransform() const {
1110 return true;
1111}
1112
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001113compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1114 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001115 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001116 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1117 }
1118 return result;
1119}
1120
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001121} // namespace impl
1122} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001123
1124// TODO(b/129481165): remove the #pragma below and fix conversion issues
1125#pragma clang diagnostic pop // ignored "-Wconversion"