pixels: Move Snapshot and related data structures to pixels (#37590)

1. The `shared` directory is for the "_traits" crates, which will likely
   be moved out of this directly at some point and renamed "_api". These
   crates expose the API of crates to avoid circular dependencies.
   `Snapshot` isn't really this.
2. `Snapshot` is essentially a specialied kind of `Image` so it makes
   sense that it is grouped with other image-related things in `pixels`.

Testing: This should not change any behavior so is covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-06-20 16:47:06 +02:00 committed by GitHub
parent 80a7de8c9c
commit 309485d2db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 123 additions and 154 deletions

View file

@ -113,7 +113,6 @@ servo_config = { path = "../config" }
servo_geometry = { path = "../geometry" }
servo_rand = { path = "../rand" }
servo_url = { path = "../url" }
snapshot = { workspace = true }
smallvec = { workspace = true, features = ["union"] }
strum = { workspace = true }
strum_macros = { workspace = true }

View file

@ -5,9 +5,9 @@
//! Common interfaces for Canvas Contexts
use euclid::default::Size2D;
use pixels::Snapshot;
use script_bindings::root::Dom;
use script_layout_interface::HTMLCanvasData;
use snapshot::Snapshot;
use webrender_api::ImageKey;
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;

View file

@ -20,7 +20,7 @@ use euclid::vec2;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::image_cache::{ImageCache, ImageResponse};
use net_traits::request::CorsSettings;
use pixels::PixelFormat;
use pixels::{PixelFormat, Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use profile_traits::ipc as profiled_ipc;
use servo_url::{ImmutableOrigin, ServoUrl};
use style::color::{AbsoluteColor, ColorFlags, ColorSpace};
@ -330,7 +330,7 @@ impl CanvasState {
&self,
url: ServoUrl,
cors_setting: Option<CorsSettings>,
) -> Option<snapshot::Snapshot> {
) -> Option<Snapshot> {
let img = match self.request_image_from_cache(url, cors_setting) {
ImageResponse::Loaded(image, _) => {
if let Some(image) = image.as_raster_image() {
@ -350,15 +350,15 @@ impl CanvasState {
let size = Size2D::new(img.metadata.width, img.metadata.height);
let format = match img.format {
PixelFormat::BGRA8 => snapshot::PixelFormat::BGRA,
PixelFormat::RGBA8 => snapshot::PixelFormat::RGBA,
PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
pixel_format => unimplemented!("unsupported pixel format ({:?})", pixel_format),
};
let alpha_mode = snapshot::AlphaMode::Transparent {
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
Some(snapshot::Snapshot::from_vec(
Some(Snapshot::from_vec(
size.cast(),
format,
alpha_mode,
@ -393,10 +393,10 @@ impl CanvasState {
self.send_canvas_2d_msg(Canvas2dMsg::GetImageData(rect, canvas_size, sender));
let mut snapshot = receiver.recv().unwrap().to_owned();
snapshot.transform(
snapshot::AlphaMode::Transparent {
SnapshotAlphaMode::Transparent {
premultiplied: false,
},
snapshot::PixelFormat::RGBA,
SnapshotPixelFormat::RGBA,
);
snapshot.to_vec()
}

View file

@ -6,10 +6,10 @@ use canvas_traits::canvas::{Canvas2dMsg, CanvasId, CanvasMsg, FromScriptMsg};
use dom_struct::dom_struct;
use euclid::default::Size2D;
use ipc_channel::ipc::IpcSender;
use pixels::Snapshot;
use profile_traits::ipc;
use script_bindings::inheritance::Castable;
use servo_url::ServoUrl;
use snapshot::Snapshot;
use webrender_api::ImageKey;
use crate::canvas_context::{CanvasContext, CanvasHelpers, LayoutCanvasRenderingContextHelpers};

View file

@ -60,11 +60,10 @@ use net_traits::{
CoreResourceMsg, CoreResourceThread, FetchResponseListener, IpcSend, ReferrerPolicy,
ResourceThreads, fetch_async,
};
use pixels::{CorsStatus, PixelFormat};
use pixels::{CorsStatus, PixelFormat, Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use profile_traits::{ipc as profile_ipc, mem as profile_mem, time as profile_time};
use script_bindings::interfaces::GlobalScopeHelpers;
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use snapshot::Snapshot;
use timers::{TimerEventRequest, TimerId};
use url::Origin;
use uuid::Uuid;
@ -3084,13 +3083,13 @@ impl GlobalScope {
let size = Size2D::new(img.metadata.width, img.metadata.height);
let format = match img.format {
PixelFormat::BGRA8 => snapshot::PixelFormat::BGRA,
PixelFormat::RGBA8 => snapshot::PixelFormat::RGBA,
PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
pixel_format => {
unimplemented!("unsupported pixel format ({:?})", pixel_format)
},
};
let alpha_mode = snapshot::AlphaMode::Transparent {
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
@ -3278,13 +3277,13 @@ impl GlobalScope {
let size = Size2D::new(img.metadata.width, img.metadata.height);
let format = match img.format {
PixelFormat::BGRA8 => snapshot::PixelFormat::BGRA,
PixelFormat::RGBA8 => snapshot::PixelFormat::RGBA,
PixelFormat::BGRA8 => SnapshotPixelFormat::BGRA,
PixelFormat::RGBA8 => SnapshotPixelFormat::RGBA,
pixel_format => {
unimplemented!("unsupported pixel format ({:?})", pixel_format)
},
};
let alpha_mode = snapshot::AlphaMode::Transparent {
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
@ -3319,13 +3318,13 @@ impl GlobalScope {
return p;
}
let alpha_mode = snapshot::AlphaMode::Transparent {
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
let snapshot = Snapshot::from_vec(
image_data.get_size().cast(),
snapshot::PixelFormat::RGBA,
SnapshotPixelFormat::RGBA,
alpha_mode,
image_data.to_vec(),
);

View file

@ -21,10 +21,10 @@ use image::{ColorType, ImageEncoder, ImageError};
use ipc_channel::ipc::{self as ipcchan};
use js::error::throw_type_error;
use js::rust::{HandleObject, HandleValue};
use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use script_layout_interface::HTMLCanvasData;
use servo_media::streams::MediaStreamType;
use servo_media::streams::registry::MediaStreamId;
use snapshot::Snapshot;
use style::attr::AttrValue;
use super::node::NodeDamage;
@ -543,15 +543,15 @@ impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
let image_type = EncodedImageType::from(mime_type);
snapshot.transform(
if image_type == EncodedImageType::Jpeg {
snapshot::AlphaMode::AsOpaque {
SnapshotAlphaMode::AsOpaque {
premultiplied: true,
}
} else {
snapshot::AlphaMode::Transparent {
SnapshotAlphaMode::Transparent {
premultiplied: false,
}
},
snapshot::PixelFormat::RGBA,
SnapshotPixelFormat::RGBA,
);
let mut url = format!("data:{};base64,", image_type.as_mime_type());
@ -627,8 +627,8 @@ impl HTMLCanvasElementMethods<crate::DomTypeHolder> for HTMLCanvasElement {
};
snapshot.transform(
snapshot::AlphaMode::Transparent{ premultiplied: false },
snapshot::PixelFormat::RGBA
SnapshotAlphaMode::Transparent { premultiplied: false },
SnapshotPixelFormat::RGBA
);
// Step 4.1: If result is non-null, then set result to a serialization of

View file

@ -19,10 +19,10 @@ use net_traits::{
FetchMetadata, FetchResponseListener, FetchResponseMsg, NetworkError, ResourceFetchTiming,
ResourceTimingType,
};
use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use script_layout_interface::{HTMLMediaData, MediaMetadata};
use servo_media::player::video::VideoFrame;
use servo_url::ServoUrl;
use snapshot::Snapshot;
use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use crate::document_loader::{LoadBlocker, LoadType};
@ -146,13 +146,13 @@ impl HTMLVideoElement {
Some(frame) => {
let size = Size2D::new(frame.get_width() as u32, frame.get_height() as u32);
if !frame.is_gl_texture() {
let alpha_mode = snapshot::AlphaMode::Transparent {
let alpha_mode = SnapshotAlphaMode::Transparent {
premultiplied: false,
};
Some(Snapshot::from_vec(
size.cast(),
snapshot::PixelFormat::BGRA,
SnapshotPixelFormat::BGRA,
alpha_mode,
frame.get_data().to_vec(),
))

View file

@ -9,7 +9,7 @@ use base::id::{ImageBitmapId, ImageBitmapIndex};
use constellation_traits::SerializableImageBitmap;
use dom_struct::dom_struct;
use euclid::default::{Point2D, Rect, Size2D};
use snapshot::Snapshot;
use pixels::{Snapshot, SnapshotAlphaMode, SnapshotPixelFormat};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
@ -246,18 +246,18 @@ impl ImageBitmap {
match options.premultiplyAlpha {
PremultiplyAlpha::Default | PremultiplyAlpha::Premultiply => {
output.transform(
snapshot::AlphaMode::Transparent {
SnapshotAlphaMode::Transparent {
premultiplied: true,
},
snapshot::PixelFormat::BGRA,
SnapshotPixelFormat::BGRA,
);
},
PremultiplyAlpha::None => {
output.transform(
snapshot::AlphaMode::Transparent {
SnapshotAlphaMode::Transparent {
premultiplied: false,
},
snapshot::PixelFormat::BGRA,
SnapshotPixelFormat::BGRA,
);
},
}

View file

@ -7,7 +7,7 @@ use std::cell::Cell;
use dom_struct::dom_struct;
use euclid::default::Size2D;
use js::rust::{HandleObject, HandleValue};
use snapshot::Snapshot;
use pixels::Snapshot;
use crate::canvas_context::{CanvasContext, OffscreenRenderingContext};
use crate::dom::bindings::cell::{DomRefCell, Ref};

View file

@ -7,7 +7,7 @@ use crate::canvas_context::CanvasContext;
use crate::dom::bindings::codegen::UnionTypes::HTMLCanvasElementOrOffscreenCanvas;
use canvas_traits::canvas::Canvas2dMsg;
use dom_struct::dom_struct;
use snapshot::Snapshot;
use pixels::Snapshot;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,

View file

@ -21,9 +21,9 @@ use js::jsapi::{JSObject, Type};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, NullValue, ObjectValue, UInt32Value};
use js::rust::{CustomAutoRooterGuard, HandleObject, MutableHandleValue};
use js::typedarray::{ArrayBufferView, CreateWith, Float32, Int32Array, Uint32, Uint32Array};
use pixels::Snapshot;
use script_bindings::interfaces::WebGL2RenderingContextHelpers;
use servo_config::pref;
use snapshot::Snapshot;
use url::Host;
use webrender_api::ImageKey;

View file

@ -30,10 +30,9 @@ use js::typedarray::{
TypedArrayElementCreator, Uint32Array,
};
use net_traits::image_cache::ImageResponse;
use pixels::{self, PixelFormat};
use pixels::{self, PixelFormat, Snapshot, SnapshotPixelFormat};
use serde::{Deserialize, Serialize};
use servo_config::pref;
use snapshot::Snapshot;
use webrender_api::ImageKey;
use crate::canvas_context::CanvasContext;
@ -586,8 +585,8 @@ impl WebGLRenderingContext {
let snapshot = snapshot.as_ipc();
let size = snapshot.size().cast();
let format = match snapshot.format() {
snapshot::PixelFormat::RGBA => PixelFormat::RGBA8,
snapshot::PixelFormat::BGRA => PixelFormat::BGRA8,
SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
};
let premultiply = snapshot.alpha_mode().is_premultiplied();
TexPixels::new(snapshot.to_ipc_shared_memory(), size, format, premultiply)
@ -666,8 +665,8 @@ impl WebGLRenderingContext {
let snapshot = snapshot.as_ipc();
let size = snapshot.size().cast();
let format = match snapshot.format() {
snapshot::PixelFormat::RGBA => PixelFormat::RGBA8,
snapshot::PixelFormat::BGRA => PixelFormat::BGRA8,
SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
};
let premultiply = snapshot.alpha_mode().is_premultiplied();
TexPixels::new(snapshot.to_ipc_shared_memory(), size, format, premultiply)
@ -687,8 +686,8 @@ impl WebGLRenderingContext {
let snapshot = snapshot.as_ipc();
let size = snapshot.size().cast();
let format: PixelFormat = match snapshot.format() {
snapshot::PixelFormat::RGBA => PixelFormat::RGBA8,
snapshot::PixelFormat::BGRA => PixelFormat::BGRA8,
SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
};
let premultiply = snapshot.alpha_mode().is_premultiplied();
TexPixels::new(snapshot.to_ipc_shared_memory(), size, format, premultiply)
@ -1996,7 +1995,7 @@ impl CanvasContext for WebGLRenderingContext {
let (data, alpha_mode) = receiver.recv().unwrap();
Some(Snapshot::from_vec(
size.cast(),
snapshot::PixelFormat::RGBA,
SnapshotPixelFormat::RGBA,
alpha_mode,
data.to_vec(),
))

View file

@ -8,7 +8,7 @@ use std::cell::RefCell;
use arrayvec::ArrayVec;
use dom_struct::dom_struct;
use ipc_channel::ipc::{self};
use snapshot::Snapshot;
use pixels::Snapshot;
use webgpu_traits::{
ContextConfiguration, PRESENTATION_BUFFER_COUNT, WebGPU, WebGPUContextId, WebGPURequest,
WebGPUTexture,