Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1293 - [Android] Support Pause/Resume
Browse files Browse the repository at this point in the history
Gabriel Jacobo 2011-12-23 12:55:11 PST

The attached files provide some improvement over the current handling of
pause/resume in Android.
- I disabled the exit(status) instruction in SDL_main as that makes the entire
app instead of the SDL thread exit (while not needed for pause/resume it is
needed for Live Wallpapers, an SDLActivity for which I'll upload in a separate
bug).
- Added nativePause and nativeResume which basically just mark the window as
visible/hidden, something that the end user needs to take into consideration
(ideally pausing the event loop).

Also, this arrangement creates a new GL context when needed, which at least in
my test system is every time you go away from the app and come back to it. So,
this means that the textures need to be generated again after resuming (a
problem the end user didn't have before because the app exited completely when
it should've been pausing). I'd like to know if there's a standard way of
letting the user know that the GL context has changed and that he needs to
refresh his textures, or if this is out of the scope of the library and each
user handles it in their own way (I don't know how/if this same thing is
handled in  the iPhone backend, but it would be wise to try to imitate that).

Gabriel Jacobo 2011-12-23 12:57:10 PST
Also, in the SDLActivity the EGL handling code is moved up to the Activity from
the Surface code, as I think it is possible (in theory) that the surface is
destroyed temporarily while the context remains alive (though in practice in my
test system this is not the case)
  • Loading branch information
slouken committed Jan 8, 2012
1 parent 81102e2 commit 542e375
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 130 deletions.
305 changes: 177 additions & 128 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -30,10 +30,20 @@ public class SDLActivity extends Activity {
private static SDLActivity mSingleton;
private static SDLSurface mSurface;

// This is what SDL runs in. It invokes SDL_main(), eventually
private static Thread mSDLThread;

// Audio
private static Thread mAudioThread;
private static AudioTrack mAudioTrack;

// EGL private objects
private static EGLContext mEGLContext;
private static EGLSurface mEGLSurface;
private static EGLDisplay mEGLDisplay;
private static EGLConfig mEGLConfig;
private static int mGLMajor, mGLMinor;

// Load the .so
static {
System.loadLibrary("SDL");
Expand All @@ -55,18 +65,38 @@ protected void onCreate(Bundle savedInstanceState) {
mSurface = new SDLSurface(getApplication());
setContentView(mSurface);
SurfaceHolder holder = mSurface.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}

// Events
protected void onPause() {
//Log.v("SDL", "onPause()");
Log.v("SDL", "onPause()");
super.onPause();
SDLActivity.nativePause();
}

protected void onResume() {
//Log.v("SDL", "onResume()");
Log.v("SDL", "onResume()");
super.onResume();
SDLActivity.nativeResume();
}

protected void onDestroy() {
super.onDestroy();
Log.v("SDL", "onDestroy()");
// Send a quit message to the application
SDLActivity.nativeQuit();

// Now wait for the SDL thread to quit
if (mSDLThread != null) {
try {
mSDLThread.join();
} catch(Exception e) {
Log.v("SDL", "Problem stopping thread: " + e);
}
mSDLThread = null;

//Log.v("SDL", "Finished waiting for SDL thread");
}
}

// Messages from the SDLMain thread
Expand All @@ -92,6 +122,8 @@ void sendCommand(int command, Object data) {
// C functions we call
public static native void nativeInit();
public static native void nativeQuit();
public static native void nativePause();
public static native void nativeResume();
public static native void onNativeResize(int x, int y, int format);
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
Expand All @@ -105,11 +137,11 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId,
// Java functions called from C

public static boolean createGLContext(int majorVersion, int minorVersion) {
return mSurface.initEGL(majorVersion, minorVersion);
return initEGL(majorVersion, minorVersion);
}

public static void flipBuffers() {
mSurface.flipEGL();
flipEGL();
}

public static void setActivityTitle(String title) {
Expand All @@ -121,6 +153,138 @@ public static Context getContext() {
return mSingleton;
}

public static void startApp() {
// Start up the C app thread
if (mSDLThread == null) {
mSDLThread = new Thread(new SDLMain(), "SDLThread");
mSDLThread.start();
}
else {
SDLActivity.nativeResume();
}
}

// EGL functions
public static boolean initEGL(int majorVersion, int minorVersion) {
if (SDLActivity.mEGLDisplay == null) {
//Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);

try {
EGL10 egl = (EGL10)EGLContext.getEGL();

EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

int[] version = new int[2];
egl.eglInitialize(dpy, version);

int EGL_OPENGL_ES_BIT = 1;
int EGL_OPENGL_ES2_BIT = 4;
int renderableType = 0;
if (majorVersion == 2) {
renderableType = EGL_OPENGL_ES2_BIT;
} else if (majorVersion == 1) {
renderableType = EGL_OPENGL_ES_BIT;
}
int[] configSpec = {
//EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_RENDERABLE_TYPE, renderableType,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
Log.e("SDL", "No EGL config available");
return false;
}
EGLConfig config = configs[0];

/*int EGL_CONTEXT_CLIENT_VERSION=0x3098;
int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, majorVersion, EGL10.EGL_NONE };
EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, contextAttrs);
if (ctx == EGL10.EGL_NO_CONTEXT) {
Log.e("SDL", "Couldn't create context");
return false;
}
SDLActivity.mEGLContext = ctx;*/
SDLActivity.mEGLDisplay = dpy;
SDLActivity.mEGLConfig = config;
SDLActivity.mGLMajor = majorVersion;
SDLActivity.mGLMinor = minorVersion;

SDLActivity.createEGLSurface();
} catch(Exception e) {
Log.v("SDL", e + "");
for (StackTraceElement s : e.getStackTrace()) {
Log.v("SDL", s.toString());
}
}
}
else SDLActivity.createEGLSurface();

return true;
}

public static boolean createEGLContext() {
EGL10 egl = (EGL10)EGLContext.getEGL();
int EGL_CONTEXT_CLIENT_VERSION=0x3098;
int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, SDLActivity.mGLMajor, EGL10.EGL_NONE };
SDLActivity.mEGLContext = egl.eglCreateContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, EGL10.EGL_NO_CONTEXT, contextAttrs);
if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
Log.e("SDL", "Couldn't create context");
return false;
}
return true;
}

public static boolean createEGLSurface() {
if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
EGL10 egl = (EGL10)EGLContext.getEGL();
if (SDLActivity.mEGLContext == null) createEGLContext();

Log.v("SDL", "Creating new EGL Surface");
EGLSurface surface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
if (surface == EGL10.EGL_NO_SURFACE) {
Log.e("SDL", "Couldn't create surface");
return false;
}

if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");
createEGLContext();
if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, surface, surface, SDLActivity.mEGLContext)) {
Log.e("SDL", "Failed making EGL Context current");
return false;
}
}
SDLActivity.mEGLSurface = surface;
return true;
}
return false;
}

