tiny-skia 0.1.0

A stripped down Skia fork that supports only 2D shapes rendering on a CPU.
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
#![doc(html_root_url = "https://docs.rs/tiny-skia/0.1.0")]

use std::ops::{Deref, DerefMut};
use std::slice;

mod ffi {
    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_surface { _unused: [u8; 0] }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_canvas { _unused: [u8; 0] }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_paint { _unused: [u8; 0] }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_path { _unused: [u8; 0] }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_shader { _unused: [u8; 0] }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_path_effect { _unused: [u8; 0] }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_transform {
        pub a: f32,
        pub b: f32,
        pub c: f32,
        pub d: f32,
        pub e: f32,
        pub f: f32,
    }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_point {
        pub x: f32,
        pub y: f32,
    }

    #[repr(C)]
    #[derive(Clone, Copy, Debug)]
    pub struct skiac_surface_data {
        pub ptr: *mut u8,
        pub size: u32,
    }

    extern "C" {

        pub fn skiac_surface_create_rgba_premultiplied(
            width: i32,
            height: i32,
        ) -> *mut skiac_surface;

        pub fn skiac_surface_create_rgba(
            width: i32,
            height: i32,
        ) -> *mut skiac_surface;

        pub fn skiac_surface_destroy(
            surface: *mut skiac_surface,
        );

        pub fn skiac_surface_copy_rgba(
            surface: *mut skiac_surface,
            x: u32, y: u32, width: u32, height: u32,
        ) -> *mut skiac_surface;

        pub fn skiac_surface_get_canvas(
            surface: *mut skiac_surface,
        ) -> *mut skiac_canvas;

        pub fn skiac_surface_get_width(
            surface: *mut skiac_surface,
        ) -> i32;

        pub fn skiac_surface_get_height(
            surface: *mut skiac_surface,
        ) -> i32;

        pub fn skiac_surface_read_pixels(
            surface: *mut skiac_surface,
            data: *mut skiac_surface_data,
        );

        pub fn skiac_surface_get_alpha_type(
            surface: *mut skiac_surface,
        ) -> i32;

        pub fn skiac_canvas_clear(
            canvas: *mut skiac_canvas,
            color: u32,
        );

        pub fn skiac_canvas_flush(
            canvas: *mut skiac_canvas,
        );

        pub fn skiac_canvas_set_transform(
            canvas: *mut skiac_canvas,
            ts: skiac_transform,
        );

        pub fn skiac_canvas_concat(
            canvas: *mut skiac_canvas,
            ts: skiac_transform,
        );

        pub fn skiac_canvas_scale(
            canvas: *mut skiac_canvas,
            sx: f32,
            sy: f32,
        );

        pub fn skiac_canvas_translate(
            canvas: *mut skiac_canvas,
            dx: f32,
            dy: f32,
        );

        pub fn skiac_canvas_get_total_transform(
            canvas: *mut skiac_canvas,
        ) -> skiac_transform;

        pub fn skiac_canvas_draw_path(
            canvas: *mut skiac_canvas,
            path: *mut skiac_path,
            paint: *mut skiac_paint,
        );

        pub fn skiac_canvas_draw_rect(
            canvas: *mut skiac_canvas,
            x: f32, y: f32, w: f32, h: f32,
            paint: *mut skiac_paint,
        );

        pub fn skiac_canvas_draw_surface(
            canvas: *mut skiac_canvas,
            surface: *mut skiac_surface,
            left: f32,
            top: f32,
            alpha: u8,
            blend_mode: i32,
            filter_quality: i32,
        );

        pub fn skiac_canvas_draw_surface_rect(
            canvas: *mut skiac_canvas,
            surface: *mut skiac_surface,
            x: f32, y: f32, w: f32, h: f32,
            filter_quality: i32,
        );

        pub fn skiac_canvas_reset_transform(
            canvas: *mut skiac_canvas,
        );

        pub fn skiac_canvas_clip_rect(
            canvas: *mut skiac_canvas,
            x: f32, y: f32, w: f32, h: f32,
        );

        pub fn skiac_canvas_save(
            canvas: *mut skiac_canvas,
        );

        pub fn skiac_canvas_restore(
            canvas: *mut skiac_canvas,
        );

        pub fn skiac_paint_create() -> *mut skiac_paint;

        pub fn skiac_paint_destroy(
            paint: *mut skiac_paint,
        );

        pub fn skiac_paint_set_style(
            paint: *mut skiac_paint,
            style: i32,
        );

        pub fn skiac_paint_set_color(
            paint: *mut skiac_paint,
            r: u8, g: u8, b: u8, a: u8,
        );

        pub fn skiac_paint_set_alpha(
            paint: *mut skiac_paint,
            a: u8,
        );

        pub fn skiac_paint_set_anti_alias(
            paint: *mut skiac_paint,
            aa: bool,
        );

        pub fn skiac_paint_set_blend_mode(
            paint: *mut skiac_paint,
            blend_mode: i32,
        );

        pub fn skiac_paint_set_shader(
            paint: *mut skiac_paint,
            shader: *mut skiac_shader,
        );

        pub fn skiac_paint_set_stroke_width(
            paint: *mut skiac_paint,
            width: f32,
        );

        pub fn skiac_paint_set_stroke_cap(
            paint: *mut skiac_paint,
            cap: i32,
        );

        pub fn skiac_paint_set_stroke_join(
            paint: *mut skiac_paint,
            join: i32,
        );

        pub fn skiac_paint_set_stroke_miter(
            paint: *mut skiac_paint,
            miter: f32,
        );

        pub fn skiac_paint_set_path_effect(
            paint: *mut skiac_paint,
            path_effect: *mut skiac_path_effect,
        );

        pub fn skiac_path_create() -> *mut skiac_path;

        pub fn skiac_path_destroy(
            path: *mut skiac_path,
        );

        pub fn skiac_path_set_fill_type(
            path: *mut skiac_path,
            kind: i32,
        );

        pub fn skiac_path_move_to(
            path: *mut skiac_path,
            x: f32,
            y: f32,
        );

        pub fn skiac_path_line_to(
            path: *mut skiac_path,
            x: f32,
            y: f32,
        );

        pub fn skiac_path_cubic_to(
            path: *mut skiac_path,
            x1: f32, y1: f32,
            x2: f32, y2: f32,
            x3: f32, y3: f32,
        );

        pub fn skiac_path_close(
            path: *mut skiac_path,
        );

        pub fn skiac_path_effect_make_dash_path(
            intervals: *const f32,
            count: i32,
            phase: f32,
        ) -> *mut skiac_path_effect;

        pub fn skiac_path_effect_destroy(
            path_effect: *mut skiac_path_effect,
        );

        pub fn skiac_shader_make_linear_gradient(
            points: *const skiac_point,
            colors: *const super::Color,
            positions: *const f32,
            count: i32,
            tile_mode: i32,
            flags: u32,
            ts: skiac_transform,
        ) -> *mut skiac_shader;

        pub fn skiac_shader_make_two_point_conical_gradient(
            start_point: skiac_point,
            start_radius: f32,
            end_point: skiac_point,
            end_radius: f32,
            colors: *const super::Color,
            positions: *const f32,
            count: i32,
            tile_mode: i32,
            flags: u32,
            ts: skiac_transform,
        ) -> *mut skiac_shader;

        pub fn skiac_shader_make_from_surface_image(
            surface: *mut skiac_surface,
            ts: skiac_transform,
        ) -> *mut skiac_shader;

        pub fn skiac_shader_destroy(
            shader: *mut skiac_shader,
        );
    }
}


