Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing createImageBitmap #26296

Merged
merged 1 commit into from Apr 30, 2020
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

implemented CreateImageBitmap function for Canvas image source

  • Loading branch information
JayalakshmiV authored and ramyananth committed Apr 30, 2020
commit ef6f99d8f52549e40cee25ead7fabdf04c5bcc54
@@ -5,6 +5,9 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::BroadcastChannelBinding::BroadcastChannelMethods;
use crate::dom::bindings::codegen::Bindings::EventSourceBinding::EventSourceBinding::EventSourceMethods;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapOptions, ImageBitmapSource,
};
use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionState;
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
@@ -31,6 +34,7 @@ use crate::dom::eventtarget::EventTarget;
use crate::dom::file::File;
use crate::dom::htmlscriptelement::ScriptId;
use crate::dom::identityhub::Identities;
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::messageevent::MessageEvent;
use crate::dom::messageport::MessagePort;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
@@ -41,7 +45,7 @@ use crate::dom::window::Window;
use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::dom::workletglobalscope::WorkletGlobalScope;
use crate::microtask::{Microtask, MicrotaskQueue, UserMicrotask};
use crate::realms::{enter_realm, InRealm};
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
use crate::script_module::ModuleTree;
use crate::script_runtime::{CommonScriptMsg, JSContext as SafeJSContext, ScriptChan, ScriptPort};
use crate::script_thread::{MainThreadScriptChan, ScriptThread};
@@ -2227,6 +2231,71 @@ impl GlobalScope {
}))
}

pub fn create_image_bitmap(
&self,
image: ImageBitmapSource,
options: &ImageBitmapOptions,
) -> Rc<Promise> {
let in_realm_proof = AlreadyInRealm::assert(&self);
let p = Promise::new_in_current_realm(&self, InRealm::Already(&in_realm_proof));
if options.resizeWidth.map_or(false, |w| w == 0) {
p.reject_error(Error::InvalidState);
return p;
}

if options.resizeHeight.map_or(false, |w| w == 0) {
p.reject_error(Error::InvalidState);
return p;
}

let promise = match image {
ImageBitmapSource::HTMLCanvasElement(ref canvas) => {
// https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument
if !canvas.is_valid() {
p.reject_error(Error::InvalidState);
return p;
}

if let Some((data, size)) = canvas.fetch_all_data() {
let data = data
.map(|data| data.to_vec())
.unwrap_or_else(|| vec![0; size.area() as usize * 4]);

let image_bitmap = ImageBitmap::new(&self, size.width, size.height).unwrap();

image_bitmap.set_bitmap_data(data);
image_bitmap.set_origin_clean(canvas.origin_is_clean());
p.resolve_native(&(image_bitmap));
}
p
},
ImageBitmapSource::OffscreenCanvas(ref canvas) => {
// https://html.spec.whatwg.org/multipage/#check-the-usability-of-the-image-argument
if !canvas.is_valid() {
p.reject_error(Error::InvalidState);
return p;
}

if let Some((data, size)) = canvas.fetch_all_data() {
let data = data
.map(|data| data.to_vec())
.unwrap_or_else(|| vec![0; size.area() as usize * 4]);

let image_bitmap = ImageBitmap::new(&self, size.width, size.height).unwrap();
image_bitmap.set_bitmap_data(data);
image_bitmap.set_origin_clean(canvas.origin_is_clean());
p.resolve_native(&(image_bitmap));
}
p
},
_ => {
p.reject_error(Error::NotSupported);
return p;
},
};
promise
}

pub fn fire_timer(&self, handle: TimerEventId) {
self.timers.fire_timer(handle, self);
}
@@ -12,6 +12,7 @@ use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use dom_struct::dom_struct;

use std::cell::Cell;
use std::vec::Vec;

#[dom_struct]
@@ -20,6 +21,7 @@ pub struct ImageBitmap {
width: u32,
height: u32,
bitmap_data: DomRefCell<Vec<u8>>,
origin_clean: Cell<bool>,
}

impl ImageBitmap {
@@ -29,6 +31,7 @@ impl ImageBitmap {
width: width_arg,
height: height_arg,
bitmap_data: DomRefCell::new(vec![]),
origin_clean: Cell::new(true),
}
}

@@ -39,6 +42,14 @@ impl ImageBitmap {

Ok(reflect_dom_object(imagebitmap, global))
}

pub fn set_bitmap_data(&self, data: Vec<u8>) {
*self.bitmap_data.borrow_mut() = data;
}

pub fn set_origin_clean(&self, origin_is_clean: bool) {
self.origin_clean.set(origin_is_clean);
}
}

impl ImageBitmapMethods for ImageBitmap {
@@ -24,7 +24,7 @@ interface mixin WindowOrWorkerGlobalScope {
void queueMicrotask(VoidFunction callback);

// ImageBitmap
// Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, optional ImageBitmapOptions options);
Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, optional ImageBitmapOptions options = {});
// Promise<ImageBitmap> createImageBitmap(
// ImageBitmapSource image, long sx, long sy, long sw, long sh, optional ImageBitmapOptions options);
};
@@ -7,6 +7,9 @@ use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
DocumentMethods, DocumentReadyState,
};
use crate::dom::bindings::codegen::Bindings::HistoryBinding::HistoryBinding::HistoryMethods;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapOptions, ImageBitmapSource,
};
use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryListBinding::MediaQueryListMethods;
use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
@@ -889,6 +892,18 @@ impl WindowMethods for Window {
.queue_function_as_microtask(callback);
}

// https://html.spec.whatwg.org/multipage/#dom-createimagebitmap
fn CreateImageBitmap(
&self,
image: ImageBitmapSource,
options: &ImageBitmapOptions,
) -> Rc<Promise> {
let p = self
.upcast::<GlobalScope>()
.create_image_bitmap(image, options);
p
}

// https://html.spec.whatwg.org/multipage/#dom-window
fn Window(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
@@ -3,6 +3,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapOptions, ImageBitmapSource,
};
use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
use crate::dom::bindings::codegen::Bindings::WorkerBinding::WorkerType;
@@ -347,6 +350,18 @@ impl WorkerGlobalScopeMethods for WorkerGlobalScope {
.queue_function_as_microtask(callback);
}

// https://html.spec.whatwg.org/multipage/#dom-createimagebitmap
fn CreateImageBitmap(
&self,
image: ImageBitmapSource,
options: &ImageBitmapOptions,
) -> Rc<Promise> {
let p = self
.upcast::<GlobalScope>()
.create_image_bitmap(image, options);
p
}

