diff options
Diffstat (limited to 'src/corelib/platform/android/qandroidnativeinterface.cpp')
-rw-r--r-- | src/corelib/platform/android/qandroidnativeinterface.cpp | 107 |
1 files changed, 45 insertions, 62 deletions
diff --git a/src/corelib/platform/android/qandroidnativeinterface.cpp b/src/corelib/platform/android/qandroidnativeinterface.cpp index a3a10091cb..fc3a09c78b 100644 --- a/src/corelib/platform/android/qandroidnativeinterface.cpp +++ b/src/corelib/platform/android/qandroidnativeinterface.cpp @@ -1,50 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/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 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <QtCore/qcoreapplication_platform.h> +#include <QtCore/private/qnativeinterface_p.h> #include <QtCore/private/qjnihelpers_p.h> #include <QtCore/qjniobject.h> #if QT_CONFIG(future) && !defined(QT_NO_QOBJECT) -#include <QtConcurrent/QtConcurrent> +#include <QtCore/qfuture.h> +#include <QtCore/qfuturewatcher.h> #include <QtCore/qpromise.h> +#include <QtCore/qtimer.h> +#include <QtCore/qthreadpool.h> #include <deque> +#include <memory> #endif QT_BEGIN_NAMESPACE @@ -52,10 +21,14 @@ QT_BEGIN_NAMESPACE #if QT_CONFIG(future) && !defined(QT_NO_QOBJECT) static const char qtNativeClassName[] = "org/qtproject/qt/android/QtNative"; -typedef std::pair<std::function<QVariant()>, QSharedPointer<QPromise<QVariant>>> RunnablePair; -typedef std::deque<RunnablePair> PendingRunnables; +struct PendingRunnable { + std::function<QVariant()> function; + std::shared_ptr<QPromise<QVariant>> promise; +}; + +using PendingRunnables = std::deque<PendingRunnable>; Q_GLOBAL_STATIC(PendingRunnables, g_pendingRunnables); -static QBasicMutex g_pendingRunnablesMutex; +Q_CONSTINIT static QBasicMutex g_pendingRunnablesMutex; #endif /*! @@ -73,14 +46,14 @@ static QBasicMutex g_pendingRunnablesMutex; QT_DEFINE_NATIVE_INTERFACE(QAndroidApplication); /*! - \fn jobject QNativeInterface::QAndroidApplication::context() + \fn QJniObject QNativeInterface::QAndroidApplication::context() - Returns the Android context as a \c jobject. The context is an \c Activity + Returns the Android context as a \c QJniObject. The context is an \c Activity if the main activity object is valid. Otherwise, the context is a \c Service. \since 6.2 */ -jobject QNativeInterface::QAndroidApplication::context() +QtJniTypes::Context QNativeInterface::QAndroidApplication::context() { return QtAndroidPrivate::context(); } @@ -95,7 +68,7 @@ jobject QNativeInterface::QAndroidApplication::context() */ bool QNativeInterface::QAndroidApplication::isActivityContext() { - return QtAndroidPrivate::activity(); + return QtAndroidPrivate::activity().isValid(); } /*! @@ -121,8 +94,7 @@ int QNativeInterface::QAndroidApplication::sdkVersion() */ void QNativeInterface::QAndroidApplication::hideSplashScreen(int duration) { - QJniObject::callStaticMethod<void>("org/qtproject/qt/android/QtNative", - "hideSplashScreen", "(I)V", duration); + QtAndroidPrivate::activity().callMethod<void>("hideSplashScreen", duration); } /*! @@ -190,12 +162,12 @@ QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread( const std::function<QVariant()> &runnable, const QDeadlineTimer timeout) { - QSharedPointer<QPromise<QVariant>> promise(new QPromise<QVariant>()); + auto promise = std::make_shared<QPromise<QVariant>>(); QFuture<QVariant> future = promise->future(); promise->start(); - (void) QtConcurrent::run([=, &future]() { - if (!timeout.isForever()) { + if (!timeout.isForever()) { + QThreadPool::globalInstance()->start([=]() mutable { QEventLoop loop; QTimer::singleShot(timeout.remainingTime(), &loop, [&]() { future.cancel(); @@ -211,12 +183,24 @@ QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread( loop.quit(); }); watcher.setFuture(future); + + // we're going to sleep, make sure we don't block + // QThreadPool::globalInstance(): + + QThreadPool::globalInstance()->releaseThread(); + const auto sg = qScopeGuard([] { + QThreadPool::globalInstance()->reserveThread(); + }); loop.exec(); - } - }); + }); + } QMutexLocker locker(&g_pendingRunnablesMutex); - g_pendingRunnables->push_back(std::pair(runnable, promise)); +#ifdef __cpp_aggregate_paren_init + g_pendingRunnables->emplace_back(runnable, std::move(promise)); +#else + g_pendingRunnables->push_back({runnable, std::move(promise)}); +#endif locker.unlock(); QJniObject::callStaticMethod<void>(qtNativeClassName, @@ -234,24 +218,23 @@ static void runPendingCppRunnables(JNIEnv */*env*/, jobject /*obj*/) if (g_pendingRunnables->empty()) break; - std::pair pair = std::move(g_pendingRunnables->front()); + PendingRunnable r = std::move(g_pendingRunnables->front()); g_pendingRunnables->pop_front(); locker.unlock(); // run the runnable outside the sync block! - auto promise = pair.second; - if (!promise->isCanceled()) - promise->addResult(pair.first()); - promise->finish(); + if (!r.promise->isCanceled()) + r.promise->addResult(r.function()); + r.promise->finish(); } } #endif -bool QtAndroidPrivate::registerNativeInterfaceNatives() +bool QtAndroidPrivate::registerNativeInterfaceNatives(QJniEnvironment &env) { #if QT_CONFIG(future) && !defined(QT_NO_QOBJECT) - JNINativeMethod methods = {"runPendingCppRunnables", "()V", (void *)runPendingCppRunnables}; - return QJniEnvironment().registerNativeMethods(qtNativeClassName, &methods, 1); + const JNINativeMethod methods = {"runPendingCppRunnables", "()V", (void *)runPendingCppRunnables}; + return env.registerNativeMethods(qtNativeClassName, &methods, 1); #else return true; #endif |