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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use crate::prelude::*;
use crate::{ColorSpace, IPoint, IRect, ISize};
use skia_bindings as sb;
use skia_bindings::{SkAlphaType, SkColorType, SkImageInfo, SkYUVColorSpace};

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum AlphaType {
    Unknown = SkAlphaType::kUnknown_SkAlphaType as _,
    Opaque = SkAlphaType::kOpaque_SkAlphaType as _,
    Premul = SkAlphaType::kPremul_SkAlphaType as _,
    Unpremul = SkAlphaType::kUnpremul_SkAlphaType as _,
}

impl NativeTransmutable<SkAlphaType> for AlphaType {}
#[test]
fn test_alpha_type_layout() {
    AlphaType::test_layout()
}

impl AlphaType {
    pub fn is_opaque(self) -> bool {
        self == AlphaType::Opaque
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum ColorType {
    Unknown = SkColorType::kUnknown_SkColorType as _,
    Alpha8 = SkColorType::kAlpha_8_SkColorType as _,
    RGB565 = SkColorType::kRGB_565_SkColorType as _,
    ARGB4444 = SkColorType::kARGB_4444_SkColorType as _,
    RGBA8888 = SkColorType::kRGBA_8888_SkColorType as _,
    RGB888x = SkColorType::kRGB_888x_SkColorType as _,
    BGRA8888 = SkColorType::kBGRA_8888_SkColorType as _,
    RGBA1010102 = SkColorType::kRGBA_1010102_SkColorType as _,
    RGB101010x = SkColorType::kRGB_101010x_SkColorType as _,
    Gray8 = SkColorType::kGray_8_SkColorType as _,
    RGBAF16Norm = SkColorType::kRGBA_F16Norm_SkColorType as _,
    RGBAF16 = SkColorType::kRGBA_F16_SkColorType as _,
    RGBAF32 = SkColorType::kRGBA_F32_SkColorType as _,
}

impl NativeTransmutable<SkColorType> for ColorType {}
#[test]
fn test_color_type_layout() {
    ColorType::test_layout()
}

impl ColorType {
    // error[E0658]: dereferencing raw pointers in constants is unstable (see issue #51911)
    /*
    pub const N32 : Self = unsafe {
        *((&SkColorType::kN32_SkColorType) as *const _ as *const _)
    };
    */

    pub fn n32() -> Self {
        Self::from_native(SkColorType::kN32_SkColorType)
    }

    pub fn bytes_per_pixel(self) -> usize {
        unsafe {
            sb::SkColorTypeBytesPerPixel(self.into_native())
                .try_into()
                .unwrap()
        }
    }

    pub fn is_always_opaque(self) -> bool {
        unsafe { sb::SkColorTypeIsAlwaysOpaque(self.into_native()) }
    }

    pub fn validate_alpha_type(self, alpha_type: AlphaType) -> Option<AlphaType> {
        let mut alpha_type_r = AlphaType::Unknown;
        unsafe {
            sb::SkColorTypeValidateAlphaType(
                self.into_native(),
                alpha_type.into_native(),
                alpha_type_r.native_mut(),
            )
        }
        .if_true_some(alpha_type_r)
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum YUVColorSpace {
    JPEG = SkYUVColorSpace::kJPEG_SkYUVColorSpace as _,
    Rec601 = SkYUVColorSpace::kRec601_SkYUVColorSpace as _,
    Rec709 = SkYUVColorSpace::kRec709_SkYUVColorSpace as _,
    Identity = SkYUVColorSpace::kIdentity_SkYUVColorSpace as _,
}

impl NativeTransmutable<SkYUVColorSpace> for YUVColorSpace {}
#[test]
fn test_yuv_color_space_layout() {
    YUVColorSpace::test_layout()
}

impl Default for YUVColorSpace {
    fn default() -> Self {
        YUVColorSpace::Identity
    }
}

pub type ImageInfo = Handle<SkImageInfo>;

impl NativeDrop for SkImageInfo {
    fn drop(&mut self) {
        unsafe { sb::C_SkImageInfo_destruct(self) }
    }
}

impl NativeClone for SkImageInfo {
    fn clone(&self) -> Self {
        unsafe {
            construct(|image_info| {
                sb::C_SkImageInfo_Construct(image_info);
                sb::C_SkImageInfo_Copy(self, image_info);
            })
        }
    }
}

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

impl Default for Handle<SkImageInfo> {
    fn default() -> Self {
        Self::construct(|image_info| unsafe { sb::C_SkImageInfo_Construct(image_info) })
    }
}

impl Handle<SkImageInfo> {
    pub fn new(
        dimensions: impl Into<ISize>,
        ct: ColorType,
        at: AlphaType,
        cs: impl Into<Option<ColorSpace>>,
    ) -> Self {
        let dimensions = dimensions.into();
        let mut image_info = Self::default();

        unsafe {
            sb::C_SkImageInfo_Make(
                image_info.native_mut(),
                dimensions.width,
                dimensions.height,
                ct.into_native(),
                at.into_native(),
                cs.into().into_ptr_or_null(),
            )
        }
        image_info
    }

    pub fn new_n32(
        dimensions: impl Into<ISize>,
        at: AlphaType,
        cs: impl Into<Option<ColorSpace>>,
    ) -> ImageInfo {
        Self::new(dimensions, ColorType::n32(), at, cs)
    }

    pub fn new_s32(dimensions: impl Into<ISize>, at: AlphaType) -> ImageInfo {
        let dimensions = dimensions.into();
        let mut image_info = Self::default();
        unsafe {
            sb::C_SkImageInfo_MakeS32(
                image_info.native_mut(),
                dimensions.width,
                dimensions.height,
                at.into_native(),
            );
        }
        image_info
    }

    pub fn new_n32_premul(
        dimensions: impl Into<ISize>,
        cs: impl Into<Option<ColorSpace>>,
    ) -> ImageInfo {
        Self::new(dimensions, ColorType::n32(), AlphaType::Premul, cs)
    }

    pub fn new_a8(dimensions: impl Into<ISize>) -> ImageInfo {
        Self::new(dimensions, ColorType::Alpha8, AlphaType::Premul, None)
    }

    pub fn new_unknown(dimensions: Option<ISize>) -> ImageInfo {
        Self::new(
            dimensions.unwrap_or_default(),
            ColorType::Unknown,
            AlphaType::Unknown,
            None,
        )
    }

    pub fn width(&self) -> i32 {
        self.dimensions().width
    }

    pub fn height(&self) -> i32 {
        self.dimensions().height
    }

    pub fn color_type(&self) -> ColorType {
        ColorType::from_native(self.native().fColorType)
    }

    pub fn alpha_type(&self) -> AlphaType {
        AlphaType::from_native(self.native().fAlphaType)
    }

    pub fn color_space(&self) -> Option<ColorSpace> {
        ColorSpace::from_ptr(unsafe { sb::C_SkImageInfo_colorSpace(self.native()) })
    }

    pub fn is_empty(&self) -> bool {
        self.dimensions().is_empty()
    }

    pub fn is_opaque(&self) -> bool {
        self.alpha_type().is_opaque()
    }

    pub fn dimensions(&self) -> ISize {
        ISize::from_native(self.native().fDimensions)
    }

    pub fn bounds(&self) -> IRect {
        IRect::from_size(self.dimensions())
    }

    pub fn is_gamma_close_to_srgb(&self) -> bool {
        unsafe { sb::C_SkImageInfo_gammaCloseToSRGB(self.native()) }
    }

    pub fn with_dimensions(&self, new_dimensions: impl Into<ISize>) -> Self {
        Self::new(
            new_dimensions,
            self.color_type(),
            self.alpha_type(),
            self.color_space(),
        )
    }

    pub fn with_alpha_type(&self, new_alpha_type: AlphaType) -> Self {
        Self::new(
            self.dimensions(),
            self.color_type(),
            new_alpha_type,
            self.color_space(),
        )
    }

    pub fn with_color_type(&self, new_color_type: ColorType) -> Self {
        Self::new(
            self.dimensions(),
            new_color_type,
            self.alpha_type(),
            self.color_space(),
        )
    }

    pub fn with_color_space(&self, new_color_space: impl Into<Option<ColorSpace>>) -> Self {
        Self::new(
            self.dimensions(),
            self.color_type(),
            self.alpha_type(),
            new_color_space,
        )
    }

    pub fn bytes_per_pixel(&self) -> usize {
        unsafe { self.native().bytesPerPixel().try_into().unwrap() }
    }

    pub fn shift_per_pixel(&self) -> usize {
        unsafe { self.native().shiftPerPixel().try_into().unwrap() }
    }

    pub fn min_row_bytes(&self) -> usize {
        usize::try_from(self.width()).unwrap() * self.bytes_per_pixel()
    }

    pub fn compute_offset(&self, point: impl Into<IPoint>, row_bytes: usize) -> usize {
        let point = point.into();
        unsafe { self.native().computeOffset(point.x, point.y, row_bytes) }
    }

    pub fn compute_byte_size(&self, row_bytes: usize) -> usize {
        unsafe { self.native().computeByteSize(row_bytes) }
    }

    pub fn compute_min_byte_size(&self) -> usize {
        self.compute_byte_size(self.min_row_bytes())
    }

    pub fn valid_row_bytes(&self, row_bytes: usize) -> bool {
        row_bytes >= self.min_row_bytes()
    }

    pub fn reset(&mut self) -> &mut Self {
        unsafe { sb::C_SkImageInfo_reset(self.native_mut()) };
        self
    }
}

#[test]
fn ref_cnt_in_relation_to_color_space() {
    let cs = ColorSpace::new_srgb();
    let before = cs.native().ref_cnt();
    {
        let ii = ImageInfo::new_n32((10, 10), AlphaType::Premul, Some(cs.clone()));
        // one for the capture in image info
        assert_eq!(before + 1, cs.native().ref_cnt());
        let cs2 = ii.color_space();
        // and one for the returned one.
        assert_eq!(before + 2, cs.native().ref_cnt());
        drop(cs2);
    }
    assert_eq!(before, cs.native().ref_cnt())
}