blob: 796fe7dbf004b08b18594e43328ba24aed398537 [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
Lloyd Piquec29e4c62019-03-07 21:48:19 -080058namespace {
59
60template <typename T>
61class Reversed {
62public:
63 explicit Reversed(const T& container) : mContainer(container) {}
64 auto begin() { return mContainer.rbegin(); }
65 auto end() { return mContainer.rend(); }
66
67private:
68 const T& mContainer;
69};
70
71// Helper for enumerating over a container in reverse order
72template <typename T>
73Reversed<T> reversed(const T& c) {
74 return Reversed<T>(c);
75}
76
Marin Shalamanovb15d2272020-09-17 21:41:52 +020077struct ScaleVector {
78 float x;
79 float y;
80};
81
82// Returns a ScaleVector (x, y) such that from.scale(x, y) = to',
83// where to' will have the same size as "to". In the case where "from" and "to"
84// start at the origin to'=to.
85ScaleVector getScale(const Rect& from, const Rect& to) {
86 return {.x = static_cast<float>(to.width()) / from.width(),
87 .y = static_cast<float>(to.height()) / from.height()};
88}
89
Lloyd Piquec29e4c62019-03-07 21:48:19 -080090} // namespace
91
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070092std::shared_ptr<Output> createOutput(
93 const compositionengine::CompositionEngine& compositionEngine) {
94 return createOutputTemplated<Output>(compositionEngine);
95}
Lloyd Pique32cbe282018-10-19 13:09:22 -070096
97Output::~Output() = default;
98
Lloyd Pique32cbe282018-10-19 13:09:22 -070099bool Output::isValid() const {
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700100 return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface &&
101 mRenderSurface->isValid();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102}
103
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700104std::optional<DisplayId> Output::getDisplayId() const {
105 return {};
106}
107
Lloyd Pique32cbe282018-10-19 13:09:22 -0700108const std::string& Output::getName() const {
109 return mName;
110}
111
112void Output::setName(const std::string& name) {
113 mName = name;
114}
115
116void Output::setCompositionEnabled(bool enabled) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700117 auto& outputState = editState();
118 if (outputState.isEnabled == enabled) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700119 return;
120 }
121
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700122 outputState.isEnabled = enabled;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123 dirtyEntireOutput();
124}
125
Alec Mouri023c1882021-05-08 16:36:33 -0700126void Output::setLayerCachingEnabled(bool enabled) {
127 if (enabled == (mPlanner != nullptr)) {
128 return;
129 }
130
131 if (enabled) {
132 mPlanner = std::make_unique<planner::Planner>();
133 if (mRenderSurface) {
134 mPlanner->setDisplaySize(mRenderSurface->getSize());
135 }
136 } else {
137 mPlanner.reset();
138 }
139}
140
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200141void Output::setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
142 const Rect& orientedDisplaySpaceRect) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700143 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200144
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200145 outputState.displaySpace.orientation = orientation;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200146 LOG_FATAL_IF(outputState.displaySpace.bounds == Rect::INVALID_RECT,
147 "The display bounds are unknown.");
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200148
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200149 // Compute orientedDisplaySpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200150 ui::Size orientedSize = outputState.displaySpace.bounds.getSize();
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200151 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200152 std::swap(orientedSize.width, orientedSize.height);
153 }
154 outputState.orientedDisplaySpace.bounds = Rect(orientedSize);
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200155 outputState.orientedDisplaySpace.content = orientedDisplaySpaceRect;
156
157 // Compute displaySpace.content
158 const uint32_t transformOrientationFlags = ui::Transform::toRotationFlags(orientation);
159 ui::Transform rotation;
160 if (transformOrientationFlags != ui::Transform::ROT_INVALID) {
161 const auto displaySize = outputState.displaySpace.bounds;
162 rotation.set(transformOrientationFlags, displaySize.width(), displaySize.height());
163 }
164 outputState.displaySpace.content = rotation.transform(orientedDisplaySpaceRect);
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200165
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200166 // Compute framebufferSpace
167 outputState.framebufferSpace.orientation = orientation;
168 LOG_FATAL_IF(outputState.framebufferSpace.bounds == Rect::INVALID_RECT,
169 "The framebuffer bounds are unknown.");
170 const auto scale =
Marin Shalamanov209ae612020-10-01 00:17:39 +0200171 getScale(outputState.displaySpace.bounds, outputState.framebufferSpace.bounds);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200172 outputState.framebufferSpace.content = outputState.displaySpace.content.scale(scale.x, scale.y);
173
174 // Compute layerStackSpace
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200175 outputState.layerStackSpace.content = layerStackSpaceRect;
176 outputState.layerStackSpace.bounds = layerStackSpaceRect;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200177
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200178 outputState.transform = outputState.layerStackSpace.getTransform(outputState.displaySpace);
179 outputState.needsFiltering = outputState.transform.needsBilinearFiltering();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700180 dirtyEntireOutput();
181}
182
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200183void Output::setDisplaySize(const ui::Size& size) {
Lloyd Pique31cb2942018-10-19 17:23:03 -0700184 mRenderSurface->setDisplaySize(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200185
186 auto& state = editState();
187
188 // Update framebuffer space
189 const Rect newBounds(size);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200190 state.framebufferSpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200191
192 // Update display space
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200193 state.displaySpace.bounds = newBounds;
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200194 state.transform = state.layerStackSpace.getTransform(state.displaySpace);
195
196 // Update oriented display space
197 const auto orientation = state.displaySpace.orientation;
198 ui::Size orientedSize = size;
199 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
200 std::swap(orientedSize.width, orientedSize.height);
201 }
202 const Rect newOrientedBounds(orientedSize);
Marin Shalamanovb15d2272020-09-17 21:41:52 +0200203 state.orientedDisplaySpace.bounds = newOrientedBounds;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700204
Dan Stoza6166c312021-01-15 16:34:05 -0800205 if (mPlanner) {
206 mPlanner->setDisplaySize(size);
207 }
208
Lloyd Pique32cbe282018-10-19 13:09:22 -0700209 dirtyEntireOutput();
210}
211
Garfield Tan54edd912020-10-21 16:31:41 -0700212ui::Transform::RotationFlags Output::getTransformHint() const {
213 return static_cast<ui::Transform::RotationFlags>(getState().transform.getOrientation());
214}
215
Lloyd Piqueef36b002019-01-23 17:52:04 -0800216void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700217 auto& outputState = editState();
218 outputState.layerStackId = layerStackId;
219 outputState.layerStackInternal = isInternal;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700220
221 dirtyEntireOutput();
222}
223
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800224void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700225 auto& colorTransformMatrix = editState().colorTransformMatrix;
226 if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) {
Lloyd Pique77f79a22019-04-29 15:55:40 -0700227 return;
228 }
229
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700230 colorTransformMatrix = *args.colorTransformMatrix;
Lloyd Piqueef958122019-02-05 18:00:12 -0800231
232 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700233}
234
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800235void Output::setColorProfile(const ColorProfile& colorProfile) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700236 ui::Dataspace targetDataspace =
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800237 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
238 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800239
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700240 auto& outputState = editState();
241 if (outputState.colorMode == colorProfile.mode &&
242 outputState.dataspace == colorProfile.dataspace &&
243 outputState.renderIntent == colorProfile.renderIntent &&
244 outputState.targetDataspace == targetDataspace) {
Lloyd Piqueef958122019-02-05 18:00:12 -0800245 return;
246 }
247
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700248 outputState.colorMode = colorProfile.mode;
249 outputState.dataspace = colorProfile.dataspace;
250 outputState.renderIntent = colorProfile.renderIntent;
251 outputState.targetDataspace = targetDataspace;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700252
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800253 mRenderSurface->setBufferDataspace(colorProfile.dataspace);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700254
Lloyd Pique32cbe282018-10-19 13:09:22 -0700255 ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)",
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800256 decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode,
257 decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent);
Lloyd Piqueef958122019-02-05 18:00:12 -0800258
259 dirtyEntireOutput();
Lloyd Pique32cbe282018-10-19 13:09:22 -0700260}
261
262void Output::dump(std::string& out) const {
263 using android::base::StringAppendF;
264
265 StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str());
266
267 out.append("\n ");
268
269 dumpBase(out);
270}
271
272void Output::dumpBase(std::string& out) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700273 dumpState(out);
Lloyd Pique31cb2942018-10-19 17:23:03 -0700274
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700275 if (mDisplayColorProfile) {
276 mDisplayColorProfile->dump(out);
277 } else {
278 out.append(" No display color profile!\n");
279 }
280
Lloyd Pique31cb2942018-10-19 17:23:03 -0700281 if (mRenderSurface) {
282 mRenderSurface->dump(out);
283 } else {
284 out.append(" No render surface!\n");
285 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800286
Lloyd Pique01c77c12019-04-17 12:48:32 -0700287 android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount());
288 for (const auto* outputLayer : getOutputLayersOrderedByZ()) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800289 if (!outputLayer) {
290 continue;
291 }
292 outputLayer->dump(out);
293 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700294}
295
Dan Stoza269dc4d2021-01-15 15:07:43 -0800296void Output::dumpPlannerInfo(const Vector<String16>& args, std::string& out) const {
297 if (!mPlanner) {
298 base::StringAppendF(&out, "Planner is disabled\n");
299 return;
300 }
301 base::StringAppendF(&out, "Planner info for display [%s]\n", mName.c_str());
302 mPlanner->dump(args, out);
303}
304
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700305compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const {
306 return mDisplayColorProfile.get();
307}
308
309void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
310 mDisplayColorProfile = std::move(mode);
311}
312
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800313const Output::ReleasedLayers& Output::getReleasedLayersForTest() const {
314 return mReleasedLayers;
315}
316
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700317void Output::setDisplayColorProfileForTest(
318 std::unique_ptr<compositionengine::DisplayColorProfile> mode) {
319 mDisplayColorProfile = std::move(mode);
320}
321
Lloyd Pique31cb2942018-10-19 17:23:03 -0700322compositionengine::RenderSurface* Output::getRenderSurface() const {
323 return mRenderSurface.get();
324}
325
326void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
327 mRenderSurface = std::move(surface);
Dan Stoza6166c312021-01-15 16:34:05 -0800328 const auto size = mRenderSurface->getSize();
329 editState().framebufferSpace.bounds = Rect(size);
330 if (mPlanner) {
331 mPlanner->setDisplaySize(size);
332 }
Lloyd Pique31cb2942018-10-19 17:23:03 -0700333 dirtyEntireOutput();
334}
335
Vishnu Nair9b079a22020-01-21 14:36:08 -0800336void Output::cacheClientCompositionRequests(uint32_t cacheSize) {
337 if (cacheSize == 0) {
338 mClientCompositionRequestCache.reset();
339 } else {
340 mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize);
341 }
342};
343
Lloyd Pique31cb2942018-10-19 17:23:03 -0700344void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) {
345 mRenderSurface = std::move(surface);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700346}
347
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000348Region Output::getDirtyRegion(bool repaintEverything) const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700349 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200350 Region dirty(outputState.layerStackSpace.content);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000351 if (!repaintEverything) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700352 dirty.andSelf(outputState.dirtyRegion);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700353 }
354 return dirty;
355}
356
Lloyd Piquec6687342019-03-07 21:34:57 -0800357bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const {
Lloyd Piqueef36b002019-01-23 17:52:04 -0800358 // The layerStackId's must match, and also the layer must not be internal
359 // only when not on an internal output.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700360 const auto& outputState = getState();
361 return layerStackId && (*layerStackId == outputState.layerStackId) &&
362 (!internalOnly || outputState.layerStackInternal);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700363}
364
Lloyd Piquede196652020-01-22 17:29:58 -0800365bool Output::belongsInOutput(const sp<compositionengine::LayerFE>& layerFE) const {
366 const auto* layerFEState = layerFE->getCompositionState();
367 return layerFEState && belongsInOutput(layerFEState->layerStackId, layerFEState->internalOnly);
Lloyd Pique66c20c42019-03-07 21:44:02 -0800368}
369
Lloyd Piquedf336d92019-03-07 21:38:42 -0800370std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800371 const sp<LayerFE>& layerFE) const {
372 return impl::createOutputLayer(*this, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -0800373}
374
Lloyd Piquede196652020-01-22 17:29:58 -0800375compositionengine::OutputLayer* Output::getOutputLayerForLayer(const sp<LayerFE>& layerFE) const {
376 auto index = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700377 return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800378}
379
Lloyd Pique01c77c12019-04-17 12:48:32 -0700380std::optional<size_t> Output::findCurrentOutputLayerForLayer(
Lloyd Piquede196652020-01-22 17:29:58 -0800381 const sp<compositionengine::LayerFE>& layer) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700382 for (size_t i = 0; i < getOutputLayerCount(); i++) {
383 auto outputLayer = getOutputLayerOrderedByZByIndex(i);
Lloyd Piquede196652020-01-22 17:29:58 -0800384 if (outputLayer && &outputLayer->getLayerFE() == layer.get()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700385 return i;
386 }
387 }
388 return std::nullopt;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800389}
390
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800391void Output::setReleasedLayers(Output::ReleasedLayers&& layers) {
392 mReleasedLayers = std::move(layers);
393}
394
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800395void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs,
396 LayerFESet& geomSnapshots) {
397 ATRACE_CALL();
398 ALOGV(__FUNCTION__);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800399
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800400 rebuildLayerStacks(refreshArgs, geomSnapshots);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800401}
402
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800403void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800404 ATRACE_CALL();
405 ALOGV(__FUNCTION__);
406
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800407 updateColorProfile(refreshArgs);
Dan Stoza269dc4d2021-01-15 15:07:43 -0800408 updateCompositionState(refreshArgs);
409 planComposition();
410 writeCompositionState(refreshArgs);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800411 setColorTransform(refreshArgs);
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800412 beginFrame();
413 prepareFrame();
414 devOptRepaintFlash(refreshArgs);
415 finishFrame(refreshArgs);
416 postFramebuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800417 renderCachedSets();
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800418}
419
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800420void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs,
421 LayerFESet& layerFESet) {
422 ATRACE_CALL();
423 ALOGV(__FUNCTION__);
424
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700425 auto& outputState = editState();
426
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800427 // Do nothing if this output is not enabled or there is no need to perform this update
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700428 if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800429 return;
430 }
431
432 // Process the layers to determine visibility and coverage
433 compositionengine::Output::CoverageState coverage{layerFESet};
434 collectVisibleLayers(refreshArgs, coverage);
435
436 // Compute the resulting coverage for this output, and store it for later
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 const ui::Transform& tr = outputState.transform;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200438 Region undefinedRegion{outputState.displaySpace.bounds};
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800439 undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers));
440
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 outputState.undefinedRegion = undefinedRegion;
442 outputState.dirtyRegion.orSelf(coverage.dirtyRegion);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800443}
444
445void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs,
446 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800447 // Evaluate the layers from front to back to determine what is visible. This
448 // also incrementally calculates the coverage information for each layer as
449 // well as the entire output.
Lloyd Piquede196652020-01-22 17:29:58 -0800450 for (auto layer : reversed(refreshArgs.layers)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700451 // Incrementally process the coverage for each layer
452 ensureOutputLayerIfVisible(layer, coverage);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800453
454 // TODO(b/121291683): Stop early if the output is completely covered and
455 // no more layers could even be visible underneath the ones on top.
456 }
457
Lloyd Pique01c77c12019-04-17 12:48:32 -0700458 setReleasedLayers(refreshArgs);
459
460 finalizePendingOutputLayers();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800461}
462
Lloyd Piquede196652020-01-22 17:29:58 -0800463void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
Lloyd Pique01c77c12019-04-17 12:48:32 -0700464 compositionengine::Output::CoverageState& coverage) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800465 // Ensure we have a snapshot of the basic geometry layer state. Limit the
466 // snapshots to once per frame for each candidate layer, as layers may
467 // appear on multiple outputs.
468 if (!coverage.latchedLayers.count(layerFE)) {
469 coverage.latchedLayers.insert(layerFE);
Lloyd Piquede196652020-01-22 17:29:58 -0800470 layerFE->prepareCompositionState(compositionengine::LayerFE::StateSubset::BasicGeometry);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800471 }
472
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800473 // Only consider the layers on the given layer stack
Lloyd Piquede196652020-01-22 17:29:58 -0800474 if (!belongsInOutput(layerFE)) {
475 return;
476 }
477
478 // Obtain a read-only pointer to the front-end layer state
479 const auto* layerFEState = layerFE->getCompositionState();
480 if (CC_UNLIKELY(!layerFEState)) {
481 return;
482 }
483
484 // handle hidden surfaces by setting the visible region to empty
485 if (CC_UNLIKELY(!layerFEState->isVisible)) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700486 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800487 }
488
489 /*
490 * opaqueRegion: area of a surface that is fully opaque.
491 */
492 Region opaqueRegion;
493
494 /*
495 * visibleRegion: area of a surface that is visible on screen and not fully
496 * transparent. This is essentially the layer's footprint minus the opaque
497 * regions above it. Areas covered by a translucent surface are considered
498 * visible.
499 */
500 Region visibleRegion;
501
502 /*
503 * coveredRegion: area of a surface that is covered by all visible regions
504 * above it (which includes the translucent areas).
505 */
506 Region coveredRegion;
507
508 /*
509 * transparentRegion: area of a surface that is hinted to be completely
510 * transparent. This is only used to tell when the layer has no visible non-
511 * transparent regions and can be removed from the layer list. It does not
512 * affect the visibleRegion of this layer or any layers beneath it. The hint
513 * may not be correct if apps don't respect the SurfaceView restrictions
514 * (which, sadly, some don't).
515 */
516 Region transparentRegion;
517
Vishnu Naira483b4a2019-12-12 15:07:52 -0800518 /*
519 * shadowRegion: Region cast by the layer's shadow.
520 */
521 Region shadowRegion;
522
Lloyd Piquede196652020-01-22 17:29:58 -0800523 const ui::Transform& tr = layerFEState->geomLayerTransform;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800524
525 // Get the visible region
526 // TODO(b/121291683): Is it worth creating helper methods on LayerFEState
527 // for computations like this?
Lloyd Piquede196652020-01-22 17:29:58 -0800528 const Rect visibleRect(tr.transform(layerFEState->geomLayerBounds));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800529 visibleRegion.set(visibleRect);
530
Lloyd Piquede196652020-01-22 17:29:58 -0800531 if (layerFEState->shadowRadius > 0.0f) {
Vishnu Naira483b4a2019-12-12 15:07:52 -0800532 // if the layer casts a shadow, offset the layers visible region and
533 // calculate the shadow region.
Lloyd Piquede196652020-01-22 17:29:58 -0800534 const auto inset = static_cast<int32_t>(ceilf(layerFEState->shadowRadius) * -1.0f);
Vishnu Naira483b4a2019-12-12 15:07:52 -0800535 Rect visibleRectWithShadows(visibleRect);
536 visibleRectWithShadows.inset(inset, inset, inset, inset);
537 visibleRegion.set(visibleRectWithShadows);
538 shadowRegion = visibleRegion.subtract(visibleRect);
539 }
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800540
541 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700542 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800543 }
544
545 // Remove the transparent area from the visible region
Lloyd Piquede196652020-01-22 17:29:58 -0800546 if (!layerFEState->isOpaque) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800547 if (tr.preserveRects()) {
548 // transform the transparent region
Lloyd Piquede196652020-01-22 17:29:58 -0800549 transparentRegion = tr.transform(layerFEState->transparentRegionHint);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800550 } else {
551 // transformation too complex, can't do the
552 // transparent region optimization.
553 transparentRegion.clear();
554 }
555 }
556
557 // compute the opaque region
Lloyd Pique0a456232020-01-16 17:51:13 -0800558 const auto layerOrientation = tr.getOrientation();
Lloyd Piquede196652020-01-22 17:29:58 -0800559 if (layerFEState->isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800560 // If we one of the simple category of transforms (0/90/180/270 rotation
561 // + any flip), then the opaque region is the layer's footprint.
562 // Otherwise we don't try and compute the opaque region since there may
563 // be errors at the edges, and we treat the entire layer as
564 // translucent.
Vishnu Naira483b4a2019-12-12 15:07:52 -0800565 opaqueRegion.set(visibleRect);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800566 }
567
568 // Clip the covered region to the visible region
569 coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion);
570
571 // Update accumAboveCoveredLayers for next (lower) layer
572 coverage.aboveCoveredLayers.orSelf(visibleRegion);
573
574 // subtract the opaque region covered by the layers above us
575 visibleRegion.subtractSelf(coverage.aboveOpaqueLayers);
576
577 if (visibleRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700578 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800579 }
580
581 // Get coverage information for the layer as previously displayed,
582 // also taking over ownership from mOutputLayersorderedByZ.
Lloyd Piquede196652020-01-22 17:29:58 -0800583 auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layerFE);
Lloyd Pique01c77c12019-04-17 12:48:32 -0700584 auto prevOutputLayer =
585 prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800586
587 // Get coverage information for the layer as previously displayed
588 // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h
589 const Region kEmptyRegion;
590 const Region& oldVisibleRegion =
591 prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion;
592 const Region& oldCoveredRegion =
593 prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion;
594
595 // compute this layer's dirty region
596 Region dirty;
Lloyd Piquede196652020-01-22 17:29:58 -0800597 if (layerFEState->contentDirty) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800598 // we need to invalidate the whole region
599 dirty = visibleRegion;
600 // as well, as the old visible region
601 dirty.orSelf(oldVisibleRegion);
602 } else {
603 /* compute the exposed region:
604 * the exposed region consists of two components:
605 * 1) what's VISIBLE now and was COVERED before
606 * 2) what's EXPOSED now less what was EXPOSED before
607 *
608 * note that (1) is conservative, we start with the whole visible region
609 * but only keep what used to be covered by something -- which mean it
610 * may have been exposed.
611 *
612 * (2) handles areas that were not covered by anything but got exposed
613 * because of a resize.
614 *
615 */
616 const Region newExposed = visibleRegion - coveredRegion;
617 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
618 dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed);
619 }
620 dirty.subtractSelf(coverage.aboveOpaqueLayers);
621
622 // accumulate to the screen dirty region
623 coverage.dirtyRegion.orSelf(dirty);
624
625 // Update accumAboveOpaqueLayers for next (lower) layer
626 coverage.aboveOpaqueLayers.orSelf(opaqueRegion);
627
628 // Compute the visible non-transparent region
629 Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion);
630
Vishnu Naira483b4a2019-12-12 15:07:52 -0800631 // Perform the final check to see if this layer is visible on this output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800632 // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below)
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700633 const auto& outputState = getState();
634 Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion));
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200635 drawRegion.andSelf(outputState.displaySpace.bounds);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800636 if (drawRegion.isEmpty()) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700637 return;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800638 }
639
Vishnu Naira483b4a2019-12-12 15:07:52 -0800640 Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion);
641
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800642 // The layer is visible. Either reuse the existing outputLayer if we have
643 // one, or create a new one if we do not.
Lloyd Piquede196652020-01-22 17:29:58 -0800644 auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800645
646 // Store the layer coverage information into the layer state as some of it
647 // is useful later.
648 auto& outputLayerState = result->editState();
649 outputLayerState.visibleRegion = visibleRegion;
650 outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion;
651 outputLayerState.coveredRegion = coveredRegion;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200652 outputLayerState.outputSpaceVisibleRegion = outputState.transform.transform(
653 visibleNonShadowRegion.intersect(outputState.layerStackSpace.content));
Vishnu Naira483b4a2019-12-12 15:07:52 -0800654 outputLayerState.shadowRegion = shadowRegion;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800655}
656
657void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) {
658 // The base class does nothing with this call.
659}
660
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800661void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700662 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800663 layer->getLayerFE().prepareCompositionState(
664 args.updatingGeometryThisFrame ? LayerFE::StateSubset::GeometryAndContent
665 : LayerFE::StateSubset::Content);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800666 }
667}
668
Dan Stoza269dc4d2021-01-15 15:07:43 -0800669void Output::updateCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800670 ATRACE_CALL();
671 ALOGV(__FUNCTION__);
672
Alec Mourif9a2a2c2019-11-12 12:46:02 -0800673 if (!getState().isEnabled) {
674 return;
675 }
676
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800677 mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
678 bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
679
Lloyd Pique01c77c12019-04-17 12:48:32 -0700680 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique7a234912019-10-03 11:54:27 -0700681 layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800682 refreshArgs.devOptForceClientComposition ||
Snild Dolkow9e217d62020-04-22 15:53:42 +0200683 forceClientComposition,
684 refreshArgs.internalDisplayRotationFlags);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800685
686 if (mLayerRequestingBackgroundBlur == layer) {
687 forceClientComposition = false;
688 }
Dan Stoza269dc4d2021-01-15 15:07:43 -0800689 }
690}
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800691
Dan Stoza269dc4d2021-01-15 15:07:43 -0800692void Output::planComposition() {
693 if (!mPlanner || !getState().isEnabled) {
694 return;
695 }
696
697 ATRACE_CALL();
698 ALOGV(__FUNCTION__);
699
700 mPlanner->plan(getOutputLayersOrderedByZ());
701}
702
703void Output::writeCompositionState(const compositionengine::CompositionRefreshArgs& refreshArgs) {
704 ATRACE_CALL();
705 ALOGV(__FUNCTION__);
706
707 if (!getState().isEnabled) {
708 return;
709 }
710
Ady Abraham3645e642021-04-20 18:39:00 -0700711 editState().earliestPresentTime = refreshArgs.earliestPresentTime;
712
Leon Scroggins III2e74a4c2021-04-09 13:41:14 -0400713 compositionengine::OutputLayer* peekThroughLayer = nullptr;
Dan Stoza6166c312021-01-15 16:34:05 -0800714 sp<GraphicBuffer> previousOverride = nullptr;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400715 bool includeGeometry = refreshArgs.updatingGeometryThisFrame;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400716 uint32_t z = 0;
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400717 bool overrideZ = false;
Dan Stoza269dc4d2021-01-15 15:07:43 -0800718 for (auto* layer : getOutputLayersOrderedByZ()) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400719 if (layer == peekThroughLayer) {
720 // No longer needed, although it should not show up again, so
721 // resetting it is not truly needed either.
722 peekThroughLayer = nullptr;
723
724 // peekThroughLayer was already drawn ahead of its z order.
725 continue;
726 }
Dan Stoza6166c312021-01-15 16:34:05 -0800727 bool skipLayer = false;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400728 const auto& overrideInfo = layer->getState().overrideInfo;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400729 if (overrideInfo.buffer != nullptr) {
730 if (previousOverride && overrideInfo.buffer->getBuffer() == previousOverride) {
Dan Stoza6166c312021-01-15 16:34:05 -0800731 ALOGV("Skipping redundant buffer");
732 skipLayer = true;
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400733 } else {
734 // First layer with the override buffer.
735 if (overrideInfo.peekThroughLayer) {
736 peekThroughLayer = overrideInfo.peekThroughLayer;
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400737
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400738 // Draw peekThroughLayer first.
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400739 overrideZ = true;
740 includeGeometry = true;
741 constexpr bool isPeekingThrough = true;
742 peekThroughLayer->writeStateToHWC(includeGeometry, false, z++, overrideZ,
743 isPeekingThrough);
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400744 }
745
746 previousOverride = overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800747 }
Dan Stoza6166c312021-01-15 16:34:05 -0800748 }
749
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400750 constexpr bool isPeekingThrough = false;
751 layer->writeStateToHWC(includeGeometry, skipLayer, z++, overrideZ, isPeekingThrough);
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800752 }
753}
754
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800755compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const {
756 compositionengine::OutputLayer* layerRequestingBgComposition = nullptr;
757 for (auto* layer : getOutputLayersOrderedByZ()) {
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100758 auto* compState = layer->getLayerFE().getCompositionState();
759
760 // If any layer has a sideband stream, we will disable blurs. In that case, we don't
761 // want to force client composition because of the blur.
762 if (compState->sidebandStream != nullptr) {
763 return nullptr;
764 }
765 if (compState->backgroundBlurRadius > 0 || compState->blurRegions.size() > 0) {
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800766 layerRequestingBgComposition = layer;
767 }
768 }
769 return layerRequestingBgComposition;
770}
771
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800772void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) {
773 setColorProfile(pickColorProfile(refreshArgs));
774}
775
776// Returns a data space that fits all visible layers. The returned data space
777// can only be one of
778// - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced)
779// - Dataspace::DISPLAY_P3
780// - Dataspace::DISPLAY_BT2020
781// The returned HDR data space is one of
782// - Dataspace::UNKNOWN
783// - Dataspace::BT2020_HLG
784// - Dataspace::BT2020_PQ
785ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace,
786 bool* outIsHdrClientComposition) const {
787 ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB;
788 *outHdrDataSpace = ui::Dataspace::UNKNOWN;
789
Lloyd Pique01c77c12019-04-17 12:48:32 -0700790 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800791 switch (layer->getLayerFE().getCompositionState()->dataspace) {
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800792 case ui::Dataspace::V0_SCRGB:
793 case ui::Dataspace::V0_SCRGB_LINEAR:
794 case ui::Dataspace::BT2020:
795 case ui::Dataspace::BT2020_ITU:
796 case ui::Dataspace::BT2020_LINEAR:
797 case ui::Dataspace::DISPLAY_BT2020:
798 bestDataSpace = ui::Dataspace::DISPLAY_BT2020;
799 break;
800 case ui::Dataspace::DISPLAY_P3:
801 bestDataSpace = ui::Dataspace::DISPLAY_P3;
802 break;
803 case ui::Dataspace::BT2020_PQ:
804 case ui::Dataspace::BT2020_ITU_PQ:
805 bestDataSpace = ui::Dataspace::DISPLAY_P3;
806 *outHdrDataSpace = ui::Dataspace::BT2020_PQ;
Lloyd Piquede196652020-01-22 17:29:58 -0800807 *outIsHdrClientComposition =
808 layer->getLayerFE().getCompositionState()->forceClientComposition;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800809 break;
810 case ui::Dataspace::BT2020_HLG:
811 case ui::Dataspace::BT2020_ITU_HLG:
812 bestDataSpace = ui::Dataspace::DISPLAY_P3;
813 // When there's mixed PQ content and HLG content, we set the HDR
814 // data space to be BT2020_PQ and convert HLG to PQ.
815 if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) {
816 *outHdrDataSpace = ui::Dataspace::BT2020_HLG;
817 }
818 break;
819 default:
820 break;
821 }
822 }
823
824 return bestDataSpace;
825}
826
827compositionengine::Output::ColorProfile Output::pickColorProfile(
828 const compositionengine::CompositionRefreshArgs& refreshArgs) const {
829 if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) {
830 return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
831 ui::RenderIntent::COLORIMETRIC,
832 refreshArgs.colorSpaceAgnosticDataspace};
833 }
834
835 ui::Dataspace hdrDataSpace;
836 bool isHdrClientComposition = false;
837 ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition);
838
839 switch (refreshArgs.forceOutputColorMode) {
840 case ui::ColorMode::SRGB:
841 bestDataSpace = ui::Dataspace::V0_SRGB;
842 break;
843 case ui::ColorMode::DISPLAY_P3:
844 bestDataSpace = ui::Dataspace::DISPLAY_P3;
845 break;
846 default:
847 break;
848 }
849
850 // respect hdrDataSpace only when there is no legacy HDR support
851 const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN &&
852 !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition;
853 if (isHdr) {
854 bestDataSpace = hdrDataSpace;
855 }
856
857 ui::RenderIntent intent;
858 switch (refreshArgs.outputColorSetting) {
859 case OutputColorSetting::kManaged:
860 case OutputColorSetting::kUnmanaged:
861 intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC
862 : ui::RenderIntent::COLORIMETRIC;
863 break;
864 case OutputColorSetting::kEnhanced:
865 intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE;
866 break;
867 default: // vendor display color setting
868 intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting);
869 break;
870 }
871
872 ui::ColorMode outMode;
873 ui::Dataspace outDataSpace;
874 ui::RenderIntent outRenderIntent;
875 mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode,
876 &outRenderIntent);
877
878 return ColorProfile{outMode, outDataSpace, outRenderIntent,
879 refreshArgs.colorSpaceAgnosticDataspace};
880}
881
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800882void Output::beginFrame() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700883 auto& outputState = editState();
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800884 const bool dirty = !getDirtyRegion(false).isEmpty();
Lloyd Pique01c77c12019-04-17 12:48:32 -0700885 const bool empty = getOutputLayerCount() == 0;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700886 const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800887
888 // If nothing has changed (!dirty), don't recompose.
889 // If something changed, but we don't currently have any visible layers,
890 // and didn't when we last did a composition, then skip it this time.
891 // The second rule does two things:
892 // - When all layers are removed from a display, we'll emit one black
893 // frame, then nothing more until we get new layers.
894 // - When a display is created with a private layer stack, we won't
895 // emit any black frames until a layer is added to the layer stack.
896 const bool mustRecompose = dirty && !(empty && wasEmpty);
897
898 const char flagPrefix[] = {'-', '+'};
899 static_cast<void>(flagPrefix);
900 ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__,
901 mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty],
902 flagPrefix[empty], flagPrefix[wasEmpty]);
903
904 mRenderSurface->beginFrame(mustRecompose);
905
906 if (mustRecompose) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700907 outputState.lastCompositionHadVisibleLayers = !empty;
Lloyd Piqued0a92a02019-02-19 17:47:26 -0800908 }
909}
910
Lloyd Pique66d68602019-02-13 14:23:31 -0800911void Output::prepareFrame() {
912 ATRACE_CALL();
913 ALOGV(__FUNCTION__);
914
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700915 const auto& outputState = getState();
916 if (!outputState.isEnabled) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800917 return;
918 }
919
920 chooseCompositionStrategy();
921
Dan Stoza47437bb2021-01-15 16:21:07 -0800922 if (mPlanner) {
923 mPlanner->reportFinalPlan(getOutputLayersOrderedByZ());
924 }
925
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700926 mRenderSurface->prepareFrame(outputState.usesClientComposition,
927 outputState.usesDeviceComposition);
Lloyd Pique66d68602019-02-13 14:23:31 -0800928}
929
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800930void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) {
931 if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) {
932 return;
933 }
934
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700935 if (getState().isEnabled) {
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800936 // transform the dirty region into this screen's coordinate space
937 const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything);
938 if (!dirtyRegion.isEmpty()) {
939 base::unique_fd readyFence;
940 // redraw the whole screen
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800941 static_cast<void>(composeSurfaces(dirtyRegion, refreshArgs));
Lloyd Piquef8cf14d2019-02-28 16:03:12 -0800942
943 mRenderSurface->queueBuffer(std::move(readyFence));
944 }
945 }
946
947 postFramebuffer();
948
949 std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay);
950
951 prepareFrame();
952}
953
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800954void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800955 ATRACE_CALL();
956 ALOGV(__FUNCTION__);
957
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700958 if (!getState().isEnabled) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800959 return;
960 }
961
962 // Repaint the framebuffer (if needed), getting the optional fence for when
963 // the composition completes.
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800964 auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
Lloyd Piqued3d69882019-02-28 16:03:46 -0800965 if (!optReadyFence) {
966 return;
967 }
968
969 // swap buffers (presentation)
970 mRenderSurface->queueBuffer(std::move(*optReadyFence));
971}
972
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800973std::optional<base::unique_fd> Output::composeSurfaces(
974 const Region& debugRegion, const compositionengine::CompositionRefreshArgs& refreshArgs) {
Lloyd Pique688abd42019-02-15 15:42:24 -0800975 ATRACE_CALL();
976 ALOGV(__FUNCTION__);
977
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700978 const auto& outputState = getState();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800979 OutputCompositionState& outputCompositionState = editState();
Lloyd Pique688abd42019-02-15 15:42:24 -0800980 const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700981 outputState.usesClientComposition};
Lloyd Piquee9eff972020-05-05 12:36:44 -0700982
983 auto& renderEngine = getCompositionEngine().getRenderEngine();
984 const bool supportsProtectedContent = renderEngine.supportsProtectedContent();
985
986 // If we the display is secure, protected content support is enabled, and at
987 // least one layer has protected content, we need to use a secure back
988 // buffer.
989 if (outputState.isSecure && supportsProtectedContent) {
990 auto layers = getOutputLayersOrderedByZ();
991 bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) {
992 return layer->getLayerFE().getCompositionState()->hasProtectedContent;
993 });
994 if (needsProtected != renderEngine.isProtected()) {
995 renderEngine.useProtectedContext(needsProtected);
996 }
997 if (needsProtected != mRenderSurface->isProtected() &&
998 needsProtected == renderEngine.isProtected()) {
999 mRenderSurface->setProtected(needsProtected);
1000 }
Peiyong Lin09f910f2020-09-25 10:54:13 -07001001 } else if (!outputState.isSecure && renderEngine.isProtected()) {
1002 renderEngine.useProtectedContext(false);
Lloyd Piquee9eff972020-05-05 12:36:44 -07001003 }
1004
1005 base::unique_fd fd;
Alec Mouria90a5702021-04-16 16:36:21 +00001006
1007 std::shared_ptr<renderengine::ExternalTexture> tex;
Lloyd Piquee9eff972020-05-05 12:36:44 -07001008
1009 // If we aren't doing client composition on this output, but do have a
1010 // flipClientTarget request for this frame on this output, we still need to
1011 // dequeue a buffer.
1012 if (hasClientComposition || outputState.flipClientTarget) {
Alec Mouria90a5702021-04-16 16:36:21 +00001013 tex = mRenderSurface->dequeueBuffer(&fd);
1014 if (tex == nullptr) {
Lloyd Piquee9eff972020-05-05 12:36:44 -07001015 ALOGW("Dequeuing buffer for display [%s] failed, bailing out of "
1016 "client composition for this frame",
1017 mName.c_str());
1018 return {};
1019 }
1020 }
1021
Lloyd Piqued3d69882019-02-28 16:03:46 -08001022 base::unique_fd readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001023 if (!hasClientComposition) {
Lloyd Piquea76ce462020-01-14 13:06:37 -08001024 setExpensiveRenderingExpected(false);
Lloyd Piqued3d69882019-02-28 16:03:46 -08001025 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001026 }
1027
1028 ALOGV("hasClientComposition");
1029
Lloyd Pique688abd42019-02-15 15:42:24 -08001030 renderengine::DisplaySettings clientCompositionDisplay;
Marin Shalamanovb15d2272020-09-17 21:41:52 +02001031 clientCompositionDisplay.physicalDisplay = outputState.framebufferSpace.content;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001032 clientCompositionDisplay.clip = outputState.layerStackSpace.content;
Marin Shalamanov68933fb2020-09-10 17:58:12 +02001033 clientCompositionDisplay.orientation =
1034 ui::Transform::toRotationFlags(outputState.displaySpace.orientation);
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001035 clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut()
1036 ? outputState.dataspace
1037 : ui::Dataspace::UNKNOWN;
Lloyd Pique688abd42019-02-15 15:42:24 -08001038 clientCompositionDisplay.maxLuminance =
1039 mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance();
1040
1041 // Compute the global color transform matrix.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001042 if (!outputState.usesDeviceComposition && !getSkipColorTransform()) {
1043 clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix;
Lloyd Pique688abd42019-02-15 15:42:24 -08001044 }
1045
1046 // Note: Updated by generateClientCompositionRequests
1047 clientCompositionDisplay.clearRegion = Region::INVALID_REGION;
1048
1049 // Generate the client composition requests for the layers on this output.
Vishnu Nair9b079a22020-01-21 14:36:08 -08001050 std::vector<LayerFE::LayerSettings> clientCompositionLayers =
Lloyd Pique688abd42019-02-15 15:42:24 -08001051 generateClientCompositionRequests(supportsProtectedContent,
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001052 clientCompositionDisplay.clearRegion,
1053 clientCompositionDisplay.outputDataspace);
Lloyd Pique688abd42019-02-15 15:42:24 -08001054 appendRegionFlashRequests(debugRegion, clientCompositionLayers);
1055
Vishnu Nair9b079a22020-01-21 14:36:08 -08001056 // Check if the client composition requests were rendered into the provided graphic buffer. If
1057 // so, we can reuse the buffer and avoid client composition.
1058 if (mClientCompositionRequestCache) {
Alec Mouria90a5702021-04-16 16:36:21 +00001059 if (mClientCompositionRequestCache->exists(tex->getBuffer()->getId(),
1060 clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001061 clientCompositionLayers)) {
1062 outputCompositionState.reusedClientComposition = true;
1063 setExpensiveRenderingExpected(false);
1064 return readyFence;
1065 }
Alec Mouria90a5702021-04-16 16:36:21 +00001066 mClientCompositionRequestCache->add(tex->getBuffer()->getId(), clientCompositionDisplay,
Vishnu Nair9b079a22020-01-21 14:36:08 -08001067 clientCompositionLayers);
1068 }
1069
Lloyd Pique688abd42019-02-15 15:42:24 -08001070 // We boost GPU frequency here because there will be color spaces conversion
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001071 // or complex GPU shaders and it's expensive. We boost the GPU frequency so that
1072 // GPU composition can finish in time. We must reset GPU frequency afterwards,
1073 // because high frequency consumes extra battery.
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001074 const bool expensiveBlurs =
1075 refreshArgs.blursAreExpensive && mLayerRequestingBackgroundBlur != nullptr;
Lloyd Pique688abd42019-02-15 15:42:24 -08001076 const bool expensiveRenderingExpected =
Lucas Dupin2dd6f392020-02-18 17:43:36 -08001077 clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || expensiveBlurs;
Lloyd Pique688abd42019-02-15 15:42:24 -08001078 if (expensiveRenderingExpected) {
1079 setExpensiveRenderingExpected(true);
1080 }
1081
Vishnu Nair9b079a22020-01-21 14:36:08 -08001082 std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers;
1083 clientCompositionLayerPointers.reserve(clientCompositionLayers.size());
1084 std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(),
1085 std::back_inserter(clientCompositionLayerPointers),
1086 [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* {
1087 return &settings;
1088 });
1089
Alec Mourie4034bb2019-11-19 12:45:54 -08001090 const nsecs_t renderEngineStart = systemTime();
Alec Mouri1684c702021-02-04 12:27:26 -08001091 // Only use the framebuffer cache when rendering to an internal display
1092 // TODO(b/173560331): This is only to help mitigate memory leaks from virtual displays because
1093 // right now we don't have a concrete eviction policy for output buffers: GLESRenderEngine
1094 // bounds its framebuffer cache but Skia RenderEngine has no current policy. The best fix is
1095 // probably to encapsulate the output buffer into a structure that dispatches resource cleanup
1096 // over to RenderEngine, in which case this flag can be removed from the drawLayers interface.
1097 const bool useFramebufferCache = outputState.layerStackInternal;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001098 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +00001099 renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, tex,
Alec Mouri1684c702021-02-04 12:27:26 -08001100 useFramebufferCache, std::move(fd), &readyFence);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001101
1102 if (status != NO_ERROR && mClientCompositionRequestCache) {
1103 // If rendering was not successful, remove the request from the cache.
Alec Mouria90a5702021-04-16 16:36:21 +00001104 mClientCompositionRequestCache->remove(tex->getBuffer()->getId());
Vishnu Nair9b079a22020-01-21 14:36:08 -08001105 }
1106
Alec Mourie4034bb2019-11-19 12:45:54 -08001107 auto& timeStats = getCompositionEngine().getTimeStats();
1108 if (readyFence.get() < 0) {
1109 timeStats.recordRenderEngineDuration(renderEngineStart, systemTime());
1110 } else {
1111 timeStats.recordRenderEngineDuration(renderEngineStart,
1112 std::make_shared<FenceTime>(
1113 new Fence(dup(readyFence.get()))));
1114 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001115
Lloyd Piqued3d69882019-02-28 16:03:46 -08001116 return readyFence;
Lloyd Pique688abd42019-02-15 15:42:24 -08001117}
1118
Vishnu Nair9b079a22020-01-21 14:36:08 -08001119std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -08001120 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) {
Vishnu Nair9b079a22020-01-21 14:36:08 -08001121 std::vector<LayerFE::LayerSettings> clientCompositionLayers;
Lloyd Pique688abd42019-02-15 15:42:24 -08001122 ALOGV("Rendering client layers");
1123
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001124 const auto& outputState = getState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001125 const Region viewportRegion(outputState.layerStackSpace.content);
Lloyd Pique688abd42019-02-15 15:42:24 -08001126 bool firstLayer = true;
1127 // Used when a layer clears part of the buffer.
Peiyong Lind8460c82020-07-28 16:04:22 -07001128 Region stubRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -08001129
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001130 bool disableBlurs = false;
Huihong Luo91ac3b52021-04-08 11:07:41 -07001131 sp<GraphicBuffer> previousOverrideBuffer = nullptr;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001132
Lloyd Pique01c77c12019-04-17 12:48:32 -07001133 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001134 const auto& layerState = layer->getState();
Lloyd Piquede196652020-01-22 17:29:58 -08001135 const auto* layerFEState = layer->getLayerFE().getCompositionState();
Lloyd Pique688abd42019-02-15 15:42:24 -08001136 auto& layerFE = layer->getLayerFE();
1137
Lloyd Piquea2468662019-03-07 21:31:06 -08001138 const Region clip(viewportRegion.intersect(layerState.visibleRegion));
Lloyd Pique688abd42019-02-15 15:42:24 -08001139 ALOGV("Layer: %s", layerFE.getDebugName());
1140 if (clip.isEmpty()) {
1141 ALOGV(" Skipping for empty clip");
1142 firstLayer = false;
1143 continue;
1144 }
1145
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001146 disableBlurs |= layerFEState->sidebandStream != nullptr;
1147
Vishnu Naira483b4a2019-12-12 15:07:52 -08001148 const bool clientComposition = layer->requiresClientComposition();
Lloyd Pique688abd42019-02-15 15:42:24 -08001149
1150 // We clear the client target for non-client composed layers if
1151 // requested by the HWC. We skip this if the layer is not an opaque
1152 // rectangle, as by definition the layer must blend with whatever is
1153 // underneath. We also skip the first layer as the buffer target is
1154 // guaranteed to start out cleared.
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001155 const bool clearClientComposition =
Lloyd Piquede196652020-01-22 17:29:58 -08001156 layerState.clearClientTarget && layerFEState->isOpaque && !firstLayer;
Lloyd Pique688abd42019-02-15 15:42:24 -08001157
1158 ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition);
1159
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001160 // If the layer casts a shadow but the content casting the shadow is occluded, skip
1161 // composing the non-shadow content and only draw the shadows.
1162 const bool realContentIsVisible = clientComposition &&
1163 !layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty();
1164
Lloyd Pique688abd42019-02-15 15:42:24 -08001165 if (clientComposition || clearClientComposition) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001166 compositionengine::LayerFE::ClientCompositionTargetSettings
1167 targetSettings{.clip = clip,
1168 .needsFiltering =
1169 layer->needsFiltering() || outputState.needsFiltering,
1170 .isSecure = outputState.isSecure,
1171 .supportsProtectedContent = supportsProtectedContent,
1172 .clearRegion = clientComposition ? clearRegion : stubRegion,
1173 .viewport = outputState.layerStackSpace.content,
1174 .dataspace = outputDataspace,
1175 .realContentIsVisible = realContentIsVisible,
Galia Peycheva66eaf4a2020-11-09 13:17:57 +01001176 .clearContent = !clientComposition,
1177 .disableBlurs = disableBlurs};
Dan Stoza6166c312021-01-15 16:34:05 -08001178
1179 std::vector<LayerFE::LayerSettings> results;
1180 if (layer->getState().overrideInfo.buffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +00001181 if (layer->getState().overrideInfo.buffer->getBuffer() != previousOverrideBuffer) {
Huihong Luo91ac3b52021-04-08 11:07:41 -07001182 results = layer->getOverrideCompositionList();
Alec Mouria90a5702021-04-16 16:36:21 +00001183 previousOverrideBuffer = layer->getState().overrideInfo.buffer->getBuffer();
Huihong Luo91ac3b52021-04-08 11:07:41 -07001184 ALOGV("Replacing [%s] with override in RE", layer->getLayerFE().getDebugName());
1185 } else {
1186 ALOGV("Skipping redundant override buffer for [%s] in RE",
1187 layer->getLayerFE().getDebugName());
1188 }
Dan Stoza6166c312021-01-15 16:34:05 -08001189 } else {
1190 results = layerFE.prepareClientCompositionList(targetSettings);
1191 if (realContentIsVisible && !results.empty()) {
1192 layer->editState().clientCompositionTimestamp = systemTime();
1193 }
Lloyd Pique688abd42019-02-15 15:42:24 -08001194 }
Vishnu Nairb87d94f2020-02-13 09:17:36 -08001195
1196 clientCompositionLayers.insert(clientCompositionLayers.end(),
1197 std::make_move_iterator(results.begin()),
1198 std::make_move_iterator(results.end()));
1199 results.clear();
Lloyd Pique688abd42019-02-15 15:42:24 -08001200 }
1201
1202 firstLayer = false;
1203 }
1204
1205 return clientCompositionLayers;
1206}
1207
1208void Output::appendRegionFlashRequests(
Vishnu Nair9b079a22020-01-21 14:36:08 -08001209 const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) {
Lloyd Pique688abd42019-02-15 15:42:24 -08001210 if (flashRegion.isEmpty()) {
1211 return;
1212 }
1213
Vishnu Nair9b079a22020-01-21 14:36:08 -08001214 LayerFE::LayerSettings layerSettings;
Lloyd Pique688abd42019-02-15 15:42:24 -08001215 layerSettings.source.buffer.buffer = nullptr;
1216 layerSettings.source.solidColor = half3(1.0, 0.0, 1.0);
1217 layerSettings.alpha = half(1.0);
1218
1219 for (const auto& rect : flashRegion) {
1220 layerSettings.geometry.boundaries = rect.toFloatRect();
1221 clientCompositionLayers.push_back(layerSettings);
1222 }
1223}
1224
1225void Output::setExpensiveRenderingExpected(bool) {
1226 // The base class does nothing with this call.
1227}
1228
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001229void Output::postFramebuffer() {
1230 ATRACE_CALL();
1231 ALOGV(__FUNCTION__);
1232
1233 if (!getState().isEnabled) {
1234 return;
1235 }
1236
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001237 auto& outputState = editState();
1238 outputState.dirtyRegion.clear();
Lloyd Piqued3d69882019-02-28 16:03:46 -08001239 mRenderSurface->flip();
1240
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001241 auto frame = presentAndGetFrameFences();
1242
Lloyd Pique7d90ba52019-08-08 11:57:53 -07001243 mRenderSurface->onPresentDisplayCompleted();
1244
Lloyd Pique01c77c12019-04-17 12:48:32 -07001245 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001246 // The layer buffer from the previous frame (if any) is released
1247 // by HWC only when the release fence from this frame (if any) is
1248 // signaled. Always get the release fence from HWC first.
1249 sp<Fence> releaseFence = Fence::NO_FENCE;
1250
1251 if (auto hwcLayer = layer->getHwcLayer()) {
1252 if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
1253 releaseFence = f->second;
1254 }
1255 }
1256
1257 // If the layer was client composited in the previous frame, we
1258 // need to merge with the previous client target acquire fence.
1259 // Since we do not track that, always merge with the current
1260 // client target acquire fence when it is available, even though
1261 // this is suboptimal.
1262 // TODO(b/121291683): Track previous frame client target acquire fence.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001263 if (outputState.usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001264 releaseFence =
1265 Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
1266 }
1267
1268 layer->getLayerFE().onLayerDisplayed(releaseFence);
1269 }
1270
1271 // We've got a list of layers needing fences, that are disjoint with
Lloyd Pique01c77c12019-04-17 12:48:32 -07001272 // OutputLayersOrderedByZ. The best we can do is to
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001273 // supply them with the present fence.
1274 for (auto& weakLayer : mReleasedLayers) {
1275 if (auto layer = weakLayer.promote(); layer != nullptr) {
1276 layer->onLayerDisplayed(frame.presentFence);
1277 }
1278 }
1279
1280 // Clear out the released layers now that we're done with them.
1281 mReleasedLayers.clear();
1282}
1283
Dan Stoza6166c312021-01-15 16:34:05 -08001284void Output::renderCachedSets() {
1285 if (mPlanner) {
Huihong Luoa5825112021-03-24 12:28:29 -07001286 mPlanner->renderCachedSets(getCompositionEngine().getRenderEngine(), getState());
Dan Stoza6166c312021-01-15 16:34:05 -08001287 }
1288}
1289
Lloyd Pique32cbe282018-10-19 13:09:22 -07001290void Output::dirtyEntireOutput() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001291 auto& outputState = editState();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001292 outputState.dirtyRegion.set(outputState.displaySpace.bounds);
Lloyd Pique32cbe282018-10-19 13:09:22 -07001293}
1294
Lloyd Pique66d68602019-02-13 14:23:31 -08001295void Output::chooseCompositionStrategy() {
1296 // The base output implementation can only do client composition
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001297 auto& outputState = editState();
1298 outputState.usesClientComposition = true;
1299 outputState.usesDeviceComposition = false;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001300 outputState.reusedClientComposition = false;
Lloyd Pique66d68602019-02-13 14:23:31 -08001301}
1302
Lloyd Pique688abd42019-02-15 15:42:24 -08001303bool Output::getSkipColorTransform() const {
1304 return true;
1305}
1306
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001307compositionengine::Output::FrameFences Output::presentAndGetFrameFences() {
1308 compositionengine::Output::FrameFences result;
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001309 if (getState().usesClientComposition) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -08001310 result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence();
1311 }
1312 return result;
1313}
1314
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001315} // namespace impl
1316} // namespace android::compositionengine