script/
image_animation.rs1use std::cell::Cell;
6use std::sync::Arc;
7use std::time::Duration;
8
9use layout_api::AnimatingImages;
10use malloc_size_of::MallocSizeOf;
11use paint_api::ImageUpdate;
12use parking_lot::RwLock;
13use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
14use timers::{TimerEventRequest, TimerId};
15
16use crate::dom::bindings::refcounted::Trusted;
17use crate::dom::node::Node;
18use crate::dom::window::Window;
19use crate::script_thread::with_script_thread;
20
21#[derive(Clone, Default, JSTraceable)]
22#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
23pub struct ImageAnimationManager {
24 #[no_trace]
27 animating_images: Arc<RwLock<AnimatingImages>>,
28
29 #[no_trace]
31 callback_timer_id: Cell<Option<TimerId>>,
32}
33
34impl MallocSizeOf for ImageAnimationManager {
35 fn size_of(&self, ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
36 (*self.animating_images.read()).size_of(ops)
37 }
38}
39
40impl ImageAnimationManager {
41 pub(crate) fn animating_images(&self) -> Arc<RwLock<AnimatingImages>> {
42 self.animating_images.clone()
43 }
44
45 fn duration_to_next_frame(&self, now: f64) -> Option<Duration> {
46 self.animating_images
47 .read()
48 .node_to_state_map
49 .values()
50 .map(|state| state.duration_to_next_frame(now))
51 .min()
52 }
53
54 pub(crate) fn update_active_frames(&self, window: &Window, now: f64) {
55 if self.animating_images.read().is_empty() {
56 return;
57 }
58
59 let updates = self
60 .animating_images
61 .write()
62 .node_to_state_map
63 .values_mut()
64 .filter_map(|state| {
65 if !state.update_frame_for_animation_timeline_value(now) {
66 return None;
67 }
68
69 let image = &state.image;
70 let frame = image
71 .frame_data(state.active_frame)
72 .expect("No frame found")
73 .clone();
74 if let Some(mut descriptor) =
75 image.webrender_image_descriptor_and_offset_for_frame()
76 {
77 descriptor.offset = frame.byte_range.start as i32;
78 Some(ImageUpdate::UpdateImageForAnimation(
79 image.id.unwrap(),
80 descriptor,
81 ))
82 } else {
83 error!("Doing normal image update which will be slow!");
84 None
85 }
86 })
87 .collect();
88 window
89 .paint_api()
90 .update_images(window.webview_id().into(), updates);
91
92 self.maybe_schedule_update(window, now);
93 }
94
95 pub(crate) fn maybe_schedule_update_after_layout(&self, window: &Window, now: f64) {
98 if self.animating_images().write().clear_dirty() {
99 self.maybe_schedule_update(window, now);
100 }
101 }
102
103 fn maybe_schedule_update(&self, window: &Window, now: f64) {
104 with_script_thread(|script_thread| {
105 if let Some(current_timer_id) = self.callback_timer_id.take() {
106 self.callback_timer_id.set(None);
107 script_thread.cancel_timer(current_timer_id);
108 }
109
110 if let Some(duration) = self.duration_to_next_frame(now) {
111 let trusted_window = Trusted::new(window);
112 let timer_id = script_thread.schedule_timer(TimerEventRequest {
113 callback: Box::new(move || {
114 let window = trusted_window.root();
115 window.Document().set_has_pending_animated_image_update();
116 }),
117 duration,
118 });
119 self.callback_timer_id.set(Some(timer_id));
120 }
121 })
122 }
123
124 pub(crate) fn cancel_animations_for_node(&self, node: &Node) {
125 self.animating_images().write().remove(node.to_opaque());
126 }
127}