♻️ Create an ImageStore type

This commit is contained in:
Belén Albeza 2024-12-10 15:41:50 +01:00
parent 967bc75a1c
commit 7b1934dcb6
4 changed files with 48 additions and 26 deletions

View file

@ -1,26 +1,25 @@
use std::collections::HashMap;
use skia::Contains; use skia::Contains;
use skia_safe as skia; use skia_safe as skia;
use std::collections::HashMap;
use uuid::Uuid; use uuid::Uuid;
use crate::shapes::{Image, Shape}; use crate::shapes::Shape;
use crate::view::Viewbox; use crate::view::Viewbox;
mod blend; mod blend;
mod gpu_state; mod gpu_state;
mod images;
mod options; mod options;
use gpu_state::GpuState; use gpu_state::GpuState;
use options::RenderOptions; use options::RenderOptions;
pub use blend::BlendMode; pub use blend::BlendMode;
pub use images::*;
pub trait Renderable { pub trait Renderable {
fn render( fn render(&self, surface: &mut skia::Surface, images: &ImageStore) -> Result<(), String>;
&self,
surface: &mut skia::Surface,
images: &HashMap<Uuid, Image>,
) -> Result<(), String>;
fn blend_mode(&self) -> BlendMode; fn blend_mode(&self) -> BlendMode;
fn opacity(&self) -> f32; fn opacity(&self) -> f32;
} }
@ -45,7 +44,7 @@ pub(crate) struct RenderState {
pub cached_surface_image: Option<CachedSurfaceImage>, pub cached_surface_image: Option<CachedSurfaceImage>,
options: RenderOptions, options: RenderOptions,
pub viewbox: Viewbox, pub viewbox: Viewbox,
images: HashMap<Uuid, Image>, images: ImageStore,
} }
impl RenderState { impl RenderState {
@ -68,20 +67,16 @@ impl RenderState {
cached_surface_image: None, cached_surface_image: None,
options: RenderOptions::default(), options: RenderOptions::default(),
viewbox: Viewbox::new(width as f32, height as f32), viewbox: Viewbox::new(width as f32, height as f32),
images: HashMap::with_capacity(2048), images: ImageStore::new(),
} }
} }
pub fn add_image(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> { pub fn add_image(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> {
let image_data = skia::Data::new_copy(image_data); self.images.add(id, image_data)
let image = Image::from_encoded(image_data).ok_or("Error decoding image data")?;
self.images.insert(id, image);
Ok(())
} }
pub fn has_image(&mut self, id: &Uuid) -> bool { pub fn has_image(&mut self, id: &Uuid) -> bool {
self.images.contains_key(id) self.images.contains(id)
} }
pub fn set_debug_flags(&mut self, debug: u32) { pub fn set_debug_flags(&mut self, debug: u32) {

View file

@ -0,0 +1,33 @@
use skia_safe as skia;
use std::collections::HashMap;
use uuid::Uuid;
pub type Image = skia::Image;
pub struct ImageStore {
images: HashMap<Uuid, Image>,
}
impl ImageStore {
pub fn new() -> Self {
Self {
images: HashMap::with_capacity(2048),
}
}
pub fn add(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> {
let image_data = skia::Data::new_copy(image_data);
let image = Image::from_encoded(image_data).ok_or("Error decoding image data")?;
self.images.insert(id, image);
Ok(())
}
pub fn contains(&mut self, id: &Uuid) -> bool {
self.images.contains_key(id)
}
pub fn get(&self, id: &Uuid) -> Option<&Image> {
self.images.get(id)
}
}

View file

@ -7,7 +7,7 @@ use crate::render::BlendMode;
mod fills; mod fills;
mod images; mod images;
mod paths; mod paths;
mod render; mod renderable;
pub use fills::*; pub use fills::*;
pub use images::*; pub use images::*;
pub use paths::*; pub use paths::*;

View file

@ -1,10 +1,8 @@
use skia_safe as skia; use skia_safe as skia;
use std::collections::HashMap;
use uuid::Uuid;
use super::{draw_image_in_container, Fill, Image, Kind, Shape}; use super::{draw_image_in_container, Fill, Kind, Shape};
use crate::math::Rect; use crate::math::Rect;
use crate::render::Renderable; use crate::render::{ImageStore, Renderable};
impl Renderable for Shape { impl Renderable for Shape {
fn blend_mode(&self) -> crate::render::BlendMode { fn blend_mode(&self) -> crate::render::BlendMode {
@ -15,11 +13,7 @@ impl Renderable for Shape {
self.opacity self.opacity
} }
fn render( fn render(&self, surface: &mut skia_safe::Surface, images: &ImageStore) -> Result<(), String> {
&self,
surface: &mut skia_safe::Surface,
images: &HashMap<Uuid, Image>,
) -> Result<(), String> {
let mut transform = skia::Matrix::new_identity(); let mut transform = skia::Matrix::new_identity();
let (translate_x, translate_y) = self.translation(); let (translate_x, translate_y) = self.translation();
let (scale_x, scale_y) = self.scale(); let (scale_x, scale_y) = self.scale();
@ -59,7 +53,7 @@ impl Renderable for Shape {
fn render_fill( fn render_fill(
surface: &mut skia::Surface, surface: &mut skia::Surface,
images: &HashMap<Uuid, Image>, images: &ImageStore,
fill: &Fill, fill: &Fill,
selrect: Rect, selrect: Rect,
kind: &Kind, kind: &Kind,