Skip to content

Commit

Permalink
Create surfman context from scratch in glwindow (#257)
Browse files Browse the repository at this point in the history
* Create surfman context from scratch in glwindow

Signed-off-by: Wu Yu Wei <[email protected]>

* Create connection from display handle

Signed-off-by: Wu Yuwei <[email protected]>

---------

Signed-off-by: Wu Yu Wei <[email protected]>
Signed-off-by: Wu Yuwei <[email protected]>
  • Loading branch information
wusyong authored Jan 3, 2025
1 parent db11d8d commit 4fd38cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions webxr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ log = "0.4.6"
openxr = { version = "0.19", optional = true }
serde = { version = "1.0", optional = true }
glow = "0.16"
raw-window-handle = "0.6"
surfman = { git = "https://github.com/servo/surfman", rev = "300789ddbda45c89e9165c31118bf1c4c07f89f6", features = [
"chains",
"sm-raw-window-handle-06",
] }

[target.'cfg(target_os = "windows")'.dependencies]
Expand Down
34 changes: 20 additions & 14 deletions webxr/glwindow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use euclid::{
Angle, Point2D, Rect, RigidTransform3D, Rotation3D, Size2D, Transform3D, UnknownUnit, Vector3D,
};
use glow::{self as gl, Context as Gl, HasContext};
use std::ffi::c_void;
use raw_window_handle::DisplayHandle;
use std::num::NonZeroU32;
use std::rc::Rc;
use surfman::chains::{PreserveBuffer, SwapChain, SwapChainAPI, SwapChains, SwapChainsAPI};
use surfman::{
Adapter, Connection, Context as SurfmanContext, ContextAttributes, Device as SurfmanDevice,
GLApi, NativeWidget, SurfaceAccess, SurfaceType,
Adapter, Connection, Context as SurfmanContext, ContextAttributeFlags, ContextAttributes,
Device as SurfmanDevice, GLApi, GLVersion, NativeWidget, SurfaceAccess, SurfaceType,
};
use webxr_api::util::ClipPlanes;
use webxr_api::{
Expand Down Expand Up @@ -52,6 +52,7 @@ pub trait GlWindow {
fn get_mode(&self) -> GlWindowMode {
GlWindowMode::Blit
}
fn display_handle(&self) -> DisplayHandle;
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand All @@ -72,21 +73,26 @@ pub struct GlWindowDiscovery {
connection: Connection,
adapter: Adapter,
context_attributes: ContextAttributes,
factory: Box<dyn Fn() -> Result<Box<dyn GlWindow>, ()>>,
window: Rc<dyn GlWindow>,
}

impl GlWindowDiscovery {
pub fn new(
connection: Connection,
adapter: Adapter,
context_attributes: ContextAttributes,
factory: Box<dyn Fn() -> Result<Box<dyn GlWindow>, ()>>,
) -> GlWindowDiscovery {
pub fn new(window: Rc<dyn GlWindow>) -> GlWindowDiscovery {
let connection = Connection::from_display_handle(window.display_handle()).unwrap();
let adapter = connection.create_adapter().unwrap();
let flags = ContextAttributeFlags::ALPHA
| ContextAttributeFlags::DEPTH
| ContextAttributeFlags::STENCIL;
let version = match connection.gl_api() {
GLApi::GLES => GLVersion { major: 3, minor: 0 },
GLApi::GL => GLVersion { major: 3, minor: 2 },
};
let context_attributes = ContextAttributes { flags, version };
GlWindowDiscovery {
connection,
adapter,
context_attributes,
factory,
window,
}
}
}
Expand All @@ -103,7 +109,7 @@ impl DiscoveryAPI<SurfmanGL> for GlWindowDiscovery {
let connection = self.connection.clone();
let adapter = self.adapter.clone();
let context_attributes = self.context_attributes.clone();
let window = (self.factory)().or(Err(Error::NoMatchingDevice))?;
let window = self.window.clone();
xr.run_on_main_thread(move |grand_manager| {
GlWindowDevice::new(
connection,
Expand All @@ -128,7 +134,7 @@ pub struct GlWindowDevice {
device: SurfmanDevice,
context: SurfmanContext,
gl: Rc<Gl>,
window: Box<dyn GlWindow>,
window: Rc<dyn GlWindow>,
grand_manager: LayerGrandManager<SurfmanGL>,
layer_manager: Option<LayerManager>,
target_swap_chain: Option<SwapChain<SurfmanDevice>>,
Expand Down Expand Up @@ -339,7 +345,7 @@ impl GlWindowDevice {
connection: Connection,
adapter: Adapter,
context_attributes: ContextAttributes,
window: Box<dyn GlWindow>,
window: Rc<dyn GlWindow>,
granted_features: Vec<String>,
grand_manager: LayerGrandManager<SurfmanGL>,
) -> Result<GlWindowDevice, Error> {
Expand Down

0 comments on commit 4fd38cf

Please sign in to comment.