#[allow(unrooted_must_root)]
// https://fetch.spec.whatwg.org/#fetch-method
fn Fetch(

This file was deleted.

@@ -8,6 +8,9 @@
[[data-expected-height\] 3]
expected: FAIL

[[data-expected-height\] 4]
[[data-expected-height\] 1]
expected: FAIL

[[data-expected-height\] 2]
expected: FAIL

This file was deleted.

@@ -8,3 +8,6 @@
[throws if handleEvent is thruthy and not callable]
expected: FAIL

[looks up handleEvent method on every event dispatch]
expected: FAIL

@@ -14,3 +14,6 @@
[<li>Outside 3</li>]
expected: FAIL

[<li>Image Inside 2</li>]
expected: FAIL

@@ -0,0 +1,4 @@
[elementsFromPoint-invalid-cases.html]
[The root element is the last element returned for otherwise empty queries within the viewport]
expected: FAIL

@@ -312,27 +312,24 @@
[fetch(): separate response Content-Type: text/plain ]
expected: NOTRUN

[<iframe>: separate response Content-Type: text/plain */*;charset=gbk]
[<iframe>: separate response Content-Type: text/html */*;charset=gbk]
expected: FAIL

[<iframe>: separate response Content-Type: text/html;" text/plain]
expected: FAIL

[<iframe>: separate response Content-Type: text/html */*]
[<iframe>: combined response Content-Type: text/html;" \\" text/plain]
expected: FAIL

[<iframe>: combined response Content-Type: text/html;" \\" text/plain]
[<iframe>: separate response Content-Type: text/html;" \\" text/plain]
expected: FAIL

[<iframe>: combined response Content-Type: text/html;" text/plain]
[<iframe>: combined response Content-Type: */* text/html]
expected: FAIL

[<iframe>: combined response Content-Type: text/html */*]
[<iframe>: separate response Content-Type: text/html */*]
expected: FAIL

[<iframe>: separate response Content-Type: text/plain */*]
[<iframe>: separate response Content-Type: text/html;x=" text/plain]
expected: FAIL

[<iframe>: combined response Content-Type: text/html */*;charset=gbk]
[<iframe>: combined response Content-Type: text/html;x=" text/plain]
expected: FAIL

@@ -53,6 +53,12 @@
[combined text/javascript ]
expected: FAIL

[separate text/javascript x/x]
expected: FAIL

[separate text/javascript;charset=windows-1252 error text/javascript]
expected: FAIL

[separate text/javascript;charset=windows-1252 text/javascript]
expected: FAIL

This file was deleted.

@@ -41,6 +41,3 @@
[Window replaceable attribute: screenLeft]
expected: FAIL

[Window method: createImageBitmap]
expected: FAIL

@@ -1379,6 +1379,15 @@
[ElementInternals interface: operation setFormValue((File or USVString or FormData)?, optional (File or USVString or FormData)?)]
expected: FAIL

[Navigator interface: operation registerProtocolHandler(DOMString, USVString)]
expected: FAIL

[Navigator interface: window.navigator must inherit property "registerProtocolHandler(DOMString, USVString)" with the proper type]
expected: FAIL

[Navigator interface: calling registerProtocolHandler(DOMString, USVString) on window.navigator with too few arguments must throw TypeError]
expected: FAIL


[idlharness.https.html?include=(Document|Window)]
[Document interface: documentWithHandlers must inherit property "queryCommandEnabled(DOMString)" with the proper type]
@@ -1810,9 +1819,6 @@
[Window interface: operation createImageBitmap(ImageBitmapSource, optional ImageBitmapOptions)]
expected: FAIL

[Window interface: window must inherit property "createImageBitmap(ImageBitmapSource, optional ImageBitmapOptions)" with the proper type]
expected: FAIL

[Document interface: calling execCommand(DOMString, optional boolean, optional DOMString) on new Document() with too few arguments must throw TypeError]
expected: FAIL

@@ -1822,9 +1828,6 @@
[Document interface: new Document() must inherit property "execCommand(DOMString, optional boolean, optional DOMString)" with the proper type]
expected: FAIL

[Window interface: window must inherit property "createImageBitmap(ImageBitmapSource, long, long, long, long, optional ImageBitmapOptions)" with the proper type]
expected: FAIL

[Document interface: calling execCommand(DOMString, optional boolean, optional DOMString) on iframe.contentDocument with too few arguments must throw TypeError]
expected: FAIL

@@ -2403,9 +2406,6 @@
[HTMLMeterElement interface: document.createElement("meter") must inherit property "value" with the proper type]
expected: FAIL

[HTMLElement interface: attribute isContentEditable]
expected: FAIL

[HTMLEmbedElement interface: attribute align]
expected: FAIL

@@ -2616,9 +2616,6 @@
[HTMLInputElement interface: document.createElement("input") must inherit property "useMap" with the proper type]
expected: FAIL
[HTMLElement interface: document.createElement("noscript") must inherit property "isContentEditable" with the proper type]
expected: FAIL
[HTMLAreaElement interface: attribute ping]
expected: FAIL
@@ -3234,9 +3231,6 @@
[HTMLMarqueeElement interface: document.createElement("marquee") must inherit property "trueSpeed" with the proper type]
expected: FAIL
[HTMLElement interface: attribute contentEditable]
expected: FAIL
[HTMLInputElement interface: createInput("file") must inherit property "align" with the proper type]
expected: FAIL
@@ -3579,9 +3573,6 @@
[HTMLTableCellElement interface: document.createElement("th") must inherit property "ch" with the proper type]
expected: FAIL

[HTMLElement interface: document.createElement("noscript") must inherit property "contentEditable" with the proper type]
expected: FAIL

[HTMLTextAreaElement interface: document.createElement("textarea") must inherit property "autocomplete" with the proper type]
expected: FAIL

@@ -494,9 +494,6 @@
[OffscreenCanvasRenderingContext2D interface: operation fill(optional CanvasFillRule)]
expected: FAIL

[WorkerGlobalScope interface: self must inherit property "createImageBitmap(ImageBitmapSource, long, long, long, long, optional ImageBitmapOptions)" with the proper type]
expected: FAIL

[OffscreenCanvasRenderingContext2D interface: operation isPointInPath(unrestricted double, unrestricted double, optional CanvasFillRule)]
expected: FAIL

@@ -551,9 +548,6 @@
[Path2D interface: operation addPath(Path2D, optional DOMMatrix2DInit)]
expected: FAIL

[WorkerGlobalScope interface: self must inherit property "createImageBitmap(ImageBitmapSource, optional ImageBitmapOptions)" with the proper type]
expected: FAIL

[OffscreenCanvasRenderingContext2D interface: operation clip(optional CanvasFillRule)]
expected: FAIL

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.