Update ImageProcessing test.

Add async filtering.

Change-Id: I4e32a9b1fe9221b09a7d1433b3da11a5e422d911
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index 445751e..c1f0d2a 100644
--- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -18,6 +18,8 @@
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.graphics.BitmapFactory;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -82,11 +84,37 @@
     private boolean mDoingBenchmark;
 
     private TestBase mTest;
+    private int mRunCount;
 
     public void updateDisplay() {
+        mHandler.sendMessage(Message.obtain());
+    }
+
+    private Handler mHandler = new Handler() {
+        // Allow the filter to complete without blocking the UI
+        // thread.  When the message arrives that the op is complete
+        // we will either mark completion or start a new filter if
+        // more work is ready.  Either way, display the result.
+        @Override
+        public void handleMessage(Message msg) {
             mTest.updateBitmap(mBitmapOut);
             mDisplayView.invalidate();
-    }
+
+            boolean doTest = false;
+            synchronized(this) {
+                if (mRunCount > 0) {
+                    mRunCount--;
+                    if (mRunCount > 0) {
+                        doTest = true;
+                    }
+                }
+            }
+            if (doTest) {
+                mTest.runTestSendMessage();
+            }
+        }
+
+    };
 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
         if (fromUser) {
@@ -103,8 +131,18 @@
                 mTest.onBar5Changed(progress);
             }
 
-            mTest.runTest();
-            updateDisplay();
+            boolean doTest = false;
+            synchronized(this) {
+                if (mRunCount == 0) {
+                    doTest = true;
+                    mRunCount = 1;
+                } else {
+                    mRunCount = 2;
+                }
+            }
+            if (doTest) {
+                mTest.runTestSendMessage();
+            }
         }
     }
 
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java
index bb3f2f3..2c4b3ba 100644
--- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java
@@ -45,9 +45,22 @@
     protected Allocation mInPixelsAllocation;
     protected Allocation mInPixelsAllocation2;
     protected Allocation mOutPixelsAllocation;
+    protected ScriptC_msg mMessageScript;
 
     protected ImageProcessingActivity act;
 
+    private class MessageProcessor extends RenderScript.RSMessageHandler {
+        ImageProcessingActivity mAct;
+
+        MessageProcessor(ImageProcessingActivity act) {
+            mAct = act;
+        }
+
+        public void run() {
+            mAct.updateDisplay();
+        }
+    }
+
     // Override to use UI elements
     public void onBar1Changed(int progress) {
     }
@@ -96,6 +109,8 @@
     public final void createBaseTest(ImageProcessingActivity ipact, Bitmap b, Bitmap b2) {
         act = ipact;
         mRS = RenderScript.create(act);
+        mRS.setMessageHandler(new MessageProcessor(act));
+        mMessageScript = new ScriptC_msg(mRS);
         mInPixelsAllocation = Allocation.createFromBitmap(mRS, b,
                                                           Allocation.MipmapControl.MIPMAP_NONE,
                                                           Allocation.USAGE_SCRIPT);
@@ -117,6 +132,11 @@
     public void runTest() {
     }
 
+    final public void runTestSendMessage() {
+        runTest();
+        mMessageScript.invoke_sendMsg();
+    }
+
     public void finish() {
         mRS.finish();
     }
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/msg.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/msg.rs
new file mode 100644
index 0000000..1a19ffc
--- /dev/null
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/msg.rs
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma version(1)
+#pragma rs java_package_name(com.android.rs.image)
+
+void sendMsg() {
+    rsSendToClientBlocking(0);
+}
+