Revert "Follow minikin::FontFamily constructor signature change."

This reverts commit a0a4828614edfd633ab86f04408e7f4e55b491dd.

Bug: 34378805
Change-Id: Icbdbfbde2efe8eaebbe4334a679e3b3247057388
(cherry picked from commit c0d685b87aa544bc23c1ed1c01e88f6c04855f72)
diff --git a/core/jni/android/graphics/FontFamily.cpp b/core/jni/android/graphics/FontFamily.cpp
index adee2fd..15e7165 100644
--- a/core/jni/android/graphics/FontFamily.cpp
+++ b/core/jni/android/graphics/FontFamily.cpp
@@ -39,54 +39,27 @@
 
 namespace android {
 
-struct NativeFamilyBuilder {
-    uint32_t langId;
-    int variant;
-    std::vector<minikin::Font> fonts;
-};
-
-static jlong FontFamily_initBuilder(JNIEnv* env, jobject clazz, jstring lang, jint variant) {
-    NativeFamilyBuilder* builder = new NativeFamilyBuilder();
-    if (lang != nullptr) {
-        ScopedUtfChars str(env, lang);
-        builder->langId = minikin::FontStyle::registerLanguageList(str.c_str());
-    } else {
-        builder->langId = minikin::FontStyle::registerLanguageList("");
+static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) {
+    if (lang == NULL) {
+        return (jlong)new minikin::FontFamily(variant);
     }
-    builder->variant = variant;
-    return reinterpret_cast<jlong>(builder);
+    ScopedUtfChars str(env, lang);
+    uint32_t langId = minikin::FontStyle::registerLanguageList(str.c_str());
+    return (jlong)new minikin::FontFamily(langId, variant);
 }
 
-static jlong FontFamily_create(jlong builderPtr) {
-    if (builderPtr == 0) {
-        return 0;
-    }
-    NativeFamilyBuilder* builder = reinterpret_cast<NativeFamilyBuilder*>(builderPtr);
-    minikin::FontFamily* family = new minikin::FontFamily(
-            builder->langId, builder->variant, std::move(builder->fonts));
-    delete builder;
-    return reinterpret_cast<jlong>(family);
-}
-
-static void FontFamily_unref(jlong familyPtr) {
+static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) {
     minikin::FontFamily* fontFamily = reinterpret_cast<minikin::FontFamily*>(familyPtr);
     fontFamily->Unref();
 }
 
-static void addSkTypeface(jlong builderPtr, sk_sp<SkTypeface> face, const void* fontData,
-        size_t fontSize, int ttcIndex) {
+static jboolean addSkTypeface(minikin::FontFamily* family, sk_sp<SkTypeface> face,
+        const void* fontData, size_t fontSize, int ttcIndex) {
     minikin::MinikinFont* minikinFont =
             new MinikinFontSkia(std::move(face), fontData, fontSize, ttcIndex);
-    NativeFamilyBuilder* builder = reinterpret_cast<NativeFamilyBuilder*>(builderPtr);
-    int weight;
-    bool italic;
-    if (!minikin::FontFamily::analyzeStyle(minikinFont, &weight, &italic)) {
-        ALOGE("analyzeStyle failed. Using default style");
-        weight = 400;
-        italic = false;
-    }
-    builder->fonts.push_back(minikin::Font(minikinFont, minikin::FontStyle(weight / 100, italic)));
+    bool result = family->addFont(minikinFont);
     minikinFont->Unref();
+    return result;
 }
 
 static void release_global_ref(const void* /*data*/, void* context) {
@@ -112,7 +85,7 @@
     }
 }
 
-static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong builderPtr, jobject bytebuf,
+static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jobject bytebuf,
         jint ttcIndex) {
     NPE_CHECK_RETURN_ZERO(env, bytebuf);
     const void* fontPtr = env->GetDirectBufferAddress(bytebuf);
@@ -139,8 +112,8 @@
         ALOGE("addFont failed to create font");
         return false;
     }
-    addSkTypeface(builderPtr, std::move(face), fontPtr, (size_t)fontSize, ttcIndex);
-    return true;
+    minikin::FontFamily* fontFamily = reinterpret_cast<minikin::FontFamily*>(familyPtr);
+    return addSkTypeface(fontFamily, std::move(face), fontPtr, (size_t)fontSize, ttcIndex);
 }
 
 static struct {
@@ -153,7 +126,7 @@
     jfieldID mStyleValue;
 } gAxisClassInfo;
 
