1
0
Fork 0
forked from wry/wry

autocommit 2022-04-04 23:09:39 CEST

This commit is contained in:
Julian Orth 2022-04-04 23:09:39 +02:00
parent e897d271af
commit 5f79aab15f
21 changed files with 870 additions and 731 deletions

View file

@ -1,19 +1,15 @@
use crate::drm::drm::NodeType;
use crate::render::egl::device::EglDevice;
use crate::render::egl::sys::{
eglBindAPI, EGLAttrib, EGLLabelKHR, EGLenum, EGLint, EGL_DEBUG_MSG_CRITICAL_KHR,
EGL_DEBUG_MSG_ERROR_KHR, EGL_DEBUG_MSG_INFO_KHR, EGL_DEBUG_MSG_WARN_KHR,
EGL_DRM_DEVICE_FILE_EXT, EGL_NONE, EGL_OPENGL_ES_API, EGL_TRUE,
EGL_DEBUG_MSG_ERROR_KHR, EGL_DEBUG_MSG_INFO_KHR, EGL_DEBUG_MSG_WARN_KHR, EGL_NONE,
EGL_OPENGL_ES_API, EGL_TRUE,
};
use crate::render::ext::{get_client_ext, get_device_ext, ClientExt, DeviceExt};
use crate::render::ext::{get_client_ext, ClientExt};
use crate::render::proc::ExtProc;
use crate::render::RenderError;
use ahash::AHashMap;
use bstr::ByteSlice;
use log::Level;
use once_cell::sync::Lazy;
use std::ffi::{CStr, CString};
use std::ptr;
use std::ffi::CStr;
use sys::{
EGL_BAD_ACCESS, EGL_BAD_ALLOC, EGL_BAD_ATTRIBUTE, EGL_BAD_CONFIG, EGL_BAD_CONTEXT,
EGL_BAD_CURRENT_SURFACE, EGL_BAD_DEVICE_EXT, EGL_BAD_DISPLAY, EGL_BAD_MATCH,
@ -23,7 +19,6 @@ use sys::{
use uapi::c;
pub mod context;
pub mod device;
pub mod display;
pub mod image;
pub mod sys;
@ -36,14 +31,8 @@ pub fn init() -> Result<(), RenderError> {
if !EXTS.contains(ClientExt::EXT_PLATFORM_BASE) {
return Err(RenderError::ExtPlatformBase);
}
if !EXTS.device_query() {
return Err(RenderError::DeviceQuery);
}
if !EXTS.device_enumeration() {
return Err(RenderError::DeviceEnumeration);
}
if !EXTS.platform_gbm() {
return Err(RenderError::DeviceEnumeration);
if !EXTS.contains(ClientExt::KHR_PLATFORM_GBM) {
return Err(RenderError::GbmExt);
}
if EXTS.contains(ClientExt::KHR_DEBUG) {
let attrib: &[EGLAttrib] = &[
@ -67,49 +56,6 @@ pub fn init() -> Result<(), RenderError> {
Ok(())
}
pub fn find_drm_device(
drm_dev: &AHashMap<NodeType, CString>,
) -> Result<Option<EglDevice>, RenderError> {
for device in query_devices()? {
if device.exts.contains(DeviceExt::EXT_DEVICE_DRM) {
let device_file = device.query_string(EGL_DRM_DEVICE_FILE_EXT)?;
for name in drm_dev.values() {
if device_file == &**name {
return Ok(Some(device));
}
}
}
}
Ok(None)
}
pub fn query_devices() -> Result<Vec<EglDevice>, RenderError> {
if !EXTS.device_enumeration() {
return Err(RenderError::DeviceEnumeration);
}
unsafe {
let mut devices = vec![];
let mut num_devices = 0;
let res = PROCS.eglQueryDevicesEXT(num_devices, ptr::null_mut(), &mut num_devices);
if res != EGL_TRUE {
return Err(RenderError::QueryDevices);
}
devices.reserve_exact(num_devices as usize);
let res = PROCS.eglQueryDevicesEXT(num_devices, devices.as_mut_ptr(), &mut num_devices);
if res != EGL_TRUE {
return Err(RenderError::QueryDevices);
}
devices.set_len(num_devices as usize);
Ok(devices
.into_iter()
.map(|d| EglDevice {
exts: get_device_ext(d),
dev: d,
})
.collect())
}
}
unsafe extern "C" fn egl_log(
error: EGLenum,
command: *const c::c_char,

View file

@ -1,100 +0,0 @@
use crate::format::{formats, Format};
use crate::render::egl::display::EglDisplay;
use crate::render::egl::sys::{
eglInitialize, EGLDeviceEXT, EGLDisplay, EGLint, EGL_PLATFORM_DEVICE_EXT, EGL_TRUE,
};
use crate::render::egl::PROCS;
use crate::render::ext::{get_display_ext, DeviceExt, DisplayExt};
use crate::render::RenderError;
use ahash::AHashMap;
use std::ffi::CStr;
use std::ptr;
use std::rc::Rc;
#[derive(Debug, Copy, Clone)]
pub struct EglDevice {
pub exts: DeviceExt,
pub dev: EGLDeviceEXT,
}
impl EglDevice {
pub fn query_string(&self, name: EGLint) -> Result<&'static CStr, RenderError> {
unsafe {
let res = PROCS.eglQueryDeviceStringEXT(self.dev, name);
if res.is_null() {
return Err(RenderError::DeviceQueryString);
}
Ok(CStr::from_ptr(res))
}
}
pub fn create_display(&self) -> Result<Rc<EglDisplay>, RenderError> {
unsafe {
let dpy = PROCS.eglGetPlatformDisplayEXT(
EGL_PLATFORM_DEVICE_EXT as _,
self.dev.0,
ptr::null(),
);
if dpy.is_none() {
return Err(RenderError::GetDisplay);
}
let mut dpy = EglDisplay {
exts: DisplayExt::empty(),
formats: Rc::new(AHashMap::new()),
dev: Some(*self),
gbm: None,
dpy,
};
let mut major = 0;
let mut minor = 0;
if eglInitialize(dpy.dpy, &mut major, &mut minor) != EGL_TRUE {
return Err(RenderError::Initialize);
}
dpy.exts = get_display_ext(dpy.dpy);
if !dpy.exts.intersects(DisplayExt::KHR_IMAGE_BASE) {
return Err(RenderError::ImageBase);
}
if !dpy
.exts
.intersects(DisplayExt::EXT_IMAGE_DMA_BUF_IMPORT_MODIFIERS)
{
return Err(RenderError::DmaBufImport);
}
if !dpy
.exts
.intersects(DisplayExt::KHR_NO_CONFIG_CONTEXT | DisplayExt::MESA_CONFIGLESS_CONTEXT)
{
return Err(RenderError::ConfiglessContext);
}
if !dpy.exts.intersects(DisplayExt::KHR_SURFACELESS_CONTEXT) {
return Err(RenderError::SurfacelessContext);
}
dpy.formats = Rc::new(query_formats(dpy.dpy)?);
Ok(Rc::new(dpy))
}
}
}
unsafe fn query_formats(dpy: EGLDisplay) -> Result<AHashMap<u32, &'static Format>, RenderError> {
let mut vec = vec![];
let mut num = 0;
let res = PROCS.eglQueryDmaBufFormatsEXT(dpy, num, ptr::null_mut(), &mut num);
if res != EGL_TRUE {
return Err(RenderError::QueryDmaBufFormats);
}
vec.reserve_exact(num as usize);
let res = PROCS.eglQueryDmaBufFormatsEXT(dpy, num, vec.as_mut_ptr(), &mut num);
if res != EGL_TRUE {
return Err(RenderError::QueryDmaBufFormats);
}
vec.set_len(num as usize);
let mut res = AHashMap::new();
let formats = formats();
for fmt in vec {
if let Some(format) = formats.get(&(fmt as u32)) {
res.insert(format.drm, *format);
}
}
Ok(res)
}

View file

@ -1,9 +1,9 @@
use std::ptr;
use crate::drm::dma::DmaBuf;
use crate::drm::drm::Drm;
use crate::drm::gbm::GbmDevice;
use crate::drm::INVALID_MODIFIER;
use crate::format::{Format, formats};
use crate::format::{formats, Format};
use crate::render::egl::context::EglContext;
use crate::render::egl::device::EglDevice;
use crate::render::egl::image::EglImage;
use crate::render::egl::sys::{
eglCreateContext, eglTerminate, EGLClientBuffer, EGLConfig, EGLContext, EGLDisplay, EGLint,
@ -19,20 +19,18 @@ use crate::render::egl::sys::{
EGL_LINUX_DRM_FOURCC_EXT, EGL_NONE, EGL_TRUE, EGL_WIDTH,
};
use crate::render::egl::PROCS;
use crate::render::ext::{get_gl_ext, DisplayExt, GlExt, get_display_ext};
use crate::render::ext::{get_display_ext, get_gl_ext, DisplayExt, GlExt};
use crate::render::sys::{eglInitialize, EGL_PLATFORM_GBM_KHR};
use crate::render::RenderError;
use ahash::AHashMap;
use std::ptr;
use std::rc::Rc;
use crate::drm::drm::Drm;
use crate::drm::gbm::GbmDevice;
use crate::render::sys::{EGL_PLATFORM_GBM_KHR, eglInitialize};
#[derive(Debug)]
pub struct EglDisplay {
pub exts: DisplayExt,
pub formats: Rc<AHashMap<u32, &'static Format>>,
pub dev: Option<EglDevice>,
pub gbm: Option<GbmDevice>,
pub gbm: GbmDevice,
pub dpy: EGLDisplay,
}
@ -54,8 +52,7 @@ impl EglDisplay {
let mut dpy = EglDisplay {
exts: DisplayExt::empty(),
formats: Rc::new(AHashMap::new()),
dev: None,
gbm: Some(gbm),
gbm,
dpy,
};
let mut major = 0;

View file

@ -14,7 +14,6 @@ egl_transparent!(EGLImageKHR);
egl_transparent!(EGLContext);
egl_transparent!(EGLClientBuffer);
egl_transparent!(EGLLabelKHR);
egl_transparent!(EGLDeviceEXT);
pub type EGLDEBUGPROCKHR = unsafe extern "C" fn(
error: EGLenum,
@ -50,8 +49,6 @@ pub const EGL_BAD_SURFACE: EGLint = 0x300D;
pub const EGL_CONTEXT_LOST: EGLint = 0x300E;
pub const EGL_BAD_DEVICE_EXT: EGLint = 0x322B;
pub const EGL_OPENGL_ES_API: EGLenum = 0x30A0;
pub const EGL_DRM_DEVICE_FILE_EXT: EGLint = 0x3233;
pub const EGL_PLATFORM_DEVICE_EXT: EGLint = 0x313F;
pub const EGL_PLATFORM_GBM_KHR: EGLint = 0x31D7;
pub const EGL_CONTEXT_CLIENT_VERSION: EGLint = 0x3098;

View file

@ -1,5 +1,4 @@
use crate::render::egl::sys::{eglQueryString, EGLDeviceEXT, EGLDisplay, EGL_EXTENSIONS};
use crate::render::egl::PROCS;
use crate::render::egl::sys::{eglQueryString, EGLDisplay, EGL_EXTENSIONS};
use crate::render::gl::sys::{glGetString, GL_EXTENSIONS};
use crate::utils::trim::AsciiTrim;
use ahash::AHashSet;
@ -48,25 +47,7 @@ bitflags::bitflags! {
const EXT_CLIENT_EXTENSION = 1 << 0;
const EXT_PLATFORM_BASE = 1 << 1;
const KHR_PLATFORM_GBM = 1 << 2;
const EXT_PLATFORM_DEVICE = 1 << 3;
const EXT_DEVICE_BASE = 1 << 4;
const EXT_DEVICE_ENUMERATION = 1 << 5;
const EXT_DEVICE_QUERY = 1 << 6;
const KHR_DEBUG = 1 << 7;
}
}
impl ClientExt {
pub fn device_enumeration(self) -> bool {
self.contains(Self::EXT_DEVICE_BASE | Self::EXT_DEVICE_ENUMERATION)
}
pub fn device_query(self) -> bool {
self.contains(Self::EXT_DEVICE_BASE | Self::EXT_DEVICE_QUERY)
}
pub fn platform_gbm(self) -> bool {
self.contains(Self::KHR_PLATFORM_GBM)
const KHR_DEBUG = 1 << 3;
}
}
@ -74,13 +55,6 @@ pub fn get_client_ext() -> ClientExt {
let map = [
("EGL_EXT_platform_base", ClientExt::EXT_PLATFORM_BASE),
("EGL_KHR_platform_gbm", ClientExt::KHR_PLATFORM_GBM),
("EGL_EXT_platform_device", ClientExt::EXT_PLATFORM_DEVICE),
("EGL_EXT_device_base", ClientExt::EXT_DEVICE_BASE),
(
"EGL_EXT_device_enumeration",
ClientExt::EXT_DEVICE_ENUMERATION,
),
("EGL_EXT_device_query", ClientExt::EXT_DEVICE_QUERY),
("EGL_KHR_debug", ClientExt::KHR_DEBUG),
];
match unsafe { get_dpy_extensions(EGLDisplay::none()) } {
@ -132,35 +106,6 @@ pub(super) unsafe fn get_display_ext(dpy: EGLDisplay) -> DisplayExt {
}
}
bitflags::bitflags! {
pub struct DeviceExt: u32 {
const MESA_DEVICE_SOFTWARE = 1 << 0;
const EXT_DEVICE_PERSISTENT_ID = 1 << 1;
const EXT_DEVICE_DRM = 1 << 2;
const EXT_DEVICE_DRM_RENDER_NODE = 1 << 3;
}
}
pub(super) unsafe fn get_device_ext(dev: EGLDeviceEXT) -> DeviceExt {
let map = [
("EGL_MESA_device_software", DeviceExt::MESA_DEVICE_SOFTWARE),
(
"EGL_EXT_device_persistent_id",
DeviceExt::EXT_DEVICE_PERSISTENT_ID,
),
("EGL_EXT_device_drm", DeviceExt::EXT_DEVICE_DRM),
(
"EGL_EXT_device_drm_render_node",
DeviceExt::EXT_DEVICE_DRM_RENDER_NODE,
),
];
let ext = PROCS.eglQueryDeviceStringEXT(dev, EGL_EXTENSIONS);
match get_extensions(ext) {
Some(exts) => get_typed_ext(&exts, DeviceExt::empty(), &map),
_ => DeviceExt::empty(),
}
}
bitflags::bitflags! {
pub struct GlExt: u32 {
const GL_OES_EGL_IMAGE = 1 << 0;

View file

@ -2,7 +2,7 @@ use crate::drm::dma::DmaBuf;
use crate::drm::drm::{Drm, NodeType};
use crate::format::{Format, XRGB8888};
use crate::render::egl::context::EglContext;
use crate::render::egl::find_drm_device;
use crate::render::egl::display::EglDisplay;
use crate::render::gl::program::GlProgram;
use crate::render::gl::render_buffer::GlRenderBuffer;
use crate::render::gl::sys::GLint;
@ -16,7 +16,6 @@ use std::ffi::CString;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
use uapi::ustr;
use crate::render::egl::display::EglDisplay;
pub(super) struct TexProg {
pub(super) prog: GlProgram,