Introduce Image struct in core::image

This commit is contained in:
Héctor Ramón Jiménez 2024-08-04 04:30:12 +02:00
parent 974ae6d1e7
commit 92bd3ecd6b
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
19 changed files with 184 additions and 334 deletions

View file

@ -1,5 +1,4 @@
//! Build and draw geometry.
use crate::core::image;
use crate::core::svg;
use crate::core::text::LineHeight;
use crate::core::{
@ -9,11 +8,11 @@ use crate::graphics::cache::{self, Cached};
use crate::graphics::color;
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::{
self, LineCap, LineDash, LineJoin, Path, Stroke, Style,
self, Image, LineCap, LineDash, LineJoin, Path, Stroke, Style,
};
use crate::graphics::gradient::{self, Gradient};
use crate::graphics::mesh::{self, Mesh};
use crate::graphics::{self, Image, Text};
use crate::graphics::{self, Text};
use crate::text;
use crate::triangle;
@ -27,7 +26,7 @@ use std::sync::Arc;
pub enum Geometry {
Live {
meshes: Vec<Mesh>,
images: Vec<Image>,
images: Vec<graphics::Image>,
text: Vec<Text>,
},
Cached(Cache),
@ -36,7 +35,7 @@ pub enum Geometry {
#[derive(Debug, Clone)]
pub struct Cache {
pub meshes: Option<triangle::Cache>,
pub images: Option<Arc<[Image]>>,
pub images: Option<Arc<[graphics::Image]>>,
pub text: Option<text::Cache>,
}
@ -99,7 +98,7 @@ pub struct Frame {
clip_bounds: Rectangle,
buffers: BufferStack,
meshes: Vec<Mesh>,
images: Vec<Image>,
images: Vec<graphics::Image>,
text: Vec<Text>,
transforms: Transforms,
fill_tessellator: tessellation::FillTessellator,
@ -377,25 +376,15 @@ impl geometry::frame::Backend for Frame {
}
}
fn draw_image(
&mut self,
handle: &image::Handle,
bounds: Rectangle,
filter_method: image::FilterMethod,
rotation: Radians,
opacity: f32,
) {
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<Image>) {
let mut image = image.into();
let (bounds, external_rotation) =
self.transforms.current.transform_rectangle(bounds);
self.images.push(Image::Raster {
handle: handle.clone(),
filter_method,
bounds,
rotation: rotation + external_rotation,
opacity,
snap: false,
});
image.rotation += external_rotation;
self.images.push(graphics::Image::Raster(image, bounds));
}
fn draw_svg(
@ -409,7 +398,7 @@ impl geometry::frame::Backend for Frame {
let (bounds, external_rotation) =
self.transforms.current.transform_rectangle(bounds);
self.images.push(Image::Vector {
self.images.push(graphics::Image::Vector {
handle: handle.clone(),
color,
bounds,

View file

@ -220,25 +220,18 @@ impl Pipeline {
for image in images {
match &image {
#[cfg(feature = "image")]
Image::Raster {
handle,
filter_method,
bounds,
rotation,
opacity,
snap,
} => {
Image::Raster(image, bounds) => {
if let Some(atlas_entry) =
cache.upload_raster(device, encoder, handle)
cache.upload_raster(device, encoder, &image.handle)
{
add_instances(
[bounds.x, bounds.y],
[bounds.width, bounds.height],
f32::from(*rotation),
*opacity,
*snap,
f32::from(image.rotation),
image.opacity,
image.snap,
atlas_entry,
match filter_method {
match image.filter_method {
crate::core::image::FilterMethod::Nearest => {
nearest_instances
}

View file

@ -1,5 +1,6 @@
use crate::core::{
renderer, Background, Color, Point, Radians, Rectangle, Transformation,
self, renderer, Background, Color, Point, Radians, Rectangle,
Transformation,
};
use crate::graphics;
use crate::graphics::color;
@ -112,29 +113,10 @@ impl Layer {
self.pending_text.push(text);
}
pub fn draw_image(
&mut self,
image: &Image,
transformation: Transformation,
) {
pub fn draw_image(&mut self, image: Image, transformation: Transformation) {
match image {
Image::Raster {
handle,
filter_method,
bounds,
rotation,
opacity,
snap,
} => {
self.draw_raster(
handle.clone(),
*filter_method,
*bounds,
transformation,
*rotation,
*opacity,
*snap,
);
Image::Raster(image, bounds) => {
self.draw_raster(image, bounds, transformation);
}
Image::Vector {
handle,
@ -145,11 +127,11 @@ impl Layer {
} => {
self.draw_svg(
handle.clone(),
*color,
*bounds,
color,
bounds,
transformation,
*rotation,
*opacity,
rotation,
opacity,
);
}
}
@ -157,22 +139,11 @@ impl Layer {
pub fn draw_raster(
&mut self,
handle: crate::core::image::Handle,
filter_method: crate::core::image::FilterMethod,
image: core::Image,
bounds: Rectangle,
transformation: Transformation,
rotation: Radians,
opacity: f32,
snap: bool,
) {
let image = Image::Raster {
handle,
filter_method,
bounds: bounds * transformation,
rotation,
opacity,
snap,
};
let image = Image::Raster(image, bounds * transformation);
self.images.push(image);
}

View file

@ -527,24 +527,9 @@ impl core::image::Renderer for Renderer {
self.image_cache.borrow_mut().measure_image(handle)
}
fn draw_image(
&mut self,
handle: Self::Handle,
filter_method: core::image::FilterMethod,
bounds: Rectangle,
rotation: core::Radians,
opacity: f32,
) {
fn draw_image(&mut self, image: core::Image, bounds: Rectangle) {
let (layer, transformation) = self.layers.current_mut();
layer.draw_raster(
handle,
filter_method,
bounds,
transformation,
rotation,
opacity,
true,
);
layer.draw_raster(image, bounds, transformation);
}
}
@ -602,7 +587,7 @@ impl graphics::geometry::Renderer for Renderer {
layer.draw_mesh_group(meshes, transformation);
for image in images {
layer.draw_image(&image, transformation);
layer.draw_image(image, transformation);
}
layer.draw_text_group(text, transformation);
@ -613,7 +598,7 @@ impl graphics::geometry::Renderer for Renderer {
}
if let Some(images) = cache.images {
for image in images.iter() {
for image in images.iter().cloned() {
layer.draw_image(image, transformation);
}
}