skia_safe/gpu/ganesh/
yuva_backend_textures.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::gpu::{BackendFormat, BackendTexture, Mipmapped, SurfaceOrigin};
use crate::{prelude::*, YUVAInfo, YUVColorSpace};
use skia_bindings::{self as sb, GrYUVABackendTextureInfo, GrYUVABackendTextures};
use std::{fmt, iter};

/// A description of a set [BackendTexture]s that hold the planar data described by a [YUVAInfo].
pub type YUVABackendTextureInfo = Handle<GrYUVABackendTextureInfo>;
unsafe_send_sync!(YUVABackendTextureInfo);

impl NativeDrop for GrYUVABackendTextureInfo {
    fn drop(&mut self) {
        unsafe { sb::C_GrYUVABackendTextureInfo_destruct(self) }
    }
}

impl NativeClone for GrYUVABackendTextureInfo {
    fn clone(&self) -> Self {
        construct(|cloned| unsafe { sb::C_GrYUVABackendTextureInfo_CopyConstruct(cloned, self) })
    }
}

impl NativePartialEq for GrYUVABackendTextureInfo {
    fn eq(&self, rhs: &Self) -> bool {
        unsafe { sb::C_GrYUVABackendTextureInfo_equals(self, rhs) }
    }
}

impl fmt::Debug for YUVABackendTextureInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("YUVABackendTextureInfo")
            .field("yuva_info", &self.yuva_info())
            .field("yuv_color_space", &self.yuv_color_space())
            .field("mipmapped", &self.mipmapped())
            .field("texture_origin", &self.texture_origin())
            .field("plane_formats", &self.plane_formats())
            .finish()
    }
}

impl YUVABackendTextureInfo {
    pub const MAX_PLANES: usize = YUVAInfo::MAX_PLANES;

    /// Initializes a [YUVABackendTextureInfo] to describe a set of textures that can store the
    /// planes indicated by the [YUVAInfo]. The texture dimensions are taken from the [YUVAInfo]'s
    /// plane dimensions. All the described textures share a common origin. The planar image this
    /// describes will be mip mapped if all the textures are individually mip mapped as indicated
    /// by [Mipmapped]. This will return [None] if the passed formats' channels don't agree with [YUVAInfo].
    pub fn new(
        info: &YUVAInfo,
        formats: &[BackendFormat],
        mip_mapped: Mipmapped,
        origin: SurfaceOrigin,
    ) -> Option<Self> {
        if formats.len() != info.num_planes() {
            return None;
        }

        let mut formats = formats.to_vec();
        formats.extend(
            iter::repeat_with(BackendFormat::new_invalid).take(Self::MAX_PLANES - formats.len()),
        );
        assert_eq!(formats.len(), Self::MAX_PLANES);

        let n = unsafe {
            GrYUVABackendTextureInfo::new(info.native(), formats[0].native(), mip_mapped, origin)
        };
        Self::native_is_valid(&n).if_true_then_some(|| Self::from_native_c(n))
    }

    pub fn yuva_info(&self) -> &YUVAInfo {
        YUVAInfo::from_native_ref(&self.native().fYUVAInfo)
    }

    pub fn yuv_color_space(&self) -> YUVColorSpace {
        self.yuva_info().yuv_color_space()
    }

    pub fn mipmapped(&self) -> Mipmapped {
        self.native().fMipmapped
    }

    pub fn texture_origin(&self) -> SurfaceOrigin {
        self.native().fTextureOrigin
    }

    /// The number of [crate::Pixmap] planes.
    pub fn num_planes(&self) -> usize {
        self.yuva_info().num_planes()
    }

    /// Format of the ith plane, or `None` if `i >= Self::num_planes()`
    pub fn plane_format(&self, i: usize) -> Option<&BackendFormat> {
        (i < self.num_planes()).if_true_some(BackendFormat::from_native_ref(
            &self.native().fPlaneFormats[i],
        ))
    }

    /// All plane formats.
    pub fn plane_formats(&self) -> &[BackendFormat] {
        unsafe {
            let formats = BackendFormat::from_native_ref(&self.native().fPlaneFormats[0]);
            safer::from_raw_parts(formats, self.num_planes())
        }
    }

    /// Returns `true` if this has been configured with a valid [YUVAInfo] with compatible texture.
    pub(crate) fn native_is_valid(info: &GrYUVABackendTextureInfo) -> bool {
        YUVAInfo::native_is_valid(&info.fYUVAInfo)
    }
}

/// A set of [BackendTexture]s that hold the planar data for an image described a [YUVAInfo].
pub type YUVABackendTextures = Handle<GrYUVABackendTextures>;
unsafe_send_sync!(YUVABackendTextures);

impl NativeDrop for GrYUVABackendTextures {
    fn drop(&mut self) {
        unsafe { sb::C_GrYUVABackendTextures_destruct(self) }
    }
}

impl fmt::Debug for YUVABackendTextures {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("YUVABackendTextures")
            .field("yuva_info", &self.yuva_info())
            .field("texture_origin", &self.texture_origin())
            .field("textures", &self.textures())
            .finish()
    }
}

impl YUVABackendTextures {
    pub fn new(
        info: &YUVAInfo,
        textures: &[BackendTexture],
        texture_origin: SurfaceOrigin,
    ) -> Option<Self> {
        if textures.len() != info.num_planes() {
            return None;
        }
        let new_invalid = BackendTexture::new_invalid();
        let new_invalid_ptr = new_invalid.native() as *const _;

        let mut texture_handles = textures
            .iter()
            .map(|tex| tex.native() as *const _)
            .collect::<Vec<_>>();
        texture_handles
            .extend(iter::repeat(new_invalid_ptr).take(YUVAInfo::MAX_PLANES - textures.len()));
        assert_eq!(texture_handles.len(), YUVAInfo::MAX_PLANES);

        let n = construct(|cloned| unsafe {
            sb::C_GrYUVABackendTextures_construct(
                cloned,
                info.native(),
                texture_handles.as_ptr(),
                texture_origin,
            )
        });
        Self::native_is_valid(&n).if_true_then_some(|| Self::from_native_c(n))
    }

    pub fn textures(&self) -> Vec<BackendTexture> {
        unsafe {
            let textures_ptr = sb::C_GrYUVABackendTextures_textures(self.native());
            let textures = safer::from_raw_parts(textures_ptr, self.num_planes());
            textures
                .iter()
                .map(|native_texture_ref| {
                    BackendTexture::from_ptr(sb::C_GrBackendTexture_Clone(native_texture_ref))
                        .unwrap()
                })
                .collect()
        }
    }

    pub fn texture(&self, i: usize) -> Option<BackendTexture> {
        self.textures().get(i).cloned()
    }

    pub fn yuva_info(&self) -> &YUVAInfo {
        YUVAInfo::from_native_ref(unsafe { &*sb::C_GrYUVABackendTextures_yuvaInfo(self.native()) })
    }

    pub fn num_planes(&self) -> usize {
        self.yuva_info().num_planes()
    }

    pub fn texture_origin(&self) -> SurfaceOrigin {
        unsafe { sb::C_GrYUVABackendTextures_textureOrigin(self.native()) }
    }

    pub(crate) fn native_is_valid(n: &GrYUVABackendTextures) -> bool {
        YUVAInfo::native_is_valid(unsafe { &*sb::C_GrYUVABackendTextures_yuvaInfo(n) })
    }
}