Skip to main content

layout/
context.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::collections::HashMap;
6use std::sync::Arc;
7
8use embedder_traits::UntrustedNodeAddress;
9use euclid::Size2D;
10use fonts::FontContext;
11use layout_api::{
12    AnimatingImages, IFrameSizes, LayoutImageDestination, LayoutNode, PendingImage,
13    PendingImageState, PendingRasterizationImage,
14};
15use net_traits::image_cache::{
16    Image as CachedImage, ImageCache, ImageCacheResult, ImageOrMetadataAvailable, PendingImageId,
17    RasterizationResult,
18};
19use net_traits::request::InternalRequest;
20use parking_lot::{Mutex, RwLock};
21use pixels::RasterImage;
22use script::layout_dom::ServoLayoutNode;
23use servo_base::id::PainterId;
24use servo_url::{ImmutableOrigin, ServoUrl};
25use style::context::SharedStyleContext;
26use style::dom::OpaqueNode;
27use style::values::computed::color::Color;
28use style::values::computed::image::{Gradient, Image};
29use style_traits::DevicePixel;
30use uuid::Uuid;
31use webrender_api::ImageKey;
32use webrender_api::units::{DeviceIntSize, DeviceSize};
33
34pub(crate) type CachedImageOrError = Result<CachedImage, ResolveImageError>;
35
36pub(crate) struct LayoutContext<'a> {
37    /// Bits shared by the layout and style system.
38    pub style_context: SharedStyleContext<'a>,
39
40    /// A FontContext to be used during layout.
41    pub font_context: Arc<FontContext>,
42
43    /// A collection of `<iframe>` sizes to send back to script.
44    pub iframe_sizes: Mutex<IFrameSizes>,
45
46    /// An [`ImageResolver`] used for resolving images during box and fragment
47    /// tree construction. Later passed to display list construction.
48    pub image_resolver: Arc<ImageResolver>,
49
50    /// The [`PainterId`] that identifies which `RenderingContext` that this layout targets.
51    pub painter_id: PainterId,
52
53    /// Whether or not parallel layout should be allowed for this layout.
54    pub allow_parallel_layout: bool,
55
56    /// The minimum number of jobs that need to be larger than
57    /// [`Self::parallelism_job_size_minimum`] in order to enable parallelism.
58    pub parallelism_job_count_minimum: usize,
59
60    /// The minimum size a job needs to be to be counted when determining if the number of
61    /// jobs exceeds [`Self::parallelism_job_count_minimum`].
62    pub parallelism_job_size_minimum: usize,
63
64    /// The device dimensions on which this layout is running, in device pixels.
65    pub device_size: Size2D<f32, DevicePixel>,
66}
67
68impl LayoutContext<'_> {
69    pub(crate) fn should_parallelize(&self, number_of_jobs: usize) -> bool {
70        self.allow_parallel_layout && number_of_jobs >= self.parallelism_job_count_minimum
71    }
72
73    pub(crate) fn should_parallelize_layout(&self, jobs: impl Iterator<Item = usize>) -> bool {
74        self.allow_parallel_layout &&
75            jobs.filter(|job| *job >= self.parallelism_job_size_minimum)
76                .count() >=
77                self.parallelism_job_count_minimum
78    }
79}
80
81pub enum ResolvedImage<'a> {
82    Gradient(&'a Gradient),
83    Color(&'a Color),
84    // The size is tracked explicitly as image-set images can specify their
85    // natural resolution which affects the final size for raster images.
86    Image {
87        image: CachedImage,
88        size: DeviceSize,
89        url: ServoUrl,
90    },
91}
92
93#[derive(Clone, Copy, Debug)]
94pub enum ResolveImageError {
95    LoadError,
96    ImagePending,
97    OnlyMetadata,
98    InvalidUrl,
99    MissingNode,
100    ImageMissingFromImageSet,
101    NotImplementedYet,
102    None,
103}
104
105pub(crate) enum LayoutImageCacheResult {
106    Pending,
107    DataAvailable(ImageOrMetadataAvailable),
108    LoadError,
109}
110
111pub(crate) struct ImageResolver {
112    /// The origin of the `Document` that this [`ImageResolver`] resolves images for.
113    pub origin: ImmutableOrigin,
114
115    /// Reference to the script thread image cache.
116    pub image_cache: Arc<dyn ImageCache>,
117
118    /// A list of in-progress image loads to be shared with the script thread.
119    pub pending_images: Mutex<Vec<PendingImage>>,
120
121    /// A list of fully loaded vector images that need to be rasterized to a specific
122    /// size determined by layout. This will be shared with the script thread.
123    pub pending_rasterization_images: Mutex<Vec<PendingRasterizationImage>>,
124
125    /// A list of `SVGSVGElement`s encountered during layout that are not
126    /// serialized yet. This is needed to support inline SVGs as they are treated
127    /// as replaced elements and the layout is responsible for triggering the
128    /// network load for the corresponding serialized data: urls (similar to
129    /// background images).
130    pub pending_svg_elements_for_serialization: Mutex<Vec<UntrustedNodeAddress>>,
131
132    /// A shared reference to script's map of DOM nodes with animated images. This is used
133    /// to manage image animations in script and inform the script about newly animating
134    /// nodes.
135    pub animating_images: Arc<RwLock<AnimatingImages>>,
136
137    // A cache that maps image resources used in CSS (e.g as the `url()` value
138    // for `background-image` or `content` property) to the final resolved image data.
139    pub resolved_images_cache: Arc<RwLock<HashMap<ServoUrl, CachedImageOrError>>>,
140
141    /// The current animation timeline value used to properly initialize animating images.
142    pub animation_timeline_value: f64,
143}
144
145impl Drop for ImageResolver {
146    fn drop(&mut self) {
147        if !std::thread::panicking() {
148            assert!(self.pending_images.lock().is_empty());
149            assert!(self.pending_rasterization_images.lock().is_empty());
150            assert!(
151                self.pending_svg_elements_for_serialization
152                    .lock()
153                    .is_empty()
154            );
155        }
156    }
157}
158
159impl ImageResolver {
160    pub(crate) fn get_or_request_image_or_meta(
161        &self,
162        node: OpaqueNode,
163        url: ServoUrl,
164        destination: LayoutImageDestination,
165        is_internal_request: InternalRequest,
166    ) -> LayoutImageCacheResult {
167        // Check for available image or start tracking.
168        let cache_result =
169            self.image_cache
170                .get_cached_image_status(url.clone(), &self.origin, None);
171
172        match cache_result {
173            ImageCacheResult::Available(img_or_meta) => {
174                LayoutImageCacheResult::DataAvailable(img_or_meta)
175            },
176            // Image has been requested, is still pending. Return no image for this paint loop.
177            // When the image loads it will trigger a reflow and/or repaint.
178            ImageCacheResult::Pending(id) => {
179                let image = PendingImage {
180                    state: PendingImageState::PendingResponse,
181                    node: node.into(),
182                    id,
183                    origin: self.origin.clone(),
184                    destination,
185                    is_internal_request,
186                };
187                self.pending_images.lock().push(image);
188                LayoutImageCacheResult::Pending
189            },
190            // Not yet requested - request image or metadata from the cache
191            ImageCacheResult::ReadyForRequest(id) => {
192                let image = PendingImage {
193                    state: PendingImageState::Unrequested(url),
194                    node: node.into(),
195                    id,
196                    origin: self.origin.clone(),
197                    destination,
198                    is_internal_request,
199                };
200                self.pending_images.lock().push(image);
201                LayoutImageCacheResult::Pending
202            },
203            // Image failed to load, so just return the same error.
204            ImageCacheResult::FailedToLoadOrDecode => LayoutImageCacheResult::LoadError,
205        }
206    }
207
208    pub(crate) fn handle_animated_image(&self, node: OpaqueNode, image: Arc<RasterImage>) {
209        let mut animating_images = self.animating_images.write();
210        if !image.should_animate() {
211            animating_images.remove(node);
212        } else {
213            animating_images.maybe_insert_or_update(node, image, self.animation_timeline_value);
214        }
215    }
216
217    pub(crate) fn get_cached_image_for_url(
218        &self,
219        node: OpaqueNode,
220        url: ServoUrl,
221        destination: LayoutImageDestination,
222        is_internal_request: InternalRequest,
223    ) -> Result<CachedImage, ResolveImageError> {
224        if let Some(cached_image) = self.resolved_images_cache.read().get(&url) {
225            return cached_image.clone();
226        }
227
228        let result =
229            self.get_or_request_image_or_meta(node, url.clone(), destination, is_internal_request);
230        match result {
231            LayoutImageCacheResult::DataAvailable(img_or_meta) => match img_or_meta {
232                ImageOrMetadataAvailable::ImageAvailable { image, .. } => {
233                    if let Some(image) = image.as_raster_image() {
234                        self.handle_animated_image(node, image);
235                    }
236
237                    let mut resolved_images_cache = self.resolved_images_cache.write();
238                    resolved_images_cache.insert(url, Ok(image.clone()));
239                    Ok(image)
240                },
241                ImageOrMetadataAvailable::MetadataAvailable(..) => {
242                    Result::Err(ResolveImageError::OnlyMetadata)
243                },
244            },
245            LayoutImageCacheResult::Pending => Result::Err(ResolveImageError::ImagePending),
246            LayoutImageCacheResult::LoadError => {
247                let error = Err(ResolveImageError::LoadError);
248                self.resolved_images_cache
249                    .write()
250                    .insert(url, error.clone());
251                error
252            },
253        }
254    }
255
256    pub(crate) fn rasterize_vector_image(
257        &self,
258        image_id: PendingImageId,
259        size: DeviceIntSize,
260        node: OpaqueNode,
261        svg_id: Option<Uuid>,
262    ) -> Option<RasterImage> {
263        match self
264            .image_cache
265            .rasterize_vector_image(image_id, size, svg_id)
266        {
267            RasterizationResult::Image(raster_image) => Some(raster_image),
268            RasterizationResult::PendingRasterization => {
269                self.pending_rasterization_images
270                    .lock()
271                    .push(PendingRasterizationImage {
272                        id: image_id,
273                        node: node.into(),
274                        size,
275                    });
276                None
277            },
278            RasterizationResult::InvalidSvgParameters => None,
279        }
280    }
281
282    /// Resolve a cached image to a WebRender [`ImageKey`]
283    pub(crate) fn image_key_from_cached_image(
284        &self,
285        image: &CachedImage,
286        size: DeviceIntSize,
287        node: Option<OpaqueNode>,
288    ) -> Option<ImageKey> {
289        match image {
290            CachedImage::Raster(raster_image) => raster_image.id,
291            CachedImage::Vector(vector_image) => node.and_then(|node| {
292                self.rasterize_vector_image(vector_image.id, size, node, vector_image.svg_id)
293                    .and_then(|rasterized_image| rasterized_image.id)
294            }),
295        }
296    }
297
298    pub(crate) fn queue_svg_element_for_serialization(&self, element: ServoLayoutNode<'_>) {
299        self.pending_svg_elements_for_serialization
300            .lock()
301            .push(element.opaque().into())
302    }
303
304    pub(crate) fn resolve_image<'a>(
305        &self,
306        node: Option<OpaqueNode>,
307        image: &'a Image,
308    ) -> Result<ResolvedImage<'a>, ResolveImageError> {
309        match image {
310            // TODO: Add support for PaintWorklet and CrossFade rendering.
311            Image::None => Result::Err(ResolveImageError::None),
312            Image::CrossFade(_) => Result::Err(ResolveImageError::NotImplementedYet),
313            Image::PaintWorklet(_) => Result::Err(ResolveImageError::NotImplementedYet),
314            Image::Gradient(gradient) => Ok(ResolvedImage::Gradient(gradient)),
315            Image::Image(color) => Ok(ResolvedImage::Color(color)),
316            Image::Url(image_url) => {
317                // FIXME: images won’t always have in intrinsic width or
318                // height when support for SVG is added, or a WebRender
319                // `ImageKey`, for that matter.
320                //
321                // FIXME: It feels like this should take into account the pseudo
322                // element and not just the node.
323                let image_url = image_url.url().ok_or(ResolveImageError::InvalidUrl)?;
324                let node = node.ok_or(ResolveImageError::MissingNode)?;
325                let image = self.get_cached_image_for_url(
326                    node,
327                    image_url.clone().into(),
328                    LayoutImageDestination::DisplayListBuilding,
329                    InternalRequest::No,
330                )?;
331                let metadata = image.metadata();
332                let size = Size2D::new(metadata.width, metadata.height).to_f32();
333                Ok(ResolvedImage::Image {
334                    image,
335                    size,
336                    url: image_url.clone().into(),
337                })
338            },
339            Image::ImageSet(image_set) => {
340                image_set
341                    .items
342                    .get(image_set.selected_index)
343                    .ok_or(ResolveImageError::ImageMissingFromImageSet)
344                    .and_then(|image| {
345                        self.resolve_image(node, &image.image)
346                            .map(|info| match info {
347                                ResolvedImage::Image {
348                                    image: cached_image,
349                                    url,
350                                    ..
351                                } => {
352                                    // From <https://drafts.csswg.org/css-images-4/#image-set-notation>:
353                                    // > A <resolution> (optional). This is used to help the UA decide
354                                    // > which <image-set-option> to choose. If the image reference is
355                                    // > for a raster image, it also specifies the image’s natural
356                                    // > resolution, overriding any other source of data that might
357                                    // > supply a natural resolution.
358                                    let image_metadata = cached_image.metadata();
359                                    let size = if cached_image.as_raster_image().is_some() {
360                                        let scale_factor = image.resolution.dppx();
361                                        Size2D::new(
362                                            image_metadata.width as f32 / scale_factor,
363                                            image_metadata.height as f32 / scale_factor,
364                                        )
365                                    } else {
366                                        Size2D::new(image_metadata.width, image_metadata.height)
367                                            .to_f32()
368                                    };
369
370                                    ResolvedImage::Image {
371                                        image: cached_image,
372                                        size,
373                                        url,
374                                    }
375                                },
376                                _ => info,
377                            })
378                    })
379            },
380            Image::LightDark(..) => unreachable!("light-dark() should be disabled"),
381        }
382    }
383}