Skip to content

Commit

Permalink
More OpenGL changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Speykious committed Jun 23, 2024
1 parent ebc0d47 commit fcd6262
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ loki-draw = { git = "https://github.com/loki-chat/loki-draw.git" }

[features]
default = ["opengl"]
opengl = []
opengl = ["loki-linux/glx"]

[workspace]
members = ["loki-linux", "loki-mac"]
15 changes: 7 additions & 8 deletions examples/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use {
OpenglDrawer,
},
lokinit::prelude::*,
std::{ffi::CString, ptr},
std::ffi::CString,
};

fn main() {
lok::init();

gl::load_with(|func| {
let name = CString::new(func).unwrap();

lok::load_opengl_func(name.as_ptr()).unwrap_or(ptr::null_mut())
lok::load_opengl_func(name.as_ptr())
});

let window = lok::create_window(
Expand All @@ -27,10 +26,10 @@ fn main() {
println!("Creating surface");
let surface = window.create_surface(OpenGlConfig::default());
println!("Making surface active");
surface.make_active();
window.make_surface_active(surface);

let mut drawer = OpenglDrawer::new(600, 400, 1.0);
draw(&mut drawer, &surface);
draw(&mut drawer, window, surface);

while let Some(event) = lok::poll_event() {
match event.kind {
Expand All @@ -42,14 +41,14 @@ fn main() {
},
1.0,
);
draw(&mut drawer, &surface);
draw(&mut drawer, window, surface);
}
_ => {}
}
}
}

