1use std::time::Duration;
2
3#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
4#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
5pub enum CacheDomain {
6 EncodedImage,
7 DecodedImage,
8 Svg,
9 Blur,
10 Text,
11 StaticLayer,
12 ScrollRaster,
13 Skia,
14 ComponentOutput,
15 HostScene,
16 Diagnostics,
17 Persistent,
18}
19
20impl CacheDomain {
21 pub const fn as_str(self) -> &'static str {
22 match self {
23 Self::EncodedImage => "encoded-image",
24 Self::DecodedImage => "decoded-image",
25 Self::Svg => "svg",
26 Self::Blur => "blur",
27 Self::Text => "text",
28 Self::StaticLayer => "static-layer",
29 Self::ScrollRaster => "scroll-raster",
30 Self::Skia => "skia",
31 Self::ComponentOutput => "component-output",
32 Self::HostScene => "host-scene",
33 Self::Diagnostics => "diagnostics",
34 Self::Persistent => "persistent",
35 }
36 }
37}
38
39#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
40#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
41pub enum ResourceClass {
42 Live,
43 Rebuildable,
44 #[default]
45 Cache,
46 Transient,
47}
48
49#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
50#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
51pub enum RetentionClass {
52 Frame,
53 WhileVisible,
54 Scene,
55 #[default]
56 Session,
57 Persistent,
58}
59
60#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
61#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
62pub enum CachePriority {
63 Low,
64 #[default]
65 Normal,
66 High,
67}
68
69#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
70#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
71pub enum ImageCachePolicy {
72 #[default]
73 ApplicationDefault,
74 NoStore,
75 WhileVisible,
76 Scene,
77 Session,
78 Persistent {
79 max_age: Duration,
80 revalidate: bool,
81 },
82}
83
84#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
85#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
86pub enum CacheScope {
87 #[default]
88 Memory,
89 Persistent,
90 AllRebuildable,
91}
92
93#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
94#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
95pub enum TrimReason {
96 #[default]
97 SoftBudget,
98 HardBudget,
99 WindowHidden,
100 AllWindowsHidden,
101 SessionUnmounted,
102 DeviceLost,
103 ThemeOrScaleChanged,
104 ModeratePressure,
105 CriticalPressure,
106 Explicit,
107 Shutdown,
108}
109
110#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
111pub enum MemoryEvent {
112 FrameCommitted,
113 WindowHidden,
114 WindowShown,
115 AllWindowsHidden,
116 SessionUnmounted,
117 RendererDeviceLost,
118 ThemeOrScaleChanged,
119 ModeratePressure,
120 CriticalPressure,
121 ExplicitTrim,
122 ApplicationShutdown,
123}
124
125impl MemoryEvent {
126 pub const fn trim_reason(self) -> TrimReason {
127 match self {
128 Self::FrameCommitted => TrimReason::SoftBudget,
129 Self::WindowHidden => TrimReason::WindowHidden,
130 Self::WindowShown => TrimReason::SoftBudget,
131 Self::AllWindowsHidden => TrimReason::AllWindowsHidden,
132 Self::SessionUnmounted => TrimReason::SessionUnmounted,
133 Self::RendererDeviceLost => TrimReason::DeviceLost,
134 Self::ThemeOrScaleChanged => TrimReason::ThemeOrScaleChanged,
135 Self::ModeratePressure => TrimReason::ModeratePressure,
136 Self::CriticalPressure => TrimReason::CriticalPressure,
137 Self::ExplicitTrim => TrimReason::Explicit,
138 Self::ApplicationShutdown => TrimReason::Shutdown,
139 }
140 }
141}
142
143#[cfg_attr(feature = "diagnostics-serde", derive(serde::Serialize))]
144#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
145pub enum MemoryAction {
146 #[default]
147 None,
148 EnforceBudget,
149 Trim {
150 scope: CacheScope,
151 target_bytes: usize,
152 },
153}
154
155#[derive(Clone, Debug, Hash, PartialEq, Eq)]
156pub struct CacheKey {
157 pub namespace: String,
158 pub key: String,
159 pub version: u64,
160}
161
162impl CacheKey {
163 pub fn new(namespace: impl Into<String>, key: impl Into<String>, version: u64) -> Self {
164 Self {
165 namespace: namespace.into(),
166 key: key.into(),
167 version,
168 }
169 }
170}