1use crate::display::PixelFormat;
10use crate::element::{Element, IntoElement};
11use crate::pod::FrameScope;
12use crate::renderers::software::rasterize_text;
13use crate::sdf::SurfacePainter;
14use crate::solver::Region;
15use crate::style::Color;
16use crate::svg::math::{floor_f32, sqrt_f32};
17
18#[inline(always)]
19fn canon_f32(v: f32) -> u32 {
20 if v == 0.0 {
21 0
22 } else {
23 v.to_bits()
24 }
25}
26
27#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
29pub struct StaticSceneId(pub u32);
30
31#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
33pub struct CustomShaderId(pub u32);
34
35#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
37pub enum CustomShaderLayer {
38 #[default]
40 Background,
41 Foreground,
43}
44
45#[repr(C)]
47#[derive(Copy, Clone, Debug)]
48pub struct Viewport2D {
49 pub offset_x: f32,
50 pub offset_y: f32,
51 pub zoom: f32,
52}
53
54impl Default for Viewport2D {
55 #[inline(always)]
56 fn default() -> Self {
57 Self {
58 offset_x: 0.0,
59 offset_y: 0.0,
60 zoom: 1.0,
61 }
62 }
63}
64
65impl PartialEq for Viewport2D {
66 #[inline(always)]
67 fn eq(&self, other: &Self) -> bool {
68 canon_f32(self.offset_x) == canon_f32(other.offset_x)
69 && canon_f32(self.offset_y) == canon_f32(other.offset_y)
70 && canon_f32(self.zoom) == canon_f32(other.zoom)
71 }
72}
73
74impl Eq for Viewport2D {}
75
76impl Viewport2D {
77 #[inline(always)]
78 pub const fn new(offset_x: f32, offset_y: f32, zoom: f32) -> Self {
79 Self {
80 offset_x,
81 offset_y,
82 zoom,
83 }
84 }
85
86 #[inline(always)]
88 pub fn world_to_local(&self, wx: f32, wy: f32) -> (f32, f32) {
89 (wx * self.zoom + self.offset_x, wy * self.zoom + self.offset_y)
90 }
91
92 #[inline(always)]
94 pub fn local_to_world(&self, lx: f32, ly: f32) -> (f32, f32) {
95 let inv = if self.zoom.abs() > 1e-6 {
96 1.0 / self.zoom
97 } else {
98 1.0
99 };
100 ((lx - self.offset_x) * inv, (ly - self.offset_y) * inv)
101 }
102
103 #[inline(always)]
105 pub fn projected_px(&self, world_size: f32) -> f32 {
106 world_size * self.zoom
107 }
108
109 #[inline]
111 pub fn visible_world_bounds(&self, canvas_w: f32, canvas_h: f32) -> [f32; 4] {
112 let (x0, y0) = self.local_to_world(0.0, 0.0);
113 let (x1, y1) = self.local_to_world(canvas_w, canvas_h);
114 [x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)]
115 }
116
117 #[inline]
119 pub fn world_aabb_to_screen_region(&self, aabb: [f32; 4], canvas_bounds: Region) -> Region {
120 let (lx0, ly0) = self.world_to_local(aabb[0], aabb[1]);
121 let (lx1, ly1) = self.world_to_local(aabb[2], aabb[3]);
122 let min_x = floor_f32(lx0.min(lx1)) as i32 - 2;
123 let min_y = floor_f32(ly0.min(ly1)) as i32 - 2;
124 let max_x = floor_f32(lx0.max(lx1)) as i32 + 3;
125 let max_y = floor_f32(ly0.max(ly1)) as i32 + 3;
126 let sx = (canvas_bounds.x as i32 + min_x).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
127 let sy = (canvas_bounds.y as i32 + min_y).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
128 let sw = (max_x - min_x).clamp(0, i16::MAX as i32) as i16;
129 let sh = (max_y - min_y).clamp(0, i16::MAX as i32) as i16;
130 Region::new(sx, sy, sw, sh).intersect(canvas_bounds)
131 }
132
133 #[inline]
135 pub fn camera_hash(&self) -> u32 {
136 let mut h: u32 = 0x811c_9dc5;
137 for bits in [
138 canon_f32(self.offset_x),
139 canon_f32(self.offset_y),
140 canon_f32(self.zoom),
141 ] {
142 for b in bits.to_le_bytes() {
143 h ^= b as u32;
144 h = h.wrapping_mul(0x0100_0193);
145 }
146 }
147 h
148 }
149}
150
151#[repr(u32)]
153#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
154pub enum CanvasPrimitiveKind {
155 #[default]
157 RoundedRect = 0,
158 Circle = 1,
160 Capsule = 2,
162}
163
164#[repr(C)]
169#[derive(Copy, Clone, Debug)]
170pub struct CanvasStaticGeom {
171 pub cx: f32,
172 pub cy: f32,
173 pub w: f32,
174 pub h: f32,
175 pub cos: f32,
176 pub sin: f32,
177 pub corner_r: f32,
178 pub kind_flags: u32,
179}
180
181const _: () = assert!(core::mem::size_of::<CanvasStaticGeom>() == 32);
182
183impl Default for CanvasStaticGeom {
184 #[inline(always)]
185 fn default() -> Self {
186 Self {
187 cx: 0.0,
188 cy: 0.0,
189 w: 16.0,
190 h: 16.0,
191 cos: 1.0,
192 sin: 0.0,
193 corner_r: 3.0,
194 kind_flags: 0,
195 }
196 }
197}
198
199impl CanvasStaticGeom {
200 #[inline(always)]
202 pub const fn rounded_rect_rotated(
203 cx: f32,
204 cy: f32,
205 w: f32,
206 h: f32,
207 cos: f32,
208 sin: f32,
209 corner_r: f32,
210 ) -> Self {
211 Self {
212 cx,
213 cy,
214 w,
215 h,
216 cos,
217 sin,
218 corner_r,
219 kind_flags: CanvasPrimitiveKind::RoundedRect as u32,
220 }
221 }
222
223 #[inline(always)]
225 pub const fn circle(cx: f32, cy: f32, diameter: f32) -> Self {
226 Self {
227 cx,
228 cy,
229 w: diameter,
230 h: diameter,
231 cos: 1.0,
232 sin: 0.0,
233 corner_r: diameter * 0.5,
234 kind_flags: CanvasPrimitiveKind::Circle as u32,
235 }
236 }
237
238 #[inline]
240 pub fn capsule(x0: f32, y0: f32, x1: f32, y1: f32, thickness: f32) -> Self {
241 let dx = x1 - x0;
242 let dy = y1 - y0;
243 let len = sqrt_f32(dx * dx + dy * dy);
244 let (cos, sin) = if len > 1e-5 {
245 (dx / len, dy / len)
246 } else {
247 (1.0, 0.0)
248 };
249 Self {
250 cx: (x0 + x1) * 0.5,
251 cy: (y0 + y1) * 0.5,
252 w: len,
253 h: thickness.max(1.0),
254 cos,
255 sin,
256 corner_r: thickness * 0.5,
257 kind_flags: CanvasPrimitiveKind::Capsule as u32,
258 }
259 }
260
261 #[inline]
263 pub fn world_aabb(&self) -> [f32; 4] {
264 let hw = if self.kind_flags == CanvasPrimitiveKind::Capsule as u32 {
265 (self.w + self.h) * 0.5
266 } else {
267 self.w * 0.5
268 };
269 let hh = self.h * 0.5;
270 let ac = self.cos.abs();
271 let as_ = self.sin.abs();
272 let ex = hw * ac + hh * as_;
273 let ey = hw * as_ + hh * ac;
274 [self.cx - ex, self.cy - ey, self.cx + ex, self.cy + ey]
275 }
276}
277
278pub const CANVAS_STATE_HOVERED: u8 = 1 << 0;
280pub const CANVAS_STATE_SELECTED: u8 = 1 << 1;
281pub const CANVAS_STATE_DISABLED: u8 = 1 << 2;
282pub const CANVAS_STATE_LOCKED: u8 = 1 << 3;
283pub const CANVAS_STATE_FOCUSED: u8 = 1 << 4;
284
285#[repr(C)]
287#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
288pub struct CanvasItemState {
289 pub palette_idx: u8,
291 pub flags: u8,
293 pub badge_mask: u8,
295 pub lod_group: u8,
297}
298
299const _: () = assert!(core::mem::size_of::<CanvasItemState>() == 4);
300
301impl CanvasItemState {
302 #[inline(always)]
303 pub const fn new(palette_idx: u8) -> Self {
304 Self {
305 palette_idx,
306 flags: 0,
307 badge_mask: 0,
308 lod_group: 0,
309 }
310 }
311
312 #[inline(always)]
313 pub const fn with_flags(mut self, flags: u8) -> Self {
314 self.flags = flags;
315 self
316 }
317
318 #[inline(always)]
319 pub const fn with_badge(mut self, badge_mask: u8) -> Self {
320 self.badge_mask = badge_mask;
321 self
322 }
323
324 #[inline(always)]
325 pub const fn is_selected(self) -> bool {
326 (self.flags & CANVAS_STATE_SELECTED) != 0
327 }
328
329 #[inline(always)]
330 pub const fn is_hovered(self) -> bool {
331 (self.flags & CANVAS_STATE_HOVERED) != 0
332 }
333
334 #[inline(always)]
335 pub const fn is_disabled(self) -> bool {
336 (self.flags & CANVAS_STATE_DISABLED) != 0
337 }
338
339 #[inline(always)]
340 pub const fn is_locked(self) -> bool {
341 (self.flags & CANVAS_STATE_LOCKED) != 0
342 }
343
344 #[inline(always)]
345 pub const fn is_focused(self) -> bool {
346 (self.flags & CANVAS_STATE_FOCUSED) != 0
347 }
348
349 #[inline(always)]
350 pub const fn pack_u32(self) -> u32 {
351 (self.palette_idx as u32)
352 | ((self.flags as u32) << 8)
353 | ((self.badge_mask as u32) << 16)
354 | ((self.lod_group as u32) << 24)
355 }
356}
357
358#[repr(C)]
360#[derive(Copy, Clone, Debug, Default)]
361pub struct CanvasMeshVertex {
362 pub x: f32,
363 pub y: f32,
364 pub color: u32,
366}
367
368const _: () = assert!(core::mem::size_of::<CanvasMeshVertex>() == 12);
369
370#[derive(Copy, Clone, Debug)]
372pub struct CanvasAsciiLabel<'a> {
373 pub wx: f32,
374 pub wy: f32,
375 pub font_px: f32,
376 pub color: Color,
377 pub text: &'a str,
378}
379
380#[derive(Copy, Clone)]
382pub struct CustomShaderCall<'a> {
383 pub id: CustomShaderId,
384 pub world_bounds: [f32; 4],
385 pub state_hash: u64,
386 pub uniform_bytes: &'a [u8],
387}
388
389impl<'a> PartialEq for CustomShaderCall<'a> {
390 #[inline]
391 fn eq(&self, other: &Self) -> bool {
392 self.id == other.id
393 && self.state_hash == other.state_hash
394 && canon_f32(self.world_bounds[0]) == canon_f32(other.world_bounds[0])
395 && canon_f32(self.world_bounds[1]) == canon_f32(other.world_bounds[1])
396 && canon_f32(self.world_bounds[2]) == canon_f32(other.world_bounds[2])
397 && canon_f32(self.world_bounds[3]) == canon_f32(other.world_bounds[3])
398 && self.uniform_bytes == other.uniform_bytes
399 }
400}
401
402impl<'a> Eq for CustomShaderCall<'a> {}
403
404#[derive(Copy, Clone)]
406pub struct SceneCanvasSpec<'a> {
407 pub id: u32,
408 pub width: i16,
409 pub height: i16,
410 pub opacity: u8,
411 pub bg: Color,
412 pub viewport: Viewport2D,
413 pub zooming: bool,
415 pub scene_id: StaticSceneId,
416 pub geom_revision: u32,
417 pub state_revision: u32,
418 pub mesh_revision: u32,
419 pub palette: &'a [Color],
421 pub geoms: &'a [CanvasStaticGeom],
423 pub states: &'a [CanvasItemState],
425 pub visible_ranges: &'a [(u32, u32)],
427 pub mesh_verts: &'a [CanvasMeshVertex],
429 pub mesh_indices: &'a [u32],
430 pub labels: &'a [CanvasAsciiLabel<'a>],
432 pub marquee: Option<[f32; 4]>,
434 pub marquee_color: Color,
435 pub dirty_screen_rects: &'a [Region],
437 pub bg_shaders: &'a [CustomShaderCall<'a>],
439 pub fg_shaders: &'a [CustomShaderCall<'a>],
441 pub software_bg_fallback: Option<&'a dyn Fn(&mut dyn SurfacePainter, Viewport2D)>,
443 pub software_fg_fallback: Option<&'a dyn Fn(&mut dyn SurfacePainter, Viewport2D)>,
445}
446
447impl<'a> SceneCanvasSpec<'a> {
448 pub fn content_hash(&self) -> u32 {
450 let mut h: u32 = 0x811c_9dc5;
451 let mut mix = |v: u32| {
452 for b in v.to_le_bytes() {
453 h ^= b as u32;
454 h = h.wrapping_mul(0x0100_0193);
455 }
456 };
457 mix(self.id);
458 mix(self.scene_id.0);
459 mix(self.geom_revision);
460 mix(self.state_revision);
461 mix(self.mesh_revision);
462 mix(self.viewport.camera_hash());
463 mix(self.bg.0);
464 mix(self.opacity as u32 | ((self.zooming as u32) << 8));
465 mix(self.labels.len() as u32);
466 for c in self.palette.iter().take(16) {
467 mix(c.0);
468 }
469 if let Some(m) = self.marquee {
470 for v in m {
471 mix(canon_f32(v));
472 }
473 }
474 for s in self.bg_shaders {
475 mix(s.id.0);
476 mix(s.state_hash as u32);
477 mix((s.state_hash >> 32) as u32);
478 }
479 for s in self.fg_shaders {
480 mix(s.id.0);
481 mix(s.state_hash as u32);
482 mix((s.state_hash >> 32) as u32);
483 }
484 h
485 }
486}
487
488impl<'a> PartialEq for SceneCanvasSpec<'a> {
489 fn eq(&self, other: &Self) -> bool {
490 self.id == other.id
491 && self.width == other.width
492 && self.height == other.height
493 && self.opacity == other.opacity
494 && self.bg == other.bg
495 && self.viewport == other.viewport
496 && self.zooming == other.zooming
497 && self.scene_id == other.scene_id
498 && self.geom_revision == other.geom_revision
499 && self.state_revision == other.state_revision
500 && self.mesh_revision == other.mesh_revision
501 && self.palette == other.palette
502 && self.labels.len() == other.labels.len()
503 && self.marquee.map(|m| m.map(canon_f32)) == other.marquee.map(|m| m.map(canon_f32))
504 && self.bg_shaders == other.bg_shaders
505 && self.fg_shaders == other.fg_shaders
506 }
507}
508
509impl<'a> Eq for SceneCanvasSpec<'a> {}
510
511pub struct SceneCanvas<'a> {
513 spec: SceneCanvasSpec<'a>,
514}
515
516impl<'a> SceneCanvas<'a> {
517 #[inline(always)]
518 pub const fn from_spec(spec: SceneCanvasSpec<'a>) -> Self {
519 Self { spec }
520 }
521}
522
523impl<'a> IntoElement<'a> for SceneCanvas<'a> {
524 #[inline]
525 fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
526 let r = cx.arena().alloc(self.spec);
527 Element::SceneCanvas(r)
528 }
529}
530
531#[inline(always)]
533pub const fn scene_canvas<'a>(spec: SceneCanvasSpec<'a>) -> SceneCanvas<'a> {
534 SceneCanvas::from_spec(spec)
535}
536
537struct BufferPainter<'a, P: PixelFormat> {
542 bounds: Region,
543 clip: Region,
544 origin_x: i16,
545 origin_y: i16,
546 stride: usize,
547 target: &'a mut [P],
548}
549
550impl<'a, P: PixelFormat> SurfacePainter for BufferPainter<'a, P> {
551 #[inline(always)]
552 fn bounds(&self) -> Region {
553 self.bounds
554 }
555
556 #[inline(always)]
557 fn clip(&self) -> Region {
558 self.clip
559 }
560
561 #[inline(always)]
562 fn draw_text(&mut self, bounds: Region, text: &str, color: Color, font_size: u8) {
563 rasterize_text(
564 bounds,
565 self.clip,
566 text,
567 color,
568 font_size,
569 0x10,
570 self.origin_x,
571 self.origin_y,
572 self.stride,
573 self.target,
574 );
575 }
576
577 #[inline(always)]
578 fn blend_pixel(&mut self, x: i16, y: i16, color: Color, alpha: u8) {
579 if alpha == 0 || !self.clip.contains(x, y) {
580 return;
581 }
582 let rx = (x - self.origin_x) as usize;
583 let ry = (y - self.origin_y) as usize;
584 let idx = ry * self.stride + rx;
585 if idx < self.target.len() {
586 let eff_a = ((color.a() as u16 * alpha as u16) / 255) as u8;
587 if eff_a == 0 {
588 return;
589 }
590 if eff_a == 255 {
591 self.target[idx] = P::from_color(color);
592 } else {
593 let dst = self.target[idx].to_color();
594 let inv = 255u16 - eff_a as u16;
595 let a16 = eff_a as u16;
596 let r = ((color.r() as u16 * a16 + dst.r() as u16 * inv) / 255) as u8;
597 let g = ((color.g() as u16 * a16 + dst.g() as u16 * inv) / 255) as u8;
598 let b = ((color.b() as u16 * a16 + dst.b() as u16 * inv) / 255) as u8;
599 self.target[idx] = P::from_color(Color::rgb(r, g, b));
600 }
601 }
602 }
603}
604
605#[inline(always)]
606fn blend_px<P: PixelFormat>(target: &mut [P], idx: usize, color: Color, alpha: u8) {
607 if alpha == 0 || idx >= target.len() {
608 return;
609 }
610 if alpha == 255 {
611 target[idx] = P::from_color(color);
612 return;
613 }
614 let dst = target[idx].to_color();
615 let inv = 255u16 - alpha as u16;
616 let a16 = alpha as u16;
617 let r = ((color.r() as u16 * a16 + dst.r() as u16 * inv) / 255) as u8;
618 let g = ((color.g() as u16 * a16 + dst.g() as u16 * inv) / 255) as u8;
619 let b = ((color.b() as u16 * a16 + dst.b() as u16 * inv) / 255) as u8;
620 target[idx] = P::from_color(Color::rgb(r, g, b));
621}
622
623#[inline]
624fn resolve_item_colors(state: CanvasItemState, palette: &[Color]) -> (Color, Color, bool) {
625 let base = palette
626 .get(state.palette_idx as usize)
627 .copied()
628 .unwrap_or(Color::rgb(59, 130, 246));
629 if state.is_disabled() {
630 return (base.scale_alpha(90), Color::TRANSPARENT, false);
631 }
632 let mut fill = base;
633 let mut border = Color::TRANSPARENT;
634 if state.is_selected() {
635 fill = base.lerp(Color::WHITE, 46);
636 border = Color::WHITE;
637 } else if state.is_hovered() {
638 fill = base.lerp(Color::WHITE, 28);
639 border = base.lerp(Color::WHITE, 140);
640 }
641 if state.is_focused() {
642 border = Color::rgb(96, 165, 250);
643 }
644 (fill, border, state.badge_mask != 0 || state.is_locked())
645}
646
647#[inline]
648fn rasterize_triangle<P: PixelFormat>(
649 x0: f32,
650 y0: f32,
651 x1: f32,
652 y1: f32,
653 x2: f32,
654 y2: f32,
655 color: Color,
656 eff_alpha: u8,
657 clip: Region,
658 origin_x: i16,
659 origin_y: i16,
660 stride: usize,
661 target: &mut [P],
662) {
663 if eff_alpha == 0 {
664 return;
665 }
666 let min_x = (floor_f32(x0.min(x1).min(x2)) as i32).max(clip.x as i32);
667 let max_x = (floor_f32(x0.max(x1).max(x2)) as i32 + 1).min(clip.right() as i32);
668 let min_y = (floor_f32(y0.min(y1).min(y2)) as i32).max(clip.y as i32);
669 let max_y = (floor_f32(y0.max(y1).max(y2)) as i32 + 1).min(clip.bottom() as i32);
670 if min_x >= max_x || min_y >= max_y {
671 return;
672 }
673 let area = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
674 if area.abs() < 1e-4 {
675 return;
676 }
677 let sign = if area > 0.0 { 1.0 } else { -1.0 };
678 for py in min_y..max_y {
679 let fy = py as f32 + 0.5;
680 let row_off = ((py as i16 - origin_y) as usize) * stride;
681 for px in min_x..max_x {
682 let fx = px as f32 + 0.5;
683 let e0 = ((x1 - x0) * (fy - y0) - (y1 - y0) * (fx - x0)) * sign;
684 let e1 = ((x2 - x1) * (fy - y1) - (y2 - y1) * (fx - x1)) * sign;
685 let e2 = ((x0 - x2) * (fy - y2) - (y0 - y2) * (fx - x2)) * sign;
686 if e0 >= 0.0 && e1 >= 0.0 && e2 >= 0.0 {
687 let idx = row_off + ((px as i16 - origin_x) as usize);
688 blend_px(target, idx, color, eff_alpha);
689 }
690 }
691 }
692}
693
694#[allow(clippy::too_many_arguments)]
696pub fn rasterize_scene_canvas<P: PixelFormat>(
697 bounds: Region,
698 clip: Region,
699 spec: &SceneCanvasSpec<'_>,
700 scale: f32,
701 origin_x: i16,
702 origin_y: i16,
703 stride: usize,
704 target: &mut [P],
705) {
706 let eff = bounds.intersect(clip);
707 if eff.is_empty() || spec.opacity == 0 {
708 return;
709 }
710 let s = if scale > 0.0 { scale } else { 1.0 };
711 let vp_zoom = spec.viewport.zoom * s;
712 let vp_ox = bounds.x as f32 + spec.viewport.offset_x * s;
713 let vp_oy = bounds.y as f32 + spec.viewport.offset_y * s;
714
715 if spec.bg.a() > 0 {
717 let a = ((spec.bg.a() as u16 * spec.opacity as u16) / 255) as u8;
718 for py in eff.y..eff.bottom() {
719 let row_off = ((py - origin_y) as usize) * stride;
720 for px in eff.x..eff.right() {
721 blend_px(target, row_off + ((px - origin_x) as usize), spec.bg, a);
722 }
723 }
724 }
725
726 if let Some(bg_cb) = spec.software_bg_fallback {
728 let mut painter = BufferPainter {
729 bounds,
730 clip: eff,
731 origin_x,
732 origin_y,
733 stride,
734 target,
735 };
736 bg_cb(&mut painter, spec.viewport);
737 }
738
739 let mut t_idx = 0usize;
741 while t_idx + 2 < spec.mesh_indices.len() {
742 let i0 = spec.mesh_indices[t_idx] as usize;
743 let i1 = spec.mesh_indices[t_idx + 1] as usize;
744 let i2 = spec.mesh_indices[t_idx + 2] as usize;
745 t_idx += 3;
746 if i0 >= spec.mesh_verts.len()
747 || i1 >= spec.mesh_verts.len()
748 || i2 >= spec.mesh_verts.len()
749 {
750 continue;
751 }
752 let v0 = spec.mesh_verts[i0];
753 let v1 = spec.mesh_verts[i1];
754 let v2 = spec.mesh_verts[i2];
755 let col = Color(v0.color);
756 let a = ((col.a() as u16 * spec.opacity as u16) / 255) as u8;
757 rasterize_triangle(
758 v0.x * vp_zoom + vp_ox,
759 v0.y * vp_zoom + vp_oy,
760 v1.x * vp_zoom + vp_ox,
761 v1.y * vp_zoom + vp_oy,
762 v2.x * vp_zoom + vp_ox,
763 v2.y * vp_zoom + vp_oy,
764 col,
765 a,
766 eff,
767 origin_x,
768 origin_y,
769 stride,
770 target,
771 );
772 }
773
774 let full_range = [(0u32, spec.geoms.len() as u32)];
776 let ranges: &[(u32, u32)] = if spec.visible_ranges.is_empty() {
777 &full_range
778 } else {
779 spec.visible_ranges
780 };
781
782 for &(r_start, r_end) in ranges {
783 let start = (r_start as usize).min(spec.geoms.len());
784 let end = (r_end as usize).min(spec.geoms.len());
785 for i in start..end {
786 let g = &spec.geoms[i];
787 let st = spec
788 .states
789 .get(i)
790 .copied()
791 .unwrap_or_default();
792 let (fill_col, border_col, has_badge) = resolve_item_colors(st, spec.palette);
793 let scx = g.cx * vp_zoom + vp_ox;
794 let scy = g.cy * vp_zoom + vp_oy;
795 let sw = g.w * vp_zoom;
796 let sh = g.h * vp_zoom;
797 let is_capsule = g.kind_flags == CanvasPrimitiveKind::Capsule as u32;
798 let is_circle = g.kind_flags == CanvasPrimitiveKind::Circle as u32;
799 let hw = if is_capsule { (sw + sh) * 0.5 } else { sw * 0.5 };
800 let hh = sh * 0.5;
801 let ext_x = hw * g.cos.abs() + hh * g.sin.abs() + 2.0;
802 let ext_y = hw * g.sin.abs() + hh * g.cos.abs() + 2.0;
803
804 let min_x = (floor_f32(scx - ext_x) as i32).max(eff.x as i32);
805 let max_x = (floor_f32(scx + ext_x) as i32 + 1).min(eff.right() as i32);
806 let min_y = (floor_f32(scy - ext_y) as i32).max(eff.y as i32);
807 let max_y = (floor_f32(scy + ext_y) as i32 + 1).min(eff.bottom() as i32);
808 if min_x >= max_x || min_y >= max_y {
809 continue;
810 }
811
812 let corner_r = (g.corner_r * vp_zoom).min(hw.min(hh)).max(0.0);
813 let border_px = if border_col.a() > 0 {
814 (1.5 * s).clamp(1.0, 3.0)
815 } else {
816 0.0
817 };
818 let fill_a = ((fill_col.a() as u16 * spec.opacity as u16) / 255) as u8;
819 let border_a = ((border_col.a() as u16 * spec.opacity as u16) / 255) as u8;
820
821 for py in min_y..max_y {
822 let dy = py as f32 + 0.5 - scy;
823 let row_off = ((py as i16 - origin_y) as usize) * stride;
824 for px in min_x..max_x {
825 let dx = px as f32 + 0.5 - scx;
826 let lx = dx * g.cos + dy * g.sin;
828 let ly = -dx * g.sin + dy * g.cos;
829
830 let dist = if is_circle {
831 sqrt_f32(lx * lx + ly * ly) - hw.min(hh)
832 } else if is_capsule {
833 let half_seg = (sw * 0.5).max(0.0);
834 let qx = (lx.abs() - half_seg).max(0.0);
835 sqrt_f32(qx * qx + ly * ly) - hh
836 } else {
837 let qx = lx.abs() - (hw - corner_r);
838 let qy = ly.abs() - (hh - corner_r);
839 let ox = qx.max(0.0);
840 let oy = qy.max(0.0);
841 sqrt_f32(ox * ox + oy * oy) + qx.max(qy).min(0.0) - corner_r
842 };
843
844 if dist <= 0.75 {
845 let cov = (0.75 - dist).clamp(0.0, 1.0);
846 let idx = row_off + ((px as i16 - origin_x) as usize);
847 if border_px > 0.0 && dist > -border_px {
848 let a = ((border_a as f32) * cov) as u8;
849 blend_px(target, idx, border_col, a);
850 } else {
851 let a = ((fill_a as f32) * cov) as u8;
852 blend_px(target, idx, fill_col, a);
853 }
854 if has_badge && hw >= 6.0 && hh >= 6.0 {
856 let bx = lx - (hw - 3.5);
857 let by = ly + (hh - 3.5);
858 if bx * bx + by * by <= 4.5 {
859 blend_px(target, idx, Color::rgb(250, 204, 21), spec.opacity);
860 }
861 }
862 }
863 }
864 }
865 }
866 }
867
868 for lbl in spec.labels {
870 if lbl.text.is_empty() {
871 continue;
872 }
873 let sx = (lbl.wx * vp_zoom + vp_ox) as i32;
874 let sy = (lbl.wy * vp_zoom + vp_oy) as i32;
875 let fpx = (lbl.font_px * s).clamp(7.0, 32.0) as u8;
876 let est_w = ((lbl.text.len() as i32) * (fpx as i32) * 3 / 4).max(16);
877 let est_h = (fpx as i32) + 4;
878 let tb = Region::new(
879 (sx - est_w / 2).clamp(i16::MIN as i32, i16::MAX as i32) as i16,
880 (sy - est_h / 2).clamp(i16::MIN as i32, i16::MAX as i32) as i16,
881 est_w.min(i16::MAX as i32) as i16,
882 est_h.min(i16::MAX as i32) as i16,
883 );
884 if !tb.intersect(eff).is_empty() {
885 rasterize_text(
887 tb,
888 eff,
889 lbl.text,
890 lbl.color,
891 fpx,
892 1 | 0x10,
893 origin_x,
894 origin_y,
895 stride,
896 target,
897 );
898 }
899 }
900
901 if let Some([mx0, my0, mx1, my1]) = spec.marquee {
903 let rx0 = (bounds.x as f32 + mx0.min(mx1) * s) as i32;
904 let ry0 = (bounds.y as f32 + my0.min(my1) * s) as i32;
905 let rx1 = (bounds.x as f32 + mx0.max(mx1) * s) as i32;
906 let ry1 = (bounds.y as f32 + my0.max(my1) * s) as i32;
907 let mr = Region::new(
908 rx0.clamp(i16::MIN as i32, i16::MAX as i32) as i16,
909 ry0.clamp(i16::MIN as i32, i16::MAX as i32) as i16,
910 (rx1 - rx0).clamp(0, i16::MAX as i32) as i16,
911 (ry1 - ry0).clamp(0, i16::MAX as i32) as i16,
912 )
913 .intersect(eff);
914 if !mr.is_empty() {
915 let fill_a = ((spec.marquee_color.a() as u16 * 56) / 255) as u8;
916 let edge_a = ((spec.marquee_color.a() as u16 * 210) / 255) as u8;
917 for py in mr.y..mr.bottom() {
918 let row_off = ((py - origin_y) as usize) * stride;
919 let is_y_edge = py == mr.y || py == mr.bottom() - 1;
920 for px in mr.x..mr.right() {
921 let is_edge = is_y_edge || px == mr.x || px == mr.right() - 1;
922 blend_px(
923 target,
924 row_off + ((px - origin_x) as usize),
925 spec.marquee_color,
926 if is_edge { edge_a } else { fill_a },
927 );
928 }
929 }
930 }
931 }
932
933 if let Some(fg_cb) = spec.software_fg_fallback {
935 let mut painter = BufferPainter {
936 bounds,
937 clip: eff,
938 origin_x,
939 origin_y,
940 stride,
941 target,
942 };
943 fg_cb(&mut painter, spec.viewport);
944 }
945}