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/. */
use std::cell::RefCell;
#[allow(unused_imports)]
use std::marker::PhantomData;
use std::mem;
use std::sync::{Arc, Mutex};
@ -247,23 +245,10 @@ pub trait GenericDrawTarget {
source: Rect<i32>,
destination: Point2D<i32>,
);
fn create_gradient_stops(
&self,
gradient_stops: Vec<GradientStop>,
extend_mode: ExtendMode,
) -> GradientStops;
fn create_gradient_stops(&self, gradient_stops: Vec<GradientStop>) -> GradientStops;
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder>;
fn create_similar_draw_target(
&self,
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 create_similar_draw_target(&self, size: &Size2D<i32>) -> Box<dyn GenericDrawTarget>;
fn create_source_surface_from_data(&self, data: &[u8]) -> Option<SourceSurface>;
fn draw_surface(
&mut self,
surface: SourceSurface,
@ -292,7 +277,6 @@ pub trait GenericDrawTarget {
draw_options: &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_transform(&self) -> Transform2D<f32>;
fn pop_clip(&mut self);
@ -325,11 +309,6 @@ pub trait GenericDrawTarget {
fn snapshot_data_owned(&self) -> Vec<u8>;
}
#[derive(Clone)]
pub enum ExtendMode {
Raqote(()),
}
pub enum GradientStop {
Raqote(raqote::GradientStop),
}
@ -348,10 +327,6 @@ pub enum CompositionOp {
Raqote(raqote::BlendMode),
}
pub enum SurfaceFormat {
Raqote(()),
}
#[derive(Clone)]
pub enum SourceSurface {
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>),
}
pub enum DrawSurfaceOptions {
Raqote(()),
}
#[derive(Clone)]
pub enum DrawOptions {
Raqote(raqote::DrawOptions),
}
#[derive(Clone)]
pub enum StrokeOptions<'a> {
Raqote(raqote::StrokeStyle, PhantomData<&'a ()>),
pub enum StrokeOptions {
Raqote(raqote::StrokeStyle),
}
#[derive(Clone, Copy)]
pub enum Filter {
Linear,
Point,
Bilinear,
Nearest,
}
pub(crate) type CanvasFontContext = FontContext<FontCacheThread>;
@ -1162,11 +1133,7 @@ impl<'a> CanvasData<'a> {
pixels::rgba8_byte_swap_and_premultiply_inplace(&mut imagedata);
let source_surface = self
.drawtarget
.create_source_surface_from_data(
&imagedata,
rect.size.to_i32(),
rect.size.width as i32 * 4,
)
.create_source_surface_from_data(&imagedata)
.unwrap();
self.drawtarget.copy_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> {
let mut draw_target = self.drawtarget.create_similar_draw_target(
&Size2D::new(
let mut draw_target = self.drawtarget.create_similar_draw_target(&Size2D::new(
source_rect.size.width as i32,
source_rect.size.height as i32,
),
self.drawtarget.get_format(),
);
));
let matrix = self.state.transform.then(
&Transform2D::identity().pre_translate(-source_rect.origin.to_vector().cast::<f32>()),
);
@ -1290,7 +1254,7 @@ pub struct CanvasPaintState<'a> {
pub draw_options: DrawOptions,
pub fill_style: Pattern<'a>,
pub stroke_style: Pattern<'a>,
pub stroke_opts: StrokeOptions<'a>,
pub stroke_opts: StrokeOptions,
/// The current 2D transform matrix.
pub transform: Transform2D<f32>,
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.
// Otherwise, the image must be rendered using nearest-neighbor interpolation.
let filter = if smoothing_enabled {
Filter::Linear
Filter::Bilinear
} else {
Filter::Point
Filter::Nearest
};
let image_size = image_size.to_i32();
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();
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
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::marker::PhantomData;
use canvas_traits::canvas::*;
use cssparser::RGBA;
use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D};
@ -15,9 +13,8 @@ use raqote::PathOp;
use crate::canvas_data;
use crate::canvas_data::{
Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, ExtendMode, Filter,
GenericDrawTarget, GenericPathBuilder, GradientStop, GradientStops, Path, SourceSurface,
StrokeOptions, SurfaceFormat,
Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, Filter, GenericDrawTarget,
GenericPathBuilder, GradientStop, GradientStops, Path, SourceSurface, StrokeOptions,
};
use crate::canvas_paint_thread::AntialiasMode;
@ -85,7 +82,7 @@ impl<'a> CanvasPaintState<'a> {
draw_options: DrawOptions::Raqote(raqote::DrawOptions::new()),
fill_style: canvas_data::Pattern::Raqote(pattern.clone()),
stroke_style: canvas_data::Pattern::Raqote(pattern),
stroke_opts: StrokeOptions::Raqote(Default::default(), PhantomData),
stroke_opts: StrokeOptions::Raqote(Default::default()),
transform: Transform2D::identity(),
shadow_offset_x: 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) {
match self {
StrokeOptions::Raqote(options, _) => options.width = _val,
StrokeOptions::Raqote(options) => options.width = _val,
}
}
pub fn set_miter_limit(&mut self, _val: f32) {
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) {
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) {
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 {
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.
// It feels cumbersome to convert GradientStop back and forth just to use
// `create_gradient_stops()`, so I'll leave this here for now.
fn create_gradient_stops(
&self,
gradient_stops: Vec<GradientStop>,
_extend_mode: ExtendMode,
) -> GradientStops {
fn create_gradient_stops(&self, gradient_stops: Vec<GradientStop>) -> GradientStops {
let mut stops = gradient_stops
.into_iter()
.map(|item| item.as_raqote().clone())
@ -406,19 +399,10 @@ impl GenericDrawTarget for raqote::DrawTarget {
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> {
Box::new(PathBuilder::new())
}
fn create_similar_draw_target(
&self,
size: &Size2D<i32>,
_format: SurfaceFormat,
) -> Box<dyn GenericDrawTarget> {
fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Box<dyn GenericDrawTarget> {
Box::new(raqote::DrawTarget::new(size.width, size.height))
}
fn create_source_surface_from_data(
&self,
data: &[u8],
_size: Size2D<i32>,
_stride: i32,
) -> Option<SourceSurface> {
fn create_source_surface_from_data(&self, data: &[u8]) -> Option<SourceSurface> {
Some(SourceSurface::Raqote(data.to_vec()))
}
#[allow(unsafe_code)]
@ -588,9 +572,6 @@ impl GenericDrawTarget for raqote::DrawTarget {
&DrawOptions::Raqote(draw_options),
);
}
fn get_format(&self) -> SurfaceFormat {
SurfaceFormat::Raqote(())
}
fn get_size(&self) -> Size2D<i32> {
Size2D::new(self.width(), self.height())
}
@ -696,8 +677,8 @@ impl GenericDrawTarget for raqote::DrawTarget {
impl Filter {
fn to_raqote(&self) -> raqote::FilterMode {
match self {
Filter::Linear => raqote::FilterMode::Bilinear,
Filter::Point => raqote::FilterMode::Nearest,
Filter::Bilinear => raqote::FilterMode::Bilinear,
Filter::Nearest => raqote::FilterMode::Nearest,
}
}
}