diff options
author | pylbrecht <palbrecht@mailbox.org> | 2020-01-25 19:36:02 +0100 |
---|---|---|
committer | pylbrecht <palbrecht@mailbox.org> | 2020-01-25 21:29:24 +0100 |
commit | ebdf4693abcd3f05a88cfb9d20664c0a489f4964 (patch) | |
tree | 1ec973a120abad79fade44ff704d30b19a571686 /components/script/canvas_state.rs | |
parent | 937efba0cd6662b4e0060e7056c52dac2896535c (diff) | |
download | servo-ebdf4693abcd3f05a88cfb9d20664c0a489f4964.tar.gz servo-ebdf4693abcd3f05a88cfb9d20664c0a489f4964.zip |
Update fill and stroke style only when required
So far fill and stroke style updates have been sent to the canvas paint
thread by `SetFillStyle()` and `SetStrokeStyle()`. This resulted in
fill/stroke style updates not being considered by the canvas paint
thread between the latest call of `SetFillStyle()`/`SetStrokeStyle()` and
the drawing operation (e.g. fill or stroke).
This issue is solved by making `SetFillStyle()` and `SetStrokeStyle()`
update the local canvas state and propagating the state to the canvas
paint thread right before any drawing operation that requires it.
Diffstat (limited to 'components/script/canvas_state.rs')
-rw-r--r-- | components/script/canvas_state.rs | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/components/script/canvas_state.rs b/components/script/canvas_state.rs index 0416c96aff0..c59cb3c8823 100644 --- a/components/script/canvas_state.rs +++ b/components/script/canvas_state.rs @@ -654,6 +654,7 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect pub fn fill_rect(&self, x: f64, y: f64, width: f64, height: f64) { if let Some(rect) = self.create_drawable_rect(x, y, width, height) { + self.update_fill_style(); self.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect)); } } @@ -668,6 +669,7 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect pub fn stroke_rect(&self, x: f64, y: f64, width: f64, height: f64) { if let Some(rect) = self.create_drawable_rect(x, y, width, height) { + self.update_stroke_style(); self.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect)); } } @@ -756,24 +758,15 @@ impl CanvasState { StringOrCanvasGradientOrCanvasPattern::String(string) => { if let Ok(rgba) = self.parse_color(canvas, &string) { self.state.borrow_mut().stroke_style = CanvasFillOrStrokeStyle::Color(rgba); - self.send_canvas_2d_msg(Canvas2dMsg::SetStrokeStyle(FillOrStrokeStyle::Color( - rgba, - ))); } }, StringOrCanvasGradientOrCanvasPattern::CanvasGradient(gradient) => { self.state.borrow_mut().stroke_style = CanvasFillOrStrokeStyle::Gradient(Dom::from_ref(&*gradient)); - self.send_canvas_2d_msg(Canvas2dMsg::SetStrokeStyle( - gradient.to_fill_or_stroke_style(), - )); }, StringOrCanvasGradientOrCanvasPattern::CanvasPattern(pattern) => { self.state.borrow_mut().stroke_style = CanvasFillOrStrokeStyle::Pattern(Dom::from_ref(&*pattern)); - self.send_canvas_2d_msg(Canvas2dMsg::SetStrokeStyle( - pattern.to_fill_or_stroke_style(), - )); if !pattern.origin_is_clean() { self.set_origin_unclean(); } @@ -781,6 +774,15 @@ impl CanvasState { } } + fn update_stroke_style(&self) { + let style = match &self.state.borrow().stroke_style { + CanvasFillOrStrokeStyle::Color(rgba) => FillOrStrokeStyle::Color(*rgba), + CanvasFillOrStrokeStyle::Gradient(gradient) => gradient.to_fill_or_stroke_style(), + CanvasFillOrStrokeStyle::Pattern(pattern) => pattern.to_fill_or_stroke_style(), + }; + self.send_canvas_2d_msg(Canvas2dMsg::SetStrokeStyle(style)); + } + // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle pub fn fill_style(&self) -> StringOrCanvasGradientOrCanvasPattern { match self.state.borrow().fill_style { @@ -808,24 +810,15 @@ impl CanvasState { StringOrCanvasGradientOrCanvasPattern::String(string) => { if let Ok(rgba) = self.parse_color(canvas, &string) { self.state.borrow_mut().fill_style = CanvasFillOrStrokeStyle::Color(rgba); - self.send_canvas_2d_msg(Canvas2dMsg::SetFillStyle(FillOrStrokeStyle::Color( - rgba, - ))) } }, StringOrCanvasGradientOrCanvasPattern::CanvasGradient(gradient) => { self.state.borrow_mut().fill_style = CanvasFillOrStrokeStyle::Gradient(Dom::from_ref(&*gradient)); - self.send_canvas_2d_msg(Canvas2dMsg::SetFillStyle( - gradient.to_fill_or_stroke_style(), - )); }, StringOrCanvasGradientOrCanvasPattern::CanvasPattern(pattern) => { self.state.borrow_mut().fill_style = CanvasFillOrStrokeStyle::Pattern(Dom::from_ref(&*pattern)); - self.send_canvas_2d_msg(Canvas2dMsg::SetFillStyle( - pattern.to_fill_or_stroke_style(), - )); if !pattern.origin_is_clean() { self.set_origin_unclean(); } @@ -833,6 +826,15 @@ impl CanvasState { } } + fn update_fill_style(&self) { + let style = match &self.state.borrow().fill_style { + CanvasFillOrStrokeStyle::Color(rgba) => FillOrStrokeStyle::Color(*rgba), + CanvasFillOrStrokeStyle::Gradient(gradient) => gradient.to_fill_or_stroke_style(), + CanvasFillOrStrokeStyle::Pattern(pattern) => pattern.to_fill_or_stroke_style(), + }; + self.send_canvas_2d_msg(Canvas2dMsg::SetFillStyle(style)); + } + // https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient pub fn create_linear_gradient( &self, @@ -995,6 +997,7 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext pub fn fill_text(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) { let parsed_text: String = text.into(); + self.update_fill_style(); self.send_canvas_2d_msg(Canvas2dMsg::FillText(parsed_text, x, y, max_width)); } @@ -1302,11 +1305,13 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-fill pub fn fill(&self, _fill_rule: CanvasFillRule) { // TODO: Process fill rule + self.update_fill_style(); self.send_canvas_2d_msg(Canvas2dMsg::Fill); } // https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke pub fn stroke(&self) { + self.update_stroke_style(); self.send_canvas_2d_msg(Canvas2dMsg::Stroke); } |