compositing_traits/
lib.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
5//! The interface to the `compositing` crate.
6
7use std::fmt::{Debug, Error, Formatter};
8
9use base::Epoch;
10use base::id::{PainterId, PipelineId, WebViewId};
11use crossbeam_channel::Sender;
12use embedder_traits::{AnimationState, EventLoopWaker};
13use log::warn;
14use malloc_size_of_derive::MallocSizeOf;
15use rustc_hash::FxHashMap;
16use smallvec::SmallVec;
17use strum_macros::IntoStaticStr;
18use webrender_api::{DocumentId, FontVariation};
19
20pub mod display_list;
21pub mod largest_contentful_paint_candidate;
22pub mod rendering_context;
23pub mod viewport_description;
24
25use std::sync::{Arc, Mutex};
26
27use base::generic_channel::{self, GenericCallback, GenericSender};
28use bitflags::bitflags;
29use display_list::CompositorDisplayListInfo;
30use embedder_traits::ScreenGeometry;
31use euclid::default::Size2D as UntypedSize2D;
32use ipc_channel::ipc::{self, IpcSharedMemory};
33use profile_traits::mem::{OpaqueSender, ReportsChan};
34use serde::{Deserialize, Serialize};
35pub use webrender_api::ExternalImageSource;
36use webrender_api::units::{LayoutVector2D, TexelRect};
37use webrender_api::{
38    BuiltDisplayList, BuiltDisplayListDescriptor, ExternalImage, ExternalImageData,
39    ExternalImageHandler, ExternalImageId, ExternalScrollId, FontInstanceFlags, FontInstanceKey,
40    FontKey, ImageData, ImageDescriptor, ImageKey, NativeFontHandle,
41    PipelineId as WebRenderPipelineId,
42};
43
44use crate::largest_contentful_paint_candidate::LCPCandidate;
45use crate::viewport_description::ViewportDescription;
46
47/// Sends messages to the compositor.
48#[derive(Clone)]
49pub struct CompositorProxy {
50    pub sender: Sender<Result<CompositorMsg, ipc_channel::Error>>,
51    /// Access to [`Self::sender`] that is possible to send across an IPC
52    /// channel. These messages are routed via the router thread to
53    /// [`Self::sender`].
54    pub cross_process_compositor_api: CrossProcessCompositorApi,
55    pub event_loop_waker: Box<dyn EventLoopWaker>,
56}
57
58impl OpaqueSender<CompositorMsg> for CompositorProxy {
59    fn send(&self, message: CompositorMsg) {
60        CompositorProxy::send(self, message)
61    }
62}
63
64impl CompositorProxy {
65    pub fn send(&self, msg: CompositorMsg) {
66        self.route_msg(Ok(msg))
67    }
68
69    /// Helper method to route a deserialized IPC message to the receiver.
70    ///
71    /// This method is a temporary solution, and will be removed when migrating
72    /// to `GenericChannel`.
73    pub fn route_msg(&self, msg: Result<CompositorMsg, ipc_channel::Error>) {
74        if let Err(err) = self.sender.send(msg) {
75            warn!("Failed to send response ({:?}).", err);
76        }
77        self.event_loop_waker.wake();
78    }
79}
80
81/// Messages from (or via) the constellation thread to the compositor.
82#[derive(Deserialize, IntoStaticStr, Serialize)]
83pub enum CompositorMsg {
84    /// Alerts the compositor that the given pipeline has changed whether it is running animations.
85    ChangeRunningAnimationsState(WebViewId, PipelineId, AnimationState),
86    /// Create or update a webview, given its frame tree.
87    CreateOrUpdateWebView(SendableFrameTree),
88    /// Remove a webview.
89    RemoveWebView(WebViewId),
90    /// Set whether to use less resources by stopping animations.
91    SetThrottled(WebViewId, PipelineId, bool),
92    /// WebRender has produced a new frame. This message informs the compositor that
93    /// the frame is ready. It contains a bool to indicate if it needs to composite, the
94    /// `DocumentId` of the new frame and the `PainterId` of the associated painter.
95    NewWebRenderFrameReady(PainterId, DocumentId, bool),
96    /// Script or the Constellation is notifying the renderer that a Pipeline has finished
97    /// shutting down. The renderer will not discard the Pipeline until both report that
98    /// they have fully shut it down, to avoid recreating it due to any subsequent
99    /// messages.
100    PipelineExited(WebViewId, PipelineId, PipelineExitSource),
101    /// Inform WebRender of the existence of this pipeline.
102    SendInitialTransaction(WebViewId, WebRenderPipelineId),
103    /// Scroll the given node ([`ExternalScrollId`]) by the provided delta. This
104    /// will only adjust the node's scroll position and will *not* do panning in
105    /// the pinch zoom viewport.
106    ScrollNodeByDelta(
107        WebViewId,
108        WebRenderPipelineId,
109        LayoutVector2D,
110        ExternalScrollId,
111    ),
112    /// Scroll the WebView's viewport by the given delta. This will also do panning
113    /// in the pinch zoom viewport if possible and the remaining delta will be used
114    /// to scroll the root layer.
115    ScrollViewportByDelta(WebViewId, LayoutVector2D),
116    /// Update the rendering epoch of the given `Pipeline`.
117    UpdateEpoch {
118        /// The [`WebViewId`] that this display list belongs to.
119        webview_id: WebViewId,
120        /// The [`PipelineId`] of the `Pipeline` to update.
121        pipeline_id: PipelineId,
122        /// The new [`Epoch`] value.
123        epoch: Epoch,
124    },
125    /// Inform WebRender of a new display list for the given pipeline.
126    SendDisplayList {
127        /// The [`WebViewId`] that this display list belongs to.
128        webview_id: WebViewId,
129        /// A descriptor of this display list used to construct this display list from raw data.
130        display_list_descriptor: BuiltDisplayListDescriptor,
131        /// An [ipc::IpcBytesReceiver] used to send the raw data of the display list.
132        display_list_receiver: ipc::IpcBytesReceiver,
133    },
134    /// Ask the renderer to generate a frame for the current set of display lists that
135    /// have been sent to the renderer.
136    GenerateFrame,
137    /// Create a new image key. The result will be returned via the
138    /// provided channel sender.
139    GenerateImageKey(GenericSender<ImageKey>),
140    /// The same as the above but it will be forwarded to the pipeline instead
141    /// of send via a channel.
142    GenerateImageKeysForPipeline(PipelineId),
143    /// Perform a resource update operation.
144    UpdateImages(SmallVec<[ImageUpdate; 1]>),
145    /// Pause all pipeline display list processing for the given pipeline until the
146    /// following image updates have been received. This is used to ensure that canvas
147    /// elements have had a chance to update their rendering and send the image update to
148    /// the renderer before their associated display list is actually displayed.
149    DelayNewFrameForCanvas(PipelineId, Epoch, Vec<ImageKey>),
150
151    /// Generate a new batch of font keys which can be used to allocate
152    /// keys asynchronously.
153    GenerateFontKeys(
154        usize,
155        usize,
156        GenericSender<(Vec<FontKey>, Vec<FontInstanceKey>)>,
157        PainterId,
158    ),
159    /// Add a font with the given data and font key.
160    AddFont(FontKey, Arc<IpcSharedMemory>, u32),
161    /// Add a system font with the given font key and handle.
162    AddSystemFont(FontKey, NativeFontHandle),
163    /// Add an instance of a font with the given instance key.
164    AddFontInstance(
165        FontInstanceKey,
166        FontKey,
167        f32,
168        FontInstanceFlags,
169        Vec<FontVariation>,
170    ),
171    /// Remove the given font resources from our WebRender instance.
172    RemoveFonts(Vec<FontKey>, Vec<FontInstanceKey>),
173    /// Measure the current memory usage associated with the compositor.
174    /// The report must be sent on the provided channel once it's complete.
175    CollectMemoryReport(ReportsChan),
176    /// A top-level frame has parsed a viewport metatag and is sending the new constraints.
177    Viewport(WebViewId, ViewportDescription),
178    /// Let the compositor know that the given WebView is ready to have a screenshot taken
179    /// after the given pipeline's epochs have been rendered.
180    ScreenshotReadinessReponse(WebViewId, FxHashMap<PipelineId, Epoch>),
181    /// The candidate of largest-contentful-paint
182    SendLCPCandidate(LCPCandidate, WebViewId, PipelineId, Epoch),
183}
184
185impl Debug for CompositorMsg {
186    fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
187        let string: &'static str = self.into();
188        write!(formatter, "{string}")
189    }
190}
191
192#[derive(Deserialize, Serialize)]
193pub struct SendableFrameTree {
194    pub pipeline: CompositionPipeline,
195    pub children: Vec<SendableFrameTree>,
196}
197
198/// The subset of the pipeline that is needed for layer composition.
199#[derive(Clone, Deserialize, Serialize)]
200pub struct CompositionPipeline {
201    pub id: PipelineId,
202    pub webview_id: WebViewId,
203}
204
205/// A mechanism to send messages from ScriptThread to the parent process' WebRender instance.
206#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
207pub struct CrossProcessCompositorApi(GenericCallback<CompositorMsg>);
208
209impl CrossProcessCompositorApi {
210    /// Create a new [`CrossProcessCompositorApi`] struct.
211    pub fn new(callback: GenericCallback<CompositorMsg>) -> Self {
212        CrossProcessCompositorApi(callback)
213    }
214
215    /// Create a new [`CrossProcessCompositorApi`] struct that does not have a listener on the other
216    /// end to use for unit testing.
217    pub fn dummy() -> Self {
218        let callback = GenericCallback::new(|_msg| ()).unwrap();
219        Self(callback)
220    }
221
222    /// Inform WebRender of the existence of this pipeline.
223    pub fn send_initial_transaction(&self, webview_id: WebViewId, pipeline: WebRenderPipelineId) {
224        if let Err(e) = self
225            .0
226            .send(CompositorMsg::SendInitialTransaction(webview_id, pipeline))
227        {
228            warn!("Error sending initial transaction: {}", e);
229        }
230    }
231
232    /// Scroll the given node ([`ExternalScrollId`]) by the provided delta. This
233    /// will only adjust the node's scroll position and will *not* do panning in
234    /// the pinch zoom viewport.
235    pub fn scroll_node_by_delta(
236        &self,
237        webview_id: WebViewId,
238        pipeline_id: WebRenderPipelineId,
239        delta: LayoutVector2D,
240        scroll_id: ExternalScrollId,
241    ) {
242        if let Err(error) = self.0.send(CompositorMsg::ScrollNodeByDelta(
243            webview_id,
244            pipeline_id,
245            delta,
246            scroll_id,
247        )) {
248            warn!("Error scrolling node: {error}");
249        }
250    }
251
252    /// Scroll the WebView's viewport by the given delta. This will also do panning
253    /// in the pinch zoom viewport if possible and the remaining delta will be used
254    /// to scroll the root layer.
255    ///
256    /// Note the value provided here is in `DeviceIndependentPixels` and will first be
257    /// converted to `DevicePixels` by the renderer.
258    pub fn scroll_viewport_by_delta(&self, webview_id: WebViewId, delta: LayoutVector2D) {
259        if let Err(error) = self
260            .0
261            .send(CompositorMsg::ScrollViewportByDelta(webview_id, delta))
262        {
263            warn!("Error scroll viewport: {error}");
264        }
265    }
266
267    pub fn delay_new_frame_for_canvas(
268        &self,
269        pipeline_id: PipelineId,
270        canvas_epoch: Epoch,
271        image_keys: Vec<ImageKey>,
272    ) {
273        if let Err(error) = self.0.send(CompositorMsg::DelayNewFrameForCanvas(
274            pipeline_id,
275            canvas_epoch,
276            image_keys,
277        )) {
278            warn!("Error delaying frames for canvas image updates {error:?}");
279        }
280    }
281
282    /// Inform the renderer that the rendering epoch has advanced. This typically happens after
283    /// a new display list is sent and/or canvas and animated images are updated.
284    pub fn update_epoch(&self, webview_id: WebViewId, pipeline_id: PipelineId, epoch: Epoch) {
285        if let Err(error) = self.0.send(CompositorMsg::UpdateEpoch {
286            webview_id,
287            pipeline_id,
288            epoch,
289        }) {
290            warn!("Error updating epoch for pipeline: {error:?}");
291        }
292    }
293
294    /// Inform WebRender of a new display list for the given pipeline.
295    pub fn send_display_list(
296        &self,
297        webview_id: WebViewId,
298        display_list_info: &CompositorDisplayListInfo,
299        list: BuiltDisplayList,
300    ) {
301        let (display_list_data, display_list_descriptor) = list.into_data();
302        let (display_list_sender, display_list_receiver) = ipc::bytes_channel().unwrap();
303        if let Err(e) = self.0.send(CompositorMsg::SendDisplayList {
304            webview_id,
305            display_list_descriptor,
306            display_list_receiver,
307        }) {
308            warn!("Error sending display list: {}", e);
309        }
310
311        let display_list_info_serialized =
312            bincode::serialize(&display_list_info).unwrap_or_default();
313        if let Err(error) = display_list_sender.send(&display_list_info_serialized) {
314            warn!("Error sending display list info: {error}");
315        }
316
317        if let Err(error) = display_list_sender.send(&display_list_data.items_data) {
318            warn!("Error sending display list items: {error}");
319        }
320        if let Err(error) = display_list_sender.send(&display_list_data.cache_data) {
321            warn!("Error sending display list cache data: {error}");
322        }
323        if let Err(error) = display_list_sender.send(&display_list_data.spatial_tree) {
324            warn!("Error sending display spatial tree: {error}");
325        }
326    }
327
328    /// Send the largest contentful paint candidate to the compositor.
329    pub fn send_lcp_candidate(
330        &self,
331        lcp_candidate: LCPCandidate,
332        webview_id: WebViewId,
333        pipeline_id: PipelineId,
334        epoch: Epoch,
335    ) {
336        if let Err(error) = self.0.send(CompositorMsg::SendLCPCandidate(
337            lcp_candidate,
338            webview_id,
339            pipeline_id,
340            epoch,
341        )) {
342            warn!("Error sending LCPCandidate: {error}");
343        }
344    }
345
346    /// Ask the Servo renderer to generate a new frame after having new display lists.
347    pub fn generate_frame(&self) {
348        if let Err(error) = self.0.send(CompositorMsg::GenerateFrame) {
349            warn!("Error generating frame: {error}");
350        }
351    }
352
353    /// Create a new image key. Blocks until the key is available.
354    pub fn generate_image_key_blocking(&self) -> Option<ImageKey> {
355        let (sender, receiver) = generic_channel::channel().unwrap();
356        self.0.send(CompositorMsg::GenerateImageKey(sender)).ok()?;
357        receiver.recv().ok()
358    }
359
360    /// Sends a message to the compositor for creating new image keys.
361    /// The compositor will then send a batch of keys over the constellation to the script_thread
362    /// and the appropriate pipeline.
363    pub fn generate_image_key_async(&self, pipeline_id: PipelineId) {
364        if let Err(e) = self
365            .0
366            .send(CompositorMsg::GenerateImageKeysForPipeline(pipeline_id))
367        {
368            warn!("Could not send image keys to Compositor {}", e);
369        }
370    }
371
372    pub fn add_image(
373        &self,
374        key: ImageKey,
375        descriptor: ImageDescriptor,
376        data: SerializableImageData,
377    ) {
378        self.update_images([ImageUpdate::AddImage(key, descriptor, data)].into());
379    }
380
381    pub fn update_image(
382        &self,
383        key: ImageKey,
384        descriptor: ImageDescriptor,
385        data: SerializableImageData,
386        epoch: Option<Epoch>,
387    ) {
388        self.update_images([ImageUpdate::UpdateImage(key, descriptor, data, epoch)].into());
389    }
390
391    pub fn delete_image(&self, key: ImageKey) {
392        self.update_images([ImageUpdate::DeleteImage(key)].into());
393    }
394
395    /// Perform an image resource update operation.
396    pub fn update_images(&self, updates: SmallVec<[ImageUpdate; 1]>) {
397        if let Err(e) = self.0.send(CompositorMsg::UpdateImages(updates)) {
398            warn!("error sending image updates: {}", e);
399        }
400    }
401
402    pub fn remove_unused_font_resources(
403        &self,
404        keys: Vec<FontKey>,
405        instance_keys: Vec<FontInstanceKey>,
406    ) {
407        if keys.is_empty() && instance_keys.is_empty() {
408            return;
409        }
410        let _ = self.0.send(CompositorMsg::RemoveFonts(keys, instance_keys));
411    }
412
413    pub fn add_font_instance(
414        &self,
415        font_instance_key: FontInstanceKey,
416        font_key: FontKey,
417        size: f32,
418        flags: FontInstanceFlags,
419        variations: Vec<FontVariation>,
420    ) {
421        let _x = self.0.send(CompositorMsg::AddFontInstance(
422            font_instance_key,
423            font_key,
424            size,
425            flags,
426            variations,
427        ));
428    }
429
430    pub fn add_font(&self, font_key: FontKey, data: Arc<IpcSharedMemory>, index: u32) {
431        let _ = self.0.send(CompositorMsg::AddFont(font_key, data, index));
432    }
433
434    pub fn add_system_font(&self, font_key: FontKey, handle: NativeFontHandle) {
435        let _ = self.0.send(CompositorMsg::AddSystemFont(font_key, handle));
436    }
437
438    pub fn fetch_font_keys(
439        &self,
440        number_of_font_keys: usize,
441        number_of_font_instance_keys: usize,
442        painter_id: PainterId,
443    ) -> (Vec<FontKey>, Vec<FontInstanceKey>) {
444        let (sender, receiver) = generic_channel::channel().expect("Could not create IPC channel");
445        let _ = self.0.send(CompositorMsg::GenerateFontKeys(
446            number_of_font_keys,
447            number_of_font_instance_keys,
448            sender,
449            painter_id,
450        ));
451        receiver.recv().unwrap()
452    }
453
454    pub fn viewport(&self, webview_id: WebViewId, description: ViewportDescription) {
455        let _ = self
456            .0
457            .send(CompositorMsg::Viewport(webview_id, description));
458    }
459
460    pub fn pipeline_exited(
461        &self,
462        webview_id: WebViewId,
463        pipeline_id: PipelineId,
464        source: PipelineExitSource,
465    ) {
466        let _ = self.0.send(CompositorMsg::PipelineExited(
467            webview_id,
468            pipeline_id,
469            source,
470        ));
471    }
472}
473
474/// This trait is used as a bridge between the different GL clients
475/// in Servo that handles WebRender ExternalImages and the WebRender
476/// ExternalImageHandler API.
477//
478/// This trait is used to notify lock/unlock messages and get the
479/// required info that WR needs.
480pub trait WebRenderExternalImageApi {
481    fn lock(&mut self, id: u64) -> (ExternalImageSource<'_>, UntypedSize2D<i32>);
482    fn unlock(&mut self, id: u64);
483}
484
485/// Type of WebRender External Image Handler.
486pub enum WebRenderImageHandlerType {
487    WebGl,
488    Media,
489    WebGpu,
490}
491
492/// List of WebRender external images to be shared among all external image
493/// consumers (WebGL, Media, WebGPU).
494/// It ensures that external image identifiers are unique.
495#[derive(Default)]
496pub struct WebRenderExternalImageRegistry {
497    /// Map of all generated external images.
498    external_images: FxHashMap<ExternalImageId, WebRenderImageHandlerType>,
499    /// Id generator for the next external image identifier.
500    next_image_id: u64,
501}
502
503impl WebRenderExternalImageRegistry {
504    pub fn next_id(&mut self, handler_type: WebRenderImageHandlerType) -> ExternalImageId {
505        self.next_image_id += 1;
506        let key = ExternalImageId(self.next_image_id);
507        self.external_images.insert(key, handler_type);
508        key
509    }
510
511    pub fn remove(&mut self, key: &ExternalImageId) {
512        self.external_images.remove(key);
513    }
514
515    pub fn get(&self, key: &ExternalImageId) -> Option<&WebRenderImageHandlerType> {
516        self.external_images.get(key)
517    }
518}
519
520/// WebRender External Image Handler implementation.
521pub struct WebRenderExternalImageHandlers {
522    /// WebGL handler.
523    webgl_handler: Option<Box<dyn WebRenderExternalImageApi>>,
524    /// Media player handler.
525    media_handler: Option<Box<dyn WebRenderExternalImageApi>>,
526    /// WebGPU handler.
527    webgpu_handler: Option<Box<dyn WebRenderExternalImageApi>>,
528    /// WebRender external images.
529    external_images: Arc<Mutex<WebRenderExternalImageRegistry>>,
530}
531
532impl WebRenderExternalImageHandlers {
533    pub fn new() -> (Self, Arc<Mutex<WebRenderExternalImageRegistry>>) {
534        let external_images = Arc::new(Mutex::new(WebRenderExternalImageRegistry::default()));
535        (
536            Self {
537                webgl_handler: None,
538                media_handler: None,
539                webgpu_handler: None,
540                external_images: external_images.clone(),
541            },
542            external_images,
543        )
544    }
545
546    pub fn set_handler(
547        &mut self,
548        handler: Box<dyn WebRenderExternalImageApi>,
549        handler_type: WebRenderImageHandlerType,
550    ) {
551        match handler_type {
552            WebRenderImageHandlerType::WebGl => self.webgl_handler = Some(handler),
553            WebRenderImageHandlerType::Media => self.media_handler = Some(handler),
554            WebRenderImageHandlerType::WebGpu => self.webgpu_handler = Some(handler),
555        }
556    }
557}
558
559impl ExternalImageHandler for WebRenderExternalImageHandlers {
560    /// Lock the external image. Then, WR could start to read the
561    /// image content.
562    /// The WR client should not change the image content until the
563    /// unlock() call.
564    fn lock(
565        &mut self,
566        key: ExternalImageId,
567        _channel_index: u8,
568        _is_composited: bool,
569    ) -> ExternalImage<'_> {
570        let external_images = self.external_images.lock().unwrap();
571        let handler_type = external_images
572            .get(&key)
573            .expect("Tried to get unknown external image");
574        match handler_type {
575            WebRenderImageHandlerType::WebGl => {
576                let (source, size) = self.webgl_handler.as_mut().unwrap().lock(key.0);
577                let texture_id = match source {
578                    ExternalImageSource::NativeTexture(b) => b,
579                    _ => panic!("Wrong type"),
580                };
581                ExternalImage {
582                    uv: TexelRect::new(0.0, size.height as f32, size.width as f32, 0.0),
583                    source: ExternalImageSource::NativeTexture(texture_id),
584                }
585            },
586            WebRenderImageHandlerType::Media => {
587                let (source, size) = self.media_handler.as_mut().unwrap().lock(key.0);
588                let texture_id = match source {
589                    ExternalImageSource::NativeTexture(b) => b,
590                    _ => panic!("Wrong type"),
591                };
592                ExternalImage {
593                    uv: TexelRect::new(0.0, size.height as f32, size.width as f32, 0.0),
594                    source: ExternalImageSource::NativeTexture(texture_id),
595                }
596            },
597            WebRenderImageHandlerType::WebGpu => {
598                let (source, size) = self.webgpu_handler.as_mut().unwrap().lock(key.0);
599                ExternalImage {
600                    uv: TexelRect::new(0.0, size.height as f32, size.width as f32, 0.0),
601                    source,
602                }
603            },
604        }
605    }
606
607    /// Unlock the external image. The WR should not read the image
608    /// content after this call.
609    fn unlock(&mut self, key: ExternalImageId, _channel_index: u8) {
610        let external_images = self.external_images.lock().unwrap();
611        let handler_type = external_images
612            .get(&key)
613            .expect("Tried to get unknown external image");
614        match handler_type {
615            WebRenderImageHandlerType::WebGl => self.webgl_handler.as_mut().unwrap().unlock(key.0),
616            WebRenderImageHandlerType::Media => self.media_handler.as_mut().unwrap().unlock(key.0),
617            WebRenderImageHandlerType::WebGpu => {
618                self.webgpu_handler.as_mut().unwrap().unlock(key.0)
619            },
620        };
621    }
622}
623
624#[derive(Deserialize, Serialize)]
625/// Serializable image updates that must be performed by WebRender.
626pub enum ImageUpdate {
627    /// Register a new image.
628    AddImage(ImageKey, ImageDescriptor, SerializableImageData),
629    /// Delete a previously registered image registration.
630    DeleteImage(ImageKey),
631    /// Update an existing image registration.
632    UpdateImage(
633        ImageKey,
634        ImageDescriptor,
635        SerializableImageData,
636        Option<Epoch>,
637    ),
638}
639
640impl Debug for ImageUpdate {
641    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
642        match self {
643            Self::AddImage(image_key, image_desc, _) => f
644                .debug_tuple("AddImage")
645                .field(image_key)
646                .field(image_desc)
647                .finish(),
648            Self::DeleteImage(image_key) => f.debug_tuple("DeleteImage").field(image_key).finish(),
649            Self::UpdateImage(image_key, image_desc, _, epoch) => f
650                .debug_tuple("UpdateImage")
651                .field(image_key)
652                .field(image_desc)
653                .field(epoch)
654                .finish(),
655        }
656    }
657}
658
659#[derive(Debug, Deserialize, Serialize)]
660/// Serialized `ImageData`. It contains IPC byte channel receiver to prevent from loading bytes too
661/// slow.
662pub enum SerializableImageData {
663    /// A simple series of bytes, provided by the embedding and owned by WebRender.
664    /// The format is stored out-of-band, currently in ImageDescriptor.
665    Raw(IpcSharedMemory),
666    /// An image owned by the embedding, and referenced by WebRender. This may
667    /// take the form of a texture or a heap-allocated buffer.
668    External(ExternalImageData),
669}
670
671impl From<SerializableImageData> for ImageData {
672    fn from(value: SerializableImageData) -> Self {
673        match value {
674            SerializableImageData::Raw(shared_memory) => ImageData::new(shared_memory.to_vec()),
675            SerializableImageData::External(image) => ImageData::External(image),
676        }
677    }
678}
679
680/// A trait that exposes the embedding layer's `WebView` to the Servo renderer.
681/// This is to prevent a dependency cycle between the renderer and the embedding
682/// layer.
683pub trait WebViewTrait {
684    fn id(&self) -> WebViewId;
685    fn screen_geometry(&self) -> Option<ScreenGeometry>;
686    fn set_animating(&self, new_value: bool);
687}
688
689/// What entity is reporting that a `Pipeline` has exited. Only when all have
690/// done this will the renderer discard its details.
691#[derive(Clone, Copy, Default, Deserialize, PartialEq, Serialize)]
692pub struct PipelineExitSource(u8);
693
694bitflags! {
695    impl PipelineExitSource: u8 {
696        const Script = 1 << 0;
697        const Constellation = 1 << 1;
698    }
699}