Truly independent web browser
LibWeb: Gather resize observations made beside an update later
Andreas Kling3176e57a8bd1
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 2b28e41a69c..e1858a604a9 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -9943,7 +9943,7 @@ static size_t calculate_depth_for_node(Node const& node)}// https://drafts.csswg.org/resize-observer-1/#gather-active-observations-h-void Document::gather_active_observations_at_depth(size_t depth)+void Document::gather_active_observations_at_depth(size_t depth, u64 rendering_update_serial){// 1. Let depth be the depth passed in.@@ -9963,6 +9963,10 @@ void Document::gather_active_observations_at_depth(size_t depth)// 2. For each observation in observer.[[observationTargets]] run this step:for (auto& observation : observer->observation_targets()) {+ // NB: An observation made beside a rendering update in flight is gathered by the rendering updates after it.+ if (observation->first_rendering_update_to_gather() > rendering_update_serial)+ continue;+// 1. If observation.isActive() is trueif (observation->is_active()) {auto target = observation->target();diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex d6496113716..bb6737caf22 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1438,7 +1438,7 @@ public:GC::RootVector<GC::Ref<DOM::Element>> named_elements_with_name(Utf16FlyString const&) const;static bool is_named_element_with_name(Element const&, Utf16FlyString const&);- void gather_active_observations_at_depth(size_t depth);+ void gather_active_observations_at_depth(size_t depth, u64 rendering_update_serial);[[nodiscard]] size_t broadcast_active_resize_observations(Layout::BegunRead const& read);[[nodiscard]] bool has_active_resize_observations();[[nodiscard]] bool has_skipped_resize_observations();diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex 11ea880b151..656f99a4e81 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -683,7 +683,7 @@ static HeldTasks tasks_rendering_update_holds(Vector<GC::Root<DOM::Document>> coreturn HeldTasks::None;}-static void update_style_and_layout_for_rendering(DOM::Document&);+static void update_style_and_layout_for_rendering(DOM::Document&, u64 update_serial);// https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering// NB: The documents step 3 of update the rendering keeps.@@ -1009,7 +1009,7 @@ void EventLoop::update_the_rendering()}}}- update_style_and_layout_for_rendering(document);+ update_style_and_layout_for_rendering(document, m_rendering_update_serial);}update_the_rendering_after_style_and_layout(docs, frame_timestamp, m_rendering_update_serial);@@ -1033,7 +1033,7 @@ void EventLoop::update_whether_font_face_sets_are_pending_on_the_environment(Vec// 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)+static void update_style_and_layout_for_rendering(DOM::Document& document, u64 update_serial){// 1. Let resizeObserverDepth be 0.size_t resize_observer_depth = 0;@@ -1117,7 +1117,7 @@ static void update_style_and_layout_for_rendering(DOM::Document& document)continue;// 5. Gather active resize observations at depth resizeObserverDepth for doc.- document.gather_active_observations_at_depth(resize_observer_depth);+ document.gather_active_observations_at_depth(resize_observer_depth, update_serial);// 6. If doc has active resize observations:if (document.has_active_resize_observations()) {@@ -1362,7 +1362,7 @@ void EventLoop::resume_rendering_update_in_flight(){TemporaryChange running { m_running_rendering_task, true };// The style and layout of the doc whose transaction flew take it in, waiting for it to land.- update_style_and_layout_for_rendering(update->document());+ update_style_and_layout_for_rendering(update->document(), update->serial);update_the_rendering_after_style_and_layout(update->docs, update->frame_timestamp, update->serial);}// What the steps asked for, an animation frame of a promise they resolved for one, asks for the next rendering@@ -1402,6 +1402,13 @@ bool EventLoop::finish_ended_rendering_update_as_its_layout_flew()return true;}+u64 EventLoop::first_rendering_update_to_gather_resize_observations_of(DOM::Document const& document) const+{+ if (m_rendering_update_in_flight && &m_rendering_update_in_flight->document() == &document)+ return m_rendering_update_in_flight->serial + 1;+ return 0;+}+void EventLoop::finish_rendering_update_in_flight(){// The steps after the style transaction take it in, waiting for it to land.diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.hindex 1b91be04670..27f6013d1f9 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h@@ -186,6 +186,9 @@ public:bool lays_out_rendering_update_in_flight() const;// Runs the steps of the rendering update whose style transaction flies, which take the transaction in.void finish_rendering_update_in_flight();+ // The serial number of the first rendering update that may gather a resize observation of `document` made now. A+ // task beside a rendering update whose style and layout of the document have yet to run comes after the whole of it.+ u64 first_rendering_update_to_gather_resize_observations_of(DOM::Document const&) const;// A rendering task that would find a rendering update not yet finished keeps its place in the queue until the update// has finished, rather than wait for it. One that finds a frame still in flight runs beside it.bool holds_rendering_opportunity() const;diff --git a/Libraries/LibWeb/ResizeObserver/ResizeObservation.h b/Libraries/LibWeb/ResizeObserver/ResizeObservation.hindex 4e4a4287135..4e93f236cea 100644--- a/Libraries/LibWeb/ResizeObserver/ResizeObservation.h+++ b/Libraries/LibWeb/ResizeObserver/ResizeObservation.h@@ -27,12 +27,17 @@ public:void set_last_reported_sizes(ReadonlySpan<GC::Ref<ResizeObserverSize>>);+ // The serial number of the first rendering update that gathers the observation.+ u64 first_rendering_update_to_gather() const { return m_first_rendering_update_to_gather; }+ void set_first_rendering_update_to_gather(u64 serial) { m_first_rendering_update_to_gather = serial; }+explicit ResizeObservation(DOM::Element& target, ObservedBox observed_box);private:GC::Weak<DOM::Element> m_target;ObservedBox m_observed_box;Vector<ResizeObserverSize::RawSize> m_last_reported_sizes;+ u64 m_first_rendering_update_to_gather { 0 };};}diff --git a/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp b/Libraries/LibWeb/ResizeObserver/ResizeObserver.cppindex da11f734f26..b3ff7255388 100644--- a/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp+++ b/Libraries/LibWeb/ResizeObserver/ResizeObserver.cpp@@ -12,6 +12,7 @@#include <LibWeb/Bindings/Wrappable.h>#include <LibWeb/Bindings/WrapperWorld.h>#include <LibWeb/DOM/Document.h>+#include <LibWeb/HTML/EventLoop/EventLoop.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/HTML/Window.h>#include <LibWeb/Page/Page.h>@@ -114,6 +115,7 @@ void ResizeObserver::observe(DOM::Element& target, ResizeObserverOptions options// 3. Let resizeObservation be new ResizeObservation(target, observedBox).auto resize_observation = MUST(ResizeObservation::create(target, observed_box));+ resize_observation->set_first_rendering_update_to_gather(HTML::main_thread_event_loop().first_rendering_update_to_gather_resize_observations_of(*m_document));// 4. Add the resizeObservation to the [[observationTargets]] slot.m_observation_targets.append(resize_observation);