Skip to content

Commit

Permalink
egl: Fallback if EGL_KHR_display_reference not supported
Browse files Browse the repository at this point in the history
libglvnd reports client extensions if at least one vendor supports them.
This might lead to display creation failure when a vendor library reports support but
fails to create a display for another reason (like when the nvidia egl implementation is installed but no nvidia card is available).
To work around this issue retry creation without EGL_KHR_display_reference.
  • Loading branch information
Molytho committed Oct 10, 2023
1 parent 2b77be5 commit 6a03553
Showing 1 changed file with 114 additions and 38 deletions.
152 changes: 114 additions & 38 deletions glutin/src/api/egl/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,52 @@ impl Display {

let mut attrs = Vec::<EGLint>::with_capacity(3);

if extensions.contains("EGL_KHR_display_reference") {
attrs.push(egl::TRACK_REFERENCES_KHR as _);
attrs.push(egl::TRUE as _);
}

// TODO: Some extensions exist like EGL_EXT_device_drm which allow specifying
// which DRM master fd to use under the hood by the implementation. This would
// mean there would need to be an unsafe equivalent to this function.

// Push at the end so we can pop it on failure
let has_display_reference = extensions.contains("EGL_KHR_display_reference");
if has_display_reference {
attrs.push(egl::TRACK_REFERENCES_KHR as _);
attrs.push(egl::TRUE as _);
}

// Push `egl::NONE` to terminate the list.
attrs.push(egl::NONE as EGLint);

let display = Self::check_display_error(unsafe {
egl.GetPlatformDisplayEXT(
egl::PLATFORM_DEVICE_EXT,
device.raw_device() as *mut _,
attrs.as_ptr(),
)
})
// NOTE: This fallback is needed because libglvnd advertises client extensions
// if at least one vendor library supports them. This leads to creation
// failures for the vendor libraries not supporting
// EGL_KHR_display_reference. Also according to the spec creation is allowed
// to fail with EGL_KHR_display_reference set to EGL_TRUE even if
// EGL_KHR_display_reference is advertised in the client extension
// string, so just always try creation without EGL_KHR_display_reference
// if it failed using it.
let mut retry_on_failure = has_display_reference;
let platform_display = loop {
let platform_display = Self::check_display_error(unsafe {
egl.GetPlatformDisplayEXT(
egl::PLATFORM_DEVICE_EXT,
device.raw_device() as *mut _,
attrs.as_ptr(),
)
});

if platform_display.is_err() && retry_on_failure {
attrs.pop();
attrs.pop();
attrs.pop();
attrs.push(egl::NONE as EGLint);
retry_on_failure = false;
continue;
}

break platform_display;
}
.map(EglDisplay::Ext)?;

Self::initialize_display(egl, display, None)
Self::initialize_display(egl, platform_display, None)
}

/// Get the [`Device`] the display is using.
Expand Down Expand Up @@ -244,23 +268,48 @@ impl Display {
},
};

if extensions.contains("EGL_KHR_display_reference") {
attrs.push(egl::TRACK_REFERENCES_KHR as _);
attrs.push(egl::TRUE as _);
}

// Be explicit here.
if display.is_null() {
display = egl::DEFAULT_DISPLAY as *mut _;
}

// Push at the end so we can pop it on failure
let has_display_reference = extensions.contains("EGL_KHR_display_reference");
if has_display_reference {
attrs.push(egl::TRACK_REFERENCES_KHR as _);
attrs.push(egl::TRUE as _);
}

// Push `egl::NONE` to terminate the list.
attrs.push(egl::NONE as EGLAttrib);

