servo-layout 0.1.0

A component of the servo web-engine.
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
/* 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/. */

#![expect(unsafe_code)]

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt::Debug;
use std::process;
use std::rc::Rc;
use std::sync::{Arc, LazyLock};

use app_units::Au;
use bitflags::bitflags;
use embedder_traits::{Theme, ViewportDetails};
use euclid::{Point2D, Rect, Scale, Size2D};
use fonts::{FontContext, FontContextWebFontMethods, WebFontDocumentContext};
use fonts_traits::StylesheetWebFontLoadFinishedCallback;
use layout_api::wrapper_traits::LayoutNode;
use layout_api::{
    AxesOverflow, BoxAreaType, CSSPixelRectIterator, IFrameSizes, Layout, LayoutConfig,
    LayoutFactory, OffsetParentResponse, PhysicalSides, QueryMsg, ReflowGoal, ReflowPhasesRun,
    ReflowRequest, ReflowRequestRestyle, ReflowResult, ReflowStatistics, ScrollContainerQueryFlags,
    ScrollContainerResponse, TrustedNodeAddress, with_layout_state,
};
use log::{debug, error, warn};
use malloc_size_of::{MallocConditionalSizeOf, MallocSizeOf, MallocSizeOfOps};
use net_traits::image_cache::ImageCache;
use paint_api::CrossProcessPaintApi;
use paint_api::display_list::ScrollType;
use parking_lot::{Mutex, RwLock};
use profile_traits::mem::{Report, ReportKind};
use profile_traits::time::{
    self as profile_time, TimerMetadata, TimerMetadataFrameType, TimerMetadataReflowType,
};
use profile_traits::{path, time_profile};
use rustc_hash::FxHashMap;
use script::layout_dom::{ServoLayoutDocument, ServoLayoutElement, ServoLayoutNode};
use script_traits::{DrawAPaintImageResult, PaintWorkletError, Painter, ScriptThreadMessage};
use servo_arc::Arc as ServoArc;
use servo_base::generic_channel::GenericSender;
use servo_base::id::{PipelineId, WebViewId};
use servo_config::opts::{self, DiagnosticsLogging};
use servo_config::pref;
use servo_url::ServoUrl;
use style::animation::DocumentAnimationSet;
use style::context::{
    QuirksMode, RegisteredSpeculativePainter, RegisteredSpeculativePainters, SharedStyleContext,
};
use style::device::Device;
use style::device::servo::FontMetricsProvider;
use style::dom::{OpaqueNode, ShowSubtreeDataAndPrimaryValues, TElement, TNode};
use style::font_metrics::FontMetrics;
use style::global_style_data::GLOBAL_STYLE_DATA;
use style::invalidation::element::restyle_hints::RestyleHint;
use style::invalidation::stylesheets::StylesheetInvalidationSet;
use style::media_queries::{MediaList, MediaType};
use style::properties::style_structs::Font;
use style::properties::{ComputedValues, PropertyId};
use style::queries::values::PrefersColorScheme;
use style::selector_parser::{PseudoElement, RestyleDamage, SnapshotMap};
use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard, StylesheetGuards};
use style::stylesheets::{
    CustomMediaMap, DocumentStyleSheet, Origin, Stylesheet, StylesheetInDocument,
};
use style::stylist::Stylist;
use style::traversal::DomTraversal;
use style::traversal_flags::TraversalFlags;
use style::values::computed::font::GenericFontFamily;
use style::values::computed::{CSSPixelLength, FontSize, Length, NonNegativeLength, XLang};
use style::values::specified::font::{KeywordInfo, QueryFontMetricsFlags};
use style::{Zero, driver};
use style_traits::{CSSPixel, SpeculativePainter};
use stylo_atoms::Atom;
use url::Url;
use webrender_api::ExternalScrollId;
use webrender_api::units::{DevicePixel, LayoutVector2D};

use crate::context::{CachedImageOrError, ImageResolver, LayoutContext};
use crate::display_list::{DisplayListBuilder, HitTest, PaintTimingHandler, StackingContextTree};
use crate::query::{
    find_character_offset_in_fragment_descendants, get_the_text_steps, process_box_area_request,
    process_box_areas_request, process_client_rect_request, process_current_css_zoom_query,
    process_effective_overflow_query, process_node_scroll_area_request,
    process_offset_parent_query, process_padding_request, process_resolved_font_style_query,
    process_resolved_style_request, process_scroll_container_query,
};
use crate::traversal::{RecalcStyle, compute_damage_and_rebuild_box_tree};
use crate::{BoxTree, FragmentTree};

// This mutex is necessary due to syncronisation issues between two different types of thread-local storage
// which manifest themselves when the layout thread tries to layout iframes in parallel with the main page
//
// See: https://github.com/servo/servo/pull/29792
// And: https://gist.github.com/mukilan/ed57eb61b83237a05fbf6360ec5e33b0
static STYLE_THREAD_POOL: Mutex<&LazyLock<style::global_style_data::StyleThreadPool>> =
    Mutex::new(&style::global_style_data::STYLE_THREAD_POOL);

/// A CSS file to style the user agent stylesheet.
static USER_AGENT_CSS: &[u8] = include_bytes!("./stylesheets/user-agent.css");

/// A CSS file to style the Servo browser.
static SERVO_CSS: &[u8] = include_bytes!("./stylesheets/servo.css");

/// A CSS file to style the presentational hints.
static PRESENTATIONAL_HINTS_CSS: &[u8] = include_bytes!("./stylesheets/presentational-hints.css");

/// A CSS file to style the quirks mode.
static QUIRKS_MODE_CSS: &[u8] = include_bytes!("./stylesheets/quirks-mode.css");

/// Information needed by layout.
pub struct LayoutThread {
    /// The ID of the pipeline that we belong to.
    id: PipelineId,

    /// The webview that contains the pipeline we belong to.
    webview_id: WebViewId,

    /// The URL of the pipeline that we belong to.
    url: ServoUrl,

    /// Performs CSS selector matching and style resolution.
    stylist: Stylist,

    /// Is the current reflow of an iframe, as opposed to a root window?
    is_iframe: bool,

    /// The channel on which messages can be sent to the script thread.
    script_chan: GenericSender<ScriptThreadMessage>,

    /// The channel on which messages can be sent to the time profiler.
    time_profiler_chan: profile_time::ProfilerChan,

    /// Reference to the script thread image cache.
    image_cache: Arc<dyn ImageCache>,

    /// A FontContext to be used during layout.
    font_context: Arc<FontContext>,

    /// Whether or not user agent stylesheets have been added to the Stylist or not.
    have_added_user_agent_stylesheets: bool,

    // A vector of parsed `DocumentStyleSheet`s representing the corresponding `UserStyleSheet`s
    // associated with the `WebView` to which this `Layout` belongs. The `DocumentStylesheet`s might
    // be shared with `Layout`s in the same `ScriptThread`.
    user_stylesheets: Rc<Vec<DocumentStyleSheet>>,

    /// Whether or not this [`LayoutImpl`]'s [`Device`] has changed since the last restyle.
    /// If it has, a restyle is pending.
    device_has_changed: bool,

