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.

Change-Id: I85edeebe38825e20b2e86f4f4815689dfc332ef9
diff --git a/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index 09654ab..5de09f7 100644
--- a/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -362,7 +362,7 @@
     }
 
     private void createScript() {
-        mRS = RenderScript.create();
+        mRS = RenderScript.create(this);
         mRS.setMessageHandler(new FilterCallback());
 
         mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
diff --git a/java/tests/src/com/android/rs/test/RSTestCore.java b/java/tests/src/com/android/rs/test/RSTestCore.java
index c1f652f..265e1d6 100644
--- a/java/tests/src/com/android/rs/test/RSTestCore.java
+++ b/java/tests/src/com/android/rs/test/RSTestCore.java
@@ -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 @@
 public class RSTestCore {
     int mWidth;
     int mHeight;
+    Context mCtx;
 
-    public RSTestCore() {
+    public RSTestCore(Context ctx) {
+        mCtx = ctx;
     }
 
     private Resources mRes;
@@ -61,10 +64,10 @@
 
         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());
diff --git a/java/tests/src/com/android/rs/test/RSTestView.java b/java/tests/src/com/android/rs/test/RSTestView.java
index 2f7542d..368f286 100644
--- a/java/tests/src/com/android/rs/test/RSTestView.java
+++ b/java/tests/src/com/android/rs/test/RSTestView.java
@@ -41,8 +41,11 @@
 
 public class RSTestView extends RSSurfaceView {
 
+    private Context mCtx;
+
     public RSTestView(Context context) {
         super(context);
+        mCtx = context;
         //setFocusable(true);
     }
 
@@ -55,7 +58,7 @@
             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 @@
         return ret;
     }
 }
-
-
diff --git a/java/tests/src/com/android/rs/test/UT_fp_mad.java b/java/tests/src/com/android/rs/test/UT_fp_mad.java
index 409192b..f2c91af 100644
--- a/java/tests/src/com/android/rs/test/UT_fp_mad.java
+++ b/java/tests/src/com/android/rs/test/UT_fp_mad.java
@@ -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 @@
         pRS.destroy();
     }
 }
-
diff --git a/java/tests/src/com/android/rs/test/UT_primitives.java b/java/tests/src/com/android/rs/test/UT_primitives.java
index 6e0859a..b7a65a5 100644
--- a/java/tests/src/com/android/rs/test/UT_primitives.java
+++ b/java/tests/src/com/android/rs/test/UT_primitives.java
@@ -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 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 @@
         pRS.destroy();
     }
 }
-
diff --git a/java/tests/src/com/android/rs/test/UT_rsdebug.java b/java/tests/src/com/android/rs/test/UT_rsdebug.java
index df31f98..0614b1a 100644
--- a/java/tests/src/com/android/rs/test/UT_rsdebug.java
+++ b/java/tests/src/com/android/rs/test/UT_rsdebug.java
@@ -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 @@
         pRS.destroy();
     }
 }
-
diff --git a/java/tests/src/com/android/rs/test/UT_rstypes.java b/java/tests/src/com/android/rs/test/UT_rstypes.java
index d1232ce..74211c8 100644
--- a/java/tests/src/com/android/rs/test/UT_rstypes.java
+++ b/java/tests/src/com/android/rs/test/UT_rstypes.java
@@ -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 @@
         pRS.destroy();
     }
 }
-
diff --git a/java/tests/src/com/android/rs/test/UnitTest.java b/java/tests/src/com/android/rs/test/UnitTest.java
index 8923a19..a7722c7 100644
--- a/java/tests/src/com/android/rs/test/UnitTest.java
+++ b/java/tests/src/com/android/rs/test/UnitTest.java
@@ -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 @@
     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 @@
     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 @@
         }
     }
 }
-
diff --git a/rs.spec b/rs.spec
index 9f817b6..5daba08 100644
--- a/rs.spec
+++ b/rs.spec
@@ -322,6 +322,7 @@
 
 ScriptCCreate {
         param const char * resName
+        param const char * cacheDir
 	ret RsScript
 	}
 
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index b3dbf11..507430d 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -402,7 +402,7 @@
 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 @@
         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 @@
     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 @@
     ss->mScript.clear();
     s->incUserRef();
 
-    ss->runCompiler(rsc, s.get(), resName);
+    ss->runCompiler(rsc, s.get(), resName, cacheDir);
     ss->clear(rsc);
     return s.get();
 }
diff --git a/rsScriptC.h b/rsScriptC.h
index ab2db5c..a714132 100644
--- a/rsScriptC.h
+++ b/rsScriptC.h
@@ -83,7 +83,7 @@
     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;