From e2768e75591d3b4666008d83d5d6281ed793b952 Mon Sep 17 00:00:00 2001 From: Matthias Gabriel Date: Tue, 13 Feb 2024 06:20:34 +0100 Subject: [PATCH] improve rendering performance a bit --- src/renderer.rs | 25 +++++++++++++++++++------ src/sketch_board.rs | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/renderer.rs b/src/renderer.rs index b2e4b52..be523aa 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -14,20 +14,33 @@ use crate::tools::Drawable; use crate::tools::Tool; pub struct Renderer { - original_image: Pixbuf, + original_image: ImageSurface, crop_tool: Rc>, drawables: Vec>, redo_stack: Vec>, } impl Renderer { - pub fn new(original_image: Pixbuf, crop_tool: Rc>) -> Self { - Self { - original_image, + pub fn new(original_image: Pixbuf, crop_tool: Rc>) -> Result { + let original_image_surface = ImageSurface::create( + Format::ARgb32, + original_image.width(), + original_image.height(), + )?; + + // render background image + let cx: Context = Context::new(original_image_surface.clone())?; + cx.set_operator(Operator::Over); + + cx.set_source_pixbuf(&original_image, 0.0, 0.0); + cx.paint()?; + + Ok(Self { + original_image: original_image_surface, drawables: Vec::new(), redo_stack: Vec::new(), crop_tool, - } + }) } pub fn render_full_size( @@ -45,7 +58,7 @@ impl Renderer { cx.set_operator(Operator::Over); // render background image - cx.set_source_pixbuf(&self.original_image, 0.0, 0.0); + cx.set_source_surface(&self.original_image, 0.0, 0.0)?; cx.paint()?; // render comitted drawables diff --git a/src/sketch_board.rs b/src/sketch_board.rs index f15676c..e4c29d3 100644 --- a/src/sketch_board.rs +++ b/src/sketch_board.rs @@ -452,7 +452,7 @@ impl Component for SketchBoard { handler: DrawHandler::new(), active_tool: tools.get(&config.initial_tool()), style: Style::default(), - renderer: Renderer::new(image, tools.get_crop_tool()), + renderer: Renderer::new(image, tools.get_crop_tool()).expect("Can't create renderer"), scale_factor: 1.0, tools, };