ableos/kernel/src/arch/x86_64/graphics.rs

57 lines
1.7 KiB
Rust

use virtio_drivers::transport::Transport;
use crate::arch::virtio::AbleosHal;
use {embedded_graphics::pixelcolor::Rgb888, virtio_drivers::device::gpu::VirtIOGpu};
use {
able_graphics_library::raw_pixel::Display,
embedded_graphics::prelude::*,
limine::FramebufferRequest,
spin::{Lazy, Mutex},
};
pub static DISPLAY: Lazy<Mutex<Display>> = Lazy::new(|| {
static FB_REQ: FramebufferRequest = FramebufferRequest::new(0);
let fb1 = &FB_REQ.get_response().get().unwrap().framebuffers()[0];
let m = Mutex::new(Display {
fb: fb1.address.as_ptr().unwrap().cast(),
size: Size::new(fb1.width as u32, fb1.height as u32),
color: Rgb888::WHITE,
});
log::info!("Graphics initialised");
m
});
pub fn init() {
Lazy::force(&DISPLAY);
}
pub fn virtio_gpu<T: Transport>(transport: T) {
let mut gpu = VirtIOGpu::<AbleosHal, T>::new(transport).expect("failed to create gpu driver");
let (width, height) = gpu.resolution().expect("failed to get resolution");
let width = width as usize;
let height = height as usize;
log::info!("GPU resolution is {}x{}", width, height);
let fb = gpu.setup_framebuffer().expect("failed to get fb");
for y in 0..height {
for x in 0..width {
let idx = (y * width + x) * 4;
fb[idx] = x as u8;
fb[idx + 1] = y as u8;
fb[idx + 2] = (x + y) as u8;
}
}
gpu.flush().expect("failed to flush");
//delay some time
log::info!("virtio-gpu show graphics....");
for _ in 0..100000 {
for _ in 0..100000 {
unsafe {
core::arch::asm!("nop");
}
}
}
log::info!("virtio-gpu test finished");
}