src/video/android/SDL_androidevents.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Jun 2012 13:57:42 -0300
changeset 6330 0fa55ca2efdd
parent 6138 4c64952a58fb
child 6469 ce84310d85ef
permissions -rwxr-xr-x
Fixes #1422, restores GL context automatically under Android
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 #include "SDL_androidevents.h"
    26 
    27 void
    28 Android_PumpEvents(_THIS)
    29 {
    30     static int isPaused = 0;
    31     /* No polling necessary */
    32 
    33     /*
    34      * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
    35      * When the pause semaphoe is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
    36      * When the resume semaphore is signaled, SDL_GL_CreateContext is called which in turn calls Java code
    37      * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
    38      */
    39     if (isPaused) {
    40 #if SDL_ANDROID_BLOCK_ON_PAUSE
    41         if(SDL_SemWait(Android_ResumeSem) == 0) {
    42 #else
    43         if(SDL_SemTryWait(Android_ResumeSem) == 0) {
    44 #endif
    45             isPaused = 0;
    46             /* TODO: Should we double check if we are on the same thread as the one that made the original GL context?
    47              * This call will go through the following chain of calls in Java:
    48              * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
    49              * SDLActivity::createEGLContext will attempt to restore the GL context first, and if that fails it will create a new one
    50              * If a new GL context is created, the user needs to restore the textures manually (TODO: notify the user that this happened with a message)
    51              */
    52             SDL_GL_CreateContext(Android_Window);
    53         }
    54     }
    55     else {
    56         if(SDL_SemTryWait(Android_PauseSem) == 0) {
    57             /* If we fall in here, the system is/was paused */
    58             isPaused = 1;
    59         }
    60     }
    61 }
    62 
    63 #endif /* SDL_VIDEO_DRIVER_ANDROID */
    64 
    65 /* vi: set ts=4 sw=4 expandtab: */