From 2f2472c42be8e80f00019594036744d902e09dcb Mon Sep 17 00:00:00 2001 From: Tim Murray Date: Thu, 22 Aug 2013 14:55:26 -0700 Subject: [PATCH] Add garbage collection hooks bug 10428172 Change-Id: I6365aa58bbc99aa134e1f261a5819e07a7d70fb0 --- graphics/java/android/renderscript/Allocation.java | 13 +++++++++++++ .../java/android/renderscript/RenderScript.java | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index 362b586376068..7d05a744a19cd 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -61,6 +61,7 @@ public class Allocation extends BaseObj { Bitmap mBitmap; int mUsage; Allocation mAdaptedAllocation; + int mSize; boolean mConstrainedLOD; boolean mConstrainedFace; @@ -268,10 +269,22 @@ public class Allocation extends BaseObj { mType = t; mUsage = usage; + mSize = mType.getCount() * mType.getElement().getBytesSize(); if (t != null) { updateCacheInfo(t); } + try { + RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize); + } catch (Exception e) { + Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e); + throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e); + } + } + + protected void finalize() throws Throwable { + RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize); + super.finalize(); } private void validateIsInt32() { diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index 4de4766577c40..854f07928bd04 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -18,6 +18,7 @@ package android.renderscript; import java.io.File; import java.lang.reflect.Field; +import java.lang.reflect.Method; import android.content.Context; import android.content.pm.ApplicationInfo; @@ -61,10 +62,23 @@ public class RenderScript { static boolean sInitialized; native static void _nInit(); + static Object sRuntime; + static Method registerNativeAllocation; + static Method registerNativeFree; static { sInitialized = false; if (!SystemProperties.getBoolean("config.disable_renderscript", false)) { + try { + Class vm_runtime = Class.forName("dalvik.system.VMRuntime"); + Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); + sRuntime = get_runtime.invoke(null); + registerNativeAllocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE); + registerNativeFree = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE); + } catch (Exception e) { + Log.e(LOG_TAG, "Error loading GC methods: " + e); + throw new RSRuntimeException("Error loading GC methods: " + e); + } try { System.loadLibrary("rs_jni"); _nInit();