1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! The restyle damage is a hint that tells layout which kind of operations may
//! be needed in presence of incremental style changes.

use crate::computed_values::display::T as Display;
use crate::matching::{StyleChange, StyleDifference};
use crate::properties::ComputedValues;
use std::fmt;

bitflags! {
    /// Individual layout actions that may be necessary after restyling.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub struct ServoRestyleDamage: u8 {
        /// Repaint the node itself.
        ///
        /// Currently unused; need to decide how this propagates.
        const REPAINT = 0x01;

        /// The stacking-context-relative position of this node or its
        /// descendants has changed.
        ///
        /// Propagates both up and down the flow tree.
        const REPOSITION = 0x02;

        /// Recompute the overflow regions (bounding box of object and all descendants).
        ///
        /// Propagates down the flow tree because the computation is bottom-up.
        const STORE_OVERFLOW = 0x04;

        /// Recompute intrinsic inline_sizes (minimum and preferred).
        ///
        /// Propagates down the flow tree because the computation is.
        /// bottom-up.
        const BUBBLE_ISIZES = 0x08;

        /// Recompute actual inline-sizes and block-sizes, only taking
        /// out-of-flow children into account.
        ///
        /// Propagates up the flow tree because the computation is top-down.
        const REFLOW_OUT_OF_FLOW = 0x10;

        /// Recompute actual inline_sizes and block_sizes.
        ///
        /// Propagates up the flow tree because the computation is top-down.
        const REFLOW = 0x20;

        /// Re-resolve generated content.
        ///
        /// Propagates up the flow tree because the computation is inorder.
        const RESOLVE_GENERATED_CONTENT = 0x40;

        /// The entire flow needs to be reconstructed.
        const RECONSTRUCT_FLOW = 0x80;
    }
}

malloc_size_of_is_0!(ServoRestyleDamage);

impl ServoRestyleDamage {
    /// Compute the `StyleDifference` (including the appropriate restyle damage)
    /// for a given style change between `old` and `new`.
    pub fn compute_style_difference(old: &ComputedValues, new: &ComputedValues) -> StyleDifference {
        let damage = compute_damage(old, new);
        let change = if damage.is_empty() {
            StyleChange::Unchanged
        } else {
            // FIXME(emilio): Differentiate between reset and inherited
            // properties here, and set `reset_only` appropriately so the
            // optimization to skip the cascade in those cases applies.
            StyleChange::Changed { reset_only: false }
        };
        StyleDifference { damage, change }
    }

    /// Returns a bitmask that represents a flow that needs to be rebuilt and
    /// reflowed.
    ///
    /// FIXME(bholley): Do we ever actually need this? Shouldn't
    /// RECONSTRUCT_FLOW imply everything else?
    pub fn rebuild_and_reflow() -> ServoRestyleDamage {
        ServoRestyleDamage::REPAINT |
            ServoRestyleDamage::REPOSITION |
            ServoRestyleDamage::STORE_OVERFLOW |
            ServoRestyleDamage::BUBBLE_ISIZES |
            ServoRestyleDamage::REFLOW_OUT_OF_FLOW |
            ServoRestyleDamage::REFLOW |
            ServoRestyleDamage::RECONSTRUCT_FLOW
    }

    /// Returns a bitmask indicating that the frame needs to be reconstructed.
    pub fn reconstruct() -> ServoRestyleDamage {
        ServoRestyleDamage::RECONSTRUCT_FLOW
    }

    /// Supposing a flow has the given `position` property and this damage,
    /// returns the damage that we should add to the *parent* of this flow.
    pub fn damage_for_parent(self, child_is_absolutely_positioned: bool) -> ServoRestyleDamage {
        if child_is_absolutely_positioned {
            self & (ServoRestyleDamage::REPAINT |
                ServoRestyleDamage::REPOSITION |
                ServoRestyleDamage::STORE_OVERFLOW |
                ServoRestyleDamage::REFLOW_OUT_OF_FLOW |
                ServoRestyleDamage::RESOLVE_GENERATED_CONTENT)
        } else {
            self & (ServoRestyleDamage::REPAINT |
                ServoRestyleDamage::REPOSITION |
                ServoRestyleDamage::STORE_OVERFLOW |
                ServoRestyleDamage::REFLOW |
                ServoRestyleDamage::REFLOW_OUT_OF_FLOW |
                ServoRestyleDamage::RESOLVE_GENERATED_CONTENT)
        }
    }

