From a49f7b94df0082ae273829a2eae8371a2ed554ab Mon Sep 17 00:00:00 2001 From: Curi0 Date: Sat, 16 Sep 2017 15:06:48 +0530 Subject: [PATCH] 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