Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 322cd0b

Browse files
committed
Pre-warm default font manager, to reduce time cost of engine setup
1 parent 2999d0c commit 322cd0b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

shell/platform/android/flutter_main.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "flutter/shell/common/shell.h"
2222
#include "flutter/shell/common/switches.h"
2323
#include "third_party/dart/runtime/include/dart_tools_api.h"
24+
#include "third_party/skia/include/core/SkFontMgr.h"
2425

2526
namespace flutter {
2627

@@ -155,6 +156,10 @@ void FlutterMain::SetupObservatoryUriCallback(JNIEnv* env) {
155156
});
156157
}
157158

159+
static void CreateDefaultFontManager(JNIEnv* env, jclass jcaller) {
160+
sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault());
161+
}
162+
158163
bool FlutterMain::Register(JNIEnv* env) {
159164
static const JNINativeMethod methods[] = {
160165
{
@@ -163,6 +168,11 @@ bool FlutterMain::Register(JNIEnv* env) {
163168
"lang/String;Ljava/lang/String;Ljava/lang/String;J)V",
164169
.fnPtr = reinterpret_cast<void*>(&Init),
165170
},
171+
{
172+
.name = "nativeCreateDefaultFontManager",
173+
.signature = "()V",
174+
.fnPtr = reinterpret_cast<void*>(&CreateDefaultFontManager),
175+
},
166176
};
167177

168178
jclass clazz = env->FindClass("io/flutter/embedding/engine/FlutterJNI");

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public static native void nativeInit(
106106
@NonNull String engineCachesPath,
107107
long initTimeMillis);
108108

109+
/**
110+
* Create the default font manager provided by SkFontMgr::RefDefault() which is a process-wide
111+
* singleton owned by Skia. Note that, the first call to SkFontMgr::RefDefault() will take
112+
* noticeable time, but later calls will return a reference to the preexisting font manager.
113+
*/
114+
public static native void nativeCreateDefaultFontManager();
115+
109116
// TODO(mattcarroll): add javadocs
110117
@UiThread
111118
public native boolean nativeGetIsSoftwareRenderingEnabled();

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ public InitResult call() {
144144

145145
System.loadLibrary("flutter");
146146

147+
// Pre-warm the default font manager as soon as possible on a background thread.
148+
// It helps to reduce time cost of engine setup that blocks the platform thread.
149+
new Thread(
150+
new Runnable() {
151+
@Override
152+
public void run() {
153+
FlutterJNI.nativeCreateDefaultFontManager();
154+
}
155+
})
156+
.start();
157+
147158
if (resourceExtractor != null) {
148159
resourceExtractor.waitForCompletion();
149160
}

0 commit comments

Comments
 (0)