Skip to content
Snippets Groups Projects
Commit 00334626 authored by Jerome Gaillard's avatar Jerome Gaillard
Browse files

Move native registries to their own inner classes

Having native methods called in the static initalization of the class
that defines them is problematic for the native registration on host
JVM. This moves the registries into their own inner classes to avoid
this issue.

Bug: 337329128
Test: N/A
Change-Id: Id767661d4da38f53c112bb1376798141b38d2c6c
parent 363c98e3
No related branches found
No related tags found
No related merge requests found
......@@ -45,15 +45,17 @@ public class FontFamily {
private static String TAG = "FontFamily";
private static final NativeAllocationRegistry sBuilderRegistry =
NativeAllocationRegistry.createMalloced(
FontFamily.class.getClassLoader(), nGetBuilderReleaseFunc());
private @Nullable Runnable mNativeBuilderCleaner;
private static final NativeAllocationRegistry sFamilyRegistry =
NativeAllocationRegistry.createMalloced(
FontFamily.class.getClassLoader(), nGetFamilyReleaseFunc());
private static class NoImagePreloadHolder {
private static final NativeAllocationRegistry sBuilderRegistry =
NativeAllocationRegistry.createMalloced(
FontFamily.class.getClassLoader(), nGetBuilderReleaseFunc());
private static final NativeAllocationRegistry sFamilyRegistry =
NativeAllocationRegistry.createMalloced(
FontFamily.class.getClassLoader(), nGetFamilyReleaseFunc());
}
/**
* @hide
......@@ -74,7 +76,8 @@ public class FontFamily {
publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
public FontFamily() {
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);
}
mBuilderPtr = nInitBuilder(langsString, variant);
mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr);
mNativeBuilderCleaner = NoImagePreloadHolder.sBuilderRegistry.registerNativeAllocation(this,
mBuilderPtr);
}
/**
......@@ -113,7 +117,7 @@ public class FontFamily {
mNativeBuilderCleaner.run();
mBuilderPtr = 0;
if (mNativePtr != 0) {
sFamilyRegistry.registerNativeAllocation(this, mNativePtr);
NoImagePreloadHolder.sFamilyRegistry.registerNativeAllocation(this, mNativePtr);
}
return mNativePtr != 0;
}
......
......@@ -61,13 +61,15 @@ public final class Font {
private static final int STYLE_ITALIC = 1;
private static final int STYLE_NORMAL = 0;
private static final NativeAllocationRegistry BUFFER_REGISTRY =
NativeAllocationRegistry.createMalloced(
ByteBuffer.class.getClassLoader(), nGetReleaseNativeFont());
private static final NativeAllocationRegistry FONT_REGISTRY =
NativeAllocationRegistry.createMalloced(Font.class.getClassLoader(),
nGetReleaseNativeFont());
private static class NoImagePreloadHolder {
private static final NativeAllocationRegistry BUFFER_REGISTRY =
NativeAllocationRegistry.createMalloced(
ByteBuffer.class.getClassLoader(), nGetReleaseNativeFont());
private static final NativeAllocationRegistry FONT_REGISTRY =
NativeAllocationRegistry.createMalloced(Font.class.getClassLoader(),
nGetReleaseNativeFont());
}
/**
* A builder class for creating new Font.
......@@ -530,7 +532,7 @@ public final class Font {
public Font(long 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);
// 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.
mBuffer = fromNative.asReadOnlyBuffer();
......
......@@ -73,9 +73,11 @@ public final class FontFamily {
* A builder class for creating new FontFamily.
*/
public static final class Builder {
private static final NativeAllocationRegistry sFamilyRegistory =
NativeAllocationRegistry.createMalloced(FontFamily.class.getClassLoader(),
nGetReleaseNativeFamily());
private static class NoImagePreloadHolder {
private static final NativeAllocationRegistry sFamilyRegistry =
NativeAllocationRegistry.createMalloced(FontFamily.class.getClassLoader(),
nGetReleaseNativeFamily());
}
private final ArrayList<Font> mFonts = new ArrayList<>();
// 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,
isDefaultFallback, variableFamilyType);
final FontFamily family = new FontFamily(ptr);
sFamilyRegistory.registerNativeAllocation(family, ptr);
NoImagePreloadHolder.sFamilyRegistry.registerNativeAllocation(family, ptr);
return family;
}
......
......@@ -480,9 +480,11 @@ public class LineBreaker {
}
}
private static final NativeAllocationRegistry sRegistry =
NativeAllocationRegistry.createMalloced(
LineBreaker.class.getClassLoader(), nGetReleaseFunc());
private static class NoImagePreloadHolder {
private static final NativeAllocationRegistry sRegistry =
NativeAllocationRegistry.createMalloced(
LineBreaker.class.getClassLoader(), nGetReleaseFunc());
}
private final long mNativePtr;
......@@ -494,7 +496,7 @@ public class LineBreaker {
@Nullable int[] indents, boolean useBoundsForWidth) {
mNativePtr = nInit(breakStrategy, hyphenationFrequency,
justify == JUSTIFICATION_MODE_INTER_WORD, indents, useBoundsForWidth);
sRegistry.registerNativeAllocation(this, mNativePtr);
NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mNativePtr);
}
/**
......
......@@ -46,9 +46,11 @@ import java.util.Objects;
* @see TextRunShaper#shapeTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)
*/
public final class PositionedGlyphs {
private static final NativeAllocationRegistry REGISTRY =
NativeAllocationRegistry.createMalloced(
Typeface.class.getClassLoader(), nReleaseFunc());
private static class NoImagePreloadHolder {
private static final NativeAllocationRegistry REGISTRY =
NativeAllocationRegistry.createMalloced(
Typeface.class.getClassLoader(), nReleaseFunc());
}
private final long mLayoutPtr;
private final float mXOffset;
......@@ -259,7 +261,7 @@ public final class PositionedGlyphs {
mFonts.add(prevFont);
}
REGISTRY.registerNativeAllocation(this, layoutPtr);
NoImagePreloadHolder.REGISTRY.registerNativeAllocation(this, layoutPtr);
}
@CriticalNative
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment