Skip to content

Commit

Permalink
multigpu: allow to specify the egl context priority
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeissl committed Sep 15, 2023
1 parent bd06ffc commit 20ebb70
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/backend/renderer/multigpu/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wayland_server::protocol::wl_buffer;

use crate::backend::{
drm::{CreateDrmNodeError, DrmNode},
egl::{EGLContext, EGLDevice, EGLDisplay, Error as EGLError},
egl::{context::ContextPriority, EGLContext, EGLDevice, EGLDisplay, Error as EGLError},
renderer::{
gles::{GlesError, GlesRenderer},
multigpu::{ApiDevice, Error as MultiError, GraphicsApi},
Expand Down Expand Up @@ -58,6 +58,7 @@ type Factory = Box<dyn Fn(&EGLDisplay) -> Result<GlesRenderer, Error>>;
/// A [`GraphicsApi`] utilizing EGL for device enumeration and OpenGL ES for rendering.
pub struct EglGlesBackend<R> {
factory: Option<Factory>,
context_priority: Option<ContextPriority>,
_renderer: std::marker::PhantomData<R>,
}

Expand All @@ -71,6 +72,7 @@ impl<R> Default for EglGlesBackend<R> {
fn default() -> Self {
EglGlesBackend {
factory: None,
context_priority: None,
_renderer: std::marker::PhantomData,
}
}
Expand All @@ -87,6 +89,18 @@ impl<R> EglGlesBackend<R> {
..Default::default()
}
}

/// Initialize a new [`EglGlesBackend`] with a [`ContextPriority`] for instantiating [`GlesRenderer`]s
///
/// Note: This is mutually exclusive with [`EglGlesBackend::with_factory`](EglGlesBackend::with_factory),
/// but you can create an [`EGLContext`] with a specific [`ContextPriority`] within your factory.
/// See [`EGLContext::new_with_priority`] for more information.
pub fn with_context_priority(priority: ContextPriority) -> Self {
Self {
context_priority: Some(priority),
..Default::default()
}
}
}

impl<R: From<GlesRenderer> + Renderer<Error = GlesError>> GraphicsApi for EglGlesBackend<R> {
Expand All @@ -113,7 +127,9 @@ impl<R: From<GlesRenderer> + Renderer<Error = GlesError>> GraphicsApi for EglGle
let renderer = if let Some(factory) = self.factory.as_ref() {
factory(&display)?.into()
} else {
let context = EGLContext::new(&display).map_err(Error::Egl)?;
let context =
EGLContext::new_with_priority(&display, self.context_priority.unwrap_or_default())
.map_err(Error::Egl)?;
unsafe { GlesRenderer::new(context).map_err(Error::Gl)? }.into()
};

Expand Down
20 changes: 18 additions & 2 deletions src/backend/renderer/multigpu/gbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wayland_server::protocol::wl_buffer;

use crate::backend::{
drm::{CreateDrmNodeError, DrmNode},
egl::{EGLContext, EGLDisplay, Error as EGLError},
egl::{context::ContextPriority, EGLContext, EGLDisplay, Error as EGLError},
renderer::{
gles::{GlesError, GlesRenderer},
multigpu::{ApiDevice, Error as MultiError, GraphicsApi},
Expand Down Expand Up @@ -61,6 +61,7 @@ type Factory = Box<dyn Fn(&EGLDisplay) -> Result<GlesRenderer, Error>>;
pub struct GbmGlesBackend<R> {
devices: HashMap<DrmNode, EGLDisplay>,
factory: Option<Factory>,
context_priority: Option<ContextPriority>,
_renderer: std::marker::PhantomData<R>,
}

Expand All @@ -77,6 +78,7 @@ impl<R> Default for GbmGlesBackend<R> {
GbmGlesBackend {
devices: HashMap::new(),
factory: None,
context_priority: None,
_renderer: std::marker::PhantomData,
}
}
Expand All @@ -94,6 +96,18 @@ impl<R> GbmGlesBackend<R> {
}
}

/// Initialize a new [`GbmGlesBackend`] with a [`ContextPriority`] for instantiating [`GlesRenderer`]s
///
/// Note: This is mutually exclusive with [`GbmGlesBackend::with_factory`](GbmGlesBackend::with_factory),
/// but you can create an [`EGLContext`] with a specific [`ContextPriority`] within your factory.
/// See [`EGLContext::new_with_priority`] for more information.
pub fn with_context_priority(priority: ContextPriority) -> Self {
Self {
context_priority: Some(priority),
..Default::default()
}
}

/// Add a new GBM device for a given node to the api
pub fn add_node<T: AsFd + Send + 'static>(
&mut self,
Expand Down Expand Up @@ -135,7 +149,9 @@ impl<R: From<GlesRenderer> + Renderer<Error = GlesError>> GraphicsApi for GbmGle
let renderer = if let Some(factory) = self.factory.as_ref() {
factory(display)?.into()
} else {
let context = EGLContext::new(display).map_err(Error::Egl)?;
let context =
EGLContext::new_with_priority(display, self.context_priority.unwrap_or_default())
.map_err(Error::Egl)?;
unsafe { GlesRenderer::new(context).map_err(Error::Gl)? }.into()
};

Expand Down

0 comments on commit 20ebb70

Please sign in to comment.