am 07c6fa12: Merge "Implement RS GC worker thread." into jb-mr2-dev

* commit '07c6fa123da8c7a25edab751543d96fea1dd57f6':
  Implement RS GC worker thread.
This commit is contained in:
Jason Sams
2013-04-09 21:24:42 -07:00
committed by Android Git Automerger
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;
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.");

View File

@ -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);
}

View File

@ -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) {
}

View File

@ -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);
}