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