Skip to main content

paint/
paint.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::{Cell, Ref, RefCell, RefMut};
6use std::collections::HashMap;
7use std::env;
8use std::fs::create_dir_all;
9use std::rc::Rc;
10#[cfg(feature = "webgl")]
11use std::thread::JoinHandle;
12use std::time::{SystemTime, UNIX_EPOCH};
13
14use bitflags::bitflags;
15use crossbeam_channel::Sender;
16use dpi::PhysicalSize;
17use embedder_traits::{
18    EventLoopWaker, InputEventAndId, InputEventId, InputEventResult, ScreenshotCaptureError,
19    Scroll, ShutdownState, ViewportDetails, WebViewPoint, WebViewRect,
20};
21use euclid::{Scale, Size2D};
22use image::RgbaImage;
23use log::{debug, warn};
24use paint_api::rendering_context::RenderingContext;
25use paint_api::{
26    PaintMessage, PaintProxy, PainterSurfmanDetails, PainterSurfmanDetailsMap,
27    WebRenderExternalImageIdManager, WebViewTrait,
28};
29use profile_traits::mem::{
30    ProcessReports, ProfilerRegistration, Report, ReportKind, perform_memory_report,
31};
32use profile_traits::path;
33use profile_traits::time::{self as profile_time};
34use servo_base::generic_channel::{self, GenericSender, RoutedReceiver};
35use servo_base::id::{PainterId, PipelineId, WebViewId};
36#[cfg(feature = "webgl")]
37use servo_canvas_traits::webgl::{WebGLContextId, WebGLThreads};
38use servo_config::pref;
39use servo_constellation_traits::EmbedderToConstellationMessage;
40use servo_geometry::DeviceIndependentPixel;
41use style_traits::CSSPixel;
42#[cfg(feature = "webgl")]
43use surfman::Device;
44#[cfg(feature = "webgl")]
45use surfman::chains::SwapChains;
46#[cfg(feature = "webgl")]
47use webgl::WebGLComm;
48#[cfg(feature = "webgl")]
49use webgl::webgl_thread::WebGLContextBusyMap;
50#[cfg(feature = "webgpu")]
51use webgpu::canvas_context::WebGpuExternalImageMap;
52use webrender::{CaptureBits, MemoryReport};
53use webrender_api::units::{DevicePixel, DevicePoint};
54use webrender_api::{FontInstanceKey, FontKey, ImageKey};
55#[cfg(feature = "webxr")]
56use webxr::WebXrRegistry;
57
58use crate::InitialPaintState;
59use crate::painter::Painter;
60use crate::webview_renderer::UnknownWebView;
61
62/// An option to control what kind of WebRender debugging is enabled while Servo is running.
63#[derive(Copy, Clone)]
64pub enum WebRenderDebugOption {
65    Profiler,
66    TextureCacheDebug,
67    RenderTargetDebug,
68}
69
70/// Keeps track of all webgl related elements
71#[cfg(feature = "webgl")]
72pub struct WebGLPaint {
73    /// A [`HashMap`] of `WebGLContextId` to a usage count. This count indicates when
74    /// WebRender is still rendering the context. This is used to ensure properly clean
75    /// up of all Surfman `Surface`s.
76    pub(crate) busy_webgl_contexts_map: WebGLContextBusyMap,
77
78    /// The [`WebGLThreads`] for this renderer.
79    webgl_threads: WebGLThreads,
80
81    /// A [`JoinHandle`] for joining the WebGL thread once the exit message is sent.
82    webgl_join_handle: Cell<Option<JoinHandle<()>>>,
83
84    /// The shared [`SwapChains`] used by [`WebGLThreads`] for this renderer.
85    pub(crate) swap_chains: SwapChains<WebGLContextId, Device>,
86}
87
88#[cfg(feature = "webgl")]
89impl WebGLPaint {
90    fn shutdown(&self) {
91        self.webgl_threads.exit();
92        if let Some(webgl_join_handle) = self.webgl_join_handle.take() &&
93            webgl_join_handle.join().is_err()
94        {
95            warn!("Could not join WebGLThread.");
96        }
97    }
98}
99
100/// [`Paint`] is Servo's rendering subsystem. It has a few responsibilities:
101///
102/// 1. Maintain a WebRender instance for each [`RenderingContext`] that Servo knows about.
103///    [`RenderingContext`]s are per-`WebView`, but more than one `WebView` can use the same
104///    [`RenderingContext`]. This allows multiple `WebView`s to share the same WebRender
105///    instance which is more efficient. This is useful for tabbed web browsers.
106/// 2. Receive display lists from the layout of all of the currently active `Pipeline`s
107///    (frames). These display lists are sent to WebRender, and new frames are generated.
108///    Once the frame is ready the [`Painter`] for the WebRender instance will ask libservo
109///    to inform the embedder that a new frame is ready so that it can trigger a paint.
110/// 3. Drive animation and animation callback updates. Animation updates should ideally be
111///    coordinated with the system vsync signal, so the `RefreshDriver` is exposed in the
112///    API to allow the embedder to do this. The [`Painter`] then asks its `WebView`s to
113///    update their rendering, which triggers layouts.
114/// 4. Eagerly handle scrolling and touch events. In order to avoid latency when handling
115///    these kind of actions, each [`Painter`] will eagerly process touch events and
116///    perform panning and zooming operations on their WebRender contents -- informing the
117///    WebView contents asynchronously.
118///
119/// `Paint` and all of its contained structs should **never** block on the Constellation,
120/// because sometimes the Constellation blocks on us.
121pub struct Paint {
122    /// All of the [`Painters`] for this [`Paint`]. Each [`Painter`] handles painting to
123    /// a single [`RenderingContext`].
124    painters: Vec<Rc<RefCell<Painter>>>,
125
126    /// A [`PaintProxy`] which can be used to allow other parts of Servo to communicate
127    /// with this [`Paint`].
128    pub(crate) paint_proxy: PaintProxy,
129
130    /// An [`EventLoopWaker`] used to wake up the main embedder event loop when the renderer needs
131    /// to run.
132    pub(crate) event_loop_waker: Box<dyn EventLoopWaker>,
133
134    /// Tracks whether we are in the process of shutting down, or have shut down and
135    /// should shut down `Paint`. This is shared with the `Servo` instance.
136    shutdown_state: Rc<Cell<ShutdownState>>,
137
138    /// The port on which we receive messages.
139    paint_receiver: RoutedReceiver<PaintMessage>,
140
141    /// The channel on which messages can be sent to the constellation.
142    pub(crate) embedder_to_constellation_sender: Sender<EmbedderToConstellationMessage>,
143
144    /// The [`WebRenderExternalImageIdManager`] used to generate new `ExternalImageId`s.
145    webrender_external_image_id_manager: WebRenderExternalImageIdManager,
146
147    /// A [`HashMap`] of [`PainterId`] to the Surfaman types (`Device`, `Adapter`) that
148    /// are specific to a particular [`Painter`].
149    pub(crate) painter_surfman_details_map: PainterSurfmanDetailsMap,
150
151    #[cfg(feature = "webgl")]
152    /// Keeps track of all webgl related elements.
153    pub(crate) webgl_paint: WebGLPaint,
154
155    /// The channel on which messages can be sent to the time profiler.
156    time_profiler_chan: profile_time::ProfilerChan,
157
158    /// A handle to the memory profiler which will automatically unregister
159    /// when it's dropped.
160    _mem_profiler_registration: ProfilerRegistration,
161
162    /// Some XR devices want to run on the main thread.
163    #[cfg(feature = "webxr")]
164    webxr_main_thread: RefCell<webxr::MainThreadRegistry>,
165
166    /// An map of external images shared between all `WebGpuExternalImages`.
167    #[cfg(feature = "webgpu")]
168    webgpu_image_map: std::cell::OnceCell<WebGpuExternalImageMap>,
169}
170
171/// Why we need to be repainted. This is used for debugging.
172#[derive(Clone, Copy, Default, PartialEq)]
173pub(crate) struct RepaintReason(u8);
174
175bitflags! {
176    impl RepaintReason: u8 {
177        /// We're performing the single repaint in headless mode.
178        const ReadyForScreenshot = 1 << 0;
179        /// We're performing a repaint to run an animation.
180        const ChangedAnimationState = 1 << 1;
181        /// A new WebRender frame has arrived.
182        const NewWebRenderFrame = 1 << 2;
183        /// The window has been resized and will need to be synchronously repainted.
184        const Resize = 1 << 3;
185        /// A fling has started and a repaint needs to happen to process the animation.
186        const StartedFlinging = 1 << 4;
187        /// A blinking text caret requires a redraw.
188        const BlinkingCaret = 1 << 5;
189        /// The observers of the refresh driver wait for a paint after the next frame start.
190        const FrameStart = 1 << 6;
191    }
192}
193
194impl Paint {
195    pub fn new(state: InitialPaintState) -> Rc<RefCell<Self>> {
196        let registration = state.mem_profiler_chan.prepare_memory_reporting(
197            "paint".into(),
198            state.paint_proxy.clone(),
199            PaintMessage::CollectMemoryReport,
200        );
201
202        let webrender_external_image_id_manager = WebRenderExternalImageIdManager::default();
203        let painter_surfman_details_map = PainterSurfmanDetailsMap::default();
204        #[cfg(feature = "webgl")]
205        let WebGLComm {
206            webgl_threads,
207            swap_chains,
208            busy_webgl_context_map,
209            #[cfg(feature = "webxr")]
210            webxr_layer_grand_manager,
211            join_handle: webgl_join_handle,
212        } = WebGLComm::new(
213            state.paint_proxy.cross_process_paint_api.clone(),
214            webrender_external_image_id_manager.clone(),
215            painter_surfman_details_map.clone(),
216        );
217
218        // Create the WebXR main thread.
219        #[cfg(feature = "webxr")]
220        let mut webxr_main_thread = webxr::MainThreadRegistry::new(
221            state.event_loop_waker.clone(),
222            webxr_layer_grand_manager,
223        )
224        .expect("Failed to create WebXR device registry");
225        #[cfg(feature = "webxr")]
226        webxr_main_thread.register_fallback(webxr::inline::InlineDiscovery);
227
228        #[cfg(feature = "webgl")]
229        let webgl_paint = WebGLPaint {
230            busy_webgl_contexts_map: busy_webgl_context_map,
231            webgl_threads,
232            webgl_join_handle: Cell::new(Some(webgl_join_handle)),
233            swap_chains,
234        };
235        Rc::new(RefCell::new(Paint {
236            painters: Default::default(),
237            paint_proxy: state.paint_proxy,
238            event_loop_waker: state.event_loop_waker,
239            shutdown_state: state.shutdown_state,
240            paint_receiver: state.receiver,
241            embedder_to_constellation_sender: state.embedder_to_constellation_sender.clone(),
242            webrender_external_image_id_manager,
243            #[cfg(feature = "webgl")]
244            webgl_paint,
245            time_profiler_chan: state.time_profiler_chan,
246            _mem_profiler_registration: registration,
247            painter_surfman_details_map,
248            #[cfg(feature = "webxr")]
249            webxr_main_thread: RefCell::new(webxr_main_thread),
250            #[cfg(feature = "webgpu")]
251            webgpu_image_map: Default::default(),
252        }))
253    }
254
255    #[cfg(feature = "webxr")]
256    pub fn register_webxr_registry(&self, registry: Box<dyn WebXrRegistry>) {
257        let mut webxr_main_thread = self.webxr_main_thread.borrow_mut();
258        registry.register(&mut webxr_main_thread)
259    }
260
261    pub fn register_rendering_context(
262        &mut self,
263        rendering_context: Rc<dyn RenderingContext>,
264    ) -> PainterId {
265        if let Some(painter_id) = self.painters.iter().find_map(|painter| {
266            let painter = painter.borrow();
267            if Rc::ptr_eq(&painter.rendering_context, &rendering_context) {
268                Some(painter.painter_id)
269            } else {
270                None
271            }
272        }) {
273            return painter_id;
274        }
275
276        let painter = Painter::new(rendering_context.clone(), self);
277        let painter_id = painter.painter_id;
278        self.painters.push(Rc::new(RefCell::new(painter)));
279
280        // These are only used to serve WebGL external images, which handles their
281        // absence from the map
282        let Some(connection) = rendering_context.connection() else {
283            warn!("The rendering context has no surfman connection, WebGL will be unavailable");
284            return painter_id;
285        };
286        let Some(adapter) = rendering_context.adapter() else {
287            warn!("Could not retrieve surfman adapter, WebGL will be unavailable");
288            return painter_id;
289        };
290
291        self.painter_surfman_details_map.insert(
292            painter_id,
293            PainterSurfmanDetails {
294                connection,
295                adapter,
296            },
297        );
298
299        painter_id
300    }
301
302    fn remove_painter(&mut self, painter_id: PainterId) {
303        // The shared details map must be removed first in order to avoid the creation of new
304        // devices after `clear_painter_resources` is called.
305        self.painter_surfman_details_map.remove(painter_id);
306
307        #[cfg(feature = "webgl")]
308        if !self
309            .webgl_paint
310            .webgl_threads
311            .clear_painter_resources(painter_id)
312        {
313            warn!("Could not clear {painter_id:?} resources in WebGLThread");
314        }
315
316        // This is called last so that the surfman `Device` is dropped on this thread.
317        self.painters
318            .retain(|painter| painter.borrow().painter_id != painter_id);
319    }
320
321    pub(crate) fn maybe_painter<'a>(&'a self, painter_id: PainterId) -> Option<Ref<'a, Painter>> {
322        self.painters
323            .iter()
324            .map(|painter| painter.borrow())
325            .find(|painter| painter.painter_id == painter_id)
326    }
327
328    pub(crate) fn painter<'a>(&'a self, painter_id: PainterId) -> Ref<'a, Painter> {
329        self.maybe_painter(painter_id)
330            .expect("painter_id not found")
331    }
332
333    pub(crate) fn maybe_painter_mut<'a>(
334        &'a self,
335        painter_id: PainterId,
336    ) -> Option<RefMut<'a, Painter>> {
337        self.painters
338            .iter()
339            .map(|painter| painter.borrow_mut())
340            .find(|painter| painter.painter_id == painter_id)
341    }
342
343    pub(crate) fn painter_mut<'a>(&'a self, painter_id: PainterId) -> RefMut<'a, Painter> {
344        self.maybe_painter_mut(painter_id)
345            .expect("painter_id not found")
346    }
347
348    pub fn painter_id(&self) -> PainterId {
349        self.painters[0].borrow().painter_id
350    }
351
352    pub fn rendering_context_size(&self, painter_id: PainterId) -> Size2D<u32, DevicePixel> {
353        self.painter(painter_id).rendering_context.size2d()
354    }
355
356    #[cfg(feature = "webgl")]
357    pub fn webgl_threads(&self) -> WebGLThreads {
358        self.webgl_paint.webgl_threads.clone()
359    }
360
361    pub fn webrender_external_image_id_manager(&self) -> WebRenderExternalImageIdManager {
362        self.webrender_external_image_id_manager.clone()
363    }
364
365    pub fn webxr_running(&self) -> bool {
366        #[cfg(feature = "webxr")]
367        {
368            self.webxr_main_thread.borrow().running()
369        }
370        #[cfg(not(feature = "webxr"))]
371        {
372            false
373        }
374    }
375
376    #[cfg(feature = "webxr")]
377    pub fn webxr_main_thread_registry(&self) -> webxr_api::Registry {
378        self.webxr_main_thread.borrow().registry()
379    }
380
381    #[cfg(feature = "webgpu")]
382    pub fn webgpu_image_map(&self) -> WebGpuExternalImageMap {
383        self.webgpu_image_map.get_or_init(Default::default).clone()
384    }
385
386    pub fn webviews_needing_repaint(&self) -> Vec<WebViewId> {
387        self.painters
388            .iter()
389            .flat_map(|painter| painter.borrow().webviews_needing_repaint())
390            .collect()
391    }
392
393    pub fn finish_shutting_down(&self) {
394        // Drain paint port, sometimes messages contain channels that are blocking
395        // another thread from finishing (i.e. SetFrameTree).
396        while self.paint_receiver.try_recv().is_ok() {}
397
398        #[cfg(feature = "webgl")]
399        self.webgl_paint.shutdown();
400
401        // Tell the profiler, memory profiler, and scrolling timer to shut down.
402        if let Some((sender, receiver)) = generic_channel::channel() {
403            self.time_profiler_chan
404                .send(profile_time::ProfilerMsg::Exit(sender));
405            let _ = receiver.recv();
406        }
407    }
408
409    fn handle_browser_message(&self, msg: PaintMessage) {
410        trace_msg_from_constellation!(msg, "{msg:?}");
411
412        match self.shutdown_state() {
413            ShutdownState::NotShuttingDown => {},
414            ShutdownState::ShuttingDown => {
415                self.handle_browser_message_while_shutting_down(msg);
416                return;
417            },
418            ShutdownState::FinishedShuttingDown => {
419                // Messages to Paint are ignored after shutdown is complete.
420                return;
421            },
422        }
423
424        match msg {
425            PaintMessage::CollectMemoryReport(sender) => {
426                self.collect_memory_report(sender);
427            },
428            PaintMessage::ChangeRunningAnimationsState(
429                webview_id,
430                pipeline_id,
431                animation_state,
432            ) => {
433                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
434                    painter.change_running_animations_state(
435                        webview_id,
436                        pipeline_id,
437                        animation_state,
438                    );
439                }
440            },
441            PaintMessage::SetFrameTreeForWebView(webview_id, frame_tree) => {
442                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
443                    painter.set_frame_tree_for_webview(&frame_tree);
444                }
445            },
446            PaintMessage::SetThrottled(webview_id, pipeline_id, throttled) => {
447                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
448                    painter.set_throttled(webview_id, pipeline_id, throttled);
449                }
450            },
451            PaintMessage::PipelineExited(webview_id, pipeline_id, pipeline_exit_source) => {
452                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
453                    painter.notify_pipeline_exited(webview_id, pipeline_id, pipeline_exit_source);
454                }
455            },
456            PaintMessage::NewWebRenderFrameReady(..) => {
457                unreachable!("New WebRender frames should be handled in the caller.");
458            },
459            PaintMessage::SendInitialTransaction(webview_id, pipeline_id) => {
460                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
461                    painter.send_initial_pipeline_transaction(webview_id, pipeline_id);
462                }
463            },
464            PaintMessage::ScrollNodeByDelta(
465                webview_id,
466                pipeline_id,
467                offset,
468                external_scroll_id,
469            ) => {
470                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
471                    painter.scroll_node_by_delta(
472                        webview_id,
473                        pipeline_id,
474                        offset,
475                        external_scroll_id,
476                    );
477                }
478            },
479            PaintMessage::ScrollViewportByDelta(webview_id, delta) => {
480                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
481                    painter.scroll_viewport_by_delta(webview_id, delta);
482                }
483            },
484            PaintMessage::UpdateEpoch {
485                webview_id,
486                pipeline_id,
487                epoch,
488            } => {
489                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
490                    painter.update_epoch(webview_id, pipeline_id, epoch);
491                }
492            },
493            PaintMessage::SendDisplayList {
494                webview_id,
495                display_list_descriptor,
496                display_list_info_receiver,
497                display_list_data_receiver,
498            } => {
499                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
500                    painter.handle_new_display_list(
501                        webview_id,
502                        display_list_descriptor,
503                        display_list_info_receiver,
504                        display_list_data_receiver,
505                    );
506                }
507            },
508            PaintMessage::GenerateFrame(painter_ids) => {
509                for painter_id in painter_ids {
510                    if let Some(mut painter) = self.maybe_painter_mut(painter_id) {
511                        painter.generate_frame_for_script();
512                    }
513                }
514            },
515            PaintMessage::GenerateImageKey(webview_id, result_sender) => {
516                self.handle_generate_image_key(webview_id, result_sender);
517            },
518            PaintMessage::GenerateImageKeysForPipeline(webview_id, pipeline_id) => {
519                self.handle_generate_image_keys_for_pipeline(webview_id, pipeline_id);
520            },
521            PaintMessage::UpdateImages(painter_id, updates) => {
522                if let Some(mut painter) = self.maybe_painter_mut(painter_id) {
523                    painter.update_images(updates);
524                }
525            },
526            PaintMessage::DelayNewFrameForCanvas(
527                webview_id,
528                pipeline_id,
529                canvas_epoch,
530                image_keys,
531            ) => {
532                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
533                    painter.delay_new_frames_for_canvas(pipeline_id, canvas_epoch, image_keys);
534                }
535            },
536            PaintMessage::AddFont(painter_id, font_key, data, index) => {
537                debug_assert!(painter_id == font_key.into());
538
539                if let Some(mut painter) = self.maybe_painter_mut(painter_id) {
540                    painter.add_font(font_key, data, index);
541                }
542            },
543            PaintMessage::AddSystemFont(painter_id, font_key, native_handle) => {
544                debug_assert!(painter_id == font_key.into());
545
546                if let Some(mut painter) = self.maybe_painter_mut(painter_id) {
547                    painter.add_system_font(font_key, native_handle);
548                }
549            },
550            PaintMessage::AddFontInstance(
551                painter_id,
552                font_instance_key,
553                font_key,
554                size,
555                flags,
556                platform_options,
557                variations,
558            ) => {
559                debug_assert!(painter_id == font_key.into());
560                debug_assert!(painter_id == font_instance_key.into());
561
562                if let Some(mut painter) = self.maybe_painter_mut(painter_id) {
563                    painter.add_font_instance(
564                        font_instance_key,
565                        font_key,
566                        size,
567                        flags,
568                        platform_options,
569                        variations,
570                    );
571                }
572            },
573            PaintMessage::RemoveFonts(painter_id, keys, instance_keys) => {
574                if let Some(mut painter) = self.maybe_painter_mut(painter_id) {
575                    painter.remove_fonts(keys, instance_keys);
576                }
577            },
578            PaintMessage::GenerateFontKeys(
579                number_of_font_keys,
580                number_of_font_instance_keys,
581                result_sender,
582                painter_id,
583            ) => {
584                self.handle_generate_font_keys(
585                    number_of_font_keys,
586                    number_of_font_instance_keys,
587                    result_sender,
588                    painter_id,
589                );
590            },
591            PaintMessage::Viewport(webview_id, viewport_description) => {
592                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
593                    painter.set_viewport_description(webview_id, viewport_description);
594                }
595            },
596            PaintMessage::ScreenshotReadinessReponse(webview_id, pipelines_and_epochs) => {
597                if let Some(painter) = self.maybe_painter(webview_id.into()) {
598                    painter.handle_screenshot_readiness_reply(webview_id, pipelines_and_epochs);
599                }
600            },
601        }
602    }
603
604    pub fn remove_webview(&mut self, webview_id: WebViewId) {
605        let painter_id = webview_id.into();
606
607        {
608            let mut painter = self.painter_mut(painter_id);
609            painter.remove_webview(webview_id);
610            if !painter.is_empty() {
611                return;
612            }
613        }
614
615        self.remove_painter(painter_id);
616    }
617
618    fn collect_memory_report(&self, sender: profile_traits::mem::ReportsChan) {
619        let mut memory_report = MemoryReport::default();
620        for painter in &self.painters {
621            memory_report += painter.borrow().report_memory();
622        }
623
624        let mut reports = vec![
625            Report {
626                path: path!["webrender", "fonts"],
627                kind: ReportKind::ExplicitJemallocHeapSize,
628                size: memory_report.fonts,
629            },
630            Report {
631                path: path!["webrender", "images"],
632                kind: ReportKind::ExplicitJemallocHeapSize,
633                size: memory_report.images,
634            },
635            Report {
636                path: path!["webrender", "display-list"],
637                kind: ReportKind::ExplicitJemallocHeapSize,
638                size: memory_report.display_list,
639            },
640        ];
641
642        perform_memory_report(|ops| {
643            let scroll_trees_memory_usage = self
644                .painters
645                .iter()
646                .map(|painter| painter.borrow().scroll_trees_memory_usage(ops))
647                .sum();
648            reports.push(Report {
649                path: path!["paint", "scroll-tree"],
650                kind: ReportKind::ExplicitJemallocHeapSize,
651                size: scroll_trees_memory_usage,
652            });
653        });
654
655        sender.send(ProcessReports::new(reports));
656    }
657
658    /// Handle messages sent to `Paint` during the shutdown process. In general,
659    /// the things `Paint` can do in this state are limited. It's very important to
660    /// answer any synchronous messages though as other threads might be waiting on the
661    /// results to finish their own shut down process. We try to do as little as possible
662    /// during this time.
663    ///
664    /// When that involves generating WebRender ids, our approach here is to simply
665    /// generate them, but assume they will never be used, since once shutting down
666    /// `Paint` no longer does any WebRender frame generation.
667    fn handle_browser_message_while_shutting_down(&self, msg: PaintMessage) {
668        match msg {
669            PaintMessage::PipelineExited(webview_id, pipeline_id, pipeline_exit_source) => {
670                if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
671                    painter.notify_pipeline_exited(webview_id, pipeline_id, pipeline_exit_source);
672                }
673            },
674            PaintMessage::GenerateImageKey(webview_id, result_sender) => {
675                self.handle_generate_image_key(webview_id, result_sender);
676            },
677            PaintMessage::GenerateImageKeysForPipeline(webview_id, pipeline_id) => {
678                self.handle_generate_image_keys_for_pipeline(webview_id, pipeline_id);
679            },
680            PaintMessage::GenerateFontKeys(
681                number_of_font_keys,
682                number_of_font_instance_keys,
683                result_sender,
684                painter_id,
685            ) => {
686                self.handle_generate_font_keys(
687                    number_of_font_keys,
688                    number_of_font_instance_keys,
689                    result_sender,
690                    painter_id,
691                );
692            },
693            _ => {
694                debug!("Ignoring message ({:?} while shutting down", msg);
695            },
696        }
697    }
698
699    pub fn add_webview(&self, webview: Box<dyn WebViewTrait>, viewport_details: ViewportDetails) {
700        self.painter_mut(webview.id().into())
701            .add_webview(webview, viewport_details);
702    }
703
704    pub fn show_webview(&self, webview_id: WebViewId) -> Result<(), UnknownWebView> {
705        self.painter_mut(webview_id.into())
706            .set_webview_hidden(webview_id, false)
707    }
708
709    pub fn hide_webview(&self, webview_id: WebViewId) -> Result<(), UnknownWebView> {
710        self.painter_mut(webview_id.into())
711            .set_webview_hidden(webview_id, true)
712    }
713
714    pub fn set_hidpi_scale_factor(
715        &self,
716        webview_id: WebViewId,
717        new_scale_factor: Scale<f32, DeviceIndependentPixel, DevicePixel>,
718    ) {
719        if self.shutdown_state() != ShutdownState::NotShuttingDown {
720            return;
721        }
722        self.painter_mut(webview_id.into())
723            .set_hidpi_scale_factor(webview_id, new_scale_factor);
724    }
725
726    pub fn resize_rendering_context(&self, webview_id: WebViewId, new_size: PhysicalSize<u32>) {
727        if self.shutdown_state() != ShutdownState::NotShuttingDown {
728            return;
729        }
730        self.painter_mut(webview_id.into())
731            .resize_rendering_context(new_size);
732    }
733
734    pub fn set_screen_size(&self, webview_id: WebViewId, new_size: Size2D<f32, DevicePixel>) {
735        if self.shutdown_state() != ShutdownState::NotShuttingDown {
736            return;
737        }
738        self.painter_mut(webview_id.into())
739            .set_screen_size(webview_id, new_size);
740    }
741
742    pub fn set_page_zoom(&self, webview_id: WebViewId, new_zoom: f32) {
743        if self.shutdown_state() != ShutdownState::NotShuttingDown {
744            return;
745        }
746        self.painter_mut(webview_id.into())
747            .set_page_zoom(webview_id, new_zoom);
748    }
749
750    pub fn page_zoom(&self, webview_id: WebViewId) -> f32 {
751        self.painter(webview_id.into()).page_zoom(webview_id)
752    }
753
754    /// Render the WebRender scene to the active `RenderingContext`.
755    pub fn render(&self, webview_id: WebViewId) {
756        self.painter_mut(webview_id.into())
757            .render(&self.time_profiler_chan);
758    }
759
760    /// Get the message receiver for this [`Paint`].
761    pub fn receiver(&self) -> &RoutedReceiver<PaintMessage> {
762        &self.paint_receiver
763    }
764
765    #[servo_tracing::instrument(skip_all)]
766    pub fn handle_messages(&self, mut messages: Vec<PaintMessage>) {
767        // Pull out the `NewWebRenderFrameReady` messages from the list of messages and handle them
768        // at the end of this function. This prevents overdraw when more than a single message of
769        // this type of received. In addition, if any of these frames need a repaint, that reflected
770        // when calling `handle_new_webrender_frame_ready`.
771        let mut saw_webrender_frame_ready_for_painter = HashMap::new();
772        messages.retain(|message| match message {
773            PaintMessage::NewWebRenderFrameReady(painter_id, _document_id, need_repaint) => {
774                if let Some(painter) = self.maybe_painter(*painter_id) {
775                    painter.decrement_pending_frames();
776                    *saw_webrender_frame_ready_for_painter
777                        .entry(*painter_id)
778                        .or_insert(*need_repaint) |= *need_repaint;
779                }
780
781                false
782            },
783            _ => true,
784        });
785
786        for message in messages {
787            self.handle_browser_message(message);
788            if self.shutdown_state() == ShutdownState::FinishedShuttingDown {
789                return;
790            }
791        }
792
793        for (painter_id, repaint_needed) in saw_webrender_frame_ready_for_painter.iter() {
794            if let Some(painter) = self.maybe_painter(*painter_id) {
795                painter.handle_new_webrender_frame_ready(*repaint_needed);
796            }
797        }
798    }
799
800    #[servo_tracing::instrument(skip_all)]
801    pub fn perform_updates(&self) -> bool {
802        if self.shutdown_state() == ShutdownState::FinishedShuttingDown {
803            return false;
804        }
805
806        // Run the WebXR main thread
807        #[cfg(feature = "webxr")]
808        self.webxr_main_thread.borrow_mut().run_one_frame();
809
810        for painter in &self.painters {
811            painter.borrow_mut().perform_updates();
812        }
813
814        self.shutdown_state() != ShutdownState::FinishedShuttingDown
815    }
816
817    pub fn toggle_webrender_debug(&self, option: WebRenderDebugOption) {
818        for painter in &self.painters {
819            painter.borrow_mut().toggle_webrender_debug(option);
820        }
821    }
822
823    pub fn capture_webrender(&self, webview_id: WebViewId) {
824        let capture_id = SystemTime::now()
825            .duration_since(UNIX_EPOCH)
826            .unwrap_or_default()
827            .as_secs()
828            .to_string();
829        let available_path = [env::current_dir(), Ok(env::temp_dir())]
830            .iter()
831            .filter_map(|val| {
832                val.as_ref()
833                    .map(|dir| dir.join("webrender-captures").join(&capture_id))
834                    .ok()
835            })
836            .find(|val| create_dir_all(val).is_ok());
837
838        let Some(capture_path) = available_path else {
839            log::error!("Couldn't create a path for WebRender captures.");
840            return;
841        };
842
843        log::info!("Saving WebRender capture to {capture_path:?}");
844        self.painter(webview_id.into())
845            .webrender_api
846            .save_capture(capture_path, CaptureBits::all());
847    }
848
849    /// Returning `false` means this is not going to reach the Constellation,
850    /// and we need to directly notify the embedder that input event is handled.
851    pub fn notify_input_event(&self, webview_id: WebViewId, event: InputEventAndId) -> bool {
852        if self.shutdown_state() != ShutdownState::NotShuttingDown {
853            return false;
854        }
855        self.painter_mut(webview_id.into())
856            .notify_input_event(webview_id, event)
857    }
858
859    pub fn notify_scroll_event(&self, webview_id: WebViewId, scroll: Scroll, point: WebViewPoint) {
860        if self.shutdown_state() != ShutdownState::NotShuttingDown {
861            return;
862        }
863        self.painter_mut(webview_id.into())
864            .notify_scroll_event(webview_id, scroll, point);
865    }
866
867    pub fn adjust_pinch_zoom(
868        &self,
869        webview_id: WebViewId,
870        pinch_zoom_delta: f32,
871        center: DevicePoint,
872    ) {
873        if self.shutdown_state() != ShutdownState::NotShuttingDown {
874            return;
875        }
876        self.painter_mut(webview_id.into())
877            .adjust_pinch_zoom(webview_id, pinch_zoom_delta, center);
878    }
879
880    pub fn pinch_zoom(&self, webview_id: WebViewId) -> f32 {
881        self.painter(webview_id.into()).pinch_zoom(webview_id)
882    }
883
884    pub fn device_pixels_per_page_pixel(
885        &self,
886        webview_id: WebViewId,
887    ) -> Scale<f32, CSSPixel, DevicePixel> {
888        self.painter_mut(webview_id.into())
889            .device_pixels_per_page_pixel(webview_id)
890    }
891
892    pub(crate) fn shutdown_state(&self) -> ShutdownState {
893        self.shutdown_state.get()
894    }
895
896    pub fn request_screenshot(
897        &self,
898        webview_id: WebViewId,
899        rect: Option<WebViewRect>,
900        callback: Box<dyn FnOnce(Result<RgbaImage, ScreenshotCaptureError>) + 'static>,
901    ) {
902        self.painter(webview_id.into())
903            .request_screenshot(webview_id, rect, callback);
904    }
905
906    pub fn notify_input_event_handled(
907        &self,
908        webview_id: WebViewId,
909        input_event_id: InputEventId,
910        result: InputEventResult,
911    ) {
912        if let Some(mut painter) = self.maybe_painter_mut(webview_id.into()) {
913            painter.notify_input_event_handled(webview_id, input_event_id, result);
914        }
915    }
916
917    /// Generate an image key from the appropriate [`Painter`] or, if it is unknown, generate
918    /// a dummy image key. The unknown case needs to be handled because requests for keys
919    /// could theoretically come after a [`Painter`] has been released. A dummy key is okay
920    /// in this case because we will never render again in that case.
921    fn handle_generate_image_key(
922        &self,
923        webview_id: WebViewId,
924        result_sender: GenericSender<ImageKey>,
925    ) {
926        let painter_id = webview_id.into();
927        let image_key = self.maybe_painter(painter_id).map_or_else(
928            || ImageKey::new(painter_id.into(), 0),
929            |painter| painter.webrender_api.generate_image_key(),
930        );
931        let _ = result_sender.send(image_key);
932    }
933
934    /// Generate image keys from the appropriate [`Painter`] or, if it is unknown, generate
935    /// dummy image keys. The unknown case needs to be handled because requests for keys
936    /// could theoretically come after a [`Painter`] has been released. A dummy key is okay
937    /// in this case because we will never render again in that case.
938    fn handle_generate_image_keys_for_pipeline(
939        &self,
940        webview_id: WebViewId,
941        pipeline_id: PipelineId,
942    ) {
943        let painter_id = webview_id.into();
944        let painter = self.maybe_painter(painter_id);
945        let image_keys = (0..pref!(image_key_batch_size))
946            .map(|_| {
947                painter.as_ref().map_or_else(
948                    || ImageKey::new(painter_id.into(), 0),
949                    |painter| painter.webrender_api.generate_image_key(),
950                )
951            })
952            .collect();
953
954        let _ = self.embedder_to_constellation_sender.send(
955            EmbedderToConstellationMessage::SendImageKeysForPipeline(pipeline_id, image_keys),
956        );
957    }
958
959    /// Generate font keys from the appropriate [`Painter`] or, if it is unknown, generate
960    /// dummy font keys. The unknown case needs to be handled because requests for keys
961    /// could theoretically come after a [`Painter`] has been released. A dummy key is okay
962    /// in this case because we will never render again in that case.
963    fn handle_generate_font_keys(
964        &self,
965        number_of_font_keys: usize,
966        number_of_font_instance_keys: usize,
967        result_sender: GenericSender<(Vec<FontKey>, Vec<FontInstanceKey>)>,
968        painter_id: PainterId,
969    ) {
970        let painter = self.maybe_painter(painter_id);
971        let font_keys = (0..number_of_font_keys)
972            .map(|_| {
973                painter.as_ref().map_or_else(
974                    || FontKey::new(painter_id.into(), 0),
975                    |painter| painter.webrender_api.generate_font_key(),
976                )
977            })
978            .collect();
979        let font_instance_keys = (0..number_of_font_instance_keys)
980            .map(|_| {
981                painter.as_ref().map_or_else(
982                    || FontInstanceKey::new(painter_id.into(), 0),
983                    |painter| painter.webrender_api.generate_font_instance_key(),
984                )
985            })
986            .collect();
987
988        let _ = result_sender.send((font_keys, font_instance_keys));
989    }
990}