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 =
342 GPUCanvasContext::new(&global_scope, self, channel, CanGc::from_cx(cx));
343 self.set_rendering_context(|| RenderingContext::WebGPU(Dom::from_ref(&*context)));
344 context
345 })
346 }
347
348 pub(crate) fn get_base_webgl_context(&self) -> Option<DomRoot<WebGLRenderingContext>> {
350 match *self.context_mode.borrow() {
351 Some(RenderingContext::WebGL(ref context)) => Some(DomRoot::from_ref(context)),
352 Some(RenderingContext::WebGL2(ref context)) => Some(context.base_context()),
353 _ => None,
354 }
355 }
356
357 #[expect(unsafe_code)]
358 fn get_gl_attributes(
359 cx: &mut js::context::JSContext,
360 options: HandleValue,
361 ) -> Option<GLContextAttributes> {
362 unsafe {
363 match WebGLContextAttributes::new(cx, options) {
364 Ok(ConversionResult::Success(attrs)) => Some(attrs.convert()),
365 Ok(ConversionResult::Failure(error)) => {
366 throw_type_error(cx.raw_cx(), &error);
367 None
368 },
369 _ => {
370 debug!("Unexpected error on conversion of WebGLContextAttributes");
371 None
372 },
373 }
374 }
375 }
376
377 pub(crate) fn is_valid(&self) -> bool {
378 self.Height() != 0 && self.Width() != 0
379 }
380
381 pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
382 match self.context_mode.borrow().as_ref() {
383 Some(context) => context.get_image_data(),
384 None => {
385 let size = self.get_size();
386 if size.is_empty() ||
387 pixels::compute_rgba8_byte_length_if_within_limit(
388 size.width as usize,
389 size.height as usize,
390 )
391 .is_none()
392 {
393 None
394 } else {
395 Some(Snapshot::cleared(size.cast()))
396 }
397 },
398 }
399 }
400
401 fn maybe_quality(quality: HandleValue) -> Option<f64> {
402 if quality.is_number() {
403 Some(quality.to_number())
404 } else {
405 None
406 }
407 }
408
409 pub(crate) fn update_rendering(&self, epoch: Epoch) -> Option<ImageKey> {
410 let context = self.context()?;
411 let image_key = self.image_key.get()?;
412 let pending = match &*context {
413 RenderingContext::Placeholder(..) => false,
414 RenderingContext::Context2d(context) => context.update_rendering(epoch),
415 RenderingContext::BitmapRenderer(context) => context.update_rendering(epoch),
416 RenderingContext::WebGL(context) => context.update_rendering(epoch),
417 RenderingContext::WebGL2(context) => context.base_context().update_rendering(epoch),
418 #[cfg(feature = "webgpu")]
419 RenderingContext::WebGPU(context) => context.update_rendering(epoch),
420 };
421
422 if pending {
423 return Some(image_key);
424 }
425 None
426 }
427}
428
429impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
430 make_uint_getter!(Width, "width", DEFAULT_WIDTH);
432
433 fn SetWidth(&self, cx: &mut js::context::JSContext, value: u32) -> Fallible<()> {
435 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
439 return Err(Error::InvalidState(None));
440 }
441
442 let value = if value > UNSIGNED_LONG_MAX {
443 DEFAULT_WIDTH
444 } else {
445 value
446 };
447 self.upcast::<Element>()
448 .set_attribute(cx, &html5ever::local_name!("width"), value.into());
449 Ok(())
450 }
451
452 make_uint_getter!(Height, "height", DEFAULT_HEIGHT);
454
455 fn SetHeight(&self, cx: &mut js::context::JSContext, value: u32) -> Fallible<()> {
457 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
461 return Err(Error::InvalidState(None));
462 }
463
464 let value = if value > UNSIGNED_LONG_MAX {
465 DEFAULT_HEIGHT
466 } else {
467 value
468 };
469 self.upcast::<Element>()
470 .set_attribute(cx, &html5ever::local_name!("height"), value.into());
471 Ok(())
472 }
473
474 fn GetContext(
476 &self,
477 cx: &mut js::context::JSContext,
478 id: DOMString,
479 options: HandleValue,
480 ) -> Fallible<Option<RootedRenderingContext>> {
481 if let Some(RenderingContext::Placeholder(_)) = *self.context_mode.borrow() {
483 return Err(Error::InvalidState(None));
484 }
485
486 Ok(match &*id.str() {
487 "2d" => self
488 .get_or_init_2d_context(cx)
489 .map(RootedRenderingContext::CanvasRenderingContext2D),
490 "bitmaprenderer" => self
491 .get_or_init_bitmaprenderer_context(cx)
492 .map(RootedRenderingContext::ImageBitmapRenderingContext),
493 "webgl" | "experimental-webgl" => self
494 .get_or_init_webgl_context(cx, options)
495 .map(RootedRenderingContext::WebGLRenderingContext),
496 "webgl2" | "experimental-webgl2" => self
497 .get_or_init_webgl2_context(cx, options)
498 .map(RootedRenderingContext::WebGL2RenderingContext),
499 #[cfg(feature = "webgpu")]
500 "webgpu" => self
501 .get_or_init_webgpu_context(cx)
502 .map(RootedRenderingContext::GPUCanvasContext),
503 _ => None,
504 })
505 }
506
507 fn ToDataURL(
509 &self,
510 _context: JSContext,
511 mime_type: DOMString,
512 quality: HandleValue,
513 ) -> Fallible<USVString> {
514 if !self.origin_is_clean() {
517 return Err(Error::Security(None));
518 }
519
520 if self.Width() == 0 || self.Height() == 0 {
525 return Ok(USVString("data:,".into()));
526 }
527
528 let Some(mut snapshot) = self.get_image_data() else {
531 return Ok(USVString("data:,".into()));
532 };
533
534 let image_type = EncodedImageType::from(&mime_type.str() as &str);
535
536 let mut url = format!("data:{};base64,", image_type.as_mime_type());
537
538 let mut encoder = base64::write::EncoderStringWriter::from_consumer(
539 &mut url,
540 &base64::engine::general_purpose::STANDARD,
541 );
542
543 if snapshot
544 .encode_for_mime_type(&image_type, Self::maybe_quality(quality), &mut encoder)
545 .is_err()
546 {
547 return Ok(USVString("data:,".into()));
549 }
550
551 encoder.into_inner();
553 Ok(USVString(url))
554 }
555
556 fn ToBlob(
558 &self,
559 _cx: JSContext,
560 callback: Rc<BlobCallback>,
561 mime_type: DOMString,
562 quality: HandleValue,
563 ) -> Fallible<()> {
564 if !self.origin_is_clean() {
568 return Err(Error::Security(None));
569 }
570
571 let result = if self.Width() == 0 || self.Height() == 0 {
576 None
577 } else {
578 self.get_image_data()
579 };
580
581 let this = Trusted::new(self);
582 let callback_id = self.callback_id.get().wrapping_add(1);
583 self.callback_id.set(callback_id);
584
585 self.blob_callbacks
586 .borrow_mut()
587 .insert(callback_id, callback);
588 let quality = Self::maybe_quality(quality);
589 let image_type = EncodedImageType::from(&mime_type.str() as &str);
590
591 self.global()
592 .task_manager()
593 .canvas_blob_task_source()
594 .queue(task!(to_blob: move |cx| {
595 let this = this.root();
596 let Some(callback) = &this.blob_callbacks.borrow_mut().remove(&callback_id) else {
597 return error!("Expected blob callback, but found none!");
598 };
599
600 let Some(mut snapshot) = result else {
601 let _ = callback.Call__(cx, None, ExceptionHandling::Report);
602 return;
603 };
604
605 let mut encoded: Vec<u8> = vec![];
610 let blob_impl;
611 let blob;
612 let result = match snapshot.encode_for_mime_type(&image_type, quality, &mut encoded) {
613 Ok(..) => {
614 blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
618 blob = Blob::new(cx, &this.global(), blob_impl);
619 Some(&*blob)
620 }
621 Err(..) => None,
622 };
623
624 let _ = callback.Call__(cx, result, ExceptionHandling::Report);
626 }));
627
628 Ok(())
629 }
630
631 fn TransferControlToOffscreen(
633 &self,
634 cx: &mut js::context::JSContext,
635 ) -> Fallible<DomRoot<OffscreenCanvas>> {
636 if self.context_mode.borrow().is_some() {
637 return Err(Error::InvalidState(None));
640 };
641
642 let offscreen_canvas = OffscreenCanvas::new(
648 cx,
649 &self.global(),
650 None,
651 self.Width().into(),
652 self.Height().into(),
653 Some(WeakRef::new(self)),
654 );
655
656 self.set_rendering_context(|| RenderingContext::Placeholder(offscreen_canvas.as_traced()));
658
659 Ok(offscreen_canvas)
661 }
662
663 fn CaptureStream(
665 &self,
666 cx: &mut js::context::JSContext,
667 _frame_request_rate: Option<Finite<f64>>,
668 ) -> DomRoot<MediaStream> {
669 let global = self.global();
670 let stream = MediaStream::new(cx, &global);
671 let track =
672 MediaStreamTrack::new(cx, &global, MediaStreamId::new(), MediaStreamType::Video);
673 stream.AddTrack(&track);
674 stream
675 }
676}
677
678impl VirtualMethods for HTMLCanvasElement {
679 fn super_type(&self) -> Option<&dyn VirtualMethods> {
680 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
681 }
682
683 fn attribute_mutated(
684 &self,
685 cx: &mut js::context::JSContext,
686 attr: AttrRef<'_>,
687 mutation: AttributeMutation,
688 ) {
689 self.super_type()
690 .unwrap()
691 .attribute_mutated(cx, attr, mutation);
692 match attr.local_name() {
693 &local_name!("width") | &local_name!("height") => {
694 self.recreate_contexts_after_resize();
695 self.upcast::<Node>().dirty(NodeDamage::Other);
696 },
697 _ => {},
698 };
699 }
700
701 fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
702 match attr.local_name() {
703 &local_name!("width") | &local_name!("height") => true,
704 _ => self
705 .super_type()
706 .unwrap()
707 .attribute_affects_presentational_hints(attr),
708 }
709 }
710
711 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
712 match *name {
713 local_name!("width") => AttrValue::from_u32(value.into(), DEFAULT_WIDTH),
714 local_name!("height") => AttrValue::from_u32(value.into(), DEFAULT_HEIGHT),
715 _ => self
716 .super_type()
717 .unwrap()
718 .parse_plain_attribute(name, value),
719 }
720 }
721}
722
723impl Convert<GLContextAttributes> for WebGLContextAttributes {
724 fn convert(self) -> GLContextAttributes {
725 GLContextAttributes {
726 alpha: self.alpha,
727 depth: self.depth,
728 stencil: self.stencil,
729 antialias: self.antialias,
730 premultiplied_alpha: self.premultipliedAlpha,
731 preserve_drawing_buffer: self.preserveDrawingBuffer,
732 }
733 }
734}