blob: b07c904917ccafa39da1ef81d2c76103cf389d25 [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 Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070024#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070025#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070029#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080030
31// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
34
Lloyd Pique688abd42019-02-15 15:42:24 -080035#include <renderengine/DisplaySettings.h>
36#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic pop // ignored "-Wconversion"
40
Lloyd Pique32cbe282018-10-19 13:09:22 -070041#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080042#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080043#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070044
Lloyd Pique688abd42019-02-15 15:42:24 -080045#include "TracedOrdinal.h"
46
Lloyd Piquefeb73d72018-12-04 17:23:44 -080047namespace android::compositionengine {
48
49Output::~Output() = default;
50
51namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070052
Lloyd Piquec29e4c62019-03-07 21:48:19 -080053namespace {
54
55template <typename T>
56class Reversed {
57public:
58 explicit Reversed(const T& container) : mContainer(container) {}
59 auto begin() { return mContainer.rbegin(); }
60 auto end() { return mContainer.rend(); }
61
62private:
63 const T& mContainer;
64};
65
66// Helper for enumerating over a container in reverse order
67template <typename T>
68Reversed<T> reversed(const T& c) {
69 return Reversed<T>(c);
70}
71
72} // namespace
73
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070074std::shared_ptr<Output> createOutput(
75 const compositionengine::CompositionEngine& compositionEngine) {
76 return createOutputTemplated<Output>(compositionEngine);
77}
Lloyd Pique32cbe282018-10-19 13:09:22 -070078
79Output::~Output() = default;
80
Lloyd Pique32cbe282018-10-19 13:09:22 -070081bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070082 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
83 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -070084}
85
Lloyd Pique6c564cf2019-05-17 17:31:36 -070086std::optional<DisplayId> Output::getDisplayId() const {
87 return {};
88}
89
Lloyd Pique32cbe282018-10-19 13:09:22 -070090const std::string& Output::getName() const {
91 return mName;
92}
93
94void Output::setName(const std::string& name) {
95 mName = name;
96}
97
98void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070099 auto& outputState = editState();
100 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700101 return;
102 }
103
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700104 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105 dirtyEntireOutput();
106}
107
Lloyd Pique0a456232020-01-16 17:51:13 -0800108void Output::setProjection(const ui::Transform& transform, uint32_t orientation, const Rect& frame,
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800109 const Rect& viewport, const Rect& sourceClip,
110 const Rect& destinationClip, bool needsFiltering) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700111 auto& outputState = editState();
112 outputState.transform = transform;
113 outputState.orientation = orientation;
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800114 outputState.sourceClip = sourceClip;
115 outputState.destinationClip = destinationClip;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700116 outputState.frame = frame;
117 outputState.viewport = viewport;
118 outputState.needsFiltering = needsFiltering;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119
120 dirtyEntireOutput();
121}
122
Lloyd Pique688abd42019-02-15 15:42:24 -0800123// TODO(b/121291683): Rename setSize() once more is moved.
Lloyd Pique31cb2942018-10-19 17:23:03 -0700124void Output::setBounds(const ui::Size& size) {
125 mRenderSurface->setDisplaySize(size);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700126 // TODO(b/121291683): Rename outputState.size once more is moved.
127 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700128
129 dirtyEntireOutput();
130}
131
Lloyd Piqueef36b002019-01-23 17:52:04 -0800132void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700133 auto& outputState = editState();
134 outputState.layerStackId = layerStackId;
135 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700136
137 dirtyEntireOutput();
138}
139
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800140void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700141 auto& colorTransformMatrix = editState().colorTransformMatrix;
142 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700143 return;
144 }
145
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700146 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800147
148 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700149}
150
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800151void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700152 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800153 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
154 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800155
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700156 auto& outputState = editState();
157 if (outputState.colorMode == colorProfile.mode &&
158 outputState.dataspace == colorProfile.dataspace &&
159 outputState.renderIntent == colorProfile.renderIntent &&
160 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800161 return;
162 }
163
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700164 outputState.colorMode = colorProfile.mode;
165 outputState.dataspace = colorProfile.dataspace;
166 outputState.renderIntent = colorProfile.renderIntent;
167 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700168
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800169 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700170
Lloyd Pique32cbe282018-10-19 13:09:22 -0700171 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800172 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
173 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800174
175 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700176}
177
178void Output::dump(std::string& out) const {
179 using android::base::StringAppendF;
180
181 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
182
183 out.append("\n ");
184
185 dumpBase(out);
186}
187
188void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700189 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700190
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700191 if (mDisplayColorProfile) {
192 mDisplayColorProfile->dump(out);
193 } else {
194 out.append(" No display color profile!\n");
195 }
196
Lloyd Pique31cb2942018-10-19 17:23:03 -0700197 if (mRenderSurface) {
198 mRenderSurface->dump(out);
199 } else {
200 out.append(" No render surface!\n");
201 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800202
Lloyd Pique01c77c12019-04-17 12:48:32 -0700203 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
204 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800205 if (!outputLayer) {
206 continue;
207 }
208 outputLayer->dump(out);
209 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700210}
211
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700212compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
213 return mDisplayColorProfile.get();
214}
215
216void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
217 mDisplayColorProfile = std::move(mode);
218}
219
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800220const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
221 return mReleasedLayers;
222}
223
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700224void Output::setDisplayColorProfileForTest(
225 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
226 mDisplayColorProfile = std::move(mode);
227}
228
Lloyd Pique31cb2942018-10-19 17:23:03 -0700229compositionengine::RenderSurface* Output::getRenderSurface() const {
230 return mRenderSurface.get();
231}
232
233void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
234 mRenderSurface = std::move(surface);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700235 editState().bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700236
237 dirtyEntireOutput();
238}
239
Vishnu Nair9b079a22020-01-21 14:36:08 -0800240void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
241 if (cacheSize == 0) {
242 mClientCompositionRequestCache.reset();
243 } else {
244 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
245 }
246};
247
Lloyd Pique31cb2942018-10-19 17:23:03 -0700248void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
249 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700250}
251
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000252Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700253 const auto& outputState = getState();
254 Region dirty(outputState.viewport);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000255 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700256 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700257 }
258 return dirty;
259}
260
Lloyd Piquec6687342019-03-07 21:34:57 -0800261bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800262 // The layerStackId's must match, and also the layer must not be internal
263 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700264 const auto& outputState = getState();
265 return layerStackId && (*layerStackId == outputState.layerStackId) &&
266 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700267}
268
Lloyd Piquede196652020-01-22 17:29:58 -0800269bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
270 const auto* layerFEState = layerFE->getCompositionState();
271 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800272}
273
Lloyd Piquedf336d92019-03-07 21:38:42 -0800274std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800275 const sp<LayerFE>& layerFE) const {
276 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800277}
278
Lloyd Piquede196652020-01-22 17:29:58 -0800279compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
280 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700281 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800282}
283
Lloyd Pique01c77c12019-04-17 12:48:32 -0700284std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800285 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700286 for (size_t i = 0; i < getOutputLayerCount(); i++) {
287 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800288 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700289 return i;
290 }
291 }
292 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800293}
294
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800295void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
296 mReleasedLayers = std::move(layers);
297}
298
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800299void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
300 LayerFESet& geomSnapshots) {
301 ATRACE_CALL();
302 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800303
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800304 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800305}
306
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800307void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800308 ATRACE_CALL();
309 ALOGV(__FUNCTION__);
310
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800311 updateColorProfile(refreshArgs);
312 updateAndWriteCompositionState(refreshArgs);
313 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800314 beginFrame();
315 prepareFrame();
316 devOptRepaintFlash(refreshArgs);
317 finishFrame(refreshArgs);
318 postFramebuffer();
319}
320
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800321void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
322 LayerFESet& layerFESet) {
323 ATRACE_CALL();
324 ALOGV(__FUNCTION__);
325
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700326 auto& outputState = editState();
327
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800328 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700329 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800330 return;
331 }
332
333 // Process the layers to determine visibility and coverage
334 compositionengine::Output::CoverageState coverage{layerFESet};
335 collectVisibleLayers(refreshArgs, coverage);
336
337 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700338 const ui::Transform& tr = outputState.transform;
339 Region undefinedRegion{outputState.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800340 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
341
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700342 outputState.undefinedRegion = undefinedRegion;
343 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800344}
345
346void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
347 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800348 // Evaluate the layers from front to back to determine what is visible. This
349 // also incrementally calculates the coverage information for each layer as
350 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800351 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700352 // Incrementally process the coverage for each layer
353 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800354
355 // TODO(b/121291683): Stop early if the output is completely covered and
356 // no more layers could even be visible underneath the ones on top.
357 }
358
Lloyd Pique01c77c12019-04-17 12:48:32 -0700359 setReleasedLayers(refreshArgs);
360
361 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800362
363 // Generate a simple Z-order values to each visible output layer
364 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700365 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800366 outputLayer->editState().z = zOrder++;
367 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800368}
369
Lloyd Piquede196652020-01-22 17:29:58 -0800370void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700371 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800372 // Ensure we have a snapshot of the basic geometry layer state. Limit the
373 // snapshots to once per frame for each candidate layer, as layers may
374 // appear on multiple outputs.
375 if (!coverage.latchedLayers.count(layerFE)) {
376 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800377 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800378 }
379
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800380 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800381 if (!belongsInOutput(layerFE)) {
382 return;
383 }
384
385 // Obtain a read-only pointer to the front-end layer state
386 const auto* layerFEState = layerFE->getCompositionState();
387 if (CC_UNLIKELY(!layerFEState)) {
388 return;
389 }
390
391 // handle hidden surfaces by setting the visible region to empty
392 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700393 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800394 }
395
396 /*
397 * opaqueRegion: area of a surface that is fully opaque.
398 */
399 Region opaqueRegion;
400
401 /*
402 * visibleRegion: area of a surface that is visible on screen and not fully
403 * transparent. This is essentially the layer's footprint minus the opaque
404 * regions above it. Areas covered by a translucent surface are considered
405 * visible.
406 */
407 Region visibleRegion;
408
409 /*
410 * coveredRegion: area of a surface that is covered by all visible regions
411 * above it (which includes the translucent areas).
412 */
413 Region coveredRegion;
414
415 /*
416 * transparentRegion: area of a surface that is hinted to be completely
417 * transparent. This is only used to tell when the layer has no visible non-
418 * transparent regions and can be removed from the layer list. It does not
419 * affect the visibleRegion of this layer or any layers beneath it. The hint
420 * may not be correct if apps don't respect the SurfaceView restrictions
421 * (which, sadly, some don't).
422 */
423 Region transparentRegion;
424
Vishnu Naira483b4a2019-12-12 15:07:52 -0800425 /*
426 * shadowRegion: Region cast by the layer's shadow.
427 */
428 Region shadowRegion;
429
Lloyd Piquede196652020-01-22 17:29:58 -0800430 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800431
432 // Get the visible region
433 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
434 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800435 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800436 visibleRegion.set(visibleRect);
437
Lloyd Piquede196652020-01-22 17:29:58 -0800438 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800439 // if the layer casts a shadow, offset the layers visible region and
440 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800441 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800442 Rect visibleRectWithShadows(visibleRect);
443 visibleRectWithShadows.inset(inset, inset, inset, inset);
444 visibleRegion.set(visibleRectWithShadows);
445 shadowRegion = visibleRegion.subtract(visibleRect);
446 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800447
448 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700449 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800450 }
451
452 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800453 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800454 if (tr.preserveRects()) {
455 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800456 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800457 } else {
458 // transformation too complex, can't do the
459 // transparent region optimization.
460 transparentRegion.clear();
461 }
462 }
463
464 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800465 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800466 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800467 // If we one of the simple category of transforms (0/90/180/270 rotation
468 // + any flip), then the opaque region is the layer's footprint.
469 // Otherwise we don't try and compute the opaque region since there may
470 // be errors at the edges, and we treat the entire layer as
471 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800472 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 }
474
475 // Clip the covered region to the visible region
476 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
477
478 // Update accumAboveCoveredLayers for next (lower) layer
479 coverage.aboveCoveredLayers.orSelf(visibleRegion);
480
481 // subtract the opaque region covered by the layers above us
482 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
483
484 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700485 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800486 }
487
488 // Get coverage information for the layer as previously displayed,
489 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800490 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700491 auto prevOutputLayer =
492 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800493
494 // Get coverage information for the layer as previously displayed
495 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
496 const Region kEmptyRegion;
497 const Region& oldVisibleRegion =
498 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
499 const Region& oldCoveredRegion =
500 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
501
502 // compute this layer's dirty region
503 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800504 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800505 // we need to invalidate the whole region
506 dirty = visibleRegion;
507 // as well, as the old visible region
508 dirty.orSelf(oldVisibleRegion);
509 } else {
510 /* compute the exposed region:
511 * the exposed region consists of two components:
512 * 1) what's VISIBLE now and was COVERED before
513 * 2) what's EXPOSED now less what was EXPOSED before
514 *
515 * note that (1) is conservative, we start with the whole visible region
516 * but only keep what used to be covered by something -- which mean it
517 * may have been exposed.
518 *
519 * (2) handles areas that were not covered by anything but got exposed
520 * because of a resize.
521 *
522 */
523 const Region newExposed = visibleRegion - coveredRegion;
524 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
525 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
526 }
527 dirty.subtractSelf(coverage.aboveOpaqueLayers);
528
529 // accumulate to the screen dirty region
530 coverage.dirtyRegion.orSelf(dirty);
531
532 // Update accumAboveOpaqueLayers for next (lower) layer
533 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
534
535 // Compute the visible non-transparent region
536 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
537
Vishnu Naira483b4a2019-12-12 15:07:52 -0800538 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800539 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700540 const auto& outputState = getState();
541 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanove67fcd02020-07-01 13:25:38 +0000542 drawRegion.andSelf(outputState.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700544 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800545 }
546
Vishnu Naira483b4a2019-12-12 15:07:52 -0800547 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
548
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549 // The layer is visible. Either reuse the existing outputLayer if we have
550 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800551 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552
553 // Store the layer coverage information into the layer state as some of it
554 // is useful later.
555 auto& outputLayerState = result->editState();
556 outputLayerState.visibleRegion = visibleRegion;
557 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
558 outputLayerState.coveredRegion = coveredRegion;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800559 outputLayerState.outputSpaceVisibleRegion =
560 outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport));
561 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800562}
563
564void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
565 // The base class does nothing with this call.
566}
567
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800568void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700569 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800570 layer->getLayerFE().prepareCompositionState(
571 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
572 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800573 }
574}
575
576void Output::updateAndWriteCompositionState(
577 const compositionengine::CompositionRefreshArgs& refreshArgs) {
578 ATRACE_CALL();
579 ALOGV(__FUNCTION__);
580
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800581 if (!getState().isEnabled) {
582 return;
583 }
584
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800585 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
586 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
587
Lloyd Pique01c77c12019-04-17 12:48:32 -0700588 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700589 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800590 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200591 forceClientComposition,
592 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800593
594 if (mLayerRequestingBackgroundBlur == layer) {
595 forceClientComposition = false;
596 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800597
598 // Send the updated state to the HWC, if appropriate.
599 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
600 }
601}
602
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800603compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
604 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
605 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800606 if (layer->getLayerFE().getCompositionState()->backgroundBlurRadius > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800607 layerRequestingBgComposition = layer;
608 }
609 }
610 return layerRequestingBgComposition;
611}
612
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800613void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
614 setColorProfile(pickColorProfile(refreshArgs));
615}
616
617// Returns a data space that fits all visible layers. The returned data space
618// can only be one of
619// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
620// - Dataspace::DISPLAY_P3
621// - Dataspace::DISPLAY_BT2020
622// The returned HDR data space is one of
623// - Dataspace::UNKNOWN
624// - Dataspace::BT2020_HLG
625// - Dataspace::BT2020_PQ
626ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
627 bool* outIsHdrClientComposition) const {
628 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
629 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
630
Lloyd Pique01c77c12019-04-17 12:48:32 -0700631 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800632 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800633 case ui::Dataspace::V0_SCRGB:
634 case ui::Dataspace::V0_SCRGB_LINEAR:
635 case ui::Dataspace::BT2020:
636 case ui::Dataspace::BT2020_ITU:
637 case ui::Dataspace::BT2020_LINEAR:
638 case ui::Dataspace::DISPLAY_BT2020:
639 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
640 break;
641 case ui::Dataspace::DISPLAY_P3:
642 bestDataSpace = ui::Dataspace::DISPLAY_P3;
643 break;
644 case ui::Dataspace::BT2020_PQ:
645 case ui::Dataspace::BT2020_ITU_PQ:
646 bestDataSpace = ui::Dataspace::DISPLAY_P3;
647 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800648 *outIsHdrClientComposition =
649 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800650 break;
651 case ui::Dataspace::BT2020_HLG:
652 case ui::Dataspace::BT2020_ITU_HLG:
653 bestDataSpace = ui::Dataspace::DISPLAY_P3;
654 // When there's mixed PQ content and HLG content, we set the HDR
655 // data space to be BT2020_PQ and convert HLG to PQ.
656 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
657 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
658 }
659 break;
660 default:
661 break;
662 }
663 }
664
665 return bestDataSpace;
666}
667
668compositionengine::Output::ColorProfile Output::pickColorProfile(
669 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
670 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
671 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
672 ui::RenderIntent::COLORIMETRIC,
673 refreshArgs.colorSpaceAgnosticDataspace};
674 }
675
676 ui::Dataspace hdrDataSpace;
677 bool isHdrClientComposition = false;
678 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
679
680 switch (refreshArgs.forceOutputColorMode) {
681 case ui::ColorMode::SRGB:
682 bestDataSpace = ui::Dataspace::V0_SRGB;
683 break;
684 case ui::ColorMode::DISPLAY_P3:
685 bestDataSpace = ui::Dataspace::DISPLAY_P3;
686 break;
687 default:
688 break;
689 }
690
691 // respect hdrDataSpace only when there is no legacy HDR support
692 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
693 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
694 if (isHdr) {
695 bestDataSpace = hdrDataSpace;
696 }
697
698 ui::RenderIntent intent;
699 switch (refreshArgs.outputColorSetting) {
700 case OutputColorSetting::kManaged:
701 case OutputColorSetting::kUnmanaged:
702 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
703 : ui::RenderIntent::COLORIMETRIC;
704 break;
705 case OutputColorSetting::kEnhanced:
706 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
707 break;
708 default: // vendor display color setting
709 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
710 break;
711 }
712
713 ui::ColorMode outMode;
714 ui::Dataspace outDataSpace;
715 ui::RenderIntent outRenderIntent;
716 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
717 &outRenderIntent);
718
719 return ColorProfile{outMode, outDataSpace, outRenderIntent,
720 refreshArgs.colorSpaceAgnosticDataspace};
721}
722
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800723void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700724 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800725 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700726 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700727 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800728
729 // If nothing has changed (!dirty), don't recompose.
730 // If something changed, but we don't currently have any visible layers,
731 // and didn't when we last did a composition, then skip it this time.
732 // The second rule does two things:
733 // - When all layers are removed from a display, we'll emit one black
734 // frame, then nothing more until we get new layers.
735 // - When a display is created with a private layer stack, we won't
736 // emit any black frames until a layer is added to the layer stack.
737 const bool mustRecompose = dirty && !(empty && wasEmpty);
738
739 const char flagPrefix[] = {'-', '+'};
740 static_cast<void>(flagPrefix);
741 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
742 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
743 flagPrefix[empty], flagPrefix[wasEmpty]);
744
745 mRenderSurface->beginFrame(mustRecompose);
746
747 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700748 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800749 }
750}
751
Lloyd Pique66d68602019-02-13 14:23:31 -0800752void Output::prepareFrame() {
753 ATRACE_CALL();
754 ALOGV(__FUNCTION__);
755
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700756 const auto& outputState = getState();
757 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800758 return;
759 }
760
761 chooseCompositionStrategy();
762
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700763 mRenderSurface->prepareFrame(outputState.usesClientComposition,
764 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800765}
766
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800767void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
768 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
769 return;
770 }
771
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700772 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800773 // transform the dirty region into this screen's coordinate space
774 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
775 if (!dirtyRegion.isEmpty()) {
776 base::unique_fd readyFence;
777 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800778 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800779
780 mRenderSurface->queueBuffer(std::move(readyFence));
781 }
782 }
783
784 postFramebuffer();
785
786 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
787
788 prepareFrame();
789}
790
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800791void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800792 ATRACE_CALL();
793 ALOGV(__FUNCTION__);
794
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700795 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800796 return;
797 }
798
799 // Repaint the framebuffer (if needed), getting the optional fence for when
800 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800801 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800802 if (!optReadyFence) {
803 return;
804 }
805
806 // swap buffers (presentation)
807 mRenderSurface->queueBuffer(std::move(*optReadyFence));
808}
809
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800810std::optional<base::unique_fd> Output::composeSurfaces(
811 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800812 ATRACE_CALL();
813 ALOGV(__FUNCTION__);
814
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700815 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800816 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800817 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700818 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700819
820 auto& renderEngine = getCompositionEngine().getRenderEngine();
821 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
822
823 // If we the display is secure, protected content support is enabled, and at
824 // least one layer has protected content, we need to use a secure back
825 // buffer.
826 if (outputState.isSecure && supportsProtectedContent) {
827 auto layers = getOutputLayersOrderedByZ();
828 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
829 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
830 });
831 if (needsProtected != renderEngine.isProtected()) {
832 renderEngine.useProtectedContext(needsProtected);
833 }
834 if (needsProtected != mRenderSurface->isProtected() &&
835 needsProtected == renderEngine.isProtected()) {
836 mRenderSurface->setProtected(needsProtected);
837 }
838 }
839
840 base::unique_fd fd;
841 sp<GraphicBuffer> buf;
842
843 // If we aren't doing client composition on this output, but do have a
844 // flipClientTarget request for this frame on this output, we still need to
845 // dequeue a buffer.
846 if (hasClientComposition || outputState.flipClientTarget) {
847 buf = mRenderSurface->dequeueBuffer(&fd);
848 if (buf == nullptr) {
849 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
850 "client composition for this frame",
851 mName.c_str());
852 return {};
853 }
854 }
855
Lloyd Piqued3d69882019-02-28 16:03:46 -0800856 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800857 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800858 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800859 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800860 }
861
862 ALOGV("hasClientComposition");
863
Lloyd Pique688abd42019-02-15 15:42:24 -0800864 renderengine::DisplaySettings clientCompositionDisplay;
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800865 clientCompositionDisplay.physicalDisplay = outputState.destinationClip;
866 clientCompositionDisplay.clip = outputState.sourceClip;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700867 clientCompositionDisplay.orientation = outputState.orientation;
868 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
869 ? outputState.dataspace
870 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800871 clientCompositionDisplay.maxLuminance =
872 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
873
874 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700875 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
876 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800877 }
878
879 // Note: Updated by generateClientCompositionRequests
880 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
881
882 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800883 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800884 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800885 clientCompositionDisplay.clearRegion,
886 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800887 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
888
Vishnu Nair9b079a22020-01-21 14:36:08 -0800889 // Check if the client composition requests were rendered into the provided graphic buffer. If
890 // so, we can reuse the buffer and avoid client composition.
891 if (mClientCompositionRequestCache) {
892 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
893 clientCompositionLayers)) {
894 outputCompositionState.reusedClientComposition = true;
895 setExpensiveRenderingExpected(false);
896 return readyFence;
897 }
898 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
899 clientCompositionLayers);
900 }
901
Lloyd Pique688abd42019-02-15 15:42:24 -0800902 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800903 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
904 // GPU composition can finish in time. We must reset GPU frequency afterwards,
905 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800906 const bool expensiveBlurs =
907 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800908 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800909 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -0800910 if (expensiveRenderingExpected) {
911 setExpensiveRenderingExpected(true);
912 }
913
Vishnu Nair9b079a22020-01-21 14:36:08 -0800914 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
915 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
916 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
917 std::back_inserter(clientCompositionLayerPointers),
918 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
919 return &settings;
920 });
921
Alec Mourie4034bb2019-11-19 12:45:54 -0800922 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800923 status_t status =
Ana Krulecfc874ae2020-02-22 15:39:32 -0800924 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, buf,
925 /*useFramebufferCache=*/true, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800926
927 if (status != NO_ERROR && mClientCompositionRequestCache) {
928 // If rendering was not successful, remove the request from the cache.
929 mClientCompositionRequestCache->remove(buf->getId());
930 }
931
Alec Mourie4034bb2019-11-19 12:45:54 -0800932 auto& timeStats = getCompositionEngine().getTimeStats();
933 if (readyFence.get() < 0) {
934 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
935 } else {
936 timeStats.recordRenderEngineDuration(renderEngineStart,
937 std::make_shared<FenceTime>(
938 new Fence(dup(readyFence.get()))));
939 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800940
Lloyd Piqued3d69882019-02-28 16:03:46 -0800941 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800942}
943
Vishnu Nair9b079a22020-01-21 14:36:08 -0800944std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800945 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800946 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -0800947 ALOGV("Rendering client layers");
948
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700949 const auto& outputState = getState();
950 const Region viewportRegion(outputState.viewport);
Lloyd Pique688abd42019-02-15 15:42:24 -0800951 const bool useIdentityTransform = false;
952 bool firstLayer = true;
953 // Used when a layer clears part of the buffer.
954 Region dummyRegion;
955
Lloyd Pique01c77c12019-04-17 12:48:32 -0700956 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800957 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -0800958 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800959 auto& layerFE = layer->getLayerFE();
960
Lloyd Piquea2468662019-03-07 21:31:06 -0800961 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800962 ALOGV("Layer: %s", layerFE.getDebugName());
963 if (clip.isEmpty()) {
964 ALOGV(" Skipping for empty clip");
965 firstLayer = false;
966 continue;
967 }
968
Vishnu Naira483b4a2019-12-12 15:07:52 -0800969 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800970
971 // We clear the client target for non-client composed layers if
972 // requested by the HWC. We skip this if the layer is not an opaque
973 // rectangle, as by definition the layer must blend with whatever is
974 // underneath. We also skip the first layer as the buffer target is
975 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800976 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -0800977 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -0800978
979 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
980
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800981 // If the layer casts a shadow but the content casting the shadow is occluded, skip
982 // composing the non-shadow content and only draw the shadows.
983 const bool realContentIsVisible = clientComposition &&
984 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
985
Lloyd Pique688abd42019-02-15 15:42:24 -0800986 if (clientComposition || clearClientComposition) {
987 compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{
988 clip,
989 useIdentityTransform,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700990 layer->needsFiltering() || outputState.needsFiltering,
991 outputState.isSecure,
Lloyd Pique688abd42019-02-15 15:42:24 -0800992 supportsProtectedContent,
993 clientComposition ? clearRegion : dummyRegion,
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800994 outputState.viewport,
995 outputDataspace,
996 realContentIsVisible,
997 !clientComposition, /* clearContent */
Lloyd Pique688abd42019-02-15 15:42:24 -0800998 };
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800999 std::vector<LayerFE::LayerSettings> results =
1000 layerFE.prepareClientCompositionList(targetSettings);
1001 if (realContentIsVisible && !results.empty()) {
1002 layer->editState().clientCompositionTimestamp = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -08001003 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001004
1005 clientCompositionLayers.insert(clientCompositionLayers.end(),
1006 std::make_move_iterator(results.begin()),
1007 std::make_move_iterator(results.end()));
1008 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001009 }
1010
1011 firstLayer = false;
1012 }
1013
1014 return clientCompositionLayers;
1015}
1016
1017void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001018 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001019 if (flashRegion.isEmpty()) {
1020 return;
1021 }
1022
Vishnu Nair9b079a22020-01-21 14:36:08 -08001023 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001024 layerSettings.source.buffer.buffer = nullptr;
1025 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1026 layerSettings.alpha = half(1.0);
1027
1028 for (const auto& rect : flashRegion) {
1029 layerSettings.geometry.boundaries = rect.toFloatRect();
1030 clientCompositionLayers.push_back(layerSettings);
1031 }
1032}
1033
1034void Output::setExpensiveRenderingExpected(bool) {
1035 // The base class does nothing with this call.
1036}
1037
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001038void Output::postFramebuffer() {
1039 ATRACE_CALL();
1040 ALOGV(__FUNCTION__);
1041
1042 if (!getState().isEnabled) {
1043 return;
1044 }
1045
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001046 auto& outputState = editState();
1047 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001048 mRenderSurface->flip();
1049
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001050 auto frame = presentAndGetFrameFences();
1051
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001052 mRenderSurface->onPresentDisplayCompleted();
1053
Lloyd Pique01c77c12019-04-17 12:48:32 -07001054 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001055 // The layer buffer from the previous frame (if any) is released
1056 // by HWC only when the release fence from this frame (if any) is
1057 // signaled. Always get the release fence from HWC first.
1058 sp<Fence> releaseFence = Fence::NO_FENCE;
1059
1060 if (auto hwcLayer = layer->getHwcLayer()) {
1061 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1062 releaseFence = f->second;
1063 }
1064 }
1065
1066 // If the layer was client composited in the previous frame, we
1067 // need to merge with the previous client target acquire fence.
1068 // Since we do not track that, always merge with the current
1069 // client target acquire fence when it is available, even though
1070 // this is suboptimal.
1071 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001072 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001073 releaseFence =
1074 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1075 }
1076
1077 layer->getLayerFE().onLayerDisplayed(releaseFence);
1078 }
1079
1080 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001081 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001082 // supply them with the present fence.
1083 for (auto& weakLayer : mReleasedLayers) {
1084 if (auto layer = weakLayer.promote(); layer != nullptr) {
1085 layer->onLayerDisplayed(frame.presentFence);
1086 }
1087 }
1088
1089 // Clear out the released layers now that we're done with them.
1090 mReleasedLayers.clear();
1091}
1092
Lloyd Pique32cbe282018-10-19 13:09:22 -07001093void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001094 auto& outputState = editState();
1095 outputState.dirtyRegion.set(outputState.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001096}
1097
Lloyd Pique66d68602019-02-13 14:23:31 -08001098void Output::chooseCompositionStrategy() {
1099 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001100 auto& outputState = editState();
1101 outputState.usesClientComposition = true;
1102 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001103 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001104}
1105
Lloyd Pique688abd42019-02-15 15:42:24 -08001106bool Output::getSkipColorTransform() const {
1107 return true;
1108}
1109
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001110compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1111 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001112 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001113 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1114 }
1115 return result;
1116}
1117
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001118} // namespace impl
1119} // namespace android::compositionengine