Make canvas send their data themselves to other canvas

This commit is contained in:
Anthony Ramine 2016-05-28 13:53:09 +02:00
parent 2086d216dd
commit f8c3fe1076
7 changed files with 43 additions and 26 deletions

View file

@ -148,6 +148,12 @@ impl<'a> CanvasPaintThread<'a> {
Canvas2dMsg::DrawImageSelf(image_size, dest_rect, source_rect, smoothing_enabled) => {
painter.draw_image_self(image_size, dest_rect, source_rect, smoothing_enabled)
}
Canvas2dMsg::DrawImageInOther(
renderer, image_size, dest_rect, source_rect, smoothing, sender
) => {
painter.draw_image_in_other(
renderer, image_size, dest_rect, source_rect, smoothing, sender)
}
Canvas2dMsg::MoveTo(ref point) => painter.move_to(point),
Canvas2dMsg::LineTo(ref point) => painter.line_to(point),
Canvas2dMsg::Rect(ref rect) => painter.rect(rect),
@ -371,6 +377,26 @@ impl<'a> CanvasPaintThread<'a> {
}
}
fn draw_image_in_other(&self,
renderer: IpcSender<CanvasMsg>,
image_size: Size2D<f64>,
dest_rect: Rect<f64>,
source_rect: Rect<f64>,
smoothing_enabled: bool,
sender: IpcSender<()>) {
let mut image_data = self.read_pixels(source_rect.to_i32(), image_size);
// TODO: avoid double byte_swap.
byte_swap(&mut image_data);
let msg = CanvasMsg::Canvas2d(Canvas2dMsg::DrawImage(
image_data, source_rect.size, dest_rect, source_rect, smoothing_enabled));
renderer.send(msg).unwrap();
// We acknowledge to the caller here that the data was sent to the
// other canvas so that if JS immediately afterwards try to get the
// pixels of the other one, it won't retrieve the other values.
sender.send(()).unwrap();
}
fn move_to(&self, point: &Point2D<AzFloat>) {
self.path_builder.move_to(*point)
}

View file

@ -79,6 +79,8 @@ pub enum Canvas2dMsg {
ArcTo(Point2D<f32>, Point2D<f32>, f32),
DrawImage(Vec<u8>, Size2D<f64>, Rect<f64>, Rect<f64>, bool),
DrawImageSelf(Size2D<f64>, Rect<f64>, Rect<f64>, bool),
DrawImageInOther(
IpcSender<CanvasMsg>, Size2D<f64>, Rect<f64>, Rect<f64>, bool, IpcSender<()>),
BeginPath,
BezierCurveTo(Point2D<f32>, Point2D<f32>, Point2D<f32>),
ClearRect(Rect<f32>),

View file

@ -20,7 +20,6 @@ tinyfiledialogs = {git = "https://github.com/jdm/tinyfiledialogs"}
angle = {git = "https://github.com/servo/angle", branch = "servo"}
app_units = {version = "0.2.3", features = ["plugins"]}
bitflags = "0.7"
canvas = {path = "../canvas"}
canvas_traits = {path = "../canvas_traits"}
caseless = "0.1.0"
cssparser = {version = "0.5.4", features = ["heap_size", "serde-serialization"]}

View file

@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use canvas::canvas_paint_thread::RectToi32;
use canvas_traits::{Canvas2dMsg, CanvasCommonMsg, CanvasMsg};
use canvas_traits::{CompositionOrBlending, LineCapStyle, LineJoinStyle};
use canvas_traits::{FillOrStrokeStyle, FillRule, LinearGradientStyle, RadialGradientStyle, RepetitionStyle};
@ -363,36 +362,30 @@ impl CanvasRenderingContext2D {
let smoothing_enabled = self.state.borrow().image_smoothing_enabled;
// If the source and target canvas are the same
let msg = if &*self.canvas == canvas {
CanvasMsg::Canvas2d(Canvas2dMsg::DrawImageSelf(image_size,
dest_rect,
source_rect,
smoothing_enabled))
if &*self.canvas == canvas {
let msg = CanvasMsg::Canvas2d(Canvas2dMsg::DrawImageSelf(
image_size, dest_rect, source_rect, smoothing_enabled));
self.ipc_renderer.send(msg).unwrap();
} else {
// Source and target canvases are different
let context = match canvas.get_or_init_2d_context() {
Some(context) => context,
None => return Err(Error::InvalidState),
};
let (sender, receiver) = ipc::channel().unwrap();
let msg = CanvasMsg::Canvas2d(Canvas2dMsg::DrawImageInOther(
self.ipc_renderer.clone(),
image_size,
dest_rect,
source_rect,
smoothing_enabled,
sender));
let renderer = context.get_ipc_renderer();
let (sender, receiver) = ipc::channel::<Vec<u8>>().unwrap();
// Reads pixels from source image
renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::GetImageData(source_rect.to_i32(),
image_size,
sender)))
.unwrap();
let imagedata = receiver.recv().unwrap();
// Writes pixels to destination canvas
CanvasMsg::Canvas2d(Canvas2dMsg::DrawImage(imagedata,
source_rect.size,
dest_rect,
source_rect,
smoothing_enabled))
renderer.send(msg).unwrap();
receiver.recv().unwrap();
};
self.ipc_renderer.send(msg).unwrap();
self.mark_as_dirty();
Ok(())
}

View file

@ -34,7 +34,6 @@ extern crate app_units;
#[allow(unused_extern_crates)]
#[macro_use]
extern crate bitflags;
extern crate canvas;
extern crate canvas_traits;
extern crate caseless;
extern crate core;

View file

@ -1874,7 +1874,6 @@ dependencies = [
"angle 0.1.0 (git+https://github.com/servo/angle?branch=servo)",
"app_units 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"canvas 0.0.1",
"canvas_traits 0.0.1",
"caseless 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",

1
ports/cef/Cargo.lock generated
View file

@ -1732,7 +1732,6 @@ dependencies = [
"angle 0.1.0 (git+https://github.com/servo/angle?branch=servo)",
"app_units 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"canvas 0.0.1",
"canvas_traits 0.0.1",
"caseless 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",