    /// Supposing the *parent* of a flow with the given `position` property has
    /// this damage, returns the damage that we should add to this flow.
    pub fn damage_for_child(
        self,
        parent_is_absolutely_positioned: bool,
        child_is_absolutely_positioned: bool,
    ) -> ServoRestyleDamage {
        match (
            parent_is_absolutely_positioned,
            child_is_absolutely_positioned,
        ) {
            (false, true) => {
                // Absolute children are out-of-flow and therefore insulated from changes.
                //
                // FIXME(pcwalton): Au contraire, if the containing block dimensions change!
                self & (ServoRestyleDamage::REPAINT | ServoRestyleDamage::REPOSITION)
            },
            (true, false) => {
                // Changing the position of an absolutely-positioned block requires us to reflow
                // its kids.
                if self.contains(ServoRestyleDamage::REFLOW_OUT_OF_FLOW) {
                    self | ServoRestyleDamage::REFLOW
                } else {
                    self
                }
            },
            _ => {
                // TODO(pcwalton): Take floatedness into account.
                self & (ServoRestyleDamage::REPAINT |
                    ServoRestyleDamage::REPOSITION |
                    ServoRestyleDamage::REFLOW)
            },
        }
    }
}

impl Default for ServoRestyleDamage {
    fn default() -> Self {
        Self::empty()
    }
}

impl fmt::Display for ServoRestyleDamage {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        let mut first_elem = true;

        let to_iter = [
            (ServoRestyleDamage::REPAINT, "Repaint"),
            (ServoRestyleDamage::REPOSITION, "Reposition"),
            (ServoRestyleDamage::STORE_OVERFLOW, "StoreOverflow"),
            (ServoRestyleDamage::BUBBLE_ISIZES, "BubbleISizes"),
            (ServoRestyleDamage::REFLOW_OUT_OF_FLOW, "ReflowOutOfFlow"),
            (ServoRestyleDamage::REFLOW, "Reflow"),
            (
                ServoRestyleDamage::RESOLVE_GENERATED_CONTENT,
                "ResolveGeneratedContent",
            ),
            (ServoRestyleDamage::RECONSTRUCT_FLOW, "ReconstructFlow"),
        ];

        for &(damage, damage_str) in &to_iter {
            if self.contains(damage) {
                if !first_elem {
                    write!(f, " | ")?;
                }
                write!(f, "{}", damage_str)?;
                first_elem = false;
            }
        }

        if first_elem {
            write!(f, "NoDamage")?;
        }

        Ok(())
    }
}

fn compute_damage(old: &ComputedValues, new: &ComputedValues) -> ServoRestyleDamage {
    let mut damage = ServoRestyleDamage::empty();

    // This should check every CSS property, as enumerated in the fields of
    // https://doc.servo.org/style/properties/struct.ComputedValues.html

    // This uses short-circuiting boolean OR for its side effects and ignores the result.
    let _ = restyle_damage_rebuild_and_reflow!(
        old,
        new,
        damage,
        [
            ServoRestyleDamage::REPAINT,
            ServoRestyleDamage::REPOSITION,
            ServoRestyleDamage::STORE_OVERFLOW,
            ServoRestyleDamage::BUBBLE_ISIZES,
            ServoRestyleDamage::REFLOW_OUT_OF_FLOW,
            ServoRestyleDamage::REFLOW,
            ServoRestyleDamage::RECONSTRUCT_FLOW
        ],
        old.get_box().original_display != new.get_box().original_display
    ) || (new.get_box().display == Display::Inline &&
        restyle_damage_rebuild_and_reflow_inline!(
            old,
            new,
            damage,
            [
                ServoRestyleDamage::REPAINT,
                ServoRestyleDamage::REPOSITION,
                ServoRestyleDamage::STORE_OVERFLOW,
                ServoRestyleDamage::BUBBLE_ISIZES,
                ServoRestyleDamage::REFLOW_OUT_OF_FLOW,
                ServoRestyleDamage::REFLOW,
                ServoRestyleDamage::RECONSTRUCT_FLOW
            ]
        )) ||
        restyle_damage_reflow!(
            old,
            new,
            damage,
            [
                ServoRestyleDamage::REPAINT,
                ServoRestyleDamage::REPOSITION,
                ServoRestyleDamage::STORE_OVERFLOW,
                ServoRestyleDamage::BUBBLE_ISIZES,
                ServoRestyleDamage::REFLOW_OUT_OF_FLOW,
                ServoRestyleDamage::REFLOW
            ]
        ) ||
        restyle_damage_reflow_out_of_flow!(
            old,
            new,
            damage,
            [
                ServoRestyleDamage::REPAINT,
                ServoRestyleDamage::REPOSITION,
                ServoRestyleDamage::STORE_OVERFLOW,
                ServoRestyleDamage::REFLOW_OUT_OF_FLOW
            ]
        ) ||
        restyle_damage_repaint!(old, new, damage, [ServoRestyleDamage::REPAINT]);

    // Paint worklets may depend on custom properties,
    // so if they have changed we should repaint.
    if !old.custom_properties_equal(new) {
        damage.insert(ServoRestyleDamage::REPAINT);
    }

    // If the layer requirements of this flow have changed due to the value
    // of the transform, then reflow is required to rebuild the layers.
    if old.transform_requires_layer() != new.transform_requires_layer() {
        damage.insert(ServoRestyleDamage::rebuild_and_reflow());
    }

    damage
}