Skip to content

Commit

Permalink
Update for upstream baseview changes + OpenGL
Browse files Browse the repository at this point in the history
OpenGL context creation is now handled by baseview instead of by
raw-gl-context for the reasons outlined in
RustAudio/baseview#115. This fixes issues on a
variety of desktop Linux configurations. raw-window-handle and
keyboard-types have also been updated to match those used by baseview.
  • Loading branch information
robbert-vdh committed Mar 17, 2022
1 parent f3f5e53 commit 01029a9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
7 changes: 3 additions & 4 deletions baseview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ description = "Baseview backend for vizia"


[dependencies]
raw-gl-context = {git = "https://github.com/glowcoil/raw-gl-context", rev = "01f6251e7605e1d7de4bf1fead39693819398ef0"}
vizia_core = { path = "../core" }
baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "f6e99e9aa6f5aeb6b721cb05e4d882a51d995909"}
keyboard-types = { version = "0.5.0", default-features = false }
raw-window-handle = "0.3"
baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "b3712638bacb3fdf2883cb5aa3f6caed0e91ac8c", features = ["opengl"] }
keyboard-types = { version = "0.6.2", default-features = false }
raw-window-handle = "0.4"
femtovg = {version = "0.3.0", default-features = false}
24 changes: 15 additions & 9 deletions baseview/src/parent_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ pub struct ParentWindow(pub *mut ::std::ffi::c_void);
#[cfg(target_os = "macos")]
unsafe impl HasRawWindowHandle for ParentWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
use raw_window_handle::macos::MacOSHandle;
use raw_window_handle::AppKitHandle;

RawWindowHandle::MacOS(MacOSHandle {
ns_view: self.0 as *mut ::std::ffi::c_void,
..MacOSHandle::empty()
})
let mut handle = AppKitHandle::empty();
handle.ns_view = self.0;

RawWindowHandle::AppKit(handle)
}
}

#[cfg(target_os = "windows")]
unsafe impl HasRawWindowHandle for ParentWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
use raw_window_handle::windows::WindowsHandle;
use raw_window_handle::Win32Handle;

let mut handle = Win32Handle::empty();
handle.hwnd = self.0;

RawWindowHandle::Windows(WindowsHandle { hwnd: self.0, ..WindowsHandle::empty() })
RawWindowHandle::Win32(handle)
}
}

#[cfg(target_os = "linux")]
unsafe impl HasRawWindowHandle for ParentWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
use raw_window_handle::unix::XcbHandle;
use raw_window_handle::XcbHandle;

let mut handle = XcbHandle::empty();
handle.window = self.0 as u32;

RawWindowHandle::Xcb(XcbHandle { window: self.0 as u32, ..XcbHandle::empty() })
RawWindowHandle::Xcb(handle)
}
}
37 changes: 20 additions & 17 deletions baseview/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{application::ApplicationRunner, Renderer};
use baseview::gl::GlConfig;
use baseview::{
Event, EventStatus, Window, WindowHandle, WindowHandler, WindowOpenOptions, WindowScalePolicy,
};
Expand All @@ -9,7 +10,6 @@ use vizia_core::*;
/// Handles a vizia_baseview application
pub(crate) struct ViziaWindow {
application: ApplicationRunner,
context: raw_gl_context::GlContext,
builder: Option<Box<dyn Fn(&mut Context) + Send>>,
on_idle: Option<Box<dyn Fn(&mut Context) + Send>>,
}
Expand All @@ -22,13 +22,14 @@ impl ViziaWindow {
builder: Option<Box<dyn Fn(&mut Context) + Send>>,
on_idle: Option<Box<dyn Fn(&mut Context) + Send>>,
) -> ViziaWindow {
let (renderer, context) = load_renderer(window);
let context = window.gl_context().expect("Window was created without OpenGL support");
let renderer = load_renderer(window);

context.make_current();
unsafe { context.make_current() };
let application = ApplicationRunner::new(cx, win_desc, renderer);
context.make_not_current();
unsafe { context.make_not_current() };

ViziaWindow { application, context, builder, on_idle }
ViziaWindow { application, builder, on_idle }
}

/// Open a new child window.
Expand All @@ -54,6 +55,7 @@ impl ViziaWindow {
),
scale: WindowScalePolicy::ScaleFactor(1.0),
//scale: WindowScalePolicy::SystemScaleFactor,
gl_config: Some(GlConfig { vsync: false, ..GlConfig::default() }),
};

Window::open_parented(
Expand Down Expand Up @@ -86,6 +88,7 @@ impl ViziaWindow {
),
scale: WindowScalePolicy::ScaleFactor(1.0),
//scale: WindowScalePolicy::SystemScaleFactor,
gl_config: Some(GlConfig { vsync: false, ..GlConfig::default() }),
};

Window::open_as_if_parented(
Expand Down Expand Up @@ -116,6 +119,7 @@ impl ViziaWindow {
),
scale: WindowScalePolicy::ScaleFactor(1.0),
//scale: WindowScalePolicy::SystemScaleFactor,
gl_config: Some(GlConfig { vsync: false, ..GlConfig::default() }),
};

Window::open_blocking(
Expand All @@ -129,17 +133,19 @@ impl ViziaWindow {
}

impl WindowHandler for ViziaWindow {
fn on_frame(&mut self, _window: &mut Window) {
fn on_frame(&mut self, window: &mut Window) {
let context = window.gl_context().expect("Window was created without OpenGL support");

self.application.rebuild(&self.builder);

self.application.on_frame_update();

self.context.make_current();
unsafe { context.make_current() };

self.application.render();
self.context.swap_buffers();
context.swap_buffers();

self.context.make_not_current();
unsafe { context.make_not_current() };
}

fn on_event(&mut self, _window: &mut Window<'_>, event: Event) -> EventStatus {
Expand All @@ -158,20 +164,17 @@ impl WindowHandler for ViziaWindow {
}
}

fn load_renderer(window: &Window) -> (Renderer, raw_gl_context::GlContext) {
let mut config = raw_gl_context::GlConfig::default();
config.vsync = false;

let context = raw_gl_context::GlContext::create(window, config).unwrap();
fn load_renderer(window: &Window) -> Renderer {
let context = window.gl_context().expect("Window was created without OpenGL support");

context.make_current();
unsafe { context.make_current() };

let renderer = unsafe {
femtovg::renderer::OpenGl::new_from_function(|s| context.get_proc_address(s) as *const _)
.expect("Cannot create renderer")
};

context.make_not_current();
unsafe { context.make_not_current() };

(renderer, context)
renderer
}
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ image = { version = "0.24.0", default-features = false, features = ["png"] } # i
morphorm = {git = "https://github.com/vizia/morphorm", features = ["rounding"], rev = "8d955d2f1d5b698e4fd1dbdfe36b3f3d9adbd19a" }
bitflags = "1.3.2"
fnv = "1.0.7"
keyboard-types = { version = "0.5.0", default-features = false }
keyboard-types = { version = "0.6.2", default-features = false }
fluent-bundle = "0.15.2"
fluent-langneg = "0.13"
fluent-syntax = "0.11.0"
Expand Down
2 changes: 1 addition & 1 deletion core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl Context {
}

pub fn add_stylesheet(&mut self, path: &str) -> Result<(), std::io::Error> {
let style_string = std::fs::read_to_string(path.clone())?;
let style_string = std::fs::read_to_string(path)?;
self.resource_manager.stylesheets.push(path.to_owned());
self.style.parse_theme(&style_string);

Expand Down
2 changes: 1 addition & 1 deletion winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "Winit backend for vizia"
[dependencies]
winit = "0.26.1"
femtovg = { version = "0.3.0", default-features = false }
keyboard-types = { version = "0.5.0", default-features = false }
keyboard-types = { version = "0.6.2", default-features = false }
vizia_core = { path = "../core", version = "0.1" }
#fnv = "1.0.7"

Expand Down

0 comments on commit 01029a9

Please sign in to comment.