1
1
use std:: os:: raw:: c_float;
2
- use std:: sync:: Arc ;
3
2
4
3
use skia_safe:: { BlendMode , Contains , Paint , Rect } ;
5
4
6
- use crate :: context:: { Context , Recorder } ;
5
+ use crate :: context:: Context ;
7
6
8
7
impl Context {
9
8
pub fn clear_rect ( & mut self , x : c_float , y : c_float , width : c_float , height : c_float ) {
@@ -16,36 +15,24 @@ impl Context {
16
15
{
17
16
// if rect fully encloses canvas, erase existing content (but preserve CTM, path, etc.)
18
17
true => {
19
- let mut recorder = skia_safe:: PictureRecorder :: new ( ) ;
20
- let canvas = recorder. begin_recording ( self . surface_data . bounds , None ) ;
21
- canvas. set_matrix ( & self . state . matrix . into ( ) ) ;
22
-
23
- if let Some ( clip) = self . state . clip . as_ref ( ) {
24
- canvas. clip_path ( clip. path ( ) , skia_safe:: ClipOp :: Intersect , Some ( true ) ) ;
25
- }
26
-
27
- self . recorder = Arc :: new ( parking_lot:: Mutex :: new ( Recorder {
28
- bounds : self . surface_data . bounds ,
29
- current : recorder,
30
- is_dirty : true ,
31
- layers : vec ! [ ] ,
32
- cache : None ,
33
- matrix : skia_safe:: Matrix :: new_identity ( ) ,
34
- clip : None ,
35
- } ) ) ;
36
-
18
+ self . with_recorder ( |mut recorder|{
19
+ recorder. set_bounds ( self . surface_data . bounds ) ;
20
+ recorder. set_matrix ( self . state . matrix ) ;
21
+ recorder. set_clip ( & self . state . clip ) ;
22
+ } ) ;
37
23
}
38
24
39
25
// otherwise, paint over the specified region but preserve overdrawn vectors
40
26
false => {
41
- self . with_canvas_dirty ( |canvas| {
42
- let mut paint = Paint :: default ( ) ;
43
- paint
44
- . set_anti_alias ( true )
45
- . set_style ( skia_safe:: PaintStyle :: Fill )
46
- . set_blend_mode ( BlendMode :: Clear ) ;
27
+ let mut paint = Paint :: default ( ) ;
28
+ paint
29
+ . set_anti_alias ( true )
30
+ . set_style ( skia_safe:: PaintStyle :: Fill )
31
+ . set_blend_mode ( BlendMode :: Clear ) ;
32
+
33
+ self . render_to_canvas ( & paint, |canvas, paint| {
47
34
let rect = Rect :: from_xywh ( x, y, width, height) ;
48
- canvas. draw_rect ( & rect, & paint) ;
35
+ canvas. draw_rect ( & rect, paint) ;
49
36
} ) ;
50
37
}
51
38
}
@@ -57,15 +44,15 @@ impl Context {
57
44
}
58
45
59
46
pub fn fill_rect ( & mut self , rect : & Rect ) {
60
- self . with_canvas_dirty ( |canvas| {
47
+ self . render_to_canvas ( self . state . paint . fill_paint ( ) , |canvas, paint | {
61
48
if let Some ( paint) = self . state . paint . fill_shadow_paint (
62
49
self . state . shadow_offset ,
63
50
self . state . shadow_color ,
64
51
self . state . shadow_blur ,
65
52
) {
66
53
canvas. draw_rect ( rect, & paint) ;
67
54
}
68
- canvas. draw_rect ( rect, self . state . paint . fill_paint ( ) ) ;
55
+ canvas. draw_rect ( rect, paint) ;
69
56
} ) ;
70
57
}
71
58
@@ -75,15 +62,15 @@ impl Context {
75
62
}
76
63
77
64
pub fn stroke_rect ( & mut self , rect : & Rect ) {
78
- self . with_canvas_dirty ( |canvas| {
65
+ self . render_to_canvas ( self . state . paint . stroke_paint ( ) , |canvas, paint | {
79
66
if let Some ( paint) = & mut self . state . paint . stroke_shadow_paint (
80
67
self . state . shadow_offset ,
81
68
self . state . shadow_color ,
82
69
self . state . shadow_blur ,
83
70
) {
84
71
canvas. draw_rect ( rect, & paint) ;
85
72
}
86
- canvas. draw_rect ( rect, self . state . paint . stroke_paint ( ) ) ;
73
+ canvas. draw_rect ( rect, paint) ;
87
74
} ) ;
88
75
}
89
76
}
0 commit comments