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 active_batches: 0,
132 clip_stack: Vec::with_capacity(16),
133 last_submitted_batches: 0,
134 last_coalesced_merges: 0,
135 last_culled_instances: 0,
136 last_uploaded_instances: 0,
137 last_uploaded_bytes: 0,
138 readback: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
139 enable_readback,
140 }
141 }
142
143 #[must_use]
153 pub fn last_frame_telemetry(&self) -> (usize, usize, usize, usize, usize) {
154 (
155 self.get_last_submitted_batches(),
156 self.get_last_coalesced_merges(),
157 self.get_last_culled_instances(),
158 self.get_last_uploaded_instances(),
159 self.get_last_uploaded_bytes(),
160 )
161 }
162
163 #[must_use]
173 pub fn batch_hash(&self) -> u64 {
174 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
175 let active: usize = self.get_active_batches().min(self.get_batches().len());
176 for batch in self.get_batches()[..active].iter() {
177 for inst in batch.get_instances().iter() {
178 for v in inst
179 .get_rect()
180 .iter()
181 .chain(inst.get_color().iter())
182 .chain(inst.get_radii().iter())
183 {
184 hash ^= v.to_bits() as u64;
185 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
186 }
187 }
188 }
189 hash
190 }
191
192 #[inline(always)]
203 #[must_use]
204 fn quad_inside_rect(quad: [f32; 4], container: Rect) -> bool {
205 let qx0: f32 = quad[0];
206 let qy0: f32 = quad[1];
207 let qx1: f32 = qx0 + quad[2];
208 let qy1: f32 = qy0 + quad[3];
209 let x_in: bool = qx0 >= container.get_x() && qx1 <= container.right();
210 let y_in: bool = qy0 >= container.get_y() && qy1 <= container.bottom();
211 container.get_width() > 0.0 && container.get_height() > 0.0 && x_in && y_in
212 }
213
214 #[inline(always)]
225 #[must_use]
226 fn quad_intersects_clip(quad: [f32; 4], clip: Rect) -> bool {
227 let qw: f32 = quad[2];
228 let qh: f32 = quad[3];
229 let quad_empty: bool = qw <= 0.0 || qh <= 0.0;
230 let clip_empty: bool = clip.get_width() <= 0.0 || clip.get_height() <= 0.0;
231 if quad_empty || clip_empty {
232 return false;
233 }
234 let qx0: f32 = quad[0];
235 let qy0: f32 = quad[1];
236 let qx1: f32 = qx0 + qw;
237 let qy1: f32 = qy0 + qh;
238 let x_hit: bool = qx1 > clip.get_x() && qx0 < clip.right();
239 let y_hit: bool = qy1 > clip.get_y() && qy0 < clip.bottom();
240 x_hit && y_hit
241 }
242
243 #[inline(always)]
254 #[must_use]
255 fn rect_inside_rect(inner: Rect, outer: Rect) -> bool {
256 let x_in: bool = inner.get_x() >= outer.get_x() && inner.right() <= outer.right();
257 let y_in: bool = inner.get_y() >= outer.get_y() && inner.bottom() <= outer.bottom();
258 x_in && y_in
259 }
260
261 #[inline(always)]
272 #[must_use]
273 fn union_rect_quad(existing: Rect, quad: [f32; 4]) -> Rect {
274 let x0: f32 = existing.get_x().min(quad[0]);
275 let y0: f32 = existing.get_y().min(quad[1]);
276 let x1: f32 = existing.right().max(quad[0] + quad[2]);
277 let y1: f32 = existing.bottom().max(quad[1] + quad[3]);
278 Rect::new(x0, y0, (x1 - x0).max(0.0), (y1 - y0).max(0.0))
279 }
280
281 pub fn render(
293 &mut self,
294 display_list: &DisplayList,
295 damage: DamageRegion,
296 ) -> Result<(usize, usize), PlatformError> {
297 let scale: f32 = display_list.get_scale_factor().max(0.5);
298 let phys_w: u32 = ((display_list.get_viewport().get_width() * scale).round() as u32).max(1);
299 let phys_h: u32 =
300 ((display_list.get_viewport().get_height() * scale).round() as u32).max(1);
301 let full_clip: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
302 let full_entry: GpuClipEntry = GpuClipEntry {
303 scissor: full_clip,
304 safe_inner: full_clip,
305 };
306 let Self {
307 driver,
308 batches,
309 active_batches,
310 clip_stack,
311 last_submitted_batches,
312 last_coalesced_merges,
313 last_culled_instances,
314 last_uploaded_instances,
315 last_uploaded_bytes,
316 readback,
317 enable_readback,
318 } = self;
319 clip_stack.clear();
320 clip_stack.push(full_entry);
321 *active_batches = 0;
322 let mut tail_state: GpuBatchCoalesceState = GpuBatchCoalesceState::default();
323 let mut coalesced_merges: usize = 0;
324 let mut culled_instances: usize = 0;
325 let mut uploaded_instances: usize = 0;
326 let mut clear_color: Color = COLOR_WHITE;
327 for cmd in display_list.get_commands().iter() {
328 let clip_entry: GpuClipEntry = clip_stack.last().copied().unwrap_or(full_entry);
329 let active_clip: Rect = clip_entry.scissor;
330 match cmd {
331 DrawCommand::Clear(c) => {
332 clear_color = *c;
333 }
334 DrawCommand::PushClip(rect) => {
335 let phys: Rect = snap_physical_rect(*rect, scale);
336 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
337 let next_safe: Rect = clip_entry.safe_inner.intersect(phys).unwrap_or_default();
338 clip_stack.push(GpuClipEntry {
339 scissor: next_scissor,
340 safe_inner: next_safe,
341 });
342 }
343 DrawCommand::PushRoundedClip { rect, radius } => {
344 let phys: Rect = snap_physical_rect(*rect, scale);
345 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
346 let max_r: f32 = (radius.max_radius() * scale).ceil();
347 let inset_w: f32 = (phys.get_width() - max_r * 2.0).max(0.0);
348 let inset_h: f32 = (phys.get_height() - max_r * 2.0).max(0.0);
349 let rounded_safe: Rect =
350 Rect::new(phys.get_x() + max_r, phys.get_y() + max_r, inset_w, inset_h);
351 let next_safe: Rect = clip_entry
352 .safe_inner
353 .intersect(rounded_safe)
354 .unwrap_or_default();
355 clip_stack.push(GpuClipEntry {
356 scissor: next_scissor,
357 safe_inner: next_safe,
358 });
359 }
360 DrawCommand::PopClip => {
361 if clip_stack.len() > 1 {
362 let _: Option<GpuClipEntry> = clip_stack.pop();
363 }
364 }
365 DrawCommand::FillRoundedRect {
366 rect,
367 color,
368 radius,
369 border,
370 } => {
371 let snapped: Rect = snap_physical_rect(*rect, scale);
372 let quad: [f32; 4] = [
373 snapped.get_x(),
374 snapped.get_y(),
375 snapped.get_width(),
376 snapped.get_height(),
377 ];
378 if !Self::quad_intersects_clip(quad, active_clip) {
379 culled_instances += 1;
380 continue;
381 }
382 let bw: f32 = if border.get_width() > 0.0 {
383 (border.get_width() * scale).max(1.0)
384 } else {
385 0.0
386 };
387 let inst: GpuQuadInstance = GpuQuadInstance {
388 rect: quad,
389 clip: [
390 active_clip.get_x(),
391 active_clip.get_y(),
392 active_clip.right(),
393 active_clip.bottom(),
394 ],
395 color: color.to_f32_array(),
396 secondary_color: border.get_color().to_f32_array(),
397 radii: [
398 radius.get_top_left() * scale,
399 radius.get_top_right() * scale,
400 radius.get_bottom_right() * scale,
401 radius.get_bottom_left() * scale,
402 ],
403 stroke_or_blur: bw,
404 angle_rad: 0.0,
405 kind: GpuPrimitiveKind::RoundedRect as u32,
406 reserved: 0,
407 };
408 Self::push_instance_coalesced(
409 batches,
410 active_batches,
411 &mut tail_state,
412 clip_entry,
413 inst,
414 &mut coalesced_merges,
415 );
416 uploaded_instances += 1;
417 }
418 DrawCommand::LinearGradientRect {
419 rect,
420 gradient,
421 radius,
422 } => {
423 let snapped: Rect = snap_physical_rect(*rect, scale);
424 let quad: [f32; 4] = [
425 snapped.get_x(),
426 snapped.get_y(),
427 snapped.get_width(),
428 snapped.get_height(),
429 ];
430 if !Self::quad_intersects_clip(quad, active_clip) {
431 culled_instances += 1;
432 continue;
433 }
434 let inst: GpuQuadInstance = GpuQuadInstance {
435 rect: quad,
436 clip: [
437 active_clip.get_x(),
438 active_clip.get_y(),
439 active_clip.right(),
440 active_clip.bottom(),
441 ],
442 color: gradient.get_start_color().to_f32_array(),
443 secondary_color: gradient.get_end_color().to_f32_array(),
444 radii: [
445 radius.get_top_left() * scale,
446 radius.get_top_right() * scale,
447 radius.get_bottom_right() * scale,
448 radius.get_bottom_left() * scale,
449 ],
450 stroke_or_blur: 0.0,
451 angle_rad: gradient.get_angle_deg().to_radians(),
452 kind: GpuPrimitiveKind::Gradient as u32,
453 reserved: 0,
454 };
455 Self::push_instance_coalesced(
456 batches,
457 active_batches,
458 &mut tail_state,
459 clip_entry,
460 inst,
461 &mut coalesced_merges,
462 );
463 uploaded_instances += 1;
464 }
465 DrawCommand::BoxShadow {
466 rect,
467 radius,
468 shadow,
469 } => {
470 let spread: f32 = shadow.get_spread_radius() * scale;
471 let blur: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
472 let sigma: f32 = (blur * 0.5).max(0.75);
473 let pad: f32 = (sigma * 3.0).ceil();
474 let base_rect: Rect = snap_physical_rect(*rect, scale);
475 let caster_x: f32 =
476 base_rect.get_x() + (shadow.get_offset_x() * scale).round() - spread;
477 let caster_y: f32 =
478 base_rect.get_y() + (shadow.get_offset_y() * scale).round() - spread;
479 let caster_w: f32 = (base_rect.get_width() + spread * 2.0).max(1.0);
480 let caster_h: f32 = (base_rect.get_height() + spread * 2.0).max(1.0);
481 let quad: [f32; 4] = [
482 caster_x - pad,
483 caster_y - pad,
484 caster_w + pad * 2.0,
485 caster_h + pad * 2.0,
486 ];
487 if !Self::quad_intersects_clip(quad, active_clip) {
488 culled_instances += 1;
489 continue;
490 }
491 let inst: GpuQuadInstance = GpuQuadInstance {
492 rect: quad,
493 clip: [
494 active_clip.get_x(),
495 active_clip.get_y(),
496 active_clip.right(),
497 active_clip.bottom(),
498 ],
499 color: shadow.get_color().to_f32_array(),
500 secondary_color: [0.0; 4],
501 radii: [
502 (radius.get_top_left() * scale + spread).max(0.0),
503 (radius.get_top_right() * scale + spread).max(0.0),
504 (radius.get_bottom_right() * scale + spread).max(0.0),
505 (radius.get_bottom_left() * scale + spread).max(0.0),
506 ],
507 stroke_or_blur: sigma,
508 angle_rad: pad,
509 kind: GpuPrimitiveKind::Shadow as u32,
510 reserved: 0,
511 };
512 Self::push_instance_coalesced(
513 batches,
514 active_batches,
515 &mut tail_state,
516 clip_entry,
517 inst,
518 &mut coalesced_merges,
519 );
520 uploaded_instances += 1;
521 }
522 DrawCommand::TextRun { rect, text, style } => {
523 let quad: [f32; 4] = [
524 rect.get_x() * scale,
525 rect.get_y() * scale,
526 rect.get_width() * scale,
527 rect.get_height() * scale,
528 ];
529 if !Self::quad_intersects_clip(quad, active_clip) {
530 culled_instances += 1;
531 continue;
532 }
533 let glyph_count: usize = if text.is_ascii() {
534 text.as_bytes()
535 .iter()
536 .filter(|b: &&u8| **b != b' ')
537 .count()
538 .max(1)
539 } else {
540 text.chars().filter(|c: &char| *c != ' ').count().max(1)
541 };
542 let inst: GpuQuadInstance = GpuQuadInstance {
543 rect: quad,
544 clip: [
545 active_clip.get_x(),
546 active_clip.get_y(),
547 active_clip.right(),
548 active_clip.bottom(),
549 ],
550 color: style.get_color().to_f32_array(),
551 secondary_color: [0.0; 4],
552 radii: [0.0; 4],
553 stroke_or_blur: style.get_font_size() * scale,
554 angle_rad: 0.0,
555 kind: GpuPrimitiveKind::Glyph as u32,
556 reserved: glyph_count as u32,
557 };
558 Self::push_instance_coalesced(
559 batches,
560 active_batches,
561 &mut tail_state,
562 clip_entry,
563 inst,
564 &mut coalesced_merges,
565 );
566 uploaded_instances += 1;
567 }
568 DrawCommand::TextCaret {
569 text_rect,
570 content_rect,
571 text,
572 style,
573 caret_height,
574 color,
575 multiline,
576 } => {
577 if active_clip.get_width() <= 0.0 || active_clip.get_height() <= 0.0 {
578 culled_instances += 1;
579 continue;
580 }
581 let meas: TextLayoutResult = DeterministicTextMeasurer.measure(
582 text,
583 *style,
584 if *multiline {
585 Some(content_rect.get_width().max(1.0))
586 } else {
587 None
588 },
589 );
590 let last_w: f32 = meas
591 .get_lines()
592 .last()
593 .map(TextLine::get_width)
594 .unwrap_or(0.0);
595 let line_idx: usize = meas.get_lines().len().saturating_sub(1);
596 let trailing_gap: f32 = if last_w > 0.0 { 1.5 } else { 0.0 };
597 let min_x: f32 = content_rect.get_x();
598 let max_x: f32 = (content_rect.right() - 2.0).max(min_x);
599 let caret_x: f32 = (text_rect.get_x() + last_w + trailing_gap)
600 .min(max_x)
601 .max(min_x);
602 let caret_y: f32 = if *multiline {
603 (content_rect.get_y()
604 + (line_idx as f32) * meas.get_line_height_px()
605 + ((meas.get_line_height_px() - caret_height) * 0.5).max(0.0))
606 .min(content_rect.bottom() - caret_height)
607 .max(content_rect.get_y())
608 } else {
609 content_rect.get_y()
610 + ((content_rect.get_height() - caret_height) * 0.5).max(0.0)
611 };
612 let quad: [f32; 4] = [
613 caret_x * scale,
614 caret_y * scale,
615 (1.5 * scale).max(1.0),
616 (caret_height * scale).max(2.0),
617 ];
618 if !Self::quad_intersects_clip(quad, active_clip) {
619 culled_instances += 1;
620 continue;
621 }
622 let inst: GpuQuadInstance = GpuQuadInstance {
623 rect: quad,
624 clip: [
625 active_clip.get_x(),
626 active_clip.get_y(),
627 active_clip.right(),
628 active_clip.bottom(),
629 ],
630 color: color.to_f32_array(),
631 secondary_color: [0.0; 4],
632 radii: [0.5 * scale; 4],
633 stroke_or_blur: 0.0,
634 angle_rad: 0.0,
635 kind: GpuPrimitiveKind::RoundedRect as u32,
636 reserved: 0,
637 };
638 Self::push_instance_coalesced(
639 batches,
640 active_batches,
641 &mut tail_state,
642 clip_entry,
643 inst,
644 &mut coalesced_merges,
645 );
646 uploaded_instances += 1;
647 }
648 DrawCommand::Image { rect, radius, .. } => {
649 let snapped: Rect = snap_physical_rect(*rect, scale);
650 let quad: [f32; 4] = [
651 snapped.get_x(),
652 snapped.get_y(),
653 snapped.get_width(),
654 snapped.get_height(),
655 ];
656 if !Self::quad_intersects_clip(quad, active_clip) {
657 culled_instances += 1;
658 continue;
659 }
660 let inst: GpuQuadInstance = GpuQuadInstance {
661 rect: quad,
662 clip: [
663 active_clip.get_x(),
664 active_clip.get_y(),
665 active_clip.right(),
666 active_clip.bottom(),
667 ],
668 color: [1.0, 1.0, 1.0, 1.0],
669 secondary_color: [0.0; 4],
670 radii: [
671 radius.get_top_left() * scale,
672 radius.get_top_right() * scale,
673 radius.get_bottom_right() * scale,
674 radius.get_bottom_left() * scale,
675 ],
676 stroke_or_blur: 0.0,
677 angle_rad: 0.0,
678 kind: GpuPrimitiveKind::TexturedQuad as u32,
679 reserved: 0,
680 };
681 Self::push_instance_coalesced(
682 batches,
683 active_batches,
684 &mut tail_state,
685 clip_entry,
686 inst,
687 &mut coalesced_merges,
688 );
689 uploaded_instances += 1;
690 }
691 DrawCommand::Circle {
692 center,
693 radius,
694 color,
695 } => {
696 let r: f32 = radius * scale;
697 let quad: [f32; 4] = [
698 center.get_x() * scale - r,
699 center.get_y() * scale - r,
700 r * 2.0,
701 r * 2.0,
702 ];
703 if !Self::quad_intersects_clip(quad, active_clip) {
704 culled_instances += 1;
705 continue;
706 }
707 let inst: GpuQuadInstance = GpuQuadInstance {
708 rect: quad,
709 clip: [
710 active_clip.get_x(),
711 active_clip.get_y(),
712 active_clip.right(),
713 active_clip.bottom(),
714 ],
715 color: color.to_f32_array(),
716 secondary_color: [0.0; 4],
717 radii: [r; 4],
718 stroke_or_blur: 0.0,
719 angle_rad: 0.0,
720 kind: GpuPrimitiveKind::Circle as u32,
721 reserved: 0,
722 };
723 Self::push_instance_coalesced(
724 batches,
725 active_batches,
726 &mut tail_state,
727 clip_entry,
728 inst,
729 &mut coalesced_merges,
730 );
731 uploaded_instances += 1;
732 }
733 DrawCommand::Line {
734 from,
735 to,
736 color,
737 width,
738 } => {
739 let dx: f32 = (to.get_x() - from.get_x()) * scale;
740 let dy: f32 = (to.get_y() - from.get_y()) * scale;
741 let x0: f32 = from.get_x().min(to.get_x()) * scale;
742 let y0: f32 = from.get_y().min(to.get_y()) * scale;
743 let w: f32 = dx.abs().max(width * scale);
744 let h: f32 = dy.abs().max(width * scale);
745 let quad: [f32; 4] = [x0, y0, w, h];
746 if !Self::quad_intersects_clip(quad, active_clip) {
747 culled_instances += 1;
748 continue;
749 }
750 let inst: GpuQuadInstance = GpuQuadInstance {
751 rect: quad,
752 clip: [
753 active_clip.get_x(),
754 active_clip.get_y(),
755 active_clip.right(),
756 active_clip.bottom(),
757 ],
758 color: color.to_f32_array(),
759 secondary_color: [0.0; 4],
760 radii: [
761 from.get_x() * scale,
762 from.get_y() * scale,
763 to.get_x() * scale,
764 to.get_y() * scale,
765 ],
766 stroke_or_blur: width * scale,
767 angle_rad: dy.atan2(dx),
768 kind: GpuPrimitiveKind::Line as u32,
769 reserved: 0,
770 };
771 Self::push_instance_coalesced(
772 batches,
773 active_batches,
774 &mut tail_state,
775 clip_entry,
776 inst,
777 &mut coalesced_merges,
778 );
779 uploaded_instances += 1;
780 }
781 DrawCommand::Triangle { p0, p1, p2, color } => {
782 let min_x: f32 = p0.get_x().min(p1.get_x()).min(p2.get_x()) * scale;
783 let min_y: f32 = p0.get_y().min(p1.get_y()).min(p2.get_y()) * scale;
784 let max_x: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x()) * scale;
785 let max_y: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y()) * scale;
786 let quad: [f32; 4] = [
787 min_x,
788 min_y,
789 (max_x - min_x).max(1.0),
790 (max_y - min_y).max(1.0),
791 ];
792 if !Self::quad_intersects_clip(quad, active_clip) {
793 culled_instances += 1;
794 continue;
795 }
796 let inst: GpuQuadInstance = GpuQuadInstance {
797 rect: quad,
798 clip: [
799 active_clip.get_x(),
800 active_clip.get_y(),
801 active_clip.right(),
802 active_clip.bottom(),
803 ],
804 color: color.to_f32_array(),
805 secondary_color: [p0.get_x() * scale, p0.get_y() * scale, 0.0, 0.0],
806 radii: [
807 p1.get_x() * scale,
808 p1.get_y() * scale,
809 p2.get_x() * scale,
810 p2.get_y() * scale,
811 ],
812 stroke_or_blur: 0.0,
813 angle_rad: 0.0,
814 kind: GpuPrimitiveKind::Triangle as u32,
815 reserved: 3,
816 };
817 Self::push_instance_coalesced(
818 batches,
819 active_batches,
820 &mut tail_state,
821 clip_entry,
822 inst,
823 &mut coalesced_merges,
824 );
825 uploaded_instances += 1;
826 }
827 DrawCommand::SpriteRegion { rect, tint, .. } => {
828 let snapped: Rect = snap_physical_rect(*rect, scale);
829 let quad: [f32; 4] = [
830 snapped.get_x(),
831 snapped.get_y(),
832 snapped.get_width(),
833 snapped.get_height(),
834 ];
835 if !Self::quad_intersects_clip(quad, active_clip) {
836 culled_instances += 1;
837 continue;
838 }
839 let inst: GpuQuadInstance = GpuQuadInstance {
840 rect: quad,
841 clip: [
842 active_clip.get_x(),
843 active_clip.get_y(),
844 active_clip.right(),
845 active_clip.bottom(),
846 ],
847 color: tint.to_f32_array(),
848 secondary_color: [0.0; 4],
849 radii: [0.0; 4],
850 stroke_or_blur: 0.0,
851 angle_rad: 0.0,
852 kind: GpuPrimitiveKind::TexturedQuad as u32,
853 reserved: 1,
854 };
855 Self::push_instance_coalesced(
856 batches,
857 active_batches,
858 &mut tail_state,
859 clip_entry,
860 inst,
861 &mut coalesced_merges,
862 );
863 uploaded_instances += 1;
864 }
865 }
866 }
867 let active_slice: &[GpuBatch] = &batches[..(*active_batches).min(batches.len())];
868 let submitted: usize = driver.submit_batches(phys_w, phys_h, clear_color, active_slice)?;
869 *last_submitted_batches = submitted;
870 *last_coalesced_merges = coalesced_merges;
871 *last_culled_instances = culled_instances;
872 *last_uploaded_instances = uploaded_instances;
873 *last_uploaded_bytes = uploaded_instances.saturating_mul(GPU_QUAD_INSTANCE_BYTES);
874 let pixels: usize = if *enable_readback {
875 let (_, px): (usize, usize) = readback.render(display_list, damage);
876 px
877 } else {
878 (phys_w as usize) * (phys_h as usize)
879 };
880 Ok((submitted, pixels))
881 }
882
883 fn push_instance_coalesced(
894 batches: &mut Vec<GpuBatch>,
895 active_batches: &mut usize,
896 tail_state: &mut GpuBatchCoalesceState,
897 clip_entry: GpuClipEntry,
898 instance: GpuQuadInstance,
899 coalesced_merges: &mut usize,
900 ) {
901 let scissor: Rect = clip_entry.scissor;
902 let inst_unclipped: bool = Self::quad_inside_rect(instance.rect, clip_entry.safe_inner);
903 let count: usize = *active_batches;
904 if count > 0 {
905 let last: &mut GpuBatch = &mut batches[count - 1];
906 if last.get_instances().len() < MAX_QUADS_PER_BATCH {
907 let last_scissor: Rect = last.get_scissor();
908 if last_scissor == scissor {
909 last.get_mut_instances().push(instance);
910 tail_state.all_unclipped = tail_state.all_unclipped && inst_unclipped;
911 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
912 return;
913 }
914 if inst_unclipped && Self::quad_inside_rect(instance.rect, last_scissor) {
915 last.get_mut_instances().push(instance);
916 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
917 *coalesced_merges += 1;
918 return;
919 }
920 if tail_state.all_unclipped && Self::rect_inside_rect(tail_state.bounds, scissor) {
921 last.set_scissor(scissor);
922 last.get_mut_instances().push(instance);
923 tail_state.all_unclipped = inst_unclipped;
924 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
925 *coalesced_merges += 1;
926 return;
927 }
928 }
929 }
930 if count < batches.len() {
931 let slot: &mut GpuBatch = &mut batches[count];
932 slot.set_scissor(scissor);
933 slot.get_mut_instances().clear();
934 slot.get_mut_instances().push(instance);
935 } else {
936 let mut batch: GpuBatch = GpuBatch::default();
937 batch.set_scissor(scissor);
938 batch.get_mut_instances().push(instance);
939 batches.push(batch);
940 }
941 *active_batches = count + 1;
942 tail_state.all_unclipped = inst_unclipped;
943 tail_state.bounds = Rect::new(
944 instance.rect[0],
945 instance.rect[1],
946 instance.rect[2],
947 instance.rect[3],
948 );
949 }
950}