| 1 |
use alloc::{boxed::Box, vec::Vec}; |
| 2 |
|
| 3 |
use crate::{Api, Capabilities, ExposedAdapter, Instance, InstanceError}; |
| 4 |
|
| 5 |
use super::{DynAdapter, DynResource, DynResourceExt as _, DynSurface}; |
| 6 |
|
| 7 |
pub struct DynExposedAdapter { |
| 8 |
pub adapter: Box<dyn DynAdapter>, |
| 9 |
pub info: wgt::AdapterInfo, |
| 10 |
pub features: wgt::Features, |
| 11 |
pub capabilities: Capabilities, |
| 12 |
} |
| 13 |
|
| 14 |
impl DynExposedAdapter { |
| 15 |
/// Returns the backend this adapter is using. |
| 16 |
pub fn backend(&self) -> wgt::Backend { |
| 17 |
self.info.backend |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl<A: Api> From<ExposedAdapter<A>> for DynExposedAdapter { |
| 22 |
fn from(exposed_adapter: ExposedAdapter<A>) -> Self { |
| 23 |
Self { |
| 24 |
adapter: Box::new(exposed_adapter.adapter), |
| 25 |
info: exposed_adapter.info, |
| 26 |
features: exposed_adapter.features, |
| 27 |
capabilities: exposed_adapter.capabilities, |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
pub trait DynInstance: DynResource { |
| 33 |
unsafe fn create_surface( |
| 34 |
&self, |
| 35 |
display_handle: raw_window_handle::RawDisplayHandle, |
| 36 |
window_handle: raw_window_handle::RawWindowHandle, |
| 37 |
) -> Result<Box<dyn DynSurface>, InstanceError>; |
| 38 |
|
| 39 |
unsafe fn enumerate_adapters( |
| 40 |
&self, |
| 41 |
surface_hint: Option<&dyn DynSurface>, |
| 42 |
) -> Vec<DynExposedAdapter>; |
| 43 |
} |
| 44 |
|
| 45 |
impl<I: Instance + DynResource> DynInstance for I { |
| 46 |
unsafe fn create_surface( |
| 47 |
&self, |
| 48 |
display_handle: raw_window_handle::RawDisplayHandle, |
| 49 |
window_handle: raw_window_handle::RawWindowHandle, |
| 50 |
) -> Result<Box<dyn DynSurface>, InstanceError> { |
| 51 |
unsafe { I::create_surface(self, display_handle, window_handle) } |
| 52 |
.map(|surface| -> Box<dyn DynSurface> { Box::new(surface) }) |
| 53 |
} |
| 54 |
|
| 55 |
unsafe fn enumerate_adapters( |
| 56 |
&self, |
| 57 |
surface_hint: Option<&dyn DynSurface>, |
| 58 |
) -> Vec<DynExposedAdapter> { |
| 59 |
let surface_hint = surface_hint.map(|s| s.expect_downcast_ref()); |
| 60 |
unsafe { I::enumerate_adapters(self, surface_hint) } |
| 61 |
.into_iter() |
| 62 |
.map(|exposed| DynExposedAdapter { |
| 63 |
adapter: Box::new(exposed.adapter), |
| 64 |
info: exposed.info, |
| 65 |
features: exposed.features, |
| 66 |
capabilities: exposed.capabilities, |
| 67 |
}) |
| 68 |
.collect() |
| 69 |
} |
| 70 |
} |