1use super::*;
2
3impl Default for DisplayListFrameArena {
5 fn default() -> Self {
11 Self {
12 string_pool: Vec::new(),
13 portals_scratch: Vec::new(),
14 z_sort_scratch: Vec::new(),
15 portal_ancestors_scratch: Vec::new(),
16 base_layer_version: 0,
17 prev_base_digest: 0,
18 cur_base_digest: FNV1A_OFFSET_BASIS,
19 running_fast_hash: FNV1A_OFFSET_BASIS,
20 base_layer_finalized: false,
21 }
22 }
23}
24
25impl Clone for DisplayListFrameArena {
27 fn clone(&self) -> Self {
37 let Self {
38 base_layer_version,
39 prev_base_digest,
40 cur_base_digest,
41 running_fast_hash,
42 base_layer_finalized,
43 ..
44 } = self;
45 Self {
46 string_pool: Vec::new(),
47 portals_scratch: Vec::new(),
48 z_sort_scratch: Vec::new(),
49 portal_ancestors_scratch: Vec::new(),
50 base_layer_version: *base_layer_version,
51 prev_base_digest: *prev_base_digest,
52 cur_base_digest: *cur_base_digest,
53 running_fast_hash: *running_fast_hash,
54 base_layer_finalized: *base_layer_finalized,
55 }
56 }
57}
58
59impl PartialEq for DisplayListFrameArena {
61 fn eq(&self, _other: &Self) -> bool {
72 true
73 }
74}
75
76impl DisplayList {
78 pub(crate) fn reserve_steady_state(&mut self) {
84 let Self {
85 commands, arena, ..
86 } = self;
87 if commands.capacity() < 512 {
88 commands.reserve(512 - commands.len());
89 }
90 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
91 arena
92 .string_pool
93 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
94 }
95 }
96
97 pub(crate) fn prewarm_scroll_string_pool(&mut self, spare_count: usize) {
104 let Self { arena, .. } = self;
105 let target: usize = arena
106 .string_pool
107 .len()
108 .saturating_add(spare_count)
109 .min(DISPLAY_LIST_STRING_POOL_CAP);
110 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
111 arena
112 .string_pool
113 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
114 }
115 while arena.string_pool.len() < target {
116 arena.string_pool.push(String::with_capacity(128));
117 }
118 }
119
120 pub fn reset(&mut self, viewport: Size, scale_factor: f32) {
128 let safe_scale: f32 = scale_factor.max(0.5);
129 let Self {
130 commands,
131 viewport: view_proj,
132 scale_factor: sf,
133 portal_split_index,
134 gpu_scene_pass,
135 arena,
136 ..
137 } = self;
138 *gpu_scene_pass = None;
139 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
140 arena
141 .string_pool
142 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
143 }
144 let pool_start: usize = arena.string_pool.len();
145 for cmd in commands.drain(..) {
146 match cmd {
147 DrawCommand::TextRun { mut text, .. } | DrawCommand::TextCaret { mut text, .. }
148 if text.capacity() > 0
149 && arena.string_pool.len() < DISPLAY_LIST_STRING_POOL_CAP =>
150 {
151 text.clear();
152 arena.string_pool.push(text);
153 }
154 _ => {}
155 }
156 }
157 if arena.string_pool.len() > pool_start + 1 {
158 arena.string_pool[pool_start..].reverse();
159 }
160 *view_proj = viewport;
161 *sf = safe_scale;
162 *portal_split_index = 0;
163 let mut seed: u64 = FNV1A_OFFSET_BASIS;
164 Self::mix_hash(&mut seed, viewport.get_width().to_bits() as u64);
165 Self::mix_hash(&mut seed, viewport.get_height().to_bits() as u64);
166 Self::mix_hash(&mut seed, safe_scale.to_bits() as u64);
167 arena.prev_base_digest = arena.cur_base_digest;
168 arena.cur_base_digest = seed;
169 arena.running_fast_hash = seed;
170 arena.base_layer_finalized = false;
171 }
172
173 pub fn set_gpu_scene_pass(&mut self, pass: Option<DirectGpuScenePass>) {
179 let Self { gpu_scene_pass, .. } = self;
180 *gpu_scene_pass = pass;
181 }
182
183 #[must_use]
189 pub fn gpu_scene_pass_clone(&self) -> Option<DirectGpuScenePass> {
190 let Self { gpu_scene_pass, .. } = self;
191 gpu_scene_pass.clone()
192 }
193
194 #[inline]
205 #[must_use]
206 pub(crate) fn alloc_text(&mut self, src: &str) -> String {
207 if src.is_empty() {
208 return String::new();
209 }
210 let Self { arena, .. } = self;
211 let pool: &mut Vec<String> = &mut arena.string_pool;
212 let len: usize = pool.len();
213 if len > 0 {
214 let need: usize = src.len();
215 if pool[len - 1].capacity() >= need
216 && let Some(mut reused) = pool.pop()
217 {
218 reused.clear();
219 reused.push_str(src);
220 return reused;
221 }
222 for idx in (0..len.saturating_sub(1)).rev() {
223 if pool[idx].capacity() >= need {
224 let mut reused: String = pool.swap_remove(idx);
225 reused.clear();
226 reused.push_str(src);
227 return reused;
228 }
229 }
230 if let Some(mut reused) = pool.pop() {
231 reused.clear();
232 reused.push_str(src);
233 return reused;
234 }
235 }
236 let mut fresh: String = String::with_capacity(src.len().max(128));
237 fresh.push_str(src);
238 fresh
239 }
240
241 pub fn push(&mut self, command: DrawCommand) {
248 let Self {
249 commands, arena, ..
250 } = self;
251 Self::fold_command_fast(&mut arena.running_fast_hash, &command);
252 if !arena.base_layer_finalized {
253 Self::fold_command_full(&mut arena.cur_base_digest, &command);
254 }
255 commands.push(command);
256 }
257
258 pub fn extend_commands(&mut self, commands_slice: &[DrawCommand]) {
265 for command_item in commands_slice.iter() {
266 self.push(command_item.clone());
267 }
268 }
269
270 #[must_use]
280 pub fn fast_digest(&self) -> u64 {
281 let Self { arena, .. } = self;
282 arena.running_fast_hash
283 }
284
285 #[inline]
295 #[must_use]
296 pub(crate) fn snapshot_digest_state(&self) -> (u64, u64) {
297 let Self { arena, .. } = self;
298 (arena.running_fast_hash, arena.cur_base_digest)
299 }
300
301 #[inline]
308 pub(crate) fn pop_empty_clip(&mut self, saved: (u64, u64)) {
309 let Self {
310 commands, arena, ..
311 } = self;
312 let _: Option<DrawCommand> = commands.pop();
313 arena.running_fast_hash = saved.0;
314 arena.cur_base_digest = saved.1;
315 }
316
317 pub(crate) fn attenuate_range_opacity(&mut self, start: usize, end: usize, opacity: f32) {
326 if opacity >= 0.999 || start >= end {
327 return;
328 }
329 let clamped: f32 = opacity.clamp(0.0, 1.0);
330 let Self {
331 commands, arena, ..
332 } = self;
333 let end_clamped: usize = end.min(commands.len());
334 for cmd in commands[start..end_clamped].iter_mut() {
335 match cmd {
336 DrawCommand::Clear(char_item)
337 | DrawCommand::Line {
338 color: char_item, ..
339 }
340 | DrawCommand::Circle {
341 color: char_item, ..
342 }
343 | DrawCommand::Triangle {
344 color: char_item, ..
345 }
346 | DrawCommand::TextCaret {
347 color: char_item, ..
348 } => {
349 *char_item = char_item.multiply_alpha(clamped);
350 }
351 DrawCommand::FillRoundedRect { color, border, .. } => {
352 *color = color.multiply_alpha(clamped);
353 border.set_color(border.get_color().multiply_alpha(clamped));
354 }
355 DrawCommand::LinearGradientRect { gradient, .. } => {
356 gradient.set_start_color(gradient.get_start_color().multiply_alpha(clamped));
357 gradient.set_end_color(gradient.get_end_color().multiply_alpha(clamped));
358 }
359 DrawCommand::BoxShadow { shadow, .. } => {
360 shadow.set_color(shadow.get_color().multiply_alpha(clamped));
361 }
362 DrawCommand::TextRun { style, .. } => {
363 style.set_color(style.get_color().multiply_alpha(clamped));
364 }
365 DrawCommand::SpriteRegion { tint, .. } => {
366 *tint = tint.multiply_alpha(clamped);
367 }
368 DrawCommand::BackdropBlur { .. }
369 | DrawCommand::Image { .. }
370 | DrawCommand::SharedImage { .. }
371 | DrawCommand::ExternalSurface { .. }
372 | DrawCommand::PushClip(_)
373 | DrawCommand::PushRoundedClip { .. }
374 | DrawCommand::PopClip => {}
375 }
376 }
377 let op_bits: u64 = clamped.to_bits() as u64;
378 Self::mix_hash(&mut arena.running_fast_hash, op_bits);
379 if !arena.base_layer_finalized {
380 Self::mix_hash(&mut arena.cur_base_digest, op_bits);
381 }
382 }
383
384 #[inline]
391 pub(crate) fn finalize_base_layer(&mut self, split_index: usize) {
392 let Self {
393 portal_split_index,
394 arena,
395 ..
396 } = self;
397 *portal_split_index = split_index;
398 Self::mix_hash(&mut arena.cur_base_digest, split_index as u64);
399 if arena.base_layer_version == 0 || arena.cur_base_digest != arena.prev_base_digest {
400 arena.base_layer_version = arena.base_layer_version.wrapping_add(1).max(1);
401 }
402 arena.base_layer_finalized = true;
403 }
404
405 #[inline]
415 #[must_use]
416 pub fn base_layer_version(&self) -> u64 {
417 let Self { arena, .. } = self;
418 arena.base_layer_version
419 }
420
421 #[must_use]
431 pub fn len(&self) -> usize {
432 self.get_commands().len()
433 }
434
435 #[must_use]
445 pub fn is_empty(&self) -> bool {
446 self.get_commands().is_empty()
447 }
448
449 #[inline(always)]
456 fn mix_hash(hash: &mut u64, value: u64) {
457 *hash ^= value;
458 *hash = hash.wrapping_mul(FNV1A_PRIME);
459 }
460
461 #[inline]
468 fn fold_command_fast(hash: &mut u64, cmd: &DrawCommand) {
469 match cmd {
470 DrawCommand::Clear(char_item) => {
471 Self::mix_hash(hash, 1);
472 Self::mix_hash(hash, u32::from_le_bytes(char_item.to_rgba_bytes()) as u64);
473 }
474 DrawCommand::FillRoundedRect {
475 rect,
476 color,
477 border,
478 ..
479 } => {
480 Self::mix_hash(hash, 2);
481 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
482 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
483 Self::mix_hash(hash, rect.get_width().to_bits() as u64);
484 Self::mix_hash(hash, rect.get_height().to_bits() as u64);
485 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
486 Self::mix_hash(hash, border.get_width().to_bits() as u64);
487 }
488 DrawCommand::LinearGradientRect { rect, gradient, .. } => {
489 Self::mix_hash(hash, 3);
490 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
491 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
492 Self::mix_hash(
493 hash,
494 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
495 );
496 }
497 DrawCommand::BoxShadow { rect, shadow, .. } => {
498 Self::mix_hash(hash, 4);
499 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
500 Self::mix_hash(hash, shadow.get_blur_radius().to_bits() as u64);
501 }
502 DrawCommand::BackdropBlur {
503 rect, blur_radius, ..
504 } => {
505 Self::mix_hash(hash, 15);
506 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
507 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
508 Self::mix_hash(hash, blur_radius.to_bits() as u64);
509 }
510 DrawCommand::TextRun { rect, text, style } => {
511 Self::mix_hash(hash, 5);
512 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
513 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
514 Self::mix_hash(hash, text.len() as u64);
515 let bytes: &[u8] = text.as_bytes();
516 let prefix_len: usize = bytes.len().min(16);
517 for &byte in bytes[..prefix_len].iter() {
518 Self::mix_hash(hash, byte as u64);
519 }
520 Self::mix_hash(
521 hash,
522 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
523 );
524 }
525 DrawCommand::TextCaret {
526 text_rect,
527 text,
528 color,
529 ..
530 } => {
531 Self::mix_hash(hash, 13);
532 Self::mix_hash(hash, text_rect.get_x().to_bits() as u64);
533 Self::mix_hash(hash, text_rect.get_y().to_bits() as u64);
534 Self::mix_hash(hash, text.len() as u64);
535 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
536 }
537 DrawCommand::Image { rect, buffer, .. } => {
538 Self::mix_hash(hash, 6);
539 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
540 Self::mix_hash(hash, buffer.get_width() as u64);
541 Self::mix_hash(hash, buffer.get_height() as u64);
542 }
543 DrawCommand::SharedImage {
544 rect,
545 width,
546 height,
547 ..
548 } => {
549 Self::mix_hash(hash, 6);
550 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
551 Self::mix_hash(hash, *width as u64);
552 Self::mix_hash(hash, *height as u64);
553 }
554 DrawCommand::Line {
555 from,
556 to,
557 color,
558 width,
559 } => {
560 Self::mix_hash(hash, 7);
561 Self::mix_hash(hash, from.get_x().to_bits() as u64);
562 Self::mix_hash(hash, to.get_y().to_bits() as u64);
563 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
564 Self::mix_hash(hash, width.to_bits() as u64);
565 }
566 DrawCommand::Circle {
567 center,
568 radius,
569 color,
570 } => {
571 Self::mix_hash(hash, 8);
572 Self::mix_hash(hash, center.get_x().to_bits() as u64);
573 Self::mix_hash(hash, center.get_y().to_bits() as u64);
574 Self::mix_hash(hash, radius.to_bits() as u64);
575 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
576 }
577 DrawCommand::Triangle { p0, p1, p2, color } => {
578 Self::mix_hash(hash, 9);
579 Self::mix_hash(hash, p0.get_x().to_bits() as u64);
580 Self::mix_hash(hash, p1.get_y().to_bits() as u64);
581 Self::mix_hash(hash, p2.get_x().to_bits() as u64);
582 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
583 }
584 DrawCommand::SpriteRegion {
585 rect,
586 src_rect,
587 tint,
588 ..
589 } => {
590 Self::mix_hash(hash, 10);
591 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
592 Self::mix_hash(hash, src_rect.get_x().to_bits() as u64);
593 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
594 }
595 DrawCommand::ExternalSurface { rect, radius } => {
596 Self::mix_hash(hash, 15);
597 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
598 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
599 Self::mix_hash(hash, rect.get_width().to_bits() as u64);
600 Self::mix_hash(hash, rect.get_height().to_bits() as u64);
601 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
602 }
603 DrawCommand::PushClip(red_val) => {
604 Self::mix_hash(hash, 11);
605 Self::mix_hash(hash, red_val.get_x().to_bits() as u64);
606 Self::mix_hash(hash, red_val.get_y().to_bits() as u64);
607 }
608 DrawCommand::PushRoundedClip { rect, radius } => {
609 Self::mix_hash(hash, 14);
610 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
611 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
612 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
613 }
614 DrawCommand::PopClip => {
615 Self::mix_hash(hash, 12);
616 }
617 }
618 }
619
620 #[inline]
627 fn fold_bytes_wordwise(hash: &mut u64, bytes: &[u8]) {
628 Self::mix_hash(hash, bytes.len() as u64);
629 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
630 for chunk in chunks.iter() {
631 Self::mix_hash(hash, u64::from_ne_bytes(*chunk));
632 }
633 if !rem.is_empty() {
634 let mut tail: u64 = 0;
635 for (idx, &blue_val) in rem.iter().enumerate() {
636 tail |= (blue_val as u64) << (idx * 8);
637 }
638 Self::mix_hash(hash, tail);
639 }
640 }
641
642 #[inline]
649 pub(crate) fn fold_command_full(hash: &mut u64, cmd: &DrawCommand) {
650 match cmd {
651 DrawCommand::Clear(char_item) => {
652 Self::mix_hash(hash, 1);
653 Self::mix_hash(hash, u32::from_le_bytes(char_item.to_rgba_bytes()) as u64);
654 }
655 DrawCommand::FillRoundedRect {
656 rect,
657 color,
658 radius,
659 border,
660 } => {
661 Self::mix_hash(hash, 2);
662 Self::mix_hash(
663 hash,
664 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
665 );
666 Self::mix_hash(
667 hash,
668 ((rect.get_width().to_bits() as u64) << 32)
669 | (rect.get_height().to_bits() as u64),
670 );
671 Self::mix_hash(
672 hash,
673 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
674 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64),
675 );
676 Self::mix_hash(
677 hash,
678 ((radius.get_top_left().to_bits() as u64) << 32)
679 | (radius.get_top_right().to_bits() as u64),
680 );
681 Self::mix_hash(
682 hash,
683 ((radius.get_bottom_right().to_bits() as u64) << 32)
684 | (radius.get_bottom_left().to_bits() as u64),
685 );
686 Self::mix_hash(hash, border.get_width().to_bits() as u64);
687 }
688 DrawCommand::LinearGradientRect {
689 rect,
690 gradient,
691 radius,
692 } => {
693 Self::mix_hash(hash, 3);
694 Self::mix_hash(
695 hash,
696 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
697 );
698 Self::mix_hash(
699 hash,
700 ((rect.get_width().to_bits() as u64) << 32)
701 | (rect.get_height().to_bits() as u64),
702 );
703 Self::mix_hash(
704 hash,
705 ((u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64) << 32)
706 | (u32::from_le_bytes(gradient.get_end_color().to_rgba_bytes()) as u64),
707 );
708 Self::mix_hash(
709 hash,
710 ((gradient.get_angle_deg().to_bits() as u64) << 32)
711 | (radius.max_radius().to_bits() as u64),
712 );
713 }
714 DrawCommand::BoxShadow {
715 rect,
716 radius,
717 shadow,
718 } => {
719 Self::mix_hash(hash, 4);
720 Self::mix_hash(
721 hash,
722 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
723 );
724 Self::mix_hash(
725 hash,
726 ((rect.get_width().to_bits() as u64) << 32)
727 | (rect.get_height().to_bits() as u64),
728 );
729 Self::mix_hash(
730 hash,
731 ((shadow.get_offset_x().to_bits() as u64) << 32)
732 | (shadow.get_offset_y().to_bits() as u64),
733 );
734 Self::mix_hash(
735 hash,
736 ((shadow.get_blur_radius().to_bits() as u64) << 32)
737 | (shadow.get_spread_radius().to_bits() as u64),
738 );
739 Self::mix_hash(
740 hash,
741 ((radius.max_radius().to_bits() as u64) << 32)
742 | (u32::from_le_bytes(shadow.get_color().to_rgba_bytes()) as u64),
743 );
744 }
745 DrawCommand::BackdropBlur {
746 rect,
747 radius,
748 blur_radius,
749 } => {
750 Self::mix_hash(hash, 15);
751 Self::mix_hash(
752 hash,
753 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
754 );
755 Self::mix_hash(
756 hash,
757 ((rect.get_width().to_bits() as u64) << 32)
758 | (rect.get_height().to_bits() as u64),
759 );
760 Self::mix_hash(
761 hash,
762 ((radius.max_radius().to_bits() as u64) << 32) | (blur_radius.to_bits() as u64),
763 );
764 }
765 DrawCommand::TextRun { rect, text, style } => {
766 Self::mix_hash(hash, 5);
767 Self::mix_hash(
768 hash,
769 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
770 );
771 Self::mix_hash(
772 hash,
773 ((rect.get_width().to_bits() as u64) << 32)
774 | (rect.get_height().to_bits() as u64),
775 );
776 let style_flags: u64 = (style.get_family() as u64)
777 | ((style.get_weight() as u64) << 8)
778 | ((style.get_align() as u64) << 16)
779 | (u64::from(style.get_italic()) << 24)
780 | ((u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64) << 32);
781 Self::mix_hash(hash, style_flags);
782 Self::mix_hash(hash, style.get_font_size().to_bits() as u64);
783 Self::fold_bytes_wordwise(hash, text.as_bytes());
784 }
785 DrawCommand::TextCaret {
786 text_rect,
787 content_rect,
788 text,
789 style,
790 caret_height,
791 color,
792 multiline,
793 } => {
794 Self::mix_hash(hash, 13);
795 Self::mix_hash(
796 hash,
797 ((text_rect.get_x().to_bits() as u64) << 32)
798 | (text_rect.get_y().to_bits() as u64),
799 );
800 Self::mix_hash(
801 hash,
802 ((content_rect.get_width().to_bits() as u64) << 32)
803 | (caret_height.to_bits() as u64),
804 );
805 Self::mix_hash(
806 hash,
807 ((style.get_font_size().to_bits() as u64) << 32)
808 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64)
809 ^ u64::from(*multiline),
810 );
811 Self::fold_bytes_wordwise(hash, text.as_bytes());
812 }
813 DrawCommand::Image {
814 rect,
815 buffer,
816 radius,
817 } => {
818 Self::mix_hash(hash, 6);
819 Self::mix_hash(
820 hash,
821 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
822 );
823 Self::mix_hash(
824 hash,
825 ((rect.get_width().to_bits() as u64) << 32)
826 | (rect.get_height().to_bits() as u64),
827 );
828 Self::mix_hash(
829 hash,
830 ((buffer.get_width() as u64) << 32) | (buffer.get_height() as u64),
831 );
832 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
833 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
834 }
835 DrawCommand::SharedImage {
836 rect,
837 width,
838 height,
839 pixels,
840 radius,
841 } => {
842 Self::mix_hash(hash, 6);
843 Self::mix_hash(
844 hash,
845 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
846 );
847 Self::mix_hash(
848 hash,
849 ((rect.get_width().to_bits() as u64) << 32)
850 | (rect.get_height().to_bits() as u64),
851 );
852 Self::mix_hash(hash, ((*width as u64) << 32) | (*height as u64));
853 Self::mix_hash(hash, pixels.as_ptr() as usize as u64);
854 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
855 }
856 DrawCommand::Line {
857 from,
858 to,
859 color,
860 width,
861 } => {
862 Self::mix_hash(hash, 7);
863 Self::mix_hash(
864 hash,
865 ((from.get_x().to_bits() as u64) << 32) | (from.get_y().to_bits() as u64),
866 );
867 Self::mix_hash(
868 hash,
869 ((to.get_x().to_bits() as u64) << 32) | (to.get_y().to_bits() as u64),
870 );
871 Self::mix_hash(
872 hash,
873 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
874 | (width.to_bits() as u64),
875 );
876 }
877 DrawCommand::Circle {
878 center,
879 radius,
880 color,
881 } => {
882 Self::mix_hash(hash, 8);
883 Self::mix_hash(
884 hash,
885 ((center.get_x().to_bits() as u64) << 32) | (center.get_y().to_bits() as u64),
886 );
887 Self::mix_hash(
888 hash,
889 ((radius.to_bits() as u64) << 32)
890 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64),
891 );
892 }
893 DrawCommand::Triangle { p0, p1, p2, color } => {
894 Self::mix_hash(hash, 9);
895 Self::mix_hash(
896 hash,
897 ((p0.get_x().to_bits() as u64) << 32) | (p0.get_y().to_bits() as u64),
898 );
899 Self::mix_hash(
900 hash,
901 ((p1.get_x().to_bits() as u64) << 32) | (p1.get_y().to_bits() as u64),
902 );
903 Self::mix_hash(
904 hash,
905 ((p2.get_x().to_bits() as u64) << 32) | (p2.get_y().to_bits() as u64),
906 );
907 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
908 }
909 DrawCommand::SpriteRegion {
910 rect,
911 buffer,
912 src_rect,
913 tint,
914 } => {
915 Self::mix_hash(hash, 10);
916 Self::mix_hash(
917 hash,
918 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
919 );
920 Self::mix_hash(
921 hash,
922 ((rect.get_width().to_bits() as u64) << 32)
923 | (rect.get_height().to_bits() as u64),
924 );
925 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
926 Self::mix_hash(
927 hash,
928 ((src_rect.get_x().to_bits() as u64) << 32)
929 | (src_rect.get_y().to_bits() as u64),
930 );
931 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
932 }
933 DrawCommand::ExternalSurface { rect, radius } => {
934 Self::mix_hash(hash, 15);
935 Self::mix_hash(
936 hash,
937 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
938 );
939 Self::mix_hash(
940 hash,
941 ((rect.get_width().to_bits() as u64) << 32)
942 | (rect.get_height().to_bits() as u64),
943 );
944 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
945 }
946 DrawCommand::PushClip(red_val) => {
947 Self::mix_hash(hash, 11);
948 Self::mix_hash(
949 hash,
950 ((red_val.get_x().to_bits() as u64) << 32) | (red_val.get_y().to_bits() as u64),
951 );
952 Self::mix_hash(
953 hash,
954 ((red_val.get_width().to_bits() as u64) << 32)
955 | (red_val.get_height().to_bits() as u64),
956 );
957 }
958 DrawCommand::PushRoundedClip { rect, radius } => {
959 Self::mix_hash(hash, 14);
960 Self::mix_hash(
961 hash,
962 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
963 );
964 Self::mix_hash(
965 hash,
966 ((rect.get_width().to_bits() as u64) << 32)
967 | (rect.get_height().to_bits() as u64),
968 );
969 Self::mix_hash(
970 hash,
971 ((radius.get_top_left().to_bits() as u64) << 32)
972 | (radius.get_top_right().to_bits() as u64),
973 );
974 Self::mix_hash(
975 hash,
976 ((radius.get_bottom_right().to_bits() as u64) << 32)
977 | (radius.get_bottom_left().to_bits() as u64),
978 );
979 }
980 DrawCommand::PopClip => {
981 Self::mix_hash(hash, 12);
982 }
983 }
984 }
985
986 #[inline]
996 #[must_use]
997 pub fn fast_hash(&self) -> u64 {
998 let Self {
999 commands, arena, ..
1000 } = self;
1001 let mut hash: u64 = arena.running_fast_hash;
1002 Self::mix_hash(&mut hash, commands.len() as u64);
1003 hash
1004 }
1005
1006 #[inline]
1016 #[must_use]
1017 pub fn base_layer_hash(&self) -> u64 {
1018 let Self {
1019 commands, arena, ..
1020 } = self;
1021 if arena.base_layer_finalized {
1022 return arena.base_layer_version.max(1);
1023 }
1024 let mut hash: u64 = arena.cur_base_digest;
1025 Self::mix_hash(&mut hash, commands.len() as u64);
1026 if hash == 0 { 1 } else { hash }
1027 }
1028}
1029
1030impl PaintFragmentCache {
1032 pub fn clear(&mut self) {
1038 let Self {
1039 has_last_scene,
1040 last_has_canvas,
1041 slots,
1042 cmd_slab,
1043 text_slab,
1044 canvas_stability,
1045 ..
1046 } = self;
1047 *has_last_scene = false;
1048 *last_has_canvas = false;
1049 slots.fill(RetainedPaintFragment::default());
1050 cmd_slab.clear();
1051 text_slab.clear();
1052 for entry in canvas_stability.iter_mut() {
1053 *entry = CanvasStabilityEntry::default();
1054 }
1055 }
1056
1057 #[must_use]
1067 pub fn stats(&self) -> (u64, u64, u64, u64) {
1068 (
1069 self.get_whole_list_hits(),
1070 self.get_fragment_hits(),
1071 self.get_fragment_misses(),
1072 self.get_fragment_evictions(),
1073 )
1074 }
1075
1076 #[inline]
1082 fn ensure_slots(&mut self) {
1083 let Self {
1084 slots,
1085 cmd_slab,
1086 text_slab,
1087 ..
1088 } = self;
1089 if slots.len() != PAINT_FRAGMENT_CACHE_SLOTS {
1090 slots.resize(PAINT_FRAGMENT_CACHE_SLOTS, RetainedPaintFragment::default());
1091 }
1092 if cmd_slab.capacity() < PAINT_FRAGMENT_CMD_SLAB_CAP {
1093 cmd_slab.reserve_exact(PAINT_FRAGMENT_CMD_SLAB_CAP - cmd_slab.len());
1094 }
1095 if text_slab.capacity() < PAINT_FRAGMENT_TEXT_SLAB_CAP {
1096 text_slab.reserve_exact(PAINT_FRAGMENT_TEXT_SLAB_CAP - text_slab.len());
1097 }
1098 }
1099
1100 #[inline]
1113 fn encode_cached_command(
1114 cmd: &DrawCommand,
1115 text_dst: &mut [u8],
1116 text_cursor: &mut usize,
1117 text_base: usize,
1118 ) -> CachedDrawCommand {
1119 match cmd {
1120 DrawCommand::Clear(char_item) => CachedDrawCommand::Clear(*char_item),
1121 DrawCommand::FillRoundedRect {
1122 rect,
1123 color,
1124 radius,
1125 border,
1126 } => CachedDrawCommand::FillRoundedRect {
1127 rect: *rect,
1128 color: *color,
1129 radius: *radius,
1130 border: *border,
1131 },
1132 DrawCommand::LinearGradientRect {
1133 rect,
1134 gradient,
1135 radius,
1136 } => CachedDrawCommand::LinearGradientRect {
1137 rect: *rect,
1138 gradient: *gradient,
1139 radius: *radius,
1140 },
1141 DrawCommand::BoxShadow {
1142 rect,
1143 radius,
1144 shadow,
1145 } => CachedDrawCommand::BoxShadow {
1146 rect: *rect,
1147 radius: *radius,
1148 shadow: *shadow,
1149 },
1150 DrawCommand::BackdropBlur {
1151 rect,
1152 radius,
1153 blur_radius,
1154 } => CachedDrawCommand::BackdropBlur {
1155 rect: *rect,
1156 radius: *radius,
1157 blur_radius: *blur_radius,
1158 },
1159 DrawCommand::TextRun { rect, text, style } => {
1160 let bytes: &[u8] = text.as_bytes();
1161 let start_rel: usize = *text_cursor;
1162 let end_rel: usize = start_rel.saturating_add(bytes.len());
1163 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1164 *text_cursor = end_rel;
1165 CachedDrawCommand::TextRun {
1166 rect: *rect,
1167 text_start: (text_base.saturating_add(start_rel)) as u32,
1168 text_len: bytes.len() as u32,
1169 style: *style,
1170 }
1171 }
1172 DrawCommand::TextCaret {
1173 text_rect,
1174 content_rect,
1175 text,
1176 style,
1177 caret_height,
1178 color,
1179 multiline,
1180 } => {
1181 let bytes: &[u8] = text.as_bytes();
1182 let start_rel: usize = *text_cursor;
1183 let end_rel: usize = start_rel.saturating_add(bytes.len());
1184 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1185 *text_cursor = end_rel;
1186 CachedDrawCommand::TextCaret {
1187 text_rect: *text_rect,
1188 content_rect: *content_rect,
1189 text_start: (text_base.saturating_add(start_rel)) as u32,
1190 text_len: bytes.len() as u32,
1191 style: *style,
1192 caret_height: *caret_height,
1193 color: *color,
1194 multiline: *multiline,
1195 }
1196 }
1197 DrawCommand::Image {
1198 rect,
1199 buffer,
1200 radius,
1201 } => CachedDrawCommand::Image {
1202 rect: *rect,
1203 buffer: buffer.clone(),
1204 radius: *radius,
1205 },
1206 DrawCommand::SharedImage {
1207 rect,
1208 width,
1209 height,
1210 pixels,
1211 radius,
1212 } => CachedDrawCommand::SharedImage {
1213 rect: *rect,
1214 width: *width,
1215 height: *height,
1216 pixels: Arc::clone(pixels),
1217 radius: *radius,
1218 },
1219 DrawCommand::Line {
1220 from,
1221 to,
1222 color,
1223 width,
1224 } => CachedDrawCommand::Line {
1225 from: *from,
1226 to: *to,
1227 color: *color,
1228 width: *width,
1229 },
1230 DrawCommand::Circle {
1231 center,
1232 radius,
1233 color,
1234 } => CachedDrawCommand::Circle {
1235 center: *center,
1236 radius: *radius,
1237 color: *color,
1238 },
1239 DrawCommand::Triangle { p0, p1, p2, color } => CachedDrawCommand::Triangle {
1240 p0: *p0,
1241 p1: *p1,
1242 p2: *p2,
1243 color: *color,
1244 },
1245 DrawCommand::SpriteRegion {
1246 rect,
1247 buffer,
1248 src_rect,
1249 tint,
1250 } => CachedDrawCommand::SpriteRegion {
1251 rect: *rect,
1252 buffer: buffer.clone(),
1253 src_rect: *src_rect,
1254 tint: *tint,
1255 },
1256 DrawCommand::ExternalSurface { rect, radius } => CachedDrawCommand::ExternalSurface {
1257 rect: *rect,
1258 radius: *radius,
1259 },
1260 DrawCommand::PushClip(rect) => CachedDrawCommand::PushClip(*rect),
1261 DrawCommand::PushRoundedClip { rect, radius } => CachedDrawCommand::PushRoundedClip {
1262 rect: *rect,
1263 radius: *radius,
1264 },
1265 DrawCommand::PopClip => CachedDrawCommand::PopClip,
1266 }
1267 }
1268
1269 fn capture_fragment(
1278 &mut self,
1279 slot_idx: usize,
1280 meta: (NodeId, u32, u32, Point, u64, u64),
1281 cmd_slice: &[DrawCommand],
1282 ) {
1283 let (node_id, phys_scale_milli, opacity_bits, offset_phys, geom_hash, style_text_hash): (
1284 NodeId,
1285 u32,
1286 u32,
1287 Point,
1288 u64,
1289 u64,
1290 ) = meta;
1291 let Self {
1292 slots,
1293 cmd_slab,
1294 text_slab,
1295 fragment_misses,
1296 fragment_evictions,
1297 ..
1298 } = self;
1299 if slot_idx >= slots.len() {
1300 return;
1301 }
1302 *fragment_misses = fragment_misses.wrapping_add(1);
1303 let need_cmds: usize = cmd_slice.len();
1304 let mut need_text: usize = 0;
1305 for cmd in cmd_slice.iter() {
1306 match cmd {
1307 DrawCommand::TextRun { text, .. } | DrawCommand::TextCaret { text, .. } => {
1308 need_text = need_text.saturating_add(text.len());
1309 }
1310 _ => {}
1311 }
1312 }
1313 if need_cmds > PAINT_FRAGMENT_CMD_SLAB_CAP || need_text > PAINT_FRAGMENT_TEXT_SLAB_CAP {
1314 return;
1315 }
1316 let existing: RetainedPaintFragment = slots[slot_idx];
1317 if existing.occupied && existing.node_id != node_id {
1318 *fragment_evictions = fragment_evictions.wrapping_add(1);
1319 }
1320 if existing.occupied
1321 && need_cmds <= (existing.cmd_cap as usize)
1322 && need_text <= (existing.text_cap as usize)
1323 {
1324 let cmd_base: usize = existing.cmd_start as usize;
1325 let text_base: usize = existing.text_start as usize;
1326 let text_end: usize = text_base.saturating_add(need_text);
1327 let mut text_cursor: usize = 0;
1328 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1329 for (item_index, cmd) in cmd_slice.iter().enumerate() {
1330 cmd_slab[cmd_base + item_index] =
1331 Self::encode_cached_command(cmd, text_dst, &mut text_cursor, text_base);
1332 }
1333 slots[slot_idx] = RetainedPaintFragment {
1334 occupied: true,
1335 node_id,
1336 phys_scale_milli,
1337 opacity_bits,
1338 offset_phys,
1339 geom_hash,
1340 style_text_hash,
1341 cmd_start: existing.cmd_start,
1342 cmd_len: need_cmds as u32,
1343 cmd_cap: existing.cmd_cap,
1344 text_start: existing.text_start,
1345 text_cap: existing.text_cap,
1346 };
1347 return;
1348 }
1349 if cmd_slab.len().saturating_add(need_cmds) > PAINT_FRAGMENT_CMD_SLAB_CAP
1350 || text_slab.len().saturating_add(need_text) > PAINT_FRAGMENT_TEXT_SLAB_CAP
1351 {
1352 let mut evicted: u64 = 0;
1353 for source_val in slots.iter_mut() {
1354 if source_val.occupied {
1355 evicted = evicted.saturating_add(1);
1356 *source_val = RetainedPaintFragment::default();
1357 }
1358 }
1359 *fragment_evictions = fragment_evictions.wrapping_add(evicted);
1360 cmd_slab.clear();
1361 text_slab.clear();
1362 }
1363 let cmd_start: usize = cmd_slab.len();
1364 let text_base: usize = text_slab.len();
1365 let text_end: usize = text_base.saturating_add(need_text);
1366 text_slab.resize(text_end, 0);
1367 let mut text_cursor: usize = 0;
1368 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1369 for cmd in cmd_slice.iter() {
1370 cmd_slab.push(Self::encode_cached_command(
1371 cmd,
1372 text_dst,
1373 &mut text_cursor,
1374 text_base,
1375 ));
1376 }
1377 slots[slot_idx] = RetainedPaintFragment {
1378 occupied: true,
1379 node_id,
1380 phys_scale_milli,
1381 opacity_bits,
1382 offset_phys,
1383 geom_hash,
1384 style_text_hash,
1385 cmd_start: cmd_start as u32,
1386 cmd_len: need_cmds as u32,
1387 cmd_cap: need_cmds as u32,
1388 text_start: text_base as u32,
1389 text_cap: need_text as u32,
1390 };
1391 }
1392
1393 #[inline]
1408 fn record_canvas_output(
1409 &mut self,
1410 node_id: NodeId,
1411 phys_scale_milli: u32,
1412 bounds: Rect,
1413 tree_revision: u64,
1414 cmds: &[DrawCommand],
1415 ) -> bool {
1416 let slot_idx: usize = (node_id.get_index() as usize) & (CANVAS_STABILITY_SLOTS - 1);
1417 let Self {
1418 canvas_stability, ..
1419 } = self;
1420 let slot: &mut CanvasStabilityEntry = &mut canvas_stability[slot_idx];
1421 let mut has_raster: bool = false;
1422 let mut cmd_hash: u64 = FNV1A_OFFSET_BASIS;
1423 for cmd in cmds.iter() {
1424 if matches!(
1425 cmd,
1426 DrawCommand::Image { .. }
1427 | DrawCommand::SharedImage { .. }
1428 | DrawCommand::SpriteRegion { .. }
1429 | DrawCommand::ExternalSurface { .. }
1430 ) {
1431 has_raster = true;
1432 break;
1433 }
1434 DisplayList::fold_command_full(&mut cmd_hash, cmd);
1435 }
1436 if has_raster {
1437 *slot = CanvasStabilityEntry {
1438 node_id,
1439 phys_scale_milli,
1440 bounds,
1441 tree_revision,
1442 cmd_hash: 0,
1443 stable_hits: 0,
1444 proven_dynamic: true,
1445 occupied: true,
1446 };
1447 return true;
1448 }
1449 if slot.occupied
1450 && slot.node_id == node_id
1451 && slot.phys_scale_milli == phys_scale_milli
1452 && slot.bounds == bounds
1453 {
1454 if slot.proven_dynamic {
1455 return true;
1456 }
1457 if slot.cmd_hash == cmd_hash {
1458 slot.tree_revision = tree_revision;
1459 slot.stable_hits = slot.stable_hits.saturating_add(1);
1460 return slot.stable_hits < 2;
1461 }
1462 if slot.tree_revision == tree_revision {
1463 slot.proven_dynamic = true;
1464 return true;
1465 }
1466 slot.tree_revision = tree_revision;
1467 slot.cmd_hash = cmd_hash;
1468 slot.stable_hits = 1;
1469 return true;
1470 }
1471 *slot = CanvasStabilityEntry {
1472 node_id,
1473 phys_scale_milli,
1474 bounds,
1475 tree_revision,
1476 cmd_hash,
1477 stable_hits: 1,
1478 proven_dynamic: false,
1479 occupied: true,
1480 };
1481 true
1482 }
1483}
1484
1485impl PaintPass {
1487 #[inline]
1497 #[must_use]
1498 fn text_selection_sig(tree: &SceneTree) -> u64 {
1499 let Some(sel) = tree.text_selection() else {
1500 return 0;
1501 };
1502 if sel.is_empty() {
1503 return 0;
1504 }
1505 let anchor_id: NodeId = sel.get_anchor_node();
1506 let focus_id: NodeId = sel.get_focus_node();
1507 let mut height_val: u64 =
1508 ((anchor_id.get_index() as u64) << 32) | (anchor_id.get_generation() as u64);
1509 height_val =
1510 (height_val ^ (sel.get_anchor_char() as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1511 let focus_bits: u64 =
1512 ((focus_id.get_index() as u64) << 32) | (focus_id.get_generation() as u64);
1513 height_val = (height_val ^ focus_bits).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1514 height_val =
1515 (height_val ^ (sel.get_focus_char() as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1516 if height_val == 0 { 1 } else { height_val }
1517 }
1518
1519 #[inline(always)]
1526 fn fx_fold(hash: &mut u64, word: u64) {
1527 *hash = (*hash ^ word).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1528 }
1529
1530 #[inline(always)]
1537 fn fx_fold_rect(hash: &mut u64, rect: Rect) {
1538 let lo: u64 = ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64);
1539 let hi: u64 =
1540 ((rect.get_width().to_bits() as u64) << 32) | (rect.get_height().to_bits() as u64);
1541 Self::fx_fold(hash, lo);
1542 Self::fx_fold(hash, hi);
1543 }
1544
1545 #[inline]
1552 fn fx_fold_paint(hash: &mut u64, paint: &PaintStyle) {
1553 let bg_bits: u64 = paint
1554 .background_color()
1555 .map(|color_value: Color| {
1556 u32::from_le_bytes(color_value.to_rgba_bytes()) as u64 | (1_u64 << 32)
1557 })
1558 .unwrap_or(0);
1559 Self::fx_fold(hash, bg_bits);
1560 if let Some(grad) = paint.background_gradient() {
1561 let g0: u64 = u32::from_le_bytes(grad.get_start_color().to_rgba_bytes()) as u64;
1562 let g1: u64 = u32::from_le_bytes(grad.get_end_color().to_rgba_bytes()) as u64;
1563 Self::fx_fold(hash, (g0 << 32) | g1);
1564 Self::fx_fold(hash, grad.get_angle_deg().to_bits() as u64 | (1_u64 << 33));
1565 }
1566 let border: Border = paint.get_border();
1567 let b_word: u64 = ((border.get_width().to_bits() as u64) << 32)
1568 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64);
1569 Self::fx_fold(hash, b_word);
1570 let red_val: CornerRadius = paint.get_radius();
1571 let r0: u64 = ((red_val.get_top_left().to_bits() as u64) << 32)
1572 | (red_val.get_top_right().to_bits() as u64);
1573 let r1: u64 = ((red_val.get_bottom_right().to_bits() as u64) << 32)
1574 | (red_val.get_bottom_left().to_bits() as u64);
1575 Self::fx_fold(hash, r0);
1576 Self::fx_fold(hash, r1);
1577 Self::fx_fold(hash, paint.get_opacity().to_bits() as u64);
1578 if let Some(sh) = paint.box_shadow() {
1579 let s0: u64 =
1580 ((sh.get_offset_x().to_bits() as u64) << 32) | (sh.get_offset_y().to_bits() as u64);
1581 let s1: u64 = ((sh.get_blur_radius().to_bits() as u64) << 32)
1582 | (sh.get_spread_radius().to_bits() as u64);
1583 Self::fx_fold(hash, s0);
1584 Self::fx_fold(hash, s1);
1585 Self::fx_fold(
1586 hash,
1587 u32::from_le_bytes(sh.get_color().to_rgba_bytes()) as u64 | (1_u64 << 34),
1588 );
1589 }
1590 }
1591
1592 #[inline]
1600 fn fx_fold_text(hash: &mut u64, text_style: &TextStyle, text: &str) {
1601 let m0: u64 = ((text_style.get_font_size().to_bits() as u64) << 32)
1602 | (text_style.get_line_height().to_bits() as u64);
1603 let m1: u64 = ((text_style.get_letter_spacing().to_bits() as u64) << 32)
1604 | (u32::from_le_bytes(text_style.get_color().to_rgba_bytes()) as u64);
1605 let flags: u64 = (text_style.get_family() as u64)
1606 | ((text_style.get_weight() as u64) << 8)
1607 | ((text_style.get_align() as u64) << 16)
1608 | ((text_style.get_overflow() as u64) << 24)
1609 | (u64::from(text_style.get_italic()) << 32);
1610 Self::fx_fold(hash, m0);
1611 Self::fx_fold(hash, m1);
1612 Self::fx_fold(hash, flags);
1613 let bytes: &[u8] = text.as_bytes();
1614 Self::fx_fold(hash, bytes.len() as u64);
1615 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
1616 for chunk in chunks.iter() {
1617 Self::fx_fold(hash, u64::from_ne_bytes(*chunk));
1618 }
1619 if !rem.is_empty() {
1620 let mut tail: u64 = 0;
1621 for (idx, &blue_val) in rem.iter().enumerate() {
1622 tail |= (blue_val as u64) << (idx * 8);
1623 }
1624 Self::fx_fold(hash, tail);
1625 }
1626 }
1627
1628 #[inline(always)]
1638 #[must_use]
1639 fn quantize_scale_milli(scale: f32) -> u32 {
1640 (scale.max(0.5) * 1000.0).round() as u32
1641 }
1642
1643 #[inline]
1658 #[must_use]
1659 fn fingerprint_subtree_children(
1660 tree: &SceneTree,
1661 parent: NodeId,
1662 depth: usize,
1663 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1664 focus_pair: (Option<NodeId>, bool),
1665 acc: &mut (usize, u64, u64),
1666 ) -> bool {
1667 let child_count: usize = tree.child_count(parent);
1668 if depth > PAINT_FRAGMENT_MAX_DEPTH || child_count > PAINT_FRAGMENT_MAX_CHILDREN {
1669 return false;
1670 }
1671 let (active_hover_target, has_text_sel): (Option<NodeId>, bool) = focus_pair;
1672 Self::fx_fold(&mut acc.1, child_count as u64);
1673 for cid in tree.children_iter(parent) {
1674 if acc.0 >= PAINT_FRAGMENT_MAX_NODES
1675 || active_hover_target == Some(cid)
1676 || (has_text_sel && tree.text_selection_range_for(cid).is_some())
1677 || tree.backdrop_blur_radius(cid) > 0.0
1678 {
1679 return false;
1680 }
1681 let Some(child) = tree.get_node(cid) else {
1682 return false;
1683 };
1684 acc.0 += 1;
1685 let c_inter: InteractionState = child.get_interaction();
1686 let c_active: bool = c_inter.get_hovered() || c_inter.get_pressed();
1687 if c_active || c_inter.get_focused() {
1688 return false;
1689 }
1690 let c_style: &Style = child.style_ref();
1691 let child_is_top_overlay: bool = c_style.layout.get_position() == Position::Fixed
1692 || (c_style.layout.get_position() == Position::Absolute
1693 && c_style.layout.get_z_index() >= 50);
1694 if child_is_top_overlay {
1695 return false;
1696 }
1697 let c_tag: u64 = match child.get_kind() {
1698 NodeKind::View | NodeKind::Fragment => 1,
1699 NodeKind::Text(text) => {
1700 Self::fx_fold_text(&mut acc.2, &c_style.text, text.as_str());
1701 Self::fx_fold_rect(&mut acc.1, child.layout_box.get_content_bounds());
1702 2
1703 }
1704 NodeKind::Image(buf) => {
1705 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1706 Self::fx_fold(
1707 &mut acc.2,
1708 ((buf.get_width() as u64) << 32) | (buf.get_height() as u64),
1709 );
1710 Self::fx_fold(&mut acc.2, buf.get_pixels().as_ptr() as usize as u64);
1711 3
1712 }
1713 NodeKind::Canvas(CanvasCaching::RetainedStatic) => {
1714 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1715 if let Some(callback_fn) = child.canvas_callback() {
1716 let cb_addr: u64 = (Rc::as_ptr(&callback_fn) as *const ()) as usize as u64;
1717 Self::fx_fold(&mut acc.2, cb_addr);
1718 }
1719 4
1720 }
1721 NodeKind::Portal
1722 | NodeKind::Canvas(CanvasCaching::DynamicPerFrame)
1723 | NodeKind::Input(_)
1724 | NodeKind::ScrollView => {
1725 return false;
1726 }
1727 };
1728 let c_id_bits: u64 = ((cid.get_index() as u64) << 32) | (cid.get_generation() as u64);
1729 Self::fx_fold(&mut acc.1, c_id_bits);
1730 let raw_b: Rect = child.layout_box.get_bounds();
1731 Self::fx_fold_rect(&mut acc.1, raw_b);
1732 let ct: Transform2D = c_style.paint.get_transform();
1733 Self::fx_fold(
1734 &mut acc.1,
1735 ((ct.get_translate_x().to_bits() as u64) << 32)
1736 | (ct.get_translate_y().to_bits() as u64),
1737 );
1738 Self::fx_fold(
1739 &mut acc.1,
1740 ((ct.get_scale_x().to_bits() as u64) << 32) | (ct.get_scale_y().to_bits() as u64),
1741 );
1742 let c_lay_bits: u64 = (c_tag << 40)
1743 | ((c_style.layout.get_display() as u64) << 32)
1744 | ((c_style.layout.get_overflow() as u64) << 24)
1745 | ((c_style.layout.get_z_index() as u32) as u64);
1746 Self::fx_fold(&mut acc.2, c_lay_bits);
1747 Self::fx_fold_paint(&mut acc.2, &c_style.paint);
1748 let mut next_cull_check: Option<(Point, Rect, f32, f32)> = None;
1749 if let Some((off_phys, cull_rect, scale, inv_scale)) = unclipped_cull {
1750 let tx_phys: f32 = (ct.get_translate_x() * scale).round();
1751 let ty_phys: f32 = (ct.get_translate_y() * scale).round();
1752 let tot_x: f32 = off_phys.get_x() + tx_phys;
1753 let tot_y: f32 = off_phys.get_y() + ty_phys;
1754 let rx0: f32 = (raw_b.get_x() * scale).round();
1755 let ry0: f32 = (raw_b.get_y() * scale).round();
1756 let rx1: f32 = (raw_b.right() * scale).round();
1757 let ry1: f32 = (raw_b.bottom() * scale).round();
1758 let stroke_width: f32 = ((rx1 - rx0).max(0.0) * ct.get_scale_x()).round().max(0.0);
1759 let sh: f32 = ((ry1 - ry0).max(0.0) * ct.get_scale_y()).round().max(0.0);
1760 let sdx: f32 = (((rx1 - rx0).max(0.0) - stroke_width) * 0.5).round();
1761 let sdy: f32 = (((ry1 - ry0).max(0.0) - sh) * 0.5).round();
1762 let c_bounds: Rect = Rect::new(
1763 (rx0 + tot_x + sdx) * inv_scale,
1764 (ry0 + tot_y + sdy) * inv_scale,
1765 stroke_width * inv_scale,
1766 sh * inv_scale,
1767 );
1768 let c_vis: Rect = Self::conservative_self_vis_rect(
1769 child,
1770 c_bounds,
1771 &c_style.paint,
1772 false,
1773 (tot_x, tot_y),
1774 scale,
1775 inv_scale,
1776 );
1777 let c_in_x: bool =
1778 c_vis.get_x() >= cull_rect.get_x() && c_vis.right() <= cull_rect.right();
1779 let c_in_y: bool =
1780 c_vis.get_y() >= cull_rect.get_y() && c_vis.bottom() <= cull_rect.bottom();
1781 if !c_in_x || !c_in_y {
1782 return false;
1783 }
1784 if c_style.layout.get_overflow() != Overflow::Hidden {
1785 let c_scroll: Point = child.layout_box.get_scroll_offset();
1786 let next_off: Point = Point::new(
1787 tot_x - (c_scroll.get_x() * scale).round(),
1788 tot_y - (c_scroll.get_y() * scale).round(),
1789 );
1790 next_cull_check = Some((next_off, cull_rect, scale, inv_scale));
1791 }
1792 }
1793 if tree.has_children(cid)
1794 && !Self::fingerprint_subtree_children(
1795 tree,
1796 cid,
1797 depth + 1,
1798 next_cull_check,
1799 focus_pair,
1800 acc,
1801 )
1802 {
1803 return false;
1804 }
1805 }
1806 true
1807 }
1808
1809 #[inline]
1823 #[must_use]
1824 fn fingerprint_subtree(
1825 tree: &SceneTree,
1826 node_id: NodeId,
1827 node: &SceneNode,
1828 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1829 focus_pair: (Option<NodeId>, bool),
1830 ) -> Option<(u64, u64)> {
1831 let child_count: usize = tree.child_count(node_id);
1832 if child_count == 0
1833 || child_count > PAINT_FRAGMENT_MAX_CHILDREN
1834 || tree.backdrop_blur_radius(node_id) > 0.0
1835 {
1836 return None;
1837 }
1838 let style: &Style = node.style_ref();
1839 let transform: Transform2D = style.paint.get_transform();
1840 let scroll: Point = node.layout_box.get_scroll_offset();
1841 let mut geom_h: u64 = child_count as u64;
1842 Self::fx_fold_rect(&mut geom_h, node.layout_box.get_bounds());
1843 Self::fx_fold(
1844 &mut geom_h,
1845 ((transform.get_translate_x().to_bits() as u64) << 32)
1846 | (transform.get_translate_y().to_bits() as u64),
1847 );
1848 Self::fx_fold(
1849 &mut geom_h,
1850 ((transform.get_scale_x().to_bits() as u64) << 32)
1851 | (transform.get_scale_y().to_bits() as u64),
1852 );
1853 Self::fx_fold(
1854 &mut geom_h,
1855 ((scroll.get_x().to_bits() as u64) << 32) | (scroll.get_y().to_bits() as u64),
1856 );
1857 let mut style_h: u64 =
1858 ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
1859 Self::fx_fold_paint(&mut style_h, &style.paint);
1860 let mut acc: (usize, u64, u64) = (0, geom_h, style_h);
1861 if !Self::fingerprint_subtree_children(
1862 tree,
1863 node_id,
1864 1,
1865 unclipped_cull,
1866 focus_pair,
1867 &mut acc,
1868 ) {
1869 return None;
1870 }
1871 Some((acc.1, acc.2))
1872 }
1873
1874 #[inline(always)]
1888 #[must_use]
1889 fn translate_coord(
1890 item_val: f32,
1891 d_phys: f32,
1892 scale: f32,
1893 inv_scale: f32,
1894 dyadic: bool,
1895 ) -> f32 {
1896 if d_phys == 0.0 {
1897 return item_val;
1898 }
1899 if dyadic {
1900 return item_val + d_phys * inv_scale;
1901 }
1902 ((item_val * scale).round() + d_phys) * inv_scale
1903 }
1904
1905 #[inline(always)]
1920 #[must_use]
1921 fn translate_rect(
1922 rect: Rect,
1923 dx_phys: f32,
1924 dy_phys: f32,
1925 scale: f32,
1926 inv_scale: f32,
1927 dyadic: bool,
1928 ) -> Rect {
1929 Rect::new(
1930 Self::translate_coord(rect.get_x(), dx_phys, scale, inv_scale, dyadic),
1931 Self::translate_coord(rect.get_y(), dy_phys, scale, inv_scale, dyadic),
1932 rect.get_width(),
1933 rect.get_height(),
1934 )
1935 }
1936
1937 #[inline]
1947 fn push_translated_command(
1948 cmd: &CachedDrawCommand,
1949 text_slab: &[u8],
1950 delta_phys: (f32, f32),
1951 scale_info: (f32, f32, bool),
1952 display_list: &mut DisplayList,
1953 ) {
1954 let (dx_phys, dy_phys): (f32, f32) = delta_phys;
1955 let (scale, inv_scale, dyadic): (f32, f32, bool) = scale_info;
1956 let translated: DrawCommand = match cmd {
1957 CachedDrawCommand::Clear(char_item) => DrawCommand::Clear(*char_item),
1958 CachedDrawCommand::FillRoundedRect {
1959 rect,
1960 color,
1961 radius,
1962 border,
1963 } => DrawCommand::FillRoundedRect {
1964 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1965 color: *color,
1966 radius: *radius,
1967 border: *border,
1968 },
1969 CachedDrawCommand::LinearGradientRect {
1970 rect,
1971 gradient,
1972 radius,
1973 } => DrawCommand::LinearGradientRect {
1974 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1975 gradient: *gradient,
1976 radius: *radius,
1977 },
1978 CachedDrawCommand::BoxShadow {
1979 rect,
1980 radius,
1981 shadow,
1982 } => DrawCommand::BoxShadow {
1983 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1984 radius: *radius,
1985 shadow: *shadow,
1986 },
1987 CachedDrawCommand::BackdropBlur {
1988 rect,
1989 radius,
1990 blur_radius,
1991 } => DrawCommand::BackdropBlur {
1992 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1993 radius: *radius,
1994 blur_radius: *blur_radius,
1995 },
1996 CachedDrawCommand::TextRun {
1997 rect,
1998 text_start,
1999 text_len,
2000 style,
2001 } => {
2002 let source_val: usize = *text_start as usize;
2003 let event_ref: usize = source_val.saturating_add(*text_len as usize);
2004 let src: &str = text_slab
2005 .get(source_val..event_ref)
2006 .and_then(|blue_val: &[u8]| from_utf8(blue_val).ok())
2007 .unwrap_or("");
2008 DrawCommand::TextRun {
2009 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2010 text: display_list.alloc_text(src),
2011 style: *style,
2012 }
2013 }
2014 CachedDrawCommand::TextCaret {
2015 text_rect,
2016 content_rect,
2017 text_start,
2018 text_len,
2019 style,
2020 caret_height,
2021 color,
2022 multiline,
2023 } => {
2024 let scale_val: usize = *text_start as usize;
2025 let error_val: usize = scale_val.saturating_add(*text_len as usize);
2026 let src: &str = text_slab
2027 .get(scale_val..error_val)
2028 .and_then(|byte_val: &[u8]| from_utf8(byte_val).ok())
2029 .unwrap_or("");
2030 DrawCommand::TextCaret {
2031 text_rect: Self::translate_rect(
2032 *text_rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
2033 ),
2034 content_rect: Self::translate_rect(
2035 *content_rect,
2036 dx_phys,
2037 dy_phys,
2038 scale,
2039 inv_scale,
2040 dyadic,
2041 ),
2042 text: display_list.alloc_text(src),
2043 style: *style,
2044 caret_height: *caret_height,
2045 color: *color,
2046 multiline: *multiline,
2047 }
2048 }
2049 CachedDrawCommand::Image {
2050 rect,
2051 buffer,
2052 radius,
2053 } => DrawCommand::Image {
2054 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2055 buffer: buffer.clone(),
2056 radius: *radius,
2057 },
2058 CachedDrawCommand::SharedImage {
2059 rect,
2060 width,
2061 height,
2062 pixels,
2063 radius,
2064 } => DrawCommand::SharedImage {
2065 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2066 width: *width,
2067 height: *height,
2068 pixels: Arc::clone(pixels),
2069 radius: *radius,
2070 },
2071 CachedDrawCommand::Line {
2072 from,
2073 to,
2074 color,
2075 width,
2076 } => DrawCommand::Line {
2077 from: Point::new(
2078 Self::translate_coord(from.get_x(), dx_phys, scale, inv_scale, dyadic),
2079 Self::translate_coord(from.get_y(), dy_phys, scale, inv_scale, dyadic),
2080 ),
2081 to: Point::new(
2082 Self::translate_coord(to.get_x(), dx_phys, scale, inv_scale, dyadic),
2083 Self::translate_coord(to.get_y(), dy_phys, scale, inv_scale, dyadic),
2084 ),
2085 color: *color,
2086 width: *width,
2087 },
2088 CachedDrawCommand::Circle {
2089 center,
2090 radius,
2091 color,
2092 } => DrawCommand::Circle {
2093 center: Point::new(
2094 Self::translate_coord(center.get_x(), dx_phys, scale, inv_scale, dyadic),
2095 Self::translate_coord(center.get_y(), dy_phys, scale, inv_scale, dyadic),
2096 ),
2097 radius: *radius,
2098 color: *color,
2099 },
2100 CachedDrawCommand::Triangle { p0, p1, p2, color } => DrawCommand::Triangle {
2101 p0: Point::new(
2102 Self::translate_coord(p0.get_x(), dx_phys, scale, inv_scale, dyadic),
2103 Self::translate_coord(p0.get_y(), dy_phys, scale, inv_scale, dyadic),
2104 ),
2105 p1: Point::new(
2106 Self::translate_coord(p1.get_x(), dx_phys, scale, inv_scale, dyadic),
2107 Self::translate_coord(p1.get_y(), dy_phys, scale, inv_scale, dyadic),
2108 ),
2109 p2: Point::new(
2110 Self::translate_coord(p2.get_x(), dx_phys, scale, inv_scale, dyadic),
2111 Self::translate_coord(p2.get_y(), dy_phys, scale, inv_scale, dyadic),
2112 ),
2113 color: *color,
2114 },
2115 CachedDrawCommand::SpriteRegion {
2116 rect,
2117 buffer,
2118 src_rect,
2119 tint,
2120 } => DrawCommand::SpriteRegion {
2121 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2122 buffer: buffer.clone(),
2123 src_rect: *src_rect,
2124 tint: *tint,
2125 },
2126 CachedDrawCommand::ExternalSurface { rect, radius } => DrawCommand::ExternalSurface {
2127 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2128 radius: *radius,
2129 },
2130 CachedDrawCommand::PushClip(rect) => DrawCommand::PushClip(Self::translate_rect(
2131 *rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
2132 )),
2133 CachedDrawCommand::PushRoundedClip { rect, radius } => DrawCommand::PushRoundedClip {
2134 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2135 radius: *radius,
2136 },
2137 CachedDrawCommand::PopClip => DrawCommand::PopClip,
2138 };
2139 display_list.push(translated);
2140 }
2141
2142 pub fn build(
2152 tree: &SceneTree,
2153 viewport: Size,
2154 scale_factor: f32,
2155 clear_color: Color,
2156 display_list: &mut DisplayList,
2157 ) {
2158 let mut saw_canvas: bool = false;
2159 Self::build_inner(
2160 tree,
2161 viewport,
2162 scale_factor,
2163 clear_color,
2164 display_list,
2165 None,
2166 &mut saw_canvas,
2167 );
2168 }
2169
2170 #[inline(always)]
2180 #[must_use]
2181 fn pack_opt_node_id(node: Option<NodeId>) -> u64 {
2182 match node {
2183 Some(id) => ((id.get_index() as u64) << 32) | (id.get_generation() as u64),
2184 None => u64::MAX,
2185 }
2186 }
2187
2188 pub fn build_retained(
2200 tree: &SceneTree,
2201 viewport: Size,
2202 scale_factor: f32,
2203 clear_color: Color,
2204 layout_changed: bool,
2205 display_list: &mut DisplayList,
2206 cache: &mut PaintFragmentCache,
2207 ) {
2208 let clamped_scale: f32 = scale_factor.max(0.5);
2209 let focus: FocusManager = tree.get_focus();
2210 let sig: PaintSceneSignature = PaintSceneSignature {
2211 tree_ptr: (tree as *const SceneTree) as usize,
2212 tree_revision: tree.get_revision(),
2213 viewport_w_bits: viewport.get_width().to_bits(),
2214 viewport_h_bits: viewport.get_height().to_bits(),
2215 scale_bits: clamped_scale.to_bits(),
2216 clear_rgba: u32::from_le_bytes(clear_color.to_rgba_bytes()),
2217 focused_packed: Self::pack_opt_node_id(focus.focused_node()),
2218 hovered_packed: Self::pack_opt_node_id(focus.hovered_node()),
2219 pressed_packed: Self::pack_opt_node_id(focus.pressed_node()),
2220 caret_visible: tree.get_caret_visible(),
2221 text_sel_sig: Self::text_selection_sig(tree),
2222 };
2223 if !layout_changed
2224 && !cache.last_has_canvas
2225 && !display_list.is_empty()
2226 && cache.has_last_scene
2227 && cache.last_scene_sig == sig
2228 {
2229 cache.whole_list_hits = cache.whole_list_hits.wrapping_add(1);
2230 return;
2231 }
2232 cache.ensure_slots();
2233 let mut saw_canvas: bool = false;
2234 Self::build_inner(
2235 tree,
2236 viewport,
2237 clamped_scale,
2238 clear_color,
2239 display_list,
2240 Some(cache),
2241 &mut saw_canvas,
2242 );
2243 cache.last_has_canvas = saw_canvas;
2244 cache.has_last_scene = true;
2245 cache.last_scene_sig = sig;
2246 }
2247
2248 fn collect_portal_ancestors(tree: &SceneTree, out_ancestors: &mut Vec<NodeId>) {
2255 out_ancestors.clear();
2256 let portal_nodes: &[NodeId] = tree.portal_nodes();
2257 if portal_nodes.is_empty() {
2258 return;
2259 }
2260 for &pid in portal_nodes.iter() {
2261 let mut cur: Option<NodeId> = tree.get_node(pid).and_then(SceneNode::parent_id);
2262 while let Some(anc) = cur {
2263 if out_ancestors.contains(&anc) {
2264 break;
2265 }
2266 out_ancestors.push(anc);
2267 cur = tree.get_node(anc).and_then(SceneNode::parent_id);
2268 }
2269 }
2270 if out_ancestors.len() > 16 {
2271 out_ancestors.sort_unstable_by_key(|id: &NodeId| {
2272 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2273 });
2274 }
2275 }
2276
2277 #[inline]
2288 #[must_use]
2289 fn is_portal_ancestor(ancestors: &[NodeId], node_id: NodeId) -> bool {
2290 if ancestors.is_empty() {
2291 return false;
2292 }
2293 if ancestors.len() <= 16 {
2294 return ancestors.contains(&node_id);
2295 }
2296 let target: u64 = ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
2297 ancestors
2298 .binary_search_by_key(&target, |id: &NodeId| {
2299 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2300 })
2301 .is_ok()
2302 }
2303
2304 #[inline]
2320 #[must_use]
2321 fn conservative_self_vis_rect(
2322 node: &SceneNode,
2323 bounds: Rect,
2324 paint: &PaintStyle,
2325 focused: bool,
2326 total_off_phys: (f32, f32),
2327 scale: f32,
2328 inv_scale: f32,
2329 ) -> Rect {
2330 let mut min_x: f32 = bounds.get_x() - 1.0;
2331 let mut min_y: f32 = bounds.get_y() - 1.0;
2332 let mut max_x: f32 = bounds.right() + 1.0;
2333 let mut max_y: f32 = bounds.bottom() + 1.0;
2334 if let Some(shadow) = paint.box_shadow()
2335 && shadow.get_color().get_alpha() > 0
2336 {
2337 let spread: f32 = shadow.get_spread_radius().max(0.0);
2338 let blur_phys: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
2339 let sigma_phys: f32 = (blur_phys * 0.5).max(0.75);
2340 let blur_pad: f32 = (sigma_phys * 3.0).ceil() * inv_scale + 2.0 * inv_scale;
2341 let pad: f32 = spread + blur_pad;
2342 let sx0: f32 = bounds.get_x() + shadow.get_offset_x() - pad;
2343 let sy0: f32 = bounds.get_y() + shadow.get_offset_y() - pad;
2344 let sx1: f32 = bounds.right() + shadow.get_offset_x() + pad;
2345 let sy1: f32 = bounds.bottom() + shadow.get_offset_y() + pad;
2346 min_x = min_x.min(sx0);
2347 min_y = min_y.min(sy0);
2348 max_x = max_x.max(sx1);
2349 max_y = max_y.max(sy1);
2350 }
2351 if focused && let Some(ring) = paint.focus_ring_border() {
2352 let rw: f32 = ring.get_width().max(0.0) + 1.0;
2353 min_x = min_x.min(bounds.get_x() - rw);
2354 min_y = min_y.min(bounds.get_y() - rw);
2355 max_x = max_x.max(bounds.right() + rw);
2356 max_y = max_y.max(bounds.bottom() + rw);
2357 }
2358 if matches!(node.get_kind(), NodeKind::Text(_)) {
2359 let content_b: Rect = node.layout_box.get_content_bounds();
2360 let cx0: f32 = ((content_b.get_x() * scale).round() + total_off_phys.0) * inv_scale;
2361 let cy0: f32 = ((content_b.get_y() * scale).round() + total_off_phys.1) * inv_scale;
2362 let pad: f32 = node.style_ref().text.get_font_size() * 0.5 + 2.0;
2363 min_x = min_x.min(cx0 - pad);
2364 min_y = min_y.min(cy0 - pad);
2365 max_x = max_x.max(cx0 + content_b.get_width() + pad);
2366 max_y = max_y.max(cy0 + content_b.get_height() + pad);
2367 }
2368 Rect::new(
2369 min_x,
2370 min_y,
2371 (max_x - min_x).max(0.0),
2372 (max_y - min_y).max(0.0),
2373 )
2374 }
2375
2376 fn visit_culled_portal_child(
2385 tree: &SceneTree,
2386 child_id: NodeId,
2387 parent_opacity: f32,
2388 scratch: &mut PaintScratch<'_>,
2389 ) {
2390 let Some(child) = tree.get_node(child_id) else {
2391 return;
2392 };
2393 let style: &Style = child.style_ref();
2394 let layout: &LayoutStyle = &style.layout;
2395 if layout.get_display() == Display::None {
2396 return;
2397 }
2398 let is_portal: bool = matches!(child.get_kind(), NodeKind::Portal)
2399 || layout.get_position() == Position::Fixed;
2400 if is_portal {
2401 if let Some(portal_queue) = scratch.portals.as_deref_mut() {
2402 portal_queue.push((layout.get_z_index(), portal_queue.len(), child_id));
2403 }
2404 return;
2405 }
2406 if !Self::is_portal_ancestor(scratch.portal_ancestors, child_id) {
2407 return;
2408 }
2409 let hovered: bool = child.get_interaction().get_hovered();
2410 let paint_opacity: f32 = if hovered {
2411 child
2412 .hover_paint()
2413 .map(|hp: PaintStyle| hp.get_opacity())
2414 .unwrap_or_else(|| style.paint.get_opacity())
2415 } else {
2416 style.paint.get_opacity()
2417 };
2418 let next_opacity: f32 = (parent_opacity * paint_opacity).clamp(0.0, 1.0);
2419 if next_opacity <= 0.0 {
2420 return;
2421 }
2422 Self::collect_portals_in_culled_subtree(tree, child_id, next_opacity, scratch);
2423 }
2424
2425 fn collect_portals_in_culled_subtree(
2434 tree: &SceneTree,
2435 node_id: NodeId,
2436 parent_opacity: f32,
2437 scratch: &mut PaintScratch<'_>,
2438 ) {
2439 if tree.get_node(node_id).is_none() {
2440 return;
2441 }
2442 let child_count: usize = tree.child_count(node_id);
2443 if child_count == 0 {
2444 return;
2445 }
2446 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
2447 if !needs_z_sort {
2448 for child_id in tree.children_iter(node_id) {
2449 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2450 }
2451 } else {
2452 let sort_start: usize = scratch.z_sort.len();
2453 for (idx, cid) in tree.children_iter(node_id).enumerate() {
2454 if let Some(char_item) = tree.get_node(cid) {
2455 scratch
2456 .z_sort
2457 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
2458 }
2459 }
2460 let sort_end: usize = scratch.z_sort.len();
2461 scratch.z_sort[sort_start..sort_end].sort_by(
2462 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
2463 target_node_id
2464 .0
2465 .cmp(&element_id.0)
2466 .then_with(|| target_node_id.1.cmp(&element_id.1))
2467 },
2468 );
2469 for slot_idx in sort_start..sort_end {
2470 let child_id: NodeId = scratch.z_sort[slot_idx].2;
2471 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2472 }
2473 scratch.z_sort.truncate(sort_start);
2474 }
2475 }
2476
2477 fn build_inner(
2489 tree: &SceneTree,
2490 viewport: Size,
2491 scale_factor: f32,
2492 clear_color: Color,
2493 display_list: &mut DisplayList,
2494 mut fragments: Option<&mut PaintFragmentCache>,
2495 saw_canvas: &mut bool,
2496 ) {
2497 display_list.reset(viewport, scale_factor);
2498 display_list.push(DrawCommand::Clear(clear_color));
2499 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
2500 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
2501 let mut portals: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.portals_scratch);
2502 let mut z_sort: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.z_sort_scratch);
2503 let mut portal_ancestors: Vec<NodeId> =
2504 take(&mut display_list.arena.portal_ancestors_scratch);
2505 portals.clear();
2506 z_sort.clear();
2507 Self::collect_portal_ancestors(tree, &mut portal_ancestors);
2508 {
2509 let mut scratch: PaintScratch<'_> = PaintScratch {
2510 portals: Some(&mut portals),
2511 z_sort: &mut z_sort,
2512 portal_ancestors: portal_ancestors.as_slice(),
2513 fragments: fragments.as_deref_mut(),
2514 saw_canvas,
2515 };
2516 Self::emit_node(
2517 tree,
2518 tree.get_root(),
2519 (Point::new(0.0, 0.0), 1.0),
2520 viewport_rect,
2521 active_hover_target,
2522 &mut scratch,
2523 display_list,
2524 );
2525 }
2526 display_list.finalize_base_layer(display_list.len());
2527 if portals.len() > 1 {
2528 portals.sort_by(
2529 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
2530 target_node_id
2531 .0
2532 .cmp(&element_id.0)
2533 .then_with(|| target_node_id.1.cmp(&element_id.1))
2534 },
2535 );
2536 }
2537 {
2538 let mut portal_scratch: PaintScratch<'_> = PaintScratch {
2539 portals: None,
2540 z_sort: &mut z_sort,
2541 portal_ancestors: &[],
2542 fragments,
2543 saw_canvas,
2544 };
2545 for &(_, _, portal_id) in portals.iter() {
2546 Self::emit_node(
2547 tree,
2548 portal_id,
2549 (Point::new(0.0, 0.0), 1.0),
2550 viewport_rect,
2551 active_hover_target,
2552 &mut portal_scratch,
2553 display_list,
2554 );
2555 }
2556 }
2557 portals.clear();
2558 z_sort.clear();
2559 portal_ancestors.clear();
2560 display_list.arena.portals_scratch = portals;
2561 display_list.arena.z_sort_scratch = z_sort;
2562 display_list.arena.portal_ancestors_scratch = portal_ancestors;
2563 }
2564
2565 #[must_use]
2575 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
2576 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
2577 while let Some(id) = cursor {
2578 let Some(node) = tree.get_node(id) else {
2579 break;
2580 };
2581 let style: &Style = node.style_ref();
2582 let paint: &PaintStyle = &style.paint;
2583 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2584 || style.layout.get_position() == Position::Fixed;
2585 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2586 && paint.background_color().is_none();
2587 if !is_scrim && !is_transparent_canvas {
2588 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
2589 || node
2590 .get_handlers()
2591 .handler_for(&UiEventKind::Click(Point::default()))
2592 .is_some();
2593 if is_interactive {
2594 return Some(id);
2595 }
2596 }
2597 cursor = node.parent_id();
2598 }
2599 None
2600 }
2601
2602 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
2614 if !hovered && !pressed {
2615 return base;
2616 }
2617 let delta: i32 = if pressed { 24 } else { 14 };
2618 match base {
2619 Some(char_item) if char_item.get_alpha() > 0 => {
2620 let luma: u32 = (char_item.get_red() as u32) * 299
2621 + (char_item.get_green() as u32) * 587
2622 + (char_item.get_blue() as u32) * 114;
2623 if luma < 120_000 {
2624 let lift: i32 = if pressed { 30 } else { 20 };
2625 Some(Color::new(
2626 (char_item.get_red() as i32 + lift).clamp(0, 255) as u8,
2627 (char_item.get_green() as i32 + lift).clamp(0, 255) as u8,
2628 (char_item.get_blue() as i32 + lift).clamp(0, 255) as u8,
2629 char_item.get_alpha(),
2630 ))
2631 } else {
2632 Some(Color::new(
2633 (char_item.get_red() as i32 - delta).clamp(0, 255) as u8,
2634 (char_item.get_green() as i32 - delta).clamp(0, 255) as u8,
2635 (char_item.get_blue() as i32 - delta).clamp(0, 255) as u8,
2636 char_item.get_alpha(),
2637 ))
2638 }
2639 }
2640 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
2641 }
2642 }
2643
2644 fn emit_node(
2656 tree: &SceneTree,
2657 node_id: NodeId,
2658 offset_and_opacity: (Point, f32),
2659 cull_rect: Rect,
2660 active_hover_target: Option<NodeId>,
2661 scratch: &mut PaintScratch<'_>,
2662 display_list: &mut DisplayList,
2663 ) {
2664 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
2665 let Some(node) = tree.get_node(node_id) else {
2666 return;
2667 };
2668 let style: &Style = node.style_ref();
2669 let layout: &LayoutStyle = &style.layout;
2670 if layout.get_display() == Display::None {
2671 return;
2672 }
2673 let is_portal_or_fixed: bool =
2674 matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
2675 let is_root_chrome_overlay: bool = layout.get_position() == Position::Absolute
2676 && layout.get_z_index() >= 50
2677 && layout.get_z_index() < 140
2678 && offset_phys == Point::new(0.0, 0.0)
2679 && (parent_opacity - 1.0).abs() <= 0.001
2680 && cull_rect.get_x() <= 0.0
2681 && cull_rect.get_y() <= 0.0;
2682 let promote_to_top_layer: bool = is_portal_or_fixed || is_root_chrome_overlay;
2683 if let Some(portal_queue) = scratch.portals.as_deref_mut()
2684 && promote_to_top_layer
2685 {
2686 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
2687 return;
2688 }
2689 let interaction: InteractionState = node.get_interaction();
2690 let hovered: bool = interaction.get_hovered();
2691 let pressed: bool = interaction.get_pressed();
2692 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
2693 return;
2694 }
2695 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
2696 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2697 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
2698 if opacity <= 0.0 {
2699 return;
2700 }
2701 let scale: f32 = display_list.get_scale_factor().max(0.5);
2702 let inv_scale: f32 = 1.0 / scale;
2703 let raw_bounds: Rect = node.layout_box.get_bounds();
2704 let transform: Transform2D = prelim_paint.get_transform();
2705 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
2706 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
2707 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
2708 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
2709 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
2710 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
2711 let raw_x1: f32 = (raw_bounds.right() * scale).round();
2712 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
2713 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
2714 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
2715 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
2716 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
2717 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
2718 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
2719 let bounds: Rect = Rect::new(
2720 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
2721 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
2722 scaled_w_phys * inv_scale,
2723 scaled_h_phys * inv_scale,
2724 );
2725 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2726 let clipped_cull: Option<Rect> = if clipped {
2727 cull_rect.intersect(bounds)
2728 } else {
2729 Some(cull_rect)
2730 };
2731 let focused: bool = interaction.get_focused();
2732 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
2733 node,
2734 bounds,
2735 prelim_paint,
2736 focused,
2737 (total_off_x, total_off_y),
2738 scale,
2739 inv_scale,
2740 );
2741 let self_visible: bool = bounds.get_width() > 0.0
2742 && bounds.get_height() > 0.0
2743 && self_vis_rect.intersect(cull_rect).is_some();
2744 if clipped && clipped_cull.is_none() {
2745 if self_visible {
2746 if let Some(mut shadow) = prelim_paint.box_shadow() {
2747 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2748 display_list.push(DrawCommand::BoxShadow {
2749 rect: bounds,
2750 radius: prelim_paint.get_radius(),
2751 shadow,
2752 });
2753 }
2754 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
2755 ring.set_color(ring.get_color().multiply_alpha(opacity));
2756 let ring_w: f32 = ring.get_width();
2757 let ring_rect: Rect = Rect::new(
2758 bounds.get_x() - ring_w,
2759 bounds.get_y() - ring_w,
2760 bounds.get_width() + ring_w * 2.0,
2761 bounds.get_height() + ring_w * 2.0,
2762 );
2763 display_list.push(DrawCommand::FillRoundedRect {
2764 rect: ring_rect,
2765 color: COLOR_TRANSPARENT,
2766 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
2767 border: ring,
2768 });
2769 }
2770 }
2771 if scratch.portals.is_some()
2772 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
2773 {
2774 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
2775 }
2776 return;
2777 }
2778 let child_count: usize = tree.child_count(node_id);
2779 if !self_visible && child_count == 0 {
2780 return;
2781 }
2782 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
2783 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
2784 if self_visible
2785 && (hover_visual || pressed)
2786 && paint_owned.is_none()
2787 && (hovered || node.hover_paint().is_none())
2788 {
2789 let base_paint: &PaintStyle = &style.paint;
2790 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2791 || layout.get_position() == Position::Fixed;
2792 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2793 && base_paint.background_color().is_none();
2794 if !is_scrim && !is_transparent_canvas {
2795 let is_interactive: bool = base_paint.get_cursor() == CursorIcon::Pointer
2796 || node
2797 .get_handlers()
2798 .handler_for(&UiEventKind::Click(Point::default()))
2799 .is_some();
2800 if is_interactive {
2801 let mut tinted: PaintStyle = *base_paint;
2802 tinted.set_background(Self::interactive_tint(
2803 tinted.background_color(),
2804 hover_visual,
2805 pressed,
2806 ));
2807 paint_owned = Some(tinted);
2808 }
2809 }
2810 }
2811 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2812 let phys_scale_milli: u32 = Self::quantize_scale_milli(scale);
2813 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
2814 let frag_overflow_ok: bool = layout.get_overflow() == Overflow::Hidden
2815 || (layout.get_overflow() == Overflow::Visible
2816 && node_id != tree.get_root()
2817 && child_count >= 2);
2818 if self_visible
2819 && scratch.fragments.is_some()
2820 && scratch.portals.is_some()
2821 && matches!(node.get_kind(), NodeKind::View)
2822 && frag_overflow_ok
2823 && !hovered
2824 && !pressed
2825 && !interaction.get_focused()
2826 && active_hover_target != Some(node_id)
2827 {
2828 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
2829 && self_vis_rect.right() <= cull_rect.right();
2830 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
2831 && self_vis_rect.bottom() <= cull_rect.bottom();
2832 if vis_x_in && vis_y_in {
2833 let has_text_sel: bool = !tree.text_selection_spans_ref().is_empty();
2834 let unclipped_cull: Option<(Point, Rect, f32, f32)> =
2835 if layout.get_overflow() == Overflow::Hidden {
2836 None
2837 } else {
2838 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
2839 let child_off: Point = Point::new(
2840 total_off_x - (raw_node_scroll.get_x() * scale).round(),
2841 total_off_y - (raw_node_scroll.get_y() * scale).round(),
2842 );
2843 Some((child_off, cull_rect, scale, inv_scale))
2844 };
2845 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_subtree(
2846 tree,
2847 node_id,
2848 node,
2849 unclipped_cull,
2850 (active_hover_target, has_text_sel),
2851 ) {
2852 let slot_idx: usize =
2853 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
2854 let opacity_bits: u32 = parent_opacity.to_bits();
2855 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
2856 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
2857 if let Some(cache) = scratch.fragments.as_deref_mut()
2858 && let Some(&cached) = cache.slots.get(slot_idx)
2859 && cached.occupied
2860 && cached.node_id == node_id
2861 && cached.phys_scale_milli == phys_scale_milli
2862 && cached.opacity_bits == opacity_bits
2863 && cached.geom_hash == geom_hash
2864 && cached.style_text_hash == style_text_hash
2865 {
2866 let dx_phys: f32 = offset_phys.get_x() - cached.offset_phys.get_x();
2867 let dy_phys: f32 = offset_phys.get_y() - cached.offset_phys.get_y();
2868 let c_start: usize = cached.cmd_start as usize;
2869 let c_end: usize = c_start.saturating_add(cached.cmd_len as usize);
2870 if let Some(cmds) = cache.cmd_slab.get(c_start..c_end) {
2871 display_list.get_mut_commands().reserve(cmds.len());
2872 let text_slab: &[u8] = cache.text_slab.as_slice();
2873 for cmd in cmds.iter() {
2874 Self::push_translated_command(
2875 cmd,
2876 text_slab,
2877 (dx_phys, dy_phys),
2878 (scale, inv_scale, dyadic_scale),
2879 display_list,
2880 );
2881 }
2882 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
2883 return;
2884 }
2885 }
2886 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
2887 }
2888 }
2889 }
2890 let box_size: Size = display_list.get_viewport();
2891 let show_scrollbar: bool = box_size.get_width().min(box_size.get_height()) >= 540.0;
2892 let wants_scrollbar: bool = show_scrollbar
2893 && layout.get_overflow() == Overflow::Scroll
2894 && bounds.get_height() > 24.0;
2895 let emit_clip: bool = clipped && (child_count > 0 || wants_scrollbar);
2896 let has_canvas_clip: bool =
2897 matches!(node.get_kind(), NodeKind::Canvas(_)) && node.canvas_callback().is_some();
2898 let has_rounded_border: bool =
2899 paint.get_border().get_width() > 0.0 && paint.get_radius().max_radius() > 0.0;
2900 let defer_rounded_border: bool = has_rounded_border && (emit_clip || has_canvas_clip);
2901 if self_visible {
2902 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
2903 if blur_r > 0.0 {
2904 display_list.push(DrawCommand::BackdropBlur {
2905 rect: bounds,
2906 radius: paint.get_radius(),
2907 blur_radius: blur_r,
2908 });
2909 }
2910 if let Some(mut shadow) = paint.box_shadow() {
2911 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2912 display_list.push(DrawCommand::BoxShadow {
2913 rect: bounds,
2914 radius: paint.get_radius(),
2915 shadow,
2916 });
2917 }
2918 if let Some(mut gradient) = paint.background_gradient() {
2919 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
2920 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
2921 display_list.push(DrawCommand::LinearGradientRect {
2922 rect: bounds,
2923 gradient,
2924 radius: paint.get_radius(),
2925 });
2926 } else if let Some(bg) = paint.background_color() {
2927 let border: Border = if defer_rounded_border {
2928 Border::default()
2929 } else {
2930 let mut direct_border: Border = paint.get_border();
2931 direct_border.set_color(direct_border.get_color().multiply_alpha(opacity));
2932 direct_border
2933 };
2934 display_list.push(DrawCommand::FillRoundedRect {
2935 rect: bounds,
2936 color: bg.multiply_alpha(opacity),
2937 radius: paint.get_radius(),
2938 border,
2939 });
2940 } else if paint.get_border().get_width() > 0.0 && !defer_rounded_border {
2941 let mut border: Border = paint.get_border();
2942 border.set_color(border.get_color().multiply_alpha(opacity));
2943 display_list.push(DrawCommand::FillRoundedRect {
2944 rect: bounds,
2945 color: COLOR_TRANSPARENT,
2946 radius: paint.get_radius(),
2947 border,
2948 });
2949 }
2950 if interaction.get_focused()
2951 && let Some(mut ring) = paint.focus_ring_border()
2952 {
2953 ring.set_color(ring.get_color().multiply_alpha(opacity));
2954 let ring_w: f32 = ring.get_width();
2955 let ring_rect: Rect = Rect::new(
2956 bounds.get_x() - ring_w,
2957 bounds.get_y() - ring_w,
2958 bounds.get_width() + ring_w * 2.0,
2959 bounds.get_height() + ring_w * 2.0,
2960 );
2961 display_list.push(DrawCommand::FillRoundedRect {
2962 rect: ring_rect,
2963 color: COLOR_TRANSPARENT,
2964 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
2965 border: ring,
2966 });
2967 }
2968 match node.get_kind() {
2969 NodeKind::Text(text) => {
2970 let mut text_style: TextStyle = style.text;
2971 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2972 let content_b: Rect = node.layout_box.get_content_bounds();
2973 let cx0: f32 = (content_b.get_x() * scale).round();
2974 let cy0: f32 = (content_b.get_y() * scale).round();
2975 let shifted_content: Rect = Rect::new(
2976 (cx0 + total_off_x) * inv_scale,
2977 (cy0 + total_off_y) * inv_scale,
2978 content_b.get_width(),
2979 content_b.get_height(),
2980 );
2981 let has_box_chrome: bool = style
2982 .paint
2983 .background_color()
2984 .is_some_and(|color_value: Color| color_value.get_alpha() > 0)
2985 || style.paint.background_gradient().is_some()
2986 || style.paint.get_border().get_width() > 0.0
2987 || style.layout.get_height() != Length::Auto
2988 || style.layout.get_min_height() != Length::Auto;
2989 let text_run_rect: Rect = if has_box_chrome
2990 && shifted_content.get_height() > 1.0
2991 {
2992 let cached_h: f32 = node.layout_box.get_content_size().get_height();
2993 let measured_h: f32 =
2994 if cached_h > 0.0 && cached_h + 0.5 < shifted_content.get_height() {
2995 cached_h
2996 } else {
2997 DeterministicTextMeasurer
2998 .measure_size(
2999 text.as_str(),
3000 text_style,
3001 Some(shifted_content.get_width().max(1.0)),
3002 )
3003 .get_height()
3004 };
3005 if measured_h > 0.0 && shifted_content.get_height() > measured_h + 0.5 {
3006 let v_off_phys: f32 =
3007 (((shifted_content.get_height() - measured_h) * 0.5).max(0.0)
3008 * scale)
3009 .round();
3010 Rect::new(
3011 shifted_content.get_x(),
3012 (cy0 + total_off_y + v_off_phys) * inv_scale,
3013 shifted_content.get_width(),
3014 measured_h,
3015 )
3016 } else {
3017 shifted_content
3018 }
3019 } else {
3020 shifted_content
3021 };
3022 if let Some(sel_range) = tree.text_selection_range_for(node_id) {
3023 let sel_color: Color = tree
3024 .effective_selection_color(node_id)
3025 .multiply_alpha(opacity);
3026 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3027 text.as_str(),
3028 text_style,
3029 text_run_rect,
3030 sel_range,
3031 );
3032 let sel_single_to_end: bool = sel_rects.len() == 1
3033 && text_style.get_align() == TextAlign::Left
3034 && sel_range.1 >= text.chars().count();
3035 let exact_span_right: f32 =
3036 ((content_b.right() * scale).round() + total_off_x) * inv_scale;
3037 for sel_rect in sel_rects.into_iter() {
3038 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3039 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3040 let sx1: f32 = if sel_single_to_end {
3041 exact_span_right.max(sx0 + inv_scale)
3042 } else {
3043 (sel_rect.right() * scale).round() * inv_scale
3044 };
3045 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3046 display_list.push(DrawCommand::FillRoundedRect {
3047 rect: Rect::new(
3048 sx0,
3049 sy0,
3050 (sx1 - sx0).max(inv_scale),
3051 (sy1 - sy0).max(inv_scale),
3052 ),
3053 color: sel_color,
3054 radius: CornerRadius::default(),
3055 border: Border::default(),
3056 });
3057 }
3058 }
3059 let pooled_text: String = display_list.alloc_text(text.as_str());
3060 display_list.push(DrawCommand::TextRun {
3061 rect: text_run_rect,
3062 text: pooled_text,
3063 style: text_style,
3064 });
3065 }
3066 NodeKind::Input(input_state) => {
3067 let mut text_style: TextStyle = style.text;
3068 let is_empty_value: bool = input_state.get_value().is_empty();
3069 let is_focused: bool = interaction.get_focused();
3070 let rendered_text: String = if is_empty_value {
3071 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
3072 display_list.alloc_text(input_state.get_placeholder().as_str())
3073 } else if input_state.get_password() {
3074 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3075 let star_count: usize = input_state.get_value().chars().count();
3076 let mut masked: String = display_list.alloc_text("");
3077 for _ in 0..star_count {
3078 masked.push('*');
3079 }
3080 masked
3081 } else {
3082 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3083 display_list.alloc_text(input_state.get_value().as_str())
3084 };
3085 let content_b: Rect = node.layout_box.get_content_bounds();
3086 let cx0: f32 = (content_b.get_x() * scale).round();
3087 let cy0: f32 = (content_b.get_y() * scale).round();
3088 let shifted_content: Rect = Rect::new(
3089 (cx0 + total_off_x) * inv_scale,
3090 (cy0 + total_off_y) * inv_scale,
3091 content_b.get_width(),
3092 content_b.get_height(),
3093 );
3094 let line_h: f32 =
3095 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
3096 let text_rect: Rect = if input_state.get_multiline() {
3097 shifted_content
3098 } else {
3099 text_style.set_overflow(TextOverflow::Clip);
3100 let v_off_phys: f32 =
3101 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
3102 .round();
3103 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
3104 Rect::new(
3105 shifted_content.get_x(),
3106 text_y,
3107 shifted_content.get_width(),
3108 line_h,
3109 )
3110 };
3111 let run_rect: Rect =
3112 if is_empty_value && is_focused && !rendered_text.is_empty() {
3113 let clearance_phys: f32 =
3114 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
3115 let clearance_logical: f32 = clearance_phys * inv_scale;
3116 Rect::new(
3117 text_rect.get_x() + clearance_logical,
3118 text_rect.get_y(),
3119 (text_rect.get_width() - clearance_logical).max(0.0),
3120 text_rect.get_height(),
3121 )
3122 } else {
3123 text_rect
3124 };
3125 let show_caret: bool = is_focused && tree.get_caret_visible();
3126 let caret_text: String = if show_caret && !is_empty_value {
3127 let caret_prefix: &str =
3128 input_state.caret_prefix_for_display(rendered_text.as_str());
3129 display_list.alloc_text(caret_prefix)
3130 } else {
3131 String::new()
3132 };
3133 display_list.push(DrawCommand::PushClip(shifted_content));
3134 if is_focused
3135 && !is_empty_value
3136 && let Some(sel_range) = input_state.selection_char_range()
3137 {
3138 let sel_color: Color = tree
3139 .effective_selection_color(node_id)
3140 .multiply_alpha(opacity);
3141 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3142 rendered_text.as_str(),
3143 text_style,
3144 text_rect,
3145 sel_range,
3146 );
3147 for sel_rect in sel_rects.into_iter() {
3148 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3149 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3150 let sx1: f32 = (sel_rect.right() * scale).round() * inv_scale;
3151 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3152 display_list.push(DrawCommand::FillRoundedRect {
3153 rect: Rect::new(
3154 sx0,
3155 sy0,
3156 (sx1 - sx0).max(inv_scale),
3157 (sy1 - sy0).max(inv_scale),
3158 ),
3159 color: sel_color,
3160 radius: CornerRadius::default(),
3161 border: Border::default(),
3162 });
3163 }
3164 }
3165 display_list.push(DrawCommand::TextRun {
3166 rect: run_rect,
3167 text: rendered_text,
3168 style: text_style,
3169 });
3170 if show_caret {
3171 let caret_h: f32 = (text_style.get_font_size() * 1.15)
3172 .min(shifted_content.get_height().max(12.0));
3173 display_list.push(DrawCommand::TextCaret {
3174 text_rect,
3175 content_rect: shifted_content,
3176 text: caret_text,
3177 style: text_style,
3178 caret_height: caret_h,
3179 color: style.text.get_color().multiply_alpha(opacity),
3180 multiline: input_state.get_multiline(),
3181 });
3182 }
3183 display_list.push(DrawCommand::PopClip);
3184 }
3185 NodeKind::Image(buffer) => {
3186 display_list.push(DrawCommand::Image {
3187 rect: bounds,
3188 buffer: buffer.clone(),
3189 radius: paint.get_radius(),
3190 });
3191 }
3192 NodeKind::Canvas(caching) => {
3193 if let Some(callback) = node.canvas_callback()
3194 && let Ok(mut func) = callback.try_borrow_mut()
3195 {
3196 let canvas_bw: f32 = paint.get_border().get_width();
3197 let canvas_inset: f32 = if canvas_bw > 0.0 {
3198 ((canvas_bw * scale).ceil().max(1.0)) * inv_scale
3199 } else {
3200 0.0
3201 };
3202 let canvas_clip_rect: Rect = if canvas_inset > 0.0 {
3203 Rect::new(
3204 bounds.get_x() + canvas_inset,
3205 bounds.get_y() + canvas_inset,
3206 (bounds.get_width() - canvas_inset * 2.0).max(0.0),
3207 (bounds.get_height() - canvas_inset * 2.0).max(0.0),
3208 )
3209 } else {
3210 bounds
3211 };
3212 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
3213 .min(bounds.get_height() * 0.5)
3214 .max(0.0);
3215 let canvas_raw_r: CornerRadius = paint.get_radius();
3216 let canvas_inner_r: CornerRadius = CornerRadius::new(
3217 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_inset).max(0.0),
3218 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_inset)
3219 .max(0.0),
3220 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_inset)
3221 .max(0.0),
3222 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_inset)
3223 .max(0.0),
3224 );
3225 if canvas_inner_r.max_radius() > 0.25 {
3226 display_list.push(DrawCommand::PushRoundedClip {
3227 rect: canvas_clip_rect,
3228 radius: canvas_inner_r,
3229 });
3230 } else {
3231 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
3232 }
3233 let canvas_cmd_start: usize = display_list.len();
3234 func(bounds, display_list);
3235 let canvas_cmd_end: usize = display_list.len();
3236 if opacity < 0.999 {
3237 display_list.attenuate_range_opacity(
3238 canvas_cmd_start,
3239 canvas_cmd_end,
3240 opacity,
3241 );
3242 }
3243 if *caching != CanvasCaching::RetainedStatic {
3244 let canvas_is_dynamic: bool =
3245 if let Some(cache) = scratch.fragments.as_deref_mut() {
3246 let emitted: &[DrawCommand] = &display_list.get_commands()
3247 [canvas_cmd_start..canvas_cmd_end];
3248 cache.record_canvas_output(
3249 node_id,
3250 phys_scale_milli,
3251 bounds,
3252 tree.get_revision(),
3253 emitted,
3254 )
3255 } else {
3256 true
3257 };
3258 if canvas_is_dynamic {
3259 *scratch.saw_canvas = true;
3260 }
3261 }
3262 display_list.push(DrawCommand::PopClip);
3263 if canvas_bw > 0.0 && canvas_raw_r.max_radius() > 0.0 {
3264 let mut canvas_border: Border = paint.get_border();
3265 canvas_border
3266 .set_color(canvas_border.get_color().multiply_alpha(opacity));
3267 display_list.push(DrawCommand::FillRoundedRect {
3268 rect: bounds,
3269 color: COLOR_TRANSPARENT,
3270 radius: canvas_raw_r,
3271 border: canvas_border,
3272 });
3273 }
3274 } else if *caching == CanvasCaching::DynamicPerFrame {
3275 *scratch.saw_canvas = true;
3276 }
3277 }
3278 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
3279 }
3280 }
3281 let clip_push_idx: usize = display_list.len();
3282 let clip_saved_digest: (u64, u64) = display_list.snapshot_digest_state();
3283 if emit_clip {
3284 let border_width: f32 = paint.get_border().get_width();
3285 let border_inset: f32 = if border_width > 0.0 {
3286 ((border_width * scale).ceil().max(1.0)) * inv_scale
3287 } else {
3288 0.0
3289 };
3290 let clip_rect: Rect = if border_inset > 0.0 {
3291 Rect::new(
3292 bounds.get_x() + border_inset,
3293 bounds.get_y() + border_inset,
3294 (bounds.get_width() - border_inset * 2.0).max(0.0),
3295 (bounds.get_height() - border_inset * 2.0).max(0.0),
3296 )
3297 } else {
3298 bounds
3299 };
3300 let max_r: f32 = (bounds.get_width() * 0.5)
3301 .min(bounds.get_height() * 0.5)
3302 .max(0.0);
3303 let raw_r: CornerRadius = paint.get_radius();
3304 let inner_r: CornerRadius = CornerRadius::new(
3305 (raw_r.get_top_left().min(max_r) - border_inset).max(0.0),
3306 (raw_r.get_top_right().min(max_r) - border_inset).max(0.0),
3307 (raw_r.get_bottom_right().min(max_r) - border_inset).max(0.0),
3308 (raw_r.get_bottom_left().min(max_r) - border_inset).max(0.0),
3309 );
3310 if inner_r.max_radius() > 0.25 {
3311 display_list.push(DrawCommand::PushRoundedClip {
3312 rect: clip_rect,
3313 radius: inner_r,
3314 });
3315 } else {
3316 display_list.push(DrawCommand::PushClip(clip_rect));
3317 }
3318 }
3319 if child_count > 0 {
3320 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3321 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
3322 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
3323 let child_offset_phys: Point =
3324 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
3325 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
3326 if !needs_z_sort {
3327 for child_id in tree.children_iter(node_id) {
3328 Self::emit_node(
3329 tree,
3330 child_id,
3331 (child_offset_phys, opacity),
3332 next_cull,
3333 active_hover_target,
3334 scratch,
3335 display_list,
3336 );
3337 }
3338 } else {
3339 let sort_start: usize = scratch.z_sort.len();
3340 for (idx, cid) in tree.children_iter(node_id).enumerate() {
3341 if let Some(char_item) = tree.get_node(cid) {
3342 scratch
3343 .z_sort
3344 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
3345 }
3346 }
3347 let sort_end: usize = scratch.z_sort.len();
3348 scratch.z_sort[sort_start..sort_end].sort_by(
3349 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
3350 target_node_id
3351 .0
3352 .cmp(&element_id.0)
3353 .then_with(|| target_node_id.1.cmp(&element_id.1))
3354 },
3355 );
3356 for slot_idx in sort_start..sort_end {
3357 let child_id: NodeId = scratch.z_sort[slot_idx].2;
3358 Self::emit_node(
3359 tree,
3360 child_id,
3361 (child_offset_phys, opacity),
3362 next_cull,
3363 active_hover_target,
3364 scratch,
3365 display_list,
3366 );
3367 }
3368 scratch.z_sort.truncate(sort_start);
3369 }
3370 }
3371 if wants_scrollbar {
3372 let computed_box: &LayoutBox = &node.layout_box;
3373 let border_w: f32 = paint.get_border().get_width();
3374 let inner_x: f32 = bounds.get_x() + border_w;
3375 let inner_y: f32 = bounds.get_y() + border_w;
3376 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
3377 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
3378 let view_w: f32 = computed_box.get_content_bounds().get_width().max(1.0);
3379 let view_h: f32 = computed_box.get_content_bounds().get_height().max(1.0);
3380 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
3381 Some((vy, vh)) => (vy, vh),
3382 None => (
3383 computed_box.get_scroll_offset().get_y(),
3384 computed_box.get_content_size().get_height(),
3385 ),
3386 };
3387 let scroll_x: f32 = computed_box.get_scroll_offset().get_x();
3388 let total_w: f32 = computed_box.get_content_size().get_width();
3389 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
3390 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
3391 let has_v_bar: bool = max_scroll_y > 1.0;
3392 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
3393 let corner_inset: f32 = if has_v_bar && has_h_bar { 6.0 } else { 0.0 };
3394 if has_v_bar {
3395 let track_margin: f32 = 3.0;
3396 let edge_inset: f32 = 1.0;
3397 let thumb_w: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3398 let track_x: f32 = (inner_x + inner_w - thumb_w - edge_inset).max(inner_x);
3399 let track_y: f32 = inner_y + track_margin;
3400 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
3401 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
3402 let min_thumb_h: f32 = 20.0_f32.min(track_h);
3403 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
3404 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
3405 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
3406 let thumb_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3407 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3408 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
3409 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3410 let thumb_alpha: u8 = if pressed {
3411 188
3412 } else if hovered {
3413 148
3414 } else {
3415 88
3416 };
3417 display_list.push(DrawCommand::FillRoundedRect {
3418 rect: Rect::new(
3419 thumb_x_snapped,
3420 thumb_y_snapped,
3421 thumb_w_snapped,
3422 thumb_h_snapped,
3423 ),
3424 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3425 radius: CornerRadius::all(thumb_w * 0.5),
3426 border: Border::default(),
3427 });
3428 }
3429 if has_h_bar {
3430 let track_margin: f32 = 3.0;
3431 let edge_inset: f32 = 1.0;
3432 let thumb_h: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3433 let track_y: f32 = (inner_y + inner_h - thumb_h - edge_inset).max(inner_y);
3434 let track_x: f32 = inner_x + track_margin;
3435 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
3436 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
3437 let min_thumb_w: f32 = 20.0_f32.min(track_w);
3438 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
3439 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
3440 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
3441 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
3442 let thumb_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3443 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3444 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3445 let thumb_alpha: u8 = if pressed {
3446 188
3447 } else if hovered {
3448 148
3449 } else {
3450 88
3451 };
3452 display_list.push(DrawCommand::FillRoundedRect {
3453 rect: Rect::new(
3454 thumb_x_snapped,
3455 thumb_y_snapped,
3456 thumb_w_snapped,
3457 thumb_h_snapped,
3458 ),
3459 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3460 radius: CornerRadius::all(thumb_h * 0.5),
3461 border: Border::default(),
3462 });
3463 }
3464 }
3465 if emit_clip {
3466 if display_list.len() == clip_push_idx + 1 {
3467 display_list.pop_empty_clip(clip_saved_digest);
3468 if self_visible && defer_rounded_border && !has_canvas_clip {
3469 let mut outer_border: Border = paint.get_border();
3470 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3471 display_list.push(DrawCommand::FillRoundedRect {
3472 rect: bounds,
3473 color: COLOR_TRANSPARENT,
3474 radius: paint.get_radius(),
3475 border: outer_border,
3476 });
3477 }
3478 } else {
3479 display_list.push(DrawCommand::PopClip);
3480 if self_visible && has_rounded_border && !has_canvas_clip {
3481 let mut outer_border: Border = paint.get_border();
3482 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3483 display_list.push(DrawCommand::FillRoundedRect {
3484 rect: bounds,
3485 color: COLOR_TRANSPARENT,
3486 radius: paint.get_radius(),
3487 border: outer_border,
3488 });
3489 }
3490 }
3491 }
3492 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
3493 && let Some(cache) = scratch.fragments.as_deref_mut()
3494 {
3495 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
3496 cache.capture_fragment(
3497 slot_idx,
3498 (
3499 node_id,
3500 phys_scale_milli,
3501 parent_opacity.to_bits(),
3502 offset_phys,
3503 geom_hash,
3504 style_text_hash,
3505 ),
3506 cmd_slice,
3507 );
3508 }
3509 }
3510}