summaryrefslogtreecommitdiff
path: root/graphics/java/android/renderscript/RenderScript.java
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/java/android/renderscript/RenderScript.java')
-rw-r--r--graphics/java/android/renderscript/RenderScript.java284
1 files changed, 254 insertions, 30 deletions
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 66960bcdbebf..33639dc57d4f 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -18,7 +18,9 @@ package android.renderscript;
import java.io.File;
import java.lang.reflect.Field;
+import java.util.concurrent.locks.*;
+import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@@ -83,11 +85,7 @@ public class RenderScript {
native void nContextInitToClient(int con);
native void nContextDeinitToClient(int con);
- /**
- * Name of the file that holds the object cache.
- */
- private static final String CACHE_PATH = "com.android.renderscript.cache";
- static String mCachePath;
+ static File mCacheDir;
/**
* Sets the directory to use as a persistent storage for the
@@ -97,11 +95,40 @@ public class RenderScript {
* @param cacheDir A directory the current process can write to
*/
public static void setupDiskCache(File cacheDir) {
- File f = new File(cacheDir, CACHE_PATH);
- mCachePath = f.getAbsolutePath();
- f.mkdirs();
+ // Defer creation of cache path to nScriptCCreate().
+ mCacheDir = cacheDir;
}
+ /**
+ * ContextType specifies the specific type of context to be created.
+ *
+ */
+ public enum ContextType {
+ /**
+ * NORMAL context, this is the default and what shipping apps should
+ * use.
+ */
+ NORMAL (0),
+
+ /**
+ * DEBUG context, perform extra runtime checks to validate the
+ * kernels and APIs are being used as intended. Get and SetElementAt
+ * will be bounds checked in this mode.
+ */
+ DEBUG (1),
+
+ /**
+ * PROFILE context, Intended to be used once the first time an
+ * application is run on a new device. This mode allows the runtime to
+ * do additional testing and performance tuning.
+ */
+ PROFILE (2);
+
+ int mID;
+ ContextType(int id) {
+ mID = id;
+ }
+ }
// Methods below are wrapped to protect the non-threadsafe
// lockless fifo.
@@ -122,9 +149,9 @@ public class RenderScript {
stencilMin, stencilPref,
samplesMin, samplesPref, samplesQ, dpi);
}
- native int rsnContextCreate(int dev, int ver, int sdkVer);
- synchronized int nContextCreate(int dev, int ver, int sdkVer) {
- return rsnContextCreate(dev, ver, sdkVer);
+ native int rsnContextCreate(int dev, int ver, int sdkVer, int contextType);
+ synchronized int nContextCreate(int dev, int ver, int sdkVer, int contextType) {
+ return rsnContextCreate(dev, ver, sdkVer, contextType);
}
native void rsnContextDestroy(int con);
synchronized void nContextDestroy() {
@@ -157,6 +184,12 @@ public class RenderScript {
rsnContextFinish(mContext);
}
+ native void rsnContextSendMessage(int con, int id, int[] data);
+ synchronized void nContextSendMessage(int id, int[] data) {
+ validate();
+ rsnContextSendMessage(mContext, id, data);
+ }
+
native void rsnContextBindRootScript(int con, int script);
synchronized void nContextBindRootScript(int script) {
validate();
@@ -240,10 +273,10 @@ public class RenderScript {
rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
}
- native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces);
- synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces) {
+ native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
+ synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
validate();
- return rsnTypeCreate(mContext, eid, x, y, z, mips, faces);
+ return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
}
native void rsnTypeGetNativeData(int con, int id, int[] typeData);
synchronized void nTypeGetNativeData(int id, int[] typeData) {
@@ -261,6 +294,14 @@ public class RenderScript {
validate();
return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
}
+
+ native int rsnAllocationCreateBitmapBackedAllocation(int con, int type, int mip, Bitmap bmp, int usage);
+ synchronized int nAllocationCreateBitmapBackedAllocation(int type, int mip, Bitmap bmp, int usage) {
+ validate();
+ return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
+ }
+
+
native int rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
validate();
@@ -289,15 +330,10 @@ public class RenderScript {
validate();
rsnAllocationSyncAll(mContext, alloc, src);
}
- native int rsnAllocationGetSurfaceTextureID(int con, int alloc);
- synchronized int nAllocationGetSurfaceTextureID(int alloc) {
+ native Surface rsnAllocationGetSurface(int con, int alloc);
+ synchronized Surface nAllocationGetSurface(int alloc) {
validate();
- return rsnAllocationGetSurfaceTextureID(mContext, alloc);
- }
- native void rsnAllocationGetSurfaceTextureID2(int con, int alloc, SurfaceTexture st);
- synchronized void nAllocationGetSurfaceTextureID2(int alloc, SurfaceTexture st) {
- validate();
- rsnAllocationGetSurfaceTextureID2(mContext, alloc, st);
+ return rsnAllocationGetSurface(mContext, alloc);
}
native void rsnAllocationSetSurface(int con, int alloc, Surface sur);
synchronized void nAllocationSetSurface(int alloc, Surface sur) {
@@ -401,6 +437,46 @@ public class RenderScript {
rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
}
+ native void rsnAllocationData3D(int con,
+ int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
+ int dstMip,
+ int width, int height, int depth,
+ int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
+ int srcMip);
+ synchronized void nAllocationData3D(int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
+ int dstMip,
+ int width, int height, int depth,
+ int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
+ int srcMip) {
+ validate();
+ rsnAllocationData3D(mContext,
+ dstAlloc, dstXoff, dstYoff, dstZoff,
+ dstMip, width, height, depth,
+ srcAlloc, srcXoff, srcYoff, srcZoff, srcMip);
+ }
+
+ native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes);
+ synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes) {
+ validate();
+ rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
+ }
+ native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes);
+ synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes) {
+ validate();
+ rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
+ }
+ native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes);
+ synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes) {
+ validate();
+ rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
+ }
+ native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes);
+ synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes) {
+ validate();
+ rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
+ }
+
+
native void rsnAllocationRead(int con, int id, byte[] d);
synchronized void nAllocationRead(int id, byte[] d) {
validate();
@@ -432,11 +508,6 @@ public class RenderScript {
validate();
rsnAllocationResize1D(mContext, id, dimX);
}
- native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
- synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
- validate();
- rsnAllocationResize2D(mContext, id, dimX, dimY);
- }
native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
@@ -503,6 +574,10 @@ public class RenderScript {
}
native void rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params);
native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
+ native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout, byte[] params,
+ int xstart, int xend, int ystart, int yend, int zstart, int zend);
+ native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout,
+ int xstart, int xend, int ystart, int yend, int zstart, int zend);
synchronized void nScriptForEach(int id, int slot, int ain, int aout, byte[] params) {
validate();
if (params == null) {
@@ -511,36 +586,75 @@ public class RenderScript {
rsnScriptForEach(mContext, id, slot, ain, aout, params);
}
}
+
+ synchronized void nScriptForEachClipped(int id, int slot, int ain, int aout, byte[] params,
+ int xstart, int xend, int ystart, int yend, int zstart, int zend) {
+ validate();
+ if (params == null) {
+ rsnScriptForEachClipped(mContext, id, slot, ain, aout, xstart, xend, ystart, yend, zstart, zend);
+ } else {
+ rsnScriptForEachClipped(mContext, id, slot, ain, aout, params, xstart, xend, ystart, yend, zstart, zend);
+ }
+ }
+
native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
validate();
rsnScriptInvokeV(mContext, id, slot, params);
}
+
native void rsnScriptSetVarI(int con, int id, int slot, int val);
synchronized void nScriptSetVarI(int id, int slot, int val) {
validate();
rsnScriptSetVarI(mContext, id, slot, val);
}
+ native int rsnScriptGetVarI(int con, int id, int slot);
+ synchronized int nScriptGetVarI(int id, int slot) {
+ validate();
+ return rsnScriptGetVarI(mContext, id, slot);
+ }
+
native void rsnScriptSetVarJ(int con, int id, int slot, long val);
synchronized void nScriptSetVarJ(int id, int slot, long val) {
validate();
rsnScriptSetVarJ(mContext, id, slot, val);
}
+ native long rsnScriptGetVarJ(int con, int id, int slot);
+ synchronized long nScriptGetVarJ(int id, int slot) {
+ validate();
+ return rsnScriptGetVarJ(mContext, id, slot);
+ }
+
native void rsnScriptSetVarF(int con, int id, int slot, float val);
synchronized void nScriptSetVarF(int id, int slot, float val) {
validate();
rsnScriptSetVarF(mContext, id, slot, val);
}
+ native float rsnScriptGetVarF(int con, int id, int slot);
+ synchronized float nScriptGetVarF(int id, int slot) {
+ validate();
+ return rsnScriptGetVarF(mContext, id, slot);
+ }
native void rsnScriptSetVarD(int con, int id, int slot, double val);
synchronized void nScriptSetVarD(int id, int slot, double val) {
validate();
rsnScriptSetVarD(mContext, id, slot, val);
}
+ native double rsnScriptGetVarD(int con, int id, int slot);
+ synchronized double nScriptGetVarD(int id, int slot) {
+ validate();
+ return rsnScriptGetVarD(mContext, id, slot);
+ }
native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
validate();
rsnScriptSetVarV(mContext, id, slot, val);
}
+ native void rsnScriptGetVarV(int con, int id, int slot, byte[] val);
+ synchronized void nScriptGetVarV(int id, int slot, byte[] val) {
+ validate();
+ rsnScriptGetVarV(mContext, id, slot, val);
+ }
native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
int e, int[] dims);
synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
@@ -690,6 +804,8 @@ public class RenderScript {
int mContext;
@SuppressWarnings({"FieldCanBeLocal"})
MessageThread mMessageThread;
+ GCThread mGCThread;
+
Element mElement_U8;
Element mElement_I8;
@@ -772,6 +888,9 @@ public class RenderScript {
Sampler mSampler_WRAP_NEAREST;
Sampler mSampler_WRAP_LINEAR;
Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
+ Sampler mSampler_MIRRORED_REPEAT_NEAREST;
+ Sampler mSampler_MIRRORED_REPEAT_LINEAR;
+ Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
@@ -816,6 +935,17 @@ public class RenderScript {
}
/**
+ * Place a message into the message queue to be sent back to the message
+ * handler once all previous commands have been executed.
+ *
+ * @param id
+ * @param data
+ */
+ public void sendMessage(int id, int[] data) {
+ nContextSendMessage(id, data);
+ }
+
+ /**
* Runtime error base class. An application should derive from this class
* if it wishes to install an error handler. When errors occur at runtime
* the fields in this class will be filled and the run method called.
@@ -887,6 +1017,7 @@ public class RenderScript {
static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
static final int RS_MESSAGE_TO_CLIENT_USER = 4;
+ static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5;
static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
@@ -939,12 +1070,18 @@ public class RenderScript {
mRS.mErrorCallback.mErrorNum = subID;
mRS.mErrorCallback.run();
} else {
+ android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
// Do not throw here. In these cases, we do not have
// a fatal error.
}
continue;
}
+ if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
+ Allocation.sendBufferNotification(subID);
+ continue;
+ }
+
// 2: teardown.
// But we want to avoid starving other threads during
// teardown by yielding until the next line in the destructor
@@ -958,6 +1095,60 @@ public class RenderScript {
}
}
+ static class GCThread extends Thread {
+ RenderScript mRS;
+ boolean mRun = true;
+
+ long currentSize = 0;
+ long targetSize; // call System.gc after 512MB of allocs
+
+ final Lock lock = new ReentrantLock();
+ final Condition cond = lock.newCondition();
+
+ GCThread(RenderScript rs) {
+ super("RSGCThread");
+ mRS = rs;
+
+ }
+
+ public void run() {
+ ActivityManager am = (ActivityManager)mRS.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
+ ActivityManager.MemoryInfo meminfo = new ActivityManager.MemoryInfo();
+ am.getMemoryInfo(meminfo);
+ targetSize = (long)(meminfo.totalMem * .5f);
+
+ while(mRun) {
+ System.gc();
+ lock.lock();
+ try {
+ cond.awaitUninterruptibly();
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ Log.d(LOG_TAG, "GCThread exiting.");
+ }
+
+ public synchronized void addAllocSize(long bytes) {
+ currentSize += bytes;
+ if (currentSize >= targetSize) {
+ lock.lock();
+ try {
+ cond.signal();
+ } finally {
+ lock.unlock();
+ }
+ }
+ }
+
+ public synchronized void removeAllocSize(long bytes) {
+ currentSize -= bytes;
+ }
+
+ }
+
+
RenderScript(Context ctx) {
if (ctx != null) {
mApplicationContext = ctx.getApplicationContext();
@@ -974,22 +1165,40 @@ public class RenderScript {
}
/**
+ * @hide
+ */
+ public static RenderScript create(Context ctx, int sdkVersion) {
+ 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.
*
* @hide
* @param ctx The context.
* @return RenderScript
*/
- public static RenderScript create(Context ctx, int sdkVersion) {
+ public static RenderScript create(Context ctx, int sdkVersion, ContextType ct) {
RenderScript rs = new RenderScript(ctx);
rs.mDev = rs.nDeviceCreate();
- rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion);
+ rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion, ct.mID);
if (rs.mContext == 0) {
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;
}
@@ -1000,8 +1209,20 @@ public class RenderScript {
* @return RenderScript
*/
public static RenderScript create(Context ctx) {
+ return create(ctx, ContextType.NORMAL);
+ }
+
+ /**
+ * Create a basic RenderScript context.
+ *
+ *
+ * @param ctx The context.
+ * @param ct The type of context to be created.
+ * @return RenderScript
+ */
+ public static RenderScript create(Context ctx, ContextType ct) {
int v = ctx.getApplicationInfo().targetSdkVersion;
- return create(ctx, v);
+ return create(ctx, v, ct);
}
/**
@@ -1032,8 +1253,11 @@ public class RenderScript {
validate();
nContextDeinitToClient(mContext);
mMessageThread.mRun = false;
+ mGCThread.mRun = false;
+ mGCThread.addAllocSize(0);
try {
mMessageThread.join();
+ mGCThread.join();
} catch(InterruptedException e) {
}