Skip to main content

script/dom/canvas/
offscreencanvas.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;
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use euclid::default::Size2D;
10#[cfg(feature = "webgl")]
11use js::error::throw_type_error;
12use js::realm::CurrentRealm;
13use js::rust::{HandleObject, HandleValue};
14use pixels::{EncodedImageType, Snapshot};
15use rustc_hash::FxHashMap;
16use script_bindings::cell::{DomRefCell, Ref};
17#[cfg(feature = "webgl")]
18use script_bindings::inheritance::Castable;
19#[cfg(feature = "webgl")]
20use script_bindings::reflector::DomObject;
21use script_bindings::reflector::reflect_dom_object_with_proto;
22use script_bindings::weakref::WeakRef;
23use servo_base::id::{OffscreenCanvasId, OffscreenCanvasIndex};
24#[cfg(feature = "webgl")]
25use servo_canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
26use servo_constellation_traits::{BlobImpl, TransferableOffscreenCanvas};
27
28use crate::canvas_context::{CanvasContext, OffscreenRenderingContext};
29#[cfg(feature = "webgl")]
30use crate::conversions::Convert;
31use crate::dom::bindings::codegen::Bindings::OffscreenCanvasBinding::{
32    ImageEncodeOptions, OffscreenCanvasMethods,
33    OffscreenRenderingContext as RootedOffscreenRenderingContext, OffscreenRenderingContextId,
34};
35#[cfg(feature = "webgl")]
36use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
37use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas;
38#[cfg(feature = "webgl")]
39use crate::dom::bindings::conversions::ConversionResult;
40use crate::dom::bindings::error::{Error, Fallible};
41use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
42use crate::dom::bindings::reflector::DomGlobal;
43use crate::dom::bindings::root::{Dom, DomRoot};
44use crate::dom::bindings::structuredclone::StructuredData;
45use crate::dom::bindings::transferable::Transferable;
46use crate::dom::blob::Blob;
47use crate::dom::eventtarget::EventTarget;
48use crate::dom::globalscope::GlobalScope;
49use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
50use crate::dom::imagebitmap::ImageBitmap;
51use crate::dom::imagebitmaprenderingcontext::ImageBitmapRenderingContext;
52use crate::dom::offscreencanvasrenderingcontext2d::OffscreenCanvasRenderingContext2D;
53use crate::dom::promise::Promise;
54#[cfg(feature = "webgl")]
55use crate::dom::types::{WebGLRenderingContext, Window};
56#[cfg(feature = "webgl")]
57use crate::dom::webgl::webgl2renderingcontext::WebGL2RenderingContext;
58
59/// <https://html.spec.whatwg.org/multipage/#offscreencanvas>
60#[dom_struct]
61pub(crate) struct OffscreenCanvas {
62    eventtarget: EventTarget,
63    width: Cell<u64>,
64    height: Cell<u64>,
65
66    /// Represents both the [bitmap] and the [context mode] of the canvas.
67    ///
68    /// [bitmap]: https://html.spec.whatwg.org/multipage/#offscreencanvas-bitmap
69    /// [context mode]: https://html.spec.whatwg.org/multipage/#offscreencanvas-context-mode
70    context: DomRefCell<Option<OffscreenRenderingContext>>,
71
72    /// <https://html.spec.whatwg.org/multipage/#offscreencanvas-placeholder>
73    placeholder: Option<WeakRef<HTMLCanvasElement>>,
74}
75
76impl OffscreenCanvas {
77    pub(crate) fn new_inherited(
78        width: u64,
79        height: u64,
80        placeholder: Option<WeakRef<HTMLCanvasElement>>,
81    ) -> OffscreenCanvas {
82        OffscreenCanvas {
83            eventtarget: EventTarget::new_inherited(),
84            width: Cell::new(width),
85            height: Cell::new(height),
86            context: DomRefCell::new(None),
87            placeholder,
88        }
89    }
90
91    pub(crate) fn new(
92        cx: &mut js::context::JSContext,
93        global: &GlobalScope,
94        proto: Option<HandleObject>,
95        width: u64,
96        height: u64,
97        placeholder: Option<WeakRef<HTMLCanvasElement>>,
98    ) -> DomRoot<OffscreenCanvas> {
99        reflect_dom_object_with_proto(
100            cx,
101            Box::new(OffscreenCanvas::new_inherited(width, height, placeholder)),
102            global,
103            proto,
104        )
105    }
106
107    pub(crate) fn get_size(&self) -> Size2D<u32> {
108        Size2D::new(
109            self.Width().try_into().unwrap_or(u32::MAX),
110            self.Height().try_into().unwrap_or(u32::MAX),
111        )
112    }
113
114    #[cfg(feature = "webgl")]
115    fn get_gl_attributes(
116        cx: &mut js::context::JSContext,
117        options: HandleValue,
118    ) -> Option<GLContextAttributes> {
119        match WebGLContextAttributes::new(cx, options) {
120            Ok(ConversionResult::Success(attrs)) => Some(attrs.convert()),
121            Ok(ConversionResult::Failure(error)) => {
122                throw_type_error(cx, &error);
123                None
124            },
125            _ => {
126                debug!("Unexpected error on conversion of WebGLContextAttributes");
127                None
128            },
129        }
130    }
131
132    pub(crate) fn origin_is_clean(&self) -> bool {
133        match *self.context.borrow() {
134            Some(ref context) => context.origin_is_clean(),
135            _ => true,
136        }
137    }
138
139    pub(crate) fn context(&self) -> Option<Ref<'_, OffscreenRenderingContext>> {
140        Ref::filter_map(self.context.borrow(), |ctx| ctx.as_ref()).ok()
141    }
142
143    pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
144        match self.context.borrow().as_ref() {
145            Some(context) => context.get_image_data(),
146            None => {
147                let size = self.get_size();
148                if size.is_empty() ||
149                    pixels::compute_rgba8_byte_length_if_within_limit(
150                        size.width as usize,
151                        size.height as usize,
152                    )
153                    .is_none()
154                {
155                    None
156                } else {
157                    Some(Snapshot::cleared(size))
158                }
159            },
160        }
161    }
162
163    pub(crate) fn get_or_init_2d_context(
164        &self,
165        cx: &mut js::context::JSContext,
166    ) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
167        if let Some(ctx) = self.context() {
168            return match *ctx {
169                OffscreenRenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
170                _ => None,
171            };
172        }
173        let context =
174            OffscreenCanvasRenderingContext2D::new(cx, &self.global(), self, self.get_size())?;
175        *self.context.safe_borrow_mut(cx.no_gc()) = Some(OffscreenRenderingContext::Context2d(
176            Dom::from_ref(&*context),
177        ));
178        Some(context)
179    }
180
181    /// <https://html.spec.whatwg.org/multipage/#offscreen-context-type-bitmaprenderer>
182    pub(crate) fn get_or_init_bitmaprenderer_context(
183        &self,
184        cx: &mut js::context::JSContext,
185    ) -> Option<DomRoot<ImageBitmapRenderingContext>> {
186        // Return the same object as was returned the last time the method was
187        // invoked with this same first argument.
188        if let Some(ctx) = self.context() {
189            return match *ctx {
190                OffscreenRenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
191                _ => None,
192            };
193        }
194
195        // Step 1. Let context be the result of running the
196        // ImageBitmapRenderingContext creation algorithm given this and
197        // options.
198        let canvas =
199            RootedHTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(DomRoot::from_ref(self));
200
201        let context = ImageBitmapRenderingContext::new(cx, &self.global(), &canvas);
202
203        // Step 2. Set this's context mode to bitmaprenderer.
204        *self.context.safe_borrow_mut(cx.no_gc()) = Some(
205            OffscreenRenderingContext::BitmapRenderer(Dom::from_ref(&*context)),
206        );
207
208        // Step 3. Return context.
209        Some(context)
210    }
211
212    #[cfg(feature = "webgl")]
213    // <https://html.spec.whatwg.org/multipage/#offscreen-context-type-webgl>
214    pub(crate) fn get_or_init_webgl_context(
215        &self,
216        cx: &mut js::context::JSContext,
217        options: HandleValue,
218    ) -> Option<DomRoot<WebGLRenderingContext>> {
219        if let Some(ctx) = self.context() {
220            return match *ctx {
221                OffscreenRenderingContext::WebGL(ref ctx) => Some(DomRoot::from_ref(ctx)),
222                _ => None,
223            };
224        }
225
226        // 1. Let context be the result of following the instructions given in the
227        // WebGL specifications' Context Creation sections.
228        let canvas =
229            RootedHTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(DomRoot::from_ref(self));
230        let size = self.get_size();
231        let attrs = Self::get_gl_attributes(cx, options)?;
232        self.global()
233            .downcast::<Window>()
234            .and_then(|window| {
235                WebGLRenderingContext::new(cx, window, &canvas, WebGLVersion::WebGL1, size, attrs)
236            })
237            .map(|context| {
238                // Step 2. If context is null, then return null;
239                // otherwise set this's context mode to webgl or webgl2.
240                *self.context.safe_borrow_mut(cx.no_gc()) =
241                    Some(OffscreenRenderingContext::WebGL(Dom::from_ref(&*context)));
242
243                // Step 3. Return context.
244                context
245            })
246    }
247
248    #[cfg(feature = "webgl")]
249    // <https://html.spec.whatwg.org/multipage/#offscreen-context-type-webgl>
250    fn get_or_init_webgl2_context(
251        &self,
252        cx: &mut js::context::JSContext,
253        options: HandleValue,
254    ) -> Option<DomRoot<WebGL2RenderingContext>> {
255        if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
256        {
257            return None;
258        }
259        if let Some(ctx) = self.context() {
260            return match *ctx {
261                OffscreenRenderingContext::WebGL2(ref ctx) => Some(DomRoot::from_ref(ctx)),
262                _ => None,
263            };
264        }
265
266        // 1. Let context be the result of following the instructions given in the
267        // WebGL specifications' Context Creation sections.
268        let canvas =
269            RootedHTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(DomRoot::from_ref(self));
270        let size = self.get_size();
271        let attrs = Self::get_gl_attributes(cx, options)?;
272        self.global()
273            .downcast::<Window>()
274            .and_then(|window| WebGL2RenderingContext::new(cx, window, &canvas, size, attrs))
275            .map(|context| {
276                // Step 2. If context is null, then return null;
277                // otherwise set this's context mode to webgl or webgl2.
278                *self.context.safe_borrow_mut(cx.no_gc()) =
279                    Some(OffscreenRenderingContext::WebGL2(Dom::from_ref(&*context)));
280
281                // Step 3. Return context.
282                context
283            })
284    }
285
286    pub(crate) fn placeholder(&self) -> Option<DomRoot<HTMLCanvasElement>> {
287        self.placeholder
288            .as_ref()
289            .and_then(|placeholder| placeholder.root())
290    }
291}
292
293impl Transferable for OffscreenCanvas {
294    type Index = OffscreenCanvasIndex;
295    type Data = TransferableOffscreenCanvas;
296
297    /// <https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transfer-steps>
298    fn transfer(
299        &self,
300        cx: &mut js::context::JSContext,
301    ) -> Fallible<(OffscreenCanvasId, TransferableOffscreenCanvas)> {
302        // <https://html.spec.whatwg.org/multipage/#structuredserializewithtransfer>
303        // Step 5.2. If transferable has a [[Detached]] internal slot and
304        // transferable.[[Detached]] is true, then throw a "DataCloneError"
305        // DOMException.
306        if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
307            return Err(Error::DataClone(None));
308        }
309
310        // Step 1. If value's context mode is not equal to none, then throw an
311        // "InvalidStateError" DOMException.
312        if !self.context.borrow().is_none() {
313            return Err(Error::InvalidState(None));
314        }
315
316        // TODO(#37882): Allow to transfer with a placeholder canvas element.
317        if self.placeholder.is_some() {
318            return Err(Error::InvalidState(None));
319        }
320
321        // Step 2. Set value's context mode to detached.
322        *self.context.safe_borrow_mut(cx.no_gc()) = Some(OffscreenRenderingContext::Detached);
323
324        // Step 3. Let width and height be the dimensions of value's bitmap.
325        // Step 5. Unset value's bitmap.
326        let width = self.width.replace(0);
327        let height = self.height.replace(0);
328
329        // TODO(#37918) Step 4. Let language and direction be the values of
330        // value's inherited language and inherited direction.
331
332        // Step 6. Set dataHolder.[[Width]] to width and dataHolder.[[Height]]
333        // to height.
334
335        // TODO(#37918) Step 7. Set dataHolder.[[Language]] to language and
336        // dataHolder.[[Direction]] to direction.
337
338        // TODO(#37882) Step 8. Set dataHolder.[[PlaceholderCanvas]] to be a
339        // weak reference to value's placeholder canvas element, if value has
340        // one, or null if it does not.
341        let transferred = TransferableOffscreenCanvas { width, height };
342
343        Ok((OffscreenCanvasId::new(), transferred))
344    }
345
346    /// <https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transfer-receiving-steps>
347    fn transfer_receive(
348        cx: &mut js::context::JSContext,
349        owner: &GlobalScope,
350        _: OffscreenCanvasId,
351        transferred: TransferableOffscreenCanvas,
352    ) -> Result<DomRoot<Self>, ()> {
353        // Step 1. Initialize value's bitmap to a rectangular array of
354        // transparent black pixels with width given by dataHolder.[[Width]] and
355        // height given by dataHolder.[[Height]].
356
357        // TODO(#37918) Step 2. Set value's inherited language to
358        // dataHolder.[[Language]] and its inherited direction to
359        // dataHolder.[[Direction]].
360
361        // TODO(#37882) Step 3. If dataHolder.[[PlaceholderCanvas]] is not null,
362        // set value's placeholder canvas element to
363        // dataHolder.[[PlaceholderCanvas]] (while maintaining the weak
364        // reference semantics).
365        Ok(OffscreenCanvas::new(
366            cx,
367            owner,
368            None,
369            transferred.width,
370            transferred.height,
371            None,
372        ))
373    }
374
375    fn serialized_storage<'a>(
376        data: StructuredData<'a, '_>,
377    ) -> &'a mut Option<FxHashMap<OffscreenCanvasId, Self::Data>> {
378        match data {
379            StructuredData::Reader(r) => &mut r.offscreen_canvases,
380            StructuredData::Writer(w) => &mut w.offscreen_canvases,
381        }
382    }
383}
384
385impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
386    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas>
387    fn Constructor(
388        cx: &mut js::context::JSContext,
389        global: &GlobalScope,
390        proto: Option<HandleObject>,
391        width: u64,
392        height: u64,
393    ) -> Fallible<DomRoot<OffscreenCanvas>> {
394        Ok(OffscreenCanvas::new(cx, global, proto, width, height, None))
395    }
396
397    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-getcontext>
398    fn GetContext(
399        &self,
400        cx: &mut js::context::JSContext,
401        id: OffscreenRenderingContextId,
402        options: HandleValue,
403    ) -> Fallible<Option<RootedOffscreenRenderingContext>> {
404        // Step 3. Throw an "InvalidStateError" DOMException if the
405        // OffscreenCanvas object's context mode is detached.
406        if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
407            return Err(Error::InvalidState(None));
408        }
409
410        match id {
411            OffscreenRenderingContextId::_2d => Ok(self
412                .get_or_init_2d_context(cx)
413                .map(RootedOffscreenRenderingContext::OffscreenCanvasRenderingContext2D)),
414            OffscreenRenderingContextId::Bitmaprenderer => Ok(self
415                .get_or_init_bitmaprenderer_context(cx)
416                .map(RootedOffscreenRenderingContext::ImageBitmapRenderingContext)),
417            #[cfg(feature = "webgl")]
418            OffscreenRenderingContextId::Webgl => Ok(self
419                .get_or_init_webgl_context(cx, options)
420                .map(RootedOffscreenRenderingContext::WebGLRenderingContext)),
421            #[cfg(feature = "webgl")]
422            OffscreenRenderingContextId::Experimental_webgl => Ok(self
423                .get_or_init_webgl_context(cx, options)
424                .map(RootedOffscreenRenderingContext::WebGLRenderingContext)),
425            #[cfg(feature = "webgl")]
426            OffscreenRenderingContextId::Webgl2 => Ok(self
427                .get_or_init_webgl2_context(cx, options)
428                .map(RootedOffscreenRenderingContext::WebGL2RenderingContext)),
429            #[cfg(feature = "webgl")]
430            OffscreenRenderingContextId::Experimental_webgl2 => Ok(self
431                .get_or_init_webgl2_context(cx, options)
432                .map(RootedOffscreenRenderingContext::WebGL2RenderingContext)),
433        }
434    }
435
436    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-width>
437    fn Width(&self) -> u64 {
438        self.width.get()
439    }
440
441    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-width>
442    fn SetWidth(&self, cx: &mut js::context::JSContext, value: u64) {
443        self.width.set(value);
444
445        if let Some(canvas_context) = self.context() {
446            canvas_context.resize();
447        }
448
449        if let Some(canvas) = self.placeholder() {
450            canvas.set_natural_width(cx, value as _)
451        }
452    }
453
454    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height>
455    fn Height(&self) -> u64 {
456        self.height.get()
457    }
458
459    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height>
460    fn SetHeight(&self, cx: &mut js::context::JSContext, value: u64) {
461        self.height.set(value);
462
463        if let Some(canvas_context) = self.context() {
464            canvas_context.resize();
465        }
466
467        if let Some(canvas) = self.placeholder() {
468            canvas.set_natural_height(cx, value as _)
469        }
470    }
471
472    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-transfertoimagebitmap>
473    fn TransferToImageBitmap(
474        &self,
475        cx: &mut js::context::JSContext,
476    ) -> Fallible<DomRoot<ImageBitmap>> {
477        // Step 1. If the value of this OffscreenCanvas object's [[Detached]]
478        // internal slot is set to true, then throw an "InvalidStateError"
479        // DOMException.
480        if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
481            return Err(Error::InvalidState(None));
482        }
483
484        // Step 2. If this OffscreenCanvas object's context mode is set to none,
485        // then throw an "InvalidStateError" DOMException.
486        if self.context.borrow().is_none() {
487            return Err(Error::InvalidState(None));
488        }
489
490        // Step 3. Let image be a newly created ImageBitmap object that
491        // references the same underlying bitmap data as this OffscreenCanvas
492        // object's bitmap.
493        let Some(snapshot) = self.get_image_data() else {
494            return Err(Error::InvalidState(None));
495        };
496
497        let image_bitmap = ImageBitmap::new(cx, &self.global(), snapshot);
498        image_bitmap.set_origin_clean(self.origin_is_clean());
499
500        // Step 4. Set this OffscreenCanvas object's bitmap to reference a newly
501        // created bitmap of the same dimensions and color space as the previous
502        // bitmap, and with its pixels initialized to transparent black, or
503        // opaque black if the rendering context's alpha is false.
504        if let Some(canvas_context) = self.context() {
505            canvas_context.reset_bitmap();
506        }
507
508        // Step 5. Return image.
509        Ok(image_bitmap)
510    }
511
512    /// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-converttoblob>
513    fn ConvertToBlob(
514        &self,
515        cx: &mut js::context::JSContext,
516        options: &ImageEncodeOptions,
517    ) -> Rc<Promise> {
518        // Step 5. Let result be a new promise object.
519        let mut realm = CurrentRealm::assert(cx);
520        let promise = Promise::new_in_realm(&mut realm);
521
522        // Step 1. If the value of this's [[Detached]] internal slot is true,
523        // then return a promise rejected with an "InvalidStateError"
524        // DOMException.
525        if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
526            promise.reject_error(cx, Error::InvalidState(None));
527            return promise;
528        }
529
530        // Step 2. If this's context mode is 2d and the rendering context's
531        // output bitmap's origin-clean flag is set to false, then return a
532        // promise rejected with a "SecurityError" DOMException.
533        if !self.origin_is_clean() {
534            promise.reject_error(cx, Error::Security(None));
535            return promise;
536        }
537
538        // Step 3. If this's bitmap has no pixels (i.e., either its horizontal
539        // dimension or its vertical dimension is zero), then return a promise
540        // rejected with an "IndexSizeError" DOMException.
541        if self.Width() == 0 || self.Height() == 0 {
542            promise.reject_error(cx, Error::IndexSize(None));
543            return promise;
544        }
545
546        // Step 4. Let bitmap be a copy of this's bitmap.
547        let Some(mut snapshot) = self.get_image_data() else {
548            promise.reject_error(cx, Error::InvalidState(None));
549            return promise;
550        };
551
552        // Step 7. Run these steps in parallel:
553        // Step 7.1. Let file be a serialization of bitmap as a file, with
554        // options's type and quality if present.
555        // Step 7.2. Queue a global task on the canvas blob serialization task
556        // source given global to run these steps:
557        let trusted_this = Trusted::new(self);
558        let trusted_promise = TrustedPromise::new(promise.clone());
559
560        let image_type = EncodedImageType::from(&options.type_.str() as &str);
561        let quality = options.quality;
562
563        self.global()
564            .task_manager()
565            .canvas_blob_task_source()
566            .queue(task!(convert_to_blob: move |cx| {
567                let this = trusted_this.root();
568                let promise = trusted_promise.root();
569
570                let mut encoded: Vec<u8> = vec![];
571
572                if snapshot.encode_for_mime_type(&image_type, quality, &mut encoded).is_err() {
573                    // Step 7.2.1. If file is null, then reject result with an
574                    // "EncodingError" DOMException.
575                    promise.reject_error(cx, Error::Encoding(None));
576                    return;
577                };
578
579                // Step 7.2.2. Otherwise, resolve result with a new Blob object,
580                // created in global's relevant realm, representing file.
581                let blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type().to_owned());
582                let blob = Blob::new(cx, &this.global(), blob_impl);
583
584                promise.resolve_native(cx, &blob);
585            }));
586
587        // Step 8. Return result.
588        promise
589    }
590}