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

Commit 13c4422e authored by Eric Miao's avatar Eric Miao
Browse files

Track HardwareBuffer native allocations

Bug: b/363028387
Flag: com.android.libcore.native_metrics

This CL will make use of the new API to create NativeAllocationRegistry
that associates with HardwareBuffer.class if libcore.native_metrics is
enabled (otherwise falls back to the legacy API). This allows tracking
of native allocations that are HardwareBuffer specific.

Change-Id: I848cf66dacf1cf82baf1ff8d2012866ee548973f
parent ad216bac
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -277,6 +277,17 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
        return new HardwareBuffer(nativeObject);
    }

    /**
     * @hide
     */
    private static NativeAllocationRegistry getRegistry(long size) {
        final long func = nGetNativeFinalizer();
        final Class cls = HardwareBuffer.class;
        return com.android.libcore.Flags.nativeMetrics()
            ? NativeAllocationRegistry.createNonmalloced(cls, func, size)
            : NativeAllocationRegistry.createNonmalloced(cls.getClassLoader(), func, size);
    }

    /**
     * Private use only. See {@link #create(int, int, int, int, long)}. May also be
     * called from JNI using an already allocated native <code>HardwareBuffer</code>.
@@ -285,10 +296,7 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
    private HardwareBuffer(long nativeObject) {
        mNativeObject = nativeObject;
        long bufferSize = nEstimateSize(nativeObject);
        ClassLoader loader = HardwareBuffer.class.getClassLoader();
        NativeAllocationRegistry registry = new NativeAllocationRegistry(
                loader, nGetNativeFinalizer(), bufferSize);
        mCleaner = registry.registerNativeAllocation(this, mNativeObject);
        mCleaner = getRegistry(bufferSize).registerNativeAllocation(this, mNativeObject);
        mCloseGuard.open("HardwareBuffer.close");
    }