Skip to content

Commit

Permalink
Use IpcSharedMemory for Canvas2dMsg::DrawImage (servo#30544)
Browse files Browse the repository at this point in the history
* Use `IpcSharedMemory` for `Canvas2DMsg::DrawImage`

* Fix `Canvas2dMsg::DrawEmptyImage` crashes

* Do not premultiply canvas image data

* Move `image_data` back to its original position
  • Loading branch information
EnnuiL committed Oct 18, 2023
1 parent 66258bf commit 2d7dfb0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
13 changes: 11 additions & 2 deletions components/canvas/canvas_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -469,6 +470,7 @@ impl<'a> CanvasData<'a> {
source_rect.size,
dest_rect,
smoothing_enabled,
premultiply,
&draw_options,
);
};
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions components/canvas/canvas_paint_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion components/canvas_traits/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub struct CanvasImageData {
pub enum Canvas2dMsg {
Arc(Point2D<f32>, f32, f32, f32, bool),
ArcTo(Point2D<f32>, Point2D<f32>, f32),
DrawImage(Option<ByteBuf>, Size2D<f64>, Rect<f64>, Rect<f64>, bool),
DrawImage(IpcSharedMemory, Size2D<f64>, Rect<f64>, Rect<f64>, bool),
DrawEmptyImage(Size2D<f64>, Rect<f64>, Rect<f64>),
DrawImageInOther(CanvasId, Size2D<f64>, Rect<f64>, Rect<f64>, bool),
BeginPath,
BezierCurveTo(Point2D<f32>, Point2D<f32>, Point2D<f32>),
Expand Down
22 changes: 9 additions & 13 deletions components/script/canvas_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ use canvas_traits::canvas::{
use cssparser::{Color as CSSColor, Parser, ParserInput, RGBA};
use euclid::default::{Point2D, Rect, Size2D, Transform2D};
use euclid::vec2;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::ipc::{self, IpcSender, IpcSharedMemory};
use net_traits::image_cache::{ImageCache, ImageResponse};
use net_traits::request::CorsSettings;
use pixels::PixelFormat;
use profile_traits::ipc as profiled_ipc;
use script_traits::ScriptMsg;
use serde_bytes::ByteBuf;
use servo_url::{ImmutableOrigin, ServoUrl};
use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps;
use style::properties::style_structs::Font;
Expand Down Expand Up @@ -260,7 +259,7 @@ impl CanvasState {
&self,
url: ServoUrl,
cors_setting: Option<CorsSettings>,
) -> Option<(Vec<u8>, Size2D<u32>)> {
) -> Option<(IpcSharedMemory, Size2D<u32>)> {
let img = match self.request_image_from_cache(url, cors_setting) {
ImageResponse::Loaded(img, _) => img,
ImageResponse::PlaceholderLoaded(_, _) |
Expand All @@ -272,7 +271,7 @@ impl CanvasState {

let image_size = Size2D::new(img.width, img.height);
let image_data = match img.format {
PixelFormat::BGRA8 => img.bytes.to_vec(),
PixelFormat::BGRA8 => img.bytes.clone(),
pixel_format => unimplemented!("unsupported pixel format ({:?})", pixel_format),
};

Expand Down Expand Up @@ -492,12 +491,10 @@ impl CanvasState {
},
}
} else {
self.send_canvas_2d_msg(Canvas2dMsg::DrawImage(
None,
self.send_canvas_2d_msg(Canvas2dMsg::DrawEmptyImage(
image_size,
dest_rect,
source_rect,
smoothing_enabled,
));
}

Expand Down Expand Up @@ -554,12 +551,10 @@ impl CanvasState {
_ => return Err(Error::InvalidState),
}
} else {
self.send_canvas_2d_msg(Canvas2dMsg::DrawImage(
None,
self.send_canvas_2d_msg(Canvas2dMsg::DrawEmptyImage(
image_size,
dest_rect,
source_rect,
smoothing_enabled,
));
}

Expand All @@ -582,10 +577,9 @@ impl CanvasState {
dh: Option<f64>,
) -> ErrorResult {
debug!("Fetching image {}.", url);
let (mut image_data, image_size) = self
let (image_data, image_size) = self
.fetch_image_data(url, cors_setting)
.ok_or(Error::InvalidState)?;
pixels::rgba8_premultiply_inplace(&mut image_data);
let image_size = image_size.to_f64();

let dw = dw.unwrap_or(image_size.width);
Expand All @@ -603,7 +597,7 @@ impl CanvasState {

let smoothing_enabled = self.state.borrow().image_smoothing_enabled;
self.send_canvas_2d_msg(Canvas2dMsg::DrawImage(
Some(ByteBuf::from(image_data)),
image_data,
image_size,
dest_rect,
source_rect,
Expand Down Expand Up @@ -916,6 +910,7 @@ impl CanvasState {
.and_then(|url| {
self.fetch_image_data(url, cors_setting_for_element(image.upcast()))
})
.map(|data| (data.0.to_vec(), data.1))
.ok_or(Error::InvalidState)?
},
CanvasImageSource::HTMLCanvasElement(ref canvas) => {
Expand All @@ -935,6 +930,7 @@ impl CanvasState {
CanvasImageSource::CSSStyleValue(ref value) => value
.get_url(self.base_url.clone())
.and_then(|url| self.fetch_image_data(url, None))
.map(|data| (data.0.to_vec(), data.1))
.ok_or(Error::InvalidState)?,
};

Expand Down

0 comments on commit 2d7dfb0

Please sign in to comment.