-static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong builderPtr,
+static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong familyPtr,
         jobject font, jint ttcIndex, jobject listOfAxis, jint weight, jboolean isItalic) {
     NPE_CHECK_RETURN_ZERO(env, font);
 
@@ -205,11 +178,10 @@
         ALOGE("addFont failed to create font, invalid request");
         return false;
     }
+    minikin::FontFamily* fontFamily = reinterpret_cast<minikin::FontFamily*>(familyPtr);
     minikin::MinikinFont* minikinFont =
-            new MinikinFontSkia(std::move(face), fontPtr, fontSize, ttcIndex);
-    NativeFamilyBuilder* builder = reinterpret_cast<NativeFamilyBuilder*>(builderPtr);
-    builder->fonts.push_back(minikin::Font(minikinFont,
-            minikin::FontStyle(weight / 100, isItalic)));
+            new MinikinFontSkia(std::move(face), fontPtr, (size_t)fontSize, ttcIndex);
+    fontFamily->addFont(minikinFont, minikin::FontStyle(weight / 100, isItalic));
     minikinFont->Unref();
     return true;
 }
@@ -218,7 +190,7 @@
     delete static_cast<Asset*>(context);
 }
 
-static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong builderPtr,
+static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr,
         jobject jassetMgr, jstring jpath) {
     NPE_CHECK_RETURN_ZERO(env, jassetMgr);
     NPE_CHECK_RETURN_ZERO(env, jpath);
@@ -250,16 +222,14 @@
         ALOGE("addFontFromAsset failed to create font %s", str.c_str());
         return false;
     }
