diff options
author | Jason Sams <jsams@google.com> | 2013-04-10 04:21:01 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2013-04-10 04:21:01 +0000 |
commit | 07c6fa123da8c7a25edab751543d96fea1dd57f6 (patch) | |
tree | f5325ff5ed0350828946fa52ca43adc5bb01385c /graphics/java/android/renderscript/RenderScript.java | |
parent | 808723f2b8a3842114f064a5ad4ee08fa9698192 (diff) | |
parent | f8c033db1edf36a0ab09568c3142054f0be2d1a1 (diff) |
Merge "Implement RS GC worker thread." into jb-mr2-dev
Diffstat (limited to 'graphics/java/android/renderscript/RenderScript.java')
-rw-r--r-- | graphics/java/android/renderscript/RenderScript.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index 5d72267a440b..2438532c98dd 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -759,6 +759,8 @@ public class RenderScript { int mContext; @SuppressWarnings({"FieldCanBeLocal"}) MessageThread mMessageThread; + GCThread mGCThread; + Element mElement_U8; Element mElement_I8; @@ -1041,6 +1043,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(); @@ -1063,6 +1108,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. * @@ -1079,7 +1133,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; } @@ -1134,8 +1190,10 @@ public class RenderScript { validate(); nContextDeinitToClient(mContext); mMessageThread.mRun = false; + mGCThread.mRun = false; try { mMessageThread.join(); + mGCThread.join(); } catch(InterruptedException e) { } |