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::context::NoGC;
12use js::error::throw_type_error;
13use js::rust::{HandleObject, HandleValue};
14use layout_api::HTMLCanvasData;
15use pixels::{EncodedImageType, Snapshot};
16use rustc_hash::FxHashMap;
17use script_bindings::cell::{DomRefCell, Ref};
18use script_bindings::reflector::DomObject;
19use script_bindings::weakref::WeakRef;
20use servo_base::Epoch;
21use servo_canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
22use servo_constellation_traits::BlobImpl;
23#[cfg(feature = "webgpu")]
24use servo_constellation_traits::ScriptToConstellationMessage;
25use servo_media::streams::MediaStreamType;
26use servo_media::streams::registry::MediaStreamId;
27use style::attr::AttrValue;
28use webrender_api::ImageKey;
29
30use crate::canvas_context::{CanvasContext, RenderingContext};
31use crate::conversions::Convert;
32use crate::dom::bindings::callback::ExceptionHandling;
33use crate::dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::{
34 BlobCallback, HTMLCanvasElementMethods, RenderingContext as RootedRenderingContext,
35};
36use crate::dom::bindings::codegen::Bindings::MediaStreamBinding::MediaStreamMethods;
37use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
38use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas;
39use crate::dom::bindings::conversions::ConversionResult;
40use crate::dom::bindings::error::{Error, Fallible};
41use crate::dom::bindings::inheritance::Castable;
42use crate::dom::bindings::num::Finite;
43use crate::dom::bindings::refcounted::Trusted;
44use crate::dom::bindings::reflector::DomGlobal;
45use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom};
46use crate::dom::bindings::str::{DOMString, USVString};
47use crate::dom::blob::Blob;
48use crate::dom::canvasrenderingcontext2d::CanvasRenderingContext2D;
49use crate::dom::document::Document;
50use crate::dom::element::attributes::storage::AttrRef;
51use crate::dom::element::{AttributeMutation, Element};
52#[cfg(not(feature = "webgpu"))]
53use crate::dom::gpucanvascontext::GPUCanvasContext;
54use crate::dom::html::htmlelement::HTMLElement;
55use crate::dom::imagebitmaprenderingcontext::ImageBitmapRenderingContext;
56use crate::dom::mediastream::MediaStream;
57use crate::dom::mediastreamtrack::MediaStreamTrack;
58use crate::dom::node::virtualmethods::VirtualMethods;
59use crate::dom::node::{Node, NodeDamage, NodeTraits};
60use crate::dom::offscreencanvas::OffscreenCanvas;
61use crate::dom::values::UNSIGNED_LONG_MAX;
62use crate::dom::webgl::webgl2renderingcontext::WebGL2RenderingContext;
63use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
64#[cfg(feature = "webgpu")]
65use crate::dom::webgpu::gpucanvascontext::GPUCanvasContext;
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_weak_referenceable_node_with_proto(
115 cx,
116 Rc::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(
190 &self,
191 no_gc: &NoGC,
192 make_rendering_context: impl FnOnce() -> RenderingContext,
193 ) {
194 self.upcast::<Node>()
195 .dirty(no_gc, NodeDamage::ContentOrHeritage);
196 self.context_mode
197 .borrow_mut()
198 .replace(make_rendering_context());
199
200 let Some(rendering_context) = &*self.context_mode.borrow() else {
201 return;
202 };
203
204 let get_image_key = || self.owner_window().image_cache().get_image_key();
205 let image_key = match rendering_context {
206 RenderingContext::Placeholder(..) => None,
207 RenderingContext::Context2d(..) => get_image_key(),
208 RenderingContext::BitmapRenderer(..) => get_image_key(),
209 RenderingContext::WebGL(..) => get_image_key(),
210 RenderingContext::WebGL2(..) => get_image_key(),
211 #[cfg(feature = "webgpu")]
212 RenderingContext::WebGPU(..) => get_image_key(),
213 };
214 self.image_key.set(image_key);
215 if let Some(image_key) = image_key {
216 rendering_context.set_image_key(image_key);
217 }
218 }
219
220 fn get_or_init_2d_context(
221 &self,
222 cx: &mut js::context::JSContext,
223 ) -> Option<DomRoot<CanvasRenderingContext2D>> {
224 if let Some(ctx) = self.context() {
225 return match *ctx {
226 RenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
227 _ => None,
228 };
229 }
230
231 let window = self.owner_window();
232 let size = self.get_size();
233 let context = CanvasRenderingContext2D::new(cx, window.as_global_scope(), self, size)?;
234 self.set_rendering_context(cx.no_gc(), || {
235 RenderingContext::Context2d(Dom::from_ref(&*context))
236 });
237 Some(context)
238 }
239
240 fn get_or_init_bitmaprenderer_context(
242 &self,
243 cx: &mut js::context::JSContext,
244 ) -> Option<DomRoot<ImageBitmapRenderingContext>> {
245 if let Some(ctx) = self.context() {
248 return match *ctx {
249 RenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
250 _ => None,
251 };
252 }
253
254 let canvas =
258 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
259
260 let context = ImageBitmapRenderingContext::new(cx, &self.owner_global(), &canvas);
262 self.set_rendering_context(cx.no_gc(), || {
263 RenderingContext::BitmapRenderer(Dom::from_ref(&*context))
264 });
265
266 Some(context)
268 }
269
270 fn get_or_init_webgl_context(
271 &self,
272 cx: &mut js::context::JSContext,
273 options: HandleValue,
274 ) -> Option<DomRoot<WebGLRenderingContext>> {
275 if let Some(ctx) = self.context() {
276 return match *ctx {
277 RenderingContext::WebGL(ref ctx) => Some(DomRoot::from_ref(ctx)),
278 _ => None,
279 };
280 }
281 let window = self.owner_window();
282 let canvas =
283 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
284 let size = self.get_size();
285 let attrs = Self::get_gl_attributes(cx, options)?;
286 let context =
287 WebGLRenderingContext::new(cx, &window, &canvas, WebGLVersion::WebGL1, size, attrs)?;
288 self.set_rendering_context(cx.no_gc(), || {
289 RenderingContext::WebGL(Dom::from_ref(&*context))
290 });
291 Some(context)
292 }
293
294 fn get_or_init_webgl2_context(
295 &self,
296 cx: &mut js::context::JSContext,
297 options: HandleValue,
298 ) -> Option<DomRoot<WebGL2RenderingContext>> {
299 if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
300 {
301 return None;
302 }
303 if let Some(ctx) = self.context() {
304 return match *ctx {
305 RenderingContext::WebGL2(ref ctx) => Some(DomRoot::from_ref(ctx)),
306 _ => None,
307 };
308 }
309 let window = self.owner_window();
310 let canvas =
311 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(self));
312 let size = self.get_size();
313 let attrs = Self::get_gl_attributes(cx, options)?;
314 let context = WebGL2RenderingContext::new(cx, &window, &canvas, size, attrs)?;
315 self.set_rendering_context(cx.no_gc(), || {
316 RenderingContext::WebGL2(Dom::from_ref(&*context))
317 });
318 Some(context)
319 }
320
321 #[cfg(not(feature = "webgpu"))]
322 fn get_or_init_webgpu_context(&self) -> Option<DomRoot<GPUCanvasContext>> {
323 None
324 }
325
326 #[cfg(feature = "webgpu")]
327 fn get_or_init_webgpu_context(
328 &self,
329 cx: &mut js::context::JSContext,
330 ) -> Option<DomRoot<GPUCanvasContext>> {
331 use servo_base::generic_channel;
332
333 if let Some(ctx) = self.context() {
334 return match *ctx {
335 RenderingContext::WebGPU(ref ctx) => Some(DomRoot::from_ref(ctx)),
336 _ => None,
337 };
338 }
339 let (sender, receiver) = generic_channel::channel().unwrap();
340 let global_scope = self.owner_global();
341 let _ = global_scope
342 .script_to_constellation_chan()
343 .send(ScriptToConstellationMessage::GetWebGPUChan(sender));
344 receiver
345 .recv()
346 .expect("Failed to get WebGPU channel")
347 .map(|channel| {
348 let context = GPUCanvasContext::new(cx, &global_scope, self, channel);
349 self.set_rendering_context(cx.no_gc(), || {
350 RenderingContext::WebGPU(Dom::from_ref(&*context))
351 });
352 context
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(&self, mime_type: DOMString, quality: HandleValue) -> Fallible<USVString> {
508 if !self.origin_is_clean() {
511 return Err(Error::Security(None));
512 }
513
514 if self.Width() == 0 || self.Height() == 0 {
519 return Ok(USVString("data:,".into()));
520 }
521
522 let Some(mut snapshot) = self.get_image_data() else {
525 return Ok(USVString("data:,".into()));
526 };
527
528 let image_type = EncodedImageType::from(&mime_type.str() as &str);
529
530 let mut url = format!("data:{};base64,", image_type.as_mime_type());
531
532 let mut encoder = base64::write::EncoderStringWriter::from_consumer(
533 &mut url,
534 &base64::engine::general_purpose::STANDARD,
535 );
536
537 if snapshot
538 .encode_for_mime_type(&image_type, Self::maybe_quality(quality), &mut encoder)
539 .is_err()
540 {
541 return Ok(USVString("data:,".into()));
543 }
544
545 encoder.into_inner();
547 Ok(USVString(url))
548 }
549
550 fn ToBlob(
552 &self,
553 callback: Rc<BlobCallback>,
554 mime_type: DOMString,
555 quality: HandleValue,
556 ) -> Fallible<()> {
557 if !self.origin_is_clean() {
561 return Err(Error::Security(None));
562 }
563
564 let result = if self.Width() == 0 || self.Height() == 0 {
569 None
570 } else {
571 self.get_image_data()
572 };
573
574 let this = Trusted::new(self);
575 let callback_id = self.callback_id.get().wrapping_add(1);
576 self.callback_id.set(callback_id);
577
578 self.blob_callbacks
579 .borrow_mut()
580 .insert(callback_id, callback);
581 let quality = Self::maybe_quality(quality);
582 let image_type = EncodedImageType::from(&mime_type.str() as &str);
583
584 self.global()
585 .task_manager()
586 .canvas_blob_task_source()
587 .queue(task!(to_blob: move |cx| {
588 let this = this.root();
589 let Some(callback) = &this.blob_callbacks.borrow_mut().remove(&callback_id) else {
590 return error!("Expected blob callback, but found none!");
591 };
592
593 let Some(mut snapshot) = result else {
594 let _ = callback.Call__(cx, None, ExceptionHandling::Report);
595 return;
596 };
597
598 let mut encoded: Vec<u8> = vec![];
603 let blob_impl;
604 let blob;
605 let result = match snapshot.encode_for_mime_type(&image_type, quality, &mut encoded) {
606 Ok(..) => {
607 blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
611 blob = Blob::new(cx, &this.global(), blob_impl);
612 Some(&*blob)
613 }
614 Err(..) => None,
615 };
616
617 let _ = callback.Call__(cx, result, ExceptionHandling::Report);
619 }));
620
621 Ok(())
622 }
623
624 fn TransferControlToOffscreen(
626 &self,
627 cx: &mut js::context::JSContext,
628 ) -> Fallible<DomRoot<OffscreenCanvas>> {
629 if self.context_mode.borrow().is_some() {
630 return Err(Error::InvalidState(None));
633 };
634
635 let offscreen_canvas = OffscreenCanvas::new(
641 cx,
642 &self.global(),
643 None,
644 self.Width().into(),
645 self.Height().into(),
646 Some(WeakRef::new(self)),
647 );
648
649 self.set_rendering_context(cx.no_gc(), || {
651 RenderingContext::Placeholder(offscreen_canvas.as_traced())
652 });
653
654 Ok(offscreen_canvas)
656 }
657
658 fn CaptureStream(
660 &self,
661 cx: &mut js::context::JSContext,
662 _frame_request_rate: Option<Finite<f64>>,
663 ) -> DomRoot<MediaStream> {
664 let global = self.global();
665 let stream = MediaStream::new(cx, &global);
666 let track =
667 MediaStreamTrack::new(cx, &global, MediaStreamId::new(), MediaStreamType::Video);
668 stream.AddTrack(&track);
669 stream
670 }
671}
672
673impl VirtualMethods for HTMLCanvasElement {
674 fn super_type(&self) -> Option<&dyn VirtualMethods> {
675 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
676 }
677
678 fn attribute_mutated(
679 &self,
680 cx: &mut js::context::JSContext,
681 attr: AttrRef<'_>,
682 mutation: AttributeMutation,
683 ) {
684 self.super_type()
685 .unwrap()
686 .attribute_mutated(cx, attr, mutation);
687 match attr.local_name() {
688 &local_name!("width") | &local_name!("height") => {
689 self.recreate_contexts_after_resize();
690 self.upcast::<Node>().dirty(cx.no_gc(), NodeDamage::Other);
691 },
692 _ => {},
693 };
694 }
695
696 fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
697 match attr.local_name() {
698 &local_name!("width") | &local_name!("height") => true,
699 _ => self
700 .super_type()
701 .unwrap()
702 .attribute_affects_presentational_hints(attr),
703 }
704 }
705
706 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
707 match *name {
708 local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
709 local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
710 _ => self
711 .super_type()
712 .unwrap()
713 .parse_plain_attribute(name, value),
714 }
715 }
716}
717
718impl Convert<GLContextAttributes> for WebGLContextAttributes {
719 fn convert(self) -> GLContextAttributes {
720 GLContextAttributes {
721 alpha: self.alpha,
722 depth: self.depth,
723 stencil: self.stencil,
724 antialias: self.antialias,
725 premultiplied_alpha: self.premultipliedAlpha,
726 preserve_drawing_buffer: self.preserveDrawingBuffer,
727 }
728 }
729}