1use super::*;
2
3impl GpuPrimitiveInstance {
5 #[must_use]
15 pub fn pack_rgba8_f32(rgba: [f32; 4]) -> u32 {
16 let red_val: u32 = (rgba[0].clamp(0.0, 1.0) * 255.0).round() as u32;
17 let green_val: u32 = (rgba[1].clamp(0.0, 1.0) * 255.0).round() as u32;
18 let blue_val: u32 = (rgba[2].clamp(0.0, 1.0) * 255.0).round() as u32;
19 let alpha_val: u32 = (rgba[3].clamp(0.0, 1.0) * 255.0).round() as u32;
20 red_val | (green_val << 8) | (blue_val << 16) | (alpha_val << 24)
21 }
22
23 #[must_use]
33 pub fn pack_color_rgba8(color: Color) -> u32 {
34 (color.get_red() as u32)
35 | ((color.get_green() as u32) << 8)
36 | ((color.get_blue() as u32) << 16)
37 | ((color.get_alpha() as u32) << 24)
38 }
39
40 #[must_use]
50 pub fn pack_u16_q4(value: f32) -> u16 {
51 (value.clamp(0.0, 4095.9375) * 16.0).round() as u16
52 }
53
54 #[must_use]
67 pub fn corner_radius_cap(rect: [f32; 4], kind: u32, shadow_pad: f32) -> f32 {
68 let shortest: f32 = rect[2].min(rect[3]);
69 let inner: f32 = if kind == GpuPrimitiveKind::Shadow as u32 {
70 shortest - shadow_pad * 2.0
71 } else {
72 shortest
73 };
74 (inner * 0.5).max(0.0)
75 }
76
77 #[must_use]
87 pub fn f32_to_f16_bits(value: f32) -> u16 {
88 let bits: u32 = value.to_bits();
89 let sign: u32 = (bits >> 16) & 0x8000;
90 let exp: i32 = ((bits >> 23) & 0xFF) as i32 - 127 + 15;
91 let frac: u32 = bits & 0x007F_FFFF;
92 if exp <= 0 {
93 if exp < -10 {
94 return sign as u16;
95 }
96 let mant: u32 = (frac | 0x0080_0000) >> (1 - exp);
97 return (sign | ((mant + 0x1000) >> 13)) as u16;
98 }
99 if exp >= 31 {
100 return (sign | 0x7C00) as u16;
101 }
102 let rounded: u32 = frac + 0x1000;
103 if (rounded & 0x0080_0000) != 0 {
104 (sign | (((exp + 1) as u32) << 10)) as u16
105 } else {
106 (sign | ((exp as u32) << 10) | (rounded >> 13)) as u16
107 }
108 }
109
110 #[must_use]
121 pub fn pack_f16x2(lo: f32, hi: f32) -> u32 {
122 (Self::f32_to_f16_bits(lo) as u32) | ((Self::f32_to_f16_bits(hi) as u32) << 16)
123 }
124
125 #[must_use]
138 pub const fn pack_kind_flags(
139 kind: PrimKind,
140 clip_mask_or_sub: u16,
141 tex_slot: u8,
142 layer_id: u8,
143 ) -> u32 {
144 ((kind as u32) & 0xF)
145 | (((clip_mask_or_sub as u32) & 0xFFF) << 4)
146 | ((tex_slot as u32) << 16)
147 | ((layer_id as u32) << 24)
148 }
149
150 #[must_use]
156 pub fn prim_kind(&self) -> PrimKind {
157 match self.get_kind_flags() & 0xF {
158 1 => PrimKind::LinearGradientRect,
159 2 => PrimKind::Shadow,
160 3 => PrimKind::GlyphAlpha8,
161 4 => PrimKind::GlyphColorRgba8,
162 5 => PrimKind::ImageTexture,
163 6 => PrimKind::VectorShape,
164 7 => PrimKind::ExternalGpuSurface,
165 _ => PrimKind::RoundedRect,
166 }
167 }
168
169 #[must_use]
175 pub fn clip_or_sub_kind(&self) -> u16 {
176 ((self.get_kind_flags() >> 4) & 0xFFF) as u16
177 }
178
179 #[must_use]
193 pub fn encode_rounded_clip_bits(
194 parent_scissor: Rect,
195 parent_bits: u16,
196 next_scissor: Rect,
197 pushed_rect: Rect,
198 pushed_radius_phys: Option<CornerRadius>,
199 ) -> u16 {
200 if next_scissor.get_width() <= 0.0 || next_scissor.get_height() <= 0.0 {
201 return 0;
202 }
203 let parent_mask: u16 = parent_bits & 0xF;
204 let parent_q_rad: u16 = (parent_bits >> 4) & 0x3F;
205 let mut mask: u16 = 0;
206 if parent_mask != 0 && parent_q_rad > 0 {
207 let same_left: bool = (next_scissor.get_x() - parent_scissor.get_x()).abs() <= 0.5;
208 let same_top: bool = (next_scissor.get_y() - parent_scissor.get_y()).abs() <= 0.5;
209 let same_right: bool = (next_scissor.right() - parent_scissor.right()).abs() <= 0.5;
210 let same_bottom: bool = (next_scissor.bottom() - parent_scissor.bottom()).abs() <= 0.5;
211 if (parent_mask & 1) != 0 && same_left && same_top {
212 mask |= 1;
213 }
214 if (parent_mask & 2) != 0 && same_right && same_top {
215 mask |= 2;
216 }
217 if (parent_mask & 4) != 0 && same_right && same_bottom {
218 mask |= 4;
219 }
220 if (parent_mask & 8) != 0 && same_left && same_bottom {
221 mask |= 8;
222 }
223 }
224 let mut q_rad: u16 = if mask != 0 { parent_q_rad } else { 0 };
225 if let Some(rad) = pushed_radius_phys {
226 let cap_r: f32 = (pushed_rect.get_width() * 0.5)
227 .min(pushed_rect.get_height() * 0.5)
228 .max(0.0);
229 let r_tl: f32 = rad.get_top_left().min(cap_r);
230 let r_tr: f32 = rad.get_top_right().min(cap_r);
231 let r_br: f32 = rad.get_bottom_right().min(cap_r);
232 let r_bl: f32 = rad.get_bottom_left().min(cap_r);
233 let max_pushed_r: f32 = r_tl.max(r_tr).max(r_br).max(r_bl);
234 let pushed_q_rad: u16 = (max_pushed_r * 2.0).round().clamp(0.0, 63.0) as u16;
235 if pushed_q_rad > 0 {
236 let touch_left: bool = (next_scissor.get_x() - pushed_rect.get_x()).abs() <= 0.5;
237 let touch_top: bool = (next_scissor.get_y() - pushed_rect.get_y()).abs() <= 0.5;
238 let touch_right: bool = (next_scissor.right() - pushed_rect.right()).abs() <= 0.5;
239 let touch_bottom: bool =
240 (next_scissor.bottom() - pushed_rect.bottom()).abs() <= 0.5;
241 let mut pushed_mask: u16 = 0;
242 if r_tl > 0.25 && touch_left && touch_top {
243 pushed_mask |= 1;
244 }
245 if r_tr > 0.25 && touch_right && touch_top {
246 pushed_mask |= 2;
247 }
248 if r_br > 0.25 && touch_right && touch_bottom {
249 pushed_mask |= 4;
250 }
251 if r_bl > 0.25 && touch_left && touch_bottom {
252 pushed_mask |= 8;
253 }
254 if pushed_mask != 0 {
255 mask |= pushed_mask;
256 q_rad = q_rad.max(pushed_q_rad);
257 }
258 }
259 }
260 if mask == 0 || q_rad == 0 {
261 0
262 } else {
263 (mask & 0xF) | ((q_rad & 0x3F) << 4)
264 }
265 }
266
267 #[inline(always)]
278 #[must_use]
279 pub fn with_rounded_clip_bits(mut self, rounded_clip_bits: u16) -> Self {
280 if rounded_clip_bits != 0 {
281 let current_flags: u32 = self.get_kind_flags();
282 let next_flags: u32 =
283 (current_flags & !(0x3FF_u32 << 6)) | (((rounded_clip_bits as u32) & 0x3FF) << 6);
284 let _: &mut Self = self.set_kind_flags(next_flags);
285 }
286 self
287 }
288
289 #[must_use]
295 pub fn texture_slot(&self) -> u8 {
296 ((self.get_kind_flags() >> 16) & 0xFF) as u8
297 }
298
299 #[must_use]
305 pub fn layer_id(&self) -> u8 {
306 ((self.get_kind_flags() >> 24) & 0xFF) as u8
307 }
308
309 #[must_use]
321 pub fn from_quad_instance(quad: &GpuQuadInstance, tex_slot: u8, layer_id: u8) -> Self {
322 const {
323 assert!(size_of::<GpuPrimitiveInstance>() == GPU_PRIMITIVE_INSTANCE_BYTES);
324 }
325 let rect: [f32; 4] = quad.get_rect();
326 let clip_rect: [f32; 4] = quad.get_clip();
327 let color_rgba: u32 = Self::pack_rgba8_f32(quad.get_color());
328 let mut aux_color_rgba: u32 = Self::pack_rgba8_f32(quad.get_secondary_color());
329 let mut uv_rect: [u16; 4] = [0, 0, u16::MAX, u16::MAX];
330 let src_radii: [f32; 4] = quad.get_radii();
331 let radius_cap: f32 = Self::corner_radius_cap(rect, quad.get_kind(), quad.get_angle_rad());
332 let mut radii_border: [u16; 4] = [
333 Self::pack_u16_q4(src_radii[0].min(radius_cap)),
334 Self::pack_u16_q4(src_radii[1].min(radius_cap)),
335 Self::pack_u16_q4(src_radii[2].min(radius_cap)),
336 Self::pack_u16_q4(src_radii[3].min(radius_cap)),
337 ];
338 let (prim_kind, sub_kind): (PrimKind, u16) = match quad.get_kind() {
339 1 => (PrimKind::LinearGradientRect, 0),
340 2 => (PrimKind::Shadow, 0),
341 3 => {
342 let is_color_glyph: bool = (quad.get_reserved() & 0x8000_0000) != 0;
343 if is_color_glyph {
344 (PrimKind::GlyphColorRgba8, 0)
345 } else {
346 (PrimKind::GlyphAlpha8, 0)
347 }
348 }
349 4 => (PrimKind::ImageTexture, 0),
350 5 => (PrimKind::VectorShape, VectorSubKind::Circle as u16),
351 6 => {
352 let inv_w: f32 = 1.0 / rect[2].max(1.0);
353 let inv_h: f32 = 1.0 / rect[3].max(1.0);
354 let red_val: [f32; 4] = quad.get_radii();
355 uv_rect = [
356 (((red_val[0] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
357 (((red_val[1] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
358 (((red_val[2] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
359 (((red_val[3] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
360 ];
361 radii_border = [0; 4];
362 (PrimKind::VectorShape, VectorSubKind::Line as u16)
363 }
364 7 => {
365 let inv_w: f32 = 1.0 / rect[2].max(1.0);
366 let inv_h: f32 = 1.0 / rect[3].max(1.0);
367 let p0: [f32; 4] = quad.get_secondary_color();
368 let radius_val: [f32; 4] = quad.get_radii();
369 aux_color_rgba = Self::pack_rgba8_f32([
370 ((p0[0] - rect[0]) * inv_w).clamp(0.0, 1.0),
371 ((p0[1] - rect[1]) * inv_h).clamp(0.0, 1.0),
372 0.0,
373 1.0,
374 ]);
375 uv_rect = [
376 (((radius_val[0] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
377 (((radius_val[1] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
378 (((radius_val[2] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
379 (((radius_val[3] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
380 ];
381 radii_border = [0; 4];
382 (PrimKind::VectorShape, VectorSubKind::Triangle as u16)
383 }
384 8 => (PrimKind::ExternalGpuSurface, 0),
385 _ => (PrimKind::RoundedRect, 0),
386 };
387 let kind_flags: u32 = Self::pack_kind_flags(prim_kind, sub_kind, tex_slot, layer_id);
388 let extra_param: u32 = Self::pack_f16x2(quad.get_stroke_or_blur(), quad.get_angle_rad());
389 Self {
390 rect,
391 clip_rect,
392 color_rgba,
393 aux_color_rgba,
394 uv_rect,
395 radii_border,
396 kind_flags,
397 extra_param,
398 }
399 }
400
401 #[must_use]
416 pub fn external_surface(
417 bounds: Rect,
418 clip: Rect,
419 radii: [f32; 4],
420 uv_rect: [u16; 4],
421 tex_slot: u8,
422 layer_id: u8,
423 ) -> Self {
424 Self {
425 rect: [
426 bounds.get_x(),
427 bounds.get_y(),
428 bounds.get_width(),
429 bounds.get_height(),
430 ],
431 clip_rect: [clip.get_x(), clip.get_y(), clip.right(), clip.bottom()],
432 color_rgba: 0xFFFF_FFFF,
433 aux_color_rgba: 0,
434 uv_rect,
435 radii_border: [
436 Self::pack_u16_q4(radii[0]),
437 Self::pack_u16_q4(radii[1]),
438 Self::pack_u16_q4(radii[2]),
439 Self::pack_u16_q4(radii[3]),
440 ],
441 kind_flags: Self::pack_kind_flags(PrimKind::ExternalGpuSurface, 0, tex_slot, layer_id),
442 extra_param: 0,
443 }
444 }
445}
446
447impl GpuLayerUniform {
449 #[must_use]
461 pub fn new(viewport_w: f32, viewport_h: f32, scroll_offset: [f32; 2]) -> Self {
462 const {
463 assert!(size_of::<GpuLayerUniform>() == GPU_LAYER_UNIFORM_BYTES);
464 }
465 Self {
466 viewport_size: [viewport_w.max(1.0), viewport_h.max(1.0)],
467 scroll_offset,
468 }
469 }
470}
471
472impl GpuDriver for RecordingGpuDriver {
474 fn backend(&self) -> GraphicsBackend {
484 self.get_backend_kind()
485 }
486
487 fn shader_source(&self) -> &'static str {
497 match self.get_backend_kind() {
498 GraphicsBackend::Metal => MSL_UI_SHADER,
499 GraphicsBackend::Direct3D12 | GraphicsBackend::Direct3D11 => HLSL_UI_SHADER,
500 GraphicsBackend::WebGl2 | GraphicsBackend::OpenGlEs => GLSL_ES3_UI_FRAGMENT_SHADER,
501 GraphicsBackend::WebGpu
502 | GraphicsBackend::Vulkan
503 | GraphicsBackend::SoftwareCpu
504 | GraphicsBackend::Canvas2d
505 | GraphicsBackend::Headless => WGSL_UI_SHADER,
506 }
507 }
508
509 fn submit_batches(
523 &mut self,
524 _width: u32,
525 _height: u32,
526 clear_color: Color,
527 batches: &[GpuBatch],
528 ) -> Result<usize, PlatformError> {
529 let count: usize = batches.len();
530 let instances: usize = batches
531 .iter()
532 .fold(0_usize, |acc: usize, blue_val: &GpuBatch| {
533 acc + blue_val.get_instances().len()
534 });
535 self.set_last_clear(clear_color);
536 self.set_last_instance_count(instances);
537 self.set_total_draw_calls(self.get_total_draw_calls().saturating_add(count));
538 Ok(count)
539 }
540}
541
542impl HardwareRenderer {
544 #[must_use]
558 pub fn with_backend(
559 backend: GraphicsBackend,
560 width: u32,
561 height: u32,
562 config: RenderConfig,
563 enable_readback: bool,
564 ) -> Self {
565 Self::with_backend_and_font(
566 backend,
567 width,
568 height,
569 config,
570 enable_readback,
571 NativeSystemFont::deterministic(),
572 )
573 }
574
575 #[must_use]
590 pub fn with_backend_and_font(
591 backend: GraphicsBackend,
592 width: u32,
593 height: u32,
594 config: RenderConfig,
595 enable_readback: bool,
596 font_provider: NativeSystemFont,
597 ) -> Self {
598 let driver: NativeGpuDriver = NativeGpuDriver::probe(backend);
599 let mut batches: Vec<GpuBatch> = Vec::with_capacity(16);
600 for _ in 0..8usize {
601 let mut batch: GpuBatch = GpuBatch::default();
602 batch.get_mut_instances().reserve(256);
603 batch.get_mut_primitives().reserve(256);
604 batches.push(batch);
605 }
606 Self {
607 driver,
608 batches,
609 active_batches: 0,
610 clip_stack: Vec::with_capacity(16),
611 last_submitted_batches: 0,
612 last_coalesced_merges: 0,
613 last_culled_instances: 0,
614 last_uploaded_instances: 0,
615 last_uploaded_bytes: 0,
616 readback: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
617 enable_readback,
618 resource_ring: GpuResourceRing::default(),
619 direct_gpu_frames: 0,
620 cpu_readback_frames: 0,
621 last_cpu_rgba_upload_bytes: 0,
622 has_unsupported_effect: false,
623 last_fence_id: 0,
624 blur_passes: Vec::with_capacity(8),
625 active_scroll_offset: [0.0, 0.0],
626 active_layer_id: 0,
627 last_uploaded_primitive_bytes: 0,
628 }
629 }
630
631 pub fn set_font_provider(&mut self, font_provider: NativeSystemFont) -> &mut Self {
642 let _: &mut SoftwareRenderer = self.get_mut_readback().set_font_provider(font_provider);
643 self
644 }
645
646 pub fn capture_reference_readback(
658 &mut self,
659 display_list: &DisplayList,
660 damage: DamageRegion,
661 ) -> &Framebuffer {
662 let (_, _): (usize, usize) = self.get_mut_readback().render(display_list, damage);
663 self.get_readback().get_framebuffer()
664 }
665
666 pub fn complete_gpu_fence(&mut self, fence_id: u64) -> usize {
677 self.get_mut_resource_ring().complete_fence(fence_id)
678 }
679
680 pub fn trim_gpu_caches(&mut self, retain_ratio: f32) -> usize {
691 self.get_mut_resource_ring()
692 .apply_retention_ratio(retain_ratio)
693 }
694
695 #[must_use]
705 pub fn last_frame_telemetry(&self) -> (usize, usize, usize, usize, usize) {
706 (
707 self.get_last_submitted_batches(),
708 self.get_last_coalesced_merges(),
709 self.get_last_culled_instances(),
710 self.get_last_uploaded_instances(),
711 self.get_last_uploaded_bytes(),
712 )
713 }
714
715 #[must_use]
725 pub fn active_batches_slice(&self) -> &[GpuBatch] {
726 let active: usize = self.get_active_batches().min(self.get_batches().len());
727 &self.get_batches()[..active]
728 }
729
730 #[must_use]
740 pub fn batch_hash(&self) -> u64 {
741 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
742 let active: usize = self.get_active_batches().min(self.get_batches().len());
743 for batch in self.get_batches()[..active].iter() {
744 for inst in batch.get_instances().iter() {
745 for item_val in inst
746 .get_rect()
747 .iter()
748 .chain(inst.get_color().iter())
749 .chain(inst.get_radii().iter())
750 {
751 hash ^= item_val.to_bits() as u64;
752 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
753 }
754 }
755 }
756 hash
757 }
758
759 #[inline(always)]
770 #[must_use]
771 fn quad_inside_rect(quad: [f32; 4], container: Rect) -> bool {
772 let qx0: f32 = quad[0];
773 let qy0: f32 = quad[1];
774 let qx1: f32 = qx0 + quad[2];
775 let qy1: f32 = qy0 + quad[3];
776 let x_in: bool = qx0 >= container.get_x() && qx1 <= container.right();
777 let y_in: bool = qy0 >= container.get_y() && qy1 <= container.bottom();
778 container.get_width() > 0.0 && container.get_height() > 0.0 && x_in && y_in
779 }
780
781 #[inline(always)]
792 #[must_use]
793 fn quad_intersects_clip(quad: [f32; 4], clip: Rect) -> bool {
794 let qw: f32 = quad[2];
795 let qh: f32 = quad[3];
796 let quad_empty: bool = qw <= 0.0 || qh <= 0.0;
797 let clip_empty: bool = clip.get_width() <= 0.0 || clip.get_height() <= 0.0;
798 if quad_empty || clip_empty {
799 return false;
800 }
801 let qx0: f32 = quad[0];
802 let qy0: f32 = quad[1];
803 let qx1: f32 = qx0 + qw;
804 let qy1: f32 = qy0 + qh;
805 let x_hit: bool = qx1 > clip.get_x() && qx0 < clip.right();
806 let y_hit: bool = qy1 > clip.get_y() && qy0 < clip.bottom();
807 x_hit && y_hit
808 }
809
810 #[inline(always)]
821 #[must_use]
822 fn rect_inside_rect(inner: Rect, outer: Rect) -> bool {
823 let x_in: bool = inner.get_x() >= outer.get_x() && inner.right() <= outer.right();
824 let y_in: bool = inner.get_y() >= outer.get_y() && inner.bottom() <= outer.bottom();
825 x_in && y_in
826 }
827
828 #[inline(always)]
839 #[must_use]
840 fn union_rect_quad(existing: Rect, quad: [f32; 4]) -> Rect {
841 let x0: f32 = existing.get_x().min(quad[0]);
842 let y0: f32 = existing.get_y().min(quad[1]);
843 let x1: f32 = existing.right().max(quad[0] + quad[2]);
844 let y1: f32 = existing.bottom().max(quad[1] + quad[3]);
845 Rect::new(x0, y0, (x1 - x0).max(0.0), (y1 - y0).max(0.0))
846 }
847
848 pub fn render(
860 &mut self,
861 display_list: &DisplayList,
862 damage: DamageRegion,
863 ) -> Result<(usize, usize), PlatformError> {
864 let scale: f32 = display_list.get_scale_factor().max(0.5);
865 let phys_w: u32 = ((display_list.get_viewport().get_width() * scale).round() as u32).max(1);
866 let phys_h: u32 =
867 ((display_list.get_viewport().get_height() * scale).round() as u32).max(1);
868 let full_clip: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
869 let full_entry: GpuClipEntry = GpuClipEntry {
870 scissor: full_clip,
871 safe_inner: full_clip,
872 rounded_clip_bits: 0,
873 };
874 let Self {
875 driver,
876 batches,
877 active_batches,
878 clip_stack,
879 last_submitted_batches,
880 last_coalesced_merges,
881 last_culled_instances,
882 last_uploaded_instances,
883 last_uploaded_bytes,
884 readback,
885 enable_readback,
886 resource_ring,
887 direct_gpu_frames,
888 cpu_readback_frames,
889 last_cpu_rgba_upload_bytes,
890 has_unsupported_effect,
891 last_fence_id,
892 blur_passes,
893 active_scroll_offset,
894 active_layer_id,
895 last_uploaded_primitive_bytes,
896 } = self;
897 clip_stack.clear();
898 clip_stack.push(full_entry);
899 blur_passes.clear();
900 *active_batches = 0;
901 *has_unsupported_effect = false;
902 let layer_scroll: [f32; 2] = *active_scroll_offset;
903 let layer_id: u8 = *active_layer_id;
904 let gpu_supports_blur: bool = driver.supports_backdrop_blur_pass();
905 let mut tail_state: GpuBatchCoalesceState = GpuBatchCoalesceState {
906 scroll_offset: layer_scroll,
907 layer_id,
908 ..GpuBatchCoalesceState::default()
909 };
910 let mut coalesced_merges: usize = 0;
911 let mut culled_instances: usize = 0;
912 let mut uploaded_instances: usize = 0;
913 let mut clear_color: Color = COLOR_WHITE;
914 for cmd in display_list.get_commands().iter() {
915 let clip_entry: GpuClipEntry = clip_stack.last().copied().unwrap_or(full_entry);
916 let active_clip: Rect = clip_entry.scissor;
917 match cmd {
918 DrawCommand::Clear(color_item) => {
919 clear_color = *color_item;
920 }
921 DrawCommand::PushClip(rect) => {
922 let phys: Rect = snap_physical_rect(*rect, scale);
923 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
924 let next_safe: Rect = clip_entry.safe_inner.intersect(phys).unwrap_or_default();
925 let next_bits: u16 = GpuPrimitiveInstance::encode_rounded_clip_bits(
926 active_clip,
927 clip_entry.rounded_clip_bits,
928 next_scissor,
929 phys,
930 None,
931 );
932 clip_stack.push(GpuClipEntry {
933 scissor: next_scissor,
934 safe_inner: next_safe,
935 rounded_clip_bits: next_bits,
936 });
937 }
938 DrawCommand::PushRoundedClip { rect, radius } => {
939 let phys: Rect = snap_physical_rect(*rect, scale);
940 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
941 let phys_radius: CornerRadius = CornerRadius::new(
942 radius.get_top_left() * scale,
943 radius.get_top_right() * scale,
944 radius.get_bottom_right() * scale,
945 radius.get_bottom_left() * scale,
946 );
947 let max_r: f32 = phys_radius.max_radius().ceil();
948 let inset_w: f32 = (phys.get_width() - max_r * 2.0).max(0.0);
949 let inset_h: f32 = (phys.get_height() - max_r * 2.0).max(0.0);
950 let rounded_safe: Rect =
951 Rect::new(phys.get_x() + max_r, phys.get_y() + max_r, inset_w, inset_h);
952 let next_safe: Rect = clip_entry
953 .safe_inner
954 .intersect(rounded_safe)
955 .unwrap_or_default();
956 let next_bits: u16 = GpuPrimitiveInstance::encode_rounded_clip_bits(
957 active_clip,
958 clip_entry.rounded_clip_bits,
959 next_scissor,
960 phys,
961 Some(phys_radius),
962 );
963 clip_stack.push(GpuClipEntry {
964 scissor: next_scissor,
965 safe_inner: next_safe,
966 rounded_clip_bits: next_bits,
967 });
968 }
969 DrawCommand::PopClip => {
970 if clip_stack.len() > 1 {
971 let _: Option<GpuClipEntry> = clip_stack.pop();
972 }
973 }
974 DrawCommand::FillRoundedRect {
975 rect,
976 color,
977 radius,
978 border,
979 } => {
980 let snapped: Rect = snap_physical_rect(*rect, scale);
981 let quad: [f32; 4] = [
982 snapped.get_x(),
983 snapped.get_y(),
984 snapped.get_width(),
985 snapped.get_height(),
986 ];
987 if !Self::quad_intersects_clip(quad, active_clip) {
988 culled_instances += 1;
989 continue;
990 }
991 let border_width: f32 = if border.get_width() > 0.0 {
992 (border.get_width() * scale).max(1.0)
993 } else {
994 0.0
995 };
996 let inst: GpuQuadInstance = GpuQuadInstance {
997 rect: quad,
998 clip: [
999 active_clip.get_x(),
1000 active_clip.get_y(),
1001 active_clip.right(),
1002 active_clip.bottom(),
1003 ],
1004 color: color.to_f32_array(),
1005 secondary_color: border.get_color().to_f32_array(),
1006 radii: [
1007 radius.get_top_left() * scale,
1008 radius.get_top_right() * scale,
1009 radius.get_bottom_right() * scale,
1010 radius.get_bottom_left() * scale,
1011 ],
1012 stroke_or_blur: border_width,
1013 angle_rad: 0.0,
1014 kind: GpuPrimitiveKind::RoundedRect as u32,
1015 reserved: 0,
1016 };
1017 Self::push_instance_coalesced(
1018 batches,
1019 active_batches,
1020 &mut tail_state,
1021 clip_entry,
1022 inst,
1023 &mut coalesced_merges,
1024 );
1025 uploaded_instances += 1;
1026 }
1027 DrawCommand::LinearGradientRect {
1028 rect,
1029 gradient,
1030 radius,
1031 } => {
1032 let snapped: Rect = snap_physical_rect(*rect, scale);
1033 let quad: [f32; 4] = [
1034 snapped.get_x(),
1035 snapped.get_y(),
1036 snapped.get_width(),
1037 snapped.get_height(),
1038 ];
1039 if !Self::quad_intersects_clip(quad, active_clip) {
1040 culled_instances += 1;
1041 continue;
1042 }
1043 let inst: GpuQuadInstance = GpuQuadInstance {
1044 rect: quad,
1045 clip: [
1046 active_clip.get_x(),
1047 active_clip.get_y(),
1048 active_clip.right(),
1049 active_clip.bottom(),
1050 ],
1051 color: gradient.get_start_color().to_f32_array(),
1052 secondary_color: gradient.get_end_color().to_f32_array(),
1053 radii: [
1054 radius.get_top_left() * scale,
1055 radius.get_top_right() * scale,
1056 radius.get_bottom_right() * scale,
1057 radius.get_bottom_left() * scale,
1058 ],
1059 stroke_or_blur: 0.0,
1060 angle_rad: gradient.get_angle_deg().to_radians(),
1061 kind: GpuPrimitiveKind::Gradient as u32,
1062 reserved: 0,
1063 };
1064 Self::push_instance_coalesced(
1065 batches,
1066 active_batches,
1067 &mut tail_state,
1068 clip_entry,
1069 inst,
1070 &mut coalesced_merges,
1071 );
1072 uploaded_instances += 1;
1073 }
1074 DrawCommand::BoxShadow {
1075 rect,
1076 radius,
1077 shadow,
1078 } => {
1079 let spread: f32 = shadow.get_spread_radius() * scale;
1080 let blur: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
1081 let sigma: f32 = (blur * 0.5).max(0.75);
1082 let pad: f32 = (sigma * 3.0).ceil();
1083 let base_rect: Rect = snap_physical_rect(*rect, scale);
1084 let caster_x: f32 =
1085 base_rect.get_x() + (shadow.get_offset_x() * scale).round() - spread;
1086 let caster_y: f32 =
1087 base_rect.get_y() + (shadow.get_offset_y() * scale).round() - spread;
1088 let caster_w: f32 = (base_rect.get_width() + spread * 2.0).max(1.0);
1089 let caster_h: f32 = (base_rect.get_height() + spread * 2.0).max(1.0);
1090 let quad: [f32; 4] = [
1091 caster_x - pad,
1092 caster_y - pad,
1093 caster_w + pad * 2.0,
1094 caster_h + pad * 2.0,
1095 ];
1096 if !Self::quad_intersects_clip(quad, active_clip) {
1097 culled_instances += 1;
1098 continue;
1099 }
1100 let inst: GpuQuadInstance = GpuQuadInstance {
1101 rect: quad,
1102 clip: [
1103 active_clip.get_x(),
1104 active_clip.get_y(),
1105 active_clip.right(),
1106 active_clip.bottom(),
1107 ],
1108 color: shadow.get_color().to_f32_array(),
1109 secondary_color: [0.0; 4],
1110 radii: [
1111 (radius.get_top_left() * scale + spread).max(0.0),
1112 (radius.get_top_right() * scale + spread).max(0.0),
1113 (radius.get_bottom_right() * scale + spread).max(0.0),
1114 (radius.get_bottom_left() * scale + spread).max(0.0),
1115 ],
1116 stroke_or_blur: sigma,
1117 angle_rad: pad,
1118 kind: GpuPrimitiveKind::Shadow as u32,
1119 reserved: 0,
1120 };
1121 Self::push_instance_coalesced(
1122 batches,
1123 active_batches,
1124 &mut tail_state,
1125 clip_entry,
1126 inst,
1127 &mut coalesced_merges,
1128 );
1129 uploaded_instances += 1;
1130 }
1131 DrawCommand::BackdropBlur {
1132 rect,
1133 radius,
1134 blur_radius,
1135 } => {
1136 if !gpu_supports_blur {
1137 *has_unsupported_effect = true;
1138 }
1139 let snapped: Rect = snap_physical_rect(*rect, scale);
1140 let quad: [f32; 4] = [
1141 snapped.get_x(),
1142 snapped.get_y(),
1143 snapped.get_width(),
1144 snapped.get_height(),
1145 ];
1146 if !Self::quad_intersects_clip(quad, active_clip) {
1147 culled_instances += 1;
1148 continue;
1149 }
1150 let phys_blur: f32 = (*blur_radius * scale).clamp(0.5, 96.0);
1151 let radii_px: [f32; 4] = [
1152 radius.get_top_left() * scale,
1153 radius.get_top_right() * scale,
1154 radius.get_bottom_right() * scale,
1155 radius.get_bottom_left() * scale,
1156 ];
1157 let halo: Rect = Rect::new(
1158 (snapped.get_x() - phys_blur).max(0.0),
1159 (snapped.get_y() - phys_blur).max(0.0),
1160 (snapped.get_width() + phys_blur * 2.0).min(phys_w as f32),
1161 (snapped.get_height() + phys_blur * 2.0).min(phys_h as f32),
1162 );
1163 blur_passes.push(GpuBackdropBlurPass {
1164 bounds: snapped,
1165 input_bounds: halo,
1166 clip_rect: active_clip,
1167 radii: radii_px,
1168 blur_radius: phys_blur,
1169 layer_id,
1170 });
1171 let inst: GpuQuadInstance = GpuQuadInstance {
1172 rect: quad,
1173 clip: [
1174 active_clip.get_x(),
1175 active_clip.get_y(),
1176 active_clip.right(),
1177 active_clip.bottom(),
1178 ],
1179 color: [1.0, 1.0, 1.0, 1.0],
1180 secondary_color: [0.0; 4],
1181 radii: radii_px,
1182 stroke_or_blur: phys_blur,
1183 angle_rad: 0.0,
1184 kind: GpuPrimitiveKind::BackdropBlur as u32,
1185 reserved: 0,
1186 };
1187 Self::push_instance_coalesced(
1188 batches,
1189 active_batches,
1190 &mut tail_state,
1191 clip_entry,
1192 inst,
1193 &mut coalesced_merges,
1194 );
1195 uploaded_instances += 1;
1196 }
1197 DrawCommand::TextRun { rect, text, style } => {
1198 let run_quad: [f32; 4] = [
1199 rect.get_x() * scale,
1200 rect.get_y() * scale,
1201 rect.get_width() * scale,
1202 rect.get_height() * scale,
1203 ];
1204 if !Self::quad_intersects_clip(run_quad, active_clip) {
1205 culled_instances += 1;
1206 continue;
1207 }
1208 let glyph_count: usize = if text.is_ascii() {
1209 text.as_bytes()
1210 .iter()
1211 .filter(|byte_item: &&u8| **byte_item != b' ')
1212 .count()
1213 .max(1)
1214 } else {
1215 text.chars()
1216 .filter(|char_item: &char| *char_item != ' ')
1217 .count()
1218 .max(1)
1219 };
1220 let text_instance: GpuQuadInstance = GpuQuadInstance {
1221 rect: run_quad,
1222 clip: [
1223 active_clip.get_x(),
1224 active_clip.get_y(),
1225 active_clip.right(),
1226 active_clip.bottom(),
1227 ],
1228 color: style.get_color().to_f32_array(),
1229 secondary_color: [0.0; 4],
1230 radii: [0.0; 4],
1231 stroke_or_blur: style.get_font_size() * scale,
1232 angle_rad: 0.0,
1233 kind: GpuPrimitiveKind::Glyph as u32,
1234 reserved: glyph_count as u32,
1235 };
1236 Self::push_instance_coalesced(
1237 batches,
1238 active_batches,
1239 &mut tail_state,
1240 clip_entry,
1241 text_instance,
1242 &mut coalesced_merges,
1243 );
1244 uploaded_instances += 1;
1245 }
1246 DrawCommand::TextCaret {
1247 text_rect,
1248 content_rect,
1249 text,
1250 style,
1251 caret_height,
1252 color,
1253 multiline,
1254 } => {
1255 if active_clip.get_width() <= 0.0 || active_clip.get_height() <= 0.0 {
1256 culled_instances += 1;
1257 continue;
1258 }
1259 let meas: TextLayoutResult = DeterministicTextMeasurer.measure(
1260 text,
1261 *style,
1262 if *multiline {
1263 Some(content_rect.get_width().max(1.0))
1264 } else {
1265 None
1266 },
1267 );
1268 let phys_font_px: f32 = (style.get_font_size() * scale).max(6.0);
1269 let letter_sp: f32 = style.get_letter_spacing() * scale;
1270 let last_w_phys: f32 = if let Some(line) = meas.get_lines().last() {
1271 let font_provider: NativeSystemFont = readback.get_font_provider().clone();
1272 let atlas: &mut GlyphAtlas = readback.get_mut_glyph_atlas();
1273 let mut acc: f32 = 0.0;
1274 for ch in line.get_text().chars() {
1275 if ch == ' ' || ch == '\t' {
1276 acc += measure_char_advance(ch, *style) * scale + letter_sp;
1277 } else {
1278 let key: GlyphKey =
1279 GlyphKey::from_text_style(ch, phys_font_px, *style);
1280 let green_val: &RasterizedGlyph =
1281 atlas.get_or_rasterize_with(key, &font_provider);
1282 acc += (green_val.get_advance() as f32).max(1.0) + letter_sp;
1283 }
1284 }
1285 acc
1286 } else {
1287 0.0
1288 };
1289 let line_idx: usize = meas.get_lines().len().saturating_sub(1);
1290 let trailing_gap_phys: f32 = if last_w_phys > 0.0 { 1.0 * scale } else { 0.0 };
1291 let min_x_phys: f32 = content_rect.get_x() * scale;
1292 let max_x_phys: f32 = ((content_rect.right() - 2.0) * scale).max(min_x_phys);
1293 let caret_x_phys: f32 =
1294 (text_rect.get_x() * scale + last_w_phys + trailing_gap_phys)
1295 .min(max_x_phys)
1296 .max(min_x_phys);
1297 let caret_y: f32 = if *multiline {
1298 (content_rect.get_y()
1299 + (line_idx as f32) * meas.get_line_height_px()
1300 + ((meas.get_line_height_px() - caret_height) * 0.5).max(0.0))
1301 .min(content_rect.bottom() - caret_height)
1302 .max(content_rect.get_y())
1303 } else {
1304 content_rect.get_y()
1305 + ((content_rect.get_height() - caret_height) * 0.5).max(0.0)
1306 };
1307 let quad: [f32; 4] = [
1308 caret_x_phys.round(),
1309 (caret_y * scale).round(),
1310 (1.5 * scale).max(1.0),
1311 (caret_height * scale).max(2.0),
1312 ];
1313 if !Self::quad_intersects_clip(quad, active_clip) {
1314 culled_instances += 1;
1315 continue;
1316 }
1317 let inst: GpuQuadInstance = GpuQuadInstance {
1318 rect: quad,
1319 clip: [
1320 active_clip.get_x(),
1321 active_clip.get_y(),
1322 active_clip.right(),
1323 active_clip.bottom(),
1324 ],
1325 color: color.to_f32_array(),
1326 secondary_color: [0.0; 4],
1327 radii: [0.5 * scale; 4],
1328 stroke_or_blur: 0.0,
1329 angle_rad: 0.0,
1330 kind: GpuPrimitiveKind::RoundedRect as u32,
1331 reserved: 0,
1332 };
1333 Self::push_instance_coalesced(
1334 batches,
1335 active_batches,
1336 &mut tail_state,
1337 clip_entry,
1338 inst,
1339 &mut coalesced_merges,
1340 );
1341 uploaded_instances += 1;
1342 }
1343 DrawCommand::Image { rect, radius, .. }
1344 | DrawCommand::SharedImage { rect, radius, .. } => {
1345 let snapped: Rect = snap_physical_rect(*rect, scale);
1346 let quad: [f32; 4] = [
1347 snapped.get_x(),
1348 snapped.get_y(),
1349 snapped.get_width(),
1350 snapped.get_height(),
1351 ];
1352 if !Self::quad_intersects_clip(quad, active_clip) {
1353 culled_instances += 1;
1354 continue;
1355 }
1356 let inst: GpuQuadInstance = GpuQuadInstance {
1357 rect: quad,
1358 clip: [
1359 active_clip.get_x(),
1360 active_clip.get_y(),
1361 active_clip.right(),
1362 active_clip.bottom(),
1363 ],
1364 color: [1.0, 1.0, 1.0, 1.0],
1365 secondary_color: [0.0; 4],
1366 radii: [
1367 radius.get_top_left() * scale,
1368 radius.get_top_right() * scale,
1369 radius.get_bottom_right() * scale,
1370 radius.get_bottom_left() * scale,
1371 ],
1372 stroke_or_blur: 0.0,
1373 angle_rad: 0.0,
1374 kind: GpuPrimitiveKind::TexturedQuad as u32,
1375 reserved: 0,
1376 };
1377 Self::push_instance_coalesced(
1378 batches,
1379 active_batches,
1380 &mut tail_state,
1381 clip_entry,
1382 inst,
1383 &mut coalesced_merges,
1384 );
1385 uploaded_instances += 1;
1386 }
1387 DrawCommand::Circle {
1388 center,
1389 radius,
1390 color,
1391 } => {
1392 let red_val: f32 = radius * scale;
1393 let pad: f32 = 1.5;
1394 let extent: f32 = red_val + pad;
1395 let quad: [f32; 4] = [
1396 center.get_x() * scale - extent,
1397 center.get_y() * scale - extent,
1398 extent * 2.0,
1399 extent * 2.0,
1400 ];
1401 if !Self::quad_intersects_clip(quad, active_clip) {
1402 culled_instances += 1;
1403 continue;
1404 }
1405 let inst: GpuQuadInstance = GpuQuadInstance {
1406 rect: quad,
1407 clip: [
1408 active_clip.get_x(),
1409 active_clip.get_y(),
1410 active_clip.right(),
1411 active_clip.bottom(),
1412 ],
1413 color: color.to_f32_array(),
1414 secondary_color: [0.0; 4],
1415 radii: [red_val; 4],
1416 stroke_or_blur: red_val,
1417 angle_rad: 0.0,
1418 kind: GpuPrimitiveKind::Circle as u32,
1419 reserved: 0,
1420 };
1421 Self::push_instance_coalesced(
1422 batches,
1423 active_batches,
1424 &mut tail_state,
1425 clip_entry,
1426 inst,
1427 &mut coalesced_merges,
1428 );
1429 uploaded_instances += 1;
1430 }
1431 DrawCommand::Line {
1432 from,
1433 to,
1434 color,
1435 width,
1436 } => {
1437 let fx0: f32 = from.get_x() * scale;
1438 let fy0: f32 = from.get_y() * scale;
1439 let fx1: f32 = to.get_x() * scale;
1440 let fy1: f32 = to.get_y() * scale;
1441 let dx: f32 = fx1 - fx0;
1442 let dy: f32 = fy1 - fy0;
1443 let stroke_px: f32 = (width * scale).max(1.0);
1444 let pad: f32 = stroke_px * 0.5 + 1.5;
1445 let min_x: f32 = fx0.min(fx1) - pad;
1446 let min_y: f32 = fy0.min(fy1) - pad;
1447 let max_x: f32 = fx0.max(fx1) + pad;
1448 let max_y: f32 = fy0.max(fy1) + pad;
1449 let quad: [f32; 4] = [
1450 min_x,
1451 min_y,
1452 (max_x - min_x).max(2.0),
1453 (max_y - min_y).max(2.0),
1454 ];
1455 if !Self::quad_intersects_clip(quad, active_clip) {
1456 culled_instances += 1;
1457 continue;
1458 }
1459 let inst: GpuQuadInstance = GpuQuadInstance {
1460 rect: quad,
1461 clip: [
1462 active_clip.get_x(),
1463 active_clip.get_y(),
1464 active_clip.right(),
1465 active_clip.bottom(),
1466 ],
1467 color: color.to_f32_array(),
1468 secondary_color: [0.0; 4],
1469 radii: [fx0, fy0, fx1, fy1],
1470 stroke_or_blur: stroke_px,
1471 angle_rad: dy.atan2(dx),
1472 kind: GpuPrimitiveKind::Line as u32,
1473 reserved: 0,
1474 };
1475 Self::push_instance_coalesced(
1476 batches,
1477 active_batches,
1478 &mut tail_state,
1479 clip_entry,
1480 inst,
1481 &mut coalesced_merges,
1482 );
1483 uploaded_instances += 1;
1484 }
1485 DrawCommand::Triangle { p0, p1, p2, color } => {
1486 let min_x: f32 = p0.get_x().min(p1.get_x()).min(p2.get_x()) * scale - 1.5;
1487 let min_y: f32 = p0.get_y().min(p1.get_y()).min(p2.get_y()) * scale - 1.5;
1488 let max_x: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x()) * scale + 1.5;
1489 let max_y: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y()) * scale + 1.5;
1490 let quad: [f32; 4] = [
1491 min_x,
1492 min_y,
1493 (max_x - min_x).max(2.0),
1494 (max_y - min_y).max(2.0),
1495 ];
1496 if !Self::quad_intersects_clip(quad, active_clip) {
1497 culled_instances += 1;
1498 continue;
1499 }
1500 let inst: GpuQuadInstance = GpuQuadInstance {
1501 rect: quad,
1502 clip: [
1503 active_clip.get_x(),
1504 active_clip.get_y(),
1505 active_clip.right(),
1506 active_clip.bottom(),
1507 ],
1508 color: color.to_f32_array(),
1509 secondary_color: [p0.get_x() * scale, p0.get_y() * scale, 0.0, 0.0],
1510 radii: [
1511 p1.get_x() * scale,
1512 p1.get_y() * scale,
1513 p2.get_x() * scale,
1514 p2.get_y() * scale,
1515 ],
1516 stroke_or_blur: 0.0,
1517 angle_rad: 0.0,
1518 kind: GpuPrimitiveKind::Triangle as u32,
1519 reserved: 3,
1520 };
1521 Self::push_instance_coalesced(
1522 batches,
1523 active_batches,
1524 &mut tail_state,
1525 clip_entry,
1526 inst,
1527 &mut coalesced_merges,
1528 );
1529 uploaded_instances += 1;
1530 }
1531 DrawCommand::SpriteRegion { rect, tint, .. } => {
1532 let snapped: Rect = snap_physical_rect(*rect, scale);
1533 let quad: [f32; 4] = [
1534 snapped.get_x(),
1535 snapped.get_y(),
1536 snapped.get_width(),
1537 snapped.get_height(),
1538 ];
1539 if !Self::quad_intersects_clip(quad, active_clip) {
1540 culled_instances += 1;
1541 continue;
1542 }
1543 let inst: GpuQuadInstance = GpuQuadInstance {
1544 rect: quad,
1545 clip: [
1546 active_clip.get_x(),
1547 active_clip.get_y(),
1548 active_clip.right(),
1549 active_clip.bottom(),
1550 ],
1551 color: tint.to_f32_array(),
1552 secondary_color: [0.0; 4],
1553 radii: [0.0; 4],
1554 stroke_or_blur: 0.0,
1555 angle_rad: 0.0,
1556 kind: GpuPrimitiveKind::TexturedQuad as u32,
1557 reserved: 1,
1558 };
1559 Self::push_instance_coalesced(
1560 batches,
1561 active_batches,
1562 &mut tail_state,
1563 clip_entry,
1564 inst,
1565 &mut coalesced_merges,
1566 );
1567 uploaded_instances += 1;
1568 }
1569 }
1570 }
1571 let prim_upload_bytes: usize =
1572 uploaded_instances.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
1573 let upload_bytes: usize = prim_upload_bytes;
1574 let acquired_fence: u64 = match resource_ring
1575 .acquire_staging(prim_upload_bytes, GpuUploadPriority::RequiredFrame)
1576 {
1577 GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
1578 GpuRingAcquireResult::DeferredFrameRingFull
1579 | GpuRingAcquireResult::DroppedPrefetchRingFull => {
1580 let prev: u64 =
1581 GpuResourceRing::next_monotonic_fence(resource_ring.get_completed_fence_id());
1582 let _: usize = resource_ring.complete_fence(prev);
1583 match resource_ring
1584 .acquire_staging(prim_upload_bytes, GpuUploadPriority::RequiredFrame)
1585 {
1586 GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
1587 GpuRingAcquireResult::DeferredFrameRingFull
1588 | GpuRingAcquireResult::DroppedPrefetchRingFull => {
1589 resource_ring.get_next_fence_id()
1590 }
1591 }
1592 }
1593 };
1594 let active_slice: &[GpuBatch] = &batches[..(*active_batches).min(batches.len())];
1595 let submitted: usize = driver.submit_batches(phys_w, phys_h, clear_color, active_slice)?;
1596 *last_fence_id = acquired_fence;
1597 let retire_window: u64 = acquired_fence.saturating_sub(2);
1598 if retire_window > 0 {
1599 let _: usize = resource_ring.complete_fence(retire_window);
1600 }
1601 *last_submitted_batches = submitted;
1602 *last_coalesced_merges = coalesced_merges;
1603 *last_culled_instances = culled_instances;
1604 *last_uploaded_instances = uploaded_instances;
1605 *last_uploaded_bytes = upload_bytes;
1606 *last_uploaded_primitive_bytes = prim_upload_bytes;
1607 let pixels: usize = if *enable_readback {
1608 let (_, px): (usize, usize) = readback.render(display_list, damage);
1609 *cpu_readback_frames = cpu_readback_frames.saturating_add(1);
1610 *last_cpu_rgba_upload_bytes = (phys_w as usize)
1611 .saturating_mul(phys_h as usize)
1612 .saturating_mul(4);
1613 px
1614 } else {
1615 *direct_gpu_frames = direct_gpu_frames.saturating_add(1);
1616 *last_cpu_rgba_upload_bytes = 0;
1617 (phys_w as usize).saturating_mul(phys_h as usize)
1618 };
1619 Ok((submitted, pixels))
1620 }
1621
1622 fn push_instance_coalesced(
1633 batches: &mut Vec<GpuBatch>,
1634 active_batches: &mut usize,
1635 tail_state: &mut GpuBatchCoalesceState,
1636 clip_entry: GpuClipEntry,
1637 instance: GpuQuadInstance,
1638 coalesced_merges: &mut usize,
1639 ) {
1640 let scissor: Rect = clip_entry.scissor;
1641 let is_blur: bool = instance.kind == GpuPrimitiveKind::BackdropBlur as u32;
1642 let inst_unclipped: bool =
1643 !is_blur && Self::quad_inside_rect(instance.rect, clip_entry.safe_inner);
1644 let base_prim: GpuPrimitiveInstance =
1645 GpuPrimitiveInstance::from_quad_instance(&instance, 0, tail_state.layer_id);
1646 let prim: GpuPrimitiveInstance = if inst_unclipped {
1647 base_prim
1648 } else {
1649 base_prim.with_rounded_clip_bits(clip_entry.rounded_clip_bits)
1650 };
1651 let count: usize = *active_batches;
1652 if count > 0 {
1653 let last: &mut GpuBatch = &mut batches[count - 1];
1654 if last.get_instances().len() < MAX_QUADS_PER_BATCH
1655 && last.get_layer_id() == tail_state.layer_id
1656 && last.get_scroll_offset() == tail_state.scroll_offset
1657 {
1658 let last_scissor: Rect = last.get_scissor();
1659 let blur_boundary: bool = tail_state.has_backdrop_blur || is_blur;
1660 if last_scissor == scissor && !blur_boundary {
1661 last.get_mut_instances().push(instance);
1662 last.get_mut_primitives().push(prim);
1663 tail_state.all_unclipped = tail_state.all_unclipped && inst_unclipped;
1664 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
1665 return;
1666 }
1667 if !blur_boundary
1668 && inst_unclipped
1669 && Self::quad_inside_rect(instance.rect, last_scissor)
1670 {
1671 last.get_mut_instances().push(instance);
1672 last.get_mut_primitives().push(prim);
1673 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
1674 *coalesced_merges += 1;
1675 return;
1676 }
1677 if !blur_boundary
1678 && tail_state.all_unclipped
1679 && Self::rect_inside_rect(tail_state.bounds, scissor)
1680 {
1681 last.set_scissor(scissor);
1682 last.get_mut_instances().push(instance);
1683 last.get_mut_primitives().push(prim);
1684 tail_state.all_unclipped = inst_unclipped;
1685 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, instance.rect);
1686 *coalesced_merges += 1;
1687 return;
1688 }
1689 }
1690 }
1691 if count < batches.len() {
1692 let slot: &mut GpuBatch = &mut batches[count];
1693 slot.set_scissor(scissor);
1694 slot.set_scroll_offset(tail_state.scroll_offset);
1695 slot.set_layer_id(tail_state.layer_id);
1696 slot.get_mut_instances().clear();
1697 slot.get_mut_instances().push(instance);
1698 slot.get_mut_primitives().clear();
1699 slot.get_mut_primitives().push(prim);
1700 } else {
1701 let mut batch: GpuBatch = GpuBatch::default();
1702 batch.set_scissor(scissor);
1703 batch.set_scroll_offset(tail_state.scroll_offset);
1704 batch.set_layer_id(tail_state.layer_id);
1705 batch.get_mut_instances().reserve(512);
1706 batch.get_mut_primitives().reserve(512);
1707 batch.get_mut_instances().push(instance);
1708 batch.get_mut_primitives().push(prim);
1709 batches.push(batch);
1710 }
1711 *active_batches = count + 1;
1712 tail_state.all_unclipped = inst_unclipped;
1713 tail_state.has_backdrop_blur = is_blur;
1714 tail_state.bounds = Rect::new(
1715 instance.rect[0],
1716 instance.rect[1],
1717 instance.rect[2],
1718 instance.rect[3],
1719 );
1720 }
1721}