Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement priority hints for EGL #1700

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -126,6 +127,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);
Comment on lines +134 to +145
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put a match on a separate line please and then just push with as casting.

}

attrs.push(egl::NONE as EGLint);

let shared_context = if let Some(shared_context) =
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably use the notation with not supported we use everywhere else, so say that glx, wgl, and cgl is not supported.

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that drivers could confuse things, can we use Option internally here? So when it's None we don't push, but when it's e.g. Some(Medium) it's pushed to ensure the Medium.


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
Loading