From 3243f1753bca71c5b14d9f43c34aad51e59a01a4 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 29 May 2019 14:50:45 -0400 Subject: [PATCH] Add cargo feature to control canvas backend. --- Cargo.lock | 6 +- components/canvas/Cargo.toml | 7 +- components/canvas/azure_backend.rs | 744 +++++++++++++++++ components/canvas/canvas_data.rs | 967 ++++------------------- components/canvas/canvas_paint_thread.rs | 10 +- components/canvas/lib.rs | 6 + components/canvas/raqote_backend.rs | 124 +++ components/constellation/Cargo.toml | 6 +- components/servo/Cargo.toml | 10 +- ports/glutin/Cargo.toml | 10 +- python/servo/build_commands.py | 3 + 11 files changed, 1077 insertions(+), 816 deletions(-) create mode 100644 components/canvas/azure_backend.rs create mode 100644 components/canvas/raqote_backend.rs diff --git a/Cargo.lock b/Cargo.lock index 63ce2d499a0..62676c779a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1258,7 +1258,7 @@ dependencies = [ [[package]] name = "failure" -version = "0.1.3" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1329,7 +1329,7 @@ dependencies = [ "dirs 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dwrote 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.19.8 (registry+https://github.com/rust-lang/crates.io-index)", - "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "float-ord 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "freetype 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5692,7 +5692,7 @@ dependencies = [ "checksum euclid 0.19.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7a4719a544a67ed3fc33784c2bd2c6581663dfe83b719a6ae05c6dabc3b51c73" "checksum euclid_macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdcb84c18ea5037a1c5a23039b4ff29403abce2e0d6b1daa11cf0bde2b30be15" "checksum expat-sys 2.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "658f19728920138342f68408b7cf7644d90d4784353d8ebc32e7e8663dbe45fa" -"checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7" +"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33" diff --git a/components/canvas/Cargo.toml b/components/canvas/Cargo.toml index c91302f02b3..065ed01ca75 100644 --- a/components/canvas/Cargo.toml +++ b/components/canvas/Cargo.toml @@ -11,10 +11,13 @@ name = "canvas" path = "lib.rs" [features] +azure_backend = ["azure"] +default = ["azure_backend"] webgl_backtrace = ["canvas_traits/webgl_backtrace"] +raqote_backend = ["raqote"] [dependencies] -azure = {git = "https://github.com/servo/rust-azure"} +azure = {git = "https://github.com/servo/rust-azure", optional = true} byteorder = "1" canvas_traits = {path = "../canvas_traits"} compositing = {path = "../compositing"} @@ -27,7 +30,7 @@ ipc-channel = "0.11" log = "0.4" num-traits = "0.2" offscreen_gl_context = {version = "0.22", features = ["serde", "osmesa"]} -raqote = {git = "https://github.com/jrmuizel/raqote"} +raqote = {git = "https://github.com/jrmuizel/raqote", optional = true} pixels = {path = "../pixels"} serde_bytes = "0.10" servo_config = {path = "../config"} diff --git a/components/canvas/azure_backend.rs b/components/canvas/azure_backend.rs new file mode 100644 index 00000000000..8727751d5a6 --- /dev/null +++ b/components/canvas/azure_backend.rs @@ -0,0 +1,744 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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 cssparser::RGBA; +use crate::canvas_data::{ + Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, GenericDrawTarget, + GenericPathBuilder, Path, Pattern, StrokeOptions, +}; +use crate::canvas_paint_thread::AntialiasMode; +use azure::azure::{AzFloat, AzGradientStop, AzIntSize, AzPoint}; +use azure::azure_hl; +use azure::azure_hl::SurfacePattern; +use azure::azure_hl::{AsAzurePoint, CapStyle, JoinStyle}; +use azure::azure_hl::{BackendType, ColorPattern, DrawTarget}; +use azure::azure_hl::{LinearGradientPattern, RadialGradientPattern}; +use euclid::{Point2D, Rect, Size2D, Transform2D, Vector2D}; + +pub struct AzureBackend; + +impl Backend for AzureBackend { + fn get_composition_op(&self, opts: &DrawOptions) -> CompositionOp { + CompositionOp::Azure(opts.as_azure().composition) + } + + fn need_to_draw_shadow(&self, color: &Color) -> bool { + self.state.shadow_color.as_azure().a != 0.0f32 + } + + fn size_from_pattern(&self, rect: &Rect, pattern: &Pattern) -> Option> { + match pattern { + Pattern::Azure(azure_hl::Pattern::Surface(ref surface)) => { + let surface_size = surface.size(); + let size = match (surface.repeat_x, surface.repeat_y) { + (true, true) => rect.size, + (true, false) => Size2D::new(rect.size.width, 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) + }, + }; + Some(size) + } + Pattern::Azure(_) => None + } + } + + fn set_shadow_color<'a>(&mut self, color: RGBA, state: &mut CanvasPaintState<'a>) { + state.shadow_color = Color::Azure(color.to_azure_style()); + } + + fn set_fill_style<'a>( + &mut self, + style: FillOrStrokeStyle, + state: &mut CanvasPaintState<'a>, + drawtarget: &GenericDrawTarget, + ) { + if let Some(pattern) = style.to_azure_pattern(drawtarget) { + state.fill_style = Pattern::Azure(pattern) + } + } + + fn set_stroke_style<'a>( + &mut self, + style: FillOrStrokeStyle, + state: &mut CanvasPaintState<'a>, + drawtarget: &GenericDrawTarget) + { + if let Some(pattern) = style.to_azure_pattern(drawtarget) { + state.stroke_style = Pattern::Azure(pattern) + } + } + + fn set_global_composition<'a>(&mut self, op: CompositionOrBlending, state: &mut CanvasPaintState<'a>) { + state + .draw_options + .as_azure_mut() + .set_composition_op(op.to_azure_style()); + } + + fn create_drawtarget(&self, size: Size2D) -> Box { + // FIXME(nox): Why is the size made of i32 values? + Box::new(DrawTarget::new( + BackendType::Skia, + size.to_i32(), + azure_hl::SurfaceFormat::B8G8R8A8, + )) + } + + fn recreate_paint_state<'a>(&self, state: &CanvasPaintState<'a>) -> CanvasPaintState<'a> { + CanvasPaintState::new(AntialiasMode::from_azure( + self.state.draw_options.as_azure().antialias + )) + } +} + +impl<'a> CanvasPaintState<'a> { + pub fn new(antialias: AntialiasMode) -> CanvasPaintState<'a> { + CanvasPaintState { + draw_options: DrawOptions::Azure(azure_hl::DrawOptions::new(1.0, azure_hl::CompositionOp::Over, antialias.into_azure())), + fill_style: Pattern::Azure(azure_hl::Pattern::Color(ColorPattern::new(azure_hl::Color::black()))), + stroke_style: Pattern::Azure(azure_hl::Pattern::Color(ColorPattern::new(azure_hl::Color::black()))), + stroke_opts: StrokeOptions::Azure(azure_hl::StrokeOptions::new( + 1.0, + JoinStyle::MiterOrBevel, + CapStyle::Butt, + 10.0, + &[], + )), + transform: Transform2D::identity(), + shadow_offset_x: 0.0, + shadow_offset_y: 0.0, + shadow_blur: 0.0, + shadow_color: Color::Azure(azure_hl::Color::transparent()), + } + } +} + +impl GenericPathBuilder for azure_hl::PathBuilder { + fn arc( + &self, + origin: Point2D, + radius: f32, + start_angle: f32, + end_angle: f32, + anticlockwise: bool, + ) { + self.arc( + origin as Point2D, + radius as AzFloat, + start_angle as AzFloat, + end_angle as AzFloat, + anticlockwise, + ); + } + fn bezier_curve_to( + &self, + control_point1: &Point2D, + control_point2: &Point2D, + control_point3: &Point2D, + ) { + self.bezier_curve_to( + control_point1 as &Point2D, + control_point2 as &Point2D, + control_point3 as &Point2D, + ); + } + fn close(&self) { + self.close(); + } + fn ellipse( + &self, + origin: Point2D, + radius_x: f32, + radius_y: f32, + rotation_angle: f32, + start_angle: f32, + end_angle: f32, + anticlockwise: bool, + ) { + self.ellipse( + origin as Point2D, + radius_x as AzFloat, + radius_y as AzFloat, + rotation_angle as AzFloat, + start_angle as AzFloat, + end_angle as AzFloat, + anticlockwise, + ); + } + fn get_current_point(&self) -> Point2D { + let AzPoint { x, y } = self.get_current_point(); + Point2D::new(x as f32, y as f32) + } + fn line_to(&self, point: Point2D) { + self.line_to(point as Point2D); + } + fn move_to(&self, point: Point2D) { + self.move_to(point as Point2D); + } + fn quadratic_curve_to(&self, control_point: &Point2D, end_point: &Point2D) { + self.quadratic_curve_to( + control_point as &Point2D, + end_point as &Point2D, + ); + } + fn finish(&self) -> Path { + Path::Azure(self.finish()) + } +} + +impl GenericDrawTarget for azure_hl::DrawTarget { + fn clear_rect(&self, rect: &Rect) { + self.clear_rect(rect as &Rect); + } + + fn copy_surface(&self, surface: SourceSurface, source: Rect, destination: Point2D) { + self.copy_surface(surface.into_azure(), source, destination); + } + + fn create_gradient_stops( + &self, + gradient_stops: Vec, + extend_mode: ExtendMode, + ) -> GradientStops { + let gradient_stops: Vec = + gradient_stops.into_iter().map(|x| x.into_azure()).collect(); + GradientStops::Azure(self.create_gradient_stops(&gradient_stops, extend_mode.into_azure())) + } + + fn create_path_builder(&self) -> Box { + Box::new(self.create_path_builder()) + } + + fn create_similar_draw_target( + &self, + size: &Size2D, + format: SurfaceFormat, + ) -> Box { + Box::new(self.create_similar_draw_target(size, format.into_azure())) + } + fn create_source_surface_from_data( + &self, + data: &[u8], + size: Size2D, + stride: i32, + ) -> Option { + self.create_source_surface_from_data(data, size, stride, azure_hl::SurfaceFormat::B8G8R8A8) + .map(|s| SourceSurface::Azure(s)) + } + fn draw_surface( + &self, + surface: SourceSurface, + dest: Rect, + source: Rect, + filter: Filter, + draw_options: &DrawOptions, + ) { + let surf_options = azure_hl::DrawSurfaceOptions::new(filter.as_azure(), true); + let draw_options = azure_hl::DrawOptions::new( + draw_options.as_azure().alpha, + draw_options.as_azure().composition.into_azure(), + azure_hl::AntialiasMode::None + ); + self.draw_surface( + surface.into_azure(), + dest as Rect, + source as Rect, + surf_options.into_azure(), + options.into_azure(), + ); + } + fn draw_surface_with_shadow( + &self, + surface: SourceSurface, + dest: &Point2D, + color: &Color, + offset: &Vector2D, + sigma: f32, + operator: CompositionOp, + ) { + self.draw_surface_with_shadow( + surface.into_azure(), + dest as &Point2D, + color.as_azure(), + offset as &Vector2D, + sigma as AzFloat, + operator.into_azure(), + ); + } + fn fill(&self, path: &Path, pattern: Pattern, draw_options: &DrawOptions) { + self.fill( + path.as_azure(), + pattern.as_azure().to_pattern_ref(), + draw_options.as_azure(), + ); + } + fn fill_rect(&self, rect: &Rect, pattern: Pattern, draw_options: Option<&DrawOptions>) { + self.fill_rect( + rect as &Rect, + pattern.as_azure().to_pattern_ref(), + draw_options.map(|x| x.as_azure()), + ); + } + fn get_format(&self) -> SurfaceFormat { + SurfaceFormat::Azure(self.get_format()) + } + fn get_size(&self) -> Size2D { + let size = self.get_size(); + Size2D::new(size.width, size.height) + } + fn get_transform(&self) -> Transform2D { + self.get_transform() as Transform2D + } + fn pop_clip(&self) { + self.pop_clip(); + } + fn push_clip(&self, path: &Path) { + self.push_clip(path.as_azure()); + } + fn set_transform(&self, matrix: &Transform2D) { + self.set_transform(matrix as &Transform2D); + } + fn snapshot(&self) -> SourceSurface { + SourceSurface::Azure(self.snapshot()) + } + fn stroke( + &self, + path: &Path, + pattern: Pattern, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, + ) { + self.stroke( + path.as_azure(), + pattern.as_azure().to_pattern_ref(), + stroke_options.as_azure(), + draw_options.as_azure(), + ); + } + fn stroke_line( + &self, + start: Point2D, + end: Point2D, + pattern: Pattern, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, + ) { + let stroke_options = stroke_options.as_azure(); + let cap = match stroke_options.line_join { + JoinStyle::Round => CapStyle::Round, + _ => CapStyle::Butt, + }; + + let stroke_opts = azure_hl::StrokeOptions::new( + stroke_options.line_width, + stroke_options.line_join, + cap, + stroke_options.miter_limit, + stroke_options.mDashPattern, + ); + + self.stroke_line( + start, + end, + pattern.as_azure().to_pattern_ref(), + stroke_options.as_azure(), + draw_options.as_azure(), + ); + } + fn stroke_rect( + &self, + rect: &Rect, + pattern: Pattern, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, + ) { + self.stroke_rect( + rect as &Rect, + pattern.as_azure().to_pattern_ref(), + stroke_options.as_azure(), + draw_options.as_azure(), + ); + } + + fn snapshot_data(&self) -> &[u8] { + unsafe { + self.snapshot().get_data_surface().data() + } + } +} + +impl AntialiasMode { + fn into_azure(self) -> azure_hl::AntialiasMode { + match self { + AntialiasMode::Default => azure_hl::AntialiasMode::Default, + AntialiasMode::None => azure_hl::AntialiasMode::None, + } + } + + fn from_azure(val: azure_hl::AntialiasMode) { + match val { + azure_hl::AntialiasMode::Default => AntialiasMode::Default, + azure_hl::AntialiasMode::None => AntialiasMode::None, + } + } +} + +impl ExtendMode { + fn into_azure(self) -> azure_hl::ExtendMode { + match self { + ExtendMode::Azure(m) => m, + } + } +} + +impl GradientStop { + fn into_azure(self) -> AzGradientStop { + match self { + GradientStop::Azure(s) => s, + } + } +} + +impl GradientStops { + fn into_azure(self) -> azure_hl::GradientStops { + match self { + GradientStops::Azure(s) => s, + } + } +} + +impl Color { + fn as_azure(&self) -> &azure_hl::Color { + match self { + Color::Azure(s) => s, + } + } +} + +impl CompositionOp { + fn into_azure(self) -> azure_hl::CompositionOp { + match self { + CompositionOp::Azure(s) => s, + } + } +} + +impl SurfaceFormat { + fn into_azure(self) -> azure_hl::SurfaceFormat { + match self { + SurfaceFormat::Azure(s) => s, + } + } +} + +impl SourceSurface { + fn into_azure(self) -> azure_hl::SourceSurface { + match self { + SourceSurface::Azure(s) => s, + } + } +} + +impl IntSize { + fn into_azure(self) -> AzIntSize { + match self { + IntSize::Azure(s) => s, + } + } +} + +impl Path { + fn as_azure(&self) -> &azure_hl::Path { + match self { + Path::Azure(p) => p, + } + } +} + +impl Pattern { + fn as_azure(&self) -> &azure_hl::Pattern { + match self { + Pattern::Azure(p) => p, + } + } +} + +impl DrawSurfaceOptions { + fn into_azure(self) -> azure_hl::DrawSurfaceOptions { + match self { + DrawSurfaceOptions::Azure(options) => options, + } + } +} + +impl DrawOptions { + fn as_azure(&self) -> &azure_hl::DrawOptions { + match self { + DrawOptions::Azure(options) => options, + } + } + fn as_azure_mut(&mut self) -> &mut azure_hl::DrawOptions { + match self { + DrawOptions::Azure(options) => options, + } + } + fn into_azure(self) -> azure_hl::DrawOptions { + match self { + DrawOptions::Azure(options) => options, + } + } + pub fn set_alpha(&mut self, val: f32) { + match self { + DrawOptions::Azure(options) => options.alpha = val as AzFloat, + } + } +} + +impl<'a> StrokeOptions<'a> { + pub fn as_azure(&self) -> &azure_hl::StrokeOptions<'a> { + match self { + StrokeOptions::Azure(options) => options, + } + } + pub fn set_line_width(&mut self, val: f32) { + match self { + StrokeOptions::Azure(options) => options.line_width = val as AzFloat, + } + } + pub fn set_miter_limit(&mut self, val: f32) { + match self { + StrokeOptions::Azure(options) => options.miter_limit = val as AzFloat, + } + } + pub fn set_line_join(&mut self, val: LineJoinStyle) { + match self { + StrokeOptions::Azure(options) => options.line_join = val.to_azure_style(), + } + } + pub fn set_line_cap(&mut self, val: LineCapStyle) { + match self { + StrokeOptions::Azure(options) => options.line_cap = val.to_azure_style(), + } + } +} + +pub trait ToAzureStyle { + type Target; + fn to_azure_style(self) -> Self::Target; +} + +impl ToAzureStyle for Rect { + type Target = Rect; + + fn to_azure_style(self) -> Rect { + Rect::new( + Point2D::new(self.origin.x as f32, self.origin.y as f32), + Size2D::new(self.size.width as f32, self.size.height as f32), + ) + } +} + +impl ToAzureStyle for LineCapStyle { + type Target = CapStyle; + + fn to_azure_style(self) -> CapStyle { + match self { + LineCapStyle::Butt => CapStyle::Butt, + LineCapStyle::Round => CapStyle::Round, + LineCapStyle::Square => CapStyle::Square, + } + } +} + +impl ToAzureStyle for LineJoinStyle { + type Target = JoinStyle; + + fn to_azure_style(self) -> JoinStyle { + match self { + LineJoinStyle::Round => JoinStyle::Round, + LineJoinStyle::Bevel => JoinStyle::Bevel, + LineJoinStyle::Miter => JoinStyle::Miter, + } + } +} + +impl ToAzureStyle for CompositionStyle { + type Target = azure_hl::CompositionOp; + + fn to_azure_style(self) -> azure_hl::CompositionOp { + match self { + CompositionStyle::SrcIn => azure_hl::CompositionOp::In, + CompositionStyle::SrcOut => azure_hl::CompositionOp::Out, + CompositionStyle::SrcOver => azure_hl::CompositionOp::Over, + CompositionStyle::SrcAtop => azure_hl::CompositionOp::Atop, + CompositionStyle::DestIn => azure_hl::CompositionOp::DestIn, + CompositionStyle::DestOut => azure_hl::CompositionOp::DestOut, + CompositionStyle::DestOver => azure_hl::CompositionOp::DestOver, + CompositionStyle::DestAtop => azure_hl::CompositionOp::DestAtop, + CompositionStyle::Copy => azure_hl::CompositionOp::Source, + CompositionStyle::Lighter => azure_hl::CompositionOp::Add, + CompositionStyle::Xor => azure_hl::CompositionOp::Xor, + } + } +} + +impl ToAzureStyle for BlendingStyle { + type Target = azure_hl::CompositionOp; + + fn to_azure_style(self) -> azure_hl::CompositionOp { + match self { + BlendingStyle::Multiply => azure_hl::CompositionOp::Multiply, + BlendingStyle::Screen => azure_hl::CompositionOp::Screen, + BlendingStyle::Overlay => azure_hl::CompositionOp::Overlay, + BlendingStyle::Darken => azure_hl::CompositionOp::Darken, + BlendingStyle::Lighten => azure_hl::CompositionOp::Lighten, + BlendingStyle::ColorDodge => azure_hl::CompositionOp::ColorDodge, + BlendingStyle::ColorBurn => azure_hl::CompositionOp::ColorBurn, + BlendingStyle::HardLight => azure_hl::CompositionOp::HardLight, + BlendingStyle::SoftLight => azure_hl::CompositionOp::SoftLight, + BlendingStyle::Difference => azure_hl::CompositionOp::Difference, + BlendingStyle::Exclusion => azure_hl::CompositionOp::Exclusion, + BlendingStyle::Hue => azure_hl::CompositionOp::Hue, + BlendingStyle::Saturation => azure_hl::CompositionOp::Saturation, + BlendingStyle::Color => azure_hl::CompositionOp::Color, + BlendingStyle::Luminosity => azure_hl::CompositionOp::Luminosity, + } + } +} + +impl ToAzureStyle for CompositionOrBlending { + type Target = azure_hl::CompositionOp; + + fn to_azure_style(self) -> azure_hl::CompositionOp { + match self { + CompositionOrBlending::Composition(op) => op.to_azure_style(), + CompositionOrBlending::Blending(op) => op.to_azure_style(), + } + } +} + +pub trait ToAzurePattern { + fn to_azure_pattern(&self, drawtarget: &GenericDrawTarget) -> Option; +} + +impl ToAzurePattern for FillOrStrokeStyle { + fn to_azure_pattern(&self, drawtarget: &GenericDrawTarget) -> Option { + Some(match *self { + FillOrStrokeStyle::Color(ref color) => { + azure_hl::Pattern::Color(ColorPattern::new(color.to_azure_style())) + }, + FillOrStrokeStyle::LinearGradient(ref linear_gradient_style) => { + let gradient_stops: Vec = linear_gradient_style + .stops + .iter() + .map(|s| GradientStop::Azure(azure_hl::GradientStop { + offset: s.offset as f32, + color: s.color.to_azure_style(), + })) + .collect(); + + azure_hl::Pattern::LinearGradient(LinearGradientPattern::new( + &Point2D::new( + linear_gradient_style.x0 as f32, + linear_gradient_style.y0 as f32, + ), + &Point2D::new( + linear_gradient_style.x1 as f32, + linear_gradient_style.y1 as f32, + ), + drawtarget.create_gradient_stops(gradient_stops, ExtendMode::Azure(azure_hl::ExtendMode::Clamp)).into_azure(), + &Transform2D::identity(), + )) + }, + FillOrStrokeStyle::RadialGradient(ref radial_gradient_style) => { + let gradient_stops: Vec = radial_gradient_style + .stops + .iter() + .map(|s| GradientStop::Azure(azure_hl::GradientStop { + offset: s.offset as f32, + color: s.color.to_azure_style(), + })) + .collect(); + + azure_hl::Pattern::RadialGradient(RadialGradientPattern::new( + &Point2D::new( + radial_gradient_style.x0 as f32, + radial_gradient_style.y0 as f32, + ), + &Point2D::new( + radial_gradient_style.x1 as f32, + radial_gradient_style.y1 as f32, + ), + radial_gradient_style.r0 as f32, + radial_gradient_style.r1 as f32, + drawtarget.create_gradient_stops(gradient_stops, ExtendMode::Azure(azure_hl::ExtendMode::Clamp)).into_azure(), + &Transform2D::identity(), + )) + }, + FillOrStrokeStyle::Surface(ref surface_style) => { + let source_surface = drawtarget.create_source_surface_from_data( + &surface_style.surface_data, + // FIXME(nox): Why are those i32 values? + surface_style.surface_size.to_i32(), + surface_style.surface_size.width as i32 * 4, + )?.into_azure(); + azure_hl::Pattern::Surface(SurfacePattern::new( + source_surface.azure_source_surface, + surface_style.repeat_x, + surface_style.repeat_y, + &Transform2D::identity(), + )) + }, + }) + } +} + +impl ToAzureStyle for RGBA { + type Target = azure_hl::Color; + + fn to_azure_style(self) -> azure_hl::Color { + azure_hl::Color::rgba( + self.red_f32() as f32, + self.green_f32() as f32, + self.blue_f32() as f32, + self.alpha_f32() as f32, + ) + } +} + +impl Pattern { + pub fn is_zero_size_gradient(&self) -> bool { + match *self { + Pattern::Azure(azure_hl::Pattern::LinearGradient(ref az_pattern)) => { + gradient.is_zero_size() + } + _ => false, + } + } +} + +impl Filter { + fn as_azure(&self) -> azure_hl::Filter { + match *self { + Filter::Linear => azure_hl::Filter::Linear, + Filter::Point => azure_hl::Filter::Point, + } + } +} + +impl Path { + pub fn transformed_copy_to_builder(&self, transform: &Transform2D) -> Box { + Box::new(self.as_azure().transformed_copy_to_builder(transform)) + } + + pub fn contains_point(&self, x: f64, y: f64, path_transform: &Transform2D) -> bool { + self.as_azure().contains_point(x, y, path_transform) + } + + pub fn copy_to_builder(&self) -> Box { + self.as_azure().copy_to_builder() + } +} diff --git a/components/canvas/canvas_data.rs b/components/canvas/canvas_data.rs index bb14e223149..a2312fea1ca 100644 --- a/components/canvas/canvas_data.rs +++ b/components/canvas/canvas_data.rs @@ -2,18 +2,13 @@ * 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 azure::azure::{AzFloat, AzGradientStop, AzIntSize, AzPoint}; -use azure::azure_hl; -use azure::azure_hl::SurfacePattern; -use azure::azure_hl::{AntialiasMode, AsAzurePoint, CapStyle, JoinStyle}; -use azure::azure_hl::{BackendType, DrawTarget}; -use azure::azure_hl::{ColorPattern, Filter}; -use azure::azure_hl::{LinearGradientPattern, RadialGradientPattern}; +use crate::canvas_paint_thread::AntialiasMode; use canvas_traits::canvas::*; use cssparser::RGBA; use euclid::{Point2D, Rect, Size2D, Transform2D, Vector2D}; use ipc_channel::ipc::{IpcSender, IpcSharedMemory}; use num_traits::ToPrimitive; +use std::marker::PhantomData; use std::mem; use std::sync::Arc; use webrender::api::DirtyRect; @@ -57,9 +52,21 @@ impl PathState { } } +pub trait Backend { + fn get_composition_op(&self, opts: &DrawOptions) -> CompositionOp; + fn need_to_draw_shadow(&self, color: &Color) -> bool; + fn set_shadow_color<'a>(&mut self, color: RGBA, state: &mut CanvasPaintState<'a>); + fn set_fill_style<'a>(&mut self, style: FillOrStrokeStyle, state: &mut CanvasPaintState<'a>, drawtarget: &GenericDrawTarget); + fn set_stroke_style<'a>(&mut self, style: FillOrStrokeStyle, state: &mut CanvasPaintState<'a>, drawtarget: &GenericDrawTarget); + fn set_global_composition<'a>(&mut self, op: CompositionOrBlending, state: &mut CanvasPaintState<'a>); + fn create_drawtarget(&self, size: Size2D) -> Box; + fn recreate_paint_state<'a>(&self, state: &CanvasPaintState<'a>) -> CanvasPaintState<'a>; + fn size_from_pattern(&self, rect: &Rect, pattern: &Pattern) -> Option>; +} + /// A generic PathBuilder that abstracts the interface for /// azure's and raqote's PathBuilder. -trait GenericPathBuilder { +pub trait GenericPathBuilder { fn arc( &self, origin: Point2D, @@ -92,93 +99,20 @@ trait GenericPathBuilder { fn finish(&self) -> Path; } -impl GenericPathBuilder for azure_hl::PathBuilder { - fn arc( - &self, - origin: Point2D, - radius: f32, - start_angle: f32, - end_angle: f32, - anticlockwise: bool, - ) { - self.arc( - origin as Point2D, - radius as AzFloat, - start_angle as AzFloat, - end_angle as AzFloat, - anticlockwise, - ); - } - fn bezier_curve_to( - &self, - control_point1: &Point2D, - control_point2: &Point2D, - control_point3: &Point2D, - ) { - self.bezier_curve_to( - control_point1 as &Point2D, - control_point2 as &Point2D, - control_point3 as &Point2D, - ); - } - fn close(&self) { - self.close(); - } - fn ellipse( - &self, - origin: Point2D, - radius_x: f32, - radius_y: f32, - rotation_angle: f32, - start_angle: f32, - end_angle: f32, - anticlockwise: bool, - ) { - self.ellipse( - origin as Point2D, - radius_x as AzFloat, - radius_y as AzFloat, - rotation_angle as AzFloat, - start_angle as AzFloat, - end_angle as AzFloat, - anticlockwise, - ); - } - fn get_current_point(&self) -> Point2D { - let AzPoint { x, y } = self.get_current_point(); - Point2D::new(x as f32, y as f32) - } - fn line_to(&self, point: Point2D) { - self.line_to(point as Point2D); - } - fn move_to(&self, point: Point2D) { - self.move_to(point as Point2D); - } - fn quadratic_curve_to(&self, control_point: &Point2D, end_point: &Point2D) { - self.quadratic_curve_to( - control_point as &Point2D, - end_point as &Point2D, - ); - } - fn finish(&self) -> Path { - Path::Azure(self.finish()) - } -} - /// 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. struct PathBuilderRef<'a> { builder: &'a Box, - transform: Transform2D, + transform: Transform2D, } impl<'a> PathBuilderRef<'a> { - fn line_to(&self, pt: &Point2D) { + fn line_to(&self, pt: &Point2D) { let pt = self.transform.transform_point(pt); self.builder.line_to(pt); } - fn move_to(&self, pt: &Point2D) { + fn move_to(&self, pt: &Point2D) { let pt = self.transform.transform_point(pt); self.builder.move_to(pt); } @@ -202,7 +136,7 @@ impl<'a> PathBuilderRef<'a> { self.builder.close(); } - fn quadratic_curve_to(&self, cp: &Point2D, endpoint: &Point2D) { + fn quadratic_curve_to(&self, cp: &Point2D, endpoint: &Point2D) { self.builder.quadratic_curve_to( &self.transform.transform_point(cp), &self.transform.transform_point(endpoint), @@ -211,9 +145,9 @@ impl<'a> PathBuilderRef<'a> { fn bezier_curve_to( &self, - cp1: &Point2D, - cp2: &Point2D, - endpoint: &Point2D, + cp1: &Point2D, + cp2: &Point2D, + endpoint: &Point2D, ) { self.builder.bezier_curve_to( &self.transform.transform_point(cp1), @@ -224,10 +158,10 @@ impl<'a> PathBuilderRef<'a> { fn arc( &self, - center: &Point2D, - radius: AzFloat, - start_angle: AzFloat, - end_angle: AzFloat, + center: &Point2D, + radius: f32, + start_angle: f32, + end_angle: f32, ccw: bool, ) { let center = self.transform.transform_point(center); @@ -237,12 +171,12 @@ impl<'a> PathBuilderRef<'a> { pub fn ellipse( &self, - center: &Point2D, - radius_x: AzFloat, - radius_y: AzFloat, - rotation_angle: AzFloat, - start_angle: AzFloat, - end_angle: AzFloat, + center: &Point2D, + radius_x: f32, + radius_y: f32, + rotation_angle: f32, + start_angle: f32, + end_angle: f32, ccw: bool, ) { let center = self.transform.transform_point(center); @@ -257,7 +191,7 @@ impl<'a> PathBuilderRef<'a> { ); } - fn current_point(&self) -> Option> { + fn current_point(&self) -> Option> { let inverse = match self.transform.inverse() { Some(i) => i, None => return None, @@ -270,7 +204,7 @@ impl<'a> PathBuilderRef<'a> { // TODO(pylbrecht) // This defines required methods for DrawTarget of azure and raqote // The prototypes are derived from azure's methods. -trait GenericDrawTarget { +pub trait GenericDrawTarget { fn clear_rect(&self, rect: &Rect); fn copy_surface(&self, surface: SourceSurface, source: Rect, destination: Point2D); fn create_gradient_stops( @@ -289,15 +223,14 @@ trait GenericDrawTarget { data: &[u8], size: Size2D, stride: i32, - format: SurfaceFormat, ) -> Option; fn draw_surface( &self, surface: SourceSurface, - dest: Rect, - source: Rect, - surf_options: DrawSurfaceOptions, - options: DrawOptions, + dest: Rect, + source: Rect, + filter: Filter, + draw_options: &DrawOptions, ); fn draw_surface_with_shadow( &self, @@ -311,7 +244,7 @@ trait GenericDrawTarget { fn fill(&self, path: &Path, pattern: Pattern, draw_options: &DrawOptions); fn fill_rect(&self, rect: &Rect, pattern: Pattern, draw_options: Option<&DrawOptions>); fn get_format(&self) -> SurfaceFormat; - fn get_size(&self) -> IntSize; + fn get_size(&self) -> Size2D; fn get_transform(&self) -> Transform2D; fn pop_clip(&self); fn push_clip(&self, path: &Path); @@ -339,394 +272,109 @@ trait GenericDrawTarget { stroke_options: &StrokeOptions, draw_options: &DrawOptions, ); -} - -impl GenericDrawTarget for azure_hl::DrawTarget { - fn clear_rect(&self, rect: &Rect) { - self.clear_rect(rect as &Rect); - } - - fn copy_surface(&self, surface: SourceSurface, source: Rect, destination: Point2D) { - self.copy_surface(surface.into_azure(), source, destination); - } - - fn create_gradient_stops( - &self, - gradient_stops: Vec, - extend_mode: ExtendMode, - ) -> GradientStops { - let gradient_stops: Vec = - gradient_stops.into_iter().map(|x| x.into_azure()).collect(); - GradientStops::Azure(self.create_gradient_stops(&gradient_stops, extend_mode.into_azure())) - } - - fn create_path_builder(&self) -> Box { - Box::new(self.create_path_builder()) - } - - fn create_similar_draw_target( - &self, - size: &Size2D, - format: SurfaceFormat, - ) -> Box { - Box::new(self.create_similar_draw_target(size, format.into_azure())) - } - fn create_source_surface_from_data( - &self, - data: &[u8], - size: Size2D, - stride: i32, - format: SurfaceFormat, - ) -> Option { - 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, - source: Rect, - surf_options: DrawSurfaceOptions, - options: DrawOptions, - ) { - self.draw_surface( - surface.into_azure(), - dest as Rect, - source as Rect, - surf_options.into_azure(), - options.into_azure(), - ); - } - fn draw_surface_with_shadow( - &self, - surface: SourceSurface, - dest: &Point2D, - color: &Color, - offset: &Vector2D, - sigma: f32, - operator: CompositionOp, - ) { - self.draw_surface_with_shadow( - surface.into_azure(), - dest as &Point2D, - color.as_azure(), - offset as &Vector2D, - sigma as AzFloat, - operator.into_azure(), - ); - } - fn fill(&self, path: &Path, pattern: Pattern, draw_options: &DrawOptions) { - self.fill( - path.as_azure(), - pattern.as_azure().to_pattern_ref(), - draw_options.as_azure(), - ); - } - fn fill_rect(&self, rect: &Rect, pattern: Pattern, draw_options: Option<&DrawOptions>) { - self.fill_rect( - rect as &Rect, - pattern.as_azure().to_pattern_ref(), - draw_options.map(|x| x.as_azure()), - ); - } - fn get_format(&self) -> SurfaceFormat { - SurfaceFormat::Azure(self.get_format()) - } - fn get_size(&self) -> IntSize { - IntSize::Azure(self.get_size()) - } - fn get_transform(&self) -> Transform2D { - self.get_transform() as Transform2D - } - fn pop_clip(&self) { - self.pop_clip(); - } - fn push_clip(&self, path: &Path) { - self.push_clip(path.as_azure()); - } - fn set_transform(&self, matrix: &Transform2D) { - self.set_transform(matrix as &Transform2D); - } - fn snapshot(&self) -> SourceSurface { - SourceSurface::Azure(self.snapshot()) - } - fn stroke( - &self, - path: &Path, - pattern: Pattern, - stroke_options: &StrokeOptions, - draw_options: &DrawOptions, - ) { - self.stroke( - path.as_azure(), - pattern.as_azure().to_pattern_ref(), - stroke_options.as_azure(), - draw_options.as_azure(), - ); - } - fn stroke_line( - &self, - start: Point2D, - end: Point2D, - pattern: Pattern, - stroke_options: &StrokeOptions, - draw_options: &DrawOptions, - ) { - self.stroke_line( - start as Point2D, - end as Point2D, - pattern.as_azure().to_pattern_ref(), - stroke_options.as_azure(), - draw_options.as_azure(), - ); - } - fn stroke_rect( - &self, - rect: &Rect, - pattern: Pattern, - stroke_options: &StrokeOptions, - draw_options: &DrawOptions, - ) { - self.stroke_rect( - rect as &Rect, - pattern.as_azure().to_pattern_ref(), - stroke_options.as_azure(), - draw_options.as_azure(), - ); - } + fn snapshot_data(&self) -> Vec; } #[derive(Clone)] -enum ExtendMode { +pub enum ExtendMode { + #[cfg(feature = "azure_backend")] Azure(azure_hl::ExtendMode), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl ExtendMode { - fn into_azure(self) -> azure_hl::ExtendMode { - match self { - ExtendMode::Azure(m) => m, - _ => unreachable!(), - } - } -} - -enum GradientStop { +pub enum GradientStop { + #[cfg(feature = "azure_backend")] Azure(AzGradientStop), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl GradientStop { - fn into_azure(self) -> AzGradientStop { - match self { - GradientStop::Azure(s) => s, - _ => unreachable!(), - } - } -} - -enum GradientStops { +pub enum GradientStops { + #[cfg(feature = "azure_backend")] Azure(azure_hl::GradientStops), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl GradientStops { - fn into_azure(self) -> azure_hl::GradientStops { - match self { - GradientStops::Azure(s) => s, - _ => unreachable!(), - } - } -} - #[derive(Clone)] pub enum Color { + #[cfg(feature = "azure_backend")] Azure(azure_hl::Color), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl Color { - fn as_azure(&self) -> &azure_hl::Color { - match self { - Color::Azure(s) => s, - _ => unreachable!(), - } - } -} #[derive(Clone)] -enum CompositionOp { +pub enum CompositionOp { + #[cfg(feature = "azure_backend")] Azure(azure_hl::CompositionOp), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl CompositionOp { - fn into_azure(self) -> azure_hl::CompositionOp { - match self { - CompositionOp::Azure(s) => s, - _ => unreachable!(), - } - } -} - -enum SurfaceFormat { +pub enum SurfaceFormat { + #[cfg(feature = "azure_backend")] Azure(azure_hl::SurfaceFormat), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl SurfaceFormat { - fn into_azure(self) -> azure_hl::SurfaceFormat { - match self { - SurfaceFormat::Azure(s) => s, - _ => unreachable!(), - } - } -} - #[derive(Clone)] -enum SourceSurface { +pub enum SourceSurface { + #[cfg(feature = "azure_backend")] Azure(azure_hl::SourceSurface), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl SourceSurface { - fn into_azure(self) -> azure_hl::SourceSurface { - match self { - SourceSurface::Azure(s) => s, - _ => unreachable!(), - } - } -} - -enum IntSize { - Azure(AzIntSize), - Raqote(()), -} - -impl IntSize { - fn into_azure(self) -> AzIntSize { - match self { - IntSize::Azure(s) => s, - _ => unreachable!(), - } - } -} - -enum Path { +pub enum Path { + #[cfg(feature = "azure_backend")] Azure(azure_hl::Path), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl Path { - fn as_azure(&self) -> &azure_hl::Path { - match self { - Path::Azure(p) => p, - _ => unreachable!(), - } - } -} - #[derive(Clone)] -enum Pattern { +pub enum Pattern { + #[cfg(feature = "azure_backend")] Azure(azure_hl::Pattern), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl Pattern { - fn as_azure(&self) -> &azure_hl::Pattern { - match self { - Pattern::Azure(p) => p, - _ => unreachable!(), - } - } -} - -enum DrawSurfaceOptions { +pub enum DrawSurfaceOptions { + #[cfg(feature = "azure_backend")] Azure(azure_hl::DrawSurfaceOptions), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl DrawSurfaceOptions { - fn into_azure(self) -> azure_hl::DrawSurfaceOptions { - match self { - DrawSurfaceOptions::Azure(options) => options, - _ => unreachable!(), - } - } -} - #[derive(Clone)] -enum DrawOptions { +pub enum DrawOptions { + #[cfg(feature = "azure_backend")] Azure(azure_hl::DrawOptions), + #[cfg(feature = "raqote_backend")] Raqote(()), } -impl DrawOptions { - fn as_azure(&self) -> &azure_hl::DrawOptions { - match self { - DrawOptions::Azure(options) => options, - _ => 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)] -enum StrokeOptions<'a> { +pub enum StrokeOptions<'a> { + #[cfg(feature = "azure_backend")] Azure(azure_hl::StrokeOptions<'a>), - Raqote(()), + #[cfg(feature = "raqote_backend")] + Raqote(PhantomData<&'a ()>), } -impl<'a> StrokeOptions<'a> { - fn as_azure(&self) -> &azure_hl::StrokeOptions<'a> { - match self { - StrokeOptions::Azure(options) => options, - _ => 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!(), - } - } +#[derive(Clone, Copy)] +pub enum Filter { + Linear, + Point, } pub struct CanvasData<'a> { + backend: Box, drawtarget: Box, path_state: Option, state: CanvasPaintState<'a>, @@ -740,6 +388,16 @@ pub struct CanvasData<'a> { pub canvas_id: CanvasId, } +#[cfg(feature = "azure_backend")] +fn create_backend() -> Box { + Box::new(crate::azure_backend::AzureBackend) +} + +#[cfg(feature = "raqote_backend")] +fn create_backend() -> Box { + Box::new(crate::raqote_backend::RaqoteBackend) +} + impl<'a> CanvasData<'a> { pub fn new( size: Size2D, @@ -747,12 +405,14 @@ impl<'a> CanvasData<'a> { antialias: AntialiasMode, canvas_id: CanvasId, ) -> CanvasData<'a> { - let draw_target = CanvasData::create(size); + let backend = create_backend(); + let draw_target = backend.create_drawtarget(size);; let webrender_api = webrender_api_sender.create_api(); CanvasData { + backend, drawtarget: draw_target, path_state: None, - state: CanvasPaintState::new(antialias, CanvasBackend::Azure), + state: CanvasPaintState::new(antialias), saved_states: vec![], webrender_api: webrender_api, image_key: None, @@ -780,7 +440,6 @@ impl<'a> CanvasData<'a> { }; - // TODO(pylbrecht) create another clousure for raqote let writer = |draw_target: &GenericDrawTarget| { write_image( draw_target, @@ -788,8 +447,7 @@ impl<'a> CanvasData<'a> { source_rect.size, dest_rect, smoothing_enabled, - CompositionOp::Azure(self.state.draw_options.as_azure().composition), - self.state.draw_options.as_azure().alpha, + &self.state.draw_options, ); }; @@ -826,31 +484,15 @@ impl<'a> CanvasData<'a> { } pub fn fill_rect(&self, rect: &Rect) { - if is_zero_size_gradient(&self.state.fill_style) { + if self.state.fill_style.is_zero_size_gradient() { return; // Paint nothing if gradient size is zero. } let draw_rect = Rect::new( rect.origin, - match &self.state.fill_style { - Pattern::Azure(pattern) => { - match pattern { - azure_hl::Pattern::Surface(ref surface) => { - let surface_size = surface.size(); - match (surface.repeat_x, surface.repeat_y) { - (true, true) => rect.size, - (true, false) => Size2D::new(rect.size.width, 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, - } - }, - _ => unreachable!(), - } + self.backend + .size_from_pattern(&rect, &self.state.fill_style) + .unwrap_or(rect.size), ); if self.need_to_draw_shadow() { @@ -875,7 +517,7 @@ impl<'a> CanvasData<'a> { } pub fn stroke_rect(&self, rect: &Rect) { - if is_zero_size_gradient(&self.state.stroke_style) { + if self.state.stroke_style.is_zero_size_gradient() { return; // Paint nothing if gradient size is zero. } @@ -889,23 +531,11 @@ impl<'a> CanvasData<'a> { ); }); } else if rect.size.width == 0. || rect.size.height == 0. { - let cap = match self.state.stroke_opts.as_azure().line_join { - JoinStyle::Round => CapStyle::Round, - _ => CapStyle::Butt, - }; - - let stroke_opts = StrokeOptions::Azure(azure_hl::StrokeOptions::new( - self.state.stroke_opts.as_azure().line_width, - self.state.stroke_opts.as_azure().line_join, - cap, - self.state.stroke_opts.as_azure().miter_limit, - self.state.stroke_opts.as_azure().mDashPattern, - )); self.drawtarget.stroke_line( rect.origin, rect.bottom_right(), self.state.stroke_style.clone(), - &stroke_opts, + &self.state.stroke_opts, &self.state.draw_options, ); } else { @@ -951,7 +581,7 @@ impl<'a> CanvasData<'a> { // any transform is present. let new_state = match *self.path_state.as_ref().unwrap() { PathState::UserSpacePath(ref path, Some(ref transform)) => { - Some(Box::new(path.as_azure().transformed_copy_to_builder(transform))) + Some(path.transformed_copy_to_builder(transform)) }, PathState::UserSpacePath(..) | PathState::UserSpacePathBuilder(..) | @@ -973,8 +603,8 @@ impl<'a> CanvasData<'a> { return; }, }; - let builder = Box::new(path.as_azure().transformed_copy_to_builder(&inverse)); - Some(Path::Azure(builder.finish())) + let builder = path.transformed_copy_to_builder(&inverse); + Some(builder.finish()) }, PathState::UserSpacePathBuilder(..) | PathState::UserSpacePath(..) => None, }; @@ -993,7 +623,7 @@ impl<'a> CanvasData<'a> { } pub fn fill(&mut self) { - if is_zero_size_gradient(&self.state.fill_style) { + if self.state.fill_style.is_zero_size_gradient() { return; // Paint nothing if gradient size is zero. } @@ -1006,7 +636,7 @@ impl<'a> CanvasData<'a> { } pub fn stroke(&mut self) { - if is_zero_size_gradient(&self.state.stroke_style) { + if self.state.stroke_style.is_zero_size_gradient() { return; // Paint nothing if gradient size is zero. } @@ -1036,18 +666,18 @@ impl<'a> CanvasData<'a> { Some(PathState::UserSpacePath(ref path, ref transform)) => { let target_transform = self.drawtarget.get_transform(); let path_transform = transform.as_ref().unwrap_or(&target_transform); - path.as_azure().contains_point(x, y, path_transform) + path.contains_point(x, y, path_transform) }, Some(_) | None => false, }; chan.send(result).unwrap(); } - pub fn move_to(&mut self, point: &Point2D) { + pub fn move_to(&mut self, point: &Point2D) { self.path_builder().move_to(point); } - pub fn line_to(&mut self, point: &Point2D) { + pub fn line_to(&mut self, point: &Point2D) { self.path_builder().line_to(point); } @@ -1069,14 +699,14 @@ impl<'a> CanvasData<'a> { &PathState::UserSpacePathBuilder(ref builder, Some(ref transform)) => { let path = builder.finish(); Some(PathState::DeviceSpacePathBuilder( - Box::new(path.as_azure().transformed_copy_to_builder(transform)), + path.transformed_copy_to_builder(transform), )) }, &PathState::UserSpacePath(ref path, Some(ref transform)) => Some( - PathState::DeviceSpacePathBuilder(Box::new(path.as_azure().transformed_copy_to_builder(transform))), + PathState::DeviceSpacePathBuilder(path.transformed_copy_to_builder(transform)), ), &PathState::UserSpacePath(ref path, None) => Some(PathState::UserSpacePathBuilder( - Box::new(path.as_azure().copy_to_builder()), + path.copy_to_builder(), None, )), } @@ -1119,34 +749,34 @@ impl<'a> CanvasData<'a> { self.path_builder().rect(rect); } - pub fn quadratic_curve_to(&mut self, cp: &Point2D, endpoint: &Point2D) { + pub fn quadratic_curve_to(&mut self, cp: &Point2D, endpoint: &Point2D) { self.path_builder().quadratic_curve_to(cp, endpoint); } pub fn bezier_curve_to( &mut self, - cp1: &Point2D, - cp2: &Point2D, - endpoint: &Point2D, + cp1: &Point2D, + cp2: &Point2D, + endpoint: &Point2D, ) { self.path_builder().bezier_curve_to(cp1, cp2, endpoint); } pub fn arc( &mut self, - center: &Point2D, - radius: AzFloat, - start_angle: AzFloat, - end_angle: AzFloat, + center: &Point2D, + radius: f32, + start_angle: f32, + end_angle: f32, ccw: bool, ) { self.path_builder() .arc(center, radius, start_angle, end_angle, ccw); } - pub fn arc_to(&mut self, cp1: &Point2D, cp2: &Point2D, radius: AzFloat) { + pub fn arc_to(&mut self, cp1: &Point2D, cp2: &Point2D, radius: f32) { let cp0 = match self.path_builder().current_point() { - Some(p) => p.as_azure_point(), + Some(p) => p, None => return, }; let cp1 = *cp1; @@ -1209,12 +839,12 @@ impl<'a> CanvasData<'a> { pub fn ellipse( &mut self, - center: &Point2D, - radius_x: AzFloat, - radius_y: AzFloat, - rotation_angle: AzFloat, - start_angle: AzFloat, - end_angle: AzFloat, + center: &Point2D, + radius_x: f32, + radius_y: f32, + rotation_angle: f32, + start_angle: f32, + end_angle: f32, ccw: bool, ) { self.path_builder().ellipse( @@ -1229,15 +859,11 @@ impl<'a> CanvasData<'a> { } pub fn set_fill_style(&mut self, style: FillOrStrokeStyle) { - if let Some(pattern) = style.to_azure_pattern(&*self.drawtarget) { - self.state.fill_style = Pattern::Azure(pattern) - } + self.backend.set_fill_style(style, &mut self.state, &*self.drawtarget); } pub fn set_stroke_style(&mut self, style: FillOrStrokeStyle) { - if let Some(pattern) = style.to_azure_pattern(&*self.drawtarget) { - self.state.stroke_style = Pattern::Azure(pattern) - } + self.backend.set_stroke_style(style, &mut self.state, &*self.drawtarget); } pub fn set_line_width(&mut self, width: f32) { @@ -1277,24 +903,12 @@ impl<'a> CanvasData<'a> { } pub fn set_global_composition(&mut self, op: CompositionOrBlending) { - self.state - .draw_options - .as_azure_mut() - .set_composition_op(op.to_azure_style()); - } - - pub fn create(size: Size2D) -> Box { - // FIXME(nox): Why is the size made of i32 values? - Box::new(DrawTarget::new( - BackendType::Skia, - size.to_i32(), - azure_hl::SurfaceFormat::B8G8R8A8, - )) + self.backend.set_global_composition(op, &mut self.state); } pub fn recreate(&mut self, size: Size2D) { - self.drawtarget = CanvasData::create(Size2D::new(size.width as u64, size.height as u64)); - self.state = CanvasPaintState::new(self.state.draw_options.as_azure().antialias, CanvasBackend::Azure); + self.drawtarget = self.backend.create_drawtarget(Size2D::new(size.width as u64, size.height as u64)); + self.state = self.backend.recreate_paint_state(&self.state); self.saved_states.clear(); // 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 @@ -1311,15 +925,15 @@ impl<'a> CanvasData<'a> { #[allow(unsafe_code)] pub fn send_pixels(&mut self, chan: IpcSender) { - let data = IpcSharedMemory::from_bytes(unsafe { - self.drawtarget.snapshot().into_azure().get_data_surface().data() - }); + let data = IpcSharedMemory::from_bytes( + &self.drawtarget.snapshot_data() + ); chan.send(data).unwrap(); } #[allow(unsafe_code)] pub fn send_data(&mut self, chan: IpcSender) { - let size = self.drawtarget.get_size().into_azure(); + let size = self.drawtarget.get_size(); let descriptor = webrender_api::ImageDescriptor { size: webrender_api::DeviceIntSize::new(size.width, size.height), @@ -1329,9 +943,9 @@ impl<'a> CanvasData<'a> { is_opaque: false, allow_mipmaps: false, }; - let data = webrender_api::ImageData::Raw(Arc::new(unsafe { - self.drawtarget.snapshot().into_azure().get_data_surface().data().into() - })); + let data = webrender_api::ImageData::Raw(Arc::new( + self.drawtarget.snapshot_data().into() + )); let mut txn = webrender_api::Transaction::new(); @@ -1372,7 +986,6 @@ impl<'a> CanvasData<'a> { &imagedata, rect.size.to_i32(), rect.size.width as i32 * 4, - SurfaceFormat::Azure(azure_hl::SurfaceFormat::B8G8R8A8), ) .unwrap(); self.drawtarget.copy_surface( @@ -1394,13 +1007,13 @@ impl<'a> CanvasData<'a> { self.state.shadow_blur = value; } - pub fn set_shadow_color(&mut self, value: Color) { - self.state.shadow_color = value; + pub fn set_shadow_color(&mut self, value: RGBA) { + self.backend.set_shadow_color(value, &mut self.state); } // https://html.spec.whatwg.org/multipage/#when-shadows-are-drawn fn need_to_draw_shadow(&self) -> bool { - self.state.shadow_color.as_azure().a != 0.0f32 && + self.backend.need_to_draw_shadow(&self.state.shadow_color) && (self.state.shadow_offset_x != 0.0f64 || self.state.shadow_offset_y != 0.0f64 || self.state.shadow_blur != 0.0f64) @@ -1431,16 +1044,16 @@ impl<'a> CanvasData<'a> { self.drawtarget.draw_surface_with_shadow( new_draw_target.snapshot(), &Point2D::new( - shadow_src_rect.origin.x as AzFloat, - shadow_src_rect.origin.y as AzFloat, + shadow_src_rect.origin.x as f32, + shadow_src_rect.origin.y as f32, ), &self.state.shadow_color, &Vector2D::new( - self.state.shadow_offset_x as AzFloat, - self.state.shadow_offset_y as AzFloat, + self.state.shadow_offset_x as f32, + self.state.shadow_offset_y as f32, ), - (self.state.shadow_blur / 2.0f64) as AzFloat, - CompositionOp::Azure(self.state.draw_options.as_azure().composition), + (self.state.shadow_blur / 2.0f64) as f32, + self.backend.get_composition_op(&self.state.draw_options), ); } @@ -1457,12 +1070,11 @@ impl<'a> CanvasData<'a> { return vec![]; } - let data_surface = self.drawtarget.snapshot().into_azure().get_data_surface(); pixels::rgba8_get_rect( - unsafe { data_surface.data() }, + &self.drawtarget.snapshot_data(), canvas_size.to_u32(), read_rect.to_u32(), - ) + ) .into_owned() } } @@ -1482,72 +1094,18 @@ impl<'a> Drop for CanvasData<'a> { } } -enum CanvasBackend { - Azure, - Raqote, -} - #[derive(Clone)] -struct CanvasPaintState<'a> { - draw_options: DrawOptions, - fill_style: Pattern, - stroke_style: Pattern, - stroke_opts: StrokeOptions<'a>, +pub struct CanvasPaintState<'a> { + pub draw_options: DrawOptions, + pub fill_style: Pattern, + pub stroke_style: Pattern, + pub stroke_opts: StrokeOptions<'a>, /// The current 2D transform matrix. - transform: Transform2D, - shadow_offset_x: f64, - shadow_offset_y: f64, - shadow_blur: f64, - shadow_color: Color, -} - -impl<'a> CanvasPaintState<'a> { - fn new(antialias: AntialiasMode, backend: CanvasBackend) -> CanvasPaintState<'a> { - match backend { - CanvasBackend::Azure => CanvasPaintState { - draw_options: DrawOptions::Azure(azure_hl::DrawOptions::new(1.0, azure_hl::CompositionOp::Over, antialias)), - fill_style: Pattern::Azure(azure_hl::Pattern::Color(ColorPattern::new(azure_hl::Color::black()))), - stroke_style: Pattern::Azure(azure_hl::Pattern::Color(ColorPattern::new(azure_hl::Color::black()))), - stroke_opts: StrokeOptions::Azure(azure_hl::StrokeOptions::new( - 1.0, - JoinStyle::MiterOrBevel, - CapStyle::Butt, - 10.0, - &[], - )), - transform: Transform2D::identity(), - shadow_offset_x: 0.0, - shadow_offset_y: 0.0, - shadow_blur: 0.0, - shadow_color: Color::Azure(azure_hl::Color::transparent()), - }, - CanvasBackend::Raqote => CanvasPaintState { - draw_options: DrawOptions::Raqote(()), - fill_style: Pattern::Raqote(()), - stroke_style: Pattern::Raqote(()), - stroke_opts: StrokeOptions::Raqote(()), - transform: Transform2D::identity(), - shadow_offset_x: 0.0, - shadow_offset_y: 0.0, - shadow_blur: 0.0, - shadow_color: Color::Raqote(()), - } - } - } -} - -fn is_zero_size_gradient(pattern: &Pattern) -> bool { - match pattern { - Pattern::Azure(ref az_pattern) => { - if let azure_hl::Pattern::LinearGradient(ref gradient) = az_pattern { - if gradient.is_zero_size() { - return true; - } - } - false - }, - _ => unreachable!(), - } + pub transform: Transform2D, + pub shadow_offset_x: f64, + pub shadow_offset_y: f64, + pub shadow_blur: f64, + pub shadow_color: Color, } /// It writes an image to the destination target @@ -1562,8 +1120,7 @@ fn write_image( image_size: Size2D, dest_rect: Rect, smoothing_enabled: bool, - composition_op: CompositionOp, - global_alpha: f32, + draw_options: &DrawOptions, ) { if image_data.is_empty() { return; @@ -1586,17 +1143,14 @@ fn write_image( &image_data, image_size, image_size.width * 4, - SurfaceFormat::Azure(azure_hl::SurfaceFormat::B8G8R8A8), - ) + ) .unwrap(); - let draw_surface_options = DrawSurfaceOptions::Azure(azure_hl::DrawSurfaceOptions::new(filter, true)); - let draw_options = - DrawOptions::Azure(azure_hl::DrawOptions::new(global_alpha, composition_op.into_azure(), AntialiasMode::None)); + draw_target.draw_surface( source_surface, - dest_rect.to_azure_style(), - image_rect.to_azure_style(), - draw_surface_options, + dest_rect, + image_rect, + filter, draw_options, ); } @@ -1647,188 +1201,3 @@ impl RectToi32 for Rect { ) } } - -pub trait ToAzureStyle { - type Target; - fn to_azure_style(self) -> Self::Target; -} - -impl ToAzureStyle for Rect { - type Target = Rect; - - fn to_azure_style(self) -> Rect { - Rect::new( - Point2D::new(self.origin.x as AzFloat, self.origin.y as AzFloat), - Size2D::new(self.size.width as AzFloat, self.size.height as AzFloat), - ) - } -} - -impl ToAzureStyle for LineCapStyle { - type Target = CapStyle; - - fn to_azure_style(self) -> CapStyle { - match self { - LineCapStyle::Butt => CapStyle::Butt, - LineCapStyle::Round => CapStyle::Round, - LineCapStyle::Square => CapStyle::Square, - } - } -} - -impl ToAzureStyle for LineJoinStyle { - type Target = JoinStyle; - - fn to_azure_style(self) -> JoinStyle { - match self { - LineJoinStyle::Round => JoinStyle::Round, - LineJoinStyle::Bevel => JoinStyle::Bevel, - LineJoinStyle::Miter => JoinStyle::Miter, - } - } -} - -impl ToAzureStyle for CompositionStyle { - type Target = azure_hl::CompositionOp; - - fn to_azure_style(self) -> azure_hl::CompositionOp { - match self { - CompositionStyle::SrcIn => azure_hl::CompositionOp::In, - CompositionStyle::SrcOut => azure_hl::CompositionOp::Out, - CompositionStyle::SrcOver => azure_hl::CompositionOp::Over, - CompositionStyle::SrcAtop => azure_hl::CompositionOp::Atop, - CompositionStyle::DestIn => azure_hl::CompositionOp::DestIn, - CompositionStyle::DestOut => azure_hl::CompositionOp::DestOut, - CompositionStyle::DestOver => azure_hl::CompositionOp::DestOver, - CompositionStyle::DestAtop => azure_hl::CompositionOp::DestAtop, - CompositionStyle::Copy => azure_hl::CompositionOp::Source, - CompositionStyle::Lighter => azure_hl::CompositionOp::Add, - CompositionStyle::Xor => azure_hl::CompositionOp::Xor, - } - } -} - -impl ToAzureStyle for BlendingStyle { - type Target = azure_hl::CompositionOp; - - fn to_azure_style(self) -> azure_hl::CompositionOp { - match self { - BlendingStyle::Multiply => azure_hl::CompositionOp::Multiply, - BlendingStyle::Screen => azure_hl::CompositionOp::Screen, - BlendingStyle::Overlay => azure_hl::CompositionOp::Overlay, - BlendingStyle::Darken => azure_hl::CompositionOp::Darken, - BlendingStyle::Lighten => azure_hl::CompositionOp::Lighten, - BlendingStyle::ColorDodge => azure_hl::CompositionOp::ColorDodge, - BlendingStyle::ColorBurn => azure_hl::CompositionOp::ColorBurn, - BlendingStyle::HardLight => azure_hl::CompositionOp::HardLight, - BlendingStyle::SoftLight => azure_hl::CompositionOp::SoftLight, - BlendingStyle::Difference => azure_hl::CompositionOp::Difference, - BlendingStyle::Exclusion => azure_hl::CompositionOp::Exclusion, - BlendingStyle::Hue => azure_hl::CompositionOp::Hue, - BlendingStyle::Saturation => azure_hl::CompositionOp::Saturation, - BlendingStyle::Color => azure_hl::CompositionOp::Color, - BlendingStyle::Luminosity => azure_hl::CompositionOp::Luminosity, - } - } -} - -impl ToAzureStyle for CompositionOrBlending { - type Target = azure_hl::CompositionOp; - - fn to_azure_style(self) -> azure_hl::CompositionOp { - match self { - CompositionOrBlending::Composition(op) => op.to_azure_style(), - CompositionOrBlending::Blending(op) => op.to_azure_style(), - } - } -} - -pub trait ToAzurePattern { - fn to_azure_pattern(&self, drawtarget: &GenericDrawTarget) -> Option; -} - -impl ToAzurePattern for FillOrStrokeStyle { - fn to_azure_pattern(&self, drawtarget: &GenericDrawTarget) -> Option { - Some(match *self { - FillOrStrokeStyle::Color(ref color) => { - azure_hl::Pattern::Color(ColorPattern::new(color.to_azure_style())) - }, - FillOrStrokeStyle::LinearGradient(ref linear_gradient_style) => { - let gradient_stops: Vec = linear_gradient_style - .stops - .iter() - .map(|s| GradientStop::Azure(azure_hl::GradientStop { - offset: s.offset as AzFloat, - color: s.color.to_azure_style(), - })) - .collect(); - - azure_hl::Pattern::LinearGradient(LinearGradientPattern::new( - &Point2D::new( - linear_gradient_style.x0 as AzFloat, - linear_gradient_style.y0 as AzFloat, - ), - &Point2D::new( - linear_gradient_style.x1 as AzFloat, - linear_gradient_style.y1 as AzFloat, - ), - drawtarget.create_gradient_stops(gradient_stops, ExtendMode::Azure(azure_hl::ExtendMode::Clamp)).into_azure(), - &Transform2D::identity(), - )) - }, - FillOrStrokeStyle::RadialGradient(ref radial_gradient_style) => { - let gradient_stops: Vec = radial_gradient_style - .stops - .iter() - .map(|s| GradientStop::Azure(azure_hl::GradientStop { - offset: s.offset as AzFloat, - color: s.color.to_azure_style(), - })) - .collect(); - - azure_hl::Pattern::RadialGradient(RadialGradientPattern::new( - &Point2D::new( - radial_gradient_style.x0 as AzFloat, - radial_gradient_style.y0 as AzFloat, - ), - &Point2D::new( - radial_gradient_style.x1 as AzFloat, - radial_gradient_style.y1 as AzFloat, - ), - radial_gradient_style.r0 as AzFloat, - radial_gradient_style.r1 as AzFloat, - drawtarget.create_gradient_stops(gradient_stops, ExtendMode::Azure(azure_hl::ExtendMode::Clamp)).into_azure(), - &Transform2D::identity(), - )) - }, - FillOrStrokeStyle::Surface(ref surface_style) => { - let source_surface = drawtarget.create_source_surface_from_data( - &surface_style.surface_data, - // FIXME(nox): Why are those i32 values? - surface_style.surface_size.to_i32(), - surface_style.surface_size.width as i32 * 4, - SurfaceFormat::Azure(azure_hl::SurfaceFormat::B8G8R8A8), - )?.into_azure(); - azure_hl::Pattern::Surface(SurfacePattern::new( - source_surface.azure_source_surface, - surface_style.repeat_x, - surface_style.repeat_y, - &Transform2D::identity(), - )) - }, - }) - } -} - -impl ToAzureStyle for RGBA { - type Target = azure_hl::Color; - - fn to_azure_style(self) -> azure_hl::Color { - azure_hl::Color::rgba( - self.red_f32() as AzFloat, - self.green_f32() as AzFloat, - self.blue_f32() as AzFloat, - self.alpha_f32() as AzFloat, - ) - } -} diff --git a/components/canvas/canvas_paint_thread.rs b/components/canvas/canvas_paint_thread.rs index c69b35e7272..afaa9f49d6a 100644 --- a/components/canvas/canvas_paint_thread.rs +++ b/components/canvas/canvas_paint_thread.rs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use crate::canvas_data::*; -use azure::azure_hl::AntialiasMode; use canvas_traits::canvas::*; use euclid::Size2D; use ipc_channel::ipc::{self, IpcSender}; @@ -11,6 +10,11 @@ use std::borrow::ToOwned; use std::collections::HashMap; use std::thread; +pub enum AntialiasMode { + Default, + None, +} + pub struct CanvasPaintThread<'a> { canvases: HashMap>, next_canvas_id: CanvasId, @@ -195,9 +199,9 @@ impl<'a> CanvasPaintThread<'a> { self.canvas(canvas_id).set_shadow_offset_y(value) }, Canvas2dMsg::SetShadowBlur(value) => self.canvas(canvas_id).set_shadow_blur(value), - Canvas2dMsg::SetShadowColor(ref color) => self + Canvas2dMsg::SetShadowColor(color) => self .canvas(canvas_id) - .set_shadow_color(Color::Azure(color.to_azure_style())), + .set_shadow_color(color), } } diff --git a/components/canvas/lib.rs b/components/canvas/lib.rs index cb6fe50f8d8..28dd1e04f5d 100644 --- a/components/canvas/lib.rs +++ b/components/canvas/lib.rs @@ -7,6 +7,12 @@ #[macro_use] extern crate log; +#[cfg(feature = "azure_backend")] +mod azure_backend; + +#[cfg(feature = "raqote_backend")] +mod raqote_backend; + pub mod canvas_data; pub mod canvas_paint_thread; pub mod gl_context; diff --git a/components/canvas/raqote_backend.rs b/components/canvas/raqote_backend.rs new file mode 100644 index 00000000000..2d51c7f4493 --- /dev/null +++ b/components/canvas/raqote_backend.rs @@ -0,0 +1,124 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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 cssparser::RGBA; +use crate::canvas_data::{ + Backend, CanvasPaintState, Color, CompositionOp, DrawOptions, GenericDrawTarget, + GenericPathBuilder, Path, Pattern, StrokeOptions, +}; +use crate::canvas_paint_thread::AntialiasMode; +use canvas_traits::canvas::*; +use euclid::{Rect, Size2D, Transform2D}; +use std::marker::PhantomData; + +pub struct RaqoteBackend; + +impl Backend for RaqoteBackend { + fn get_composition_op(&self, _opts: &DrawOptions) -> CompositionOp { + unimplemented!() + } + + fn need_to_draw_shadow(&self, _color: &Color) -> bool { + unimplemented!() + } + + fn size_from_pattern(&self, _rect: &Rect, _pattern: &Pattern) -> Option> { + unimplemented!() + } + + fn set_shadow_color<'a>(&mut self, _color: RGBA, _state: &mut CanvasPaintState<'a>) { + unimplemented!() + } + + fn set_fill_style<'a>( + &mut self, + _style: FillOrStrokeStyle, + _state: &mut CanvasPaintState<'a>, + _drawtarget: &GenericDrawTarget, + ) { + unimplemented!() + } + + fn set_stroke_style<'a>( + &mut self, + _style: FillOrStrokeStyle, + _state: &mut CanvasPaintState<'a>, + _drawtarget: &GenericDrawTarget) + { + unimplemented!() + } + + fn set_global_composition<'a>(&mut self, _op: CompositionOrBlending, _state: &mut CanvasPaintState<'a>) { + unimplemented!() + } + + fn create_drawtarget(&self, _size: Size2D) -> Box { + unimplemented!() + } + + fn recreate_paint_state<'a>(&self, _state: &CanvasPaintState<'a>) -> CanvasPaintState<'a> { + CanvasPaintState::new(AntialiasMode::Default) + } +} + +impl<'a> CanvasPaintState<'a> { + pub fn new(_antialias: AntialiasMode) -> CanvasPaintState<'a> { + CanvasPaintState { + draw_options: DrawOptions::Raqote(()), + fill_style: Pattern::Raqote(()), + stroke_style: Pattern::Raqote(()), + stroke_opts: StrokeOptions::Raqote(PhantomData), + transform: Transform2D::identity(), + shadow_offset_x: 0.0, + shadow_offset_y: 0.0, + shadow_blur: 0.0, + shadow_color: Color::Raqote(()), + } + } +} + +impl Pattern { + pub fn is_zero_size_gradient(&self) -> bool { + match *self { + Pattern::Raqote(()) => unimplemented!(), + } + } +} + +impl<'a> StrokeOptions<'a> { + pub fn set_line_width(&mut self, _val: f32) { + unimplemented!() + } + pub fn set_miter_limit(&mut self, _val: f32) { + unimplemented!() + } + pub fn set_line_join(&mut self, _val: LineJoinStyle) { + unimplemented!() + } + pub fn set_line_cap(&mut self, _val: LineCapStyle) { + unimplemented!() + } +} + +impl DrawOptions { + pub fn set_alpha(&mut self, _val: f32) { + match self { + DrawOptions::Raqote(()) => unimplemented!(), + } + } +} + +impl Path { + pub fn transformed_copy_to_builder(&self, _transform: &Transform2D) -> Box { + unimplemented!() + } + + pub fn contains_point(&self, _x: f64, _y: f64, _path_transform: &Transform2D) -> bool { + unimplemented!() + } + + pub fn copy_to_builder(&self) -> Box { + unimplemented!() + } +} diff --git a/components/constellation/Cargo.toml b/components/constellation/Cargo.toml index 795a1c5921e..fc2021d3af3 100644 --- a/components/constellation/Cargo.toml +++ b/components/constellation/Cargo.toml @@ -10,11 +10,15 @@ publish = false name = "constellation" path = "lib.rs" +[features] +azure_backend = ["canvas/azure_backend"] +raqote_backend = ["canvas/raqote_backend"] + [dependencies] background_hang_monitor = { path = "../background_hang_monitor"} backtrace = "0.3" bluetooth_traits = { path = "../bluetooth_traits" } -canvas = {path = "../canvas"} +canvas = {path = "../canvas", default-features = false} clipboard = "0.5" canvas_traits = {path = "../canvas_traits"} compositing = {path = "../compositing"} diff --git a/components/servo/Cargo.toml b/components/servo/Cargo.toml index a81954f885f..86e5f0852a6 100644 --- a/components/servo/Cargo.toml +++ b/components/servo/Cargo.toml @@ -12,14 +12,15 @@ path = "lib.rs" crate-type = ["rlib"] [features] -max_log_level = ["log/release_max_level_info"] -webdriver = ["webdriver_server"] -energy-profiling = ["profile_traits/energy-profiling"] +azure_backend = ["canvas/azure_backend"] debugmozjs = ["script/debugmozjs"] +energy-profiling = ["profile_traits/energy-profiling"] profilemozjs = ["script/profilemozjs"] googlevr = ["webvr/googlevr"] js_backtrace = ["script/js_backtrace"] +max_log_level = ["log/release_max_level_info"] native-bluetooth = ["bluetooth/native-bluetooth"] +raqote_backend = ["canvas/raqote_backend"] webrender_debugger = ["webrender/debugger"] no_static_freetype = ["webrender/no_static_freetype"] oculusvr = ["webvr/oculusvr"] @@ -28,6 +29,7 @@ unstable = [ "profile/unstable", "script/unstable", ] +webdriver = ["webdriver_server"] webgl_backtrace = [ "script/webgl_backtrace", "canvas/webgl_backtrace", @@ -38,7 +40,7 @@ webgl_backtrace = [ background_hang_monitor = {path = "../background_hang_monitor"} bluetooth_traits = {path = "../bluetooth_traits"} bluetooth = {path = "../bluetooth"} -canvas = {path = "../canvas"} +canvas = {path = "../canvas", default-features = false} canvas_traits = {path = "../canvas_traits"} compositing = {path = "../compositing", features = ["gl"]} constellation = {path = "../constellation"} diff --git a/ports/glutin/Cargo.toml b/ports/glutin/Cargo.toml index 41fbf47d734..534abcbf351 100644 --- a/ports/glutin/Cargo.toml +++ b/ports/glutin/Cargo.toml @@ -27,18 +27,20 @@ OriginalFilename = "servo.exe" ProductName = "Servo" [features] +azure_backend = ["libservo/azure_backend"] default = ["unstable", "default-except-unstable"] default-except-unstable = ["webdriver", "max_log_level"] -native-bluetooth = ["libservo/native-bluetooth"] -max_log_level = ["log/release_max_level_info"] -webdriver = ["libservo/webdriver"] energy-profiling = ["libservo/energy-profiling"] debugmozjs = ["libservo/debugmozjs"] +js_backtrace = ["libservo/js_backtrace"] +max_log_level = ["log/release_max_level_info"] +native-bluetooth = ["libservo/native-bluetooth"] profilemozjs = ["libservo/profilemozjs"] +raqote_backend = ["libservo/raqote_backend"] unstable = ["libservo/unstable"] +webdriver = ["libservo/webdriver"] webgl_backtrace = ["libservo/webgl_backtrace"] webrender_debugger = ["libservo/webrender_debugger"] -js_backtrace = ["libservo/js_backtrace"] [target.'cfg(not(target_os = "android"))'.dependencies] backtrace = "0.3" diff --git a/python/servo/build_commands.py b/python/servo/build_commands.py index 1a00d8be97d..292879b52ea 100644 --- a/python/servo/build_commands.py +++ b/python/servo/build_commands.py @@ -296,6 +296,9 @@ class MachCommands(CommandBase): if self.config["build"]["dom-backtrace"]: features += ["dom-backtrace"] + if "raqote_backend" not in features: + features += ["azure_backend"] + if features: opts += ["--features", "%s" % ' '.join(features)]