Skip to main content

skia_safe/gpu/ganesh/mtl/
surface_metal.rs

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