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 444f626
Showing 1 changed file with 83 additions and 29 deletions.
112 changes: 83 additions & 29 deletions glutin/src/api/egl/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,17 @@ 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);

Expand All @@ -146,6 +148,24 @@ impl Display {
attrs.as_ptr(),
)
})
.or_else(|err| {
if has_display_reference {
attrs.pop();
attrs.pop();
attrs.pop();
attrs.push(egl::NONE as EGLint);
Self::check_display_error(unsafe {
egl.GetPlatformDisplayEXT(
egl::PLATFORM_DEVICE_EXT,
device.raw_device() as *mut _,
attrs.as_ptr(),
)
})
} else {

Check failure on line 164 in glutin/src/api/egl/display.rs

View workflow job for this annotation

GitHub Actions / Check formatting

Diff in /home/runner/work/glutin/glutin/glutin/src/api/egl/display.rs
Err(err)
}

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

Self::initialize_display(egl, display, None)
Expand Down Expand Up @@ -244,23 +264,40 @@ 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 =
let platform_display =
unsafe { egl.GetPlatformDisplay(platform, display as *mut _, attrs.as_ptr()) };

Self::check_display_error(display).map(EglDisplay::Khr)
Self::check_display_error(platform_display)
.or_else(|err| {
if has_display_reference {
attrs.pop();
attrs.pop();
attrs.pop();
attrs.push(egl::NONE as EGLAttrib);
let platform_display = unsafe {
egl.GetPlatformDisplay(platform, display as *mut _, attrs.as_ptr())
};
Self::check_display_error(platform_display)
} else {
Err(err)
}
})
.map(EglDisplay::Khr)
}

fn get_platform_display_ext(egl: &Egl, display: RawDisplayHandle) -> Result<EglDisplay> {
Expand Down Expand Up @@ -309,33 +346,50 @@ 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 =
let platform_display =
unsafe { egl.GetPlatformDisplayEXT(platform, display as *mut _, attrs.as_ptr()) };

Self::check_display_error(display).map(|display| {
if legacy {
// NOTE: For angle we use the Legacy code path, as that uses CreateWindowSurface
// instead of CreatePlatformWindowSurface*. The latter somehow
// doesn't work, only the former does. But Angle's own example also use the
// former: https://github.com/google/angle/blob/main/util/EGLWindow.cpp#L424
EglDisplay::Legacy(display)
} else {
EglDisplay::Ext(display)
}
})
Self::check_display_error(platform_display)
.or_else(|err| {
if has_display_reference {
attrs.pop();
attrs.pop();
attrs.pop();
attrs.push(egl::NONE as EGLint);
let platform_display = unsafe {
egl.GetPlatformDisplayEXT(platform, display as *mut _, attrs.as_ptr())
};
Self::check_display_error(platform_display)
} else {
Err(err)
}
})
.map(|display| {
if legacy {
// NOTE: For angle we use the Legacy code path, as that uses CreateWindowSurface
// instead of CreatePlatformWindowSurface*. The latter somehow
// doesn't work, only the former does. But Angle's own example also use the
// former: https://github.com/google/angle/blob/main/util/EGLWindow.cpp#L424
EglDisplay::Legacy(display)
} else {
EglDisplay::Ext(display)
}
})
}

fn get_display(egl: &Egl, display: RawDisplayHandle) -> Result<EglDisplay> {
Expand Down

0 comments on commit 444f626

Please sign in to comment.