blob: c3e5cfde5a35b674bff3223389db0049951854b6 [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
78const std::string& Output::getName() const {
79 return mName;
80}
81
82void Output::setName(const std::string& name) {
83 mName = name;
84}
85
86void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070087 auto& outputState = editState();
88 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -070089 return;
90 }
91
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070092 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -070093 dirtyEntireOutput();
94}
95
96void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame,
97 const Rect& viewport, const Rect& scissor, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070098 auto& outputState = editState();
99 outputState.transform = transform;
100 outputState.orientation = orientation;
101 outputState.scissor = scissor;
102 outputState.frame = frame;
103 outputState.viewport = viewport;
104 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105
106 dirtyEntireOutput();
107}
108
Lloyd Pique688abd42019-02-15 15:42:24 -0800109// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700110void Output::setBounds(const ui::Size& size) {
111 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700112 // TODO(b/121291683): Rename outputState.size once more is moved.
113 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114
115 dirtyEntireOutput();
116}
117
Lloyd Piqueef36b002019-01-23 17:52:04 -0800118void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700119 auto& outputState = editState();
120 outputState.layerStackId = layerStackId;
121 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700122
123 dirtyEntireOutput();
124}
125
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800126void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700127 auto& colorTransformMatrix = editState().colorTransformMatrix;
128 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700129 return;
130 }
131
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700132 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800133
134 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700135}
136
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800137void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700138 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800139 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
140 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800141
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700142 auto& outputState = editState();
143 if (outputState.colorMode == colorProfile.mode &&
144 outputState.dataspace == colorProfile.dataspace &&
145 outputState.renderIntent == colorProfile.renderIntent &&
146 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800147 return;
148 }
149
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700150 outputState.colorMode = colorProfile.mode;
151 outputState.dataspace = colorProfile.dataspace;
152 outputState.renderIntent = colorProfile.renderIntent;
153 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700154
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800155 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700156
Lloyd Pique32cbe282018-10-19 13:09:22 -0700157 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800158 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
159 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800160
161 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700162}
163
164void Output::dump(std::string& out) const {
165 using android::base::StringAppendF;
166
167 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
168
169 out.append("\n ");
170
171 dumpBase(out);
172}
173
174void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700175 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700176
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700177 if (mDisplayColorProfile) {
178 mDisplayColorProfile->dump(out);
179 } else {
180 out.append(" No display color profile!\n");
181 }
182
Lloyd Pique31cb2942018-10-19 17:23:03 -0700183 if (mRenderSurface) {
184 mRenderSurface->dump(out);
185 } else {
186 out.append(" No render surface!\n");
187 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800188
Lloyd Pique01c77c12019-04-17 12:48:32 -0700189 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
190 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800191 if (!outputLayer) {
192 continue;
193 }
194 outputLayer->dump(out);
195 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700196}
197
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700198compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
199 return mDisplayColorProfile.get();
200}
201
202void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
203 mDisplayColorProfile = std::move(mode);
204}
205
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800206const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
207 return mReleasedLayers;
208}
209
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700210void Output::setDisplayColorProfileForTest(
211 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
212 mDisplayColorProfile = std::move(mode);
213}
214
Lloyd Pique31cb2942018-10-19 17:23:03 -0700215compositionengine::RenderSurface* Output::getRenderSurface() const {
216 return mRenderSurface.get();
217}
218
219void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
220 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700221 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700222
223 dirtyEntireOutput();
224}
225
226void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
227 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700228}
229
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000230Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700231 const auto& outputState = getState();
232 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000233 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700234 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700235 }
236 return dirty;
237}
238
Lloyd Piquec6687342019-03-07 21:34:57 -0800239bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800240 // The layerStackId's must match, and also the layer must not be internal
241 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700242 const auto& outputState = getState();
243 return layerStackId && (*layerStackId == outputState.layerStackId) &&
244 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700245}
246
Lloyd Pique66c20c42019-03-07 21:44:02 -0800247bool Output::belongsInOutput(const compositionengine::Layer* layer) const {
248 if (!layer) {
249 return false;
250 }
251
Lloyd Pique9755fb72019-03-26 14:44:40 -0700252 const auto& layerFEState = layer->getFEState();
Lloyd Pique66c20c42019-03-07 21:44:02 -0800253 return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly);
254}
255
Lloyd Piquedf336d92019-03-07 21:38:42 -0800256std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Pique01c77c12019-04-17 12:48:32 -0700257 const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800258 return impl::createOutputLayer(*this, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800259}
260
Lloyd Pique01c77c12019-04-17 12:48:32 -0700261compositionengine::OutputLayer* Output::getOutputLayerForLayer(
262 compositionengine::Layer* layer) const {
263 auto index = findCurrentOutputLayerForLayer(layer);
264 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800265}
266
Lloyd Pique01c77c12019-04-17 12:48:32 -0700267std::optional<size_t> Output::findCurrentOutputLayerForLayer(
268 compositionengine::Layer* layer) const {
269 for (size_t i = 0; i < getOutputLayerCount(); i++) {
270 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
271 if (outputLayer && &outputLayer->getLayer() == layer) {
272 return i;
273 }
274 }
275 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800276}
277
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800278void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
279 mReleasedLayers = std::move(layers);
280}
281
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800282void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
283 LayerFESet& geomSnapshots) {
284 ATRACE_CALL();
285 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800286
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800287 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800288}
289
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800290void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800291 ATRACE_CALL();
292 ALOGV(__FUNCTION__);
293
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800294 updateColorProfile(refreshArgs);
295 updateAndWriteCompositionState(refreshArgs);
296 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800297 beginFrame();
298 prepareFrame();
299 devOptRepaintFlash(refreshArgs);
300 finishFrame(refreshArgs);
301 postFramebuffer();
302}
303
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800304void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
305 LayerFESet& layerFESet) {
306 ATRACE_CALL();
307 ALOGV(__FUNCTION__);
308
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700309 auto& outputState = editState();
310
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800311 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700312 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800313 return;
314 }
315
316 // Process the layers to determine visibility and coverage
317 compositionengine::Output::CoverageState coverage{layerFESet};
318 collectVisibleLayers(refreshArgs, coverage);
319
320 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700321 const ui::Transform& tr = outputState.transform;
322 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800323 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
324
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700325 outputState.undefinedRegion = undefinedRegion;
326 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800327}
328
329void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
330 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800331 // Evaluate the layers from front to back to determine what is visible. This
332 // also incrementally calculates the coverage information for each layer as
333 // well as the entire output.
334 for (auto& layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700335 // Incrementally process the coverage for each layer
336 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800337
338 // TODO(b/121291683): Stop early if the output is completely covered and
339 // no more layers could even be visible underneath the ones on top.
340 }
341
Lloyd Pique01c77c12019-04-17 12:48:32 -0700342 setReleasedLayers(refreshArgs);
343
344 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800345
346 // Generate a simple Z-order values to each visible output layer
347 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700348 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800349 outputLayer->editState().z = zOrder++;
350 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800351}
352
Lloyd Pique01c77c12019-04-17 12:48:32 -0700353void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer,
354 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800355 // Note: Converts a wp<LayerFE> to a sp<LayerFE>
356 auto layerFE = layer->getLayerFE();
357 if (layerFE == nullptr) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700358 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800359 }
360
361 // Ensure we have a snapshot of the basic geometry layer state. Limit the
362 // snapshots to once per frame for each candidate layer, as layers may
363 // appear on multiple outputs.
364 if (!coverage.latchedLayers.count(layerFE)) {
365 coverage.latchedLayers.insert(layerFE);
Lloyd Pique9755fb72019-03-26 14:44:40 -0700366 layerFE->latchCompositionState(layer->editFEState(),
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800367 compositionengine::LayerFE::StateSubset::BasicGeometry);
368 }
369
370 // Obtain a read-only reference to the front-end layer state
Lloyd Pique9755fb72019-03-26 14:44:40 -0700371 const auto& layerFEState = layer->getFEState();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800372
373 // Only consider the layers on the given layer stack
374 if (!belongsInOutput(layer.get())) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700375 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800376 }
377
378 /*
379 * opaqueRegion: area of a surface that is fully opaque.
380 */
381 Region opaqueRegion;
382
383 /*
384 * visibleRegion: area of a surface that is visible on screen and not fully
385 * transparent. This is essentially the layer's footprint minus the opaque
386 * regions above it. Areas covered by a translucent surface are considered
387 * visible.
388 */
389 Region visibleRegion;
390
391 /*
392 * coveredRegion: area of a surface that is covered by all visible regions
393 * above it (which includes the translucent areas).
394 */
395 Region coveredRegion;
396
397 /*
398 * transparentRegion: area of a surface that is hinted to be completely
399 * transparent. This is only used to tell when the layer has no visible non-
400 * transparent regions and can be removed from the layer list. It does not
401 * affect the visibleRegion of this layer or any layers beneath it. The hint
402 * may not be correct if apps don't respect the SurfaceView restrictions
403 * (which, sadly, some don't).
404 */
405 Region transparentRegion;
406
407 // handle hidden surfaces by setting the visible region to empty
408 if (CC_UNLIKELY(!layerFEState.isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700409 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800410 }
411
412 const ui::Transform& tr = layerFEState.geomLayerTransform;
413
414 // Get the visible region
415 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
416 // for computations like this?
417 visibleRegion.set(Rect(tr.transform(layerFEState.geomLayerBounds)));
418
419 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700420 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800421 }
422
423 // Remove the transparent area from the visible region
424 if (!layerFEState.isOpaque) {
425 if (tr.preserveRects()) {
426 // transform the transparent region
427 transparentRegion = tr.transform(layerFEState.transparentRegionHint);
428 } else {
429 // transformation too complex, can't do the
430 // transparent region optimization.
431 transparentRegion.clear();
432 }
433 }
434
435 // compute the opaque region
436 const int32_t layerOrientation = tr.getOrientation();
437 if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
438 // If we one of the simple category of transforms (0/90/180/270 rotation
439 // + any flip), then the opaque region is the layer's footprint.
440 // Otherwise we don't try and compute the opaque region since there may
441 // be errors at the edges, and we treat the entire layer as
442 // translucent.
443 opaqueRegion = visibleRegion;
444 }
445
446 // Clip the covered region to the visible region
447 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
448
449 // Update accumAboveCoveredLayers for next (lower) layer
450 coverage.aboveCoveredLayers.orSelf(visibleRegion);
451
452 // subtract the opaque region covered by the layers above us
453 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
454
455 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700456 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800457 }
458
459 // Get coverage information for the layer as previously displayed,
460 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700461 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get());
462 auto prevOutputLayer =
463 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800464
465 // Get coverage information for the layer as previously displayed
466 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
467 const Region kEmptyRegion;
468 const Region& oldVisibleRegion =
469 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
470 const Region& oldCoveredRegion =
471 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
472
473 // compute this layer's dirty region
474 Region dirty;
475 if (layerFEState.contentDirty) {
476 // we need to invalidate the whole region
477 dirty = visibleRegion;
478 // as well, as the old visible region
479 dirty.orSelf(oldVisibleRegion);
480 } else {
481 /* compute the exposed region:
482 * the exposed region consists of two components:
483 * 1) what's VISIBLE now and was COVERED before
484 * 2) what's EXPOSED now less what was EXPOSED before
485 *
486 * note that (1) is conservative, we start with the whole visible region
487 * but only keep what used to be covered by something -- which mean it
488 * may have been exposed.
489 *
490 * (2) handles areas that were not covered by anything but got exposed
491 * because of a resize.
492 *
493 */
494 const Region newExposed = visibleRegion - coveredRegion;
495 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
496 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
497 }
498 dirty.subtractSelf(coverage.aboveOpaqueLayers);
499
500 // accumulate to the screen dirty region
501 coverage.dirtyRegion.orSelf(dirty);
502
503 // Update accumAboveOpaqueLayers for next (lower) layer
504 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
505
506 // Compute the visible non-transparent region
507 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
508
509 // Peform the final check to see if this layer is visible on this output
510 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700511 const auto& outputState = getState();
512 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
513 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800514 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700515 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800516 }
517
518 // The layer is visible. Either reuse the existing outputLayer if we have
519 // one, or create a new one if we do not.
Lloyd Pique01c77c12019-04-17 12:48:32 -0700520 auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800521
522 // Store the layer coverage information into the layer state as some of it
523 // is useful later.
524 auto& outputLayerState = result->editState();
525 outputLayerState.visibleRegion = visibleRegion;
526 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
527 outputLayerState.coveredRegion = coveredRegion;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700528 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
529 outputLayerState.visibleRegion.intersect(outputState.viewport));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800530}
531
532void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
533 // The base class does nothing with this call.
534}
535
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800536void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700537 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700538 layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(),
Lloyd Piquec6687342019-03-07 21:34:57 -0800539 args.updatingGeometryThisFrame
540 ? LayerFE::StateSubset::GeometryAndContent
541 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800542 }
543}
544
545void Output::updateAndWriteCompositionState(
546 const compositionengine::CompositionRefreshArgs& refreshArgs) {
547 ATRACE_CALL();
548 ALOGV(__FUNCTION__);
549
Lloyd Pique01c77c12019-04-17 12:48:32 -0700550 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800551 if (refreshArgs.devOptForceClientComposition) {
552 layer->editState().forceClientComposition = true;
553 }
554
555 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame);
556
557 // Send the updated state to the HWC, if appropriate.
558 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
559 }
560}
561
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800562void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
563 setColorProfile(pickColorProfile(refreshArgs));
564}
565
566// Returns a data space that fits all visible layers. The returned data space
567// can only be one of
568// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
569// - Dataspace::DISPLAY_P3
570// - Dataspace::DISPLAY_BT2020
571// The returned HDR data space is one of
572// - Dataspace::UNKNOWN
573// - Dataspace::BT2020_HLG
574// - Dataspace::BT2020_PQ
575ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
576 bool* outIsHdrClientComposition) const {
577 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
578 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
579
Lloyd Pique01c77c12019-04-17 12:48:32 -0700580 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique9755fb72019-03-26 14:44:40 -0700581 switch (layer->getLayer().getFEState().dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800582 case ui::Dataspace::V0_SCRGB:
583 case ui::Dataspace::V0_SCRGB_LINEAR:
584 case ui::Dataspace::BT2020:
585 case ui::Dataspace::BT2020_ITU:
586 case ui::Dataspace::BT2020_LINEAR:
587 case ui::Dataspace::DISPLAY_BT2020:
588 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
589 break;
590 case ui::Dataspace::DISPLAY_P3:
591 bestDataSpace = ui::Dataspace::DISPLAY_P3;
592 break;
593 case ui::Dataspace::BT2020_PQ:
594 case ui::Dataspace::BT2020_ITU_PQ:
595 bestDataSpace = ui::Dataspace::DISPLAY_P3;
596 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700597 *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800598 break;
599 case ui::Dataspace::BT2020_HLG:
600 case ui::Dataspace::BT2020_ITU_HLG:
601 bestDataSpace = ui::Dataspace::DISPLAY_P3;
602 // When there's mixed PQ content and HLG content, we set the HDR
603 // data space to be BT2020_PQ and convert HLG to PQ.
604 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
605 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
606 }
607 break;
608 default:
609 break;
610 }
611 }
612
613 return bestDataSpace;
614}
615
616compositionengine::Output::ColorProfile Output::pickColorProfile(
617 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
618 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
619 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
620 ui::RenderIntent::COLORIMETRIC,
621 refreshArgs.colorSpaceAgnosticDataspace};
622 }
623
624 ui::Dataspace hdrDataSpace;
625 bool isHdrClientComposition = false;
626 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
627
628 switch (refreshArgs.forceOutputColorMode) {
629 case ui::ColorMode::SRGB:
630 bestDataSpace = ui::Dataspace::V0_SRGB;
631 break;
632 case ui::ColorMode::DISPLAY_P3:
633 bestDataSpace = ui::Dataspace::DISPLAY_P3;
634 break;
635 default:
636 break;
637 }
638
639 // respect hdrDataSpace only when there is no legacy HDR support
640 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
641 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
642 if (isHdr) {
643 bestDataSpace = hdrDataSpace;
644 }
645
646 ui::RenderIntent intent;
647 switch (refreshArgs.outputColorSetting) {
648 case OutputColorSetting::kManaged:
649 case OutputColorSetting::kUnmanaged:
650 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
651 : ui::RenderIntent::COLORIMETRIC;
652 break;
653 case OutputColorSetting::kEnhanced:
654 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
655 break;
656 default: // vendor display color setting
657 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
658 break;
659 }
660
661 ui::ColorMode outMode;
662 ui::Dataspace outDataSpace;
663 ui::RenderIntent outRenderIntent;
664 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
665 &outRenderIntent);
666
667 return ColorProfile{outMode, outDataSpace, outRenderIntent,
668 refreshArgs.colorSpaceAgnosticDataspace};
669}
670
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800671void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700672 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800673 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700674 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700675 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800676
677 // If nothing has changed (!dirty), don't recompose.
678 // If something changed, but we don't currently have any visible layers,
679 // and didn't when we last did a composition, then skip it this time.
680 // The second rule does two things:
681 // - When all layers are removed from a display, we'll emit one black
682 // frame, then nothing more until we get new layers.
683 // - When a display is created with a private layer stack, we won't
684 // emit any black frames until a layer is added to the layer stack.
685 const bool mustRecompose = dirty && !(empty && wasEmpty);
686
687 const char flagPrefix[] = {'-', '+'};
688 static_cast<void>(flagPrefix);
689 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
690 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
691 flagPrefix[empty], flagPrefix[wasEmpty]);
692
693 mRenderSurface->beginFrame(mustRecompose);
694
695 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700696 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800697 }
698}
699
Lloyd Pique66d68602019-02-13 14:23:31 -0800700void Output::prepareFrame() {
701 ATRACE_CALL();
702 ALOGV(__FUNCTION__);
703
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700704 const auto& outputState = getState();
705 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800706 return;
707 }
708
709 chooseCompositionStrategy();
710
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700711 mRenderSurface->prepareFrame(outputState.usesClientComposition,
712 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800713}
714
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800715void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
716 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
717 return;
718 }
719
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700720 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800721 // transform the dirty region into this screen's coordinate space
722 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
723 if (!dirtyRegion.isEmpty()) {
724 base::unique_fd readyFence;
725 // redraw the whole screen
Lloyd Piqued3d69882019-02-28 16:03:46 -0800726 static_cast<void>(composeSurfaces(dirtyRegion));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800727
728 mRenderSurface->queueBuffer(std::move(readyFence));
729 }
730 }
731
732 postFramebuffer();
733
734 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
735
736 prepareFrame();
737}
738
Lloyd Piqued3d69882019-02-28 16:03:46 -0800739void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) {
740 ATRACE_CALL();
741 ALOGV(__FUNCTION__);
742
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700743 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800744 return;
745 }
746
747 // Repaint the framebuffer (if needed), getting the optional fence for when
748 // the composition completes.
749 auto optReadyFence = composeSurfaces(Region::INVALID_REGION);
750 if (!optReadyFence) {
751 return;
752 }
753
754 // swap buffers (presentation)
755 mRenderSurface->queueBuffer(std::move(*optReadyFence));
756}
757
758std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800759 ATRACE_CALL();
760 ALOGV(__FUNCTION__);
761
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700762 const auto& outputState = getState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800763 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700764 outputState.usesClientComposition};
Lloyd Piqued3d69882019-02-28 16:03:46 -0800765 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800766 if (!hasClientComposition) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800767 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800768 }
769
770 ALOGV("hasClientComposition");
771
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700772 auto& renderEngine = getCompositionEngine().getRenderEngine();
Lloyd Pique688abd42019-02-15 15:42:24 -0800773 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
774
775 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700776 clientCompositionDisplay.physicalDisplay = outputState.scissor;
777 clientCompositionDisplay.clip = outputState.scissor;
778 clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4();
779 clientCompositionDisplay.orientation = outputState.orientation;
780 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
781 ? outputState.dataspace
782 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800783 clientCompositionDisplay.maxLuminance =
784 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
785
786 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700787 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
788 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800789 }
790
791 // Note: Updated by generateClientCompositionRequests
792 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
793
794 // Generate the client composition requests for the layers on this output.
795 std::vector<renderengine::LayerSettings> clientCompositionLayers =
796 generateClientCompositionRequests(supportsProtectedContent,
797 clientCompositionDisplay.clearRegion);
798 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
799
800 // If we the display is secure, protected content support is enabled, and at
801 // least one layer has protected content, we need to use a secure back
802 // buffer.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700803 if (outputState.isSecure && supportsProtectedContent) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700804 auto layers = getOutputLayersOrderedByZ();
805 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
806 return layer->getLayer().getFEState().hasProtectedContent;
807 });
Lloyd Pique688abd42019-02-15 15:42:24 -0800808 if (needsProtected != renderEngine.isProtected()) {
809 renderEngine.useProtectedContext(needsProtected);
810 }
811 if (needsProtected != mRenderSurface->isProtected() &&
812 needsProtected == renderEngine.isProtected()) {
813 mRenderSurface->setProtected(needsProtected);
814 }
815 }
816
817 base::unique_fd fd;
818 sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd);
819 if (buf == nullptr) {
820 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
821 "client composition for this frame",
822 mName.c_str());
Lloyd Piqued3d69882019-02-28 16:03:46 -0800823 return std::nullopt;
Lloyd Pique688abd42019-02-15 15:42:24 -0800824 }
825
826 // We boost GPU frequency here because there will be color spaces conversion
827 // and it's expensive. We boost the GPU frequency so that GPU composition can
828 // finish in time. We must reset GPU frequency afterwards, because high frequency
829 // consumes extra battery.
830 const bool expensiveRenderingExpected =
831 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3;
832 if (expensiveRenderingExpected) {
833 setExpensiveRenderingExpected(true);
834 }
835
836 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayers,
837 buf->getNativeBuffer(), /*useFramebufferCache=*/true, std::move(fd),
Lloyd Piqued3d69882019-02-28 16:03:46 -0800838 &readyFence);
Lloyd Pique688abd42019-02-15 15:42:24 -0800839
840 if (expensiveRenderingExpected) {
841 setExpensiveRenderingExpected(false);
842 }
843
Lloyd Piqued3d69882019-02-28 16:03:46 -0800844 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800845}
846
847std::vector<renderengine::LayerSettings> Output::generateClientCompositionRequests(
848 bool supportsProtectedContent, Region& clearRegion) {
849 std::vector<renderengine::LayerSettings> clientCompositionLayers;
850 ALOGV("Rendering client layers");
851
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700852 const auto& outputState = getState();
853 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800854 const bool useIdentityTransform = false;
855 bool firstLayer = true;
856 // Used when a layer clears part of the buffer.
857 Region dummyRegion;
858
Lloyd Pique01c77c12019-04-17 12:48:32 -0700859 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800860 const auto& layerState = layer->getState();
Lloyd Pique9755fb72019-03-26 14:44:40 -0700861 const auto& layerFEState = layer->getLayer().getFEState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800862 auto& layerFE = layer->getLayerFE();
863
Lloyd Piquea2468662019-03-07 21:31:06 -0800864 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800865 ALOGV("Layer: %s", layerFE.getDebugName());
866 if (clip.isEmpty()) {
867 ALOGV(" Skipping for empty clip");
868 firstLayer = false;
869 continue;
870 }
871
872 bool clientComposition = layer->requiresClientComposition();
873
874 // We clear the client target for non-client composed layers if
875 // requested by the HWC. We skip this if the layer is not an opaque
876 // rectangle, as by definition the layer must blend with whatever is
877 // underneath. We also skip the first layer as the buffer target is
878 // guaranteed to start out cleared.
879 bool clearClientComposition =
880 layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer;
881
882 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
883
884 if (clientComposition || clearClientComposition) {
885 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
886 clip,
887 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700888 layer->needsFiltering() || outputState.needsFiltering,
889 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800890 supportsProtectedContent,
891 clientComposition ? clearRegion : dummyRegion,
892 };
893 if (auto result = layerFE.prepareClientComposition(targetSettings)) {
Lloyd Piquec2d54d42019-08-28 18:04:21 -0700894 if (!clientComposition) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800895 auto& layerSettings = *result;
896 layerSettings.source.buffer.buffer = nullptr;
897 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
898 layerSettings.alpha = half(0.0);
899 layerSettings.disableBlending = true;
900 }
901
902 clientCompositionLayers.push_back(*result);
903 }
904 }
905
906 firstLayer = false;
907 }
908
909 return clientCompositionLayers;
910}
911
912void Output::appendRegionFlashRequests(
913 const Region& flashRegion,
914 std::vector<renderengine::LayerSettings>& clientCompositionLayers) {
915 if (flashRegion.isEmpty()) {
916 return;
917 }
918
919 renderengine::LayerSettings layerSettings;
920 layerSettings.source.buffer.buffer = nullptr;
921 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
922 layerSettings.alpha = half(1.0);
923
924 for (const auto& rect : flashRegion) {
925 layerSettings.geometry.boundaries = rect.toFloatRect();
926 clientCompositionLayers.push_back(layerSettings);
927 }
928}
929
930void Output::setExpensiveRenderingExpected(bool) {
931 // The base class does nothing with this call.
932}
933
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800934void Output::postFramebuffer() {
935 ATRACE_CALL();
936 ALOGV(__FUNCTION__);
937
938 if (!getState().isEnabled) {
939 return;
940 }
941
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700942 auto& outputState = editState();
943 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -0800944 mRenderSurface->flip();
945
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800946 auto frame = presentAndGetFrameFences();
947
Lloyd Pique7d90ba52019-08-08 11:57:53 -0700948 mRenderSurface->onPresentDisplayCompleted();
949
Lloyd Pique01c77c12019-04-17 12:48:32 -0700950 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800951 // The layer buffer from the previous frame (if any) is released
952 // by HWC only when the release fence from this frame (if any) is
953 // signaled. Always get the release fence from HWC first.
954 sp<Fence> releaseFence = Fence::NO_FENCE;
955
956 if (auto hwcLayer = layer->getHwcLayer()) {
957 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
958 releaseFence = f->second;
959 }
960 }
961
962 // If the layer was client composited in the previous frame, we
963 // need to merge with the previous client target acquire fence.
964 // Since we do not track that, always merge with the current
965 // client target acquire fence when it is available, even though
966 // this is suboptimal.
967 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700968 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800969 releaseFence =
970 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
971 }
972
973 layer->getLayerFE().onLayerDisplayed(releaseFence);
974 }
975
976 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -0700977 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800978 // supply them with the present fence.
979 for (auto& weakLayer : mReleasedLayers) {
980 if (auto layer = weakLayer.promote(); layer != nullptr) {
981 layer->onLayerDisplayed(frame.presentFence);
982 }
983 }
984
985 // Clear out the released layers now that we're done with them.
986 mReleasedLayers.clear();
987}
988
Lloyd Pique32cbe282018-10-19 13:09:22 -0700989void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700990 auto& outputState = editState();
991 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700992}
993
Lloyd Pique66d68602019-02-13 14:23:31 -0800994void Output::chooseCompositionStrategy() {
995 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700996 auto& outputState = editState();
997 outputState.usesClientComposition = true;
998 outputState.usesDeviceComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800999}
1000
Lloyd Pique688abd42019-02-15 15:42:24 -08001001bool Output::getSkipColorTransform() const {
1002 return true;
1003}
1004
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001005compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1006 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001007 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001008 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1009 }
1010 return result;
1011}
1012
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001013} // namespace impl
1014} // namespace android::compositionengine