diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2020-01-25 22:06:47 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-25 22:06:47 -0500 |
commit | 0bd266149263cbdcd0ddd677ab3a7eaa0322b660 (patch) | |
tree | b68daa966a4e2ac3c75d4f8e63ea751ffb0df40e /components/script | |
parent | 694bb408cc4d59394ca2698967606f28ba053e43 (diff) | |
parent | 5a773bf55a3dfd1b486887c3c36bafe0cbe58f2e (diff) | |
download | servo-0bd266149263cbdcd0ddd677ab3a7eaa0322b660.tar.gz servo-0bd266149263cbdcd0ddd677ab3a7eaa0322b660.zip |
Auto merge of #25603 - pylbrecht:raqote, r=jdm
Update CanvasRenderingContext2D fillStyle and strokeStyle 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 along with the drawing operations that require it.
<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix part of #25331
<!-- Either: -->
- [x] There are tests for these changes
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/canvas_state.rs | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/components/script/canvas_state.rs b/components/script/canvas_state.rs index 0416c96aff0..c9492ed5da5 100644 --- a/components/script/canvas_state.rs +++ b/components/script/canvas_state.rs @@ -64,6 +64,16 @@ pub(crate) enum CanvasFillOrStrokeStyle { Pattern(Dom<CanvasPattern>), } +impl CanvasFillOrStrokeStyle { + fn to_fill_or_stroke_style(&self) -> FillOrStrokeStyle { + match self { + CanvasFillOrStrokeStyle::Color(rgba) => FillOrStrokeStyle::Color(*rgba), + CanvasFillOrStrokeStyle::Gradient(gradient) => gradient.to_fill_or_stroke_style(), + CanvasFillOrStrokeStyle::Pattern(pattern) => pattern.to_fill_or_stroke_style(), + } + } +} + #[unrooted_must_root_lint::must_root] #[derive(Clone, JSTraceable, MallocSizeOf)] pub(crate) struct CanvasContextState { @@ -654,7 +664,8 @@ 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.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect)); + let style = self.state.borrow().fill_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect, style)); } } @@ -668,7 +679,8 @@ 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.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect)); + let style = self.state.borrow().stroke_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect, style)); } } @@ -756,24 +768,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(); } @@ -808,24 +811,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(); } @@ -995,7 +989,8 @@ 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.send_canvas_2d_msg(Canvas2dMsg::FillText(parsed_text, x, y, max_width)); + let style = self.state.borrow().fill_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::FillText(parsed_text, x, y, max_width, style)); } // https://html.spec.whatwg.org/multipage/#textmetrics @@ -1302,12 +1297,14 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-fill pub fn fill(&self, _fill_rule: CanvasFillRule) { // TODO: Process fill rule - self.send_canvas_2d_msg(Canvas2dMsg::Fill); + let style = self.state.borrow().fill_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::Fill(style)); } // https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke pub fn stroke(&self) { - self.send_canvas_2d_msg(Canvas2dMsg::Stroke); + let style = self.state.borrow().stroke_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::Stroke(style)); } // https://html.spec.whatwg.org/multipage/#dom-context-2d-clip |