Skip to main content

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