ferroui_vulkan/i_vulkan_platform_surface.rs
1use crate::{IVulkanPlatformGraphicsContext, IVulkanRenderTarget};
2use ferroui_base::platform::surfaces::IPlatformRenderSurface;
3use ferroui_base::PixelSize;
4use std::any::TypeId;
5use std::rc::Rc;
6use std::sync::Arc;
7
8/// A surface that is presented to through a `VkSurfaceKHR` and a swap chain.
9///
10/// A render surface announces that it is one through
11/// `try_get_surface_kind(TypeId::of::<dyn IVulkanKhrSurfacePlatformSurface>())`,
12/// which returns an `Rc<dyn IVulkanKhrSurfacePlatformSurface>`; see
13/// [`try_get_vulkan_khr_surface`].
14pub trait IVulkanKhrSurfacePlatformSurface: IPlatformRenderSurface {
15 /// The scaling from logical units to device pixels.
16 fn scaling(&self) -> f64;
17
18 /// The size of the surface in device pixels.
19 fn size(&self) -> PixelSize;
20
21 /// Creates the `VkSurfaceKHR` of the surface for the instance of
22 /// `context`.
23 ///
24 /// # Panics
25 /// Panics when the window system refuses the surface.
26 fn create_surface(&self, context: &dyn IVulkanPlatformGraphicsContext) -> u64;
27
28 /// Releases the surface.
29 fn dispose(&self);
30}
31
32/// The render surface viewed as a surface with a `VkSurfaceKHR`, when it is
33/// one.
34pub fn try_get_vulkan_khr_surface(
35 surface: &dyn IPlatformRenderSurface,
36) -> Option<Rc<dyn IVulkanKhrSurfacePlatformSurface>> {
37 surface
38 .try_get_surface_kind(TypeId::of::<dyn IVulkanKhrSurfacePlatformSurface>())?
39 .downcast_ref::<Rc<dyn IVulkanKhrSurfacePlatformSurface>>()
40 .cloned()
41}
42
43/// A platform render surface that provides its own [`IVulkanRenderTarget`],
44/// e.g. an offscreen image-based target that doesn't involve a swapchain.
45///
46/// A render surface announces that it is one through
47/// `try_get_surface_kind(TypeId::of::<dyn IVulkanRenderTargetPlatformSurface>())`,
48/// which returns an `Rc<dyn IVulkanRenderTargetPlatformSurface>`; see
49/// [`try_get_vulkan_render_target_surface`].
50///
51/// This API is private to the framework and its backends.
52pub trait IVulkanRenderTargetPlatformSurface: IPlatformRenderSurface {
53 /// Creates the render target of the surface for `context`.
54 fn create_render_target(&self, context: &Rc<dyn IVulkanPlatformGraphicsContext>) -> Rc<dyn IVulkanRenderTarget>;
55}
56
57/// The render surface viewed as a surface with its own Vulkan render
58/// target, when it is one.
59pub fn try_get_vulkan_render_target_surface(
60 surface: &dyn IPlatformRenderSurface,
61) -> Option<Rc<dyn IVulkanRenderTargetPlatformSurface>> {
62 surface
63 .try_get_surface_kind(TypeId::of::<dyn IVulkanRenderTargetPlatformSurface>())?
64 .downcast_ref::<Rc<dyn IVulkanRenderTargetPlatformSurface>>()
65 .cloned()
66}
67
68/// Lets a context present to surfaces that are not Vulkan surfaces
69/// themselves (the window of a platform).
70///
71/// A platform registers the factory among the platform features of its
72/// options ([`VulkanPlatformSpecificOptions`](crate::VulkanPlatformSpecificOptions)),
73/// from where every context of the platform graphics takes it; a context
74/// announces it through its optional features as an `Arc<dyn
75/// IVulkanKhrSurfacePlatformSurfaceFactory>` (`try_get_shared`).
76pub trait IVulkanKhrSurfacePlatformSurfaceFactory: Send + Sync {
77 /// Whether the context can present to the surface.
78 fn can_render_to_surface(
79 &self,
80 context: &dyn IVulkanPlatformGraphicsContext,
81 surface: &Arc<dyn IPlatformRenderSurface>,
82 ) -> bool;
83
84 /// Creates the Vulkan view of the surface.
85 fn create_surface(
86 &self,
87 context: &dyn IVulkanPlatformGraphicsContext,
88 surface: &Arc<dyn IPlatformRenderSurface>,
89 ) -> Rc<dyn IVulkanKhrSurfacePlatformSurface>;
90}