Fixed OpenAL eating CPU in background

This commit is contained in:
pelya
2012-06-21 18:04:11 +03:00
parent 532b652594
commit 0f478832a7
8 changed files with 43 additions and 12 deletions

View File

@@ -57,7 +57,7 @@ class AudioThread {
if( mParent.isPaused() ) if( mParent.isPaused() )
{ {
try{ try{
Thread.sleep(200); Thread.sleep(500);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
else else

View File

@@ -15,6 +15,7 @@ SdlVideoResize=y
SdlVideoResizeKeepAspect=n SdlVideoResizeKeepAspect=n
CompatibilityHacks= CompatibilityHacks=
CompatibilityHacksStaticInit=n CompatibilityHacksStaticInit=n
CompatibilityHacksTextInputEmulatesHwKeyboard=n
AppUsesMouse=n AppUsesMouse=n
AppNeedsTwoButtonMouse=n AppNeedsTwoButtonMouse=n
ShowMouseCursor=n ShowMouseCursor=n
@@ -25,12 +26,12 @@ AppUsesJoystick=n
AppHandlesJoystickSensitivity=n AppHandlesJoystickSensitivity=n
AppUsesMultitouch=n AppUsesMultitouch=n
NonBlockingSwapBuffers=n NonBlockingSwapBuffers=n
RedefinedKeys="RETURN LCTRL PAGEUP PAGEDOWN LCTRL" RedefinedKeys="RETURN LCTRL NO_REMAP NO_REMAP LCTRL"
AppTouchscreenKeyboardKeysAmount=4 AppTouchscreenKeyboardKeysAmount=4
AppTouchscreenKeyboardKeysAmountAutoFire=1 AppTouchscreenKeyboardKeysAmountAutoFire=1
RedefinedKeysScreenKb="RETURN LCTRL PAGEUP PAGEDOWN LCTRL" RedefinedKeysScreenKb="RETURN LCTRL PAGEUP PAGEDOWN LCTRL"
StartupMenuButtonTimeout=3000 StartupMenuButtonTimeout=3000
HiddenMenuOptions='' HiddenMenuOptions='OptionalDownloadConfig'
FirstStartMenuOptions='' FirstStartMenuOptions=''
MultiABI=n MultiABI=n
AppVersionCode=110014 AppVersionCode=110014

View File

@@ -34,8 +34,8 @@ StartupMenuButtonTimeout=3000
HiddenMenuOptions='OptionalDownloadConfig' HiddenMenuOptions='OptionalDownloadConfig'
FirstStartMenuOptions='' FirstStartMenuOptions=''
MultiABI=n MultiABI=n
AppVersionCode=03302 AppVersionCode=03303
AppVersionName="0.3.3.02" AppVersionName="0.3.3.03"
ResetSdlConfigForThisVersion=n ResetSdlConfigForThisVersion=n
DeleteFilesOnUpgrade="%" DeleteFilesOnUpgrade="%"
CompiledLibraries=" sdl_image physfs boost_system curl openal jpeg png tremor ogg" CompiledLibraries=" sdl_image physfs boost_system curl openal jpeg png tremor ogg"

View File

@@ -14,7 +14,7 @@ LOCAL_CPP_EXTENSION := .cpp
LOCAL_SRC_FILES := $(foreach F, $(APP_SUBDIRS), $(addprefix $(F)/,$(notdir $(wildcard $(LOCAL_PATH)/$(F)/*.cpp)))) LOCAL_SRC_FILES := $(foreach F, $(APP_SUBDIRS), $(addprefix $(F)/,$(notdir $(wildcard $(LOCAL_PATH)/$(F)/*.cpp))))
LOCAL_SRC_FILES += $(foreach F, $(APP_SUBDIRS), $(addprefix $(F)/,$(notdir $(wildcard $(LOCAL_PATH)/$(F)/*.c)))) LOCAL_SRC_FILES += $(foreach F, $(APP_SUBDIRS), $(addprefix $(F)/,$(notdir $(wildcard $(LOCAL_PATH)/$(F)/*.c))))
LOCAL_SHARED_LIBRARIES := LOCAL_SHARED_LIBRARIES := sdl-$(SDL_VERSION)
LOCAL_STATIC_LIBRARIES := LOCAL_STATIC_LIBRARIES :=

View File

@@ -28,6 +28,12 @@
#include "AL/alc.h" #include "AL/alc.h"
#include "AL/android.h" #include "AL/android.h"
typedef void ( * SDL_ANDROID_ApplicationPutToBackgroundCallback_t ) (void);
extern int SDL_ANDROID_SetOpenALPutToBackgroundCallback(
SDL_ANDROID_ApplicationPutToBackgroundCallback_t PutToBackground,
SDL_ANDROID_ApplicationPutToBackgroundCallback_t Restored );
static int doPause=0; static int doPause=0;
int resumeHandled; int resumeHandled;
int pauseHandled; int pauseHandled;
@@ -106,6 +112,10 @@ static void* thread_function(void* arg)
{ {
(*env)->CallNonvirtualVoidMethod(env, track, cAudioTrack, mPause); (*env)->CallNonvirtualVoidMethod(env, track, cAudioTrack, mPause);
pauseHandled=1; pauseHandled=1;
while(doPause)
{
usleep(500000);
}
} }
if(!doPause && !resumeHandled) if(!doPause && !resumeHandled)
{ {
@@ -144,6 +154,7 @@ static ALCboolean android_open_playback(ALCdevice *device, const ALCchar *device
int channels; int channels;
int bytes; int bytes;
SDL_ANDROID_SetOpenALPutToBackgroundCallback(al_android_pause_playback, al_android_resume_playback);
if (!cAudioTrack) if (!cAudioTrack)
{ {
/* Cache AudioTrack class and it's method id's /* Cache AudioTrack class and it's method id's
@@ -308,4 +319,4 @@ AL_API void AL_APIENTRY al_android_resume_playback()
doPause=0; doPause=0;
resumeHandled=0; resumeHandled=0;
AL_PRINT("Audio resumed."); AL_PRINT("Audio resumed.");
} }

View File

@@ -986,10 +986,10 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouseButtonsPressed) (JNIEnv* env, jobj
btn = SDL_BUTTON_MIDDLE; btn = SDL_BUTTON_MIDDLE;
break; break;
case MOUSE_HW_BUTTON_BACK: case MOUSE_HW_BUTTON_BACK:
btn = SDL_BUTTON_WHEELUP; btn = SDL_BUTTON_X1;
break; break;
case MOUSE_HW_BUTTON_FORWARD: case MOUSE_HW_BUTTON_FORWARD:
btn = SDL_BUTTON_WHEELDOWN; btn = SDL_BUTTON_X2;
break; break;
} }
SDL_ANDROID_MainThreadPushMouseButton( pressedState ? SDL_PRESSED : SDL_RELEASED, btn ); SDL_ANDROID_MainThreadPushMouseButton( pressedState ? SDL_PRESSED : SDL_RELEASED, btn );

View File

@@ -84,6 +84,8 @@ static void appRestoredCallbackDefault(void)
static SDL_ANDROID_ApplicationPutToBackgroundCallback_t appPutToBackgroundCallback = appPutToBackgroundCallbackDefault; static SDL_ANDROID_ApplicationPutToBackgroundCallback_t appPutToBackgroundCallback = appPutToBackgroundCallbackDefault;
static SDL_ANDROID_ApplicationPutToBackgroundCallback_t appRestoredCallback = appRestoredCallbackDefault; static SDL_ANDROID_ApplicationPutToBackgroundCallback_t appRestoredCallback = appRestoredCallbackDefault;
static SDL_ANDROID_ApplicationPutToBackgroundCallback_t openALPutToBackgroundCallback = NULL;
static SDL_ANDROID_ApplicationPutToBackgroundCallback_t openALRestoredCallback = NULL;
int SDL_ANDROID_CallJavaSwapBuffers() int SDL_ANDROID_CallJavaSwapBuffers()
{ {
@@ -120,6 +122,8 @@ int SDL_ANDROID_CallJavaSwapBuffers()
__android_log_print(ANDROID_LOG_INFO, "libSDL", "OpenGL context recreated, refreshing textures"); __android_log_print(ANDROID_LOG_INFO, "libSDL", "OpenGL context recreated, refreshing textures");
SDL_ANDROID_VideoContextRecreated(); SDL_ANDROID_VideoContextRecreated();
appRestoredCallback(); appRestoredCallback();
if(openALRestoredCallback)
openALRestoredCallback();
} }
if( showScreenKeyboardDeferred ) if( showScreenKeyboardDeferred )
{ {
@@ -195,6 +199,9 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeGlContextLost) ( JNIEnv* env, jobject thiz
__android_log_print(ANDROID_LOG_INFO, "libSDL", "OpenGL context lost, waiting for new OpenGL context"); __android_log_print(ANDROID_LOG_INFO, "libSDL", "OpenGL context lost, waiting for new OpenGL context");
glContextLost = 1; glContextLost = 1;
appPutToBackgroundCallback(); appPutToBackgroundCallback();
if(openALPutToBackgroundCallback)
openALPutToBackgroundCallback();
#if SDL_VERSION_ATLEAST(1,3,0) #if SDL_VERSION_ATLEAST(1,3,0)
//if( ANDROID_CurrentWindow ) //if( ANDROID_CurrentWindow )
// SDL_SendWindowEvent(ANDROID_CurrentWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0); // SDL_SendWindowEvent(ANDROID_CurrentWindow, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
@@ -293,6 +300,18 @@ int SDL_ANDROID_SetApplicationPutToBackgroundCallback(
appRestoredCallback = appRestored; appRestoredCallback = appRestored;
} }
extern int SDL_ANDROID_SetOpenALPutToBackgroundCallback(
SDL_ANDROID_ApplicationPutToBackgroundCallback_t PutToBackground,
SDL_ANDROID_ApplicationPutToBackgroundCallback_t Restored );
int SDL_ANDROID_SetOpenALPutToBackgroundCallback(
SDL_ANDROID_ApplicationPutToBackgroundCallback_t PutToBackground,
SDL_ANDROID_ApplicationPutToBackgroundCallback_t Restored )
{
openALPutToBackgroundCallback = PutToBackground;
openALRestoredCallback = Restored;
}
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(Settings_nativeSetSmoothVideo) (JNIEnv* env, jobject thiz) JAVA_EXPORT_NAME(Settings_nativeSetSmoothVideo) (JNIEnv* env, jobject thiz)
{ {

View File

@@ -221,11 +221,11 @@ SDL_ANDROID_SetApplicationPutToBackgroundCallback( callback_t appPutToBackground
where callback_t is function pointer of type "void (*) void". where callback_t is function pointer of type "void (*) void".
The default callbacks will call another Android-specific functions: The default callbacks will call another Android-specific functions:
SDL_ANDROID_PauseAudioPlayback() and SDL_ANDROID_ResumeAudioPlayback() SDL_ANDROID_PauseAudioPlayback() and SDL_ANDROID_ResumeAudioPlayback()
which will pause and resume audio from HW layer, so appplication does not need to destroy and re-init audio. which will pause and resume audio from HW layer, so appplication does not need to destroy and re-init audio,
and in general you don't need to redefine those functions, unless you want to play audio in background.
Also, the usual event SDL_ACTIVEEVENT with flag SDL_APPACTIVE will be sent when that happens, Also, the usual event SDL_ACTIVEEVENT with flag SDL_APPACTIVE will be sent when that happens,
and also SDL_VIDEORESIZE event will be sent (the same behavior as in MacOsX SDL implementation). and also SDL_VIDEORESIZE event will be sent (the same behavior as in MacOsX SDL implementation).
If you're using OpenAL for an audio playback you have to call functions al_android_pause_playback() If you're using OpenAL it will be paused automatically when your app goes to background.
and al_android_resume_playback() by yourself when SDL calls your callbacks.
If you're using pure SDL 1.2 API (with or without HW acceleration) you don't need to worry about anything - If you're using pure SDL 1.2 API (with or without HW acceleration) you don't need to worry about anything -
the SDL itself will re-create GL textures and fill them with pixel data from existing SDL HW surfaces, the SDL itself will re-create GL textures and fill them with pixel data from existing SDL HW surfaces,