[gpu] Add GPU impl

This commit is contained in:
2024-02-19 09:09:21 +05:30
parent 61c05516b5
commit a7d55cb2b8
2 changed files with 31 additions and 16 deletions

View File

@@ -1,31 +1,46 @@
use std::cell::RefCell;
use crate::graphics::graphics_adapter::GraphicsAdapter;
use crate::misc::emulator_error::DeviceType::GRAPHICS;
use crate::misc::emulator_error::EmulatorError;
use crate::misc::result::EmulatorResult;
pub const DEVICE_FRAMEBUFFER_SIZE: usize = 256 * 256;
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct GraphicsProcessor {
framebuffer: Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>,
frame_buffer: RefCell<Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>>,
graphics_adapter: Box<dyn GraphicsAdapter>
}
/// Abstracted graphics processor. Refer GraphicsAdapter
/// Abstracted graphics processor. Calls `[GraphicsAdapter]`
impl GraphicsProcessor {
pub fn try_new() -> EmulatorResult<GraphicsProcessor> {
pub fn try_new(graphics_adapter: Box<dyn GraphicsAdapter>) -> EmulatorResult<GraphicsProcessor> {
let framebuffer = vec![0; DEVICE_FRAMEBUFFER_SIZE].into_boxed_slice()
.try_into()
.map_err(|_| {
EmulatorError::AllocationFailure(GRAPHICS, "Failed to allocate graphics")
})?;
Ok(GraphicsProcessor {
framebuffer
frame_buffer: RefCell::new(framebuffer),
graphics_adapter
})
}
pub fn set_framebuffer(&mut self, memory_slice: &[u8]) {
self.framebuffer.copy_from_slice(memory_slice);
/// take a copy of FB and
pub fn draw(&self, memory_slice: &[u8;DEVICE_FRAMEBUFFER_SIZE])->EmulatorResult<()>{
self.set_framebuffer(memory_slice);
let fb_immut = self.frame_buffer.borrow();
self.graphics_adapter.draw(fb_immut)
}
pub fn get_framebuffer(&self) -> &[u8; DEVICE_FRAMEBUFFER_SIZE] {
&self.framebuffer
fn set_framebuffer(&self, memory_slice: &[u8;DEVICE_FRAMEBUFFER_SIZE]) {
let mut fb = self.frame_buffer.borrow_mut();
fb.copy_from_slice(memory_slice);
}
pub fn get_framebuffer(&self) -> Box<[u8; DEVICE_FRAMEBUFFER_SIZE]> {
self.frame_buffer.borrow().clone()
}
}
#[cfg(test)]
mod test{
}

View File

@@ -1,17 +1,17 @@
use crate::emu::graphics::{DEVICE_FRAMEBUFFER_SIZE, GraphicsProcessor};
use std::cell::Ref;
use std::fmt::Debug;
use crate::misc::result::EmulatorResult;
pub trait GraphicsAdapter{
fn draw(&mut self,frame_buf:&[u8;DEVICE_FRAMEBUFFER_SIZE])->EmulatorResult<()>;
pub trait GraphicsAdapter: Debug {
fn draw(&self, frame_buf: Ref<Box<[u8; 65536]>>) -> EmulatorResult<()>;
}
#[derive(Debug, Clone)]
pub struct SDLGraphicsAdapter {
graphics_processor: GraphicsProcessor
}
impl GraphicsAdapter for SDLGraphicsAdapter {
fn draw(&mut self, frame_buf: &[u8; DEVICE_FRAMEBUFFER_SIZE]) -> EmulatorResult<()> {
fn draw(&self, frame_buffer: Ref<Box<[u8; 65536]>>) -> EmulatorResult<()> {
todo!()
}
}