Commit cdf9d19d authored by robbycerantola's avatar robbycerantola

image_fast benchmark

parent f9e8df2c
......@@ -14,8 +14,8 @@ keywords = [
]
[dependencies]
rayon = "0.9"
time = "*"
[features]
default = []
no_std = []
......
......@@ -18,35 +18,30 @@ fn main() {
window.set(Color::rgb(255,255,255));
//create image data : a green square
let data = vec![Color::rgba(100,200,10,2);200000];
let data2 = vec![Color::rgba(200,100,10,2);200000];
let data = vec![Color::rgba(100,200,10,20);200000];
let data2 = vec![Color::rgba(200,100,10,20);200000];
//draw image sequentially
//draw image benchmarking
println!("Benchmarking implementations to draw an image on window:");
let mut t = time::now();
for _i in 1..TIMES {
window.image(10,10,500,400, &data[..]);
}
println!("image {:?}", time::now()-t);
//draw image parallelizing
t = time::now();
for _i in 1..TIMES {
window.image_par(20,20,500,400, &data[..]);
}
println!("image_par {:?}", time::now()-t);
t = time::now();
for _i in 1..TIMES {
window.image_fast(30,30,500,400, &data[..]);
for _i in 0..TIMES {
window.image(10,10,500,400, &data[..]);
}
println!("image_fast {:?}", time::now()-t);
let mut t2 = time::now();
let dt = (t2-t)/TIMES;
println!(" image {:?}",dt );
t = time::now();
for _i in 1..TIMES {
window.image_veryfast(40,40,500,400, &data2[..]);
for _i in 0..TIMES {
window.image_fast(40,40,500,400, &data2[..]);
}
println!("image_veryfast{:?}", time::now()-t);
t2 = time::now();
let dt2 = (t2-t)/TIMES;
println!("image_fast {:?}",dt2);
println!("-------------------------");
println!("difference {:?}", dt-dt2);
window.sync();
'events: loop {
......
......@@ -12,8 +12,6 @@ extern crate alloc;
#[cfg(not(feature="no_std"))]
extern crate core;
extern crate rayon;
pub static FONT: &'static [u8] = include_bytes!("../res/unifont.font");
pub use color::Color;
......
use core::cmp;
use rayon::prelude::*;
use FONT;
use color::Color;
use graphicspath::GraphicsPath;
use graphicspath::PointType;
use std::sync::{Arc, Mutex};
#[cfg(target_arch = "x86")]
#[inline(always)]
#[cold]
......@@ -310,96 +307,11 @@ pub trait Renderer {
}
}
//VERY SLOW parallel implementation !
fn image_par(&mut self, start_x: i32, start_y: i32, w: u32, _h: u32, image_data: &[Color]) {
let window_w = self.width() as usize;
let window_len = self.data().len();
let data = Arc::new(Mutex::new(self.data_mut()));
let w = w as usize;
(0..image_data.len())
.into_par_iter()
.map(|i| {
let y0 = i/w;
let y = y0 + start_y as usize ;
let x = start_x as usize + i-(y0*w) ;
let window_index = y * window_w + x;
if window_index <= window_len {
let new = image_data[i].data;
let alpha = (new >> 24) & 0xFF;
if alpha > 0 {
let old = unsafe{ &mut data.lock().unwrap()[window_index].data};
if alpha >= 255 {
*old = new;
} else {
let n_r = (((new >> 16) & 0xFF) * alpha) >> 8;
let n_g = (((new >> 8) & 0xFF) * alpha) >> 8;
let n_b = ((new & 0xFF) * alpha) >> 8;
let n_alpha = 255 - alpha;
let o_a = (((*old >> 24) & 0xFF) * n_alpha) >> 8;
let o_r = (((*old >> 16) & 0xFF) * n_alpha) >> 8;
let o_g = (((*old >> 8) & 0xFF) * n_alpha) >> 8;
let o_b = ((*old & 0xFF) * n_alpha) >> 8;
*old = ((o_a << 24) | (o_r << 16) | (o_g << 8) | o_b) + ((alpha << 24) | (n_r << 16) | (n_g << 8) | n_b);
}
}
}
})
.count();
}
fn image_fast (&mut self, start_x: i32, start_y: i32, w: u32, _h: u32, image_data: &[Color]) {
let window_w = self.width() as usize;
let window_len = self.data().len();
let data = self.data_mut();
let w = w as usize;
(0..image_data.len())
.map(|i| {
let y0 = i/w;
let y = y0 + start_y as usize ;
let x = start_x as usize + i-(y0*w) ;
let window_index = y * window_w + x;
if window_index <= window_len {
let new = image_data[i].data;
let alpha = (new >> 24) & 0xFF;
if alpha > 0 {
let old = unsafe{ &mut data[window_index].data};
if alpha >= 255 {
*old = new;
} else {
let n_r = (((new >> 16) & 0xFF) * alpha) >> 8;
let n_g = (((new >> 8) & 0xFF) * alpha) >> 8;
let n_b = ((new & 0xFF) * alpha) >> 8;
let n_alpha = 255 - alpha;
let o_a = (((*old >> 24) & 0xFF) * n_alpha) >> 8;
let o_r = (((*old >> 16) & 0xFF) * n_alpha) >> 8;
let o_g = (((*old >> 8) & 0xFF) * n_alpha) >> 8;
let o_b = ((*old & 0xFF) * n_alpha) >> 8;
*old = ((o_a << 24) | (o_r << 16) | (o_g << 8) | o_b) + ((alpha << 24) | (n_r << 16) | (n_g << 8) | n_b);
}
}
}
})
.count();
}
fn image_veryfast (&mut self, start_x: i32, start_y: i32, w: u32, _h: u32, image_data: &[Color]) {
let window_w = self.width() as usize;
let window_len = self.data().len();
let data = self.data_mut();
let w = w as usize;
let start_x = start_x as usize;
let start_y = start_y as usize;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment