Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ servo-media-gstreamer = { git = "https://github.com/servo/media" }
servo_arc = { git = "https://github.com/servo/stylo", branch = "2025-03-15" }
smallbitvec = "2.6.0"
smallvec = "1.15"
snapshot = { path = "./components/shared/snapshot" }
static_assertions = "1.1"
string_cache = "0.8"
string_cache_codegen = "0.5"
Expand Down
1 change: 1 addition & 0 deletions components/canvas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pixels = { path = "../pixels" }
range = { path = "../range" }
raqote = "0.8.5"
servo_arc = { workspace = true }
snapshot = { workspace = true }
stylo = { workspace = true }
surfman = { workspace = true }
unicode-script = { workspace = true }
Expand Down
65 changes: 39 additions & 26 deletions components/canvas/canvas_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use log::warn;
use num_traits::ToPrimitive;
use range::Range;
use servo_arc::Arc as ServoArc;
use snapshot::Snapshot;
use style::color::AbsoluteColor;
use style::properties::style_structs::Font as FontStyleStruct;
use unicode_script::Script;
Expand Down Expand Up @@ -521,8 +522,7 @@ pub trait GenericDrawTarget {
stroke_options: &StrokeOptions,
draw_options: &DrawOptions,
);
fn snapshot_data(&self, f: &dyn Fn(&[u8]) -> Vec<u8>) -> Vec<u8>;
fn snapshot_data_owned(&self) -> Vec<u8>;
fn snapshot_data(&self) -> &[u8];
}

