Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Skia refactor: introduce a GL loader method #11

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions src/native/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,36 +470,12 @@ where
tl_display::set_display(display);
let event_handler = f.0();

let skia_ctx = {
// Skia initialization on OpenGL ES
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
let name = std::ffi::CString::new(proc).unwrap();
let get_proc_address = libegl.eglGetProcAddress.expect("non-null function pointer");
match get_proc_address(name.as_ptr() as _) {
Some(procaddr) => procaddr as *const std::ffi::c_void,
None => std::ptr::null(),
}
})
.expect("Failed to create Skia <-> OpenGL ES interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid);

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

SkiaContext::new(dctx, fb_info, screen_width as i32, screen_height as i32)
};
let skia_ctx = SkiaContext::from_gl_loader(|proc| {
let name = std::ffi::CString::new(proc).unwrap();
let get_procaddr = libegl.eglGetProcAddress.expect("non-null function pointer");
(get_procaddr)(name.as_ptr() as _)
})
.expect("Failed to create Skia <-> OpenGL ES interface");

let mut s = MainThreadState {
libegl,
Expand Down
43 changes: 8 additions & 35 deletions src/native/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,42 +267,15 @@ pub fn define_glk_or_mtk_view_dlg(superclass: &Class) -> *const Class {
panic!("Non-GL backend unsupported with Skia");
}

let skia_ctx = {
// Skia initialization on OpenGL ES
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
if proc == "eglGetCurrentDisplay" {
return std::ptr::null();
}
let skia_ctx = SkiaContext::from_gl_loader(|proc| {
if proc == "eglGetCurrentDisplay" {
None
} else {
let name = std::ffi::CString::new(proc).unwrap();
unsafe {
match get_proc_address(name.as_ptr() as _) {
Some(procaddr) => procaddr as *const std::ffi::c_void,
None => std::ptr::null(),
}
}
})
.expect("Failed to create Skia <-> OpenGL interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
unsafe {
gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid);
}

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

SkiaContext::new(dctx, fb_info, screen_width, screen_height)
};
get_proc_address(name.as_ptr() as _)
}
})
.expect("Failed to create Skia <-> OpenGL interface");

payload.event_handler = Some((f(), skia_ctx));
}
Expand Down
39 changes: 7 additions & 32 deletions src/native/linux_wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use libwayland_egl::*;

use crate::{
event::EventHandler,
gl,
native::{egl, NativeDisplayData},
skia::SkiaContext,
};
Expand Down Expand Up @@ -480,37 +479,6 @@ where
libegl.eglGetProcAddress.expect("non-null function pointer")(name.as_ptr() as _)
});

let skia_ctx = {
// Skia initialization on OpenGL ES
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
let name = std::ffi::CString::new(proc).unwrap();
let get_proc_address = libegl.eglGetProcAddress.expect("non-null function pointer");
match get_proc_address(name.as_ptr() as _) {
Some(procaddr) => procaddr as *const std::ffi::c_void,
None => std::ptr::null(),
}
})
.expect("Failed to create Skia <-> OpenGL ES interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid);

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

SkiaContext::new(dctx, fb_info, conf.window_width, conf.window_height)
};

if !display.decoration_manager.is_null() {
let server_decoration: *mut extensions::xdg_decoration::zxdg_toplevel_decoration_v1 = wl_request_constructor!(
display.client,
Expand Down Expand Up @@ -539,6 +507,13 @@ where

tl_display::set_display(display);

let skia_ctx = SkiaContext::from_gl_loader(|proc| {
let name = std::ffi::CString::new(proc).unwrap();
let get_procaddr = (libegl.eglGetProcAddress).expect("non-null function pointer");
(get_procaddr)(name.as_ptr() as _)
})
.expect("Failed to create Skia <-> OpenGL ES interface");

let event_handler = (f.take().unwrap())();
payload.ctx = Some((event_handler, skia_ctx));

Expand Down
76 changes: 14 additions & 62 deletions src/native/linux_x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,37 +519,14 @@ where
tl_display::with(|d| d.set_fullscreen(window, true));
}

let skia_ctx = {
// Skia initialization on OpenGL
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
if proc == "eglGetCurrentDisplay" {
return std::ptr::null();
}
match glx.libgl.get_procaddr(proc) {
Some(procaddr) => procaddr as *const libc::c_void,
None => std::ptr::null(),
}
})
.expect("Failed to create Skia <-> OpenGL interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
unsafe { gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid) };

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

