Skip to main content

skia_safe/core/
surface_props.rs

1use std::fmt;
2
3use skia_bindings::{self as sb, SkPixelGeometry, SkSurfaceProps};
4
5use crate::{prelude::*, scalar};
6
7// TODO: use the enum rewriter and strip underscores?
8/// Description of how the LCD strips are arranged for each pixel. If this is unknown, or the
9/// pixels are meant to be "portable" and/or transformed before showing (e.g. rotated, scaled)
10/// then use [`PixelGeometry::Unknown`].
11#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
12#[repr(i32)]
13pub enum PixelGeometry {
14    #[default]
15    Unknown = SkPixelGeometry::kUnknown_SkPixelGeometry as _,
16    RGBH = SkPixelGeometry::kRGB_H_SkPixelGeometry as _,
17    BGRH = SkPixelGeometry::kBGR_H_SkPixelGeometry as _,
18    RGBV = SkPixelGeometry::kRGB_V_SkPixelGeometry as _,
19    BGRV = SkPixelGeometry::kBGR_V_SkPixelGeometry as _,
20}
21
22native_transmutable!(SkPixelGeometry, PixelGeometry);
23
24impl PixelGeometry {
25    pub fn is_rgb(self) -> bool {
26        self == PixelGeometry::RGBH || self == PixelGeometry::RGBV
27    }
28
29    pub fn is_bgr(self) -> bool {
30        self == PixelGeometry::BGRH || self == PixelGeometry::BGRV
31    }
32
33    pub fn is_h(self) -> bool {
34        self == PixelGeometry::RGBH || self == PixelGeometry::BGRH
35    }
36
37    pub fn is_v(self) -> bool {
38        self == PixelGeometry::RGBV || self == PixelGeometry::BGRV
39    }
40}
41
42bitflags! {
43    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44    pub struct SurfacePropsFlags: u32 {
45        #[allow(clippy::unnecessary_cast)]
46        const DEFAULT = sb::SkSurfaceProps_Flags_kDefault_Flag as u32;
47        #[allow(clippy::unnecessary_cast)]
48        const USE_DEVICE_INDEPENDENT_FONTS =
49            sb::SkSurfaceProps_Flags_kUseDeviceIndependentFonts_Flag as u32;
50        /// Use internal MSAA to render to non-MSAA GPU surfaces.
51        #[allow(clippy::unnecessary_cast)]
52        const DYNAMIC_MSAA =
53            sb::SkSurfaceProps_Flags_kDynamicMSAA_Flag as u32;
54        /// If set, all rendering will have dithering enabled. Currently this only impacts GPU
55        /// backends.
56        #[allow(clippy::unnecessary_cast)]
57        const ALWAYS_DITHER =
58            sb::SkSurfaceProps_Flags_kAlwaysDither_Flag as u32;
59        /// The surface will preserve transparent draws (instead of skipping them).
60        #[allow(clippy::unnecessary_cast)]
61        const PRESERVES_TRANSPARENT_DRAWS =
62            sb::SkSurfaceProps_Flags_kPreservesTransparentDraws_Flag as u32;
63    }
64}
65
66impl Default for SurfacePropsFlags {
67    fn default() -> Self {
68        SurfacePropsFlags::empty()
69    }
70}
71
72/// Describes properties and constraints of a given surface. The rendering engine can parse these
73/// during drawing, and can sometimes optimize its performance (e.g. disabling an expensive
74/// feature).
75#[derive(Copy, Clone)]
76#[repr(transparent)]
77pub struct SurfaceProps(SkSurfaceProps);
78
79native_transmutable!(SkSurfaceProps, SurfaceProps);
80
81impl PartialEq for SurfaceProps {
82    fn eq(&self, other: &Self) -> bool {
83        unsafe { sb::C_SkSurfaceProps_Equals(self.native(), other.native()) }
84    }
85}
86
87impl Eq for SurfaceProps {}
88
89impl Default for SurfaceProps {
90    fn default() -> Self {
91        SurfaceProps::new(Default::default(), Default::default())
92    }
93}
94
95impl fmt::Debug for SurfaceProps {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        f.debug_struct("SurfaceProps")
98            .field("flags", &self.flags())
99            .field("pixel_geometry", &self.pixel_geometry())
100            .field("text_contrast", &self.text_contrast())
101            .field("text_gamma", &self.text_gamma())
102            .finish()
103    }
104}
105
106impl SurfaceProps {
107    /// - `flags` surface properties flags
108    /// - `pixel_geometry` pixel geometry
109    pub fn new(flags: SurfacePropsFlags, pixel_geometry: PixelGeometry) -> SurfaceProps {
110        Self::from_native_c(unsafe {
111            SkSurfaceProps::new1(flags.bits(), pixel_geometry.into_native())
112        })
113    }
114
115    /// Specified pixel geometry, text contrast, and gamma.
116    ///
117    /// - `flags` surface properties flags
118    /// - `pixel_geometry` pixel geometry
119    /// - `text_contrast` text contrast
120    /// - `text_gamma` text gamma
121    pub fn new_with_text_properties(
122        flags: SurfacePropsFlags,
123        pixel_geometry: PixelGeometry,
124        text_contrast: scalar,
125        text_gamma: scalar,
126    ) -> SurfaceProps {
127        Self::from_native_c(unsafe {
128            SkSurfaceProps::new2(
129                flags.bits(),
130                pixel_geometry.into_native(),
131                text_contrast,
132                text_gamma,
133            )
134        })
135    }
136
137    pub fn flags(self) -> SurfacePropsFlags {
138        SurfacePropsFlags::from_bits_truncate(self.native().fFlags)
139    }
140
141    #[must_use]
142    pub fn clone_with_pixel_geometry(&self, new_pixel_geometry: PixelGeometry) -> Self {
143        Self::new_with_text_properties(
144            self.flags(),
145            new_pixel_geometry,
146            self.text_contrast(),
147            self.text_gamma(),
148        )
149    }
150
151    pub const MAX_CONTRAST_INCLUSIVE: scalar = 1.;
152    pub const MIN_CONTRAST_INCLUSIVE: scalar = 0.;
153    pub const MAX_GAMMA_EXCLUSIVE: scalar = 4.;
154    pub const MIN_GAMMA_INCLUSIVE: scalar = 0.;
155
156    pub fn pixel_geometry(self) -> PixelGeometry {
157        PixelGeometry::from_native_c(self.native().fPixelGeometry)
158    }
159
160    pub fn text_contrast(self) -> scalar {
161        self.native().fTextContrast
162    }
163
164    pub fn text_gamma(self) -> scalar {
165        self.native().fTextGamma
166    }
167
168    pub fn is_use_device_independent_fonts(self) -> bool {
169        self.flags()
170            .contains(SurfacePropsFlags::USE_DEVICE_INDEPENDENT_FONTS)
171    }
172
173    pub fn is_always_dither(self) -> bool {
174        self.flags().contains(SurfacePropsFlags::ALWAYS_DITHER)
175    }
176
177    pub fn preserves_transparent_draws(self) -> bool {
178        self.flags()
179            .contains(SurfacePropsFlags::PRESERVES_TRANSPARENT_DRAWS)
180    }
181}
182
183#[test]
184fn create() {
185    let props = SurfaceProps::new(
186        SurfacePropsFlags::USE_DEVICE_INDEPENDENT_FONTS,
187        PixelGeometry::RGBH,
188    );
189    assert_eq!(
190        SurfacePropsFlags::USE_DEVICE_INDEPENDENT_FONTS,
191        props.flags()
192    );
193    assert_eq!(PixelGeometry::RGBH, props.pixel_geometry());
194    assert!(props.is_use_device_independent_fonts());
195}