fn draw(drawer: &mut OpenglDrawer, surface: &WindowSurface) {
fn draw(drawer: &mut OpenglDrawer, window: WindowHandle, surface: WindowSurface) {
drawer.begin_frame();
drawer.clear();
drawer.draw_rect(&RectBlueprint {
Expand All @@ -67,5 +66,5 @@ fn draw(drawer: &mut OpenglDrawer, surface: &WindowSurface) {
alpha: 1.,
});
drawer.end_frame();
surface.flush();
window.flush_surface(surface);
}
8 changes: 6 additions & 2 deletions loki-linux/src/glx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#![allow(non_snake_case, clippy::upper_case_acronyms)]

use std::ffi::{c_char, c_int, c_uint, c_ulong};
use std::ffi::{c_char, c_int, c_uint, c_ulong, c_void};

use crate::library;
use crate::x11::{Bool, Font, Pixmap, XDisplay, XVisualInfo, XWindow, XID};
Expand Down Expand Up @@ -126,7 +126,7 @@ library! {
pub fn glXChooseVisual(display: *mut XDisplay, screen: c_int, attrib_list: *mut c_int) -> *mut XVisualInfo;
pub fn glXCreateContext(display: *mut XDisplay, vis: *mut XVisualInfo, share_list: GLXContext, direct: Bool) -> *mut XVisualInfo;
pub fn glXDestroyContext(display: *mut XDisplay, ctx: GLXContext);
pub fn glXMakeCurrent(display: *mut XDisplay, drawable: GLXDrawable) -> Bool;
pub fn glXMakeCurrent(display: *mut XDisplay, drawable: GLXDrawable, ctx: GLXContext) -> Bool;
pub fn glXCopyContext(display: *mut XDisplay, src: GLXContext, dst: GLXContext, mask: c_ulong);
pub fn glXSwapBuffers(display: *mut XDisplay, drawable: GLXDrawable);
pub fn glXCreateGLXPixmap(display: *mut XDisplay, visual: *mut XVisualInfo, pixmap: Pixmap) -> GLXPixmap;
Expand Down Expand Up @@ -170,4 +170,8 @@ library! {
pub fn glXQueryContext(display: *mut XDisplay, ctx: GLXContext, attribute: c_int, value: *mut c_int) -> c_int;
pub fn glXSelectEvent(display: *mut XDisplay, drawable: GLXDrawable, mask: c_ulong);
pub fn glXGetSelectedEvent(display: *mut XDisplay, drawable: GLXDrawable, mask: *mut c_ulong);

// GLX 1.4 and later

pub fn glXGetProcAddress(proc_name: *const u8) -> *mut c_void;
}
2 changes: 2 additions & 0 deletions loki-mac/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(target_os = "macos")]

pub mod classes;
pub mod dynload;
pub mod enums;
Expand Down
15 changes: 10 additions & 5 deletions src/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ use crate::{
prelude::WindowHandle,
};

pub trait OpenGlSurface {
fn make_active(&self);
fn flush(&self);
}

impl WindowHandle {
pub fn create_surface(&self, cfg: OpenGlConfig) -> WindowSurface {
lok::with(|backend| backend.create_window_surface(*self, cfg))
}

#[cfg(feature = "opengl")]
pub fn make_surface_active(&self, surface: crate::native::WindowSurface) {
lok::with(|backend| backend.make_surface_active(*self, surface))
}

#[cfg(feature = "opengl")]
pub fn flush_surface(&self, surface: crate::native::WindowSurface) {
lok::with(|backend| backend.flush_surface(*self, surface))
}
}

#[derive(Debug, Default)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod window;

pub mod prelude {
#[cfg(feature = "opengl")]
pub use crate::gl::{OpenGlConfig, OpenGlSurface, WindowSurface};
pub use crate::gl::{OpenGlConfig, WindowSurface};
pub use crate::{
event::{Event, EventKind, KeyboardEvent, MouseButton, MouseEvent, TouchEvent, TouchPhase},
keycode::KeyCode,
Expand Down
10 changes: 5 additions & 5 deletions src/lok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ pub trait LokinitBackend {
#[cfg(feature = "opengl")]
fn create_window_surface(&mut self, window: WindowHandle, cfg: OpenGlConfig) -> WindowSurface;
#[cfg(feature = "opengl")]
fn load_opengl_func(&mut self, proc_name: *const c_char) -> Option<*mut c_void>;
fn load_opengl_func(&mut self, proc_name: *const c_char) -> *mut c_void;
#[cfg(feature = "opengl")]
fn make_surface_active(&self, surface: WindowSurface);
fn make_surface_active(&self, window: WindowHandle, surface: WindowSurface);
#[cfg(feature = "opengl")]
fn flush_surface(&self, surface: WindowSurface);
fn flush_surface(&self, window: WindowHandle, surface: WindowSurface);
#[cfg(feature = "opengl")]
fn update_surface(&self, surface: WindowSurface);
}

thread_local! {
static INSTANCE: RefCell<Option<DefaultLokinitBackend>> = RefCell::new(None);
static INSTANCE: RefCell<Option<DefaultLokinitBackend>> = const { RefCell::new(None) };
}

/// Initializes Lokinit with a default backend.
Expand Down Expand Up @@ -107,6 +107,6 @@ fn create_window_surface(window: WindowHandle, cfg: OpenGlConfig) -> WindowSurfa
with(|instance| instance.create_window_surface(window, cfg))
}
#[cfg(feature = "opengl")]
pub fn load_opengl_func(name: *const c_char) -> Option<*mut c_void> {
pub fn load_opengl_func(name: *const c_char) -> *mut c_void {
with(|instance| instance.load_opengl_func(name))
}
2 changes: 1 addition & 1 deletion src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod types {
pub type WindowId = usize;

#[cfg(feature = "opengl")]
pub type WindowSurface = ();
pub type WindowSurface = linux::opengl::GlSurface;
}
#[cfg(target_os = "macos")]
mod types {
Expand Down
33 changes: 28 additions & 5 deletions src/native/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,42 @@ impl LokinitBackend for LinuxBackend {
&mut self,
handle: WindowHandle,
config: OpenGlConfig,
) -> GLSurface {
) -> WindowSurface {
match self {
Self::X11(x11) => x11.create_window_surface(handle, config),
Self::Wayland(wl) => wl.create_window_surface(handle, config),
}
}

#[cfg(feature = "opengl")]
fn load_opengl_func(
&mut self,
proc_name: *const std::ffi::c_char,
) -> Option<*mut std::ffi::c_void> {
todo!("load opengl func")
) -> *mut std::ffi::c_void {
match self {
Self::X11(x11) => x11.load_opengl_func(proc_name),
Self::Wayland(wl) => wl.load_opengl_func(proc_name),
}
}

#[cfg(feature = "opengl")]
fn make_surface_active(&self, handle: WindowHandle, surface: super::WindowSurface) {
match self {
Self::X11(x11) => x11.make_surface_active(handle, surface),
Self::Wayland(wl) => wl.make_surface_active(handle, surface),
}
}
}

type WindowSurface = ();
#[cfg(feature = "opengl")]
fn flush_surface(&self, handle: WindowHandle, surface: super::WindowSurface) {
match self {
Self::X11(x11) => x11.flush_surface(handle, surface),
Self::Wayland(wl) => wl.flush_surface(handle, surface),
}
}

#[cfg(feature = "opengl")]
fn update_surface(&self, surface: super::WindowSurface) {
todo!()
}
}
14 changes: 2 additions & 12 deletions src/native/linux/opengl.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
use loki_linux::glx::GLXContext;

use super::OpenGlSurface;

pub struct GlxSurface(pub(crate) GLXContext);

impl OpenGlSurface for GlxSurface {
fn make_active(&self) {
todo!()
}
fn flush(&self) {
todo!()
}
}
#[derive(Clone, Copy)]
pub struct GlSurface(pub(crate) GLXContext);
25 changes: 19 additions & 6 deletions src/native/linux/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,31 @@ impl LokinitBackend for WaylandBackend {
}

#[cfg(feature = "opengl")]
fn load_opengl_func(
&mut self,
_proc_name: *const std::ffi::c_char,
) -> Option<*mut std::ffi::c_void> {
fn load_opengl_func(&mut self, _proc_name: *const std::ffi::c_char) -> *mut std::ffi::c_void {
todo!()
}

#[cfg(feature = "opengl")]
fn create_window_surface(
&mut self,
_window: WindowHandle,
_cfg: crate::prelude::OpenGLConfig,
) -> WindowSurface {
_cfg: crate::prelude::OpenGlConfig,
) -> super::WindowSurface {
todo!()
}

#[cfg(feature = "opengl")]
fn make_surface_active(&self, window: WindowHandle, surface: crate::native::WindowSurface) {
todo!()
}

#[cfg(feature = "opengl")]
fn flush_surface(&self, window: WindowHandle, surface: super::WindowSurface) {
todo!()
}

#[cfg(feature = "opengl")]
fn update_surface(&self, surface: crate::native::WindowSurface) {
todo!()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/linux/wayland/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl WaylandBackend {
.unwrap();
self.client.call_method(
&window.wl_surface,
WlSurfaceMethod::Attach(Some(window.buffer), 0, 0),
WlSurfaceMethod::Attach(Some(window.buffer.wl_buffer()), 0, 0),
);
self.client
.call_method(&xdg_surface, XdgSurfaceMethod::AckConfigure(serial));
Expand Down
96 changes: 49 additions & 47 deletions src/native/linux/wayland/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,60 @@ impl ShmAllocator {
})
}

pub fn allocate(&mut self, client: &WaylandClient, size: u64) -> Buffer {}
pub fn allocate(&mut self, client: &WaylandClient, size: u64) -> Buffer {
todo!()
}

pub fn free(&mut self, client: &WaylandClient, buffer: Buffer) {
let Buffer { wl_buffer, range } = buffer;
client.call_method(&wl_buffer, WlBufferMethod::Destroy);

let mut idx = self.free_list.len().div_ceil(2);
loop {
let current = self.free_list.get_mut(idx).unwrap();
match current.cmp(&range) {
Ordering::Less => {
if let Some(next) = self.free_list.get(idx + 1) {
if next > &range {
if current.end + 1 == range.start && range.end + 1 == next.start {
next.start = current.start;
self.free_list.remove(idx);
} else if current.end + 1 == range.start {
current.end = range.end;
} else if range.end + 1 == next.start {
next.start = range.start;
} else {
self.free_list.insert(idx + 1, range);
}
}
} else if current.end == range.start - 1 {
current.end = range.end;
} else {
self.free_list.push(range);
}
}
Ordering::Greater => {
if let Some(prev) = self.free_list.get(idx.saturating_sub(1)) {
if prev < &range {
if prev.end + 1 == range.start && range.end + 1 == current.start {
prev.end = current.end;
self.free_list.remove(idx);
} else if prev.end + 1 == range.start {
prev.end = range.end;
} else if range.end + 1 == current.start {
current.start = range.start;
} else {
self.free_list.insert(idx, range)
}
}
} else if range.end + 1 == current.start {
current.start = range.start;
} else {
self.free_list.push(range);
}
}
Ordering::Equal => unreachable!(),
}
}
// loop {
// let current = self.free_list.get_mut(idx).unwrap();
// match range.cmp(current) {
// Ordering::Less => {
// if let Some(next) = self.free_list.get(idx + 1) {
// if next > &range {
// if current.end + 1 == range.start && range.end + 1 == next.start {
// next.start = current.start;
// self.free_list.remove(idx);
// } else if current.end + 1 == range.start {
// current.end = range.end;
// } else if range.end + 1 == next.start {
// next.start = range.start;
// } else {
// self.free_list.insert(idx + 1, range);
// }
// }
// } else if current.end == range.start - 1 {
// current.end = range.end;
// } else {
// self.free_list.push(range);
// }
// }
// Ordering::Greater => {
// if let Some(prev) = self.free_list.get(idx.saturating_sub(1)) {
// if prev < &range {
// if prev.end + 1 == range.start && range.end + 1 == current.start {
// prev.end = current.end;
// self.free_list.remove(idx);
// } else if prev.end + 1 == range.start {
// prev.end = range.end;
// } else if range.end + 1 == current.start {
// current.start = range.start;
// } else {
// self.free_list.insert(idx, range)
// }
// }
// } else if range.end + 1 == current.start {
// current.start = range.start;
// } else {
// self.free_list.push(range);
// }
// }
// Ordering::Equal => unreachable!(),
// }
// }
}
}
Loading

0 comments on commit fcd6262

Please sign in to comment.