1use super::*;
2
3impl HybridRenderer {
5 #[must_use]
18 pub fn with_backend(
19 gpu_backend: GraphicsBackend,
20 width: u32,
21 height: u32,
22 config: RenderConfig,
23 ) -> Self {
24 Self::with_backend_and_font(
25 gpu_backend,
26 width,
27 height,
28 config,
29 NativeSystemFont::deterministic(),
30 )
31 }
32
33 #[must_use]
47 pub fn with_backend_and_font(
48 gpu_backend: GraphicsBackend,
49 width: u32,
50 height: u32,
51 config: RenderConfig,
52 font_provider: NativeSystemFont,
53 ) -> Self {
54 let mut compositor: RecordingGpuDriver = RecordingGpuDriver::default();
55 compositor.set_backend_kind(gpu_backend);
56 Self {
57 software: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
58 compositor,
59 last_cpu_effect_regions: Vec::new(),
60 last_gpu_native_commands: 0,
61 last_cpu_tile_count: 0,
62 last_cpu_upload_bytes: 0,
63 last_cpu_rasterized_pixels: 0,
64 }
65 }
66
67 #[must_use]
77 pub fn partition_commands(display_list: &DisplayList) -> HybridCommandPartition {
78 let scale: f32 = display_list.get_scale_factor().max(0.5);
79 let mut gpu_native_count: usize = 0;
80 let mut gpu_covered_pixels: usize = 0;
81 let mut cpu_effect_regions: Vec<Rect> = Vec::new();
82 for cmd in display_list.get_commands().iter() {
83 match cmd {
84 DrawCommand::BackdropBlur { rect, .. } => {
85 let phys: Rect = Rect::new(
86 rect.get_x() * scale,
87 rect.get_y() * scale,
88 (rect.get_width() * scale).max(1.0),
89 (rect.get_height() * scale).max(1.0),
90 );
91 cpu_effect_regions.push(phys);
92 }
93 DrawCommand::FillRoundedRect { rect, .. }
94 | DrawCommand::LinearGradientRect { rect, .. }
95 | DrawCommand::BoxShadow { rect, .. }
96 | DrawCommand::TextRun { rect, .. }
97 | DrawCommand::Image { rect, .. }
98 | DrawCommand::SharedImage { rect, .. }
99 | DrawCommand::SpriteRegion { rect, .. } => {
100 gpu_native_count = gpu_native_count.saturating_add(1);
101 let pw: usize = ((rect.get_width() * scale).ceil() as usize).max(1);
102 let ph: usize = ((rect.get_height() * scale).ceil() as usize).max(1);
103 gpu_covered_pixels = gpu_covered_pixels.saturating_add(pw.saturating_mul(ph));
104 }
105 DrawCommand::Circle { radius, .. } => {
106 gpu_native_count = gpu_native_count.saturating_add(1);
107 let d: usize = (((*radius) * 2.0 * scale).ceil() as usize).max(1);
108 gpu_covered_pixels = gpu_covered_pixels.saturating_add(d.saturating_mul(d));
109 }
110 DrawCommand::Line {
111 from, to, width, ..
112 } => {
113 gpu_native_count = gpu_native_count.saturating_add(1);
114 let lw: usize = (((to.get_x() - from.get_x()).abs().max(*width) * scale).ceil()
115 as usize)
116 .max(1);
117 let lh: usize = (((to.get_y() - from.get_y()).abs().max(*width) * scale).ceil()
118 as usize)
119 .max(1);
120 gpu_covered_pixels = gpu_covered_pixels.saturating_add(lw.saturating_mul(lh));
121 }
122 DrawCommand::Triangle { p0, p1, p2, .. } => {
123 gpu_native_count = gpu_native_count.saturating_add(1);
124 let w: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x())
125 - p0.get_x().min(p1.get_x()).min(p2.get_x());
126 let h: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y())
127 - p0.get_y().min(p1.get_y()).min(p2.get_y());
128 let pw: usize = ((w * scale).ceil() as usize).max(1);
129 let ph: usize = ((h * scale).ceil() as usize).max(1);
130 gpu_covered_pixels = gpu_covered_pixels.saturating_add(pw.saturating_mul(ph));
131 }
132 DrawCommand::Clear(_)
133 | DrawCommand::TextCaret { .. }
134 | DrawCommand::PushClip(_)
135 | DrawCommand::PushRoundedClip { .. }
136 | DrawCommand::PopClip => {
137 gpu_native_count = gpu_native_count.saturating_add(1);
138 }
139 }
140 }
141 HybridCommandPartition {
142 gpu_native_count,
143 cpu_effect_regions,
144 gpu_covered_pixels,
145 }
146 }
147
148 #[must_use]
162 pub fn evaluate_last_frame_net_gain(
163 &self,
164 gpu_full_ms: f64,
165 cpu_full_ms: f64,
166 cpu_tile_ms: f64,
167 gpu_composite_ms: f64,
168 ) -> HybridNetGainEstimate {
169 let fb: &Framebuffer = self.get_software().get_framebuffer();
170 estimate_hybrid_net_gain(
171 fb.get_width(),
172 fb.get_height(),
173 self.get_last_cpu_effect_regions().as_slice(),
174 gpu_full_ms,
175 cpu_full_ms,
176 cpu_tile_ms,
177 gpu_composite_ms,
178 )
179 }
180
181 pub fn render(
193 &mut self,
194 display_list: &DisplayList,
195 damage: DamageRegion,
196 ) -> Result<(usize, usize), PlatformError> {
197 let scale: f32 = display_list.get_scale_factor().max(0.5);
198 let vp: Size = display_list.get_viewport();
199 let phys_w: u32 = if vp.get_width() > 0.0 {
200 ((vp.get_width() * scale).round() as u32).max(1)
201 } else {
202 self.get_software().get_framebuffer().get_width().max(1)
203 };
204 let phys_h: u32 = if vp.get_height() > 0.0 {
205 ((vp.get_height() * scale).round() as u32).max(1)
206 } else {
207 self.get_software().get_framebuffer().get_height().max(1)
208 };
209 if self.get_software().get_framebuffer().get_width() != phys_w
210 || self.get_software().get_framebuffer().get_height() != phys_h
211 {
212 let sw: &mut SoftwareRenderer = self.get_mut_software();
213 if let Some(fb) = sw.framebuffer.as_mut() {
214 fb.resize(phys_w, phys_h);
215 }
216 }
217 let full_rect: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
218 let partition: HybridCommandPartition = Self::partition_commands(display_list);
219 let mut batch: GpuBatch = GpuBatch::default();
220 batch.set_scissor(full_rect);
221 let mut clear_color: Color = COLOR_TRANSPARENT;
222 for cmd in display_list.get_commands().iter() {
223 match cmd {
224 DrawCommand::Clear(c) => {
225 clear_color = *c;
226 }
227 DrawCommand::FillRoundedRect {
228 rect,
229 color,
230 radius,
231 border,
232 } => {
233 let quad: GpuQuadInstance = GpuQuadInstance {
234 rect: [
235 rect.get_x() * scale,
236 rect.get_y() * scale,
237 rect.get_width() * scale,
238 rect.get_height() * scale,
239 ],
240 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
241 color: color.to_f32_array(),
242 secondary_color: border.get_color().to_f32_array(),
243 radii: [
244 radius.get_top_left() * scale,
245 radius.get_top_right() * scale,
246 radius.get_bottom_right() * scale,
247 radius.get_bottom_left() * scale,
248 ],
249 stroke_or_blur: border.get_width() * scale,
250 angle_rad: 0.0,
251 kind: GpuPrimitiveKind::RoundedRect as u32,
252 reserved: 0,
253 };
254 let prim: GpuPrimitiveInstance =
255 GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
256 batch.get_mut_instances().push(quad);
257 batch.get_mut_primitives().push(prim);
258 }
259 DrawCommand::LinearGradientRect {
260 rect,
261 gradient,
262 radius,
263 } => {
264 let quad: GpuQuadInstance = GpuQuadInstance {
265 rect: [
266 rect.get_x() * scale,
267 rect.get_y() * scale,
268 rect.get_width() * scale,
269 rect.get_height() * scale,
270 ],
271 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
272 color: gradient.get_start_color().to_f32_array(),
273 secondary_color: gradient.get_end_color().to_f32_array(),
274 radii: [
275 radius.get_top_left() * scale,
276 radius.get_top_right() * scale,
277 radius.get_bottom_right() * scale,
278 radius.get_bottom_left() * scale,
279 ],
280 stroke_or_blur: 0.0,
281 angle_rad: gradient.get_angle_deg().to_radians(),
282 kind: GpuPrimitiveKind::Gradient as u32,
283 reserved: 0,
284 };
285 let prim: GpuPrimitiveInstance =
286 GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
287 batch.get_mut_instances().push(quad);
288 batch.get_mut_primitives().push(prim);
289 }
290 DrawCommand::BoxShadow {
291 rect,
292 radius,
293 shadow,
294 } => {
295 let pad: f32 = (shadow.get_blur_radius() + shadow.get_spread_radius()) * scale;
296 let quad: GpuQuadInstance = GpuQuadInstance {
297 rect: [
298 (rect.get_x() + shadow.get_offset_x()) * scale - pad,
299 (rect.get_y() + shadow.get_offset_y()) * scale - pad,
300 rect.get_width() * scale + pad * 2.0,
301 rect.get_height() * scale + pad * 2.0,
302 ],
303 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
304 color: shadow.get_color().to_f32_array(),
305 secondary_color: [0.0; 4],
306 radii: [
307 radius.get_top_left() * scale,
308 radius.get_top_right() * scale,
309 radius.get_bottom_right() * scale,
310 radius.get_bottom_left() * scale,
311 ],
312 stroke_or_blur: shadow.get_blur_radius() * scale,
313 angle_rad: pad,
314 kind: GpuPrimitiveKind::Shadow as u32,
315 reserved: 0,
316 };
317 let prim: GpuPrimitiveInstance =
318 GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
319 batch.get_mut_instances().push(quad);
320 batch.get_mut_primitives().push(prim);
321 }
322 DrawCommand::TextRun { rect, style, .. } => {
323 let quad: GpuQuadInstance = GpuQuadInstance {
324 rect: [
325 rect.get_x() * scale,
326 rect.get_y() * scale,
327 rect.get_width() * scale,
328 rect.get_height() * scale,
329 ],
330 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
331 color: style.get_color().to_f32_array(),
332 secondary_color: [0.0; 4],
333 radii: [0.0; 4],
334 stroke_or_blur: 0.0,
335 angle_rad: 0.0,
336 kind: GpuPrimitiveKind::Glyph as u32,
337 reserved: 0,
338 };
339 let prim: GpuPrimitiveInstance =
340 GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
341 batch.get_mut_instances().push(quad);
342 batch.get_mut_primitives().push(prim);
343 }
344 DrawCommand::Image { rect, radius, .. }
345 | DrawCommand::SharedImage { rect, radius, .. } => {
346 let quad: GpuQuadInstance = GpuQuadInstance {
347 rect: [
348 rect.get_x() * scale,
349 rect.get_y() * scale,
350 rect.get_width() * scale,
351 rect.get_height() * scale,
352 ],
353 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
354 color: [1.0, 1.0, 1.0, 1.0],
355 secondary_color: [0.0; 4],
356 radii: [
357 radius.get_top_left() * scale,
358 radius.get_top_right() * scale,
359 radius.get_bottom_right() * scale,
360 radius.get_bottom_left() * scale,
361 ],
362 stroke_or_blur: 0.0,
363 angle_rad: 0.0,
364 kind: GpuPrimitiveKind::TexturedQuad as u32,
365 reserved: 0,
366 };
367 let prim: GpuPrimitiveInstance =
368 GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
369 batch.get_mut_instances().push(quad);
370 batch.get_mut_primitives().push(prim);
371 }
372 DrawCommand::Circle {
373 center,
374 radius,
375 color,
376 } => {
377 let r: f32 = radius * scale;
378 let quad: GpuQuadInstance = GpuQuadInstance {
379 rect: [
380 center.get_x() * scale - r,
381 center.get_y() * scale - r,
382 r * 2.0,
383 r * 2.0,
384 ],
385 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
386 color: color.to_f32_array(),
387 secondary_color: [0.0; 4],
388 radii: [r; 4],
389 stroke_or_blur: 0.0,
390 angle_rad: 0.0,
391 kind: GpuPrimitiveKind::Circle as u32,
392 reserved: 0,
393 };
394 let prim: GpuPrimitiveInstance =
395 GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
396 batch.get_mut_instances().push(quad);
397 batch.get_mut_primitives().push(prim);
398 }
399 DrawCommand::BackdropBlur { .. }
400 | DrawCommand::TextCaret { .. }
401 | DrawCommand::Line { .. }
402 | DrawCommand::Triangle { .. }
403 | DrawCommand::SpriteRegion { .. }
404 | DrawCommand::PushClip(_)
405 | DrawCommand::PushRoundedClip { .. }
406 | DrawCommand::PopClip => {}
407 }
408 }
409 let mut cpu_tiles: usize = 0;
410 let mut cpu_pixels: usize = 0;
411 let mut cpu_upload_bytes: usize = 0;
412 if !partition.get_cpu_effect_regions().is_empty() {
413 let mut sparse_damage: DamageRegion = DamageRegion::default();
414 for region in partition.get_cpu_effect_regions().iter() {
415 let clipped_x: f32 = region.get_x().clamp(0.0, phys_w as f32);
416 let clipped_y: f32 = region.get_y().clamp(0.0, phys_h as f32);
417 let clipped_w: f32 = region
418 .get_width()
419 .min((phys_w as f32 - clipped_x).max(0.0))
420 .max(0.0);
421 let clipped_h: f32 = region
422 .get_height()
423 .min((phys_h as f32 - clipped_y).max(0.0))
424 .max(0.0);
425 if clipped_w <= 0.0 || clipped_h <= 0.0 {
426 continue;
427 }
428 let tight_rect: Rect = Rect::new(clipped_x, clipped_y, clipped_w, clipped_h);
429 if !damage.get_full_damage()
430 && let Some(dmg_rect) = damage.bounds_rect()
431 && tight_rect.intersect(dmg_rect).is_none()
432 {
433 continue;
434 }
435 sparse_damage.add_rect(tight_rect);
436 let tile_w: usize = clipped_w.ceil() as usize;
437 let tile_h: usize = clipped_h.ceil() as usize;
438 let tile_bytes: usize = tile_w
439 .saturating_mul(tile_h)
440 .saturating_mul(HYBRID_RGBA8_BYTES_PER_PIXEL);
441 cpu_upload_bytes = cpu_upload_bytes.saturating_add(tile_bytes);
442 let tile_quad: GpuQuadInstance = GpuQuadInstance {
443 rect: [clipped_x, clipped_y, clipped_w, clipped_h],
444 clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
445 color: [1.0, 1.0, 1.0, 1.0],
446 secondary_color: [0.0; 4],
447 radii: [0.0; 4],
448 stroke_or_blur: 0.0,
449 angle_rad: 0.0,
450 kind: GpuPrimitiveKind::TexturedQuad as u32,
451 reserved: 1,
452 };
453 let tile_prim: GpuPrimitiveInstance =
454 GpuPrimitiveInstance::from_quad_instance(&tile_quad, 0, 0);
455 batch.get_mut_instances().push(tile_quad);
456 batch.get_mut_primitives().push(tile_prim);
457 }
458 if sparse_damage.bounds_rect().is_some() {
459 let (tiles, pixels): (usize, usize) =
460 self.get_mut_software().render(display_list, sparse_damage);
461 cpu_tiles = tiles;
462 cpu_pixels = pixels;
463 }
464 }
465 self.set_last_cpu_effect_regions(partition.get_cpu_effect_regions().clone());
466 self.set_last_gpu_native_commands(partition.get_gpu_native_count());
467 self.set_last_cpu_tile_count(cpu_tiles);
468 self.set_last_cpu_upload_bytes(cpu_upload_bytes);
469 self.set_last_cpu_rasterized_pixels(cpu_pixels);
470 let submitted: usize =
471 self.get_mut_compositor()
472 .submit_batches(phys_w, phys_h, clear_color, &[batch])?;
473 let total_pixels: usize = if cpu_pixels > 0 {
474 partition
475 .get_gpu_covered_pixels()
476 .saturating_add(cpu_pixels)
477 } else {
478 partition.get_gpu_covered_pixels().max(1)
479 };
480 Ok((submitted.max(1), total_pixels))
481 }
482}