1 /* 2 * Copyright (C) 2011-2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.renderscript.cts; 18 19 import android.graphics.Bitmap; 20 import android.renderscript.Allocation; 21 import android.renderscript.AllocationAdapter; 22 import android.renderscript.Allocation.MipmapControl; 23 import android.renderscript.Element; 24 import android.renderscript.RSIllegalArgumentException; 25 import android.renderscript.RSInvalidStateException; 26 import android.renderscript.Type; 27 import android.renderscript.Type.Builder; 28 29 import android.renderscript.ScriptIntrinsicColorMatrix; 30 import android.renderscript.ScriptIntrinsicConvolve3x3; 31 import android.renderscript.ScriptGroup; 32 import android.renderscript.Matrix4f; 33 import android.util.Log; 34 35 public class ScriptGroupTest extends RSBaseCompute { 36 37 static int bDimX = 48; 38 static int bDimY = 8; 39 testScriptGroupSingleKernel()40 public void testScriptGroupSingleKernel() { 41 ScriptGroup group; 42 43 Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create(); 44 45 ScriptIntrinsicColorMatrix mColorMatrix; 46 47 mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 48 49 Allocation a1_copy, a2_copy; 50 a1_copy = Allocation.createTyped(mRS, connect); 51 a2_copy = Allocation.createTyped(mRS, connect); 52 53 Matrix4f m = new Matrix4f(); 54 m.set(1, 0, 0.2f); 55 m.set(1, 1, 0.9f); 56 m.set(1, 2, 0.2f); 57 mColorMatrix.setColorMatrix(m); 58 59 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 60 b.addKernel(mColorMatrix.getKernelID()); 61 group = b.create(); 62 63 group.setInput(mColorMatrix.getKernelID(), a1_copy); 64 group.setOutput(mColorMatrix.getKernelID(), a2_copy); 65 66 group.execute(); 67 } 68 testScriptGroupDisconnectedKernel()69 public void testScriptGroupDisconnectedKernel() { 70 ScriptGroup group; 71 72 Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create(); 73 74 ScriptIntrinsicColorMatrix mColorMatrix, mColorMatrix2; 75 76 mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 77 mColorMatrix2 = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 78 79 Allocation a1_copy, a2_copy; 80 81 a1_copy = Allocation.createTyped(mRS, connect); 82 a2_copy = Allocation.createTyped(mRS, connect); 83 84 Matrix4f m = new Matrix4f(); 85 m.set(1, 0, 0.2f); 86 m.set(1, 1, 0.9f); 87 m.set(1, 2, 0.2f); 88 mColorMatrix.setColorMatrix(m); 89 mColorMatrix2.setColorMatrix(m); 90 91 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 92 b.addKernel(mColorMatrix.getKernelID()); 93 b.addKernel(mColorMatrix2.getKernelID()); 94 try { 95 group = b.create(); 96 fail("should throw RSInvalidStateException."); 97 } catch (RSInvalidStateException e) { 98 99 } 100 } 101 102 testScriptGroupFieldConnection()103 public void testScriptGroupFieldConnection() { 104 ScriptGroup group; 105 106 Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create(); 107 108 ScriptIntrinsicConvolve3x3 mConvolve3x3; 109 ScriptIntrinsicColorMatrix mColorMatrix; 110 111 mConvolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS)); 112 mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 113 114 Allocation a1_copy, a2_copy; 115 a1_copy = Allocation.createTyped(mRS, connect); 116 a2_copy = Allocation.createTyped(mRS, connect); 117 118 float f[] = new float[9]; 119 f[0] = 0.f; f[1] = -1.f; f[2] = 0.f; 120 f[3] = -1.f; f[4] = 5.f; f[5] = -1.f; 121 f[6] = 0.f; f[7] = -1.f; f[8] = 0.f; 122 123 mConvolve3x3.setCoefficients(f); 124 125 Matrix4f m = new Matrix4f(); 126 m.set(1, 0, 0.2f); 127 m.set(1, 1, 0.9f); 128 m.set(1, 2, 0.2f); 129 mColorMatrix.setColorMatrix(m); 130 131 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 132 b.addKernel(mColorMatrix.getKernelID()); 133 b.addKernel(mConvolve3x3.getKernelID()); 134 b.addConnection(connect, mColorMatrix.getKernelID(), mConvolve3x3.getFieldID_Input()); 135 group = b.create(); 136 137 group.setInput(mColorMatrix.getKernelID(), a1_copy); 138 group.setOutput(mConvolve3x3.getKernelID(), a2_copy); 139 140 group.execute(); 141 142 } 143 testScriptGroupDisconnectedDAG()144 public void testScriptGroupDisconnectedDAG() { 145 ScriptGroup group; 146 147 Type connect = new Type.Builder(mRS, Element.U8_4(mRS)).setX(bDimX).setY(bDimY).create(); 148 149 ScriptIntrinsicConvolve3x3 mConvolve3x3, mConvolve3x32; 150 ScriptIntrinsicColorMatrix mColorMatrix, mColorMatrix2; 151 152 mConvolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS)); 153 mConvolve3x32 = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS)); 154 mColorMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 155 mColorMatrix2 = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 156 157 Allocation a1_copy, a2_copy; 158 a1_copy = Allocation.createTyped(mRS, connect); 159 a2_copy = Allocation.createTyped(mRS, connect); 160 161 float f[] = new float[9]; 162 f[0] = 0.f; f[1] = -1.f; f[2] = 0.f; 163 f[3] = -1.f; f[4] = 5.f; f[5] = -1.f; 164 f[6] = 0.f; f[7] = -1.f; f[8] = 0.f; 165 166 mConvolve3x3.setCoefficients(f); 167 mConvolve3x32.setCoefficients(f); 168 169 Matrix4f m = new Matrix4f(); 170 m.set(1, 0, 0.2f); 171 m.set(1, 1, 0.9f); 172 m.set(1, 2, 0.2f); 173 mColorMatrix.setColorMatrix(m); 174 mColorMatrix2.setColorMatrix(m); 175 176 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 177 b.addKernel(mColorMatrix.getKernelID()); 178 b.addKernel(mColorMatrix2.getKernelID()); 179 b.addKernel(mConvolve3x3.getKernelID()); 180 b.addKernel(mConvolve3x32.getKernelID()); 181 b.addConnection(connect, mColorMatrix.getKernelID(), mConvolve3x3.getFieldID_Input()); 182 b.addConnection(connect, mColorMatrix2.getKernelID(), mConvolve3x32.getFieldID_Input()); 183 try { 184 group = b.create(); 185 fail("RSInvalidStateException expected"); 186 } catch (RSInvalidStateException e) { 187 188 } 189 190 } 191 testScriptGroupTorture()192 public void testScriptGroupTorture() { 193 ScriptGroup group; 194 195 int[] result = new int[1]; 196 197 bDimX = 1; 198 199 Type connect = new Type.Builder(mRS, Element.I32(mRS)).setX(bDimX).create(); 200 Type compareType = new Type.Builder(mRS, Element.I32(mRS)).create(); 201 202 ScriptC_scriptgroup node1, node2, node3, node4, node5, compare; 203 node1 = new ScriptC_scriptgroup(mRS); 204 node2 = new ScriptC_scriptgroup(mRS); 205 node3 = new ScriptC_scriptgroup(mRS); 206 node4 = new ScriptC_scriptgroup(mRS); 207 node5 = new ScriptC_scriptgroup(mRS); 208 209 compare = new ScriptC_scriptgroup(mRS); 210 211 Allocation in1, in2, out, resultAlloc; 212 in1 = Allocation.createTyped(mRS, connect); 213 in2 = Allocation.createTyped(mRS, connect); 214 215 out = Allocation.createTyped(mRS, connect); 216 resultAlloc = Allocation.createTyped(mRS, compareType); 217 218 node1.set_memset_toValue(1); 219 node1.forEach_memset(in1); 220 node1.set_memset_toValue(2); 221 node1.forEach_memset(in2); 222 223 node1.set_arith_operation(2); 224 node2.set_arith_operation(1); 225 node3.set_arith_operation(0); 226 node4.set_arith_operation(0); 227 node5.set_arith_operation(1); 228 229 node3.set_arith_use_rs_allocation(1); 230 node4.set_arith_use_rs_allocation(1); 231 232 node1.set_arith_value(5); 233 node2.set_arith_value(3); 234 node5.set_arith_value(7); 235 236 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 237 b.addKernel(node1.getKernelID_arith()); 238 b.addKernel(node2.getKernelID_arith()); 239 b.addKernel(node3.getKernelID_arith()); 240 b.addKernel(node4.getKernelID_arith()); 241 b.addKernel(node5.getKernelID_arith()); 242 243 b.addConnection(connect, node1.getKernelID_arith(), node2.getKernelID_arith()); 244 b.addConnection(connect, node1.getKernelID_arith(), node3.getFieldID_arith_rs_input()); 245 b.addConnection(connect, node2.getKernelID_arith(), node4.getFieldID_arith_rs_input()); 246 b.addConnection(connect, node3.getKernelID_arith(), node4.getKernelID_arith()); 247 b.addConnection(connect, node4.getKernelID_arith(), node5.getKernelID_arith()); 248 249 group = b.create(); 250 group.setInput(node1.getKernelID_arith(), in1); 251 group.setInput(node3.getKernelID_arith(), in2); 252 253 group.setOutput(node5.getKernelID_arith(), out); 254 255 group.execute(); 256 257 mRS.finish(); 258 259 compare.set_compare_value(2); 260 compare.forEach_compare(out); 261 compare.forEach_getCompareResult(resultAlloc); 262 resultAlloc.copyTo(result); 263 assertTrue(result[0] == 2); 264 } 265 266 /** 267 * Tests a case where a shared global variable is updated by the first kernel in a group, 268 * but then read by a subsequent kernel. 269 * 270 * The test ensures that we don't accidentally apply any fusion optimizations to the kernel 271 * pair, since there is a potential dependency that crosses the kernel cell boundary. 272 */ testScriptGroupSharedGlobal()273 public void testScriptGroupSharedGlobal() { 274 Type i32 = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create(); 275 Type u32 = new Type.Builder(mRS, Element.U32(mRS)).setX(2).create(); 276 277 Allocation aFailed = Allocation.createTyped(mRS, i32); 278 Allocation aSharedInt = Allocation.createTyped(mRS, i32); 279 280 ScriptC_group1 mG1 = new ScriptC_group1(mRS); 281 ScriptC_group2 mG2 = new ScriptC_group2(mRS); 282 283 mG1.set_aSharedInt(aSharedInt); 284 mG2.set_aSharedInt(aSharedInt); 285 mG2.set_aFailed(aFailed); 286 287 int [] Failed = new int [1]; 288 Failed[0] = 0; 289 aFailed.copyFrom(Failed); 290 291 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 292 293 // Writes to aSharedInt[x] in the kernel. 294 b.addKernel(mG1.getKernelID_setSharedInt()); 295 // Reads aSharedInt[1] to verify it is -5. 296 b.addKernel(mG2.getKernelID_getSharedInt()); 297 // If we fuse mG1/mG2, we won't see the update to the aSharedInt[1] during mG2 for x == 0. 298 // The update is only visible if we correctly identify the dependency and execute all of 299 // mG1 before starting on mG2. 300 b.addConnection(u32, mG1.getKernelID_setSharedInt(), mG2.getKernelID_getSharedInt()); 301 ScriptGroup group = b.create(); 302 group.execute(); 303 304 mG2.invoke_verify(); 305 aFailed.copyTo(Failed); 306 if (Failed[0] != 0) { 307 FoundError = true; 308 } 309 310 checkForErrors(); 311 } 312 } 313