blob: 6c2f9c515c43c0d01ed0e87cedfcf2fff734b599 [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
Alec Mouria90a5702021-04-16 16:36:21 +000017#include <SurfaceFlingerProperties.sysprop.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070018#include <android-base/stringprintf.h>
19#include <compositionengine/CompositionEngine.h>
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080020#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070021#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080022#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070023#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070024#include <compositionengine/RenderSurface.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070025#include <compositionengine/impl/Output.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070026#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080027#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070028#include <compositionengine/impl/OutputLayerCompositionState.h>
Dan Stoza269dc4d2021-01-15 15:07:43 -080029#include <compositionengine/impl/planner/Planner.h>
30
Alec Mouria90a5702021-04-16 16:36:21 +000031#include <thread>
32
33#include "renderengine/ExternalTexture.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080034
35// TODO(b/129481165): remove the #pragma below and fix conversion issues
36#pragma clang diagnostic push
37#pragma clang diagnostic ignored "-Wconversion"
38
Lloyd Pique688abd42019-02-15 15:42:24 -080039#include <renderengine/DisplaySettings.h>
40#include <renderengine/RenderEngine.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080041
42// TODO(b/129481165): remove the #pragma below and fix conversion issues
43#pragma clang diagnostic pop // ignored "-Wconversion"
44
Dan Stoza269dc4d2021-01-15 15:07:43 -080045#include <android-base/properties.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070046#include <ui/DebugUtils.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080047#include <ui/HdrCapabilities.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080048#include <utils/Trace.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070049
Lloyd Pique688abd42019-02-15 15:42:24 -080050#include "TracedOrdinal.h"
51
Lloyd Piquefeb73d72018-12-04 17:23:44 -080052namespace android::compositionengine {
53
54Output::~Output() = default;
55
56namespace impl {
Lloyd Pique32cbe282018-10-19 13:09:22 -070057
Dan Stoza269dc4d2021-01-15 15:07:43 -080058Output::Output() {
59 const bool enableLayerCaching = [] {
60 const bool enable =
61 android::sysprop::SurfaceFlingerProperties::enable_layer_caching().value_or(false);
62 return base::GetBoolProperty(std::string("debug.sf.enable_layer_caching"), enable);
63 }();
64
65 if (enableLayerCaching) {
66 mPlanner = std::make_unique<planner::Planner>();
67 }
68}
69
Lloyd Piquec29e4c62019-03-07 21:48:19 -080070namespace {
71
72template <typename T>
73class Reversed {
74public:
75 explicit Reversed(const T& container) : mContainer(container) {}
76 auto begin() { return mContainer.rbegin(); }
77 auto end() { return mContainer.rend(); }
78
79private:
80 const T& mContainer;
81};
82
83// Helper for enumerating over a container in reverse order
84template <typename T>
85Reversed<T> reversed(const T& c) {
86 return Reversed<T>(c);
87}
88
Marin Shalamanovb15d2272020-09-17 21:41:52 +020089struct ScaleVector {
90 float x;
91 float y;
92};
93
94// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
95// where to' will have the same size as "to". In the case where "from" and "to"
96// start at the origin to'=to.
97ScaleVector getScale(const Rect& from, const Rect& to) {
98 return {.x = static_cast<float>(to.width()) / from.width(),
99 .y = static_cast<float>(to.height()) / from.height()};
100}
101
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800102} // namespace
103
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700104std::shared_ptr<Output> createOutput(
105 const compositionengine::CompositionEngine& compositionEngine) {
106 return createOutputTemplated<Output>(compositionEngine);
107}
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108
109Output::~Output() = default;
110
Lloyd Pique32cbe282018-10-19 13:09:22 -0700111bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700112 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
113 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700114}
115
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700116std::optional<DisplayId> Output::getDisplayId() const {
117 return {};
118}
119
Lloyd Pique32cbe282018-10-19 13:09:22 -0700120const std::string& Output::getName() const {
121 return mName;
122}
123
124void Output::setName(const std::string& name) {
125 mName = name;
126}
127
128void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700129 auto& outputState = editState();
130 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700131 return;
132 }
133
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700134 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700135 dirtyEntireOutput();
136}
137
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200138void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
139 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700140 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200141
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200142 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200143 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
144 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200145
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200146 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200147 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200148 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200149 std::swap(orientedSize.width, orientedSize.height);
150 }
151 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200152 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
153
154 // Compute displaySpace.content
155 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
156 ui::Transform rotation;
157 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
158 const auto displaySize = outputState.displaySpace.bounds;
159 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
160 }
161 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200162
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200163 // Compute framebufferSpace
164 outputState.framebufferSpace.orientation = orientation;
165 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
166 "The framebuffer bounds are unknown.");
167 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200168 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200169 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
170
171 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200172 outputState.layerStackSpace.content = layerStackSpaceRect;
173 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200174
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200175 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
176 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700177 dirtyEntireOutput();
178}
179
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200180void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700181 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200182
183 auto& state = editState();
184
185 // Update framebuffer space
186 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200187 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200188
189 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200190 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
192
193 // Update oriented display space
194 const auto orientation = state.displaySpace.orientation;
195 ui::Size orientedSize = size;
196 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
197 std::swap(orientedSize.width, orientedSize.height);
198 }
199 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200200 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700201
Dan Stoza6166c312021-01-15 16:34:05 -0800202 if (mPlanner) {
203 mPlanner->setDisplaySize(size);
204 }
205
Lloyd Pique32cbe282018-10-19 13:09:22 -0700206 dirtyEntireOutput();
207}
208
Garfield Tan54edd912020-10-21 16:31:41 -0700209ui::Transform::RotationFlags Output::getTransformHint() const {
210 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
211}
212
Lloyd Piqueef36b002019-01-23 17:52:04 -0800213void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700214 auto& outputState = editState();
215 outputState.layerStackId = layerStackId;
216 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700217
218 dirtyEntireOutput();
219}
220
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800221void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700222 auto& colorTransformMatrix = editState().colorTransformMatrix;
223 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700224 return;
225 }
226
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700227 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800228
229 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700230}
231
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800232void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700233 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800234 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
235 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800236
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700237 auto& outputState = editState();
238 if (outputState.colorMode == colorProfile.mode &&
239 outputState.dataspace == colorProfile.dataspace &&
240 outputState.renderIntent == colorProfile.renderIntent &&
241 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800242 return;
243 }
244
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700245 outputState.colorMode = colorProfile.mode;
246 outputState.dataspace = colorProfile.dataspace;
247 outputState.renderIntent = colorProfile.renderIntent;
248 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700249
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800250 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700251
Lloyd Pique32cbe282018-10-19 13:09:22 -0700252 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800253 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
254 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800255
256 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700257}
258
John Reckac09e452021-04-07 16:35:37 -0400259void Output::setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) {
260 auto& outputState = editState();
261 if (outputState.sdrWhitePointNits == sdrWhitePointNits &&
262 outputState.displayBrightnessNits == displayBrightnessNits) {
263 // Nothing changed
264 return;
265 }
266 outputState.sdrWhitePointNits = sdrWhitePointNits;
267 outputState.displayBrightnessNits = displayBrightnessNits;
268 dirtyEntireOutput();
269}
270
Lloyd Pique32cbe282018-10-19 13:09:22 -0700271void Output::dump(std::string& out) const {
272 using android::base::StringAppendF;
273
274 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
275
276 out.append("\n ");
277
278 dumpBase(out);
279}
280
281void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700282 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700283
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700284 if (mDisplayColorProfile) {
285 mDisplayColorProfile->dump(out);
286 } else {
287 out.append(" No display color profile!\n");
288 }
289
Lloyd Pique31cb2942018-10-19 17:23:03 -0700290 if (mRenderSurface) {
291 mRenderSurface->dump(out);
292 } else {
293 out.append(" No render surface!\n");
294 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800295
Lloyd Pique01c77c12019-04-17 12:48:32 -0700296 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
297 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800298 if (!outputLayer) {
299 continue;
300 }
301 outputLayer->dump(out);
302 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700303}
304
Dan Stoza269dc4d2021-01-15 15:07:43 -0800305void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
306 if (!mPlanner) {
307 base::StringAppendF(&out, "Planner is disabled\n");
308 return;
309 }
310 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
311 mPlanner->dump(args, out);
312}
313
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700314compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
315 return mDisplayColorProfile.get();
316}
317
318void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
319 mDisplayColorProfile = std::move(mode);
320}
321
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800322const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
323 return mReleasedLayers;
324}
325
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700326void Output::setDisplayColorProfileForTest(
327 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
328 mDisplayColorProfile = std::move(mode);
329}
330
Lloyd Pique31cb2942018-10-19 17:23:03 -0700331compositionengine::RenderSurface* Output::getRenderSurface() const {
332 return mRenderSurface.get();
333}
334
335void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
336 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800337 const auto size = mRenderSurface->getSize();
338 editState().framebufferSpace.bounds = Rect(size);
339 if (mPlanner) {
340 mPlanner->setDisplaySize(size);
341 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700342 dirtyEntireOutput();
343}
344
Vishnu Nair9b079a22020-01-21 14:36:08 -0800345void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
346 if (cacheSize == 0) {
347 mClientCompositionRequestCache.reset();
348 } else {
349 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
350 }
351};
352
Lloyd Pique31cb2942018-10-19 17:23:03 -0700353void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
354 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700355}
356
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000357Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700358 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200359 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000360 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700361 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700362 }
363 return dirty;
364}
365
Lloyd Piquec6687342019-03-07 21:34:57 -0800366bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800367 // The layerStackId's must match, and also the layer must not be internal
368 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700369 const auto& outputState = getState();
370 return layerStackId && (*layerStackId == outputState.layerStackId) &&
371 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700372}
373
Lloyd Piquede196652020-01-22 17:29:58 -0800374bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
375 const auto* layerFEState = layerFE->getCompositionState();
376 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800377}
378
Lloyd Piquedf336d92019-03-07 21:38:42 -0800379std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800380 const sp<LayerFE>& layerFE) const {
381 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800382}
383
Lloyd Piquede196652020-01-22 17:29:58 -0800384compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
385 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700386 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800387}
388
Lloyd Pique01c77c12019-04-17 12:48:32 -0700389std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800390 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700391 for (size_t i = 0; i < getOutputLayerCount(); i++) {
392 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800393 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700394 return i;
395 }
396 }
397 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800398}
399
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800400void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
401 mReleasedLayers = std::move(layers);
402}
403
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800404void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
405 LayerFESet& geomSnapshots) {
406 ATRACE_CALL();
407 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800408
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800409 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800410}
411
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800412void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800413 ATRACE_CALL();
414 ALOGV(__FUNCTION__);
415
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800416 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800417 updateCompositionState(refreshArgs);
418 planComposition();
419 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800420 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800421 beginFrame();
422 prepareFrame();
423 devOptRepaintFlash(refreshArgs);
424 finishFrame(refreshArgs);
425 postFramebuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800426 renderCachedSets();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800427}
428
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800429void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
430 LayerFESet& layerFESet) {
431 ATRACE_CALL();
432 ALOGV(__FUNCTION__);
433
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700434 auto& outputState = editState();
435
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800436 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800438 return;
439 }
440
441 // Process the layers to determine visibility and coverage
442 compositionengine::Output::CoverageState coverage{layerFESet};
443 collectVisibleLayers(refreshArgs, coverage);
444
445 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700446 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200447 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800448 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
449
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700450 outputState.undefinedRegion = undefinedRegion;
451 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800452}
453
454void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
455 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800456 // Evaluate the layers from front to back to determine what is visible. This
457 // also incrementally calculates the coverage information for each layer as
458 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800459 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700460 // Incrementally process the coverage for each layer
461 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800462
463 // TODO(b/121291683): Stop early if the output is completely covered and
464 // no more layers could even be visible underneath the ones on top.
465 }
466
Lloyd Pique01c77c12019-04-17 12:48:32 -0700467 setReleasedLayers(refreshArgs);
468
469 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800470}
471
Lloyd Piquede196652020-01-22 17:29:58 -0800472void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700473 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800474 // Ensure we have a snapshot of the basic geometry layer state. Limit the
475 // snapshots to once per frame for each candidate layer, as layers may
476 // appear on multiple outputs.
477 if (!coverage.latchedLayers.count(layerFE)) {
478 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800479 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800480 }
481
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800482 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800483 if (!belongsInOutput(layerFE)) {
484 return;
485 }
486
487 // Obtain a read-only pointer to the front-end layer state
488 const auto* layerFEState = layerFE->getCompositionState();
489 if (CC_UNLIKELY(!layerFEState)) {
490 return;
491 }
492
493 // handle hidden surfaces by setting the visible region to empty
494 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700495 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800496 }
497
498 /*
499 * opaqueRegion: area of a surface that is fully opaque.
500 */
501 Region opaqueRegion;
502
503 /*
504 * visibleRegion: area of a surface that is visible on screen and not fully
505 * transparent. This is essentially the layer's footprint minus the opaque
506 * regions above it. Areas covered by a translucent surface are considered
507 * visible.
508 */
509 Region visibleRegion;
510
511 /*
512 * coveredRegion: area of a surface that is covered by all visible regions
513 * above it (which includes the translucent areas).
514 */
515 Region coveredRegion;
516
517 /*
518 * transparentRegion: area of a surface that is hinted to be completely
519 * transparent. This is only used to tell when the layer has no visible non-
520 * transparent regions and can be removed from the layer list. It does not
521 * affect the visibleRegion of this layer or any layers beneath it. The hint
522 * may not be correct if apps don't respect the SurfaceView restrictions
523 * (which, sadly, some don't).
524 */
525 Region transparentRegion;
526
Vishnu Naira483b4a2019-12-12 15:07:52 -0800527 /*
528 * shadowRegion: Region cast by the layer's shadow.
529 */
530 Region shadowRegion;
531
Lloyd Piquede196652020-01-22 17:29:58 -0800532 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800533
534 // Get the visible region
535 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
536 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800537 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800538 visibleRegion.set(visibleRect);
539
Lloyd Piquede196652020-01-22 17:29:58 -0800540 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800541 // if the layer casts a shadow, offset the layers visible region and
542 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800543 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800544 Rect visibleRectWithShadows(visibleRect);
545 visibleRectWithShadows.inset(inset, inset, inset, inset);
546 visibleRegion.set(visibleRectWithShadows);
547 shadowRegion = visibleRegion.subtract(visibleRect);
548 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800549
550 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700551 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800552 }
553
554 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800555 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800556 if (tr.preserveRects()) {
557 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800558 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800559 } else {
560 // transformation too complex, can't do the
561 // transparent region optimization.
562 transparentRegion.clear();
563 }
564 }
565
566 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800567 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800568 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800569 // If we one of the simple category of transforms (0/90/180/270 rotation
570 // + any flip), then the opaque region is the layer's footprint.
571 // Otherwise we don't try and compute the opaque region since there may
572 // be errors at the edges, and we treat the entire layer as
573 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800574 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800575 }
576
577 // Clip the covered region to the visible region
578 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
579
580 // Update accumAboveCoveredLayers for next (lower) layer
581 coverage.aboveCoveredLayers.orSelf(visibleRegion);
582
583 // subtract the opaque region covered by the layers above us
584 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
585
586 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700587 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800588 }
589
590 // Get coverage information for the layer as previously displayed,
591 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800592 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700593 auto prevOutputLayer =
594 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800595
596 // Get coverage information for the layer as previously displayed
597 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
598 const Region kEmptyRegion;
599 const Region& oldVisibleRegion =
600 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
601 const Region& oldCoveredRegion =
602 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
603
604 // compute this layer's dirty region
605 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800606 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800607 // we need to invalidate the whole region
608 dirty = visibleRegion;
609 // as well, as the old visible region
610 dirty.orSelf(oldVisibleRegion);
611 } else {
612 /* compute the exposed region:
613 * the exposed region consists of two components:
614 * 1) what's VISIBLE now and was COVERED before
615 * 2) what's EXPOSED now less what was EXPOSED before
616 *
617 * note that (1) is conservative, we start with the whole visible region
618 * but only keep what used to be covered by something -- which mean it
619 * may have been exposed.
620 *
621 * (2) handles areas that were not covered by anything but got exposed
622 * because of a resize.
623 *
624 */
625 const Region newExposed = visibleRegion - coveredRegion;
626 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
627 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
628 }
629 dirty.subtractSelf(coverage.aboveOpaqueLayers);
630
631 // accumulate to the screen dirty region
632 coverage.dirtyRegion.orSelf(dirty);
633
634 // Update accumAboveOpaqueLayers for next (lower) layer
635 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
636
637 // Compute the visible non-transparent region
638 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
639
Vishnu Naira483b4a2019-12-12 15:07:52 -0800640 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800641 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700642 const auto& outputState = getState();
643 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200644 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800645 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700646 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800647 }
648
Vishnu Naira483b4a2019-12-12 15:07:52 -0800649 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
650
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800651 // The layer is visible. Either reuse the existing outputLayer if we have
652 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800653 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800654
655 // Store the layer coverage information into the layer state as some of it
656 // is useful later.
657 auto& outputLayerState = result->editState();
658 outputLayerState.visibleRegion = visibleRegion;
659 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
660 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200661 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
662 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800663 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800664}
665
666void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
667 // The base class does nothing with this call.
668}
669
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800670void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700671 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800672 layer->getLayerFE().prepareCompositionState(
673 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
674 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800675 }
676}
677
Dan Stoza269dc4d2021-01-15 15:07:43 -0800678void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800679 ATRACE_CALL();
680 ALOGV(__FUNCTION__);
681
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800682 if (!getState().isEnabled) {
683 return;
684 }
685
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800686 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
687 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
688
Lloyd Pique01c77c12019-04-17 12:48:32 -0700689 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700690 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800691 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200692 forceClientComposition,
693 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800694
695 if (mLayerRequestingBackgroundBlur == layer) {
696 forceClientComposition = false;
697 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800698 }
699}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800700
Dan Stoza269dc4d2021-01-15 15:07:43 -0800701void Output::planComposition() {
702 if (!mPlanner || !getState().isEnabled) {
703 return;
704 }
705
706 ATRACE_CALL();
707 ALOGV(__FUNCTION__);
708
709 mPlanner->plan(getOutputLayersOrderedByZ());
710}
711
712void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
713 ATRACE_CALL();
714 ALOGV(__FUNCTION__);
715
716 if (!getState().isEnabled) {
717 return;
718 }
719
Ady Abraham3645e642021-04-20 18:39:00 -0700720 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
721
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400722 OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800723 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400724 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400725 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400726 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800727 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400728 if (layer == peekThroughLayer) {
729 // No longer needed, although it should not show up again, so
730 // resetting it is not truly needed either.
731 peekThroughLayer = nullptr;
732
733 // peekThroughLayer was already drawn ahead of its z order.
734 continue;
735 }
Dan Stoza6166c312021-01-15 16:34:05 -0800736 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400737 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400738 if (overrideInfo.buffer != nullptr) {
739 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800740 ALOGV("Skipping redundant buffer");
741 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400742 } else {
743 // First layer with the override buffer.
744 if (overrideInfo.peekThroughLayer) {
745 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400746
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400747 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400748 overrideZ = true;
749 includeGeometry = true;
750 constexpr bool isPeekingThrough = true;
751 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
752 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400753 }
754
755 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800756 }
Dan Stoza6166c312021-01-15 16:34:05 -0800757 }
758
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400759 constexpr bool isPeekingThrough = false;
760 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800761 }
762}
763
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800764compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
765 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
766 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100767 auto* compState = layer->getLayerFE().getCompositionState();
768
769 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
770 // want to force client composition because of the blur.
771 if (compState->sidebandStream != nullptr) {
772 return nullptr;
773 }
774 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800775 layerRequestingBgComposition = layer;
776 }
777 }
778 return layerRequestingBgComposition;
779}
780
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800781void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
782 setColorProfile(pickColorProfile(refreshArgs));
783}
784
785// Returns a data space that fits all visible layers. The returned data space
786// can only be one of
787// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
788// - Dataspace::DISPLAY_P3
789// - Dataspace::DISPLAY_BT2020
790// The returned HDR data space is one of
791// - Dataspace::UNKNOWN
792// - Dataspace::BT2020_HLG
793// - Dataspace::BT2020_PQ
794ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
795 bool* outIsHdrClientComposition) const {
796 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
797 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
798
Lloyd Pique01c77c12019-04-17 12:48:32 -0700799 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800800 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800801 case ui::Dataspace::V0_SCRGB:
802 case ui::Dataspace::V0_SCRGB_LINEAR:
803 case ui::Dataspace::BT2020:
804 case ui::Dataspace::BT2020_ITU:
805 case ui::Dataspace::BT2020_LINEAR:
806 case ui::Dataspace::DISPLAY_BT2020:
807 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
808 break;
809 case ui::Dataspace::DISPLAY_P3:
810 bestDataSpace = ui::Dataspace::DISPLAY_P3;
811 break;
812 case ui::Dataspace::BT2020_PQ:
813 case ui::Dataspace::BT2020_ITU_PQ:
814 bestDataSpace = ui::Dataspace::DISPLAY_P3;
815 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800816 *outIsHdrClientComposition =
817 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800818 break;
819 case ui::Dataspace::BT2020_HLG:
820 case ui::Dataspace::BT2020_ITU_HLG:
821 bestDataSpace = ui::Dataspace::DISPLAY_P3;
822 // When there's mixed PQ content and HLG content, we set the HDR
823 // data space to be BT2020_PQ and convert HLG to PQ.
824 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
825 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
826 }
827 break;
828 default:
829 break;
830 }
831 }
832
833 return bestDataSpace;
834}
835
836compositionengine::Output::ColorProfile Output::pickColorProfile(
837 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
838 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
839 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
840 ui::RenderIntent::COLORIMETRIC,
841 refreshArgs.colorSpaceAgnosticDataspace};
842 }
843
844 ui::Dataspace hdrDataSpace;
845 bool isHdrClientComposition = false;
846 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
847
848 switch (refreshArgs.forceOutputColorMode) {
849 case ui::ColorMode::SRGB:
850 bestDataSpace = ui::Dataspace::V0_SRGB;
851 break;
852 case ui::ColorMode::DISPLAY_P3:
853 bestDataSpace = ui::Dataspace::DISPLAY_P3;
854 break;
855 default:
856 break;
857 }
858
859 // respect hdrDataSpace only when there is no legacy HDR support
860 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
861 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
862 if (isHdr) {
863 bestDataSpace = hdrDataSpace;
864 }
865
866 ui::RenderIntent intent;
867 switch (refreshArgs.outputColorSetting) {
868 case OutputColorSetting::kManaged:
869 case OutputColorSetting::kUnmanaged:
870 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
871 : ui::RenderIntent::COLORIMETRIC;
872 break;
873 case OutputColorSetting::kEnhanced:
874 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
875 break;
876 default: // vendor display color setting
877 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
878 break;
879 }
880
881 ui::ColorMode outMode;
882 ui::Dataspace outDataSpace;
883 ui::RenderIntent outRenderIntent;
884 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
885 &outRenderIntent);
886
887 return ColorProfile{outMode, outDataSpace, outRenderIntent,
888 refreshArgs.colorSpaceAgnosticDataspace};
889}
890
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800891void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700892 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800893 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700894 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700895 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800896
897 // If nothing has changed (!dirty), don't recompose.
898 // If something changed, but we don't currently have any visible layers,
899 // and didn't when we last did a composition, then skip it this time.
900 // The second rule does two things:
901 // - When all layers are removed from a display, we'll emit one black
902 // frame, then nothing more until we get new layers.
903 // - When a display is created with a private layer stack, we won't
904 // emit any black frames until a layer is added to the layer stack.
905 const bool mustRecompose = dirty && !(empty && wasEmpty);
906
907 const char flagPrefix[] = {'-', '+'};
908 static_cast<void>(flagPrefix);
909 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
910 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
911 flagPrefix[empty], flagPrefix[wasEmpty]);
912
913 mRenderSurface->beginFrame(mustRecompose);
914
915 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700916 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800917 }
918}
919
Lloyd Pique66d68602019-02-13 14:23:31 -0800920void Output::prepareFrame() {
921 ATRACE_CALL();
922 ALOGV(__FUNCTION__);
923
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700924 const auto& outputState = getState();
925 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800926 return;
927 }
928
929 chooseCompositionStrategy();
930
Dan Stoza47437bb2021-01-15 16:21:07 -0800931 if (mPlanner) {
932 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
933 }
934
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700935 mRenderSurface->prepareFrame(outputState.usesClientComposition,
936 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800937}
938
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800939void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
940 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
941 return;
942 }
943
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700944 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800945 // transform the dirty region into this screen's coordinate space
946 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
947 if (!dirtyRegion.isEmpty()) {
948 base::unique_fd readyFence;
949 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800950 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800951
952 mRenderSurface->queueBuffer(std::move(readyFence));
953 }
954 }
955
956 postFramebuffer();
957
958 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
959
960 prepareFrame();
961}
962
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800963void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800964 ATRACE_CALL();
965 ALOGV(__FUNCTION__);
966
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700967 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800968 return;
969 }
970
971 // Repaint the framebuffer (if needed), getting the optional fence for when
972 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800973 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800974 if (!optReadyFence) {
975 return;
976 }
977
978 // swap buffers (presentation)
979 mRenderSurface->queueBuffer(std::move(*optReadyFence));
980}
981
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800982std::optional<base::unique_fd> Output::composeSurfaces(
983 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800984 ATRACE_CALL();
985 ALOGV(__FUNCTION__);
986
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700987 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800988 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800989 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700990 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700991
992 auto& renderEngine = getCompositionEngine().getRenderEngine();
993 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
994
995 // If we the display is secure, protected content support is enabled, and at
996 // least one layer has protected content, we need to use a secure back
997 // buffer.
998 if (outputState.isSecure && supportsProtectedContent) {
999 auto layers = getOutputLayersOrderedByZ();
1000 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
1001 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
1002 });
1003 if (needsProtected != renderEngine.isProtected()) {
1004 renderEngine.useProtectedContext(needsProtected);
1005 }
1006 if (needsProtected != mRenderSurface->isProtected() &&
1007 needsProtected == renderEngine.isProtected()) {
1008 mRenderSurface->setProtected(needsProtected);
1009 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001010 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1011 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001012 }
1013
1014 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001015
1016 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001017
1018 // If we aren't doing client composition on this output, but do have a
1019 // flipClientTarget request for this frame on this output, we still need to
1020 // dequeue a buffer.
1021 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001022 tex = mRenderSurface->dequeueBuffer(&fd);
1023 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001024 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1025 "client composition for this frame",
1026 mName.c_str());
1027 return {};
1028 }
1029 }
1030
Lloyd Piqued3d69882019-02-28 16:03:46 -08001031 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001032 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001033 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001034 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001035 }
1036
1037 ALOGV("hasClientComposition");
1038
Lloyd Pique688abd42019-02-15 15:42:24 -08001039 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001040 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001041 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001042 clientCompositionDisplay.orientation =
1043 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001044 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1045 ? outputState.dataspace
1046 : ui::Dataspace::UNKNOWN;
John Reckac09e452021-04-07 16:35:37 -04001047
1048 // If we have a valid current display brightness use that, otherwise fall back to the
1049 // display's max desired
1050 clientCompositionDisplay.maxLuminance = outputState.displayBrightnessNits > 0.f
1051 ? outputState.displayBrightnessNits
1052 : mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1053 clientCompositionDisplay.sdrWhitePointNits = outputState.sdrWhitePointNits;
Lloyd Pique688abd42019-02-15 15:42:24 -08001054
1055 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001056 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1057 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001058 }
1059
1060 // Note: Updated by generateClientCompositionRequests
1061 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1062
1063 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001064 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001065 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001066 clientCompositionDisplay.clearRegion,
1067 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001068 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1069
Vishnu Nair9b079a22020-01-21 14:36:08 -08001070 // Check if the client composition requests were rendered into the provided graphic buffer. If
1071 // so, we can reuse the buffer and avoid client composition.
1072 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001073 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1074 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001075 clientCompositionLayers)) {
1076 outputCompositionState.reusedClientComposition = true;
1077 setExpensiveRenderingExpected(false);
1078 return readyFence;
1079 }
Alec Mouria90a5702021-04-16 16:36:21 +00001080 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001081 clientCompositionLayers);
1082 }
1083
Lloyd Pique688abd42019-02-15 15:42:24 -08001084 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001085 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1086 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1087 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001088 const bool expensiveBlurs =
1089 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001090 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001091 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001092 if (expensiveRenderingExpected) {
1093 setExpensiveRenderingExpected(true);
1094 }
1095
Vishnu Nair9b079a22020-01-21 14:36:08 -08001096 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1097 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1098 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1099 std::back_inserter(clientCompositionLayerPointers),
1100 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1101 return &settings;
1102 });
1103
Alec Mourie4034bb2019-11-19 12:45:54 -08001104 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001105 // Only use the framebuffer cache when rendering to an internal display
1106 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1107 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1108 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1109 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1110 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1111 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001112 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001113 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001114 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001115
1116 if (status != NO_ERROR && mClientCompositionRequestCache) {
1117 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001118 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001119 }
1120
Alec Mourie4034bb2019-11-19 12:45:54 -08001121 auto& timeStats = getCompositionEngine().getTimeStats();
1122 if (readyFence.get() < 0) {
1123 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1124 } else {
1125 timeStats.recordRenderEngineDuration(renderEngineStart,
1126 std::make_shared<FenceTime>(
1127 new Fence(dup(readyFence.get()))));
1128 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001129
Lloyd Piqued3d69882019-02-28 16:03:46 -08001130 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001131}
1132
Vishnu Nair9b079a22020-01-21 14:36:08 -08001133std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001134 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001135 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001136 ALOGV("Rendering client layers");
1137
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001138 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001139 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001140 bool firstLayer = true;
1141 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001142 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001143
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001144 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001145 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001146
Lloyd Pique01c77c12019-04-17 12:48:32 -07001147 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001148 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001149 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001150 auto& layerFE = layer->getLayerFE();
1151
Lloyd Piquea2468662019-03-07 21:31:06 -08001152 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001153 ALOGV("Layer: %s", layerFE.getDebugName());
1154 if (clip.isEmpty()) {
1155 ALOGV(" Skipping for empty clip");
1156 firstLayer = false;
1157 continue;
1158 }
1159
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001160 disableBlurs |= layerFEState->sidebandStream != nullptr;
1161
Vishnu Naira483b4a2019-12-12 15:07:52 -08001162 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001163
1164 // We clear the client target for non-client composed layers if
1165 // requested by the HWC. We skip this if the layer is not an opaque
1166 // rectangle, as by definition the layer must blend with whatever is
1167 // underneath. We also skip the first layer as the buffer target is
1168 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001169 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001170 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001171
1172 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1173
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001174 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1175 // composing the non-shadow content and only draw the shadows.
1176 const bool realContentIsVisible = clientComposition &&
1177 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1178
Lloyd Pique688abd42019-02-15 15:42:24 -08001179 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001180 compositionengine::LayerFE::ClientCompositionTargetSettings
1181 targetSettings{.clip = clip,
1182 .needsFiltering =
1183 layer->needsFiltering() || outputState.needsFiltering,
1184 .isSecure = outputState.isSecure,
1185 .supportsProtectedContent = supportsProtectedContent,
1186 .clearRegion = clientComposition ? clearRegion : stubRegion,
1187 .viewport = outputState.layerStackSpace.content,
1188 .dataspace = outputDataspace,
1189 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001190 .clearContent = !clientComposition,
1191 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001192
1193 std::vector<LayerFE::LayerSettings> results;
1194 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001195 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001196 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001197 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001198 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1199 } else {
1200 ALOGV("Skipping redundant override buffer for [%s] in RE",
1201 layer->getLayerFE().getDebugName());
1202 }
Dan Stoza6166c312021-01-15 16:34:05 -08001203 } else {
1204 results = layerFE.prepareClientCompositionList(targetSettings);
1205 if (realContentIsVisible && !results.empty()) {
1206 layer->editState().clientCompositionTimestamp = systemTime();
1207 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001208 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001209
1210 clientCompositionLayers.insert(clientCompositionLayers.end(),
1211 std::make_move_iterator(results.begin()),
1212 std::make_move_iterator(results.end()));
1213 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001214 }
1215
1216 firstLayer = false;
1217 }
1218
1219 return clientCompositionLayers;
1220}
1221
1222void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001223 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001224 if (flashRegion.isEmpty()) {
1225 return;
1226 }
1227
Vishnu Nair9b079a22020-01-21 14:36:08 -08001228 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001229 layerSettings.source.buffer.buffer = nullptr;
1230 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1231 layerSettings.alpha = half(1.0);
1232
1233 for (const auto& rect : flashRegion) {
1234 layerSettings.geometry.boundaries = rect.toFloatRect();
1235 clientCompositionLayers.push_back(layerSettings);
1236 }
1237}
1238
1239void Output::setExpensiveRenderingExpected(bool) {
1240 // The base class does nothing with this call.
1241}
1242
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001243void Output::postFramebuffer() {
1244 ATRACE_CALL();
1245 ALOGV(__FUNCTION__);
1246
1247 if (!getState().isEnabled) {
1248 return;
1249 }
1250
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001251 auto& outputState = editState();
1252 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001253 mRenderSurface->flip();
1254
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001255 auto frame = presentAndGetFrameFences();
1256
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001257 mRenderSurface->onPresentDisplayCompleted();
1258
Lloyd Pique01c77c12019-04-17 12:48:32 -07001259 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001260 // The layer buffer from the previous frame (if any) is released
1261 // by HWC only when the release fence from this frame (if any) is
1262 // signaled. Always get the release fence from HWC first.
1263 sp<Fence> releaseFence = Fence::NO_FENCE;
1264
1265 if (auto hwcLayer = layer->getHwcLayer()) {
1266 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1267 releaseFence = f->second;
1268 }
1269 }
1270
1271 // If the layer was client composited in the previous frame, we
1272 // need to merge with the previous client target acquire fence.
1273 // Since we do not track that, always merge with the current
1274 // client target acquire fence when it is available, even though
1275 // this is suboptimal.
1276 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001277 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001278 releaseFence =
1279 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1280 }
1281
1282 layer->getLayerFE().onLayerDisplayed(releaseFence);
1283 }
1284
1285 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001286 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001287 // supply them with the present fence.
1288 for (auto& weakLayer : mReleasedLayers) {
1289 if (auto layer = weakLayer.promote(); layer != nullptr) {
1290 layer->onLayerDisplayed(frame.presentFence);
1291 }
1292 }
1293
1294 // Clear out the released layers now that we're done with them.
1295 mReleasedLayers.clear();
1296}
1297
Dan Stoza6166c312021-01-15 16:34:05 -08001298void Output::renderCachedSets() {
1299 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001300 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001301 }
1302}
1303
Lloyd Pique32cbe282018-10-19 13:09:22 -07001304void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001305 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001306 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001307}
1308
Lloyd Pique66d68602019-02-13 14:23:31 -08001309void Output::chooseCompositionStrategy() {
1310 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001311 auto& outputState = editState();
1312 outputState.usesClientComposition = true;
1313 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001314 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001315}
1316
Lloyd Pique688abd42019-02-15 15:42:24 -08001317bool Output::getSkipColorTransform() const {
1318 return true;
1319}
1320
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001321compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1322 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001323 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001324 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1325 }
1326 return result;
1327}
1328
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001329} // namespace impl
1330} // namespace android::compositionengine