Clean up a bit of the canvas backend abstractions (#30637)

* Clean up a bit of the canvas backend abstractions

* Remove unused import
Sneaky, sneaky little hecker
This commit is contained in:
Ennui Langeweile 2023-10-27 20:06:43 -03:00 committed by GitHub
parent faf928f3a8
commit a6ceca6d9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 86 deletions

View file

@ -3,8 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell; use std::cell::RefCell;
#[allow(unused_imports)]
use std::marker::PhantomData;
use std::mem; use std::mem;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -247,23 +245,10 @@ pub trait GenericDrawTarget {
source: Rect<i32>, source: Rect<i32>,
destination: Point2D<i32>, destination: Point2D<i32>,
); );
fn create_gradient_stops( fn create_gradient_stops(&self, gradient_stops: Vec<GradientStop>) -> GradientStops;
&self,
gradient_stops: Vec<GradientStop>,
extend_mode: ExtendMode,
) -> GradientStops;
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder>; fn create_path_builder(&self) -> Box<dyn GenericPathBuilder>;
fn create_similar_draw_target( fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Box<dyn GenericDrawTarget>;
&self, fn create_source_surface_from_data(&self, data: &[u8]) -> Option<SourceSurface>;
size: &Size2D<i32>,
format: SurfaceFormat,
) -> Box<dyn GenericDrawTarget>;
fn create_source_surface_from_data(
&self,
data: &[u8],
size: Size2D<i32>,
stride: i32,
) -> Option<SourceSurface>;
fn draw_surface( fn draw_surface(
&mut self, &mut self,
surface: SourceSurface, surface: SourceSurface,
@ -292,7 +277,6 @@ pub trait GenericDrawTarget {
draw_options: &DrawOptions, draw_options: &DrawOptions,
); );
fn fill_rect(&mut self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>); fn fill_rect(&mut self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>);
fn get_format(&self) -> SurfaceFormat;
fn get_size(&self) -> Size2D<i32>; fn get_size(&self) -> Size2D<i32>;
fn get_transform(&self) -> Transform2D<f32>; fn get_transform(&self) -> Transform2D<f32>;
fn pop_clip(&mut self); fn pop_clip(&mut self);
@ -325,11 +309,6 @@ pub trait GenericDrawTarget {
fn snapshot_data_owned(&self) -> Vec<u8>; fn snapshot_data_owned(&self) -> Vec<u8>;
} }
#[derive(Clone)]
pub enum ExtendMode {
Raqote(()),
}
pub enum GradientStop { pub enum GradientStop {
Raqote(raqote::GradientStop), Raqote(raqote::GradientStop),
} }
@ -348,10 +327,6 @@ pub enum CompositionOp {
Raqote(raqote::BlendMode), Raqote(raqote::BlendMode),
} }
pub enum SurfaceFormat {
Raqote(()),
}
#[derive(Clone)] #[derive(Clone)]
pub enum SourceSurface { pub enum SourceSurface {
Raqote(Vec<u8>), // TODO: See if we can avoid the alloc (probably?) Raqote(Vec<u8>), // TODO: See if we can avoid the alloc (probably?)
@ -367,24 +342,20 @@ pub enum Pattern<'a> {
Raqote(crate::raqote_backend::Pattern<'a>), Raqote(crate::raqote_backend::Pattern<'a>),
} }
pub enum DrawSurfaceOptions {
Raqote(()),
}
#[derive(Clone)] #[derive(Clone)]
pub enum DrawOptions { pub enum DrawOptions {
Raqote(raqote::DrawOptions), Raqote(raqote::DrawOptions),
} }
#[derive(Clone)] #[derive(Clone)]
pub enum StrokeOptions<'a> { pub enum StrokeOptions {
Raqote(raqote::StrokeStyle, PhantomData<&'a ()>), Raqote(raqote::StrokeStyle),
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum Filter { pub enum Filter {
Linear, Bilinear,
Point, Nearest,
} }
pub(crate) type CanvasFontContext = FontContext<FontCacheThread>; pub(crate) type CanvasFontContext = FontContext<FontCacheThread>;
@ -1162,11 +1133,7 @@ impl<'a> CanvasData<'a> {
pixels::rgba8_byte_swap_and_premultiply_inplace(&mut imagedata); pixels::rgba8_byte_swap_and_premultiply_inplace(&mut imagedata);
let source_surface = self let source_surface = self
.drawtarget .drawtarget
.create_source_surface_from_data( .create_source_surface_from_data(&imagedata)
&imagedata,
rect.size.to_i32(),
rect.size.width as i32 * 4,
)
.unwrap(); .unwrap();
self.drawtarget.copy_surface( self.drawtarget.copy_surface(
source_surface, source_surface,
@ -1212,13 +1179,10 @@ impl<'a> CanvasData<'a> {
} }
fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> Box<dyn GenericDrawTarget> { fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> Box<dyn GenericDrawTarget> {
let mut draw_target = self.drawtarget.create_similar_draw_target( let mut draw_target = self.drawtarget.create_similar_draw_target(&Size2D::new(
&Size2D::new( source_rect.size.width as i32,
source_rect.size.width as i32, source_rect.size.height as i32,
source_rect.size.height as i32, ));
),
self.drawtarget.get_format(),
);
let matrix = self.state.transform.then( let matrix = self.state.transform.then(
&Transform2D::identity().pre_translate(-source_rect.origin.to_vector().cast::<f32>()), &Transform2D::identity().pre_translate(-source_rect.origin.to_vector().cast::<f32>()),
); );
@ -1290,7 +1254,7 @@ pub struct CanvasPaintState<'a> {
pub draw_options: DrawOptions, pub draw_options: DrawOptions,
pub fill_style: Pattern<'a>, pub fill_style: Pattern<'a>,
pub stroke_style: Pattern<'a>, pub stroke_style: Pattern<'a>,
pub stroke_opts: StrokeOptions<'a>, pub stroke_opts: StrokeOptions,
/// The current 2D transform matrix. /// The current 2D transform matrix.
pub transform: Transform2D<f32>, pub transform: Transform2D<f32>,
pub shadow_offset_x: f64, pub shadow_offset_x: f64,
@ -1333,14 +1297,13 @@ fn write_image(
// to apply a smoothing algorithm to the image data when it is scaled. // to apply a smoothing algorithm to the image data when it is scaled.
// Otherwise, the image must be rendered using nearest-neighbor interpolation. // Otherwise, the image must be rendered using nearest-neighbor interpolation.
let filter = if smoothing_enabled { let filter = if smoothing_enabled {
Filter::Linear Filter::Bilinear
} else { } else {
Filter::Point Filter::Nearest
}; };
let image_size = image_size.to_i32();
let source_surface = draw_target let source_surface = draw_target
.create_source_surface_from_data(&image_data, image_size, image_size.width * 4) .create_source_surface_from_data(&image_data)
.unwrap(); .unwrap();
draw_target.draw_surface(source_surface, dest_rect, image_rect, filter, draw_options); draw_target.draw_surface(source_surface, dest_rect, image_rect, filter, draw_options);

View file

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::marker::PhantomData;
use canvas_traits::canvas::*; use canvas_traits::canvas::*;
use cssparser::RGBA; use cssparser::RGBA;
use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D}; use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D};
@ -15,9 +13,8 @@ use raqote::PathOp;
use crate::canvas_data; use crate::canvas_data;
use crate::canvas_data::{ use crate::canvas_data::{
Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, ExtendMode, Filter, Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, Filter, GenericDrawTarget,
GenericDrawTarget, GenericPathBuilder, GradientStop, GradientStops, Path, SourceSurface, GenericPathBuilder, GradientStop, GradientStops, Path, SourceSurface, StrokeOptions,
StrokeOptions, SurfaceFormat,
}; };
use crate::canvas_paint_thread::AntialiasMode; use crate::canvas_paint_thread::AntialiasMode;
@ -85,7 +82,7 @@ impl<'a> CanvasPaintState<'a> {
draw_options: DrawOptions::Raqote(raqote::DrawOptions::new()), draw_options: DrawOptions::Raqote(raqote::DrawOptions::new()),
fill_style: canvas_data::Pattern::Raqote(pattern.clone()), fill_style: canvas_data::Pattern::Raqote(pattern.clone()),
stroke_style: canvas_data::Pattern::Raqote(pattern), stroke_style: canvas_data::Pattern::Raqote(pattern),
stroke_opts: StrokeOptions::Raqote(Default::default(), PhantomData), stroke_opts: StrokeOptions::Raqote(Default::default()),
transform: Transform2D::identity(), transform: Transform2D::identity(),
shadow_offset_x: 0.0, shadow_offset_x: 0.0,
shadow_offset_y: 0.0, shadow_offset_y: 0.0,
@ -267,30 +264,30 @@ impl canvas_data::Pattern<'_> {
} }
} }
impl<'a> StrokeOptions<'a> { impl StrokeOptions {
pub fn set_line_width(&mut self, _val: f32) { pub fn set_line_width(&mut self, _val: f32) {
match self { match self {
StrokeOptions::Raqote(options, _) => options.width = _val, StrokeOptions::Raqote(options) => options.width = _val,
} }
} }
pub fn set_miter_limit(&mut self, _val: f32) { pub fn set_miter_limit(&mut self, _val: f32) {
match self { match self {
StrokeOptions::Raqote(options, _) => options.miter_limit = _val, StrokeOptions::Raqote(options) => options.miter_limit = _val,
} }
} }
pub fn set_line_join(&mut self, val: LineJoinStyle) { pub fn set_line_join(&mut self, val: LineJoinStyle) {
match self { match self {
StrokeOptions::Raqote(options, _) => options.join = val.to_raqote_style(), StrokeOptions::Raqote(options) => options.join = val.to_raqote_style(),
} }
} }
pub fn set_line_cap(&mut self, val: LineCapStyle) { pub fn set_line_cap(&mut self, val: LineCapStyle) {
match self { match self {
StrokeOptions::Raqote(options, _) => options.cap = val.to_raqote_style(), StrokeOptions::Raqote(options) => options.cap = val.to_raqote_style(),
} }
} }
pub fn as_raqote(&self) -> &raqote::StrokeStyle { pub fn as_raqote(&self) -> &raqote::StrokeStyle {
match self { match self {
StrokeOptions::Raqote(options, _) => options, StrokeOptions::Raqote(options) => options,
} }
} }
} }
@ -389,11 +386,7 @@ impl GenericDrawTarget for raqote::DrawTarget {
// Somehow a duplicate of `create_gradient_stops()` with different types. // Somehow a duplicate of `create_gradient_stops()` with different types.
// It feels cumbersome to convert GradientStop back and forth just to use // It feels cumbersome to convert GradientStop back and forth just to use
// `create_gradient_stops()`, so I'll leave this here for now. // `create_gradient_stops()`, so I'll leave this here for now.
fn create_gradient_stops( fn create_gradient_stops(&self, gradient_stops: Vec<GradientStop>) -> GradientStops {
&self,
gradient_stops: Vec<GradientStop>,
_extend_mode: ExtendMode,
) -> GradientStops {
let mut stops = gradient_stops let mut stops = gradient_stops
.into_iter() .into_iter()
.map(|item| item.as_raqote().clone()) .map(|item| item.as_raqote().clone())
@ -406,19 +399,10 @@ impl GenericDrawTarget for raqote::DrawTarget {
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> { fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> {
Box::new(PathBuilder::new()) Box::new(PathBuilder::new())
} }
fn create_similar_draw_target( fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Box<dyn GenericDrawTarget> {
&self,
size: &Size2D<i32>,
_format: SurfaceFormat,
) -> Box<dyn GenericDrawTarget> {
Box::new(raqote::DrawTarget::new(size.width, size.height)) Box::new(raqote::DrawTarget::new(size.width, size.height))
} }
fn create_source_surface_from_data( fn create_source_surface_from_data(&self, data: &[u8]) -> Option<SourceSurface> {
&self,
data: &[u8],
_size: Size2D<i32>,
_stride: i32,
) -> Option<SourceSurface> {
Some(SourceSurface::Raqote(data.to_vec())) Some(SourceSurface::Raqote(data.to_vec()))
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
@ -588,9 +572,6 @@ impl GenericDrawTarget for raqote::DrawTarget {
&DrawOptions::Raqote(draw_options), &DrawOptions::Raqote(draw_options),
); );
} }
fn get_format(&self) -> SurfaceFormat {
SurfaceFormat::Raqote(())
}
fn get_size(&self) -> Size2D<i32> { fn get_size(&self) -> Size2D<i32> {
Size2D::new(self.width(), self.height()) Size2D::new(self.width(), self.height())
} }
@ -696,8 +677,8 @@ impl GenericDrawTarget for raqote::DrawTarget {
impl Filter { impl Filter {
fn to_raqote(&self) -> raqote::FilterMode { fn to_raqote(&self) -> raqote::FilterMode {
match self { match self {
Filter::Linear => raqote::FilterMode::Bilinear, Filter::Bilinear => raqote::FilterMode::Bilinear,
Filter::Point => raqote::FilterMode::Nearest, Filter::Nearest => raqote::FilterMode::Nearest,
} }
} }
} }