diff options
-rw-r--r-- | graphics/java/android/renderscript/Allocation.java | 10 | ||||
-rw-r--r-- | graphics/java/android/renderscript/BaseObj.java | 6 | ||||
-rw-r--r-- | graphics/java/android/renderscript/RenderScript.java | 58 | ||||
-rw-r--r-- | graphics/java/android/renderscript/ScriptC.java | 2 |
4 files changed, 75 insertions, 1 deletions
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index 54de6aa4240a..914799250729 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -274,12 +274,20 @@ public class Allocation extends BaseObj { } } + // don't need to account for USAGE_SHARED Allocations + if ((usage & USAGE_SHARED) == 0) { + int numBytes = t.getCount() * t.getElement().getBytesSize(); + rs.addAllocSizeForGC(numBytes); + mGCSize = numBytes; + } + mType = t; mUsage = usage; if (t != null) { updateCacheInfo(t); } + } private void validateIsInt32() { @@ -1235,6 +1243,7 @@ public class Allocation extends BaseObj { if (type.getID(rs) == 0) { throw new RSInvalidStateException("Bad Type"); } + int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); if (id == 0) { throw new RSRuntimeException("Allocation creation failed."); @@ -1384,7 +1393,6 @@ public class Allocation extends BaseObj { return alloc; } - int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage); if (id == 0) { throw new RSRuntimeException("Load failed."); diff --git a/graphics/java/android/renderscript/BaseObj.java b/graphics/java/android/renderscript/BaseObj.java index f464f9bb1025..c2ebc9f79d6a 100644 --- a/graphics/java/android/renderscript/BaseObj.java +++ b/graphics/java/android/renderscript/BaseObj.java @@ -71,6 +71,9 @@ public class BaseObj { private int mID; private boolean mDestroyed; private String mName; + + int mGCSize; + RenderScript mRS; /** @@ -135,6 +138,9 @@ public class BaseObj { throw new RSInvalidStateException("Object already destroyed."); } mDestroyed = true; + if (mGCSize != 0) { + mRS.removeAllocSizeForGC(mGCSize); + } mRS.nObjDestroy(mID); } 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) { } diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java index 108b230f1a5b..9221c7aac69f 100644 --- a/graphics/java/android/renderscript/ScriptC.java +++ b/graphics/java/android/renderscript/ScriptC.java @@ -60,6 +60,8 @@ public class ScriptC extends Script { throw new RSRuntimeException("Loading of ScriptC script failed."); } setID(id); + mGCSize = 2 * 1024 * 1024; + rs.addAllocSizeForGC(mGCSize); } |