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

Commit cca8170d authored by James Shargo's avatar James Shargo Committed by Jim Shargo
Browse files

surface: Roughly annotate Surface.java with memory usage

We've been seeing a resource leak of un-GC'd surfaces leading to system
failure. This CL updates the Surface.java class with calls into the
runtime to more accurately reflect the resources being held by these
objects.

The choice of size here is somewhat arbitrary, but should strike a good
middle ground between worst and average case scenarios.

Bug: 307862724
Test: Surface tests and new unit test
Change-Id: Ibee8af1e5e6ba0c98b64053f866746b2f9adfbb2
parent 75cb774d
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -39,8 +39,10 @@ import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import android.view.flags.Flags;

import dalvik.system.CloseGuard;
import dalvik.system.VMRuntime;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -101,6 +103,10 @@ public class Surface implements Parcelable {
            long nativeObject, float frameRate, int compatibility, int changeFrameRateStrategy);
    private static native void nativeDestroy(long nativeObject);

    // 5MB is a wild guess for what the average surface should be. On most new phones, a full-screen
    // surface is about 9MB... but not all surfaces are screen size. This should be a nice balance.
    private static final long SURFACE_NATIVE_ALLOCATION_SIZE_BYTES = 5_000_000;

    public static final @android.annotation.NonNull Parcelable.Creator<Surface> CREATOR =
            new Parcelable.Creator<Surface>() {
        @Override
@@ -331,6 +337,7 @@ public class Surface implements Parcelable {
     */
    @UnsupportedAppUsage
    public Surface() {
        registerNativeMemoryUsage();
    }

    /**
@@ -343,6 +350,7 @@ public class Surface implements Parcelable {
     */
    public Surface(@NonNull SurfaceControl from) {
        copyFrom(from);
        registerNativeMemoryUsage();
    }

    /**
@@ -370,6 +378,7 @@ public class Surface implements Parcelable {
            mName = surfaceTexture.toString();
            setNativeObjectLocked(nativeCreateFromSurfaceTexture(surfaceTexture));
        }
        registerNativeMemoryUsage();
    }

    /* called from android_view_Surface_createFromIGraphicBufferProducer() */
@@ -378,6 +387,7 @@ public class Surface implements Parcelable {
        synchronized (mLock) {
            setNativeObjectLocked(nativeObject);
        }
        registerNativeMemoryUsage();
    }

    @Override
@@ -389,6 +399,7 @@ public class Surface implements Parcelable {
            release();
        } finally {
            super.finalize();
            freeNativeMemoryUsage();
        }
    }

@@ -1243,4 +1254,16 @@ public class Surface implements Parcelable {
            return mIsWideColorGamut;
        }
    }

    private static void registerNativeMemoryUsage() {
        if (Flags.enableSurfaceNativeAllocRegistration()) {
            VMRuntime.getRuntime().registerNativeAllocation(SURFACE_NATIVE_ALLOCATION_SIZE_BYTES);
        }
    }

    private static void freeNativeMemoryUsage() {
        if (Flags.enableSurfaceNativeAllocRegistration()) {
            VMRuntime.getRuntime().registerNativeFree(SURFACE_NATIVE_ALLOCATION_SIZE_BYTES);
        }
    }
}