Start making general-purpose Wayland context driver that could work

for both Vulkan and EGL/GL
This commit is contained in:
twinaphex 2016-02-17 23:22:54 +01:00
parent 56a14ffc3c
commit eeada0c458

View File

@ -14,6 +14,19 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
/* Temporary switch until this driver is ready for Vulkan,
* replace HAVE_VULKAN_SUPPORT with HAVE_VULKAN then */
#if 0
#ifdef HAVE_VULKAN
#define HAVE_VULKAN_SUPPORT
#endif
#endif
#ifdef HAVE_VULKAN_SUPPORT
#define VK_USE_PLATFORM_WAYLAND_KHR
#include "../common/vulkan_common.h"
#endif
#include <sys/poll.h>
#include <unistd.h>
@ -28,9 +41,36 @@
#include "../common/egl_common.h"
#include "../common/gl_common.h"
static volatile sig_atomic_t g_quit = 0;
#ifdef HAVE_VULKAN_SUPPORT
static VkInstance cached_instance;
static VkDevice cached_device;
#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) do { \
wl->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
if (wl->fp##entrypoint == NULL) { \
RARCH_ERR("vkGetInstanceProcAddr failed to find vk%s\n", #entrypoint); \
goto error; \
} \
} while(0)
#define GET_DEVICE_PROC_ADDR(dev, entrypoint) do { \
wl->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk" #entrypoint); \
if (wl->fp##entrypoint == NULL) { \
RARCH_ERR("vkGetDeviceProcAddr failed to find vk%s\n", #entrypoint); \
goto error; \
} \
} while(0)
#endif
typedef struct gfx_ctx_wayland_data
{
#ifdef HAVE_EGL
egl_ctx_data_t egl;
struct wl_egl_window *win;
#endif
bool resize;
int fd;
unsigned width;
@ -41,17 +81,62 @@ typedef struct gfx_ctx_wayland_data
struct wl_surface *surface;
struct wl_shell_surface *shell_surf;
struct wl_shell *shell;
struct wl_egl_window *win;
struct wl_keyboard *wl_keyboard;
struct wl_pointer *wl_pointer;
unsigned swap_interval;
bool need_new_swapchain;
unsigned buffer_scale;
#ifdef HAVE_VULKAN_SUPPORT
struct vulkan_context context;
PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
PFN_vkQueuePresentKHR fpQueuePresentKHR;
PFN_vkCreateWaylandSurfaceKHR fpCreateWaylandSurfaceKHR;
PFN_vkDestroySurfaceKHR fpDestroySurfaceKHR;
VkSurfaceKHR surface;
VkSwapchainKHR swapchain;
#endif
} gfx_ctx_wayland_data_t;
#ifdef HAVE_VULKAN_SUPPORT
/* Forward declaration */
static bool vulkan_create_swapchain(gfx_ctx_wayland_data_t *wl);
#endif
static enum gfx_ctx_api wl_api;
#ifndef EGL_OPENGL_ES3_BIT_KHR
#define EGL_OPENGL_ES3_BIT_KHR 0x0040
#endif
static void sighandler(int sig)
{
(void)sig;
g_quit = 1;
}
static void install_sighandlers(void)
{
struct sigaction sa;
sa.sa_sigaction = NULL;
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
/* Shell surface callbacks. */
static void shell_surface_handle_ping(void *data,
struct wl_shell_surface *shell_surface,
@ -70,8 +155,8 @@ static void shell_surface_handle_configure(void *data,
(void)shell_surface;
(void)edges;
wl->width = width;
wl->height = height;
wl->width = wl->buffer_scale * width;
wl->height = wl->buffer_scale * height;
RARCH_LOG("[Wayland/EGL]: Surface configure: %u x %u.\n",
wl->width, wl->height);
@ -127,10 +212,58 @@ static void gfx_ctx_wl_destroy_resources(gfx_ctx_wayland_data_t *wl)
if (!wl)
return;
egl_destroy(wl);
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
egl_destroy(wl);
if (wl->win)
wl_egl_window_destroy(wl->win);
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
if (wl->context.queue)
vkQueueWaitIdle(wl->context.queue);
if (wl->swapchain)
wl->fpDestroySwapchainKHR(wl->context.device, wl->swapchain, NULL);
if (wl->surface)
wl->fpDestroySurfaceKHR(wl->context.instance, wl->surface, NULL);
for (i = 0; i < VULKAN_MAX_SWAPCHAIN_IMAGES; i++)
{
if (wl->context.swapchain_semaphores[i] != VK_NULL_HANDLE)
vkDestroySemaphore(wl->context.device, wl->context.swapchain_semaphores[i], NULL);
if (wl->context.swapchain_fences[i] != VK_NULL_HANDLE)
vkDestroyFence(wl->context.device, wl->context.swapchain_fences[i], NULL);
}
if (video_driver_ctl(RARCH_DISPLAY_CTL_IS_VIDEO_CACHE_CONTEXT, NULL))
{
cached_device = wl->context.device;
cached_instance = wl->context.instance;
}
else
{
if (wl->context.device)
vkDestroyDevice(wl->context.device, NULL);
if (wl->context.instance)
vkDestroyInstance(wl->context.instance, NULL);
}
if (wl->fd >= 0)
close(wl->fd);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
if (wl->win)
wl_egl_window_destroy(wl->win);
if (wl->shell)
wl_shell_destroy(wl->shell);
if (wl->compositor)
@ -175,7 +308,7 @@ static void flush_wayland_fd(gfx_ctx_wayland_data_t *wl)
if (fd.revents & (POLLERR | POLLHUP))
{
close(wl->fd);
g_egl_quit = true;
g_quit = true;
}
if (fd.revents & POLLIN)
@ -189,17 +322,32 @@ static void gfx_ctx_wl_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height,
unsigned frame_count)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
unsigned new_width, new_height;
(void)frame_count;
flush_wayland_fd((gfx_ctx_wayland_data_t*)data);
flush_wayland_fd(wl);
new_width = *width;
new_height = *height;
gfx_ctx_wl_get_video_size(data, &new_width, &new_height);
switch (wl_api)
{
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
/* Swapchains are recreated in set_resize as a
* central place, so use that to trigger swapchain reinit. */
*resize = wl->need_new_swapchain;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
if (new_width != *width || new_height != *height)
{
*resize = true;
@ -207,14 +355,42 @@ static void gfx_ctx_wl_check_window(void *data, bool *quit,
*height = new_height;
}
*quit = g_egl_quit;
*quit = g_quit;
}
static bool gfx_ctx_wl_set_resize(void *data, unsigned width, unsigned height)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl_egl_window_resize(wl->win, width, height, 0, 0);
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
wl_egl_window_resize(wl->win, width, height, 0, 0);
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
wl->width = width;
wl->height = height;
if (!vulkan_create_swapchain(wl))
{
RARCH_ERR("[Wayland/Vulkan]: Failed to update swapchain.\n");
return false;
}
else
wl->context.invalid_swapchain = true;
wl->need_new_swapchain = false;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
return true;
}
@ -245,6 +421,7 @@ static void gfx_ctx_wl_get_video_size(void *data,
#define DEFAULT_WINDOWED_WIDTH 640
#define DEFAULT_WINDOWED_HEIGHT 480
#ifdef HAVE_EGL
#define WL_EGL_ATTRIBS_BASE \
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \
EGL_RED_SIZE, 1, \
@ -252,9 +429,33 @@ static void gfx_ctx_wl_get_video_size(void *data,
EGL_BLUE_SIZE, 1, \
EGL_ALPHA_SIZE, 0, \
EGL_DEPTH_SIZE, 0
#endif
static void *gfx_ctx_wl_init(void *video_driver)
{
#ifdef HAVE_VULKAN_SUPPORT
unsigned i;
uint32_t queue_count;
VkApplicationInfo app = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
VkInstanceCreateInfo info = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
VkDeviceQueueCreateInfo queue_info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
VkPhysicalDeviceFeatures features = { false };
VkDeviceCreateInfo device_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
VkQueueFamilyProperties queue_properties[32];
uint32_t gpu_count = 1;
bool found_queue = false;
static const float one = 1.0f;
static const char *instance_extensions[] = {
"VK_KHR_surface",
"VK_KHR_wayland_surface",
};
static const char *device_extensions[] = {
"VK_KHR_swapchain",
};
#endif
#ifdef HAVE_OPENGL
static const EGLint egl_attribs_gl[] = {
WL_EGL_ATTRIBS_BASE,
@ -284,6 +485,7 @@ static void *gfx_ctx_wl_init(void *video_driver)
#endif
#ifdef HAVE_EGL
static const EGLint egl_attribs_vg[] = {
WL_EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
@ -293,14 +495,15 @@ static void *gfx_ctx_wl_init(void *video_driver)
EGLint major = 0, minor = 0;
EGLint n;
const EGLint *attrib_ptr = NULL;
#endif
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)
calloc(1, sizeof(gfx_ctx_wayland_data_t));
(void)video_driver;
if (!wl)
return NULL;
(void)video_driver;
switch (wl->egl.api)
{
case GFX_CTX_OPENGL_API:
@ -325,11 +528,12 @@ static void *gfx_ctx_wl_init(void *video_driver)
attrib_ptr = egl_attribs_vg;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
g_egl_quit = 0;
g_quit = 0;
wl->dpy = wl_display_connect(NULL);
if (!wl->dpy)
@ -338,9 +542,25 @@ static void *gfx_ctx_wl_init(void *video_driver)
goto error;
}
install_sighandlers();
wl->registry = wl_display_get_registry(wl->dpy);
wl_registry_add_listener(wl->registry, &registry_listener, wl);
wl_display_dispatch(wl->dpy);
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
wl_display_dispatch(wl->dpy);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
wl_display_roundtrip(wl->dpy);
if (!wl->compositor)
@ -357,15 +577,120 @@ static void *gfx_ctx_wl_init(void *video_driver)
wl->fd = wl_display_get_fd(wl->dpy);
if (!egl_init_context(wl, (EGLNativeDisplayType)wl->dpy,
&major, &minor, &n, attrib_ptr))
switch (wl_api)
{
egl_report_error();
goto error;
}
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
if (!egl_init_context(wl, (EGLNativeDisplayType)wl->dpy,
&major, &minor, &n, attrib_ptr))
{
egl_report_error();
goto error;
}
if (n == 0 || !egl_has_config(wl))
goto error;
if (n == 0 || !egl_has_config(wl))
goto error;
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
app.pApplicationName = "RetroArch";
app.applicationVersion = 0;
app.pEngineName = "RetroArch";
app.engineVersion = 0;
app.apiVersion = VK_API_VERSION;
info.pApplicationInfo = &app;
info.enabledExtensionCount = ARRAY_SIZE(instance_extensions);
info.ppEnabledExtensionNames = instance_extensions;
if (cached_instance)
{
wl->context.instance = cached_instance;
cached_instance = NULL;
}
else if (vkCreateInstance(&info, NULL, &wl->context.instance) != VK_SUCCESS)
goto error;
if (vkEnumeratePhysicalDevices(wl->context.instance, &gpu_count, &wl->context.gpu) != VK_SUCCESS)
goto error;
if (gpu_count != 1)
{
RARCH_ERR("[Wayland/Vulkan]: Failed to enumerate Vulkan physical device.\n");
goto error;
}
vkGetPhysicalDeviceProperties(wl->context.gpu, &wl->context.gpu_properties);
vkGetPhysicalDeviceMemoryProperties(wl->context.gpu, &wl->context.memory_properties);
vkGetPhysicalDeviceQueueFamilyProperties(wl->context.gpu, &queue_count, NULL);
if (queue_count < 1 || queue_count > 32)
goto error;
vkGetPhysicalDeviceQueueFamilyProperties(wl->context.gpu, &queue_count, queue_properties);
for (i = 0; i < queue_count; i++)
{
if (queue_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
wl->context.graphics_queue_index = i;
RARCH_LOG("[Wayland/Vulkan]: Device supports %u sub-queues.\n",
queue_properties[i].queueCount);
found_queue = true;
break;
}
}
if (!found_queue)
{
RARCH_ERR("[Wayland/Vulkan]: Did not find suitable graphics queue.\n");
goto error;
}
queue_info.queueFamilyIndex = wl->context.graphics_queue_index;
queue_info.queueCount = 1;
queue_info.pQueuePriorities = &one;
device_info.queueCreateInfoCount = 1;
device_info.pQueueCreateInfos = &queue_info;
device_info.enabledExtensionCount = ARRAY_SIZE(device_extensions);
device_info.ppEnabledExtensionNames = device_extensions;
device_info.pEnabledFeatures = &features;
if (cached_device)
{
wl->context.device = cached_device;
cached_device = NULL;
video_driver_ctl(RARCH_DISPLAY_CTL_SET_VIDEO_CACHE_CONTEXT_ACK, NULL);
RARCH_LOG("[Vulkan]: Using cached Vulkan context.\n");
}
else if (vkCreateDevice(wl->context.gpu, &device_info, NULL, &wl->context.device) != VK_SUCCESS)
goto error;
vkGetDeviceQueue(wl->context.device, wl->context.graphics_queue_index, 0, &wl->context.queue);
GET_INSTANCE_PROC_ADDR(wl->context.instance, GetPhysicalDeviceSurfaceSupportKHR);
GET_INSTANCE_PROC_ADDR(wl->context.instance, GetPhysicalDeviceSurfaceCapabilitiesKHR);
GET_INSTANCE_PROC_ADDR(wl->context.instance, GetPhysicalDeviceSurfaceFormatsKHR);
GET_INSTANCE_PROC_ADDR(wl->context.instance, GetPhysicalDeviceSurfacePresentModesKHR);
GET_INSTANCE_PROC_ADDR(wl->context.instance, CreateWaylandSurfaceKHR);
GET_INSTANCE_PROC_ADDR(wl->context.instance, DestroySurfaceKHR);
GET_DEVICE_PROC_ADDR(wl->context.device, CreateSwapchainKHR);
GET_DEVICE_PROC_ADDR(wl->context.device, DestroySwapchainKHR);
GET_DEVICE_PROC_ADDR(wl->context.device, GetSwapchainImagesKHR);
GET_DEVICE_PROC_ADDR(wl->context.device, AcquireNextImageKHR);
GET_DEVICE_PROC_ADDR(wl->context.device, QueuePresentKHR);
wl->context.queue_lock = slock_new();
if (!wl->context.queue_lock)
goto error;
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
return wl;
@ -378,6 +703,7 @@ error:
return NULL;
}
#ifdef HAVE_EGL
static EGLint *egl_fill_attribs(gfx_ctx_wayland_data_t *wl, EGLint *attr)
{
switch (wl->egl.api)
@ -436,6 +762,7 @@ static EGLint *egl_fill_attribs(gfx_ctx_wayland_data_t *wl, EGLint *attr)
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
@ -443,6 +770,7 @@ static EGLint *egl_fill_attribs(gfx_ctx_wayland_data_t *wl, EGLint *attr)
*attr = EGL_NONE;
return attr;
}
#endif
static void gfx_ctx_wl_destroy(void *data)
{
@ -453,26 +781,85 @@ static void gfx_ctx_wl_destroy(void *data)
gfx_ctx_wl_destroy_resources(wl);
switch (wl_api)
{
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
if (wl->context.queue_lock)
slock_free(wl->context.queue_lock);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
free(wl);
}
static void gfx_ctx_wl_set_swap_interval(void *data, unsigned swap_interval)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
egl_set_swap_interval(data, swap_interval);
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
if (wl->swap_interval != swap_interval)
{
wl->swap_interval = swap_interval;
if (wl->swapchain)
wl->need_new_swapchain = true;
}
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static bool gfx_ctx_wl_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
#ifdef HAVE_EGL
EGLint egl_attribs[16];
EGLint *attr = NULL;
EGLint *attr = egl_fill_attribs(
(gfx_ctx_wayland_data_t*)data, egl_attribs);
#endif
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
egl_install_sighandlers();
wl->width = width ? width : DEFAULT_WINDOWED_WIDTH;
wl->height = height ? height : DEFAULT_WINDOWED_HEIGHT;
attr = egl_fill_attribs(wl, egl_attribs);
/* TODO: Use wl_output::scale to obtain correct value. */
wl->buffer_scale = 1;
wl->width = width ? width : DEFAULT_WINDOWED_WIDTH;
wl->height = height ? height : DEFAULT_WINDOWED_HEIGHT;
wl->surface = wl_compositor_create_surface(wl->compositor);
wl->surface = wl_compositor_create_surface(wl->compositor);
wl->win = wl_egl_window_create(wl->surface, wl->width, wl->height);
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) || defined(HAVE_VG)
#ifdef HAVE_EGL
wl->win = wl_egl_window_create(wl->surface, wl->width, wl->height);
#endif
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
wl->shell_surf = wl_shell_get_shell_surface(wl->shell, wl->surface);
wl_shell_surface_add_listener(wl->shell_surf, &shell_surface_listener, wl);
@ -480,21 +867,62 @@ static bool gfx_ctx_wl_set_video_mode(void *data,
wl_shell_surface_set_class(wl->shell_surf, "RetroArch");
wl_shell_surface_set_title(wl->shell_surf, "RetroArch");
if (!egl_create_context(wl, (attr != egl_attribs) ? egl_attribs : NULL))
switch (wl_api)
{
egl_report_error();
goto error;
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) || defined(HAVE_VG)
#ifdef HAVE_EGL
if (!egl_create_context(wl, (attr != egl_attribs) ? egl_attribs : NULL))
{
egl_report_error();
goto error;
}
if (!egl_create_surface(wl, (EGLNativeWindowType)wl->win))
goto error;
#endif
egl_set_swap_interval(wl, wl->egl.interval);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
if (!egl_create_surface(wl, (EGLNativeWindowType)wl->win))
goto error;
egl_set_swap_interval(wl, wl->egl.interval);
if (fullscreen)
wl_shell_surface_set_fullscreen(wl->shell_surf, WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, 0, NULL);
wl_shell_surface_set_fullscreen(wl->shell_surf,
WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, 0, NULL);
flush_wayland_fd(wl);
switch (wl_api)
{
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
{
VkWaylandSurfaceCreateInfoKHR wl_info =
{ VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR };
wl_display_roundtrip(wl->dpy);
wl_info.display = wl->dpy;
wl_info.surface = wl->surf;
wl->fpCreateWaylandSurfaceKHR(wl->context.instance,
&wl_info, NULL, &wl->surface);
if (!vulkan_create_swapchain(wl))
goto error;
}
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
return true;
error:
@ -572,6 +1000,13 @@ static bool gfx_ctx_wl_bind_api(void *video_driver,
#else
break;
#endif
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
return true;
#else
break;
#endif
case GFX_CTX_NONE:
default:
break;
}
@ -721,11 +1156,265 @@ static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
};
#ifdef HAVE_VULKAN_SUPPORT
static void *gfx_ctx_wl_get_context_data(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
return &wl->context;
}
static void vulkan_acquire_next_image(gfx_ctx_wayland_data_t *wl)
{
VkResult err;
VkSemaphoreCreateInfo sem_info = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
VkFenceCreateInfo info = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
VkFence fence;
VkFence *next_fence;
unsigned index;
vkCreateFence(wl->context.device, &info, NULL, &fence);
err = wl->fpAcquireNextImageKHR(wl->context.device, wl->swapchain, UINT64_MAX,
VK_NULL_HANDLE, fence, &wl->context.current_swapchain_index);
index = wl->context.current_swapchain_index;
if (wl->context.swapchain_semaphores[index] == VK_NULL_HANDLE)
vkCreateSemaphore(wl->context.device, &sem_info, NULL, &wl->context.swapchain_semaphores[index]);
vkWaitForFences(wl->context.device, 1, &fence, true, UINT64_MAX);
vkDestroyFence(wl->context.device, fence, NULL);
next_fence = &wl->context.swapchain_fences[index];
if (*next_fence != VK_NULL_HANDLE)
{
vkWaitForFences(wl->context.device, 1, next_fence, true, UINT64_MAX);
vkResetFences(wl->context.device, 1, next_fence);
}
else
vkCreateFence(wl->context.device, &info, NULL, next_fence);
if (err != VK_SUCCESS)
{
RARCH_LOG("[Wayland/Vulkan]: AcquireNextImage failed, invalidating swapchain.\n");
wl->context.invalid_swapchain = true;
}
}
static bool vulkan_create_swapchain(gfx_ctx_wayland_data_t *wl)
{
VkSurfaceCapabilitiesKHR surface_properties;
VkSurfaceFormatKHR formats[256];
VkSurfaceFormatKHR format;
VkExtent2D swapchain_size;
VkPresentModeKHR swapchain_present_mode = wl->swap_interval ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_MAILBOX_KHR;
VkSurfaceTransformFlagBitsKHR pre_transform;
VkSwapchainKHR old_swapchain;
VkSwapchainCreateInfoKHR info = { VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR };
uint32_t format_count;
uint32_t desired_swapchain_images;
unsigned i;
RARCH_LOG("[Wayland/Vulkan]: Creating swapchain with present mode: %u\n", (unsigned)swapchain_present_mode);
wl->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(wl->context.gpu, wl->surface, &surface_properties);
wl->fpGetPhysicalDeviceSurfaceFormatsKHR(wl->context.gpu, wl->surface, &format_count, NULL);
wl->fpGetPhysicalDeviceSurfaceFormatsKHR(wl->context.gpu, wl->surface, &format_count, formats);
if (format_count == 1 && formats[0].format == VK_FORMAT_UNDEFINED)
{
format = formats[0];
format.format = VK_FORMAT_B8G8R8A8_UNORM;
}
else
{
if (format_count == 0)
{
RARCH_ERR("[Wayland Vulkan]: Surface has no formats.\n");
return false;
}
format = formats[0];
}
if (surface_properties.currentExtent.width == -1)
{
swapchain_size.width = wl->width;
swapchain_size.height = wl->height;
}
else
swapchain_size = surface_properties.currentExtent;
desired_swapchain_images = surface_properties.minImageCount + 1;
if ((surface_properties.maxImageCount > 0) && (desired_swapchain_images > surface_properties.maxImageCount))
desired_swapchain_images = surface_properties.maxImageCount;
if (surface_properties.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
pre_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
else
pre_transform = surface_properties.currentTransform;
old_swapchain = wl->swapchain;
info.surface = wl->surface;
info.minImageCount = desired_swapchain_images;
info.imageFormat = format.format;
info.imageColorSpace = format.colorSpace;
info.imageExtent.width = swapchain_size.width;
info.imageExtent.height = swapchain_size.height;
info.imageArrayLayers = 1;
info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
info.preTransform = pre_transform;
info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
info.presentMode = swapchain_present_mode;
info.clipped = true;
info.oldSwapchain = old_swapchain;
wl->fpCreateSwapchainKHR(wl->context.device, &info, NULL, &wl->swapchain);
if (old_swapchain != VK_NULL_HANDLE)
wl->fpDestroySwapchainKHR(wl->context.device, old_swapchain, NULL);
wl->context.swapchain_width = swapchain_size.width;
wl->context.swapchain_height = swapchain_size.height;
/* Make sure we create a backbuffer format that is as we expect. */
switch (format.format)
{
case VK_FORMAT_B8G8R8A8_SRGB:
wl->context.swapchain_format = VK_FORMAT_B8G8R8A8_UNORM;
wl->context.swapchain_is_srgb = true;
break;
case VK_FORMAT_R8G8B8A8_SRGB:
wl->context.swapchain_format = VK_FORMAT_R8G8B8A8_UNORM;
wl->context.swapchain_is_srgb = true;
break;
case VK_FORMAT_R8G8B8_SRGB:
wl->context.swapchain_format = VK_FORMAT_R8G8B8_UNORM;
wl->context.swapchain_is_srgb = true;
break;
case VK_FORMAT_B8G8R8_SRGB:
wl->context.swapchain_format = VK_FORMAT_B8G8R8_UNORM;
wl->context.swapchain_is_srgb = true;
break;
default:
wl->context.swapchain_format = format.format;
break;
}
wl->fpGetSwapchainImagesKHR(wl->context.device, wl->swapchain,
&wl->context.num_swapchain_images, NULL);
wl->fpGetSwapchainImagesKHR(wl->context.device, wl->swapchain,
&wl->context.num_swapchain_images, wl->context.swapchain_images);
for (i = 0; i < wl->context.num_swapchain_images; i++)
{
if (wl->context.swapchain_fences[i])
{
vkDestroyFence(wl->context.device, wl->context.swapchain_fences[i], NULL);
wl->context.swapchain_fences[i] = VK_NULL_HANDLE;
}
}
vulkan_acquire_next_image(wl);
return true;
}
static void vulkan_present(gfx_ctx_wayland_data_t *wl, unsigned index)
{
VkResult result = VK_SUCCESS, err = VK_SUCCESS;
VkPresentInfoKHR present = { VK_STRUCTURE_TYPE_PRESENT_INFO_KHR };
present.swapchainCount = 1;
present.pSwapchains = &wl->swapchain;
present.pImageIndices = &index;
present.pResults = &result;
present.waitSemaphoreCount = 1;
present.pWaitSemaphores = &wl->context.swapchain_semaphores[index];
/* Better hope QueuePresent doesn't block D: */
slock_lock(wl->context.queue_lock);
err = wl->fpQueuePresentKHR(wl->context.queue, &present);
if (err != VK_SUCCESS || result != VK_SUCCESS)
{
RARCH_LOG("[Wayland/Vulkan]: QueuePresent failed, invalidating swapchain.\n");
wl->context.invalid_swapchain = true;
}
slock_unlock(wl->context.queue_lock);
}
#endif
static void gfx_ctx_wl_swap_buffers(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
egl_swap_buffers(data);
#endif
break;
case GFX_CTX_VULKAN_API:
#ifdef HAVE_VULKAN_SUPPORT
vulkan_present(wl, wl->context.current_swapchain_index);
vulkan_acquire_next_image(wl);
flush_wayland_fd(wl);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
static gfx_ctx_proc_t gfx_ctx_wl_get_proc_address(const char *symbol)
{
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
return egl_get_proc_address(symbol);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
return NULL;
}
static void gfx_ctx_wl_bind_hw_render(void *data, bool enable)
{
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
case GFX_CTX_OPENGL_ES_API:
case GFX_CTX_OPENVG_API:
#ifdef HAVE_EGL
egl_bind_hw_render(data, enable);
#endif
break;
case GFX_CTX_NONE:
default:
break;
}
}
const gfx_ctx_driver_t gfx_ctx_wayland = {
gfx_ctx_wl_init,
gfx_ctx_wl_destroy,
gfx_ctx_wl_bind_api,
egl_set_swap_interval,
gfx_ctx_wl_set_swap_interval,
gfx_ctx_wl_set_video_mode,
gfx_ctx_wl_get_video_size,
NULL, /* get_video_output_size */
@ -739,12 +1428,17 @@ const gfx_ctx_driver_t gfx_ctx_wayland = {
gfx_ctx_wl_has_focus,
gfx_ctx_wl_suppress_screensaver,
gfx_ctx_wl_has_windowed,
egl_swap_buffers,
gfx_ctx_wl_swap_buffers,
gfx_ctx_wl_input_driver,
egl_get_proc_address,
gfx_ctx_wl_get_proc_address,
NULL,
NULL,
NULL,
"wayland",
egl_bind_hw_render,
gfx_ctx_wl_bind_hw_render,
#ifdef HAVE_VULKAN_SUPPORT
gfx_ctx_wl_get_context_data
#else
NULL
#endif
};