Skip to main content

net/
image_cache.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, OnceCell};
6use std::cmp::min;
7use std::collections::hash_map::Entry::{Occupied, Vacant};
8use std::collections::{HashMap, HashSet, VecDeque};
9use std::panic::AssertUnwindSafe;
10use std::sync::Arc;
11use std::{mem, thread_local};
12
13use embedder_traits::resources::ResourceBytes;
14use imsz::imsz_from_reader;
15use log::{debug, error, warn};
16use malloc_size_of::{MallocConditionalSizeOf, MallocSizeOf as MallocSizeOfTrait, MallocSizeOfOps};
17use malloc_size_of_derive::MallocSizeOf;
18use mime::Mime;
19use net_traits::image_cache::{
20    FontResolver, Image, ImageCache, ImageCacheFactory, ImageCacheResponseCallback,
21    ImageCacheResponseMessage, ImageCacheResult, ImageLoadListener, ImageOrMetadataAvailable,
22    ImageResponse, PendingImageId, RasterizationCompleteResponse, RasterizationResult, VectorImage,
23};
24use net_traits::request::CorsSettings;
25use net_traits::{FetchResponseMsg, Metadata, NetworkError};
26use paint_api::{CrossProcessPaintApi, ImageUpdate, SerializableImageData};
27use parking_lot::Mutex;
28use pixels::{CorsStatus, ImageFrame, ImageMetadata, PixelFormat, RasterImage, load_from_memory};
29use profile_traits::mem::{Report, ReportKind};
30use profile_traits::path;
31use resvg::tiny_skia;
32use resvg::usvg::{self, fontdb};
33use rustc_hash::{FxHashMap, FxHashSet};
34use servo_base::cross_process_instant::CrossProcessInstant;
35use servo_base::id::{PipelineId, WebViewId};
36use servo_base::threadpool::ThreadPool;
37use servo_url::{ImmutableOrigin, ServoUrl};
38use uuid::Uuid;
39use webrender_api::ImageKey as WebRenderImageKey;
40use webrender_api::units::DeviceIntSize;
41
42use crate::image_cache::KeyCacheState::PipelineClosed;
43
44const MAX_SVG_DEPTH: u32 = 500;
45
46thread_local! {
47    pub static SUPPRESS_ABORT_IN_PANIC_HOOK: Cell<bool> = const { Cell::new(false) };
48}
49
50/// Returns `true` if embedder's custom panic hook handler should *not* treat the panic
51/// as a fatal error that requires termination of the engine.
52///
53/// This is needed to catch and silence panics in `resvg` crate. `servoshell
54/// registers a custom panic hook that aborts the process in `hard_fail` mode
55/// even when using `catch_unwind`.
56pub fn should_panic_hook_suppress_termination() -> bool {
57    SUPPRESS_ABORT_IN_PANIC_HOOK.get()
58}
59
60// We bake in rippy.png as a fallback, in case the embedder does not provide a broken
61// image icon resource. This version is 229 bytes, so don't exchange it against
62// something of higher resolution.
63const FALLBACK_RIPPY: &[u8] = include_bytes!("resources/rippy.png");
64
65/// The current SVG stack relies on `resvg` to provide the natural dimensions of
66/// the SVG, which it automatically infers from the width/height/viewBox properties
67/// of the SVG. Since these can be arbitrarily large, this can cause us to allocate
68/// a pixmap with very large dimensions leading to the process being killed due to
69/// memory exhaustion. For example, the `/css/css-transforms/perspective-svg-001.html`
70/// test uses very large values for viewBox. Hence, we just clamp the maximum
71/// width/height of the pixmap allocated for rasterization.
72const MAX_SVG_PIXMAP_DIMENSION: u32 = 5000;
73
74//
75// TODO(gw): Remaining work on image cache:
76//     * Make use of the prefetch support in various parts of the code.
77//     * Profile time in GetImageIfAvailable - might be worth caching these
78//       results per paint / layout.
79//
80// MAYBE(Yoric):
81//     * For faster lookups, it might be useful to store the LoadKey in the
82//       DOM once we have performed a first load.
83
84// ======================================================================
85// Helper functions.
86// ======================================================================
87
88fn parse_svg_document_in_memory(
89    bytes: &[u8],
90    usvg_options: Arc<usvg::Options>,
91) -> Result<usvg::Tree, &'static str> {
92    usvg::Tree::from_data(bytes, &usvg_options)
93        .inspect_err(|error| {
94            warn!("Error when parsing SVG data: {error}");
95        })
96        .map_err(|_| "Not a valid SVG document")
97}
98
99fn decode_bytes_sync(
100    key: LoadKey,
101    bytes: &[u8],
102    cors: CorsStatus,
103    content_type: Option<Mime>,
104    usvg_options: Arc<usvg::Options>,
105) -> DecoderMsg {
106    let is_svg_document = content_type.is_some_and(|content_type| {
107        (
108            content_type.type_(),
109            content_type.subtype(),
110            content_type.suffix(),
111        ) == (mime::IMAGE, mime::SVG, Some(mime::XML))
112    });
113
114    let image = if is_svg_document {
115        parse_svg_document_in_memory(bytes, usvg_options)
116            .ok()
117            .map(|svg_tree| {
118                DecodedImage::Vector(VectorImageData {
119                    svg_tree: Arc::new(svg_tree),
120                    cors_status: cors,
121                })
122            })
123    } else {
124        load_from_memory(bytes, cors).map(DecodedImage::Raster)
125    };
126
127    DecoderMsg { key, image }
128}
129
130fn set_webrender_image_key(
131    paint_api: &CrossProcessPaintApi,
132    image: &mut RasterImage,
133    image_key: WebRenderImageKey,
134) {
135    if image.id.is_some() {
136        return;
137    }
138
139    let (descriptor, ipc_shared_memory, should_be_cached) =
140        image.webrender_image_descriptor_and_data_for_frame(0);
141    let data = SerializableImageData::Raw(ipc_shared_memory);
142
143    paint_api.add_image(image_key, descriptor, data, should_be_cached);
144    image.id = Some(image_key);
145}
146
147// ======================================================================
148// Aux structs and enums.
149// ======================================================================
150
151/// <https://html.spec.whatwg.org/multipage/#list-of-available-images>
152type ImageKey = (ServoUrl, ImmutableOrigin, Option<CorsSettings>);
153
154// Represents all the currently pending loads/decodings. For
155// performance reasons, loads are indexed by a dedicated load key.
156#[derive(Default, MallocSizeOf)]
157struct AllPendingLoads {
158    // The loads, indexed by a load key. Used during most operations,
159    // for performance reasons.
160    loads: FxHashMap<LoadKey, PendingLoad>,
161
162    // Get a load key from its url and requesting origin. Used ony when starting and
163    // finishing a load or when adding a new listener.
164    url_to_load_key: HashMap<ImageKey, LoadKey>,
165
166    // A counter used to generate instances of LoadKey
167    keygen: LoadKeyGenerator,
168}
169
170impl AllPendingLoads {
171    fn new() -> AllPendingLoads {
172        AllPendingLoads {
173            loads: FxHashMap::default(),
174            url_to_load_key: HashMap::default(),
175            keygen: LoadKeyGenerator::new(),
176        }
177    }
178
179    // get a PendingLoad from its LoadKey.
180    fn get_by_key_mut(&mut self, key: &LoadKey) -> Option<&mut PendingLoad> {
181        self.loads.get_mut(key)
182    }
183
184    fn remove(&mut self, key: &LoadKey) -> Option<PendingLoad> {
185        self.loads.remove(key).inspect(|pending_load| {
186            self.url_to_load_key
187                .remove(&(
188                    pending_load.url.clone(),
189                    pending_load.load_origin.clone(),
190                    pending_load.cors_setting,
191                ))
192                .unwrap();
193        })
194    }
195
196    fn get_cached(
197        &mut self,
198        url: ServoUrl,
199        origin: &ImmutableOrigin,
200        cors_status: Option<CorsSettings>,
201    ) -> CacheResult<'_> {
202        match self
203            .url_to_load_key
204            .entry((url.clone(), origin.clone(), cors_status))
205        {
206            Occupied(url_entry) => {
207                let load_key = url_entry.get();
208                CacheResult::Hit(*load_key, self.loads.get_mut(load_key).unwrap())
209            },
210            Vacant(url_entry) => {
211                let load_key = self.keygen.next();
212                url_entry.insert(load_key);
213
214                let pending_load = PendingLoad::new(url, origin.clone(), cors_status);
215                match self.loads.entry(load_key) {
216                    Occupied(_) => unreachable!(),
217                    Vacant(load_entry) => {
218                        let mut_load = load_entry.insert(pending_load);
219                        CacheResult::Miss(Some((load_key, mut_load)))
220                    },
221                }
222            },
223        }
224    }
225}
226
227/// Result of accessing a cache.
228enum CacheResult<'a> {
229    /// The value was in the cache.
230    Hit(LoadKey, &'a mut PendingLoad),
231    /// The value was not in the cache and needed to be regenerated.
232    Miss(Option<(LoadKey, &'a mut PendingLoad)>),
233}
234
235/// Represents an image that has completed loading.
236/// Images that fail to load (due to network or decode
237/// failure) are still stored here, so that they aren't
238/// fetched again.
239#[derive(MallocSizeOf)]
240struct CompletedLoad {
241    image_response: ImageResponse,
242    id: PendingImageId,
243}
244
245impl CompletedLoad {
246    fn new(image_response: ImageResponse, id: PendingImageId) -> CompletedLoad {
247        CompletedLoad { image_response, id }
248    }
249}
250
251#[derive(Clone, MallocSizeOf)]
252struct VectorImageData {
253    #[conditional_malloc_size_of]
254    svg_tree: Arc<usvg::Tree>,
255    cors_status: CorsStatus,
256}
257
258impl std::fmt::Debug for VectorImageData {
259    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
260        f.debug_struct("VectorImageData").finish()
261    }
262}
263
264enum DecodedImage {
265    Raster(RasterImage),
266    Vector(VectorImageData),
267}
268
269/// Message that the decoder worker threads send to the image cache.
270struct DecoderMsg {
271    key: LoadKey,
272    image: Option<DecodedImage>,
273}
274
275#[derive(MallocSizeOf)]
276enum ImageBytes {
277    InProgress(Vec<u8>),
278    Complete(#[conditional_malloc_size_of] Arc<Vec<u8>>),
279}
280
281impl ImageBytes {
282    fn extend_from_slice(&mut self, data: &[u8]) {
283        match *self {
284            ImageBytes::InProgress(ref mut bytes) => bytes.extend_from_slice(data),
285            ImageBytes::Complete(_) => panic!("attempted modification of complete image bytes"),
286        }
287    }
288
289    fn mark_complete(&mut self) -> Arc<Vec<u8>> {
290        let mut bytes = {
291            let own_bytes = match *self {
292                ImageBytes::InProgress(ref mut bytes) => bytes,
293                ImageBytes::Complete(_) => panic!("attempted modification of complete image bytes"),
294            };
295            mem::take(own_bytes)
296        };
297        bytes.shrink_to_fit();
298        let bytes = Arc::new(bytes);
299        *self = ImageBytes::Complete(bytes.clone());
300        bytes
301    }
302
303    fn as_slice(&self) -> &[u8] {
304        match *self {
305            ImageBytes::InProgress(ref bytes) => bytes,
306            ImageBytes::Complete(ref bytes) => bytes,
307        }
308    }
309
310    fn set_capacity(&mut self, size: usize) {
311        match self {
312            ImageBytes::InProgress(items) => items.reserve(size.saturating_sub(items.len())),
313            ImageBytes::Complete(_) => error!("Want to set capacity on already completed image."),
314        }
315    }
316}
317
318// A key used to communicate during loading.
319type LoadKey = PendingImageId;
320
321#[derive(Default, MallocSizeOf)]
322struct LoadKeyGenerator {
323    counter: u64,
324}
325
326impl LoadKeyGenerator {
327    fn new() -> LoadKeyGenerator {
328        LoadKeyGenerator { counter: 0 }
329    }
330    fn next(&mut self) -> PendingImageId {
331        self.counter += 1;
332        PendingImageId(self.counter)
333    }
334}
335
336#[derive(Debug)]
337enum LoadResult {
338    LoadedRasterImage(RasterImage),
339    LoadedVectorImage(VectorImageData),
340    FailedToLoadOrDecode,
341}
342
343/// Represents an image that is either being loaded
344/// by the resource thread, or decoded by a worker thread.
345#[derive(MallocSizeOf)]
346struct PendingLoad {
347    /// The bytes loaded so far. Reset to an empty vector once loading
348    /// is complete and the buffer has been transmitted to the decoder.
349    bytes: ImageBytes,
350
351    /// Image metadata, if available.
352    metadata: Option<ImageMetadata>,
353
354    /// Once loading is complete, the result of the operation.
355    result: Option<Result<(), NetworkError>>,
356
357    /// The time when the response finished loading.
358    response_end: Option<CrossProcessInstant>,
359
360    /// The listeners that are waiting for this response to complete.
361    listeners: Vec<ImageLoadListener>,
362
363    /// The url being loaded. Do not forget that this may be several Mb
364    /// if we are loading a data: url.
365    url: ServoUrl,
366
367    /// The origin that requested this load.
368    load_origin: ImmutableOrigin,
369
370    /// The CORS attribute setting for the requesting
371    cors_setting: Option<CorsSettings>,
372
373    /// The CORS status of this image response.
374    cors_status: CorsStatus,
375
376    /// The URL of the final response that contains a body.
377    final_url: Option<ServoUrl>,
378
379    /// The MIME type from the `Content-type` header of the HTTP response, if any.
380    content_type: Option<Mime>,
381}
382
383impl PendingLoad {
384    fn new(
385        url: ServoUrl,
386        load_origin: ImmutableOrigin,
387        cors_setting: Option<CorsSettings>,
388    ) -> PendingLoad {
389        PendingLoad {
390            bytes: ImageBytes::InProgress(vec![]),
391            metadata: None,
392            result: None,
393            response_end: None,
394            listeners: vec![],
395            url,
396            load_origin,
397            final_url: None,
398            cors_setting,
399            cors_status: CorsStatus::Unsafe,
400            content_type: None,
401        }
402    }
403
404    fn add_listener(&mut self, listener: ImageLoadListener) {
405        self.listeners.push(listener);
406    }
407}
408
409#[derive(Default, MallocSizeOf)]
410struct RasterizationTask {
411    #[ignore_malloc_size_of = "Fn is difficult to measure"]
412    listeners: Vec<(PipelineId, ImageCacheResponseCallback)>,
413    result: Option<RasterImage>,
414}
415
416/// Used for storing images that do not have a `WebRenderImageKey` yet.
417#[derive(Debug, MallocSizeOf)]
418enum PendingKey {
419    RasterImage((LoadKey, RasterImage)),
420    Svg((LoadKey, RasterImage, DeviceIntSize)),
421}
422
423/// The state of the `WebRenderImageKey`` cache
424#[derive(Debug, Default, MallocSizeOf)]
425enum KeyCacheState {
426    /// We already requested a batch of keys.
427    PendingBatch,
428    /// We have some keys in the cache.
429    Ready(Vec<WebRenderImageKey>),
430    /// Currently filling images from the KeyCache. No new keys will be requested.
431    Processing,
432    /// We will not process any images anymore because the pipeline is shut down.
433    #[default]
434    PipelineClosed,
435}
436
437impl KeyCacheState {
438    fn size(&self) -> usize {
439        match self {
440            KeyCacheState::PendingBatch |
441            KeyCacheState::Processing |
442            KeyCacheState::PipelineClosed => 0,
443            KeyCacheState::Ready(items) => items.len(),
444        }
445    }
446}
447
448/// As getting new keys takes a round trip over the constellation, we keep a small cache of them.
449/// Additionally, this cache will store image resources that do not have a key yet because those
450/// are needed to complete the load.
451#[derive(Default, MallocSizeOf)]
452struct KeyCache {
453    /// A cache of `WebRenderImageKey`.
454    cache: KeyCacheState,
455    /// These images are loaded but have no key assigned to yet.
456    images_pending_keys: VecDeque<PendingKey>,
457    /// A set of `LoadKey` and image size pairs which have been evicted
458    /// but are either being rasterized or are in images_pending_key
459    evicted_images: HashSet<(LoadKey, DeviceIntSize)>,
460}
461
462impl KeyCache {
463    fn new() -> Self {
464        KeyCache {
465            cache: KeyCacheState::Ready(Vec::new()),
466            images_pending_keys: VecDeque::new(),
467            evicted_images: HashSet::new(),
468        }
469    }
470}
471
472#[derive(Debug, Default, MallocSizeOf)]
473/// A structure that stores if a current SVG element with a PendingImageId and a DeviceIntSize is already being rasterized.
474struct SvgRasterizationTaskStore(FxHashSet<(PendingImageId, DeviceIntSize)>);
475
476impl SvgRasterizationTaskStore {
477    /// Returns true if it is already being rasterized, otherwise false and sets it.
478    fn is_or_set_being_rasterized(
479        &mut self,
480        pending_image_id: PendingImageId,
481        size: DeviceIntSize,
482    ) -> bool {
483        !self.0.insert((pending_image_id, size))
484    }
485
486    /// Removes the task
487    fn remove_being_rasterized(&mut self, pending_image_id: PendingImageId, size: DeviceIntSize) {
488        self.0.remove(&(pending_image_id, size));
489    }
490
491    fn remove_all_for_id(&mut self, pending_image_id: PendingImageId) {
492        self.0.retain(|(id, _size)| *id != pending_image_id);
493    }
494}
495
496/// ## Image cache implementation.
497#[derive(MallocSizeOf)]
498struct ImageCacheStore {
499    /// Images that are loading over network, or decoding.
500    pending_loads: AllPendingLoads,
501
502    /// Images that have finished loading (successful or not)
503    completed_loads: HashMap<ImageKey, CompletedLoad>,
504
505    /// Vector (e.g. SVG) images that have been sucessfully loaded and parsed
506    /// but are yet to be rasterized. Since the same SVG data can be used for
507    /// rasterizing at different sizes, we use this hasmap to share the data.
508    vector_images: FxHashMap<PendingImageId, VectorImageData>,
509
510    /// Vector images for which rasterization at a particular size has started
511    /// or completed. If completed, the `result` member of `RasterizationTask`
512    /// contains the rasterized image.
513    rasterized_vector_images: FxHashMap<(PendingImageId, DeviceIntSize), RasterizationTask>,
514
515    /// Maps a pending image id to a set of sizes for which that image was requested
516    svg_rasterization_task_store: SvgRasterizationTaskStore,
517
518    /// The [`RasterImage`] used for the broken image icon, initialized lazily, only when necessary.
519    #[conditional_malloc_size_of]
520    broken_image_icon_image: OnceCell<Option<Arc<RasterImage>>>,
521
522    /// Cross-process `Paint` API instance.
523    paint_api: CrossProcessPaintApi,
524
525    /// The [`WebView`] of the `Webview` associated with this [`ImageCache`].
526    webview_id: WebViewId,
527
528    /// The [`PipelineId`] of the `Pipeline` associated with this [`ImageCache`].
529    pipeline_id: PipelineId,
530
531    /// Main struct to handle the cache of `WebRenderImageKey` and
532    /// images that do not have a key yet.
533    key_cache: KeyCache,
534}
535
536impl ImageCacheStore {
537    #[cfg(feature = "test-util")]
538    fn number_of_rasterize_tasks(&self) -> usize {
539        self.svg_rasterization_task_store.0.len()
540    }
541
542    /// Finishes loading the image by setting the WebRenderImageKey and calling `compete_load` or `complete_load_svg`.
543    fn set_key_and_finish_load(&mut self, pending_image: PendingKey, image_key: WebRenderImageKey) {
544        match pending_image {
545            PendingKey::RasterImage((pending_id, mut raster_image)) => {
546                // We can have concurrent sync and async loads for the same image, so if it's
547                // not pending anymore we early return since the async result will be ignored in that case.
548                if self.pending_loads.get_by_key_mut(&pending_id).is_none() {
549                    return;
550                }
551                set_webrender_image_key(&self.paint_api, &mut raster_image, image_key);
552                self.complete_load(pending_id, LoadResult::LoadedRasterImage(raster_image));
553            },
554            PendingKey::Svg((pending_id, mut raster_image, requested_size)) => {
555                // We can have concurrent sync and async loads for the same image, so if it's
556                // not pending anymore we early return since the async result will be ignored in that case.
557                if !self
558                    .rasterized_vector_images
559                    .contains_key(&(pending_id, requested_size))
560                {
561                    return;
562                }
563                set_webrender_image_key(&self.paint_api, &mut raster_image, image_key);
564                self.svg_rasterization_task_store
565                    .remove_being_rasterized(pending_id, requested_size);
566                self.complete_load_svg(raster_image, pending_id, requested_size);
567            },
568        }
569    }
570
571    /// If a key is available the image will be immediately loaded, otherwise it will load then the next batch of
572    /// keys is received. Only call this if the image does not have a `LoadKey` yet.
573    fn load_image_with_keycache(&mut self, pending_image: PendingKey) {
574        if let PendingKey::Svg((pending_id, ref _raster_image, requested_size)) = pending_image &&
575            self.key_cache
576                .evicted_images
577                .remove(&(pending_id, requested_size))
578        {
579            self.svg_rasterization_task_store
580                .remove_being_rasterized(pending_id, requested_size);
581            return;
582        }
583        match self.key_cache.cache {
584            KeyCacheState::PendingBatch | KeyCacheState::Processing => {
585                self.key_cache.images_pending_keys.push_back(pending_image);
586            },
587            KeyCacheState::Ready(ref mut cache) => match cache.pop() {
588                Some(image_key) => {
589                    self.set_key_and_finish_load(pending_image, image_key);
590                },
591                None => {
592                    self.key_cache.images_pending_keys.push_back(pending_image);
593                    self.fetch_more_image_keys();
594                },
595            },
596            KeyCacheState::PipelineClosed => {},
597        }
598    }
599
600    fn evict_image_from_keycache(
601        &mut self,
602        image_id: &PendingImageId,
603        requested_size: &DeviceIntSize,
604    ) {
605        self.key_cache
606            .evicted_images
607            .insert((*image_id, *requested_size));
608    }
609
610    fn fetch_more_image_keys(&mut self) {
611        self.key_cache.cache = KeyCacheState::PendingBatch;
612        self.paint_api
613            .generate_image_key_async(self.webview_id, self.pipeline_id);
614    }
615
616    /// Insert received keys into the cache and complete the loading of images.
617    fn insert_keys_and_load_images(&mut self, image_keys: Vec<WebRenderImageKey>) {
618        match &mut self.key_cache.cache {
619            KeyCacheState::Processing => {
620                // We can set this now to ready as we have the exclusive write access.
621                self.key_cache.cache = KeyCacheState::Ready(image_keys);
622                let len = min(
623                    self.key_cache.cache.size(),
624                    self.key_cache.images_pending_keys.len(),
625                );
626                let images = self
627                    .key_cache
628                    .images_pending_keys
629                    .drain(0..len)
630                    .collect::<Vec<PendingKey>>();
631                for key in images {
632                    self.load_image_with_keycache(key);
633                }
634                // It is important to fetch new image keys as we might have missed previous returns.
635                if !self.key_cache.images_pending_keys.is_empty() {
636                    self.paint_api
637                        .generate_image_key_async(self.webview_id, self.pipeline_id);
638                    self.key_cache.cache = KeyCacheState::PendingBatch
639                }
640            },
641            KeyCacheState::PendingBatch | KeyCacheState::Ready(_) => {
642                unreachable!("A batch was received while we didn't request one")
643            },
644            PipelineClosed => {},
645        }
646    }
647
648    /// Complete the loading the of the rasterized svg image. This needs the `RasterImage` to
649    /// already have a `WebRenderImageKey`.
650    fn complete_load_svg(
651        &mut self,
652        rasterized_image: RasterImage,
653        pending_image_id: PendingImageId,
654        requested_size: DeviceIntSize,
655    ) {
656        let listeners = {
657            self.rasterized_vector_images
658                .get_mut(&(pending_image_id, requested_size))
659                .map(|task| {
660                    task.result = Some(rasterized_image);
661                    std::mem::take(&mut task.listeners)
662                })
663                .unwrap_or_default()
664        };
665
666        for (pipeline_id, callback) in listeners {
667            callback(ImageCacheResponseMessage::VectorImageRasterizationComplete(
668                RasterizationCompleteResponse {
669                    pipeline_id,
670                    image_id: pending_image_id,
671                    requested_size,
672                },
673            ));
674        }
675    }
676
677    /// The rest of complete load. This requires that images have a valid `WebRenderImageKey`.
678    fn complete_load(&mut self, key: LoadKey, load_result: LoadResult) {
679        debug!("Completed decoding for {:?}", load_result);
680        let pending_load = match self.pending_loads.remove(&key) {
681            Some(load) => load,
682            None => return,
683        };
684        let url = pending_load.final_url;
685        let image_response = match load_result {
686            LoadResult::LoadedRasterImage(mut raster_image) => {
687                assert!(raster_image.id.is_some());
688                raster_image.load_time = pending_load.response_end;
689                ImageResponse::Loaded(Image::Raster(Arc::new(raster_image)), url.unwrap())
690            },
691            LoadResult::LoadedVectorImage(vector_image) => {
692                self.vector_images.insert(key, vector_image.clone());
693                let natural_dimensions = vector_image.svg_tree.size().to_int_size();
694                let metadata = ImageMetadata {
695                    width: natural_dimensions.width(),
696                    height: natural_dimensions.height(),
697                };
698
699                let vector_image = VectorImage {
700                    id: key,
701                    svg_id: None,
702                    metadata,
703                    cors_status: vector_image.cors_status,
704                    load_time: pending_load.response_end,
705                };
706                ImageResponse::Loaded(Image::Vector(vector_image), url.unwrap())
707            },
708            LoadResult::FailedToLoadOrDecode => ImageResponse::FailedToLoadOrDecode,
709        };
710
711        let completed_load = CompletedLoad::new(image_response.clone(), key);
712        self.completed_loads.insert(
713            (
714                pending_load.url,
715                pending_load.load_origin,
716                pending_load.cors_setting,
717            ),
718            completed_load,
719        );
720
721        for listener in pending_load.listeners {
722            listener.respond(image_response.clone());
723        }
724    }
725
726    fn remove_loaded_image(
727        &mut self,
728        url: &ServoUrl,
729        origin: &ImmutableOrigin,
730        cors_setting: &Option<CorsSettings>,
731    ) {
732        if let Some(loaded_image) =
733            self.completed_loads
734                .remove(&(url.clone(), origin.clone(), *cors_setting)) &&
735            let ImageResponse::Loaded(Image::Raster(image), _) = loaded_image.image_response &&
736            let Some(id) = image.id
737        {
738            self.paint_api.update_images(
739                self.webview_id.into(),
740                vec![ImageUpdate::DeleteImage(id)].into(),
741            );
742        }
743    }
744
745    fn remove_rasterized_vector_image(
746        &mut self,
747        image_id: &PendingImageId,
748        device_size: &DeviceIntSize,
749    ) {
750        if let Some(entry) = self
751            .rasterized_vector_images
752            .remove(&(*image_id, *device_size))
753        {
754            if let Some(result) = entry.result {
755                if let Some(image_id) = result.id {
756                    self.paint_api.update_images(
757                        self.webview_id.into(),
758                        vec![ImageUpdate::DeleteImage(image_id)].into(),
759                    );
760                }
761            } else {
762                // If there is no corresponding rasterized_vector_image result,
763                // then the vector image is either being rasterized or is in
764                // self.store.key_cache.pending_image_keys. Either way, we need to notify the
765                // KeyCache that it was evicted.
766                self.evict_image_from_keycache(image_id, device_size);
767            }
768        } else {
769            // If there is no corresponding rasterized_vector_image result,
770            // then the vector image is either being rasterized or is in
771            // self.store.key_cache.pending_image_keys. Either way, we need to notify the
772            // KeyCache that it was evicted.
773            self.evict_image_from_keycache(image_id, device_size);
774        }
775    }
776
777    /// Return a completed image if it exists, or None if there is no complete load
778    /// or the complete load is not fully decoded or is unavailable.
779    fn get_completed_image_if_available(
780        &self,
781        url: ServoUrl,
782        origin: ImmutableOrigin,
783        cors_setting: Option<CorsSettings>,
784    ) -> Option<Result<(Image, ServoUrl), ()>> {
785        self.completed_loads
786            .get(&(url, origin, cors_setting))
787            .map(|completed_load| match &completed_load.image_response {
788                ImageResponse::Loaded(image, url) => Ok((image.clone(), url.clone())),
789                ImageResponse::FailedToLoadOrDecode | ImageResponse::MetadataLoaded(_) => Err(()),
790            })
791    }
792
793    /// Handle a message from one of the decoder worker threads or from a sync
794    /// decoding operation.
795    fn handle_decoder(&mut self, msg: DecoderMsg) {
796        let image = match msg.image {
797            None => LoadResult::FailedToLoadOrDecode,
798            Some(DecodedImage::Raster(raster_image)) => {
799                self.load_image_with_keycache(PendingKey::RasterImage((msg.key, raster_image)));
800                return;
801            },
802            Some(DecodedImage::Vector(vector_image_data)) => {
803                LoadResult::LoadedVectorImage(vector_image_data)
804            },
805        };
806        self.complete_load(msg.key, image);
807    }
808}
809
810pub struct ImageCacheFactoryImpl {
811    /// The data to use for the broken image icon used when images cannot load.
812    broken_image_icon_data: Arc<ResourceBytes>,
813    /// Thread pool for image decoding
814    thread_pool: Arc<ThreadPool>,
815}
816
817impl ImageCacheFactoryImpl {
818    pub fn new(broken_image_icon_data: ResourceBytes) -> Self {
819        debug!("Creating new ImageCacheFactoryImpl");
820
821        Self {
822            broken_image_icon_data: Arc::new(broken_image_icon_data),
823            thread_pool: ThreadPool::global(),
824        }
825    }
826}
827
828impl ImageCacheFactory for ImageCacheFactoryImpl {
829    fn create(
830        &self,
831        webview_id: WebViewId,
832        pipeline_id: PipelineId,
833        paint_api: &CrossProcessPaintApi,
834        font_resolver: Arc<dyn FontResolver>,
835    ) -> Arc<dyn ImageCache> {
836        let image_string_href_resolver = Box::new(move |_: &str, _: &usvg::Options| {
837            // Do not try to load `href` in <image> as local file path.
838            None
839        });
840        let font_resolver2 = font_resolver.clone();
841        let font_resolver3 = font_resolver.clone();
842        let usvg_font_resolver = usvg::FontResolver {
843            select_font: Box::new(move |font, database| font_resolver2.resolve(font, database)),
844            select_fallback: Box::new(move |char, ids, database| {
845                font_resolver3.resolve_fallback(char, ids, database)
846            }),
847        };
848
849        let opt = usvg::Options {
850            image_href_resolver: usvg::ImageHrefResolver {
851                resolve_data: usvg::ImageHrefResolver::default_data_resolver(),
852                resolve_string: image_string_href_resolver,
853            },
854            font_resolver: usvg_font_resolver,
855            fontdb: Arc::new(fontdb::Database::new()),
856            max_depth: MAX_SVG_DEPTH,
857            ..usvg::Options::default()
858        };
859
860        Arc::new(ImageCacheImpl {
861            store: Arc::new(Mutex::new(ImageCacheStore {
862                pending_loads: AllPendingLoads::new(),
863                completed_loads: HashMap::new(),
864                vector_images: FxHashMap::default(),
865                rasterized_vector_images: FxHashMap::default(),
866                broken_image_icon_image: OnceCell::new(),
867                paint_api: paint_api.clone(),
868                pipeline_id,
869                webview_id,
870                key_cache: KeyCache::new(),
871                svg_rasterization_task_store: SvgRasterizationTaskStore::default(),
872            })),
873            svg_id_image_id_map: Arc::new(Mutex::new(FxHashMap::default())),
874            broken_image_icon_data: self.broken_image_icon_data.clone(),
875            thread_pool: self.thread_pool.clone(),
876            usvg_options: Arc::new(opt),
877            usvg_font_resolver: font_resolver.clone(),
878        })
879    }
880}
881
882pub struct ImageCacheImpl {
883    /// Per-[`ImageCache`] data.
884    store: Arc<Mutex<ImageCacheStore>>,
885    /// Maps an SVGElement uuid to a pending image id in the store
886    svg_id_image_id_map: Arc<Mutex<FxHashMap<Uuid, PendingImageId>>>,
887    /// The data to use for the broken image icon used when images cannot load.
888    broken_image_icon_data: Arc<ResourceBytes>,
889    /// Thread pool for image decoding. This is shared with other [`ImageCache`]s in the
890    /// same process.
891    thread_pool: Arc<ThreadPool>,
892    /// The options for usvg. Contains a fontdb::Database and fontresolver.
893    usvg_options: Arc<usvg::Options<'static>>,
894    /// A font resolve used for resolving fonts when rasterizing SVGs.
895    ///
896    /// This is only used inside `usvg::Options` but is here so we can measure it.
897    usvg_font_resolver: Arc<dyn FontResolver>,
898}
899
900impl ImageCache for ImageCacheImpl {
901    fn memory_reports(&self, prefix: &str, ops: &mut MallocSizeOfOps) -> Vec<Report> {
902        let store_size = self.store.lock().size_of(ops);
903        let fontdb_size = self.usvg_options.conditional_size_of(ops);
904        let broken_image_size = self.broken_image_icon_data.conditional_size_of(ops);
905        let svg_id_map = self.svg_id_image_id_map.conditional_size_of(ops);
906        let svg_font_resolver = self.usvg_font_resolver.size_of(ops);
907        vec![
908            Report {
909                path: path![prefix, "image-cache", "cache"],
910                kind: ReportKind::ExplicitSystemHeapSize,
911                size: store_size,
912            },
913            Report {
914                path: path![prefix, "image-cache", "svg_id_map"],
915                kind: ReportKind::ExplicitSystemHeapSize,
916                size: svg_id_map,
917            },
918            Report {
919                path: path![prefix, "image-cache", "broken_image_icon"],
920                kind: ReportKind::ExplicitSystemHeapSize,
921                size: broken_image_size,
922            },
923            Report {
924                path: path![prefix, "image-cache", "svg_font_resolver"],
925                kind: ReportKind::ExplicitSystemHeapSize,
926                size: svg_font_resolver,
927            },
928            Report {
929                path: path![prefix, "image-cache", "usvg_options"],
930                kind: ReportKind::ExplicitSystemHeapSize,
931                size: fontdb_size,
932            },
933        ]
934    }
935
936    #[cfg(feature = "test-util")]
937    fn number_of_rasterize_tasks(&self) -> usize {
938        self.store.lock().number_of_rasterize_tasks()
939    }
940
941    fn get_image_key(&self) -> Option<WebRenderImageKey> {
942        let mut store = self.store.lock();
943        if let KeyCacheState::Ready(ref mut cache) = store.key_cache.cache {
944            if let Some(image_key) = cache.pop() {
945                return Some(image_key);
946            }
947
948            store.fetch_more_image_keys();
949        }
950
951        store
952            .paint_api
953            .generate_image_key_blocking(store.webview_id)
954    }
955
956    fn get_image(
957        &self,
958        url: ServoUrl,
959        origin: ImmutableOrigin,
960        cors_setting: Option<CorsSettings>,
961    ) -> Option<Image> {
962        let store = self.store.lock();
963        let result = store.get_completed_image_if_available(url, origin, cors_setting);
964        match result {
965            Some(Ok((img, _))) => Some(img),
966            _ => None,
967        }
968    }
969
970    fn get_cached_image_status(
971        &self,
972        url: ServoUrl,
973        origin: &ImmutableOrigin,
974        cors_setting: Option<CorsSettings>,
975    ) -> ImageCacheResult {
976        let mut store = self.store.lock();
977        if let Some(result) =
978            store.get_completed_image_if_available(url.clone(), origin.clone(), cors_setting)
979        {
980            match result {
981                Ok((image, image_url)) => {
982                    debug!("{} is available", url);
983                    return ImageCacheResult::Available(ImageOrMetadataAvailable::ImageAvailable {
984                        image,
985                        url: image_url,
986                    });
987                },
988                Err(()) => {
989                    debug!("{} is not available", url);
990                    return ImageCacheResult::FailedToLoadOrDecode;
991                },
992            }
993        }
994
995        let result = store
996            .pending_loads
997            .get_cached(url.clone(), origin, cors_setting);
998        match result {
999            CacheResult::Hit(key, pl) => match (&pl.result, &pl.metadata) {
1000                (&Some(Ok(_)), _) => ImageCacheResult::Pending(key),
1001                (&None, Some(meta)) => {
1002                    debug!("Metadata available for {} ({:?})", url, key);
1003                    ImageCacheResult::Available(ImageOrMetadataAvailable::MetadataAvailable(
1004                        *meta, key,
1005                    ))
1006                },
1007                (&Some(Err(_)), _) | (&None, &None) => {
1008                    debug!("{} ({:?}) is still pending", url, key);
1009                    ImageCacheResult::Pending(key)
1010                },
1011            },
1012            CacheResult::Miss(Some((key, _pl))) => {
1013                debug!("Should be requesting {} ({:?})", url, key);
1014                ImageCacheResult::ReadyForRequest(key)
1015            },
1016            CacheResult::Miss(None) => {
1017                debug!("Couldn't find an entry for {}", url);
1018                ImageCacheResult::FailedToLoadOrDecode
1019            },
1020        }
1021    }
1022
1023    fn add_rasterization_complete_listener(
1024        &self,
1025        pipeline_id: PipelineId,
1026        image_id: PendingImageId,
1027        requested_size: DeviceIntSize,
1028        callback: ImageCacheResponseCallback,
1029    ) {
1030        {
1031            let mut store = self.store.lock();
1032            let key = (image_id, requested_size);
1033            if !store.vector_images.contains_key(&image_id) {
1034                warn!("Unknown image requested for rasterization for key {key:?}");
1035                return;
1036            };
1037
1038            let Some(task) = store.rasterized_vector_images.get_mut(&key) else {
1039                warn!("Image rasterization task not found in the cache for key {key:?}");
1040                return;
1041            };
1042
1043            // If `result` is `None`, the task is still pending.
1044            if task.result.is_none() {
1045                task.listeners.push((pipeline_id, callback));
1046                return;
1047            }
1048        }
1049
1050        callback(ImageCacheResponseMessage::VectorImageRasterizationComplete(
1051            RasterizationCompleteResponse {
1052                pipeline_id,
1053                image_id,
1054                requested_size,
1055            },
1056        ));
1057    }
1058
1059    fn rasterize_vector_image(
1060        &self,
1061        image_id: PendingImageId,
1062        requested_size: DeviceIntSize,
1063        svg_id: Option<Uuid>,
1064    ) -> RasterizationResult {
1065        let mut store = self.store.lock();
1066        let Some(vector_image) = store.vector_images.get(&image_id).cloned() else {
1067            warn!("Unknown image id {image_id:?} requested for rasterization");
1068            return RasterizationResult::InvalidSvgParameters;
1069        };
1070
1071        // This early return relies on the fact that the result of image rasterization cannot
1072        // ever be `None`. If that were the case we would need to check whether the entry
1073        // in the `HashMap` was `Occupied` or not.
1074        let entry = store
1075            .rasterized_vector_images
1076            .entry((image_id, requested_size))
1077            .or_default();
1078        if let Some(result) = entry.result.as_ref() {
1079            return RasterizationResult::Image(result.clone());
1080        }
1081
1082        if let Some(svg_id) = svg_id &&
1083            let Some(old_mapped_image_id) =
1084                self.svg_id_image_id_map.lock().insert(svg_id, image_id) &&
1085            old_mapped_image_id != image_id
1086        {
1087            store.vector_images.remove(&old_mapped_image_id);
1088            store
1089                .rasterized_vector_images
1090                .remove(&(old_mapped_image_id, requested_size));
1091            store
1092                .svg_rasterization_task_store
1093                .remove_all_for_id(old_mapped_image_id);
1094        }
1095
1096        let natural_size = vector_image.svg_tree.size().to_int_size();
1097        let natural_tinyskia_size = {
1098            let width = natural_size.width().min(MAX_SVG_PIXMAP_DIMENSION);
1099            let height = natural_size.height().min(MAX_SVG_PIXMAP_DIMENSION);
1100            tiny_skia::IntSize::from_wh(width, height)
1101        };
1102        let tinyskia_requested_size = {
1103            let width = requested_size
1104                .width
1105                .try_into()
1106                .unwrap_or(0)
1107                .min(MAX_SVG_PIXMAP_DIMENSION);
1108            let height = requested_size
1109                .height
1110                .try_into()
1111                .unwrap_or(0)
1112                .min(MAX_SVG_PIXMAP_DIMENSION);
1113            tiny_skia::IntSize::from_wh(width, height).or(natural_tinyskia_size)
1114        };
1115        let Some(tinyskia_requested_size) = tinyskia_requested_size else {
1116            debug!(
1117                "Asked for requested size {:?} which has zero size, and natural size {:?} has zero dimension. Not returning image",
1118                requested_size, natural_size
1119            );
1120            return RasterizationResult::InvalidSvgParameters;
1121        };
1122
1123        if natural_tinyskia_size.is_none() {
1124            debug!("SVG natural size has zero dimension. Not returning image");
1125            return RasterizationResult::InvalidSvgParameters;
1126        }
1127
1128        if store
1129            .svg_rasterization_task_store
1130            .is_or_set_being_rasterized(image_id, requested_size)
1131        {
1132            return RasterizationResult::PendingRasterization;
1133        }
1134
1135        let store = self.store.clone();
1136        self.thread_pool.spawn(move || {
1137            let transform = tiny_skia::Transform::from_scale(
1138                tinyskia_requested_size.width() as f32 / natural_size.width() as f32,
1139                tinyskia_requested_size.height() as f32 / natural_size.height() as f32,
1140            );
1141            let mut pixmap = tiny_skia::Pixmap::new(
1142                tinyskia_requested_size.width(),
1143                tinyskia_requested_size.height(),
1144            )
1145            .unwrap();
1146
1147            // Some SVG documents, primarily ones created by fuzzers, can cause resvg to fail
1148            // assertions and panic. We catch any panics in `resvg::render` here so that we don't
1149            // crash the whole engine for such cases. In case of a panic, the completion listeners
1150            // added for this request will never get called.
1151            //
1152            // We also need to set `SUPPRESS_ABORT_IN_PANIC_HOOK` for the duration of the call
1153            // because servoshell registers a custom hook that intercepts the panic and crashes
1154            // the process when run in hard_fail mode.
1155            //
1156            // `AssertUnwindSafe` should be safe here since we will remove the `vector_image`
1157            // from `store.vector_images` and won't use it again. This assumes `resvg::render`
1158            // doesn't use internal global state that could become invalid after the panic.
1159            SUPPRESS_ABORT_IN_PANIC_HOOK.set(true);
1160            let resvg_result = std::panic::catch_unwind(AssertUnwindSafe(|| {
1161                resvg::render(&vector_image.svg_tree, transform, &mut pixmap.as_mut());
1162
1163                let bytes = pixmap.take();
1164                let frame = ImageFrame {
1165                    delay: None,
1166                    byte_range: 0..bytes.len(),
1167                    width: tinyskia_requested_size.width(),
1168                    height: tinyskia_requested_size.height(),
1169                };
1170
1171                RasterImage {
1172                    metadata: ImageMetadata {
1173                        width: tinyskia_requested_size.width(),
1174                        height: tinyskia_requested_size.height(),
1175                    },
1176                    format: PixelFormat::RGBA8,
1177                    frames: vec![frame],
1178                    bytes: Arc::new(bytes),
1179                    id: None,
1180                    cors_status: vector_image.cors_status,
1181                    load_time: None,
1182                    is_opaque: false,
1183                    loop_count: None,
1184                }
1185            }));
1186            SUPPRESS_ABORT_IN_PANIC_HOOK.set(false);
1187
1188            match resvg_result {
1189                Ok(rasterized_image) => {
1190                    let mut store = store.lock();
1191                    store.load_image_with_keycache(PendingKey::Svg((
1192                        image_id,
1193                        rasterized_image,
1194                        requested_size,
1195                    )));
1196                },
1197                Err(_) => {
1198                    warn!("resvg panicked while rasterizing SVG image {image_id:?} at {requested_size:?}");
1199                    let mut store = store.lock();
1200                    // Clean up data for this rasterization request.
1201                    store
1202                        .svg_rasterization_task_store
1203                        .remove_being_rasterized(image_id, requested_size);
1204                    store
1205                        .rasterized_vector_images
1206                        .remove(&(image_id, requested_size));
1207
1208                    // Remove the `image_id` from `vector_images` so the check at the top of this
1209                    // method will fail for subsequent calls and won't trigger rasterization
1210                    // again. Note, however, this means subsequent rasterization requests for
1211                    // a different `requested_size` will also fail, but the panics in resvg so far
1212                    // seem indepenent of the size, so this should be good enough.
1213                    store.vector_images.remove(&image_id);
1214                },
1215            }
1216        });
1217        RasterizationResult::PendingRasterization
1218    }
1219
1220    /// Add a new listener for the given pending image id. If the image is already present,
1221    /// the responder will still receive the expected response.
1222    fn add_listener(&self, listener: ImageLoadListener) {
1223        let mut store = self.store.lock();
1224        self.add_listener_with_store(&mut store, listener);
1225    }
1226
1227    fn evict_completed_image(
1228        &self,
1229        url: &ServoUrl,
1230        origin: &ImmutableOrigin,
1231        cors_setting: &Option<CorsSettings>,
1232    ) {
1233        let mut store = self.store.lock();
1234        store.remove_loaded_image(url, origin, cors_setting);
1235    }
1236
1237    fn evict_rasterized_image(&self, svg_id: &Uuid) {
1238        let mut store = self.store.lock();
1239        if let Some(mapped_image_id) = self.svg_id_image_id_map.lock().remove(svg_id) {
1240            store.pending_loads.remove(&mapped_image_id);
1241            store.vector_images.remove(&mapped_image_id);
1242            let images_to_remove = store
1243                .rasterized_vector_images
1244                .keys()
1245                .filter(|(id, _size)| *id == mapped_image_id)
1246                .cloned()
1247                .collect::<Vec<_>>();
1248            for (id, requested_size) in images_to_remove {
1249                store.remove_rasterized_vector_image(&id, &requested_size);
1250            }
1251        }
1252    }
1253
1254    /// Inform the image cache about a response for a pending request.
1255    fn notify_pending_response(&self, id: PendingImageId, action: FetchResponseMsg) {
1256        match (action, id) {
1257            (FetchResponseMsg::ProcessRequestBody(..), _) |
1258            (FetchResponseMsg::ProcessCspViolations(..), _) => (),
1259            (FetchResponseMsg::ProcessResponse(_, response), _) => {
1260                debug!("Received {:?} for {:?}", response.as_ref().map(|_| ()), id);
1261                let mut store = self.store.lock();
1262                if let Some(pending_load) = store.pending_loads.get_by_key_mut(&id) {
1263                    let (cors_status, metadata): (_, Option<Metadata>) = match response {
1264                        Ok(fetch_metadata) => {
1265                            (fetch_metadata.cors_status(), Some(fetch_metadata.into()))
1266                        },
1267                        Err(_) => (CorsStatus::Unsafe, None),
1268                    };
1269                    let (final_url, content_type) = metadata.map_or((None, None), |metadata| {
1270                        (
1271                            Some(metadata.final_url),
1272                            metadata
1273                                .content_type
1274                                .map(|content_type| content_type.into_inner().into()),
1275                        )
1276                    });
1277                    pending_load.final_url = final_url;
1278                    pending_load.content_type = content_type;
1279                    pending_load.cors_status = cors_status;
1280                } else {
1281                    debug!("Pending load for id {:?} already evicted from cache", id);
1282                }
1283            },
1284            (FetchResponseMsg::ProcessResponseChunk(_, data), _) => {
1285                debug!("Got some data for {:?}", id);
1286                let mut store = self.store.lock();
1287                if let Some(pending_load) = store.pending_loads.get_by_key_mut(&id) {
1288                    pending_load.bytes.extend_from_slice(&data);
1289
1290                    // jmr0 TODO: possibly move to another task?
1291                    if pending_load.metadata.is_none() {
1292                        let mut reader = std::io::Cursor::new(pending_load.bytes.as_slice());
1293                        if let Ok(info) = imsz_from_reader(&mut reader) {
1294                            let img_metadata = ImageMetadata {
1295                                width: info.width as u32,
1296                                height: info.height as u32,
1297                            };
1298                            for listener in &pending_load.listeners {
1299                                listener.respond(ImageResponse::MetadataLoaded(img_metadata));
1300                            }
1301                            pending_load.metadata = Some(img_metadata);
1302                        }
1303                    }
1304                } else {
1305                    debug!("Pending load for id {:?} already evicted from cache", id);
1306                }
1307            },
1308            (FetchResponseMsg::ProcessResponseEOF(_, result, timing), key) => {
1309                debug!("Received EOF for {:?}", key);
1310                match result {
1311                    Ok(_) => {
1312                        let (bytes, cors_status, content_type) = {
1313                            let mut store = self.store.lock();
1314                            if let Some(pending_load) = store.pending_loads.get_by_key_mut(&id) {
1315                                pending_load.result = Some(Ok(()));
1316                                pending_load.response_end = Some(
1317                                    timing.response_end.unwrap_or_else(CrossProcessInstant::now),
1318                                );
1319                                debug!("Async decoding {} ({:?})", pending_load.url, key);
1320                                (
1321                                    pending_load.bytes.mark_complete(),
1322                                    pending_load.cors_status,
1323                                    pending_load.content_type.clone(),
1324                                )
1325                            } else {
1326                                debug!("Pending load for id {:?} already evicted from cache", id);
1327                                return;
1328                            }
1329                        };
1330
1331                        let local_store = self.store.clone();
1332                        let usvg_options = self.usvg_options.clone();
1333                        self.thread_pool.spawn(move || {
1334                            let msg = decode_bytes_sync(
1335                                key,
1336                                &bytes,
1337                                cors_status,
1338                                content_type,
1339                                usvg_options,
1340                            );
1341                            local_store.lock().handle_decoder(msg);
1342                        });
1343                    },
1344                    Err(error) => {
1345                        debug!("Processing error for {key:?}: {error:?}");
1346                        let mut store = self.store.lock();
1347                        store.complete_load(id, LoadResult::FailedToLoadOrDecode)
1348                    },
1349                }
1350            },
1351            (FetchResponseMsg::ProcessContentLength(_response_id, size), _key) => {
1352                let mut store = self.store.lock();
1353                if let Some(pending_load) = store.pending_loads.get_by_key_mut(&id) {
1354                    pending_load.bytes.set_capacity(size);
1355                }
1356            },
1357        }
1358    }
1359
1360    /// This method does not block
1361    fn dispatch_fill_key_cache_with_batch_of_keys(&self, image_keys: Vec<WebRenderImageKey>) {
1362        // This is safe to do because of the following reason.
1363        // The only way this can be in a unwelcome state is the following chain of events
1364        // dispatch_fill_key -> get_image_key -> fetch_image_keys -> insert_keys_and_load_images.
1365        // However, we ignore all calls for this when the state is set to processing. Returning
1366        // the state to anything else enforces that we have the exclusive write access to the KeyCache.
1367        self.store.lock().key_cache.cache = KeyCacheState::Processing;
1368
1369        let store = self.store.clone();
1370        self.thread_pool.spawn(move || {
1371            store.lock().insert_keys_and_load_images(image_keys);
1372        });
1373    }
1374
1375    fn clear(&self) {
1376        self.store.lock().clear();
1377        *self.svg_id_image_id_map.lock() = Default::default();
1378    }
1379
1380    fn get_broken_image_icon(&self) -> Option<Arc<RasterImage>> {
1381        let store = self.store.lock();
1382        store
1383            .broken_image_icon_image
1384            .get_or_init(|| {
1385                let mut image = load_from_memory(
1386                    self.broken_image_icon_data.as_ref().as_ref(),
1387                    CorsStatus::Unsafe,
1388                )
1389                .or_else(|| load_from_memory(FALLBACK_RIPPY, CorsStatus::Unsafe))?;
1390                let image_key = store
1391                    .paint_api
1392                    .generate_image_key_blocking(store.webview_id)
1393                    .expect("Could not generate image key for broken image icon");
1394                set_webrender_image_key(&store.paint_api, &mut image, image_key);
1395                Some(Arc::new(image))
1396            })
1397            .clone()
1398    }
1399}
1400
1401impl ImageCacheStore {
1402    /// Clear the image cache.
1403    // Webrender currently does not care about keys that are loaded but do not have an image attached to it.
1404    fn clear(&mut self) {
1405        let deletions: smallvec::SmallVec<_> = self
1406            .completed_loads
1407            .values()
1408            .filter_map(|load| match &load.image_response {
1409                ImageResponse::Loaded(Image::Raster(image), _) => {
1410                    image.id.map(ImageUpdate::DeleteImage)
1411                },
1412                _ => None,
1413            })
1414            .chain(
1415                self.rasterized_vector_images
1416                    .values()
1417                    .filter_map(|task| task.result.as_ref()?.id.map(ImageUpdate::DeleteImage)),
1418            )
1419            .chain(
1420                self.broken_image_icon_image
1421                    .get()
1422                    .and_then(|icon| icon.as_ref())
1423                    .and_then(|icon| icon.id)
1424                    .map(ImageUpdate::DeleteImage),
1425            )
1426            .collect();
1427        if !deletions.is_empty() {
1428            self.paint_api
1429                .update_images(self.webview_id.into(), deletions);
1430        }
1431        // Clear these fields, since `clear()` will be called multiple times,
1432        // explicitly on pipeline close, and again on Drop (as a safeguard,
1433        // since we could forget to explicitly clear).
1434        self.completed_loads = Default::default();
1435        self.vector_images = Default::default();
1436        self.rasterized_vector_images = Default::default();
1437        self.svg_rasterization_task_store = Default::default();
1438        self.pending_loads = Default::default();
1439        self.key_cache = Default::default();
1440
1441        let _ = self.broken_image_icon_image.take();
1442    }
1443}
1444
1445impl Drop for ImageCacheStore {
1446    fn drop(&mut self) {
1447        self.clear();
1448    }
1449}
1450
1451impl ImageCacheImpl {
1452    /// Require self.store.lock() before calling.
1453    fn add_listener_with_store(&self, store: &mut ImageCacheStore, listener: ImageLoadListener) {
1454        let id = listener.id;
1455        if let Some(load) = store.pending_loads.get_by_key_mut(&id) {
1456            if let Some(ref metadata) = load.metadata {
1457                listener.respond(ImageResponse::MetadataLoaded(*metadata));
1458            }
1459            load.add_listener(listener);
1460            return;
1461        }
1462        if let Some(load) = store.completed_loads.values().find(|l| l.id == id) {
1463            listener.respond(load.image_response.clone());
1464            return;
1465        }
1466        warn!("Couldn't find cached entry for listener {:?}", id);
1467    }
1468}