Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android) add mechanics to resume application from background #660

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 89 additions & 10 deletions src/Magnum/Platform/AndroidApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ bool AndroidApplication::tryCreate(const Configuration& configuration, const GLC
return false;
}

_glConfiguration = glConfiguration;

/* Choose config */
const EGLint configAttributes[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, glConfiguration.colorBufferSize().r(),
EGL_GREEN_SIZE, glConfiguration.colorBufferSize().g(),
EGL_BLUE_SIZE, glConfiguration.colorBufferSize().b(),
EGL_ALPHA_SIZE, glConfiguration.colorBufferSize().a(),
EGL_DEPTH_SIZE, glConfiguration.depthBufferSize(),
EGL_STENCIL_SIZE, glConfiguration.stencilBufferSize(),
EGL_RED_SIZE, _glConfiguration.colorBufferSize().r(),
EGL_GREEN_SIZE, _glConfiguration.colorBufferSize().g(),
EGL_BLUE_SIZE, _glConfiguration.colorBufferSize().b(),
EGL_ALPHA_SIZE, _glConfiguration.colorBufferSize().a(),
EGL_DEPTH_SIZE, _glConfiguration.depthBufferSize(),
EGL_STENCIL_SIZE, _glConfiguration.stencilBufferSize(),
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
Expand Down Expand Up @@ -176,6 +178,71 @@ bool AndroidApplication::tryCreate(const Configuration& configuration, const GLC
return _context->tryCreate(glConfiguration);
}

bool AndroidApplication::reactivateContext() {
/* Choose config */
const EGLint configAttributes[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, _glConfiguration.colorBufferSize().r(),
EGL_GREEN_SIZE, _glConfiguration.colorBufferSize().g(),
EGL_BLUE_SIZE, _glConfiguration.colorBufferSize().b(),
EGL_ALPHA_SIZE, _glConfiguration.colorBufferSize().a(),
EGL_DEPTH_SIZE, _glConfiguration.depthBufferSize(),
EGL_STENCIL_SIZE, _glConfiguration.stencilBufferSize(),
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLint configCount;
EGLConfig config;
if (!eglChooseConfig(_display, configAttributes, &config, 1, &configCount)) {
Error() << "Platform::AndroidApplication::tryCreate(): cannot choose EGL config:"
<< Implementation::eglErrorString(eglGetError());
return false;
}

/* Resize native window and match it to the selected format */
EGLint format;
CORRADE_INTERNAL_ASSERT_OUTPUT(eglGetConfigAttrib(_display, config, EGL_NATIVE_VISUAL_ID, &format));
ANativeWindow_setBuffersGeometry(_state->window,
configuration.size().isZero() ? 0 : configuration.size().x(),
configuration.size().isZero() ? 0 : configuration.size().y(), format);

/* Create surface and context */
if (!(_surface = eglCreateWindowSurface(_display, config, _state->window, nullptr))) {
Error() << "Platform::AndroidApplication::tryCreate(): cannot create EGL window surface:"
<< Implementation::eglErrorString(eglGetError());
return false;
}

/* Make the context current */
CORRADE_INTERNAL_ASSERT_OUTPUT(eglMakeCurrent(_display, _surface, _surface, _glContext));
return true;
}

void AndroidApplication::pauseContext() {
/* Pause the context current */
CORRADE_INTERNAL_ASSERT_OUTPUT(eglMakeCurrent(_display, nullptr, nullptr, nullptr));
Comment on lines +222 to +223
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work if you kept the context current and just made it bound to no surface at all?

Suggested change
/* Pause the context current */
CORRADE_INTERNAL_ASSERT_OUTPUT(eglMakeCurrent(_display, nullptr, nullptr, nullptr));
/* Keep the GL context current but without any surface */
CORRADE_INTERNAL_ASSERT_OUTPUT(eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, _glContext));

Because I'm wondering if it's guaranteed that no app code gets executed between APP_CMD_LOST_FOCUS and APP_CMD_INIT_WINDOW. And if it does, it'd be probably good to have the GL context current.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested change, at least from my side, seems to work fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's guaranteed that no app code gets executed between APP_CMD_LOST_FOCUS and APP_CMD_INIT_WINDOW

I'm not sure about there being a guarantee, but it's one of the reasons I added _application_has_valid_surface - such that the user has some info to branch on.


_application_has_valid_surface = false;
}

void AndroidApplication::pauseApplication()
{
pauseContext();
}

void AndroidApplication::resumeApplication()
{
reactivateContext();
}

bool AndroidApplication::hasValidSurface() {
return _application_has_valid_surface;
}

bool AndroidApplication::hasFocus() {
return _application_has_focus;
}

Vector2i AndroidApplication::framebufferSize() const {
return {ANativeWindow_getWidth(_state->window),
ANativeWindow_getHeight(_state->window)};
Expand Down Expand Up @@ -214,18 +281,30 @@ void AndroidApplication::commandEvent(android_app* state, int32_t cmd) {
/* Create the application */
if(!data.instance) {
data.instance = data.instancer(state);
data.instance->drawEvent();
}
else {
data.instance->resumeApplication();
}
data.instance->drawEvent();
data.instance->_application_has_valid_surface = true;
break;

case APP_CMD_TERM_WINDOW:
/* Destroy the application */
data.instance.reset();
/* This can occur if: the application is destroyed, or just sent to the background */
if (data.instance) {
data.instance->pauseApplication();
}
break;

case APP_CMD_GAINED_FOCUS:
if (data.instance) {
data.instance->_application_has_focus = true;
}
break;
case APP_CMD_LOST_FOCUS:
/** @todo Make use of these */
if (data.instance) {
data.instance->_application_has_focus = false;
}
break;

case APP_CMD_CONFIG_CHANGED: {
Expand Down
50 changes: 50 additions & 0 deletions src/Magnum/Platform/AndroidApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,51 @@ class AndroidApplication {

/** @{ @name Screen handling */

/**
* @brief Recreate the OpenGL context
*
* returns @cpp false @ce if the context cannot be created,
* @cpp true @ce otherwise.
*/
bool reactivateContext();

/**
* @brief Pause the OpenGL context
*
*
*/
void pauseContext();

/**
* @brief Pause the Application
*
* Called when the application is attempted to be terminated or sent to the background
*/
virtual void pauseApplication();

/**
* @brief Resume the Application
*
* Called when the application comes back to the foreground
*/
virtual void resumeApplication();

/**
* @brief OpenGL surface useability
*
* returns @cpp false @ce if the surface is not available for rendering
* @cpp true @ce otherwise.
*/
bool hasValidSurface();

/**
* @brief Application focus
*
* returns @cpp false @ce if the application is not focused.
* @cpp true @ce otherwise.
*/
bool hasFocus();

public:
/**
* @brief Window size
Expand Down Expand Up @@ -564,6 +609,11 @@ class AndroidApplication {
EGLSurface _surface;
EGLContext _glContext;

GLConfiguration _glConfiguration;

bool _application_has_valid_surface = false;
bool _application_has_focus = false;

/* We have no way to query previous pointer positions, so we have to
maintain them like this. For pointers capable of hover (mouse, pen)
the _previousHoverPointerPosition is used, NaN signalling that the
Expand Down