Search code examples
androidandroid-ndksdl-2egl

libEGL error when running SDL2 (ndk) Android app with orientation locked to landscape


I'm playing around with SDL2 on Android and I have a problem when setting the orientation to stay in landscape.

I have the following lines in my AndroidManifest.xml:

android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"

Without the orientation|screenSize and landscape line, everything is fine. When these lines are added and I tap the back button (to close that app), I get the following log line printed over and over:

E libEGL  : eglSwapBuffersWithDamageKHR:1089 error 300d (EGL_BAD_SURFACE)

I am only using SDL functions for rendering with no OpenGL code.

Any ideas why setting the orientation in the manifest would cause this? Is this not the correct way to lock orientation?

Update:

Setting the orientation in the onCreate method is good workaround and seems to do the job. Here's the line of code for that:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

It would still be good to know why setting the orientation in the manifest breaks everything. Setting it to portrait is fine, it's just setting it to landscape that gives the issue.

Update 2:

Setting the orientation in the onCreate method is mostly fine. Closing the app doesn't cause problems, switching to the home screen is fine and so is switching to another application. I found the app would open back up again with no problems. I did find that the app would crash when the screen turns off. Again, this issue only occurs when the app is locked in landscape mode, in portrait it works fine. In the logging I noticed that the onDestroy method is being called when the power button is press in landscape mode, but it's not being called in portrait. When turning the screen on again to resume, the onCreate method isn't even called. It just stops.

I may end up locking the screen in portrait and just rendering everything sideways.


Solution

  • I found the answer.

    The errors I was getting when closing the app no longer occur in the latest development version of the SDL2 (as suggested by Treble on the SDL forums). That can be found here.

    The problem with the app crashing when resuming from the screen being off was due to the onDestroy method being called. When the screen goes off, Android calls the activity's onDestroy method, the orientation is set to portrait, the activity is re-initialised, and then the screen goes off.

    The fact that this crashes my app probably means that I'm not destroying everything correctly (which I will be looking in to).

    I found this post which details a good way to get around this issue. All you need to do is make sure you have these lines in your manifest:

    android:screenOrientation="landscape"
    android:configChanges="keyboardHidden|orientation|screenSize"
    

    ... and this in the activity:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
    }
    

    This means that change in configuration (like orientation) is not handled by destroying and recreating the activity.

    Adding this, along with updating to the latest dev build of SDL2 seems to have fixed my problems.