Change std:vector to eina_array
[platform/upstream/SDL.git] / src / video / tizen / SDL_tizenvulkan.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4   Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
5
6   This software is provided 'as-is', without any express or implied
7   warranty.  In no event will the authors be held liable for any damages
8   arising from the use of this software.
9
10   Permission is granted to anyone to use this software for any purpose,
11   including commercial applications, and to alter it and redistribute it
12   freely, subject to the following restrictions:
13
14   1. The origin of this software must not be misrepresented; you must not
15      claim that you wrote the original software. If you use this software
16      in a product, an acknowledgment in the product documentation would be
17      appreciated but is not required.
18   2. Altered source versions must be plainly marked as such, and must not be
19      misrepresented as being the original software.
20   3. This notice may not be removed or altered from any source distribution.
21 */
22 #include "../../SDL_internal.h"
23
24 #include "../SDL_sysvideo.h"
25
26 #include "SDL_syswm.h"
27 #include "SDL_video.h"
28 #include "SDL_loadso.h"
29
30 #include "SDL_tizenwindow.h"
31 #include "SDL_tizenvideo.h"
32 #include "SDL_tizenvulkan.h"
33 #include "SDL_log.h"
34
35 #if SDL_VIDEO_VULKAN
36 #define DEFAULT_VULKAN "libvulkan.so"
37
38 #define LOAD_FUNC(NAME) \
39 _this->vk_data->NAME = SDL_LoadFunction(_this->vk_data->vk_dll_handle, #NAME); \
40 if (!_this->vk_data->NAME) \
41 { \
42     SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Could not retrieve Vulkan function " #NAME); \
43     return -1; \
44 }
45
46 SDL_bool
47 Tizen_vulkan_GetInstanceExtensions(_THIS, const char* driver, unsigned int* count, char** names)
48 {
49     uint32_t instance_extension_count = 0;
50     uint32_t enabled_extension_count = 0;
51     VkResult err;
52
53     if (strcmp(driver, "tizen")) {
54         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Unsupported video driver '%s'", driver);
55         return SDL_FALSE;
56     }
57
58     if (!names) *count = 0;
59
60     err = _this->vk_data->vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
61     if (err < 0) {
62         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Fail to get Instance extension");
63         return SDL_FALSE;
64     }
65
66     if (instance_extension_count > 0) {
67         uint32_t i;
68         VkExtensionProperties *instance_extensions =
69             malloc(sizeof(VkExtensionProperties) * instance_extension_count);
70         err = _this->vk_data->vkEnumerateInstanceExtensionProperties(
71             NULL, &instance_extension_count, instance_extensions);
72         for (i = 0; i < instance_extension_count; i++) {
73             if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,instance_extensions[i].extensionName)) {
74                 if (names && (*count > 0)) names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
75                 else enabled_extension_count++;
76             }
77
78             if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,instance_extensions[i].extensionName)) {
79                 if (names && (*count > 0)) names[enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
80                 else enabled_extension_count++;
81             }
82         }
83         free(instance_extensions);
84     }
85
86     if (*count == 0)
87         *count = enabled_extension_count;
88
89     return SDL_TRUE;
90 }
91
92 SDL_bool
93 Tizen_vulkan_CreateSurface(_THIS, SDL_Window* window, SDL_vulkanInstance instance, SDL_vulkanSurface* surface)
94 {
95     SDL_SysWMinfo wminfo;
96     SDL_WindowData *wmdata = (SDL_WindowData *)window->driverdata;
97     SDL_VideoData  *video_data  = (SDL_VideoData *)_this->driverdata;
98
99     if (!SDL_GetWindowWMInfo(window, &wminfo))
100         return SDL_FALSE;
101
102     switch (wminfo.subsystem)
103     {
104     case SDL_SYSWM_TIZEN:
105         {
106             VkWaylandSurfaceCreateInfoKHR createInfo;
107             VkResult ret;
108             createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
109             createInfo.pNext = NULL;
110             createInfo.flags = 0;
111             createInfo.display = video_data->display;
112             createInfo.surface = wmdata->surface;
113
114             ret = _this->vk_data->vkCreateWaylandSurfaceKHR((VkInstance)instance, &createInfo, NULL, (VkSurfaceKHR*)surface);
115             if (ret != VK_SUCCESS) {
116                 SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "fail to vkCreateWaylandSurfaceKHR : %i", (int)ret);
117                 return SDL_FALSE;
118             }
119             return SDL_TRUE;
120         }
121     default:
122         (void)surface;
123         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Unsupported subsystem %i", (int)wminfo.subsystem);
124         return SDL_FALSE;
125     }
126 }
127
128
129 int
130 Tizen_vulkan_LoadLibrary(_THIS, const char *vk_path)
131 {
132     void *vk_dll_handle = NULL;
133     char *path = NULL;
134
135     if (_this->vk_config.driver_loaded) {
136         return 0;
137     }
138
139     _this->vk_data = (struct SDL_vulkan_Data *) SDL_calloc(1, sizeof(SDL_vulkan_Data));
140     if (!_this->vk_data) {
141         return SDL_OutOfMemory();
142     }
143
144     if (vk_path) {
145         vk_dll_handle = SDL_LoadObject(vk_path);
146     }
147
148     if (!vk_dll_handle) {
149         path = SDL_getenv("SDL_VIDEO_VULKAN_DRIVER");
150
151         if (!path) {
152             path = DEFAULT_VULKAN;
153         }
154
155         vk_dll_handle = SDL_LoadObject(path);
156     }
157
158     _this->vk_data->vk_dll_handle = vk_dll_handle;
159
160     if (vk_dll_handle == NULL) {
161         SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "Could not initialize Vulkan library");
162         return -1;
163     }
164
165     LOAD_FUNC(vkEnumerateInstanceExtensionProperties);
166     LOAD_FUNC(vkCreateWaylandSurfaceKHR);
167
168     _this->vk_config.driver_loaded = 1;
169
170     return 0;
171 }
172
173 #endif