fix EGL context loss on Android
This commit is contained in:
@@ -54,8 +54,13 @@ struct saved_state {
|
||||
float angle;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
EGLDisplay display;
|
||||
EGLContext context;
|
||||
};
|
||||
|
||||
EGLDisplay g_display = EGL_NO_DISPLAY;
|
||||
EGLContext g_context = EGL_NO_CONTEXT;
|
||||
|
||||
/**
|
||||
* Shared state for our app.
|
||||
*/
|
||||
@@ -251,9 +256,14 @@ static int engine_init_display(struct engine* engine) {
|
||||
EGLSurface surface;
|
||||
EGLContext context;
|
||||
|
||||
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
|
||||
eglInitialize(display, 0, 0);
|
||||
EGLDisplay display = g_display;
|
||||
if (g_display == EGL_NO_DISPLAY)
|
||||
{
|
||||
LOG("DYSPLAY CREATE");
|
||||
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
eglInitialize(display, 0, 0);
|
||||
}
|
||||
else LOG("DISPLAY RESUME");
|
||||
|
||||
/* Here, the application chooses the configuration it desires.
|
||||
* find the best match if possible, otherwise use the very first one
|
||||
@@ -295,7 +305,15 @@ static int engine_init_display(struct engine* engine) {
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
context = eglCreateContext(display, config, EGL_NO_CONTEXT, attribs_test);
|
||||
bool resuming_context = true;
|
||||
context = g_context;
|
||||
if (g_context == EGL_NO_CONTEXT)
|
||||
{
|
||||
LOG("CONTEXT CREATE");
|
||||
context = eglCreateContext(display, config, EGL_NO_CONTEXT, attribs_test);
|
||||
resuming_context = false;
|
||||
}
|
||||
else LOG("CONTEXT RESUME");
|
||||
|
||||
if (context == EGL_NO_CONTEXT)
|
||||
{
|
||||
@@ -363,6 +381,15 @@ static int engine_init_display(struct engine* engine) {
|
||||
engine->height = h;
|
||||
engine->state.angle = 0;
|
||||
|
||||
g_display = display;
|
||||
g_context = context;
|
||||
|
||||
if (resuming_context)
|
||||
{
|
||||
LOG("RESUME APP");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check openGL on the system
|
||||
auto opengl_info = { GL_VENDOR, GL_RENDERER, GL_VERSION/*, GL_EXTENSIONS*/ };
|
||||
for (auto name : opengl_info) {
|
||||
@@ -496,13 +523,13 @@ static void engine_draw_frame(struct engine* engine) {
|
||||
static void engine_term_display(struct engine* engine) {
|
||||
if (engine->display != EGL_NO_DISPLAY) {
|
||||
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
if (engine->context != EGL_NO_CONTEXT) {
|
||||
eglDestroyContext(engine->display, engine->context);
|
||||
}
|
||||
// if (engine->context != EGL_NO_CONTEXT) {
|
||||
// eglDestroyContext(engine->display, engine->context);
|
||||
// }
|
||||
if (engine->surface != EGL_NO_SURFACE) {
|
||||
eglDestroySurface(engine->display, engine->surface);
|
||||
}
|
||||
eglTerminate(engine->display);
|
||||
// eglTerminate(engine->display);
|
||||
}
|
||||
engine->animating = 0;
|
||||
engine->display = EGL_NO_DISPLAY;
|
||||
@@ -700,11 +727,16 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
|
||||
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
|
||||
struct engine* engine = (struct engine*)app->userData;
|
||||
switch (cmd) {
|
||||
case APP_CMD_RESUME:
|
||||
LOG("APP_CMD_RESUME");
|
||||
App::I.redraw = true;
|
||||
break;
|
||||
case APP_CMD_SAVE_STATE:
|
||||
// The system has asked us to save our current state. Do so.
|
||||
engine->app->savedState = malloc(sizeof(struct saved_state));
|
||||
*((struct saved_state*)engine->app->savedState) = engine->state;
|
||||
engine->app->savedStateSize = sizeof(struct saved_state);
|
||||
LOG("SAVE STATE");
|
||||
break;
|
||||
case APP_CMD_INIT_WINDOW:
|
||||
// The window is being shown, get it ready.
|
||||
@@ -715,7 +747,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
|
||||
break;
|
||||
case APP_CMD_TERM_WINDOW:
|
||||
// The window is being hidden or closed, clean it up.
|
||||
App::I.terminate();
|
||||
//App::I.terminate();
|
||||
engine_term_display(engine);
|
||||
//exit(0);
|
||||
break;
|
||||
@@ -772,12 +804,14 @@ void android_main(struct android_app* state) {
|
||||
state->looper, LOOPER_ID_USER,
|
||||
NULL, NULL);
|
||||
|
||||
LOG("START MAIN");
|
||||
if (state->savedState != NULL) {
|
||||
// We are starting with a previous saved state; restore from it.
|
||||
engine.state = *(struct saved_state*)state->savedState;
|
||||
}
|
||||
|
||||
//App::I.create();
|
||||
App::I.redraw = true;
|
||||
|
||||
// loop waiting for stuff to do.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user