0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-08 08:09:14 -05:00

♻️ 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_safe as skia;
use std::collections::HashMap;
use uuid::Uuid;
use crate::shapes::{Image, Shape};
use crate::shapes::Shape;
use crate::view::Viewbox;
mod blend;
mod gpu_state;
mod images;
mod options;
use gpu_state::GpuState;
use options::RenderOptions;
pub use blend::BlendMode;
pub use images::*;
pub trait Renderable {
fn render(
&self,
surface: &mut skia::Surface,
images: &HashMap<Uuid, Image>,
) -> Result<(), String>;
fn render(&self, surface: &mut skia::Surface, images: &ImageStore) -> Result<(), String>;
fn blend_mode(&self) -> BlendMode;
fn opacity(&self) -> f32;
}
@ -45,7 +44,7 @@ pub(crate) struct RenderState {
pub cached_surface_image: Option<CachedSurfaceImage>,
options: RenderOptions,
pub viewbox: Viewbox,
images: HashMap<Uuid, Image>,
images: ImageStore,
}
impl RenderState {
@ -68,20 +67,16 @@ impl RenderState {
cached_surface_image: None,
options: RenderOptions::default(),
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> {
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(())
self.images.add(id, image_data)
}
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) {

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 images;
mod paths;
mod render;
mod renderable;
pub use fills::*;
pub use images::*;
pub use paths::*;

View file

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