blob: bdd4427d20071130405aef6ad07bfb33d06c8914 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001/*
2 * Copyright 2022 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
17// #define LOG_NDEBUG 0
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
20#define LOG_TAG "LayerSnapshotBuilder"
21
22#include "LayerSnapshotBuilder.h"
23#include <gui/TraceUtils.h>
Vishnu Nairfed7c122023-03-18 01:54:43 +000024#include <ui/FloatRect.h>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070025
Vishnu Nair8fc721b2022-12-22 20:06:32 +000026#include <numeric>
Vishnu Nairb76d99a2023-03-19 18:22:31 -070027#include <optional>
28
29#include <gui/TraceUtils.h>
Vishnu Nair8fc721b2022-12-22 20:06:32 +000030#include "DisplayHardware/HWC2.h"
31#include "DisplayHardware/Hal.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000032#include "LayerLog.h"
Vishnu Nairb76d99a2023-03-19 18:22:31 -070033#include "LayerSnapshotBuilder.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000034#include "TimeStats/TimeStats.h"
Vishnu Nair8fc721b2022-12-22 20:06:32 +000035#include "ftl/small_map.h"
36
37namespace android::surfaceflinger::frontend {
38
39using namespace ftl::flag_operators;
40
41namespace {
42FloatRect getMaxDisplayBounds(
43 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
44 const ui::Size maxSize = [&displays] {
45 if (displays.empty()) return ui::Size{5000, 5000};
46
47 return std::accumulate(displays.begin(), displays.end(), ui::kEmptySize,
48 [](ui::Size size, const auto& pair) -> ui::Size {
49 const auto& display = pair.second;
50 return {std::max(size.getWidth(), display.info.logicalWidth),
51 std::max(size.getHeight(), display.info.logicalHeight)};
52 });
53 }();
54
55 // Ignore display bounds for now since they will be computed later. Use a large Rect bound
56 // to ensure it's bigger than an actual display will be.
57 const float xMax = static_cast<float>(maxSize.getWidth()) * 10.f;
58 const float yMax = static_cast<float>(maxSize.getHeight()) * 10.f;
59
60 return {-xMax, -yMax, xMax, yMax};
61}
62
63// Applies the given transform to the region, while protecting against overflows caused by any
64// offsets. If applying the offset in the transform to any of the Rects in the region would result
65// in an overflow, they are not added to the output Region.
66Region transformTouchableRegionSafely(const ui::Transform& t, const Region& r,
67 const std::string& debugWindowName) {
68 // Round the translation using the same rounding strategy used by ui::Transform.
69 const auto tx = static_cast<int32_t>(t.tx() + 0.5);
70 const auto ty = static_cast<int32_t>(t.ty() + 0.5);
71
72 ui::Transform transformWithoutOffset = t;
73 transformWithoutOffset.set(0.f, 0.f);
74
75 const Region transformed = transformWithoutOffset.transform(r);
76
77 // Apply the translation to each of the Rects in the region while discarding any that overflow.
78 Region ret;
79 for (const auto& rect : transformed) {
80 Rect newRect;
81 if (__builtin_add_overflow(rect.left, tx, &newRect.left) ||
82 __builtin_add_overflow(rect.top, ty, &newRect.top) ||
83 __builtin_add_overflow(rect.right, tx, &newRect.right) ||
84 __builtin_add_overflow(rect.bottom, ty, &newRect.bottom)) {
85 ALOGE("Applying transform to touchable region of window '%s' resulted in an overflow.",
86 debugWindowName.c_str());
87 continue;
88 }
89 ret.orSelf(newRect);
90 }
91 return ret;
92}
93
94/*
95 * We don't want to send the layer's transform to input, but rather the
96 * parent's transform. This is because Layer's transform is
97 * information about how the buffer is placed on screen. The parent's
98 * transform makes more sense to send since it's information about how the
99 * layer is placed on screen. This transform is used by input to determine
100 * how to go from screen space back to window space.
101 */
102ui::Transform getInputTransform(const LayerSnapshot& snapshot) {
103 if (!snapshot.hasBufferOrSidebandStream()) {
104 return snapshot.geomLayerTransform;
105 }
106 return snapshot.parentTransform;
107}
108
109/**
Vishnu Nairfed7c122023-03-18 01:54:43 +0000110 * Returns the bounds used to fill the input frame and the touchable region.
111 *
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000112 * Similar to getInputTransform, we need to update the bounds to include the transform.
113 * This is because bounds don't include the buffer transform, where the input assumes
114 * that's already included.
115 */
Vishnu Nairfed7c122023-03-18 01:54:43 +0000116std::pair<FloatRect, bool> getInputBounds(const LayerSnapshot& snapshot, bool fillParentBounds) {
117 FloatRect inputBounds = snapshot.croppedBufferSize.toFloatRect();
118 if (snapshot.hasBufferOrSidebandStream() && snapshot.croppedBufferSize.isValid() &&
119 snapshot.localTransform.getType() != ui::Transform::IDENTITY) {
120 inputBounds = snapshot.localTransform.transform(inputBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000121 }
122
Vishnu Nairfed7c122023-03-18 01:54:43 +0000123 bool inputBoundsValid = snapshot.croppedBufferSize.isValid();
124 if (!inputBoundsValid) {
125 /**
126 * Input bounds are based on the layer crop or buffer size. But if we are using
127 * the layer bounds as the input bounds (replaceTouchableRegionWithCrop flag) then
128 * we can use the parent bounds as the input bounds if the layer does not have buffer
129 * or a crop. We want to unify this logic but because of compat reasons we cannot always
130 * use the parent bounds. A layer without a buffer can get input. So when a window is
131 * initially added, its touchable region can fill its parent layer bounds and that can
132 * have negative consequences.
133 */
134 inputBounds = fillParentBounds ? snapshot.geomLayerBounds : FloatRect{};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000135 }
Vishnu Nairfed7c122023-03-18 01:54:43 +0000136
137 // Clamp surface inset to the input bounds.
138 const float inset = static_cast<float>(snapshot.inputInfo.surfaceInset);
139 const float xSurfaceInset = std::clamp(inset, 0.f, inputBounds.getWidth() / 2.f);
140 const float ySurfaceInset = std::clamp(inset, 0.f, inputBounds.getHeight() / 2.f);
141
142 // Apply the insets to the input bounds.
143 inputBounds.left += xSurfaceInset;
144 inputBounds.top += ySurfaceInset;
145 inputBounds.right -= xSurfaceInset;
146 inputBounds.bottom -= ySurfaceInset;
147 return {inputBounds, inputBoundsValid};
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000148}
149
Vishnu Nairfed7c122023-03-18 01:54:43 +0000150Rect getInputBoundsInDisplaySpace(const LayerSnapshot& snapshot, const FloatRect& insetBounds,
151 const ui::Transform& screenToDisplay) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000152 // InputDispatcher works in the display device's coordinate space. Here, we calculate the
153 // frame and transform used for the layer, which determines the bounds and the coordinate space
154 // within which the layer will receive input.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000155
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000156 // Coordinate space definitions:
157 // - display: The display device's coordinate space. Correlates to pixels on the display.
158 // - screen: The post-rotation coordinate space for the display, a.k.a. logical display space.
159 // - layer: The coordinate space of this layer.
160 // - input: The coordinate space in which this layer will receive input events. This could be
161 // different than layer space if a surfaceInset is used, which changes the origin
162 // of the input space.
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000163
164 // Crop the input bounds to ensure it is within the parent's bounds.
Vishnu Nairfed7c122023-03-18 01:54:43 +0000165 const FloatRect croppedInsetBoundsInLayer = snapshot.geomLayerBounds.intersect(insetBounds);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000166
167 const ui::Transform layerToScreen = getInputTransform(snapshot);
168 const ui::Transform layerToDisplay = screenToDisplay * layerToScreen;
169
Vishnu Nairfed7c122023-03-18 01:54:43 +0000170 return Rect{layerToDisplay.transform(croppedInsetBoundsInLayer)};
171}
172
173void fillInputFrameInfo(gui::WindowInfo& info, const ui::Transform& screenToDisplay,
174 const LayerSnapshot& snapshot) {
175 auto [inputBounds, inputBoundsValid] = getInputBounds(snapshot, /*fillParentBounds=*/false);
176 if (!inputBoundsValid) {
177 info.touchableRegion.clear();
178 }
179
180 const Rect roundedFrameInDisplay =
181 getInputBoundsInDisplaySpace(snapshot, inputBounds, screenToDisplay);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000182 info.frameLeft = roundedFrameInDisplay.left;
183 info.frameTop = roundedFrameInDisplay.top;
184 info.frameRight = roundedFrameInDisplay.right;
185 info.frameBottom = roundedFrameInDisplay.bottom;
186
187 ui::Transform inputToLayer;
Vishnu Nairfed7c122023-03-18 01:54:43 +0000188 inputToLayer.set(inputBounds.left, inputBounds.top);
189 const ui::Transform layerToScreen = getInputTransform(snapshot);
190 const ui::Transform inputToDisplay = screenToDisplay * layerToScreen * inputToLayer;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000191
192 // InputDispatcher expects a display-to-input transform.
193 info.transform = inputToDisplay.inverse();
194
195 // The touchable region is specified in the input coordinate space. Change it to display space.
196 info.touchableRegion =
197 transformTouchableRegionSafely(inputToDisplay, info.touchableRegion, snapshot.name);
198}
199
200void handleDropInputMode(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot) {
201 if (snapshot.inputInfo.inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
202 return;
203 }
204
205 // Check if we need to drop input unconditionally
206 const gui::DropInputMode dropInputMode = snapshot.dropInputMode;
207 if (dropInputMode == gui::DropInputMode::ALL) {
208 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
209 ALOGV("Dropping input for %s as requested by policy.", snapshot.name.c_str());
210 return;
211 }
212
213 // Check if we need to check if the window is obscured by parent
214 if (dropInputMode != gui::DropInputMode::OBSCURED) {
215 return;
216 }
217
218 // Check if the parent has set an alpha on the layer
219 if (parentSnapshot.color.a != 1.0_hf) {
220 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
221 ALOGV("Dropping input for %s as requested by policy because alpha=%f",
222 snapshot.name.c_str(), static_cast<float>(parentSnapshot.color.a));
223 }
224
225 // Check if the parent has cropped the buffer
226 Rect bufferSize = snapshot.croppedBufferSize;
227 if (!bufferSize.isValid()) {
228 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
229 return;
230 }
231
232 // Screenbounds are the layer bounds cropped by parents, transformed to screenspace.
233 // To check if the layer has been cropped, we take the buffer bounds, apply the local
234 // layer crop and apply the same set of transforms to move to screenspace. If the bounds
235 // match then the layer has not been cropped by its parents.
236 Rect bufferInScreenSpace(snapshot.geomLayerTransform.transform(bufferSize));
237 bool croppedByParent = bufferInScreenSpace != Rect{snapshot.transformedBounds};
238
239 if (croppedByParent) {
240 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
241 ALOGV("Dropping input for %s as requested by policy because buffer is cropped by parent",
242 snapshot.name.c_str());
243 } else {
244 // If the layer is not obscured by its parents (by setting an alpha or crop), then only drop
245 // input if the window is obscured. This check should be done in surfaceflinger but the
246 // logic currently resides in inputflinger. So pass the if_obscured check to input to only
247 // drop input events if the window is obscured.
248 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED;
249 }
250}
251
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000252auto getBlendMode(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
253 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
254 if (snapshot.alpha != 1.0f || !snapshot.isContentOpaque()) {
255 blendMode = requested.premultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
256 : Hwc2::IComposerClient::BlendMode::COVERAGE;
257 }
258 return blendMode;
259}
260
Vishnu Naircfb2d252023-01-19 04:44:02 +0000261void updateSurfaceDamage(const RequestedLayerState& requested, bool hasReadyFrame,
262 bool forceFullDamage, Region& outSurfaceDamageRegion) {
263 if (!hasReadyFrame) {
264 outSurfaceDamageRegion.clear();
265 return;
266 }
267 if (forceFullDamage) {
268 outSurfaceDamageRegion = Region::INVALID_REGION;
269 } else {
270 outSurfaceDamageRegion = requested.surfaceDamageRegion;
271 }
272}
273
Vishnu Nair80a5a702023-02-11 01:21:51 +0000274void updateVisibility(LayerSnapshot& snapshot, bool visible) {
275 snapshot.isVisible = visible;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000276
277 // TODO(b/238781169) we are ignoring this compat for now, since we will have
278 // to remove any optimization based on visibility.
279
280 // For compatibility reasons we let layers which can receive input
281 // receive input before they have actually submitted a buffer. Because
282 // of this we use canReceiveInput instead of isVisible to check the
283 // policy-visibility, ignoring the buffer state. However for layers with
284 // hasInputInfo()==false we can use the real visibility state.
285 // We are just using these layers for occlusion detection in
286 // InputDispatcher, and obviously if they aren't visible they can't occlude
287 // anything.
Vishnu Nair80a5a702023-02-11 01:21:51 +0000288 const bool visibleForInput =
Vishnu Nair40d02282023-02-28 21:11:40 +0000289 snapshot.hasInputInfo() ? snapshot.canReceiveInput() : snapshot.isVisible;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000290 snapshot.inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_VISIBLE, !visibleForInput);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000291}
292
293bool needsInputInfo(const LayerSnapshot& snapshot, const RequestedLayerState& requested) {
294 if (requested.potentialCursor) {
295 return false;
296 }
297
298 if (snapshot.inputInfo.token != nullptr) {
299 return true;
300 }
301
302 if (snapshot.hasBufferOrSidebandStream()) {
303 return true;
304 }
305
306 return requested.windowInfoHandle &&
307 requested.windowInfoHandle->getInfo()->inputConfig.test(
308 gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
309}
310
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000311void updateMetadata(LayerSnapshot& snapshot, const RequestedLayerState& requested,
312 const LayerSnapshotBuilder::Args& args) {
313 snapshot.metadata.clear();
314 for (const auto& [key, mandatory] : args.supportedLayerGenericMetadata) {
315 auto compatIter = args.genericLayerMetadataKeyMap.find(key);
316 if (compatIter == std::end(args.genericLayerMetadataKeyMap)) {
317 continue;
318 }
319 const uint32_t id = compatIter->second;
320 auto it = requested.metadata.mMap.find(id);
321 if (it == std::end(requested.metadata.mMap)) {
322 continue;
323 }
324
325 snapshot.metadata.emplace(key,
326 compositionengine::GenericLayerMetadataEntry{mandatory,
327 it->second});
328 }
329}
330
Vishnu Naircfb2d252023-01-19 04:44:02 +0000331void clearChanges(LayerSnapshot& snapshot) {
332 snapshot.changes.clear();
333 snapshot.contentDirty = false;
334 snapshot.hasReadyFrame = false;
335 snapshot.sidebandStreamHasFrame = false;
336 snapshot.surfaceDamage.clear();
337}
338
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000339} // namespace
340
341LayerSnapshot LayerSnapshotBuilder::getRootSnapshot() {
342 LayerSnapshot snapshot;
Vishnu Nair92990e22023-02-24 20:01:05 +0000343 snapshot.path = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000344 snapshot.changes = ftl::Flags<RequestedLayerState::Changes>();
345 snapshot.isHiddenByPolicyFromParent = false;
346 snapshot.isHiddenByPolicyFromRelativeParent = false;
347 snapshot.parentTransform.reset();
348 snapshot.geomLayerTransform.reset();
349 snapshot.geomInverseLayerTransform.reset();
350 snapshot.geomLayerBounds = getMaxDisplayBounds({});
351 snapshot.roundedCorner = RoundedCornerState();
352 snapshot.stretchEffect = {};
353 snapshot.outputFilter.layerStack = ui::DEFAULT_LAYER_STACK;
354 snapshot.outputFilter.toInternalDisplay = false;
355 snapshot.isSecure = false;
356 snapshot.color.a = 1.0_hf;
357 snapshot.colorTransformIsIdentity = true;
358 snapshot.shadowRadius = 0.f;
359 snapshot.layerMetadata.mMap.clear();
360 snapshot.relativeLayerMetadata.mMap.clear();
361 snapshot.inputInfo.touchOcclusionMode = gui::TouchOcclusionMode::BLOCK_UNTRUSTED;
362 snapshot.dropInputMode = gui::DropInputMode::NONE;
363 snapshot.isTrustedOverlay = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000364 snapshot.gameMode = gui::GameMode::Unsupported;
365 snapshot.frameRate = {};
366 snapshot.fixedTransformHint = ui::Transform::ROT_INVALID;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000367 return snapshot;
368}
369
370LayerSnapshotBuilder::LayerSnapshotBuilder() : mRootSnapshot(getRootSnapshot()) {}
371
372LayerSnapshotBuilder::LayerSnapshotBuilder(Args args) : LayerSnapshotBuilder() {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000373 args.forceUpdate = ForceUpdateFlags::ALL;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000374 updateSnapshots(args);
375}
376
377bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000378 if (args.forceUpdate != ForceUpdateFlags::NONE || args.displayChanges) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000379 // force update requested, or we have display changes, so skip the fast path
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000380 return false;
381 }
382
383 if (args.layerLifecycleManager.getGlobalChanges().get() == 0) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000384 return true;
385 }
386
387 if (args.layerLifecycleManager.getGlobalChanges() != RequestedLayerState::Changes::Content) {
388 // We have changes that require us to walk the hierarchy and update child layers.
389 // No fast path for you.
390 return false;
391 }
392
393 // There are only content changes which do not require any child layer snapshots to be updated.
394 ALOGV("%s", __func__);
395 ATRACE_NAME("FastPath");
396
397 // Collect layers with changes
398 ftl::SmallMap<uint32_t, RequestedLayerState*, 10> layersWithChanges;
399 for (auto& layer : args.layerLifecycleManager.getLayers()) {
400 if (layer->changes.test(RequestedLayerState::Changes::Content)) {
401 layersWithChanges.emplace_or_replace(layer->id, layer.get());
402 }
403 }
404
405 // Walk through the snapshots, clearing previous change flags and updating the snapshots
406 // if needed.
407 for (auto& snapshot : mSnapshots) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000408 auto it = layersWithChanges.find(snapshot->path.id);
409 if (it != layersWithChanges.end()) {
410 ALOGV("%s fast path snapshot changes = %s", __func__,
411 mRootSnapshot.changes.string().c_str());
412 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Nair92990e22023-02-24 20:01:05 +0000413 updateSnapshot(*snapshot, args, *it->second, mRootSnapshot, root);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000414 }
415 }
416 return true;
417}
418
419void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
420 ATRACE_NAME("UpdateSnapshots");
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000421 if (args.parentCrop) {
422 mRootSnapshot.geomLayerBounds = *args.parentCrop;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000423 } else if (args.forceUpdate == ForceUpdateFlags::ALL || args.displayChanges) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000424 mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
425 }
426 if (args.displayChanges) {
427 mRootSnapshot.changes = RequestedLayerState::Changes::AffectsChildren |
428 RequestedLayerState::Changes::Geometry;
429 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000430 if (args.forceUpdate == ForceUpdateFlags::HIERARCHY) {
431 mRootSnapshot.changes |=
432 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Visibility;
433 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000434 LayerHierarchy::TraversalPath root = LayerHierarchy::TraversalPath::ROOT;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000435 if (args.root.getLayer()) {
436 // The hierarchy can have a root layer when used for screenshots otherwise, it will have
437 // multiple children.
438 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root, args.root.getLayer()->id,
439 LayerHierarchy::Variant::Attached);
440 updateSnapshotsInHierarchy(args, args.root, root, mRootSnapshot);
441 } else {
442 for (auto& [childHierarchy, variant] : args.root.mChildren) {
443 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(root,
444 childHierarchy->getLayer()->id,
445 variant);
446 updateSnapshotsInHierarchy(args, *childHierarchy, root, mRootSnapshot);
447 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000448 }
449
Vishnu Nairfccd6362023-02-24 23:39:53 +0000450 const bool hasUnreachableSnapshots = sortSnapshotsByZ(args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000451 clearChanges(mRootSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000452
453 // Destroy unreachable snapshots
Vishnu Nairfccd6362023-02-24 23:39:53 +0000454 if (!hasUnreachableSnapshots) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000455 return;
456 }
457
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000458 auto it = mSnapshots.begin();
459 while (it < mSnapshots.end()) {
460 auto& traversalPath = it->get()->path;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000461 if (!it->get()->unreachable) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000462 it++;
463 continue;
464 }
465
466 mIdToSnapshot.erase(traversalPath);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000467 mSnapshots.back()->globalZ = it->get()->globalZ;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000468 std::iter_swap(it, mSnapshots.end() - 1);
469 mSnapshots.erase(mSnapshots.end() - 1);
470 }
471}
472
473void LayerSnapshotBuilder::update(const Args& args) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000474 for (auto& snapshot : mSnapshots) {
475 clearChanges(*snapshot);
476 }
477
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000478 if (tryFastUpdate(args)) {
479 return;
480 }
481 updateSnapshots(args);
482}
483
Vishnu Naircfb2d252023-01-19 04:44:02 +0000484const LayerSnapshot& LayerSnapshotBuilder::updateSnapshotsInHierarchy(
485 const Args& args, const LayerHierarchy& hierarchy,
486 LayerHierarchy::TraversalPath& traversalPath, const LayerSnapshot& parentSnapshot) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000487 const RequestedLayerState* layer = hierarchy.getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000488 LayerSnapshot* snapshot = getSnapshot(traversalPath);
489 const bool newSnapshot = snapshot == nullptr;
490 if (newSnapshot) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000491 snapshot = createSnapshot(traversalPath, *layer, parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000492 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000493 scheduler::LayerInfo::FrameRate oldFrameRate = snapshot->frameRate;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000494 if (traversalPath.isRelative()) {
495 bool parentIsRelative = traversalPath.variant == LayerHierarchy::Variant::Relative;
496 updateRelativeState(*snapshot, parentSnapshot, parentIsRelative, args);
497 } else {
498 if (traversalPath.isAttached()) {
499 resetRelativeState(*snapshot);
500 }
Vishnu Nair92990e22023-02-24 20:01:05 +0000501 updateSnapshot(*snapshot, args, *layer, parentSnapshot, traversalPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000502 }
503
504 for (auto& [childHierarchy, variant] : hierarchy.mChildren) {
505 LayerHierarchy::ScopedAddToTraversalPath addChildToPath(traversalPath,
506 childHierarchy->getLayer()->id,
507 variant);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000508 const LayerSnapshot& childSnapshot =
509 updateSnapshotsInHierarchy(args, *childHierarchy, traversalPath, *snapshot);
510 updateChildState(*snapshot, childSnapshot, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000511 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000512
513 if (oldFrameRate == snapshot->frameRate) {
514 snapshot->changes.clear(RequestedLayerState::Changes::FrameRate);
515 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000516 return *snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000517}
518
519LayerSnapshot* LayerSnapshotBuilder::getSnapshot(uint32_t layerId) const {
520 if (layerId == UNASSIGNED_LAYER_ID) {
521 return nullptr;
522 }
523 LayerHierarchy::TraversalPath path{.id = layerId};
524 return getSnapshot(path);
525}
526
527LayerSnapshot* LayerSnapshotBuilder::getSnapshot(const LayerHierarchy::TraversalPath& id) const {
528 auto it = mIdToSnapshot.find(id);
529 return it == mIdToSnapshot.end() ? nullptr : it->second;
530}
531
Vishnu Nair92990e22023-02-24 20:01:05 +0000532LayerSnapshot* LayerSnapshotBuilder::createSnapshot(const LayerHierarchy::TraversalPath& path,
533 const RequestedLayerState& layer,
534 const LayerSnapshot& parentSnapshot) {
535 mSnapshots.emplace_back(std::make_unique<LayerSnapshot>(layer, path));
Vishnu Naircfb2d252023-01-19 04:44:02 +0000536 LayerSnapshot* snapshot = mSnapshots.back().get();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000537 snapshot->globalZ = static_cast<size_t>(mSnapshots.size()) - 1;
Vishnu Nair92990e22023-02-24 20:01:05 +0000538 if (path.isClone() && path.variant != LayerHierarchy::Variant::Mirror) {
539 snapshot->mirrorRootPath = parentSnapshot.mirrorRootPath;
540 }
541 mIdToSnapshot[path] = snapshot;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000542 return snapshot;
543}
544
Vishnu Nairfccd6362023-02-24 23:39:53 +0000545bool LayerSnapshotBuilder::sortSnapshotsByZ(const Args& args) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000546 if (!mResortSnapshots && args.forceUpdate == ForceUpdateFlags::NONE &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000547 !args.layerLifecycleManager.getGlobalChanges().any(
548 RequestedLayerState::Changes::Hierarchy |
549 RequestedLayerState::Changes::Visibility)) {
550 // We are not force updating and there are no hierarchy or visibility changes. Avoid sorting
551 // the snapshots.
Vishnu Nairfccd6362023-02-24 23:39:53 +0000552 return false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000553 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000554 mResortSnapshots = false;
555
Vishnu Nairfccd6362023-02-24 23:39:53 +0000556 for (auto& snapshot : mSnapshots) {
557 snapshot->unreachable = true;
558 }
559
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000560 size_t globalZ = 0;
561 args.root.traverseInZOrder(
562 [this, &globalZ](const LayerHierarchy&,
563 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
564 LayerSnapshot* snapshot = getSnapshot(traversalPath);
565 if (!snapshot) {
566 return false;
567 }
568
Vishnu Nairfccd6362023-02-24 23:39:53 +0000569 snapshot->unreachable = false;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000570 if (snapshot->getIsVisible() || snapshot->hasInputInfo()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000571 updateVisibility(*snapshot, snapshot->getIsVisible());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000572 size_t oldZ = snapshot->globalZ;
573 size_t newZ = globalZ++;
574 snapshot->globalZ = newZ;
575 if (oldZ == newZ) {
576 return true;
577 }
578 mSnapshots[newZ]->globalZ = oldZ;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000579 LLOGV(snapshot->sequence, "Made visible z=%zu -> %zu %s", oldZ, newZ,
580 snapshot->getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000581 std::iter_swap(mSnapshots.begin() + static_cast<ssize_t>(oldZ),
582 mSnapshots.begin() + static_cast<ssize_t>(newZ));
583 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000584 return true;
585 });
Vishnu Naircfb2d252023-01-19 04:44:02 +0000586 mNumInterestingSnapshots = (int)globalZ;
Vishnu Nairfccd6362023-02-24 23:39:53 +0000587 bool hasUnreachableSnapshots = false;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000588 while (globalZ < mSnapshots.size()) {
589 mSnapshots[globalZ]->globalZ = globalZ;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000590 /* mark unreachable snapshots as explicitly invisible */
591 updateVisibility(*mSnapshots[globalZ], false);
Vishnu Nairfccd6362023-02-24 23:39:53 +0000592 if (mSnapshots[globalZ]->unreachable) {
593 hasUnreachableSnapshots = true;
594 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000595 globalZ++;
596 }
Vishnu Nairfccd6362023-02-24 23:39:53 +0000597 return hasUnreachableSnapshots;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000598}
599
600void LayerSnapshotBuilder::updateRelativeState(LayerSnapshot& snapshot,
601 const LayerSnapshot& parentSnapshot,
602 bool parentIsRelative, const Args& args) {
603 if (parentIsRelative) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000604 snapshot.isHiddenByPolicyFromRelativeParent =
605 parentSnapshot.isHiddenByPolicyFromParent || parentSnapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000606 if (args.includeMetadata) {
607 snapshot.relativeLayerMetadata = parentSnapshot.layerMetadata;
608 }
609 } else {
610 snapshot.isHiddenByPolicyFromRelativeParent =
611 parentSnapshot.isHiddenByPolicyFromRelativeParent;
612 if (args.includeMetadata) {
613 snapshot.relativeLayerMetadata = parentSnapshot.relativeLayerMetadata;
614 }
615 }
616 snapshot.isVisible = snapshot.getIsVisible();
617}
618
Vishnu Naircfb2d252023-01-19 04:44:02 +0000619void LayerSnapshotBuilder::updateChildState(LayerSnapshot& snapshot,
620 const LayerSnapshot& childSnapshot, const Args& args) {
621 if (snapshot.childState.hasValidFrameRate) {
622 return;
623 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000624 if (args.forceUpdate == ForceUpdateFlags::ALL ||
625 childSnapshot.changes.test(RequestedLayerState::Changes::FrameRate)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000626 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes
627 // for the same reason we are allowing touch boost for those layers. See
628 // RefreshRateSelector::rankFrameRates for details.
629 using FrameRateCompatibility = scheduler::LayerInfo::FrameRateCompatibility;
630 const auto layerVotedWithDefaultCompatibility = childSnapshot.frameRate.rate.isValid() &&
631 childSnapshot.frameRate.type == FrameRateCompatibility::Default;
632 const auto layerVotedWithNoVote =
633 childSnapshot.frameRate.type == FrameRateCompatibility::NoVote;
634 const auto layerVotedWithExactCompatibility = childSnapshot.frameRate.rate.isValid() &&
635 childSnapshot.frameRate.type == FrameRateCompatibility::Exact;
636
637 snapshot.childState.hasValidFrameRate |= layerVotedWithDefaultCompatibility ||
638 layerVotedWithNoVote || layerVotedWithExactCompatibility;
639
640 // If we don't have a valid frame rate, but the children do, we set this
641 // layer as NoVote to allow the children to control the refresh rate
642 if (!snapshot.frameRate.rate.isValid() &&
643 snapshot.frameRate.type != FrameRateCompatibility::NoVote &&
644 snapshot.childState.hasValidFrameRate) {
645 snapshot.frameRate =
646 scheduler::LayerInfo::FrameRate(Fps(), FrameRateCompatibility::NoVote);
647 snapshot.changes |= childSnapshot.changes & RequestedLayerState::Changes::FrameRate;
648 }
649 }
650}
651
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000652void LayerSnapshotBuilder::resetRelativeState(LayerSnapshot& snapshot) {
653 snapshot.isHiddenByPolicyFromRelativeParent = false;
654 snapshot.relativeLayerMetadata.mMap.clear();
655}
656
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700657uint32_t getPrimaryDisplayRotationFlags(
658 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays) {
659 for (auto& [_, display] : displays) {
660 if (display.isPrimary) {
661 return display.rotationFlags;
662 }
663 }
664 return 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000665}
666
667void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& args,
668 const RequestedLayerState& requested,
669 const LayerSnapshot& parentSnapshot,
Vishnu Nair92990e22023-02-24 20:01:05 +0000670 const LayerHierarchy::TraversalPath& path) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000671 // Always update flags and visibility
672 ftl::Flags<RequestedLayerState::Changes> parentChanges = parentSnapshot.changes &
673 (RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::Geometry |
674 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Metadata |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000675 RequestedLayerState::Changes::AffectsChildren |
676 RequestedLayerState::Changes::FrameRate);
Vishnu Nair92990e22023-02-24 20:01:05 +0000677 snapshot.changes |= parentChanges | requested.changes;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000678 snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
Vishnu Nair3af0ec02023-02-10 04:13:48 +0000679 parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
680 (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000681 snapshot.contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000682 // TODO(b/238781169) scope down the changes to only buffer updates.
Vishnu Naird47bcee2023-02-24 18:08:51 +0000683 snapshot.hasReadyFrame = requested.hasReadyFrame();
684 snapshot.sidebandStreamHasFrame = requested.hasSidebandStreamFrame();
Vishnu Naircfb2d252023-01-19 04:44:02 +0000685 updateSurfaceDamage(requested, snapshot.hasReadyFrame, args.forceFullDamage,
686 snapshot.surfaceDamage);
Vishnu Nair92990e22023-02-24 20:01:05 +0000687 snapshot.outputFilter.layerStack = parentSnapshot.path == LayerHierarchy::TraversalPath::ROOT
688 ? requested.layerStack
689 : parentSnapshot.outputFilter.layerStack;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000690
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700691 uint32_t primaryDisplayRotationFlags = getPrimaryDisplayRotationFlags(args.displays);
Vishnu Nair92990e22023-02-24 20:01:05 +0000692 const bool forceUpdate = args.forceUpdate == ForceUpdateFlags::ALL ||
693 snapshot.changes.any(RequestedLayerState::Changes::Visibility |
694 RequestedLayerState::Changes::Created);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000695
Vishnu Naircfb2d252023-01-19 04:44:02 +0000696 // always update the buffer regardless of visibility
Vishnu Nair80a5a702023-02-11 01:21:51 +0000697 if (forceUpdate || requested.what & layer_state_t::BUFFER_CHANGES || args.displayChanges) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000698 snapshot.acquireFence =
699 (requested.externalTexture &&
700 requested.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged))
701 ? requested.bufferData->acquireFence
702 : Fence::NO_FENCE;
703 snapshot.buffer =
704 requested.externalTexture ? requested.externalTexture->getBuffer() : nullptr;
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700705 snapshot.bufferSize = requested.getBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000706 snapshot.geomBufferSize = snapshot.bufferSize;
707 snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize);
708 snapshot.dataspace = requested.dataspace;
709 snapshot.externalTexture = requested.externalTexture;
710 snapshot.frameNumber = (requested.bufferData) ? requested.bufferData->frameNumber : 0;
711 snapshot.geomBufferTransform = requested.bufferTransform;
712 snapshot.geomBufferUsesDisplayInverseTransform = requested.transformToDisplayInverse;
713 snapshot.geomContentCrop = requested.getBufferCrop();
714 snapshot.geomUsesSourceCrop = snapshot.hasBufferOrSidebandStream();
715 snapshot.hasProtectedContent = requested.externalTexture &&
716 requested.externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
717 snapshot.isHdrY410 = requested.dataspace == ui::Dataspace::BT2020_ITU_PQ &&
718 requested.api == NATIVE_WINDOW_API_MEDIA &&
719 requested.bufferData->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102;
720 snapshot.sidebandStream = requested.sidebandStream;
721 snapshot.transparentRegionHint = requested.transparentRegion;
722 snapshot.color.rgb = requested.getColor().rgb;
John Reck68796592023-01-25 13:47:12 -0500723 snapshot.currentSdrHdrRatio = requested.currentSdrHdrRatio;
724 snapshot.desiredSdrHdrRatio = requested.desiredSdrHdrRatio;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000725 }
726
Vishnu Nair92990e22023-02-24 20:01:05 +0000727 if (snapshot.isHiddenByPolicyFromParent &&
728 !snapshot.changes.test(RequestedLayerState::Changes::Created)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000729 if (forceUpdate ||
730 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
731 RequestedLayerState::Changes::Geometry |
732 RequestedLayerState::Changes::Input)) {
733 updateInput(snapshot, requested, parentSnapshot, path, args);
734 }
735 return;
736 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000737
738 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren)) {
739 // If root layer, use the layer stack otherwise get the parent's layer stack.
740 snapshot.color.a = parentSnapshot.color.a * requested.color.a;
741 snapshot.alpha = snapshot.color.a;
742 snapshot.isSecure =
743 parentSnapshot.isSecure || (requested.flags & layer_state_t::eLayerSecure);
744 snapshot.isTrustedOverlay = parentSnapshot.isTrustedOverlay || requested.isTrustedOverlay;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000745 snapshot.outputFilter.toInternalDisplay = parentSnapshot.outputFilter.toInternalDisplay ||
746 (requested.flags & layer_state_t::eLayerSkipScreenshot);
747 snapshot.stretchEffect = (requested.stretchEffect.hasEffect())
748 ? requested.stretchEffect
749 : parentSnapshot.stretchEffect;
750 if (!parentSnapshot.colorTransformIsIdentity) {
751 snapshot.colorTransform = parentSnapshot.colorTransform * requested.colorTransform;
752 snapshot.colorTransformIsIdentity = false;
753 } else {
754 snapshot.colorTransform = requested.colorTransform;
755 snapshot.colorTransformIsIdentity = !requested.hasColorTransform;
756 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000757 snapshot.gameMode = requested.metadata.has(gui::METADATA_GAME_MODE)
758 ? requested.gameMode
759 : parentSnapshot.gameMode;
Vishnu Naira9c43762023-01-27 19:10:25 +0000760 // Display mirrors are always placed in a VirtualDisplay so we never want to capture layers
761 // marked as skip capture
762 snapshot.handleSkipScreenshotFlag = parentSnapshot.handleSkipScreenshotFlag ||
763 (requested.layerStackToMirror != ui::INVALID_LAYER_STACK);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000764 }
765
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700766 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::AffectsChildren) ||
767 args.displayChanges) {
768 snapshot.fixedTransformHint = requested.fixedTransformHint != ui::Transform::ROT_INVALID
769 ? requested.fixedTransformHint
770 : parentSnapshot.fixedTransformHint;
771
772 if (snapshot.fixedTransformHint != ui::Transform::ROT_INVALID) {
773 snapshot.transformHint = snapshot.fixedTransformHint;
774 } else {
775 const auto display = args.displays.get(snapshot.outputFilter.layerStack);
776 snapshot.transformHint = display.has_value()
777 ? std::make_optional<>(display->get().transformHint)
778 : std::nullopt;
779 }
780 }
781
Vishnu Naird47bcee2023-02-24 18:08:51 +0000782 if (forceUpdate ||
783 snapshot.changes.any(RequestedLayerState::Changes::FrameRate |
784 RequestedLayerState::Changes::Hierarchy)) {
785 snapshot.frameRate = (requested.requestedFrameRate.rate.isValid() ||
786 (requested.requestedFrameRate.type ==
787 scheduler::LayerInfo::FrameRateCompatibility::NoVote))
788 ? requested.requestedFrameRate
789 : parentSnapshot.frameRate;
790 }
791
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000792 if (forceUpdate || requested.what & layer_state_t::eMetadataChanged) {
793 updateMetadata(snapshot, requested, args);
794 }
795
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000796 if (forceUpdate || requested.changes.get() != 0) {
797 snapshot.compositionType = requested.getCompositionType();
798 snapshot.dimmingEnabled = requested.dimmingEnabled;
799 snapshot.layerOpaqueFlagSet =
800 (requested.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque;
Alec Mourif4af03e2023-02-11 00:25:24 +0000801 snapshot.cachingHint = requested.cachingHint;
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000802 snapshot.frameRateSelectionPriority = requested.frameRateSelectionPriority;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000803 }
804
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000805 if (forceUpdate || snapshot.changes.any(RequestedLayerState::Changes::Content)) {
806 snapshot.color.rgb = requested.getColor().rgb;
807 snapshot.isColorspaceAgnostic = requested.colorSpaceAgnostic;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000808 snapshot.backgroundBlurRadius = args.supportsBlur
809 ? static_cast<int>(parentSnapshot.color.a * (float)requested.backgroundBlurRadius)
810 : 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000811 snapshot.blurRegions = requested.blurRegions;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000812 for (auto& region : snapshot.blurRegions) {
813 region.alpha = region.alpha * snapshot.color.a;
814 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000815 snapshot.hdrMetadata = requested.hdrMetadata;
816 }
817
818 if (forceUpdate ||
819 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
820 RequestedLayerState::Changes::Geometry)) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700821 updateLayerBounds(snapshot, requested, parentSnapshot, primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000822 updateRoundedCorner(snapshot, requested, parentSnapshot);
823 }
824
825 if (forceUpdate ||
826 snapshot.changes.any(RequestedLayerState::Changes::Hierarchy |
827 RequestedLayerState::Changes::Geometry |
828 RequestedLayerState::Changes::Input)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000829 updateInput(snapshot, requested, parentSnapshot, path, args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000830 }
831
832 // computed snapshot properties
833 updateShadows(snapshot, requested, args.globalShadowSettings);
834 if (args.includeMetadata) {
835 snapshot.layerMetadata = parentSnapshot.layerMetadata;
836 snapshot.layerMetadata.merge(requested.metadata);
837 }
838 snapshot.forceClientComposition = snapshot.isHdrY410 || snapshot.shadowSettings.length > 0 ||
839 requested.blurRegions.size() > 0 || snapshot.stretchEffect.hasEffect();
Vishnu Nairc765c6c2023-02-23 00:08:01 +0000840 snapshot.contentOpaque = snapshot.isContentOpaque();
841 snapshot.isOpaque = snapshot.contentOpaque && !snapshot.roundedCorner.hasRoundedCorners() &&
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000842 snapshot.color.a == 1.f;
843 snapshot.blendMode = getBlendMode(snapshot, requested);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000844 LLOGV(snapshot.sequence,
Vishnu Nair92990e22023-02-24 20:01:05 +0000845 "%supdated %s changes:%s parent:%s requested:%s requested:%s from parent %s",
846 args.forceUpdate == ForceUpdateFlags::ALL ? "Force " : "",
847 snapshot.getDebugString().c_str(), snapshot.changes.string().c_str(),
848 parentSnapshot.changes.string().c_str(), requested.changes.string().c_str(),
849 std::to_string(requested.what).c_str(), parentSnapshot.getDebugString().c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000850}
851
852void LayerSnapshotBuilder::updateRoundedCorner(LayerSnapshot& snapshot,
853 const RequestedLayerState& requested,
854 const LayerSnapshot& parentSnapshot) {
855 snapshot.roundedCorner = RoundedCornerState();
856 RoundedCornerState parentRoundedCorner;
857 if (parentSnapshot.roundedCorner.hasRoundedCorners()) {
858 parentRoundedCorner = parentSnapshot.roundedCorner;
859 ui::Transform t = snapshot.localTransform.inverse();
860 parentRoundedCorner.cropRect = t.transform(parentRoundedCorner.cropRect);
861 parentRoundedCorner.radius.x *= t.getScaleX();
862 parentRoundedCorner.radius.y *= t.getScaleY();
863 }
864
865 FloatRect layerCropRect = snapshot.croppedBufferSize.toFloatRect();
866 const vec2 radius(requested.cornerRadius, requested.cornerRadius);
867 RoundedCornerState layerSettings(layerCropRect, radius);
868 const bool layerSettingsValid = layerSettings.hasRoundedCorners() && !layerCropRect.isEmpty();
869 const bool parentRoundedCornerValid = parentRoundedCorner.hasRoundedCorners();
870 if (layerSettingsValid && parentRoundedCornerValid) {
871 // If the parent and the layer have rounded corner settings, use the parent settings if
872 // the parent crop is entirely inside the layer crop. This has limitations and cause
873 // rendering artifacts. See b/200300845 for correct fix.
874 if (parentRoundedCorner.cropRect.left > layerCropRect.left &&
875 parentRoundedCorner.cropRect.top > layerCropRect.top &&
876 parentRoundedCorner.cropRect.right < layerCropRect.right &&
877 parentRoundedCorner.cropRect.bottom < layerCropRect.bottom) {
878 snapshot.roundedCorner = parentRoundedCorner;
879 } else {
880 snapshot.roundedCorner = layerSettings;
881 }
882 } else if (layerSettingsValid) {
883 snapshot.roundedCorner = layerSettings;
884 } else if (parentRoundedCornerValid) {
885 snapshot.roundedCorner = parentRoundedCorner;
886 }
887}
888
889void LayerSnapshotBuilder::updateLayerBounds(LayerSnapshot& snapshot,
890 const RequestedLayerState& requested,
891 const LayerSnapshot& parentSnapshot,
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700892 uint32_t primaryDisplayRotationFlags) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000893 snapshot.croppedBufferSize = requested.getCroppedBufferSize(snapshot.bufferSize);
894 snapshot.geomCrop = requested.crop;
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700895 snapshot.localTransform = requested.getTransform(primaryDisplayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000896 snapshot.localTransformInverse = snapshot.localTransform.inverse();
897 snapshot.geomLayerTransform = parentSnapshot.geomLayerTransform * snapshot.localTransform;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000898 const bool transformWasInvalid = snapshot.invalidTransform;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000899 snapshot.invalidTransform = !LayerSnapshot::isTransformValid(snapshot.geomLayerTransform);
900 if (snapshot.invalidTransform) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000901 auto& t = snapshot.geomLayerTransform;
902 auto& requestedT = requested.requestedTransform;
903 std::string transformDebug =
904 base::StringPrintf(" transform={%f,%f,%f,%f} requestedTransform={%f,%f,%f,%f}",
905 t.dsdx(), t.dsdy(), t.dtdx(), t.dtdy(), requestedT.dsdx(),
906 requestedT.dsdy(), requestedT.dtdx(), requestedT.dtdy());
907 std::string bufferDebug;
908 if (requested.externalTexture) {
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700909 auto unRotBuffer = requested.getUnrotatedBufferSize(primaryDisplayRotationFlags);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000910 auto& destFrame = requested.destinationFrame;
911 bufferDebug = base::StringPrintf(" buffer={%d,%d} displayRot=%d"
912 " destFrame={%d,%d,%d,%d} unRotBuffer={%d,%d}",
913 requested.externalTexture->getWidth(),
914 requested.externalTexture->getHeight(),
Vishnu Nairb76d99a2023-03-19 18:22:31 -0700915 primaryDisplayRotationFlags, destFrame.left,
916 destFrame.top, destFrame.right, destFrame.bottom,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000917 unRotBuffer.getHeight(), unRotBuffer.getWidth());
918 }
919 ALOGW("Resetting transform for %s because it is invalid.%s%s",
920 snapshot.getDebugString().c_str(), transformDebug.c_str(), bufferDebug.c_str());
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000921 snapshot.geomLayerTransform.reset();
922 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000923 if (transformWasInvalid != snapshot.invalidTransform) {
924 // If transform is invalid, the layer will be hidden.
925 mResortSnapshots = true;
926 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000927 snapshot.geomInverseLayerTransform = snapshot.geomLayerTransform.inverse();
928
929 FloatRect parentBounds = parentSnapshot.geomLayerBounds;
930 parentBounds = snapshot.localTransform.inverse().transform(parentBounds);
931 snapshot.geomLayerBounds =
932 (requested.externalTexture) ? snapshot.bufferSize.toFloatRect() : parentBounds;
933 if (!requested.crop.isEmpty()) {
934 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(requested.crop.toFloatRect());
935 }
936 snapshot.geomLayerBounds = snapshot.geomLayerBounds.intersect(parentBounds);
937 snapshot.transformedBounds = snapshot.geomLayerTransform.transform(snapshot.geomLayerBounds);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000938 const Rect geomLayerBoundsWithoutTransparentRegion =
939 RequestedLayerState::reduce(Rect(snapshot.geomLayerBounds),
940 requested.transparentRegion);
941 snapshot.transformedBoundsWithoutTransparentRegion =
942 snapshot.geomLayerTransform.transform(geomLayerBoundsWithoutTransparentRegion);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000943 snapshot.parentTransform = parentSnapshot.geomLayerTransform;
944
945 // Subtract the transparent region and snap to the bounds
Vishnu Naircfb2d252023-01-19 04:44:02 +0000946 const Rect bounds =
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000947 RequestedLayerState::reduce(snapshot.croppedBufferSize, requested.transparentRegion);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000948 if (requested.potentialCursor) {
949 snapshot.cursorFrame = snapshot.geomLayerTransform.transform(bounds);
950 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000951}
952
953void LayerSnapshotBuilder::updateShadows(LayerSnapshot& snapshot,
954 const RequestedLayerState& requested,
955 const renderengine::ShadowSettings& globalShadowSettings) {
956 snapshot.shadowRadius = requested.shadowRadius;
957 snapshot.shadowSettings.length = requested.shadowRadius;
958 if (snapshot.shadowRadius > 0.f) {
959 snapshot.shadowSettings = globalShadowSettings;
960
961 // Note: this preserves existing behavior of shadowing the entire layer and not cropping
962 // it if transparent regions are present. This may not be necessary since shadows are
963 // typically cast by layers without transparent regions.
964 snapshot.shadowSettings.boundaries = snapshot.geomLayerBounds;
965
966 // If the casting layer is translucent, we need to fill in the shadow underneath the
967 // layer. Otherwise the generated shadow will only be shown around the casting layer.
968 snapshot.shadowSettings.casterIsTranslucent =
969 !snapshot.isContentOpaque() || (snapshot.alpha < 1.0f);
970 snapshot.shadowSettings.ambientColor *= snapshot.alpha;
971 snapshot.shadowSettings.spotColor *= snapshot.alpha;
972 }
973}
974
975void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot,
976 const RequestedLayerState& requested,
977 const LayerSnapshot& parentSnapshot,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000978 const LayerHierarchy::TraversalPath& path,
979 const Args& args) {
980 if (requested.windowInfoHandle) {
981 snapshot.inputInfo = *requested.windowInfoHandle->getInfo();
982 } else {
983 snapshot.inputInfo = {};
Vishnu Nair40d02282023-02-28 21:11:40 +0000984 // b/271132344 revisit this and see if we can always use the layers uid/pid
985 snapshot.inputInfo.name = requested.name;
986 snapshot.inputInfo.ownerUid = static_cast<int32_t>(requested.ownerUid);
987 snapshot.inputInfo.ownerPid = requested.ownerPid;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000988 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000989
Vishnu Nair93b8b792023-02-27 19:40:24 +0000990 snapshot.inputInfo.id = static_cast<int32_t>(snapshot.uniqueSequence);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000991 snapshot.inputInfo.displayId = static_cast<int32_t>(snapshot.outputFilter.layerStack.id);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000992 if (!needsInputInfo(snapshot, requested)) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000993 return;
994 }
995
Vishnu Naircfb2d252023-01-19 04:44:02 +0000996 static frontend::DisplayInfo sDefaultInfo = {.isSecure = false};
997 const std::optional<frontend::DisplayInfo> displayInfoOpt =
998 args.displays.get(snapshot.outputFilter.layerStack);
999 bool noValidDisplay = !displayInfoOpt.has_value();
1000 auto displayInfo = displayInfoOpt.value_or(sDefaultInfo);
1001
1002 if (!requested.windowInfoHandle) {
1003 snapshot.inputInfo.inputConfig = gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL;
1004 }
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001005 fillInputFrameInfo(snapshot.inputInfo, displayInfo.transform, snapshot);
1006
1007 if (noValidDisplay) {
1008 // Do not let the window receive touches if it is not associated with a valid display
1009 // transform. We still allow the window to receive keys and prevent ANRs.
1010 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::NOT_TOUCHABLE;
1011 }
1012
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001013 snapshot.inputInfo.alpha = snapshot.color.a;
Vishnu Nair40d02282023-02-28 21:11:40 +00001014 snapshot.inputInfo.touchOcclusionMode = requested.hasInputInfo()
1015 ? requested.windowInfoHandle->getInfo()->touchOcclusionMode
1016 : parentSnapshot.inputInfo.touchOcclusionMode;
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001017 if (requested.dropInputMode == gui::DropInputMode::ALL ||
1018 parentSnapshot.dropInputMode == gui::DropInputMode::ALL) {
1019 snapshot.dropInputMode = gui::DropInputMode::ALL;
1020 } else if (requested.dropInputMode == gui::DropInputMode::OBSCURED ||
1021 parentSnapshot.dropInputMode == gui::DropInputMode::OBSCURED) {
1022 snapshot.dropInputMode = gui::DropInputMode::OBSCURED;
1023 } else {
1024 snapshot.dropInputMode = gui::DropInputMode::NONE;
1025 }
1026
1027 handleDropInputMode(snapshot, parentSnapshot);
1028
1029 // If the window will be blacked out on a display because the display does not have the secure
1030 // flag and the layer has the secure flag set, then drop input.
1031 if (!displayInfo.isSecure && snapshot.isSecure) {
1032 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::DROP_INPUT;
1033 }
1034
1035 auto cropLayerSnapshot = getSnapshot(requested.touchCropId);
1036 if (snapshot.inputInfo.replaceTouchableRegionWithCrop) {
Vishnu Nairfed7c122023-03-18 01:54:43 +00001037 Rect inputBoundsInDisplaySpace;
1038 if (!cropLayerSnapshot) {
1039 FloatRect inputBounds = getInputBounds(snapshot, /*fillParentBounds=*/true).first;
1040 inputBoundsInDisplaySpace =
1041 getInputBoundsInDisplaySpace(snapshot, inputBounds, displayInfo.transform);
1042 } else {
1043 FloatRect inputBounds =
1044 getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1045 inputBoundsInDisplaySpace =
1046 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1047 displayInfo.transform);
1048 }
1049 snapshot.inputInfo.touchableRegion = Region(inputBoundsInDisplaySpace);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001050 } else if (cropLayerSnapshot) {
Vishnu Nairfed7c122023-03-18 01:54:43 +00001051 FloatRect inputBounds = getInputBounds(*cropLayerSnapshot, /*fillParentBounds=*/true).first;
1052 Rect inputBoundsInDisplaySpace =
1053 getInputBoundsInDisplaySpace(*cropLayerSnapshot, inputBounds,
1054 displayInfo.transform);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001055 snapshot.inputInfo.touchableRegion = snapshot.inputInfo.touchableRegion.intersect(
Vishnu Nairfed7c122023-03-18 01:54:43 +00001056 displayInfo.transform.transform(inputBoundsInDisplaySpace));
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001057 }
1058
1059 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
1060 // if it was set by WM for a known system overlay
1061 if (snapshot.isTrustedOverlay) {
1062 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::TRUSTED_OVERLAY;
1063 }
1064
1065 // If the layer is a clone, we need to crop the input region to cloned root to prevent
1066 // touches from going outside the cloned area.
1067 if (path.isClone()) {
1068 snapshot.inputInfo.inputConfig |= gui::WindowInfo::InputConfig::CLONE;
Vishnu Nair92990e22023-02-24 20:01:05 +00001069 auto clonedRootSnapshot = getSnapshot(snapshot.mirrorRootPath);
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001070 if (clonedRootSnapshot) {
1071 const Rect rect =
1072 displayInfo.transform.transform(Rect{clonedRootSnapshot->transformedBounds});
1073 snapshot.inputInfo.touchableRegion = snapshot.inputInfo.touchableRegion.intersect(rect);
1074 }
1075 }
1076}
1077
1078std::vector<std::unique_ptr<LayerSnapshot>>& LayerSnapshotBuilder::getSnapshots() {
1079 return mSnapshots;
1080}
1081
Vishnu Naircfb2d252023-01-19 04:44:02 +00001082void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) const {
1083 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1084 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1085 if (!snapshot.isVisible) continue;
1086 visitor(snapshot);
1087 }
1088}
1089
Vishnu Nair3af0ec02023-02-10 04:13:48 +00001090// Visit each visible snapshot in z-order
1091void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
1092 const LayerHierarchy& root) const {
1093 root.traverseInZOrder(
1094 [this, visitor](const LayerHierarchy&,
1095 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
1096 LayerSnapshot* snapshot = getSnapshot(traversalPath);
1097 if (snapshot && snapshot->isVisible) {
1098 visitor(*snapshot);
1099 }
1100 return true;
1101 });
1102}
1103
Vishnu Naircfb2d252023-01-19 04:44:02 +00001104void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
1105 for (int i = 0; i < mNumInterestingSnapshots; i++) {
1106 std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
1107 if (!snapshot->isVisible) continue;
1108 visitor(snapshot);
1109 }
1110}
1111
1112void LayerSnapshotBuilder::forEachInputSnapshot(const ConstVisitor& visitor) const {
1113 for (int i = mNumInterestingSnapshots - 1; i >= 0; i--) {
1114 LayerSnapshot& snapshot = *mSnapshots[(size_t)i];
1115 if (!snapshot.hasInputInfo()) continue;
1116 visitor(snapshot);
1117 }
1118}
1119
Vishnu Nair8fc721b2022-12-22 20:06:32 +00001120} // namespace android::surfaceflinger::frontend