Isolate platform channels for desktop (flutter/engine#35893)

This commit is contained in:
gaaclarke
2022-09-20 16:56:04 -07:00
committed by GitHub
parent 44d6ab7959
commit 6b2a14d348
11 changed files with 268 additions and 15 deletions

View File

@@ -2073,6 +2073,7 @@ FILE: ../../../flutter/shell/platform/embedder/pixel_formats.cc
FILE: ../../../flutter/shell/platform/embedder/pixel_formats.h
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.cc
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.h
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder_unittests.cc
FILE: ../../../flutter/shell/platform/embedder/test_utils/key_codes.g.h
FILE: ../../../flutter/shell/platform/embedder/test_utils/proc_table_replacement.h
FILE: ../../../flutter/shell/platform/embedder/vsync_waiter_embedder.cc

View File

@@ -586,10 +586,6 @@ class PlatformDispatcher {
/// [token]. This is required if platform channels are to be used on a
/// background isolate.
void registerBackgroundIsolate(RootIsolateToken token) {
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.");
}
__registerBackgroundIsolate(token._token);
}
@FfiNative<Void Function(Int64)>('PlatformConfigurationNativeApi::RegisterBackgroundIsolate')

View File

@@ -24,6 +24,13 @@ class PlatformMessageHandler {
virtual void HandlePlatformMessage(
std::unique_ptr<PlatformMessage> message) = 0;
/// Returns true if the platform message will ALWAYS be be dispatched to the
/// platform thread.
///
/// Platforms thats support Background Platform Channels will return
/// false.
virtual bool DoesHandlePlatformMessageOnPlatformThread() const = 0;
/// Performs the return procedure for an associated call to
/// HandlePlatformMessage.
/// This method should be thread-safe and able to be invoked on any thread.

View File

@@ -1223,7 +1223,20 @@ void Shell::OnEngineHandlePlatformMessage(
}
if (platform_message_handler_) {
if (route_messages_through_platform_thread_) {
if (route_messages_through_platform_thread_ &&
!platform_message_handler_
->DoesHandlePlatformMessageOnPlatformThread()) {
#if _WIN32
// On Windows capturing a TaskRunner with a TaskRunner will cause an
// uncaught exception in process shutdown because of the deletion order of
// global variables. See also
// https://github.com/flutter/flutter/issues/111575.
// This won't be an issue until Windows supports background platform
// channels (https://github.com/flutter/flutter/issues/93945). Then this
// can potentially be addressed by capturing a weak_ptr to an object that
// retains the ui TaskRunner, instead of the TaskRunner directly.
FML_DCHECK(false);
#endif
// We route messages through the platform thread temporarily when the
// shell is being initialized to be backwards compatible with setting
// message handlers in the same event as starting the isolate, but after
@@ -1233,12 +1246,9 @@ void Shell::OnEngineHandlePlatformMessage(
[weak_platform_message_handler =
std::weak_ptr<PlatformMessageHandler>(platform_message_handler_),
message = std::move(message), ui_task_runner]() mutable {
// The static leak checker gets confused by the use of
// fml::MakeCopyable.
// NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
ui_task_runner->PostTask(fml::MakeCopyable(
[weak_platform_message_handler, message = std::move(message),
ui_task_runner]() mutable {
ui_task_runner->PostTask(
fml::MakeCopyable([weak_platform_message_handler,
message = std::move(message)]() mutable {
auto platform_message_handler =
weak_platform_message_handler.lock();
if (platform_message_handler) {

View File

@@ -145,6 +145,7 @@ class MockPlatformMessageHandler : public PlatformMessageHandler {
public:
MOCK_METHOD1(HandlePlatformMessage,
void(std::unique_ptr<PlatformMessage> message));
MOCK_CONST_METHOD0(DoesHandlePlatformMessageOnPlatformThread, bool());
MOCK_METHOD2(InvokePlatformMessageResponseCallback,
void(int response_id, std::unique_ptr<fml::Mapping> mapping));
MOCK_METHOD1(InvokePlatformMessageEmptyResponseCallback,

View File

@@ -20,6 +20,9 @@ class PlatformMessageHandlerAndroid : public PlatformMessageHandler {
explicit PlatformMessageHandlerAndroid(
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade);
void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) override;
bool DoesHandlePlatformMessageOnPlatformThread() const override {
return false;
}
void InvokePlatformMessageResponseCallback(
int response_id,
std::unique_ptr<fml::Mapping> mapping) override;

View File

@@ -25,6 +25,8 @@ class PlatformMessageHandlerIos : public PlatformMessageHandler {
void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) override;
bool DoesHandlePlatformMessageOnPlatformThread() const override { return false; }
void InvokePlatformMessageResponseCallback(int response_id,
std::unique_ptr<fml::Mapping> mapping) override;

View File

@@ -221,6 +221,7 @@ if (enable_unittests) {
include_dirs = [ "." ]
sources = [
"platform_view_embedder_unittests.cc",
"tests/embedder_config_builder.cc",
"tests/embedder_config_builder.h",
"tests/embedder_test.cc",

View File

@@ -4,19 +4,58 @@
#include "flutter/shell/platform/embedder/platform_view_embedder.h"
#include "flutter/fml/make_copyable.h"
namespace flutter {
class PlatformViewEmbedder::EmbedderPlatformMessageHandler
: public PlatformMessageHandler {
public:
EmbedderPlatformMessageHandler(
fml::WeakPtr<PlatformView> parent,
fml::RefPtr<fml::TaskRunner> platform_task_runner)
: parent_(parent), platform_task_runner_(platform_task_runner) {}
virtual void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) {
platform_task_runner_->PostTask(fml::MakeCopyable(
[parent = parent_, message = std::move(message)]() mutable {
if (parent) {
parent->HandlePlatformMessage(std::move(message));
} else {
FML_DLOG(WARNING) << "Deleted engine dropping message on channel "
<< message->channel();
}
}));
}
virtual bool DoesHandlePlatformMessageOnPlatformThread() const {
return true;
}
virtual void InvokePlatformMessageResponseCallback(
int response_id,
std::unique_ptr<fml::Mapping> mapping) {}
virtual void InvokePlatformMessageEmptyResponseCallback(int response_id) {}
private:
fml::WeakPtr<PlatformView> parent_;
fml::RefPtr<fml::TaskRunner> platform_task_runner_;
};
PlatformViewEmbedder::PlatformViewEmbedder(
PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
EmbedderSurfaceSoftware::SoftwareDispatchTable software_dispatch_table,
PlatformDispatchTable platform_dispatch_table,
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
: PlatformView(delegate, std::move(task_runners)),
: PlatformView(delegate, task_runners),
external_view_embedder_(external_view_embedder),
embedder_surface_(
std::make_unique<EmbedderSurfaceSoftware>(software_dispatch_table,
external_view_embedder_)),
platform_message_handler_(new EmbedderPlatformMessageHandler(
GetWeakPtr(),
task_runners.GetPlatformTaskRunner())),
platform_dispatch_table_(platform_dispatch_table) {}
#ifdef SHELL_ENABLE_GL
@@ -27,12 +66,15 @@ PlatformViewEmbedder::PlatformViewEmbedder(
bool fbo_reset_after_present,
PlatformDispatchTable platform_dispatch_table,
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
: PlatformView(delegate, std::move(task_runners)),
: PlatformView(delegate, task_runners),
external_view_embedder_(external_view_embedder),
embedder_surface_(
std::make_unique<EmbedderSurfaceGL>(gl_dispatch_table,
fbo_reset_after_present,
external_view_embedder_)),
platform_message_handler_(new EmbedderPlatformMessageHandler(
GetWeakPtr(),
task_runners.GetPlatformTaskRunner())),
platform_dispatch_table_(platform_dispatch_table) {}
#endif
@@ -43,9 +85,12 @@ PlatformViewEmbedder::PlatformViewEmbedder(
std::unique_ptr<EmbedderSurfaceMetal> embedder_surface,
PlatformDispatchTable platform_dispatch_table,
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
: PlatformView(delegate, std::move(task_runners)),
: PlatformView(delegate, task_runners),
external_view_embedder_(external_view_embedder),
embedder_surface_(std::move(embedder_surface)),
platform_message_handler_(new EmbedderPlatformMessageHandler(
GetWeakPtr(),
task_runners.GetPlatformTaskRunner())),
platform_dispatch_table_(platform_dispatch_table) {}
#endif
@@ -56,9 +101,12 @@ PlatformViewEmbedder::PlatformViewEmbedder(
std::unique_ptr<EmbedderSurfaceVulkan> embedder_surface,
PlatformDispatchTable platform_dispatch_table,
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder)
: PlatformView(delegate, std::move(task_runners)),
: PlatformView(delegate, task_runners),
external_view_embedder_(external_view_embedder),
embedder_surface_(std::move(embedder_surface)),
platform_message_handler_(new EmbedderPlatformMessageHandler(
GetWeakPtr(),
task_runners.GetPlatformTaskRunner())),
platform_dispatch_table_(platform_dispatch_table) {}
#endif
@@ -146,4 +194,9 @@ void PlatformViewEmbedder::OnPreEngineRestart() const {
}
}
std::shared_ptr<PlatformMessageHandler>
PlatformViewEmbedder::GetPlatformMessageHandler() const {
return platform_message_handler_;
}
} // namespace flutter

View File

@@ -100,9 +100,15 @@ class PlatformViewEmbedder final : public PlatformView {
// |PlatformView|
void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) override;
// |PlatformView|
std::shared_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
const override;
private:
class EmbedderPlatformMessageHandler;
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder_;
std::unique_ptr<EmbedderSurface> embedder_surface_;
std::shared_ptr<EmbedderPlatformMessageHandler> platform_message_handler_;
PlatformDispatchTable platform_dispatch_table_;
// |PlatformView|

View File

@@ -0,0 +1,173 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/embedder/platform_view_embedder.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstring>
namespace flutter {
namespace testing {
namespace {
class MockDelegate : public PlatformView::Delegate {
MOCK_METHOD1(OnPlatformViewCreated, void(std::unique_ptr<Surface>));
MOCK_METHOD0(OnPlatformViewDestroyed, void());
MOCK_METHOD0(OnPlatformViewScheduleFrame, void());
MOCK_METHOD1(OnPlatformViewSetNextFrameCallback,
void(const fml::closure& closure));
MOCK_METHOD1(OnPlatformViewSetViewportMetrics,
void(const ViewportMetrics& metrics));
MOCK_METHOD1(OnPlatformViewDispatchPlatformMessage,
void(std::unique_ptr<PlatformMessage> message));
MOCK_METHOD1(OnPlatformViewDispatchPointerDataPacket,
void(std::unique_ptr<PointerDataPacket> packet));
MOCK_METHOD3(OnPlatformViewDispatchSemanticsAction,
void(int32_t id,
SemanticsAction action,
fml::MallocMapping args));
MOCK_METHOD1(OnPlatformViewSetSemanticsEnabled, void(bool enabled));
MOCK_METHOD1(OnPlatformViewSetAccessibilityFeatures, void(int32_t flags));
MOCK_METHOD1(OnPlatformViewRegisterTexture,
void(std::shared_ptr<Texture> texture));
MOCK_METHOD1(OnPlatformViewUnregisterTexture, void(int64_t texture_id));
MOCK_METHOD1(OnPlatformViewMarkTextureFrameAvailable,
void(int64_t texture_id));
MOCK_METHOD3(LoadDartDeferredLibrary,
void(intptr_t loading_unit_id,
std::unique_ptr<const fml::Mapping> snapshot_data,
std::unique_ptr<const fml::Mapping> snapshot_instructions));
MOCK_METHOD3(LoadDartDeferredLibraryError,
void(intptr_t loading_unit_id,
const std::string error_message,
bool transient));
MOCK_METHOD2(UpdateAssetResolverByType,
void(std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type));
MOCK_CONST_METHOD0(OnPlatformViewGetSettings, const Settings&());
};
class MockResponse : public PlatformMessageResponse {
public:
MOCK_METHOD1(Complete, void(std::unique_ptr<fml::Mapping> data));
MOCK_METHOD0(CompleteEmpty, void());
};
} // namespace
TEST(PlatformViewEmbedderTest, HasPlatformMessageHandler) {
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
ThreadHost::Type::Platform);
flutter::TaskRunners task_runners = flutter::TaskRunners(
"HasPlatformMessageHandler", thread_host.platform_thread->GetTaskRunner(),
nullptr, nullptr, nullptr);
fml::AutoResetWaitableEvent latch;
task_runners.GetPlatformTaskRunner()->PostTask([&latch, task_runners] {
MockDelegate delegate;
EmbedderSurfaceSoftware::SoftwareDispatchTable software_dispatch_table;
PlatformViewEmbedder::PlatformDispatchTable platform_dispatch_table;
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder;
auto embedder = std::make_unique<PlatformViewEmbedder>(
delegate, task_runners, software_dispatch_table,
platform_dispatch_table, external_view_embedder);
ASSERT_TRUE(embedder->GetPlatformMessageHandler());
latch.Signal();
});
latch.Wait();
}
TEST(PlatformViewEmbedderTest, Dispatches) {
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
ThreadHost::Type::Platform);
flutter::TaskRunners task_runners = flutter::TaskRunners(
"HasPlatformMessageHandler", thread_host.platform_thread->GetTaskRunner(),
nullptr, nullptr, nullptr);
bool did_call = false;
std::unique_ptr<PlatformViewEmbedder> embedder;
{
fml::AutoResetWaitableEvent latch;
task_runners.GetPlatformTaskRunner()->PostTask([&latch, task_runners,
&did_call, &embedder] {
MockDelegate delegate;
EmbedderSurfaceSoftware::SoftwareDispatchTable software_dispatch_table;
PlatformViewEmbedder::PlatformDispatchTable platform_dispatch_table;
platform_dispatch_table.platform_message_response_callback =
[&did_call](std::unique_ptr<PlatformMessage> message) {
did_call = true;
};
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder;
embedder = std::make_unique<PlatformViewEmbedder>(
delegate, task_runners, software_dispatch_table,
platform_dispatch_table, external_view_embedder);
auto platform_message_handler = embedder->GetPlatformMessageHandler();
fml::RefPtr<PlatformMessageResponse> response =
fml::MakeRefCounted<MockResponse>();
std::unique_ptr<PlatformMessage> message =
std::make_unique<PlatformMessage>("foo", response);
platform_message_handler->HandlePlatformMessage(std::move(message));
latch.Signal();
});
latch.Wait();
}
{
fml::AutoResetWaitableEvent latch;
thread_host.platform_thread->GetTaskRunner()->PostTask([&latch, &embedder] {
embedder.reset();
latch.Signal();
});
latch.Wait();
}
EXPECT_TRUE(did_call);
}
TEST(PlatformViewEmbedderTest, DeletionDisabledDispatch) {
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
ThreadHost::Type::Platform);
flutter::TaskRunners task_runners = flutter::TaskRunners(
"HasPlatformMessageHandler", thread_host.platform_thread->GetTaskRunner(),
nullptr, nullptr, nullptr);
bool did_call = false;
{
fml::AutoResetWaitableEvent latch;
task_runners.GetPlatformTaskRunner()->PostTask([&latch, task_runners,
&did_call] {
MockDelegate delegate;
EmbedderSurfaceSoftware::SoftwareDispatchTable software_dispatch_table;
PlatformViewEmbedder::PlatformDispatchTable platform_dispatch_table;
platform_dispatch_table.platform_message_response_callback =
[&did_call](std::unique_ptr<PlatformMessage> message) {
did_call = true;
};
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder;
auto embedder = std::make_unique<PlatformViewEmbedder>(
delegate, task_runners, software_dispatch_table,
platform_dispatch_table, external_view_embedder);
auto platform_message_handler = embedder->GetPlatformMessageHandler();
fml::RefPtr<PlatformMessageResponse> response =
fml::MakeRefCounted<MockResponse>();
std::unique_ptr<PlatformMessage> message =
std::make_unique<PlatformMessage>("foo", response);
platform_message_handler->HandlePlatformMessage(std::move(message));
embedder.reset();
latch.Signal();
});
latch.Wait();
}
{
fml::AutoResetWaitableEvent latch;
thread_host.platform_thread->GetTaskRunner()->PostTask(
[&latch] { latch.Signal(); });
latch.Wait();
}
EXPECT_FALSE(did_call);
}
} // namespace testing
} // namespace flutter