SkiaContext::new(dctx, fb_info, w, h)
};
let skia_ctx = SkiaContext::from_gl_loader(|proc| {
if proc == "eglGetCurrentDisplay" {
None
} else {
glx.libgl.get_procaddr(proc)
}
})
.expect("Failed to create Skia <-> OpenGL interface");

let mut display = display.with_skia(skia_ctx);
let mut event_handler = (f.take().unwrap())();
Expand Down Expand Up @@ -640,37 +617,12 @@ where

(display.libx11.XFlush)(display.display);

let skia_ctx = {
// Skia initialization on OpenGL ES
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
let name = std::ffi::CString::new(proc).unwrap();
let get_procaddr = (egl_lib.eglGetProcAddress).expect("non-null function pointer");

match (get_procaddr)(name.as_ptr() as _) {
Some(procaddr) => procaddr as *const libc::c_void,
None => std::ptr::null(),
}
})
.expect("Failed to create Skia <-> OpenGL ES interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
unsafe { gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid) };

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

SkiaContext::new(dctx, fb_info, w, h)
};
let skia_ctx = SkiaContext::from_gl_loader(|proc| {
let name = std::ffi::CString::new(proc).unwrap();
let get_procaddr = (egl_lib.eglGetProcAddress).expect("non-null function pointer");
(get_procaddr)(name.as_ptr() as _)
})
.expect("Failed to create Skia <-> OpenGL ES interface");

let mut display = display.with_skia(skia_ctx);
let mut event_handler = (f.take().unwrap())();
Expand Down
97 changes: 14 additions & 83 deletions src/native/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,49 +556,15 @@ pub fn define_opengl_view_class() -> *const Class {
unsafe { get_proc_address(name.as_ptr() as _) }
});

let skia_ctx = {
// Skia initialization on OpenGL ES
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
if proc == "eglGetCurrentDisplay" {
return std::ptr::null();
}
let skia_ctx = SkiaContext::from_gl_loader(|proc| {
if proc == "eglGetCurrentDisplay" {
None
} else {
let name = std::ffi::CString::new(proc).unwrap();
unsafe {
match get_proc_address(name.as_ptr() as _) {
Some(procaddr) => procaddr as *const std::ffi::c_void,
None => std::ptr::null(),
}
}
})
.expect("Failed to create Skia <-> OpenGL interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
unsafe {
gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid);
}

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

let bounds: NSRect = unsafe { msg_send![this, bounds] };

SkiaContext::new(
dctx,
fb_info,
bounds.size.width as i32,
bounds.size.height as i32,
)
};
get_proc_address(name.as_ptr() as _)
}
})
.expect("Failed to create Skia <-> OpenGL interface");

let payload = get_window_payload(this);
let f = payload.f.take().unwrap();
Expand Down Expand Up @@ -655,47 +621,12 @@ pub fn define_metal_view_class() -> *const Class {
let payload = get_window_payload(this);

if payload.ctx.is_none() {
let skia_ctx = {
// Skia initialization on OpenGL ES
use skia_safe::gpu::{gl::FramebufferInfo, DirectContext};
use std::convert::TryInto;

// TODO: Skia <-> Metal interface
#[allow(clippy::diverging_sub_expression)]
let interface = unimplemented!();

// let interface = skia_safe::gpu::gl::Interface::new_load_with(|proc| {
// if proc == "eglGetCurrentDisplay" {
// return std::ptr::null();
// }
// let name = std::ffi::CString::new(proc).unwrap();
// unsafe {
// match get_proc_address(name.as_ptr() as _) {
// Some(procaddr) => procaddr as *const std::ffi::c_void,
// None => std::ptr::null(),
// }
// }
// })
// .expect("Failed to create Skia <-> Metal interface");

let dctx = DirectContext::new_gl(Some(interface), None)
.expect("Failed to create Skia's direct context");

let fb_info = {
let mut fboid: gl::GLint = 0;
unsafe {
gl::glGetIntegerv(gl::GL_FRAMEBUFFER_BINDING, &mut fboid);
};

FramebufferInfo {
fboid: fboid.try_into().unwrap(),
format: gl::GL_RGBA8,
}
};

todo!()
//SkiaContext::new(dctx, fb_info, conf.window_width, conf.window_height)
};
// TODO: Skia <-> Metal interface
#[allow(clippy::diverging_sub_expression)]
let skia_ctx = unimplemented!();

// let skia_ctx = SkiaContext::from_metal_loader()
// .expect("Failed to create Skia <-> Metal interface");

let f = payload.f.take().unwrap();
payload.ctx = Some((f(), skia_ctx));
Expand Down
Loading