pub enum GradientStop {
Expand Down Expand Up @@ -605,9 +605,8 @@ impl<'a> CanvasData<'a> {
offset: 0,
flags: ImageDescriptorFlags::empty(),
};
let data = SerializableImageData::Raw(IpcSharedMemory::from_bytes(
&draw_target.snapshot_data_owned(),
));
let data =
SerializableImageData::Raw(IpcSharedMemory::from_bytes(draw_target.snapshot_data()));
compositor_api.update_images(vec![ImageUpdate::AddImage(image_key, descriptor, data)]);
CanvasData {
backend,
Expand All @@ -628,7 +627,7 @@ impl<'a> CanvasData<'a> {
pub fn draw_image(
&mut self,
image_data: &[u8],
image_size: Size2D<f64>,
image_size: Size2D<u64>,
dest_rect: Rect<f64>,
source_rect: Rect<f64>,
smoothing_enabled: bool,
Expand All @@ -637,8 +636,8 @@ impl<'a> CanvasData<'a> {
// We round up the floating pixel values to draw the pixels
let source_rect = source_rect.ceil();
// It discards the extra pixels (if any) that won't be painted
let image_data = if Rect::from_size(image_size).contains_rect(&source_rect) {
pixels::rgba8_get_rect(image_data, image_size.to_u64(), source_rect.to_u64()).into()
let image_data = if Rect::from_size(image_size.to_f64()).contains_rect(&source_rect) {
pixels::rgba8_get_rect(image_data, image_size, source_rect.to_u64()).into()
} else {
image_data.into()
};
Expand Down Expand Up @@ -1412,12 +1411,8 @@ impl<'a> CanvasData<'a> {
self.update_image_rendering();
}

pub fn send_pixels(&mut self, chan: IpcSender<IpcSharedMemory>) {
self.drawtarget.snapshot_data(&|bytes| {
let data = IpcSharedMemory::from_bytes(bytes);
chan.send(data).unwrap();
vec![]
});
pub fn snapshot(&self) {
self.drawtarget.snapshot_data();
}

/// Update image in WebRender
Expand All @@ -1430,7 +1425,7 @@ impl<'a> CanvasData<'a> {
flags: ImageDescriptorFlags::empty(),
};
let data = SerializableImageData::Raw(IpcSharedMemory::from_bytes(
&self.drawtarget.snapshot_data_owned(),
self.drawtarget.snapshot_data(),
));

self.compositor_api
Expand Down Expand Up @@ -1529,18 +1524,36 @@ impl<'a> CanvasData<'a> {
/// canvas_size: The size of the canvas we're reading from
/// read_rect: The area of the canvas we want to read from
#[allow(unsafe_code)]
pub fn read_pixels(&self, read_rect: Rect<u64>, canvas_size: Size2D<u64>) -> Vec<u8> {
let canvas_rect = Rect::from_size(canvas_size);
if canvas_rect
.intersection(&read_rect)
.is_none_or(|rect| rect.is_empty())
{
return vec![];
}
pub fn read_pixels(
&self,
read_rect: Option<Rect<u64>>,
canvas_size: Option<Size2D<u64>>,
) -> Snapshot {
let canvas_size = canvas_size.unwrap_or(self.drawtarget.get_size().cast());

let data = if let Some(read_rect) = read_rect {
let canvas_rect = Rect::from_size(canvas_size);
if canvas_rect
.intersection(&read_rect)
.is_none_or(|rect| rect.is_empty())
{
vec![]
} else {
let bytes = self.drawtarget.snapshot_data();
pixels::rgba8_get_rect(bytes, canvas_size, read_rect).to_vec()
}
} else {
self.drawtarget.snapshot_data().to_vec()
};

self.drawtarget.snapshot_data(&|bytes| {
pixels::rgba8_get_rect(bytes, canvas_size, read_rect).into_owned()
})
Snapshot::from_vec(
canvas_size,
snapshot::PixelFormat::BGRA,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit weird to see the bgra8 format here when we call a function named rgba8_get_rect just above this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rgba8_get_rect just extracts subimage (pixels) of image regardless of pixel inner structure (pixel just needs to be 4B), if input image is BGRA it's result will also be BGRA and that's whats happening here.

snapshot::AlphaMode::Transparent {
premultiplied: true,
},
data,
)
}
}

Expand Down
45 changes: 24 additions & 21 deletions components/canvas/canvas_paint_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ impl<'a> CanvasPaintThread<'a> {
},
Ok(CanvasMsg::FromScript(message, canvas_id)) => match message {
FromScriptMsg::SendPixels(chan) => {
canvas_paint_thread.canvas(canvas_id).send_pixels(chan);
chan.send(canvas_paint_thread
.canvas(canvas_id)
.read_pixels(None, None)
.as_ipc()
).unwrap();
},
},
Err(e) => {
Expand Down Expand Up @@ -159,24 +163,21 @@ impl<'a> CanvasPaintThread<'a> {
Canvas2dMsg::IsPointInPath(path, x, y, fill_rule, chan) => self
.canvas(canvas_id)
.is_point_in_path_(&path[..], x, y, fill_rule, chan),
Canvas2dMsg::DrawImage(
ref image_data,
image_size,
dest_rect,
source_rect,
smoothing_enabled,
) => self.canvas(canvas_id).draw_image(
image_data,
image_size,
dest_rect,
source_rect,
smoothing_enabled,
true,
),
Canvas2dMsg::DrawImage(snapshot, dest_rect, source_rect, smoothing_enabled) => {
let snapshot = snapshot.to_owned();
self.canvas(canvas_id).draw_image(
snapshot.data(),
snapshot.size(),
dest_rect,
source_rect,
smoothing_enabled,
!snapshot.alpha_mode().is_premultiplied(),
)
},
Canvas2dMsg::DrawEmptyImage(image_size, dest_rect, source_rect) => {
self.canvas(canvas_id).draw_image(
&vec![0; image_size.area() as usize * 4],
image_size,
image_size.to_u64(),
dest_rect,
source_rect,
false,
Expand All @@ -192,10 +193,10 @@ impl<'a> CanvasPaintThread<'a> {
) => {
let image_data = self
.canvas(canvas_id)
.read_pixels(source_rect.to_u64(), image_size.to_u64());
.read_pixels(Some(source_rect.to_u64()), Some(image_size.to_u64()));
self.canvas(other_canvas_id).draw_image(
&image_data,
source_rect.size,
image_data.data(),
source_rect.size.to_u64(),
dest_rect,
source_rect,
smoothing,
Expand Down Expand Up @@ -244,8 +245,10 @@ impl<'a> CanvasPaintThread<'a> {
self.canvas(canvas_id).set_global_composition(op)
},
Canvas2dMsg::GetImageData(dest_rect, canvas_size, sender) => {
let pixels = self.canvas(canvas_id).read_pixels(dest_rect, canvas_size);
sender.send(&pixels).unwrap();
let snapshot = self
.canvas(canvas_id)
.read_pixels(Some(dest_rect), Some(canvas_size));
sender.send(snapshot.as_ipc()).unwrap();
},
Canvas2dMsg::PutImageData(rect, receiver) => {
self.canvas(canvas_id)
Expand Down
17 changes: 3 additions & 14 deletions components/canvas/raqote_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ impl GenericDrawTarget for raqote::DrawTarget {
self.set_transform(matrix);
}
fn snapshot(&self) -> SourceSurface {
SourceSurface::Raqote(self.snapshot_data_owned())
SourceSurface::Raqote(self.snapshot_data().to_vec())
}
fn stroke(
&mut self,
Expand Down Expand Up @@ -694,20 +694,9 @@ impl GenericDrawTarget for raqote::DrawTarget {
);
}
#[allow(unsafe_code)]
fn snapshot_data(&self, f: &dyn Fn(&[u8]) -> Vec<u8>) -> Vec<u8> {
fn snapshot_data(&self) -> &[u8] {
let v = self.get_data();
f(
unsafe {
std::slice::from_raw_parts(v.as_ptr() as *const u8, std::mem::size_of_val(v))
},
)
}
#[allow(unsafe_code)]
fn snapshot_data_owned(&self) -> Vec<u8> {
let v = self.get_data();
unsafe {
std::slice::from_raw_parts(v.as_ptr() as *const u8, std::mem::size_of_val(v)).into()
}
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, std::mem::size_of_val(v)) }
}
}

Expand Down
9 changes: 8 additions & 1 deletion components/canvas/webgl_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use glow::{
bytes_per_type, components_per_format,
};
use half::f16;
use ipc_channel::ipc::IpcSharedMemory;
use log::{debug, error, trace, warn};
use pixels::{self, PixelFormat, unmultiply_inplace};
use surfman::chains::{PreserveBuffer, SwapChains, SwapChainsAPI};
Expand Down Expand Up @@ -1212,7 +1213,13 @@ impl WebGLImpl {
glow::PixelPackData::Slice(Some(&mut pixels)),
)
};
sender.send(&pixels).unwrap();
let alpha_mode = match (attributes.alpha, attributes.premultiplied_alpha) {
(true, premultiplied) => snapshot::AlphaMode::Transparent { premultiplied },
(false, _) => snapshot::AlphaMode::Opaque,
};
sender
.send((IpcSharedMemory::from_bytes(&pixels), alpha_mode))
.unwrap();
},
WebGLCommand::ReadPixelsPP(rect, format, pixel_type, offset) => unsafe {
gl.read_pixels(
Expand Down
Loading