Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android Support #1087

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open

Add Android Support #1087

wants to merge 27 commits into from

Conversation

xCuri0
Copy link

@xCuri0 xCuri0 commented Oct 3, 2017

I added Android support to GLFW. Some features may not be implemented but the basic context creation works.You can test it by importing the native-activity sample in Android Studio, adding GLFW to CMakeLists.txt and changing main.c to be like any normal GLFW program. main() works since the actual entry point is located in android_init.c > android_main(). OpenGL contexts use egl_context.c which required little modification to work.
screenshot_2017-10-03-13-34-16-834_com example native_activity
Vulkan window surface creation uses the VK_KHR_android_surface extension.
screenshot_2017-09-27-21-24-09-357_com example native_activity

I tested it on a Xiaomi Redmi Note 4X, please see if it works on other devices if possible.

@elmindreda elmindreda added the enhancement Feature suggestions and PRs label Oct 3, 2017
@xCuri0
Copy link
Author

xCuri0 commented Oct 4, 2017

@elmindreda Status on this ?

@elmindreda
Copy link
Member

@xCuri0 I will be back on Sunday and will start looking through this properly then.

@elmindreda
Copy link
Member

Please hold off on any further updates in the meantime.

Copy link
Member

@elmindreda elmindreda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a good foundation for an Android port! Thank you!

This first review is only about necessary minor cleanup and doesn't really address the design.

@@ -197,7 +197,7 @@ extern "C" {
#include <OpenGL/glu.h>
#endif

#else /*__APPLE__*/
#elif !defined(ANDROID) /*__APPLE__*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the default header behavior. I think the application should define one of the GLFW_INCLUDE_ES* macros if that's what the program uses, just like on desktop.

@@ -5207,5 +5207,4 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window
}
#endif

#endif /* _glfw3_h_ */

#endif /* _glfw3_h_ */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't remove EOF newline.

@@ -564,6 +564,11 @@ GLFWAPI int glfwGetOSMesaDepthBuffer(GLFWwindow* window, int* width, int* height
GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow* window);
#endif

#if defined(GLFW_EXPOSE_NATIVE_ANDROID)
#include <android_native_app_glue.h>
Copy link
Member

@elmindreda elmindreda Oct 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Platform headers should be included further up in this file.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paths that include substitutions should be quoted. Compare with other examples in this file.

@@ -95,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/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't hardcode an NDK path for all platforms; instead add it only when building for Android.


void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
{
*height = ANativeWindow_getHeight(window->android->window);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either or both of width and height may be NULL.

@@ -564,6 +564,11 @@ GLFWAPI int glfwGetOSMesaDepthBuffer(GLFWwindow* window, int* width, int* height
GLFWAPI OSMesaContext glfwGetOSMesaContext(GLFWwindow* window);
#endif

#if defined(GLFW_EXPOSE_NATIVE_ANDROID)
#include <android_native_app_glue.h>
GLFWAPI struct android_app * glfwGetAndroidApp(GLFWwindow* window);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a need for a window parameter here as the app struct is available in a global.


const char* _glfwPlatformGetVersionString(void)
{
return _GLFW_VERSION_NUMBER " Android";
Copy link
Member

@elmindreda elmindreda Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version string should list EGL for Android. See the version strings of other platforms.


ANativeWindow_setBuffersGeometry(window->android->window, wndconfig->width, wndconfig->height, 0);

if (ctxconfig->client != GLFW_NO_API) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GLFW uses Allman style.


void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
{
*xpos = x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either or both of xpos and ypos may be NULL.

@xCuri0
Copy link
Author

xCuri0 commented Oct 17, 2017

@elmindreda I've pushed the changes to my fork but for some reason it doesn't show up in the pull request.

@elmindreda
Copy link
Member

@xCuri0 Must have been a glitch. Looks like it shows up correctly now.

@elmindreda
Copy link
Member

Thanks, those are good fixes!

@xCuri0
Copy link
Author

xCuri0 commented Oct 17, 2017

@elmindreda Yep it shows for me now too. Also what do you mean by include substitutions should be quoted ?

@elmindreda
Copy link
Member

@xCuri0 Thank you! Will try it out asap.

@kingofoz
Copy link

hi @xCuri0 did you fix the segmentation fault error of the cursor state bug?
hi @elmindreda is the android support merged?

@Towerthousand
Copy link

Hey, tried it out on a Nexus 6P, 5X and a Pixel 2 XL. Really want to get this merged. How can I help this PR move forward? @elmindreda

@elmindreda
Copy link
Member

@kingofoz Not yet. Currently I'm working on finishing version 3.3.

@Towerthousand Thanks for asking! You can join forces with @xCuri0 to stabilize and flesh out this port or have a look at #1098 for things to do on the 3.3 release.

@Jeffset
Copy link

Jeffset commented Dec 10, 2017

@xCuri0 @elmindreda Took a look at this port and played with improving it a bit. And I think it's impressing and promising. But I have a bunch of questions. I can't find a trivial way to attach (f. ex) multitouch events, mobile sensors to existing API. As GLFW not yet designed as library for mobile platforms, does this port going to extend API or rework (generalize somehow) existing one? And what of compatibility? Or maybe only limited set of mobile features'll be available? I'd like to know what do you think of this problems :)
P. S.
I'm chiefly interested in this port and ready to contribute.

@elmindreda
Copy link
Member

@Jeffset As for touch input, have a look at the touch branch. None yet for sensors. It's quite likely that there will be an official mobile subset of the API if support for Android and iOS pans out.

@xCuri0
Copy link
Author

xCuri0 commented Dec 12, 2017

@kingofoz I haven't used a cursor state since it causes segmentation fault.

@xCuri0
Copy link
Author

xCuri0 commented Dec 26, 2017

@elmindreda Desktop platforms like Windows and Linux also have APIs for sensors.My laptop has an accelerometer used for HDD protection which can be accessed in Linux.

@rafal-tarnow
Copy link

good job!, I have tested on Galaxy A5 (2016) Android 6.0.1 and it works

@WardBenjamin
Copy link

Looks awesome! What's the current status on this?

@mumin16
Copy link

mumin16 commented Aug 27, 2018

it cant run the release version

@mumin16
Copy link

mumin16 commented Aug 27, 2018

there is a problem.
untitled

@xCuri0
Copy link
Author

xCuri0 commented Aug 28, 2018

@mumin16 Can you tell the Android version of the emulator ? I only tested Nougat MIUI and Oreo 8.1 AOSP. Some people got it working on TouchWiz Marshmallow too

@evilbinary
Copy link

@mumin16 what is your gui?

@mumin16
Copy link

mumin16 commented Aug 28, 2018

i try it:
nox emulator-> 4.4.2
memu emulator->5.1.1

gui is nuklear

@evilbinary
Copy link

@mumin16 what's your project? Is it open source project?

@mumin16
Copy link

mumin16 commented Aug 28, 2018

if you press the home, application crashes and we get error : A/libc: Fatal signal 11 (SIGSEGV) at 0x00000017 (code=1). we had been trying to call an uninitialised Canvas inside another Class so when it was trying to get the height or width of it, it would crash. if you rotate mobile device, app locks sometimes.

@xCuri0
Copy link
Author

xCuri0 commented Aug 29, 2018

@mumin16 both of those are known bugs. I can't implement state saving unless the app using the library stores all its variables in a struct which the library makes

@Darky-Lucera
Copy link

Excuse me, are there any news ?

@tim3385
Copy link

tim3385 commented Jun 29, 2019

Thanks for your work.
Do you base on touch branch?

@kapsyst
Copy link

kapsyst commented Jul 30, 2019

Is there any movement on this? Would be nice to support all platforms via GLFW...

@avaxar
Copy link

avaxar commented Apr 8, 2023

It's been almost 6 years. Would love to see the continuation of progress on this PR.

@LDprg
Copy link

LDprg commented Mar 18, 2024

I think this should actually be revived. Since sdl is the only real alternative supporting android. GLFM for example does not support vulkan so is no real option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature suggestions and PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet