Add Android Vulkan surface creation

very untested may not work at all
This commit is contained in:
Curi0 2017-09-09 11:37:07 +05:30
parent af67cf3710
commit b10f4cddcf
4 changed files with 63 additions and 15 deletions

View File

@ -27,8 +27,6 @@
#include <dlfcn.h> #include <dlfcn.h>
#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNull null
#define _GLFW_PLATFORM_CONTEXT_STATE #define _GLFW_PLATFORM_CONTEXT_STATE
#define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_MONITOR_STATE
#define _GLFW_PLATFORM_CURSOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE
@ -40,16 +38,31 @@
#include "posix_time.h" #include "posix_time.h"
#include "posix_thread.h" #include "posix_thread.h"
#include "android_joystick.h" #include "android_joystick.h"
#include <android/native_window.h>
#define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL) #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
#define _glfw_dlclose(handle) dlclose(handle) #define _glfw_dlclose(handle) dlclose(handle)
#define _glfw_dlsym(handle, name) dlsym(handle, name) #define _glfw_dlsym(handle, name) dlsym(handle, name)
#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowAndroid android
// Null-specific per-window data // Null-specific per-window data
// //
typedef struct _GLFWwindowNull typedef struct _GLFWwindowAndroid
{ {
int width; int width;
int height; int height;
} _GLFWwindowNull; ANativeWindow *window;
} _GLFWwindowAndroid;
typedef VkFlags VkAndroidSurfaceCreateFlagsKHR;
typedef struct VkAndroidSurfaceCreateInfoKHR {
VkStructureType sType;
const void* pNext;
VkAndroidSurfaceCreateFlagsKHR flags;
ANativeWindow* surface;
} VkAndroidSurfaceCreateInfoKHR;
typedef VkResult (APIENTRY *PFN_vkCreateAndroidSurfaceKHR)(VkInstance,const VkAndroidSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);

View File

@ -31,8 +31,8 @@
static int createNativeWindow(_GLFWwindow* window, static int createNativeWindow(_GLFWwindow* window,
const _GLFWwndconfig* wndconfig) const _GLFWwndconfig* wndconfig)
{ {
window->null.width = wndconfig->width; window->android.width = wndconfig->width;
window->null.height = wndconfig->height; window->android.height = wndconfig->height;
return GLFW_TRUE; return GLFW_TRUE;
} }
@ -104,15 +104,15 @@ void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
{ {
if (width) if (width)
*width = window->null.width; *width = window->android.width;
if (height) if (height)
*height = window->null.height; *height = window->android.height;
} }
void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
{ {
window->null.width = width; window->android.width = width;
window->null.height = height; window->android.height = height;
} }
void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window, void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
@ -128,9 +128,9 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d)
void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
{ {
if (width) if (width)
*width = window->null.width; *width = window->android.width;
if (height) if (height)
*height = window->null.height; *height = window->android.height;
} }
void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
@ -273,13 +273,18 @@ int _glfwPlatformGetKeyScancode(int key)
void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
{ {
if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_android_surface)
return;
extensions[0] = "VK_KHR_surface";
extensions[1] = "VK_KHR_android_surface";
} }
int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
VkPhysicalDevice device, VkPhysicalDevice device,
uint32_t queuefamily) uint32_t queuefamily)
{ {
return GLFW_FALSE; return GLFW_TRUE;
} }
VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
@ -287,7 +292,31 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
const VkAllocationCallbacks* allocator, const VkAllocationCallbacks* allocator,
VkSurfaceKHR* surface) VkSurfaceKHR* surface)
{ {
// This seems like the most appropriate error to return here VkResult err;
return VK_ERROR_INITIALIZATION_FAILED; VkAndroidSurfaceCreateInfoKHR sci;
PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR;
vkCreateAndroidSurfaceKHR = (PFN_vkCreateAndroidSurfaceKHR)
vkGetInstanceProcAddr(instance, "vkCreateAndroidSurfaceKHR");
if (!vkCreateAndroidSurfaceKHR)
{
_glfwInputError(GLFW_API_UNAVAILABLE,
"Android: Vulkan instance missing VK_KHR_android_surface extension");
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
memset(&sci, 0, sizeof(sci));
sci.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
sci.surface = window->android.window;
err = vkCreateAndroidSurfaceKHR(instance, &sci, allocator, surface);
if (err)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Android: Failed to create Vulkan surface: %s",
_glfwGetVulkanResultString(err));
}
return err;
} }

View File

@ -127,6 +127,7 @@ typedef enum VkStructureType
VK_STRUCTURE_TYPE_MIR_SURFACE_CREATE_INFO_KHR = 1000007000, VK_STRUCTURE_TYPE_MIR_SURFACE_CREATE_INFO_KHR = 1000007000,
VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR = 1000009000, VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR = 1000009000,
VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK = 1000053000, VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK = 1000053000,
VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR = 1000008000,
VK_STRUCTURE_TYPE_MAX_ENUM = 0x7FFFFFFF VK_STRUCTURE_TYPE_MAX_ENUM = 0x7FFFFFFF
} VkStructureType; } VkStructureType;
@ -590,6 +591,8 @@ struct _GLFWlibrary
GLFWbool KHR_wayland_surface; GLFWbool KHR_wayland_surface;
#elif defined(_GLFW_MIR) #elif defined(_GLFW_MIR)
GLFWbool KHR_mir_surface; GLFWbool KHR_mir_surface;
#elif defined(_GLFW_ANDROID)
GLFWbool KHR_android_surface;
#endif #endif
} vk; } vk;

View File

@ -137,6 +137,9 @@ GLFWbool _glfwInitVulkan(int mode)
#elif defined(_GLFW_MIR) #elif defined(_GLFW_MIR)
else if (strcmp(ep[i].extensionName, "VK_KHR_mir_surface") == 0) else if (strcmp(ep[i].extensionName, "VK_KHR_mir_surface") == 0)
_glfw.vk.KHR_mir_surface = GLFW_TRUE; _glfw.vk.KHR_mir_surface = GLFW_TRUE;
#elif defined(_GLFW_ANDROID)
else if (strcmp(ep[i].extensionName, "VK_KHR_android_surface") == 0)
_glfw.vk.KHR_android_surface = GLFW_TRUE;
#endif #endif
} }