1use crate::kernel::font::FONT_DATA;
2use core::cell::Cell;
3
4static ICON_CURSOR_64: &[u8] = include_bytes!("../../icons/ic_fluent_cursor_64_regular.svg");
5
6#[derive(Copy, Clone)]
7pub struct Color(pub u32);
8
9impl Color {
10 pub fn to_hardware_format(self) -> u32 {
11 let val = self.0;
12 let a = val & 0xFF000000;
13 let r = (val >> 16) & 0xFF;
14 let g = (val >> 8) & 0xFF;
15 let b = val & 0xFF;
16 a | (b << 16) | (g << 8) | r
17 }
18
19 pub fn from_hardware_format(val: u32) -> Color {
20 let a = val & 0xFF000000;
21 let r = (val >> 16) & 0xFF;
22 let g = (val >> 8) & 0xFF;
23 let b = val & 0xFF;
24 Color(a | (b << 16) | (g << 8) | r)
25 }
26}
27
28impl Color {
29 pub const BLACK: Color = Color(0xFF000000);
30 pub const WHITE: Color = Color(0xFFFFFFFF);
31 pub const LIGHT_GRAY: Color = Color(0xFFC6C6C6);
32 pub const DARK_GRAY: Color = Color(0xFF848484);
33 pub const BLUE: Color = Color(0xFF000084);
34 pub const DARK_RED: Color = Color(0xFF840000);
35 pub const INACTIVE_TITLE: Color = Color(0xFF5A5A5A);
36}
37
38#[inline(always)]
41pub fn div_255_fast(x: u32) -> u32 {
42 (x + 1 + (x >> 8)) >> 8
43}
44
45
46const DMA_THRESHOLD_PIXELS: u32 = 2048; pub static FRAME_NO: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);
53pub static HERO_TEXT_FRAME: core::sync::atomic::AtomicU32 =
55 core::sync::atomic::AtomicU32::new(u32::MAX);
56
57
58pub struct Screen {
59 pub vram: *mut u32,
60 pub back_buffer: *mut u32,
61 pub use_back_buffer: core::cell::Cell<bool>,
62 pub width: u32,
63 pub height: u32,
64 pub pitch: u32, pub clip: Cell<Option<(u32, u32, u32, u32)>>, pub round_clip: Cell<Option<(i32, i32, i32, i32, i32)>>,
69 clip_stack: Cell<[Option<(u32, u32, u32, u32)>; 8]>,
70 clip_depth: Cell<usize>,
71
72 render_target: Cell<*mut u32>,
74 target_width: Cell<u32>,
75 target_height: Cell<u32>,
76 saved_target_clip: Cell<Option<Option<(u32, u32, u32, u32)>>>,
77
78 vsync: Cell<bool>, page: Cell<u32>, fb_virtual_height: Cell<u32>, fb_size: Cell<u32>, prev_content_region: Cell<Option<(u32, u32, u32, u32)>>,
85 page_cursor: Cell<[(u32, u32); 2]>,
88 vsync_init_count: Cell<u8>,
90}
91
92pub static TEXT_DRAW_TICKS: core::sync::atomic::AtomicUsize =
94 core::sync::atomic::AtomicUsize::new(0);
95
96pub static BOXFILL_TICKS: core::sync::atomic::AtomicUsize =
98 core::sync::atomic::AtomicUsize::new(0);
99pub static SHADOW_TICKS: core::sync::atomic::AtomicUsize =
101 core::sync::atomic::AtomicUsize::new(0);
102
103pub static RRECT_TICKS: core::sync::atomic::AtomicUsize =
105 core::sync::atomic::AtomicUsize::new(0);
106
107pub static PIXBG_TICKS: core::sync::atomic::AtomicUsize =
110 core::sync::atomic::AtomicUsize::new(0);
111
112pub struct PixBgTimer(usize);
114impl PixBgTimer {
115 #[allow(clippy::new_without_default)]
116 pub fn new() -> Self {
117 Self(crate::kernel::timer::get_ticks())
118 }
119}
120impl Drop for PixBgTimer {
121 fn drop(&mut self) {
122 PIXBG_TICKS.fetch_add(
123 crate::kernel::timer::get_ticks().wrapping_sub(self.0),
124 core::sync::atomic::Ordering::Relaxed,
125 );
126 }
127}
128
129pub struct RRectTimer(usize);
131impl RRectTimer {
132 #[allow(clippy::new_without_default)]
133 pub fn new() -> Self {
134 Self(crate::kernel::timer::get_ticks())
135 }
136}
137impl Drop for RRectTimer {
138 fn drop(&mut self) {
139 RRECT_TICKS.fetch_add(
140 crate::kernel::timer::get_ticks().wrapping_sub(self.0),
141 core::sync::atomic::Ordering::Relaxed,
142 );
143 }
144}
145
146struct AccTimer(usize, &'static core::sync::atomic::AtomicUsize);
147impl Drop for AccTimer {
148 fn drop(&mut self) {
149 self.1.fetch_add(
150 crate::kernel::timer::get_ticks().wrapping_sub(self.0),
151 core::sync::atomic::Ordering::Relaxed,
152 );
153 }
154}
155
156struct TextDrawTimer(usize);
158impl Drop for TextDrawTimer {
159 fn drop(&mut self) {
160 TEXT_DRAW_TICKS.fetch_add(
161 crate::kernel::timer::get_ticks().wrapping_sub(self.0),
162 core::sync::atomic::Ordering::Relaxed,
163 );
164 }
165}
166
167impl Screen {
168 pub fn new(vram: *mut u32, width: u32, height: u32, pitch: u32) -> Self {
169 Self {
170 vram,
171 back_buffer: core::ptr::null_mut(),
172 use_back_buffer: core::cell::Cell::new(false),
173 width,
174 height,
175 pitch,
176 clip: Cell::new(None),
177 round_clip: Cell::new(None),
178 clip_stack: Cell::new([None; 8]),
179 clip_depth: Cell::new(0),
180 render_target: Cell::new(core::ptr::null_mut()),
181 target_width: Cell::new(width),
182 target_height: Cell::new(height),
183 saved_target_clip: Cell::new(None),
184 vsync: Cell::new(false),
185 page: Cell::new(0),
186 fb_virtual_height: Cell::new(0),
187 fb_size: Cell::new(0),
188 prev_content_region: Cell::new(None),
189 page_cursor: Cell::new([(0, 0), (0, 0)]),
190 vsync_init_count: Cell::new(0),
191 }
192 }
193
194 pub fn vsync_enabled(&self) -> bool {
195 self.vsync.get()
196 }
197
198 pub fn has_pending_vsync_sync(&self) -> bool {
199 self.prev_content_region.get().is_some()
200 }
201
202 pub fn debug_geometry(&self) {
205 let addr = self.vram as usize;
206 let region = if addr >= crate::kernel::mmu::DEVICE_REGION_START {
207 "uncached/Device OK"
208 } else {
209 "CACHED RAM (<0x38000000) !!"
210 };
211 crate::info!(
212 "[FB] vram=0x{:08X} ({}) | {}x{} | pitch={} (width*4={}) | virt_h={} | size={}",
213 addr,
214 region,
215 self.width,
216 self.height,
217 self.pitch,
218 self.width * 4,
219 self.fb_virtual_height.get(),
220 self.fb_size.get()
221 );
222 }
223
224 pub fn set_fb_geometry(&self, virtual_height: u32, size: u32) {
227 self.fb_virtual_height.set(virtual_height);
228 self.fb_size.set(size);
229 }
230
231 pub fn enable_vsync(&self) -> bool {
236 let virt_h = self.fb_virtual_height.get();
237 let size = self.fb_size.get();
238 let two_pages = virt_h >= self.height * 2
239 && (size == 0 || size as u64 >= (self.pitch as u64) * (self.height as u64) * 2);
240 if !two_pages {
241 self.vsync.set(false);
242 return false;
243 }
244 if let Err(e) = crate::kernel::mailbox::set_virtual_offset(0, 0) {
246 crate::warn!("[GPU] VSync 初期オフセット設定失敗: {}", e);
247 self.vsync.set(false);
248 return false;
249 }
250 self.page.set(0);
251 self.vsync.set(true);
252 self.vsync_init_count.set(2);
254 self.prev_content_region.set(None);
255 self.page_cursor.set([(0, 0), (0, 0)]);
256 true
257 }
258
259 pub fn set_render_target(&self, buf: *mut u32, w: u32, h: u32) {
260 let old_clip = self.clip.get();
262 self.saved_target_clip.set(Some(old_clip));
263 self.clip.set(None);
264 self.render_target.set(buf);
265 self.target_width.set(w);
266 self.target_height.set(h);
267 }
268
269 pub fn reset_render_target(&self) {
270 self.render_target.set(core::ptr::null_mut());
271 self.target_width.set(self.width);
272 self.target_height.set(self.height);
273 if let Some(saved) = self.saved_target_clip.take() {
275 self.clip.set(saved);
276 }
277 }
278
279 pub fn resolve_target(&self) -> (*mut u32, u32, u32) {
280 let custom_target = self.render_target.get();
281 if !custom_target.is_null() {
282 (
283 custom_target,
284 self.target_width.get(),
285 self.target_height.get(),
286 )
287 } else {
288 let base_ptr = if self.use_back_buffer.get() && !self.back_buffer.is_null() {
289 self.back_buffer
290 } else {
291 self.vram
292 };
293 (base_ptr, self.width, self.height)
294 }
295 }
296
297 pub fn resolve_stride(&self) -> u32 {
299 let custom_target = self.render_target.get();
300 if !custom_target.is_null() {
301 self.target_width.get()
302 } else if self.use_back_buffer.get() && !self.back_buffer.is_null() {
303 self.width
304 } else {
305 self.pitch / 4
306 }
307 }
308
309 pub fn enable_double_buffering(&mut self) {
310 if self.back_buffer.is_null() {
311 let Ok(layout) = core::alloc::Layout::array::<u32>((self.width * self.height) as usize)
313 else {
314 return; };
316 unsafe {
317 self.back_buffer = alloc::alloc::alloc_zeroed(layout) as *mut u32;
318 }
319 if self.back_buffer.is_null() {
320 return; }
322 }
323 self.use_back_buffer.set(true);
324 }
325
326 pub fn flush(&self) {
327 if self.use_back_buffer.get() && !self.back_buffer.is_null() {
328 let stride = self.pitch / 4;
329 let w = self.width;
330 let h = self.height;
331
332 if self.vsync.get() {
333 let page = self.page.get();
336 let page_off_px = page * h * stride; let dst = self.vram as usize + (page_off_px * 4) as usize;
338 if stride == w {
339 crate::kernel::dma::dma_memcpy(5, self.back_buffer as usize, dst, w * h * 4);
340 } else {
341 crate::kernel::dma::dma_memcpy_2d(
342 5,
343 self.back_buffer as usize,
344 dst,
345 w * 4,
346 h,
347 w * 4,
348 stride * 4,
349 );
350 }
351 if let Err(e) = crate::kernel::mailbox::set_virtual_offset(0, page * h) {
353 crate::warn!("[GPU] ページフリップ失敗 (page={}): {}", page, e);
354 }
355 if let Err(e) = crate::kernel::mailbox::wait_vsync() {
357 crate::warn!("[GPU] VSync 待機失敗: {}", e);
358 }
359 self.page.set(1 - page);
361 } else if stride == w {
362 crate::kernel::dma::dma_memcpy(
363 5,
364 self.back_buffer as usize,
365 self.vram as usize,
366 w * h * 4,
367 );
368 } else {
369 crate::kernel::dma::dma_memcpy_2d(
370 5,
371 self.back_buffer as usize,
372 self.vram as usize,
373 w * 4,
374 h,
375 w * 4,
376 stride * 4,
377 );
378 }
379 }
380 }
381
382 pub fn flush_rect(&self, x0: u32, y0: u32, x1: u32, y1: u32) {
383 if self.vsync.get() {
386 self.flush();
387 return;
388 }
389 if self.use_back_buffer.get() && !self.back_buffer.is_null() {
390 let stride = self.pitch / 4;
391 let w = self.width;
392 let h = self.height;
393
394 let cx0 = x0.min(w - 1);
395 let cx1 = x1.min(w - 1).max(cx0);
396 let cy0 = y0.min(h - 1);
397 let cy1 = y1.min(h - 1).max(cy0);
398
399 let rect_w = cx1 - cx0 + 1;
400 let rect_h = cy1 - cy0 + 1;
401
402 if rect_w * rect_h >= DMA_THRESHOLD_PIXELS {
403 let src_offset = (cy0 * w + cx0) * 4;
404 let dst_offset = (cy0 * stride + cx0) * 4;
405 crate::kernel::dma::dma_memcpy_2d(
406 5,
407 self.back_buffer as usize + src_offset as usize,
408 self.vram as usize + dst_offset as usize,
409 rect_w * 4,
410 rect_h,
411 w * 4,
412 stride * 4,
413 );
414 } else {
415 for y in cy0..=cy1 {
416 unsafe {
417 let src = self.back_buffer.add((y * w + cx0) as usize);
418 let dst = self.vram.add((y * stride + cx0) as usize);
419 core::ptr::copy_nonoverlapping(src, dst, rect_w as usize);
420 }
421 }
422 }
423 }
424 }
425
426 pub fn scroll_rect(&self, x: u32, y: u32, w: u32, h: u32, dy: i32) {
434 if dy == 0 || w == 0 || h == 0 {
435 return;
436 }
437 let abs_dy = dy.unsigned_abs();
438 if abs_dy >= h {
439 return;
440 }
441 let (target_ptr, buf_w, buf_h) = self.resolve_target();
442 if x >= buf_w || y >= buf_h {
443 return;
444 }
445 let actual_w = w.min(buf_w - x);
446 let actual_h = h.min(buf_h - y);
447 if abs_dy >= actual_h {
448 return;
449 }
450
451 let stride = self.resolve_stride() as usize;
452 let move_h = (actual_h - abs_dy) as usize;
453 let copy_w = actual_w as usize;
454
455 unsafe {
456 if dy > 0 {
457 let src_y = (y + abs_dy) as usize;
459 let dst_y = y as usize;
460 for row in 0..move_h {
461 let src = target_ptr.add((src_y + row) * stride + x as usize);
462 let dst = target_ptr.add((dst_y + row) * stride + x as usize);
463 core::ptr::copy(src, dst, copy_w);
464 }
465 } else {
466 let src_y = y as usize;
468 let dst_y = (y + abs_dy) as usize;
469 for row in (0..move_h).rev() {
470 let src = target_ptr.add((src_y + row) * stride + x as usize);
471 let dst = target_ptr.add((dst_y + row) * stride + x as usize);
472 core::ptr::copy(src, dst, copy_w);
473 }
474 }
475 }
476 }
477
478 pub fn draw_mouse_direct(&self, mouse: &Mouse) {
480 let prev = self.use_back_buffer.get();
481 self.use_back_buffer.set(false);
482 mouse.draw(self);
483 self.use_back_buffer.set(prev);
484 }
485
486 pub fn flush_with_cursor(&self, mouse: &Mouse) {
489 if !self.use_back_buffer.get() || self.back_buffer.is_null() {
490 self.flush();
491 return;
492 }
493 let cursor_size = Mouse::SIZE as usize;
494 let w = self.width as usize;
495 let h = self.height as usize;
496 let cx = mouse.x as usize;
497 let cy = mouse.y as usize;
498
499 let rows = cursor_size.min(h.saturating_sub(cy));
501 let cols = cursor_size.min(w.saturating_sub(cx));
502 let mut saved = alloc::vec![0u32; cursor_size * cursor_size];
503 unsafe {
504 for row in 0..rows {
505 let src = self.back_buffer.add((cy + row) * w + cx);
506 core::ptr::copy_nonoverlapping(
507 src,
508 saved.as_mut_ptr().add(row * cursor_size),
509 cols,
510 );
511 }
512 }
513
514 mouse.draw(self);
516 self.flush();
517
518 unsafe {
520 for row in 0..rows {
521 let dst = self.back_buffer.add((cy + row) * w + cx);
522 core::ptr::copy_nonoverlapping(saved.as_ptr().add(row * cursor_size), dst, cols);
523 }
524 }
525 }
526
527 pub fn flush_dirty_pageflip(&self, content: Option<(u32, u32, u32, u32)>, mouse: &Mouse) {
532 if !self.vsync.get() || !self.use_back_buffer.get() || self.back_buffer.is_null() {
533 self.flush_with_cursor(mouse);
535 return;
536 }
537 let w = self.width;
538 let h = self.height;
539 let stride = self.pitch / 4;
540 let cs = Mouse::SIZE;
541
542 #[inline]
543 fn union(a: (u32, u32, u32, u32), b: (u32, u32, u32, u32)) -> (u32, u32, u32, u32) {
544 (a.0.min(b.0), a.1.min(b.1), a.2.max(b.2), a.3.max(b.3))
545 }
546
547 #[inline]
548 fn cursor_rect(x: u32, y: u32, cs: u32, w: u32, h: u32) -> (u32, u32, u32, u32) {
549 (x.min(w), y.min(h), (x + cs).min(w), (y + cs).min(h))
550 }
551
552 let page = self.page.get(); let c_new = cursor_rect(mouse.x, mouse.y, cs, w, h);
555 let old_c = self.page_cursor.get()[page as usize];
557 let c_old = cursor_rect(old_c.0, old_c.1, cs, w, h);
558
559 let content_now = content.map(|c| (c.0.min(w), c.1.min(h), c.2.min(w), c.3.min(h)));
561
562 let region = if self.vsync_init_count.get() > 0 {
572 self.vsync_init_count.set(self.vsync_init_count.get() - 1);
573 (0, 0, w, h)
574 } else {
575 let content_region = match (content_now, self.prev_content_region.get()) {
577 (Some(a), Some(b)) => Some(union(a, b)),
578 (Some(a), None) => Some(a),
579 (None, Some(b)) => Some(b),
580 (None, None) => None,
581 };
582 let mut r = union(c_new, c_old);
583 if let Some(cr) = content_region {
584 r = union(r, cr);
585 }
586 r
587 };
588
589 let x0 = region.0.min(w);
590 let y0 = region.1.min(h);
591 let x1 = region.2.min(w).max(x0);
592 let y1 = region.3.min(h).max(y0);
593
594 let cx = mouse.x as usize;
596 let cy = mouse.y as usize;
597 let wsz = w as usize;
598 let hsz = h as usize;
599 let rows = (cs as usize).min(hsz.saturating_sub(cy));
600 let cols = (cs as usize).min(wsz.saturating_sub(cx));
601 let mut saved = alloc::vec![0u32; (cs * cs) as usize];
602 unsafe {
603 for row in 0..rows {
604 let src = self.back_buffer.add((cy + row) * wsz + cx);
605 core::ptr::copy_nonoverlapping(
606 src,
607 saved.as_mut_ptr().add(row * cs as usize),
608 cols,
609 );
610 }
611 }
612 mouse.draw(self); if x1 > x0 && y1 > y0 {
616 let rect_w = x1 - x0;
617 let rect_h = y1 - y0;
618 let src_off = y0 * w + x0;
619 let dst_off = page * h * stride + y0 * stride + x0;
620 crate::kernel::dma::dma_memcpy_2d(
621 5,
622 self.back_buffer as usize + (src_off * 4) as usize,
623 self.vram as usize + (dst_off * 4) as usize,
624 rect_w * 4,
625 rect_h,
626 w * 4,
627 stride * 4,
628 );
629 if let Err(e) = crate::kernel::mailbox::set_virtual_offset(0, page * h) {
630 crate::warn!("[GPU] ページフリップ失敗 (cursor flush, page={}): {}", page, e);
631 }
632 if let Err(e) = crate::kernel::mailbox::wait_vsync() {
633 crate::warn!("[GPU] VSync 待機失敗 (cursor flush): {}", e);
634 }
635 self.page.set(1 - page);
636 }
637
638 unsafe {
640 for row in 0..rows {
641 let dst = self.back_buffer.add((cy + row) * wsz + cx);
642 core::ptr::copy_nonoverlapping(saved.as_ptr().add(row * cs as usize), dst, cols);
643 }
644 }
645
646 let mut pc = self.page_cursor.get();
648 pc[page as usize] = (mouse.x, mouse.y);
649 self.page_cursor.set(pc);
650 self.prev_content_region.set(content_now);
651 }
652
653 pub fn set_clip(&self, x0: u32, y0: u32, x1: u32, y1: u32) {
654 self.clip.set(Some((x0, y0, x1, y1)));
655 }
656
657 pub fn clip_rect(&self) -> Option<(u32, u32, u32, u32)> {
659 self.clip.get()
660 }
661
662 pub fn clear_clip(&self) {
663 self.clip.set(None);
664 }
665
666 pub fn push_clip(&self, x0: u32, y0: u32, x1: u32, y1: u32) {
669 let depth = self.clip_depth.get();
670 if depth < 8 {
671 let mut stack = self.clip_stack.get();
672 stack[depth] = self.clip.get();
673 self.clip_stack.set(stack);
674 self.clip_depth.set(depth + 1);
675 }
676 let new_clip = if let Some((cx0, cy0, cx1, cy1)) = self.clip.get() {
677 (cx0.max(x0), cy0.max(y0), cx1.min(x1), cy1.min(y1))
678 } else {
679 (x0, y0, x1, y1)
680 };
681 self.clip.set(Some(new_clip));
682 }
683
684 pub fn pop_clip(&self) {
686 let depth = self.clip_depth.get();
687 if depth > 0 {
688 let stack = self.clip_stack.get();
689 self.clip.set(stack[depth - 1]);
690 self.clip_depth.set(depth - 1);
691 } else {
692 self.clip.set(None);
693 }
694 }
695
696 pub fn clear(&self, color: Color) {
697 if let Some((cx0, cy0, cx1, cy1)) = self.clip.get() {
700 self.boxfill(cx0, cy0, cx1, cy1, color);
701 return;
702 }
703
704 let (target_ptr, _w, h) = self.resolve_target();
705
706 let stride = self.resolve_stride();
710 let color_val = color.to_hardware_format();
711 crate::kernel::dma::dma_fill_rect(
712 5, &color_val as *const _ as usize,
714 target_ptr as usize,
715 stride,
716 h,
717 stride * 4,
718 );
719 }
720
721 #[inline]
727 fn outside_round_clip(&self, x: u32, y: u32) -> bool {
728 match self.round_clip.get() {
729 None => false,
730 Some((x0, y0, x1, y1, r)) => {
731 let (x, y) = (x as i32, y as i32);
732 let inset = crate::kernel::draw_clip::round_clip_inset(y, y0, y1, r);
733 inset > 0 && (x < x0 + inset || x >= x1 - inset)
734 }
735 }
736 }
737
738 pub fn draw_pixel(&self, x: u32, y: u32, color: Color) {
739 if let Some((cx0, cy0, cx1, cy1)) = self.clip.get() {
740 if x < cx0 || x >= cx1 || y < cy0 || y >= cy1 {
741 return;
742 }
743 }
744 if self.outside_round_clip(x, y) {
745 return;
746 }
747
748 let a = (color.0 >> 24) & 0xFF;
757 let color = if a == 0 || a == 0xFF {
758 color
759 } else {
760 Color(crate::os_lib::web_engine::alpha_blend::blend_argb_over(
761 color.0,
762 self.read_pixel(x, y).0 | 0xFF00_0000,
763 ))
764 };
765
766 let (target_ptr, w, h) = self.resolve_target();
767 if x < w && y < h {
770 let stride = self.resolve_stride();
771 unsafe {
772 let ptr = target_ptr.add((y * stride + x) as usize);
773 core::ptr::write_volatile(ptr, color.to_hardware_format());
774 }
775 }
776 }
777
778 pub fn read_pixel(&self, x: u32, y: u32) -> Color {
779 let (target_ptr, w, h) = self.resolve_target();
780 if x < w && y < h {
781 let stride = self.resolve_stride();
782 unsafe {
783 let ptr = target_ptr.add((y * stride + x) as usize);
784 Color::from_hardware_format(core::ptr::read_volatile(ptr))
785 }
786 } else {
787 Color::BLACK
788 }
789 }
790
791 pub fn draw_pixel_alpha(&self, x: u32, y: u32, color: Color, alpha: u8) {
799 if alpha == 0 {
800 return;
801 }
802 if let Some((cx0, cy0, cx1, cy1)) = self.clip.get() {
803 if x < cx0 || x >= cx1 || y < cy0 || y >= cy1 {
804 return;
805 }
806 }
807 if self.outside_round_clip(x, y) {
808 return;
809 }
810
811 let (target_ptr, w, h) = self.resolve_target();
812 if x >= w || y >= h {
813 return;
814 }
815 let stride = self.resolve_stride();
816 unsafe {
817 let ptr = target_ptr.add((y * stride + x) as usize);
818 if alpha == 255 {
819 core::ptr::write_volatile(ptr, color.to_hardware_format());
820 return;
821 }
822
823 let bg_val = core::ptr::read_volatile(ptr);
824 let a = alpha as u32;
826 let inv_a = 255 - a;
827
828 let fg_r = (color.0 >> 16) & 0xFF;
829 let fg_g = (color.0 >> 8) & 0xFF;
830 let fg_b = color.0 & 0xFF;
831
832 let fg_rb_term = ((fg_b * a + 127) << 16) | (fg_r * a + 127);
833 let bg_rb = bg_val & 0x00FF_00FF;
834 let rb_prod = fg_rb_term + bg_rb * inv_a;
835 let r = div_255_fast(rb_prod & 0xFFFF);
836 let b = div_255_fast(rb_prod >> 16);
837 let g = div_255_fast(fg_g * a + 127 + ((bg_val >> 8) & 0xFF) * inv_a);
838
839 let blended = 0xFF00_0000 | (b << 16) | (g << 8) | r;
840 core::ptr::write_volatile(ptr, blended);
841 }
842 }
843
844 pub fn draw_string_vector(&self, x: u32, y: u32, s: &str, color: Color, size_px: u32) {
846 let mut font_lock = crate::kernel::vector_font::GLOBAL_VECTOR_FONT.lock();
847 if let Some(ref mut font) = *font_lock {
848 let (_, w, h) = self.resolve_target();
849 let mut cur_x = x as i32;
850 for c in s.chars() {
851 if let Some(glyph) = font.get_glyph(c, size_px) {
852 let gx = cur_x + glyph.x_offset;
853 let gy = y as i32 + glyph.y_offset + (size_px as i32 * 85 / 100);
855
856 for row in 0..glyph.height {
857 let py = gy + row as i32;
858 if py < 0 || py >= h as i32 {
859 continue;
860 }
861 for col in 0..glyph.width {
862 let px = gx + col as i32;
863 if px < 0 || px >= w as i32 {
864 continue;
865 }
866
867 let alpha = glyph.data[(row * glyph.width + col) as usize];
868 if alpha > 0 {
869 self.draw_pixel_alpha(px as u32, py as u32, color, alpha);
870 }
871 }
872 }
873
874 cur_x += glyph.advance as i32;
875 }
876 }
877 }
878 }
879
880 pub fn draw_string_vector_bg(
882 &self,
883 x: u32,
884 y: u32,
885 s: &str,
886 color: Color,
887 size_px: u32,
888 bg: Color,
889 ) {
890 let mut font_lock = crate::kernel::vector_font::GLOBAL_VECTOR_FONT.lock();
891 if let Some(ref mut font) = *font_lock {
892 let (_, w, h) = self.resolve_target();
893 let mut cur_x = x as i32;
894 for c in s.chars() {
895 if let Some(glyph) = font.get_color_glyph(c, size_px, color.0, bg.0) {
896 let gx = cur_x + glyph.x_offset;
897 let gy = y as i32 + glyph.y_offset + (size_px as i32 * 85 / 100);
898
899 for row in 0..glyph.height {
900 let py = gy + row as i32;
901 if py < 0 || py >= h as i32 {
902 continue;
903 }
904 for col in 0..glyph.width {
905 let px = gx + col as i32;
906 if px < 0 || px >= w as i32 {
907 continue;
908 }
909 let pixel_color = glyph.data[(row * glyph.width + col) as usize];
910 if (pixel_color >> 24) > 0 {
911 self.draw_pixel(px as u32, py as u32, Color(pixel_color));
913 }
914 }
915 }
916 cur_x += glyph.advance as i32;
917 } else {
918 cur_x += (size_px / 2) as i32;
919 }
920 }
921 }
922 }
923
924 pub fn draw_vscrollbar_styled(
939 &self,
940 track_x: u32,
941 track_y: i32,
942 track_h: i32,
943 content_h: i32,
944 viewport_h: i32,
945 scroll_pos: i32,
946 thumb_width: u32,
947 thumb_color: Color,
948 track_color: Option<Color>,
949 ) {
950 if content_h <= viewport_h || track_h <= 0 || viewport_h <= 0 || thumb_width == 0 {
951 return;
952 }
953 if let Some(track_col) = track_color {
954 self.boxfill(
955 track_x,
956 track_y.max(0) as u32,
957 track_x + thumb_width,
958 (track_y + track_h).max(0) as u32,
959 track_col,
960 );
961 }
962 let max_scroll = (content_h - viewport_h).max(1);
963 let thumb_h = ((track_h * viewport_h) / content_h).clamp(24.min(track_h), track_h);
964 let ratio = scroll_pos.clamp(0, max_scroll) as f32 / max_scroll as f32;
965 let thumb_y = track_y + (ratio * (track_h - thumb_h) as f32) as i32;
966 self.boxfill(
967 track_x,
968 thumb_y.max(0) as u32,
969 track_x + thumb_width,
970 (thumb_y + thumb_h).max(0) as u32,
971 thumb_color,
972 );
973 }
974
975 pub fn draw_vscrollbar(
976 &self,
977 track_x: u32,
978 track_y: i32,
979 track_h: i32,
980 content_h: i32,
981 viewport_h: i32,
982 scroll_pos: i32,
983 ) {
984 let thumb_color = Color(crate::kernel::config::get_config().theme.scrollbar_thumb);
985 self.draw_vscrollbar_styled(
986 track_x,
987 track_y,
988 track_h,
989 content_h,
990 viewport_h,
991 scroll_pos,
992 6,
993 thumb_color,
994 None,
995 );
996 }
997
998 pub fn boxfill(&self, x0: u32, y0: u32, x1: u32, y1: u32, color: Color) {
1007 let a = (color.0 >> 24) & 0xFF;
1012 if a == 0 {
1026 return;
1027 }
1028 if let Some((rx0, ry0, rx1, ry1, r)) = self.round_clip.get() {
1030 let (bx0, by0, bx1, by1) = (x0 as i32, y0 as i32, x1 as i32, y1 as i32);
1031 let touches_corner = by0 < ry0 + r || by1 >= ry1 - r;
1032 if touches_corner && bx1 >= bx0 && by1 >= by0 {
1033 self.round_clip.set(None);
1034 let mut run_start: Option<i32> = None;
1035 for y in by0..=by1 {
1036 let inset = crate::kernel::draw_clip::round_clip_inset(y, ry0, ry1, r);
1037 if inset == 0 {
1038 run_start.get_or_insert(y);
1039 continue;
1040 }
1041 if let Some(s0) = run_start.take() {
1042 self.boxfill(x0, s0 as u32, x1, (y - 1) as u32, color);
1043 }
1044 let lx = bx0.max(rx0 + inset);
1045 let hx = bx1.min(rx1 - inset - 1);
1046 if hx >= lx && y >= 0 {
1047 self.boxfill(lx.max(0) as u32, y as u32, hx.max(0) as u32, y as u32, color);
1048 }
1049 }
1050 if let Some(s0) = run_start {
1051 self.boxfill(x0, s0 as u32, x1, y1, color);
1052 }
1053 self.round_clip.set(Some((rx0, ry0, rx1, ry1, r)));
1054 return;
1055 }
1056 }
1057 if a != 0xFF {
1058 self.boxfill_alpha(x0, y0, x1, y1, color, a as u8);
1059 return;
1060 }
1061
1062 let x0 = if x0 >= 0x8000_0000 {
1064 if x1 >= 0x8000_0000 { return; }
1065 0
1066 } else {
1067 x0
1068 };
1069 let y0 = if y0 >= 0x8000_0000 {
1070 if y1 >= 0x8000_0000 { return; }
1071 0
1072 } else {
1073 y0
1074 };
1075
1076 let (target_ptr, w, h) = self.resolve_target();
1077 if (color.0 & 0x00FF_FFFF) == 0x00FF_6B6B {
1082 static BG_DIAG: core::sync::atomic::AtomicU32 =
1083 core::sync::atomic::AtomicU32::new(0);
1084 if BG_DIAG.fetch_add(1, core::sync::atomic::Ordering::Relaxed) < 3 {
1085 crate::warn!(
1086 "[TARGET][BG] hero fill target=0x{:x} w={} h={} rect=({},{})-({},{})",
1087 target_ptr as usize, w, h, x0, y0, x1, y1
1088 );
1089 }
1090 }
1091 let (cx0, cy0, cx1, cy1) =
1098 match crate::kernel::draw_clip::clip_fill_rect(x0, y0, x1, y1, w, h, self.clip.get()) {
1099 Some(r) => r,
1100 None => return,
1101 };
1102
1103 let rect_w = cx1 - cx0 + 1;
1104 let rect_h = cy1 - cy0 + 1;
1105 let stride = self.resolve_stride();
1106
1107 if rect_w * rect_h >= DMA_THRESHOLD_PIXELS {
1108 let dst_offset = (cy0 * stride + cx0) * 4;
1109 let color_val = color.to_hardware_format();
1110 crate::kernel::dma::dma_fill_rect(
1111 5,
1112 &color_val as *const _ as usize,
1113 target_ptr as usize + dst_offset as usize,
1114 rect_w,
1115 rect_h,
1116 stride * 4,
1117 );
1118 } else {
1119 let c = color.to_hardware_format();
1120 for y in cy0..=cy1 {
1121 unsafe {
1122 let row_ptr = target_ptr.add((y * stride + cx0) as usize);
1123 let count = (cx1 - cx0 + 1) as usize;
1124 let slice = core::slice::from_raw_parts_mut(row_ptr, count);
1125 slice.fill(c);
1126 }
1127 }
1128 }
1129 }
1130
1131 pub fn boxfill_alpha(&self, x0: u32, y0: u32, x1: u32, y1: u32, color: Color, alpha: u8) {
1135 if alpha == 0 {
1136 return;
1137 }
1138
1139 let x0 = if x0 >= 0x8000_0000 {
1141 if x1 >= 0x8000_0000 { return; }
1142 0
1143 } else {
1144 x0
1145 };
1146 let y0 = if y0 >= 0x8000_0000 {
1147 if y1 >= 0x8000_0000 { return; }
1148 0
1149 } else {
1150 y0
1151 };
1152
1153 if alpha == 255 {
1154 self.boxfill(x0, y0, x1, y1, color);
1155 return;
1156 }
1157 if self.round_clip.get().is_some() {
1159 let (target_w, target_h) = {
1160 let (_, w, h) = self.resolve_target();
1161 (w, h)
1162 };
1163 if let Some((cx0, cy0, cx1, cy1)) = crate::kernel::draw_clip::clip_fill_rect(
1164 x0,
1165 y0,
1166 x1,
1167 y1,
1168 target_w,
1169 target_h,
1170 self.clip.get(),
1171 ) {
1172 for y in cy0..=cy1 {
1173 for x in cx0..=cx1 {
1174 self.draw_pixel_alpha(x, y, color, alpha);
1175 }
1176 }
1177 }
1178 return;
1179 }
1180
1181 let (target_ptr, w, h) = self.resolve_target();
1182 let (cx0, cy0, cx1, cy1) =
1183 match crate::kernel::draw_clip::clip_fill_rect(x0, y0, x1, y1, w, h, self.clip.get()) {
1184 Some(r) => r,
1185 None => return,
1186 };
1187
1188 let rect_w = cx1 - cx0 + 1;
1189 let stride = self.resolve_stride();
1190
1191 let a = alpha as u32;
1192 let inv_a = 255 - a;
1193 let fg_r = (color.0 >> 16) & 0xFF;
1194 let fg_g = (color.0 >> 8) & 0xFF;
1195 let fg_b = color.0 & 0xFF;
1196 let fg_r_term = fg_r * a + 127;
1197 let fg_g_term = fg_g * a + 127;
1198 let fg_b_term = fg_b * a + 127;
1199 let fg_rb_term = (fg_b_term << 16) | fg_r_term;
1200
1201 let rect_w_usize = rect_w as usize;
1202 let stride_usize = stride as usize;
1203
1204 for y in cy0..=cy1 {
1205 unsafe {
1206 let mut ptr = target_ptr.add(y as usize * stride_usize + cx0 as usize);
1207 for _ in 0..rect_w_usize {
1208 let bg_val = core::ptr::read_volatile(ptr);
1209 let bg_rb = bg_val & 0x00FF_00FF;
1212 let rb_prod = fg_rb_term + bg_rb * inv_a;
1213 let r = div_255_fast(rb_prod & 0xFFFF);
1214 let b = div_255_fast(rb_prod >> 16);
1215 let g = div_255_fast(fg_g_term + ((bg_val >> 8) & 0xFF) * inv_a);
1216
1217 core::ptr::write_volatile(ptr, 0xFF00_0000 | (b << 16) | (g << 8) | r);
1218 ptr = ptr.add(1);
1219 }
1220 }
1221 }
1222 }
1223
1224 pub fn draw_scanline_colors(&self, x0: i32, y: i32, colors: &[u32]) {
1228 if colors.is_empty() {
1229 return;
1230 }
1231
1232 if self.round_clip.get().is_some() {
1234 for (i, &col) in colors.iter().enumerate() {
1235 let px = x0 + i as i32;
1236 if px >= 0 {
1237 let a = ((col >> 24) & 0xFF) as u8;
1238 self.draw_pixel_alpha(px as u32, y as u32, Color(col), a);
1239 }
1240 }
1241 return;
1242 }
1243
1244 let (target_ptr, w, h) = self.resolve_target();
1245 if y < 0 || y >= h as i32 {
1246 return;
1247 }
1248 let (clip_x0, clip_y0, clip_x1, clip_y1) = match self.clip.get() {
1249 Some((cx0, cy0, cx1, cy1)) => (cx0.min(w), cy0.min(h), cx1.min(w), cy1.min(h)),
1250 None => (0, 0, w, h),
1251 };
1252 let uy = y as u32;
1253 if uy < clip_y0 || uy >= clip_y1 {
1254 return;
1255 }
1256
1257 let len = colors.len() as i32;
1258 let line_x0 = x0;
1259 let line_x1 = x0 + len;
1260 let vis_x0 = line_x0.max(clip_x0 as i32);
1261 let vis_x1 = line_x1.min(clip_x1 as i32);
1262 if vis_x0 >= vis_x1 {
1263 return;
1264 }
1265
1266 let src_start = (vis_x0 - line_x0) as usize;
1267 let count = (vis_x1 - vis_x0) as usize;
1268 let stride = self.resolve_stride() as usize;
1269
1270 unsafe {
1271 let mut ptr = target_ptr.add(y as usize * stride + vis_x0 as usize);
1272 for i in 0..count {
1273 let c = colors[src_start + i];
1274 let a = (c >> 24) & 0xFF ;
1275 if a == 0 {
1276 ptr = ptr.add(1);
1277 continue;
1278 }
1279 let r = (c >> 16) & 0xFF;
1280 let g = (c >> 8) & 0xFF;
1281 let b = c & 0xFF;
1282
1283 if a == 255 {
1284 let hw = 0xFF00_0000 | (b << 16) | (g << 8) | r;
1285 core::ptr::write_volatile(ptr, hw);
1286 } else {
1287 let inv_a = 255 - a;
1288 let bg_val = core::ptr::read_volatile(ptr);
1289 let bg_rb = bg_val & 0x00FF_00FF;
1290 let fg_rb_term = ((b * a + 127) << 16) | (r * a + 127);
1291 let rb_prod = fg_rb_term + bg_rb * inv_a;
1292 let out_r = div_255_fast(rb_prod & 0xFFFF);
1293 let out_b = div_255_fast(rb_prod >> 16);
1294 let out_g = div_255_fast((g * a + 127) + ((bg_val >> 8) & 0xFF) * inv_a);
1295 core::ptr::write_volatile(ptr, 0xFF00_0000 | (out_b << 16) | (out_g << 8) | out_r);
1296 }
1297 ptr = ptr.add(1);
1298 }
1299 }
1300 }
1301
1302 pub fn draw_char(&self, x: u32, y: u32, c: char, color: Color) {
1303 let code = c as usize;
1304 if code >= 256 {
1305 return;
1306 }
1307 let font = &FONT_DATA[code];
1308 let (target_ptr, w, h) = self.resolve_target();
1309 let stride = self.resolve_stride();
1310 let clip = self.clip.get();
1311 let hw_color = color.to_hardware_format();
1312
1313 for i in 0..16u32 {
1314 let py = y + i;
1315 if py >= h {
1316 continue;
1317 }
1318 if let Some((_, cy0, _, cy1)) = clip {
1319 if py < cy0 || py >= cy1 {
1320 continue;
1321 }
1322 }
1323 let d = font[i as usize];
1324 if d == 0 {
1325 continue;
1326 }
1327 unsafe {
1328 let row_ptr = target_ptr.add((py * stride) as usize);
1329 for bit in 0..8u32 {
1330 if (d & (0x80 >> bit)) != 0 {
1331 let px = x + bit;
1332 if px < w {
1333 if let Some((cx0, _, cx1, _)) = clip {
1334 if px < cx0 || px >= cx1 {
1335 continue;
1336 }
1337 }
1338 core::ptr::write_volatile(row_ptr.add(px as usize), hw_color);
1339 }
1340 }
1341 }
1342 }
1343 }
1344 }
1345
1346 pub fn draw_string(&self, x: u32, y: u32, s: &str, color: Color) {
1347 let mut cur_x = x;
1348 for c in s.chars() {
1349 self.draw_char(cur_x, y, c, color);
1350 cur_x += 8;
1351 }
1352 }
1353
1354 pub fn measure_string_monospace(&self, s: &str) -> u32 {
1355 let mut font_lock = crate::kernel::vector_font::GLOBAL_TERMINAL_FONT.lock();
1356 if let Some(ref mut font) = *font_lock {
1357 font.get_string_width(s, 16)
1358 } else {
1359 let mut width = 0;
1360 for c in s.chars() {
1361 width += if (c as u32) < 128 { 8 } else { 16 };
1362 }
1363 width
1364 }
1365 }
1366
1367 pub fn draw_string_monospace(&self, x: u32, y: u32, s: &str, color: Color) {
1368 let mut font_lock = crate::kernel::vector_font::GLOBAL_TERMINAL_FONT.lock();
1369 if let Some(font) = font_lock.as_mut() {
1370 let mut cur_x = x as i32;
1371 let mut cur_y = y as i32;
1372 let (target_ptr, w, h) = self.resolve_target();
1373 let stride = self.resolve_stride();
1374 let clip = self.clip.get();
1375
1376 for c in s.chars() {
1377 if c == '\n' {
1378 cur_x = x as i32;
1379 cur_y += 16;
1380 continue;
1381 }
1382
1383 if let Some(glyph) = font.get_glyph(c, 16) {
1384 let gx = cur_x + glyph.x_offset;
1385 let gy = cur_y + glyph.y_offset + (16 * 14 / 16);
1386
1387 for row in 0..glyph.height {
1388 let py = gy + row as i32;
1389 if py < 0 || py >= h as i32 {
1390 continue;
1391 }
1392 if let Some((_, cy0, _, cy1)) = clip {
1393 if (py as u32) < cy0 || (py as u32) >= cy1 {
1394 continue;
1395 }
1396 }
1397 let row_offset = (py as usize) * (stride as usize);
1398
1399 for col in 0..glyph.width {
1400 let px = gx + col as i32;
1401 if px < 0 || px >= w as i32 {
1402 continue;
1403 }
1404 if let Some((cx0, _, cx1, _)) = clip {
1405 if (px as u32) < cx0 || (px as u32) >= cx1 {
1406 continue;
1407 }
1408 }
1409 let alpha = glyph.data[(row * glyph.width + col) as usize];
1410 if alpha > 0 {
1411 unsafe {
1412 let ptr = target_ptr.add(row_offset + px as usize);
1413 let bg_val = Color::from_hardware_format(core::ptr::read_volatile(ptr)).0 | 0xFF00_0000;
1414 let blended = crate::os_lib::web_engine::alpha_blend::blend_argb_over(
1415 ((alpha as u32) << 24) | (color.0 & 0x00FF_FFFF),
1416 bg_val,
1417 );
1418 core::ptr::write_volatile(ptr, Color(blended).to_hardware_format());
1419 }
1420 }
1421 }
1422 }
1423 cur_x += glyph.advance as i32;
1424 } else {
1425 let char_width = if (c as u32) > 127 { 16 } else { 8 };
1426 cur_x += char_width;
1427 }
1428
1429 if cur_x > w as i32 - 16 {
1430 break;
1431 }
1432 }
1433 } else {
1434 let mut cur_x = x;
1435 let (_, w, _) = self.resolve_target();
1436 for c in s.chars() {
1437 if c == '\n' {
1438 continue;
1439 }
1440 let code = c as u32;
1441 if code < 128 {
1442 self.draw_char(cur_x, y, c, color);
1443 cur_x += 8;
1444 } else {
1445 self.draw_char_ja_or_fallback(cur_x, y, code, color);
1446 cur_x += 16;
1447 }
1448 if cur_x >= w.saturating_sub(8) {
1449 break;
1450 }
1451 }
1452 }
1453 }
1454
1455 pub fn draw_string_monospace_bg(&self, x: u32, y: u32, s: &str, color: Color, bg: Color) {
1457 let mut font_lock = crate::kernel::vector_font::GLOBAL_TERMINAL_FONT.lock();
1458 if let Some(ref mut font) = *font_lock {
1459 let mut cur_x = x as i32;
1460 let mut cur_y = y as i32;
1461 let (_, w, h) = self.resolve_target();
1462
1463 for c in s.chars() {
1464 if c == '\n' {
1465 cur_x = x as i32;
1466 cur_y += 16;
1467 continue;
1468 }
1469
1470 if let Some(glyph) = font.get_color_glyph(c, 16, color.0, bg.0) {
1471 let gx = cur_x + glyph.x_offset;
1472 let gy = cur_y + glyph.y_offset + (16 * 14 / 16);
1473
1474 for row in 0..glyph.height {
1475 let py = gy + row as i32;
1476 if py < 0 || py >= h as i32 {
1477 continue;
1478 }
1479 for col in 0..glyph.width {
1480 let px = gx + col as i32;
1481 if px < 0 || px >= w as i32 {
1482 continue;
1483 }
1484 let pixel_color = glyph.data[(row * glyph.width + col) as usize];
1485 if (pixel_color >> 24) > 0 {
1486 self.draw_pixel(px as u32, py as u32, Color(pixel_color));
1488 }
1489 }
1490 }
1491 cur_x += glyph.advance as i32;
1492 } else {
1493 let char_width = if (c as u32) > 127 { 16 } else { 8 };
1494 cur_x += char_width;
1495 }
1496
1497 if cur_x > w as i32 - 16 {
1498 break;
1499 }
1500 }
1501 } else {
1502 let mut cur_x = x;
1504 let (_, w, _) = self.resolve_target();
1505 for c in s.chars() {
1506 if c == '\n' {
1507 continue;
1508 }
1509 let code = c as u32;
1510 let char_w = if code < 128 { 8u32 } else { 16u32 };
1511 self.boxfill(cur_x, y, cur_x + char_w, y + 16, bg);
1513 if code < 128 {
1514 self.draw_char(cur_x, y, c, color);
1515 cur_x += 8;
1516 } else {
1517 self.draw_char_ja_or_fallback(cur_x, y, code, color);
1518 cur_x += 16;
1519 }
1520 if cur_x >= w.saturating_sub(8) {
1521 break;
1522 }
1523 }
1524 }
1525 }
1526
1527 pub fn measure_string_ja(&self, s: &str, size: u32) -> u32 {
1528 let mut width = 0;
1529 let mut font_lock = crate::kernel::vector_font::GLOBAL_VECTOR_FONT.lock();
1530 if let Some(ref mut font) = *font_lock {
1531 for c in s.chars() {
1532 if let Some(glyph) = font.get_glyph(c, size) {
1533 width += glyph.advance;
1534 } else {
1535 width += if (c as u32) < 128 { size / 2 } else { size };
1536 }
1537 }
1538 } else {
1539 for c in s.chars() {
1540 width += if (c as u32) < 128 { 8 } else { 16 };
1541 }
1542 }
1543 width
1544 }
1545
1546 pub fn draw_string_ja(&self, x: u32, y: u32, s: &str, color: Color) {
1548 self.draw_string_ja_size(x, y, s, 16, color);
1549 }
1550
1551 pub fn draw_string_ja_size(&self, x: u32, y: u32, s: &str, size: u32, color: Color) {
1552 self.draw_string_ja_size_ext(x, y, s, size, color, false);
1553 }
1554
1555 pub fn draw_string_ja_size_ext(
1556 &self,
1557 x: u32,
1558 y: u32,
1559 s: &str,
1560 size: u32,
1561 color: Color,
1562 italic: bool,
1563 ) {
1564 let mut font_lock = crate::kernel::vector_font::GLOBAL_VECTOR_FONT.lock();
1565 if let Some(font) = font_lock.as_mut() {
1566 let mut cur_x = x as i32;
1567 let mut cur_y = y as i32;
1568 let (_, w, h) = self.resolve_target();
1569
1570 for c in s.chars() {
1571 if c == '\n' {
1572 cur_x = x as i32;
1573 cur_y += size as i32;
1574 continue;
1575 }
1576
1577 if let Some(glyph) = font.get_glyph(c, size) {
1578 let gx = cur_x + glyph.x_offset;
1579 let gy = cur_y + glyph.y_offset + (size as i32 * 14 / 16);
1580
1581 for row in 0..glyph.height {
1582 let py = gy + row as i32;
1583 if py < 0 || py >= h as i32 {
1584 continue;
1585 }
1586 for col in 0..glyph.width {
1587 let mut px = gx + col as i32;
1588 if italic {
1589 let shift = ((glyph.height as i32 - 1 - row as i32) * 2) / 7;
1593 px += shift;
1594 }
1595 if px < 0 || px >= w as i32 {
1596 continue;
1597 }
1598 let alpha = glyph.data[(row * glyph.width + col) as usize];
1599 self.draw_pixel_alpha(px as u32, py as u32, color, alpha);
1600 }
1601 }
1602 cur_x += glyph.advance as i32;
1603 } else {
1604 let code = c as u32;
1605 let char_width = if code > 127 {
1606 size as i32
1607 } else {
1608 size as i32 / 2
1609 };
1610 if code < 128 {
1611 let mut cx = cur_x;
1612 if italic {
1613 cx += 2;
1614 }
1615 self.draw_char(cx as u32, cur_y as u32, c, color);
1616 } else {
1617 let mut cx = cur_x;
1619 if italic {
1620 cx += 3;
1621 }
1622 self.draw_char_ja_or_fallback(cx as u32, cur_y as u32, code, color);
1623 }
1624 cur_x += char_width;
1625 }
1626
1627 if cur_x > w as i32 - size as i32 {
1628 cur_x = x as i32;
1629 cur_y += size as i32;
1630 if cur_y >= h as i32 {
1631 break;
1632 }
1633 }
1634 }
1635 } else {
1636 let mut cur_x = x;
1637 for c in s.chars() {
1638 let code = c as u32;
1639 let mut cx = cur_x;
1640 if italic {
1641 cx += 2;
1642 }
1643 if code < 128 {
1644 self.draw_char(cx, y, c, color);
1645 cur_x += size / 2;
1646 } else {
1647 self.draw_char_ja_or_fallback(cx, y, code, color);
1648 cur_x += size;
1649 }
1650 }
1651 }
1652 }
1653
1654 pub fn draw_string_ja_size_bg(
1655 &self,
1656 x: u32,
1657 y: u32,
1658 s: &str,
1659 size: u32,
1660 color: Color,
1661 bg: Color,
1662 ) {
1663 self.draw_string_ja_size_ext_bg(x, y, s, size, color, false, bg);
1664 }
1665
1666 pub fn draw_string_ja_size_ext_bg(
1667 &self,
1668 x: u32,
1669 y: u32,
1670 s: &str,
1671 size: u32,
1672 color: Color,
1673 italic: bool,
1674 bg: Color,
1675 ) {
1676 self.draw_string_ja_size_ext_bg_ls(x, y, s, size, color, italic, bg, 0);
1677 }
1678
1679 #[allow(clippy::too_many_arguments)]
1681 pub fn draw_string_ja_size_ext_bg_ls(
1682 &self,
1683 x: u32,
1684 y: u32,
1685 s: &str,
1686 size: u32,
1687 color: Color,
1688 italic: bool,
1689 bg: Color,
1690 ls: i32,
1691 ) {
1692 self.draw_string_ja_size_ext_bg_ls_ws(x, y, s, size, color, italic, bg, ls, 0);
1693 }
1694
1695 #[allow(clippy::too_many_arguments)]
1697 #[allow(clippy::too_many_arguments)]
1720 pub fn draw_string_family(
1721 &self,
1722 x: u32,
1723 y: u32,
1724 s: &str,
1725 size: u32,
1726 color: Color,
1727 italic: bool,
1728 bg: Color,
1729 ls: i32,
1730 ws: i32,
1731 family_list: &str,
1732 weight: u16,
1733 ) -> u16 {
1734 if !family_list.is_empty() {
1735 let mut pos = (x as i32, y as i32);
1742 let drawn = crate::kernel::vector_font::with_family_font_batched(
1743 s,
1744 family_list,
1745 weight,
1746 crate::kernel::glyph_batch::DRAW_BATCH_CHARS,
1747 false,
1748 |font, _, chunk| !self.draw_glyphs_with_font(font, x, &mut pos, chunk, size, color, italic, bg, ls, ws),
1749 );
1750 if let Some(got) = drawn {
1751 return got;
1752 }
1753 if !family_list.is_empty() {
1757 static MISS: spin::Mutex<alloc::collections::BTreeSet<alloc::string::String>> =
1758 spin::Mutex::new(alloc::collections::BTreeSet::new());
1759 let mut m = MISS.lock();
1760 if m.insert(alloc::string::String::from(family_list)) {
1761 crate::warn!("[FONT] 該当フォント無しで既定へ family={:?}", family_list);
1762 }
1763 }
1764 }
1765 self.draw_string_ja_size_ext_bg_ls_ws(x, y, s, size, color, italic, bg, ls, ws);
1766 0
1767 }
1768
1769 #[allow(clippy::too_many_arguments)]
1770 pub fn draw_string_ja_size_ext_bg_ls_ws(
1771 &self,
1772 x: u32,
1773 y: u32,
1774 s: &str,
1775 size: u32,
1776 color: Color,
1777 italic: bool,
1778 bg: Color,
1779 ls: i32,
1780 ws: i32,
1781 ) {
1782 let mut font_lock = crate::kernel::vector_font::GLOBAL_VECTOR_FONT.lock();
1783 if let Some(font) = font_lock.as_mut() {
1784 let mut pos = (x as i32, y as i32);
1785 self.draw_glyphs_with_font(font, x, &mut pos, s, size, color, italic, bg, ls, ws);
1786 }
1787 }
1788
1789 #[allow(clippy::too_many_arguments)]
1795 fn draw_glyphs_with_font(
1796 &self,
1797 font: &mut crate::kernel::vector_font::VectorFont,
1798 x: u32,
1799 pos: &mut (i32, i32),
1800 s: &str,
1801 size: u32,
1802 color: Color,
1803 italic: bool,
1804 bg: Color,
1805 ls: i32,
1806 ws: i32,
1807 ) -> bool {
1808 let font_opt: Option<&mut crate::kernel::vector_font::VectorFont> = Some(font);
1816 let y = pos.1.max(0) as u32;
1817 let mut reached_bottom = false;
1818 if let Some(font) = font_opt {
1819 let mut cur_x = pos.0;
1820 let mut cur_y = pos.1;
1821 let (target_ptr, w, h) = self.resolve_target();
1822 let stride = self.resolve_stride();
1823 let clip = self.clip.get();
1824
1825 for c in s.chars() {
1826 if c == '\n' {
1827 cur_x = x as i32;
1828 cur_y += size as i32;
1829 continue;
1830 }
1831
1832 if size >= 40 {
1834 static GLYPH_DIAG: core::sync::atomic::AtomicU32 =
1835 core::sync::atomic::AtomicU32::new(0);
1836 let n = GLYPH_DIAG.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
1837 if n < 6 {
1838 let g = font.get_color_glyph(c, size, color.0, bg.0);
1839 match g {
1840 Some(gl) => crate::warn!(
1841 "[GLYPH][DIAG] c={:?} size={} target_w={} target_h={} cur=({},{}) gw={} gh={} xoff={} yoff={} adv={} px0=0x{:08x}",
1842 c, size, w, h, cur_x, cur_y,
1843 gl.width, gl.height, gl.x_offset, gl.y_offset, gl.advance,
1844 gl.data.first().copied().unwrap_or(0)
1845 ),
1846 None => crate::warn!(
1847 "[GLYPH][DIAG] c={:?} size={} target_w={} target_h={} -> NO GLYPH (fallback path)",
1848 c, size, w, h
1849 ),
1850 }
1851 }
1852 }
1853
1854 if let Some(glyph) = font.get_color_glyph(c, size, color.0, bg.0) {
1855 let gx = cur_x + glyph.x_offset;
1856 let gy = cur_y + glyph.y_offset + (size as i32 * 14 / 16);
1857
1858 let mut written = 0u32;
1859
1860 let clip_y0 = clip.map(|c| c.1 as i32).unwrap_or(0).max(0);
1861 let clip_y1 = clip.map(|c| c.3 as i32).unwrap_or(h as i32).min(h as i32);
1862 let row_start = (clip_y0 - gy).clamp(0, glyph.height as i32) as usize;
1863 let row_end = (clip_y1 - gy).clamp(0, glyph.height as i32) as usize;
1864
1865 if !italic {
1866 let clip_x0 = clip.map(|c| c.0 as i32).unwrap_or(0).max(0);
1867 let clip_x1 = clip.map(|c| c.2 as i32).unwrap_or(w as i32).min(w as i32);
1868 let col_start = (clip_x0 - gx).clamp(0, glyph.width as i32) as usize;
1869 let col_end = (clip_x1 - gx).clamp(0, glyph.width as i32) as usize;
1870
1871 if row_start < row_end && col_start < col_end {
1872 let gw = glyph.width as usize;
1873 let stride_usize = stride as usize;
1874 for row in row_start..row_end {
1875 let py = (gy + row as i32) as usize;
1876 let row_offset = py * stride_usize;
1877 let glyph_row_offset = row * gw;
1878 unsafe {
1879 let mut dst_ptr = target_ptr.add(row_offset + ((gx + col_start as i32) as usize));
1880 for src_idx in (glyph_row_offset + col_start)..(glyph_row_offset + col_end) {
1881 let pixel_color = *glyph.data.get_unchecked(src_idx);
1882 let a = (pixel_color >> 24) & 0xFF;
1883 if a == 0xFF {
1884 let hw = Color(pixel_color).to_hardware_format();
1885 core::ptr::write_volatile(dst_ptr, hw);
1886 written += 1;
1887 } else if a > 0 {
1888 let bg_val = Color::from_hardware_format(core::ptr::read_volatile(dst_ptr)).0 | 0xFF00_0000;
1889 let eff_a = crate::os_lib::web_engine::font_gamma::correct_glyph_coverage(
1890 a as u8,
1891 pixel_color,
1892 bg_val,
1893 );
1894 let adjusted_pixel_color = ((eff_a as u32) << 24) | (pixel_color & 0x00FF_FFFF);
1895 let blended = crate::os_lib::web_engine::alpha_blend::blend_argb_over(adjusted_pixel_color, bg_val);
1896 core::ptr::write_volatile(dst_ptr, Color(blended).to_hardware_format());
1897 written += 1;
1898 }
1899 dst_ptr = dst_ptr.add(1);
1900 }
1901 }
1902 }
1903 }
1904 } else {
1905 let gw = glyph.width as usize;
1907 let gh = glyph.height as i32;
1908 for row in row_start..row_end {
1909 let py = gy + row as i32;
1910 let row_offset = (py as usize) * (stride as usize);
1911 let shift = ((gh - 1 - row as i32) * 2) / 7;
1912
1913 for col in 0..glyph.width {
1914 let px = gx + col as i32 + shift;
1915 if px < 0 || px >= w as i32 {
1916 continue;
1917 }
1918 if let Some((cx0, _, cx1, _)) = clip {
1919 if (px as u32) < cx0 || (px as u32) >= cx1 {
1920 continue;
1921 }
1922 }
1923 let pixel_color = glyph.data[row * gw + col as usize];
1924 let a = (pixel_color >> 24) & 0xFF;
1925 if a > 0 {
1926 unsafe {
1927 let ptr = target_ptr.add(row_offset + px as usize);
1928 if a == 0xFF {
1929 core::ptr::write_volatile(ptr, Color(pixel_color).to_hardware_format());
1930 } else {
1931 let bg_val = Color::from_hardware_format(core::ptr::read_volatile(ptr)).0 | 0xFF00_0000;
1932 let eff_a = crate::os_lib::web_engine::font_gamma::correct_glyph_coverage(
1933 a as u8,
1934 pixel_color,
1935 bg_val,
1936 );
1937 let adjusted_pixel_color = ((eff_a as u32) << 24) | (pixel_color & 0x00FF_FFFF);
1938 let blended = crate::os_lib::web_engine::alpha_blend::blend_argb_over(adjusted_pixel_color, bg_val);
1939 core::ptr::write_volatile(ptr, Color(blended).to_hardware_format());
1940 }
1941 }
1942 written += 1;
1943 }
1944 }
1945 }
1946 }
1947 if size >= 40 {
1948 static PX_DIAG: core::sync::atomic::AtomicU32 =
1949 core::sync::atomic::AtomicU32::new(0);
1950 HERO_TEXT_FRAME.store(
1953 FRAME_NO.load(core::sync::atomic::Ordering::Relaxed),
1954 core::sync::atomic::Ordering::Relaxed,
1955 );
1956 if PX_DIAG.fetch_add(1, core::sync::atomic::Ordering::Relaxed) < 4 {
1957 let (tp, tw, th) = self.resolve_target();
1958 crate::warn!(
1962 "[TARGET][TEXT] c={:?} target=0x{:x} w={} h={} calls={} gx={} gy={} clip={:?} rt=0x{:x} bb=0x{:x} vram=0x{:x} use_bb={}",
1963 c, tp as usize, tw, th, written, gx, gy, self.clip.get(),
1964 self.render_target.get() as usize,
1965 self.back_buffer as usize,
1966 self.vram as usize,
1967 self.use_back_buffer.get()
1968 );
1969 }
1970 }
1971 cur_x += glyph.advance as i32 + ls + if c == ' ' { ws } else { 0 };
1972 } else {
1973 let code = c as u32;
1974 let char_width = if code > 127 {
1975 size as i32
1976 } else {
1977 size as i32 / 2
1978 };
1979 if code < 128 {
1980 let mut cx = cur_x;
1981 if italic {
1982 cx += 2;
1983 }
1984 self.boxfill(
1986 cx as u32,
1987 cur_y as u32,
1988 cx as u32 + (size / 2),
1989 cur_y as u32 + size,
1990 bg,
1991 );
1992 self.draw_char(cx as u32, cur_y as u32, c, color);
1993 } else {
1994 let mut cx = cur_x;
1996 if italic {
1997 cx += 3;
1998 }
1999 self.boxfill(
2000 cx as u32,
2001 cur_y as u32,
2002 cx as u32 + size,
2003 cur_y as u32 + size,
2004 bg,
2005 );
2006 self.draw_char_ja_or_fallback(cx as u32, cur_y as u32, code, color);
2007 }
2008 cur_x += char_width + ls + if c == ' ' { ws } else { 0 };
2009 }
2010
2011 if cur_x > w as i32 - size as i32 {
2012 cur_x = x as i32;
2013 cur_y += size as i32;
2014 if cur_y >= h as i32 {
2015 reached_bottom = true;
2016 break;
2017 }
2018 }
2019 }
2020 *pos = (cur_x, cur_y);
2021 } else {
2022 let mut cur_x = x;
2024 let (_, _w, _) = self.resolve_target();
2025 for c in s.chars() {
2026 let code = c as u32;
2027 let mut cx = cur_x;
2028 if italic {
2029 cx += 2;
2030 }
2031 let char_w = if code < 128 { size / 2 } else { size };
2032 self.boxfill(cx, y, cx + char_w, y + size, bg);
2033 if code < 128 {
2034 self.draw_char(cx, y, c, color);
2035 cur_x = (cur_x as i32 + size as i32 / 2 + ls).max(0) as u32;
2036 } else {
2037 self.draw_char_ja_or_fallback(cx, y, code, color);
2038 cur_x = (cur_x as i32 + size as i32 + ls).max(0) as u32;
2039 }
2040 }
2041 *pos = (cur_x as i32, pos.1);
2042 }
2043 reached_bottom
2044 }
2045
2046 pub fn draw_text_emphasis(
2053 &self,
2054 x: u32,
2055 y: u32,
2056 s: &str,
2057 size: u32,
2058 mark: &str,
2059 color: Color,
2060 ls: i32,
2061 ws: i32,
2062 ) {
2063 if mark.is_empty() {
2064 return;
2065 }
2066 let mut font_lock = crate::kernel::vector_font::GLOBAL_VECTOR_FONT.lock();
2067 let Some(font) = font_lock.as_mut() else {
2068 return;
2069 };
2070 let mut cur_x = x as i32;
2071 let mut cur_y = y as i32;
2072 let (_, w, h) = self.resolve_target();
2073 let r = ((size as i32) / 6).max(1);
2075 for c in s.chars() {
2076 if c == '\n' {
2077 cur_x = x as i32;
2078 cur_y += size as i32;
2079 continue;
2080 }
2081 let advance = if let Some(glyph) = font.get_color_glyph(c, size, color.0, color.0) {
2083 glyph.advance as i32
2084 } else {
2085 let code = c as u32;
2086 if code > 127 {
2087 size as i32
2088 } else {
2089 size as i32 / 2
2090 }
2091 };
2092 if !c.is_whitespace() {
2093 let cx = cur_x + advance / 2;
2094 let cy = cur_y - r * 3;
2095 if cx >= 0 && cy >= 0 && (cx as u32) < w && (cy as u32) < h {
2096 match mark {
2097 "circle" => self.draw_circle_outline(cx as u32, cy as u32, r as u32, color),
2098 "triangle" => self.draw_triangle_fill(cx as u32, cy as u32, r as u32, color),
2099 "sesame" => {
2100 self.draw_circle_fill((cx - r) as u32, cy as u32, (r / 2).max(1) as u32, color);
2101 self.draw_circle_fill((cx + r) as u32, cy as u32, (r / 2).max(1) as u32, color);
2102 }
2103 _ => self.draw_circle_fill(cx as u32, cy as u32, r as u32, color),
2106 }
2107 }
2108 }
2109 cur_x += advance + ls + if c == ' ' { ws } else { 0 };
2110 }
2111 }
2112
2113 fn draw_circle_fill(&self, cx: u32, cy: u32, r: u32, color: Color) {
2115 let r_i = r as i32;
2116 for dy in -r_i..=r_i {
2117 for dx in -r_i..=r_i {
2118 if dx * dx + dy * dy <= r_i * r_i {
2119 let px = cx as i32 + dx;
2120 let py = cy as i32 + dy;
2121 if px >= 0 && py >= 0 {
2122 self.draw_pixel(px as u32, py as u32, color);
2123 }
2124 }
2125 }
2126 }
2127 }
2128
2129 fn draw_circle_outline(&self, cx: u32, cy: u32, r: u32, color: Color) {
2131 let r_i = r as i32;
2132 let r_inner = (r_i - 1).max(0);
2133 for dy in -r_i..=r_i {
2134 for dx in -r_i..=r_i {
2135 let d2 = dx * dx + dy * dy;
2136 if d2 <= r_i * r_i && d2 >= r_inner * r_inner {
2137 let px = cx as i32 + dx;
2138 let py = cy as i32 + dy;
2139 if px >= 0 && py >= 0 {
2140 self.draw_pixel(px as u32, py as u32, color);
2141 }
2142 }
2143 }
2144 }
2145 }
2146
2147 fn draw_triangle_fill(&self, cx: u32, cy: u32, r: u32, color: Color) {
2149 let r_i = r as i32;
2150 for dy in 0..=(r_i * 2) {
2151 let py = cy as i32 - r_i + dy;
2152 if py < 0 {
2153 continue;
2154 }
2155 let half_w = (dy * r_i) / (r_i * 2).max(1);
2156 for dx in -half_w..=half_w {
2157 let px = cx as i32 + dx;
2158 if px >= 0 {
2159 self.draw_pixel(px as u32, py as u32, color);
2160 }
2161 }
2162 }
2163 }
2164
2165 fn draw_char_ja_16x16(&self, x: u32, y: u32, bitmap: &[u8; 8], color: Color) {
2167 for row in 0..8 {
2168 let byte = bitmap[row];
2169 for col in 0..8 {
2170 if (byte & (0x80 >> col)) != 0 {
2171 let px = x + col as u32 * 2;
2172 let py = y + row as u32 * 2;
2173 self.draw_pixel(px, py, color);
2174 self.draw_pixel(px + 1, py, color);
2175 self.draw_pixel(px, py + 1, color);
2176 self.draw_pixel(px + 1, py + 1, color);
2177 }
2178 }
2179 }
2180 }
2181
2182 pub fn draw_char_ja_or_fallback(&self, x: u32, y: u32, code: u32, color: Color) {
2184 if let Some(bitmap) = crate::kernel::font_ja::get_font_bitmap(code) {
2185 self.draw_char_ja_16x16(x, y, bitmap, color);
2186 } else if let Some((bitmap, emoji_col)) = crate::kernel::emoji::get_emoji_bitmap(code) {
2187 self.draw_char_ja_16x16(x, y, bitmap, emoji_col.unwrap_or(color));
2188 } else {
2189 self.draw_char_ja_tofu(x, y, color);
2190 }
2191 }
2192
2193 fn draw_char_ja_tofu(&self, x: u32, y: u32, color: Color) {
2195 for dy in 0..16 {
2196 for dx in 0..16 {
2197 if dx == 0 || dx == 15 || dy == 0 || dy == 15 {
2198 self.draw_pixel(x + dx, y + dy, color);
2199 }
2200 }
2201 }
2202 }
2203
2204 pub fn draw_svg_icon(&self, x: u32, y: u32, svg_data: &[u8], size: u32, tint: Color) {
2210 if let Some(alpha_mask) = crate::os_lib::svg::get_or_rasterize(svg_data, size) {
2211 for row in 0..size {
2212 for col in 0..size {
2213 let alpha = alpha_mask[(row * size + col) as usize];
2214 if alpha > 0 {
2215 self.draw_pixel_alpha(x + col, y + row, tint, alpha);
2216 }
2217 }
2218 }
2219 }
2220 }
2221
2222 pub fn draw_bmp(
2224 &self,
2225 x: u32,
2226 y: u32,
2227 target_width: u32,
2228 target_height: u32,
2229 bmp_data: &[u8],
2230 ) -> Result<(), &'static str> {
2231 if bmp_data.len() < 54 {
2232 return Err("Data too short");
2233 }
2234 if &bmp_data[0..2] != b"BM" {
2235 return Err("Not a BMP file");
2236 }
2237
2238 let off_bits =
2239 u32::from_le_bytes(bmp_data[10..14].try_into().map_err(|_| "Invalid offset")?) as usize;
2240 let width = i32::from_le_bytes(bmp_data[18..22].try_into().map_err(|_| "Invalid width")?);
2241 let height = i32::from_le_bytes(bmp_data[22..26].try_into().map_err(|_| "Invalid height")?);
2242 let bit_count = u16::from_le_bytes(
2243 bmp_data[28..30]
2244 .try_into()
2245 .map_err(|_| "Invalid bitcount")?,
2246 );
2247 let compression = u32::from_le_bytes(
2248 bmp_data[30..34]
2249 .try_into()
2250 .map_err(|_| "Invalid compression")?,
2251 );
2252
2253 if compression != 0 {
2254 return Err("Only uncompressed BMP is supported");
2255 }
2256 if bit_count != 24 && bit_count != 32 {
2257 return Err("Only 24-bit or 32-bit BMP is supported");
2258 }
2259
2260 let abs_height = height.unsigned_abs();
2261 let is_bottom_up = height > 0;
2262 let bytes_per_pixel = (bit_count / 8) as usize;
2263 let row_stride = (width as usize * bytes_per_pixel).div_ceil(4) * 4;
2264
2265 if target_width == 0 || target_height == 0 {
2266 return Ok(());
2267 }
2268
2269 for row in 0..target_height {
2270 let src_row_idx = (row * abs_height) / target_height;
2271 let src_row = if is_bottom_up {
2272 abs_height - 1 - src_row_idx
2273 } else {
2274 src_row_idx
2275 };
2276 let row_offset = off_bits + (src_row as usize * row_stride);
2277 if row_offset + (width as usize * bytes_per_pixel) > bmp_data.len() {
2278 return Err("BMP pixel data out of bounds");
2279 }
2280 for col in 0..target_width {
2281 let src_col = (col * width as u32) / target_width;
2282 let pixel_offset = row_offset + (src_col as usize * bytes_per_pixel);
2283 let b = bmp_data[pixel_offset];
2284 let g = bmp_data[pixel_offset + 1];
2285 let r = bmp_data[pixel_offset + 2];
2286 let a = if bytes_per_pixel == 4 {
2287 bmp_data[pixel_offset + 3]
2288 } else {
2289 255
2290 };
2291
2292 let color = Color(0xFF000000 | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32));
2293 self.draw_pixel_alpha(x + col, y + row, color, a);
2294 }
2295 }
2296 Ok(())
2297 }
2298
2299 pub fn draw_image(
2301 &self,
2302 x: u32,
2303 y: u32,
2304 target_width: u32,
2305 target_height: u32,
2306 data: &[u8],
2307 ) -> Result<(), &'static str> {
2308 if target_width == 0 || target_height == 0 {
2309 return Ok(());
2310 }
2311
2312 if data.len() > 8 && &data[0..8] == b"\x89PNG\r\n\x1a\n" {
2313 let mut decoder = zune_png::PngDecoder::new(data);
2314 match decoder.decode_raw() {
2315 Ok(pixels) => {
2316 let info = decoder.get_info().ok_or("Failed to get PNG info")?;
2317 let width = info.width as u32;
2318 let height = info.height as u32;
2319 let bytes_per_pixel = pixels.len() / (width as usize * height as usize).max(1);
2320
2321 for row in 0..target_height {
2322 let src_row = (row * height) / target_height;
2323 for col in 0..target_width {
2324 let src_col = (col * width) / target_width;
2325 let idx = ((src_row * width + src_col) as usize) * bytes_per_pixel;
2326 if idx + bytes_per_pixel <= pixels.len() {
2327 let (r, g, b, a) = match bytes_per_pixel {
2328 1 => (pixels[idx], pixels[idx], pixels[idx], 255),
2329 2 => (pixels[idx], pixels[idx], pixels[idx], pixels[idx + 1]),
2330 3 => (pixels[idx], pixels[idx + 1], pixels[idx + 2], 255),
2331 4 => (
2332 pixels[idx],
2333 pixels[idx + 1],
2334 pixels[idx + 2],
2335 pixels[idx + 3],
2336 ),
2337 _ => (0, 0, 0, 255),
2338 };
2339 let color = Color(
2340 0xFF000000
2341 | ((r as u32) << 16)
2342 | ((g as u32) << 8)
2343 | (b as u32),
2344 );
2345 self.draw_pixel_alpha(x + col, y + row, color, a);
2346 }
2347 }
2348 }
2349 Ok(())
2350 }
2351 Err(_) => Err("Failed to decode PNG"),
2352 }
2353 } else if data.len() > 2 && data[0] == 0xFF && data[1] == 0xD8 {
2354 let mut decoder = zune_jpeg::JpegDecoder::new(data);
2355 match decoder.decode() {
2356 Ok(pixels) => {
2357 let info = decoder.info().ok_or("Failed to get JPEG info")?;
2358 let width = info.width as u32;
2359 let height = info.height as u32;
2360 let bytes_per_pixel = pixels.len() / (width as usize * height as usize).max(1);
2361
2362 for row in 0..target_height {
2363 let src_row = (row * height) / target_height;
2364 for col in 0..target_width {
2365 let src_col = (col * width) / target_width;
2366 let idx = ((src_row * width + src_col) as usize) * bytes_per_pixel;
2367 if idx + bytes_per_pixel <= pixels.len() {
2368 let (r, g, b) = match bytes_per_pixel {
2369 1 => (pixels[idx], pixels[idx], pixels[idx]),
2370 3 => (pixels[idx], pixels[idx + 1], pixels[idx + 2]),
2371 _ => (0, 0, 0),
2372 };
2373 let color = Color(
2374 0xFF000000
2375 | ((r as u32) << 16)
2376 | ((g as u32) << 8)
2377 | (b as u32),
2378 );
2379 self.draw_pixel_alpha(x + col, y + row, color, 255);
2380 }
2381 }
2382 }
2383 Ok(())
2384 }
2385 Err(_) => Err("Failed to decode JPEG"),
2386 }
2387 } else if data.len() > 2 && &data[0..2] == b"BM" {
2388 self.draw_bmp(x, y, target_width, target_height, data)
2389 } else {
2390 Err("Unsupported image format")
2391 }
2392 }
2393
2394 pub fn draw_gradient_rect(
2397 &self,
2398 x0: u32,
2399 y0: u32,
2400 x1: u32,
2401 y1: u32,
2402 c1: Color,
2403 c2: Color,
2404 vertical: bool,
2405 ) {
2406 if x1 <= x0 || y1 <= y0 {
2407 return;
2408 }
2409 let r1 = ((c1.0 >> 16) & 0xFF) as f32;
2410 let g1 = ((c1.0 >> 8) & 0xFF) as f32;
2411 let b1 = (c1.0 & 0xFF) as f32;
2412 let r2 = ((c2.0 >> 16) & 0xFF) as f32;
2413 let g2 = ((c2.0 >> 8) & 0xFF) as f32;
2414 let b2 = (c2.0 & 0xFF) as f32;
2415 if vertical {
2416 let h = (y1 - y0) as f32;
2417 for dy in 0..(y1 - y0) {
2418 let t = dy as f32 / h.max(1.0);
2419 let r = ((1.0 - t) * r1 + t * r2) as u32;
2420 let g = ((1.0 - t) * g1 + t * g2) as u32;
2421 let b = ((1.0 - t) * b1 + t * b2) as u32;
2422 let c = 0xFF000000 | (r << 16) | (g << 8) | b;
2423 self.boxfill(x0, y0 + dy, x1, y0 + dy + 1, Color(c));
2424 }
2425 } else {
2426 let w = (x1 - x0) as f32;
2427 for dx in 0..(x1 - x0) {
2428 let t = dx as f32 / w.max(1.0);
2429 let r = ((1.0 - t) * r1 + t * r2) as u32;
2430 let g = ((1.0 - t) * g1 + t * g2) as u32;
2431 let b = ((1.0 - t) * b1 + t * b2) as u32;
2432 let c = 0xFF000000 | (r << 16) | (g << 8) | b;
2433 self.boxfill(x0 + dx, y0, x0 + dx + 1, y1, Color(c));
2434 }
2435 }
2436 }
2437
2438 pub fn draw_radial_gradient_rect(&self, x0: u32, y0: u32, x1: u32, y1: u32, c1: Color, c2: Color) {
2441 if x1 <= x0 || y1 <= y0 {
2442 return;
2443 }
2444 let r1 = ((c1.0 >> 16) & 0xFF) as f32;
2445 let g1 = ((c1.0 >> 8) & 0xFF) as f32;
2446 let b1 = (c1.0 & 0xFF) as f32;
2447 let r2 = ((c2.0 >> 16) & 0xFF) as f32;
2448 let g2 = ((c2.0 >> 8) & 0xFF) as f32;
2449 let b2 = (c2.0 & 0xFF) as f32;
2450 let w = (x1 - x0) as f32;
2451 let h = (y1 - y0) as f32;
2452 let cx = w / 2.0;
2453 let cy = h / 2.0;
2454 let max_dist = libm::sqrtf(cx * cx + cy * cy).max(1.0);
2455 for dy in 0..(y1 - y0) {
2456 for dx in 0..(x1 - x0) {
2457 let px = dx as f32 - cx;
2458 let py = dy as f32 - cy;
2459 let dist = libm::sqrtf(px * px + py * py);
2460 let t = (dist / max_dist).min(1.0);
2461 let r = ((1.0 - t) * r1 + t * r2) as u32;
2462 let g = ((1.0 - t) * g1 + t * g2) as u32;
2463 let b = ((1.0 - t) * b1 + t * b2) as u32;
2464 let c = 0xFF000000 | (r << 16) | (g << 8) | b;
2465 self.boxfill(x0 + dx, y0 + dy, x0 + dx + 1, y0 + dy + 1, Color(c));
2466 }
2467 }
2468 }
2469
2470 pub fn draw_conic_gradient_rect(&self, x0: u32, y0: u32, x1: u32, y1: u32, c1: Color, c2: Color) {
2475 if x1 <= x0 || y1 <= y0 {
2476 return;
2477 }
2478 let r1 = ((c1.0 >> 16) & 0xFF) as f32;
2479 let g1 = ((c1.0 >> 8) & 0xFF) as f32;
2480 let b1 = (c1.0 & 0xFF) as f32;
2481 let r2 = ((c2.0 >> 16) & 0xFF) as f32;
2482 let g2 = ((c2.0 >> 8) & 0xFF) as f32;
2483 let b2 = (c2.0 & 0xFF) as f32;
2484 let w = (x1 - x0) as f32;
2485 let h = (y1 - y0) as f32;
2486 let cx = w / 2.0;
2487 let cy = h / 2.0;
2488 const PI: f32 = core::f32::consts::PI;
2489 for dy in 0..(y1 - y0) {
2490 for dx in 0..(x1 - x0) {
2491 let px = dx as f32 - cx;
2492 let py = dy as f32 - cy;
2493 let mut angle = libm::atan2f(px, -py);
2495 if angle < 0.0 {
2496 angle += 2.0 * PI;
2497 }
2498 let t = angle / (2.0 * PI); let r = ((1.0 - t) * r1 + t * r2) as u32;
2500 let g = ((1.0 - t) * g1 + t * g2) as u32;
2501 let b = ((1.0 - t) * b1 + t * b2) as u32;
2502 let c = 0xFF000000 | (r << 16) | (g << 8) | b;
2503 self.boxfill(x0 + dx, y0 + dy, x0 + dx + 1, y0 + dy + 1, Color(c));
2504 }
2505 }
2506 }
2507
2508 fn sample_repeating_colors(colors: &[Color], t: f32) -> u32 {
2511 let Some(&first) = colors.first() else {
2512 return 0xFF000000;
2513 };
2514 if colors.len() < 2 {
2515 return first.0;
2516 }
2517 let scaled = t * (colors.len() - 1) as f32;
2518 let seg = (scaled as usize).min(colors.len() - 2);
2519 let local_t = scaled - seg as f32;
2520 let c1 = colors[seg];
2521 let c2 = colors[seg + 1];
2522 let r1 = ((c1.0 >> 16) & 0xFF) as f32;
2523 let g1 = ((c1.0 >> 8) & 0xFF) as f32;
2524 let b1 = (c1.0 & 0xFF) as f32;
2525 let r2 = ((c2.0 >> 16) & 0xFF) as f32;
2526 let g2 = ((c2.0 >> 8) & 0xFF) as f32;
2527 let b2 = (c2.0 & 0xFF) as f32;
2528 let r = ((1.0 - local_t) * r1 + local_t * r2) as u32;
2529 let g = ((1.0 - local_t) * g1 + local_t * g2) as u32;
2530 let b = ((1.0 - local_t) * b1 + local_t * b2) as u32;
2531 0xFF000000 | (r << 16) | (g << 8) | b
2532 }
2533
2534 pub fn draw_repeating_linear_gradient_rect(
2538 &self,
2539 x0: u32,
2540 y0: u32,
2541 x1: u32,
2542 y1: u32,
2543 colors: &[Color],
2544 cycle_px: i32,
2545 vertical: bool,
2546 ) {
2547 if x1 <= x0 || y1 <= y0 || colors.is_empty() {
2548 return;
2549 }
2550 let cycle = cycle_px.max(1) as f32;
2551 if vertical {
2552 for dy in 0..(y1 - y0) {
2553 let t = (dy as f32 % cycle) / cycle;
2554 let c = Self::sample_repeating_colors(colors, t);
2555 self.boxfill(x0, y0 + dy, x1, y0 + dy + 1, Color(c));
2556 }
2557 } else {
2558 for dx in 0..(x1 - x0) {
2559 let t = (dx as f32 % cycle) / cycle;
2560 let c = Self::sample_repeating_colors(colors, t);
2561 self.boxfill(x0 + dx, y0, x0 + dx + 1, y1, Color(c));
2562 }
2563 }
2564 }
2565
2566 pub fn draw_repeating_radial_gradient_rect(
2570 &self,
2571 x0: u32,
2572 y0: u32,
2573 x1: u32,
2574 y1: u32,
2575 colors: &[Color],
2576 cycle_px: i32,
2577 ) {
2578 if x1 <= x0 || y1 <= y0 || colors.is_empty() {
2579 return;
2580 }
2581 let cycle = cycle_px.max(1) as f32;
2582 let w = (x1 - x0) as f32;
2583 let h = (y1 - y0) as f32;
2584 let cx = w / 2.0;
2585 let cy = h / 2.0;
2586 for dy in 0..(y1 - y0) {
2587 for dx in 0..(x1 - x0) {
2588 let px = dx as f32 - cx;
2589 let py = dy as f32 - cy;
2590 let dist = libm::sqrtf(px * px + py * py);
2591 let t = (dist % cycle) / cycle;
2592 let c = Self::sample_repeating_colors(colors, t);
2593 self.boxfill(x0 + dx, y0 + dy, x0 + dx + 1, y0 + dy + 1, Color(c));
2594 }
2595 }
2596 }
2597
2598 pub fn draw_shadow_rect(
2600 &self,
2601 x0: i32,
2602 y0: i32,
2603 x1: i32,
2604 y1: i32,
2605 ox: i32,
2606 oy: i32,
2607 color: Color,
2608 ) {
2609 if x1 <= x0 || y1 <= y0 {
2610 return;
2611 }
2612 let sx0 = (x0 + ox).max(0) as u32;
2613 let sy0 = (y0 + oy).max(0) as u32;
2614 let sx1 = (x1 + ox).max(0) as u32;
2615 let sy1 = (y1 + oy).max(0) as u32;
2616 let alpha = ((color.0 >> 24) & 0xFF) as u8;
2617 let alpha = if alpha == 0 { 80u8 } else { alpha }; if sx1 <= sx0 || sy1 <= sy0 {
2619 return;
2620 }
2621 for y in sy0..sy1 {
2622 for x in sx0..sx1 {
2623 self.draw_pixel_alpha(x, y, Color(color.0 | 0xFF000000), alpha);
2624 }
2625 }
2626 }
2627
2628 #[allow(clippy::too_many_arguments)]
2636 pub fn draw_shadow_rect_rounded(
2637 &self,
2638 x0: i32,
2639 y0: i32,
2640 x1: i32,
2641 y1: i32,
2642 ox: i32,
2643 oy: i32,
2644 blur: i32,
2645 radius: (i32, i32, i32, i32),
2646 color: Color,
2647 ) {
2648 if x1 <= x0 || y1 <= y0 {
2649 return;
2650 }
2651 let alpha = ((color.0 >> 24) & 0xFF) as u8;
2652 let alpha = if alpha == 0 { 80u8 } else { alpha };
2653 let blur = blur.max(0);
2654 let half = (blur + 1) / 2;
2655 let (rx0, ry0, rx1, ry1) = (x0 + ox, y0 + oy, x1 + ox, y1 + oy);
2657 let sx0 = (rx0 - half).max(0);
2658 let sy0 = (ry0 - half).max(0);
2659 let sx1 = (rx1 + half).clamp(0, self.width as i32);
2660 let sy1 = (ry1 + half).clamp(0, self.height as i32);
2661 if sx1 <= sx0 || sy1 <= sy0 {
2662 return;
2663 }
2664 let w = x1 - x0;
2665 let h = y1 - y0;
2666 let has_radius = radius != (0, 0, 0, 0);
2667 let rgb = Color(color.0 | 0xFF000000);
2668
2669 for y in sy0..sy1 {
2670 for x in sx0..sx1 {
2671 let a = if blur == 0 {
2672 if has_radius
2673 && !crate::os_lib::web_engine::draw_helpers::point_in_rounded_rect(
2674 x - rx0,
2675 y - ry0,
2676 w,
2677 h,
2678 radius,
2679 )
2680 {
2681 continue;
2682 }
2683 alpha
2684 } else {
2685 let dx = (rx0 - x).max(x - (rx1 - 1)).max(0) as f32;
2687 let dy = (ry0 - y).max(y - (ry1 - 1)).max(0) as f32;
2688 let d = if dx > 0.0 || dy > 0.0 {
2689 libm::sqrtf(dx * dx + dy * dy)
2690 } else {
2691 -((x - rx0).min(rx1 - 1 - x).min(y - ry0).min(ry1 - 1 - y) as f32)
2692 };
2693 let c = crate::os_lib::web_engine::box_shadow::coverage(d, blur);
2694 if c <= 0.0 {
2695 continue;
2696 }
2697 (alpha as f32 * c) as u8
2698 };
2699 if a > 0 {
2700 self.draw_pixel_alpha(x as u32, y as u32, rgb, a);
2701 }
2702 }
2703 }
2704 }
2705
2706 pub fn draw_inset_shadow_rect(
2708 &self,
2709 x0: i32,
2710 y0: i32,
2711 x1: i32,
2712 y1: i32,
2713 ox: i32,
2714 oy: i32,
2715 blur: i32,
2716 radius: (i32, i32, i32, i32),
2717 color: Color,
2718 ) {
2719 if x1 <= x0 || y1 <= y0 {
2720 return;
2721 }
2722 let ix0 = x0.max(0) as u32;
2723 let iy0 = y0.max(0) as u32;
2724 let ix1 = x1.max(0) as u32;
2725 let iy1 = y1.max(0) as u32;
2726 let alpha = ((color.0 >> 24) & 0xFF) as u8;
2727 let alpha = if alpha == 0 { 90u8 } else { alpha };
2728 let shadow_color = color.0 | 0xFF000000;
2729 let w = x1 - x0;
2730 let h = y1 - y0;
2731 let has_radius = radius != (0, 0, 0, 0);
2732 let shadow_depth = blur.clamp(4, 30);
2733
2734 for y in iy0..iy1 {
2735 let ry = (y as i32) - y0;
2736 for x in ix0..ix1 {
2737 let rx = (x as i32) - x0;
2738 if has_radius && !crate::os_lib::web_engine::draw_helpers::point_in_rounded_rect(rx, ry, w, h, radius) {
2739 continue;
2740 }
2741 let d_left = rx - ox;
2742 let d_top = ry - oy;
2743 let d_right = (w - rx) + ox;
2744 let d_bottom = (h - ry) + oy;
2745 let min_dist = d_left.min(d_top).min(d_right).min(d_bottom);
2746 if min_dist >= 0 && min_dist < shadow_depth {
2747 let factor = 1.0 - (min_dist as f32 / shadow_depth as f32);
2748 let pix_alpha = ((alpha as f32) * factor) as u8;
2749 if pix_alpha > 0 {
2750 self.draw_pixel_alpha(x, y, Color(shadow_color), pix_alpha);
2751 }
2752 }
2753 }
2754 }
2755 }
2756
2757 pub fn fill_signed(&self, x0: i32, y0: i32, x1: i32, y1: i32, color: Color) {
2759 if let Some((a, b, c, d)) = crate::kernel::draw_clip::signed_rect_to_fill(x0, y0, x1, y1) {
2760 self.boxfill(a, b, c, d, color);
2761 }
2762 }
2763
2764 pub fn fill_signed_alpha(&self, x0: i32, y0: i32, x1: i32, y1: i32, color: Color, alpha: u8) {
2766 if let Some((a, b, c, d)) = crate::kernel::draw_clip::signed_rect_to_fill(x0, y0, x1, y1) {
2767 self.boxfill_alpha(a, b, c, d, color, alpha);
2768 }
2769 }
2770
2771 pub fn draw_rounded_rect_fill(
2779 &self,
2780 x0: i32,
2781 y0: i32,
2782 x1: i32,
2783 y1: i32,
2784 radius: (i32, i32, i32, i32),
2785 color: Color,
2786 ) {
2787 let (tl, tr, br, bl) = radius;
2788 if tl <= 0 && tr <= 0 && br <= 0 && bl <= 0 {
2789 self.fill_signed(x0, y0, x1 - 1, y1 - 1, color);
2790 return;
2791 }
2792 let w = x1 - x0;
2793 let h = y1 - y0;
2794 if w <= 0 || h <= 0 {
2795 return;
2796 }
2797 let mut run: Option<(i32, i32, i32)> = None; for k in 0..=h {
2799 let cur = if k < h {
2800 Some(crate::kernel::draw_clip::rounded_row_insets(k, w, h, radius))
2801 } else {
2802 None
2803 };
2804 if let Some((ks, l, r)) = run {
2805 if cur != Some((l, r)) {
2806 self.fill_signed(x0 + l, y0 + ks, x1 - r - 1, y0 + k - 1, color);
2807 run = None;
2808 }
2809 }
2810 if run.is_none() {
2811 if let Some((l, r)) = cur {
2812 run = Some((k, l, r));
2813 }
2814 }
2815 }
2816 }
2817
2818 pub fn draw_rounded_rect_border(
2820 &self,
2821 x0: i32,
2822 y0: i32,
2823 x1: i32,
2824 y1: i32,
2825 radius: (i32, i32, i32, i32),
2826 color: Color,
2827 _border_style: &str,
2828 ) {
2829 self.draw_rounded_rect_border_sides(x0, y0, x1, y1, radius, (1, 1, 1, 1), (color.0, color.0, color.0, color.0));
2830 }
2831
2832 pub fn draw_rounded_rect_border_sides(
2841 &self,
2842 x0: i32,
2843 y0: i32,
2844 x1: i32,
2845 y1: i32,
2846 radius: (i32, i32, i32, i32),
2847 widths: (i32, i32, i32, i32),
2848 colors: (u32, u32, u32, u32),
2849 ) {
2850 let w = x1 - x0;
2851 let h = y1 - y0;
2852 if w <= 0 || h <= 0 {
2853 return;
2854 }
2855 let (wt, wr, wb, wl) = (widths.0.max(0), widths.1.max(0), widths.2.max(0), widths.3.max(0));
2856 if wt == 0 && wr == 0 && wb == 0 && wl == 0 {
2857 return;
2858 }
2859 let cap = (w / 2).min(h / 2).max(0);
2860 let (tl, tr, br, bl) = (
2861 radius.0.clamp(0, cap),
2862 radius.1.clamp(0, cap),
2863 radius.2.clamp(0, cap),
2864 radius.3.clamp(0, cap),
2865 );
2866 let in_radius = (
2867 (tl - wt.max(wl)).max(0),
2868 (tr - wt.max(wr)).max(0),
2869 (br - wb.max(wr)).max(0),
2870 (bl - wb.max(wl)).max(0),
2871 );
2872 let iw = w - wl - wr;
2873 let ih = h - wt - wb;
2874 type Row = ([(i32, i32, u32); 2], usize);
2876 let mut run: Option<(i32, Row)> = None;
2877 for k in 0..=h {
2878 let cur: Option<Row> = if k < h {
2879 let (lo, ro) = crate::kernel::draw_clip::rounded_row_insets(k, w, h, radius);
2880 let ox0 = x0 + lo;
2881 let ox1 = x1 - ro;
2882 let mut segs = [(0, 0, 0u32); 2];
2883 let mut n = 0;
2884 let iy = k - wt;
2885 if iw > 0 && iy >= 0 && iy < ih {
2886 let (ilo, iro) = crate::kernel::draw_clip::rounded_row_insets(iy, iw, ih, in_radius);
2887 let ix0 = (x0 + wl + ilo).clamp(ox0, ox1);
2888 let ix1 = (x1 - wr - iro).clamp(ix0, ox1);
2889 if ix0 > ox0 {
2890 segs[n] = (ox0, ix0, colors.3);
2891 n += 1;
2892 }
2893 if ox1 > ix1 {
2894 segs[n] = (ix1, ox1, colors.1);
2895 n += 1;
2896 }
2897 } else if k < wt {
2898 segs[0] = (ox0, ox1, colors.0);
2899 n = 1;
2900 } else if k >= h - wb {
2901 segs[0] = (ox0, ox1, colors.2);
2902 n = 1;
2903 } else {
2904 let mid = (ox0 + ox1) / 2;
2906 segs[0] = (ox0, mid, colors.3);
2907 segs[1] = (mid, ox1, colors.1);
2908 n = 2;
2909 }
2910 Some((segs, n))
2911 } else {
2912 None
2913 };
2914 if let Some((ks, prev)) = run {
2915 if cur != Some(prev) {
2916 for &(sx0, sx1, c) in &prev.0[..prev.1] {
2917 if sx1 > sx0 && (c >> 24) & 0xFF != 0 {
2918 self.fill_signed(sx0, y0 + ks, sx1 - 1, y0 + k - 1, Color(c));
2919 }
2920 }
2921 run = None;
2922 }
2923 }
2924 if run.is_none() {
2925 if let Some(c) = cur {
2926 run = Some((k, c));
2927 }
2928 }
2929 }
2930 }
2931}
2932
2933#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2935pub enum CursorKind {
2936 Default,
2938 Text,
2940 Pointer,
2942 Grab,
2944 Crosshair,
2946 NotAllowed,
2948}
2949
2950impl CursorKind {
2951 pub fn from_css(name: &str) -> Self {
2953 match name.trim() {
2954 "pointer" => Self::Pointer,
2955 "text" => Self::Text,
2956 "grab" | "grabbing" => Self::Grab,
2957 "crosshair" => Self::Crosshair,
2958 "not-allowed" => Self::NotAllowed,
2959 _ => Self::Default,
2960 }
2961 }
2962}
2963
2964pub struct Mouse {
2966 pub x: u32,
2967 pub y: u32,
2968 pub is_clicked: bool,
2969 pub click_frame: u8,
2970 pub is_text_cursor: bool,
2972 pub cursor_kind: CursorKind,
2974}
2975
2976impl Mouse {
2977 pub const SIZE: u32 = 32;
2978
2979 pub const fn new(x: u32, y: u32) -> Self {
2980 Self {
2981 x,
2982 y,
2983 is_clicked: false,
2984 click_frame: 0,
2985 is_text_cursor: false,
2986 cursor_kind: CursorKind::Default,
2987 }
2988 }
2989
2990 pub fn draw(&self, screen: &Screen) {
2991 let (target_ptr, w, h) = screen.resolve_target();
2992 let stride = screen.resolve_stride();
2993 let cursor_size = Self::SIZE;
2994
2995 let (draw_x, draw_y) = if self.is_clicked {
2997 (self.x + 1, self.y + 1)
2998 } else {
2999 (self.x, self.y)
3000 };
3001
3002 if self.is_text_cursor || self.cursor_kind == CursorKind::Text {
3004 self.draw_text_cursor(screen, draw_x, draw_y);
3005 return;
3006 }
3007 match self.cursor_kind {
3008 CursorKind::Pointer => {
3009 self.draw_pointer_cursor(screen, draw_x, draw_y);
3010 return;
3011 }
3012 CursorKind::Crosshair => {
3013 self.draw_crosshair_cursor(screen, draw_x, draw_y);
3014 return;
3015 }
3016 CursorKind::NotAllowed => {
3017 self.draw_not_allowed_cursor(screen, draw_x, draw_y);
3018 return;
3019 }
3020 CursorKind::Grab => {
3021 self.draw_grab_cursor(screen, draw_x, draw_y);
3022 return;
3023 }
3024 CursorKind::Default | CursorKind::Text => {}
3025 }
3026
3027 if let Some(alpha_mask) = crate::os_lib::svg::get_or_rasterize(ICON_CURSOR_64, cursor_size)
3028 {
3029 let theme = crate::kernel::config::get_config().theme;
3030 let mut c_fill = theme.mouse_cursor;
3031 let c_stroke = theme.mouse_stroke;
3032
3033 if self.is_clicked {
3035 let r = ((c_fill >> 16) & 0xFF) * 8 / 10;
3036 let g = ((c_fill >> 8) & 0xFF) * 8 / 10;
3037 let b = (c_fill & 0xFF) * 8 / 10;
3038 c_fill = 0xFF000000 | (r << 16) | (g << 8) | b;
3039 }
3040
3041 for row in 0..cursor_size {
3042 let py = draw_y + row;
3043 if py >= h {
3044 continue;
3045 }
3046
3047 let max_col = (w.saturating_sub(draw_x)).min(cursor_size) as usize;
3048 if max_col == 0 {
3049 continue;
3050 }
3051
3052 let offset = (py * stride + draw_x) as usize;
3053 unsafe {
3054 let mut row_buf = [0u32; 32];
3055 let dst = target_ptr.add(offset);
3056
3057 let src = if !screen.back_buffer.is_null() {
3059 let src_offset = (py * w + draw_x) as usize;
3060 screen.back_buffer.add(src_offset)
3061 } else {
3062 dst
3063 };
3064
3065 core::ptr::copy_nonoverlapping(src, row_buf.as_mut_ptr(), max_col);
3066
3067 for col in 0..max_col {
3069 let mask_idx = (row * cursor_size + col as u32) as usize;
3070 let alpha = alpha_mask[mask_idx];
3071 if alpha > 0 {
3072 let mut is_edge = false;
3074 if row == 0
3075 || row == cursor_size - 1
3076 || col == 0
3077 || col as u32 == cursor_size - 1
3078 {
3079 is_edge = true;
3080 } else {
3081 'outer: for dy in -1..=1 {
3083 for dx in -1..=1 {
3084 let ny = row as i32 + dy;
3085 let nx = col as i32 + dx;
3086 let n_idx = (ny * cursor_size as i32 + nx) as usize;
3087 if alpha_mask[n_idx] < 80 {
3088 is_edge = true;
3089 break 'outer;
3090 }
3091 }
3092 }
3093 }
3094
3095 let bg = row_buf[col];
3096 let r_bg = (bg >> 16) & 0xFF;
3097 let g_bg = (bg >> 8) & 0xFF;
3098 let b_bg = bg & 0xFF;
3099
3100 let fg_color = if is_edge { c_stroke } else { c_fill };
3102 let r_fg = (fg_color >> 16) & 0xFF;
3103 let g_fg = (fg_color >> 8) & 0xFF;
3104 let b_fg = fg_color & 0xFF;
3105
3106 let a = alpha as u32;
3107 let inv_a = 255 - a;
3108
3109 let r = div_255_fast(r_fg * a + r_bg * inv_a);
3110 let g = div_255_fast(g_fg * a + g_bg * inv_a);
3111 let b = div_255_fast(b_fg * a + b_bg * inv_a);
3112
3113 row_buf[col] = (r << 16) | (g << 8) | b;
3114 }
3115 }
3116
3117 core::ptr::copy_nonoverlapping(row_buf.as_ptr(), dst, max_col);
3119 }
3120 }
3121
3122 if self.is_clicked && self.click_frame > 0 && self.click_frame < 8 {
3124 let spark_color = theme.mouse_cursor; let center_x = draw_x as i32;
3126 let center_y = draw_y as i32;
3127 let r1 = 4 + (self.click_frame as i32) * 2; let r2 = r1 + 4; let dirs = [
3132 (0, -1), (1, -1), (1, 0), (1, 1),
3133 (0, 1), (-1, 1), (-1, 0), (-1, -1)
3134 ];
3135
3136 for &(dx, dy) in dirs.iter() {
3137 for r in r1..=r2 {
3138 let px = center_x + dx * r;
3139 let py = center_y + dy * r;
3140 if px >= 0 && px < w as i32 && py >= 0 && py < h as i32 {
3141 let offset = (py as u32 * stride + px as u32) as usize;
3142 unsafe {
3143 let dst = target_ptr.add(offset);
3144 *dst = spark_color;
3145 }
3146 }
3147 }
3148 }
3149 }
3150 }
3151 }
3152
3153 fn draw_text_cursor(&self, screen: &Screen, draw_x: u32, draw_y: u32) {
3160 let theme = crate::kernel::config::get_config().theme;
3161 let color = Color(theme.mouse_cursor);
3162 let size = Self::SIZE;
3163 let bar_h = size.saturating_sub(4);
3164 let cx = draw_x + size / 2;
3165 let top_y = draw_y + 2;
3166 let bottom_y = top_y + bar_h;
3167 for dx in 0..=(size / 4) {
3169 screen.draw_pixel(cx.saturating_sub(size / 8) + dx, top_y, color);
3170 screen.draw_pixel(cx.saturating_sub(size / 8) + dx, bottom_y, color);
3171 }
3172 for y in top_y..=bottom_y {
3174 screen.draw_pixel(cx, y, color);
3175 }
3176 }
3177
3178 fn draw_pointer_cursor(&self, screen: &Screen, draw_x: u32, draw_y: u32) {
3180 #[rustfmt::skip]
3181 const POINTER_BITMAP: [&str; 22] = [
3182 "....#..............",
3183 "...#*#.............",
3184 "...#*#.............",
3185 "...#*#.............",
3186 "...#*#.............",
3187 "...#*#...#.........",
3188 "...#*#..#*#...#....",
3189 "...#*#..#*#..#*#...",
3190 "..##*#..#*#..#*#.#.",
3191 ".#**#*##*#*##*#*#*#",
3192 "#***#*#**#**#*#*#*#",
3193 "#****#**********#*#",
3194 ".#***************#.",
3195 "..#**************#.",
3196 "..#**************#.",
3197 "...#*************#.",
3198 "...#*************#.",
3199 "....#***********#..",
3200 ".....#*********#...",
3201 "......#*******#....",
3202 ".......#*****#.....",
3203 "........#####......",
3204 ];
3205 let theme = crate::kernel::config::get_config().theme;
3206 let stroke = Color(theme.mouse_stroke);
3207 let fill = Color(theme.mouse_cursor);
3208 Self::draw_bitmap_pattern(screen, draw_x, draw_y, &POINTER_BITMAP, stroke, fill);
3209 }
3210
3211 fn draw_crosshair_cursor(&self, screen: &Screen, draw_x: u32, draw_y: u32) {
3213 #[rustfmt::skip]
3214 const CROSSHAIR_BITMAP: [&str; 15] = [
3215 ".......#.......",
3216 ".......*.......",
3217 ".......*.......",
3218 ".......*.......",
3219 ".......#.......",
3220 "...............",
3221 ".###.......###.",
3222 "#***#.....#***#",
3223 ".###.......###.",
3224 "...............",
3225 ".......#.......",
3226 ".......*.......",
3227 ".......*.......",
3228 ".......*.......",
3229 ".......#.......",
3230 ];
3231 let theme = crate::kernel::config::get_config().theme;
3232 let stroke = Color(theme.mouse_stroke);
3233 let fill = Color(theme.mouse_cursor);
3234 Self::draw_bitmap_pattern(screen, draw_x + 8, draw_y + 8, &CROSSHAIR_BITMAP, stroke, fill);
3235 }
3236
3237 fn draw_not_allowed_cursor(&self, screen: &Screen, draw_x: u32, draw_y: u32) {
3239 #[rustfmt::skip]
3240 const NOT_ALLOWED_BITMAP: [&str; 16] = [
3241 ".....#####.....",
3242 "...##*****##...",
3243 "..#*#*****#*#..",
3244 ".#***#****#**#.",
3245 ".#****#***#**#.",
3246 "#******#**#***#",
3247 "#*******#*#***#",
3248 "#********##***#",
3249 "#***##********#",
3250 "#***#*#*******#",
3251 "#***#**#******#",
3252 ".#**#***#****#.",
3253 ".#**#****#***#.",
3254 "..#*#*****#*#..",
3255 "...##*****##...",
3256 ".....#####.....",
3257 ];
3258 let stroke = Color(0xFF000000);
3259 let fill = Color(0xFFFF4444); Self::draw_bitmap_pattern(screen, draw_x + 8, draw_y + 8, &NOT_ALLOWED_BITMAP, stroke, fill);
3261 }
3262
3263 fn draw_grab_cursor(&self, screen: &Screen, draw_x: u32, draw_y: u32) {
3265 #[rustfmt::skip]
3266 const GRAB_BITMAP: [&str; 17] = [
3267 "...#...#...#.....",
3268 "..#*#.#*#.#*#....",
3269 "..#*#.#*#.#*#.#..",
3270 "..#*#.#*#.#*#.#*.#",
3271 "..#*#.#*#.#*#.#*#",
3272 ".#*#*#*#*#*#*#*#.",
3273 "#***************#",
3274 "#***************#",
3275 ".#**************#",
3276 "..#*************#",
3277 "..#*************#",
3278 "...#************#",
3279 "...#***********#.",
3280 "....#*********#..",
3281 ".....#*******#...",
3282 "......#*****#....",
3283 ".......#####.....",
3284 ];
3285 let theme = crate::kernel::config::get_config().theme;
3286 let stroke = Color(theme.mouse_stroke);
3287 let fill = Color(theme.mouse_cursor);
3288 Self::draw_bitmap_pattern(screen, draw_x + 4, draw_y + 4, &GRAB_BITMAP, stroke, fill);
3289 }
3290
3291 fn draw_bitmap_pattern(
3293 screen: &Screen,
3294 draw_x: u32,
3295 draw_y: u32,
3296 bitmap: &[&str],
3297 stroke_color: Color,
3298 fill_color: Color,
3299 ) {
3300 for (row_idx, row_str) in bitmap.iter().enumerate() {
3301 let py = draw_y + row_idx as u32;
3302 if py >= screen.height {
3303 continue;
3304 }
3305 for (col_idx, ch) in row_str.chars().enumerate() {
3306 let px = draw_x + col_idx as u32;
3307 if px >= screen.width {
3308 continue;
3309 }
3310 match ch {
3311 '#' => screen.draw_pixel(px, py, stroke_color),
3312 '*' => screen.draw_pixel(px, py, fill_color),
3313 _ => {}
3314 }
3315 }
3316 }
3317 }
3318
3319 pub fn move_to(&mut self, screen: &Screen, new_x: u32, new_y: u32) {
3321 self.x = new_x.clamp(0, screen.width - 1);
3322 self.y = new_y.clamp(0, screen.height - 1);
3323 }
3324}
3325
3326pub static mut SPLASH_ACTIVE: bool = false;
3330pub static LOGO_PNG: &[u8] = include_bytes!("../../images/os_logo.png");
3331
3332static mut BOOT_LOG_COUNT: usize = 0;
3333static mut BOOT_LOG_BUFFER: [[u8; 128]; 16] = [[0; 128]; 16];
3334static mut BOOT_LOG_LENS: [usize; 16] = [0; 16];
3335
3336static mut CURRENT_LINE_BUFFER: [u8; 256] = [0; 256];
3337static mut CURRENT_LINE_LEN: usize = 0;
3338
3339pub unsafe fn write_splash_char(c: u8) {
3343 if !SPLASH_ACTIVE {
3344 return;
3345 }
3346 if CURRENT_LINE_LEN > 255 {
3349 CURRENT_LINE_LEN = 0;
3350 }
3351 if c == b'\n' {
3352 if CURRENT_LINE_LEN > 0 {
3353 if let Ok(s) = core::str::from_utf8(&CURRENT_LINE_BUFFER[..CURRENT_LINE_LEN.min(255)]) {
3354 add_boot_log(s);
3355 }
3356 CURRENT_LINE_LEN = 0;
3357 }
3358 } else {
3359 if CURRENT_LINE_LEN < 255 {
3360 CURRENT_LINE_BUFFER[CURRENT_LINE_LEN] = c;
3361 CURRENT_LINE_LEN += 1;
3362 }
3363 }
3364}
3365
3366pub unsafe fn write_splash_str(s: &str) {
3370 if !SPLASH_ACTIVE {
3371 return;
3372 }
3373 for &b in s.as_bytes() {
3374 write_splash_char(b);
3375 }
3376}
3377
3378pub unsafe fn add_boot_log(s: &str) {
3382 if !SPLASH_ACTIVE {
3383 return;
3384 }
3385 const MAX_LINE_CHARS: usize = 55;
3386
3387 for line in s.lines() {
3388 let line = line.trim_end();
3389 if line.is_empty() {
3390 continue;
3391 }
3392
3393 let mut rest = line;
3396 while !rest.is_empty() {
3397 let mut cols = 0usize;
3398 let mut cut = rest.len();
3399 for (i, c) in rest.char_indices() {
3400 let w = if (c as u32) < 128 { 1 } else { 2 };
3401 if cols + w > MAX_LINE_CHARS || i + c.len_utf8() > 128 {
3402 cut = i;
3403 break;
3404 }
3405 cols += w;
3406 }
3407 let cut = cut.max(rest.chars().next().map_or(rest.len(), char::len_utf8));
3408 let (head, tail) = rest.split_at_checked(cut).unwrap_or((rest, ""));
3409 let chunk = head.as_bytes();
3410 let chunk_len = chunk.len();
3411 rest = tail;
3412
3413 if BOOT_LOG_COUNT < 16 {
3414 let idx = BOOT_LOG_COUNT;
3415 BOOT_LOG_BUFFER[idx][..chunk_len].copy_from_slice(chunk);
3416 BOOT_LOG_LENS[idx] = chunk_len;
3417 BOOT_LOG_COUNT += 1;
3418 } else {
3419 for i in 0..15 {
3420 BOOT_LOG_BUFFER[i] = BOOT_LOG_BUFFER[i + 1];
3421 BOOT_LOG_LENS[i] = BOOT_LOG_LENS[i + 1];
3422 }
3423 BOOT_LOG_BUFFER[15][..chunk_len].copy_from_slice(chunk);
3424 BOOT_LOG_LENS[15] = chunk_len;
3425 }
3426 }
3427 }
3428 redraw_splash();
3429}
3430
3431pub unsafe fn draw_splash_logo() {
3435 if !SPLASH_ACTIVE {
3436 return;
3437 }
3438 if let Some(ref screen) = crate::CURRENT_SCREEN {
3439 screen.clear(Color(0xFF000000));
3440 let mut decoder = zune_png::PngDecoder::new(LOGO_PNG);
3441 if decoder.decode_raw().is_ok() {
3442 if let Some(info) = decoder.get_info() {
3443 let img_w = info.width as u32;
3444 let img_h = info.height as u32;
3445 let area_w = 480u32;
3446 let area_h = screen.height;
3447 let target_w = area_w.min(img_w);
3448 let target_h = (target_w * img_h) / img_w;
3449 let target_h = target_h.min(area_h);
3450 let target_w = (target_h * img_w) / img_h;
3451
3452 let x = (area_w - target_w) / 2 + 20;
3453 let y = (screen.height - target_h) / 2;
3454 if let Err(e) = screen.draw_image(x, y, target_w, target_h, LOGO_PNG) {
3455 crate::warn!("[GPU] スプラッシュロゴ描画失敗: {}", e);
3456 }
3457 }
3458 }
3459 redraw_splash();
3460 }
3461}
3462
3463pub unsafe fn redraw_splash() {
3467 if !SPLASH_ACTIVE {
3468 return;
3469 }
3470 if let Some(ref screen) = crate::CURRENT_SCREEN {
3471 let area_x = 520u32;
3472 let area_y = 20u32;
3473 let area_w = screen.width.saturating_sub(area_x).saturating_sub(20);
3474 let area_h = screen.height.saturating_sub(area_y).saturating_sub(20);
3475 screen.boxfill(
3476 area_x,
3477 area_y,
3478 area_x + area_w,
3479 area_y + area_h,
3480 Color(0xFF000000),
3481 );
3482
3483 let start_y = (screen.height.saturating_sub(40)) as i32;
3484 let line_height = 28i32;
3485
3486 for i in 0..BOOT_LOG_COUNT {
3487 let y = start_y - (BOOT_LOG_COUNT - 1 - i) as i32 * line_height;
3488 if y >= area_y as i32 {
3489 if let Ok(s) = core::str::from_utf8(&BOOT_LOG_BUFFER[i][..BOOT_LOG_LENS[i]]) {
3490 screen.draw_string_monospace(area_x + 10, y as u32, s, Color(0xFFCCCCCC));
3491 }
3492 }
3493 }
3494 }
3495}
3496
3497pub struct SplashWriter;
3498
3499impl core::fmt::Write for SplashWriter {
3500 fn write_str(&mut self, s: &str) -> core::fmt::Result {
3501 unsafe {
3502 write_splash_str(s);
3503 }
3504 Ok(())
3505 }
3506}