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