1use std::cell::{Cell, RefCell};
6use std::rc::Rc;
7
8use canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
9use constellation_traits::BlobImpl;
10#[cfg(feature = "webgpu")]
11use constellation_traits::ScriptToConstellationMessage;
12use dom_struct::dom_struct;
13use euclid::default::Size2D;
14use html5ever::{LocalName, Prefix, local_name, ns};
15#[cfg(feature = "webgpu")]
16use ipc_channel::ipc::{self as ipcchan};
17use js::error::throw_type_error;
18use js::rust::{HandleObject, HandleValue};
19use layout_api::HTMLCanvasData;
20use pixels::{EncodedImageType, Snapshot};
21use rustc_hash::FxHashMap;
22use script_bindings::weakref::WeakRef;
23use servo_media::streams::MediaStreamType;
24use servo_media::streams::registry::MediaStreamId;
25use style::attr::AttrValue;
26
27use crate::canvas_context::{CanvasContext, LayoutCanvasRenderingContextHelpers, RenderingContext};
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 as RootedHTMLCanvasElementOrOffscreenCanvas;
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<FxHashMap<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(FxHashMap::default()),
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
159pub(crate) trait LayoutHTMLCanvasElementHelpers {
160 fn data(self) -> HTMLCanvasData;
161}
162
163impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> {
164 #[allow(unsafe_code)]
165 fn data(self) -> HTMLCanvasData {
166 let source = unsafe {
167 match self.unsafe_get().context_mode.borrow_for_layout().as_ref() {
168 Some(RenderingContext::Context2d(context)) => {
169 context.to_layout().canvas_data_source()
170 },
171 Some(RenderingContext::BitmapRenderer(context)) => {
172 context.to_layout().canvas_data_source()
173 },
174 Some(RenderingContext::WebGL(context)) => context.to_layout().canvas_data_source(),
175 Some(RenderingContext::WebGL2(context)) => context.to_layout().canvas_data_source(),
176 #[cfg(feature = "webgpu")]
177 Some(RenderingContext::WebGPU(context)) => context.to_layout().canvas_data_source(),
178 Some(RenderingContext::Placeholder(_)) | None => None,
179 }
180 };
181
182 let width_attr = self
183 .upcast::<Element>()
184 .get_attr_for_layout(&ns!(), &local_name!("width"));
185 let height_attr = self
186 .upcast::<Element>()
187 .get_attr_for_layout(&ns!(), &local_name!("height"));
188 HTMLCanvasData {
189 source,
190 width: width_attr.map_or(DEFAULT_WIDTH, |val| val.as_uint()),
191 height: height_attr.map_or(DEFAULT_HEIGHT, |val| val.as_uint()),
192 }
193 }
194}
195
196impl HTMLCanvasElement {
197 pub(crate) fn context(&self) -> Option<Ref<'_, RenderingContext>> {
198 Ref::filter_map(self.context_mode.borrow(), |ctx| ctx.as_ref()).ok()
199 }
200
201 fn get_or_init_2d_context(&self, can_gc: CanGc) -> Option<DomRoot<CanvasRenderingContext2D>> {
202 if let Some(ctx) = self.context() {
203 return match *ctx {
204 RenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
205 _ => None,
206 };
207 }
208
209 let window = self.owner_window();
210 let size = self.get_size();
211 let context = CanvasRenderingContext2D::new(window.as_global_scope(), self, size, can_gc)?;
212 *self.context_mode.borrow_mut() =
213 Some(RenderingContext::Context2d(Dom::from_ref(&*context)));
214 Some(context)
215 }
216
217 fn get_or_init_bitmaprenderer_context(
219 &self,
220 can_gc: CanGc,
221 ) -> Option<DomRoot<ImageBitmapRenderingContext>> {
222 if let Some(ctx) = self.context() {
225 return match *ctx {
226 RenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
227 _ => None,
228 };
229 }
230
231 let canvas =
235 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
236
237 let context = ImageBitmapRenderingContext::new(&self.owner_global(), &canvas, can_gc);
238
239 *self.context_mode.borrow_mut() =
241 Some(RenderingContext::BitmapRenderer(Dom::from_ref(&*context)));
242
243 Some(context)
245 }
246
247 fn get_or_init_webgl_context(
248 &self,
249 cx: JSContext,
250 options: HandleValue,
251 can_gc: CanGc,
252 ) -> Option<DomRoot<WebGLRenderingContext>> {
253 if let Some(ctx) = self.context() {
254 return match *ctx {
255 RenderingContext::WebGL(ref ctx) => Some(DomRoot::from_ref(ctx)),
256 _ => None,
257 };
258 }
259 let window = self.owner_window();
260 let canvas =
261 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
262 let size = self.get_size();
263 let attrs = Self::get_gl_attributes(cx, options, can_gc)?;
264 let context = WebGLRenderingContext::new(
265 &window,
266 &canvas,
267 WebGLVersion::WebGL1,
268 size,
269 attrs,
270 can_gc,
271 )?;
272 *self.context_mode.borrow_mut() = Some(RenderingContext::WebGL(Dom::from_ref(&*context)));
273 Some(context)
274 }
275
276 fn get_or_init_webgl2_context(
277 &self,
278 cx: JSContext,
279 options: HandleValue,
280 can_gc: CanGc,
281 ) -> Option<DomRoot<WebGL2RenderingContext>> {
282 if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
283 {
284 return None;
285 }
286 if let Some(ctx) = self.context() {
287 return match *ctx {
288 RenderingContext::WebGL2(ref ctx) => Some(DomRoot::from_ref(ctx)),
289 _ => None,
290 };
291 }
292 let window = self.owner_window();
293 let canvas =
294 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
295 let size = self.get_size();
296 let attrs = Self::get_gl_attributes(cx, options, can_gc)?;
297 let context = WebGL2RenderingContext::new(&window, &canvas, size, attrs, can_gc)?;
298 *self.context_mode.borrow_mut() = Some(RenderingContext::WebGL2(Dom::from_ref(&*context)));
299 Some(context)
300 }
301
302 #[cfg(not(feature = "webgpu"))]
303 fn get_or_init_webgpu_context(&self) -> Option<DomRoot<GPUCanvasContext>> {
304 None
305 }
306
307 #[cfg(feature = "webgpu")]
308 fn get_or_init_webgpu_context(&self, can_gc: CanGc) -> Option<DomRoot<GPUCanvasContext>> {
309 if let Some(ctx) = self.context() {
310 return match *ctx {
311 RenderingContext::WebGPU(ref ctx) => Some(DomRoot::from_ref(ctx)),
312 _ => None,
313 };
314 }
315 let (sender, receiver) = ipcchan::channel().unwrap();
316 let global_scope = self.owner_global();
317 let _ = global_scope
318 .script_to_constellation_chan()
319 .send(ScriptToConstellationMessage::GetWebGPUChan(sender));
320 receiver
321 .recv()
322 .expect("Failed to get WebGPU channel")
323 .map(|channel| {
324 let context = GPUCanvasContext::new(&global_scope, self, channel, can_gc);
325 *self.context_mode.borrow_mut() =
326 Some(RenderingContext::WebGPU(Dom::from_ref(&*context)));
327 context
328 })
329 }
330
331 pub(crate) fn get_base_webgl_context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
333 match *self.context_mode.borrow() {
334 Some(RenderingContext::WebGL(ref context)) => Some(DomRoot::from_ref(context)),
335 Some(RenderingContext::WebGL2(ref context)) => Some(context.base_context()),
336 _ => None,
337 }
338 }
339
340 #[allow(unsafe_code)]
341 fn get_gl_attributes(
342 cx: JSContext,
343 options: HandleValue,
344 can_gc: CanGc,
345 ) -> Option<GLContextAttributes> {
346 unsafe {
347 match WebGLContextAttributes::new(cx, options, can_gc) {
348 Ok(ConversionResult::Success(attrs)) => Some(attrs.convert()),
349 Ok(ConversionResult::Failure(error)) => {
350 throw_type_error(*cx, &error);
351 None
352 },
353 _ => {
354 debug!("Unexpected error on conversion of WebGLContextAttributes");
355 None
356 },
357 }
358 }
359 }
360
361 pub(crate) fn is_valid(&self) -> bool {
362 self.Height() != 0 && self.Width() != 0
363 }
364
365 pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
366 match self.context_mode.borrow().as_ref() {
367 Some(context) => context.get_image_data(),
368 None => {
369 let size = self.get_size();
370 if size.is_empty() ||
371 pixels::compute_rgba8_byte_length_if_within_limit(
372 size.width as usize,
373 size.height as usize,
374 )
375 .is_none()
376 {
377 None
378 } else {
379 Some(Snapshot::cleared(size.cast()))
380 }
381 },
382 }
383 }
384
385 fn maybe_quality(quality: HandleValue) -> Option<f64> {
386 if quality.is_number() {
387 Some(quality.to_number())
388 } else {
389 None
390 }
391 }
392}
393
394impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
395 make_uint_getter!(Width, "width", DEFAULT_WIDTH);
397
398 fn SetWidth(&self, value: u32, can_gc: CanGc) -> Fallible<()> {
400 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
404 return Err(Error::InvalidState);
405 }
406
407 let value = if value > UNSIGNED_LONG_MAX {
408 DEFAULT_WIDTH
409 } else {
410 value
411 };
412 let element = self.upcast::<Element>();
413 element.set_uint_attribute(&html5ever::local_name!("width"), value, can_gc);
414 Ok(())
415 }
416
417 make_uint_getter!(Height, "height", DEFAULT_HEIGHT);
419
420 fn SetHeight(&self, value: u32, can_gc: CanGc) -> Fallible<()> {
422 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
426 return Err(Error::InvalidState);
427 }
428
429 let value = if value > UNSIGNED_LONG_MAX {
430 DEFAULT_HEIGHT
431 } else {
432 value
433 };
434 let element = self.upcast::<Element>();
435 element.set_uint_attribute(&html5ever::local_name!("height"), value, can_gc);
436 Ok(())
437 }
438
439 fn GetContext(
441 &self,
442 cx: JSContext,
443 id: DOMString,
444 options: HandleValue,
445 can_gc: CanGc,
446 ) -> Fallible<Option<RootedRenderingContext>> {
447 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
449 return Err(Error::InvalidState);
450 }
451
452 Ok(match &*id {
453 "2d" => self
454 .get_or_init_2d_context(can_gc)
455 .map(RootedRenderingContext::CanvasRenderingContext2D),
456 "bitmaprenderer" => self
457 .get_or_init_bitmaprenderer_context(can_gc)
458 .map(RootedRenderingContext::ImageBitmapRenderingContext),
459 "webgl" | "experimental-webgl" => self
460 .get_or_init_webgl_context(cx, options, can_gc)
461 .map(RootedRenderingContext::WebGLRenderingContext),
462 "webgl2" | "experimental-webgl2" => self
463 .get_or_init_webgl2_context(cx, options, can_gc)
464 .map(RootedRenderingContext::WebGL2RenderingContext),
465 #[cfg(feature = "webgpu")]
466 "webgpu" => self
467 .get_or_init_webgpu_context(can_gc)
468 .map(RootedRenderingContext::GPUCanvasContext),
469 _ => None,
470 })
471 }
472
473 fn ToDataURL(
475 &self,
476 _context: JSContext,
477 mime_type: DOMString,
478 quality: HandleValue,
479 ) -> Fallible<USVString> {
480 if !self.origin_is_clean() {
483 return Err(Error::Security);
484 }
485
486 if self.Width() == 0 || self.Height() == 0 {
491 return Ok(USVString("data:,".into()));
492 }
493
494 let Some(mut snapshot) = self.get_image_data() else {
497 return Ok(USVString("data:,".into()));
498 };
499
500 let image_type = EncodedImageType::from(mime_type.to_string());
501
502 let mut url = format!("data:{};base64,", image_type.as_mime_type());
503
504 let mut encoder = base64::write::EncoderStringWriter::from_consumer(
505 &mut url,
506 &base64::engine::general_purpose::STANDARD,
507 );
508
509 if snapshot
510 .encode_for_mime_type(&image_type, Self::maybe_quality(quality), &mut encoder)
511 .is_err()
512 {
513 return Ok(USVString("data:,".into()));
515 }
516
517 encoder.into_inner();
519 Ok(USVString(url))
520 }
521
522 fn ToBlob(
524 &self,
525 _cx: JSContext,
526 callback: Rc<BlobCallback>,
527 mime_type: DOMString,
528 quality: HandleValue,
529 ) -> Fallible<()> {
530 if !self.origin_is_clean() {
534 return Err(Error::Security);
535 }
536
537 let result = if self.Width() == 0 || self.Height() == 0 {
542 None
543 } else {
544 self.get_image_data()
545 };
546
547 let this = Trusted::new(self);
548 let callback_id = self.callback_id.get().wrapping_add(1);
549 self.callback_id.set(callback_id);
550
551 self.blob_callbacks
552 .borrow_mut()
553 .insert(callback_id, callback);
554 let quality = Self::maybe_quality(quality);
555 let image_type = EncodedImageType::from(mime_type.to_string());
556
557 self.global()
558 .task_manager()
559 .canvas_blob_task_source()
560 .queue(task!(to_blob: move || {
561 let this = this.root();
562 let Some(callback) = &this.blob_callbacks.borrow_mut().remove(&callback_id) else {
563 return error!("Expected blob callback, but found none!");
564 };
565
566 let Some(mut snapshot) = result else {
567 let _ = callback.Call__(None, ExceptionHandling::Report, CanGc::note());
568 return;
569 };
570
571 let mut encoded: Vec<u8> = vec![];
576 let blob_impl;
577 let blob;
578 let result = match snapshot.encode_for_mime_type(&image_type, quality, &mut encoded) {
579 Ok(..) => {
580 blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
584 blob = Blob::new(&this.global(), blob_impl, CanGc::note());
585 Some(&*blob)
586 }
587 Err(..) => None,
588 };
589
590 let _ = callback.Call__(result, ExceptionHandling::Report, CanGc::note());
592 }));
593
594 Ok(())
595 }
596
597 fn TransferControlToOffscreen(&self, can_gc: CanGc) -> Fallible<DomRoot<OffscreenCanvas>> {
599 if self.context_mode.borrow().is_some() {
600 return Err(Error::InvalidState);
603 };
604
605 let offscreen_canvas = OffscreenCanvas::new(
611 &self.global(),
612 None,
613 self.Width().into(),
614 self.Height().into(),
615 Some(WeakRef::new(self)),
616 can_gc,
617 );
618
619 *self.context_mode.borrow_mut() =
621 Some(RenderingContext::Placeholder(offscreen_canvas.as_traced()));
622
623 Ok(offscreen_canvas)
625 }
626
627 fn CaptureStream(
629 &self,
630 _frame_request_rate: Option<Finite<f64>>,
631 can_gc: CanGc,
632 ) -> DomRoot<MediaStream> {
633 let global = self.global();
634 let stream = MediaStream::new(&global, can_gc);
635 let track = MediaStreamTrack::new(
636 &global,
637 MediaStreamId::new(),
638 MediaStreamType::Video,
639 can_gc,
640 );
641 stream.AddTrack(&track);
642 stream
643 }
644}
645
646impl VirtualMethods for HTMLCanvasElement {
647 fn super_type(&self) -> Option<&dyn VirtualMethods> {
648 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
649 }
650
651 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
652 self.super_type()
653 .unwrap()
654 .attribute_mutated(attr, mutation, can_gc);
655 match attr.local_name() {
656 &local_name!("width") | &local_name!("height") => {
657 self.recreate_contexts_after_resize();
658 self.upcast::<Node>().dirty(NodeDamage::Other);
659 },
660 _ => {},
661 };
662 }
663
664 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
665 match *name {
666 local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
667 local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
668 _ => self
669 .super_type()
670 .unwrap()
671 .parse_plain_attribute(name, value),
672 }
673 }
674}
675
676impl Convert<GLContextAttributes> for WebGLContextAttributes {
677 fn convert(self) -> GLContextAttributes {
678 GLContextAttributes {
679 alpha: self.alpha,
680 depth: self.depth,
681 stencil: self.stencil,
682 antialias: self.antialias,
683 premultiplied_alpha: self.premultipliedAlpha,
684 preserve_drawing_buffer: self.preserveDrawingBuffer,
685 }
686 }
687}