1use std::cell::{Cell, RefCell};
6use std::collections::HashMap;
7use std::rc::Rc;
8
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 script_bindings::weakref::WeakRef;
23use servo_media::streams::MediaStreamType;
24use servo_media::streams::registry::MediaStreamId;
25use style::attr::AttrValue;
26
27pub(crate) use crate::canvas_context::*;
28use crate::conversions::Convert;
29use crate::dom::attr::Attr;
30use crate::dom::bindings::callback::ExceptionHandling;
31use crate::dom::bindings::cell::{DomRefCell, Ref};
32use crate::dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::{
33 BlobCallback, HTMLCanvasElementMethods, RenderingContext as RootedRenderingContext,
34};
35use crate::dom::bindings::codegen::Bindings::MediaStreamBinding::MediaStreamMethods;
36use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
37use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
38use crate::dom::bindings::conversions::ConversionResult;
39use crate::dom::bindings::error::{Error, Fallible};
40use crate::dom::bindings::inheritance::Castable;
41use crate::dom::bindings::num::Finite;
42use crate::dom::bindings::refcounted::Trusted;
43use crate::dom::bindings::reflector::{DomGlobal, DomObject};
44use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, ToLayout};
45use crate::dom::bindings::str::{DOMString, USVString};
46use crate::dom::blob::Blob;
47use crate::dom::canvasrenderingcontext2d::CanvasRenderingContext2D;
48use crate::dom::document::Document;
49use crate::dom::element::{AttributeMutation, Element, LayoutElementHelpers};
50#[cfg(not(feature = "webgpu"))]
51use crate::dom::gpucanvascontext::GPUCanvasContext;
52use crate::dom::html::htmlelement::HTMLElement;
53use crate::dom::imagebitmaprenderingcontext::ImageBitmapRenderingContext;
54use crate::dom::mediastream::MediaStream;
55use crate::dom::mediastreamtrack::MediaStreamTrack;
56use crate::dom::node::{Node, NodeDamage, NodeTraits};
57use crate::dom::offscreencanvas::OffscreenCanvas;
58use crate::dom::values::UNSIGNED_LONG_MAX;
59use crate::dom::virtualmethods::VirtualMethods;
60use crate::dom::webgl::webgl2renderingcontext::WebGL2RenderingContext;
61use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
62#[cfg(feature = "webgpu")]
63use crate::dom::webgpu::gpucanvascontext::GPUCanvasContext;
64use crate::script_runtime::{CanGc, JSContext};
65
66const DEFAULT_WIDTH: u32 = 300;
67const DEFAULT_HEIGHT: u32 = 150;
68
69#[dom_struct]
71pub(crate) struct HTMLCanvasElement {
72 htmlelement: HTMLElement,
73
74 context_mode: DomRefCell<Option<RenderingContext>>,
76
77 callback_id: Cell<u32>,
79 #[ignore_malloc_size_of = "not implemented for webidl callbacks"]
80 blob_callbacks: RefCell<HashMap<u32, Rc<BlobCallback>>>,
81}
82
83impl HTMLCanvasElement {
84 fn new_inherited(
85 local_name: LocalName,
86 prefix: Option<Prefix>,
87 document: &Document,
88 ) -> HTMLCanvasElement {
89 HTMLCanvasElement {
90 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
91 context_mode: DomRefCell::new(None),
92 callback_id: Cell::new(0),
93 blob_callbacks: RefCell::new(HashMap::new()),
94 }
95 }
96
97 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
98 pub(crate) fn new(
99 local_name: LocalName,
100 prefix: Option<Prefix>,
101 document: &Document,
102 proto: Option<HandleObject>,
103 can_gc: CanGc,
104 ) -> DomRoot<HTMLCanvasElement> {
105 Node::reflect_node_with_proto(
106 Box::new(HTMLCanvasElement::new_inherited(
107 local_name, prefix, document,
108 )),
109 document,
110 proto,
111 can_gc,
112 )
113 }
114
115 fn recreate_contexts_after_resize(&self) {
116 if let Some(ref context) = *self.context_mode.borrow() {
117 context.resize()
118 }
119 }
120
121 pub(crate) fn get_size(&self) -> Size2D<u32> {
122 Size2D::new(self.Width(), self.Height())
123 }
124
125 pub(crate) fn origin_is_clean(&self) -> bool {
126 match *self.context_mode.borrow() {
127 Some(ref context) => context.origin_is_clean(),
128 _ => true,
129 }
130 }
131
132 pub(crate) fn mark_as_dirty(&self) {
133 if let Some(ref context) = *self.context_mode.borrow() {
134 context.mark_as_dirty()
135 }
136 }
137
138 pub(crate) fn set_natural_width(&self, value: u32, can_gc: CanGc) {
139 let value = if value > UNSIGNED_LONG_MAX {
140 DEFAULT_WIDTH
141 } else {
142 value
143 };
144 let element = self.upcast::<Element>();
145 element.set_uint_attribute(&html5ever::local_name!("width"), value, can_gc);
146 }
147
148 pub(crate) fn set_natural_height(&self, value: u32, can_gc: CanGc) {
149 let value = if value > UNSIGNED_LONG_MAX {
150 DEFAULT_HEIGHT
151 } else {
152 value
153 };
154 let element = self.upcast::<Element>();
155 element.set_uint_attribute(&html5ever::local_name!("height"), value, can_gc);
156 }
157}
158
159impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> {
160 #[allow(unsafe_code)]
161 fn data(self) -> HTMLCanvasData {
162 let source = unsafe {
163 match self.unsafe_get().context_mode.borrow_for_layout().as_ref() {
164 Some(RenderingContext::Context2d(context)) => {
165 context.to_layout().canvas_data_source()
166 },
167 Some(RenderingContext::BitmapRenderer(context)) => {
168 context.to_layout().canvas_data_source()
169 },
170 Some(RenderingContext::WebGL(context)) => context.to_layout().canvas_data_source(),
171 Some(RenderingContext::WebGL2(context)) => context.to_layout().canvas_data_source(),
172 #[cfg(feature = "webgpu")]
173 Some(RenderingContext::WebGPU(context)) => context.to_layout().canvas_data_source(),
174 Some(RenderingContext::Placeholder(_)) | None => None,
175 }
176 };
177
178 let width_attr = self
179 .upcast::<Element>()
180 .get_attr_for_layout(&ns!(), &local_name!("width"));
181 let height_attr = self
182 .upcast::<Element>()
183 .get_attr_for_layout(&ns!(), &local_name!("height"));
184 HTMLCanvasData {
185 source,
186 width: width_attr.map_or(DEFAULT_WIDTH, |val| val.as_uint()),
187 height: height_attr.map_or(DEFAULT_HEIGHT, |val| val.as_uint()),
188 }
189 }
190}
191
192impl HTMLCanvasElement {
193 pub(crate) fn context(&self) -> Option<Ref<'_, RenderingContext>> {
194 Ref::filter_map(self.context_mode.borrow(), |ctx| ctx.as_ref()).ok()
195 }
196
197 fn get_or_init_2d_context(&self, can_gc: CanGc) -> Option<DomRoot<CanvasRenderingContext2D>> {
198 if let Some(ctx) = self.context() {
199 return match *ctx {
200 RenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
201 _ => None,
202 };
203 }
204
205 let window = self.owner_window();
206 let size = self.get_size();
207 let context = CanvasRenderingContext2D::new(window.as_global_scope(), self, size, can_gc)?;
208 *self.context_mode.borrow_mut() =
209 Some(RenderingContext::Context2d(Dom::from_ref(&*context)));
210 Some(context)
211 }
212
213 fn get_or_init_bitmaprenderer_context(
215 &self,
216 can_gc: CanGc,
217 ) -> Option<DomRoot<ImageBitmapRenderingContext>> {
218 if let Some(ctx) = self.context() {
221 return match *ctx {
222 RenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
223 _ => None,
224 };
225 }
226
227 let context = ImageBitmapRenderingContext::new(
231 &self.owner_global(),
232 HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self)),
233 can_gc,
234 );
235
236 *self.context_mode.borrow_mut() =
238 Some(RenderingContext::BitmapRenderer(Dom::from_ref(&*context)));
239
240 Some(context)
242 }
243
244 fn get_or_init_webgl_context(
245 &self,
246 cx: JSContext,
247 options: HandleValue,
248 can_gc: CanGc,
249 ) -> Option<DomRoot<WebGLRenderingContext>> {
250 if let Some(ctx) = self.context() {
251 return match *ctx {
252 RenderingContext::WebGL(ref ctx) => Some(DomRoot::from_ref(ctx)),
253 _ => None,
254 };
255 }
256 let window = self.owner_window();
257 let size = self.get_size();
258 let attrs = Self::get_gl_attributes(cx, options)?;
259 let canvas = HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
260 let context = WebGLRenderingContext::new(
261 &window,
262 &canvas,
263 WebGLVersion::WebGL1,
264 size,
265 attrs,
266 can_gc,
267 )?;
268 *self.context_mode.borrow_mut() = Some(RenderingContext::WebGL(Dom::from_ref(&*context)));
269 Some(context)
270 }
271
272 fn get_or_init_webgl2_context(
273 &self,
274 cx: JSContext,
275 options: HandleValue,
276 can_gc: CanGc,
277 ) -> Option<DomRoot<WebGL2RenderingContext>> {
278 if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
279 {
280 return None;
281 }
282 if let Some(ctx) = self.context() {
283 return match *ctx {
284 RenderingContext::WebGL2(ref ctx) => Some(DomRoot::from_ref(ctx)),
285 _ => None,
286 };
287 }
288 let window = self.owner_window();
289 let size = self.get_size();
290 let attrs = Self::get_gl_attributes(cx, options)?;
291 let canvas = HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
292 let context = WebGL2RenderingContext::new(&window, &canvas, size, attrs, can_gc)?;
293 *self.context_mode.borrow_mut() = Some(RenderingContext::WebGL2(Dom::from_ref(&*context)));
294 Some(context)
295 }
296
297 #[cfg(not(feature = "webgpu"))]
298 fn get_or_init_webgpu_context(&self) -> Option<DomRoot<GPUCanvasContext>> {
299 None
300 }
301
302 #[cfg(feature = "webgpu")]
303 fn get_or_init_webgpu_context(&self, can_gc: CanGc) -> Option<DomRoot<GPUCanvasContext>> {
304 if let Some(ctx) = self.context() {
305 return match *ctx {
306 RenderingContext::WebGPU(ref ctx) => Some(DomRoot::from_ref(ctx)),
307 _ => None,
308 };
309 }
310 let (sender, receiver) = ipcchan::channel().unwrap();
311 let global_scope = self.owner_global();
312 let _ = global_scope
313 .script_to_constellation_chan()
314 .send(ScriptToConstellationMessage::GetWebGPUChan(sender));
315 receiver
316 .recv()
317 .expect("Failed to get WebGPU channel")
318 .map(|channel| {
319 let context = GPUCanvasContext::new(&global_scope, self, channel, can_gc);
320 *self.context_mode.borrow_mut() =
321 Some(RenderingContext::WebGPU(Dom::from_ref(&*context)));
322 context
323 })
324 }
325
326 pub(crate) fn get_base_webgl_context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
328 match *self.context_mode.borrow() {
329 Some(RenderingContext::WebGL(ref context)) => Some(DomRoot::from_ref(context)),
330 Some(RenderingContext::WebGL2(ref context)) => Some(context.base_context()),
331 _ => None,
332 }
333 }
334
335 #[allow(unsafe_code)]
336 fn get_gl_attributes(cx: JSContext, options: HandleValue) -> Option<GLContextAttributes> {
337 unsafe {
338 match WebGLContextAttributes::new(cx, options) {
339 Ok(ConversionResult::Success(attrs)) => Some(attrs.convert()),
340 Ok(ConversionResult::Failure(error)) => {
341 throw_type_error(*cx, &error);
342 None
343 },
344 _ => {
345 debug!("Unexpected error on conversion of WebGLContextAttributes");
346 None
347 },
348 }
349 }
350 }
351
352 pub(crate) fn is_valid(&self) -> bool {
353 self.Height() != 0 && self.Width() != 0
354 }
355
356 pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
357 match self.context_mode.borrow().as_ref() {
358 Some(context) => context.get_image_data(),
359 None => {
360 let size = self.get_size();
361 if size.is_empty() ||
362 pixels::compute_rgba8_byte_length_if_within_limit(
363 size.width as usize,
364 size.height as usize,
365 )
366 .is_none()
367 {
368 None
369 } else {
370 Some(Snapshot::cleared(size.cast()))
371 }
372 },
373 }
374 }
375
376 fn maybe_quality(quality: HandleValue) -> Option<f64> {
377 if quality.is_number() {
378 Some(quality.to_number())
379 } else {
380 None
381 }
382 }
383}
384
385impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
386 make_uint_getter!(Width, "width", DEFAULT_WIDTH);
388
389 fn SetWidth(&self, value: u32, can_gc: CanGc) -> Fallible<()> {
391 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
395 return Err(Error::InvalidState);
396 }
397
398 let value = if value > UNSIGNED_LONG_MAX {
399 DEFAULT_WIDTH
400 } else {
401 value
402 };
403 let element = self.upcast::<Element>();
404 element.set_uint_attribute(&html5ever::local_name!("width"), value, can_gc);
405 Ok(())
406 }
407
408 make_uint_getter!(Height, "height", DEFAULT_HEIGHT);
410
411 fn SetHeight(&self, value: u32, can_gc: CanGc) -> Fallible<()> {
413 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
417 return Err(Error::InvalidState);
418 }
419
420 let value = if value > UNSIGNED_LONG_MAX {
421 DEFAULT_HEIGHT
422 } else {
423 value
424 };
425 let element = self.upcast::<Element>();
426 element.set_uint_attribute(&html5ever::local_name!("height"), value, can_gc);
427 Ok(())
428 }
429
430 fn GetContext(
432 &self,
433 cx: JSContext,
434 id: DOMString,
435 options: HandleValue,
436 can_gc: CanGc,
437 ) -> Fallible<Option<RootedRenderingContext>> {
438 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
440 return Err(Error::InvalidState);
441 }
442
443 Ok(match &*id {
444 "2d" => self
445 .get_or_init_2d_context(can_gc)
446 .map(RootedRenderingContext::CanvasRenderingContext2D),
447 "bitmaprenderer" => self
448 .get_or_init_bitmaprenderer_context(can_gc)
449 .map(RootedRenderingContext::ImageBitmapRenderingContext),
450 "webgl" | "experimental-webgl" => self
451 .get_or_init_webgl_context(cx, options, can_gc)
452 .map(RootedRenderingContext::WebGLRenderingContext),
453 "webgl2" | "experimental-webgl2" => self
454 .get_or_init_webgl2_context(cx, options, can_gc)
455 .map(RootedRenderingContext::WebGL2RenderingContext),
456 #[cfg(feature = "webgpu")]
457 "webgpu" => self
458 .get_or_init_webgpu_context(can_gc)
459 .map(RootedRenderingContext::GPUCanvasContext),
460 _ => None,
461 })
462 }
463
464 fn ToDataURL(
466 &self,
467 _context: JSContext,
468 mime_type: DOMString,
469 quality: HandleValue,
470 ) -> Fallible<USVString> {
471 if !self.origin_is_clean() {
474 return Err(Error::Security);
475 }
476
477 if self.Width() == 0 || self.Height() == 0 {
482 return Ok(USVString("data:,".into()));
483 }
484
485 let Some(mut snapshot) = self.get_image_data() else {
488 return Ok(USVString("data:,".into()));
489 };
490
491 let image_type = EncodedImageType::from(mime_type.to_string());
492
493 let mut url = format!("data:{};base64,", image_type.as_mime_type());
494
495 let mut encoder = base64::write::EncoderStringWriter::from_consumer(
496 &mut url,
497 &base64::engine::general_purpose::STANDARD,
498 );
499
500 if snapshot
501 .encode_for_mime_type(&image_type, Self::maybe_quality(quality), &mut encoder)
502 .is_err()
503 {
504 return Ok(USVString("data:,".into()));
506 }
507
508 encoder.into_inner();
510 Ok(USVString(url))
511 }
512
513 fn ToBlob(
515 &self,
516 _cx: JSContext,
517 callback: Rc<BlobCallback>,
518 mime_type: DOMString,
519 quality: HandleValue,
520 ) -> Fallible<()> {
521 if !self.origin_is_clean() {
525 return Err(Error::Security);
526 }
527
528 let result = if self.Width() == 0 || self.Height() == 0 {
533 None
534 } else {
535 self.get_image_data()
536 };
537
538 let this = Trusted::new(self);
539 let callback_id = self.callback_id.get().wrapping_add(1);
540 self.callback_id.set(callback_id);
541
542 self.blob_callbacks
543 .borrow_mut()
544 .insert(callback_id, callback);
545 let quality = Self::maybe_quality(quality);
546 let image_type = EncodedImageType::from(mime_type.to_string());
547
548 self.global()
549 .task_manager()
550 .canvas_blob_task_source()
551 .queue(task!(to_blob: move || {
552 let this = this.root();
553 let Some(callback) = &this.blob_callbacks.borrow_mut().remove(&callback_id) else {
554 return error!("Expected blob callback, but found none!");
555 };
556
557 let Some(mut snapshot) = result else {
558 let _ = callback.Call__(None, ExceptionHandling::Report, CanGc::note());
559 return;
560 };
561
562 let mut encoded: Vec<u8> = vec![];
567 let blob_impl;
568 let blob;
569 let result = match snapshot.encode_for_mime_type(&image_type, quality, &mut encoded) {
570 Ok(..) => {
571 blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
575 blob = Blob::new(&this.global(), blob_impl, CanGc::note());
576 Some(&*blob)
577 }
578 Err(..) => None,
579 };
580
581 let _ = callback.Call__(result, ExceptionHandling::Report, CanGc::note());
583 }));
584
585 Ok(())
586 }
587
588 fn TransferControlToOffscreen(&self, can_gc: CanGc) -> Fallible<DomRoot<OffscreenCanvas>> {
590 if self.context_mode.borrow().is_some() {
591 return Err(Error::InvalidState);
594 };
595
596 let offscreen_canvas = OffscreenCanvas::new(
602 &self.global(),
603 None,
604 self.Width().into(),
605 self.Height().into(),
606 Some(WeakRef::new(self)),
607 can_gc,
608 );
609
610 *self.context_mode.borrow_mut() =
612 Some(RenderingContext::Placeholder(offscreen_canvas.as_traced()));
613
614 Ok(offscreen_canvas)
616 }
617
618 fn CaptureStream(
620 &self,
621 _frame_request_rate: Option<Finite<f64>>,
622 can_gc: CanGc,
623 ) -> DomRoot<MediaStream> {
624 let global = self.global();
625 let stream = MediaStream::new(&global, can_gc);
626 let track = MediaStreamTrack::new(
627 &global,
628 MediaStreamId::new(),
629 MediaStreamType::Video,
630 can_gc,
631 );
632 stream.AddTrack(&track);
633 stream
634 }
635}
636
637impl VirtualMethods for HTMLCanvasElement {
638 fn super_type(&self) -> Option<&dyn VirtualMethods> {
639 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
640 }
641
642 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
643 self.super_type()
644 .unwrap()
645 .attribute_mutated(attr, mutation, can_gc);
646 match attr.local_name() {
647 &local_name!("width") | &local_name!("height") => {
648 self.recreate_contexts_after_resize();
649 self.upcast::<Node>().dirty(NodeDamage::Other);
650 },
651 _ => {},
652 };
653 }
654
655 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
656 match *name {
657 local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
658 local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
659 _ => self
660 .super_type()
661 .unwrap()
662 .parse_plain_attribute(name, value),
663 }
664 }
665}
666
667impl Convert<GLContextAttributes> for WebGLContextAttributes {
668 fn convert(self) -> GLContextAttributes {
669 GLContextAttributes {
670 alpha: self.alpha,
671 depth: self.depth,
672 stencil: self.stencil,
673 antialias: self.antialias,
674 premultiplied_alpha: self.premultipliedAlpha,
675 preserve_drawing_buffer: self.preserveDrawingBuffer,
676 }
677 }
678}