mirror of
https://github.com/servo/servo.git
synced 2025-09-30 00:29:14 +01:00
script: Integrate animated image updates into ScriptThread
event loop (#38941)
Instead of manually triggering `ScriptThread::update_the_rendering`, have animated images trigger rendering updates via the `ScriptThread` event loop. This should result in fewer calls to `ScriptThread::update_the_rendering`. Testing: This should not change behavior and is thus covered by existing tests. Signed-off-by: Martin Robinson <mrobinson@igalia.com> Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
parent
e7a963cca0
commit
87fe202ded
6 changed files with 90 additions and 74 deletions
|
@ -2,7 +2,9 @@
|
|||
* 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/. */
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use compositing_traits::{ImageUpdate, SerializableImageData};
|
||||
use embedder_traits::UntrustedNodeAddress;
|
||||
|
@ -12,17 +14,21 @@ use layout_api::ImageAnimationState;
|
|||
use libc::c_void;
|
||||
use malloc_size_of::MallocSizeOf;
|
||||
use parking_lot::RwLock;
|
||||
use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
|
||||
use script_bindings::root::Dom;
|
||||
use style::dom::OpaqueNode;
|
||||
use timers::{TimerEventRequest, TimerId};
|
||||
use webrender_api::units::DeviceIntSize;
|
||||
use webrender_api::{ImageDescriptor, ImageDescriptorFlags, ImageFormat};
|
||||
|
||||
use crate::dom::bindings::cell::DomRefCell;
|
||||
use crate::dom::bindings::refcounted::Trusted;
|
||||
use crate::dom::bindings::trace::NoTrace;
|
||||
use crate::dom::node::{Node, from_untrusted_node_address};
|
||||
use crate::dom::window::Window;
|
||||
use crate::script_thread::with_script_thread;
|
||||
|
||||
#[derive(Clone, Debug, Default, JSTraceable)]
|
||||
#[derive(Clone, Default, JSTraceable)]
|
||||
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||
pub struct ImageAnimationManager {
|
||||
#[no_trace]
|
||||
|
@ -32,6 +38,10 @@ pub struct ImageAnimationManager {
|
|||
///
|
||||
/// TODO(mrobinson): This does not properly handle animating images that are in pseudo-elements.
|
||||
rooted_nodes: DomRefCell<FxHashMap<NoTrace<OpaqueNode>, Dom<Node>>>,
|
||||
|
||||
/// The [`TimerId`] of the currently scheduled animated image update callback.
|
||||
#[no_trace]
|
||||
callback_timer_id: Cell<Option<TimerId>>,
|
||||
}
|
||||
|
||||
impl MallocSizeOf for ImageAnimationManager {
|
||||
|
@ -47,19 +57,19 @@ impl ImageAnimationManager {
|
|||
self.node_to_image_map.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn next_scheduled_time(&self, now: f64) -> Option<f64> {
|
||||
fn duration_to_next_frame(&self, now: f64) -> Option<Duration> {
|
||||
self.node_to_image_map
|
||||
.read()
|
||||
.values()
|
||||
.map(|state| state.time_to_next_frame(now))
|
||||
.min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
|
||||
}
|
||||
|
||||
pub(crate) fn image_animations_present(&self) -> bool {
|
||||
!self.node_to_image_map.read().is_empty()
|
||||
.map(|state| state.duration_to_next_frame(now))
|
||||
.min()
|
||||
}
|
||||
|
||||
pub(crate) fn update_active_frames(&self, window: &Window, now: f64) {
|
||||
if self.node_to_image_map.read().is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let rooted_nodes = self.rooted_nodes.borrow();
|
||||
let updates = self
|
||||
.node_to_image_map
|
||||
|
@ -95,20 +105,26 @@ impl ImageAnimationManager {
|
|||
})
|
||||
.collect();
|
||||
window.compositor_api().update_images(updates);
|
||||
|
||||
self.maybe_schedule_animated_image_update_callback(window, now);
|
||||
}
|
||||
|
||||
/// Ensure that all nodes with animating images are rooted and unroots any nodes that
|
||||
/// no longer have an animating image. This should be called immediately after a
|
||||
/// restyle, to ensure that these addresses are still valid.
|
||||
#[allow(unsafe_code)]
|
||||
pub(crate) fn update_rooted_dom_nodes(&self) {
|
||||
pub(crate) fn update_rooted_dom_nodes(&self, window: &Window, now: f64) {
|
||||
let mut rooted_nodes = self.rooted_nodes.borrow_mut();
|
||||
let node_to_image_map = self.node_to_image_map.read();
|
||||
|
||||
let mut added_node = false;
|
||||
for opaque_node in node_to_image_map.keys() {
|
||||
let opaque_node = *opaque_node;
|
||||
if rooted_nodes.contains_key(&NoTrace(opaque_node)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
added_node = true;
|
||||
let address = UntrustedNodeAddress(opaque_node.0 as *const c_void);
|
||||
unsafe {
|
||||
rooted_nodes.insert(
|
||||
|
@ -118,6 +134,32 @@ impl ImageAnimationManager {
|
|||
};
|
||||
}
|
||||
|
||||
let length_before = rooted_nodes.len();
|
||||
rooted_nodes.retain(|node, _| node_to_image_map.contains_key(&node.0));
|
||||
|
||||
if added_node || length_before != rooted_nodes.len() {
|
||||
self.maybe_schedule_animated_image_update_callback(window, now);
|
||||
}
|
||||
}
|
||||
|
||||
fn maybe_schedule_animated_image_update_callback(&self, window: &Window, now: f64) {
|
||||
with_script_thread(|script_thread| {
|
||||
if let Some(current_timer_id) = self.callback_timer_id.take() {
|
||||
self.callback_timer_id.set(None);
|
||||
script_thread.cancel_timer(current_timer_id);
|
||||
}
|
||||
|
||||
if let Some(duration) = self.duration_to_next_frame(now) {
|
||||
let trusted_window = Trusted::new(window);
|
||||
let timer_id = script_thread.schedule_timer(TimerEventRequest {
|
||||
callback: Box::new(move || {
|
||||
let window = trusted_window.root();
|
||||
window.Document().set_has_pending_animated_image_update();
|
||||
}),
|
||||
duration,
|
||||
});
|
||||
self.callback_timer_id.set(Some(timer_id));
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue