@@ -6,136 +6,202 @@ use crate::raster::Image;
6
6
use core:: ops:: Deref ;
7
7
use dyn_any:: DynAny ;
8
8
use glam:: { DAffine2 , DVec2 } ;
9
- # [ cfg ( feature = "wgpu" ) ]
10
- use std:: sync :: Arc ;
9
+ use std :: fmt :: Debug ;
10
+ use std:: ops :: DerefMut ;
11
11
12
- #[ derive( Clone , Debug , Hash , PartialEq , Eq , Copy ) ]
13
- pub struct CPU ;
14
- #[ derive( Clone , Debug , Hash , PartialEq , Eq , Copy ) ]
15
- pub struct GPU ;
12
+ mod __private {
13
+ pub trait Sealed { }
14
+ }
16
15
17
- trait Storage : ' static { }
18
- impl Storage for CPU { }
19
- impl Storage for GPU { }
16
+ pub trait Storage : __private :: Sealed + Clone + Debug + ' static {
17
+ fn is_empty ( & self ) -> bool ;
18
+ }
20
19
21
- #[ derive( Clone , Debug , Hash , PartialEq ) ]
22
- #[ allow( private_bounds) ]
23
- pub struct Raster < T : Storage > {
24
- data : RasterStorage ,
20
+ #[ derive( Clone , Debug , PartialEq , Hash , Default ) ]
21
+ pub struct Raster < T >
22
+ where
23
+ Raster < T > : Storage ,
24
+ {
25
25
storage : T ,
26
26
}
27
27
28
- unsafe impl < T : Storage > dyn_any:: StaticType for Raster < T > {
28
+ unsafe impl < T > dyn_any:: StaticType for Raster < T >
29
+ where
30
+ Raster < T > : Storage ,
31
+ {
29
32
type Static = Raster < T > ;
30
33
}
31
- #[ derive( Clone , Debug , Hash , PartialEq , DynAny ) ]
32
- pub enum RasterStorage {
33
- Cpu ( Image < Color > ) ,
34
- #[ cfg( feature = "wgpu" ) ]
35
- Gpu ( Arc < wgpu:: Texture > ) ,
36
- #[ cfg( not( feature = "wgpu" ) ) ]
37
- Gpu ( ( ) ) ,
38
- }
39
34
40
- impl RasterStorage { }
41
- impl Raster < CPU > {
42
- pub fn new_cpu ( image : Image < Color > ) -> Self {
43
- Self {
44
- data : RasterStorage :: Cpu ( image) ,
45
- storage : CPU ,
46
- }
35
+ impl < T > Raster < T >
36
+ where
37
+ Raster < T > : Storage ,
38
+ {
39
+ pub fn new ( t : T ) -> Self {
40
+ Self { storage : t }
47
41
}
48
- pub fn data ( & self ) -> & Image < Color > {
49
- let RasterStorage :: Cpu ( cpu) = & self . data else { unreachable ! ( ) } ;
50
- cpu
51
- }
52
- pub fn data_mut ( & mut self ) -> & mut Image < Color > {
53
- let RasterStorage :: Cpu ( cpu) = & mut self . data else { unreachable ! ( ) } ;
54
- cpu
55
- }
56
- pub fn into_data ( self ) -> Image < Color > {
57
- let RasterStorage :: Cpu ( cpu) = self . data else { unreachable ! ( ) } ;
58
- cpu
42
+ }
43
+
44
+ impl < T > Deref for Raster < T >
45
+ where
46
+ Raster < T > : Storage ,
47
+ {
48
+ type Target = T ;
49
+
50
+ fn deref ( & self ) -> & Self :: Target {
51
+ & self . storage
59
52
}
60
- pub fn is_empty ( & self ) -> bool {
61
- let data = self . data ( ) ;
62
- data. height == 0 || data. width == 0
53
+ }
54
+
55
+ impl < T > DerefMut for Raster < T >
56
+ where
57
+ Raster < T > : Storage ,
58
+ {
59
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
60
+ & mut self . storage
63
61
}
64
62
}
65
- impl Default for Raster < CPU > {
66
- fn default ( ) -> Self {
67
- Self {
68
- data : RasterStorage :: Cpu ( Image :: default ( ) ) ,
69
- storage : CPU ,
63
+
64
+ pub type RasterDataTable < Storage > = Instances < Raster < Storage > > ;
65
+
66
+ pub use cpu:: CPU ;
67
+
68
+ mod cpu {
69
+ use super :: * ;
70
+ use crate :: raster_types:: __private:: Sealed ;
71
+
72
+ #[ derive( Clone , Debug , Default , PartialEq , Hash , DynAny ) ]
73
+ pub struct CPU ( Image < Color > ) ;
74
+
75
+ impl Sealed for Raster < CPU > { }
76
+
77
+ impl Storage for Raster < CPU > {
78
+ fn is_empty ( & self ) -> bool {
79
+ self . 0 . height == 0 || self . 0 . width == 0
70
80
}
71
81
}
72
- }
73
- impl Deref for Raster < CPU > {
74
- type Target = Image < Color > ;
75
82
76
- fn deref ( & self ) -> & Self :: Target {
77
- self . data ( )
83
+ impl Raster < CPU > {
84
+ pub fn new_cpu ( image : Image < Color > ) -> Self {
85
+ Self :: new ( CPU ( image) )
86
+ }
87
+
88
+ pub fn data ( & self ) -> & Image < Color > {
89
+ self
90
+ }
91
+
92
+ pub fn data_mut ( & mut self ) -> & mut Image < Color > {
93
+ self
94
+ }
95
+
96
+ pub fn into_data ( self ) -> Image < Color > {
97
+ self . storage . 0
98
+ }
78
99
}
79
- }
80
- #[ cfg( feature = "wgpu" ) ]
81
- impl Raster < GPU > {
82
- pub fn new_gpu ( image : Arc < wgpu:: Texture > ) -> Self {
83
- Self {
84
- data : RasterStorage :: Gpu ( image) ,
85
- storage : GPU ,
100
+
101
+ impl Deref for CPU {
102
+ type Target = Image < Color > ;
103
+
104
+ fn deref ( & self ) -> & Self :: Target {
105
+ & self . 0
86
106
}
87
107
}
88
- pub fn data ( & self ) -> & wgpu:: Texture {
89
- let RasterStorage :: Gpu ( gpu) = & self . data else { unreachable ! ( ) } ;
90
- gpu
108
+
109
+ impl DerefMut for CPU {
110
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
111
+ & mut self . 0
112
+ }
91
113
}
92
- pub fn data_mut ( & mut self ) -> & mut Arc < wgpu:: Texture > {
93
- let RasterStorage :: Gpu ( gpu) = & mut self . data else { unreachable ! ( ) } ;
94
- gpu
114
+
115
+ impl < ' de > serde:: Deserialize < ' de > for Raster < CPU > {
116
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
117
+ where
118
+ D : serde:: Deserializer < ' de > ,
119
+ {
120
+ Ok ( Raster :: new_cpu ( Image :: deserialize ( deserializer) ?) )
121
+ }
95
122
}
96
- pub fn data_owned ( & self ) -> Arc < wgpu:: Texture > {
97
- let RasterStorage :: Gpu ( gpu) = & self . data else { unreachable ! ( ) } ;
98
- gpu. clone ( )
123
+
124
+ impl serde:: Serialize for Raster < CPU > {
125
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
126
+ where
127
+ S : serde:: Serializer ,
128
+ {
129
+ self . 0 . serialize ( serializer)
130
+ }
99
131
}
100
132
}
101
133
102
- impl Raster < GPU > {
103
- #[ cfg( feature = "wgpu" ) ]
104
- pub fn is_empty ( & self ) -> bool {
105
- let data = self . data ( ) ;
106
- data. width ( ) == 0 || data. height ( ) == 0
134
+ pub use gpu:: GPU ;
135
+
136
+ #[ cfg( feature = "wgpu" ) ]
137
+ mod gpu {
138
+ use super :: * ;
139
+ use crate :: raster_types:: __private:: Sealed ;
140
+
141
+ #[ derive( Clone , Debug , PartialEq , Hash ) ]
142
+ pub struct GPU {
143
+ texture : wgpu:: Texture ,
107
144
}
108
- #[ cfg( not( feature = "wgpu" ) ) ]
109
- pub fn is_empty ( & self ) -> bool {
110
- true
145
+
146
+ impl Sealed for Raster < GPU > { }
147
+
148
+ impl Storage for Raster < GPU > {
149
+ fn is_empty ( & self ) -> bool {
150
+ self . texture . width ( ) == 0 || self . texture . height ( ) == 0
151
+ }
152
+ }
153
+
154
+ impl Raster < GPU > {
155
+ pub fn new_gpu ( texture : wgpu:: Texture ) -> Self {
156
+ Self :: new ( GPU { texture } )
157
+ }
158
+
159
+ pub fn data ( & self ) -> & wgpu:: Texture {
160
+ & self . texture
161
+ }
111
162
}
112
163
}
113
164
114
- #[ cfg( feature = "wgpu" ) ]
115
- impl Deref for Raster < GPU > {
116
- type Target = wgpu :: Texture ;
165
+ #[ cfg( not ( feature = "wgpu" ) ) ]
166
+ mod gpu {
167
+ use super :: * ;
117
168
118
- fn deref ( & self ) -> & Self :: Target {
119
- self . data ( )
169
+ #[ derive( Clone , Debug ) ]
170
+ pub struct GPU ;
171
+
172
+ impl Storage for Raster < GPU > {
173
+ fn is_empty ( & self ) -> bool {
174
+ true
175
+ }
120
176
}
121
177
}
122
178
123
- pub type RasterDataTable < Storage > = Instances < Raster < Storage > > ;
179
+ mod gpu_common {
180
+ use super :: * ;
124
181
125
- // TODO: Make this not dupliated
126
- impl BoundingBox for RasterDataTable < CPU > {
127
- fn bounding_box ( & self , transform : DAffine2 , _include_stroke : bool ) -> Option < [ DVec2 ; 2 ] > {
128
- self . instance_ref_iter ( )
129
- . filter ( |instance| !instance. instance . is_empty ( ) ) // Eliminate empty images
130
- . flat_map ( |instance| {
131
- let transform = transform * * instance. transform ;
132
- ( transform. matrix2 . determinant ( ) != 0. ) . then ( || ( transform * Quad :: from_box ( [ DVec2 :: ZERO , DVec2 :: ONE ] ) ) . bounding_box ( ) )
133
- } )
134
- . reduce ( Quad :: combine_bounds)
182
+ impl < ' de > serde:: Deserialize < ' de > for Raster < GPU > {
183
+ fn deserialize < D > ( _deserializer : D ) -> Result < Self , D :: Error >
184
+ where
185
+ D : serde:: Deserializer < ' de > ,
186
+ {
187
+ unimplemented ! ( )
188
+ }
189
+ }
190
+
191
+ impl serde:: Serialize for Raster < GPU > {
192
+ fn serialize < S > ( & self , _serializer : S ) -> Result < S :: Ok , S :: Error >
193
+ where
194
+ S : serde:: Serializer ,
195
+ {
196
+ unimplemented ! ( )
197
+ }
135
198
}
136
199
}
137
200
138
- impl BoundingBox for RasterDataTable < GPU > {
201
+ impl < T > BoundingBox for RasterDataTable < T >
202
+ where
203
+ Raster < T > : Storage ,
204
+ {
139
205
fn bounding_box ( & self , transform : DAffine2 , _include_stroke : bool ) -> Option < [ DVec2 ; 2 ] > {
140
206
self . instance_ref_iter ( )
141
207
. filter ( |instance| !instance. instance . is_empty ( ) ) // Eliminate empty images
0 commit comments