Skip to main content

ferroui_base/platform/
i_external_objects_render_interface_context_feature.rs

1use super::{IBitmapImpl, IPlatformHandle, PlatformGraphicsDrmFormat, PlatformGraphicsExternalImageProperties};
2use crate::rendering::composition::{CompositionGpuImportedImageSynchronizationCapabilities, ICompositionImportableSharedGpuContextImage};
3use std::rc::Rc;
4use std::sync::Arc;
5
6/// The import of GPU objects created outside of the framework into the
7/// render context: a feature of the render interface context.
8pub trait IExternalObjectsRenderInterfaceContextFeature {
9    /// Returns the list of image handle types supported by the current GPU backend, see [`KnownPlatformGraphicsExternalImageHandleTypes`](super::KnownPlatformGraphicsExternalImageHandleTypes)
10    fn supported_image_handle_types(&self) -> Vec<String>;
11
12    /// Returns the list of semaphore types supported by the current GPU backend, see [`KnownPlatformGraphicsExternalSemaphoreHandleTypes`](super::KnownPlatformGraphicsExternalSemaphoreHandleTypes)
13    fn supported_semaphore_types(&self) -> Vec<String>;
14
15    /// Returns the DRM format/modifier pairs the current GPU backend can import as
16    /// [`KnownPlatformGraphicsExternalImageHandleTypes::DMA_BUF_FILE_DESCRIPTOR`](super::KnownPlatformGraphicsExternalImageHandleTypes::DMA_BUF_FILE_DESCRIPTOR) images,
17    /// or `None` when the backend cannot enumerate them.
18    fn supported_dma_buf_formats(&self) -> Option<Vec<PlatformGraphicsDrmFormat>> {
19        None
20    }
21
22    fn import_image(
23        &self,
24        handle: Rc<dyn IPlatformHandle>,
25        properties: PlatformGraphicsExternalImageProperties,
26    ) -> Rc<dyn IPlatformRenderInterfaceImportedImage>;
27
28    /// Imports an image of a context that shares with this one. The image
29    /// is shared with the thread that made it, which keeps it and disposes
30    /// it: this is called on the thread that renders.
31    fn import_shared_image(
32        &self,
33        image: Arc<dyn ICompositionImportableSharedGpuContextImage>,
34    ) -> Rc<dyn IPlatformRenderInterfaceImportedImage>;
35
36    fn import_semaphore(&self, handle: Rc<dyn IPlatformHandle>) -> Rc<dyn IPlatformRenderInterfaceImportedSemaphore>;
37
38    fn get_synchronization_capabilities(&self, image_handle_type: &str)
39        -> CompositionGpuImportedImageSynchronizationCapabilities;
40
41    fn device_uuid(&self) -> Option<Vec<u8>>;
42
43    fn device_luid(&self) -> Option<Vec<u8>>;
44}
45
46/// This interface allows proper management of ref-counted platform handles.
47/// If we immediately wrap the handle, the caller can destroy its copy immediately after the call
48/// This is needed for MoltenVK-based users that can e.g. get an MTLSharedEvent from a VkSemaphore.
49/// This does NOT actually increase the ref-counter of MTLSharedEvent, since it's declared as
50/// __unsafe_unretained in vulkan headers.
51/// Same happens with exporting an IOSurfaceRef from a VkImage.
52/// So in a case when the VkSemaphore or VkImage is destroyed, the "handle" which is actually a pointer
53/// will be pointing to a dead object.
54/// To prevent this we need to increase the reference counter in a handle-specific means
55/// synchronously before returning control back to the user.
56///
57/// This is not needed for fds or DXGI handles, since those are _created_ on demand as proper NT handles
58pub trait IExternalObjectsHandleWrapRenderInterfaceContextFeature {
59    fn wrap_image_handle_on_any_thread(
60        &self,
61        handle: &Rc<dyn IPlatformHandle>,
62        properties: PlatformGraphicsExternalImageProperties,
63    ) -> Option<Rc<dyn IExternalObjectsWrappedGpuHandle>>;
64
65    fn wrap_semaphore_handle_on_any_thread(
66        &self,
67        handle: &Rc<dyn IPlatformHandle>,
68    ) -> Option<Rc<dyn IExternalObjectsWrappedGpuHandle>>;
69}
70
71/// A platform handle that holds a reference of its own to the GPU object;
72/// disposing it releases that reference.
73pub trait IExternalObjectsWrappedGpuHandle: IPlatformHandle {
74    fn dispose(&self);
75
76    /// The handle as a plain platform handle (an explicit upcast).
77    fn as_platform_handle(self: Rc<Self>) -> Rc<dyn IPlatformHandle>;
78}
79
80/// A GPU object imported into the render context.
81pub trait IPlatformRenderInterfaceImportedObject {
82    fn dispose(&self);
83}
84
85/// A GPU image imported into the render context.
86pub trait IPlatformRenderInterfaceImportedImage: IPlatformRenderInterfaceImportedObject {
87    fn snapshot_with_keyed_mutex(&self, acquire_index: u32, release_index: u32) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
88
89    fn snapshot_with_semaphores(
90        &self,
91        wait_for_semaphore: &Rc<dyn IPlatformRenderInterfaceImportedSemaphore>,
92        signal_semaphore: &Rc<dyn IPlatformRenderInterfaceImportedSemaphore>,
93    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
94
95    fn snapshot_with_timeline_semaphores(
96        &self,
97        wait_for_semaphore: &Rc<dyn IPlatformRenderInterfaceImportedSemaphore>,
98        wait_for_value: u64,
99        signal_semaphore: &Rc<dyn IPlatformRenderInterfaceImportedSemaphore>,
100        signal_value: u64,
101    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
102
103    fn snapshot_with_automatic_sync(&self) -> std::sync::Arc<crate::platform::SharedBitmapImpl>;
104}
105
106/// A GPU semaphore imported into the render context.
107pub trait IPlatformRenderInterfaceImportedSemaphore: IPlatformRenderInterfaceImportedObject {
108    /// Lets the backend that imported the semaphore recover its concrete
109    /// type (the cast upstream performs on the semaphores of a snapshot).
110    fn as_any(&self) -> &dyn std::any::Any;
111}