Merge "Implement RS GC worker thread." into jb-mr2-dev

This commit is contained in:
Jason Sams
2013-04-10 04:21:01 +00:00
committed by Android (Google) Code Review
4 changed files with 75 additions and 1 deletions

View File

@ -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; mType = t;
mUsage = usage; mUsage = usage;
if (t != null) { if (t != null) {
updateCacheInfo(t); updateCacheInfo(t);
} }
} }
private void validateIsInt32() { private void validateIsInt32() {
@ -1235,6 +1243,7 @@ public class Allocation extends BaseObj {
if (type.getID(rs) == 0) { if (type.getID(rs) == 0) {
throw new RSInvalidStateException("Bad Type"); throw new RSInvalidStateException("Bad Type");
} }
int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
if (id == 0) { if (id == 0) {
throw new RSRuntimeException("Allocation creation failed."); throw new RSRuntimeException("Allocation creation failed.");
@ -1384,7 +1393,6 @@ public class Allocation extends BaseObj {
return alloc; return alloc;
} }
int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage); int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
if (id == 0) { if (id == 0) {
throw new RSRuntimeException("Load failed."); throw new RSRuntimeException("Load failed.");

View File

@ -71,6 +71,9 @@ public class BaseObj {
private int mID; private int mID;
private boolean mDestroyed; private boolean mDestroyed;
private String mName; private String mName;
int mGCSize;
RenderScript mRS; RenderScript mRS;
/** /**
@ -135,6 +138,9 @@ public class BaseObj {
throw new RSInvalidStateException("Object already destroyed."); throw new RSInvalidStateException("Object already destroyed.");
} }
mDestroyed = true; mDestroyed = true;
if (mGCSize != 0) {
mRS.removeAllocSizeForGC(mGCSize);
}
mRS.nObjDestroy(mID); mRS.nObjDestroy(mID);
} }

View File

@ -759,6 +759,8 @@ public class RenderScript {
int mContext; int mContext;
@SuppressWarnings({"FieldCanBeLocal"}) @SuppressWarnings({"FieldCanBeLocal"})
MessageThread mMessageThread; MessageThread mMessageThread;
GCThread mGCThread;
Element mElement_U8; Element mElement_U8;
Element mElement_I8; 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) { RenderScript(Context ctx) {
if (ctx != null) { if (ctx != null) {
mApplicationContext = ctx.getApplicationContext(); mApplicationContext = ctx.getApplicationContext();
@ -1063,6 +1108,15 @@ public class RenderScript {
return create(ctx, sdkVersion, ContextType.NORMAL); 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. * Create a basic RenderScript context.
* *
@ -1079,7 +1133,9 @@ public class RenderScript {
throw new RSDriverException("Failed to create RS context."); throw new RSDriverException("Failed to create RS context.");
} }
rs.mMessageThread = new MessageThread(rs); rs.mMessageThread = new MessageThread(rs);
rs.mGCThread = new GCThread(rs);
rs.mMessageThread.start(); rs.mMessageThread.start();
rs.mGCThread.start();
return rs; return rs;
} }
@ -1134,8 +1190,10 @@ public class RenderScript {
validate(); validate();
nContextDeinitToClient(mContext); nContextDeinitToClient(mContext);
mMessageThread.mRun = false; mMessageThread.mRun = false;
mGCThread.mRun = false;
try { try {
mMessageThread.join(); mMessageThread.join();
mGCThread.join();
} catch(InterruptedException e) { } catch(InterruptedException e) {
} }

View File

@ -60,6 +60,8 @@ public class ScriptC extends Script {
throw new RSRuntimeException("Loading of ScriptC script failed."); throw new RSRuntimeException("Loading of ScriptC script failed.");
} }
setID(id); setID(id);
mGCSize = 2 * 1024 * 1024;
rs.addAllocSizeForGC(mGCSize);
} }