script: Unify script-based "update the rendering" and throttle it to 60 FPS (#38431)

Instead of running "update the rendering" at every IPC message, only run
it when a timeout has occured in script. In addition, avoid updating the
rendering if a rendering update isn't necessary. This should greatly
reduce the amount of processing that has to happen in script.

Because we are running many fewer calls to "update the rendering" it is
reasonable now to ensure that these always work the same way. In
particular, we always run rAF and update the animation timeline when
updating the ernder

In addition, pull the following things out of reflow:

 - Code dealing with informing the Constellation that a Pipeline has
   become Idle when waiting for a screenshot.
 - Detecting when it is time to fulfill the `document.fonts.ready`
   promise.

The latter means that reflow can never cause a garbage collection,
making timing of reflows more consistent and simplifying many callsites
that need to do script queries.

Followup changes will seek to simplify the way that ScriptThread-driven
animation timeouts happen even simpler.

Testing: In general, this should not change testable behavior so much,
though it
does seem to fix one test.  The main improvement here should be that
the ScriptThread does less work.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Martin Robinson 2025-08-04 18:27:00 +02:00 committed by GitHub
parent bcc8314dd5
commit 9416251cab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 607 additions and 672 deletions

View file

@ -194,14 +194,13 @@ fn matching_links(
links: &NodeList,
link_text: String,
partial: bool,
can_gc: CanGc,
) -> impl Iterator<Item = String> + '_ {
links
.iter()
.filter(move |node| {
let content = node
.downcast::<HTMLElement>()
.map(|element| element.InnerText(can_gc))
.map(|element| element.InnerText())
.map_or("".to_owned(), String::from)
.trim()
.to_owned();
@ -218,7 +217,6 @@ fn all_matching_links(
root_node: &Node,
link_text: String,
partial: bool,
can_gc: CanGc,
) -> Result<Vec<String>, ErrorStatus> {
// <https://w3c.github.io/webdriver/#dfn-find>
// Step 7.2. If a DOMException, SyntaxError, XPathException, or other error occurs
@ -226,7 +224,7 @@ fn all_matching_links(
root_node
.query_selector_all(DOMString::from("a"))
.map_err(|_| ErrorStatus::InvalidSelector)
.map(|nodes| matching_links(&nodes, link_text, partial, can_gc).collect())
.map(|nodes| matching_links(&nodes, link_text, partial).collect())
}
#[allow(unsafe_code)]
@ -749,7 +747,6 @@ pub(crate) fn handle_find_elements_link_text(
selector: String,
partial: bool,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
can_gc: CanGc,
) {
match retrieve_document_and_check_root_existence(documents, pipeline) {
Ok(document) => reply
@ -757,7 +754,6 @@ pub(crate) fn handle_find_elements_link_text(
document.upcast::<Node>(),
selector.clone(),
partial,
can_gc,
))
.unwrap(),
Err(error) => reply.send(Err(error)).unwrap(),
@ -896,12 +892,11 @@ pub(crate) fn handle_find_element_elements_link_text(
selector: String,
partial: bool,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
can_gc: CanGc,
) {
reply
.send(
get_known_element(documents, pipeline, element_id).and_then(|element| {
all_matching_links(element.upcast::<Node>(), selector.clone(), partial, can_gc)
all_matching_links(element.upcast::<Node>(), selector.clone(), partial)
}),
)
.unwrap();
@ -986,17 +981,11 @@ pub(crate) fn handle_find_shadow_elements_link_text(
selector: String,
partial: bool,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
can_gc: CanGc,
) {
reply
.send(
get_known_shadow_root(documents, pipeline, shadow_root_id).and_then(|shadow_root| {
all_matching_links(
shadow_root.upcast::<Node>(),
selector.clone(),
partial,
can_gc,
)
all_matching_links(shadow_root.upcast::<Node>(), selector.clone(), partial)
}),
)
.unwrap();
@ -1540,14 +1529,13 @@ pub(crate) fn handle_get_text(
pipeline: PipelineId,
node_id: String,
reply: IpcSender<Result<String, ErrorStatus>>,
can_gc: CanGc,
) {
reply
.send(
get_known_element(documents, pipeline, node_id).map(|element| {
element
.downcast::<HTMLElement>()
.map(|htmlelement| htmlelement.InnerText(can_gc).to_string())
.map(|htmlelement| htmlelement.InnerText().to_string())
.unwrap_or_else(|| {
element
.upcast::<Node>()
@ -1652,7 +1640,6 @@ pub(crate) fn handle_get_css(
node_id: String,
name: String,
reply: IpcSender<Result<String, ErrorStatus>>,
can_gc: CanGc,
) {
reply
.send(
@ -1661,7 +1648,7 @@ pub(crate) fn handle_get_css(
String::from(
window
.GetComputedStyle(&element, None)
.GetPropertyValue(DOMString::from(name), can_gc),
.GetPropertyValue(DOMString::from(name)),
)
}),
)
@ -1852,7 +1839,7 @@ pub(crate) fn handle_element_click(
can_gc,
);
if !is_element_in_view(&container, &paint_tree, can_gc) {
if !is_element_in_view(&container, &paint_tree) {
return Err(ErrorStatus::ElementNotInteractable);
}
@ -1912,7 +1899,7 @@ pub(crate) fn handle_element_click(
}
/// <https://w3c.github.io/webdriver/#dfn-in-view>
fn is_element_in_view(element: &Element, paint_tree: &[DomRoot<Element>], can_gc: CanGc) -> bool {
fn is_element_in_view(element: &Element, paint_tree: &[DomRoot<Element>]) -> bool {
// An element is in view if it is a member of its own pointer-interactable paint tree,
// given the pretense that its pointer events are not disabled.
if !paint_tree.contains(&DomRoot::from_ref(element)) {
@ -1923,7 +1910,7 @@ fn is_element_in_view(element: &Element, paint_tree: &[DomRoot<Element>], can_gc
// An element is said to have pointer events disabled
// if the resolved value of its "pointer-events" style property is "none".
let pointer_events_not_disabled = element
.style(can_gc)
.style()
.is_none_or(|style| style.get_inherited_ui().pointer_events != PointerEvents::None);
pointer_events_not_disabled
@ -1946,7 +1933,6 @@ fn get_element_pointer_interactable_paint_tree(
Some(center_point) => document.ElementsFromPoint(
Finite::wrap(center_point.x as f64),
Finite::wrap(center_point.y as f64),
can_gc,
),
None => Vec::new(),
}