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 #[cfg_attr(not(target_arch = "wasm32"), inline)]
2152 #[must_use]
2153 pub(crate) fn viewport_shows_desktop_scrollbars(viewport: Size) -> bool {
2154 viewport.get_width() >= DESKTOP_SCROLLBAR_MIN_WIDTH_DP
2155 && viewport.get_height() >= DESKTOP_SCROLLBAR_MIN_HEIGHT_DP
2156 }
2157
2158 pub fn build(
2168 tree: &SceneTree,
2169 viewport: Size,
2170 scale_factor: f32,
2171 clear_color: Color,
2172 display_list: &mut DisplayList,
2173 ) {
2174 let mut saw_canvas: bool = false;
2175 Self::build_inner(
2176 tree,
2177 viewport,
2178 scale_factor,
2179 clear_color,
2180 display_list,
2181 None,
2182 &mut saw_canvas,
2183 );
2184 }
2185
2186 #[inline(always)]
2196 #[must_use]
2197 fn pack_opt_node_id(node: Option<NodeId>) -> u64 {
2198 match node {
2199 Some(id) => ((id.get_index() as u64) << 32) | (id.get_generation() as u64),
2200 None => u64::MAX,
2201 }
2202 }
2203
2204 pub fn build_retained(
2216 tree: &SceneTree,
2217 viewport: Size,
2218 scale_factor: f32,
2219 clear_color: Color,
2220 layout_changed: bool,
2221 display_list: &mut DisplayList,
2222 cache: &mut PaintFragmentCache,
2223 ) {
2224 let clamped_scale: f32 = scale_factor.max(0.5);
2225 let focus: FocusManager = tree.get_focus();
2226 let sig: PaintSceneSignature = PaintSceneSignature {
2227 tree_ptr: (tree as *const SceneTree) as usize,
2228 tree_revision: tree.get_revision(),
2229 viewport_w_bits: viewport.get_width().to_bits(),
2230 viewport_h_bits: viewport.get_height().to_bits(),
2231 scale_bits: clamped_scale.to_bits(),
2232 clear_rgba: u32::from_le_bytes(clear_color.to_rgba_bytes()),
2233 focused_packed: Self::pack_opt_node_id(focus.focused_node()),
2234 hovered_packed: Self::pack_opt_node_id(focus.hovered_node()),
2235 pressed_packed: Self::pack_opt_node_id(focus.pressed_node()),
2236 caret_visible: tree.get_caret_visible(),
2237 text_sel_sig: Self::text_selection_sig(tree),
2238 scrollbars_suppressed: tree.get_scrollbars_suppressed(),
2239 };
2240 if !layout_changed
2241 && !cache.last_has_canvas
2242 && !display_list.is_empty()
2243 && cache.has_last_scene
2244 && cache.last_scene_sig == sig
2245 {
2246 cache.whole_list_hits = cache.whole_list_hits.wrapping_add(1);
2247 return;
2248 }
2249 cache.ensure_slots();
2250 let mut saw_canvas: bool = false;
2251 Self::build_inner(
2252 tree,
2253 viewport,
2254 clamped_scale,
2255 clear_color,
2256 display_list,
2257 Some(cache),
2258 &mut saw_canvas,
2259 );
2260 cache.last_has_canvas = saw_canvas;
2261 cache.has_last_scene = true;
2262 cache.last_scene_sig = sig;
2263 }
2264
2265 fn collect_portal_ancestors(tree: &SceneTree, out_ancestors: &mut Vec<NodeId>) {
2272 out_ancestors.clear();
2273 let portal_nodes: &[NodeId] = tree.portal_nodes();
2274 if portal_nodes.is_empty() {
2275 return;
2276 }
2277 for &pid in portal_nodes.iter() {
2278 let mut cur: Option<NodeId> = tree.get_node(pid).and_then(SceneNode::parent_id);
2279 while let Some(anc) = cur {
2280 if out_ancestors.contains(&anc) {
2281 break;
2282 }
2283 out_ancestors.push(anc);
2284 cur = tree.get_node(anc).and_then(SceneNode::parent_id);
2285 }
2286 }
2287 if out_ancestors.len() > 16 {
2288 out_ancestors.sort_unstable_by_key(|id: &NodeId| {
2289 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2290 });
2291 }
2292 }
2293
2294 #[inline]
2305 #[must_use]
2306 fn is_portal_ancestor(ancestors: &[NodeId], node_id: NodeId) -> bool {
2307 if ancestors.is_empty() {
2308 return false;
2309 }
2310 if ancestors.len() <= 16 {
2311 return ancestors.contains(&node_id);
2312 }
2313 let target: u64 = ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
2314 ancestors
2315 .binary_search_by_key(&target, |id: &NodeId| {
2316 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2317 })
2318 .is_ok()
2319 }
2320
2321 #[inline]
2337 #[must_use]
2338 fn conservative_self_vis_rect(
2339 node: &SceneNode,
2340 bounds: Rect,
2341 paint: &PaintStyle,
2342 focused: bool,
2343 total_off_phys: (f32, f32),
2344 scale: f32,
2345 inv_scale: f32,
2346 ) -> Rect {
2347 let mut min_x: f32 = bounds.get_x() - 1.0;
2348 let mut min_y: f32 = bounds.get_y() - 1.0;
2349 let mut max_x: f32 = bounds.right() + 1.0;
2350 let mut max_y: f32 = bounds.bottom() + 1.0;
2351 if let Some(shadow) = paint.box_shadow()
2352 && shadow.get_color().get_alpha() > 0
2353 {
2354 let spread: f32 = shadow.get_spread_radius().max(0.0);
2355 let blur_phys: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
2356 let sigma_phys: f32 = (blur_phys * 0.5).max(0.75);
2357 let blur_pad: f32 = (sigma_phys * 3.0).ceil() * inv_scale + 2.0 * inv_scale;
2358 let pad: f32 = spread + blur_pad;
2359 let sx0: f32 = bounds.get_x() + shadow.get_offset_x() - pad;
2360 let sy0: f32 = bounds.get_y() + shadow.get_offset_y() - pad;
2361 let sx1: f32 = bounds.right() + shadow.get_offset_x() + pad;
2362 let sy1: f32 = bounds.bottom() + shadow.get_offset_y() + pad;
2363 min_x = min_x.min(sx0);
2364 min_y = min_y.min(sy0);
2365 max_x = max_x.max(sx1);
2366 max_y = max_y.max(sy1);
2367 }
2368 if focused && let Some(ring) = paint.focus_ring_border() {
2369 let rw: f32 = ring.get_width().max(0.0) + 1.0;
2370 min_x = min_x.min(bounds.get_x() - rw);
2371 min_y = min_y.min(bounds.get_y() - rw);
2372 max_x = max_x.max(bounds.right() + rw);
2373 max_y = max_y.max(bounds.bottom() + rw);
2374 }
2375 if matches!(node.get_kind(), NodeKind::Text(_)) {
2376 let content_b: Rect = node.layout_box.get_content_bounds();
2377 let cx0: f32 = ((content_b.get_x() * scale).round() + total_off_phys.0) * inv_scale;
2378 let cy0: f32 = ((content_b.get_y() * scale).round() + total_off_phys.1) * inv_scale;
2379 let pad: f32 = node.style_ref().text.get_font_size() * 0.5 + 2.0;
2380 min_x = min_x.min(cx0 - pad);
2381 min_y = min_y.min(cy0 - pad);
2382 max_x = max_x.max(cx0 + content_b.get_width() + pad);
2383 max_y = max_y.max(cy0 + content_b.get_height() + pad);
2384 }
2385 Rect::new(
2386 min_x,
2387 min_y,
2388 (max_x - min_x).max(0.0),
2389 (max_y - min_y).max(0.0),
2390 )
2391 }
2392
2393 fn visit_culled_portal_child(
2402 tree: &SceneTree,
2403 child_id: NodeId,
2404 parent_opacity: f32,
2405 scratch: &mut PaintScratch<'_>,
2406 ) {
2407 let Some(child) = tree.get_node(child_id) else {
2408 return;
2409 };
2410 let style: &Style = child.style_ref();
2411 let layout: &LayoutStyle = &style.layout;
2412 if layout.get_display() == Display::None {
2413 return;
2414 }
2415 let is_portal: bool = matches!(child.get_kind(), NodeKind::Portal)
2416 || layout.get_position() == Position::Fixed;
2417 if is_portal {
2418 if let Some(portal_queue) = scratch.portals.as_deref_mut() {
2419 portal_queue.push((layout.get_z_index(), portal_queue.len(), child_id));
2420 }
2421 return;
2422 }
2423 if !Self::is_portal_ancestor(scratch.portal_ancestors, child_id) {
2424 return;
2425 }
2426 let hovered: bool = child.get_interaction().get_hovered();
2427 let paint_opacity: f32 = if hovered {
2428 child
2429 .hover_paint()
2430 .map(|hp: PaintStyle| hp.get_opacity())
2431 .unwrap_or_else(|| style.paint.get_opacity())
2432 } else {
2433 style.paint.get_opacity()
2434 };
2435 let next_opacity: f32 = (parent_opacity * paint_opacity).clamp(0.0, 1.0);
2436 if next_opacity <= 0.0 {
2437 return;
2438 }
2439 Self::collect_portals_in_culled_subtree(tree, child_id, next_opacity, scratch);
2440 }
2441
2442 fn collect_portals_in_culled_subtree(
2451 tree: &SceneTree,
2452 node_id: NodeId,
2453 parent_opacity: f32,
2454 scratch: &mut PaintScratch<'_>,
2455 ) {
2456 if tree.get_node(node_id).is_none() {
2457 return;
2458 }
2459 let child_count: usize = tree.child_count(node_id);
2460 if child_count == 0 {
2461 return;
2462 }
2463 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
2464 if !needs_z_sort {
2465 for child_id in tree.children_iter(node_id) {
2466 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2467 }
2468 } else {
2469 let sort_start: usize = scratch.z_sort.len();
2470 for (idx, cid) in tree.children_iter(node_id).enumerate() {
2471 if let Some(char_item) = tree.get_node(cid) {
2472 scratch
2473 .z_sort
2474 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
2475 }
2476 }
2477 let sort_end: usize = scratch.z_sort.len();
2478 scratch.z_sort[sort_start..sort_end].sort_by(
2479 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
2480 target_node_id
2481 .0
2482 .cmp(&element_id.0)
2483 .then_with(|| target_node_id.1.cmp(&element_id.1))
2484 },
2485 );
2486 for slot_idx in sort_start..sort_end {
2487 let child_id: NodeId = scratch.z_sort[slot_idx].2;
2488 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2489 }
2490 scratch.z_sort.truncate(sort_start);
2491 }
2492 }
2493
2494 fn build_inner(
2506 tree: &SceneTree,
2507 viewport: Size,
2508 scale_factor: f32,
2509 clear_color: Color,
2510 display_list: &mut DisplayList,
2511 mut fragments: Option<&mut PaintFragmentCache>,
2512 saw_canvas: &mut bool,
2513 ) {
2514 display_list.reset(viewport, scale_factor);
2515 display_list.push(DrawCommand::Clear(clear_color));
2516 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
2517 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
2518 let mut portals: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.portals_scratch);
2519 let mut z_sort: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.z_sort_scratch);
2520 let mut portal_ancestors: Vec<NodeId> =
2521 take(&mut display_list.arena.portal_ancestors_scratch);
2522 portals.clear();
2523 z_sort.clear();
2524 Self::collect_portal_ancestors(tree, &mut portal_ancestors);
2525 {
2526 let mut scratch: PaintScratch<'_> = PaintScratch {
2527 portals: Some(&mut portals),
2528 z_sort: &mut z_sort,
2529 portal_ancestors: portal_ancestors.as_slice(),
2530 fragments: fragments.as_deref_mut(),
2531 saw_canvas,
2532 };
2533 Self::emit_node(
2534 tree,
2535 tree.get_root(),
2536 (Point::new(0.0, 0.0), 1.0),
2537 viewport_rect,
2538 active_hover_target,
2539 &mut scratch,
2540 display_list,
2541 );
2542 }
2543 display_list.finalize_base_layer(display_list.len());
2544 if portals.len() > 1 {
2545 portals.sort_by(
2546 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
2547 target_node_id
2548 .0
2549 .cmp(&element_id.0)
2550 .then_with(|| target_node_id.1.cmp(&element_id.1))
2551 },
2552 );
2553 }
2554 {
2555 let mut portal_scratch: PaintScratch<'_> = PaintScratch {
2556 portals: None,
2557 z_sort: &mut z_sort,
2558 portal_ancestors: &[],
2559 fragments,
2560 saw_canvas,
2561 };
2562 for &(_, _, portal_id) in portals.iter() {
2563 Self::emit_node(
2564 tree,
2565 portal_id,
2566 (Point::new(0.0, 0.0), 1.0),
2567 viewport_rect,
2568 active_hover_target,
2569 &mut portal_scratch,
2570 display_list,
2571 );
2572 }
2573 }
2574 portals.clear();
2575 z_sort.clear();
2576 portal_ancestors.clear();
2577 display_list.arena.portals_scratch = portals;
2578 display_list.arena.z_sort_scratch = z_sort;
2579 display_list.arena.portal_ancestors_scratch = portal_ancestors;
2580 }
2581
2582 #[must_use]
2592 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
2593 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
2594 while let Some(id) = cursor {
2595 let Some(node) = tree.get_node(id) else {
2596 break;
2597 };
2598 let style: &Style = node.style_ref();
2599 let paint: &PaintStyle = &style.paint;
2600 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2601 || style.layout.get_position() == Position::Fixed;
2602 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2603 && paint.background_color().is_none();
2604 if !is_scrim && !is_transparent_canvas {
2605 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
2606 || node
2607 .get_handlers()
2608 .handler_for(&UiEventKind::Click(Point::default()))
2609 .is_some();
2610 if is_interactive {
2611 return Some(id);
2612 }
2613 }
2614 cursor = node.parent_id();
2615 }
2616 None
2617 }
2618
2619 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
2631 if !hovered && !pressed {
2632 return base;
2633 }
2634 let delta: i32 = if pressed { 24 } else { 14 };
2635 match base {
2636 Some(char_item) if char_item.get_alpha() > 0 => {
2637 let luma: u32 = (char_item.get_red() as u32) * 299
2638 + (char_item.get_green() as u32) * 587
2639 + (char_item.get_blue() as u32) * 114;
2640 if luma < 120_000 {
2641 let lift: i32 = if pressed { 30 } else { 20 };
2642 Some(Color::new(
2643 (char_item.get_red() as i32 + lift).clamp(0, 255) as u8,
2644 (char_item.get_green() as i32 + lift).clamp(0, 255) as u8,
2645 (char_item.get_blue() as i32 + lift).clamp(0, 255) as u8,
2646 char_item.get_alpha(),
2647 ))
2648 } else {
2649 Some(Color::new(
2650 (char_item.get_red() as i32 - delta).clamp(0, 255) as u8,
2651 (char_item.get_green() as i32 - delta).clamp(0, 255) as u8,
2652 (char_item.get_blue() as i32 - delta).clamp(0, 255) as u8,
2653 char_item.get_alpha(),
2654 ))
2655 }
2656 }
2657 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
2658 }
2659 }
2660
2661 fn emit_node(
2673 tree: &SceneTree,
2674 node_id: NodeId,
2675 offset_and_opacity: (Point, f32),
2676 cull_rect: Rect,
2677 active_hover_target: Option<NodeId>,
2678 scratch: &mut PaintScratch<'_>,
2679 display_list: &mut DisplayList,
2680 ) {
2681 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
2682 let Some(node) = tree.get_node(node_id) else {
2683 return;
2684 };
2685 let style: &Style = node.style_ref();
2686 let layout: &LayoutStyle = &style.layout;
2687 if layout.get_display() == Display::None {
2688 return;
2689 }
2690 let is_portal_or_fixed: bool =
2691 matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
2692 let is_root_chrome_overlay: bool = layout.get_position() == Position::Absolute
2693 && layout.get_z_index() >= 50
2694 && layout.get_z_index() < 140
2695 && offset_phys == Point::new(0.0, 0.0)
2696 && (parent_opacity - 1.0).abs() <= 0.001
2697 && cull_rect.get_x() <= 0.0
2698 && cull_rect.get_y() <= 0.0;
2699 let promote_to_top_layer: bool = is_portal_or_fixed || is_root_chrome_overlay;
2700 if let Some(portal_queue) = scratch.portals.as_deref_mut()
2701 && promote_to_top_layer
2702 {
2703 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
2704 return;
2705 }
2706 let interaction: InteractionState = node.get_interaction();
2707 let hovered: bool = interaction.get_hovered();
2708 let pressed: bool = interaction.get_pressed();
2709 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
2710 return;
2711 }
2712 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
2713 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2714 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
2715 if opacity <= 0.0 {
2716 return;
2717 }
2718 let scale: f32 = display_list.get_scale_factor().max(0.5);
2719 let inv_scale: f32 = 1.0 / scale;
2720 let raw_bounds: Rect = node.layout_box.get_bounds();
2721 let transform: Transform2D = prelim_paint.get_transform();
2722 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
2723 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
2724 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
2725 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
2726 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
2727 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
2728 let raw_x1: f32 = (raw_bounds.right() * scale).round();
2729 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
2730 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
2731 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
2732 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
2733 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
2734 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
2735 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
2736 let bounds: Rect = Rect::new(
2737 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
2738 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
2739 scaled_w_phys * inv_scale,
2740 scaled_h_phys * inv_scale,
2741 );
2742 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2743 let clipped_cull: Option<Rect> = if clipped {
2744 cull_rect.intersect(bounds)
2745 } else {
2746 Some(cull_rect)
2747 };
2748 let focused: bool = interaction.get_focused();
2749 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
2750 node,
2751 bounds,
2752 prelim_paint,
2753 focused,
2754 (total_off_x, total_off_y),
2755 scale,
2756 inv_scale,
2757 );
2758 let self_visible: bool = bounds.get_width() > 0.0
2759 && bounds.get_height() > 0.0
2760 && self_vis_rect.intersect(cull_rect).is_some();
2761 if clipped && clipped_cull.is_none() {
2762 if self_visible {
2763 if let Some(mut shadow) = prelim_paint.box_shadow() {
2764 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2765 display_list.push(DrawCommand::BoxShadow {
2766 rect: bounds,
2767 radius: prelim_paint.get_radius(),
2768 shadow,
2769 });
2770 }
2771 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
2772 ring.set_color(ring.get_color().multiply_alpha(opacity));
2773 let ring_w: f32 = ring.get_width();
2774 let ring_rect: Rect = Rect::new(
2775 bounds.get_x() - ring_w,
2776 bounds.get_y() - ring_w,
2777 bounds.get_width() + ring_w * 2.0,
2778 bounds.get_height() + ring_w * 2.0,
2779 );
2780 display_list.push(DrawCommand::FillRoundedRect {
2781 rect: ring_rect,
2782 color: COLOR_TRANSPARENT,
2783 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
2784 border: ring,
2785 });
2786 }
2787 }
2788 if scratch.portals.is_some()
2789 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
2790 {
2791 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
2792 }
2793 return;
2794 }
2795 let child_count: usize = tree.child_count(node_id);
2796 if !self_visible && child_count == 0 {
2797 return;
2798 }
2799 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
2800 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
2801 if self_visible
2802 && (hover_visual || pressed)
2803 && paint_owned.is_none()
2804 && (hovered || node.hover_paint().is_none())
2805 {
2806 let base_paint: &PaintStyle = &style.paint;
2807 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2808 || layout.get_position() == Position::Fixed;
2809 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2810 && base_paint.background_color().is_none();
2811 if !is_scrim && !is_transparent_canvas {
2812 let is_interactive: bool = base_paint.get_cursor() == CursorIcon::Pointer
2813 || node
2814 .get_handlers()
2815 .handler_for(&UiEventKind::Click(Point::default()))
2816 .is_some();
2817 if is_interactive {
2818 let mut tinted: PaintStyle = *base_paint;
2819 tinted.set_background(Self::interactive_tint(
2820 tinted.background_color(),
2821 hover_visual,
2822 pressed,
2823 ));
2824 paint_owned = Some(tinted);
2825 }
2826 }
2827 }
2828 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2829 let phys_scale_milli: u32 = Self::quantize_scale_milli(scale);
2830 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
2831 let frag_overflow_ok: bool = layout.get_overflow() == Overflow::Hidden
2832 || (layout.get_overflow() == Overflow::Visible
2833 && node_id != tree.get_root()
2834 && child_count >= 2);
2835 if self_visible
2836 && scratch.fragments.is_some()
2837 && scratch.portals.is_some()
2838 && matches!(node.get_kind(), NodeKind::View)
2839 && frag_overflow_ok
2840 && !hovered
2841 && !pressed
2842 && !interaction.get_focused()
2843 && active_hover_target != Some(node_id)
2844 {
2845 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
2846 && self_vis_rect.right() <= cull_rect.right();
2847 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
2848 && self_vis_rect.bottom() <= cull_rect.bottom();
2849 if vis_x_in && vis_y_in {
2850 let has_text_sel: bool = !tree.text_selection_spans_ref().is_empty();
2851 let unclipped_cull: Option<(Point, Rect, f32, f32)> =
2852 if layout.get_overflow() == Overflow::Hidden {
2853 None
2854 } else {
2855 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
2856 let child_off: Point = Point::new(
2857 total_off_x - (raw_node_scroll.get_x() * scale).round(),
2858 total_off_y - (raw_node_scroll.get_y() * scale).round(),
2859 );
2860 Some((child_off, cull_rect, scale, inv_scale))
2861 };
2862 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_subtree(
2863 tree,
2864 node_id,
2865 node,
2866 unclipped_cull,
2867 (active_hover_target, has_text_sel),
2868 ) {
2869 let slot_idx: usize =
2870 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
2871 let opacity_bits: u32 = parent_opacity.to_bits();
2872 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
2873 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
2874 if let Some(cache) = scratch.fragments.as_deref_mut()
2875 && let Some(&cached) = cache.slots.get(slot_idx)
2876 && cached.occupied
2877 && cached.node_id == node_id
2878 && cached.phys_scale_milli == phys_scale_milli
2879 && cached.opacity_bits == opacity_bits
2880 && cached.geom_hash == geom_hash
2881 && cached.style_text_hash == style_text_hash
2882 {
2883 let dx_phys: f32 = offset_phys.get_x() - cached.offset_phys.get_x();
2884 let dy_phys: f32 = offset_phys.get_y() - cached.offset_phys.get_y();
2885 let c_start: usize = cached.cmd_start as usize;
2886 let c_end: usize = c_start.saturating_add(cached.cmd_len as usize);
2887 if let Some(cmds) = cache.cmd_slab.get(c_start..c_end) {
2888 display_list.get_mut_commands().reserve(cmds.len());
2889 let text_slab: &[u8] = cache.text_slab.as_slice();
2890 for cmd in cmds.iter() {
2891 Self::push_translated_command(
2892 cmd,
2893 text_slab,
2894 (dx_phys, dy_phys),
2895 (scale, inv_scale, dyadic_scale),
2896 display_list,
2897 );
2898 }
2899 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
2900 return;
2901 }
2902 }
2903 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
2904 }
2905 }
2906 }
2907 let box_size: Size = display_list.get_viewport();
2908 let show_scrollbar: bool =
2909 !tree.get_scrollbars_suppressed() && Self::viewport_shows_desktop_scrollbars(box_size);
2910 let wants_scrollbar: bool = show_scrollbar
2911 && layout.get_overflow() == Overflow::Scroll
2912 && bounds.get_height() > 24.0;
2913 let emit_clip: bool = clipped && (child_count > 0 || wants_scrollbar);
2914 let has_canvas_clip: bool =
2915 matches!(node.get_kind(), NodeKind::Canvas(_)) && node.canvas_callback().is_some();
2916 let has_rounded_border: bool =
2917 paint.get_border().get_width() > 0.0 && paint.get_radius().max_radius() > 0.0;
2918 let defer_rounded_border: bool = has_rounded_border && (emit_clip || has_canvas_clip);
2919 if self_visible {
2920 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
2921 if blur_r > 0.0 {
2922 display_list.push(DrawCommand::BackdropBlur {
2923 rect: bounds,
2924 radius: paint.get_radius(),
2925 blur_radius: blur_r,
2926 });
2927 }
2928 if let Some(mut shadow) = paint.box_shadow() {
2929 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2930 display_list.push(DrawCommand::BoxShadow {
2931 rect: bounds,
2932 radius: paint.get_radius(),
2933 shadow,
2934 });
2935 }
2936 if let Some(mut gradient) = paint.background_gradient() {
2937 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
2938 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
2939 display_list.push(DrawCommand::LinearGradientRect {
2940 rect: bounds,
2941 gradient,
2942 radius: paint.get_radius(),
2943 });
2944 } else if let Some(bg) = paint.background_color() {
2945 let border: Border = if defer_rounded_border {
2946 Border::default()
2947 } else {
2948 let mut direct_border: Border = paint.get_border();
2949 direct_border.set_color(direct_border.get_color().multiply_alpha(opacity));
2950 direct_border
2951 };
2952 display_list.push(DrawCommand::FillRoundedRect {
2953 rect: bounds,
2954 color: bg.multiply_alpha(opacity),
2955 radius: paint.get_radius(),
2956 border,
2957 });
2958 } else if paint.get_border().get_width() > 0.0 && !defer_rounded_border {
2959 let mut border: Border = paint.get_border();
2960 border.set_color(border.get_color().multiply_alpha(opacity));
2961 display_list.push(DrawCommand::FillRoundedRect {
2962 rect: bounds,
2963 color: COLOR_TRANSPARENT,
2964 radius: paint.get_radius(),
2965 border,
2966 });
2967 }
2968 if interaction.get_focused()
2969 && let Some(mut ring) = paint.focus_ring_border()
2970 {
2971 ring.set_color(ring.get_color().multiply_alpha(opacity));
2972 let ring_w: f32 = ring.get_width();
2973 let ring_rect: Rect = Rect::new(
2974 bounds.get_x() - ring_w,
2975 bounds.get_y() - ring_w,
2976 bounds.get_width() + ring_w * 2.0,
2977 bounds.get_height() + ring_w * 2.0,
2978 );
2979 display_list.push(DrawCommand::FillRoundedRect {
2980 rect: ring_rect,
2981 color: COLOR_TRANSPARENT,
2982 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
2983 border: ring,
2984 });
2985 }
2986 match node.get_kind() {
2987 NodeKind::Text(text) => {
2988 let mut text_style: TextStyle = style.text;
2989 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2990 let content_b: Rect = node.layout_box.get_content_bounds();
2991 let cx0: f32 = (content_b.get_x() * scale).round();
2992 let cy0: f32 = (content_b.get_y() * scale).round();
2993 let shifted_content: Rect = Rect::new(
2994 (cx0 + total_off_x) * inv_scale,
2995 (cy0 + total_off_y) * inv_scale,
2996 content_b.get_width(),
2997 content_b.get_height(),
2998 );
2999 let has_box_chrome: bool = style
3000 .paint
3001 .background_color()
3002 .is_some_and(|color_value: Color| color_value.get_alpha() > 0)
3003 || style.paint.background_gradient().is_some()
3004 || style.paint.get_border().get_width() > 0.0
3005 || style.layout.get_height() != Length::Auto
3006 || style.layout.get_min_height() != Length::Auto;
3007 let text_run_rect: Rect = if has_box_chrome
3008 && shifted_content.get_height() > 1.0
3009 {
3010 let cached_h: f32 = node.layout_box.get_content_size().get_height();
3011 let measured_h: f32 =
3012 if cached_h > 0.0 && cached_h + 0.5 < shifted_content.get_height() {
3013 cached_h
3014 } else {
3015 DeterministicTextMeasurer
3016 .measure_size(
3017 text.as_str(),
3018 text_style,
3019 Some(shifted_content.get_width().max(1.0)),
3020 )
3021 .get_height()
3022 };
3023 if measured_h > 0.0 && shifted_content.get_height() > measured_h + 0.5 {
3024 let v_off_phys: f32 =
3025 (((shifted_content.get_height() - measured_h) * 0.5).max(0.0)
3026 * scale)
3027 .round();
3028 Rect::new(
3029 shifted_content.get_x(),
3030 (cy0 + total_off_y + v_off_phys) * inv_scale,
3031 shifted_content.get_width(),
3032 measured_h,
3033 )
3034 } else {
3035 shifted_content
3036 }
3037 } else {
3038 shifted_content
3039 };
3040 if let Some(sel_range) = tree.text_selection_range_for(node_id) {
3041 let sel_color: Color = tree
3042 .effective_selection_color(node_id)
3043 .multiply_alpha(opacity);
3044 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3045 text.as_str(),
3046 text_style,
3047 text_run_rect,
3048 sel_range,
3049 );
3050 let sel_single_to_end: bool = sel_rects.len() == 1
3051 && text_style.get_align() == TextAlign::Left
3052 && sel_range.1 >= text.chars().count();
3053 let exact_span_right: f32 =
3054 ((content_b.right() * scale).round() + total_off_x) * inv_scale;
3055 for sel_rect in sel_rects.into_iter() {
3056 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3057 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3058 let sx1: f32 = if sel_single_to_end {
3059 exact_span_right.max(sx0 + inv_scale)
3060 } else {
3061 (sel_rect.right() * scale).round() * inv_scale
3062 };
3063 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3064 display_list.push(DrawCommand::FillRoundedRect {
3065 rect: Rect::new(
3066 sx0,
3067 sy0,
3068 (sx1 - sx0).max(inv_scale),
3069 (sy1 - sy0).max(inv_scale),
3070 ),
3071 color: sel_color,
3072 radius: CornerRadius::default(),
3073 border: Border::default(),
3074 });
3075 }
3076 }
3077 let pooled_text: String = display_list.alloc_text(text.as_str());
3078 display_list.push(DrawCommand::TextRun {
3079 rect: text_run_rect,
3080 text: pooled_text,
3081 style: text_style,
3082 });
3083 }
3084 NodeKind::Input(input_state) => {
3085 let mut text_style: TextStyle = style.text;
3086 let is_empty_value: bool = input_state.get_value().is_empty();
3087 let is_focused: bool = interaction.get_focused();
3088 let rendered_text: String = if is_empty_value {
3089 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
3090 display_list.alloc_text(input_state.get_placeholder().as_str())
3091 } else if input_state.get_password() {
3092 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3093 let star_count: usize = input_state.get_value().chars().count();
3094 let mut masked: String = display_list.alloc_text("");
3095 for _ in 0..star_count {
3096 masked.push('*');
3097 }
3098 masked
3099 } else {
3100 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3101 display_list.alloc_text(input_state.get_value().as_str())
3102 };
3103 let content_b: Rect = node.layout_box.get_content_bounds();
3104 let cx0: f32 = (content_b.get_x() * scale).round();
3105 let cy0: f32 = (content_b.get_y() * scale).round();
3106 let shifted_content: Rect = Rect::new(
3107 (cx0 + total_off_x) * inv_scale,
3108 (cy0 + total_off_y) * inv_scale,
3109 content_b.get_width(),
3110 content_b.get_height(),
3111 );
3112 let line_h: f32 =
3113 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
3114 let text_rect: Rect = if input_state.get_multiline() {
3115 shifted_content
3116 } else {
3117 text_style.set_overflow(TextOverflow::Clip);
3118 let v_off_phys: f32 =
3119 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
3120 .round();
3121 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
3122 Rect::new(
3123 shifted_content.get_x(),
3124 text_y,
3125 shifted_content.get_width(),
3126 line_h,
3127 )
3128 };
3129 let run_rect: Rect =
3130 if is_empty_value && is_focused && !rendered_text.is_empty() {
3131 let clearance_phys: f32 =
3132 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
3133 let clearance_logical: f32 = clearance_phys * inv_scale;
3134 Rect::new(
3135 text_rect.get_x() + clearance_logical,
3136 text_rect.get_y(),
3137 (text_rect.get_width() - clearance_logical).max(0.0),
3138 text_rect.get_height(),
3139 )
3140 } else {
3141 text_rect
3142 };
3143 let show_caret: bool = is_focused && tree.get_caret_visible();
3144 let caret_text: String = if show_caret && !is_empty_value {
3145 let caret_prefix: &str =
3146 input_state.caret_prefix_for_display(rendered_text.as_str());
3147 display_list.alloc_text(caret_prefix)
3148 } else {
3149 String::new()
3150 };
3151 display_list.push(DrawCommand::PushClip(shifted_content));
3152 if is_focused
3153 && !is_empty_value
3154 && let Some(sel_range) = input_state.selection_char_range()
3155 {
3156 let sel_color: Color = tree
3157 .effective_selection_color(node_id)
3158 .multiply_alpha(opacity);
3159 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3160 rendered_text.as_str(),
3161 text_style,
3162 text_rect,
3163 sel_range,
3164 );
3165 for sel_rect in sel_rects.into_iter() {
3166 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3167 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3168 let sx1: f32 = (sel_rect.right() * scale).round() * inv_scale;
3169 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3170 display_list.push(DrawCommand::FillRoundedRect {
3171 rect: Rect::new(
3172 sx0,
3173 sy0,
3174 (sx1 - sx0).max(inv_scale),
3175 (sy1 - sy0).max(inv_scale),
3176 ),
3177 color: sel_color,
3178 radius: CornerRadius::default(),
3179 border: Border::default(),
3180 });
3181 }
3182 }
3183 display_list.push(DrawCommand::TextRun {
3184 rect: run_rect,
3185 text: rendered_text,
3186 style: text_style,
3187 });
3188 if show_caret {
3189 let caret_h: f32 = (text_style.get_font_size() * 1.15)
3190 .min(shifted_content.get_height().max(12.0));
3191 display_list.push(DrawCommand::TextCaret {
3192 text_rect,
3193 content_rect: shifted_content,
3194 text: caret_text,
3195 style: text_style,
3196 caret_height: caret_h,
3197 color: style.text.get_color().multiply_alpha(opacity),
3198 multiline: input_state.get_multiline(),
3199 });
3200 }
3201 display_list.push(DrawCommand::PopClip);
3202 }
3203 NodeKind::Image(buffer) => {
3204 display_list.push(DrawCommand::Image {
3205 rect: bounds,
3206 buffer: buffer.clone(),
3207 radius: paint.get_radius(),
3208 });
3209 }
3210 NodeKind::Canvas(caching) => {
3211 if let Some(callback) = node.canvas_callback()
3212 && let Ok(mut func) = callback.try_borrow_mut()
3213 {
3214 let canvas_bw: f32 = paint.get_border().get_width();
3215 let canvas_inset: f32 = if canvas_bw > 0.0 {
3216 ((canvas_bw * scale).ceil().max(1.0)) * inv_scale
3217 } else {
3218 0.0
3219 };
3220 let canvas_clip_rect: Rect = if canvas_inset > 0.0 {
3221 Rect::new(
3222 bounds.get_x() + canvas_inset,
3223 bounds.get_y() + canvas_inset,
3224 (bounds.get_width() - canvas_inset * 2.0).max(0.0),
3225 (bounds.get_height() - canvas_inset * 2.0).max(0.0),
3226 )
3227 } else {
3228 bounds
3229 };
3230 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
3231 .min(bounds.get_height() * 0.5)
3232 .max(0.0);
3233 let canvas_raw_r: CornerRadius = paint.get_radius();
3234 let canvas_inner_r: CornerRadius = CornerRadius::new(
3235 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_inset).max(0.0),
3236 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_inset)
3237 .max(0.0),
3238 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_inset)
3239 .max(0.0),
3240 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_inset)
3241 .max(0.0),
3242 );
3243 if canvas_inner_r.max_radius() > 0.25 {
3244 display_list.push(DrawCommand::PushRoundedClip {
3245 rect: canvas_clip_rect,
3246 radius: canvas_inner_r,
3247 });
3248 } else {
3249 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
3250 }
3251 let canvas_cmd_start: usize = display_list.len();
3252 func(bounds, display_list);
3253 let canvas_cmd_end: usize = display_list.len();
3254 if opacity < 0.999 {
3255 display_list.attenuate_range_opacity(
3256 canvas_cmd_start,
3257 canvas_cmd_end,
3258 opacity,
3259 );
3260 }
3261 if *caching != CanvasCaching::RetainedStatic {
3262 let canvas_is_dynamic: bool =
3263 if let Some(cache) = scratch.fragments.as_deref_mut() {
3264 let emitted: &[DrawCommand] = &display_list.get_commands()
3265 [canvas_cmd_start..canvas_cmd_end];
3266 cache.record_canvas_output(
3267 node_id,
3268 phys_scale_milli,
3269 bounds,
3270 tree.get_revision(),
3271 emitted,
3272 )
3273 } else {
3274 true
3275 };
3276 if canvas_is_dynamic {
3277 *scratch.saw_canvas = true;
3278 }
3279 }
3280 display_list.push(DrawCommand::PopClip);
3281 if canvas_bw > 0.0 && canvas_raw_r.max_radius() > 0.0 {
3282 let mut canvas_border: Border = paint.get_border();
3283 canvas_border
3284 .set_color(canvas_border.get_color().multiply_alpha(opacity));
3285 display_list.push(DrawCommand::FillRoundedRect {
3286 rect: bounds,
3287 color: COLOR_TRANSPARENT,
3288 radius: canvas_raw_r,
3289 border: canvas_border,
3290 });
3291 }
3292 } else if *caching == CanvasCaching::DynamicPerFrame {
3293 *scratch.saw_canvas = true;
3294 }
3295 }
3296 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
3297 }
3298 }
3299 let clip_push_idx: usize = display_list.len();
3300 let clip_saved_digest: (u64, u64) = display_list.snapshot_digest_state();
3301 if emit_clip {
3302 let border_width: f32 = paint.get_border().get_width();
3303 let border_inset: f32 = if border_width > 0.0 {
3304 ((border_width * scale).ceil().max(1.0)) * inv_scale
3305 } else {
3306 0.0
3307 };
3308 let clip_rect: Rect = if border_inset > 0.0 {
3309 Rect::new(
3310 bounds.get_x() + border_inset,
3311 bounds.get_y() + border_inset,
3312 (bounds.get_width() - border_inset * 2.0).max(0.0),
3313 (bounds.get_height() - border_inset * 2.0).max(0.0),
3314 )
3315 } else {
3316 bounds
3317 };
3318 let max_r: f32 = (bounds.get_width() * 0.5)
3319 .min(bounds.get_height() * 0.5)
3320 .max(0.0);
3321 let raw_r: CornerRadius = paint.get_radius();
3322 let inner_r: CornerRadius = CornerRadius::new(
3323 (raw_r.get_top_left().min(max_r) - border_inset).max(0.0),
3324 (raw_r.get_top_right().min(max_r) - border_inset).max(0.0),
3325 (raw_r.get_bottom_right().min(max_r) - border_inset).max(0.0),
3326 (raw_r.get_bottom_left().min(max_r) - border_inset).max(0.0),
3327 );
3328 if inner_r.max_radius() > 0.25 {
3329 display_list.push(DrawCommand::PushRoundedClip {
3330 rect: clip_rect,
3331 radius: inner_r,
3332 });
3333 } else {
3334 display_list.push(DrawCommand::PushClip(clip_rect));
3335 }
3336 }
3337 if child_count > 0 {
3338 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3339 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
3340 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
3341 let child_offset_phys: Point =
3342 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
3343 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
3344 if !needs_z_sort {
3345 for child_id in tree.children_iter(node_id) {
3346 Self::emit_node(
3347 tree,
3348 child_id,
3349 (child_offset_phys, opacity),
3350 next_cull,
3351 active_hover_target,
3352 scratch,
3353 display_list,
3354 );
3355 }
3356 } else {
3357 let sort_start: usize = scratch.z_sort.len();
3358 for (idx, cid) in tree.children_iter(node_id).enumerate() {
3359 if let Some(char_item) = tree.get_node(cid) {
3360 scratch
3361 .z_sort
3362 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
3363 }
3364 }
3365 let sort_end: usize = scratch.z_sort.len();
3366 scratch.z_sort[sort_start..sort_end].sort_by(
3367 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
3368 target_node_id
3369 .0
3370 .cmp(&element_id.0)
3371 .then_with(|| target_node_id.1.cmp(&element_id.1))
3372 },
3373 );
3374 for slot_idx in sort_start..sort_end {
3375 let child_id: NodeId = scratch.z_sort[slot_idx].2;
3376 Self::emit_node(
3377 tree,
3378 child_id,
3379 (child_offset_phys, opacity),
3380 next_cull,
3381 active_hover_target,
3382 scratch,
3383 display_list,
3384 );
3385 }
3386 scratch.z_sort.truncate(sort_start);
3387 }
3388 }
3389 if wants_scrollbar {
3390 let computed_box: &LayoutBox = &node.layout_box;
3391 let border_w: f32 = paint.get_border().get_width();
3392 let inner_x: f32 = bounds.get_x() + border_w;
3393 let inner_y: f32 = bounds.get_y() + border_w;
3394 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
3395 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
3396 let view_w: f32 = computed_box.get_content_bounds().get_width().max(1.0);
3397 let view_h: f32 = computed_box.get_content_bounds().get_height().max(1.0);
3398 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
3399 Some((vy, vh)) => (vy, vh),
3400 None => (
3401 computed_box.get_scroll_offset().get_y(),
3402 computed_box.get_content_size().get_height(),
3403 ),
3404 };
3405 let scroll_x: f32 = computed_box.get_scroll_offset().get_x();
3406 let total_w: f32 = computed_box.get_content_size().get_width();
3407 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
3408 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
3409 let has_v_bar: bool = max_scroll_y > 1.0;
3410 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
3411 let corner_inset: f32 = if has_v_bar && has_h_bar { 6.0 } else { 0.0 };
3412 if has_v_bar {
3413 let track_margin: f32 = 3.0;
3414 let edge_inset: f32 = 1.0;
3415 let thumb_w: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3416 let track_x: f32 = (inner_x + inner_w - thumb_w - edge_inset).max(inner_x);
3417 let track_y: f32 = inner_y + track_margin;
3418 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
3419 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
3420 let min_thumb_h: f32 = 20.0_f32.min(track_h);
3421 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
3422 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
3423 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
3424 let thumb_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3425 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3426 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
3427 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3428 let thumb_alpha: u8 = if pressed {
3429 188
3430 } else if hovered {
3431 148
3432 } else {
3433 88
3434 };
3435 display_list.push(DrawCommand::FillRoundedRect {
3436 rect: Rect::new(
3437 thumb_x_snapped,
3438 thumb_y_snapped,
3439 thumb_w_snapped,
3440 thumb_h_snapped,
3441 ),
3442 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3443 radius: CornerRadius::all(thumb_w * 0.5),
3444 border: Border::default(),
3445 });
3446 }
3447 if has_h_bar {
3448 let track_margin: f32 = 3.0;
3449 let edge_inset: f32 = 1.0;
3450 let thumb_h: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3451 let track_y: f32 = (inner_y + inner_h - thumb_h - edge_inset).max(inner_y);
3452 let track_x: f32 = inner_x + track_margin;
3453 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
3454 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
3455 let min_thumb_w: f32 = 20.0_f32.min(track_w);
3456 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
3457 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
3458 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
3459 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
3460 let thumb_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3461 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3462 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3463 let thumb_alpha: u8 = if pressed {
3464 188
3465 } else if hovered {
3466 148
3467 } else {
3468 88
3469 };
3470 display_list.push(DrawCommand::FillRoundedRect {
3471 rect: Rect::new(
3472 thumb_x_snapped,
3473 thumb_y_snapped,
3474 thumb_w_snapped,
3475 thumb_h_snapped,
3476 ),
3477 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3478 radius: CornerRadius::all(thumb_h * 0.5),
3479 border: Border::default(),
3480 });
3481 }
3482 }
3483 if emit_clip {
3484 if display_list.len() == clip_push_idx + 1 {
3485 display_list.pop_empty_clip(clip_saved_digest);
3486 if self_visible && defer_rounded_border && !has_canvas_clip {
3487 let mut outer_border: Border = paint.get_border();
3488 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3489 display_list.push(DrawCommand::FillRoundedRect {
3490 rect: bounds,
3491 color: COLOR_TRANSPARENT,
3492 radius: paint.get_radius(),
3493 border: outer_border,
3494 });
3495 }
3496 } else {
3497 display_list.push(DrawCommand::PopClip);
3498 if self_visible && has_rounded_border && !has_canvas_clip {
3499 let mut outer_border: Border = paint.get_border();
3500 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3501 display_list.push(DrawCommand::FillRoundedRect {
3502 rect: bounds,
3503 color: COLOR_TRANSPARENT,
3504 radius: paint.get_radius(),
3505 border: outer_border,
3506 });
3507 }
3508 }
3509 }
3510 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
3511 && let Some(cache) = scratch.fragments.as_deref_mut()
3512 {
3513 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
3514 cache.capture_fragment(
3515 slot_idx,
3516 (
3517 node_id,
3518 phys_scale_milli,
3519 parent_opacity.to_bits(),
3520 offset_phys,
3521 geom_hash,
3522 style_text_hash,
3523 ),
3524 cmd_slice,
3525 );
3526 }
3527 }
3528}