@@ -534,6 +534,22 @@ pub enum SurfaceTargetUnsafe {
534
534
SwapChainPanel ( * mut std:: ffi:: c_void ) ,
535
535
}
536
536
537
+ impl SurfaceTargetUnsafe {
538
+ /// Creates a [`SurfaceTargetUnsafe::RawHandle`] from a window.
539
+ ///
540
+ /// This is unsafe since it assumes that the window outlives the resulting surface target
541
+ /// (and subsequently the surface created for this target).
542
+ pub unsafe fn from_window < T > ( window : & T ) -> Result < Self , raw_window_handle:: HandleError >
543
+ where
544
+ T : HasDisplayHandle + HasWindowHandle ,
545
+ {
546
+ Ok ( Self :: RawHandle {
547
+ raw_display_handle : window. display_handle ( ) ?. as_raw ( ) ,
548
+ raw_window_handle : window. window_handle ( ) ?. as_raw ( ) ,
549
+ } )
550
+ }
551
+ }
552
+
537
553
/// Handle to a binding group layout.
538
554
///
539
555
/// A `BindGroupLayout` is a handle to the GPU-side layout of a binding group. It can be used to
@@ -1881,29 +1897,16 @@ impl Instance {
1881
1897
1882
1898
let target = target. into ( ) ;
1883
1899
let mut surface = match target {
1884
- SurfaceTarget :: Window ( window) => {
1885
- let raw_display_handle = window
1886
- . display_handle ( )
1887
- . map_err ( |e| CreateSurfaceError {
1900
+ SurfaceTarget :: Window ( window) => unsafe {
1901
+ let surface = self . create_surface_unsafe (
1902
+ SurfaceTargetUnsafe :: from_window ( & window) . map_err ( |e| CreateSurfaceError {
1888
1903
inner : CreateSurfaceErrorKind :: RawHandle ( e) ,
1889
- } ) ?
1890
- . as_raw ( ) ;
1891
- let raw_window_handle = window
1892
- . window_handle ( )
1893
- . map_err ( |e| CreateSurfaceError {
1894
- inner : CreateSurfaceErrorKind :: RawHandle ( e) ,
1895
- } ) ?
1896
- . as_raw ( ) ;
1897
-
1904
+ } ) ?,
1905
+ ) ;
1898
1906
handle_origin = Some ( window) ;
1899
1907
1900
- unsafe {
1901
- self . create_surface_unsafe ( SurfaceTargetUnsafe :: RawHandle {
1902
- raw_display_handle,
1903
- raw_window_handle,
1904
- } )
1905
- } ?
1906
- }
1908
+ surface
1909
+ } ?,
1907
1910
1908
1911
#[ cfg( any( webgpu, webgl) ) ]
1909
1912
SurfaceTarget :: Canvas ( canvas) => {
0 commit comments