2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
23 * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's
27 #include "../../SDL_internal.h"
29 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_ANDROID
31 #include "SDL_androidvideo.h"
32 #include "SDL_androidwindow.h"
33 #include "SDL_assert.h"
35 #include "SDL_loadso.h"
36 #include "SDL_androidvulkan.h"
37 #include "SDL_syswm.h"
39 int Android_Vulkan_LoadLibrary(_THIS, const char *path)
41 VkExtensionProperties *extensions = NULL;
42 Uint32 i, extensionCount = 0;
43 SDL_bool hasSurfaceExtension = SDL_FALSE;
44 SDL_bool hasAndroidSurfaceExtension = SDL_FALSE;
45 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
46 if(_this->vulkan_config.loader_handle)
47 return SDL_SetError("Vulkan already loaded");
49 /* Load the Vulkan loader library */
51 path = SDL_getenv("SDL_VULKAN_LIBRARY");
53 path = "libvulkan.so";
54 _this->vulkan_config.loader_handle = SDL_LoadObject(path);
55 if(!_this->vulkan_config.loader_handle)
57 SDL_strlcpy(_this->vulkan_config.loader_path, path,
58 SDL_arraysize(_this->vulkan_config.loader_path));
59 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
60 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
61 if(!vkGetInstanceProcAddr)
63 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
64 _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
65 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
66 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
67 if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties)
69 extensions = SDL_Vulkan_CreateInstanceExtensionsList(
70 (PFN_vkEnumerateInstanceExtensionProperties)
71 _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
75 for(i = 0; i < extensionCount; i++)
77 if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
78 hasSurfaceExtension = SDL_TRUE;
79 else if(SDL_strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
80 hasAndroidSurfaceExtension = SDL_TRUE;
83 if(!hasSurfaceExtension)
85 SDL_SetError("Installed Vulkan doesn't implement the "
86 VK_KHR_SURFACE_EXTENSION_NAME " extension");
89 else if(!hasAndroidSurfaceExtension)
91 SDL_SetError("Installed Vulkan doesn't implement the "
92 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME "extension");
98 SDL_UnloadObject(_this->vulkan_config.loader_handle);
99 _this->vulkan_config.loader_handle = NULL;
103 void Android_Vulkan_UnloadLibrary(_THIS)
105 if(_this->vulkan_config.loader_handle)
107 SDL_UnloadObject(_this->vulkan_config.loader_handle);
108 _this->vulkan_config.loader_handle = NULL;
112 SDL_bool Android_Vulkan_GetInstanceExtensions(_THIS,
117 static const char *const extensionsForAndroid[] = {
118 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
120 if(!_this->vulkan_config.loader_handle)
122 SDL_SetError("Vulkan is not loaded");
125 return SDL_Vulkan_GetInstanceExtensions_Helper(
126 count, names, SDL_arraysize(extensionsForAndroid),
127 extensionsForAndroid);
130 SDL_bool Android_Vulkan_CreateSurface(_THIS,
133 VkSurfaceKHR *surface)
135 SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
136 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
137 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
138 PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR =
139 (PFN_vkCreateAndroidSurfaceKHR)vkGetInstanceProcAddr(
140 (VkInstance)instance,
141 "vkCreateAndroidSurfaceKHR");
142 VkAndroidSurfaceCreateInfoKHR createInfo;
145 if(!_this->vulkan_config.loader_handle)
147 SDL_SetError("Vulkan is not loaded");
151 if(!vkCreateAndroidSurfaceKHR)
153 SDL_SetError(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
154 " extension is not enabled in the Vulkan instance.");
157 SDL_zero(createInfo);
158 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
159 createInfo.pNext = NULL;
160 createInfo.flags = 0;
161 createInfo.window = windowData->native_window;
162 result = vkCreateAndroidSurfaceKHR(instance, &createInfo,
164 if(result != VK_SUCCESS)
166 SDL_SetError("vkCreateAndroidSurfaceKHR failed: %s",
167 SDL_Vulkan_GetResultString(result));
175 /* vi: set ts=4 sw=4 expandtab: */