Skip to main content

script/dom/canvas/2d/
canvasrenderingcontext2d.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use euclid::default::Size2D;
7use js::context::{JSContext, NoGC};
8use pixels::Snapshot;
9use script_bindings::reflector::{AssociatedMemory, Reflector, reflect_dom_object_with_cx};
10use servo_base::{Epoch, generic_channel};
11use servo_canvas_traits::canvas::{CanvasCommand, CanvasId};
12use webrender_api::ImageKey;
13
14use super::canvas_state::CanvasState;
15use crate::canvas_context::{CanvasContext, CanvasHelpers, HTMLCanvasElementOrOffscreenCanvas};
16use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
17    CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
18    CanvasRenderingContext2DMethods, CanvasTextAlign, CanvasTextBaseline,
19};
20use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
21use crate::dom::bindings::codegen::UnionTypes::{
22    HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
23    StringOrCanvasGradientOrCanvasPattern,
24};
25use crate::dom::bindings::error::{ErrorResult, Fallible};
26use crate::dom::bindings::num::Finite;
27use crate::dom::bindings::reflector::DomGlobal;
28use crate::dom::bindings::root::{Dom, DomRoot};
29use crate::dom::bindings::str::DOMString;
30use crate::dom::canvasgradient::CanvasGradient;
31use crate::dom::canvaspattern::CanvasPattern;
32use crate::dom::dommatrix::DOMMatrix;
33use crate::dom::globalscope::GlobalScope;
34use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
35use crate::dom::imagedata::ImageData;
36use crate::dom::path2d::Path2D;
37use crate::dom::textmetrics::TextMetrics;
38
39// https://html.spec.whatwg.org/multipage/#canvasrenderingcontext2d
40#[dom_struct(associated_memory)]
41pub(crate) struct CanvasRenderingContext2D {
42    reflector_: Reflector<AssociatedMemory>,
43    canvas: HTMLCanvasElementOrOffscreenCanvas,
44    canvas_state: CanvasState,
45}
46
47impl CanvasRenderingContext2D {
48    const RGBA8_BYTES_PER_PIXEL: usize = 4;
49    /// Ideally this would be two bitmap buffers, unfortunately we currently have ~4 copies
50    /// which are all retained until GC:
51    ///
52    /// 1. The draw target's backing bitmap in the canvas paint thread
53    ///    (`CanvasData::draw_target`, e.g. `pixmap` in `VelloCPUDrawTarget`).
54    /// 2. Additional backend resources (approximated with 1 bitmap buffer, actually depends on backend):
55    ///    e.g. `ctx` and `resources` in `VelloCPUDrawTarget` for vello_cpu.
56    ///    TODO: #46785 tracks getting accurate statistics from the backend.
57    /// 3. `CachedImageData::Raw` in WebRender (cpu memory copy).
58    /// 4. GPU memory in webrender / driver (also lives in RAM on unified memory systems).
59    ///
60    /// This metric is only used to inform the GC about memory pressure, so it doesn't need to
61    /// be completely accurate, but undercounting may delay GCs and hence increase memory usage.
62    /// On mobile devices this slightly undercounts (usually unified memory), on desktop this
63    /// slightly overcounts, since GPU memory is separate.
64    const ASSOCIATED_MEMORY_BUFFER_COUNT: usize = 4;
65
66    fn associated_memory_size(size: Size2D<u64>) -> usize {
67        (size.width as usize)
68            .saturating_mul(size.height as usize)
69            .saturating_mul(Self::RGBA8_BYTES_PER_PIXEL)
70            .saturating_mul(Self::ASSOCIATED_MEMORY_BUFFER_COUNT)
71    }
72
73    pub(crate) fn update_associated_memory_size(&self) {
74        self.reflector_.update_memory_size(
75            self,
76            Self::associated_memory_size(self.canvas_state.bitmap_dimensions()),
77        );
78    }
79
80    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
81    pub(crate) fn new_inherited(
82        global: &GlobalScope,
83        canvas: HTMLCanvasElementOrOffscreenCanvas,
84        size: Size2D<u32>,
85    ) -> Option<CanvasRenderingContext2D> {
86        let canvas_state =
87            CanvasState::new(global, Size2D::new(size.width as u64, size.height as u64))?;
88        Some(CanvasRenderingContext2D {
89            reflector_: Reflector::new(),
90            canvas,
91            canvas_state,
92        })
93    }
94
95    pub(crate) fn new(
96        cx: &mut JSContext,
97        global: &GlobalScope,
98        canvas: &HTMLCanvasElement,
99        size: Size2D<u32>,
100    ) -> Option<DomRoot<CanvasRenderingContext2D>> {
101        CanvasRenderingContext2D::new_inherited(
102            global,
103            HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(Dom::from_ref(canvas)),
104            size,
105        )
106        .map(|context| {
107            let context = reflect_dom_object_with_cx(Box::new(context), global, cx);
108            context.update_associated_memory_size();
109            context
110        })
111    }
112
113    pub(crate) fn send_canvas_command_immediate(&self, msg: CanvasCommand) {
114        self.canvas_state.send_canvas_command_immediate(msg)
115    }
116
117    pub(crate) fn set_image_key(&self, image_key: ImageKey) {
118        self.canvas_state.set_image_key(image_key);
119    }
120
121    pub(crate) fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
122        if !self.onscreen() {
123            return false;
124        }
125        self.canvas_state.update_rendering(Some(canvas_epoch))
126    }
127}
128
129impl CanvasContext for CanvasRenderingContext2D {
130    type ID = CanvasId;
131
132    fn context_id(&self) -> Self::ID {
133        self.canvas_state.get_canvas_id()
134    }
135
136    fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
137        Some(RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas))
138    }
139
140    fn resize(&self) {
141        self.canvas_state.set_bitmap_dimensions(self.size().cast());
142        self.update_associated_memory_size();
143    }
144
145    fn reset_bitmap(&self) {
146        self.canvas_state.reset_bitmap()
147    }
148
149    fn get_image_data(&self) -> Option<Snapshot> {
150        if !self.canvas_state.is_paintable() {
151            return None;
152        }
153
154        let (sender, receiver) = generic_channel::channel().unwrap();
155        self.canvas_state
156            .send_canvas_command_immediate(CanvasCommand::GetImageData(None, sender));
157        Some(receiver.recv().unwrap().to_owned())
158    }
159
160    fn origin_is_clean(&self) -> bool {
161        self.canvas_state.origin_is_clean()
162    }
163
164    fn mark_as_dirty(&self) {
165        self.canvas.mark_as_dirty();
166    }
167}
168
169// We add a guard to each of methods by the spec:
170// http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas_CR/
171//
172// > Except where otherwise specified, for the 2D context interface,
173// > any method call with a numeric argument whose value is infinite or a NaN value must be ignored.
174//
175//  Restricted values are guarded in glue code. Therefore we need not add a guard.
176//
177// FIXME: this behavior should might be generated by some annotations to idl.
178impl CanvasRenderingContext2DMethods<crate::DomTypeHolder> for CanvasRenderingContext2D {
179    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-canvas>
180    fn Canvas(&self) -> DomRoot<HTMLCanvasElement> {
181        match &self.canvas {
182            HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => canvas.as_rooted(),
183            _ => panic!("Should not be called from offscreen canvas"),
184        }
185    }
186
187    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-save>
188    fn Save(&self) {
189        self.canvas_state.save()
190    }
191
192    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-restore>
193    fn Restore(&self) {
194        self.canvas_state.restore()
195    }
196
197    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
198    fn Reset(&self) {
199        self.canvas_state.reset()
200    }
201
202    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-scale>
203    fn Scale(&self, x: f64, y: f64) {
204        self.canvas_state.scale(x, y)
205    }
206
207    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate>
208    fn Rotate(&self, angle: f64) {
209        self.canvas_state.rotate(angle)
210    }
211
212    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-translate>
213    fn Translate(&self, x: f64, y: f64) {
214        self.canvas_state.translate(x, y)
215    }
216
217    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-transform>
218    fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
219        self.canvas_state.transform(a, b, c, d, e, f)
220    }
221
222    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform>
223    fn GetTransform(&self, cx: &mut JSContext) -> DomRoot<DOMMatrix> {
224        self.canvas_state.get_transform(&self.global(), cx)
225    }
226
227    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
228    fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
229        self.canvas_state.set_transform(a, b, c, d, e, f);
230        Ok(())
231    }
232
233    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
234    fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
235        self.canvas_state.set_transform_(transform)
236    }
237
238    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform>
239    fn ResetTransform(&self) {
240        self.canvas_state.reset_transform()
241    }
242
243    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha>
244    fn GlobalAlpha(&self) -> f64 {
245        self.canvas_state.global_alpha()
246    }
247
248    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha>
249    fn SetGlobalAlpha(&self, alpha: f64) {
250        self.canvas_state.set_global_alpha(alpha)
251    }
252
253    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation>
254    fn GlobalCompositeOperation(&self) -> DOMString {
255        self.canvas_state.global_composite_operation()
256    }
257
258    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation>
259    fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
260        self.canvas_state.set_global_composite_operation(op_str)
261    }
262
263    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect>
264    fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
265        self.canvas_state.fill_rect(x, y, width, height);
266        self.mark_as_dirty();
267    }
268
269    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect>
270    fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
271        self.canvas_state.clear_rect(x, y, width, height);
272        self.mark_as_dirty();
273    }
274
275    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect>
276    fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
277        self.canvas_state.stroke_rect(x, y, width, height);
278        self.mark_as_dirty();
279    }
280
281    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath>
282    fn BeginPath(&self) {
283        self.canvas_state.begin_path()
284    }
285
286    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath>
287    fn ClosePath(&self) {
288        self.canvas_state.close_path()
289    }
290
291    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fill>
292    fn Fill(&self, fill_rule: CanvasFillRule) {
293        self.canvas_state.fill(fill_rule);
294        self.mark_as_dirty();
295    }
296
297    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fill>
298    fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
299        self.canvas_state.fill_(path.segments(), fill_rule);
300        self.mark_as_dirty();
301    }
302
303    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke>
304    fn Stroke(&self) {
305        self.canvas_state.stroke();
306        self.mark_as_dirty();
307    }
308
309    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke>
310    fn Stroke_(&self, path: &Path2D) {
311        self.canvas_state.stroke_(path.segments());
312        self.mark_as_dirty();
313    }
314
315    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clip>
316    fn Clip(&self, fill_rule: CanvasFillRule) {
317        self.canvas_state.clip(fill_rule)
318    }
319
320    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clip>
321    fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
322        self.canvas_state.clip_(path.segments(), fill_rule)
323    }
324
325    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath>
326    fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
327        self.canvas_state
328            .is_point_in_path(&self.global(), x, y, fill_rule)
329    }
330
331    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath>
332    fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
333        self.canvas_state
334            .is_point_in_path_(&self.global(), path.segments(), x, y, fill_rule)
335    }
336
337    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext>
338    fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
339        self.canvas_state.fill_text(
340            &self.global(),
341            self.canvas.canvas().as_deref(),
342            text,
343            x,
344            y,
345            max_width,
346        );
347        self.mark_as_dirty();
348    }
349
350    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroketext>
351    fn StrokeText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
352        self.canvas_state.stroke_text(
353            &self.global(),
354            self.canvas.canvas().as_deref(),
355            text,
356            x,
357            y,
358            max_width,
359        );
360        self.mark_as_dirty();
361    }
362
363    /// <https://html.spec.whatwg.org/multipage/#textmetrics>
364    fn MeasureText(&self, cx: &mut JSContext, text: DOMString) -> DomRoot<TextMetrics> {
365        self.canvas_state
366            .measure_text(&self.global(), self.canvas.canvas().as_deref(), text, cx)
367    }
368
369    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-font>
370    fn Font(&self) -> DOMString {
371        self.canvas_state.font()
372    }
373
374    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-font>
375    fn SetFont(&self, value: DOMString) {
376        self.canvas_state
377            .set_font(self.canvas.canvas().as_deref(), value)
378    }
379
380    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign>
381    fn TextAlign(&self) -> CanvasTextAlign {
382        self.canvas_state.text_align()
383    }
384
385    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign>
386    fn SetTextAlign(&self, value: CanvasTextAlign) {
387        self.canvas_state.set_text_align(value)
388    }
389
390    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline>
391    fn TextBaseline(&self) -> CanvasTextBaseline {
392        self.canvas_state.text_baseline()
393    }
394
395    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline>
396    fn SetTextBaseline(&self, value: CanvasTextBaseline) {
397        self.canvas_state.set_text_baseline(value)
398    }
399
400    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-direction>
401    fn Direction(&self) -> CanvasDirection {
402        self.canvas_state.direction()
403    }
404
405    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-direction>
406    fn SetDirection(&self, value: CanvasDirection) {
407        self.canvas_state.set_direction(value)
408    }
409
410    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
411    fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
412        self.canvas_state
413            .draw_image(self.canvas.canvas().as_deref(), image, dx, dy)
414    }
415
416    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
417    fn DrawImage_(
418        &self,
419        image: CanvasImageSource,
420        dx: f64,
421        dy: f64,
422        dw: f64,
423        dh: f64,
424    ) -> ErrorResult {
425        self.canvas_state
426            .draw_image_(self.canvas.canvas().as_deref(), image, dx, dy, dw, dh)
427    }
428
429    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
430    fn DrawImage__(
431        &self,
432        image: CanvasImageSource,
433        sx: f64,
434        sy: f64,
435        sw: f64,
436        sh: f64,
437        dx: f64,
438        dy: f64,
439        dw: f64,
440        dh: f64,
441    ) -> ErrorResult {
442        self.canvas_state.draw_image__(
443            self.canvas.canvas().as_deref(),
444            image,
445            sx,
446            sy,
447            sw,
448            sh,
449            dx,
450            dy,
451            dw,
452            dh,
453        )
454    }
455
456    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto>
457    fn MoveTo(&self, x: f64, y: f64) {
458        self.canvas_state.move_to(x, y)
459    }
460
461    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto>
462    fn LineTo(&self, x: f64, y: f64) {
463        self.canvas_state.line_to(x, y)
464    }
465
466    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rect>
467    fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
468        self.canvas_state.rect(x, y, width, height)
469    }
470
471    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto>
472    fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
473        self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
474    }
475
476    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto>
477    fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
478        self.canvas_state
479            .bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
480    }
481
482    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arc>
483    fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
484        self.canvas_state.arc(x, y, r, start, end, ccw)
485    }
486
487    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto>
488    fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
489        self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
490    }
491
492    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse>
493    fn Ellipse(
494        &self,
495        x: f64,
496        y: f64,
497        rx: f64,
498        ry: f64,
499        rotation: f64,
500        start: f64,
501        end: f64,
502        ccw: bool,
503    ) -> ErrorResult {
504        self.canvas_state
505            .ellipse(x, y, rx, ry, rotation, start, end, ccw)
506    }
507
508    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled>
509    fn ImageSmoothingEnabled(&self) -> bool {
510        self.canvas_state.image_smoothing_enabled()
511    }
512
513    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled>
514    fn SetImageSmoothingEnabled(&self, value: bool) {
515        self.canvas_state.set_image_smoothing_enabled(value)
516    }
517
518    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
519    fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
520        self.canvas_state.stroke_style()
521    }
522
523    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
524    fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
525        self.canvas_state
526            .set_stroke_style(self.canvas.canvas().as_deref(), value)
527    }
528
529    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
530    fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
531        self.canvas_state.fill_style()
532    }
533
534    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
535    fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
536        self.canvas_state
537            .set_fill_style(self.canvas.canvas().as_deref(), value)
538    }
539
540    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata>
541    fn CreateImageData(
542        &self,
543        cx: &mut JSContext,
544        sw: i32,
545        sh: i32,
546    ) -> Fallible<DomRoot<ImageData>> {
547        self.canvas_state
548            .create_image_data(cx, &self.global(), sw, sh)
549    }
550
551    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata>
552    fn CreateImageData_(
553        &self,
554        cx: &mut JSContext,
555        imagedata: &ImageData,
556    ) -> Fallible<DomRoot<ImageData>> {
557        self.canvas_state
558            .create_image_data_(cx, &self.global(), imagedata)
559    }
560
561    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getimagedata>
562    fn GetImageData(
563        &self,
564        cx: &mut JSContext,
565        sx: i32,
566        sy: i32,
567        sw: i32,
568        sh: i32,
569    ) -> Fallible<DomRoot<ImageData>> {
570        self.canvas_state
571            .get_image_data(cx, self.canvas.size(), &self.global(), sx, sy, sw, sh)
572    }
573
574    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata>
575    fn PutImageData(&self, no_gc: &NoGC, imagedata: &ImageData, dx: i32, dy: i32) {
576        self.canvas_state
577            .put_image_data(no_gc, self.canvas.size(), imagedata, dx, dy);
578        self.mark_as_dirty();
579    }
580
581    // https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
582    fn PutImageData_(
583        &self,
584        no_gc: &NoGC,
585        imagedata: &ImageData,
586        dx: i32,
587        dy: i32,
588        dirty_x: i32,
589        dirty_y: i32,
590        dirty_width: i32,
591        dirty_height: i32,
592    ) {
593        self.canvas_state.put_image_data_(
594            no_gc,
595            self.canvas.size(),
596            imagedata,
597            dx,
598            dy,
599            dirty_x,
600            dirty_y,
601            dirty_width,
602            dirty_height,
603        );
604        self.mark_as_dirty();
605    }
606
607    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient>
608    fn CreateLinearGradient(
609        &self,
610        cx: &mut JSContext,
611        x0: Finite<f64>,
612        y0: Finite<f64>,
613        x1: Finite<f64>,
614        y1: Finite<f64>,
615    ) -> DomRoot<CanvasGradient> {
616        self.canvas_state
617            .create_linear_gradient(&self.global(), cx, x0, y0, x1, y1)
618    }
619
620    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient>
621    fn CreateRadialGradient(
622        &self,
623        cx: &mut JSContext,
624        x0: Finite<f64>,
625        y0: Finite<f64>,
626        r0: Finite<f64>,
627        x1: Finite<f64>,
628        y1: Finite<f64>,
629        r1: Finite<f64>,
630    ) -> Fallible<DomRoot<CanvasGradient>> {
631        self.canvas_state
632            .create_radial_gradient(&self.global(), cx, x0, y0, r0, x1, y1, r1)
633    }
634
635    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern>
636    fn CreatePattern(
637        &self,
638        cx: &mut JSContext,
639        image: CanvasImageSource,
640        repetition: DOMString,
641    ) -> Fallible<Option<DomRoot<CanvasPattern>>> {
642        self.canvas_state
643            .create_pattern(&self.global(), cx, image, repetition)
644    }
645
646    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth>
647    fn LineWidth(&self) -> f64 {
648        self.canvas_state.line_width()
649    }
650
651    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth>
652    fn SetLineWidth(&self, width: f64) {
653        self.canvas_state.set_line_width(width)
654    }
655
656    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap>
657    fn LineCap(&self) -> CanvasLineCap {
658        self.canvas_state.line_cap()
659    }
660
661    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap>
662    fn SetLineCap(&self, cap: CanvasLineCap) {
663        self.canvas_state.set_line_cap(cap)
664    }
665
666    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin>
667    fn LineJoin(&self) -> CanvasLineJoin {
668        self.canvas_state.line_join()
669    }
670
671    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin>
672    fn SetLineJoin(&self, join: CanvasLineJoin) {
673        self.canvas_state.set_line_join(join)
674    }
675
676    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit>
677    fn MiterLimit(&self) -> f64 {
678        self.canvas_state.miter_limit()
679    }
680
681    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit>
682    fn SetMiterLimit(&self, limit: f64) {
683        self.canvas_state.set_miter_limit(limit)
684    }
685
686    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
687    fn SetLineDash(&self, segments: Vec<f64>) {
688        self.canvas_state.set_line_dash(segments);
689    }
690
691    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
692    fn GetLineDash(&self) -> Vec<f64> {
693        self.canvas_state.line_dash()
694    }
695
696    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
697    fn LineDashOffset(&self) -> f64 {
698        self.canvas_state.line_dash_offset()
699    }
700
701    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
702    fn SetLineDashOffset(&self, offset: f64) {
703        self.canvas_state.set_line_dash_offset(offset);
704    }
705
706    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx>
707    fn ShadowOffsetX(&self) -> f64 {
708        self.canvas_state.shadow_offset_x()
709    }
710
711    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx>
712    fn SetShadowOffsetX(&self, value: f64) {
713        self.canvas_state.set_shadow_offset_x(value)
714    }
715
716    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety>
717    fn ShadowOffsetY(&self) -> f64 {
718        self.canvas_state.shadow_offset_y()
719    }
720
721    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety>
722    fn SetShadowOffsetY(&self, value: f64) {
723        self.canvas_state.set_shadow_offset_y(value)
724    }
725
726    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur>
727    fn ShadowBlur(&self) -> f64 {
728        self.canvas_state.shadow_blur()
729    }
730
731    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur>
732    fn SetShadowBlur(&self, value: f64) {
733        self.canvas_state.set_shadow_blur(value)
734    }
735
736    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor>
737    fn ShadowColor(&self) -> DOMString {
738        self.canvas_state.shadow_color()
739    }
740
741    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor>
742    fn SetShadowColor(&self, value: DOMString) {
743        self.canvas_state
744            .set_shadow_color(self.canvas.canvas().as_deref(), value)
745    }
746}