Skip to content

Commit

Permalink
Terminate Qt gracefully
Browse files Browse the repository at this point in the history
When the application is closed via the task manager on Android
then we need to ensure that that the application can close down
any running event loops. So we wake up all the event loops and
then call quit() directly on the application object to start a
graceful termination of the application.

In order to aid the graceful termination of Qt then a check is
added to ensure that it does not try to create a new surface when
the application is suspended. This prevents it from locking
while trying to create a new surface when this is not possible.

Fixes: QTBUG-70772
Change-Id: I6795b3d280e178d7f1207004a1b965a31a0cc9e9
Reviewed-by: Paul Olav Tvete <[email protected]>
Reviewed-by: Andy Shaw <[email protected]>
  • Loading branch information
bog-dan-ro authored and AndyShawQt committed Feb 28, 2019
1 parent 077e499 commit 8880ef7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/plugins/platforms/android/androidjnimain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,15 @@ static void quitQtAndroidPlugin(JNIEnv *env, jclass /*clazz*/)
static void terminateQt(JNIEnv *env, jclass /*clazz*/)
{
// QAndroidEventDispatcherStopper is stopped when the user uses the task manager to kill the application
if (!QAndroidEventDispatcherStopper::instance()->stopped()) {
sem_wait(&m_terminateSemaphore);
sem_destroy(&m_terminateSemaphore);
if (QAndroidEventDispatcherStopper::instance()->stopped()) {
QAndroidEventDispatcherStopper::instance()->startAll();
QCoreApplication::quit();
QAndroidEventDispatcherStopper::instance()->goingToStop(false);
}

sem_wait(&m_terminateSemaphore);
sem_destroy(&m_terminateSemaphore);

env->DeleteGlobalRef(m_applicationClass);
env->DeleteGlobalRef(m_classLoaderObject);
if (m_resourcesObj)
Expand All @@ -583,10 +588,7 @@ static void terminateQt(JNIEnv *env, jclass /*clazz*/)
m_androidPlatformIntegration = nullptr;
delete m_androidAssetsFileEngineHandler;
m_androidAssetsFileEngineHandler = nullptr;

if (!QAndroidEventDispatcherStopper::instance()->stopped()) {
sem_post(&m_exitSemaphore);
}
sem_post(&m_exitSemaphore);
}

static void setSurface(JNIEnv *env, jobject /*thiz*/, jint id, jobject jSurface, jint w, jint h)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#include <QSurfaceFormat>
#include <QtGui/private/qwindow_p.h>
#include <QtGui/qguiapplication.h>

#include <qpa/qwindowsysteminterface.h>
#include <qpa/qplatformscreen.h>
Expand Down Expand Up @@ -121,7 +122,7 @@ void QAndroidPlatformOpenGLWindow::setGeometry(const QRect &rect)

EGLSurface QAndroidPlatformOpenGLWindow::eglSurface(EGLConfig config)
{
if (QAndroidEventDispatcherStopper::stopped())
if (QAndroidEventDispatcherStopper::stopped() || QGuiApplication::applicationState() == Qt::ApplicationSuspended)
return m_eglSurface;

QMutexLocker lock(&m_surfaceMutex);
Expand Down

0 comments on commit 8880ef7

Please sign in to comment.