mirror of
https://github.com/servo/servo.git
synced 2025-06-11 01:50:10 +00:00
Add missing basic functionality for ImageBitmap https://html.spec.whatwg.org/multipage/#imagebitmap including new variant of creation bitmap with source rectangle https://html.spec.whatwg.org/multipage/#dom-createimagebitmap and cropping bitmap data with formatting (partially) https://html.spec.whatwg.org/multipage/#cropped-to-the-source-rectangle-with-formatting For now the following functionality not implemented: - image orientation support (such as EXIF metadata) - scale down/up (resize option) - color space conversion Add ImageBitmap/HMTLVideoElement to CanvasImageSource for Canvas2D https://html.spec.whatwg.org/multipage/#canvasimagesource Add ImageBitmap to TexImageSource for WebGL https://registry.khronos.org/webgl/specs/latest/1.0/index.html Testing: Covered by existed WPT tests - html/canvas/element/manual/imagebitmap/* - html/canvas/element/manual/wide-gamut-canvas/* - html/semantics/embedded-content/the-canvas-element/* - webgl/tests/conformance/textures/image_bitmap_from* Fixes: https://github.com/servo/servo/issues/34112 Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
110 lines
3.5 KiB
Rust
110 lines
3.5 KiB
Rust
/* 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 snapshot::Snapshot;
|
|
|
|
use crate::dom::bindings::cell::DomRefCell;
|
|
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::ImageBitmapMethods;
|
|
use crate::dom::bindings::error::Fallible;
|
|
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]
|
|
pub(crate) struct ImageBitmap {
|
|
reflector_: Reflector,
|
|
width: u32,
|
|
height: u32,
|
|
/// 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(width: u32, height: u32) -> ImageBitmap {
|
|
ImageBitmap {
|
|
reflector_: Reflector::new(),
|
|
width,
|
|
height,
|
|
bitmap_data: DomRefCell::new(Some(Snapshot::cleared(Size2D::new(width, width).cast()))),
|
|
origin_clean: Cell::new(true),
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub(crate) fn new(
|
|
global: &GlobalScope,
|
|
width: u32,
|
|
height: u32,
|
|
can_gc: CanGc,
|
|
) -> Fallible<DomRoot<ImageBitmap>> {
|
|
//assigning to a variable the return object of new_inherited
|
|
let imagebitmap = Box::new(ImageBitmap::new_inherited(width, height));
|
|
|
|
Ok(reflect_dom_object(imagebitmap, global, can_gc))
|
|
}
|
|
|
|
pub(crate) fn bitmap_data(&self) -> Option<Snapshot> {
|
|
self.bitmap_data.borrow().clone()
|
|
}
|
|
|
|
pub(crate) fn set_bitmap_data(&self, data: Snapshot) {
|
|
*self.bitmap_data.borrow_mut() = Some(data);
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
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.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.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();
|
|
}
|
|
}
|