#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PaintStyle {
    Fill = 0,
    Stroke = 1,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FillType {
    Winding = 0,
    EvenOdd = 1,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum StrokeCap {
    Butt = 0,
    Round = 1,
    Square = 2,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum StrokeJoin {
    Miter = 0,
    Round = 1,
    Bevel = 2,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum TileMode {
    Clamp = 0,
    Repeat = 1,
    Mirror = 2,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum BlendMode {
    /// Replaces destination with zero: fully transparent.
    Clear = 0,
    /// Replaces destination.
    Source,
    /// Preserves destination.
    Destination,
    /// Source over destination.
    SourceOver,
    /// Destination over source.
    DestinationOver,
    /// Source trimmed inside destination.
    SourceIn,
    /// Destination trimmed by source.
    DestinationIn,
    /// Source trimmed outside destination.
    SourceOut,
    /// Destination trimmed outside source.
    DestinationOut,
    /// Source inside destination blended with destination.
    SourceATop,
    /// Destination inside source blended with source.
    DestinationATop,
    /// Each of source and destination trimmed outside the other.
    Xor,
    /// Sum of colors.
    Plus,
    /// Product of premultiplied colors; darkens destination.
    Modulate,
    /// Multiply inverse of pixels, inverting result; brightens destination.
    Screen,
    /// Multiply or screen, depending on destination.
    Overlay,
    /// Darker of source and destination.
    Darken,
    /// Lighter of source and destination.
    Lighten,
    /// Brighten destination to reflect source.
    ColorDodge,
    /// Darken destination to reflect source.
    ColorBurn,
    /// Multiply or screen, depending on source.
    HardLight,
    /// Lighten or darken, depending on source.
    SoftLight,
    /// Subtract darker from lighter with higher contrast.
    Difference,
    /// Subtract darker from lighter with lower contrast.
    Exclusion,
    /// Multiply source with destination, darkening image.
    Multiply,
    /// Hue of source with saturation and luminosity of destination.
    Hue,
    /// Saturation of source with hue and luminosity of destination.
    Saturation,
    /// Hue and saturation of source with luminosity of destination.
    Color,
    /// Luminosity of source with hue and saturation of destination.
    Luminosity,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum FilterQuality {
    None = 0,
    Low = 1,
    Medium = 2,
    High = 3,
}

/// Describes how to interpret the alpha component of a pixel.
///
/// A pixel may be opaque, or alpha, describing multiple levels of transparency.
///
/// In simple blending, alpha weights the draw color and the destination
/// color to create a new color. If alpha describes a weight from zero to one:
///
/// new color = draw color * alpha + destination color * (1 - alpha)
///
/// In practice alpha is encoded in two or more bits, where 1.0 equals all bits set.
///
/// RGB may have alpha included in each component value; the stored
/// value is the original RGB multiplied by alpha. Premultiplied color
/// components improve performance.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum AlphaType {
    Unknown,
    Opaque,
    Premultiplied,
    Unpremultiplied,
}

pub struct Surface {
    ptr: *mut ffi::skiac_surface,
    canvas: Canvas,
}

impl Surface {
    // TODO: use AlphaType

    #[inline]
    pub fn new_rgba(width: u32, height: u32) -> Option<Surface> {
        unsafe {
            Self::from_ptr(ffi::skiac_surface_create_rgba(width as i32, height as i32))
        }
    }

    #[inline]
    pub fn new_rgba_premultiplied(width: u32, height: u32) -> Option<Surface> {
        unsafe {
            Self::from_ptr(ffi::skiac_surface_create_rgba_premultiplied(width as i32, height as i32))
        }
    }

    #[inline]
    unsafe fn from_ptr(ptr: *mut ffi::skiac_surface) -> Option<Surface> {
        if ptr.is_null() {
            None
        } else {
            Some(Surface {
                ptr,
                canvas: Canvas(ffi::skiac_surface_get_canvas(ptr))
            })
        }
    }

    #[inline]
    pub fn copy_rgba(&self, x: u32, y: u32, width: u32, height: u32) -> Option<Surface> {
        unsafe { Self::from_ptr(ffi::skiac_surface_copy_rgba(self.ptr, x, y, width, height)) }
    }

    #[inline]
    pub fn try_clone(&self) -> Option<Surface> {
        unsafe { Self::from_ptr(ffi::skiac_surface_copy_rgba(self.ptr, 0, 0, self.width(), self.height())) }
    }

    #[cfg(feature = "png-encoding")]
    pub fn save_png<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), png::EncodingError> {
        let file = std::fs::File::create(path)?;
        let ref mut w = std::io::BufWriter::new(file);

        let mut encoder = png::Encoder::new(w, self.width(), self.height());
        encoder.set_color(png::ColorType::RGBA);
        encoder.set_depth(png::BitDepth::Eight);
        let mut writer = encoder.write_header()?;

        if self.alpha_type() == AlphaType::Premultiplied {
            // TODO: remove allocation
            let mut data = self.data_u8().to_vec();
            demultiply_alpha(&mut data);
            writer.write_image_data(&data)
        } else if self.alpha_type() == AlphaType::Unpremultiplied {
            writer.write_image_data(&self.data())
        } else {
            panic!("unsupported alpha type")
        }
    }

    #[inline]
    pub fn width(&self) -> u32 {
        unsafe { ffi::skiac_surface_get_width(self.ptr) as u32 }
    }

    #[inline]
    pub fn height(&self) -> u32 {
        unsafe { ffi::skiac_surface_get_height(self.ptr) as u32 }
    }

    #[inline]
    pub fn alpha_type(&self) -> AlphaType {
        let kind = unsafe { ffi::skiac_surface_get_alpha_type(self.ptr) };
        match kind {
            0 => AlphaType::Unknown,
            1 => AlphaType::Opaque,
            2 => AlphaType::Premultiplied,
            3 => AlphaType::Unpremultiplied,
            _ => unreachable!(),
        }
    }

    #[inline]
    pub fn data_u8(&self) -> &[u8] {
        unsafe {
            let mut data = ffi::skiac_surface_data {
                ptr: std::ptr::null_mut(),
                size: 0,
            };
            ffi::skiac_surface_read_pixels(self.ptr, &mut data);

            slice::from_raw_parts(data.ptr, data.size as usize)
        }
    }

    #[inline]
    pub fn data(&self) -> SurfaceData {
        SurfaceData {
            slice: self.data_u8(),
        }
    }

    #[inline]
    pub fn data_mut(&mut self) -> SurfaceDataMut {
        unsafe {
            let mut data = ffi::skiac_surface_data {
                ptr: std::ptr::null_mut(),
                size: 0,
            };
            ffi::skiac_surface_read_pixels(self.ptr, &mut data);

            SurfaceDataMut {
                slice: slice::from_raw_parts_mut(data.ptr, data.size as usize),
            }
        }
    }
}

impl std::ops::Deref for Surface {
    type Target = Canvas;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.canvas
    }
}

impl std::ops::DerefMut for Surface {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.canvas
    }
}

impl Drop for Surface {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ffi::skiac_surface_destroy(self.ptr);
        }
    }
}


