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

Commit c5698bab authored by Jerome Gaillard's avatar Jerome Gaillard Committed by Android (Google) Code Review
Browse files

Merge "Move native registries to their own inner classes" into main

parents 7e27b6a7 00334626
Loading
Loading
Loading
Loading
+14 −10
Original line number Original line Diff line number Diff line
@@ -45,15 +45,17 @@ public class FontFamily {


    private static String TAG = "FontFamily";
    private static String TAG = "FontFamily";


    private @Nullable Runnable mNativeBuilderCleaner;

    private static class NoImagePreloadHolder {
        private static final NativeAllocationRegistry sBuilderRegistry =
        private static final NativeAllocationRegistry sBuilderRegistry =
                NativeAllocationRegistry.createMalloced(
                NativeAllocationRegistry.createMalloced(
                        FontFamily.class.getClassLoader(), nGetBuilderReleaseFunc());
                        FontFamily.class.getClassLoader(), nGetBuilderReleaseFunc());


    private @Nullable Runnable mNativeBuilderCleaner;

        private static final NativeAllocationRegistry sFamilyRegistry =
        private static final NativeAllocationRegistry sFamilyRegistry =
                NativeAllocationRegistry.createMalloced(
                NativeAllocationRegistry.createMalloced(
                        FontFamily.class.getClassLoader(), nGetFamilyReleaseFunc());
                        FontFamily.class.getClassLoader(), nGetFamilyReleaseFunc());
    }


    /**
    /**
     * @hide
     * @hide
@@ -74,7 +76,8 @@ public class FontFamily {
            publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
            publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
    public FontFamily() {
    public FontFamily() {
        mBuilderPtr = nInitBuilder(null, 0);
        mBuilderPtr = nInitBuilder(null, 0);
        mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr);
        mNativeBuilderCleaner = NoImagePreloadHolder.sBuilderRegistry.registerNativeAllocation(this,
                mBuilderPtr);
    }
    }


    /**
    /**
@@ -92,7 +95,8 @@ public class FontFamily {
            langsString = TextUtils.join(",", langs);
            langsString = TextUtils.join(",", langs);
        }
        }
        mBuilderPtr = nInitBuilder(langsString, variant);
        mBuilderPtr = nInitBuilder(langsString, variant);
        mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr);
        mNativeBuilderCleaner = NoImagePreloadHolder.sBuilderRegistry.registerNativeAllocation(this,
                mBuilderPtr);
    }
    }


    /**
    /**
@@ -113,7 +117,7 @@ public class FontFamily {
        mNativeBuilderCleaner.run();
        mNativeBuilderCleaner.run();
        mBuilderPtr = 0;
        mBuilderPtr = 0;
        if (mNativePtr != 0) {
        if (mNativePtr != 0) {
            sFamilyRegistry.registerNativeAllocation(this, mNativePtr);
            NoImagePreloadHolder.sFamilyRegistry.registerNativeAllocation(this, mNativePtr);
        }
        }
        return mNativePtr != 0;
        return mNativePtr != 0;
    }
    }
+11 −9
Original line number Original line Diff line number Diff line
@@ -61,6 +61,7 @@ public final class Font {
    private static final int STYLE_ITALIC = 1;
    private static final int STYLE_ITALIC = 1;
    private static final int STYLE_NORMAL = 0;
    private static final int STYLE_NORMAL = 0;


    private static class NoImagePreloadHolder {
        private static final NativeAllocationRegistry BUFFER_REGISTRY =
        private static final NativeAllocationRegistry BUFFER_REGISTRY =
                NativeAllocationRegistry.createMalloced(
                NativeAllocationRegistry.createMalloced(
                        ByteBuffer.class.getClassLoader(), nGetReleaseNativeFont());
                        ByteBuffer.class.getClassLoader(), nGetReleaseNativeFont());
@@ -68,6 +69,7 @@ public final class Font {
        private static final NativeAllocationRegistry FONT_REGISTRY =
        private static final NativeAllocationRegistry FONT_REGISTRY =
                NativeAllocationRegistry.createMalloced(Font.class.getClassLoader(),
                NativeAllocationRegistry.createMalloced(Font.class.getClassLoader(),
                        nGetReleaseNativeFont());
                        nGetReleaseNativeFont());
    }


    /**
    /**
     * A builder class for creating new Font.
     * A builder class for creating new Font.
@@ -530,7 +532,7 @@ public final class Font {
    public Font(long nativePtr) {
    public Font(long nativePtr) {
        mNativePtr = nativePtr;
        mNativePtr = nativePtr;


        FONT_REGISTRY.registerNativeAllocation(this, mNativePtr);
        NoImagePreloadHolder.FONT_REGISTRY.registerNativeAllocation(this, mNativePtr);
    }
    }


    /**
    /**
@@ -551,7 +553,7 @@ public final class Font {
                ByteBuffer fromNative = nNewByteBuffer(mNativePtr);
                ByteBuffer fromNative = nNewByteBuffer(mNativePtr);


                // Bind ByteBuffer's lifecycle with underlying font object.
                // Bind ByteBuffer's lifecycle with underlying font object.
                BUFFER_REGISTRY.registerNativeAllocation(fromNative, ref);
                NoImagePreloadHolder.BUFFER_REGISTRY.registerNativeAllocation(fromNative, ref);


                // JNI NewDirectBuffer creates writable ByteBuffer even if it is mmaped readonly.
                // JNI NewDirectBuffer creates writable ByteBuffer even if it is mmaped readonly.
                mBuffer = fromNative.asReadOnlyBuffer();
                mBuffer = fromNative.asReadOnlyBuffer();
+6 −4
Original line number Original line Diff line number Diff line
@@ -73,9 +73,11 @@ public final class FontFamily {
     * A builder class for creating new FontFamily.
     * A builder class for creating new FontFamily.
     */
     */
    public static final class Builder {
    public static final class Builder {
        private static final NativeAllocationRegistry sFamilyRegistory =
        private static class NoImagePreloadHolder {
            private static final NativeAllocationRegistry sFamilyRegistry =
                    NativeAllocationRegistry.createMalloced(FontFamily.class.getClassLoader(),
                    NativeAllocationRegistry.createMalloced(FontFamily.class.getClassLoader(),
                            nGetReleaseNativeFamily());
                            nGetReleaseNativeFamily());
        }


        private final ArrayList<Font> mFonts = new ArrayList<>();
        private final ArrayList<Font> mFonts = new ArrayList<>();
        // Most FontFamily only has  regular, bold, italic, bold-italic. Thus 4 should be good for
        // Most FontFamily only has  regular, bold, italic, bold-italic. Thus 4 should be good for
@@ -183,7 +185,7 @@ public final class FontFamily {
            final long ptr = nBuild(builderPtr, langTags, variant, isCustomFallback,
            final long ptr = nBuild(builderPtr, langTags, variant, isCustomFallback,
                    isDefaultFallback, variableFamilyType);
                    isDefaultFallback, variableFamilyType);
            final FontFamily family = new FontFamily(ptr);
            final FontFamily family = new FontFamily(ptr);
            sFamilyRegistory.registerNativeAllocation(family, ptr);
            NoImagePreloadHolder.sFamilyRegistry.registerNativeAllocation(family, ptr);
            return family;
            return family;
        }
        }


+6 −4
Original line number Original line Diff line number Diff line
@@ -480,9 +480,11 @@ public class LineBreaker {
        }
        }
    }
    }


    private static class NoImagePreloadHolder {
        private static final NativeAllocationRegistry sRegistry =
        private static final NativeAllocationRegistry sRegistry =
                NativeAllocationRegistry.createMalloced(
                NativeAllocationRegistry.createMalloced(
                        LineBreaker.class.getClassLoader(), nGetReleaseFunc());
                        LineBreaker.class.getClassLoader(), nGetReleaseFunc());
    }


    private final long mNativePtr;
    private final long mNativePtr;


