From a833fa4197ff91c2bd303156cfb757b4d5499d58 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Fri, 8 Sep 2017 21:30:25 +0530 Subject: [PATCH 01/21] Implement Android skelaton --- CMakeLists.txt | 3 + src/CMakeLists.txt | 5 + src/android_init.c | 50 +++++++ src/android_joystick.c | 42 ++++++ src/android_joystick.h | 31 +++++ src/android_monitor.c | 55 ++++++++ src/android_platform.h | 55 ++++++++ src/android_window.c | 293 +++++++++++++++++++++++++++++++++++++++++ src/glfw_config.h.in | 3 + src/internal.h | 2 + 10 files changed, 539 insertions(+) create mode 100644 src/android_init.c create mode 100644 src/android_joystick.c create mode 100644 src/android_joystick.h create mode 100644 src/android_monitor.c create mode 100644 src/android_platform.h create mode 100644 src/android_window.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 56c1f386d..c4985675d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -169,6 +169,9 @@ elseif (WIN32) elseif (APPLE) set(_GLFW_COCOA 1) message(STATUS "Using Cocoa for window creation") +elseif (ANDROID) + set(_GLFW_ANDROID 1) + message(STATUS "Using Android for window creation") elseif (UNIX) set(_GLFW_X11 1) message(STATUS "Using X11 for window creation") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b14512cc5..9560110cc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -59,6 +59,11 @@ elseif (_GLFW_OSMESA) posix_time.h posix_thread.h osmesa_context.h) set(glfw_SOURCES ${common_SOURCES} null_init.c null_monitor.c null_window.c null_joystick.c posix_time.c posix_thread.c osmesa_context.c) +elseif (_GLFW_ANDROID) + set(glfw_HEADERS ${common_HEADERS} android_platform.h android_joystick.h + posix_time.h posix_thread.h) + set(glfw_SOURCES ${common_SOURCES} android_init.c android_monitor.c android_window.c + android_joystick.c posix_time.c posix_thread.c) endif() if (APPLE) diff --git a/src/android_init.c b/src/android_init.c new file mode 100644 index 000000000..b03b44476 --- /dev/null +++ b/src/android_init.c @@ -0,0 +1,50 @@ +//======================================================================== +// GLFW 3.3 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2016 Google Inc. +// Copyright (c) 2006-2016 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + + +////////////////////////////////////////////////////////////////////////// +////// GLFW platform API ////// +////////////////////////////////////////////////////////////////////////// + +int _glfwPlatformInit(void) +{ + _glfwInitTimerPOSIX(); + return GLFW_TRUE; +} + +void _glfwPlatformTerminate(void) +{ + _glfwTerminateOSMesa(); +} + +const char* _glfwPlatformGetVersionString(void) +{ + return _GLFW_VERSION_NUMBER " Android"; +} + diff --git a/src/android_joystick.c b/src/android_joystick.c new file mode 100644 index 000000000..afd65e15d --- /dev/null +++ b/src/android_joystick.c @@ -0,0 +1,42 @@ +//======================================================================== +// GLFW 3.3 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2006-2016 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + + +////////////////////////////////////////////////////////////////////////// +////// GLFW platform API ////// +////////////////////////////////////////////////////////////////////////// + +int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode) +{ + return GLFW_FALSE; +} + +void _glfwPlatformUpdateGamepadGUID(char* guid) +{ +} + diff --git a/src/android_joystick.h b/src/android_joystick.h new file mode 100644 index 000000000..db724ad70 --- /dev/null +++ b/src/android_joystick.h @@ -0,0 +1,31 @@ +//======================================================================== +// GLFW 3.3 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2006-2016 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#define _GLFW_PLATFORM_JOYSTICK_STATE int nulljs +#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE int nulljs + +#define _GLFW_PLATFORM_MAPPING_NAME "Android" + diff --git a/src/android_monitor.c b/src/android_monitor.c new file mode 100644 index 000000000..f5678bbc3 --- /dev/null +++ b/src/android_monitor.c @@ -0,0 +1,55 @@ +//======================================================================== +// GLFW 3.3 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2016 Google Inc. +// Copyright (c) 2006-2016 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + + +////////////////////////////////////////////////////////////////////////// +////// GLFW platform API ////// +////////////////////////////////////////////////////////////////////////// + +void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos) +{ +} + +GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) +{ + return NULL; +} + +void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) +{ +} + +void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp) +{ +} + +void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp) +{ +} + diff --git a/src/android_platform.h b/src/android_platform.h new file mode 100644 index 000000000..02dbe8f70 --- /dev/null +++ b/src/android_platform.h @@ -0,0 +1,55 @@ +//======================================================================== +// GLFW 3.3 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2016 Google Inc. +// Copyright (c) 2006-2016 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include + +#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNull null + +#define _GLFW_PLATFORM_CONTEXT_STATE +#define _GLFW_PLATFORM_MONITOR_STATE +#define _GLFW_PLATFORM_CURSOR_STATE +#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE +#define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE +#define _GLFW_EGL_CONTEXT_STATE +#define _GLFW_EGL_LIBRARY_CONTEXT_STATE +#include "osmesa_context.h" +#include "posix_time.h" +#include "posix_thread.h" +#include "android_joystick.h" + +#define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL) +#define _glfw_dlclose(handle) dlclose(handle) +#define _glfw_dlsym(handle, name) dlsym(handle, name) + +// Null-specific per-window data +// +typedef struct _GLFWwindowNull +{ + int width; + int height; +} _GLFWwindowNull; + diff --git a/src/android_window.c b/src/android_window.c new file mode 100644 index 000000000..2e627672c --- /dev/null +++ b/src/android_window.c @@ -0,0 +1,293 @@ +//======================================================================== +// GLFW 3.3 - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2016 Google Inc. +// Copyright (c) 2006-2016 Camilla Löwy +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + + +static int createNativeWindow(_GLFWwindow* window, + const _GLFWwndconfig* wndconfig) +{ + window->null.width = wndconfig->width; + window->null.height = wndconfig->height; + + return GLFW_TRUE; +} + + +////////////////////////////////////////////////////////////////////////// +////// GLFW platform API ////// +////////////////////////////////////////////////////////////////////////// + +int _glfwPlatformCreateWindow(_GLFWwindow* window, + const _GLFWwndconfig* wndconfig, + const _GLFWctxconfig* ctxconfig, + const _GLFWfbconfig* fbconfig) +{ + if (!createNativeWindow(window, wndconfig)) + return GLFW_FALSE; + + if (ctxconfig->client != GLFW_NO_API) + { + if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API || + ctxconfig->source == GLFW_OSMESA_CONTEXT_API) + { + if (!_glfwInitOSMesa()) + return GLFW_FALSE; + if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig)) + return GLFW_FALSE; + } + else + { + _glfwInputError(GLFW_API_UNAVAILABLE, "Null: EGL not available"); + return GLFW_FALSE; + } + } + + return GLFW_TRUE; +} + +void _glfwPlatformDestroyWindow(_GLFWwindow* window) +{ + if (window->context.destroy) + window->context.destroy(window); +} + +void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) +{ +} + +void _glfwPlatformSetWindowIcon(_GLFWwindow* window, int count, + const GLFWimage* images) +{ +} + +void _glfwPlatformSetWindowMonitor(_GLFWwindow* window, + _GLFWmonitor* monitor, + int xpos, int ypos, + int width, int height, + int refreshRate) +{ +} + +void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos) +{ +} + +void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) +{ +} + +void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) +{ + if (width) + *width = window->null.width; + if (height) + *height = window->null.height; +} + +void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) +{ + window->null.width = width; + window->null.height = height; +} + +void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window, + int minwidth, int minheight, + int maxwidth, int maxheight) +{ +} + +void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) +{ +} + +void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) +{ + if (width) + *width = window->null.width; + if (height) + *height = window->null.height; +} + +void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, + int* left, int* top, + int* right, int* bottom) +{ +} + +void _glfwPlatformIconifyWindow(_GLFWwindow* window) +{ +} + +void _glfwPlatformRestoreWindow(_GLFWwindow* window) +{ +} + +void _glfwPlatformMaximizeWindow(_GLFWwindow* window) +{ +} + +int _glfwPlatformWindowMaximized(_GLFWwindow* window) +{ + return GLFW_FALSE; +} + +void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled) +{ +} + +void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled) +{ +} + +void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled) +{ +} + +void _glfwPlatformShowWindow(_GLFWwindow* window) +{ +} + + +void _glfwPlatformRequestWindowAttention(_GLFWwindow* window) +{ +} + +void _glfwPlatformUnhideWindow(_GLFWwindow* window) +{ +} + +void _glfwPlatformHideWindow(_GLFWwindow* window) +{ +} + +void _glfwPlatformFocusWindow(_GLFWwindow* window) +{ +} + +int _glfwPlatformWindowFocused(_GLFWwindow* window) +{ + return GLFW_FALSE; +} + +int _glfwPlatformWindowIconified(_GLFWwindow* window) +{ + return GLFW_FALSE; +} + +int _glfwPlatformWindowVisible(_GLFWwindow* window) +{ + return GLFW_FALSE; +} + +void _glfwPlatformPollEvents(void) +{ +} + +void _glfwPlatformWaitEvents(void) +{ +} + +void _glfwPlatformWaitEventsTimeout(double timeout) +{ +} + +void _glfwPlatformPostEmptyEvent(void) +{ +} + +void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos) +{ +} + +void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) +{ +} + +void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) +{ +} + +int _glfwPlatformCreateCursor(_GLFWcursor* cursor, + const GLFWimage* image, + int xhot, int yhot) +{ + return GLFW_TRUE; +} + +int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape) +{ + return GLFW_TRUE; +} + +void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) +{ +} + +void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) +{ +} + +void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) +{ +} + +const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) +{ + return NULL; +} + +const char* _glfwPlatformGetScancodeName(int scancode) +{ + return ""; +} + +int _glfwPlatformGetKeyScancode(int key) +{ + return -1; +} + +void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) +{ +} + +int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, + VkPhysicalDevice device, + uint32_t queuefamily) +{ + return GLFW_FALSE; +} + +VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, + _GLFWwindow* window, + const VkAllocationCallbacks* allocator, + VkSurfaceKHR* surface) +{ + // This seems like the most appropriate error to return here + return VK_ERROR_INITIALIZATION_FAILED; +} + diff --git a/src/glfw_config.h.in b/src/glfw_config.h.in index bc9f5d3e1..eed29a8c0 100644 --- a/src/glfw_config.h.in +++ b/src/glfw_config.h.in @@ -46,6 +46,9 @@ #cmakedefine _GLFW_MIR // Define this to 1 if building GLFW for OSMesa #cmakedefine _GLFW_OSMESA +// Define this to 1 if building GLFW for Android +#cmakedefine _GLFW_ANDROID + // Define this to 1 if building as a shared library / dynamic library / DLL #cmakedefine _GLFW_BUILD_DLL diff --git a/src/internal.h b/src/internal.h index a95d1f867..4c38103e4 100644 --- a/src/internal.h +++ b/src/internal.h @@ -190,6 +190,8 @@ typedef void (APIENTRY * PFN_vkVoidFunction)(void); #include "mir_platform.h" #elif defined(_GLFW_OSMESA) #include "null_platform.h" +#elif defined(_GLFW_ANDROID) + #include "android_platform.h" #else #error "No supported window creation API selected" #endif From af67cf3710e2eb140d0fe460cafdc6ce89e1d747 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Sat, 9 Sep 2017 10:03:40 +0530 Subject: [PATCH 02/21] Fix finding Vulkan on Android --- CMake/modules/FindVulkan.cmake | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMake/modules/FindVulkan.cmake b/CMake/modules/FindVulkan.cmake index 5bdb55605..731de2f31 100644 --- a/CMake/modules/FindVulkan.cmake +++ b/CMake/modules/FindVulkan.cmake @@ -34,6 +34,9 @@ elseif (APPLE) find_library(VULKAN_STATIC_LIBRARY MoltenVK) find_path(VULKAN_INCLUDE_DIR NAMES vulkan/vulkan.h HINTS "${VULKAN_LIBRARY}/Headers") +elseif (ANDROID) + find_path(VULKAN_INCLUDE_DIR NAMES vulkan/vulkan.h NO_CMAKE_FIND_ROOT_PATH) + find_library(VULKAN_LIBRARY NAMES vulkan NO_CMAKE_FIND_ROOT_PATH) else() find_path(VULKAN_INCLUDE_DIR NAMES vulkan/vulkan.h HINTS "$ENV{VULKAN_SDK}/include") From b10f4cddcf3e4854c56189c099a0c57c9790d47a Mon Sep 17 00:00:00 2001 From: Curi0 Date: Sat, 9 Sep 2017 11:37:07 +0530 Subject: [PATCH 03/21] Add Android Vulkan surface creation very untested may not work at all --- src/android_platform.h | 21 +++++++++++++---- src/android_window.c | 51 +++++++++++++++++++++++++++++++++--------- src/internal.h | 3 +++ src/vulkan.c | 3 +++ 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/android_platform.h b/src/android_platform.h index 02dbe8f70..4d4f283a1 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -27,8 +27,6 @@ #include -#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNull null - #define _GLFW_PLATFORM_CONTEXT_STATE #define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE @@ -40,16 +38,31 @@ #include "posix_time.h" #include "posix_thread.h" #include "android_joystick.h" +#include #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL) #define _glfw_dlclose(handle) dlclose(handle) #define _glfw_dlsym(handle, name) dlsym(handle, name) +#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowAndroid android + // Null-specific per-window data // -typedef struct _GLFWwindowNull +typedef struct _GLFWwindowAndroid { int width; 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*); diff --git a/src/android_window.c b/src/android_window.c index 2e627672c..424fedbb4 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -31,8 +31,8 @@ static int createNativeWindow(_GLFWwindow* window, const _GLFWwndconfig* wndconfig) { - window->null.width = wndconfig->width; - window->null.height = wndconfig->height; + window->android.width = wndconfig->width; + window->android.height = wndconfig->height; return GLFW_TRUE; } @@ -104,15 +104,15 @@ void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) { if (width) - *width = window->null.width; + *width = window->android.width; if (height) - *height = window->null.height; + *height = window->android.height; } void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) { - window->null.width = width; - window->null.height = height; + window->android.width = width; + window->android.height = height; } void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window, @@ -128,9 +128,9 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) { if (width) - *width = window->null.width; + *width = window->android.width; if (height) - *height = window->null.height; + *height = window->android.height; } void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, @@ -273,13 +273,18 @@ int _glfwPlatformGetKeyScancode(int key) 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, VkPhysicalDevice device, uint32_t queuefamily) { - return GLFW_FALSE; + return GLFW_TRUE; } VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, @@ -287,7 +292,31 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface) { - // This seems like the most appropriate error to return here - return VK_ERROR_INITIALIZATION_FAILED; + VkResult err; + 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; } diff --git a/src/internal.h b/src/internal.h index 4c38103e4..3f3575b02 100644 --- a/src/internal.h +++ b/src/internal.h @@ -127,6 +127,7 @@ typedef enum VkStructureType VK_STRUCTURE_TYPE_MIR_SURFACE_CREATE_INFO_KHR = 1000007000, VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR = 1000009000, VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK = 1000053000, + VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR = 1000008000, VK_STRUCTURE_TYPE_MAX_ENUM = 0x7FFFFFFF } VkStructureType; @@ -590,6 +591,8 @@ struct _GLFWlibrary GLFWbool KHR_wayland_surface; #elif defined(_GLFW_MIR) GLFWbool KHR_mir_surface; +#elif defined(_GLFW_ANDROID) + GLFWbool KHR_android_surface; #endif } vk; diff --git a/src/vulkan.c b/src/vulkan.c index f2473cf6e..70730407d 100644 --- a/src/vulkan.c +++ b/src/vulkan.c @@ -137,6 +137,9 @@ GLFWbool _glfwInitVulkan(int mode) #elif defined(_GLFW_MIR) else if (strcmp(ep[i].extensionName, "VK_KHR_mir_surface") == 0) _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 } From a49f7b94df0082ae273829a2eae8371a2ed554ab Mon Sep 17 00:00:00 2001 From: Curi0 Date: Sat, 16 Sep 2017 15:06:48 +0530 Subject: [PATCH 04/21] Add EGL context support and entry point untested trying to link it with a native activity results in a linker error --- include/GLFW/glfw3.h | 3 ++- src/CMakeLists.txt | 5 +++-- src/android_platform.h | 18 ++++++++++-------- src/android_window.c | 43 +++++++++++++++++++++++------------------- src/egl_context.c | 8 ++++++-- src/egl_context.h | 26 +------------------------ 6 files changed, 46 insertions(+), 57 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 05e76a9ae..1d8c43e13 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -197,7 +197,7 @@ extern "C" { #include #endif - #else /*__APPLE__*/ + #elif !defined(ANDROID) /*__APPLE__*/ #include #if defined(GLFW_INCLUDE_GLEXT) @@ -5202,3 +5202,4 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window #endif /* _glfw3_h_ */ +extern void glfwMain(); \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9560110cc..c58314187 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -63,7 +63,7 @@ elseif (_GLFW_ANDROID) set(glfw_HEADERS ${common_HEADERS} android_platform.h android_joystick.h posix_time.h posix_thread.h) set(glfw_SOURCES ${common_SOURCES} android_init.c android_monitor.c android_window.c - android_joystick.c posix_time.c posix_thread.c) + android_joystick.c posix_time.c posix_thread.c egl_context.c osmesa_context.c ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c) endif() if (APPLE) @@ -100,7 +100,8 @@ target_include_directories(glfw PUBLIC target_include_directories(glfw PRIVATE "${GLFW_SOURCE_DIR}/src" "${GLFW_BINARY_DIR}/src" - ${glfw_INCLUDE_DIRS}) + ${glfw_INCLUDE_DIRS} + ${ANDROID_NDK}/sources/android/native_app_glue/) # HACK: When building on MinGW, WINVER and UNICODE need to be defined before # the inclusion of stddef.h (by glfw3.h), which is itself included before diff --git a/src/android_platform.h b/src/android_platform.h index 4d4f283a1..14f3baf0e 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -27,24 +27,26 @@ #include -#define _GLFW_PLATFORM_CONTEXT_STATE -#define _GLFW_PLATFORM_MONITOR_STATE -#define _GLFW_PLATFORM_CURSOR_STATE -#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE -#define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE -#define _GLFW_EGL_CONTEXT_STATE -#define _GLFW_EGL_LIBRARY_CONTEXT_STATE +#include "egl_context.h" #include "osmesa_context.h" #include "posix_time.h" #include "posix_thread.h" #include "android_joystick.h" #include +#include #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL) #define _glfw_dlclose(handle) dlclose(handle) #define _glfw_dlsym(handle, name) dlsym(handle, name) +#define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android.app->window) #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowAndroid android +#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE +#define _GLFW_PLATFORM_MONITOR_STATE +#define _GLFW_PLATFORM_CURSOR_STATE + +#define _GLFW_PLATFORM_CONTEXT_STATE +#define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE // Null-specific per-window data // @@ -52,7 +54,7 @@ typedef struct _GLFWwindowAndroid { int width; int height; - ANativeWindow *window; + struct android_app *app; } _GLFWwindowAndroid; typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; diff --git a/src/android_window.c b/src/android_window.c index 424fedbb4..b375c6c99 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -26,18 +26,24 @@ //======================================================================== #include "internal.h" +#include +#include -static int createNativeWindow(_GLFWwindow* window, - const _GLFWwndconfig* wndconfig) -{ - window->android.width = wndconfig->width; - window->android.height = wndconfig->height; +struct engine { + struct android_app* app; +}; +struct engine engine; + +// Android Entry Point +void android_main(struct android_app *app) { + memset(&engine, 0, sizeof(engine)); + app->userData = &engine; + engine.app = app; + + glfwMain(); - return GLFW_TRUE; } - - ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -47,24 +53,23 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - if (!createNativeWindow(window, wndconfig)) - return GLFW_FALSE; - + window->android.app = engine.app; if (ctxconfig->client != GLFW_NO_API) { - if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API || - ctxconfig->source == GLFW_OSMESA_CONTEXT_API) + if (ctxconfig->source == GLFW_EGL_CONTEXT_API) + { + if (!_glfwInitEGL()) + return GLFW_FALSE; + if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig)) + return GLFW_FALSE; + } + else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) { if (!_glfwInitOSMesa()) return GLFW_FALSE; if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig)) return GLFW_FALSE; } - else - { - _glfwInputError(GLFW_API_UNAVAILABLE, "Null: EGL not available"); - return GLFW_FALSE; - } } return GLFW_TRUE; @@ -307,7 +312,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, memset(&sci, 0, sizeof(sci)); sci.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; - sci.surface = window->android.window; + sci.surface = window->android.app->window; err = vkCreateAndroidSurfaceKHR(instance, &sci, allocator, surface); if (err) diff --git a/src/egl_context.c b/src/egl_context.c index d1bbbf08d..f8ee4bf7c 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -373,8 +373,12 @@ GLFWbool _glfwInitEGL(void) _glfwTerminateEGL(); return GLFW_FALSE; } - - _glfw.egl.display = eglGetDisplay(_GLFW_EGL_NATIVE_DISPLAY); + #if !defined(ANDROID) + _glfw.egl.display = eglGetDisplay(_GLFW_EGL_NATIVE_DISPLAY); + #else + _glfw.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY); + #endif + if (_glfw.egl.display == EGL_NO_DISPLAY) { _glfwInputError(GLFW_API_UNAVAILABLE, diff --git a/src/egl_context.h b/src/egl_context.h index bfab5111f..640d5d82a 100644 --- a/src/egl_context.h +++ b/src/egl_context.h @@ -25,31 +25,7 @@ // //======================================================================== -#if defined(_GLFW_USE_EGLPLATFORM_H) - #include -#elif defined(_GLFW_WIN32) - #define EGLAPIENTRY __stdcall -typedef HDC EGLNativeDisplayType; -typedef HWND EGLNativeWindowType; -#elif defined(_GLFW_COCOA) - #define EGLAPIENTRY -typedef void* EGLNativeDisplayType; -typedef id EGLNativeWindowType; -#elif defined(_GLFW_X11) - #define EGLAPIENTRY -typedef Display* EGLNativeDisplayType; -typedef Window EGLNativeWindowType; -#elif defined(_GLFW_WAYLAND) - #define EGLAPIENTRY -typedef struct wl_display* EGLNativeDisplayType; -typedef struct wl_egl_window* EGLNativeWindowType; -#elif defined(_GLFW_MIR) - #define EGLAPIENTRY -typedef MirEGLNativeDisplayType EGLNativeDisplayType; -typedef MirEGLNativeWindowType EGLNativeWindowType; -#else - #error "No supported EGL platform selected" -#endif +#include #define EGL_SUCCESS 0x3000 #define EGL_NOT_INITIALIZED 0x3001 From 28e66f717a1627e251b24f639a7592ec1785d91b Mon Sep 17 00:00:00 2001 From: Curi0 Date: Tue, 26 Sep 2017 11:37:36 +0530 Subject: [PATCH 05/21] Fix Vulkan library loading on Android --- src/vulkan.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/vulkan.c b/src/vulkan.c index 70730407d..7a09be54d 100644 --- a/src/vulkan.c +++ b/src/vulkan.c @@ -53,6 +53,8 @@ GLFWbool _glfwInitVulkan(int mode) _glfw.vk.handle = _glfw_dlopen("vulkan-1.dll"); #elif defined(_GLFW_COCOA) _glfw.vk.handle = _glfw_dlopen("libMoltenVK.dylib"); +#elif defined(_GLFW_ANDROID) + _glfw.vk.handle = _glfw_dlopen("libvulkan.so"); #else _glfw.vk.handle = _glfw_dlopen("libvulkan.so.1"); #endif From 83c4c2d81a585d8b50853159bdd05bcabc25f6ab Mon Sep 17 00:00:00 2001 From: Curi0 Date: Wed, 27 Sep 2017 18:25:49 +0530 Subject: [PATCH 06/21] Fix pointer related issues and add glfwGetAndroidApp --- include/GLFW/glfw3native.h | 4 ++++ src/android_platform.h | 2 +- src/android_window.c | 17 ++++++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index 4372cb766..6154bb758 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -564,6 +564,10 @@ GLFWAPI int glfwGetOSMesaDepthBuffer(GLFWwindow* window, int* width, int* height GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow* window); #endif +#ifdef defined(GLFW_EXPOSE_NATIVE_ANDROID) +GLFWAPI HWND glfwGetAndroidApp(GLFWwindow* window); +#endif + #ifdef __cplusplus } #endif diff --git a/src/android_platform.h b/src/android_platform.h index 14f3baf0e..4d20f9008 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -63,7 +63,7 @@ typedef struct VkAndroidSurfaceCreateInfoKHR { VkStructureType sType; const void* pNext; VkAndroidSurfaceCreateFlagsKHR flags; - ANativeWindow* surface; + ANativeWindow* window; } VkAndroidSurfaceCreateInfoKHR; typedef VkResult (APIENTRY *PFN_vkCreateAndroidSurfaceKHR)(VkInstance,const VkAndroidSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*); diff --git a/src/android_window.c b/src/android_window.c index b375c6c99..5697b6700 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -39,7 +39,7 @@ struct engine engine; void android_main(struct android_app *app) { memset(&engine, 0, sizeof(engine)); app->userData = &engine; - engine.app = app; + engine.app = &app; glfwMain(); @@ -53,7 +53,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - window->android.app = engine.app; + window->android.app = &engine.app; if (ctxconfig->client != GLFW_NO_API) { if (ctxconfig->source == GLFW_EGL_CONTEXT_API) @@ -312,7 +312,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, memset(&sci, 0, sizeof(sci)); sci.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; - sci.surface = window->android.app->window; + sci.window = &window->android.app->window; err = vkCreateAndroidSurfaceKHR(instance, &sci, allocator, surface); if (err) @@ -325,3 +325,14 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, return err; } +////////////////////////////////////////////////////////////////////////// +////// GLFW native API ////// +////////////////////////////////////////////////////////////////////////// + +GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* handle) +{ + _GLFWwindow* window = (_GLFWwindow*) handle; + _GLFW_REQUIRE_INIT_OR_RETURN(NULL); + return window->android.app; +} + From 7848a3c72c7637e6ec41efe3e09815bd9e1747f3 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Wed, 27 Sep 2017 21:21:37 +0530 Subject: [PATCH 07/21] Vulkan now fully working Tested on Xiaomi Redmi Note 4X runnning latest global update --- src/android_window.c | 56 +++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/android_window.c b/src/android_window.c index 5697b6700..732d83c8d 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -29,19 +29,45 @@ #include #include +struct android_app* app; -struct engine { - struct android_app* app; -}; -struct engine engine; +void handle_cmd(struct android_app* _app, int32_t cmd) { + switch (cmd) { + case APP_CMD_INIT_WINDOW: + // The window is being shown, get it ready. + app = _app; + __android_log_print(ANDROID_LOG_INFO, "GLFW", + "Window initialized", cmd); + default: + __android_log_print(ANDROID_LOG_INFO, "GLFW", + "event not handled: %d", cmd); + } +} // Android Entry Point -void android_main(struct android_app *app) { - memset(&engine, 0, sizeof(engine)); - app->userData = &engine; - engine.app = &app; +void android_main(struct android_app *app) { + app->onAppCmd = handle_cmd; + pthread_t my_thread; + pthread_create(&my_thread, NULL, &glfwMain, NULL); - glfwMain(); + while (1) { + int ident; + int events; + struct android_poll_source* source; + + while ((ident=ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) { + + // Process this event. + if (source != NULL) { + source->process(app, source); + } + + // Check if we are exiting. + if (app->destroyRequested != 0) { + return; + } + } + } } ////////////////////////////////////////////////////////////////////////// @@ -53,7 +79,6 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - window->android.app = &engine.app; if (ctxconfig->client != GLFW_NO_API) { if (ctxconfig->source == GLFW_EGL_CONTEXT_API) @@ -279,7 +304,7 @@ int _glfwPlatformGetKeyScancode(int key) void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) { if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_android_surface) - return; + return; extensions[0] = "VK_KHR_surface"; extensions[1] = "VK_KHR_android_surface"; @@ -302,7 +327,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR; vkCreateAndroidSurfaceKHR = (PFN_vkCreateAndroidSurfaceKHR) - vkGetInstanceProcAddr(instance, "vkCreateAndroidSurfaceKHR"); + vkGetInstanceProcAddr(instance, "vkCreateAndroidSurfaceKHR"); if (!vkCreateAndroidSurfaceKHR) { _glfwInputError(GLFW_API_UNAVAILABLE, @@ -312,7 +337,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, memset(&sci, 0, sizeof(sci)); sci.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; - sci.window = &window->android.app->window; + sci.window = app->window; err = vkCreateAndroidSurfaceKHR(instance, &sci, allocator, surface); if (err) @@ -333,6 +358,5 @@ GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* handle) { _GLFWwindow* window = (_GLFWwindow*) handle; _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - return window->android.app; -} - + return app; +} \ No newline at end of file From 672fed88b7fb884608741e829e5419bff8816b56 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Thu, 28 Sep 2017 13:11:57 +0530 Subject: [PATCH 08/21] Fix error where the app would sometimes crash due to app being NULL --- include/GLFW/glfw3.h | 2 +- src/android_window.c | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 1d8c43e13..9ea2f4046 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5202,4 +5202,4 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window #endif /* _glfw3_h_ */ -extern void glfwMain(); \ No newline at end of file +extern int main(); \ No newline at end of file diff --git a/src/android_window.c b/src/android_window.c index 732d83c8d..bd31e98bf 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -37,7 +37,7 @@ void handle_cmd(struct android_app* _app, int32_t cmd) { // The window is being shown, get it ready. app = _app; __android_log_print(ANDROID_LOG_INFO, "GLFW", - "Window initialized", cmd); + "Window initialized"); default: __android_log_print(ANDROID_LOG_INFO, "GLFW", "event not handled: %d", cmd); @@ -47,8 +47,7 @@ void handle_cmd(struct android_app* _app, int32_t cmd) { // Android Entry Point void android_main(struct android_app *app) { app->onAppCmd = handle_cmd; - pthread_t my_thread; - pthread_create(&my_thread, NULL, &glfwMain, NULL); + pthread_t t;pthread_create(&t, NULL, &main, NULL); // Call the main entry point while (1) { int ident; @@ -79,6 +78,9 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { + while (app == NULL); // Wait for the app to be initialized or the app will crash occasionally + window->android.app = app; + if (ctxconfig->client != GLFW_NO_API) { if (ctxconfig->source == GLFW_EGL_CONTEXT_API) @@ -337,7 +339,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, memset(&sci, 0, sizeof(sci)); sci.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; - sci.window = app->window; + sci.window = window->android.app->window; err = vkCreateAndroidSurfaceKHR(instance, &sci, allocator, surface); if (err) @@ -356,7 +358,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* handle) { - _GLFWwindow* window = (_GLFWwindow*) handle; + _GLFWwindow* window = (_GLFWwindow*)handle; _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - return app; + return window->android.app; } \ No newline at end of file From 49ded3c18e8ffc2cc0be9f7271eb480b2133f984 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Sat, 30 Sep 2017 20:21:18 +0530 Subject: [PATCH 09/21] EGL fixes and updated code --- src/android_window.c | 7 ++++++- src/egl_context.c | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/android_window.c b/src/android_window.c index bd31e98bf..1fd7e9384 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -83,7 +83,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (ctxconfig->client != GLFW_NO_API) { - if (ctxconfig->source == GLFW_EGL_CONTEXT_API) + if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API | GLFW_EGL_CONTEXT_API) { if (!_glfwInitEGL()) return GLFW_FALSE; @@ -319,6 +319,11 @@ int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, return GLFW_TRUE; } +int _glfwPlatformFramebufferTransparent(_GLFWwindow* window) +{ + return GLFW_FALSE; +} + VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, _GLFWwindow* window, const VkAllocationCallbacks* allocator, diff --git a/src/egl_context.c b/src/egl_context.c index 9fb789f7f..23981c8d6 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -630,9 +630,11 @@ GLFWbool _glfwCreateContextEGL(_GLFWwindow* window, "libGLES_CM.dll", #elif defined(_GLFW_COCOA) "libGLESv1_CM.dylib", +#elif defined(_GLFW_ANDROID) + "libGLESv1_CM.so", + "libGLES_CM.so", #else "libGLESv1_CM.so.1", - "libGLES_CM.so.1", #endif NULL }; @@ -647,6 +649,8 @@ GLFWbool _glfwCreateContextEGL(_GLFWwindow* window, "libGLESv2.dylib", #elif defined(__CYGWIN__) "libGLESv2-2.so", +#elif defined(_GLFW_ANDROID) + "libGLESv2.so", #else "libGLESv2.so.2", #endif From e1c09aa2b53981ae78285a9a2995da768442b404 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Mon, 2 Oct 2017 17:29:32 +0530 Subject: [PATCH 10/21] More fixes Rendering now works if it is on the main thread --- src/context.c | 6 ------ src/egl_context.c | 4 +++- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/context.c b/src/context.c index 3842f0a37..3b4336994 100644 --- a/src/context.c +++ b/src/context.c @@ -585,12 +585,6 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle) _GLFW_REQUIRE_INIT(); - if (window && window->context.client == GLFW_NO_API) - { - _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL); - return; - } - if (previous) { if (!window || window->context.source != previous->context.source) diff --git a/src/egl_context.c b/src/egl_context.c index 23981c8d6..6140f0f78 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -311,6 +311,8 @@ GLFWbool _glfwInitEGL(void) "libEGL.dylib", #elif defined(__CYGWIN__) "libEGL-1.so", +#elif defined(_GLFW_ANDROID) + "libEGL.so", #else "libEGL.so.1", #endif @@ -465,7 +467,6 @@ GLFWbool _glfwCreateContextEGL(_GLFWwindow* window, EGLConfig config; EGLContext share = NULL; int index = 0; - if (!_glfw.egl.display) { _glfwInputError(GLFW_API_UNAVAILABLE, "EGL: API not available"); @@ -635,6 +636,7 @@ GLFWbool _glfwCreateContextEGL(_GLFWwindow* window, "libGLES_CM.so", #else "libGLESv1_CM.so.1", + "libGLES_CM.so.1" #endif NULL }; From e5d800c344dac3e4b95ee6c5c326dbd8f0714594 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Tue, 3 Oct 2017 13:38:28 +0530 Subject: [PATCH 11/21] Cleanup and refractoring EGL / OpenGL ES also now working --- include/GLFW/glfw3.h | 4 +--- src/android_init.c | 39 +++++++++++++++++++++++++++++++++++++++ src/android_platform.h | 5 +++-- src/android_window.c | 41 ----------------------------------------- 4 files changed, 43 insertions(+), 46 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 17ef60cc3..8d1600d91 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5207,6 +5207,4 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window } #endif -#endif /* _glfw3_h_ */ - -extern int main(); \ No newline at end of file +#endif /* _glfw3_h_ */ \ No newline at end of file diff --git a/src/android_init.c b/src/android_init.c index b03b44476..b94e597ca 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -25,9 +25,47 @@ // //======================================================================== +#include #include "internal.h" +extern int main(); +void handle_cmd(struct android_app* _app, int32_t cmd) { + switch (cmd) { + case APP_CMD_INIT_WINDOW: + // The window is being shown so the initialization is finished. + app = _app; + break; + default: + __android_log_print(ANDROID_LOG_INFO, "GLFW", + "event not handled: %d", cmd); + } +} +// Android Entry Point +void android_main(struct android_app *app) { + app->onAppCmd = handle_cmd; + pthread_t t;pthread_create(&t, NULL, &main, NULL); // Call the main entry point + + while (1) { + int ident; + int events; + struct android_poll_source* source; + + while ((ident=ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) { + + // Process this event. + if (source != NULL) { + source->process(app, source); + } + + // Check if we are exiting. + if (app->destroyRequested != 0) { + return; + } + } + } + +} ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -35,6 +73,7 @@ int _glfwPlatformInit(void) { _glfwInitTimerPOSIX(); + while (app == NULL); // Wait for the app to be initialized or the app will crash occasionally return GLFW_TRUE; } diff --git a/src/android_platform.h b/src/android_platform.h index 4d20f9008..f97f893e3 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -48,8 +48,9 @@ #define _GLFW_PLATFORM_CONTEXT_STATE #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE -// Null-specific per-window data -// +struct android_app* app; + +// Android-specific per-window data typedef struct _GLFWwindowAndroid { int width; diff --git a/src/android_window.c b/src/android_window.c index 1fd7e9384..4056438c0 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -29,46 +29,6 @@ #include #include -struct android_app* app; - -void handle_cmd(struct android_app* _app, int32_t cmd) { - switch (cmd) { - case APP_CMD_INIT_WINDOW: - // The window is being shown, get it ready. - app = _app; - __android_log_print(ANDROID_LOG_INFO, "GLFW", - "Window initialized"); - default: - __android_log_print(ANDROID_LOG_INFO, "GLFW", - "event not handled: %d", cmd); - } -} - -// Android Entry Point -void android_main(struct android_app *app) { - app->onAppCmd = handle_cmd; - pthread_t t;pthread_create(&t, NULL, &main, NULL); // Call the main entry point - - while (1) { - int ident; - int events; - struct android_poll_source* source; - - while ((ident=ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) { - - // Process this event. - if (source != NULL) { - source->process(app, source); - } - - // Check if we are exiting. - if (app->destroyRequested != 0) { - return; - } - } - } - -} ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -78,7 +38,6 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - while (app == NULL); // Wait for the app to be initialized or the app will crash occasionally window->android.app = app; if (ctxconfig->client != GLFW_NO_API) From 7e56f7463365fbf87225bd609f3791fdd701bbbd Mon Sep 17 00:00:00 2001 From: Curi0 Date: Tue, 3 Oct 2017 14:37:57 +0530 Subject: [PATCH 12/21] Fix CI build --- src/egl_context.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/egl_context.h b/src/egl_context.h index 4304369cd..fded86445 100644 --- a/src/egl_context.h +++ b/src/egl_context.h @@ -25,7 +25,35 @@ // //======================================================================== +#if defined(_GLFW_USE_EGLPLATFORM_H) #include +#elif defined(_GLFW_ANDROID) +#define EGLAPIENTRY +typedef void* EGLNativeDisplayType; +typedef struct ANativeWindow* EGLNativeWindowType; +#elif defined(_GLFW_WIN32) +#define EGLAPIENTRY __stdcall +typedef HDC EGLNativeDisplayType; +typedef HWND EGLNativeWindowType; +#elif defined(_GLFW_COCOA) +#define EGLAPIENTRY +typedef void* EGLNativeDisplayType; +typedef id EGLNativeWindowType; +#elif defined(_GLFW_X11) +#define EGLAPIENTRY +typedef Display* EGLNativeDisplayType; +typedef Window EGLNativeWindowType; +#elif defined(_GLFW_WAYLAND) +#define EGLAPIENTRY +typedef struct wl_display* EGLNativeDisplayType; +typedef struct wl_egl_window* EGLNativeWindowType; +#elif defined(_GLFW_MIR) +#define EGLAPIENTRY +typedef MirEGLNativeDisplayType EGLNativeDisplayType; +typedef MirEGLNativeWindowType EGLNativeWindowType; +#else +#error "No supported EGL platform selected" +#endif #define EGL_SUCCESS 0x3000 #define EGL_NOT_INITIALIZED 0x3001 From 51e0ca05611ac50547031352091e9aa395f784c0 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Tue, 3 Oct 2017 15:10:19 +0530 Subject: [PATCH 13/21] Fix building on Linux --- src/egl_context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/egl_context.c b/src/egl_context.c index 6140f0f78..40aa25f65 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -636,7 +636,7 @@ GLFWbool _glfwCreateContextEGL(_GLFWwindow* window, "libGLES_CM.so", #else "libGLESv1_CM.so.1", - "libGLES_CM.so.1" + "libGLES_CM.so.1", #endif NULL }; From 1daf1401fb07c556c8216657ba6e0c49b91ed6ee Mon Sep 17 00:00:00 2001 From: Curi0 Date: Wed, 4 Oct 2017 20:55:01 +0530 Subject: [PATCH 14/21] Polish code --- include/GLFW/glfw3native.h | 5 +++-- src/android_init.c | 12 +++--------- src/android_window.c | 2 +- src/context.c | 5 +++++ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index 6154bb758..f9ec60eef 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -564,8 +564,9 @@ GLFWAPI int glfwGetOSMesaDepthBuffer(GLFWwindow* window, int* width, int* height GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow* window); #endif -#ifdef defined(GLFW_EXPOSE_NATIVE_ANDROID) -GLFWAPI HWND glfwGetAndroidApp(GLFWwindow* window); +#if defined(GLFW_EXPOSE_NATIVE_ANDROID) +#include +GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* window); #endif #ifdef __cplusplus diff --git a/src/android_init.c b/src/android_init.c index b94e597ca..98d20f76f 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -30,21 +30,15 @@ extern int main(); void handle_cmd(struct android_app* _app, int32_t cmd) { - switch (cmd) { + switch (cmd) case APP_CMD_INIT_WINDOW: - // The window is being shown so the initialization is finished. - app = _app; - break; - default: - __android_log_print(ANDROID_LOG_INFO, "GLFW", - "event not handled: %d", cmd); - } + app = _app; // The window is being shown so the initialization is finished. } // Android Entry Point void android_main(struct android_app *app) { app->onAppCmd = handle_cmd; - pthread_t t;pthread_create(&t, NULL, &main, NULL); // Call the main entry point + pthread_create(&(pthread_t){0}, NULL, (void*)&main, NULL); // Call the main entry point while (1) { int ident; diff --git a/src/android_window.c b/src/android_window.c index 4056438c0..d47778d2c 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -42,7 +42,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (ctxconfig->client != GLFW_NO_API) { - if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API | GLFW_EGL_CONTEXT_API) + if ((ctxconfig->source == GLFW_NATIVE_CONTEXT_API) | (ctxconfig->source == GLFW_EGL_CONTEXT_API)) { if (!_glfwInitEGL()) return GLFW_FALSE; diff --git a/src/context.c b/src/context.c index 3b4336994..71352665f 100644 --- a/src/context.c +++ b/src/context.c @@ -585,6 +585,11 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle) _GLFW_REQUIRE_INIT(); + if (window && window->context.client == GLFW_NO_API) + { + _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL); + return; + } if (previous) { if (!window || window->context.source != previous->context.source) From 56816cd0ba026da4c85865a22727673c94e1129e Mon Sep 17 00:00:00 2001 From: Curi0 Date: Wed, 11 Oct 2017 13:09:25 +0530 Subject: [PATCH 15/21] Add initial input support , fix bugs , refractor and implement some methods Rotation also works now --- src/android_init.c | 32 +++++++++++++---------- src/android_platform.h | 13 +++------- src/android_window.c | 58 ++++++++++++++++++++++++++---------------- src/context.c | 1 + 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/android_init.c b/src/android_init.c index 98d20f76f..a05f90880 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -30,9 +30,21 @@ extern int main(); void handle_cmd(struct android_app* _app, int32_t cmd) { - switch (cmd) - case APP_CMD_INIT_WINDOW: - app = _app; // The window is being shown so the initialization is finished. + switch (cmd) { + case APP_CMD_INIT_WINDOW: { + app = _app; // The window is being shown so the initialization is finished. + break; + } + case APP_CMD_LOST_FOCUS: { + break; + } + case APP_CMD_GAINED_FOCUS: { + break; + } + case APP_CMD_TERM_WINDOW: { + glfwDestroyWindow((GLFWwindow *) _glfw.windowListHead); + } +} } // Android Entry Point @@ -45,18 +57,10 @@ void android_main(struct android_app *app) { int events; struct android_poll_source* source; - while ((ident=ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) { - - // Process this event. - if (source != NULL) { + // Process events + while ((ident=ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) + if (source != NULL) source->process(app, source); - } - - // Check if we are exiting. - if (app->destroyRequested != 0) { - return; - } - } } } diff --git a/src/android_platform.h b/src/android_platform.h index f97f893e3..5e27bc8d0 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -39,8 +39,8 @@ #define _glfw_dlclose(handle) dlclose(handle) #define _glfw_dlsym(handle, name) dlsym(handle, name) -#define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android.app->window) -#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowAndroid android +#define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android->window) +#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE #define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE @@ -48,15 +48,8 @@ #define _GLFW_PLATFORM_CONTEXT_STATE #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE -struct android_app* app; +struct android_app *app; -// Android-specific per-window data -typedef struct _GLFWwindowAndroid -{ - int width; - int height; - struct android_app *app; -} _GLFWwindowAndroid; typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; diff --git a/src/android_window.c b/src/android_window.c index d47778d2c..a46869c6f 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -26,8 +26,20 @@ //======================================================================== #include "internal.h" -#include -#include + +float x,y; + +static int32_t handle_input(struct android_app* app, AInputEvent* event) +{ + if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) + for (size_t i = 0; i < AMotionEvent_getPointerCount(event); ++i) { + x = AMotionEvent_getX(event, i); + y = AMotionEvent_getY(event, i); + } + if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) + _glfwInputKey(_glfw.windowListHead, 0 , AKeyEvent_getKeyCode(event), GLFW_PRESS,0); + return 0; +} ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// @@ -38,33 +50,36 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - window->android.app = app; + window->android = app; + window->android->onInputEvent = handle_input; - if (ctxconfig->client != GLFW_NO_API) - { - if ((ctxconfig->source == GLFW_NATIVE_CONTEXT_API) | (ctxconfig->source == GLFW_EGL_CONTEXT_API)) - { + ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); + + if (ctxconfig->client != GLFW_NO_API) { + if ((ctxconfig->source == GLFW_NATIVE_CONTEXT_API) | + (ctxconfig->source == GLFW_EGL_CONTEXT_API)) { if (!_glfwInitEGL()) return GLFW_FALSE; if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig)) return GLFW_FALSE; - } - else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) - { + } else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) { if (!_glfwInitOSMesa()) return GLFW_FALSE; if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig)) return GLFW_FALSE; } + } return GLFW_TRUE; } + void _glfwPlatformDestroyWindow(_GLFWwindow* window) { if (window->context.destroy) window->context.destroy(window); + ANativeActivity_finish(window->android->activity); } void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) @@ -86,6 +101,7 @@ void _glfwPlatformSetWindowMonitor(_GLFWwindow* window, void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos) { + } void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) @@ -94,22 +110,19 @@ void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) { - if (width) - *width = window->android.width; - if (height) - *height = window->android.height; + *height = ANativeWindow_getHeight(window->android->window); + *width = ANativeWindow_getWidth(window->android->window); } void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) { - window->android.width = width; - window->android.height = height; } void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window, int minwidth, int minheight, int maxwidth, int maxheight) { + } void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) @@ -118,10 +131,7 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) { - if (width) - *width = window->android.width; - if (height) - *height = window->android.height; + } void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, @@ -197,10 +207,12 @@ int _glfwPlatformWindowVisible(_GLFWwindow* window) void _glfwPlatformPollEvents(void) { + _glfwInputCursorPos(_glfw.windowListHead, x ,y); } void _glfwPlatformWaitEvents(void) { + _glfwPlatformPollEvents(); } void _glfwPlatformWaitEventsTimeout(double timeout) @@ -213,6 +225,8 @@ void _glfwPlatformPostEmptyEvent(void) void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos) { + *xpos = x; + *ypos = y; } void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) @@ -303,7 +317,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, memset(&sci, 0, sizeof(sci)); sci.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; - sci.window = window->android.app->window; + sci.window = window->android->window; err = vkCreateAndroidSurfaceKHR(instance, &sci, allocator, surface); if (err) @@ -324,5 +338,5 @@ GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* handle) { _GLFWwindow* window = (_GLFWwindow*)handle; _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - return window->android.app; + return window->android; } \ No newline at end of file diff --git a/src/context.c b/src/context.c index 71352665f..3842f0a37 100644 --- a/src/context.c +++ b/src/context.c @@ -590,6 +590,7 @@ GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle) _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL); return; } + if (previous) { if (!window || window->context.source != previous->context.source) From 57dd782d2a61fc24f269fe8cca7659697be39b75 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Tue, 17 Oct 2017 10:48:57 +0530 Subject: [PATCH 16/21] Change copyright --- src/android_init.c | 2 +- src/android_joystick.c | 1 + src/android_monitor.c | 3 ++- src/android_platform.h | 2 +- src/android_window.c | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/android_init.c b/src/android_init.c index a05f90880..13100a21c 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -1,7 +1,7 @@ //======================================================================== // GLFW 3.3 - www.glfw.org //------------------------------------------------------------------------ -// Copyright (c) 2016 Google Inc. +// Copyright (c) 2017 Curi0 // Copyright (c) 2006-2016 Camilla Löwy // // This software is provided 'as-is', without any express or implied diff --git a/src/android_joystick.c b/src/android_joystick.c index afd65e15d..7f2849b7d 100644 --- a/src/android_joystick.c +++ b/src/android_joystick.c @@ -1,6 +1,7 @@ //======================================================================== // GLFW 3.3 - www.glfw.org //------------------------------------------------------------------------ +// Copyright (c) 2017 Curi0 // Copyright (c) 2006-2016 Camilla Löwy // // This software is provided 'as-is', without any express or implied diff --git a/src/android_monitor.c b/src/android_monitor.c index f5678bbc3..e9e211e5b 100644 --- a/src/android_monitor.c +++ b/src/android_monitor.c @@ -1,7 +1,7 @@ //======================================================================== // GLFW 3.3 - www.glfw.org //------------------------------------------------------------------------ -// Copyright (c) 2016 Google Inc. +// Copyright (c) 2017 Curi0 // Copyright (c) 2006-2016 Camilla Löwy // // This software is provided 'as-is', without any express or implied @@ -25,6 +25,7 @@ // //======================================================================== + #include "internal.h" diff --git a/src/android_platform.h b/src/android_platform.h index 5e27bc8d0..616cc088e 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -1,7 +1,7 @@ //======================================================================== // GLFW 3.3 - www.glfw.org //------------------------------------------------------------------------ -// Copyright (c) 2016 Google Inc. +// Copyright (c) 2017 Curi0 // Copyright (c) 2006-2016 Camilla Löwy // // This software is provided 'as-is', without any express or implied diff --git a/src/android_window.c b/src/android_window.c index a46869c6f..ab1c36fc4 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -1,7 +1,7 @@ //======================================================================== // GLFW 3.3 - www.glfw.org //------------------------------------------------------------------------ -// Copyright (c) 2016 Google Inc. +// Copyright (c) 2017 Curi0 // Copyright (c) 2006-2016 Camilla Löwy // // This software is provided 'as-is', without any express or implied From a0d99c8b59c8a20fe6188a31e6da1b8471768966 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Tue, 17 Oct 2017 18:44:16 +0530 Subject: [PATCH 17/21] Adding requested changes --- CMakeLists.txt | 7 +++++++ include/GLFW/glfw3.h | 4 ++-- include/GLFW/glfw3native.h | 3 +++ src/CMakeLists.txt | 3 +-- src/android_init.c | 2 +- src/android_platform.h | 2 +- src/android_window.c | 2 +- src/egl_context.c | 7 ++----- src/egl_context.h | 38 +++++++++++++++++++------------------- 9 files changed, 37 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4985675d..d876aba4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -333,6 +333,13 @@ if (_GLFW_COCOA) set(glfw_PKG_LIBS "-framework Cocoa -framework IOKit -framework CoreFoundation -framework CoreVideo") endif() +#-------------------------------------------------------------------- +# Use Android APIs for window creation +#-------------------------------------------------------------------- +if (_GLFW_ANDROID) + list(APPEND glfw_INCLUDE_DIRS "${ANDROID_NDK}/sources/android/native_app_glue/") +endif() + #-------------------------------------------------------------------- # Export GLFW library dependencies #-------------------------------------------------------------------- diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 8d1600d91..31c973206 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -197,7 +197,7 @@ extern "C" { #include #endif - #elif !defined(ANDROID) /*__APPLE__*/ + #else /*__APPLE__*/ #include #if defined(GLFW_INCLUDE_GLEXT) @@ -5207,4 +5207,4 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window } #endif -#endif /* _glfw3_h_ */ \ No newline at end of file +#endif /* _glfw3_h_ */ diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index f9ec60eef..f499a53bd 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -123,6 +123,9 @@ extern "C" { #include #endif +#if defined(GLFW_EXPOSE_NATIVE_ANDROID) + #include +#endif /************************************************************************* * Functions diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c58314187..4ed950496 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -100,8 +100,7 @@ target_include_directories(glfw PUBLIC target_include_directories(glfw PRIVATE "${GLFW_SOURCE_DIR}/src" "${GLFW_BINARY_DIR}/src" - ${glfw_INCLUDE_DIRS} - ${ANDROID_NDK}/sources/android/native_app_glue/) + ${glfw_INCLUDE_DIRS}) # HACK: When building on MinGW, WINVER and UNICODE need to be defined before # the inclusion of stddef.h (by glfw3.h), which is itself included before diff --git a/src/android_init.c b/src/android_init.c index 13100a21c..590e21252 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -82,6 +82,6 @@ void _glfwPlatformTerminate(void) const char* _glfwPlatformGetVersionString(void) { - return _GLFW_VERSION_NUMBER " Android"; + return _GLFW_VERSION_NUMBER " Android EGL"; } diff --git a/src/android_platform.h b/src/android_platform.h index 616cc088e..ea33266f2 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -47,10 +47,10 @@ #define _GLFW_PLATFORM_CONTEXT_STATE #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE +#define _GLFW_EGL_NATIVE_DISPLAY EGL_DEFAULT_DISPLAY struct android_app *app; - typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; typedef struct VkAndroidSurfaceCreateInfoKHR { diff --git a/src/android_window.c b/src/android_window.c index ab1c36fc4..1faf2308e 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -339,4 +339,4 @@ GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* handle) _GLFWwindow* window = (_GLFWwindow*)handle; _GLFW_REQUIRE_INIT_OR_RETURN(NULL); return window->android; -} \ No newline at end of file +} diff --git a/src/egl_context.c b/src/egl_context.c index 40aa25f65..742e7f4ac 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -393,11 +393,8 @@ GLFWbool _glfwInitEGL(void) _glfwTerminateEGL(); return GLFW_FALSE; } - #if !defined(ANDROID) - _glfw.egl.display = eglGetDisplay(_GLFW_EGL_NATIVE_DISPLAY); - #else - _glfw.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY); - #endif + + _glfw.egl.display = eglGetDisplay(_GLFW_EGL_NATIVE_DISPLAY); if (_glfw.egl.display == EGL_NO_DISPLAY) { diff --git a/src/egl_context.h b/src/egl_context.h index fded86445..2c4a3b6a8 100644 --- a/src/egl_context.h +++ b/src/egl_context.h @@ -26,31 +26,31 @@ //======================================================================== #if defined(_GLFW_USE_EGLPLATFORM_H) -#include + #include #elif defined(_GLFW_ANDROID) -#define EGLAPIENTRY -typedef void* EGLNativeDisplayType; -typedef struct ANativeWindow* EGLNativeWindowType; + #define EGLAPIENTRY + typedef void* EGLNativeDisplayType; + typedef struct ANativeWindow* EGLNativeWindowType; #elif defined(_GLFW_WIN32) -#define EGLAPIENTRY __stdcall -typedef HDC EGLNativeDisplayType; -typedef HWND EGLNativeWindowType; + #define EGLAPIENTRY __stdcall + typedef HDC EGLNativeDisplayType; + typedef HWND EGLNativeWindowType; #elif defined(_GLFW_COCOA) -#define EGLAPIENTRY -typedef void* EGLNativeDisplayType; -typedef id EGLNativeWindowType; + #define EGLAPIENTRY + typedef void* EGLNativeDisplayType; + typedef id EGLNativeWindowType; #elif defined(_GLFW_X11) -#define EGLAPIENTRY -typedef Display* EGLNativeDisplayType; -typedef Window EGLNativeWindowType; + #define EGLAPIENTRY + typedef Display* EGLNativeDisplayType; + typedef Window EGLNativeWindowType; #elif defined(_GLFW_WAYLAND) -#define EGLAPIENTRY -typedef struct wl_display* EGLNativeDisplayType; -typedef struct wl_egl_window* EGLNativeWindowType; + #define EGLAPIENTRY + typedef struct wl_display* EGLNativeDisplayType; + typedef struct wl_egl_window* EGLNativeWindowType; #elif defined(_GLFW_MIR) -#define EGLAPIENTRY -typedef MirEGLNativeDisplayType EGLNativeDisplayType; -typedef MirEGLNativeWindowType EGLNativeWindowType; + #define EGLAPIENTRY + typedef MirEGLNativeDisplayType EGLNativeDisplayType; + typedef MirEGLNativeWindowType EGLNativeWindowType; #else #error "No supported EGL platform selected" #endif From 51000fdbb705ca6f3825bffbbf785d89c4b2f411 Mon Sep 17 00:00:00 2001 From: Curi0 Date: Wed, 18 Oct 2017 10:18:33 +0530 Subject: [PATCH 18/21] More changes --- include/GLFW/glfw3native.h | 1 - src/CMakeLists.txt | 2 +- src/android_window.c | 28 ++++++++++++++++++---------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index f499a53bd..07ab74222 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -568,7 +568,6 @@ GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow* window); #endif #if defined(GLFW_EXPOSE_NATIVE_ANDROID) -#include GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* window); #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4ed950496..7cdb2f14a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -63,7 +63,7 @@ elseif (_GLFW_ANDROID) set(glfw_HEADERS ${common_HEADERS} android_platform.h android_joystick.h posix_time.h posix_thread.h) set(glfw_SOURCES ${common_SOURCES} android_init.c android_monitor.c android_window.c - android_joystick.c posix_time.c posix_thread.c egl_context.c osmesa_context.c ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c) + android_joystick.c posix_time.c posix_thread.c egl_context.c osmesa_context.c "${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c") endif() if (APPLE) diff --git a/src/android_window.c b/src/android_window.c index 1faf2308e..75b52ca20 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -32,7 +32,8 @@ float x,y; static int32_t handle_input(struct android_app* app, AInputEvent* event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) - for (size_t i = 0; i < AMotionEvent_getPointerCount(event); ++i) { + for (size_t i = 0; i < AMotionEvent_getPointerCount(event); ++i) + { x = AMotionEvent_getX(event, i); y = AMotionEvent_getY(event, i); } @@ -55,14 +56,18 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); - if (ctxconfig->client != GLFW_NO_API) { + if (ctxconfig->client != GLFW_NO_API) + { if ((ctxconfig->source == GLFW_NATIVE_CONTEXT_API) | - (ctxconfig->source == GLFW_EGL_CONTEXT_API)) { + (ctxconfig->source == GLFW_EGL_CONTEXT_API)) + { if (!_glfwInitEGL()) return GLFW_FALSE; if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig)) return GLFW_FALSE; - } else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) { + } + else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) + { if (!_glfwInitOSMesa()) return GLFW_FALSE; if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig)) @@ -70,7 +75,6 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, } } - return GLFW_TRUE; } @@ -110,8 +114,10 @@ void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) { - *height = ANativeWindow_getHeight(window->android->window); - *width = ANativeWindow_getWidth(window->android->window); + if (height) + *height = ANativeWindow_getHeight(window->android->window); + if (width) + *width = ANativeWindow_getWidth(window->android->window); } void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) @@ -225,8 +231,10 @@ void _glfwPlatformPostEmptyEvent(void) void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos) { - *xpos = x; - *ypos = y; + if (xpos) + *xpos = x; + if (ypos) + *ypos = y; } void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) @@ -336,7 +344,7 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* handle) { - _GLFWwindow* window = (_GLFWwindow*)handle; + _GLFWwindow *window = (_GLFWwindow*)handle; _GLFW_REQUIRE_INIT_OR_RETURN(NULL); return window->android; } From b6d4a0146dc346d7d37edc9a9ef9c11eacc3efbd Mon Sep 17 00:00:00 2001 From: Curi0 Date: Wed, 18 Oct 2017 19:24:33 +0530 Subject: [PATCH 19/21] Add _GLFW_PLATFORM_LIBRARY_WINDOW_STATE I tried adding a cursor state but it resulted in wierd segmentation fault errors --- src/android_init.c | 9 +++------ src/android_platform.h | 4 ++-- src/android_window.c | 7 +++---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/android_init.c b/src/android_init.c index 590e21252..ba572a4d6 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -32,7 +32,7 @@ extern int main(); void handle_cmd(struct android_app* _app, int32_t cmd) { switch (cmd) { case APP_CMD_INIT_WINDOW: { - app = _app; // The window is being shown so the initialization is finished. + _glfw.app = _app; // The window is being shown so the initialization is finished. break; } case APP_CMD_LOST_FOCUS: { @@ -53,12 +53,9 @@ void android_main(struct android_app *app) { pthread_create(&(pthread_t){0}, NULL, (void*)&main, NULL); // Call the main entry point while (1) { - int ident; - int events; struct android_poll_source* source; - // Process events - while ((ident=ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0) + while ((ALooper_pollAll(0, NULL, NULL,(void**)&source)) >= 0) if (source != NULL) source->process(app, source); } @@ -71,7 +68,7 @@ void android_main(struct android_app *app) { int _glfwPlatformInit(void) { _glfwInitTimerPOSIX(); - while (app == NULL); // Wait for the app to be initialized or the app will crash occasionally + while (_glfw.app == NULL); // Wait for the app to be initialized or the app will crash occasionally return GLFW_TRUE; } diff --git a/src/android_platform.h b/src/android_platform.h index ea33266f2..1e9da6210 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -41,7 +41,7 @@ #define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android->window) #define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android -#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE +#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE struct android_app* app #define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE @@ -49,7 +49,7 @@ #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE #define _GLFW_EGL_NATIVE_DISPLAY EGL_DEFAULT_DISPLAY -struct android_app *app; +float x,y; typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; diff --git a/src/android_window.c b/src/android_window.c index 75b52ca20..708aeda01 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -27,8 +27,6 @@ #include "internal.h" -float x,y; - static int32_t handle_input(struct android_app* app, AInputEvent* event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) @@ -39,6 +37,7 @@ static int32_t handle_input(struct android_app* app, AInputEvent* event) } if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) _glfwInputKey(_glfw.windowListHead, 0 , AKeyEvent_getKeyCode(event), GLFW_PRESS,0); + return 0; } @@ -51,7 +50,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - window->android = app; + window->android = _glfw.app; window->android->onInputEvent = handle_input; ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); @@ -213,7 +212,7 @@ int _glfwPlatformWindowVisible(_GLFWwindow* window) void _glfwPlatformPollEvents(void) { - _glfwInputCursorPos(_glfw.windowListHead, x ,y); + _glfwInputCursorPos(_glfw.windowListHead, x, y); } void _glfwPlatformWaitEvents(void) From d8db80828d4be98650d2a3295a273cbba3c441ec Mon Sep 17 00:00:00 2001 From: Andreas Streichardt Date: Sat, 2 Dec 2017 11:12:15 +0100 Subject: [PATCH 20/21] Add framebuffersize() implementation --- src/android_window.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/android_window.c b/src/android_window.c index 708aeda01..898928182 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -136,7 +136,10 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) { - + // the underlying buffergeometry is currently being initialized from the + // window width and height...so high resolution displays are currently + // not supported...so it is safe to just call GetWindowSize() for now + _glfwPlatformGetWindowSize(window, width, height); } void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, From 4d8935625b7252153e620656a233cb2bc7d7c52b Mon Sep 17 00:00:00 2001 From: Andreas Streichardt Date: Sun, 3 Dec 2017 11:33:30 +0100 Subject: [PATCH 21/21] Remove separate thread for android --- src/android_init.c | 18 ++++++------------ src/android_platform.h | 9 +++++++-- src/android_window.c | 28 +++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/android_init.c b/src/android_init.c index ba572a4d6..b0cc1b84b 100644 --- a/src/android_init.c +++ b/src/android_init.c @@ -28,11 +28,12 @@ #include #include "internal.h" +struct android_app* _globalApp; + extern int main(); void handle_cmd(struct android_app* _app, int32_t cmd) { switch (cmd) { case APP_CMD_INIT_WINDOW: { - _glfw.app = _app; // The window is being shown so the initialization is finished. break; } case APP_CMD_LOST_FOCUS: { @@ -50,16 +51,9 @@ void handle_cmd(struct android_app* _app, int32_t cmd) { // Android Entry Point void android_main(struct android_app *app) { app->onAppCmd = handle_cmd; - pthread_create(&(pthread_t){0}, NULL, (void*)&main, NULL); // Call the main entry point - - while (1) { - struct android_poll_source* source; - // Process events - while ((ALooper_pollAll(0, NULL, NULL,(void**)&source)) >= 0) - if (source != NULL) - source->process(app, source); - } - + // hmmm...global....eek + _globalApp = app; + main(); } ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// @@ -67,8 +61,8 @@ void android_main(struct android_app *app) { int _glfwPlatformInit(void) { + _glfw.gstate.app = _globalApp; _glfwInitTimerPOSIX(); - while (_glfw.app == NULL); // Wait for the app to be initialized or the app will crash occasionally return GLFW_TRUE; } diff --git a/src/android_platform.h b/src/android_platform.h index 1e9da6210..6dcf631f5 100644 --- a/src/android_platform.h +++ b/src/android_platform.h @@ -40,8 +40,8 @@ #define _glfw_dlsym(handle, name) dlsym(handle, name) #define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->android->window) -#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android -#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE struct android_app* app +#define _GLFW_PLATFORM_WINDOW_STATE struct android_app* android; +#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE android_gstate gstate; #define _GLFW_PLATFORM_MONITOR_STATE #define _GLFW_PLATFORM_CURSOR_STATE @@ -51,6 +51,11 @@ float x,y; +typedef struct android_gstate { + struct android_app* app; + struct android_poll_source* source; +} android_gstate; + typedef VkFlags VkAndroidSurfaceCreateFlagsKHR; typedef struct VkAndroidSurfaceCreateInfoKHR { diff --git a/src/android_window.c b/src/android_window.c index 708aeda01..0ebcdac76 100644 --- a/src/android_window.c +++ b/src/android_window.c @@ -41,6 +41,15 @@ static int32_t handle_input(struct android_app* app, AInputEvent* event) return 0; } +static void handleEvents(int timeout) { + ALooper_pollOnce(0, NULL, NULL,(void**)&_glfw.gstate.source); + + if (_glfw.gstate.source != NULL) { + _glfw.gstate.source->process(_glfw.gstate.app, _glfw.gstate.source); + } + _glfwInputCursorPos(_glfw.windowListHead, x, y); +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -50,10 +59,15 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { - window->android = _glfw.app; + // wait for window to become ready + while (_glfw.gstate.app->window == NULL) { + handleEvents(-1); + } + // hmmm maybe should be ANative_Window only? + window->android = _glfw.gstate.app; window->android->onInputEvent = handle_input; - ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); + //ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0); if (ctxconfig->client != GLFW_NO_API) { @@ -136,7 +150,10 @@ void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n, int d) void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) { - + // the underlying buffergeometry is currently being initialized from the + // window width and height...so high resolution displays are currently + // not supported...so it is safe to just call GetWindowSize() for now + _glfwPlatformGetWindowSize(window, width, height); } void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, @@ -212,16 +229,17 @@ int _glfwPlatformWindowVisible(_GLFWwindow* window) void _glfwPlatformPollEvents(void) { - _glfwInputCursorPos(_glfw.windowListHead, x, y); + handleEvents(0); } void _glfwPlatformWaitEvents(void) { - _glfwPlatformPollEvents(); + handleEvents(-1); } void _glfwPlatformWaitEventsTimeout(double timeout) { + handleEvents(timeout * 1e3); } void _glfwPlatformPostEmptyEvent(void)