Skip to content

Commit

Permalink
Make HTMLCanvasElement::fetch_all_data return a shared memory blob
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Nov 21, 2018
1 parent 804d964 commit 9a8d03a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
17 changes: 6 additions & 11 deletions components/canvas/canvas_data.rs
Expand Up @@ -13,9 +13,8 @@ use azure::azure_hl::{ExtendMode, GradientStop, LinearGradientPattern, RadialGra
use canvas_traits::canvas::*;
use cssparser::RGBA;
use euclid::{Point2D, Rect, Size2D, Transform2D, Vector2D};
use ipc_channel::ipc::IpcSender;
use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
use num_traits::ToPrimitive;
use serde_bytes::ByteBuf;
use std::mem;
use std::sync::Arc;

Expand Down Expand Up @@ -451,15 +450,11 @@ impl<'a> CanvasData<'a> {
}

#[allow(unsafe_code)]
pub fn send_pixels(&mut self, chan: IpcSender<Option<ByteBuf>>) {
let data = unsafe {
self.drawtarget
.snapshot()
.get_data_surface()
.data()
.to_vec()
};
chan.send(Some(data.into())).unwrap();
pub fn send_pixels(&mut self, chan: IpcSender<IpcSharedMemory>) {
let data = IpcSharedMemory::from_bytes(unsafe {
self.drawtarget.snapshot().get_data_surface().data()
});
chan.send(data).unwrap();
}

#[allow(unsafe_code)]
Expand Down
4 changes: 2 additions & 2 deletions components/canvas_traits/canvas.rs
Expand Up @@ -4,7 +4,7 @@

use cssparser::RGBA;
use euclid::{Point2D, Rect, Size2D, Transform2D};
use ipc_channel::ipc::{IpcBytesReceiver, IpcBytesSender, IpcSender};
use ipc_channel::ipc::{IpcBytesReceiver, IpcBytesSender, IpcSender, IpcSharedMemory};
use serde_bytes::ByteBuf;
use std::default::Default;
use std::str::FromStr;
Expand Down Expand Up @@ -87,7 +87,7 @@ pub enum FromLayoutMsg {

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum FromScriptMsg {
SendPixels(IpcSender<Option<ByteBuf>>),
SendPixels(IpcSender<IpcSharedMemory>),
}

#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
Expand Down
6 changes: 5 additions & 1 deletion components/script/dom/canvasrenderingcontext2d.rs
Expand Up @@ -1296,7 +1296,11 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
.ok_or(Error::InvalidState)?
},
CanvasImageSource::HTMLCanvasElement(ref canvas) => {
canvas.fetch_all_data().ok_or(Error::InvalidState)?
let (data, size) = canvas.fetch_all_data().ok_or(Error::InvalidState)?;
let data = data
.map(|data| data.to_vec())
.unwrap_or_else(|| vec![0; size.area() as usize * 4]);
(data, size)
},
CanvasImageSource::CSSStyleValue(ref value) => value
.get_url(self.base_url.clone())
Expand Down
7 changes: 4 additions & 3 deletions components/script/dom/htmlcanvaselement.rs
Expand Up @@ -36,6 +36,7 @@ use euclid::{Rect, Size2D};
use html5ever::{LocalName, Prefix};
use image::png::PNGEncoder;
use image::ColorType;
use ipc_channel::ipc::IpcSharedMemory;
use js::error::throw_type_error;
use js::jsapi::JSContext;
use js::rust::HandleValue;
Expand Down Expand Up @@ -280,7 +281,7 @@ impl HTMLCanvasElement {
self.Height() != 0 && self.Width() != 0
}

pub fn fetch_all_data(&self) -> Option<(Vec<u8>, Size2D<u32>)> {
pub fn fetch_all_data(&self) -> Option<(Option<IpcSharedMemory>, Size2D<u32>)> {
let size = self.get_size();

if size.width == 0 || size.height == 0 {
Expand All @@ -297,7 +298,7 @@ impl HTMLCanvasElement {
);
context.get_ipc_renderer().send(msg).unwrap();

receiver.recv().unwrap()?.into()
Some(receiver.recv().unwrap())
},
Some(&CanvasContext::WebGL(_)) => {
// TODO: add a method in WebGLRenderingContext to get the pixels.
Expand All @@ -307,7 +308,7 @@ impl HTMLCanvasElement {
// TODO: add a method in WebGL2RenderingContext to get the pixels.
return None;
},
None => vec![0; size.height as usize * size.width as usize * 4],
None => None,
};

Some((data, size))
Expand Down
10 changes: 4 additions & 6 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -569,12 +569,10 @@ impl WebGLRenderingContext {
return Err(Error::Security);
}
if let Some((data, size)) = canvas.fetch_all_data() {
TexPixels::new(
IpcSharedMemory::from_bytes(&data),
size,
PixelFormat::BGRA8,
true,
)
let data = data.unwrap_or_else(|| {
IpcSharedMemory::from_bytes(&vec![0; size.area() as usize * 4])
});
TexPixels::new(data, size, PixelFormat::BGRA8, true)
} else {
return Ok(None);
}
Expand Down

0 comments on commit 9a8d03a

Please sign in to comment.