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 pub(crate) fn is_overlay_backdrop_node(node: &SceneNode) -> bool {
2670 let style: &Style = node.style_ref();
2671 let layout: &LayoutStyle = style.layout_ref();
2672 let paint: &PaintStyle = style.paint_ref();
2673 if matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed {
2674 return true;
2675 }
2676 if layout.get_position() != Position::Absolute || node.hover_paint().is_some() {
2677 return false;
2678 }
2679 let no_border: bool = paint.get_border().get_width() <= 0.0;
2680 let no_radius: bool = paint.get_radius().max_radius() <= 0.0;
2681 let full_inset_cover: bool = layout.top_offset().is_some()
2682 && layout.right_offset().is_some()
2683 && layout.bottom_offset().is_some()
2684 && layout.left_offset().is_some();
2685 let full_percent_cover: bool = layout.get_width() == Length::Percent(100.0)
2686 && layout.get_height() == Length::Percent(100.0);
2687 if no_border && no_radius && (full_inset_cover || full_percent_cover) {
2688 return true;
2689 }
2690 let has_visible_bg: bool = paint
2691 .background_color()
2692 .is_some_and(|bg_color: Color| bg_color.get_alpha() > 0)
2693 || paint.background_gradient().is_some();
2694 let spans_axis: bool = (layout.left_offset().is_some() && layout.right_offset().is_some())
2695 || (layout.top_offset().is_some() && layout.bottom_offset().is_some())
2696 || layout.get_width() == Length::Percent(100.0)
2697 || layout.get_height() == Length::Percent(100.0);
2698 !has_visible_bg && no_border && no_radius && spans_axis
2699 }
2700
2701 #[must_use]
2711 pub(crate) fn is_auto_hover_tint_eligible(node: &SceneNode) -> bool {
2712 if Self::is_overlay_backdrop_node(node) {
2713 return false;
2714 }
2715 let paint: &PaintStyle = node.style_ref().paint_ref();
2716 let has_visible_bg: bool = paint
2717 .background_color()
2718 .is_some_and(|bg_color: Color| bg_color.get_alpha() > 0)
2719 || paint.background_gradient().is_some();
2720 let is_transparent_media: bool =
2721 matches!(node.get_kind(), NodeKind::Canvas(_) | NodeKind::Image(_)) && !has_visible_bg;
2722 if is_transparent_media {
2723 return false;
2724 }
2725 paint.get_cursor() == CursorIcon::Pointer
2726 || node
2727 .get_handlers()
2728 .handler_for(&UiEventKind::Click(Point::default()))
2729 .is_some()
2730 }
2731
2732 #[must_use]
2743 pub(crate) fn resolve_hover_tint_target(
2744 tree: &SceneTree,
2745 start: Option<NodeId>,
2746 ) -> Option<NodeId> {
2747 let mut cursor: Option<NodeId> = start;
2748 while let Some(id) = cursor {
2749 let Some(node) = tree.get_node(id) else {
2750 break;
2751 };
2752 if Self::is_auto_hover_tint_eligible(node) {
2753 return Some(id);
2754 }
2755 let style: &Style = node.style_ref();
2756 let is_top_layer_barrier: bool = matches!(node.get_kind(), NodeKind::Portal)
2757 || style.layout_ref().get_position() == Position::Fixed;
2758 if is_top_layer_barrier || style.paint_ref().get_stop_event_propagation() {
2759 break;
2760 }
2761 cursor = node.parent_id();
2762 }
2763 None
2764 }
2765
2766 #[must_use]
2776 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
2777 Self::resolve_hover_tint_target(tree, tree.get_focus().hovered_node())
2778 }
2779
2780 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
2792 if !hovered && !pressed {
2793 return base;
2794 }
2795 let delta: i32 = if pressed { 24 } else { 14 };
2796 match base {
2797 Some(char_item) if char_item.get_alpha() > 0 => {
2798 let luma: u32 = (char_item.get_red() as u32) * 299
2799 + (char_item.get_green() as u32) * 587
2800 + (char_item.get_blue() as u32) * 114;
2801 if luma < 120_000 {
2802 let lift: i32 = if pressed { 30 } else { 20 };
2803 Some(Color::new(
2804 (char_item.get_red() as i32 + lift).clamp(0, 255) as u8,
2805 (char_item.get_green() as i32 + lift).clamp(0, 255) as u8,
2806 (char_item.get_blue() as i32 + lift).clamp(0, 255) as u8,
2807 char_item.get_alpha(),
2808 ))
2809 } else {
2810 Some(Color::new(
2811 (char_item.get_red() as i32 - delta).clamp(0, 255) as u8,
2812 (char_item.get_green() as i32 - delta).clamp(0, 255) as u8,
2813 (char_item.get_blue() as i32 - delta).clamp(0, 255) as u8,
2814 char_item.get_alpha(),
2815 ))
2816 }
2817 }
2818 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
2819 }
2820 }
2821
2822 fn emit_node(
2834 tree: &SceneTree,
2835 node_id: NodeId,
2836 offset_and_opacity: (Point, f32),
2837 cull_rect: Rect,
2838 active_hover_target: Option<NodeId>,
2839 scratch: &mut PaintScratch<'_>,
2840 display_list: &mut DisplayList,
2841 ) {
2842 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
2843 let Some(node) = tree.get_node(node_id) else {
2844 return;
2845 };
2846 let style: &Style = node.style_ref();
2847 let layout: &LayoutStyle = &style.layout;
2848 if layout.get_display() == Display::None {
2849 return;
2850 }
2851 let is_portal_or_fixed: bool =
2852 matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
2853 let is_root_chrome_overlay: bool = layout.get_position() == Position::Absolute
2854 && layout.get_z_index() >= 50
2855 && layout.get_z_index() < 140
2856 && offset_phys == Point::new(0.0, 0.0)
2857 && (parent_opacity - 1.0).abs() <= 0.001
2858 && cull_rect.get_x() <= 0.0
2859 && cull_rect.get_y() <= 0.0;
2860 let promote_to_top_layer: bool = is_portal_or_fixed || is_root_chrome_overlay;
2861 if let Some(portal_queue) = scratch.portals.as_deref_mut()
2862 && promote_to_top_layer
2863 {
2864 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
2865 return;
2866 }
2867 let interaction: InteractionState = node.get_interaction();
2868 let hovered: bool = interaction.get_hovered();
2869 let pressed: bool = interaction.get_pressed();
2870 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
2871 return;
2872 }
2873 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
2874 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2875 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
2876 if opacity <= 0.0 {
2877 return;
2878 }
2879 let scale: f32 = display_list.get_scale_factor().max(0.5);
2880 let inv_scale: f32 = 1.0 / scale;
2881 let raw_bounds: Rect = node.layout_box.get_bounds();
2882 let transform: Transform2D = prelim_paint.get_transform();
2883 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
2884 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
2885 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
2886 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
2887 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
2888 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
2889 let raw_x1: f32 = (raw_bounds.right() * scale).round();
2890 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
2891 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
2892 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
2893 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
2894 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
2895 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
2896 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
2897 let bounds: Rect = Rect::new(
2898 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
2899 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
2900 scaled_w_phys * inv_scale,
2901 scaled_h_phys * inv_scale,
2902 );
2903 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2904 let clipped_cull: Option<Rect> = if clipped {
2905 cull_rect.intersect(bounds)
2906 } else {
2907 Some(cull_rect)
2908 };
2909 let focused: bool = interaction.get_focused();
2910 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
2911 node,
2912 bounds,
2913 prelim_paint,
2914 focused,
2915 (total_off_x, total_off_y),
2916 scale,
2917 inv_scale,
2918 );
2919 let self_visible: bool = bounds.get_width() > 0.0
2920 && bounds.get_height() > 0.0
2921 && self_vis_rect.intersect(cull_rect).is_some();
2922 if clipped && clipped_cull.is_none() {
2923 if self_visible {
2924 if let Some(mut shadow) = prelim_paint.box_shadow() {
2925 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2926 display_list.push(DrawCommand::BoxShadow {
2927 rect: bounds,
2928 radius: prelim_paint.get_radius(),
2929 shadow,
2930 });
2931 }
2932 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
2933 ring.set_color(ring.get_color().multiply_alpha(opacity));
2934 let ring_w: f32 = ring.get_width();
2935 let ring_rect: Rect = Rect::new(
2936 bounds.get_x() - ring_w,
2937 bounds.get_y() - ring_w,
2938 bounds.get_width() + ring_w * 2.0,
2939 bounds.get_height() + ring_w * 2.0,
2940 );
2941 display_list.push(DrawCommand::FillRoundedRect {
2942 rect: ring_rect,
2943 color: COLOR_TRANSPARENT,
2944 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
2945 border: ring,
2946 });
2947 }
2948 }
2949 if scratch.portals.is_some()
2950 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
2951 {
2952 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
2953 }
2954 return;
2955 }
2956 let child_count: usize = tree.child_count(node_id);
2957 if !self_visible && child_count == 0 {
2958 return;
2959 }
2960 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
2961 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
2962 if self_visible
2963 && (hover_visual || pressed)
2964 && paint_owned.is_none()
2965 && (hovered || node.hover_paint().is_none())
2966 && Self::is_auto_hover_tint_eligible(node)
2967 {
2968 let mut tinted: PaintStyle = *style.paint_ref();
2969 tinted.set_background(Self::interactive_tint(
2970 tinted.background_color(),
2971 hover_visual,
2972 pressed,
2973 ));
2974 paint_owned = Some(tinted);
2975 }
2976 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2977 let phys_scale_milli: u32 = Self::quantize_scale_milli(scale);
2978 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
2979 let frag_overflow_ok: bool = layout.get_overflow() == Overflow::Hidden
2980 || (layout.get_overflow() == Overflow::Visible
2981 && node_id != tree.get_root()
2982 && child_count >= 2);
2983 if self_visible
2984 && scratch.fragments.is_some()
2985 && scratch.portals.is_some()
2986 && matches!(node.get_kind(), NodeKind::View)
2987 && frag_overflow_ok
2988 && !hovered
2989 && !pressed
2990 && !interaction.get_focused()
2991 && active_hover_target != Some(node_id)
2992 {
2993 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
2994 && self_vis_rect.right() <= cull_rect.right();
2995 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
2996 && self_vis_rect.bottom() <= cull_rect.bottom();
2997 if vis_x_in && vis_y_in {
2998 let has_text_sel: bool = !tree.text_selection_spans_ref().is_empty();
2999 let unclipped_cull: Option<(Point, Rect, f32, f32)> =
3000 if layout.get_overflow() == Overflow::Hidden {
3001 None
3002 } else {
3003 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3004 let child_off: Point = Point::new(
3005 total_off_x - (raw_node_scroll.get_x() * scale).round(),
3006 total_off_y - (raw_node_scroll.get_y() * scale).round(),
3007 );
3008 Some((child_off, cull_rect, scale, inv_scale))
3009 };
3010 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_subtree(
3011 tree,
3012 node_id,
3013 node,
3014 unclipped_cull,
3015 (active_hover_target, has_text_sel),
3016 ) {
3017 let slot_idx: usize =
3018 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
3019 let opacity_bits: u32 = parent_opacity.to_bits();
3020 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
3021 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
3022 if let Some(cache) = scratch.fragments.as_deref_mut()
3023 && let Some(&cached) = cache.slots.get(slot_idx)
3024 && cached.occupied
3025 && cached.node_id == node_id
3026 && cached.phys_scale_milli == phys_scale_milli
3027 && cached.opacity_bits == opacity_bits
3028 && cached.geom_hash == geom_hash
3029 && cached.style_text_hash == style_text_hash
3030 {
3031 let dx_phys: f32 = offset_phys.get_x() - cached.offset_phys.get_x();
3032 let dy_phys: f32 = offset_phys.get_y() - cached.offset_phys.get_y();
3033 let c_start: usize = cached.cmd_start as usize;
3034 let c_end: usize = c_start.saturating_add(cached.cmd_len as usize);
3035 if let Some(cmds) = cache.cmd_slab.get(c_start..c_end) {
3036 display_list.get_mut_commands().reserve(cmds.len());
3037 let text_slab: &[u8] = cache.text_slab.as_slice();
3038 for cmd in cmds.iter() {
3039 Self::push_translated_command(
3040 cmd,
3041 text_slab,
3042 (dx_phys, dy_phys),
3043 (scale, inv_scale, dyadic_scale),
3044 display_list,
3045 );
3046 }
3047 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
3048 return;
3049 }
3050 }
3051 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
3052 }
3053 }
3054 }
3055 let box_size: Size = display_list.get_viewport();
3056 let show_scrollbar: bool =
3057 !tree.get_scrollbars_suppressed() && Self::viewport_shows_desktop_scrollbars(box_size);
3058 let wants_scrollbar: bool = show_scrollbar
3059 && layout.get_overflow() == Overflow::Scroll
3060 && bounds.get_height() > 24.0;
3061 let emit_clip: bool = clipped && (child_count > 0 || wants_scrollbar);
3062 let has_canvas_clip: bool =
3063 matches!(node.get_kind(), NodeKind::Canvas(_)) && node.canvas_callback().is_some();
3064 let has_rounded_border: bool =
3065 paint.get_border().get_width() > 0.0 && paint.get_radius().max_radius() > 0.0;
3066 let defer_rounded_border: bool = has_rounded_border && (emit_clip || has_canvas_clip);
3067 if self_visible {
3068 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
3069 if blur_r > 0.0 {
3070 display_list.push(DrawCommand::BackdropBlur {
3071 rect: bounds,
3072 radius: paint.get_radius(),
3073 blur_radius: blur_r,
3074 });
3075 }
3076 if let Some(mut shadow) = paint.box_shadow() {
3077 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
3078 display_list.push(DrawCommand::BoxShadow {
3079 rect: bounds,
3080 radius: paint.get_radius(),
3081 shadow,
3082 });
3083 }
3084 if let Some(mut gradient) = paint.background_gradient() {
3085 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
3086 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
3087 display_list.push(DrawCommand::LinearGradientRect {
3088 rect: bounds,
3089 gradient,
3090 radius: paint.get_radius(),
3091 });
3092 } else if let Some(bg) = paint.background_color() {
3093 let border: Border = if defer_rounded_border {
3094 Border::default()
3095 } else {
3096 let mut direct_border: Border = paint.get_border();
3097 direct_border.set_color(direct_border.get_color().multiply_alpha(opacity));
3098 direct_border
3099 };
3100 display_list.push(DrawCommand::FillRoundedRect {
3101 rect: bounds,
3102 color: bg.multiply_alpha(opacity),
3103 radius: paint.get_radius(),
3104 border,
3105 });
3106 } else if paint.get_border().get_width() > 0.0 && !defer_rounded_border {
3107 let mut border: Border = paint.get_border();
3108 border.set_color(border.get_color().multiply_alpha(opacity));
3109 display_list.push(DrawCommand::FillRoundedRect {
3110 rect: bounds,
3111 color: COLOR_TRANSPARENT,
3112 radius: paint.get_radius(),
3113 border,
3114 });
3115 }
3116 if interaction.get_focused()
3117 && let Some(mut ring) = paint.focus_ring_border()
3118 {
3119 ring.set_color(ring.get_color().multiply_alpha(opacity));
3120 let ring_w: f32 = ring.get_width();
3121 let ring_rect: Rect = Rect::new(
3122 bounds.get_x() - ring_w,
3123 bounds.get_y() - ring_w,
3124 bounds.get_width() + ring_w * 2.0,
3125 bounds.get_height() + ring_w * 2.0,
3126 );
3127 display_list.push(DrawCommand::FillRoundedRect {
3128 rect: ring_rect,
3129 color: COLOR_TRANSPARENT,
3130 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
3131 border: ring,
3132 });
3133 }
3134 match node.get_kind() {
3135 NodeKind::Text(text) => {
3136 let mut text_style: TextStyle = style.text;
3137 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3138 let content_b: Rect = node.layout_box.get_content_bounds();
3139 let cx0: f32 = (content_b.get_x() * scale).round();
3140 let cy0: f32 = (content_b.get_y() * scale).round();
3141 let shifted_content: Rect = Rect::new(
3142 (cx0 + total_off_x) * inv_scale,
3143 (cy0 + total_off_y) * inv_scale,
3144 content_b.get_width(),
3145 content_b.get_height(),
3146 );
3147 let has_box_chrome: bool = style
3148 .paint
3149 .background_color()
3150 .is_some_and(|color_value: Color| color_value.get_alpha() > 0)
3151 || style.paint.background_gradient().is_some()
3152 || style.paint.get_border().get_width() > 0.0
3153 || style.layout.get_height() != Length::Auto
3154 || style.layout.get_min_height() != Length::Auto;
3155 let text_run_rect: Rect = if has_box_chrome
3156 && shifted_content.get_height() > 1.0
3157 {
3158 let cached_h: f32 = node.layout_box.get_content_size().get_height();
3159 let measured_h: f32 =
3160 if cached_h > 0.0 && cached_h + 0.5 < shifted_content.get_height() {
3161 cached_h
3162 } else {
3163 DeterministicTextMeasurer
3164 .measure_size(
3165 text.as_str(),
3166 text_style,
3167 Some(shifted_content.get_width().max(1.0)),
3168 )
3169 .get_height()
3170 };
3171 if measured_h > 0.0 && shifted_content.get_height() > measured_h + 0.5 {
3172 let v_off_phys: f32 =
3173 (((shifted_content.get_height() - measured_h) * 0.5).max(0.0)
3174 * scale)
3175 .round();
3176 Rect::new(
3177 shifted_content.get_x(),
3178 (cy0 + total_off_y + v_off_phys) * inv_scale,
3179 shifted_content.get_width(),
3180 measured_h,
3181 )
3182 } else {
3183 shifted_content
3184 }
3185 } else {
3186 shifted_content
3187 };
3188 if let Some(sel_range) = tree.text_selection_range_for(node_id) {
3189 let sel_color: Color = tree
3190 .effective_selection_color(node_id)
3191 .multiply_alpha(opacity);
3192 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3193 text.as_str(),
3194 text_style,
3195 text_run_rect,
3196 sel_range,
3197 );
3198 let sel_single_to_end: bool = sel_rects.len() == 1
3199 && text_style.get_align() == TextAlign::Left
3200 && sel_range.1 >= text.chars().count();
3201 let exact_span_right: f32 =
3202 ((content_b.right() * scale).round() + total_off_x) * inv_scale;
3203 for sel_rect in sel_rects.into_iter() {
3204 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3205 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3206 let sx1: f32 = if sel_single_to_end {
3207 exact_span_right.max(sx0 + inv_scale)
3208 } else {
3209 (sel_rect.right() * scale).round() * inv_scale
3210 };
3211 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3212 display_list.push(DrawCommand::FillRoundedRect {
3213 rect: Rect::new(
3214 sx0,
3215 sy0,
3216 (sx1 - sx0).max(inv_scale),
3217 (sy1 - sy0).max(inv_scale),
3218 ),
3219 color: sel_color,
3220 radius: CornerRadius::default(),
3221 border: Border::default(),
3222 });
3223 }
3224 }
3225 let pooled_text: String = display_list.alloc_text(text.as_str());
3226 display_list.push(DrawCommand::TextRun {
3227 rect: text_run_rect,
3228 text: pooled_text,
3229 style: text_style,
3230 });
3231 }
3232 NodeKind::Input(input_state) => {
3233 let mut text_style: TextStyle = style.text;
3234 let is_empty_value: bool = input_state.get_value().is_empty();
3235 let is_focused: bool = interaction.get_focused();
3236 let rendered_text: String = if is_empty_value {
3237 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
3238 display_list.alloc_text(input_state.get_placeholder().as_str())
3239 } else if input_state.get_password() {
3240 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3241 let star_count: usize = input_state.get_value().chars().count();
3242 let mut masked: String = display_list.alloc_text("");
3243 for _ in 0..star_count {
3244 masked.push('*');
3245 }
3246 masked
3247 } else {
3248 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
3249 display_list.alloc_text(input_state.get_value().as_str())
3250 };
3251 let content_b: Rect = node.layout_box.get_content_bounds();
3252 let cx0: f32 = (content_b.get_x() * scale).round();
3253 let cy0: f32 = (content_b.get_y() * scale).round();
3254 let shifted_content: Rect = Rect::new(
3255 (cx0 + total_off_x) * inv_scale,
3256 (cy0 + total_off_y) * inv_scale,
3257 content_b.get_width(),
3258 content_b.get_height(),
3259 );
3260 let line_h: f32 =
3261 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
3262 let text_rect: Rect = if input_state.get_multiline() {
3263 shifted_content
3264 } else {
3265 text_style.set_overflow(TextOverflow::Clip);
3266 let v_off_phys: f32 =
3267 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
3268 .round();
3269 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
3270 Rect::new(
3271 shifted_content.get_x(),
3272 text_y,
3273 shifted_content.get_width(),
3274 line_h,
3275 )
3276 };
3277 let run_rect: Rect =
3278 if is_empty_value && is_focused && !rendered_text.is_empty() {
3279 let clearance_phys: f32 =
3280 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
3281 let clearance_logical: f32 = clearance_phys * inv_scale;
3282 Rect::new(
3283 text_rect.get_x() + clearance_logical,
3284 text_rect.get_y(),
3285 (text_rect.get_width() - clearance_logical).max(0.0),
3286 text_rect.get_height(),
3287 )
3288 } else {
3289 text_rect
3290 };
3291 let show_caret: bool = is_focused && tree.get_caret_visible();
3292 let caret_text: String = if show_caret && !is_empty_value {
3293 let caret_prefix: &str =
3294 input_state.caret_prefix_for_display(rendered_text.as_str());
3295 display_list.alloc_text(caret_prefix)
3296 } else {
3297 String::new()
3298 };
3299 display_list.push(DrawCommand::PushClip(shifted_content));
3300 if is_focused
3301 && !is_empty_value
3302 && let Some(sel_range) = input_state.selection_char_range()
3303 {
3304 let sel_color: Color = tree
3305 .effective_selection_color(node_id)
3306 .multiply_alpha(opacity);
3307 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3308 rendered_text.as_str(),
3309 text_style,
3310 text_rect,
3311 sel_range,
3312 );
3313 for sel_rect in sel_rects.into_iter() {
3314 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3315 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3316 let sx1: f32 = (sel_rect.right() * scale).round() * inv_scale;
3317 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3318 display_list.push(DrawCommand::FillRoundedRect {
3319 rect: Rect::new(
3320 sx0,
3321 sy0,
3322 (sx1 - sx0).max(inv_scale),
3323 (sy1 - sy0).max(inv_scale),
3324 ),
3325 color: sel_color,
3326 radius: CornerRadius::default(),
3327 border: Border::default(),
3328 });
3329 }
3330 }
3331 display_list.push(DrawCommand::TextRun {
3332 rect: run_rect,
3333 text: rendered_text,
3334 style: text_style,
3335 });
3336 if show_caret {
3337 let caret_h: f32 = (text_style.get_font_size() * 1.15)
3338 .min(shifted_content.get_height().max(12.0));
3339 display_list.push(DrawCommand::TextCaret {
3340 text_rect,
3341 content_rect: shifted_content,
3342 text: caret_text,
3343 style: text_style,
3344 caret_height: caret_h,
3345 color: style.text.get_color().multiply_alpha(opacity),
3346 multiline: input_state.get_multiline(),
3347 });
3348 }
3349 display_list.push(DrawCommand::PopClip);
3350 }
3351 NodeKind::Image(buffer) => {
3352 display_list.push(DrawCommand::Image {
3353 rect: bounds,
3354 buffer: buffer.clone(),
3355 radius: paint.get_radius(),
3356 });
3357 }
3358 NodeKind::Canvas(caching) => {
3359 if let Some(callback) = node.canvas_callback()
3360 && let Ok(mut func) = callback.try_borrow_mut()
3361 {
3362 let canvas_bw: f32 = paint.get_border().get_width();
3363 let canvas_inset: f32 = if canvas_bw > 0.0 {
3364 ((canvas_bw * scale).ceil().max(1.0)) * inv_scale
3365 } else {
3366 0.0
3367 };
3368 let canvas_clip_rect: Rect = if canvas_inset > 0.0 {
3369 Rect::new(
3370 bounds.get_x() + canvas_inset,
3371 bounds.get_y() + canvas_inset,
3372 (bounds.get_width() - canvas_inset * 2.0).max(0.0),
3373 (bounds.get_height() - canvas_inset * 2.0).max(0.0),
3374 )
3375 } else {
3376 bounds
3377 };
3378 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
3379 .min(bounds.get_height() * 0.5)
3380 .max(0.0);
3381 let canvas_raw_r: CornerRadius = paint.get_radius();
3382 let canvas_inner_r: CornerRadius = CornerRadius::new(
3383 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_inset).max(0.0),
3384 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_inset)
3385 .max(0.0),
3386 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_inset)
3387 .max(0.0),
3388 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_inset)
3389 .max(0.0),
3390 );
3391 let canvas_clip_saved_digest: (u64, u64) =
3392 display_list.snapshot_digest_state();
3393 if canvas_inner_r.max_radius() > 0.25 {
3394 display_list.push(DrawCommand::PushRoundedClip {
3395 rect: canvas_clip_rect,
3396 radius: canvas_inner_r,
3397 });
3398 } else {
3399 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
3400 }
3401 let canvas_cmd_start: usize = display_list.len();
3402 let canvas_body_saved_digest: (u64, u64) =
3403 display_list.snapshot_digest_state();
3404 func(bounds, display_list);
3405 display_list
3406 .sanitize_clip_balance(canvas_cmd_start, canvas_body_saved_digest);
3407 let canvas_cmd_end: usize = display_list.len();
3408 if opacity < 0.999 {
3409 display_list.attenuate_range_opacity(
3410 canvas_cmd_start,
3411 canvas_cmd_end,
3412 opacity,
3413 );
3414 }
3415 if *caching != CanvasCaching::RetainedStatic {
3416 let canvas_is_dynamic: bool =
3417 if let Some(cache) = scratch.fragments.as_deref_mut() {
3418 let emitted: &[DrawCommand] = &display_list.get_commands()
3419 [canvas_cmd_start..canvas_cmd_end];
3420 cache.record_canvas_output(
3421 node_id,
3422 phys_scale_milli,
3423 bounds,
3424 tree.get_revision(),
3425 emitted,
3426 )
3427 } else {
3428 true
3429 };
3430 if canvas_is_dynamic {
3431 *scratch.saw_canvas = true;
3432 }
3433 }
3434 if canvas_cmd_end == canvas_cmd_start {
3435 display_list.pop_empty_clip(canvas_clip_saved_digest);
3436 } else {
3437 display_list.push(DrawCommand::PopClip);
3438 }
3439 if canvas_bw > 0.0 && canvas_raw_r.max_radius() > 0.0 {
3440 let mut canvas_border: Border = paint.get_border();
3441 canvas_border
3442 .set_color(canvas_border.get_color().multiply_alpha(opacity));
3443 display_list.push(DrawCommand::FillRoundedRect {
3444 rect: bounds,
3445 color: COLOR_TRANSPARENT,
3446 radius: canvas_raw_r,
3447 border: canvas_border,
3448 });
3449 }
3450 } else if *caching == CanvasCaching::DynamicPerFrame {
3451 *scratch.saw_canvas = true;
3452 }
3453 }
3454 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
3455 }
3456 }
3457 let clip_push_idx: usize = display_list.len();
3458 let clip_saved_digest: (u64, u64) = display_list.snapshot_digest_state();
3459 if emit_clip {
3460 let border_width: f32 = paint.get_border().get_width();
3461 let border_inset: f32 = if border_width > 0.0 {
3462 ((border_width * scale).ceil().max(1.0)) * inv_scale
3463 } else {
3464 0.0
3465 };
3466 let clip_rect: Rect = if border_inset > 0.0 {
3467 Rect::new(
3468 bounds.get_x() + border_inset,
3469 bounds.get_y() + border_inset,
3470 (bounds.get_width() - border_inset * 2.0).max(0.0),
3471 (bounds.get_height() - border_inset * 2.0).max(0.0),
3472 )
3473 } else {
3474 bounds
3475 };
3476 let max_r: f32 = (bounds.get_width() * 0.5)
3477 .min(bounds.get_height() * 0.5)
3478 .max(0.0);
3479 let raw_r: CornerRadius = paint.get_radius();
3480 let inner_r: CornerRadius = CornerRadius::new(
3481 (raw_r.get_top_left().min(max_r) - border_inset).max(0.0),
3482 (raw_r.get_top_right().min(max_r) - border_inset).max(0.0),
3483 (raw_r.get_bottom_right().min(max_r) - border_inset).max(0.0),
3484 (raw_r.get_bottom_left().min(max_r) - border_inset).max(0.0),
3485 );
3486 if inner_r.max_radius() > 0.25 {
3487 display_list.push(DrawCommand::PushRoundedClip {
3488 rect: clip_rect,
3489 radius: inner_r,
3490 });
3491 } else {
3492 display_list.push(DrawCommand::PushClip(clip_rect));
3493 }
3494 }
3495 if child_count > 0 {
3496 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3497 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
3498 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
3499 let child_offset_phys: Point =
3500 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
3501 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
3502 if !needs_z_sort {
3503 for child_id in tree.children_iter(node_id) {
3504 Self::emit_node(
3505 tree,
3506 child_id,
3507 (child_offset_phys, opacity),
3508 next_cull,
3509 active_hover_target,
3510 scratch,
3511 display_list,
3512 );
3513 }
3514 } else {
3515 let sort_start: usize = scratch.z_sort.len();
3516 for (idx, cid) in tree.children_iter(node_id).enumerate() {
3517 if let Some(char_item) = tree.get_node(cid) {
3518 scratch
3519 .z_sort
3520 .push((char_item.style_ref().layout.get_z_index(), idx, cid));
3521 }
3522 }
3523 let sort_end: usize = scratch.z_sort.len();
3524 scratch.z_sort[sort_start..sort_end].sort_by(
3525 |target_node_id: &(i32, usize, NodeId), element_id: &(i32, usize, NodeId)| {
3526 target_node_id
3527 .0
3528 .cmp(&element_id.0)
3529 .then_with(|| target_node_id.1.cmp(&element_id.1))
3530 },
3531 );
3532 for slot_idx in sort_start..sort_end {
3533 let child_id: NodeId = scratch.z_sort[slot_idx].2;
3534 Self::emit_node(
3535 tree,
3536 child_id,
3537 (child_offset_phys, opacity),
3538 next_cull,
3539 active_hover_target,
3540 scratch,
3541 display_list,
3542 );
3543 }
3544 scratch.z_sort.truncate(sort_start);
3545 }
3546 }
3547 if wants_scrollbar {
3548 let computed_box: &LayoutBox = &node.layout_box;
3549 let border_w: f32 = paint.get_border().get_width();
3550 let inner_x: f32 = bounds.get_x() + border_w;
3551 let inner_y: f32 = bounds.get_y() + border_w;
3552 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
3553 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
3554 let view_w: f32 = computed_box.get_content_bounds().get_width().max(1.0);
3555 let view_h: f32 = computed_box.get_content_bounds().get_height().max(1.0);
3556 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
3557 Some((vy, vh)) => (vy, vh),
3558 None => (
3559 computed_box.get_scroll_offset().get_y(),
3560 computed_box.get_content_size().get_height(),
3561 ),
3562 };
3563 let scroll_x: f32 = computed_box.get_scroll_offset().get_x();
3564 let total_w: f32 = computed_box.get_content_size().get_width();
3565 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
3566 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
3567 let has_v_bar: bool = max_scroll_y > 1.0;
3568 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
3569 let corner_inset: f32 = if has_v_bar && has_h_bar { 6.0 } else { 0.0 };
3570 if has_v_bar {
3571 let track_margin: f32 = 3.0;
3572 let edge_inset: f32 = 1.0;
3573 let thumb_w: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3574 let track_x: f32 = (inner_x + inner_w - thumb_w - edge_inset).max(inner_x);
3575 let track_y: f32 = inner_y + track_margin;
3576 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
3577 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
3578 let min_thumb_h: f32 = 20.0_f32.min(track_h);
3579 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
3580 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
3581 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
3582 let thumb_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3583 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3584 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
3585 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3586 let thumb_alpha: u8 = if pressed {
3587 188
3588 } else if hovered {
3589 148
3590 } else {
3591 88
3592 };
3593 display_list.push(DrawCommand::FillRoundedRect {
3594 rect: Rect::new(
3595 thumb_x_snapped,
3596 thumb_y_snapped,
3597 thumb_w_snapped,
3598 thumb_h_snapped,
3599 ),
3600 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3601 radius: CornerRadius::all(thumb_w * 0.5),
3602 border: Border::default(),
3603 });
3604 }
3605 if has_h_bar {
3606 let track_margin: f32 = 3.0;
3607 let edge_inset: f32 = 1.0;
3608 let thumb_h: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3609 let track_y: f32 = (inner_y + inner_h - thumb_h - edge_inset).max(inner_y);
3610 let track_x: f32 = inner_x + track_margin;
3611 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
3612 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
3613 let min_thumb_w: f32 = 20.0_f32.min(track_w);
3614 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
3615 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
3616 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
3617 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
3618 let thumb_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3619 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3620 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3621 let thumb_alpha: u8 = if pressed {
3622 188
3623 } else if hovered {
3624 148
3625 } else {
3626 88
3627 };
3628 display_list.push(DrawCommand::FillRoundedRect {
3629 rect: Rect::new(
3630 thumb_x_snapped,
3631 thumb_y_snapped,
3632 thumb_w_snapped,
3633 thumb_h_snapped,
3634 ),
3635 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3636 radius: CornerRadius::all(thumb_h * 0.5),
3637 border: Border::default(),
3638 });
3639 }
3640 }
3641 if emit_clip {
3642 if display_list.len() == clip_push_idx + 1 {
3643 display_list.pop_empty_clip(clip_saved_digest);
3644 if self_visible && defer_rounded_border && !has_canvas_clip {
3645 let mut outer_border: Border = paint.get_border();
3646 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3647 display_list.push(DrawCommand::FillRoundedRect {
3648 rect: bounds,
3649 color: COLOR_TRANSPARENT,
3650 radius: paint.get_radius(),
3651 border: outer_border,
3652 });
3653 }
3654 } else {
3655 display_list.push(DrawCommand::PopClip);
3656 if self_visible && has_rounded_border && !has_canvas_clip {
3657 let mut outer_border: Border = paint.get_border();
3658 outer_border.set_color(outer_border.get_color().multiply_alpha(opacity));
3659 display_list.push(DrawCommand::FillRoundedRect {
3660 rect: bounds,
3661 color: COLOR_TRANSPARENT,
3662 radius: paint.get_radius(),
3663 border: outer_border,
3664 });
3665 }
3666 }
3667 }
3668 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
3669 && let Some(cache) = scratch.fragments.as_deref_mut()
3670 {
3671 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
3672 cache.capture_fragment(
3673 slot_idx,
3674 (
3675 node_id,
3676 phys_scale_milli,
3677 parent_opacity.to_bits(),
3678 offset_phys,
3679 geom_hash,
3680 style_text_hash,
3681 ),
3682 cmd_slice,
3683 );
3684 }
3685 }
3686}