feat(display-server): add VirtioFramebuffer adapter for QEMU
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
pub mod mock;
|
pub mod mock;
|
||||||
|
pub mod qemu_virtio;
|
||||||
|
|||||||
81
crates/servers/display-server/src/adapters/qemu_virtio.rs
Normal file
81
crates/servers/display-server/src/adapters/qemu_virtio.rs
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
use crate::framebuffer::Framebuffer;
|
||||||
|
|
||||||
|
const VIRTIO_GPU_BASE: usize = 0x10008000;
|
||||||
|
const FB_WIDTH: u16 = 640;
|
||||||
|
const FB_HEIGHT: u16 = 480;
|
||||||
|
const FB_BPP: usize = 4;
|
||||||
|
|
||||||
|
pub struct VirtioFramebuffer {
|
||||||
|
buffer: &'static mut [u8],
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
stride: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VirtioFramebuffer {
|
||||||
|
pub fn new(buffer_addr: usize) -> Self {
|
||||||
|
let stride = FB_WIDTH as usize * FB_BPP;
|
||||||
|
let size = stride * FB_HEIGHT as usize;
|
||||||
|
let buffer = unsafe { core::slice::from_raw_parts_mut(buffer_addr as *mut u8, size) };
|
||||||
|
|
||||||
|
for byte in buffer.iter_mut() {
|
||||||
|
*byte = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self {
|
||||||
|
buffer,
|
||||||
|
width: FB_WIDTH,
|
||||||
|
height: FB_HEIGHT,
|
||||||
|
stride,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Framebuffer for VirtioFramebuffer {
|
||||||
|
fn width(&self) -> u16 {
|
||||||
|
self.width
|
||||||
|
}
|
||||||
|
|
||||||
|
fn height(&self) -> u16 {
|
||||||
|
self.height
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_pixel(&mut self, x: u16, y: u16, color: u32) {
|
||||||
|
if x >= self.width || y >= self.height {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let offset = (y as usize) * self.stride + (x as usize) * FB_BPP;
|
||||||
|
let bytes = color.to_le_bytes();
|
||||||
|
self.buffer[offset] = bytes[0];
|
||||||
|
self.buffer[offset + 1] = bytes[1];
|
||||||
|
self.buffer[offset + 2] = bytes[2];
|
||||||
|
self.buffer[offset + 3] = bytes[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_rect(&mut self, x: u16, y: u16, w: u16, h: u16, color: u32) {
|
||||||
|
for dy in 0..h {
|
||||||
|
for dx in 0..w {
|
||||||
|
self.write_pixel(x + dx, y + dy, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear(&mut self, color: u32) {
|
||||||
|
let bytes = color.to_le_bytes();
|
||||||
|
for y in 0..self.height as usize {
|
||||||
|
for x in 0..self.width as usize {
|
||||||
|
let offset = y * self.stride + x * FB_BPP;
|
||||||
|
self.buffer[offset] = bytes[0];
|
||||||
|
self.buffer[offset + 1] = bytes[1];
|
||||||
|
self.buffer[offset + 2] = bytes[2];
|
||||||
|
self.buffer[offset + 3] = bytes[3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) {
|
||||||
|
// VirtIO GPU flush would send TRANSFER_TO_HOST_2D + RESOURCE_FLUSH
|
||||||
|
// For now, the buffer is memory-mapped — no explicit flush needed
|
||||||
|
// when using ramfb or a simple scanout
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user