    /// Is this the first reflow in this LayoutThread?
    have_ever_generated_display_list: Cell<bool>,

    /// Whether a new overflow calculation needs to happen due to changes to the fragment
    /// tree. This is set to true every time a restyle requests overflow calculation.
    need_overflow_calculation: Cell<bool>,

    /// Whether a new display list is necessary due to changes to layout or stacking
    /// contexts. This is set to true every time layout changes, even when a display list
    /// isn't requested for this layout, such as for layout queries. The next time a
    /// layout requests a display list, it is produced unconditionally, even when the
    /// layout trees remain the same.
    need_new_display_list: Cell<bool>,

    /// Whether or not the existing stacking context tree is dirty and needs to be
    /// rebuilt. This happens after a relayout or overflow update. The reason that we
    /// don't simply clear the stacking context tree when it becomes dirty is that we need
    /// to preserve scroll offsets from the old tree to the new one.
    need_new_stacking_context_tree: Cell<bool>,

    /// The box tree.
    box_tree: RefCell<Option<Arc<BoxTree>>>,

    /// The fragment tree.
    fragment_tree: RefCell<Option<Rc<FragmentTree>>>,

    /// The [`StackingContextTree`] cached from previous layouts.
    stacking_context_tree: RefCell<Option<StackingContextTree>>,

    // A cache that maps image resources specified in CSS (e.g as the `url()` value
    // for `background-image` or `content` properties) to either the final resolved
    // image data, or an error if the image cache failed to load/decode the image.
    resolved_images_cache: Arc<RwLock<HashMap<ServoUrl, CachedImageOrError>>>,

    /// The executors for paint worklets.
    registered_painters: RegisteredPaintersImpl,

    /// Cross-process access to the `Paint` API.
    paint_api: CrossProcessPaintApi,

    /// Debug options, copied from configuration to this `LayoutThread` in order
    /// to avoid having to constantly access the thread-safe global options.
    debug: DiagnosticsLogging,

    /// Tracks the node that was highlighted by the devtools during the last reflow.
    ///
    /// If this changed, then we need to create a new display list.
    previously_highlighted_dom_node: Cell<Option<OpaqueNode>>,

    /// Handler for all Paint Timings
    paint_timing_handler: RefCell<Option<PaintTimingHandler>>,

    /// Whether accessibility is active in this layout.
    /// (Note: this is a temporary field which will be replaced with an optional accessibility tree member.)
    accessibility_active: Cell<bool>,
}

pub struct LayoutFactoryImpl();

impl LayoutFactory for LayoutFactoryImpl {
    fn create(&self, config: LayoutConfig) -> Box<dyn Layout> {
        Box::new(LayoutThread::new(config))
    }
}

impl Drop for LayoutThread {
    fn drop(&mut self) {
        let (keys, instance_keys) = self
            .font_context
            .collect_unused_webrender_resources(true /* all */);
        self.paint_api
            .remove_unused_font_resources(self.webview_id.into(), keys, instance_keys)
    }
}

impl Layout for LayoutThread {
    fn device(&self) -> &Device {
        self.stylist.device()
    }

    fn set_theme(&mut self, theme: Theme) -> bool {
        let theme: PrefersColorScheme = theme.into();
        let device = self.stylist.device_mut();
        if theme == device.color_scheme() {
            return false;
        }

        device.set_color_scheme(theme);
        self.device_has_changed = true;
        true
    }

    fn set_viewport_details(&mut self, viewport_details: ViewportDetails) -> bool {
        let device = self.stylist.device_mut();
        let device_pixel_ratio = Scale::new(viewport_details.hidpi_scale_factor.get());
        if device.viewport_size() == viewport_details.size &&
            device.device_pixel_ratio() == device_pixel_ratio
        {
            return false;
        }

        device.set_viewport_size(viewport_details.size);
        device.set_device_pixel_ratio(device_pixel_ratio);
        self.device_has_changed = true;
        true
    }

    fn load_web_fonts_from_stylesheet(
        &self,
        stylesheet: &ServoArc<Stylesheet>,
        document_context: &WebFontDocumentContext,
    ) {
        let guard = stylesheet.shared_lock.read();
        self.load_all_web_fonts_from_stylesheet_with_guard(
            &DocumentStyleSheet(stylesheet.clone()),
            &guard,
            document_context,
        );
    }

    #[servo_tracing::instrument(skip_all)]
    fn add_stylesheet(
        &mut self,
        stylesheet: ServoArc<Stylesheet>,
        before_stylesheet: Option<ServoArc<Stylesheet>>,
        document_context: &WebFontDocumentContext,
    ) {
        let guard = stylesheet.shared_lock.read();
        let stylesheet = DocumentStyleSheet(stylesheet.clone());
        self.load_all_web_fonts_from_stylesheet_with_guard(&stylesheet, &guard, document_context);

        match before_stylesheet {
            Some(insertion_point) => self.stylist.insert_stylesheet_before(
                stylesheet,
                DocumentStyleSheet(insertion_point),
                &guard,
            ),
            None => self.stylist.append_stylesheet(stylesheet, &guard),
        }
    }

    #[servo_tracing::instrument(skip_all)]
    fn remove_stylesheet(&mut self, stylesheet: ServoArc<Stylesheet>) {
        let guard = stylesheet.shared_lock.read();
        let stylesheet = DocumentStyleSheet(stylesheet.clone());
        self.stylist.remove_stylesheet(stylesheet.clone(), &guard);
        self.font_context
            .remove_all_web_fonts_from_stylesheet(&stylesheet);
    }

    #[servo_tracing::instrument(skip_all)]
    fn remove_cached_image(&mut self, url: &ServoUrl) {
        let mut resolved_images_cache = self.resolved_images_cache.write();
        resolved_images_cache.remove(url);
    }

    /// Return the resolved values of this node's padding rect.
    #[servo_tracing::instrument(skip_all)]
    fn query_padding(&self, node: TrustedNodeAddress) -> Option<PhysicalSides> {
        with_layout_state(|| {
            // If we have not built a fragment tree yet, there is no way we have layout information for
            // this query, which can be run without forcing a layout (for IntersectionObserver).
            if self.fragment_tree.borrow().is_none() {
                return None;
            }

            let node = unsafe { ServoLayoutNode::new(&node) };
            process_padding_request(node.to_threadsafe())
        })
    }

    /// Return the union of this node's areas in the coordinate space of the Document. This is used
    /// to implement `getBoundingClientRect()` and support many other API where the such query is
    /// required.
    ///
    /// Part of <https://drafts.csswg.org/cssom-view-1/#element-get-the-bounding-box>.
    #[servo_tracing::instrument(skip_all)]
    fn query_box_area(
        &self,
        node: TrustedNodeAddress,
        area: BoxAreaType,
        exclude_transform_and_inline: bool,
    ) -> Option<Rect<Au, CSSPixel>> {
        with_layout_state(|| {
            // If we have not built a fragment tree yet, there is no way we have layout information for
            // this query, which can be run without forcing a layout (for IntersectionObserver).
            if self.fragment_tree.borrow().is_none() {
                return None;
            }

            let node = unsafe { ServoLayoutNode::new(&node) };
            let stacking_context_tree = self.stacking_context_tree.borrow();
            let stacking_context_tree = stacking_context_tree
                .as_ref()
                .expect("Should always have a StackingContextTree for box area queries");
            process_box_area_request(
                stacking_context_tree,
                node.to_threadsafe(),
                area,
                exclude_transform_and_inline,
            )
        })
    }

