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 pub(crate) fn sanitize_clip_balance(&mut self, start: usize, saved_digest: (u64, u64)) {
392 let len_now: usize = self.get_commands().len();
393 if start >= len_now {
394 return;
395 }
396 let mut depth: usize = 0;
397 let mut has_underflow: bool = false;
398 for cmd in self.get_commands()[start..].iter() {
399 match cmd {
400 DrawCommand::PushClip(_) | DrawCommand::PushRoundedClip { .. } => {
401 depth = depth.saturating_add(1);
402 }
403 DrawCommand::PopClip => {
404 if depth == 0 {
405 has_underflow = true;
406 } else {
407 depth -= 1;
408 }
409 }
410 _ => {}
411 }
412 }
413 if has_underflow {
414 let Self {
415 commands, arena, ..
416 } = self;
417 let mut write_idx: usize = start;
418 let mut local_depth: usize = 0;
419 for read_idx in start..commands.len() {
420 let keep: bool = match &commands[read_idx] {
421 DrawCommand::PushClip(_) | DrawCommand::PushRoundedClip { .. } => {
422 local_depth = local_depth.saturating_add(1);
423 true
424 }
425 DrawCommand::PopClip => {
426 if local_depth == 0 {
427 false
428 } else {
429 local_depth -= 1;
430 true
431 }
432 }
433 _ => true,
434 };
435 if keep {
436 if write_idx != read_idx {
437 commands.swap(write_idx, read_idx);
438 }
439 write_idx += 1;
440 }
441 }
442 commands.truncate(write_idx);
443 arena.running_fast_hash = saved_digest.0;
444 if !arena.base_layer_finalized {
445 arena.cur_base_digest = saved_digest.1;
446 }
447 for cmd in commands[start..].iter() {
448 Self::fold_command_fast(&mut arena.running_fast_hash, cmd);
449 if !arena.base_layer_finalized {
450 Self::fold_command_full(&mut arena.cur_base_digest, cmd);
451 }
452 }
453 depth = local_depth;
454 }
455 while depth > 0 {
456 self.push(DrawCommand::PopClip);
457 depth -= 1;
458 }
459 }
460
461 #[inline]
468 pub(crate) fn finalize_base_layer(&mut self, split_index: usize) {
469 let Self {
470 portal_split_index,
471 arena,
472 ..
473 } = self;
474 *portal_split_index = split_index;
475 Self::mix_hash(&mut arena.cur_base_digest, split_index as u64);
476 if arena.base_layer_version == 0 || arena.cur_base_digest != arena.prev_base_digest {
477 arena.base_layer_version = arena.base_layer_version.wrapping_add(1).max(1);
478 }
479 arena.base_layer_finalized = true;
480 }
481
482 #[inline]
492 #[must_use]
493 pub fn base_layer_version(&self) -> u64 {
494 let Self { arena, .. } = self;
495 arena.base_layer_version
496 }
497
498 #[must_use]
508 pub fn len(&self) -> usize {
509 self.get_commands().len()
510 }
511
512 #[must_use]
522 pub fn is_empty(&self) -> bool {
523 self.get_commands().is_empty()
524 }
525
526 #[inline(always)]
533 fn mix_hash(hash: &mut u64, value: u64) {
534 *hash ^= value;
535 *hash = hash.wrapping_mul(FNV1A_PRIME);
536 }
537
538 #[inline]
545 fn fold_command_fast(hash: &mut u64, cmd: &DrawCommand) {
546 match cmd {
547 DrawCommand::Clear(char_item) => {
548 Self::mix_hash(hash, 1);
549 Self::mix_hash(hash, u32::from_le_bytes(char_item.to_rgba_bytes()) as u64);
550 }
551 DrawCommand::FillRoundedRect {
552 rect,
553 color,
554 border,
555 ..
556 } => {
557 Self::mix_hash(hash, 2);
558 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
559 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
560 Self::mix_hash(hash, rect.get_width().to_bits() as u64);
561 Self::mix_hash(hash, rect.get_height().to_bits() as u64);
562 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
563 Self::mix_hash(hash, border.get_width().to_bits() as u64);
564 }
565 DrawCommand::LinearGradientRect { rect, gradient, .. } => {
566 Self::mix_hash(hash, 3);
567 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
568 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
569 Self::mix_hash(
570 hash,
571 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
572 );
573 }
574 DrawCommand::BoxShadow { rect, shadow, .. } => {
575 Self::mix_hash(hash, 4);
576 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
577 Self::mix_hash(hash, shadow.get_blur_radius().to_bits() as u64);
578 }
579 DrawCommand::BackdropBlur {
580 rect, blur_radius, ..
581 } => {
582 Self::mix_hash(hash, 15);
583 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
584 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
585 Self::mix_hash(hash, blur_radius.to_bits() as u64);
586 }
587 DrawCommand::TextRun { rect, text, style } => {
588 Self::mix_hash(hash, 5);
589 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
590 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
591 Self::mix_hash(hash, text.len() as u64);
592 let bytes: &[u8] = text.as_bytes();
593 let prefix_len: usize = bytes.len().min(16);
594 for &byte in bytes[..prefix_len].iter() {
595 Self::mix_hash(hash, byte as u64);
596 }
597 Self::mix_hash(
598 hash,
599 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
600 );
601 }
602 DrawCommand::TextCaret {
603 text_rect,
604 text,
605 color,
606 ..
607 } => {
608 Self::mix_hash(hash, 13);
609 Self::mix_hash(hash, text_rect.get_x().to_bits() as u64);
610 Self::mix_hash(hash, text_rect.get_y().to_bits() as u64);
611 Self::mix_hash(hash, text.len() as u64);
612 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
613 }
614 DrawCommand::Image { rect, buffer, .. } => {
615 Self::mix_hash(hash, 6);
616 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
617 Self::mix_hash(hash, buffer.get_width() as u64);
618 Self::mix_hash(hash, buffer.get_height() as u64);
619 }
620 DrawCommand::SharedImage {
621 rect,
622 width,
623 height,
624 ..
625 } => {
626 Self::mix_hash(hash, 6);
627 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
628 Self::mix_hash(hash, *width as u64);
629 Self::mix_hash(hash, *height as u64);
630 }
631 DrawCommand::Line {
632 from,
633 to,
634 color,
635 width,
636 } => {
637 Self::mix_hash(hash, 7);
638 Self::mix_hash(hash, from.get_x().to_bits() as u64);
639 Self::mix_hash(hash, to.get_y().to_bits() as u64);
640 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
641 Self::mix_hash(hash, width.to_bits() as u64);
642 }
643 DrawCommand::Circle {
644 center,
645 radius,
646 color,
647 } => {
648 Self::mix_hash(hash, 8);
649 Self::mix_hash(hash, center.get_x().to_bits() as u64);
650 Self::mix_hash(hash, center.get_y().to_bits() as u64);
651 Self::mix_hash(hash, radius.to_bits() as u64);
652 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
653 }
654 DrawCommand::Triangle { p0, p1, p2, color } => {
655 Self::mix_hash(hash, 9);
656 Self::mix_hash(hash, p0.get_x().to_bits() as u64);
657 Self::mix_hash(hash, p1.get_y().to_bits() as u64);
658 Self::mix_hash(hash, p2.get_x().to_bits() as u64);
659 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
660 }
661 DrawCommand::SpriteRegion {
662 rect,
663 src_rect,
664 tint,
665 ..
666 } => {
667 Self::mix_hash(hash, 10);
668 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
669 Self::mix_hash(hash, src_rect.get_x().to_bits() as u64);
670 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
671 }
672 DrawCommand::ExternalSurface { rect, radius } => {
673 Self::mix_hash(hash, 15);
674 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
675 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
676 Self::mix_hash(hash, rect.get_width().to_bits() as u64);
677 Self::mix_hash(hash, rect.get_height().to_bits() as u64);
678 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
679 }
680 DrawCommand::PushClip(red_val) => {
681 Self::mix_hash(hash, 11);
682 Self::mix_hash(hash, red_val.get_x().to_bits() as u64);
683 Self::mix_hash(hash, red_val.get_y().to_bits() as u64);
684 }
685 DrawCommand::PushRoundedClip { rect, radius } => {
686 Self::mix_hash(hash, 14);
687 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
688 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
689 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
690 }
691 DrawCommand::PopClip => {
692 Self::mix_hash(hash, 12);
693 }
694 }
695 }
696
697 #[inline]
704 fn fold_bytes_wordwise(hash: &mut u64, bytes: &[u8]) {
705 Self::mix_hash(hash, bytes.len() as u64);
706 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
707 for chunk in chunks.iter() {
708 Self::mix_hash(hash, u64::from_ne_bytes(*chunk));
709 }
710 if !rem.is_empty() {
711 let mut tail: u64 = 0;
712 for (idx, &blue_val) in rem.iter().enumerate() {
713 tail |= (blue_val as u64) << (idx * 8);
714 }
715 Self::mix_hash(hash, tail);
716 }
717 }
718
719 #[inline]
726 pub(crate) fn fold_command_full(hash: &mut u64, cmd: &DrawCommand) {
727 match cmd {
728 DrawCommand::Clear(char_item) => {
729 Self::mix_hash(hash, 1);
730 Self::mix_hash(hash, u32::from_le_bytes(char_item.to_rgba_bytes()) as u64);
731 }
732 DrawCommand::FillRoundedRect {
733 rect,
734 color,
735 radius,
736 border,
737 } => {
738 Self::mix_hash(hash, 2);
739 Self::mix_hash(
740 hash,
741 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
742 );
743 Self::mix_hash(
744 hash,
745 ((rect.get_width().to_bits() as u64) << 32)
746 | (rect.get_height().to_bits() as u64),
747 );
748 Self::mix_hash(
749 hash,
750 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
751 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64),
752 );
753 Self::mix_hash(
754 hash,
755 ((radius.get_top_left().to_bits() as u64) << 32)
756 | (radius.get_top_right().to_bits() as u64),
757 );
758 Self::mix_hash(
759 hash,
760 ((radius.get_bottom_right().to_bits() as u64) << 32)
761 | (radius.get_bottom_left().to_bits() as u64),
762 );
763 Self::mix_hash(hash, border.get_width().to_bits() as u64);
764 }
765 DrawCommand::LinearGradientRect {
766 rect,
767 gradient,
768 radius,
769 } => {
770 Self::mix_hash(hash, 3);
771 Self::mix_hash(
772 hash,
773 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
774 );
775 Self::mix_hash(
776 hash,
777 ((rect.get_width().to_bits() as u64) << 32)
778 | (rect.get_height().to_bits() as u64),
779 );
780 Self::mix_hash(
781 hash,
782 ((u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64) << 32)
783 | (u32::from_le_bytes(gradient.get_end_color().to_rgba_bytes()) as u64),
784 );
785 Self::mix_hash(
786 hash,
787 ((gradient.get_angle_deg().to_bits() as u64) << 32)
788 | (radius.max_radius().to_bits() as u64),
789 );
790 }
791 DrawCommand::BoxShadow {
792 rect,
793 radius,
794 shadow,
795 } => {
796 Self::mix_hash(hash, 4);
797 Self::mix_hash(
798 hash,
799 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
800 );
801 Self::mix_hash(
802 hash,
803 ((rect.get_width().to_bits() as u64) << 32)
804 | (rect.get_height().to_bits() as u64),
805 );
806 Self::mix_hash(
807 hash,
808 ((shadow.get_offset_x().to_bits() as u64) << 32)
809 | (shadow.get_offset_y().to_bits() as u64),
810 );
811 Self::mix_hash(
812 hash,
813 ((shadow.get_blur_radius().to_bits() as u64) << 32)
814 | (shadow.get_spread_radius().to_bits() as u64),
815 );
816 Self::mix_hash(
817 hash,
818 ((radius.max_radius().to_bits() as u64) << 32)
819 | (u32::from_le_bytes(shadow.get_color().to_rgba_bytes()) as u64),
820 );
821 }
822 DrawCommand::BackdropBlur {
823 rect,
824 radius,
825 blur_radius,
826 } => {
827 Self::mix_hash(hash, 15);
828 Self::mix_hash(
829 hash,
830 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
831 );
832 Self::mix_hash(
833 hash,
834 ((rect.get_width().to_bits() as u64) << 32)
835 | (rect.get_height().to_bits() as u64),
836 );
837 Self::mix_hash(
838 hash,
839 ((radius.max_radius().to_bits() as u64) << 32) | (blur_radius.to_bits() as u64),
840 );
841 }
842 DrawCommand::TextRun { rect, text, style } => {
843 Self::mix_hash(hash, 5);
844 Self::mix_hash(
845 hash,
846 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
847 );
848 Self::mix_hash(
849 hash,
850 ((rect.get_width().to_bits() as u64) << 32)
851 | (rect.get_height().to_bits() as u64),
852 );
853 let style_flags: u64 = (style.get_family() as u64)
854 | ((style.get_weight() as u64) << 8)
855 | ((style.get_align() as u64) << 16)
856 | (u64::from(style.get_italic()) << 24)
857 | ((u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64) << 32);
858 Self::mix_hash(hash, style_flags);
859 Self::mix_hash(hash, style.get_font_size().to_bits() as u64);
860 Self::fold_bytes_wordwise(hash, text.as_bytes());
861 }
862 DrawCommand::TextCaret {
863 text_rect,
864 content_rect,
865 text,
866 style,
867 caret_height,
868 color,
869 multiline,
870 } => {
871 Self::mix_hash(hash, 13);
872 Self::mix_hash(
873 hash,
874 ((text_rect.get_x().to_bits() as u64) << 32)
875 | (text_rect.get_y().to_bits() as u64),
876 );
877 Self::mix_hash(
878 hash,
879 ((content_rect.get_width().to_bits() as u64) << 32)
880 | (caret_height.to_bits() as u64),
881 );
882 Self::mix_hash(
883 hash,
884 ((style.get_font_size().to_bits() as u64) << 32)
885 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64)
886 ^ u64::from(*multiline),
887 );
888 Self::fold_bytes_wordwise(hash, text.as_bytes());
889 }
890 DrawCommand::Image {
891 rect,
892 buffer,
893 radius,
894 } => {
895 Self::mix_hash(hash, 6);
896 Self::mix_hash(
897 hash,
898 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
899 );
900 Self::mix_hash(
901 hash,
902 ((rect.get_width().to_bits() as u64) << 32)
903 | (rect.get_height().to_bits() as u64),
904 );
905 Self::mix_hash(
906 hash,
907 ((buffer.get_width() as u64) << 32) | (buffer.get_height() as u64),
908 );
909 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
910 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
911 }
912 DrawCommand::SharedImage {
913 rect,
914 width,
915 height,
916 pixels,
917 radius,
918 } => {
919 Self::mix_hash(hash, 6);
920 Self::mix_hash(
921 hash,
922 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
923 );
924 Self::mix_hash(
925 hash,
926 ((rect.get_width().to_bits() as u64) << 32)
927 | (rect.get_height().to_bits() as u64),
928 );
929 Self::mix_hash(hash, ((*width as u64) << 32) | (*height as u64));
930 Self::mix_hash(hash, pixels.as_ptr() as usize as u64);
931 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
932 }
933 DrawCommand::Line {
934 from,
935 to,
936 color,
937 width,
938 } => {
939 Self::mix_hash(hash, 7);
940 Self::mix_hash(
941 hash,
942 ((from.get_x().to_bits() as u64) << 32) | (from.get_y().to_bits() as u64),
943 );
944 Self::mix_hash(
945 hash,
946 ((to.get_x().to_bits() as u64) << 32) | (to.get_y().to_bits() as u64),
947 );
948 Self::mix_hash(
949 hash,
950 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
951 | (width.to_bits() as u64),
952 );
953 }
954 DrawCommand::Circle {
955 center,
956 radius,
957 color,
958 } => {
959 Self::mix_hash(hash, 8);
960 Self::mix_hash(
961 hash,
962 ((center.get_x().to_bits() as u64) << 32) | (center.get_y().to_bits() as u64),
963 );
964 Self::mix_hash(
965 hash,
966 ((radius.to_bits() as u64) << 32)
967 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64),
968 );
969 }
970 DrawCommand::Triangle { p0, p1, p2, color } => {
971 Self::mix_hash(hash, 9);
972 Self::mix_hash(
973 hash,
974 ((p0.get_x().to_bits() as u64) << 32) | (p0.get_y().to_bits() as u64),
975 );
976 Self::mix_hash(
977 hash,
978 ((p1.get_x().to_bits() as u64) << 32) | (p1.get_y().to_bits() as u64),
979 );
980 Self::mix_hash(
981 hash,
982 ((p2.get_x().to_bits() as u64) << 32) | (p2.get_y().to_bits() as u64),
983 );
984 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
985 }
986 DrawCommand::SpriteRegion {
987 rect,
988 buffer,
989 src_rect,
990 tint,
991 } => {
992 Self::mix_hash(hash, 10);
993 Self::mix_hash(
994 hash,
995 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
996 );
997 Self::mix_hash(
998 hash,
999 ((rect.get_width().to_bits() as u64) << 32)
1000 | (rect.get_height().to_bits() as u64),
1001 );
1002 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
1003 Self::mix_hash(
1004 hash,
1005 ((src_rect.get_x().to_bits() as u64) << 32)
1006 | (src_rect.get_y().to_bits() as u64),
1007 );
1008 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
1009 }
1010 DrawCommand::ExternalSurface { rect, radius } => {
1011 Self::mix_hash(hash, 15);
1012 Self::mix_hash(
1013 hash,
1014 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
1015 );
1016 Self::mix_hash(
1017 hash,
1018 ((rect.get_width().to_bits() as u64) << 32)
1019 | (rect.get_height().to_bits() as u64),
1020 );
1021 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
1022 }
1023 DrawCommand::PushClip(red_val) => {
1024 Self::mix_hash(hash, 11);
1025 Self::mix_hash(
1026 hash,
1027 ((red_val.get_x().to_bits() as u64) << 32) | (red_val.get_y().to_bits() as u64),
1028 );
1029 Self::mix_hash(
1030 hash,
1031 ((red_val.get_width().to_bits() as u64) << 32)
1032 | (red_val.get_height().to_bits() as u64),
1033 );
1034 }
1035 DrawCommand::PushRoundedClip { rect, radius } => {
1036 Self::mix_hash(hash, 14);
1037 Self::mix_hash(
1038 hash,
1039 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
1040 );
1041 Self::mix_hash(
1042 hash,
1043 ((rect.get_width().to_bits() as u64) << 32)
1044 | (rect.get_height().to_bits() as u64),
1045 );
1046 Self::mix_hash(
1047 hash,
1048 ((radius.get_top_left().to_bits() as u64) << 32)
1049 | (radius.get_top_right().to_bits() as u64),
1050 );
1051 Self::mix_hash(
1052 hash,
1053 ((radius.get_bottom_right().to_bits() as u64) << 32)
1054 | (radius.get_bottom_left().to_bits() as u64),
1055 );
1056 }
1057 DrawCommand::PopClip => {
1058 Self::mix_hash(hash, 12);
1059 }
1060 }
1061 }
1062
1063 #[inline]
1073 #[must_use]
1074 pub fn fast_hash(&self) -> u64 {
1075 let Self {
1076 commands, arena, ..
1077 } = self;
1078 let mut hash: u64 = arena.running_fast_hash;
1079 Self::mix_hash(&mut hash, commands.len() as u64);
1080 hash
1081 }
1082
1083 #[inline]
1093 #[must_use]
1094 pub fn base_layer_hash(&self) -> u64 {
1095 let Self {
1096 commands, arena, ..
1097 } = self;
1098 if arena.base_layer_finalized {
1099 return arena.base_layer_version.max(1);
1100 }
1101 let mut hash: u64 = arena.cur_base_digest;
1102 Self::mix_hash(&mut hash, commands.len() as u64);
1103 if hash == 0 { 1 } else { hash }
1104 }
1105}
1106
1107impl PaintFragmentCache {
1109 pub fn clear(&mut self) {
1115 let Self {
1116 has_last_scene,
1117 last_has_canvas,
1118 slots,
1119 cmd_slab,
1120 text_slab,
1121 canvas_stability,
1122 ..
1123 } = self;
1124 *has_last_scene = false;
1125 *last_has_canvas = false;
1126 slots.fill(RetainedPaintFragment::default());
1127 cmd_slab.clear();
1128 text_slab.clear();
1129 for entry in canvas_stability.iter_mut() {
1130 *entry = CanvasStabilityEntry::default();
1131 }
1132 }
1133
1134 #[must_use]
1144 pub fn stats(&self) -> (u64, u64, u64, u64) {
1145 (
1146 self.get_whole_list_hits(),
1147 self.get_fragment_hits(),
1148 self.get_fragment_misses(),
1149 self.get_fragment_evictions(),
1150 )
1151 }
1152
1153 #[inline]
1159 fn ensure_slots(&mut self) {
1160 let Self {
1161 slots,
1162 cmd_slab,
1163 text_slab,
1164 ..
1165 } = self;
1166 if slots.len() != PAINT_FRAGMENT_CACHE_SLOTS {
1167 slots.resize(PAINT_FRAGMENT_CACHE_SLOTS, RetainedPaintFragment::default());
1168 }
1169 if cmd_slab.capacity() < PAINT_FRAGMENT_CMD_SLAB_CAP {
1170 cmd_slab.reserve_exact(PAINT_FRAGMENT_CMD_SLAB_CAP - cmd_slab.len());
1171 }
1172 if text_slab.capacity() < PAINT_FRAGMENT_TEXT_SLAB_CAP {
1173 text_slab.reserve_exact(PAINT_FRAGMENT_TEXT_SLAB_CAP - text_slab.len());
1174 }
1175 }
1176
1177 #[inline]
1190 fn encode_cached_command(
1191 cmd: &DrawCommand,
1192 text_dst: &mut [u8],
1193 text_cursor: &mut usize,
1194 text_base: usize,
1195 ) -> CachedDrawCommand {
1196 match cmd {
1197 DrawCommand::Clear(char_item) => CachedDrawCommand::Clear(*char_item),
1198 DrawCommand::FillRoundedRect {
1199 rect,
1200 color,
1201 radius,
1202 border,
1203 } => CachedDrawCommand::FillRoundedRect {
1204 rect: *rect,
1205 color: *color,
1206 radius: *radius,
1207 border: *border,
1208 },
1209 DrawCommand::LinearGradientRect {
1210 rect,
1211 gradient,
1212 radius,
1213 } => CachedDrawCommand::LinearGradientRect {
1214 rect: *rect,
1215 gradient: *gradient,
1216 radius: *radius,
1217 },
1218 DrawCommand::BoxShadow {
1219 rect,
1220 radius,
1221 shadow,
1222 } => CachedDrawCommand::BoxShadow {
1223 rect: *rect,
1224 radius: *radius,
1225 shadow: *shadow,
1226 },
1227 DrawCommand::BackdropBlur {
1228 rect,
1229 radius,
1230 blur_radius,
1231 } => CachedDrawCommand::BackdropBlur {
1232 rect: *rect,
1233 radius: *radius,
1234 blur_radius: *blur_radius,
1235 },
1236 DrawCommand::TextRun { rect, text, style } => {
1237 let bytes: &[u8] = text.as_bytes();
1238 let start_rel: usize = *text_cursor;
1239 let end_rel: usize = start_rel.saturating_add(bytes.len());
1240 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1241 *text_cursor = end_rel;
1242 CachedDrawCommand::TextRun {
1243 rect: *rect,
1244 text_start: (text_base.saturating_add(start_rel)) as u32,
1245 text_len: bytes.len() as u32,
1246 style: *style,
1247 }
1248 }
1249 DrawCommand::TextCaret {
1250 text_rect,
1251 content_rect,
1252 text,
1253 style,
1254 caret_height,
1255 color,
1256 multiline,
1257 } => {
1258 let bytes: &[u8] = text.as_bytes();
1259 let start_rel: usize = *text_cursor;
1260 let end_rel: usize = start_rel.saturating_add(bytes.len());
1261 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1262 *text_cursor = end_rel;
1263 CachedDrawCommand::TextCaret {
1264 text_rect: *text_rect,
1265 content_rect: *content_rect,
1266 text_start: (text_base.saturating_add(start_rel)) as u32,
1267 text_len: bytes.len() as u32,
1268 style: *style,
1269 caret_height: *caret_height,
1270 color: *color,
1271 multiline: *multiline,
1272 }
1273 }
1274 DrawCommand::Image {
1275 rect,
1276 buffer,
1277 radius,
1278 } => CachedDrawCommand::Image {
1279 rect: *rect,
1280 buffer: buffer.clone(),
1281 radius: *radius,
1282 },
1283 DrawCommand::SharedImage {
1284 rect,
1285 width,
1286 height,
1287 pixels,
1288 radius,
1289 } => CachedDrawCommand::SharedImage {
1290 rect: *rect,
1291 width: *width,
1292 height: *height,
1293 pixels: Arc::clone(pixels),
1294 radius: *radius,
1295 },
1296 DrawCommand::Line {
1297 from,
1298 to,
1299 color,
1300 width,
1301 } => CachedDrawCommand::Line {
1302 from: *from,
1303 to: *to,
1304 color: *color,
1305 width: *width,
1306 },
1307 DrawCommand::Circle {
1308 center,
1309 radius,
1310 color,
1311 } => CachedDrawCommand::Circle {
1312 center: *center,
1313 radius: *radius,
1314 color: *color,
1315 },
1316 DrawCommand::Triangle { p0, p1, p2, color } => CachedDrawCommand::Triangle {
1317 p0: *p0,
1318 p1: *p1,
1319 p2: *p2,
1320 color: *color,
1321 },
1322 DrawCommand::SpriteRegion {
1323 rect,
1324 buffer,
1325 src_rect,
1326 tint,
1327 } => CachedDrawCommand::SpriteRegion {
1328 rect: *rect,
1329 buffer: buffer.clone(),
1330 src_rect: *src_rect,
1331 tint: *tint,
1332 },
1333 DrawCommand::ExternalSurface { rect, radius } => CachedDrawCommand::ExternalSurface {
1334 rect: *rect,
1335 radius: *radius,
1336 },
1337 DrawCommand::PushClip(rect) => CachedDrawCommand::PushClip(*rect),
1338 DrawCommand::PushRoundedClip { rect, radius } => CachedDrawCommand::PushRoundedClip {
1339 rect: *rect,
1340 radius: *radius,
1341 },
1342 DrawCommand::PopClip => CachedDrawCommand::PopClip,
1343 }
1344 }
1345
1346 fn capture_fragment(
1355 &mut self,
1356 slot_idx: usize,
1357 meta: (NodeId, u32, u32, Point, u64, u64),
1358 cmd_slice: &[DrawCommand],
1359 ) {
1360 let (node_id, phys_scale_milli, opacity_bits, offset_phys, geom_hash, style_text_hash): (
1361 NodeId,
1362 u32,
1363 u32,
1364 Point,
1365 u64,
1366 u64,
1367 ) = meta;
1368 let Self {
1369 slots,
1370 cmd_slab,
1371 text_slab,
1372 fragment_misses,
1373 fragment_evictions,
1374 ..
1375 } = self;
1376 if slot_idx >= slots.len() {
1377 return;
1378 }
1379 *fragment_misses = fragment_misses.wrapping_add(1);
1380 let need_cmds: usize = cmd_slice.len();
1381 let mut need_text: usize = 0;
1382 for cmd in cmd_slice.iter() {
1383 match cmd {
1384 DrawCommand::TextRun { text, .. } | DrawCommand::TextCaret { text, .. } => {
1385 need_text = need_text.saturating_add(text.len());
1386 }
1387 _ => {}
1388 }
1389 }
1390 if need_cmds > PAINT_FRAGMENT_CMD_SLAB_CAP || need_text > PAINT_FRAGMENT_TEXT_SLAB_CAP {
1391 return;
1392 }
1393 let existing: RetainedPaintFragment = slots[slot_idx];
1394 if existing.occupied && existing.node_id != node_id {
1395 *fragment_evictions = fragment_evictions.wrapping_add(1);
1396 }
1397 if existing.occupied
1398 && need_cmds <= (existing.cmd_cap as usize)
1399 && need_text <= (existing.text_cap as usize)
1400 {
1401 let cmd_base: usize = existing.cmd_start as usize;
1402 let text_base: usize = existing.text_start as usize;
1403 let text_end: usize = text_base.saturating_add(need_text);
1404 let mut text_cursor: usize = 0;
1405 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1406 for (item_index, cmd) in cmd_slice.iter().enumerate() {
1407 cmd_slab[cmd_base + item_index] =
1408 Self::encode_cached_command(cmd, text_dst, &mut text_cursor, text_base);
1409 }
1410 slots[slot_idx] = RetainedPaintFragment {
1411 occupied: true,
1412 node_id,
1413 phys_scale_milli,
1414 opacity_bits,
1415 offset_phys,
1416 geom_hash,
1417 style_text_hash,
1418 cmd_start: existing.cmd_start,
1419 cmd_len: need_cmds as u32,
1420 cmd_cap: existing.cmd_cap,
1421 text_start: existing.text_start,
1422 text_cap: existing.text_cap,
1423 };
1424 return;
1425 }
1426 if cmd_slab.len().saturating_add(need_cmds) > PAINT_FRAGMENT_CMD_SLAB_CAP
1427 || text_slab.len().saturating_add(need_text) > PAINT_FRAGMENT_TEXT_SLAB_CAP
1428 {
1429 let mut evicted: u64 = 0;
1430 for source_val in slots.iter_mut() {
1431 if source_val.occupied {
1432 evicted = evicted.saturating_add(1);
1433 *source_val = RetainedPaintFragment::default();
1434 }
1435 }
1436 *fragment_evictions = fragment_evictions.wrapping_add(evicted);
1437 cmd_slab.clear();
1438 text_slab.clear();
1439 }
1440 let cmd_start: usize = cmd_slab.len();
1441 let text_base: usize = text_slab.len();
1442 let text_end: usize = text_base.saturating_add(need_text);
1443 text_slab.resize(text_end, 0);
1444 let mut text_cursor: usize = 0;
1445 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1446 for cmd in cmd_slice.iter() {
1447 cmd_slab.push(Self::encode_cached_command(
1448 cmd,
1449 text_dst,
1450 &mut text_cursor,
1451 text_base,
1452 ));
1453 }
1454 slots[slot_idx] = RetainedPaintFragment {
1455 occupied: true,
1456 node_id,
1457 phys_scale_milli,
1458 opacity_bits,
1459 offset_phys,
1460 geom_hash,
1461 style_text_hash,
1462 cmd_start: cmd_start as u32,
1463 cmd_len: need_cmds as u32,
1464 cmd_cap: need_cmds as u32,
1465 text_start: text_base as u32,
1466 text_cap: need_text as u32,
1467 };
1468 }
1469
1470 #[inline]
1485 fn record_canvas_output(
1486 &mut self,
1487 node_id: NodeId,
1488 phys_scale_milli: u32,
1489 bounds: Rect,
1490 tree_revision: u64,
1491 cmds: &[DrawCommand],
1492 ) -> bool {
1493 let slot_idx: usize = (node_id.get_index() as usize) & (CANVAS_STABILITY_SLOTS - 1);
1494 let Self {
1495 canvas_stability, ..
1496 } = self;
1497 let slot: &mut CanvasStabilityEntry = &mut canvas_stability[slot_idx];
1498 let mut has_raster: bool = false;
1499 let mut cmd_hash: u64 = FNV1A_OFFSET_BASIS;
1500 for cmd in cmds.iter() {
1501 if matches!(
1502 cmd,
1503 DrawCommand::Image { .. }
1504 | DrawCommand::SharedImage { .. }
1505 | DrawCommand::SpriteRegion { .. }
1506 | DrawCommand::ExternalSurface { .. }
1507 ) {
1508 has_raster = true;
1509 break;
1510 }
1511 DisplayList::fold_command_full(&mut cmd_hash, cmd);
1512 }
1513 if has_raster {
1514 *slot = CanvasStabilityEntry {
1515 node_id,
1516 phys_scale_milli,
1517 bounds,
1518 tree_revision,
1519 cmd_hash: 0,
1520 stable_hits: 0,
1521 proven_dynamic: true,
1522 occupied: true,
1523 };
1524 return true;
1525 }
1526 if slot.occupied
1527 && slot.node_id == node_id
1528 && slot.phys_scale_milli == phys_scale_milli
1529 && slot.bounds == bounds
1530 {
1531 if slot.proven_dynamic {
1532 return true;
1533 }
1534 if slot.cmd_hash == cmd_hash {
1535 slot.tree_revision = tree_revision;
1536 slot.stable_hits = slot.stable_hits.saturating_add(1);
1537 return slot.stable_hits < 2;
1538 }
1539 if slot.tree_revision == tree_revision {
1540 slot.proven_dynamic = true;
1541 return true;
1542 }
1543 slot.tree_revision = tree_revision;
1544 slot.cmd_hash = cmd_hash;
1545 slot.stable_hits = 1;
1546 return true;
1547 }
1548 *slot = CanvasStabilityEntry {
1549 node_id,
1550 phys_scale_milli,
1551 bounds,
1552 tree_revision,
1553 cmd_hash,
1554 stable_hits: 1,
1555 proven_dynamic: false,
1556 occupied: true,
1557 };
1558 true
1559 }
1560}
1561
1562impl PaintPass {
1564 #[inline]
1574 #[must_use]
1575 fn text_selection_sig(tree: &SceneTree) -> u64 {
1576 let Some(sel) = tree.text_selection() else {
1577 return 0;
1578 };
1579 if sel.is_empty() {
1580 return 0;
1581 }
1582 let anchor_id: NodeId = sel.get_anchor_node();
1583 let focus_id: NodeId = sel.get_focus_node();
1584 let mut height_val: u64 =
1585 ((anchor_id.get_index() as u64) << 32) | (anchor_id.get_generation() as u64);
1586 height_val =
1587 (height_val ^ (sel.get_anchor_char() as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1588 let focus_bits: u64 =
1589 ((focus_id.get_index() as u64) << 32) | (focus_id.get_generation() as u64);
1590 height_val = (height_val ^ focus_bits).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1591 height_val =
1592 (height_val ^ (sel.get_focus_char() as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1593 if height_val == 0 { 1 } else { height_val }
1594 }
1595
1596 #[inline(always)]
1603 fn fx_fold(hash: &mut u64, word: u64) {
1604 *hash = (*hash ^ word).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1605 }
1606
1607 #[inline(always)]
1614 fn fx_fold_rect(hash: &mut u64, rect: Rect) {
1615 let lo: u64 = ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64);
1616 let hi: u64 =
1617 ((rect.get_width().to_bits() as u64) << 32) | (rect.get_height().to_bits() as u64);
1618 Self::fx_fold(hash, lo);
1619 Self::fx_fold(hash, hi);
1620 }
1621
1622 #[inline]
1629 fn fx_fold_paint(hash: &mut u64, paint: &PaintStyle) {
1630 let bg_bits: u64 = paint
1631 .background_color()
1632 .map(|color_value: Color| {
1633 u32::from_le_bytes(color_value.to_rgba_bytes()) as u64 | (1_u64 << 32)
1634 })
1635 .unwrap_or(0);
1636 Self::fx_fold(hash, bg_bits);
1637 if let Some(grad) = paint.background_gradient() {
1638 let g0: u64 = u32::from_le_bytes(grad.get_start_color().to_rgba_bytes()) as u64;
1639 let g1: u64 = u32::from_le_bytes(grad.get_end_color().to_rgba_bytes()) as u64;
1640 Self::fx_fold(hash, (g0 << 32) | g1);
1641 Self::fx_fold(hash, grad.get_angle_deg().to_bits() as u64 | (1_u64 << 33));
1642 }
1643 let border: Border = paint.get_border();
1644 let b_word: u64 = ((border.get_width().to_bits() as u64) << 32)
1645 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64);
1646 Self::fx_fold(hash, b_word);
1647 let red_val: CornerRadius = paint.get_radius();
1648 let r0: u64 = ((red_val.get_top_left().to_bits() as u64) << 32)
1649 | (red_val.get_top_right().to_bits() as u64);
1650 let r1: u64 = ((red_val.get_bottom_right().to_bits() as u64) << 32)
1651 | (red_val.get_bottom_left().to_bits() as u64);
1652 Self::fx_fold(hash, r0);
1653 Self::fx_fold(hash, r1);
1654 Self::fx_fold(hash, paint.get_opacity().to_bits() as u64);
1655 if let Some(sh) = paint.box_shadow() {
1656 let s0: u64 =
1657 ((sh.get_offset_x().to_bits() as u64) << 32) | (sh.get_offset_y().to_bits() as u64);
1658 let s1: u64 = ((sh.get_blur_radius().to_bits() as u64) << 32)
1659 | (sh.get_spread_radius().to_bits() as u64);
1660 Self::fx_fold(hash, s0);
1661 Self::fx_fold(hash, s1);
1662 Self::fx_fold(
1663 hash,
1664 u32::from_le_bytes(sh.get_color().to_rgba_bytes()) as u64 | (1_u64 << 34),
1665 );
1666 }
1667 }
1668
1669 #[inline]
1677 fn fx_fold_text(hash: &mut u64, text_style: &TextStyle, text: &str) {
1678 let m0: u64 = ((text_style.get_font_size().to_bits() as u64) << 32)
1679 | (text_style.get_line_height().to_bits() as u64);
1680 let m1: u64 = ((text_style.get_letter_spacing().to_bits() as u64) << 32)
1681 | (u32::from_le_bytes(text_style.get_color().to_rgba_bytes()) as u64);
1682 let flags: u64 = (text_style.get_family() as u64)
1683 | ((text_style.get_weight() as u64) << 8)
1684 | ((text_style.get_align() as u64) << 16)
1685 | ((text_style.get_overflow() as u64) << 24)
1686 | (u64::from(text_style.get_italic()) << 32);
1687 Self::fx_fold(hash, m0);
1688 Self::fx_fold(hash, m1);
1689 Self::fx_fold(hash, flags);
1690 let bytes: &[u8] = text.as_bytes();
1691 Self::fx_fold(hash, bytes.len() as u64);
1692 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
1693 for chunk in chunks.iter() {
1694 Self::fx_fold(hash, u64::from_ne_bytes(*chunk));
1695 }
1696 if !rem.is_empty() {
1697 let mut tail: u64 = 0;
1698 for (idx, &blue_val) in rem.iter().enumerate() {
1699 tail |= (blue_val as u64) << (idx * 8);
1700 }
1701 Self::fx_fold(hash, tail);
1702 }
1703 }
1704
1705 #[inline(always)]
1715 #[must_use]
1716 fn quantize_scale_milli(scale: f32) -> u32 {
1717 (scale.max(0.5) * 1000.0).round() as u32
1718 }
1719
1720 #[inline]
1735 #[must_use]
1736 fn fingerprint_subtree_children(
1737 tree: &SceneTree,
1738 parent: NodeId,
1739 depth: usize,
1740 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1741 focus_pair: (Option<NodeId>, bool),
1742 acc: &mut (usize, u64, u64),
1743 ) -> bool {
1744 let child_count: usize = tree.child_count(parent);
1745 if depth > PAINT_FRAGMENT_MAX_DEPTH || child_count > PAINT_FRAGMENT_MAX_CHILDREN {
1746 return false;
1747 }
1748 let (active_hover_target, has_text_sel): (Option<NodeId>, bool) = focus_pair;
1749 Self::fx_fold(&mut acc.1, child_count as u64);
1750 for cid in tree.children_iter(parent) {
1751 if acc.0 >= PAINT_FRAGMENT_MAX_NODES
1752 || active_hover_target == Some(cid)
1753 || (has_text_sel && tree.text_selection_range_for(cid).is_some())
1754 || tree.backdrop_blur_radius(cid) > 0.0
1755 {
1756 return false;
1757 }
1758 let Some(child) = tree.get_node(cid) else {
1759 return false;
1760 };
1761 acc.0 += 1;
1762 let c_inter: InteractionState = child.get_interaction();
1763 let c_active: bool = c_inter.get_hovered() || c_inter.get_pressed();
1764 if c_active || c_inter.get_focused() {
1765 return false;
1766 }
1767 let c_style: &Style = child.style_ref();
1768 let child_is_top_overlay: bool = c_style.layout.get_position() == Position::Fixed
1769 || (c_style.layout.get_position() == Position::Absolute
1770 && c_style.layout.get_z_index() >= 50);
1771 if child_is_top_overlay {
1772 return false;
1773 }
1774 let c_tag: u64 = match child.get_kind() {
1775 NodeKind::View | NodeKind::Fragment => 1,
1776 NodeKind::Text(text) => {
1777 Self::fx_fold_text(&mut acc.2, &c_style.text, text.as_str());
1778 Self::fx_fold_rect(&mut acc.1, child.layout_box.get_content_bounds());
1779 2
1780 }
1781 NodeKind::Image(buf) => {
1782 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1783 Self::fx_fold(
1784 &mut acc.2,
1785 ((buf.get_width() as u64) << 32) | (buf.get_height() as u64),
1786 );
1787 Self::fx_fold(&mut acc.2, buf.get_pixels().as_ptr() as usize as u64);
1788 3
1789 }
1790 NodeKind::Canvas(CanvasCaching::RetainedStatic) => {
1791 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1792 if let Some(callback_fn) = child.canvas_callback() {
1793 let cb_addr: u64 = (Rc::as_ptr(&callback_fn) as *const ()) as usize as u64;
1794 Self::fx_fold(&mut acc.2, cb_addr);
1795 }
1796 4
1797 }
1798 NodeKind::Portal
1799 | NodeKind::Canvas(CanvasCaching::DynamicPerFrame)
1800 | NodeKind::Input(_)
1801 | NodeKind::ScrollView => {
1802 return false;
1803 }
1804 };
1805 let c_id_bits: u64 = ((cid.get_index() as u64) << 32) | (cid.get_generation() as u64);
1806 Self::fx_fold(&mut acc.1, c_id_bits);
1807 let raw_b: Rect = child.layout_box.get_bounds();
1808 Self::fx_fold_rect(&mut acc.1, raw_b);
1809 let ct: Transform2D = c_style.paint.get_transform();
1810 Self::fx_fold(
1811 &mut acc.1,
1812 ((ct.get_translate_x().to_bits() as u64) << 32)
1813 | (ct.get_translate_y().to_bits() as u64),
1814 );
1815 Self::fx_fold(
1816 &mut acc.1,
1817 ((ct.get_scale_x().to_bits() as u64) << 32) | (ct.get_scale_y().to_bits() as u64),
1818 );
1819 let c_lay_bits: u64 = (c_tag << 40)
1820 | ((c_style.layout.get_display() as u64) << 32)
1821 | ((c_style.layout.get_overflow() as u64) << 24)
1822 | ((c_style.layout.get_z_index() as u32) as u64);
1823 Self::fx_fold(&mut acc.2, c_lay_bits);
1824 Self::fx_fold_paint(&mut acc.2, &c_style.paint);
1825 let mut next_cull_check: Option<(Point, Rect, f32, f32)> = None;
1826 if let Some((off_phys, cull_rect, scale, inv_scale)) = unclipped_cull {
1827 let tx_phys: f32 = (ct.get_translate_x() * scale).round();
1828 let ty_phys: f32 = (ct.get_translate_y() * scale).round();
1829 let tot_x: f32 = off_phys.get_x() + tx_phys;
1830 let tot_y: f32 = off_phys.get_y() + ty_phys;
1831 let rx0: f32 = (raw_b.get_x() * scale).round();
1832 let ry0: f32 = (raw_b.get_y() * scale).round();
1833 let rx1: f32 = (raw_b.right() * scale).round();
1834 let ry1: f32 = (raw_b.bottom() * scale).round();
1835 let stroke_width: f32 = ((rx1 - rx0).max(0.0) * ct.get_scale_x()).round().max(0.0);
1836 let sh: f32 = ((ry1 - ry0).max(0.0) * ct.get_scale_y()).round().max(0.0);
1837 let sdx: f32 = (((rx1 - rx0).max(0.0) - stroke_width) * 0.5).round();
1838 let sdy: f32 = (((ry1 - ry0).max(0.0) - sh) * 0.5).round();
1839 let c_bounds: Rect = Rect::new(
1840 (rx0 + tot_x + sdx) * inv_scale,
1841 (ry0 + tot_y + sdy) * inv_scale,
1842 stroke_width * inv_scale,
1843 sh * inv_scale,
1844 );
1845 let c_vis: Rect = Self::conservative_self_vis_rect(
1846 child,
1847 c_bounds,
1848 &c_style.paint,
1849 false,
1850 (tot_x, tot_y),
1851 scale,
1852 inv_scale,
1853 );
1854 let c_in_x: bool =
1855 c_vis.get_x() >= cull_rect.get_x() && c_vis.right() <= cull_rect.right();
1856 let c_in_y: bool =
1857 c_vis.get_y() >= cull_rect.get_y() && c_vis.bottom() <= cull_rect.bottom();
1858 if !c_in_x || !c_in_y {
1859 return false;
1860 }
1861 if c_style.layout.get_overflow() != Overflow::Hidden {
1862 let c_scroll: Point = child.layout_box.get_scroll_offset();
1863 let next_off: Point = Point::new(
1864 tot_x - (c_scroll.get_x() * scale).round(),
1865 tot_y - (c_scroll.get_y() * scale).round(),
1866 );
1867 next_cull_check = Some((next_off, cull_rect, scale, inv_scale));
1868 }
1869 }
1870 if tree.has_children(cid)
1871 && !Self::fingerprint_subtree_children(
1872 tree,
1873 cid,
1874 depth + 1,
1875 next_cull_check,
1876 focus_pair,
1877 acc,
1878 )
1879 {
1880 return false;
1881 }
1882 }
1883 true
1884 }
1885
1886 #[inline]
1900 #[must_use]
1901 fn fingerprint_subtree(
1902 tree: &SceneTree,
1903 node_id: NodeId,
1904 node: &SceneNode,
1905 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1906 focus_pair: (Option<NodeId>, bool),
1907 ) -> Option<(u64, u64)> {
1908 let child_count: usize = tree.child_count(node_id);
1909 if child_count == 0
1910 || child_count > PAINT_FRAGMENT_MAX_CHILDREN
1911 || tree.backdrop_blur_radius(node_id) > 0.0
1912 {
1913 return None;
1914 }
1915 let style: &Style = node.style_ref();
1916 let transform: Transform2D = style.paint.get_transform();
1917 let scroll: Point = node.layout_box.get_scroll_offset();
1918 let mut geom_h: u64 = child_count as u64;
1919 Self::fx_fold_rect(&mut geom_h, node.layout_box.get_bounds());
1920 Self::fx_fold(
1921 &mut geom_h,
1922 ((transform.get_translate_x().to_bits() as u64) << 32)
1923 | (transform.get_translate_y().to_bits() as u64),
1924 );
1925 Self::fx_fold(
1926 &mut geom_h,
1927 ((transform.get_scale_x().to_bits() as u64) << 32)
1928 | (transform.get_scale_y().to_bits() as u64),
1929 );
1930 Self::fx_fold(
1931 &mut geom_h,
1932 ((scroll.get_x().to_bits() as u64) << 32) | (scroll.get_y().to_bits() as u64),
1933 );
1934 let mut style_h: u64 =
1935 ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
1936 Self::fx_fold_paint(&mut style_h, &style.paint);
1937 let mut acc: (usize, u64, u64) = (0, geom_h, style_h);
1938 if !Self::fingerprint_subtree_children(
1939 tree,
1940 node_id,
1941 1,
1942 unclipped_cull,
1943 focus_pair,
1944 &mut acc,
1945 ) {
1946 return None;
1947 }
1948 Some((acc.1, acc.2))
1949 }
1950
1951 #[inline(always)]
1965 #[must_use]
1966 fn translate_coord(
1967 item_val: f32,
1968 d_phys: f32,
1969 scale: f32,
1970 inv_scale: f32,
1971 dyadic: bool,
1972 ) -> f32 {
1973 if d_phys == 0.0 {
1974 return item_val;
1975 }
1976 if dyadic {
1977 return item_val + d_phys * inv_scale;
1978 }
1979 ((item_val * scale).round() + d_phys) * inv_scale
1980 }
1981
1982 #[inline(always)]
1997 #[must_use]
1998 fn translate_rect(
1999 rect: Rect,
2000 dx_phys: f32,
2001 dy_phys: f32,
2002 scale: f32,
2003 inv_scale: f32,
2004 dyadic: bool,
2005 ) -> Rect {
2006 Rect::new(
2007 Self::translate_coord(rect.get_x(), dx_phys, scale, inv_scale, dyadic),
2008 Self::translate_coord(rect.get_y(), dy_phys, scale, inv_scale, dyadic),
2009 rect.get_width(),
2010 rect.get_height(),
2011 )
2012 }
2013
2014 #[inline]
2024 fn push_translated_command(
2025 cmd: &CachedDrawCommand,
2026 text_slab: &[u8],
2027 delta_phys: (f32, f32),
2028 scale_info: (f32, f32, bool),
2029 display_list: &mut DisplayList,
2030 ) {
2031 let (dx_phys, dy_phys): (f32, f32) = delta_phys;
2032 let (scale, inv_scale, dyadic): (f32, f32, bool) = scale_info;
2033 let translated: DrawCommand = match cmd {
2034 CachedDrawCommand::Clear(char_item) => DrawCommand::Clear(*char_item),
2035 CachedDrawCommand::FillRoundedRect {
2036 rect,
2037 color,
2038 radius,
2039 border,
2040 } => DrawCommand::FillRoundedRect {
2041 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2042 color: *color,
2043 radius: *radius,
2044 border: *border,
2045 },
2046 CachedDrawCommand::LinearGradientRect {
2047 rect,
2048 gradient,
2049 radius,
2050 } => DrawCommand::LinearGradientRect {
2051 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2052 gradient: *gradient,
2053 radius: *radius,
2054 },
2055 CachedDrawCommand::BoxShadow {
2056 rect,
2057 radius,
2058 shadow,
2059 } => DrawCommand::BoxShadow {
2060 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2061 radius: *radius,
2062 shadow: *shadow,
2063 },
2064 CachedDrawCommand::BackdropBlur {
2065 rect,
2066 radius,
2067 blur_radius,
2068 } => DrawCommand::BackdropBlur {
2069 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2070 radius: *radius,
2071 blur_radius: *blur_radius,
2072 },
2073 CachedDrawCommand::TextRun {
2074 rect,
2075 text_start,
2076 text_len,
2077 style,
2078 } => {
2079 let source_val: usize = *text_start as usize;
2080 let event_ref: usize = source_val.saturating_add(*text_len as usize);
2081 let src: &str = text_slab
2082 .get(source_val..event_ref)
2083 .and_then(|blue_val: &[u8]| from_utf8(blue_val).ok())
2084 .unwrap_or("");
2085 DrawCommand::TextRun {
2086 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2087 text: display_list.alloc_text(src),
2088 style: *style,
2089 }
2090 }
2091 CachedDrawCommand::TextCaret {
2092 text_rect,
2093 content_rect,
2094 text_start,
2095 text_len,
2096 style,
2097 caret_height,
2098 color,
2099 multiline,
2100 } => {
2101 let scale_val: usize = *text_start as usize;
2102 let error_val: usize = scale_val.saturating_add(*text_len as usize);
2103 let src: &str = text_slab
2104 .get(scale_val..error_val)
2105 .and_then(|byte_val: &[u8]| from_utf8(byte_val).ok())
2106 .unwrap_or("");
2107 DrawCommand::TextCaret {
2108 text_rect: Self::translate_rect(
2109 *text_rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
2110 ),
2111 content_rect: Self::translate_rect(
2112 *content_rect,
2113 dx_phys,
2114 dy_phys,
2115 scale,
2116 inv_scale,
2117 dyadic,
2118 ),
2119 text: display_list.alloc_text(src),
2120 style: *style,
2121 caret_height: *caret_height,
2122 color: *color,
2123 multiline: *multiline,
2124 }
2125 }
2126 CachedDrawCommand::Image {
2127 rect,
2128 buffer,
2129 radius,
2130 } => DrawCommand::Image {
2131 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2132 buffer: buffer.clone(),
2133 radius: *radius,
2134 },
2135 CachedDrawCommand::SharedImage {
2136 rect,
2137 width,
2138 height,
2139 pixels,
2140 radius,
2141 } => DrawCommand::SharedImage {
2142 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2143 width: *width,
2144 height: *height,
2145 pixels: Arc::clone(pixels),
2146 radius: *radius,
2147 },
2148 CachedDrawCommand::Line {
2149 from,
2150 to,
2151 color,
2152 width,
2153 } => DrawCommand::Line {
2154 from: Point::new(
2155 Self::translate_coord(from.get_x(), dx_phys, scale, inv_scale, dyadic),
2156 Self::translate_coord(from.get_y(), dy_phys, scale, inv_scale, dyadic),
2157 ),
2158 to: Point::new(
2159 Self::translate_coord(to.get_x(), dx_phys, scale, inv_scale, dyadic),
2160 Self::translate_coord(to.get_y(), dy_phys, scale, inv_scale, dyadic),
2161 ),
2162 color: *color,
2163 width: *width,
2164 },
2165 CachedDrawCommand::Circle {
2166 center,
2167 radius,
2168 color,
2169 } => DrawCommand::Circle {
2170 center: Point::new(
2171 Self::translate_coord(center.get_x(), dx_phys, scale, inv_scale, dyadic),
2172 Self::translate_coord(center.get_y(), dy_phys, scale, inv_scale, dyadic),
2173 ),
2174 radius: *radius,
2175 color: *color,
2176 },
2177 CachedDrawCommand::Triangle { p0, p1, p2, color } => DrawCommand::Triangle {
2178 p0: Point::new(
2179 Self::translate_coord(p0.get_x(), dx_phys, scale, inv_scale, dyadic),
2180 Self::translate_coord(p0.get_y(), dy_phys, scale, inv_scale, dyadic),
2181 ),
2182 p1: Point::new(
2183 Self::translate_coord(p1.get_x(), dx_phys, scale, inv_scale, dyadic),
2184 Self::translate_coord(p1.get_y(), dy_phys, scale, inv_scale, dyadic),
2185 ),
2186 p2: Point::new(
2187 Self::translate_coord(p2.get_x(), dx_phys, scale, inv_scale, dyadic),
2188 Self::translate_coord(p2.get_y(), dy_phys, scale, inv_scale, dyadic),
2189 ),
2190 color: *color,
2191 },
2192 CachedDrawCommand::SpriteRegion {
2193 rect,
2194 buffer,
2195 src_rect,
2196 tint,
2197 } => DrawCommand::SpriteRegion {
2198 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2199 buffer: buffer.clone(),
2200 src_rect: *src_rect,
2201 tint: *tint,
2202 },
2203 CachedDrawCommand::ExternalSurface { rect, radius } => DrawCommand::ExternalSurface {
2204 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2205 radius: *radius,
2206 },
2207 CachedDrawCommand::PushClip(rect) => DrawCommand::PushClip(Self::translate_rect(
2208 *rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
2209 )),
2210 CachedDrawCommand::PushRoundedClip { rect, radius } => DrawCommand::PushRoundedClip {
2211 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2212 radius: *radius,
2213 },
2214 CachedDrawCommand::PopClip => DrawCommand::PopClip,
2215 };
2216 display_list.push(translated);
2217 }
2218
2219 #[cfg_attr(not(target_arch = "wasm32"), inline)]
2229 #[must_use]
2230 pub(crate) fn viewport_shows_desktop_scrollbars(viewport: Size) -> bool {
2231 viewport.get_width() >= DESKTOP_SCROLLBAR_MIN_WIDTH_DP
2232 && viewport.get_height() >= DESKTOP_SCROLLBAR_MIN_HEIGHT_DP
2233 }
2234
2235 pub fn build(
2245 tree: &SceneTree,
2246 viewport: Size,
2247 scale_factor: f32,
2248 clear_color: Color,
2249 display_list: &mut DisplayList,
2250 ) {
2251 let mut saw_canvas: bool = false;
2252 Self::build_inner(
2253 tree,
2254 viewport,
2255 scale_factor,
2256 clear_color,
2257 display_list,
2258 None,
2259 &mut saw_canvas,
2260 );
2261 }
2262
2263 #[inline(always)]
2273 #[must_use]
2274 fn pack_opt_node_id(node: Option<NodeId>) -> u64 {
2275 match node {
2276 Some(id) => ((id.get_index() as u64) << 32) | (id.get_generation() as u64),
2277 None => u64::MAX,
2278 }
2279 }
2280
2281 pub fn build_retained(
2293 tree: &SceneTree,
2294 viewport: Size,
2295 scale_factor: f32,
2296 clear_color: Color,
2297 layout_changed: bool,
2298 display_list: &mut DisplayList,
2299 cache: &mut PaintFragmentCache,
2300 ) {
2301 let clamped_scale: f32 = scale_factor.max(0.5);
2302 let focus: FocusManager = tree.get_focus();
2303 let sig: PaintSceneSignature = PaintSceneSignature {
2304 tree_ptr: (tree as *const SceneTree) as usize,
2305 tree_revision: tree.get_revision(),
2306 viewport_w_bits: viewport.get_width().to_bits(),
2307 viewport_h_bits: viewport.get_height().to_bits(),
2308 scale_bits: clamped_scale.to_bits(),
2309 clear_rgba: u32::from_le_bytes(clear_color.to_rgba_bytes()),
2310 focused_packed: Self::pack_opt_node_id(focus.focused_node()),
2311 hovered_packed: Self::pack_opt_node_id(focus.hovered_node()),
2312 pressed_packed: Self::pack_opt_node_id(focus.pressed_node()),
2313 caret_visible: tree.get_caret_visible(),
2314 text_sel_sig: Self::text_selection_sig(tree),
2315 scrollbars_suppressed: tree.get_scrollbars_suppressed(),
2316 };
2317 if !layout_changed
2318 && !cache.last_has_canvas
2319 && !display_list.is_empty()
2320 && cache.has_last_scene
2321 && cache.last_scene_sig == sig
2322 {
2323 cache.whole_list_hits = cache.whole_list_hits.wrapping_add(1);
2324 return;
2325 }
2326 cache.ensure_slots();
2327 let mut saw_canvas: bool = false;
2328 Self::build_inner(
2329 tree,
2330 viewport,
2331 clamped_scale,
2332 clear_color,
2333 display_list,
2334 Some(cache),
2335 &mut saw_canvas,
2336 );
2337 cache.last_has_canvas = saw_canvas;
2338 cache.has_last_scene = true;
2339 cache.last_scene_sig = sig;
2340 }
2341
2342 fn collect_portal_ancestors(tree: &SceneTree, out_ancestors: &mut Vec<NodeId>) {
2349 out_ancestors.clear();
2350 let portal_nodes: &[NodeId] = tree.portal_nodes();
2351 if portal_nodes.is_empty() {
2352 return;
2353 }
2354 for &pid in portal_nodes.iter() {
2355 let mut cur: Option<NodeId> = tree.get_node(pid).and_then(SceneNode::parent_id);
2356 while let Some(anc) = cur {
2357 if out_ancestors.contains(&anc) {
2358 break;
2359 }
2360 out_ancestors.push(anc);
2361 cur = tree.get_node(anc).and_then(SceneNode::parent_id);
2362 }
2363 }
2364 if out_ancestors.len() > 16 {
2365 out_ancestors.sort_unstable_by_key(|id: &NodeId| {
2366 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2367 });
2368 }
2369 }
2370
2371 #[inline]
2382 #[must_use]
2383 fn is_portal_ancestor(ancestors: &[NodeId], node_id: NodeId) -> bool {
2384 if ancestors.is_empty() {
2385 return false;
2386 }
2387 if ancestors.len() <= 16 {
2388 return ancestors.contains(&node_id);
2389 }
2390 let target: u64 = ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
2391 ancestors
2392 .binary_search_by_key(&target, |id: &NodeId| {
2393 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2394 })
2395 .is_ok()
2396 }
2397
2398 #[inline]
2414 #[must_use]
2415 fn conservative_self_vis_rect(
2416 node: &SceneNode,
2417 bounds: Rect,
2418 paint: &PaintStyle,
2419 focused: bool,
2420 total_off_phys: (f32, f32),
2421 scale: f32,
2422 inv_scale: f32,
2423 ) -> Rect {
2424 let mut min_x: f32 = bounds.get_x() - 1.0;
2425 let mut min_y: f32 = bounds.get_y() - 1.0;
2426 let mut max_x: f32 = bounds.right() + 1.0;
2427 let mut max_y: f32 = bounds.bottom() + 1.0;
2428 if let Some(shadow) = paint.box_shadow()
2429 && shadow.get_color().get_alpha() > 0
2430 {
2431 let spread: f32 = shadow.get_spread_radius().max(0.0);
2432 let blur_phys: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
2433 let sigma_phys: f32 = (blur_phys * 0.5).max(0.75);
2434 let blur_pad: f32 = (sigma_phys * 3.0).ceil() * inv_scale + 2.0 * inv_scale;
2435 let pad: f32 = spread + blur_pad;
2436 let sx0: f32 = bounds.get_x() + shadow.get_offset_x() - pad;
2437 let sy0: f32 = bounds.get_y() + shadow.get_offset_y() - pad;
2438 let sx1: f32 = bounds.right() + shadow.get_offset_x() + pad;
2439 let sy1: f32 = bounds.bottom() + shadow.get_offset_y() + pad;
2440 min_x = min_x.min(sx0);
2441 min_y = min_y.min(sy0);
2442 max_x = max_x.max(sx1);
2443 max_y = max_y.max(sy1);
2444 }
2445 if focused && let Some(ring) = paint.focus_ring_border() {
2446 let rw: f32 = ring.get_width().max(0.0) + 1.0;
2447 min_x = min_x.min(bounds.get_x() - rw);
2448 min_y = min_y.min(bounds.get_y() - rw);
2449 max_x = max_x.max(bounds.right() + rw);
2450 max_y = max_y.max(bounds.bottom() + rw);
2451 }
2452 if matches!(node.get_kind(), NodeKind::Text(_)) {
2453 let content_b: Rect = node.layout_box.get_content_bounds();
2454 let cx0: f32 = ((content_b.get_x() * scale).round() + total_off_phys.0) * inv_scale;
2455 let cy0: f32 = ((content_b.get_y() * scale).round() + total_off_phys.1) * inv_scale;
2456 let pad: f32 = node.style_ref().text.get_font_size() * 0.5 + 2.0;
2457 min_x = min_x.min(cx0 - pad);
2458 min_y = min_y.min(cy0 - pad);
2459 max_x = max_x.max(cx0 + content_b.get_width() + pad);
2460 max_y = max_y.max(cy0 + content_b.get_height() + pad);
2461 }
2462 Rect::new(
2463 min_x,
2464 min_y,
2465 (max_x - min_x).max(0.0),
2466 (max_y - min_y).max(0.0),
2467 )
2468 }
2469
2470 fn visit_culled_portal_child(
2479 tree: &SceneTree,
2480 child_id: NodeId,
2481 parent_opacity: f32,
2482 scratch: &mut PaintScratch<'_>,
2483 ) {
2484 let Some(child) = tree.get_node(child_id) else {
2485 return;
2486 };
2487 let style: &Style = child.style_ref();
2488 let layout: &LayoutStyle = &style.layout;
2489 if layout.get_display() == Display::None {
2490 return;
2491 }
2492 let is_portal: bool = matches!(child.get_kind(), NodeKind::Portal)
2493 || layout.get_position() == Position::Fixed;
2494 if is_portal {
2495 if let Some(portal_queue) = scratch.portals.as_deref_mut() {
2496 portal_queue.push((layout.get_z_index(), portal_queue.len(), child_id));
2497 }
2498 return;
2499 }
2500 if !Self::is_portal_ancestor(scratch.portal_ancestors, child_id) {
2501 return;
2502 }
2503 let hovered: bool = child.get_interaction().get_hovered();
2504 let paint_opacity: f32 = if hovered {
2505 child
2506 .hover_paint()
2507 .map(|hp: PaintStyle| hp.get_opacity())
2508 .unwrap_or_else(|| style.paint.get_opacity())
2509 } else {
2510 style.paint.get_opacity()
2511 };
2512 let next_opacity: f32 = (parent_opacity * paint_opacity).clamp(0.0, 1.0);
2513 if next_opacity <= 0.0 {
2514 return;
2515 }
2516 Self::collect_portals_in_culled_subtree(tree, child_id, next_opacity, scratch);
2517 }
2518
2519 fn collect_portals_in_culled_subtree(
2528 tree: &SceneTree,
2529 node_id: NodeId,
2530 parent_opacity: f32,
2531 scratch: &mut PaintScratch<'_>,
2532 ) {
2533 if tree.get_node(node_id).is_none() {
2534 return;
2535 }
2536 let child_count: usize = tree.child_count(node_id);
2537 if child_count == 0 {
2538 return;
2539 }
2540 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
2541 if !needs_z_sort {
2542 for child_id in tree.children_iter(node_id) {
2543 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2544 }
2545 } else {
2546 let sort_start: usize = scratch.z_sort.len();
2547 for (idx, cid) in tree.children_iter(node_id).enumerate() {
2548 if let Some(char_item) = tree.get_node(cid) {
2549 scratch
2550 .z_sort
2551 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
2552 }
2553 }
2554 let sort_end: usize = scratch.z_sort.len();
2555 scratch.z_sort[sort_start..sort_end].sort_by(
2556 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
2557 target_node_id
2558 .0
2559 .cmp(&element_id.0)
2560 .then_with(|| target_node_id.1.cmp(&element_id.1))
2561 },
2562 );
2563 for slot_idx in sort_start..sort_end {
2564 let child_id: NodeId = scratch.z_sort[slot_idx].2;
2565 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2566 }
2567 scratch.z_sort.truncate(sort_start);
2568 }
2569 }
2570
2571 fn build_inner(
2583 tree: &SceneTree,
2584 viewport: Size,
2585 scale_factor: f32,
2586 clear_color: Color,
2587 display_list: &mut DisplayList,
2588 mut fragments: Option<&mut PaintFragmentCache>,
2589 saw_canvas: &mut bool,
2590 ) {
2591 display_list.reset(viewport, scale_factor);
2592 display_list.push(DrawCommand::Clear(clear_color));
2593 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
2594 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
2595 let mut portals: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.portals_scratch);
2596 let mut z_sort: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.z_sort_scratch);
2597 let mut portal_ancestors: Vec<NodeId> =
2598 take(&mut display_list.arena.portal_ancestors_scratch);
2599 portals.clear();
2600 z_sort.clear();
2601 Self::collect_portal_ancestors(tree, &mut portal_ancestors);
2602 {
2603 let mut scratch: PaintScratch<'_> = PaintScratch {
2604 portals: Some(&mut portals),
2605 z_sort: &mut z_sort,
2606 portal_ancestors: portal_ancestors.as_slice(),
2607 fragments: fragments.as_deref_mut(),
2608 saw_canvas,
2609 };
2610 Self::emit_node(
2611 tree,
2612 tree.get_root(),
2613 (Point::new(0.0, 0.0), 1.0),
2614 viewport_rect,
2615 active_hover_target,
2616 &mut scratch,
2617 display_list,
2618 );
2619 }
2620 display_list.finalize_base_layer(display_list.len());
2621 if portals.len() > 1 {
2622 portals.sort_by(
2623 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
2624 target_node_id
2625 .0
2626 .cmp(&element_id.0)
2627 .then_with(|| target_node_id.1.cmp(&element_id.1))
2628 },
2629 );
2630 }
2631 {
2632 let mut portal_scratch: PaintScratch<'_> = PaintScratch {
2633 portals: None,
2634 z_sort: &mut z_sort,
2635 portal_ancestors: &[],
2636 fragments,
2637 saw_canvas,
2638 };
2639 for &(_, _, portal_id) in portals.iter() {
2640 Self::emit_node(
2641 tree,
2642 portal_id,
2643 (Point::new(0.0, 0.0), 1.0),
2644 viewport_rect,
2645 active_hover_target,
2646 &mut portal_scratch,
2647 display_list,
2648 );
2649 }
2650 }
2651 portals.clear();
2652 z_sort.clear();
2653 portal_ancestors.clear();
2654 display_list.arena.portals_scratch = portals;
2655 display_list.arena.z_sort_scratch = z_sort;
2656 display_list.arena.portal_ancestors_scratch = portal_ancestors;
2657 }
2658
2659 #[must_use]
2669 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
2670 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
2671 while let Some(id) = cursor {
2672 let Some(node) = tree.get_node(id) else {
2673 break;
2674 };
2675 let style: &Style = node.style_ref();
2676 let paint: &PaintStyle = &style.paint;
2677 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2678 || style.layout.get_position() == Position::Fixed;
2679 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2680 && paint.background_color().is_none();
2681 if !is_scrim && !is_transparent_canvas {
2682 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
2683 || node
2684 .get_handlers()
2685 .handler_for(&UiEventKind::Click(Point::default()))
2686 .is_some();
2687 if is_interactive {
2688 return Some(id);
2689 }
2690 }
2691 cursor = node.parent_id();
2692 }
2693 None
2694 }
2695
2696 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
2708 if !hovered && !pressed {
2709 return base;
2710 }
2711 let delta: i32 = if pressed { 24 } else { 14 };
2712 match base {
2713 Some(char_item) if char_item.get_alpha() > 0 => {
2714 let luma: u32 = (char_item.get_red() as u32) * 299
2715 + (char_item.get_green() as u32) * 587
2716 + (char_item.get_blue() as u32) * 114;
2717 if luma < 120_000 {
2718 let lift: i32 = if pressed { 30 } else { 20 };
2719 Some(Color::new(
2720 (char_item.get_red() as i32 + lift).clamp(0, 255) as u8,
2721 (char_item.get_green() as i32 + lift).clamp(0, 255) as u8,
2722 (char_item.get_blue() as i32 + lift).clamp(0, 255) as u8,
2723 char_item.get_alpha(),
2724 ))
2725 } else {
2726 Some(Color::new(
2727 (char_item.get_red() as i32 - delta).clamp(0, 255) as u8,
2728 (char_item.get_green() as i32 - delta).clamp(0, 255) as u8,
2729 (char_item.get_blue() as i32 - delta).clamp(0, 255) as u8,
2730 char_item.get_alpha(),
2731 ))
2732 }
2733 }
2734 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
2735 }
2736 }
2737
2738 fn emit_node(
2750 tree: &SceneTree,
2751 node_id: NodeId,
2752 offset_and_opacity: (Point, f32),
2753 cull_rect: Rect,
2754 active_hover_target: Option<NodeId>,
2755 scratch: &mut PaintScratch<'_>,
2756 display_list: &mut DisplayList,
2757 ) {
2758 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
2759 let Some(node) = tree.get_node(node_id) else {
2760 return;
2761 };
2762 let style: &Style = node.style_ref();
2763 let layout: &LayoutStyle = &style.layout;
2764 if layout.get_display() == Display::None {
2765 return;
2766 }
2767 let is_portal_or_fixed: bool =
2768 matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
2769 let is_root_chrome_overlay: bool = layout.get_position() == Position::Absolute
2770 && layout.get_z_index() >= 50
2771 && layout.get_z_index() < 140
2772 && offset_phys == Point::new(0.0, 0.0)
2773 && (parent_opacity - 1.0).abs() <= 0.001
2774 && cull_rect.get_x() <= 0.0
2775 && cull_rect.get_y() <= 0.0;
2776 let promote_to_top_layer: bool = is_portal_or_fixed || is_root_chrome_overlay;
2777 if let Some(portal_queue) = scratch.portals.as_deref_mut()
2778 && promote_to_top_layer
2779 {
2780 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
2781 return;
2782 }
2783 let interaction: InteractionState = node.get_interaction();
2784 let hovered: bool = interaction.get_hovered();
2785 let pressed: bool = interaction.get_pressed();
2786 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
2787 return;
2788 }
2789 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
2790 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2791 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
2792 if opacity <= 0.0 {
2793 return;
2794 }
2795 let scale: f32 = display_list.get_scale_factor().max(0.5);
2796 let inv_scale: f32 = 1.0 / scale;
2797 let raw_bounds: Rect = node.layout_box.get_bounds();
2798 let transform: Transform2D = prelim_paint.get_transform();
2799 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
2800 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
2801 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
2802 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
2803 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
2804 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
2805 let raw_x1: f32 = (raw_bounds.right() * scale).round();
2806 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
2807 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
2808 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
2809 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
2810 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
2811 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
2812 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
2813 let bounds: Rect = Rect::new(
2814 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
2815 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
2816 scaled_w_phys * inv_scale,
2817 scaled_h_phys * inv_scale,
2818 );
2819 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2820 let clipped_cull: Option<Rect> = if clipped {
2821 cull_rect.intersect(bounds)
2822 } else {
2823 Some(cull_rect)
2824 };
2825 let focused: bool = interaction.get_focused();
2826 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
2827 node,
2828 bounds,
2829 prelim_paint,
2830 focused,
2831 (total_off_x, total_off_y),
2832 scale,
2833 inv_scale,
2834 );
2835 let self_visible: bool = bounds.get_width() > 0.0
2836 && bounds.get_height() > 0.0
2837 && self_vis_rect.intersect(cull_rect).is_some();
2838 if clipped && clipped_cull.is_none() {
2839 if self_visible {
2840 if let Some(mut shadow) = prelim_paint.box_shadow() {
2841 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2842 display_list.push(DrawCommand::BoxShadow {
2843 rect: bounds,
2844 radius: prelim_paint.get_radius(),
2845 shadow,
2846 });
2847 }
2848 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
2849 ring.set_color(ring.get_color().multiply_alpha(opacity));
2850 let ring_w: f32 = ring.get_width();
2851 let ring_rect: Rect = Rect::new(
2852 bounds.get_x() - ring_w,
2853 bounds.get_y() - ring_w,
2854 bounds.get_width() + ring_w * 2.0,
2855 bounds.get_height() + ring_w * 2.0,
2856 );
2857 display_list.push(DrawCommand::FillRoundedRect {
2858 rect: ring_rect,
2859 color: COLOR_TRANSPARENT,
2860 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
2861 border: ring,
2862 });
2863 }
2864 }
2865 if scratch.portals.is_some()
2866 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
2867 {
2868 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
2869 }
2870 return;
2871 }
2872 let child_count: usize = tree.child_count(node_id);
2873 if !self_visible && child_count == 0 {
2874 return;
2875 }
2876 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
2877 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
2878 if self_visible
2879 && (hover_visual || pressed)
2880 && paint_owned.is_none()
2881 && (hovered || node.hover_paint().is_none())
2882 {
2883 let base_paint: &PaintStyle = &style.paint;
2884 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2885 || layout.get_position() == Position::Fixed;
2886 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2887 && base_paint.background_color().is_none();
2888 if !is_scrim && !is_transparent_canvas {
2889 let is_interactive: bool = base_paint.get_cursor() == CursorIcon::Pointer
2890 || node
2891 .get_handlers()
2892 .handler_for(&UiEventKind::Click(Point::default()))
2893 .is_some();
2894 if is_interactive {
2895 let mut tinted: PaintStyle = *base_paint;
2896 tinted.set_background(Self::interactive_tint(
2897 tinted.background_color(),
2898 hover_visual,
2899 pressed,
2900 ));
2901 paint_owned = Some(tinted);
2902 }
2903 }
2904 }
2905 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2906 let phys_scale_milli: u32 = Self::quantize_scale_milli(scale);
2907 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
2908 let frag_overflow_ok: bool = layout.get_overflow() == Overflow::Hidden
2909 || (layout.get_overflow() == Overflow::Visible
2910 && node_id != tree.get_root()
2911 && child_count >= 2);
2912 if self_visible
2913 && scratch.fragments.is_some()
2914 && scratch.portals.is_some()
2915 && matches!(node.get_kind(), NodeKind::View)
2916 && frag_overflow_ok
2917 && !hovered
2918 && !pressed
2919 && !interaction.get_focused()
2920 && active_hover_target != Some(node_id)
2921 {
2922 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
2923 && self_vis_rect.right() <= cull_rect.right();
2924 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
2925 && self_vis_rect.bottom() <= cull_rect.bottom();
2926 if vis_x_in && vis_y_in {
2927 let has_text_sel: bool = !tree.text_selection_spans_ref().is_empty();
2928 let unclipped_cull: Option<(Point, Rect, f32, f32)> =
2929 if layout.get_overflow() == Overflow::Hidden {
2930 None
2931 } else {
2932 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
2933 let child_off: Point = Point::new(
2934 total_off_x - (raw_node_scroll.get_x() * scale).round(),
2935 total_off_y - (raw_node_scroll.get_y() * scale).round(),
2936 );
2937 Some((child_off, cull_rect, scale, inv_scale))
2938 };
2939 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_subtree(
2940 tree,
2941 node_id,
2942 node,
2943 unclipped_cull,
2944 (active_hover_target, has_text_sel),
2945 ) {
2946 let slot_idx: usize =
2947 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
2948 let opacity_bits: u32 = parent_opacity.to_bits();
2949 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
2950 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
2951 if let Some(cache) = scratch.fragments.as_deref_mut()
2952 && let Some(&cached) = cache.slots.get(slot_idx)
2953 && cached.occupied
2954 && cached.node_id == node_id
2955 && cached.phys_scale_milli == phys_scale_milli
2956 && cached.opacity_bits == opacity_bits
2957 && cached.geom_hash == geom_hash
2958 && cached.style_text_hash == style_text_hash
2959 {
2960 let dx_phys: f32 = offset_phys.get_x() - cached.offset_phys.get_x();
2961 let dy_phys: f32 = offset_phys.get_y() - cached.offset_phys.get_y();
2962 let c_start: usize = cached.cmd_start as usize;
2963 let c_end: usize = c_start.saturating_add(cached.cmd_len as usize);
2964 if let Some(cmds) = cache.cmd_slab.get(c_start..c_end) {
2965 display_list.get_mut_commands().reserve(cmds.len());
2966 let text_slab: &[u8] = cache.text_slab.as_slice();
2967 for cmd in cmds.iter() {
2968 Self::push_translated_command(
2969 cmd,
2970 text_slab,
2971 (dx_phys, dy_phys),
2972 (scale, inv_scale, dyadic_scale),
2973 display_list,
2974 );
2975 }
2976 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
2977 return;
2978 }
2979 }
2980 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
2981 }
2982 }
2983 }
2984 let box_size: Size = display_list.get_viewport();
2985 let show_scrollbar: bool =
2986 !tree.get_scrollbars_suppressed() && Self::viewport_shows_desktop_scrollbars(box_size);
2987 let wants_scrollbar: bool = show_scrollbar
2988 && layout.get_overflow() == Overflow::Scroll
2989 && bounds.get_height() > 24.0;
2990 let emit_clip: bool = clipped && (child_count > 0 || wants_scrollbar);
2991 let has_canvas_clip: bool =
2992 matches!(node.get_kind(), NodeKind::Canvas(_)) && node.canvas_callback().is_some();
2993 let has_rounded_border: bool =
2994 paint.get_border().get_width() > 0.0 && paint.get_radius().max_radius() > 0.0;
2995 let defer_rounded_border: bool = has_rounded_border && (emit_clip || has_canvas_clip);
2996 if self_visible {
2997 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
2998 if blur_r > 0.0 {
2999 display_list.push(DrawCommand::BackdropBlur {
3000 rect: bounds,
3001 radius: paint.get_radius(),
3002 blur_radius: blur_r,
3003 });
3004 }
3005 if let Some(mut shadow) = paint.box_shadow() {
3006 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
3007 display_list.push(DrawCommand::BoxShadow {
3008 rect: bounds,
3009 radius: paint.get_radius(),
3010 shadow,
3011 });
3012 }
3013 if let Some(mut gradient) = paint.background_gradient() {
3014 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
3015 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
3016 display_list.push(DrawCommand::LinearGradientRect {
3017 rect: bounds,
3018 gradient,
3019 radius: paint.get_radius(),
3020 });
3021 } else if let Some(bg) = paint.background_color() {
3022 let border: Border = if defer_rounded_border {
3023 Border::default()
3024 } else {
3025 let mut direct_border: Border = paint.get_border();
3026 direct_border.set_color(direct_border.get_color().multiply_alpha(opacity));
3027 direct_border
3028 };
3029 display_list.push(DrawCommand::FillRoundedRect {
3030 rect: bounds,
3031 color: bg.multiply_alpha(opacity),
3032 radius: paint.get_radius(),
3033 border,
3034 });
3035 } else if paint.get_border().get_width() > 0.0 && !defer_rounded_border {
3036 let mut border: Border = paint.get_border();
3037 border.set_color(border.get_color().multiply_alpha(opacity));
3038 display_list.push(DrawCommand::FillRoundedRect {
3039 rect: bounds,
3040 color: COLOR_TRANSPARENT,
3041 radius: paint.get_radius(),
3042 border,
3043 });
3044 }
3045 if interaction.get_focused()
3046 && let Some(mut ring) = paint.focus_ring_border()
3047 {
3048 ring.set_color(ring.get_color().multiply_alpha(opacity));
3049 let ring_w: f32 = ring.get_width();
3050 let ring_rect: Rect = Rect::new(
3051 bounds.get_x() - ring_w,
3052 bounds.get_y() - ring_w,
3053 bounds.get_width() + ring_w * 2.0,
3054 bounds.get_height() + ring_w * 2.0,
3055 );
3056 display_list.push(DrawCommand::FillRoundedRect {
3057 rect: ring_rect,
3058 color: COLOR_TRANSPARENT,
3059 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
3060 border: ring,
3061 });
3062 }
3063 match node.get_kind() {
3064 NodeKind::Text(text) => {
3065 let mut text_style: TextStyle = style.text;
3066 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3067 let content_b: Rect = node.layout_box.get_content_bounds();
3068 let cx0: f32 = (content_b.get_x() * scale).round();
3069 let cy0: f32 = (content_b.get_y() * scale).round();
3070 let shifted_content: Rect = Rect::new(
3071 (cx0 + total_off_x) * inv_scale,
3072 (cy0 + total_off_y) * inv_scale,
3073 content_b.get_width(),
3074 content_b.get_height(),
3075 );
3076 let has_box_chrome: bool = style
3077 .paint
3078 .background_color()
3079 .is_some_and(|color_value: Color| color_value.get_alpha() > 0)
3080 || style.paint.background_gradient().is_some()
3081 || style.paint.get_border().get_width() > 0.0
3082 || style.layout.get_height() != Length::Auto
3083 || style.layout.get_min_height() != Length::Auto;
3084 let text_run_rect: Rect = if has_box_chrome
3085 && shifted_content.get_height() > 1.0
3086 {
3087 let cached_h: f32 = node.layout_box.get_content_size().get_height();
3088 let measured_h: f32 =
3089 if cached_h > 0.0 && cached_h + 0.5 < shifted_content.get_height() {
3090 cached_h
3091 } else {
3092 DeterministicTextMeasurer
3093 .measure_size(
3094 text.as_str(),
3095 text_style,
3096 Some(shifted_content.get_width().max(1.0)),
3097 )
3098 .get_height()
3099 };
3100 if measured_h > 0.0 && shifted_content.get_height() > measured_h + 0.5 {
3101 let v_off_phys: f32 =
3102 (((shifted_content.get_height() - measured_h) * 0.5).max(0.0)
3103 * scale)
3104 .round();
3105 Rect::new(
3106 shifted_content.get_x(),
3107 (cy0 + total_off_y + v_off_phys) * inv_scale,
3108 shifted_content.get_width(),
3109 measured_h,
3110 )
3111 } else {
3112 shifted_content
3113 }
3114 } else {
3115 shifted_content
3116 };
3117 if let Some(sel_range) = tree.text_selection_range_for(node_id) {
3118 let sel_color: Color = tree
3119 .effective_selection_color(node_id)
3120 .multiply_alpha(opacity);
3121 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3122 text.as_str(),
3123 text_style,
3124 text_run_rect,
3125 sel_range,
3126 );
3127 let sel_single_to_end: bool = sel_rects.len() == 1
3128 && text_style.get_align() == TextAlign::Left
3129 && sel_range.1 >= text.chars().count();
3130 let exact_span_right: f32 =
3131 ((content_b.right() * scale).round() + total_off_x) * inv_scale;
3132 for sel_rect in sel_rects.into_iter() {
3133 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3134 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3135 let sx1: f32 = if sel_single_to_end {
3136 exact_span_right.max(sx0 + inv_scale)
3137 } else {
3138 (sel_rect.right() * scale).round() * inv_scale
3139 };
3140 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3141 display_list.push(DrawCommand::FillRoundedRect {
3142 rect: Rect::new(
3143 sx0,
3144 sy0,
3145 (sx1 - sx0).max(inv_scale),
3146 (sy1 - sy0).max(inv_scale),
3147 ),
3148 color: sel_color,
3149 radius: CornerRadius::default(),
3150 border: Border::default(),
3151 });
3152 }
3153 }
3154 let pooled_text: String = display_list.alloc_text(text.as_str());
3155 display_list.push(DrawCommand::TextRun {
3156 rect: text_run_rect,
3157 text: pooled_text,
3158 style: text_style,
3159 });
3160 }
3161 NodeKind::Input(input_state) => {
3162 let mut text_style: TextStyle = style.text;
3163 let is_empty_value: bool = input_state.get_value().is_empty();
3164 let is_focused: bool = interaction.get_focused();
3165 let rendered_text: String = if is_empty_value {
3166 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
3167 display_list.alloc_text(input_state.get_placeholder().as_str())
3168 } else if input_state.get_password() {
3169 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3170 let star_count: usize = input_state.get_value().chars().count();
3171 let mut masked: String = display_list.alloc_text("");
3172 for _ in 0..star_count {
3173 masked.push('*');
3174 }
3175 masked
3176 } else {
3177 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3178 display_list.alloc_text(input_state.get_value().as_str())
3179 };
3180 let content_b: Rect = node.layout_box.get_content_bounds();
3181 let cx0: f32 = (content_b.get_x() * scale).round();
3182 let cy0: f32 = (content_b.get_y() * scale).round();
3183 let shifted_content: Rect = Rect::new(
3184 (cx0 + total_off_x) * inv_scale,
3185 (cy0 + total_off_y) * inv_scale,
3186 content_b.get_width(),
3187 content_b.get_height(),
3188 );
3189 let line_h: f32 =
3190 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
3191 let text_rect: Rect = if input_state.get_multiline() {
3192 shifted_content
3193 } else {
3194 text_style.set_overflow(TextOverflow::Clip);
3195 let v_off_phys: f32 =
3196 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
3197 .round();
3198 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
3199 Rect::new(
3200 shifted_content.get_x(),
3201 text_y,
3202 shifted_content.get_width(),
3203 line_h,
3204 )
3205 };
3206 let run_rect: Rect =
3207 if is_empty_value && is_focused && !rendered_text.is_empty() {
3208 let clearance_phys: f32 =
3209 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
3210 let clearance_logical: f32 = clearance_phys * inv_scale;
3211 Rect::new(
3212 text_rect.get_x() + clearance_logical,
3213 text_rect.get_y(),
3214 (text_rect.get_width() - clearance_logical).max(0.0),
3215 text_rect.get_height(),
3216 )
3217 } else {
3218 text_rect
3219 };
3220 let show_caret: bool = is_focused && tree.get_caret_visible();
3221 let caret_text: String = if show_caret && !is_empty_value {
3222 let caret_prefix: &str =
3223 input_state.caret_prefix_for_display(rendered_text.as_str());
3224 display_list.alloc_text(caret_prefix)
3225 } else {
3226 String::new()
3227 };
3228 display_list.push(DrawCommand::PushClip(shifted_content));
3229 if is_focused
3230 && !is_empty_value
3231 && let Some(sel_range) = input_state.selection_char_range()
3232 {
3233 let sel_color: Color = tree
3234 .effective_selection_color(node_id)
3235 .multiply_alpha(opacity);
3236 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3237 rendered_text.as_str(),
3238 text_style,
3239 text_rect,
3240 sel_range,
3241 );
3242 for sel_rect in sel_rects.into_iter() {
3243 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3244 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3245 let sx1: f32 = (sel_rect.right() * scale).round() * inv_scale;
3246 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3247 display_list.push(DrawCommand::FillRoundedRect {
3248 rect: Rect::new(
3249 sx0,
3250 sy0,
3251 (sx1 - sx0).max(inv_scale),
3252 (sy1 - sy0).max(inv_scale),
3253 ),
3254 color: sel_color,
3255 radius: CornerRadius::default(),
3256 border: Border::default(),
3257 });
3258 }
3259 }
3260 display_list.push(DrawCommand::TextRun {
3261 rect: run_rect,
3262 text: rendered_text,
3263 style: text_style,
3264 });
3265 if show_caret {
3266 let caret_h: f32 = (text_style.get_font_size() * 1.15)
3267 .min(shifted_content.get_height().max(12.0));
3268 display_list.push(DrawCommand::TextCaret {
3269 text_rect,
3270 content_rect: shifted_content,
3271 text: caret_text,
3272 style: text_style,
3273 caret_height: caret_h,
3274 color: style.text.get_color().multiply_alpha(opacity),
3275 multiline: input_state.get_multiline(),
3276 });
3277 }
3278 display_list.push(DrawCommand::PopClip);
3279 }
3280 NodeKind::Image(buffer) => {
3281 display_list.push(DrawCommand::Image {
3282 rect: bounds,
3283 buffer: buffer.clone(),
3284 radius: paint.get_radius(),
3285 });
3286 }
3287 NodeKind::Canvas(caching) => {
3288 if let Some(callback) = node.canvas_callback()
3289 && let Ok(mut func) = callback.try_borrow_mut()
3290 {
3291 let canvas_bw: f32 = paint.get_border().get_width();
3292 let canvas_inset: f32 = if canvas_bw > 0.0 {
3293 ((canvas_bw * scale).ceil().max(1.0)) * inv_scale
3294 } else {
3295 0.0
3296 };
3297 let canvas_clip_rect: Rect = if canvas_inset > 0.0 {
3298 Rect::new(
3299 bounds.get_x() + canvas_inset,
3300 bounds.get_y() + canvas_inset,
3301 (bounds.get_width() - canvas_inset * 2.0).max(0.0),
3302 (bounds.get_height() - canvas_inset * 2.0).max(0.0),
3303 )
3304 } else {
3305 bounds
3306 };
3307 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
3308 .min(bounds.get_height() * 0.5)
3309 .max(0.0);
3310 let canvas_raw_r: CornerRadius = paint.get_radius();
3311 let canvas_inner_r: CornerRadius = CornerRadius::new(
3312 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_inset).max(0.0),
3313 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_inset)
3314 .max(0.0),
3315 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_inset)
3316 .max(0.0),
3317 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_inset)
3318 .max(0.0),
3319 );
3320 let canvas_clip_saved_digest: (u64, u64) =
3321 display_list.snapshot_digest_state();
3322 if canvas_inner_r.max_radius() > 0.25 {
3323 display_list.push(DrawCommand::PushRoundedClip {
3324 rect: canvas_clip_rect,
3325 radius: canvas_inner_r,
3326 });
3327 } else {
3328 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
3329 }
3330 let canvas_cmd_start: usize = display_list.len();
3331 let canvas_body_saved_digest: (u64, u64) =
3332 display_list.snapshot_digest_state();
3333 func(bounds, display_list);
3334 display_list
3335 .sanitize_clip_balance(canvas_cmd_start, canvas_body_saved_digest);
3336 let canvas_cmd_end: usize = display_list.len();
3337 if opacity < 0.999 {
3338 display_list.attenuate_range_opacity(
3339 canvas_cmd_start,
3340 canvas_cmd_end,
3341 opacity,
3342 );
3343 }
3344 if *caching != CanvasCaching::RetainedStatic {
3345 let canvas_is_dynamic: bool =
3346 if let Some(cache) = scratch.fragments.as_deref_mut() {
3347 let emitted: &[DrawCommand] = &display_list.get_commands()
3348 [canvas_cmd_start..canvas_cmd_end];
3349 cache.record_canvas_output(
3350 node_id,
3351 phys_scale_milli,
3352 bounds,
3353 tree.get_revision(),
3354 emitted,
3355 )
3356 } else {
3357 true
3358 };
3359 if canvas_is_dynamic {
3360 *scratch.saw_canvas = true;
3361 }
3362 }
3363 if canvas_cmd_end == canvas_cmd_start {
3364 display_list.pop_empty_clip(canvas_clip_saved_digest);
3365 } else {
3366 display_list.push(DrawCommand::PopClip);
3367 }
3368 if canvas_bw > 0.0 && canvas_raw_r.max_radius() > 0.0 {
3369 let mut canvas_border: Border = paint.get_border();
3370 canvas_border
3371 .set_color(canvas_border.get_color().multiply_alpha(opacity));
3372 display_list.push(DrawCommand::FillRoundedRect {
3373 rect: bounds,
3374 color: COLOR_TRANSPARENT,
3375 radius: canvas_raw_r,
3376 border: canvas_border,
3377 });
3378 }
3379 } else if *caching == CanvasCaching::DynamicPerFrame {
3380 *scratch.saw_canvas = true;
3381 }
3382 }
3383 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
3384 }
3385 }
3386 let clip_push_idx: usize = display_list.len();
3387 let clip_saved_digest: (u64, u64) = display_list.snapshot_digest_state();
3388 if emit_clip {
3389 let border_width: f32 = paint.get_border().get_width();
3390 let border_inset: f32 = if border_width > 0.0 {
3391 ((border_width * scale).ceil().max(1.0)) * inv_scale
3392 } else {
3393 0.0
3394 };
3395 let clip_rect: Rect = if border_inset > 0.0 {
3396 Rect::new(
3397 bounds.get_x() + border_inset,
3398 bounds.get_y() + border_inset,
3399 (bounds.get_width() - border_inset * 2.0).max(0.0),
3400 (bounds.get_height() - border_inset * 2.0).max(0.0),
3401 )
3402 } else {
3403 bounds
3404 };
3405 let max_r: f32 = (bounds.get_width() * 0.5)
3406 .min(bounds.get_height() * 0.5)
3407 .max(0.0);
3408 let raw_r: CornerRadius = paint.get_radius();
3409 let inner_r: CornerRadius = CornerRadius::new(
3410 (raw_r.get_top_left().min(max_r) - border_inset).max(0.0),
3411 (raw_r.get_top_right().min(max_r) - border_inset).max(0.0),
3412 (raw_r.get_bottom_right().min(max_r) - border_inset).max(0.0),
3413 (raw_r.get_bottom_left().min(max_r) - border_inset).max(0.0),
3414 );
3415 if inner_r.max_radius() > 0.25 {
3416 display_list.push(DrawCommand::PushRoundedClip {
3417 rect: clip_rect,
3418 radius: inner_r,
3419 });
3420 } else {
3421 display_list.push(DrawCommand::PushClip(clip_rect));
3422 }
3423 }
3424 if child_count > 0 {
3425 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3426 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
3427 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
3428 let child_offset_phys: Point =
3429 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
3430 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
3431 if !needs_z_sort {
3432 for child_id in tree.children_iter(node_id) {
3433 Self::emit_node(
3434 tree,
3435 child_id,
3436 (child_offset_phys, opacity),
3437 next_cull,
3438 active_hover_target,
3439 scratch,
3440 display_list,
3441 );
3442 }
3443 } else {
3444 let sort_start: usize = scratch.z_sort.len();
3445 for (idx, cid) in tree.children_iter(node_id).enumerate() {
3446 if let Some(char_item) = tree.get_node(cid) {
3447 scratch
3448 .z_sort
3449 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
3450 }
3451 }
3452 let sort_end: usize = scratch.z_sort.len();
3453 scratch.z_sort[sort_start..sort_end].sort_by(
3454 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
3455 target_node_id
3456 .0
3457 .cmp(&element_id.0)
3458 .then_with(|| target_node_id.1.cmp(&element_id.1))
3459 },
3460 );
3461 for slot_idx in sort_start..sort_end {
3462 let child_id: NodeId = scratch.z_sort[slot_idx].2;
3463 Self::emit_node(
3464 tree,
3465 child_id,
3466 (child_offset_phys, opacity),
3467 next_cull,
3468 active_hover_target,
3469 scratch,
3470 display_list,
3471 );
3472 }
3473 scratch.z_sort.truncate(sort_start);
3474 }
3475 }
3476 if wants_scrollbar {
3477 let computed_box: &LayoutBox = &node.layout_box;
3478 let border_w: f32 = paint.get_border().get_width();
3479 let inner_x: f32 = bounds.get_x() + border_w;
3480 let inner_y: f32 = bounds.get_y() + border_w;
3481 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
3482 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
3483 let view_w: f32 = computed_box.get_content_bounds().get_width().max(1.0);
3484 let view_h: f32 = computed_box.get_content_bounds().get_height().max(1.0);
3485 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
3486 Some((vy, vh)) => (vy, vh),
3487 None => (
3488 computed_box.get_scroll_offset().get_y(),
3489 computed_box.get_content_size().get_height(),
3490 ),
3491 };
3492 let scroll_x: f32 = computed_box.get_scroll_offset().get_x();
3493 let total_w: f32 = computed_box.get_content_size().get_width();
3494 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
3495 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
3496 let has_v_bar: bool = max_scroll_y > 1.0;
3497 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
3498 let corner_inset: f32 = if has_v_bar && has_h_bar { 6.0 } else { 0.0 };
3499 if has_v_bar {
3500 let track_margin: f32 = 3.0;
3501 let edge_inset: f32 = 1.0;
3502 let thumb_w: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3503 let track_x: f32 = (inner_x + inner_w - thumb_w - edge_inset).max(inner_x);
3504 let track_y: f32 = inner_y + track_margin;
3505 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
3506 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
3507 let min_thumb_h: f32 = 20.0_f32.min(track_h);
3508 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
3509 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
3510 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
3511 let thumb_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3512 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3513 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
3514 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3515 let thumb_alpha: u8 = if pressed {
3516 188
3517 } else if hovered {
3518 148
3519 } else {
3520 88
3521 };
3522 display_list.push(DrawCommand::FillRoundedRect {
3523 rect: Rect::new(
3524 thumb_x_snapped,
3525 thumb_y_snapped,
3526 thumb_w_snapped,
3527 thumb_h_snapped,
3528 ),
3529 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3530 radius: CornerRadius::all(thumb_w * 0.5),
3531 border: Border::default(),
3532 });
3533 }
3534 if has_h_bar {
3535 let track_margin: f32 = 3.0;
3536 let edge_inset: f32 = 1.0;
3537 let thumb_h: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3538 let track_y: f32 = (inner_y + inner_h - thumb_h - edge_inset).max(inner_y);
3539 let track_x: f32 = inner_x + track_margin;
3540 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
3541 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
3542 let min_thumb_w: f32 = 20.0_f32.min(track_w);
3543 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
3544 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
3545 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
3546 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
3547 let thumb_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3548 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3549 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3550 let thumb_alpha: u8 = if pressed {
3551 188
3552 } else if hovered {
3553 148
3554 } else {
3555 88
3556 };
3557 display_list.push(DrawCommand::FillRoundedRect {
3558 rect: Rect::new(
3559 thumb_x_snapped,
3560 thumb_y_snapped,
3561 thumb_w_snapped,
3562 thumb_h_snapped,
3563 ),
3564 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3565 radius: CornerRadius::all(thumb_h * 0.5),
3566 border: Border::default(),
3567 });
3568 }
3569 }
3570 if emit_clip {
3571 if display_list.len() == clip_push_idx + 1 {
3572 display_list.pop_empty_clip(clip_saved_digest);
3573 if self_visible && defer_rounded_border && !has_canvas_clip {
3574 let mut outer_border: Border = paint.get_border();
3575 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3576 display_list.push(DrawCommand::FillRoundedRect {
3577 rect: bounds,
3578 color: COLOR_TRANSPARENT,
3579 radius: paint.get_radius(),
3580 border: outer_border,
3581 });
3582 }
3583 } else {
3584 display_list.push(DrawCommand::PopClip);
3585 if self_visible && has_rounded_border && !has_canvas_clip {
3586 let mut outer_border: Border = paint.get_border();
3587 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3588 display_list.push(DrawCommand::FillRoundedRect {
3589 rect: bounds,
3590 color: COLOR_TRANSPARENT,
3591 radius: paint.get_radius(),
3592 border: outer_border,
3593 });
3594 }
3595 }
3596 }
3597 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
3598 && let Some(cache) = scratch.fragments.as_deref_mut()
3599 {
3600 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
3601 cache.capture_fragment(
3602 slot_idx,
3603 (
3604 node_id,
3605 phys_scale_milli,
3606 parent_opacity.to_bits(),
3607 offset_phys,
3608 geom_hash,
3609 style_text_hash,
3610 ),
3611 cmd_slice,
3612 );
3613 }
3614 }
3615}