let display =
unsafe { egl.GetPlatformDisplay(platform, display as *mut _, attrs.as_ptr()) };
// NOTE: This fallback is needed because libglvnd advertises client extensions
// if at least one vendor library supports them. This leads to creation
// failures for the vendor libraries not supporting
// EGL_KHR_display_reference. Also according to the spec creation is allowed
// to fail with EGL_KHR_display_reference set to EGL_TRUE even if
// EGL_KHR_display_reference is advertised in the client extension
// string, so just always try creation without EGL_KHR_display_reference
// if it failed using it.
let mut retry_on_failure = has_display_reference;
let platform_display = loop {
let platform_display = Self::check_display_error(unsafe {
egl.GetPlatformDisplay(platform, display as *mut _, attrs.as_ptr())
});

if platform_display.is_err() && retry_on_failure {
attrs.pop();
attrs.pop();
attrs.pop();
attrs.push(egl::NONE as EGLAttrib);
retry_on_failure = false;
continue;
}

Self::check_display_error(display).map(EglDisplay::Khr)
break platform_display;
};

platform_display.map(EglDisplay::Khr)
}

fn get_platform_display_ext(egl: &Egl, display: RawDisplayHandle) -> Result<EglDisplay> {
Expand Down Expand Up @@ -309,23 +358,48 @@ impl Display {
},
};

if extensions.contains("EGL_KHR_display_reference") {
attrs.push(egl::TRACK_REFERENCES_KHR as _);
attrs.push(egl::TRUE as _);
}

// Be explicit here.
if display.is_null() {
display = egl::DEFAULT_DISPLAY as *mut _;
}

// Push at the end so we can pop it on failure
let has_display_reference = extensions.contains("EGL_KHR_display_reference");
if has_display_reference {
attrs.push(egl::TRACK_REFERENCES_KHR as _);
attrs.push(egl::TRUE as _);
}

// Push `egl::NONE` to terminate the list.
attrs.push(egl::NONE as EGLint);

let display =
unsafe { egl.GetPlatformDisplayEXT(platform, display as *mut _, attrs.as_ptr()) };
// NOTE: This fallback is needed because libglvnd advertises client extensions
// if at least one vendor library supports them. This leads to creation
// failures for the vendor libraries not supporting
// EGL_KHR_display_reference. Also according to the spec creation is allowed
// to fail with EGL_KHR_display_reference set to EGL_TRUE even if
// EGL_KHR_display_reference is advertised in the client extension
// string, so just always try creation without EGL_KHR_display_reference
// if it failed using it.
let mut retry_on_failure = has_display_reference;
let platform_display = loop {
let platform_display = Self::check_display_error(unsafe {
egl.GetPlatformDisplayEXT(platform, display as *mut _, attrs.as_ptr())
});

if platform_display.is_err() && retry_on_failure {
attrs.pop();
attrs.pop();
attrs.pop();
attrs.push(egl::NONE as EGLint);
retry_on_failure = false;
continue;
}

break platform_display;
};

Self::check_display_error(display).map(|display| {
platform_display.map(|display| {
if legacy {
// NOTE: For angle we use the Legacy code path, as that uses CreateWindowSurface
// instead of CreatePlatformWindowSurface*. The latter somehow
Expand Down Expand Up @@ -536,21 +610,23 @@ impl DisplayInner {
// terminate the display without worry for the instance being
// reused elsewhere.
let mut track_references = MaybeUninit::<EGLAttrib>::uninit();
unsafe {
(match self.raw {
EglDisplay::Khr(khr) => self.egl.QueryDisplayAttribKHR(
(match self.raw {
EglDisplay::Khr(khr) => unsafe {
self.egl.QueryDisplayAttribKHR(
khr,
egl::TRACK_REFERENCES_KHR as _,
track_references.as_mut_ptr(),
),
EglDisplay::Ext(ext) => self.egl.QueryDisplayAttribEXT(
)
},
EglDisplay::Ext(ext) => unsafe {
self.egl.QueryDisplayAttribEXT(
ext,
egl::TRACK_REFERENCES_KHR as _,
track_references.as_mut_ptr(),
),
EglDisplay::Legacy(_) => egl::FALSE,
} == egl::TRUE)
}
)
},
EglDisplay::Legacy(_) => egl::FALSE,
} == egl::TRUE)
}
}

Expand Down

0 comments on commit 6a03553

Please sign in to comment.