mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Implement DOM to texture
This commit is contained in:
parent
a9022be0c3
commit
8ae0739bab
17 changed files with 238 additions and 13 deletions
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue