1use super::*;
2
3impl GpuDriver for RecordingGpuDriver {
5 fn backend(&self) -> GraphicsBackend {
15 self.get_backend_kind()
16 }
17
18 fn shader_source(&self) -> &'static str {
28 match self.get_backend_kind() {
29 GraphicsBackend::Metal => MSL_UI_SHADER,
30 GraphicsBackend::Direct3D12 | GraphicsBackend::Direct3D11 => HLSL_UI_SHADER,
31 GraphicsBackend::WebGl2 | GraphicsBackend::OpenGlEs => GLSL_ES3_UI_FRAGMENT_SHADER,
32 GraphicsBackend::WebGpu
33 | GraphicsBackend::Vulkan
34 | GraphicsBackend::SoftwareCpu
35 | GraphicsBackend::Canvas2d
36 | GraphicsBackend::Headless => WGSL_UI_SHADER,
37 }
38 }
39
40 fn submit_batches(
54 &mut self,
55 _width: u32,
56 _height: u32,
57 clear_color: Color,
58 batches: &[GpuBatch],
59 ) -> Result<usize, PlatformError> {
60 let count: usize = batches.len();
61 let instances: usize = batches.iter().fold(0_usize, |acc: usize, b: &GpuBatch| {
62 acc + b.get_instances().len()
63 });
64 self.set_last_clear(clear_color);
65 self.set_last_instance_count(instances);
66 self.set_total_draw_calls(self.get_total_draw_calls().saturating_add(count));
67 Ok(count)
68 }
69}
70
71impl HardwareRenderer {
73 #[must_use]
87 pub fn with_backend(
88 backend: GraphicsBackend,
89 width: u32,
90 height: u32,
91 config: RenderConfig,
92 enable_readback: bool,
93 ) -> Self {
94 Self::with_backend_and_font(
95 backend,
96 width,
97 height,
98 config,
99 enable_readback,
100 NativeSystemFont::deterministic(),
101 )
102 }
103
104 #[must_use]
119 pub fn with_backend_and_font(
120 backend: GraphicsBackend,
121 width: u32,
122 height: u32,
123 config: RenderConfig,
124 enable_readback: bool,
125 font_provider: NativeSystemFont,
126 ) -> Self {
127 let driver: NativeGpuDriver = NativeGpuDriver::probe(backend);
128 Self {
129 driver,
130 batches: Vec::with_capacity(16),
131 readback: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
132 enable_readback,
133 }
134 }
135
136 #[must_use]
146 pub fn batch_hash(&self) -> u64 {
147 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
148 for batch in self.get_batches().iter() {
149 for inst in batch.get_instances().iter() {
150 for v in inst
151 .get_rect()
152 .iter()
153 .chain(inst.get_color().iter())
154 .chain(inst.get_radii().iter())
155 {
156 hash ^= v.to_bits() as u64;
157 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
158 }
159 }
160 }
161 hash
162 }
163
164 pub fn render(
176 &mut self,
177 display_list: &DisplayList,
178 damage: DamageRegion,
179 ) -> Result<(usize, usize), PlatformError> {
180 let scale: f32 = display_list.get_scale_factor().max(0.5);
181 let phys_w: u32 = ((display_list.get_viewport().get_width() * scale).round() as u32).max(1);
182 let phys_h: u32 =
183 ((display_list.get_viewport().get_height() * scale).round() as u32).max(1);
184 let full_clip: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
185 let mut clip_stack: Vec<Rect> = vec![full_clip];
186 let mut clear_color: Color = COLOR_WHITE;
187 self.get_mut_batches().clear();
188 for cmd in display_list.get_commands().iter() {
189 let active_clip: Rect = clip_stack.last().copied().unwrap_or(full_clip);
190 match cmd {
191 DrawCommand::Clear(c) => {
192 clear_color = *c;
193 }
194 DrawCommand::PushClip(rect) | DrawCommand::PushRoundedClip { rect, .. } => {
195 let phys: Rect = snap_physical_rect(*rect, scale);
196 clip_stack.push(active_clip.intersect(phys).unwrap_or_default());
197 }
198 DrawCommand::PopClip => {
199 if clip_stack.len() > 1 {
200 clip_stack.pop();
201 }
202 }
203 DrawCommand::FillRoundedRect {
204 rect,
205 color,
206 radius,
207 border,
208 } => {
209 let snapped: Rect = snap_physical_rect(*rect, scale);
210 let bw: f32 = if border.get_width() > 0.0 {
211 (border.get_width() * scale).max(1.0)
212 } else {
213 0.0
214 };
215 let inst: GpuQuadInstance = GpuQuadInstance {
216 rect: [
217 snapped.get_x(),
218 snapped.get_y(),
219 snapped.get_width(),
220 snapped.get_height(),
221 ],
222 clip: [
223 active_clip.get_x(),
224 active_clip.get_y(),
225 active_clip.right(),
226 active_clip.bottom(),
227 ],
228 color: color.to_f32_array(),
229 secondary_color: border.get_color().to_f32_array(),
230 radii: [
231 radius.get_top_left() * scale,
232 radius.get_top_right() * scale,
233 radius.get_bottom_right() * scale,
234 radius.get_bottom_left() * scale,
235 ],
236 stroke_or_blur: bw,
237 angle_rad: 0.0,
238 kind: GpuPrimitiveKind::RoundedRect as u32,
239 reserved: 0,
240 };
241 self.push_instance(active_clip, inst);
242 }
243 DrawCommand::LinearGradientRect {
244 rect,
245 gradient,
246 radius,
247 } => {
248 let snapped: Rect = snap_physical_rect(*rect, scale);
249 let inst: GpuQuadInstance = GpuQuadInstance {
250 rect: [
251 snapped.get_x(),
252 snapped.get_y(),
253 snapped.get_width(),
254 snapped.get_height(),
255 ],
256 clip: [
257 active_clip.get_x(),
258 active_clip.get_y(),
259 active_clip.right(),
260 active_clip.bottom(),
261 ],
262 color: gradient.get_start_color().to_f32_array(),
263 secondary_color: gradient.get_end_color().to_f32_array(),
264 radii: [
265 radius.get_top_left() * scale,
266 radius.get_top_right() * scale,
267 radius.get_bottom_right() * scale,
268 radius.get_bottom_left() * scale,
269 ],
270 stroke_or_blur: 0.0,
271 angle_rad: gradient.get_angle_deg().to_radians(),
272 kind: GpuPrimitiveKind::Gradient as u32,
273 reserved: 0,
274 };
275 self.push_instance(active_clip, inst);
276 }
277 DrawCommand::BoxShadow {
278 rect,
279 radius,
280 shadow,
281 } => {
282 let spread: f32 = shadow.get_spread_radius() * scale;
283 let blur: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
284 let sigma: f32 = (blur * 0.5).max(0.75);
285 let pad: f32 = (sigma * 3.0).ceil();
286 let base_rect: Rect = snap_physical_rect(*rect, scale);
287 let caster_x: f32 =
288 base_rect.get_x() + (shadow.get_offset_x() * scale).round() - spread;
289 let caster_y: f32 =
290 base_rect.get_y() + (shadow.get_offset_y() * scale).round() - spread;
291 let caster_w: f32 = (base_rect.get_width() + spread * 2.0).max(1.0);
292 let caster_h: f32 = (base_rect.get_height() + spread * 2.0).max(1.0);
293 let inst: GpuQuadInstance = GpuQuadInstance {
294 rect: [
295 caster_x - pad,
296 caster_y - pad,
297 caster_w + pad * 2.0,
298 caster_h + pad * 2.0,
299 ],
300 clip: [
301 active_clip.get_x(),
302 active_clip.get_y(),
303 active_clip.right(),
304 active_clip.bottom(),
305 ],
306 color: shadow.get_color().to_f32_array(),
307 secondary_color: [0.0; 4],
308 radii: [
309 (radius.get_top_left() * scale + spread).max(0.0),
310 (radius.get_top_right() * scale + spread).max(0.0),
311 (radius.get_bottom_right() * scale + spread).max(0.0),
312 (radius.get_bottom_left() * scale + spread).max(0.0),
313 ],
314 stroke_or_blur: sigma,
315 angle_rad: pad,
316 kind: GpuPrimitiveKind::Shadow as u32,
317 reserved: 0,
318 };
319 self.push_instance(active_clip, inst);
320 }
321 DrawCommand::TextRun { rect, text, style } => {
322 let glyph_count: usize =
323 text.chars().filter(|c: &char| *c != ' ').count().max(1);
324 let inst: GpuQuadInstance = GpuQuadInstance {
325 rect: [
326 rect.get_x() * scale,
327 rect.get_y() * scale,
328 rect.get_width() * scale,
329 rect.get_height() * scale,
330 ],
331 clip: [
332 active_clip.get_x(),
333 active_clip.get_y(),
334 active_clip.right(),
335 active_clip.bottom(),
336 ],
337 color: style.get_color().to_f32_array(),
338 secondary_color: [0.0; 4],
339 radii: [0.0; 4],
340 stroke_or_blur: style.get_font_size() * scale,
341 angle_rad: 0.0,
342 kind: GpuPrimitiveKind::Glyph as u32,
343 reserved: glyph_count as u32,
344 };
345 self.push_instance(active_clip, inst);
346 }
347 DrawCommand::TextCaret {
348 text_rect,
349 content_rect,
350 text,
351 style,
352 caret_height,
353 color,
354 multiline,
355 } => {
356 let meas: TextLayoutResult = DeterministicTextMeasurer.measure(
357 text,
358 *style,
359 if *multiline {
360 Some(content_rect.get_width().max(1.0))
361 } else {
362 None
363 },
364 );
365 let last_w: f32 = meas
366 .get_lines()
367 .last()
368 .map(TextLine::get_width)
369 .unwrap_or(0.0);
370 let line_idx: usize = meas.get_lines().len().saturating_sub(1);
371 let caret_x: f32 =
372 (text_rect.get_x() + last_w + 1.5).min(content_rect.right() - 2.0);
373 let caret_y: f32 = if *multiline {
374 (content_rect.get_y()
375 + (line_idx as f32) * meas.get_line_height_px()
376 + ((meas.get_line_height_px() - caret_height) * 0.5).max(0.0))
377 .min(content_rect.bottom() - caret_height)
378 .max(content_rect.get_y())
379 } else {
380 content_rect.get_y()
381 + ((content_rect.get_height() - caret_height) * 0.5).max(0.0)
382 };
383 let inst: GpuQuadInstance = GpuQuadInstance {
384 rect: [
385 caret_x * scale,
386 caret_y * scale,
387 (1.5 * scale).max(1.0),
388 (caret_height * scale).max(2.0),
389 ],
390 clip: [
391 active_clip.get_x(),
392 active_clip.get_y(),
393 active_clip.right(),
394 active_clip.bottom(),
395 ],
396 color: color.to_f32_array(),
397 secondary_color: [0.0; 4],
398 radii: [0.5 * scale; 4],
399 stroke_or_blur: 0.0,
400 angle_rad: 0.0,
401 kind: GpuPrimitiveKind::RoundedRect as u32,
402 reserved: 0,
403 };
404 self.push_instance(active_clip, inst);
405 }
406 DrawCommand::Image { rect, radius, .. } => {
407 let snapped: Rect = snap_physical_rect(*rect, scale);
408 let inst: GpuQuadInstance = GpuQuadInstance {
409 rect: [
410 snapped.get_x(),
411 snapped.get_y(),
412 snapped.get_width(),
413 snapped.get_height(),
414 ],
415 clip: [
416 active_clip.get_x(),
417 active_clip.get_y(),
418 active_clip.right(),
419 active_clip.bottom(),
420 ],
421 color: [1.0, 1.0, 1.0, 1.0],
422 secondary_color: [0.0; 4],
423 radii: [
424 radius.get_top_left() * scale,
425 radius.get_top_right() * scale,
426 radius.get_bottom_right() * scale,
427 radius.get_bottom_left() * scale,
428 ],
429 stroke_or_blur: 0.0,
430 angle_rad: 0.0,
431 kind: GpuPrimitiveKind::TexturedQuad as u32,
432 reserved: 0,
433 };
434 self.push_instance(active_clip, inst);
435 }
436 DrawCommand::Circle {
437 center,
438 radius,
439 color,
440 } => {
441 let r: f32 = radius * scale;
442 let inst: GpuQuadInstance = GpuQuadInstance {
443 rect: [
444 center.get_x() * scale - r,
445 center.get_y() * scale - r,
446 r * 2.0,
447 r * 2.0,
448 ],
449 clip: [
450 active_clip.get_x(),
451 active_clip.get_y(),
452 active_clip.right(),
453 active_clip.bottom(),
454 ],
455 color: color.to_f32_array(),
456 secondary_color: [0.0; 4],
457 radii: [r; 4],
458 stroke_or_blur: 0.0,
459 angle_rad: 0.0,
460 kind: GpuPrimitiveKind::Circle as u32,
461 reserved: 0,
462 };
463 self.push_instance(active_clip, inst);
464 }
465 DrawCommand::Line {
466 from,
467 to,
468 color,
469 width,
470 } => {
471 let dx: f32 = (to.get_x() - from.get_x()) * scale;
472 let dy: f32 = (to.get_y() - from.get_y()) * scale;
473 let x0: f32 = from.get_x().min(to.get_x()) * scale;
474 let y0: f32 = from.get_y().min(to.get_y()) * scale;
475 let w: f32 = dx.abs().max(width * scale);
476 let h: f32 = dy.abs().max(width * scale);
477 let inst: GpuQuadInstance = GpuQuadInstance {
478 rect: [x0, y0, w, h],
479 clip: [
480 active_clip.get_x(),
481 active_clip.get_y(),
482 active_clip.right(),
483 active_clip.bottom(),
484 ],
485 color: color.to_f32_array(),
486 secondary_color: [0.0; 4],
487 radii: [
488 from.get_x() * scale,
489 from.get_y() * scale,
490 to.get_x() * scale,
491 to.get_y() * scale,
492 ],
493 stroke_or_blur: width * scale,
494 angle_rad: dy.atan2(dx),
495 kind: GpuPrimitiveKind::Line as u32,
496 reserved: 0,
497 };
498 self.push_instance(active_clip, inst);
499 }
500 DrawCommand::Triangle { p0, p1, p2, color } => {
501 let min_x: f32 = p0.get_x().min(p1.get_x()).min(p2.get_x()) * scale;
502 let min_y: f32 = p0.get_y().min(p1.get_y()).min(p2.get_y()) * scale;
503 let max_x: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x()) * scale;
504 let max_y: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y()) * scale;
505 let inst: GpuQuadInstance = GpuQuadInstance {
506 rect: [
507 min_x,
508 min_y,
509 (max_x - min_x).max(1.0),
510 (max_y - min_y).max(1.0),
511 ],
512 clip: [
513 active_clip.get_x(),
514 active_clip.get_y(),
515 active_clip.right(),
516 active_clip.bottom(),
517 ],
518 color: color.to_f32_array(),
519 secondary_color: [p0.get_x() * scale, p0.get_y() * scale, 0.0, 0.0],
520 radii: [
521 p1.get_x() * scale,
522 p1.get_y() * scale,
523 p2.get_x() * scale,
524 p2.get_y() * scale,
525 ],
526 stroke_or_blur: 0.0,
527 angle_rad: 0.0,
528 kind: GpuPrimitiveKind::Triangle as u32,
529 reserved: 3,
530 };
531 self.push_instance(active_clip, inst);
532 }
533 DrawCommand::SpriteRegion { rect, tint, .. } => {
534 let snapped: Rect = snap_physical_rect(*rect, scale);
535 let inst: GpuQuadInstance = GpuQuadInstance {
536 rect: [
537 snapped.get_x(),
538 snapped.get_y(),
539 snapped.get_width(),
540 snapped.get_height(),
541 ],
542 clip: [
543 active_clip.get_x(),
544 active_clip.get_y(),
545 active_clip.right(),
546 active_clip.bottom(),
547 ],
548 color: tint.to_f32_array(),
549 secondary_color: [0.0; 4],
550 radii: [0.0; 4],
551 stroke_or_blur: 0.0,
552 angle_rad: 0.0,
553 kind: GpuPrimitiveKind::TexturedQuad as u32,
554 reserved: 1,
555 };
556 self.push_instance(active_clip, inst);
557 }
558 }
559 }
560 let Self {
561 driver,
562 batches,
563 readback,
564 enable_readback,
565 } = self;
566 let submitted: usize = driver.submit_batches(phys_w, phys_h, clear_color, batches)?;
567 let pixels: usize = if *enable_readback {
568 let (_, px): (usize, usize) = readback.render(display_list, damage);
569 px
570 } else {
571 (phys_w as usize) * (phys_h as usize)
572 };
573 Ok((submitted, pixels))
574 }
575
576 fn push_instance(&mut self, scissor: Rect, instance: GpuQuadInstance) {
584 if let Some(last) = self.get_mut_batches().last_mut()
585 && last.get_scissor() == scissor
586 && last.get_instances().len() < MAX_QUADS_PER_BATCH
587 {
588 last.get_mut_instances().push(instance);
589 return;
590 }
591 let mut batch: GpuBatch = GpuBatch::default();
592 batch.set_scissor(scissor);
593 batch.get_mut_instances().push(instance);
594 self.get_mut_batches().push(batch);
595 }
596}