Skip to main content

gstreamer_vulkan/
vulkan_operation.rs

1use crate::VulkanOperation;
2use crate::ffi;
3use glib::{prelude::*, translate::*};
4
5#[derive(Debug)]
6#[must_use = "Need to call `end`, otherwise drop will panic."]
7pub struct VulkanOperationGuard<'a> {
8    obj: &'a VulkanOperation,
9    ended: bool,
10}
11
12impl VulkanOperationGuard<'_> {
13    #[doc(alias = "gst_vulkan_operation_end")]
14    pub fn end(mut self) -> Result<(), glib::Error> {
15        self.ended = true;
16        unsafe {
17            let mut error = std::ptr::null_mut();
18            let is_ok = ffi::gst_vulkan_operation_end(self.obj.to_glib_none().0, &mut error);
19            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
20            if error.is_null() {
21                Ok(())
22            } else {
23                Err(from_glib_full(error))
24            }
25        }
26    }
27}
28
29impl Drop for VulkanOperationGuard<'_> {
30    fn drop(&mut self) {
31        if !self.ended {
32            panic!("Dropped a VulkanOperationGuard without calling `end`.")
33        }
34    }
35}
36impl PartialEq for VulkanOperationGuard<'_> {
37    fn eq(&self, other: &Self) -> bool {
38        self.obj == other.obj
39    }
40}
41impl Eq for VulkanOperationGuard<'_> {}
42
43pub trait VulkanOperationExtManual: IsA<VulkanOperation> + 'static {
44    // rustdoc-stripper-ignore-next
45    /// Returns a guard struct for the begun operation.
46    /// The `end` method on the guard **must** be called; Dropping it without results in a panic
47    // rustdoc-stripper-ignore-next-stop
48    /// See also: [`end()`][Self::end()] and [`VulkanOperationExt::reset()`][crate::prelude::VulkanOperationExt::reset()]
49    ///
50    /// Attempts to set the operation ready to work. It instantiates the common
51    /// command buffer in `self` and calls vkBeginCommandBuffer.
52    ///
53    /// After calling this function you can register commands in the command buffer,
54    /// and finally call [`end()`][Self::end()]. [`VulkanOperationExt::reset()`][crate::prelude::VulkanOperationExt::reset()] is
55    /// called internally if something failed.
56    ///
57    /// # Returns
58    ///
59    /// whether the operation started. It might fill `error`.
60    #[doc(alias = "gst_vulkan_operation_begin")]
61    fn begin<'a>(&'a self) -> Result<VulkanOperationGuard<'a>, glib::Error> {
62        unsafe {
63            let mut error = std::ptr::null_mut();
64            let is_ok = ffi::gst_vulkan_operation_begin(self.as_ref().to_glib_none().0, &mut error);
65            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
66            if !error.is_null() {
67                return Err(from_glib_full(error));
68            }
69        }
70        Ok(VulkanOperationGuard {
71            obj: self.upcast_ref(),
72            ended: false,
73        })
74    }
75}
76impl<O: IsA<VulkanOperation>> VulkanOperationExtManual for O {}