SDL_androidvulkan.c 6.4 KB

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