summaryrefslogtreecommitdiff
path: root/graphics/java/android/renderscript/RenderScript.java
diff options
context:
space:
mode:
authorJason Sams <jsams@google.com>2013-04-09 21:27:53 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2013-04-09 21:27:53 -0700
commit171af247589f63fffee37a0e81a975afb62279a2 (patch)
treea4c136808ce7de93eb71449e8c01d2720ee2a095 /graphics/java/android/renderscript/RenderScript.java
parent2c128082c65d696fc1ae71c098ab3f47c411a554 (diff)
parentfd38ca2cdcaa07800058a12622896cdd9d2e2189 (diff)
am fd38ca2c: am 07c6fa12: Merge "Implement RS GC worker thread." into jb-mr2-dev
* commit 'fd38ca2cdcaa07800058a12622896cdd9d2e2189': Implement RS GC worker thread.
Diffstat (limited to 'graphics/java/android/renderscript/RenderScript.java')
-rw-r--r--graphics/java/android/renderscript/RenderScript.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 5c1fd5da5fdd..d24c01add312 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -767,6 +767,8 @@ public class RenderScript {
int mContext;
@SuppressWarnings({"FieldCanBeLocal"})
MessageThread mMessageThread;
+ GCThread mGCThread;
+
Element mElement_U8;
Element mElement_I8;
@@ -1049,6 +1051,49 @@ public class RenderScript {
}
}
+ static class GCThread extends Thread {
+ RenderScript mRS;
+ boolean mRun = true;
+
+ int currentSize = 0;
+ final static int targetSize = 256*1024*1024; // call System.gc after 256MB of allocs
+
+ GCThread(RenderScript rs) {
+ super("RSGCThread");
+ mRS = rs;
+
+ }
+
+ public void run() {
+ while(mRun) {
+ boolean doGC = false;
+ synchronized(this) {
+ if (currentSize >= targetSize) {
+ doGC = true;
+ }
+ }
+ if (doGC == true) {
+ System.gc();
+ }
+ try {
+ sleep(1, 0);
+ } catch(InterruptedException e) {
+ }
+ }
+ Log.d(LOG_TAG, "GCThread exiting.");
+ }
+
+ public synchronized void addAllocSize(int bytes) {
+ currentSize += bytes;
+ }
+
+ public synchronized void removeAllocSize(int bytes) {
+ currentSize -= bytes;
+ }
+
+ }
+
+
RenderScript(Context ctx) {
if (ctx != null) {
mApplicationContext = ctx.getApplicationContext();
@@ -1071,6 +1116,15 @@ public class RenderScript {
return create(ctx, sdkVersion, ContextType.NORMAL);
}
+ void addAllocSizeForGC(int bytes) {
+ mGCThread.addAllocSize(bytes);
+ }
+
+ void removeAllocSizeForGC(int bytes) {
+ mGCThread.removeAllocSize(bytes);
+ }
+
+
/**
* Create a basic RenderScript context.
*
@@ -1092,7 +1146,9 @@ public class RenderScript {
throw new RSDriverException("Failed to create RS context.");
}
rs.mMessageThread = new MessageThread(rs);
+ rs.mGCThread = new GCThread(rs);
rs.mMessageThread.start();
+ rs.mGCThread.start();
return rs;
}
@@ -1147,8 +1203,10 @@ public class RenderScript {
validate();
nContextDeinitToClient(mContext);
mMessageThread.mRun = false;
+ mGCThread.mRun = false;
try {
mMessageThread.join();
+ mGCThread.join();
} catch(InterruptedException e) {
}