pub struct SurfaceData<'a> {
    slice: &'a [u8],
}

impl<'a> Deref for SurfaceData<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        self.slice
    }
}


pub struct SurfaceDataMut<'a> {
    slice: &'a mut [u8],
}

impl<'a> Deref for SurfaceDataMut<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        self.slice
    }
}

impl<'a> DerefMut for SurfaceDataMut<'a> {
    #[inline]
    fn deref_mut(&mut self) -> &mut [u8] {
        self.slice
    }
}


#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Color(pub u32);

impl Color {
    #[inline]
    pub fn new(a: u8, r: u8, g: u8, b: u8) -> Color {
        Color((a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | (b as u32))
    }
}


pub struct Canvas(*mut ffi::skiac_canvas);

impl Canvas {
    #[inline]
    pub fn clear(&mut self) {
        unsafe { ffi::skiac_canvas_clear(self.0, 0); }
    }

    #[inline]
    pub fn fill(&mut self, r: u8, g: u8, b: u8, a: u8) {
        unsafe {
            ffi::skiac_canvas_clear(
                self.0,
                (a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | b as u32
            );
        }
    }

    #[inline]
    pub fn flush(&mut self) {
        unsafe { ffi::skiac_canvas_flush(self.0); }
    }

    #[inline]
    pub fn set_transform(&mut self, ts: Transform) {
        unsafe { ffi::skiac_canvas_set_transform(self.0, ts.into()); }
    }

    #[inline]
    pub fn concat(&mut self, ts: Transform) {
        unsafe { ffi::skiac_canvas_concat(self.0, ts.into()); }
    }

    #[inline]
    pub fn scale(&mut self, sx: f32, sy: f32) {
        unsafe { ffi::skiac_canvas_scale(self.0, sx, sy); }
    }

    #[inline]
    pub fn translate(&mut self, dx: f32, dy: f32) {
        unsafe { ffi::skiac_canvas_translate(self.0, dx, dy); }
    }

    #[inline]
    pub fn get_transform(&self) -> Transform {
        unsafe { ffi::skiac_canvas_get_total_transform(self.0).into() }
    }

    #[inline]
    pub fn reset_transform(&mut self) {
        unsafe { ffi::skiac_canvas_reset_transform(self.0); }
    }

    #[inline]
    pub fn draw_path(&mut self, path: &Path, paint: &Paint) {
        unsafe { ffi::skiac_canvas_draw_path(self.0, path.0, paint.0); }
    }

    #[inline]
    pub fn draw_rect(&mut self, x: f32, y: f32, w: f32, h: f32, paint: &Paint) {
        unsafe { ffi::skiac_canvas_draw_rect(self.0, x, y, w, h, paint.0); }
    }

    #[inline]
    pub fn draw_surface(
        &mut self,
        surface: &Surface,
        left: f32,
        top: f32,
        alpha: u8,
        blend_mode: BlendMode,
        filter_quality: FilterQuality,
    ) {
        unsafe {
            ffi::skiac_canvas_draw_surface(
                self.0, surface.ptr, left, top, alpha, blend_mode as i32, filter_quality as i32,
            );
        }
    }

    #[inline]
    pub fn draw_surface_rect(
        &mut self,
        surface: &Surface,
        x: f32, y: f32, w: f32, h: f32,
        filter_quality: FilterQuality,
    ) {
        unsafe {
            ffi::skiac_canvas_draw_surface_rect(
                self.0, surface.ptr, x, y, w, h, filter_quality as i32,
            );
        }
    }

    #[inline]
    pub fn set_clip_rect(&mut self, x: f32, y: f32, w: f32, h: f32) {
        unsafe { ffi::skiac_canvas_clip_rect(self.0, x, y, w, h); }
    }

    #[inline]
    pub fn save(&mut self) {
        unsafe { ffi::skiac_canvas_save(self.0); }
    }

    #[inline]
    pub fn restore(&mut self) {
        unsafe { ffi::skiac_canvas_restore(self.0); }
    }
}

pub struct Paint(*mut ffi::skiac_paint);

impl Paint {
    #[inline]
    pub fn new() -> Paint {
        unsafe { Paint(ffi::skiac_paint_create()) }
    }

    #[inline]
    pub fn set_style(&mut self, style: PaintStyle) {
        unsafe { ffi::skiac_paint_set_style(self.0, style as i32); }
    }

    #[inline]
    pub fn set_color(&mut self, r: u8, g: u8, b: u8, a: u8) {
        unsafe { ffi::skiac_paint_set_color(self.0, r, g, b, a); }
    }

    #[inline]
    pub fn set_alpha(&mut self, a: u8) {
        unsafe { ffi::skiac_paint_set_alpha(self.0, a); }
    }

    #[inline]
    pub fn set_anti_alias(&mut self, aa: bool) {
        unsafe { ffi::skiac_paint_set_anti_alias(self.0, aa); }
    }

    #[inline]
    pub fn set_blend_mode(&mut self, blend_mode: BlendMode) {
        unsafe { ffi::skiac_paint_set_blend_mode(self.0, blend_mode as i32); }
    }

    #[inline]
    pub fn set_shader(&mut self, shader: &Shader) {
        unsafe { ffi::skiac_paint_set_shader(self.0, shader.0); }
    }

    #[inline]
    pub fn set_stroke_width(&mut self, width: f32) {
        unsafe { ffi::skiac_paint_set_stroke_width(self.0, width); }
    }

    #[inline]
    pub fn set_stroke_cap(&mut self, cap: StrokeCap) {
        unsafe { ffi::skiac_paint_set_stroke_cap(self.0, cap as i32); }
    }

    #[inline]
    pub fn set_stroke_join(&mut self, join: StrokeJoin) {
        unsafe { ffi::skiac_paint_set_stroke_join(self.0, join as i32); }
    }

    #[inline]
    pub fn set_stroke_miter(&mut self, miter: f32) {
        unsafe { ffi::skiac_paint_set_stroke_miter(self.0, miter as f32); }
    }

    #[inline]
    pub fn set_path_effect(&mut self, path_effect: &PathEffect) {
        unsafe { ffi::skiac_paint_set_path_effect(self.0, path_effect.0); }
    }
}

impl Drop for Paint {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::skiac_paint_destroy(self.0) }
    }
}


pub struct Path(*mut ffi::skiac_path);

impl Path {
    #[inline]
    pub fn new() -> Path {
        unsafe { Path(ffi::skiac_path_create()) }
    }

    #[inline]
    pub fn set_fill_type(&mut self, kind: FillType) {
        unsafe { ffi::skiac_path_set_fill_type(self.0, kind as i32); }
    }

    #[inline]
    pub fn move_to(&mut self, x: f32, y: f32) {
        unsafe { ffi::skiac_path_move_to(self.0, x, y); }
    }

    #[inline]
    pub fn line_to(&mut self, x: f32, y: f32) {
        unsafe { ffi::skiac_path_line_to(self.0, x, y); }
    }

    #[inline]
    pub fn cubic_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) {
        unsafe { ffi::skiac_path_cubic_to(self.0, x1, y1, x2, y2, x3, y3); }
    }

    #[inline]
    pub fn close(&mut self) {
        unsafe { ffi::skiac_path_close(self.0); }
    }
}

impl Drop for Path {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::skiac_path_destroy(self.0); }
    }
}


pub struct Gradient {
    pub colors: Vec<Color>,
    pub positions: Vec<f32>,
    pub tile_mode: TileMode,
    pub transform: Transform,
}

