skia_safe/gpu/ganesh/mtl/
surface_metal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::{gpu, prelude::*, Surface, SurfaceProps};
use skia_bindings as sb;

/// Creates [`Surface`] from CAMetalLayer.
/// Returned [`Surface`] takes a reference on the CAMetalLayer. The ref on the layer will be
/// released when the [`Surface`] is destroyed.
///
/// Only available when Metal API is enabled.
///
/// Will grab the current drawable from the layer and use its texture as a `backend_rt` to
/// create a renderable surface.
///
/// * `context` - GPU context
/// * `layer` - [`gpu::mtl::Handle`] (expected to be a CAMetalLayer*)
/// * `sample_cnt` - samples per pixel, or 0 to disable full scene anti-aliasing
/// * `color_space` - range of colors; may be `None`
/// * `surface_props` - LCD striping orientation and setting for device independent
///                        fonts; may be `None`
/// * `drawable` - Pointer to drawable to be filled in when this surface is
///                        instantiated; may not be `None`
///
/// Returns: created [`Surface`], or `None`
#[allow(clippy::missing_safety_doc)]
#[allow(clippy::too_many_arguments)]
pub unsafe fn wrap_ca_metal_layer(
    context: &mut gpu::RecordingContext,
    layer: gpu::mtl::Handle,
    origin: gpu::SurfaceOrigin,
    sample_cnt: impl Into<Option<usize>>,
    color_type: crate::ColorType,
    color_space: impl Into<Option<crate::ColorSpace>>,
    surface_props: Option<&SurfaceProps>,
    drawable: *mut gpu::mtl::Handle,
) -> Option<Surface> {
    Surface::from_ptr(sb::C_SkSurfaces_WrapCAMetalLayer(
        context.native_mut(),
        layer,
        origin,
        sample_cnt.into().unwrap_or(0).try_into().unwrap(),
        color_type.into_native(),
        color_space.into().into_ptr_or_null(),
        surface_props.native_ptr_or_null(),
        drawable,
    ))
}

/// Creates [`Surface`] from MTKView.
/// Returned [`Surface`] takes a reference on the MTKView. The ref on the layer will be
/// released when the [`Surface`] is destroyed.
///
/// Only available when Metal API is enabled.
///
/// Will grab the current drawable from the layer and use its texture as a `backend_rt` to
/// create a renderable surface.
///
/// * `context` - GPU context
/// * `layer` - [`gpu::mtl::Handle`] (expected to be a MTKView*)
/// * `sample_cnt` - samples per pixel, or 0 to disable full scene anti-aliasing
/// * `color_space` - range of colors; may be `None`
/// * `surface_props` - LCD striping orientation and setting for device independent
///                        fonts; may be `None`
///
/// Returns: created [`Surface`], or `None`
#[allow(clippy::missing_safety_doc)]
#[cfg(feature = "metal")]
pub unsafe fn wrap_mtk_view(
    context: &mut gpu::RecordingContext,
    mtk_view: gpu::mtl::Handle,
    origin: gpu::SurfaceOrigin,
    sample_count: impl Into<Option<usize>>,
    color_type: crate::ColorType,
    color_space: impl Into<Option<crate::ColorSpace>>,
    surface_props: Option<&SurfaceProps>,
) -> Option<Surface> {
    Surface::from_ptr(sb::C_SkSurfaces_WrapMTKView(
        context.native_mut(),
        mtk_view,
        origin,
        sample_count.into().unwrap_or(0).try_into().unwrap(),
        color_type.into_native(),
        color_space.into().into_ptr_or_null(),
        surface_props.native_ptr_or_null(),
    ))
}