1use std::cell::{Cell, RefCell};
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use euclid::default::Size2D;
10use html5ever::{LocalName, Prefix, local_name, ns};
11use js::error::throw_type_error;
12use js::rust::{HandleObject, HandleValue};
13use layout_api::HTMLCanvasData;
14use pixels::{EncodedImageType, Snapshot};
15use rustc_hash::FxHashMap;
16use script_bindings::cell::{DomRefCell, Ref};
17use script_bindings::reflector::DomObject;
18use script_bindings::weakref::WeakRef;
19use servo_base::Epoch;
20use servo_canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
21use servo_constellation_traits::BlobImpl;
22#[cfg(feature = "webgpu")]
23use servo_constellation_traits::ScriptToConstellationMessage;
24use servo_media::streams::MediaStreamType;
25use servo_media::streams::registry::MediaStreamId;
26use style::attr::AttrValue;
27use webrender_api::ImageKey;
28
29use crate::canvas_context::{CanvasContext, RenderingContext};
30use crate::conversions::Convert;
31use crate::dom::bindings::callback::ExceptionHandling;
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;
44use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom};
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::attributes::storage::AttrRef;
50use crate::dom::element::{AttributeMutation, Element};
51#[cfg(not(feature = "webgpu"))]
52use crate::dom::gpucanvascontext::GPUCanvasContext;
53use crate::dom::html::htmlelement::HTMLElement;
54use crate::dom::imagebitmaprenderingcontext::ImageBitmapRenderingContext;
55use crate::dom::mediastream::MediaStream;
56use crate::dom::mediastreamtrack::MediaStreamTrack;
57use crate::dom::node::{Node, NodeDamage, NodeTraits};
58use crate::dom::offscreencanvas::OffscreenCanvas;
59use crate::dom::values::UNSIGNED_LONG_MAX;
60use crate::dom::virtualmethods::VirtualMethods;
61use crate::dom::webgl::webgl2renderingcontext::WebGL2RenderingContext;
62use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
63#[cfg(feature = "webgpu")]
64use crate::dom::webgpu::gpucanvascontext::GPUCanvasContext;
65use crate::script_runtime::{CanGc, JSContext};
66
67const DEFAULT_WIDTH: u32 = 300;
68const DEFAULT_HEIGHT: u32 = 150;
69
70#[dom_struct]
72pub(crate) struct HTMLCanvasElement {
73 htmlelement: HTMLElement,
74
75 context_mode: DomRefCell<Option<RenderingContext>>,
77
78 callback_id: Cell<u32>,
80
81 #[conditional_malloc_size_of]
83 blob_callbacks: RefCell<FxHashMap<u32, Rc<BlobCallback>>>,
84
85 #[no_trace]
89 image_key: Cell<Option<ImageKey>>,
90}
91
92impl HTMLCanvasElement {
93 fn new_inherited(
94 local_name: LocalName,
95 prefix: Option<Prefix>,
96 document: &Document,
97 ) -> HTMLCanvasElement {
98 HTMLCanvasElement {
99 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
100 context_mode: DomRefCell::new(None),
101 callback_id: Cell::new(0),
102 blob_callbacks: RefCell::new(FxHashMap::default()),
103 image_key: Default::default(),
104 }
105 }
106
107 pub(crate) fn new(
108 cx: &mut js::context::JSContext,
109 local_name: LocalName,
110 prefix: Option<Prefix>,
111 document: &Document,
112 proto: Option<HandleObject>,
113 ) -> DomRoot<HTMLCanvasElement> {
114 Node::reflect_node_with_proto(
115 cx,
116 Box::new(HTMLCanvasElement::new_inherited(
117 local_name, prefix, document,
118 )),
119 document,
120 proto,
121 )
122 }
123
124 fn recreate_contexts_after_resize(&self) {
125 if let Some(ref context) = *self.context_mode.borrow() {
126 context.resize()
127 }
128 }
129
130 pub(crate) fn get_size(&self) -> Size2D<u32> {
131 Size2D::new(self.Width(), self.Height())
132 }
133
134 pub(crate) fn origin_is_clean(&self) -> bool {
135 match *self.context_mode.borrow() {
136 Some(ref context) => context.origin_is_clean(),
137 _ => true,
138 }
139 }
140
141 pub(crate) fn mark_as_dirty(&self) {
142 if let Some(ref context) = *self.context_mode.borrow() {
143 context.mark_as_dirty()
144 }
145 }
146
147 pub(crate) fn set_natural_width(&self, cx: &mut js::context::JSContext, value: u32) {
148 let value = if value > UNSIGNED_LONG_MAX {
149 DEFAULT_WIDTH
150 } else {
151 value
152 };
153 self.upcast::<Element>()
154 .set_attribute(cx, &html5ever::local_name!("width"), value.into());
155 }
156
157 pub(crate) fn set_natural_height(&self, cx: &mut js::context::JSContext, value: u32) {
158 let value = if value > UNSIGNED_LONG_MAX {
159 DEFAULT_HEIGHT
160 } else {
161 value
162 };
163 self.upcast::<Element>()
164 .set_attribute(cx, &html5ever::local_name!("height"), value.into());
165 }
166}
167
168impl LayoutDom<'_, HTMLCanvasElement> {
169 pub(crate) fn data(self) -> HTMLCanvasData {
170 let width_attr = self
171 .upcast::<Element>()
172 .get_attr_for_layout(&ns!(), &local_name!("width"));
173 let height_attr = self
174 .upcast::<Element>()
175 .get_attr_for_layout(&ns!(), &local_name!("height"));
176 HTMLCanvasData {
177 image_key: self.unsafe_get().image_key.get(),
178 width: width_attr.map_or(DEFAULT_WIDTH, |val| val.as_uint()),
179 height: height_attr.map_or(DEFAULT_HEIGHT, |val| val.as_uint()),
180 }
181 }
182}
183
184impl HTMLCanvasElement {
185 pub(crate) fn context(&self) -> Option<Ref<'_, RenderingContext>> {
186 Ref::filter_map(self.context_mode.borrow(), |ctx| ctx.as_ref()).ok()
187 }
188
189 fn set_rendering_context(&self, make_rendering_context: impl FnOnce() -> RenderingContext) {
190 self.upcast::<Node>().dirty(NodeDamage::ContentOrHeritage);
191 self.context_mode
192 .borrow_mut()
193 .replace(make_rendering_context());
194
195 let Some(rendering_context) = &*self.context_mode.borrow() else {
196 return;
197 };
198
199 let get_image_key = || self.owner_window().image_cache().get_image_key();
200 let image_key = match rendering_context {
201 RenderingContext::Placeholder(..) => None,
202 RenderingContext::Context2d(..) => get_image_key(),
203 RenderingContext::BitmapRenderer(..) => get_image_key(),
204 RenderingContext::WebGL(..) => get_image_key(),
205 RenderingContext::WebGL2(..) => get_image_key(),
206 #[cfg(feature = "webgpu")]
207 RenderingContext::WebGPU(..) => get_image_key(),
208 };
209 self.image_key.set(image_key);
210 if let Some(image_key) = image_key {
211 rendering_context.set_image_key(image_key);
212 }
213 }
214
215 fn get_or_init_2d_context(
216 &self,
217 cx: &mut js::context::JSContext,
218 ) -> Option<DomRoot<CanvasRenderingContext2D>> {
219 if let Some(ctx) = self.context() {
220 return match *ctx {
221 RenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
222 _ => None,
223 };
224 }
225
226 let window = self.owner_window();
227 let size = self.get_size();
228 let context = CanvasRenderingContext2D::new(
229 window.as_global_scope(),
230 self,
231 size,
232 CanGc::from_cx(cx),
233 )?;
234 self.set_rendering_context(|| RenderingContext::Context2d(Dom::from_ref(&*context)));
235 Some(context)
236 }
237
238 fn get_or_init_bitmaprenderer_context(
240 &self,
241 cx: &mut js::context::JSContext,
242 ) -> Option<DomRoot<ImageBitmapRenderingContext>> {
243 if let Some(ctx) = self.context() {
246 return match *ctx {
247 RenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
248 _ => None,
249 };
250 }
251
252 let canvas =
256 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
257
258 let context =
260 ImageBitmapRenderingContext::new(&self.owner_global(), &canvas, CanGc::from_cx(cx));
261 self.set_rendering_context(|| RenderingContext::BitmapRenderer(Dom::from_ref(&*context)));
262
263 Some(context)
265 }
266
267 fn get_or_init_webgl_context(
268 &self,
269 cx: &mut js::context::JSContext,
270 options: HandleValue,
271 ) -> Option<DomRoot<WebGLRenderingContext>> {
272 if let Some(ctx) = self.context() {
273 return match *ctx {
274 RenderingContext::WebGL(ref ctx) => Some(DomRoot::from_ref(ctx)),
275 _ => None,
276 };
277 }
278 let window = self.owner_window();
279 let canvas =
280 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
281 let size = self.get_size();
282 let attrs = Self::get_gl_attributes(cx, options)?;
283 let context =
284 WebGLRenderingContext::new(cx, &window, &canvas, WebGLVersion::WebGL1, size, attrs)?;
285 self.set_rendering_context(|| RenderingContext::WebGL(Dom::from_ref(&*context)));
286 Some(context)
287 }
288
289 fn get_or_init_webgl2_context(
290 &self,
291 cx: &mut js::context::JSContext,
292 options: HandleValue,
293 ) -> Option<DomRoot<WebGL2RenderingContext>> {
294 if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
295 {
296 return None;
297 }
298 if let Some(ctx) = self.context() {
299 return match *ctx {
300 RenderingContext::WebGL2(ref ctx) => Some(DomRoot::from_ref(ctx)),
301 _ => None,
302 };
303 }
304 let window = self.owner_window();
305 let canvas =
306 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
307 let size = self.get_size();
308 let attrs = Self::get_gl_attributes(cx, options)?;
309 let context = WebGL2RenderingContext::new(cx, &window, &canvas, size, attrs)?;
310 self.set_rendering_context(|| RenderingContext::WebGL2(Dom::from_ref(&*context)));
311 Some(context)
312 }
313
314 #[cfg(not(feature = "webgpu"))]
315 fn get_or_init_webgpu_context(&self) -> Option<DomRoot<GPUCanvasContext>> {
316 None
317 }
318
319 #[cfg(feature = "webgpu")]
320 fn get_or_init_webgpu_context(
321 &self,
322 cx: &mut js::context::JSContext,
323 ) -> Option<DomRoot<GPUCanvasContext>> {
324 use servo_base::generic_channel;
325
326 if let Some(ctx) = self.context() {
327 return match *ctx {
328 RenderingContext::WebGPU(ref ctx) => Some(DomRoot::from_ref(ctx)),
329 _ => None,
330 };
331 }
332 let (sender, receiver) = generic_channel::channel().unwrap();
333 let global_scope = self.owner_global();
334 let _ = global_scope
335 .script_to_constellation_chan()
336 .send(ScriptToConstellationMessage::GetWebGPUChan(sender));
337 receiver
338 .recv()
339 .expect("Failed to get WebGPU channel")
340 .map(|channel| {
341 let context = GPUCanvasContext::new(cx, &global_scope, self, channel);
342 self.set_rendering_context(|| RenderingContext::WebGPU(Dom::from_ref(&*context)));
343 context
344 })
345 }
346
347 pub(crate) fn get_base_webgl_context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
349 match *self.context_mode.borrow() {
350 Some(RenderingContext::WebGL(ref context)) => Some(DomRoot::from_ref(context)),
351 Some(RenderingContext::WebGL2(ref context)) => Some(context.base_context()),
352 _ => None,
353 }
354 }
355
356 #[expect(unsafe_code)]
357 fn get_gl_attributes(
358 cx: &mut js::context::JSContext,
359 options: HandleValue,
360 ) -> Option<GLContextAttributes> {
361 unsafe {
362 match WebGLContextAttributes::new(cx, options) {
363 Ok(ConversionResult::Success(attrs)) => Some(attrs.convert()),
364 Ok(ConversionResult::Failure(error)) => {
365 throw_type_error(cx.raw_cx(), &error);
366 None
367 },
368 _ => {
369 debug!("Unexpected error on conversion of WebGLContextAttributes");
370 None
371 },
372 }
373 }
374 }
375
376 pub(crate) fn is_valid(&self) -> bool {
377 self.Height() != 0 && self.Width() != 0
378 }
379
380 pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
381 match self.context_mode.borrow().as_ref() {
382 Some(context) => context.get_image_data(),
383 None => {
384 let size = self.get_size();
385 if size.is_empty() ||
386 pixels::compute_rgba8_byte_length_if_within_limit(
387 size.width as usize,
388 size.height as usize,
389 )
390 .is_none()
391 {
392 None
393 } else {
394 Some(Snapshot::cleared(size.cast()))
395 }
396 },
397 }
398 }
399
400 fn maybe_quality(quality: HandleValue) -> Option<f64> {
401 if quality.is_number() {
402 Some(quality.to_number())
403 } else {
404 None
405 }
406 }
407
408 pub(crate) fn update_rendering(&self, epoch: Epoch) -> Option<ImageKey> {
409 let context = self.context()?;
410 let image_key = self.image_key.get()?;
411 let pending = match &*context {
412 RenderingContext::Placeholder(..) => false,
413 RenderingContext::Context2d(context) => context.update_rendering(epoch),
414 RenderingContext::BitmapRenderer(context) => context.update_rendering(epoch),
415 RenderingContext::WebGL(context) => context.update_rendering(epoch),
416 RenderingContext::WebGL2(context) => context.base_context().update_rendering(epoch),
417 #[cfg(feature = "webgpu")]
418 RenderingContext::WebGPU(context) => context.update_rendering(epoch),
419 };
420
421 if pending {
422 return Some(image_key);
423 }
424 None
425 }
426}
427
428impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
429 make_uint_getter!(Width, "width", DEFAULT_WIDTH);
431
432 fn SetWidth(&self, cx: &mut js::context::JSContext, value: u32) -> Fallible<()> {
434 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
438 return Err(Error::InvalidState(None));
439 }
440
441 let value = if value > UNSIGNED_LONG_MAX {
442 DEFAULT_WIDTH
443 } else {
444 value
445 };
446 self.upcast::<Element>()
447 .set_attribute(cx, &html5ever::local_name!("width"), value.into());
448 Ok(())
449 }
450
451 make_uint_getter!(Height, "height", DEFAULT_HEIGHT);
453
454 fn SetHeight(&self, cx: &mut js::context::JSContext, value: u32) -> Fallible<()> {
456 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
460 return Err(Error::InvalidState(None));
461 }
462
463 let value = if value > UNSIGNED_LONG_MAX {
464 DEFAULT_HEIGHT
465 } else {
466 value
467 };
468 self.upcast::<Element>()
469 .set_attribute(cx, &html5ever::local_name!("height"), value.into());
470 Ok(())
471 }
472
473 fn GetContext(
475 &self,
476 cx: &mut js::context::JSContext,
477 id: DOMString,
478 options: HandleValue,
479 ) -> Fallible<Option<RootedRenderingContext>> {
480 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
482 return Err(Error::InvalidState(None));
483 }
484
485 Ok(match &*id.str() {
486 "2d" => self
487 .get_or_init_2d_context(cx)
488 .map(RootedRenderingContext::CanvasRenderingContext2D),
489 "bitmaprenderer" => self
490 .get_or_init_bitmaprenderer_context(cx)
491 .map(RootedRenderingContext::ImageBitmapRenderingContext),
492 "webgl" | "experimental-webgl" => self
493 .get_or_init_webgl_context(cx, options)
494 .map(RootedRenderingContext::WebGLRenderingContext),
495 "webgl2" | "experimental-webgl2" => self
496 .get_or_init_webgl2_context(cx, options)
497 .map(RootedRenderingContext::WebGL2RenderingContext),
498 #[cfg(feature = "webgpu")]
499 "webgpu" => self
500 .get_or_init_webgpu_context(cx)
501 .map(RootedRenderingContext::GPUCanvasContext),
502 _ => None,
503 })
504 }
505
506 fn ToDataURL(
508 &self,
509 _context: JSContext,
510 mime_type: DOMString,
511 quality: HandleValue,
512 ) -> Fallible<USVString> {
513 if !self.origin_is_clean() {
516 return Err(Error::Security(None));
517 }
518
519 if self.Width() == 0 || self.Height() == 0 {
524 return Ok(USVString("data:,".into()));
525 }
526
527 let Some(mut snapshot) = self.get_image_data() else {
530 return Ok(USVString("data:,".into()));
531 };
532
533 let image_type = EncodedImageType::from(&mime_type.str() as &str);
534
535 let mut url = format!("data:{};base64,", image_type.as_mime_type());
536
537 let mut encoder = base64::write::EncoderStringWriter::from_consumer(
538 &mut url,
539 &base64::engine::general_purpose::STANDARD,
540 );
541
542 if snapshot
543 .encode_for_mime_type(&image_type, Self::maybe_quality(quality), &mut encoder)
544 .is_err()
545 {
546 return Ok(USVString("data:,".into()));
548 }
549
550 encoder.into_inner();
552 Ok(USVString(url))
553 }
554
555 fn ToBlob(
557 &self,
558 _cx: JSContext,
559 callback: Rc<BlobCallback>,
560 mime_type: DOMString,
561 quality: HandleValue,
562 ) -> Fallible<()> {
563 if !self.origin_is_clean() {
567 return Err(Error::Security(None));
568 }
569
570 let result = if self.Width() == 0 || self.Height() == 0 {
575 None
576 } else {
577 self.get_image_data()
578 };
579
580 let this = Trusted::new(self);
581 let callback_id = self.callback_id.get().wrapping_add(1);
582 self.callback_id.set(callback_id);
583
584 self.blob_callbacks
585 .borrow_mut()
586 .insert(callback_id, callback);
587 let quality = Self::maybe_quality(quality);
588 let image_type = EncodedImageType::from(&mime_type.str() as &str);
589
590 self.global()
591 .task_manager()
592 .canvas_blob_task_source()
593 .queue(task!(to_blob: move |cx| {
594 let this = this.root();
595 let Some(callback) = &this.blob_callbacks.borrow_mut().remove(&callback_id) else {
596 return error!("Expected blob callback, but found none!");
597 };
598
599 let Some(mut snapshot) = result else {
600 let _ = callback.Call__(cx, None, ExceptionHandling::Report);
601 return;
602 };
603
604 let mut encoded: Vec<u8> = vec![];
609 let blob_impl;
610 let blob;
611 let result = match snapshot.encode_for_mime_type(&image_type, quality, &mut encoded) {
612 Ok(..) => {
613 blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
617 blob = Blob::new(cx, &this.global(), blob_impl);
618 Some(&*blob)
619 }
620 Err(..) => None,
621 };
622
623 let _ = callback.Call__(cx, result, ExceptionHandling::Report);
625 }));
626
627 Ok(())
628 }
629
630 fn TransferControlToOffscreen(
632 &self,
633 cx: &mut js::context::JSContext,
634 ) -> Fallible<DomRoot<OffscreenCanvas>> {
635 if self.context_mode.borrow().is_some() {
636 return Err(Error::InvalidState(None));
639 };
640
641 let offscreen_canvas = OffscreenCanvas::new(
647 cx,
648 &self.global(),
649 None,
650 self.Width().into(),
651 self.Height().into(),
652 Some(WeakRef::new(self)),
653 );
654
655 self.set_rendering_context(|| RenderingContext::Placeholder(offscreen_canvas.as_traced()));
657
658 Ok(offscreen_canvas)
660 }
661
662 fn CaptureStream(
664 &self,
665 cx: &mut js::context::JSContext,
666 _frame_request_rate: Option<Finite<f64>>,
667 ) -> DomRoot<MediaStream> {
668 let global = self.global();
669 let stream = MediaStream::new(cx, &global);
670 let track =
671 MediaStreamTrack::new(cx, &global, MediaStreamId::new(), MediaStreamType::Video);
672 stream.AddTrack(&track);
673 stream
674 }
675}
676
677impl VirtualMethods for HTMLCanvasElement {
678 fn super_type(&self) -> Option<&dyn VirtualMethods> {
679 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
680 }
681
682 fn attribute_mutated(
683 &self,
684 cx: &mut js::context::JSContext,
685 attr: AttrRef<'_>,
686 mutation: AttributeMutation,
687 ) {
688 self.super_type()
689 .unwrap()
690 .attribute_mutated(cx, attr, mutation);
691 match attr.local_name() {
692 &local_name!("width") | &local_name!("height") => {
693 self.recreate_contexts_after_resize();
694 self.upcast::<Node>().dirty(NodeDamage::Other);
695 },
696 _ => {},
697 };
698 }
699
700 fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
701 match attr.local_name() {
702 &local_name!("width") | &local_name!("height") => true,
703 _ => self
704 .super_type()
705 .unwrap()
706 .attribute_affects_presentational_hints(attr),
707 }
708 }
709
710 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
711 match *name {
712 local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
713 local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
714 _ => self
715 .super_type()
716 .unwrap()
717 .parse_plain_attribute(name, value),
718 }
719 }
720}
721
722impl Convert<GLContextAttributes> for WebGLContextAttributes {
723 fn convert(self) -> GLContextAttributes {
724 GLContextAttributes {
725 alpha: self.alpha,
726 depth: self.depth,
727 stencil: self.stencil,
728 antialias: self.antialias,
729 premultiplied_alpha: self.premultipliedAlpha,
730 preserve_drawing_buffer: self.preserveDrawingBuffer,
731 }
732 }
733}