Skip to content

Commit

Permalink
Initial Steps OffScreenCanvas API
Browse files Browse the repository at this point in the history
  • Loading branch information
maharsh312 authored and jdm committed Jan 16, 2019
1 parent 7400219 commit f564b24
Show file tree
Hide file tree
Showing 1,437 changed files with 6,113 additions and 34 deletions.
2 changes: 2 additions & 0 deletions components/script/dom/mod.rs
Expand Up @@ -407,6 +407,8 @@ pub mod nodeiterator;
pub mod nodelist;
pub mod offlineaudiocompletionevent;
pub mod offlineaudiocontext;
pub mod offscreencanvas;
pub mod offscreencanvasrenderingcontext2d;
pub mod oscillatornode;
pub mod pagetransitionevent;
pub mod paintrenderingcontext2d;
Expand Down
146 changes: 146 additions & 0 deletions components/script/dom/offscreencanvas.rs
@@ -0,0 +1,146 @@
/* 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 http://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasBinding::{
OffscreenCanvasMethods, OffscreenRenderingContext, Wrap as OffscreenCanvasWrap,
};
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::offscreencanvasrenderingcontext2d::OffscreenCanvasRenderingContext2D;
use dom_struct::dom_struct;
use euclid::Size2D;
use js::jsapi::JSContext;
use js::rust::HandleValue;
use ref_filter_map;
use std::cell::Cell;
use std::cell::Ref;

#[must_root]
#[derive(Clone, JSTraceable, MallocSizeOf)]
pub enum OffscreenCanvasContext {
OffscreenContext2d(Dom<OffscreenCanvasRenderingContext2D>),
//WebGL(Dom<WebGLRenderingContext>),
//WebGL2(Dom<WebGL2RenderingContext>),
}

#[dom_struct]
pub struct OffscreenCanvas {
eventtarget: EventTarget,
height: Cell<u64>,
width: Cell<u64>,
context: DomRefCell<Option<OffscreenCanvasContext>>,
placeholder: Option<Dom<HTMLCanvasElement>>,
}

impl OffscreenCanvas {
pub fn new_inherited(
height: u64,
width: u64,
placeholder: Option<&HTMLCanvasElement>,
) -> OffscreenCanvas {
OffscreenCanvas {
eventtarget: EventTarget::new_inherited(),
height: Cell::new(height),
width: Cell::new(width),
context: DomRefCell::new(None),
placeholder: placeholder.map(Dom::from_ref),
}
}

pub fn new(
global: &GlobalScope,
height: u64,
width: u64,
placeholder: Option<&HTMLCanvasElement>,
) -> DomRoot<OffscreenCanvas> {
reflect_dom_object(
Box::new(OffscreenCanvas::new_inherited(height, width, placeholder)),
global,
OffscreenCanvasWrap,
)
}

pub fn Constructor(
global: &GlobalScope,
height: u64,
width: u64,
) -> Fallible<DomRoot<OffscreenCanvas>> {
let offscreencanvas = OffscreenCanvas::new(global, height, width, None);
Ok(offscreencanvas)
}

pub fn get_size(&self) -> Size2D<u64> {
Size2D::new(self.Width(), self.Height())
}

pub fn context(&self) -> Option<Ref<OffscreenCanvasContext>> {
ref_filter_map::ref_filter_map(self.context.borrow(), |ctx| ctx.as_ref())
}

#[allow(unsafe_code)]
fn get_or_init_2d_context(&self) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
if let Some(ctx) = self.context() {
return match *ctx {
OffscreenCanvasContext::OffscreenContext2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
};
}
let size = self.get_size();
let context = OffscreenCanvasRenderingContext2D::new(&self.global(), self, size);
*self.context.borrow_mut() = Some(OffscreenCanvasContext::OffscreenContext2d(
Dom::from_ref(&*context),
));
Some(context)
}
}

impl OffscreenCanvasMethods for OffscreenCanvas {
// https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-getcontext
#[allow(unsafe_code)]
unsafe fn GetContext(
&self,
_cx: *mut JSContext,
id: DOMString,
_options: HandleValue,
) -> Option<OffscreenRenderingContext> {
match &*id {
"2d" => self
.get_or_init_2d_context()
.map(OffscreenRenderingContext::OffscreenCanvasRenderingContext2D),
/*"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),*/
_ => None,
}
}

