1use crate::{ClippedShape, Galley, Mesh, Primitive, Shape};
4
5#[derive(Clone, Copy, PartialEq)]
7enum ElementSize {
8 Unknown,
9 Homogeneous(usize),
10 Heterogenous,
11}
12
13impl Default for ElementSize {
14 fn default() -> Self {
15 Self::Unknown
16 }
17}
18
19#[derive(Clone, Copy, Default, PartialEq)]
21pub struct AllocInfo {
22 element_size: ElementSize,
23 num_allocs: usize,
24 num_elements: usize,
25 num_bytes: usize,
26}
27
28impl<T> From<&[T]> for AllocInfo {
29 fn from(slice: &[T]) -> Self {
30 Self::from_slice(slice)
31 }
32}
33
34impl std::ops::Add for AllocInfo {
35 type Output = Self;
36
37 fn add(self, rhs: Self) -> Self {
38 use ElementSize::{Heterogenous, Homogeneous, Unknown};
39 let element_size = match (self.element_size, rhs.element_size) {
40 (Heterogenous, _) | (_, Heterogenous) => Heterogenous,
41 (Unknown, other) | (other, Unknown) => other,
42 (Homogeneous(lhs), Homogeneous(rhs)) if lhs == rhs => Homogeneous(lhs),
43 _ => Heterogenous,
44 };
45
46 Self {
47 element_size,
48 num_allocs: self.num_allocs + rhs.num_allocs,
49 num_elements: self.num_elements + rhs.num_elements,
50 num_bytes: self.num_bytes + rhs.num_bytes,
51 }
52 }
53}
54
55impl std::ops::AddAssign for AllocInfo {
56 fn add_assign(&mut self, rhs: Self) {
57 *self = *self + rhs;
58 }
59}
60
61impl std::iter::Sum for AllocInfo {
62 fn sum<I>(iter: I) -> Self
63 where
64 I: Iterator<Item = Self>,
65 {
66 let mut sum = Self::default();
67 for value in iter {
68 sum += value;
69 }
70 sum
71 }
72}
73
74impl AllocInfo {
75 pub fn from_galley(galley: &Galley) -> Self {
89 Self::from_slice(galley.text().as_bytes())
90 + Self::from_slice(&galley.rows)
91 + galley.rows.iter().map(Self::from_galley_row).sum()
92 }
93
94 fn from_galley_row(row: &crate::text::Row) -> Self {
95 Self::from_mesh(&row.visuals.mesh) + Self::from_slice(&row.glyphs)
96 }
97
98 pub fn from_mesh(mesh: &Mesh) -> Self {
99 Self::from_slice(&mesh.indices) + Self::from_slice(&mesh.vertices)
100 }
101
102 pub fn from_slice<T>(slice: &[T]) -> Self {
103 use std::mem::size_of;
104 let element_size = size_of::<T>();
105 Self {
106 element_size: ElementSize::Homogeneous(element_size),
107 num_allocs: 1,
108 num_elements: slice.len(),
109 num_bytes: std::mem::size_of_val(slice),
110 }
111 }
112
113 pub fn num_elements(&self) -> usize {
114 assert!(self.element_size != ElementSize::Heterogenous);
115 self.num_elements
116 }
117
118 pub fn num_allocs(&self) -> usize {
119 self.num_allocs
120 }
121
122 pub fn num_bytes(&self) -> usize {
123 self.num_bytes
124 }
125
126 pub fn megabytes(&self) -> String {
127 megabytes(self.num_bytes())
128 }
129
130 pub fn format(&self, what: &str) -> String {
131 if self.num_allocs() == 0 {
132 format!("{:6} {:16}", 0, what)
133 } else if self.num_allocs() == 1 {
134 format!(
135 "{:6} {:16} {} 1 allocation",
136 self.num_elements,
137 what,
138 self.megabytes()
139 )
140 } else if self.element_size != ElementSize::Heterogenous {
141 format!(
142 "{:6} {:16} {} {:3} allocations",
143 self.num_elements(),
144 what,
145 self.megabytes(),
146 self.num_allocs()
147 )
148 } else {
149 format!(
150 "{:6} {:16} {} {:3} allocations",
151 "",
152 what,
153 self.megabytes(),
154 self.num_allocs()
155 )
156 }
157 }
158}
159
160#[derive(Clone, Copy, Default)]
162pub struct PaintStats {
163 pub shapes: AllocInfo,
164 pub shape_text: AllocInfo,
165 pub shape_path: AllocInfo,
166 pub shape_mesh: AllocInfo,
167 pub shape_vec: AllocInfo,
168 pub num_callbacks: usize,
169
170 pub text_shape_vertices: AllocInfo,
171 pub text_shape_indices: AllocInfo,
172
173 pub clipped_primitives: AllocInfo,
175 pub vertices: AllocInfo,
176 pub indices: AllocInfo,
177}
178
179impl PaintStats {
180 pub fn from_shapes(shapes: &[ClippedShape]) -> Self {
181 let mut stats = Self::default();
182 stats.shape_path.element_size = ElementSize::Heterogenous; stats.shape_vec.element_size = ElementSize::Heterogenous; stats.shapes = AllocInfo::from_slice(shapes);
186 for ClippedShape { shape, .. } in shapes {
187 stats.add(shape);
188 }
189 stats
190 }
191
192 fn add(&mut self, shape: &Shape) {
193 match shape {
194 Shape::Vec(shapes) => {
195 self.shapes += AllocInfo::from_slice(shapes);
197 self.shape_vec += AllocInfo::from_slice(shapes);
198 for shape in shapes {
199 self.add(shape);
200 }
201 }
202 Shape::Noop
203 | Shape::Circle { .. }
204 | Shape::Ellipse { .. }
205 | Shape::LineSegment { .. }
206 | Shape::Rect { .. }
207 | Shape::CubicBezier(_)
208 | Shape::QuadraticBezier(_) => {}
209 Shape::Path(path_shape) => {
210 self.shape_path += AllocInfo::from_slice(&path_shape.points);
211 }
212 Shape::Text(text_shape) => {
213 self.shape_text += AllocInfo::from_galley(&text_shape.galley);
214
215 for row in &text_shape.galley.rows {
216 self.text_shape_indices += AllocInfo::from_slice(&row.visuals.mesh.indices);
217 self.text_shape_vertices += AllocInfo::from_slice(&row.visuals.mesh.vertices);
218 }
219 }
220 Shape::Mesh(mesh) => {
221 self.shape_mesh += AllocInfo::from_mesh(mesh);
222 }
223 Shape::Callback(_) => {
224 self.num_callbacks += 1;
225 }
226 }
227 }
228
229 pub fn with_clipped_primitives(
230 mut self,
231 clipped_primitives: &[crate::ClippedPrimitive],
232 ) -> Self {
233 self.clipped_primitives += AllocInfo::from_slice(clipped_primitives);
234 for clipped_primitive in clipped_primitives {
235 if let Primitive::Mesh(mesh) = &clipped_primitive.primitive {
236 self.vertices += AllocInfo::from_slice(&mesh.vertices);
237 self.indices += AllocInfo::from_slice(&mesh.indices);
238 }
239 }
240 self
241 }
242}
243
244fn megabytes(size: usize) -> String {
245 format!("{:.2} MB", size as f64 / 1e6)
246}