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: vp,
132 scale_factor: sf,
133 portal_split_index,
134 arena,
135 } = self;
136 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
137 arena
138 .string_pool
139 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
140 }
141 let pool_start: usize = arena.string_pool.len();
142 for cmd in commands.drain(..) {
143 match cmd {
144 DrawCommand::TextRun { mut text, .. } | DrawCommand::TextCaret { mut text, .. }
145 if text.capacity() > 0
146 && arena.string_pool.len() < DISPLAY_LIST_STRING_POOL_CAP =>
147 {
148 text.clear();
149 arena.string_pool.push(text);
150 }
151 _ => {}
152 }
153 }
154 if arena.string_pool.len() > pool_start + 1 {
155 arena.string_pool[pool_start..].reverse();
156 }
157 *vp = viewport;
158 *sf = safe_scale;
159 *portal_split_index = 0;
160 let mut seed: u64 = FNV1A_OFFSET_BASIS;
161 Self::mix_hash(&mut seed, viewport.get_width().to_bits() as u64);
162 Self::mix_hash(&mut seed, viewport.get_height().to_bits() as u64);
163 Self::mix_hash(&mut seed, safe_scale.to_bits() as u64);
164 arena.prev_base_digest = arena.cur_base_digest;
165 arena.cur_base_digest = seed;
166 arena.running_fast_hash = seed;
167 arena.base_layer_finalized = false;
168 }
169
170 #[inline]
181 #[must_use]
182 pub(crate) fn alloc_text(&mut self, src: &str) -> String {
183 if src.is_empty() {
184 return String::new();
185 }
186 let Self { arena, .. } = self;
187 let pool: &mut Vec<String> = &mut arena.string_pool;
188 let len: usize = pool.len();
189 if len > 0 {
190 let need: usize = src.len();
191 if pool[len - 1].capacity() >= need
192 && let Some(mut reused) = pool.pop()
193 {
194 reused.clear();
195 reused.push_str(src);
196 return reused;
197 }
198 for idx in (0..len.saturating_sub(1)).rev() {
199 if pool[idx].capacity() >= need {
200 let mut reused: String = pool.swap_remove(idx);
201 reused.clear();
202 reused.push_str(src);
203 return reused;
204 }
205 }
206 if let Some(mut reused) = pool.pop() {
207 reused.clear();
208 reused.push_str(src);
209 return reused;
210 }
211 }
212 let mut fresh: String = String::with_capacity(src.len().max(128));
213 fresh.push_str(src);
214 fresh
215 }
216
217 pub fn push(&mut self, command: DrawCommand) {
224 let Self {
225 commands, arena, ..
226 } = self;
227 Self::fold_command_fast(&mut arena.running_fast_hash, &command);
228 if !arena.base_layer_finalized {
229 Self::fold_command_full(&mut arena.cur_base_digest, &command);
230 }
231 commands.push(command);
232 }
233
234 #[inline]
244 #[must_use]
245 pub(crate) fn snapshot_digest_state(&self) -> (u64, u64) {
246 let Self { arena, .. } = self;
247 (arena.running_fast_hash, arena.cur_base_digest)
248 }
249
250 #[inline]
257 pub(crate) fn pop_empty_clip(&mut self, saved: (u64, u64)) {
258 let Self {
259 commands, arena, ..
260 } = self;
261 let _: Option<DrawCommand> = commands.pop();
262 arena.running_fast_hash = saved.0;
263 arena.cur_base_digest = saved.1;
264 }
265
266 #[inline]
273 pub(crate) fn finalize_base_layer(&mut self, split_index: usize) {
274 let Self {
275 portal_split_index,
276 arena,
277 ..
278 } = self;
279 *portal_split_index = split_index;
280 Self::mix_hash(&mut arena.cur_base_digest, split_index as u64);
281 if arena.base_layer_version == 0 || arena.cur_base_digest != arena.prev_base_digest {
282 arena.base_layer_version = arena.base_layer_version.wrapping_add(1).max(1);
283 }
284 arena.base_layer_finalized = true;
285 }
286
287 #[inline]
297 #[must_use]
298 pub fn base_layer_version(&self) -> u64 {
299 let Self { arena, .. } = self;
300 arena.base_layer_version
301 }
302
303 #[must_use]
313 pub fn len(&self) -> usize {
314 self.get_commands().len()
315 }
316
317 #[must_use]
327 pub fn is_empty(&self) -> bool {
328 self.get_commands().is_empty()
329 }
330
331 #[inline(always)]
338 fn mix_hash(hash: &mut u64, value: u64) {
339 *hash ^= value;
340 *hash = hash.wrapping_mul(FNV1A_PRIME);
341 }
342
343 #[inline]
350 fn fold_command_fast(hash: &mut u64, cmd: &DrawCommand) {
351 match cmd {
352 DrawCommand::Clear(c) => {
353 Self::mix_hash(hash, 1);
354 Self::mix_hash(hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
355 }
356 DrawCommand::FillRoundedRect {
357 rect,
358 color,
359 border,
360 ..
361 } => {
362 Self::mix_hash(hash, 2);
363 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
364 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
365 Self::mix_hash(hash, rect.get_width().to_bits() as u64);
366 Self::mix_hash(hash, rect.get_height().to_bits() as u64);
367 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
368 Self::mix_hash(hash, border.get_width().to_bits() as u64);
369 }
370 DrawCommand::LinearGradientRect { rect, gradient, .. } => {
371 Self::mix_hash(hash, 3);
372 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
373 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
374 Self::mix_hash(
375 hash,
376 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
377 );
378 }
379 DrawCommand::BoxShadow { rect, shadow, .. } => {
380 Self::mix_hash(hash, 4);
381 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
382 Self::mix_hash(hash, shadow.get_blur_radius().to_bits() as u64);
383 }
384 DrawCommand::BackdropBlur {
385 rect, blur_radius, ..
386 } => {
387 Self::mix_hash(hash, 15);
388 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
389 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
390 Self::mix_hash(hash, blur_radius.to_bits() as u64);
391 }
392 DrawCommand::TextRun { rect, text, style } => {
393 Self::mix_hash(hash, 5);
394 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
395 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
396 Self::mix_hash(hash, text.len() as u64);
397 let bytes: &[u8] = text.as_bytes();
398 let prefix_len: usize = bytes.len().min(16);
399 for &byte in bytes[..prefix_len].iter() {
400 Self::mix_hash(hash, byte as u64);
401 }
402 Self::mix_hash(
403 hash,
404 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
405 );
406 }
407 DrawCommand::TextCaret {
408 text_rect,
409 text,
410 color,
411 ..
412 } => {
413 Self::mix_hash(hash, 13);
414 Self::mix_hash(hash, text_rect.get_x().to_bits() as u64);
415 Self::mix_hash(hash, text_rect.get_y().to_bits() as u64);
416 Self::mix_hash(hash, text.len() as u64);
417 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
418 }
419 DrawCommand::Image { rect, buffer, .. } => {
420 Self::mix_hash(hash, 6);
421 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
422 Self::mix_hash(hash, buffer.get_width() as u64);
423 Self::mix_hash(hash, buffer.get_height() as u64);
424 }
425 DrawCommand::SharedImage {
426 rect,
427 width,
428 height,
429 ..
430 } => {
431 Self::mix_hash(hash, 6);
432 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
433 Self::mix_hash(hash, *width as u64);
434 Self::mix_hash(hash, *height as u64);
435 }
436 DrawCommand::Line {
437 from,
438 to,
439 color,
440 width,
441 } => {
442 Self::mix_hash(hash, 7);
443 Self::mix_hash(hash, from.get_x().to_bits() as u64);
444 Self::mix_hash(hash, to.get_y().to_bits() as u64);
445 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
446 Self::mix_hash(hash, width.to_bits() as u64);
447 }
448 DrawCommand::Circle {
449 center,
450 radius,
451 color,
452 } => {
453 Self::mix_hash(hash, 8);
454 Self::mix_hash(hash, center.get_x().to_bits() as u64);
455 Self::mix_hash(hash, center.get_y().to_bits() as u64);
456 Self::mix_hash(hash, radius.to_bits() as u64);
457 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
458 }
459 DrawCommand::Triangle { p0, p1, p2, color } => {
460 Self::mix_hash(hash, 9);
461 Self::mix_hash(hash, p0.get_x().to_bits() as u64);
462 Self::mix_hash(hash, p1.get_y().to_bits() as u64);
463 Self::mix_hash(hash, p2.get_x().to_bits() as u64);
464 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
465 }
466 DrawCommand::SpriteRegion {
467 rect,
468 src_rect,
469 tint,
470 ..
471 } => {
472 Self::mix_hash(hash, 10);
473 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
474 Self::mix_hash(hash, src_rect.get_x().to_bits() as u64);
475 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
476 }
477 DrawCommand::PushClip(r) => {
478 Self::mix_hash(hash, 11);
479 Self::mix_hash(hash, r.get_x().to_bits() as u64);
480 Self::mix_hash(hash, r.get_y().to_bits() as u64);
481 }
482 DrawCommand::PushRoundedClip { rect, radius } => {
483 Self::mix_hash(hash, 14);
484 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
485 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
486 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
487 }
488 DrawCommand::PopClip => {
489 Self::mix_hash(hash, 12);
490 }
491 }
492 }
493
494 #[inline]
501 fn fold_bytes_wordwise(hash: &mut u64, bytes: &[u8]) {
502 Self::mix_hash(hash, bytes.len() as u64);
503 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
504 for chunk in chunks.iter() {
505 Self::mix_hash(hash, u64::from_ne_bytes(*chunk));
506 }
507 if !rem.is_empty() {
508 let mut tail: u64 = 0;
509 for (idx, &b) in rem.iter().enumerate() {
510 tail |= (b as u64) << (idx * 8);
511 }
512 Self::mix_hash(hash, tail);
513 }
514 }
515
516 #[inline]
523 pub(crate) fn fold_command_full(hash: &mut u64, cmd: &DrawCommand) {
524 match cmd {
525 DrawCommand::Clear(c) => {
526 Self::mix_hash(hash, 1);
527 Self::mix_hash(hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
528 }
529 DrawCommand::FillRoundedRect {
530 rect,
531 color,
532 radius,
533 border,
534 } => {
535 Self::mix_hash(hash, 2);
536 Self::mix_hash(
537 hash,
538 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
539 );
540 Self::mix_hash(
541 hash,
542 ((rect.get_width().to_bits() as u64) << 32)
543 | (rect.get_height().to_bits() as u64),
544 );
545 Self::mix_hash(
546 hash,
547 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
548 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64),
549 );
550 Self::mix_hash(
551 hash,
552 ((radius.get_top_left().to_bits() as u64) << 32)
553 | (radius.get_top_right().to_bits() as u64),
554 );
555 Self::mix_hash(
556 hash,
557 ((radius.get_bottom_right().to_bits() as u64) << 32)
558 | (radius.get_bottom_left().to_bits() as u64),
559 );
560 Self::mix_hash(hash, border.get_width().to_bits() as u64);
561 }
562 DrawCommand::LinearGradientRect {
563 rect,
564 gradient,
565 radius,
566 } => {
567 Self::mix_hash(hash, 3);
568 Self::mix_hash(
569 hash,
570 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
571 );
572 Self::mix_hash(
573 hash,
574 ((rect.get_width().to_bits() as u64) << 32)
575 | (rect.get_height().to_bits() as u64),
576 );
577 Self::mix_hash(
578 hash,
579 ((u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64) << 32)
580 | (u32::from_le_bytes(gradient.get_end_color().to_rgba_bytes()) as u64),
581 );
582 Self::mix_hash(
583 hash,
584 ((gradient.get_angle_deg().to_bits() as u64) << 32)
585 | (radius.max_radius().to_bits() as u64),
586 );
587 }
588 DrawCommand::BoxShadow {
589 rect,
590 radius,
591 shadow,
592 } => {
593 Self::mix_hash(hash, 4);
594 Self::mix_hash(
595 hash,
596 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
597 );
598 Self::mix_hash(
599 hash,
600 ((rect.get_width().to_bits() as u64) << 32)
601 | (rect.get_height().to_bits() as u64),
602 );
603 Self::mix_hash(
604 hash,
605 ((shadow.get_offset_x().to_bits() as u64) << 32)
606 | (shadow.get_offset_y().to_bits() as u64),
607 );
608 Self::mix_hash(
609 hash,
610 ((shadow.get_blur_radius().to_bits() as u64) << 32)
611 | (shadow.get_spread_radius().to_bits() as u64),
612 );
613 Self::mix_hash(
614 hash,
615 ((radius.max_radius().to_bits() as u64) << 32)
616 | (u32::from_le_bytes(shadow.get_color().to_rgba_bytes()) as u64),
617 );
618 }
619 DrawCommand::BackdropBlur {
620 rect,
621 radius,
622 blur_radius,
623 } => {
624 Self::mix_hash(hash, 15);
625 Self::mix_hash(
626 hash,
627 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
628 );
629 Self::mix_hash(
630 hash,
631 ((rect.get_width().to_bits() as u64) << 32)
632 | (rect.get_height().to_bits() as u64),
633 );
634 Self::mix_hash(
635 hash,
636 ((radius.max_radius().to_bits() as u64) << 32) | (blur_radius.to_bits() as u64),
637 );
638 }
639 DrawCommand::TextRun { rect, text, style } => {
640 Self::mix_hash(hash, 5);
641 Self::mix_hash(
642 hash,
643 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
644 );
645 Self::mix_hash(
646 hash,
647 ((rect.get_width().to_bits() as u64) << 32)
648 | (rect.get_height().to_bits() as u64),
649 );
650 let style_flags: u64 = (style.get_family() as u64)
651 | ((style.get_weight() as u64) << 8)
652 | ((style.get_align() as u64) << 16)
653 | (u64::from(style.get_italic()) << 24)
654 | ((u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64) << 32);
655 Self::mix_hash(hash, style_flags);
656 Self::mix_hash(hash, style.get_font_size().to_bits() as u64);
657 Self::fold_bytes_wordwise(hash, text.as_bytes());
658 }
659 DrawCommand::TextCaret {
660 text_rect,
661 content_rect,
662 text,
663 style,
664 caret_height,
665 color,
666 multiline,
667 } => {
668 Self::mix_hash(hash, 13);
669 Self::mix_hash(
670 hash,
671 ((text_rect.get_x().to_bits() as u64) << 32)
672 | (text_rect.get_y().to_bits() as u64),
673 );
674 Self::mix_hash(
675 hash,
676 ((content_rect.get_width().to_bits() as u64) << 32)
677 | (caret_height.to_bits() as u64),
678 );
679 Self::mix_hash(
680 hash,
681 ((style.get_font_size().to_bits() as u64) << 32)
682 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64)
683 ^ u64::from(*multiline),
684 );
685 Self::fold_bytes_wordwise(hash, text.as_bytes());
686 }
687 DrawCommand::Image {
688 rect,
689 buffer,
690 radius,
691 } => {
692 Self::mix_hash(hash, 6);
693 Self::mix_hash(
694 hash,
695 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
696 );
697 Self::mix_hash(
698 hash,
699 ((rect.get_width().to_bits() as u64) << 32)
700 | (rect.get_height().to_bits() as u64),
701 );
702 Self::mix_hash(
703 hash,
704 ((buffer.get_width() as u64) << 32) | (buffer.get_height() as u64),
705 );
706 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
707 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
708 }
709 DrawCommand::SharedImage {
710 rect,
711 width,
712 height,
713 pixels,
714 radius,
715 } => {
716 Self::mix_hash(hash, 6);
717 Self::mix_hash(
718 hash,
719 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
720 );
721 Self::mix_hash(
722 hash,
723 ((rect.get_width().to_bits() as u64) << 32)
724 | (rect.get_height().to_bits() as u64),
725 );
726 Self::mix_hash(hash, ((*width as u64) << 32) | (*height as u64));
727 Self::mix_hash(hash, pixels.as_ptr() as usize as u64);
728 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
729 }
730 DrawCommand::Line {
731 from,
732 to,
733 color,
734 width,
735 } => {
736 Self::mix_hash(hash, 7);
737 Self::mix_hash(
738 hash,
739 ((from.get_x().to_bits() as u64) << 32) | (from.get_y().to_bits() as u64),
740 );
741 Self::mix_hash(
742 hash,
743 ((to.get_x().to_bits() as u64) << 32) | (to.get_y().to_bits() as u64),
744 );
745 Self::mix_hash(
746 hash,
747 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
748 | (width.to_bits() as u64),
749 );
750 }
751 DrawCommand::Circle {
752 center,
753 radius,
754 color,
755 } => {
756 Self::mix_hash(hash, 8);
757 Self::mix_hash(
758 hash,
759 ((center.get_x().to_bits() as u64) << 32) | (center.get_y().to_bits() as u64),
760 );
761 Self::mix_hash(
762 hash,
763 ((radius.to_bits() as u64) << 32)
764 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64),
765 );
766 }
767 DrawCommand::Triangle { p0, p1, p2, color } => {
768 Self::mix_hash(hash, 9);
769 Self::mix_hash(
770 hash,
771 ((p0.get_x().to_bits() as u64) << 32) | (p0.get_y().to_bits() as u64),
772 );
773 Self::mix_hash(
774 hash,
775 ((p1.get_x().to_bits() as u64) << 32) | (p1.get_y().to_bits() as u64),
776 );
777 Self::mix_hash(
778 hash,
779 ((p2.get_x().to_bits() as u64) << 32) | (p2.get_y().to_bits() as u64),
780 );
781 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
782 }
783 DrawCommand::SpriteRegion {
784 rect,
785 buffer,
786 src_rect,
787 tint,
788 } => {
789 Self::mix_hash(hash, 10);
790 Self::mix_hash(
791 hash,
792 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
793 );
794 Self::mix_hash(
795 hash,
796 ((rect.get_width().to_bits() as u64) << 32)
797 | (rect.get_height().to_bits() as u64),
798 );
799 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
800 Self::mix_hash(
801 hash,
802 ((src_rect.get_x().to_bits() as u64) << 32)
803 | (src_rect.get_y().to_bits() as u64),
804 );
805 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
806 }
807 DrawCommand::PushClip(r) => {
808 Self::mix_hash(hash, 11);
809 Self::mix_hash(
810 hash,
811 ((r.get_x().to_bits() as u64) << 32) | (r.get_y().to_bits() as u64),
812 );
813 Self::mix_hash(
814 hash,
815 ((r.get_width().to_bits() as u64) << 32) | (r.get_height().to_bits() as u64),
816 );
817 }
818 DrawCommand::PushRoundedClip { rect, radius } => {
819 Self::mix_hash(hash, 14);
820 Self::mix_hash(
821 hash,
822 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
823 );
824 Self::mix_hash(
825 hash,
826 ((rect.get_width().to_bits() as u64) << 32)
827 | (rect.get_height().to_bits() as u64),
828 );
829 Self::mix_hash(
830 hash,
831 ((radius.get_top_left().to_bits() as u64) << 32)
832 | (radius.get_top_right().to_bits() as u64),
833 );
834 Self::mix_hash(
835 hash,
836 ((radius.get_bottom_right().to_bits() as u64) << 32)
837 | (radius.get_bottom_left().to_bits() as u64),
838 );
839 }
840 DrawCommand::PopClip => {
841 Self::mix_hash(hash, 12);
842 }
843 }
844 }
845
846 #[inline]
856 #[must_use]
857 pub fn fast_hash(&self) -> u64 {
858 let Self {
859 commands, arena, ..
860 } = self;
861 let mut hash: u64 = arena.running_fast_hash;
862 Self::mix_hash(&mut hash, commands.len() as u64);
863 hash
864 }
865
866 #[inline]
876 #[must_use]
877 pub fn base_layer_hash(&self) -> u64 {
878 let Self {
879 commands, arena, ..
880 } = self;
881 if arena.base_layer_finalized {
882 return arena.base_layer_version.max(1);
883 }
884 let mut hash: u64 = arena.cur_base_digest;
885 Self::mix_hash(&mut hash, commands.len() as u64);
886 if hash == 0 { 1 } else { hash }
887 }
888}
889
890impl PaintFragmentCache {
892 pub fn clear(&mut self) {
898 let Self {
899 has_last_scene,
900 last_has_canvas,
901 slots,
902 cmd_slab,
903 text_slab,
904 canvas_stability,
905 ..
906 } = self;
907 *has_last_scene = false;
908 *last_has_canvas = false;
909 slots.fill(RetainedPaintFragment::default());
910 cmd_slab.clear();
911 text_slab.clear();
912 for entry in canvas_stability.iter_mut() {
913 *entry = CanvasStabilityEntry::default();
914 }
915 }
916
917 #[must_use]
927 pub fn stats(&self) -> (u64, u64, u64, u64) {
928 (
929 self.get_whole_list_hits(),
930 self.get_fragment_hits(),
931 self.get_fragment_misses(),
932 self.get_fragment_evictions(),
933 )
934 }
935
936 #[inline]
942 fn ensure_slots(&mut self) {
943 let Self {
944 slots,
945 cmd_slab,
946 text_slab,
947 ..
948 } = self;
949 if slots.len() != PAINT_FRAGMENT_CACHE_SLOTS {
950 slots.resize(PAINT_FRAGMENT_CACHE_SLOTS, RetainedPaintFragment::default());
951 }
952 if cmd_slab.capacity() < PAINT_FRAGMENT_CMD_SLAB_CAP {
953 cmd_slab.reserve_exact(PAINT_FRAGMENT_CMD_SLAB_CAP - cmd_slab.len());
954 }
955 if text_slab.capacity() < PAINT_FRAGMENT_TEXT_SLAB_CAP {
956 text_slab.reserve_exact(PAINT_FRAGMENT_TEXT_SLAB_CAP - text_slab.len());
957 }
958 }
959
960 #[inline]
973 fn encode_cached_command(
974 cmd: &DrawCommand,
975 text_dst: &mut [u8],
976 text_cursor: &mut usize,
977 text_base: usize,
978 ) -> CachedDrawCommand {
979 match cmd {
980 DrawCommand::Clear(c) => CachedDrawCommand::Clear(*c),
981 DrawCommand::FillRoundedRect {
982 rect,
983 color,
984 radius,
985 border,
986 } => CachedDrawCommand::FillRoundedRect {
987 rect: *rect,
988 color: *color,
989 radius: *radius,
990 border: *border,
991 },
992 DrawCommand::LinearGradientRect {
993 rect,
994 gradient,
995 radius,
996 } => CachedDrawCommand::LinearGradientRect {
997 rect: *rect,
998 gradient: *gradient,
999 radius: *radius,
1000 },
1001 DrawCommand::BoxShadow {
1002 rect,
1003 radius,
1004 shadow,
1005 } => CachedDrawCommand::BoxShadow {
1006 rect: *rect,
1007 radius: *radius,
1008 shadow: *shadow,
1009 },
1010 DrawCommand::BackdropBlur {
1011 rect,
1012 radius,
1013 blur_radius,
1014 } => CachedDrawCommand::BackdropBlur {
1015 rect: *rect,
1016 radius: *radius,
1017 blur_radius: *blur_radius,
1018 },
1019 DrawCommand::TextRun { rect, text, style } => {
1020 let bytes: &[u8] = text.as_bytes();
1021 let start_rel: usize = *text_cursor;
1022 let end_rel: usize = start_rel.saturating_add(bytes.len());
1023 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1024 *text_cursor = end_rel;
1025 CachedDrawCommand::TextRun {
1026 rect: *rect,
1027 text_start: (text_base.saturating_add(start_rel)) as u32,
1028 text_len: bytes.len() as u32,
1029 style: *style,
1030 }
1031 }
1032 DrawCommand::TextCaret {
1033 text_rect,
1034 content_rect,
1035 text,
1036 style,
1037 caret_height,
1038 color,
1039 multiline,
1040 } => {
1041 let bytes: &[u8] = text.as_bytes();
1042 let start_rel: usize = *text_cursor;
1043 let end_rel: usize = start_rel.saturating_add(bytes.len());
1044 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1045 *text_cursor = end_rel;
1046 CachedDrawCommand::TextCaret {
1047 text_rect: *text_rect,
1048 content_rect: *content_rect,
1049 text_start: (text_base.saturating_add(start_rel)) as u32,
1050 text_len: bytes.len() as u32,
1051 style: *style,
1052 caret_height: *caret_height,
1053 color: *color,
1054 multiline: *multiline,
1055 }
1056 }
1057 DrawCommand::Image {
1058 rect,
1059 buffer,
1060 radius,
1061 } => CachedDrawCommand::Image {
1062 rect: *rect,
1063 buffer: buffer.clone(),
1064 radius: *radius,
1065 },
1066 DrawCommand::SharedImage {
1067 rect,
1068 width,
1069 height,
1070 pixels,
1071 radius,
1072 } => CachedDrawCommand::SharedImage {
1073 rect: *rect,
1074 width: *width,
1075 height: *height,
1076 pixels: Arc::clone(pixels),
1077 radius: *radius,
1078 },
1079 DrawCommand::Line {
1080 from,
1081 to,
1082 color,
1083 width,
1084 } => CachedDrawCommand::Line {
1085 from: *from,
1086 to: *to,
1087 color: *color,
1088 width: *width,
1089 },
1090 DrawCommand::Circle {
1091 center,
1092 radius,
1093 color,
1094 } => CachedDrawCommand::Circle {
1095 center: *center,
1096 radius: *radius,
1097 color: *color,
1098 },
1099 DrawCommand::Triangle { p0, p1, p2, color } => CachedDrawCommand::Triangle {
1100 p0: *p0,
1101 p1: *p1,
1102 p2: *p2,
1103 color: *color,
1104 },
1105 DrawCommand::SpriteRegion {
1106 rect,
1107 buffer,
1108 src_rect,
1109 tint,
1110 } => CachedDrawCommand::SpriteRegion {
1111 rect: *rect,
1112 buffer: buffer.clone(),
1113 src_rect: *src_rect,
1114 tint: *tint,
1115 },
1116 DrawCommand::PushClip(rect) => CachedDrawCommand::PushClip(*rect),
1117 DrawCommand::PushRoundedClip { rect, radius } => CachedDrawCommand::PushRoundedClip {
1118 rect: *rect,
1119 radius: *radius,
1120 },
1121 DrawCommand::PopClip => CachedDrawCommand::PopClip,
1122 }
1123 }
1124
1125 fn capture_fragment(
1134 &mut self,
1135 slot_idx: usize,
1136 meta: (NodeId, u32, u32, Point, u64, u64),
1137 cmd_slice: &[DrawCommand],
1138 ) {
1139 let (node_id, phys_scale_milli, opacity_bits, offset_phys, geom_hash, style_text_hash): (
1140 NodeId,
1141 u32,
1142 u32,
1143 Point,
1144 u64,
1145 u64,
1146 ) = meta;
1147 let Self {
1148 slots,
1149 cmd_slab,
1150 text_slab,
1151 fragment_misses,
1152 fragment_evictions,
1153 ..
1154 } = self;
1155 if slot_idx >= slots.len() {
1156 return;
1157 }
1158 *fragment_misses = fragment_misses.wrapping_add(1);
1159 let need_cmds: usize = cmd_slice.len();
1160 let mut need_text: usize = 0;
1161 for cmd in cmd_slice.iter() {
1162 match cmd {
1163 DrawCommand::TextRun { text, .. } | DrawCommand::TextCaret { text, .. } => {
1164 need_text = need_text.saturating_add(text.len());
1165 }
1166 _ => {}
1167 }
1168 }
1169 if need_cmds > PAINT_FRAGMENT_CMD_SLAB_CAP || need_text > PAINT_FRAGMENT_TEXT_SLAB_CAP {
1170 return;
1171 }
1172 let existing: RetainedPaintFragment = slots[slot_idx];
1173 if existing.occupied && existing.node_id != node_id {
1174 *fragment_evictions = fragment_evictions.wrapping_add(1);
1175 }
1176 if existing.occupied
1177 && need_cmds <= (existing.cmd_cap as usize)
1178 && need_text <= (existing.text_cap as usize)
1179 {
1180 let cmd_base: usize = existing.cmd_start as usize;
1181 let text_base: usize = existing.text_start as usize;
1182 let text_end: usize = text_base.saturating_add(need_text);
1183 let mut text_cursor: usize = 0;
1184 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1185 for (i, cmd) in cmd_slice.iter().enumerate() {
1186 cmd_slab[cmd_base + i] =
1187 Self::encode_cached_command(cmd, text_dst, &mut text_cursor, text_base);
1188 }
1189 slots[slot_idx] = RetainedPaintFragment {
1190 occupied: true,
1191 node_id,
1192 phys_scale_milli,
1193 opacity_bits,
1194 offset_phys,
1195 geom_hash,
1196 style_text_hash,
1197 cmd_start: existing.cmd_start,
1198 cmd_len: need_cmds as u32,
1199 cmd_cap: existing.cmd_cap,
1200 text_start: existing.text_start,
1201 text_cap: existing.text_cap,
1202 };
1203 return;
1204 }
1205 if cmd_slab.len().saturating_add(need_cmds) > PAINT_FRAGMENT_CMD_SLAB_CAP
1206 || text_slab.len().saturating_add(need_text) > PAINT_FRAGMENT_TEXT_SLAB_CAP
1207 {
1208 let mut evicted: u64 = 0;
1209 for s in slots.iter_mut() {
1210 if s.occupied {
1211 evicted = evicted.saturating_add(1);
1212 *s = RetainedPaintFragment::default();
1213 }
1214 }
1215 *fragment_evictions = fragment_evictions.wrapping_add(evicted);
1216 cmd_slab.clear();
1217 text_slab.clear();
1218 }
1219 let cmd_start: usize = cmd_slab.len();
1220 let text_base: usize = text_slab.len();
1221 let text_end: usize = text_base.saturating_add(need_text);
1222 text_slab.resize(text_end, 0);
1223 let mut text_cursor: usize = 0;
1224 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1225 for cmd in cmd_slice.iter() {
1226 cmd_slab.push(Self::encode_cached_command(
1227 cmd,
1228 text_dst,
1229 &mut text_cursor,
1230 text_base,
1231 ));
1232 }
1233 slots[slot_idx] = RetainedPaintFragment {
1234 occupied: true,
1235 node_id,
1236 phys_scale_milli,
1237 opacity_bits,
1238 offset_phys,
1239 geom_hash,
1240 style_text_hash,
1241 cmd_start: cmd_start as u32,
1242 cmd_len: need_cmds as u32,
1243 cmd_cap: need_cmds as u32,
1244 text_start: text_base as u32,
1245 text_cap: need_text as u32,
1246 };
1247 }
1248
1249 #[inline]
1264 fn record_canvas_output(
1265 &mut self,
1266 node_id: NodeId,
1267 phys_scale_milli: u32,
1268 bounds: Rect,
1269 tree_revision: u64,
1270 cmds: &[DrawCommand],
1271 ) -> bool {
1272 let slot_idx: usize = (node_id.get_index() as usize) & (CANVAS_STABILITY_SLOTS - 1);
1273 let Self {
1274 canvas_stability, ..
1275 } = self;
1276 let slot: &mut CanvasStabilityEntry = &mut canvas_stability[slot_idx];
1277 let mut has_raster: bool = false;
1278 let mut cmd_hash: u64 = FNV1A_OFFSET_BASIS;
1279 for cmd in cmds.iter() {
1280 if matches!(
1281 cmd,
1282 DrawCommand::Image { .. }
1283 | DrawCommand::SharedImage { .. }
1284 | DrawCommand::SpriteRegion { .. }
1285 ) {
1286 has_raster = true;
1287 break;
1288 }
1289 DisplayList::fold_command_full(&mut cmd_hash, cmd);
1290 }
1291 if has_raster {
1292 *slot = CanvasStabilityEntry {
1293 node_id,
1294 phys_scale_milli,
1295 bounds,
1296 tree_revision,
1297 cmd_hash: 0,
1298 stable_hits: 0,
1299 proven_dynamic: true,
1300 occupied: true,
1301 };
1302 return true;
1303 }
1304 if slot.occupied
1305 && slot.node_id == node_id
1306 && slot.phys_scale_milli == phys_scale_milli
1307 && slot.bounds == bounds
1308 {
1309 if slot.proven_dynamic {
1310 return true;
1311 }
1312 if slot.cmd_hash == cmd_hash {
1313 slot.tree_revision = tree_revision;
1314 slot.stable_hits = slot.stable_hits.saturating_add(1);
1315 return slot.stable_hits < 2;
1316 }
1317 if slot.tree_revision == tree_revision {
1318 slot.proven_dynamic = true;
1319 return true;
1320 }
1321 slot.tree_revision = tree_revision;
1322 slot.cmd_hash = cmd_hash;
1323 slot.stable_hits = 1;
1324 return true;
1325 }
1326 *slot = CanvasStabilityEntry {
1327 node_id,
1328 phys_scale_milli,
1329 bounds,
1330 tree_revision,
1331 cmd_hash,
1332 stable_hits: 1,
1333 proven_dynamic: false,
1334 occupied: true,
1335 };
1336 true
1337 }
1338}
1339
1340impl PaintPass {
1342 #[inline]
1352 #[must_use]
1353 fn text_selection_sig(tree: &SceneTree) -> u64 {
1354 let Some(sel) = tree.text_selection() else {
1355 return 0;
1356 };
1357 if sel.is_empty() {
1358 return 0;
1359 }
1360 let (start, end): (usize, usize) = sel.ordered_range();
1361 let nid: NodeId = sel.get_node_id();
1362 let mut h: u64 = ((nid.get_index() as u64) << 32) | (nid.get_generation() as u64);
1363 h = (h ^ (start as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1364 h = (h ^ (end as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1365 if h == 0 { 1 } else { h }
1366 }
1367
1368 #[inline(always)]
1375 fn fx_fold(hash: &mut u64, word: u64) {
1376 *hash = (*hash ^ word).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1377 }
1378
1379 #[inline(always)]
1386 fn fx_fold_rect(hash: &mut u64, rect: Rect) {
1387 let lo: u64 = ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64);
1388 let hi: u64 =
1389 ((rect.get_width().to_bits() as u64) << 32) | (rect.get_height().to_bits() as u64);
1390 Self::fx_fold(hash, lo);
1391 Self::fx_fold(hash, hi);
1392 }
1393
1394 #[inline]
1401 fn fx_fold_paint(hash: &mut u64, paint: &PaintStyle) {
1402 let bg_bits: u64 = paint
1403 .background_color()
1404 .map(|c: Color| u32::from_le_bytes(c.to_rgba_bytes()) as u64 | (1_u64 << 32))
1405 .unwrap_or(0);
1406 Self::fx_fold(hash, bg_bits);
1407 if let Some(grad) = paint.background_gradient() {
1408 let g0: u64 = u32::from_le_bytes(grad.get_start_color().to_rgba_bytes()) as u64;
1409 let g1: u64 = u32::from_le_bytes(grad.get_end_color().to_rgba_bytes()) as u64;
1410 Self::fx_fold(hash, (g0 << 32) | g1);
1411 Self::fx_fold(hash, grad.get_angle_deg().to_bits() as u64 | (1_u64 << 33));
1412 }
1413 let border: Border = paint.get_border();
1414 let b_word: u64 = ((border.get_width().to_bits() as u64) << 32)
1415 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64);
1416 Self::fx_fold(hash, b_word);
1417 let r: CornerRadius = paint.get_radius();
1418 let r0: u64 =
1419 ((r.get_top_left().to_bits() as u64) << 32) | (r.get_top_right().to_bits() as u64);
1420 let r1: u64 = ((r.get_bottom_right().to_bits() as u64) << 32)
1421 | (r.get_bottom_left().to_bits() as u64);
1422 Self::fx_fold(hash, r0);
1423 Self::fx_fold(hash, r1);
1424 Self::fx_fold(hash, paint.get_opacity().to_bits() as u64);
1425 if let Some(sh) = paint.box_shadow() {
1426 let s0: u64 =
1427 ((sh.get_offset_x().to_bits() as u64) << 32) | (sh.get_offset_y().to_bits() as u64);
1428 let s1: u64 = ((sh.get_blur_radius().to_bits() as u64) << 32)
1429 | (sh.get_spread_radius().to_bits() as u64);
1430 Self::fx_fold(hash, s0);
1431 Self::fx_fold(hash, s1);
1432 Self::fx_fold(
1433 hash,
1434 u32::from_le_bytes(sh.get_color().to_rgba_bytes()) as u64 | (1_u64 << 34),
1435 );
1436 }
1437 }
1438
1439 #[inline]
1447 fn fx_fold_text(hash: &mut u64, text_style: &TextStyle, text: &str) {
1448 let m0: u64 = ((text_style.get_font_size().to_bits() as u64) << 32)
1449 | (text_style.get_line_height().to_bits() as u64);
1450 let m1: u64 = ((text_style.get_letter_spacing().to_bits() as u64) << 32)
1451 | (u32::from_le_bytes(text_style.get_color().to_rgba_bytes()) as u64);
1452 let flags: u64 = (text_style.get_family() as u64)
1453 | ((text_style.get_weight() as u64) << 8)
1454 | ((text_style.get_align() as u64) << 16)
1455 | ((text_style.get_overflow() as u64) << 24)
1456 | (u64::from(text_style.get_italic()) << 32);
1457 Self::fx_fold(hash, m0);
1458 Self::fx_fold(hash, m1);
1459 Self::fx_fold(hash, flags);
1460 let bytes: &[u8] = text.as_bytes();
1461 Self::fx_fold(hash, bytes.len() as u64);
1462 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
1463 for chunk in chunks.iter() {
1464 Self::fx_fold(hash, u64::from_ne_bytes(*chunk));
1465 }
1466 if !rem.is_empty() {
1467 let mut tail: u64 = 0;
1468 for (idx, &b) in rem.iter().enumerate() {
1469 tail |= (b as u64) << (idx * 8);
1470 }
1471 Self::fx_fold(hash, tail);
1472 }
1473 }
1474
1475 #[inline(always)]
1485 #[must_use]
1486 fn quantize_scale_milli(scale: f32) -> u32 {
1487 (scale.max(0.5) * 1000.0).round() as u32
1488 }
1489
1490 #[inline]
1505 #[must_use]
1506 fn fingerprint_subtree_children(
1507 tree: &SceneTree,
1508 parent: NodeId,
1509 depth: usize,
1510 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1511 focus_pair: (Option<NodeId>, Option<NodeId>),
1512 acc: &mut (usize, u64, u64),
1513 ) -> bool {
1514 let child_count: usize = tree.child_count(parent);
1515 if depth > PAINT_FRAGMENT_MAX_DEPTH || child_count > PAINT_FRAGMENT_MAX_CHILDREN {
1516 return false;
1517 }
1518 let (active_hover_target, selection_node): (Option<NodeId>, Option<NodeId>) = focus_pair;
1519 Self::fx_fold(&mut acc.1, child_count as u64);
1520 for cid in tree.children_iter(parent) {
1521 if acc.0 >= PAINT_FRAGMENT_MAX_NODES
1522 || active_hover_target == Some(cid)
1523 || selection_node == Some(cid)
1524 || tree.backdrop_blur_radius(cid) > 0.0
1525 {
1526 return false;
1527 }
1528 let Some(child) = tree.get_node(cid) else {
1529 return false;
1530 };
1531 acc.0 += 1;
1532 let c_inter: InteractionState = child.get_interaction();
1533 let c_active: bool = c_inter.get_hovered() || c_inter.get_pressed();
1534 if c_active || c_inter.get_focused() {
1535 return false;
1536 }
1537 let c_style: &Style = child.style_ref();
1538 if c_style.layout.get_position() == Position::Fixed {
1539 return false;
1540 }
1541 let c_tag: u64 = match child.get_kind() {
1542 NodeKind::View | NodeKind::Fragment => 1,
1543 NodeKind::Text(text) => {
1544 Self::fx_fold_text(&mut acc.2, &c_style.text, text.as_str());
1545 Self::fx_fold_rect(&mut acc.1, child.layout_box.get_content_bounds());
1546 2
1547 }
1548 NodeKind::Image(buf) => {
1549 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1550 Self::fx_fold(
1551 &mut acc.2,
1552 ((buf.get_width() as u64) << 32) | (buf.get_height() as u64),
1553 );
1554 Self::fx_fold(&mut acc.2, buf.get_pixels().as_ptr() as usize as u64);
1555 3
1556 }
1557 NodeKind::Canvas(CanvasCaching::RetainedStatic) => {
1558 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1559 if let Some(cb) = child.canvas_callback() {
1560 let cb_addr: u64 = (Rc::as_ptr(&cb) as *const ()) as usize as u64;
1561 Self::fx_fold(&mut acc.2, cb_addr);
1562 }
1563 4
1564 }
1565 NodeKind::Portal
1566 | NodeKind::Canvas(CanvasCaching::DynamicPerFrame)
1567 | NodeKind::Input(_)
1568 | NodeKind::ScrollView => {
1569 return false;
1570 }
1571 };
1572 let c_id_bits: u64 = ((cid.get_index() as u64) << 32) | (cid.get_generation() as u64);
1573 Self::fx_fold(&mut acc.1, c_id_bits);
1574 let raw_b: Rect = child.layout_box.get_bounds();
1575 Self::fx_fold_rect(&mut acc.1, raw_b);
1576 let ct: Transform2D = c_style.paint.get_transform();
1577 Self::fx_fold(
1578 &mut acc.1,
1579 ((ct.get_translate_x().to_bits() as u64) << 32)
1580 | (ct.get_translate_y().to_bits() as u64),
1581 );
1582 Self::fx_fold(
1583 &mut acc.1,
1584 ((ct.get_scale_x().to_bits() as u64) << 32) | (ct.get_scale_y().to_bits() as u64),
1585 );
1586 let c_lay_bits: u64 = (c_tag << 40)
1587 | ((c_style.layout.get_display() as u64) << 32)
1588 | ((c_style.layout.get_overflow() as u64) << 24)
1589 | ((c_style.layout.get_z_index() as u32) as u64);
1590 Self::fx_fold(&mut acc.2, c_lay_bits);
1591 Self::fx_fold_paint(&mut acc.2, &c_style.paint);
1592 let mut next_cull_check: Option<(Point, Rect, f32, f32)> = None;
1593 if let Some((off_phys, cull_rect, scale, inv_scale)) = unclipped_cull {
1594 let tx_phys: f32 = (ct.get_translate_x() * scale).round();
1595 let ty_phys: f32 = (ct.get_translate_y() * scale).round();
1596 let tot_x: f32 = off_phys.get_x() + tx_phys;
1597 let tot_y: f32 = off_phys.get_y() + ty_phys;
1598 let rx0: f32 = (raw_b.get_x() * scale).round();
1599 let ry0: f32 = (raw_b.get_y() * scale).round();
1600 let rx1: f32 = (raw_b.right() * scale).round();
1601 let ry1: f32 = (raw_b.bottom() * scale).round();
1602 let sw: f32 = ((rx1 - rx0).max(0.0) * ct.get_scale_x()).round().max(0.0);
1603 let sh: f32 = ((ry1 - ry0).max(0.0) * ct.get_scale_y()).round().max(0.0);
1604 let sdx: f32 = (((rx1 - rx0).max(0.0) - sw) * 0.5).round();
1605 let sdy: f32 = (((ry1 - ry0).max(0.0) - sh) * 0.5).round();
1606 let c_bounds: Rect = Rect::new(
1607 (rx0 + tot_x + sdx) * inv_scale,
1608 (ry0 + tot_y + sdy) * inv_scale,
1609 sw * inv_scale,
1610 sh * inv_scale,
1611 );
1612 let c_vis: Rect = Self::conservative_self_vis_rect(
1613 child,
1614 c_bounds,
1615 &c_style.paint,
1616 false,
1617 (tot_x, tot_y),
1618 scale,
1619 inv_scale,
1620 );
1621 let c_in_x: bool =
1622 c_vis.get_x() >= cull_rect.get_x() && c_vis.right() <= cull_rect.right();
1623 let c_in_y: bool =
1624 c_vis.get_y() >= cull_rect.get_y() && c_vis.bottom() <= cull_rect.bottom();
1625 if !c_in_x || !c_in_y {
1626 return false;
1627 }
1628 if c_style.layout.get_overflow() != Overflow::Hidden {
1629 let c_scroll: Point = child.layout_box.get_scroll_offset();
1630 let next_off: Point = Point::new(
1631 tot_x - (c_scroll.get_x() * scale).round(),
1632 tot_y - (c_scroll.get_y() * scale).round(),
1633 );
1634 next_cull_check = Some((next_off, cull_rect, scale, inv_scale));
1635 }
1636 }
1637 if tree.has_children(cid)
1638 && !Self::fingerprint_subtree_children(
1639 tree,
1640 cid,
1641 depth + 1,
1642 next_cull_check,
1643 focus_pair,
1644 acc,
1645 )
1646 {
1647 return false;
1648 }
1649 }
1650 true
1651 }
1652
1653 #[inline]
1667 #[must_use]
1668 fn fingerprint_subtree(
1669 tree: &SceneTree,
1670 node_id: NodeId,
1671 node: &SceneNode,
1672 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1673 focus_pair: (Option<NodeId>, Option<NodeId>),
1674 ) -> Option<(u64, u64)> {
1675 let child_count: usize = tree.child_count(node_id);
1676 if child_count == 0
1677 || child_count > PAINT_FRAGMENT_MAX_CHILDREN
1678 || tree.backdrop_blur_radius(node_id) > 0.0
1679 {
1680 return None;
1681 }
1682 let style: &Style = node.style_ref();
1683 let transform: Transform2D = style.paint.get_transform();
1684 let scroll: Point = node.layout_box.get_scroll_offset();
1685 let mut geom_h: u64 = child_count as u64;
1686 Self::fx_fold_rect(&mut geom_h, node.layout_box.get_bounds());
1687 Self::fx_fold(
1688 &mut geom_h,
1689 ((transform.get_translate_x().to_bits() as u64) << 32)
1690 | (transform.get_translate_y().to_bits() as u64),
1691 );
1692 Self::fx_fold(
1693 &mut geom_h,
1694 ((transform.get_scale_x().to_bits() as u64) << 32)
1695 | (transform.get_scale_y().to_bits() as u64),
1696 );
1697 Self::fx_fold(
1698 &mut geom_h,
1699 ((scroll.get_x().to_bits() as u64) << 32) | (scroll.get_y().to_bits() as u64),
1700 );
1701 let mut style_h: u64 =
1702 ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
1703 Self::fx_fold_paint(&mut style_h, &style.paint);
1704 let mut acc: (usize, u64, u64) = (0, geom_h, style_h);
1705 if !Self::fingerprint_subtree_children(
1706 tree,
1707 node_id,
1708 1,
1709 unclipped_cull,
1710 focus_pair,
1711 &mut acc,
1712 ) {
1713 return None;
1714 }
1715 Some((acc.1, acc.2))
1716 }
1717
1718 #[inline(always)]
1732 #[must_use]
1733 fn translate_coord(v: f32, d_phys: f32, scale: f32, inv_scale: f32, dyadic: bool) -> f32 {
1734 if d_phys == 0.0 {
1735 return v;
1736 }
1737 if dyadic {
1738 return v + d_phys * inv_scale;
1739 }
1740 ((v * scale).round() + d_phys) * inv_scale
1741 }
1742
1743 #[inline(always)]
1758 #[must_use]
1759 fn translate_rect(
1760 rect: Rect,
1761 dx_phys: f32,
1762 dy_phys: f32,
1763 scale: f32,
1764 inv_scale: f32,
1765 dyadic: bool,
1766 ) -> Rect {
1767 Rect::new(
1768 Self::translate_coord(rect.get_x(), dx_phys, scale, inv_scale, dyadic),
1769 Self::translate_coord(rect.get_y(), dy_phys, scale, inv_scale, dyadic),
1770 rect.get_width(),
1771 rect.get_height(),
1772 )
1773 }
1774
1775 #[inline]
1785 fn push_translated_command(
1786 cmd: &CachedDrawCommand,
1787 text_slab: &[u8],
1788 delta_phys: (f32, f32),
1789 scale_info: (f32, f32, bool),
1790 display_list: &mut DisplayList,
1791 ) {
1792 let (dx_phys, dy_phys): (f32, f32) = delta_phys;
1793 let (scale, inv_scale, dyadic): (f32, f32, bool) = scale_info;
1794 let translated: DrawCommand = match cmd {
1795 CachedDrawCommand::Clear(c) => DrawCommand::Clear(*c),
1796 CachedDrawCommand::FillRoundedRect {
1797 rect,
1798 color,
1799 radius,
1800 border,
1801 } => DrawCommand::FillRoundedRect {
1802 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1803 color: *color,
1804 radius: *radius,
1805 border: *border,
1806 },
1807 CachedDrawCommand::LinearGradientRect {
1808 rect,
1809 gradient,
1810 radius,
1811 } => DrawCommand::LinearGradientRect {
1812 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1813 gradient: *gradient,
1814 radius: *radius,
1815 },
1816 CachedDrawCommand::BoxShadow {
1817 rect,
1818 radius,
1819 shadow,
1820 } => DrawCommand::BoxShadow {
1821 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1822 radius: *radius,
1823 shadow: *shadow,
1824 },
1825 CachedDrawCommand::BackdropBlur {
1826 rect,
1827 radius,
1828 blur_radius,
1829 } => DrawCommand::BackdropBlur {
1830 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1831 radius: *radius,
1832 blur_radius: *blur_radius,
1833 },
1834 CachedDrawCommand::TextRun {
1835 rect,
1836 text_start,
1837 text_len,
1838 style,
1839 } => {
1840 let s: usize = *text_start as usize;
1841 let e: usize = s.saturating_add(*text_len as usize);
1842 let src: &str = text_slab
1843 .get(s..e)
1844 .and_then(|b: &[u8]| from_utf8(b).ok())
1845 .unwrap_or("");
1846 DrawCommand::TextRun {
1847 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1848 text: display_list.alloc_text(src),
1849 style: *style,
1850 }
1851 }
1852 CachedDrawCommand::TextCaret {
1853 text_rect,
1854 content_rect,
1855 text_start,
1856 text_len,
1857 style,
1858 caret_height,
1859 color,
1860 multiline,
1861 } => {
1862 let s: usize = *text_start as usize;
1863 let e: usize = s.saturating_add(*text_len as usize);
1864 let src: &str = text_slab
1865 .get(s..e)
1866 .and_then(|b: &[u8]| from_utf8(b).ok())
1867 .unwrap_or("");
1868 DrawCommand::TextCaret {
1869 text_rect: Self::translate_rect(
1870 *text_rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
1871 ),
1872 content_rect: Self::translate_rect(
1873 *content_rect,
1874 dx_phys,
1875 dy_phys,
1876 scale,
1877 inv_scale,
1878 dyadic,
1879 ),
1880 text: display_list.alloc_text(src),
1881 style: *style,
1882 caret_height: *caret_height,
1883 color: *color,
1884 multiline: *multiline,
1885 }
1886 }
1887 CachedDrawCommand::Image {
1888 rect,
1889 buffer,
1890 radius,
1891 } => DrawCommand::Image {
1892 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1893 buffer: buffer.clone(),
1894 radius: *radius,
1895 },
1896 CachedDrawCommand::SharedImage {
1897 rect,
1898 width,
1899 height,
1900 pixels,
1901 radius,
1902 } => DrawCommand::SharedImage {
1903 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1904 width: *width,
1905 height: *height,
1906 pixels: Arc::clone(pixels),
1907 radius: *radius,
1908 },
1909 CachedDrawCommand::Line {
1910 from,
1911 to,
1912 color,
1913 width,
1914 } => DrawCommand::Line {
1915 from: Point::new(
1916 Self::translate_coord(from.get_x(), dx_phys, scale, inv_scale, dyadic),
1917 Self::translate_coord(from.get_y(), dy_phys, scale, inv_scale, dyadic),
1918 ),
1919 to: Point::new(
1920 Self::translate_coord(to.get_x(), dx_phys, scale, inv_scale, dyadic),
1921 Self::translate_coord(to.get_y(), dy_phys, scale, inv_scale, dyadic),
1922 ),
1923 color: *color,
1924 width: *width,
1925 },
1926 CachedDrawCommand::Circle {
1927 center,
1928 radius,
1929 color,
1930 } => DrawCommand::Circle {
1931 center: Point::new(
1932 Self::translate_coord(center.get_x(), dx_phys, scale, inv_scale, dyadic),
1933 Self::translate_coord(center.get_y(), dy_phys, scale, inv_scale, dyadic),
1934 ),
1935 radius: *radius,
1936 color: *color,
1937 },
1938 CachedDrawCommand::Triangle { p0, p1, p2, color } => DrawCommand::Triangle {
1939 p0: Point::new(
1940 Self::translate_coord(p0.get_x(), dx_phys, scale, inv_scale, dyadic),
1941 Self::translate_coord(p0.get_y(), dy_phys, scale, inv_scale, dyadic),
1942 ),
1943 p1: Point::new(
1944 Self::translate_coord(p1.get_x(), dx_phys, scale, inv_scale, dyadic),
1945 Self::translate_coord(p1.get_y(), dy_phys, scale, inv_scale, dyadic),
1946 ),
1947 p2: Point::new(
1948 Self::translate_coord(p2.get_x(), dx_phys, scale, inv_scale, dyadic),
1949 Self::translate_coord(p2.get_y(), dy_phys, scale, inv_scale, dyadic),
1950 ),
1951 color: *color,
1952 },
1953 CachedDrawCommand::SpriteRegion {
1954 rect,
1955 buffer,
1956 src_rect,
1957 tint,
1958 } => DrawCommand::SpriteRegion {
1959 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1960 buffer: buffer.clone(),
1961 src_rect: *src_rect,
1962 tint: *tint,
1963 },
1964 CachedDrawCommand::PushClip(rect) => DrawCommand::PushClip(Self::translate_rect(
1965 *rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
1966 )),
1967 CachedDrawCommand::PushRoundedClip { rect, radius } => DrawCommand::PushRoundedClip {
1968 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1969 radius: *radius,
1970 },
1971 CachedDrawCommand::PopClip => DrawCommand::PopClip,
1972 };
1973 display_list.push(translated);
1974 }
1975
1976 pub fn build(
1986 tree: &SceneTree,
1987 viewport: Size,
1988 scale_factor: f32,
1989 clear_color: Color,
1990 display_list: &mut DisplayList,
1991 ) {
1992 let mut saw_canvas: bool = false;
1993 Self::build_inner(
1994 tree,
1995 viewport,
1996 scale_factor,
1997 clear_color,
1998 display_list,
1999 None,
2000 &mut saw_canvas,
2001 );
2002 }
2003
2004 #[inline(always)]
2014 #[must_use]
2015 fn pack_opt_node_id(node: Option<NodeId>) -> u64 {
2016 match node {
2017 Some(id) => ((id.get_index() as u64) << 32) | (id.get_generation() as u64),
2018 None => u64::MAX,
2019 }
2020 }
2021
2022 pub fn build_retained(
2034 tree: &SceneTree,
2035 viewport: Size,
2036 scale_factor: f32,
2037 clear_color: Color,
2038 layout_changed: bool,
2039 display_list: &mut DisplayList,
2040 cache: &mut PaintFragmentCache,
2041 ) {
2042 let clamped_scale: f32 = scale_factor.max(0.5);
2043 let focus: FocusManager = tree.get_focus();
2044 let sig: PaintSceneSignature = PaintSceneSignature {
2045 tree_ptr: (tree as *const SceneTree) as usize,
2046 tree_revision: tree.get_revision(),
2047 viewport_w_bits: viewport.get_width().to_bits(),
2048 viewport_h_bits: viewport.get_height().to_bits(),
2049 scale_bits: clamped_scale.to_bits(),
2050 clear_rgba: u32::from_le_bytes(clear_color.to_rgba_bytes()),
2051 focused_packed: Self::pack_opt_node_id(focus.focused_node()),
2052 hovered_packed: Self::pack_opt_node_id(focus.hovered_node()),
2053 pressed_packed: Self::pack_opt_node_id(focus.pressed_node()),
2054 caret_visible: tree.get_caret_visible(),
2055 text_sel_sig: Self::text_selection_sig(tree),
2056 };
2057 if !layout_changed
2058 && !cache.last_has_canvas
2059 && !display_list.is_empty()
2060 && cache.has_last_scene
2061 && cache.last_scene_sig == sig
2062 {
2063 cache.whole_list_hits = cache.whole_list_hits.wrapping_add(1);
2064 return;
2065 }
2066 cache.ensure_slots();
2067 let mut saw_canvas: bool = false;
2068 Self::build_inner(
2069 tree,
2070 viewport,
2071 clamped_scale,
2072 clear_color,
2073 display_list,
2074 Some(cache),
2075 &mut saw_canvas,
2076 );
2077 cache.last_has_canvas = saw_canvas;
2078 cache.has_last_scene = true;
2079 cache.last_scene_sig = sig;
2080 }
2081
2082 fn collect_portal_ancestors(tree: &SceneTree, out_ancestors: &mut Vec<NodeId>) {
2089 out_ancestors.clear();
2090 let portal_nodes: &[NodeId] = tree.portal_nodes();
2091 if portal_nodes.is_empty() {
2092 return;
2093 }
2094 for &pid in portal_nodes.iter() {
2095 let mut cur: Option<NodeId> = tree.get_node(pid).and_then(SceneNode::parent_id);
2096 while let Some(anc) = cur {
2097 if out_ancestors.contains(&anc) {
2098 break;
2099 }
2100 out_ancestors.push(anc);
2101 cur = tree.get_node(anc).and_then(SceneNode::parent_id);
2102 }
2103 }
2104 if out_ancestors.len() > 16 {
2105 out_ancestors.sort_unstable_by_key(|id: &NodeId| {
2106 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2107 });
2108 }
2109 }
2110
2111 #[inline]
2122 #[must_use]
2123 fn is_portal_ancestor(ancestors: &[NodeId], node_id: NodeId) -> bool {
2124 if ancestors.is_empty() {
2125 return false;
2126 }
2127 if ancestors.len() <= 16 {
2128 return ancestors.contains(&node_id);
2129 }
2130 let target: u64 = ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
2131 ancestors
2132 .binary_search_by_key(&target, |id: &NodeId| {
2133 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2134 })
2135 .is_ok()
2136 }
2137
2138 #[inline]
2154 #[must_use]
2155 fn conservative_self_vis_rect(
2156 node: &SceneNode,
2157 bounds: Rect,
2158 paint: &PaintStyle,
2159 focused: bool,
2160 total_off_phys: (f32, f32),
2161 scale: f32,
2162 inv_scale: f32,
2163 ) -> Rect {
2164 let mut min_x: f32 = bounds.get_x() - 1.0;
2165 let mut min_y: f32 = bounds.get_y() - 1.0;
2166 let mut max_x: f32 = bounds.right() + 1.0;
2167 let mut max_y: f32 = bounds.bottom() + 1.0;
2168 if let Some(shadow) = paint.box_shadow()
2169 && shadow.get_color().get_a() > 0
2170 {
2171 let spread: f32 = shadow.get_spread_radius().max(0.0);
2172 let blur_phys: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
2173 let sigma_phys: f32 = (blur_phys * 0.5).max(0.75);
2174 let blur_pad: f32 = (sigma_phys * 3.0).ceil() * inv_scale + 2.0 * inv_scale;
2175 let pad: f32 = spread + blur_pad;
2176 let sx0: f32 = bounds.get_x() + shadow.get_offset_x() - pad;
2177 let sy0: f32 = bounds.get_y() + shadow.get_offset_y() - pad;
2178 let sx1: f32 = bounds.right() + shadow.get_offset_x() + pad;
2179 let sy1: f32 = bounds.bottom() + shadow.get_offset_y() + pad;
2180 min_x = min_x.min(sx0);
2181 min_y = min_y.min(sy0);
2182 max_x = max_x.max(sx1);
2183 max_y = max_y.max(sy1);
2184 }
2185 if focused && let Some(ring) = paint.focus_ring_border() {
2186 let rw: f32 = ring.get_width().max(0.0) + 1.0;
2187 min_x = min_x.min(bounds.get_x() - rw);
2188 min_y = min_y.min(bounds.get_y() - rw);
2189 max_x = max_x.max(bounds.right() + rw);
2190 max_y = max_y.max(bounds.bottom() + rw);
2191 }
2192 if matches!(node.get_kind(), NodeKind::Text(_)) {
2193 let content_b: Rect = node.layout_box.get_content_bounds();
2194 let cx0: f32 = ((content_b.get_x() * scale).round() + total_off_phys.0) * inv_scale;
2195 let cy0: f32 = ((content_b.get_y() * scale).round() + total_off_phys.1) * inv_scale;
2196 let pad: f32 = node.style_ref().text.get_font_size() * 0.5 + 2.0;
2197 min_x = min_x.min(cx0 - pad);
2198 min_y = min_y.min(cy0 - pad);
2199 max_x = max_x.max(cx0 + content_b.get_width() + pad);
2200 max_y = max_y.max(cy0 + content_b.get_height() + pad);
2201 }
2202 Rect::new(
2203 min_x,
2204 min_y,
2205 (max_x - min_x).max(0.0),
2206 (max_y - min_y).max(0.0),
2207 )
2208 }
2209
2210 fn visit_culled_portal_child(
2219 tree: &SceneTree,
2220 child_id: NodeId,
2221 parent_opacity: f32,
2222 scratch: &mut PaintScratch<'_>,
2223 ) {
2224 let Some(child) = tree.get_node(child_id) else {
2225 return;
2226 };
2227 let style: &Style = child.style_ref();
2228 let layout: &LayoutStyle = &style.layout;
2229 if layout.get_display() == Display::None {
2230 return;
2231 }
2232 let is_portal: bool = matches!(child.get_kind(), NodeKind::Portal)
2233 || layout.get_position() == Position::Fixed;
2234 if is_portal {
2235 if let Some(portal_queue) = scratch.portals.as_deref_mut() {
2236 portal_queue.push((layout.get_z_index(), portal_queue.len(), child_id));
2237 }
2238 return;
2239 }
2240 if !Self::is_portal_ancestor(scratch.portal_ancestors, child_id) {
2241 return;
2242 }
2243 let hovered: bool = child.get_interaction().get_hovered();
2244 let paint_opacity: f32 = if hovered {
2245 child
2246 .hover_paint()
2247 .map(|hp: PaintStyle| hp.get_opacity())
2248 .unwrap_or_else(|| style.paint.get_opacity())
2249 } else {
2250 style.paint.get_opacity()
2251 };
2252 let next_opacity: f32 = (parent_opacity * paint_opacity).clamp(0.0, 1.0);
2253 if next_opacity <= 0.0 {
2254 return;
2255 }
2256 Self::collect_portals_in_culled_subtree(tree, child_id, next_opacity, scratch);
2257 }
2258
2259 fn collect_portals_in_culled_subtree(
2268 tree: &SceneTree,
2269 node_id: NodeId,
2270 parent_opacity: f32,
2271 scratch: &mut PaintScratch<'_>,
2272 ) {
2273 if tree.get_node(node_id).is_none() {
2274 return;
2275 }
2276 let child_count: usize = tree.child_count(node_id);
2277 if child_count == 0 {
2278 return;
2279 }
2280 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
2281 if !needs_z_sort {
2282 for child_id in tree.children_iter(node_id) {
2283 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2284 }
2285 } else {
2286 let sort_start: usize = scratch.z_sort.len();
2287 for (idx, cid) in tree.children_iter(node_id).enumerate() {
2288 if let Some(c) = tree.get_node(cid) {
2289 scratch
2290 .z_sort
2291 .push((c.style_ref().layout.get_z_index(), idx, cid));
2292 }
2293 }
2294 let sort_end: usize = scratch.z_sort.len();
2295 scratch.z_sort[sort_start..sort_end].sort_by(
2296 |a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
2297 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
2298 },
2299 );
2300 for slot_idx in sort_start..sort_end {
2301 let child_id: NodeId = scratch.z_sort[slot_idx].2;
2302 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2303 }
2304 scratch.z_sort.truncate(sort_start);
2305 }
2306 }
2307
2308 fn build_inner(
2320 tree: &SceneTree,
2321 viewport: Size,
2322 scale_factor: f32,
2323 clear_color: Color,
2324 display_list: &mut DisplayList,
2325 mut fragments: Option<&mut PaintFragmentCache>,
2326 saw_canvas: &mut bool,
2327 ) {
2328 display_list.reset(viewport, scale_factor);
2329 display_list.push(DrawCommand::Clear(clear_color));
2330 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
2331 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
2332 let mut portals: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.portals_scratch);
2333 let mut z_sort: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.z_sort_scratch);
2334 let mut portal_ancestors: Vec<NodeId> =
2335 take(&mut display_list.arena.portal_ancestors_scratch);
2336 portals.clear();
2337 z_sort.clear();
2338 Self::collect_portal_ancestors(tree, &mut portal_ancestors);
2339 {
2340 let mut scratch: PaintScratch<'_> = PaintScratch {
2341 portals: Some(&mut portals),
2342 z_sort: &mut z_sort,
2343 portal_ancestors: portal_ancestors.as_slice(),
2344 fragments: fragments.as_deref_mut(),
2345 saw_canvas,
2346 };
2347 Self::emit_node(
2348 tree,
2349 tree.get_root(),
2350 (Point::new(0.0, 0.0), 1.0),
2351 viewport_rect,
2352 active_hover_target,
2353 &mut scratch,
2354 display_list,
2355 );
2356 }
2357 display_list.finalize_base_layer(display_list.len());
2358 if portals.len() > 1 {
2359 portals.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
2360 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
2361 });
2362 }
2363 {
2364 let mut portal_scratch: PaintScratch<'_> = PaintScratch {
2365 portals: None,
2366 z_sort: &mut z_sort,
2367 portal_ancestors: &[],
2368 fragments,
2369 saw_canvas,
2370 };
2371 for &(_, _, portal_id) in portals.iter() {
2372 Self::emit_node(
2373 tree,
2374 portal_id,
2375 (Point::new(0.0, 0.0), 1.0),
2376 viewport_rect,
2377 active_hover_target,
2378 &mut portal_scratch,
2379 display_list,
2380 );
2381 }
2382 }
2383 portals.clear();
2384 z_sort.clear();
2385 portal_ancestors.clear();
2386 display_list.arena.portals_scratch = portals;
2387 display_list.arena.z_sort_scratch = z_sort;
2388 display_list.arena.portal_ancestors_scratch = portal_ancestors;
2389 }
2390
2391 #[must_use]
2401 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
2402 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
2403 while let Some(id) = cursor {
2404 let Some(node) = tree.get_node(id) else {
2405 break;
2406 };
2407 let style: &Style = node.style_ref();
2408 let paint: &PaintStyle = &style.paint;
2409 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2410 || style.layout.get_position() == Position::Fixed;
2411 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2412 && paint.background_color().is_none();
2413 if !is_scrim && !is_transparent_canvas {
2414 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
2415 || node
2416 .get_handlers()
2417 .handler_for(&UiEventKind::Click(Point::default()))
2418 .is_some();
2419 if is_interactive {
2420 return Some(id);
2421 }
2422 }
2423 cursor = node.parent_id();
2424 }
2425 None
2426 }
2427
2428 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
2440 if !hovered && !pressed {
2441 return base;
2442 }
2443 let delta: i32 = if pressed { 24 } else { 14 };
2444 match base {
2445 Some(c) if c.get_a() > 0 => {
2446 let luma: u32 =
2447 (c.get_r() as u32) * 299 + (c.get_g() as u32) * 587 + (c.get_b() as u32) * 114;
2448 if luma < 120_000 {
2449 let lift: i32 = if pressed { 30 } else { 20 };
2450 Some(Color::new(
2451 (c.get_r() as i32 + lift).clamp(0, 255) as u8,
2452 (c.get_g() as i32 + lift).clamp(0, 255) as u8,
2453 (c.get_b() as i32 + lift).clamp(0, 255) as u8,
2454 c.get_a(),
2455 ))
2456 } else {
2457 Some(Color::new(
2458 (c.get_r() as i32 - delta).clamp(0, 255) as u8,
2459 (c.get_g() as i32 - delta).clamp(0, 255) as u8,
2460 (c.get_b() as i32 - delta).clamp(0, 255) as u8,
2461 c.get_a(),
2462 ))
2463 }
2464 }
2465 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
2466 }
2467 }
2468
2469 fn emit_node(
2481 tree: &SceneTree,
2482 node_id: NodeId,
2483 offset_and_opacity: (Point, f32),
2484 cull_rect: Rect,
2485 active_hover_target: Option<NodeId>,
2486 scratch: &mut PaintScratch<'_>,
2487 display_list: &mut DisplayList,
2488 ) {
2489 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
2490 let Some(node) = tree.get_node(node_id) else {
2491 return;
2492 };
2493 let style: &Style = node.style_ref();
2494 let layout: &LayoutStyle = &style.layout;
2495 if layout.get_display() == Display::None {
2496 return;
2497 }
2498 if let Some(portal_queue) = scratch.portals.as_deref_mut()
2499 && (matches!(node.get_kind(), NodeKind::Portal)
2500 || layout.get_position() == Position::Fixed)
2501 {
2502 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
2503 return;
2504 }
2505 let interaction: InteractionState = node.get_interaction();
2506 let hovered: bool = interaction.get_hovered();
2507 let pressed: bool = interaction.get_pressed();
2508 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
2509 return;
2510 }
2511 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
2512 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2513 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
2514 if opacity <= 0.0 {
2515 return;
2516 }
2517 let scale: f32 = display_list.get_scale_factor().max(0.5);
2518 let inv_scale: f32 = 1.0 / scale;
2519 let raw_bounds: Rect = node.layout_box.get_bounds();
2520 let transform: Transform2D = prelim_paint.get_transform();
2521 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
2522 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
2523 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
2524 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
2525 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
2526 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
2527 let raw_x1: f32 = (raw_bounds.right() * scale).round();
2528 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
2529 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
2530 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
2531 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
2532 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
2533 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
2534 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
2535 let bounds: Rect = Rect::new(
2536 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
2537 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
2538 scaled_w_phys * inv_scale,
2539 scaled_h_phys * inv_scale,
2540 );
2541 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2542 let clipped_cull: Option<Rect> = if clipped {
2543 cull_rect.intersect(bounds)
2544 } else {
2545 Some(cull_rect)
2546 };
2547 let focused: bool = interaction.get_focused();
2548 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
2549 node,
2550 bounds,
2551 prelim_paint,
2552 focused,
2553 (total_off_x, total_off_y),
2554 scale,
2555 inv_scale,
2556 );
2557 let self_visible: bool = bounds.get_width() > 0.0
2558 && bounds.get_height() > 0.0
2559 && self_vis_rect.intersect(cull_rect).is_some();
2560 if clipped && clipped_cull.is_none() {
2561 if self_visible {
2562 if let Some(mut shadow) = prelim_paint.box_shadow() {
2563 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2564 display_list.push(DrawCommand::BoxShadow {
2565 rect: bounds,
2566 radius: prelim_paint.get_radius(),
2567 shadow,
2568 });
2569 }
2570 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
2571 ring.set_color(ring.get_color().multiply_alpha(opacity));
2572 let ring_w: f32 = ring.get_width();
2573 let ring_rect: Rect = Rect::new(
2574 bounds.get_x() - ring_w,
2575 bounds.get_y() - ring_w,
2576 bounds.get_width() + ring_w * 2.0,
2577 bounds.get_height() + ring_w * 2.0,
2578 );
2579 display_list.push(DrawCommand::FillRoundedRect {
2580 rect: ring_rect,
2581 color: COLOR_TRANSPARENT,
2582 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
2583 border: ring,
2584 });
2585 }
2586 }
2587 if scratch.portals.is_some()
2588 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
2589 {
2590 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
2591 }
2592 return;
2593 }
2594 let child_count: usize = tree.child_count(node_id);
2595 if !self_visible && child_count == 0 {
2596 return;
2597 }
2598 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
2599 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
2600 if self_visible
2601 && (hover_visual || pressed)
2602 && paint_owned.is_none()
2603 && (hovered || node.hover_paint().is_none())
2604 {
2605 let base_paint: &PaintStyle = &style.paint;
2606 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2607 || layout.get_position() == Position::Fixed;
2608 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2609 && base_paint.background_color().is_none();
2610 if !is_scrim && !is_transparent_canvas {
2611 let is_interactive: bool = base_paint.get_cursor() == CursorIcon::Pointer
2612 || node
2613 .get_handlers()
2614 .handler_for(&UiEventKind::Click(Point::default()))
2615 .is_some();
2616 if is_interactive {
2617 let mut tinted: PaintStyle = *base_paint;
2618 tinted.set_background(Self::interactive_tint(
2619 tinted.background_color(),
2620 hover_visual,
2621 pressed,
2622 ));
2623 paint_owned = Some(tinted);
2624 }
2625 }
2626 }
2627 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2628 let phys_scale_milli: u32 = Self::quantize_scale_milli(scale);
2629 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
2630 let frag_overflow_ok: bool = layout.get_overflow() == Overflow::Hidden
2631 || (layout.get_overflow() == Overflow::Visible
2632 && node_id != tree.get_root()
2633 && child_count >= 2);
2634 if self_visible
2635 && scratch.fragments.is_some()
2636 && scratch.portals.is_some()
2637 && matches!(node.get_kind(), NodeKind::View)
2638 && frag_overflow_ok
2639 && !hovered
2640 && !pressed
2641 && !interaction.get_focused()
2642 && active_hover_target != Some(node_id)
2643 {
2644 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
2645 && self_vis_rect.right() <= cull_rect.right();
2646 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
2647 && self_vis_rect.bottom() <= cull_rect.bottom();
2648 if vis_x_in && vis_y_in {
2649 let sel_node: Option<NodeId> = tree
2650 .text_selection()
2651 .filter(|s: &SelectionState| !s.is_empty())
2652 .map(|s: SelectionState| s.get_node_id());
2653 let unclipped_cull: Option<(Point, Rect, f32, f32)> =
2654 if layout.get_overflow() == Overflow::Hidden {
2655 None
2656 } else {
2657 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
2658 let child_off: Point = Point::new(
2659 total_off_x - (raw_node_scroll.get_x() * scale).round(),
2660 total_off_y - (raw_node_scroll.get_y() * scale).round(),
2661 );
2662 Some((child_off, cull_rect, scale, inv_scale))
2663 };
2664 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_subtree(
2665 tree,
2666 node_id,
2667 node,
2668 unclipped_cull,
2669 (active_hover_target, sel_node),
2670 ) {
2671 let slot_idx: usize =
2672 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
2673 let opacity_bits: u32 = parent_opacity.to_bits();
2674 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
2675 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
2676 if let Some(cache) = scratch.fragments.as_deref_mut()
2677 && let Some(&cached) = cache.slots.get(slot_idx)
2678 && cached.occupied
2679 && cached.node_id == node_id
2680 && cached.phys_scale_milli == phys_scale_milli
2681 && cached.opacity_bits == opacity_bits
2682 && cached.geom_hash == geom_hash
2683 && cached.style_text_hash == style_text_hash
2684 {
2685 let dx_phys: f32 = offset_phys.get_x() - cached.offset_phys.get_x();
2686 let dy_phys: f32 = offset_phys.get_y() - cached.offset_phys.get_y();
2687 let c_start: usize = cached.cmd_start as usize;
2688 let c_end: usize = c_start.saturating_add(cached.cmd_len as usize);
2689 if let Some(cmds) = cache.cmd_slab.get(c_start..c_end) {
2690 display_list.get_mut_commands().reserve(cmds.len());
2691 let text_slab: &[u8] = cache.text_slab.as_slice();
2692 for cmd in cmds.iter() {
2693 Self::push_translated_command(
2694 cmd,
2695 text_slab,
2696 (dx_phys, dy_phys),
2697 (scale, inv_scale, dyadic_scale),
2698 display_list,
2699 );
2700 }
2701 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
2702 return;
2703 }
2704 }
2705 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
2706 }
2707 }
2708 }
2709 if self_visible {
2710 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
2711 if blur_r > 0.0 {
2712 display_list.push(DrawCommand::BackdropBlur {
2713 rect: bounds,
2714 radius: paint.get_radius(),
2715 blur_radius: blur_r,
2716 });
2717 }
2718 if let Some(mut shadow) = paint.box_shadow() {
2719 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2720 display_list.push(DrawCommand::BoxShadow {
2721 rect: bounds,
2722 radius: paint.get_radius(),
2723 shadow,
2724 });
2725 }
2726 if let Some(mut gradient) = paint.background_gradient() {
2727 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
2728 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
2729 display_list.push(DrawCommand::LinearGradientRect {
2730 rect: bounds,
2731 gradient,
2732 radius: paint.get_radius(),
2733 });
2734 } else if let Some(bg) = paint.background_color() {
2735 let mut border: Border = paint.get_border();
2736 border.set_color(border.get_color().multiply_alpha(opacity));
2737 display_list.push(DrawCommand::FillRoundedRect {
2738 rect: bounds,
2739 color: bg.multiply_alpha(opacity),
2740 radius: paint.get_radius(),
2741 border,
2742 });
2743 } else if paint.get_border().get_width() > 0.0 {
2744 let mut border: Border = paint.get_border();
2745 border.set_color(border.get_color().multiply_alpha(opacity));
2746 display_list.push(DrawCommand::FillRoundedRect {
2747 rect: bounds,
2748 color: COLOR_TRANSPARENT,
2749 radius: paint.get_radius(),
2750 border,
2751 });
2752 }
2753 if interaction.get_focused()
2754 && let Some(mut ring) = paint.focus_ring_border()
2755 {
2756 ring.set_color(ring.get_color().multiply_alpha(opacity));
2757 let ring_w: f32 = ring.get_width();
2758 let ring_rect: Rect = Rect::new(
2759 bounds.get_x() - ring_w,
2760 bounds.get_y() - ring_w,
2761 bounds.get_width() + ring_w * 2.0,
2762 bounds.get_height() + ring_w * 2.0,
2763 );
2764 display_list.push(DrawCommand::FillRoundedRect {
2765 rect: ring_rect,
2766 color: COLOR_TRANSPARENT,
2767 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
2768 border: ring,
2769 });
2770 }
2771 match node.get_kind() {
2772 NodeKind::Text(text) => {
2773 let mut text_style: TextStyle = style.text;
2774 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2775 let content_b: Rect = node.layout_box.get_content_bounds();
2776 let cx0: f32 = (content_b.get_x() * scale).round();
2777 let cy0: f32 = (content_b.get_y() * scale).round();
2778 let shifted_content: Rect = Rect::new(
2779 (cx0 + total_off_x) * inv_scale,
2780 (cy0 + total_off_y) * inv_scale,
2781 content_b.get_width(),
2782 content_b.get_height(),
2783 );
2784 if let Some(sel) = tree.text_selection()
2785 && sel.get_node_id() == node_id
2786 && !sel.is_empty()
2787 {
2788 let sel_color: Color = tree
2789 .effective_selection_color(node_id)
2790 .multiply_alpha(opacity);
2791 let sel_rects: Vec<Rect> = compute_text_selection_rects(
2792 text.as_str(),
2793 text_style,
2794 shifted_content,
2795 sel.ordered_range(),
2796 );
2797 for sel_rect in sel_rects.into_iter() {
2798 display_list.push(DrawCommand::FillRoundedRect {
2799 rect: sel_rect,
2800 color: sel_color,
2801 radius: CornerRadius::all(2.0),
2802 border: Border::default(),
2803 });
2804 }
2805 }
2806 let pooled_text: String = display_list.alloc_text(text.as_str());
2807 display_list.push(DrawCommand::TextRun {
2808 rect: shifted_content,
2809 text: pooled_text,
2810 style: text_style,
2811 });
2812 }
2813 NodeKind::Input(input_state) => {
2814 let mut text_style: TextStyle = style.text;
2815 let is_empty_value: bool = input_state.get_value().is_empty();
2816 let is_focused: bool = interaction.get_focused();
2817 let rendered_text: String = if is_empty_value {
2818 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
2819 display_list.alloc_text(input_state.get_placeholder().as_str())
2820 } else if input_state.get_password() {
2821 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2822 let star_count: usize = input_state.get_value().chars().count();
2823 let mut masked: String = display_list.alloc_text("");
2824 for _ in 0..star_count {
2825 masked.push('*');
2826 }
2827 masked
2828 } else {
2829 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2830 display_list.alloc_text(input_state.get_value().as_str())
2831 };
2832 let content_b: Rect = node.layout_box.get_content_bounds();
2833 let cx0: f32 = (content_b.get_x() * scale).round();
2834 let cy0: f32 = (content_b.get_y() * scale).round();
2835 let shifted_content: Rect = Rect::new(
2836 (cx0 + total_off_x) * inv_scale,
2837 (cy0 + total_off_y) * inv_scale,
2838 content_b.get_width(),
2839 content_b.get_height(),
2840 );
2841 let line_h: f32 =
2842 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
2843 let text_rect: Rect = if input_state.get_multiline() {
2844 shifted_content
2845 } else {
2846 text_style.set_overflow(TextOverflow::Clip);
2847 let v_off_phys: f32 =
2848 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
2849 .round();
2850 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
2851 Rect::new(
2852 shifted_content.get_x(),
2853 text_y,
2854 shifted_content.get_width(),
2855 line_h,
2856 )
2857 };
2858 let run_rect: Rect =
2859 if is_empty_value && is_focused && !rendered_text.is_empty() {
2860 let clearance_phys: f32 =
2861 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
2862 let clearance_logical: f32 = clearance_phys * inv_scale;
2863 Rect::new(
2864 text_rect.get_x() + clearance_logical,
2865 text_rect.get_y(),
2866 (text_rect.get_width() - clearance_logical).max(0.0),
2867 text_rect.get_height(),
2868 )
2869 } else {
2870 text_rect
2871 };
2872 let show_caret: bool = is_focused && tree.get_caret_visible();
2873 let caret_text: String = if show_caret && !is_empty_value {
2874 display_list.alloc_text(rendered_text.as_str())
2875 } else {
2876 String::new()
2877 };
2878 display_list.push(DrawCommand::PushClip(shifted_content));
2879 display_list.push(DrawCommand::TextRun {
2880 rect: run_rect,
2881 text: rendered_text,
2882 style: text_style,
2883 });
2884 if show_caret {
2885 let caret_h: f32 = (text_style.get_font_size() * 1.15)
2886 .min(shifted_content.get_height().max(12.0));
2887 display_list.push(DrawCommand::TextCaret {
2888 text_rect,
2889 content_rect: shifted_content,
2890 text: caret_text,
2891 style: text_style,
2892 caret_height: caret_h,
2893 color: style.text.get_color().multiply_alpha(opacity),
2894 multiline: input_state.get_multiline(),
2895 });
2896 }
2897 display_list.push(DrawCommand::PopClip);
2898 }
2899 NodeKind::Image(buffer) => {
2900 display_list.push(DrawCommand::Image {
2901 rect: bounds,
2902 buffer: buffer.clone(),
2903 radius: paint.get_radius(),
2904 });
2905 }
2906 NodeKind::Canvas(caching) => {
2907 if let Some(callback) = node.canvas_callback()
2908 && let Ok(mut func) = callback.try_borrow_mut()
2909 {
2910 let canvas_bw: f32 = paint.get_border().get_width();
2911 let canvas_clip_rect: Rect = if canvas_bw > 0.0 {
2912 Rect::new(
2913 bounds.get_x() + canvas_bw,
2914 bounds.get_y() + canvas_bw,
2915 (bounds.get_width() - canvas_bw * 2.0).max(0.0),
2916 (bounds.get_height() - canvas_bw * 2.0).max(0.0),
2917 )
2918 } else {
2919 bounds
2920 };
2921 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
2922 .min(bounds.get_height() * 0.5)
2923 .max(0.0);
2924 let canvas_raw_r: CornerRadius = paint.get_radius();
2925 let canvas_inner_r: CornerRadius = CornerRadius::new(
2926 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_bw).max(0.0),
2927 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_bw).max(0.0),
2928 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_bw)
2929 .max(0.0),
2930 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_bw).max(0.0),
2931 );
2932 if canvas_inner_r.max_radius() > 0.25 {
2933 display_list.push(DrawCommand::PushRoundedClip {
2934 rect: canvas_clip_rect,
2935 radius: canvas_inner_r,
2936 });
2937 } else {
2938 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
2939 }
2940 let canvas_cmd_start: usize = display_list.len();
2941 func(bounds, display_list);
2942 let canvas_cmd_end: usize = display_list.len();
2943 if *caching != CanvasCaching::RetainedStatic {
2944 let canvas_is_dynamic: bool =
2945 if let Some(cache) = scratch.fragments.as_deref_mut() {
2946 let emitted: &[DrawCommand] = &display_list.get_commands()
2947 [canvas_cmd_start..canvas_cmd_end];
2948 cache.record_canvas_output(
2949 node_id,
2950 phys_scale_milli,
2951 bounds,
2952 tree.get_revision(),
2953 emitted,
2954 )
2955 } else {
2956 true
2957 };
2958 if canvas_is_dynamic {
2959 *scratch.saw_canvas = true;
2960 }
2961 }
2962 display_list.push(DrawCommand::PopClip);
2963 } else if *caching == CanvasCaching::DynamicPerFrame {
2964 *scratch.saw_canvas = true;
2965 }
2966 }
2967 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
2968 }
2969 }
2970 let vp: Size = display_list.get_viewport();
2971 let show_scrollbar: bool = vp.get_width().min(vp.get_height()) >= 540.0;
2972 let wants_scrollbar: bool = show_scrollbar
2973 && layout.get_overflow() == Overflow::Scroll
2974 && bounds.get_height() > 24.0;
2975 let emit_clip: bool = clipped && (child_count > 0 || wants_scrollbar);
2976 let clip_push_idx: usize = display_list.len();
2977 let clip_saved_digest: (u64, u64) = display_list.snapshot_digest_state();
2978 if emit_clip {
2979 let bw: f32 = paint.get_border().get_width();
2980 let clip_rect: Rect = if bw > 0.0 {
2981 Rect::new(
2982 bounds.get_x() + bw,
2983 bounds.get_y() + bw,
2984 (bounds.get_width() - bw * 2.0).max(0.0),
2985 (bounds.get_height() - bw * 2.0).max(0.0),
2986 )
2987 } else {
2988 bounds
2989 };
2990 let max_r: f32 = (bounds.get_width() * 0.5)
2991 .min(bounds.get_height() * 0.5)
2992 .max(0.0);
2993 let raw_r: CornerRadius = paint.get_radius();
2994 let inner_r: CornerRadius = CornerRadius::new(
2995 (raw_r.get_top_left().min(max_r) - bw).max(0.0),
2996 (raw_r.get_top_right().min(max_r) - bw).max(0.0),
2997 (raw_r.get_bottom_right().min(max_r) - bw).max(0.0),
2998 (raw_r.get_bottom_left().min(max_r) - bw).max(0.0),
2999 );
3000 if inner_r.max_radius() > 0.25 {
3001 display_list.push(DrawCommand::PushRoundedClip {
3002 rect: clip_rect,
3003 radius: inner_r,
3004 });
3005 } else {
3006 display_list.push(DrawCommand::PushClip(clip_rect));
3007 }
3008 }
3009 if child_count > 0 {
3010 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3011 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
3012 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
3013 let child_offset_phys: Point =
3014 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
3015 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
3016 if !needs_z_sort {
3017 for child_id in tree.children_iter(node_id) {
3018 Self::emit_node(
3019 tree,
3020 child_id,
3021 (child_offset_phys, opacity),
3022 next_cull,
3023 active_hover_target,
3024 scratch,
3025 display_list,
3026 );
3027 }
3028 } else {
3029 let sort_start: usize = scratch.z_sort.len();
3030 for (idx, cid) in tree.children_iter(node_id).enumerate() {
3031 if let Some(c) = tree.get_node(cid) {
3032 scratch
3033 .z_sort
3034 .push((c.style_ref().layout.get_z_index(), idx, cid));
3035 }
3036 }
3037 let sort_end: usize = scratch.z_sort.len();
3038 scratch.z_sort[sort_start..sort_end].sort_by(
3039 |a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
3040 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
3041 },
3042 );
3043 for slot_idx in sort_start..sort_end {
3044 let child_id: NodeId = scratch.z_sort[slot_idx].2;
3045 Self::emit_node(
3046 tree,
3047 child_id,
3048 (child_offset_phys, opacity),
3049 next_cull,
3050 active_hover_target,
3051 scratch,
3052 display_list,
3053 );
3054 }
3055 scratch.z_sort.truncate(sort_start);
3056 }
3057 }
3058 if wants_scrollbar {
3059 let lbox: &LayoutBox = &node.layout_box;
3060 let border_w: f32 = paint.get_border().get_width();
3061 let inner_x: f32 = bounds.get_x() + border_w;
3062 let inner_y: f32 = bounds.get_y() + border_w;
3063 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
3064 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
3065 let view_w: f32 = lbox.get_content_bounds().get_width().max(1.0);
3066 let view_h: f32 = lbox.get_content_bounds().get_height().max(1.0);
3067 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
3068 Some((vy, vh)) => (vy, vh),
3069 None => (
3070 lbox.get_scroll_offset().get_y(),
3071 lbox.get_content_size().get_height(),
3072 ),
3073 };
3074 let scroll_x: f32 = lbox.get_scroll_offset().get_x();
3075 let total_w: f32 = lbox.get_content_size().get_width();
3076 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
3077 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
3078 let has_v_bar: bool = max_scroll_y > 1.0;
3079 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
3080 let corner_inset: f32 = if has_v_bar && has_h_bar { 8.0 } else { 0.0 };
3081 if has_v_bar {
3082 let track_margin: f32 = 3.0;
3083 let track_w: f32 = if hovered { 6.0 } else { 4.0 };
3084 let track_x: f32 = (inner_x + inner_w - track_w - 2.0).max(inner_x);
3085 let track_y: f32 = inner_y + track_margin;
3086 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
3087 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
3088 let min_thumb_h: f32 = 20.0_f32.min(track_h);
3089 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
3090 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
3091 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
3092 let track_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3093 let track_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3094 let track_w_snapped: f32 = (track_w * scale).round().max(1.0) * inv_scale;
3095 let track_h_snapped: f32 = (track_h * scale).round().max(1.0) * inv_scale;
3096 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
3097 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3098 if hovered {
3099 display_list.push(DrawCommand::FillRoundedRect {
3100 rect: Rect::new(
3101 track_x_snapped,
3102 track_y_snapped,
3103 track_w_snapped,
3104 track_h_snapped,
3105 ),
3106 color: Color::rgba(113, 113, 122, 24).multiply_alpha(opacity),
3107 radius: CornerRadius::all(3.0),
3108 border: Border::default(),
3109 });
3110 }
3111 let thumb_alpha: u8 = if hovered { 180 } else { 112 };
3112 display_list.push(DrawCommand::FillRoundedRect {
3113 rect: Rect::new(
3114 track_x_snapped,
3115 thumb_y_snapped,
3116 track_w_snapped,
3117 thumb_h_snapped,
3118 ),
3119 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3120 radius: CornerRadius::all(track_w * 0.5),
3121 border: Border::default(),
3122 });
3123 }
3124 if has_h_bar {
3125 let track_margin: f32 = 3.0;
3126 let track_h: f32 = if hovered { 6.0 } else { 4.0 };
3127 let track_y: f32 = (inner_y + inner_h - track_h - 2.0).max(inner_y);
3128 let track_x: f32 = inner_x + track_margin;
3129 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
3130 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
3131 let min_thumb_w: f32 = 20.0_f32.min(track_w);
3132 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
3133 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
3134 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
3135 let track_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3136 let track_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3137 let track_w_snapped: f32 = (track_w * scale).round().max(1.0) * inv_scale;
3138 let track_h_snapped: f32 = (track_h * scale).round().max(1.0) * inv_scale;
3139 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
3140 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3141 if hovered {
3142 display_list.push(DrawCommand::FillRoundedRect {
3143 rect: Rect::new(
3144 track_x_snapped,
3145 track_y_snapped,
3146 track_w_snapped,
3147 track_h_snapped,
3148 ),
3149 color: Color::rgba(113, 113, 122, 24).multiply_alpha(opacity),
3150 radius: CornerRadius::all(3.0),
3151 border: Border::default(),
3152 });
3153 }
3154 let thumb_alpha: u8 = if hovered { 180 } else { 112 };
3155 display_list.push(DrawCommand::FillRoundedRect {
3156 rect: Rect::new(
3157 thumb_x_snapped,
3158 track_y_snapped,
3159 thumb_w_snapped,
3160 track_h_snapped,
3161 ),
3162 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3163 radius: CornerRadius::all(track_h * 0.5),
3164 border: Border::default(),
3165 });
3166 }
3167 }
3168 if emit_clip {
3169 if display_list.len() == clip_push_idx + 1 {
3170 display_list.pop_empty_clip(clip_saved_digest);
3171 } else {
3172 display_list.push(DrawCommand::PopClip);
3173 }
3174 }
3175 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
3176 && let Some(cache) = scratch.fragments.as_deref_mut()
3177 {
3178 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
3179 cache.capture_fragment(
3180 slot_idx,
3181 (
3182 node_id,
3183 phys_scale_milli,
3184 parent_opacity.to_bits(),
3185 offset_phys,
3186 geom_hash,
3187 style_text_hash,
3188 ),
3189 cmd_slice,
3190 );
3191 }
3192 }
3193}