Merge "Do not hold unnecessary map reference" into sc-dev am: b755757c2f

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13432327

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I975c3272baddb06cff9145bdabe28eec65ff0dbd
This commit is contained in:
Seigo Nonaka
2021-01-29 05:21:11 +00:00
committed by Automerger Merge Worker
2 changed files with 46 additions and 71 deletions

View File

@@ -63,7 +63,7 @@ public final class FontManagerService extends IFontManager.Stub {
@Override @Override
public FontConfig getFontConfig() throws RemoteException { public FontConfig getFontConfig() throws RemoteException {
return getCurrentFontSettings().getSystemFontConfig(); return getSystemFontConfig();
} }
/* package */ static class SystemFontException extends AndroidException { /* package */ static class SystemFontException extends AndroidException {
@@ -103,7 +103,7 @@ public final class FontManagerService extends IFontManager.Stub {
if (!Typeface.ENABLE_LAZY_TYPEFACE_INITIALIZATION) { if (!Typeface.ENABLE_LAZY_TYPEFACE_INITIALIZATION) {
return null; return null;
} }
return mService.getCurrentFontSettings().getSerializedSystemFontMap(); return mService.getCurrentFontMap();
} }
}); });
publishBinderService(Context.FONT_SERVICE, mService); publishBinderService(Context.FONT_SERVICE, mService);
@@ -162,7 +162,7 @@ public final class FontManagerService extends IFontManager.Stub {
@GuardedBy("FontManagerService.this") @GuardedBy("FontManagerService.this")
@Nullable @Nullable
private SystemFontSettings mCurrentFontSettings = null; private SharedMemory mSerializedFontMap = null;
private FontManagerService(Context context) { private FontManagerService(Context context) {
mContext = context; mContext = context;
@@ -188,12 +188,12 @@ public final class FontManagerService extends IFontManager.Stub {
return mContext; return mContext;
} }
@NonNull /* package */ SystemFontSettings getCurrentFontSettings() { @NonNull /* package */ SharedMemory getCurrentFontMap() {
synchronized (FontManagerService.this) { synchronized (FontManagerService.this) {
if (mCurrentFontSettings == null) { if (mSerializedFontMap == null) {
mCurrentFontSettings = SystemFontSettings.create(mUpdatableFontDir); mSerializedFontMap = buildNewSerializedFontMap();
} }
return mCurrentFontSettings; return mSerializedFontMap;
} }
} }
@@ -207,7 +207,7 @@ public final class FontManagerService extends IFontManager.Stub {
synchronized (FontManagerService.this) { synchronized (FontManagerService.this) {
mUpdatableFontDir.installFontFile(fd, pkcs7Signature); mUpdatableFontDir.installFontFile(fd, pkcs7Signature);
// Create updated font map in the next getSerializedSystemFontMap() call. // Create updated font map in the next getSerializedSystemFontMap() call.
mCurrentFontSettings = null; mSerializedFontMap = null;
} }
} }
@@ -245,69 +245,44 @@ public final class FontManagerService extends IFontManager.Stub {
new FontManagerShellCommand(this).exec(this, in, out, err, args, callback, result); new FontManagerShellCommand(this).exec(this, in, out, err, args, callback, result);
} }
/* package */ static class SystemFontSettings { /**
private final @NonNull SharedMemory mSerializedSystemFontMap; * Returns an active system font configuration.
private final @NonNull FontConfig mSystemFontConfig; */
private final @NonNull Map<String, FontFamily[]> mSystemFallbackMap; public @NonNull FontConfig getSystemFontConfig() {
private final @NonNull Map<String, Typeface> mSystemTypefaceMap; if (mUpdatableFontDir != null) {
return mUpdatableFontDir.getSystemFontConfig();
} else {
return SystemFonts.getSystemPreinstalledFontConfig();
}
}
SystemFontSettings( /**
@NonNull SharedMemory serializedSystemFontMap, * Make new serialized font map data.
@NonNull FontConfig systemFontConfig, */
@NonNull Map<String, FontFamily[]> systemFallbackMap, public @Nullable SharedMemory buildNewSerializedFontMap() {
@NonNull Map<String, Typeface> systemTypefaceMap) { try {
mSerializedSystemFontMap = serializedSystemFontMap; final FontConfig fontConfig = getSystemFontConfig();
mSystemFontConfig = systemFontConfig; final Map<String, FontFamily[]> fallback = SystemFonts.buildSystemFallback(fontConfig);
mSystemFallbackMap = systemFallbackMap; final Map<String, Typeface> typefaceMap =
mSystemTypefaceMap = systemTypefaceMap; SystemFonts.buildSystemTypefaces(fontConfig, fallback);
return Typeface.serializeFontMap(typefaceMap);
} catch (IOException | ErrnoException e) {
Slog.w(TAG, "Failed to serialize updatable font map. "
+ "Retrying with system image fonts.", e);
} }
public @NonNull SharedMemory getSerializedSystemFontMap() { try {
return mSerializedSystemFontMap;
}
public @NonNull FontConfig getSystemFontConfig() {
return mSystemFontConfig;
}
public @NonNull Map<String, FontFamily[]> getSystemFallbackMap() {
return mSystemFallbackMap;
}
public @NonNull Map<String, Typeface> getSystemTypefaceMap() {
return mSystemTypefaceMap;
}
public static @Nullable SystemFontSettings create(
@Nullable UpdatableFontDir updatableFontDir) {
if (updatableFontDir != null) {
final FontConfig fontConfig = updatableFontDir.getSystemFontConfig();
final Map<String, FontFamily[]> fallback =
SystemFonts.buildSystemFallback(fontConfig);
final Map<String, Typeface> typefaceMap =
SystemFonts.buildSystemTypefaces(fontConfig, fallback);
try {
final SharedMemory shm = Typeface.serializeFontMap(typefaceMap);
return new SystemFontSettings(shm, fontConfig, fallback, typefaceMap);
} catch (IOException | ErrnoException e) {
Slog.w(TAG, "Failed to serialize updatable font map. "
+ "Retrying with system image fonts.", e);
}
}
final FontConfig fontConfig = SystemFonts.getSystemPreinstalledFontConfig(); final FontConfig fontConfig = SystemFonts.getSystemPreinstalledFontConfig();
final Map<String, FontFamily[]> fallback = SystemFonts.buildSystemFallback(fontConfig); final Map<String, FontFamily[]> fallback = SystemFonts.buildSystemFallback(fontConfig);
final Map<String, Typeface> typefaceMap = final Map<String, Typeface> typefaceMap =
SystemFonts.buildSystemTypefaces(fontConfig, fallback); SystemFonts.buildSystemTypefaces(fontConfig, fallback);
try {
final SharedMemory shm = Typeface.serializeFontMap(typefaceMap); return Typeface.serializeFontMap(typefaceMap);
return new SystemFontSettings(shm, fontConfig, fallback, typefaceMap); } catch (IOException | ErrnoException e) {
} catch (IOException | ErrnoException e) { Slog.e(TAG, "Failed to serialize SystemServer system font map", e);
Slog.e(TAG, "Failed to serialize SystemServer system font map", e);
}
return null;
} }
return null;
} }
} }

