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