blob: 650b5c1d291c5300408fcae41301be7479db9a91 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <thread>
22
Lloyd Pique32cbe282018-10-19 13:09:22 -070023#include <android-base/stringprintf.h>
24#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080025#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070026#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080027#include <compositionengine/Layer.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070029#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070030#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070031#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070032#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080033#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070034#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include <renderengine/DisplaySettings.h>
36#include <renderengine/RenderEngine.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070037#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080038#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080039#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070040
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include "TracedOrdinal.h"
42
Lloyd Piquefeb73d72018-12-04 17:23:44 -080043namespace android::compositionengine {
44
45Output::~Output() = default;
46
47namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070048
Lloyd Piquec29e4c62019-03-07 21:48:19 -080049namespace {
50
51template <typename T>
52class Reversed {
53public:
54 explicit Reversed(const T& container) : mContainer(container) {}
55 auto begin() { return mContainer.rbegin(); }
56 auto end() { return mContainer.rend(); }
57
58private:
59 const T& mContainer;
60};
61
62// Helper for enumerating over a container in reverse order
63template <typename T>
64Reversed<T> reversed(const T& c) {
65 return Reversed<T>(c);
66}
67
68} // namespace
69
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070070std::shared_ptr<Output> createOutput(
71 const compositionengine::CompositionEngine& compositionEngine) {
72 return createOutputTemplated<Output>(compositionEngine);
73}
Lloyd Pique32cbe282018-10-19 13:09:22 -070074
75Output::~Output() = default;
76
Lloyd Pique32cbe282018-10-19 13:09:22 -070077bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070078 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
79 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070080}
81
Lloyd Pique6c564cf2019-05-17 17:31:36 -070082std::optional<DisplayId> Output::getDisplayId() const {
83 return {};
84}
85
Lloyd Pique32cbe282018-10-19 13:09:22 -070086const std::string& Output::getName() const {
87 return mName;
88}
89
90void Output::setName(const std::string& name) {
91 mName = name;
92}
93
94void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070095 auto& outputState = editState();
96 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070097 return;
98 }
99
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700100 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101 dirtyEntireOutput();
102}
103
104void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame,
105 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700106 auto& outputState = editState();
107 outputState.transform = transform;
108 outputState.orientation = orientation;
109 outputState.scissor = scissor;
110 outputState.frame = frame;
111 outputState.viewport = viewport;
112 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700113
114 dirtyEntireOutput();
115}
116
Lloyd Pique688abd42019-02-15 15:42:24 -0800117// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700118void Output::setBounds(const ui::Size& size) {
119 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700120 // TODO(b/121291683): Rename outputState.size once more is moved.
121 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700122
123 dirtyEntireOutput();
124}
125
Lloyd Piqueef36b002019-01-23 17:52:04 -0800126void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127 auto& outputState = editState();
128 outputState.layerStackId = layerStackId;
129 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700130
131 dirtyEntireOutput();
132}
133
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800134void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700135 auto& colorTransformMatrix = editState().colorTransformMatrix;
136 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700137 return;
138 }
139
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800141
142 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700143}
144
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800145void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700146 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800147 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
148 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800149
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700150 auto& outputState = editState();
151 if (outputState.colorMode == colorProfile.mode &&
152 outputState.dataspace == colorProfile.dataspace &&
153 outputState.renderIntent == colorProfile.renderIntent &&
154 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800155 return;
156 }
157
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700158 outputState.colorMode = colorProfile.mode;
159 outputState.dataspace = colorProfile.dataspace;
160 outputState.renderIntent = colorProfile.renderIntent;
161 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700162
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800163 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700164
Lloyd Pique32cbe282018-10-19 13:09:22 -0700165 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800166 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
167 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800168
169 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700170}
171
172void Output::dump(std::string& out) const {
173 using android::base::StringAppendF;
174
175 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
176
177 out.append("\n ");
178
179 dumpBase(out);
180}
181
182void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700183 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700185 if (mDisplayColorProfile) {
186 mDisplayColorProfile->dump(out);
187 } else {
188 out.append(" No display color profile!\n");
189 }
190
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191 if (mRenderSurface) {
192 mRenderSurface->dump(out);
193 } else {
194 out.append(" No render surface!\n");
195 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800196
Lloyd Pique01c77c12019-04-17 12:48:32 -0700197 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
198 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800199 if (!outputLayer) {
200 continue;
201 }
202 outputLayer->dump(out);
203 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700204}
205
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700206compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
207 return mDisplayColorProfile.get();
208}
209
210void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
211 mDisplayColorProfile = std::move(mode);
212}
213
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800214const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
215 return mReleasedLayers;
216}
217
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700218void Output::setDisplayColorProfileForTest(
219 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
220 mDisplayColorProfile = std::move(mode);
221}
222
Lloyd Pique31cb2942018-10-19 17:23:03 -0700223compositionengine::RenderSurface* Output::getRenderSurface() const {
224 return mRenderSurface.get();
225}
226
227void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
228 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700229 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700230
231 dirtyEntireOutput();
232}
233
234void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
235 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700236}
237
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000238Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700239 const auto& outputState = getState();
240 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000241 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700242 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700243 }
244 return dirty;
245}
246
Lloyd Piquec6687342019-03-07 21:34:57 -0800247bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800248 // The layerStackId's must match, and also the layer must not be internal
249 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700250 const auto& outputState = getState();
251 return layerStackId && (*layerStackId == outputState.layerStackId) &&
252 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700253}
254
Lloyd Pique66c20c42019-03-07 21:44:02 -0800255bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
256 if (!layer) {
257 return false;
258 }
259
Lloyd Pique9755fb72019-03-26 14:44:40 -0700260 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800261 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
262}
263
Lloyd Piquedf336d92019-03-07 21:38:42 -0800264std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700265 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800266 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800267}
268
Lloyd Pique01c77c12019-04-17 12:48:32 -0700269compositionengine::OutputLayer* Output::getOutputLayerForLayer(
270 compositionengine::Layer* layer) const {
271 auto index = findCurrentOutputLayerForLayer(layer);
272 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800273}
274
Lloyd Pique01c77c12019-04-17 12:48:32 -0700275std::optional<size_t> Output::findCurrentOutputLayerForLayer(
276 compositionengine::Layer* layer) const {
277 for (size_t i = 0; i < getOutputLayerCount(); i++) {
278 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
279 if (outputLayer && &outputLayer->getLayer() == layer) {
280 return i;
281 }
282 }
283 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800284}
285
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800286void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
287 mReleasedLayers = std::move(layers);
288}
289
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800290void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
291 LayerFESet& geomSnapshots) {
292 ATRACE_CALL();
293 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800294
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800295 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800296}
297
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800298void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800299 ATRACE_CALL();
300 ALOGV(__FUNCTION__);
301
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800302 updateColorProfile(refreshArgs);
303 updateAndWriteCompositionState(refreshArgs);
304 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800305 beginFrame();
306 prepareFrame();
307 devOptRepaintFlash(refreshArgs);
308 finishFrame(refreshArgs);
309 postFramebuffer();
310}
311
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800312void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
313 LayerFESet& layerFESet) {
314 ATRACE_CALL();
315 ALOGV(__FUNCTION__);
316
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700317 auto& outputState = editState();
318
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800319 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700320 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800321 return;
322 }
323
324 // Process the layers to determine visibility and coverage
325 compositionengine::Output::CoverageState coverage{layerFESet};
326 collectVisibleLayers(refreshArgs, coverage);
327
328 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700329 const ui::Transform& tr = outputState.transform;
330 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800331 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
332
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700333 outputState.undefinedRegion = undefinedRegion;
334 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800335}
336
337void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
338 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800339 // Evaluate the layers from front to back to determine what is visible. This
340 // also incrementally calculates the coverage information for each layer as
341 // well as the entire output.
342 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700343 // Incrementally process the coverage for each layer
344 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800345
346 // TODO(b/121291683): Stop early if the output is completely covered and
347 // no more layers could even be visible underneath the ones on top.
348 }
349
Lloyd Pique01c77c12019-04-17 12:48:32 -0700350 setReleasedLayers(refreshArgs);
351
352 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800353
354 // Generate a simple Z-order values to each visible output layer
355 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700356 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800357 outputLayer->editState().z = zOrder++;
358 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800359}
360
Lloyd Pique01c77c12019-04-17 12:48:32 -0700361void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
362 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800363 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
364 auto layerFE = layer->getLayerFE();
365 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700366 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800367 }
368
369 // Ensure we have a snapshot of the basic geometry layer state. Limit the
370 // snapshots to once per frame for each candidate layer, as layers may
371 // appear on multiple outputs.
372 if (!coverage.latchedLayers.count(layerFE)) {
373 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700374 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800375 compositionengine::LayerFE::StateSubset::BasicGeometry);
376 }
377
378 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700379 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380
381 // Only consider the layers on the given layer stack
382 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700383 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800384 }
385
386 /*
387 * opaqueRegion: area of a surface that is fully opaque.
388 */
389 Region opaqueRegion;
390
391 /*
392 * visibleRegion: area of a surface that is visible on screen and not fully
393 * transparent. This is essentially the layer's footprint minus the opaque
394 * regions above it. Areas covered by a translucent surface are considered
395 * visible.
396 */
397 Region visibleRegion;
398
399 /*
400 * coveredRegion: area of a surface that is covered by all visible regions
401 * above it (which includes the translucent areas).
402 */
403 Region coveredRegion;
404
405 /*
406 * transparentRegion: area of a surface that is hinted to be completely
407 * transparent. This is only used to tell when the layer has no visible non-
408 * transparent regions and can be removed from the layer list. It does not
409 * affect the visibleRegion of this layer or any layers beneath it. The hint
410 * may not be correct if apps don't respect the SurfaceView restrictions
411 * (which, sadly, some don't).
412 */
413 Region transparentRegion;
414
Vishnu Naira483b4a2019-12-12 15:07:52 -0800415 /*
416 * shadowRegion: Region cast by the layer's shadow.
417 */
418 Region shadowRegion;
419
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800420 // handle hidden surfaces by setting the visible region to empty
421 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700422 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800423 }
424
425 const ui::Transform& tr = layerFEState.geomLayerTransform;
426
427 // Get the visible region
428 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
429 // for computations like this?
Vishnu Naira483b4a2019-12-12 15:07:52 -0800430 const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds));
431 visibleRegion.set(visibleRect);
432
433 if (layerFEState.shadowRadius > 0.0f) {
434 // if the layer casts a shadow, offset the layers visible region and
435 // calculate the shadow region.
436 const int32_t inset = layerFEState.shadowRadius * -1.0f;
437 Rect visibleRectWithShadows(visibleRect);
438 visibleRectWithShadows.inset(inset, inset, inset, inset);
439 visibleRegion.set(visibleRectWithShadows);
440 shadowRegion = visibleRegion.subtract(visibleRect);
441 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800442
443 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700444 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800445 }
446
447 // Remove the transparent area from the visible region
448 if (!layerFEState.isOpaque) {
449 if (tr.preserveRects()) {
450 // transform the transparent region
451 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
452 } else {
453 // transformation too complex, can't do the
454 // transparent region optimization.
455 transparentRegion.clear();
456 }
457 }
458
459 // compute the opaque region
460 const int32_t layerOrientation = tr.getOrientation();
461 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
462 // If we one of the simple category of transforms (0/90/180/270 rotation
463 // + any flip), then the opaque region is the layer's footprint.
464 // Otherwise we don't try and compute the opaque region since there may
465 // be errors at the edges, and we treat the entire layer as
466 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800467 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468 }
469
470 // Clip the covered region to the visible region
471 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
472
473 // Update accumAboveCoveredLayers for next (lower) layer
474 coverage.aboveCoveredLayers.orSelf(visibleRegion);
475
476 // subtract the opaque region covered by the layers above us
477 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
478
479 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700480 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800481 }
482
483 // Get coverage information for the layer as previously displayed,
484 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700485 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
486 auto prevOutputLayer =
487 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800488
489 // Get coverage information for the layer as previously displayed
490 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
491 const Region kEmptyRegion;
492 const Region& oldVisibleRegion =
493 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
494 const Region& oldCoveredRegion =
495 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
496
497 // compute this layer's dirty region
498 Region dirty;
499 if (layerFEState.contentDirty) {
500 // we need to invalidate the whole region
501 dirty = visibleRegion;
502 // as well, as the old visible region
503 dirty.orSelf(oldVisibleRegion);
504 } else {
505 /* compute the exposed region:
506 * the exposed region consists of two components:
507 * 1) what's VISIBLE now and was COVERED before
508 * 2) what's EXPOSED now less what was EXPOSED before
509 *
510 * note that (1) is conservative, we start with the whole visible region
511 * but only keep what used to be covered by something -- which mean it
512 * may have been exposed.
513 *
514 * (2) handles areas that were not covered by anything but got exposed
515 * because of a resize.
516 *
517 */
518 const Region newExposed = visibleRegion - coveredRegion;
519 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
520 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
521 }
522 dirty.subtractSelf(coverage.aboveOpaqueLayers);
523
524 // accumulate to the screen dirty region
525 coverage.dirtyRegion.orSelf(dirty);
526
527 // Update accumAboveOpaqueLayers for next (lower) layer
528 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
529
530 // Compute the visible non-transparent region
531 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
532
Vishnu Naira483b4a2019-12-12 15:07:52 -0800533 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800534 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700535 const auto& outputState = getState();
536 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
537 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800538 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700539 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800540 }
541
Vishnu Naira483b4a2019-12-12 15:07:52 -0800542 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
543
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800544 // The layer is visible. Either reuse the existing outputLayer if we have
545 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700546 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800547
548 // Store the layer coverage information into the layer state as some of it
549 // is useful later.
550 auto& outputLayerState = result->editState();
551 outputLayerState.visibleRegion = visibleRegion;
552 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
553 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800554 outputLayerState.outputSpaceVisibleRegion =
555 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
556 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800557}
558
559void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
560 // The base class does nothing with this call.
561}
562
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800563void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700564 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700565 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800566 args.updatingGeometryThisFrame
567 ? LayerFE::StateSubset::GeometryAndContent
568 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800569 }
570}
571
572void Output::updateAndWriteCompositionState(
573 const compositionengine::CompositionRefreshArgs& refreshArgs) {
574 ATRACE_CALL();
575 ALOGV(__FUNCTION__);
576
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800577 if (!getState().isEnabled) {
578 return;
579 }
580
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800581 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
582 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
583
Lloyd Pique01c77c12019-04-17 12:48:32 -0700584 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700585 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800586 refreshArgs.devOptForceClientComposition ||
587 forceClientComposition);
588
589 if (mLayerRequestingBackgroundBlur == layer) {
590 forceClientComposition = false;
591 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800592
593 // Send the updated state to the HWC, if appropriate.
594 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
595 }
596}
597
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800598compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
599 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
600 for (auto* layer : getOutputLayersOrderedByZ()) {
601 if (layer->getLayer().getFEState().backgroundBlurRadius > 0) {
602 layerRequestingBgComposition = layer;
603 }
604 }
605 return layerRequestingBgComposition;
606}
607
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800608void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
609 setColorProfile(pickColorProfile(refreshArgs));
610}
611
612// Returns a data space that fits all visible layers. The returned data space
613// can only be one of
614// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
615// - Dataspace::DISPLAY_P3
616// - Dataspace::DISPLAY_BT2020
617// The returned HDR data space is one of
618// - Dataspace::UNKNOWN
619// - Dataspace::BT2020_HLG
620// - Dataspace::BT2020_PQ
621ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
622 bool* outIsHdrClientComposition) const {
623 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
624 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
625
Lloyd Pique01c77c12019-04-17 12:48:32 -0700626 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700627 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800628 case ui::Dataspace::V0_SCRGB:
629 case ui::Dataspace::V0_SCRGB_LINEAR:
630 case ui::Dataspace::BT2020:
631 case ui::Dataspace::BT2020_ITU:
632 case ui::Dataspace::BT2020_LINEAR:
633 case ui::Dataspace::DISPLAY_BT2020:
634 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
635 break;
636 case ui::Dataspace::DISPLAY_P3:
637 bestDataSpace = ui::Dataspace::DISPLAY_P3;
638 break;
639 case ui::Dataspace::BT2020_PQ:
640 case ui::Dataspace::BT2020_ITU_PQ:
641 bestDataSpace = ui::Dataspace::DISPLAY_P3;
642 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700643 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800644 break;
645 case ui::Dataspace::BT2020_HLG:
646 case ui::Dataspace::BT2020_ITU_HLG:
647 bestDataSpace = ui::Dataspace::DISPLAY_P3;
648 // When there's mixed PQ content and HLG content, we set the HDR
649 // data space to be BT2020_PQ and convert HLG to PQ.
650 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
651 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
652 }
653 break;
654 default:
655 break;
656 }
657 }
658
659 return bestDataSpace;
660}
661
662compositionengine::Output::ColorProfile Output::pickColorProfile(
663 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
664 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
665 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
666 ui::RenderIntent::COLORIMETRIC,
667 refreshArgs.colorSpaceAgnosticDataspace};
668 }
669
670 ui::Dataspace hdrDataSpace;
671 bool isHdrClientComposition = false;
672 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
673
674 switch (refreshArgs.forceOutputColorMode) {
675 case ui::ColorMode::SRGB:
676 bestDataSpace = ui::Dataspace::V0_SRGB;
677 break;
678 case ui::ColorMode::DISPLAY_P3:
679 bestDataSpace = ui::Dataspace::DISPLAY_P3;
680 break;
681 default:
682 break;
683 }
684
685 // respect hdrDataSpace only when there is no legacy HDR support
686 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
687 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
688 if (isHdr) {
689 bestDataSpace = hdrDataSpace;
690 }
691
692 ui::RenderIntent intent;
693 switch (refreshArgs.outputColorSetting) {
694 case OutputColorSetting::kManaged:
695 case OutputColorSetting::kUnmanaged:
696 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
697 : ui::RenderIntent::COLORIMETRIC;
698 break;
699 case OutputColorSetting::kEnhanced:
700 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
701 break;
702 default: // vendor display color setting
703 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
704 break;
705 }
706
707 ui::ColorMode outMode;
708 ui::Dataspace outDataSpace;
709 ui::RenderIntent outRenderIntent;
710 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
711 &outRenderIntent);
712
713 return ColorProfile{outMode, outDataSpace, outRenderIntent,
714 refreshArgs.colorSpaceAgnosticDataspace};
715}
716
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800717void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700718 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800719 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700720 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700721 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800722
723 // If nothing has changed (!dirty), don't recompose.
724 // If something changed, but we don't currently have any visible layers,
725 // and didn't when we last did a composition, then skip it this time.
726 // The second rule does two things:
727 // - When all layers are removed from a display, we'll emit one black
728 // frame, then nothing more until we get new layers.
729 // - When a display is created with a private layer stack, we won't
730 // emit any black frames until a layer is added to the layer stack.
731 const bool mustRecompose = dirty && !(empty && wasEmpty);
732
733 const char flagPrefix[] = {'-', '+'};
734 static_cast<void>(flagPrefix);
735 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
736 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
737 flagPrefix[empty], flagPrefix[wasEmpty]);
738
739 mRenderSurface->beginFrame(mustRecompose);
740
741 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700742 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800743 }
744}
745
Lloyd Pique66d68602019-02-13 14:23:31 -0800746void Output::prepareFrame() {
747 ATRACE_CALL();
748 ALOGV(__FUNCTION__);
749
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700750 const auto& outputState = getState();
751 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800752 return;
753 }
754
755 chooseCompositionStrategy();
756
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700757 mRenderSurface->prepareFrame(outputState.usesClientComposition,
758 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800759}
760
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800761void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
762 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
763 return;
764 }
765
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700766 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800767 // transform the dirty region into this screen's coordinate space
768 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
769 if (!dirtyRegion.isEmpty()) {
770 base::unique_fd readyFence;
771 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800772 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800773
774 mRenderSurface->queueBuffer(std::move(readyFence));
775 }
776 }
777
778 postFramebuffer();
779
780 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
781
782 prepareFrame();
783}
784
Lloyd Piqued3d69882019-02-28 16:03:46 -0800785void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
786 ATRACE_CALL();
787 ALOGV(__FUNCTION__);
788
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700789 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800790 return;
791 }
792
793 // Repaint the framebuffer (if needed), getting the optional fence for when
794 // the composition completes.
795 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
796 if (!optReadyFence) {
797 return;
798 }
799
800 // swap buffers (presentation)
801 mRenderSurface->queueBuffer(std::move(*optReadyFence));
802}
803
804std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800805 ATRACE_CALL();
806 ALOGV(__FUNCTION__);
807
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700808 const auto& outputState = getState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800809 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700810 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800811 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800812 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800813 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800814 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800815 }
816
817 ALOGV("hasClientComposition");
818
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700819 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800820 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
821
822 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700823 clientCompositionDisplay.physicalDisplay = outputState.scissor;
824 clientCompositionDisplay.clip = outputState.scissor;
825 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
826 clientCompositionDisplay.orientation = outputState.orientation;
827 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
828 ? outputState.dataspace
829 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800830 clientCompositionDisplay.maxLuminance =
831 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
832
833 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700834 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
835 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800836 }
837
838 // Note: Updated by generateClientCompositionRequests
839 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
840
841 // Generate the client composition requests for the layers on this output.
842 std::vector<renderengine::LayerSettings> clientCompositionLayers =
843 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800844 clientCompositionDisplay.clearRegion,
845 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800846 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
847
848 // If we the display is secure, protected content support is enabled, and at
849 // least one layer has protected content, we need to use a secure back
850 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700851 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700852 auto layers = getOutputLayersOrderedByZ();
853 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
854 return layer->getLayer().getFEState().hasProtectedContent;
855 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800856 if (needsProtected != renderEngine.isProtected()) {
857 renderEngine.useProtectedContext(needsProtected);
858 }
859 if (needsProtected != mRenderSurface->isProtected() &&
860 needsProtected == renderEngine.isProtected()) {
861 mRenderSurface->setProtected(needsProtected);
862 }
863 }
864
865 base::unique_fd fd;
866 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
867 if (buf == nullptr) {
868 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
869 "client composition for this frame",
870 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800871 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800872 }
873
874 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800875 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
876 // GPU composition can finish in time. We must reset GPU frequency afterwards,
877 // because high frequency consumes extra battery.
Lloyd Pique688abd42019-02-15 15:42:24 -0800878 const bool expensiveRenderingExpected =
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800879 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 ||
880 mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800881 if (expensiveRenderingExpected) {
882 setExpensiveRenderingExpected(true);
883 }
884
Alec Mourie4034bb2019-11-19 12:45:54 -0800885 const nsecs_t renderEngineStart = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -0800886 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
887 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800888 &readyFence);
Alec Mourie4034bb2019-11-19 12:45:54 -0800889 auto& timeStats = getCompositionEngine().getTimeStats();
890 if (readyFence.get() < 0) {
891 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
892 } else {
893 timeStats.recordRenderEngineDuration(renderEngineStart,
894 std::make_shared<FenceTime>(
895 new Fence(dup(readyFence.get()))));
896 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800897
Lloyd Piqued3d69882019-02-28 16:03:46 -0800898 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800899}
900
901std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800902 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800903 std::vector<renderengine::LayerSettings> clientCompositionLayers;
904 ALOGV("Rendering client layers");
905
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700906 const auto& outputState = getState();
907 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800908 const bool useIdentityTransform = false;
909 bool firstLayer = true;
910 // Used when a layer clears part of the buffer.
911 Region dummyRegion;
912
Lloyd Pique01c77c12019-04-17 12:48:32 -0700913 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800914 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700915 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800916 auto& layerFE = layer->getLayerFE();
917
Lloyd Piquea2468662019-03-07 21:31:06 -0800918 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800919 ALOGV("Layer: %s", layerFE.getDebugName());
920 if (clip.isEmpty()) {
921 ALOGV(" Skipping for empty clip");
922 firstLayer = false;
923 continue;
924 }
925
Vishnu Naira483b4a2019-12-12 15:07:52 -0800926 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800927
928 // We clear the client target for non-client composed layers if
929 // requested by the HWC. We skip this if the layer is not an opaque
930 // rectangle, as by definition the layer must blend with whatever is
931 // underneath. We also skip the first layer as the buffer target is
932 // guaranteed to start out cleared.
933 bool clearClientComposition =
934 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
935
936 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
937
938 if (clientComposition || clearClientComposition) {
939 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
940 clip,
941 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700942 layer->needsFiltering() || outputState.needsFiltering,
943 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800944 supportsProtectedContent,
945 clientComposition ? clearRegion : dummyRegion,
946 };
947 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700948 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800949 auto& layerSettings = *result;
950 layerSettings.source.buffer.buffer = nullptr;
951 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
952 layerSettings.alpha = half(0.0);
953 layerSettings.disableBlending = true;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800954 } else {
955 std::optional<renderengine::LayerSettings> shadowLayer =
956 layerFE.prepareShadowClientComposition(*result, outputState.viewport,
957 outputDataspace);
958 if (shadowLayer) {
959 clientCompositionLayers.push_back(*shadowLayer);
960 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800961 }
962
Vishnu Naira483b4a2019-12-12 15:07:52 -0800963 // If the layer casts a shadow but the content casting the shadow is occluded, skip
964 // composing the non-shadow content and only draw the shadows.
965 const bool skipNonShadowContentComposition = clientComposition &&
966 layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
967
968 if (!skipNonShadowContentComposition) {
969 layer->editState().clientCompositionTimestamp = systemTime();
970 clientCompositionLayers.push_back(*result);
971 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800972 }
973 }
974
975 firstLayer = false;
976 }
977
978 return clientCompositionLayers;
979}
980
981void Output::appendRegionFlashRequests(
982 const Region& flashRegion,
983 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
984 if (flashRegion.isEmpty()) {
985 return;
986 }
987
988 renderengine::LayerSettings layerSettings;
989 layerSettings.source.buffer.buffer = nullptr;
990 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
991 layerSettings.alpha = half(1.0);
992
993 for (const auto& rect : flashRegion) {
994 layerSettings.geometry.boundaries = rect.toFloatRect();
995 clientCompositionLayers.push_back(layerSettings);
996 }
997}
998
999void Output::setExpensiveRenderingExpected(bool) {
1000 // The base class does nothing with this call.
1001}
1002
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001003void Output::postFramebuffer() {
1004 ATRACE_CALL();
1005 ALOGV(__FUNCTION__);
1006
1007 if (!getState().isEnabled) {
1008 return;
1009 }
1010
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001011 auto& outputState = editState();
1012 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001013 mRenderSurface->flip();
1014
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001015 auto frame = presentAndGetFrameFences();
1016
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001017 mRenderSurface->onPresentDisplayCompleted();
1018
Lloyd Pique01c77c12019-04-17 12:48:32 -07001019 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001020 // The layer buffer from the previous frame (if any) is released
1021 // by HWC only when the release fence from this frame (if any) is
1022 // signaled. Always get the release fence from HWC first.
1023 sp<Fence> releaseFence = Fence::NO_FENCE;
1024
1025 if (auto hwcLayer = layer->getHwcLayer()) {
1026 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1027 releaseFence = f->second;
1028 }
1029 }
1030
1031 // If the layer was client composited in the previous frame, we
1032 // need to merge with the previous client target acquire fence.
1033 // Since we do not track that, always merge with the current
1034 // client target acquire fence when it is available, even though
1035 // this is suboptimal.
1036 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001037 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001038 releaseFence =
1039 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1040 }
1041
1042 layer->getLayerFE().onLayerDisplayed(releaseFence);
1043 }
1044
1045 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001046 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001047 // supply them with the present fence.
1048 for (auto& weakLayer : mReleasedLayers) {
1049 if (auto layer = weakLayer.promote(); layer != nullptr) {
1050 layer->onLayerDisplayed(frame.presentFence);
1051 }
1052 }
1053
1054 // Clear out the released layers now that we're done with them.
1055 mReleasedLayers.clear();
1056}
1057
Lloyd Pique32cbe282018-10-19 13:09:22 -07001058void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001059 auto& outputState = editState();
1060 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001061}
1062
Lloyd Pique66d68602019-02-13 14:23:31 -08001063void Output::chooseCompositionStrategy() {
1064 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001065 auto& outputState = editState();
1066 outputState.usesClientComposition = true;
1067 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001068}
1069
Lloyd Pique688abd42019-02-15 15:42:24 -08001070bool Output::getSkipColorTransform() const {
1071 return true;
1072}
1073
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001074compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1075 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001076 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001077 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1078 }
1079 return result;
1080}
1081
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001082} // namespace impl
1083} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001084
1085// TODO(b/129481165): remove the #pragma below and fix conversion issues
1086#pragma clang diagnostic pop // ignored "-Wconversion"