Make CanvasData use GenericDrawTarget

This commit is contained in:
pylbrecht 2019-05-24 15:45:15 +02:00 committed by Josh Matthews
parent 7ace517770
commit 42bf1984be
2 changed files with 223 additions and 172 deletions

View file

@ -7,7 +7,7 @@ use azure::azure_hl;
use azure::azure_hl::SurfacePattern; use azure::azure_hl::SurfacePattern;
use azure::azure_hl::{AntialiasMode, AsAzurePoint, CapStyle, JoinStyle}; use azure::azure_hl::{AntialiasMode, AsAzurePoint, CapStyle, JoinStyle};
use azure::azure_hl::{BackendType, DrawTarget}; use azure::azure_hl::{BackendType, DrawTarget};
use azure::azure_hl::{ColorPattern, DrawSurfaceOptions, Filter, PathBuilder}; use azure::azure_hl::{ColorPattern, Filter};
use azure::azure_hl::{LinearGradientPattern, RadialGradientPattern}; use azure::azure_hl::{LinearGradientPattern, RadialGradientPattern};
use canvas_traits::canvas::*; use canvas_traits::canvas::*;
use cssparser::RGBA; use cssparser::RGBA;
@ -30,13 +30,13 @@ enum PathState {
/// Path builder in user-space. If a transform has been applied /// Path builder in user-space. If a transform has been applied
/// but no further path operations have occurred, it is stored /// but no further path operations have occurred, it is stored
/// in the optional field. /// in the optional field.
UserSpacePathBuilder(PathBuilder, Option<Transform2D<AzFloat>>), UserSpacePathBuilder(Box<GenericPathBuilder>, Option<Transform2D<f32>>),
/// Path builder in device-space. /// Path builder in device-space.
DeviceSpacePathBuilder(PathBuilder), DeviceSpacePathBuilder(Box<GenericPathBuilder>),
/// Path in user-space. If a transform has been applied but /// Path in user-space. If a transform has been applied but
/// but no further path operations have occurred, it is stored /// but no further path operations have occurred, it is stored
/// in the optional field. /// in the optional field.
UserSpacePath(azure_hl::Path, Option<Transform2D<AzFloat>>), UserSpacePath(Path, Option<Transform2D<f32>>),
} }
impl PathState { impl PathState {
@ -47,7 +47,7 @@ impl PathState {
} }
} }
fn path(&self) -> &azure_hl::Path { fn path(&self) -> &Path {
match *self { match *self {
PathState::UserSpacePath(ref p, _) => p, PathState::UserSpacePath(ref p, _) => p,
PathState::UserSpacePathBuilder(..) | PathState::DeviceSpacePathBuilder(..) => { PathState::UserSpacePathBuilder(..) | PathState::DeviceSpacePathBuilder(..) => {
@ -89,6 +89,7 @@ trait GenericPathBuilder {
fn line_to(&self, point: Point2D<f32>); fn line_to(&self, point: Point2D<f32>);
fn move_to(&self, point: Point2D<f32>); fn move_to(&self, point: Point2D<f32>);
fn quadratic_curve_to(&self, control_point: &Point2D<f32>, end_point: &Point2D<f32>); fn quadratic_curve_to(&self, control_point: &Point2D<f32>, end_point: &Point2D<f32>);
fn finish(&self) -> Path;
} }
impl GenericPathBuilder for azure_hl::PathBuilder { impl GenericPathBuilder for azure_hl::PathBuilder {
@ -159,12 +160,15 @@ impl GenericPathBuilder for azure_hl::PathBuilder {
end_point as &Point2D<AzFloat>, end_point as &Point2D<AzFloat>,
); );
} }
fn finish(&self) -> Path {
Path::Azure(self.finish())
}
} }
/// A wrapper around a stored PathBuilder and an optional transformation that should be /// A wrapper around a stored PathBuilder and an optional transformation that should be
/// applied to any points to ensure they are in the matching device space. /// applied to any points to ensure they are in the matching device space.
struct PathBuilderRef<'a> { struct PathBuilderRef<'a> {
builder: &'a PathBuilder, builder: &'a Box<GenericPathBuilder>,
transform: Transform2D<AzFloat>, transform: Transform2D<AzFloat>,
} }
@ -274,7 +278,7 @@ trait GenericDrawTarget {
gradient_stops: Vec<GradientStop>, gradient_stops: Vec<GradientStop>,
extend_mode: ExtendMode, extend_mode: ExtendMode,
) -> GradientStops; ) -> GradientStops;
fn create_path_builder(&self); fn create_path_builder(&self) -> Box<GenericPathBuilder>;
fn create_similar_draw_target( fn create_similar_draw_target(
&self, &self,
size: &Size2D<i32>, size: &Size2D<i32>,
@ -286,6 +290,14 @@ trait GenericDrawTarget {
size: Size2D<i32>, size: Size2D<i32>,
stride: i32, stride: i32,
format: SurfaceFormat, format: SurfaceFormat,
) -> Option<SourceSurface>;
fn draw_surface(
&self,
surface: SourceSurface,
dest: Rect<f32>,
source: Rect<f32>,
surf_options: DrawSurfaceOptions,
options: DrawOptions,
); );
fn draw_surface_with_shadow( fn draw_surface_with_shadow(
&self, &self,
@ -296,8 +308,8 @@ trait GenericDrawTarget {
sigma: f32, sigma: f32,
operator: CompositionOp, operator: CompositionOp,
); );
fn fill(&self, path: &Path, pattern: PatternRef, draw_options: &DrawOptions); fn fill(&self, path: &Path, pattern: Pattern, draw_options: &DrawOptions);
fn fill_rect(&self, rect: &Rect<f32>, pattern: PatternRef, draw_options: Option<&DrawOptions>); fn fill_rect(&self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>);
fn get_format(&self) -> SurfaceFormat; fn get_format(&self) -> SurfaceFormat;
fn get_size(&self) -> IntSize; fn get_size(&self) -> IntSize;
fn get_transform(&self) -> Transform2D<f32>; fn get_transform(&self) -> Transform2D<f32>;
@ -308,7 +320,7 @@ trait GenericDrawTarget {
fn stroke( fn stroke(
&self, &self,
path: &Path, path: &Path,
pattern: PatternRef, pattern: Pattern,
stroke_options: &StrokeOptions, stroke_options: &StrokeOptions,
draw_options: &DrawOptions, draw_options: &DrawOptions,
); );
@ -316,14 +328,14 @@ trait GenericDrawTarget {
&self, &self,
start: Point2D<f32>, start: Point2D<f32>,
end: Point2D<f32>, end: Point2D<f32>,
pattern: PatternRef, pattern: Pattern,
stroke_options: &StrokeOptions, stroke_options: &StrokeOptions,
draw_options: &DrawOptions, draw_options: &DrawOptions,
); );
fn stroke_rect( fn stroke_rect(
&self, &self,
rect: &Rect<f32>, rect: &Rect<f32>,
pattern: PatternRef, pattern: Pattern,
stroke_options: &StrokeOptions, stroke_options: &StrokeOptions,
draw_options: &DrawOptions, draw_options: &DrawOptions,
); );
@ -348,8 +360,8 @@ impl GenericDrawTarget for azure_hl::DrawTarget {
GradientStops::Azure(self.create_gradient_stops(&gradient_stops, extend_mode.into_azure())) GradientStops::Azure(self.create_gradient_stops(&gradient_stops, extend_mode.into_azure()))
} }
fn create_path_builder(&self) { fn create_path_builder(&self) -> Box<GenericPathBuilder> {
self.create_path_builder(); Box::new(self.create_path_builder())
} }
fn create_similar_draw_target( fn create_similar_draw_target(
@ -365,8 +377,24 @@ impl GenericDrawTarget for azure_hl::DrawTarget {
size: Size2D<i32>, size: Size2D<i32>,
stride: i32, stride: i32,
format: SurfaceFormat, format: SurfaceFormat,
) -> Option<SourceSurface> {
self.create_source_surface_from_data(data, size, stride, format.into_azure()).map(|s| SourceSurface::Azure(s))
}
fn draw_surface(
&self,
surface: SourceSurface,
dest: Rect<f32>,
source: Rect<f32>,
surf_options: DrawSurfaceOptions,
options: DrawOptions,
) { ) {
self.create_source_surface_from_data(data, size, stride, format.into_azure()); self.draw_surface(
surface.into_azure(),
dest as Rect<AzFloat>,
source as Rect<AzFloat>,
surf_options.into_azure(),
options.into_azure(),
);
} }
fn draw_surface_with_shadow( fn draw_surface_with_shadow(
&self, &self,
@ -386,17 +414,17 @@ impl GenericDrawTarget for azure_hl::DrawTarget {
operator.into_azure(), operator.into_azure(),
); );
} }
fn fill(&self, path: &Path, pattern: PatternRef, draw_options: &DrawOptions) { fn fill(&self, path: &Path, pattern: Pattern, draw_options: &DrawOptions) {
self.fill( self.fill(
path.as_azure(), path.as_azure(),
pattern.into_azure(), pattern.as_azure().to_pattern_ref(),
draw_options.as_azure(), draw_options.as_azure(),
); );
} }
fn fill_rect(&self, rect: &Rect<f32>, pattern: PatternRef, draw_options: Option<&DrawOptions>) { fn fill_rect(&self, rect: &Rect<f32>, pattern: Pattern, draw_options: Option<&DrawOptions>) {
self.fill_rect( self.fill_rect(
rect as &Rect<AzFloat>, rect as &Rect<AzFloat>,
pattern.into_azure(), pattern.as_azure().to_pattern_ref(),
draw_options.map(|x| x.as_azure()), draw_options.map(|x| x.as_azure()),
); );
} }
@ -424,13 +452,13 @@ impl GenericDrawTarget for azure_hl::DrawTarget {
fn stroke( fn stroke(
&self, &self,
path: &Path, path: &Path,
pattern: PatternRef, pattern: Pattern,
stroke_options: &StrokeOptions, stroke_options: &StrokeOptions,
draw_options: &DrawOptions, draw_options: &DrawOptions,
) { ) {
self.stroke( self.stroke(
path.as_azure(), path.as_azure(),
pattern.into_azure(), pattern.as_azure().to_pattern_ref(),
stroke_options.as_azure(), stroke_options.as_azure(),
draw_options.as_azure(), draw_options.as_azure(),
); );
@ -439,14 +467,14 @@ impl GenericDrawTarget for azure_hl::DrawTarget {
&self, &self,
start: Point2D<f32>, start: Point2D<f32>,
end: Point2D<f32>, end: Point2D<f32>,
pattern: PatternRef, pattern: Pattern,
stroke_options: &StrokeOptions, stroke_options: &StrokeOptions,
draw_options: &DrawOptions, draw_options: &DrawOptions,
) { ) {
self.stroke_line( self.stroke_line(
start as Point2D<AzFloat>, start as Point2D<AzFloat>,
end as Point2D<AzFloat>, end as Point2D<AzFloat>,
pattern.into_azure(), pattern.as_azure().to_pattern_ref(),
stroke_options.as_azure(), stroke_options.as_azure(),
draw_options.as_azure(), draw_options.as_azure(),
); );
@ -454,13 +482,13 @@ impl GenericDrawTarget for azure_hl::DrawTarget {
fn stroke_rect( fn stroke_rect(
&self, &self,
rect: &Rect<f32>, rect: &Rect<f32>,
pattern: PatternRef, pattern: Pattern,
stroke_options: &StrokeOptions, stroke_options: &StrokeOptions,
draw_options: &DrawOptions, draw_options: &DrawOptions,
) { ) {
self.stroke_rect( self.stroke_rect(
rect as &Rect<AzFloat>, rect as &Rect<AzFloat>,
pattern.into_azure(), pattern.as_azure().to_pattern_ref(),
stroke_options.as_azure(), stroke_options.as_azure(),
draw_options.as_azure(), draw_options.as_azure(),
); );
@ -474,13 +502,6 @@ enum ExtendMode {
} }
impl ExtendMode { impl ExtendMode {
fn as_azure(&self) -> &azure_hl::ExtendMode {
match self {
ExtendMode::Azure(m) => m,
_ => unreachable!(),
}
}
fn into_azure(self) -> azure_hl::ExtendMode { fn into_azure(self) -> azure_hl::ExtendMode {
match self { match self {
ExtendMode::Azure(m) => m, ExtendMode::Azure(m) => m,
@ -489,20 +510,12 @@ impl ExtendMode {
} }
} }
#[derive(Clone)]
enum GradientStop { enum GradientStop {
Azure(AzGradientStop), Azure(AzGradientStop),
Raqote(()), Raqote(()),
} }
impl GradientStop { impl GradientStop {
fn as_azure(&self) -> &AzGradientStop {
match self {
GradientStop::Azure(s) => s,
_ => unreachable!(),
}
}
fn into_azure(self) -> AzGradientStop { fn into_azure(self) -> AzGradientStop {
match self { match self {
GradientStop::Azure(s) => s, GradientStop::Azure(s) => s,
@ -511,20 +524,12 @@ impl GradientStop {
} }
} }
#[derive(Clone)]
enum GradientStops { enum GradientStops {
Azure(azure_hl::GradientStops), Azure(azure_hl::GradientStops),
Raqote(()), Raqote(()),
} }
impl GradientStops { impl GradientStops {
fn as_azure(&self) -> &azure_hl::GradientStops {
match self {
GradientStops::Azure(s) => s,
_ => unreachable!(),
}
}
fn into_azure(self) -> azure_hl::GradientStops { fn into_azure(self) -> azure_hl::GradientStops {
match self { match self {
GradientStops::Azure(s) => s, GradientStops::Azure(s) => s,
@ -534,7 +539,7 @@ impl GradientStops {
} }
#[derive(Clone)] #[derive(Clone)]
enum Color { pub enum Color {
Azure(azure_hl::Color), Azure(azure_hl::Color),
Raqote(()), Raqote(()),
} }
@ -563,7 +568,6 @@ impl CompositionOp {
} }
} }
#[derive(Clone)]
enum SurfaceFormat { enum SurfaceFormat {
Azure(azure_hl::SurfaceFormat), Azure(azure_hl::SurfaceFormat),
Raqote(()), Raqote(()),
@ -593,14 +597,13 @@ impl SourceSurface {
} }
} }
#[derive(Clone)]
enum IntSize { enum IntSize {
Azure(AzIntSize), Azure(AzIntSize),
Raqote(()), Raqote(()),
} }
impl IntSize { impl IntSize {
fn as_azure(&self) -> &AzIntSize { fn into_azure(self) -> AzIntSize {
match self { match self {
IntSize::Azure(s) => s, IntSize::Azure(s) => s,
_ => unreachable!(), _ => unreachable!(),
@ -608,7 +611,6 @@ impl IntSize {
} }
} }
#[derive(Clone)]
enum Path { enum Path {
Azure(azure_hl::Path), Azure(azure_hl::Path),
Raqote(()), Raqote(()),
@ -629,28 +631,6 @@ enum Pattern {
Raqote(()), Raqote(()),
} }
#[derive(Clone)]
enum PatternRef<'a> {
Azure(azure_hl::PatternRef<'a>),
Raqote(()),
}
impl<'a> PatternRef<'a> {
fn as_azure(&self) -> &azure_hl::PatternRef<'a> {
match self {
PatternRef::Azure(p) => p,
_ => unreachable!(),
}
}
fn into_azure(self) -> azure_hl::PatternRef<'a> {
match self {
PatternRef::Azure(p) => p,
_ => unreachable!(),
}
}
}
impl Pattern { impl Pattern {
fn as_azure(&self) -> &azure_hl::Pattern { fn as_azure(&self) -> &azure_hl::Pattern {
match self { match self {
@ -660,6 +640,20 @@ impl Pattern {
} }
} }
enum DrawSurfaceOptions {
Azure(azure_hl::DrawSurfaceOptions),
Raqote(()),
}
impl DrawSurfaceOptions {
fn into_azure(self) -> azure_hl::DrawSurfaceOptions {
match self {
DrawSurfaceOptions::Azure(options) => options,
_ => unreachable!(),
}
}
}
#[derive(Clone)] #[derive(Clone)]
enum DrawOptions { enum DrawOptions {
Azure(azure_hl::DrawOptions), Azure(azure_hl::DrawOptions),
@ -673,6 +667,24 @@ impl DrawOptions {
_ => unreachable!(), _ => unreachable!(),
} }
} }
fn as_azure_mut(&mut self) -> &mut azure_hl::DrawOptions {
match self {
DrawOptions::Azure(options) => options,
_ => unreachable!(),
}
}
fn into_azure(self) -> azure_hl::DrawOptions {
match self {
DrawOptions::Azure(options) => options,
_ => unreachable!(),
}
}
fn set_alpha(&mut self, val: f32) {
match self {
DrawOptions::Azure(options) => options.alpha = val as AzFloat,
_ => unreachable!(),
}
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -688,10 +700,34 @@ impl<'a> StrokeOptions<'a> {
_ => unreachable!(), _ => unreachable!(),
} }
} }
fn set_line_width(&mut self, val: f32) {
match self {
StrokeOptions::Azure(options) => options.line_width = val as AzFloat,
_ => unreachable!(),
}
}
fn set_miter_limit(&mut self, val: f32) {
match self {
StrokeOptions::Azure(options) => options.miter_limit = val as AzFloat,
_ => unreachable!(),
}
}
fn set_line_join(&mut self, val: LineJoinStyle) {
match self {
StrokeOptions::Azure(options) => options.line_join = val.to_azure_style(),
_ => unreachable!(),
}
}
fn set_line_cap(&mut self, val: LineCapStyle) {
match self {
StrokeOptions::Azure(options) => options.line_cap = val.to_azure_style(),
_ => unreachable!(),
}
}
} }
pub struct CanvasData<'a> { pub struct CanvasData<'a> {
drawtarget: DrawTarget, drawtarget: Box<GenericDrawTarget>,
path_state: Option<PathState>, path_state: Option<PathState>,
state: CanvasPaintState<'a>, state: CanvasPaintState<'a>,
saved_states: Vec<CanvasPaintState<'a>>, saved_states: Vec<CanvasPaintState<'a>>,
@ -743,15 +779,17 @@ impl<'a> CanvasData<'a> {
image_data.into() image_data.into()
}; };
let writer = |draw_target: &DrawTarget| {
// TODO(pylbrecht) create another clousure for raqote
let writer = |draw_target: &GenericDrawTarget| {
write_image( write_image(
&draw_target, draw_target,
image_data, image_data,
source_rect.size, source_rect.size,
dest_rect, dest_rect,
smoothing_enabled, smoothing_enabled,
self.state.draw_options.composition, CompositionOp::Azure(self.state.draw_options.as_azure().composition),
self.state.draw_options.alpha, self.state.draw_options.as_azure().alpha,
); );
}; };
@ -761,9 +799,10 @@ impl<'a> CanvasData<'a> {
Size2D::new(dest_rect.size.width as f32, dest_rect.size.height as f32), Size2D::new(dest_rect.size.width as f32, dest_rect.size.height as f32),
); );
// TODO(pylbrecht) pass another closure for raqote
self.draw_with_shadow(&rect, writer); self.draw_with_shadow(&rect, writer);
} else { } else {
writer(&self.drawtarget); writer(&*self.drawtarget);
} }
} }
@ -793,34 +832,39 @@ impl<'a> CanvasData<'a> {
let draw_rect = Rect::new( let draw_rect = Rect::new(
rect.origin, rect.origin,
match self.state.fill_style { match &self.state.fill_style {
azure_hl::Pattern::Surface(ref surface) => { Pattern::Azure(pattern) => {
let surface_size = surface.size(); match pattern {
match (surface.repeat_x, surface.repeat_y) { azure_hl::Pattern::Surface(ref surface) => {
(true, true) => rect.size, let surface_size = surface.size();
(true, false) => Size2D::new(rect.size.width, surface_size.height as f32), match (surface.repeat_x, surface.repeat_y) {
(false, true) => Size2D::new(surface_size.width as f32, rect.size.height), (true, true) => rect.size,
(false, false) => { (true, false) => Size2D::new(rect.size.width, surface_size.height as f32),
Size2D::new(surface_size.width as f32, surface_size.height as f32) (false, true) => Size2D::new(surface_size.width as f32, rect.size.height),
}, (false, false) => {
Size2D::new(surface_size.width as f32, surface_size.height as f32)
},
}
}
_ => rect.size,
} }
}, },
_ => rect.size, _ => unreachable!(),
}, }
); );
if self.need_to_draw_shadow() { if self.need_to_draw_shadow() {
self.draw_with_shadow(&draw_rect, |new_draw_target: &DrawTarget| { self.draw_with_shadow(&draw_rect, |new_draw_target: &GenericDrawTarget| {
new_draw_target.fill_rect( new_draw_target.fill_rect(
&draw_rect, &draw_rect,
self.state.fill_style.to_pattern_ref(), self.state.fill_style.clone(),
Some(&self.state.draw_options), Some(&self.state.draw_options),
); );
}); });
} else { } else {
self.drawtarget.fill_rect( self.drawtarget.fill_rect(
&draw_rect, &draw_rect,
self.state.fill_style.to_pattern_ref(), self.state.fill_style.clone(),
Some(&self.state.draw_options), Some(&self.state.draw_options),
); );
} }
@ -836,38 +880,38 @@ impl<'a> CanvasData<'a> {
} }
if self.need_to_draw_shadow() { if self.need_to_draw_shadow() {
self.draw_with_shadow(&rect, |new_draw_target: &DrawTarget| { self.draw_with_shadow(&rect, |new_draw_target: &GenericDrawTarget| {
new_draw_target.stroke_rect( new_draw_target.stroke_rect(
rect, rect,
self.state.stroke_style.to_pattern_ref(), self.state.stroke_style.clone(),
&self.state.stroke_opts, &self.state.stroke_opts,
&self.state.draw_options, &self.state.draw_options,
); );
}); });
} else if rect.size.width == 0. || rect.size.height == 0. { } else if rect.size.width == 0. || rect.size.height == 0. {
let cap = match self.state.stroke_opts.line_join { let cap = match self.state.stroke_opts.as_azure().line_join {
JoinStyle::Round => CapStyle::Round, JoinStyle::Round => CapStyle::Round,
_ => CapStyle::Butt, _ => CapStyle::Butt,
}; };
let stroke_opts = azure_hl::StrokeOptions::new( let stroke_opts = StrokeOptions::Azure(azure_hl::StrokeOptions::new(
self.state.stroke_opts.line_width, self.state.stroke_opts.as_azure().line_width,
self.state.stroke_opts.line_join, self.state.stroke_opts.as_azure().line_join,
cap, cap,
self.state.stroke_opts.miter_limit, self.state.stroke_opts.as_azure().miter_limit,
self.state.stroke_opts.mDashPattern, self.state.stroke_opts.as_azure().mDashPattern,
); ));
self.drawtarget.stroke_line( self.drawtarget.stroke_line(
rect.origin, rect.origin,
rect.bottom_right(), rect.bottom_right(),
self.state.stroke_style.to_pattern_ref(), self.state.stroke_style.clone(),
&stroke_opts, &stroke_opts,
&self.state.draw_options, &self.state.draw_options,
); );
} else { } else {
self.drawtarget.stroke_rect( self.drawtarget.stroke_rect(
rect, rect,
self.state.stroke_style.to_pattern_ref(), self.state.stroke_style.clone(),
&self.state.stroke_opts, &self.state.stroke_opts,
&self.state.draw_options, &self.state.draw_options,
); );
@ -907,7 +951,7 @@ impl<'a> CanvasData<'a> {
// any transform is present. // any transform is present.
let new_state = match *self.path_state.as_ref().unwrap() { let new_state = match *self.path_state.as_ref().unwrap() {
PathState::UserSpacePath(ref path, Some(ref transform)) => { PathState::UserSpacePath(ref path, Some(ref transform)) => {
Some(path.transformed_copy_to_builder(transform)) Some(Box::new(path.as_azure().transformed_copy_to_builder(transform)))
}, },
PathState::UserSpacePath(..) | PathState::UserSpacePath(..) |
PathState::UserSpacePathBuilder(..) | PathState::UserSpacePathBuilder(..) |
@ -929,8 +973,8 @@ impl<'a> CanvasData<'a> {
return; return;
}, },
}; };
let builder = path.transformed_copy_to_builder(&inverse); let builder = Box::new(path.as_azure().transformed_copy_to_builder(&inverse));
Some(builder.finish()) Some(Path::Azure(builder.finish()))
}, },
PathState::UserSpacePathBuilder(..) | PathState::UserSpacePath(..) => None, PathState::UserSpacePathBuilder(..) | PathState::UserSpacePath(..) => None,
}; };
@ -941,7 +985,7 @@ impl<'a> CanvasData<'a> {
assert!(self.path_state.as_ref().unwrap().is_path()) assert!(self.path_state.as_ref().unwrap().is_path())
} }
fn path(&self) -> &azure_hl::Path { fn path(&self) -> &Path {
self.path_state self.path_state
.as_ref() .as_ref()
.expect("Should have called ensure_path()") .expect("Should have called ensure_path()")
@ -956,7 +1000,7 @@ impl<'a> CanvasData<'a> {
self.ensure_path(); self.ensure_path();
self.drawtarget.fill( self.drawtarget.fill(
&self.path(), &self.path(),
self.state.fill_style.to_pattern_ref(), self.state.fill_style.clone(),
&self.state.draw_options, &self.state.draw_options,
); );
} }
@ -969,7 +1013,7 @@ impl<'a> CanvasData<'a> {
self.ensure_path(); self.ensure_path();
self.drawtarget.stroke( self.drawtarget.stroke(
&self.path(), &self.path(),
self.state.stroke_style.to_pattern_ref(), self.state.stroke_style.clone(),
&self.state.stroke_opts, &self.state.stroke_opts,
&self.state.draw_options, &self.state.draw_options,
); );
@ -992,7 +1036,7 @@ impl<'a> CanvasData<'a> {
Some(PathState::UserSpacePath(ref path, ref transform)) => { Some(PathState::UserSpacePath(ref path, ref transform)) => {
let target_transform = self.drawtarget.get_transform(); let target_transform = self.drawtarget.get_transform();
let path_transform = transform.as_ref().unwrap_or(&target_transform); let path_transform = transform.as_ref().unwrap_or(&target_transform);
path.contains_point(x, y, path_transform) path.as_azure().contains_point(x, y, path_transform)
}, },
Some(_) | None => false, Some(_) | None => false,
}; };
@ -1025,14 +1069,14 @@ impl<'a> CanvasData<'a> {
&PathState::UserSpacePathBuilder(ref builder, Some(ref transform)) => { &PathState::UserSpacePathBuilder(ref builder, Some(ref transform)) => {
let path = builder.finish(); let path = builder.finish();
Some(PathState::DeviceSpacePathBuilder( Some(PathState::DeviceSpacePathBuilder(
path.transformed_copy_to_builder(transform), Box::new(path.as_azure().transformed_copy_to_builder(transform)),
)) ))
}, },
&PathState::UserSpacePath(ref path, Some(ref transform)) => Some( &PathState::UserSpacePath(ref path, Some(ref transform)) => Some(
PathState::DeviceSpacePathBuilder(path.transformed_copy_to_builder(transform)), PathState::DeviceSpacePathBuilder(Box::new(path.as_azure().transformed_copy_to_builder(transform))),
), ),
&PathState::UserSpacePath(ref path, None) => Some(PathState::UserSpacePathBuilder( &PathState::UserSpacePath(ref path, None) => Some(PathState::UserSpacePathBuilder(
path.copy_to_builder(), Box::new(path.as_azure().copy_to_builder()),
None, None,
)), )),
} }
@ -1185,31 +1229,31 @@ impl<'a> CanvasData<'a> {
} }
pub fn set_fill_style(&mut self, style: FillOrStrokeStyle) { pub fn set_fill_style(&mut self, style: FillOrStrokeStyle) {
if let Some(pattern) = style.to_azure_pattern(&self.drawtarget) { if let Some(pattern) = style.to_azure_pattern(&*self.drawtarget) {
self.state.fill_style = pattern self.state.fill_style = Pattern::Azure(pattern)
} }
} }
pub fn set_stroke_style(&mut self, style: FillOrStrokeStyle) { pub fn set_stroke_style(&mut self, style: FillOrStrokeStyle) {
if let Some(pattern) = style.to_azure_pattern(&self.drawtarget) { if let Some(pattern) = style.to_azure_pattern(&*self.drawtarget) {
self.state.stroke_style = pattern self.state.stroke_style = Pattern::Azure(pattern)
} }
} }
pub fn set_line_width(&mut self, width: f32) { pub fn set_line_width(&mut self, width: f32) {
self.state.stroke_opts.line_width = width; self.state.stroke_opts.set_line_width(width);
} }
pub fn set_line_cap(&mut self, cap: LineCapStyle) { pub fn set_line_cap(&mut self, cap: LineCapStyle) {
self.state.stroke_opts.line_cap = cap.to_azure_style(); self.state.stroke_opts.set_line_cap(cap);
} }
pub fn set_line_join(&mut self, join: LineJoinStyle) { pub fn set_line_join(&mut self, join: LineJoinStyle) {
self.state.stroke_opts.line_join = join.to_azure_style(); self.state.stroke_opts.set_line_join(join);
} }
pub fn set_miter_limit(&mut self, limit: f32) { pub fn set_miter_limit(&mut self, limit: f32) {
self.state.stroke_opts.miter_limit = limit; self.state.stroke_opts.set_miter_limit(limit);
} }
pub fn set_transform(&mut self, transform: &Transform2D<f32>) { pub fn set_transform(&mut self, transform: &Transform2D<f32>) {
@ -1229,27 +1273,28 @@ impl<'a> CanvasData<'a> {
} }
pub fn set_global_alpha(&mut self, alpha: f32) { pub fn set_global_alpha(&mut self, alpha: f32) {
self.state.draw_options.alpha = alpha; self.state.draw_options.set_alpha(alpha);
} }
pub fn set_global_composition(&mut self, op: CompositionOrBlending) { pub fn set_global_composition(&mut self, op: CompositionOrBlending) {
self.state self.state
.draw_options .draw_options
.as_azure_mut()
.set_composition_op(op.to_azure_style()); .set_composition_op(op.to_azure_style());
} }
pub fn create(size: Size2D<u64>) -> DrawTarget { pub fn create(size: Size2D<u64>) -> Box<GenericDrawTarget> {
// FIXME(nox): Why is the size made of i32 values? // FIXME(nox): Why is the size made of i32 values?
DrawTarget::new( Box::new(DrawTarget::new(
BackendType::Skia, BackendType::Skia,
size.to_i32(), size.to_i32(),
azure_hl::SurfaceFormat::B8G8R8A8, azure_hl::SurfaceFormat::B8G8R8A8,
) ))
} }
pub fn recreate(&mut self, size: Size2D<u32>) { pub fn recreate(&mut self, size: Size2D<u32>) {
self.drawtarget = CanvasData::create(Size2D::new(size.width as u64, size.height as u64)); self.drawtarget = CanvasData::create(Size2D::new(size.width as u64, size.height as u64));
self.state = CanvasPaintState::new(self.state.draw_options.antialias, CanvasBackend::Azure); self.state = CanvasPaintState::new(self.state.draw_options.as_azure().antialias, CanvasBackend::Azure);
self.saved_states.clear(); self.saved_states.clear();
// Webrender doesn't let images change size, so we clear the webrender image key. // Webrender doesn't let images change size, so we clear the webrender image key.
// TODO: there is an annying race condition here: the display list builder // TODO: there is an annying race condition here: the display list builder
@ -1267,14 +1312,14 @@ impl<'a> CanvasData<'a> {
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub fn send_pixels(&mut self, chan: IpcSender<IpcSharedMemory>) { pub fn send_pixels(&mut self, chan: IpcSender<IpcSharedMemory>) {
let data = IpcSharedMemory::from_bytes(unsafe { let data = IpcSharedMemory::from_bytes(unsafe {
self.drawtarget.snapshot().get_data_surface().data() self.drawtarget.snapshot().into_azure().get_data_surface().data()
}); });
chan.send(data).unwrap(); chan.send(data).unwrap();
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub fn send_data(&mut self, chan: IpcSender<CanvasImageData>) { pub fn send_data(&mut self, chan: IpcSender<CanvasImageData>) {
let size = self.drawtarget.get_size(); let size = self.drawtarget.get_size().into_azure();
let descriptor = webrender_api::ImageDescriptor { let descriptor = webrender_api::ImageDescriptor {
size: webrender_api::DeviceIntSize::new(size.width, size.height), size: webrender_api::DeviceIntSize::new(size.width, size.height),
@ -1285,7 +1330,7 @@ impl<'a> CanvasData<'a> {
allow_mipmaps: false, allow_mipmaps: false,
}; };
let data = webrender_api::ImageData::Raw(Arc::new(unsafe { let data = webrender_api::ImageData::Raw(Arc::new(unsafe {
self.drawtarget.snapshot().get_data_surface().data().into() self.drawtarget.snapshot().into_azure().get_data_surface().data().into()
})); }));
let mut txn = webrender_api::Transaction::new(); let mut txn = webrender_api::Transaction::new();
@ -1327,7 +1372,7 @@ impl<'a> CanvasData<'a> {
&imagedata, &imagedata,
rect.size.to_i32(), rect.size.to_i32(),
rect.size.width as i32 * 4, rect.size.width as i32 * 4,
azure_hl::SurfaceFormat::B8G8R8A8, SurfaceFormat::Azure(azure_hl::SurfaceFormat::B8G8R8A8),
) )
.unwrap(); .unwrap();
self.drawtarget.copy_surface( self.drawtarget.copy_surface(
@ -1349,19 +1394,19 @@ impl<'a> CanvasData<'a> {
self.state.shadow_blur = value; self.state.shadow_blur = value;
} }
pub fn set_shadow_color(&mut self, value: azure_hl::Color) { pub fn set_shadow_color(&mut self, value: Color) {
self.state.shadow_color = value; self.state.shadow_color = value;
} }
// https://html.spec.whatwg.org/multipage/#when-shadows-are-drawn // https://html.spec.whatwg.org/multipage/#when-shadows-are-drawn
fn need_to_draw_shadow(&self) -> bool { fn need_to_draw_shadow(&self) -> bool {
self.state.shadow_color.a != 0.0f32 && self.state.shadow_color.as_azure().a != 0.0f32 &&
(self.state.shadow_offset_x != 0.0f64 || (self.state.shadow_offset_x != 0.0f64 ||
self.state.shadow_offset_y != 0.0f64 || self.state.shadow_offset_y != 0.0f64 ||
self.state.shadow_blur != 0.0f64) self.state.shadow_blur != 0.0f64)
} }
fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> DrawTarget { fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> Box<GenericDrawTarget> {
let draw_target = self.drawtarget.create_similar_draw_target( let draw_target = self.drawtarget.create_similar_draw_target(
&Size2D::new( &Size2D::new(
source_rect.size.width as i32, source_rect.size.width as i32,
@ -1378,11 +1423,11 @@ impl<'a> CanvasData<'a> {
fn draw_with_shadow<F>(&self, rect: &Rect<f32>, draw_shadow_source: F) fn draw_with_shadow<F>(&self, rect: &Rect<f32>, draw_shadow_source: F)
where where
F: FnOnce(&DrawTarget), F: FnOnce(&GenericDrawTarget),
{ {
let shadow_src_rect = self.state.transform.transform_rect(rect); let shadow_src_rect = self.state.transform.transform_rect(rect);
let new_draw_target = self.create_draw_target_for_shadow(&shadow_src_rect); let new_draw_target = self.create_draw_target_for_shadow(&shadow_src_rect);
draw_shadow_source(&new_draw_target); draw_shadow_source(&*new_draw_target);
self.drawtarget.draw_surface_with_shadow( self.drawtarget.draw_surface_with_shadow(
new_draw_target.snapshot(), new_draw_target.snapshot(),
&Point2D::new( &Point2D::new(
@ -1395,7 +1440,7 @@ impl<'a> CanvasData<'a> {
self.state.shadow_offset_y as AzFloat, self.state.shadow_offset_y as AzFloat,
), ),
(self.state.shadow_blur / 2.0f64) as AzFloat, (self.state.shadow_blur / 2.0f64) as AzFloat,
self.state.draw_options.composition, CompositionOp::Azure(self.state.draw_options.as_azure().composition),
); );
} }
@ -1407,17 +1452,18 @@ impl<'a> CanvasData<'a> {
let canvas_rect = Rect::from_size(canvas_size); let canvas_rect = Rect::from_size(canvas_size);
if canvas_rect if canvas_rect
.intersection(&read_rect) .intersection(&read_rect)
.map_or(true, |rect| rect.is_empty()) .map_or(true, |rect| rect.is_empty())
{ {
return vec![]; return vec![];
} }
let data_surface = self.drawtarget.snapshot().get_data_surface();
let data_surface = self.drawtarget.snapshot().into_azure().get_data_surface();
pixels::rgba8_get_rect( pixels::rgba8_get_rect(
unsafe { data_surface.data() }, unsafe { data_surface.data() },
canvas_size.to_u32(), canvas_size.to_u32(),
read_rect.to_u32(), read_rect.to_u32(),
) )
.into_owned() .into_owned()
} }
} }
@ -1490,13 +1536,18 @@ impl<'a> CanvasPaintState<'a> {
} }
} }
fn is_zero_size_gradient(pattern: &azure_hl::Pattern) -> bool { fn is_zero_size_gradient(pattern: &Pattern) -> bool {
if let &azure_hl::Pattern::LinearGradient(ref gradient) = pattern { match pattern {
if gradient.is_zero_size() { Pattern::Azure(ref az_pattern) => {
return true; if let azure_hl::Pattern::LinearGradient(ref gradient) = az_pattern {
} if gradient.is_zero_size() {
return true;
}
}
false
},
_ => unreachable!(),
} }
false
} }
/// It writes an image to the destination target /// It writes an image to the destination target
@ -1506,12 +1557,12 @@ fn is_zero_size_gradient(pattern: &azure_hl::Pattern) -> bool {
/// dest_rect: Area of the destination target where the pixels will be copied /// dest_rect: Area of the destination target where the pixels will be copied
/// smoothing_enabled: It determines if smoothing is applied to the image result /// smoothing_enabled: It determines if smoothing is applied to the image result
fn write_image( fn write_image(
draw_target: &DrawTarget, draw_target: &GenericDrawTarget,
image_data: Vec<u8>, image_data: Vec<u8>,
image_size: Size2D<f64>, image_size: Size2D<f64>,
dest_rect: Rect<f64>, dest_rect: Rect<f64>,
smoothing_enabled: bool, smoothing_enabled: bool,
composition_op: azure_hl::CompositionOp, composition_op: CompositionOp,
global_alpha: f32, global_alpha: f32,
) { ) {
if image_data.is_empty() { if image_data.is_empty() {
@ -1535,12 +1586,12 @@ fn write_image(
&image_data, &image_data,
image_size, image_size,
image_size.width * 4, image_size.width * 4,
azure_hl::SurfaceFormat::B8G8R8A8, SurfaceFormat::Azure(azure_hl::SurfaceFormat::B8G8R8A8),
) )
.unwrap(); .unwrap();
let draw_surface_options = DrawSurfaceOptions::new(filter, true); let draw_surface_options = DrawSurfaceOptions::Azure(azure_hl::DrawSurfaceOptions::new(filter, true));
let draw_options = let draw_options =
azure_hl::DrawOptions::new(global_alpha, composition_op, AntialiasMode::None); DrawOptions::Azure(azure_hl::DrawOptions::new(global_alpha, composition_op.into_azure(), AntialiasMode::None));
draw_target.draw_surface( draw_target.draw_surface(
source_surface, source_surface,
dest_rect.to_azure_style(), dest_rect.to_azure_style(),
@ -1693,23 +1744,23 @@ impl ToAzureStyle for CompositionOrBlending {
} }
pub trait ToAzurePattern { pub trait ToAzurePattern {
fn to_azure_pattern(&self, drawtarget: &DrawTarget) -> Option<azure_hl::Pattern>; fn to_azure_pattern(&self, drawtarget: &GenericDrawTarget) -> Option<azure_hl::Pattern>;
} }
impl ToAzurePattern for FillOrStrokeStyle { impl ToAzurePattern for FillOrStrokeStyle {
fn to_azure_pattern(&self, drawtarget: &DrawTarget) -> Option<azure_hl::Pattern> { fn to_azure_pattern(&self, drawtarget: &GenericDrawTarget) -> Option<azure_hl::Pattern> {
Some(match *self { Some(match *self {
FillOrStrokeStyle::Color(ref color) => { FillOrStrokeStyle::Color(ref color) => {
azure_hl::Pattern::Color(ColorPattern::new(color.to_azure_style())) azure_hl::Pattern::Color(ColorPattern::new(color.to_azure_style()))
}, },
FillOrStrokeStyle::LinearGradient(ref linear_gradient_style) => { FillOrStrokeStyle::LinearGradient(ref linear_gradient_style) => {
let gradient_stops: Vec<azure_hl::GradientStop> = linear_gradient_style let gradient_stops: Vec<GradientStop> = linear_gradient_style
.stops .stops
.iter() .iter()
.map(|s| azure_hl::GradientStop { .map(|s| GradientStop::Azure(azure_hl::GradientStop {
offset: s.offset as AzFloat, offset: s.offset as AzFloat,
color: s.color.to_azure_style(), color: s.color.to_azure_style(),
}) }))
.collect(); .collect();
azure_hl::Pattern::LinearGradient(LinearGradientPattern::new( azure_hl::Pattern::LinearGradient(LinearGradientPattern::new(
@ -1721,18 +1772,18 @@ impl ToAzurePattern for FillOrStrokeStyle {
linear_gradient_style.x1 as AzFloat, linear_gradient_style.x1 as AzFloat,
linear_gradient_style.y1 as AzFloat, linear_gradient_style.y1 as AzFloat,
), ),
drawtarget.create_gradient_stops(&gradient_stops, azure_hl::ExtendMode::Clamp), drawtarget.create_gradient_stops(gradient_stops, ExtendMode::Azure(azure_hl::ExtendMode::Clamp)).into_azure(),
&Transform2D::identity(), &Transform2D::identity(),
)) ))
}, },
FillOrStrokeStyle::RadialGradient(ref radial_gradient_style) => { FillOrStrokeStyle::RadialGradient(ref radial_gradient_style) => {
let gradient_stops: Vec<azure_hl::GradientStop> = radial_gradient_style let gradient_stops: Vec<GradientStop> = radial_gradient_style
.stops .stops
.iter() .iter()
.map(|s| azure_hl::GradientStop { .map(|s| GradientStop::Azure(azure_hl::GradientStop {
offset: s.offset as AzFloat, offset: s.offset as AzFloat,
color: s.color.to_azure_style(), color: s.color.to_azure_style(),
}) }))
.collect(); .collect();
azure_hl::Pattern::RadialGradient(RadialGradientPattern::new( azure_hl::Pattern::RadialGradient(RadialGradientPattern::new(
@ -1746,7 +1797,7 @@ impl ToAzurePattern for FillOrStrokeStyle {
), ),
radial_gradient_style.r0 as AzFloat, radial_gradient_style.r0 as AzFloat,
radial_gradient_style.r1 as AzFloat, radial_gradient_style.r1 as AzFloat,
drawtarget.create_gradient_stops(&gradient_stops, azure_hl::ExtendMode::Clamp), drawtarget.create_gradient_stops(gradient_stops, ExtendMode::Azure(azure_hl::ExtendMode::Clamp)).into_azure(),
&Transform2D::identity(), &Transform2D::identity(),
)) ))
}, },
@ -1756,8 +1807,8 @@ impl ToAzurePattern for FillOrStrokeStyle {
// FIXME(nox): Why are those i32 values? // FIXME(nox): Why are those i32 values?
surface_style.surface_size.to_i32(), surface_style.surface_size.to_i32(),
surface_style.surface_size.width as i32 * 4, surface_style.surface_size.width as i32 * 4,
azure_hl::SurfaceFormat::B8G8R8A8, SurfaceFormat::Azure(azure_hl::SurfaceFormat::B8G8R8A8),
)?; )?.into_azure();
azure_hl::Pattern::Surface(SurfacePattern::new( azure_hl::Pattern::Surface(SurfacePattern::new(
source_surface.azure_source_surface, source_surface.azure_source_surface,
surface_style.repeat_x, surface_style.repeat_x,

View file

@ -197,7 +197,7 @@ impl<'a> CanvasPaintThread<'a> {
Canvas2dMsg::SetShadowBlur(value) => self.canvas(canvas_id).set_shadow_blur(value), Canvas2dMsg::SetShadowBlur(value) => self.canvas(canvas_id).set_shadow_blur(value),
Canvas2dMsg::SetShadowColor(ref color) => self Canvas2dMsg::SetShadowColor(ref color) => self
.canvas(canvas_id) .canvas(canvas_id)
.set_shadow_color(color.to_azure_style()), .set_shadow_color(Color::Azure(color.to_azure_style())),
} }
} }