    /// Get a `Vec` of bounding boxes of this node's `Fragment`s specific area in the coordinate space of
    /// the Document. This is used to implement `getClientRects()`.
    ///
    /// See <https://drafts.csswg.org/cssom-view/#dom-element-getclientrects>.
    #[servo_tracing::instrument(skip_all)]
    fn query_box_areas(&self, node: TrustedNodeAddress, area: BoxAreaType) -> CSSPixelRectIterator {
        with_layout_state(|| {
            // If we have not built a fragment tree yet, there is no way we have layout information for
            // this query, which can be run without forcing a layout (for IntersectionObserver).
            if self.fragment_tree.borrow().is_none() {
                return Box::new(std::iter::empty()) as CSSPixelRectIterator;
            }

            let node = unsafe { ServoLayoutNode::new(&node) };
            let stacking_context_tree = self.stacking_context_tree.borrow();
            let stacking_context_tree = stacking_context_tree
                .as_ref()
                .expect("Should always have a StackingContextTree for box area queries");
            process_box_areas_request(stacking_context_tree, node.to_threadsafe(), area)
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_client_rect(&self, node: TrustedNodeAddress) -> Rect<i32, CSSPixel> {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node) };
            process_client_rect_request(node.to_threadsafe())
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_current_css_zoom(&self, node: TrustedNodeAddress) -> f32 {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node) };
            process_current_css_zoom_query(node)
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_element_inner_outer_text(&self, node: layout_api::TrustedNodeAddress) -> String {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node) };
            get_the_text_steps(node)
        })
    }
    #[servo_tracing::instrument(skip_all)]
    fn query_offset_parent(&self, node: TrustedNodeAddress) -> OffsetParentResponse {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node) };
            let stacking_context_tree = self.stacking_context_tree.borrow();
            let stacking_context_tree = stacking_context_tree
                .as_ref()
                .expect("Should always have a StackingContextTree for offset parent queries");
            process_offset_parent_query(&stacking_context_tree.paint_info.scroll_tree, node)
                .unwrap_or_default()
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_scroll_container(
        &self,
        node: Option<TrustedNodeAddress>,
        flags: ScrollContainerQueryFlags,
    ) -> Option<ScrollContainerResponse> {
        with_layout_state(|| {
            let node = unsafe { node.as_ref().map(|node| ServoLayoutNode::new(node)) };
            let viewport_overflow = self
                .box_tree
                .borrow()
                .as_ref()
                .expect("Should have a BoxTree for all scroll container queries.")
                .viewport_overflow;
            process_scroll_container_query(node, flags, viewport_overflow)
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_resolved_style(
        &self,
        node: TrustedNodeAddress,
        pseudo: Option<PseudoElement>,
        property_id: PropertyId,
        animations: DocumentAnimationSet,
        animation_timeline_value: f64,
    ) -> String {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node) };
            let document = node.owner_doc();
            let document_shared_lock = document.style_shared_lock();
            let guards = StylesheetGuards {
                author: &document_shared_lock.read(),
                ua_or_user: &GLOBAL_STYLE_DATA.shared_lock.read(),
            };
            let snapshot_map = SnapshotMap::new();

            let shared_style_context = self.build_shared_style_context(
                guards,
                &snapshot_map,
                animation_timeline_value,
                &animations,
                TraversalFlags::empty(),
            );

            process_resolved_style_request(&shared_style_context, node, &pseudo, &property_id)
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_resolved_font_style(
        &self,
        node: TrustedNodeAddress,
        value: &str,
        animations: DocumentAnimationSet,
        animation_timeline_value: f64,
    ) -> Option<ServoArc<Font>> {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node) };
            let document = node.owner_doc();
            let document_shared_lock = document.style_shared_lock();
            let guards = StylesheetGuards {
                author: &document_shared_lock.read(),
                ua_or_user: &GLOBAL_STYLE_DATA.shared_lock.read(),
            };
            let snapshot_map = SnapshotMap::new();
            let shared_style_context = self.build_shared_style_context(
                guards,
                &snapshot_map,
                animation_timeline_value,
                &animations,
                TraversalFlags::empty(),
            );

            process_resolved_font_style_query(
                &shared_style_context,
                node,
                value,
                self.url.clone(),
                document_shared_lock,
            )
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_scrolling_area(&self, node: Option<TrustedNodeAddress>) -> Rect<i32, CSSPixel> {
        with_layout_state(|| {
            let node = node.map(|node| unsafe { ServoLayoutNode::new(&node).to_threadsafe() });
            process_node_scroll_area_request(node, self.fragment_tree.borrow().clone())
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_text_index(
        &self,
        node: TrustedNodeAddress,
        point_in_node: Point2D<Au, CSSPixel>,
    ) -> Option<usize> {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node).to_threadsafe() };
            let stacking_context_tree = self.stacking_context_tree.borrow_mut();
            let stacking_context_tree = stacking_context_tree.as_ref()?;
            find_character_offset_in_fragment_descendants(
                &node,
                stacking_context_tree,
                point_in_node,
            )
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_elements_from_point(
        &self,
        point: webrender_api::units::LayoutPoint,
        flags: layout_api::ElementsFromPointFlags,
    ) -> Vec<layout_api::ElementsFromPointResult> {
        with_layout_state(|| {
            self.stacking_context_tree
                .borrow_mut()
                .as_mut()
                .map(|tree| HitTest::run(tree, point, flags))
                .unwrap_or_default()
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn query_effective_overflow(&self, node: TrustedNodeAddress) -> Option<AxesOverflow> {
        with_layout_state(|| {
            let node = unsafe { ServoLayoutNode::new(&node).to_threadsafe() };
            process_effective_overflow_query(node)
        })
    }

    fn exit_now(&mut self) {}

    fn collect_reports(&self, reports: &mut Vec<Report>, ops: &mut MallocSizeOfOps) {
        // TODO: Measure more than just display list, stylist, and font context.
        let formatted_url = &format!("url({})", self.url);
        reports.push(Report {
            path: path![formatted_url, "layout-thread", "display-list"],
            kind: ReportKind::ExplicitJemallocHeapSize,
            size: 0,
        });

        reports.push(Report {
            path: path![formatted_url, "layout-thread", "stylist"],
            kind: ReportKind::ExplicitJemallocHeapSize,
            size: self.stylist.size_of(ops),
        });

        reports.push(Report {
            path: path![formatted_url, "layout-thread", "font-context"],
            kind: ReportKind::ExplicitJemallocHeapSize,
            size: self.font_context.conditional_size_of(ops),
        });

        reports.push(Report {
            path: path![formatted_url, "layout-thread", "box-tree"],
            kind: ReportKind::ExplicitJemallocHeapSize,
            size: self
                .box_tree
                .borrow()
                .as_ref()
                .map_or(0, |tree| tree.conditional_size_of(ops)),
        });

        reports.push(Report {
            path: path![formatted_url, "layout-thread", "fragment-tree"],
            kind: ReportKind::ExplicitJemallocHeapSize,
            size: self
                .fragment_tree
                .borrow()
                .as_ref()
                .map(|tree| tree.conditional_size_of(ops))
                .unwrap_or_default(),
        });

        reports.push(Report {
            path: path![formatted_url, "layout-thread", "stacking-context-tree"],
            kind: ReportKind::ExplicitJemallocHeapSize,
            size: self.stacking_context_tree.size_of(ops),
        });

        reports.extend(self.image_cache.memory_reports(formatted_url, ops));
    }

    fn set_quirks_mode(&mut self, quirks_mode: QuirksMode) {
        self.stylist.set_quirks_mode(quirks_mode);
    }

    fn reflow(&mut self, reflow_request: ReflowRequest) -> Option<ReflowResult> {
        time_profile!(
            profile_time::ProfilerCategory::Layout,
            self.profiler_metadata(),
            self.time_profiler_chan.clone(),
            || with_layout_state(|| self.handle_reflow(reflow_request)),
        )
    }

    fn ensure_stacking_context_tree(&self, viewport_details: ViewportDetails) {
        with_layout_state(|| {
            if self.stacking_context_tree.borrow().is_some() &&
                !self.need_new_stacking_context_tree.get()
            {
                return;
            }
            self.build_stacking_context_tree(viewport_details);
        })
    }

    fn register_paint_worklet_modules(
        &mut self,
        _name: Atom,
        _properties: Vec<Atom>,
        _painter: Box<dyn Painter>,
    ) {
    }

    fn set_scroll_offsets_from_renderer(
        &mut self,
        scroll_states: &FxHashMap<ExternalScrollId, LayoutVector2D>,
    ) {
        let mut stacking_context_tree = self.stacking_context_tree.borrow_mut();
        let Some(stacking_context_tree) = stacking_context_tree.as_mut() else {
            warn!("Received scroll offsets before finishing layout.");
            return;
        };

        stacking_context_tree
            .paint_info
            .scroll_tree
            .set_all_scroll_offsets(scroll_states);
    }

    fn scroll_offset(&self, id: ExternalScrollId) -> Option<LayoutVector2D> {
        self.stacking_context_tree
            .borrow_mut()
            .as_mut()
            .and_then(|tree| tree.paint_info.scroll_tree.scroll_offset(id))
    }

    fn needs_new_display_list(&self) -> bool {
        self.need_new_display_list.get()
    }

    fn set_needs_new_display_list(&self) {
        self.need_new_display_list.set(true);
    }

    /// <https://drafts.css-houdini.org/css-properties-values-api-1/#the-registerproperty-function>
    fn stylist_mut(&mut self) -> &mut Stylist {
        &mut self.stylist
    }

    fn set_accessibility_active(&self, active: bool) {
        if !(pref!(accessibility_enabled)) {
            return;
        }

        self.accessibility_active.replace(active);
    }
}

impl LayoutThread {
    fn new(config: LayoutConfig) -> LayoutThread {
        // Let webrender know about this pipeline by sending an empty display list.
        config
            .paint_api
            .send_initial_transaction(config.webview_id, config.id.into());

        let mut font = Font::initial_values();
        let default_font_size = pref!(fonts_default_size);
        font.font_size = FontSize {
            computed_size: NonNegativeLength::new(default_font_size as f32),
            used_size: NonNegativeLength::new(default_font_size as f32),
            keyword_info: KeywordInfo::medium(),
        };

        // The device pixel ratio is incorrect (it does not have the hidpi value),
        // but it will be set correctly when the initial reflow takes place.
        let device = Device::new(
            MediaType::screen(),
            QuirksMode::NoQuirks,
            config.viewport_details.size,
            Scale::new(config.viewport_details.hidpi_scale_factor.get()),
            Box::new(LayoutFontMetricsProvider(config.font_context.clone())),
            ComputedValues::initial_values_with_font_override(font),
            config.theme.into(),
        );

        LayoutThread {
            id: config.id,
            webview_id: config.webview_id,
            url: config.url,
            is_iframe: config.is_iframe,
            script_chan: config.script_chan.clone(),
            time_profiler_chan: config.time_profiler_chan,
            registered_painters: RegisteredPaintersImpl(Default::default()),
            image_cache: config.image_cache,
            font_context: config.font_context,
            have_added_user_agent_stylesheets: false,
            have_ever_generated_display_list: Cell::new(false),
            device_has_changed: false,
            need_overflow_calculation: Cell::new(false),
            need_new_display_list: Cell::new(false),
            need_new_stacking_context_tree: Cell::new(false),
            box_tree: Default::default(),
            fragment_tree: Default::default(),
            stacking_context_tree: Default::default(),
            paint_api: config.paint_api,
            stylist: Stylist::new(device, QuirksMode::NoQuirks),
            resolved_images_cache: Default::default(),
            debug: opts::get().debug.clone(),
            previously_highlighted_dom_node: Cell::new(None),
            paint_timing_handler: Default::default(),
            user_stylesheets: config.user_stylesheets,
            accessibility_active: Cell::new(false),
        }
    }

    fn build_shared_style_context<'a>(
        &'a self,
        guards: StylesheetGuards<'a>,
        snapshot_map: &'a SnapshotMap,
        animation_timeline_value: f64,
        animations: &DocumentAnimationSet,
        traversal_flags: TraversalFlags,
    ) -> SharedStyleContext<'a> {
        SharedStyleContext {
            stylist: &self.stylist,
            options: GLOBAL_STYLE_DATA.options.clone(),
            guards,
            visited_styles_enabled: false,
            animations: animations.clone(),
            registered_speculative_painters: &self.registered_painters,
            current_time_for_animations: animation_timeline_value,
            traversal_flags,
            snapshot_map,
        }
    }

    fn load_all_web_fonts_from_stylesheet_with_guard(
        &self,
        stylesheet: &DocumentStyleSheet,
        guard: &SharedRwLockReadGuard,
        document_context: &WebFontDocumentContext,
    ) {
        let custom_media = &CustomMediaMap::default();
        if !stylesheet.is_effective_for_device(self.stylist.device(), custom_media, guard) {
            return;
        }

        let locked_script_channel = Mutex::new(self.script_chan.clone());
        let pipeline_id = self.id;
        let web_font_finished_loading_callback = move |succeeded: bool| {
            if succeeded {
                let _ = locked_script_channel
                    .lock()
                    .send(ScriptThreadMessage::WebFontLoaded(pipeline_id));
            }
        };

        self.font_context.add_all_web_fonts_from_stylesheet(
            self.webview_id,
            stylesheet,
            guard,
            self.stylist.device(),
            Arc::new(web_font_finished_loading_callback) as StylesheetWebFontLoadFinishedCallback,
            document_context,
        );
    }

    /// In some cases, if a restyle isn't necessary we can skip doing any work for layout
    /// entirely. This check allows us to return early from layout without doing any work
    /// at all.
    fn can_skip_reflow_request_entirely(&self, reflow_request: &ReflowRequest) -> bool {
        // If a restyle is necessary, restyle and reflow is a necessity.
        if reflow_request.restyle.is_some() {
            return false;
        }
        // We always need to at least build a fragment tree.
        if self.fragment_tree.borrow().is_none() {
            return false;
        }

        // If we have a fragment tree and it's up-to-date and this reflow
        // doesn't need more reflow results, we can skip the rest of layout.
        let necessary_phases = ReflowPhases::necessary(&reflow_request.reflow_goal);
        if necessary_phases.is_empty() {
            return true;
        }

        // If only the stacking context tree is required, and it's up-to-date,
        // layout is unnecessary, otherwise a layout is necessary.
        if necessary_phases == ReflowPhases::StackingContextTreeConstruction {
            return self.stacking_context_tree.borrow().is_some() &&
                !self.need_new_stacking_context_tree.get();
        }

        // Otherwise, the only interesting thing is whether the current display
        // list is up-to-date.
        assert_eq!(
            necessary_phases,
            ReflowPhases::StackingContextTreeConstruction | ReflowPhases::DisplayListConstruction
        );
        !self.need_new_display_list.get()
    }

    fn maybe_print_reflow_event(&self, reflow_request: &ReflowRequest) {
        if !self.debug.relayout_event {
            return;
        }

        println!(
            "**** Reflow({}) => {:?}, {:?}",
            self.id,
            reflow_request.reflow_goal,
            reflow_request
                .restyle
                .as_ref()
                .map(|restyle| restyle.reason)
                .unwrap_or_default()
        );
    }

    /// Checks whether we need to update the scroll node, and report whether the
    /// node is scrolled. We need to update the scroll node whenever it is requested.
    fn handle_update_scroll_node_request(&self, reflow_request: &ReflowRequest) -> bool {
        if let ReflowGoal::UpdateScrollNode(external_scroll_id, offset) = reflow_request.reflow_goal
        {
            self.set_scroll_offset_from_script(external_scroll_id, offset)
        } else {
            false
        }
    }

    /// The high-level routine that performs layout.
    #[servo_tracing::instrument(skip_all)]
    fn handle_reflow(&mut self, mut reflow_request: ReflowRequest) -> Option<ReflowResult> {
        self.maybe_print_reflow_event(&reflow_request);

        if self.can_skip_reflow_request_entirely(&reflow_request) {
            // We can skip layout, but we might need to update a scroll node.
            return self
                .handle_update_scroll_node_request(&reflow_request)
                .then(|| ReflowResult {
                    reflow_phases_run: ReflowPhasesRun::UpdatedScrollNodeOffset,
                    ..Default::default()
                });
        }

        let document = unsafe { ServoLayoutNode::new(&reflow_request.document) };
        let document = document.as_document().unwrap();
        let Some(root_element) = document.root_element() else {
            debug!("layout: No root node: bailing");
            return None;
        };

        let image_resolver = Arc::new(ImageResolver {
            origin: reflow_request.origin.clone(),
            image_cache: self.image_cache.clone(),
            resolved_images_cache: self.resolved_images_cache.clone(),
            pending_images: Mutex::default(),
            pending_rasterization_images: Mutex::default(),
            pending_svg_elements_for_serialization: Mutex::default(),
            animating_images: reflow_request.animating_images.clone(),
            animation_timeline_value: reflow_request.animation_timeline_value,
        });
        let mut reflow_statistics = Default::default();

        let (mut reflow_phases_run, iframe_sizes) = self.restyle_and_build_trees(
            &mut reflow_request,
            document,
            root_element,
            &image_resolver,
        );
        if self.calculate_overflow() {
            reflow_phases_run.insert(ReflowPhasesRun::CalculatedOverflow);
        }
        if self.build_stacking_context_tree_for_reflow(&reflow_request) {
            reflow_phases_run.insert(ReflowPhasesRun::BuiltStackingContextTree);
        }
        if self.build_display_list(&reflow_request, &image_resolver, &mut reflow_statistics) {
            reflow_phases_run.insert(ReflowPhasesRun::BuiltDisplayList);
        }
        if self.handle_update_scroll_node_request(&reflow_request) {
            reflow_phases_run.insert(ReflowPhasesRun::UpdatedScrollNodeOffset);
        }

        let pending_images = std::mem::take(&mut *image_resolver.pending_images.lock());
        let pending_rasterization_images =
            std::mem::take(&mut *image_resolver.pending_rasterization_images.lock());
        let pending_svg_elements_for_serialization =
            std::mem::take(&mut *image_resolver.pending_svg_elements_for_serialization.lock());

        Some(ReflowResult {
            reflow_phases_run,
            pending_images,
            pending_rasterization_images,
            pending_svg_elements_for_serialization,
            iframe_sizes: Some(iframe_sizes),
            reflow_statistics,
        })
    }

    #[servo_tracing::instrument(skip_all)]
    fn prepare_stylist_for_reflow<'dom>(
        &mut self,
        reflow_request: &ReflowRequest,
        document: ServoLayoutDocument<'dom>,
        guards: &StylesheetGuards,
        ua_stylesheets: &UserAgentStylesheets,
    ) -> StylesheetInvalidationSet {
        if !self.have_added_user_agent_stylesheets {
            for stylesheet in &ua_stylesheets.user_agent_stylesheets {
                self.stylist
                    .append_stylesheet(stylesheet.clone(), guards.ua_or_user);
                self.load_all_web_fonts_from_stylesheet_with_guard(
                    stylesheet,
                    guards.ua_or_user,
                    &reflow_request.document_context,
                );
            }

            for user_stylesheet in self.user_stylesheets.iter() {
                self.stylist
                    .append_stylesheet(user_stylesheet.clone(), guards.ua_or_user);
                self.load_all_web_fonts_from_stylesheet_with_guard(
                    user_stylesheet,
                    guards.ua_or_user,
                    &reflow_request.document_context,
                );
            }

            if self.stylist.quirks_mode() == QuirksMode::Quirks {
                self.stylist.append_stylesheet(
                    ua_stylesheets.quirks_mode_stylesheet.clone(),
                    guards.ua_or_user,
                );
                self.load_all_web_fonts_from_stylesheet_with_guard(
                    &ua_stylesheets.quirks_mode_stylesheet,
                    guards.ua_or_user,
                    &reflow_request.document_context,
                );
            }
            self.have_added_user_agent_stylesheets = true;
        }

        if reflow_request.stylesheets_changed() {
            self.stylist
                .force_stylesheet_origins_dirty(Origin::Author.into());
        }

        // Flush shadow roots stylesheets if dirty.
        document.flush_shadow_roots_stylesheets(&mut self.stylist, guards.author);

        self.stylist.flush(guards)
    }

    #[servo_tracing::instrument(skip_all)]
    fn restyle_and_build_trees(
        &mut self,
        reflow_request: &mut ReflowRequest,
        document: ServoLayoutDocument<'_>,
        root_element: ServoLayoutElement<'_>,
        image_resolver: &Arc<ImageResolver>,
    ) -> (ReflowPhasesRun, IFrameSizes) {
        let mut snapshot_map = SnapshotMap::new();
        let _snapshot_setter = match reflow_request.restyle.as_mut() {
            Some(restyle) => SnapshotSetter::new(restyle, &mut snapshot_map),
            None => return Default::default(),
        };

        let document_shared_lock = document.style_shared_lock();
        let author_guard = document_shared_lock.read();
        let ua_stylesheets = &*UA_STYLESHEETS;
        let guards = StylesheetGuards {
            author: &author_guard,
            ua_or_user: &GLOBAL_STYLE_DATA.shared_lock.read(),
        };

        let rayon_pool = STYLE_THREAD_POOL.lock();
        let rayon_pool = rayon_pool.pool();
        let rayon_pool = rayon_pool.as_ref();

        let device_has_changed = std::mem::replace(&mut self.device_has_changed, false);
        if device_has_changed {
            let sheet_origins_affected_by_device_change = self
                .stylist
                .media_features_change_changed_style(&guards, self.device());
            self.stylist
                .force_stylesheet_origins_dirty(sheet_origins_affected_by_device_change);

            if let Some(mut data) = root_element.mutate_data() {
                data.hint.insert(RestyleHint::recascade_subtree());
            }
        }

        self.prepare_stylist_for_reflow(reflow_request, document, &guards, ua_stylesheets)
            .process_style(root_element, Some(&snapshot_map));

        if self.previously_highlighted_dom_node.get() != reflow_request.highlighted_dom_node {
            // Need to manually force layout to build a new display list regardless of whether the box tree
            // changed or not.
            self.need_new_display_list.set(true);
        }

        let layout_context = LayoutContext {
            style_context: self.build_shared_style_context(
                guards,
                &snapshot_map,
                reflow_request.animation_timeline_value,
                &reflow_request.animations,
                match reflow_request.stylesheets_changed() {
                    true => TraversalFlags::ForCSSRuleChanges,
                    false => TraversalFlags::empty(),
                },
            ),
            font_context: self.font_context.clone(),
            iframe_sizes: Mutex::default(),
            use_rayon: rayon_pool.is_some(),
            image_resolver: image_resolver.clone(),
            painter_id: self.webview_id.into(),
        };

        let restyle = reflow_request
            .restyle
            .as_ref()
            .expect("Should not get here if there is not restyle.");

        let recalc_style_traversal;
        let dirty_root;
        {
            let _span = profile_traits::trace_span!("Styling").entered();

            let original_dirty_root = unsafe {
                ServoLayoutNode::new(&restyle.dirty_root.unwrap())
                    .as_element()
                    .unwrap()
            };

            recalc_style_traversal = RecalcStyle::new(&layout_context);
            let token = {
                let shared =
                    DomTraversal::<ServoLayoutElement>::shared_context(&recalc_style_traversal);
                RecalcStyle::pre_traverse(original_dirty_root, shared)
            };

            if !token.should_traverse() {
                layout_context.style_context.stylist.rule_tree().maybe_gc();
                return Default::default();
            }

            dirty_root = driver::traverse_dom(&recalc_style_traversal, token, rayon_pool).as_node();
        }

        let root_node = root_element.as_node();
        let damage_from_environment = if device_has_changed {
            RestyleDamage::RELAYOUT
        } else {
            Default::default()
        };

        let mut box_tree = self.box_tree.borrow_mut();
        let damage = {
            let box_tree = &mut *box_tree;
            let mut compute_damage_and_build_box_tree = || {
                compute_damage_and_rebuild_box_tree(
                    box_tree,
                    &layout_context,
                    dirty_root,
                    root_node,
                    damage_from_environment,
                )
            };

            if let Some(pool) = rayon_pool {
                pool.install(compute_damage_and_build_box_tree)
            } else {
                compute_damage_and_build_box_tree()
            }
        };

        if damage.contains(RestyleDamage::RECALCULATE_OVERFLOW) {
            self.need_overflow_calculation.set(true);
        }
        if damage.contains(RestyleDamage::REBUILD_STACKING_CONTEXT) {
            self.need_new_stacking_context_tree.set(true);
        }
        if damage.contains(RestyleDamage::REPAINT) {
            self.need_new_display_list.set(true);
        }
        if !damage.contains(RestyleDamage::RELAYOUT) {
            layout_context.style_context.stylist.rule_tree().maybe_gc();
            return (ReflowPhasesRun::empty(), IFrameSizes::default());
        }

        let box_tree = &*box_tree;
        let viewport_size = self.stylist.device().au_viewport_size();
        let run_layout = || {
            box_tree
                .as_ref()
                .unwrap()
                .layout(recalc_style_traversal.context(), viewport_size)
        };
        let fragment_tree = Rc::new(if let Some(pool) = rayon_pool {
            pool.install(run_layout)
        } else {
            run_layout()
        });

        *self.fragment_tree.borrow_mut() = Some(fragment_tree);

        if self.debug.style_tree {
            println!(
                "{:?}",
                ShowSubtreeDataAndPrimaryValues(root_element.as_node())
            );
        }
        if self.debug.rule_tree {
            recalc_style_traversal
                .context()
                .style_context
                .stylist
                .rule_tree()
                .dump_stdout(&layout_context.style_context.guards);
        }

        // GC the rule tree if some heuristics are met.
        layout_context.style_context.stylist.rule_tree().maybe_gc();

        let mut iframe_sizes = layout_context.iframe_sizes.lock();
        (
            ReflowPhasesRun::RanLayout,
            std::mem::take(&mut *iframe_sizes),
        )
    }

    #[servo_tracing::instrument(name = "Overflow Calculation", skip_all)]
    fn calculate_overflow(&self) -> bool {
        if !self.need_overflow_calculation.get() {
            return false;
        }

        if let Some(fragment_tree) = &*self.fragment_tree.borrow() {
            fragment_tree.calculate_scrollable_overflow();
            if self.debug.flow_tree {
                fragment_tree.print();
            }
        }

        self.need_overflow_calculation.set(false);
        assert!(self.need_new_display_list.get());
        assert!(self.need_new_stacking_context_tree.get());

        true
    }

    fn build_stacking_context_tree_for_reflow(&self, reflow_request: &ReflowRequest) -> bool {
        if !ReflowPhases::necessary(&reflow_request.reflow_goal)
            .contains(ReflowPhases::StackingContextTreeConstruction)
        {
            return false;
        }
        if !self.need_new_stacking_context_tree.get() {
            return false;
        }

        self.build_stacking_context_tree(reflow_request.viewport_details)
    }

    #[servo_tracing::instrument(name = "Stacking Context Tree Construction", skip_all)]
    fn build_stacking_context_tree(&self, viewport_details: ViewportDetails) -> bool {
        let Some(fragment_tree) = &*self.fragment_tree.borrow() else {
            return false;
        };

        let mut stacking_context_tree = self.stacking_context_tree.borrow_mut();
        let old_scroll_offsets = stacking_context_tree
            .as_ref()
            .map(|tree| tree.paint_info.scroll_tree.scroll_offsets());

        // Build the StackingContextTree. This turns the `FragmentTree` into a
        // tree of fragments in CSS painting order and also creates all
        // applicable spatial and clip nodes.
        let mut new_stacking_context_tree = StackingContextTree::new(
            fragment_tree,
            viewport_details,
            self.id.into(),
            !self.have_ever_generated_display_list.get(),
            &self.debug,
        );

        // When a new StackingContextTree is built, it contains a freshly built
        // ScrollTree. We want to preserve any existing scroll offsets in that tree,
        // adjusted by any new scroll constraints.
        if let Some(old_scroll_offsets) = old_scroll_offsets {
            new_stacking_context_tree
                .paint_info
                .scroll_tree
                .set_all_scroll_offsets(&old_scroll_offsets);
        }

        if self.debug.scroll_tree {
            new_stacking_context_tree
                .paint_info
                .scroll_tree
                .debug_print();
        }

        *stacking_context_tree = Some(new_stacking_context_tree);

        // The stacking context tree is up-to-date again.
        self.need_new_stacking_context_tree.set(false);
        assert!(self.need_new_display_list.get());

        true
    }

    /// Build the display list for the current layout and send it to the renderer. If no display
    /// list is built, returns false.
    #[servo_tracing::instrument(name = "Display List Construction", skip_all)]
    fn build_display_list(
        &self,
        reflow_request: &ReflowRequest,
        image_resolver: &Arc<ImageResolver>,
        reflow_statistics: &mut ReflowStatistics,
    ) -> bool {
        if !ReflowPhases::necessary(&reflow_request.reflow_goal)
            .contains(ReflowPhases::DisplayListConstruction)
        {
            return false;
        }
        let Some(fragment_tree) = &*self.fragment_tree.borrow() else {
            return false;
        };
        let mut stacking_context_tree = self.stacking_context_tree.borrow_mut();
        let Some(stacking_context_tree) = stacking_context_tree.as_mut() else {
            return false;
        };

        // If a non-display-list-generating reflow updated layout in a previous refow, we
        // cannot skip display list generation here the next time a display list is
        // requested.
        if !self.need_new_display_list.get() {
            return false;
        }

        // TODO: Eventually this should be set when `paint_info` is created, but that requires
        // ensuring that the Epoch is passed to any method that can creates `StackingContextTree`.
        stacking_context_tree.paint_info.epoch = reflow_request.epoch;

        let mut paint_timing_handler = self.paint_timing_handler.borrow_mut();
        // This ensures that we only create the PaintTimingHandler once per layout thread.
        let paint_timing_handler = match paint_timing_handler.as_mut() {
            Some(paint_timing_handler) => paint_timing_handler,
            None => {
                *paint_timing_handler = Some(PaintTimingHandler::new(
                    stacking_context_tree
                        .paint_info
                        .viewport_details
                        .layout_size(),
                ));
                paint_timing_handler.as_mut().unwrap()
            },
        };

        let built_display_list = DisplayListBuilder::build(
            stacking_context_tree,
            fragment_tree,
            image_resolver.clone(),
            self.device().device_pixel_ratio(),
            reflow_request.highlighted_dom_node,
            &self.debug,
            paint_timing_handler,
            reflow_statistics,
        );
        self.paint_api.send_display_list(
            self.webview_id,
            &stacking_context_tree.paint_info,
            built_display_list,
        );

        if paint_timing_handler.did_lcp_candidate_update() {
            if let Some(lcp_candidate) = paint_timing_handler.largest_contentful_paint_candidate() {
                self.paint_api.send_lcp_candidate(
                    lcp_candidate,
                    self.webview_id,
                    self.id,
                    stacking_context_tree.paint_info.epoch,
                );
                paint_timing_handler.unset_lcp_candidate_updated();
            }
        }

        let (keys, instance_keys) = self
            .font_context
            .collect_unused_webrender_resources(false /* all */);
        self.paint_api
            .remove_unused_font_resources(self.webview_id.into(), keys, instance_keys);

        self.have_ever_generated_display_list.set(true);
        self.need_new_display_list.set(false);
        self.previously_highlighted_dom_node
            .set(reflow_request.highlighted_dom_node);
        true
    }

    fn set_scroll_offset_from_script(
        &self,
        external_scroll_id: ExternalScrollId,
        offset: LayoutVector2D,
    ) -> bool {
        let mut stacking_context_tree = self.stacking_context_tree.borrow_mut();
        let Some(stacking_context_tree) = stacking_context_tree.as_mut() else {
            return false;
        };

        if let Some(offset) = stacking_context_tree
            .paint_info
            .scroll_tree
            .set_scroll_offset_for_node_with_external_scroll_id(
                external_scroll_id,
                offset,
                ScrollType::Script,
            )
        {
            self.paint_api.scroll_node_by_delta(
                self.webview_id,
                self.id.into(),
                offset,
                external_scroll_id,
            );
            true
        } else {
            false
        }
    }

    /// Returns profiling information which is passed to the time profiler.
    fn profiler_metadata(&self) -> Option<TimerMetadata> {
        Some(TimerMetadata {
            url: self.url.to_string(),
            iframe: if self.is_iframe {
                TimerMetadataFrameType::IFrame
            } else {
                TimerMetadataFrameType::RootWindow
            },
            incremental: if self.have_ever_generated_display_list.get() {
                TimerMetadataReflowType::Incremental
            } else {
                TimerMetadataReflowType::FirstReflow
            },
        })
    }
}

fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
    fn parse_ua_stylesheet(
        shared_lock: &SharedRwLock,
        filename: &str,
        content: &[u8],
    ) -> Result<DocumentStyleSheet, &'static str> {
        let url = Url::parse(&format!("chrome://resources/{:?}", filename))
            .ok()
            .unwrap();
        Ok(DocumentStyleSheet(ServoArc::new(Stylesheet::from_bytes(
            content,
            url.into(),
            None,
            None,
            Origin::UserAgent,
            ServoArc::new(shared_lock.wrap(MediaList::empty())),
            shared_lock.clone(),
            None,
            None,
            QuirksMode::NoQuirks,
        ))))
    }

    let shared_lock = &GLOBAL_STYLE_DATA.shared_lock;

    // FIXME: presentational-hints.css should be at author origin with zero specificity.
    //        (Does it make a difference?)
    let user_agent_stylesheets = vec![
        parse_ua_stylesheet(shared_lock, "user-agent.css", USER_AGENT_CSS)?,
        parse_ua_stylesheet(shared_lock, "servo.css", SERVO_CSS)?,
        parse_ua_stylesheet(
            shared_lock,
            "presentational-hints.css",
            PRESENTATIONAL_HINTS_CSS,
        )?,
    ];

    let quirks_mode_stylesheet =
        parse_ua_stylesheet(shared_lock, "quirks-mode.css", QUIRKS_MODE_CSS)?;

    Ok(UserAgentStylesheets {
        user_agent_stylesheets,
        quirks_mode_stylesheet,
    })
}

