1use std::hash::{Hash, Hasher};
2
3use crate::{
4 Brush, Color, ColorFilter, CornerRadii, DrawPrimitive, FxHasher, ImageBitmap, LayerShape,
5 Point, Rect, RenderEffect, RuntimeShader, ShadowPrimitive, Stroke,
6};
7
8pub trait RenderHash {
9 fn render_hash(&self) -> u64;
10}
11
12impl RenderHash for Color {
13 fn render_hash(&self) -> u64 {
14 finish_hash(|state| hash_color(*self, state))
15 }
16}
17
18impl RenderHash for Point {
19 fn render_hash(&self) -> u64 {
20 finish_hash(|state| hash_point(*self, state))
21 }
22}
23
24impl RenderHash for Rect {
25 fn render_hash(&self) -> u64 {
26 finish_hash(|state| hash_rect(*self, state))
27 }
28}
29
30impl RenderHash for CornerRadii {
31 fn render_hash(&self) -> u64 {
32 finish_hash(|state| hash_corner_radii(*self, state))
33 }
34}
35
36impl RenderHash for LayerShape {
37 fn render_hash(&self) -> u64 {
38 finish_hash(|state| hash_layer_shape(*self, state))
39 }
40}
41
42impl RenderHash for Stroke {
43 fn render_hash(&self) -> u64 {
44 finish_hash(|state| hash_stroke(*self, state))
45 }
46}
47
48impl RenderHash for Brush {
49 fn render_hash(&self) -> u64 {
50 finish_hash(|state| hash_brush(self, state))
51 }
52}
53
54impl RenderHash for ColorFilter {
55 fn render_hash(&self) -> u64 {
56 finish_hash(|state| hash_color_filter(*self, state))
57 }
58}
59
60impl RenderHash for ImageBitmap {
61 fn render_hash(&self) -> u64 {
62 finish_hash(|state| self.id().hash(state))
63 }
64}
65
66impl RenderHash for RuntimeShader {
67 fn render_hash(&self) -> u64 {
68 finish_hash(|state| hash_runtime_shader(self, state))
69 }
70}
71
72impl RenderHash for RenderEffect {
73 fn render_hash(&self) -> u64 {
74 finish_hash(|state| hash_render_effect(self, state))
75 }
76}
77
78impl RenderHash for DrawPrimitive {
79 fn render_hash(&self) -> u64 {
80 finish_hash(|state| hash_draw_primitive(self, state))
81 }
82}
83
84impl RenderHash for ShadowPrimitive {
85 fn render_hash(&self) -> u64 {
86 finish_hash(|state| hash_shadow_primitive(self, state))
87 }
88}
89
90fn finish_hash(write: impl FnOnce(&mut FxHasher)) -> u64 {
91 let mut hasher = FxHasher::default();
92 write(&mut hasher);
93 hasher.finish()
94}
95
96fn hash_f32_bits<H: Hasher>(value: f32, state: &mut H) {
97 value.to_bits().hash(state);
98}
99
100fn hash_color<H: Hasher>(color: Color, state: &mut H) {
101 hash_f32_bits(color.0, state);
102 hash_f32_bits(color.1, state);
103 hash_f32_bits(color.2, state);
104 hash_f32_bits(color.3, state);
105}
106
107fn hash_point<H: Hasher>(point: Point, state: &mut H) {
108 hash_f32_bits(point.x, state);
109 hash_f32_bits(point.y, state);
110}
111
112fn hash_rect<H: Hasher>(rect: Rect, state: &mut H) {
113 hash_f32_bits(rect.x, state);
114 hash_f32_bits(rect.y, state);
115 hash_f32_bits(rect.width, state);
116 hash_f32_bits(rect.height, state);
117}
118
119fn hash_corner_radii<H: Hasher>(radii: CornerRadii, state: &mut H) {
120 hash_f32_bits(radii.top_left, state);
121 hash_f32_bits(radii.top_right, state);
122 hash_f32_bits(radii.bottom_right, state);
123 hash_f32_bits(radii.bottom_left, state);
124}
125
126fn hash_stroke<H: Hasher>(stroke: Stroke, state: &mut H) {
130 hash_f32_bits(stroke.width, state);
131 stroke.cap.hash(state);
132 stroke.join.hash(state);
133}
134
135fn hash_optional_stroke<H: Hasher>(stroke: Option<Stroke>, state: &mut H) {
136 match stroke {
137 Some(stroke) => {
138 1u8.hash(state);
139 hash_stroke(stroke, state);
140 }
141 None => 0u8.hash(state),
142 }
143}
144
145fn hash_layer_shape<H: Hasher>(shape: LayerShape, state: &mut H) {
146 match shape {
147 LayerShape::Rectangle => 0u8.hash(state),
148 LayerShape::Rounded(shape) => {
149 1u8.hash(state);
150 hash_corner_radii(shape.radii(), state);
151 }
152 }
153}
154
155fn hash_brush<H: Hasher>(brush: &Brush, state: &mut H) {
156 match brush {
157 Brush::Solid(color) => {
158 0u8.hash(state);
159 hash_color(*color, state);
160 }
161 Brush::LinearGradient {
162 colors,
163 stops,
164 start,
165 end,
166 tile_mode,
167 } => {
168 1u8.hash(state);
169 hash_color_slice(colors, state);
170 hash_optional_stop_list(stops.as_deref(), state);
171 hash_point(*start, state);
172 hash_point(*end, state);
173 tile_mode.hash(state);
174 }
175 Brush::RadialGradient {
176 colors,
177 stops,
178 center,
179 radius,
180 tile_mode,
181 } => {
182 2u8.hash(state);
183 hash_color_slice(colors, state);
184 hash_optional_stop_list(stops.as_deref(), state);
185 hash_point(*center, state);
186 hash_f32_bits(*radius, state);
187 tile_mode.hash(state);
188 }
189 Brush::SweepGradient {
190 colors,
191 stops,
192 center,
193 } => {
194 3u8.hash(state);
195 hash_color_slice(colors, state);
196 hash_optional_stop_list(stops.as_deref(), state);
197 hash_point(*center, state);
198 }
199 }
200}
201
202fn hash_color_slice<H: Hasher>(colors: &[Color], state: &mut H) {
203 colors.len().hash(state);
204 for color in colors {
205 hash_color(*color, state);
206 }
207}
208
209fn hash_optional_stop_list<H: Hasher>(stops: Option<&[f32]>, state: &mut H) {
210 match stops {
211 Some(stops) => {
212 1u8.hash(state);
213 stops.len().hash(state);
214 for stop in stops {
215 hash_f32_bits(*stop, state);
216 }
217 }
218 None => 0u8.hash(state),
219 }
220}
221
222fn hash_color_filter<H: Hasher>(filter: ColorFilter, state: &mut H) {
223 match filter {
224 ColorFilter::Tint(color) => {
225 0u8.hash(state);
226 hash_color(color, state);
227 }
228 ColorFilter::Modulate(color) => {
229 1u8.hash(state);
230 hash_color(color, state);
231 }
232 ColorFilter::Matrix(matrix) => {
233 2u8.hash(state);
234 for value in matrix {
235 hash_f32_bits(value, state);
236 }
237 }
238 }
239}
240
241fn hash_runtime_shader<H: Hasher>(shader: &RuntimeShader, state: &mut H) {
242 shader.source_hash().hash(state);
243 shader.overrides_hash().hash(state);
244 hash_f32_bits(shader.input_padding(), state);
245 hash_f32_bits(shader.output_padding(), state);
246 for rect in [shader.output_support(), shader.sample_domain()] {
247 match rect {
248 Some(rect) => {
249 1u8.hash(state);
250 hash_f32_bits(rect.x, state);
251 hash_f32_bits(rect.y, state);
252 hash_f32_bits(rect.width, state);
253 hash_f32_bits(rect.height, state);
254 }
255 None => 0u8.hash(state),
256 }
257 }
258 shader.hash_substrates(state);
259 shader.uniforms().len().hash(state);
260 for uniform in shader.uniforms() {
261 hash_f32_bits(*uniform, state);
262 }
263}
264
265fn hash_render_effect<H: Hasher>(effect: &RenderEffect, state: &mut H) {
266 match effect {
267 RenderEffect::Blur {
268 radius_x,
269 radius_y,
270 edge_treatment,
271 } => {
272 0u8.hash(state);
273 hash_f32_bits(*radius_x, state);
274 hash_f32_bits(*radius_y, state);
275 edge_treatment.hash(state);
276 }
277 RenderEffect::Offset { offset_x, offset_y } => {
278 1u8.hash(state);
279 hash_f32_bits(*offset_x, state);
280 hash_f32_bits(*offset_y, state);
281 }
282 RenderEffect::Shader { shader } => {
283 2u8.hash(state);
284 hash_runtime_shader(shader, state);
285 }
286 RenderEffect::Chain { first, second } => {
287 3u8.hash(state);
288 hash_render_effect(first, state);
289 hash_render_effect(second, state);
290 }
291 }
292}
293
294fn hash_draw_primitive<H: Hasher>(primitive: &DrawPrimitive, state: &mut H) {
295 match primitive {
296 DrawPrimitive::Content => {
297 0u8.hash(state);
298 }
299 DrawPrimitive::Blend {
300 primitive,
301 blend_mode,
302 } => {
303 1u8.hash(state);
304 blend_mode.hash(state);
305 hash_draw_primitive(primitive, state);
306 }
307 DrawPrimitive::Rect {
308 rect,
309 brush,
310 stroke,
311 } => {
312 2u8.hash(state);
313 hash_rect(*rect, state);
314 hash_brush(brush, state);
315 hash_optional_stroke(*stroke, state);
316 }
317 DrawPrimitive::RoundRect {
318 rect,
319 brush,
320 radii,
321 stroke,
322 } => {
323 3u8.hash(state);
324 hash_rect(*rect, state);
325 hash_brush(brush, state);
326 hash_corner_radii(*radii, state);
327 hash_optional_stroke(*stroke, state);
328 }
329 DrawPrimitive::Arc {
330 rect,
331 brush,
332 center,
333 radius,
334 start_angle,
335 sweep_angle,
336 stroke,
337 inner_radius,
338 } => {
339 6u8.hash(state);
340 hash_rect(*rect, state);
341 hash_brush(brush, state);
342 hash_point(*center, state);
343 hash_f32_bits(*radius, state);
344 hash_f32_bits(*start_angle, state);
345 hash_f32_bits(*sweep_angle, state);
346 hash_optional_stroke(*stroke, state);
347 hash_f32_bits(*inner_radius, state);
348 }
349 DrawPrimitive::Image {
350 rect,
351 image,
352 alpha,
353 color_filter,
354 sampling,
355 src_rect,
356 } => {
357 4u8.hash(state);
358 hash_rect(*rect, state);
359 image.id().hash(state);
360 hash_f32_bits(*alpha, state);
361 sampling.hash(state);
362 match color_filter {
363 Some(filter) => {
364 1u8.hash(state);
365 hash_color_filter(*filter, state);
366 }
367 None => 0u8.hash(state),
368 }
369 match src_rect {
370 Some(rect) => {
371 1u8.hash(state);
372 hash_rect(*rect, state);
373 }
374 None => 0u8.hash(state),
375 }
376 }
377 DrawPrimitive::Text(text) => {
378 7u8.hash(state);
379 hash_rect(text.rect, state);
380 text.text.hash(state);
381 hash_text_style(&text.style, state);
382 hash_color(text.color, state);
383 }
384 DrawPrimitive::Shadow(shadow) => {
385 5u8.hash(state);
386 hash_shadow_primitive(shadow, state);
387 }
388 }
389}
390
391fn hash_text_style<H: Hasher>(style: &crate::DrawTextStyle, state: &mut H) {
392 style.font_family.hash(state);
393 hash_f32_bits(style.font_size, state);
394 style.font_weight.hash(state);
395 style.font_style.hash(state);
396 hash_f32_bits(style.letter_spacing, state);
397 match style.line_height {
398 Some(line_height) => {
399 1u8.hash(state);
400 hash_f32_bits(line_height, state);
401 }
402 None => 0u8.hash(state),
403 }
404 style.align.hash(state);
405 style.vertical_align.hash(state);
406}
407
408fn hash_shadow_primitive<H: Hasher>(shadow: &ShadowPrimitive, state: &mut H) {
409 match shadow {
410 ShadowPrimitive::Drop {
411 shape,
412 cutout,
413 blur_radius,
414 blend_mode,
415 } => {
416 0u8.hash(state);
417 hash_draw_primitive(shape, state);
418 match cutout {
419 Some(cutout) => {
420 1u8.hash(state);
421 hash_draw_primitive(cutout, state);
422 }
423 None => 0u8.hash(state),
424 }
425 hash_f32_bits(*blur_radius, state);
426 blend_mode.hash(state);
427 }
428 ShadowPrimitive::Inner {
429 fill,
430 cutout,
431 blur_radius,
432 blend_mode,
433 clip_rect,
434 } => {
435 1u8.hash(state);
436 hash_draw_primitive(fill, state);
437 hash_draw_primitive(cutout, state);
438 hash_f32_bits(*blur_radius, state);
439 blend_mode.hash(state);
440 hash_rect(*clip_rect, state);
441 }
442 }
443}
444
445#[cfg(test)]
446#[path = "tests/render_hash_tests.rs"]
447mod tests;