diff --git a/components/canvas/canvas_paint_task.rs b/components/canvas/canvas_paint_task.rs index c9aa2d0ebd8..76af23fa44f 100644 --- a/components/canvas/canvas_paint_task.rs +++ b/components/canvas/canvas_paint_task.rs @@ -40,6 +40,7 @@ pub enum CanvasMsg { SetStrokeStyle(FillOrStrokeStyle), SetLineWidth(f32), SetTransform(Matrix2D), + SetGlobalAlpha(f32), Recreate(Size2D), SendPixelContents(Sender>), GetImageData(Rect, Size2D, Sender>), @@ -105,7 +106,7 @@ impl<'a> CanvasPaintTask<'a> { image_size, image_size.width * 4, SurfaceFormat::B8G8R8A8); let draw_surface_options = DrawSurfaceOptions::new(filter, true); - let draw_options = DrawOptions::new(1.0f64 as AzFloat, 0); + let draw_options = DrawOptions::new(self.draw_options.alpha, 0); self.drawtarget.draw_surface(source_surface, dest_rect.to_azfloat(), @@ -182,6 +183,7 @@ impl<'a> CanvasPaintTask<'a> { pub struct CanvasPaintTask<'a> { drawtarget: DrawTarget, + draw_options: DrawOptions, fill_style: Pattern, stroke_style: Pattern, stroke_opts: StrokeOptions<'a>, @@ -197,6 +199,7 @@ impl<'a> CanvasPaintTask<'a> { let path_builder = draw_target.create_path_builder(); CanvasPaintTask { drawtarget: draw_target, + draw_options: DrawOptions::new(1.0, 0), fill_style: Pattern::Color(ColorPattern::new(color::black())), stroke_style: Pattern::Color(ColorPattern::new(color::black())), stroke_opts: StrokeOptions::new(1.0, JoinStyle::MiterOrBevel, CapStyle::Butt, 1.0, &[]), @@ -243,6 +246,7 @@ impl<'a> CanvasPaintTask<'a> { CanvasMsg::SetStrokeStyle(style) => painter.set_stroke_style(style), CanvasMsg::SetLineWidth(width) => painter.set_line_width(width), CanvasMsg::SetTransform(ref matrix) => painter.set_transform(matrix), + CanvasMsg::SetGlobalAlpha(alpha) => painter.set_global_alpha(alpha), CanvasMsg::Recreate(size) => painter.recreate(size), CanvasMsg::SendPixelContents(chan) => painter.send_pixel_contents(chan), CanvasMsg::GetImageData(dest_rect, canvas_size, chan) => painter.get_image_data(dest_rect, canvas_size, chan), @@ -256,8 +260,7 @@ impl<'a> CanvasPaintTask<'a> { } fn fill_rect(&self, rect: &Rect) { - let drawopts = DrawOptions::new(1.0, 0); - self.drawtarget.fill_rect(rect, self.fill_style.to_pattern_ref(), Some(&drawopts)); + self.drawtarget.fill_rect(rect, self.fill_style.to_pattern_ref(), Some(&self.draw_options)); } fn clear_rect(&self, rect: &Rect) { @@ -265,10 +268,9 @@ impl<'a> CanvasPaintTask<'a> { } fn stroke_rect(&self, rect: &Rect) { - let drawopts = DrawOptions::new(1.0, 0); match self.stroke_style { Pattern::Color(ref color) => { - self.drawtarget.stroke_rect(rect, color, &self.stroke_opts, &drawopts) + self.drawtarget.stroke_rect(rect, color, &self.stroke_opts, &self.draw_options) } _ => { // TODO(pcwalton) @@ -285,10 +287,9 @@ impl<'a> CanvasPaintTask<'a> { } fn fill(&self) { - let draw_options = DrawOptions::new(1.0, 0); match self.fill_style { Pattern::Color(ref color) => { - self.drawtarget.fill(&self.path_builder.finish(), color, &draw_options); + self.drawtarget.fill(&self.path_builder.finish(), color, &self.draw_options); } _ => { // TODO(pcwalton) @@ -297,11 +298,10 @@ impl<'a> CanvasPaintTask<'a> { } fn stroke(&self) { - let draw_options = DrawOptions::new(1.0, 0); match self.stroke_style { Pattern::Color(ref color) => { self.drawtarget.stroke(&self.path_builder.finish(), - color, &self.stroke_opts, &draw_options); + color, &self.stroke_opts, &self.draw_options); } _ => { // TODO @@ -430,6 +430,10 @@ impl<'a> CanvasPaintTask<'a> { self.drawtarget.set_transform(transform) } + fn set_global_alpha(&mut self, alpha: f32) { + self.draw_options.alpha = alpha; + } + fn create(size: Size2D) -> DrawTarget { DrawTarget::new(BackendType::Skia, size, SurfaceFormat::B8G8R8A8) } diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index 690d003d81a..8213e7afc87 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -49,6 +49,7 @@ pub struct CanvasRenderingContext2D { global: GlobalField, renderer: Sender, canvas: JS, + global_alpha: Cell, image_smoothing_enabled: Cell, stroke_color: Cell, line_width: Cell, @@ -70,6 +71,7 @@ impl CanvasRenderingContext2D { global: GlobalField::from_rooted(&global), renderer: CanvasPaintTask::start(size), canvas: JS::from_rooted(canvas), + global_alpha: Cell::new(1.0), image_smoothing_enabled: Cell::new(true), stroke_color: Cell::new(black), line_width: Cell::new(1.0), @@ -342,6 +344,19 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D> self.update_transform() } + fn GlobalAlpha(self) -> f64 { + self.global_alpha.get() + } + + fn SetGlobalAlpha(self, alpha: f64) { + if !alpha.is_finite() || alpha > 1.0 || alpha < 0.0 { + return; + } + + self.global_alpha.set(alpha); + self.renderer.send(CanvasMsg::SetGlobalAlpha(alpha as f32)).unwrap() + } + fn FillRect(self, x: f64, y: f64, width: f64, height: f64) { if !(x.is_finite() && y.is_finite() && width.is_finite() && height.is_finite()) { diff --git a/components/script/dom/webidls/CanvasRenderingContext2D.webidl b/components/script/dom/webidls/CanvasRenderingContext2D.webidl index 567462e2c3f..6808e3c7d24 100644 --- a/components/script/dom/webidls/CanvasRenderingContext2D.webidl +++ b/components/script/dom/webidls/CanvasRenderingContext2D.webidl @@ -49,7 +49,7 @@ interface CanvasRenderingContext2D { //void resetTransform(); // compositing - // attribute unrestricted double globalAlpha; // (default 1.0) + attribute unrestricted double globalAlpha; // (default 1.0) // attribute DOMString globalCompositeOperation; // (default source-over) // image smoothing diff --git a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.canvas.html.ini b/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.canvas.html.ini deleted file mode 100644 index 2032a2a017c..00000000000 --- a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.canvas.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.composite.globalAlpha.canvas.html] - type: testharness - [Canvas test: 2d.composite.globalAlpha.canvas] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.default.html.ini b/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.default.html.ini deleted file mode 100644 index 5e17eb50b21..00000000000 --- a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.default.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.composite.globalAlpha.default.html] - type: testharness - [Canvas test: 2d.composite.globalAlpha.default] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.fill.html.ini b/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.fill.html.ini deleted file mode 100644 index 2c38aab1c77..00000000000 --- a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.fill.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.composite.globalAlpha.fill.html] - type: testharness - [Canvas test: 2d.composite.globalAlpha.fill] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.image.html.ini b/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.image.html.ini deleted file mode 100644 index 58a667efe51..00000000000 --- a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.image.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.composite.globalAlpha.image.html] - type: testharness - [Canvas test: 2d.composite.globalAlpha.image] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.invalid.html.ini b/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.invalid.html.ini deleted file mode 100644 index 2b16fc32a08..00000000000 --- a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.invalid.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.composite.globalAlpha.invalid.html] - type: testharness - [Canvas test: 2d.composite.globalAlpha.invalid] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.range.html.ini b/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.range.html.ini deleted file mode 100644 index 0577c735609..00000000000 --- a/tests/wpt/metadata/2dcontext/compositing/2d.composite.globalAlpha.range.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.composite.globalAlpha.range.html] - type: testharness - [Canvas test: 2d.composite.globalAlpha.range] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/drawing-images-to-the-canvas/2d.drawImage.alpha.html.ini b/tests/wpt/metadata/2dcontext/drawing-images-to-the-canvas/2d.drawImage.alpha.html.ini deleted file mode 100644 index 6d71c0fe6e4..00000000000 --- a/tests/wpt/metadata/2dcontext/drawing-images-to-the-canvas/2d.drawImage.alpha.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.drawImage.alpha.html] - type: testharness - [Canvas test: 2d.drawImage.alpha] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/drawing-rectangles-to-the-canvas/2d.strokeRect.globalalpha.html.ini b/tests/wpt/metadata/2dcontext/drawing-rectangles-to-the-canvas/2d.strokeRect.globalalpha.html.ini deleted file mode 100644 index 6d4c4fe583d..00000000000 --- a/tests/wpt/metadata/2dcontext/drawing-rectangles-to-the-canvas/2d.strokeRect.globalalpha.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.strokeRect.globalalpha.html] - type: testharness - [strokeRect is affected by globalAlpha] - expected: FAIL - diff --git a/tests/wpt/metadata/html/dom/interfaces.html.ini b/tests/wpt/metadata/html/dom/interfaces.html.ini index 4e86f02ce4e..f4821d971af 100644 --- a/tests/wpt/metadata/html/dom/interfaces.html.ini +++ b/tests/wpt/metadata/html/dom/interfaces.html.ini @@ -6915,9 +6915,6 @@ [CanvasRenderingContext2D interface: operation resetTransform()] expected: FAIL - [CanvasRenderingContext2D interface: attribute globalAlpha] - expected: FAIL - [CanvasRenderingContext2D interface: attribute globalCompositeOperation] expected: FAIL @@ -7077,9 +7074,6 @@ [CanvasRenderingContext2D interface: document.createElement("canvas").getContext("2d") must inherit property "resetTransform" with the proper type (12)] expected: FAIL - [CanvasRenderingContext2D interface: document.createElement("canvas").getContext("2d") must inherit property "globalAlpha" with the proper type (13)] - expected: FAIL - [CanvasRenderingContext2D interface: document.createElement("canvas").getContext("2d") must inherit property "globalCompositeOperation" with the proper type (14)] expected: FAIL