/// This structure holds the user-agent stylesheets.
pub struct UserAgentStylesheets {
    /// The user agent stylesheets.
    pub user_agent_stylesheets: Vec<DocumentStyleSheet>,
    /// The quirks mode stylesheet.
    pub quirks_mode_stylesheet: DocumentStyleSheet,
}

static UA_STYLESHEETS: LazyLock<UserAgentStylesheets> =
    LazyLock::new(|| match get_ua_stylesheets() {
        Ok(stylesheets) => stylesheets,
        Err(filename) => {
            error!("Failed to load UA stylesheet {}!", filename);
            process::exit(1);
        },
    });

struct RegisteredPainterImpl {
    painter: Box<dyn Painter>,
    name: Atom,
    // FIXME: Should be a PrecomputedHashMap.
    properties: FxHashMap<Atom, PropertyId>,
}

impl SpeculativePainter for RegisteredPainterImpl {
    fn speculatively_draw_a_paint_image(
        &self,
        properties: Vec<(Atom, String)>,
        arguments: Vec<String>,
    ) {
        self.painter
            .speculatively_draw_a_paint_image(properties, arguments);
    }
}

impl RegisteredSpeculativePainter for RegisteredPainterImpl {
    fn properties(&self) -> &FxHashMap<Atom, PropertyId> {
        &self.properties
    }
    fn name(&self) -> Atom {
        self.name.clone()
    }
}

