Skip to content

Commit

Permalink
Squashed:
Browse files Browse the repository at this point in the history
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
  • Loading branch information
prokopyl committed Mar 27, 2024
1 parent f5ae585 commit 4a82bfc
Show file tree
Hide file tree
Showing 21 changed files with 1,680 additions and 1,534 deletions.
23 changes: 12 additions & 11 deletions examples/open_parented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ struct ParentWindowHandler {
}

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

let window_open_options = baseview::WindowOpenOptions {
Expand All @@ -28,8 +28,9 @@ impl ParentWindowHandler {
#[cfg(feature = "opengl")]
gl_config: None,
};

let child_window =
Window::open_parented(window, window_open_options, ChildWindowHandler::new);
Window::open_parented(&window, window_open_options, ChildWindowHandler::new);

// TODO: no way to query physical size initially?
Self {
Expand All @@ -43,7 +44,7 @@ impl ParentWindowHandler {
}

impl WindowHandler for ParentWindowHandler {
fn on_frame(&mut self, _window: &mut Window) {
fn on_frame(&mut self) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
buf.fill(0xFFAAAAAA);
Expand All @@ -52,7 +53,7 @@ impl WindowHandler for ParentWindowHandler {
buf.present().unwrap();
}

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&mut self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Parent Resized: {:?}", info);
Expand Down Expand Up @@ -83,9 +84,9 @@ struct ChildWindowHandler {
}

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

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

impl WindowHandler for ChildWindowHandler {
fn on_frame(&mut self, _window: &mut Window) {
fn on_frame(&mut self) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
buf.fill(0xFFAA0000);
Expand All @@ -103,7 +104,7 @@ impl WindowHandler for ChildWindowHandler {
buf.present().unwrap();
}

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&mut self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Child Resized: {:?}", info);
Expand Down
8 changes: 4 additions & 4 deletions examples/open_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct OpenWindowExample {
}

impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self, _window: &mut Window) {
fn on_frame(&mut self) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
buf.fill(0xFFAAAAAA);
Expand All @@ -37,7 +37,7 @@ impl WindowHandler for OpenWindowExample {
}
}

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

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

OpenWindowExample {
Expand Down
46 changes: 16 additions & 30 deletions src/gl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
use std::ffi::c_void;
use std::marker::PhantomData;

// On X11 creating the context is a two step process
#[cfg(not(target_os = "linux"))]
use raw_window_handle::RawWindowHandle;
use std::rc::{Rc, Weak};

#[cfg(target_os = "windows")]
mod win;
pub(crate) mod win;
#[cfg(target_os = "windows")]
use win as platform;
pub(crate) use win as platform;

// We need to use this directly within the X11 window creation to negotiate the correct visual
#[cfg(target_os = "linux")]
pub(crate) mod x11;
#[cfg(target_os = "linux")]
pub(crate) use self::x11 as platform;
pub(crate) use x11 as platform;

#[cfg(target_os = "macos")]
mod macos;
pub(crate) mod macos;
#[cfg(target_os = "macos")]
use macos as platform;
pub(crate) use macos as platform;

#[derive(Clone, Debug)]
pub struct GlConfig {
Expand Down Expand Up @@ -70,46 +65,37 @@ pub enum GlError {
}

pub struct GlContext {
context: platform::GlContext,
phantom: PhantomData<*mut ()>,
context: Weak<platform::GlContext>,
}

impl GlContext {
#[cfg(not(target_os = "linux"))]
pub(crate) unsafe fn create(
parent: &RawWindowHandle, config: GlConfig,
) -> Result<GlContext, GlError> {
platform::GlContext::create(parent, config)
.map(|context| GlContext { context, phantom: PhantomData })
pub(crate) fn new(context: Weak<platform::GlContext>) -> GlContext {
GlContext { context }
}

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

pub unsafe fn make_current(&self) {
self.context.make_current();
self.context().make_current();
}

pub unsafe fn make_not_current(&self) {
self.context.make_not_current();
self.context().make_not_current();
}

pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
self.context.get_proc_address(symbol)
self.context().get_proc_address(symbol)
}

pub fn swap_buffers(&self) {
self.context.swap_buffers();
self.context().swap_buffers();
}

/// On macOS the `NSOpenGLView` needs to be resized separtely from our main view.
#[cfg(target_os = "macos")]
pub(crate) fn resize(&self, size: cocoa::foundation::NSSize) {
self.context.resize(size);
self.context().resize(size);
}
}
12 changes: 4 additions & 8 deletions src/gl/win.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::{c_void, CString, OsStr};
use std::os::windows::ffi::OsStrExt;

use raw_window_handle::RawWindowHandle;
use raw_window_handle::{RawWindowHandle, Win32WindowHandle};

use winapi::shared::minwindef::{HINSTANCE, HMODULE};
use winapi::shared::ntdef::WCHAR;
Expand Down Expand Up @@ -77,13 +77,9 @@ extern "C" {
}

impl GlContext {
pub unsafe fn create(parent: &RawWindowHandle, config: GlConfig) -> Result<GlContext, GlError> {
let handle = if let RawWindowHandle::Win32(handle) = parent {
handle
} else {
return Err(GlError::InvalidWindowHandle);
};

pub unsafe fn create(
handle: &Win32WindowHandle, config: GlConfig,
) -> Result<GlContext, GlError> {
if handle.hwnd.is_null() {
return Err(GlError::InvalidWindowHandle);
}
Expand Down
Loading

0 comments on commit 4a82bfc

Please sign in to comment.