use super::*;
unsafe extern "C" {
fn dlopen(filename: *const c_char, flag: i32) -> *mut c_void;
fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
fn open(pathname: *const c_char, flags: i32, ...) -> i32;
fn unlink(pathname: *const c_char) -> i32;
fn ftruncate(fd: i32, length: i64) -> i32;
fn mmap(
addr: *mut c_void,
length: usize,
prot: i32,
flags: i32,
fd: i32,
offset: isize,
) -> *mut c_void;
fn munmap(addr: *mut c_void, length: usize) -> i32;
fn close(fd: i32) -> i32;
fn getpid() -> i32;
fn poll(fds: *mut c_void, nfds: usize, timeout: i32) -> i32;
fn pipe2(fds: *mut i32, flags: i32) -> i32;
fn read(fd: i32, buf: *mut c_void, count: usize) -> isize;
fn write(fd: i32, buf: *const c_void, count: usize) -> isize;
}
pub(crate) fn linux_dlsym(handle: *mut c_void, sym: &'static str) -> *mut c_void {
if handle.is_null() {
return null_mut();
}
unsafe { dlsym(handle, sym.as_ptr().cast::<c_char>()) }
}
pub(crate) fn linux_x11_syms() -> &'static LinuxX11Syms {
LINUX_X11_SYMS.get_or_init(|| -> LinuxX11Syms {
unsafe {
let x11: *mut c_void = dlopen(LINUX_LIB_X11.as_ptr().cast::<c_char>(), RTLD_LAZY);
if x11.is_null() {
return LinuxX11Syms::default();
}
LinuxX11Syms {
lib: x11 as usize,
open_display: linux_dlsym(x11, SYM_X_OPEN_DISPLAY) as usize,
default_screen: linux_dlsym(x11, SYM_X_DEFAULT_SCREEN) as usize,
root_window: linux_dlsym(x11, SYM_X_ROOT_WINDOW) as usize,
create_simple_window: linux_dlsym(x11, SYM_X_CREATE_SIMPLE_WINDOW) as usize,
store_name: linux_dlsym(x11, SYM_X_STORE_NAME) as usize,
map_window: linux_dlsym(x11, SYM_X_MAP_WINDOW) as usize,
resize_window: linux_dlsym(x11, SYM_X_RESIZE_WINDOW) as usize,
destroy_window: linux_dlsym(x11, SYM_X_DESTROY_WINDOW) as usize,
flush: linux_dlsym(x11, SYM_X_FLUSH) as usize,
default_gc: linux_dlsym(x11, SYM_X_DEFAULT_GC) as usize,
default_visual: linux_dlsym(x11, SYM_X_DEFAULT_VISUAL) as usize,
default_depth: linux_dlsym(x11, SYM_X_DEFAULT_DEPTH) as usize,
create_image: linux_dlsym(x11, SYM_X_CREATE_IMAGE) as usize,
put_image: linux_dlsym(x11, SYM_X_PUT_IMAGE) as usize,
free: linux_dlsym(x11, SYM_X_FREE) as usize,
select_input: linux_dlsym(x11, SYM_X_SELECT_INPUT) as usize,
intern_atom: linux_dlsym(x11, SYM_X_INTERN_ATOM) as usize,
set_wm_protocols: linux_dlsym(x11, SYM_X_SET_WM_PROTOCOLS) as usize,
pending: linux_dlsym(x11, SYM_X_PENDING) as usize,
next_event: linux_dlsym(x11, SYM_X_NEXT_EVENT) as usize,
lookup_string: linux_dlsym(x11, SYM_X_LOOKUP_STRING) as usize,
resource_manager_string: linux_dlsym(x11, SYM_X_RESOURCE_MANAGER_STRING) as usize,
connection_number: linux_dlsym(x11, SYM_X_CONNECTION_NUMBER) as usize,
}
}
})
}
pub(crate) fn linux_xrandr_syms() -> &'static LinuxXRandrSyms {
LINUX_XRANDR_SYMS.get_or_init(|| -> LinuxXRandrSyms {
unsafe {
let xrandr: *mut c_void = dlopen(LINUX_LIB_XRANDR.as_ptr().cast::<c_char>(), RTLD_LAZY);
if xrandr.is_null() {
return LinuxXRandrSyms::default();
}
LinuxXRandrSyms {
get_screen_info: linux_dlsym(xrandr, SYM_XRR_GET_SCREEN_INFO) as usize,
config_current_rate: linux_dlsym(xrandr, SYM_XRR_CONFIG_CURRENT_RATE) as usize,
free_screen_config_info: linux_dlsym(xrandr, SYM_XRR_FREE_SCREEN_CONFIG_INFO)
as usize,
}
}
})
}
pub(crate) fn linux_xshm_syms() -> &'static LinuxXShmSyms {
LINUX_XSHM_SYMS.get_or_init(|| -> LinuxXShmSyms {
unsafe {
let xext: *mut c_void = dlopen(LINUX_LIB_XEXT.as_ptr().cast::<c_char>(), RTLD_LAZY);
if xext.is_null() {
return LinuxXShmSyms::default();
}
LinuxXShmSyms {
query_extension: linux_dlsym(xext, SYM_XSHM_QUERY_EXTENSION) as usize,
create_image: linux_dlsym(xext, SYM_XSHM_CREATE_IMAGE) as usize,
attach: linux_dlsym(xext, SYM_XSHM_ATTACH) as usize,
detach: linux_dlsym(xext, SYM_XSHM_DETACH) as usize,
put_image: linux_dlsym(xext, SYM_XSHM_PUT_IMAGE) as usize,
}
}
})
}
pub(crate) fn linux_wayland_syms() -> &'static LinuxWaylandSyms {
LINUX_WAYLAND_SYMS.get_or_init(|| -> LinuxWaylandSyms {
unsafe {
let vk: *mut c_void = dlopen(LINUX_LIB_VULKAN.as_ptr().cast::<c_char>(), RTLD_LAZY);
let vulkan_wsi_available: bool =
!vk.is_null() && !linux_dlsym(vk, SYM_VK_CREATE_WAYLAND_SURFACE).is_null();
let xlib_vulkan_wsi_available: bool =
!vk.is_null() && !linux_dlsym(vk, SYM_VK_CREATE_XLIB_SURFACE).is_null();
let wl: *mut c_void = dlopen(LINUX_LIB_WAYLAND.as_ptr().cast::<c_char>(), RTLD_LAZY);
if wl.is_null() {
return LinuxWaylandSyms {
vulkan_wsi_available,
xlib_vulkan_wsi_available,
..LinuxWaylandSyms::default()
};
}
LinuxWaylandSyms {
lib: wl as usize,
display_connect: linux_dlsym(wl, SYM_WL_DISPLAY_CONNECT) as usize,
display_disconnect: linux_dlsym(wl, SYM_WL_DISPLAY_DISCONNECT) as usize,
display_dispatch_pending: linux_dlsym(wl, SYM_WL_DISPLAY_DISPATCH_PENDING) as usize,
display_dispatch: linux_dlsym(wl, SYM_WL_DISPLAY_DISPATCH) as usize,
display_flush: linux_dlsym(wl, SYM_WL_DISPLAY_FLUSH) as usize,
display_roundtrip: linux_dlsym(wl, SYM_WL_DISPLAY_ROUNDTRIP) as usize,
display_get_error: linux_dlsym(wl, SYM_WL_DISPLAY_GET_ERROR) as usize,
display_get_fd: linux_dlsym(wl, SYM_WL_DISPLAY_GET_FD) as usize,
display_prepare_read: linux_dlsym(wl, SYM_WL_DISPLAY_PREPARE_READ) as usize,
display_read_events: linux_dlsym(wl, SYM_WL_DISPLAY_READ_EVENTS) as usize,
display_cancel_read: linux_dlsym(wl, SYM_WL_DISPLAY_CANCEL_READ) as usize,
proxy_marshal_flags: linux_dlsym(wl, SYM_WL_PROXY_MARSHAL_FLAGS) as usize,
proxy_add_listener: linux_dlsym(wl, SYM_WL_PROXY_ADD_LISTENER) as usize,
proxy_destroy: linux_dlsym(wl, SYM_WL_PROXY_DESTROY) as usize,
proxy_get_version: linux_dlsym(wl, SYM_WL_PROXY_GET_VERSION) as usize,
registry_interface: linux_dlsym(wl, SYM_WL_REGISTRY_INTERFACE) as usize,
compositor_interface: linux_dlsym(wl, SYM_WL_COMPOSITOR_INTERFACE) as usize,
surface_interface: linux_dlsym(wl, SYM_WL_SURFACE_INTERFACE) as usize,
shm_interface: linux_dlsym(wl, SYM_WL_SHM_INTERFACE) as usize,
shm_pool_interface: linux_dlsym(wl, SYM_WL_SHM_POOL_INTERFACE) as usize,
buffer_interface: linux_dlsym(wl, SYM_WL_BUFFER_INTERFACE) as usize,
callback_interface: linux_dlsym(wl, SYM_WL_CALLBACK_INTERFACE) as usize,
vulkan_wsi_available,
xlib_vulkan_wsi_available,
}
}
})
}
pub(crate) fn linux_egl_gles_syms() -> &'static LinuxEglGlesSyms {
LINUX_EGL_GLES_SYMS.get_or_init(|| -> LinuxEglGlesSyms {
unsafe {
let egl: *mut c_void = dlopen(LINUX_LIB_EGL.as_ptr().cast::<c_char>(), RTLD_LAZY);
let gles: *mut c_void = dlopen(LINUX_LIB_GLESV2.as_ptr().cast::<c_char>(), RTLD_LAZY);
let wl_egl: *mut c_void =
dlopen(LINUX_LIB_WAYLAND_EGL.as_ptr().cast::<c_char>(), RTLD_LAZY);
if egl.is_null() || gles.is_null() {
return LinuxEglGlesSyms::default();
}
let syms: LinuxEglGlesSyms = LinuxEglGlesSyms {
ready: false,
wayland_egl_ready: false,
egl_get_display: linux_dlsym(egl, SYM_EGL_GET_DISPLAY) as usize,
egl_initialize: linux_dlsym(egl, SYM_EGL_INITIALIZE) as usize,
egl_bind_api: linux_dlsym(egl, SYM_EGL_BIND_API) as usize,
egl_choose_config: linux_dlsym(egl, SYM_EGL_CHOOSE_CONFIG) as usize,
egl_create_context: linux_dlsym(egl, SYM_EGL_CREATE_CONTEXT) as usize,
egl_create_window_surface: linux_dlsym(egl, SYM_EGL_CREATE_WINDOW_SURFACE) as usize,
egl_make_current: linux_dlsym(egl, SYM_EGL_MAKE_CURRENT) as usize,
egl_swap_interval: linux_dlsym(egl, SYM_EGL_SWAP_INTERVAL) as usize,
egl_swap_buffers: linux_dlsym(egl, SYM_EGL_SWAP_BUFFERS) as usize,
egl_destroy_surface: linux_dlsym(egl, SYM_EGL_DESTROY_SURFACE) as usize,
egl_destroy_context: linux_dlsym(egl, SYM_EGL_DESTROY_CONTEXT) as usize,
egl_terminate: linux_dlsym(egl, SYM_EGL_TERMINATE) as usize,
wl_egl_window_create: linux_dlsym(wl_egl, SYM_WL_EGL_WINDOW_CREATE) as usize,
wl_egl_window_resize: linux_dlsym(wl_egl, SYM_WL_EGL_WINDOW_RESIZE) as usize,
wl_egl_window_destroy: linux_dlsym(wl_egl, SYM_WL_EGL_WINDOW_DESTROY) as usize,
gl_create_shader: linux_dlsym(gles, SYM_GL_CREATE_SHADER) as usize,
gl_shader_source: linux_dlsym(gles, SYM_GL_SHADER_SOURCE) as usize,
gl_compile_shader: linux_dlsym(gles, SYM_GL_COMPILE_SHADER) as usize,
gl_get_shaderiv: linux_dlsym(gles, SYM_GL_GET_SHADERIV) as usize,
gl_delete_shader: linux_dlsym(gles, SYM_GL_DELETE_SHADER) as usize,
gl_create_program: linux_dlsym(gles, SYM_GL_CREATE_PROGRAM) as usize,
gl_attach_shader: linux_dlsym(gles, SYM_GL_ATTACH_SHADER) as usize,
gl_link_program: linux_dlsym(gles, SYM_GL_LINK_PROGRAM) as usize,
gl_get_programiv: linux_dlsym(gles, SYM_GL_GET_PROGRAMIV) as usize,
gl_delete_program: linux_dlsym(gles, SYM_GL_DELETE_PROGRAM) as usize,
gl_use_program: linux_dlsym(gles, SYM_GL_USE_PROGRAM) as usize,
gl_get_uniform_location: linux_dlsym(gles, SYM_GL_GET_UNIFORM_LOCATION) as usize,
gl_uniform1i: linux_dlsym(gles, SYM_GL_UNIFORM1I) as usize,
gl_uniform2f: linux_dlsym(gles, SYM_GL_UNIFORM2F) as usize,
gl_gen_vertex_arrays: linux_dlsym(gles, SYM_GL_GEN_VERTEX_ARRAYS) as usize,
gl_bind_vertex_array: linux_dlsym(gles, SYM_GL_BIND_VERTEX_ARRAY) as usize,
gl_delete_vertex_arrays: linux_dlsym(gles, SYM_GL_DELETE_VERTEX_ARRAYS) as usize,
gl_gen_buffers: linux_dlsym(gles, SYM_GL_GEN_BUFFERS) as usize,
gl_bind_buffer: linux_dlsym(gles, SYM_GL_BIND_BUFFER) as usize,
gl_buffer_data: linux_dlsym(gles, SYM_GL_BUFFER_DATA) as usize,
gl_buffer_sub_data: linux_dlsym(gles, SYM_GL_BUFFER_SUB_DATA) as usize,
gl_delete_buffers: linux_dlsym(gles, SYM_GL_DELETE_BUFFERS) as usize,
gl_enable_vertex_attrib_array: linux_dlsym(gles, SYM_GL_ENABLE_VERTEX_ATTRIB_ARRAY)
as usize,
gl_vertex_attrib_pointer: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_POINTER) as usize,
gl_vertex_attrib_ipointer: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_IPOINTER)
as usize,
gl_vertex_attrib_divisor: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_DIVISOR) as usize,
gl_gen_textures: linux_dlsym(gles, SYM_GL_GEN_TEXTURES) as usize,
gl_bind_texture: linux_dlsym(gles, SYM_GL_BIND_TEXTURE) as usize,
gl_tex_parameteri: linux_dlsym(gles, SYM_GL_TEX_PARAMETERI) as usize,
gl_tex_storage_2d: linux_dlsym(gles, SYM_GL_TEX_STORAGE_2D) as usize,
gl_tex_image_2d: linux_dlsym(gles, SYM_GL_TEX_IMAGE_2D) as usize,
gl_tex_sub_image_2d: linux_dlsym(gles, SYM_GL_TEX_SUB_IMAGE_2D) as usize,
gl_delete_textures: linux_dlsym(gles, SYM_GL_DELETE_TEXTURES) as usize,
gl_active_texture: linux_dlsym(gles, SYM_GL_ACTIVE_TEXTURE) as usize,
gl_pixel_storei: linux_dlsym(gles, SYM_GL_PIXEL_STOREI) as usize,
gl_viewport: linux_dlsym(gles, SYM_GL_VIEWPORT) as usize,
gl_enable: linux_dlsym(gles, SYM_GL_ENABLE) as usize,
gl_disable: linux_dlsym(gles, SYM_GL_DISABLE) as usize,
gl_blend_func_separate: linux_dlsym(gles, SYM_GL_BLEND_FUNC_SEPARATE) as usize,
gl_scissor: linux_dlsym(gles, SYM_GL_SCISSOR) as usize,
gl_clear_color: linux_dlsym(gles, SYM_GL_CLEAR_COLOR) as usize,
gl_clear: linux_dlsym(gles, SYM_GL_CLEAR) as usize,
gl_draw_arrays_instanced: linux_dlsym(gles, SYM_GL_DRAW_ARRAYS_INSTANCED) as usize,
gl_draw_arrays: linux_dlsym(gles, SYM_GL_DRAW_ARRAYS) as usize,
gl_uniform4f: linux_dlsym(gles, SYM_GL_UNIFORM4F) as usize,
gl_uniform_matrix4fv: linux_dlsym(gles, SYM_GL_UNIFORM_MATRIX4FV) as usize,
gl_get_integerv: linux_dlsym(gles, SYM_GL_GET_INTEGERV) as usize,
gl_gen_framebuffers: linux_dlsym(gles, SYM_GL_GEN_FRAMEBUFFERS) as usize,
gl_bind_framebuffer: linux_dlsym(gles, SYM_GL_BIND_FRAMEBUFFER) as usize,
gl_framebuffer_texture_2d: linux_dlsym(gles, SYM_GL_FRAMEBUFFER_TEXTURE_2D)
as usize,
gl_delete_framebuffers: linux_dlsym(gles, SYM_GL_DELETE_FRAMEBUFFERS) as usize,
gl_gen_renderbuffers: linux_dlsym(gles, SYM_GL_GEN_RENDERBUFFERS) as usize,
gl_bind_renderbuffer: linux_dlsym(gles, SYM_GL_BIND_RENDERBUFFER) as usize,
gl_renderbuffer_storage: linux_dlsym(gles, SYM_GL_RENDERBUFFER_STORAGE) as usize,
gl_framebuffer_renderbuffer: linux_dlsym(gles, SYM_GL_FRAMEBUFFER_RENDERBUFFER)
as usize,
gl_delete_renderbuffers: linux_dlsym(gles, SYM_GL_DELETE_RENDERBUFFERS) as usize,
gl_depth_mask: linux_dlsym(gles, SYM_GL_DEPTH_MASK) as usize,
gl_depth_func: linux_dlsym(gles, SYM_GL_DEPTH_FUNC) as usize,
gl_clear_depthf: linux_dlsym(gles, SYM_GL_CLEAR_DEPTHF) as usize,
};
let core_ok: bool = syms.get_egl_get_display() != 0
&& syms.get_egl_initialize() != 0
&& syms.get_egl_choose_config() != 0
&& syms.get_egl_create_context() != 0
&& syms.get_egl_create_window_surface() != 0
&& syms.get_egl_make_current() != 0
&& syms.get_egl_swap_buffers() != 0
&& syms.get_gl_create_shader() != 0
&& syms.get_gl_create_program() != 0
&& syms.get_gl_draw_arrays_instanced() != 0
&& syms.get_gl_vertex_attrib_divisor() != 0;
let wl_ok: bool = syms.get_wl_egl_window_create() != 0
&& syms.get_wl_egl_window_resize() != 0
&& syms.get_wl_egl_window_destroy() != 0;
LinuxEglGlesSyms {
ready: core_ok,
wayland_egl_ready: core_ok && wl_ok,
..syms
}
}
})
}
pub(crate) fn linux_create_wake_pipe() -> (i32, i32) {
let mut fds: [i32; 2] = [-1, -1];
let rc: i32 = unsafe { pipe2(fds.as_mut_ptr(), LINUX_PIPE_NONBLOCK_CLOEXEC) };
if rc == 0 { (fds[0], fds[1]) } else { (-1, -1) }
}
pub(crate) fn linux_signal_wake_pipe(write_fd: i32) -> bool {
if write_fd < 0 {
return false;
}
let byte: [u8; 1] = [1];
unsafe { write(write_fd, byte.as_ptr().cast::<c_void>(), 1) == 1 }
}
pub(crate) fn linux_drain_wake_pipe(read_fd: i32) {
if read_fd < 0 {
return;
}
let mut scratch: [u8; 64] = [0; 64];
unsafe {
while read(
read_fd,
scratch.as_mut_ptr().cast::<c_void>(),
scratch.len(),
) > 0
{}
}
}
pub(crate) unsafe extern "C" fn linux_wl_frame_callback_done(
data: *mut c_void,
callback: *mut c_void,
_callback_data: u32,
) {
if !data.is_null() {
let pending: &AtomicBool = unsafe { &*data.cast::<AtomicBool>() };
pending.store(false, Ordering::Release);
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if !callback.is_null() && wl.proxy_destroy != 0 {
unsafe {
let destroy_fn: unsafe extern "C" fn(*mut c_void) =
transmute(wl.proxy_destroy as *const c_void);
destroy_fn(callback);
}
}
}
pub(crate) fn linux_wl_arm_frame_callback(wl_surface: usize, vsync_flag: &AtomicBool) -> bool {
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl_surface == 0
|| wl.proxy_marshal_flags == 0
|| wl.proxy_add_listener == 0
|| wl.callback_interface == 0
{
return false;
}
if vsync_flag.swap(true, Ordering::AcqRel) {
return true;
}
unsafe {
let marshal: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
...
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
let add_listener: unsafe extern "C" fn(*mut c_void, *const c_void, *mut c_void) -> i32 =
transmute(wl.proxy_add_listener as *const c_void);
let cb: *mut c_void = marshal(
wl_surface as *mut c_void,
WL_OPCODE_SURFACE_FRAME,
wl.callback_interface as *const c_void,
1,
0,
null_mut::<c_void>(),
);
if cb.is_null() {
vsync_flag.store(false, Ordering::Release);
return false;
}
let listener_ptr: *const c_void =
(&WL_CALLBACK_LISTENER as *const WlCallbackListener).cast();
let data_ptr: *mut c_void = (vsync_flag as *const AtomicBool).cast_mut().cast();
let _: i32 = add_listener(cb, listener_ptr, data_ptr);
true
}
}
unsafe fn linux_gles_compile_shader(
syms: &LinuxEglGlesSyms,
stage: u32,
source: &str,
) -> Option<u32> {
unsafe {
let create_shader: unsafe extern "C" fn(u32) -> u32 =
transmute(syms.get_gl_create_shader() as *const c_void);
let shader_source: unsafe extern "C" fn(u32, i32, *const *const c_char, *const i32) =
transmute(syms.get_gl_shader_source() as *const c_void);
let compile_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_compile_shader() as *const c_void);
let get_shaderiv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_shaderiv() as *const c_void);
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
let shader: u32 = create_shader(stage);
if shader == 0 {
return None;
}
let ptrs: [*const c_char; 1] = [source.as_ptr().cast::<c_char>()];
let lens: [i32; 1] = [source.len() as i32];
shader_source(shader, 1, ptrs.as_ptr(), lens.as_ptr());
compile_shader(shader);
let mut ok: i32 = 0;
get_shaderiv(shader, LINUX_GL_COMPILE_STATUS, &mut ok);
if ok == 0 {
delete_shader(shader);
return None;
}
Some(shader)
}
}
unsafe fn linux_gles_create_texture(
syms: &LinuxEglGlesSyms,
internal_fmt: u32,
pixel_fmt: u32,
width: u32,
height: u32,
pixels: &[u8],
alignment: i32,
) -> u32 {
unsafe {
let gen_textures: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_textures() as *const c_void);
let bind_texture: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let tex_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_tex_parameteri() as *const c_void);
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
let mut tex: u32 = 0;
gen_textures(1, &mut tex);
bind_texture(LINUX_GL_TEXTURE_2D, tex);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_S,
LINUX_GL_CLAMP_TO_EDGE,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_T,
LINUX_GL_CLAMP_TO_EDGE,
);
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, alignment);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
if syms.get_gl_tex_storage_2d() != 0 {
let tex_storage: unsafe extern "C" fn(u32, i32, u32, i32, i32) =
transmute(syms.get_gl_tex_storage_2d() as *const c_void);
tex_storage(
LINUX_GL_TEXTURE_2D,
1,
internal_fmt,
width.max(1) as i32,
height.max(1) as i32,
);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
width.max(1) as i32,
height.max(1) as i32,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
} else {
let tex_image: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_image_2d() as *const c_void);
tex_image(
LINUX_GL_TEXTURE_2D,
0,
internal_fmt as i32,
width.max(1) as i32,
height.max(1) as i32,
0,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
}
tex
}
}
pub(crate) fn linux_egl_create_surface(
is_wayland: bool,
display: usize,
window: u64,
width: u32,
height: u32,
) -> Option<LinuxEglGlesSurface> {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
if display == 0 || window == 0 || !syms.get_ready() {
return None;
}
if is_wayland && !syms.get_wayland_egl_ready() {
return None;
}
let w: u32 = width.max(1);
let h: u32 = height.max(1);
unsafe {
let egl_get_display: unsafe extern "C" fn(*mut c_void) -> *mut c_void =
transmute(syms.get_egl_get_display() as *const c_void);
let egl_initialize: unsafe extern "C" fn(*mut c_void, *mut i32, *mut i32) -> u32 =
transmute(syms.get_egl_initialize() as *const c_void);
let egl_bind_api: unsafe extern "C" fn(u32) -> u32 =
transmute(syms.get_egl_bind_api() as *const c_void);
let egl_choose_config: unsafe extern "C" fn(
*mut c_void,
*const i32,
*mut *mut c_void,
i32,
*mut i32,
) -> u32 = transmute(syms.get_egl_choose_config() as *const c_void);
let egl_create_context: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*const i32,
) -> *mut c_void = transmute(syms.get_egl_create_context() as *const c_void);
let egl_create_window_surface: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
usize,
*const i32,
) -> *mut c_void = transmute(syms.get_egl_create_window_surface() as *const c_void);
let egl_make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
let egl_swap_interval: unsafe extern "C" fn(*mut c_void, i32) -> u32 =
transmute(syms.get_egl_swap_interval() as *const c_void);
let egl_dpy: *mut c_void = egl_get_display(display as *mut c_void);
if egl_dpy.is_null() {
return None;
}
let mut major: i32 = 0;
let mut minor: i32 = 0;
if egl_initialize(egl_dpy, &mut major, &mut minor) == 0 {
return None;
}
let _: u32 = egl_bind_api(LINUX_EGL_OPENGL_ES_API);
let cfg_attribs: [i32; 13] = [
LINUX_EGL_SURFACE_TYPE,
LINUX_EGL_WINDOW_BIT,
LINUX_EGL_RENDERABLE_TYPE,
LINUX_EGL_OPENGL_ES3_BIT,
LINUX_EGL_RED_SIZE,
8,
LINUX_EGL_GREEN_SIZE,
8,
LINUX_EGL_BLUE_SIZE,
8,
LINUX_EGL_ALPHA_SIZE,
8,
LINUX_EGL_NONE,
];
let mut cfg: *mut c_void = null_mut();
let mut num_cfg: i32 = 0;
if egl_choose_config(egl_dpy, cfg_attribs.as_ptr(), &mut cfg, 1, &mut num_cfg) == 0
|| num_cfg < 1
|| cfg.is_null()
{
return None;
}
let ctx_attribs: [i32; 3] = [LINUX_EGL_CONTEXT_CLIENT_VERSION, 3, LINUX_EGL_NONE];
let egl_ctx: *mut c_void =
egl_create_context(egl_dpy, cfg, null_mut(), ctx_attribs.as_ptr());
if egl_ctx.is_null() {
return None;
}
let mut wl_egl_win: usize = 0;
let native_win_handle: usize = if is_wayland {
let create_wl_egl: unsafe extern "C" fn(*mut c_void, i32, i32) -> *mut c_void =
transmute(syms.get_wl_egl_window_create() as *const c_void);
let created: *mut c_void =
create_wl_egl(window as usize as *mut c_void, w as i32, h as i32);
if created.is_null() {
return None;
}
wl_egl_win = created as usize;
wl_egl_win
} else {
window as usize
};
let egl_surf: *mut c_void =
egl_create_window_surface(egl_dpy, cfg, native_win_handle, null());
let mut state: LinuxEglGlesSurface = LinuxEglGlesSurface {
egl_display: egl_dpy as usize,
egl_context: egl_ctx as usize,
egl_surface: egl_surf as usize,
wl_egl_window: wl_egl_win,
program: 0,
vao: 0,
quad_vbo: 0,
instance_vbo: 0,
instance_vbo_cap: 0,
glyph_r8_tex: 0,
glyph_r8_w: 1,
glyph_r8_h: 1,
glyph_r8_rev: 0,
glyph_rgba8_tex: 0,
image_tex: 0,
image_w: 1,
image_h: 1,
image_rev: 0,
external_tex: 0,
viewport_loc: -1,
scroll_loc: -1,
width: w,
height: h,
is_wayland,
max_tex_dim: 4096,
scene: None,
scratch_prims: Vec::new(),
scratch_ranges: Vec::new(),
vsync_pending: Box::new(AtomicBool::new(false)),
};
if egl_surf.is_null() || egl_make_current(egl_dpy, egl_surf, egl_surf, egl_ctx) == 0 {
return None;
}
if syms.get_egl_swap_interval() != 0 {
let _: u32 = egl_swap_interval(egl_dpy, 0);
}
if syms.get_gl_get_integerv() != 0 {
let get_int: unsafe extern "C" fn(u32, *mut i32) =
transmute(syms.get_gl_get_integerv() as *const c_void);
let mut max_tex: i32 = 4096;
get_int(LINUX_GL_MAX_TEXTURE_SIZE, &mut max_tex);
if max_tex > 0 {
state.set_max_tex_dim((max_tex as u32).clamp(256, 8192));
}
}
let vs: u32 =
linux_gles_compile_shader(syms, LINUX_GL_VERTEX_SHADER, GLSL_ES3_UI_VERTEX_SHADER)?;
let fs: u32 = match linux_gles_compile_shader(
syms,
LINUX_GL_FRAGMENT_SHADER,
GLSL_ES3_UI_FRAGMENT_SHADER,
) {
Some(shader) => shader,
None => {
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
delete_shader(vs);
return None;
}
};
let create_program: unsafe extern "C" fn() -> u32 =
transmute(syms.get_gl_create_program() as *const c_void);
let attach_shader: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_attach_shader() as *const c_void);
let link_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_link_program() as *const c_void);
let get_programiv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_programiv() as *const c_void);
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
let program: u32 = create_program();
attach_shader(program, vs);
attach_shader(program, fs);
link_program(program);
delete_shader(vs);
delete_shader(fs);
let mut linked: i32 = 0;
get_programiv(program, LINUX_GL_LINK_STATUS, &mut linked);
if linked == 0 {
let delete_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
delete_program(program);
return None;
}
state.set_program(program);
let use_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let get_uniform: unsafe extern "C" fn(u32, *const c_char) -> i32 =
transmute(syms.get_gl_get_uniform_location() as *const c_void);
let uniform1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
use_program(program);
state.set_viewport_loc(get_uniform(
program,
LINUX_GL_UNIFORM_VIEWPORT.as_ptr().cast::<c_char>(),
));
state.set_scroll_loc(get_uniform(
program,
LINUX_GL_UNIFORM_SCROLL_OFFSET.as_ptr().cast::<c_char>(),
));
let u_gr8: i32 = get_uniform(program, LINUX_GL_UNIFORM_GLYPH_R8.as_ptr().cast::<c_char>());
if u_gr8 >= 0 {
uniform1i(u_gr8, 0);
}
let u_grgba: i32 = get_uniform(
program,
LINUX_GL_UNIFORM_GLYPH_RGBA8.as_ptr().cast::<c_char>(),
);
if u_grgba >= 0 {
uniform1i(u_grgba, 1);
}
let u_img: i32 = get_uniform(
program,
LINUX_GL_UNIFORM_IMAGE_TEX.as_ptr().cast::<c_char>(),
);
if u_img >= 0 {
uniform1i(u_img, 2);
}
let u_ext: i32 = get_uniform(
program,
LINUX_GL_UNIFORM_EXTERNAL_TEX.as_ptr().cast::<c_char>(),
);
if u_ext >= 0 {
uniform1i(u_ext, 3);
}
let gen_vaos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_vertex_arrays() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gen_bufs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_buffers() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let enable_attrib: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let mut vao: u32 = 0;
let mut bufs: [u32; 2] = [0; 2];
gen_vaos(1, &mut vao);
gen_bufs(2, bufs.as_mut_ptr());
state.set_vao(vao);
state.set_quad_vbo(bufs[0]);
state.set_instance_vbo(bufs[1]);
bind_vao(vao);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[0]);
buf_data(
LINUX_GL_ARRAY_BUFFER,
(LINUX_GL_UNIT_QUAD.len() * size_of::<f32>()) as isize,
LINUX_GL_UNIT_QUAD.as_ptr().cast::<c_void>(),
LINUX_GL_STATIC_DRAW,
);
enable_attrib(0);
attrib_ptr(
0,
2,
LINUX_GL_FLOAT,
0,
(2 * size_of::<f32>()) as i32,
null(),
);
let one_r8: [u8; 1] = [255];
let one_rgba: [u8; 4] = [255, 255, 255, 255];
state.set_glyph_r8_tex(linux_gles_create_texture(
syms,
LINUX_GL_R8,
LINUX_GL_RED,
1,
1,
&one_r8,
1,
));
state.set_glyph_rgba8_tex(linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
1,
1,
&one_rgba,
4,
));
state.set_image_tex(linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
1,
1,
&one_rgba,
4,
));
state.set_external_tex(linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
1,
1,
&one_rgba,
4,
));
Some(state)
}
}
unsafe fn linux_gles_bind_instance_attribs(
syms: &LinuxEglGlesSyms,
instance_vbo: u32,
base_bytes: usize,
) {
unsafe {
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let enable_attrib: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let attrib_iptr: unsafe extern "C" fn(u32, i32, u32, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_ipointer() as *const c_void);
let attrib_div: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_vertex_attrib_divisor() as *const c_void);
bind_buf(LINUX_GL_ARRAY_BUFFER, instance_vbo);
let attribs: [Gles3InstanceAttribSpec; 5] = gles3_ui_instance_attribs();
for spec in attribs.iter() {
let loc: u32 = spec.get_location();
let offset: *const c_void =
(base_bytes.saturating_add(spec.get_byte_offset())) as *const c_void;
enable_attrib(loc);
if spec.get_is_integer() {
attrib_iptr(
loc,
spec.get_components(),
LINUX_GL_UNSIGNED_INT,
spec.get_stride(),
offset,
);
} else {
attrib_ptr(
loc,
spec.get_components(),
LINUX_GL_FLOAT,
0,
spec.get_stride(),
offset,
);
}
attrib_div(loc, 1);
}
}
}
pub(crate) fn linux_egl_present_batch(
surface: &mut LinuxEglGlesSurface,
wl_surface: usize,
batch: &DirectGpuFrameBatch,
) -> Result<(), PlatformError> {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
if !syms.get_ready()
|| surface.get_egl_display() == 0
|| surface.get_egl_surface() == 0
|| surface.get_egl_context() == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let vw: u32 = batch.get_viewport_width().max(1);
let vh: u32 = batch.get_viewport_height().max(1);
unsafe {
if surface.get_is_wayland()
&& surface.get_wl_egl_window() != 0
&& (vw != surface.get_width() || vh != surface.get_height())
&& syms.get_wl_egl_window_resize() != 0
{
let resize_wl_egl: unsafe extern "C" fn(*mut c_void, i32, i32, i32, i32) =
transmute(syms.get_wl_egl_window_resize() as *const c_void);
resize_wl_egl(
surface.get_wl_egl_window() as *mut c_void,
vw as i32,
vh as i32,
0,
0,
);
surface.set_width(vw);
surface.set_height(vh);
}
let egl_make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
if egl_make_current(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_context() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
if let Some(glyph_atlas) = batch.glyph_r8_atlas_ref()
&& glyph_atlas.get_width() > 0
&& glyph_atlas.get_height() > 0
&& glyph_atlas.get_revision() != surface.get_glyph_r8_rev()
{
let gw: u32 = glyph_atlas.get_width();
let gh: u32 = glyph_atlas.get_height();
active_tex(LINUX_GL_TEXTURE0);
if gw == surface.get_glyph_r8_w() && gh == surface.get_glyph_r8_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_r8_tex());
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 1);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
gw as i32,
gh as i32,
LINUX_GL_RED,
LINUX_GL_UNSIGNED_BYTE,
glyph_atlas.get_pixels().as_ptr().cast::<c_void>(),
);
} else {
let old_tex: u32 = surface.get_glyph_r8_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_R8,
LINUX_GL_RED,
gw,
gh,
&glyph_atlas.get_pixels()[..],
1,
);
surface.set_glyph_r8_tex(new_tex);
surface.set_glyph_r8_w(gw);
surface.set_glyph_r8_h(gh);
}
surface.set_glyph_r8_rev(glyph_atlas.get_revision());
}
if let Some(img_atlas) = batch.image_rgba8_atlas_ref()
&& img_atlas.get_width() > 0
&& img_atlas.get_height() > 0
&& img_atlas.get_revision() != surface.get_image_rev()
{
let iw: u32 = img_atlas.get_width();
let ih: u32 = img_atlas.get_height();
active_tex(LINUX_GL_TEXTURE2);
if iw == surface.get_image_w() && ih == surface.get_image_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 4);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
iw as i32,
ih as i32,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
img_atlas.get_pixels().as_ptr().cast::<c_void>(),
);
} else {
let old_tex: u32 = surface.get_image_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
iw,
ih,
&img_atlas.get_pixels()[..],
4,
);
surface.set_image_tex(new_tex);
surface.set_image_w(iw);
surface.set_image_h(ih);
}
surface.set_image_rev(img_atlas.get_revision());
}
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let gl_enable: unsafe extern "C" fn(u32) = transmute(syms.get_gl_enable() as *const c_void);
let gl_clear_color: unsafe extern "C" fn(f32, f32, f32, f32) =
transmute(syms.get_gl_clear_color() as *const c_void);
let gl_clear: unsafe extern "C" fn(u32) = transmute(syms.get_gl_clear() as *const c_void);
gl_viewport(0, 0, vw as i32, vh as i32);
gl_disable(LINUX_GL_SCISSOR_TEST);
let clear: [f32; 4] = batch.get_clear_rgba();
gl_clear_color(clear[0], clear[1], clear[2], clear[3]);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT);
let prims: &[GpuPrimitiveInstance] = batch.get_primitives().as_slice();
let total_prims: usize = prims.len();
let byte_len: usize = total_prims.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
if byte_len > 0 {
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let buf_sub: unsafe extern "C" fn(u32, isize, isize, *const c_void) =
transmute(syms.get_gl_buffer_sub_data() as *const c_void);
bind_buf(LINUX_GL_ARRAY_BUFFER, surface.get_instance_vbo());
if byte_len > surface.get_instance_vbo_cap() {
let alloc: usize = byte_len.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
alloc as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
surface.set_instance_vbo_cap(alloc);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
byte_len as isize,
prims.as_ptr().cast::<c_void>(),
);
let blend_sep: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_blend_func_separate() as *const c_void);
let use_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uniform2f: unsafe extern "C" fn(i32, f32, f32) =
transmute(syms.get_gl_uniform2f() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gl_scissor: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_scissor() as *const c_void);
let draw_instanced: unsafe extern "C" fn(u32, i32, i32, i32) =
transmute(syms.get_gl_draw_arrays_instanced() as *const c_void);
gl_enable(LINUX_GL_BLEND);
blend_sep(
LINUX_GL_SRC_ALPHA,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
LINUX_GL_ONE,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
);
gl_enable(LINUX_GL_SCISSOR_TEST);
use_program(surface.get_program());
if surface.get_viewport_loc() >= 0 {
uniform2f(surface.get_viewport_loc(), vw as f32, vh as f32);
}
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_r8_tex());
active_tex(LINUX_GL_TEXTURE1);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_rgba8_tex());
active_tex(LINUX_GL_TEXTURE2);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_external_tex());
bind_vao(surface.get_vao());
for range in batch.get_draw_ranges().iter() {
let base_inst: usize = range.get_base_instance() as usize;
let count: usize = range.get_instance_count() as usize;
if count == 0 || base_inst >= total_prims {
continue;
}
let draw_count: usize = count.min(total_prims - base_inst);
let sc: [i32; 4] = gles3_scissor_from_top_down(
vh,
range.get_scissor_x(),
range.get_scissor_y(),
range.get_scissor_w(),
range.get_scissor_h(),
);
gl_scissor(sc[0], sc[1], sc[2], sc[3]);
if surface.get_scroll_loc() >= 0 {
uniform2f(
surface.get_scroll_loc(),
range.get_scroll_x(),
range.get_scroll_y(),
);
}
let base_bytes: usize = base_inst.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
linux_gles_bind_instance_attribs(syms, surface.get_instance_vbo(), base_bytes);
draw_instanced(LINUX_GL_TRIANGLES, 0, 6, draw_count as i32);
}
gl_disable(LINUX_GL_SCISSOR_TEST);
}
if surface.get_is_wayland() && wl_surface != 0 {
let LinuxEglGlesSurface { vsync_pending, .. } = surface;
let _: bool = linux_wl_arm_frame_callback(wl_surface, vsync_pending.as_ref());
}
let swap_buffers: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_swap_buffers() as *const c_void);
if swap_buffers(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
Ok(())
}
}
unsafe fn linux_gles_create_empty_rgba8_texture(
syms: &LinuxEglGlesSyms,
width: u32,
height: u32,
) -> u32 {
unsafe {
let gen_tex: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_textures() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let tex_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_tex_parameteri() as *const c_void);
let mut tex: u32 = 0;
gen_tex(1, &mut tex);
bind_tex(LINUX_GL_TEXTURE_2D, tex);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_S,
LINUX_GL_CLAMP_TO_EDGE,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_T,
LINUX_GL_CLAMP_TO_EDGE,
);
let w: i32 = width.max(1) as i32;
let h: i32 = height.max(1) as i32;
if syms.get_gl_tex_storage_2d() != 0 {
let tex_storage: unsafe extern "C" fn(u32, i32, u32, i32, i32) =
transmute(syms.get_gl_tex_storage_2d() as *const c_void);
tex_storage(LINUX_GL_TEXTURE_2D, 1, LINUX_GL_RGBA8, w, h);
} else {
let tex_image: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_image_2d() as *const c_void);
tex_image(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA8 as i32,
w,
h,
0,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
null(),
);
}
tex
}
}
unsafe fn linux_gles_link_program(
syms: &LinuxEglGlesSyms,
vs_src: &str,
fs_src: &str,
) -> Option<u32> {
unsafe {
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
let vs: u32 = linux_gles_compile_shader(syms, LINUX_GL_VERTEX_SHADER, vs_src)?;
let Some(fs) = linux_gles_compile_shader(syms, LINUX_GL_FRAGMENT_SHADER, fs_src) else {
delete_shader(vs);
return None;
};
let create_program: unsafe extern "C" fn() -> u32 =
transmute(syms.get_gl_create_program() as *const c_void);
let attach_shader: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_attach_shader() as *const c_void);
let link_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_link_program() as *const c_void);
let get_programiv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_programiv() as *const c_void);
let program: u32 = create_program();
attach_shader(program, vs);
attach_shader(program, fs);
link_program(program);
delete_shader(vs);
delete_shader(fs);
let mut linked: i32 = 0;
get_programiv(program, LINUX_GL_LINK_STATUS, &mut linked);
if linked == 0 {
let delete_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
delete_program(program);
return None;
}
Some(program)
}
}
unsafe fn linux_gles_init_scene_pipeline(syms: &LinuxEglGlesSyms) -> Option<LinuxEglScenePipeline> {
if syms.get_gl_draw_arrays() == 0
|| syms.get_gl_uniform4f() == 0
|| syms.get_gl_uniform_matrix4fv() == 0
|| syms.get_gl_gen_framebuffers() == 0
|| syms.get_gl_bind_framebuffer() == 0
|| syms.get_gl_framebuffer_texture_2d() == 0
|| syms.get_gl_gen_renderbuffers() == 0
|| syms.get_gl_bind_renderbuffer() == 0
|| syms.get_gl_renderbuffer_storage() == 0
|| syms.get_gl_framebuffer_renderbuffer() == 0
|| syms.get_gl_depth_mask() == 0
|| syms.get_gl_depth_func() == 0
|| syms.get_gl_clear_depthf() == 0
{
return None;
}
unsafe {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
let ocean_program: u32 = linux_gles_link_program(
syms,
LINUX_GL_SCENE_FULLSCREEN_VERT,
LINUX_GL_SCENE_OCEAN_FRAG,
)?;
let Some(boat_program) =
linux_gles_link_program(syms, LINUX_GL_SCENE_BOAT_VERT, LINUX_GL_SCENE_BOAT_FRAG)
else {
del_prog(ocean_program);
return None;
};
let Some(sky3d_program) = linux_gles_link_program(
syms,
LINUX_GL_SCENE_FULLSCREEN_VERT,
LINUX_GL_SCENE_3D_SKY_FRAG,
) else {
del_prog(ocean_program);
del_prog(boat_program);
return None;
};
let Some(mesh3d_program) = linux_gles_link_program(
syms,
LINUX_GL_SCENE_3D_MESH_VERT,
LINUX_GL_SCENE_3D_MESH_FRAG,
) else {
del_prog(ocean_program);
del_prog(boat_program);
del_prog(sky3d_program);
return None;
};
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let get_uni: unsafe extern "C" fn(u32, *const c_char) -> i32 =
transmute(syms.get_gl_get_uniform_location() as *const c_void);
let uni1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
use_prog(ocean_program);
let wake_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_WAKE_TEX.as_ptr().cast::<c_char>(),
);
if wake_loc >= 0 {
uni1i(wake_loc, 0);
}
let ocean_time_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_OCEAN_TIME.as_ptr().cast::<c_char>(),
);
let ocean_roll_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_OCEAN_ROLL.as_ptr().cast::<c_char>(),
);
let ocean_sun_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_OCEAN_SUN.as_ptr().cast::<c_char>(),
);
let sky3d_cam_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_CAM_POS.as_ptr().cast::<c_char>(),
);
let sky3d_sun_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SUN_COL.as_ptr().cast::<c_char>(),
);
let sky3d_hor_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SKY_HOR.as_ptr().cast::<c_char>(),
);
let sky3d_zen_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SKY_ZEN.as_ptr().cast::<c_char>(),
);
let mesh3d_vp_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_VIEW_PROJ.as_ptr().cast::<c_char>(),
);
let mesh3d_cam_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_CAM_POS.as_ptr().cast::<c_char>(),
);
let mesh3d_sun_fog_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SUN_FOG.as_ptr().cast::<c_char>(),
);
let mesh3d_sun_col_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SUN_COL.as_ptr().cast::<c_char>(),
);
let mesh3d_hor_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SKY_HOR.as_ptr().cast::<c_char>(),
);
let mesh3d_zen_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SKY_ZEN.as_ptr().cast::<c_char>(),
);
let gen_fbos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_framebuffers() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let fbo_tex2d: unsafe extern "C" fn(u32, u32, u32, u32, i32) =
transmute(syms.get_gl_framebuffer_texture_2d() as *const c_void);
let gen_rbos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_renderbuffers() as *const c_void);
let bind_rbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_renderbuffer() as *const c_void);
let rbo_storage: unsafe extern "C" fn(u32, u32, i32, i32) =
transmute(syms.get_gl_renderbuffer_storage() as *const c_void);
let fbo_rbo: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_framebuffer_renderbuffer() as *const c_void);
let mut scene_fbo: u32 = 0;
let mut scene_depth_rbo: u32 = 0;
gen_fbos(1, &mut scene_fbo);
gen_rbos(1, &mut scene_depth_rbo);
let scene_color_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, 1, 1);
let one_rgba: [u8; 4] = [128, 128, 0, 255];
let wake_tex: u32 =
linux_gles_create_texture(syms, LINUX_GL_RGBA8, LINUX_GL_RGBA, 1, 1, &one_rgba, 4);
bind_rbo(LINUX_GL_RENDERBUFFER, scene_depth_rbo);
rbo_storage(LINUX_GL_RENDERBUFFER, LINUX_GL_DEPTH_COMPONENT24, 1, 1);
bind_fbo(LINUX_GL_FRAMEBUFFER, scene_fbo);
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
scene_color_tex,
0,
);
fbo_rbo(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_DEPTH_ATTACHMENT,
LINUX_GL_RENDERBUFFER,
scene_depth_rbo,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
let gen_vaos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_vertex_arrays() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gen_bufs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_buffers() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let enable_attrib: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let attrib_div: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_vertex_attrib_divisor() as *const c_void);
let mut vaos: [u32; 2] = [0; 2];
let mut bufs: [u32; 3] = [0; 3];
gen_vaos(2, vaos.as_mut_ptr());
gen_bufs(3, bufs.as_mut_ptr());
bind_vao(vaos[0]);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[0]);
enable_attrib(0);
attrib_ptr(0, 4, LINUX_GL_FLOAT, 0, 32, null());
enable_attrib(1);
attrib_ptr(1, 4, LINUX_GL_FLOAT, 0, 32, 16 as *const c_void);
bind_vao(vaos[1]);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[1]);
for slot in 0u32..3u32 {
enable_attrib(slot);
attrib_ptr(
slot,
4,
LINUX_GL_FLOAT,
0,
48,
((slot as usize) * 16) as *const c_void,
);
attrib_div(slot, 0);
}
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[2]);
for slot in 0u32..6u32 {
enable_attrib(3 + slot);
attrib_ptr(
3 + slot,
4,
LINUX_GL_FLOAT,
0,
96,
((slot as usize) * 16) as *const c_void,
);
attrib_div(3 + slot, 1);
}
bind_vao(0);
Some(LinuxEglScenePipeline {
scene_fbo,
scene_color_tex,
scene_depth_rbo,
scene_w: 0,
scene_h: 0,
wake_tex,
wake_w: 0,
wake_h: 0,
ocean_program,
ocean_time_loc,
ocean_roll_loc,
ocean_sun_loc,
boat_program,
boat_vao: vaos[0],
boat_vbo: bufs[0],
boat_vbo_cap: 0,
sky3d_program,
sky3d_cam_loc,
sky3d_sun_loc,
sky3d_hor_loc,
sky3d_zen_loc,
mesh3d_program,
mesh3d_vao: vaos[1],
mesh3d_vert_vbo: bufs[1],
mesh3d_vert_cap: 0,
mesh3d_vert_rev: 0,
mesh3d_inst_vbo: bufs[2],
mesh3d_inst_cap: 0,
mesh3d_vp_loc,
mesh3d_cam_loc,
mesh3d_sun_fog_loc,
mesh3d_sun_col_loc,
mesh3d_hor_loc,
mesh3d_zen_loc,
})
}
}
unsafe fn linux_gles_execute_scene_pass(
syms: &LinuxEglGlesSyms,
surface: &mut LinuxEglGlesSurface,
pass: &DirectGpuScenePass,
) -> u32 {
let fallback_ext: u32 = surface.get_external_tex();
let max_dim: u32 = surface.get_max_tex_dim().clamp(256, 4096);
let ui_vao: u32 = surface.get_vao();
let LinuxEglGlesSurface { scene, .. } = surface;
unsafe {
if scene.is_none() {
*scene = linux_gles_init_scene_pipeline(syms);
}
let Some(sc) = scene.as_mut() else {
return fallback_ext;
};
let (req_w, req_h): (u32, u32) = match pass {
DirectGpuScenePass::OceanFluid(ocean) => (
ocean.get_target_width().clamp(64, max_dim),
ocean.get_target_height().clamp(64, max_dim),
),
DirectGpuScenePass::World3d(world) => (
world.get_target_width().clamp(64, max_dim),
world.get_target_height().clamp(64, max_dim),
),
};
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let fbo_tex2d: unsafe extern "C" fn(u32, u32, u32, u32, i32) =
transmute(syms.get_gl_framebuffer_texture_2d() as *const c_void);
let bind_rbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_renderbuffer() as *const c_void);
let rbo_storage: unsafe extern "C" fn(u32, u32, i32, i32) =
transmute(syms.get_gl_renderbuffer_storage() as *const c_void);
let fbo_rbo: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_framebuffer_renderbuffer() as *const c_void);
if sc.get_scene_w() != req_w || sc.get_scene_h() != req_h {
active_tex(LINUX_GL_TEXTURE3);
let old_tex: u32 = sc.get_scene_color_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, req_w, req_h);
sc.set_scene_color_tex(new_tex);
bind_rbo(LINUX_GL_RENDERBUFFER, sc.get_scene_depth_rbo());
rbo_storage(
LINUX_GL_RENDERBUFFER,
LINUX_GL_DEPTH_COMPONENT24,
req_w as i32,
req_h as i32,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, sc.get_scene_fbo());
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
new_tex,
0,
);
fbo_rbo(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_DEPTH_ATTACHMENT,
LINUX_GL_RENDERBUFFER,
sc.get_scene_depth_rbo(),
);
sc.set_scene_w(req_w);
sc.set_scene_h(req_h);
}
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let gl_enable: unsafe extern "C" fn(u32) = transmute(syms.get_gl_enable() as *const c_void);
let depth_mask: unsafe extern "C" fn(u8) =
transmute(syms.get_gl_depth_mask() as *const c_void);
let depth_func: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_depth_func() as *const c_void);
let clear_depthf: unsafe extern "C" fn(f32) =
transmute(syms.get_gl_clear_depthf() as *const c_void);
let clear_color: unsafe extern "C" fn(f32, f32, f32, f32) =
transmute(syms.get_gl_clear_color() as *const c_void);
let gl_clear: unsafe extern "C" fn(u32) = transmute(syms.get_gl_clear() as *const c_void);
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uni4f: unsafe extern "C" fn(i32, f32, f32, f32, f32) =
transmute(syms.get_gl_uniform4f() as *const c_void);
let uni_mat4: unsafe extern "C" fn(i32, i32, u8, *const f32) =
transmute(syms.get_gl_uniform_matrix4fv() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let buf_sub: unsafe extern "C" fn(u32, isize, isize, *const c_void) =
transmute(syms.get_gl_buffer_sub_data() as *const c_void);
let draw_arrays: unsafe extern "C" fn(u32, i32, i32) =
transmute(syms.get_gl_draw_arrays() as *const c_void);
let draw_instanced: unsafe extern "C" fn(u32, i32, i32, i32) =
transmute(syms.get_gl_draw_arrays_instanced() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, sc.get_scene_fbo());
gl_disable(LINUX_GL_SCISSOR_TEST);
gl_viewport(0, 0, req_w as i32, req_h as i32);
match pass {
DirectGpuScenePass::OceanFluid(ocean) => {
let wake_w: u32 = ocean.get_wake_width().max(1);
let wake_h: u32 = ocean.get_wake_height().max(1);
let wake_bytes: &[u8] = ocean.get_wake_rgba8().as_slice();
let req_wake: usize = (wake_w as usize)
.saturating_mul(wake_h as usize)
.saturating_mul(4);
if wake_bytes.len() >= req_wake
&& (sc.get_wake_w() != wake_w
|| sc.get_wake_h() != wake_h
|| ocean.get_has_active_wake())
{
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
active_tex(LINUX_GL_TEXTURE0);
if sc.get_wake_w() != wake_w || sc.get_wake_h() != wake_h {
let old_wake: u32 = sc.get_wake_tex();
if old_wake != 0 {
del_tex(1, &old_wake);
}
let new_wake: u32 = linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
wake_w,
wake_h,
wake_bytes,
4,
);
sc.set_wake_tex(new_wake);
sc.set_wake_w(wake_w);
sc.set_wake_h(wake_h);
} else {
bind_tex(LINUX_GL_TEXTURE_2D, sc.get_wake_tex());
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 4);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
wake_w as i32,
wake_h as i32,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
wake_bytes.as_ptr().cast::<c_void>(),
);
}
}
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
gl_disable(LINUX_GL_BLEND);
use_prog(sc.get_ocean_program());
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, sc.get_wake_tex());
let boat_uv: [f32; 2] = ocean.get_boat_uv();
let sun: [f32; 3] = ocean.get_sun_dir();
if sc.get_ocean_time_loc() >= 0 {
uni4f(
sc.get_ocean_time_loc(),
ocean.get_sim_time(),
ocean.get_agitation(),
ocean.get_horizon_v(),
ocean.get_aspect(),
);
}
if sc.get_ocean_roll_loc() >= 0 {
uni4f(
sc.get_ocean_roll_loc(),
ocean.get_roll_cos(),
ocean.get_roll_sin(),
boat_uv[0],
boat_uv[1],
);
}
if sc.get_ocean_sun_loc() >= 0 {
uni4f(
sc.get_ocean_sun_loc(),
sun[0],
sun[1],
sun[2],
if ocean.get_has_active_wake() {
1.0
} else {
0.0
},
);
}
bind_vao(ui_vao);
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
let boat_verts: &[GpuOceanBoatVertex] = ocean.get_boat_vertices().as_slice();
if !boat_verts.is_empty() {
let byte_len: usize = boat_verts
.len()
.saturating_mul(size_of::<GpuOceanBoatVertex>());
let blend_sep: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_blend_func_separate() as *const c_void);
gl_enable(LINUX_GL_BLEND);
blend_sep(
LINUX_GL_SRC_ALPHA,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
LINUX_GL_ONE,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
);
use_prog(sc.get_boat_program());
bind_vao(sc.get_boat_vao());
bind_buf(LINUX_GL_ARRAY_BUFFER, sc.get_boat_vbo());
if byte_len > sc.get_boat_vbo_cap() {
let cap: usize = byte_len.max(1024).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
sc.set_boat_vbo_cap(cap);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
byte_len as isize,
boat_verts.as_ptr().cast::<c_void>(),
);
draw_arrays(LINUX_GL_TRIANGLES, 0, boat_verts.len() as i32);
bind_vao(0);
gl_disable(LINUX_GL_BLEND);
}
}
DirectGpuScenePass::World3d(world) => {
let cam: [f32; 4] = world.get_camera_pos();
let sun_fog: [f32; 4] = world.get_sun_dir_fog();
let sun_col: [f32; 4] = world.get_sun_color_ambient();
let sky_hor: [f32; 4] = world.get_sky_horizon_tod();
let sky_zen: [f32; 4] = world.get_sky_zenith_aspect();
gl_disable(LINUX_GL_BLEND);
depth_mask(1);
clear_depthf(1.0);
clear_color(0.04, 0.05, 0.09, 1.0);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT | LINUX_GL_DEPTH_BUFFER_BIT);
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
use_prog(sc.get_sky3d_program());
if sc.get_sky3d_cam_loc() >= 0 {
uni4f(sc.get_sky3d_cam_loc(), cam[0], cam[1], cam[2], cam[3]);
}
if sc.get_sky3d_sun_loc() >= 0 {
uni4f(
sc.get_sky3d_sun_loc(),
sun_col[0],
sun_col[1],
sun_col[2],
sun_col[3],
);
}
if sc.get_sky3d_hor_loc() >= 0 {
uni4f(
sc.get_sky3d_hor_loc(),
sky_hor[0],
sky_hor[1],
sky_hor[2],
sky_hor[3],
);
}
if sc.get_sky3d_zen_loc() >= 0 {
uni4f(
sc.get_sky3d_zen_loc(),
sky_zen[0],
sky_zen[1],
sky_zen[2],
sky_zen[3],
);
}
bind_vao(ui_vao);
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
let verts: &[GpuWorld3dVertex] = world.get_mesh_buffer().get_vertices().as_slice();
let insts: &[GpuWorld3dInstance] = world.get_instances().as_slice();
if !verts.is_empty() && !insts.is_empty() {
let mesh_rev: u64 = world.get_mesh_buffer().get_revision();
bind_vao(sc.get_mesh3d_vao());
if sc.get_mesh3d_vert_rev() != mesh_rev || sc.get_mesh3d_vert_cap() == 0 {
let vbytes: usize =
verts.len().saturating_mul(size_of::<GpuWorld3dVertex>());
bind_buf(LINUX_GL_ARRAY_BUFFER, sc.get_mesh3d_vert_vbo());
if vbytes > sc.get_mesh3d_vert_cap() {
let cap: usize = vbytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STATIC_DRAW,
);
sc.set_mesh3d_vert_cap(cap);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
vbytes as isize,
verts.as_ptr().cast::<c_void>(),
);
sc.set_mesh3d_vert_rev(mesh_rev);
}
let ibytes: usize = insts.len().saturating_mul(size_of::<GpuWorld3dInstance>());
bind_buf(LINUX_GL_ARRAY_BUFFER, sc.get_mesh3d_inst_vbo());
if ibytes > sc.get_mesh3d_inst_cap() {
let cap: usize = ibytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
sc.set_mesh3d_inst_cap(cap);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
ibytes as isize,
insts.as_ptr().cast::<c_void>(),
);
gl_enable(LINUX_GL_DEPTH_TEST);
depth_func(LINUX_GL_LESS);
depth_mask(1);
use_prog(sc.get_mesh3d_program());
let vp: [f32; 16] = world.get_view_proj();
if sc.get_mesh3d_vp_loc() >= 0 {
uni_mat4(sc.get_mesh3d_vp_loc(), 1, 0, vp.as_ptr());
}
if sc.get_mesh3d_cam_loc() >= 0 {
uni4f(sc.get_mesh3d_cam_loc(), cam[0], cam[1], cam[2], cam[3]);
}
if sc.get_mesh3d_sun_fog_loc() >= 0 {
uni4f(
sc.get_mesh3d_sun_fog_loc(),
sun_fog[0],
sun_fog[1],
sun_fog[2],
sun_fog[3],
);
}
if sc.get_mesh3d_sun_col_loc() >= 0 {
uni4f(
sc.get_mesh3d_sun_col_loc(),
sun_col[0],
sun_col[1],
sun_col[2],
sun_col[3],
);
}
if sc.get_mesh3d_hor_loc() >= 0 {
uni4f(
sc.get_mesh3d_hor_loc(),
sky_hor[0],
sky_hor[1],
sky_hor[2],
sky_hor[3],
);
}
if sc.get_mesh3d_zen_loc() >= 0 {
uni4f(
sc.get_mesh3d_zen_loc(),
sky_zen[0],
sky_zen[1],
sky_zen[2],
sky_zen[3],
);
}
for batch in world.get_draw_batches().iter() {
if batch.get_vertex_count() == 0 || batch.get_instance_count() == 0 {
continue;
}
let base_byte_off: usize =
(batch.get_instance_start() as usize).saturating_mul(96);
for slot in 0u32..6u32 {
attrib_ptr(
3 + slot,
4,
LINUX_GL_FLOAT,
0,
96,
(base_byte_off + (slot as usize) * 16) as *const c_void,
);
}
draw_instanced(
LINUX_GL_TRIANGLES,
batch.get_vertex_start() as i32,
batch.get_vertex_count() as i32,
batch.get_instance_count() as i32,
);
}
bind_vao(0);
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
}
}
}
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
sc.get_scene_color_tex()
}
}
unsafe fn linux_gles_destroy_scene_pipeline(
syms: &LinuxEglGlesSyms,
scene: &LinuxEglScenePipeline,
) {
unsafe {
if syms.get_gl_delete_textures() != 0 {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let texs: [u32; 2] = [scene.get_scene_color_tex(), scene.get_wake_tex()];
del_tex(2, texs.as_ptr());
}
if syms.get_gl_delete_renderbuffers() != 0 && scene.get_scene_depth_rbo() != 0 {
let del_rbo: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_renderbuffers() as *const c_void);
let rbo: u32 = scene.get_scene_depth_rbo();
del_rbo(1, &rbo);
}
if syms.get_gl_delete_framebuffers() != 0 && scene.get_scene_fbo() != 0 {
let del_fbo: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_framebuffers() as *const c_void);
let fbo: u32 = scene.get_scene_fbo();
del_fbo(1, &fbo);
}
if syms.get_gl_delete_buffers() != 0 {
let del_buf: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_buffers() as *const c_void);
let bufs: [u32; 3] = [
scene.get_boat_vbo(),
scene.get_mesh3d_vert_vbo(),
scene.get_mesh3d_inst_vbo(),
];
del_buf(3, bufs.as_ptr());
}
if syms.get_gl_delete_vertex_arrays() != 0 {
let del_vao: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_vertex_arrays() as *const c_void);
let vaos: [u32; 2] = [scene.get_boat_vao(), scene.get_mesh3d_vao()];
del_vao(2, vaos.as_ptr());
}
if syms.get_gl_delete_program() != 0 {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
for prog in [
scene.get_ocean_program(),
scene.get_boat_program(),
scene.get_sky3d_program(),
scene.get_mesh3d_program(),
] {
if prog != 0 {
del_prog(prog);
}
}
}
}
}
unsafe fn linux_gles_upload_atlas_dirty_rects(
syms: &LinuxEglGlesSyms,
pixel_fmt: u32,
alignment: i32,
atlas_w: u32,
atlas_h: u32,
pixels: &[u8],
dirty_rects: &[PixelDirtyRect],
) {
unsafe {
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, alignment);
if dirty_rects.is_empty() {
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
atlas_w as i32,
atlas_h as i32,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
return;
}
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, atlas_w as i32);
for rect in dirty_rects.iter() {
let rx: u32 = rect.get_x().min(atlas_w);
let ry: u32 = rect.get_y().min(atlas_h);
let rw: u32 = rect.get_width().min(atlas_w.saturating_sub(rx));
let rh: u32 = rect.get_height().min(atlas_h.saturating_sub(ry));
if rw == 0 || rh == 0 {
continue;
}
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, rx as i32);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, ry as i32);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
rx as i32,
ry as i32,
rw as i32,
rh as i32,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
}
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, 0);
}
}
pub(crate) fn linux_egl_present_submission(
surface: &mut LinuxEglGlesSurface,
wl_surface: usize,
submission: &GpuFrameSubmission,
) -> Result<(usize, usize), PlatformError> {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
if !syms.get_ready()
|| surface.get_egl_display() == 0
|| surface.get_egl_surface() == 0
|| surface.get_egl_context() == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let vw: u32 = submission.get_width().max(1);
let vh: u32 = submission.get_height().max(1);
unsafe {
if surface.get_is_wayland()
&& surface.get_wl_egl_window() != 0
&& (vw != surface.get_width() || vh != surface.get_height())
&& syms.get_wl_egl_window_resize() != 0
{
let resize_wl_egl: unsafe extern "C" fn(*mut c_void, i32, i32, i32, i32) =
transmute(syms.get_wl_egl_window_resize() as *const c_void);
resize_wl_egl(
surface.get_wl_egl_window() as *mut c_void,
vw as i32,
vh as i32,
0,
0,
);
surface.set_width(vw);
surface.set_height(vh);
}
let egl_make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
if egl_make_current(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_context() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
if let Some(glyph_atlas) = submission.glyph_atlas_r8_ref()
&& glyph_atlas.get_width() > 0
&& glyph_atlas.get_height() > 0
{
let gw: u32 = glyph_atlas.get_width();
let gh: u32 = glyph_atlas.get_height();
let pixels: &[u8] = &glyph_atlas.get_pixels()[..];
if pixels.len() >= (gw as usize).saturating_mul(gh as usize) {
active_tex(LINUX_GL_TEXTURE0);
if gw == surface.get_glyph_r8_w() && gh == surface.get_glyph_r8_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_r8_tex());
linux_gles_upload_atlas_dirty_rects(
syms,
LINUX_GL_RED,
1,
gw,
gh,
pixels,
submission.get_glyph_dirty_rects().as_slice(),
);
} else {
let old_tex: u32 = surface.get_glyph_r8_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_R8,
LINUX_GL_RED,
gw,
gh,
pixels,
1,
);
surface.set_glyph_r8_tex(new_tex);
surface.set_glyph_r8_w(gw);
surface.set_glyph_r8_h(gh);
}
surface.set_glyph_r8_rev(glyph_atlas.get_revision());
}
}
if let Some(img_atlas) = submission.image_atlas_rgba8_ref()
&& img_atlas.get_width() > 0
&& img_atlas.get_height() > 0
{
let iw: u32 = img_atlas.get_width();
let ih: u32 = img_atlas.get_height();
let pixels: &[u8] = &img_atlas.get_pixels()[..];
if pixels.len() >= (iw as usize).saturating_mul(ih as usize).saturating_mul(4) {
active_tex(LINUX_GL_TEXTURE2);
if iw == surface.get_image_w() && ih == surface.get_image_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
linux_gles_upload_atlas_dirty_rects(
syms,
LINUX_GL_RGBA,
4,
iw,
ih,
pixels,
submission.get_image_dirty_rects().as_slice(),
);
} else {
let old_tex: u32 = surface.get_image_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
iw,
ih,
pixels,
4,
);
surface.set_image_tex(new_tex);
surface.set_image_w(iw);
surface.set_image_h(ih);
}
surface.set_image_rev(img_atlas.get_revision());
}
}
let active_external_tex: u32 = if let Some(pass) = submission.gpu_scene_pass_ref() {
linux_gles_execute_scene_pass(syms, surface, pass)
} else {
surface.get_external_tex()
};
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let gl_enable: unsafe extern "C" fn(u32) = transmute(syms.get_gl_enable() as *const c_void);
let gl_clear_color: unsafe extern "C" fn(f32, f32, f32, f32) =
transmute(syms.get_gl_clear_color() as *const c_void);
let gl_clear: unsafe extern "C" fn(u32) = transmute(syms.get_gl_clear() as *const c_void);
if syms.get_gl_bind_framebuffer() != 0 {
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
}
gl_viewport(0, 0, vw as i32, vh as i32);
gl_disable(LINUX_GL_SCISSOR_TEST);
gl_disable(LINUX_GL_DEPTH_TEST);
let clear: [f32; 4] = submission.get_clear_color().to_f32_array();
gl_clear_color(clear[0], clear[1], clear[2], clear[3]);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT);
let (total_prims, total_ranges): (usize, usize) = {
let LinuxEglGlesSurface {
scratch_prims,
scratch_ranges,
..
} = surface;
scratch_prims.clear();
scratch_ranges.clear();
for batch in submission.get_batches().iter() {
let prims: &[GpuPrimitiveInstance] = batch.get_primitives().as_slice();
if prims.is_empty() {
continue;
}
let base_inst: usize = scratch_prims.len();
scratch_prims.extend_from_slice(prims);
scratch_ranges.push((
base_inst,
prims.len(),
batch.get_scissor(),
batch.get_scroll_offset(),
));
}
(scratch_prims.len(), scratch_ranges.len())
};
let byte_len: usize = total_prims.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
if byte_len > 0 {
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let buf_sub: unsafe extern "C" fn(u32, isize, isize, *const c_void) =
transmute(syms.get_gl_buffer_sub_data() as *const c_void);
bind_buf(LINUX_GL_ARRAY_BUFFER, surface.get_instance_vbo());
if byte_len > surface.get_instance_vbo_cap() {
let alloc: usize = byte_len.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
alloc as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
surface.set_instance_vbo_cap(alloc);
}
let prims_ptr: *const c_void = {
let LinuxEglGlesSurface { scratch_prims, .. } = surface;
scratch_prims.as_ptr().cast::<c_void>()
};
buf_sub(LINUX_GL_ARRAY_BUFFER, 0, byte_len as isize, prims_ptr);
let blend_sep: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_blend_func_separate() as *const c_void);
let use_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uniform2f: unsafe extern "C" fn(i32, f32, f32) =
transmute(syms.get_gl_uniform2f() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gl_scissor: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_scissor() as *const c_void);
let draw_instanced: unsafe extern "C" fn(u32, i32, i32, i32) =
transmute(syms.get_gl_draw_arrays_instanced() as *const c_void);
gl_enable(LINUX_GL_BLEND);
blend_sep(
LINUX_GL_SRC_ALPHA,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
LINUX_GL_ONE,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
);
gl_enable(LINUX_GL_SCISSOR_TEST);
use_program(surface.get_program());
if surface.get_viewport_loc() >= 0 {
uniform2f(surface.get_viewport_loc(), vw as f32, vh as f32);
}
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_r8_tex());
active_tex(LINUX_GL_TEXTURE1);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_rgba8_tex());
active_tex(LINUX_GL_TEXTURE2);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, active_external_tex);
bind_vao(surface.get_vao());
let scroll_loc: i32 = surface.get_scroll_loc();
let inst_vbo: u32 = surface.get_instance_vbo();
let LinuxEglGlesSurface { scratch_ranges, .. } = surface;
for &(base_inst, count, scissor, scroll) in scratch_ranges.iter() {
if count == 0 || base_inst >= total_prims {
continue;
}
let draw_count: usize = count.min(total_prims - base_inst);
let sc: [i32; 4] = gles3_scissor_from_top_down(
vh,
scissor.get_x().max(0.0).floor() as u32,
scissor.get_y().max(0.0).floor() as u32,
scissor.get_width().max(1.0).ceil() as u32,
scissor.get_height().max(1.0).ceil() as u32,
);
gl_scissor(sc[0], sc[1], sc[2], sc[3]);
if scroll_loc >= 0 {
uniform2f(scroll_loc, scroll[0], scroll[1]);
}
let base_bytes: usize = base_inst.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
linux_gles_bind_instance_attribs(syms, inst_vbo, base_bytes);
draw_instanced(LINUX_GL_TRIANGLES, 0, 6, draw_count as i32);
}
gl_disable(LINUX_GL_SCISSOR_TEST);
}
if surface.get_is_wayland() && wl_surface != 0 {
let LinuxEglGlesSurface { vsync_pending, .. } = surface;
let _: bool = linux_wl_arm_frame_callback(wl_surface, vsync_pending.as_ref());
}
let swap_buffers: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_swap_buffers() as *const c_void);
if swap_buffers(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
Ok((total_ranges.max(1), total_prims))
}
}
pub(crate) fn linux_egl_destroy_surface(surface: &mut LinuxEglGlesSurface) {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
let dpy: *mut c_void = surface.get_egl_display() as *mut c_void;
let ctx: *mut c_void = surface.get_egl_context() as *mut c_void;
let surf: *mut c_void = surface.get_egl_surface() as *mut c_void;
unsafe {
if !dpy.is_null() && !ctx.is_null() && !surf.is_null() && syms.get_egl_make_current() != 0 {
let make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
if make_current(dpy, surf, surf, ctx) != 0 {
let taken_scene: Option<LinuxEglScenePipeline> = {
let LinuxEglGlesSurface { scene, .. } = surface;
scene.take()
};
if let Some(scene_pipe) = taken_scene.as_ref() {
linux_gles_destroy_scene_pipeline(syms, scene_pipe);
}
if syms.get_gl_delete_textures() != 0 {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let texs: [u32; 4] = [
surface.get_glyph_r8_tex(),
surface.get_glyph_rgba8_tex(),
surface.get_image_tex(),
surface.get_external_tex(),
];
del_tex(4, texs.as_ptr());
}
if syms.get_gl_delete_buffers() != 0 {
let del_buf: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_buffers() as *const c_void);
let bufs: [u32; 2] = [surface.get_quad_vbo(), surface.get_instance_vbo()];
del_buf(2, bufs.as_ptr());
}
if syms.get_gl_delete_vertex_arrays() != 0 && surface.get_vao() != 0 {
let del_vao: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_vertex_arrays() as *const c_void);
let vao: u32 = surface.get_vao();
del_vao(1, &vao);
}
if syms.get_gl_delete_program() != 0 && surface.get_program() != 0 {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
del_prog(surface.get_program());
}
}
let _: u32 = make_current(dpy, null_mut(), null_mut(), null_mut());
}
if !dpy.is_null() && !surf.is_null() && syms.get_egl_destroy_surface() != 0 {
let del_surf: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_destroy_surface() as *const c_void);
let _: u32 = del_surf(dpy, surf);
}
if !dpy.is_null() && !ctx.is_null() && syms.get_egl_destroy_context() != 0 {
let del_ctx: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_destroy_context() as *const c_void);
let _: u32 = del_ctx(dpy, ctx);
}
if surface.get_wl_egl_window() != 0 && syms.get_wl_egl_window_destroy() != 0 {
let del_wl_egl: unsafe extern "C" fn(*mut c_void) =
transmute(syms.get_wl_egl_window_destroy() as *const c_void);
del_wl_egl(surface.get_wl_egl_window() as *mut c_void);
}
if !dpy.is_null() && syms.get_egl_terminate() != 0 {
let term: unsafe extern "C" fn(*mut c_void) -> u32 =
transmute(syms.get_egl_terminate() as *const c_void);
let _: u32 = term(dpy);
}
}
}
pub(crate) fn linux_alloc_shm_file(size: usize) -> Result<(i32, usize, usize), PlatformError> {
if size == 0 || size > (i32::MAX as usize) {
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
unsafe {
let mut fd: i32 = -1;
let memfd_sym: *mut c_void = dlsym(null_mut(), SYM_MEMFD_CREATE.as_ptr().cast::<c_char>());
if !memfd_sym.is_null() {
let memfd_create: unsafe extern "C" fn(*const c_char, u32) -> i32 =
transmute(memfd_sym as *const c_void);
fd = memfd_create(WL_MEMFD_NAME.as_ptr().cast::<c_char>(), LINUX_MFD_CLOEXEC);
}
if fd < 0 {
let pid: i32 = getpid();
let seq: u64 = LINUX_SHM_SEQ.fetch_add(1, Ordering::Relaxed);
let path_str: String = format!("{LINUX_DEV_SHM_PREFIX}-{pid}-{seq}");
if let Ok(c_path) = CString::new(path_str) {
fd = open(c_path.as_ptr(), LINUX_SHM_OPEN_FLAGS, LINUX_SHM_FILE_MODE);
if fd >= 0 {
let _: i32 = unlink(c_path.as_ptr());
}
}
}
if fd < 0 {
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
if ftruncate(fd, size as i64) != 0 {
let _: i32 = close(fd);
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
let ptr: *mut c_void = mmap(
null_mut(),
size,
LINUX_MMAP_PROT_RW,
LINUX_MMAP_SHARED,
fd,
0,
);
if ptr.is_null() || (ptr as isize) == -1 {
let _: i32 = close(fd);
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
Ok((fd, ptr as usize, size))
}
}
pub(crate) fn linux_free_shm_file(fd: i32, ptr: usize, len: usize) {
unsafe {
if ptr != 0 && len > 0 {
let _: i32 = munmap(ptr as *mut c_void, len);
}
if fd >= 0 {
let _: i32 = close(fd);
}
}
}
pub(crate) unsafe extern "C" fn linux_wl_registry_global(
data: *mut c_void,
registry: *mut c_void,
name: u32,
interface: *const c_char,
version: u32,
) {
if data.is_null() || registry.is_null() || interface.is_null() {
return;
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.proxy_marshal_flags == 0 {
return;
}
unsafe {
let Ok(iface_str) = CStr::from_ptr(interface).to_str() else {
return;
};
let state: &mut WlRegistryBindState = &mut *data.cast::<WlRegistryBindState>();
let marshal_bind: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
u32,
*const c_char,
u32,
*mut c_void,
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
if iface_str == WL_IFACE_COMPOSITOR && state.compositor == 0 && wl.compositor_interface != 0
{
let bind_ver: u32 = version.clamp(1, WL_COMPOSITOR_MAX_VERSION);
let proxy: *mut c_void = marshal_bind(
registry,
WL_OPCODE_REGISTRY_BIND,
wl.compositor_interface as *const c_void,
bind_ver,
0,
name,
interface,
bind_ver,
null_mut(),
);
state.compositor = proxy as usize;
state.compositor_version = bind_ver;
} else if iface_str == WL_IFACE_SHM && state.shm == 0 && wl.shm_interface != 0 {
let bind_ver: u32 = version.clamp(1, WL_SHM_MAX_VERSION);
let proxy: *mut c_void = marshal_bind(
registry,
WL_OPCODE_REGISTRY_BIND,
wl.shm_interface as *const c_void,
bind_ver,
0,
name,
interface,
bind_ver,
null_mut(),
);
state.shm = proxy as usize;
}
}
}
pub(crate) unsafe extern "C" fn linux_wl_registry_global_remove(
_data: *mut c_void,
_registry: *mut c_void,
_name: u32,
) {
}
pub(crate) unsafe extern "C" fn linux_wl_buffer_release(data: *mut c_void, _buffer: *mut c_void) {
if data.is_null() {
return;
}
unsafe {
let busy_flag: &AtomicBool = &*data.cast::<AtomicBool>();
busy_flag.store(false, Ordering::Release);
}
}
fn linux_wayland_bind_globals(wl_display: *mut c_void) -> (usize, usize, u64, u32) {
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl_display.is_null()
|| wl.proxy_marshal_flags == 0
|| wl.proxy_add_listener == 0
|| wl.display_roundtrip == 0
|| wl.registry_interface == 0
{
return (0, 0, 0, 0);
}
unsafe {
let marshal_get_reg: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
*mut c_void,
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
let add_listener: unsafe extern "C" fn(*mut c_void, *const c_void, *mut c_void) -> i32 =
transmute(wl.proxy_add_listener as *const c_void);
let roundtrip: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_roundtrip as *const c_void);
let registry: *mut c_void = marshal_get_reg(
wl_display,
WL_OPCODE_DISPLAY_GET_REGISTRY,
wl.registry_interface as *const c_void,
1,
0,
null_mut(),
);
if registry.is_null() {
return (0, 0, 0, 0);
}
let mut bind_state: WlRegistryBindState = WlRegistryBindState::default();
let _: i32 = add_listener(
registry,
(&raw const WL_REGISTRY_LISTENER).cast::<c_void>(),
(&raw mut bind_state).cast::<c_void>(),
);
let _: i32 = roundtrip(wl_display);
if wl.proxy_destroy != 0 {
let proxy_destroy: unsafe extern "C" fn(*mut c_void) =
transmute(wl.proxy_destroy as *const c_void);
proxy_destroy(registry);
}
let mut surface: u64 = 0;
let mut surface_version: u32 = bind_state.compositor_version;
if bind_state.compositor != 0 && wl.surface_interface != 0 {
let marshal_create_surface: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
*mut c_void,
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
let wl_surface: *mut c_void = marshal_create_surface(
bind_state.compositor as *mut c_void,
WL_OPCODE_COMPOSITOR_CREATE_SURFACE,
wl.surface_interface as *const c_void,
bind_state.compositor_version.max(1),
0,
null_mut(),
);
surface = wl_surface as u64;
if !wl_surface.is_null() && wl.proxy_get_version != 0 {
let get_ver: unsafe extern "C" fn(*mut c_void) -> u32 =
transmute(wl.proxy_get_version as *const c_void);
surface_version = get_ver(wl_surface);
}
}
(
bind_state.compositor,
bind_state.shm,
surface,
surface_version,
)
}
}
#[must_use]
pub(crate) fn linux_display_refresh_rate_hz(display: *mut c_void, window: usize) -> f32 {
if let Ok(val) = env::var(LINUX_REFRESH_ENV_VAR)
&& let Ok(parsed) = val.trim().parse::<f32>()
&& parsed.is_finite()
&& parsed >= MIN_REFRESH_RATE_HZ
{
return parsed.max(MIN_REFRESH_RATE_HZ);
}
if display.is_null() || window == 0 {
return FALLBACK_REFRESH_RATE_HZ;
}
let syms: &LinuxXRandrSyms = linux_xrandr_syms();
if syms.get_screen_info == 0
|| syms.config_current_rate == 0
|| syms.free_screen_config_info == 0
{
return FALLBACK_REFRESH_RATE_HZ;
}
unsafe {
let get_info: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void =
transmute(syms.get_screen_info as *const c_void);
let cur_rate: unsafe extern "C" fn(*mut c_void) -> i16 =
transmute(syms.config_current_rate as *const c_void);
let free_info: unsafe extern "C" fn(*mut c_void) =
transmute(syms.free_screen_config_info as *const c_void);
let config: *mut c_void = get_info(display, window);
if config.is_null() {
return FALLBACK_REFRESH_RATE_HZ;
}
let rate: i16 = cur_rate(config);
free_info(config);
if rate >= 24 {
(rate as f32).max(MIN_REFRESH_RATE_HZ)
} else {
FALLBACK_REFRESH_RATE_HZ
}
}
}
pub(crate) fn linux_x11_query_scale_factor(display: *mut c_void) -> f32 {
for var_name in LINUX_DPI_ENV_VARS.iter() {
if let Ok(val) = env::var(var_name)
&& let Ok(parsed) = val.trim().parse::<f32>()
&& parsed.is_finite()
&& parsed > 0.0
{
return clamp_viewport_scale(parsed);
}
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.resource_manager_string != 0 && !display.is_null() {
unsafe {
let x_rm: unsafe extern "C" fn(*mut c_void) -> *const c_char =
transmute(syms.resource_manager_string as *const c_void);
let raw_ptr: *const c_char = x_rm(display);
if !raw_ptr.is_null()
&& let Ok(text) = CStr::from_ptr(raw_ptr).to_str()
{
for line in text.lines() {
let trimmed: &str = line.trim();
if let Some(rest) = trimmed.strip_prefix(XFT_DPI_PREFIX)
&& let Ok(dpi) = rest.trim().parse::<f32>()
&& dpi.is_finite()
&& dpi > 24.0
{
return clamp_viewport_scale(dpi / X11_DEFAULT_DPI);
}
}
}
}
}
1.0
}
pub(crate) fn linux_wayland_resolve_buffer_scale(scale_factor: f32) -> i32 {
let safe_scale: f32 = clamp_viewport_scale(scale_factor);
safe_scale.ceil().clamp(1.0, VIEWPORT_SCALE_MAX) as i32
}
fn linux_x11_probe_mit_shm(display: *mut c_void) -> bool {
let shm: &LinuxXShmSyms = linux_xshm_syms();
if display.is_null()
|| shm.query_extension == 0
|| shm.create_image == 0
|| shm.attach == 0
|| shm.detach == 0
|| shm.put_image == 0
{
return false;
}
unsafe {
let query_fn: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(shm.query_extension as *const c_void);
query_fn(display) != 0
}
}
#[must_use]
fn linux_should_prefer_wayland() -> bool {
if let Ok(force_x11) = env::var(LINUX_FORCE_X11_ENV)
&& force_x11.trim() == "1"
{
return false;
}
env::var(LINUX_WAYLAND_DISPLAY_ENV)
.map(|val: String| !val.trim().is_empty())
.unwrap_or(false)
}
pub(crate) fn linux_create_native_window(
config: &WindowConfig,
) -> Result<LinuxNativeWindowHandle, PlatformError> {
unsafe {
if linux_should_prefer_wayland() {
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.lib != 0 && wl.display_connect != 0 {
let wl_connect: unsafe extern "C" fn(*const c_char) -> *mut c_void =
transmute(wl.display_connect as *const c_void);
let wl_display: *mut c_void = wl_connect(null());
if !wl_display.is_null() {
let (wl_compositor, wl_shm, wl_surface, wl_surface_version): (
usize,
usize,
u64,
u32,
) = linux_wayland_bind_globals(wl_display);
let scale_factor: f32 = linux_x11_query_scale_factor(null_mut());
return Ok(LinuxNativeWindowHandle {
lib: wl.lib,
is_wayland: true,
display: wl_display as usize,
window: wl_surface,
gc: 0,
visual: 0,
depth: 32,
wm_delete_atom: 0,
scale_factor,
mit_shm_available: false,
wayland_vulkan_wsi: wl.vulkan_wsi_available,
xlib_vulkan_wsi: false,
wl_compositor,
wl_shm,
wl_surface_version,
});
}
}
}
let x11: &LinuxX11Syms = linux_x11_syms();
if x11.lib != 0
&& x11.open_display != 0
&& x11.default_screen != 0
&& x11.root_window != 0
&& x11.create_simple_window != 0
{
let x_open: unsafe extern "C" fn(*const c_char) -> *mut c_void =
transmute(x11.open_display as *const c_void);
let display: *mut c_void = x_open(null());
if !display.is_null() {
let x_screen: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(x11.default_screen as *const c_void);
let x_root: unsafe extern "C" fn(*mut c_void, i32) -> u64 =
transmute(x11.root_window as *const c_void);
let x_create: unsafe extern "C" fn(
*mut c_void,
u64,
i32,
i32,
u32,
u32,
u32,
u64,
u64,
) -> u64 = transmute(x11.create_simple_window as *const c_void);
let screen: i32 = x_screen(display);
let root: u64 = x_root(display, screen);
let scale_factor: f32 = linux_x11_query_scale_factor(display);
let phys_w: u32 = ((config.get_width() as f32) * scale_factor)
.round()
.max(1.0) as u32;
let phys_h: u32 = ((config.get_height() as f32) * scale_factor)
.round()
.max(1.0) as u32;
let (pos_x, pos_y): (i32, i32) = config.position_value().unwrap_or((0, 0));
let win: u64 = x_create(display, root, pos_x, pos_y, phys_w, phys_h, 0, 0, 0);
let gc: usize = if x11.default_gc != 0 {
let x_gc: unsafe extern "C" fn(*mut c_void, i32) -> *mut c_void =
transmute(x11.default_gc as *const c_void);
x_gc(display, screen) as usize
} else {
0
};
let visual: usize = if x11.default_visual != 0 {
let x_vis: unsafe extern "C" fn(*mut c_void, i32) -> *mut c_void =
transmute(x11.default_visual as *const c_void);
x_vis(display, screen) as usize
} else {
0
};
let depth: u32 = if x11.default_depth != 0 {
let x_dep: unsafe extern "C" fn(*mut c_void, i32) -> i32 =
transmute(x11.default_depth as *const c_void);
x_dep(display, screen).max(24) as u32
} else {
24
};
if x11.select_input != 0 {
let x_select: unsafe extern "C" fn(*mut c_void, u64, isize) -> i32 =
transmute(x11.select_input as *const c_void);
let _: i32 = x_select(display, win, X11_EVENT_MASK);
}
let mut wm_delete_atom: u64 = 0;
if x11.intern_atom != 0 && x11.set_wm_protocols != 0 {
let x_intern: unsafe extern "C" fn(*mut c_void, *const c_char, i32) -> u64 =
transmute(x11.intern_atom as *const c_void);
let x_proto: unsafe extern "C" fn(*mut c_void, u64, *mut u64, i32) -> i32 =
transmute(x11.set_wm_protocols as *const c_void);
wm_delete_atom =
x_intern(display, ATOM_WM_DELETE_WINDOW.as_ptr().cast::<c_char>(), 0);
if wm_delete_atom != 0 {
let _: i32 = x_proto(display, win, &mut wm_delete_atom, 1);
}
}
if x11.store_name != 0
&& let Ok(c_title) = CString::new(config.get_title().as_str())
{
let x_store: unsafe extern "C" fn(*mut c_void, u64, *const c_char) -> i32 =
transmute(x11.store_name as *const c_void);
let _: i32 = x_store(display, win, c_title.as_ptr());
}
if x11.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(x11.flush as *const c_void);
let _: i32 = x_flush(display);
}
let mit_shm_available: bool = linux_x11_probe_mit_shm(display);
let wl_probe: &LinuxWaylandSyms = linux_wayland_syms();
return Ok(LinuxNativeWindowHandle {
lib: x11.lib,
is_wayland: false,
display: display as usize,
window: win,
gc,
visual,
depth,
wm_delete_atom,
scale_factor,
mit_shm_available,
wayland_vulkan_wsi: false,
xlib_vulkan_wsi: wl_probe.xlib_vulkan_wsi_available,
wl_compositor: 0,
wl_shm: 0,
wl_surface_version: 0,
});
}
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.lib != 0 && wl.display_connect != 0 {
let wl_connect: unsafe extern "C" fn(*const c_char) -> *mut c_void =
transmute(wl.display_connect as *const c_void);
let wl_display: *mut c_void = wl_connect(null());
if !wl_display.is_null() {
let (wl_compositor, wl_shm, wl_surface, wl_surface_version): (
usize,
usize,
u64,
u32,
) = linux_wayland_bind_globals(wl_display);
return Ok(LinuxNativeWindowHandle {
lib: wl.lib,
is_wayland: true,
display: wl_display as usize,
window: wl_surface,
gc: 0,
visual: 0,
depth: 32,
wm_delete_atom: 0,
scale_factor: linux_x11_query_scale_factor(null_mut()),
mit_shm_available: false,
wayland_vulkan_wsi: wl.vulkan_wsi_available,
xlib_vulkan_wsi: false,
wl_compositor,
wl_shm,
wl_surface_version,
});
}
}
}
Err(PlatformError::InitializationFailed(
ERR_LINUX_NO_DISPLAY.to_string(),
))
}
pub(crate) fn linux_x11_map_window(display: usize, window: u64) {
if display == 0 || window == 0 {
return;
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.map_window == 0 {
return;
}
unsafe {
let x_map: unsafe extern "C" fn(*mut c_void, u64) -> i32 =
transmute(syms.map_window as *const c_void);
let _: i32 = x_map(display as *mut c_void, window);
if syms.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.flush as *const c_void);
let _: i32 = x_flush(display as *mut c_void);
}
}
}
fn linux_pack_bgra_subrect(
framebuffer: &Framebuffer,
x0: u32,
y0: u32,
width: u32,
height: u32,
) -> Vec<u8> {
let fb_w: usize = framebuffer.get_width() as usize;
let src: &[u8] = framebuffer.get_pixels().as_slice();
let mut bgra: Vec<u8> = Vec::with_capacity((width as usize) * (height as usize) * 4);
for row in y0..(y0 + height) {
let row_start: usize = ((row as usize) * fb_w + (x0 as usize)) * 4;
let row_end: usize = row_start + (width as usize) * 4;
if row_end <= src.len() {
for px in src[row_start..row_end].as_chunks::<4>().0 {
bgra.push(px[2]);
bgra.push(px[1]);
bgra.push(px[0]);
bgra.push(px[3]);
}
}
}
bgra
}
#[must_use]
pub(crate) fn linux_normalize_damage_rects(
framebuffer: &Framebuffer,
damage: DamageRegion,
) -> Vec<(u32, u32, u32, u32)> {
let fb_w: u32 = framebuffer.get_width();
let fb_h: u32 = framebuffer.get_height();
if fb_w == 0 || fb_h == 0 {
return Vec::new();
}
if damage.get_full_damage() {
return vec![(0, 0, fb_w, fb_h)];
}
let DamageRegion {
rects: raw_rects,
rect_count,
..
} = damage;
let count: usize = (rect_count as usize).min(raw_rects.len());
let mut rects: Vec<(u32, u32, u32, u32)> = Vec::new();
for rect in raw_rects[..count].iter() {
let x0: u32 = (rect.get_x().floor().max(0.0) as u32).min(fb_w);
let y0: u32 = (rect.get_y().floor().max(0.0) as u32).min(fb_h);
let x1: u32 = ((rect.get_x() + rect.get_width()).ceil().max(0.0) as u32).min(fb_w);
let y1: u32 = ((rect.get_y() + rect.get_height()).ceil().max(0.0) as u32).min(fb_h);
if x1 > x0 && y1 > y0 {
rects.push((x0, y0, x1 - x0, y1 - y0));
}
}
if rects.is_empty()
&& let Some(bounds) = damage.bounds_rect()
{
let x0: u32 = (bounds.get_x().floor().max(0.0) as u32).min(fb_w);
let y0: u32 = (bounds.get_y().floor().max(0.0) as u32).min(fb_h);
let x1: u32 = ((bounds.get_x() + bounds.get_width()).ceil().max(0.0) as u32).min(fb_w);
let y1: u32 = ((bounds.get_y() + bounds.get_height()).ceil().max(0.0) as u32).min(fb_h);
if x1 > x0 && y1 > y0 {
rects.push((x0, y0, x1 - x0, y1 - y0));
}
}
rects
}
pub(crate) fn linux_copy_rgba_to_wayland_shm(
framebuffer: &Framebuffer,
dst: &mut [u8],
stride: usize,
rects: &[(u32, u32, u32, u32)],
format: u32,
) -> u64 {
let fb_w: usize = framebuffer.get_width() as usize;
let src: &[u8] = framebuffer.get_pixels().as_slice();
let opaque_alpha: bool = format == WL_SHM_FORMAT_XRGB8888;
let mut copied_bytes: u64 = 0;
for &(x0, y0, rw, rh) in rects.iter() {
let row_pixels: usize = rw as usize;
let row_bytes: usize = row_pixels.saturating_mul(4);
for row in y0..(y0 + rh) {
let src_start: usize = ((row as usize) * fb_w + (x0 as usize)) * 4;
let src_end: usize = src_start + row_bytes;
let dst_start: usize = (row as usize) * stride + (x0 as usize) * 4;
let dst_end: usize = dst_start + row_bytes;
if src_end <= src.len() && dst_end <= dst.len() {
let src_row: &[u8] = &src[src_start..src_end];
let dst_row: &mut [u8] = &mut dst[dst_start..dst_end];
for (src_px, dst_px) in src_row
.as_chunks::<4>()
.0
.iter()
.zip(dst_row.as_chunks_mut::<4>().0.iter_mut())
{
dst_px[0] = src_px[2];
dst_px[1] = src_px[1];
dst_px[2] = src_px[0];
dst_px[3] = if opaque_alpha { 0xFF } else { src_px[3] };
}
copied_bytes = copied_bytes.saturating_add(row_bytes as u64);
}
}
}
copied_bytes
}
pub(crate) fn linux_present_window_region(
rec: &mut LinuxWindowRecord,
framebuffer: &Framebuffer,
damage: DamageRegion,
) -> Result<LinuxPresentTelemetry, PlatformError> {
if rec.get_is_wayland() {
let display: usize = rec.get_display();
let surface: usize = rec.get_window() as usize;
let wl_shm: usize = rec.get_wl_shm();
let surface_version: u32 = rec.get_wl_surface_version();
let buffer_scale: i32 =
linux_wayland_resolve_buffer_scale(rec.get_state().get_scale_factor());
let mut transport: NativeWaylandShmTransport =
NativeWaylandShmTransport::new(display, surface, wl_shm, surface_version);
transport.set_buffer_scale(buffer_scale);
let shm_arc: Arc<Mutex<WaylandShmSwapchain>> = rec.wayland_shm_handle();
let result: Result<LinuxPresentTelemetry, PlatformError> = match shm_arc.lock() {
Ok(mut guard) => guard.present_framebuffer(framebuffer, damage, &mut transport),
Err(_) => Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SURFACE_MISSING.to_string(),
)),
};
match result {
Ok(telemetry) => {
rec.set_presentation_mode(LinuxPresentationMode::WaylandShmSoftware);
rec.set_cpu_mirror_active(telemetry.get_cpu_mirror_active());
rec.set_last_telemetry(telemetry);
Ok(telemetry)
}
Err(err) => {
let mut failed: LinuxPresentTelemetry = rec.get_last_telemetry();
failed.set_mode(LinuxPresentationMode::WaylandShmSoftware);
failed.set_outcome(LinuxPresentOutcome::Failed);
failed.set_cpu_mirror_active(true);
failed.set_upload_bytes(0);
failed.set_dirty_rect_count(0);
failed.set_failed_frames(failed.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::WaylandShmSoftware);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(failed);
Err(err)
}
}
} else {
linux_x11_present_framebuffer_region(rec, framebuffer, damage)
}
}
fn linux_x11_present_framebuffer_region(
rec: &mut LinuxWindowRecord,
framebuffer: &Framebuffer,
damage: DamageRegion,
) -> Result<LinuxPresentTelemetry, PlatformError> {
let mut telemetry: LinuxPresentTelemetry = rec.get_last_telemetry();
telemetry.set_mode(LinuxPresentationMode::X11Software);
telemetry.set_cpu_mirror_active(true);
let fb_w: u32 = framebuffer.get_width();
let fb_h: u32 = framebuffer.get_height();
let required_bytes: usize = (fb_w as usize)
.saturating_mul(fb_h as usize)
.saturating_mul(4);
if fb_w == 0 || fb_h == 0 || framebuffer.get_pixels().len() < required_bytes {
telemetry.set_outcome(LinuxPresentOutcome::Failed);
telemetry.set_upload_bytes(0);
telemetry.set_dirty_rect_count(0);
telemetry.set_failed_frames(telemetry.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Err(PlatformError::SurfaceError(
ERR_LINUX_INVALID_FRAMEBUFFER.to_string(),
));
}
let rects: Vec<(u32, u32, u32, u32)> = linux_normalize_damage_rects(framebuffer, damage);
if rects.is_empty() {
telemetry.set_outcome(LinuxPresentOutcome::SkippedUnchanged);
telemetry.set_upload_bytes(0);
telemetry.set_dirty_rect_count(0);
telemetry.set_skipped_unchanged_frames(
telemetry.get_skipped_unchanged_frames().saturating_add(1),
);
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Ok(telemetry);
}
let syms: &LinuxX11Syms = linux_x11_syms();
if rec.get_display() == 0
|| rec.get_window() == 0
|| rec.get_gc() == 0
|| rec.get_visual() == 0
|| syms.create_image == 0
|| syms.put_image == 0
{
telemetry.set_outcome(LinuxPresentOutcome::Failed);
telemetry.set_upload_bytes(0);
telemetry.set_dirty_rect_count(0);
telemetry.set_failed_frames(telemetry.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Err(PlatformError::SurfaceError(
ERR_LINUX_X11_SURFACE_MISSING.to_string(),
));
}
let mut upload_bytes: u64 = 0;
unsafe {
let x_create_img: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
u32,
i32,
i32,
*mut c_char,
u32,
u32,
i32,
i32,
) -> *mut c_void = transmute(syms.create_image as *const c_void);
let x_put_img: unsafe extern "C" fn(
*mut c_void,
u64,
*mut c_void,
*mut c_void,
i32,
i32,
i32,
i32,
u32,
u32,
) -> i32 = transmute(syms.put_image as *const c_void);
let display: *mut c_void = rec.get_display() as *mut c_void;
let visual: *mut c_void = rec.get_visual() as *mut c_void;
let gc: *mut c_void = rec.get_gc() as *mut c_void;
for &(x0, y0, rw, rh) in rects.iter() {
let mut bgra: Vec<u8> = if x0 == 0 && y0 == 0 && rw == fb_w && rh == fb_h {
framebuffer.to_bgra_bytes()
} else {
linux_pack_bgra_subrect(framebuffer, x0, y0, rw, rh)
};
let stride: i32 = (rw as i32) * 4;
let img: *mut c_void = x_create_img(
display,
visual,
rec.get_depth(),
X11_Z_PIXMAP,
0,
bgra.as_mut_ptr().cast::<c_char>(),
rw,
rh,
32,
stride,
);
if img.is_null() {
telemetry.set_outcome(LinuxPresentOutcome::Failed);
telemetry.set_upload_bytes(upload_bytes);
telemetry.set_dirty_rect_count(rects.len() as u32);
telemetry.set_failed_frames(telemetry.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Err(PlatformError::SurfaceError(
ERR_LINUX_X11_SURFACE_MISSING.to_string(),
));
}
let _: i32 = x_put_img(
display,
rec.get_window(),
gc,
img,
0,
0,
x0 as i32,
y0 as i32,
rw,
rh,
);
upload_bytes = upload_bytes.saturating_add(bgra.len() as u64);
if syms.free != 0 {
let x_free: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.free as *const c_void);
let _: i32 = x_free(img);
}
}
if syms.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.flush as *const c_void);
let _: i32 = x_flush(display);
}
}
telemetry.set_outcome(LinuxPresentOutcome::Submitted);
telemetry.set_upload_bytes(upload_bytes);
telemetry.set_dirty_rect_count(rects.len() as u32);
telemetry.set_submitted_frames(telemetry.get_submitted_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
Ok(telemetry)
}
#[must_use]
fn linux_x11_modifiers(state: u32) -> Modifiers {
Modifiers::new(
(state & X11_SHIFT_MASK) != 0,
(state & X11_CTRL_MASK) != 0,
(state & X11_ALT_MASK) != 0,
(state & X11_META_MASK) != 0,
)
}
#[must_use]
fn linux_x11_keysym_to_keycode(keysym: u64, ch: Option<char>) -> KeyCode {
match keysym {
0xFF08 => KeyCode::Backspace,
0xFF09 => KeyCode::Tab,
0xFF0D | 0xFF8D => KeyCode::Enter,
0xFF1B => KeyCode::Escape,
0xFF50 => KeyCode::Home,
0xFF51 => KeyCode::ArrowLeft,
0xFF52 => KeyCode::ArrowUp,
0xFF53 => KeyCode::ArrowRight,
0xFF54 => KeyCode::ArrowDown,
0xFF55 => KeyCode::PageUp,
0xFF56 => KeyCode::PageDown,
0xFF57 => KeyCode::End,
0xFFFF => KeyCode::Delete,
0x0020 => KeyCode::Space,
0x0021..=0x007E => KeyCode::Character((keysym as u8 as char).to_ascii_lowercase()),
_ => match ch {
Some(char_item) if !char_item.is_control() => KeyCode::Character(char_item),
_ => KeyCode::Unknown,
},
}
}
#[must_use]
fn linux_find_window(
xid: u64,
map: &HashMap<WindowId, LinuxWindowRecord>,
) -> Option<(WindowId, f32, u64)> {
for (wid, rec) in map.iter() {
if !rec.get_is_wayland() && rec.get_window() == xid {
return Some((
*wid,
rec.get_state().get_scale_factor(),
rec.get_wm_delete_atom(),
));
}
}
None
}
unsafe fn linux_x11_handle_raw_event(
raw: &X11EventRaw,
lookup_sym: *mut c_void,
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
) {
let ev_type: i32 = ((raw.pad[0] as u64) & 0xFFFF_FFFF) as i32;
unsafe {
match ev_type {
X11_CONFIGURE_NOTIFY => {
let cfg_ev: X11ConfigureEvent =
*(raw as *const X11EventRaw).cast::<X11ConfigureEvent>();
if let Some((wid, scale, _)) = linux_find_window(cfg_ev.window, windows) {
let phys_w: u32 = cfg_ev.width.max(1) as u32;
let phys_h: u32 = cfg_ev.height.max(1) as u32;
let exact_lw: f32 = ((phys_w as f32) / scale).max(1.0);
let exact_lh: f32 = ((phys_h as f32) / scale).max(1.0);
let log_w: u32 = exact_lw.round().max(1.0) as u32;
let log_h: u32 = exact_lh.round().max(1.0) as u32;
if let Some(rec) = windows.get_mut(&wid) {
let prev_w: u32 = rec.get_state().get_physical_width();
let prev_h: u32 = rec.get_state().get_physical_height();
if prev_w != phys_w || prev_h != phys_h {
rec.get_mut_config().set_width(log_w);
rec.get_mut_config().set_height(log_h);
rec.get_mut_state().set_logical_width(exact_lw);
rec.get_mut_state().set_logical_height(exact_lh);
rec.get_mut_state().set_physical_width(phys_w);
rec.get_mut_state().set_physical_height(phys_h);
out_events.push(WindowEvent::Resized(wid, log_w, log_h, scale));
}
}
}
}
X11_EXPOSE => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(kb.window, windows) {
out_events.push(WindowEvent::RedrawRequested(wid));
}
}
X11_CLIENT_MESSAGE => {
let cm: X11ClientMessageEvent =
*(raw as *const X11EventRaw).cast::<X11ClientMessageEvent>();
if let Some((wid, _, wm_del)) = linux_find_window(cm.window, windows)
&& wm_del != 0
&& cm.data[0] == wm_del
{
out_events.push(WindowEvent::CloseRequested(wid));
}
}
X11_FOCUS_IN | X11_FOCUS_OUT => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(kb.window, windows) {
let focused: bool = ev_type == X11_FOCUS_IN;
if let Some(rec) = windows.get_mut(&wid) {
rec.get_mut_state().set_focused(focused);
}
if !focused {
out_events.push(WindowEvent::PointerCancel(wid, 0));
}
out_events.push(WindowEvent::Focused(wid, focused));
let lc_ev: SystemEvent = if focused {
SystemEvent::Foreground
} else {
SystemEvent::Inactive
};
out_events.push(WindowEvent::System(lc_ev));
}
}
X11_UNMAP_NOTIFY => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(kb.window, windows) {
out_events.push(WindowEvent::PointerCancel(wid, 0));
out_events.push(WindowEvent::System(SystemEvent::Background));
}
}
X11_MAP_NOTIFY => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if linux_find_window(kb.window, windows).is_some() {
out_events.push(WindowEvent::System(SystemEvent::Foreground));
}
}
X11_DESTROY_NOTIFY => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if linux_find_window(kb.window, windows).is_some() {
out_events.push(WindowEvent::System(SystemEvent::Terminate));
}
}
X11_MOTION_NOTIFY => {
let mot: X11MotionEvent = *(raw as *const X11EventRaw).cast::<X11MotionEvent>();
if let Some((wid, scale, _)) = linux_find_window(mot.window, windows) {
let pos: Point = Point::new((mot.x as f32) / scale, (mot.y as f32) / scale);
let pressed_btn: f32 = if (mot.state & 0x0100) != 0 { 1.0 } else { 0.0 };
let point_coords: PointerEvent = PointerEvent::new(
1,
PointerDevice::Mouse,
pos,
PointerButton::Primary,
pressed_btn,
linux_x11_modifiers(mot.state),
);
out_events.push(WindowEvent::PointerMove(wid, point_coords));
}
}
X11_BUTTON_PRESS | X11_BUTTON_RELEASE => {
let button_node: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, scale, _)) = linux_find_window(button_node.window, windows) {
if ev_type == X11_BUTTON_PRESS
&& (button_node.detail == 4 || button_node.detail == 5)
{
let step: f32 = if button_node.detail == 4 { -40.0 } else { 40.0 };
let delta: Point = if (button_node.state & X11_SHIFT_MASK) != 0 {
Point::new(step, 0.0)
} else {
Point::new(0.0, step)
};
out_events.push(WindowEvent::Scroll(wid, delta));
} else if ev_type == X11_BUTTON_PRESS
&& (button_node.detail == 6 || button_node.detail == 7)
{
let dx: f32 = if button_node.detail == 6 { -40.0 } else { 40.0 };
out_events.push(WindowEvent::Scroll(wid, Point::new(dx, 0.0)));
} else if button_node.detail <= 3 {
let button: PointerButton = match button_node.detail {
2 => PointerButton::Auxiliary,
3 => PointerButton::Secondary,
_ => PointerButton::Primary,
};
let pos: Point = Point::new(
(button_node.x as f32) / scale,
(button_node.y as f32) / scale,
);
let pressed: bool = ev_type == X11_BUTTON_PRESS;
let pev: PointerEvent = PointerEvent::new(
1,
PointerDevice::Mouse,
pos,
button,
if pressed { 1.0 } else { 0.0 },
linux_x11_modifiers(button_node.state),
);
if pressed {
out_events.push(WindowEvent::PointerDown(wid, pev));
} else {
out_events.push(WindowEvent::PointerUp(wid, pev));
}
}
}
}
X11_KEY_PRESS | X11_KEY_RELEASE => {
let mut key_ev: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(key_ev.window, windows) {
let mut buf: [u8; 16] = [0; 16];
let mut keysym: u64 = 0;
let mut len: i32 = 0;
if !lookup_sym.is_null() {
let lookup_fn: unsafe extern "C" fn(
*mut X11KeyOrButtonEvent,
*mut c_char,
i32,
*mut u64,
*mut c_void,
) -> i32 = transmute(lookup_sym);
len = lookup_fn(
&mut key_ev,
buf.as_mut_ptr().cast::<c_char>(),
16,
&mut keysym,
null_mut(),
);
}
let decoded_str: Option<&str> = if len > 0 {
from_utf8(&buf[..(len as usize).min(16)]).ok()
} else {
None
};
let first_ch: Option<char> =
decoded_str.and_then(|text_value: &str| text_value.chars().next());
let code: KeyCode = linux_x11_keysym_to_keycode(keysym, first_ch);
let mods: Modifiers = linux_x11_modifiers(key_ev.state);
let kev: KeyEvent = KeyEvent::new(code, mods, false);
if ev_type == X11_KEY_PRESS {
out_events.push(WindowEvent::KeyDown(wid, kev));
if !mods.get_ctrl()
&& !mods.get_meta()
&& let Some(text) = decoded_str
&& text
.chars()
.all(|char_value: char| !char_value.is_control())
&& !text.is_empty()
{
out_events.push(WindowEvent::TextInput(wid, text.to_string()));
}
} else {
out_events.push(WindowEvent::KeyUp(wid, kev));
}
}
}
_ => {}
}
}
}
pub(crate) fn linux_x11_pump_events(
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
) {
if windows.is_empty() {
return;
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.display_dispatch_pending != 0 {
let mut wl_displays: Vec<usize> = Vec::new();
for rec in windows.values() {
if rec.get_is_wayland()
&& rec.get_display() != 0
&& !wl_displays.contains(&rec.get_display())
{
wl_displays.push(rec.get_display());
}
}
unsafe {
let wl_dispatch_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_dispatch_pending as *const c_void);
for disp in wl_displays.into_iter() {
let _: i32 = wl_dispatch_pending(disp as *mut c_void);
}
}
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.pending == 0 || syms.next_event == 0 {
return;
}
let displays: Vec<usize> = {
let mut seen: Vec<usize> = Vec::new();
for rec in windows.values() {
if !rec.get_is_wayland() && rec.get_display() != 0 && !seen.contains(&rec.get_display())
{
seen.push(rec.get_display());
}
}
seen
};
let lookup_sym: *mut c_void = syms.lookup_string as *mut c_void;
unsafe {
let x_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.pending as *const c_void);
let x_next: unsafe extern "C" fn(*mut c_void, *mut X11EventRaw) -> i32 =
transmute(syms.next_event as *const c_void);
for disp_addr in displays.into_iter() {
let disp: *mut c_void = disp_addr as *mut c_void;
let mut raw: X11EventRaw = X11EventRaw { pad: [0; 24] };
while x_pending(disp) > 0 {
let _: i32 = x_next(disp, &mut raw);
linux_x11_handle_raw_event(&raw, lookup_sym, windows, out_events);
}
}
}
}
pub(crate) fn linux_x11_wait_events(
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
wake_read_fd: i32,
) {
if windows.is_empty() {
return;
}
if let Some(wl_disp) = windows
.values()
.find(|rec: &&LinuxWindowRecord| rec.get_is_wayland() && rec.get_display() != 0)
.map(LinuxWindowRecord::get_display)
{
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.display_get_fd != 0
&& wl.display_prepare_read != 0
&& wl.display_read_events != 0
&& wl.display_cancel_read != 0
&& wl.display_dispatch_pending != 0
{
unsafe {
let get_fd: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_get_fd as *const c_void);
let prepare_read: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_prepare_read as *const c_void);
let read_events: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_read_events as *const c_void);
let cancel_read: unsafe extern "C" fn(*mut c_void) =
transmute(wl.display_cancel_read as *const c_void);
let dispatch_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_dispatch_pending as *const c_void);
let dpy_ptr: *mut c_void = wl_disp as *mut c_void;
while prepare_read(dpy_ptr) != 0 {
let _: i32 = dispatch_pending(dpy_ptr);
}
if wl.display_flush != 0 {
let flush_fn: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_flush as *const c_void);
let _: i32 = flush_fn(dpy_ptr);
}
let wl_fd: i32 = get_fd(dpy_ptr);
let mut fds: [LinuxPollFd; 2] = [
LinuxPollFd {
fd: wl_fd,
events: LINUX_POLLIN,
revents: 0,
},
LinuxPollFd {
fd: wake_read_fd,
events: LINUX_POLLIN,
revents: 0,
},
];
let nfds: usize = if wake_read_fd >= 0 { 2 } else { 1 };
let ready: i32 = poll(fds.as_mut_ptr().cast::<c_void>(), nfds, -1);
if ready > 0 && (fds[0].revents & LINUX_POLLIN) != 0 {
let _: i32 = read_events(dpy_ptr);
let _: i32 = dispatch_pending(dpy_ptr);
} else {
cancel_read(dpy_ptr);
}
if nfds == 2 && (fds[1].revents & LINUX_POLLIN) != 0 {
linux_drain_wake_pipe(wake_read_fd);
}
}
return;
}
if wl.display_dispatch != 0 {
unsafe {
let wl_dispatch: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_dispatch as *const c_void);
let _: i32 = wl_dispatch(wl_disp as *mut c_void);
}
}
return;
}
let first_disp: usize = windows
.values()
.find(|rec: &&LinuxWindowRecord| !rec.get_is_wayland() && rec.get_display() != 0)
.map(LinuxWindowRecord::get_display)
.unwrap_or(0);
if first_disp == 0 {
return;
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.connection_number != 0 && syms.pending != 0 {
unsafe {
let x_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.pending as *const c_void);
let x_conn_fd: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.connection_number as *const c_void);
let dpy_ptr: *mut c_void = first_disp as *mut c_void;
if x_pending(dpy_ptr) == 0 {
if syms.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.flush as *const c_void);
let _: i32 = x_flush(dpy_ptr);
}
let x_fd: i32 = x_conn_fd(dpy_ptr);
let mut fds: [LinuxPollFd; 2] = [
LinuxPollFd {
fd: x_fd,
events: LINUX_POLLIN,
revents: 0,
},
LinuxPollFd {
fd: wake_read_fd,
events: LINUX_POLLIN,
revents: 0,
},
];
let nfds: usize = if wake_read_fd >= 0 { 2 } else { 1 };
let _: i32 = poll(fds.as_mut_ptr().cast::<c_void>(), nfds, -1);
if nfds == 2 && (fds[1].revents & LINUX_POLLIN) != 0 {
linux_drain_wake_pipe(wake_read_fd);
}
}
}
linux_x11_pump_events(windows, out_events);
return;
}
if syms.next_event == 0 {
return;
}
let lookup_sym: *mut c_void = syms.lookup_string as *mut c_void;
unsafe {
let x_next: unsafe extern "C" fn(*mut c_void, *mut X11EventRaw) -> i32 =
transmute(syms.next_event as *const c_void);
let mut raw: X11EventRaw = X11EventRaw { pad: [0; 24] };
let _: i32 = x_next(first_disp as *mut c_void, &mut raw);
linux_x11_handle_raw_event(&raw, lookup_sym, windows, out_events);
}
linux_x11_pump_events(windows, out_events);
}
pub(crate) fn linux_wayland_disconnect(display: usize) {
if display == 0 {
return;
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.display_disconnect == 0 {
return;
}
unsafe {
let disconnect: unsafe extern "C" fn(*mut c_void) =
transmute(wl.display_disconnect as *const c_void);
disconnect(display as *mut c_void);
}
}
pub(crate) fn linux_x11_set_title(display: usize, window: u64, title: &str) {
let syms: &LinuxX11Syms = linux_x11_syms();
if display == 0 || window == 0 || syms.store_name == 0 {
return;
}
if let Ok(c_title) = CString::new(title) {
unsafe {
let func: unsafe extern "C" fn(*mut c_void, u64, *const c_char) -> i32 =
transmute(syms.store_name as *const c_void);
let _: i32 = func(display as *mut c_void, window, c_title.as_ptr());
}
}
}
pub(crate) fn linux_x11_resize(display: usize, window: u64, width: u32, height: u32) {
let syms: &LinuxX11Syms = linux_x11_syms();
if display == 0 || window == 0 || syms.resize_window == 0 {
return;
}
unsafe {
let func: unsafe extern "C" fn(*mut c_void, u64, u32, u32) -> i32 =
transmute(syms.resize_window as *const c_void);
let _: i32 = func(display as *mut c_void, window, width.max(1), height.max(1));
}
}
pub(crate) fn linux_x11_destroy(display: usize, window: u64) {
let syms: &LinuxX11Syms = linux_x11_syms();
if display == 0 || window == 0 || syms.destroy_window == 0 {
return;
}
unsafe {
let func: unsafe extern "C" fn(*mut c_void, u64) -> i32 =
transmute(syms.destroy_window as *const c_void);
let _: i32 = func(display as *mut c_void, window);
}
}