-
-    addSkTypeface(builderPtr, std::move(face), buf, bufSize, 0 /* ttc index */);
-    return true;
+    minikin::FontFamily* fontFamily = reinterpret_cast<minikin::FontFamily*>(familyPtr);
+    return addSkTypeface(fontFamily, std::move(face), buf, bufSize, /* ttcIndex */ 0);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
 static const JNINativeMethod gFontFamilyMethods[] = {
-    { "nInitBuilder",          "(Ljava/lang/String;I)J", (void*)FontFamily_initBuilder },
-    { "nCreateFamily",         "(J)J", (void*)FontFamily_create },
+    { "nCreateFamily",         "(Ljava/lang/String;I)J", (void*)FontFamily_create },
     { "nUnrefFamily",          "(J)V", (void*)FontFamily_unref },
     { "nAddFont",              "(JLjava/nio/ByteBuffer;I)Z", (void*)FontFamily_addFont },
     { "nAddFontWeightStyle",   "(JLjava/nio/ByteBuffer;ILjava/util/List;IZ)Z",
diff --git a/graphics/java/android/graphics/FontFamily.java b/graphics/java/android/graphics/FontFamily.java
index 2733c43f..e48bf79 100644
--- a/graphics/java/android/graphics/FontFamily.java
+++ b/graphics/java/android/graphics/FontFamily.java
@@ -18,7 +18,6 @@
 
 import android.content.res.AssetManager;
 import android.util.Log;
-import dalvik.annotation.optimization.CriticalNative;
 
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -40,11 +39,11 @@
      */
     public long mNativePtr;
 
-    // Points native font family builder. Must be zero after freezing this family.
-    private long mBuilderPtr;
-
     public FontFamily() {
-        mBuilderPtr = nInitBuilder(null, 0);
+        mNativePtr = nCreateFamily(null, 0);
+        if (mNativePtr == 0) {
+            throw new IllegalStateException("error creating native FontFamily");
+        }
     }
 
     public FontFamily(String lang, String variant) {
@@ -54,15 +53,10 @@
         } else if ("elegant".equals(variant)) {
             varEnum = 2;
         }
-        mBuilderPtr = nInitBuilder(lang, varEnum);
-    }
-
-    public void freeze() {
-        if (mBuilderPtr == 0) {
-            throw new IllegalStateException("This FontFamily is already frozen");
+        mNativePtr = nCreateFamily(lang, varEnum);
+        if (mNativePtr == 0) {
+            throw new IllegalStateException("error creating native FontFamily");
         }
-        mNativePtr = nCreateFamily(mBuilderPtr);
-        mBuilderPtr = 0;
     }
 
     @Override
@@ -75,14 +69,11 @@
     }
 
     public boolean addFont(String path, int ttcIndex) {
-        if (mBuilderPtr == 0) {
-            throw new IllegalStateException("Unable to call addFont after freezing.");
-        }
         try (FileInputStream file = new FileInputStream(path)) {
             FileChannel fileChannel = file.getChannel();
             long fontSize = fileChannel.size();
             ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
-            return nAddFont(mBuilderPtr, fontBuffer, ttcIndex);
+            return nAddFont(mNativePtr, fontBuffer, ttcIndex);
         } catch (IOException e) {
             Log.e(TAG, "Error mapping font file " + path);
             return false;
@@ -91,29 +82,19 @@
 
     public boolean addFontWeightStyle(ByteBuffer font, int ttcIndex, List<FontListParser.Axis> axes,
             int weight, boolean style) {
-        if (mBuilderPtr == 0) {
-            throw new IllegalStateException("Unable to call addFontWeightStyle after freezing.");
-        }
-        return nAddFontWeightStyle(mBuilderPtr, font, ttcIndex, axes, weight, style);
+        return nAddFontWeightStyle(mNativePtr, font, ttcIndex, axes, weight, style);
     }
 
     public boolean addFontFromAsset(AssetManager mgr, String path) {
-        if (mBuilderPtr == 0) {
-            throw new IllegalStateException("Unable to call addFontFromAsset after freezing.");
-        }
-        return nAddFontFromAsset(mBuilderPtr, mgr, path);
+        return nAddFontFromAsset(mNativePtr, mgr, path);
     }
 
-    private static native long nInitBuilder(String lang, int variant);
-
-    @CriticalNative
-    private static native long nCreateFamily(long mBuilderPtr);
-
-    @CriticalNative
+    private static native long nCreateFamily(String lang, int variant);
     private static native void nUnrefFamily(long nativePtr);
-    private static native boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex);
-    private static native boolean nAddFontWeightStyle(long builderPtr, ByteBuffer font,
+    private static native boolean nAddFont(long nativeFamily, ByteBuffer font, int ttcIndex);
+    private static native boolean nAddFontWeightStyle(long nativeFamily, ByteBuffer font,
             int ttcIndex, List<FontListParser.Axis> listOfAxis,
             int weight, boolean isItalic);
-    private static native boolean nAddFontFromAsset(long builderPtr, AssetManager mgr, String path);
+    private static native boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr,
+            String path);
 }
diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java
index a8c1690..2886f0d 100644
--- a/graphics/java/android/graphics/Typeface.java
+++ b/graphics/java/android/graphics/Typeface.java
@@ -196,7 +196,6 @@
 
                 FontFamily fontFamily = new FontFamily();
                 if (fontFamily.addFontFromAsset(mgr, path)) {
-                    fontFamily.freeze();
                     FontFamily[] families = { fontFamily };
                     typeface = createFromFamiliesWithDefault(families);
                     sDynamicTypefaceCache.put(key, typeface);
@@ -246,7 +245,6 @@
         if (sFallbackFonts != null) {
             FontFamily fontFamily = new FontFamily();
             if (fontFamily.addFont(path, 0 /* ttcIndex */)) {
-                fontFamily.freeze();
                 FontFamily[] families = { fontFamily };
                 return createFromFamiliesWithDefault(families);
             }
@@ -317,7 +315,6 @@
                 Log.e(TAG, "Error creating font " + font.fontName + "#" + font.ttcIndex);
             }
         }
-        fontFamily.freeze();
         return fontFamily;
     }
 
diff --git a/libs/hwui/hwui/Typeface.cpp b/libs/hwui/hwui/Typeface.cpp
index 9041b44..ca43156 100644
--- a/libs/hwui/hwui/Typeface.cpp
+++ b/libs/hwui/hwui/Typeface.cpp
@@ -130,9 +130,9 @@
     sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(fontData.release());
     LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);
 
+    minikin::FontFamily* family = new minikin::FontFamily();
     minikin::MinikinFont* font = new MinikinFontSkia(std::move(typeface), data, st.st_size, 0);
-    minikin::FontFamily* family = new minikin::FontFamily(
-                 std::vector<minikin::Font>({ minikin::Font(font, minikin::FontStyle()) }));
+    family->addFont(font);
     font->Unref();
 
     std::vector<minikin::FontFamily*> typefaces = { family };