style/servo/
restyle_damage.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! The restyle damage is a hint that tells layout which kind of operations may
6//! be needed in presence of incremental style changes.
7
8use bitflags::Flags;
9
10use crate::computed_values::isolation::T as Isolation;
11use crate::computed_values::mix_blend_mode::T as MixBlendMode;
12use crate::computed_values::transform_style::T as TransformStyle;
13use crate::dom::TElement;
14use crate::matching::{StyleChange, StyleDifference};
15use crate::properties::{
16    restyle_damage_rebuild_box, restyle_damage_rebuild_stacking_context,
17    restyle_damage_recalculate_overflow, restyle_damage_repaint, style_structs, ComputedValues,
18};
19use crate::values::computed::basic_shape::ClipPath;
20use crate::values::computed::Perspective;
21use crate::values::generics::transform::{GenericRotate, GenericScale, GenericTranslate};
22use std::fmt;
23
24bitflags! {
25    /// Major phases of layout that need to be run due to the damage to a node during restyling. In
26    /// addition to the 4 bytes used for that, the rest of the `u16` is exposed as an extension point
27    /// for users of the crate to add their own custom types of damage that correspond to the
28    /// layout system they are implementing.
29    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
30    pub struct ServoRestyleDamage: u16 {
31        /// Repaint the node itself.
32        ///
33        /// Propagates both up and down the flow tree.
34        const REPAINT = 0b0001;
35
36        /// Rebuilds the stacking contexts.
37        ///
38        /// Propagates both up and down the flow tree.
39        const REBUILD_STACKING_CONTEXT = 0b0011;
40
41        /// Recalculates the scrollable overflow.
42        ///
43        /// Propagates both up and down the flow tree.
44        const RECALCULATE_OVERFLOW = 0b0111;
45
46        /// Any other type of damage, which requires running layout again.
47        ///
48        /// Propagates both up and down the flow tree.
49        const RELAYOUT = 0b1111;
50    }
51}
52
53malloc_size_of_is_0!(ServoRestyleDamage);
54
55impl ServoRestyleDamage {
56    /// Compute the `StyleDifference` (including the appropriate restyle damage)
57    /// for a given style change between `old` and `new`.
58    pub fn compute_style_difference<E: TElement>(
59        old: &ComputedValues,
60        new: &ComputedValues,
61    ) -> StyleDifference {
62        let mut damage = if std::ptr::eq(old, new) {
63            ServoRestyleDamage::empty()
64        } else {
65            compute_damage(old, new)
66        };
67
68        if damage.is_empty() {
69            return StyleDifference {
70                damage,
71                change: StyleChange::Unchanged,
72            };
73        }
74
75        if damage.contains(ServoRestyleDamage::RELAYOUT) {
76            damage |= E::compute_layout_damage(old, new);
77        }
78
79        // FIXME(emilio): Differentiate between reset and inherited
80        // properties here, and set `reset_only` appropriately so the
81        // optimization to skip the cascade in those cases applies.
82        let change = StyleChange::Changed { reset_only: false };
83
84        StyleDifference { damage, change }
85    }
86
87    /// Returns a bitmask indicating that the frame needs to be reconstructed.
88    pub fn reconstruct() -> ServoRestyleDamage {
89        // There's no way of knowing what kind of damage system the embedder will use, but part of
90        // this interface is that a fully saturated restyle damage means to rebuild everything.
91        ServoRestyleDamage::from_bits_retain(<ServoRestyleDamage as Flags>::Bits::MAX)
92    }
93}
94
95impl Default for ServoRestyleDamage {
96    fn default() -> Self {
97        Self::empty()
98    }
99}
100
101impl fmt::Display for ServoRestyleDamage {
102    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
103        let mut first_elem = true;
104
105        let to_iter = [
106            (ServoRestyleDamage::REPAINT, "Repaint"),
107            (
108                ServoRestyleDamage::REBUILD_STACKING_CONTEXT,
109                "Rebuild stacking context",
110            ),
111            (
112                ServoRestyleDamage::RECALCULATE_OVERFLOW,
113                "Recalculate overflow",
114            ),
115            (ServoRestyleDamage::RELAYOUT, "Relayout"),
116        ];
117
118        for &(damage, damage_str) in &to_iter {
119            if self.contains(damage) {
120                if !first_elem {
121                    write!(f, " | ")?;
122                }
123                write!(f, "{}", damage_str)?;
124                first_elem = false;
125            }
126        }
127
128        if first_elem {
129            write!(f, "NoDamage")?;
130        }
131
132        Ok(())
133    }
134}
135
136fn augmented_restyle_damage_rebuild_box(old: &ComputedValues, new: &ComputedValues) -> bool {
137    let old_box = old.get_box();
138    let new_box = new.get_box();
139    restyle_damage_rebuild_box(old, new)
140        || old_box.original_display != new_box.original_display
141        || old_box.has_transform_or_perspective() != new_box.has_transform_or_perspective()
142        || old.get_effects().filter.0.is_empty() != new.get_effects().filter.0.is_empty()
143}
144
145fn augmented_restyle_damage_rebuild_stacking_context(
146    old: &ComputedValues,
147    new: &ComputedValues,
148) -> bool {
149    restyle_damage_rebuild_stacking_context(old, new)
150        || old.guarantees_stacking_context() != new.guarantees_stacking_context()
151}
152fn compute_damage(old: &ComputedValues, new: &ComputedValues) -> ServoRestyleDamage {
153    let mut damage = ServoRestyleDamage::empty();
154
155    // Damage flags higher up the if-else chain imply damage flags lower down the if-else chain,
156    // so we can skip the diffing process for later flags if an earlier flag is true
157    if augmented_restyle_damage_rebuild_box(old, new) {
158        damage.insert(ServoRestyleDamage::RELAYOUT)
159    } else if restyle_damage_recalculate_overflow(old, new) {
160        damage.insert(ServoRestyleDamage::RECALCULATE_OVERFLOW)
161    } else if augmented_restyle_damage_rebuild_stacking_context(old, new) {
162        damage.insert(ServoRestyleDamage::REBUILD_STACKING_CONTEXT);
163    } else if restyle_damage_repaint(old, new) {
164        damage.insert(ServoRestyleDamage::REPAINT);
165    }
166    // Paint worklets may depend on custom properties, so if they have changed we should repaint.
167    else if !old.custom_properties_equal(new) {
168        damage.insert(ServoRestyleDamage::REPAINT);
169    }
170
171    damage
172}
173
174impl ComputedValues {
175    /// Some properties establish a stacking context when they are set to a non-initial value.
176    /// In that case, the damage is only set to `ServoRestyleDamage::REPAINT` because we don't
177    /// need to rebuild stacking contexts when the style changes between different non-initial
178    /// values. This function checks whether any of these properties is set to a value that
179    /// guarantees a stacking context, so that we only do the work when this changes.
180    /// Note that it's still possible to establish a stacking context when this returns false.
181    pub fn guarantees_stacking_context(&self) -> bool {
182        self.get_effects().opacity != 1.0
183            || self.get_effects().mix_blend_mode != MixBlendMode::Normal
184            || self.get_svg().clip_path != ClipPath::None
185            || self.get_box().isolation == Isolation::Isolate
186    }
187}
188
189impl style_structs::Box {
190    /// Whether there is a non-default transform or perspective style set
191    pub fn has_transform_or_perspective(&self) -> bool {
192        !self.transform.0.is_empty()
193            || self.scale != GenericScale::None
194            || self.rotate != GenericRotate::None
195            || self.translate != GenericTranslate::None
196            || self.perspective != Perspective::None
197            || self.transform_style == TransformStyle::Preserve3d
198    }
199}