impl Painter for RegisteredPainterImpl {
    fn draw_a_paint_image(
        &self,
        size: Size2D<f32, CSSPixel>,
        device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
        properties: Vec<(Atom, String)>,
        arguments: Vec<String>,
    ) -> Result<DrawAPaintImageResult, PaintWorkletError> {
        self.painter
            .draw_a_paint_image(size, device_pixel_ratio, properties, arguments)
    }
}

struct RegisteredPaintersImpl(HashMap<Atom, RegisteredPainterImpl>);

impl RegisteredSpeculativePainters for RegisteredPaintersImpl {
    fn get(&self, name: &Atom) -> Option<&dyn RegisteredSpeculativePainter> {
        self.0
            .get(name)
            .map(|painter| painter as &dyn RegisteredSpeculativePainter)
    }
}

struct LayoutFontMetricsProvider(Arc<FontContext>);

impl FontMetricsProvider for LayoutFontMetricsProvider {
    fn query_font_metrics(
        &self,
        _vertical: bool,
        font: &Font,
        base_size: CSSPixelLength,
        _flags: QueryFontMetricsFlags,
    ) -> FontMetrics {
        let font_context = &self.0;
        let font_group = self
            .0
            .font_group_with_size(ServoArc::new(font.clone()), base_size.into());

        let Some(first_font_metrics) = font_group
            .first(font_context)
            .map(|font| font.metrics.clone())
        else {
            return Default::default();
        };

        // Only use the x-height of this font if it is non-zero. Some fonts return
        // inaccurate metrics, which shouldn't be used.
        let x_height = Some(first_font_metrics.x_height)
            .filter(|x_height| !x_height.is_zero())
            .map(CSSPixelLength::from);

        let zero_advance_measure = first_font_metrics
            .zero_horizontal_advance
            .or_else(|| {
                font_group
                    .find_by_codepoint(font_context, '0', None, XLang::get_initial_value())?
                    .metrics
                    .zero_horizontal_advance
            })
            .map(CSSPixelLength::from);

        let ic_width = first_font_metrics
            .ic_horizontal_advance
            .or_else(|| {
                font_group
                    .find_by_codepoint(font_context, '\u{6C34}', None, XLang::get_initial_value())?
                    .metrics
                    .ic_horizontal_advance
            })
            .map(CSSPixelLength::from);

        FontMetrics {
            x_height,
            zero_advance_measure,
            cap_height: None,
            ic_width,
            ascent: first_font_metrics.ascent.into(),
            script_percent_scale_down: None,
            script_script_percent_scale_down: None,
        }
    }