pub struct LinearGradient {
    pub start_point: (f32, f32),
    pub end_point: (f32, f32),
    pub base: Gradient,
}

pub struct TwoPointConicalGradient {
    pub start: (f32, f32),
    pub start_radius: f32,
    pub end: (f32, f32),
    pub end_radius: f32,
    pub base: Gradient,
}

pub struct Shader(*mut ffi::skiac_shader);

impl Shader {
    #[inline]
    pub fn new_linear_gradient(grad: &LinearGradient) -> Option<Shader> {
        let points = [
            ffi::skiac_point { x: grad.start_point.0, y: grad.start_point.1 },
            ffi::skiac_point { x: grad.end_point.0, y: grad.end_point.1 }
        ];

        unsafe {
            Self::from_ptr(ffi::skiac_shader_make_linear_gradient(
                points.as_ptr(),
                grad.base.colors.as_ptr(),
                grad.base.positions.as_ptr(),
                grad.base.colors.len() as i32,
                grad.base.tile_mode as i32,
                0 as u32,
                grad.base.transform.into(),
            ))
        }
    }

    #[inline]
    pub fn new_two_point_conical_gradient(grad: &TwoPointConicalGradient) -> Option<Shader> {
        let start_point = ffi::skiac_point { x: grad.start.0, y: grad.start.1 };
        let end_point = ffi::skiac_point { x: grad.end.0, y: grad.end.1 };

        unsafe {
            Self::from_ptr(ffi::skiac_shader_make_two_point_conical_gradient(
                start_point, grad.start_radius,
                end_point, grad.end_radius,
                grad.base.colors.as_ptr(),
                grad.base.positions.as_ptr(),
                grad.base.colors.len() as i32,
                grad.base.tile_mode as i32,
                0 as u32,
                grad.base.transform.into(),
            ))
        }
    }

    #[inline]
    pub fn new_from_surface_image(surface: &Surface, ts: Transform) -> Option<Shader> {
        unsafe {
            Self::from_ptr(ffi::skiac_shader_make_from_surface_image(surface.ptr, ts.into()))
        }
    }

    #[inline]
    unsafe fn from_ptr(ptr: *mut ffi::skiac_shader) -> Option<Shader> {
        if ptr.is_null() {
            None
        } else {
            Some(Shader(ptr))
        }
    }
}

impl Drop for Shader {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::skiac_shader_destroy(self.0); }
    }
}


pub struct PathEffect(*mut ffi::skiac_path_effect);

impl PathEffect {
    #[inline]
    pub fn new_dash_path(intervals: &[f32], phase: f32) -> Option<PathEffect> {
        unsafe {
            let ptr = ffi::skiac_path_effect_make_dash_path(
                intervals.as_ptr(),
                intervals.len() as i32,
                phase,
            );

            if ptr.is_null() {
                None
            } else {
                Some(PathEffect(ptr))
            }
        }
    }
}

impl Drop for PathEffect {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::skiac_path_effect_destroy(self.0); }
    }
}


#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Transform {
    pub a: f32,
    pub b: f32,
    pub c: f32,
    pub d: f32,
    pub e: f32,
    pub f: f32,
}

impl Transform {
    pub fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
        Transform { a, b, c, d, e, f }
    }
}

impl Default for Transform {
    fn default() -> Self {
        Transform::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
    }
}

impl From<ffi::skiac_transform> for Transform {
    #[inline]
    fn from(ts: ffi::skiac_transform) -> Self {
        Transform::new(ts.a, ts.b, ts.c, ts.d, ts.e, ts.f)
    }
}

impl From<Transform> for ffi::skiac_transform {
    #[inline]
    fn from(ts: Transform) -> Self {
        ffi::skiac_transform {
            a: ts.a,
            b: ts.b,
            c: ts.c,
            d: ts.d,
            e: ts.e,
            f: ts.f,
        }
    }
}


fn demultiply_alpha(data: &mut [u8]) {
    struct RGBA { r: u8, g: u8, b: u8, a: u8 }

    let data: &mut [RGBA] = unsafe {
        slice::from_raw_parts_mut(data.as_ptr() as *mut _, data.len() / 4)
    };

    for p in data {
        let a = p.a as f64 / 255.0;
        p.b = (p.b as f64 / a + 0.5) as u8;
        p.g = (p.g as f64 / a + 0.5) as u8;
        p.r = (p.r as f64 / a + 0.5) as u8;
    }
}