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