Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Window from a borrowed handle to a weakly owned handle #174

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 15 additions & 12 deletions examples/render_femtovg/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use baseview::gl::GlConfig;
use baseview::gl::{GlConfig, GlContext};
use baseview::{
Event, EventStatus, MouseEvent, PhyPoint, Size, Window, WindowEvent, WindowHandler, WindowInfo,
WindowOpenOptions, WindowScalePolicy,
Expand All @@ -7,42 +7,45 @@ use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color};

struct FemtovgExample {
_window: Window,
gl_context: GlContext,
canvas: Canvas<OpenGl>,
current_size: WindowInfo,
current_mouse_position: PhyPoint,
damaged: bool,
}

impl FemtovgExample {
fn new(window: &mut Window) -> Self {
let context = window.gl_context().unwrap();
unsafe { context.make_current() };
fn new(window: Window) -> Self {
let gl_context = window.gl_context().unwrap();
unsafe { gl_context.make_current() };

let renderer =
unsafe { OpenGl::new_from_function(|s| context.get_proc_address(s)) }.unwrap();
unsafe { OpenGl::new_from_function(|s| gl_context.get_proc_address(s)) }.unwrap();

let mut canvas = Canvas::new(renderer).unwrap();
// TODO: get actual window width
canvas.set_size(512, 512, 1.0);

unsafe { context.make_not_current() };
unsafe { gl_context.make_not_current() };
Self {
canvas,
current_size: WindowInfo::from_logical_size(Size { width: 512.0, height: 512.0 }, 1.0),
current_mouse_position: PhyPoint { x: 256, y: 256 },
damaged: true,
_window: window,
gl_context,
}
}
}

impl WindowHandler for FemtovgExample {
fn on_frame(&mut self, window: &mut Window) {
fn on_frame(&mut self) {
if !self.damaged {
return;
}

let context = window.gl_context().unwrap();
unsafe { context.make_current() };
unsafe { self.gl_context.make_current() };

let screen_height = self.canvas.height();
let screen_width = self.canvas.width();
Expand Down Expand Up @@ -70,12 +73,12 @@ impl WindowHandler for FemtovgExample {

// Tell renderer to execute all drawing commands
self.canvas.flush();
context.swap_buffers();
unsafe { context.make_not_current() };
self.gl_context.swap_buffers();
unsafe { self.gl_context.make_not_current() };
self.damaged = false;
}

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(size)) => {
let phy_size = size.physical_size();
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::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
Loading