script/dom/html/
htmlcanvaselement.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 std::cell::{Cell, RefCell};
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use euclid::default::Size2D;
10use html5ever::{LocalName, Prefix, local_name, ns};
11use js::error::throw_type_error;
12use js::rust::{HandleObject, HandleValue};
13use layout_api::HTMLCanvasData;
14use pixels::{EncodedImageType, Snapshot};
15use rustc_hash::FxHashMap;
16use script_bindings::weakref::WeakRef;
17use servo_base::Epoch;
18use servo_canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
19use servo_constellation_traits::BlobImpl;
20#[cfg(feature = "webgpu")]
21use servo_constellation_traits::ScriptToConstellationMessage;
22use servo_media::streams::MediaStreamType;
23use servo_media::streams::registry::MediaStreamId;
24use style::attr::AttrValue;
25use webrender_api::ImageKey;
26
27use crate::canvas_context::{CanvasContext, RenderingContext};
28use crate::conversions::Convert;
29use crate::dom::attr::Attr;
30use crate::dom::bindings::callback::ExceptionHandling;
31use crate::dom::bindings::cell::{DomRefCell, Ref};
32use crate::dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::{
33    BlobCallback, HTMLCanvasElementMethods, RenderingContext as RootedRenderingContext,
34};
35use crate::dom::bindings::codegen::Bindings::MediaStreamBinding::MediaStreamMethods;
36use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
37use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas;
38use crate::dom::bindings::conversions::ConversionResult;
39use crate::dom::bindings::error::{Error, Fallible};
40use crate::dom::bindings::inheritance::Castable;
41use crate::dom::bindings::num::Finite;
42use crate::dom::bindings::refcounted::Trusted;
43use crate::dom::bindings::reflector::{DomGlobal, DomObject};
44use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom};
45use crate::dom::bindings::str::{DOMString, USVString};
46use crate::dom::blob::Blob;
47use crate::dom::canvasrenderingcontext2d::CanvasRenderingContext2D;
48use crate::dom::document::Document;
49use crate::dom::element::{AttributeMutation, Element, LayoutElementHelpers};
50#[cfg(not(feature = "webgpu"))]
51use crate::dom::gpucanvascontext::GPUCanvasContext;
52use crate::dom::html::htmlelement::HTMLElement;
53use crate::dom::imagebitmaprenderingcontext::ImageBitmapRenderingContext;
54use crate::dom::mediastream::MediaStream;
55use crate::dom::mediastreamtrack::MediaStreamTrack;
56use crate::dom::node::{Node, NodeDamage, NodeTraits};
57use crate::dom::offscreencanvas::OffscreenCanvas;
58use crate::dom::values::UNSIGNED_LONG_MAX;
59use crate::dom::virtualmethods::VirtualMethods;
60use crate::dom::webgl::webgl2renderingcontext::WebGL2RenderingContext;
61use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
62#[cfg(feature = "webgpu")]
63use crate::dom::webgpu::gpucanvascontext::GPUCanvasContext;
64use crate::script_runtime::{CanGc, JSContext};
65
66const DEFAULT_WIDTH: u32 = 300;
67const DEFAULT_HEIGHT: u32 = 150;
68
69/// <https://html.spec.whatwg.org/multipage/#htmlcanvaselement>
70#[dom_struct]
71pub(crate) struct HTMLCanvasElement {
72    htmlelement: HTMLElement,
73
74    /// <https://html.spec.whatwg.org/multipage/#concept-canvas-context-mode>
75    context_mode: DomRefCell<Option<RenderingContext>>,
76
77    /// This id along with [`Self::blob_callbacks`] are used to keep track of ongoing toBlob() calls.
78    callback_id: Cell<u32>,
79
80    /// This hashmap along with [`Self::callback_id`] are used to keep track of ongoing toBlob() calls.
81    #[conditional_malloc_size_of]
82    blob_callbacks: RefCell<FxHashMap<u32, Rc<BlobCallback>>>,
83
84    /// The [`ImageKey`] used to render this [`HTMLCanvasElement`] to the WebRender scene, if it
85    /// has a `RenderingContext`, otherwise `None`. Note that this key is owned by the `RenderingContext`
86    /// itself which will take care of cleaning it up.
87    #[no_trace]
88    image_key: Cell<Option<ImageKey>>,
89}
90
91impl HTMLCanvasElement {
92    fn new_inherited(
93        local_name: LocalName,
94        prefix: Option<Prefix>,
95        document: &Document,
96    ) -> HTMLCanvasElement {
97        HTMLCanvasElement {
98            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
99            context_mode: DomRefCell::new(None),
100            callback_id: Cell::new(0),
101            blob_callbacks: RefCell::new(FxHashMap::default()),
102            image_key: Default::default(),
103        }
104    }
105
106    pub(crate) fn new(
107        cx: &mut js::context::JSContext,
108        local_name: LocalName,
109        prefix: Option<Prefix>,
110        document: &Document,
111        proto: Option<HandleObject>,
112    ) -> DomRoot<HTMLCanvasElement> {
113        Node::reflect_node_with_proto(
114            cx,
115            Box::new(HTMLCanvasElement::new_inherited(
116                local_name, prefix, document,
117            )),
118            document,
119            proto,
120        )
121    }
122
123    fn recreate_contexts_after_resize(&self) {
124        if let Some(ref context) = *self.context_mode.borrow() {
125            context.resize()
126        }
127    }
128
129    pub(crate) fn get_size(&self) -> Size2D<u32> {
130        Size2D::new(self.Width(), self.Height())
131    }
132
133    pub(crate) fn origin_is_clean(&self) -> bool {
134        match *self.context_mode.borrow() {
135            Some(ref context) => context.origin_is_clean(),
136            _ => true,
137        }
138    }
139
140    pub(crate) fn mark_as_dirty(&self) {
141        if let Some(ref context) = *self.context_mode.borrow() {
142            context.mark_as_dirty()
143        }
144    }
145
146    pub(crate) fn set_natural_width(&self, value: u32, can_gc: CanGc) {
147        let value = if value > UNSIGNED_LONG_MAX {
148            DEFAULT_WIDTH
149        } else {
150            value
151        };
152        let element = self.upcast::<Element>();
153        element.set_uint_attribute(&html5ever::local_name!("width"), value, can_gc);
154    }
155
156    pub(crate) fn set_natural_height(&self, value: u32, can_gc: CanGc) {
157        let value = if value > UNSIGNED_LONG_MAX {
158            DEFAULT_HEIGHT
159        } else {
160            value
161        };
162        let element = self.upcast::<Element>();
163        element.set_uint_attribute(&html5ever::local_name!("height"), value, can_gc);
164    }
165}
166
167pub(crate) trait LayoutHTMLCanvasElementHelpers {
168    fn data(self) -> HTMLCanvasData;
169}
170
171impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> {
172    fn data(self) -> HTMLCanvasData {
173        let width_attr = self
174            .upcast::<Element>()
175            .get_attr_for_layout(&ns!(), &local_name!("width"));
176        let height_attr = self
177            .upcast::<Element>()
178            .get_attr_for_layout(&ns!(), &local_name!("height"));
179        HTMLCanvasData {
180            image_key: self.unsafe_get().image_key.get(),
181            width: width_attr.map_or(DEFAULT_WIDTH, |val| val.as_uint()),
182            height: height_attr.map_or(DEFAULT_HEIGHT, |val| val.as_uint()),
183        }
184    }
185}
186
187impl HTMLCanvasElement {
188    pub(crate) fn context(&self) -> Option<Ref<'_, RenderingContext>> {
189        Ref::filter_map(self.context_mode.borrow(), |ctx| ctx.as_ref()).ok()
190    }
191
192    fn set_rendering_context(&self, make_rendering_context: impl FnOnce() -> RenderingContext) {
193        self.upcast::<Node>().dirty(NodeDamage::ContentOrHeritage);
194        self.context_mode
195            .borrow_mut()
196            .replace(make_rendering_context());
197
198        let Some(rendering_context) = &*self.context_mode.borrow() else {
199            return;
200        };
201
202        let get_image_key = || self.owner_window().image_cache().get_image_key();
203        let image_key = match rendering_context {
204            RenderingContext::Placeholder(..) => None,
205            RenderingContext::Context2d(..) => get_image_key(),
206            RenderingContext::BitmapRenderer(_) => None,
207            RenderingContext::WebGL(..) => get_image_key(),
208            RenderingContext::WebGL2(..) => get_image_key(),
209            #[cfg(feature = "webgpu")]
210            RenderingContext::WebGPU(..) => get_image_key(),
211        };
212        self.image_key.set(image_key);
213        if let Some(image_key) = image_key {
214            rendering_context.set_image_key(image_key);
215        }
216    }
217
218    fn get_or_init_2d_context(&self, can_gc: CanGc) -> Option<DomRoot<CanvasRenderingContext2D>> {
219        if let Some(ctx) = self.context() {
220            return match *ctx {
221                RenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
222                _ => None,
223            };
224        }
225
226        let window = self.owner_window();
227        let size = self.get_size();
228        let context = CanvasRenderingContext2D::new(window.as_global_scope(), self, size, can_gc)?;
229        self.set_rendering_context(|| RenderingContext::Context2d(Dom::from_ref(&*context)));
230        Some(context)
231    }
232
233    /// <https://html.spec.whatwg.org/multipage/#canvas-context-bitmaprenderer>
234    fn get_or_init_bitmaprenderer_context(
235        &self,
236        can_gc: CanGc,
237    ) -> Option<DomRoot<ImageBitmapRenderingContext>> {
238        // Return the same object as was returned the last time the method was
239        // invoked with this same first argument.
240        if let Some(ctx) = self.context() {
241            return match *ctx {
242                RenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
243                _ => None,
244            };
245        }
246
247        // Step 1. Let context be the result of running the
248        // ImageBitmapRenderingContext creation algorithm given this and
249        // options.
250        let canvas =
251            RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
252
253        // Step 2. Set this's context mode to bitmaprenderer.
254        let context = ImageBitmapRenderingContext::new(&self.owner_global(), &canvas, can_gc);
255        self.set_rendering_context(|| RenderingContext::BitmapRenderer(Dom::from_ref(&*context)));
256
257        // Step 3. Return context.
258        Some(context)
259    }
260
261    fn get_or_init_webgl_context(
262        &self,
263        cx: JSContext,
264        options: HandleValue,
265        can_gc: CanGc,
266    ) -> Option<DomRoot<WebGLRenderingContext>> {
267        if let Some(ctx) = self.context() {
268            return match *ctx {
269                RenderingContext::WebGL(ref ctx) => Some(DomRoot::from_ref(ctx)),
270                _ => None,
271            };
272        }
273        let window = self.owner_window();
274        let canvas =
275            RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
276        let size = self.get_size();
277        let attrs = Self::get_gl_attributes(cx, options, can_gc)?;
278        let context = WebGLRenderingContext::new(
279            &window,
280            &canvas,
281            WebGLVersion::WebGL1,
282            size,
283            attrs,
284            can_gc,
285        )?;
286        self.set_rendering_context(|| RenderingContext::WebGL(Dom::from_ref(&*context)));
287        Some(context)
288    }
289
290    fn get_or_init_webgl2_context(
291        &self,
292        cx: JSContext,
293        options: HandleValue,
294        can_gc: CanGc,
295    ) -> Option<DomRoot<WebGL2RenderingContext>> {
296        if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
297        {
298            return None;
299        }
300        if let Some(ctx) = self.context() {
301            return match *ctx {
302                RenderingContext::WebGL2(ref ctx) => Some(DomRoot::from_ref(ctx)),
303                _ => None,
304            };
305        }
306        let window = self.owner_window();
307        let canvas =
308            RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
309        let size = self.get_size();
310        let attrs = Self::get_gl_attributes(cx, options, can_gc)?;
311        let context = WebGL2RenderingContext::new(&window, &canvas, size, attrs, can_gc)?;
312        self.set_rendering_context(|| RenderingContext::WebGL2(Dom::from_ref(&*context)));
313        Some(context)
314    }
315
316    #[cfg(not(feature = "webgpu"))]
317    fn get_or_init_webgpu_context(&self) -> Option<DomRoot<GPUCanvasContext>> {
318        None
319    }
320
321    #[cfg(feature = "webgpu")]
322    fn get_or_init_webgpu_context(&self, can_gc: CanGc) -> Option<DomRoot<GPUCanvasContext>> {
323        use servo_base::generic_channel;
324
325        if let Some(ctx) = self.context() {
326            return match *ctx {
327                RenderingContext::WebGPU(ref ctx) => Some(DomRoot::from_ref(ctx)),
328                _ => None,
329            };
330        }
331        let (sender, receiver) = generic_channel::channel().unwrap();
332        let global_scope = self.owner_global();
333        let _ = global_scope
334            .script_to_constellation_chan()
335            .send(ScriptToConstellationMessage::GetWebGPUChan(sender));
336        receiver
337            .recv()
338            .expect("Failed to get WebGPU channel")
339            .map(|channel| {
340                let context = GPUCanvasContext::new(&global_scope, self, channel, can_gc);
341                self.set_rendering_context(|| RenderingContext::WebGPU(Dom::from_ref(&*context)));
342                context
343            })
344    }
345
346    /// Gets the base WebGLRenderingContext for WebGL or WebGL 2, if exists.
347    pub(crate) fn get_base_webgl_context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
348        match *self.context_mode.borrow() {
349            Some(RenderingContext::WebGL(ref context)) => Some(DomRoot::from_ref(context)),
350            Some(RenderingContext::WebGL2(ref context)) => Some(context.base_context()),
351            _ => None,
352        }
353    }
354
355    #[expect(unsafe_code)]
356    fn get_gl_attributes(
357        cx: JSContext,
358        options: HandleValue,
359        can_gc: CanGc,
360    ) -> Option<GLContextAttributes> {
361        unsafe {
362            match WebGLContextAttributes::new(cx, options, can_gc) {
363                Ok(ConversionResult::Success(attrs)) => Some(attrs.convert()),
364                Ok(ConversionResult::Failure(error)) => {
365                    throw_type_error(*cx, &error);
366                    None
367                },
368                _ => {
369                    debug!("Unexpected error on conversion of WebGLContextAttributes");
370                    None
371                },
372            }
373        }
374    }
375
376    pub(crate) fn is_valid(&self) -> bool {
377        self.Height() != 0 && self.Width() != 0
378    }
379
380    pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
381        match self.context_mode.borrow().as_ref() {
382            Some(context) => context.get_image_data(),
383            None => {
384                let size = self.get_size();
385                if size.is_empty() ||
386                    pixels::compute_rgba8_byte_length_if_within_limit(
387                        size.width as usize,
388                        size.height as usize,
389                    )
390                    .is_none()
391                {
392                    None
393                } else {
394                    Some(Snapshot::cleared(size.cast()))
395                }
396            },
397        }
398    }
399
400    fn maybe_quality(quality: HandleValue) -> Option<f64> {
401        if quality.is_number() {
402            Some(quality.to_number())
403        } else {
404            None
405        }
406    }
407
408    pub(crate) fn update_rendering(&self, epoch: Epoch) -> Option<ImageKey> {
409        let context = self.context()?;
410        let image_key = self.image_key.get()?;
411        match &*context {
412            RenderingContext::Placeholder(..) => false,
413            RenderingContext::Context2d(context) => context.update_rendering(epoch),
414            RenderingContext::BitmapRenderer(..) => false,
415            RenderingContext::WebGL(context) => context.update_rendering(epoch),
416            RenderingContext::WebGL2(context) => context.base_context().update_rendering(epoch),
417            #[cfg(feature = "webgpu")]
418            RenderingContext::WebGPU(context) => context.update_rendering(epoch),
419        }
420        .then_some(image_key)
421    }
422}
423
424impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
425    // https://html.spec.whatwg.org/multipage/#dom-canvas-width
426    make_uint_getter!(Width, "width", DEFAULT_WIDTH);
427
428    /// <https://html.spec.whatwg.org/multipage/#dom-canvas-width>
429    fn SetWidth(&self, value: u32, can_gc: CanGc) -> Fallible<()> {
430        // > When setting the value of the width or height attribute, if the context mode of the canvas element
431        // > is set to placeholder, the user agent must throw an "InvalidStateError" DOMException and leave the
432        // > attribute's value unchanged.
433        if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
434            return Err(Error::InvalidState(None));
435        }
436
437        let value = if value > UNSIGNED_LONG_MAX {
438            DEFAULT_WIDTH
439        } else {
440            value
441        };
442        let element = self.upcast::<Element>();
443        element.set_uint_attribute(&html5ever::local_name!("width"), value, can_gc);
444        Ok(())
445    }
446
447    // https://html.spec.whatwg.org/multipage/#dom-canvas-height
448    make_uint_getter!(Height, "height", DEFAULT_HEIGHT);
449
450    /// <https://html.spec.whatwg.org/multipage/#dom-canvas-height>
451    fn SetHeight(&self, value: u32, can_gc: CanGc) -> Fallible<()> {
452        // > When setting the value of the width or height attribute, if the context mode of the canvas element
453        // > is set to placeholder, the user agent must throw an "InvalidStateError" DOMException and leave the
454        // > attribute's value unchanged.
455        if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
456            return Err(Error::InvalidState(None));
457        }
458
459        let value = if value > UNSIGNED_LONG_MAX {
460            DEFAULT_HEIGHT
461        } else {
462            value
463        };
464        let element = self.upcast::<Element>();
465        element.set_uint_attribute(&html5ever::local_name!("height"), value, can_gc);
466        Ok(())
467    }
468
469    /// <https://html.spec.whatwg.org/multipage/#dom-canvas-getcontext>
470    fn GetContext(
471        &self,
472        cx: JSContext,
473        id: DOMString,
474        options: HandleValue,
475        can_gc: CanGc,
476    ) -> Fallible<Option<RootedRenderingContext>> {
477        // Always throw an InvalidState exception when the canvas is in Placeholder mode (See table in the spec).
478        if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
479            return Err(Error::InvalidState(None));
480        }
481
482        Ok(match &*id.str() {
483            "2d" => self
484                .get_or_init_2d_context(can_gc)
485                .map(RootedRenderingContext::CanvasRenderingContext2D),
486            "bitmaprenderer" => self
487                .get_or_init_bitmaprenderer_context(can_gc)
488                .map(RootedRenderingContext::ImageBitmapRenderingContext),
489            "webgl" | "experimental-webgl" => self
490                .get_or_init_webgl_context(cx, options, can_gc)
491                .map(RootedRenderingContext::WebGLRenderingContext),
492            "webgl2" | "experimental-webgl2" => self
493                .get_or_init_webgl2_context(cx, options, can_gc)
494                .map(RootedRenderingContext::WebGL2RenderingContext),
495            #[cfg(feature = "webgpu")]
496            "webgpu" => self
497                .get_or_init_webgpu_context(can_gc)
498                .map(RootedRenderingContext::GPUCanvasContext),
499            _ => None,
500        })
501    }
502
503    /// <https://html.spec.whatwg.org/multipage/#dom-canvas-todataurl>
504    fn ToDataURL(
505        &self,
506        _context: JSContext,
507        mime_type: DOMString,
508        quality: HandleValue,
509    ) -> Fallible<USVString> {
510        // Step 1: If this canvas element's bitmap's origin-clean flag is set to false,
511        // then throw a "SecurityError" DOMException.
512        if !self.origin_is_clean() {
513            return Err(Error::Security(None));
514        }
515
516        // Step 2: If this canvas element's bitmap has no pixels (i.e. either its
517        // horizontal dimension or its vertical dimension is zero), then return the string
518        // "data:,". (This is the shortest data: URL; it represents the empty string in a
519        // text/plain resource.)
520        if self.Width() == 0 || self.Height() == 0 {
521            return Ok(USVString("data:,".into()));
522        }
523
524        // Step 3: Let file be a serialization of this canvas element's bitmap as a file,
525        // passing type and quality if given.
526        let Some(mut snapshot) = self.get_image_data() else {
527            return Ok(USVString("data:,".into()));
528        };
529
530        let image_type = EncodedImageType::from(mime_type.to_string());
531
532        let mut url = format!("data:{};base64,", image_type.as_mime_type());
533
534        let mut encoder = base64::write::EncoderStringWriter::from_consumer(
535            &mut url,
536            &base64::engine::general_purpose::STANDARD,
537        );
538
539        if snapshot
540            .encode_for_mime_type(&image_type, Self::maybe_quality(quality), &mut encoder)
541            .is_err()
542        {
543            // Step 4. If file is null, then return "data:,".
544            return Ok(USVString("data:,".into()));
545        }
546
547        // Step 5. Return a data: URL representing file. [RFC2397]
548        encoder.into_inner();
549        Ok(USVString(url))
550    }
551
552    /// <https://html.spec.whatwg.org/multipage/#dom-canvas-toblob>
553    fn ToBlob(
554        &self,
555        _cx: JSContext,
556        callback: Rc<BlobCallback>,
557        mime_type: DOMString,
558        quality: HandleValue,
559    ) -> Fallible<()> {
560        // Step 1.
561        // If this canvas element's bitmap's origin-clean flag is set to false, then throw a
562        // "SecurityError" DOMException.
563        if !self.origin_is_clean() {
564            return Err(Error::Security(None));
565        }
566
567        // Step 2. Let result be null.
568        // Step 3. If this canvas element's bitmap has pixels (i.e., neither its horizontal dimension
569        // nor its vertical dimension is zero),
570        // then set result to a copy of this canvas element's bitmap.
571        let result = if self.Width() == 0 || self.Height() == 0 {
572            None
573        } else {
574            self.get_image_data()
575        };
576
577        let this = Trusted::new(self);
578        let callback_id = self.callback_id.get().wrapping_add(1);
579        self.callback_id.set(callback_id);
580
581        self.blob_callbacks
582            .borrow_mut()
583            .insert(callback_id, callback);
584        let quality = Self::maybe_quality(quality);
585        let image_type = EncodedImageType::from(mime_type.to_string());
586
587        self.global()
588            .task_manager()
589            .canvas_blob_task_source()
590            .queue(task!(to_blob: move || {
591                let this = this.root();
592                let Some(callback) = &this.blob_callbacks.borrow_mut().remove(&callback_id) else {
593                    return error!("Expected blob callback, but found none!");
594                };
595
596                let Some(mut snapshot) = result else {
597                    let _ = callback.Call__(None, ExceptionHandling::Report, CanGc::note());
598                    return;
599                };
600
601                // Step 4.1: If result is non-null, then set result to a serialization of
602                // result as a file with type and quality if given.
603                // Step 4.2: Queue an element task on the canvas blob serialization task
604                // source given the canvas element to run these steps:
605                let mut encoded: Vec<u8> = vec![];
606                let blob_impl;
607                let blob;
608                let result = match snapshot.encode_for_mime_type(&image_type, quality, &mut encoded) {
609                   Ok(..) => {
610                       // Step 4.2.1: If result is non-null, then set result to a new Blob
611                       // object, created in the relevant realm of this canvas element,
612                       // representing result. [FILEAPI]
613                       blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
614                       blob = Blob::new(&this.global(), blob_impl, CanGc::note());
615                       Some(&*blob)
616                   }
617                   Err(..) => None,
618                };
619
620                // Step 4.2.2: Invoke callback with « result » and "report".
621                let _ = callback.Call__(result, ExceptionHandling::Report, CanGc::note());
622            }));
623
624        Ok(())
625    }
626
627    /// <https://html.spec.whatwg.org/multipage/#dom-canvas-transfercontroltooffscreen>
628    fn TransferControlToOffscreen(&self, can_gc: CanGc) -> Fallible<DomRoot<OffscreenCanvas>> {
629        if self.context_mode.borrow().is_some() {
630            // Step 1.
631            // If this canvas element's context mode is not set to none, throw an "InvalidStateError" DOMException.
632            return Err(Error::InvalidState(None));
633        };
634
635        // Step 2.
636        // Let offscreenCanvas be a new OffscreenCanvas object with its width and height equal to the values of
637        // the width and height content attributes of this canvas element.
638        // Step 3.
639        // Set the placeholder canvas element of offscreenCanvas to a weak reference to this canvas element.
640        let offscreen_canvas = OffscreenCanvas::new(
641            &self.global(),
642            None,
643            self.Width().into(),
644            self.Height().into(),
645            Some(WeakRef::new(self)),
646            can_gc,
647        );
648
649        // Step 4. Set this canvas element's context mode to placeholder.
650        self.set_rendering_context(|| RenderingContext::Placeholder(offscreen_canvas.as_traced()));
651
652        // Step 5. Return offscreenCanvas.
653        Ok(offscreen_canvas)
654    }
655
656    /// <https://w3c.github.io/mediacapture-fromelement/#dom-htmlcanvaselement-capturestream>
657    fn CaptureStream(
658        &self,
659        _frame_request_rate: Option<Finite<f64>>,
660        can_gc: CanGc,
661    ) -> DomRoot<MediaStream> {
662        let global = self.global();
663        let stream = MediaStream::new(&global, can_gc);
664        let track = MediaStreamTrack::new(
665            &global,
666            MediaStreamId::new(),
667            MediaStreamType::Video,
668            can_gc,
669        );
670        stream.AddTrack(&track);
671        stream
672    }
673}
674
675impl VirtualMethods for HTMLCanvasElement {
676    fn super_type(&self) -> Option<&dyn VirtualMethods> {
677        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
678    }
679
680    fn attribute_mutated(
681        &self,
682        cx: &mut js::context::JSContext,
683        attr: &Attr,
684        mutation: AttributeMutation,
685    ) {
686        self.super_type()
687            .unwrap()
688            .attribute_mutated(cx, attr, mutation);
689        match attr.local_name() {
690            &local_name!("width") | &local_name!("height") => {
691                self.recreate_contexts_after_resize();
692                self.upcast::<Node>().dirty(NodeDamage::Other);
693            },
694            _ => {},
695        };
696    }
697
698    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
699        match attr.local_name() {
700            &local_name!("width") | &local_name!("height") => true,
701            _ => self
702                .super_type()
703                .unwrap()
704                .attribute_affects_presentational_hints(attr),
705        }
706    }
707
708    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
709        match *name {
710            local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
711            local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
712            _ => self
713                .super_type()
714                .unwrap()
715                .parse_plain_attribute(name, value),
716        }
717    }
718}
719
720impl Convert<GLContextAttributes> for WebGLContextAttributes {
721    fn convert(self) -> GLContextAttributes {
722        GLContextAttributes {
723            alpha: self.alpha,
724            depth: self.depth,
725            stencil: self.stencil,
726            antialias: self.antialias,
727            premultiplied_alpha: self.premultipliedAlpha,
728            preserve_drawing_buffer: self.preserveDrawingBuffer,
729        }
730    }
731}