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