Lighten grain and make live preview gpu friendly.

Preview now does yuv->rgb using the intrinsic
script. Grain uses a smaller wrapped rand buffer
so it is not just rand test.

bug 7216044

Change-Id: If74eedc7d3cf264895133671edc546af9b1527f2
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Grain.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Grain.java
index cd54c2e..8618cc8 100644
--- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Grain.java
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Grain.java
@@ -45,19 +45,40 @@
         mScript.set_gNoiseStrength(s);
     }
 
+    private int findHighBit(int v) {
+        int bit = 0;
+        while (v > 1) {
+            bit++;
+            v >>= 1;
+        }
+        return bit;
+    }
+
+
     public void createTest(android.content.res.Resources res) {
         int width = mInPixelsAllocation.getType().getX();
         int height = mInPixelsAllocation.getType().getY();
 
+        int noiseW = findHighBit(width);
+        int noiseH = findHighBit(height);
+        if (noiseW > 9) {
+            noiseW = 9;
+        }
+        if (noiseH > 9) {
+            noiseH = 9;
+        }
+        noiseW = 1 << noiseW;
+        noiseH = 1 << noiseH;
+
         Type.Builder tb = new Type.Builder(mRS, Element.U8(mRS));
-        tb.setX(width);
-        tb.setY(height);
+        tb.setX(noiseW);
+        tb.setY(noiseH);
         mNoise = Allocation.createTyped(mRS, tb.create());
         mNoise2 = Allocation.createTyped(mRS, tb.create());
 
         mScript = new ScriptC_grain(mRS, res, R.raw.grain);
-        mScript.set_gWidth(width);
-        mScript.set_gHeight(height);
+        mScript.set_gWMask(noiseW - 1);
+        mScript.set_gHMask(noiseH - 1);
         mScript.set_gNoiseStrength(0.5f);
         mScript.set_gBlendSource(mNoise);
         mScript.set_gNoise(mNoise2);
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/grain.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/grain.rs
index ea42524..c8531f3 100644
--- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/grain.rs
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/grain.rs
@@ -38,15 +38,15 @@
  *  1  2  1
  */
 
-int32_t gWidth;
-int32_t gHeight;
+int32_t gWMask;
+int32_t gHMask;
 
 rs_allocation gBlendSource;
 void blend9(uchar *out, uint32_t x, uint32_t y) {
-    uint32_t x1 = min((int32_t)x+1, (int32_t)(gWidth -1));
-    uint32_t x2 = max((int32_t)x-1, (int32_t)0);
-    uint32_t y1 = min((int32_t)y+1, (int32_t)(gHeight -1));
-    uint32_t y2 = max((int32_t)y-1, (int32_t)0);
+    uint32_t x1 = (x-1) & gWMask;
+    uint32_t x2 = (x+1) & gWMask;
+    uint32_t y1 = (y-1) & gHMask;
+    uint32_t y2 = (y+1) & gHMask;
 
     uint p00 = 56 *  rsGetElementAt_uchar(gBlendSource, x1, y1);
     uint p01 = 114 * rsGetElementAt_uchar(gBlendSource, x, y1);
@@ -78,7 +78,7 @@
 rs_allocation gNoise;
 void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
     float4 ip = convert_float4(*in);
-    float pnoise = (float) rsGetElementAt_uchar(gNoise, x, y);
+    float pnoise = (float) rsGetElementAt_uchar(gNoise, x & gWMask, y & gHMask);
 
     float energy_level = ip.r + ip.g + ip.b;
     float energy_mask = (28.f - sqrt(energy_level)) * 0.03571f;
diff --git a/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java b/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
index f21331f..89eec2c 100644
--- a/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
+++ b/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
@@ -305,8 +305,7 @@
 
             long t1 = java.lang.System.currentTimeMillis();
 
-            mFilterYuv.execute(data);
-            mFilterYuv.copyOut(mCallbackBitmap);
+            mFilterYuv.execute(data, mCallbackBitmap);
 
             long t2 = java.lang.System.currentTimeMillis();
             mTiming[mTimingSlot++] = t2 - t1;
diff --git a/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/RsYuv.java b/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/RsYuv.java
index cced198..978ae12 100644
--- a/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/RsYuv.java
+++ b/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/RsYuv.java
@@ -42,6 +42,7 @@
     private Allocation mAllocationOut;
     private Allocation mAllocationIn;
     private ScriptC_yuv mScript;
+    private ScriptIntrinsicYuvToRGB mYuv;
 
     RsYuv(RenderScript rs, Resources res, int width, int height) {
         mHeight = height;
@@ -50,6 +51,8 @@
         mScript = new ScriptC_yuv(mRS, res, R.raw.yuv);
         mScript.invoke_setSize(mWidth, mHeight);
 
+        mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
+
         Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
         tb.setX(mWidth);
         tb.setY(mHeight);
@@ -58,34 +61,16 @@
         mAllocationIn = Allocation.createSized(rs, Element.U8(mRS), (mHeight * mWidth) +
                                                ((mHeight / 2) * (mWidth / 2) * 2));
 
-        mScript.bind_gYuvIn(mAllocationIn);
+        mYuv.setInput(mAllocationIn);
     }
 
     private long mTiming[] = new long[50];
     private int mTimingSlot = 0;
 
-    void execute(byte[] yuv) {
+    void execute(byte[] yuv, Bitmap b) {
         mAllocationIn.copyFrom(yuv);
-        mRS.finish();
-
-        long t1 = java.lang.System.currentTimeMillis();
-        mScript.forEach_root(mAllocationOut);
-        mRS.finish();
-        long t2 = java.lang.System.currentTimeMillis();
-
-        mTiming[mTimingSlot++] = t2 - t1;
-        if (mTimingSlot >= mTiming.length) {
-            float total = 0;
-            for (int i=0; i<mTiming.length; i++) {
-                total += (float)mTiming[i];
-            }
-            total /= mTiming.length;
-            Log.e("yuv", "core time = " + total);
-            mTimingSlot = 0;
-        }
-    }
-
-    void copyOut(Bitmap b) {
+        mYuv.forEach(mAllocationOut);
+        mScript.forEach_root(mAllocationOut, mAllocationOut);
         mAllocationOut.copyTo(b);
     }
 
diff --git a/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/yuv.rs b/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/yuv.rs
index 6057eff..884812d 100644
--- a/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/yuv.rs
+++ b/tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/yuv.rs
@@ -3,8 +3,6 @@
 #pragma rs java_package_name(com.android.rs.livepreview)
 #pragma rs_fp_relaxed
 
-uchar *gYuvIn;
-
 static int gWidth;
 static int gHeight;
 static uchar crossProcess_tableR[256];
@@ -80,13 +78,9 @@
     return convert_uchar4(c);
 }
 
-void root(uchar4 *out, uint32_t x, uint32_t y) {
-    uchar Y = gYuvIn[(y * gWidth) + x];
-    uchar *uv = &gYuvIn[gWidth * gHeight];
-    uv += (((x>>1)<<1) + (y>>1) * gWidth);
-
-    uchar4 p = rsYuvToRGBA_uchar4(Y, uv[1], uv[0]);
-    p = crossProcess_i(p);
+void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
+    uchar4 p;
+    p = crossProcess_i(*in);
     p = vignette(p, x, y);
 
     out->rgba = p;