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 8 => PrimKind::BackdropBlur,
166 _ => PrimKind::RoundedRect,
167 }
168 }
169
170 #[must_use]
176 pub fn clip_or_sub_kind(&self) -> u16 {
177 ((self.get_kind_flags() >> 4) & 0xFFF) as u16
178 }
179
180 #[must_use]
190 pub fn encode_clip_radius_6bit(radius_px: f32) -> u16 {
191 let clamped: f32 = radius_px.clamp(0.0, 500.0);
192 if clamped <= 15.5 {
193 (clamped * 2.0).round().clamp(0.0, 31.0) as u16
194 } else if clamped <= 76.0 {
195 32_u16 + (((clamped - 16.0) / 4.0).round().clamp(0.0, 15.0) as u16)
196 } else {
197 48_u16 + (((clamped - 80.0) / 28.0).round().clamp(0.0, 15.0) as u16)
198 }
199 }
200
201 #[must_use]
211 pub fn decode_clip_radius_6bit(encoded: u16) -> f32 {
212 let masked: u16 = encoded & 0x3F;
213 if masked <= 31 {
214 (masked as f32) * 0.5
215 } else if masked <= 47 {
216 16.0 + ((masked - 32) as f32) * 4.0
217 } else {
218 80.0 + ((masked - 48) as f32) * 28.0
219 }
220 }
221
222 #[must_use]
236 pub fn encode_rounded_clip_bits(
237 parent_scissor: Rect,
238 parent_bits: u16,
239 next_scissor: Rect,
240 pushed_rect: Rect,
241 pushed_radius_phys: Option<CornerRadius>,
242 ) -> u16 {
243 if next_scissor.get_width() <= 0.0 || next_scissor.get_height() <= 0.0 {
244 return 0;
245 }
246 let parent_mask: u16 = parent_bits & 0xF;
247 let parent_q_rad: u16 = (parent_bits >> 4) & 0x3F;
248 let mut mask: u16 = 0;
249 if parent_mask != 0 && parent_q_rad > 0 {
250 let same_left: bool = (next_scissor.get_x() - parent_scissor.get_x()).abs() <= 0.5;
251 let same_top: bool = (next_scissor.get_y() - parent_scissor.get_y()).abs() <= 0.5;
252 let same_right: bool = (next_scissor.right() - parent_scissor.right()).abs() <= 0.5;
253 let same_bottom: bool = (next_scissor.bottom() - parent_scissor.bottom()).abs() <= 0.5;
254 if (parent_mask & 1) != 0 && same_left && same_top {
255 mask |= 1;
256 }
257 if (parent_mask & 2) != 0 && same_right && same_top {
258 mask |= 2;
259 }
260 if (parent_mask & 4) != 0 && same_right && same_bottom {
261 mask |= 4;
262 }
263 if (parent_mask & 8) != 0 && same_left && same_bottom {
264 mask |= 8;
265 }
266 }
267 let mut q_rad: u16 = if mask != 0 { parent_q_rad } else { 0 };
268 if let Some(rad) = pushed_radius_phys {
269 let cap_r: f32 = (pushed_rect.get_width() * 0.5)
270 .min(pushed_rect.get_height() * 0.5)
271 .max(0.0);
272 let r_tl: f32 = rad.get_top_left().min(cap_r);
273 let r_tr: f32 = rad.get_top_right().min(cap_r);
274 let r_br: f32 = rad.get_bottom_right().min(cap_r);
275 let r_bl: f32 = rad.get_bottom_left().min(cap_r);
276 let max_pushed_r: f32 = r_tl.max(r_tr).max(r_br).max(r_bl);
277 let pushed_q_rad: u16 = Self::encode_clip_radius_6bit(max_pushed_r);
278 if pushed_q_rad > 0 {
279 let touch_left: bool = (next_scissor.get_x() - pushed_rect.get_x()).abs() <= 0.5;
280 let touch_top: bool = (next_scissor.get_y() - pushed_rect.get_y()).abs() <= 0.5;
281 let touch_right: bool = (next_scissor.right() - pushed_rect.right()).abs() <= 0.5;
282 let touch_bottom: bool =
283 (next_scissor.bottom() - pushed_rect.bottom()).abs() <= 0.5;
284 let mut pushed_mask: u16 = 0;
285 if r_tl > 0.25 && touch_left && touch_top {
286 pushed_mask |= 1;
287 }
288 if r_tr > 0.25 && touch_right && touch_top {
289 pushed_mask |= 2;
290 }
291 if r_br > 0.25 && touch_right && touch_bottom {
292 pushed_mask |= 4;
293 }
294 if r_bl > 0.25 && touch_left && touch_bottom {
295 pushed_mask |= 8;
296 }
297 if pushed_mask != 0 {
298 mask |= pushed_mask;
299 q_rad = q_rad.max(pushed_q_rad);
300 }
301 }
302 }
303 if mask == 0 || q_rad == 0 {
304 0
305 } else {
306 (mask & 0xF) | ((q_rad & 0x3F) << 4)
307 }
308 }
309
310 #[inline(always)]
321 #[must_use]
322 pub fn with_rounded_clip_bits(mut self, rounded_clip_bits: u16) -> Self {
323 if rounded_clip_bits != 0 {
324 let current_flags: u32 = self.get_kind_flags();
325 let next_flags: u32 =
326 (current_flags & !(0x3FF_u32 << 6)) | (((rounded_clip_bits as u32) & 0x3FF) << 6);
327 let _: &mut Self = self.set_kind_flags(next_flags);
328 }
329 self
330 }
331
332 #[must_use]
338 pub fn texture_slot(&self) -> u8 {
339 ((self.get_kind_flags() >> 16) & 0xFF) as u8
340 }
341
342 #[must_use]
348 pub fn layer_id(&self) -> u8 {
349 ((self.get_kind_flags() >> 24) & 0xFF) as u8
350 }
351
352 #[must_use]
364 pub fn from_quad_instance(quad: &GpuQuadInstance, tex_slot: u8, layer_id: u8) -> Self {
365 const {
366 assert!(size_of::<GpuPrimitiveInstance>() == GPU_PRIMITIVE_INSTANCE_BYTES);
367 }
368 let rect: [f32; 4] = quad.get_rect();
369 let clip_rect: [f32; 4] = quad.get_clip();
370 let color_rgba: u32 = Self::pack_rgba8_f32(quad.get_color());
371 let mut aux_color_rgba: u32 = Self::pack_rgba8_f32(quad.get_secondary_color());
372 let mut uv_rect: [u16; 4] = [0, 0, u16::MAX, u16::MAX];
373 let src_radii: [f32; 4] = quad.get_radii();
374 let radius_cap: f32 = Self::corner_radius_cap(rect, quad.get_kind(), quad.get_angle_rad());
375 let mut radii_border: [u16; 4] = [
376 Self::pack_u16_q4(src_radii[0].min(radius_cap)),
377 Self::pack_u16_q4(src_radii[1].min(radius_cap)),
378 Self::pack_u16_q4(src_radii[2].min(radius_cap)),
379 Self::pack_u16_q4(src_radii[3].min(radius_cap)),
380 ];
381 let (prim_kind, sub_kind): (PrimKind, u16) = match quad.get_kind() {
382 1 => (PrimKind::LinearGradientRect, 0),
383 2 => (PrimKind::Shadow, 0),
384 3 => {
385 let is_color_glyph: bool = (quad.get_reserved() & 0x8000_0000) != 0;
386 if is_color_glyph {
387 (PrimKind::GlyphColorRgba8, 0)
388 } else {
389 (PrimKind::GlyphAlpha8, 0)
390 }
391 }
392 4 => (PrimKind::ImageTexture, 0),
393 5 => (PrimKind::VectorShape, VectorSubKind::Circle as u16),
394 6 => {
395 let inv_w: f32 = 1.0 / rect[2].max(1.0);
396 let inv_h: f32 = 1.0 / rect[3].max(1.0);
397 let red_val: [f32; 4] = quad.get_radii();
398 uv_rect = [
399 (((red_val[0] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
400 (((red_val[1] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
401 (((red_val[2] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
402 (((red_val[3] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
403 ];
404 radii_border = [0; 4];
405 (PrimKind::VectorShape, VectorSubKind::Line as u16)
406 }
407 7 => {
408 let inv_w: f32 = 1.0 / rect[2].max(1.0);
409 let inv_h: f32 = 1.0 / rect[3].max(1.0);
410 let p0: [f32; 4] = quad.get_secondary_color();
411 let radius_val: [f32; 4] = quad.get_radii();
412 let p0_nx: f32 = ((p0[0] - rect[0]) * inv_w).clamp(0.0, 1.0);
413 let p0_ny: f32 = ((p0[1] - rect[1]) * inv_h).clamp(0.0, 1.0);
414 aux_color_rgba = Self::pack_rgba8_f32([p0_nx, p0_ny, 0.0, 1.0]);
415 uv_rect = [
416 (((radius_val[0] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
417 (((radius_val[1] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
418 (((radius_val[2] - rect[0]) * inv_w).clamp(0.0, 1.0) * 65535.0).round() as u16,
419 (((radius_val[3] - rect[1]) * inv_h).clamp(0.0, 1.0) * 65535.0).round() as u16,
420 ];
421 radii_border = [
422 (p0_nx * 65535.0).round() as u16,
423 (p0_ny * 65535.0).round() as u16,
424 0,
425 0,
426 ];
427 (PrimKind::VectorShape, VectorSubKind::Triangle as u16)
428 }
429 8 => (PrimKind::BackdropBlur, 0),
430 9 => (PrimKind::ExternalGpuSurface, 0),
431 _ => (PrimKind::RoundedRect, 0),
432 };
433 let kind_flags: u32 = Self::pack_kind_flags(prim_kind, sub_kind, tex_slot, layer_id);
434 let extra_param: u32 = Self::pack_f16x2(quad.get_stroke_or_blur(), quad.get_angle_rad());
435 Self {
436 rect,
437 clip_rect,
438 color_rgba,
439 aux_color_rgba,
440 uv_rect,
441 radii_border,
442 kind_flags,
443 extra_param,
444 }
445 }
446
447 #[must_use]
462 pub fn external_surface(
463 bounds: Rect,
464 clip: Rect,
465 radii: [f32; 4],
466 uv_rect: [u16; 4],
467 tex_slot: u8,
468 layer_id: u8,
469 ) -> Self {
470 Self {
471 rect: [
472 bounds.get_x(),
473 bounds.get_y(),
474 bounds.get_width(),
475 bounds.get_height(),
476 ],
477 clip_rect: [clip.get_x(), clip.get_y(), clip.right(), clip.bottom()],
478 color_rgba: 0xFFFF_FFFF,
479 aux_color_rgba: 0,
480 uv_rect,
481 radii_border: [
482 Self::pack_u16_q4(radii[0]),
483 Self::pack_u16_q4(radii[1]),
484 Self::pack_u16_q4(radii[2]),
485 Self::pack_u16_q4(radii[3]),
486 ],
487 kind_flags: Self::pack_kind_flags(PrimKind::ExternalGpuSurface, 0, tex_slot, layer_id),
488 extra_param: 0,
489 }
490 }
491}
492
493impl GpuLayerUniform {
495 #[must_use]
507 pub fn new(viewport_w: f32, viewport_h: f32, scroll_offset: [f32; 2]) -> Self {
508 const {
509 assert!(size_of::<GpuLayerUniform>() == GPU_LAYER_UNIFORM_BYTES);
510 }
511 Self {
512 viewport_size: [viewport_w.max(1.0), viewport_h.max(1.0)],
513 scroll_offset,
514 }
515 }
516}
517
518impl GpuDriver for RecordingGpuDriver {
520 fn backend(&self) -> GraphicsBackend {
530 self.get_backend_kind()
531 }
532
533 fn shader_source(&self) -> &'static str {
543 match self.get_backend_kind() {
544 GraphicsBackend::Metal => MSL_UI_SHADER,
545 GraphicsBackend::Direct3D12 | GraphicsBackend::Direct3D11 => HLSL_UI_SHADER,
546 GraphicsBackend::WebGl2 | GraphicsBackend::OpenGlEs => GLSL_ES3_UI_FRAGMENT_SHADER,
547 GraphicsBackend::WebGpu
548 | GraphicsBackend::Vulkan
549 | GraphicsBackend::SoftwareCpu
550 | GraphicsBackend::Canvas2d
551 | GraphicsBackend::Headless => WGSL_UI_SHADER,
552 }
553 }
554
555 fn submit_batches(
569 &mut self,
570 _width: u32,
571 _height: u32,
572 clear_color: Color,
573 batches: &[GpuBatch],
574 ) -> Result<usize, PlatformError> {
575 let count: usize = batches.len();
576 let instances: usize = batches
577 .iter()
578 .fold(0_usize, |acc: usize, blue_val: &GpuBatch| {
579 acc + blue_val
580 .get_primitives()
581 .len()
582 .max(blue_val.get_instances().len())
583 });
584 self.set_last_clear(clear_color);
585 self.set_last_instance_count(instances);
586 self.set_total_draw_calls(self.get_total_draw_calls().saturating_add(count));
587 Ok(count)
588 }
589}
590
591impl HardwareRenderer {
593 #[must_use]
607 pub fn with_backend(
608 backend: GraphicsBackend,
609 width: u32,
610 height: u32,
611 config: RenderConfig,
612 enable_readback: bool,
613 ) -> Self {
614 Self::with_backend_and_font(
615 backend,
616 width,
617 height,
618 config,
619 enable_readback,
620 NativeSystemFont::deterministic(),
621 )
622 }
623
624 #[must_use]
639 pub fn with_backend_and_font(
640 backend: GraphicsBackend,
641 width: u32,
642 height: u32,
643 config: RenderConfig,
644 enable_readback: bool,
645 font_provider: NativeSystemFont,
646 ) -> Self {
647 let driver: NativeGpuDriver = NativeGpuDriver::probe(backend);
648 let per_glyph_atlas: bool = config.get_direct_gpu_present();
649 let mut batches: Vec<GpuBatch> = Vec::with_capacity(16);
650 for _ in 0..8usize {
651 let mut batch: GpuBatch = GpuBatch::default();
652 batch.get_mut_primitives().reserve(256);
653 batches.push(batch);
654 }
655 Self {
656 driver,
657 batches,
658 active_batches: 0,
659 clip_stack: Vec::with_capacity(16),
660 last_submitted_batches: 0,
661 last_coalesced_merges: 0,
662 last_culled_instances: 0,
663 last_uploaded_instances: 0,
664 last_uploaded_bytes: 0,
665 readback: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
666 enable_readback,
667 resource_ring: GpuResourceRing::default(),
668 direct_gpu_frames: 0,
669 cpu_readback_frames: 0,
670 last_cpu_rgba_upload_bytes: 0,
671 has_unsupported_effect: false,
672 last_fence_id: 0,
673 blur_passes: Vec::with_capacity(8),
674 active_scroll_offset: [0.0, 0.0],
675 active_layer_id: 0,
676 last_uploaded_primitive_bytes: 0,
677 gpu_glyph_atlas: IncrementalGpuGlyphAtlas::default(),
678 gpu_image_atlas: IncrementalGpuImageAtlas::default(),
679 per_glyph_atlas,
680 }
681 }
682
683 pub fn set_font_provider(&mut self, font_provider: NativeSystemFont) -> &mut Self {
694 let _: &mut SoftwareRenderer = self.get_mut_readback().set_font_provider(font_provider);
695 self
696 }
697
698 pub fn capture_reference_readback(
710 &mut self,
711 display_list: &DisplayList,
712 damage: DamageRegion,
713 ) -> &Framebuffer {
714 let (_, _): (usize, usize) = self.get_mut_readback().render(display_list, damage);
715 self.get_readback().get_framebuffer()
716 }
717
718 pub fn complete_gpu_fence(&mut self, fence_id: u64) -> usize {
729 self.get_mut_resource_ring().complete_fence(fence_id)
730 }
731
732 pub fn trim_gpu_caches(&mut self, retain_ratio: f32) -> usize {
743 self.get_mut_resource_ring()
744 .apply_retention_ratio(retain_ratio)
745 }
746
747 #[must_use]
757 pub fn last_frame_telemetry(&self) -> (usize, usize, usize, usize, usize) {
758 (
759 self.get_last_submitted_batches(),
760 self.get_last_coalesced_merges(),
761 self.get_last_culled_instances(),
762 self.get_last_uploaded_instances(),
763 self.get_last_uploaded_bytes(),
764 )
765 }
766
767 #[must_use]
777 pub fn active_batches_slice(&self) -> &[GpuBatch] {
778 let active: usize = self.get_active_batches().min(self.get_batches().len());
779 &self.get_batches()[..active]
780 }
781
782 #[must_use]
792 pub fn batch_hash(&self) -> u64 {
793 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
794 let active: usize = self.get_active_batches().min(self.get_batches().len());
795 for batch in self.get_batches()[..active].iter() {
796 if !batch.get_primitives().is_empty() {
797 for prim in batch.get_primitives().iter() {
798 for item_val in prim.get_rect().iter().chain(prim.get_clip_rect().iter()) {
799 hash ^= item_val.to_bits() as u64;
800 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
801 }
802 for word in [
803 prim.get_color_rgba(),
804 prim.get_aux_color_rgba(),
805 prim.get_kind_flags(),
806 prim.get_extra_param(),
807 ] {
808 hash ^= word as u64;
809 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
810 }
811 for lane in prim
812 .get_uv_rect()
813 .iter()
814 .chain(prim.get_radii_border().iter())
815 {
816 hash ^= *lane as u64;
817 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
818 }
819 }
820 } else {
821 for inst in batch.get_instances().iter() {
822 for item_val in inst
823 .get_rect()
824 .iter()
825 .chain(inst.get_color().iter())
826 .chain(inst.get_radii().iter())
827 {
828 hash ^= item_val.to_bits() as u64;
829 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
830 }
831 }
832 }
833 }
834 hash
835 }
836
837 #[inline(always)]
848 #[must_use]
849 fn quad_inside_rect(quad: [f32; 4], container: Rect) -> bool {
850 let qx0: f32 = quad[0];
851 let qy0: f32 = quad[1];
852 let qx1: f32 = qx0 + quad[2];
853 let qy1: f32 = qy0 + quad[3];
854 let x_in: bool = qx0 >= container.get_x() && qx1 <= container.right();
855 let y_in: bool = qy0 >= container.get_y() && qy1 <= container.bottom();
856 container.get_width() > 0.0 && container.get_height() > 0.0 && x_in && y_in
857 }
858
859 #[inline(always)]
870 #[must_use]
871 fn quad_intersects_clip(quad: [f32; 4], clip: Rect) -> bool {
872 let qw: f32 = quad[2];
873 let qh: f32 = quad[3];
874 let quad_empty: bool = qw <= 0.0 || qh <= 0.0;
875 let clip_empty: bool = clip.get_width() <= 0.0 || clip.get_height() <= 0.0;
876 if quad_empty || clip_empty {
877 return false;
878 }
879 let qx0: f32 = quad[0];
880 let qy0: f32 = quad[1];
881 let qx1: f32 = qx0 + qw;
882 let qy1: f32 = qy0 + qh;
883 let x_hit: bool = qx1 > clip.get_x() && qx0 < clip.right();
884 let y_hit: bool = qy1 > clip.get_y() && qy0 < clip.bottom();
885 x_hit && y_hit
886 }
887
888 #[inline(always)]
899 #[must_use]
900 fn rect_inside_rect(inner: Rect, outer: Rect) -> bool {
901 let x_in: bool = inner.get_x() >= outer.get_x() && inner.right() <= outer.right();
902 let y_in: bool = inner.get_y() >= outer.get_y() && inner.bottom() <= outer.bottom();
903 x_in && y_in
904 }
905
906 #[inline(always)]
917 #[must_use]
918 fn union_rect_quad(existing: Rect, quad: [f32; 4]) -> Rect {
919 let x0: f32 = existing.get_x().min(quad[0]);
920 let y0: f32 = existing.get_y().min(quad[1]);
921 let x1: f32 = existing.right().max(quad[0] + quad[2]);
922 let y1: f32 = existing.bottom().max(quad[1] + quad[3]);
923 Rect::new(x0, y0, (x1 - x0).max(0.0), (y1 - y0).max(0.0))
924 }
925
926 pub fn render(
938 &mut self,
939 display_list: &DisplayList,
940 damage: DamageRegion,
941 ) -> Result<(usize, usize), PlatformError> {
942 self.render_with_physical_size(display_list, damage, None)
943 }
944
945 pub fn render_with_physical_size(
958 &mut self,
959 display_list: &DisplayList,
960 damage: DamageRegion,
961 physical_size: Option<(u32, u32)>,
962 ) -> Result<(usize, usize), PlatformError> {
963 let scale: f32 = display_list.get_scale_factor().clamp(0.25, 16.0);
964 let (phys_w, phys_h): (u32, u32) = match physical_size {
965 Some((width_px, height_px)) if width_px > 0 && height_px > 0 => (width_px, height_px),
966 _ => (
967 ((display_list.get_viewport().get_width() * scale).round() as u32).max(1),
968 ((display_list.get_viewport().get_height() * scale).round() as u32).max(1),
969 ),
970 };
971 let max_blur_px: f32 = (64.0 * scale).clamp(64.0, 512.0);
972 let full_clip: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
973 let full_entry: GpuClipEntry = GpuClipEntry {
974 scissor: full_clip,
975 safe_inner: full_clip,
976 rounded_clip_bits: 0,
977 };
978 let Self {
979 driver,
980 batches,
981 active_batches,
982 clip_stack,
983 last_submitted_batches,
984 last_coalesced_merges,
985 last_culled_instances,
986 last_uploaded_instances,
987 last_uploaded_bytes,
988 readback,
989 enable_readback,
990 resource_ring,
991 direct_gpu_frames,
992 cpu_readback_frames,
993 last_cpu_rgba_upload_bytes,
994 has_unsupported_effect,
995 last_fence_id,
996 blur_passes,
997 active_scroll_offset,
998 active_layer_id,
999 last_uploaded_primitive_bytes,
1000 gpu_glyph_atlas,
1001 gpu_image_atlas,
1002 per_glyph_atlas,
1003 } = self;
1004 gpu_image_atlas.begin_frame();
1005 let mut current_glyph_h: u32 = gpu_glyph_atlas.get_height();
1006 let mut current_img_w: u32 = gpu_image_atlas.get_width();
1007 let mut current_img_h: u32 = gpu_image_atlas.get_height();
1008 clip_stack.clear();
1009 clip_stack.push(full_entry);
1010 blur_passes.clear();
1011 *active_batches = 0;
1012 *has_unsupported_effect = false;
1013 let layer_scroll: [f32; 2] = *active_scroll_offset;
1014 let layer_id: u8 = *active_layer_id;
1015 let gpu_supports_blur: bool = driver.supports_backdrop_blur_pass()
1016 || matches!(
1017 driver.backend(),
1018 GraphicsBackend::Metal
1019 | GraphicsBackend::WebGl2
1020 | GraphicsBackend::OpenGlEs
1021 | GraphicsBackend::WebGpu
1022 | GraphicsBackend::Vulkan
1023 | GraphicsBackend::Direct3D12
1024 | GraphicsBackend::Direct3D11
1025 );
1026 let mut tail_state: GpuBatchCoalesceState = GpuBatchCoalesceState {
1027 scroll_offset: layer_scroll,
1028 layer_id,
1029 ..GpuBatchCoalesceState::default()
1030 };
1031 let mut coalesced_merges: usize = 0;
1032 let mut culled_instances: usize = 0;
1033 let mut uploaded_instances: usize = 0;
1034 let mut clear_color: Color = COLOR_WHITE;
1035 for cmd in display_list.get_commands().iter() {
1036 let clip_entry: GpuClipEntry = clip_stack.last().copied().unwrap_or(full_entry);
1037 let active_clip: Rect = clip_entry.scissor;
1038 match cmd {
1039 DrawCommand::Clear(color_item) => {
1040 clear_color = *color_item;
1041 }
1042 DrawCommand::PushClip(rect) => {
1043 let phys: Rect = snap_physical_rect(*rect, scale);
1044 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
1045 let next_safe: Rect = clip_entry.safe_inner.intersect(phys).unwrap_or_default();
1046 let next_bits: u16 = GpuPrimitiveInstance::encode_rounded_clip_bits(
1047 active_clip,
1048 clip_entry.rounded_clip_bits,
1049 next_scissor,
1050 phys,
1051 None,
1052 );
1053 clip_stack.push(GpuClipEntry {
1054 scissor: next_scissor,
1055 safe_inner: next_safe,
1056 rounded_clip_bits: next_bits,
1057 });
1058 }
1059 DrawCommand::PushRoundedClip { rect, radius } => {
1060 let phys: Rect = snap_physical_rect(*rect, scale);
1061 let next_scissor: Rect = active_clip.intersect(phys).unwrap_or_default();
1062 let phys_radius: CornerRadius = CornerRadius::new(
1063 radius.get_top_left() * scale,
1064 radius.get_top_right() * scale,
1065 radius.get_bottom_right() * scale,
1066 radius.get_bottom_left() * scale,
1067 );
1068 let max_r: f32 = phys_radius.max_radius().ceil();
1069 let inset_w: f32 = (phys.get_width() - max_r * 2.0).max(0.0);
1070 let inset_h: f32 = (phys.get_height() - max_r * 2.0).max(0.0);
1071 let rounded_safe: Rect =
1072 Rect::new(phys.get_x() + max_r, phys.get_y() + max_r, inset_w, inset_h);
1073 let next_safe: Rect = clip_entry
1074 .safe_inner
1075 .intersect(rounded_safe)
1076 .unwrap_or_default();
1077 let next_bits: u16 = GpuPrimitiveInstance::encode_rounded_clip_bits(
1078 active_clip,
1079 clip_entry.rounded_clip_bits,
1080 next_scissor,
1081 phys,
1082 Some(phys_radius),
1083 );
1084 clip_stack.push(GpuClipEntry {
1085 scissor: next_scissor,
1086 safe_inner: next_safe,
1087 rounded_clip_bits: next_bits,
1088 });
1089 }
1090 DrawCommand::PopClip => {
1091 if clip_stack.len() > 1 {
1092 let _: Option<GpuClipEntry> = clip_stack.pop();
1093 }
1094 }
1095 DrawCommand::FillRoundedRect {
1096 rect,
1097 color,
1098 radius,
1099 border,
1100 } => {
1101 let snapped: Rect = snap_physical_rect(*rect, scale);
1102 let quad: [f32; 4] = [
1103 snapped.get_x(),
1104 snapped.get_y(),
1105 snapped.get_width(),
1106 snapped.get_height(),
1107 ];
1108 if !Self::quad_intersects_clip(quad, active_clip) {
1109 culled_instances += 1;
1110 continue;
1111 }
1112 let border_width: f32 = if border.get_width() > 0.0 {
1113 (border.get_width() * scale).max(1.0)
1114 } else {
1115 0.0
1116 };
1117 let inst: GpuQuadInstance = GpuQuadInstance {
1118 rect: quad,
1119 clip: [
1120 active_clip.get_x(),
1121 active_clip.get_y(),
1122 active_clip.right(),
1123 active_clip.bottom(),
1124 ],
1125 color: color.to_f32_array(),
1126 secondary_color: border.get_color().to_f32_array(),
1127 radii: [
1128 radius.get_top_left() * scale,
1129 radius.get_top_right() * scale,
1130 radius.get_bottom_right() * scale,
1131 radius.get_bottom_left() * scale,
1132 ],
1133 stroke_or_blur: border_width,
1134 angle_rad: 0.0,
1135 kind: GpuPrimitiveKind::RoundedRect as u32,
1136 reserved: 0,
1137 };
1138 Self::push_instance_coalesced(
1139 batches,
1140 active_batches,
1141 &mut tail_state,
1142 clip_entry,
1143 inst,
1144 &mut coalesced_merges,
1145 );
1146 uploaded_instances += 1;
1147 }
1148 DrawCommand::LinearGradientRect {
1149 rect,
1150 gradient,
1151 radius,
1152 } => {
1153 let snapped: Rect = snap_physical_rect(*rect, scale);
1154 let quad: [f32; 4] = [
1155 snapped.get_x(),
1156 snapped.get_y(),
1157 snapped.get_width(),
1158 snapped.get_height(),
1159 ];
1160 if !Self::quad_intersects_clip(quad, active_clip) {
1161 culled_instances += 1;
1162 continue;
1163 }
1164 let inst: GpuQuadInstance = GpuQuadInstance {
1165 rect: quad,
1166 clip: [
1167 active_clip.get_x(),
1168 active_clip.get_y(),
1169 active_clip.right(),
1170 active_clip.bottom(),
1171 ],
1172 color: gradient.get_start_color().to_f32_array(),
1173 secondary_color: gradient.get_end_color().to_f32_array(),
1174 radii: [
1175 radius.get_top_left() * scale,
1176 radius.get_top_right() * scale,
1177 radius.get_bottom_right() * scale,
1178 radius.get_bottom_left() * scale,
1179 ],
1180 stroke_or_blur: 0.0,
1181 angle_rad: gradient.get_angle_deg().to_radians(),
1182 kind: GpuPrimitiveKind::Gradient as u32,
1183 reserved: 0,
1184 };
1185 Self::push_instance_coalesced(
1186 batches,
1187 active_batches,
1188 &mut tail_state,
1189 clip_entry,
1190 inst,
1191 &mut coalesced_merges,
1192 );
1193 uploaded_instances += 1;
1194 }
1195 DrawCommand::BoxShadow {
1196 rect,
1197 radius,
1198 shadow,
1199 } => {
1200 let spread: f32 = shadow.get_spread_radius() * scale;
1201 let blur: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(max_blur_px);
1202 let sigma: f32 = (blur * 0.5).max(0.75);
1203 let pad: f32 = (sigma * 3.0).ceil();
1204 let base_rect: Rect = snap_physical_rect(*rect, scale);
1205 let caster_x: f32 =
1206 base_rect.get_x() + (shadow.get_offset_x() * scale).round() - spread;
1207 let caster_y: f32 =
1208 base_rect.get_y() + (shadow.get_offset_y() * scale).round() - spread;
1209 let caster_w: f32 = (base_rect.get_width() + spread * 2.0).max(1.0);
1210 let caster_h: f32 = (base_rect.get_height() + spread * 2.0).max(1.0);
1211 let quad: [f32; 4] = [
1212 caster_x - pad,
1213 caster_y - pad,
1214 caster_w + pad * 2.0,
1215 caster_h + pad * 2.0,
1216 ];
1217 if !Self::quad_intersects_clip(quad, active_clip) {
1218 culled_instances += 1;
1219 continue;
1220 }
1221 let inst: GpuQuadInstance = GpuQuadInstance {
1222 rect: quad,
1223 clip: [
1224 active_clip.get_x(),
1225 active_clip.get_y(),
1226 active_clip.right(),
1227 active_clip.bottom(),
1228 ],
1229 color: shadow.get_color().to_f32_array(),
1230 secondary_color: [0.0; 4],
1231 radii: [
1232 (radius.get_top_left() * scale + spread).max(0.0),
1233 (radius.get_top_right() * scale + spread).max(0.0),
1234 (radius.get_bottom_right() * scale + spread).max(0.0),
1235 (radius.get_bottom_left() * scale + spread).max(0.0),
1236 ],
1237 stroke_or_blur: sigma,
1238 angle_rad: pad,
1239 kind: GpuPrimitiveKind::Shadow as u32,
1240 reserved: 0,
1241 };
1242 Self::push_instance_coalesced(
1243 batches,
1244 active_batches,
1245 &mut tail_state,
1246 clip_entry,
1247 inst,
1248 &mut coalesced_merges,
1249 );
1250 uploaded_instances += 1;
1251 }
1252 DrawCommand::BackdropBlur {
1253 rect,
1254 radius,
1255 blur_radius,
1256 } => {
1257 if !gpu_supports_blur {
1258 *has_unsupported_effect = true;
1259 }
1260 let snapped: Rect = snap_physical_rect(*rect, scale);
1261 let quad: [f32; 4] = [
1262 snapped.get_x(),
1263 snapped.get_y(),
1264 snapped.get_width(),
1265 snapped.get_height(),
1266 ];
1267 let Some(vis_blur) = snapped.intersect(active_clip) else {
1268 culled_instances += 1;
1269 continue;
1270 };
1271 if vis_blur.get_width() <= 0.0 || vis_blur.get_height() <= 0.0 {
1272 culled_instances += 1;
1273 continue;
1274 }
1275 let phys_blur: f32 = (*blur_radius * scale).clamp(0.5, max_blur_px);
1276 let radii_px: [f32; 4] = [
1277 radius.get_top_left() * scale,
1278 radius.get_top_right() * scale,
1279 radius.get_bottom_right() * scale,
1280 radius.get_bottom_left() * scale,
1281 ];
1282 let halo_x0: f32 = (vis_blur.get_x() - phys_blur)
1283 .max(active_clip.get_x())
1284 .max(0.0);
1285 let halo_y0: f32 = (vis_blur.get_y() - phys_blur)
1286 .max(active_clip.get_y())
1287 .max(0.0);
1288 let halo_x1: f32 = (vis_blur.right() + phys_blur)
1289 .min(active_clip.right())
1290 .min(phys_w as f32);
1291 let halo_y1: f32 = (vis_blur.bottom() + phys_blur)
1292 .min(active_clip.bottom())
1293 .min(phys_h as f32);
1294 let halo: Rect = Rect::new(
1295 halo_x0,
1296 halo_y0,
1297 (halo_x1 - halo_x0).max(0.0),
1298 (halo_y1 - halo_y0).max(0.0),
1299 );
1300 let pass_layer_id: u8 = tail_state.layer_id;
1301 blur_passes.push(GpuBackdropBlurPass {
1302 bounds: snapped,
1303 input_bounds: halo,
1304 clip_rect: active_clip,
1305 radii: radii_px,
1306 blur_radius: phys_blur,
1307 layer_id: pass_layer_id,
1308 });
1309 let inv_vp_w: f32 = 1.0 / (phys_w.max(1) as f32);
1310 let inv_vp_h: f32 = 1.0 / (phys_h.max(1) as f32);
1311 let blur_uv: [u16; 4] = [
1312 (snapped.get_x() * inv_vp_w * 65535.0)
1313 .round()
1314 .clamp(0.0, 65535.0) as u16,
1315 (snapped.get_y() * inv_vp_h * 65535.0)
1316 .round()
1317 .clamp(0.0, 65535.0) as u16,
1318 (snapped.right() * inv_vp_w * 65535.0)
1319 .round()
1320 .clamp(0.0, 65535.0) as u16,
1321 (snapped.bottom() * inv_vp_h * 65535.0)
1322 .round()
1323 .clamp(0.0, 65535.0) as u16,
1324 ];
1325 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
1326 let blur_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1327 rect: quad,
1328 clip_rect: [
1329 active_clip.get_x(),
1330 active_clip.get_y(),
1331 active_clip.right(),
1332 active_clip.bottom(),
1333 ],
1334 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
1335 aux_color_rgba: 0,
1336 uv_rect: blur_uv,
1337 radii_border: [
1338 GpuPrimitiveInstance::pack_u16_q4(radii_px[0].min(radius_cap)),
1339 GpuPrimitiveInstance::pack_u16_q4(radii_px[1].min(radius_cap)),
1340 GpuPrimitiveInstance::pack_u16_q4(radii_px[2].min(radius_cap)),
1341 GpuPrimitiveInstance::pack_u16_q4(radii_px[3].min(radius_cap)),
1342 ],
1343 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
1344 PrimKind::BackdropBlur,
1345 0,
1346 0,
1347 pass_layer_id,
1348 ),
1349 extra_param: GpuPrimitiveInstance::pack_f16x2(phys_blur, 0.0),
1350 };
1351 Self::push_primitive_coalesced(
1352 batches,
1353 active_batches,
1354 &mut tail_state,
1355 clip_entry,
1356 blur_prim,
1357 true,
1358 &mut coalesced_merges,
1359 );
1360 tail_state.layer_id = tail_state.layer_id.saturating_add(1);
1361 uploaded_instances += 1;
1362 }
1363 DrawCommand::TextRun { rect, text, style } => {
1364 let run_quad: [f32; 4] = [
1365 rect.get_x() * scale,
1366 rect.get_y() * scale,
1367 rect.get_width() * scale,
1368 rect.get_height() * scale,
1369 ];
1370 if !Self::quad_intersects_clip(run_quad, active_clip) {
1371 culled_instances += 1;
1372 continue;
1373 }
1374 if *per_glyph_atlas {
1375 let wrap_w: Option<f32> = if matches!(
1376 style.get_overflow(),
1377 TextOverflow::Wrap | TextOverflow::Ellipsis
1378 ) {
1379 Some(rect.get_width().max(1.0))
1380 } else {
1381 None
1382 };
1383 let layout_res: TextLayoutResult =
1384 DeterministicTextMeasurer.measure(text, *style, wrap_w);
1385 let phys_font_px: f32 = (style.get_font_size() * scale).max(6.0);
1386 let letter_sp: f32 = style.get_letter_spacing() * scale;
1387 let line_h_phys: f32 = layout_res.get_line_height_px() * scale;
1388 let font_provider: NativeSystemFont = readback.get_font_provider().clone();
1389 let cpu_atlas: &mut GlyphAtlas = readback.get_mut_glyph_atlas();
1390 let text_color_u32: u32 =
1391 GpuPrimitiveInstance::pack_color_rgba8(style.get_color());
1392 let clip_arr: [f32; 4] = [
1393 active_clip.get_x(),
1394 active_clip.get_y(),
1395 active_clip.right(),
1396 active_clip.bottom(),
1397 ];
1398 let box_w_phys: f32 = rect.get_width() * scale;
1399 let base_x_phys: f32 = (rect.get_x() * scale).round();
1400 let mut line_glyphs: Vec<(Option<GpuGlyphPlacement>, f32)> =
1401 Vec::with_capacity(32);
1402 for (line_idx, line) in layout_res.get_lines().iter().enumerate() {
1403 let line_y: f32 =
1404 (rect.get_y() * scale + (line_idx as f32) * line_h_phys).round();
1405 line_glyphs.clear();
1406 let mut actual_line_w: f32 = 0.0;
1407 for ch in line.get_text().chars() {
1408 if ch == ' ' || ch == '\t' {
1409 let space_adv: f32 =
1410 measure_char_advance(ch, *style) * scale + letter_sp;
1411 actual_line_w += space_adv;
1412 line_glyphs.push((None, space_adv));
1413 } else {
1414 let key: GlyphKey =
1415 GlyphKey::from_text_style(ch, phys_font_px, *style);
1416 let raster: &RasterizedGlyph =
1417 cpu_atlas.get_or_rasterize_with(key, &font_provider);
1418 let adv_phys: f32 =
1419 (raster.get_advance() as f32).max(1.0) + letter_sp;
1420 let placement: GpuGlyphPlacement =
1421 gpu_glyph_atlas.insert_or_get(key, raster);
1422 let next_glyph_h: u32 = gpu_glyph_atlas.get_height();
1423 if next_glyph_h != current_glyph_h && next_glyph_h > 0 {
1424 Self::rescale_emitted_glyph_uvs(
1425 batches.as_mut_slice(),
1426 *active_batches,
1427 line_glyphs.as_mut_slice(),
1428 current_glyph_h,
1429 next_glyph_h,
1430 );
1431 current_glyph_h = next_glyph_h;
1432 }
1433 actual_line_w += adv_phys;
1434 line_glyphs.push((Some(placement), adv_phys));
1435 }
1436 }
1437 let fit_scale: f32 =
1438 if box_w_phys > 1.0 && actual_line_w > box_w_phys + 1.0 {
1439 (box_w_phys / actual_line_w).clamp(0.72, 1.0)
1440 } else {
1441 1.0
1442 };
1443 let fitted_w: f32 = actual_line_w * fit_scale;
1444 let align_x: f32 = match style.get_align() {
1445 TextAlign::Left => 0.0,
1446 TextAlign::Center => ((box_w_phys - fitted_w) * 0.5).max(0.0),
1447 TextAlign::Right => (box_w_phys - fitted_w).max(0.0),
1448 };
1449 let mut pen_x: f32 = base_x_phys + align_x.round();
1450 for &(placement_opt, adv_phys) in line_glyphs.iter() {
1451 if let Some(placement) = placement_opt
1452 && placement.get_width() > 0
1453 && placement.get_height() > 0
1454 {
1455 let gw: f32 = placement.get_width() as f32;
1456 let gh: f32 = placement.get_height() as f32;
1457 let glyph_y: f32 = line_y + ((line_h_phys - gh) * 0.5).round();
1458 let glyph_quad: [f32; 4] = [pen_x.round(), glyph_y, gw, gh];
1459 if Self::quad_intersects_clip(glyph_quad, active_clip) {
1460 let kind_flags: u32 = GpuPrimitiveInstance::pack_kind_flags(
1461 PrimKind::GlyphAlpha8,
1462 0,
1463 0,
1464 tail_state.layer_id,
1465 );
1466 let glyph_prim: GpuPrimitiveInstance =
1467 GpuPrimitiveInstance {
1468 rect: glyph_quad,
1469 clip_rect: clip_arr,
1470 color_rgba: text_color_u32,
1471 aux_color_rgba: 0,
1472 uv_rect: placement.get_uv_rect(),
1473 radii_border: [0; 4],
1474 kind_flags,
1475 extra_param: 0,
1476 };
1477 Self::push_primitive_coalesced(
1478 batches,
1479 active_batches,
1480 &mut tail_state,
1481 clip_entry,
1482 glyph_prim,
1483 false,
1484 &mut coalesced_merges,
1485 );
1486 uploaded_instances += 1;
1487 } else {
1488 culled_instances += 1;
1489 }
1490 }
1491 pen_x += adv_phys * fit_scale;
1492 }
1493 }
1494 } else {
1495 let glyph_count: usize = if text.is_ascii() {
1496 text.as_bytes()
1497 .iter()
1498 .filter(|byte_item: &&u8| **byte_item != b' ')
1499 .count()
1500 .max(1)
1501 } else {
1502 text.chars()
1503 .filter(|char_item: &char| *char_item != ' ')
1504 .count()
1505 .max(1)
1506 };
1507 let text_instance: GpuQuadInstance = GpuQuadInstance {
1508 rect: run_quad,
1509 clip: [
1510 active_clip.get_x(),
1511 active_clip.get_y(),
1512 active_clip.right(),
1513 active_clip.bottom(),
1514 ],
1515 color: style.get_color().to_f32_array(),
1516 secondary_color: [0.0; 4],
1517 radii: [0.0; 4],
1518 stroke_or_blur: style.get_font_size() * scale,
1519 angle_rad: 0.0,
1520 kind: GpuPrimitiveKind::Glyph as u32,
1521 reserved: glyph_count as u32,
1522 };
1523 Self::push_instance_coalesced(
1524 batches,
1525 active_batches,
1526 &mut tail_state,
1527 clip_entry,
1528 text_instance,
1529 &mut coalesced_merges,
1530 );
1531 uploaded_instances += 1;
1532 }
1533 }
1534 DrawCommand::TextCaret {
1535 text_rect,
1536 content_rect,
1537 text,
1538 style,
1539 caret_height,
1540 color,
1541 multiline,
1542 } => {
1543 if active_clip.get_width() <= 0.0 || active_clip.get_height() <= 0.0 {
1544 culled_instances += 1;
1545 continue;
1546 }
1547 let meas: TextLayoutResult = DeterministicTextMeasurer.measure(
1548 text,
1549 *style,
1550 if *multiline {
1551 Some(content_rect.get_width().max(1.0))
1552 } else {
1553 None
1554 },
1555 );
1556 let phys_font_px: f32 = (style.get_font_size() * scale).max(6.0);
1557 let letter_sp: f32 = style.get_letter_spacing() * scale;
1558 let last_w_phys: f32 = if let Some(line) = meas.get_lines().last() {
1559 let font_provider: NativeSystemFont = readback.get_font_provider().clone();
1560 let atlas: &mut GlyphAtlas = readback.get_mut_glyph_atlas();
1561 let mut acc: f32 = 0.0;
1562 for ch in line.get_text().chars() {
1563 if ch == ' ' || ch == '\t' {
1564 acc += measure_char_advance(ch, *style) * scale + letter_sp;
1565 } else {
1566 let key: GlyphKey =
1567 GlyphKey::from_text_style(ch, phys_font_px, *style);
1568 let green_val: &RasterizedGlyph =
1569 atlas.get_or_rasterize_with(key, &font_provider);
1570 acc += (green_val.get_advance() as f32).max(1.0) + letter_sp;
1571 }
1572 }
1573 acc
1574 } else {
1575 0.0
1576 };
1577 let line_idx: usize = meas.get_lines().len().saturating_sub(1);
1578 let trailing_gap_phys: f32 = if last_w_phys > 0.0 { 1.0 * scale } else { 0.0 };
1579 let min_x_phys: f32 = content_rect.get_x() * scale;
1580 let max_x_phys: f32 = ((content_rect.right() - 2.0) * scale).max(min_x_phys);
1581 let caret_x_phys: f32 =
1582 (text_rect.get_x() * scale + last_w_phys + trailing_gap_phys)
1583 .min(max_x_phys)
1584 .max(min_x_phys);
1585 let caret_y: f32 = if *multiline {
1586 (content_rect.get_y()
1587 + (line_idx as f32) * meas.get_line_height_px()
1588 + ((meas.get_line_height_px() - caret_height) * 0.5).max(0.0))
1589 .min(content_rect.bottom() - caret_height)
1590 .max(content_rect.get_y())
1591 } else {
1592 content_rect.get_y()
1593 + ((content_rect.get_height() - caret_height) * 0.5).max(0.0)
1594 };
1595 let quad: [f32; 4] = [
1596 caret_x_phys.round(),
1597 (caret_y * scale).round(),
1598 (1.5 * scale).max(1.0),
1599 (caret_height * scale).max(2.0),
1600 ];
1601 if !Self::quad_intersects_clip(quad, active_clip) {
1602 culled_instances += 1;
1603 continue;
1604 }
1605 let inst: GpuQuadInstance = GpuQuadInstance {
1606 rect: quad,
1607 clip: [
1608 active_clip.get_x(),
1609 active_clip.get_y(),
1610 active_clip.right(),
1611 active_clip.bottom(),
1612 ],
1613 color: color.to_f32_array(),
1614 secondary_color: [0.0; 4],
1615 radii: [0.5 * scale; 4],
1616 stroke_or_blur: 0.0,
1617 angle_rad: 0.0,
1618 kind: GpuPrimitiveKind::RoundedRect as u32,
1619 reserved: 0,
1620 };
1621 Self::push_instance_coalesced(
1622 batches,
1623 active_batches,
1624 &mut tail_state,
1625 clip_entry,
1626 inst,
1627 &mut coalesced_merges,
1628 );
1629 uploaded_instances += 1;
1630 }
1631 DrawCommand::Image {
1632 rect,
1633 buffer,
1634 radius,
1635 } => {
1636 let snapped: Rect = snap_physical_rect(*rect, scale);
1637 let quad: [f32; 4] = [
1638 snapped.get_x(),
1639 snapped.get_y(),
1640 snapped.get_width(),
1641 snapped.get_height(),
1642 ];
1643 if !Self::quad_intersects_clip(quad, active_clip) {
1644 culled_instances += 1;
1645 continue;
1646 }
1647 let uv_rect: [u16; 4] = gpu_image_atlas
1648 .insert_or_get(
1649 buffer.get_width(),
1650 buffer.get_height(),
1651 buffer.get_pixels().as_ref(),
1652 )
1653 .map_or([0, 0, u16::MAX, u16::MAX], |slot: GpuImagePlacement| {
1654 slot.get_uv_rect()
1655 });
1656 let next_img_w: u32 = gpu_image_atlas.get_width();
1657 let next_img_h: u32 = gpu_image_atlas.get_height();
1658 if (next_img_w != current_img_w || next_img_h != current_img_h)
1659 && next_img_w > 0
1660 && next_img_h > 0
1661 {
1662 Self::rescale_emitted_image_uvs(
1663 batches.as_mut_slice(),
1664 *active_batches,
1665 (current_img_w, current_img_h),
1666 (next_img_w, next_img_h),
1667 );
1668 current_img_w = next_img_w;
1669 current_img_h = next_img_h;
1670 }
1671 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
1672 let img_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1673 rect: quad,
1674 clip_rect: [
1675 active_clip.get_x(),
1676 active_clip.get_y(),
1677 active_clip.right(),
1678 active_clip.bottom(),
1679 ],
1680 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
1681 aux_color_rgba: 0,
1682 uv_rect,
1683 radii_border: [
1684 GpuPrimitiveInstance::pack_u16_q4(
1685 (radius.get_top_left() * scale).min(radius_cap),
1686 ),
1687 GpuPrimitiveInstance::pack_u16_q4(
1688 (radius.get_top_right() * scale).min(radius_cap),
1689 ),
1690 GpuPrimitiveInstance::pack_u16_q4(
1691 (radius.get_bottom_right() * scale).min(radius_cap),
1692 ),
1693 GpuPrimitiveInstance::pack_u16_q4(
1694 (radius.get_bottom_left() * scale).min(radius_cap),
1695 ),
1696 ],
1697 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
1698 PrimKind::ImageTexture,
1699 0,
1700 0,
1701 tail_state.layer_id,
1702 ),
1703 extra_param: 0,
1704 };
1705 Self::push_primitive_coalesced(
1706 batches,
1707 active_batches,
1708 &mut tail_state,
1709 clip_entry,
1710 img_prim,
1711 false,
1712 &mut coalesced_merges,
1713 );
1714 uploaded_instances += 1;
1715 }
1716 DrawCommand::SharedImage {
1717 rect,
1718 width,
1719 height,
1720 pixels,
1721 radius,
1722 } => {
1723 let snapped: Rect = snap_physical_rect(*rect, scale);
1724 let quad: [f32; 4] = [
1725 snapped.get_x(),
1726 snapped.get_y(),
1727 snapped.get_width(),
1728 snapped.get_height(),
1729 ];
1730 if !Self::quad_intersects_clip(quad, active_clip) {
1731 culled_instances += 1;
1732 continue;
1733 }
1734 let uv_rect: [u16; 4] = gpu_image_atlas
1735 .insert_or_get(*width, *height, pixels.as_slice())
1736 .map_or([0, 0, u16::MAX, u16::MAX], |slot: GpuImagePlacement| {
1737 slot.get_uv_rect()
1738 });
1739 let next_img_w: u32 = gpu_image_atlas.get_width();
1740 let next_img_h: u32 = gpu_image_atlas.get_height();
1741 if (next_img_w != current_img_w || next_img_h != current_img_h)
1742 && next_img_w > 0
1743 && next_img_h > 0
1744 {
1745 Self::rescale_emitted_image_uvs(
1746 batches.as_mut_slice(),
1747 *active_batches,
1748 (current_img_w, current_img_h),
1749 (next_img_w, next_img_h),
1750 );
1751 current_img_w = next_img_w;
1752 current_img_h = next_img_h;
1753 }
1754 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
1755 let img_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1756 rect: quad,
1757 clip_rect: [
1758 active_clip.get_x(),
1759 active_clip.get_y(),
1760 active_clip.right(),
1761 active_clip.bottom(),
1762 ],
1763 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
1764 aux_color_rgba: 0,
1765 uv_rect,
1766 radii_border: [
1767 GpuPrimitiveInstance::pack_u16_q4(
1768 (radius.get_top_left() * scale).min(radius_cap),
1769 ),
1770 GpuPrimitiveInstance::pack_u16_q4(
1771 (radius.get_top_right() * scale).min(radius_cap),
1772 ),
1773 GpuPrimitiveInstance::pack_u16_q4(
1774 (radius.get_bottom_right() * scale).min(radius_cap),
1775 ),
1776 GpuPrimitiveInstance::pack_u16_q4(
1777 (radius.get_bottom_left() * scale).min(radius_cap),
1778 ),
1779 ],
1780 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
1781 PrimKind::ImageTexture,
1782 0,
1783 0,
1784 tail_state.layer_id,
1785 ),
1786 extra_param: 0,
1787 };
1788 Self::push_primitive_coalesced(
1789 batches,
1790 active_batches,
1791 &mut tail_state,
1792 clip_entry,
1793 img_prim,
1794 false,
1795 &mut coalesced_merges,
1796 );
1797 uploaded_instances += 1;
1798 }
1799 DrawCommand::Circle {
1800 center,
1801 radius,
1802 color,
1803 } => {
1804 let red_val: f32 = radius * scale;
1805 let pad: f32 = 1.5;
1806 let extent: f32 = red_val + pad;
1807 let quad: [f32; 4] = [
1808 center.get_x() * scale - extent,
1809 center.get_y() * scale - extent,
1810 extent * 2.0,
1811 extent * 2.0,
1812 ];
1813 if !Self::quad_intersects_clip(quad, active_clip) {
1814 culled_instances += 1;
1815 continue;
1816 }
1817 let inst: GpuQuadInstance = GpuQuadInstance {
1818 rect: quad,
1819 clip: [
1820 active_clip.get_x(),
1821 active_clip.get_y(),
1822 active_clip.right(),
1823 active_clip.bottom(),
1824 ],
1825 color: color.to_f32_array(),
1826 secondary_color: [0.0; 4],
1827 radii: [red_val; 4],
1828 stroke_or_blur: red_val,
1829 angle_rad: 0.0,
1830 kind: GpuPrimitiveKind::Circle as u32,
1831 reserved: 0,
1832 };
1833 Self::push_instance_coalesced(
1834 batches,
1835 active_batches,
1836 &mut tail_state,
1837 clip_entry,
1838 inst,
1839 &mut coalesced_merges,
1840 );
1841 uploaded_instances += 1;
1842 }
1843 DrawCommand::Line {
1844 from,
1845 to,
1846 color,
1847 width,
1848 } => {
1849 let fx0: f32 = from.get_x() * scale;
1850 let fy0: f32 = from.get_y() * scale;
1851 let fx1: f32 = to.get_x() * scale;
1852 let fy1: f32 = to.get_y() * scale;
1853 let dx: f32 = fx1 - fx0;
1854 let dy: f32 = fy1 - fy0;
1855 let stroke_px: f32 = (width * scale).max(1.0);
1856 let pad: f32 = stroke_px * 0.5 + 1.5;
1857 let min_x: f32 = fx0.min(fx1) - pad;
1858 let min_y: f32 = fy0.min(fy1) - pad;
1859 let max_x: f32 = fx0.max(fx1) + pad;
1860 let max_y: f32 = fy0.max(fy1) + pad;
1861 let quad: [f32; 4] = [
1862 min_x,
1863 min_y,
1864 (max_x - min_x).max(2.0),
1865 (max_y - min_y).max(2.0),
1866 ];
1867 if !Self::quad_intersects_clip(quad, active_clip) {
1868 culled_instances += 1;
1869 continue;
1870 }
1871 let inst: GpuQuadInstance = GpuQuadInstance {
1872 rect: quad,
1873 clip: [
1874 active_clip.get_x(),
1875 active_clip.get_y(),
1876 active_clip.right(),
1877 active_clip.bottom(),
1878 ],
1879 color: color.to_f32_array(),
1880 secondary_color: [0.0; 4],
1881 radii: [fx0, fy0, fx1, fy1],
1882 stroke_or_blur: stroke_px,
1883 angle_rad: dy.atan2(dx),
1884 kind: GpuPrimitiveKind::Line as u32,
1885 reserved: 0,
1886 };
1887 Self::push_instance_coalesced(
1888 batches,
1889 active_batches,
1890 &mut tail_state,
1891 clip_entry,
1892 inst,
1893 &mut coalesced_merges,
1894 );
1895 uploaded_instances += 1;
1896 }
1897 DrawCommand::Triangle { p0, p1, p2, color } => {
1898 let min_x: f32 = p0.get_x().min(p1.get_x()).min(p2.get_x()) * scale - 1.5;
1899 let min_y: f32 = p0.get_y().min(p1.get_y()).min(p2.get_y()) * scale - 1.5;
1900 let max_x: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x()) * scale + 1.5;
1901 let max_y: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y()) * scale + 1.5;
1902 let quad: [f32; 4] = [
1903 min_x,
1904 min_y,
1905 (max_x - min_x).max(2.0),
1906 (max_y - min_y).max(2.0),
1907 ];
1908 if !Self::quad_intersects_clip(quad, active_clip) {
1909 culled_instances += 1;
1910 continue;
1911 }
1912 let inst: GpuQuadInstance = GpuQuadInstance {
1913 rect: quad,
1914 clip: [
1915 active_clip.get_x(),
1916 active_clip.get_y(),
1917 active_clip.right(),
1918 active_clip.bottom(),
1919 ],
1920 color: color.to_f32_array(),
1921 secondary_color: [p0.get_x() * scale, p0.get_y() * scale, 0.0, 0.0],
1922 radii: [
1923 p1.get_x() * scale,
1924 p1.get_y() * scale,
1925 p2.get_x() * scale,
1926 p2.get_y() * scale,
1927 ],
1928 stroke_or_blur: 0.0,
1929 angle_rad: 0.0,
1930 kind: GpuPrimitiveKind::Triangle as u32,
1931 reserved: 3,
1932 };
1933 Self::push_instance_coalesced(
1934 batches,
1935 active_batches,
1936 &mut tail_state,
1937 clip_entry,
1938 inst,
1939 &mut coalesced_merges,
1940 );
1941 uploaded_instances += 1;
1942 }
1943 DrawCommand::SpriteRegion {
1944 rect,
1945 buffer,
1946 src_rect,
1947 tint,
1948 } => {
1949 let snapped: Rect = snap_physical_rect(*rect, scale);
1950 let quad: [f32; 4] = [
1951 snapped.get_x(),
1952 snapped.get_y(),
1953 snapped.get_width(),
1954 snapped.get_height(),
1955 ];
1956 if !Self::quad_intersects_clip(quad, active_clip) {
1957 culled_instances += 1;
1958 continue;
1959 }
1960 let uv_rect: [u16; 4] = gpu_image_atlas
1961 .insert_or_get(
1962 buffer.get_width(),
1963 buffer.get_height(),
1964 buffer.get_pixels().as_ref(),
1965 )
1966 .map_or([0, 0, u16::MAX, u16::MAX], |slot: GpuImagePlacement| {
1967 gpu_image_atlas.sub_uv_rect(
1968 slot,
1969 buffer.get_width(),
1970 buffer.get_height(),
1971 *src_rect,
1972 )
1973 });
1974 let next_img_w: u32 = gpu_image_atlas.get_width();
1975 let next_img_h: u32 = gpu_image_atlas.get_height();
1976 if (next_img_w != current_img_w || next_img_h != current_img_h)
1977 && next_img_w > 0
1978 && next_img_h > 0
1979 {
1980 Self::rescale_emitted_image_uvs(
1981 batches.as_mut_slice(),
1982 *active_batches,
1983 (current_img_w, current_img_h),
1984 (next_img_w, next_img_h),
1985 );
1986 current_img_w = next_img_w;
1987 current_img_h = next_img_h;
1988 }
1989 let sprite_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
1990 rect: quad,
1991 clip_rect: [
1992 active_clip.get_x(),
1993 active_clip.get_y(),
1994 active_clip.right(),
1995 active_clip.bottom(),
1996 ],
1997 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(*tint),
1998 aux_color_rgba: 0,
1999 uv_rect,
2000 radii_border: [0; 4],
2001 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
2002 PrimKind::ImageTexture,
2003 0,
2004 0,
2005 tail_state.layer_id,
2006 ),
2007 extra_param: 0,
2008 };
2009 Self::push_primitive_coalesced(
2010 batches,
2011 active_batches,
2012 &mut tail_state,
2013 clip_entry,
2014 sprite_prim,
2015 false,
2016 &mut coalesced_merges,
2017 );
2018 uploaded_instances += 1;
2019 }
2020 DrawCommand::ExternalSurface { rect, radius } => {
2021 let snapped: Rect = snap_physical_rect(*rect, scale);
2022 let quad: [f32; 4] = [
2023 snapped.get_x(),
2024 snapped.get_y(),
2025 snapped.get_width(),
2026 snapped.get_height(),
2027 ];
2028 if !Self::quad_intersects_clip(quad, active_clip) {
2029 culled_instances += 1;
2030 continue;
2031 }
2032 let radius_cap: f32 = GpuPrimitiveInstance::corner_radius_cap(quad, 0, 0.0);
2033 let ext_prim: GpuPrimitiveInstance = GpuPrimitiveInstance {
2034 rect: quad,
2035 clip_rect: [
2036 active_clip.get_x(),
2037 active_clip.get_y(),
2038 active_clip.right(),
2039 active_clip.bottom(),
2040 ],
2041 color_rgba: GpuPrimitiveInstance::pack_color_rgba8(COLOR_WHITE),
2042 aux_color_rgba: 0,
2043 uv_rect: [0, 0, u16::MAX, u16::MAX],
2044 radii_border: [
2045 GpuPrimitiveInstance::pack_u16_q4(
2046 (radius.get_top_left() * scale).min(radius_cap),
2047 ),
2048 GpuPrimitiveInstance::pack_u16_q4(
2049 (radius.get_top_right() * scale).min(radius_cap),
2050 ),
2051 GpuPrimitiveInstance::pack_u16_q4(
2052 (radius.get_bottom_right() * scale).min(radius_cap),
2053 ),
2054 GpuPrimitiveInstance::pack_u16_q4(
2055 (radius.get_bottom_left() * scale).min(radius_cap),
2056 ),
2057 ],
2058 kind_flags: GpuPrimitiveInstance::pack_kind_flags(
2059 PrimKind::ExternalGpuSurface,
2060 0,
2061 0,
2062 tail_state.layer_id,
2063 ),
2064 extra_param: 0,
2065 };
2066 Self::push_primitive_coalesced(
2067 batches,
2068 active_batches,
2069 &mut tail_state,
2070 clip_entry,
2071 ext_prim,
2072 false,
2073 &mut coalesced_merges,
2074 );
2075 uploaded_instances += 1;
2076 }
2077 }
2078 }
2079 let prim_upload_bytes: usize =
2080 uploaded_instances.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
2081 let upload_bytes: usize = prim_upload_bytes;
2082 let acquired_fence: u64 = match resource_ring
2083 .acquire_staging(prim_upload_bytes, GpuUploadPriority::RequiredFrame)
2084 {
2085 GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
2086 GpuRingAcquireResult::DeferredFrameRingFull
2087 | GpuRingAcquireResult::DroppedPrefetchRingFull => {
2088 let prev: u64 =
2089 GpuResourceRing::next_monotonic_fence(resource_ring.get_completed_fence_id());
2090 let _: usize = resource_ring.complete_fence(prev);
2091 match resource_ring
2092 .acquire_staging(prim_upload_bytes, GpuUploadPriority::RequiredFrame)
2093 {
2094 GpuRingAcquireResult::Acquired { fence_id, .. } => fence_id,
2095 GpuRingAcquireResult::DeferredFrameRingFull
2096 | GpuRingAcquireResult::DroppedPrefetchRingFull => {
2097 resource_ring.get_next_fence_id()
2098 }
2099 }
2100 }
2101 };
2102 if driver.get_surface_layer_handle() != 0 {
2103 let _: usize = gpu_glyph_atlas.flush_to_driver(driver);
2104 if !blur_passes.is_empty() && driver.supports_backdrop_blur_pass() {
2105 let _: usize = driver.execute_dual_kawase_blur_passes(phys_w, phys_h, blur_passes);
2106 }
2107 } else {
2108 gpu_glyph_atlas.get_mut_pending_dirty_rects().clear();
2109 gpu_glyph_atlas.set_needs_full_upload(false);
2110 }
2111 let active_slice: &[GpuBatch] = &batches[..(*active_batches).min(batches.len())];
2112 let submitted: usize = driver.submit_batches(phys_w, phys_h, clear_color, active_slice)?;
2113 *last_fence_id = acquired_fence;
2114 let hw_done: u64 = completed_gpu_fence();
2115 if driver.get_device_handle() != 0 && hw_done > 0 {
2116 let _: usize = resource_ring.complete_fence(hw_done.min(acquired_fence));
2117 } else {
2118 let retire_window: u64 = acquired_fence.saturating_sub(2);
2119 if retire_window > 0 {
2120 let _: usize = resource_ring.complete_fence(retire_window);
2121 }
2122 }
2123 *last_submitted_batches = submitted;
2124 *last_coalesced_merges = coalesced_merges;
2125 *last_culled_instances = culled_instances;
2126 *last_uploaded_instances = uploaded_instances;
2127 *last_uploaded_bytes = upload_bytes;
2128 *last_uploaded_primitive_bytes = prim_upload_bytes;
2129 let pixels: usize = if *enable_readback {
2130 let (_, px): (usize, usize) = readback.render(display_list, damage);
2131 *cpu_readback_frames = cpu_readback_frames.saturating_add(1);
2132 *last_cpu_rgba_upload_bytes = (phys_w as usize)
2133 .saturating_mul(phys_h as usize)
2134 .saturating_mul(4);
2135 px
2136 } else {
2137 *direct_gpu_frames = direct_gpu_frames.saturating_add(1);
2138 *last_cpu_rgba_upload_bytes = 0;
2139 (phys_w as usize).saturating_mul(phys_h as usize)
2140 };
2141 Ok((submitted, pixels))
2142 }
2143
2144 fn push_instance_coalesced(
2155 batches: &mut Vec<GpuBatch>,
2156 active_batches: &mut usize,
2157 tail_state: &mut GpuBatchCoalesceState,
2158 clip_entry: GpuClipEntry,
2159 instance: GpuQuadInstance,
2160 coalesced_merges: &mut usize,
2161 ) {
2162 let is_blur: bool = instance.kind == GpuPrimitiveKind::BackdropBlur as u32;
2163 let base_prim: GpuPrimitiveInstance =
2164 GpuPrimitiveInstance::from_quad_instance(&instance, 0, tail_state.layer_id);
2165 Self::push_primitive_coalesced(
2166 batches,
2167 active_batches,
2168 tail_state,
2169 clip_entry,
2170 base_prim,
2171 is_blur,
2172 coalesced_merges,
2173 );
2174 }
2175
2176 fn push_primitive_coalesced(
2188 batches: &mut Vec<GpuBatch>,
2189 active_batches: &mut usize,
2190 tail_state: &mut GpuBatchCoalesceState,
2191 clip_entry: GpuClipEntry,
2192 base_prim: GpuPrimitiveInstance,
2193 is_blur: bool,
2194 coalesced_merges: &mut usize,
2195 ) {
2196 let scissor: Rect = clip_entry.scissor;
2197 let quad_rect: [f32; 4] = base_prim.get_rect();
2198 let inst_unclipped: bool =
2199 !is_blur && Self::quad_inside_rect(quad_rect, clip_entry.safe_inner);
2200 let prim: GpuPrimitiveInstance = if inst_unclipped {
2201 base_prim
2202 } else {
2203 base_prim.with_rounded_clip_bits(clip_entry.rounded_clip_bits)
2204 };
2205 let count: usize = *active_batches;
2206 if count > 0 {
2207 let last: &mut GpuBatch = &mut batches[count - 1];
2208 if last.get_primitives().len() < MAX_QUADS_PER_BATCH
2209 && last.get_layer_id() == tail_state.layer_id
2210 && last.get_scroll_offset() == tail_state.scroll_offset
2211 {
2212 let last_scissor: Rect = last.get_scissor();
2213 let blur_boundary: bool = tail_state.has_backdrop_blur || is_blur;
2214 if last_scissor == scissor && !blur_boundary {
2215 last.get_mut_primitives().push(prim);
2216 tail_state.all_unclipped = tail_state.all_unclipped && inst_unclipped;
2217 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, quad_rect);
2218 return;
2219 }
2220 if !blur_boundary
2221 && inst_unclipped
2222 && Self::quad_inside_rect(quad_rect, last_scissor)
2223 {
2224 last.get_mut_primitives().push(prim);
2225 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, quad_rect);
2226 *coalesced_merges += 1;
2227 return;
2228 }
2229 if !blur_boundary
2230 && tail_state.all_unclipped
2231 && Self::rect_inside_rect(tail_state.bounds, scissor)
2232 {
2233 last.set_scissor(scissor);
2234 last.get_mut_primitives().push(prim);
2235 tail_state.all_unclipped = inst_unclipped;
2236 tail_state.bounds = Self::union_rect_quad(tail_state.bounds, quad_rect);
2237 *coalesced_merges += 1;
2238 return;
2239 }
2240 }
2241 }
2242 if count < batches.len() {
2243 let slot: &mut GpuBatch = &mut batches[count];
2244 slot.set_scissor(scissor);
2245 slot.set_scroll_offset(tail_state.scroll_offset);
2246 slot.set_layer_id(tail_state.layer_id);
2247 slot.get_mut_instances().clear();
2248 slot.get_mut_primitives().clear();
2249 slot.get_mut_primitives().push(prim);
2250 } else {
2251 let mut batch: GpuBatch = GpuBatch::default();
2252 batch.set_scissor(scissor);
2253 batch.set_scroll_offset(tail_state.scroll_offset);
2254 batch.set_layer_id(tail_state.layer_id);
2255 batch.get_mut_primitives().reserve(512);
2256 batch.get_mut_primitives().push(prim);
2257 batches.push(batch);
2258 }
2259 *active_batches = count + 1;
2260 tail_state.all_unclipped = inst_unclipped;
2261 tail_state.has_backdrop_blur = is_blur;
2262 tail_state.bounds = Rect::new(quad_rect[0], quad_rect[1], quad_rect[2], quad_rect[3]);
2263 }
2264
2265 fn rescale_emitted_glyph_uvs(
2275 batches: &mut [GpuBatch],
2276 active_batches: usize,
2277 line_glyphs: &mut [(Option<GpuGlyphPlacement>, f32)],
2278 old_height: u32,
2279 new_height: u32,
2280 ) {
2281 if old_height == 0 || new_height == 0 || old_height == new_height {
2282 return;
2283 }
2284 let v_scale: f32 = (old_height as f32) / (new_height as f32);
2285 for batch in batches.iter_mut().take(active_batches) {
2286 for prim in batch.get_mut_primitives().iter_mut() {
2287 if prim.prim_kind() == PrimKind::GlyphAlpha8 {
2288 let mut uv: [u16; 4] = prim.get_uv_rect();
2289 uv[1] = (f32::from(uv[1]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2290 uv[3] = (f32::from(uv[3]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2291 prim.set_uv_rect(uv);
2292 }
2293 }
2294 }
2295 for (placement_opt, _) in line_glyphs.iter_mut() {
2296 if let Some(placement) = placement_opt.as_mut() {
2297 let mut uv: [u16; 4] = placement.get_uv_rect();
2298 uv[1] = (f32::from(uv[1]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2299 uv[3] = (f32::from(uv[3]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2300 placement.set_uv_rect(uv);
2301 }
2302 }
2303 }
2304
2305 fn rescale_emitted_image_uvs(
2314 batches: &mut [GpuBatch],
2315 active_batches: usize,
2316 old_dims: (u32, u32),
2317 new_dims: (u32, u32),
2318 ) {
2319 let (old_w, old_h): (u32, u32) = old_dims;
2320 let (new_w, new_h): (u32, u32) = new_dims;
2321 if old_w == 0
2322 || old_h == 0
2323 || new_w == 0
2324 || new_h == 0
2325 || (old_w == new_w && old_h == new_h)
2326 {
2327 return;
2328 }
2329 let u_scale: f32 = (old_w as f32) / (new_w as f32);
2330 let v_scale: f32 = (old_h as f32) / (new_h as f32);
2331 for batch in batches.iter_mut().take(active_batches) {
2332 for prim in batch.get_mut_primitives().iter_mut() {
2333 if prim.prim_kind() == PrimKind::ImageTexture {
2334 let mut uv: [u16; 4] = prim.get_uv_rect();
2335 uv[0] = (f32::from(uv[0]) * u_scale).round().clamp(0.0, 65535.0) as u16;
2336 uv[1] = (f32::from(uv[1]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2337 uv[2] = (f32::from(uv[2]) * u_scale).round().clamp(0.0, 65535.0) as u16;
2338 uv[3] = (f32::from(uv[3]) * v_scale).round().clamp(0.0, 65535.0) as u16;
2339 prim.set_uv_rect(uv);
2340 }
2341 }
2342 }
2343 }
2344}