SDL  2.0
SDL_androidvulkan.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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 SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_ANDROID
30 
31 #include "SDL_androidvideo.h"
32 #include "SDL_androidwindow.h"
33 #include "SDL_assert.h"
34 
35 #include "SDL_loadso.h"
36 #include "SDL_androidvulkan.h"
37 #include "SDL_syswm.h"
38 
39 int Android_Vulkan_LoadLibrary(_THIS, const char *path)
40 {
41  VkExtensionProperties *extensions = NULL;
42  Uint32 i, extensionCount = 0;
43  SDL_bool hasSurfaceExtension = SDL_FALSE;
44  SDL_bool hasAndroidSurfaceExtension = SDL_FALSE;
47  return SDL_SetError("Vulkan already loaded");
48 
49  /* Load the Vulkan loader library */
50  if(!path)
51  path = SDL_getenv("SDL_VULKAN_LIBRARY");
52  if(!path)
53  path = "libvulkan.so";
56  return -1;
60  _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
62  goto fail;
66  VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
68  goto fail;
69  extensions = SDL_Vulkan_CreateInstanceExtensionsList(
72  &extensionCount);
73  if(!extensions)
74  goto fail;
75  for(i = 0; i < extensionCount; i++)
76  {
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;
81  }
82  SDL_free(extensions);
83  if(!hasSurfaceExtension)
84  {
85  SDL_SetError("Installed Vulkan doesn't implement the "
86  VK_KHR_SURFACE_EXTENSION_NAME " extension");
87  goto fail;
88  }
89  else if(!hasAndroidSurfaceExtension)
90  {
91  SDL_SetError("Installed Vulkan doesn't implement the "
92  VK_KHR_ANDROID_SURFACE_EXTENSION_NAME "extension");
93  goto fail;
94  }
95  return 0;
96 
97 fail:
100  return -1;
101 }
102 
103 void Android_Vulkan_UnloadLibrary(_THIS)
104 {
106  {
109  }
110 }
111 
112 SDL_bool Android_Vulkan_GetInstanceExtensions(_THIS,
114  unsigned *count,
115  const char **names)
116 {
117  static const char *const extensionsForAndroid[] = {
118  VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
119  };
121  {
122  SDL_SetError("Vulkan is not loaded");
123  return SDL_FALSE;
124  }
125  return SDL_Vulkan_GetInstanceExtensions_Helper(
126  count, names, SDL_arraysize(extensionsForAndroid),
127  extensionsForAndroid);
128 }
129 
130 SDL_bool Android_Vulkan_CreateSurface(_THIS,
132  VkInstance instance,
133  VkSurfaceKHR *surface)
134 {
135  SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
138  PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR =
139  (PFN_vkCreateAndroidSurfaceKHR)vkGetInstanceProcAddr(
140  (VkInstance)instance,
141  "vkCreateAndroidSurfaceKHR");
142  VkAndroidSurfaceCreateInfoKHR createInfo;
144 
146  {
147  SDL_SetError("Vulkan is not loaded");
148  return SDL_FALSE;
149  }
150 
151  if(!vkCreateAndroidSurfaceKHR)
152  {
153  SDL_SetError(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
154  " extension is not enabled in the Vulkan instance.");
155  return SDL_FALSE;
156  }
157  SDL_zero(createInfo);
159  createInfo.pNext = NULL;
160  createInfo.flags = 0;
161  createInfo.window = windowData->native_window;
162  result = vkCreateAndroidSurfaceKHR(instance, &createInfo,
163  NULL, surface);
164  if(result != VK_SUCCESS)
165  {
166  SDL_SetError("vkCreateAndroidSurfaceKHR failed: %s",
167  SDL_Vulkan_GetResultString(result));
168  return SDL_FALSE;
169  }
170  return SDL_TRUE;
171 }
172 
173 #endif
174 
175 /* vi: set ts=4 sw=4 expandtab: */
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
SDL_VideoDevice::vkGetInstanceProcAddr
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr
Definition: SDL_sysvideo.h:372
NULL
#define NULL
Definition: begin_code.h:164
surface
EGLSurface surface
Definition: eglext.h:248
SDL_WindowData
Definition: SDL_androidwindow.h:36
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
SDL_androidvideo.h
SDL_androidvulkan.h
SDL_UnloadObject
#define SDL_UnloadObject
Definition: SDL_dynapi_overrides.h:234
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3730
SDL_WindowData::native_window
ANativeWindow * native_window
Definition: SDL_androidwindow.h:39
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9432
SDL_LoadObject
#define SDL_LoadObject
Definition: SDL_dynapi_overrides.h:232
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
SDL_androidwindow.h
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
VK_SUCCESS
@ VK_SUCCESS
Definition: vulkan.h:123
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_VideoDevice::loader_handle
void * loader_handle
Definition: SDL_sysvideo.h:376
PFN_vkGetInstanceProcAddr
PFN_vkVoidFunction(VKAPI_PTR * PFN_vkGetInstanceProcAddr)(VkInstance instance, const char *pName)
Definition: vulkan.h:2484
VK_NULL_HANDLE
#define VK_NULL_HANDLE
Definition: vulkan.h:49
VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR
@ VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR
Definition: vulkan.h:215
SDL_VideoDevice::loader_path
char loader_path[256]
Definition: SDL_sysvideo.h:375
SDL_assert.h
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
names
GLuint GLuint * names
Definition: SDL_opengl_glext.h:4956
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_VideoDevice::vkEnumerateInstanceExtensionProperties
PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties
Definition: SDL_sysvideo.h:373
PFN_vkEnumerateInstanceExtensionProperties
VkResult(VKAPI_PTR * PFN_vkEnumerateInstanceExtensionProperties)(const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
Definition: vulkan.h:2488
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
VkResult
VkResult
Definition: vulkan.h:122
SDL_getenv
#define SDL_getenv
Definition: SDL_dynapi_overrides.h:378
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_LoadFunction
void * SDL_LoadFunction(void *handle, const char *name)
VK_KHR_SURFACE_EXTENSION_NAME
#define VK_KHR_SURFACE_EXTENSION_NAME
Definition: vulkan.h:3416
SDL_VideoDevice::vulkan_config
struct SDL_VideoDevice::@34 vulkan_config
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
VkExtensionProperties
Definition: vulkan.h:1682
SDL_strcmp
#define SDL_strcmp
Definition: SDL_dynapi_overrides.h:417
SDL_loadso.h
vkGetInstanceProcAddr
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *pName)
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_syswm.h