iced_wgpu/image/
cache.rs

1use crate::core::{self, Size};
2use crate::image::atlas::{self, Atlas};
3
4use std::sync::Arc;
5
6#[derive(Debug)]
7pub struct Cache {
8    atlas: Atlas,
9    #[cfg(feature = "image")]
10    raster: crate::image::raster::Cache,
11    #[cfg(feature = "svg")]
12    vector: crate::image::vector::Cache,
13}
14
15impl Cache {
16    pub fn new(
17        device: &wgpu::Device,
18        backend: wgpu::Backend,
19        layout: Arc<wgpu::BindGroupLayout>,
20    ) -> Self {
21        Self {
22            atlas: Atlas::new(device, backend, layout),
23            #[cfg(feature = "image")]
24            raster: crate::image::raster::Cache::default(),
25            #[cfg(feature = "svg")]
26            vector: crate::image::vector::Cache::default(),
27        }
28    }
29
30    pub fn bind_group(&self) -> &wgpu::BindGroup {
31        self.atlas.bind_group()
32    }
33
34    pub fn layer_count(&self) -> usize {
35        self.atlas.layer_count()
36    }
37
38    #[cfg(feature = "image")]
39    pub fn measure_image(&mut self, handle: &core::image::Handle) -> Size<u32> {
40        self.raster.load(handle).dimensions()
41    }
42
43    #[cfg(feature = "svg")]
44    pub fn measure_svg(&mut self, handle: &core::svg::Handle) -> Size<u32> {
45        self.vector.load(handle).viewport_dimensions()
46    }
47
48    #[cfg(feature = "image")]
49    pub fn upload_raster(
50        &mut self,
51        device: &wgpu::Device,
52        encoder: &mut wgpu::CommandEncoder,
53        handle: &core::image::Handle,
54    ) -> Option<&atlas::Entry> {
55        self.raster.upload(device, encoder, handle, &mut self.atlas)
56    }
57
58    #[cfg(feature = "svg")]
59    pub fn upload_vector(
60        &mut self,
61        device: &wgpu::Device,
62        encoder: &mut wgpu::CommandEncoder,
63        handle: &core::svg::Handle,
64        color: Option<core::Color>,
65        size: [f32; 2],
66        scale: f32,
67    ) -> Option<&atlas::Entry> {
68        self.vector.upload(
69            device,
70            encoder,
71            handle,
72            color,
73            size,
74            scale,
75            &mut self.atlas,
76        )
77    }
78
79    pub fn trim(&mut self) {
80        #[cfg(feature = "image")]
81        self.raster.trim(&mut self.atlas);
82
83        #[cfg(feature = "svg")]
84        self.vector.trim(&mut self.atlas);
85    }
86}