1use super::*;
2
3impl Default for DisplayListFrameArena {
5 fn default() -> Self {
11 Self {
12 string_pool: Vec::new(),
13 portals_scratch: Vec::new(),
14 z_sort_scratch: Vec::new(),
15 portal_ancestors_scratch: Vec::new(),
16 base_layer_version: 0,
17 prev_base_digest: 0,
18 cur_base_digest: FNV1A_OFFSET_BASIS,
19 running_fast_hash: FNV1A_OFFSET_BASIS,
20 base_layer_finalized: false,
21 }
22 }
23}
24
25impl Clone for DisplayListFrameArena {
27 fn clone(&self) -> Self {
37 let Self {
38 base_layer_version,
39 prev_base_digest,
40 cur_base_digest,
41 running_fast_hash,
42 base_layer_finalized,
43 ..
44 } = self;
45 Self {
46 string_pool: Vec::new(),
47 portals_scratch: Vec::new(),
48 z_sort_scratch: Vec::new(),
49 portal_ancestors_scratch: Vec::new(),
50 base_layer_version: *base_layer_version,
51 prev_base_digest: *prev_base_digest,
52 cur_base_digest: *cur_base_digest,
53 running_fast_hash: *running_fast_hash,
54 base_layer_finalized: *base_layer_finalized,
55 }
56 }
57}
58
59impl PartialEq for DisplayListFrameArena {
61 fn eq(&self, _other: &Self) -> bool {
72 true
73 }
74}
75
76impl DisplayList {
78 pub(crate) fn reserve_steady_state(&mut self) {
84 let Self {
85 commands, arena, ..
86 } = self;
87 if commands.capacity() < 512 {
88 commands.reserve(512 - commands.len());
89 }
90 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
91 arena
92 .string_pool
93 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
94 }
95 }
96
97 pub(crate) fn prewarm_scroll_string_pool(&mut self, spare_count: usize) {
104 let Self { arena, .. } = self;
105 let target: usize = arena
106 .string_pool
107 .len()
108 .saturating_add(spare_count)
109 .min(DISPLAY_LIST_STRING_POOL_CAP);
110 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
111 arena
112 .string_pool
113 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
114 }
115 while arena.string_pool.len() < target {
116 arena.string_pool.push(String::with_capacity(128));
117 }
118 }
119
120 pub fn reset(&mut self, viewport: Size, scale_factor: f32) {
128 let safe_scale: f32 = scale_factor.max(0.5);
129 let Self {
130 commands,
131 viewport: vp,
132 scale_factor: sf,
133 portal_split_index,
134 arena,
135 } = self;
136 if arena.string_pool.capacity() < DISPLAY_LIST_STRING_POOL_CAP {
137 arena
138 .string_pool
139 .reserve(DISPLAY_LIST_STRING_POOL_CAP - arena.string_pool.len());
140 }
141 let pool_start: usize = arena.string_pool.len();
142 for cmd in commands.drain(..) {
143 match cmd {
144 DrawCommand::TextRun { mut text, .. } | DrawCommand::TextCaret { mut text, .. }
145 if text.capacity() > 0
146 && arena.string_pool.len() < DISPLAY_LIST_STRING_POOL_CAP =>
147 {
148 text.clear();
149 arena.string_pool.push(text);
150 }
151 _ => {}
152 }
153 }
154 if arena.string_pool.len() > pool_start + 1 {
155 arena.string_pool[pool_start..].reverse();
156 }
157 *vp = viewport;
158 *sf = safe_scale;
159 *portal_split_index = 0;
160 let mut seed: u64 = FNV1A_OFFSET_BASIS;
161 Self::mix_hash(&mut seed, viewport.get_width().to_bits() as u64);
162 Self::mix_hash(&mut seed, viewport.get_height().to_bits() as u64);
163 Self::mix_hash(&mut seed, safe_scale.to_bits() as u64);
164 arena.prev_base_digest = arena.cur_base_digest;
165 arena.cur_base_digest = seed;
166 arena.running_fast_hash = seed;
167 arena.base_layer_finalized = false;
168 }
169
170 #[inline]
181 #[must_use]
182 pub(crate) fn alloc_text(&mut self, src: &str) -> String {
183 if src.is_empty() {
184 return String::new();
185 }
186 let Self { arena, .. } = self;
187 let pool: &mut Vec<String> = &mut arena.string_pool;
188 let len: usize = pool.len();
189 if len > 0 {
190 let need: usize = src.len();
191 if pool[len - 1].capacity() >= need
192 && let Some(mut reused) = pool.pop()
193 {
194 reused.clear();
195 reused.push_str(src);
196 return reused;
197 }
198 for idx in (0..len.saturating_sub(1)).rev() {
199 if pool[idx].capacity() >= need {
200 let mut reused: String = pool.swap_remove(idx);
201 reused.clear();
202 reused.push_str(src);
203 return reused;
204 }
205 }
206 if let Some(mut reused) = pool.pop() {
207 reused.clear();
208 reused.push_str(src);
209 return reused;
210 }
211 }
212 let mut fresh: String = String::with_capacity(src.len().max(128));
213 fresh.push_str(src);
214 fresh
215 }
216
217 pub fn push(&mut self, command: DrawCommand) {
224 let Self {
225 commands, arena, ..
226 } = self;
227 Self::fold_command_fast(&mut arena.running_fast_hash, &command);
228 if !arena.base_layer_finalized {
229 Self::fold_command_full(&mut arena.cur_base_digest, &command);
230 }
231 commands.push(command);
232 }
233
234 #[inline]
244 #[must_use]
245 pub(crate) fn snapshot_digest_state(&self) -> (u64, u64) {
246 let Self { arena, .. } = self;
247 (arena.running_fast_hash, arena.cur_base_digest)
248 }
249
250 #[inline]
257 pub(crate) fn pop_empty_clip(&mut self, saved: (u64, u64)) {
258 let Self {
259 commands, arena, ..
260 } = self;
261 let _: Option<DrawCommand> = commands.pop();
262 arena.running_fast_hash = saved.0;
263 arena.cur_base_digest = saved.1;
264 }
265
266 pub(crate) fn attenuate_range_opacity(&mut self, start: usize, end: usize, opacity: f32) {
275 if opacity >= 0.999 || start >= end {
276 return;
277 }
278 let clamped: f32 = opacity.clamp(0.0, 1.0);
279 let Self {
280 commands, arena, ..
281 } = self;
282 let end_clamped: usize = end.min(commands.len());
283 for cmd in commands[start..end_clamped].iter_mut() {
284 match cmd {
285 DrawCommand::Clear(c)
286 | DrawCommand::Line { color: c, .. }
287 | DrawCommand::Circle { color: c, .. }
288 | DrawCommand::Triangle { color: c, .. }
289 | DrawCommand::TextCaret { color: c, .. } => {
290 *c = c.multiply_alpha(clamped);
291 }
292 DrawCommand::FillRoundedRect { color, border, .. } => {
293 *color = color.multiply_alpha(clamped);
294 border.set_color(border.get_color().multiply_alpha(clamped));
295 }
296 DrawCommand::LinearGradientRect { gradient, .. } => {
297 gradient.set_start_color(gradient.get_start_color().multiply_alpha(clamped));
298 gradient.set_end_color(gradient.get_end_color().multiply_alpha(clamped));
299 }
300 DrawCommand::BoxShadow { shadow, .. } => {
301 shadow.set_color(shadow.get_color().multiply_alpha(clamped));
302 }
303 DrawCommand::TextRun { style, .. } => {
304 style.set_color(style.get_color().multiply_alpha(clamped));
305 }
306 DrawCommand::SpriteRegion { tint, .. } => {
307 *tint = tint.multiply_alpha(clamped);
308 }
309 DrawCommand::BackdropBlur { .. }
310 | DrawCommand::Image { .. }
311 | DrawCommand::SharedImage { .. }
312 | DrawCommand::PushClip(_)
313 | DrawCommand::PushRoundedClip { .. }
314 | DrawCommand::PopClip => {}
315 }
316 }
317 let op_bits: u64 = clamped.to_bits() as u64;
318 Self::mix_hash(&mut arena.running_fast_hash, op_bits);
319 if !arena.base_layer_finalized {
320 Self::mix_hash(&mut arena.cur_base_digest, op_bits);
321 }
322 }
323
324 #[inline]
331 pub(crate) fn finalize_base_layer(&mut self, split_index: usize) {
332 let Self {
333 portal_split_index,
334 arena,
335 ..
336 } = self;
337 *portal_split_index = split_index;
338 Self::mix_hash(&mut arena.cur_base_digest, split_index as u64);
339 if arena.base_layer_version == 0 || arena.cur_base_digest != arena.prev_base_digest {
340 arena.base_layer_version = arena.base_layer_version.wrapping_add(1).max(1);
341 }
342 arena.base_layer_finalized = true;
343 }
344
345 #[inline]
355 #[must_use]
356 pub fn base_layer_version(&self) -> u64 {
357 let Self { arena, .. } = self;
358 arena.base_layer_version
359 }
360
361 #[must_use]
371 pub fn len(&self) -> usize {
372 self.get_commands().len()
373 }
374
375 #[must_use]
385 pub fn is_empty(&self) -> bool {
386 self.get_commands().is_empty()
387 }
388
389 #[inline(always)]
396 fn mix_hash(hash: &mut u64, value: u64) {
397 *hash ^= value;
398 *hash = hash.wrapping_mul(FNV1A_PRIME);
399 }
400
401 #[inline]
408 fn fold_command_fast(hash: &mut u64, cmd: &DrawCommand) {
409 match cmd {
410 DrawCommand::Clear(c) => {
411 Self::mix_hash(hash, 1);
412 Self::mix_hash(hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
413 }
414 DrawCommand::FillRoundedRect {
415 rect,
416 color,
417 border,
418 ..
419 } => {
420 Self::mix_hash(hash, 2);
421 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
422 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
423 Self::mix_hash(hash, rect.get_width().to_bits() as u64);
424 Self::mix_hash(hash, rect.get_height().to_bits() as u64);
425 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
426 Self::mix_hash(hash, border.get_width().to_bits() as u64);
427 }
428 DrawCommand::LinearGradientRect { rect, gradient, .. } => {
429 Self::mix_hash(hash, 3);
430 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
431 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
432 Self::mix_hash(
433 hash,
434 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
435 );
436 }
437 DrawCommand::BoxShadow { rect, shadow, .. } => {
438 Self::mix_hash(hash, 4);
439 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
440 Self::mix_hash(hash, shadow.get_blur_radius().to_bits() as u64);
441 }
442 DrawCommand::BackdropBlur {
443 rect, blur_radius, ..
444 } => {
445 Self::mix_hash(hash, 15);
446 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
447 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
448 Self::mix_hash(hash, blur_radius.to_bits() as u64);
449 }
450 DrawCommand::TextRun { rect, text, style } => {
451 Self::mix_hash(hash, 5);
452 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
453 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
454 Self::mix_hash(hash, text.len() as u64);
455 let bytes: &[u8] = text.as_bytes();
456 let prefix_len: usize = bytes.len().min(16);
457 for &byte in bytes[..prefix_len].iter() {
458 Self::mix_hash(hash, byte as u64);
459 }
460 Self::mix_hash(
461 hash,
462 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
463 );
464 }
465 DrawCommand::TextCaret {
466 text_rect,
467 text,
468 color,
469 ..
470 } => {
471 Self::mix_hash(hash, 13);
472 Self::mix_hash(hash, text_rect.get_x().to_bits() as u64);
473 Self::mix_hash(hash, text_rect.get_y().to_bits() as u64);
474 Self::mix_hash(hash, text.len() as u64);
475 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
476 }
477 DrawCommand::Image { rect, buffer, .. } => {
478 Self::mix_hash(hash, 6);
479 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
480 Self::mix_hash(hash, buffer.get_width() as u64);
481 Self::mix_hash(hash, buffer.get_height() as u64);
482 }
483 DrawCommand::SharedImage {
484 rect,
485 width,
486 height,
487 ..
488 } => {
489 Self::mix_hash(hash, 6);
490 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
491 Self::mix_hash(hash, *width as u64);
492 Self::mix_hash(hash, *height as u64);
493 }
494 DrawCommand::Line {
495 from,
496 to,
497 color,
498 width,
499 } => {
500 Self::mix_hash(hash, 7);
501 Self::mix_hash(hash, from.get_x().to_bits() as u64);
502 Self::mix_hash(hash, to.get_y().to_bits() as u64);
503 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
504 Self::mix_hash(hash, width.to_bits() as u64);
505 }
506 DrawCommand::Circle {
507 center,
508 radius,
509 color,
510 } => {
511 Self::mix_hash(hash, 8);
512 Self::mix_hash(hash, center.get_x().to_bits() as u64);
513 Self::mix_hash(hash, center.get_y().to_bits() as u64);
514 Self::mix_hash(hash, radius.to_bits() as u64);
515 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
516 }
517 DrawCommand::Triangle { p0, p1, p2, color } => {
518 Self::mix_hash(hash, 9);
519 Self::mix_hash(hash, p0.get_x().to_bits() as u64);
520 Self::mix_hash(hash, p1.get_y().to_bits() as u64);
521 Self::mix_hash(hash, p2.get_x().to_bits() as u64);
522 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
523 }
524 DrawCommand::SpriteRegion {
525 rect,
526 src_rect,
527 tint,
528 ..
529 } => {
530 Self::mix_hash(hash, 10);
531 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
532 Self::mix_hash(hash, src_rect.get_x().to_bits() as u64);
533 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
534 }
535 DrawCommand::PushClip(r) => {
536 Self::mix_hash(hash, 11);
537 Self::mix_hash(hash, r.get_x().to_bits() as u64);
538 Self::mix_hash(hash, r.get_y().to_bits() as u64);
539 }
540 DrawCommand::PushRoundedClip { rect, radius } => {
541 Self::mix_hash(hash, 14);
542 Self::mix_hash(hash, rect.get_x().to_bits() as u64);
543 Self::mix_hash(hash, rect.get_y().to_bits() as u64);
544 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
545 }
546 DrawCommand::PopClip => {
547 Self::mix_hash(hash, 12);
548 }
549 }
550 }
551
552 #[inline]
559 fn fold_bytes_wordwise(hash: &mut u64, bytes: &[u8]) {
560 Self::mix_hash(hash, bytes.len() as u64);
561 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
562 for chunk in chunks.iter() {
563 Self::mix_hash(hash, u64::from_ne_bytes(*chunk));
564 }
565 if !rem.is_empty() {
566 let mut tail: u64 = 0;
567 for (idx, &b) in rem.iter().enumerate() {
568 tail |= (b as u64) << (idx * 8);
569 }
570 Self::mix_hash(hash, tail);
571 }
572 }
573
574 #[inline]
581 pub(crate) fn fold_command_full(hash: &mut u64, cmd: &DrawCommand) {
582 match cmd {
583 DrawCommand::Clear(c) => {
584 Self::mix_hash(hash, 1);
585 Self::mix_hash(hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
586 }
587 DrawCommand::FillRoundedRect {
588 rect,
589 color,
590 radius,
591 border,
592 } => {
593 Self::mix_hash(hash, 2);
594 Self::mix_hash(
595 hash,
596 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
597 );
598 Self::mix_hash(
599 hash,
600 ((rect.get_width().to_bits() as u64) << 32)
601 | (rect.get_height().to_bits() as u64),
602 );
603 Self::mix_hash(
604 hash,
605 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
606 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64),
607 );
608 Self::mix_hash(
609 hash,
610 ((radius.get_top_left().to_bits() as u64) << 32)
611 | (radius.get_top_right().to_bits() as u64),
612 );
613 Self::mix_hash(
614 hash,
615 ((radius.get_bottom_right().to_bits() as u64) << 32)
616 | (radius.get_bottom_left().to_bits() as u64),
617 );
618 Self::mix_hash(hash, border.get_width().to_bits() as u64);
619 }
620 DrawCommand::LinearGradientRect {
621 rect,
622 gradient,
623 radius,
624 } => {
625 Self::mix_hash(hash, 3);
626 Self::mix_hash(
627 hash,
628 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
629 );
630 Self::mix_hash(
631 hash,
632 ((rect.get_width().to_bits() as u64) << 32)
633 | (rect.get_height().to_bits() as u64),
634 );
635 Self::mix_hash(
636 hash,
637 ((u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64) << 32)
638 | (u32::from_le_bytes(gradient.get_end_color().to_rgba_bytes()) as u64),
639 );
640 Self::mix_hash(
641 hash,
642 ((gradient.get_angle_deg().to_bits() as u64) << 32)
643 | (radius.max_radius().to_bits() as u64),
644 );
645 }
646 DrawCommand::BoxShadow {
647 rect,
648 radius,
649 shadow,
650 } => {
651 Self::mix_hash(hash, 4);
652 Self::mix_hash(
653 hash,
654 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
655 );
656 Self::mix_hash(
657 hash,
658 ((rect.get_width().to_bits() as u64) << 32)
659 | (rect.get_height().to_bits() as u64),
660 );
661 Self::mix_hash(
662 hash,
663 ((shadow.get_offset_x().to_bits() as u64) << 32)
664 | (shadow.get_offset_y().to_bits() as u64),
665 );
666 Self::mix_hash(
667 hash,
668 ((shadow.get_blur_radius().to_bits() as u64) << 32)
669 | (shadow.get_spread_radius().to_bits() as u64),
670 );
671 Self::mix_hash(
672 hash,
673 ((radius.max_radius().to_bits() as u64) << 32)
674 | (u32::from_le_bytes(shadow.get_color().to_rgba_bytes()) as u64),
675 );
676 }
677 DrawCommand::BackdropBlur {
678 rect,
679 radius,
680 blur_radius,
681 } => {
682 Self::mix_hash(hash, 15);
683 Self::mix_hash(
684 hash,
685 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
686 );
687 Self::mix_hash(
688 hash,
689 ((rect.get_width().to_bits() as u64) << 32)
690 | (rect.get_height().to_bits() as u64),
691 );
692 Self::mix_hash(
693 hash,
694 ((radius.max_radius().to_bits() as u64) << 32) | (blur_radius.to_bits() as u64),
695 );
696 }
697 DrawCommand::TextRun { rect, text, style } => {
698 Self::mix_hash(hash, 5);
699 Self::mix_hash(
700 hash,
701 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
702 );
703 Self::mix_hash(
704 hash,
705 ((rect.get_width().to_bits() as u64) << 32)
706 | (rect.get_height().to_bits() as u64),
707 );
708 let style_flags: u64 = (style.get_family() as u64)
709 | ((style.get_weight() as u64) << 8)
710 | ((style.get_align() as u64) << 16)
711 | (u64::from(style.get_italic()) << 24)
712 | ((u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64) << 32);
713 Self::mix_hash(hash, style_flags);
714 Self::mix_hash(hash, style.get_font_size().to_bits() as u64);
715 Self::fold_bytes_wordwise(hash, text.as_bytes());
716 }
717 DrawCommand::TextCaret {
718 text_rect,
719 content_rect,
720 text,
721 style,
722 caret_height,
723 color,
724 multiline,
725 } => {
726 Self::mix_hash(hash, 13);
727 Self::mix_hash(
728 hash,
729 ((text_rect.get_x().to_bits() as u64) << 32)
730 | (text_rect.get_y().to_bits() as u64),
731 );
732 Self::mix_hash(
733 hash,
734 ((content_rect.get_width().to_bits() as u64) << 32)
735 | (caret_height.to_bits() as u64),
736 );
737 Self::mix_hash(
738 hash,
739 ((style.get_font_size().to_bits() as u64) << 32)
740 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64)
741 ^ u64::from(*multiline),
742 );
743 Self::fold_bytes_wordwise(hash, text.as_bytes());
744 }
745 DrawCommand::Image {
746 rect,
747 buffer,
748 radius,
749 } => {
750 Self::mix_hash(hash, 6);
751 Self::mix_hash(
752 hash,
753 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
754 );
755 Self::mix_hash(
756 hash,
757 ((rect.get_width().to_bits() as u64) << 32)
758 | (rect.get_height().to_bits() as u64),
759 );
760 Self::mix_hash(
761 hash,
762 ((buffer.get_width() as u64) << 32) | (buffer.get_height() as u64),
763 );
764 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
765 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
766 }
767 DrawCommand::SharedImage {
768 rect,
769 width,
770 height,
771 pixels,
772 radius,
773 } => {
774 Self::mix_hash(hash, 6);
775 Self::mix_hash(
776 hash,
777 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
778 );
779 Self::mix_hash(
780 hash,
781 ((rect.get_width().to_bits() as u64) << 32)
782 | (rect.get_height().to_bits() as u64),
783 );
784 Self::mix_hash(hash, ((*width as u64) << 32) | (*height as u64));
785 Self::mix_hash(hash, pixels.as_ptr() as usize as u64);
786 Self::mix_hash(hash, radius.max_radius().to_bits() as u64);
787 }
788 DrawCommand::Line {
789 from,
790 to,
791 color,
792 width,
793 } => {
794 Self::mix_hash(hash, 7);
795 Self::mix_hash(
796 hash,
797 ((from.get_x().to_bits() as u64) << 32) | (from.get_y().to_bits() as u64),
798 );
799 Self::mix_hash(
800 hash,
801 ((to.get_x().to_bits() as u64) << 32) | (to.get_y().to_bits() as u64),
802 );
803 Self::mix_hash(
804 hash,
805 ((u32::from_le_bytes(color.to_rgba_bytes()) as u64) << 32)
806 | (width.to_bits() as u64),
807 );
808 }
809 DrawCommand::Circle {
810 center,
811 radius,
812 color,
813 } => {
814 Self::mix_hash(hash, 8);
815 Self::mix_hash(
816 hash,
817 ((center.get_x().to_bits() as u64) << 32) | (center.get_y().to_bits() as u64),
818 );
819 Self::mix_hash(
820 hash,
821 ((radius.to_bits() as u64) << 32)
822 | (u32::from_le_bytes(color.to_rgba_bytes()) as u64),
823 );
824 }
825 DrawCommand::Triangle { p0, p1, p2, color } => {
826 Self::mix_hash(hash, 9);
827 Self::mix_hash(
828 hash,
829 ((p0.get_x().to_bits() as u64) << 32) | (p0.get_y().to_bits() as u64),
830 );
831 Self::mix_hash(
832 hash,
833 ((p1.get_x().to_bits() as u64) << 32) | (p1.get_y().to_bits() as u64),
834 );
835 Self::mix_hash(
836 hash,
837 ((p2.get_x().to_bits() as u64) << 32) | (p2.get_y().to_bits() as u64),
838 );
839 Self::mix_hash(hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
840 }
841 DrawCommand::SpriteRegion {
842 rect,
843 buffer,
844 src_rect,
845 tint,
846 } => {
847 Self::mix_hash(hash, 10);
848 Self::mix_hash(
849 hash,
850 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
851 );
852 Self::mix_hash(
853 hash,
854 ((rect.get_width().to_bits() as u64) << 32)
855 | (rect.get_height().to_bits() as u64),
856 );
857 Self::mix_hash(hash, buffer.get_pixels().as_ptr() as usize as u64);
858 Self::mix_hash(
859 hash,
860 ((src_rect.get_x().to_bits() as u64) << 32)
861 | (src_rect.get_y().to_bits() as u64),
862 );
863 Self::mix_hash(hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
864 }
865 DrawCommand::PushClip(r) => {
866 Self::mix_hash(hash, 11);
867 Self::mix_hash(
868 hash,
869 ((r.get_x().to_bits() as u64) << 32) | (r.get_y().to_bits() as u64),
870 );
871 Self::mix_hash(
872 hash,
873 ((r.get_width().to_bits() as u64) << 32) | (r.get_height().to_bits() as u64),
874 );
875 }
876 DrawCommand::PushRoundedClip { rect, radius } => {
877 Self::mix_hash(hash, 14);
878 Self::mix_hash(
879 hash,
880 ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64),
881 );
882 Self::mix_hash(
883 hash,
884 ((rect.get_width().to_bits() as u64) << 32)
885 | (rect.get_height().to_bits() as u64),
886 );
887 Self::mix_hash(
888 hash,
889 ((radius.get_top_left().to_bits() as u64) << 32)
890 | (radius.get_top_right().to_bits() as u64),
891 );
892 Self::mix_hash(
893 hash,
894 ((radius.get_bottom_right().to_bits() as u64) << 32)
895 | (radius.get_bottom_left().to_bits() as u64),
896 );
897 }
898 DrawCommand::PopClip => {
899 Self::mix_hash(hash, 12);
900 }
901 }
902 }
903
904 #[inline]
914 #[must_use]
915 pub fn fast_hash(&self) -> u64 {
916 let Self {
917 commands, arena, ..
918 } = self;
919 let mut hash: u64 = arena.running_fast_hash;
920 Self::mix_hash(&mut hash, commands.len() as u64);
921 hash
922 }
923
924 #[inline]
934 #[must_use]
935 pub fn base_layer_hash(&self) -> u64 {
936 let Self {
937 commands, arena, ..
938 } = self;
939 if arena.base_layer_finalized {
940 return arena.base_layer_version.max(1);
941 }
942 let mut hash: u64 = arena.cur_base_digest;
943 Self::mix_hash(&mut hash, commands.len() as u64);
944 if hash == 0 { 1 } else { hash }
945 }
946}
947
948impl PaintFragmentCache {
950 pub fn clear(&mut self) {
956 let Self {
957 has_last_scene,
958 last_has_canvas,
959 slots,
960 cmd_slab,
961 text_slab,
962 canvas_stability,
963 ..
964 } = self;
965 *has_last_scene = false;
966 *last_has_canvas = false;
967 slots.fill(RetainedPaintFragment::default());
968 cmd_slab.clear();
969 text_slab.clear();
970 for entry in canvas_stability.iter_mut() {
971 *entry = CanvasStabilityEntry::default();
972 }
973 }
974
975 #[must_use]
985 pub fn stats(&self) -> (u64, u64, u64, u64) {
986 (
987 self.get_whole_list_hits(),
988 self.get_fragment_hits(),
989 self.get_fragment_misses(),
990 self.get_fragment_evictions(),
991 )
992 }
993
994 #[inline]
1000 fn ensure_slots(&mut self) {
1001 let Self {
1002 slots,
1003 cmd_slab,
1004 text_slab,
1005 ..
1006 } = self;
1007 if slots.len() != PAINT_FRAGMENT_CACHE_SLOTS {
1008 slots.resize(PAINT_FRAGMENT_CACHE_SLOTS, RetainedPaintFragment::default());
1009 }
1010 if cmd_slab.capacity() < PAINT_FRAGMENT_CMD_SLAB_CAP {
1011 cmd_slab.reserve_exact(PAINT_FRAGMENT_CMD_SLAB_CAP - cmd_slab.len());
1012 }
1013 if text_slab.capacity() < PAINT_FRAGMENT_TEXT_SLAB_CAP {
1014 text_slab.reserve_exact(PAINT_FRAGMENT_TEXT_SLAB_CAP - text_slab.len());
1015 }
1016 }
1017
1018 #[inline]
1031 fn encode_cached_command(
1032 cmd: &DrawCommand,
1033 text_dst: &mut [u8],
1034 text_cursor: &mut usize,
1035 text_base: usize,
1036 ) -> CachedDrawCommand {
1037 match cmd {
1038 DrawCommand::Clear(c) => CachedDrawCommand::Clear(*c),
1039 DrawCommand::FillRoundedRect {
1040 rect,
1041 color,
1042 radius,
1043 border,
1044 } => CachedDrawCommand::FillRoundedRect {
1045 rect: *rect,
1046 color: *color,
1047 radius: *radius,
1048 border: *border,
1049 },
1050 DrawCommand::LinearGradientRect {
1051 rect,
1052 gradient,
1053 radius,
1054 } => CachedDrawCommand::LinearGradientRect {
1055 rect: *rect,
1056 gradient: *gradient,
1057 radius: *radius,
1058 },
1059 DrawCommand::BoxShadow {
1060 rect,
1061 radius,
1062 shadow,
1063 } => CachedDrawCommand::BoxShadow {
1064 rect: *rect,
1065 radius: *radius,
1066 shadow: *shadow,
1067 },
1068 DrawCommand::BackdropBlur {
1069 rect,
1070 radius,
1071 blur_radius,
1072 } => CachedDrawCommand::BackdropBlur {
1073 rect: *rect,
1074 radius: *radius,
1075 blur_radius: *blur_radius,
1076 },
1077 DrawCommand::TextRun { rect, text, style } => {
1078 let bytes: &[u8] = text.as_bytes();
1079 let start_rel: usize = *text_cursor;
1080 let end_rel: usize = start_rel.saturating_add(bytes.len());
1081 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1082 *text_cursor = end_rel;
1083 CachedDrawCommand::TextRun {
1084 rect: *rect,
1085 text_start: (text_base.saturating_add(start_rel)) as u32,
1086 text_len: bytes.len() as u32,
1087 style: *style,
1088 }
1089 }
1090 DrawCommand::TextCaret {
1091 text_rect,
1092 content_rect,
1093 text,
1094 style,
1095 caret_height,
1096 color,
1097 multiline,
1098 } => {
1099 let bytes: &[u8] = text.as_bytes();
1100 let start_rel: usize = *text_cursor;
1101 let end_rel: usize = start_rel.saturating_add(bytes.len());
1102 text_dst[start_rel..end_rel].copy_from_slice(bytes);
1103 *text_cursor = end_rel;
1104 CachedDrawCommand::TextCaret {
1105 text_rect: *text_rect,
1106 content_rect: *content_rect,
1107 text_start: (text_base.saturating_add(start_rel)) as u32,
1108 text_len: bytes.len() as u32,
1109 style: *style,
1110 caret_height: *caret_height,
1111 color: *color,
1112 multiline: *multiline,
1113 }
1114 }
1115 DrawCommand::Image {
1116 rect,
1117 buffer,
1118 radius,
1119 } => CachedDrawCommand::Image {
1120 rect: *rect,
1121 buffer: buffer.clone(),
1122 radius: *radius,
1123 },
1124 DrawCommand::SharedImage {
1125 rect,
1126 width,
1127 height,
1128 pixels,
1129 radius,
1130 } => CachedDrawCommand::SharedImage {
1131 rect: *rect,
1132 width: *width,
1133 height: *height,
1134 pixels: Arc::clone(pixels),
1135 radius: *radius,
1136 },
1137 DrawCommand::Line {
1138 from,
1139 to,
1140 color,
1141 width,
1142 } => CachedDrawCommand::Line {
1143 from: *from,
1144 to: *to,
1145 color: *color,
1146 width: *width,
1147 },
1148 DrawCommand::Circle {
1149 center,
1150 radius,
1151 color,
1152 } => CachedDrawCommand::Circle {
1153 center: *center,
1154 radius: *radius,
1155 color: *color,
1156 },
1157 DrawCommand::Triangle { p0, p1, p2, color } => CachedDrawCommand::Triangle {
1158 p0: *p0,
1159 p1: *p1,
1160 p2: *p2,
1161 color: *color,
1162 },
1163 DrawCommand::SpriteRegion {
1164 rect,
1165 buffer,
1166 src_rect,
1167 tint,
1168 } => CachedDrawCommand::SpriteRegion {
1169 rect: *rect,
1170 buffer: buffer.clone(),
1171 src_rect: *src_rect,
1172 tint: *tint,
1173 },
1174 DrawCommand::PushClip(rect) => CachedDrawCommand::PushClip(*rect),
1175 DrawCommand::PushRoundedClip { rect, radius } => CachedDrawCommand::PushRoundedClip {
1176 rect: *rect,
1177 radius: *radius,
1178 },
1179 DrawCommand::PopClip => CachedDrawCommand::PopClip,
1180 }
1181 }
1182
1183 fn capture_fragment(
1192 &mut self,
1193 slot_idx: usize,
1194 meta: (NodeId, u32, u32, Point, u64, u64),
1195 cmd_slice: &[DrawCommand],
1196 ) {
1197 let (node_id, phys_scale_milli, opacity_bits, offset_phys, geom_hash, style_text_hash): (
1198 NodeId,
1199 u32,
1200 u32,
1201 Point,
1202 u64,
1203 u64,
1204 ) = meta;
1205 let Self {
1206 slots,
1207 cmd_slab,
1208 text_slab,
1209 fragment_misses,
1210 fragment_evictions,
1211 ..
1212 } = self;
1213 if slot_idx >= slots.len() {
1214 return;
1215 }
1216 *fragment_misses = fragment_misses.wrapping_add(1);
1217 let need_cmds: usize = cmd_slice.len();
1218 let mut need_text: usize = 0;
1219 for cmd in cmd_slice.iter() {
1220 match cmd {
1221 DrawCommand::TextRun { text, .. } | DrawCommand::TextCaret { text, .. } => {
1222 need_text = need_text.saturating_add(text.len());
1223 }
1224 _ => {}
1225 }
1226 }
1227 if need_cmds > PAINT_FRAGMENT_CMD_SLAB_CAP || need_text > PAINT_FRAGMENT_TEXT_SLAB_CAP {
1228 return;
1229 }
1230 let existing: RetainedPaintFragment = slots[slot_idx];
1231 if existing.occupied && existing.node_id != node_id {
1232 *fragment_evictions = fragment_evictions.wrapping_add(1);
1233 }
1234 if existing.occupied
1235 && need_cmds <= (existing.cmd_cap as usize)
1236 && need_text <= (existing.text_cap as usize)
1237 {
1238 let cmd_base: usize = existing.cmd_start as usize;
1239 let text_base: usize = existing.text_start as usize;
1240 let text_end: usize = text_base.saturating_add(need_text);
1241 let mut text_cursor: usize = 0;
1242 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1243 for (i, cmd) in cmd_slice.iter().enumerate() {
1244 cmd_slab[cmd_base + i] =
1245 Self::encode_cached_command(cmd, text_dst, &mut text_cursor, text_base);
1246 }
1247 slots[slot_idx] = RetainedPaintFragment {
1248 occupied: true,
1249 node_id,
1250 phys_scale_milli,
1251 opacity_bits,
1252 offset_phys,
1253 geom_hash,
1254 style_text_hash,
1255 cmd_start: existing.cmd_start,
1256 cmd_len: need_cmds as u32,
1257 cmd_cap: existing.cmd_cap,
1258 text_start: existing.text_start,
1259 text_cap: existing.text_cap,
1260 };
1261 return;
1262 }
1263 if cmd_slab.len().saturating_add(need_cmds) > PAINT_FRAGMENT_CMD_SLAB_CAP
1264 || text_slab.len().saturating_add(need_text) > PAINT_FRAGMENT_TEXT_SLAB_CAP
1265 {
1266 let mut evicted: u64 = 0;
1267 for s in slots.iter_mut() {
1268 if s.occupied {
1269 evicted = evicted.saturating_add(1);
1270 *s = RetainedPaintFragment::default();
1271 }
1272 }
1273 *fragment_evictions = fragment_evictions.wrapping_add(evicted);
1274 cmd_slab.clear();
1275 text_slab.clear();
1276 }
1277 let cmd_start: usize = cmd_slab.len();
1278 let text_base: usize = text_slab.len();
1279 let text_end: usize = text_base.saturating_add(need_text);
1280 text_slab.resize(text_end, 0);
1281 let mut text_cursor: usize = 0;
1282 let text_dst: &mut [u8] = &mut text_slab[text_base..text_end];
1283 for cmd in cmd_slice.iter() {
1284 cmd_slab.push(Self::encode_cached_command(
1285 cmd,
1286 text_dst,
1287 &mut text_cursor,
1288 text_base,
1289 ));
1290 }
1291 slots[slot_idx] = RetainedPaintFragment {
1292 occupied: true,
1293 node_id,
1294 phys_scale_milli,
1295 opacity_bits,
1296 offset_phys,
1297 geom_hash,
1298 style_text_hash,
1299 cmd_start: cmd_start as u32,
1300 cmd_len: need_cmds as u32,
1301 cmd_cap: need_cmds as u32,
1302 text_start: text_base as u32,
1303 text_cap: need_text as u32,
1304 };
1305 }
1306
1307 #[inline]
1322 fn record_canvas_output(
1323 &mut self,
1324 node_id: NodeId,
1325 phys_scale_milli: u32,
1326 bounds: Rect,
1327 tree_revision: u64,
1328 cmds: &[DrawCommand],
1329 ) -> bool {
1330 let slot_idx: usize = (node_id.get_index() as usize) & (CANVAS_STABILITY_SLOTS - 1);
1331 let Self {
1332 canvas_stability, ..
1333 } = self;
1334 let slot: &mut CanvasStabilityEntry = &mut canvas_stability[slot_idx];
1335 let mut has_raster: bool = false;
1336 let mut cmd_hash: u64 = FNV1A_OFFSET_BASIS;
1337 for cmd in cmds.iter() {
1338 if matches!(
1339 cmd,
1340 DrawCommand::Image { .. }
1341 | DrawCommand::SharedImage { .. }
1342 | DrawCommand::SpriteRegion { .. }
1343 ) {
1344 has_raster = true;
1345 break;
1346 }
1347 DisplayList::fold_command_full(&mut cmd_hash, cmd);
1348 }
1349 if has_raster {
1350 *slot = CanvasStabilityEntry {
1351 node_id,
1352 phys_scale_milli,
1353 bounds,
1354 tree_revision,
1355 cmd_hash: 0,
1356 stable_hits: 0,
1357 proven_dynamic: true,
1358 occupied: true,
1359 };
1360 return true;
1361 }
1362 if slot.occupied
1363 && slot.node_id == node_id
1364 && slot.phys_scale_milli == phys_scale_milli
1365 && slot.bounds == bounds
1366 {
1367 if slot.proven_dynamic {
1368 return true;
1369 }
1370 if slot.cmd_hash == cmd_hash {
1371 slot.tree_revision = tree_revision;
1372 slot.stable_hits = slot.stable_hits.saturating_add(1);
1373 return slot.stable_hits < 2;
1374 }
1375 if slot.tree_revision == tree_revision {
1376 slot.proven_dynamic = true;
1377 return true;
1378 }
1379 slot.tree_revision = tree_revision;
1380 slot.cmd_hash = cmd_hash;
1381 slot.stable_hits = 1;
1382 return true;
1383 }
1384 *slot = CanvasStabilityEntry {
1385 node_id,
1386 phys_scale_milli,
1387 bounds,
1388 tree_revision,
1389 cmd_hash,
1390 stable_hits: 1,
1391 proven_dynamic: false,
1392 occupied: true,
1393 };
1394 true
1395 }
1396}
1397
1398impl PaintPass {
1400 #[inline]
1410 #[must_use]
1411 fn text_selection_sig(tree: &SceneTree) -> u64 {
1412 let Some(sel) = tree.text_selection() else {
1413 return 0;
1414 };
1415 if sel.is_empty() {
1416 return 0;
1417 }
1418 let anchor_id: NodeId = sel.get_anchor_node();
1419 let focus_id: NodeId = sel.get_focus_node();
1420 let mut h: u64 =
1421 ((anchor_id.get_index() as u64) << 32) | (anchor_id.get_generation() as u64);
1422 h = (h ^ (sel.get_anchor_char() as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1423 let focus_bits: u64 =
1424 ((focus_id.get_index() as u64) << 32) | (focus_id.get_generation() as u64);
1425 h = (h ^ focus_bits).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1426 h = (h ^ (sel.get_focus_char() as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1427 if h == 0 { 1 } else { h }
1428 }
1429
1430 #[inline(always)]
1437 fn fx_fold(hash: &mut u64, word: u64) {
1438 *hash = (*hash ^ word).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
1439 }
1440
1441 #[inline(always)]
1448 fn fx_fold_rect(hash: &mut u64, rect: Rect) {
1449 let lo: u64 = ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64);
1450 let hi: u64 =
1451 ((rect.get_width().to_bits() as u64) << 32) | (rect.get_height().to_bits() as u64);
1452 Self::fx_fold(hash, lo);
1453 Self::fx_fold(hash, hi);
1454 }
1455
1456 #[inline]
1463 fn fx_fold_paint(hash: &mut u64, paint: &PaintStyle) {
1464 let bg_bits: u64 = paint
1465 .background_color()
1466 .map(|c: Color| u32::from_le_bytes(c.to_rgba_bytes()) as u64 | (1_u64 << 32))
1467 .unwrap_or(0);
1468 Self::fx_fold(hash, bg_bits);
1469 if let Some(grad) = paint.background_gradient() {
1470 let g0: u64 = u32::from_le_bytes(grad.get_start_color().to_rgba_bytes()) as u64;
1471 let g1: u64 = u32::from_le_bytes(grad.get_end_color().to_rgba_bytes()) as u64;
1472 Self::fx_fold(hash, (g0 << 32) | g1);
1473 Self::fx_fold(hash, grad.get_angle_deg().to_bits() as u64 | (1_u64 << 33));
1474 }
1475 let border: Border = paint.get_border();
1476 let b_word: u64 = ((border.get_width().to_bits() as u64) << 32)
1477 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64);
1478 Self::fx_fold(hash, b_word);
1479 let r: CornerRadius = paint.get_radius();
1480 let r0: u64 =
1481 ((r.get_top_left().to_bits() as u64) << 32) | (r.get_top_right().to_bits() as u64);
1482 let r1: u64 = ((r.get_bottom_right().to_bits() as u64) << 32)
1483 | (r.get_bottom_left().to_bits() as u64);
1484 Self::fx_fold(hash, r0);
1485 Self::fx_fold(hash, r1);
1486 Self::fx_fold(hash, paint.get_opacity().to_bits() as u64);
1487 if let Some(sh) = paint.box_shadow() {
1488 let s0: u64 =
1489 ((sh.get_offset_x().to_bits() as u64) << 32) | (sh.get_offset_y().to_bits() as u64);
1490 let s1: u64 = ((sh.get_blur_radius().to_bits() as u64) << 32)
1491 | (sh.get_spread_radius().to_bits() as u64);
1492 Self::fx_fold(hash, s0);
1493 Self::fx_fold(hash, s1);
1494 Self::fx_fold(
1495 hash,
1496 u32::from_le_bytes(sh.get_color().to_rgba_bytes()) as u64 | (1_u64 << 34),
1497 );
1498 }
1499 }
1500
1501 #[inline]
1509 fn fx_fold_text(hash: &mut u64, text_style: &TextStyle, text: &str) {
1510 let m0: u64 = ((text_style.get_font_size().to_bits() as u64) << 32)
1511 | (text_style.get_line_height().to_bits() as u64);
1512 let m1: u64 = ((text_style.get_letter_spacing().to_bits() as u64) << 32)
1513 | (u32::from_le_bytes(text_style.get_color().to_rgba_bytes()) as u64);
1514 let flags: u64 = (text_style.get_family() as u64)
1515 | ((text_style.get_weight() as u64) << 8)
1516 | ((text_style.get_align() as u64) << 16)
1517 | ((text_style.get_overflow() as u64) << 24)
1518 | (u64::from(text_style.get_italic()) << 32);
1519 Self::fx_fold(hash, m0);
1520 Self::fx_fold(hash, m1);
1521 Self::fx_fold(hash, flags);
1522 let bytes: &[u8] = text.as_bytes();
1523 Self::fx_fold(hash, bytes.len() as u64);
1524 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
1525 for chunk in chunks.iter() {
1526 Self::fx_fold(hash, u64::from_ne_bytes(*chunk));
1527 }
1528 if !rem.is_empty() {
1529 let mut tail: u64 = 0;
1530 for (idx, &b) in rem.iter().enumerate() {
1531 tail |= (b as u64) << (idx * 8);
1532 }
1533 Self::fx_fold(hash, tail);
1534 }
1535 }
1536
1537 #[inline(always)]
1547 #[must_use]
1548 fn quantize_scale_milli(scale: f32) -> u32 {
1549 (scale.max(0.5) * 1000.0).round() as u32
1550 }
1551
1552 #[inline]
1567 #[must_use]
1568 fn fingerprint_subtree_children(
1569 tree: &SceneTree,
1570 parent: NodeId,
1571 depth: usize,
1572 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1573 focus_pair: (Option<NodeId>, bool),
1574 acc: &mut (usize, u64, u64),
1575 ) -> bool {
1576 let child_count: usize = tree.child_count(parent);
1577 if depth > PAINT_FRAGMENT_MAX_DEPTH || child_count > PAINT_FRAGMENT_MAX_CHILDREN {
1578 return false;
1579 }
1580 let (active_hover_target, has_text_sel): (Option<NodeId>, bool) = focus_pair;
1581 Self::fx_fold(&mut acc.1, child_count as u64);
1582 for cid in tree.children_iter(parent) {
1583 if acc.0 >= PAINT_FRAGMENT_MAX_NODES
1584 || active_hover_target == Some(cid)
1585 || (has_text_sel && tree.text_selection_range_for(cid).is_some())
1586 || tree.backdrop_blur_radius(cid) > 0.0
1587 {
1588 return false;
1589 }
1590 let Some(child) = tree.get_node(cid) else {
1591 return false;
1592 };
1593 acc.0 += 1;
1594 let c_inter: InteractionState = child.get_interaction();
1595 let c_active: bool = c_inter.get_hovered() || c_inter.get_pressed();
1596 if c_active || c_inter.get_focused() {
1597 return false;
1598 }
1599 let c_style: &Style = child.style_ref();
1600 if c_style.layout.get_position() == Position::Fixed {
1601 return false;
1602 }
1603 let c_tag: u64 = match child.get_kind() {
1604 NodeKind::View | NodeKind::Fragment => 1,
1605 NodeKind::Text(text) => {
1606 Self::fx_fold_text(&mut acc.2, &c_style.text, text.as_str());
1607 Self::fx_fold_rect(&mut acc.1, child.layout_box.get_content_bounds());
1608 2
1609 }
1610 NodeKind::Image(buf) => {
1611 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1612 Self::fx_fold(
1613 &mut acc.2,
1614 ((buf.get_width() as u64) << 32) | (buf.get_height() as u64),
1615 );
1616 Self::fx_fold(&mut acc.2, buf.get_pixels().as_ptr() as usize as u64);
1617 3
1618 }
1619 NodeKind::Canvas(CanvasCaching::RetainedStatic) => {
1620 Self::fx_fold(&mut acc.2, tree.node_paint_epoch(cid) as u64);
1621 if let Some(cb) = child.canvas_callback() {
1622 let cb_addr: u64 = (Rc::as_ptr(&cb) as *const ()) as usize as u64;
1623 Self::fx_fold(&mut acc.2, cb_addr);
1624 }
1625 4
1626 }
1627 NodeKind::Portal
1628 | NodeKind::Canvas(CanvasCaching::DynamicPerFrame)
1629 | NodeKind::Input(_)
1630 | NodeKind::ScrollView => {
1631 return false;
1632 }
1633 };
1634 let c_id_bits: u64 = ((cid.get_index() as u64) << 32) | (cid.get_generation() as u64);
1635 Self::fx_fold(&mut acc.1, c_id_bits);
1636 let raw_b: Rect = child.layout_box.get_bounds();
1637 Self::fx_fold_rect(&mut acc.1, raw_b);
1638 let ct: Transform2D = c_style.paint.get_transform();
1639 Self::fx_fold(
1640 &mut acc.1,
1641 ((ct.get_translate_x().to_bits() as u64) << 32)
1642 | (ct.get_translate_y().to_bits() as u64),
1643 );
1644 Self::fx_fold(
1645 &mut acc.1,
1646 ((ct.get_scale_x().to_bits() as u64) << 32) | (ct.get_scale_y().to_bits() as u64),
1647 );
1648 let c_lay_bits: u64 = (c_tag << 40)
1649 | ((c_style.layout.get_display() as u64) << 32)
1650 | ((c_style.layout.get_overflow() as u64) << 24)
1651 | ((c_style.layout.get_z_index() as u32) as u64);
1652 Self::fx_fold(&mut acc.2, c_lay_bits);
1653 Self::fx_fold_paint(&mut acc.2, &c_style.paint);
1654 let mut next_cull_check: Option<(Point, Rect, f32, f32)> = None;
1655 if let Some((off_phys, cull_rect, scale, inv_scale)) = unclipped_cull {
1656 let tx_phys: f32 = (ct.get_translate_x() * scale).round();
1657 let ty_phys: f32 = (ct.get_translate_y() * scale).round();
1658 let tot_x: f32 = off_phys.get_x() + tx_phys;
1659 let tot_y: f32 = off_phys.get_y() + ty_phys;
1660 let rx0: f32 = (raw_b.get_x() * scale).round();
1661 let ry0: f32 = (raw_b.get_y() * scale).round();
1662 let rx1: f32 = (raw_b.right() * scale).round();
1663 let ry1: f32 = (raw_b.bottom() * scale).round();
1664 let sw: f32 = ((rx1 - rx0).max(0.0) * ct.get_scale_x()).round().max(0.0);
1665 let sh: f32 = ((ry1 - ry0).max(0.0) * ct.get_scale_y()).round().max(0.0);
1666 let sdx: f32 = (((rx1 - rx0).max(0.0) - sw) * 0.5).round();
1667 let sdy: f32 = (((ry1 - ry0).max(0.0) - sh) * 0.5).round();
1668 let c_bounds: Rect = Rect::new(
1669 (rx0 + tot_x + sdx) * inv_scale,
1670 (ry0 + tot_y + sdy) * inv_scale,
1671 sw * inv_scale,
1672 sh * inv_scale,
1673 );
1674 let c_vis: Rect = Self::conservative_self_vis_rect(
1675 child,
1676 c_bounds,
1677 &c_style.paint,
1678 false,
1679 (tot_x, tot_y),
1680 scale,
1681 inv_scale,
1682 );
1683 let c_in_x: bool =
1684 c_vis.get_x() >= cull_rect.get_x() && c_vis.right() <= cull_rect.right();
1685 let c_in_y: bool =
1686 c_vis.get_y() >= cull_rect.get_y() && c_vis.bottom() <= cull_rect.bottom();
1687 if !c_in_x || !c_in_y {
1688 return false;
1689 }
1690 if c_style.layout.get_overflow() != Overflow::Hidden {
1691 let c_scroll: Point = child.layout_box.get_scroll_offset();
1692 let next_off: Point = Point::new(
1693 tot_x - (c_scroll.get_x() * scale).round(),
1694 tot_y - (c_scroll.get_y() * scale).round(),
1695 );
1696 next_cull_check = Some((next_off, cull_rect, scale, inv_scale));
1697 }
1698 }
1699 if tree.has_children(cid)
1700 && !Self::fingerprint_subtree_children(
1701 tree,
1702 cid,
1703 depth + 1,
1704 next_cull_check,
1705 focus_pair,
1706 acc,
1707 )
1708 {
1709 return false;
1710 }
1711 }
1712 true
1713 }
1714
1715 #[inline]
1729 #[must_use]
1730 fn fingerprint_subtree(
1731 tree: &SceneTree,
1732 node_id: NodeId,
1733 node: &SceneNode,
1734 unclipped_cull: Option<(Point, Rect, f32, f32)>,
1735 focus_pair: (Option<NodeId>, bool),
1736 ) -> Option<(u64, u64)> {
1737 let child_count: usize = tree.child_count(node_id);
1738 if child_count == 0
1739 || child_count > PAINT_FRAGMENT_MAX_CHILDREN
1740 || tree.backdrop_blur_radius(node_id) > 0.0
1741 {
1742 return None;
1743 }
1744 let style: &Style = node.style_ref();
1745 let transform: Transform2D = style.paint.get_transform();
1746 let scroll: Point = node.layout_box.get_scroll_offset();
1747 let mut geom_h: u64 = child_count as u64;
1748 Self::fx_fold_rect(&mut geom_h, node.layout_box.get_bounds());
1749 Self::fx_fold(
1750 &mut geom_h,
1751 ((transform.get_translate_x().to_bits() as u64) << 32)
1752 | (transform.get_translate_y().to_bits() as u64),
1753 );
1754 Self::fx_fold(
1755 &mut geom_h,
1756 ((transform.get_scale_x().to_bits() as u64) << 32)
1757 | (transform.get_scale_y().to_bits() as u64),
1758 );
1759 Self::fx_fold(
1760 &mut geom_h,
1761 ((scroll.get_x().to_bits() as u64) << 32) | (scroll.get_y().to_bits() as u64),
1762 );
1763 let mut style_h: u64 =
1764 ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
1765 Self::fx_fold_paint(&mut style_h, &style.paint);
1766 let mut acc: (usize, u64, u64) = (0, geom_h, style_h);
1767 if !Self::fingerprint_subtree_children(
1768 tree,
1769 node_id,
1770 1,
1771 unclipped_cull,
1772 focus_pair,
1773 &mut acc,
1774 ) {
1775 return None;
1776 }
1777 Some((acc.1, acc.2))
1778 }
1779
1780 #[inline(always)]
1794 #[must_use]
1795 fn translate_coord(v: f32, d_phys: f32, scale: f32, inv_scale: f32, dyadic: bool) -> f32 {
1796 if d_phys == 0.0 {
1797 return v;
1798 }
1799 if dyadic {
1800 return v + d_phys * inv_scale;
1801 }
1802 ((v * scale).round() + d_phys) * inv_scale
1803 }
1804
1805 #[inline(always)]
1820 #[must_use]
1821 fn translate_rect(
1822 rect: Rect,
1823 dx_phys: f32,
1824 dy_phys: f32,
1825 scale: f32,
1826 inv_scale: f32,
1827 dyadic: bool,
1828 ) -> Rect {
1829 Rect::new(
1830 Self::translate_coord(rect.get_x(), dx_phys, scale, inv_scale, dyadic),
1831 Self::translate_coord(rect.get_y(), dy_phys, scale, inv_scale, dyadic),
1832 rect.get_width(),
1833 rect.get_height(),
1834 )
1835 }
1836
1837 #[inline]
1847 fn push_translated_command(
1848 cmd: &CachedDrawCommand,
1849 text_slab: &[u8],
1850 delta_phys: (f32, f32),
1851 scale_info: (f32, f32, bool),
1852 display_list: &mut DisplayList,
1853 ) {
1854 let (dx_phys, dy_phys): (f32, f32) = delta_phys;
1855 let (scale, inv_scale, dyadic): (f32, f32, bool) = scale_info;
1856 let translated: DrawCommand = match cmd {
1857 CachedDrawCommand::Clear(c) => DrawCommand::Clear(*c),
1858 CachedDrawCommand::FillRoundedRect {
1859 rect,
1860 color,
1861 radius,
1862 border,
1863 } => DrawCommand::FillRoundedRect {
1864 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1865 color: *color,
1866 radius: *radius,
1867 border: *border,
1868 },
1869 CachedDrawCommand::LinearGradientRect {
1870 rect,
1871 gradient,
1872 radius,
1873 } => DrawCommand::LinearGradientRect {
1874 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1875 gradient: *gradient,
1876 radius: *radius,
1877 },
1878 CachedDrawCommand::BoxShadow {
1879 rect,
1880 radius,
1881 shadow,
1882 } => DrawCommand::BoxShadow {
1883 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1884 radius: *radius,
1885 shadow: *shadow,
1886 },
1887 CachedDrawCommand::BackdropBlur {
1888 rect,
1889 radius,
1890 blur_radius,
1891 } => DrawCommand::BackdropBlur {
1892 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1893 radius: *radius,
1894 blur_radius: *blur_radius,
1895 },
1896 CachedDrawCommand::TextRun {
1897 rect,
1898 text_start,
1899 text_len,
1900 style,
1901 } => {
1902 let s: usize = *text_start as usize;
1903 let e: usize = s.saturating_add(*text_len as usize);
1904 let src: &str = text_slab
1905 .get(s..e)
1906 .and_then(|b: &[u8]| from_utf8(b).ok())
1907 .unwrap_or("");
1908 DrawCommand::TextRun {
1909 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1910 text: display_list.alloc_text(src),
1911 style: *style,
1912 }
1913 }
1914 CachedDrawCommand::TextCaret {
1915 text_rect,
1916 content_rect,
1917 text_start,
1918 text_len,
1919 style,
1920 caret_height,
1921 color,
1922 multiline,
1923 } => {
1924 let s: usize = *text_start as usize;
1925 let e: usize = s.saturating_add(*text_len as usize);
1926 let src: &str = text_slab
1927 .get(s..e)
1928 .and_then(|b: &[u8]| from_utf8(b).ok())
1929 .unwrap_or("");
1930 DrawCommand::TextCaret {
1931 text_rect: Self::translate_rect(
1932 *text_rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
1933 ),
1934 content_rect: Self::translate_rect(
1935 *content_rect,
1936 dx_phys,
1937 dy_phys,
1938 scale,
1939 inv_scale,
1940 dyadic,
1941 ),
1942 text: display_list.alloc_text(src),
1943 style: *style,
1944 caret_height: *caret_height,
1945 color: *color,
1946 multiline: *multiline,
1947 }
1948 }
1949 CachedDrawCommand::Image {
1950 rect,
1951 buffer,
1952 radius,
1953 } => DrawCommand::Image {
1954 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1955 buffer: buffer.clone(),
1956 radius: *radius,
1957 },
1958 CachedDrawCommand::SharedImage {
1959 rect,
1960 width,
1961 height,
1962 pixels,
1963 radius,
1964 } => DrawCommand::SharedImage {
1965 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
1966 width: *width,
1967 height: *height,
1968 pixels: Arc::clone(pixels),
1969 radius: *radius,
1970 },
1971 CachedDrawCommand::Line {
1972 from,
1973 to,
1974 color,
1975 width,
1976 } => DrawCommand::Line {
1977 from: Point::new(
1978 Self::translate_coord(from.get_x(), dx_phys, scale, inv_scale, dyadic),
1979 Self::translate_coord(from.get_y(), dy_phys, scale, inv_scale, dyadic),
1980 ),
1981 to: Point::new(
1982 Self::translate_coord(to.get_x(), dx_phys, scale, inv_scale, dyadic),
1983 Self::translate_coord(to.get_y(), dy_phys, scale, inv_scale, dyadic),
1984 ),
1985 color: *color,
1986 width: *width,
1987 },
1988 CachedDrawCommand::Circle {
1989 center,
1990 radius,
1991 color,
1992 } => DrawCommand::Circle {
1993 center: Point::new(
1994 Self::translate_coord(center.get_x(), dx_phys, scale, inv_scale, dyadic),
1995 Self::translate_coord(center.get_y(), dy_phys, scale, inv_scale, dyadic),
1996 ),
1997 radius: *radius,
1998 color: *color,
1999 },
2000 CachedDrawCommand::Triangle { p0, p1, p2, color } => DrawCommand::Triangle {
2001 p0: Point::new(
2002 Self::translate_coord(p0.get_x(), dx_phys, scale, inv_scale, dyadic),
2003 Self::translate_coord(p0.get_y(), dy_phys, scale, inv_scale, dyadic),
2004 ),
2005 p1: Point::new(
2006 Self::translate_coord(p1.get_x(), dx_phys, scale, inv_scale, dyadic),
2007 Self::translate_coord(p1.get_y(), dy_phys, scale, inv_scale, dyadic),
2008 ),
2009 p2: Point::new(
2010 Self::translate_coord(p2.get_x(), dx_phys, scale, inv_scale, dyadic),
2011 Self::translate_coord(p2.get_y(), dy_phys, scale, inv_scale, dyadic),
2012 ),
2013 color: *color,
2014 },
2015 CachedDrawCommand::SpriteRegion {
2016 rect,
2017 buffer,
2018 src_rect,
2019 tint,
2020 } => DrawCommand::SpriteRegion {
2021 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2022 buffer: buffer.clone(),
2023 src_rect: *src_rect,
2024 tint: *tint,
2025 },
2026 CachedDrawCommand::PushClip(rect) => DrawCommand::PushClip(Self::translate_rect(
2027 *rect, dx_phys, dy_phys, scale, inv_scale, dyadic,
2028 )),
2029 CachedDrawCommand::PushRoundedClip { rect, radius } => DrawCommand::PushRoundedClip {
2030 rect: Self::translate_rect(*rect, dx_phys, dy_phys, scale, inv_scale, dyadic),
2031 radius: *radius,
2032 },
2033 CachedDrawCommand::PopClip => DrawCommand::PopClip,
2034 };
2035 display_list.push(translated);
2036 }
2037
2038 pub fn build(
2048 tree: &SceneTree,
2049 viewport: Size,
2050 scale_factor: f32,
2051 clear_color: Color,
2052 display_list: &mut DisplayList,
2053 ) {
2054 let mut saw_canvas: bool = false;
2055 Self::build_inner(
2056 tree,
2057 viewport,
2058 scale_factor,
2059 clear_color,
2060 display_list,
2061 None,
2062 &mut saw_canvas,
2063 );
2064 }
2065
2066 #[inline(always)]
2076 #[must_use]
2077 fn pack_opt_node_id(node: Option<NodeId>) -> u64 {
2078 match node {
2079 Some(id) => ((id.get_index() as u64) << 32) | (id.get_generation() as u64),
2080 None => u64::MAX,
2081 }
2082 }
2083
2084 pub fn build_retained(
2096 tree: &SceneTree,
2097 viewport: Size,
2098 scale_factor: f32,
2099 clear_color: Color,
2100 layout_changed: bool,
2101 display_list: &mut DisplayList,
2102 cache: &mut PaintFragmentCache,
2103 ) {
2104 let clamped_scale: f32 = scale_factor.max(0.5);
2105 let focus: FocusManager = tree.get_focus();
2106 let sig: PaintSceneSignature = PaintSceneSignature {
2107 tree_ptr: (tree as *const SceneTree) as usize,
2108 tree_revision: tree.get_revision(),
2109 viewport_w_bits: viewport.get_width().to_bits(),
2110 viewport_h_bits: viewport.get_height().to_bits(),
2111 scale_bits: clamped_scale.to_bits(),
2112 clear_rgba: u32::from_le_bytes(clear_color.to_rgba_bytes()),
2113 focused_packed: Self::pack_opt_node_id(focus.focused_node()),
2114 hovered_packed: Self::pack_opt_node_id(focus.hovered_node()),
2115 pressed_packed: Self::pack_opt_node_id(focus.pressed_node()),
2116 caret_visible: tree.get_caret_visible(),
2117 text_sel_sig: Self::text_selection_sig(tree),
2118 };
2119 if !layout_changed
2120 && !cache.last_has_canvas
2121 && !display_list.is_empty()
2122 && cache.has_last_scene
2123 && cache.last_scene_sig == sig
2124 {
2125 cache.whole_list_hits = cache.whole_list_hits.wrapping_add(1);
2126 return;
2127 }
2128 cache.ensure_slots();
2129 let mut saw_canvas: bool = false;
2130 Self::build_inner(
2131 tree,
2132 viewport,
2133 clamped_scale,
2134 clear_color,
2135 display_list,
2136 Some(cache),
2137 &mut saw_canvas,
2138 );
2139 cache.last_has_canvas = saw_canvas;
2140 cache.has_last_scene = true;
2141 cache.last_scene_sig = sig;
2142 }
2143
2144 fn collect_portal_ancestors(tree: &SceneTree, out_ancestors: &mut Vec<NodeId>) {
2151 out_ancestors.clear();
2152 let portal_nodes: &[NodeId] = tree.portal_nodes();
2153 if portal_nodes.is_empty() {
2154 return;
2155 }
2156 for &pid in portal_nodes.iter() {
2157 let mut cur: Option<NodeId> = tree.get_node(pid).and_then(SceneNode::parent_id);
2158 while let Some(anc) = cur {
2159 if out_ancestors.contains(&anc) {
2160 break;
2161 }
2162 out_ancestors.push(anc);
2163 cur = tree.get_node(anc).and_then(SceneNode::parent_id);
2164 }
2165 }
2166 if out_ancestors.len() > 16 {
2167 out_ancestors.sort_unstable_by_key(|id: &NodeId| {
2168 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2169 });
2170 }
2171 }
2172
2173 #[inline]
2184 #[must_use]
2185 fn is_portal_ancestor(ancestors: &[NodeId], node_id: NodeId) -> bool {
2186 if ancestors.is_empty() {
2187 return false;
2188 }
2189 if ancestors.len() <= 16 {
2190 return ancestors.contains(&node_id);
2191 }
2192 let target: u64 = ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
2193 ancestors
2194 .binary_search_by_key(&target, |id: &NodeId| {
2195 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
2196 })
2197 .is_ok()
2198 }
2199
2200 #[inline]
2216 #[must_use]
2217 fn conservative_self_vis_rect(
2218 node: &SceneNode,
2219 bounds: Rect,
2220 paint: &PaintStyle,
2221 focused: bool,
2222 total_off_phys: (f32, f32),
2223 scale: f32,
2224 inv_scale: f32,
2225 ) -> Rect {
2226 let mut min_x: f32 = bounds.get_x() - 1.0;
2227 let mut min_y: f32 = bounds.get_y() - 1.0;
2228 let mut max_x: f32 = bounds.right() + 1.0;
2229 let mut max_y: f32 = bounds.bottom() + 1.0;
2230 if let Some(shadow) = paint.box_shadow()
2231 && shadow.get_color().get_a() > 0
2232 {
2233 let spread: f32 = shadow.get_spread_radius().max(0.0);
2234 let blur_phys: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
2235 let sigma_phys: f32 = (blur_phys * 0.5).max(0.75);
2236 let blur_pad: f32 = (sigma_phys * 3.0).ceil() * inv_scale + 2.0 * inv_scale;
2237 let pad: f32 = spread + blur_pad;
2238 let sx0: f32 = bounds.get_x() + shadow.get_offset_x() - pad;
2239 let sy0: f32 = bounds.get_y() + shadow.get_offset_y() - pad;
2240 let sx1: f32 = bounds.right() + shadow.get_offset_x() + pad;
2241 let sy1: f32 = bounds.bottom() + shadow.get_offset_y() + pad;
2242 min_x = min_x.min(sx0);
2243 min_y = min_y.min(sy0);
2244 max_x = max_x.max(sx1);
2245 max_y = max_y.max(sy1);
2246 }
2247 if focused && let Some(ring) = paint.focus_ring_border() {
2248 let rw: f32 = ring.get_width().max(0.0) + 1.0;
2249 min_x = min_x.min(bounds.get_x() - rw);
2250 min_y = min_y.min(bounds.get_y() - rw);
2251 max_x = max_x.max(bounds.right() + rw);
2252 max_y = max_y.max(bounds.bottom() + rw);
2253 }
2254 if matches!(node.get_kind(), NodeKind::Text(_)) {
2255 let content_b: Rect = node.layout_box.get_content_bounds();
2256 let cx0: f32 = ((content_b.get_x() * scale).round() + total_off_phys.0) * inv_scale;
2257 let cy0: f32 = ((content_b.get_y() * scale).round() + total_off_phys.1) * inv_scale;
2258 let pad: f32 = node.style_ref().text.get_font_size() * 0.5 + 2.0;
2259 min_x = min_x.min(cx0 - pad);
2260 min_y = min_y.min(cy0 - pad);
2261 max_x = max_x.max(cx0 + content_b.get_width() + pad);
2262 max_y = max_y.max(cy0 + content_b.get_height() + pad);
2263 }
2264 Rect::new(
2265 min_x,
2266 min_y,
2267 (max_x - min_x).max(0.0),
2268 (max_y - min_y).max(0.0),
2269 )
2270 }
2271
2272 fn visit_culled_portal_child(
2281 tree: &SceneTree,
2282 child_id: NodeId,
2283 parent_opacity: f32,
2284 scratch: &mut PaintScratch<'_>,
2285 ) {
2286 let Some(child) = tree.get_node(child_id) else {
2287 return;
2288 };
2289 let style: &Style = child.style_ref();
2290 let layout: &LayoutStyle = &style.layout;
2291 if layout.get_display() == Display::None {
2292 return;
2293 }
2294 let is_portal: bool = matches!(child.get_kind(), NodeKind::Portal)
2295 || layout.get_position() == Position::Fixed;
2296 if is_portal {
2297 if let Some(portal_queue) = scratch.portals.as_deref_mut() {
2298 portal_queue.push((layout.get_z_index(), portal_queue.len(), child_id));
2299 }
2300 return;
2301 }
2302 if !Self::is_portal_ancestor(scratch.portal_ancestors, child_id) {
2303 return;
2304 }
2305 let hovered: bool = child.get_interaction().get_hovered();
2306 let paint_opacity: f32 = if hovered {
2307 child
2308 .hover_paint()
2309 .map(|hp: PaintStyle| hp.get_opacity())
2310 .unwrap_or_else(|| style.paint.get_opacity())
2311 } else {
2312 style.paint.get_opacity()
2313 };
2314 let next_opacity: f32 = (parent_opacity * paint_opacity).clamp(0.0, 1.0);
2315 if next_opacity <= 0.0 {
2316 return;
2317 }
2318 Self::collect_portals_in_culled_subtree(tree, child_id, next_opacity, scratch);
2319 }
2320
2321 fn collect_portals_in_culled_subtree(
2330 tree: &SceneTree,
2331 node_id: NodeId,
2332 parent_opacity: f32,
2333 scratch: &mut PaintScratch<'_>,
2334 ) {
2335 if tree.get_node(node_id).is_none() {
2336 return;
2337 }
2338 let child_count: usize = tree.child_count(node_id);
2339 if child_count == 0 {
2340 return;
2341 }
2342 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
2343 if !needs_z_sort {
2344 for child_id in tree.children_iter(node_id) {
2345 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2346 }
2347 } else {
2348 let sort_start: usize = scratch.z_sort.len();
2349 for (idx, cid) in tree.children_iter(node_id).enumerate() {
2350 if let Some(c) = tree.get_node(cid) {
2351 scratch
2352 .z_sort
2353 .push((c.style_ref().layout.get_z_index(), idx, cid));
2354 }
2355 }
2356 let sort_end: usize = scratch.z_sort.len();
2357 scratch.z_sort[sort_start..sort_end].sort_by(
2358 |a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
2359 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
2360 },
2361 );
2362 for slot_idx in sort_start..sort_end {
2363 let child_id: NodeId = scratch.z_sort[slot_idx].2;
2364 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
2365 }
2366 scratch.z_sort.truncate(sort_start);
2367 }
2368 }
2369
2370 fn build_inner(
2382 tree: &SceneTree,
2383 viewport: Size,
2384 scale_factor: f32,
2385 clear_color: Color,
2386 display_list: &mut DisplayList,
2387 mut fragments: Option<&mut PaintFragmentCache>,
2388 saw_canvas: &mut bool,
2389 ) {
2390 display_list.reset(viewport, scale_factor);
2391 display_list.push(DrawCommand::Clear(clear_color));
2392 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
2393 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
2394 let mut portals: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.portals_scratch);
2395 let mut z_sort: Vec<(i32, usize, NodeId)> = take(&mut display_list.arena.z_sort_scratch);
2396 let mut portal_ancestors: Vec<NodeId> =
2397 take(&mut display_list.arena.portal_ancestors_scratch);
2398 portals.clear();
2399 z_sort.clear();
2400 Self::collect_portal_ancestors(tree, &mut portal_ancestors);
2401 {
2402 let mut scratch: PaintScratch<'_> = PaintScratch {
2403 portals: Some(&mut portals),
2404 z_sort: &mut z_sort,
2405 portal_ancestors: portal_ancestors.as_slice(),
2406 fragments: fragments.as_deref_mut(),
2407 saw_canvas,
2408 };
2409 Self::emit_node(
2410 tree,
2411 tree.get_root(),
2412 (Point::new(0.0, 0.0), 1.0),
2413 viewport_rect,
2414 active_hover_target,
2415 &mut scratch,
2416 display_list,
2417 );
2418 }
2419 display_list.finalize_base_layer(display_list.len());
2420 if portals.len() > 1 {
2421 portals.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
2422 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
2423 });
2424 }
2425 {
2426 let mut portal_scratch: PaintScratch<'_> = PaintScratch {
2427 portals: None,
2428 z_sort: &mut z_sort,
2429 portal_ancestors: &[],
2430 fragments,
2431 saw_canvas,
2432 };
2433 for &(_, _, portal_id) in portals.iter() {
2434 Self::emit_node(
2435 tree,
2436 portal_id,
2437 (Point::new(0.0, 0.0), 1.0),
2438 viewport_rect,
2439 active_hover_target,
2440 &mut portal_scratch,
2441 display_list,
2442 );
2443 }
2444 }
2445 portals.clear();
2446 z_sort.clear();
2447 portal_ancestors.clear();
2448 display_list.arena.portals_scratch = portals;
2449 display_list.arena.z_sort_scratch = z_sort;
2450 display_list.arena.portal_ancestors_scratch = portal_ancestors;
2451 }
2452
2453 #[must_use]
2463 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
2464 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
2465 while let Some(id) = cursor {
2466 let Some(node) = tree.get_node(id) else {
2467 break;
2468 };
2469 let style: &Style = node.style_ref();
2470 let paint: &PaintStyle = &style.paint;
2471 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2472 || style.layout.get_position() == Position::Fixed;
2473 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2474 && paint.background_color().is_none();
2475 if !is_scrim && !is_transparent_canvas {
2476 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
2477 || node
2478 .get_handlers()
2479 .handler_for(&UiEventKind::Click(Point::default()))
2480 .is_some();
2481 if is_interactive {
2482 return Some(id);
2483 }
2484 }
2485 cursor = node.parent_id();
2486 }
2487 None
2488 }
2489
2490 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
2502 if !hovered && !pressed {
2503 return base;
2504 }
2505 let delta: i32 = if pressed { 24 } else { 14 };
2506 match base {
2507 Some(c) if c.get_a() > 0 => {
2508 let luma: u32 =
2509 (c.get_r() as u32) * 299 + (c.get_g() as u32) * 587 + (c.get_b() as u32) * 114;
2510 if luma < 120_000 {
2511 let lift: i32 = if pressed { 30 } else { 20 };
2512 Some(Color::new(
2513 (c.get_r() as i32 + lift).clamp(0, 255) as u8,
2514 (c.get_g() as i32 + lift).clamp(0, 255) as u8,
2515 (c.get_b() as i32 + lift).clamp(0, 255) as u8,
2516 c.get_a(),
2517 ))
2518 } else {
2519 Some(Color::new(
2520 (c.get_r() as i32 - delta).clamp(0, 255) as u8,
2521 (c.get_g() as i32 - delta).clamp(0, 255) as u8,
2522 (c.get_b() as i32 - delta).clamp(0, 255) as u8,
2523 c.get_a(),
2524 ))
2525 }
2526 }
2527 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
2528 }
2529 }
2530
2531 fn emit_node(
2543 tree: &SceneTree,
2544 node_id: NodeId,
2545 offset_and_opacity: (Point, f32),
2546 cull_rect: Rect,
2547 active_hover_target: Option<NodeId>,
2548 scratch: &mut PaintScratch<'_>,
2549 display_list: &mut DisplayList,
2550 ) {
2551 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
2552 let Some(node) = tree.get_node(node_id) else {
2553 return;
2554 };
2555 let style: &Style = node.style_ref();
2556 let layout: &LayoutStyle = &style.layout;
2557 if layout.get_display() == Display::None {
2558 return;
2559 }
2560 if let Some(portal_queue) = scratch.portals.as_deref_mut()
2561 && (matches!(node.get_kind(), NodeKind::Portal)
2562 || layout.get_position() == Position::Fixed)
2563 {
2564 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
2565 return;
2566 }
2567 let interaction: InteractionState = node.get_interaction();
2568 let hovered: bool = interaction.get_hovered();
2569 let pressed: bool = interaction.get_pressed();
2570 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
2571 return;
2572 }
2573 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
2574 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2575 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
2576 if opacity <= 0.0 {
2577 return;
2578 }
2579 let scale: f32 = display_list.get_scale_factor().max(0.5);
2580 let inv_scale: f32 = 1.0 / scale;
2581 let raw_bounds: Rect = node.layout_box.get_bounds();
2582 let transform: Transform2D = prelim_paint.get_transform();
2583 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
2584 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
2585 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
2586 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
2587 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
2588 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
2589 let raw_x1: f32 = (raw_bounds.right() * scale).round();
2590 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
2591 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
2592 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
2593 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
2594 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
2595 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
2596 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
2597 let bounds: Rect = Rect::new(
2598 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
2599 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
2600 scaled_w_phys * inv_scale,
2601 scaled_h_phys * inv_scale,
2602 );
2603 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
2604 let clipped_cull: Option<Rect> = if clipped {
2605 cull_rect.intersect(bounds)
2606 } else {
2607 Some(cull_rect)
2608 };
2609 let focused: bool = interaction.get_focused();
2610 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
2611 node,
2612 bounds,
2613 prelim_paint,
2614 focused,
2615 (total_off_x, total_off_y),
2616 scale,
2617 inv_scale,
2618 );
2619 let self_visible: bool = bounds.get_width() > 0.0
2620 && bounds.get_height() > 0.0
2621 && self_vis_rect.intersect(cull_rect).is_some();
2622 if clipped && clipped_cull.is_none() {
2623 if self_visible {
2624 if let Some(mut shadow) = prelim_paint.box_shadow() {
2625 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2626 display_list.push(DrawCommand::BoxShadow {
2627 rect: bounds,
2628 radius: prelim_paint.get_radius(),
2629 shadow,
2630 });
2631 }
2632 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
2633 ring.set_color(ring.get_color().multiply_alpha(opacity));
2634 let ring_w: f32 = ring.get_width();
2635 let ring_rect: Rect = Rect::new(
2636 bounds.get_x() - ring_w,
2637 bounds.get_y() - ring_w,
2638 bounds.get_width() + ring_w * 2.0,
2639 bounds.get_height() + ring_w * 2.0,
2640 );
2641 display_list.push(DrawCommand::FillRoundedRect {
2642 rect: ring_rect,
2643 color: COLOR_TRANSPARENT,
2644 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
2645 border: ring,
2646 });
2647 }
2648 }
2649 if scratch.portals.is_some()
2650 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
2651 {
2652 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
2653 }
2654 return;
2655 }
2656 let child_count: usize = tree.child_count(node_id);
2657 if !self_visible && child_count == 0 {
2658 return;
2659 }
2660 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
2661 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
2662 if self_visible
2663 && (hover_visual || pressed)
2664 && paint_owned.is_none()
2665 && (hovered || node.hover_paint().is_none())
2666 {
2667 let base_paint: &PaintStyle = &style.paint;
2668 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
2669 || layout.get_position() == Position::Fixed;
2670 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas(_))
2671 && base_paint.background_color().is_none();
2672 if !is_scrim && !is_transparent_canvas {
2673 let is_interactive: bool = base_paint.get_cursor() == CursorIcon::Pointer
2674 || node
2675 .get_handlers()
2676 .handler_for(&UiEventKind::Click(Point::default()))
2677 .is_some();
2678 if is_interactive {
2679 let mut tinted: PaintStyle = *base_paint;
2680 tinted.set_background(Self::interactive_tint(
2681 tinted.background_color(),
2682 hover_visual,
2683 pressed,
2684 ));
2685 paint_owned = Some(tinted);
2686 }
2687 }
2688 }
2689 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
2690 let phys_scale_milli: u32 = Self::quantize_scale_milli(scale);
2691 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
2692 let frag_overflow_ok: bool = layout.get_overflow() == Overflow::Hidden
2693 || (layout.get_overflow() == Overflow::Visible
2694 && node_id != tree.get_root()
2695 && child_count >= 2);
2696 if self_visible
2697 && scratch.fragments.is_some()
2698 && scratch.portals.is_some()
2699 && matches!(node.get_kind(), NodeKind::View)
2700 && frag_overflow_ok
2701 && !hovered
2702 && !pressed
2703 && !interaction.get_focused()
2704 && active_hover_target != Some(node_id)
2705 {
2706 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
2707 && self_vis_rect.right() <= cull_rect.right();
2708 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
2709 && self_vis_rect.bottom() <= cull_rect.bottom();
2710 if vis_x_in && vis_y_in {
2711 let has_text_sel: bool = !tree.text_selection_spans_ref().is_empty();
2712 let unclipped_cull: Option<(Point, Rect, f32, f32)> =
2713 if layout.get_overflow() == Overflow::Hidden {
2714 None
2715 } else {
2716 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
2717 let child_off: Point = Point::new(
2718 total_off_x - (raw_node_scroll.get_x() * scale).round(),
2719 total_off_y - (raw_node_scroll.get_y() * scale).round(),
2720 );
2721 Some((child_off, cull_rect, scale, inv_scale))
2722 };
2723 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_subtree(
2724 tree,
2725 node_id,
2726 node,
2727 unclipped_cull,
2728 (active_hover_target, has_text_sel),
2729 ) {
2730 let slot_idx: usize =
2731 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
2732 let opacity_bits: u32 = parent_opacity.to_bits();
2733 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
2734 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
2735 if let Some(cache) = scratch.fragments.as_deref_mut()
2736 && let Some(&cached) = cache.slots.get(slot_idx)
2737 && cached.occupied
2738 && cached.node_id == node_id
2739 && cached.phys_scale_milli == phys_scale_milli
2740 && cached.opacity_bits == opacity_bits
2741 && cached.geom_hash == geom_hash
2742 && cached.style_text_hash == style_text_hash
2743 {
2744 let dx_phys: f32 = offset_phys.get_x() - cached.offset_phys.get_x();
2745 let dy_phys: f32 = offset_phys.get_y() - cached.offset_phys.get_y();
2746 let c_start: usize = cached.cmd_start as usize;
2747 let c_end: usize = c_start.saturating_add(cached.cmd_len as usize);
2748 if let Some(cmds) = cache.cmd_slab.get(c_start..c_end) {
2749 display_list.get_mut_commands().reserve(cmds.len());
2750 let text_slab: &[u8] = cache.text_slab.as_slice();
2751 for cmd in cmds.iter() {
2752 Self::push_translated_command(
2753 cmd,
2754 text_slab,
2755 (dx_phys, dy_phys),
2756 (scale, inv_scale, dyadic_scale),
2757 display_list,
2758 );
2759 }
2760 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
2761 return;
2762 }
2763 }
2764 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
2765 }
2766 }
2767 }
2768 if self_visible {
2769 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
2770 if blur_r > 0.0 {
2771 display_list.push(DrawCommand::BackdropBlur {
2772 rect: bounds,
2773 radius: paint.get_radius(),
2774 blur_radius: blur_r,
2775 });
2776 }
2777 if let Some(mut shadow) = paint.box_shadow() {
2778 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
2779 display_list.push(DrawCommand::BoxShadow {
2780 rect: bounds,
2781 radius: paint.get_radius(),
2782 shadow,
2783 });
2784 }
2785 if let Some(mut gradient) = paint.background_gradient() {
2786 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
2787 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
2788 display_list.push(DrawCommand::LinearGradientRect {
2789 rect: bounds,
2790 gradient,
2791 radius: paint.get_radius(),
2792 });
2793 } else if let Some(bg) = paint.background_color() {
2794 let mut border: Border = paint.get_border();
2795 border.set_color(border.get_color().multiply_alpha(opacity));
2796 display_list.push(DrawCommand::FillRoundedRect {
2797 rect: bounds,
2798 color: bg.multiply_alpha(opacity),
2799 radius: paint.get_radius(),
2800 border,
2801 });
2802 } else if paint.get_border().get_width() > 0.0 {
2803 let mut border: Border = paint.get_border();
2804 border.set_color(border.get_color().multiply_alpha(opacity));
2805 display_list.push(DrawCommand::FillRoundedRect {
2806 rect: bounds,
2807 color: COLOR_TRANSPARENT,
2808 radius: paint.get_radius(),
2809 border,
2810 });
2811 }
2812 if interaction.get_focused()
2813 && let Some(mut ring) = paint.focus_ring_border()
2814 {
2815 ring.set_color(ring.get_color().multiply_alpha(opacity));
2816 let ring_w: f32 = ring.get_width();
2817 let ring_rect: Rect = Rect::new(
2818 bounds.get_x() - ring_w,
2819 bounds.get_y() - ring_w,
2820 bounds.get_width() + ring_w * 2.0,
2821 bounds.get_height() + ring_w * 2.0,
2822 );
2823 display_list.push(DrawCommand::FillRoundedRect {
2824 rect: ring_rect,
2825 color: COLOR_TRANSPARENT,
2826 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
2827 border: ring,
2828 });
2829 }
2830 match node.get_kind() {
2831 NodeKind::Text(text) => {
2832 let mut text_style: TextStyle = style.text;
2833 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2834 let content_b: Rect = node.layout_box.get_content_bounds();
2835 let cx0: f32 = (content_b.get_x() * scale).round();
2836 let cy0: f32 = (content_b.get_y() * scale).round();
2837 let shifted_content: Rect = Rect::new(
2838 (cx0 + total_off_x) * inv_scale,
2839 (cy0 + total_off_y) * inv_scale,
2840 content_b.get_width(),
2841 content_b.get_height(),
2842 );
2843 let has_box_chrome: bool = style
2844 .paint
2845 .background_color()
2846 .is_some_and(|c: Color| c.get_a() > 0)
2847 || style.paint.background_gradient().is_some()
2848 || style.paint.get_border().get_width() > 0.0
2849 || style.layout.get_height() != Length::Auto
2850 || style.layout.get_min_height() != Length::Auto;
2851 let text_run_rect: Rect = if has_box_chrome
2852 && shifted_content.get_height() > 1.0
2853 {
2854 let cached_h: f32 = node.layout_box.get_content_size().get_height();
2855 let measured_h: f32 =
2856 if cached_h > 0.0 && cached_h + 0.5 < shifted_content.get_height() {
2857 cached_h
2858 } else {
2859 DeterministicTextMeasurer
2860 .measure_size(
2861 text.as_str(),
2862 text_style,
2863 Some(shifted_content.get_width().max(1.0)),
2864 )
2865 .get_height()
2866 };
2867 if measured_h > 0.0 && shifted_content.get_height() > measured_h + 0.5 {
2868 let v_off_phys: f32 =
2869 (((shifted_content.get_height() - measured_h) * 0.5).max(0.0)
2870 * scale)
2871 .round();
2872 Rect::new(
2873 shifted_content.get_x(),
2874 (cy0 + total_off_y + v_off_phys) * inv_scale,
2875 shifted_content.get_width(),
2876 measured_h,
2877 )
2878 } else {
2879 shifted_content
2880 }
2881 } else {
2882 shifted_content
2883 };
2884 if let Some(sel_range) = tree.text_selection_range_for(node_id) {
2885 let sel_color: Color = tree
2886 .effective_selection_color(node_id)
2887 .multiply_alpha(opacity);
2888 let sel_rects: Vec<Rect> = compute_text_selection_rects(
2889 text.as_str(),
2890 text_style,
2891 text_run_rect,
2892 sel_range,
2893 );
2894 let sel_single_to_end: bool = sel_rects.len() == 1
2895 && text_style.get_align() == TextAlign::Left
2896 && sel_range.1 >= text.chars().count();
2897 let exact_span_right: f32 =
2898 ((content_b.right() * scale).round() + total_off_x) * inv_scale;
2899 for sel_rect in sel_rects.into_iter() {
2900 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
2901 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
2902 let sx1: f32 = if sel_single_to_end {
2903 exact_span_right.max(sx0 + inv_scale)
2904 } else {
2905 (sel_rect.right() * scale).round() * inv_scale
2906 };
2907 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
2908 display_list.push(DrawCommand::FillRoundedRect {
2909 rect: Rect::new(
2910 sx0,
2911 sy0,
2912 (sx1 - sx0).max(inv_scale),
2913 (sy1 - sy0).max(inv_scale),
2914 ),
2915 color: sel_color,
2916 radius: CornerRadius::default(),
2917 border: Border::default(),
2918 });
2919 }
2920 }
2921 let pooled_text: String = display_list.alloc_text(text.as_str());
2922 display_list.push(DrawCommand::TextRun {
2923 rect: text_run_rect,
2924 text: pooled_text,
2925 style: text_style,
2926 });
2927 }
2928 NodeKind::Input(input_state) => {
2929 let mut text_style: TextStyle = style.text;
2930 let is_empty_value: bool = input_state.get_value().is_empty();
2931 let is_focused: bool = interaction.get_focused();
2932 let rendered_text: String = if is_empty_value {
2933 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
2934 display_list.alloc_text(input_state.get_placeholder().as_str())
2935 } else if input_state.get_password() {
2936 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2937 let star_count: usize = input_state.get_value().chars().count();
2938 let mut masked: String = display_list.alloc_text("");
2939 for _ in 0..star_count {
2940 masked.push('*');
2941 }
2942 masked
2943 } else {
2944 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
2945 display_list.alloc_text(input_state.get_value().as_str())
2946 };
2947 let content_b: Rect = node.layout_box.get_content_bounds();
2948 let cx0: f32 = (content_b.get_x() * scale).round();
2949 let cy0: f32 = (content_b.get_y() * scale).round();
2950 let shifted_content: Rect = Rect::new(
2951 (cx0 + total_off_x) * inv_scale,
2952 (cy0 + total_off_y) * inv_scale,
2953 content_b.get_width(),
2954 content_b.get_height(),
2955 );
2956 let line_h: f32 =
2957 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
2958 let text_rect: Rect = if input_state.get_multiline() {
2959 shifted_content
2960 } else {
2961 text_style.set_overflow(TextOverflow::Clip);
2962 let v_off_phys: f32 =
2963 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
2964 .round();
2965 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
2966 Rect::new(
2967 shifted_content.get_x(),
2968 text_y,
2969 shifted_content.get_width(),
2970 line_h,
2971 )
2972 };
2973 let run_rect: Rect =
2974 if is_empty_value && is_focused && !rendered_text.is_empty() {
2975 let clearance_phys: f32 =
2976 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
2977 let clearance_logical: f32 = clearance_phys * inv_scale;
2978 Rect::new(
2979 text_rect.get_x() + clearance_logical,
2980 text_rect.get_y(),
2981 (text_rect.get_width() - clearance_logical).max(0.0),
2982 text_rect.get_height(),
2983 )
2984 } else {
2985 text_rect
2986 };
2987 let show_caret: bool = is_focused && tree.get_caret_visible();
2988 let caret_text: String = if show_caret && !is_empty_value {
2989 let caret_prefix: &str =
2990 input_state.caret_prefix_for_display(rendered_text.as_str());
2991 display_list.alloc_text(caret_prefix)
2992 } else {
2993 String::new()
2994 };
2995 display_list.push(DrawCommand::PushClip(shifted_content));
2996 if is_focused
2997 && !is_empty_value
2998 && let Some(sel_range) = input_state.selection_char_range()
2999 {
3000 let sel_color: Color = tree
3001 .effective_selection_color(node_id)
3002 .multiply_alpha(opacity);
3003 let sel_rects: Vec<Rect> = compute_text_selection_rects(
3004 rendered_text.as_str(),
3005 text_style,
3006 text_rect,
3007 sel_range,
3008 );
3009 for sel_rect in sel_rects.into_iter() {
3010 let sx0: f32 = (sel_rect.get_x() * scale).round() * inv_scale;
3011 let sy0: f32 = (sel_rect.get_y() * scale).round() * inv_scale;
3012 let sx1: f32 = (sel_rect.right() * scale).round() * inv_scale;
3013 let sy1: f32 = (sel_rect.bottom() * scale).round() * inv_scale;
3014 display_list.push(DrawCommand::FillRoundedRect {
3015 rect: Rect::new(
3016 sx0,
3017 sy0,
3018 (sx1 - sx0).max(inv_scale),
3019 (sy1 - sy0).max(inv_scale),
3020 ),
3021 color: sel_color,
3022 radius: CornerRadius::default(),
3023 border: Border::default(),
3024 });
3025 }
3026 }
3027 display_list.push(DrawCommand::TextRun {
3028 rect: run_rect,
3029 text: rendered_text,
3030 style: text_style,
3031 });
3032 if show_caret {
3033 let caret_h: f32 = (text_style.get_font_size() * 1.15)
3034 .min(shifted_content.get_height().max(12.0));
3035 display_list.push(DrawCommand::TextCaret {
3036 text_rect,
3037 content_rect: shifted_content,
3038 text: caret_text,
3039 style: text_style,
3040 caret_height: caret_h,
3041 color: style.text.get_color().multiply_alpha(opacity),
3042 multiline: input_state.get_multiline(),
3043 });
3044 }
3045 display_list.push(DrawCommand::PopClip);
3046 }
3047 NodeKind::Image(buffer) => {
3048 display_list.push(DrawCommand::Image {
3049 rect: bounds,
3050 buffer: buffer.clone(),
3051 radius: paint.get_radius(),
3052 });
3053 }
3054 NodeKind::Canvas(caching) => {
3055 if let Some(callback) = node.canvas_callback()
3056 && let Ok(mut func) = callback.try_borrow_mut()
3057 {
3058 let canvas_bw: f32 = paint.get_border().get_width();
3059 let canvas_clip_rect: Rect = if canvas_bw > 0.0 {
3060 Rect::new(
3061 bounds.get_x() + canvas_bw,
3062 bounds.get_y() + canvas_bw,
3063 (bounds.get_width() - canvas_bw * 2.0).max(0.0),
3064 (bounds.get_height() - canvas_bw * 2.0).max(0.0),
3065 )
3066 } else {
3067 bounds
3068 };
3069 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
3070 .min(bounds.get_height() * 0.5)
3071 .max(0.0);
3072 let canvas_raw_r: CornerRadius = paint.get_radius();
3073 let canvas_inner_r: CornerRadius = CornerRadius::new(
3074 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_bw).max(0.0),
3075 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_bw).max(0.0),
3076 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_bw)
3077 .max(0.0),
3078 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_bw).max(0.0),
3079 );
3080 if canvas_inner_r.max_radius() > 0.25 {
3081 display_list.push(DrawCommand::PushRoundedClip {
3082 rect: canvas_clip_rect,
3083 radius: canvas_inner_r,
3084 });
3085 } else {
3086 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
3087 }
3088 let canvas_cmd_start: usize = display_list.len();
3089 func(bounds, display_list);
3090 let canvas_cmd_end: usize = display_list.len();
3091 if opacity < 0.999 {
3092 display_list.attenuate_range_opacity(
3093 canvas_cmd_start,
3094 canvas_cmd_end,
3095 opacity,
3096 );
3097 }
3098 if *caching != CanvasCaching::RetainedStatic {
3099 let canvas_is_dynamic: bool =
3100 if let Some(cache) = scratch.fragments.as_deref_mut() {
3101 let emitted: &[DrawCommand] = &display_list.get_commands()
3102 [canvas_cmd_start..canvas_cmd_end];
3103 cache.record_canvas_output(
3104 node_id,
3105 phys_scale_milli,
3106 bounds,
3107 tree.get_revision(),
3108 emitted,
3109 )
3110 } else {
3111 true
3112 };
3113 if canvas_is_dynamic {
3114 *scratch.saw_canvas = true;
3115 }
3116 }
3117 display_list.push(DrawCommand::PopClip);
3118 } else if *caching == CanvasCaching::DynamicPerFrame {
3119 *scratch.saw_canvas = true;
3120 }
3121 }
3122 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
3123 }
3124 }
3125 let vp: Size = display_list.get_viewport();
3126 let show_scrollbar: bool = vp.get_width().min(vp.get_height()) >= 540.0;
3127 let wants_scrollbar: bool = show_scrollbar
3128 && layout.get_overflow() == Overflow::Scroll
3129 && bounds.get_height() > 24.0;
3130 let emit_clip: bool = clipped && (child_count > 0 || wants_scrollbar);
3131 let clip_push_idx: usize = display_list.len();
3132 let clip_saved_digest: (u64, u64) = display_list.snapshot_digest_state();
3133 if emit_clip {
3134 let bw: f32 = paint.get_border().get_width();
3135 let clip_rect: Rect = if bw > 0.0 {
3136 Rect::new(
3137 bounds.get_x() + bw,
3138 bounds.get_y() + bw,
3139 (bounds.get_width() - bw * 2.0).max(0.0),
3140 (bounds.get_height() - bw * 2.0).max(0.0),
3141 )
3142 } else {
3143 bounds
3144 };
3145 let max_r: f32 = (bounds.get_width() * 0.5)
3146 .min(bounds.get_height() * 0.5)
3147 .max(0.0);
3148 let raw_r: CornerRadius = paint.get_radius();
3149 let inner_r: CornerRadius = CornerRadius::new(
3150 (raw_r.get_top_left().min(max_r) - bw).max(0.0),
3151 (raw_r.get_top_right().min(max_r) - bw).max(0.0),
3152 (raw_r.get_bottom_right().min(max_r) - bw).max(0.0),
3153 (raw_r.get_bottom_left().min(max_r) - bw).max(0.0),
3154 );
3155 if inner_r.max_radius() > 0.25 {
3156 display_list.push(DrawCommand::PushRoundedClip {
3157 rect: clip_rect,
3158 radius: inner_r,
3159 });
3160 } else {
3161 display_list.push(DrawCommand::PushClip(clip_rect));
3162 }
3163 }
3164 if child_count > 0 {
3165 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
3166 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
3167 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
3168 let child_offset_phys: Point =
3169 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
3170 let needs_z_sort: bool = child_count > 1 && tree.has_nonzero_z_child(node_id);
3171 if !needs_z_sort {
3172 for child_id in tree.children_iter(node_id) {
3173 Self::emit_node(
3174 tree,
3175 child_id,
3176 (child_offset_phys, opacity),
3177 next_cull,
3178 active_hover_target,
3179 scratch,
3180 display_list,
3181 );
3182 }
3183 } else {
3184 let sort_start: usize = scratch.z_sort.len();
3185 for (idx, cid) in tree.children_iter(node_id).enumerate() {
3186 if let Some(c) = tree.get_node(cid) {
3187 scratch
3188 .z_sort
3189 .push((c.style_ref().layout.get_z_index(), idx, cid));
3190 }
3191 }
3192 let sort_end: usize = scratch.z_sort.len();
3193 scratch.z_sort[sort_start..sort_end].sort_by(
3194 |a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
3195 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
3196 },
3197 );
3198 for slot_idx in sort_start..sort_end {
3199 let child_id: NodeId = scratch.z_sort[slot_idx].2;
3200 Self::emit_node(
3201 tree,
3202 child_id,
3203 (child_offset_phys, opacity),
3204 next_cull,
3205 active_hover_target,
3206 scratch,
3207 display_list,
3208 );
3209 }
3210 scratch.z_sort.truncate(sort_start);
3211 }
3212 }
3213 if wants_scrollbar {
3214 let lbox: &LayoutBox = &node.layout_box;
3215 let border_w: f32 = paint.get_border().get_width();
3216 let inner_x: f32 = bounds.get_x() + border_w;
3217 let inner_y: f32 = bounds.get_y() + border_w;
3218 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
3219 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
3220 let view_w: f32 = lbox.get_content_bounds().get_width().max(1.0);
3221 let view_h: f32 = lbox.get_content_bounds().get_height().max(1.0);
3222 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
3223 Some((vy, vh)) => (vy, vh),
3224 None => (
3225 lbox.get_scroll_offset().get_y(),
3226 lbox.get_content_size().get_height(),
3227 ),
3228 };
3229 let scroll_x: f32 = lbox.get_scroll_offset().get_x();
3230 let total_w: f32 = lbox.get_content_size().get_width();
3231 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
3232 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
3233 let has_v_bar: bool = max_scroll_y > 1.0;
3234 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
3235 let corner_inset: f32 = if has_v_bar && has_h_bar { 6.0 } else { 0.0 };
3236 if has_v_bar {
3237 let track_margin: f32 = 3.0;
3238 let edge_inset: f32 = 1.0;
3239 let thumb_w: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3240 let track_x: f32 = (inner_x + inner_w - thumb_w - edge_inset).max(inner_x);
3241 let track_y: f32 = inner_y + track_margin;
3242 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
3243 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
3244 let min_thumb_h: f32 = 20.0_f32.min(track_h);
3245 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
3246 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
3247 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
3248 let thumb_x_snapped: f32 = (track_x * scale).round() * inv_scale;
3249 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3250 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
3251 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3252 let thumb_alpha: u8 = if pressed {
3253 188
3254 } else if hovered {
3255 148
3256 } else {
3257 88
3258 };
3259 display_list.push(DrawCommand::FillRoundedRect {
3260 rect: Rect::new(
3261 thumb_x_snapped,
3262 thumb_y_snapped,
3263 thumb_w_snapped,
3264 thumb_h_snapped,
3265 ),
3266 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3267 radius: CornerRadius::all(thumb_w * 0.5),
3268 border: Border::default(),
3269 });
3270 }
3271 if has_h_bar {
3272 let track_margin: f32 = 3.0;
3273 let edge_inset: f32 = 1.0;
3274 let thumb_h: f32 = if hovered || pressed { 4.5 } else { 3.0 };
3275 let track_y: f32 = (inner_y + inner_h - thumb_h - edge_inset).max(inner_y);
3276 let track_x: f32 = inner_x + track_margin;
3277 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
3278 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
3279 let min_thumb_w: f32 = 20.0_f32.min(track_w);
3280 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
3281 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
3282 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
3283 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
3284 let thumb_y_snapped: f32 = (track_y * scale).round() * inv_scale;
3285 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
3286 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
3287 let thumb_alpha: u8 = if pressed {
3288 188
3289 } else if hovered {
3290 148
3291 } else {
3292 88
3293 };
3294 display_list.push(DrawCommand::FillRoundedRect {
3295 rect: Rect::new(
3296 thumb_x_snapped,
3297 thumb_y_snapped,
3298 thumb_w_snapped,
3299 thumb_h_snapped,
3300 ),
3301 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
3302 radius: CornerRadius::all(thumb_h * 0.5),
3303 border: Border::default(),
3304 });
3305 }
3306 }
3307 if emit_clip {
3308 if display_list.len() == clip_push_idx + 1 {
3309 display_list.pop_empty_clip(clip_saved_digest);
3310 } else {
3311 display_list.push(DrawCommand::PopClip);
3312 }
3313 }
3314 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
3315 && let Some(cache) = scratch.fragments.as_deref_mut()
3316 {
3317 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
3318 cache.capture_fragment(
3319 slot_idx,
3320 (
3321 node_id,
3322 phys_scale_milli,
3323 parent_opacity.to_bits(),
3324 offset_phys,
3325 geom_hash,
3326 style_text_hash,
3327 ),
3328 cmd_slice,
3329 );
3330 }
3331 }
3332}