| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/flow/layers/container_layer.h" |
| 6 | |
| 7 | #include <optional> |
| 8 | |
| 9 | namespace flutter { |
| 10 | |
| 11 | ContainerLayer::ContainerLayer() : child_paint_bounds_(SkRect::MakeEmpty()) {} |
| 12 | |
| 13 | void ContainerLayer::Diff(DiffContext* context, const Layer* old_layer) { |
| 14 | auto old_container = static_cast<const ContainerLayer*>(old_layer); |
| 15 | DiffContext::AutoSubtreeRestore subtree(context); |
| 16 | DiffChildren(context, old_layer: old_container); |
| 17 | context->SetLayerPaintRegion(layer: this, region: context->CurrentSubtreeRegion()); |
| 18 | } |
| 19 | |
| 20 | void ContainerLayer::PreservePaintRegion(DiffContext* context) { |
| 21 | Layer::PreservePaintRegion(context); |
| 22 | for (auto& layer : layers_) { |
| 23 | layer->PreservePaintRegion(context); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void ContainerLayer::DiffChildren(DiffContext* context, |
| 28 | const ContainerLayer* old_layer) { |
| 29 | if (context->IsSubtreeDirty()) { |
| 30 | for (auto& layer : layers_) { |
| 31 | layer->Diff(context, old_layer: nullptr); |
| 32 | } |
| 33 | return; |
| 34 | } |
| 35 | FML_DCHECK(old_layer); |
| 36 | |
| 37 | const auto& prev_layers = old_layer->layers_; |
| 38 | |
| 39 | // first mismatched element |
| 40 | int new_children_top = 0; |
| 41 | int old_children_top = 0; |
| 42 | |
| 43 | // last mismatched element |
| 44 | int new_children_bottom = layers_.size() - 1; |
| 45 | int old_children_bottom = prev_layers.size() - 1; |
| 46 | |
| 47 | while ((old_children_top <= old_children_bottom) && |
| 48 | (new_children_top <= new_children_bottom)) { |
| 49 | if (!layers_[new_children_top]->IsReplacing( |
| 50 | context, old_layer: prev_layers[old_children_top].get())) { |
| 51 | break; |
| 52 | } |
| 53 | ++new_children_top; |
| 54 | ++old_children_top; |
| 55 | } |
| 56 | |
| 57 | while ((old_children_top <= old_children_bottom) && |
| 58 | (new_children_top <= new_children_bottom)) { |
| 59 | if (!layers_[new_children_bottom]->IsReplacing( |
| 60 | context, old_layer: prev_layers[old_children_bottom].get())) { |
| 61 | break; |
| 62 | } |
| 63 | --new_children_bottom; |
| 64 | --old_children_bottom; |
| 65 | } |
| 66 | |
| 67 | // old layers that don't match |
| 68 | for (int i = old_children_top; i <= old_children_bottom; ++i) { |
| 69 | auto layer = prev_layers[i]; |
| 70 | context->AddDamage(damage: context->GetOldLayerPaintRegion(layer: layer.get())); |
| 71 | } |
| 72 | |
| 73 | for (int i = 0; i < static_cast<int>(layers_.size()); ++i) { |
| 74 | if (i < new_children_top || i > new_children_bottom) { |
| 75 | int i_prev = |
| 76 | i < new_children_top ? i : prev_layers.size() - (layers_.size() - i); |
| 77 | auto layer = layers_[i]; |
| 78 | auto prev_layer = prev_layers[i_prev]; |
| 79 | auto paint_region = context->GetOldLayerPaintRegion(layer: prev_layer.get()); |
| 80 | if (layer == prev_layer && !paint_region.has_readback() && |
| 81 | !paint_region.has_texture()) { |
| 82 | // for retained layers, stop processing the subtree and add existing |
| 83 | // region; We know current subtree is not dirty (every ancestor up to |
| 84 | // here matches) so the retained subtree will render identically to |
| 85 | // previous frame; We can only do this if there is no readback in the |
| 86 | // subtree. Layers that do readback must be able to register readback |
| 87 | // inside Diff |
| 88 | context->AddExistingPaintRegion(region: paint_region); |
| 89 | |
| 90 | // While we don't need to diff retained layers, we still need to |
| 91 | // associate their paint region with current layer tree so that we can |
| 92 | // retrieve it in next frame diff |
| 93 | layer->PreservePaintRegion(context); |
| 94 | } else { |
| 95 | layer->Diff(context, old_layer: prev_layer.get()); |
| 96 | } |
| 97 | } else { |
| 98 | DiffContext::AutoSubtreeRestore subtree(context); |
| 99 | context->MarkSubtreeDirty(); |
| 100 | auto layer = layers_[i]; |
| 101 | layer->Diff(context, old_layer: nullptr); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void ContainerLayer::Add(std::shared_ptr<Layer> layer) { |
| 107 | layers_.emplace_back(args: std::move(layer)); |
| 108 | } |
| 109 | |
| 110 | void ContainerLayer::Preroll(PrerollContext* context) { |
| 111 | SkRect child_paint_bounds = SkRect::MakeEmpty(); |
| 112 | PrerollChildren(context, child_paint_bounds: &child_paint_bounds); |
| 113 | set_paint_bounds(child_paint_bounds); |
| 114 | } |
| 115 | |
| 116 | void ContainerLayer::Paint(PaintContext& context) const { |
| 117 | FML_DCHECK(needs_painting(context)); |
| 118 | |
| 119 | PaintChildren(context); |
| 120 | } |
| 121 | |
| 122 | static bool safe_intersection_test(const SkRect* rect1, const SkRect& rect2) { |
| 123 | if (rect1->isEmpty() || rect2.isEmpty()) { |
| 124 | return false; |
| 125 | } |
| 126 | return rect1->intersects(r: rect2); |
| 127 | } |
| 128 | |
| 129 | void ContainerLayer::PrerollChildren(PrerollContext* context, |
| 130 | SkRect* child_paint_bounds) { |
| 131 | // Platform views have no children, so context->has_platform_view should |
| 132 | // always be false. |
| 133 | FML_DCHECK(!context->has_platform_view); |
| 134 | FML_DCHECK(!context->has_texture_layer); |
| 135 | |
| 136 | bool child_has_platform_view = false; |
| 137 | bool child_has_texture_layer = false; |
| 138 | bool all_renderable_state_flags = LayerStateStack::kCallerCanApplyAnything; |
| 139 | |
| 140 | for (auto& layer : layers_) { |
| 141 | // Reset context->has_platform_view and context->has_texture_layer to false |
| 142 | // so that layers aren't treated as if they have a platform view or texture |
| 143 | // layer based on one being previously found in a sibling tree. |
| 144 | context->has_platform_view = false; |
| 145 | context->has_texture_layer = false; |
| 146 | |
| 147 | // Initialize the renderable state flags to false to force the layer to |
| 148 | // opt-in to applying state attributes during its |Preroll| |
| 149 | context->renderable_state_flags = 0; |
| 150 | |
| 151 | layer->Preroll(context); |
| 152 | |
| 153 | all_renderable_state_flags &= context->renderable_state_flags; |
| 154 | if (safe_intersection_test(rect1: child_paint_bounds, rect2: layer->paint_bounds())) { |
| 155 | // This will allow inheritance by a linear sequence of non-overlapping |
| 156 | // children, but will fail with a grid or other arbitrary 2D layout. |
| 157 | // See https://github.com/flutter/flutter/issues/93899 |
| 158 | all_renderable_state_flags = 0; |
| 159 | } |
| 160 | child_paint_bounds->join(r: layer->paint_bounds()); |
| 161 | |
| 162 | child_has_platform_view = |
| 163 | child_has_platform_view || context->has_platform_view; |
| 164 | child_has_texture_layer = |
| 165 | child_has_texture_layer || context->has_texture_layer; |
| 166 | } |
| 167 | |
| 168 | context->has_platform_view = child_has_platform_view; |
| 169 | context->has_texture_layer = child_has_texture_layer; |
| 170 | context->renderable_state_flags = all_renderable_state_flags; |
| 171 | set_subtree_has_platform_view(child_has_platform_view); |
| 172 | set_children_renderable_state_flags(all_renderable_state_flags); |
| 173 | set_child_paint_bounds(*child_paint_bounds); |
| 174 | } |
| 175 | |
| 176 | void ContainerLayer::PaintChildren(PaintContext& context) const { |
| 177 | // We can no longer call FML_DCHECK here on the needs_painting(context) |
| 178 | // condition as that test is only valid for the PaintContext that |
| 179 | // is initially handed to a layer's Paint() method. By the time the |
| 180 | // layer calls PaintChildren(), though, it may have modified the |
| 181 | // PaintContext so the test doesn't work in this "context". |
| 182 | |
| 183 | // Apply any outstanding state that the children cannot individually |
| 184 | // and collectively handle. |
| 185 | auto restore = context.state_stack.applyState( |
| 186 | bounds: child_paint_bounds(), can_apply_flags: children_renderable_state_flags()); |
| 187 | |
| 188 | // Intentionally not tracing here as there should be no self-time |
| 189 | // and the trace event on this common function has a small overhead. |
| 190 | for (auto& layer : layers_) { |
| 191 | if (layer->needs_painting(context)) { |
| 192 | layer->Paint(context); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } // namespace flutter |
| 198 | |