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 2308 - opengles2 renderer creation fails with "Could not create EGL context" on Android
Summary: opengles2 renderer creation fails with "Could not create EGL context" on Android
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: HG 2.1
Hardware: ARM Android (All)
: P2 normal
Assignee: Gabriel Jacobo
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-12-15 22:18 UTC by carniblood
Modified: 2014-02-25 20:43 UTC (History)
1 user (show)

See Also:


Attachments
attempt to fix opengles renderer (for reference) (3.20 KB, patch)
2013-12-18 21:41 UTC, carniblood
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description carniblood 2013-12-15 22:18:33 UTC
When I call SDL_CreateRenderer() on android, opengles (not opengles2) renderer is created. If I force opengles2 driver with SDL_SetHint(), SDL_CreateRenderer() fails with "Could not create EGL context" error.

I saw this problem first on 2.0.1 (it may have started earlier).
It seems there was a similar bug on 2.0.1 (https://bugzilla.libsdl.org/show_bug.cgi?id=2204) but my bug still occurs on revision 8048.

By searching into SDL code, I figured out a couple of issues:

1) gl_config.major_version is wrong in SDL_EGL_ChooseConfig() [sdl_egl.c] because "SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2)" is set too late (only during GLES2_CreateRenderer() [sdl_render_gles2.c], which is after window creation and SDL_EGL_ChooseConfig, except if SDL_RecreateWindow is called).

I temporarly fixed my code by manually calling "SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2)" before window creation, but I guess a better fix is needed in SDL code.

for reference, here is my current code for creating window and renderer:

SDL_SetHint("SDL_HINT_RENDER_DRIVER", "opengles2");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
m_pWindow = SDL_CreateWindow(windowName,
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
		0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);


2) When SDL_GL_CONTEXT_MAJOR_VERSION is set too late, default gl_config.major_version is defined from sdl_video.c with major_version = 2 when SDL_VIDEO_OPENGL_ES2 is defined or 1 if only SDL_VIDEO_OPENGL_ES is defined. The default value on android is 1 because SDL_VIDEO_OPENGL_ES2 is not defined. SDL_VIDEO_OPENGL_ES2 should not be defined in sdl_config_android.h? At least with SDL_VIDEO_OPENGL_ES2 defined, opengles2 renderer creation is working and only forcing opengles driver fails.
Comment 1 Gabriel Jacobo 2013-12-16 13:11:13 UTC
I added the missing define here: http://hg.libsdl.org/SDL/rev/ac809ea54d63

Regarding the first point, I'm afraid the only fix I can offer is maybe an update in the documentation stating this requirement more clearly. To create the EGL surface attached to the window we have to know the type and version of context that will be used, that means that SDL_GL_CONTEXT_MAJOR_VERSION, SDL_GL_CONTEXT_MINOR_VERSION and SDL_GL_CONTEXT_PROFILE_MASK have to be set before the window is created.

How does that sound?
Comment 2 carniblood 2013-12-18 21:41:06 UTC
Created attachment 1500 [details]
attempt to fix opengles renderer (for reference)
Comment 3 carniblood 2013-12-18 21:41:25 UTC
Thanks. So that's means what I thought was a workaround was actually the right way to force opengles version. That's good.

But I think, if I'm not mistaken, there is still one case where there can be a problem: if opengles2 renderer creation fails for any reason, SDL will be unable to create opengles renderer as a second choice.

What about checking for current SDL_GL_CONTEXT_MAJOR_VERSION, SDL_GL_CONTEXT_MINOR_VERSION and SDL_GL_CONTEXT_PROFILE_MASK in GLES2_CreateRenderer() and GLES_CreateRenderer() and recreate the window if it doesn't match (in a similar way with the check for SDL_WINDOW_OPENGL flag)?
I put a patch in attachment for reference.

I tried it and it seems to work, but maybe it causes some problems if the user actually set manually one of these values? I'm not sure about it.
I also tried on Windows platform, and with this patch the window is always recreated on renderer creation, since the default GL values are set for opengl renderer, not opengles. But this means Windows implementation differs with Android one, and doesn't need the right type and version before renderer creation?

Anyway if it cause too many undesirable side effects, it would be still possible to fix it on user side, if I manually recreate the windows with a different context version to try to create another renderer, but it would be nice if it can be handled on sdl side.
Comment 4 Gabriel Jacobo 2014-02-25 20:43:18 UTC
Fixed: https://hg.libsdl.org/SDL/rev/a1563cbde7a5