epaint/
texture_handle.rs

1use std::sync::Arc;
2
3use crate::{
4    emath::NumExt, mutex::RwLock, textures::TextureOptions, ImageData, ImageDelta, TextureId,
5    TextureManager,
6};
7
8/// Used to paint images.
9///
10/// An _image_ is pixels stored in RAM, and represented using [`ImageData`].
11/// Before you can paint it however, you need to convert it to a _texture_.
12///
13/// If you are using egui, use `egui::Context::load_texture`.
14///
15/// The [`TextureHandle`] can be cloned cheaply.
16/// When the last [`TextureHandle`] for specific texture is dropped, the texture is freed.
17///
18/// See also [`TextureManager`].
19#[must_use]
20pub struct TextureHandle {
21    tex_mngr: Arc<RwLock<TextureManager>>,
22    id: TextureId,
23}
24
25impl Drop for TextureHandle {
26    fn drop(&mut self) {
27        self.tex_mngr.write().free(self.id);
28    }
29}
30
31impl Clone for TextureHandle {
32    fn clone(&self) -> Self {
33        self.tex_mngr.write().retain(self.id);
34        Self {
35            tex_mngr: self.tex_mngr.clone(),
36            id: self.id,
37        }
38    }
39}
40
41impl PartialEq for TextureHandle {
42    #[inline]
43    fn eq(&self, other: &Self) -> bool {
44        self.id == other.id
45    }
46}
47
48impl Eq for TextureHandle {}
49
50impl std::hash::Hash for TextureHandle {
51    #[inline]
52    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
53        self.id.hash(state);
54    }
55}
56
57impl TextureHandle {
58    /// If you are using egui, use `egui::Context::load_texture` instead.
59    pub fn new(tex_mngr: Arc<RwLock<TextureManager>>, id: TextureId) -> Self {
60        Self { tex_mngr, id }
61    }
62
63    #[inline]
64    pub fn id(&self) -> TextureId {
65        self.id
66    }
67
68    /// Assign a new image to an existing texture.
69    #[allow(clippy::needless_pass_by_ref_mut)] // Intentionally hide interiority of mutability
70    pub fn set(&mut self, image: impl Into<ImageData>, options: TextureOptions) {
71        self.tex_mngr
72            .write()
73            .set(self.id, ImageDelta::full(image.into(), options));
74    }
75
76    /// Assign a new image to a subregion of the whole texture.
77    #[allow(clippy::needless_pass_by_ref_mut)] // Intentionally hide interiority of mutability
78    pub fn set_partial(
79        &mut self,
80        pos: [usize; 2],
81        image: impl Into<ImageData>,
82        options: TextureOptions,
83    ) {
84        self.tex_mngr
85            .write()
86            .set(self.id, ImageDelta::partial(pos, image.into(), options));
87    }
88
89    /// width x height
90    pub fn size(&self) -> [usize; 2] {
91        self.tex_mngr
92            .read()
93            .meta(self.id)
94            .map_or([0, 0], |tex| tex.size)
95    }
96
97    /// width x height
98    pub fn size_vec2(&self) -> crate::Vec2 {
99        let [w, h] = self.size();
100        crate::Vec2::new(w as f32, h as f32)
101    }
102
103    /// `width x height x bytes_per_pixel`
104    pub fn byte_size(&self) -> usize {
105        self.tex_mngr
106            .read()
107            .meta(self.id)
108            .map_or(0, |tex| tex.bytes_used())
109    }
110
111    /// width / height
112    pub fn aspect_ratio(&self) -> f32 {
113        let [w, h] = self.size();
114        w as f32 / h.at_least(1) as f32
115    }
116
117    /// Debug-name.
118    pub fn name(&self) -> String {
119        self.tex_mngr
120            .read()
121            .meta(self.id)
122            .map_or_else(|| "<none>".to_owned(), |tex| tex.name.clone())
123    }
124}
125
126impl From<&TextureHandle> for TextureId {
127    #[inline(always)]
128    fn from(handle: &TextureHandle) -> Self {
129        handle.id()
130    }
131}
132
133impl From<&mut TextureHandle> for TextureId {
134    #[inline(always)]
135    fn from(handle: &mut TextureHandle) -> Self {
136        handle.id()
137    }
138}