From 0d0d4db144904440d9c5a31113e9856646057902 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Mon, 20 Nov 2023 07:57:02 -0500 Subject: [PATCH] Replace calls to SkFontMgr::RefDefault (flutter/engine#48179) Skia is removing this API, so clients must track a default FontMgr if they want one. See https://issues.skia.org/issues/305780908 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- engine/src/flutter/impeller/typographer/BUILD.gn | 1 + .../impeller/typographer/typographer_unittests.cc | 3 ++- engine/src/flutter/lib/web_ui/skwasm/fonts.cpp | 9 +++++++-- engine/src/flutter/shell/platform/android/BUILD.gn | 1 + .../src/flutter/shell/platform/android/flutter_main.cc | 4 ++-- .../android/io/flutter/embedding/engine/FlutterJNI.java | 7 ++++--- engine/src/flutter/third_party/txt/src/txt/platform.h | 2 +- 7 files changed, 18 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/impeller/typographer/BUILD.gn b/engine/src/flutter/impeller/typographer/BUILD.gn index 32287b7d52..67b19de6cc 100644 --- a/engine/src/flutter/impeller/typographer/BUILD.gn +++ b/engine/src/flutter/impeller/typographer/BUILD.gn @@ -46,5 +46,6 @@ impeller_component("typographer_unittests") { "../playground:playground_test", "backends/skia:typographer_skia_backend", "backends/stb:typographer_stb_backend", + "//flutter/third_party/txt", ] } diff --git a/engine/src/flutter/impeller/typographer/typographer_unittests.cc b/engine/src/flutter/impeller/typographer/typographer_unittests.cc index 6488783dd4..f8fa84eb8f 100644 --- a/engine/src/flutter/impeller/typographer/typographer_unittests.cc +++ b/engine/src/flutter/impeller/typographer/typographer_unittests.cc @@ -12,6 +12,7 @@ #include "third_party/skia/include/core/SkFontMgr.h" #include "third_party/skia/include/core/SkRect.h" #include "third_party/skia/include/core/SkTextBlob.h" +#include "txt/platform.h" // TODO(zanderso): https://github.com/flutter/flutter/issues/127701 // NOLINTBEGIN(bugprone-unchecked-optional-access) @@ -275,7 +276,7 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecreatedIfTypeChanges) { } TEST_P(TypographerTest, MaybeHasOverlapping) { - sk_sp font_mgr = SkFontMgr::RefDefault(); + sk_sp font_mgr = txt::GetDefaultFontManager(); sk_sp typeface = font_mgr->matchFamilyStyle("Arial", SkFontStyle::Normal()); SkFont sk_font(typeface, 0.5f); diff --git a/engine/src/flutter/lib/web_ui/skwasm/fonts.cpp b/engine/src/flutter/lib/web_ui/skwasm/fonts.cpp index 6209730032..e8e3e4d5ea 100644 --- a/engine/src/flutter/lib/web_ui/skwasm/fonts.cpp +++ b/engine/src/flutter/lib/web_ui/skwasm/fonts.cpp @@ -4,6 +4,7 @@ #include "export.h" #include "third_party/skia/include/core/SkFontMgr.h" +#include "third_party/skia/include/ports/SkFontMgr_empty.h" #include "third_party/skia/modules/skparagraph/include/FontCollection.h" #include "third_party/skia/modules/skparagraph/include/TypefaceFontProvider.h" #include "wrappers.h" @@ -28,9 +29,13 @@ SKWASM_EXPORT void fontCollection_dispose(FlutterFontCollection* collection) { delete collection; } +static sk_sp default_fontmgr() { + static sk_sp mgr = SkFontMgr_New_Custom_Empty(); + return mgr; +} + SKWASM_EXPORT SkTypeface* typeface_create(SkData* fontData) { - auto typeface = - SkFontMgr::RefDefault()->makeFromData(sk_ref_sp(fontData)); + auto typeface = default_fontmgr()->makeFromData(sk_ref_sp(fontData)); return typeface.release(); } diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index 10aa924ccc..37615a587f 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -155,6 +155,7 @@ source_set("flutter_shell_native_src") { "//flutter/shell/platform/android/surface", "//flutter/shell/platform/android/surface:native_window", "//flutter/skia", + "//flutter/third_party/txt", "//flutter/vulkan", ] diff --git a/engine/src/flutter/shell/platform/android/flutter_main.cc b/engine/src/flutter/shell/platform/android/flutter_main.cc index 782b373c21..8b49f7e40b 100644 --- a/engine/src/flutter/shell/platform/android/flutter_main.cc +++ b/engine/src/flutter/shell/platform/android/flutter_main.cc @@ -25,7 +25,7 @@ #include "flutter/shell/common/shell.h" #include "flutter/shell/common/switches.h" #include "third_party/dart/runtime/include/dart_tools_api.h" -#include "third_party/skia/include/core/SkFontMgr.h" +#include "txt/platform.h" namespace flutter { @@ -198,7 +198,7 @@ void FlutterMain::SetupDartVMServiceUriCallback(JNIEnv* env) { static void PrefetchDefaultFontManager(JNIEnv* env, jclass jcaller) { // Initialize a singleton owned by Skia. - SkFontMgr::RefDefault(); + txt::GetDefaultFontManager(); } bool FlutterMain::Register(JNIEnv* env) { 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 1d66eeff9d..006d2d18e1 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 @@ -151,9 +151,10 @@ public class FlutterJNI { private static native void nativePrefetchDefaultFontManager(); /** - * Prefetch the default font manager provided by SkFontMgr::RefDefault() which is a process-wide - * singleton owned by Skia. Note that, the first call to SkFontMgr::RefDefault() will take - * noticeable time, but later calls will return a reference to the preexisting font manager. + * Prefetch the default font manager provided by txt::GetDefaultFontManager() which is a + * process-wide singleton owned by Skia. Note that, the first call to txt::GetDefaultFontManager() + * will take noticeable time, but later calls will return a reference to the preexisting font + * manager. * *

This method should only be called once across all FlutterJNI instances. */ diff --git a/engine/src/flutter/third_party/txt/src/txt/platform.h b/engine/src/flutter/third_party/txt/src/txt/platform.h index 7c29e19001..7ce2dd2e1c 100644 --- a/engine/src/flutter/third_party/txt/src/txt/platform.h +++ b/engine/src/flutter/third_party/txt/src/txt/platform.h @@ -15,7 +15,7 @@ namespace txt { std::vector GetDefaultFontFamilies(); -sk_sp GetDefaultFontManager(uint32_t font_initialization_data); +sk_sp GetDefaultFontManager(uint32_t font_initialization_data = 0); } // namespace txt