1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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 Wladimir J. van der Laan. Based on Jacob Lifshay's
24  * SDL_x11vulkan.c, Mark Callow's SDL_androidvulkan.c, and
25  * the FSL demo framework.
26  */
27 
28 #include "../../SDL_internal.h"
29 
30 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_VIVANTE
31 
32 #include "SDL_vivantevideo.h"
33 #include "SDL_assert.h"
34 
35 #include "SDL_loadso.h"
36 #include "SDL_vivantevulkan.h"
37 #include "SDL_syswm.h"
38 
VIVANTE_Vulkan_LoadLibrary(_THIS,const char * path)39 int VIVANTE_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 hasDisplayExtension = SDL_FALSE;
45     PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
46     if(_this->vulkan_config.loader_handle)
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     {
54         /* If no path set, try Vivante fb vulkan driver explicitly */
55         path = "libvulkan-fb.so";
56         _this->vulkan_config.loader_handle = SDL_LoadObject(path);
57         if(!_this->vulkan_config.loader_handle)
58         {
59             /* If that couldn't be loaded, fall back to default name */
60             path = "libvulkan.so";
61             _this->vulkan_config.loader_handle = SDL_LoadObject(path);
62         }
63     } else {
64         _this->vulkan_config.loader_handle = SDL_LoadObject(path);
65     }
66     if(!_this->vulkan_config.loader_handle)
67         return -1;
68     SDL_strlcpy(_this->vulkan_config.loader_path, path,
69                 SDL_arraysize(_this->vulkan_config.loader_path));
70     SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "vivante: Loaded vulkan driver %s", path);
71     vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
72         _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
73     if(!vkGetInstanceProcAddr)
74         goto fail;
75     _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
76     _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
77         (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
78             VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
79     if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties)
80         goto fail;
81     extensions = SDL_Vulkan_CreateInstanceExtensionsList(
82         (PFN_vkEnumerateInstanceExtensionProperties)
83             _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
84         &extensionCount);
85     if(!extensions)
86         goto fail;
87     for(i = 0; i < extensionCount; i++)
88     {
89         if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
90             hasSurfaceExtension = SDL_TRUE;
91         else if(SDL_strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, extensions[i].extensionName) == 0)
92             hasDisplayExtension = SDL_TRUE;
93     }
94     SDL_free(extensions);
95     if(!hasSurfaceExtension)
96     {
97         SDL_SetError("Installed Vulkan doesn't implement the "
98                      VK_KHR_SURFACE_EXTENSION_NAME " extension");
99         goto fail;
100     }
101     else if(!hasDisplayExtension)
102     {
103         SDL_SetError("Installed Vulkan doesn't implement the "
104                      VK_KHR_DISPLAY_EXTENSION_NAME "extension");
105         goto fail;
106     }
107     return 0;
108 
109 fail:
110     SDL_UnloadObject(_this->vulkan_config.loader_handle);
111     _this->vulkan_config.loader_handle = NULL;
112     return -1;
113 }
114 
VIVANTE_Vulkan_UnloadLibrary(_THIS)115 void VIVANTE_Vulkan_UnloadLibrary(_THIS)
116 {
117     if(_this->vulkan_config.loader_handle)
118     {
119         SDL_UnloadObject(_this->vulkan_config.loader_handle);
120         _this->vulkan_config.loader_handle = NULL;
121     }
122 }
123 
VIVANTE_Vulkan_GetInstanceExtensions(_THIS,SDL_Window * window,unsigned * count,const char ** names)124 SDL_bool VIVANTE_Vulkan_GetInstanceExtensions(_THIS,
125                                           SDL_Window *window,
126                                           unsigned *count,
127                                           const char **names)
128 {
129     static const char *const extensionsForVivante[] = {
130         VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_DISPLAY_EXTENSION_NAME
131     };
132     if(!_this->vulkan_config.loader_handle)
133     {
134         SDL_SetError("Vulkan is not loaded");
135         return SDL_FALSE;
136     }
137     return SDL_Vulkan_GetInstanceExtensions_Helper(
138             count, names, SDL_arraysize(extensionsForVivante),
139             extensionsForVivante);
140 }
141 
VIVANTE_Vulkan_CreateSurface(_THIS,SDL_Window * window,VkInstance instance,VkSurfaceKHR * surface)142 SDL_bool VIVANTE_Vulkan_CreateSurface(_THIS,
143                                   SDL_Window *window,
144                                   VkInstance instance,
145                                   VkSurfaceKHR *surface)
146 {
147     if(!_this->vulkan_config.loader_handle)
148     {
149         SDL_SetError("Vulkan is not loaded");
150         return SDL_FALSE;
151     }
152     return SDL_Vulkan_Display_CreateSurface(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface);
153 }
154 
155 #endif
156 
157 /* vi: set ts=4 sw=4 expandtab: */
158 
159