Skip to content

Commit 999f97d

Browse files
committed
Squashed:
More post-rebase fixes, add missing flush call Post-rebase fixes Fix windows OpenGL context creation Minor fixes Squashed: Port X11 backend to x11rb Fix macOS build Win32 refactor Make the added with_scale_factor fn pub(crate) for now Remove unneeded internal HasRawWindowHandle implementation Simplify everything by making the public Window/GlContext types hold the Weak themselves Remove lifetime from Window type, refactor and split X11 backend Added functional open_parented example
1 parent 70e7af6 commit 999f97d

21 files changed

+1680
-1534
lines changed

examples/open_parented.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ struct ParentWindowHandler {
1414
}
1515

1616
impl ParentWindowHandler {
17-
pub fn new(window: &mut Window) -> Self {
18-
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
19-
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
17+
pub fn new(window: Window) -> Self {
18+
let ctx = unsafe { softbuffer::Context::new(&window) }.unwrap();
19+
let mut surface = unsafe { softbuffer::Surface::new(&ctx, &window) }.unwrap();
2020
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
2121

2222
let window_open_options = baseview::WindowOpenOptions {
@@ -28,8 +28,9 @@ impl ParentWindowHandler {
2828
#[cfg(feature = "opengl")]
2929
gl_config: None,
3030
};
31+
3132
let child_window =
32-
Window::open_parented(window, window_open_options, ChildWindowHandler::new);
33+
Window::open_parented(&window, window_open_options, ChildWindowHandler::new);
3334

3435
// TODO: no way to query physical size initially?
3536
Self {
@@ -43,7 +44,7 @@ impl ParentWindowHandler {
4344
}
4445

4546
impl WindowHandler for ParentWindowHandler {
46-
fn on_frame(&mut self, _window: &mut Window) {
47+
fn on_frame(&mut self) {
4748
let mut buf = self.surface.buffer_mut().unwrap();
4849
if self.damaged {
4950
buf.fill(0xFFAAAAAA);
@@ -52,7 +53,7 @@ impl WindowHandler for ParentWindowHandler {
5253
buf.present().unwrap();
5354
}
5455

55-
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
56+
fn on_event(&mut self, event: Event) -> EventStatus {
5657
match event {
5758
Event::Window(WindowEvent::Resized(info)) => {
5859
println!("Parent Resized: {:?}", info);
@@ -83,9 +84,9 @@ struct ChildWindowHandler {
8384
}
8485

8586
impl ChildWindowHandler {
86-
pub fn new(window: &mut Window) -> Self {
87-
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
88-
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
87+
pub fn new(window: Window) -> Self {
88+
let ctx = unsafe { softbuffer::Context::new(&window) }.unwrap();
89+
let mut surface = unsafe { softbuffer::Surface::new(&ctx, &window) }.unwrap();
8990
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
9091

9192
// TODO: no way to query physical size initially?
@@ -94,7 +95,7 @@ impl ChildWindowHandler {
9495
}
9596

9697
impl WindowHandler for ChildWindowHandler {
97-
fn on_frame(&mut self, _window: &mut Window) {
98+
fn on_frame(&mut self) {
9899
let mut buf = self.surface.buffer_mut().unwrap();
99100
if self.damaged {
100101
buf.fill(0xFFAA0000);
@@ -103,7 +104,7 @@ impl WindowHandler for ChildWindowHandler {
103104
buf.present().unwrap();
104105
}
105106

106-
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
107+
fn on_event(&mut self, event: Event) -> EventStatus {
107108
match event {
108109
Event::Window(WindowEvent::Resized(info)) => {
109110
println!("Child Resized: {:?}", info);

examples/open_window.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct OpenWindowExample {
2424
}
2525

2626
impl WindowHandler for OpenWindowExample {
27-
fn on_frame(&mut self, _window: &mut Window) {
27+
fn on_frame(&mut self) {
2828
let mut buf = self.surface.buffer_mut().unwrap();
2929
if self.damaged {
3030
buf.fill(0xFFAAAAAA);
@@ -37,7 +37,7 @@ impl WindowHandler for OpenWindowExample {
3737
}
3838
}
3939

40-
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
40+
fn on_event(&mut self, event: Event) -> EventStatus {
4141
match &event {
4242
#[cfg(target_os = "macos")]
4343
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard(&"This is a test!"),
@@ -84,8 +84,8 @@ fn main() {
8484
});
8585

8686
Window::open_blocking(window_open_options, |window| {
87-
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
88-
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
87+
let ctx = unsafe { softbuffer::Context::new(&window) }.unwrap();
88+
let mut surface = unsafe { softbuffer::Surface::new(&ctx, &window) }.unwrap();
8989
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
9090

9191
OpenWindowExample {

src/gl/mod.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
use std::ffi::c_void;
2-
use std::marker::PhantomData;
3-
4-
// On X11 creating the context is a two step process
5-
#[cfg(not(target_os = "linux"))]
6-
use raw_window_handle::RawWindowHandle;
2+
use std::rc::{Rc, Weak};
73

84
#[cfg(target_os = "windows")]
9-
mod win;
5+
pub(crate) mod win;
106
#[cfg(target_os = "windows")]
11-
use win as platform;
7+
pub(crate) use win as platform;
128

13-
// We need to use this directly within the X11 window creation to negotiate the correct visual
149
#[cfg(target_os = "linux")]
1510
pub(crate) mod x11;
1611
#[cfg(target_os = "linux")]
17-
pub(crate) use self::x11 as platform;
12+
pub(crate) use x11 as platform;
1813

1914
#[cfg(target_os = "macos")]
20-
mod macos;
15+
pub(crate) mod macos;
2116
#[cfg(target_os = "macos")]
22-
use macos as platform;
17+
pub(crate) use macos as platform;
2318

2419
#[derive(Clone, Debug)]
2520
pub struct GlConfig {
@@ -70,46 +65,37 @@ pub enum GlError {
7065
}
7166

7267
pub struct GlContext {
73-
context: platform::GlContext,
74-
phantom: PhantomData<*mut ()>,
68+
context: Weak<platform::GlContext>,
7569
}
7670

7771
impl GlContext {
78-
#[cfg(not(target_os = "linux"))]
79-
pub(crate) unsafe fn create(
80-
parent: &RawWindowHandle, config: GlConfig,
81-
) -> Result<GlContext, GlError> {
82-
platform::GlContext::create(parent, config)
83-
.map(|context| GlContext { context, phantom: PhantomData })
72+
pub(crate) fn new(context: Weak<platform::GlContext>) -> GlContext {
73+
GlContext { context }
8474
}
8575

86-
/// The X11 version needs to be set up in a different way compared to the Windows and macOS
87-
/// versions. So the platform-specific versions should be used to construct the context within
88-
/// baseview, and then this object can be passed to the user.
89-
#[cfg(target_os = "linux")]
90-
pub(crate) fn new(context: platform::GlContext) -> GlContext {
91-
GlContext { context, phantom: PhantomData }
76+
fn context(&self) -> Rc<platform::GlContext> {
77+
self.context.upgrade().expect("GL context has been destroyed")
9278
}
9379

9480
pub unsafe fn make_current(&self) {
95-
self.context.make_current();
81+
self.context().make_current();
9682
}
9783

9884
pub unsafe fn make_not_current(&self) {
99-
self.context.make_not_current();
85+
self.context().make_not_current();
10086
}
10187

10288
pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
103-
self.context.get_proc_address(symbol)
89+
self.context().get_proc_address(symbol)
10490
}
10591

10692
pub fn swap_buffers(&self) {
107-
self.context.swap_buffers();
93+
self.context().swap_buffers();
10894
}
10995

11096
/// On macOS the `NSOpenGLView` needs to be resized separtely from our main view.
11197
#[cfg(target_os = "macos")]
11298
pub(crate) fn resize(&self, size: cocoa::foundation::NSSize) {
113-
self.context.resize(size);
99+
self.context().resize(size);
114100
}
115101
}

src/gl/win.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ffi::{c_void, CString, OsStr};
22
use std::os::windows::ffi::OsStrExt;
33

4-
use raw_window_handle::RawWindowHandle;
4+
use raw_window_handle::{RawWindowHandle, Win32WindowHandle};
55

66
use winapi::shared::minwindef::{HINSTANCE, HMODULE};
77
use winapi::shared::ntdef::WCHAR;
@@ -77,13 +77,9 @@ extern "C" {
7777
}
7878

7979
impl GlContext {
80-
pub unsafe fn create(parent: &RawWindowHandle, config: GlConfig) -> Result<GlContext, GlError> {
81-
let handle = if let RawWindowHandle::Win32(handle) = parent {
82-
handle
83-
} else {
84-
return Err(GlError::InvalidWindowHandle);
85-
};
86-
80+
pub unsafe fn create(
81+
handle: &Win32WindowHandle, config: GlConfig,
82+
) -> Result<GlContext, GlError> {
8783
if handle.hwnd.is_null() {
8884
return Err(GlError::InvalidWindowHandle);
8985
}

0 commit comments

Comments
 (0)