mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
When using the WebGL external image API, use sync calls if we happen to be on the WebGL thread
This commit is contained in:
parent
8ec28978cd
commit
0178fcae4e
3 changed files with 191 additions and 222 deletions
|
@ -3,7 +3,7 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use crate::gl_context::GLContextFactory;
|
use crate::gl_context::GLContextFactory;
|
||||||
use crate::webgl_thread::{TexturesMap, WebGLMainThread, WebGLThread, WebGLThreadInit};
|
use crate::webgl_thread::{WebGLMainThread, WebGLThread, WebGLThreadInit};
|
||||||
use canvas_traits::webgl::webgl_channel;
|
use canvas_traits::webgl::webgl_channel;
|
||||||
use canvas_traits::webgl::DOMToTextureCommand;
|
use canvas_traits::webgl::DOMToTextureCommand;
|
||||||
use canvas_traits::webgl::{WebGLChan, WebGLContextId, WebGLMsg, WebGLPipeline, WebGLReceiver};
|
use canvas_traits::webgl::{WebGLChan, WebGLContextId, WebGLMsg, WebGLPipeline, WebGLReceiver};
|
||||||
|
@ -13,8 +13,6 @@ use euclid::default::Size2D;
|
||||||
use fnv::FnvHashMap;
|
use fnv::FnvHashMap;
|
||||||
use gleam::gl;
|
use gleam::gl;
|
||||||
use servo_config::pref;
|
use servo_config::pref;
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
@ -25,20 +23,21 @@ pub struct WebGLThreads(WebGLSender<WebGLMsg>);
|
||||||
|
|
||||||
pub enum ThreadMode {
|
pub enum ThreadMode {
|
||||||
MainThread(Box<dyn EventLoopWaker>),
|
MainThread(Box<dyn EventLoopWaker>),
|
||||||
OffThread(Rc<dyn gl::Gl>),
|
OffThread,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebGLThreads {
|
impl WebGLThreads {
|
||||||
/// Creates a new WebGLThreads object
|
/// Creates a new WebGLThreads object
|
||||||
pub fn new(
|
pub fn new(
|
||||||
gl_factory: GLContextFactory,
|
gl_factory: GLContextFactory,
|
||||||
|
webrender_gl: Rc<dyn gl::Gl>,
|
||||||
webrender_api_sender: webrender_api::RenderApiSender,
|
webrender_api_sender: webrender_api::RenderApiSender,
|
||||||
webvr_compositor: Option<Box<dyn WebVRRenderHandler>>,
|
webvr_compositor: Option<Box<dyn WebVRRenderHandler>>,
|
||||||
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
|
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
|
||||||
mode: ThreadMode,
|
mode: ThreadMode,
|
||||||
) -> (
|
) -> (
|
||||||
WebGLThreads,
|
WebGLThreads,
|
||||||
Option<WebGLMainThread>,
|
Option<Rc<WebGLMainThread>>,
|
||||||
Box<dyn WebrenderExternalImageApi>,
|
Box<dyn WebrenderExternalImageApi>,
|
||||||
Option<Box<dyn webrender::OutputImageHandler>>,
|
Option<Box<dyn webrender::OutputImageHandler>>,
|
||||||
) {
|
) {
|
||||||
|
@ -54,33 +53,24 @@ impl WebGLThreads {
|
||||||
};
|
};
|
||||||
|
|
||||||
let output_handler = if pref!(dom.webgl.dom_to_texture.enabled) {
|
let output_handler = if pref!(dom.webgl.dom_to_texture.enabled) {
|
||||||
Some(Box::new(match mode {
|
Some(Box::new(OutputHandler::new(
|
||||||
ThreadMode::MainThread(..) => OutputHandler::new_main_thread(),
|
webrender_gl.clone(),
|
||||||
ThreadMode::OffThread(ref webrender_gl) => {
|
sender.clone(),
|
||||||
OutputHandler::new_off_thread(webrender_gl.clone(), sender.clone())
|
)))
|
||||||
},
|
|
||||||
}))
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let (external, webgl_thread) = match mode {
|
let external = WebGLExternalImages::new(webrender_gl, sender.clone());
|
||||||
ThreadMode::MainThread(event_loop_waker) => {
|
|
||||||
let textures = Rc::new(RefCell::new(HashMap::new()));
|
|
||||||
let thread_data =
|
|
||||||
WebGLThread::run_on_current_thread(init, event_loop_waker, textures.clone());
|
|
||||||
(
|
|
||||||
WebGLExternalImages::new_main_thread(textures),
|
|
||||||
Some(thread_data),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
ThreadMode::OffThread(webrender_gl) => {
|
let webgl_thread = match mode {
|
||||||
|
ThreadMode::MainThread(event_loop_waker) => {
|
||||||
|
let thread = WebGLThread::run_on_current_thread(init, event_loop_waker);
|
||||||
|
Some(thread)
|
||||||
|
},
|
||||||
|
ThreadMode::OffThread => {
|
||||||
WebGLThread::run_on_own_thread(init);
|
WebGLThread::run_on_own_thread(init);
|
||||||
(
|
None
|
||||||
WebGLExternalImages::new_off_thread(webrender_gl, sender.clone()),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -107,113 +97,90 @@ impl WebGLThreads {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bridge between the webrender::ExternalImage callbacks and the WebGLThreads.
|
/// Bridge between the webrender::ExternalImage callbacks and the WebGLThreads.
|
||||||
enum WebGLExternalImages {
|
struct WebGLExternalImages {
|
||||||
OffThread {
|
webrender_gl: Rc<dyn gl::Gl>,
|
||||||
webrender_gl: Rc<dyn gl::Gl>,
|
webgl_channel: WebGLSender<WebGLMsg>,
|
||||||
webgl_channel: WebGLSender<WebGLMsg>,
|
// Used to avoid creating a new channel on each received WebRender request.
|
||||||
// Used to avoid creating a new channel on each received WebRender request.
|
lock_channel: (
|
||||||
lock_channel: (
|
WebGLSender<(u32, Size2D<i32>, usize)>,
|
||||||
WebGLSender<(u32, Size2D<i32>, usize)>,
|
WebGLReceiver<(u32, Size2D<i32>, usize)>,
|
||||||
WebGLReceiver<(u32, Size2D<i32>, usize)>,
|
),
|
||||||
),
|
|
||||||
},
|
|
||||||
MainThread {
|
|
||||||
textures: TexturesMap,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebGLExternalImages {
|
impl WebGLExternalImages {
|
||||||
fn new_off_thread(webrender_gl: Rc<dyn gl::Gl>, channel: WebGLSender<WebGLMsg>) -> Self {
|
fn new(webrender_gl: Rc<dyn gl::Gl>, channel: WebGLSender<WebGLMsg>) -> Self {
|
||||||
WebGLExternalImages::OffThread {
|
WebGLExternalImages {
|
||||||
webrender_gl,
|
webrender_gl,
|
||||||
webgl_channel: channel,
|
webgl_channel: channel,
|
||||||
lock_channel: webgl_channel().unwrap(),
|
lock_channel: webgl_channel().unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_main_thread(textures: TexturesMap) -> Self {
|
|
||||||
WebGLExternalImages::MainThread { textures }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebrenderExternalImageApi for WebGLExternalImages {
|
impl WebrenderExternalImageApi for WebGLExternalImages {
|
||||||
fn lock(&mut self, id: u64) -> (u32, Size2D<i32>) {
|
fn lock(&mut self, id: u64) -> (u32, Size2D<i32>) {
|
||||||
match *self {
|
if let Some(main_thread) = WebGLMainThread::on_current_thread() {
|
||||||
WebGLExternalImages::OffThread {
|
// If we're on the same thread as WebGL, we can get the data directly
|
||||||
ref webgl_channel,
|
main_thread
|
||||||
ref webrender_gl,
|
.thread_data
|
||||||
ref lock_channel,
|
.borrow_mut()
|
||||||
} => {
|
.handle_lock_unsync(WebGLContextId(id as usize))
|
||||||
// WebGL Thread has it's own GL command queue that we need to synchronize with the WR GL command queue.
|
} else {
|
||||||
// The WebGLMsg::Lock message inserts a fence in the WebGL command queue.
|
// WebGL Thread has it's own GL command queue that we need to synchronize with the WR GL command queue.
|
||||||
webgl_channel
|
// The WebGLMsg::Lock message inserts a fence in the WebGL command queue.
|
||||||
.send(WebGLMsg::Lock(
|
self.webgl_channel
|
||||||
WebGLContextId(id as usize),
|
.send(WebGLMsg::Lock(
|
||||||
lock_channel.0.clone(),
|
WebGLContextId(id as usize),
|
||||||
))
|
self.lock_channel.0.clone(),
|
||||||
.unwrap();
|
))
|
||||||
let (image_id, size, gl_sync) = lock_channel.1.recv().unwrap();
|
.unwrap();
|
||||||
// The next glWaitSync call is run on the WR thread and it's used to synchronize the two
|
let (image_id, size, gl_sync) = self.lock_channel.1.recv().unwrap();
|
||||||
// flows of OpenGL commands in order to avoid WR using a semi-ready WebGL texture.
|
// The next glWaitSync call is run on the WR thread and it's used to synchronize the two
|
||||||
// glWaitSync doesn't block WR thread, it affects only internal OpenGL subsystem.
|
// flows of OpenGL commands in order to avoid WR using a semi-ready WebGL texture.
|
||||||
webrender_gl.wait_sync(gl_sync as gl::GLsync, 0, gl::TIMEOUT_IGNORED);
|
// glWaitSync doesn't block WR thread, it affects only internal OpenGL subsystem.
|
||||||
(image_id, size)
|
self.webrender_gl
|
||||||
},
|
.wait_sync(gl_sync as gl::GLsync, 0, gl::TIMEOUT_IGNORED);
|
||||||
|
(image_id, size)
|
||||||
WebGLExternalImages::MainThread { ref textures } => {
|
|
||||||
let textures = textures.borrow();
|
|
||||||
let entry = textures
|
|
||||||
.get(&WebGLContextId(id as usize))
|
|
||||||
.expect("no texture entry???");
|
|
||||||
(entry.0, entry.1)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unlock(&mut self, id: u64) {
|
fn unlock(&mut self, id: u64) {
|
||||||
match *self {
|
if let Some(main_thread) = WebGLMainThread::on_current_thread() {
|
||||||
WebGLExternalImages::OffThread {
|
// If we're on the same thread as WebGL, we can unlock directly
|
||||||
ref webgl_channel, ..
|
main_thread
|
||||||
} => {
|
.thread_data
|
||||||
webgl_channel
|
.borrow_mut()
|
||||||
.send(WebGLMsg::Unlock(WebGLContextId(id as usize)))
|
.handle_unlock(WebGLContextId(id as usize))
|
||||||
.unwrap();
|
} else {
|
||||||
},
|
self.webgl_channel
|
||||||
|
.send(WebGLMsg::Unlock(WebGLContextId(id as usize)))
|
||||||
WebGLExternalImages::MainThread { .. } => {},
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// struct used to implement DOMToTexture feature and webrender::OutputImageHandler trait.
|
/// struct used to implement DOMToTexture feature and webrender::OutputImageHandler trait.
|
||||||
type OutputHandlerData = Option<(u32, Size2D<i32>)>;
|
type OutputHandlerData = Option<(u32, Size2D<i32>)>;
|
||||||
enum OutputHandler {
|
struct OutputHandler {
|
||||||
OffThread {
|
webrender_gl: Rc<dyn gl::Gl>,
|
||||||
webrender_gl: Rc<dyn gl::Gl>,
|
webgl_channel: WebGLSender<WebGLMsg>,
|
||||||
webgl_channel: WebGLSender<WebGLMsg>,
|
// Used to avoid creating a new channel on each received WebRender request.
|
||||||
// Used to avoid creating a new channel on each received WebRender request.
|
lock_channel: (
|
||||||
lock_channel: (
|
WebGLSender<OutputHandlerData>,
|
||||||
WebGLSender<OutputHandlerData>,
|
WebGLReceiver<OutputHandlerData>,
|
||||||
WebGLReceiver<OutputHandlerData>,
|
),
|
||||||
),
|
sync_objects: FnvHashMap<webrender_api::PipelineId, gl::GLsync>,
|
||||||
sync_objects: FnvHashMap<webrender_api::PipelineId, gl::GLsync>,
|
|
||||||
},
|
|
||||||
MainThread,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutputHandler {
|
impl OutputHandler {
|
||||||
fn new_off_thread(webrender_gl: Rc<dyn gl::Gl>, channel: WebGLSender<WebGLMsg>) -> Self {
|
fn new(webrender_gl: Rc<dyn gl::Gl>, channel: WebGLSender<WebGLMsg>) -> Self {
|
||||||
OutputHandler::OffThread {
|
OutputHandler {
|
||||||
webrender_gl,
|
webrender_gl,
|
||||||
webgl_channel: channel,
|
webgl_channel: channel,
|
||||||
lock_channel: webgl_channel().unwrap(),
|
lock_channel: webgl_channel().unwrap(),
|
||||||
sync_objects: Default::default(),
|
sync_objects: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_main_thread() -> Self {
|
|
||||||
OutputHandler::MainThread
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bridge between the WR frame outputs and WebGL to implement DOMToTexture synchronization.
|
/// Bridge between the WR frame outputs and WebGL to implement DOMToTexture synchronization.
|
||||||
|
@ -222,49 +189,41 @@ impl webrender::OutputImageHandler for OutputHandler {
|
||||||
&mut self,
|
&mut self,
|
||||||
id: webrender_api::PipelineId,
|
id: webrender_api::PipelineId,
|
||||||
) -> Option<(u32, webrender_api::units::FramebufferIntSize)> {
|
) -> Option<(u32, webrender_api::units::FramebufferIntSize)> {
|
||||||
match *self {
|
// Insert a fence in the WR command queue
|
||||||
OutputHandler::OffThread {
|
let gl_sync = self
|
||||||
ref webrender_gl,
|
.webrender_gl
|
||||||
ref lock_channel,
|
.fence_sync(gl::SYNC_GPU_COMMANDS_COMPLETE, 0);
|
||||||
ref webgl_channel,
|
self.sync_objects.insert(id, gl_sync);
|
||||||
..
|
|
||||||
} => {
|
|
||||||
// Insert a fence in the WR command queue
|
|
||||||
let gl_sync = 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, lock_channel.0.clone());
|
|
||||||
webgl_channel
|
|
||||||
.send(WebGLMsg::DOMToTextureCommand(command))
|
|
||||||
.unwrap();
|
|
||||||
lock_channel.1.recv().unwrap().map(|(tex_id, size)| {
|
|
||||||
(
|
|
||||||
tex_id,
|
|
||||||
webrender_api::units::FramebufferIntSize::new(size.width, size.height),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
OutputHandler::MainThread => unimplemented!(),
|
let result = if let Some(main_thread) = WebGLMainThread::on_current_thread() {
|
||||||
}
|
main_thread
|
||||||
|
.thread_data
|
||||||
|
.borrow_mut()
|
||||||
|
.handle_dom_to_texture_lock(id, gl_sync as usize)
|
||||||
|
} else {
|
||||||
|
// 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()
|
||||||
|
};
|
||||||
|
|
||||||
|
result.map(|(tex_id, size)| {
|
||||||
|
(
|
||||||
|
tex_id,
|
||||||
|
webrender_api::units::FramebufferIntSize::new(size.width, size.height),
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unlock(&mut self, id: webrender_api::PipelineId) {
|
fn unlock(&mut self, id: webrender_api::PipelineId) {
|
||||||
match *self {
|
if let Some(gl_sync) = self.sync_objects.remove(&id) {
|
||||||
OutputHandler::OffThread {
|
// Flush the Sync object into the GPU's command queue to guarantee that it it's signaled.
|
||||||
ref webrender_gl,
|
self.webrender_gl.flush();
|
||||||
ref mut sync_objects,
|
// Mark the sync object for deletion.
|
||||||
..
|
self.webrender_gl.delete_sync(gl_sync);
|
||||||
} => {
|
|
||||||
if let Some(gl_sync) = sync_objects.remove(&id) {
|
|
||||||
// Flush the Sync object into the GPU's command queue to guarantee that it it's signaled.
|
|
||||||
webrender_gl.flush();
|
|
||||||
// Mark the sync object for deletion.
|
|
||||||
webrender_gl.delete_sync(gl_sync);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
OutputHandler::MainThread => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,8 @@ use ipc_channel::router::ROUTER;
|
||||||
use offscreen_gl_context::{DrawBuffer, GLContext, NativeGLContextMethods};
|
use offscreen_gl_context::{DrawBuffer, GLContext, NativeGLContextMethods};
|
||||||
use pixels::{self, PixelFormat};
|
use pixels::{self, PixelFormat};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::cell::Cell;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
@ -80,12 +80,6 @@ pub(crate) struct WebGLThread {
|
||||||
sender: WebGLSender<WebGLMsg>,
|
sender: WebGLSender<WebGLMsg>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A map of GL contexts to backing textures and their sizes.
|
|
||||||
/// Only used for accessing this information when the WebGL processing is run
|
|
||||||
/// on the main thread and the compositor needs access to this information
|
|
||||||
/// synchronously.
|
|
||||||
pub(crate) type TexturesMap = Rc<RefCell<HashMap<WebGLContextId, (u32, Size2D<i32>)>>>;
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum EventLoop {
|
enum EventLoop {
|
||||||
Blocking,
|
Blocking,
|
||||||
|
@ -105,24 +99,36 @@ pub(crate) struct WebGLThreadInit {
|
||||||
/// The extra data required to run an instance of WebGLThread when it is
|
/// The extra data required to run an instance of WebGLThread when it is
|
||||||
/// not running in its own thread.
|
/// not running in its own thread.
|
||||||
pub struct WebGLMainThread {
|
pub struct WebGLMainThread {
|
||||||
thread_data: WebGLThread,
|
pub(crate) thread_data: RefCell<WebGLThread>,
|
||||||
shut_down: bool,
|
shut_down: Cell<bool>,
|
||||||
textures: TexturesMap,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebGLMainThread {
|
impl WebGLMainThread {
|
||||||
/// Synchronously process all outstanding WebGL messages.
|
/// Synchronously process all outstanding WebGL messages.
|
||||||
pub fn process(&mut self) {
|
pub fn process(&self) {
|
||||||
if self.shut_down {
|
if self.shut_down.get() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Any context could be current when we start.
|
// Any context could be current when we start.
|
||||||
self.thread_data.bound_context_id = None;
|
self.thread_data.borrow_mut().bound_context_id = None;
|
||||||
self.shut_down = !self
|
self.shut_down.set(
|
||||||
.thread_data
|
!self
|
||||||
.process(EventLoop::Nonblocking, Some(self.textures.clone()))
|
.thread_data
|
||||||
|
.borrow_mut()
|
||||||
|
.process(EventLoop::Nonblocking),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the main GL thread if called from the main thread,
|
||||||
|
/// and `None` otherwise.
|
||||||
|
pub(crate) fn on_current_thread() -> Option<Rc<WebGLMainThread>> {
|
||||||
|
WEBGL_MAIN_THREAD.with(|main_thread| main_thread.borrow().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static WEBGL_MAIN_THREAD: RefCell<Option<Rc<WebGLMainThread>>> = RefCell::new(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebGLThread {
|
impl WebGLThread {
|
||||||
|
@ -158,8 +164,7 @@ impl WebGLThread {
|
||||||
pub(crate) fn run_on_current_thread(
|
pub(crate) fn run_on_current_thread(
|
||||||
mut init: WebGLThreadInit,
|
mut init: WebGLThreadInit,
|
||||||
event_loop_waker: Box<dyn EventLoopWaker>,
|
event_loop_waker: Box<dyn EventLoopWaker>,
|
||||||
textures: TexturesMap,
|
) -> Rc<WebGLMainThread> {
|
||||||
) -> WebGLMainThread {
|
|
||||||
if let WebGLReceiver::Ipc(ref mut receiver) = init.receiver {
|
if let WebGLReceiver::Ipc(ref mut receiver) = init.receiver {
|
||||||
// Interpose a new channel in between the existing WebGL channel endpoints.
|
// Interpose a new channel in between the existing WebGL channel endpoints.
|
||||||
// This will bounce all WebGL messages through the router thread adding a small
|
// This will bounce all WebGL messages through the router thread adding a small
|
||||||
|
@ -176,11 +181,17 @@ impl WebGLThread {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebGLMainThread {
|
let result = Rc::new(WebGLMainThread {
|
||||||
thread_data: WebGLThread::new(init),
|
thread_data: RefCell::new(WebGLThread::new(init)),
|
||||||
textures,
|
shut_down: Cell::new(false),
|
||||||
shut_down: false,
|
});
|
||||||
}
|
|
||||||
|
WEBGL_MAIN_THREAD.with(|main_thread| {
|
||||||
|
debug_assert!(main_thread.borrow().is_none());
|
||||||
|
*main_thread.borrow_mut() = Some(result.clone());
|
||||||
|
});
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform all initialization required to run an instance of WebGLThread
|
/// Perform all initialization required to run an instance of WebGLThread
|
||||||
|
@ -190,18 +201,18 @@ impl WebGLThread {
|
||||||
.name("WebGL thread".to_owned())
|
.name("WebGL thread".to_owned())
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
let mut data = WebGLThread::new(init);
|
let mut data = WebGLThread::new(init);
|
||||||
data.process(EventLoop::Blocking, None);
|
data.process(EventLoop::Blocking);
|
||||||
})
|
})
|
||||||
.expect("Thread spawning failed");
|
.expect("Thread spawning failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(&mut self, loop_type: EventLoop, textures: Option<TexturesMap>) -> bool {
|
fn process(&mut self, loop_type: EventLoop) -> bool {
|
||||||
let webgl_chan = WebGLChan(self.sender.clone());
|
let webgl_chan = WebGLChan(self.sender.clone());
|
||||||
while let Ok(msg) = match loop_type {
|
while let Ok(msg) = match loop_type {
|
||||||
EventLoop::Blocking => self.receiver.recv(),
|
EventLoop::Blocking => self.receiver.recv(),
|
||||||
EventLoop::Nonblocking => self.receiver.try_recv(),
|
EventLoop::Nonblocking => self.receiver.try_recv(),
|
||||||
} {
|
} {
|
||||||
let exit = self.handle_msg(msg, &webgl_chan, textures.as_ref());
|
let exit = self.handle_msg(msg, &webgl_chan);
|
||||||
if exit {
|
if exit {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -210,16 +221,11 @@ impl WebGLThread {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles a generic WebGLMsg message
|
/// Handles a generic WebGLMsg message
|
||||||
fn handle_msg(
|
fn handle_msg(&mut self, msg: WebGLMsg, webgl_chan: &WebGLChan) -> bool {
|
||||||
&mut self,
|
|
||||||
msg: WebGLMsg,
|
|
||||||
webgl_chan: &WebGLChan,
|
|
||||||
textures: Option<&TexturesMap>,
|
|
||||||
) -> bool {
|
|
||||||
trace!("processing {:?}", msg);
|
trace!("processing {:?}", msg);
|
||||||
match msg {
|
match msg {
|
||||||
WebGLMsg::CreateContext(version, size, attributes, result_sender) => {
|
WebGLMsg::CreateContext(version, size, attributes, result_sender) => {
|
||||||
let result = self.create_webgl_context(version, size, attributes, textures);
|
let result = self.create_webgl_context(version, size, attributes);
|
||||||
result_sender
|
result_sender
|
||||||
.send(result.map(|(id, limits, share_mode)| {
|
.send(result.map(|(id, limits, share_mode)| {
|
||||||
let data = Self::make_current_if_needed(
|
let data = Self::make_current_if_needed(
|
||||||
|
@ -264,10 +270,10 @@ impl WebGLThread {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
},
|
},
|
||||||
WebGLMsg::ResizeContext(ctx_id, size, sender) => {
|
WebGLMsg::ResizeContext(ctx_id, size, sender) => {
|
||||||
self.resize_webgl_context(ctx_id, size, sender, textures);
|
self.resize_webgl_context(ctx_id, size, sender);
|
||||||
},
|
},
|
||||||
WebGLMsg::RemoveContext(ctx_id) => {
|
WebGLMsg::RemoveContext(ctx_id) => {
|
||||||
self.remove_webgl_context(ctx_id, textures);
|
self.remove_webgl_context(ctx_id);
|
||||||
},
|
},
|
||||||
WebGLMsg::WebGLCommand(ctx_id, command, backtrace) => {
|
WebGLMsg::WebGLCommand(ctx_id, command, backtrace) => {
|
||||||
self.handle_webgl_command(ctx_id, command, backtrace);
|
self.handle_webgl_command(ctx_id, command, backtrace);
|
||||||
|
@ -337,7 +343,7 @@ impl WebGLThread {
|
||||||
context_id: WebGLContextId,
|
context_id: WebGLContextId,
|
||||||
sender: WebGLSender<(u32, Size2D<i32>, usize)>,
|
sender: WebGLSender<(u32, Size2D<i32>, usize)>,
|
||||||
) {
|
) {
|
||||||
sender.send(self.handle_lock_inner(context_id)).unwrap();
|
sender.send(self.handle_lock_sync(context_id)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// handle_lock, but unconditionally IPC (used by webxr)
|
/// handle_lock, but unconditionally IPC (used by webxr)
|
||||||
|
@ -346,12 +352,12 @@ impl WebGLThread {
|
||||||
context_id: WebGLContextId,
|
context_id: WebGLContextId,
|
||||||
sender: IpcSender<(u32, Size2D<i32>, usize)>,
|
sender: IpcSender<(u32, Size2D<i32>, usize)>,
|
||||||
) {
|
) {
|
||||||
sender.send(self.handle_lock_inner(context_id)).unwrap();
|
sender.send(self.handle_lock_sync(context_id)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shared code between handle_lock and handle_lock_ipc, does the actual syncing/flushing
|
/// Shared code between handle_lock and handle_lock_ipc, does the actual syncing/flushing
|
||||||
/// but the caller must send the response back
|
/// but the caller must send the response back
|
||||||
fn handle_lock_inner(&mut self, context_id: WebGLContextId) -> (u32, Size2D<i32>, usize) {
|
fn handle_lock_sync(&mut self, context_id: WebGLContextId) -> (u32, Size2D<i32>, usize) {
|
||||||
let data =
|
let data =
|
||||||
Self::make_current_if_needed(context_id, &self.contexts, &mut self.bound_context_id)
|
Self::make_current_if_needed(context_id, &self.contexts, &mut self.bound_context_id)
|
||||||
.expect("WebGLContext not found in a WebGLMsg::Lock message");
|
.expect("WebGLContext not found in a WebGLMsg::Lock message");
|
||||||
|
@ -361,23 +367,38 @@ impl WebGLThread {
|
||||||
// The related gl().wait_sync call is performed in the WR thread. See WebGLExternalImageApi for mor details.
|
// The related gl().wait_sync call is performed in the WR thread. See WebGLExternalImageApi for mor details.
|
||||||
let gl_sync = data.ctx.gl().fence_sync(gl::SYNC_GPU_COMMANDS_COMPLETE, 0);
|
let gl_sync = data.ctx.gl().fence_sync(gl::SYNC_GPU_COMMANDS_COMPLETE, 0);
|
||||||
info.gl_sync = Some(gl_sync);
|
info.gl_sync = Some(gl_sync);
|
||||||
|
debug_assert!(data.ctx.gl().get_error() == gl::NO_ERROR);
|
||||||
|
|
||||||
// It is important that the fence sync is properly flushed into the GPU's command queue.
|
// It is important that the fence sync is properly flushed into the GPU's command queue.
|
||||||
// Without proper flushing, the sync object may never be signaled.
|
// Without proper flushing, the sync object may never be signaled.
|
||||||
data.ctx.gl().flush();
|
data.ctx.gl().flush();
|
||||||
|
debug_assert!(data.ctx.gl().get_error() == gl::NO_ERROR);
|
||||||
|
|
||||||
(info.texture_id, info.size, gl_sync as usize)
|
(info.texture_id, info.size, gl_sync as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A version of locking that doesn't return a GLsync object,
|
||||||
|
/// used when it's being called from the same thread.
|
||||||
|
pub(crate) fn handle_lock_unsync(&mut self, context_id: WebGLContextId) -> (u32, Size2D<i32>) {
|
||||||
|
let info = self.cached_context_info.get_mut(&context_id).unwrap();
|
||||||
|
info.render_state = ContextRenderState::Locked(None);
|
||||||
|
(info.texture_id, info.size)
|
||||||
|
}
|
||||||
|
|
||||||
/// Handles an unlock external callback received from webrender::ExternalImageHandler
|
/// Handles an unlock external callback received from webrender::ExternalImageHandler
|
||||||
fn handle_unlock(&mut self, context_id: WebGLContextId) {
|
pub(crate) fn handle_unlock(&mut self, context_id: WebGLContextId) {
|
||||||
let data =
|
|
||||||
Self::make_current_if_needed(context_id, &self.contexts, &mut self.bound_context_id)
|
|
||||||
.expect("WebGLContext not found in a WebGLMsg::Unlock message");
|
|
||||||
let info = self.cached_context_info.get_mut(&context_id).unwrap();
|
let info = self.cached_context_info.get_mut(&context_id).unwrap();
|
||||||
info.render_state = ContextRenderState::Unlocked;
|
info.render_state = ContextRenderState::Unlocked;
|
||||||
if let Some(gl_sync) = info.gl_sync.take() {
|
if let Some(gl_sync) = info.gl_sync.take() {
|
||||||
|
let data = Self::make_current_if_needed(
|
||||||
|
context_id,
|
||||||
|
&self.contexts,
|
||||||
|
&mut self.bound_context_id,
|
||||||
|
)
|
||||||
|
.expect("WebGLContext not found in a WebGLMsg::Unlock message");
|
||||||
// Release the GLSync object.
|
// Release the GLSync object.
|
||||||
data.ctx.gl().delete_sync(gl_sync);
|
data.ctx.gl().delete_sync(gl_sync);
|
||||||
|
debug_assert!(data.ctx.gl().get_error() == gl::NO_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -387,7 +408,6 @@ impl WebGLThread {
|
||||||
version: WebGLVersion,
|
version: WebGLVersion,
|
||||||
size: Size2D<u32>,
|
size: Size2D<u32>,
|
||||||
attributes: GLContextAttributes,
|
attributes: GLContextAttributes,
|
||||||
textures: Option<&TexturesMap>,
|
|
||||||
) -> Result<(WebGLContextId, GLLimits, WebGLContextShareMode), String> {
|
) -> Result<(WebGLContextId, GLLimits, WebGLContextShareMode), String> {
|
||||||
// Creating a new GLContext may make the current bound context_id dirty.
|
// Creating a new GLContext may make the current bound context_id dirty.
|
||||||
// Clear it to ensure that make_current() is called in subsequent commands.
|
// Clear it to ensure that make_current() is called in subsequent commands.
|
||||||
|
@ -425,10 +445,6 @@ impl WebGLThread {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(ref textures) = textures {
|
|
||||||
textures.borrow_mut().insert(id, (texture_id, size));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.cached_context_info.insert(
|
self.cached_context_info.insert(
|
||||||
id,
|
id,
|
||||||
WebGLContextInfo {
|
WebGLContextInfo {
|
||||||
|
@ -451,7 +467,6 @@ impl WebGLThread {
|
||||||
context_id: WebGLContextId,
|
context_id: WebGLContextId,
|
||||||
size: Size2D<u32>,
|
size: Size2D<u32>,
|
||||||
sender: WebGLSender<Result<(), String>>,
|
sender: WebGLSender<Result<(), String>>,
|
||||||
textures: Option<&TexturesMap>,
|
|
||||||
) {
|
) {
|
||||||
let data = Self::make_current_if_needed_mut(
|
let data = Self::make_current_if_needed_mut(
|
||||||
context_id,
|
context_id,
|
||||||
|
@ -477,12 +492,6 @@ impl WebGLThread {
|
||||||
info.texture_id = texture_id;
|
info.texture_id = texture_id;
|
||||||
info.size = real_size;
|
info.size = real_size;
|
||||||
|
|
||||||
if let Some(ref textures) = textures {
|
|
||||||
textures
|
|
||||||
.borrow_mut()
|
|
||||||
.insert(context_id, (texture_id, real_size));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update WR image if needed. Resize image updates are only required for SharedTexture mode.
|
// Update WR image if needed. Resize image updates are only required for SharedTexture mode.
|
||||||
// Readback mode already updates the image every frame to send the raw pixels.
|
// Readback mode already updates the image every frame to send the raw pixels.
|
||||||
// See `handle_update_wr_image`.
|
// See `handle_update_wr_image`.
|
||||||
|
@ -508,7 +517,7 @@ impl WebGLThread {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes a WebGLContext and releases attached resources.
|
/// Removes a WebGLContext and releases attached resources.
|
||||||
fn remove_webgl_context(&mut self, context_id: WebGLContextId, textures: Option<&TexturesMap>) {
|
fn remove_webgl_context(&mut self, context_id: WebGLContextId) {
|
||||||
// Release webrender image keys.
|
// Release webrender image keys.
|
||||||
if let Some(info) = self.cached_context_info.remove(&context_id) {
|
if let Some(info) = self.cached_context_info.remove(&context_id) {
|
||||||
let mut txn = webrender_api::Transaction::new();
|
let mut txn = webrender_api::Transaction::new();
|
||||||
|
@ -527,10 +536,6 @@ impl WebGLThread {
|
||||||
// Release GL context.
|
// Release GL context.
|
||||||
self.contexts.remove(&context_id);
|
self.contexts.remove(&context_id);
|
||||||
|
|
||||||
if let Some(ref textures) = textures {
|
|
||||||
textures.borrow_mut().remove(&context_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Removing a GLContext may make the current bound context_id dirty.
|
// Removing a GLContext may make the current bound context_id dirty.
|
||||||
self.bound_context_id = None;
|
self.bound_context_id = None;
|
||||||
}
|
}
|
||||||
|
@ -628,25 +633,7 @@ impl WebGLThread {
|
||||||
self.webrender_api.send_transaction(document_id, txn);
|
self.webrender_api.send_transaction(document_id, txn);
|
||||||
},
|
},
|
||||||
DOMToTextureCommand::Lock(pipeline_id, gl_sync, sender) => {
|
DOMToTextureCommand::Lock(pipeline_id, gl_sync, sender) => {
|
||||||
let contexts = &self.contexts;
|
let result = self.handle_dom_to_texture_lock(pipeline_id, gl_sync);
|
||||||
let bound_context_id = &mut self.bound_context_id;
|
|
||||||
let result = self.dom_outputs.get(&pipeline_id).and_then(|dom_data| {
|
|
||||||
let data = Self::make_current_if_needed(
|
|
||||||
dom_data.context_id,
|
|
||||||
contexts,
|
|
||||||
bound_context_id,
|
|
||||||
);
|
|
||||||
data.and_then(|data| {
|
|
||||||
// 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.
|
|
||||||
data.ctx
|
|
||||||
.gl()
|
|
||||||
.wait_sync(gl_sync as gl::GLsync, 0, gl::TIMEOUT_IGNORED);
|
|
||||||
Some((dom_data.texture_id.get(), dom_data.size))
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send the texture id and size to WR.
|
// Send the texture id and size to WR.
|
||||||
sender.send(result).unwrap();
|
sender.send(result).unwrap();
|
||||||
},
|
},
|
||||||
|
@ -666,6 +653,28 @@ impl WebGLThread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn handle_dom_to_texture_lock(
|
||||||
|
&mut self,
|
||||||
|
pipeline_id: webrender_api::PipelineId,
|
||||||
|
gl_sync: usize,
|
||||||
|
) -> Option<(u32, Size2D<i32>)> {
|
||||||
|
let contexts = &self.contexts;
|
||||||
|
let bound_context_id = &mut self.bound_context_id;
|
||||||
|
self.dom_outputs.get(&pipeline_id).and_then(|dom_data| {
|
||||||
|
let data =
|
||||||
|
Self::make_current_if_needed(dom_data.context_id, contexts, bound_context_id);
|
||||||
|
data.and_then(|data| {
|
||||||
|
// 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.
|
||||||
|
data.ctx
|
||||||
|
.gl()
|
||||||
|
.wait_sync(gl_sync as gl::GLsync, 0, gl::TIMEOUT_IGNORED);
|
||||||
|
Some((dom_data.texture_id.get(), dom_data.size))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets a reference to a GLContextWrapper for a given WebGLContextId and makes it current if required.
|
/// Gets a reference to a GLContextWrapper for a given WebGLContextId and makes it current if required.
|
||||||
fn make_current_if_needed<'a>(
|
fn make_current_if_needed<'a>(
|
||||||
context_id: WebGLContextId,
|
context_id: WebGLContextId,
|
||||||
|
@ -843,7 +852,7 @@ impl Drop for WebGLThread {
|
||||||
// Call remove_context functions in order to correctly delete WebRender image keys.
|
// Call remove_context functions in order to correctly delete WebRender image keys.
|
||||||
let context_ids: Vec<WebGLContextId> = self.contexts.keys().map(|id| *id).collect();
|
let context_ids: Vec<WebGLContextId> = self.contexts.keys().map(|id| *id).collect();
|
||||||
for id in context_ids {
|
for id in context_ids {
|
||||||
self.remove_webgl_context(id, None);
|
self.remove_webgl_context(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -227,7 +227,7 @@ pub struct Servo<Window: WindowMethods + 'static + ?Sized> {
|
||||||
embedder_receiver: EmbedderReceiver,
|
embedder_receiver: EmbedderReceiver,
|
||||||
embedder_events: Vec<(Option<BrowserId>, EmbedderMsg)>,
|
embedder_events: Vec<(Option<BrowserId>, EmbedderMsg)>,
|
||||||
profiler_enabled: bool,
|
profiler_enabled: bool,
|
||||||
webgl_thread_data: Option<WebGLMainThread>,
|
webgl_thread_data: Option<Rc<WebGLMainThread>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -420,13 +420,14 @@ where
|
||||||
let webgl_result = gl_factory.map(|factory| {
|
let webgl_result = gl_factory.map(|factory| {
|
||||||
let (webgl_threads, thread_data, image_handler, output_handler) = WebGLThreads::new(
|
let (webgl_threads, thread_data, image_handler, output_handler) = WebGLThreads::new(
|
||||||
factory,
|
factory,
|
||||||
|
window.gl(),
|
||||||
webrender_api_sender.clone(),
|
webrender_api_sender.clone(),
|
||||||
webvr_compositor.map(|c| c as Box<_>),
|
webvr_compositor.map(|c| c as Box<_>),
|
||||||
external_images.clone(),
|
external_images.clone(),
|
||||||
if run_webgl_on_main_thread {
|
if run_webgl_on_main_thread {
|
||||||
ThreadMode::MainThread(embedder.create_event_loop_waker())
|
ThreadMode::MainThread(embedder.create_event_loop_waker())
|
||||||
} else {
|
} else {
|
||||||
ThreadMode::OffThread(window.gl())
|
ThreadMode::OffThread
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue