We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4681

Summary: SDL_GL_CreateContext crashes on android when SDL_GL_MULTISAMPLESAMPLES is used
Product: SDL Reporter: someuniquename
Component: *don't know*Assignee: Sylvain <sylvain.becker>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: blocker    
Priority: P2 CC: someuniquename
Version: 2.0.10Keywords: target-2.0.10
Hardware: ARM   
OS: Android (All)   
Attachments: test.c

Description someuniquename 2019-06-21 12:58:21 UTC
Sdl2 version: hg tip 89b3e1e9839c

I am testing kivy app built with python-for-android.

If multisamples is enabled, the app dies with an error: 
SDL_GL_CreateContext(self.win): Unable to make EGL context current (call to eglMakeCurrent failed, reporting an error of EGL_BAD_SURFACE)

The relevant kivy sdl2 window init code does a lot of different things: https://github.com/kivy/kivy/blob/master/kivy/core/window/_window_sdl2.pyx#L76
 
But i believe that minimal code to reproduce is this:
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2)
self.win = SDL_CreateWindow(NULL, x, y, width, height, self.win_flags)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0)
self.ctx = SDL_GL_CreateContext(self.win)

The bug is gone when multisamples are disabled in kivy (the first two lines in the code above)
Comment 1 someuniquename 2019-06-21 16:58:25 UTC
Ah, issue with EGL_BAD_SURFACE is more complicated. For more complex app, even without multisampling the chance of successful start is about 30%
Comment 2 someuniquename 2019-06-21 18:25:44 UTC
this is the adb log of when the app crashes: https://gist.github.com/Fak3/429328b50c9eb11b8c896ad73e236a1a
Comment 3 Sam Lantinga 2019-06-21 20:58:49 UTC
Sylvain, can you look at this ASAP for SDL 2.0.10 release?

Thanks!
Comment 4 Sam Lantinga 2019-06-21 20:59:19 UTC
Whoops, didn't mean to mark this fixed.
Comment 5 Sylvain 2019-06-22 06:29:43 UTC
I tried those flags, and I could always start my app and/or create a GL context.
I tried several times to makes sure about the 30% ratio.


This is not clear, if the issue is with the attributes MULTISAMPLE, it should always happen, right? 

But if the issue is with the MakeCurrent failing, so it shouldn't be related to your attributes, and fail with any context ?! And you should check when current is made.

Maybe, you should try with simple C app to reduce the scope of the issue.
Comment 6 someuniquename 2019-06-22 12:17:17 UTC
Maybe Multisample just makes the bug to appear a bit more reliably, but it is not crucial for the app to crash.

Is there any tutorial of how to build an app for android?

I bisected and tested my app against repo history. 

The last known changeset that works fine is this:

Android: make sure surfaceChanged try to enter into 'resumed' state.
Wed, 02 Jan 2019 17:41:33 +0100 (5 months ago)
changeset 12492	fe136f38ab71

Then changesets 12493-12511 crash app because of a different issue, related to concurrency (i think), and throw segfaults.

And then the first changeset that fixes concurrency but experince this topic issue with EGL_BAD_SURFACE is this:

Android: concurrency issue for Android_SetWindowFullscreen()
Wed, 09 Jan 2019 15:18:41 +0100 (5 months ago)
changeset 12512	 afc8e5d1d992

So the bug was introduced between 02 Jan and 09 Jan, somewhere in changesets 12493-12512
Comment 7 Sylvain 2019-06-22 12:28:50 UTC
So this commit introduce the issue ? 
https://hg.libsdl.org/SDL/rev/afc8e5d1d992
It only adds some synchronization to make the function call be atomic.
So this is strange :/


Can you try to use RGB 565 instead of 888 in you KIVY code ?!



At the same time, here's some docs for android:
https://hg.libsdl.org/SDL/file/afc8e5d1d992/docs/README-android.md
there is also a script:
https://hg.libsdl.org/SDL/file/afc8e5d1d992/build-scripts/androidbuild.sh
not sure how much it can help

but in short ...
You need to have android ndk, you can build with ndk-build
you need also to setup some gradle build file also.
Comment 8 someuniquename 2019-06-22 12:41:59 UTC
> So this commit introduce the issue ? 
> https://hg.libsdl.org/SDL/rev/afc8e5d1d992

I don't know if it was this commit, because ~20 commits before this one, from 12493 to 12511 mask the EGL_BAD_SURFACE error with another error.



> Can you try to use RGB 565 instead of 888 in you KIVY code ?!

Kivy does this right now:
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16)
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0)
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1)

How should i make it use RGB 565 properly? Just change three lines?
Comment 9 Sylvain 2019-06-22 12:47:15 UTC
remove 
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8)

add

        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 6)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5)
Comment 10 Sylvain 2019-06-22 12:51:35 UTC
Also if you could bisect a little bit more to find the commit with issue
Comment 11 Sylvain 2019-06-22 12:54:52 UTC
also, there is a mistake:
not 

        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 6)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5)

but

        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 6)
(RGB == 565)
Comment 12 Sylvain 2019-06-22 12:55:17 UTC
sorry:
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5)
Comment 13 someuniquename 2019-06-22 13:46:45 UTC
Ok, rgb 565 works fine and doesn't crash.
Comment 14 Sylvain 2019-06-22 15:07:36 UTC
Ok, another try:

Can you set-back ARGB8888
And comment out this line in the java file:

https://hg.libsdl.org/SDL/file/5cbf6472a916/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java#l1138
Comment 15 someuniquename 2019-06-22 15:55:16 UTC
commented this line, and app works fine. But SDL reports that format is 565:

V/SDL     (26215): pixel format RGB_565

should it be like that?

btw, i am testing commit afc8e5d1d992 (first known failing one), not the tip
Comment 16 Sylvain 2019-06-22 19:35:08 UTC
You can also try with latest, but I believe it would be the same.

Can you provide me an APK which fails so that I can try ?

Can you tell which phone you use ? which android version also ?
Comment 17 someuniquename 2019-06-22 20:10:24 UTC
failing apk: https://drive.google.com/file/d/1G7qIJfRdAy87d2Qe_XX3oBA4HX__c6dk/view?usp=sharing

the device is zte nubia z11 mini 
Amdroid 5.1.1

V/SDL     (25270): Device: NX529J
V/SDL     (25270): Model: NX529J
V/SDL     (25270): Window size: 1080x1920
V/SDL     (25270): Device size: 1080x1920
I/python  (25270): [INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
I/python  (25270): [INFO   ] [GL          ] Backend used <sdl2>
I/python  (25270): [INFO   ] [GL          ] OpenGL version <b'OpenGL ES 3.1 V@124.0 (GIT@I041b18199e)'>
I/python  (25270): [INFO   ] [GL          ] OpenGL vendor <b'Qualcomm'>
I/python  (25270): [INFO   ] [GL          ] OpenGL renderer <b'Adreno (TM) 405'>
I/python  (25270): [INFO   ] [GL          ] OpenGL parsed version: 3, 1
I/python  (25270): [INFO   ] [GL          ] Texture max size <16384>
I/python  (25270): [INFO   ] [GL          ] Texture max units <16>
Comment 18 Sylvain 2019-06-23 06:58:42 UTC
I tried but this is not clear to me to see any issue. It doesn't crash.
I dont get the same log you published with "Unable to get a Window, abort."

(I may need to test on an older device)


I notice it gets stuck in "Loading" and I have to go to background/foreground so that it continues. Then it display "L hi 1 "



Did you try to run a sample SDL2 C app ?
Comment 19 someuniquename 2019-06-23 07:09:52 UTC
Where can I get the sample c app?
Comment 20 Sylvain 2019-06-23 13:23:56 UTC
Created attachment 3842 [details]
test.c

Here's a minimal in order to create a test-case.

Modify it and try to get it crashing with the multisample enable.
Comment 21 Sylvain 2019-06-23 14:15:11 UTC
I have a device where your .apk fails with:
[CRITICAL] [App         ] Unable to get a Window, abort.
(Samsung S5 Android 6.0.1)

but, on this same device, If run another app, armeabi-v7a, modified with:

        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16)
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8)
        SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0)
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1)
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2)
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2)
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0)

this is working.
Comment 22 someuniquename 2019-06-23 15:09:26 UTC
> I have a device where your .apk fails with: [CRITICAL] [App ] Unable to get a Window, abort. (Samsung S5 Android 6.0.1)

Look about 20 lines above the error - does it say this:

I/python  ( 9712): [CRITICAL] [Window      ] Unable to find any valuable Window provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes
I/python  ( 9712): sdl2 - RuntimeError: b'Unable to make EGL context current (call to eglMakeCurrent failed, reporting an error of EGL_BAD_SURFACE)'
I/python  ( 9712):   File "/home/u1/.local/share/python-for-android/build/python-installs/paradox/kivy/core/__init__.py", line 70, in core_select_lib
I/python  ( 9712):   File "/home/u1/.local/share/python-for-android/build/python-installs/paradox/kivy/core/window/window_sdl2.py", line 152, in __init__
I/python  ( 9712):   File "/home/u1/.local/share/python-for-android/build/python-installs/paradox/kivy/core/window/__init__.py", line 969, in __init__
I/python  ( 9712):   File "/home/u1/.local/share/python-for-android/build/python-installs/paradox/kivy/core/window/window_sdl2.py", line 289, in create_window
I/python  ( 9712):   File "kivy/core/window/_window_sdl2.pyx", line 233, in kivy.core.window._window_sdl2._WindowSDL2Storage.setup_window
I/python  ( 9712):   File "kivy/core/window/_window_sdl2.pyx", line 75, in kivy.core.window._window_sdl2._WindowSDL2Storage.die

?
Comment 23 Sylvain 2019-06-23 20:18:52 UTC
Yes, same log
Comment 24 someuniquename 2019-06-26 04:42:40 UTC
Putting Android activity lock around all the sdl calls makes this bug go away
Comment 25 Sylvain 2019-06-28 14:42:36 UTC
commit https://hg.libsdl.org/SDL/rev/dd9169424181
details in bug #4681
Comment 26 someuniquename 2019-07-01 05:02:35 UTC
This bug is resolved with https://hg.libsdl.org/SDL/rev/dd9169424181
Closing.