Skip to content

Commit

Permalink
renderer: Introduce multigpu module
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakulix committed Mar 1, 2022
1 parent feecb91 commit e9493f7
Show file tree
Hide file tree
Showing 4 changed files with 1,888 additions and 17 deletions.
32 changes: 23 additions & 9 deletions src/backend/renderer/gles2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Drop for Gles2Target {
pub struct Gles2Renderer {
buffers: Vec<Gles2Buffer>,
target: Option<Gles2Target>,
extensions: Vec<String>,
pub(crate) extensions: Vec<String>,
tex_programs: [Gles2TexProgram; shaders::FRAGMENT_COUNT],
solid_program: Gles2SolidProgram,
#[cfg(feature = "wayland_frontend")]
Expand Down Expand Up @@ -676,7 +676,7 @@ impl Gles2Renderer {
Ok(renderer)
}

fn make_current(&self) -> Result<(), MakeCurrentError> {
pub(crate) fn make_current(&self) -> Result<(), MakeCurrentError> {
unsafe {
if let Some(&Gles2Target::Surface(ref surface)) = self.target.as_ref() {
self.egl.make_current_with_surface(&**surface)?;
Expand Down Expand Up @@ -786,20 +786,26 @@ impl ImportMem for Gles2Renderer {
// this is guaranteed a non-public internal type, so we are good.
surface
.and_then(|surface| surface.data_map.get::<Rc<Gles2TextureInternal>>().cloned())
.filter(|texture| texture.size == (width, height).into())
.unwrap_or_else(|| {
let mut tex = 0;
unsafe { self.gl.GenTextures(1, &mut tex) };
// new texture, upload in full
upload_full = true;
Rc::new(Gles2TextureInternal {
let new = Rc::new(Gles2TextureInternal {
texture: tex,
texture_kind: shader_idx,
is_external: false,
y_inverted: false,
size: (width, height).into(),
egl_images: None,
destruction_callback_sender: self.destruction_callback_sender.clone(),
})
});
if let Some(surface) = surface {
let copy = new.clone();
surface.data_map.insert_if_missing(|| copy);
}
new
}),
);

Expand Down Expand Up @@ -975,7 +981,12 @@ impl ImportEgl for Gles2Renderer {
self.egl_reader.as_ref()
}

fn import_egl_buffer(&mut self, buffer: &wl_buffer::WlBuffer) -> Result<Gles2Texture, Gles2Error> {
fn import_egl_buffer(
&mut self,
buffer: &wl_buffer::WlBuffer,
_surface: Option<&crate::wayland::compositor::SurfaceData>,
_damage: &[Rectangle<i32, Buffer>],
) -> Result<Gles2Texture, Gles2Error> {
if !self.extensions.iter().any(|ext| ext == "GL_OES_EGL_image") {
return Err(Gles2Error::GLExtensionNotSupported(&["GL_OES_EGL_image"]));
}
Expand Down Expand Up @@ -1023,7 +1034,11 @@ impl ImportEgl for Gles2Renderer {

#[cfg(feature = "wayland_frontend")]
impl ImportDma for Gles2Renderer {
fn import_dmabuf(&mut self, buffer: &Dmabuf) -> Result<Gles2Texture, Gles2Error> {
fn import_dmabuf(
&mut self,
buffer: &Dmabuf,
_damage: Option<&[Rectangle<i32, Buffer>]>,
) -> Result<Gles2Texture, Gles2Error> {
use crate::backend::allocator::Buffer;
if !self.extensions.iter().any(|ext| ext == "GL_OES_EGL_image") {
return Err(Gles2Error::GLExtensionNotSupported(&["GL_OES_EGL_image"]));
Expand All @@ -1049,7 +1064,6 @@ impl ImportDma for Gles2Renderer {
egl_images: Some(vec![image]),
destruction_callback_sender: self.destruction_callback_sender.clone(),
}));
self.egl.unbind()?;
self.dmabuf_cache.insert(buffer.weak(), texture.clone());
Ok(texture)
})
Expand Down Expand Up @@ -1120,6 +1134,8 @@ impl Gles2Renderer {
}

impl ExportMem for Gles2Renderer {
type TextureMapping = Gles2Mapping;

fn copy_framebuffer(
&mut self,
region: Rectangle<i32, Buffer>,
Expand Down Expand Up @@ -1158,7 +1174,6 @@ impl ExportMem for Gles2Renderer {
texture: &Self::TextureId,
region: Rectangle<i32, Buffer>,
) -> Result<Self::TextureMapping, Self::Error> {
let size = texture.size();
let mut pbo = 0;
let old_target = self.target.take();
self.bind(texture.clone())?;
Expand Down Expand Up @@ -1644,7 +1659,6 @@ impl Gles2Renderer {
impl Renderer for Gles2Renderer {
type Error = Gles2Error;
type TextureId = Gles2Texture;
type TextureMapping = Gles2Mapping;
type Frame = Gles2Frame;

fn downscale_filter(&mut self, filter: TextureFilter) -> Result<(), Self::Error> {
Expand Down
26 changes: 18 additions & 8 deletions src/backend/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use crate::backend::egl::{
Error as EglError,
};

pub mod multigpu;

#[cfg(feature = "wayland_frontend")]
pub mod utils;

Expand Down Expand Up @@ -190,9 +192,8 @@ pub trait Renderer {
type Error: Error;
/// Texture Handle type used by this renderer.
type TextureId: Texture;
/// Texture type representing a downloaded pixel buffer.
type TextureMapping: TextureMapping;
/// Type representing a currently in-progress frame during the [`Renderer::render`]-call
#[cfg(not(feature = "nightly"))]
type Frame: Frame<Error = Self::Error, TextureId = Self::TextureId>;

/// Set the filter method to be used when rendering a texture into a smaller area than its size
Expand Down Expand Up @@ -355,6 +356,8 @@ pub trait ImportEgl: Renderer {
fn import_egl_buffer(
&mut self,
buffer: &wl_buffer::WlBuffer,
surface: Option<&crate::wayland::compositor::SurfaceData>,
damage: &[Rectangle<i32, Buffer>],
) -> Result<<Self as Renderer>::TextureId, <Self as Renderer>::Error>;
}

Expand All @@ -380,13 +383,16 @@ pub trait ImportDma: Renderer {
fn import_dma_buffer(
&mut self,
buffer: &wl_buffer::WlBuffer,
surface: Option<&crate::wayland::compositor::SurfaceData>,
damage: &[Rectangle<i32, Buffer>],
) -> Result<<Self as Renderer>::TextureId, <Self as Renderer>::Error> {
let _ = surface;
let dmabuf = buffer
.as_ref()
.user_data()
.get::<Dmabuf>()
.expect("import_dma_buffer without checking buffer type?");
self.import_dmabuf(dmabuf)
self.import_dmabuf(dmabuf, Some(damage))
}

/// Import a given raw dmabuf into the renderer.
Expand All @@ -403,6 +409,7 @@ pub trait ImportDma: Renderer {
fn import_dmabuf(
&mut self,
dmabuf: &Dmabuf,
damage: Option<&[Rectangle<i32, Buffer>]>,
) -> Result<<Self as Renderer>::TextureId, <Self as Renderer>::Error>;
}

Expand Down Expand Up @@ -453,8 +460,8 @@ impl<R: Renderer + ImportMem + ImportEgl + ImportDma> ImportAll for R {
) -> Option<Result<<Self as Renderer>::TextureId, <Self as Renderer>::Error>> {
match buffer_type(buffer) {
Some(BufferType::Shm) => Some(self.import_shm_buffer(buffer, surface, damage)),
Some(BufferType::Egl) => Some(self.import_egl_buffer(buffer)),
Some(BufferType::Dma) => Some(self.import_dma_buffer(buffer)),
Some(BufferType::Egl) => Some(self.import_egl_buffer(buffer, surface, damage)),
Some(BufferType::Dma) => Some(self.import_dma_buffer(buffer, surface, damage)),
_ => None,
}
}
Expand All @@ -473,14 +480,17 @@ impl<R: Renderer + ImportMem + ImportDma> ImportAll for R {
) -> Option<Result<<Self as Renderer>::TextureId, <Self as Renderer>::Error>> {
match buffer_type(buffer) {
Some(BufferType::Shm) => Some(self.import_shm_buffer(buffer, surface, damage)),
Some(BufferType::Dma) => Some(self.import_dma_buffer(buffer)),
Some(BufferType::Dma) => Some(self.import_dma_buffer(buffer, surface, damage)),
_ => None,
}
}
}

/// Trait for renderers supporting exporting contents of framebuffers or textures into memory.
pub trait ExportMem: Renderer {
/// Texture type representing a downloaded pixel buffer.
type TextureMapping: TextureMapping;

/// Copies the contents of the currently bound framebuffer.
///
/// This operation is not destructive, the contexts of the framebuffer keep being valid.
Expand All @@ -492,7 +502,7 @@ pub trait ExportMem: Renderer {
fn copy_framebuffer(
&mut self,
region: Rectangle<i32, Buffer>,
) -> Result<<Self as Renderer>::TextureMapping, <Self as Renderer>::Error>;
) -> Result<Self::TextureMapping, <Self as Renderer>::Error>;
/// Copies the contents of the currently bound framebuffer.
/// *Note*: This function may change or invalidate the current bind.
///
Expand All @@ -505,7 +515,7 @@ pub trait ExportMem: Renderer {
&mut self,
texture: &Self::TextureId,
region: Rectangle<i32, Buffer>,
) -> Result<<Self as Renderer>::TextureMapping, Self::Error>;
) -> Result<Self::TextureMapping, Self::Error>;
/// Returns a read-only pointer to a previously created texture mapping.
///
/// The format of the returned slice is RGBA8.
Expand Down
Loading

0 comments on commit e9493f7

Please sign in to comment.