blob: aa638b7d916f846858f6327365d396e7657afe5b [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
Lloyd Pique01c77c12019-04-17 12:48:32 -0700554 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700555 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
556 refreshArgs.devOptForceClientComposition);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800557
558 // Send the updated state to the HWC, if appropriate.
559 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
560 }
561}
562
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800563void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
564 setColorProfile(pickColorProfile(refreshArgs));
565}
566
567// Returns a data space that fits all visible layers. The returned data space
568// can only be one of
569// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
570// - Dataspace::DISPLAY_P3
571// - Dataspace::DISPLAY_BT2020
572// The returned HDR data space is one of
573// - Dataspace::UNKNOWN
574// - Dataspace::BT2020_HLG
575// - Dataspace::BT2020_PQ
576ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
577 bool* outIsHdrClientComposition) const {
578 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
579 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
580
Lloyd Pique01c77c12019-04-17 12:48:32 -0700581 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700582 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800583 case ui::Dataspace::V0_SCRGB:
584 case ui::Dataspace::V0_SCRGB_LINEAR:
585 case ui::Dataspace::BT2020:
586 case ui::Dataspace::BT2020_ITU:
587 case ui::Dataspace::BT2020_LINEAR:
588 case ui::Dataspace::DISPLAY_BT2020:
589 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
590 break;
591 case ui::Dataspace::DISPLAY_P3:
592 bestDataSpace = ui::Dataspace::DISPLAY_P3;
593 break;
594 case ui::Dataspace::BT2020_PQ:
595 case ui::Dataspace::BT2020_ITU_PQ:
596 bestDataSpace = ui::Dataspace::DISPLAY_P3;
597 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700598 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800599 break;
600 case ui::Dataspace::BT2020_HLG:
601 case ui::Dataspace::BT2020_ITU_HLG:
602 bestDataSpace = ui::Dataspace::DISPLAY_P3;
603 // When there's mixed PQ content and HLG content, we set the HDR
604 // data space to be BT2020_PQ and convert HLG to PQ.
605 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
606 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
607 }
608 break;
609 default:
610 break;
611 }
612 }
613
614 return bestDataSpace;
615}
616
617compositionengine::Output::ColorProfile Output::pickColorProfile(
618 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
619 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
620 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
621 ui::RenderIntent::COLORIMETRIC,
622 refreshArgs.colorSpaceAgnosticDataspace};
623 }
624
625 ui::Dataspace hdrDataSpace;
626 bool isHdrClientComposition = false;
627 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
628
629 switch (refreshArgs.forceOutputColorMode) {
630 case ui::ColorMode::SRGB:
631 bestDataSpace = ui::Dataspace::V0_SRGB;
632 break;
633 case ui::ColorMode::DISPLAY_P3:
634 bestDataSpace = ui::Dataspace::DISPLAY_P3;
635 break;
636 default:
637 break;
638 }
639
640 // respect hdrDataSpace only when there is no legacy HDR support
641 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
642 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
643 if (isHdr) {
644 bestDataSpace = hdrDataSpace;
645 }
646
647 ui::RenderIntent intent;
648 switch (refreshArgs.outputColorSetting) {
649 case OutputColorSetting::kManaged:
650 case OutputColorSetting::kUnmanaged:
651 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
652 : ui::RenderIntent::COLORIMETRIC;
653 break;
654 case OutputColorSetting::kEnhanced:
655 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
656 break;
657 default: // vendor display color setting
658 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
659 break;
660 }
661
662 ui::ColorMode outMode;
663 ui::Dataspace outDataSpace;
664 ui::RenderIntent outRenderIntent;
665 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
666 &outRenderIntent);
667
668 return ColorProfile{outMode, outDataSpace, outRenderIntent,
669 refreshArgs.colorSpaceAgnosticDataspace};
670}
671
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800672void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700673 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800674 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700675 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700676 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800677
678 // If nothing has changed (!dirty), don't recompose.
679 // If something changed, but we don't currently have any visible layers,
680 // and didn't when we last did a composition, then skip it this time.
681 // The second rule does two things:
682 // - When all layers are removed from a display, we'll emit one black
683 // frame, then nothing more until we get new layers.
684 // - When a display is created with a private layer stack, we won't
685 // emit any black frames until a layer is added to the layer stack.
686 const bool mustRecompose = dirty && !(empty && wasEmpty);
687
688 const char flagPrefix[] = {'-', '+'};
689 static_cast<void>(flagPrefix);
690 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
691 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
692 flagPrefix[empty], flagPrefix[wasEmpty]);
693
694 mRenderSurface->beginFrame(mustRecompose);
695
696 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700697 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800698 }
699}
700
Lloyd Pique66d68602019-02-13 14:23:31 -0800701void Output::prepareFrame() {
702 ATRACE_CALL();
703 ALOGV(__FUNCTION__);
704
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700705 const auto& outputState = getState();
706 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800707 return;
708 }
709
710 chooseCompositionStrategy();
711
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700712 mRenderSurface->prepareFrame(outputState.usesClientComposition,
713 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800714}
715
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800716void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
717 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
718 return;
719 }
720
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700721 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800722 // transform the dirty region into this screen's coordinate space
723 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
724 if (!dirtyRegion.isEmpty()) {
725 base::unique_fd readyFence;
726 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800727 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800728
729 mRenderSurface->queueBuffer(std::move(readyFence));
730 }
731 }
732
733 postFramebuffer();
734
735 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
736
737 prepareFrame();
738}
739
Lloyd Piqued3d69882019-02-28 16:03:46 -0800740void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
741 ATRACE_CALL();
742 ALOGV(__FUNCTION__);
743
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700744 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800745 return;
746 }
747
748 // Repaint the framebuffer (if needed), getting the optional fence for when
749 // the composition completes.
750 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
751 if (!optReadyFence) {
752 return;
753 }
754
755 // swap buffers (presentation)
756 mRenderSurface->queueBuffer(std::move(*optReadyFence));
757}
758
759std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800760 ATRACE_CALL();
761 ALOGV(__FUNCTION__);
762
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700763 const auto& outputState = getState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800764 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700765 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800766 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800767 if (!hasClientComposition) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800768 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800769 }
770
771 ALOGV("hasClientComposition");
772
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700773 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800774 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
775
776 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700777 clientCompositionDisplay.physicalDisplay = outputState.scissor;
778 clientCompositionDisplay.clip = outputState.scissor;
779 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
780 clientCompositionDisplay.orientation = outputState.orientation;
781 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
782 ? outputState.dataspace
783 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800784 clientCompositionDisplay.maxLuminance =
785 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
786
787 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700788 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
789 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800790 }
791
792 // Note: Updated by generateClientCompositionRequests
793 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
794
795 // Generate the client composition requests for the layers on this output.
796 std::vector<renderengine::LayerSettings> clientCompositionLayers =
797 generateClientCompositionRequests(supportsProtectedContent,
798 clientCompositionDisplay.clearRegion);
799 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
800
801 // If we the display is secure, protected content support is enabled, and at
802 // least one layer has protected content, we need to use a secure back
803 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700804 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700805 auto layers = getOutputLayersOrderedByZ();
806 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
807 return layer->getLayer().getFEState().hasProtectedContent;
808 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800809 if (needsProtected != renderEngine.isProtected()) {
810 renderEngine.useProtectedContext(needsProtected);
811 }
812 if (needsProtected != mRenderSurface->isProtected() &&
813 needsProtected == renderEngine.isProtected()) {
814 mRenderSurface->setProtected(needsProtected);
815 }
816 }
817
818 base::unique_fd fd;
819 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
820 if (buf == nullptr) {
821 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
822 "client composition for this frame",
823 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800824 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800825 }
826
827 // We boost GPU frequency here because there will be color spaces conversion
828 // and it's expensive. We boost the GPU frequency so that GPU composition can
829 // finish in time. We must reset GPU frequency afterwards, because high frequency
830 // consumes extra battery.
831 const bool expensiveRenderingExpected =
832 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
833 if (expensiveRenderingExpected) {
834 setExpensiveRenderingExpected(true);
835 }
836
837 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
838 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800839 &readyFence);
Lloyd Pique688abd42019-02-15 15:42:24 -0800840
841 if (expensiveRenderingExpected) {
842 setExpensiveRenderingExpected(false);
843 }
844
Lloyd Piqued3d69882019-02-28 16:03:46 -0800845 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800846}
847
848std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
849 bool supportsProtectedContent, Region& clearRegion) {
850 std::vector<renderengine::LayerSettings> clientCompositionLayers;
851 ALOGV("Rendering client layers");
852
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700853 const auto& outputState = getState();
854 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800855 const bool useIdentityTransform = false;
856 bool firstLayer = true;
857 // Used when a layer clears part of the buffer.
858 Region dummyRegion;
859
Lloyd Pique01c77c12019-04-17 12:48:32 -0700860 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800861 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700862 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800863 auto& layerFE = layer->getLayerFE();
864
Lloyd Piquea2468662019-03-07 21:31:06 -0800865 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800866 ALOGV("Layer: %s", layerFE.getDebugName());
867 if (clip.isEmpty()) {
868 ALOGV(" Skipping for empty clip");
869 firstLayer = false;
870 continue;
871 }
872
873 bool clientComposition = layer->requiresClientComposition();
874
875 // We clear the client target for non-client composed layers if
876 // requested by the HWC. We skip this if the layer is not an opaque
877 // rectangle, as by definition the layer must blend with whatever is
878 // underneath. We also skip the first layer as the buffer target is
879 // guaranteed to start out cleared.
880 bool clearClientComposition =
881 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
882
883 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
884
885 if (clientComposition || clearClientComposition) {
886 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
887 clip,
888 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700889 layer->needsFiltering() || outputState.needsFiltering,
890 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800891 supportsProtectedContent,
892 clientComposition ? clearRegion : dummyRegion,
893 };
894 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700895 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800896 auto& layerSettings = *result;
897 layerSettings.source.buffer.buffer = nullptr;
898 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
899 layerSettings.alpha = half(0.0);
900 layerSettings.disableBlending = true;
901 }
902
903 clientCompositionLayers.push_back(*result);
904 }
905 }
906
907 firstLayer = false;
908 }
909
910 return clientCompositionLayers;
911}
912
913void Output::appendRegionFlashRequests(
914 const Region& flashRegion,
915 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
916 if (flashRegion.isEmpty()) {
917 return;
918 }
919
920 renderengine::LayerSettings layerSettings;
921 layerSettings.source.buffer.buffer = nullptr;
922 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
923 layerSettings.alpha = half(1.0);
924
925 for (const auto& rect : flashRegion) {
926 layerSettings.geometry.boundaries = rect.toFloatRect();
927 clientCompositionLayers.push_back(layerSettings);
928 }
929}
930
931void Output::setExpensiveRenderingExpected(bool) {
932 // The base class does nothing with this call.
933}
934
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800935void Output::postFramebuffer() {
936 ATRACE_CALL();
937 ALOGV(__FUNCTION__);
938
939 if (!getState().isEnabled) {
940 return;
941 }
942
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700943 auto& outputState = editState();
944 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -0800945 mRenderSurface->flip();
946
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800947 auto frame = presentAndGetFrameFences();
948
Lloyd Pique7d90ba52019-08-08 11:57:53 -0700949 mRenderSurface->onPresentDisplayCompleted();
950
Lloyd Pique01c77c12019-04-17 12:48:32 -0700951 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800952 // The layer buffer from the previous frame (if any) is released
953 // by HWC only when the release fence from this frame (if any) is
954 // signaled. Always get the release fence from HWC first.
955 sp<Fence> releaseFence = Fence::NO_FENCE;
956
957 if (auto hwcLayer = layer->getHwcLayer()) {
958 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
959 releaseFence = f->second;
960 }
961 }
962
963 // If the layer was client composited in the previous frame, we
964 // need to merge with the previous client target acquire fence.
965 // Since we do not track that, always merge with the current
966 // client target acquire fence when it is available, even though
967 // this is suboptimal.
968 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700969 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800970 releaseFence =
971 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
972 }
973
974 layer->getLayerFE().onLayerDisplayed(releaseFence);
975 }
976
977 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -0700978 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800979 // supply them with the present fence.
980 for (auto& weakLayer : mReleasedLayers) {
981 if (auto layer = weakLayer.promote(); layer != nullptr) {
982 layer->onLayerDisplayed(frame.presentFence);
983 }
984 }
985
986 // Clear out the released layers now that we're done with them.
987 mReleasedLayers.clear();
988}
989
Lloyd Pique32cbe282018-10-19 13:09:22 -0700990void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700991 auto& outputState = editState();
992 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700993}
994
Lloyd Pique66d68602019-02-13 14:23:31 -0800995void Output::chooseCompositionStrategy() {
996 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700997 auto& outputState = editState();
998 outputState.usesClientComposition = true;
999 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001000}
1001
Lloyd Pique688abd42019-02-15 15:42:24 -08001002bool Output::getSkipColorTransform() const {
1003 return true;
1004}
1005
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001006compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1007 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001008 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001009 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1010 }
1011 return result;
1012}
1013
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001014} // namespace impl
1015} // namespace android::compositionengine