Skip to content
Permalink
Browse files

Implement DOM to texture

  • Loading branch information
MortimerGoro committed Oct 16, 2017
1 parent a9022be commit 8ae0739bab8a3c74e0685d9f53bbb155e4458aba

Some generated files are not rendered by default. Learn more.

@@ -21,5 +21,6 @@ ipc-channel = "0.8"
log = "0.3.5"
num-traits = "0.1.32"
offscreen_gl_context = { version = "0.11", features = ["serde", "osmesa"] }
servo_config = {path = "../config"}
webrender = {git = "https://github.com/servo/webrender"}
webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}
@@ -15,6 +15,7 @@ extern crate ipc_channel;
#[macro_use] extern crate log;
extern crate num_traits;
extern crate offscreen_gl_context;
extern crate servo_config;
extern crate webrender;
extern crate webrender_api;

@@ -6,9 +6,12 @@ use ::gl_context::GLContextFactory;
use ::webgl_thread::{WebGLExternalImageApi, WebGLExternalImageHandler, WebGLThreadObserver, WebGLThread};
use canvas_traits::webgl::{WebGLChan, WebGLContextId, WebGLMsg, WebGLPipeline, WebGLReceiver};
use canvas_traits::webgl::{WebGLSender, WebVRCommand, WebVRRenderHandler};
use canvas_traits::webgl::DOMToTextureCommand;
use canvas_traits::webgl::webgl_channel;
use euclid::Size2D;
use fnv::FnvHashMap;
use gleam::gl;
use servo_config::prefs::PREFS;
use std::marker::PhantomData;
use std::rc::Rc;
use webrender;
@@ -23,14 +26,19 @@ impl WebGLThreads {
webrender_gl: Rc<gl::Gl>,
webrender_api_sender: webrender_api::RenderApiSender,
webvr_compositor: Option<Box<WebVRRenderHandler>>)
-> (WebGLThreads, Box<webrender::ExternalImageHandler>) {
-> (WebGLThreads, Box<webrender::ExternalImageHandler>, Option<Box<webrender::OutputImageHandler>>) {
// This implementation creates a single `WebGLThread` for all the pipelines.
let channel = WebGLThread::start(gl_factory,
webrender_api_sender,
webvr_compositor.map(|c| WebVRRenderWrapper(c)),
PhantomData);
let output_handler = if PREFS.is_dom_to_texture_enabled() {
Some(Box::new(OutputHandler::new(webrender_gl.clone(), channel.clone())))
} else {
None
};
let external = WebGLExternalImageHandler::new(WebGLExternalImages::new(webrender_gl, channel.clone()));
(WebGLThreads(channel), Box::new(external))
(WebGLThreads(channel), Box::new(external), output_handler.map(|b| b as Box<_>))
}

/// Gets the WebGLThread handle for each script pipeline.
@@ -105,3 +113,47 @@ impl WebVRRenderHandler for WebVRRenderWrapper {
self.0.handle(command, texture);
}
}

/// struct used to implement DOMToTexture feature and webrender::OutputImageHandler trait.
type OutputHandlerData = Option<(u32, Size2D<i32>)>;
struct OutputHandler {
webrender_gl: Rc<gl::Gl>,
webgl_channel: WebGLSender<WebGLMsg>,
// Used to avoid creating a new channel on each received WebRender request.
lock_channel: (WebGLSender<OutputHandlerData>, WebGLReceiver<OutputHandlerData>),
sync_objects: FnvHashMap<webrender_api::PipelineId, gl::GLsync>,
}

impl OutputHandler {
fn new(webrender_gl: Rc<gl::Gl>, channel: WebGLSender<WebGLMsg>) -> Self {
Self {
webrender_gl,
webgl_channel: channel,
lock_channel: webgl_channel().unwrap(),
sync_objects: Default::default(),
}
}
}

