From 77d178e9197dba94d898572a585923ce8c1045af Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 12 Sep 2022 18:43:22 -0700 Subject: [PATCH] Started handling messages from background isolates for Android (flutter/engine#35804) --- engine/src/flutter/lib/ui/platform_dispatcher.dart | 2 +- .../android/io/flutter/embedding/engine/FlutterJNI.java | 2 +- .../io/flutter/embedding/engine/dart/DartMessenger.java | 5 ++++- .../embedding/engine/dart/PlatformMessageHandler.java | 1 + .../platform/android/platform_message_handler_android.cc | 4 ++-- .../platform/android/platform_message_handler_android.h | 2 +- .../shell/platform/android/platform_view_android_jni_impl.cc | 2 +- 7 files changed, 11 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/lib/ui/platform_dispatcher.dart b/engine/src/flutter/lib/ui/platform_dispatcher.dart index a24b2f60f8..ca1d4bc5f3 100644 --- a/engine/src/flutter/lib/ui/platform_dispatcher.dart +++ b/engine/src/flutter/lib/ui/platform_dispatcher.dart @@ -586,7 +586,7 @@ class PlatformDispatcher { /// [token]. This is required if platform channels are to be used on a /// background isolate. void registerBackgroundIsolate(RootIsolateToken token) { - if (!Platform.isIOS) { + if (!(Platform.isIOS || Platform.isAndroid)) { // Issue: https://github.com/flutter/flutter/issues/13937 throw UnimplementedError("Platform doesn't yet support platform channels on background isolates."); } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index cbe7a9ceb8..799ad214b3 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -977,7 +977,7 @@ public class FlutterJNI { nativeCleanupMessageData(messageData); } - // Called by native on the ui thread. + // Called by native on any thread. // TODO(mattcarroll): determine if message is nonull or nullable @SuppressWarnings("unused") @VisibleForTesting diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java index 8a90964d27..98cb37874c 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java @@ -311,6 +311,7 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler { @Nullable ByteBuffer message, int replyId, long messageData) { + // Called from any thread. final DartMessengerTaskQueue taskQueue = (handlerInfo != null) ? handlerInfo.taskQueue : null; TraceSection.beginAsyncSection("PlatformChannel ScheduleHandler on " + channel, replyId); Runnable myRunnable = @@ -338,11 +339,13 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler { @Override public void handleMessageFromDart( @NonNull String channel, @Nullable ByteBuffer message, int replyId, long messageData) { - // Called from the ui thread. + // Called from any thread. Log.v(TAG, "Received message from Dart over channel '" + channel + "'"); HandlerInfo handlerInfo; boolean messageDeferred; + // This lock can potentially be a bottleneck and could replaced with a + // read/write lock. synchronized (handlersLock) { handlerInfo = messageHandlers.get(channel); messageDeferred = (enableBufferingIncomingMessages.get() && handlerInfo == null); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java index 36a7a9d7eb..f062f422aa 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java @@ -10,6 +10,7 @@ import java.nio.ByteBuffer; /** Handler that receives messages from Dart code. */ public interface PlatformMessageHandler { + /** Called from any thread. */ void handleMessageFromDart( @NonNull final String channel, @Nullable ByteBuffer message, diff --git a/engine/src/flutter/shell/platform/android/platform_message_handler_android.cc b/engine/src/flutter/shell/platform/android/platform_message_handler_android.cc index 2179611a2f..8440898be9 100644 --- a/engine/src/flutter/shell/platform/android/platform_message_handler_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_message_handler_android.cc @@ -50,8 +50,8 @@ void PlatformMessageHandlerAndroid::InvokePlatformMessageEmptyResponseCallback( // |PlatformView| void PlatformMessageHandlerAndroid::HandlePlatformMessage( std::unique_ptr message) { - // Called from the ui thread. - int response_id = next_response_id_++; + // Called from any thread. + int response_id = next_response_id_.fetch_add(1); if (auto response = message->response()) { std::lock_guard lock(pending_responses_mutex_); pending_responses_[response_id] = response; diff --git a/engine/src/flutter/shell/platform/android/platform_message_handler_android.h b/engine/src/flutter/shell/platform/android/platform_message_handler_android.h index ef132c7a78..8fdfa60efa 100644 --- a/engine/src/flutter/shell/platform/android/platform_message_handler_android.h +++ b/engine/src/flutter/shell/platform/android/platform_message_handler_android.h @@ -28,7 +28,7 @@ class PlatformMessageHandlerAndroid : public PlatformMessageHandler { private: const std::shared_ptr jni_facade_; - int next_response_id_ = 1; + std::atomic next_response_id_ = 1; std::unordered_map> pending_responses_; std::mutex pending_responses_mutex_; diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc index 2d02fcbfd3..24516c75e9 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc @@ -1140,7 +1140,7 @@ PlatformViewAndroidJNIImpl::~PlatformViewAndroidJNIImpl() = default; void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage( std::unique_ptr message, int responseId) { - // Called from the ui thread. + // Called from any thread. JNIEnv* env = fml::jni::AttachCurrentThread(); auto java_object = java_object_.get(env);