skia_safe/gpu/ganesh/gl/
types.rs

1use crate::{gpu, prelude::*};
2use skia_bindings::{self as sb, GrGLFramebufferInfo, GrGLSurfaceInfo, GrGLTextureInfo};
3
4pub use skia_bindings::GrGLFormat as Format;
5variant_name!(Format::ALPHA8);
6pub use skia_bindings::GrGLStandard as Standard;
7variant_name!(Standard::GLES);
8pub use skia_bindings::GrGLenum as Enum;
9pub use skia_bindings::GrGLuint as UInt;
10
11#[derive(Copy, Clone, Eq, Debug)]
12#[repr(C)]
13pub struct TextureInfo {
14    pub target: Enum,
15    pub id: Enum,
16    pub format: Enum,
17    pub protected: gpu::Protected,
18}
19
20native_transmutable!(GrGLTextureInfo, TextureInfo, texture_info_layout);
21
22impl Default for TextureInfo {
23    fn default() -> Self {
24        Self {
25            target: 0,
26            id: 0,
27            format: 0,
28            protected: gpu::Protected::No,
29        }
30    }
31}
32
33impl PartialEq for TextureInfo {
34    fn eq(&self, other: &Self) -> bool {
35        unsafe { sb::C_GrGLTextureInfo_Equals(self.native(), other.native()) }
36    }
37}
38
39impl TextureInfo {
40    pub fn from_target_and_id(target: Enum, id: Enum) -> Self {
41        Self {
42            target,
43            id,
44            ..Default::default()
45        }
46    }
47
48    pub fn is_protected(&self) -> bool {
49        self.protected == gpu::Protected::Yes
50    }
51}
52
53#[derive(Copy, Clone, PartialEq, Eq, Debug)]
54#[repr(C)]
55pub struct FramebufferInfo {
56    pub fboid: UInt,
57    pub format: Enum,
58    pub protected: gpu::Protected,
59}
60
61native_transmutable!(
62    GrGLFramebufferInfo,
63    FramebufferInfo,
64    framebuffer_info_layout
65);
66
67impl Default for FramebufferInfo {
68    fn default() -> Self {
69        Self {
70            fboid: 0,
71            format: 0,
72            protected: gpu::Protected::No,
73        }
74    }
75}
76
77impl FramebufferInfo {
78    pub fn from_fboid(fboid: UInt) -> Self {
79        Self {
80            fboid,
81            ..Default::default()
82        }
83    }
84
85    pub fn is_protected(&self) -> bool {
86        self.protected == gpu::Protected::Yes
87    }
88}
89
90#[derive(Copy, Clone, PartialEq, Eq, Debug)]
91#[repr(C)]
92pub struct SurfaceInfo {
93    pub sample_count: u32,
94    pub level_count: u32,
95    pub protected: gpu::Protected,
96
97    pub target: Enum,
98    pub format: Enum,
99}
100
101native_transmutable!(GrGLSurfaceInfo, SurfaceInfo, surface_info_layout);
102
103impl Default for SurfaceInfo {
104    fn default() -> Self {
105        Self {
106            sample_count: 1,
107            level_count: 0,
108            protected: gpu::Protected::No,
109            target: 0,
110            format: 0,
111        }
112    }
113}
114
115bitflags! {
116    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
117    pub struct BackendState: u32 {
118        const RENDER_TARGET = sb::GrGLBackendState_kRenderTarget_GrGLBackendState as _;
119        const TEXTURE_BINDING = sb::GrGLBackendState_kTextureBinding_GrGLBackendState as _;
120        const VIEW = sb::GrGLBackendState_kView_GrGLBackendState as _;
121        const BLEND = sb::GrGLBackendState_kBlend_GrGLBackendState as _;
122        const MSAA_ENABLE = sb::GrGLBackendState_kMSAAEnable_GrGLBackendState as _;
123        const VERTEX = sb::GrGLBackendState_kVertex_GrGLBackendState as _;
124        const STENCIL = sb::GrGLBackendState_kStencil_GrGLBackendState as _;
125        const PIXEL_STORE = sb::GrGLBackendState_kPixelStore_GrGLBackendState as _;
126        const PROGRAM = sb::GrGLBackendState_kProgram_GrGLBackendState as _;
127        const FIXED_FUNCTION = sb::GrGLBackendState_kFixedFunction_GrGLBackendState as _;
128        const MISC = sb::GrGLBackendState_kMisc_GrGLBackendState as _;
129    }
130}
131
132// TODO: BackendState::ALL
133
134#[cfg(test)]
135mod tests {
136    use super::{Enum, Format};
137
138    #[test]
139    fn test_support_from_format_to_enum_and_back() {
140        let e: Enum = Format::ALPHA8.into();
141        let f: Format = e.into();
142        assert_eq!(f, Format::ALPHA8);
143    }
144
145    #[test]
146    fn test_all_formats_exhaustive() {
147        use Format::*;
148        let x = ALPHA8;
149        // !!!!!
150        // IF this match is not exhaustive anymore, the implementations of the format conversions
151        // need to be updated in `skia-bindings/src/gl.cpp`, too.
152        match x {
153            Unknown => {}
154            RGBA8 => {}
155            R8 => {}
156            ALPHA8 => {}
157            LUMINANCE8 => {}
158            LUMINANCE8_ALPHA8 => {}
159            BGRA8 => {}
160            RGB565 => {}
161            RGBA16F => {}
162            R16F => {}
163            RGB8 => {}
164            RGBX8 => {}
165            RG8 => {}
166            RGB10_A2 => {}
167            RGBA4 => {}
168            SRGB8_ALPHA8 => {}
169            COMPRESSED_ETC1_RGB8 => {}
170            COMPRESSED_RGB8_ETC2 => {}
171            COMPRESSED_RGB8_BC1 => {}
172            COMPRESSED_RGBA8_BC1 => {}
173            R16 => {}
174            RG16 => {}
175            RGBA16 => {}
176            RG16F => {}
177            LUMINANCE16F => {}
178            STENCIL_INDEX8 => {}
179            STENCIL_INDEX16 => {}
180            DEPTH24_STENCIL8 => {}
181        }
182    }
183
184    #[test]
185    fn test_format_last_color_and_last_exists() {
186        let _ = Format::Last;
187        let _ = Format::LastColorFormat;
188    }
189}