mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
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:
parent
80a7de8c9c
commit
309485d2db
32 changed files with 123 additions and 154 deletions
|
@ -26,7 +26,6 @@ pixels = { path = "../../pixels" }
|
|||
serde = { workspace = true }
|
||||
serde_bytes = { workspace = true }
|
||||
servo_config = { path = "../../config" }
|
||||
snapshot = { workspace = true }
|
||||
stylo = { workspace = true }
|
||||
webrender_api = { workspace = true }
|
||||
webxr-api = { workspace = true, features = ["ipc"] }
|
||||
|
|
|
@ -8,9 +8,9 @@ use std::str::FromStr;
|
|||
use euclid::default::{Point2D, Rect, Size2D, Transform2D};
|
||||
use ipc_channel::ipc::{IpcBytesReceiver, IpcSender};
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use pixels::IpcSnapshot;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_bytes::ByteBuf;
|
||||
use snapshot::IpcSnapshot;
|
||||
use style::color::AbsoluteColor;
|
||||
use style::properties::style_structs::Font as FontStyleStruct;
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ use glow::{
|
|||
};
|
||||
use ipc_channel::ipc::{IpcBytesReceiver, IpcBytesSender, IpcSender, IpcSharedMemory};
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use pixels::PixelFormat;
|
||||
use pixels::{PixelFormat, SnapshotAlphaMode};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use webrender_api::ImageKey;
|
||||
use webxr_api::{
|
||||
|
@ -302,7 +302,7 @@ pub enum WebGLCommand {
|
|||
Rect<u32>,
|
||||
u32,
|
||||
u32,
|
||||
IpcSender<(IpcSharedMemory, snapshot::AlphaMode)>,
|
||||
IpcSender<(IpcSharedMemory, SnapshotAlphaMode)>,
|
||||
),
|
||||
ReadPixelsPP(Rect<i32>, u32, u32, usize),
|
||||
SampleCoverage(f32, bool),
|
||||
|
|
|
@ -28,10 +28,10 @@ log = { workspace = true }
|
|||
malloc_size_of = { workspace = true }
|
||||
malloc_size_of_derive = { workspace = true }
|
||||
net_traits = { workspace = true }
|
||||
pixels = { path = "../../pixels" }
|
||||
profile_traits = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
servo_url = { path = "../../url" }
|
||||
snapshot = { workspace = true }
|
||||
strum = { workspace = true }
|
||||
strum_macros = { workspace = true }
|
||||
uuid = { workspace = true }
|
||||
|
|
|
@ -14,9 +14,9 @@ use std::path::PathBuf;
|
|||
use base::id::{BlobId, DomExceptionId, DomPointId, ImageBitmapId};
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use net_traits::filemanager_thread::RelativePos;
|
||||
use pixels::Snapshot;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use servo_url::ImmutableOrigin;
|
||||
use snapshot::Snapshot;
|
||||
use strum::EnumIter;
|
||||
use uuid::Uuid;
|
||||
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
[package]
|
||||
name = "snapshot"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
license.workspace = true
|
||||
edition.workspace = true
|
||||
publish.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "snapshot"
|
||||
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" }
|
|
@ -1,296 +0,0 @@
|
|||
/* 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::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, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum PixelFormat {
|
||||
#[default]
|
||||
RGBA,
|
||||
BGRA,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
|
||||
pub enum AlphaMode {
|
||||
/// Internal data is opaque (alpha is cleared to 1)
|
||||
Opaque,
|
||||
/// Internal data should be threated as opaque (does not mean it actually is)
|
||||
AsOpaque { premultiplied: bool },
|
||||
/// Data is not opaque
|
||||
Transparent { premultiplied: bool },
|
||||
}
|
||||
|
||||
impl Default for AlphaMode {
|
||||
fn default() -> Self {
|
||||
Self::Transparent {
|
||||
premultiplied: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AlphaMode {
|
||||
pub const fn is_premultiplied(&self) -> bool {
|
||||
match self {
|
||||
AlphaMode::Opaque => true,
|
||||
AlphaMode::AsOpaque { premultiplied } => *premultiplied,
|
||||
AlphaMode::Transparent { premultiplied } => *premultiplied,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn is_opaque(&self) -> bool {
|
||||
matches!(self, AlphaMode::Opaque | AlphaMode::AsOpaque { .. })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub enum Data {
|
||||
// TODO: https://github.com/servo/servo/issues/36594
|
||||
//IPC(IpcSharedMemory),
|
||||
Owned(Vec<u8>),
|
||||
}
|
||||
|
||||
impl Deref for Data {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match &self {
|
||||
//Data::IPC(ipc_shared_memory) => ipc_shared_memory,
|
||||
Data::Owned(items) => items,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Data {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
match self {
|
||||
//Data::IPC(ipc_shared_memory) => unsafe { ipc_shared_memory.deref_mut() },
|
||||
Data::Owned(items) => items,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type IpcSnapshot = Snapshot<IpcSharedMemory>;
|
||||
|
||||
/// Represents image bitmap with metadata, usually as snapshot of canvas
|
||||
///
|
||||
/// This allows us to hold off conversions (BGRA <-> RGBA, (un)premultiply)
|
||||
/// to when/if they are actually needed (WebGL/WebGPU can load both BGRA and RGBA).
|
||||
///
|
||||
/// Inspired by snapshot for concept in WebGPU spec:
|
||||
/// <https://gpuweb.github.io/gpuweb/#abstract-opdef-get-a-copy-of-the-image-contents-of-a-context>
|
||||
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
|
||||
pub struct Snapshot<T = Data> {
|
||||
size: Size2D<u32>,
|
||||
/// internal data (can be any format it will be converted on use if needed)
|
||||
data: T,
|
||||
/// RGBA/BGRA (reflect internal data)
|
||||
format: PixelFormat,
|
||||
/// How to treat alpha channel
|
||||
alpha_mode: AlphaMode,
|
||||
}
|
||||
|
||||
impl<T> Snapshot<T> {
|
||||
pub const fn size(&self) -> Size2D<u32> {
|
||||
self.size
|
||||
}
|
||||
|
||||
pub const fn format(&self) -> PixelFormat {
|
||||
self.format
|
||||
}
|
||||
|
||||
pub const fn alpha_mode(&self) -> AlphaMode {
|
||||
self.alpha_mode
|
||||
}
|
||||
|
||||
pub const fn is_premultiplied(&self) -> bool {
|
||||
self.alpha_mode().is_premultiplied()
|
||||
}
|
||||
|
||||
pub const fn is_opaque(&self) -> bool {
|
||||
self.alpha_mode().is_opaque()
|
||||
}
|
||||
}
|
||||
|
||||
impl Snapshot<Data> {
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
size: Size2D::zero(),
|
||||
data: Data::Owned(vec![]),
|
||||
format: PixelFormat::RGBA,
|
||||
alpha_mode: AlphaMode::Transparent {
|
||||
premultiplied: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns snapshot with provided size that is black transparent alpha
|
||||
pub fn cleared(size: Size2D<u32>) -> Self {
|
||||
Self {
|
||||
size,
|
||||
data: Data::Owned(vec![0; size.area() as usize * 4]),
|
||||
format: PixelFormat::RGBA,
|
||||
alpha_mode: AlphaMode::Transparent {
|
||||
premultiplied: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_vec(
|
||||
size: Size2D<u32>,
|
||||
format: PixelFormat,
|
||||
alpha_mode: AlphaMode,
|
||||
data: Vec<u8>,
|
||||
) -> Self {
|
||||
Self {
|
||||
size,
|
||||
data: Data::Owned(data),
|
||||
format,
|
||||
alpha_mode,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: https://github.com/servo/servo/issues/36594
|
||||
/*
|
||||
/// # Safety
|
||||
///
|
||||
/// This is safe if data is owned by this process only
|
||||
/// (ownership is transferred on send)
|
||||
pub unsafe fn from_shared_memory(
|
||||
size: Size2D<u32>,
|
||||
format: PixelFormat,
|
||||
alpha_mode: AlphaMode,
|
||||
ism: IpcSharedMemory,
|
||||
) -> Self {
|
||||
Self {
|
||||
size,
|
||||
data: Data::IPC(ism),
|
||||
format,
|
||||
alpha_mode,
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn data(&self) -> &[u8] {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self) -> &mut [u8] {
|
||||
&mut self.data
|
||||
}
|
||||
|
||||
/// Convert inner data of snapshot to target format and alpha mode.
|
||||
/// If data is already in target format and alpha mode no work will be done.
|
||||
pub fn transform(&mut self, target_alpha_mode: AlphaMode, target_format: PixelFormat) {
|
||||
let swap_rb = target_format != self.format;
|
||||
let multiply = match (self.alpha_mode, target_alpha_mode) {
|
||||
(AlphaMode::Opaque, _) => Multiply::None,
|
||||
(alpha_mode, AlphaMode::Opaque) => {
|
||||
if alpha_mode.is_premultiplied() {
|
||||
Multiply::UnMultiply
|
||||
} else {
|
||||
Multiply::None
|
||||
}
|
||||
},
|
||||
(
|
||||
AlphaMode::Transparent { premultiplied } | AlphaMode::AsOpaque { premultiplied },
|
||||
AlphaMode::Transparent {
|
||||
premultiplied: target_premultiplied,
|
||||
} |
|
||||
AlphaMode::AsOpaque {
|
||||
premultiplied: target_premultiplied,
|
||||
},
|
||||
) => {
|
||||
if premultiplied == target_premultiplied {
|
||||
Multiply::None
|
||||
} else if target_premultiplied {
|
||||
Multiply::PreMultiply
|
||||
} else {
|
||||
Multiply::UnMultiply
|
||||
}
|
||||
},
|
||||
};
|
||||
let clear_alpha = !matches!(self.alpha_mode, AlphaMode::Opaque) &&
|
||||
matches!(target_alpha_mode, AlphaMode::Opaque);
|
||||
pixels::transform_inplace(self.data.deref_mut(), multiply, swap_rb, clear_alpha);
|
||||
self.alpha_mode = target_alpha_mode;
|
||||
self.format = target_format;
|
||||
}
|
||||
|
||||
pub fn as_ipc(self) -> Snapshot<IpcSharedMemory> {
|
||||
let Snapshot {
|
||||
size,
|
||||
data,
|
||||
format,
|
||||
alpha_mode,
|
||||
} = self;
|
||||
let data = match data {
|
||||
//Data::IPC(ipc_shared_memory) => ipc_shared_memory,
|
||||
Data::Owned(items) => IpcSharedMemory::from_bytes(&items),
|
||||
};
|
||||
Snapshot {
|
||||
size,
|
||||
data,
|
||||
format,
|
||||
alpha_mode,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_vec(self) -> Vec<u8> {
|
||||
match self.data {
|
||||
Data::Owned(data) => data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Snapshot<IpcSharedMemory> {
|
||||
// TODO: https://github.com/servo/servo/issues/36594
|
||||
/*
|
||||
/// # Safety
|
||||
///
|
||||
/// This is safe if data is owned by this process only
|
||||
/// (ownership is transferred on send)
|
||||
pub unsafe fn to_data(self) -> Snapshot<Data> {
|
||||
let Snapshot {
|
||||
size,
|
||||
data,
|
||||
format,
|
||||
alpha_mode,
|
||||
} = self;
|
||||
Snapshot {
|
||||
size,
|
||||
data: Data::IPC(data),
|
||||
format,
|
||||
alpha_mode,
|
||||
}
|
||||
}
|
||||
*/
|
||||
pub fn to_owned(self) -> Snapshot<Data> {
|
||||
let Snapshot {
|
||||
size,
|
||||
data,
|
||||
format,
|
||||
alpha_mode,
|
||||
} = self;
|
||||
Snapshot {
|
||||
size,
|
||||
data: Data::Owned(data.to_vec()),
|
||||
format,
|
||||
alpha_mode,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn data(&self) -> &[u8] {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn to_ipc_shared_memory(self) -> IpcSharedMemory {
|
||||
self.data
|
||||
}
|
||||
}
|
|
@ -16,8 +16,8 @@ arrayvec = { workspace = true }
|
|||
base = { workspace = true }
|
||||
ipc-channel = { workspace = true }
|
||||
malloc_size_of = { workspace = true }
|
||||
pixels = { path = "../../pixels" }
|
||||
serde = { workspace = true }
|
||||
webrender_api = { workspace = true }
|
||||
wgpu-core = { workspace = true, features = ["serde", "wgsl"] }
|
||||
wgpu-types = { workspace = true }
|
||||
snapshot = { workspace = true }
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
use arrayvec::ArrayVec;
|
||||
use base::id::PipelineId;
|
||||
use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
|
||||
use pixels::IpcSnapshot;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use snapshot::IpcSnapshot;
|
||||
use webrender_api::ImageKey;
|
||||
use webrender_api::units::DeviceIntSize;
|
||||
use wgpu_core::Label;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue