Loading graphics/java/android/graphics/Bitmap.java +39 −8 Original line number Diff line number Diff line Loading @@ -139,19 +139,47 @@ public final class Bitmap implements Parcelable { private static final WeakHashMap<Bitmap, Void> sAllBitmaps = new WeakHashMap<>(); /** * @hide * Two NativeAllocationRegistry instances are used to register the native * allocation for each bitmap: * * 1. One with a no-op free function for updating the native allocation * size of the pixel data only, without actually releasing the native * object that's associated with the bitmap instance. * * 2. sRegistry is the other static one with a valid freeFunction and a * default (estimated) size to actually release the native object * associated with this bitmap. * * mRecycler is the cleaner runner from #1, and is used in Bitmap#recycle() * to update the native allocation size associated with the pixel data * released. */ private static NativeAllocationRegistry getRegistry(boolean malloc, long size) { private static NativeAllocationRegistry sRegistry = null; private Runnable mRecycler; private void registerNativeAllocation(boolean malloc) { final long free = nativeGetNativeFinalizer(); final long noop = nativeGetNativeNoop(); final int size = getAllocationByteCount(); NativeAllocationRegistry registry; if (com.android.libcore.readonly.Flags.nativeMetrics()) { Class cls = Bitmap.class; return malloc ? NativeAllocationRegistry.createMalloced(cls, free, size) : NativeAllocationRegistry.createNonmalloced(cls, free, size); if (sRegistry == null) { sRegistry = NativeAllocationRegistry.createMalloced(cls, free); } registry = malloc ? NativeAllocationRegistry.createMalloced(cls, noop, size) : NativeAllocationRegistry.createNonmalloced(cls, noop, size); } else { ClassLoader loader = Bitmap.class.getClassLoader(); return malloc ? NativeAllocationRegistry.createMalloced(loader, free, size) : NativeAllocationRegistry.createNonmalloced(loader, free, size); if (sRegistry == null) { sRegistry = NativeAllocationRegistry.createMalloced(loader, free); } registry = malloc ? NativeAllocationRegistry.createMalloced(loader, noop, size) : NativeAllocationRegistry.createNonmalloced(loader, noop, size); } mRecycler = registry.registerNativeAllocation(this, mNativePtr); sRegistry.registerNativeAllocation(this, mNativePtr); } /** Loading Loading @@ -187,8 +215,7 @@ public final class Bitmap implements Parcelable { mNativePtr = nativeBitmap; mSourceId = nativeGetSourceId(mNativePtr); final int allocationByteCount = getAllocationByteCount(); getRegistry(fromMalloc, allocationByteCount).registerNativeAllocation(this, mNativePtr); registerNativeAllocation(fromMalloc); synchronized (Bitmap.class) { sAllBitmaps.put(this, null); Loading Loading @@ -405,6 +432,9 @@ public final class Bitmap implements Parcelable { mNinePatchChunk = null; mRecycled = true; mHardwareBuffer = null; if (mRecycler != null) { mRecycler.run(); } } } Loading Loading @@ -2603,6 +2633,7 @@ public final class Bitmap implements Parcelable { private static native Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig); private static native int nativeGetAshmemFD(long nativeBitmap); private static native long nativeGetNativeFinalizer(); private static native long nativeGetNativeNoop(); private static native void nativeRecycle(long nativeBitmap); @UnsupportedAppUsage private static native void nativeReconfigure(long nativeBitmap, int width, int height, Loading libs/hwui/jni/Bitmap.cpp +8 −0 Original line number Diff line number Diff line Loading @@ -563,6 +563,13 @@ static void Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) { bitmap->freePixels(); } static void Bitmap_noop(BitmapWrapper* bitmap) { } static jlong Bitmap_getNativeNoop(JNIEnv*, jobject) { return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_noop)); } static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle, jint width, jint height, jint configHandle, jboolean requestPremul) { LocalScopedBitmap bitmap(bitmapHandle); Loading Loading @@ -1419,6 +1426,7 @@ static const JNINativeMethod gBitmapMethods[] = { {"nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;", (void*)Bitmap_copyAshmemConfig}, {"nativeGetAshmemFD", "(J)I", (void*)Bitmap_getAshmemFd}, {"nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer}, {"nativeGetNativeNoop", "()J", (void*)Bitmap_getNativeNoop}, {"nativeRecycle", "(J)V", (void*)Bitmap_recycle}, {"nativeReconfigure", "(JIIIZ)V", (void*)Bitmap_reconfigure}, {"nativeCompress", "(JIILjava/io/OutputStream;[B)Z", (void*)Bitmap_compress}, Loading Loading
graphics/java/android/graphics/Bitmap.java +39 −8 Original line number Diff line number Diff line Loading @@ -139,19 +139,47 @@ public final class Bitmap implements Parcelable { private static final WeakHashMap<Bitmap, Void> sAllBitmaps = new WeakHashMap<>(); /** * @hide * Two NativeAllocationRegistry instances are used to register the native * allocation for each bitmap: * * 1. One with a no-op free function for updating the native allocation * size of the pixel data only, without actually releasing the native * object that's associated with the bitmap instance. * * 2. sRegistry is the other static one with a valid freeFunction and a * default (estimated) size to actually release the native object * associated with this bitmap. * * mRecycler is the cleaner runner from #1, and is used in Bitmap#recycle() * to update the native allocation size associated with the pixel data * released. */ private static NativeAllocationRegistry getRegistry(boolean malloc, long size) { private static NativeAllocationRegistry sRegistry = null; private Runnable mRecycler; private void registerNativeAllocation(boolean malloc) { final long free = nativeGetNativeFinalizer(); final long noop = nativeGetNativeNoop(); final int size = getAllocationByteCount(); NativeAllocationRegistry registry; if (com.android.libcore.readonly.Flags.nativeMetrics()) { Class cls = Bitmap.class; return malloc ? NativeAllocationRegistry.createMalloced(cls, free, size) : NativeAllocationRegistry.createNonmalloced(cls, free, size); if (sRegistry == null) { sRegistry = NativeAllocationRegistry.createMalloced(cls, free); } registry = malloc ? NativeAllocationRegistry.createMalloced(cls, noop, size) : NativeAllocationRegistry.createNonmalloced(cls, noop, size); } else { ClassLoader loader = Bitmap.class.getClassLoader(); return malloc ? NativeAllocationRegistry.createMalloced(loader, free, size) : NativeAllocationRegistry.createNonmalloced(loader, free, size); if (sRegistry == null) { sRegistry = NativeAllocationRegistry.createMalloced(loader, free); } registry = malloc ? NativeAllocationRegistry.createMalloced(loader, noop, size) : NativeAllocationRegistry.createNonmalloced(loader, noop, size); } mRecycler = registry.registerNativeAllocation(this, mNativePtr); sRegistry.registerNativeAllocation(this, mNativePtr); } /** Loading Loading @@ -187,8 +215,7 @@ public final class Bitmap implements Parcelable { mNativePtr = nativeBitmap; mSourceId = nativeGetSourceId(mNativePtr); final int allocationByteCount = getAllocationByteCount(); getRegistry(fromMalloc, allocationByteCount).registerNativeAllocation(this, mNativePtr); registerNativeAllocation(fromMalloc); synchronized (Bitmap.class) { sAllBitmaps.put(this, null); Loading Loading @@ -405,6 +432,9 @@ public final class Bitmap implements Parcelable { mNinePatchChunk = null; mRecycled = true; mHardwareBuffer = null; if (mRecycler != null) { mRecycler.run(); } } } Loading Loading @@ -2603,6 +2633,7 @@ public final class Bitmap implements Parcelable { private static native Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig); private static native int nativeGetAshmemFD(long nativeBitmap); private static native long nativeGetNativeFinalizer(); private static native long nativeGetNativeNoop(); private static native void nativeRecycle(long nativeBitmap); @UnsupportedAppUsage private static native void nativeReconfigure(long nativeBitmap, int width, int height, Loading
libs/hwui/jni/Bitmap.cpp +8 −0 Original line number Diff line number Diff line Loading @@ -563,6 +563,13 @@ static void Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) { bitmap->freePixels(); } static void Bitmap_noop(BitmapWrapper* bitmap) { } static jlong Bitmap_getNativeNoop(JNIEnv*, jobject) { return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_noop)); } static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle, jint width, jint height, jint configHandle, jboolean requestPremul) { LocalScopedBitmap bitmap(bitmapHandle); Loading Loading @@ -1419,6 +1426,7 @@ static const JNINativeMethod gBitmapMethods[] = { {"nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;", (void*)Bitmap_copyAshmemConfig}, {"nativeGetAshmemFD", "(J)I", (void*)Bitmap_getAshmemFd}, {"nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer}, {"nativeGetNativeNoop", "()J", (void*)Bitmap_getNativeNoop}, {"nativeRecycle", "(J)V", (void*)Bitmap_recycle}, {"nativeReconfigure", "(JIIIZ)V", (void*)Bitmap_reconfigure}, {"nativeCompress", "(JIILjava/io/OutputStream;[B)Z", (void*)Bitmap_compress}, Loading