dacite/core/
event.rs

1// Copyright (c) 2017, Dennis Hamester <dennis.hamester@startmail.com>
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9// FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13// PERFORMANCE OF THIS SOFTWARE.
14
15use FromNativeObject;
16use TryDestroyError;
17use TryDestroyErrorKind;
18use VulkanObject;
19use core::allocator_helper::AllocatorHelper;
20use core::{self, Device};
21use std::cmp::Ordering;
22use std::hash::{Hash, Hasher};
23use std::ptr;
24use std::sync::Arc;
25use vks;
26
27/// See [`VkEvent`](https://www.khronos.org/registry/vulkan/specs/1.0-extensions/html/vkspec.html#VkEvent)
28#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct Event(Arc<Inner>);
30
31impl VulkanObject for Event {
32    type NativeVulkanObject = vks::core::VkEvent;
33
34    #[inline]
35    fn id(&self) -> u64 {
36        self.handle()
37    }
38
39    #[inline]
40    fn as_native_vulkan_object(&self) -> Self::NativeVulkanObject {
41        self.handle()
42    }
43
44    fn try_destroy(self) -> Result<(), TryDestroyError<Self>> {
45        let strong_count = Arc::strong_count(&self.0);
46        if strong_count == 1 {
47            Ok(())
48        }
49        else {
50            Err(TryDestroyError::new(self, TryDestroyErrorKind::InUse(Some(strong_count))))
51        }
52    }
53}
54
55pub struct FromNativeEventParameters {
56    /// `true`, if this `Event` should destroy the underlying Vulkan object, when it is dropped.
57    pub owned: bool,
58
59    /// The `Device`, from which this `Event` was created.
60    pub device: Device,
61
62    /// An `Allocator` compatible with the one used to create this `Event`.
63    ///
64    /// This parameter is ignored, if `owned` is `false`.
65    pub allocator: Option<Box<core::Allocator>>,
66}
67
68impl FromNativeEventParameters {
69    #[inline]
70    pub fn new(owned: bool, device: Device, allocator: Option<Box<core::Allocator>>) -> Self {
71        FromNativeEventParameters {
72            owned: owned,
73            device: device,
74            allocator: allocator,
75        }
76    }
77}
78
79impl FromNativeObject for Event {
80    type Parameters = FromNativeEventParameters;
81
82    unsafe fn from_native_object(object: Self::NativeVulkanObject, params: Self::Parameters) -> Self {
83        Event::new(object, params.owned, params.device, params.allocator.map(AllocatorHelper::new))
84    }
85}
86
87impl Event {
88    pub(crate) fn new(handle: vks::core::VkEvent, owned: bool, device: Device, allocator: Option<AllocatorHelper>) -> Self {
89        Event(Arc::new(Inner {
90            handle: handle,
91            owned: owned,
92            device: device,
93            allocator: allocator,
94        }))
95    }
96
97    #[inline]
98    pub(crate) fn handle(&self) -> vks::core::VkEvent {
99        self.0.handle
100    }
101
102    #[inline]
103    pub(crate) fn loader(&self) -> &vks::DeviceProcAddrLoader {
104        self.0.device.loader()
105    }
106
107    #[inline]
108    pub(crate) fn device_handle(&self) -> vks::core::VkDevice {
109        self.0.device.handle()
110    }
111
112    /// See [`vkGetEventStatus`](https://www.khronos.org/registry/vulkan/specs/1.0-extensions/html/vkspec.html#vkGetEventStatus)
113    pub fn get_status(&self) -> Result<bool, core::Error> {
114        let res = unsafe {
115            self.loader().core.vkGetEventStatus(self.device_handle(), self.handle())
116        };
117
118        match res {
119            vks::core::VK_EVENT_SET => Ok(true),
120            vks::core::VK_EVENT_RESET => Ok(false),
121            _ => Err(res.into()),
122        }
123    }
124
125    /// See [`vkSetEvent`](https://www.khronos.org/registry/vulkan/specs/1.0-extensions/html/vkspec.html#vkSetEvent)
126    pub fn set(&self) -> Result<(), core::Error> {
127        let res = unsafe {
128            self.loader().core.vkSetEvent(self.device_handle(), self.handle())
129        };
130
131        if res == vks::core::VK_SUCCESS {
132            Ok(())
133        }
134        else {
135            Err(res.into())
136        }
137    }
138
139    /// See [`vkResetEvent`](https://www.khronos.org/registry/vulkan/specs/1.0-extensions/html/vkspec.html#vkResetEvent)
140    pub fn reset(&self) -> Result<(), core::Error> {
141        let res = unsafe {
142            self.loader().core.vkResetEvent(self.device_handle(), self.handle())
143        };
144
145        if res == vks::core::VK_SUCCESS {
146            Ok(())
147        }
148        else {
149            Err(res.into())
150        }
151    }
152}
153
154#[derive(Debug)]
155struct Inner {
156    handle: vks::core::VkEvent,
157    owned: bool,
158    device: Device,
159    allocator: Option<AllocatorHelper>,
160}
161
162impl Drop for Inner {
163    fn drop(&mut self) {
164        if self.owned {
165            let allocator = match self.allocator {
166                Some(ref allocator) => allocator.callbacks(),
167                None => ptr::null(),
168            };
169
170            unsafe {
171                self.device.loader().core.vkDestroyEvent(self.device.handle(), self.handle, allocator);
172            }
173        }
174    }
175}
176
177unsafe impl Send for Inner { }
178
179unsafe impl Sync for Inner { }
180
181impl PartialEq for Inner {
182    #[inline]
183    fn eq(&self, other: &Self) -> bool {
184        self.handle == other.handle
185    }
186}
187
188impl Eq for Inner { }
189
190impl PartialOrd for Inner {
191    #[inline]
192    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
193        self.handle.partial_cmp(&other.handle)
194    }
195}
196
197impl Ord for Inner {
198    #[inline]
199    fn cmp(&self, other: &Self) -> Ordering {
200        self.handle.cmp(&other.handle)
201    }
202}
203
204impl Hash for Inner {
205    #[inline]
206    fn hash<H: Hasher>(&self, state: &mut H) {
207        self.handle.hash(state);
208    }
209}