blob: 47704ecd95091e8285984b953cfcf286f6f522d2 [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 Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070024#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080030
31// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
34
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include <renderengine/DisplaySettings.h>
36#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic pop // ignored "-Wconversion"
40
Lloyd Pique32cbe282018-10-19 13:09:22 -070041#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080042#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080043#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070044
Lloyd Pique688abd42019-02-15 15:42:24 -080045#include "TracedOrdinal.h"
46
Lloyd Piquefeb73d72018-12-04 17:23:44 -080047namespace android::compositionengine {
48
49Output::~Output() = default;
50
51namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070052
Lloyd Piquec29e4c62019-03-07 21:48:19 -080053namespace {
54
55template <typename T>
56class Reversed {
57public:
58 explicit Reversed(const T& container) : mContainer(container) {}
59 auto begin() { return mContainer.rbegin(); }
60 auto end() { return mContainer.rend(); }
61
62private:
63 const T& mContainer;
64};
65
66// Helper for enumerating over a container in reverse order
67template <typename T>
68Reversed<T> reversed(const T& c) {
69 return Reversed<T>(c);
70}
71
72} // namespace
73
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070074std::shared_ptr<Output> createOutput(
75 const compositionengine::CompositionEngine& compositionEngine) {
76 return createOutputTemplated<Output>(compositionEngine);
77}
Lloyd Pique32cbe282018-10-19 13:09:22 -070078
79Output::~Output() = default;
80
Lloyd Pique32cbe282018-10-19 13:09:22 -070081bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070082 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
83 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070084}
85
Lloyd Pique6c564cf2019-05-17 17:31:36 -070086std::optional<DisplayId> Output::getDisplayId() const {
87 return {};
88}
89
Lloyd Pique32cbe282018-10-19 13:09:22 -070090const std::string& Output::getName() const {
91 return mName;
92}
93
94void Output::setName(const std::string& name) {
95 mName = name;
96}
97
98void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070099 auto& outputState = editState();
100 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101 return;
102 }
103
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700104 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105 dirtyEntireOutput();
106}
107
Lloyd Pique0a456232020-01-16 17:51:13 -0800108void Output::setProjection(const ui::Transform& transform, uint32_t orientation, const Rect& frame,
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700110 auto& outputState = editState();
111 outputState.transform = transform;
112 outputState.orientation = orientation;
113 outputState.scissor = scissor;
114 outputState.frame = frame;
115 outputState.viewport = viewport;
116 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700117
118 dirtyEntireOutput();
119}
120
Lloyd Pique688abd42019-02-15 15:42:24 -0800121// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700122void Output::setBounds(const ui::Size& size) {
123 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700124 // TODO(b/121291683): Rename outputState.size once more is moved.
125 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700126
127 dirtyEntireOutput();
128}
129
Lloyd Piqueef36b002019-01-23 17:52:04 -0800130void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131 auto& outputState = editState();
132 outputState.layerStackId = layerStackId;
133 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700134
135 dirtyEntireOutput();
136}
137
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800138void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700139 auto& colorTransformMatrix = editState().colorTransformMatrix;
140 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700141 return;
142 }
143
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700144 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800145
146 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700147}
148
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800149void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700150 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800151 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
152 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800153
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700154 auto& outputState = editState();
155 if (outputState.colorMode == colorProfile.mode &&
156 outputState.dataspace == colorProfile.dataspace &&
157 outputState.renderIntent == colorProfile.renderIntent &&
158 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800159 return;
160 }
161
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700162 outputState.colorMode = colorProfile.mode;
163 outputState.dataspace = colorProfile.dataspace;
164 outputState.renderIntent = colorProfile.renderIntent;
165 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700166
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800167 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700168
Lloyd Pique32cbe282018-10-19 13:09:22 -0700169 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800170 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
171 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800172
173 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700174}
175
176void Output::dump(std::string& out) const {
177 using android::base::StringAppendF;
178
179 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
180
181 out.append("\n ");
182
183 dumpBase(out);
184}
185
186void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700187 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700188
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700189 if (mDisplayColorProfile) {
190 mDisplayColorProfile->dump(out);
191 } else {
192 out.append(" No display color profile!\n");
193 }
194
Lloyd Pique31cb2942018-10-19 17:23:03 -0700195 if (mRenderSurface) {
196 mRenderSurface->dump(out);
197 } else {
198 out.append(" No render surface!\n");
199 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800200
Lloyd Pique01c77c12019-04-17 12:48:32 -0700201 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
202 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800203 if (!outputLayer) {
204 continue;
205 }
206 outputLayer->dump(out);
207 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700208}
209
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700210compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
211 return mDisplayColorProfile.get();
212}
213
214void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
215 mDisplayColorProfile = std::move(mode);
216}
217
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800218const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
219 return mReleasedLayers;
220}
221
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700222void Output::setDisplayColorProfileForTest(
223 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
224 mDisplayColorProfile = std::move(mode);
225}
226
Lloyd Pique31cb2942018-10-19 17:23:03 -0700227compositionengine::RenderSurface* Output::getRenderSurface() const {
228 return mRenderSurface.get();
229}
230
231void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
232 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700233 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700234
235 dirtyEntireOutput();
236}
237
Vishnu Nair9b079a22020-01-21 14:36:08 -0800238void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
239 if (cacheSize == 0) {
240 mClientCompositionRequestCache.reset();
241 } else {
242 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
243 }
244};
245
Lloyd Pique31cb2942018-10-19 17:23:03 -0700246void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
247 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700248}
249
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000250Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 const auto& outputState = getState();
252 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000253 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700254 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255 }
256 return dirty;
257}
258
Lloyd Piquec6687342019-03-07 21:34:57 -0800259bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800260 // The layerStackId's must match, and also the layer must not be internal
261 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700262 const auto& outputState = getState();
263 return layerStackId && (*layerStackId == outputState.layerStackId) &&
264 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700265}
266
Lloyd Piquede196652020-01-22 17:29:58 -0800267bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
268 const auto* layerFEState = layerFE->getCompositionState();
269 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800270}
271
Lloyd Piquedf336d92019-03-07 21:38:42 -0800272std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800273 const sp<LayerFE>& layerFE) const {
274 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800275}
276
Lloyd Piquede196652020-01-22 17:29:58 -0800277compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
278 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700279 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800280}
281
Lloyd Pique01c77c12019-04-17 12:48:32 -0700282std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800283 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700284 for (size_t i = 0; i < getOutputLayerCount(); i++) {
285 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800286 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700287 return i;
288 }
289 }
290 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800291}
292
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800293void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
294 mReleasedLayers = std::move(layers);
295}
296
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800297void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
298 LayerFESet& geomSnapshots) {
299 ATRACE_CALL();
300 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800301
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800302 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800303}
304
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800305void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800306 ATRACE_CALL();
307 ALOGV(__FUNCTION__);
308
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800309 updateColorProfile(refreshArgs);
310 updateAndWriteCompositionState(refreshArgs);
311 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800312 beginFrame();
313 prepareFrame();
314 devOptRepaintFlash(refreshArgs);
315 finishFrame(refreshArgs);
316 postFramebuffer();
317}
318
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800319void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
320 LayerFESet& layerFESet) {
321 ATRACE_CALL();
322 ALOGV(__FUNCTION__);
323
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700324 auto& outputState = editState();
325
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800326 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700327 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800328 return;
329 }
330
331 // Process the layers to determine visibility and coverage
332 compositionengine::Output::CoverageState coverage{layerFESet};
333 collectVisibleLayers(refreshArgs, coverage);
334
335 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700336 const ui::Transform& tr = outputState.transform;
337 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800338 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
339
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700340 outputState.undefinedRegion = undefinedRegion;
341 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800342}
343
344void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
345 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800346 // Evaluate the layers from front to back to determine what is visible. This
347 // also incrementally calculates the coverage information for each layer as
348 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800349 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700350 // Incrementally process the coverage for each layer
351 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800352
353 // TODO(b/121291683): Stop early if the output is completely covered and
354 // no more layers could even be visible underneath the ones on top.
355 }
356
Lloyd Pique01c77c12019-04-17 12:48:32 -0700357 setReleasedLayers(refreshArgs);
358
359 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800360
361 // Generate a simple Z-order values to each visible output layer
362 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700363 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800364 outputLayer->editState().z = zOrder++;
365 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800366}
367
Lloyd Piquede196652020-01-22 17:29:58 -0800368void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700369 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800370 // Ensure we have a snapshot of the basic geometry layer state. Limit the
371 // snapshots to once per frame for each candidate layer, as layers may
372 // appear on multiple outputs.
373 if (!coverage.latchedLayers.count(layerFE)) {
374 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800375 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800376 }
377
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800378 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800379 if (!belongsInOutput(layerFE)) {
380 return;
381 }
382
383 // Obtain a read-only pointer to the front-end layer state
384 const auto* layerFEState = layerFE->getCompositionState();
385 if (CC_UNLIKELY(!layerFEState)) {
386 return;
387 }
388
389 // handle hidden surfaces by setting the visible region to empty
390 if (CC_UNLIKELY(!layerFEState->isVisible)) {
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 Piquede196652020-01-22 17:29:58 -0800428 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800429
430 // Get the visible region
431 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
432 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800433 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800434 visibleRegion.set(visibleRect);
435
Lloyd Piquede196652020-01-22 17:29:58 -0800436 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800437 // if the layer casts a shadow, offset the layers visible region and
438 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800439 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800440 Rect visibleRectWithShadows(visibleRect);
441 visibleRectWithShadows.inset(inset, inset, inset, inset);
442 visibleRegion.set(visibleRectWithShadows);
443 shadowRegion = visibleRegion.subtract(visibleRect);
444 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800445
446 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700447 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800448 }
449
450 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800451 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800452 if (tr.preserveRects()) {
453 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800454 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800455 } else {
456 // transformation too complex, can't do the
457 // transparent region optimization.
458 transparentRegion.clear();
459 }
460 }
461
462 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800463 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800464 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465 // If we one of the simple category of transforms (0/90/180/270 rotation
466 // + any flip), then the opaque region is the layer's footprint.
467 // Otherwise we don't try and compute the opaque region since there may
468 // be errors at the edges, and we treat the entire layer as
469 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800470 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800471 }
472
473 // Clip the covered region to the visible region
474 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
475
476 // Update accumAboveCoveredLayers for next (lower) layer
477 coverage.aboveCoveredLayers.orSelf(visibleRegion);
478
479 // subtract the opaque region covered by the layers above us
480 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
481
482 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700483 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800484 }
485
486 // Get coverage information for the layer as previously displayed,
487 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800488 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700489 auto prevOutputLayer =
490 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800491
492 // Get coverage information for the layer as previously displayed
493 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
494 const Region kEmptyRegion;
495 const Region& oldVisibleRegion =
496 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
497 const Region& oldCoveredRegion =
498 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
499
500 // compute this layer's dirty region
501 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800502 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800503 // we need to invalidate the whole region
504 dirty = visibleRegion;
505 // as well, as the old visible region
506 dirty.orSelf(oldVisibleRegion);
507 } else {
508 /* compute the exposed region:
509 * the exposed region consists of two components:
510 * 1) what's VISIBLE now and was COVERED before
511 * 2) what's EXPOSED now less what was EXPOSED before
512 *
513 * note that (1) is conservative, we start with the whole visible region
514 * but only keep what used to be covered by something -- which mean it
515 * may have been exposed.
516 *
517 * (2) handles areas that were not covered by anything but got exposed
518 * because of a resize.
519 *
520 */
521 const Region newExposed = visibleRegion - coveredRegion;
522 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
523 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
524 }
525 dirty.subtractSelf(coverage.aboveOpaqueLayers);
526
527 // accumulate to the screen dirty region
528 coverage.dirtyRegion.orSelf(dirty);
529
530 // Update accumAboveOpaqueLayers for next (lower) layer
531 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
532
533 // Compute the visible non-transparent region
534 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
535
Vishnu Naira483b4a2019-12-12 15:07:52 -0800536 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800537 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700538 const auto& outputState = getState();
539 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
540 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800541 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700542 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543 }
544
Vishnu Naira483b4a2019-12-12 15:07:52 -0800545 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
546
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800547 // The layer is visible. Either reuse the existing outputLayer if we have
548 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800549 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800550
551 // Store the layer coverage information into the layer state as some of it
552 // is useful later.
553 auto& outputLayerState = result->editState();
554 outputLayerState.visibleRegion = visibleRegion;
555 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
556 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800557 outputLayerState.outputSpaceVisibleRegion =
558 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
559 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560}
561
562void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
563 // The base class does nothing with this call.
564}
565
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800566void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700567 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800568 layer->getLayerFE().prepareCompositionState(
569 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
570 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800571 }
572}
573
574void Output::updateAndWriteCompositionState(
575 const compositionengine::CompositionRefreshArgs& refreshArgs) {
576 ATRACE_CALL();
577 ALOGV(__FUNCTION__);
578
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800579 if (!getState().isEnabled) {
580 return;
581 }
582
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800583 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
584 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
585
Lloyd Pique01c77c12019-04-17 12:48:32 -0700586 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700587 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800588 refreshArgs.devOptForceClientComposition ||
589 forceClientComposition);
590
591 if (mLayerRequestingBackgroundBlur == layer) {
592 forceClientComposition = false;
593 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800594
595 // Send the updated state to the HWC, if appropriate.
596 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
597 }
598}
599
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800600compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
601 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
602 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800603 if (layer->getLayerFE().getCompositionState()->backgroundBlurRadius > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800604 layerRequestingBgComposition = layer;
605 }
606 }
607 return layerRequestingBgComposition;
608}
609
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800610void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
611 setColorProfile(pickColorProfile(refreshArgs));
612}
613
614// Returns a data space that fits all visible layers. The returned data space
615// can only be one of
616// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
617// - Dataspace::DISPLAY_P3
618// - Dataspace::DISPLAY_BT2020
619// The returned HDR data space is one of
620// - Dataspace::UNKNOWN
621// - Dataspace::BT2020_HLG
622// - Dataspace::BT2020_PQ
623ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
624 bool* outIsHdrClientComposition) const {
625 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
626 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
627
Lloyd Pique01c77c12019-04-17 12:48:32 -0700628 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800629 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800630 case ui::Dataspace::V0_SCRGB:
631 case ui::Dataspace::V0_SCRGB_LINEAR:
632 case ui::Dataspace::BT2020:
633 case ui::Dataspace::BT2020_ITU:
634 case ui::Dataspace::BT2020_LINEAR:
635 case ui::Dataspace::DISPLAY_BT2020:
636 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
637 break;
638 case ui::Dataspace::DISPLAY_P3:
639 bestDataSpace = ui::Dataspace::DISPLAY_P3;
640 break;
641 case ui::Dataspace::BT2020_PQ:
642 case ui::Dataspace::BT2020_ITU_PQ:
643 bestDataSpace = ui::Dataspace::DISPLAY_P3;
644 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800645 *outIsHdrClientComposition =
646 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800647 break;
648 case ui::Dataspace::BT2020_HLG:
649 case ui::Dataspace::BT2020_ITU_HLG:
650 bestDataSpace = ui::Dataspace::DISPLAY_P3;
651 // When there's mixed PQ content and HLG content, we set the HDR
652 // data space to be BT2020_PQ and convert HLG to PQ.
653 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
654 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
655 }
656 break;
657 default:
658 break;
659 }
660 }
661
662 return bestDataSpace;
663}
664
665compositionengine::Output::ColorProfile Output::pickColorProfile(
666 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
667 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
668 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
669 ui::RenderIntent::COLORIMETRIC,
670 refreshArgs.colorSpaceAgnosticDataspace};
671 }
672
673 ui::Dataspace hdrDataSpace;
674 bool isHdrClientComposition = false;
675 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
676
677 switch (refreshArgs.forceOutputColorMode) {
678 case ui::ColorMode::SRGB:
679 bestDataSpace = ui::Dataspace::V0_SRGB;
680 break;
681 case ui::ColorMode::DISPLAY_P3:
682 bestDataSpace = ui::Dataspace::DISPLAY_P3;
683 break;
684 default:
685 break;
686 }
687
688 // respect hdrDataSpace only when there is no legacy HDR support
689 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
690 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
691 if (isHdr) {
692 bestDataSpace = hdrDataSpace;
693 }
694
695 ui::RenderIntent intent;
696 switch (refreshArgs.outputColorSetting) {
697 case OutputColorSetting::kManaged:
698 case OutputColorSetting::kUnmanaged:
699 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
700 : ui::RenderIntent::COLORIMETRIC;
701 break;
702 case OutputColorSetting::kEnhanced:
703 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
704 break;
705 default: // vendor display color setting
706 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
707 break;
708 }
709
710 ui::ColorMode outMode;
711 ui::Dataspace outDataSpace;
712 ui::RenderIntent outRenderIntent;
713 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
714 &outRenderIntent);
715
716 return ColorProfile{outMode, outDataSpace, outRenderIntent,
717 refreshArgs.colorSpaceAgnosticDataspace};
718}
719
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800720void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700721 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800722 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700723 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700724 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800725
726 // If nothing has changed (!dirty), don't recompose.
727 // If something changed, but we don't currently have any visible layers,
728 // and didn't when we last did a composition, then skip it this time.
729 // The second rule does two things:
730 // - When all layers are removed from a display, we'll emit one black
731 // frame, then nothing more until we get new layers.
732 // - When a display is created with a private layer stack, we won't
733 // emit any black frames until a layer is added to the layer stack.
734 const bool mustRecompose = dirty && !(empty && wasEmpty);
735
736 const char flagPrefix[] = {'-', '+'};
737 static_cast<void>(flagPrefix);
738 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
739 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
740 flagPrefix[empty], flagPrefix[wasEmpty]);
741
742 mRenderSurface->beginFrame(mustRecompose);
743
744 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700745 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800746 }
747}
748
Lloyd Pique66d68602019-02-13 14:23:31 -0800749void Output::prepareFrame() {
750 ATRACE_CALL();
751 ALOGV(__FUNCTION__);
752
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700753 const auto& outputState = getState();
754 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800755 return;
756 }
757
758 chooseCompositionStrategy();
759
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700760 mRenderSurface->prepareFrame(outputState.usesClientComposition,
761 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800762}
763
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800764void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
765 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
766 return;
767 }
768
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700769 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800770 // transform the dirty region into this screen's coordinate space
771 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
772 if (!dirtyRegion.isEmpty()) {
773 base::unique_fd readyFence;
774 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800775 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800776
777 mRenderSurface->queueBuffer(std::move(readyFence));
778 }
779 }
780
781 postFramebuffer();
782
783 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
784
785 prepareFrame();
786}
787
Lloyd Piqued3d69882019-02-28 16:03:46 -0800788void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
789 ATRACE_CALL();
790 ALOGV(__FUNCTION__);
791
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700792 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800793 return;
794 }
795
796 // Repaint the framebuffer (if needed), getting the optional fence for when
797 // the composition completes.
798 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
799 if (!optReadyFence) {
800 return;
801 }
802
803 // swap buffers (presentation)
804 mRenderSurface->queueBuffer(std::move(*optReadyFence));
805}
806
807std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800808 ATRACE_CALL();
809 ALOGV(__FUNCTION__);
810
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700811 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800812 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800813 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700814 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800815 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800816 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800817 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800818 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800819 }
820
821 ALOGV("hasClientComposition");
822
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700823 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800824 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
825
826 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700827 clientCompositionDisplay.physicalDisplay = outputState.scissor;
828 clientCompositionDisplay.clip = outputState.scissor;
829 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
830 clientCompositionDisplay.orientation = outputState.orientation;
831 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
832 ? outputState.dataspace
833 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800834 clientCompositionDisplay.maxLuminance =
835 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
836
837 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700838 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
839 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800840 }
841
842 // Note: Updated by generateClientCompositionRequests
843 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
844
845 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800846 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800847 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800848 clientCompositionDisplay.clearRegion,
849 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800850 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
851
852 // If we the display is secure, protected content support is enabled, and at
853 // least one layer has protected content, we need to use a secure back
854 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700855 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700856 auto layers = getOutputLayersOrderedByZ();
857 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
Lloyd Piquede196652020-01-22 17:29:58 -0800858 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700859 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800860 if (needsProtected != renderEngine.isProtected()) {
861 renderEngine.useProtectedContext(needsProtected);
862 }
863 if (needsProtected != mRenderSurface->isProtected() &&
864 needsProtected == renderEngine.isProtected()) {
865 mRenderSurface->setProtected(needsProtected);
866 }
867 }
868
869 base::unique_fd fd;
870 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
871 if (buf == nullptr) {
872 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
873 "client composition for this frame",
874 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800875 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800876 }
877
Vishnu Nair9b079a22020-01-21 14:36:08 -0800878 // Check if the client composition requests were rendered into the provided graphic buffer. If
879 // so, we can reuse the buffer and avoid client composition.
880 if (mClientCompositionRequestCache) {
881 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
882 clientCompositionLayers)) {
883 outputCompositionState.reusedClientComposition = true;
884 setExpensiveRenderingExpected(false);
885 return readyFence;
886 }
887 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
888 clientCompositionLayers);
889 }
890
Lloyd Pique688abd42019-02-15 15:42:24 -0800891 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800892 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
893 // GPU composition can finish in time. We must reset GPU frequency afterwards,
894 // because high frequency consumes extra battery.
Lloyd Pique688abd42019-02-15 15:42:24 -0800895 const bool expensiveRenderingExpected =
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800896 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 ||
897 mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800898 if (expensiveRenderingExpected) {
899 setExpensiveRenderingExpected(true);
900 }
901
Vishnu Nair9b079a22020-01-21 14:36:08 -0800902 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
903 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
904 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
905 std::back_inserter(clientCompositionLayerPointers),
906 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
907 return &settings;
908 });
909
Alec Mourie4034bb2019-11-19 12:45:54 -0800910 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800911 status_t status =
912 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers,
913 buf->getNativeBuffer(), /*useFramebufferCache=*/true,
914 std::move(fd), &readyFence);
915
916 if (status != NO_ERROR && mClientCompositionRequestCache) {
917 // If rendering was not successful, remove the request from the cache.
918 mClientCompositionRequestCache->remove(buf->getId());
919 }
920
Alec Mourie4034bb2019-11-19 12:45:54 -0800921 auto& timeStats = getCompositionEngine().getTimeStats();
922 if (readyFence.get() < 0) {
923 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
924 } else {
925 timeStats.recordRenderEngineDuration(renderEngineStart,
926 std::make_shared<FenceTime>(
927 new Fence(dup(readyFence.get()))));
928 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800929
Lloyd Piqued3d69882019-02-28 16:03:46 -0800930 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800931}
932
Vishnu Nair9b079a22020-01-21 14:36:08 -0800933std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800934 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800935 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -0800936 ALOGV("Rendering client layers");
937
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700938 const auto& outputState = getState();
939 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800940 const bool useIdentityTransform = false;
941 bool firstLayer = true;
942 // Used when a layer clears part of the buffer.
943 Region dummyRegion;
944
Lloyd Pique01c77c12019-04-17 12:48:32 -0700945 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800946 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -0800947 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800948 auto& layerFE = layer->getLayerFE();
949
Lloyd Piquea2468662019-03-07 21:31:06 -0800950 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800951 ALOGV("Layer: %s", layerFE.getDebugName());
952 if (clip.isEmpty()) {
953 ALOGV(" Skipping for empty clip");
954 firstLayer = false;
955 continue;
956 }
957
Vishnu Naira483b4a2019-12-12 15:07:52 -0800958 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800959
960 // We clear the client target for non-client composed layers if
961 // requested by the HWC. We skip this if the layer is not an opaque
962 // rectangle, as by definition the layer must blend with whatever is
963 // underneath. We also skip the first layer as the buffer target is
964 // guaranteed to start out cleared.
965 bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -0800966 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -0800967
968 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
969
970 if (clientComposition || clearClientComposition) {
971 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
972 clip,
973 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700974 layer->needsFiltering() || outputState.needsFiltering,
975 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800976 supportsProtectedContent,
977 clientComposition ? clearRegion : dummyRegion,
978 };
Vishnu Nair9b079a22020-01-21 14:36:08 -0800979 if (std::optional<LayerFE::LayerSettings> result =
980 layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700981 if (!clientComposition) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800982 LayerFE::LayerSettings& layerSettings = *result;
Lloyd Pique688abd42019-02-15 15:42:24 -0800983 layerSettings.source.buffer.buffer = nullptr;
984 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
985 layerSettings.alpha = half(0.0);
986 layerSettings.disableBlending = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800987 layerSettings.frameNumber = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800988 } else {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800989 std::optional<LayerFE::LayerSettings> shadowLayer =
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800990 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
991 outputDataspace);
992 if (shadowLayer) {
993 clientCompositionLayers.push_back(*shadowLayer);
994 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800995 }
996
Vishnu Naira483b4a2019-12-12 15:07:52 -0800997 // If the layer casts a shadow but the content casting the shadow is occluded, skip
998 // composing the non-shadow content and only draw the shadows.
999 const bool skipNonShadowContentComposition = clientComposition &&
1000 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1001
1002 if (!skipNonShadowContentComposition) {
1003 layer->editState().clientCompositionTimestamp = systemTime();
1004 clientCompositionLayers.push_back(*result);
1005 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001006 }
1007 }
1008
1009 firstLayer = false;
1010 }
1011
1012 return clientCompositionLayers;
1013}
1014
1015void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001016 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001017 if (flashRegion.isEmpty()) {
1018 return;
1019 }
1020
Vishnu Nair9b079a22020-01-21 14:36:08 -08001021 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001022 layerSettings.source.buffer.buffer = nullptr;
1023 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1024 layerSettings.alpha = half(1.0);
1025
1026 for (const auto& rect : flashRegion) {
1027 layerSettings.geometry.boundaries = rect.toFloatRect();
1028 clientCompositionLayers.push_back(layerSettings);
1029 }
1030}
1031
1032void Output::setExpensiveRenderingExpected(bool) {
1033 // The base class does nothing with this call.
1034}
1035
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001036void Output::postFramebuffer() {
1037 ATRACE_CALL();
1038 ALOGV(__FUNCTION__);
1039
1040 if (!getState().isEnabled) {
1041 return;
1042 }
1043
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001044 auto& outputState = editState();
1045 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001046 mRenderSurface->flip();
1047
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001048 auto frame = presentAndGetFrameFences();
1049
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001050 mRenderSurface->onPresentDisplayCompleted();
1051
Lloyd Pique01c77c12019-04-17 12:48:32 -07001052 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001053 // The layer buffer from the previous frame (if any) is released
1054 // by HWC only when the release fence from this frame (if any) is
1055 // signaled. Always get the release fence from HWC first.
1056 sp<Fence> releaseFence = Fence::NO_FENCE;
1057
1058 if (auto hwcLayer = layer->getHwcLayer()) {
1059 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1060 releaseFence = f->second;
1061 }
1062 }
1063
1064 // If the layer was client composited in the previous frame, we
1065 // need to merge with the previous client target acquire fence.
1066 // Since we do not track that, always merge with the current
1067 // client target acquire fence when it is available, even though
1068 // this is suboptimal.
1069 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001070 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001071 releaseFence =
1072 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1073 }
1074
1075 layer->getLayerFE().onLayerDisplayed(releaseFence);
1076 }
1077
1078 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001079 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001080 // supply them with the present fence.
1081 for (auto& weakLayer : mReleasedLayers) {
1082 if (auto layer = weakLayer.promote(); layer != nullptr) {
1083 layer->onLayerDisplayed(frame.presentFence);
1084 }
1085 }
1086
1087 // Clear out the released layers now that we're done with them.
1088 mReleasedLayers.clear();
1089}
1090
Lloyd Pique32cbe282018-10-19 13:09:22 -07001091void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001092 auto& outputState = editState();
1093 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001094}
1095
Lloyd Pique66d68602019-02-13 14:23:31 -08001096void Output::chooseCompositionStrategy() {
1097 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001098 auto& outputState = editState();
1099 outputState.usesClientComposition = true;
1100 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001101 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001102}
1103
Lloyd Pique688abd42019-02-15 15:42:24 -08001104bool Output::getSkipColorTransform() const {
1105 return true;
1106}
1107
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001108compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1109 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001110 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001111 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1112 }
1113 return result;
1114}
1115
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001116} // namespace impl
1117} // namespace android::compositionengine