    fn base_size_for_generic(&self, generic: GenericFontFamily) -> Length {
        Length::new(match generic {
            GenericFontFamily::Monospace => pref!(fonts_default_monospace_size),
            _ => pref!(fonts_default_size),
        } as f32)
        .max(Length::new(0.0))
    }
}

impl Debug for LayoutFontMetricsProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("LayoutFontMetricsProvider").finish()
    }
}

struct SnapshotSetter<'dom> {
    elements_with_snapshot: Vec<ServoLayoutElement<'dom>>,
}

impl SnapshotSetter<'_> {
    fn new(restyle: &mut ReflowRequestRestyle, snapshot_map: &mut SnapshotMap) -> Self {
        debug!("Draining restyles: {}", restyle.pending_restyles.len());
        let restyles = std::mem::take(&mut restyle.pending_restyles);

        let elements_with_snapshot: Vec<_> = restyles
            .iter()
            .filter(|r| r.1.snapshot.is_some())
            .map(|r| unsafe { ServoLayoutNode::new(&r.0).as_element().unwrap() })
            .collect();

        for (element, restyle) in restyles {
            let element = unsafe { ServoLayoutNode::new(&element).as_element().unwrap() };

            // If we haven't styled this node yet, we don't need to track a
            // restyle.
            let Some(mut style_data) = element.mutate_data() else {
                unsafe { element.unset_snapshot_flags() };
                continue;
            };

            debug!("Noting restyle for {:?}: {:?}", element, style_data);
            if let Some(s) = restyle.snapshot {
                unsafe { element.set_has_snapshot() };
                snapshot_map.insert(element.as_node().opaque(), s);
            }

            // Stash the data on the element for processing by the style system.
            style_data.hint.insert(restyle.hint);
            style_data.damage = restyle.damage;
        }
        Self {
            elements_with_snapshot,
        }
    }
}

