blob: 6877f8bba02e6bac12e57b529012f5e3b7429788 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080017#include <thread>
18
Lloyd Pique32cbe282018-10-19 13:09:22 -070019#include <android-base/stringprintf.h>
20#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080021#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070022#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080023#include <compositionengine/Layer.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080024#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070025#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070026#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070027#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070028#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080029#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070030#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080031#include <renderengine/DisplaySettings.h>
32#include <renderengine/RenderEngine.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070033#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080034#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080035#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070036
Lloyd Pique688abd42019-02-15 15:42:24 -080037#include "TracedOrdinal.h"
38
Lloyd Piquefeb73d72018-12-04 17:23:44 -080039namespace android::compositionengine {
40
41Output::~Output() = default;
42
43namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070044
Lloyd Piquec29e4c62019-03-07 21:48:19 -080045namespace {
46
47template <typename T>
48class Reversed {
49public:
50 explicit Reversed(const T& container) : mContainer(container) {}
51 auto begin() { return mContainer.rbegin(); }
52 auto end() { return mContainer.rend(); }
53
54private:
55 const T& mContainer;
56};
57
58// Helper for enumerating over a container in reverse order
59template <typename T>
60Reversed<T> reversed(const T& c) {
61 return Reversed<T>(c);
62}
63
64} // namespace
65
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070066std::shared_ptr<Output> createOutput(
67 const compositionengine::CompositionEngine& compositionEngine) {
68 return createOutputTemplated<Output>(compositionEngine);
69}
Lloyd Pique32cbe282018-10-19 13:09:22 -070070
71Output::~Output() = default;
72
Lloyd Pique32cbe282018-10-19 13:09:22 -070073bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070074 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
75 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070076}
77
Lloyd Pique6c564cf2019-05-17 17:31:36 -070078std::optional<DisplayId> Output::getDisplayId() const {
79 return {};
80}
81
Lloyd Pique32cbe282018-10-19 13:09:22 -070082const std::string& Output::getName() const {
83 return mName;
84}
85
86void Output::setName(const std::string& name) {
87 mName = name;
88}
89
90void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070091 auto& outputState = editState();
92 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070093 return;
94 }
95
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070096 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -070097 dirtyEntireOutput();
98}
99
100void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame,
101 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700102 auto& outputState = editState();
103 outputState.transform = transform;
104 outputState.orientation = orientation;
105 outputState.scissor = scissor;
106 outputState.frame = frame;
107 outputState.viewport = viewport;
108 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700109
110 dirtyEntireOutput();
111}
112
Lloyd Pique688abd42019-02-15 15:42:24 -0800113// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700114void Output::setBounds(const ui::Size& size) {
115 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700116 // TODO(b/121291683): Rename outputState.size once more is moved.
117 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700118
119 dirtyEntireOutput();
120}
121
Lloyd Piqueef36b002019-01-23 17:52:04 -0800122void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700123 auto& outputState = editState();
124 outputState.layerStackId = layerStackId;
125 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700126
127 dirtyEntireOutput();
128}
129
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800130void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700131 auto& colorTransformMatrix = editState().colorTransformMatrix;
132 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700133 return;
134 }
135
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700136 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800137
138 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700139}
140
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800141void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700142 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800143 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
144 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800145
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700146 auto& outputState = editState();
147 if (outputState.colorMode == colorProfile.mode &&
148 outputState.dataspace == colorProfile.dataspace &&
149 outputState.renderIntent == colorProfile.renderIntent &&
150 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800151 return;
152 }
153
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700154 outputState.colorMode = colorProfile.mode;
155 outputState.dataspace = colorProfile.dataspace;
156 outputState.renderIntent = colorProfile.renderIntent;
157 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700158
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800159 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700160
Lloyd Pique32cbe282018-10-19 13:09:22 -0700161 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800162 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
163 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800164
165 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700166}
167
168void Output::dump(std::string& out) const {
169 using android::base::StringAppendF;
170
171 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
172
173 out.append("\n ");
174
175 dumpBase(out);
176}
177
178void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700179 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700180
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700181 if (mDisplayColorProfile) {
182 mDisplayColorProfile->dump(out);
183 } else {
184 out.append(" No display color profile!\n");
185 }
186
Lloyd Pique31cb2942018-10-19 17:23:03 -0700187 if (mRenderSurface) {
188 mRenderSurface->dump(out);
189 } else {
190 out.append(" No render surface!\n");
191 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800192
Lloyd Pique01c77c12019-04-17 12:48:32 -0700193 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
194 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800195 if (!outputLayer) {
196 continue;
197 }
198 outputLayer->dump(out);
199 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700200}
201
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700202compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
203 return mDisplayColorProfile.get();
204}
205
206void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
207 mDisplayColorProfile = std::move(mode);
208}
209
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800210const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
211 return mReleasedLayers;
212}
213
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700214void Output::setDisplayColorProfileForTest(
215 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
216 mDisplayColorProfile = std::move(mode);
217}
218
Lloyd Pique31cb2942018-10-19 17:23:03 -0700219compositionengine::RenderSurface* Output::getRenderSurface() const {
220 return mRenderSurface.get();
221}
222
223void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
224 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700225 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700226
227 dirtyEntireOutput();
228}
229
230void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
231 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700232}
233
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000234Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700235 const auto& outputState = getState();
236 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000237 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700238 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700239 }
240 return dirty;
241}
242
Lloyd Piquec6687342019-03-07 21:34:57 -0800243bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800244 // The layerStackId's must match, and also the layer must not be internal
245 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700246 const auto& outputState = getState();
247 return layerStackId && (*layerStackId == outputState.layerStackId) &&
248 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249}
250
Lloyd Pique66c20c42019-03-07 21:44:02 -0800251bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
252 if (!layer) {
253 return false;
254 }
255
Lloyd Pique9755fb72019-03-26 14:44:40 -0700256 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800257 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
258}
259
Lloyd Piquedf336d92019-03-07 21:38:42 -0800260std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700261 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800262 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800263}
264
Lloyd Pique01c77c12019-04-17 12:48:32 -0700265compositionengine::OutputLayer* Output::getOutputLayerForLayer(
266 compositionengine::Layer* layer) const {
267 auto index = findCurrentOutputLayerForLayer(layer);
268 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800269}
270
Lloyd Pique01c77c12019-04-17 12:48:32 -0700271std::optional<size_t> Output::findCurrentOutputLayerForLayer(
272 compositionengine::Layer* layer) const {
273 for (size_t i = 0; i < getOutputLayerCount(); i++) {
274 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
275 if (outputLayer && &outputLayer->getLayer() == layer) {
276 return i;
277 }
278 }
279 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800280}
281
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800282void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
283 mReleasedLayers = std::move(layers);
284}
285
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800286void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
287 LayerFESet& geomSnapshots) {
288 ATRACE_CALL();
289 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800290
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800291 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800292}
293
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800294void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800295 ATRACE_CALL();
296 ALOGV(__FUNCTION__);
297
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800298 updateColorProfile(refreshArgs);
299 updateAndWriteCompositionState(refreshArgs);
300 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800301 beginFrame();
302 prepareFrame();
303 devOptRepaintFlash(refreshArgs);
304 finishFrame(refreshArgs);
305 postFramebuffer();
306}
307
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800308void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
309 LayerFESet& layerFESet) {
310 ATRACE_CALL();
311 ALOGV(__FUNCTION__);
312
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700313 auto& outputState = editState();
314
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800315 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700316 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800317 return;
318 }
319
320 // Process the layers to determine visibility and coverage
321 compositionengine::Output::CoverageState coverage{layerFESet};
322 collectVisibleLayers(refreshArgs, coverage);
323
324 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700325 const ui::Transform& tr = outputState.transform;
326 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800327 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
328
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700329 outputState.undefinedRegion = undefinedRegion;
330 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800331}
332
333void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
334 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800335 // Evaluate the layers from front to back to determine what is visible. This
336 // also incrementally calculates the coverage information for each layer as
337 // well as the entire output.
338 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700339 // Incrementally process the coverage for each layer
340 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800341
342 // TODO(b/121291683): Stop early if the output is completely covered and
343 // no more layers could even be visible underneath the ones on top.
344 }
345
Lloyd Pique01c77c12019-04-17 12:48:32 -0700346 setReleasedLayers(refreshArgs);
347
348 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800349
350 // Generate a simple Z-order values to each visible output layer
351 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700352 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800353 outputLayer->editState().z = zOrder++;
354 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800355}
356
Lloyd Pique01c77c12019-04-17 12:48:32 -0700357void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
358 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800359 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
360 auto layerFE = layer->getLayerFE();
361 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700362 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800363 }
364
365 // Ensure we have a snapshot of the basic geometry layer state. Limit the
366 // snapshots to once per frame for each candidate layer, as layers may
367 // appear on multiple outputs.
368 if (!coverage.latchedLayers.count(layerFE)) {
369 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700370 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800371 compositionengine::LayerFE::StateSubset::BasicGeometry);
372 }
373
374 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700375 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800376
377 // Only consider the layers on the given layer stack
378 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700379 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380 }
381
382 /*
383 * opaqueRegion: area of a surface that is fully opaque.
384 */
385 Region opaqueRegion;
386
387 /*
388 * visibleRegion: area of a surface that is visible on screen and not fully
389 * transparent. This is essentially the layer's footprint minus the opaque
390 * regions above it. Areas covered by a translucent surface are considered
391 * visible.
392 */
393 Region visibleRegion;
394
395 /*
396 * coveredRegion: area of a surface that is covered by all visible regions
397 * above it (which includes the translucent areas).
398 */
399 Region coveredRegion;
400
401 /*
402 * transparentRegion: area of a surface that is hinted to be completely
403 * transparent. This is only used to tell when the layer has no visible non-
404 * transparent regions and can be removed from the layer list. It does not
405 * affect the visibleRegion of this layer or any layers beneath it. The hint
406 * may not be correct if apps don't respect the SurfaceView restrictions
407 * (which, sadly, some don't).
408 */
409 Region transparentRegion;
410
411 // handle hidden surfaces by setting the visible region to empty
412 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700413 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800414 }
415
416 const ui::Transform& tr = layerFEState.geomLayerTransform;
417
418 // Get the visible region
419 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
420 // for computations like this?
421 visibleRegion.set(Rect(tr.transform(layerFEState.geomLayerBounds)));
422
423 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700424 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800425 }
426
427 // Remove the transparent area from the visible region
428 if (!layerFEState.isOpaque) {
429 if (tr.preserveRects()) {
430 // transform the transparent region
431 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
432 } else {
433 // transformation too complex, can't do the
434 // transparent region optimization.
435 transparentRegion.clear();
436 }
437 }
438
439 // compute the opaque region
440 const int32_t layerOrientation = tr.getOrientation();
441 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
442 // If we one of the simple category of transforms (0/90/180/270 rotation
443 // + any flip), then the opaque region is the layer's footprint.
444 // Otherwise we don't try and compute the opaque region since there may
445 // be errors at the edges, and we treat the entire layer as
446 // translucent.
447 opaqueRegion = visibleRegion;
448 }
449
450 // Clip the covered region to the visible region
451 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
452
453 // Update accumAboveCoveredLayers for next (lower) layer
454 coverage.aboveCoveredLayers.orSelf(visibleRegion);
455
456 // subtract the opaque region covered by the layers above us
457 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
458
459 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700460 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461 }
462
463 // Get coverage information for the layer as previously displayed,
464 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700465 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
466 auto prevOutputLayer =
467 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800468
469 // Get coverage information for the layer as previously displayed
470 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
471 const Region kEmptyRegion;
472 const Region& oldVisibleRegion =
473 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
474 const Region& oldCoveredRegion =
475 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
476
477 // compute this layer's dirty region
478 Region dirty;
479 if (layerFEState.contentDirty) {
480 // we need to invalidate the whole region
481 dirty = visibleRegion;
482 // as well, as the old visible region
483 dirty.orSelf(oldVisibleRegion);
484 } else {
485 /* compute the exposed region:
486 * the exposed region consists of two components:
487 * 1) what's VISIBLE now and was COVERED before
488 * 2) what's EXPOSED now less what was EXPOSED before
489 *
490 * note that (1) is conservative, we start with the whole visible region
491 * but only keep what used to be covered by something -- which mean it
492 * may have been exposed.
493 *
494 * (2) handles areas that were not covered by anything but got exposed
495 * because of a resize.
496 *
497 */
498 const Region newExposed = visibleRegion - coveredRegion;
499 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
500 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
501 }
502 dirty.subtractSelf(coverage.aboveOpaqueLayers);
503
504 // accumulate to the screen dirty region
505 coverage.dirtyRegion.orSelf(dirty);
506
507 // Update accumAboveOpaqueLayers for next (lower) layer
508 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
509
510 // Compute the visible non-transparent region
511 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
512
513 // Peform the final check to see if this layer is visible on this output
514 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700515 const auto& outputState = getState();
516 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
517 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800518 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700519 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800520 }
521
522 // The layer is visible. Either reuse the existing outputLayer if we have
523 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700524 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800525
526 // Store the layer coverage information into the layer state as some of it
527 // is useful later.
528 auto& outputLayerState = result->editState();
529 outputLayerState.visibleRegion = visibleRegion;
530 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
531 outputLayerState.coveredRegion = coveredRegion;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700532 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
533 outputLayerState.visibleRegion.intersect(outputState.viewport));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800534}
535
536void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
537 // The base class does nothing with this call.
538}
539
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800540void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700541 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700542 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800543 args.updatingGeometryThisFrame
544 ? LayerFE::StateSubset::GeometryAndContent
545 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800546 }
547}
548
549void Output::updateAndWriteCompositionState(
550 const compositionengine::CompositionRefreshArgs& refreshArgs) {
551 ATRACE_CALL();
552 ALOGV(__FUNCTION__);
553
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800554 if (!getState().isEnabled) {
555 return;
556 }
557
Lloyd Pique01c77c12019-04-17 12:48:32 -0700558 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700559 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
560 refreshArgs.devOptForceClientComposition);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800561
562 // Send the updated state to the HWC, if appropriate.
563 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
564 }
565}
566
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800567void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
568 setColorProfile(pickColorProfile(refreshArgs));
569}
570
571// Returns a data space that fits all visible layers. The returned data space
572// can only be one of
573// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
574// - Dataspace::DISPLAY_P3
575// - Dataspace::DISPLAY_BT2020
576// The returned HDR data space is one of
577// - Dataspace::UNKNOWN
578// - Dataspace::BT2020_HLG
579// - Dataspace::BT2020_PQ
580ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
581 bool* outIsHdrClientComposition) const {
582 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
583 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
584
Lloyd Pique01c77c12019-04-17 12:48:32 -0700585 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700586 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800587 case ui::Dataspace::V0_SCRGB:
588 case ui::Dataspace::V0_SCRGB_LINEAR:
589 case ui::Dataspace::BT2020:
590 case ui::Dataspace::BT2020_ITU:
591 case ui::Dataspace::BT2020_LINEAR:
592 case ui::Dataspace::DISPLAY_BT2020:
593 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
594 break;
595 case ui::Dataspace::DISPLAY_P3:
596 bestDataSpace = ui::Dataspace::DISPLAY_P3;
597 break;
598 case ui::Dataspace::BT2020_PQ:
599 case ui::Dataspace::BT2020_ITU_PQ:
600 bestDataSpace = ui::Dataspace::DISPLAY_P3;
601 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700602 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800603 break;
604 case ui::Dataspace::BT2020_HLG:
605 case ui::Dataspace::BT2020_ITU_HLG:
606 bestDataSpace = ui::Dataspace::DISPLAY_P3;
607 // When there's mixed PQ content and HLG content, we set the HDR
608 // data space to be BT2020_PQ and convert HLG to PQ.
609 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
610 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
611 }
612 break;
613 default:
614 break;
615 }
616 }
617
618 return bestDataSpace;
619}
620
621compositionengine::Output::ColorProfile Output::pickColorProfile(
622 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
623 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
624 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
625 ui::RenderIntent::COLORIMETRIC,
626 refreshArgs.colorSpaceAgnosticDataspace};
627 }
628
629 ui::Dataspace hdrDataSpace;
630 bool isHdrClientComposition = false;
631 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
632
633 switch (refreshArgs.forceOutputColorMode) {
634 case ui::ColorMode::SRGB:
635 bestDataSpace = ui::Dataspace::V0_SRGB;
636 break;
637 case ui::ColorMode::DISPLAY_P3:
638 bestDataSpace = ui::Dataspace::DISPLAY_P3;
639 break;
640 default:
641 break;
642 }
643
644 // respect hdrDataSpace only when there is no legacy HDR support
645 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
646 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
647 if (isHdr) {
648 bestDataSpace = hdrDataSpace;
649 }
650
651 ui::RenderIntent intent;
652 switch (refreshArgs.outputColorSetting) {
653 case OutputColorSetting::kManaged:
654 case OutputColorSetting::kUnmanaged:
655 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
656 : ui::RenderIntent::COLORIMETRIC;
657 break;
658 case OutputColorSetting::kEnhanced:
659 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
660 break;
661 default: // vendor display color setting
662 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
663 break;
664 }
665
666 ui::ColorMode outMode;
667 ui::Dataspace outDataSpace;
668 ui::RenderIntent outRenderIntent;
669 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
670 &outRenderIntent);
671
672 return ColorProfile{outMode, outDataSpace, outRenderIntent,
673 refreshArgs.colorSpaceAgnosticDataspace};
674}
675
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800676void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700677 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800678 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700679 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700680 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800681
682 // If nothing has changed (!dirty), don't recompose.
683 // If something changed, but we don't currently have any visible layers,
684 // and didn't when we last did a composition, then skip it this time.
685 // The second rule does two things:
686 // - When all layers are removed from a display, we'll emit one black
687 // frame, then nothing more until we get new layers.
688 // - When a display is created with a private layer stack, we won't
689 // emit any black frames until a layer is added to the layer stack.
690 const bool mustRecompose = dirty && !(empty && wasEmpty);
691
692 const char flagPrefix[] = {'-', '+'};
693 static_cast<void>(flagPrefix);
694 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
695 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
696 flagPrefix[empty], flagPrefix[wasEmpty]);
697
698 mRenderSurface->beginFrame(mustRecompose);
699
700 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700701 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800702 }
703}
704
Lloyd Pique66d68602019-02-13 14:23:31 -0800705void Output::prepareFrame() {
706 ATRACE_CALL();
707 ALOGV(__FUNCTION__);
708
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700709 const auto& outputState = getState();
710 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800711 return;
712 }
713
714 chooseCompositionStrategy();
715
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700716 mRenderSurface->prepareFrame(outputState.usesClientComposition,
717 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800718}
719
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800720void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
721 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
722 return;
723 }
724
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700725 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800726 // transform the dirty region into this screen's coordinate space
727 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
728 if (!dirtyRegion.isEmpty()) {
729 base::unique_fd readyFence;
730 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800731 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800732
733 mRenderSurface->queueBuffer(std::move(readyFence));
734 }
735 }
736
737 postFramebuffer();
738
739 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
740
741 prepareFrame();
742}
743
Lloyd Piqued3d69882019-02-28 16:03:46 -0800744void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
745 ATRACE_CALL();
746 ALOGV(__FUNCTION__);
747
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700748 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800749 return;
750 }
751
752 // Repaint the framebuffer (if needed), getting the optional fence for when
753 // the composition completes.
754 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
755 if (!optReadyFence) {
756 return;
757 }
758
759 // swap buffers (presentation)
760 mRenderSurface->queueBuffer(std::move(*optReadyFence));
761}
762
763std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800764 ATRACE_CALL();
765 ALOGV(__FUNCTION__);
766
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700767 const auto& outputState = getState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800768 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700769 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800770 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800771 if (!hasClientComposition) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800772 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800773 }
774
775 ALOGV("hasClientComposition");
776
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700777 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800778 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
779
780 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700781 clientCompositionDisplay.physicalDisplay = outputState.scissor;
782 clientCompositionDisplay.clip = outputState.scissor;
783 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
784 clientCompositionDisplay.orientation = outputState.orientation;
785 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
786 ? outputState.dataspace
787 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800788 clientCompositionDisplay.maxLuminance =
789 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
790
791 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700792 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
793 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800794 }
795
796 // Note: Updated by generateClientCompositionRequests
797 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
798
799 // Generate the client composition requests for the layers on this output.
800 std::vector<renderengine::LayerSettings> clientCompositionLayers =
801 generateClientCompositionRequests(supportsProtectedContent,
802 clientCompositionDisplay.clearRegion);
803 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
804
805 // If we the display is secure, protected content support is enabled, and at
806 // least one layer has protected content, we need to use a secure back
807 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700808 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700809 auto layers = getOutputLayersOrderedByZ();
810 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
811 return layer->getLayer().getFEState().hasProtectedContent;
812 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800813 if (needsProtected != renderEngine.isProtected()) {
814 renderEngine.useProtectedContext(needsProtected);
815 }
816 if (needsProtected != mRenderSurface->isProtected() &&
817 needsProtected == renderEngine.isProtected()) {
818 mRenderSurface->setProtected(needsProtected);
819 }
820 }
821
822 base::unique_fd fd;
823 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
824 if (buf == nullptr) {
825 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
826 "client composition for this frame",
827 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800828 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800829 }
830
831 // We boost GPU frequency here because there will be color spaces conversion
832 // and it's expensive. We boost the GPU frequency so that GPU composition can
833 // finish in time. We must reset GPU frequency afterwards, because high frequency
834 // consumes extra battery.
835 const bool expensiveRenderingExpected =
836 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
837 if (expensiveRenderingExpected) {
838 setExpensiveRenderingExpected(true);
839 }
840
841 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
842 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800843 &readyFence);
Lloyd Pique688abd42019-02-15 15:42:24 -0800844
845 if (expensiveRenderingExpected) {
846 setExpensiveRenderingExpected(false);
847 }
848
Lloyd Piqued3d69882019-02-28 16:03:46 -0800849 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800850}
851
852std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
853 bool supportsProtectedContent, Region& clearRegion) {
854 std::vector<renderengine::LayerSettings> clientCompositionLayers;
855 ALOGV("Rendering client layers");
856
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700857 const auto& outputState = getState();
858 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800859 const bool useIdentityTransform = false;
860 bool firstLayer = true;
861 // Used when a layer clears part of the buffer.
862 Region dummyRegion;
863
Lloyd Pique01c77c12019-04-17 12:48:32 -0700864 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800865 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700866 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800867 auto& layerFE = layer->getLayerFE();
868
Lloyd Piquea2468662019-03-07 21:31:06 -0800869 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800870 ALOGV("Layer: %s", layerFE.getDebugName());
871 if (clip.isEmpty()) {
872 ALOGV(" Skipping for empty clip");
873 firstLayer = false;
874 continue;
875 }
876
877 bool clientComposition = layer->requiresClientComposition();
878
879 // We clear the client target for non-client composed layers if
880 // requested by the HWC. We skip this if the layer is not an opaque
881 // rectangle, as by definition the layer must blend with whatever is
882 // underneath. We also skip the first layer as the buffer target is
883 // guaranteed to start out cleared.
884 bool clearClientComposition =
885 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
886
887 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
888
889 if (clientComposition || clearClientComposition) {
890 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
891 clip,
892 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700893 layer->needsFiltering() || outputState.needsFiltering,
894 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800895 supportsProtectedContent,
896 clientComposition ? clearRegion : dummyRegion,
897 };
898 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700899 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800900 auto& layerSettings = *result;
901 layerSettings.source.buffer.buffer = nullptr;
902 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
903 layerSettings.alpha = half(0.0);
904 layerSettings.disableBlending = true;
905 }
906
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800907 layer->editState().clientCompositionTimestamp = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -0800908 clientCompositionLayers.push_back(*result);
909 }
910 }
911
912 firstLayer = false;
913 }
914
915 return clientCompositionLayers;
916}
917
918void Output::appendRegionFlashRequests(
919 const Region& flashRegion,
920 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
921 if (flashRegion.isEmpty()) {
922 return;
923 }
924
925 renderengine::LayerSettings layerSettings;
926 layerSettings.source.buffer.buffer = nullptr;
927 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
928 layerSettings.alpha = half(1.0);
929
930 for (const auto& rect : flashRegion) {
931 layerSettings.geometry.boundaries = rect.toFloatRect();
932 clientCompositionLayers.push_back(layerSettings);
933 }
934}
935
936void Output::setExpensiveRenderingExpected(bool) {
937 // The base class does nothing with this call.
938}
939
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800940void Output::postFramebuffer() {
941 ATRACE_CALL();
942 ALOGV(__FUNCTION__);
943
944 if (!getState().isEnabled) {
945 return;
946 }
947
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700948 auto& outputState = editState();
949 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -0800950 mRenderSurface->flip();
951
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800952 auto frame = presentAndGetFrameFences();
953
Lloyd Pique7d90ba52019-08-08 11:57:53 -0700954 mRenderSurface->onPresentDisplayCompleted();
955
Lloyd Pique01c77c12019-04-17 12:48:32 -0700956 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800957 // The layer buffer from the previous frame (if any) is released
958 // by HWC only when the release fence from this frame (if any) is
959 // signaled. Always get the release fence from HWC first.
960 sp<Fence> releaseFence = Fence::NO_FENCE;
961
962 if (auto hwcLayer = layer->getHwcLayer()) {
963 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
964 releaseFence = f->second;
965 }
966 }
967
968 // If the layer was client composited in the previous frame, we
969 // need to merge with the previous client target acquire fence.
970 // Since we do not track that, always merge with the current
971 // client target acquire fence when it is available, even though
972 // this is suboptimal.
973 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700974 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800975 releaseFence =
976 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
977 }
978
979 layer->getLayerFE().onLayerDisplayed(releaseFence);
980 }
981
982 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -0700983 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800984 // supply them with the present fence.
985 for (auto& weakLayer : mReleasedLayers) {
986 if (auto layer = weakLayer.promote(); layer != nullptr) {
987 layer->onLayerDisplayed(frame.presentFence);
988 }
989 }
990
991 // Clear out the released layers now that we're done with them.
992 mReleasedLayers.clear();
993}
994
Lloyd Pique32cbe282018-10-19 13:09:22 -0700995void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700996 auto& outputState = editState();
997 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700998}
999
Lloyd Pique66d68602019-02-13 14:23:31 -08001000void Output::chooseCompositionStrategy() {
1001 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001002 auto& outputState = editState();
1003 outputState.usesClientComposition = true;
1004 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001005}
1006
Lloyd Pique688abd42019-02-15 15:42:24 -08001007bool Output::getSkipColorTransform() const {
1008 return true;
1009}
1010
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001011compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1012 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001013 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001014 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1015 }
1016 return result;
1017}
1018
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001019} // namespace impl
1020} // namespace android::compositionengine