1use std::cell::{Cell, RefCell};
2
3use cranpose_core::NodeId;
4use cranpose_render_common::raster_cache::{
5 LAYER_RASTER_CACHE_KIND_COUNT, LAYER_RASTER_CACHE_KIND_LABELS, LayerRasterCacheKey,
6};
7use cranpose_ui_graphics::Rect;
8
9use crate::{debug_toggles::DebugToggle, frame_graph::FrameCommandStats, run_geometry::ShapeFill};
10
11macro_rules! stats_line {
14 ($($arg:tt)*) => {{
15 #[cfg(target_os = "android")]
16 log::info!($($arg)*);
17 #[cfg(not(target_os = "android"))]
18 eprintln!($($arg)*);
19 }};
20}
21
22const TOP_ISOLATED_LAYER_LIMIT: usize = 8;
23
24#[derive(Clone, Copy, Debug, PartialEq)]
26pub struct IsolatedLayerStat {
27 pub node_id: Option<NodeId>,
29 pub logical_rect: Rect,
31 pub width: u32,
33 pub height: u32,
35}
36
37impl IsolatedLayerStat {
38 fn pixel_area(self) -> u64 {
39 (self.width as u64) * (self.height as u64)
40 }
41}
42
43impl Default for IsolatedLayerStat {
44 fn default() -> Self {
45 Self {
46 node_id: None,
47 logical_rect: Rect {
48 x: 0.0,
49 y: 0.0,
50 width: 0.0,
51 height: 0.0,
52 },
53 width: 0,
54 height: 0,
55 }
56 }
57}
58
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
62pub enum FrameDamage {
63 #[default]
66 Untracked,
67 Skipped,
70 Full,
73 Part(WindowRects),
75}
76
77#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
80pub struct WindowRects {
81 rects: [[u32; 4]; crate::frame::MAX_DAMAGE_RECTS],
82 len: usize,
83}
84
85impl WindowRects {
86 pub fn rects(&self) -> &[[u32; 4]] {
88 &self.rects[..self.len]
89 }
90
91 pub fn contains(&self, x: u32, y: u32) -> bool {
93 self.rects().iter().any(|&[left, top, width, height]| {
94 x >= left && y >= top && x < left + width && y < top + height
95 })
96 }
97
98 pub fn pixels(&self) -> u64 {
100 self.rects()
101 .iter()
102 .map(|&[_, _, width, height]| u64::from(width) * u64::from(height))
103 .sum()
104 }
105
106 pub(crate) fn push(&mut self, rect: [u32; 4]) {
109 if let Some(slot) = self.rects.get_mut(self.len) {
110 *slot = rect;
111 self.len += 1;
112 }
113 }
114}
115
116#[derive(Clone, Copy, Debug, Default, PartialEq)]
119pub struct FrameStatsSnapshot {
120 pub submits: u32,
122 pub encoder_count: u32,
124 pub submit_count: u32,
126 pub pass_count: u32,
128 pub pass_pixels: u64,
131 pub copy_count: u32,
133 pub copy_pixels: u64,
135 pub offscreen_acquires: u32,
137 pub offscreen_news: u32,
139 pub offscreen_total_bytes: u64,
141 pub transient_texture_bytes: u64,
143 pub retained_texture_bytes: u64,
145 pub upload_bytes: u64,
147 pub upload_writes: u32,
150 pub isolated_layer_renders: u32,
152 pub isolated_layer_pixels: u64,
154 pub layer_cache_hits: u32,
156 pub capture_fixup_passes: u32,
158 pub layer_cache_misses: u32,
160 pub layer_cache_hit_pixels: u64,
162 pub layer_cache_miss_pixels: u64,
164 pub layer_cache_hits_by_kind: [u32; LAYER_RASTER_CACHE_KIND_COUNT],
166 pub layer_cache_misses_by_kind: [u32; LAYER_RASTER_CACHE_KIND_COUNT],
168 pub layer_cache_miss_pixels_by_kind: [u64; LAYER_RASTER_CACHE_KIND_COUNT],
170 pub shadow_shape_cache_hits: u32,
172 pub shadow_shape_cache_misses: u32,
174 pub shadow_shape_cache_hit_pixels: u64,
176 pub shadow_shape_cache_miss_pixels: u64,
178 pub shadow_text_blur_fallbacks: u32,
181 pub blur_passes: u32,
183 pub stages: u32,
186 pub backdrop_admissions: u32,
189 pub prefix_admissions: u32,
191 pub substrates: u32,
194 pub composite_passes: u32,
198 pub effect_applies: u32,
201 pub shader_pixels: u64,
204 pub glass_rasterized_pixels: u64,
207 pub blur_pixels: u64,
211 pub shape_fill_pixels: u64,
215 pub shape_fill_pixels_by_class: [u64; ShapeFill::CLASSES],
218 pub shape_vertices: u64,
222 pub shape_passes: u32,
224 pub shape_pipeline_fallback_draws: u32,
227 pub shape_specialized_draws: u32,
229 pub shape_interior_draws: u32,
231 pub depth_passes: u32,
233 pub pipelines_created: u64,
237 pub shader_pipeline_fallback_draws: u32,
240 pub shader_specialized_draws: u32,
242 pub placeholder_draws: u32,
245 pub image_passes: u32,
247 pub text_passes: u32,
249 pub draw_calls: u32,
256 pub damage: FrameDamage,
259 pub repainted_pixels: u64,
262 pub text_image_cache_hits: u32,
264 pub text_image_cache_misses: u32,
266 pub text_image_cache_hit_pixels: u64,
268 pub text_image_cache_miss_pixels: u64,
270 pub text_image_raster_bytes: u64,
272 pub text_glyph_atlas_hits: u32,
274 pub text_glyph_atlas_misses: u32,
276 pub text_glyph_atlas_miss_pixels: u64,
278 pub text_glyph_runs_collected: u32,
281 pub offscreen_pool_size: u32,
283 pub offscreen_pool_bytes: u64,
285 pub text_pool_size: u32,
287 pub layer_cache_size: u32,
289 pub layer_cache_bytes: u64,
291 pub image_cache_size: u32,
293 pub top_isolated_layers: [Option<IsolatedLayerStat>; TOP_ISOLATED_LAYER_LIMIT],
296 pub top_isolated_layer_count: usize,
298}
299
300impl FrameStatsSnapshot {
301 fn shape_fill_by_class_text(&self) -> String {
302 let mut text = String::new();
303 for (label, pixels) in ShapeFill::LABELS
304 .iter()
305 .zip(self.shape_fill_pixels_by_class)
306 {
307 if pixels > 0 {
308 use std::fmt::Write;
309 let _ = write!(text, " {label}={:.2}MP", pixels as f64 / 1_000_000.0);
310 }
311 }
312 text
313 }
314
315 pub(crate) fn with_command_stats_added(mut self, stats: FrameCommandStats) -> Self {
316 self.submits = self.submits.saturating_add(stats.submit_count);
317 self.encoder_count = self.encoder_count.saturating_add(stats.encoder_count);
318 self.submit_count = self.submit_count.saturating_add(stats.submit_count);
319 self.pass_count = self.pass_count.saturating_add(stats.pass_count);
320 self.pass_pixels = self.pass_pixels.saturating_add(stats.pass_pixels);
321 self.copy_count = self.copy_count.saturating_add(stats.copy_count);
322 self.copy_pixels = self.copy_pixels.saturating_add(stats.copy_pixels);
323 self.transient_texture_bytes = self
324 .transient_texture_bytes
325 .saturating_add(stats.transient_texture_bytes);
326 self.retained_texture_bytes = self
327 .retained_texture_bytes
328 .max(stats.retained_texture_bytes);
329 self.upload_bytes = self.upload_bytes.saturating_add(stats.upload_bytes);
330 self.upload_writes = self.upload_writes.saturating_add(stats.upload_writes);
331 self.offscreen_acquires = self
332 .offscreen_acquires
333 .saturating_add(stats.transient_acquires);
334 self.offscreen_news = self.offscreen_news.saturating_add(stats.transient_news);
335 self
336 }
337
338 fn top_isolated_display(&self) -> String {
341 match &self.top_isolated_layers[0] {
342 Some(top) => format!(
343 "{}x{}@({:.0},{:.0})",
344 top.width, top.height, top.logical_rect.x, top.logical_rect.y
345 ),
346 None => "-".to_string(),
347 }
348 }
349
350 pub fn top_isolated_layers(self) -> impl Iterator<Item = IsolatedLayerStat> {
352 self.top_isolated_layers
353 .into_iter()
354 .flatten()
355 .take(self.top_isolated_layer_count)
356 }
357
358 fn layer_cache_hit_rate(self) -> f64 {
359 let total = self.layer_cache_hits + self.layer_cache_misses;
360 if total > 0 {
361 (self.layer_cache_hits as f64 / total as f64) * 100.0
362 } else {
363 0.0
364 }
365 }
366
367 fn miss_pixels_by_kind_display(&self) -> String {
368 by_kind_display(&self.layer_cache_miss_pixels_by_kind, |pixels| {
369 format!("{:.2}MP", pixels as f64 / 1_000_000.0)
370 })
371 }
372
373 fn hits_by_kind_display(&self) -> String {
374 by_kind_display(&self.layer_cache_hits_by_kind, |hits| hits.to_string())
375 }
376
377 fn print(self, frame_count: u64) {
378 let mb = self.offscreen_total_bytes as f64 / (1024.0 * 1024.0);
379 let upload_mb = self.upload_bytes as f64 / (1024.0 * 1024.0);
380 let retained_mb = self.retained_texture_bytes as f64 / (1024.0 * 1024.0);
381 let pool_mb = self.offscreen_pool_bytes as f64 / (1024.0 * 1024.0);
382 let layer_cache_hit_mpx = self.layer_cache_hit_pixels as f64 / 1_000_000.0;
383 let layer_cache_miss_mpx = self.layer_cache_miss_pixels as f64 / 1_000_000.0;
384 let shadow_cache_hit_mpx = self.shadow_shape_cache_hit_pixels as f64 / 1_000_000.0;
385 let shadow_cache_miss_mpx = self.shadow_shape_cache_miss_pixels as f64 / 1_000_000.0;
386 let layer_cache_mb = self.layer_cache_bytes as f64 / (1024.0 * 1024.0);
387 let isolated_layer_mpx = self.isolated_layer_pixels as f64 / 1_000_000.0;
388 stats_line!(
389 "[GPU f#{}] encoders={} submits={} passes={} pass_px={:.2}MP copies={} copy_px={:.2}MP | offscreen: acq={} new={} {:.1}MB pool={}({:.1}MB) retained={:.1}MB | \
390 uploads={:.2}MB writes={} | \
391 isolated_layers={} area={:.2}MP top={} | \
392 layer_cache: hit={} miss={} {:.1}% hit_px={:.2}MP miss_px={:.2}MP size={}({:.1}MB) hit_by_kind={} miss_px_by_kind={} | \
393 shadow_cache: shape_hit={} shape_miss={} hit_px={:.2}MP miss_px={:.2}MP text_blur_fallback={} | \
394 stages={} admit={} blur={} substrate={} composite={} effect={} shader_px={:.2}MP glass_raster_px={:.2}MP blur_px={:.2}MP | shape={} shape_fill_px={:.2}MP{} shape_verts={} interiors={} depth_passes={} image={} text={} draws={} | \
395 text_img_cache: hit={} miss={} hit_px={:.2}MP miss_px={:.2}MP raster={:.2}MB | \
396 text_glyph_atlas: hit={} miss={} miss_px={:.2}MP runs_collected={} | \
397 caches: text_pool={} img={}",
398 frame_count,
399 self.encoder_count,
400 self.submit_count,
401 self.pass_count,
402 self.pass_pixels as f64 / 1_000_000.0,
403 self.copy_count,
404 self.copy_pixels as f64 / 1_000_000.0,
405 self.offscreen_acquires,
406 self.offscreen_news,
407 mb,
408 self.offscreen_pool_size,
409 pool_mb,
410 retained_mb,
411 upload_mb,
412 self.upload_writes,
413 self.isolated_layer_renders,
414 isolated_layer_mpx,
415 self.top_isolated_display(),
416 self.layer_cache_hits,
417 self.layer_cache_misses,
418 self.layer_cache_hit_rate(),
419 layer_cache_hit_mpx,
420 layer_cache_miss_mpx,
421 self.layer_cache_size,
422 layer_cache_mb,
423 self.hits_by_kind_display(),
424 self.miss_pixels_by_kind_display(),
425 self.shadow_shape_cache_hits,
426 self.shadow_shape_cache_misses,
427 shadow_cache_hit_mpx,
428 shadow_cache_miss_mpx,
429 self.shadow_text_blur_fallbacks,
430 self.stages,
431 self.backdrop_admissions,
432 self.blur_passes,
433 self.substrates,
434 self.composite_passes,
435 self.effect_applies,
436 self.shader_pixels as f64 / 1_000_000.0,
437 self.glass_rasterized_pixels as f64 / 1_000_000.0,
438 self.blur_pixels as f64 / 1_000_000.0,
439 self.shape_passes,
440 self.shape_fill_pixels as f64 / 1_000_000.0,
441 self.shape_fill_by_class_text(),
442 self.shape_vertices,
443 self.shape_interior_draws,
444 self.depth_passes,
445 self.image_passes,
446 self.text_passes,
447 self.draw_calls,
448 self.text_image_cache_hits,
449 self.text_image_cache_misses,
450 self.text_image_cache_hit_pixels as f64 / 1_000_000.0,
451 self.text_image_cache_miss_pixels as f64 / 1_000_000.0,
452 self.text_image_raster_bytes as f64 / (1024.0 * 1024.0),
453 self.text_glyph_atlas_hits,
454 self.text_glyph_atlas_misses,
455 self.text_glyph_atlas_miss_pixels as f64 / 1_000_000.0,
456 self.text_glyph_runs_collected,
457 self.text_pool_size,
458 self.image_cache_size,
459 );
460 for (index, layer) in self.top_isolated_layers().enumerate() {
461 stats_line!(
462 " [isolated #{index}] node={:?} rect=({:.1},{:.1},{:.1},{:.1}) target={}x{}",
463 layer.node_id,
464 layer.logical_rect.x,
465 layer.logical_rect.y,
466 layer.logical_rect.width,
467 layer.logical_rect.height,
468 layer.width,
469 layer.height,
470 );
471 }
472 }
473}
474
475#[derive(Default)]
476pub(crate) struct FrameStats {
477 pub submits: Cell<u32>,
478 pub command_encoder_count: Cell<u32>,
479 pub command_submit_count: Cell<u32>,
480 pub command_pass_count: Cell<u32>,
481 pub command_pass_pixels: Cell<u64>,
482 pub command_copy_count: Cell<u32>,
483 pub command_copy_pixels: Cell<u64>,
484 pub command_transient_texture_bytes: Cell<u64>,
485 pub command_upload_bytes: Cell<u64>,
486 pub offscreen_acquires: Cell<u32>,
487 pub offscreen_news: Cell<u32>,
488 pub offscreen_total_bytes: Cell<u64>,
489 pub upload_writes: Cell<u32>,
490 pub isolated_layer_renders: Cell<u32>,
491 pub isolated_layer_pixels: Cell<u64>,
492 pub layer_cache_hits: Cell<u32>,
493 pub capture_fixup_passes: Cell<u32>,
494 pub layer_cache_misses: Cell<u32>,
495 pub layer_cache_hit_pixels: Cell<u64>,
496 pub layer_cache_miss_pixels: Cell<u64>,
497 pub layer_cache_hits_by_kind: [Cell<u32>; LAYER_RASTER_CACHE_KIND_COUNT],
498 pub layer_cache_misses_by_kind: [Cell<u32>; LAYER_RASTER_CACHE_KIND_COUNT],
499 pub layer_cache_miss_pixels_by_kind: [Cell<u64>; LAYER_RASTER_CACHE_KIND_COUNT],
500 pub shadow_shape_cache_hits: Cell<u32>,
501 pub shadow_shape_cache_misses: Cell<u32>,
502 pub shadow_shape_cache_hit_pixels: Cell<u64>,
503 pub shadow_shape_cache_miss_pixels: Cell<u64>,
504 pub shadow_text_blur_fallbacks: Cell<u32>,
505 pub blur_passes: Cell<u32>,
506 pub stages: Cell<u32>,
507 pub backdrop_admissions: Cell<u32>,
508 pub prefix_admissions: Cell<u32>,
509 pub substrates: Cell<u32>,
510 pub composite_passes: Cell<u32>,
511 pub effect_applies: Cell<u32>,
512 pub shader_pixels: Cell<u64>,
513 pub glass_rasterized_pixels: Cell<u64>,
514 pub blur_pixels: Cell<u64>,
515 pub shape_fill_pixels: Cell<u64>,
516 pub shape_fill_pixels_by_class: Cell<[u64; ShapeFill::CLASSES]>,
517 pub shape_vertices: Cell<u64>,
518 pub shape_passes: Cell<u32>,
519 pub shape_pipeline_fallback_draws: Cell<u32>,
520 pub shape_specialized_draws: Cell<u32>,
521 pub shape_interior_draws: Cell<u32>,
522 pub depth_passes: Cell<u32>,
523 pub shader_pipeline_fallback_draws: Cell<u32>,
524 pub shader_specialized_draws: Cell<u32>,
525 pub placeholder_draws: Cell<u32>,
526 pub image_passes: Cell<u32>,
527 pub text_passes: Cell<u32>,
528 pub draw_calls: Cell<u32>,
529 pub damage: Cell<FrameDamage>,
530 pub repainted_pixels: Cell<u64>,
531 pub text_image_cache_hits: Cell<u32>,
532 pub text_image_cache_misses: Cell<u32>,
533 pub text_image_cache_hit_pixels: Cell<u64>,
534 pub text_image_cache_miss_pixels: Cell<u64>,
535 pub text_image_raster_bytes: Cell<u64>,
536 pub text_glyph_atlas_hits: Cell<u32>,
537 pub text_glyph_atlas_misses: Cell<u32>,
538 pub text_glyph_atlas_miss_pixels: Cell<u64>,
539 pub text_glyph_runs_collected: Cell<u32>,
540 pub offscreen_pool_size: Cell<u32>,
541 pub offscreen_pool_bytes: Cell<u64>,
542 pub text_pool_size: Cell<u32>,
543 pub layer_cache_size: Cell<u32>,
544 pub layer_cache_bytes: Cell<u64>,
545 pub image_cache_size: Cell<u32>,
546 top_isolated_layers: RefCell<[Option<IsolatedLayerStat>; TOP_ISOLATED_LAYER_LIMIT]>,
547 top_isolated_layer_count: Cell<usize>,
548 shadow_shape_cache_miss_log_count: Cell<u32>,
549}
550
551impl FrameStats {
552 pub(crate) fn drew_stand_in(&self) -> bool {
556 self.placeholder_draws.get() != 0
557 || self.shape_pipeline_fallback_draws.get() != 0
558 || self.shader_pipeline_fallback_draws.get() != 0
559 }
560
561 pub fn record_capture_fixup_pass(&self) {
562 self.capture_fixup_passes
563 .set(self.capture_fixup_passes.get().saturating_add(1));
564 }
565
566 pub fn record_stages(&self, count: u32) {
567 self.stages.set(self.stages.get().saturating_add(count));
568 }
569
570 pub fn record_backdrop_admission(&self) {
571 self.backdrop_admissions
572 .set(self.backdrop_admissions.get().saturating_add(1));
573 }
574
575 pub fn record_prefix_admission(&self) {
576 self.prefix_admissions
577 .set(self.prefix_admissions.get().saturating_add(1));
578 }
579
580 pub fn record_command_stats(&self, stats: FrameCommandStats) {
581 self.submits
582 .set(self.submits.get().saturating_add(stats.submit_count));
583 self.command_encoder_count.set(
584 self.command_encoder_count
585 .get()
586 .saturating_add(stats.encoder_count),
587 );
588 self.command_submit_count.set(
589 self.command_submit_count
590 .get()
591 .saturating_add(stats.submit_count),
592 );
593 self.command_pass_count.set(
594 self.command_pass_count
595 .get()
596 .saturating_add(stats.pass_count),
597 );
598 self.command_pass_pixels.set(
599 self.command_pass_pixels
600 .get()
601 .saturating_add(stats.pass_pixels),
602 );
603 self.command_copy_count.set(
604 self.command_copy_count
605 .get()
606 .saturating_add(stats.copy_count),
607 );
608 self.command_copy_pixels.set(
609 self.command_copy_pixels
610 .get()
611 .saturating_add(stats.copy_pixels),
612 );
613 self.command_transient_texture_bytes.set(
614 self.command_transient_texture_bytes
615 .get()
616 .saturating_add(stats.transient_texture_bytes),
617 );
618 self.command_upload_bytes.set(
619 self.command_upload_bytes
620 .get()
621 .saturating_add(stats.upload_bytes),
622 );
623 self.upload_writes
624 .set(self.upload_writes.get().saturating_add(stats.upload_writes));
625 self.offscreen_acquires.set(
626 self.offscreen_acquires
627 .get()
628 .saturating_add(stats.transient_acquires),
629 );
630 self.offscreen_news.set(
631 self.offscreen_news
632 .get()
633 .saturating_add(stats.transient_news),
634 );
635 }
636
637 pub fn record_offscreen_acquire(
638 &self,
639 width: u32,
640 height: u32,
641 format: wgpu::TextureFormat,
642 is_new: bool,
643 ) {
644 self.offscreen_acquires
645 .set(self.offscreen_acquires.get() + 1);
646 if is_new {
647 self.offscreen_news.set(self.offscreen_news.get() + 1);
648 }
649 self.offscreen_total_bytes.set(
650 self.offscreen_total_bytes.get()
651 + (width as u64)
652 * (height as u64)
653 * crate::frame_graph::texture_format_bytes_per_pixel(format),
654 );
655 }
656
657 pub fn record_isolated_layer_render(
658 &self,
659 width: u32,
660 height: u32,
661 node_id: Option<NodeId>,
662 logical_rect: Rect,
663 ) {
664 self.isolated_layer_renders
665 .set(self.isolated_layer_renders.get().saturating_add(1));
666 self.isolated_layer_pixels.set(
667 self.isolated_layer_pixels
668 .get()
669 .saturating_add((width as u64) * (height as u64)),
670 );
671 self.record_top_isolated_layer(IsolatedLayerStat {
672 node_id,
673 logical_rect,
674 width,
675 height,
676 });
677 }
678
679 pub fn record_layer_cache_hit(&self, key: &LayerRasterCacheKey, width: u32, height: u32) {
680 let by_kind = &self.layer_cache_hits_by_kind[key.kind_slot()];
681 by_kind.set(by_kind.get().saturating_add(1));
682 self.layer_cache_hits
683 .set(self.layer_cache_hits.get().saturating_add(1));
684 self.layer_cache_hit_pixels.set(
685 self.layer_cache_hit_pixels
686 .get()
687 .saturating_add((width as u64) * (height as u64)),
688 );
689 }
690
691 pub fn record_layer_cache_miss(&self, key: &LayerRasterCacheKey, width: u32, height: u32) {
692 let slot = key.kind_slot();
693 let by_kind = &self.layer_cache_misses_by_kind[slot];
694 by_kind.set(by_kind.get().saturating_add(1));
695 let pixels_by_kind = &self.layer_cache_miss_pixels_by_kind[slot];
696 pixels_by_kind.set(
697 pixels_by_kind
698 .get()
699 .saturating_add((width as u64) * (height as u64)),
700 );
701 self.layer_cache_misses
702 .set(self.layer_cache_misses.get().saturating_add(1));
703 self.layer_cache_miss_pixels.set(
704 self.layer_cache_miss_pixels
705 .get()
706 .saturating_add((width as u64) * (height as u64)),
707 );
708 }
709
710 pub fn record_shadow_shape_cache_hit(&self, composited_pixels: u64) {
711 self.shadow_shape_cache_hits
712 .set(self.shadow_shape_cache_hits.get().saturating_add(1));
713 self.shadow_shape_cache_hit_pixels.set(
714 self.shadow_shape_cache_hit_pixels
715 .get()
716 .saturating_add(composited_pixels),
717 );
718 }
719
720 pub fn record_shadow_shape_cache_miss(&self, width: u32, height: u32) {
721 self.shadow_shape_cache_misses
722 .set(self.shadow_shape_cache_misses.get().saturating_add(1));
723 self.shadow_shape_cache_miss_pixels.set(
724 self.shadow_shape_cache_miss_pixels
725 .get()
726 .saturating_add((width as u64) * (height as u64)),
727 );
728 }
729
730 #[expect(clippy::too_many_arguments)]
731 pub fn maybe_print_shadow_shape_cache_miss(
732 &self,
733 width: u32,
734 height: u32,
735 content_hash: u64,
736 blur_radius: f32,
737 viewport_offset: [f32; 2],
738 shape_count: usize,
739 clip: Option<Rect>,
740 ) {
741 if !shadow_cache_diagnostics_enabled() {
742 return;
743 }
744
745 let count = self.shadow_shape_cache_miss_log_count.get();
746 if count >= 16 {
747 return;
748 }
749 self.shadow_shape_cache_miss_log_count.set(count + 1);
750
751 let clip_text = clip.map_or_else(
752 || "none".to_string(),
753 |clip| {
754 format!(
755 "({:.1},{:.1},{:.1},{:.1})",
756 clip.x, clip.y, clip.width, clip.height
757 )
758 },
759 );
760 stats_line!(
761 "[shadow-cache-miss #{count}] size={}x{} content_hash={content_hash} blur={:.2} viewport_offset=({:.1},{:.1}) shapes={} clip={}",
762 width,
763 height,
764 blur_radius,
765 viewport_offset[0],
766 viewport_offset[1],
767 shape_count,
768 clip_text,
769 );
770 }
771
772 pub fn record_shadow_text_blur_fallback(&self) {
773 self.shadow_text_blur_fallbacks
774 .set(self.shadow_text_blur_fallbacks.get().saturating_add(1));
775 }
776
777 pub fn add_shape_fill(&self, fill: ShapeFill) {
778 self.shape_fill_pixels.set(
779 self.shape_fill_pixels
780 .get()
781 .saturating_add(fill.total().round() as u64),
782 );
783 let mut by_class = self.shape_fill_pixels_by_class.get();
784 for (total, pixels) in by_class.iter_mut().zip(fill.pixels) {
785 *total = total.saturating_add(pixels.round() as u64);
786 }
787 self.shape_fill_pixels_by_class.set(by_class);
788 self.shape_vertices
789 .set(self.shape_vertices.get().saturating_add(fill.vertices));
790 }
791
792 pub fn bump_shapes(&self) {
793 self.shape_passes.set(self.shape_passes.get() + 1);
794 }
795
796 pub fn bump_images(&self) {
797 self.image_passes.set(self.image_passes.get() + 1);
798 }
799
800 pub fn add_draw_calls(&self, count: u32) {
801 self.draw_calls
802 .set(self.draw_calls.get().saturating_add(count));
803 }
804
805 pub fn bump_text(&self) {
806 self.text_passes.set(self.text_passes.get() + 1);
807 }
808
809 pub fn record_text_image_cache_hit(&self, width: u32, height: u32) {
810 self.text_image_cache_hits
811 .set(self.text_image_cache_hits.get().saturating_add(1));
812 self.text_image_cache_hit_pixels.set(
813 self.text_image_cache_hit_pixels
814 .get()
815 .saturating_add((width as u64) * (height as u64)),
816 );
817 }
818
819 pub fn record_text_image_cache_miss(&self, width: u32, height: u32) {
820 let pixels = (width as u64) * (height as u64);
821 self.text_image_cache_misses
822 .set(self.text_image_cache_misses.get().saturating_add(1));
823 self.text_image_cache_miss_pixels.set(
824 self.text_image_cache_miss_pixels
825 .get()
826 .saturating_add(pixels),
827 );
828 self.text_image_raster_bytes.set(
829 self.text_image_raster_bytes
830 .get()
831 .saturating_add(pixels * 4),
832 );
833 }
834
835 pub fn record_text_glyph_atlas_hits(&self, count: u32) {
836 self.text_glyph_atlas_hits
837 .set(self.text_glyph_atlas_hits.get().saturating_add(count));
838 }
839
840 pub fn record_text_glyph_run_collected(&self) {
841 self.text_glyph_runs_collected
842 .set(self.text_glyph_runs_collected.get().saturating_add(1));
843 }
844
845 pub fn record_text_glyph_atlas_miss(&self, width: u32, height: u32) {
846 self.text_glyph_atlas_misses
847 .set(self.text_glyph_atlas_misses.get().saturating_add(1));
848 self.text_glyph_atlas_miss_pixels.set(
849 self.text_glyph_atlas_miss_pixels
850 .get()
851 .saturating_add((width as u64) * (height as u64)),
852 );
853 }
854
855 pub fn snapshot(&self) -> FrameStatsSnapshot {
856 let retained_texture_bytes = self
857 .offscreen_pool_bytes
858 .get()
859 .saturating_add(self.layer_cache_bytes.get());
860 FrameStatsSnapshot {
861 submits: self.submits.get(),
862 encoder_count: self.command_encoder_count.get(),
863 submit_count: self.command_submit_count.get(),
864 pass_count: self.command_pass_count.get(),
865 pass_pixels: self.command_pass_pixels.get(),
866 copy_count: self.command_copy_count.get(),
867 copy_pixels: self.command_copy_pixels.get(),
868 offscreen_acquires: self.offscreen_acquires.get(),
869 offscreen_news: self.offscreen_news.get(),
870 offscreen_total_bytes: self.offscreen_total_bytes.get(),
871 transient_texture_bytes: self
872 .offscreen_total_bytes
873 .get()
874 .saturating_add(self.command_transient_texture_bytes.get()),
875 retained_texture_bytes,
876 upload_bytes: self.command_upload_bytes.get(),
877 upload_writes: self.upload_writes.get(),
878 isolated_layer_renders: self.isolated_layer_renders.get(),
879 isolated_layer_pixels: self.isolated_layer_pixels.get(),
880 layer_cache_hits: self.layer_cache_hits.get(),
881 capture_fixup_passes: self.capture_fixup_passes.get(),
882 layer_cache_misses: self.layer_cache_misses.get(),
883 layer_cache_hit_pixels: self.layer_cache_hit_pixels.get(),
884 layer_cache_miss_pixels: self.layer_cache_miss_pixels.get(),
885 layer_cache_hits_by_kind: self.layer_cache_hits_by_kind.each_ref().map(Cell::get),
886 layer_cache_misses_by_kind: self.layer_cache_misses_by_kind.each_ref().map(Cell::get),
887 layer_cache_miss_pixels_by_kind: self
888 .layer_cache_miss_pixels_by_kind
889 .each_ref()
890 .map(Cell::get),
891 shadow_shape_cache_hits: self.shadow_shape_cache_hits.get(),
892 shadow_shape_cache_misses: self.shadow_shape_cache_misses.get(),
893 shadow_shape_cache_hit_pixels: self.shadow_shape_cache_hit_pixels.get(),
894 shadow_shape_cache_miss_pixels: self.shadow_shape_cache_miss_pixels.get(),
895 shadow_text_blur_fallbacks: self.shadow_text_blur_fallbacks.get(),
896 blur_passes: self.blur_passes.get(),
897 stages: self.stages.get(),
898 backdrop_admissions: self.backdrop_admissions.get(),
899 prefix_admissions: self.prefix_admissions.get(),
900 substrates: self.substrates.get(),
901 composite_passes: self.composite_passes.get(),
902 effect_applies: self.effect_applies.get(),
903 shader_pixels: self.shader_pixels.get(),
904 glass_rasterized_pixels: self.glass_rasterized_pixels.get(),
905 blur_pixels: self.blur_pixels.get(),
906 shape_fill_pixels: self.shape_fill_pixels.get(),
907 shape_fill_pixels_by_class: self.shape_fill_pixels_by_class.get(),
908 shape_vertices: self.shape_vertices.get(),
909 shape_passes: self.shape_passes.get(),
910 shape_pipeline_fallback_draws: self.shape_pipeline_fallback_draws.get(),
911 shape_specialized_draws: self.shape_specialized_draws.get(),
912 shape_interior_draws: self.shape_interior_draws.get(),
913 depth_passes: self.depth_passes.get(),
914 pipelines_created: crate::render::pipelines_created(),
915 shader_pipeline_fallback_draws: self.shader_pipeline_fallback_draws.get(),
916 shader_specialized_draws: self.shader_specialized_draws.get(),
917 placeholder_draws: self.placeholder_draws.get(),
918 image_passes: self.image_passes.get(),
919 text_passes: self.text_passes.get(),
920 draw_calls: self.draw_calls.get(),
921 damage: self.damage.get(),
922 repainted_pixels: self.repainted_pixels.get(),
923 text_image_cache_hits: self.text_image_cache_hits.get(),
924 text_image_cache_misses: self.text_image_cache_misses.get(),
925 text_image_cache_hit_pixels: self.text_image_cache_hit_pixels.get(),
926 text_image_cache_miss_pixels: self.text_image_cache_miss_pixels.get(),
927 text_image_raster_bytes: self.text_image_raster_bytes.get(),
928 text_glyph_atlas_hits: self.text_glyph_atlas_hits.get(),
929 text_glyph_atlas_misses: self.text_glyph_atlas_misses.get(),
930 text_glyph_atlas_miss_pixels: self.text_glyph_atlas_miss_pixels.get(),
931 text_glyph_runs_collected: self.text_glyph_runs_collected.get(),
932 offscreen_pool_size: self.offscreen_pool_size.get(),
933 offscreen_pool_bytes: self.offscreen_pool_bytes.get(),
934 text_pool_size: self.text_pool_size.get(),
935 layer_cache_size: self.layer_cache_size.get(),
936 layer_cache_bytes: self.layer_cache_bytes.get(),
937 image_cache_size: self.image_cache_size.get(),
938 top_isolated_layers: *self.top_isolated_layers.borrow(),
939 top_isolated_layer_count: self.top_isolated_layer_count.get(),
940 }
941 }
942
943 pub fn reset(&self) {
944 self.submits.set(0);
945 self.command_encoder_count.set(0);
946 self.command_submit_count.set(0);
947 self.command_pass_count.set(0);
948 self.command_pass_pixels.set(0);
949 self.command_copy_count.set(0);
950 self.command_copy_pixels.set(0);
951 self.command_transient_texture_bytes.set(0);
952 self.command_upload_bytes.set(0);
953 self.upload_writes.set(0);
954 self.offscreen_acquires.set(0);
955 self.offscreen_news.set(0);
956 self.offscreen_total_bytes.set(0);
957 self.isolated_layer_renders.set(0);
958 self.isolated_layer_pixels.set(0);
959 self.layer_cache_hits.set(0);
960 self.capture_fixup_passes.set(0);
961 self.layer_cache_misses.set(0);
962 self.layer_cache_hit_pixels.set(0);
963 self.layer_cache_miss_pixels.set(0);
964 for slot in 0..LAYER_RASTER_CACHE_KIND_COUNT {
965 self.layer_cache_hits_by_kind[slot].set(0);
966 self.layer_cache_misses_by_kind[slot].set(0);
967 self.layer_cache_miss_pixels_by_kind[slot].set(0);
968 }
969 self.shadow_shape_cache_hits.set(0);
970 self.shadow_shape_cache_misses.set(0);
971 self.shadow_shape_cache_hit_pixels.set(0);
972 self.shadow_shape_cache_miss_pixels.set(0);
973 self.shadow_text_blur_fallbacks.set(0);
974 self.blur_passes.set(0);
975 self.stages.set(0);
976 self.backdrop_admissions.set(0);
977 self.prefix_admissions.set(0);
978 self.substrates.set(0);
979 self.composite_passes.set(0);
980 self.effect_applies.set(0);
981 self.shader_pixels.set(0);
982 self.glass_rasterized_pixels.set(0);
983 self.blur_pixels.set(0);
984 self.shape_fill_pixels.set(0);
985 self.shape_fill_pixels_by_class.set([0; ShapeFill::CLASSES]);
986 self.shape_vertices.set(0);
987 self.shape_passes.set(0);
988 self.shape_pipeline_fallback_draws.set(0);
989 self.shape_specialized_draws.set(0);
990 self.shape_interior_draws.set(0);
991 self.depth_passes.set(0);
992 self.shader_pipeline_fallback_draws.set(0);
993 self.shader_specialized_draws.set(0);
994 self.placeholder_draws.set(0);
995 self.image_passes.set(0);
996 self.text_passes.set(0);
997 self.draw_calls.set(0);
998 self.damage.set(FrameDamage::default());
999 self.repainted_pixels.set(0);
1000 self.text_image_cache_hits.set(0);
1001 self.text_image_cache_misses.set(0);
1002 self.text_image_cache_hit_pixels.set(0);
1003 self.text_image_cache_miss_pixels.set(0);
1004 self.text_image_raster_bytes.set(0);
1005 self.text_glyph_atlas_hits.set(0);
1006 self.text_glyph_atlas_misses.set(0);
1007 self.text_glyph_atlas_miss_pixels.set(0);
1008 self.text_glyph_runs_collected.set(0);
1009 *self.top_isolated_layers.borrow_mut() = [None; TOP_ISOLATED_LAYER_LIMIT];
1010 self.top_isolated_layer_count.set(0);
1011 self.shadow_shape_cache_miss_log_count.set(0);
1012 }
1013
1014 pub fn maybe_print_snapshot(
1015 &self,
1016 snapshot: FrameStatsSnapshot,
1017 frame_count: &mut u64,
1018 enabled: bool,
1019 ) {
1020 if !enabled {
1021 return;
1022 }
1023 *frame_count += 1;
1024 if (*frame_count).is_multiple_of(60) {
1025 snapshot.print(*frame_count);
1026 }
1027 }
1028
1029 fn record_top_isolated_layer(&self, layer: IsolatedLayerStat) {
1030 let mut top_layers = self.top_isolated_layers.borrow_mut();
1031 let len = self.top_isolated_layer_count.get();
1032 let insert_at = top_layers[..len]
1033 .iter()
1034 .enumerate()
1035 .find_map(|(index, existing)| {
1036 existing
1037 .filter(|existing| layer.pixel_area() > existing.pixel_area())
1038 .map(|_| index)
1039 })
1040 .unwrap_or(len);
1041
1042 if insert_at >= TOP_ISOLATED_LAYER_LIMIT {
1043 return;
1044 }
1045
1046 let new_len = if len < TOP_ISOLATED_LAYER_LIMIT {
1047 len + 1
1048 } else {
1049 TOP_ISOLATED_LAYER_LIMIT
1050 };
1051
1052 let mut index = new_len.saturating_sub(1);
1053 while index > insert_at {
1054 top_layers[index] = top_layers[index - 1];
1055 index -= 1;
1056 }
1057 top_layers[insert_at] = Some(layer);
1058 self.top_isolated_layer_count.set(new_len);
1059 }
1060}
1061
1062static GPU_STATS: DebugToggle = DebugToggle::new("CRANPOSE_GPU_STATS");
1063
1064pub(crate) fn gpu_stats_enabled() -> bool {
1065 GPU_STATS.flag()
1066}
1067
1068pub(crate) fn print_gpu_memory_report(device: &wgpu::Device, frame_count: u64) {
1069 let Some(report) = device.generate_allocator_report() else {
1070 return;
1071 };
1072
1073 const MB: f64 = 1024.0 * 1024.0;
1074 let mut blocks = String::new();
1075 for block in &report.blocks {
1076 if !blocks.is_empty() {
1077 blocks.push('+');
1078 }
1079 blocks.push_str(&format!("{:.1}", block.size as f64 / MB));
1080 }
1081
1082 stats_line!(
1083 "[GPU-MEM f#{}] reserved={:.1}MB allocated={:.1}MB blocks={}[{}MB] allocations={} | largest={:.6?}",
1084 frame_count,
1085 report.total_reserved_bytes as f64 / MB,
1086 report.total_allocated_bytes as f64 / MB,
1087 report.blocks.len(),
1088 blocks,
1089 report.allocations.len(),
1090 report,
1091 );
1092}
1093
1094static SHADOW_CACHE_DIAG: DebugToggle = DebugToggle::new("CRANPOSE_GPU_SHADOW_CACHE_DIAG");
1095
1096fn shadow_cache_diagnostics_enabled() -> bool {
1097 SHADOW_CACHE_DIAG.flag()
1098}
1099
1100fn by_kind_display<T: Copy + Default + PartialEq>(
1101 by_kind: &[T; LAYER_RASTER_CACHE_KIND_COUNT],
1102 format: impl Fn(T) -> String,
1103) -> String {
1104 LAYER_RASTER_CACHE_KIND_LABELS
1105 .iter()
1106 .zip(by_kind)
1107 .filter(|(_, value)| **value != T::default())
1108 .map(|(label, value)| format!("{label}={}", format(*value)))
1109 .collect::<Vec<_>>()
1110 .join(",")
1111}
1112
1113#[cfg(test)]
1114#[path = "tests/gpu_stats_tests.rs"]
1115mod tests;