summaryrefslogtreecommitdiff
path: root/graphics/java
diff options
context:
space:
mode:
authorSeigo Nonaka <nona@google.com>2021-05-27 15:49:37 -0700
committerSeigo Nonaka <nona@google.com>2021-05-27 15:54:24 -0700
commitd5ecababb4c65db2c7f7fa2d7e5b8ddbbae94cf1 (patch)
tree323a089246f10dc0c335e7105b17ab8750aa9c41 /graphics/java
parent433ba6df077c26ad1aa8455f1f87e7cf1ecda706 (diff)
Calculate set of available fonts in native
The previous attempt[1] of improving SystemFonts API is not good enough for users. To further improve the performance, calculate font uniqueness in native code. From Android S, the system font information is stored in native code and has more information for skipping duplication check. Bug: 188201287 Test: atest FontFamilyUpdateRequestTest Test: atest FontListParserTest Test: atest FontManagerTest Test: atest NativeSystemFontTest Test: atest PersistentSystemFontConfigTest Test: atest SystemFontsTest Test: atest SystemFontsUniqueNameTest Test: atest UpdatableFontDirTest Test: atest UpdatableSystemFontTest Test: minikin_tests Change-Id: Ib375dcda0f278c93ec8dd21636d7a22b4174f214
Diffstat (limited to 'graphics/java')
-rw-r--r--graphics/java/android/graphics/fonts/Font.java18
-rw-r--r--graphics/java/android/graphics/fonts/SystemFonts.java52
2 files changed, 19 insertions, 51 deletions
diff --git a/graphics/java/android/graphics/fonts/Font.java b/graphics/java/android/graphics/fonts/Font.java
index 69cd8bdb3e70..cd7936d50dff 100644
--- a/graphics/java/android/graphics/fonts/Font.java
+++ b/graphics/java/android/graphics/fonts/Font.java
@@ -46,7 +46,10 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.IdentityHashMap;
import java.util.Objects;
+import java.util.Set;
/**
* A font class can be used for creating FontFamily.
@@ -859,6 +862,18 @@ public final class Font {
+ "}";
}
+ /** @hide */
+ public static Set<Font> getAvailableFonts() {
+ // The font uniqueness is already calculated in the native code. So use IdentityHashMap
+ // for avoiding hash/equals calculation.
+ IdentityHashMap<Font, Font> map = new IdentityHashMap<>();
+ for (long nativePtr : nGetAvailableFontSet()) {
+ Font font = new Font(nativePtr);
+ map.put(font, font);
+ }
+ return Collections.unmodifiableSet(map.keySet());
+ }
+
@CriticalNative
private static native long nGetMinikinFontPtr(long font);
@@ -900,4 +915,7 @@ public final class Font {
@CriticalNative
private static native long nGetAxisInfo(long fontPtr, int i);
+
+ @FastNative
+ private static native long[] nGetAvailableFontSet();
}
diff --git a/graphics/java/android/graphics/fonts/SystemFonts.java b/graphics/java/android/graphics/fonts/SystemFonts.java
index 8d69d447f4e0..6278c0e23f27 100644
--- a/graphics/java/android/graphics/fonts/SystemFonts.java
+++ b/graphics/java/android/graphics/fonts/SystemFonts.java
@@ -22,7 +22,6 @@ import android.graphics.FontListParser;
import android.graphics.Typeface;
import android.text.FontConfig;
import android.util.ArrayMap;
-import android.util.ArraySet;
import android.util.Log;
import com.android.internal.annotations.GuardedBy;
@@ -39,7 +38,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
import java.util.Set;
/**
@@ -61,36 +59,6 @@ public final class SystemFonts {
private static @GuardedBy("sLock") Set<Font> sAvailableFonts;
/**
- * Helper wrapper class for skipping buffer equality check of Font#equals.
- *
- * Due to historical reasons, the Font#equals checks the byte-by-byte buffer equality which
- * requires heavy IO work in getAvailableFonts. Since the fonts came from system are all regular
- * file backed font instance and stored in the unique place, just comparing file path should be
- * good enough for this case.
- */
- private static final class SystemFontHashWrapper {
- private final Font mFont;
- SystemFontHashWrapper(Font font) {
- mFont = font;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- // All system fonts are regular-file backed font instance, so no need to
- // compare buffers.
- return mFont.paramEquals(((SystemFontHashWrapper) o).mFont);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(mFont);
- }
- }
-
- /**
* Returns all available font files in the system.
*
* @return a set of system fonts
@@ -98,25 +66,7 @@ public final class SystemFonts {
public static @NonNull Set<Font> getAvailableFonts() {
synchronized (LOCK) {
if (sAvailableFonts == null) {
- Set<SystemFontHashWrapper> set = new ArraySet<>();
- for (Typeface tf : Typeface.getSystemFontMap().values()) {
- List<FontFamily> families = tf.getFallback();
- for (int i = 0; i < families.size(); ++i) {
- FontFamily family = families.get(i);
- for (int j = 0; j < family.getSize(); ++j) {
- set.add(new SystemFontHashWrapper(family.getFont(j)));
- }
- }
- }
-
- // Unwrapping font instance for Set<Font> interface. The ArraySet#add won't call
- // Font#equals function if none of two objects has the same hash, so following
- // unwrapping won't cause bad performance due to byte-by-byte equality check.
- ArraySet<Font> result = new ArraySet(set.size());
- for (SystemFontHashWrapper wrapper : set) {
- result.add(wrapper.mFont);
- }
- sAvailableFonts = Collections.unmodifiableSet(result);
+ sAvailableFonts = Font.getAvailableFonts();
}
return sAvailableFonts;
}