skia_safe/gpu/ganesh/gl/
types.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
use crate::{gpu, prelude::*};
use skia_bindings::{self as sb, GrGLFramebufferInfo, GrGLSurfaceInfo, GrGLTextureInfo};

pub use skia_bindings::GrGLFormat as Format;
variant_name!(Format::ALPHA8);
pub use skia_bindings::GrGLStandard as Standard;
variant_name!(Standard::GLES);
pub use skia_bindings::GrGLenum as Enum;
pub use skia_bindings::GrGLuint as UInt;

#[derive(Copy, Clone, Eq, Debug)]
#[repr(C)]
pub struct TextureInfo {
    pub target: Enum,
    pub id: Enum,
    pub format: Enum,
    pub protected: gpu::Protected,
}

native_transmutable!(GrGLTextureInfo, TextureInfo, texture_info_layout);

impl Default for TextureInfo {
    fn default() -> Self {
        Self {
            target: 0,
            id: 0,
            format: 0,
            protected: gpu::Protected::No,
        }
    }
}

impl PartialEq for TextureInfo {
    fn eq(&self, other: &Self) -> bool {
        unsafe { sb::C_GrGLTextureInfo_Equals(self.native(), other.native()) }
    }
}

impl TextureInfo {
    pub fn from_target_and_id(target: Enum, id: Enum) -> Self {
        Self {
            target,
            id,
            ..Default::default()
        }
    }

    pub fn is_protected(&self) -> bool {
        self.protected == gpu::Protected::Yes
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct FramebufferInfo {
    pub fboid: UInt,
    pub format: Enum,
    pub protected: gpu::Protected,
}

native_transmutable!(
    GrGLFramebufferInfo,
    FramebufferInfo,
    framebuffer_info_layout
);

impl Default for FramebufferInfo {
    fn default() -> Self {
        Self {
            fboid: 0,
            format: 0,
            protected: gpu::Protected::No,
        }
    }
}

impl FramebufferInfo {
    pub fn from_fboid(fboid: UInt) -> Self {
        Self {
            fboid,
            ..Default::default()
        }
    }

    pub fn is_protected(&self) -> bool {
        self.protected == gpu::Protected::Yes
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct SurfaceInfo {
    pub sample_count: u32,
    pub level_count: u32,
    pub protected: gpu::Protected,

    pub target: Enum,
    pub format: Enum,
}

native_transmutable!(GrGLSurfaceInfo, SurfaceInfo, surface_info_layout);

impl Default for SurfaceInfo {
    fn default() -> Self {
        Self {
            sample_count: 1,
            level_count: 0,
            protected: gpu::Protected::No,
            target: 0,
            format: 0,
        }
    }
}

bitflags! {
    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct BackendState: u32 {
        const RENDER_TARGET = sb::GrGLBackendState_kRenderTarget_GrGLBackendState as _;
        const TEXTURE_BINDING = sb::GrGLBackendState_kTextureBinding_GrGLBackendState as _;
        const VIEW = sb::GrGLBackendState_kView_GrGLBackendState as _;
        const BLEND = sb::GrGLBackendState_kBlend_GrGLBackendState as _;
        const MSAA_ENABLE = sb::GrGLBackendState_kMSAAEnable_GrGLBackendState as _;
        const VERTEX = sb::GrGLBackendState_kVertex_GrGLBackendState as _;
        const STENCIL = sb::GrGLBackendState_kStencil_GrGLBackendState as _;
        const PIXEL_STORE = sb::GrGLBackendState_kPixelStore_GrGLBackendState as _;
        const PROGRAM = sb::GrGLBackendState_kProgram_GrGLBackendState as _;
        const FIXED_FUNCTION = sb::GrGLBackendState_kFixedFunction_GrGLBackendState as _;
        const MISC = sb::GrGLBackendState_kMisc_GrGLBackendState as _;
    }
}

// TODO: BackendState::ALL

#[cfg(test)]
mod tests {
    use super::{Enum, Format};

    #[test]
    fn test_support_from_format_to_enum_and_back() {
        let e: Enum = Format::ALPHA8.into();
        let f: Format = e.into();
        assert_eq!(f, Format::ALPHA8);
    }

    #[test]
    fn test_all_formats_exhaustive() {
        use Format::*;
        let x = ALPHA8;
        // !!!!!
        // IF this match is not exhaustive anymore, the implementations of the format conversions
        // need to be updated in `skia-bindings/src/gl.cpp`, too.
        match x {
            Unknown => {}
            RGBA8 => {}
            R8 => {}
            ALPHA8 => {}
            LUMINANCE8 => {}
            LUMINANCE8_ALPHA8 => {}
            BGRA8 => {}
            RGB565 => {}
            RGBA16F => {}
            R16F => {}
            RGB8 => {}
            RGBX8 => {}
            RG8 => {}
            RGB10_A2 => {}
            RGBA4 => {}
            SRGB8_ALPHA8 => {}
            COMPRESSED_ETC1_RGB8 => {}
            COMPRESSED_RGB8_ETC2 => {}
            COMPRESSED_RGB8_BC1 => {}
            COMPRESSED_RGBA8_BC1 => {}
            R16 => {}
            RG16 => {}
            RGBA16 => {}
            RG16F => {}
            LUMINANCE16F => {}
            STENCIL_INDEX8 => {}
            STENCIL_INDEX16 => {}
            DEPTH24_STENCIL8 => {}
        }
    }

    #[test]
    fn test_format_last_color_and_last_exists() {
        let _ = Format::Last;
        let _ = Format::LastColorFormat;
    }
}