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]
135 pub fn f16_bits_to_f32(bits: u16) -> f32 {
136 let sign: u32 = ((bits as u32) & 0x8000) << 16;
137 let exp: u32 = ((bits as u32) >> 10) & 0x1F;
138 let frac: u32 = (bits as u32) & 0x03FF;
139 if exp == 0 {
140 if frac == 0 {
141 return f32::from_bits(sign);
142 }
143 let val: f32 = (frac as f32) * (1.0 / 16_777_216.0);
144 return if sign != 0 { -val } else { val };
145 }
146 if exp == 31 {
147 return 0.0;
148 }
149 let f32_exp: u32 = (exp + 112) << 23;
150 let f32_frac: u32 = frac << 13;
151 let sign_exp: u32 = sign | f32_exp;
152 f32::from_bits(sign_exp | f32_frac)
153 }
154
155 #[must_use]
165 pub fn unpack_f16x2(packed: u32) -> (f32, f32) {
166 let lo_bits: u16 = (packed & 0xFFFF) as u16;
167 let hi_bits: u16 = ((packed >> 16) & 0xFFFF) as u16;
168 (
169 Self::f16_bits_to_f32(lo_bits),
170 Self::f16_bits_to_f32(hi_bits),
171 )
172 }
173
174 #[must_use]
184 pub fn backdrop_blur_radius_px(&self) -> f32 {
185 let (raw_blur, _): (f32, f32) = Self::unpack_f16x2(self.get_extra_param());
186 if raw_blur.is_finite() && raw_blur > 0.0 {
187 raw_blur.clamp(0.5, 96.0)
188 } else {
189 10.0
190 }
191 }
192
193 #[must_use]
203 pub fn backdrop_blur_offset(&self) -> f32 {
204 (self.backdrop_blur_radius_px() * 0.25).clamp(0.5, 8.0)
205 }
206
207 #[must_use]
220 pub fn backdrop_blur_clip_uv(
221 &self,
222 target_dims: (f32, f32),
223 tile_origin: [f32; 2],
224 flip_y_gl: bool,
225 ) -> [f32; 4] {
226 let vw: f32 = target_dims.0.max(1.0);
227 let vh: f32 = target_dims.1.max(1.0);
228 let clip: [f32; 4] = self.get_clip_rect();
229 let lx0: f32 = (clip[0] - tile_origin[0]).clamp(0.0, vw);
230 let lx1: f32 = (clip[2] - tile_origin[0]).clamp(lx0, vw);
231 let ly0: f32 = (clip[1] - tile_origin[1]).clamp(0.0, vh);
232 let ly1: f32 = (clip[3] - tile_origin[1]).clamp(ly0, vh);
233 if lx1 <= lx0 || ly1 <= ly0 {
234 return [0.0; 4];
235 }
236 let u0: f32 = lx0 / vw;
237 let u1: f32 = lx1 / vw;
238 if flip_y_gl {
239 let v_bot: f32 = (1.0 - ly1 / vh).clamp(0.0, 1.0);
240 let v_top: f32 = (1.0 - ly0 / vh).clamp(v_bot, 1.0);
241 [u0, v_bot, u1, v_top]
242 } else {
243 let v0: f32 = (ly0 / vh).clamp(0.0, 1.0);
244 let v1: f32 = (ly1 / vh).clamp(v0, 1.0);
245 [u0, v0, u1, v1]
246 }
247 }
248
249 #[must_use]
262 pub const fn pack_kind_flags(
263 kind: PrimKind,
264 clip_mask_or_sub: u16,
265 tex_slot: u8,
266 layer_id: u8,
267 ) -> u32 {
268 ((kind as u32) & 0xF)
269 | (((clip_mask_or_sub as u32) & 0xFFF) << 4)
270 | ((tex_slot as u32) << 16)
271 | ((layer_id as u32) << 24)
272 }
273
274 #[must_use]
280 pub fn prim_kind(&self) -> PrimKind {
281 match self.get_kind_flags() & 0xF {
282 1 => PrimKind::LinearGradientRect,
283 2 => PrimKind::Shadow,
284 3 => PrimKind::GlyphAlpha8,
285 4 => PrimKind::GlyphColorRgba8,
286 5 => PrimKind::ImageTexture,
287 6 => PrimKind::VectorShape,
288 7 => PrimKind::ExternalGpuSurface,
289 8 => PrimKind::BackdropBlur,
290 _ => PrimKind::RoundedRect,
291 }
292 }
293
294 #[must_use]
300 pub fn clip_or_sub_kind(&self) -> u16 {
301 ((self.get_kind_flags() >> 4) & 0xFFF) as u16
302 }
303
304 #[must_use]
314 pub fn encode_clip_radius_6bit(radius_px: f32) -> u16 {
315 let clamped: f32 = radius_px.clamp(0.0, 500.0);
316 if clamped <= 15.5 {
317 (clamped * 2.0).round().clamp(0.0, 31.0) as u16
318 } else if clamped <= 76.0 {
319 32_u16 + (((clamped - 16.0) / 4.0).round().clamp(0.0, 15.0) as u16)
320 } else {
321 48_u16 + (((clamped - 80.0) / 28.0).round().clamp(0.0, 15.0) as u16)
322 }
323 }
324
325 #[must_use]
335 pub fn decode_clip_radius_6bit(encoded: u16) -> f32 {
336 let masked: u16 = encoded & 0x3F;
337 if masked <= 31 {
338 (masked as f32) * 0.5
339 } else if masked <= 47 {
340 16.0 + ((masked - 32) as f32) * 4.0
341 } else {
342 80.0 + ((masked - 48) as f32) * 28.0
343 }
344 }
345
346 #[must_use]
360 pub fn encode_rounded_clip_bits(
361 parent_scissor: Rect,
362 parent_bits: u16,
363 next_scissor: Rect,
364 pushed_rect: Rect,
365 pushed_radius_phys: Option<CornerRadius>,
366 ) -> u16 {
367 if next_scissor.get_width() <= 0.0 || next_scissor.get_height() <= 0.0 {
368 return 0;
369 }
370 let parent_mask: u16 = parent_bits & 0xF;
371 let parent_q_rad: u16 = (parent_bits >> 4) & 0x3F;
372 let mut mask: u16 = 0;
373 if parent_mask != 0 && parent_q_rad > 0 {
374 let same_left: bool = (next_scissor.get_x() - parent_scissor.get_x()).abs() <= 0.5;
375 let same_top: bool = (next_scissor.get_y() - parent_scissor.get_y()).abs() <= 0.5;
376 let same_right: bool = (next_scissor.right() - parent_scissor.right()).abs() <= 0.5;
377 let same_bottom: bool = (next_scissor.bottom() - parent_scissor.bottom()).abs() <= 0.5;
378 if (parent_mask & 1) != 0 && same_left && same_top {
379 mask |= 1;
380 }
381 if (parent_mask & 2) != 0 && same_right && same_top {
382 mask |= 2;
383 }
384 if (parent_mask & 4) != 0 && same_right && same_bottom {
385 mask |= 4;
386 }
387 if (parent_mask & 8) != 0 && same_left && same_bottom {
388 mask |= 8;
389 }
390 }
391 let mut q_rad: u16 = if mask != 0 { parent_q_rad } else { 0 };
392 if let Some(rad) = pushed_radius_phys {
393 let cap_r: f32 = (pushed_rect.get_width() * 0.5)
394 .min(pushed_rect.get_height() * 0.5)
395 .max(0.0);
396 let r_tl: f32 = rad.get_top_left().min(cap_r);
397 let r_tr: f32 = rad.get_top_right().min(cap_r);
398 let r_br: f32 = rad.get_bottom_right().min(cap_r);
399 let r_bl: f32 = rad.get_bottom_left().min(cap_r);
400 let max_pushed_r: f32 = r_tl.max(r_tr).max(r_br).max(r_bl);
401 let pushed_q_rad: u16 = Self::encode_clip_radius_6bit(max_pushed_r);
402 if pushed_q_rad > 0 {
403 let touch_left: bool = (next_scissor.get_x() - pushed_rect.get_x()).abs() <= 0.5;
404 let touch_top: bool = (next_scissor.get_y() - pushed_rect.get_y()).abs() <= 0.5;
405 let touch_right: bool = (next_scissor.right() - pushed_rect.right()).abs() <= 0.5;
406 let touch_bottom: bool =
407 (next_scissor.bottom() - pushed_rect.bottom()).abs() <= 0.5;
408 let mut pushed_mask: u16 = 0;
409 if r_tl > 0.25 && touch_left && touch_top {
410 pushed_mask |= 1;
411 }
412 if r_tr > 0.25 && touch_right && touch_top {
413 pushed_mask |= 2;
414 }
415 if r_br > 0.25 && touch_right && touch_bottom {
416 pushed_mask |= 4;
417 }
418 if r_bl > 0.25 && touch_left && touch_bottom {
419 pushed_mask |= 8;
420 }
421 if pushed_mask != 0 {
422 mask |= pushed_mask;
423 q_rad = q_rad.max(pushed_q_rad);
424 }
425 }
426 }
427 if mask == 0 || q_rad == 0 {
428 0
429 } else {
430 (mask & 0xF) | ((q_rad & 0x3F) << 4)
431 }
432 }
433
434 #[inline(always)]
445 #[must_use]
446 pub fn with_rounded_clip_bits(mut self, rounded_clip_bits: u16) -> Self {
447 if rounded_clip_bits != 0 {
448 let current_flags: u32 = self.get_kind_flags();
449 let next_flags: u32 =
450 (current_flags & !(0x3FF_u32 << 6)) | (((rounded_clip_bits as u32) & 0x3FF) << 6);
451 let _: &mut Self = self.set_kind_flags(next_flags);
452 }
453 self
454 }
455
456 #[must_use]
462 pub fn texture_slot(&self) -> u8 {
463 ((self.get_kind_flags() >> 16) & 0xFF) as u8
464 }
465
466 #[must_use]
472 pub fn layer_id(&self) -> u8 {
473 ((self.get_kind_flags() >> 24) & 0xFF) as u8
474 }
475
476 #[must_use]
488 pub fn from_quad_instance(quad: &GpuQuadInstance, tex_slot: u8, layer_id: u8) -> Self {
489 const {
490 assert!(size_of::<GpuPrimitiveInstance>() == GPU_PRIMITIVE_INSTANCE_BYTES);
491 }
492 let rect: [f32; 4] = quad.get_rect();
493 let clip_rect: [f32; 4] = quad.get_clip();
494 let color_rgba: u32 = Self::pack_rgba8_f32(quad.get_color());
495 let mut aux_color_rgba: u32 = Self::pack_rgba8_f32(quad.get_secondary_color());
496 let mut uv_rect: [u16; 4] = [0, 0, u16::MAX, u16::MAX];
497 let src_radii: [f32; 4] = quad.get_radii();
498 let radius_cap: f32 = Self::corner_radius_cap(rect, quad.get_kind(), quad.get_angle_rad());
499 let mut radii_border: [u16; 4] = [
500 Self::pack_u16_q4(src_radii[0].min(radius_cap)),
501 Self::pack_u16_q4(src_radii[1].min(radius_cap)),
502 Self::pack_u16_q4(src_radii[2].min(radius_cap)),
503 Self::pack_u16_q4(src_radii[3].min(radius_cap)),
504 ];
505 let (prim_kind, sub_kind): (PrimKind, u16) = match quad.get_kind() {
506 1 => (PrimKind::LinearGradientRect, 0),
507 2 => (PrimKind::Shadow, 0),
508 3 => {
509 let is_color_glyph: bool = (quad.get_reserved() & 0x8000_0000) != 0;
510 if is_color_glyph {
511 (PrimKind::GlyphColorRgba8, 0)
512 } else {
513 (PrimKind::GlyphAlpha8, 0)
514 }
515 }
516 4 => (PrimKind::ImageTexture, 0),
517 5 => (PrimKind::VectorShape, VectorSubKind::Circle as u16),
518 6 => {
519 let inv_w: f32 = 1.0 / rect[2].max(1.0);
520 let inv_h: f32 = 1.0 / rect[3].max(1.0);
521 let red_val: [f32; 4] = quad.get_radii();
522 uv_rect = [
523 (((red_val[0] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
524 (((red_val[1] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
525 (((red_val[2] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
526 (((red_val[3] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
527 ];
528 radii_border = [0; 4];
529 (PrimKind::VectorShape, VectorSubKind::Line as u16)
530 }
531 7 => {
532 let inv_w: f32 = 1.0 / rect[2].max(1.0);
533 let inv_h: f32 = 1.0 / rect[3].max(1.0);
534 let p0: [f32; 4] = quad.get_secondary_color();
535 let radius_val: [f32; 4] = quad.get_radii();
536 let p0_nx: f32 = ((p0[0] - rect[0]) * inv_w).clamp(0.0, 1.0);
537 let p0_ny: f32 = ((p0[1] - rect[1]) * inv_h).clamp(0.0, 1.0);
538 aux_color_rgba = Self::pack_rgba8_f32([p0_nx, p0_ny, 0.0, 1.0]);
539 uv_rect = [
540 (((radius_val[0] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
541 (((radius_val[1] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
542 (((radius_val[2] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
543 (((radius_val[3] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
544 ];
545 radii_border = [
546 (p0_nx * 65535.0).round() as u16,
547 (p0_ny * 65535.0).round() as u16,
548 0,
549 0,
550 ];
551 (PrimKind::VectorShape, VectorSubKind::Triangle as u16)
552 }
553 8 => (PrimKind::BackdropBlur, 0),
554 9 => (PrimKind::ExternalGpuSurface, 0),
555 _ => (PrimKind::RoundedRect, 0),
556 };
557 let kind_flags: u32 = Self::pack_kind_flags(prim_kind, sub_kind, tex_slot, layer_id);
558 let extra_param: u32 = Self::pack_f16x2(quad.get_stroke_or_blur(), quad.get_angle_rad());
559 Self {
560 rect,
561 clip_rect,
562 color_rgba,
563 aux_color_rgba,
564 uv_rect,
565 radii_border,
566 kind_flags,
567 extra_param,
568 }
569 }
570
571 #[must_use]
586 pub fn external_surface(
587 bounds: Rect,
588 clip: Rect,
589 radii: [f32; 4],
590 uv_rect: [u16; 4],
591 tex_slot: u8,
592 layer_id: u8,
593 ) -> Self {
594 Self {
595 rect: [
596 bounds.get_x(),
597 bounds.get_y(),
598 bounds.get_width(),
599 bounds.get_height(),
600 ],
601 clip_rect: [clip.get_x(), clip.get_y(), clip.right(), clip.bottom()],
602 color_rgba: 0xFFFF_FFFF,
603 aux_color_rgba: 0,
604 uv_rect,
605 radii_border: [
606 Self::pack_u16_q4(radii[0]),
607 Self::pack_u16_q4(radii[1]),
608 Self::pack_u16_q4(radii[2]),
609 Self::pack_u16_q4(radii[3]),
610 ],
611 kind_flags: Self::pack_kind_flags(PrimKind::ExternalGpuSurface, 0, tex_slot, layer_id),
612 extra_param: 0,
613 }
614 }
615}
616
617impl GpuLayerUniform {
619 #[must_use]
631 pub fn new(viewport_w: f32, viewport_h: f32, scroll_offset: [f32; 2]) -> Self {
632 const {
633 assert!(size_of::<GpuLayerUniform>() == GPU_LAYER_UNIFORM_BYTES);
634 }
635 Self {
636 viewport_size: [viewport_w.max(1.0), viewport_h.max(1.0)],
637 scroll_offset,
638 }
639 }
640}
641
642impl GpuDriver for RecordingGpuDriver {
644 fn backend(&self) -> GraphicsBackend {
654 self.get_backend_kind()
655 }
656
657 fn shader_source(&self) -> &'static str {
667 match self.get_backend_kind() {
668 GraphicsBackend::Metal => MSL_UI_SHADER,
669 GraphicsBackend::Direct3D12 | GraphicsBackend::Direct3D11 => HLSL_UI_SHADER,
670 GraphicsBackend::WebGl2 | GraphicsBackend::OpenGlEs => GLSL_ES3_UI_FRAGMENT_SHADER,
671 GraphicsBackend::WebGpu
672 | GraphicsBackend::Vulkan
673 | GraphicsBackend::SoftwareCpu
674 | GraphicsBackend::Canvas2d
675 | GraphicsBackend::Headless => WGSL_UI_SHADER,
676 }
677 }
678
679 fn submit_batches(
693 &mut self,
694 _width: u32,
695 _height: u32,
696 clear_color: Color,
697 batches: &[GpuBatch],
698 ) -> Result<usize, PlatformError> {
699 let count: usize = batches.len();
700 let instances: usize = batches
701 .iter()
702 .fold(0_usize, |acc: usize, blue_val: &GpuBatch| {
703 acc + blue_val
704 .get_primitives()
705 .len()
706 .max(blue_val.get_instances().len())
707 });
708 self.set_last_clear(clear_color);
709 self.set_last_instance_count(instances);
710 self.set_total_draw_calls(self.get_total_draw_calls().saturating_add(count));
711 Ok(count)
712 }
713}
714
715impl HardwareRenderer {
717 #[must_use]
731 pub fn with_backend(
732 backend: GraphicsBackend,
733 width: u32,
734 height: u32,
735 config: RenderConfig,
736 enable_readback: bool,
737 ) -> Self {
738 Self::with_backend_and_font(
739 backend,
740 width,
741 height,
742 config,
743 enable_readback,
744 NativeSystemFont::deterministic(),
745 )
746 }
747
748 #[must_use]
763 pub fn with_backend_and_font(
764 backend: GraphicsBackend,
765 width: u32,
766 height: u32,
767 config: RenderConfig,
768 enable_readback: bool,
769 font_provider: NativeSystemFont,
770 ) -> Self {
771 let driver: NativeGpuDriver = NativeGpuDriver::probe(backend);
772 let per_glyph_atlas: bool = config.get_direct_gpu_present();
773 let mut batches: Vec<GpuBatch> = Vec::with_capacity(16);
774 for _ in 0..8usize {
775 let mut batch: GpuBatch = GpuBatch::default();
776 batch.get_mut_primitives().reserve(256);
777 batches.push(batch);
778 }
779 Self {
780 driver,
781 batches,
782 active_batches: 0,
783 clip_stack: Vec::with_capacity(16),
784 last_submitted_batches: 0,
785 last_coalesced_merges: 0,
786 last_culled_instances: 0,
787 last_uploaded_instances: 0,
788 last_uploaded_bytes: 0,
789 readback: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
790 enable_readback,
791 resource_ring: GpuResourceRing::default(),
792 direct_gpu_frames: 0,
793 cpu_readback_frames: 0,
794 last_cpu_rgba_upload_bytes: 0,
795 has_unsupported_effect: false,
796 last_fence_id: 0,
797 blur_passes: Vec::with_capacity(8),
798 active_scroll_offset: [0.0, 0.0],
799 active_layer_id: 0,
800 last_uploaded_primitive_bytes: 0,
801 gpu_glyph_atlas: IncrementalGpuGlyphAtlas::default(),
802 gpu_image_atlas: IncrementalGpuImageAtlas::default(),
803 per_glyph_atlas,
804 }
805 }
806
807 pub fn set_font_provider(&mut self, font_provider: NativeSystemFont) -> &mut Self {
818 let _: &mut SoftwareRenderer = self.get_mut_readback().set_font_provider(font_provider);
819 self
820 }
821
822 pub fn capture_reference_readback(
834 &mut self,
835 display_list: &DisplayList,
836 damage: DamageRegion,
837 ) -> &Framebuffer {
838 let (_, _): (usize, usize) = self.get_mut_readback().render(display_list, damage);
839 self.get_readback().get_framebuffer()
840 }
841
842 pub fn complete_gpu_fence(&mut self, fence_id: u64) -> usize {
853 self.get_mut_resource_ring().complete_fence(fence_id)
854 }
855
856 pub fn trim_gpu_caches(&mut self, retain_ratio: f32) -> usize {
867 self.get_mut_resource_ring()
868 .apply_retention_ratio(retain_ratio)
869 }
870
871 #[must_use]
881 pub fn last_frame_telemetry(&self) -> (usize, usize, usize, usize, usize) {
882 (
883 self.get_last_submitted_batches(),
884 self.get_last_coalesced_merges(),
885 self.get_last_culled_instances(),
886 self.get_last_uploaded_instances(),
887 self.get_last_uploaded_bytes(),
888 )
889 }
890
891 #[must_use]
901 pub fn active_batches_slice(&self) -> &[GpuBatch] {
902 let active: usize = self.get_active_batches().min(self.get_batches().len());
903 &self.get_batches()[..active]
904 }
905
906 #[must_use]
916 pub fn batch_hash(&self) -> u64 {
917 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
918 let active: usize = self.get_active_batches().min(self.get_batches().len());
919 for batch in self.get_batches()[..active].iter() {
920 if !batch.get_primitives().is_empty() {
921 for prim in batch.get_primitives().iter() {
922 for item_val in prim.get_rect().iter().chain(prim.get_clip_rect().iter()) {
923 hash ^= item_val.to_bits() as u64;
924 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
925 }
926 for word in [
927 prim.get_color_rgba(),
928 prim.get_aux_color_rgba(),
929 prim.get_kind_flags(),
930 prim.get_extra_param(),
931 ] {
932 hash ^= word as u64;
933 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
934 }
935 for lane in prim
936 .get_uv_rect()
937 .iter()
938 .chain(prim.get_radii_border().iter())
939 {
940 hash ^= *lane as u64;
941 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
942 }
943 }
944 } else {
945 for inst in batch.get_instances().iter() {
946 for item_val in inst
947 .get_rect()
948 .iter()
949 .chain(inst.get_color().iter())
950 .chain(inst.get_radii().iter())
951 {
952 hash ^= item_val.to_bits() as u64;
953 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
954 }
955 }
956 }
957 }
958 hash
959 }
960
961 #[inline(always)]
972 #[must_use]
973 fn quad_inside_rect(quad: [f32; 4], container: Rect) -> bool {
974 let qx0: f32 = quad[0];
975 let qy0: f32 = quad[1];
976 let qx1: f32 = qx0 + quad[2];
977 let qy1: f32 = qy0 + quad[3];
978 let x_in: bool = qx0 >= container.get_x() && qx1 <= container.right();
979 let y_in: bool = qy0 >= container.get_y() && qy1 <= container.bottom();
980 container.get_width() > 0.0 && container.get_height() > 0.0 && x_in && y_in
981 }
982
983 #[inline(always)]
994 #[must_use]
995 fn quad_intersects_clip(quad: [f32; 4], clip: Rect) -> bool {
996 let qw: f32 = quad[2];
997 let qh: f32 = quad[3];
998 let quad_empty: bool = qw <= 0.0 || qh <= 0.0;
999 let clip_empty: bool = clip.get_width() <= 0.0 || clip.get_height() <= 0.0;
1000 if quad_empty || clip_empty {
1001 return false;
1002 }
1003 let qx0: f32 = quad[0];
1004 let qy0: f32 = quad[1];
1005 let qx1: f32 = qx0 + qw;
1006 let qy1: f32 = qy0 + qh;
1007 let x_hit: bool = qx1 > clip.get_x() && qx0 < clip.right();
1008 let y_hit: bool = qy1 > clip.get_y() && qy0 < clip.bottom();
1009 x_hit && y_hit
1010 }
1011
1012 #[inline(always)]
1023 #[must_use]
1024 fn rect_inside_rect(inner: Rect, outer: Rect) -> bool {
1025 let x_in: bool = inner.get_x() >= outer.get_x() && inner.right() <= outer.right();
1026 let y_in: bool = inner.get_y() >= outer.get_y() && inner.bottom() <= outer.bottom();
1027 x_in && y_in
1028 }
1029
1030 #[inline(always)]
1041 #[must_use]
1042 fn union_rect_quad(existing: Rect, quad: [f32; 4]) -> Rect {
1043 let x0: f32 = existing.get_x().min(quad[0]);
1044 let y0: f32 = existing.get_y().min(quad[1]);
1045 let x1: f32 = existing.right().max(quad[0] + quad[2]);
1046 let y1: f32 = existing.bottom().max(quad[1] + quad[3]);
1047 Rect::new(x0, y0, (x1 - x0).max(0.0), (y1 - y0).max(0.0))
1048 }
1049
1050 pub fn render(
1062 &mut self,
1063 display_list: &DisplayList,
1064 damage: DamageRegion,
1065 ) -> Result<(usize, usize), PlatformError> {
1066 self.render_with_physical_size(display_list, damage, None)
1067 }
1068
1069 pub fn render_with_physical_size(
1082 &mut self,
1083 display_list: &DisplayList,
1084 damage: DamageRegion,
1085 physical_size: Option<(u32, u32)>,
1086 ) -> Result<(usize, usize), PlatformError> {
1087 let scale: f32 = display_list.get_scale_factor().clamp(0.25, 16.0);
1088 let (phys_w, phys_h): (u32, u32) = match physical_size {
1089 Some((width_px, height_px)) if width_px > 0 && height_px > 0 => (width_px, height_px),
1090 _ => (
1091 ((display_list.get_viewport().get_width() * scale).round() as u32).max(1),
1092 ((display_list.get_viewport().get_height() * scale).round() as u32).max(1),
1093 ),
1094 };
1095 let max_blur_px: f32 = (64.0 * scale).clamp(64.0, 512.0);
1096 let full_clip: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
1097 let full_entry: GpuClipEntry = GpuClipEntry {
1098 scissor: full_clip,
1099 safe_inner: full_clip,
1100 rounded_clip_bits: 0,
1101 };
1102 let Self {
1103 driver,
1104 batches,
1105 active_batches,
1106 clip_stack,
1107 last_submitted_batches,
1108 last_coalesced_merges,
1109 last_culled_instances,
1110 last_uploaded_instances,
1111 last_uploaded_bytes,
1112 readback,
1113 enable_readback,
1114 resource_ring,
1115 direct_gpu_frames,
1116 cpu_readback_frames,
1117 last_cpu_rgba_upload_bytes,
1118 has_unsupported_effect,
1119 last_fence_id,
1120 blur_passes,
1121 active_scroll_offset,
1122 active_layer_id,
1123 last_uploaded_primitive_bytes,
1124 gpu_glyph_atlas,
1125 gpu_image_atlas,
1126 per_glyph_atlas,
1127 } = self;
1128 gpu_image_atlas.begin_frame();
1129 let mut current_glyph_h: u32 = gpu_glyph_atlas.get_height();
1130 let mut current_img_w: u32 = gpu_image_atlas.get_width();
1131 let mut current_img_h: u32 = gpu_image_atlas.get_height();
1132 clip_stack.clear();
1133 clip_stack.push(full_entry);
1134 blur_passes.clear();
1135 *active_batches = 0;
1136 *has_unsupported_effect = false;
1137 let layer_scroll: [f32; 2] = *active_scroll_offset;
1138 let layer_id: u8 = *active_layer_id;
1139 let gpu_supports_blur: bool = driver.supports_backdrop_blur_pass()
1140 || matches!(
1141 driver.backend(),
1142 GraphicsBackend::Metal
1143 | GraphicsBackend::WebGl2
1144 | GraphicsBackend::OpenGlEs
1145 | GraphicsBackend::WebGpu
1146 | GraphicsBackend::Vulkan
1147 | GraphicsBackend::Direct3D12
1148 | GraphicsBackend::Direct3D11
1149 );
1150 let mut tail_state: GpuBatchCoalesceState = GpuBatchCoalesceState {
1151 scroll_offset: layer_scroll,
1152 layer_id,
1153 ..GpuBatchCoalesceState::default()
1154 };
1155 let mut coalesced_merges: usize = 0;
1156 let mut culled_instances: usize = 0;
1157 let mut uploaded_instances: usize = 0;
1158 let mut clear_color: Color = COLOR_WHITE;
1159 for cmd in display_list.get_commands().iter() {
1160 let clip_entry: GpuClipEntry = clip_stack.last().copied().unwrap_or(full_entry);
1161 let active_clip: Rect = clip_entry.scissor;
1162 match cmd {
1163 DrawCommand::Clear(color_item) => {
1164 clear_color = *color_item;
1165 }
1166 DrawCommand::PushClip(rect) => {
1167 let phys: Rect = snap_physical_rect(*rect, scale);
1168 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
1169 let next_safe: Rect = clip_entry.safe_inner.intersect(phys).unwrap_or_default();
1170 let next_bits: u16 = GpuPrimitiveInstance::encode_rounded_clip_bits(
1171 active_clip,
1172 clip_entry.rounded_clip_bits,
1173 next_scissor,
1174 phys,
1175 None,
1176 );
1177 clip_stack.push(GpuClipEntry {
1178 scissor: next_scissor,
1179 safe_inner: next_safe,
1180 rounded_clip_bits: next_bits,
1181 });
1182 }
1183 DrawCommand::PushRoundedClip { rect, radius } => {
1184 let phys: Rect = snap_physical_rect(*rect, scale);
1185 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
1186 let phys_radius: CornerRadius = CornerRadius::new(
1187 radius.get_top_left() * scale,
1188 radius.get_top_right() * scale,
1189 radius.get_bottom_right() * scale,
1190 radius.get_bottom_left() * scale,
1191 );
1192 let max_r: f32 = phys_radius.max_radius().ceil();
1193 let inset_w: f32 = (phys.get_width() - max_r * 2.0).max(0.0);
1194 let inset_h: f32 = (phys.get_height() - max_r * 2.0).max(0.0);
1195 let rounded_safe: Rect =
1196 Rect::new(phys.get_x() + max_r, phys.get_y() + max_r, inset_w, inset_h);
1197 let next_safe: Rect = clip_entry
1198 .safe_inner
1199 .intersect(rounded_safe)
1200 .unwrap_or_default();
1201 let next_bits: u16 = GpuPrimitiveInstance::encode_rounded_clip_bits(
1202 active_clip,
1203 clip_entry.rounded_clip_bits,
1204 next_scissor,
1205 phys,
1206 Some(phys_radius),
1207 );
1208 clip_stack.push(GpuClipEntry {
1209 scissor: next_scissor,
1210 safe_inner: next_safe,
1211 rounded_clip_bits: next_bits,
1212 });
1213 }
1214 DrawCommand::PopClip => {
1215 if clip_stack.len() > 1 {
1216 let _: Option<GpuClipEntry> = clip_stack.pop();
1217 }
1218 }
1219 DrawCommand::FillRoundedRect {
1220 rect,
1221 color,
1222 radius,
1223 border,
1224 } => {
1225 let snapped: Rect = snap_physical_rect(*rect, scale);
1226 let quad: [f32; 4] = [
1227 snapped.get_x(),
1228 snapped.get_y(),
1229 snapped.get_width(),
1230 snapped.get_height(),
1231 ];
1232 if !Self::quad_intersects_clip(quad, active_clip) {
1233 culled_instances += 1;
1234 continue;
1235 }
1236 let border_width: f32 = if border.get_width() > 0.0 {
1237 (border.get_width() * scale).max(1.0)
1238 } else {
1239 0.0
1240 };
1241 let inst: GpuQuadInstance = GpuQuadInstance {
1242 rect: quad,
1243 clip: [
1244 active_clip.get_x(),
1245 active_clip.get_y(),
1246 active_clip.right(),
1247 active_clip.bottom(),
1248 ],
1249 color: color.to_f32_array(),
1250 secondary_color: border.get_color().to_f32_array(),
1251 radii: [
1252 radius.get_top_left() * scale,
1253 radius.get_top_right() * scale,
1254 radius.get_bottom_right() * scale,
1255 radius.get_bottom_left() * scale,
1256 ],
1257 stroke_or_blur: border_width,
1258 angle_rad: 0.0,
1259 kind: GpuPrimitiveKind::RoundedRect as u32,
1260 reserved: 0,
1261 };
1262 Self::push_instance_coalesced(
1263 batches,
1264 active_batches,
1265 &mut tail_state,
1266 clip_entry,
1267 inst,
1268 &mut coalesced_merges,
1269 );
1270 uploaded_instances += 1;
1271 }
1272 DrawCommand::LinearGradientRect {
1273 rect,
1274 gradient,
1275 radius,
1276 } => {
1277 let snapped: Rect = snap_physical_rect(*rect, scale);
1278 let quad: [f32; 4] = [
1279 snapped.get_x(),
1280 snapped.get_y(),
1281 snapped.get_width(),
1282 snapped.get_height(),
1283 ];
1284 if !Self::quad_intersects_clip(quad, active_clip) {
1285 culled_instances += 1;
1286 continue;
1287 }
1288 let inst: GpuQuadInstance = GpuQuadInstance {
1289 rect: quad,
1290 clip: [
1291 active_clip.get_x(),
1292 active_clip.get_y(),
1293 active_clip.right(),
1294 active_clip.bottom(),
1295 ],
1296 color: gradient.get_start_color().to_f32_array(),
1297 secondary_color: gradient.get_end_color().to_f32_array(),
1298 radii: [
1299 radius.get_top_left() * scale,
1300 radius.get_top_right() * scale,
1301 radius.get_bottom_right() * scale,
1302 radius.get_bottom_left() * scale,
1303 ],
1304 stroke_or_blur: 0.0,
1305 angle_rad: gradient.get_angle_deg().to_radians(),
1306 kind: GpuPrimitiveKind::Gradient as u32,
1307 reserved: 0,
1308 };
1309 Self::push_instance_coalesced(
1310 batches,
1311 active_batches,
1312 &mut tail_state,
1313 clip_entry,
1314 inst,
1315 &mut coalesced_merges,
1316 );
1317 uploaded_instances += 1;
1318 }
1319 DrawCommand::BoxShadow {
1320 rect,
1321 radius,
1322 shadow,
1323 } => {
1324 let spread: f32 = shadow.get_spread_radius() * scale;
1325 let blur: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(max_blur_px);
1326 let sigma: f32 = (blur * 0.5).max(0.75);
1327 let pad: f32 = (sigma * 3.0).ceil();
1328 let base_rect: Rect = snap_physical_rect(*rect, scale);
1329 let caster_x: f32 =
1330 base_rect.get_x() + (shadow.get_offset_x() * scale).round() - spread;
1331 let caster_y: f32 =
1332 base_rect.get_y() + (shadow.get_offset_y() * scale).round() - spread;
1333 let caster_w: f32 = (base_rect.get_width() + spread * 2.0).max(1.0);
1334 let caster_h: f32 = (base_rect.get_height() + spread * 2.0).max(1.0);
1335 let quad: [f32; 4] = [
1336 caster_x - pad,
1337 caster_y - pad,
1338 caster_w + pad * 2.0,
1339 caster_h + pad * 2.0,
1340 ];
1341 if !Self::quad_intersects_clip(quad, active_clip) {
1342 culled_instances += 1;
1343 continue;
1344 }
1345 let inst: GpuQuadInstance = GpuQuadInstance {
1346 rect: quad,
1347 clip: [
1348 active_clip.get_x(),
1349 active_clip.get_y(),
1350 active_clip.right(),
1351 active_clip.bottom(),
1352 ],
1353 color: shadow.get_color().to_f32_array(),
1354 secondary_color: [0.0; 4],
1355 radii: [
1356 (radius.get_top_left() * scale + spread).max(0.0),
1357 (radius.get_top_right() * scale + spread).max(0.0),
1358 (radius.get_bottom_right() * scale + spread).max(0.0),
1359 (radius.get_bottom_left() * scale + spread).max(0.0),
1360 ],
1361 stroke_or_blur: sigma,
1362 angle_rad: pad,
1363 kind: GpuPrimitiveKind::Shadow as u32,
1364 reserved: 0,
1365 };
1366 Self::push_instance_coalesced(
1367 batches,
1368 active_batches,
1369 &mut tail_state,
1370 clip_entry,
1371 inst,
1372 &mut coalesced_merges,
1373 );
1374 uploaded_instances += 1;
1375 }
1376 DrawCommand::BackdropBlur {
1377 rect,
1378 radius,
1379 blur_radius,
1380 } => {
1381 if !gpu_supports_blur {
1382 *has_unsupported_effect = true;
1383 }
1384 let snapped: Rect = snap_physical_rect(*rect, scale);
1385 let quad: [f32; 4] = [
1386 snapped.get_x(),
1387 snapped.get_y(),
1388 snapped.get_width(),
1389 snapped.get_height(),
1390 ];
1391 let Some(vis_blur) = snapped.intersect(active_clip) else {
1392 culled_instances += 1;
1393 continue;
1394 };
1395 if vis_blur.get_width() <= 0.0 || vis_blur.get_height() <= 0.0 {
1396 culled_instances += 1;
1397 continue;
1398 }
1399 let phys_blur: f32 = (*blur_radius * scale).clamp(0.5, max_blur_px);
1400 let radii_px: [f32; 4] = [
1401 radius.get_top_left() * scale,
1402 radius.get_top_right() * scale,
1403 radius.get_bottom_right() * scale,
1404 radius.get_bottom_left() * scale,
1405 ];
1406 let halo_x0: f32 = (vis_blur.get_x() - phys_blur)
1407 .max(active_clip.get_x())
1408 .max(0.0);
1409 let halo_y0: f32 = (vis_blur.get_y() - phys_blur)
1410 .max(active_clip.get_y())
1411 .max(0.0);
1412 let halo_x1: f32 = (vis_blur.right() + phys_blur)
1413 .min(active_clip.right())
1414 .min(phys_w as f32);
1415 let halo_y1: f32 = (vis_blur.bottom() + phys_blur)
1416 .min(active_clip.bottom())
1417 .min(phys_h as f32);
1418 let halo: Rect = Rect::new(
1419 halo_x0,
1420 halo_y0,
1421 (halo_x1 - halo_x0).max(0.0),
1422 (halo_y1 - halo_y0).max(0.0),
1423 );
1424 let pass_layer_id: u8 = tail_state.layer_id;
1425 blur_passes.push(GpuBackdropBlurPass {
1426 bounds: snapped,
1427 input_bounds: halo,
1428 clip_rect: active_clip,
1429 radii: radii_px,
1430 blur_radius: phys_blur,
1431 layer_id: pass_layer_id,
1432 });
1433 let inv_vp_w: f32 = 1.0 / (phys_w.max(1) as f32);
1434 let inv_vp_h: f32 = 1.0 / (phys_h.max(1) as f32);
1435 let blur_uv: [u16; 4] = [
1436 (snapped.get_x() * inv_vp_w * 65535.0)
1437 .round()
1438 .clamp(0.0, 65535.0) as u16,
1439 (snapped.get_y() * inv_vp_h * 65535.0)
1440 .round()
1441 .clamp(0.0, 65535.0) as u16,
1442 (snapped.right() * inv_vp_w * 65535.0)
1443 .round()
1444 .clamp(0.0, 65535.0) as u16,
1445 (snapped.bottom() * inv_vp_h * 65535.0)
1446 .round()
1447 .clamp(0.0, 65535.0) as u16,
1448 ];
1449 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
1450 let blur_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1451 rect: quad,
1452 clip_rect: [
1453 active_clip.get_x(),
1454 active_clip.get_y(),
1455 active_clip.right(),
1456 active_clip.bottom(),
1457 ],
1458 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
1459 aux_color_rgba: 0,
1460 uv_rect: blur_uv,
1461 radii_border: [
1462 GpuPrimitiveInstance::pack_u16_q4(radii_px[0].min(radius_cap)),
1463 GpuPrimitiveInstance::pack_u16_q4(radii_px[1].min(radius_cap)),
1464 GpuPrimitiveInstance::pack_u16_q4(radii_px[2].min(radius_cap)),
1465 GpuPrimitiveInstance::pack_u16_q4(radii_px[3].min(radius_cap)),
1466 ],
1467 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
1468 PrimKind::BackdropBlur,
1469 0,
1470 0,
1471 pass_layer_id,
1472 ),
1473 extra_param: GpuPrimitiveInstance::pack_f16x2(phys_blur, 0.0),
1474 };
1475 Self::push_primitive_coalesced(
1476 batches,
1477 active_batches,
1478 &mut tail_state,
1479 clip_entry,
1480 blur_prim,
1481 true,
1482 &mut coalesced_merges,
1483 );
1484 tail_state.layer_id = tail_state.layer_id.saturating_add(1);
1485 uploaded_instances += 1;
1486 }
1487 DrawCommand::TextRun { rect, text, style } => {
1488 let run_quad: [f32; 4] = [
1489 rect.get_x() * scale,
1490 rect.get_y() * scale,
1491 rect.get_width() * scale,
1492 rect.get_height() * scale,
1493 ];
1494 if !Self::quad_intersects_clip(run_quad, active_clip) {
1495 culled_instances += 1;
1496 continue;
1497 }
1498 if *per_glyph_atlas {
1499 let wrap_w: Option<f32> = if matches!(
1500 style.get_overflow(),
1501 TextOverflow::Wrap | TextOverflow::Ellipsis
1502 ) {
1503 Some(rect.get_width().max(1.0))
1504 } else {
1505 None
1506 };
1507 let layout_res: TextLayoutResult =
1508 DeterministicTextMeasurer.measure(text, *style, wrap_w);
1509 let phys_font_px: f32 = (style.get_font_size() * scale).max(6.0);
1510 let letter_sp: f32 = style.get_letter_spacing() * scale;
1511 let line_h_phys: f32 = layout_res.get_line_height_px() * scale;
1512 let font_provider: NativeSystemFont = readback.get_font_provider().clone();
1513 let cpu_atlas: &mut GlyphAtlas = readback.get_mut_glyph_atlas();
1514 let text_color_u32: u32 =
1515 GpuPrimitiveInstance::pack_color_rgba8(style.get_color());
1516 let clip_arr: [f32; 4] = [
1517 active_clip.get_x(),
1518 active_clip.get_y(),
1519 active_clip.right(),
1520 active_clip.bottom(),
1521 ];
1522 let box_w_phys: f32 = rect.get_width() * scale;
1523 let base_x_phys: f32 = (rect.get_x() * scale).round();
1524 let mut line_glyphs: Vec<(Option<GpuGlyphPlacement>, f32)> =
1525 Vec::with_capacity(32);
1526 for (line_idx, line) in layout_res.get_lines().iter().enumerate() {
1527 let line_y: f32 =
1528 (rect.get_y() * scale + (line_idx as f32) * line_h_phys).round();
1529 line_glyphs.clear();
1530 let mut actual_line_w: f32 = 0.0;
1531 for ch in line.get_text().chars() {
1532 if ch == ' ' || ch == '\t' {
1533 let space_adv: f32 =
1534 measure_char_advance(ch, *style) * scale + letter_sp;
1535 actual_line_w += space_adv;
1536 line_glyphs.push((None, space_adv));
1537 } else {
1538 let key: GlyphKey =
1539 GlyphKey::from_text_style(ch, phys_font_px, *style);
1540 let raster: &RasterizedGlyph =
1541 cpu_atlas.get_or_rasterize_with(key, &font_provider);
1542 let adv_phys: f32 =
1543 (raster.get_advance() as f32).max(1.0) + letter_sp;
1544 let placement: GpuGlyphPlacement =
1545 gpu_glyph_atlas.insert_or_get(key, raster);
1546 let next_glyph_h: u32 = gpu_glyph_atlas.get_height();
1547 if next_glyph_h != current_glyph_h && next_glyph_h > 0 {
1548 Self::rescale_emitted_glyph_uvs(
1549 batches.as_mut_slice(),
1550 *active_batches,
1551 line_glyphs.as_mut_slice(),
1552 current_glyph_h,
1553 next_glyph_h,
1554 );
1555 current_glyph_h = next_glyph_h;
1556 }
1557 actual_line_w += adv_phys;
1558 line_glyphs.push((Some(placement), adv_phys));
1559 }
1560 }
1561 let fit_scale: f32 =
1562 if box_w_phys > 1.0 && actual_line_w > box_w_phys + 1.0 {
1563 (box_w_phys / actual_line_w).clamp(0.72, 1.0)
1564 } else {
1565 1.0
1566 };
1567 let fitted_w: f32 = actual_line_w * fit_scale;
1568 let align_x: f32 = match style.get_align() {
1569 TextAlign::Left => 0.0,
1570 TextAlign::Center => ((box_w_phys - fitted_w) * 0.5).max(0.0),
1571 TextAlign::Right => (box_w_phys - fitted_w).max(0.0),
1572 };
1573 let mut pen_x: f32 = base_x_phys + align_x.round();
1574 for &(placement_opt, adv_phys) in line_glyphs.iter() {
1575 if let Some(placement) = placement_opt
1576 && placement.get_width() > 0
1577 && placement.get_height() > 0
1578 {
1579 let gw: f32 = placement.get_width() as f32;
1580 let gh: f32 = placement.get_height() as f32;
1581 let glyph_y: f32 = line_y + ((line_h_phys - gh) * 0.5).round();
1582 let glyph_quad: [f32; 4] = [pen_x.round(), glyph_y, gw, gh];
1583 if Self::quad_intersects_clip(glyph_quad, active_clip) {
1584 let kind_flags: u32 = GpuPrimitiveInstance::pack_kind_flags(
1585 PrimKind::GlyphAlpha8,
1586 0,
1587 0,
1588 tail_state.layer_id,
1589 );
1590 let glyph_prim: GpuPrimitiveInstance =
1591 GpuPrimitiveInstance {
1592 rect: glyph_quad,
1593 clip_rect: clip_arr,
1594 color_rgba: text_color_u32,
1595 aux_color_rgba: 0,
1596 uv_rect: placement.get_uv_rect(),
1597 radii_border: [0; 4],
1598 kind_flags,
1599 extra_param: 0,
1600 };
1601 Self::push_primitive_coalesced(
1602 batches,
1603 active_batches,
1604 &mut tail_state,
1605 clip_entry,
1606 glyph_prim,
1607 false,
1608 &mut coalesced_merges,
1609 );
1610 uploaded_instances += 1;
1611 } else {
1612 culled_instances += 1;
1613 }
1614 }
1615 pen_x += adv_phys * fit_scale;
1616 }
1617 }
1618 } else {
1619 let glyph_count: usize = if text.is_ascii() {
1620 text.as_bytes()
1621 .iter()
1622 .filter(|byte_item: &&u8| **byte_item != b' ')
1623 .count()
1624 .max(1)
1625 } else {
1626 text.chars()
1627 .filter(|char_item: &char| *char_item != ' ')
1628 .count()
1629 .max(1)
1630 };
1631 let text_instance: GpuQuadInstance = GpuQuadInstance {
1632 rect: run_quad,
1633 clip: [
1634 active_clip.get_x(),
1635 active_clip.get_y(),
1636 active_clip.right(),
1637 active_clip.bottom(),
1638 ],
1639 color: style.get_color().to_f32_array(),
1640 secondary_color: [0.0; 4],
1641 radii: [0.0; 4],
1642 stroke_or_blur: style.get_font_size() * scale,
1643 angle_rad: 0.0,
1644 kind: GpuPrimitiveKind::Glyph as u32,
1645 reserved: glyph_count as u32,
1646 };
1647 Self::push_instance_coalesced(
1648 batches,
1649 active_batches,
1650 &mut tail_state,
1651 clip_entry,
1652 text_instance,
1653 &mut coalesced_merges,
1654 );
1655 uploaded_instances += 1;
1656 }
1657 }
1658 DrawCommand::TextCaret {
1659 text_rect,
1660 content_rect,
1661 text,
1662 style,
1663 caret_height,
1664 color,
1665 multiline,
1666 } => {
1667 if active_clip.get_width() <= 0.0 || active_clip.get_height() <= 0.0 {
1668 culled_instances += 1;
1669 continue;
1670 }
1671 let meas: TextLayoutResult = DeterministicTextMeasurer.measure(
1672 text,
1673 *style,
1674 if *multiline {
1675 Some(content_rect.get_width().max(1.0))
1676 } else {
1677 None
1678 },
1679 );
1680 let phys_font_px: f32 = (style.get_font_size() * scale).max(6.0);
1681 let letter_sp: f32 = style.get_letter_spacing() * scale;
1682 let last_w_phys: f32 = if let Some(line) = meas.get_lines().last() {
1683 let font_provider: NativeSystemFont = readback.get_font_provider().clone();
1684 let atlas: &mut GlyphAtlas = readback.get_mut_glyph_atlas();
1685 let mut acc: f32 = 0.0;
1686 for ch in line.get_text().chars() {
1687 if ch == ' ' || ch == '\t' {
1688 acc += measure_char_advance(ch, *style) * scale + letter_sp;
1689 } else {
1690 let key: GlyphKey =
1691 GlyphKey::from_text_style(ch, phys_font_px, *style);
1692 let green_val: &RasterizedGlyph =
1693 atlas.get_or_rasterize_with(key, &font_provider);
1694 acc += (green_val.get_advance() as f32).max(1.0) + letter_sp;
1695 }
1696 }
1697 acc
1698 } else {
1699 0.0
1700 };
1701 let line_idx: usize = meas.get_lines().len().saturating_sub(1);
1702 let trailing_gap_phys: f32 = if last_w_phys > 0.0 { 1.0 * scale } else { 0.0 };
1703 let min_x_phys: f32 = content_rect.get_x() * scale;
1704 let max_x_phys: f32 = ((content_rect.right() - 2.0) * scale).max(min_x_phys);
1705 let caret_x_phys: f32 =
1706 (text_rect.get_x() * scale + last_w_phys + trailing_gap_phys)
1707 .min(max_x_phys)
1708 .max(min_x_phys);
1709 let caret_y: f32 = if *multiline {
1710 (content_rect.get_y()
1711 + (line_idx as f32) * meas.get_line_height_px()
1712 + ((meas.get_line_height_px() - caret_height) * 0.5).max(0.0))
1713 .min(content_rect.bottom() - caret_height)
1714 .max(content_rect.get_y())
1715 } else {
1716 content_rect.get_y()
1717 + ((content_rect.get_height() - caret_height) * 0.5).max(0.0)
1718 };
1719 let quad: [f32; 4] = [
1720 caret_x_phys.round(),
1721 (caret_y * scale).round(),
1722 (1.5 * scale).max(1.0),
1723 (caret_height * scale).max(2.0),
1724 ];
1725 if !Self::quad_intersects_clip(quad, active_clip) {
1726 culled_instances += 1;
1727 continue;
1728 }
1729 let inst: GpuQuadInstance = GpuQuadInstance {
1730 rect: quad,
1731 clip: [
1732 active_clip.get_x(),
1733 active_clip.get_y(),
1734 active_clip.right(),
1735 active_clip.bottom(),
1736 ],
1737 color: color.to_f32_array(),
1738 secondary_color: [0.0; 4],
1739 radii: [0.5 * scale; 4],
1740 stroke_or_blur: 0.0,
1741 angle_rad: 0.0,
1742 kind: GpuPrimitiveKind::RoundedRect as u32,
1743 reserved: 0,
1744 };
1745 Self::push_instance_coalesced(
1746 batches,
1747 active_batches,
1748 &mut tail_state,
1749 clip_entry,
1750 inst,
1751 &mut coalesced_merges,
1752 );
1753 uploaded_instances += 1;
1754 }
1755 DrawCommand::Image {
1756 rect,
1757 buffer,
1758 radius,
1759 } => {
1760 let snapped: Rect = snap_physical_rect(*rect, scale);
1761 let quad: [f32; 4] = [
1762 snapped.get_x(),
1763 snapped.get_y(),
1764 snapped.get_width(),
1765 snapped.get_height(),
1766 ];
1767 if !Self::quad_intersects_clip(quad, active_clip) {
1768 culled_instances += 1;
1769 continue;
1770 }
1771 let uv_rect: [u16; 4] = gpu_image_atlas
1772 .insert_or_get(
1773 buffer.get_width(),
1774 buffer.get_height(),
1775 buffer.get_pixels().as_ref(),
1776 )
1777 .map_or([0, 0, u16::MAX, u16::MAX], |slot: GpuImagePlacement| {
1778 slot.get_uv_rect()
1779 });
1780 let next_img_w: u32 = gpu_image_atlas.get_width();
1781 let next_img_h: u32 = gpu_image_atlas.get_height();
1782 if (next_img_w != current_img_w || next_img_h != current_img_h)
1783 && next_img_w > 0
1784 && next_img_h > 0
1785 {
1786 Self::rescale_emitted_image_uvs(
1787 batches.as_mut_slice(),
1788 *active_batches,
1789 (current_img_w, current_img_h),
1790 (next_img_w, next_img_h),
1791 );
1792 current_img_w = next_img_w;
1793 current_img_h = next_img_h;
1794 }
1795 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
1796 let img_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1797 rect: quad,
1798 clip_rect: [
1799 active_clip.get_x(),
1800 active_clip.get_y(),
1801 active_clip.right(),
1802 active_clip.bottom(),
1803 ],
1804 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
1805 aux_color_rgba: 0,
1806 uv_rect,
1807 radii_border: [
1808 GpuPrimitiveInstance::pack_u16_q4(
1809 (radius.get_top_left() * scale).min(radius_cap),
1810 ),
1811 GpuPrimitiveInstance::pack_u16_q4(
1812 (radius.get_top_right() * scale).min(radius_cap),
1813 ),
1814 GpuPrimitiveInstance::pack_u16_q4(
1815 (radius.get_bottom_right() * scale).min(radius_cap),
1816 ),
1817 GpuPrimitiveInstance::pack_u16_q4(
1818 (radius.get_bottom_left() * scale).min(radius_cap),
1819 ),
1820 ],
1821 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
1822 PrimKind::ImageTexture,
1823 0,
1824 0,
1825 tail_state.layer_id,
1826 ),
1827 extra_param: 0,
1828 };
1829 Self::push_primitive_coalesced(
1830 batches,
1831 active_batches,
1832 &mut tail_state,
1833 clip_entry,
1834 img_prim,
1835 false,
1836 &mut coalesced_merges,
1837 );
1838 uploaded_instances += 1;
1839 }
1840 DrawCommand::SharedImage {
1841 rect,
1842 width,
1843 height,
1844 pixels,
1845 radius,
1846 } => {
1847 let snapped: Rect = snap_physical_rect(*rect, scale);
1848 let quad: [f32; 4] = [
1849 snapped.get_x(),
1850 snapped.get_y(),
1851 snapped.get_width(),
1852 snapped.get_height(),
1853 ];
1854 if !Self::quad_intersects_clip(quad, active_clip) {
1855 culled_instances += 1;
1856 continue;
1857 }
1858 let uv_rect: [u16; 4] = gpu_image_atlas
1859 .insert_or_get(*width, *height, pixels.as_slice())
1860 .map_or([0, 0, u16::MAX, u16::MAX], |slot: GpuImagePlacement| {
1861 slot.get_uv_rect()
1862 });
1863 let next_img_w: u32 = gpu_image_atlas.get_width();
1864 let next_img_h: u32 = gpu_image_atlas.get_height();
1865 if (next_img_w != current_img_w || next_img_h != current_img_h)
1866 && next_img_w > 0
1867 && next_img_h > 0
1868 {
1869 Self::rescale_emitted_image_uvs(
1870 batches.as_mut_slice(),
1871 *active_batches,
1872 (current_img_w, current_img_h),
1873 (next_img_w, next_img_h),
1874 );
1875 current_img_w = next_img_w;
1876 current_img_h = next_img_h;
1877 }
1878 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
1879 let img_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1880 rect: quad,
1881 clip_rect: [
1882 active_clip.get_x(),
1883 active_clip.get_y(),
1884 active_clip.right(),
1885 active_clip.bottom(),
1886 ],
1887 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
1888 aux_color_rgba: 0,
1889 uv_rect,
1890 radii_border: [
1891 GpuPrimitiveInstance::pack_u16_q4(
1892 (radius.get_top_left() * scale).min(radius_cap),
1893 ),
1894 GpuPrimitiveInstance::pack_u16_q4(
1895 (radius.get_top_right() * scale).min(radius_cap),
1896 ),
1897 GpuPrimitiveInstance::pack_u16_q4(
1898 (radius.get_bottom_right() * scale).min(radius_cap),
1899 ),
1900 GpuPrimitiveInstance::pack_u16_q4(
1901 (radius.get_bottom_left() * scale).min(radius_cap),
1902 ),
1903 ],
1904 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
1905 PrimKind::ImageTexture,
1906 0,
1907 0,
1908 tail_state.layer_id,
1909 ),
1910 extra_param: 0,
1911 };
1912 Self::push_primitive_coalesced(
1913 batches,
1914 active_batches,
1915 &mut tail_state,
1916 clip_entry,
1917 img_prim,
1918 false,
1919 &mut coalesced_merges,
1920 );
1921 uploaded_instances += 1;
1922 }
1923 DrawCommand::Circle {
1924 center,
1925 radius,
1926 color,
1927 } => {
1928 let red_val: f32 = radius * scale;
1929 let pad: f32 = 1.5;
1930 let extent: f32 = red_val + pad;
1931 let quad: [f32; 4] = [
1932 center.get_x() * scale - extent,
1933 center.get_y() * scale - extent,
1934 extent * 2.0,
1935 extent * 2.0,
1936 ];
1937 if !Self::quad_intersects_clip(quad, active_clip) {
1938 culled_instances += 1;
1939 continue;
1940 }
1941 let inst: GpuQuadInstance = GpuQuadInstance {
1942 rect: quad,
1943 clip: [
1944 active_clip.get_x(),
1945 active_clip.get_y(),
1946 active_clip.right(),
1947 active_clip.bottom(),
1948 ],
1949 color: color.to_f32_array(),
1950 secondary_color: [0.0; 4],
1951 radii: [red_val; 4],
1952 stroke_or_blur: red_val,
1953 angle_rad: 0.0,
1954 kind: GpuPrimitiveKind::Circle as u32,
1955 reserved: 0,
1956 };
1957 Self::push_instance_coalesced(
1958 batches,
1959 active_batches,
1960 &mut tail_state,
1961 clip_entry,
1962 inst,
1963 &mut coalesced_merges,
1964 );
1965 uploaded_instances += 1;
1966 }
1967 DrawCommand::Line {
1968 from,
1969 to,
1970 color,
1971 width,
1972 } => {
1973 let fx0: f32 = from.get_x() * scale;
1974 let fy0: f32 = from.get_y() * scale;
1975 let fx1: f32 = to.get_x() * scale;
1976 let fy1: f32 = to.get_y() * scale;
1977 let dx: f32 = fx1 - fx0;
1978 let dy: f32 = fy1 - fy0;
1979 let stroke_px: f32 = (width * scale).max(1.0);
1980 let pad: f32 = stroke_px * 0.5 + 1.5;
1981 let min_x: f32 = fx0.min(fx1) - pad;
1982 let min_y: f32 = fy0.min(fy1) - pad;
1983 let max_x: f32 = fx0.max(fx1) + pad;
1984 let max_y: f32 = fy0.max(fy1) + pad;
1985 let quad: [f32; 4] = [
1986 min_x,
1987 min_y,
1988 (max_x - min_x).max(2.0),
1989 (max_y - min_y).max(2.0),
1990 ];
1991 if !Self::quad_intersects_clip(quad, active_clip) {
1992 culled_instances += 1;
1993 continue;
1994 }
1995 let inst: GpuQuadInstance = GpuQuadInstance {
1996 rect: quad,
1997 clip: [
1998 active_clip.get_x(),
1999 active_clip.get_y(),
2000 active_clip.right(),
2001 active_clip.bottom(),
2002 ],
2003 color: color.to_f32_array(),
2004 secondary_color: [0.0; 4],
2005 radii: [fx0, fy0, fx1, fy1],
2006 stroke_or_blur: stroke_px,
2007 angle_rad: dy.atan2(dx),
2008 kind: GpuPrimitiveKind::Line as u32,
2009 reserved: 0,
2010 };
2011 Self::push_instance_coalesced(
2012 batches,
2013 active_batches,
2014 &mut tail_state,
2015 clip_entry,
2016 inst,
2017 &mut coalesced_merges,
2018 );
2019 uploaded_instances += 1;
2020 }
2021 DrawCommand::Triangle { p0, p1, p2, color } => {
2022 let min_x: f32 = p0.get_x().min(p1.get_x()).min(p2.get_x()) * scale - 1.5;
2023 let min_y: f32 = p0.get_y().min(p1.get_y()).min(p2.get_y()) * scale - 1.5;
2024 let max_x: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x()) * scale + 1.5;
2025 let max_y: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y()) * scale + 1.5;
2026 let quad: [f32; 4] = [
2027 min_x,
2028 min_y,
2029 (max_x - min_x).max(2.0),
2030 (max_y - min_y).max(2.0),
2031 ];
2032 if !Self::quad_intersects_clip(quad, active_clip) {
2033 culled_instances += 1;
2034 continue;
2035 }
2036 let inst: GpuQuadInstance = GpuQuadInstance {
2037 rect: quad,
2038 clip: [
2039 active_clip.get_x(),
2040 active_clip.get_y(),
2041 active_clip.right(),
2042 active_clip.bottom(),
2043 ],
2044 color: color.to_f32_array(),
2045 secondary_color: [p0.get_x() * scale, p0.get_y() * scale, 0.0, 0.0],
2046 radii: [
2047 p1.get_x() * scale,
2048 p1.get_y() * scale,
2049 p2.get_x() * scale,
2050 p2.get_y() * scale,
2051 ],
2052 stroke_or_blur: 0.0,
2053 angle_rad: 0.0,
2054 kind: GpuPrimitiveKind::Triangle as u32,
2055 reserved: 3,
2056 };
2057 Self::push_instance_coalesced(
2058 batches,
2059 active_batches,
2060 &mut tail_state,
2061 clip_entry,
2062 inst,
2063 &mut coalesced_merges,
2064 );
2065 uploaded_instances += 1;
2066 }
2067 DrawCommand::SpriteRegion {
2068 rect,
2069 buffer,
2070 src_rect,
2071 tint,
2072 } => {
2073 let snapped: Rect = snap_physical_rect(*rect, scale);
2074 let quad: [f32; 4] = [
2075 snapped.get_x(),
2076 snapped.get_y(),
2077 snapped.get_width(),
2078 snapped.get_height(),
2079 ];
2080 if !Self::quad_intersects_clip(quad, active_clip) {
2081 culled_instances += 1;
2082 continue;
2083 }
2084 let uv_rect: [u16; 4] = gpu_image_atlas
2085 .insert_or_get(
2086 buffer.get_width(),
2087 buffer.get_height(),
2088 buffer.get_pixels().as_ref(),
2089 )
2090 .map_or([0, 0, u16::MAX, u16::MAX], |slot: GpuImagePlacement| {
2091 gpu_image_atlas.sub_uv_rect(
2092 slot,
2093 buffer.get_width(),
2094 buffer.get_height(),
2095 *src_rect,
2096 )
2097 });
2098 let next_img_w: u32 = gpu_image_atlas.get_width();
2099 let next_img_h: u32 = gpu_image_atlas.get_height();
2100 if (next_img_w != current_img_w || next_img_h != current_img_h)
2101 && next_img_w > 0
2102 && next_img_h > 0
2103 {
2104 Self::rescale_emitted_image_uvs(
2105 batches.as_mut_slice(),
2106 *active_batches,
2107 (current_img_w, current_img_h),
2108 (next_img_w, next_img_h),
2109 );
2110 current_img_w = next_img_w;
2111 current_img_h = next_img_h;
2112 }
2113 let sprite_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
2114 rect: quad,
2115 clip_rect: [
2116 active_clip.get_x(),
2117 active_clip.get_y(),
2118 active_clip.right(),
2119 active_clip.bottom(),
2120 ],
2121 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(*tint),
2122 aux_color_rgba: 0,
2123 uv_rect,
2124 radii_border: [0; 4],
2125 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
2126 PrimKind::ImageTexture,
2127 0,
2128 0,
2129 tail_state.layer_id,
2130 ),
2131 extra_param: 0,
2132 };
2133 Self::push_primitive_coalesced(
2134 batches,
2135 active_batches,
2136 &mut tail_state,
2137 clip_entry,
2138 sprite_prim,
2139 false,
2140 &mut coalesced_merges,
2141 );
2142 uploaded_instances += 1;
2143 }
2144 DrawCommand::ExternalSurface { rect, radius } => {
2145 let snapped: Rect = snap_physical_rect(*rect, scale);
2146 let quad: [f32; 4] = [
2147 snapped.get_x(),
2148 snapped.get_y(),
2149 snapped.get_width(),
2150 snapped.get_height(),
2151 ];
2152 if !Self::quad_intersects_clip(quad, active_clip) {
2153 culled_instances += 1;
2154 continue;
2155 }
2156 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
2157 let ext_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
2158 rect: quad,
2159 clip_rect: [
2160 active_clip.get_x(),
2161 active_clip.get_y(),
2162 active_clip.right(),
2163 active_clip.bottom(),
2164 ],
2165 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
2166 aux_color_rgba: 0,
2167 uv_rect: [0, 0, u16::MAX, u16::MAX],
2168 radii_border: [
2169 GpuPrimitiveInstance::pack_u16_q4(
2170 (radius.get_top_left() * scale).min(radius_cap),
2171 ),
2172 GpuPrimitiveInstance::pack_u16_q4(
2173 (radius.get_top_right() * scale).min(radius_cap),
2174 ),
2175 GpuPrimitiveInstance::pack_u16_q4(
2176 (radius.get_bottom_right() * scale).min(radius_cap),
2177 ),
2178 GpuPrimitiveInstance::pack_u16_q4(
2179 (radius.get_bottom_left() * scale).min(radius_cap),
2180 ),
2181 ],
2182 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
2183 PrimKind::ExternalGpuSurface,
2184 0,
2185 0,
2186 tail_state.layer_id,
2187 ),
2188 extra_param: 0,
2189 };
2190 Self::push_primitive_coalesced(
2191 batches,
2192 active_batches,
2193 &mut tail_state,
2194 clip_entry,
2195 ext_prim,
2196 false,
2197 &mut coalesced_merges,
2198 );
2199 uploaded_instances += 1;
2200 }
2201 }
2202 }
2203 let prim_upload_bytes: usize =
2204 uploaded_instances.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
2205 let upload_bytes: usize = prim_upload_bytes;
2206 let acquired_fence: u64 = match resource_ring
2207 .acquire_staging(prim_upload_bytes, GpuUploadPriority::RequiredFrame)
2208 {
2209 GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
2210 GpuRingAcquireResult::DeferredFrameRingFull
2211 | GpuRingAcquireResult::DroppedPrefetchRingFull => {
2212 let prev: u64 =
2213 GpuResourceRing::next_monotonic_fence(resource_ring.get_completed_fence_id());
2214 let _: usize = resource_ring.complete_fence(prev);
2215 match resource_ring
2216 .acquire_staging(prim_upload_bytes, GpuUploadPriority::RequiredFrame)
2217 {
2218 GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
2219 GpuRingAcquireResult::DeferredFrameRingFull
2220 | GpuRingAcquireResult::DroppedPrefetchRingFull => {
2221 resource_ring.get_next_fence_id()
2222 }
2223 }
2224 }
2225 };
2226 if driver.get_surface_layer_handle() != 0 {
2227 let _: usize = gpu_glyph_atlas.flush_to_driver(driver);
2228 } else {
2229 gpu_glyph_atlas.get_mut_pending_dirty_rects().clear();
2230 gpu_glyph_atlas.set_needs_full_upload(false);
2231 }
2232 let active_slice: &[GpuBatch] = &batches[..(*active_batches).min(batches.len())];
2233 let submitted: usize = driver.submit_batches(phys_w, phys_h, clear_color, active_slice)?;
2234 *last_fence_id = acquired_fence;
2235 let hw_done: u64 = completed_gpu_fence();
2236 if driver.get_device_handle() != 0 && hw_done > 0 {
2237 let _: usize = resource_ring.complete_fence(hw_done.min(acquired_fence));
2238 } else {
2239 let retire_window: u64 = acquired_fence.saturating_sub(2);
2240 if retire_window > 0 {
2241 let _: usize = resource_ring.complete_fence(retire_window);
2242 }
2243 }
2244 *last_submitted_batches = submitted;
2245 *last_coalesced_merges = coalesced_merges;
2246 *last_culled_instances = culled_instances;
2247 *last_uploaded_instances = uploaded_instances;
2248 *last_uploaded_bytes = upload_bytes;
2249 *last_uploaded_primitive_bytes = prim_upload_bytes;
2250 let pixels: usize = if *enable_readback {
2251 let (_, px): (usize, usize) = readback.render(display_list, damage);
2252 *cpu_readback_frames = cpu_readback_frames.saturating_add(1);
2253 *last_cpu_rgba_upload_bytes = (phys_w as usize)
2254 .saturating_mul(phys_h as usize)
2255 .saturating_mul(4);
2256 px
2257 } else {
2258 *direct_gpu_frames = direct_gpu_frames.saturating_add(1);
2259 *last_cpu_rgba_upload_bytes = 0;
2260 (phys_w as usize).saturating_mul(phys_h as usize)
2261 };
2262 Ok((submitted, pixels))
2263 }
2264
2265 fn push_instance_coalesced(
2276 batches: &mut Vec<GpuBatch>,
2277 active_batches: &mut usize,
2278 tail_state: &mut GpuBatchCoalesceState,
2279 clip_entry: GpuClipEntry,
2280 instance: GpuQuadInstance,
2281 coalesced_merges: &mut usize,
2282 ) {
2283 let is_blur: bool = instance.kind == GpuPrimitiveKind::BackdropBlur as u32;
2284 let base_prim: GpuPrimitiveInstance =
2285 GpuPrimitiveInstance::from_quad_instance(&instance, 0, tail_state.layer_id);
2286 Self::push_primitive_coalesced(
2287 batches,
2288 active_batches,
2289 tail_state,
2290 clip_entry,
2291 base_prim,
2292 is_blur,
2293 coalesced_merges,
2294 );
2295 }
2296
2297 fn push_primitive_coalesced(
2309 batches: &mut Vec<GpuBatch>,
2310 active_batches: &mut usize,
2311 tail_state: &mut GpuBatchCoalesceState,
2312 clip_entry: GpuClipEntry,
2313 base_prim: GpuPrimitiveInstance,
2314 is_blur: bool,
2315 coalesced_merges: &mut usize,
2316 ) {
2317 let scissor: Rect = clip_entry.scissor;
2318 let quad_rect: [f32; 4] = base_prim.get_rect();
2319 let inst_unclipped: bool =
2320 !is_blur && Self::quad_inside_rect(quad_rect, clip_entry.safe_inner);
2321 let prim: GpuPrimitiveInstance = if inst_unclipped {
2322 base_prim
2323 } else {
2324 base_prim.with_rounded_clip_bits(clip_entry.rounded_clip_bits)
2325 };
2326 let count: usize = *active_batches;
2327 if count > 0 {
2328 let last: &mut GpuBatch = &mut batches[count - 1];
2329 if last.get_primitives().len() < MAX_QUADS_PER_BATCH
2330 && last.get_layer_id() == tail_state.layer_id
2331 && last.get_scroll_offset() == tail_state.scroll_offset
2332 {
2333 let last_scissor: Rect = last.get_scissor();
2334 let blur_boundary: bool = tail_state.has_backdrop_blur || is_blur;
2335 if last_scissor == scissor && !blur_boundary {
2336 last.get_mut_primitives().push(prim);
2337 tail_state.all_unclipped = tail_state.all_unclipped && inst_unclipped;
2338 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, quad_rect);
2339 return;
2340 }
2341 if !blur_boundary
2342 && inst_unclipped
2343 && Self::quad_inside_rect(quad_rect, last_scissor)
2344 {
2345 last.get_mut_primitives().push(prim);
2346 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, quad_rect);
2347 *coalesced_merges += 1;
2348 return;
2349 }
2350 if !blur_boundary
2351 && tail_state.all_unclipped
2352 && Self::rect_inside_rect(tail_state.bounds, scissor)
2353 {
2354 last.set_scissor(scissor);
2355 last.get_mut_primitives().push(prim);
2356 tail_state.all_unclipped = inst_unclipped;
2357 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, quad_rect);
2358 *coalesced_merges += 1;
2359 return;
2360 }
2361 }
2362 }
2363 if count < batches.len() {
2364 let slot: &mut GpuBatch = &mut batches[count];
2365 slot.set_scissor(scissor);
2366 slot.set_scroll_offset(tail_state.scroll_offset);
2367 slot.set_layer_id(tail_state.layer_id);
2368 slot.get_mut_instances().clear();
2369 slot.get_mut_primitives().clear();
2370 slot.get_mut_primitives().push(prim);
2371 } else {
2372 let mut batch: GpuBatch = GpuBatch::default();
2373 batch.set_scissor(scissor);
2374 batch.set_scroll_offset(tail_state.scroll_offset);
2375 batch.set_layer_id(tail_state.layer_id);
2376 batch.get_mut_primitives().reserve(512);
2377 batch.get_mut_primitives().push(prim);
2378 batches.push(batch);
2379 }
2380 *active_batches = count + 1;
2381 tail_state.all_unclipped = inst_unclipped;
2382 tail_state.has_backdrop_blur = is_blur;
2383 tail_state.bounds = Rect::new(quad_rect[0], quad_rect[1], quad_rect[2], quad_rect[3]);
2384 }
2385
2386 fn rescale_emitted_glyph_uvs(
2396 batches: &mut [GpuBatch],
2397 active_batches: usize,
2398 line_glyphs: &mut [(Option<GpuGlyphPlacement>, f32)],
2399 old_height: u32,
2400 new_height: u32,
2401 ) {
2402 if old_height == 0 || new_height == 0 || old_height == new_height {
2403 return;
2404 }
2405 let v_scale: f32 = (old_height as f32) / (new_height as f32);
2406 for batch in batches.iter_mut().take(active_batches) {
2407 for prim in batch.get_mut_primitives().iter_mut() {
2408 if prim.prim_kind() == PrimKind::GlyphAlpha8 {
2409 let mut uv: [u16; 4] = prim.get_uv_rect();
2410 uv[1] = (f32::from(uv[1]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2411 uv[3] = (f32::from(uv[3]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2412 prim.set_uv_rect(uv);
2413 }
2414 }
2415 }
2416 for (placement_opt, _) in line_glyphs.iter_mut() {
2417 if let Some(placement) = placement_opt.as_mut() {
2418 let mut uv: [u16; 4] = placement.get_uv_rect();
2419 uv[1] = (f32::from(uv[1]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2420 uv[3] = (f32::from(uv[3]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2421 placement.set_uv_rect(uv);
2422 }
2423 }
2424 }
2425
2426 fn rescale_emitted_image_uvs(
2435 batches: &mut [GpuBatch],
2436 active_batches: usize,
2437 old_dims: (u32, u32),
2438 new_dims: (u32, u32),
2439 ) {
2440 let (old_w, old_h): (u32, u32) = old_dims;
2441 let (new_w, new_h): (u32, u32) = new_dims;
2442 if old_w == 0
2443 || old_h == 0
2444 || new_w == 0
2445 || new_h == 0
2446 || (old_w == new_w && old_h == new_h)
2447 {
2448 return;
2449 }
2450 let u_scale: f32 = (old_w as f32) / (new_w as f32);
2451 let v_scale: f32 = (old_h as f32) / (new_h as f32);
2452 for batch in batches.iter_mut().take(active_batches) {
2453 for prim in batch.get_mut_primitives().iter_mut() {
2454 if prim.prim_kind() == PrimKind::ImageTexture {
2455 let mut uv: [u16; 4] = prim.get_uv_rect();
2456 uv[0] = (f32::from(uv[0]) * u_scale).round().clamp(0.0, 65535.0) as u16;
2457 uv[1] = (f32::from(uv[1]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2458 uv[2] = (f32::from(uv[2]) * u_scale).round().clamp(0.0, 65535.0) as u16;
2459 uv[3] = (f32::from(uv[3]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2460 prim.set_uv_rect(uv);
2461 }
2462 }
2463 }
2464 }
2465}