Skip to main content

skia_safe/gpu/ganesh/mtl/
backend_surface.rs

1pub mod backend_formats {
2    use skia_bindings as sb;
3
4    use crate::{
5        gpu::{BackendFormat, mtl},
6        prelude::*,
7    };
8
9    pub fn make_mtl(format: mtl::PixelFormat) -> BackendFormat {
10        BackendFormat::construct(|bf| unsafe { sb::C_GrBackendFormats_ConstructMtl(bf, format) })
11            .assert_valid()
12    }
13
14    pub fn as_mtl_format(backend_format: &BackendFormat) -> Option<mtl::PixelFormat> {
15        let pixel_format = unsafe { sb::C_GrBackendFormats_AsMtlFormat(backend_format.native()) };
16        // Mtl's PixelFormat == 0 is invalid.
17        (pixel_format != 0).then_some(pixel_format)
18    }
19}
20
21pub mod backend_textures {
22    use skia_bindings as sb;
23
24    use crate::{
25        gpu::{self, BackendTexture, mtl},
26        prelude::*,
27    };
28
29    #[allow(clippy::missing_safety_doc)]
30    pub unsafe fn make_mtl(
31        (width, height): (i32, i32),
32        mipmapped: gpu::Mipmapped,
33        mtl_info: &mtl::TextureInfo,
34        label: impl AsRef<str>,
35    ) -> BackendTexture {
36        let label = label.as_ref().as_bytes();
37        unsafe {
38            BackendTexture::from_native_if_valid(sb::C_GrBackendTextures_newMtl(
39                width,
40                height,
41                mipmapped,
42                mtl_info.native(),
43                label.as_ptr() as _,
44                label.len(),
45            ))
46        }
47        .unwrap()
48    }
49
50    pub fn get_mtl_texture_info(texture: &BackendTexture) -> Option<mtl::TextureInfo> {
51        unsafe {
52            let mut texture_info = mtl::TextureInfo::default();
53            sb::C_GrBackendTextures_GetMtlTextureInfo(texture.native(), texture_info.native_mut())
54                .then_some(texture_info)
55        }
56    }
57}
58
59pub mod backend_render_targets {
60    use skia_bindings as sb;
61
62    use crate::{
63        gpu::{BackendRenderTarget, mtl},
64        prelude::*,
65    };
66
67    pub fn make_mtl(
68        (width, height): (i32, i32),
69        mtl_info: &mtl::TextureInfo,
70    ) -> BackendRenderTarget {
71        BackendRenderTarget::construct(|target| unsafe {
72            sb::C_GrBackendRenderTargets_ConstructMtl(target, width, height, mtl_info.native())
73        })
74    }
75
76    pub fn get_mtl_texture_info(render_target: &BackendRenderTarget) -> Option<mtl::TextureInfo> {
77        let mut info = mtl::TextureInfo::default();
78        unsafe {
79            sb::C_GrBackendRenderTargets_GetMtlTextureInfo(
80                render_target.native(),
81                info.native_mut(),
82            )
83        }
84        .then_some(info)
85    }
86}