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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::sync::Arc;
use vks;
use ::{VdResult, CallResult, Handle, Device, EventCreateFlags, EventCreateInfo};

pub enum EventStatus {
    Signaled,
    Unsignaled,
    Error(CallResult),
}

impl From<CallResult> for EventStatus {
    fn from(res: CallResult) -> EventStatus {
        match res {
            CallResult::EventSet => EventStatus::Signaled,
            CallResult::EventReset => EventStatus::Unsignaled,
            _ => EventStatus::Error(res),
        }
    }
}

impl From<i32> for EventStatus {
    fn from(res: i32) -> EventStatus {
        CallResult::from(res).into()
    }
}



#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct EventHandle(pub(crate) vks::VkEvent);

impl EventHandle {
    #[inline(always)]
    pub fn to_raw(&self) -> vks::VkEvent {
        self.0
    }
}

unsafe impl Handle for EventHandle {
    type Target = EventHandle;

    fn handle(&self) -> Self::Target {
        *self
    }
}


#[derive(Debug)]
struct Inner {
    handle: EventHandle,
    device: Device,
}

impl Drop for Inner {
    fn drop(&mut self) {
        unsafe {
            self.device.destroy_event(self.handle, None);
        }
    }
}


/// An event.
///
///
/// ### Destruction
/// 
/// Dropping this `Event` will cause `Device::destroy_event` to be called, 
/// automatically releasing any resources associated with it.
///
#[derive(Debug, Clone)]
pub struct Event {
    inner: Arc<Inner>,
}

impl Event {
    pub fn new(device: Device, flags: EventCreateFlags) -> VdResult<Event> {
        let create_info = EventCreateInfo::builder()
            .flags(flags)
            .build();

        let handle = unsafe { device.create_event(&create_info, None)? };

        Ok(Event {
            inner: Arc::new(Inner {
                handle,
                device,
            })
        })
    }

    /// Returns this object's handle.
    pub fn handle(&self) -> EventHandle {
        self.inner.handle
    }

    /// Returns a reference to this object's associated device.
    pub fn device(&self) -> &Device {
        &self.inner.device
    }

    /// Sets this event to signaled state.
    ///
    /// https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkSetEvent.html
    ///
    pub fn set(&self) -> VdResult<()> {
        unsafe { self.inner.device.set_event(self.handle()) }
    }

    /// Resets this event to non-signaled state.
    ///
    /// https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkResetEvent.html
    ///
    pub fn reset(&self) -> VdResult<()> {
        unsafe { self.inner.device.reset_event(self.handle()) }
    }

    /// Retrieves the status of this event object.
    ///
    /// https://www.khronos.org/registry/vulkan/specs/1.0/man/html/vkGetEventStatus.html
    ///
    pub fn status(&self) -> VdResult<EventStatus> {
        unsafe { Ok(self.inner.device.get_event_status(self.handle())?.into()) }
    }
}

unsafe impl<'h> Handle for &'h Event {
    type Target = EventHandle;

    fn handle(&self) -> Self::Target {
        self.inner.handle
    }
}