diff --git a/src/plugins/platforms/android/android.pro b/src/plugins/platforms/android/android.pro index f55bc40a55e..3c3a4b4b2ed 100644 --- a/src/plugins/platforms/android/android.pro +++ b/src/plugins/platforms/android/android.pro @@ -26,6 +26,7 @@ INCLUDEPATH += \ $$QT_SOURCE_TREE/src/3rdparty/android SOURCES += $$PWD/androidplatformplugin.cpp \ + $$PWD/androiddeadlockprotector.cpp \ $$PWD/androidjnimain.cpp \ $$PWD/androidjniaccessibility.cpp \ $$PWD/androidjniinput.cpp \ @@ -53,6 +54,7 @@ SOURCES += $$PWD/androidplatformplugin.cpp \ $$PWD/qandroideventdispatcher.cpp HEADERS += $$PWD/qandroidplatformintegration.h \ + $$PWD/androidandroiddeadlockprotector.h \ $$PWD/androidjnimain.h \ $$PWD/androidjniaccessibility.h \ $$PWD/androidjniinput.h \ diff --git a/src/plugins/platforms/android/androiddeadlockprotector.cpp b/src/plugins/platforms/android/androiddeadlockprotector.cpp new file mode 100644 index 00000000000..e53e0c2447e --- /dev/null +++ b/src/plugins/platforms/android/androiddeadlockprotector.cpp @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "androiddeadlockprotector.h" + +QAtomicInt AndroidDeadlockProtector::s_blocked(0); + diff --git a/src/plugins/platforms/android/androiddeadlockprotector.h b/src/plugins/platforms/android/androiddeadlockprotector.h new file mode 100644 index 00000000000..a0a82aa9d1b --- /dev/null +++ b/src/plugins/platforms/android/androiddeadlockprotector.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ANDROID_DEADLOCKPROTECTOR_H +#define ANDROID_DEADLOCKPROTECTOR_H + +#include + +QT_BEGIN_NAMESPACE + +class AndroidDeadlockProtector +{ +public: + AndroidDeadlockProtector() + : m_acquired(0) + { + } + + ~AndroidDeadlockProtector() { + if (m_acquired) + s_blocked.storeRelease(0); + } + + bool acquire() { + m_acquired = s_blocked.testAndSetAcquire(0, 1); + return m_acquired; + } + +private: + static QAtomicInt s_blocked; + int m_acquired; +}; + +QT_END_NAMESPACE + +#endif // ANDROID_DEADLOCKPROTECTOR_H + diff --git a/src/plugins/platforms/android/qandroidinputcontext.cpp b/src/plugins/platforms/android/qandroidinputcontext.cpp index e3d488b9583..a3848c9c2b1 100644 --- a/src/plugins/platforms/android/qandroidinputcontext.cpp +++ b/src/plugins/platforms/android/qandroidinputcontext.cpp @@ -38,6 +38,7 @@ #include "androidjnimain.h" #include "androidjniinput.h" #include "qandroideventdispatcher.h" +#include "androiddeadlockprotector.h" #include #include #include @@ -998,6 +999,9 @@ QVariant QAndroidInputContext::queryFocusObjectThreadSafe(Qt::InputMethodQuery q const bool inMainThread = qGuiApp->thread() == QThread::currentThread(); if (QAndroidEventDispatcherStopper::stopped() && !inMainThread) return retval; + AndroidDeadlockProtector protector; + if (!inMainThread && !protector.acquire()) + return retval; QMetaObject::invokeMethod(this, "queryFocusObjectUnsafe", inMainThread ? Qt::DirectConnection : Qt::BlockingQueuedConnection, @@ -1015,6 +1019,9 @@ QSharedPointer QAndroidInputContext::focusObjectInputMet const bool inMainThread = qGuiApp->thread() == QThread::currentThread(); if (QAndroidEventDispatcherStopper::stopped() && !inMainThread) return QSharedPointer(); + AndroidDeadlockProtector protector; + if (!inMainThread && !protector.acquire()) + return QSharedPointer(); QInputMethodQueryEvent *queryEvent = 0; QMetaObject::invokeMethod(this, "focusObjectInputMethodQueryUnsafe", @@ -1052,7 +1059,9 @@ void QAndroidInputContext::sendInputMethodEventThreadSafe(QInputMethodEvent *eve const bool inMainThread = qGuiApp->thread() == QThread::currentThread(); if (QAndroidEventDispatcherStopper::stopped() && !inMainThread) return; - + AndroidDeadlockProtector protector; + if (!inMainThread && !protector.acquire()) + return; QMetaObject::invokeMethod(this, "sendInputMethodEventUnsafe", inMainThread ? Qt::DirectConnection : Qt::BlockingQueuedConnection, Q_ARG(QInputMethodEvent*, event)); diff --git a/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp b/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp index 8afc35c57a1..8dc8e84f0ae 100644 --- a/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp +++ b/src/plugins/platforms/android/qandroidplatformopenglwindow.cpp @@ -37,6 +37,7 @@ #include "qandroidplatformscreen.h" #include "androidjnimain.h" #include "qandroideventdispatcher.h" +#include "androiddeadlockprotector.h" #include #include @@ -120,6 +121,10 @@ EGLSurface QAndroidPlatformOpenGLWindow::eglSurface(EGLConfig config) QMutexLocker lock(&m_surfaceMutex); if (m_nativeSurfaceId == -1) { + AndroidDeadlockProtector protector; + if (!protector.acquire()) + return m_eglSurface; + const bool windowStaysOnTop = bool(window()->flags() & Qt::WindowStaysOnTopHint); m_nativeSurfaceId = QtAndroid::createSurface(this, geometry(), windowStaysOnTop, 32); m_surfaceWaitCondition.wait(&m_surfaceMutex); diff --git a/src/plugins/platforms/android/qandroidplatformscreen.cpp b/src/plugins/platforms/android/qandroidplatformscreen.cpp index 1775b442c49..684ae64703f 100644 --- a/src/plugins/platforms/android/qandroidplatformscreen.cpp +++ b/src/plugins/platforms/android/qandroidplatformscreen.cpp @@ -43,6 +43,7 @@ #include "qandroidplatformwindow.h" #include "androidjnimain.h" #include "androidjnimenu.h" +#include "androiddeadlockprotector.h" #include #include @@ -299,6 +300,9 @@ void QAndroidPlatformScreen::doRedraw() QMutexLocker lock(&m_surfaceMutex); if (m_id == -1 && m_rasterSurfaces) { m_id = QtAndroid::createSurface(this, m_availableGeometry, true, m_depth); + AndroidDeadlockProtector protector; + if (!protector.acquire()) + return; m_surfaceWaitCondition.wait(&m_surfaceMutex); }