Add test for invoke/kernel synchronization of global variables.

Bug: 8437564

Change-Id: Icd6378398d6ab6067498318ee7808d0783e47bf5
diff --git a/tests/src/android/renderscript/cts/global_sync.rs b/tests/src/android/renderscript/cts/global_sync.rs
new file mode 100644
index 0000000..c947fa2
--- /dev/null
+++ b/tests/src/android/renderscript/cts/global_sync.rs
@@ -0,0 +1,32 @@
+#pragma version(1)
+#pragma rs java_package_name(android.renderscript.cts)
+
+int gInt;
+static int sInt;
+
+rs_allocation aFailed;
+
+void test_global(int expected) {
+    if (gInt != expected) {
+        rsSetElementAt_uchar(aFailed, 1, 0);
+    }
+}
+
+void test_static_global(int expected) {
+    if (sInt != expected) {
+        rsSetElementAt_uchar(aFailed, 1, 0);
+    }
+}
+
+void __attribute__((kernel)) write_global(int ain, uint32_t x) {
+    if (x == 0) {
+        gInt = ain;
+    }
+}
+
+void __attribute__((kernel)) write_static_global(int ain, uint32_t x) {
+    if (x == 0) {
+        sInt = ain;
+    }
+}
+
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/GlobalSync.java b/tests/tests/renderscript/src/android/renderscript/cts/GlobalSync.java
new file mode 100644
index 0000000..f895661
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/GlobalSync.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+package android.renderscript.cts;
+
+import android.renderscript.Allocation;
+import android.renderscript.Element;
+import android.renderscript.RenderScript;
+import android.renderscript.Type;
+
+public class GlobalSync extends RSBaseCompute {
+    Allocation AFailed;
+    int [] Failed;
+    Allocation AIn;
+
+    protected void setupGlobalSync(RenderScript mRS, ScriptC_global_sync gs, int v) {
+        Type.Builder typeBuilder = new Type.Builder(mRS, Element.I32(mRS));
+        Type t = typeBuilder.setX(1).create();
+
+        AFailed = Allocation.createTyped(mRS, t);
+        Failed = new int [1];
+        Failed[0] = 0;
+        AFailed.copyFrom(Failed);
+        gs.set_aFailed(AFailed);
+
+        AIn = Allocation.createTyped(mRS, t);
+        int [] In = new int [1];
+        In[0] = v;
+        AIn.copyFrom(In);
+
+    }
+
+    /**
+     * Test whether we are properly synchronizing extern global data
+     * when dealing with mixed kernels/invokables.
+     */
+    public void testGlobalSync() {
+        ScriptC_global_sync gs = new ScriptC_global_sync(mRS);
+
+        int v = 7;
+        setupGlobalSync(mRS, gs, v);
+        gs.forEach_write_global(AIn);
+        gs.invoke_test_global(v);
+
+        AFailed.copyTo(Failed);
+        if (Failed[0] != 0) {
+            FoundError = true;
+        }
+
+        gs.destroy();
+        checkForErrors();
+    }
+
+    /**
+     * Test whether we are properly synchronizing static global data
+     * when dealing with mixed kernels/invokables.
+     */
+    public void testStaticGlobalSync() {
+        ScriptC_global_sync gs = new ScriptC_global_sync(mRS);
+
+        int v = 9;
+        setupGlobalSync(mRS, gs, v);
+        gs.forEach_write_static_global(AIn);
+        gs.invoke_test_static_global(v);
+
+        AFailed.copyTo(Failed);
+        if (Failed[0] != 0) {
+            FoundError = true;
+        }
+
+        gs.destroy();
+        checkForErrors();
+    }
+}
+