Skip to content

Commit

Permalink
Implement priority hints for EGL
Browse files Browse the repository at this point in the history
  • Loading branch information
valaphee committed Sep 15, 2024
1 parent 1b30cd7 commit 656ab7f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
21 changes: 20 additions & 1 deletion glutin/src/api/egl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use glutin_egl_sys::{egl, EGLContext};

use crate::config::{Api, GetGlConfig};
use crate::context::{
self, AsRawContext, ContextApi, ContextAttributes, GlProfile, RawContext, Robustness, Version,
self, AsRawContext, ContextApi, ContextAttributes, GlProfile, Priority, RawContext, Robustness,
Version,
};
use crate::display::{DisplayFeatures, GetGlDisplay};
use crate::error::{ErrorKind, Result};
Expand Down Expand Up @@ -54,6 +55,24 @@ impl Display {
},
};

if context_attributes.priority != Priority::Medium
&& self.inner.display_extensions.contains("EGL_IMG_context_priority")
{
attrs.push(egl::CONTEXT_PRIORITY_LEVEL_IMG as EGLint);
attrs.push(match context_attributes.priority {
Priority::Low => egl::CONTEXT_PRIORITY_LOW_IMG,
Priority::High => egl::CONTEXT_PRIORITY_HIGH_IMG,
Priority::Realtime => {
if self.inner.display_extensions.contains("EGL_NV_context_priority_realtime") {
egl::CONTEXT_PRIORITY_REALTIME_NV
} else {
egl::CONTEXT_PRIORITY_HIGH_IMG
}
},
_ => unreachable!(),
} as EGLint);
}

let is_one_five = self.inner.version >= Version::new(1, 5);
if is_one_five || self.inner.display_extensions.contains("EGL_KHR_create_context") {
let mut flags = 0;
Expand Down
31 changes: 31 additions & 0 deletions glutin/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ impl ContextAttributesBuilder {
self
}

/// Sets the priority hint, which might not be honored if the API does not support it,
/// if there are constraints on the number of high priority contexts available in the
/// system, or system policy limits access to high priority contexts to
/// appropriate system privilege level
///
/// # Api-specific
///
/// - Only EGL at the moment implements context priorities.
pub fn with_priority(mut self, priority: Priority) -> Self {
self.attributes.priority = priority;
self
}

/// Build the context attributes.
///
/// The `raw_window_handle` isn't required and here for WGL compatibility.
Expand Down Expand Up @@ -228,6 +241,8 @@ pub struct ContextAttributes {

pub(crate) api: Option<ContextApi>,

pub(crate) priority: Priority,

pub(crate) shared_context: Option<RawContext>,

pub(crate) raw_window_handle: Option<RawWindowHandle>,
Expand Down Expand Up @@ -608,6 +623,22 @@ pub enum RawContext {
Cgl(*const ffi::c_void),
}

/// Priority hint
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub enum Priority {
/// Lowest priority, contexts using this priority give way for most other
/// contexts.
Low,
/// Default priority.
#[default]
Medium,
/// High priority is usually required for VR applications.
High,
/// Realtime priority contexts are executed immediately and preempt any
/// current context running. But will fallback to high if not supported.
Realtime,
}

/// Pick `GlProfile` and `Version` based on the provided params.
#[cfg(any(egl_backend, glx_backend, wgl_backend))]
pub(crate) fn pick_profile(
Expand Down
2 changes: 2 additions & 0 deletions glutin_egl_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn main() {
"EGL_EXT_platform_wayland",
"EGL_EXT_platform_x11",
"EGL_EXT_swap_buffers_with_damage",
"EGL_IMG_context_priority",
"EGL_KHR_create_context",
"EGL_KHR_create_context_no_error",
"EGL_KHR_display_reference",
Expand All @@ -47,6 +48,7 @@ fn main() {
"EGL_KHR_swap_buffers_with_damage",
"EGL_KHR_wait_sync",
"EGL_MESA_platform_gbm",
"EGL_NV_context_priority_realtime",
]);

if target.contains("ios") {
Expand Down

0 comments on commit 656ab7f

Please sign in to comment.