Skip to content

Commit

Permalink
Maintain a FontCollection for each engine instance instead of a proce…
Browse files Browse the repository at this point in the history
…ss-wide singleton (flutter#5521)
  • Loading branch information
jason-simmons authored Jun 13, 2018
1 parent 2983e5d commit efd6663
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ source_set("ui") {
"$flutter_root/fml",
"$flutter_root/glue",
"$flutter_root/runtime:test_font",
"$flutter_root/third_party/txt",
"//third_party/dart/runtime/bin:embedded_dart_io",
"//third_party/rapidjson",
"//third_party/skia",
Expand All @@ -107,4 +106,6 @@ source_set("ui") {
if (is_fuchsia) {
deps += [ "//topaz/public/dart-pkg/zircon" ]
}

public_deps = ["$flutter_root/third_party/txt"]
}
7 changes: 0 additions & 7 deletions lib/ui/text/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

namespace blink {

FontCollection& FontCollection::ForProcess() {
static std::once_flag once = {};
static FontCollection* gCollection = nullptr;
std::call_once(once, []() { gCollection = new FontCollection(); });
return *gCollection;
}

FontCollection::FontCollection()
: collection_(std::make_shared<txt::FontCollection>()) {
collection_->SetDefaultFontManager(SkFontMgr::RefDefault());
Expand Down
8 changes: 3 additions & 5 deletions lib/ui/text/font_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace blink {

class FontCollection {
public:
static FontCollection& ForProcess();
FontCollection();

~FontCollection();

std::shared_ptr<txt::FontCollection> GetFontCollection() const;

Expand All @@ -28,10 +30,6 @@ class FontCollection {
private:
std::shared_ptr<txt::FontCollection> collection_;

FontCollection();

~FontCollection();

FXL_DISALLOW_COPY_AND_ASSIGN(FontCollection);
};

Expand Down
12 changes: 7 additions & 5 deletions lib/ui/text/paragraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "flutter/common/task_runners.h"
#include "flutter/lib/ui/text/font_collection.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "flutter/lib/ui/window/window.h"
#include "flutter/third_party/txt/src/txt/font_style.h"
#include "flutter/third_party/txt/src/txt/font_weight.h"
#include "flutter/third_party/txt/src/txt/paragraph_style.h"
Expand Down Expand Up @@ -110,8 +111,8 @@ fxl::RefPtr<ParagraphBuilder> ParagraphBuilder::create(
double lineHeight,
const std::u16string& ellipsis,
const std::string& locale) {
return fxl::MakeRefCounted<ParagraphBuilder>(
encoded, fontFamily, fontSize, lineHeight, ellipsis, locale);
return fxl::MakeRefCounted<ParagraphBuilder>(encoded, fontFamily, fontSize,
lineHeight, ellipsis, locale);
}

ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded,
Expand Down Expand Up @@ -155,8 +156,10 @@ ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded,
style.locale = locale;
}

FontCollection& font_collection =
UIDartState::Current()->window()->client()->GetFontCollection();
m_paragraphBuilder = std::make_unique<txt::ParagraphBuilder>(
style, blink::FontCollection::ForProcess().GetFontCollection());
style, font_collection.GetFontCollection());
} // namespace blink

ParagraphBuilder::~ParagraphBuilder() = default;
Expand Down Expand Up @@ -207,8 +210,7 @@ void ParagraphBuilder::pushStyle(tonic::Int32List& encoded,
static_cast<txt::FontWeight>(encoded[tsFontWeightIndex]);

if (mask & tsFontStyleMask)
style.font_style =
static_cast<txt::FontStyle>(encoded[tsFontStyleIndex]);
style.font_style = static_cast<txt::FontStyle>(encoded[tsFontStyleIndex]);

if (mask & tsFontFamilyMask)
style.font_family = fontFamily;
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DartLibraryNatives;
} // namespace tonic

namespace blink {
class FontCollection;
class Scene;

Dart_Handle ToByteData(const std::vector<uint8_t>& buffer);
Expand All @@ -31,6 +32,7 @@ class WindowClient {
virtual void Render(Scene* scene) = 0;
virtual void UpdateSemantics(SemanticsUpdate* update) = 0;
virtual void HandlePlatformMessage(fxl::RefPtr<PlatformMessage> message) = 0;
virtual FontCollection& GetFontCollection() = 0;

protected:
virtual ~WindowClient();
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ void RuntimeController::HandlePlatformMessage(
client_.HandlePlatformMessage(std::move(message));
}

FontCollection& RuntimeController::GetFontCollection() {
return client_.GetFontCollection();
}

Dart_Port RuntimeController::GetMainPort() {
return root_isolate_ ? root_isolate_->main_port() : ILLEGAL_PORT;
}
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "flutter/common/task_runners.h"
#include "flutter/flow/layers/layer_tree.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "flutter/lib/ui/text/font_collection.h"
#include "flutter/lib/ui/window/pointer_data_packet.h"
#include "flutter/lib/ui/window/window.h"
#include "flutter/runtime/dart_vm.h"
Expand Down Expand Up @@ -124,6 +125,9 @@ class RuntimeController final : public WindowClient {
// |blink::WindowClient|
void HandlePlatformMessage(fxl::RefPtr<PlatformMessage> message) override;

// |blink::WindowClient|
FontCollection& GetFontCollection() override;

FXL_DISALLOW_COPY_AND_ASSIGN(RuntimeController);
};

Expand Down
3 changes: 3 additions & 0 deletions runtime/runtime_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "flutter/flow/layers/layer_tree.h"
#include "flutter/lib/ui/semantics/semantics_node.h"
#include "flutter/lib/ui/text/font_collection.h"
#include "flutter/lib/ui/window/platform_message.h"
#include "third_party/dart/runtime/include/dart_api.h"

Expand All @@ -27,6 +28,8 @@ class RuntimeDelegate {

virtual void HandlePlatformMessage(fxl::RefPtr<PlatformMessage> message) = 0;

virtual FontCollection& GetFontCollection() = 0;

protected:
virtual ~RuntimeDelegate();
};
Expand Down
2 changes: 1 addition & 1 deletion shell/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ source_set("common") {
"$flutter_root/lib/ui",
"$flutter_root/runtime",
"$flutter_root/synchronization",
"$flutter_root/third_party/txt",
"//garnet/public/lib/fxl",
"//third_party/dart/runtime:dart_api",
"//third_party/rapidjson",
Expand All @@ -109,6 +108,7 @@ source_set("common") {
]

public_deps = [
"$flutter_root/third_party/txt",
"//topaz/lib/tonic",
]

Expand Down
8 changes: 6 additions & 2 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ bool Engine::UpdateAssetManager(

// Using libTXT as the text engine.
if (settings_.use_test_fonts) {
blink::FontCollection::ForProcess().RegisterTestFonts();
font_collection_.RegisterTestFonts();
} else {
blink::FontCollection::ForProcess().RegisterFonts(*asset_manager_.get());
font_collection_.RegisterFonts(*asset_manager_.get());
}

return true;
Expand Down Expand Up @@ -377,6 +377,10 @@ void Engine::HandlePlatformMessage(
}
}

blink::FontCollection& Engine::GetFontCollection() {
return font_collection_;
}

void Engine::HandleAssetPlatformMessage(
fxl::RefPtr<blink::PlatformMessage> message) {
fxl::RefPtr<blink::PlatformMessageResponse> response = message->response();
Expand Down
5 changes: 5 additions & 0 deletions shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "flutter/assets/asset_manager.h"
#include "flutter/common/task_runners.h"
#include "flutter/lib/ui/semantics/semantics_node.h"
#include "flutter/lib/ui/text/font_collection.h"
#include "flutter/lib/ui/window/platform_message.h"
#include "flutter/lib/ui/window/viewport_metrics.h"
#include "flutter/runtime/dart_vm.h"
Expand Down Expand Up @@ -109,6 +110,7 @@ class Engine final : public blink::RuntimeDelegate {
fml::RefPtr<blink::AssetManager> asset_manager_;
bool activity_running_;
bool have_surface_;
blink::FontCollection font_collection_;
fml::WeakPtrFactory<Engine> weak_factory_;

// |blink::RuntimeDelegate|
Expand All @@ -124,6 +126,9 @@ class Engine final : public blink::RuntimeDelegate {
void HandlePlatformMessage(
fxl::RefPtr<blink::PlatformMessage> message) override;

// |blink::RuntimeDelegate|
blink::FontCollection& GetFontCollection() override;

void StopAnimator();

void StartAnimatorIfPossible();
Expand Down

0 comments on commit efd6663

Please sign in to comment.