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 1425 - onResume() causes old window handles to get clobbered
Summary: onResume() causes old window handles to get clobbered
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: HG 2.0
Hardware: ARM Android (All)
: P2 major
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-19 09:58 UTC by Stevie Frederick
Modified: 2012-06-19 10:13 UTC (History)
2 users (show)

See Also:


Attachments
A few fixes required for proper pause/resume support on Android (1.87 KB, patch)
2012-05-28 15:30 UTC, Gabriel Jacobo
Details | Diff
Recreate the GL context automatically (3.96 KB, patch)
2012-06-01 15:12 UTC, Gabriel Jacobo
Details | Diff
Recreate the GL context automatically (5.91 KB, patch)
2012-06-18 18:10 UTC, Gabriel Jacobo
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stevie Frederick 2012-02-19 09:58:05 UTC
After initializing a window context and blitting imagery to the screen using said window with the combination of: GetWindowSurface() and UpdateWindowSurface(), the image is correctly displayed to the screen/display device.

However, when the Android application loses focus, i.e. onPause() is fired, these windows lose their context.

A call to UpdateWindowSurface with the previously initialized surface will bring back an error from Android's Surface module:

Surface: using an invalid surface id=1, identity=%d, should be identity=%d

Destroying the window with SDL_DestroyWindow and recreating with SDL_CreateWindow freezes the application.

Application will freeze during recreation of window: 

W/SharedBufferStack(   97): waitForCondition(ReallocateCondition) timed out (identity=1030, status=0). CPU may be pegged. trying again.

Actively searching for work-arounds.
Comment 1 Gabriel Jacobo 2012-02-27 08:23:03 UTC
Hi, I think what you mention here is what I talked about in this post: http://forums.libsdl.org/viewtopic.php?t=7926&highlight=

In short, previously SDL under Android right out quit when it lost focus, now it remains active but as you loose the GL context, you have to re create your textures. This is an Android only issue, as it doesn't seem to happen in any of the other supported architectures (even iOS). I suggested in my post some potentials fixes/workarounds but I haven't got around to implementing any of these.
Comment 2 Gabriel Jacobo 2012-05-28 15:30:44 UTC
Created attachment 862 [details]
A few fixes required for proper pause/resume support on Android

This is a patch for SDLActivity.java that allows for pause/resume of activities and it should work as is under Android > 3.0.

To complement this patch, on you app you should listen for SDL_WINDOWEVENT_RESTORED (only under Android), and call SDL_GL_CreateContext(window) to restore the GL Context, that's the only thing you need to do.

The only "to do" for this would be sending a message where the Log line:

Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");

is, to let the app know that it has to reload its textures...or we could get fancy and try to do it automagically (saving all textures to memory on pause and then restoring them on resume?)
Comment 3 Gabriel Jacobo 2012-06-01 15:12:34 UTC
Created attachment 864 [details]
Recreate the GL context automatically

Attaching my best attempt at the complimentary part of the first patch, the one that automatically re-creates the GL context on the appropriate thread.
There's no simple solution to this problem...You can't add this in GLES_WindowEvent / GLES2_WindowEvent because they are called through SDL_SetEventFilter, so they are called in the Java thread vs the SDL thread.
In the interest of speed, I've added a global variable (yuck!) that tracks the hidden/paused state of the Android window, and we do the peeking of events only in the case that the window has been hidden.

There's two To-Dos in the patch:

* TODO: Should we double check if we are on the same thread that originally created the GL context? If for some reason the user is pumping events in a separate thread other than the main SDL thread, this can cause problems.

* TODO: If the context can't be recovered, send the user a message to let him know that the GL context has changed behind the scenes
Comment 4 Gabriel Jacobo 2012-06-18 18:10:48 UTC
Created attachment 880 [details]
Recreate the GL context automatically

Uploading improved patch, this one uses two semaphores and no messages, and offers the possibility of blocking the event loop while on pause with #if SDL_ANDROID_BLOCK_ON_PAUSE
I'm using this patch now in my system and it works like a charm.
Comment 5 Gabriel Jacobo 2012-06-19 10:13:15 UTC
Fixed in http://hg.libsdl.org/SDL/rev/0fa55ca2efdd