iced_wgpu/
engine.rs

1use crate::graphics::{Antialiasing, Shell};
2use crate::primitive;
3use crate::quad;
4use crate::text;
5use crate::triangle;
6
7use std::sync::{Arc, RwLock};
8
9#[derive(Clone)]
10pub struct Engine {
11    pub(crate) device: wgpu::Device,
12    pub(crate) queue: wgpu::Queue,
13    pub(crate) format: wgpu::TextureFormat,
14
15    pub(crate) quad_pipeline: quad::Pipeline,
16    pub(crate) text_pipeline: text::Pipeline,
17    pub(crate) triangle_pipeline: triangle::Pipeline,
18    #[cfg(any(feature = "image", feature = "svg"))]
19    pub(crate) image_pipeline: crate::image::Pipeline,
20    pub(crate) primitive_storage: Arc<RwLock<primitive::Storage>>,
21    _shell: Shell,
22}
23
24impl Engine {
25    pub fn new(
26        _adapter: &wgpu::Adapter,
27        device: wgpu::Device,
28        queue: wgpu::Queue,
29        format: wgpu::TextureFormat,
30        antialiasing: Option<Antialiasing>, // TODO: Initialize AA pipelines lazily
31        shell: Shell,
32    ) -> Self {
33        Self {
34            format,
35
36            quad_pipeline: quad::Pipeline::new(&device, format),
37            text_pipeline: text::Pipeline::new(&device, &queue, format),
38            triangle_pipeline: triangle::Pipeline::new(&device, format, antialiasing),
39
40            #[cfg(any(feature = "image", feature = "svg"))]
41            image_pipeline: {
42                let backend = _adapter.get_info().backend;
43
44                crate::image::Pipeline::new(&device, format, backend)
45            },
46
47            primitive_storage: Arc::new(RwLock::new(primitive::Storage::default())),
48
49            device,
50            queue,
51            _shell: shell,
52        }
53    }
54
55    #[cfg(any(feature = "image", feature = "svg"))]
56    pub fn create_image_cache(&self) -> crate::image::Cache {
57        self.image_pipeline
58            .create_cache(&self.device, &self.queue, &self._shell)
59    }
60
61    pub fn trim(&mut self) {
62        self.text_pipeline.trim();
63
64        self.primitive_storage
65            .write()
66            .expect("primitive storage should be writable")
67            .trim();
68    }
69}