mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2025-01-18 21:17:14 +04:00
clean os/user codes for graph show
This commit is contained in:
parent
7522d056ba
commit
9d59a8271f
@ -1,8 +1,5 @@
|
||||
use crate::drivers::bus::virtio::VirtioHal;
|
||||
use crate::{
|
||||
gui::{move_rect, reset},
|
||||
sync::UPIntrFreeCell,
|
||||
};
|
||||
use crate::sync::UPIntrFreeCell;
|
||||
use alloc::sync::Arc;
|
||||
use core::any::Any;
|
||||
use virtio_drivers::{VirtIOHeader, VirtIOInput};
|
||||
@ -49,21 +46,6 @@ impl InputDevice for VirtIOInputWrapper {
|
||||
virtio_input_decoder::DecodeType::Key(key, r#type) => {
|
||||
if r#type == KeyType::Press {
|
||||
match key {
|
||||
Key::C | Key::MouseLeft => {
|
||||
reset();
|
||||
}
|
||||
Key::W => {
|
||||
move_rect(0, -10);
|
||||
}
|
||||
Key::S => {
|
||||
move_rect(0, 10);
|
||||
}
|
||||
Key::A => {
|
||||
move_rect(-10, 0);
|
||||
}
|
||||
Key::D => {
|
||||
move_rect(10, 0);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -1,67 +0,0 @@
|
||||
use alloc::sync::Arc;
|
||||
use embedded_graphics::{
|
||||
draw_target::DrawTarget,
|
||||
pixelcolor::Rgb888,
|
||||
prelude::{OriginDimensions, Point, RgbColor, Size},
|
||||
};
|
||||
|
||||
use crate::board::VIRTGPU_XRES;
|
||||
use crate::drivers::{GpuDevice, GPU_DEVICE};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Graphics {
|
||||
pub size: Size,
|
||||
pub point: Point,
|
||||
pub drv: Arc<dyn GpuDevice>,
|
||||
}
|
||||
|
||||
impl Graphics {
|
||||
pub fn new(size: Size, point: Point) -> Self {
|
||||
Self {
|
||||
size,
|
||||
point,
|
||||
drv: GPU_DEVICE.clone(),
|
||||
}
|
||||
}
|
||||
pub fn reset(&self) {
|
||||
let fb = self.drv.get_framebuffer();
|
||||
fb.fill(0u8);
|
||||
}
|
||||
|
||||
pub fn get_framebuffer(&self)-> &mut [u8] {
|
||||
self.drv.get_framebuffer()
|
||||
}
|
||||
}
|
||||
|
||||
impl OriginDimensions for Graphics {
|
||||
fn size(&self) -> Size {
|
||||
self.size
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawTarget for Graphics {
|
||||
type Color = Rgb888;
|
||||
|
||||
type Error = core::convert::Infallible;
|
||||
|
||||
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
|
||||
where
|
||||
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
|
||||
{
|
||||
let fb = self.drv.get_framebuffer();
|
||||
|
||||
pixels.into_iter().for_each(|px| {
|
||||
let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x)
|
||||
as usize
|
||||
* 4;
|
||||
if idx + 2 >= fb.len() {
|
||||
return;
|
||||
}
|
||||
fb[idx] = px.1.b();
|
||||
fb[idx + 1] = px.1.g();
|
||||
fb[idx + 2] = px.1.r();
|
||||
});
|
||||
self.drv.flush();
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
mod graphic;
|
||||
mod paint;
|
||||
|
||||
use graphic::Graphics;
|
||||
pub use paint::{init_paint, move_rect, reset};
|
@ -1,69 +0,0 @@
|
||||
use super::Graphics;
|
||||
use crate::sync::UPIntrFreeCell;
|
||||
use embedded_graphics::pixelcolor::Rgb888;
|
||||
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
|
||||
use embedded_graphics::primitives::Primitive;
|
||||
use embedded_graphics::primitives::{PrimitiveStyle, Rectangle};
|
||||
use lazy_static::*;
|
||||
|
||||
const INIT_X: i32 = 640;
|
||||
const INIT_Y: i32 = 400;
|
||||
const RECT_SIZE: u32 = 40;
|
||||
|
||||
pub struct DrawingBoard {
|
||||
graphics: Graphics,
|
||||
latest_pos: Point,
|
||||
}
|
||||
|
||||
impl DrawingBoard {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
graphics: Graphics::new(Size::new(1280, 800), Point::new(0, 0)),
|
||||
latest_pos: Point::new(INIT_X, INIT_Y),
|
||||
}
|
||||
}
|
||||
fn paint(&mut self) {
|
||||
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
|
||||
.into_styled(PrimitiveStyle::with_stroke(Rgb888::WHITE, 1))
|
||||
.draw(&mut self.graphics)
|
||||
.ok();
|
||||
}
|
||||
fn unpaint(&mut self) {
|
||||
Rectangle::with_center(self.latest_pos, Size::new(RECT_SIZE, RECT_SIZE))
|
||||
.into_styled(PrimitiveStyle::with_stroke(Rgb888::BLACK, 1))
|
||||
.draw(&mut self.graphics)
|
||||
.ok();
|
||||
}
|
||||
pub fn move_rect(&mut self, dx: i32, dy: i32) {
|
||||
self.unpaint();
|
||||
self.latest_pos.x += dx;
|
||||
self.latest_pos.y += dy;
|
||||
self.paint();
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
self.latest_pos = Point::new(INIT_X, INIT_Y);
|
||||
self.graphics.reset();
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref DRAWING_BOARD: UPIntrFreeCell<DrawingBoard> = unsafe { UPIntrFreeCell::new(DrawingBoard::new()) };
|
||||
}
|
||||
|
||||
pub fn init_paint() {
|
||||
DRAWING_BOARD.exclusive_session(|ripple| {
|
||||
ripple.paint();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn move_rect(dx: i32, dy: i32) {
|
||||
DRAWING_BOARD.exclusive_session(|ripple| {
|
||||
ripple.move_rect(dx, dy);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn reset() {
|
||||
DRAWING_BOARD.exclusive_session(|ripple| {
|
||||
ripple.reset();
|
||||
});
|
||||
}
|
@ -18,7 +18,6 @@ mod console;
|
||||
mod config;
|
||||
mod drivers;
|
||||
mod fs;
|
||||
mod gui;
|
||||
mod lang_items;
|
||||
mod mm;
|
||||
mod sbi;
|
||||
@ -70,7 +69,7 @@ pub fn rust_main() -> ! {
|
||||
timer::set_next_trigger();
|
||||
board::device_init();
|
||||
fs::list_apps();
|
||||
gui::init_paint();
|
||||
//gui::init_paint();
|
||||
task::add_initproc();
|
||||
*DEV_NON_BLOCKING_ACCESS.exclusive_access() = true;
|
||||
task::run_tasks();
|
||||
|
@ -8,7 +8,6 @@ extern crate user_lib;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::sync::Arc;
|
||||
use embedded_graphics::pixelcolor::Rgb888;
|
||||
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
|
||||
use embedded_graphics::primitives::Primitive;
|
||||
@ -23,11 +22,6 @@ const INIT_X: i32 = 640;
|
||||
const INIT_Y: i32 = 400;
|
||||
const RECT_SIZE: u32 = 40;
|
||||
|
||||
// lazy_static::lazy_static! {
|
||||
// pub static ref FB: Arc<Display> = Arc::new(Display::new(Size::new(VIRTGPU_XRES as u32, VIRTGPU_YRES as u32), Point::new(INIT_X, INIT_Y)));
|
||||
// }
|
||||
|
||||
//#[derive(Clone)]
|
||||
pub struct Display {
|
||||
pub size: Size,
|
||||
pub point: Point,
|
||||
@ -42,15 +36,10 @@ impl Display {
|
||||
"Hello world from user mode program! 0x{:X} , len {}",
|
||||
fb_ptr as usize, VIRTGPU_LEN
|
||||
);
|
||||
// let fb =
|
||||
// unsafe { Arc::new(core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize)) };
|
||||
let fb= unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) };
|
||||
let fb =
|
||||
unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) };
|
||||
Self { size, point, fb }
|
||||
}
|
||||
// pub fn reset(&self) {
|
||||
// let fb = self.drv.get_framebuffer();
|
||||
// fb.fill(0u8);
|
||||
// }
|
||||
}
|
||||
|
||||
impl OriginDimensions for Display {
|
||||
@ -68,9 +57,6 @@ impl DrawTarget for Display {
|
||||
where
|
||||
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
|
||||
{
|
||||
//let fb = self.fb.clone();
|
||||
//let fb = self.fb;
|
||||
//let mut arc_data_mut = Arc::make_mut(fb);
|
||||
pixels.into_iter().for_each(|px| {
|
||||
let idx = ((self.point.y + px.0.y) * VIRTGPU_XRES as i32 + self.point.x + px.0.x)
|
||||
as usize
|
||||
@ -117,59 +103,16 @@ impl DrawingBoard {
|
||||
self.latest_pos.y += dy;
|
||||
self.paint();
|
||||
}
|
||||
|
||||
// pub fn reset(&mut self) {
|
||||
// self.latest_pos = Point::new(INIT_X, INIT_Y);
|
||||
// self.disp.reset();
|
||||
// }
|
||||
}
|
||||
|
||||
// lazy_static! {
|
||||
// pub static ref DRAWING_BOARD: UPIntrFreeCell<DrawingBoard> =
|
||||
// unsafe { UPIntrFreeCell::new(DrawingBoard::new()) };
|
||||
// }
|
||||
|
||||
// pub fn init_paint() {
|
||||
// DRAWING_BOARD.exclusive_session(|ripple| {
|
||||
// ripple.paint();
|
||||
// });
|
||||
// }
|
||||
|
||||
// pub fn move_rect(dx: i32, dy: i32) {
|
||||
// DRAWING_BOARD.exclusive_session(|ripple| {
|
||||
// ripple.move_rect(dx, dy);
|
||||
// });
|
||||
// }
|
||||
|
||||
// pub fn reset() {
|
||||
// DRAWING_BOARD.exclusive_session(|ripple| {
|
||||
// ripple.reset();
|
||||
// });
|
||||
// }
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
// let fb_ptr = framebuffer() as *mut u8;
|
||||
let mut board=DrawingBoard::new();
|
||||
board.paint();
|
||||
for i in 0..100 {
|
||||
let mut board = DrawingBoard::new();
|
||||
for i in 0..20 {
|
||||
board.latest_pos.x += i;
|
||||
board.latest_pos.y += i;
|
||||
board.paint();
|
||||
board.paint();
|
||||
}
|
||||
// println!(
|
||||
// "Hello world from user mode program! 0x{:X} , len {}",
|
||||
// fb_ptr as usize, VIRTGPU_LEN
|
||||
// );
|
||||
// let fb = unsafe { core::slice::from_raw_parts_mut(fb_ptr as *mut u8, VIRTGPU_LEN as usize) };
|
||||
// for y in 0..800 {
|
||||
// for x in 0..1280 {
|
||||
// let idx = (y * 1280 + x) * 4;
|
||||
// fb[idx] = x as u8;
|
||||
// fb[idx + 1] = y as u8;
|
||||
// fb[idx + 2] = (x + y) as u8;
|
||||
// }
|
||||
// }
|
||||
// framebuffer_flush();
|
||||
0
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user