impl Drop for SnapshotSetter<'_> {
    fn drop(&mut self) {
        for element in &self.elements_with_snapshot {
            unsafe { element.unset_snapshot_flags() }
        }
    }
}

bitflags! {
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub struct ReflowPhases: u8 {
        const StackingContextTreeConstruction = 1 << 0;
        const DisplayListConstruction = 1 << 1;
    }
}

impl ReflowPhases {
    /// Return the necessary phases of layout for the given [`ReflowGoal`]. Note that all
    /// [`ReflowGoals`] need the basic restyle + box tree layout + fragment tree layout,
    /// so [`ReflowPhases::empty()`] implies that.
    fn necessary(reflow_goal: &ReflowGoal) -> Self {
        match reflow_goal {
            ReflowGoal::LayoutQuery(query) => match query {
                QueryMsg::NodesFromPointQuery => {
                    Self::StackingContextTreeConstruction | Self::DisplayListConstruction
                },
                QueryMsg::BoxArea |
                QueryMsg::BoxAreas |
                QueryMsg::ElementsFromPoint |
                QueryMsg::OffsetParentQuery |
                QueryMsg::ResolvedStyleQuery |
                QueryMsg::ScrollingAreaOrOffsetQuery |
                QueryMsg::TextIndexQuery => Self::StackingContextTreeConstruction,
                QueryMsg::ClientRectQuery |
                QueryMsg::CurrentCSSZoomQuery |
                QueryMsg::EffectiveOverflow |
                QueryMsg::ElementInnerOuterTextQuery |
                QueryMsg::InnerWindowDimensionsQuery |
                QueryMsg::PaddingQuery |
                QueryMsg::ResolvedFontStyleQuery |
                QueryMsg::ScrollParentQuery |
                QueryMsg::StyleQuery => Self::empty(),
            },
            ReflowGoal::UpdateScrollNode(..) | ReflowGoal::UpdateTheRendering => {
                Self::StackingContextTreeConstruction | Self::DisplayListConstruction
            },
        }
    }
}