Skip to content

Commit

Permalink
Update fill and stroke style only when required
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pylbrecht committed Jan 25, 2020
1 parent 937efba commit ebdf469
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
41 changes: 23 additions & 18 deletions components/script/canvas_state.rs
Expand Up @@ -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));
}
}
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -756,31 +758,31 @@ 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();
}
},
}
}

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 {
Expand Down Expand Up @@ -808,31 +810,31 @@ 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();
}
},
}
}

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,
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit ebdf469

Please sign in to comment.