Files
tubestation/servo/components/canvas/webgl_paint_task.rs
Diego Marcos 00f99e2944 servo: Merge #5652 - Kicking off a WebGL implementation (from dmarcos:webgl); r=jdm
@jdm @ecoal95 I'm working on making VR happen in the Browser and I want to bring to Servo the [webVR APIs](https://github.com/MozVR/webvr-spec/blob/master/webvr.idl) we already have in Gecko. Before anything happens we need a working implementation of WebGL (and also the [fullscreen API](https://fullscreen.spec.whatwg.org/)). My implementation is very basic and probably naive (I just recently started to contribute to Servo). My patch is just a starting point:

- It only implements ```clearColor``` and ```clear``` methods of the [WebGL spec](https://www.khronos.org/registry/webgl/specs/latest/).
- It uses the readback strategy that ```canvasRenderingContext2D``` is using (The webgl task paints stuff independently on it's own buffer and the compositor task request the pixels back to the webgl task when it needs them) I'm sure there are much better ways to handle this. Latency and FPS are critical in VR so we have to figure out the fastest way to push pixels to the screen. I've read something about layerizing the canvas but I'm still not sure what that even means :)
- There's an included test you can try ```./mach run tests/ref/webgl-context/clearcolor.html```

@ecoal95 I know you'll be working on this for the next three months. With a foundation in place we will be able to make quick progress in parallel. This is exciting!

Source-Repo: https://github.com/servo/servo
Source-Revision: e4b620ea54c94e03095e4108bce94ec750416bba
2015-04-20 19:29:02 -05:00

113 lines
3.8 KiB
Rust

/* 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();
}
}