1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

use crate::{prelude::*, Texture};

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::Texture>> Sealed for T {}
}

// rustdoc-stripper-ignore-next
/// Trait containing manually implemented methods of
/// [`Texture`](crate::Texture).
pub trait TextureExtManual: sealed::Sealed + IsA<Texture> + 'static {
    /// Downloads the @self into local memory.
    ///
    /// This may be an expensive operation, as the actual texture data
    /// may reside on a GPU or on a remote display server.
    ///
    /// The data format of the downloaded data is equivalent to
    /// `CAIRO_FORMAT_ARGB32`, so every downloaded pixel requires
    /// 4 bytes of memory.
    ///
    /// Downloading a texture into a Cairo image surface:
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
    ///                                       gdk_texture_get_width (texture),
    ///                                       gdk_texture_get_height (texture));
    /// gdk_texture_download (texture,
    ///                       cairo_image_surface_get_data (surface),
    ///                       cairo_image_surface_get_stride (surface));
    /// cairo_surface_mark_dirty (surface);
    /// ```
    ///
    /// For more flexible download capabilities, see
    /// [`TextureDownloader`][crate::TextureDownloader].
    /// ## `data`
    /// pointer to enough memory to be filled with the
    ///   downloaded data of @self
    /// ## `stride`
    /// rowstride in bytes
    #[doc(alias = "gdk_texture_download")]
    fn download(&self, data: &mut [u8], stride: usize) {
        unsafe {
            assert!(
                stride >= 4,
                "Stride for a CAIRO_FORMAT_ARGB32 should be >= 4"
            );
            assert!(
                stride as i32 >= self.as_ref().width() * 4,
                "The stride must be >= 4*width"
            );
            assert!(
                data.len() as i32 >= stride as i32 * self.as_ref().height(),
                "The data is not big enough to download the texture"
            );
            ffi::gdk_texture_download(self.as_ref().to_glib_none().0, data.as_mut_ptr(), stride);
        }
    }
}

impl<O: IsA<Texture>> TextureExtManual for O {}