Skip to content

Commit

Permalink
Auto merge of #26296 - ramyananth:master, r=<try>
Browse files Browse the repository at this point in the history
WIP: Implementing createImageBitmap

<!-- Please describe your changes on the following line: -->
Implementing createImageBitmap method for canvas image source.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #20650 (GitHub issue number if applicable)

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
  • Loading branch information
bors-servo authored Apr 25, 2020
2 parents b6a465c + 37e026d commit c2d6083
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
11 changes: 11 additions & 0 deletions components/script/dom/imagebitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -20,6 +21,7 @@ pub struct ImageBitmap {
width: u32,
height: u32,
bitmap_data: DomRefCell<Vec<u8>>,
origin_clean: Cell<bool>,
}

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

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

Ok(reflect_dom_object(imagebitmap, global))
}

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

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

impl ImageBitmapMethods for ImageBitmap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
67 changes: 66 additions & 1 deletion components/script/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +40,7 @@ use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::hashchangeevent::HashChangeEvent;
use crate::dom::history::History;
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::location::Location;
use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
use crate::dom::mediaquerylistevent::MediaQueryListEvent;
Expand All @@ -57,7 +61,7 @@ use crate::fetch;
use crate::layout_image::fetch_image_for_layout;
use crate::malloc_size_of::MallocSizeOf;
use crate::microtask::MicrotaskQueue;
use crate::realms::InRealm;
use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::{
CommonScriptMsg, JSContext, Runtime, ScriptChan, ScriptPort, ScriptThreadEventCategory,
};
Expand Down Expand Up @@ -889,6 +893,67 @@ 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 global = self.global();
let in_realm_proof = AlreadyInRealm::assert(&global);
let p = Promise::new_in_current_realm(&global, InRealm::Already(&in_realm_proof));
if options.resizeWidth.unwrap() == 0 {
p.reject_error(Error::InvalidState)
}

if options.resizeHeight.unwrap() == 0 {
p.reject_error(Error::InvalidState)
}

let image_bitmap = ImageBitmap::new(&global, 0, 0).unwrap();

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)
}

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

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)
}

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

image_bitmap.set_bitmap_data(data);
image_bitmap.set_origin_clean(canvas.origin_is_clean());
p.resolve_native(&(image_bitmap));
}
p
},
_ => p,
};
promise
}

// https://html.spec.whatwg.org/multipage/#dom-window
fn Window(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
Expand Down
67 changes: 66 additions & 1 deletion components/script/dom/workerglobalscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,14 +21,15 @@ use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::crypto::Crypto;
use crate::dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope;
use crate::dom::globalscope::GlobalScope;
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::performance::Performance;
use crate::dom::promise::Promise;
use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
use crate::dom::window::{base64_atob, base64_btoa};
use crate::dom::workerlocation::WorkerLocation;
use crate::dom::workernavigator::WorkerNavigator;
use crate::fetch;
use crate::realms::{enter_realm, InRealm};
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
use crate::script_runtime::JSContext;
use crate::script_runtime::{get_reports, CommonScriptMsg, Runtime, ScriptChan, ScriptPort};
use crate::task::TaskCanceller;
Expand Down Expand Up @@ -375,6 +379,67 @@ impl WorkerGlobalScopeMethods for WorkerGlobalScope {
.ascii_serialization(),
)
}

// https://html.spec.whatwg.org/multipage/#dom-createimagebitmap
fn CreateImageBitmap(
&self,
image: ImageBitmapSource,
options: &ImageBitmapOptions,
) -> Rc<Promise> {
let global = self.global();
let in_realm_proof = AlreadyInRealm::assert(&global);
let p = Promise::new_in_current_realm(&global, InRealm::Already(&in_realm_proof));
if options.resizeWidth.unwrap() == 0 {
p.reject_error(Error::InvalidState)
}

if options.resizeHeight.unwrap() == 0 {
p.reject_error(Error::InvalidState)
}

let image_bitmap = ImageBitmap::new(&global, 0, 0).unwrap();

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)
}

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

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)
}

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

image_bitmap.set_bitmap_data(data);
image_bitmap.set_origin_clean(canvas.origin_is_clean());
p.resolve_native(&(image_bitmap));
}
p
},
_ => p,
};
promise
}
}

impl WorkerGlobalScope {
Expand Down

0 comments on commit c2d6083

Please sign in to comment.