@@ -479,68 +479,54 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
479479 display_handle : raw_window_handle:: RawDisplayHandle ,
480480 window_handle : raw_window_handle:: RawWindowHandle ,
481481 id_in : Input < G , SurfaceId > ,
482- ) -> SurfaceId {
482+ ) -> Result < SurfaceId , hal :: InstanceError > {
483483 profiling:: scope!( "Instance::create_surface" ) ;
484484
485485 fn init < A : HalApi > (
486- any_surface : & mut Option < AnySurface > ,
487486 inst : & Option < A :: Instance > ,
488487 display_handle : raw_window_handle:: RawDisplayHandle ,
489488 window_handle : raw_window_handle:: RawWindowHandle ,
490- ) {
491- if any_surface. is_none ( ) {
492- if let Some ( surface) = inst. as_ref ( ) . and_then ( |inst| unsafe {
493- match inst. create_surface ( display_handle, window_handle) {
494- Ok ( raw) => Some ( HalSurface :: < A > { raw : Arc :: new ( raw) } ) ,
495- Err ( e) => {
496- log:: warn!( "Error: {:?}" , e) ;
497- None
498- }
499- }
500- } ) {
501- * any_surface = Some ( AnySurface :: new ( surface) ) ;
489+ ) -> Option < Result < AnySurface , hal:: InstanceError > > {
490+ inst. as_ref ( ) . map ( |inst| unsafe {
491+ match inst. create_surface ( display_handle, window_handle) {
492+ Ok ( raw) => Ok ( AnySurface :: new ( HalSurface :: < A > { raw : Arc :: new ( raw) } ) ) ,
493+ Err ( e) => Err ( e) ,
502494 }
503- }
495+ } )
504496 }
505497
506498 let mut hal_surface = None ;
507499 #[ cfg( all( feature = "vulkan" , not( target_arch = "wasm32" ) ) ) ]
508- init :: < hal:: api:: Vulkan > (
509- & mut hal_surface,
510- & self . instance . vulkan ,
511- display_handle,
512- window_handle,
513- ) ;
500+ if hal_surface. is_none ( ) {
501+ hal_surface =
502+ init :: < hal:: api:: Vulkan > ( & self . instance . vulkan , display_handle, window_handle) ;
503+ }
514504 #[ cfg( all( feature = "metal" , any( target_os = "macos" , target_os = "ios" ) ) ) ]
515- init :: < hal:: api:: Metal > (
516- & mut hal_surface,
517- & self . instance . metal ,
518- display_handle,
519- window_handle,
520- ) ;
505+ if hal_surface. is_none ( ) {
506+ hal_surface =
507+ init :: < hal:: api:: Metal > ( & self . instance . metal , display_handle, window_handle) ;
508+ }
521509 #[ cfg( all( feature = "dx12" , windows) ) ]
522- init :: < hal:: api:: Dx12 > (
523- & mut hal_surface,
524- & self . instance . dx12 ,
525- display_handle,
526- window_handle,
527- ) ;
510+ if hal_surface. is_none ( ) {
511+ hal_surface =
512+ init :: < hal:: api:: Dx12 > ( & self . instance . dx12 , display_handle, window_handle) ;
513+ }
528514 #[ cfg( feature = "gles" ) ]
529- init :: < hal :: api :: Gles > (
530- & mut hal_surface ,
531- & self . instance . gl ,
532- display_handle ,
533- window_handle ,
534- ) ;
515+ if hal_surface . is_none ( ) {
516+ hal_surface = init :: < hal :: api :: Gles > ( & self . instance . gl , display_handle , window_handle ) ;
517+ }
518+
519+ // This is only None if there's no instance at all.
520+ let hal_surface = hal_surface . unwrap ( ) ? ;
535521
536522 let surface = Surface {
537523 presentation : Mutex :: new ( None ) ,
538524 info : ResourceInfo :: new ( "<Surface>" ) ,
539- raw : hal_surface. unwrap ( ) ,
525+ raw : hal_surface,
540526 } ;
541527
542528 let ( id, _) = self . surfaces . prepare :: < G > ( id_in) . assign ( surface) ;
543- id
529+ Ok ( id )
544530 }
545531
546532 /// # Safety
0 commit comments