@@ -494,7 +496,7 @@ public class LineBreaker {
            @Nullable int[] indents, boolean useBoundsForWidth) {
            @Nullable int[] indents, boolean useBoundsForWidth) {
        mNativePtr = nInit(breakStrategy, hyphenationFrequency,
        mNativePtr = nInit(breakStrategy, hyphenationFrequency,
                justify == JUSTIFICATION_MODE_INTER_WORD, indents, useBoundsForWidth);
                justify == JUSTIFICATION_MODE_INTER_WORD, indents, useBoundsForWidth);
        sRegistry.registerNativeAllocation(this, mNativePtr);
        NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mNativePtr);
    }
    }


    /**
    /**
+6 −4
Original line number Original line Diff line number Diff line
@@ -46,9 +46,11 @@ import java.util.Objects;
 * @see TextRunShaper#shapeTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)
 * @see TextRunShaper#shapeTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)
 */
 */
public final class PositionedGlyphs {
public final class PositionedGlyphs {
    private static class NoImagePreloadHolder {
        private static final NativeAllocationRegistry REGISTRY =
        private static final NativeAllocationRegistry REGISTRY =
                NativeAllocationRegistry.createMalloced(
                NativeAllocationRegistry.createMalloced(
                        Typeface.class.getClassLoader(), nReleaseFunc());
                        Typeface.class.getClassLoader(), nReleaseFunc());
    }


    private final long mLayoutPtr;
    private final long mLayoutPtr;
    private final float mXOffset;
    private final float mXOffset;
@@ -259,7 +261,7 @@ public final class PositionedGlyphs {
            mFonts.add(prevFont);
            mFonts.add(prevFont);
        }
        }


        REGISTRY.registerNativeAllocation(this, layoutPtr);
        NoImagePreloadHolder.REGISTRY.registerNativeAllocation(this, layoutPtr);
    }
    }


    @CriticalNative
    @CriticalNative