diff --git a/Cargo.lock b/Cargo.lock index f9cebd773fd..da769c06780 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1209,6 +1209,7 @@ dependencies = [ "serde", "servo_malloc_size_of", "servo_url", + "snapshot", "strum", "strum_macros", "uuid", @@ -7117,8 +7118,10 @@ version = "0.0.1" dependencies = [ "euclid", "ipc-channel", + "malloc_size_of_derive", "pixels", "serde", + "servo_malloc_size_of", ] [[package]] diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index b50f0b1e75f..a34b49f4a65 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -2983,9 +2983,7 @@ impl GlobalScope { } if let Some(snapshot) = canvas.get_image_data() { - let size = snapshot.size().cast(); - let image_bitmap = ImageBitmap::new(self, size.width, size.height, can_gc); - image_bitmap.set_bitmap_data(snapshot.to_vec()); + let image_bitmap = ImageBitmap::new(self, snapshot, can_gc); image_bitmap.set_origin_clean(canvas.origin_is_clean()); p.resolve_native(&(image_bitmap), can_gc); } @@ -2999,9 +2997,7 @@ impl GlobalScope { } if let Some(snapshot) = canvas.get_image_data() { - let size = snapshot.size().cast(); - let image_bitmap = ImageBitmap::new(self, size.width, size.height, can_gc); - image_bitmap.set_bitmap_data(snapshot.to_vec()); + let image_bitmap = ImageBitmap::new(self, snapshot, can_gc); image_bitmap.set_origin_clean(canvas.origin_is_clean()); p.resolve_native(&(image_bitmap), can_gc); } diff --git a/components/script/dom/imagebitmap.rs b/components/script/dom/imagebitmap.rs index 97a1a54bba7..cddd8cf1188 100644 --- a/components/script/dom/imagebitmap.rs +++ b/components/script/dom/imagebitmap.rs @@ -2,13 +2,13 @@ * 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 std::cell::{Cell, Ref}; use std::collections::HashMap; -use std::vec::Vec; use base::id::{ImageBitmapId, ImageBitmapIndex}; use constellation_traits::SerializableImageBitmap; use dom_struct::dom_struct; +use snapshot::Snapshot; use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::ImageBitmapMethods; @@ -23,41 +23,39 @@ 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) - bitmap_data: DomRefCell>>, + #[no_trace] + bitmap_data: DomRefCell>, origin_clean: Cell, } impl ImageBitmap { - fn new_inherited(width: u32, height: u32) -> ImageBitmap { + fn new_inherited(bitmap_data: Snapshot) -> ImageBitmap { ImageBitmap { reflector_: Reflector::new(), - width, - height, - bitmap_data: DomRefCell::new(Some(vec![])), + bitmap_data: DomRefCell::new(Some(bitmap_data)), origin_clean: Cell::new(true), } } pub(crate) fn new( global: &GlobalScope, - width: u32, - height: u32, + bitmap_data: Snapshot, can_gc: CanGc, ) -> DomRoot { - //assigning to a variable the return object of new_inherited - let imagebitmap = Box::new(ImageBitmap::new_inherited(width, height)); - - reflect_dom_object(imagebitmap, global, can_gc) + reflect_dom_object( + Box::new(ImageBitmap::new_inherited(bitmap_data)), + global, + can_gc, + ) } - pub(crate) fn set_bitmap_data(&self, data: Vec) { - *self.bitmap_data.borrow_mut() = Some(data); + #[allow(dead_code)] + pub(crate) fn bitmap_data(&self) -> Ref> { + self.bitmap_data.borrow() } pub(crate) fn origin_is_clean(&self) -> bool { @@ -94,8 +92,6 @@ impl Serializable for ImageBitmap { // Step 2. Set serialized.[[BitmapData]] to a copy of value's bitmap data. let serialized = SerializableImageBitmap { - width: self.width, - height: self.height, bitmap_data: self.bitmap_data.borrow().clone().unwrap(), }; @@ -108,12 +104,8 @@ impl Serializable for ImageBitmap { serialized: Self::Data, can_gc: CanGc, ) -> Result, ()> { - let image_bitmap = ImageBitmap::new(owner, serialized.width, serialized.height, can_gc); - // Step 1. Set value's bitmap data to serialized.[[BitmapData]]. - image_bitmap.set_bitmap_data(serialized.bitmap_data); - - Ok(image_bitmap) + Ok(ImageBitmap::new(owner, serialized.bitmap_data, can_gc)) } fn serialized_storage<'a>( @@ -153,8 +145,6 @@ impl Transferable for ImageBitmap { // Step 2. Set dataHolder.[[BitmapData]] to value's bitmap data. // Step 3. Unset value's bitmap data. let serialized = SerializableImageBitmap { - width: self.width, - height: self.height, bitmap_data: self.bitmap_data.borrow_mut().take().unwrap(), }; @@ -167,13 +157,12 @@ impl Transferable for ImageBitmap { _: ImageBitmapId, serialized: SerializableImageBitmap, ) -> Result, ()> { - let image_bitmap = - ImageBitmap::new(owner, serialized.width, serialized.height, CanGc::note()); - // Step 1. Set value's bitmap data to serialized.[[BitmapData]]. - image_bitmap.set_bitmap_data(serialized.bitmap_data); - - Ok(image_bitmap) + Ok(ImageBitmap::new( + owner, + serialized.bitmap_data, + CanGc::note(), + )) } fn serialized_storage<'a>( @@ -195,7 +184,13 @@ impl ImageBitmapMethods for ImageBitmap { } // Step 2. Return this's height, in CSS pixels. - self.height + self.bitmap_data + .borrow() + .as_ref() + .unwrap() + .size() + .cast() + .height } /// @@ -206,7 +201,13 @@ impl ImageBitmapMethods for ImageBitmap { } // Step 2. Return this's width, in CSS pixels. - self.width + self.bitmap_data + .borrow() + .as_ref() + .unwrap() + .size() + .cast() + .width } /// diff --git a/components/shared/constellation/Cargo.toml b/components/shared/constellation/Cargo.toml index 3b093b3f684..15a9c53bb83 100644 --- a/components/shared/constellation/Cargo.toml +++ b/components/shared/constellation/Cargo.toml @@ -31,6 +31,7 @@ net_traits = { workspace = true } profile_traits = { workspace = true } serde = { workspace = true } servo_url = { path = "../../url" } +snapshot = { workspace = true } strum = { workspace = true } strum_macros = { workspace = true } uuid = { workspace = true } diff --git a/components/shared/constellation/structured_data/serializable.rs b/components/shared/constellation/structured_data/serializable.rs index cbb932c52ec..e72d3593bb3 100644 --- a/components/shared/constellation/structured_data/serializable.rs +++ b/components/shared/constellation/structured_data/serializable.rs @@ -16,6 +16,7 @@ use malloc_size_of_derive::MallocSizeOf; use net_traits::filemanager_thread::RelativePos; use serde::{Deserialize, Serialize}; use servo_url::ImmutableOrigin; +use snapshot::Snapshot; use strum::EnumIter; use uuid::Uuid; @@ -321,9 +322,7 @@ impl BroadcastClone for DomException { #[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)] /// A serializable version of the ImageBitmap interface. pub struct SerializableImageBitmap { - pub width: u32, - pub height: u32, - pub bitmap_data: Vec, + pub bitmap_data: Snapshot, } impl BroadcastClone for SerializableImageBitmap { diff --git a/components/shared/snapshot/Cargo.toml b/components/shared/snapshot/Cargo.toml index 9d5bd555623..81b2125a3c8 100644 --- a/components/shared/snapshot/Cargo.toml +++ b/components/shared/snapshot/Cargo.toml @@ -15,5 +15,7 @@ path = "lib.rs" [dependencies] euclid = { workspace = true } ipc-channel = { workspace = true } +malloc_size_of = { workspace = true } +malloc_size_of_derive = { workspace = true } serde = { workspace = true } pixels = { path = "../../pixels" } diff --git a/components/shared/snapshot/lib.rs b/components/shared/snapshot/lib.rs index 5a8c1a5fbc0..7f0111f7a79 100644 --- a/components/shared/snapshot/lib.rs +++ b/components/shared/snapshot/lib.rs @@ -6,17 +6,18 @@ use std::ops::{Deref, DerefMut}; use euclid::default::Size2D; use ipc_channel::ipc::IpcSharedMemory; +use malloc_size_of_derive::MallocSizeOf; use pixels::Multiply; use serde::{Deserialize, Serialize}; -#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)] pub enum PixelFormat { #[default] RGBA, BGRA, } -#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)] pub enum AlphaMode { /// Internal data is opaque (alpha is cleared to 1) Opaque, @@ -48,7 +49,7 @@ impl AlphaMode { } } -#[derive(Debug)] +#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)] pub enum Data { // TODO: https://github.com/servo/servo/issues/36594 //IPC(IpcSharedMemory), @@ -84,7 +85,7 @@ pub type IpcSnapshot = Snapshot; /// /// Inspired by snapshot for concept in WebGPU spec: /// -#[derive(Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)] pub struct Snapshot { size: Size2D, /// internal data (can be any format it will be converted on use if needed)