Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit 4fd38cf

Browse files
authored
Create surfman context from scratch in glwindow (#257)
* 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]>
1 parent db11d8d commit 4fd38cf

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

webxr/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ log = "0.4.6"
3434
openxr = { version = "0.19", optional = true }
3535
serde = { version = "1.0", optional = true }
3636
glow = "0.16"
37+
raw-window-handle = "0.6"
3738
surfman = { git = "https://github.com/servo/surfman", rev = "300789ddbda45c89e9165c31118bf1c4c07f89f6", features = [
3839
"chains",
40+
"sm-raw-window-handle-06",
3941
] }
4042

4143
[target.'cfg(target_os = "windows")'.dependencies]

webxr/glwindow/mod.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use euclid::{
99
Angle, Point2D, Rect, RigidTransform3D, Rotation3D, Size2D, Transform3D, UnknownUnit, Vector3D,
1010
};
1111
use glow::{self as gl, Context as Gl, HasContext};
12-
use std::ffi::c_void;
12+
use raw_window_handle::DisplayHandle;
1313
use std::num::NonZeroU32;
1414
use std::rc::Rc;
1515
use surfman::chains::{PreserveBuffer, SwapChain, SwapChainAPI, SwapChains, SwapChainsAPI};
1616
use surfman::{
17-
Adapter, Connection, Context as SurfmanContext, ContextAttributes, Device as SurfmanDevice,
18-
GLApi, NativeWidget, SurfaceAccess, SurfaceType,
17+
Adapter, Connection, Context as SurfmanContext, ContextAttributeFlags, ContextAttributes,
18+
Device as SurfmanDevice, GLApi, GLVersion, NativeWidget, SurfaceAccess, SurfaceType,
1919
};
2020
use webxr_api::util::ClipPlanes;
2121
use webxr_api::{
@@ -52,6 +52,7 @@ pub trait GlWindow {
5252
fn get_mode(&self) -> GlWindowMode {
5353
GlWindowMode::Blit
5454
}
55+
fn display_handle(&self) -> DisplayHandle;
5556
}
5657

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

7879
impl GlWindowDiscovery {
79-
pub fn new(
80-
connection: Connection,
81-
adapter: Adapter,
82-
context_attributes: ContextAttributes,
83-
factory: Box<dyn Fn() -> Result<Box<dyn GlWindow>, ()>>,
84-
) -> GlWindowDiscovery {
80+
pub fn new(window: Rc<dyn GlWindow>) -> GlWindowDiscovery {
81+
let connection = Connection::from_display_handle(window.display_handle()).unwrap();
82+
let adapter = connection.create_adapter().unwrap();
83+
let flags = ContextAttributeFlags::ALPHA
84+
| ContextAttributeFlags::DEPTH
85+
| ContextAttributeFlags::STENCIL;
86+
let version = match connection.gl_api() {
87+
GLApi::GLES => GLVersion { major: 3, minor: 0 },
88+
GLApi::GL => GLVersion { major: 3, minor: 2 },
89+
};
90+
let context_attributes = ContextAttributes { flags, version };
8591
GlWindowDiscovery {
8692
connection,
8793
adapter,
8894
context_attributes,
89-
factory,
95+
window,
9096
}
9197
}
9298
}
@@ -103,7 +109,7 @@ impl DiscoveryAPI<SurfmanGL> for GlWindowDiscovery {
103109
let connection = self.connection.clone();
104110
let adapter = self.adapter.clone();
105111
let context_attributes = self.context_attributes.clone();
106-
let window = (self.factory)().or(Err(Error::NoMatchingDevice))?;
112+
let window = self.window.clone();
107113
xr.run_on_main_thread(move |grand_manager| {
108114
GlWindowDevice::new(
109115
connection,
@@ -128,7 +134,7 @@ pub struct GlWindowDevice {
128134
device: SurfmanDevice,
129135
context: SurfmanContext,
130136
gl: Rc<Gl>,
131-
window: Box<dyn GlWindow>,
137+
window: Rc<dyn GlWindow>,
132138
grand_manager: LayerGrandManager<SurfmanGL>,
133139
layer_manager: Option<LayerManager>,
134140
target_swap_chain: Option<SwapChain<SurfmanDevice>>,
@@ -339,7 +345,7 @@ impl GlWindowDevice {
339345
connection: Connection,
340346
adapter: Adapter,
341347
context_attributes: ContextAttributes,
342-
window: Box<dyn GlWindow>,
348+
window: Rc<dyn GlWindow>,
343349
granted_features: Vec<String>,
344350
grand_manager: LayerGrandManager<SurfmanGL>,
345351
) -> Result<GlWindowDevice, Error> {

0 commit comments

Comments
 (0)