Skip to content

Commit

Permalink
Pre-warm default font manager, to reduce time cost of engine setup
Browse files Browse the repository at this point in the history
  • Loading branch information
scutlight committed May 18, 2020
1 parent b776e21 commit 0d297ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions shell/platform/android/flutter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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"

namespace flutter {

Expand Down Expand Up @@ -155,6 +156,10 @@ void FlutterMain::SetupObservatoryUriCallback(JNIEnv* env) {
});
}

static void CreateDefaultFontManager(JNIEnv* env, jclass jcaller) {
sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault());
}

bool FlutterMain::Register(JNIEnv* env) {
static const JNINativeMethod methods[] = {
{
Expand All @@ -163,6 +168,11 @@ bool FlutterMain::Register(JNIEnv* env) {
"lang/String;Ljava/lang/String;Ljava/lang/String;J)V",
.fnPtr = reinterpret_cast<void*>(&Init),
},
{
.name = "nativeCreateDefaultFontManager",
.signature = "()V",
.fnPtr = reinterpret_cast<void*>(&CreateDefaultFontManager),
},
};

jclass clazz = env->FindClass("io/flutter/embedding/engine/FlutterJNI");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public static native void nativeInit(
@NonNull String engineCachesPath,
long initTimeMillis);

/**
* Create 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.
*/
public static native void nativeCreateDefaultFontManager();

// TODO(mattcarroll): add javadocs
@UiThread
public native boolean nativeGetIsSoftwareRenderingEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public void startInitialization(@NonNull Context applicationContext, @NonNull Se

System.loadLibrary("flutter");

// Pre-warm the default font manager as soon as possible on a background thread.
// It helps to reduce time cost of engine setup that blocks the platform thread.
new Thread(
new Runnable() {
@Override
public void run() {
FlutterJNI.nativeCreateDefaultFontManager();
}
})
.start();

VsyncWaiter.getInstance(
(WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE))
.init();
Expand Down

0 comments on commit 0d297ba

Please sign in to comment.