Add retry for hit tests with expired epoch in result (#37085)

Follow up to [hit_test failed occasionally when the touch event is
sent](https://github.com/servo/servo/issues/35788#top) and
https://github.com/servo/servo/issues/36676#issuecomment-2882917136,
this PR adds a retry for hit tests with expired epoch in result.

Hit tests with outdated epoch mean it is too early to perform the hit
test.

Solution: retry hit test for the event on the next webrender frame.
The retry should guarantee that:

- Keep the correct order of events
- Retry time is not too long

Test cases: `./mach test-wpt --product servodriver -r
tests\wpt\tests\webdriver\tests\classic\element_click\events.py`

cc: @xiaochengh , @yezhizhen

---------

Signed-off-by: batu_hoang <longvatrong111@gmail.com>
This commit is contained in:
batu_hoang 2025-06-04 13:56:20 +08:00 committed by GitHub
parent 7439fa18d3
commit b422a65a00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 134 additions and 51 deletions

View file

@ -281,6 +281,11 @@ impl PipelineDetails {
} }
} }
pub enum HitTestError {
EpochMismatch,
Others,
}
impl ServoRenderer { impl ServoRenderer {
pub fn shutdown_state(&self) -> ShutdownState { pub fn shutdown_state(&self) -> ShutdownState {
self.shutdown_state.get() self.shutdown_state.get()
@ -290,15 +295,19 @@ impl ServoRenderer {
&self, &self,
point: DevicePoint, point: DevicePoint,
details_for_pipeline: impl Fn(PipelineId) -> Option<&'a PipelineDetails>, details_for_pipeline: impl Fn(PipelineId) -> Option<&'a PipelineDetails>,
) -> Option<CompositorHitTestResult> { ) -> Result<CompositorHitTestResult, HitTestError> {
self.hit_test_at_point_with_flags_and_pipeline( match self.hit_test_at_point_with_flags_and_pipeline(
point, point,
HitTestFlags::empty(), HitTestFlags::empty(),
None, None,
details_for_pipeline, details_for_pipeline,
) ) {
.first() Ok(hit_test_results) => hit_test_results
.cloned() .first()
.cloned()
.ok_or(HitTestError::Others),
Err(error) => Err(error),
}
} }
// TODO: split this into first half (global) and second half (one for whole compositor, one for webview) // TODO: split this into first half (global) and second half (one for whole compositor, one for webview)
@ -308,14 +317,15 @@ impl ServoRenderer {
flags: HitTestFlags, flags: HitTestFlags,
pipeline_id: Option<WebRenderPipelineId>, pipeline_id: Option<WebRenderPipelineId>,
details_for_pipeline: impl Fn(PipelineId) -> Option<&'a PipelineDetails>, details_for_pipeline: impl Fn(PipelineId) -> Option<&'a PipelineDetails>,
) -> Vec<CompositorHitTestResult> { ) -> Result<Vec<CompositorHitTestResult>, HitTestError> {
// DevicePoint and WorldPoint are the same for us. // DevicePoint and WorldPoint are the same for us.
let world_point = WorldPoint::from_untyped(point.to_untyped()); let world_point = WorldPoint::from_untyped(point.to_untyped());
let results = let results =
self.webrender_api self.webrender_api
.hit_test(self.webrender_document, pipeline_id, world_point, flags); .hit_test(self.webrender_document, pipeline_id, world_point, flags);
results let mut epoch_mismatch = false;
let results = results
.items .items
.iter() .iter()
.filter_map(|item| { .filter_map(|item| {
@ -323,10 +333,16 @@ impl ServoRenderer {
let details = details_for_pipeline(pipeline_id)?; let details = details_for_pipeline(pipeline_id)?;
// If the epoch in the tag does not match the current epoch of the pipeline, // If the epoch in the tag does not match the current epoch of the pipeline,
// then the hit test is against an old version of the display list and we // then the hit test is against an old version of the display list.
// should ignore this hit test for now.
match details.most_recent_display_list_epoch { match details.most_recent_display_list_epoch {
Some(epoch) if epoch.as_u16() == item.tag.1 => {}, Some(epoch) => {
if epoch.as_u16() != item.tag.1 {
// It's too early to hit test for now.
// New scene building is in progress.
epoch_mismatch = true;
return None;
}
},
_ => return None, _ => return None,
} }
@ -340,7 +356,13 @@ impl ServoRenderer {
scroll_tree_node: info.scroll_tree_node, scroll_tree_node: info.scroll_tree_node,
}) })
}) })
.collect() .collect();
if epoch_mismatch {
return Err(HitTestError::EpochMismatch);
}
Ok(results)
} }
pub(crate) fn send_transaction(&mut self, transaction: Transaction) { pub(crate) fn send_transaction(&mut self, transaction: Transaction) {
@ -619,7 +641,7 @@ impl IOCompositor {
.global .global
.borrow() .borrow()
.hit_test_at_point(point, details_for_pipeline); .hit_test_at_point(point, details_for_pipeline);
if let Some(result) = result { if let Ok(result) = result {
self.global.borrow_mut().update_cursor(point, &result); self.global.borrow_mut().update_cursor(point, &result);
} }
} }
@ -649,7 +671,7 @@ impl IOCompositor {
}; };
let dppx = webview_renderer.device_pixels_per_page_pixel(); let dppx = webview_renderer.device_pixels_per_page_pixel();
let point = dppx.transform_point(Point2D::new(x, y)); let point = dppx.transform_point(Point2D::new(x, y));
webview_renderer.dispatch_input_event( webview_renderer.dispatch_point_input_event(
InputEvent::MouseButton(MouseButtonEvent::new(action, button, point)) InputEvent::MouseButton(MouseButtonEvent::new(action, button, point))
.with_webdriver_message_id(Some(message_id)), .with_webdriver_message_id(Some(message_id)),
); );
@ -662,7 +684,7 @@ impl IOCompositor {
}; };
let dppx = webview_renderer.device_pixels_per_page_pixel(); let dppx = webview_renderer.device_pixels_per_page_pixel();
let point = dppx.transform_point(Point2D::new(x, y)); let point = dppx.transform_point(Point2D::new(x, y));
webview_renderer.dispatch_input_event( webview_renderer.dispatch_point_input_event(
InputEvent::MouseMove(MouseMoveEvent::new(point)) InputEvent::MouseMove(MouseMoveEvent::new(point))
.with_webdriver_message_id(Some(message_id)), .with_webdriver_message_id(Some(message_id)),
); );
@ -684,7 +706,7 @@ impl IOCompositor {
let scroll_delta = let scroll_delta =
dppx.transform_vector(Vector2D::new(delta_x as f32, delta_y as f32)); dppx.transform_vector(Vector2D::new(delta_x as f32, delta_y as f32));
webview_renderer webview_renderer
.dispatch_input_event(InputEvent::Wheel(WheelEvent { delta, point })); .dispatch_point_input_event(InputEvent::Wheel(WheelEvent { delta, point }));
webview_renderer.on_webdriver_wheel_action(scroll_delta, point); webview_renderer.on_webdriver_wheel_action(scroll_delta, point);
}, },
@ -792,6 +814,8 @@ impl IOCompositor {
let Some(webview_renderer) = self.webview_renderers.get_mut(webview_id) else { let Some(webview_renderer) = self.webview_renderers.get_mut(webview_id) else {
return warn!("Could not find WebView for incoming display list"); return warn!("Could not find WebView for incoming display list");
}; };
// WebRender is not ready until we receive "NewWebRenderFrameReady"
webview_renderer.webrender_frame_ready.set(false);
let pipeline_id = display_list_info.pipeline_id; let pipeline_id = display_list_info.pipeline_id;
let details = webview_renderer.ensure_pipeline_details(pipeline_id.into()); let details = webview_renderer.ensure_pipeline_details(pipeline_id.into());
@ -841,7 +865,8 @@ impl IOCompositor {
flags, flags,
pipeline, pipeline,
details_for_pipeline, details_for_pipeline,
); )
.unwrap_or_default();
let _ = sender.send(result); let _ = sender.send(result);
}, },
@ -1629,11 +1654,20 @@ impl IOCompositor {
}, },
CompositorMsg::NewWebRenderFrameReady(..) => { CompositorMsg::NewWebRenderFrameReady(..) => {
found_recomposite_msg = true; found_recomposite_msg = true;
compositor_messages.push(msg) compositor_messages.push(msg);
}, },
_ => compositor_messages.push(msg), _ => compositor_messages.push(msg),
} }
} }
if found_recomposite_msg {
// Process all pending events
self.webview_renderers.iter().for_each(|webview| {
webview.dispatch_pending_point_input_events();
webview.webrender_frame_ready.set(true);
});
}
for msg in compositor_messages { for msg in compositor_messages {
self.handle_browser_message(msg); self.handle_browser_message(msg);

View file

@ -2,9 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell; use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::collections::hash_map::Keys; use std::collections::hash_map::Keys;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc; use std::rc::Rc;
use base::id::{PipelineId, WebViewId}; use base::id::{PipelineId, WebViewId};
@ -25,7 +25,7 @@ use webrender_api::units::{
}; };
use webrender_api::{ExternalScrollId, HitTestFlags, ScrollLocation}; use webrender_api::{ExternalScrollId, HitTestFlags, ScrollLocation};
use crate::compositor::{PipelineDetails, ServoRenderer}; use crate::compositor::{HitTestError, PipelineDetails, ServoRenderer};
use crate::touch::{TouchHandler, TouchMoveAction, TouchMoveAllowed, TouchSequenceState}; use crate::touch::{TouchHandler, TouchMoveAction, TouchMoveAllowed, TouchSequenceState};
// Default viewport constraints // Default viewport constraints
@ -98,6 +98,10 @@ pub(crate) struct WebViewRenderer {
/// Whether or not this [`WebViewRenderer`] isn't throttled and has a pipeline with /// Whether or not this [`WebViewRenderer`] isn't throttled and has a pipeline with
/// active animations or animation frame callbacks. /// active animations or animation frame callbacks.
animating: bool, animating: bool,
/// Pending input events queue. Priavte and only this thread pushes events to it.
pending_point_input_events: RefCell<VecDeque<InputEvent>>,
/// WebRender is not ready between `SendDisplayList` and `WebRenderFrameReady` messages.
pub webrender_frame_ready: Cell<bool>,
} }
impl Drop for WebViewRenderer { impl Drop for WebViewRenderer {
@ -132,6 +136,8 @@ impl WebViewRenderer {
max_viewport_zoom: None, max_viewport_zoom: None,
hidpi_scale_factor: Scale::new(hidpi_scale_factor.0), hidpi_scale_factor: Scale::new(hidpi_scale_factor.0),
animating: false, animating: false,
pending_point_input_events: Default::default(),
webrender_frame_ready: Cell::default(),
} }
} }
@ -309,29 +315,89 @@ impl WebViewRenderer {
} }
} }
pub(crate) fn dispatch_input_event(&mut self, event: InputEvent) { pub(crate) fn dispatch_point_input_event(&mut self, mut event: InputEvent) -> bool {
// Events that do not need to do hit testing are sent directly to the // Events that do not need to do hit testing are sent directly to the
// constellation to filter down. // constellation to filter down.
let Some(point) = event.point() else { let Some(point) = event.point() else {
return; return false;
}; };
// Delay the event if the epoch is not synchronized yet (new frame is not ready),
// or hit test result would fail and the event is rejected anyway.
if !self.webrender_frame_ready.get() || !self.pending_point_input_events.borrow().is_empty()
{
self.pending_point_input_events
.borrow_mut()
.push_back(event);
return false;
}
// If we can't find a pipeline to send this event to, we cannot continue. // If we can't find a pipeline to send this event to, we cannot continue.
let get_pipeline_details = |pipeline_id| self.pipelines.get(&pipeline_id); let get_pipeline_details = |pipeline_id| self.pipelines.get(&pipeline_id);
let Some(result) = self let result = match self
.global .global
.borrow() .borrow()
.hit_test_at_point(point, get_pipeline_details) .hit_test_at_point(point, get_pipeline_details)
else { {
return; Ok(hit_test_results) => hit_test_results,
Err(HitTestError::EpochMismatch) => {
self.pending_point_input_events
.borrow_mut()
.push_back(event);
return false;
},
_ => {
return false;
},
}; };
self.global.borrow_mut().update_cursor(point, &result); match event {
InputEvent::Touch(ref mut touch_event) => {
touch_event.init_sequence_id(self.touch_handler.current_sequence_id);
},
InputEvent::MouseButton(_) | InputEvent::MouseMove(_) | InputEvent::Wheel(_) => {
self.global.borrow_mut().update_cursor(point, &result);
},
_ => unreachable!("Unexpected input event type: {event:?}"),
}
if let Err(error) = self.global.borrow().constellation_sender.send( if let Err(error) = self.global.borrow().constellation_sender.send(
EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, Some(result)), EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, Some(result)),
) { ) {
warn!("Sending event to constellation failed ({error:?})."); warn!("Sending event to constellation failed ({error:?}).");
false
} else {
true
}
}
pub(crate) fn dispatch_pending_point_input_events(&self) {
while let Some(event) = self.pending_point_input_events.borrow_mut().pop_front() {
// Events that do not need to do hit testing are sent directly to the
// constellation to filter down.
let Some(point) = event.point() else {
continue;
};
// If we can't find a pipeline to send this event to, we cannot continue.
let get_pipeline_details = |pipeline_id| self.pipelines.get(&pipeline_id);
let Ok(result) = self
.global
.borrow()
.hit_test_at_point(point, get_pipeline_details)
else {
// Don't need to process pending input events in this frame any more.
// TODO: Add multiple retry later if needed.
return;
};
self.global.borrow_mut().update_cursor(point, &result);
if let Err(error) = self.global.borrow().constellation_sender.send(
EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, Some(result)),
) {
warn!("Sending event to constellation failed ({error:?}).");
}
} }
} }
@ -401,29 +467,11 @@ impl WebViewRenderer {
} }
} }
self.dispatch_input_event(event); self.dispatch_point_input_event(event);
} }
fn send_touch_event(&self, mut event: TouchEvent) -> bool { fn send_touch_event(&mut self, event: TouchEvent) -> bool {
let get_pipeline_details = |pipeline_id| self.pipelines.get(&pipeline_id); self.dispatch_point_input_event(InputEvent::Touch(event))
let Some(result) = self
.global
.borrow()
.hit_test_at_point(event.point, get_pipeline_details)
else {
return false;
};
event.init_sequence_id(self.touch_handler.current_sequence_id);
let event = InputEvent::Touch(event);
if let Err(e) = self.global.borrow().constellation_sender.send(
EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, Some(result)),
) {
warn!("Sending event to constellation failed ({:?}).", e);
false
} else {
true
}
} }
pub(crate) fn on_touch_event(&mut self, event: TouchEvent) { pub(crate) fn on_touch_event(&mut self, event: TouchEvent) {
@ -687,13 +735,13 @@ impl WebViewRenderer {
/// <http://w3c.github.io/touch-events/#mouse-events> /// <http://w3c.github.io/touch-events/#mouse-events>
fn simulate_mouse_click(&mut self, point: DevicePoint) { fn simulate_mouse_click(&mut self, point: DevicePoint) {
let button = MouseButton::Left; let button = MouseButton::Left;
self.dispatch_input_event(InputEvent::MouseMove(MouseMoveEvent::new(point))); self.dispatch_point_input_event(InputEvent::MouseMove(MouseMoveEvent::new(point)));
self.dispatch_input_event(InputEvent::MouseButton(MouseButtonEvent::new( self.dispatch_point_input_event(InputEvent::MouseButton(MouseButtonEvent::new(
MouseButtonAction::Down, MouseButtonAction::Down,
button, button,
point, point,
))); )));
self.dispatch_input_event(InputEvent::MouseButton(MouseButtonEvent::new( self.dispatch_point_input_event(InputEvent::MouseButton(MouseButtonEvent::new(
MouseButtonAction::Up, MouseButtonAction::Up,
button, button,
point, point,
@ -858,7 +906,8 @@ impl WebViewRenderer {
HitTestFlags::FIND_ALL, HitTestFlags::FIND_ALL,
None, None,
get_pipeline_details, get_pipeline_details,
); )
.unwrap_or_default();
// Iterate through all hit test results, processing only the first node of each pipeline. // Iterate through all hit test results, processing only the first node of each pipeline.
// This is needed to propagate the scroll events from a pipeline representing an iframe to // This is needed to propagate the scroll events from a pipeline representing an iframe to