Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 635865af authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Do not hold unnecessary map reference

Do not hold the referecne of font config, fallback map and typeface map
which are not accessed frequently. Keep the final SharedMemory which
is used for launching new application.

Bug: 178472803
Test: N/A
Change-Id: Ice4fc349e5d61c6f37f7f8a6962319e70f0b373a
parent 80bb0561
Loading
Loading
Loading
Loading
+36 −61
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ public final class FontManagerService extends IFontManager.Stub {

    @Override
    public FontConfig getFontConfig() throws RemoteException {
        return getCurrentFontSettings().getSystemFontConfig();
        return getSystemFontConfig();
    }

    /* package */ static class SystemFontException extends AndroidException {
@@ -103,7 +103,7 @@ public final class FontManagerService extends IFontManager.Stub {
                            if (!Typeface.ENABLE_LAZY_TYPEFACE_INITIALIZATION) {
                                return null;
                            }
                            return mService.getCurrentFontSettings().getSerializedSystemFontMap();
                            return mService.getCurrentFontMap();
                        }
                    });
            publishBinderService(Context.FONT_SERVICE, mService);
@@ -162,7 +162,7 @@ public final class FontManagerService extends IFontManager.Stub {

    @GuardedBy("FontManagerService.this")
    @Nullable
    private SystemFontSettings mCurrentFontSettings = null;
    private SharedMemory mSerializedFontMap = null;

    private FontManagerService(Context context) {
        mContext = context;
@@ -188,12 +188,12 @@ public final class FontManagerService extends IFontManager.Stub {
        return mContext;
    }

    @NonNull /* package */ SystemFontSettings getCurrentFontSettings() {
    @NonNull /* package */ SharedMemory getCurrentFontMap() {
        synchronized (FontManagerService.this) {
            if (mCurrentFontSettings == null) {
                mCurrentFontSettings = SystemFontSettings.create(mUpdatableFontDir);
            if (mSerializedFontMap == null) {
                mSerializedFontMap = buildNewSerializedFontMap();
            }
            return mCurrentFontSettings;
            return mSerializedFontMap;
        }
    }

@@ -207,7 +207,7 @@ public final class FontManagerService extends IFontManager.Stub {
        synchronized (FontManagerService.this) {
            mUpdatableFontDir.installFontFile(fd, pkcs7Signature);
            // 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);
    }

    /* package */ static class SystemFontSettings {
        private final @NonNull SharedMemory mSerializedSystemFontMap;
        private final @NonNull FontConfig mSystemFontConfig;
        private final @NonNull Map<String, FontFamily[]> mSystemFallbackMap;
        private final @NonNull Map<String, Typeface> mSystemTypefaceMap;

        SystemFontSettings(
                @NonNull SharedMemory serializedSystemFontMap,
                @NonNull FontConfig systemFontConfig,
                @NonNull Map<String, FontFamily[]> systemFallbackMap,
                @NonNull Map<String, Typeface> systemTypefaceMap) {
            mSerializedSystemFontMap = serializedSystemFontMap;
            mSystemFontConfig = systemFontConfig;
            mSystemFallbackMap = systemFallbackMap;
            mSystemTypefaceMap = systemTypefaceMap;
        }

        public @NonNull SharedMemory getSerializedSystemFontMap() {
            return mSerializedSystemFontMap;
        }

    /**
     * Returns an active system font configuration.
     */
    public @NonNull FontConfig getSystemFontConfig() {
            return mSystemFontConfig;
        }

        public @NonNull Map<String, FontFamily[]> getSystemFallbackMap() {
            return mSystemFallbackMap;
        if (mUpdatableFontDir != null) {
            return mUpdatableFontDir.getSystemFontConfig();
        } else {
            return SystemFonts.getSystemPreinstalledFontConfig();
        }

        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);
    /**
     * Make new serialized font map data.
     */
    public @Nullable SharedMemory buildNewSerializedFontMap() {
        try {
            final FontConfig fontConfig = 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);
            return Typeface.serializeFontMap(typefaceMap);
        } catch (IOException | ErrnoException e) {
            Slog.w(TAG, "Failed to serialize updatable font map. "
                    + "Retrying with system image fonts.", e);
        }
            }

        try {
            final FontConfig fontConfig = SystemFonts.getSystemPreinstalledFontConfig();
            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);

            return Typeface.serializeFontMap(typefaceMap);
        } catch (IOException | ErrnoException e) {
            Slog.e(TAG, "Failed to serialize SystemServer system font map", e);
        }
        return null;
    }
    }

}
+8 −8
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.graphics.fonts.Font;
import android.graphics.fonts.FontFamily;
import android.graphics.fonts.FontManager;
import android.graphics.fonts.FontVariationAxis;
import android.graphics.fonts.SystemFonts;
import android.os.Binder;
import android.os.ParcelFileDescriptor;
import android.os.Process;
@@ -95,8 +96,8 @@ public class FontManagerShellCommand extends ShellCommand {
    }

    /* package */ void dumpAll(@NonNull IndentingPrintWriter w) {
        final FontManagerService.SystemFontSettings settings = mService.getCurrentFontSettings();
        dumpFontConfig(w, settings.getSystemFontConfig());
        FontConfig fontConfig = mService.getSystemFontConfig();
        dumpFontConfig(w, fontConfig);
    }

    private void dumpSingleFontConfig(
@@ -276,19 +277,19 @@ public class FontManagerShellCommand extends ShellCommand {

    private int dump(ShellCommand shell) {
        final Context ctx = mService.getContext();
        final FontManagerService.SystemFontSettings settings =
                mService.getCurrentFontSettings();

        if (!DumpUtils.checkDumpPermission(ctx, TAG, shell.getErrPrintWriter())) {
            return 1;
        }
        final IndentingPrintWriter writer =
                new IndentingPrintWriter(shell.getOutPrintWriter(), "  ");
        String nextArg = shell.getNextArg();
        FontConfig fontConfig = mService.getSystemFontConfig();
        if (nextArg == null) {
            dumpFontConfig(writer, settings.getSystemFontConfig());
            dumpFontConfig(writer, fontConfig);
        } else {
            final Map<String, FontFamily[]> fallbackMap =
                    settings.getSystemFallbackMap();
                    SystemFonts.buildSystemFallback(fontConfig);
            FontFamily[] families = fallbackMap.get(nextArg);
            if (families == null) {
                writer.println("Font Family \"" + nextArg + "\" not found");
@@ -364,10 +365,9 @@ public class FontManagerShellCommand extends ShellCommand {
    }

    private int status(ShellCommand shell) throws SystemFontException {
        final FontManagerService.SystemFontSettings settings = mService.getCurrentFontSettings();
        final IndentingPrintWriter writer =
                new IndentingPrintWriter(shell.getOutPrintWriter(), "  ");
        FontConfig config = settings.getSystemFontConfig();
        FontConfig config = mService.getSystemFontConfig();

        writer.println("Current Version: " + config.getConfigVersion());
        LocalDateTime dt = LocalDateTime.ofEpochSecond(config.getLastModifiedDate(), 0,