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