gdk4/
texture.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::{ffi, prelude::*, Texture};
6
7// rustdoc-stripper-ignore-next
8/// Trait containing manually implemented methods of
9/// [`Texture`](crate::Texture).
10pub trait TextureExtManual: IsA<Texture> + 'static {
11    /// Downloads the @self into local memory.
12    ///
13    /// This may be an expensive operation, as the actual texture data
14    /// may reside on a GPU or on a remote display server.
15    ///
16    /// The data format of the downloaded data is equivalent to
17    /// `CAIRO_FORMAT_ARGB32`, so every downloaded pixel requires
18    /// 4 bytes of memory.
19    ///
20    /// Downloading a texture into a Cairo image surface:
21    /// **⚠️ The following code is in c ⚠️**
22    ///
23    /// ```c
24    /// surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
25    ///                                       gdk_texture_get_width (texture),
26    ///                                       gdk_texture_get_height (texture));
27    /// gdk_texture_download (texture,
28    ///                       cairo_image_surface_get_data (surface),
29    ///                       cairo_image_surface_get_stride (surface));
30    /// cairo_surface_mark_dirty (surface);
31    /// ```
32    ///
33    /// For more flexible download capabilities, see
34    /// [`TextureDownloader`][crate::TextureDownloader].
35    /// ## `data`
36    /// pointer to enough memory to be filled with the
37    ///   downloaded data of @self
38    /// ## `stride`
39    /// rowstride in bytes
40    #[doc(alias = "gdk_texture_download")]
41    fn download(&self, data: &mut [u8], stride: usize) {
42        unsafe {
43            assert!(
44                stride >= 4,
45                "Stride for a CAIRO_FORMAT_ARGB32 should be >= 4"
46            );
47            assert!(
48                stride as i32 >= self.as_ref().width() * 4,
49                "The stride must be >= 4*width"
50            );
51            assert!(
52                data.len() as i32 >= stride as i32 * self.as_ref().height(),
53                "The data is not big enough to download the texture"
54            );
55            ffi::gdk_texture_download(self.as_ref().to_glib_none().0, data.as_mut_ptr(), stride);
56        }
57    }
58}
59
60impl<O: IsA<Texture>> TextureExtManual for O {}