share font collections between spawn engines (flutter/engine#23603)

This commit is contained in:
gaaclarke 2021-01-13 11:38:48 -08:00 committed by GitHub
parent de41f46ae2
commit 8af69442c0
5 changed files with 47 additions and 7 deletions

View File

@ -560,7 +560,7 @@ class RuntimeController : public PlatformConfigurationClient {
void RequestDartDeferredLibrary(intptr_t loading_unit_id) override;
const fml::WeakPtr<IOManager>& GetIOManager() const { return io_manager_; }
DartVM* GetDartVM() const { return vm_; }
virtual DartVM* GetDartVM() const { return vm_; }
const fml::RefPtr<const DartSnapshot>& GetIsolateSnapshot() const {
return isolate_snapshot_;

View File

@ -256,6 +256,7 @@ if (enable_unittests) {
"//flutter/common/graphics",
"//flutter/shell/profiling:profiling_unittests",
"//flutter/shell/version",
"//flutter/testing:fixture_test",
"//third_party/googletest:gmock",
]

View File

@ -43,6 +43,7 @@ Engine::Engine(
Settings settings,
std::unique_ptr<Animator> animator,
fml::WeakPtr<IOManager> io_manager,
const std::shared_ptr<FontCollection>& font_collection,
std::unique_ptr<RuntimeController> runtime_controller)
: delegate_(delegate),
settings_(std::move(settings)),
@ -50,6 +51,7 @@ Engine::Engine(
runtime_controller_(std::move(runtime_controller)),
activity_running_(true),
have_surface_(false),
font_collection_(font_collection),
image_decoder_(task_runners, image_decoder_task_runner, io_manager),
task_runners_(std::move(task_runners)),
weak_factory_(this) {
@ -75,6 +77,7 @@ Engine::Engine(Delegate& delegate,
settings,
std::move(animator),
io_manager,
std::make_shared<FontCollection>(),
nullptr) {
runtime_controller_ = std::make_unique<RuntimeController>(
*this, // runtime delegate
@ -111,6 +114,7 @@ std::unique_ptr<Engine> Engine::Spawn(
/*settings=*/settings,
/*animator=*/std::move(animator),
/*io_manager=*/runtime_controller_->GetIOManager(),
/*font_collection=*/font_collection_,
/*runtime_controller=*/nullptr);
result->runtime_controller_ = runtime_controller_->Spawn(
*result, // runtime delegate
@ -132,7 +136,7 @@ fml::WeakPtr<Engine> Engine::GetWeakPtr() const {
void Engine::SetupDefaultFontManager() {
TRACE_EVENT0("flutter", "Engine::SetupDefaultFontManager");
font_collection_.SetupDefaultFontManager();
font_collection_->SetupDefaultFontManager();
}
std::shared_ptr<AssetManager> Engine::GetAssetManager() {
@ -152,10 +156,10 @@ bool Engine::UpdateAssetManager(
}
// Using libTXT as the text engine.
font_collection_.RegisterFonts(asset_manager_);
font_collection_->RegisterFonts(asset_manager_);
if (settings_.use_test_fonts) {
font_collection_.RegisterTestFonts();
font_collection_->RegisterTestFonts();
}
return true;
@ -492,7 +496,7 @@ void Engine::SetNeedsReportTimings(bool needs_reporting) {
}
FontCollection& Engine::GetFontCollection() {
return font_collection_;
return *font_collection_;
}
void Engine::DoDispatchPacket(std::unique_ptr<PointerDataPacket> packet,

View File

@ -297,6 +297,7 @@ class Engine final : public RuntimeDelegate,
Settings settings,
std::unique_ptr<Animator> animator,
fml::WeakPtr<IOManager> io_manager,
const std::shared_ptr<FontCollection>& font_collection,
std::unique_ptr<RuntimeController> runtime_controller);
//----------------------------------------------------------------------------
@ -888,7 +889,7 @@ class Engine final : public RuntimeDelegate,
std::shared_ptr<AssetManager> asset_manager_;
bool activity_running_;
bool have_surface_;
FontCollection font_collection_;
std::shared_ptr<FontCollection> font_collection_;
ImageDecoder image_decoder_;
TaskRunners task_runners_;
size_t hint_freed_bytes_since_last_idle_ = 0;

View File

@ -8,6 +8,7 @@
#include "flutter/runtime/dart_vm_lifecycle.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/testing/fixture_test.h"
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "rapidjson/document.h"
@ -67,6 +68,7 @@ class MockRuntimeController : public RuntimeController {
MOCK_METHOD1(DispatchPlatformMessage, bool(fml::RefPtr<PlatformMessage>));
MOCK_METHOD3(LoadDartDeferredLibraryError,
void(intptr_t, const std::string, bool));
MOCK_CONST_METHOD0(GetDartVM, DartVM*());
};
fml::RefPtr<PlatformMessage> MakePlatformMessage(
@ -95,7 +97,7 @@ fml::RefPtr<PlatformMessage> MakePlatformMessage(
return message;
}
class EngineTest : public ::testing::Test {
class EngineTest : public testing::FixtureTest {
public:
EngineTest()
: thread_host_("EngineTest",
@ -120,6 +122,7 @@ class EngineTest : public ::testing::Test {
protected:
void SetUp() override {
settings_ = CreateSettingsForFixture();
dispatcher_maker_ = [](PointerDataDispatcher::Delegate&) {
return nullptr;
};
@ -147,6 +150,7 @@ TEST_F(EngineTest, Create) {
/*settings=*/settings_,
/*animator=*/std::move(animator_),
/*io_manager=*/io_manager_,
/*font_collection=*/std::make_shared<FontCollection>(),
/*runtime_controller=*/std::move(runtime_controller_));
EXPECT_TRUE(engine);
});
@ -167,6 +171,7 @@ TEST_F(EngineTest, DispatchPlatformMessageUnknown) {
/*settings=*/settings_,
/*animator=*/std::move(animator_),
/*io_manager=*/io_manager_,
/*font_collection=*/std::make_shared<FontCollection>(),
/*runtime_controller=*/std::move(mock_runtime_controller));
fml::RefPtr<PlatformMessageResponse> response =
@ -192,6 +197,7 @@ TEST_F(EngineTest, DispatchPlatformMessageInitialRoute) {
/*settings=*/settings_,
/*animator=*/std::move(animator_),
/*io_manager=*/io_manager_,
/*font_collection=*/std::make_shared<FontCollection>(),
/*runtime_controller=*/std::move(mock_runtime_controller));
fml::RefPtr<PlatformMessageResponse> response =
@ -224,6 +230,7 @@ TEST_F(EngineTest, DispatchPlatformMessageInitialRouteIgnored) {
/*settings=*/settings_,
/*animator=*/std::move(animator_),
/*io_manager=*/io_manager_,
/*font_collection=*/std::make_shared<FontCollection>(),
/*runtime_controller=*/std::move(mock_runtime_controller));
fml::RefPtr<PlatformMessageResponse> response =
@ -239,6 +246,32 @@ TEST_F(EngineTest, DispatchPlatformMessageInitialRouteIgnored) {
});
}
TEST_F(EngineTest, SpawnSharesFontLibrary) {
PostUITaskSync([this] {
MockRuntimeDelegate client;
auto mock_runtime_controller =
std::make_unique<MockRuntimeController>(client, task_runners_);
auto vm_ref = DartVMRef::Create(settings_);
EXPECT_CALL(*mock_runtime_controller, GetDartVM())
.WillRepeatedly(::testing::Return(vm_ref.get()));
auto engine = std::make_unique<Engine>(
/*delegate=*/delegate_,
/*dispatcher_maker=*/dispatcher_maker_,
/*image_decoder_task_runner=*/image_decoder_task_runner_,
/*task_runners=*/task_runners_,
/*settings=*/settings_,
/*animator=*/std::move(animator_),
/*io_manager=*/io_manager_,
/*font_collection=*/std::make_shared<FontCollection>(),
/*runtime_controller=*/std::move(mock_runtime_controller));
auto spawn =
engine->Spawn(delegate_, dispatcher_maker_, settings_, nullptr);
EXPECT_TRUE(spawn != nullptr);
EXPECT_EQ(&engine->GetFontCollection(), &spawn->GetFontCollection());
});
}
TEST_F(EngineTest, PassesLoadDartDeferredLibraryErrorToRuntime) {
PostUITaskSync([this] {
intptr_t error_id = 123;
@ -259,6 +292,7 @@ TEST_F(EngineTest, PassesLoadDartDeferredLibraryErrorToRuntime) {
/*settings=*/settings_,
/*animator=*/std::move(animator_),
/*io_manager=*/io_manager_,
/*font_collection=*/std::make_shared<FontCollection>(),
/*runtime_controller=*/std::move(mock_runtime_controller));
engine->LoadDartDeferredLibraryError(error_id, error_message, true);