// https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-width
fn Width(&self) -> u64 {
return self.width.get();
}

// https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-width
fn SetWidth(&self, value: u64) {
self.width.set(value);
}

// https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height
fn Height(&self) -> u64 {
return self.height.get();
}

// https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-height
fn SetHeight(&self, value: u64) {
self.height.set(value);
}
}
55 changes: 55 additions & 0 deletions components/script/dom/offscreencanvasrenderingcontext2d.rs
@@ -0,0 +1,55 @@
/* 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 http://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding;
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding::OffscreenCanvasRenderingContext2DMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::dom::offscreencanvas::OffscreenCanvas;
use dom_struct::dom_struct;
use euclid::Size2D;

#[dom_struct]
pub struct OffscreenCanvasRenderingContext2D {
reflector_: Reflector,
canvas: Option<Dom<OffscreenCanvas>>,
}

impl OffscreenCanvasRenderingContext2D {
pub fn new_inherited(
_global: &GlobalScope,
canvas: Option<&OffscreenCanvas>,
_size: Size2D<u64>,
) -> OffscreenCanvasRenderingContext2D {
OffscreenCanvasRenderingContext2D {
reflector_: Reflector::new(),
canvas: canvas.map(Dom::from_ref),
}
}

pub fn new(
_global: &GlobalScope,
canvas: &OffscreenCanvas,
_size: Size2D<u64>,
) -> DomRoot<OffscreenCanvasRenderingContext2D> {
let boxed = Box::new(OffscreenCanvasRenderingContext2D::new_inherited(
_global,
Some(canvas),
_size,
));
reflect_dom_object(
boxed,
_global,
OffscreenCanvasRenderingContext2DBinding::Wrap,
)
}
}

impl OffscreenCanvasRenderingContext2DMethods for OffscreenCanvasRenderingContext2D {
// https://html.spec.whatwg.org/multipage/offscreencontext2d-canvas
fn Canvas(&self) -> DomRoot<OffscreenCanvas> {
DomRoot::from_ref(self.canvas.as_ref().expect("No canvas."))
}
}
25 changes: 25 additions & 0 deletions components/script/dom/webidls/OffscreenCanvas.webidl
@@ -0,0 +1,25 @@
/* 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 http://mozilla.org/MPL/2.0/. */

// https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface
typedef (OffscreenCanvasRenderingContext2D or WebGLRenderingContext or WebGL2RenderingContext)
OffscreenRenderingContext;

dictionary ImageEncodeOptions {
DOMString type = "image/png";
unrestricted double quality = 1.0;
};

//enum OffscreenRenderingContextId { "2d", "webgl", "webgl2" };

[Constructor([EnforceRange] unsigned long long width, [EnforceRange] unsigned long long height),
Exposed=(Window,Worker)/*, Transferable*/, Pref="dom.offscreen_canvas.enabled"]
interface OffscreenCanvas : EventTarget {
attribute /*[EnforceRange]*/ unsigned long long width;
attribute /*[EnforceRange]*/ unsigned long long height;

OffscreenRenderingContext? getContext(DOMString contextId, optional any options = null);
//ImageBitmap transferToImageBitmap();
//Promise<Blob> convertToBlob(optional ImageEncodeOptions options);
};
@@ -0,0 +1,26 @@
/* 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 http://mozilla.org/MPL/2.0/. */

// https://html.spec.whatwg.org/multipage/#the-offscreen-2d-rendering-context
[Exposed=(Window,Worker), Pref="dom.offscreen_canvas.enabled"]
interface OffscreenCanvasRenderingContext2D {
//void commit();
readonly attribute OffscreenCanvas canvas;
};

