Skip to content

Truly independent web browser

LibWeb: Run no layout update while reading layout as it flew

Andreas Klinga40124fb15a2
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 9d42ec40688..1cff885af24 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -997,6 +997,21 @@ void EventLoop::update_the_rendering()
finish_rendering_update(update_start_time);
}
+void EventLoop::update_whether_font_face_sets_are_pending_on_the_environment(Vector<GC::Root<DOM::Document>> const& docs, TakenLayout taken_layout)
+{
+ for (auto& document : docs) {
+ // https://drafts.csswg.org/css-font-loading/#fontfaceset-pending-on-the-environment
+ // A FontFaceSet is pending on the environment if any of the following are true:
+ // - the document is still loading
+ // - the document has pending stylesheet requests
+ // - the document has pending layout operations which might cause the user agent to request a font, or which depend on recently-loaded fonts
+ TemporaryExecutionContext context(document->relevant_settings_object(), TemporaryExecutionContext::CallbacksEnabled::Yes);
+ document->fonts()->set_is_pending_on_the_environment(document->readiness() == DocumentReadyState::Loading
+ || document->has_pending_style_sheet_requests()
+ || (taken_layout == TakenLayout::UpToDate && !document->layout_is_up_to_date()));
+ }
+}
+
// Step 16 of updating the rendering, for one doc of docs: its style and layout, and the resize observations they
// answer.
static void update_style_and_layout_for_rendering(DOM::Document& document)
@@ -1253,17 +1268,11 @@ void EventLoop::update_the_rendering_after_style_and_layout(Vector<GC::Root<DOM:
document->process_top_layer_removals();
}
- for (auto& document : docs) {
- // https://drafts.csswg.org/css-font-loading/#fontfaceset-pending-on-the-environment
- // A FontFaceSet is pending on the environment if any of the following are true:
- // - the document is still loading
- // - the document has pending stylesheet requests
- // - the document has pending layout operations which might cause the user agent to request a font, or which depend on recently-loaded fonts
- TemporaryExecutionContext context(document->relevant_settings_object(), TemporaryExecutionContext::CallbacksEnabled::Yes);
- document->fonts()->set_is_pending_on_the_environment(document->readiness() == DocumentReadyState::Loading
- || document->has_pending_style_sheet_requests()
- || (taken_layout == TakenLayout::UpToDate && !document->layout_is_up_to_date()));
- }
+ // NB: Where the update's layout flew, this runs once the docs no longer read as that layout left them: a FontFaceSet
+ // that stops being pending on the environment resolves its ready promise, whose reactions run script that may
+ // write to the docs and read their layout.
+ if (taken_layout == TakenLayout::UpToDate)
+ update_whether_font_face_sets_are_pending_on_the_environment(docs, taken_layout);
// AD-HOC: The lane of the frame the update presented may tick the document's running animations beside the tasks
// after it, until the next of their events, which the main thread sends, and hover what is under the
@@ -1337,13 +1346,17 @@ void EventLoop::finish_ended_rendering_update_in_flight()
bool EventLoop::finish_ended_rendering_update_as_its_layout_flew()
{
auto& document = m_rendering_update_in_flight->document();
- DOM::Document::LayoutAsItFlew layout_as_it_flew { document };
- if (!document.take_flown_layout_in())
- return false;
- auto update = m_rendering_update_in_flight.release_nonnull();
- if (auto navigable = document.navigable())
- navigable->clamp_viewport_scroll_offset();
- update_the_rendering_after_style_and_layout(update->docs, update->frame_timestamp, update->serial, TakenLayout::AsItFlew);
+ OwnPtr<RenderingUpdateInFlight> update;
+ {
+ DOM::Document::LayoutAsItFlew layout_as_it_flew { document };
+ if (!document.take_flown_layout_in())
+ return false;
+ update = m_rendering_update_in_flight.release_nonnull();
+ if (auto navigable = document.navigable())
+ navigable->clamp_viewport_scroll_offset();
+ update_the_rendering_after_style_and_layout(update->docs, update->frame_timestamp, update->serial, TakenLayout::AsItFlew);
+ }
+ update_whether_font_face_sets_are_pending_on_the_environment(update->docs, TakenLayout::AsItFlew);
return true;
}
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index e3674e96eda..9c3772ac621 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -214,6 +214,7 @@ private:
AsItFlew,
};
void update_the_rendering_after_style_and_layout(Vector<GC::Root<DOM::Document>> const& docs, double frame_timestamp, u64 update_serial, TakenLayout = TakenLayout::UpToDate);
+ void update_whether_font_face_sets_are_pending_on_the_environment(Vector<GC::Root<DOM::Document>> const& docs, TakenLayout);
void finish_rendering_update(double update_start_time);
void resume_rendering_update_in_flight();
void finish_ended_rendering_update_in_flight();
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cpp
index bc4ff97adff..4d1e786ae21 100644
--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp
+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp
@@ -252,6 +252,13 @@ namespace Web::DOM {
void Document::update_layout(UpdateLayoutReason reason, ThrottledAnimationSamplingScope animation_sampling_scope)
{
+ // NB: While the document reads as the layout that flew left it, its layout is up to date as of that frame, and the
+ // writes made since are set aside behind the ones made meanwhile. A style update now would submit the style
+ // inputs recorded since ahead of the writes they go with, such as an inline style's declarations, and settle
+ // the elements over what those writes replace. What was written since is the next layout update's.
+ if (m_reads_layout_as_it_flew)
+ return;
+
// The update's waits for the render state are one read, which takes a frame in flight in.
Layout::ForcedReadScope read { style_computer().style_engine().render_document() };
drain_flown_style_transaction(read);
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/read-in-font-face-set-ready-beside-layout-that-flew.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/read-in-font-face-set-ready-beside-layout-that-flew.txt
new file mode 100644
index 00000000000..bb08d4c4ae1
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/read-in-font-face-set-ready-beside-layout-that-flew.txt
@@ -0,0 +1 @@
+width when fonts were ready: 30
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/reveal-in-animation-frame-beside-layout-that-flew.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/reveal-in-animation-frame-beside-layout-that-flew.txt
new file mode 100644
index 00000000000..a5f0794492b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/reveal-in-animation-frame-beside-layout-that-flew.txt
@@ -0,0 +1,2 @@
+tree width: 400
+flex widths: 133,133,133
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/read-in-font-face-set-ready-beside-layout-that-flew.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/read-in-font-face-set-ready-beside-layout-that-flew.html
new file mode 100644
index 00000000000..f03b80ea824
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/read-in-font-face-set-ready-beside-layout-that-flew.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<div id="work"></div>
+<div id="box" style="width: 10px"></div>
+<script>
+ // The document's FontFaceSet stops being pending on the environment in the steps of the rendering update whose
+ // layout flew, which the next update finishes as that layout left the document. Its ready promise is resolved then,
+ // and the script it runs reads the layout of what it writes, which is brought up to date.
+ internals.setManualRenderingOpportunities(true);
+ asyncTest(async done => {
+ if (document.readyState !== "complete")
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+ let widthWhenReady = "unread";
+ document.fonts.ready.then(() => {
+ box.style.width = "30px";
+ widthWhenReady = box.offsetWidth;
+ });
+ requestAnimationFrame(() => {
+ internals.holdNextFrame("layout");
+ let html = "";
+ for (let i = 0; i < 5000; ++i)
+ html += `<div><span>${i}</span></div>`;
+ document.getElementById("work").innerHTML = html;
+ requestAnimationFrame(() => {
+ box.style.height = "5px";
+ });
+ });
+ internals.injectRenderingOpportunity(performance.now());
+ while (internals.frameSchedulerState() !== "laying-out")
+ await nextTask();
+ internals.injectRenderingOpportunity(performance.now());
+ internals.releaseHeldFrame();
+ while (internals.frameSchedulerState() !== "idle")
+ await nextTask();
+ internals.setManualRenderingOpportunities(false);
+ println(`width when fonts were ready: ${widthWhenReady}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/reveal-in-animation-frame-beside-layout-that-flew.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/reveal-in-animation-frame-beside-layout-that-flew.html
new file mode 100644
index 00000000000..3663b03f339
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/reveal-in-animation-frame-beside-layout-that-flew.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ #tree { width: 400px; }
+ .flex { display: flex; }
+ .flex > div { flex-grow: 1; height: 10px; }
+</style>
+<div id="work"></div>
+<div id="tree" style="display: none">
+ <div id="flex" class="flex"><div id="flex-a"></div><div id="flex-b"></div><div id="flex-c"></div></div>
+</div>
+<script>
+ // An animation frame callback reveals a subtree while the layout of the last rendering update, which had a lot to
+ // lay out, is in flight. The update takes that layout in and finishes the last update's steps as its layout flew,
+ // which reads nothing written since. No rendering update ran before the one whose layout flew, so those steps are
+ // where the document's FontFaceSet stops being pending on the environment, which asks for a layout update. The
+ // reveal is the next layout's, which a read in a later task brings up to date.
+ internals.setManualRenderingOpportunities(true);
+ asyncTest(async done => {
+ if (document.readyState !== "complete")
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+ requestAnimationFrame(() => {
+ internals.holdNextFrame("layout");
+ let html = "";
+ for (let i = 0; i < 5000; ++i)
+ html += `<div><span>${i}</span></div>`;
+ document.getElementById("work").innerHTML = html;
+ requestAnimationFrame(() => {
+ document.getElementById("tree").style.display = "block";
+ });
+ });
+ internals.injectRenderingOpportunity(performance.now());
+ while (internals.frameSchedulerState() !== "laying-out")
+ await nextTask();
+ internals.injectRenderingOpportunity(performance.now());
+ internals.releaseHeldFrame();
+ while (internals.frameSchedulerState() !== "idle")
+ await nextTask();
+ internals.setManualRenderingOpportunities(false);
+ println(`tree width: ${document.getElementById("tree").offsetWidth}`);
+ println(`flex widths: ${["flex-a", "flex-b", "flex-c"].map(id => document.getElementById(id).offsetWidth).join(",")}`);
+ done();
+ });
+</script>