script: Move canvas stuff into dom/canvas folder (#39071)

Move all canvas stuff to canvas folder and 2d canvas to canvas/2d
folder. Webgl and webgpu context remain in respective folders as
outlined in
https://github.com/servo/servo/issues/38901#issuecomment-3243020235

Testing: Just refactor.
Part of https://github.com/servo/servo/issues/38901

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
Sam 2025-09-02 09:43:10 +02:00 committed by GitHub
parent f4dd2960b8
commit 069ddbfd12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 138 additions and 124 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,108 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use canvas_traits::canvas::{
CanvasGradientStop, FillOrStrokeStyle, LinearGradientStyle, RadialGradientStyle,
};
use dom_struct::dom_struct;
use super::canvas_state::parse_color;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::CanvasGradientMethods;
use crate::dom::bindings::error::{Error, ErrorResult};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
// https://html.spec.whatwg.org/multipage/#canvasgradient
#[dom_struct]
pub(crate) struct CanvasGradient {
reflector_: Reflector,
style: CanvasGradientStyle,
#[no_trace]
stops: DomRefCell<Vec<CanvasGradientStop>>,
}
#[derive(Clone, JSTraceable, MallocSizeOf)]
pub(crate) enum CanvasGradientStyle {
Linear(#[no_trace] LinearGradientStyle),
Radial(#[no_trace] RadialGradientStyle),
}
impl CanvasGradient {
fn new_inherited(style: CanvasGradientStyle) -> CanvasGradient {
CanvasGradient {
reflector_: Reflector::new(),
style,
stops: DomRefCell::new(Vec::new()),
}
}
pub(crate) fn new(
global: &GlobalScope,
style: CanvasGradientStyle,
can_gc: CanGc,
) -> DomRoot<CanvasGradient> {
reflect_dom_object(
Box::new(CanvasGradient::new_inherited(style)),
global,
can_gc,
)
}
}
impl CanvasGradientMethods<crate::DomTypeHolder> for CanvasGradient {
// https://html.spec.whatwg.org/multipage/#dom-canvasgradient-addcolorstop
fn AddColorStop(&self, offset: Finite<f64>, color: DOMString) -> ErrorResult {
if *offset < 0f64 || *offset > 1f64 {
return Err(Error::IndexSize);
}
let color = match parse_color(None, &color) {
Ok(color) => color,
Err(_) => return Err(Error::Syntax(None)),
};
self.stops.borrow_mut().push(CanvasGradientStop {
offset: (*offset),
color,
});
Ok(())
}
}
pub(crate) trait ToFillOrStrokeStyle {
fn to_fill_or_stroke_style(self) -> FillOrStrokeStyle;
}
impl ToFillOrStrokeStyle for &CanvasGradient {
fn to_fill_or_stroke_style(self) -> FillOrStrokeStyle {
let gradient_stops = self.stops.borrow().clone();
match self.style {
CanvasGradientStyle::Linear(ref gradient) => {
FillOrStrokeStyle::LinearGradient(LinearGradientStyle::new(
gradient.x0,
gradient.y0,
gradient.x1,
gradient.y1,
gradient_stops,
))
},
CanvasGradientStyle::Radial(ref gradient) => {
FillOrStrokeStyle::RadialGradient(RadialGradientStyle::new(
gradient.x0,
gradient.y0,
gradient.r0,
gradient.x1,
gradient.y1,
gradient.r1,
gradient_stops,
))
},
}
}
}

View file

@ -0,0 +1,121 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use canvas_traits::canvas::{FillOrStrokeStyle, RepetitionStyle, SurfaceStyle};
use dom_struct::dom_struct;
use euclid::default::{Size2D, Transform2D};
use pixels::{IpcSnapshot, Snapshot};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::CanvasPatternMethods;
use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
use crate::dom::bindings::error::ErrorResult;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::canvasgradient::ToFillOrStrokeStyle;
use crate::dom::dommatrixreadonly::dommatrix2dinit_to_matrix;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
/// <https://html.spec.whatwg.org/multipage/#canvaspattern>
#[dom_struct]
pub(crate) struct CanvasPattern {
reflector_: Reflector,
#[no_trace]
surface_data: IpcSnapshot,
#[no_trace]
surface_size: Size2D<u32>,
repeat_x: bool,
repeat_y: bool,
#[no_trace]
transform: DomRefCell<Transform2D<f32>>,
origin_clean: bool,
}
impl CanvasPattern {
fn new_inherited(
surface_data: Snapshot,
surface_size: Size2D<u32>,
repeat: RepetitionStyle,
origin_clean: bool,
) -> CanvasPattern {
let (x, y) = match repeat {
RepetitionStyle::Repeat => (true, true),
RepetitionStyle::RepeatX => (true, false),
RepetitionStyle::RepeatY => (false, true),
RepetitionStyle::NoRepeat => (false, false),
};
CanvasPattern {
reflector_: Reflector::new(),
surface_data: surface_data.as_ipc(),
surface_size,
repeat_x: x,
repeat_y: y,
transform: DomRefCell::new(Transform2D::identity()),
origin_clean,
}
}
pub(crate) fn new(
global: &GlobalScope,
surface_data: Snapshot,
surface_size: Size2D<u32>,
repeat: RepetitionStyle,
origin_clean: bool,
can_gc: CanGc,
) -> DomRoot<CanvasPattern> {
reflect_dom_object(
Box::new(CanvasPattern::new_inherited(
surface_data,
surface_size,
repeat,
origin_clean,
)),
global,
can_gc,
)
}
pub(crate) fn origin_is_clean(&self) -> bool {
self.origin_clean
}
}
impl CanvasPatternMethods<crate::DomTypeHolder> for CanvasPattern {
/// <https://html.spec.whatwg.org/multipage/#dom-canvaspattern-settransform>
fn SetTransform(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
// Step 1. Let matrix be the result of creating a DOMMatrix from the 2D
// dictionary transform.
let matrix = dommatrix2dinit_to_matrix(transform)?;
// Step 2. If one or more of matrix's m11 element, m12 element, m21
// element, m22 element, m41 element, or m42 element are infinite or
// NaN, then return.
if !matrix.m11.is_finite() ||
!matrix.m12.is_finite() ||
!matrix.m21.is_finite() ||
!matrix.m22.is_finite() ||
!matrix.m31.is_finite() ||
!matrix.m32.is_finite()
{
return Ok(());
}
// Step 3. Reset the pattern's transformation matrix to matrix.
*self.transform.borrow_mut() = matrix.cast();
Ok(())
}
}
impl ToFillOrStrokeStyle for &CanvasPattern {
fn to_fill_or_stroke_style(self) -> FillOrStrokeStyle {
FillOrStrokeStyle::Surface(SurfaceStyle::new(
self.surface_data.clone(),
self.surface_size,
self.repeat_x,
self.repeat_y,
*self.transform.borrow(),
))
}
}

View file

@ -0,0 +1,730 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use base::Epoch;
use canvas_traits::canvas::{Canvas2dMsg, CanvasId};
use dom_struct::dom_struct;
use euclid::default::Size2D;
use ipc_channel::ipc;
use pixels::Snapshot;
use script_bindings::inheritance::Castable;
use servo_url::ServoUrl;
use webrender_api::ImageKey;
use super::canvas_state::CanvasState;
use crate::canvas_context::{CanvasContext, CanvasHelpers, LayoutCanvasRenderingContextHelpers};
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
CanvasRenderingContext2DMethods, CanvasTextAlign, CanvasTextBaseline,
};
use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
use crate::dom::bindings::codegen::UnionTypes::{
HTMLCanvasElementOrOffscreenCanvas, StringOrCanvasGradientOrCanvasPattern,
};
use crate::dom::bindings::error::{ErrorResult, Fallible};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
use crate::dom::bindings::root::{DomRoot, LayoutDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::globalscope::GlobalScope;
use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::imagedata::ImageData;
use crate::dom::node::{Node, NodeDamage, NodeTraits};
use crate::dom::path2d::Path2D;
use crate::dom::textmetrics::TextMetrics;
use crate::script_runtime::CanGc;
// https://html.spec.whatwg.org/multipage/#canvasrenderingcontext2d
#[dom_struct]
pub(crate) struct CanvasRenderingContext2D {
reflector_: Reflector,
canvas: HTMLCanvasElementOrOffscreenCanvas,
canvas_state: CanvasState,
}
impl CanvasRenderingContext2D {
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
pub(crate) fn new_inherited(
global: &GlobalScope,
canvas: HTMLCanvasElementOrOffscreenCanvas,
size: Size2D<u32>,
) -> Option<CanvasRenderingContext2D> {
let canvas_state =
CanvasState::new(global, Size2D::new(size.width as u64, size.height as u64))?;
Some(CanvasRenderingContext2D {
reflector_: Reflector::new(),
canvas,
canvas_state,
})
}
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
pub(crate) fn new(
global: &GlobalScope,
canvas: &HTMLCanvasElement,
size: Size2D<u32>,
can_gc: CanGc,
) -> Option<DomRoot<CanvasRenderingContext2D>> {
let boxed = Box::new(CanvasRenderingContext2D::new_inherited(
global,
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(DomRoot::from_ref(canvas)),
size,
)?);
Some(reflect_dom_object(boxed, global, can_gc))
}
// https://html.spec.whatwg.org/multipage/#reset-the-rendering-context-to-its-default-state
fn reset_to_initial_state(&self) {
self.canvas_state.reset_to_initial_state();
}
/// <https://html.spec.whatwg.org/multipage/#concept-canvas-set-bitmap-dimensions>
pub(crate) fn set_canvas_bitmap_dimensions(&self, size: Size2D<u64>) {
self.canvas_state.set_bitmap_dimensions(size);
}
pub(crate) fn take_missing_image_urls(&self) -> Vec<ServoUrl> {
std::mem::take(&mut self.canvas_state.get_missing_image_urls().borrow_mut())
}
pub(crate) fn get_canvas_id(&self) -> CanvasId {
self.canvas_state.get_canvas_id()
}
pub(crate) fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
self.canvas_state.send_canvas_2d_msg(msg)
}
}
impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, CanvasRenderingContext2D> {
fn canvas_data_source(self) -> Option<ImageKey> {
let canvas_state = &self.unsafe_get().canvas_state;
if canvas_state.is_paintable() {
Some(canvas_state.image_key())
} else {
None
}
}
}
impl CanvasContext for CanvasRenderingContext2D {
type ID = CanvasId;
fn context_id(&self) -> Self::ID {
self.canvas_state.get_canvas_id()
}
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
Some(self.canvas.clone())
}
fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
if !self.onscreen() {
return false;
}
self.canvas_state.update_rendering(Some(canvas_epoch))
}
fn resize(&self) {
self.set_canvas_bitmap_dimensions(self.size().cast())
}
fn reset_bitmap(&self) {
self.canvas_state.reset_bitmap()
}
fn get_image_data(&self) -> Option<Snapshot> {
if !self.canvas_state.is_paintable() {
return None;
}
let (sender, receiver) = ipc::channel().unwrap();
self.canvas_state
.send_canvas_2d_msg(Canvas2dMsg::GetImageData(None, sender));
Some(receiver.recv().unwrap().to_owned())
}
fn origin_is_clean(&self) -> bool {
self.canvas_state.origin_is_clean()
}
fn mark_as_dirty(&self) {
if let Some(canvas) = self.canvas.canvas() {
canvas.upcast::<Node>().dirty(NodeDamage::Other);
canvas.owner_document().add_dirty_2d_canvas(self);
}
}
fn image_key(&self) -> Option<ImageKey> {
Some(self.canvas_state.image_key())
}
}
// We add a guard to each of methods by the spec:
// http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas_CR/
//
// > Except where otherwise specified, for the 2D context interface,
// > any method call with a numeric argument whose value is infinite or a NaN value must be ignored.
//
// Restricted values are guarded in glue code. Therefore we need not add a guard.
//
// FIXME: this behavior should might be generated by some annotattions to idl.
impl CanvasRenderingContext2DMethods<crate::DomTypeHolder> for CanvasRenderingContext2D {
// https://html.spec.whatwg.org/multipage/#dom-context-2d-canvas
fn Canvas(&self) -> DomRoot<HTMLCanvasElement> {
match &self.canvas {
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => canvas.clone(),
_ => panic!("Should not be called from offscreen canvas"),
}
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-save
fn Save(&self) {
self.canvas_state.save()
}
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
// https://html.spec.whatwg.org/multipage/#dom-context-2d-restore
fn Restore(&self) {
self.canvas_state.restore()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
fn Reset(&self) {
self.canvas_state.reset()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-scale
fn Scale(&self, x: f64, y: f64) {
self.canvas_state.scale(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate
fn Rotate(&self, angle: f64) {
self.canvas_state.rotate(angle)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-translate
fn Translate(&self, x: f64, y: f64) {
self.canvas_state.translate(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-transform
fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.canvas_state.transform(a, b, c, d, e, f)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
self.canvas_state.get_transform(&self.global(), can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
self.canvas_state.set_transform(a, b, c, d, e, f);
Ok(())
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
self.canvas_state.set_transform_(transform)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform
fn ResetTransform(&self) {
self.canvas_state.reset_transform()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
fn GlobalAlpha(&self) -> f64 {
self.canvas_state.global_alpha()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
fn SetGlobalAlpha(&self, alpha: f64) {
self.canvas_state.set_global_alpha(alpha)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn GlobalCompositeOperation(&self) -> DOMString {
self.canvas_state.global_composite_operation()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
self.canvas_state.set_global_composite_operation(op_str)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.fill_rect(x, y, width, height);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.clear_rect(x, y, width, height);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.stroke_rect(x, y, width, height);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath
fn BeginPath(&self) {
self.canvas_state.begin_path()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath
fn ClosePath(&self) {
self.canvas_state.close_path()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
fn Fill(&self, fill_rule: CanvasFillRule) {
self.canvas_state.fill(fill_rule);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
self.canvas_state.fill_(path.segments(), fill_rule);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
fn Stroke(&self) {
self.canvas_state.stroke();
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
fn Stroke_(&self, path: &Path2D) {
self.canvas_state.stroke_(path.segments());
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
fn Clip(&self, fill_rule: CanvasFillRule) {
self.canvas_state.clip(fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
self.canvas_state.clip_(path.segments(), fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.canvas_state
.is_point_in_path(&self.global(), x, y, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.canvas_state
.is_point_in_path_(&self.global(), path.segments(), x, y, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext
fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
self.canvas_state.fill_text(
&self.global(),
self.canvas.canvas().as_deref(),
text,
x,
y,
max_width,
);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#textmetrics
fn MeasureText(&self, text: DOMString, can_gc: CanGc) -> DomRoot<TextMetrics> {
self.canvas_state.measure_text(
&self.global(),
self.canvas.canvas().as_deref(),
text,
can_gc,
)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-font
fn Font(&self) -> DOMString {
self.canvas_state.font()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-font
fn SetFont(&self, value: DOMString) {
self.canvas_state
.set_font(self.canvas.canvas().as_deref(), value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign
fn TextAlign(&self) -> CanvasTextAlign {
self.canvas_state.text_align()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign
fn SetTextAlign(&self, value: CanvasTextAlign) {
self.canvas_state.set_text_align(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline
fn TextBaseline(&self) -> CanvasTextBaseline {
self.canvas_state.text_baseline()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline
fn SetTextBaseline(&self, value: CanvasTextBaseline) {
self.canvas_state.set_text_baseline(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-direction
fn Direction(&self) -> CanvasDirection {
self.canvas_state.direction()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-direction
fn SetDirection(&self, value: CanvasDirection) {
self.canvas_state.set_direction(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
self.canvas_state
.draw_image(self.canvas.canvas().as_deref(), image, dx, dy)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage_(
&self,
image: CanvasImageSource,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.canvas_state
.draw_image_(self.canvas.canvas().as_deref(), image, dx, dy, dw, dh)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage__(
&self,
image: CanvasImageSource,
sx: f64,
sy: f64,
sw: f64,
sh: f64,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.canvas_state.draw_image__(
self.canvas.canvas().as_deref(),
image,
sx,
sy,
sw,
sh,
dx,
dy,
dw,
dh,
)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto
fn MoveTo(&self, x: f64, y: f64) {
self.canvas_state.move_to(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto
fn LineTo(&self, x: f64, y: f64) {
self.canvas_state.line_to(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-rect
fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.rect(x, y, width, height)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto
fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto
fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
self.canvas_state
.bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-arc
fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
self.canvas_state.arc(x, y, r, start, end, ccw)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto
fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse
fn Ellipse(
&self,
x: f64,
y: f64,
rx: f64,
ry: f64,
rotation: f64,
start: f64,
end: f64,
ccw: bool,
) -> ErrorResult {
self.canvas_state
.ellipse(x, y, rx, ry, rotation, start, end, ccw)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
fn ImageSmoothingEnabled(&self) -> bool {
self.canvas_state.image_smoothing_enabled()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
fn SetImageSmoothingEnabled(&self, value: bool) {
self.canvas_state.set_image_smoothing_enabled(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.canvas_state.stroke_style()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
self.canvas_state
.set_stroke_style(self.canvas.canvas().as_deref(), value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.canvas_state.fill_style()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
self.canvas_state
.set_fill_style(self.canvas.canvas().as_deref(), value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata
fn CreateImageData(&self, sw: i32, sh: i32, can_gc: CanGc) -> Fallible<DomRoot<ImageData>> {
self.canvas_state
.create_image_data(&self.global(), sw, sh, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata
fn CreateImageData_(
&self,
imagedata: &ImageData,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
self.canvas_state
.create_image_data_(&self.global(), imagedata, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-getimagedata
fn GetImageData(
&self,
sx: i32,
sy: i32,
sw: i32,
sh: i32,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
self.canvas_state
.get_image_data(self.canvas.size(), &self.global(), sx, sy, sw, sh, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
fn PutImageData(&self, imagedata: &ImageData, dx: i32, dy: i32) {
self.canvas_state
.put_image_data(self.canvas.size(), imagedata, dx, dy)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
#[allow(unsafe_code)]
fn PutImageData_(
&self,
imagedata: &ImageData,
dx: i32,
dy: i32,
dirty_x: i32,
dirty_y: i32,
dirty_width: i32,
dirty_height: i32,
) {
self.canvas_state.put_image_data_(
self.canvas.size(),
imagedata,
dx,
dy,
dirty_x,
dirty_y,
dirty_width,
dirty_height,
);
self.mark_as_dirty();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient
fn CreateLinearGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
can_gc: CanGc,
) -> DomRoot<CanvasGradient> {
self.canvas_state
.create_linear_gradient(&self.global(), x0, y0, x1, y1, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient
fn CreateRadialGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
r0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
r1: Finite<f64>,
can_gc: CanGc,
) -> Fallible<DomRoot<CanvasGradient>> {
self.canvas_state
.create_radial_gradient(&self.global(), x0, y0, r0, x1, y1, r1, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern
fn CreatePattern(
&self,
image: CanvasImageSource,
repetition: DOMString,
can_gc: CanGc,
) -> Fallible<Option<DomRoot<CanvasPattern>>> {
self.canvas_state
.create_pattern(&self.global(), image, repetition, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
fn LineWidth(&self) -> f64 {
self.canvas_state.line_width()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
fn SetLineWidth(&self, width: f64) {
self.canvas_state.set_line_width(width)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn LineCap(&self) -> CanvasLineCap {
self.canvas_state.line_cap()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn SetLineCap(&self, cap: CanvasLineCap) {
self.canvas_state.set_line_cap(cap)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn LineJoin(&self) -> CanvasLineJoin {
self.canvas_state.line_join()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn SetLineJoin(&self, join: CanvasLineJoin) {
self.canvas_state.set_line_join(join)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
fn MiterLimit(&self) -> f64 {
self.canvas_state.miter_limit()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
fn SetMiterLimit(&self, limit: f64) {
self.canvas_state.set_miter_limit(limit)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
fn SetLineDash(&self, segments: Vec<f64>) {
self.canvas_state.set_line_dash(segments);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
fn GetLineDash(&self) -> Vec<f64> {
self.canvas_state.line_dash()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
fn LineDashOffset(&self) -> f64 {
self.canvas_state.line_dash_offset()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
fn SetLineDashOffset(&self, offset: f64) {
self.canvas_state.set_line_dash_offset(offset);
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
fn ShadowOffsetX(&self) -> f64 {
self.canvas_state.shadow_offset_x()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
fn SetShadowOffsetX(&self, value: f64) {
self.canvas_state.set_shadow_offset_x(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
fn ShadowOffsetY(&self) -> f64 {
self.canvas_state.shadow_offset_y()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
fn SetShadowOffsetY(&self, value: f64) {
self.canvas_state.set_shadow_offset_y(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
fn ShadowBlur(&self) -> f64 {
self.canvas_state.shadow_blur()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
fn SetShadowBlur(&self, value: f64) {
self.canvas_state.set_shadow_blur(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
fn ShadowColor(&self) -> DOMString {
self.canvas_state.shadow_color()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
fn SetShadowColor(&self, value: DOMString) {
self.canvas_state
.set_shadow_color(self.canvas.canvas().as_deref(), value)
}
}

View file

@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
mod canvas_state;
pub(crate) mod canvasgradient;
pub(crate) mod canvaspattern;
#[allow(dead_code)]
pub(crate) mod canvasrenderingcontext2d;
pub(crate) mod offscreencanvasrenderingcontext2d;
pub(crate) mod paintrenderingcontext2d;
pub(crate) mod path2d;
pub(crate) mod textmetrics;

View file

@ -0,0 +1,613 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::bindings::codegen::GenericBindings::CanvasRenderingContext2DBinding::CanvasRenderingContext2D_Binding::CanvasRenderingContext2DMethods;
use crate::canvas_context::CanvasContext;
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
use canvas_traits::canvas::Canvas2dMsg;
use dom_struct::dom_struct;
use pixels::Snapshot;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
CanvasTextAlign, CanvasTextBaseline,
};
use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding::OffscreenCanvasRenderingContext2DMethods;
use crate::dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern;
use crate::dom::bindings::error::{ErrorResult, Fallible};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::globalscope::GlobalScope;
use crate::dom::imagedata::ImageData;
use crate::dom::offscreencanvas::OffscreenCanvas;
use crate::dom::path2d::Path2D;
use crate::dom::textmetrics::TextMetrics;
use crate::script_runtime::CanGc;
use super::canvasrenderingcontext2d::CanvasRenderingContext2D;
#[dom_struct]
pub(crate) struct OffscreenCanvasRenderingContext2D {
context: CanvasRenderingContext2D,
}
impl OffscreenCanvasRenderingContext2D {
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
fn new_inherited(
global: &GlobalScope,
canvas: &OffscreenCanvas,
) -> Option<OffscreenCanvasRenderingContext2D> {
let size = canvas.get_size().cast();
Some(OffscreenCanvasRenderingContext2D {
context: CanvasRenderingContext2D::new_inherited(
global,
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(DomRoot::from_ref(canvas)),
size,
)?,
})
}
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
pub(crate) fn new(
global: &GlobalScope,
canvas: &OffscreenCanvas,
can_gc: CanGc,
) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
let boxed = Box::new(OffscreenCanvasRenderingContext2D::new_inherited(
global, canvas,
)?);
Some(reflect_dom_object(boxed, global, can_gc))
}
pub(crate) fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
self.context.send_canvas_2d_msg(msg)
}
}
impl CanvasContext for OffscreenCanvasRenderingContext2D {
type ID = <CanvasRenderingContext2D as CanvasContext>::ID;
fn context_id(&self) -> Self::ID {
self.context.context_id()
}
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
self.context.canvas()
}
fn resize(&self) {
self.context.resize()
}
fn reset_bitmap(&self) {
self.context.reset_bitmap()
}
fn get_image_data(&self) -> Option<Snapshot> {
self.context.get_image_data()
}
fn origin_is_clean(&self) -> bool {
self.context.origin_is_clean()
}
fn image_key(&self) -> Option<webrender_api::ImageKey> {
None
}
}
impl OffscreenCanvasRenderingContext2DMethods<crate::DomTypeHolder>
for OffscreenCanvasRenderingContext2D
{
// https://html.spec.whatwg.org/multipage/offscreencontext2d-canvas
fn Canvas(&self) -> DomRoot<OffscreenCanvas> {
match self.context.canvas() {
Some(HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas)) => canvas,
_ => panic!("Should not be called from onscreen canvas"),
}
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.context.FillRect(x, y, width, height);
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.context.ClearRect(x, y, width, height);
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.context.StrokeRect(x, y, width, height);
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
fn ShadowOffsetX(&self) -> f64 {
self.context.ShadowOffsetX()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
fn SetShadowOffsetX(&self, value: f64) {
self.context.SetShadowOffsetX(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
fn ShadowOffsetY(&self) -> f64 {
self.context.ShadowOffsetY()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
fn SetShadowOffsetY(&self, value: f64) {
self.context.SetShadowOffsetY(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
fn ShadowBlur(&self) -> f64 {
self.context.ShadowBlur()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
fn SetShadowBlur(&self, value: f64) {
self.context.SetShadowBlur(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
fn ShadowColor(&self) -> DOMString {
self.context.ShadowColor()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
fn SetShadowColor(&self, value: DOMString) {
self.context.SetShadowColor(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.context.StrokeStyle()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
self.context.SetStrokeStyle(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.context.FillStyle()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
self.context.SetFillStyle(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient
fn CreateLinearGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
can_gc: CanGc,
) -> DomRoot<CanvasGradient> {
self.context.CreateLinearGradient(x0, y0, x1, y1, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient
fn CreateRadialGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
r0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
r1: Finite<f64>,
can_gc: CanGc,
) -> Fallible<DomRoot<CanvasGradient>> {
self.context
.CreateRadialGradient(x0, y0, r0, x1, y1, r1, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern
fn CreatePattern(
&self,
image: CanvasImageSource,
repetition: DOMString,
can_gc: CanGc,
) -> Fallible<Option<DomRoot<CanvasPattern>>> {
self.context.CreatePattern(image, repetition, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-save
fn Save(&self) {
self.context.Save()
}
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
// https://html.spec.whatwg.org/multipage/#dom-context-2d-restore
fn Restore(&self) {
self.context.Restore()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
fn Reset(&self) {
self.context.Reset()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
fn GlobalAlpha(&self) -> f64 {
self.context.GlobalAlpha()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
fn SetGlobalAlpha(&self, alpha: f64) {
self.context.SetGlobalAlpha(alpha)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn GlobalCompositeOperation(&self) -> DOMString {
self.context.GlobalCompositeOperation()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
self.context.SetGlobalCompositeOperation(op_str)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
fn ImageSmoothingEnabled(&self) -> bool {
self.context.ImageSmoothingEnabled()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
fn SetImageSmoothingEnabled(&self, value: bool) {
self.context.SetImageSmoothingEnabled(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext
fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
self.context.FillText(text, x, y, max_width)
}
// https://html.spec.whatwg.org/multipage/#textmetrics
fn MeasureText(&self, text: DOMString, can_gc: CanGc) -> DomRoot<TextMetrics> {
self.context.MeasureText(text, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-font
fn Font(&self) -> DOMString {
self.context.Font()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-font
fn SetFont(&self, value: DOMString) {
self.context.SetFont(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign
fn TextAlign(&self) -> CanvasTextAlign {
self.context.TextAlign()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign
fn SetTextAlign(&self, value: CanvasTextAlign) {
self.context.SetTextAlign(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline
fn TextBaseline(&self) -> CanvasTextBaseline {
self.context.TextBaseline()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline
fn SetTextBaseline(&self, value: CanvasTextBaseline) {
self.context.SetTextBaseline(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-direction
fn Direction(&self) -> CanvasDirection {
self.context.Direction()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-direction
fn SetDirection(&self, value: CanvasDirection) {
self.context.SetDirection(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
fn LineWidth(&self) -> f64 {
self.context.LineWidth()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
fn SetLineWidth(&self, width: f64) {
self.context.SetLineWidth(width)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn LineCap(&self) -> CanvasLineCap {
self.context.LineCap()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn SetLineCap(&self, cap: CanvasLineCap) {
self.context.SetLineCap(cap)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn LineJoin(&self) -> CanvasLineJoin {
self.context.LineJoin()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn SetLineJoin(&self, join: CanvasLineJoin) {
self.context.SetLineJoin(join)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
fn MiterLimit(&self) -> f64 {
self.context.MiterLimit()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
fn SetMiterLimit(&self, limit: f64) {
self.context.SetMiterLimit(limit)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
fn SetLineDash(&self, segments: Vec<f64>) {
self.context.SetLineDash(segments)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
fn GetLineDash(&self) -> Vec<f64> {
self.context.GetLineDash()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
fn LineDashOffset(&self) -> f64 {
self.context.LineDashOffset()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
fn SetLineDashOffset(&self, offset: f64) {
self.context.SetLineDashOffset(offset)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata
fn CreateImageData(&self, sw: i32, sh: i32, can_gc: CanGc) -> Fallible<DomRoot<ImageData>> {
self.context.CreateImageData(sw, sh, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata
fn CreateImageData_(
&self,
imagedata: &ImageData,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
self.context.CreateImageData_(imagedata, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-getimagedata
fn GetImageData(
&self,
sx: i32,
sy: i32,
sw: i32,
sh: i32,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
self.context.GetImageData(sx, sy, sw, sh, can_gc)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
fn PutImageData(&self, imagedata: &ImageData, dx: i32, dy: i32) {
self.context.PutImageData(imagedata, dx, dy)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
#[allow(unsafe_code)]
fn PutImageData_(
&self,
imagedata: &ImageData,
dx: i32,
dy: i32,
dirty_x: i32,
dirty_y: i32,
dirty_width: i32,
dirty_height: i32,
) {
self.context.PutImageData_(
imagedata,
dx,
dy,
dirty_x,
dirty_y,
dirty_width,
dirty_height,
)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
self.context.DrawImage(image, dx, dy)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage_(
&self,
image: CanvasImageSource,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.context.DrawImage_(image, dx, dy, dw, dh)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage__(
&self,
image: CanvasImageSource,
sx: f64,
sy: f64,
sw: f64,
sh: f64,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.context
.DrawImage__(image, sx, sy, sw, sh, dx, dy, dw, dh)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath
fn BeginPath(&self) {
self.context.BeginPath()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
fn Fill(&self, fill_rule: CanvasFillRule) {
self.context.Fill(fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
self.context.Fill_(path, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
fn Stroke(&self) {
self.context.Stroke()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
fn Stroke_(&self, path: &Path2D) {
self.context.Stroke_(path)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
fn Clip(&self, fill_rule: CanvasFillRule) {
self.context.Clip(fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
self.context.Clip_(path, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.context.IsPointInPath(x, y, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.context.IsPointInPath_(path, x, y, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-scale
fn Scale(&self, x: f64, y: f64) {
self.context.Scale(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate
fn Rotate(&self, angle: f64) {
self.context.Rotate(angle)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-translate
fn Translate(&self, x: f64, y: f64) {
self.context.Translate(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-transform
fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.context.Transform(a, b, c, d, e, f)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
self.context.GetTransform(can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
self.context.SetTransform(a, b, c, d, e, f)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
self.context.SetTransform_(transform)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform
fn ResetTransform(&self) {
self.context.ResetTransform()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath
fn ClosePath(&self) {
self.context.ClosePath()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto
fn MoveTo(&self, x: f64, y: f64) {
self.context.MoveTo(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto
fn LineTo(&self, x: f64, y: f64) {
self.context.LineTo(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-rect
fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
self.context.Rect(x, y, width, height)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto
fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
self.context.QuadraticCurveTo(cpx, cpy, x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto
fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
self.context.BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-arc
fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
self.context.Arc(x, y, r, start, end, ccw)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto
fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
self.context.ArcTo(cp1x, cp1y, cp2x, cp2y, r)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse
fn Ellipse(
&self,
x: f64,
y: f64,
rx: f64,
ry: f64,
rotation: f64,
start: f64,
end: f64,
ccw: bool,
) -> ErrorResult {
self.context
.Ellipse(x, y, rx, ry, rotation, start, end, ccw)
}
}

View file

@ -0,0 +1,505 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use dom_struct::dom_struct;
use euclid::{Scale, Size2D};
use script_bindings::reflector::Reflector;
use servo_url::ServoUrl;
use style_traits::CSSPixel;
use webrender_api::ImageKey;
use webrender_api::units::DevicePixel;
use super::canvas_state::CanvasState;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
};
use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
use crate::dom::bindings::codegen::Bindings::PaintRenderingContext2DBinding::PaintRenderingContext2DMethods;
use crate::dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern;
use crate::dom::bindings::error::{ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{DomGlobal as _, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
use crate::dom::path2d::Path2D;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct PaintRenderingContext2D {
reflector_: Reflector,
canvas_state: CanvasState,
#[no_trace]
device_pixel_ratio: Cell<Scale<f32, CSSPixel, DevicePixel>>,
}
impl PaintRenderingContext2D {
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
fn new_inherited(global: &PaintWorkletGlobalScope) -> Option<PaintRenderingContext2D> {
Some(PaintRenderingContext2D {
reflector_: Reflector::new(),
canvas_state: CanvasState::new(global.upcast(), Size2D::zero())?,
device_pixel_ratio: Cell::new(Scale::new(1.0)),
})
}
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
pub(crate) fn new(
global: &PaintWorkletGlobalScope,
can_gc: CanGc,
) -> Option<DomRoot<PaintRenderingContext2D>> {
Some(reflect_dom_object(
Box::new(PaintRenderingContext2D::new_inherited(global)?),
global,
can_gc,
))
}
pub(crate) fn update_rendering(&self) -> bool {
self.canvas_state.update_rendering(None)
}
/// Send update to canvas paint thread and returns [`ImageKey`]
pub(crate) fn image_key(&self) -> ImageKey {
self.canvas_state.image_key()
}
pub(crate) fn take_missing_image_urls(&self) -> Vec<ServoUrl> {
std::mem::take(&mut self.canvas_state.get_missing_image_urls().borrow_mut())
}
pub(crate) fn set_bitmap_dimensions(
&self,
size: Size2D<f32, CSSPixel>,
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
) {
let size = size * device_pixel_ratio;
self.device_pixel_ratio.set(device_pixel_ratio);
self.canvas_state
.set_bitmap_dimensions(size.to_untyped().to_u64());
self.scale_by_device_pixel_ratio();
}
fn scale_by_device_pixel_ratio(&self) {
let device_pixel_ratio = self.device_pixel_ratio.get().get() as f64;
if device_pixel_ratio != 1.0 {
self.Scale(device_pixel_ratio, device_pixel_ratio);
}
}
}
impl PaintRenderingContext2DMethods<crate::DomTypeHolder> for PaintRenderingContext2D {
// https://html.spec.whatwg.org/multipage/#dom-context-2d-save
fn Save(&self) {
self.canvas_state.save()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-restore
fn Restore(&self) {
self.canvas_state.restore()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
fn Reset(&self) {
self.canvas_state.reset()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-scale
fn Scale(&self, x: f64, y: f64) {
self.canvas_state.scale(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate
fn Rotate(&self, angle: f64) {
self.canvas_state.rotate(angle)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-translate
fn Translate(&self, x: f64, y: f64) {
self.canvas_state.translate(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-transform
fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.canvas_state.transform(a, b, c, d, e, f)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
self.canvas_state.get_transform(&self.global(), can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
self.canvas_state.set_transform(a, b, c, d, e, f);
self.scale_by_device_pixel_ratio();
Ok(())
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
self.canvas_state.set_transform_(transform)?;
self.scale_by_device_pixel_ratio();
Ok(())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform
fn ResetTransform(&self) {
self.canvas_state.reset_transform();
self.scale_by_device_pixel_ratio();
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
fn GlobalAlpha(&self) -> f64 {
self.canvas_state.global_alpha()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
fn SetGlobalAlpha(&self, alpha: f64) {
self.canvas_state.set_global_alpha(alpha)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn GlobalCompositeOperation(&self) -> DOMString {
self.canvas_state.global_composite_operation()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
self.canvas_state.set_global_composite_operation(op_str)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.fill_rect(x, y, width, height)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.clear_rect(x, y, width, height)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.stroke_rect(x, y, width, height)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath
fn BeginPath(&self) {
self.canvas_state.begin_path()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath
fn ClosePath(&self) {
self.canvas_state.close_path()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
fn Fill(&self, fill_rule: CanvasFillRule) {
self.canvas_state.fill(fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
self.canvas_state.fill_(path.segments(), fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
fn Stroke(&self) {
self.canvas_state.stroke()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
fn Stroke_(&self, path: &Path2D) {
self.canvas_state.stroke_(path.segments())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
fn Clip(&self, fill_rule: CanvasFillRule) {
self.canvas_state.clip(fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
self.canvas_state.clip_(path.segments(), fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.canvas_state
.is_point_in_path(&self.global(), x, y, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.canvas_state
.is_point_in_path_(&self.global(), path.segments(), x, y, fill_rule)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
self.canvas_state.draw_image(None, image, dx, dy)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage_(
&self,
image: CanvasImageSource,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.canvas_state.draw_image_(None, image, dx, dy, dw, dh)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
fn DrawImage__(
&self,
image: CanvasImageSource,
sx: f64,
sy: f64,
sw: f64,
sh: f64,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.canvas_state
.draw_image__(None, image, sx, sy, sw, sh, dx, dy, dw, dh)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto
fn MoveTo(&self, x: f64, y: f64) {
self.canvas_state.move_to(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto
fn LineTo(&self, x: f64, y: f64) {
self.canvas_state.line_to(x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-rect
fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.rect(x, y, width, height)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto
fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto
fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
self.canvas_state
.bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-arc
fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
self.canvas_state.arc(x, y, r, start, end, ccw)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto
fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse
fn Ellipse(
&self,
x: f64,
y: f64,
rx: f64,
ry: f64,
rotation: f64,
start: f64,
end: f64,
ccw: bool,
) -> ErrorResult {
self.canvas_state
.ellipse(x, y, rx, ry, rotation, start, end, ccw)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
fn ImageSmoothingEnabled(&self) -> bool {
self.canvas_state.image_smoothing_enabled()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
fn SetImageSmoothingEnabled(&self, value: bool) {
self.canvas_state.set_image_smoothing_enabled(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.canvas_state.stroke_style()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
self.canvas_state.set_stroke_style(None, value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.canvas_state.fill_style()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
self.canvas_state.set_fill_style(None, value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient
fn CreateLinearGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
) -> DomRoot<CanvasGradient> {
self.canvas_state
.create_linear_gradient(&self.global(), x0, y0, x1, y1, CanGc::note())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient
fn CreateRadialGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
r0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
r1: Finite<f64>,
) -> Fallible<DomRoot<CanvasGradient>> {
self.canvas_state.create_radial_gradient(
&self.global(),
x0,
y0,
r0,
x1,
y1,
r1,
CanGc::note(),
)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern
fn CreatePattern(
&self,
image: CanvasImageSource,
repetition: DOMString,
) -> Fallible<Option<DomRoot<CanvasPattern>>> {
self.canvas_state
.create_pattern(&self.global(), image, repetition, CanGc::note())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
fn LineWidth(&self) -> f64 {
self.canvas_state.line_width()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
fn SetLineWidth(&self, width: f64) {
self.canvas_state.set_line_width(width)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn LineCap(&self) -> CanvasLineCap {
self.canvas_state.line_cap()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
fn SetLineCap(&self, cap: CanvasLineCap) {
self.canvas_state.set_line_cap(cap)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn LineJoin(&self) -> CanvasLineJoin {
self.canvas_state.line_join()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
fn SetLineJoin(&self, join: CanvasLineJoin) {
self.canvas_state.set_line_join(join)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
fn MiterLimit(&self) -> f64 {
self.canvas_state.miter_limit()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
fn SetMiterLimit(&self, limit: f64) {
self.canvas_state.set_miter_limit(limit)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
fn SetLineDash(&self, segments: Vec<f64>) {
self.canvas_state.set_line_dash(segments);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
fn GetLineDash(&self) -> Vec<f64> {
self.canvas_state.line_dash()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
fn LineDashOffset(&self) -> f64 {
self.canvas_state.line_dash_offset()
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
fn SetLineDashOffset(&self, offset: f64) {
self.canvas_state.set_line_dash_offset(offset);
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
fn ShadowOffsetX(&self) -> f64 {
self.canvas_state.shadow_offset_x()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
fn SetShadowOffsetX(&self, value: f64) {
self.canvas_state.set_shadow_offset_x(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
fn ShadowOffsetY(&self) -> f64 {
self.canvas_state.shadow_offset_y()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
fn SetShadowOffsetY(&self, value: f64) {
self.canvas_state.set_shadow_offset_y(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
fn ShadowBlur(&self) -> f64 {
self.canvas_state.shadow_blur()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
fn SetShadowBlur(&self, value: f64) {
self.canvas_state.set_shadow_blur(value)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
fn ShadowColor(&self) -> DOMString {
self.canvas_state.shadow_color()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
fn SetShadowColor(&self, value: DOMString) {
self.canvas_state.set_shadow_color(None, value)
}
}

View file

@ -0,0 +1,226 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use canvas_traits::canvas::Path;
use dom_struct::dom_struct;
use js::rust::HandleObject;
use script_bindings::codegen::GenericBindings::DOMMatrixBinding::DOMMatrix2DInit;
use script_bindings::error::ErrorResult;
use script_bindings::str::DOMString;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::Path2DMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::root::DomRoot;
use crate::dom::dommatrixreadonly::dommatrix2dinit_to_matrix;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct Path2D {
reflector_: Reflector,
#[ignore_malloc_size_of = "Defined in kurbo."]
#[no_trace]
path: RefCell<Path>,
}
impl Path2D {
pub(crate) fn new() -> Path2D {
Self {
reflector_: Reflector::new(),
path: RefCell::new(Path::new()),
}
}
pub(crate) fn new_with_path(other: &Path2D) -> Path2D {
Self {
reflector_: Reflector::new(),
path: other.path.clone(),
}
}
pub(crate) fn new_with_str(path: &str) -> Path2D {
Self {
reflector_: Reflector::new(),
path: RefCell::new(Path::from_svg(path)),
}
}
pub(crate) fn segments(&self) -> Path {
self.path.borrow().clone()
}
pub(crate) fn is_path_empty(&self) -> bool {
self.path.borrow().0.is_empty()
}
fn add_path(&self, other: &Path2D, transform: &DOMMatrix2DInit) -> ErrorResult {
// Step 1. If the Path2D object path has no subpaths, then return.
if other.is_path_empty() {
return Ok(());
}
// Step 2 Let matrix be the result of creating a DOMMatrix from the 2D dictionary transform.
let matrix = dommatrix2dinit_to_matrix(transform)?;
// Step 3. If one or more of matrix's m11 element, m12 element, m21
// element, m22 element, m41 element, or m42 element are infinite or
// NaN, then return.
if !matrix.m11.is_finite() ||
!matrix.m12.is_finite() ||
!matrix.m21.is_finite() ||
!matrix.m22.is_finite() ||
!matrix.m31.is_finite() ||
!matrix.m32.is_finite()
{
return Ok(());
}
// Step 4. Create a copy of all the subpaths in path. Let c be this copy.
let mut c = other.segments();
// Step 5. Transform all the coordinates and lines in c by the transform matrix `matrix`.
c.transform(matrix);
let mut path = self.path.borrow_mut();
// Step 6. Let (x, y) be the last point in the last subpath of c
let last_point = path.last_point();
// Step 7. Add all the subpaths in c to a.
path.0.extend(c.0);
// Step 8. Create a new subpath in `a` with (x, y) as the only point in the subpath.
if let Some(last_point) = last_point {
path.move_to(last_point.x, last_point.y);
}
Ok(())
}
}
impl Path2DMethods<crate::DomTypeHolder> for Path2D {
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-addpath>
fn AddPath(&self, other: &Path2D, transform: &DOMMatrix2DInit) -> ErrorResult {
self.add_path(other, transform)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath>
fn ClosePath(&self) {
self.path.borrow_mut().close_path();
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto>
fn MoveTo(&self, x: f64, y: f64) {
self.path.borrow_mut().move_to(x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto>
fn LineTo(&self, x: f64, y: f64) {
self.path.borrow_mut().line_to(x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto>
fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
self.path.borrow_mut().quadratic_curve_to(cpx, cpy, x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto>
fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
self.path
.borrow_mut()
.bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto>
fn ArcTo(&self, x1: f64, y1: f64, x2: f64, y2: f64, radius: f64) -> Fallible<()> {
self.path
.borrow_mut()
.arc_to(x1, y1, x2, y2, radius)
.map_err(|_| Error::IndexSize)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rect>
fn Rect(&self, x: f64, y: f64, w: f64, h: f64) {
self.path.borrow_mut().rect(x, y, w, h);
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arc>
fn Arc(
&self,
x: f64,
y: f64,
radius: f64,
start_angle: f64,
end_angle: f64,
counterclockwise: bool,
) -> Fallible<()> {
self.path
.borrow_mut()
.arc(x, y, radius, start_angle, end_angle, counterclockwise)
.map_err(|_| Error::IndexSize)
}
/// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse>
fn Ellipse(
&self,
x: f64,
y: f64,
radius_x: f64,
radius_y: f64,
rotation_angle: f64,
start_angle: f64,
end_angle: f64,
counterclockwise: bool,
) -> Fallible<()> {
self.path
.borrow_mut()
.ellipse(
x,
y,
radius_x,
radius_y,
rotation_angle,
start_angle,
end_angle,
counterclockwise,
)
.map_err(|_| Error::IndexSize)
}
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-dev>
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<Path2D> {
reflect_dom_object_with_proto(Box::new(Self::new()), global, proto, can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-dev>
fn Constructor_(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
other: &Path2D,
) -> DomRoot<Path2D> {
reflect_dom_object_with_proto(Box::new(Self::new_with_path(other)), global, proto, can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-path2d-dev>
fn Constructor__(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
path_string: DOMString,
) -> DomRoot<Path2D> {
reflect_dom_object_with_proto(
Box::new(Self::new_with_str(path_string.str())),
global,
proto,
can_gc,
)
}
}

View file

@ -0,0 +1,186 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use crate::dom::bindings::codegen::Bindings::TextMetricsBinding::TextMetricsMethods;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
#[dom_struct]
#[allow(non_snake_case)]
pub(crate) struct TextMetrics {
reflector_: Reflector,
width: Finite<f64>,
actualBoundingBoxLeft: Finite<f64>,
actualBoundingBoxRight: Finite<f64>,
fontBoundingBoxAscent: Finite<f64>,
fontBoundingBoxDescent: Finite<f64>,
actualBoundingBoxAscent: Finite<f64>,
actualBoundingBoxDescent: Finite<f64>,
emHeightAscent: Finite<f64>,
emHeightDescent: Finite<f64>,
hangingBaseline: Finite<f64>,
alphabeticBaseline: Finite<f64>,
ideographicBaseline: Finite<f64>,
}
#[allow(non_snake_case)]
impl TextMetrics {
#[allow(clippy::too_many_arguments)]
fn new_inherited(
width: f64,
actualBoundingBoxLeft: f64,
actualBoundingBoxRight: f64,
fontBoundingBoxAscent: f64,
fontBoundingBoxDescent: f64,
actualBoundingBoxAscent: f64,
actualBoundingBoxDescent: f64,
emHeightAscent: f64,
emHeightDescent: f64,
hangingBaseline: f64,
alphabeticBaseline: f64,
ideographicBaseline: f64,
) -> TextMetrics {
TextMetrics {
reflector_: Reflector::new(),
width: Finite::wrap(width),
actualBoundingBoxLeft: Finite::wrap(actualBoundingBoxLeft),
actualBoundingBoxRight: Finite::wrap(actualBoundingBoxRight),
fontBoundingBoxAscent: Finite::wrap(fontBoundingBoxAscent),
fontBoundingBoxDescent: Finite::wrap(fontBoundingBoxDescent),
actualBoundingBoxAscent: Finite::wrap(actualBoundingBoxAscent),
actualBoundingBoxDescent: Finite::wrap(actualBoundingBoxDescent),
emHeightAscent: Finite::wrap(emHeightAscent),
emHeightDescent: Finite::wrap(emHeightDescent),
hangingBaseline: Finite::wrap(hangingBaseline),
alphabeticBaseline: Finite::wrap(alphabeticBaseline),
ideographicBaseline: Finite::wrap(ideographicBaseline),
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
global: &GlobalScope,
width: f64,
actualBoundingBoxLeft: f64,
actualBoundingBoxRight: f64,
fontBoundingBoxAscent: f64,
fontBoundingBoxDescent: f64,
actualBoundingBoxAscent: f64,
actualBoundingBoxDescent: f64,
emHeightAscent: f64,
emHeightDescent: f64,
hangingBaseline: f64,
alphabeticBaseline: f64,
ideographicBaseline: f64,
can_gc: CanGc,
) -> DomRoot<TextMetrics> {
reflect_dom_object(
Box::new(TextMetrics::new_inherited(
width,
actualBoundingBoxLeft,
actualBoundingBoxRight,
fontBoundingBoxAscent,
fontBoundingBoxDescent,
actualBoundingBoxAscent,
actualBoundingBoxDescent,
emHeightAscent,
emHeightDescent,
hangingBaseline,
alphabeticBaseline,
ideographicBaseline,
)),
global,
can_gc,
)
}
pub(crate) fn default(global: &GlobalScope, can_gc: CanGc) -> DomRoot<Self> {
reflect_dom_object(
Box::new(Self {
reflector_: Reflector::new(),
width: Default::default(),
actualBoundingBoxLeft: Default::default(),
actualBoundingBoxRight: Default::default(),
fontBoundingBoxAscent: Default::default(),
fontBoundingBoxDescent: Default::default(),
actualBoundingBoxAscent: Default::default(),
actualBoundingBoxDescent: Default::default(),
emHeightAscent: Default::default(),
emHeightDescent: Default::default(),
hangingBaseline: Default::default(),
alphabeticBaseline: Default::default(),
ideographicBaseline: Default::default(),
}),
global,
can_gc,
)
}
}
impl TextMetricsMethods<crate::DomTypeHolder> for TextMetrics {
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-width>
fn Width(&self) -> Finite<f64> {
self.width
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxleft>
fn ActualBoundingBoxLeft(&self) -> Finite<f64> {
self.actualBoundingBoxLeft
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxright>
fn ActualBoundingBoxRight(&self) -> Finite<f64> {
self.actualBoundingBoxRight
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent>
fn FontBoundingBoxAscent(&self) -> Finite<f64> {
self.fontBoundingBoxAscent
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent>
fn FontBoundingBoxDescent(&self) -> Finite<f64> {
self.fontBoundingBoxDescent
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxascent>
fn ActualBoundingBoxAscent(&self) -> Finite<f64> {
self.actualBoundingBoxAscent
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxdescent>
fn ActualBoundingBoxDescent(&self) -> Finite<f64> {
self.actualBoundingBoxDescent
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightascent>
fn EmHeightAscent(&self) -> Finite<f64> {
self.emHeightAscent
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightdescent>
fn EmHeightDescent(&self) -> Finite<f64> {
self.emHeightDescent
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-hangingbaseline>
fn HangingBaseline(&self) -> Finite<f64> {
self.hangingBaseline
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-alphabeticbaseline>
fn AlphabeticBaseline(&self) -> Finite<f64> {
self.alphabeticBaseline
}
/// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-ideographicbaseline>
fn IdeographicBaseline(&self) -> Finite<f64> {
self.ideographicBaseline
}
}

View file

@ -0,0 +1,377 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Common interfaces for Canvas Contexts
use base::Epoch;
use euclid::default::Size2D;
use layout_api::HTMLCanvasData;
use pixels::Snapshot;
use script_bindings::root::{Dom, DomRoot};
use webrender_api::ImageKey;
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::node::{Node, NodeDamage};
#[cfg(feature = "webgpu")]
use crate::dom::types::GPUCanvasContext;
use crate::dom::types::{
CanvasRenderingContext2D, ImageBitmapRenderingContext, OffscreenCanvas,
OffscreenCanvasRenderingContext2D, WebGL2RenderingContext, WebGLRenderingContext,
};
pub(crate) trait LayoutCanvasRenderingContextHelpers {
/// `None` is rendered as transparent black (cleared canvas)
fn canvas_data_source(self) -> Option<ImageKey>;
}
pub(crate) trait LayoutHTMLCanvasElementHelpers {
fn data(self) -> HTMLCanvasData;
}
pub(crate) trait CanvasContext {
type ID;
fn context_id(&self) -> Self::ID;
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas>;
fn resize(&self);
// Resets the backing bitmap (to transparent or opaque black) without the
// context state reset.
// Used by OffscreenCanvas.transferToImageBitmap.
fn reset_bitmap(&self);
/// Returns none if area of canvas is zero.
///
/// In case of other errors it returns cleared snapshot
fn get_image_data(&self) -> Option<Snapshot>;
fn origin_is_clean(&self) -> bool {
true
}
fn size(&self) -> Size2D<u32> {
self.canvas()
.map(|canvas| canvas.size())
.unwrap_or_default()
}
fn mark_as_dirty(&self) {
if let Some(HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas)) = &self.canvas()
{
canvas.upcast::<Node>().dirty(NodeDamage::Other);
}
}
/// The WebRender [`ImageKey`] of this [`CanvasContext`] if any.
fn image_key(&self) -> Option<ImageKey>;
/// Request that the [`CanvasContext`] update the rendering of its contents,
/// returning `true` if new image was produced.
fn update_rendering(&self, _canvas_epoch: Epoch) -> bool {
false
}
fn onscreen(&self) -> bool {
let Some(canvas) = self.canvas() else {
return false;
};
match canvas {
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(ref canvas) => {
canvas.upcast::<Node>().is_connected()
},
// FIXME(34628): Offscreen canvases should be considered offscreen if a placeholder is set.
// <https://www.w3.org/TR/webgpu/#abstract-opdef-updating-the-rendering-of-a-webgpu-canvas>
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(_) => false,
}
}
}
pub(crate) trait CanvasHelpers {
fn size(&self) -> Size2D<u32>;
fn canvas(&self) -> Option<DomRoot<HTMLCanvasElement>>;
}
impl CanvasHelpers for HTMLCanvasElementOrOffscreenCanvas {
fn size(&self) -> Size2D<u32> {
match self {
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => {
canvas.get_size().cast()
},
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => canvas.get_size(),
}
}
fn canvas(&self) -> Option<DomRoot<HTMLCanvasElement>> {
match self {
HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => Some(canvas.clone()),
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => canvas.placeholder(),
}
}
}
/// Non rooted variant of [`crate::dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::RenderingContext`]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(Clone, JSTraceable, MallocSizeOf)]
pub(crate) enum RenderingContext {
Placeholder(Dom<OffscreenCanvas>),
Context2d(Dom<CanvasRenderingContext2D>),
BitmapRenderer(Dom<ImageBitmapRenderingContext>),
WebGL(Dom<WebGLRenderingContext>),
WebGL2(Dom<WebGL2RenderingContext>),
#[cfg(feature = "webgpu")]
WebGPU(Dom<GPUCanvasContext>),
}
impl CanvasContext for RenderingContext {
type ID = ();
fn context_id(&self) -> Self::ID {}
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
match self {
RenderingContext::Placeholder(offscreen_canvas) => offscreen_canvas.context()?.canvas(),
RenderingContext::Context2d(context) => context.canvas(),
RenderingContext::BitmapRenderer(context) => context.canvas(),
RenderingContext::WebGL(context) => context.canvas(),
RenderingContext::WebGL2(context) => context.canvas(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.canvas(),
}
}
fn resize(&self) {
match self {
RenderingContext::Placeholder(offscreen_canvas) => {
if let Some(context) = offscreen_canvas.context() {
context.resize()
}
},
RenderingContext::Context2d(context) => context.resize(),
RenderingContext::BitmapRenderer(context) => context.resize(),
RenderingContext::WebGL(context) => context.resize(),
RenderingContext::WebGL2(context) => context.resize(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.resize(),
}
}
fn reset_bitmap(&self) {
match self {
RenderingContext::Placeholder(offscreen_canvas) => {
if let Some(context) = offscreen_canvas.context() {
context.reset_bitmap()
}
},
RenderingContext::Context2d(context) => context.reset_bitmap(),
RenderingContext::BitmapRenderer(context) => context.reset_bitmap(),
RenderingContext::WebGL(context) => context.reset_bitmap(),
RenderingContext::WebGL2(context) => context.reset_bitmap(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.reset_bitmap(),
}
}
fn get_image_data(&self) -> Option<Snapshot> {
match self {
RenderingContext::Placeholder(offscreen_canvas) => {
offscreen_canvas.context()?.get_image_data()
},
RenderingContext::Context2d(context) => context.get_image_data(),
RenderingContext::BitmapRenderer(context) => context.get_image_data(),
RenderingContext::WebGL(context) => context.get_image_data(),
RenderingContext::WebGL2(context) => context.get_image_data(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.get_image_data(),
}
}
fn origin_is_clean(&self) -> bool {
match self {
RenderingContext::Placeholder(offscreen_canvas) => offscreen_canvas
.context()
.is_none_or(|context| context.origin_is_clean()),
RenderingContext::Context2d(context) => context.origin_is_clean(),
RenderingContext::BitmapRenderer(context) => context.origin_is_clean(),
RenderingContext::WebGL(context) => context.origin_is_clean(),
RenderingContext::WebGL2(context) => context.origin_is_clean(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.origin_is_clean(),
}
}
fn size(&self) -> Size2D<u32> {
match self {
RenderingContext::Placeholder(offscreen_canvas) => offscreen_canvas
.context()
.map(|context| context.size())
.unwrap_or_default(),
RenderingContext::Context2d(context) => context.size(),
RenderingContext::BitmapRenderer(context) => context.size(),
RenderingContext::WebGL(context) => context.size(),
RenderingContext::WebGL2(context) => context.size(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.size(),
}
}
fn mark_as_dirty(&self) {
match self {
RenderingContext::Placeholder(offscreen_canvas) => {
if let Some(context) = offscreen_canvas.context() {
context.mark_as_dirty()
}
},
RenderingContext::Context2d(context) => context.mark_as_dirty(),
RenderingContext::BitmapRenderer(context) => context.mark_as_dirty(),
RenderingContext::WebGL(context) => context.mark_as_dirty(),
RenderingContext::WebGL2(context) => context.mark_as_dirty(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.mark_as_dirty(),
}
}
fn image_key(&self) -> Option<ImageKey> {
match self {
RenderingContext::Placeholder(offscreen_canvas) => offscreen_canvas
.context()
.and_then(|context| context.image_key()),
RenderingContext::Context2d(context) => context.image_key(),
RenderingContext::BitmapRenderer(context) => context.image_key(),
RenderingContext::WebGL(context) => context.image_key(),
RenderingContext::WebGL2(context) => context.image_key(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.image_key(),
}
}
fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
match self {
RenderingContext::Placeholder(offscreen_canvas) => offscreen_canvas
.context()
.is_some_and(|context| context.update_rendering(canvas_epoch)),
RenderingContext::Context2d(context) => context.update_rendering(canvas_epoch),
RenderingContext::BitmapRenderer(context) => context.update_rendering(canvas_epoch),
RenderingContext::WebGL(context) => context.update_rendering(canvas_epoch),
RenderingContext::WebGL2(context) => context.update_rendering(canvas_epoch),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.update_rendering(canvas_epoch),
}
}
fn onscreen(&self) -> bool {
match self {
RenderingContext::Placeholder(offscreen_canvas) => offscreen_canvas
.context()
.is_some_and(|context| context.onscreen()),
RenderingContext::Context2d(context) => context.onscreen(),
RenderingContext::BitmapRenderer(context) => context.onscreen(),
RenderingContext::WebGL(context) => context.onscreen(),
RenderingContext::WebGL2(context) => context.onscreen(),
#[cfg(feature = "webgpu")]
RenderingContext::WebGPU(context) => context.onscreen(),
}
}
}
/// Non rooted variant of [`crate::dom::bindings::codegen::Bindings::OffscreenCanvasBinding::OffscreenRenderingContext`]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(Clone, JSTraceable, MallocSizeOf)]
pub(crate) enum OffscreenRenderingContext {
Context2d(Dom<OffscreenCanvasRenderingContext2D>),
BitmapRenderer(Dom<ImageBitmapRenderingContext>),
// WebGL(Dom<WebGLRenderingContext>),
// WebGL2(Dom<WebGL2RenderingContext>),
// #[cfg(feature = "webgpu")]
// WebGPU(Dom<GPUCanvasContext>),
Detached,
}
impl CanvasContext for OffscreenRenderingContext {
type ID = ();
fn context_id(&self) -> Self::ID {}
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
match self {
OffscreenRenderingContext::Context2d(context) => context.canvas(),
OffscreenRenderingContext::BitmapRenderer(context) => context.canvas(),
OffscreenRenderingContext::Detached => None,
}
}
fn resize(&self) {
match self {
OffscreenRenderingContext::Context2d(context) => context.resize(),
OffscreenRenderingContext::BitmapRenderer(context) => context.resize(),
OffscreenRenderingContext::Detached => {},
}
}
fn reset_bitmap(&self) {
match self {
OffscreenRenderingContext::Context2d(context) => context.reset_bitmap(),
OffscreenRenderingContext::BitmapRenderer(context) => context.reset_bitmap(),
OffscreenRenderingContext::Detached => {},
}
}
fn get_image_data(&self) -> Option<Snapshot> {
match self {
OffscreenRenderingContext::Context2d(context) => context.get_image_data(),
OffscreenRenderingContext::BitmapRenderer(context) => context.get_image_data(),
OffscreenRenderingContext::Detached => None,
}
}
fn origin_is_clean(&self) -> bool {
match self {
OffscreenRenderingContext::Context2d(context) => context.origin_is_clean(),
OffscreenRenderingContext::BitmapRenderer(context) => context.origin_is_clean(),
OffscreenRenderingContext::Detached => true,
}
}
fn size(&self) -> Size2D<u32> {
match self {
OffscreenRenderingContext::Context2d(context) => context.size(),
OffscreenRenderingContext::BitmapRenderer(context) => context.size(),
OffscreenRenderingContext::Detached => Size2D::default(),
}
}
fn mark_as_dirty(&self) {
match self {
OffscreenRenderingContext::Context2d(context) => context.mark_as_dirty(),
OffscreenRenderingContext::BitmapRenderer(context) => context.mark_as_dirty(),
OffscreenRenderingContext::Detached => {},
}
}
fn image_key(&self) -> Option<ImageKey> {
None
}
fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
match self {
OffscreenRenderingContext::Context2d(context) => context.update_rendering(canvas_epoch),
OffscreenRenderingContext::BitmapRenderer(context) => {
context.update_rendering(canvas_epoch)
},
OffscreenRenderingContext::Detached => false,
}
}
fn onscreen(&self) -> bool {
match self {
OffscreenRenderingContext::Context2d(context) => context.onscreen(),
OffscreenRenderingContext::BitmapRenderer(context) => context.onscreen(),
OffscreenRenderingContext::Detached => false,
}
}
}

View file

@ -0,0 +1,769 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::{Cell, Ref};
use std::collections::HashMap;
use std::rc::Rc;
use base::id::{ImageBitmapId, ImageBitmapIndex};
use constellation_traits::SerializableImageBitmap;
use dom_struct::dom_struct;
use euclid::default::{Point2D, Rect, Size2D};
use pixels::{CorsStatus, PixelFormat, Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use script_bindings::error::{Error, Fallible};
use script_bindings::realms::{AlreadyInRealm, InRealm};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapMethods, ImageBitmapOptions, ImageBitmapSource, ImageOrientation, PremultiplyAlpha,
ResizeQuality,
};
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::Serializable;
use crate::dom::bindings::structuredclone::StructuredData;
use crate::dom::bindings::transferable::Transferable;
use crate::dom::globalscope::GlobalScope;
use crate::dom::types::Promise;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct ImageBitmap {
reflector_: Reflector,
/// The actual pixel data of the bitmap
///
/// If this is `None`, then the bitmap data has been released by calling
/// [`close`](https://html.spec.whatwg.org/multipage/#dom-imagebitmap-close)
#[no_trace]
bitmap_data: DomRefCell<Option<Snapshot>>,
origin_clean: Cell<bool>,
}
impl ImageBitmap {
fn new_inherited(bitmap_data: Snapshot) -> ImageBitmap {
ImageBitmap {
reflector_: Reflector::new(),
bitmap_data: DomRefCell::new(Some(bitmap_data)),
origin_clean: Cell::new(true),
}
}
pub(crate) fn new(
global: &GlobalScope,
bitmap_data: Snapshot,
can_gc: CanGc,
) -> DomRoot<ImageBitmap> {
reflect_dom_object(
Box::new(ImageBitmap::new_inherited(bitmap_data)),
global,
can_gc,
)
}
#[allow(dead_code)]
pub(crate) fn bitmap_data(&self) -> Ref<'_, Option<Snapshot>> {
self.bitmap_data.borrow()
}
pub(crate) fn origin_is_clean(&self) -> bool {
self.origin_clean.get()
}
pub(crate) fn set_origin_clean(&self, origin_is_clean: bool) {
self.origin_clean.set(origin_is_clean);
}
/// Return the value of the [`[[Detached]]`](https://html.spec.whatwg.org/multipage/#detached)
/// internal slot
pub(crate) fn is_detached(&self) -> bool {
self.bitmap_data.borrow().is_none()
}
/// <https://html.spec.whatwg.org/multipage/#cropped-to-the-source-rectangle-with-formatting>
pub(crate) fn crop_and_transform_bitmap_data(
input: Snapshot,
mut sx: i32,
mut sy: i32,
sw: Option<i32>,
sh: Option<i32>,
options: &ImageBitmapOptions,
) -> Option<Snapshot> {
let input_size = input.size().to_i32();
// Step 2. If sx, sy, sw and sh are specified, let sourceRectangle be a rectangle whose corners
// are the four points (sx, sy), (sx+sw, sy), (sx+sw, sy+sh), (sx, sy+sh). Otherwise,
// let sourceRectangle be a rectangle whose corners are the four points (0, 0), (width of input, 0),
// (width of input, height of input), (0, height of input). If either sw or sh are negative,
// then the top-left corner of this rectangle will be to the left or above the (sx, sy) point.
let sw = sw.map_or(input_size.width, |width| {
if width < 0 {
sx = sx.saturating_add(width);
width.saturating_abs()
} else {
width
}
});
let sh = sh.map_or(input_size.height, |height| {
if height < 0 {
sy = sy.saturating_add(height);
height.saturating_abs()
} else {
height
}
});
let source_rect = Rect::new(Point2D::new(sx, sy), Size2D::new(sw, sh));
// Whether the byte length of the source bitmap exceeds the supported range.
// In the case the source is too large, we should fail, and that is not defined.
// <https://github.com/whatwg/html/issues/3323>
let Some(source_byte_length) = pixels::compute_rgba8_byte_length_if_within_limit(
source_rect.size.width as usize,
source_rect.size.height as usize,
) else {
log::warn!(
"Failed to allocate bitmap of size {:?}, too large",
source_rect.size
);
return None;
};
// Step 3. Let outputWidth be determined as follows:
// Step 4. Let outputHeight be determined as follows:
let output_size = match (options.resizeWidth, options.resizeHeight) {
(Some(width), Some(height)) => Size2D::new(width, height),
(Some(width), None) => {
let height =
source_rect.size.height as f64 * width as f64 / source_rect.size.width as f64;
Size2D::new(width, height.round() as u32)
},
(None, Some(height)) => {
let width =
source_rect.size.width as f64 * height as f64 / source_rect.size.height as f64;
Size2D::new(width.round() as u32, height)
},
(None, None) => source_rect.size.to_u32(),
};
// Whether the byte length of the output bitmap exceeds the supported range.
// In the case the output is too large, we should fail, and that is not defined.
// <https://github.com/whatwg/html/issues/3323>
let Some(output_byte_length) = pixels::compute_rgba8_byte_length_if_within_limit(
output_size.width as usize,
output_size.height as usize,
) else {
log::warn!(
"Failed to allocate bitmap of size {:?}, too large",
output_size
);
return None;
};
// TODO: Take into account the image orientation (such as EXIF metadata).
// Step 5. Place input on an infinite transparent black grid plane, positioned so that
// its top left corner is at the origin of the plane, with the x-coordinate increasing to the right,
// and the y-coordinate increasing down, and with each pixel in the input image data occupying a cell
// on the plane's grid.
let input_rect = Rect::new(Point2D::zero(), input_size);
let input_rect_cropped = source_rect
.intersection(&input_rect)
.unwrap_or(Rect::zero());
// Early out for empty tranformations.
if input_rect_cropped.is_empty() {
return Some(Snapshot::cleared(output_size));
}
// Step 6. Let output be the rectangle on the plane denoted by sourceRectangle.
let mut source: Snapshot = Snapshot::from_vec(
source_rect.size.cast(),
input.format(),
input.alpha_mode(),
vec![0; source_byte_length],
);
let source_rect_cropped = Rect::new(
Point2D::new(
input_rect_cropped.origin.x - source_rect.origin.x,
input_rect_cropped.origin.y - source_rect.origin.y,
),
input_rect_cropped.size,
);
pixels::copy_rgba8_image(
input.size(),
input_rect_cropped.cast(),
input.as_raw_bytes(),
source.size(),
source_rect_cropped.cast(),
source.as_raw_bytes_mut(),
);
// Step 7. Scale output to the size specified by outputWidth and outputHeight.
let mut output = if source.size() != output_size {
let quality = match options.resizeQuality {
ResizeQuality::Pixelated => pixels::FilterQuality::None,
ResizeQuality::Low => pixels::FilterQuality::Low,
ResizeQuality::Medium => pixels::FilterQuality::Medium,
ResizeQuality::High => pixels::FilterQuality::High,
};
let Some(output_data) = pixels::scale_rgba8_image(
source.size(),
source.as_raw_bytes(),
output_size,
quality,
) else {
log::warn!(
"Failed to scale the bitmap of size {:?} to required size {:?}",
source.size(),
output_size
);
return None;
};
debug_assert_eq!(output_data.len(), output_byte_length);
Snapshot::from_vec(
output_size,
source.format(),
source.alpha_mode(),
output_data,
)
} else {
source
};
// Step 8. If the value of the imageOrientation member of options is "flipY",
// output must be flipped vertically, disregarding any image orientation metadata
// of the source (such as EXIF metadata), if any.
if options.imageOrientation == ImageOrientation::FlipY {
pixels::flip_y_rgba8_image_inplace(output.size(), output.as_raw_bytes_mut());
}
// TODO: Step 9. If image is an img element or a Blob object, let val be the value
// of the colorSpaceConversion member of options, and then run these substeps:
// Step 10. Let val be the value of premultiplyAlpha member of options,
// and then run these substeps:
// TODO: Preserve the original input pixel format and perform conversion on demand.
match options.premultiplyAlpha {
PremultiplyAlpha::Default | PremultiplyAlpha::Premultiply => {
output.transform(
SnapshotAlphaMode::Transparent {
premultiplied: true,
},
SnapshotPixelFormat::BGRA,
);
},
PremultiplyAlpha::None => {
output.transform(
SnapshotAlphaMode::Transparent {
premultiplied: false,
},
SnapshotPixelFormat::BGRA,
);
},
}
// Step 11. Return output.
Some(output)
}
/// <https://html.spec.whatwg.org/multipage/#dom-createimagebitmap>
#[allow(clippy::too_many_arguments)]
pub(crate) fn create_image_bitmap(
global_scope: &GlobalScope,
image: ImageBitmapSource,
sx: i32,
sy: i32,
sw: Option<i32>,
sh: Option<i32>,
options: &ImageBitmapOptions,
can_gc: CanGc,
) -> Rc<Promise> {
let in_realm_proof = AlreadyInRealm::assert::<crate::DomTypeHolder>();
let p = Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), can_gc);
// Step 1. If either sw or sh is given and is 0, then return a promise rejected with a RangeError.
if sw.is_some_and(|w| w == 0) {
p.reject_error(
Error::Range("'sw' must be a non-zero value".to_owned()),
can_gc,
);
return p;
}
if sh.is_some_and(|h| h == 0) {
p.reject_error(
Error::Range("'sh' must be a non-zero value".to_owned()),
can_gc,
);
return p;
}
// Step 2. If either options's resizeWidth or options's resizeHeight is present and is 0,
// then return a promise rejected with an "InvalidStateError" DOMException.
if options.resizeWidth.is_some_and(|w| w == 0) {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
if options.resizeHeight.is_some_and(|h| h == 0) {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// The promise with image bitmap should be fulfilled on the the bitmap task source.
let fullfill_promise_on_bitmap_task_source =
|promise: &Rc<Promise>, image_bitmap: &ImageBitmap| {
let trusted_promise = TrustedPromise::new(promise.clone());
let trusted_image_bitmap = Trusted::new(image_bitmap);
global_scope.task_manager().bitmap_task_source().queue(
task!(resolve_promise: move || {
let promise = trusted_promise.root();
let image_bitmap = trusted_image_bitmap.root();
promise.resolve_native(&image_bitmap, CanGc::note());
}),
);
};
// The promise with "InvalidStateError" DOMException should be rejected
// on the the bitmap task source.
let reject_promise_on_bitmap_task_source = |promise: &Rc<Promise>| {
let trusted_promise = TrustedPromise::new(promise.clone());
global_scope
.task_manager()
.bitmap_task_source()
.queue(task!(reject_promise: move || {
let promise = trusted_promise.root();
promise.reject_error(Error::InvalidState, CanGc::note());
}));
};
// Step 3. Check the usability of the image argument. If this throws an exception or returns bad,
// then return a promise rejected with an "InvalidStateError" DOMException.
// Step 6. Switch on image:
match image {
ImageBitmapSource::HTMLImageElement(ref image) => {
// <https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument>
if !image.is_usable().is_ok_and(|u| u) {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// If no ImageBitmap object can be constructed, then the promise
// is rejected instead.
let Some(snapshot) = image.get_raster_image_data() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
// Step 6.3. Set imageBitmap's bitmap data to a copy of image's media data,
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.4. If image is not origin-clean, then set the origin-clean flag
// of imageBitmap's bitmap to false.
image_bitmap.set_origin_clean(image.same_origin(GlobalScope::entry().origin()));
// Step 6.5. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::HTMLVideoElement(ref video) => {
// <https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument>
if !video.is_usable() {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// Step 6.1. If image's networkState attribute is NETWORK_EMPTY, then return
// a promise rejected with an "InvalidStateError" DOMException.
if video.is_network_state_empty() {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// If no ImageBitmap object can be constructed, then the promise is rejected instead.
let Some(snapshot) = video.get_current_frame_data() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
// Step 6.2. Set imageBitmap's bitmap data to a copy of the frame at the current
// playback position, at the media resource's natural width and natural height
// (i.e., after any aspect-ratio correction has been applied),
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.3. If image is not origin-clean, then set the origin-clean flag
// of imageBitmap's bitmap to false.
image_bitmap.set_origin_clean(video.origin_is_clean());
// Step 6.4. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::HTMLCanvasElement(ref canvas) => {
// <https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument>
if canvas.get_size().is_empty() {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// If no ImageBitmap object can be constructed, then the promise is rejected instead.
let Some(snapshot) = canvas.get_image_data() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
// Step 6.1. Set imageBitmap's bitmap data to a copy of image's bitmap data,
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.2. Set the origin-clean flag of the imageBitmap's bitmap to the same value
// as the origin-clean flag of image's bitmap.
image_bitmap.set_origin_clean(canvas.origin_is_clean());
// Step 6.3. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::ImageBitmap(ref bitmap) => {
// <https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument>
if bitmap.is_detached() {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// If no ImageBitmap object can be constructed, then the promise is rejected instead.
let Some(snapshot) = bitmap.bitmap_data().clone() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
// Step 6.1. Set imageBitmap's bitmap data to a copy of image's bitmap data,
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.2. Set the origin-clean flag of imageBitmap's bitmap to the same value
// as the origin-clean flag of image's bitmap.
image_bitmap.set_origin_clean(bitmap.origin_is_clean());
// Step 6.3. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::OffscreenCanvas(ref canvas) => {
// <https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument>
if canvas.get_size().is_empty() {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
// If no ImageBitmap object can be constructed, then the promise is rejected instead.
let Some(snapshot) = canvas.get_image_data() else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
// Step 6.1. Set imageBitmap's bitmap data to a copy of image's bitmap data,
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.2. Set the origin-clean flag of the imageBitmap's bitmap to the same value
// as the origin-clean flag of image's bitmap.
image_bitmap.set_origin_clean(canvas.origin_is_clean());
// Step 6.3. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::Blob(ref blob) => {
// Step 6.1. Let imageData be the result of reading image's data.
// If an error occurs during reading of the object, then queue
// a global task, using the bitmap task source, to reject promise
// with an "InvalidStateError" DOMException and abort these steps.
let Ok(bytes) = blob.get_bytes() else {
reject_promise_on_bitmap_task_source(&p);
return p;
};
// Step 6.2. Apply the image sniffing rules to determine the file
// format of imageData, with MIME type of image (as given by
// image's type attribute) giving the official type.
// Step 6.3. If imageData is not in a supported image file format
// (e.g., it's not an image at all), or if imageData is corrupted
// in some fatal way such that the image dimensions cannot be obtained
// (e.g., a vector graphic with no natural size), then queue
// a global task, using the bitmap task source, to reject promise
// with an "InvalidStateError" DOMException and abort these steps.
let Some(img) = pixels::load_from_memory(&bytes, CorsStatus::Safe) else {
reject_promise_on_bitmap_task_source(&p);
return p;
};
let size = Size2D::new(img.metadata.width, img.metadata.height);
let format = match img.format {
PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
pixel_format => {
unimplemented!("unsupported pixel format ({:?})", pixel_format)
},
};
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
let snapshot = Snapshot::from_vec(
size.cast(),
format,
alpha_mode,
img.first_frame().bytes.to_vec(),
);
// Step 6.4. Set imageBitmap's bitmap data to imageData, cropped
// to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
reject_promise_on_bitmap_task_source(&p);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.5. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::ImageData(ref image_data) => {
// Step 6.1. Let buffer be image's data attribute value's [[ViewedArrayBuffer]] internal slot.
// Step 6.2. If IsDetachedBuffer(buffer) is true, then return a promise rejected
// with an "InvalidStateError" DOMException.
if image_data.is_detached() {
p.reject_error(Error::InvalidState, can_gc);
return p;
}
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
let snapshot = Snapshot::from_vec(
image_data.get_size().cast(),
SnapshotPixelFormat::RGBA,
alpha_mode,
image_data.to_vec(),
);
// Step 6.3. Set imageBitmap's bitmap data to image's image data,
// cropped to the source rectangle with formatting.
let Some(bitmap_data) =
ImageBitmap::crop_and_transform_bitmap_data(snapshot, sx, sy, sw, sh, options)
else {
p.reject_error(Error::InvalidState, can_gc);
return p;
};
let image_bitmap = Self::new(global_scope, bitmap_data, can_gc);
// Step 6.4. Queue a global task, using the bitmap task source,
// to resolve promise with imageBitmap.
fullfill_promise_on_bitmap_task_source(&p, &image_bitmap);
},
ImageBitmapSource::CSSStyleValue(_) => {
// TODO: CSSStyleValue is not part of ImageBitmapSource
// <https://html.spec.whatwg.org/multipage/#imagebitmapsource>
p.reject_error(Error::NotSupported, can_gc);
},
}
// Step 7. Return promise.
p
}
}
impl Serializable for ImageBitmap {
type Index = ImageBitmapIndex;
type Data = SerializableImageBitmap;
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:serialization-steps>
fn serialize(&self) -> Result<(ImageBitmapId, Self::Data), ()> {
// <https://html.spec.whatwg.org/multipage/#structuredserializeinternal>
// Step 19.1. If value has a [[Detached]] internal slot whose value is
// true, then throw a "DataCloneError" DOMException.
if self.is_detached() {
return Err(());
}
// Step 1. If value's origin-clean flag is not set, then throw a
// "DataCloneError" DOMException.
if !self.origin_is_clean() {
return Err(());
}
// Step 2. Set serialized.[[BitmapData]] to a copy of value's bitmap data.
let serialized = SerializableImageBitmap {
bitmap_data: self.bitmap_data.borrow().clone().unwrap(),
};
Ok((ImageBitmapId::new(), serialized))
}
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:deserialization-steps>
fn deserialize(
owner: &GlobalScope,
serialized: Self::Data,
can_gc: CanGc,
) -> Result<DomRoot<Self>, ()> {
// Step 1. Set value's bitmap data to serialized.[[BitmapData]].
Ok(ImageBitmap::new(owner, serialized.bitmap_data, can_gc))
}
fn serialized_storage<'a>(
data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<ImageBitmapId, Self::Data>> {
match data {
StructuredData::Reader(r) => &mut r.image_bitmaps,
StructuredData::Writer(w) => &mut w.image_bitmaps,
}
}
}
impl Transferable for ImageBitmap {
type Index = ImageBitmapIndex;
type Data = SerializableImageBitmap;
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:transfer-steps>
fn transfer(&self) -> Fallible<(ImageBitmapId, SerializableImageBitmap)> {
// <https://html.spec.whatwg.org/multipage/#structuredserializewithtransfer>
// Step 5.2. If transferable has a [[Detached]] internal slot and
// transferable.[[Detached]] is true, then throw a "DataCloneError"
// DOMException.
if self.is_detached() {
return Err(Error::DataClone(None));
}
// Step 1. If value's origin-clean flag is not set, then throw a
// "DataCloneError" DOMException.
if !self.origin_is_clean() {
return Err(Error::DataClone(None));
}
// Step 2. Set dataHolder.[[BitmapData]] to value's bitmap data.
// Step 3. Unset value's bitmap data.
let transferred = SerializableImageBitmap {
bitmap_data: self.bitmap_data.borrow_mut().take().unwrap(),
};
Ok((ImageBitmapId::new(), transferred))
}
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:transfer-receiving-steps>
fn transfer_receive(
owner: &GlobalScope,
_: ImageBitmapId,
transferred: SerializableImageBitmap,
) -> Result<DomRoot<Self>, ()> {
// Step 1. Set value's bitmap data to serialized.[[BitmapData]].
Ok(ImageBitmap::new(
owner,
transferred.bitmap_data,
CanGc::note(),
))
}
fn serialized_storage<'a>(
data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<ImageBitmapId, Self::Data>> {
match data {
StructuredData::Reader(r) => &mut r.transferred_image_bitmaps,
StructuredData::Writer(w) => &mut w.transferred_image_bitmaps,
}
}
}
impl ImageBitmapMethods<crate::DomTypeHolder> for ImageBitmap {
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmap-height>
fn Height(&self) -> u32 {
// Step 1. If this's [[Detached]] internal slot's value is true, then return 0.
if self.is_detached() {
return 0;
}
// Step 2. Return this's height, in CSS pixels.
self.bitmap_data
.borrow()
.as_ref()
.unwrap()
.size()
.cast()
.height
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmap-width>
fn Width(&self) -> u32 {
// Step 1. If this's [[Detached]] internal slot's value is true, then return 0.
if self.is_detached() {
return 0;
}
// Step 2. Return this's width, in CSS pixels.
self.bitmap_data
.borrow()
.as_ref()
.unwrap()
.size()
.cast()
.width
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmap-close>
fn Close(&self) {
// Step 1. Set this's [[Detached]] internal slot value to true.
// Step 2. Unset this's bitmap data.
// NOTE: The existence of the bitmap data is the internal slot in our implementation
self.bitmap_data.borrow_mut().take();
}
}

View file

@ -0,0 +1,197 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use dom_struct::dom_struct;
use euclid::default::Size2D;
use pixels::Snapshot;
use webrender_api::ImageKey;
use crate::canvas_context::{CanvasContext, CanvasHelpers, LayoutCanvasRenderingContextHelpers};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::ImageBitmapMethods;
use crate::dom::bindings::codegen::Bindings::ImageBitmapRenderingContextBinding::ImageBitmapRenderingContextMethods;
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::{DomRoot, LayoutDom};
use crate::dom::globalscope::GlobalScope;
use crate::dom::imagebitmap::ImageBitmap;
use crate::script_runtime::CanGc;
/// <https://html.spec.whatwg.org/multipage/#imagebitmaprenderingcontext>
#[dom_struct]
pub(crate) struct ImageBitmapRenderingContext {
reflector_: Reflector,
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmaprenderingcontext-canvas>
canvas: HTMLCanvasElementOrOffscreenCanvas,
/// Represents both the [output bitmap] and the [bitmap mode] of the context.
/// <https://html.spec.whatwg.org/multipage/#concept-imagebitmaprenderingcontext-output-bitmap>
/// <https://html.spec.whatwg.org/multipage/#concept-imagebitmaprenderingcontext-bitmap-mode>
#[no_trace]
bitmap: DomRefCell<Option<Snapshot>>,
origin_clean: Cell<bool>,
}
impl ImageBitmapRenderingContext {
/// <https://html.spec.whatwg.org/multipage/#imagebitmaprenderingcontext-creation-algorithm>
fn new_inherited(canvas: HTMLCanvasElementOrOffscreenCanvas) -> ImageBitmapRenderingContext {
ImageBitmapRenderingContext {
reflector_: Reflector::new(),
canvas,
bitmap: DomRefCell::new(None),
origin_clean: Cell::new(true),
}
}
pub(crate) fn new(
global: &GlobalScope,
canvas: HTMLCanvasElementOrOffscreenCanvas,
can_gc: CanGc,
) -> DomRoot<ImageBitmapRenderingContext> {
reflect_dom_object(
Box::new(ImageBitmapRenderingContext::new_inherited(canvas)),
global,
can_gc,
)
}
/// <https://html.spec.whatwg.org/multipage/#set-an-imagebitmaprenderingcontext's-output-bitmap>
fn set_bitmap(&self, image_bitmap: Option<&ImageBitmap>) {
match image_bitmap {
Some(image_bitmap) => {
// Step 2.1. Set context's bitmap mode to valid.
// Step 2.2. Set context's output bitmap to refer to the same
// underlying bitmap data as bitmap, without making a copy.
*self.bitmap.borrow_mut() = image_bitmap.bitmap_data().clone();
// The origin-clean flag of bitmap is included in the bitmap
// data to be referenced by context's output bitmap.
self.origin_clean.set(image_bitmap.origin_is_clean());
},
None => {
// Step 1.1. Set context's bitmap mode to blank.
// Step 1.2. Let canvas be the canvas element to which context is bound.
// Step 1.3. Set context's output bitmap to be transparent black
// with a natural width equal to the numeric value of canvas's
// width attribute and a natural height equal to the numeric
// value of canvas's height attribute, those values being
// interpreted in CSS pixels.
*self.bitmap.borrow_mut() = None;
// Step 1.4. Set the output bitmap's origin-clean flag to true.
self.origin_clean.set(true);
},
}
}
}
impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, ImageBitmapRenderingContext> {
fn canvas_data_source(self) -> Option<ImageKey> {
None
}
}
impl CanvasContext for ImageBitmapRenderingContext {
type ID = ();
fn context_id(&self) -> Self::ID {}
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
Some(self.canvas.clone())
}
/// <https://html.spec.whatwg.org/multipage/#the-canvas-element:concept-canvas-bitmaprenderer>
fn resize(&self) {
// The absence of the bitmap is the context's blank bitmap mode so the
// steps to set output bitmap could be omitted.
}
fn reset_bitmap(&self) {
// The newly created bitmap should be of the same dimensions as the
// previous bitmap if the context's bitmap mode is valid.
if self.bitmap.borrow().is_none() {
return;
}
let size = self.bitmap.borrow().as_ref().unwrap().size();
*self.bitmap.borrow_mut() = Some(Snapshot::cleared(size));
}
fn get_image_data(&self) -> Option<Snapshot> {
match self.bitmap.borrow().as_ref() {
Some(bitmap) => Some(bitmap.clone()),
None => {
let size = self.canvas.size();
if size.is_empty() ||
pixels::compute_rgba8_byte_length_if_within_limit(
size.width as usize,
size.height as usize,
)
.is_none()
{
None
} else {
Some(Snapshot::cleared(size))
}
},
}
}
fn origin_is_clean(&self) -> bool {
self.origin_clean.get()
}
fn size(&self) -> Size2D<u32> {
self.bitmap
.borrow()
.as_ref()
.map_or_else(|| self.canvas.size(), |bitmap| bitmap.size())
}
fn image_key(&self) -> Option<ImageKey> {
None
}
}
impl ImageBitmapRenderingContextMethods<crate::DomTypeHolder> for ImageBitmapRenderingContext {
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmaprenderingcontext-canvas>
fn Canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
self.canvas.clone()
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmaprenderingcontext-transferfromimagebitmap>
fn TransferFromImageBitmap(&self, image_bitmap: Option<&ImageBitmap>) -> Fallible<()> {
let Some(image_bitmap) = image_bitmap else {
// Step 2. If bitmap is null, then run the steps to set an
// ImageBitmapRenderingContext's output bitmap, with
// bitmapContext as the context argument and no bitmap argument,
// then return.
self.set_bitmap(None);
return Ok(());
};
// Step 3. If the value of bitmap's [[Detached]] internal slot
// is set to true, then throw an "InvalidStateError"
// DOMException.
if image_bitmap.is_detached() {
return Err(Error::InvalidState);
}
// Step 4. Run the steps to set an ImageBitmapRenderingContext's
// output bitmap, with the context argument equal to
// bitmapContext, and the bitmap argument referring to bitmap's
// underlying bitmap data.
self.set_bitmap(Some(image_bitmap));
// Step 5. Set the value of bitmap's [[Detached]] internal slot
// to true.
// Step 6. Unset bitmap's bitmap data.
image_bitmap.Close();
Ok(())
}
}

View file

@ -0,0 +1,311 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::borrow::Cow;
use std::vec::Vec;
use dom_struct::dom_struct;
use euclid::default::{Rect, Size2D};
use ipc_channel::ipc::IpcSharedMemory;
use js::gc::CustomAutoRooterGuard;
use js::jsapi::JSObject;
use js::rust::HandleObject;
use js::typedarray::{ClampedU8, Uint8ClampedArray};
use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use crate::dom::bindings::buffer_source::{
HeapBufferSource, create_buffer_source, create_heap_buffer_source_with_length,
};
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
ImageDataMethods, ImageDataPixelFormat, ImageDataSettings, PredefinedColorSpace,
};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::{CanGc, JSContext};
#[dom_struct]
pub(crate) struct ImageData {
reflector_: Reflector,
width: u32,
height: u32,
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
#[ignore_malloc_size_of = "mozjs"]
data: HeapBufferSource<ClampedU8>,
pixel_format: ImageDataPixelFormat,
color_space: PredefinedColorSpace,
}
impl ImageData {
#[allow(unsafe_code)]
pub(crate) fn new(
global: &GlobalScope,
width: u32,
height: u32,
mut data: Option<Vec<u8>>,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
let len =
pixels::compute_rgba8_byte_length_if_within_limit(width as usize, height as usize)
.ok_or(Error::Range(
"The requested image size exceeds the supported range".to_owned(),
))?;
let settings = ImageDataSettings {
colorSpace: Some(PredefinedColorSpace::Srgb),
pixelFormat: ImageDataPixelFormat::Rgba_unorm8,
};
if let Some(ref mut d) = data {
d.resize(len as usize, 0);
let cx = GlobalScope::get_cx();
rooted!(in (*cx) let mut js_object = std::ptr::null_mut::<JSObject>());
auto_root!(in(*cx) let data = create_buffer_source::<ClampedU8>(cx, &d[..], js_object.handle_mut(), can_gc)
.map_err(|_| Error::JSFailed)?);
Self::Constructor_(global, None, can_gc, data, width, Some(height), &settings)
} else {
Self::Constructor(global, None, can_gc, width, height, &settings)
}
}
#[allow(clippy::too_many_arguments)]
/// <https://html.spec.whatwg.org/multipage/#initialize-an-imagedata-object>
fn initialize(
pixels_per_row: u32,
rows: u32,
settings: &ImageDataSettings,
source: Option<CustomAutoRooterGuard<Uint8ClampedArray>>,
default_color_space: Option<PredefinedColorSpace>,
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
// 1. If source was given:
let data = if let Some(source) = source {
// 1. If settings["pixelFormat"] equals "rgba-unorm8" and source is not a Uint8ClampedArray,
// then throw an "InvalidStateError" DOMException.
// 2. If settings["pixelFormat"] is "rgba-float16" and source is not a Float16Array,
// then throw an "InvalidStateError" DOMException.
if !matches!(settings.pixelFormat, ImageDataPixelFormat::Rgba_unorm8) {
// we currently support only rgba-unorm8
return Err(Error::InvalidState);
}
// 3. Initialize the data attribute of imageData to source.
HeapBufferSource::<ClampedU8>::from_view(source)
} else {
// 2. Otherwise (source was not given):
match settings.pixelFormat {
ImageDataPixelFormat::Rgba_unorm8 => {
// 1. If settings["pixelFormat"] is "rgba-unorm8",
// then initialize the data attribute of imageData to a new Uint8ClampedArray object.
// The Uint8ClampedArray object must use a new ArrayBuffer for its storage,
// and must have a zero byte offset and byte length equal to the length of its storage, in bytes.
// The storage ArrayBuffer must have a length of 4 × rows × pixelsPerRow bytes.
// 3. If the storage ArrayBuffer could not be allocated,
// then rethrow the RangeError thrown by JavaScript, and return.
create_heap_buffer_source_with_length(
GlobalScope::get_cx(),
4 * rows * pixels_per_row,
can_gc,
)?
},
// 3. Otherwise, if settings["pixelFormat"] is "rgba-float16",
// then initialize the data attribute of imageData to a new Float16Array object.
// The Float16Array object must use a new ArrayBuffer for its storage,
// and must have a zero byte offset and byte length equal to the length of its storage, in bytes.
// The storage ArrayBuffer must have a length of 8 × rows × pixelsPerRow bytes.
// not implemented yet
}
};
// 3. Initialize the width attribute of imageData to pixelsPerRow.
let width = pixels_per_row;
// 4. Initialize the height attribute of imageData to rows.
let height = rows;
// 5. Initialize the pixelFormat attribute of imageData to settings["pixelFormat"].
let pixel_format = settings.pixelFormat;
// 6. If settings["colorSpace"] exists,
// then initialize the colorSpace attribute of imageData to settings["colorSpace"].
let color_space = settings
.colorSpace
// 7. Otherwise, if defaultColorSpace was given,
// then initialize the colorSpace attribute of imageData to defaultColorSpace.
.or(default_color_space)
// 8. Otherwise, initialize the colorSpace attribute of imageData to "srgb".
.unwrap_or(PredefinedColorSpace::Srgb);
Ok(reflect_dom_object_with_proto(
Box::new(ImageData {
reflector_: Reflector::new(),
width,
height,
data,
pixel_format,
color_space,
}),
global,
proto,
can_gc,
))
}
pub(crate) fn is_detached(&self) -> bool {
self.data.is_detached_buffer(GlobalScope::get_cx())
}
pub(crate) fn get_size(&self) -> Size2D<u32> {
Size2D::new(self.Width(), self.Height())
}
/// Nothing must change the array on the JS side while the slice is live.
#[allow(unsafe_code)]
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
assert!(self.data.is_initialized());
let internal_data = self
.data
.get_typed_array()
.expect("Failed to get Data from ImageData.");
// NOTE(nox): This is just as unsafe as `as_slice` itself even though we
// are extending the lifetime of the slice, because the data in
// this ImageData instance will never change. The method is thus unsafe
// because the array may be manipulated from JS while the reference
// is live.
unsafe {
let ptr: *const [u8] = internal_data.as_slice() as *const _;
&*ptr
}
}
/// Nothing must change the array on the JS side while the slice is live.
#[allow(unsafe_code)]
pub(crate) unsafe fn get_rect(&self, rect: Rect<u32>) -> Cow<'_, [u8]> {
pixels::rgba8_get_rect(unsafe { self.as_slice() }, self.get_size().to_u32(), rect)
}
#[allow(unsafe_code)]
pub(crate) fn get_snapshot_rect(&self, rect: Rect<u32>) -> Snapshot {
Snapshot::from_vec(
rect.size,
SnapshotPixelFormat::RGBA,
SnapshotAlphaMode::Transparent {
premultiplied: false,
},
unsafe { self.get_rect(rect).into_owned() },
)
}
#[allow(unsafe_code)]
pub(crate) fn to_shared_memory(&self) -> IpcSharedMemory {
// This is safe because we copy the slice content
IpcSharedMemory::from_bytes(unsafe { self.as_slice() })
}
#[allow(unsafe_code)]
pub(crate) fn to_vec(&self) -> Vec<u8> {
// This is safe because we copy the slice content
unsafe { self.as_slice() }.to_vec()
}
}
impl ImageDataMethods<crate::DomTypeHolder> for ImageData {
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata>
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
sw: u32,
sh: u32,
settings: &ImageDataSettings,
) -> Fallible<DomRoot<Self>> {
// 1. If one or both of sw and sh are zero, then throw an "IndexSizeError" DOMException.
if sw == 0 || sh == 0 {
return Err(Error::IndexSize);
}
// When a constructor is called for an ImageData that is too large, other browsers throw
// IndexSizeError rather than RangeError here, so we do the same.
pixels::compute_rgba8_byte_length_if_within_limit(sw as usize, sh as usize)
.ok_or(Error::IndexSize)?;
// 2. Initialize this given sw, sh, and settings.
// 3. Initialize the image data of this to transparent black.
Self::initialize(sw, sh, settings, None, None, global, proto, can_gc)
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-with-data>
fn Constructor_(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
data: CustomAutoRooterGuard<Uint8ClampedArray>,
sw: u32,
sh: Option<u32>,
settings: &ImageDataSettings,
) -> Fallible<DomRoot<Self>> {
// 1. Let bytesPerPixel be 4 if settings["pixelFormat"] is "rgba-unorm8"; otherwise 8.
let bytes_per_pixel = match settings.pixelFormat {
ImageDataPixelFormat::Rgba_unorm8 => 4,
};
// 2. Let length be the buffer source byte length of data.
let length = data.len();
if length == 0 {
return Err(Error::InvalidState);
}
// 3. If length is not a nonzero integral multiple of bytesPerPixel,
// then throw an "InvalidStateError" DOMException.
if length % bytes_per_pixel != 0 {
return Err(Error::InvalidState);
}
// 4. Let length be length divided by bytesPerPixel.
let length = length / bytes_per_pixel;
// 5. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
if sw == 0 || length % sw as usize != 0 {
return Err(Error::IndexSize);
}
// 6. Let height be length divided by sw.
let height = length / sw as usize;
// 7. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
if sh.is_some_and(|x| height != x as usize) {
return Err(Error::IndexSize);
}
// 8. Initialize this given sw, sh, settings, and source set to data.
Self::initialize(
sw,
height as u32,
settings,
Some(data),
None,
global,
proto,
can_gc,
)
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-width>
fn Width(&self) -> u32 {
self.width
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-height>
fn Height(&self) -> u32 {
self.height
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-data>
fn GetData(&self, _: JSContext) -> Fallible<Uint8ClampedArray> {
self.data.get_typed_array().map_err(|_| Error::JSFailed)
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-pixelformat>
fn PixelFormat(&self) -> ImageDataPixelFormat {
self.pixel_format
}
/// <https://html.spec.whatwg.org/multipage/#dom-imagedata-colorspace>
fn ColorSpace(&self) -> PredefinedColorSpace {
self.color_space
}
}

View file

@ -0,0 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#[path = "2d/mod.rs"]
mod canvas2d;
pub(crate) use canvas2d::*;
pub(crate) mod canvas_context;
pub(crate) mod imagebitmap;
pub(crate) mod imagebitmaprenderingcontext;
pub(crate) mod imagedata;
pub(crate) mod offscreencanvas;

View file

@ -0,0 +1,466 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::collections::HashMap;
use std::rc::Rc;
use base::id::{OffscreenCanvasId, OffscreenCanvasIndex};
use constellation_traits::{BlobImpl, TransferableOffscreenCanvas};
use dom_struct::dom_struct;
use euclid::default::Size2D;
use js::rust::{HandleObject, HandleValue};
use pixels::{EncodedImageType, Snapshot};
use script_bindings::weakref::WeakRef;
use crate::canvas_context::{CanvasContext, OffscreenRenderingContext};
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasBinding::{
ImageEncodeOptions, OffscreenCanvasMethods,
OffscreenRenderingContext as RootedOffscreenRenderingContext,
};
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::structuredclone::StructuredData;
use crate::dom::bindings::transferable::Transferable;
use crate::dom::blob::Blob;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::imagebitmaprenderingcontext::ImageBitmapRenderingContext;
use crate::dom::offscreencanvasrenderingcontext2d::OffscreenCanvasRenderingContext2D;
use crate::dom::promise::Promise;
use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::{CanGc, JSContext};
/// <https://html.spec.whatwg.org/multipage/#offscreencanvas>
#[dom_struct]
pub(crate) struct OffscreenCanvas {
eventtarget: EventTarget,
width: Cell<u64>,
height: Cell<u64>,
/// Represents both the [bitmap] and the [context mode] of the canvas.
///
/// [bitmap]: https://html.spec.whatwg.org/multipage/#offscreencanvas-bitmap
/// [context mode]: https://html.spec.whatwg.org/multipage/#offscreencanvas-context-mode
context: DomRefCell<Option<OffscreenRenderingContext>>,
/// <https://html.spec.whatwg.org/multipage/#offscreencanvas-placeholder>
placeholder: Option<WeakRef<HTMLCanvasElement>>,
}
impl OffscreenCanvas {
pub(crate) fn new_inherited(
width: u64,
height: u64,
placeholder: Option<WeakRef<HTMLCanvasElement>>,
) -> OffscreenCanvas {
OffscreenCanvas {
eventtarget: EventTarget::new_inherited(),
width: Cell::new(width),
height: Cell::new(height),
context: DomRefCell::new(None),
placeholder,
}
}
pub(crate) fn new(
global: &GlobalScope,
proto: Option<HandleObject>,
width: u64,
height: u64,
placeholder: Option<WeakRef<HTMLCanvasElement>>,
can_gc: CanGc,
) -> DomRoot<OffscreenCanvas> {
reflect_dom_object_with_proto(
Box::new(OffscreenCanvas::new_inherited(width, height, placeholder)),
global,
proto,
can_gc,
)
}
pub(crate) fn get_size(&self) -> Size2D<u32> {
Size2D::new(
self.Width().try_into().unwrap_or(u32::MAX),
self.Height().try_into().unwrap_or(u32::MAX),
)
}
pub(crate) fn origin_is_clean(&self) -> bool {
match *self.context.borrow() {
Some(ref context) => context.origin_is_clean(),
_ => true,
}
}
pub(crate) fn context(&self) -> Option<Ref<'_, OffscreenRenderingContext>> {
Ref::filter_map(self.context.borrow(), |ctx| ctx.as_ref()).ok()
}
pub(crate) fn get_image_data(&self) -> Option<Snapshot> {
match self.context.borrow().as_ref() {
Some(context) => context.get_image_data(),
None => {
let size = self.get_size();
if size.is_empty() ||
pixels::compute_rgba8_byte_length_if_within_limit(
size.width as usize,
size.height as usize,
)
.is_none()
{
None
} else {
Some(Snapshot::cleared(size))
}
},
}
}
pub(crate) fn get_or_init_2d_context(
&self,
can_gc: CanGc,
) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
if let Some(ctx) = self.context() {
return match *ctx {
OffscreenRenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
_ => None,
};
}
let context = OffscreenCanvasRenderingContext2D::new(&self.global(), self, can_gc)?;
*self.context.borrow_mut() = Some(OffscreenRenderingContext::Context2d(Dom::from_ref(
&*context,
)));
Some(context)
}
/// <https://html.spec.whatwg.org/multipage/#offscreen-context-type-bitmaprenderer>
pub(crate) fn get_or_init_bitmaprenderer_context(
&self,
can_gc: CanGc,
) -> Option<DomRoot<ImageBitmapRenderingContext>> {
// Return the same object as was returned the last time the method was
// invoked with this same first argument.
if let Some(ctx) = self.context() {
return match *ctx {
OffscreenRenderingContext::BitmapRenderer(ref ctx) => Some(DomRoot::from_ref(ctx)),
_ => None,
};
}
// Step 1. Let context be the result of running the
// ImageBitmapRenderingContext creation algorithm given this and
// options.
let context = ImageBitmapRenderingContext::new(
&self.global(),
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(DomRoot::from_ref(self)),
can_gc,
);
// Step 2. Set this's context mode to bitmaprenderer.
*self.context.borrow_mut() = Some(OffscreenRenderingContext::BitmapRenderer(
Dom::from_ref(&*context),
));
// Step 3. Return context.
Some(context)
}
pub(crate) fn placeholder(&self) -> Option<DomRoot<HTMLCanvasElement>> {
self.placeholder
.as_ref()
.and_then(|placeholder| placeholder.root())
}
}
impl Transferable for OffscreenCanvas {
type Index = OffscreenCanvasIndex;
type Data = TransferableOffscreenCanvas;
/// <https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transfer-steps>
fn transfer(&self) -> Fallible<(OffscreenCanvasId, TransferableOffscreenCanvas)> {
// <https://html.spec.whatwg.org/multipage/#structuredserializewithtransfer>
// Step 5.2. If transferable has a [[Detached]] internal slot and
// transferable.[[Detached]] is true, then throw a "DataCloneError"
// DOMException.
if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
return Err(Error::DataClone(None));
}
// Step 1. If value's context mode is not equal to none, then throw an
// "InvalidStateError" DOMException.
if !self.context.borrow().is_none() {
return Err(Error::InvalidState);
}
// TODO(#37882): Allow to transfer with a placeholder canvas element.
if self.placeholder.is_some() {
return Err(Error::InvalidState);
}
// Step 2. Set value's context mode to detached.
*self.context.borrow_mut() = Some(OffscreenRenderingContext::Detached);
// Step 3. Let width and height be the dimensions of value's bitmap.
// Step 5. Unset value's bitmap.
let width = self.width.replace(0);
let height = self.height.replace(0);
// TODO(#37918) Step 4. Let language and direction be the values of
// value's inherited language and inherited direction.
// Step 6. Set dataHolder.[[Width]] to width and dataHolder.[[Height]]
// to height.
// TODO(#37918) Step 7. Set dataHolder.[[Language]] to language and
// dataHolder.[[Direction]] to direction.
// TODO(#37882) Step 8. Set dataHolder.[[PlaceholderCanvas]] to be a
// weak reference to value's placeholder canvas element, if value has
// one, or null if it does not.
let transferred = TransferableOffscreenCanvas { width, height };
Ok((OffscreenCanvasId::new(), transferred))
}
/// <https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transfer-receiving-steps>
fn transfer_receive(
owner: &GlobalScope,
_: OffscreenCanvasId,
transferred: TransferableOffscreenCanvas,
) -> Result<DomRoot<Self>, ()> {
// Step 1. Initialize value's bitmap to a rectangular array of
// transparent black pixels with width given by dataHolder.[[Width]] and
// height given by dataHolder.[[Height]].
// TODO(#37918) Step 2. Set value's inherited language to
// dataHolder.[[Language]] and its inherited direction to
// dataHolder.[[Direction]].
// TODO(#37882) Step 3. If dataHolder.[[PlaceholderCanvas]] is not null,
// set value's placeholder canvas element to
// dataHolder.[[PlaceholderCanvas]] (while maintaining the weak
// reference semantics).
Ok(OffscreenCanvas::new(
owner,
None,
transferred.width,
transferred.height,
None,
CanGc::note(),
))
}
fn serialized_storage<'a>(
data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<OffscreenCanvasId, Self::Data>> {
match data {
StructuredData::Reader(r) => &mut r.offscreen_canvases,
StructuredData::Writer(w) => &mut w.offscreen_canvases,
}
}
}
impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas>
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
width: u64,
height: u64,
) -> Fallible<DomRoot<OffscreenCanvas>> {
Ok(OffscreenCanvas::new(
global, proto, width, height, None, can_gc,
))
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-getcontext>
fn GetContext(
&self,
_cx: JSContext,
id: DOMString,
_options: HandleValue,
can_gc: CanGc,
) -> Fallible<Option<RootedOffscreenRenderingContext>> {
// Step 3. Throw an "InvalidStateError" DOMException if the
// OffscreenCanvas object's context mode is detached.
if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
return Err(Error::InvalidState);
}
match &*id {
"2d" => Ok(self
.get_or_init_2d_context(can_gc)
.map(RootedOffscreenRenderingContext::OffscreenCanvasRenderingContext2D)),
"bitmaprenderer" => Ok(self
.get_or_init_bitmaprenderer_context(can_gc)
.map(RootedOffscreenRenderingContext::ImageBitmapRenderingContext)),
/*"webgl" | "experimental-webgl" => self
.get_or_init_webgl_context(cx, options)
.map(OffscreenRenderingContext::WebGLRenderingContext),
"webgl2" | "experimental-webgl2" => self
.get_or_init_webgl2_context(cx, options)
.map(OffscreenRenderingContext::WebGL2RenderingContext),*/
_ => Err(Error::Type(String::from(
"Unrecognized OffscreenCanvas context type",
))),
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-width>
fn Width(&self) -> u64 {
self.width.get()
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-width>
fn SetWidth(&self, value: u64, can_gc: CanGc) {
self.width.set(value);
if let Some(canvas_context) = self.context() {
canvas_context.resize();
}
if let Some(canvas) = self.placeholder() {
canvas.set_natural_width(value as _, can_gc)
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height>
fn Height(&self) -> u64 {
self.height.get()
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height>
fn SetHeight(&self, value: u64, can_gc: CanGc) {
self.height.set(value);
if let Some(canvas_context) = self.context() {
canvas_context.resize();
}
if let Some(canvas) = self.placeholder() {
canvas.set_natural_height(value as _, can_gc)
}
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-transfertoimagebitmap>
fn TransferToImageBitmap(&self, can_gc: CanGc) -> Fallible<DomRoot<ImageBitmap>> {
// Step 1. If the value of this OffscreenCanvas object's [[Detached]]
// internal slot is set to true, then throw an "InvalidStateError"
// DOMException.
if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
return Err(Error::InvalidState);
}
// Step 2. If this OffscreenCanvas object's context mode is set to none,
// then throw an "InvalidStateError" DOMException.
if self.context.borrow().is_none() {
return Err(Error::InvalidState);
}
// Step 3. Let image be a newly created ImageBitmap object that
// references the same underlying bitmap data as this OffscreenCanvas
// object's bitmap.
let Some(snapshot) = self.get_image_data() else {
return Err(Error::InvalidState);
};
let image_bitmap = ImageBitmap::new(&self.global(), snapshot, can_gc);
image_bitmap.set_origin_clean(self.origin_is_clean());
// Step 4. Set this OffscreenCanvas object's bitmap to reference a newly
// created bitmap of the same dimensions and color space as the previous
// bitmap, and with its pixels initialized to transparent black, or
// opaque black if the rendering context's alpha is false.
if let Some(canvas_context) = self.context() {
canvas_context.reset_bitmap();
}
// Step 5. Return image.
Ok(image_bitmap)
}
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-converttoblob>
fn ConvertToBlob(&self, options: &ImageEncodeOptions, can_gc: CanGc) -> Rc<Promise> {
// Step 5. Let result be a new promise object.
let in_realm_proof = AlreadyInRealm::assert::<crate::DomTypeHolder>();
let promise = Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), can_gc);
// Step 1. If the value of this's [[Detached]] internal slot is true,
// then return a promise rejected with an "InvalidStateError"
// DOMException.
if let Some(OffscreenRenderingContext::Detached) = *self.context.borrow() {
promise.reject_error(Error::InvalidState, can_gc);
return promise;
}
// Step 2. If this's context mode is 2d and the rendering context's
// output bitmap's origin-clean flag is set to false, then return a
// promise rejected with a "SecurityError" DOMException.
if !self.origin_is_clean() {
promise.reject_error(Error::Security, can_gc);
return promise;
}
// Step 3. If this's bitmap has no pixels (i.e., either its horizontal
// dimension or its vertical dimension is zero), then return a promise
// rejected with an "IndexSizeError" DOMException.
if self.Width() == 0 || self.Height() == 0 {
promise.reject_error(Error::IndexSize, can_gc);
return promise;
}
// Step 4. Let bitmap be a copy of this's bitmap.
let Some(mut snapshot) = self.get_image_data() else {
promise.reject_error(Error::InvalidState, can_gc);
return promise;
};
// Step 7. Run these steps in parallel:
// Step 7.1. Let file be a serialization of bitmap as a file, with
// options's type and quality if present.
// Step 7.2. Queue a global task on the canvas blob serialization task
// source given global to run these steps:
let trusted_this = Trusted::new(self);
let trusted_promise = TrustedPromise::new(promise.clone());
let image_type = EncodedImageType::from(options.type_.to_string());
let quality = options.quality;
self.global()
.task_manager()
.canvas_blob_task_source()
.queue(task!(convert_to_blob: move || {
let this = trusted_this.root();
let promise = trusted_promise.root();
let mut encoded: Vec<u8> = vec![];
if snapshot.encode_for_mime_type(&image_type, quality, &mut encoded).is_err() {
// Step 7.2.1. If file is null, then reject result with an
// "EncodingError" DOMException.
promise.reject_error(Error::Encoding, CanGc::note());
return;
};
// Step 7.2.2. Otherwise, resolve result with a new Blob object,
// created in global's relevant realm, representing file.
let blob_impl = BlobImpl::new_from_bytes(encoded, image_type.as_mime_type());
let blob = Blob::new(&this.global(), blob_impl, CanGc::note());
promise.resolve_native(&blob, CanGc::note());
}));
// Step 8. Return result.
promise
}
}