gdk4/
dmabuf_texture_builder.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, DmabufTextureBuilder, Texture};
6
7impl DmabufTextureBuilder {
8    /// Builds a new [`Texture`][crate::Texture] with the values set up in the builder.
9    ///
10    /// It is a programming error to call this function if any mandatory property has not been set.
11    ///
12    /// Not all formats defined in the `drm_fourcc.h` header are supported. You can use
13    /// [`DisplayExt::dmabuf_formats()`][crate::prelude::DisplayExt::dmabuf_formats()] to get a list of supported formats. If the
14    /// format is not supported by GTK, [`None`] will be returned and @error will be set.
15    ///
16    /// The `destroy` function gets called when the returned texture gets released.
17    ///
18    /// It is the responsibility of the caller to keep the file descriptors for the planes
19    /// open until the created texture is no longer used, and close them afterwards (possibly
20    /// using the @destroy notify).
21    ///
22    /// It is possible to call this function multiple times to create multiple textures,
23    /// possibly with changing properties in between.
24    ///
25    /// # Returns
26    ///
27    /// a newly built [`Texture`][crate::Texture] or `NULL`
28    ///   if the format is not supported
29    #[doc(alias = "gdk_dmabuf_texture_builder_build")]
30    #[must_use = "The builder must be built to be used"]
31    #[allow(clippy::missing_safety_doc)]
32    pub unsafe fn build(self) -> Result<Texture, glib::Error> {
33        let mut error = std::ptr::null_mut();
34
35        let result = ffi::gdk_dmabuf_texture_builder_build(
36            self.to_glib_none().0,
37            None,
38            std::ptr::null_mut(),
39            &mut error,
40        );
41        if error.is_null() {
42            if result.is_null() {
43                Err(glib::Error::new(
44                    crate::DmabufError::UnsupportedFormat,
45                    "Unsupported format",
46                ))
47            } else {
48                Ok(from_glib_full(result))
49            }
50        } else {
51            Err(from_glib_full(error))
52        }
53    }
54
55    #[doc(alias = "gdk_dmabuf_texture_builder_build")]
56    #[must_use = "The builder must be built to be used"]
57    #[allow(clippy::missing_safety_doc)]
58    pub unsafe fn build_with_release_func<F: FnOnce() + Send + 'static>(
59        self,
60        release_func: F,
61    ) -> Result<Texture, glib::Error> {
62        let mut error = std::ptr::null_mut();
63        unsafe extern "C" fn destroy_closure<F: FnOnce() + Send + 'static>(
64            func: glib::ffi::gpointer,
65        ) {
66            let released_func = Box::<F>::from_raw(func as *mut _);
67            released_func();
68        }
69        let released_func = Box::new(release_func);
70        let result = ffi::gdk_dmabuf_texture_builder_build(
71            self.to_glib_none().0,
72            Some(destroy_closure::<F>),
73            Box::into_raw(released_func) as glib::ffi::gpointer,
74            &mut error,
75        );
76        if error.is_null() {
77            if result.is_null() {
78                Err(glib::Error::new(
79                    crate::DmabufError::UnsupportedFormat,
80                    "Unsupported format",
81                ))
82            } else {
83                Ok(from_glib_full(result))
84            }
85        } else {
86            Err(from_glib_full(error))
87        }
88    }
89}