//OffscreenCanvasRenderingContext2D includes CanvasState;
//OffscreenCanvasRenderingContext2D includes CanvasTransform;
//OffscreenCanvasRenderingContext2D includes CanvasCompositing;
//OffscreenCanvasRenderingContext2D includes CanvasImageSmoothing;
//OffscreenCanvasRenderingContext2D includes CanvasFillStrokeStyles;
//OffscreenCanvasRenderingContext2D includes CanvasShadowStyles;
//OffscreenCanvasRenderingContext2D includes CanvasFilters;
//OffscreenCanvasRenderingContext2D includes CanvasRect;
//OffscreenCanvasRenderingContext2D includes CanvasDrawPath;
//OffscreenCanvasRenderingContext2D includes CanvasText;
//OffscreenCanvasRenderingContext2D includes CanvasDrawImage;
//OffscreenCanvasRenderingContext2D includes CanvasImageData;
//OffscreenCanvasRenderingContext2D includes CanvasPathDrawingStyles;
//OffscreenCanvasRenderingContext2D includes CanvasTextDrawingStyles;
//OffscreenCanvasRenderingContext2D includes CanvasPath;
1 change: 1 addition & 0 deletions resources/prefs.json
Expand Up @@ -9,6 +9,7 @@
"dom.microdata.testing.enabled": true,
"dom.mouseevent.which.enabled": false,
"dom.mutation_observer.enabled": true,
"dom.offscreen_canvas.enabled": false,
"dom.permissions.enabled": false,
"dom.permissions.testing.allowed_in_nonsecure_contexts": false,
"dom.serviceworker.timeout_seconds": 60,
Expand Down
2 changes: 2 additions & 0 deletions tests/wpt/include.ini
Expand Up @@ -97,6 +97,8 @@ skip: true
skip: false
[navigation-timing]
skip: false
[offscreen-canvas]
skip: false
[old-tests]
skip: true
[submission]
Expand Down

This file was deleted.

@@ -1,2 +1,2 @@
[abspos-float-with-inline-container.html]
expected: FAIL
expected: TIMEOUT
2 changes: 2 additions & 0 deletions tests/wpt/metadata/css/CSS2/text/white-space-002.xht.ini
@@ -0,0 +1,2 @@
[white-space-002.xht]
expected: FAIL
2 changes: 2 additions & 0 deletions tests/wpt/metadata/css/CSS2/visudet/line-height-204.html.ini
@@ -0,0 +1,2 @@
[line-height-204.html]
expected: FAIL
@@ -0,0 +1,2 @@
[mix-blend-mode-paragraph.html]
expected: FAIL
@@ -0,0 +1,2 @@
[background-repeat-round-roundup.xht]
expected: FAIL
Expand Up @@ -182,9 +182,6 @@
[Matching font-style: 'oblique 20deg' should prefer 'oblique 40deg 50deg' over 'oblique 10deg']
expected: FAIL

[Matching font-style: 'oblique 20deg' should prefer 'oblique 10deg' over 'italic']
expected: FAIL

[Matching font-style: 'oblique 20deg' should prefer 'oblique 0deg' over 'oblique -50deg -20deg']
expected: FAIL

Expand Down Expand Up @@ -248,9 +245,6 @@
[Matching font-style: 'oblique 21deg' should prefer 'oblique 21deg' over 'oblique 30deg 60deg']
expected: FAIL

[Matching font-style: 'oblique 21deg' should prefer 'oblique 40deg 50deg' over 'oblique 20deg']
expected: FAIL

[Matching font-style: 'oblique -10deg' should prefer 'oblique -5deg' over 'oblique -1deg 0deg']
expected: FAIL

Expand Down
Expand Up @@ -5,9 +5,6 @@
[Test @font-face matching for weight 470]
expected: FAIL

[Test @font-face matching for weight 500]
expected: FAIL

[Test @font-face matching for weight 600]
expected: FAIL

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -0,0 +1,2 @@
[word-break-keep-all-006.html]
expected: FAIL
Expand Up @@ -74,3 +74,6 @@
[opacity end]
expected: FAIL

[border-top-width end]
expected: FAIL

@@ -1,2 +1,2 @@
[parser-sets-attributes-and-children.html]
expected: TIMEOUT
expected: CRASH
2 changes: 1 addition & 1 deletion tests/wpt/metadata/encoding/single-byte-decoder.html.ini
Expand Up @@ -55,7 +55,7 @@
expected: FAIL

[windows-1252: iso_8859-1:1987 (XMLHttpRequest)]
expected: TIMEOUT
expected: FAIL

[windows-1254: iso_8859-9:1989 (XMLHttpRequest)]
expected: TIMEOUT
Expand Down

0 comments on commit f564b24

Please sign in to comment.