// EGL buffer flip
public static void flipEGL() {
try {
EGL10 egl = (EGL10)EGLContext.getEGL();

egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, null);

// drawing here

egl.eglWaitGL();

egl.eglSwapBuffers(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);


} catch(Exception e) {
Log.v("SDL", "flipEGL(): " + e);
for (StackTraceElement s : e.getStackTrace()) {
Log.v("SDL", s.toString());
}
}
}

// Audio
private static Object buf;

Expand Down Expand Up @@ -241,14 +405,6 @@ public void run() {
class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
View.OnKeyListener, View.OnTouchListener, SensorEventListener {

// This is what SDL runs in. It invokes SDL_main(), eventually
private Thread mSDLThread;

// EGL private objects
private EGLContext mEGLContext;
private EGLSurface mEGLSurface;
private EGLDisplay mEGLDisplay;

// Sensors
private static SensorManager mSensorManager;

Expand All @@ -268,37 +424,23 @@ public SDLSurface(Context context) {

// Called when we have a valid drawing surface
public void surfaceCreated(SurfaceHolder holder) {
//Log.v("SDL", "surfaceCreated()");

Log.v("SDL", "surfaceCreated()");
holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
SDLActivity.createEGLSurface();
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
}

// Called when we lose the surface
public void surfaceDestroyed(SurfaceHolder holder) {
//Log.v("SDL", "surfaceDestroyed()");

// Send a quit message to the application
SDLActivity.nativeQuit();

// Now wait for the SDL thread to quit
if (mSDLThread != null) {
try {
mSDLThread.join();
} catch(Exception e) {
Log.v("SDL", "Problem stopping thread: " + e);
}
mSDLThread = null;

//Log.v("SDL", "Finished waiting for SDL thread");
}

Log.v("SDL", "surfaceDestroyed()");
SDLActivity.nativePause();
enableSensor(Sensor.TYPE_ACCELEROMETER, false);
}

// Called when the surface is resized
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
//Log.v("SDL", "surfaceChanged()");
Log.v("SDL", "surfaceChanged()");

int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
switch (format) {
Expand Down Expand Up @@ -345,109 +487,16 @@ public void surfaceChanged(SurfaceHolder holder,
break;
}
SDLActivity.onNativeResize(width, height, sdlFormat);
Log.v("SDL", "Window size:" + width + "x"+height);

// Now start up the C app thread
if (mSDLThread == null) {
mSDLThread = new Thread(new SDLMain(), "SDLThread");
mSDLThread.start();
}
SDLActivity.startApp();
}

// unused
public void onDraw(Canvas canvas) {}


// EGL functions
public boolean initEGL(int majorVersion, int minorVersion) {
Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);

try {
EGL10 egl = (EGL10)EGLContext.getEGL();

EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

int[] version = new int[2];
egl.eglInitialize(dpy, version);

int EGL_OPENGL_ES_BIT = 1;
int EGL_OPENGL_ES2_BIT = 4;
int renderableType = 0;
if (majorVersion == 2) {
renderableType = EGL_OPENGL_ES2_BIT;
} else if (majorVersion == 1) {
renderableType = EGL_OPENGL_ES_BIT;
}
int[] configSpec = {
//EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_RENDERABLE_TYPE, renderableType,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
Log.e("SDL", "No EGL config available");
return false;
}
EGLConfig config = configs[0];

int EGL_CONTEXT_CLIENT_VERSION=0x3098;
int contextAttrs[] = new int[]
{
EGL_CONTEXT_CLIENT_VERSION, majorVersion,
EGL10.EGL_NONE
};
EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, contextAttrs);
if (ctx == EGL10.EGL_NO_CONTEXT) {
Log.e("SDL", "Couldn't create context");
return false;
}

EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
if (surface == EGL10.EGL_NO_SURFACE) {
Log.e("SDL", "Couldn't create surface");
return false;
}

if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) {
Log.e("SDL", "Couldn't make context current");
return false;
}

mEGLContext = ctx;
mEGLDisplay = dpy;
mEGLSurface = surface;

} catch(Exception e) {
Log.v("SDL", e + "");
for (StackTraceElement s : e.getStackTrace()) {
Log.v("SDL", s.toString());
}
}

return true;
}

// EGL buffer flip
public void flipEGL() {
try {
EGL10 egl = (EGL10)EGLContext.getEGL();

egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, null);

// drawing here

egl.eglWaitGL();

egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);


} catch(Exception e) {
Log.v("SDL", "flipEGL(): " + e);
for (StackTraceElement s : e.getStackTrace()) {
Log.v("SDL", s.toString());
}
}
}

// Key events
public boolean onKey(View v, int keyCode, KeyEvent event) {
Expand Down

0 comments on commit 542e375

Please sign in to comment.