Use IpcSharedMemory for Canvas2dMsg::DrawImage (#30544)

* Use `IpcSharedMemory` for `Canvas2DMsg::DrawImage`

* Fix `Canvas2dMsg::DrawEmptyImage` crashes

* Do not premultiply canvas image data

* Move `image_data` back to its original position
This commit is contained in:
Ennui Langeweile 2023-10-18 10:39:58 -03:00 committed by GitHub
parent 66258bfbbd
commit 2d7dfb06c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 25 deletions

View file

@ -446,11 +446,12 @@ impl<'a> CanvasData<'a> {
pub fn draw_image(
&mut self,
image_data: Vec<u8>,
image_data: &[u8],
image_size: Size2D<f64>,
dest_rect: Rect<f64>,
source_rect: Rect<f64>,
smoothing_enabled: bool,
premultiply: bool,
) {
// We round up the floating pixel values to draw the pixels
let source_rect = source_rect.ceil();
@ -469,6 +470,7 @@ impl<'a> CanvasData<'a> {
source_rect.size,
dest_rect,
smoothing_enabled,
premultiply,
&draw_options,
);
};
@ -1306,17 +1308,24 @@ pub struct CanvasPaintState<'a> {
/// image_size: The size of the image to be written
/// dest_rect: Area of the destination target where the pixels will be copied
/// smoothing_enabled: It determines if smoothing is applied to the image result
/// premultiply: Determines whenever the image data should be premultiplied or not
fn write_image(
draw_target: &mut dyn GenericDrawTarget,
image_data: Vec<u8>,
mut image_data: Vec<u8>,
image_size: Size2D<f64>,
dest_rect: Rect<f64>,
smoothing_enabled: bool,
premultiply: bool,
draw_options: &DrawOptions,
) {
if image_data.is_empty() {
return;
}
if premultiply {
pixels::rgba8_premultiply_inplace(&mut image_data);
}
let image_rect = Rect::new(Point2D::zero(), image_size);
// From spec https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage

View file

@ -175,22 +175,27 @@ impl<'a> CanvasPaintThread<'a> {
.canvas(canvas_id)
.is_point_in_path(x, y, fill_rule, chan),
Canvas2dMsg::DrawImage(
imagedata,
ref image_data,
image_size,
dest_rect,
source_rect,
smoothing_enabled,
) => {
let data = imagedata.map_or_else(
|| vec![0; image_size.width as usize * image_size.height as usize * 4],
|bytes| bytes.into_vec(),
);
) => self.canvas(canvas_id).draw_image(
&*image_data,
image_size,
dest_rect,
source_rect,
smoothing_enabled,
true,
),
Canvas2dMsg::DrawEmptyImage(image_size, dest_rect, source_rect) => {
self.canvas(canvas_id).draw_image(
data,
&vec![0; image_size.area() as usize * 4],
image_size,
dest_rect,
source_rect,
smoothing_enabled,
false,
false,
)
},
Canvas2dMsg::DrawImageInOther(
@ -204,11 +209,12 @@ impl<'a> CanvasPaintThread<'a> {
.canvas(canvas_id)
.read_pixels(source_rect.to_u64(), image_size.to_u64());
self.canvas(other_canvas_id).draw_image(
image_data.into(),
&image_data,
source_rect.size,
dest_rect,
source_rect,
smoothing,
false,
);
},
Canvas2dMsg::MoveTo(ref point) => self.canvas(canvas_id).move_to(point),