blob: ce4b84a4545d1441086791ad6ac574420ffa395c [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080017#include <thread>
18
Lloyd Pique32cbe282018-10-19 13:09:22 -070019#include <android-base/stringprintf.h>
20#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080023#include <compositionengine/Layer.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080024#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070025#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070026#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070027#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070028#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080029#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070030#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080031
32// TODO(b/129481165): remove the #pragma below and fix conversion issues
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wconversion"
35
Lloyd Pique688abd42019-02-15 15:42:24 -080036#include <renderengine/DisplaySettings.h>
37#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080038
39// TODO(b/129481165): remove the #pragma below and fix conversion issues
40#pragma clang diagnostic pop // ignored "-Wconversion"
41
Lloyd Pique32cbe282018-10-19 13:09:22 -070042#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080043#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080044#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070045
Lloyd Pique688abd42019-02-15 15:42:24 -080046#include "TracedOrdinal.h"
47
Lloyd Piquefeb73d72018-12-04 17:23:44 -080048namespace android::compositionengine {
49
50Output::~Output() = default;
51
52namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070053
Lloyd Piquec29e4c62019-03-07 21:48:19 -080054namespace {
55
56template <typename T>
57class Reversed {
58public:
59 explicit Reversed(const T& container) : mContainer(container) {}
60 auto begin() { return mContainer.rbegin(); }
61 auto end() { return mContainer.rend(); }
62
63private:
64 const T& mContainer;
65};
66
67// Helper for enumerating over a container in reverse order
68template <typename T>
69Reversed<T> reversed(const T& c) {
70 return Reversed<T>(c);
71}
72
73} // namespace
74
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070075std::shared_ptr<Output> createOutput(
76 const compositionengine::CompositionEngine& compositionEngine) {
77 return createOutputTemplated<Output>(compositionEngine);
78}
Lloyd Pique32cbe282018-10-19 13:09:22 -070079
80Output::~Output() = default;
81
Lloyd Pique32cbe282018-10-19 13:09:22 -070082bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070083 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
84 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070085}
86
Lloyd Pique6c564cf2019-05-17 17:31:36 -070087std::optional<DisplayId> Output::getDisplayId() const {
88 return {};
89}
90
Lloyd Pique32cbe282018-10-19 13:09:22 -070091const std::string& Output::getName() const {
92 return mName;
93}
94
95void Output::setName(const std::string& name) {
96 mName = name;
97}
98
99void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700100 auto& outputState = editState();
101 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102 return;
103 }
104
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700105 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700106 dirtyEntireOutput();
107}
108
Lloyd Pique0a456232020-01-16 17:51:13 -0800109void Output::setProjection(const ui::Transform& transform, uint32_t orientation, const Rect& frame,
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800110 const Rect& viewport, const Rect& sourceClip,
111 const Rect& destinationClip, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700112 auto& outputState = editState();
113 outputState.transform = transform;
114 outputState.orientation = orientation;
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800115 outputState.sourceClip = sourceClip;
116 outputState.destinationClip = destinationClip;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700117 outputState.frame = frame;
118 outputState.viewport = viewport;
119 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120
121 dirtyEntireOutput();
122}
123
Lloyd Pique688abd42019-02-15 15:42:24 -0800124// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700125void Output::setBounds(const ui::Size& size) {
126 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127 // TODO(b/121291683): Rename outputState.size once more is moved.
128 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700129
130 dirtyEntireOutput();
131}
132
Lloyd Piqueef36b002019-01-23 17:52:04 -0800133void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700134 auto& outputState = editState();
135 outputState.layerStackId = layerStackId;
136 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700137
138 dirtyEntireOutput();
139}
140
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800141void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700142 auto& colorTransformMatrix = editState().colorTransformMatrix;
143 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700144 return;
145 }
146
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700147 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800148
149 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700150}
151
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800152void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700153 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800154 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
155 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800156
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700157 auto& outputState = editState();
158 if (outputState.colorMode == colorProfile.mode &&
159 outputState.dataspace == colorProfile.dataspace &&
160 outputState.renderIntent == colorProfile.renderIntent &&
161 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800162 return;
163 }
164
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700165 outputState.colorMode = colorProfile.mode;
166 outputState.dataspace = colorProfile.dataspace;
167 outputState.renderIntent = colorProfile.renderIntent;
168 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700169
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800170 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700171
Lloyd Pique32cbe282018-10-19 13:09:22 -0700172 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800173 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
174 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800175
176 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700177}
178
179void Output::dump(std::string& out) const {
180 using android::base::StringAppendF;
181
182 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
183
184 out.append("\n ");
185
186 dumpBase(out);
187}
188
189void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700190 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700192 if (mDisplayColorProfile) {
193 mDisplayColorProfile->dump(out);
194 } else {
195 out.append(" No display color profile!\n");
196 }
197
Lloyd Pique31cb2942018-10-19 17:23:03 -0700198 if (mRenderSurface) {
199 mRenderSurface->dump(out);
200 } else {
201 out.append(" No render surface!\n");
202 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800203
Lloyd Pique01c77c12019-04-17 12:48:32 -0700204 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
205 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800206 if (!outputLayer) {
207 continue;
208 }
209 outputLayer->dump(out);
210 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700211}
212
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700213compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
214 return mDisplayColorProfile.get();
215}
216
217void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
218 mDisplayColorProfile = std::move(mode);
219}
220
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800221const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
222 return mReleasedLayers;
223}
224
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700225void Output::setDisplayColorProfileForTest(
226 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
227 mDisplayColorProfile = std::move(mode);
228}
229
Lloyd Pique31cb2942018-10-19 17:23:03 -0700230compositionengine::RenderSurface* Output::getRenderSurface() const {
231 return mRenderSurface.get();
232}
233
234void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
235 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700236 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700237
238 dirtyEntireOutput();
239}
240
Vishnu Nair9b079a22020-01-21 14:36:08 -0800241void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
242 if (cacheSize == 0) {
243 mClientCompositionRequestCache.reset();
244 } else {
245 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
246 }
247};
248
Lloyd Pique31cb2942018-10-19 17:23:03 -0700249void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
250 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700251}
252
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000253Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700254 const auto& outputState = getState();
255 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000256 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700257 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700258 }
259 return dirty;
260}
261
Lloyd Piquec6687342019-03-07 21:34:57 -0800262bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800263 // The layerStackId's must match, and also the layer must not be internal
264 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700265 const auto& outputState = getState();
266 return layerStackId && (*layerStackId == outputState.layerStackId) &&
267 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700268}
269
Lloyd Pique66c20c42019-03-07 21:44:02 -0800270bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
271 if (!layer) {
272 return false;
273 }
274
Lloyd Pique9755fb72019-03-26 14:44:40 -0700275 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800276 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
277}
278
Lloyd Piquedf336d92019-03-07 21:38:42 -0800279std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700280 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800281 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800282}
283
Lloyd Pique01c77c12019-04-17 12:48:32 -0700284compositionengine::OutputLayer* Output::getOutputLayerForLayer(
285 compositionengine::Layer* layer) const {
286 auto index = findCurrentOutputLayerForLayer(layer);
287 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800288}
289
Lloyd Pique01c77c12019-04-17 12:48:32 -0700290std::optional<size_t> Output::findCurrentOutputLayerForLayer(
291 compositionengine::Layer* layer) const {
292 for (size_t i = 0; i < getOutputLayerCount(); i++) {
293 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
294 if (outputLayer && &outputLayer->getLayer() == layer) {
295 return i;
296 }
297 }
298 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800299}
300
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800301void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
302 mReleasedLayers = std::move(layers);
303}
304
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800305void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
306 LayerFESet& geomSnapshots) {
307 ATRACE_CALL();
308 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800309
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800310 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800311}
312
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800313void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800314 ATRACE_CALL();
315 ALOGV(__FUNCTION__);
316
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800317 updateColorProfile(refreshArgs);
318 updateAndWriteCompositionState(refreshArgs);
319 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800320 beginFrame();
321 prepareFrame();
322 devOptRepaintFlash(refreshArgs);
323 finishFrame(refreshArgs);
324 postFramebuffer();
325}
326
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800327void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
328 LayerFESet& layerFESet) {
329 ATRACE_CALL();
330 ALOGV(__FUNCTION__);
331
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700332 auto& outputState = editState();
333
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800334 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700335 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800336 return;
337 }
338
339 // Process the layers to determine visibility and coverage
340 compositionengine::Output::CoverageState coverage{layerFESet};
341 collectVisibleLayers(refreshArgs, coverage);
342
343 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700344 const ui::Transform& tr = outputState.transform;
345 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800346 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
347
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700348 outputState.undefinedRegion = undefinedRegion;
349 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800350}
351
352void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
353 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800354 // Evaluate the layers from front to back to determine what is visible. This
355 // also incrementally calculates the coverage information for each layer as
356 // well as the entire output.
357 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700358 // Incrementally process the coverage for each layer
359 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800360
361 // TODO(b/121291683): Stop early if the output is completely covered and
362 // no more layers could even be visible underneath the ones on top.
363 }
364
Lloyd Pique01c77c12019-04-17 12:48:32 -0700365 setReleasedLayers(refreshArgs);
366
367 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800368
369 // Generate a simple Z-order values to each visible output layer
370 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700371 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800372 outputLayer->editState().z = zOrder++;
373 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800374}
375
Lloyd Pique01c77c12019-04-17 12:48:32 -0700376void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
377 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800378 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
379 auto layerFE = layer->getLayerFE();
380 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700381 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800382 }
383
384 // Ensure we have a snapshot of the basic geometry layer state. Limit the
385 // snapshots to once per frame for each candidate layer, as layers may
386 // appear on multiple outputs.
387 if (!coverage.latchedLayers.count(layerFE)) {
388 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700389 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800390 compositionengine::LayerFE::StateSubset::BasicGeometry);
391 }
392
393 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700394 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800395
396 // Only consider the layers on the given layer stack
397 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700398 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800399 }
400
401 /*
402 * opaqueRegion: area of a surface that is fully opaque.
403 */
404 Region opaqueRegion;
405
406 /*
407 * visibleRegion: area of a surface that is visible on screen and not fully
408 * transparent. This is essentially the layer's footprint minus the opaque
409 * regions above it. Areas covered by a translucent surface are considered
410 * visible.
411 */
412 Region visibleRegion;
413
414 /*
415 * coveredRegion: area of a surface that is covered by all visible regions
416 * above it (which includes the translucent areas).
417 */
418 Region coveredRegion;
419
420 /*
421 * transparentRegion: area of a surface that is hinted to be completely
422 * transparent. This is only used to tell when the layer has no visible non-
423 * transparent regions and can be removed from the layer list. It does not
424 * affect the visibleRegion of this layer or any layers beneath it. The hint
425 * may not be correct if apps don't respect the SurfaceView restrictions
426 * (which, sadly, some don't).
427 */
428 Region transparentRegion;
429
Vishnu Naira483b4a2019-12-12 15:07:52 -0800430 /*
431 * shadowRegion: Region cast by the layer's shadow.
432 */
433 Region shadowRegion;
434
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800435 // handle hidden surfaces by setting the visible region to empty
436 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700437 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800438 }
439
440 const ui::Transform& tr = layerFEState.geomLayerTransform;
441
442 // Get the visible region
443 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
444 // for computations like this?
Vishnu Naira483b4a2019-12-12 15:07:52 -0800445 const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
446 visibleRegion.set(visibleRect);
447
448 if (layerFEState.shadowRadius > 0.0f) {
449 // if the layer casts a shadow, offset the layers visible region and
450 // calculate the shadow region.
Lloyd Pique0a456232020-01-16 17:51:13 -0800451 const auto inset = static_cast<int32_t>(ceilf(layerFEState.shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800452 Rect visibleRectWithShadows(visibleRect);
453 visibleRectWithShadows.inset(inset, inset, inset, inset);
454 visibleRegion.set(visibleRectWithShadows);
455 shadowRegion = visibleRegion.subtract(visibleRect);
456 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800457
458 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700459 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800460 }
461
462 // Remove the transparent area from the visible region
463 if (!layerFEState.isOpaque) {
464 if (tr.preserveRects()) {
465 // transform the transparent region
466 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
467 } else {
468 // transformation too complex, can't do the
469 // transparent region optimization.
470 transparentRegion.clear();
471 }
472 }
473
474 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800475 const auto layerOrientation = tr.getOrientation();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800476 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
477 // If we one of the simple category of transforms (0/90/180/270 rotation
478 // + any flip), then the opaque region is the layer's footprint.
479 // Otherwise we don't try and compute the opaque region since there may
480 // be errors at the edges, and we treat the entire layer as
481 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800482 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800483 }
484
485 // Clip the covered region to the visible region
486 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
487
488 // Update accumAboveCoveredLayers for next (lower) layer
489 coverage.aboveCoveredLayers.orSelf(visibleRegion);
490
491 // subtract the opaque region covered by the layers above us
492 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
493
494 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700495 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800496 }
497
498 // Get coverage information for the layer as previously displayed,
499 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700500 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
501 auto prevOutputLayer =
502 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800503
504 // Get coverage information for the layer as previously displayed
505 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
506 const Region kEmptyRegion;
507 const Region& oldVisibleRegion =
508 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
509 const Region& oldCoveredRegion =
510 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
511
512 // compute this layer's dirty region
513 Region dirty;
514 if (layerFEState.contentDirty) {
515 // we need to invalidate the whole region
516 dirty = visibleRegion;
517 // as well, as the old visible region
518 dirty.orSelf(oldVisibleRegion);
519 } else {
520 /* compute the exposed region:
521 * the exposed region consists of two components:
522 * 1) what's VISIBLE now and was COVERED before
523 * 2) what's EXPOSED now less what was EXPOSED before
524 *
525 * note that (1) is conservative, we start with the whole visible region
526 * but only keep what used to be covered by something -- which mean it
527 * may have been exposed.
528 *
529 * (2) handles areas that were not covered by anything but got exposed
530 * because of a resize.
531 *
532 */
533 const Region newExposed = visibleRegion - coveredRegion;
534 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
535 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
536 }
537 dirty.subtractSelf(coverage.aboveOpaqueLayers);
538
539 // accumulate to the screen dirty region
540 coverage.dirtyRegion.orSelf(dirty);
541
542 // Update accumAboveOpaqueLayers for next (lower) layer
543 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
544
545 // Compute the visible non-transparent region
546 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
547
Vishnu Naira483b4a2019-12-12 15:07:52 -0800548 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700550 const auto& outputState = getState();
551 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
552 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800553 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700554 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800555 }
556
Vishnu Naira483b4a2019-12-12 15:07:52 -0800557 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
558
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800559 // The layer is visible. Either reuse the existing outputLayer if we have
560 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700561 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562
563 // Store the layer coverage information into the layer state as some of it
564 // is useful later.
565 auto& outputLayerState = result->editState();
566 outputLayerState.visibleRegion = visibleRegion;
567 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
568 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800569 outputLayerState.outputSpaceVisibleRegion =
570 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
571 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800572}
573
574void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
575 // The base class does nothing with this call.
576}
577
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800578void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700579 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700580 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800581 args.updatingGeometryThisFrame
582 ? LayerFE::StateSubset::GeometryAndContent
583 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800584 }
585}
586
587void Output::updateAndWriteCompositionState(
588 const compositionengine::CompositionRefreshArgs& refreshArgs) {
589 ATRACE_CALL();
590 ALOGV(__FUNCTION__);
591
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800592 if (!getState().isEnabled) {
593 return;
594 }
595
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800596 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
597 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
598
Lloyd Pique01c77c12019-04-17 12:48:32 -0700599 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700600 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800601 refreshArgs.devOptForceClientComposition ||
602 forceClientComposition);
603
604 if (mLayerRequestingBackgroundBlur == layer) {
605 forceClientComposition = false;
606 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800607
608 // Send the updated state to the HWC, if appropriate.
609 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
610 }
611}
612
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800613compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
614 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
615 for (auto* layer : getOutputLayersOrderedByZ()) {
616 if (layer->getLayer().getFEState().backgroundBlurRadius > 0) {
617 layerRequestingBgComposition = layer;
618 }
619 }
620 return layerRequestingBgComposition;
621}
622
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800623void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
624 setColorProfile(pickColorProfile(refreshArgs));
625}
626
627// Returns a data space that fits all visible layers. The returned data space
628// can only be one of
629// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
630// - Dataspace::DISPLAY_P3
631// - Dataspace::DISPLAY_BT2020
632// The returned HDR data space is one of
633// - Dataspace::UNKNOWN
634// - Dataspace::BT2020_HLG
635// - Dataspace::BT2020_PQ
636ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
637 bool* outIsHdrClientComposition) const {
638 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
639 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
640
Lloyd Pique01c77c12019-04-17 12:48:32 -0700641 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700642 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800643 case ui::Dataspace::V0_SCRGB:
644 case ui::Dataspace::V0_SCRGB_LINEAR:
645 case ui::Dataspace::BT2020:
646 case ui::Dataspace::BT2020_ITU:
647 case ui::Dataspace::BT2020_LINEAR:
648 case ui::Dataspace::DISPLAY_BT2020:
649 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
650 break;
651 case ui::Dataspace::DISPLAY_P3:
652 bestDataSpace = ui::Dataspace::DISPLAY_P3;
653 break;
654 case ui::Dataspace::BT2020_PQ:
655 case ui::Dataspace::BT2020_ITU_PQ:
656 bestDataSpace = ui::Dataspace::DISPLAY_P3;
657 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700658 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800659 break;
660 case ui::Dataspace::BT2020_HLG:
661 case ui::Dataspace::BT2020_ITU_HLG:
662 bestDataSpace = ui::Dataspace::DISPLAY_P3;
663 // When there's mixed PQ content and HLG content, we set the HDR
664 // data space to be BT2020_PQ and convert HLG to PQ.
665 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
666 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
667 }
668 break;
669 default:
670 break;
671 }
672 }
673
674 return bestDataSpace;
675}
676
677compositionengine::Output::ColorProfile Output::pickColorProfile(
678 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
679 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
680 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
681 ui::RenderIntent::COLORIMETRIC,
682 refreshArgs.colorSpaceAgnosticDataspace};
683 }
684
685 ui::Dataspace hdrDataSpace;
686 bool isHdrClientComposition = false;
687 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
688
689 switch (refreshArgs.forceOutputColorMode) {
690 case ui::ColorMode::SRGB:
691 bestDataSpace = ui::Dataspace::V0_SRGB;
692 break;
693 case ui::ColorMode::DISPLAY_P3:
694 bestDataSpace = ui::Dataspace::DISPLAY_P3;
695 break;
696 default:
697 break;
698 }
699
700 // respect hdrDataSpace only when there is no legacy HDR support
701 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
702 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
703 if (isHdr) {
704 bestDataSpace = hdrDataSpace;
705 }
706
707 ui::RenderIntent intent;
708 switch (refreshArgs.outputColorSetting) {
709 case OutputColorSetting::kManaged:
710 case OutputColorSetting::kUnmanaged:
711 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
712 : ui::RenderIntent::COLORIMETRIC;
713 break;
714 case OutputColorSetting::kEnhanced:
715 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
716 break;
717 default: // vendor display color setting
718 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
719 break;
720 }
721
722 ui::ColorMode outMode;
723 ui::Dataspace outDataSpace;
724 ui::RenderIntent outRenderIntent;
725 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
726 &outRenderIntent);
727
728 return ColorProfile{outMode, outDataSpace, outRenderIntent,
729 refreshArgs.colorSpaceAgnosticDataspace};
730}
731
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800732void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700733 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800734 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700735 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700736 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800737
738 // If nothing has changed (!dirty), don't recompose.
739 // If something changed, but we don't currently have any visible layers,
740 // and didn't when we last did a composition, then skip it this time.
741 // The second rule does two things:
742 // - When all layers are removed from a display, we'll emit one black
743 // frame, then nothing more until we get new layers.
744 // - When a display is created with a private layer stack, we won't
745 // emit any black frames until a layer is added to the layer stack.
746 const bool mustRecompose = dirty && !(empty && wasEmpty);
747
748 const char flagPrefix[] = {'-', '+'};
749 static_cast<void>(flagPrefix);
750 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
751 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
752 flagPrefix[empty], flagPrefix[wasEmpty]);
753
754 mRenderSurface->beginFrame(mustRecompose);
755
756 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700757 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800758 }
759}
760
Lloyd Pique66d68602019-02-13 14:23:31 -0800761void Output::prepareFrame() {
762 ATRACE_CALL();
763 ALOGV(__FUNCTION__);
764
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700765 const auto& outputState = getState();
766 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800767 return;
768 }
769
770 chooseCompositionStrategy();
771
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700772 mRenderSurface->prepareFrame(outputState.usesClientComposition,
773 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800774}
775
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800776void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
777 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
778 return;
779 }
780
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700781 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800782 // transform the dirty region into this screen's coordinate space
783 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
784 if (!dirtyRegion.isEmpty()) {
785 base::unique_fd readyFence;
786 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800787 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800788
789 mRenderSurface->queueBuffer(std::move(readyFence));
790 }
791 }
792
793 postFramebuffer();
794
795 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
796
797 prepareFrame();
798}
799
Lloyd Piqued3d69882019-02-28 16:03:46 -0800800void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
801 ATRACE_CALL();
802 ALOGV(__FUNCTION__);
803
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700804 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800805 return;
806 }
807
808 // Repaint the framebuffer (if needed), getting the optional fence for when
809 // the composition completes.
810 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
811 if (!optReadyFence) {
812 return;
813 }
814
815 // swap buffers (presentation)
816 mRenderSurface->queueBuffer(std::move(*optReadyFence));
817}
818
819std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800820 ATRACE_CALL();
821 ALOGV(__FUNCTION__);
822
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700823 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800824 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800825 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700826 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800827 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800828 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800829 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800830 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800831 }
832
833 ALOGV("hasClientComposition");
834
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700835 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800836 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
837
838 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800839 clientCompositionDisplay.physicalDisplay = outputState.destinationClip;
840 clientCompositionDisplay.clip = outputState.sourceClip;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700841 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
842 clientCompositionDisplay.orientation = outputState.orientation;
843 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
844 ? outputState.dataspace
845 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800846 clientCompositionDisplay.maxLuminance =
847 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
848
849 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700850 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
851 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800852 }
853
854 // Note: Updated by generateClientCompositionRequests
855 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
856
857 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800858 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800859 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800860 clientCompositionDisplay.clearRegion,
861 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800862 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
863
864 // If we the display is secure, protected content support is enabled, and at
865 // least one layer has protected content, we need to use a secure back
866 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700867 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700868 auto layers = getOutputLayersOrderedByZ();
869 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
870 return layer->getLayer().getFEState().hasProtectedContent;
871 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800872 if (needsProtected != renderEngine.isProtected()) {
873 renderEngine.useProtectedContext(needsProtected);
874 }
875 if (needsProtected != mRenderSurface->isProtected() &&
876 needsProtected == renderEngine.isProtected()) {
877 mRenderSurface->setProtected(needsProtected);
878 }
879 }
880
881 base::unique_fd fd;
882 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
883 if (buf == nullptr) {
884 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
885 "client composition for this frame",
886 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800887 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800888 }
889
Vishnu Nair9b079a22020-01-21 14:36:08 -0800890 // Check if the client composition requests were rendered into the provided graphic buffer. If
891 // so, we can reuse the buffer and avoid client composition.
892 if (mClientCompositionRequestCache) {
893 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
894 clientCompositionLayers)) {
895 outputCompositionState.reusedClientComposition = true;
896 setExpensiveRenderingExpected(false);
897 return readyFence;
898 }
899 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
900 clientCompositionLayers);
901 }
902
Lloyd Pique688abd42019-02-15 15:42:24 -0800903 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800904 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
905 // GPU composition can finish in time. We must reset GPU frequency afterwards,
906 // because high frequency consumes extra battery.
Lloyd Pique688abd42019-02-15 15:42:24 -0800907 const bool expensiveRenderingExpected =
Lucas Dupinf82ed8f2020-01-17 12:11:07 -0800908 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
Lloyd Pique688abd42019-02-15 15:42:24 -0800909 if (expensiveRenderingExpected) {
910 setExpensiveRenderingExpected(true);
911 }
912
Vishnu Nair9b079a22020-01-21 14:36:08 -0800913 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
914 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
915 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
916 std::back_inserter(clientCompositionLayerPointers),
917 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
918 return &settings;
919 });
920
Alec Mourie4034bb2019-11-19 12:45:54 -0800921 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800922 status_t status =
923 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers,
924 buf->getNativeBuffer(), /*useFramebufferCache=*/true,
925 std::move(fd), &readyFence);
926
927 if (status != NO_ERROR && mClientCompositionRequestCache) {
928 // If rendering was not successful, remove the request from the cache.
929 mClientCompositionRequestCache->remove(buf->getId());
930 }
931
Alec Mourie4034bb2019-11-19 12:45:54 -0800932 auto& timeStats = getCompositionEngine().getTimeStats();
933 if (readyFence.get() < 0) {
934 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
935 } else {
936 timeStats.recordRenderEngineDuration(renderEngineStart,
937 std::make_shared<FenceTime>(
938 new Fence(dup(readyFence.get()))));
939 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800940
Lloyd Piqued3d69882019-02-28 16:03:46 -0800941 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800942}
943
Vishnu Nair9b079a22020-01-21 14:36:08 -0800944std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800945 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800946 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -0800947 ALOGV("Rendering client layers");
948
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700949 const auto& outputState = getState();
950 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800951 const bool useIdentityTransform = false;
952 bool firstLayer = true;
953 // Used when a layer clears part of the buffer.
954 Region dummyRegion;
955
Lloyd Pique01c77c12019-04-17 12:48:32 -0700956 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800957 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700958 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800959 auto& layerFE = layer->getLayerFE();
960
Lloyd Piquea2468662019-03-07 21:31:06 -0800961 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800962 ALOGV("Layer: %s", layerFE.getDebugName());
963 if (clip.isEmpty()) {
964 ALOGV(" Skipping for empty clip");
965 firstLayer = false;
966 continue;
967 }
968
Vishnu Naira483b4a2019-12-12 15:07:52 -0800969 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800970
971 // We clear the client target for non-client composed layers if
972 // requested by the HWC. We skip this if the layer is not an opaque
973 // rectangle, as by definition the layer must blend with whatever is
974 // underneath. We also skip the first layer as the buffer target is
975 // guaranteed to start out cleared.
976 bool clearClientComposition =
977 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
978
979 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
980
981 if (clientComposition || clearClientComposition) {
982 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
983 clip,
984 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700985 layer->needsFiltering() || outputState.needsFiltering,
986 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800987 supportsProtectedContent,
988 clientComposition ? clearRegion : dummyRegion,
989 };
Vishnu Nair9b079a22020-01-21 14:36:08 -0800990 if (std::optional<LayerFE::LayerSettings> result =
991 layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700992 if (!clientComposition) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800993 LayerFE::LayerSettings& layerSettings = *result;
Lloyd Pique688abd42019-02-15 15:42:24 -0800994 layerSettings.source.buffer.buffer = nullptr;
995 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
996 layerSettings.alpha = half(0.0);
997 layerSettings.disableBlending = true;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800998 layerSettings.frameNumber = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800999 } else {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001000 std::optional<LayerFE::LayerSettings> shadowLayer =
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001001 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
1002 outputDataspace);
1003 if (shadowLayer) {
1004 clientCompositionLayers.push_back(*shadowLayer);
1005 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001006 }
1007
Vishnu Naira483b4a2019-12-12 15:07:52 -08001008 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1009 // composing the non-shadow content and only draw the shadows.
1010 const bool skipNonShadowContentComposition = clientComposition &&
1011 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1012
1013 if (!skipNonShadowContentComposition) {
1014 layer->editState().clientCompositionTimestamp = systemTime();
1015 clientCompositionLayers.push_back(*result);
1016 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001017 }
1018 }
1019
1020 firstLayer = false;
1021 }
1022
1023 return clientCompositionLayers;
1024}
1025
1026void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001027 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001028 if (flashRegion.isEmpty()) {
1029 return;
1030 }
1031
Vishnu Nair9b079a22020-01-21 14:36:08 -08001032 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001033 layerSettings.source.buffer.buffer = nullptr;
1034 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1035 layerSettings.alpha = half(1.0);
1036
1037 for (const auto& rect : flashRegion) {
1038 layerSettings.geometry.boundaries = rect.toFloatRect();
1039 clientCompositionLayers.push_back(layerSettings);
1040 }
1041}
1042
1043void Output::setExpensiveRenderingExpected(bool) {
1044 // The base class does nothing with this call.
1045}
1046
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001047void Output::postFramebuffer() {
1048 ATRACE_CALL();
1049 ALOGV(__FUNCTION__);
1050
1051 if (!getState().isEnabled) {
1052 return;
1053 }
1054
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001055 auto& outputState = editState();
1056 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001057 mRenderSurface->flip();
1058
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001059 auto frame = presentAndGetFrameFences();
1060
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001061 mRenderSurface->onPresentDisplayCompleted();
1062
Lloyd Pique01c77c12019-04-17 12:48:32 -07001063 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001064 // The layer buffer from the previous frame (if any) is released
1065 // by HWC only when the release fence from this frame (if any) is
1066 // signaled. Always get the release fence from HWC first.
1067 sp<Fence> releaseFence = Fence::NO_FENCE;
1068
1069 if (auto hwcLayer = layer->getHwcLayer()) {
1070 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1071 releaseFence = f->second;
1072 }
1073 }
1074
1075 // If the layer was client composited in the previous frame, we
1076 // need to merge with the previous client target acquire fence.
1077 // Since we do not track that, always merge with the current
1078 // client target acquire fence when it is available, even though
1079 // this is suboptimal.
1080 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001081 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001082 releaseFence =
1083 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1084 }
1085
1086 layer->getLayerFE().onLayerDisplayed(releaseFence);
1087 }
1088
1089 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001090 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001091 // supply them with the present fence.
1092 for (auto& weakLayer : mReleasedLayers) {
1093 if (auto layer = weakLayer.promote(); layer != nullptr) {
1094 layer->onLayerDisplayed(frame.presentFence);
1095 }
1096 }
1097
1098 // Clear out the released layers now that we're done with them.
1099 mReleasedLayers.clear();
1100}
1101
Lloyd Pique32cbe282018-10-19 13:09:22 -07001102void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001103 auto& outputState = editState();
1104 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001105}
1106
Lloyd Pique66d68602019-02-13 14:23:31 -08001107void Output::chooseCompositionStrategy() {
1108 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001109 auto& outputState = editState();
1110 outputState.usesClientComposition = true;
1111 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001112 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001113}
1114
Lloyd Pique688abd42019-02-15 15:42:24 -08001115bool Output::getSkipColorTransform() const {
1116 return true;
1117}
1118
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001119compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1120 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001121 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001122 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1123 }
1124 return result;
1125}
1126
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001127} // namespace impl
1128} // namespace android::compositionengine