Atlas - SDL_androidvulkan.c

Home / ext / SDL / src / video / android Lines: 1 | Size: 6592 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22/* 23 * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's 24 * SDL_x11vulkan.c. 25 */ 26 27#include "SDL_internal.h" 28 29#if defined(SDL_VIDEO_VULKAN) && defined(SDL_VIDEO_DRIVER_ANDROID) 30 31#include "../SDL_vulkan_internal.h" 32 33#include "SDL_androidvideo.h" 34#include "SDL_androidwindow.h" 35 36#include "SDL_androidvulkan.h" 37 38 39bool Android_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path) 40{ 41 VkExtensionProperties *extensions = NULL; 42 Uint32 i, extensionCount = 0; 43 bool hasSurfaceExtension = false; 44 bool hasAndroidSurfaceExtension = false; 45 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; 46 if (_this->vulkan_config.loader_handle) { 47 return SDL_SetError("Vulkan already loaded"); 48 } 49 50 // Load the Vulkan loader library 51 if (!path) { 52 path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY); 53 } 54 if (!path) { 55 path = "libvulkan.so"; 56 } 57 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 58 if (!_this->vulkan_config.loader_handle) { 59 return false; 60 } 61 SDL_strlcpy(_this->vulkan_config.loader_path, path, 62 SDL_arraysize(_this->vulkan_config.loader_path)); 63 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( 64 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); 65 if (!vkGetInstanceProcAddr) { 66 goto fail; 67 } 68 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr; 69 _this->vulkan_config.vkEnumerateInstanceExtensionProperties = 70 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( 71 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); 72 if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) { 73 goto fail; 74 } 75 extensions = SDL_Vulkan_CreateInstanceExtensionsList( 76 (PFN_vkEnumerateInstanceExtensionProperties) 77 _this->vulkan_config.vkEnumerateInstanceExtensionProperties, 78 &extensionCount); 79 if (!extensions) { 80 goto fail; 81 } 82 for (i = 0; i < extensionCount; i++) { 83 if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 84 hasSurfaceExtension = true; 85 } else if (SDL_strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 86 hasAndroidSurfaceExtension = true; 87 } 88 } 89 SDL_free(extensions); 90 if (!hasSurfaceExtension) { 91 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension"); 92 goto fail; 93 } else if (!hasAndroidSurfaceExtension) { 94 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME " extension"); 95 goto fail; 96 } 97 return true; 98 99fail: 100 SDL_UnloadObject(_this->vulkan_config.loader_handle); 101 _this->vulkan_config.loader_handle = NULL; 102 return false; 103} 104 105void Android_Vulkan_UnloadLibrary(SDL_VideoDevice *_this) 106{ 107 if (_this->vulkan_config.loader_handle) { 108 SDL_UnloadObject(_this->vulkan_config.loader_handle); 109 _this->vulkan_config.loader_handle = NULL; 110 } 111} 112 113char const * const *Android_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, Uint32 *count) 114{ 115 static const char *const extensionsForAndroid[] = { 116 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME 117 }; 118 if (count) { 119 *count = SDL_arraysize(extensionsForAndroid); 120 } 121 return extensionsForAndroid; 122} 123 124bool Android_Vulkan_CreateSurface(SDL_VideoDevice *_this, 125 SDL_Window *window, 126 VkInstance instance, 127 const struct VkAllocationCallbacks *allocator, 128 VkSurfaceKHR *surface) 129{ 130 SDL_WindowData *windowData = window->internal; 131 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 132 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 133 PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR = 134 (PFN_vkCreateAndroidSurfaceKHR)vkGetInstanceProcAddr( 135 instance, 136 "vkCreateAndroidSurfaceKHR"); 137 VkAndroidSurfaceCreateInfoKHR createInfo; 138 VkResult result; 139 140 if (!_this->vulkan_config.loader_handle) { 141 return SDL_SetError("Vulkan is not loaded"); 142 } 143 144 if (!vkCreateAndroidSurfaceKHR) { 145 return SDL_SetError(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME 146 " extension is not enabled in the Vulkan instance."); 147 } 148 149 SDL_zero(createInfo); 150 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; 151 createInfo.pNext = NULL; 152 createInfo.flags = 0; 153 createInfo.window = windowData->native_window; 154 result = vkCreateAndroidSurfaceKHR(instance, &createInfo, allocator, surface); 155 if (result != VK_SUCCESS) { 156 if (result == VK_ERROR_NATIVE_WINDOW_IN_USE_KHR) { 157 return SDL_SetError("vkCreateAndroidSurfaceKHR failed: %s, was the window created with SDL_WINDOW_VULKAN?", SDL_Vulkan_GetResultString(result)); 158 } else { 159 return SDL_SetError("vkCreateAndroidSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result)); 160 } 161 } 162 return true; 163} 164 165void Android_Vulkan_DestroySurface(SDL_VideoDevice *_this, 166 VkInstance instance, 167 VkSurfaceKHR surface, 168 const struct VkAllocationCallbacks *allocator) 169{ 170 if (_this->vulkan_config.loader_handle) { 171 SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator); 172 } 173} 174 175#endif 176
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.