diff options
author | Tim Murray <timmurray@google.com> | 2013-04-09 14:33:32 -0700 |
---|---|---|
committer | Tim Murray <timmurray@google.com> | 2013-04-09 16:01:23 -0700 |
commit | f8c033db1edf36a0ab09568c3142054f0be2d1a1 (patch) | |
tree | 60d13d018ebbd5777404657855722d6cdc345bb6 /graphics/java/android/renderscript/RenderScript.java | |
parent | b4c13c4b045f4d96c377ba1fb9d834c111412a8c (diff) |
Implement RS GC worker thread.
Bug 8579360
Change-Id: I98a5509b68e592bcbb4958f2ca72c236a22910b5
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 bef28aa89709..60f381c657ac 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -724,6 +724,8 @@ public class RenderScript { int mContext; @SuppressWarnings({"FieldCanBeLocal"}) MessageThread mMessageThread; + GCThread mGCThread; + Element mElement_U8; Element mElement_I8; @@ -1006,6 +1008,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(); @@ -1028,6 +1073,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. * @@ -1044,7 +1098,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; } @@ -1099,8 +1155,10 @@ public class RenderScript { validate(); nContextDeinitToClient(mContext); mMessageThread.mRun = false; + mGCThread.mRun = false; try { mMessageThread.join(); + mGCThread.join(); } catch(InterruptedException e) { } |