autocommit 2022-01-29 00:49:52 CET
This commit is contained in:
parent
b11a36729c
commit
85b019101a
41 changed files with 1322 additions and 61 deletions
|
|
@ -40,7 +40,7 @@ impl EglDevice {
|
|||
}
|
||||
let mut dpy = EglDisplay {
|
||||
exts: DisplayExt::empty(),
|
||||
formats: AHashMap::new(),
|
||||
formats: Rc::new(AHashMap::new()),
|
||||
dev: *self,
|
||||
dpy,
|
||||
};
|
||||
|
|
@ -68,7 +68,7 @@ impl EglDevice {
|
|||
if !dpy.exts.intersects(DisplayExt::KHR_SURFACELESS_CONTEXT) {
|
||||
return Err(RenderError::SurfacelessContext);
|
||||
}
|
||||
dpy.formats = query_formats(dpy.dpy)?;
|
||||
dpy.formats = Rc::new(query_formats(dpy.dpy)?);
|
||||
|
||||
Ok(Rc::new(dpy))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ use std::rc::Rc;
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct EglDisplay {
|
||||
pub exts: DisplayExt,
|
||||
pub formats: AHashMap<u32, &'static Format>,
|
||||
pub formats: Rc<AHashMap<u32, &'static Format>>,
|
||||
pub dev: EglDevice,
|
||||
pub dpy: EGLDisplay,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::drm::drm::Drm;
|
||||
use crate::drm::drm::{DrmDevice};
|
||||
use crate::render::egl::device::EglDevice;
|
||||
use crate::render::egl::sys::{
|
||||
eglBindAPI, EGLAttrib, EGLLabelKHR, EGLenum, EGLint, EGL_DEBUG_MSG_CRITICAL_KHR,
|
||||
|
|
@ -63,8 +63,7 @@ pub fn init() -> Result<(), RenderError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn find_drm_device(drm: &Drm) -> Result<Option<EglDevice>, RenderError> {
|
||||
let drm_dev = drm.get_device()?;
|
||||
pub fn find_drm_device(drm_dev: &DrmDevice) -> 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)?;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use uapi::c;
|
|||
pub type EGLint = i32;
|
||||
pub type EGLenum = c::c_uint;
|
||||
pub type EGLBoolean = c::c_uint;
|
||||
#[allow(dead_code)]
|
||||
pub type EGLuint64KHR = u64;
|
||||
pub type EGLAttrib = isize;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ pub type GLenum = c::c_uint;
|
|||
pub type GLfloat = f32;
|
||||
pub type GLint = c::c_int;
|
||||
pub type GLsizei = c::c_int;
|
||||
#[allow(dead_code)]
|
||||
pub type GLubyte = u8;
|
||||
pub type GLuint = c::c_uint;
|
||||
|
||||
|
|
@ -29,6 +30,7 @@ pub const GL_RENDERBUFFER: GLenum = 0x8D41;
|
|||
pub const GL_SCISSOR_TEST: GLenum = 0x0C11;
|
||||
pub const GL_TEXTURE0: GLenum = 0x84C0;
|
||||
pub const GL_TEXTURE_2D: GLenum = 0x0DE1;
|
||||
#[allow(dead_code)]
|
||||
pub const GL_TEXTURE_MAG_FILTER: GLenum = 0x2800;
|
||||
pub const GL_TEXTURE_MIN_FILTER: GLenum = 0x2801;
|
||||
pub const GL_TEXTURE_WRAP_S: GLenum = 0x2802;
|
||||
|
|
@ -54,6 +56,7 @@ extern "C" {
|
|||
renderbuffertarget: GLenum,
|
||||
renderbuffer: GLuint,
|
||||
);
|
||||
#[allow(dead_code)]
|
||||
pub fn glFramebufferTexture2D(
|
||||
target: GLenum,
|
||||
attachment: GLenum,
|
||||
|
|
@ -64,6 +67,7 @@ extern "C" {
|
|||
pub fn glCheckFramebufferStatus(target: GLenum) -> GLenum;
|
||||
pub fn glClear(mask: GLbitfield);
|
||||
pub fn glClearColor(red: GLfloat, green: GLfloat, blue: GLfloat, alpha: GLfloat);
|
||||
#[allow(dead_code)]
|
||||
pub fn glFlush();
|
||||
|
||||
pub fn glGenTextures(n: GLsizei, textures: *mut GLuint);
|
||||
|
|
@ -112,6 +116,7 @@ extern "C" {
|
|||
pub fn glGetUniformLocation(prog: GLuint, name: *const GLchar) -> GLint;
|
||||
pub fn glGetAttribLocation(prog: GLuint, name: *const GLchar) -> GLint;
|
||||
pub fn glUniform1i(location: GLint, v0: GLint);
|
||||
#[allow(dead_code)]
|
||||
pub fn glUniform1f(location: GLint, v0: GLfloat);
|
||||
pub fn glUniform4f(location: GLint, v0: GLfloat, v1: GLfloat, v2: GLfloat, v3: GLfloat);
|
||||
pub fn glVertexAttribPointer(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use crate::format::Format;
|
||||
use crate::render::egl::context::EglContext;
|
||||
use crate::render::egl::image::EglImage;
|
||||
use crate::render::egl::PROCS;
|
||||
use crate::render::gl::frame_buffer::GlFrameBuffer;
|
||||
use crate::render::gl::sys::{
|
||||
glBindFramebuffer, glBindTexture, glCheckFramebufferStatus, glDeleteTextures,
|
||||
|
|
@ -8,6 +10,7 @@ use crate::render::gl::sys::{
|
|||
GL_FRAMEBUFFER_COMPLETE, GL_LINEAR, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
||||
GL_TEXTURE_MIN_FILTER, GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T, GL_UNPACK_ROW_LENGTH_EXT,
|
||||
};
|
||||
use crate::render::sys::GLeglImageOES;
|
||||
use crate::render::RenderError;
|
||||
use std::cell::Cell;
|
||||
use std::ptr;
|
||||
|
|
@ -15,12 +18,14 @@ use std::rc::Rc;
|
|||
|
||||
pub struct GlTexture {
|
||||
pub(super) ctx: Rc<EglContext>,
|
||||
pub img: Option<Rc<EglImage>>,
|
||||
pub tex: GLuint,
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
}
|
||||
|
||||
impl GlTexture {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(
|
||||
ctx: &Rc<EglContext>,
|
||||
format: &'static Format,
|
||||
|
|
@ -49,12 +54,14 @@ impl GlTexture {
|
|||
})?;
|
||||
Ok(Rc::new(GlTexture {
|
||||
ctx: ctx.clone(),
|
||||
img: None,
|
||||
tex,
|
||||
width,
|
||||
height,
|
||||
}))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub unsafe fn to_framebuffer(self: &Rc<Self>) -> Result<Rc<GlFrameBuffer>, RenderError> {
|
||||
self.ctx.with_current(|| unsafe {
|
||||
let mut fbo = 0;
|
||||
|
|
@ -84,7 +91,27 @@ impl GlTexture {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn import_texture(
|
||||
pub fn import_img(ctx: &Rc<EglContext>, img: &Rc<EglImage>) -> Result<GlTexture, RenderError> {
|
||||
let tex = ctx.with_current(|| unsafe {
|
||||
let mut tex = 0;
|
||||
glGenTextures(1, &mut tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
PROCS.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, GLeglImageOES(img.img.0));
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
Ok(tex)
|
||||
})?;
|
||||
Ok(GlTexture {
|
||||
ctx: ctx.clone(),
|
||||
img: Some(img.clone()),
|
||||
tex,
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn import_shm(
|
||||
ctx: &Rc<EglContext>,
|
||||
data: &[Cell<u8>],
|
||||
format: &'static Format,
|
||||
|
|
@ -119,6 +146,7 @@ impl GlTexture {
|
|||
})?;
|
||||
Ok(GlTexture {
|
||||
ctx: ctx.clone(),
|
||||
img: None,
|
||||
tex,
|
||||
width,
|
||||
height,
|
||||
|
|
@ -128,11 +156,9 @@ impl GlTexture {
|
|||
|
||||
impl Drop for GlTexture {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
self.ctx.with_current(|| {
|
||||
glDeleteTextures(1, &self.tex);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
let _ = self.ctx.with_current(|| unsafe {
|
||||
glDeleteTextures(1, &self.tex);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ pub enum RenderError {
|
|||
#[error("EGL display does not support `EGL_EXT_image_dma_buf_import_modifiers`")]
|
||||
DmaBufImport,
|
||||
#[error("GLES driver does not support `GL_OES_EGL_image`")]
|
||||
#[allow(dead_code)]
|
||||
OesEglImage,
|
||||
#[error("EGL display does not support `EGL_KHR_image_base`")]
|
||||
ImageBase,
|
||||
|
|
@ -89,4 +90,6 @@ pub enum RenderError {
|
|||
UnknownDrmDevice,
|
||||
#[error("The GLES driver does not support the XRGB8888 format")]
|
||||
XRGB888,
|
||||
#[error("The DRM device does not have a render node")]
|
||||
NoRenderNode,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
#![allow(non_snake_case)]
|
||||
#![allow(non_snake_case, dead_code)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/egl_procs.rs"));
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::drm::dma::DmaBuf;
|
||||
use crate::drm::drm::Drm;
|
||||
use crate::drm::drm::{Drm, DRM_NODE_RENDER};
|
||||
use crate::format::{Format, XRGB8888};
|
||||
use crate::render::egl::context::EglContext;
|
||||
use crate::render::egl::find_drm_device;
|
||||
|
|
@ -8,16 +8,21 @@ use crate::render::gl::render_buffer::GlRenderBuffer;
|
|||
use crate::render::gl::sys::GLint;
|
||||
use crate::render::gl::texture::GlTexture;
|
||||
use crate::render::renderer::framebuffer::Framebuffer;
|
||||
use crate::render::renderer::image::Image;
|
||||
use crate::render::renderer::RENDERDOC;
|
||||
use crate::render::{RenderError, Texture};
|
||||
use ahash::AHashMap;
|
||||
use renderdoc::{RenderDoc, V100};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::ffi::CString;
|
||||
use std::rc::Rc;
|
||||
use uapi::ustr;
|
||||
|
||||
pub struct RenderContext {
|
||||
pub(super) ctx: Rc<EglContext>,
|
||||
|
||||
pub(super) render_node: Rc<CString>,
|
||||
|
||||
pub(super) renderdoc: Option<RefCell<RenderDoc<V100>>>,
|
||||
|
||||
pub(super) tex_prog: GlProgram,
|
||||
|
|
@ -32,7 +37,12 @@ pub struct RenderContext {
|
|||
|
||||
impl RenderContext {
|
||||
pub fn from_drm_device(drm: &Drm) -> Result<Self, RenderError> {
|
||||
let egl_dev = match find_drm_device(&drm)? {
|
||||
let drm_dev = drm.get_device()?;
|
||||
let node = match drm_dev.nodes().find(|(ty, _)| *ty == DRM_NODE_RENDER) {
|
||||
None => return Err(RenderError::NoRenderNode),
|
||||
Some((_, n)) => Rc::new(n.to_owned()),
|
||||
};
|
||||
let egl_dev = match find_drm_device(&drm_dev)? {
|
||||
Some(d) => d,
|
||||
None => return Err(RenderError::UnknownDrmDevice),
|
||||
};
|
||||
|
|
@ -41,10 +51,10 @@ impl RenderContext {
|
|||
return Err(RenderError::XRGB888);
|
||||
}
|
||||
let ctx = dpy.create_context()?;
|
||||
ctx.with_current(|| unsafe { Self::new(&ctx) })
|
||||
ctx.with_current(|| unsafe { Self::new(&ctx, &node) })
|
||||
}
|
||||
|
||||
unsafe fn new(ctx: &Rc<EglContext>) -> Result<Self, RenderError> {
|
||||
unsafe fn new(ctx: &Rc<EglContext>, node: &Rc<CString>) -> Result<Self, RenderError> {
|
||||
let tex_prog = GlProgram::from_shaders(
|
||||
ctx,
|
||||
include_str!("../shaders/tex.vert.glsl"),
|
||||
|
|
@ -58,6 +68,8 @@ impl RenderContext {
|
|||
Ok(Self {
|
||||
ctx: ctx.clone(),
|
||||
|
||||
render_node: node.clone(),
|
||||
|
||||
tex_prog_pos: tex_prog.get_attrib_location(ustr!("pos")),
|
||||
tex_prog_texcoord: tex_prog.get_attrib_location(ustr!("texcoord")),
|
||||
tex_prog_tex: tex_prog.get_uniform_location(ustr!("tex")),
|
||||
|
|
@ -75,6 +87,14 @@ impl RenderContext {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn render_node(&self) -> Rc<CString> {
|
||||
self.render_node.clone()
|
||||
}
|
||||
|
||||
pub fn formats(&self) -> Rc<AHashMap<u32, &'static Format>> {
|
||||
self.ctx.dpy.formats.clone()
|
||||
}
|
||||
|
||||
pub fn dmabuf_fb(self: &Rc<Self>, buf: &DmaBuf) -> Result<Rc<Framebuffer>, RenderError> {
|
||||
self.ctx.with_current(|| unsafe {
|
||||
let img = self.ctx.dpy.import_dmabuf(buf)?;
|
||||
|
|
@ -87,6 +107,16 @@ impl RenderContext {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn dmabuf_img(self: &Rc<Self>, buf: &DmaBuf) -> Result<Rc<Image>, RenderError> {
|
||||
self.ctx.with_current(|| {
|
||||
let img = self.ctx.dpy.import_dmabuf(buf)?;
|
||||
Ok(Rc::new(Image {
|
||||
ctx: self.clone(),
|
||||
gl: img,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn shmem_texture(
|
||||
self: &Rc<Self>,
|
||||
data: &[Cell<u8>],
|
||||
|
|
@ -95,7 +125,7 @@ impl RenderContext {
|
|||
height: i32,
|
||||
stride: i32,
|
||||
) -> Result<Rc<Texture>, RenderError> {
|
||||
let gl = GlTexture::import_texture(&self.ctx, data, format, width, height, stride)?;
|
||||
let gl = GlTexture::import_shm(&self.ctx, data, format, width, height, stride)?;
|
||||
Ok(Rc::new(Texture {
|
||||
ctx: self.clone(),
|
||||
gl,
|
||||
|
|
|
|||
27
src/render/renderer/image.rs
Normal file
27
src/render/renderer/image.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use crate::render::egl::image::EglImage;
|
||||
use crate::render::gl::texture::GlTexture;
|
||||
use crate::render::{RenderContext, Texture};
|
||||
use crate::RenderError;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct Image {
|
||||
pub(super) ctx: Rc<RenderContext>,
|
||||
pub(super) gl: Rc<EglImage>,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
pub fn width(&self) -> i32 {
|
||||
self.gl.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> i32 {
|
||||
self.gl.height
|
||||
}
|
||||
|
||||
pub fn to_texture(self: &Rc<Self>) -> Result<Rc<Texture>, RenderError> {
|
||||
Ok(Rc::new(Texture {
|
||||
ctx: self.ctx.clone(),
|
||||
gl: GlTexture::import_img(&self.ctx.ctx, &self.gl)?,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
pub use context::*;
|
||||
pub use framebuffer::*;
|
||||
pub use image::*;
|
||||
pub use renderer::*;
|
||||
pub use texture::*;
|
||||
|
||||
mod context;
|
||||
mod framebuffer;
|
||||
mod image;
|
||||
mod renderer;
|
||||
mod texture;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue