brackeen / glfm

OpenGL ES and input for iOS, tvOS, Android, and WebGL
zlib License
566 stars 77 forks source link

Can glfm provide an OpenGL context for Android in purely native code? #2

Closed headupinclouds closed 7 years ago

headupinclouds commented 7 years ago

I'm interested primarily in supporting OpengL ES 2.0 unit tests (GTest, etc) from CMake without the need for Java wrappers. This would entail building NDK native apps and running in /data/local/tmp/

https://github.com/brackeen/glfm/blob/master/src/glfm_platform_android.c

brackeen commented 7 years ago

GLFM for Android doesn't require developers to write any Java code, although AFAIK you'll need to build GLFM Android apps with the gradle toolchain. Does that answer your question?

headupinclouds commented 7 years ago

I'm not sure 😄

I'll try to clarify.

The usual simple "hello world" console app:

#include <stdio.h>
int main(int arg, char **argv)
{
   printf ("HELLO WORLD\n");
   return 0;
}

can be compiled via the NDK and run on an Android device in /data/local/tmp using adb without the need for gradle or Java. (This is very convenient for OpenGL ES C++ unit testing directly from CMake, for example.) I was curious if the NDK provided enough hooks to create an OpenGL context for rendering entirely from native code. (I don't know enough about Android applications to know if this is possible or not.)

although AFAIK you'll need to build GLFM Android apps with the gradle toolchain

Are you aware of something in particular that requires Gradle?

I can try it out.

Thanks!

headupinclouds commented 7 years ago

Follow up: http://stackoverflow.com/a/39820460

brackeen commented 7 years ago

Are you aware of something in particular that requires Gradle?

Building the APK. Maybe it's possible to make an APK without gradle, but I do not know.

I think the question is: can it run as a plain executable and not an APK? I have no idea.

If you're trying to automate a test process via the command line (build, upload to device, and execute), yes, this is possible via gradle and the adb tools.

headupinclouds commented 7 years ago

Building the APK. Maybe it's possible to make an APK without gradle, but I do not know.

I'm able to build and launch your Android sample with the help of the CMake android_create_apk script in the Hunter package manager (originally from Pixellight):

https://github.com/headupinclouds/glfm/blob/hunter/example/CMakeLists.txt#L43-L71

If you have any interest in this, I can make this optional, add documentation, and submit a PR.

FWIW, I'm interested in using this to automate OpenGL ES 2.0 shader testing via GTest.

I think the question is: can it run as a plain executable and not an APK? I have no idea.

For the GTest use case, I believe the main gotcha will be differences between APK and standard int main() {} return codes. I will take a look and can share what I find.

I'll go ahead and close this. Thanks.

brackeen commented 7 years ago

Thanks for pointing out Hunter. I tried the branch you created, but could not create a build (errors involving the toolchain). Perhaps I can take a look later, but it's not a priority right now.

headupinclouds commented 7 years ago

Perhaps I can take a look later, but it's not a priority right now.

Sure, here is the toolchain I tested:

$ git branch -a | grep -e "*"
* hunter
$ git status
On branch hunter
Your branch is up-to-date with 'headupinclouds/hunter'.
nothing to commit, working tree clean
$ 2>/dev/null polly.py --toolchain android-ndk-r10e-api-19-armeabi-v7a-neon --fwd GLFM_BUILD_EXAMPLE=ON --verbose --clear | tail -10
Log saved: /Users/dhirvonen/devel/glfm/_logs/polly/android-ndk-r10e-api-19-armeabi-v7a-neon/log.txt
-
Generate: 0:00:05.344082s
Build: 0:00:01.735071s
-
Total: 0:00:07.079477s
-
SUCCESS

Install APK vis adb or use Makefile launch target:

(cd _builds/android-ndk-r10e-api-19-armeabi-v7a-neon/ && make launch-GlfmExample | tail -4)
Stopping: com.example.glfmexample
Starting: Intent { cmp=com.example.glfmexample/.LoadLibraries VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} }
[100%] Built target launch-GlfmExample
/usr/local/Cellar/cmake/HEAD-f1b9fe5/bin/cmake -E cmake_progress_start /Users/dhirvonen/devel/glfm/_builds/android-ndk-r10e-api-19-armeabi-v7a-neon/CMakeFiles 0

This launches the app with the rainbow triangle.

glfm is very convenient. I will probably add this package to hunter in the near future and can run through more android toolchains in CI tests.

FWIW, and in case anyone else stumbles onto this, apparently you can perform off screen rendering in Android with a standard int main(int argc, char **argv){} application. glfm was very helpful for sorting through the details. Android simply requires eglCreatePbufferSurface as mentioned in this post: https://mkonrad.net/2014/12/08/android-off-screen-rendering-using-egl-pixelbuffers.html

For iOS you can create your main() in an objective-c++ file (.mm) and create a context directly (the iOS CMake add_executable default .app will work):

        EAGLContext egl = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        [EAGLContext setCurrentContext:egl];
        // do your rendering
headupinclouds commented 7 years ago

FYI: https://github.com/ruslo/hunter/issues/808

Thanks!