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