View File

@@ -25,6 +25,7 @@ import android.graphics.fonts.Font;
import android.graphics.fonts.FontFamily; import android.graphics.fonts.FontFamily;
import android.graphics.fonts.FontManager; import android.graphics.fonts.FontManager;
import android.graphics.fonts.FontVariationAxis; import android.graphics.fonts.FontVariationAxis;
import android.graphics.fonts.SystemFonts;
import android.os.Binder; import android.os.Binder;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.Process; import android.os.Process;
@@ -95,8 +96,8 @@ public class FontManagerShellCommand extends ShellCommand {
} }
/* package */ void dumpAll(@NonNull IndentingPrintWriter w) { /* package */ void dumpAll(@NonNull IndentingPrintWriter w) {
final FontManagerService.SystemFontSettings settings = mService.getCurrentFontSettings(); FontConfig fontConfig = mService.getSystemFontConfig();
dumpFontConfig(w, settings.getSystemFontConfig()); dumpFontConfig(w, fontConfig);
} }
private void dumpSingleFontConfig( private void dumpSingleFontConfig(
@@ -276,19 +277,19 @@ public class FontManagerShellCommand extends ShellCommand {
private int dump(ShellCommand shell) { private int dump(ShellCommand shell) {
final Context ctx = mService.getContext(); final Context ctx = mService.getContext();
final FontManagerService.SystemFontSettings settings =
mService.getCurrentFontSettings();
if (!DumpUtils.checkDumpPermission(ctx, TAG, shell.getErrPrintWriter())) { if (!DumpUtils.checkDumpPermission(ctx, TAG, shell.getErrPrintWriter())) {
return 1; return 1;
} }
final IndentingPrintWriter writer = final IndentingPrintWriter writer =
new IndentingPrintWriter(shell.getOutPrintWriter(), " "); new IndentingPrintWriter(shell.getOutPrintWriter(), " ");
String nextArg = shell.getNextArg(); String nextArg = shell.getNextArg();
FontConfig fontConfig = mService.getSystemFontConfig();
if (nextArg == null) { if (nextArg == null) {
dumpFontConfig(writer, settings.getSystemFontConfig()); dumpFontConfig(writer, fontConfig);
} else { } else {
final Map<String, FontFamily[]> fallbackMap = final Map<String, FontFamily[]> fallbackMap =
settings.getSystemFallbackMap(); SystemFonts.buildSystemFallback(fontConfig);
FontFamily[] families = fallbackMap.get(nextArg); FontFamily[] families = fallbackMap.get(nextArg);
if (families == null) { if (families == null) {
writer.println("Font Family \"" + nextArg + "\" not found"); writer.println("Font Family \"" + nextArg + "\" not found");
@@ -364,10 +365,9 @@ public class FontManagerShellCommand extends ShellCommand {
} }
private int status(ShellCommand shell) throws SystemFontException { private int status(ShellCommand shell) throws SystemFontException {
final FontManagerService.SystemFontSettings settings = mService.getCurrentFontSettings();
final IndentingPrintWriter writer = final IndentingPrintWriter writer =
new IndentingPrintWriter(shell.getOutPrintWriter(), " "); new IndentingPrintWriter(shell.getOutPrintWriter(), " ");
FontConfig config = settings.getSystemFontConfig(); FontConfig config = mService.getSystemFontConfig();
writer.println("Current Version: " + config.getConfigVersion()); writer.println("Current Version: " + config.getConfigVersion());
LocalDateTime dt = LocalDateTime.ofEpochSecond(config.getLastModifiedDate(), 0, LocalDateTime dt = LocalDateTime.ofEpochSecond(config.getLastModifiedDate(), 0,