1use crate::{gpu, prelude::*, Surface, SurfaceProps};
2use skia_bindings as sb;
3
4/// Creates [`Surface`] from CAMetalLayer.
5/// Returned [`Surface`] takes a reference on the CAMetalLayer. The ref on the layer will be
6/// released when the [`Surface`] is destroyed.
7///
8/// Only available when Metal API is enabled.
9///
10/// Will grab the current drawable from the layer and use its texture as a `backend_rt` to
11/// create a renderable surface.
12///
13/// * `context` - GPU context
14/// * `layer` - [`gpu::mtl::Handle`] (expected to be a CAMetalLayer*)
15/// * `sample_cnt` - samples per pixel, or 0 to disable full scene anti-aliasing
16/// * `color_space` - range of colors; may be `None`
17/// * `surface_props` - LCD striping orientation and setting for device independent
18/// fonts; may be `None`
19/// * `drawable` - Pointer to drawable to be filled in when this surface is
20/// instantiated; may not be `None`
21/// Returns: created [`Surface`], or `None`
22#[allow(clippy::missing_safety_doc)]
23#[allow(clippy::too_many_arguments)]
24pub unsafe fn wrap_ca_metal_layer(
25 context: &mut gpu::RecordingContext,
26 layer: gpu::mtl::Handle,
27 origin: gpu::SurfaceOrigin,
28 sample_cnt: impl Into<Option<usize>>,
29 color_type: crate::ColorType,
30 color_space: impl Into<Option<crate::ColorSpace>>,
31 surface_props: Option<&SurfaceProps>,
32 drawable: *mut gpu::mtl::Handle,
33) -> Option<Surface> {
34 Surface::from_ptr(sb::C_SkSurfaces_WrapCAMetalLayer(
35 context.native_mut(),
36 layer,
37 origin,
38 sample_cnt.into().unwrap_or(0).try_into().unwrap(),
39 color_type.into_native(),
40 color_space.into().into_ptr_or_null(),
41 surface_props.native_ptr_or_null(),
42 drawable,
43 ))
44}
45
46/// Creates [`Surface`] from MTKView.
47/// Returned [`Surface`] takes a reference on the MTKView. The ref on the layer will be
48/// released when the [`Surface`] is destroyed.
49///
50/// Only available when Metal API is enabled.
51///
52/// Will grab the current drawable from the layer and use its texture as a `backend_rt` to
53/// create a renderable surface.
54///
55/// * `context` - GPU context
56/// * `layer` - [`gpu::mtl::Handle`] (expected to be a MTKView*)
57/// * `sample_cnt` - samples per pixel, or 0 to disable full scene anti-aliasing
58/// * `color_space` - range of colors; may be `None`
59/// * `surface_props` - LCD striping orientation and setting for device independent
60/// fonts; may be `None`
61/// Returns: created [`Surface`], or `None`
62#[allow(clippy::missing_safety_doc)]
63#[cfg(feature = "metal")]
64pub unsafe fn wrap_mtk_view(
65 context: &mut gpu::RecordingContext,
66 mtk_view: gpu::mtl::Handle,
67 origin: gpu::SurfaceOrigin,
68 sample_count: impl Into<Option<usize>>,
69 color_type: crate::ColorType,
70 color_space: impl Into<Option<crate::ColorSpace>>,
71 surface_props: Option<&SurfaceProps>,
72) -> Option<Surface> {
73 Surface::from_ptr(sb::C_SkSurfaces_WrapMTKView(
74 context.native_mut(),
75 mtk_view,
76 origin,
77 sample_count.into().unwrap_or(0).try_into().unwrap(),
78 color_type.into_native(),
79 color_space.into().into_ptr_or_null(),
80 surface_props.native_ptr_or_null(),
81 ))
82}
83