/// Bridge between the WR frame outputs and WebGL to implement DOMToTexture synchronization.
impl webrender::OutputImageHandler for OutputHandler {
fn lock(&mut self, id: webrender_api::PipelineId) -> Option<(u32, webrender_api::DeviceIntSize)> {
// Insert a fence in the WR command queue
let gl_sync = self.webrender_gl.fence_sync(gl::SYNC_GPU_COMMANDS_COMPLETE, 0);
// The lock command adds a WaitSync call on the WebGL command flow.
let command = DOMToTextureCommand::Lock(id, gl_sync as usize, self.lock_channel.0.clone());
self.webgl_channel.send(WebGLMsg::DOMToTextureCommand(command)).unwrap();
self.lock_channel.1.recv().unwrap().map(|(tex_id, size)| {
(tex_id, webrender_api::DeviceIntSize::new(size.width, size.height))
})
}

fn unlock(&mut self, id: webrender_api::PipelineId) {
if let Some(gl_sync) = self.sync_objects.remove(&id) {
// Flush the Sync object into the GPU's command queue to guarantee that it it's signaled.
self.webrender_gl.flush();
// Mark the sync object for deletion.
self.webrender_gl.delete_sync(gl_sync);
}
}
}
@@ -36,6 +36,8 @@ pub struct WebGLThread<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver
webvr_compositor: Option<VR>,
/// Generic observer that listens WebGLContext creation, resize or removal events.
observer: OB,
/// Texture ids and sizes used in DOM to texture outputs.
dom_outputs: FnvHashMap<webrender_api::PipelineId, DOMToTextureData>,
}

impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR, OB> {
@@ -52,6 +54,7 @@ impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR,
next_webgl_id: 0,
webvr_compositor,
observer: observer,
dom_outputs: Default::default(),
}
}

@@ -117,6 +120,9 @@ impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR,
WebGLMsg::UpdateWebRenderImage(ctx_id, sender) => {
self.handle_update_wr_image(ctx_id, sender);
},
WebGLMsg::DOMToTextureCommand(command) => {
self.handle_dom_to_texture(command);
},
WebGLMsg::Exit => {
return true;
}
@@ -323,6 +329,54 @@ impl<VR: WebVRRenderHandler + 'static, OB: WebGLThreadObserver> WebGLThread<VR,
sender.send(image_key).unwrap();
}

fn handle_dom_to_texture(&mut self, command: DOMToTextureCommand) {
match command {
DOMToTextureCommand::Attach(context_id, texture_id, document_id, pipeline_id, size) => {
let ctx = Self::make_current_if_needed(context_id, &self.contexts, &mut self.bound_context_id)
.expect("WebGLContext not found in a WebGL DOMToTextureCommand::Attach command");
// Initialize the texture that WR will use for frame outputs.
ctx.gl().tex_image_2d(gl::TEXTURE_2D,
0,
gl::RGBA as gl::GLint,
size.width,
size.height,
0,
gl::RGBA,
gl::UNSIGNED_BYTE,
None);
self.dom_outputs.insert(pipeline_id, DOMToTextureData {
context_id, texture_id, document_id, size
});
self.webrender_api.enable_frame_output(document_id, pipeline_id, true);
},
DOMToTextureCommand::Lock(pipeline_id, gl_sync, sender) => {
let contexts = &self.contexts;
let bound_context_id = &mut self.bound_context_id;
let result = self.dom_outputs.get(&pipeline_id).and_then(|data| {
let ctx = Self::make_current_if_needed(data.context_id, contexts, bound_context_id);
ctx.and_then(|ctx| {
// The next glWaitSync call is used to synchronize the two flows of
// OpenGL commands (WR and WebGL) in order to avoid using semi-ready WR textures.
// glWaitSync doesn't block WebGL CPU thread.
ctx.gl().wait_sync(gl_sync as gl::GLsync, 0, gl::TIMEOUT_IGNORED);
Some((data.texture_id.get(), data.size))
})
});

// Send the texture id and size to WR.
sender.send(result).unwrap();
},
DOMToTextureCommand::Detach(texture_id) => {
if let Some((pipeline_id, document_id)) = self.dom_outputs.iter()
.find(|&(_, v)| v.texture_id == texture_id)
.map(|(k, v)| (*k, v.document_id)) {
self.webrender_api.enable_frame_output(document_id, pipeline_id, false);
self.dom_outputs.remove(&pipeline_id);
}
},
}
}

/// Gets a reference to a GLContextWrapper for a given WebGLContextId and makes it current if required.
fn make_current_if_needed<'a>(context_id: WebGLContextId,
contexts: &'a FnvHashMap<WebGLContextId, GLContextWrapper>,
@@ -552,6 +606,14 @@ impl<T: WebGLExternalImageApi> webrender::ExternalImageHandler for WebGLExternal
}
}

/// Data about the linked DOM<->WebGLTexture elements.
struct DOMToTextureData {
context_id: WebGLContextId,
texture_id: WebGLTextureId,
document_id: webrender_api::DocumentId,
size: Size2D<i32>,
}

/// WebGL Commands Implementation
pub struct WebGLImpl;

@@ -6,7 +6,7 @@ use euclid::Size2D;
use nonzero::NonZeroU32;
use offscreen_gl_context::{GLContextAttributes, GLLimits};
use std::fmt;
use webrender_api;
use webrender_api::{DocumentId, ImageKey, PipelineId};

/// Sender type used in WebGLCommands.
pub use ::webgl_channel::WebGLSender;
@@ -46,7 +46,9 @@ pub enum WebGLMsg {
/// Unlock messages are always sent after a Lock message.
Unlock(WebGLContextId),
/// Creates or updates the image keys required for WebRender.
UpdateWebRenderImage(WebGLContextId, WebGLSender<webrender_api::ImageKey>),
UpdateWebRenderImage(WebGLContextId, WebGLSender<ImageKey>),
/// Commands used for the DOMToTexture feature.
DOMToTextureCommand(DOMToTextureCommand),
/// Frees all resources and closes the thread.
Exit,
}
@@ -86,6 +88,11 @@ impl WebGLMsgSender {
}
}

/// Returns the WebGLContextId associated to this sender
pub fn context_id(&self) -> WebGLContextId {
self.ctx_id
}

/// Send a WebGLCommand message
#[inline]
pub fn send(&self, command: WebGLCommand) -> WebGLSendResult {
@@ -113,9 +120,13 @@ impl WebGLMsgSender {
}

#[inline]
pub fn send_update_wr_image(&self, sender: WebGLSender<webrender_api::ImageKey>) -> WebGLSendResult {
pub fn send_update_wr_image(&self, sender: WebGLSender<ImageKey>) -> WebGLSendResult {
self.sender.send(WebGLMsg::UpdateWebRenderImage(self.ctx_id, sender))
}

pub fn send_dom_to_texture(&self, command: DOMToTextureCommand) -> WebGLSendResult {
self.sender.send(WebGLMsg::DOMToTextureCommand(command))
}
}

/// WebGL Commands for a specific WebGLContext
@@ -379,6 +390,17 @@ pub trait WebVRRenderHandler: Send {
fn handle(&mut self, command: WebVRCommand, texture: Option<(u32, Size2D<i32>)>);
}

/// WebGL commands required to implement DOMToTexture feature.
#[derive(Clone, Deserialize, Serialize)]
pub enum DOMToTextureCommand {
/// Attaches a HTMLIFrameElement to a WebGLTexture.
Attach(WebGLContextId, WebGLTextureId, DocumentId, PipelineId, Size2D<i32>),
/// Releases the HTMLIFrameElement to WebGLTexture attachment.
Detach(WebGLTextureId),
/// Lock message used for a correct synchronization with WebRender GL flow.
Lock(PipelineId, usize, WebGLSender<Option<(u32, Size2D<i32>)>>),
}

impl fmt::Debug for WebGLCommand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::WebGLCommand::*;
@@ -273,4 +273,8 @@ impl Preferences {
pub fn is_webvr_enabled(&self) -> bool {
self.get("dom.webvr.enabled").as_boolean().unwrap_or(false)
}

pub fn is_dom_to_texture_enabled(&self) -> bool {
self.get("dom.webgl.dom_to_texture.enabled").as_boolean().unwrap_or(false)
}
}
@@ -511,6 +511,7 @@ impl UnprivilegedPipelineContent {
content_process_shutdown_chan: self.script_content_process_shutdown_chan,
webgl_chan: self.webgl_chan,
webvr_chan: self.webvr_chan,
webrender_document: self.webrender_document,
}, self.load_data.clone());

LTF::create(self.id,
@@ -110,7 +110,7 @@ use style::stylesheets::keyframes_rule::Keyframe;
use style::values::specified::Length;
use time::Duration;
use uuid::Uuid;
use webrender_api::ImageKey;
use webrender_api::{DocumentId, ImageKey};
use webvr_traits::WebVRGamepadHand;

/// A trait to allow tracing (only) DOM objects.
@@ -397,6 +397,7 @@ unsafe_no_jsmanaged_fields!(OpaqueStyleAndLayoutData);
unsafe_no_jsmanaged_fields!(PathBuf);
unsafe_no_jsmanaged_fields!(CSSErrorReporter);
unsafe_no_jsmanaged_fields!(DrawAPaintImageResult);
unsafe_no_jsmanaged_fields!(DocumentId);
unsafe_no_jsmanaged_fields!(ImageKey);
unsafe_no_jsmanaged_fields!(WebGLBufferId);
unsafe_no_jsmanaged_fields!(WebGLChan);
@@ -6,6 +6,7 @@ use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
use canvas_traits::canvas::{byte_swap, multiply_u8_pixel};
use canvas_traits::webgl::{WebGLContextShareMode, WebGLCommand, WebGLError};
use canvas_traits::webgl::{WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender, WebGLParameter, WebVRCommand};
use canvas_traits::webgl::DOMToTextureCommand;
use canvas_traits::webgl::WebGLError::*;
use canvas_traits::webgl::webgl_channel;
use core::cell::Ref;
@@ -25,6 +26,7 @@ use dom::bindings::str::DOMString;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::htmlcanvaselement::HTMLCanvasElement;
use dom::htmlcanvaselement::utils as canvas_utils;
use dom::htmliframeelement::HTMLIFrameElement;
use dom::node::{Node, NodeDamage, window_from_node};
use dom::webgl_extensions::WebGLExtensions;
use dom::webgl_validations::WebGLValidator;
@@ -3260,6 +3262,45 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
Ok(())
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
fn TexImageDOM(&self,
target: u32,
level: i32,
internal_format: u32,
width: i32,
height: i32,
format: u32,
data_type: u32,
source: &HTMLIFrameElement) -> Fallible<()> {
// Currently DOMToTexture only supports TEXTURE_2D, RGBA, UNSIGNED_BYTE and no levels.
if target != constants::TEXTURE_2D || level != 0 || internal_format != constants::RGBA ||
format != constants::RGBA || data_type != constants::UNSIGNED_BYTE {
return Ok(self.webgl_error(InvalidValue));
}

// Get bound texture
let texture = match self.bound_texture(constants::TEXTURE_2D) {
Some(texture) => texture,
None => {
return Ok(self.webgl_error(InvalidOperation));
}
};

let pipeline_id = source.pipeline_id().ok_or(Error::InvalidState)?;
let document_id = self.global().downcast::<Window>().ok_or(Error::InvalidState)?.webrender_document();

texture.set_attached_to_dom();

let command = DOMToTextureCommand::Attach(self.webgl_sender.context_id(),
texture.id(),
document_id,
pipeline_id.to_webrender(),
Size2D::new(width, height));
self.webgl_sender.send_dom_to_texture(command).unwrap();

Ok(())
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.8
#[allow(unsafe_code)]
unsafe fn TexSubImage2D(&self,

0 comments on commit 8ae0739

Please sign in to comment.
You can’t perform that action at this time.