Skip to content

Commit 433df8b

Browse files
committed
hal/egl: don't assume EGL-1.5 if upcast works
1 parent ea0c974 commit 433df8b

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

wgpu-hal/src/gles/adapter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
600600
) -> Option<crate::SurfaceCapabilities> {
601601
if surface.presentable {
602602
Some(crate::SurfaceCapabilities {
603-
formats: if surface.enable_srgb {
603+
formats: if surface.supports_srgb() {
604604
vec![
605605
wgt::TextureFormat::Rgba8UnormSrgb,
606606
#[cfg(not(target_arch = "wasm32"))]

wgpu-hal/src/gles/egl.rs

+51-52
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,6 @@ impl crate::Instance<super::Api> for Instance {
656656
}
657657
};
658658

659-
let enable_srgb = match inner.srgb_kind {
660-
SrgbFrameBufferKind::Core | SrgbFrameBufferKind::Khr => true,
661-
SrgbFrameBufferKind::None => false,
662-
};
663-
664659
inner
665660
.egl
666661
.make_current(inner.display, None, None, None)
@@ -676,7 +671,7 @@ impl crate::Instance<super::Api> for Instance {
676671
pbuffer: inner.pbuffer,
677672
raw_window_handle,
678673
swapchain: None,
679-
enable_srgb,
674+
srgb_kind: inner.srgb_kind,
680675
})
681676
}
682677
unsafe fn destroy_surface(&self, _surface: Surface) {}
@@ -756,7 +751,7 @@ pub struct Surface {
756751
pub(super) presentable: bool,
757752
raw_window_handle: RawWindowHandle,
758753
swapchain: Option<Swapchain>,
759-
pub(super) enable_srgb: bool,
754+
srgb_kind: SrgbFrameBufferKind,
760755
}
761756

762757
unsafe impl Send for Surface {}
@@ -834,6 +829,13 @@ impl Surface {
834829
None => None,
835830
}
836831
}
832+
833+
pub fn supports_srgb(&self) -> bool {
834+
match self.srgb_kind {
835+
SrgbFrameBufferKind::None => false,
836+
_ => true,
837+
}
838+
}
837839
}
838840

839841
impl crate::Surface<super::Api> for Surface {
@@ -891,58 +893,55 @@ impl crate::Surface<super::Api> for Surface {
891893
_ => unreachable!(),
892894
};
893895

894-
let raw = if let Some(egl) = self.egl.upcast::<egl::EGL1_5>() {
895-
let attributes = [
896-
egl::RENDER_BUFFER as usize,
897-
if cfg!(target_os = "android") {
898-
egl::BACK_BUFFER as usize
899-
} else {
900-
egl::SINGLE_BUFFER as usize
901-
},
902-
// Always enable sRGB in EGL 1.5
903-
egl::GL_COLORSPACE as usize,
904-
egl::GL_COLORSPACE_SRGB as usize,
905-
egl::ATTRIB_NONE,
906-
];
907-
896+
let mut attributes = vec![
897+
egl::RENDER_BUFFER,
898+
if cfg!(target_os = "android") {
899+
egl::BACK_BUFFER
900+
} else {
901+
egl::SINGLE_BUFFER
902+
},
903+
];
904+
match self.srgb_kind {
905+
SrgbFrameBufferKind::None => {}
906+
SrgbFrameBufferKind::Core => {
907+
attributes.push(egl::GL_COLORSPACE);
908+
attributes.push(egl::GL_COLORSPACE_SRGB);
909+
}
910+
SrgbFrameBufferKind::Khr => {
911+
attributes.push(EGL_GL_COLORSPACE_KHR as i32);
912+
attributes.push(EGL_GL_COLORSPACE_SRGB_KHR as i32);
913+
}
914+
}
915+
attributes.push(egl::ATTRIB_NONE as i32);
916+
917+
// Careful, we can still be in 1.4 version even if `upcast` succeeds
918+
let raw_result = if let Some(egl) = self.egl.upcast::<egl::EGL1_5>() {
919+
let attributes_usize = attributes
920+
.into_iter()
921+
.map(|v| v as usize)
922+
.collect::<Vec<_>>();
908923
egl.create_platform_window_surface(
909924
self.display,
910925
self.config,
911926
native_window_ptr,
912-
&attributes,
927+
&attributes_usize,
913928
)
914-
.map_err(|e| {
915-
log::warn!("Error in create_platform_window_surface: {:?}", e);
916-
crate::SurfaceError::Lost
917-
})
918929
} else {
919-
let mut attributes = vec![
920-
egl::RENDER_BUFFER,
921-
if cfg!(target_os = "android") {
922-
egl::BACK_BUFFER
923-
} else {
924-
egl::SINGLE_BUFFER
925-
},
926-
];
927-
if self.enable_srgb {
928-
attributes.push(EGL_GL_COLORSPACE_KHR as i32);
929-
attributes.push(EGL_GL_COLORSPACE_SRGB_KHR as i32);
930-
}
931-
attributes.push(egl::ATTRIB_NONE as i32);
932-
self.egl
933-
.create_window_surface(
934-
self.display,
935-
self.config,
936-
native_window_ptr,
937-
Some(&attributes),
938-
)
939-
.map_err(|e| {
940-
log::warn!("Error in create_platform_window_surface: {:?}", e);
941-
crate::SurfaceError::Lost
942-
})
943-
}?;
930+
self.egl.create_window_surface(
931+
self.display,
932+
self.config,
933+
native_window_ptr,
934+
Some(&attributes),
935+
)
936+
};
944937

945-
(raw, wl_window)
938+
match raw_result {
939+
Ok(raw) => (raw, wl_window),
940+
Err(e) => {
941+
log::warn!("Error in create_platform_window_surface: {:?}", e);
942+
return Err(crate::SurfaceError::Lost);
943+
}
944+
}
946945
}
947946
};
948947

0 commit comments

Comments
 (0)