Skip to main content

gpui/
backdrop_cache.rs

1//! Conservative reuse of framebuffer captures, shared by all SDR backends.
2use crate::*;
3use std::sync::Arc;
4
5/// Exact scene dependencies for a retained backdrop result.
6#[derive(Clone, Debug, PartialEq)]
7pub struct BackdropCacheKey {
8    /// Viewport changes invalidate even when the crop is unchanged.
9    pub viewport: Size<DevicePixels>,
10    /// Full-resolution crop origin and extent.
11    pub origin: [u32; 2],
12    /// Full-resolution crop extent.
13    pub size: [u32; 2],
14    /// Filter parameters; composition-only properties are deliberately excluded.
15    pub filter: [[[f32; 4]; 6]; 2],
16    /// Exact immutable identities and draw orders of all preceding paint commands.
17    pub revision: Arc<[(u64, DrawOrder)]>,
18    /// External content and oversized prefixes must refresh every frame.
19    pub reusable: bool,
20}
21/// Cumulative statistics exposed by headless renderers.
22#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
23pub struct BackdropCacheStats {
24    /// Source captures and two-pass filters executed.
25    pub captures: u64,
26    /// Source capture and filter passes skipped.
27    pub hits: u64,
28}
29/// Bounded cache bookkeeping; GPU textures remain owned by each backend.
30#[derive(Default)]
31pub struct BackdropCache {
32    current: Vec<BackdropCacheKey>,
33    retained: Vec<Option<BackdropCacheKey>>,
34    hits: Vec<bool>,
35    context: Vec<u64>,
36    /// Cumulative successfully encoded frame statistics.
37    pub stats: BackdropCacheStats,
38}
39impl BackdropCache {
40    /// Prepare exact keys and a backend fingerprint of clear color/render settings.
41    pub fn prepare(&mut self, keys: Vec<BackdropCacheKey>, context: &[u64]) {
42        if self.context != context {
43            self.retained.clear();
44            self.context = context.to_vec();
45        }
46        self.retained.resize_with(keys.len(), || None);
47        self.hits = keys
48            .iter()
49            .zip(&self.retained)
50            .map(|(key, old)| key.reusable && old.as_ref() == Some(key))
51            .collect();
52        self.current = keys;
53    }
54    /// Invalidate a newly allocated/replaced texture slot.
55    pub fn invalidate(&mut self, index: usize) {
56        if let Some(old) = self.retained.get_mut(index) {
57            *old = None;
58        }
59        if let Some(hit) = self.hits.get_mut(index) {
60            *hit = false;
61        }
62    }
63    /// Whether the source capture and filter passes can be skipped.
64    pub fn hit(&self, index: usize) -> bool {
65        self.hits[index]
66    }
67    /// Clear the old key before overwriting its pixels, including on failed frames.
68    pub fn start(&mut self, index: usize) {
69        if !self.hits[index] {
70            self.retained[index] = None;
71        }
72    }
73    /// Commit only boundaries encoded by a successfully encoded frame.
74    pub fn commit(&mut self, executed: &[usize]) {
75        for &index in executed {
76            if self.hits[index] {
77                self.stats.hits += 1;
78            } else {
79                self.stats.captures += 1;
80                self.retained[index] = self.current[index]
81                    .reusable
82                    .then(|| self.current[index].clone());
83            }
84        }
85    }
86}