blob: abb87695631b7ad812661cce464e4a2f72937138 [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
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200108void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
109 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700110 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200111
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200112 outputState.displaySpace.orientation = orientation;
113 // outputState.displaySpace.bounds should be already set from setDisplaySpaceSize().
114
115 // Compute the orientedDisplaySpace bounds
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200116 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200117 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200118 std::swap(orientedSize.width, orientedSize.height);
119 }
120 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200121 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
122
123 // Compute displaySpace.content
124 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
125 ui::Transform rotation;
126 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
127 const auto displaySize = outputState.displaySpace.bounds;
128 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
129 }
130 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200131
132 outputState.layerStackSpace.content = layerStackSpaceRect;
133 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200134 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
135 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700136
137 dirtyEntireOutput();
138}
139
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200140void Output::setDisplaySpaceSize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700141 mRenderSurface->setDisplaySize(size);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200142 editState().displaySpace.bounds = Rect(mRenderSurface->getSize());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700143
144 dirtyEntireOutput();
145}
146
Lloyd Piqueef36b002019-01-23 17:52:04 -0800147void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700148 auto& outputState = editState();
149 outputState.layerStackId = layerStackId;
150 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700151
152 dirtyEntireOutput();
153}
154
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800155void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700156 auto& colorTransformMatrix = editState().colorTransformMatrix;
157 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700158 return;
159 }
160
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700161 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800162
163 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700164}
165
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800166void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700167 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800168 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
169 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800170
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700171 auto& outputState = editState();
172 if (outputState.colorMode == colorProfile.mode &&
173 outputState.dataspace == colorProfile.dataspace &&
174 outputState.renderIntent == colorProfile.renderIntent &&
175 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800176 return;
177 }
178
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700179 outputState.colorMode = colorProfile.mode;
180 outputState.dataspace = colorProfile.dataspace;
181 outputState.renderIntent = colorProfile.renderIntent;
182 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700183
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800184 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700185
Lloyd Pique32cbe282018-10-19 13:09:22 -0700186 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800187 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
188 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800189
190 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700191}
192
193void Output::dump(std::string& out) const {
194 using android::base::StringAppendF;
195
196 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
197
198 out.append("\n ");
199
200 dumpBase(out);
201}
202
203void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700204 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700205
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700206 if (mDisplayColorProfile) {
207 mDisplayColorProfile->dump(out);
208 } else {
209 out.append(" No display color profile!\n");
210 }
211
Lloyd Pique31cb2942018-10-19 17:23:03 -0700212 if (mRenderSurface) {
213 mRenderSurface->dump(out);
214 } else {
215 out.append(" No render surface!\n");
216 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800217
Lloyd Pique01c77c12019-04-17 12:48:32 -0700218 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
219 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800220 if (!outputLayer) {
221 continue;
222 }
223 outputLayer->dump(out);
224 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700225}
226
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700227compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
228 return mDisplayColorProfile.get();
229}
230
231void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
232 mDisplayColorProfile = std::move(mode);
233}
234
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800235const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
236 return mReleasedLayers;
237}
238
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700239void Output::setDisplayColorProfileForTest(
240 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
241 mDisplayColorProfile = std::move(mode);
242}
243
Lloyd Pique31cb2942018-10-19 17:23:03 -0700244compositionengine::RenderSurface* Output::getRenderSurface() const {
245 return mRenderSurface.get();
246}
247
248void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
249 mRenderSurface = std::move(surface);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200250 editState().displaySpace.bounds = Rect(mRenderSurface->getSize());
Lloyd Pique31cb2942018-10-19 17:23:03 -0700251
252 dirtyEntireOutput();
253}
254
Vishnu Nair9b079a22020-01-21 14:36:08 -0800255void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
256 if (cacheSize == 0) {
257 mClientCompositionRequestCache.reset();
258 } else {
259 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
260 }
261};
262
Lloyd Pique31cb2942018-10-19 17:23:03 -0700263void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
264 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700265}
266
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000267Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700268 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200269 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000270 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700271 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700272 }
273 return dirty;
274}
275
Lloyd Piquec6687342019-03-07 21:34:57 -0800276bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800277 // The layerStackId's must match, and also the layer must not be internal
278 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700279 const auto& outputState = getState();
280 return layerStackId && (*layerStackId == outputState.layerStackId) &&
281 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700282}
283
Lloyd Piquede196652020-01-22 17:29:58 -0800284bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
285 const auto* layerFEState = layerFE->getCompositionState();
286 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800287}
288
Lloyd Piquedf336d92019-03-07 21:38:42 -0800289std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800290 const sp<LayerFE>& layerFE) const {
291 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800292}
293
Lloyd Piquede196652020-01-22 17:29:58 -0800294compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
295 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700296 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800297}
298
Lloyd Pique01c77c12019-04-17 12:48:32 -0700299std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800300 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700301 for (size_t i = 0; i < getOutputLayerCount(); i++) {
302 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800303 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700304 return i;
305 }
306 }
307 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800308}
309
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800310void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
311 mReleasedLayers = std::move(layers);
312}
313
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800314void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
315 LayerFESet& geomSnapshots) {
316 ATRACE_CALL();
317 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800318
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800319 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800320}
321
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800322void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800323 ATRACE_CALL();
324 ALOGV(__FUNCTION__);
325
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800326 updateColorProfile(refreshArgs);
327 updateAndWriteCompositionState(refreshArgs);
328 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800329 beginFrame();
330 prepareFrame();
331 devOptRepaintFlash(refreshArgs);
332 finishFrame(refreshArgs);
333 postFramebuffer();
334}
335
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800336void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
337 LayerFESet& layerFESet) {
338 ATRACE_CALL();
339 ALOGV(__FUNCTION__);
340
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700341 auto& outputState = editState();
342
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800343 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700344 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800345 return;
346 }
347
348 // Process the layers to determine visibility and coverage
349 compositionengine::Output::CoverageState coverage{layerFESet};
350 collectVisibleLayers(refreshArgs, coverage);
351
352 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700353 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200354 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800355 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
356
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700357 outputState.undefinedRegion = undefinedRegion;
358 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800359}
360
361void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
362 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800363 // Evaluate the layers from front to back to determine what is visible. This
364 // also incrementally calculates the coverage information for each layer as
365 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800366 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700367 // Incrementally process the coverage for each layer
368 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800369
370 // TODO(b/121291683): Stop early if the output is completely covered and
371 // no more layers could even be visible underneath the ones on top.
372 }
373
Lloyd Pique01c77c12019-04-17 12:48:32 -0700374 setReleasedLayers(refreshArgs);
375
376 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800377
378 // Generate a simple Z-order values to each visible output layer
379 uint32_t zOrder = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700380 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800381 outputLayer->editState().z = zOrder++;
382 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800383}
384
Lloyd Piquede196652020-01-22 17:29:58 -0800385void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700386 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800387 // Ensure we have a snapshot of the basic geometry layer state. Limit the
388 // snapshots to once per frame for each candidate layer, as layers may
389 // appear on multiple outputs.
390 if (!coverage.latchedLayers.count(layerFE)) {
391 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800392 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800393 }
394
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800395 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800396 if (!belongsInOutput(layerFE)) {
397 return;
398 }
399
400 // Obtain a read-only pointer to the front-end layer state
401 const auto* layerFEState = layerFE->getCompositionState();
402 if (CC_UNLIKELY(!layerFEState)) {
403 return;
404 }
405
406 // handle hidden surfaces by setting the visible region to empty
407 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700408 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800409 }
410
411 /*
412 * opaqueRegion: area of a surface that is fully opaque.
413 */
414 Region opaqueRegion;
415
416 /*
417 * visibleRegion: area of a surface that is visible on screen and not fully
418 * transparent. This is essentially the layer's footprint minus the opaque
419 * regions above it. Areas covered by a translucent surface are considered
420 * visible.
421 */
422 Region visibleRegion;
423
424 /*
425 * coveredRegion: area of a surface that is covered by all visible regions
426 * above it (which includes the translucent areas).
427 */
428 Region coveredRegion;
429
430 /*
431 * transparentRegion: area of a surface that is hinted to be completely
432 * transparent. This is only used to tell when the layer has no visible non-
433 * transparent regions and can be removed from the layer list. It does not
434 * affect the visibleRegion of this layer or any layers beneath it. The hint
435 * may not be correct if apps don't respect the SurfaceView restrictions
436 * (which, sadly, some don't).
437 */
438 Region transparentRegion;
439
Vishnu Naira483b4a2019-12-12 15:07:52 -0800440 /*
441 * shadowRegion: Region cast by the layer's shadow.
442 */
443 Region shadowRegion;
444
Lloyd Piquede196652020-01-22 17:29:58 -0800445 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800446
447 // Get the visible region
448 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
449 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800450 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800451 visibleRegion.set(visibleRect);
452
Lloyd Piquede196652020-01-22 17:29:58 -0800453 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800454 // if the layer casts a shadow, offset the layers visible region and
455 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800456 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800457 Rect visibleRectWithShadows(visibleRect);
458 visibleRectWithShadows.inset(inset, inset, inset, inset);
459 visibleRegion.set(visibleRectWithShadows);
460 shadowRegion = visibleRegion.subtract(visibleRect);
461 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462
463 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700464 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465 }
466
467 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800468 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800469 if (tr.preserveRects()) {
470 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800471 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800472 } else {
473 // transformation too complex, can't do the
474 // transparent region optimization.
475 transparentRegion.clear();
476 }
477 }
478
479 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800480 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800481 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800482 // If we one of the simple category of transforms (0/90/180/270 rotation
483 // + any flip), then the opaque region is the layer's footprint.
484 // Otherwise we don't try and compute the opaque region since there may
485 // be errors at the edges, and we treat the entire layer as
486 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800487 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800488 }
489
490 // Clip the covered region to the visible region
491 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
492
493 // Update accumAboveCoveredLayers for next (lower) layer
494 coverage.aboveCoveredLayers.orSelf(visibleRegion);
495
496 // subtract the opaque region covered by the layers above us
497 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
498
499 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700500 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800501 }
502
503 // Get coverage information for the layer as previously displayed,
504 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800505 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700506 auto prevOutputLayer =
507 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800508
509 // Get coverage information for the layer as previously displayed
510 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
511 const Region kEmptyRegion;
512 const Region& oldVisibleRegion =
513 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
514 const Region& oldCoveredRegion =
515 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
516
517 // compute this layer's dirty region
518 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800519 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800520 // we need to invalidate the whole region
521 dirty = visibleRegion;
522 // as well, as the old visible region
523 dirty.orSelf(oldVisibleRegion);
524 } else {
525 /* compute the exposed region:
526 * the exposed region consists of two components:
527 * 1) what's VISIBLE now and was COVERED before
528 * 2) what's EXPOSED now less what was EXPOSED before
529 *
530 * note that (1) is conservative, we start with the whole visible region
531 * but only keep what used to be covered by something -- which mean it
532 * may have been exposed.
533 *
534 * (2) handles areas that were not covered by anything but got exposed
535 * because of a resize.
536 *
537 */
538 const Region newExposed = visibleRegion - coveredRegion;
539 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
540 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
541 }
542 dirty.subtractSelf(coverage.aboveOpaqueLayers);
543
544 // accumulate to the screen dirty region
545 coverage.dirtyRegion.orSelf(dirty);
546
547 // Update accumAboveOpaqueLayers for next (lower) layer
548 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
549
550 // Compute the visible non-transparent region
551 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
552
Vishnu Naira483b4a2019-12-12 15:07:52 -0800553 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800554 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700555 const auto& outputState = getState();
556 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200557 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800558 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700559 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560 }
561
Vishnu Naira483b4a2019-12-12 15:07:52 -0800562 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
563
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800564 // The layer is visible. Either reuse the existing outputLayer if we have
565 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800566 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800567
568 // Store the layer coverage information into the layer state as some of it
569 // is useful later.
570 auto& outputLayerState = result->editState();
571 outputLayerState.visibleRegion = visibleRegion;
572 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
573 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200574 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
575 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800576 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800577}
578
579void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
580 // The base class does nothing with this call.
581}
582
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800583void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700584 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800585 layer->getLayerFE().prepareCompositionState(
586 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
587 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800588 }
589}
590
591void Output::updateAndWriteCompositionState(
592 const compositionengine::CompositionRefreshArgs& refreshArgs) {
593 ATRACE_CALL();
594 ALOGV(__FUNCTION__);
595
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800596 if (!getState().isEnabled) {
597 return;
598 }
599
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800600 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
601 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
602
Lloyd Pique01c77c12019-04-17 12:48:32 -0700603 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700604 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800605 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200606 forceClientComposition,
607 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800608
609 if (mLayerRequestingBackgroundBlur == layer) {
610 forceClientComposition = false;
611 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800612
613 // Send the updated state to the HWC, if appropriate.
614 layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
615 }
616}
617
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800618compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
619 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
620 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800621 if (layer->getLayerFE().getCompositionState()->backgroundBlurRadius > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800622 layerRequestingBgComposition = layer;
623 }
624 }
625 return layerRequestingBgComposition;
626}
627
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800628void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
629 setColorProfile(pickColorProfile(refreshArgs));
630}
631
632// Returns a data space that fits all visible layers. The returned data space
633// can only be one of
634// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
635// - Dataspace::DISPLAY_P3
636// - Dataspace::DISPLAY_BT2020
637// The returned HDR data space is one of
638// - Dataspace::UNKNOWN
639// - Dataspace::BT2020_HLG
640// - Dataspace::BT2020_PQ
641ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
642 bool* outIsHdrClientComposition) const {
643 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
644 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
645
Lloyd Pique01c77c12019-04-17 12:48:32 -0700646 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800647 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800648 case ui::Dataspace::V0_SCRGB:
649 case ui::Dataspace::V0_SCRGB_LINEAR:
650 case ui::Dataspace::BT2020:
651 case ui::Dataspace::BT2020_ITU:
652 case ui::Dataspace::BT2020_LINEAR:
653 case ui::Dataspace::DISPLAY_BT2020:
654 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
655 break;
656 case ui::Dataspace::DISPLAY_P3:
657 bestDataSpace = ui::Dataspace::DISPLAY_P3;
658 break;
659 case ui::Dataspace::BT2020_PQ:
660 case ui::Dataspace::BT2020_ITU_PQ:
661 bestDataSpace = ui::Dataspace::DISPLAY_P3;
662 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800663 *outIsHdrClientComposition =
664 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800665 break;
666 case ui::Dataspace::BT2020_HLG:
667 case ui::Dataspace::BT2020_ITU_HLG:
668 bestDataSpace = ui::Dataspace::DISPLAY_P3;
669 // When there's mixed PQ content and HLG content, we set the HDR
670 // data space to be BT2020_PQ and convert HLG to PQ.
671 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
672 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
673 }
674 break;
675 default:
676 break;
677 }
678 }
679
680 return bestDataSpace;
681}
682
683compositionengine::Output::ColorProfile Output::pickColorProfile(
684 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
685 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
686 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
687 ui::RenderIntent::COLORIMETRIC,
688 refreshArgs.colorSpaceAgnosticDataspace};
689 }
690
691 ui::Dataspace hdrDataSpace;
692 bool isHdrClientComposition = false;
693 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
694
695 switch (refreshArgs.forceOutputColorMode) {
696 case ui::ColorMode::SRGB:
697 bestDataSpace = ui::Dataspace::V0_SRGB;
698 break;
699 case ui::ColorMode::DISPLAY_P3:
700 bestDataSpace = ui::Dataspace::DISPLAY_P3;
701 break;
702 default:
703 break;
704 }
705
706 // respect hdrDataSpace only when there is no legacy HDR support
707 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
708 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
709 if (isHdr) {
710 bestDataSpace = hdrDataSpace;
711 }
712
713 ui::RenderIntent intent;
714 switch (refreshArgs.outputColorSetting) {
715 case OutputColorSetting::kManaged:
716 case OutputColorSetting::kUnmanaged:
717 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
718 : ui::RenderIntent::COLORIMETRIC;
719 break;
720 case OutputColorSetting::kEnhanced:
721 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
722 break;
723 default: // vendor display color setting
724 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
725 break;
726 }
727
728 ui::ColorMode outMode;
729 ui::Dataspace outDataSpace;
730 ui::RenderIntent outRenderIntent;
731 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
732 &outRenderIntent);
733
734 return ColorProfile{outMode, outDataSpace, outRenderIntent,
735 refreshArgs.colorSpaceAgnosticDataspace};
736}
737
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800738void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700739 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800740 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700741 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700742 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800743
744 // If nothing has changed (!dirty), don't recompose.
745 // If something changed, but we don't currently have any visible layers,
746 // and didn't when we last did a composition, then skip it this time.
747 // The second rule does two things:
748 // - When all layers are removed from a display, we'll emit one black
749 // frame, then nothing more until we get new layers.
750 // - When a display is created with a private layer stack, we won't
751 // emit any black frames until a layer is added to the layer stack.
752 const bool mustRecompose = dirty && !(empty && wasEmpty);
753
754 const char flagPrefix[] = {'-', '+'};
755 static_cast<void>(flagPrefix);
756 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
757 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
758 flagPrefix[empty], flagPrefix[wasEmpty]);
759
760 mRenderSurface->beginFrame(mustRecompose);
761
762 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700763 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800764 }
765}
766
Lloyd Pique66d68602019-02-13 14:23:31 -0800767void Output::prepareFrame() {
768 ATRACE_CALL();
769 ALOGV(__FUNCTION__);
770
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700771 const auto& outputState = getState();
772 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800773 return;
774 }
775
776 chooseCompositionStrategy();
777
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700778 mRenderSurface->prepareFrame(outputState.usesClientComposition,
779 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800780}
781
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800782void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
783 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
784 return;
785 }
786
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700787 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800788 // transform the dirty region into this screen's coordinate space
789 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
790 if (!dirtyRegion.isEmpty()) {
791 base::unique_fd readyFence;
792 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800793 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800794
795 mRenderSurface->queueBuffer(std::move(readyFence));
796 }
797 }
798
799 postFramebuffer();
800
801 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
802
803 prepareFrame();
804}
805
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800806void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800807 ATRACE_CALL();
808 ALOGV(__FUNCTION__);
809
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700810 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800811 return;
812 }
813
814 // Repaint the framebuffer (if needed), getting the optional fence for when
815 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800816 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800817 if (!optReadyFence) {
818 return;
819 }
820
821 // swap buffers (presentation)
822 mRenderSurface->queueBuffer(std::move(*optReadyFence));
823}
824
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800825std::optional<base::unique_fd> Output::composeSurfaces(
826 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800827 ATRACE_CALL();
828 ALOGV(__FUNCTION__);
829
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700830 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800831 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800832 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700833 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700834
835 auto& renderEngine = getCompositionEngine().getRenderEngine();
836 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
837
838 // If we the display is secure, protected content support is enabled, and at
839 // least one layer has protected content, we need to use a secure back
840 // buffer.
841 if (outputState.isSecure && supportsProtectedContent) {
842 auto layers = getOutputLayersOrderedByZ();
843 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
844 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
845 });
846 if (needsProtected != renderEngine.isProtected()) {
847 renderEngine.useProtectedContext(needsProtected);
848 }
849 if (needsProtected != mRenderSurface->isProtected() &&
850 needsProtected == renderEngine.isProtected()) {
851 mRenderSurface->setProtected(needsProtected);
852 }
853 }
854
855 base::unique_fd fd;
856 sp<GraphicBuffer> buf;
857
858 // If we aren't doing client composition on this output, but do have a
859 // flipClientTarget request for this frame on this output, we still need to
860 // dequeue a buffer.
861 if (hasClientComposition || outputState.flipClientTarget) {
862 buf = mRenderSurface->dequeueBuffer(&fd);
863 if (buf == nullptr) {
864 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
865 "client composition for this frame",
866 mName.c_str());
867 return {};
868 }
869 }
870
Lloyd Piqued3d69882019-02-28 16:03:46 -0800871 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800872 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -0800873 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800874 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800875 }
876
877 ALOGV("hasClientComposition");
878
Lloyd Pique688abd42019-02-15 15:42:24 -0800879 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200880 clientCompositionDisplay.physicalDisplay = outputState.displaySpace.content;
881 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200882 clientCompositionDisplay.orientation =
883 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700884 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
885 ? outputState.dataspace
886 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -0800887 clientCompositionDisplay.maxLuminance =
888 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
889
890 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700891 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
892 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -0800893 }
894
895 // Note: Updated by generateClientCompositionRequests
896 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
897
898 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -0800899 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -0800900 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800901 clientCompositionDisplay.clearRegion,
902 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -0800903 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
904
Vishnu Nair9b079a22020-01-21 14:36:08 -0800905 // Check if the client composition requests were rendered into the provided graphic buffer. If
906 // so, we can reuse the buffer and avoid client composition.
907 if (mClientCompositionRequestCache) {
908 if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay,
909 clientCompositionLayers)) {
910 outputCompositionState.reusedClientComposition = true;
911 setExpensiveRenderingExpected(false);
912 return readyFence;
913 }
914 mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay,
915 clientCompositionLayers);
916 }
917
Lloyd Pique688abd42019-02-15 15:42:24 -0800918 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800919 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
920 // GPU composition can finish in time. We must reset GPU frequency afterwards,
921 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800922 const bool expensiveBlurs =
923 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -0800924 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800925 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -0800926 if (expensiveRenderingExpected) {
927 setExpensiveRenderingExpected(true);
928 }
929
Vishnu Nair9b079a22020-01-21 14:36:08 -0800930 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
931 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
932 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
933 std::back_inserter(clientCompositionLayerPointers),
934 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
935 return &settings;
936 });
937
Alec Mourie4034bb2019-11-19 12:45:54 -0800938 const nsecs_t renderEngineStart = systemTime();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800939 status_t status =
Ana Krulecfc874ae2020-02-22 15:39:32 -0800940 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, buf,
941 /*useFramebufferCache=*/true, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800942
943 if (status != NO_ERROR && mClientCompositionRequestCache) {
944 // If rendering was not successful, remove the request from the cache.
945 mClientCompositionRequestCache->remove(buf->getId());
946 }
947
Alec Mourie4034bb2019-11-19 12:45:54 -0800948 auto& timeStats = getCompositionEngine().getTimeStats();
949 if (readyFence.get() < 0) {
950 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
951 } else {
952 timeStats.recordRenderEngineDuration(renderEngineStart,
953 std::make_shared<FenceTime>(
954 new Fence(dup(readyFence.get()))));
955 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800956
Lloyd Piqued3d69882019-02-28 16:03:46 -0800957 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -0800958}
959
Vishnu Nair9b079a22020-01-21 14:36:08 -0800960std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800961 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -0800962 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -0800963 ALOGV("Rendering client layers");
964
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700965 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200966 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -0800967 bool firstLayer = true;
968 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -0700969 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800970
Lloyd Pique01c77c12019-04-17 12:48:32 -0700971 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800972 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -0800973 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800974 auto& layerFE = layer->getLayerFE();
975
Lloyd Piquea2468662019-03-07 21:31:06 -0800976 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -0800977 ALOGV("Layer: %s", layerFE.getDebugName());
978 if (clip.isEmpty()) {
979 ALOGV(" Skipping for empty clip");
980 firstLayer = false;
981 continue;
982 }
983
Vishnu Naira483b4a2019-12-12 15:07:52 -0800984 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -0800985
986 // We clear the client target for non-client composed layers if
987 // requested by the HWC. We skip this if the layer is not an opaque
988 // rectangle, as by definition the layer must blend with whatever is
989 // underneath. We also skip the first layer as the buffer target is
990 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800991 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -0800992 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -0800993
994 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
995
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800996 // If the layer casts a shadow but the content casting the shadow is occluded, skip
997 // composing the non-shadow content and only draw the shadows.
998 const bool realContentIsVisible = clientComposition &&
999 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1000
Lloyd Pique688abd42019-02-15 15:42:24 -08001001 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001002 compositionengine::LayerFE::ClientCompositionTargetSettings
1003 targetSettings{.clip = clip,
1004 .needsFiltering =
1005 layer->needsFiltering() || outputState.needsFiltering,
1006 .isSecure = outputState.isSecure,
1007 .supportsProtectedContent = supportsProtectedContent,
1008 .clearRegion = clientComposition ? clearRegion : stubRegion,
1009 .viewport = outputState.layerStackSpace.content,
1010 .dataspace = outputDataspace,
1011 .realContentIsVisible = realContentIsVisible,
1012 .clearContent = !clientComposition};
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001013 std::vector<LayerFE::LayerSettings> results =
1014 layerFE.prepareClientCompositionList(targetSettings);
1015 if (realContentIsVisible && !results.empty()) {
1016 layer->editState().clientCompositionTimestamp = systemTime();
Lloyd Pique688abd42019-02-15 15:42:24 -08001017 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001018
1019 clientCompositionLayers.insert(clientCompositionLayers.end(),
1020 std::make_move_iterator(results.begin()),
1021 std::make_move_iterator(results.end()));
1022 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001023 }
1024
1025 firstLayer = false;
1026 }
1027
1028 return clientCompositionLayers;
1029}
1030
1031void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001032 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001033 if (flashRegion.isEmpty()) {
1034 return;
1035 }
1036
Vishnu Nair9b079a22020-01-21 14:36:08 -08001037 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001038 layerSettings.source.buffer.buffer = nullptr;
1039 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1040 layerSettings.alpha = half(1.0);
1041
1042 for (const auto& rect : flashRegion) {
1043 layerSettings.geometry.boundaries = rect.toFloatRect();
1044 clientCompositionLayers.push_back(layerSettings);
1045 }
1046}
1047
1048void Output::setExpensiveRenderingExpected(bool) {
1049 // The base class does nothing with this call.
1050}
1051
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001052void Output::postFramebuffer() {
1053 ATRACE_CALL();
1054 ALOGV(__FUNCTION__);
1055
1056 if (!getState().isEnabled) {
1057 return;
1058 }
1059
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001060 auto& outputState = editState();
1061 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001062 mRenderSurface->flip();
1063
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001064 auto frame = presentAndGetFrameFences();
1065
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001066 mRenderSurface->onPresentDisplayCompleted();
1067
Lloyd Pique01c77c12019-04-17 12:48:32 -07001068 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001069 // The layer buffer from the previous frame (if any) is released
1070 // by HWC only when the release fence from this frame (if any) is
1071 // signaled. Always get the release fence from HWC first.
1072 sp<Fence> releaseFence = Fence::NO_FENCE;
1073
1074 if (auto hwcLayer = layer->getHwcLayer()) {
1075 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1076 releaseFence = f->second;
1077 }
1078 }
1079
1080 // If the layer was client composited in the previous frame, we
1081 // need to merge with the previous client target acquire fence.
1082 // Since we do not track that, always merge with the current
1083 // client target acquire fence when it is available, even though
1084 // this is suboptimal.
1085 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001086 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001087 releaseFence =
1088 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1089 }
1090
1091 layer->getLayerFE().onLayerDisplayed(releaseFence);
1092 }
1093
1094 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001095 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001096 // supply them with the present fence.
1097 for (auto& weakLayer : mReleasedLayers) {
1098 if (auto layer = weakLayer.promote(); layer != nullptr) {
1099 layer->onLayerDisplayed(frame.presentFence);
1100 }
1101 }
1102
1103 // Clear out the released layers now that we're done with them.
1104 mReleasedLayers.clear();
1105}
1106
Lloyd Pique32cbe282018-10-19 13:09:22 -07001107void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001108 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001109 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001110}
1111
Lloyd Pique66d68602019-02-13 14:23:31 -08001112void Output::chooseCompositionStrategy() {
1113 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001114 auto& outputState = editState();
1115 outputState.usesClientComposition = true;
1116 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001117 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001118}
1119
Lloyd Pique688abd42019-02-15 15:42:24 -08001120bool Output::getSkipColorTransform() const {
1121 return true;
1122}
1123
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001124compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1125 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001126 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001127 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1128 }
1129 return result;
1130}
1131
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001132} // namespace impl
1133} // namespace android::compositionengine