Merge "1. Add Context to a RenderScript or RenderScriptGL instance. This is to allow RenderScript to better interact with the Android environment. E.g., per-app cache. 2. Plumbing, testing. 3. Added getApplicationContext in RenderScript.java."

This commit is contained in:
Shih-wei Liao
2010-12-14 11:32:43 -08:00
committed by Android (Google) Code Review
16 changed files with 82 additions and 58 deletions

View File

@@ -119,7 +119,7 @@ public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback
}
public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
RenderScriptGL rs = new RenderScriptGL(sc);
RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc);
setRenderScriptGL(rs);
return rs;
}
@@ -137,4 +137,3 @@ public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback
return mRS;
}
}

View File

@@ -18,6 +18,7 @@ package android.renderscript;
import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Config;
@@ -42,9 +43,9 @@ public class RenderScript {
@SuppressWarnings({"UnusedDeclaration", "deprecation"})
static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
private Context mApplicationContext;
/*
/*
* We use a class initializer to allow the native code to cache some
* field offsets.
*/
@@ -416,9 +417,9 @@ public class RenderScript {
synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
rsnScriptCSetScript(mContext, script, offset, length);
}
native int rsnScriptCCreate(int con, String val);
synchronized int nScriptCCreate(String val) {
return rsnScriptCCreate(mContext, val);
native int rsnScriptCCreate(int con, String val, String cacheDir);
synchronized int nScriptCCreate(String resName, String cacheDir) {
return rsnScriptCCreate(mContext, resName, cacheDir);
}
native void rsnSamplerBegin(int con);
@@ -776,17 +777,27 @@ public class RenderScript {
}
}
RenderScript() {
RenderScript(Context ctx) {
mApplicationContext = ctx.getApplicationContext();
}
/**
* Gets the application context associated with the RenderScript context.
*
* @return The application context.
*/
public final Context getApplicationContext() {
return mApplicationContext;
}
/**
* Create a basic RenderScript context.
*
*
* @param ctx The context.
* @return RenderScript
*/
public static RenderScript create() {
RenderScript rs = new RenderScript();
public static RenderScript create(Context ctx) {
RenderScript rs = new RenderScript(ctx);
rs.mDev = rs.nDeviceCreate();
rs.mContext = rs.nContextCreate(rs.mDev, 0);

View File

@@ -18,6 +18,7 @@ package android.renderscript;
import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@@ -168,10 +169,11 @@ public class RenderScriptGL extends RenderScript {
/**
* Construct a new RenderScriptGL context.
*
*
* @param ctx The context.
* @param sc The desired format of the primart rendering surface.
*/
public RenderScriptGL(SurfaceConfig sc) {
public RenderScriptGL(Context ctx, SurfaceConfig sc) {
super(ctx);
mSurfaceConfig = new SurfaceConfig(sc);
mSurface = null;
@@ -304,5 +306,3 @@ public class RenderScriptGL extends RenderScript {
}
}

View File

@@ -16,9 +16,11 @@
package android.renderscript;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
@@ -76,6 +78,7 @@ public class ScriptC extends Script {
rs.nScriptCBegin();
rs.nScriptCSetScript(pgm, 0, pgmLength);
Log.v(TAG, "Create script for resource = " + resources.getResourceName(resourceID));
return rs.nScriptCCreate(resources.getResourceName(resourceID));
String cacheDir = rs.getApplicationContext().getCacheDir().toString();
return rs.nScriptCCreate(resources.getResourceName(resourceID), cacheDir);
}
}

View File

@@ -902,11 +902,15 @@ exit:
}
static jint
nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con, jstring resName)
nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con, jstring resName, jstring cacheDir)
{
LOG_API("nScriptCCreate, con(%p)", con);
const char* resNameUTF = _env->GetStringUTFChars(resName, NULL);
return (jint)rsScriptCCreate(con, resNameUTF);
const char* cacheDirUTF = _env->GetStringUTFChars(cacheDir, NULL);
jint i = (jint)rsScriptCCreate(con, resNameUTF, cacheDirUTF);
_env->ReleaseStringUTFChars(resName, resNameUTF);
_env->ReleaseStringUTFChars(cacheDir, cacheDirUTF);
return i;
}
// ---------------------------------------------------------------------------
@@ -1297,7 +1301,7 @@ static JNINativeMethod methods[] = {
{"rsnScriptCBegin", "(I)V", (void*)nScriptCBegin },
{"rsnScriptCSetScript", "(I[BII)V", (void*)nScriptCSetScript },
{"rsnScriptCCreate", "(ILjava/lang/String;)I", (void*)nScriptCCreate },
{"rsnScriptCCreate", "(ILjava/lang/String;Ljava/lang/String;)I", (void*)nScriptCCreate },
{"rsnProgramStoreBegin", "(III)V", (void*)nProgramStoreBegin },
{"rsnProgramStoreDepthFunc", "(II)V", (void*)nProgramStoreDepthFunc },
@@ -1372,4 +1376,3 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
bail:
return result;
}

View File

@@ -362,7 +362,7 @@ public class ImageProcessingActivity extends Activity
}
private void createScript() {
mRS = RenderScript.create();
mRS = RenderScript.create(this);
mRS.setMessageHandler(new FilterCallback());
mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,

View File

@@ -16,6 +16,7 @@
package com.android.rs.test;
import android.content.Context;
import android.content.res.Resources;
import android.renderscript.*;
import android.util.Log;
@@ -28,8 +29,10 @@ import java.util.TimerTask;
public class RSTestCore {
int mWidth;
int mHeight;
Context mCtx;
public RSTestCore() {
public RSTestCore(Context ctx) {
mCtx = ctx;
}
private Resources mRes;
@@ -61,10 +64,10 @@ public class RSTestCore {
unitTests = new ArrayList<UnitTest>();
unitTests.add(new UT_primitives(this, mRes));
unitTests.add(new UT_rsdebug(this, mRes));
unitTests.add(new UT_rstypes(this, mRes));
unitTests.add(new UT_fp_mad(this, mRes));
unitTests.add(new UT_primitives(this, mRes, mCtx));
unitTests.add(new UT_rsdebug(this, mRes, mCtx));
unitTests.add(new UT_rstypes(this, mRes, mCtx));
unitTests.add(new UT_fp_mad(this, mRes, mCtx));
/*
unitTests.add(new UnitTest(null, "<Pass>", 1));
unitTests.add(new UnitTest());

View File

@@ -41,8 +41,11 @@ import android.view.MotionEvent;
public class RSTestView extends RSSurfaceView {
private Context mCtx;
public RSTestView(Context context) {
super(context);
mCtx = context;
//setFocusable(true);
}
@@ -55,7 +58,7 @@ public class RSTestView extends RSSurfaceView {
RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
mRS = createRenderScriptGL(sc);
mRS.setSurface(holder, w, h);
mRender = new RSTestCore();
mRender = new RSTestCore(mCtx);
mRender.init(mRS, getResources(), w, h);
}
}
@@ -92,5 +95,3 @@ public class RSTestView extends RSSurfaceView {
return ret;
}
}

View File

@@ -16,19 +16,20 @@
package com.android.rs.test;
import android.content.Context;
import android.content.res.Resources;
import android.renderscript.*;
public class UT_fp_mad extends UnitTest {
private Resources mRes;
protected UT_fp_mad(RSTestCore rstc, Resources res) {
super(rstc, "Fp_Mad");
protected UT_fp_mad(RSTestCore rstc, Resources res, Context ctx) {
super(rstc, "Fp_Mad", ctx);
mRes = res;
}
public void run() {
RenderScript pRS = RenderScript.create();
RenderScript pRS = RenderScript.create(mCtx);
ScriptC_fp_mad s = new ScriptC_fp_mad(pRS, mRes, R.raw.fp_mad);
pRS.setMessageHandler(mRsMessage);
s.invoke_fp_mad_test(0, 0);
@@ -37,4 +38,3 @@ public class UT_fp_mad extends UnitTest {
pRS.destroy();
}
}

View File

@@ -16,14 +16,15 @@
package com.android.rs.test;
import android.content.Context;
import android.content.res.Resources;
import android.renderscript.*;
public class UT_primitives extends UnitTest {
private Resources mRes;
protected UT_primitives(RSTestCore rstc, Resources res) {
super(rstc, "Primitives");
protected UT_primitives(RSTestCore rstc, Resources res, Context ctx) {
super(rstc, "Primitives", ctx);
mRes = res;
}
@@ -87,7 +88,7 @@ public class UT_primitives extends UnitTest {
}
public void run() {
RenderScript pRS = RenderScript.create();
RenderScript pRS = RenderScript.create(mCtx);
ScriptC_primitives s = new ScriptC_primitives(pRS, mRes, R.raw.primitives);
pRS.setMessageHandler(mRsMessage);
if (!initializeGlobals(s)) {
@@ -101,4 +102,3 @@ public class UT_primitives extends UnitTest {
pRS.destroy();
}
}

View File

@@ -16,19 +16,20 @@
package com.android.rs.test;
import android.content.Context;
import android.content.res.Resources;
import android.renderscript.*;
public class UT_rsdebug extends UnitTest {
private Resources mRes;
protected UT_rsdebug(RSTestCore rstc, Resources res) {
super(rstc, "rsDebug");
protected UT_rsdebug(RSTestCore rstc, Resources res, Context ctx) {
super(rstc, "rsDebug", ctx);
mRes = res;
}
public void run() {
RenderScript pRS = RenderScript.create();
RenderScript pRS = RenderScript.create(mCtx);
ScriptC_rsdebug s = new ScriptC_rsdebug(pRS, mRes, R.raw.rsdebug);
pRS.setMessageHandler(mRsMessage);
s.invoke_test_rsdebug(0, 0);
@@ -37,4 +38,3 @@ public class UT_rsdebug extends UnitTest {
pRS.destroy();
}
}

View File

@@ -16,19 +16,20 @@
package com.android.rs.test;
import android.content.Context;
import android.content.res.Resources;
import android.renderscript.*;
public class UT_rstypes extends UnitTest {
private Resources mRes;
protected UT_rstypes(RSTestCore rstc, Resources res) {
super(rstc, "rsTypes");
protected UT_rstypes(RSTestCore rstc, Resources res, Context ctx) {
super(rstc, "rsTypes", ctx);
mRes = res;
}
public void run() {
RenderScript pRS = RenderScript.create();
RenderScript pRS = RenderScript.create(mCtx);
ScriptC_rstypes s = new ScriptC_rstypes(pRS, mRes, R.raw.rstypes);
pRS.setMessageHandler(mRsMessage);
s.invoke_test_rstypes(0, 0);
@@ -37,4 +38,3 @@ public class UT_rstypes extends UnitTest {
pRS.destroy();
}
}

View File

@@ -15,6 +15,7 @@
*/
package com.android.rs.test;
import android.content.Context;
import android.renderscript.RenderScript.RSMessageHandler;
import android.util.Log;
@@ -24,6 +25,7 @@ public class UnitTest extends Thread {
private ScriptField_ListAllocs_s.Item mItem;
private RSTestCore mRSTC;
private boolean msgHandled;
protected Context mCtx;
/* These constants must match those in shared.rsh */
public static final int RS_MSG_TEST_PASSED = 100;
@@ -32,25 +34,26 @@ public class UnitTest extends Thread {
private static int numTests = 0;
public int testID;
protected UnitTest(RSTestCore rstc, String n, int initResult) {
protected UnitTest(RSTestCore rstc, String n, int initResult, Context ctx) {
super();
mRSTC = rstc;
name = n;
msgHandled = false;
mCtx = ctx;
result = initResult;
testID = numTests++;
}
protected UnitTest(RSTestCore rstc, String n) {
this(rstc, n, 0);
protected UnitTest(RSTestCore rstc, String n, Context ctx) {
this(rstc, n, 0, ctx);
}
protected UnitTest(RSTestCore rstc) {
this (rstc, "<Unknown>");
protected UnitTest(RSTestCore rstc, Context ctx) {
this (rstc, "<Unknown>", ctx);
}
protected UnitTest() {
this (null);
protected UnitTest(Context ctx) {
this (null, ctx);
}
protected RSMessageHandler mRsMessage = new RSMessageHandler() {
@@ -101,4 +104,3 @@ public class UnitTest extends Thread {
}
}
}

View File

@@ -322,6 +322,7 @@ ScriptCSetText {
ScriptCCreate {
param const char * resName
param const char * cacheDir
ret RsScript
}

View File

@@ -402,7 +402,7 @@ static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) {
extern const char rs_runtime_lib_bc[];
extern unsigned rs_runtime_lib_bc_size;
void ScriptCState::runCompiler(Context *rsc, ScriptC *s, const char *resName) {
void ScriptCState::runCompiler(Context *rsc, ScriptC *s, const char *resName, const char *cacheDir) {
{
s->mBccScript = bccCreateScript();
s->mEnviroment.mIsThreadable = true;
@@ -413,7 +413,8 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s, const char *resName) {
if (bccReadBC(s->mBccScript,
s->mEnviroment.mScriptText,
s->mEnviroment.mScriptTextLength,
resName) >= 0) {
resName,
cacheDir) >= 0) {
//bccLinkBC(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
bccCompileBC(s->mBccScript);
} else {
@@ -534,7 +535,7 @@ void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) {
ss->mScript->mEnviroment.mScriptTextLength = len;
}
RsScript rsi_ScriptCCreate(Context * rsc, const char *resName)
RsScript rsi_ScriptCCreate(Context * rsc, const char *resName, const char *cacheDir)
{
ScriptCState *ss = &rsc->mScriptC;
@@ -542,7 +543,7 @@ RsScript rsi_ScriptCCreate(Context * rsc, const char *resName)
ss->mScript.clear();
s->incUserRef();
ss->runCompiler(rsc, s.get(), resName);
ss->runCompiler(rsc, s.get(), resName, cacheDir);
ss->clear(rsc);
return s.get();
}

View File

@@ -83,7 +83,7 @@ public:
void init(Context *rsc);
void clear(Context *rsc);
void runCompiler(Context *rsc, ScriptC *s, const char *resName);
void runCompiler(Context *rsc, ScriptC *s, const char *resName, const char *cacheDir);
struct SymbolTable_t {
const char * mName;