mirror of
https://github.com/servo/servo.git
synced 2025-07-16 11:53:39 +01:00
canvas: Make OffscreenCanvas transferable (without placeholder) (#37872)
Follow the specification and make OffscreenCanvas objects are transferable, but not in case if there are a weak reference to placeholder canvas element. To handle it properly need to implement dedicated frame provider/dispatcher between canvas element (script thread) and offscreen canvas (dedicated worker thread). https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transferable-objects Testing: Improvements in the following tests - html/canvas/element/drawing-images-to-the-canvas/2d.drawImage.detachedcanvas.html - html/canvas/offscreen/manual/convert-to-blob/offscreencanvas.convert.to.blob* - html/canvas/offscreen/manual/the-offscreen-canvas/offscreencanvas.transfer* - html/infrastructure/safe-passing-of-structured-data/transfer-errors.window.js Part of #24276 Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
parent
c3f441d7ab
commit
4054f9a5a0
17 changed files with 159 additions and 60 deletions
|
@ -258,6 +258,7 @@ pub(crate) enum OffscreenRenderingContext {
|
||||||
//WebGL2(Dom<WebGL2RenderingContext>),
|
//WebGL2(Dom<WebGL2RenderingContext>),
|
||||||
//#[cfg(feature = "webgpu")]
|
//#[cfg(feature = "webgpu")]
|
||||||
//WebGPU(Dom<GPUCanvasContext>),
|
//WebGPU(Dom<GPUCanvasContext>),
|
||||||
|
Detached,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanvasContext for OffscreenRenderingContext {
|
impl CanvasContext for OffscreenRenderingContext {
|
||||||
|
@ -268,54 +269,63 @@ impl CanvasContext for OffscreenRenderingContext {
|
||||||
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
|
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.canvas(),
|
OffscreenRenderingContext::Context2d(context) => context.canvas(),
|
||||||
|
OffscreenRenderingContext::Detached => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(&self) {
|
fn resize(&self) {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.resize(),
|
OffscreenRenderingContext::Context2d(context) => context.resize(),
|
||||||
|
OffscreenRenderingContext::Detached => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset_bitmap(&self) {
|
fn reset_bitmap(&self) {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.reset_bitmap(),
|
OffscreenRenderingContext::Context2d(context) => context.reset_bitmap(),
|
||||||
|
OffscreenRenderingContext::Detached => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_image_data(&self) -> Option<Snapshot> {
|
fn get_image_data(&self) -> Option<Snapshot> {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.get_image_data(),
|
OffscreenRenderingContext::Context2d(context) => context.get_image_data(),
|
||||||
|
OffscreenRenderingContext::Detached => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn origin_is_clean(&self) -> bool {
|
fn origin_is_clean(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.origin_is_clean(),
|
OffscreenRenderingContext::Context2d(context) => context.origin_is_clean(),
|
||||||
|
OffscreenRenderingContext::Detached => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size(&self) -> Size2D<u32> {
|
fn size(&self) -> Size2D<u32> {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.size(),
|
OffscreenRenderingContext::Context2d(context) => context.size(),
|
||||||
|
OffscreenRenderingContext::Detached => Size2D::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mark_as_dirty(&self) {
|
fn mark_as_dirty(&self) {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.mark_as_dirty(),
|
OffscreenRenderingContext::Context2d(context) => context.mark_as_dirty(),
|
||||||
|
OffscreenRenderingContext::Detached => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_rendering(&self) {
|
fn update_rendering(&self) {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.update_rendering(),
|
OffscreenRenderingContext::Context2d(context) => context.update_rendering(),
|
||||||
|
OffscreenRenderingContext::Detached => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onscreen(&self) -> bool {
|
fn onscreen(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
OffscreenRenderingContext::Context2d(context) => context.onscreen(),
|
OffscreenRenderingContext::Context2d(context) => context.onscreen(),
|
||||||
|
OffscreenRenderingContext::Detached => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -651,6 +651,7 @@ impl CanvasState {
|
||||||
smoothing_enabled,
|
smoothing_enabled,
|
||||||
));
|
));
|
||||||
},
|
},
|
||||||
|
OffscreenRenderingContext::Detached => return Err(Error::InvalidState),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.send_canvas_2d_msg(Canvas2dMsg::DrawEmptyImage(
|
self.send_canvas_2d_msg(Canvas2dMsg::DrawEmptyImage(
|
||||||
|
@ -719,6 +720,7 @@ impl CanvasState {
|
||||||
source_rect,
|
source_rect,
|
||||||
smoothing_enabled,
|
smoothing_enabled,
|
||||||
)),
|
)),
|
||||||
|
OffscreenRenderingContext::Detached => return Err(Error::InvalidState),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => return Err(Error::InvalidState),
|
_ => return Err(Error::InvalidState),
|
||||||
|
|
|
@ -11,12 +11,12 @@ use std::ptr;
|
||||||
|
|
||||||
use base::id::{
|
use base::id::{
|
||||||
BlobId, DomExceptionId, DomPointId, ImageBitmapId, Index, MessagePortId, NamespaceIndex,
|
BlobId, DomExceptionId, DomPointId, ImageBitmapId, Index, MessagePortId, NamespaceIndex,
|
||||||
PipelineNamespaceId,
|
OffscreenCanvasId, PipelineNamespaceId,
|
||||||
};
|
};
|
||||||
use constellation_traits::{
|
use constellation_traits::{
|
||||||
BlobImpl, DomException, DomPoint, MessagePortImpl, Serializable as SerializableInterface,
|
BlobImpl, DomException, DomPoint, MessagePortImpl, Serializable as SerializableInterface,
|
||||||
SerializableImageBitmap, StructuredSerializedData, Transferrable as TransferrableInterface,
|
SerializableImageBitmap, StructuredSerializedData, TransferableOffscreenCanvas,
|
||||||
TransformStreamData,
|
Transferrable as TransferrableInterface, TransformStreamData,
|
||||||
};
|
};
|
||||||
use js::gc::RootedVec;
|
use js::gc::RootedVec;
|
||||||
use js::glue::{
|
use js::glue::{
|
||||||
|
@ -46,6 +46,7 @@ use crate::dom::dompointreadonly::DOMPointReadOnly;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::imagebitmap::ImageBitmap;
|
use crate::dom::imagebitmap::ImageBitmap;
|
||||||
use crate::dom::messageport::MessagePort;
|
use crate::dom::messageport::MessagePort;
|
||||||
|
use crate::dom::offscreencanvas::OffscreenCanvas;
|
||||||
use crate::dom::readablestream::ReadableStream;
|
use crate::dom::readablestream::ReadableStream;
|
||||||
use crate::dom::types::{DOMException, TransformStream};
|
use crate::dom::types::{DOMException, TransformStream};
|
||||||
use crate::dom::writablestream::WritableStream;
|
use crate::dom::writablestream::WritableStream;
|
||||||
|
@ -70,6 +71,7 @@ pub(super) enum StructuredCloneTags {
|
||||||
WritableStream = 0xFFFF8008,
|
WritableStream = 0xFFFF8008,
|
||||||
TransformStream = 0xFFFF8009,
|
TransformStream = 0xFFFF8009,
|
||||||
ImageBitmap = 0xFFFF800A,
|
ImageBitmap = 0xFFFF800A,
|
||||||
|
OffscreenCanvas = 0xFFFF800B,
|
||||||
Max = 0xFFFFFFFF,
|
Max = 0xFFFFFFFF,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +92,7 @@ impl From<TransferrableInterface> for StructuredCloneTags {
|
||||||
match v {
|
match v {
|
||||||
TransferrableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
|
TransferrableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
|
||||||
TransferrableInterface::MessagePort => StructuredCloneTags::MessagePort,
|
TransferrableInterface::MessagePort => StructuredCloneTags::MessagePort,
|
||||||
|
TransferrableInterface::OffscreenCanvas => StructuredCloneTags::OffscreenCanvas,
|
||||||
TransferrableInterface::ReadableStream => StructuredCloneTags::ReadableStream,
|
TransferrableInterface::ReadableStream => StructuredCloneTags::ReadableStream,
|
||||||
TransferrableInterface::WritableStream => StructuredCloneTags::WritableStream,
|
TransferrableInterface::WritableStream => StructuredCloneTags::WritableStream,
|
||||||
TransferrableInterface::TransformStream => StructuredCloneTags::TransformStream,
|
TransferrableInterface::TransformStream => StructuredCloneTags::TransformStream,
|
||||||
|
@ -274,6 +277,7 @@ fn receiver_for_type(
|
||||||
match val {
|
match val {
|
||||||
TransferrableInterface::ImageBitmap => receive_object::<ImageBitmap>,
|
TransferrableInterface::ImageBitmap => receive_object::<ImageBitmap>,
|
||||||
TransferrableInterface::MessagePort => receive_object::<MessagePort>,
|
TransferrableInterface::MessagePort => receive_object::<MessagePort>,
|
||||||
|
TransferrableInterface::OffscreenCanvas => receive_object::<OffscreenCanvas>,
|
||||||
TransferrableInterface::ReadableStream => receive_object::<ReadableStream>,
|
TransferrableInterface::ReadableStream => receive_object::<ReadableStream>,
|
||||||
TransferrableInterface::WritableStream => receive_object::<WritableStream>,
|
TransferrableInterface::WritableStream => receive_object::<WritableStream>,
|
||||||
TransferrableInterface::TransformStream => receive_object::<TransformStream>,
|
TransferrableInterface::TransformStream => receive_object::<TransformStream>,
|
||||||
|
@ -401,6 +405,7 @@ fn transfer_for_type(val: TransferrableInterface) -> TransferOperation {
|
||||||
match val {
|
match val {
|
||||||
TransferrableInterface::ImageBitmap => try_transfer::<ImageBitmap>,
|
TransferrableInterface::ImageBitmap => try_transfer::<ImageBitmap>,
|
||||||
TransferrableInterface::MessagePort => try_transfer::<MessagePort>,
|
TransferrableInterface::MessagePort => try_transfer::<MessagePort>,
|
||||||
|
TransferrableInterface::OffscreenCanvas => try_transfer::<OffscreenCanvas>,
|
||||||
TransferrableInterface::ReadableStream => try_transfer::<ReadableStream>,
|
TransferrableInterface::ReadableStream => try_transfer::<ReadableStream>,
|
||||||
TransferrableInterface::WritableStream => try_transfer::<WritableStream>,
|
TransferrableInterface::WritableStream => try_transfer::<WritableStream>,
|
||||||
TransferrableInterface::TransformStream => try_transfer::<TransformStream>,
|
TransferrableInterface::TransformStream => try_transfer::<TransformStream>,
|
||||||
|
@ -451,6 +456,7 @@ unsafe fn can_transfer_for_type(
|
||||||
match transferable {
|
match transferable {
|
||||||
TransferrableInterface::ImageBitmap => can_transfer::<ImageBitmap>(obj, cx),
|
TransferrableInterface::ImageBitmap => can_transfer::<ImageBitmap>(obj, cx),
|
||||||
TransferrableInterface::MessagePort => can_transfer::<MessagePort>(obj, cx),
|
TransferrableInterface::MessagePort => can_transfer::<MessagePort>(obj, cx),
|
||||||
|
TransferrableInterface::OffscreenCanvas => can_transfer::<OffscreenCanvas>(obj, cx),
|
||||||
TransferrableInterface::ReadableStream => can_transfer::<ReadableStream>(obj, cx),
|
TransferrableInterface::ReadableStream => can_transfer::<ReadableStream>(obj, cx),
|
||||||
TransferrableInterface::WritableStream => can_transfer::<WritableStream>(obj, cx),
|
TransferrableInterface::WritableStream => can_transfer::<WritableStream>(obj, cx),
|
||||||
TransferrableInterface::TransformStream => can_transfer::<TransformStream>(obj, cx),
|
TransferrableInterface::TransformStream => can_transfer::<TransformStream>(obj, cx),
|
||||||
|
@ -542,6 +548,8 @@ pub(crate) struct StructuredDataReader<'a> {
|
||||||
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
||||||
/// A map of transferred image bitmaps.
|
/// A map of transferred image bitmaps.
|
||||||
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
||||||
|
/// A map of transferred offscreen canvases.
|
||||||
|
pub(crate) offscreen_canvases: Option<HashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A data holder for transferred and serialized objects.
|
/// A data holder for transferred and serialized objects.
|
||||||
|
@ -564,6 +572,8 @@ pub(crate) struct StructuredDataWriter {
|
||||||
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
||||||
/// Transferred image bitmaps.
|
/// Transferred image bitmaps.
|
||||||
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
||||||
|
/// Transferred offscreen canvases.
|
||||||
|
pub(crate) offscreen_canvases: Option<HashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a structured clone. Returns a `DataClone` error if that fails.
|
/// Writes a structured clone. Returns a `DataClone` error if that fails.
|
||||||
|
@ -620,6 +630,7 @@ pub(crate) fn write(
|
||||||
blobs: sc_writer.blobs.take(),
|
blobs: sc_writer.blobs.take(),
|
||||||
image_bitmaps: sc_writer.image_bitmaps.take(),
|
image_bitmaps: sc_writer.image_bitmaps.take(),
|
||||||
transferred_image_bitmaps: sc_writer.transferred_image_bitmaps.take(),
|
transferred_image_bitmaps: sc_writer.transferred_image_bitmaps.take(),
|
||||||
|
offscreen_canvases: sc_writer.offscreen_canvases.take(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(data)
|
Ok(data)
|
||||||
|
@ -646,6 +657,7 @@ pub(crate) fn read(
|
||||||
errors: DOMErrorRecord { message: None },
|
errors: DOMErrorRecord { message: None },
|
||||||
image_bitmaps: data.image_bitmaps.take(),
|
image_bitmaps: data.image_bitmaps.take(),
|
||||||
transferred_image_bitmaps: data.transferred_image_bitmaps.take(),
|
transferred_image_bitmaps: data.transferred_image_bitmaps.take(),
|
||||||
|
offscreen_canvases: data.offscreen_canvases.take(),
|
||||||
};
|
};
|
||||||
let sc_reader_ptr = &mut sc_reader as *mut _;
|
let sc_reader_ptr = &mut sc_reader as *mut _;
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use constellation_traits::BlobImpl;
|
use base::id::{OffscreenCanvasId, OffscreenCanvasIndex};
|
||||||
|
use constellation_traits::{BlobImpl, TransferableOffscreenCanvas};
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use euclid::default::Size2D;
|
use euclid::default::Size2D;
|
||||||
use js::rust::{HandleObject, HandleValue};
|
use js::rust::{HandleObject, HandleValue};
|
||||||
|
@ -23,6 +25,8 @@ use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
|
||||||
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
|
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
|
||||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||||
use crate::dom::bindings::str::DOMString;
|
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::blob::Blob;
|
||||||
use crate::dom::eventtarget::EventTarget;
|
use crate::dom::eventtarget::EventTarget;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
|
@ -126,6 +130,7 @@ impl OffscreenCanvas {
|
||||||
if let Some(ctx) = self.context() {
|
if let Some(ctx) = self.context() {
|
||||||
return match *ctx {
|
return match *ctx {
|
||||||
OffscreenRenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
|
OffscreenRenderingContext::Context2d(ref ctx) => Some(DomRoot::from_ref(ctx)),
|
||||||
|
_ => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
let context = OffscreenCanvasRenderingContext2D::new(&self.global(), self, can_gc);
|
let context = OffscreenCanvasRenderingContext2D::new(&self.global(), self, can_gc);
|
||||||
|
@ -142,6 +147,86 @@ impl OffscreenCanvas {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Transferable for OffscreenCanvas {
|
||||||
|
type Index = OffscreenCanvasIndex;
|
||||||
|
type Data = TransferableOffscreenCanvas;
|
||||||
|
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transfer-steps>
|
||||||
|
fn transfer(&self) -> Result<(OffscreenCanvasId, TransferableOffscreenCanvas), ()> {
|
||||||
|
// TODO(#37919) 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(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(#37882): Allow to transfer with a placeholder canvas element.
|
||||||
|
if self.placeholder.is_some() {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas>
|
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas>
|
||||||
fn Constructor(
|
fn Constructor(
|
||||||
|
@ -151,8 +236,9 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
|
||||||
width: u64,
|
width: u64,
|
||||||
height: u64,
|
height: u64,
|
||||||
) -> Fallible<DomRoot<OffscreenCanvas>> {
|
) -> Fallible<DomRoot<OffscreenCanvas>> {
|
||||||
let offscreencanvas = OffscreenCanvas::new(global, proto, width, height, None, can_gc);
|
Ok(OffscreenCanvas::new(
|
||||||
Ok(offscreencanvas)
|
global, proto, width, height, None, can_gc,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-getcontext>
|
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-getcontext>
|
||||||
|
@ -163,6 +249,12 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
|
||||||
_options: HandleValue,
|
_options: HandleValue,
|
||||||
can_gc: CanGc,
|
can_gc: CanGc,
|
||||||
) -> Fallible<Option<RootedOffscreenRenderingContext>> {
|
) -> 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 {
|
match &*id {
|
||||||
"2d" => Ok(self
|
"2d" => Ok(self
|
||||||
.get_or_init_2d_context(can_gc)
|
.get_or_init_2d_context(can_gc)
|
||||||
|
@ -217,9 +309,12 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-transfertoimagebitmap>
|
/// <https://html.spec.whatwg.org/multipage/#dom-offscreencanvas-transfertoimagebitmap>
|
||||||
fn TransferToImageBitmap(&self, can_gc: CanGc) -> Fallible<DomRoot<ImageBitmap>> {
|
fn TransferToImageBitmap(&self, can_gc: CanGc) -> Fallible<DomRoot<ImageBitmap>> {
|
||||||
// TODO Step 1. If the value of this OffscreenCanvas object's
|
// Step 1. If the value of this OffscreenCanvas object's [[Detached]]
|
||||||
// [[Detached]] internal slot is set to true, then throw an
|
// internal slot is set to true, then throw an "InvalidStateError"
|
||||||
// "InvalidStateError" DOMException.
|
// 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,
|
// Step 2. If this OffscreenCanvas object's context mode is set to none,
|
||||||
// then throw an "InvalidStateError" DOMException.
|
// then throw an "InvalidStateError" DOMException.
|
||||||
|
@ -255,6 +350,14 @@ impl OffscreenCanvasMethods<crate::DomTypeHolder> for OffscreenCanvas {
|
||||||
let in_realm_proof = AlreadyInRealm::assert::<crate::DomTypeHolder>();
|
let in_realm_proof = AlreadyInRealm::assert::<crate::DomTypeHolder>();
|
||||||
let promise = Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), can_gc);
|
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
|
// 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
|
// output bitmap's origin-clean flag is set to false, then return a
|
||||||
// promise rejected with a "SecurityError" DOMException.
|
// promise rejected with a "SecurityError" DOMException.
|
||||||
|
|
|
@ -13,7 +13,7 @@ dictionary ImageEncodeOptions {
|
||||||
|
|
||||||
//enum OffscreenRenderingContextId { "2d", "webgl", "webgl2" };
|
//enum OffscreenRenderingContextId { "2d", "webgl", "webgl2" };
|
||||||
|
|
||||||
[Exposed=(Window,Worker)/*, Transferable*/, Pref="dom_offscreen_canvas_enabled"]
|
[Exposed=(Window,Worker), Transferable, Pref="dom_offscreen_canvas_enabled"]
|
||||||
interface OffscreenCanvas : EventTarget {
|
interface OffscreenCanvas : EventTarget {
|
||||||
[Throws] constructor([EnforceRange] unsigned long long width, [EnforceRange] unsigned long long height);
|
[Throws] constructor([EnforceRange] unsigned long long width, [EnforceRange] unsigned long long height);
|
||||||
attribute [EnforceRange] unsigned long long width;
|
attribute [EnforceRange] unsigned long long width;
|
||||||
|
|
|
@ -373,6 +373,8 @@ namespace_id! {HistoryStateId, HistoryStateIndex, "HistoryState"}
|
||||||
|
|
||||||
namespace_id! {ImageBitmapId, ImageBitmapIndex, "ImageBitmap"}
|
namespace_id! {ImageBitmapId, ImageBitmapIndex, "ImageBitmap"}
|
||||||
|
|
||||||
|
namespace_id! {OffscreenCanvasId, OffscreenCanvasIndex, "OffscreenCanvas"}
|
||||||
|
|
||||||
// We provide ids just for unit testing.
|
// We provide ids just for unit testing.
|
||||||
pub const TEST_NAMESPACE: PipelineNamespaceId = PipelineNamespaceId(1234);
|
pub const TEST_NAMESPACE: PipelineNamespaceId = PipelineNamespaceId(1234);
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
|
|
|
@ -214,6 +214,7 @@ pub struct SWManagerSenders {
|
||||||
|
|
||||||
/// Messages sent to Service Worker Manager thread
|
/// Messages sent to Service Worker Manager thread
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
pub enum ServiceWorkerMsg {
|
pub enum ServiceWorkerMsg {
|
||||||
/// Timeout message sent by active service workers
|
/// Timeout message sent by active service workers
|
||||||
Timeout(ServoUrl),
|
Timeout(ServoUrl),
|
||||||
|
|
|
@ -10,7 +10,9 @@ mod transferable;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use base::id::{BlobId, DomExceptionId, DomPointId, ImageBitmapId, MessagePortId};
|
use base::id::{
|
||||||
|
BlobId, DomExceptionId, DomPointId, ImageBitmapId, MessagePortId, OffscreenCanvasId,
|
||||||
|
};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use malloc_size_of_derive::MallocSizeOf;
|
use malloc_size_of_derive::MallocSizeOf;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -38,6 +40,8 @@ pub struct StructuredSerializedData {
|
||||||
pub image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
pub image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
||||||
/// Transferred image bitmap objects.
|
/// Transferred image bitmap objects.
|
||||||
pub transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
pub transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
|
||||||
|
/// Transferred offscreen canvas objects.
|
||||||
|
pub offscreen_canvases: Option<HashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StructuredSerializedData {
|
impl StructuredSerializedData {
|
||||||
|
@ -48,6 +52,7 @@ impl StructuredSerializedData {
|
||||||
match val {
|
match val {
|
||||||
Transferrable::ImageBitmap => is_field_empty(&self.transferred_image_bitmaps),
|
Transferrable::ImageBitmap => is_field_empty(&self.transferred_image_bitmaps),
|
||||||
Transferrable::MessagePort => is_field_empty(&self.ports),
|
Transferrable::MessagePort => is_field_empty(&self.ports),
|
||||||
|
Transferrable::OffscreenCanvas => is_field_empty(&self.offscreen_canvases),
|
||||||
Transferrable::ReadableStream => is_field_empty(&self.ports),
|
Transferrable::ReadableStream => is_field_empty(&self.ports),
|
||||||
Transferrable::WritableStream => is_field_empty(&self.ports),
|
Transferrable::WritableStream => is_field_empty(&self.ports),
|
||||||
Transferrable::TransformStream => is_field_empty(&self.ports),
|
Transferrable::TransformStream => is_field_empty(&self.ports),
|
||||||
|
|
|
@ -28,6 +28,8 @@ pub enum Transferrable {
|
||||||
ImageBitmap,
|
ImageBitmap,
|
||||||
/// The `MessagePort` interface.
|
/// The `MessagePort` interface.
|
||||||
MessagePort,
|
MessagePort,
|
||||||
|
/// The `OffscreenCanvas` interface.
|
||||||
|
OffscreenCanvas,
|
||||||
/// The `ReadableStream` interface.
|
/// The `ReadableStream` interface.
|
||||||
ReadableStream,
|
ReadableStream,
|
||||||
/// The `WritableStream` interface.
|
/// The `WritableStream` interface.
|
||||||
|
@ -185,3 +187,12 @@ impl MessagePortImpl {
|
||||||
self.state = MessagePortState::Detached;
|
self.state = MessagePortState::Detached;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||||
|
/// A struct supporting the transfer of OffscreenCanvas, which loosely
|
||||||
|
/// corresponds to the dataHolder in
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:offscreencanvas-16>
|
||||||
|
pub struct TransferableOffscreenCanvas {
|
||||||
|
pub width: u64,
|
||||||
|
pub height: u64,
|
||||||
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
[2d.drawImage.detachedcanvas.html]
|
|
||||||
[drawImage with detached OffscreenCanvas as the source should throw exception]
|
|
||||||
expected: FAIL
|
|
|
@ -1,3 +0,0 @@
|
||||||
[offscreencanvas.convert.to.blob.html]
|
|
||||||
[Test that call convertToBlob on a detached OffscreenCanvas throws exception]
|
|
||||||
expected: FAIL
|
|
|
@ -1,4 +0,0 @@
|
||||||
[offscreencanvas.convert.to.blob.w.html]
|
|
||||||
expected: ERROR
|
|
||||||
[Test that call convertToBlob on a detached OffscreenCanvas throws exception in a worker]
|
|
||||||
expected: FAIL
|
|
|
@ -4,6 +4,3 @@
|
||||||
|
|
||||||
[Test that transferToImageBitmap returns an ImageBitmap with correct width and height]
|
[Test that transferToImageBitmap returns an ImageBitmap with correct width and height]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Test that call transferToImageBitmap on a detached OffscreenCanvas throws an exception]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
|
@ -3,8 +3,5 @@
|
||||||
[Test that transferToImageBitmap returns an ImageBitmap with correct width and height in a worker]
|
[Test that transferToImageBitmap returns an ImageBitmap with correct width and height in a worker]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Test that call transferToImageBitmap on a detached OffscreenCanvas throws an exception in a worker]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that call transferToImageBitmap twice on a alpha-disabled context returns an ImageBitmap with correct color in a worker]
|
[Test that call transferToImageBitmap twice on a alpha-disabled context returns an ImageBitmap with correct color in a worker]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
|
@ -1,18 +1,3 @@
|
||||||
[offscreencanvas.transferrable.html]
|
[offscreencanvas.transferrable.html]
|
||||||
[Test that offscreenCanvas's size is correct after being transferred to a worker.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that calling getContext('webgl') on a detached OffscreenCanvas throws exception.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that transfer an OffscreenCanvas that has a context throws exception.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that transfer an OffscreenCanvas twice throws exception.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that calling getContext('2d') on a detached OffscreenCanvas throws exception.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that transfer an OffscreenCanvas that already have a 2d context throws exception.]
|
[Test that transfer an OffscreenCanvas that already have a 2d context throws exception.]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
|
@ -1,20 +1,10 @@
|
||||||
[offscreencanvas.transferrable.w.html]
|
[offscreencanvas.transferrable.w.html]
|
||||||
expected: ERROR
|
expected: ERROR
|
||||||
[Test that calling getContext('webgl') on a detached OffscreenCanvas throws exception in a worker.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that transfer an OffscreenCanvas that has a webgl context throws exception in a worker.]
|
[Test that transfer an OffscreenCanvas that has a webgl context throws exception in a worker.]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Test that transfer an OffscreenCanvas twice throws exception in a worker.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that calling getContext('2d') on a detached OffscreenCanvas throws exception in a worker.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that OffscreenCanvas's size is correct after being transferred from a worker.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Test that transfer an OffscreenCanvas that has a 2d context throws exception in a worker.]
|
[Test that transfer an OffscreenCanvas that has a 2d context throws exception in a worker.]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
[Test that transfer an OffscreenCanvas twice throws exception in a worker.]
|
||||||
|
expected: FAIL
|
||||||
|
|
|
@ -8,11 +8,5 @@
|
||||||
[Serialize should throw before a detached ImageBitmap is found]
|
[Serialize should throw before a detached ImageBitmap is found]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Serialize should make the OffscreenCanvas detached, so it cannot be transferred again]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Serialize should throw before a detached OffscreenCanvas is found]
|
[Serialize should throw before a detached OffscreenCanvas is found]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Cannot transfer OffscreenCanvas detached while the message was serialized]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue