Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,326 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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_paint_task::{FillOrStrokeStyle, LineCapStyle, LineJoinStyle}; | ||
use geom::matrix2d::Matrix2D; | ||
use geom::point::Point2D; | ||
use geom::rect::Rect; | ||
use geom::size::Size2D; | ||
use std::sync::mpsc::{Sender}; | ||
|
||
#[derive(Clone)] | ||
pub enum CanvasMsg { | ||
Canvas2d(Canvas2dMsg), | ||
Common(CanvasCommonMsg), | ||
WebGL(CanvasWebGLMsg), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub enum Canvas2dMsg { | ||
Arc(Point2D<f32>, f32, f32, f32, bool), | ||
ArcTo(Point2D<f32>, Point2D<f32>, f32), | ||
DrawImage(Vec<u8>, Size2D<f64>, Rect<f64>, Rect<f64>, bool), | ||
DrawImageSelf(Size2D<f64>, Rect<f64>, Rect<f64>, bool), | ||
BeginPath, | ||
BezierCurveTo(Point2D<f32>, Point2D<f32>, Point2D<f32>), | ||
ClosePath, | ||
ClearRect(Rect<f32>), | ||
Fill, | ||
FillRect(Rect<f32>), | ||
GetImageData(Rect<f64>, Size2D<f64>, Sender<Vec<u8>>), | ||
LineTo(Point2D<f32>), | ||
MoveTo(Point2D<f32>), | ||
PutImageData(Vec<u8>, Rect<f64>, Option<Rect<f64>>), | ||
QuadraticCurveTo(Point2D<f32>, Point2D<f32>), | ||
RestoreContext, | ||
SaveContext, | ||
StrokeRect(Rect<f32>), | ||
Stroke, | ||
SetFillStyle(FillOrStrokeStyle), | ||
SetStrokeStyle(FillOrStrokeStyle), | ||
SetLineWidth(f32), | ||
SetLineCap(LineCapStyle), | ||
SetLineJoin(LineJoinStyle), | ||
SetMiterLimit(f32), | ||
SetGlobalAlpha(f32), | ||
SetTransform(Matrix2D<f32>), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub enum CanvasWebGLMsg { | ||
Clear(u32), | ||
ClearColor(f32, f32, f32, f32), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub enum CanvasCommonMsg { | ||
Close, | ||
Recreate(Size2D<i32>), | ||
SendPixelContents(Sender<Vec<u8>>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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_msg::{CanvasWebGLMsg, CanvasCommonMsg, CanvasMsg}; | ||
use geom::size::Size2D; | ||
|
||
use gleam::gl; | ||
use gleam::gl::types::{GLint, GLsizei}; | ||
|
||
use util::task::spawn_named; | ||
|
||
use std::borrow::ToOwned; | ||
use std::sync::mpsc::{channel, Sender}; | ||
use util::vec::byte_swap; | ||
|
||
use glutin::{HeadlessRendererBuilder}; | ||
|
||
pub struct WebGLPaintTask { | ||
size: Size2D<i32>, | ||
} | ||
|
||
impl WebGLPaintTask { | ||
fn new(size: Size2D<i32>) -> WebGLPaintTask { | ||
WebGLPaintTask::create(size); | ||
WebGLPaintTask { | ||
size: size, | ||
} | ||
} | ||
|
||
pub fn start(size: Size2D<i32>) -> Sender<CanvasMsg> { | ||
let (chan, port) = channel::<CanvasMsg>(); | ||
spawn_named("WebGLTask".to_owned(), move || { | ||
let mut painter = WebGLPaintTask::new(size); | ||
painter.init(); | ||
loop { | ||
match port.recv().unwrap() { | ||
CanvasMsg::WebGL(message) => { | ||
match message { | ||
CanvasWebGLMsg::Clear(mask) => painter.clear(mask), | ||
CanvasWebGLMsg::ClearColor(r, g, b, a) => painter.clear_color(r, g, b, a), | ||
} | ||
}, | ||
CanvasMsg::Common(message) => { | ||
match message { | ||
CanvasCommonMsg::Close => break, | ||
CanvasCommonMsg::SendPixelContents(chan) => painter.send_pixel_contents(chan), | ||
CanvasCommonMsg::Recreate(size) => painter.recreate(size), | ||
} | ||
}, | ||
CanvasMsg::Canvas2d(_) => panic!("Wrong message sent to WebGLTask"), | ||
} | ||
} | ||
}); | ||
chan | ||
} | ||
|
||
fn create(size: Size2D<i32>) { | ||
// It creates OpenGL context | ||
let context = HeadlessRendererBuilder::new(size.width as u32, size.height as u32).build().unwrap(); | ||
unsafe { | ||
context.make_current(); | ||
} | ||
} | ||
|
||
fn init(&self) { | ||
let framebuffer_ids = gl::gen_framebuffers(1); | ||
gl::bind_framebuffer(gl::FRAMEBUFFER, framebuffer_ids[0]); | ||
|
||
let texture_ids = gl::gen_textures(1); | ||
gl::bind_texture(gl::TEXTURE_2D, texture_ids[0]); | ||
|
||
gl::tex_image_2d(gl::TEXTURE_2D, 0, gl::RGB as GLint, self.size.width as GLsizei, | ||
self.size.height as GLsizei, 0, gl::RGB, gl::UNSIGNED_BYTE, None); | ||
gl::tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as GLint); | ||
gl::tex_parameter_i(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as GLint); | ||
|
||
gl::framebuffer_texture_2d(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, gl::TEXTURE_2D, | ||
texture_ids[0], 0); | ||
gl::bind_texture(gl::TEXTURE_2D, 0); | ||
|
||
gl::viewport(0 as GLint, 0 as GLint, | ||
self.size.width as GLsizei, self.size.height as GLsizei); | ||
} | ||
|
||
fn clear(&self, mask: u32) { | ||
gl::clear(mask); | ||
} | ||
|
||
fn clear_color(&self, r: f32, g: f32, b: f32, a: f32) { | ||
gl::clear_color(r, g, b, a); | ||
} | ||
|
||
fn send_pixel_contents(&mut self, chan: Sender<Vec<u8>>) { | ||
// FIXME(#5652, dmarcos) Instead of a readback strategy we have | ||
// to layerize the canvas | ||
let mut pixels = gl::read_pixels(0, 0, | ||
self.size.width as gl::GLsizei, | ||
self.size.height as gl::GLsizei, | ||
gl::RGBA, gl::UNSIGNED_BYTE); | ||
|
||
// rgba -> bgra | ||
byte_swap(pixels.as_mut_slice()); | ||
chan.send(pixels).unwrap(); | ||
} | ||
|
||
fn recreate(&mut self, size: Size2D<i32>) { | ||
self.size = size; | ||
self.init(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.