Add public APIs for creating a Metal view attached to an SDL window. Add SDL_metal.h.
authorAlex Szpakowski <slime73@gmail.com>
Mon, 05 Aug 2019 12:35:32 -0300
changeset 12998b87ba0fad17e
parent 12997 35e9649605cb
child 12999 a7ca9815bc79
Add public APIs for creating a Metal view attached to an SDL window. Add SDL_metal.h.
CMakeLists.txt
Makefile.in
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
Xcode/SDL/SDL.xcodeproj/project.pbxproj
configure
configure.ac
include/SDL.h
include/SDL_config.h.cmake
include/SDL_config.h.in
include/SDL_config_iphoneos.h
include/SDL_config_macosx.h
include/SDL_metal.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/render/metal/SDL_render_metal.m
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoametalview.h
src/video/cocoa/SDL_cocoametalview.m
src/video/cocoa/SDL_cocoavideo.m
src/video/cocoa/SDL_cocoavulkan.m
src/video/uikit/SDL_uikitmetalview.h
src/video/uikit/SDL_uikitmetalview.m
src/video/uikit/SDL_uikitvideo.m
src/video/uikit/SDL_uikitvulkan.m
     1.1 --- a/CMakeLists.txt	Sun Aug 04 23:30:55 2019 -0300
     1.2 +++ b/CMakeLists.txt	Mon Aug 05 12:35:32 2019 -0300
     1.3 @@ -373,8 +373,10 @@
     1.4  set_option(DIRECTX             "Use DirectX for Windows audio/video" ${WINDOWS})
     1.5  set_option(WASAPI              "Use the Windows WASAPI audio driver" ${WINDOWS})
     1.6  set_option(RENDER_D3D          "Enable the Direct3D render driver" ${WINDOWS})
     1.7 +set_option(RENDER_METAL        "Enable the Metal render driver" ${APPLE})
     1.8  set_option(VIDEO_VIVANTE       "Use Vivante EGL video driver" ${UNIX_SYS})
     1.9  dep_option(VIDEO_VULKAN        "Enable Vulkan support" ON "ANDROID OR APPLE OR LINUX OR WINDOWS" OFF)
    1.10 +set_option(VIDEO_METAL         "Enable Metal support" ${APPLE})
    1.11  set_option(VIDEO_KMSDRM        "Use KMS DRM video driver" ${UNIX_SYS})
    1.12  dep_option(KMSDRM_SHARED       "Dynamically load KMS DRM support" ON "VIDEO_KMSDRM" OFF)
    1.13  option_string(BACKGROUNDING_SIGNAL "number to use for magic backgrounding signal or 'OFF'" "OFF")
    1.14 @@ -1533,6 +1535,69 @@
    1.15      set(HAVE_SDL_FILESYSTEM TRUE)
    1.16    endif()
    1.17  
    1.18 +  # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
    1.19 +  if(SDL_VIDEO)
    1.20 +    if (IOS)
    1.21 +      set(SDL_VIDEO_DRIVER_UIKIT 1)
    1.22 +      file(GLOB UIKITVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/uikit/*.m)
    1.23 +      set(SOURCE_FILES ${SOURCE_FILES} ${UIKITVIDEO_SOURCES})
    1.24 +    else()
    1.25 +      CheckCOCOA()
    1.26 +      if(VIDEO_OPENGL)
    1.27 +        set(SDL_VIDEO_OPENGL 1)
    1.28 +        set(SDL_VIDEO_OPENGL_CGL 1)
    1.29 +        set(SDL_VIDEO_RENDER_OGL 1)
    1.30 +        set(HAVE_VIDEO_OPENGL TRUE)
    1.31 +      endif()
    1.32 +
    1.33 +      if(VIDEO_OPENGLES)
    1.34 +        set(SDL_VIDEO_OPENGL_EGL 1)
    1.35 +        set(SDL_VIDEO_OPENGL_ES2 1)
    1.36 +        set(SDL_VIDEO_RENDER_OGL_ES2 1)
    1.37 +        set(HAVE_VIDEO_OPENGLES TRUE)
    1.38 +      endif()
    1.39 +    endif()
    1.40 +    
    1.41 +    if(VIDEO_VULKAN OR VIDEO_METAL OR RENDER_METAL)
    1.42 +      set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
    1.43 +      set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x objective-c")
    1.44 +      check_c_source_compiles("
    1.45 +        #include <AvailabilityMacros.h>
    1.46 +        #import <Metal/Metal.h>
    1.47 +        #import <QuartzCore/CAMetalLayer.h>
    1.48 +
    1.49 +        #if TARGET_OS_SIMULATOR || (!TARGET_CPU_X86_64 && !TARGET_CPU_ARM64)
    1.50 +        #error Metal doesn't work on this configuration
    1.51 +        #endif
    1.52 +        int main()
    1.53 +        {
    1.54 +            return 0;
    1.55 +        }
    1.56 +        " HAVE_FRAMEWORK_METAL)
    1.57 +      set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
    1.58 +      if(HAVE_FRAMEWORK_METAL)
    1.59 +        set(SDL_FRAMEWORK_METAL 1)
    1.60 +        set(SDL_FRAMEWORK_QUARTZCORE 1)
    1.61 +      else()
    1.62 +        set(VIDEO_VULKAN 0)
    1.63 +        set(VIDEO_METAL 0)
    1.64 +        set(RENDER_METAL 0)
    1.65 +      endif()
    1.66 +    endif()
    1.67 +
    1.68 +    if(VIDEO_METAL)
    1.69 +      set(SDL_VIDEO_METAL 1)
    1.70 +      set(HAVE_VIDEO_METAL TRUE)
    1.71 +    endif()
    1.72 +
    1.73 +    if(RENDER_METAL)
    1.74 +      file(GLOB RENDER_METAL_SOURCES ${SDL2_SOURCE_DIR}/src/render/metal/*.m)
    1.75 +      set(SOURCE_FILES ${SOURCE_FILES} ${RENDER_METAL_SOURCES})
    1.76 +      set(SDL_VIDEO_RENDER_METAL 1)
    1.77 +      set(HAVE_RENDER_METAL TRUE)
    1.78 +    endif()
    1.79 +  endif()
    1.80 +
    1.81    # Actually load the frameworks at the end so we don't duplicate include.
    1.82    if(SDL_FRAMEWORK_COREVIDEO)
    1.83      find_library(COREVIDEO CoreVideo)
    1.84 @@ -1562,28 +1627,20 @@
    1.85      find_library(AUDIOTOOLBOX AudioToolbox)
    1.86      list(APPEND EXTRA_LIBS ${AUDIOTOOLBOX})
    1.87    endif()
    1.88 -
    1.89 -  # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
    1.90 -  if(SDL_VIDEO)
    1.91 -    if (IOS)
    1.92 -      set(SDL_VIDEO_DRIVER_UIKIT 1)
    1.93 -      file(GLOB UIKITVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/uikit/*.m)
    1.94 -      set(SOURCE_FILES ${SOURCE_FILES} ${UIKITVIDEO_SOURCES})
    1.95 +  if(SDL_FRAMEWORK_METAL)
    1.96 +    if(IOS)
    1.97 +      find_library(METAL Metal)
    1.98 +      list(APPEND EXTRA_LIBS ${METAL})
    1.99      else()
   1.100 -      CheckCOCOA()
   1.101 -      if(VIDEO_OPENGL)
   1.102 -        set(SDL_VIDEO_OPENGL 1)
   1.103 -        set(SDL_VIDEO_OPENGL_CGL 1)
   1.104 -        set(SDL_VIDEO_RENDER_OGL 1)
   1.105 -        set(HAVE_VIDEO_OPENGL TRUE)
   1.106 -      endif()
   1.107 -
   1.108 -      if(VIDEO_OPENGLES)
   1.109 -        set(SDL_VIDEO_OPENGL_EGL 1)
   1.110 -        set(SDL_VIDEO_OPENGL_ES2 1)
   1.111 -        set(SDL_VIDEO_RENDER_OGL_ES2 1)
   1.112 -        set(HAVE_VIDEO_OPENGLES TRUE)
   1.113 -      endif()
   1.114 +      list(APPEND EXTRA_LDFLAGS "-Wl,-weak_framework,Metal")
   1.115 +    endif()
   1.116 +  endif()
   1.117 +  if(SDL_FRAMEWORK_QUARTZCORE)
   1.118 +    if(IOS)
   1.119 +      find_library(QUARTZCORE QuartzCore)
   1.120 +      list(APPEND EXTRA_LIBS ${QUARTZCORE})
   1.121 +    else()
   1.122 +      list(APPEND EXTRA_LDFLAGS "-Wl,-weak_framework,QuartzCore")
   1.123      endif()
   1.124    endif()
   1.125  
     2.1 --- a/Makefile.in	Sun Aug 04 23:30:55 2019 -0300
     2.2 +++ b/Makefile.in	Mon Aug 05 12:35:32 2019 -0300
     2.3 @@ -84,6 +84,7 @@
     2.4  	SDL_log.h \
     2.5  	SDL_main.h \
     2.6  	SDL_messagebox.h \
     2.7 +	SDL_metal.h \
     2.8  	SDL_mouse.h \
     2.9  	SDL_mutex.h \
    2.10  	SDL_name.h \
     3.1 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Sun Aug 04 23:30:55 2019 -0300
     3.2 +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Mon Aug 05 12:35:32 2019 -0300
     3.3 @@ -717,6 +717,7 @@
     3.4  		F3E3C75B224138AE007D243C /* SDL_uikit_main.c in Sources */ = {isa = PBXBuildFile; fileRef = F3E3C657224069CE007D243C /* SDL_uikit_main.c */; };
     3.5  		FA1DC2721C62BE65008F99A0 /* SDL_uikitclipboard.h in Headers */ = {isa = PBXBuildFile; fileRef = FA1DC2701C62BE65008F99A0 /* SDL_uikitclipboard.h */; };
     3.6  		FA1DC2731C62BE65008F99A0 /* SDL_uikitclipboard.m in Sources */ = {isa = PBXBuildFile; fileRef = FA1DC2711C62BE65008F99A0 /* SDL_uikitclipboard.m */; };
     3.7 +		FA24348D21D4201400B8918A /* SDL_metal.h in Headers */ = {isa = PBXBuildFile; fileRef = FA24348C21D4201400B8918A /* SDL_metal.h */; };
     3.8  		FAB5981D1BB5C31500BE72C5 /* SDL_atomic.c in Sources */ = {isa = PBXBuildFile; fileRef = 04FFAB8912E23B8D00BA343D /* SDL_atomic.c */; };
     3.9  		FAB5981E1BB5C31500BE72C5 /* SDL_spinlock.c in Sources */ = {isa = PBXBuildFile; fileRef = 04FFAB8A12E23B8D00BA343D /* SDL_spinlock.c */; };
    3.10  		FAB5981F1BB5C31500BE72C5 /* SDL_coreaudio.m in Sources */ = {isa = PBXBuildFile; fileRef = 56EA86F913E9EC2B002E47EB /* SDL_coreaudio.m */; };
    3.11 @@ -1064,6 +1065,7 @@
    3.12  		F3E3C75F224138AE007D243C /* libSDLmain.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDLmain.a; sourceTree = BUILT_PRODUCTS_DIR; };
    3.13  		FA1DC2701C62BE65008F99A0 /* SDL_uikitclipboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitclipboard.h; sourceTree = "<group>"; };
    3.14  		FA1DC2711C62BE65008F99A0 /* SDL_uikitclipboard.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitclipboard.m; sourceTree = "<group>"; };
    3.15 +		FA24348C21D4201400B8918A /* SDL_metal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_metal.h; sourceTree = "<group>"; };
    3.16  		FAB598141BB5C1B100BE72C5 /* libSDL2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDL2.a; sourceTree = BUILT_PRODUCTS_DIR; };
    3.17  		FAD4F7011BA3C4E8008346CE /* SDL_sysjoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sysjoystick_c.h; sourceTree = "<group>"; };
    3.18  		FD0BBFEF0E3933DD00D833B1 /* SDL_uikitview.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitview.h; sourceTree = "<group>"; };
    3.19 @@ -1576,15 +1578,14 @@
    3.20  			children = (
    3.21  				AA7558651595D55500BBD41B /* begin_code.h */,
    3.22  				AA7558661595D55500BBD41B /* close_code.h */,
    3.23 -				AA7558971595D55500BBD41B /* SDL.h */,
    3.24  				AA7558671595D55500BBD41B /* SDL_assert.h */,
    3.25  				AA7558681595D55500BBD41B /* SDL_atomic.h */,
    3.26  				AA7558691595D55500BBD41B /* SDL_audio.h */,
    3.27  				AADA5B8E16CCAB7C00107CF7 /* SDL_bits.h */,
    3.28  				AA75586A1595D55500BBD41B /* SDL_blendmode.h */,
    3.29  				AA75586B1595D55500BBD41B /* SDL_clipboard.h */,
    3.30 +				AA75586C1595D55500BBD41B /* SDL_config_iphoneos.h */,
    3.31  				AA75586D1595D55500BBD41B /* SDL_config.h */,
    3.32 -				AA75586C1595D55500BBD41B /* SDL_config_iphoneos.h */,
    3.33  				AA75586E1595D55500BBD41B /* SDL_copying.h */,
    3.34  				AA75586F1595D55500BBD41B /* SDL_cpuinfo.h */,
    3.35  				AA7558701595D55500BBD41B /* SDL_endian.h */,
    3.36 @@ -1602,6 +1603,7 @@
    3.37  				AA75587B1595D55500BBD41B /* SDL_log.h */,
    3.38  				AA75587C1595D55500BBD41B /* SDL_main.h */,
    3.39  				AA9FF9501637C6E5000DF050 /* SDL_messagebox.h */,
    3.40 +				FA24348C21D4201400B8918A /* SDL_metal.h */,
    3.41  				AA75587D1595D55500BBD41B /* SDL_mouse.h */,
    3.42  				AA75587E1595D55500BBD41B /* SDL_mutex.h */,
    3.43  				AA75587F1595D55500BBD41B /* SDL_name.h */,
    3.44 @@ -1630,6 +1632,7 @@
    3.45  				AA7558951595D55500BBD41B /* SDL_version.h */,
    3.46  				AA7558961595D55500BBD41B /* SDL_video.h */,
    3.47  				4D7516FE1EE1C5B400820EEA /* SDL_vulkan.h */,
    3.48 +				AA7558971595D55500BBD41B /* SDL.h */,
    3.49  			);
    3.50  			name = "Public Headers";
    3.51  			path = ../../include;
    3.52 @@ -2183,6 +2186,7 @@
    3.53  				AA75589D1595D55500BBD41B /* SDL_blendmode.h in Headers */,
    3.54  				F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */,
    3.55  				AA75589E1595D55500BBD41B /* SDL_clipboard.h in Headers */,
    3.56 +				FA24348D21D4201400B8918A /* SDL_metal.h in Headers */,
    3.57  				AA75589F1595D55500BBD41B /* SDL_config_iphoneos.h in Headers */,
    3.58  				AA7558A01595D55500BBD41B /* SDL_config.h in Headers */,
    3.59  				AA7558A11595D55500BBD41B /* SDL_copying.h in Headers */,
     4.1 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj	Sun Aug 04 23:30:55 2019 -0300
     4.2 +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj	Mon Aug 05 12:35:32 2019 -0300
     4.3 @@ -919,6 +919,7 @@
     4.4  		F3950CD8212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
     4.5  		F3950CD9212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
     4.6  		F3950CDA212BC88D00F51292 /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F3950CD7212BC88D00F51292 /* SDL_sensor.h */; settings = {ATTRIBUTES = (Public, ); }; };
     4.7 +		FA24348B21D41FFB00B8918A /* SDL_metal.h in Headers */ = {isa = PBXBuildFile; fileRef = FA24348A21D41FFB00B8918A /* SDL_metal.h */; };
     4.8  		FA73671D19A540EF004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; };
     4.9  		FA73671E19A54140004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; };
    4.10  		FA73671F19A54144004122E4 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA73671C19A540EF004122E4 /* CoreVideo.framework */; };
    4.11 @@ -1252,6 +1253,7 @@
    4.12  		F59C710300D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = "<group>"; };
    4.13  		F59C710600D5CB5801000001 /* SDL.info */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = SDL.info; sourceTree = "<group>"; };
    4.14  		F5A2EF3900C6A39A01000001 /* BUGS.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; name = BUGS.txt; path = ../../BUGS.txt; sourceTree = SOURCE_ROOT; };
    4.15 +		FA24348A21D41FFB00B8918A /* SDL_metal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_metal.h; sourceTree = "<group>"; };
    4.16  		FA73671C19A540EF004122E4 /* CoreVideo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreVideo.framework; path = System/Library/Frameworks/CoreVideo.framework; sourceTree = SDKROOT; };
    4.17  		FABA34C61D8B5DB100915323 /* SDL_coreaudio.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_coreaudio.m; sourceTree = "<group>"; };
    4.18  /* End PBXFileReference section */
    4.19 @@ -1342,6 +1344,7 @@
    4.20  				AA7557DD1595D4D800BBD41B /* SDL_log.h */,
    4.21  				AA7557DE1595D4D800BBD41B /* SDL_main.h */,
    4.22  				AA9FF9591637CBF9000DF050 /* SDL_messagebox.h */,
    4.23 +				FA24348A21D41FFB00B8918A /* SDL_metal.h */,
    4.24  				AA7557DF1595D4D800BBD41B /* SDL_mouse.h */,
    4.25  				AA7557E01595D4D800BBD41B /* SDL_mutex.h */,
    4.26  				AA7557E11595D4D800BBD41B /* SDL_name.h */,
    4.27 @@ -2029,6 +2032,7 @@
    4.28  				AA75580E1595D4D800BBD41B /* SDL_cpuinfo.h in Headers */,
    4.29  				AA7558101595D4D800BBD41B /* SDL_endian.h in Headers */,
    4.30  				AA7558121595D4D800BBD41B /* SDL_error.h in Headers */,
    4.31 +				FA24348B21D41FFB00B8918A /* SDL_metal.h in Headers */,
    4.32  				AA7558141595D4D800BBD41B /* SDL_events.h in Headers */,
    4.33  				567E2F2117C44C35005F1892 /* SDL_filesystem.h in Headers */,
    4.34  				A77E6EB4167AB0A90010E40B /* SDL_gamecontroller.h in Headers */,
     5.1 --- a/configure	Sun Aug 04 23:30:55 2019 -0300
     5.2 +++ b/configure	Mon Aug 05 12:35:32 2019 -0300
     5.3 @@ -869,6 +869,7 @@
     5.4  enable_video_x11_vm
     5.5  enable_video_vivante
     5.6  enable_video_cocoa
     5.7 +enable_video_metal
     5.8  enable_render_metal
     5.9  enable_video_directfb
    5.10  enable_directfb_shared
    5.11 @@ -1647,6 +1648,7 @@
    5.12    --enable-video-x11-vm   use X11 VM extension for fullscreen [[default=yes]]
    5.13    --enable-video-vivante  use Vivante EGL video driver [[default=yes]]
    5.14    --enable-video-cocoa    use Cocoa video driver [[default=yes]]
    5.15 +  --enable-video-metal    include Metal support [[default=yes]]
    5.16    --enable-render-metal   enable the Metal render driver [[default=yes]]
    5.17    --enable-video-directfb use DirectFB video driver [[default=no]]
    5.18    --enable-directfb-shared
    5.19 @@ -21530,6 +21532,13 @@
    5.20  
    5.21  CheckMETAL()
    5.22  {
    5.23 +    # Check whether --enable-video-metal was given.
    5.24 +if test "${enable_video_metal+set}" = set; then :
    5.25 +  enableval=$enable_video_metal;
    5.26 +else
    5.27 +  enable_video_metal=yes
    5.28 +fi
    5.29 +
    5.30      # Check whether --enable-render-metal was given.
    5.31  if test "${enable_render_metal+set}" = set; then :
    5.32    enableval=$enable_render_metal;
    5.33 @@ -21537,7 +21546,7 @@
    5.34    enable_render_metal=yes
    5.35  fi
    5.36  
    5.37 -    if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    5.38 +    if test x$enable_video = xyes -a x$enable_video_metal = xyes; then
    5.39          save_CFLAGS="$CFLAGS"
    5.40                  CFLAGS="$CFLAGS -x objective-c"
    5.41          { $as_echo "$as_me:${as_lineno-$LINENO}: checking for Metal framework" >&5
    5.42 @@ -21574,11 +21583,17 @@
    5.43  $as_echo "$have_metal" >&6; }
    5.44          if test x$have_metal = xyes; then
    5.45  
    5.46 +$as_echo "#define SDL_VIDEO_METAL 1" >>confdefs.h
    5.47 +
    5.48 +            if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    5.49 +
    5.50  $as_echo "#define SDL_VIDEO_RENDER_METAL 1" >>confdefs.h
    5.51  
    5.52 -            SOURCES="$SOURCES $srcdir/src/render/metal/*.m"
    5.53 +                SOURCES="$SOURCES $srcdir/src/render/metal/*.m"
    5.54 +            fi
    5.55              SUMMARY_video="${SUMMARY_video} metal"
    5.56          else
    5.57 +            enable_video_metal=no
    5.58              enable_render_metal=no
    5.59          fi
    5.60      fi
    5.61 @@ -25064,7 +25079,7 @@
    5.62          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,QuartzCore"
    5.63          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,UIKit"
    5.64  
    5.65 -        if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    5.66 +        if test x$enable_video_metal = xyes -o x$enable_video_vulkan = xyes; then
    5.67              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,Metal"
    5.68          fi
    5.69          ;;
    5.70 @@ -25158,7 +25173,7 @@
    5.71          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,Carbon"
    5.72          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,IOKit"
    5.73  
    5.74 -        if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    5.75 +        if test x$enable_video_metal = xyes -o x$enable_video_vulkan = xyes; then
    5.76              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-weak_framework,QuartzCore -Wl,-weak_framework,Metal"
    5.77          fi
    5.78          ;;
     6.1 --- a/configure.ac	Sun Aug 04 23:30:55 2019 -0300
     6.2 +++ b/configure.ac	Mon Aug 05 12:35:32 2019 -0300
     6.3 @@ -2048,10 +2048,13 @@
     6.4  
     6.5  CheckMETAL()
     6.6  {
     6.7 +    AC_ARG_ENABLE(video-metal,
     6.8 +AC_HELP_STRING([--enable-video-metal], [include Metal support [[default=yes]]]),
     6.9 +              , enable_video_metal=yes)
    6.10      AC_ARG_ENABLE(render-metal,
    6.11  AS_HELP_STRING([--enable-render-metal], [enable the Metal render driver [[default=yes]]]),
    6.12                                  , enable_render_metal=yes)
    6.13 -    if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    6.14 +    if test x$enable_video = xyes -a x$enable_video_metal = xyes; then
    6.15          save_CFLAGS="$CFLAGS"
    6.16          dnl Work around that we don't have Objective-C support in autoconf
    6.17          CFLAGS="$CFLAGS -x objective-c"
    6.18 @@ -2072,10 +2075,14 @@
    6.19          CFLAGS="$save_CFLAGS"
    6.20          AC_MSG_RESULT($have_metal)
    6.21          if test x$have_metal = xyes; then
    6.22 -            AC_DEFINE(SDL_VIDEO_RENDER_METAL, 1, [ ])
    6.23 -            SOURCES="$SOURCES $srcdir/src/render/metal/*.m"
    6.24 +            AC_DEFINE(SDL_VIDEO_METAL, 1, [ ])
    6.25 +            if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    6.26 +                AC_DEFINE(SDL_VIDEO_RENDER_METAL, 1, [ ])
    6.27 +                SOURCES="$SOURCES $srcdir/src/render/metal/*.m"
    6.28 +            fi
    6.29              SUMMARY_video="${SUMMARY_video} metal"
    6.30          else
    6.31 +            enable_video_metal=no
    6.32              enable_render_metal=no
    6.33          fi
    6.34      fi
    6.35 @@ -3828,7 +3835,7 @@
    6.36          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,QuartzCore"
    6.37          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,UIKit"
    6.38  
    6.39 -        if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    6.40 +        if test x$enable_video_metal = xyes -o x$enable_video_vulkan = xyes; then
    6.41              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,Metal"
    6.42          fi
    6.43          ;;
    6.44 @@ -3910,7 +3917,7 @@
    6.45          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,Carbon"
    6.46          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,IOKit"
    6.47  
    6.48 -        if test x$enable_render = xyes -a x$enable_render_metal = xyes; then
    6.49 +        if test x$enable_video_metal = xyes -o x$enable_video_vulkan = xyes; then
    6.50              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-weak_framework,QuartzCore -Wl,-weak_framework,Metal"
    6.51          fi
    6.52          ;;
     7.1 --- a/include/SDL.h	Sun Aug 04 23:30:55 2019 -0300
     7.2 +++ b/include/SDL.h	Mon Aug 05 12:35:32 2019 -0300
     7.3 @@ -47,6 +47,7 @@
     7.4  #include "SDL_loadso.h"
     7.5  #include "SDL_log.h"
     7.6  #include "SDL_messagebox.h"
     7.7 +#include "SDL_metal.h"
     7.8  #include "SDL_mutex.h"
     7.9  #include "SDL_power.h"
    7.10  #include "SDL_render.h"
     8.1 --- a/include/SDL_config.h.cmake	Sun Aug 04 23:30:55 2019 -0300
     8.2 +++ b/include/SDL_config.h.cmake	Mon Aug 05 12:35:32 2019 -0300
     8.3 @@ -388,6 +388,9 @@
     8.4  /* Enable Vulkan support */
     8.5  #cmakedefine SDL_VIDEO_VULKAN @SDL_VIDEO_VULKAN@
     8.6  
     8.7 +/* Enable Metal support */
     8.8 +#cmakedefine SDL_VIDEO_METAL @SDL_VIDEO_METAL@
     8.9 +
    8.10  /* Enable system power support */
    8.11  #cmakedefine SDL_POWER_ANDROID @SDL_POWER_ANDROID@
    8.12  #cmakedefine SDL_POWER_LINUX @SDL_POWER_LINUX@
     9.1 --- a/include/SDL_config.h.in	Sun Aug 04 23:30:55 2019 -0300
     9.2 +++ b/include/SDL_config.h.in	Mon Aug 05 12:35:32 2019 -0300
     9.3 @@ -383,6 +383,9 @@
     9.4  /* Enable Vulkan support */
     9.5  #undef SDL_VIDEO_VULKAN
     9.6  
     9.7 +/* Enable Metal support */
     9.8 +#undef SDL_VIDEO_METAL
     9.9 +
    9.10  /* Enable system power support */
    9.11  #undef SDL_POWER_LINUX
    9.12  #undef SDL_POWER_WINDOWS
    10.1 --- a/include/SDL_config_iphoneos.h	Sun Aug 04 23:30:55 2019 -0300
    10.2 +++ b/include/SDL_config_iphoneos.h	Mon Aug 05 12:35:32 2019 -0300
    10.3 @@ -181,6 +181,10 @@
    10.4  #define SDL_VIDEO_VULKAN 1
    10.5  #endif
    10.6  
    10.7 +#if SDL_PLATFORM_SUPPORTS_METAL
    10.8 +#define SDL_VIDEO_METAL 1
    10.9 +#endif
   10.10 +
   10.11  /* Enable system power support */
   10.12  #define SDL_POWER_UIKIT 1
   10.13  
    11.1 --- a/include/SDL_config_macosx.h	Sun Aug 04 23:30:55 2019 -0300
    11.2 +++ b/include/SDL_config_macosx.h	Mon Aug 05 12:35:32 2019 -0300
    11.3 @@ -193,9 +193,15 @@
    11.4  #define SDL_VIDEO_RENDER_OGL_ES2 1
    11.5  #endif
    11.6  
    11.7 -#ifndef SDL_VIDEO_RENDER_METAL
    11.8  /* Metal only supported on 64-bit architectures with 10.11+ */
    11.9  #if TARGET_CPU_X86_64 && (MAC_OS_X_VERSION_MAX_ALLOWED >= 101100)
   11.10 +#define SDL_PLATFORM_SUPPORTS_METAL    1
   11.11 +#else
   11.12 +#define SDL_PLATFORM_SUPPORTS_METAL    0
   11.13 +#endif
   11.14 +
   11.15 +#ifndef SDL_VIDEO_RENDER_METAL
   11.16 +#if SDL_PLATFORM_SUPPORTS_METAL
   11.17  #define SDL_VIDEO_RENDER_METAL    1
   11.18  #else
   11.19  #define SDL_VIDEO_RENDER_METAL    0
   11.20 @@ -219,13 +225,22 @@
   11.21  #define SDL_VIDEO_OPENGL_GLX    1
   11.22  #endif
   11.23  
   11.24 -/* Enable Vulkan support */
   11.25 -/* Metal/Vulkan Portability only supported on 64-bit architectures with 10.11+ */
   11.26 -#if TARGET_CPU_X86_64 && (MAC_OS_X_VERSION_MAX_ALLOWED >= 101100)
   11.27 +/* Enable Vulkan and Metal support */
   11.28 +#ifndef SDL_VIDEO_VULKAN
   11.29 +#if SDL_PLATFORM_SUPPORTS_METAL
   11.30  #define SDL_VIDEO_VULKAN 1
   11.31  #else
   11.32  #define SDL_VIDEO_VULKAN 0
   11.33  #endif
   11.34 +#endif
   11.35 +
   11.36 +#ifndef SDL_VIDEO_METAL
   11.37 +#if SDL_PLATFORM_SUPPORTS_METAL
   11.38 +#define SDL_VIDEO_METAL 1
   11.39 +#else
   11.40 +#define SDL_VIDEO_METAL 0
   11.41 +#endif
   11.42 +#endif
   11.43  
   11.44  /* Enable system power support */
   11.45  #define SDL_POWER_MACOSX 1
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/include/SDL_metal.h	Mon Aug 05 12:35:32 2019 -0300
    12.3 @@ -0,0 +1,91 @@
    12.4 +/*
    12.5 +  Simple DirectMedia Layer
    12.6 +  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
    12.7 +
    12.8 +  This software is provided 'as-is', without any express or implied
    12.9 +  warranty.  In no event will the authors be held liable for any damages
   12.10 +  arising from the use of this software.
   12.11 +
   12.12 +  Permission is granted to anyone to use this software for any purpose,
   12.13 +  including commercial applications, and to alter it and redistribute it
   12.14 +  freely, subject to the following restrictions:
   12.15 +
   12.16 +  1. The origin of this software must not be misrepresented; you must not
   12.17 +     claim that you wrote the original software. If you use this software
   12.18 +     in a product, an acknowledgment in the product documentation would be
   12.19 +     appreciated but is not required.
   12.20 +  2. Altered source versions must be plainly marked as such, and must not be
   12.21 +     misrepresented as being the original software.
   12.22 +  3. This notice may not be removed or altered from any source distribution.
   12.23 +*/
   12.24 +
   12.25 +/**
   12.26 + *  \file SDL_metal.h
   12.27 + *
   12.28 + *  Header file for functions to creating Metal layers and views on SDL windows.
   12.29 + */
   12.30 +
   12.31 +#ifndef SDL_metal_h_
   12.32 +#define SDL_metal_h_
   12.33 +
   12.34 +#include "SDL_video.h"
   12.35 +
   12.36 +#include "begin_code.h"
   12.37 +/* Set up for C function definitions, even when using C++ */
   12.38 +#ifdef __cplusplus
   12.39 +extern "C" {
   12.40 +#endif
   12.41 +
   12.42 +/**
   12.43 + *  \brief A handle to a CAMetalLayer-backed NSView (macOS) or UIView (iOS/tvOS).
   12.44 + *
   12.45 + *  \note This can be cast directly to an NSView or UIView.
   12.46 + */
   12.47 +typedef void *SDL_MetalView;
   12.48 +
   12.49 +/**
   12.50 + *  \name Metal support functions
   12.51 + */
   12.52 +/* @{ */
   12.53 +
   12.54 +/**
   12.55 + *  \brief Create a CAMetalLayer-backed NSView/UIView and attach it to the
   12.56 + *        specified window.
   12.57 + *
   12.58 + *  On macOS, this does *not* associate a MTLDevice with the CAMetalLayer on its
   12.59 + *  own. It is up to user code to do that.
   12.60 + *
   12.61 + *  The returned handle can be casted directly to a NSView or UIView, and the
   12.62 + *  CAMetalLayer can be accessed from the view's 'layer' property.
   12.63 + *
   12.64 + *  \code
   12.65 + *  SDL_MetalView metalview = SDL_Metal_CreateView(window);
   12.66 + *  UIView *uiview = (__bridge UIView *)metalview;
   12.67 + *  CAMetalLayer *metallayer = (CAMetalLayer *)uiview.layer;
   12.68 + *  // [...]
   12.69 + *  SDL_Metal_DestroyView(metalview);
   12.70 + *  \endcode
   12.71 + *
   12.72 + *  \sa SDL_Metal_DestroyView
   12.73 + */
   12.74 +extern DECLSPEC SDL_MetalView SDLCALL SDL_Metal_CreateView(SDL_Window * window);
   12.75 +
   12.76 +/**
   12.77 + *  \brief Destroy an existing SDL_MetalView object.
   12.78 + *
   12.79 + *  This should be called before SDL_DestroyWindow, if SDL_Metal_CreateView was
   12.80 + *  called after SDL_CreateWindow.
   12.81 + *
   12.82 + *  \sa SDL_Metal_CreateView
   12.83 + */
   12.84 +extern DECLSPEC void SDLCALL SDL_Metal_DestroyView(SDL_MetalView view);
   12.85 +
   12.86 +/* @} *//* Metal support functions */
   12.87 +
   12.88 +/* Ends C function definitions when using C++ */
   12.89 +#ifdef __cplusplus
   12.90 +}
   12.91 +#endif
   12.92 +#include "close_code.h"
   12.93 +
   12.94 +#endif /* SDL_metal_h_ */
    13.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Sun Aug 04 23:30:55 2019 -0300
    13.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Mon Aug 05 12:35:32 2019 -0300
    13.3 @@ -724,3 +724,5 @@
    13.4  #define SDL_RWwrite SDL_RWwrite_REAL
    13.5  #define SDL_RWclose SDL_RWclose_REAL
    13.6  #define SDL_LoadFile SDL_LoadFile_REAL
    13.7 +#define SDL_Metal_CreateView SDL_Metal_CreateView_REAL
    13.8 +#define SDL_Metal_DestroyView SDL_Metal_DestroyView_REAL
    14.1 --- a/src/dynapi/SDL_dynapi_procs.h	Sun Aug 04 23:30:55 2019 -0300
    14.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Mon Aug 05 12:35:32 2019 -0300
    14.3 @@ -780,3 +780,5 @@
    14.4  SDL_DYNAPI_PROC(size_t,SDL_RWwrite,(SDL_RWops *a, const void *b, size_t c, size_t d),(a,b,c,d),return)
    14.5  SDL_DYNAPI_PROC(int,SDL_RWclose,(SDL_RWops *a),(a),return)
    14.6  SDL_DYNAPI_PROC(void*,SDL_LoadFile,(const char *a, size_t *b),(a,b),return)
    14.7 +SDL_DYNAPI_PROC(SDL_MetalView,SDL_Metal_CreateView,(SDL_Window *a),(a),return)
    14.8 +SDL_DYNAPI_PROC(void,SDL_Metal_DestroyView,(SDL_MetalView a),(a),)
    15.1 --- a/src/render/metal/SDL_render_metal.m	Sun Aug 04 23:30:55 2019 -0300
    15.2 +++ b/src/render/metal/SDL_render_metal.m	Mon Aug 05 12:35:32 2019 -0300
    15.3 @@ -26,17 +26,17 @@
    15.4  #include "SDL_log.h"
    15.5  #include "SDL_assert.h"
    15.6  #include "SDL_syswm.h"
    15.7 +#include "SDL_metal.h"
    15.8  #include "../SDL_sysrender.h"
    15.9  
   15.10 -#ifdef __MACOSX__
   15.11 -#include "../../video/cocoa/SDL_cocoametalview.h"
   15.12 -#else
   15.13 -#include "../../video/uikit/SDL_uikitmetalview.h"
   15.14 -#endif
   15.15  #include <Availability.h>
   15.16  #import <Metal/Metal.h>
   15.17  #import <QuartzCore/CAMetalLayer.h>
   15.18  
   15.19 +#ifdef __MACOSX__
   15.20 +#import <AppKit/NSView.h>
   15.21 +#endif
   15.22 +
   15.23  /* Regenerate these with build-metal-shaders.sh */
   15.24  #ifdef __MACOSX__
   15.25  #include "SDL_shaders_metal_osx.h"
   15.26 @@ -118,6 +118,7 @@
   15.27      @property (nonatomic, retain) id<MTLSamplerState> mtlsamplerlinear;
   15.28      @property (nonatomic, retain) id<MTLBuffer> mtlbufconstants;
   15.29      @property (nonatomic, retain) id<MTLBuffer> mtlbufquadindices;
   15.30 +    @property (nonatomic, assign) SDL_MetalView mtlview;
   15.31      @property (nonatomic, retain) CAMetalLayer *mtllayer;
   15.32      @property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
   15.33      @property (nonatomic, assign) METAL_ShaderPipelines *activepipelines;
   15.34 @@ -1475,6 +1476,8 @@
   15.35          }
   15.36  
   15.37          DestroyAllPipelines(data.allpipelines, data.pipelinescount);
   15.38 +
   15.39 +        SDL_Metal_DestroyView(data.mtlview);
   15.40      }
   15.41  
   15.42      SDL_free(renderer);
   15.43 @@ -1501,6 +1504,8 @@
   15.44      SDL_Renderer *renderer = NULL;
   15.45      METAL_RenderData *data = NULL;
   15.46      id<MTLDevice> mtldevice = nil;
   15.47 +    SDL_MetalView view = NULL;
   15.48 +    CAMetalLayer *layer = nil;
   15.49      SDL_SysWMinfo syswm;
   15.50  
   15.51      SDL_VERSION(&syswm.version);
   15.52 @@ -1527,26 +1532,36 @@
   15.53          return NULL;
   15.54      }
   15.55  
   15.56 +    view = SDL_Metal_CreateView(window);
   15.57 +
   15.58 +    if (view == NULL) {
   15.59 +        SDL_free(renderer);
   15.60 +        return NULL;
   15.61 +    }
   15.62 +
   15.63      // !!! FIXME: error checking on all of this.
   15.64      data = [[METAL_RenderData alloc] init];
   15.65  
   15.66 +    if (data == nil) {
   15.67 +        SDL_Metal_DestroyView(view);
   15.68 +        SDL_free(renderer);
   15.69 +        return NULL;
   15.70 +    }
   15.71 +
   15.72      renderer->driverdata = (void*)CFBridgingRetain(data);
   15.73      renderer->window = window;
   15.74  
   15.75 +    data.mtlview = view;
   15.76 +
   15.77  #ifdef __MACOSX__
   15.78 -    NSView *view = Cocoa_Mtl_AddMetalView(window);
   15.79 -    CAMetalLayer *layer = (CAMetalLayer *)[view layer];
   15.80 +    layer = (CAMetalLayer *)[(NSView *)view layer];
   15.81 +#else
   15.82 +    layer = (CAMetalLayer *)[(__bridge UIView *)view layer];
   15.83 +#endif
   15.84  
   15.85      layer.device = mtldevice;
   15.86  
   15.87 -    //layer.colorspace = nil;
   15.88 -
   15.89 -#else
   15.90 -    UIView *view = UIKit_Mtl_AddMetalView(window);
   15.91 -    CAMetalLayer *layer = (CAMetalLayer *)[view layer];
   15.92 -#endif
   15.93 -
   15.94 -    // Necessary for RenderReadPixels.
   15.95 +    /* Necessary for RenderReadPixels. */
   15.96      layer.framebufferOnly = NO;
   15.97  
   15.98      data.mtldevice = layer.device;
   15.99 @@ -1763,7 +1778,6 @@
  15.100      [mtlsamplerlinear release];
  15.101      [mtlbufconstants release];
  15.102      [mtlbufquadindices release];
  15.103 -    [view release];
  15.104      [data release];
  15.105      [mtldevice release];
  15.106  #endif
    16.1 --- a/src/video/SDL_sysvideo.h	Sun Aug 04 23:30:55 2019 -0300
    16.2 +++ b/src/video/SDL_sysvideo.h	Mon Aug 05 12:35:32 2019 -0300
    16.3 @@ -26,6 +26,7 @@
    16.4  #include "SDL_messagebox.h"
    16.5  #include "SDL_shape.h"
    16.6  #include "SDL_thread.h"
    16.7 +#include "SDL_metal.h"
    16.8  
    16.9  #include "SDL_vulkan_internal.h"
   16.10  
   16.11 @@ -276,6 +277,13 @@
   16.12  
   16.13      /* * * */
   16.14      /*
   16.15 +     * Metal support
   16.16 +     */
   16.17 +    SDL_MetalView (*Metal_CreateView) (_THIS, SDL_Window * window);
   16.18 +    void (*Metal_DestroyView) (_THIS, SDL_MetalView view);
   16.19 +
   16.20 +    /* * * */
   16.21 +    /*
   16.22       * Event manager functions
   16.23       */
   16.24      void (*PumpEvents) (_THIS);
    17.1 --- a/src/video/SDL_video.c	Sun Aug 04 23:30:55 2019 -0300
    17.2 +++ b/src/video/SDL_video.c	Mon Aug 05 12:35:32 2019 -0300
    17.3 @@ -4183,4 +4183,25 @@
    17.4      }
    17.5  }
    17.6  
    17.7 +SDL_MetalView
    17.8 +SDL_Metal_CreateView(SDL_Window * window)
    17.9 +{
   17.10 +    CHECK_WINDOW_MAGIC(window, NULL);
   17.11 +
   17.12 +    if (_this->Metal_CreateView) {
   17.13 +        return _this->Metal_CreateView(_this, window);
   17.14 +    } else {
   17.15 +        SDL_SetError("Metal is not supported.");
   17.16 +        return NULL;
   17.17 +    }
   17.18 +}
   17.19 +
   17.20 +void
   17.21 +SDL_Metal_DestroyView(SDL_MetalView view)
   17.22 +{
   17.23 +    if (_this && view && _this->Metal_DestroyView) {
   17.24 +        _this->Metal_DestroyView(_this, view);
   17.25 +    }
   17.26 +}
   17.27 +
   17.28  /* vi: set ts=4 sw=4 expandtab: */
    18.1 --- a/src/video/cocoa/SDL_cocoametalview.h	Sun Aug 04 23:30:55 2019 -0300
    18.2 +++ b/src/video/cocoa/SDL_cocoametalview.h	Mon Aug 05 12:35:32 2019 -0300
    18.3 @@ -31,7 +31,7 @@
    18.4  #import "../SDL_sysvideo.h"
    18.5  #import "SDL_cocoawindow.h"
    18.6  
    18.7 -#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL)
    18.8 +#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)
    18.9  
   18.10  #import <Cocoa/Cocoa.h>
   18.11  #import <Metal/Metal.h>
   18.12 @@ -51,11 +51,12 @@
   18.13  
   18.14  @end
   18.15  
   18.16 -SDL_cocoametalview* Cocoa_Mtl_AddMetalView(SDL_Window* window);
   18.17 +SDL_MetalView Cocoa_Metal_CreateView(_THIS, SDL_Window * window);
   18.18 +void Cocoa_Metal_DestroyView(_THIS, SDL_MetalView view);
   18.19  
   18.20 -void Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h);
   18.21 +void Cocoa_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h);
   18.22  
   18.23 -#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
   18.24 +#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
   18.25  
   18.26  #endif /* SDL_cocoametalview_h_ */
   18.27  
    19.1 --- a/src/video/cocoa/SDL_cocoametalview.m	Sun Aug 04 23:30:55 2019 -0300
    19.2 +++ b/src/video/cocoa/SDL_cocoametalview.m	Mon Aug 05 12:35:32 2019 -0300
    19.3 @@ -27,7 +27,7 @@
    19.4  
    19.5  #import "SDL_cocoametalview.h"
    19.6  
    19.7 -#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL)
    19.8 +#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)
    19.9  
   19.10  #include "SDL_assert.h"
   19.11  
   19.12 @@ -100,22 +100,38 @@
   19.13  
   19.14  @end
   19.15  
   19.16 -SDL_cocoametalview*
   19.17 -Cocoa_Mtl_AddMetalView(SDL_Window* window)
   19.18 -{
   19.19 +SDL_MetalView
   19.20 +Cocoa_Metal_CreateView(_THIS, SDL_Window * window)
   19.21 +{ @autoreleasepool {
   19.22      SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
   19.23      NSView *view = data->nswindow.contentView;
   19.24      BOOL highDPI = (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) != 0;
   19.25 -    SDL_cocoametalview *metalview;
   19.26 +    SDL_cocoametalview *newview;
   19.27 +    SDL_MetalView metalview;
   19.28  
   19.29 -    metalview = [[SDL_cocoametalview alloc] initWithFrame:view.frame highDPI:highDPI];
   19.30 -    [view addSubview:metalview];
   19.31 +    newview = [[SDL_cocoametalview alloc] initWithFrame:view.frame highDPI:highDPI];
   19.32 +    if (newview == nil) {
   19.33 +        return NULL;
   19.34 +    }
   19.35 +
   19.36 +    [view addSubview:newview];
   19.37 +
   19.38 +    metalview = (SDL_MetalView)CFBridgingRetain(newview);
   19.39 +    [newview release];
   19.40 +
   19.41      return metalview;
   19.42 -}
   19.43 +}}
   19.44  
   19.45  void
   19.46 -Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
   19.47 -{
   19.48 +Cocoa_Metal_DestroyView(_THIS, SDL_MetalView view)
   19.49 +{ @autoreleasepool {
   19.50 +    SDL_cocoametalview *metalview = CFBridgingRelease(view);
   19.51 +    [metalview removeFromSuperview];
   19.52 +}}
   19.53 +
   19.54 +void
   19.55 +Cocoa_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h)
   19.56 +{ @autoreleasepool {
   19.57      SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   19.58      NSView *view = data->nswindow.contentView;
   19.59      SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
   19.60 @@ -131,8 +147,8 @@
   19.61      } else {
   19.62          SDL_GetWindowSize(window, w, h);
   19.63      }
   19.64 -}
   19.65 +}}
   19.66  
   19.67 -#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
   19.68 +#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
   19.69  
   19.70  /* vi: set ts=4 sw=4 expandtab: */
    20.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Sun Aug 04 23:30:55 2019 -0300
    20.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Mon Aug 05 12:35:32 2019 -0300
    20.3 @@ -27,6 +27,7 @@
    20.4  #include "SDL_cocoavideo.h"
    20.5  #include "SDL_cocoashape.h"
    20.6  #include "SDL_cocoavulkan.h"
    20.7 +#include "SDL_cocoametalview.h"
    20.8  #include "SDL_assert.h"
    20.9  
   20.10  /* Initialization/Query functions */
   20.11 @@ -142,6 +143,11 @@
   20.12      device->Vulkan_GetDrawableSize = Cocoa_Vulkan_GetDrawableSize;
   20.13  #endif
   20.14  
   20.15 +#if SDL_VIDEO_METAL
   20.16 +    device->Metal_CreateView = Cocoa_Metal_CreateView;
   20.17 +    device->Metal_DestroyView = Cocoa_Metal_DestroyView;
   20.18 +#endif
   20.19 +
   20.20      device->StartTextInput = Cocoa_StartTextInput;
   20.21      device->StopTextInput = Cocoa_StopTextInput;
   20.22      device->SetTextInputRect = Cocoa_SetTextInputRect;
    21.1 --- a/src/video/cocoa/SDL_cocoavulkan.m	Sun Aug 04 23:30:55 2019 -0300
    21.2 +++ b/src/video/cocoa/SDL_cocoavulkan.m	Mon Aug 05 12:35:32 2019 -0300
    21.3 @@ -194,6 +194,7 @@
    21.4                                              "vkCreateMacOSSurfaceMVK");
    21.5      VkMacOSSurfaceCreateInfoMVK createInfo = {};
    21.6      VkResult result;
    21.7 +    SDL_MetalView metalview;
    21.8  
    21.9      if (!_this->vulkan_config.loader_handle) {
   21.10          SDL_SetError("Vulkan is not loaded");
   21.11 @@ -205,23 +206,38 @@
   21.12                       " extension is not enabled in the Vulkan instance.");
   21.13          return SDL_FALSE;
   21.14      }
   21.15 +
   21.16 +    metalview = Cocoa_Metal_CreateView(_this, window);
   21.17 +    if (metalview == NULL) {
   21.18 +        return SDL_FALSE;
   21.19 +    }
   21.20 +
   21.21      createInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
   21.22      createInfo.pNext = NULL;
   21.23      createInfo.flags = 0;
   21.24 -    createInfo.pView = Cocoa_Mtl_AddMetalView(window);
   21.25 +    createInfo.pView = (const void *)metalview;
   21.26      result = vkCreateMacOSSurfaceMVK(instance, &createInfo,
   21.27                                         NULL, surface);
   21.28      if (result != VK_SUCCESS) {
   21.29 +        Cocoa_Metal_DestroyView(_this, metalview);
   21.30          SDL_SetError("vkCreateMacOSSurfaceMVK failed: %s",
   21.31                       SDL_Vulkan_GetResultString(result));
   21.32          return SDL_FALSE;
   21.33      }
   21.34 +
   21.35 +    /* Unfortunately there's no SDL_Vulkan_DestroySurface function we can call
   21.36 +     * Metal_DestroyView from. Right now the metal view's ref count is +2 (one
   21.37 +     * from returning a new view object in CreateView, and one because it's
   21.38 +     * a subview of the window.) If we release the view here to make it +1, it
   21.39 +     * will be destroyed when the window is destroyed. */
   21.40 +    CFBridgingRelease(metalview);
   21.41 +
   21.42      return SDL_TRUE;
   21.43  }
   21.44  
   21.45  void Cocoa_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
   21.46  {
   21.47 -    Cocoa_Mtl_GetDrawableSize(window, w, h);
   21.48 +    Cocoa_Metal_GetDrawableSize(window, w, h);
   21.49  }
   21.50  
   21.51  #endif
    22.1 --- a/src/video/uikit/SDL_uikitmetalview.h	Sun Aug 04 23:30:55 2019 -0300
    22.2 +++ b/src/video/uikit/SDL_uikitmetalview.h	Mon Aug 05 12:35:32 2019 -0300
    22.3 @@ -29,10 +29,10 @@
    22.4  #ifndef SDL_uikitmetalview_h_
    22.5  #define SDL_uikitmetalview_h_
    22.6  
    22.7 -#import "../SDL_sysvideo.h"
    22.8 -#import "SDL_uikitwindow.h"
    22.9 +#include "../SDL_sysvideo.h"
   22.10 +#include "SDL_uikitwindow.h"
   22.11  
   22.12 -#if SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_RENDER_METAL || SDL_VIDEO_VULKAN)
   22.13 +#if SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)
   22.14  
   22.15  #import <UIKit/UIKit.h>
   22.16  #import <Metal/Metal.h>
   22.17 @@ -47,11 +47,12 @@
   22.18  
   22.19  @end
   22.20  
   22.21 -SDL_uikitmetalview* UIKit_Mtl_AddMetalView(SDL_Window* window);
   22.22 +SDL_MetalView UIKit_Metal_CreateView(_THIS, SDL_Window * window);
   22.23 +void UIKit_Metal_DestroyView(_THIS, SDL_MetalView view);
   22.24  
   22.25 -void UIKit_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h);
   22.26 +void UIKit_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h);
   22.27  
   22.28 -#endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_RENDER_METAL || SDL_VIDEO_VULKAN) */
   22.29 +#endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
   22.30  
   22.31  #endif /* SDL_uikitmetalview_h_ */
   22.32  
    23.1 --- a/src/video/uikit/SDL_uikitmetalview.m	Sun Aug 04 23:30:55 2019 -0300
    23.2 +++ b/src/video/uikit/SDL_uikitmetalview.m	Mon Aug 05 12:35:32 2019 -0300
    23.3 @@ -28,7 +28,7 @@
    23.4  
    23.5  #include "../../SDL_internal.h"
    23.6  
    23.7 -#if SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_RENDER_METAL || SDL_VIDEO_VULKAN)
    23.8 +#if SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)
    23.9  
   23.10  #import "../SDL_sysvideo.h"
   23.11  #import "SDL_uikitwindow.h"
   23.12 @@ -73,16 +73,12 @@
   23.13  
   23.14  @end
   23.15  
   23.16 -SDL_uikitmetalview*
   23.17 -UIKit_Mtl_AddMetalView(SDL_Window* window)
   23.18 -{
   23.19 +SDL_MetalView
   23.20 +UIKit_Metal_CreateView(_THIS, SDL_Window * window)
   23.21 +{ @autoreleasepool {
   23.22      SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   23.23 -    SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
   23.24      CGFloat scale = 1.0;
   23.25 -
   23.26 -    if ([view isKindOfClass:[SDL_uikitmetalview class]]) {
   23.27 -        return (SDL_uikitmetalview *)view;
   23.28 -    }
   23.29 +    SDL_uikitmetalview *metalview;
   23.30  
   23.31      if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   23.32          /* Set the scale to the natural scale factor of the screen - then
   23.33 @@ -96,16 +92,26 @@
   23.34              scale = data.uiwindow.screen.scale;
   23.35          }
   23.36      }
   23.37 -    SDL_uikitmetalview *metalview
   23.38 -         = [[SDL_uikitmetalview alloc] initWithFrame:view.frame
   23.39 -                                               scale:scale];
   23.40 +
   23.41 +    metalview = [[SDL_uikitmetalview alloc] initWithFrame:data.uiwindow.bounds
   23.42 +                                                    scale:scale];
   23.43      [metalview setSDLWindow:window];
   23.44  
   23.45 -    return metalview;
   23.46 -}
   23.47 +    return (void*)CFBridgingRetain(metalview);
   23.48 +}}
   23.49  
   23.50  void
   23.51 -UIKit_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
   23.52 +UIKit_Metal_DestroyView(_THIS, SDL_MetalView view)
   23.53 +{ @autoreleasepool {
   23.54 +    SDL_uikitmetalview *metalview = CFBridgingRelease(view);
   23.55 +
   23.56 +    if ([metalview isKindOfClass:[SDL_uikitmetalview class]]) {
   23.57 +        [metalview setSDLWindow:NULL];
   23.58 +    }
   23.59 +}}
   23.60 +
   23.61 +void
   23.62 +UIKit_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h)
   23.63  {
   23.64      @autoreleasepool {
   23.65          SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   23.66 @@ -126,4 +132,4 @@
   23.67      }
   23.68  }
   23.69  
   23.70 -#endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_RENDER_METAL || SDL_VIDEO_VULKAN) */
   23.71 +#endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
    24.1 --- a/src/video/uikit/SDL_uikitvideo.m	Sun Aug 04 23:30:55 2019 -0300
    24.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Mon Aug 05 12:35:32 2019 -0300
    24.3 @@ -38,6 +38,7 @@
    24.4  #include "SDL_uikitopengles.h"
    24.5  #include "SDL_uikitclipboard.h"
    24.6  #include "SDL_uikitvulkan.h"
    24.7 +#include "SDL_uikitmetalview.h"
    24.8  
    24.9  #define UIKITVID_DRIVER_NAME "uikit"
   24.10  
   24.11 @@ -133,6 +134,11 @@
   24.12          device->Vulkan_GetDrawableSize = UIKit_Vulkan_GetDrawableSize;
   24.13  #endif
   24.14  
   24.15 +#if SDL_VIDEO_METAL
   24.16 +        device->Metal_CreateView = UIKit_Metal_CreateView;
   24.17 +        device->Metal_DestroyView = UIKit_Metal_DestroyView;
   24.18 +#endif
   24.19 +
   24.20          device->gl_config.accelerated = 1;
   24.21  
   24.22          return device;
    25.1 --- a/src/video/uikit/SDL_uikitvulkan.m	Sun Aug 04 23:30:55 2019 -0300
    25.2 +++ b/src/video/uikit/SDL_uikitvulkan.m	Mon Aug 05 12:35:32 2019 -0300
    25.3 @@ -200,6 +200,7 @@
    25.4                                              "vkCreateIOSSurfaceMVK");
    25.5      VkIOSSurfaceCreateInfoMVK createInfo = {};
    25.6      VkResult result;
    25.7 +    SDL_MetalView metalview;
    25.8  
    25.9      if (!_this->vulkan_config.loader_handle) {
   25.10          SDL_SetError("Vulkan is not loaded");
   25.11 @@ -212,24 +213,37 @@
   25.12          return SDL_FALSE;
   25.13      }
   25.14  
   25.15 +    metalview = UIKit_Metal_CreateView(_this, window);
   25.16 +    if (metalview == NULL) {
   25.17 +        return SDL_FALSE;
   25.18 +    }
   25.19 +
   25.20      createInfo.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
   25.21      createInfo.pNext = NULL;
   25.22      createInfo.flags = 0;
   25.23 -    createInfo.pView = (__bridge void *)UIKit_Mtl_AddMetalView(window);
   25.24 +    createInfo.pView = (const void *)metalview;
   25.25      result = vkCreateIOSSurfaceMVK(instance, &createInfo,
   25.26                                         NULL, surface);
   25.27      if (result != VK_SUCCESS) {
   25.28 +        UIKit_Metal_DestroyView(_this, metalview);
   25.29          SDL_SetError("vkCreateIOSSurfaceMVK failed: %s",
   25.30                       SDL_Vulkan_GetResultString(result));
   25.31          return SDL_FALSE;
   25.32      }
   25.33  
   25.34 +    /* Unfortunately there's no SDL_Vulkan_DestroySurface function we can call
   25.35 +     * Metal_DestroyView from. Right now the metal view's ref count is +2 (one
   25.36 +     * from returning a new view object in CreateView, and one because it's
   25.37 +     * a subview of the window.) If we release the view here to make it +1, it
   25.38 +     * will be destroyed when the window is destroyed. */
   25.39 +    CFBridgingRelease(metalview);
   25.40 +
   25.41      return SDL_TRUE;
   25.42  }
   25.43  
   25.44  void UIKit_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
   25.45  {
   25.46 -    UIKit_Mtl_GetDrawableSize(window, w, h);
   25.47 +    UIKit_Metal_GetDrawableSize(window, w, h);
   25.48  }
   25.49  
   25.50  #endif