1 /* 2 * Copyright (C) 2015 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.renderscript.*; 20 21 22 23 public class LaunchClip extends RSBaseCompute { 24 Allocation mAPassFail; 25 Allocation mAin; 26 Allocation mAout; 27 ScriptC_launchclip mScript; 28 29 int[] mIn; 30 int[] mOut; 31 int[] mPassFail; 32 33 int mDimX = 0; 34 int mDimY = 0; 35 int mDimZ = 0; 36 boolean mHasFaces = false; 37 boolean mHasLods = false; 38 int mDimA0 = 0; 39 int mDimA1 = 0; 40 int mDimA2 = 0; 41 int mDimA3 = 0; 42 int mCellCount = 0; 43 44 void setup(boolean makeIn, boolean makeOut, int x, int y, int z, boolean face, boolean lods, 45 int a0, int a1, int a2, int a3) { 46 47 mDimX = x; 48 mDimY = y; 49 mDimZ = z; 50 mHasFaces = face; 51 mHasLods = lods; 52 mDimA0 = a0; 53 mDimA1 = a1; 54 mDimA2 = a2; 55 mDimA3 = a3; 56 57 mScript = new ScriptC_launchclip(mRS); 58 mScript.set_dimX(mDimX); 59 mScript.set_dimY(mDimY); 60 mScript.set_dimZ(mDimZ); 61 mScript.set_hasFaces(mHasFaces); 62 mScript.set_hasLod(mHasLods); 63 mScript.set_dimA0(mDimA0); 64 mScript.set_dimA1(mDimA1); 65 mScript.set_dimA2(mDimA2); 66 mScript.set_dimA3(mDimA3); 67 68 Type.Builder tb = new Type.Builder(mRS, Element.I32(mRS)); 69 tb.setX(mDimX); 70 if (mDimY > 0) tb.setY(mDimY); 71 if (mDimZ > 0) tb.setZ(mDimZ); 72 if (mHasFaces) tb.setFaces(true); 73 if (mHasLods) tb.setMipmaps(true); 74 //if (mDimA0 != 0) tb.setArray(0, mDimA0); 75 //if (mDimA1 != 0) tb.setArray(1, mDimA1); 76 //if (mDimA2 != 0) tb.setArray(2, mDimA2); 77 //if (mDimA3 != 0) tb.setArray(3, mDimA3); 78 Type t = tb.create(); 79 80 if (makeIn) { 81 mIn = new int[t.getCount()]; 82 mAin = Allocation.createTyped(mRS, t); 83 mScript.forEach_zero(mAin); 84 } 85 if (makeOut) { 86 mOut = new int[t.getCount()]; 87 mAout = Allocation.createTyped(mRS, t); 88 mScript.forEach_zero(mAout); 89 } 90 91 mPassFail = new int[1]; 92 mAPassFail = Allocation.createSized(mRS, Element.U32(mRS), 1); 93 mAPassFail.copyFrom(mPassFail); 94 mScript.set_passfail(mAPassFail); 95 } 96 97 private void verifyCell(int x, int y, int z, int[] a, Script.LaunchOptions sc) { 98 int expected = 0x80000000; 99 boolean inRange = true; 100 101 if (mDimX != 0) { 102 if (x >= sc.getXStart() && x < sc.getXEnd()) { 103 expected |= x; 104 } else { 105 inRange = false; 106 } 107 } 108 109 if (mDimY != 0) { 110 if (y >= sc.getYStart() && y < sc.getYEnd()) { 111 expected |= y << 8; 112 } else { 113 inRange = false; 114 } 115 } 116 117 if (mDimZ != 0) { 118 if (z >= sc.getZStart() && z < sc.getZEnd()) { 119 expected |= z << 16; 120 } else { 121 inRange = false; 122 } 123 } 124 125 if (!inRange) { 126 expected = 0; 127 } 128 129 int val = a[x + y * mDimX + z * mDimX * mDimY]; 130 if (val != expected) { 131 String s = new String("verify error @ " + x + ", " + y + ", " + z + 132 ", expected " + expected + ", got " + val); 133 ///android.util.Log.e("rs", s); 134 throw new IllegalStateException(s); 135 } 136 } 137 138 void verifyRange(Script.LaunchOptions sc, int[] a) { 139 int itY = (mDimY > 0) ? mDimY : 1; 140 int itZ = (mDimZ > 0) ? mDimZ : 1; 141 142 for (int x = 0; x < mDimX; x++) { 143 for (int y = 0; y < itY; y++) { 144 for (int z = 0; z < itZ; z++) { 145 verifyCell(x, y, z, a, sc); 146 } 147 } 148 } 149 } 150 151 AllocationAdapter makeAdapter(Allocation base, int ax, int ay, int az, int ox, int oy, int oz) { 152 Type.Builder tb = new Type.Builder(mRS, base.getType().getElement()); 153 tb.setX(ax); 154 if (ay > 0) { 155 tb.setY(ay); 156 } 157 if (az > 0) { 158 tb.setZ(az); 159 } 160 Type t = tb.create(); 161 162 AllocationAdapter a = AllocationAdapter.createTyped(mRS, base, t); 163 a.setX(ox); 164 if (base.getType().getY() > 0) { 165 a.setY(oy); 166 } 167 if (base.getType().getZ() > 0) { 168 a.setZ(oz); 169 } 170 171 mScript.set_biasX(ox); 172 mScript.set_biasY(oy); 173 mScript.set_biasZ(oz); 174 return a; 175 } 176 177 public void testWrite1D() { 178 setup(false, true, 256, 0, 0, false, false, 0, 0, 0, 0); 179 Script.LaunchOptions sc = new Script.LaunchOptions(); 180 sc.setX(9, 77); 181 182 mScript.forEach_write1d(mAout, sc); 183 mAout.copyTo(mOut); 184 185 verifyRange(sc, mOut); 186 } 187 188 public void testWrite1DAdapter1D() { 189 setup(false, true, 256, 0, 0, false, false, 0, 0, 0, 0); 190 Script.LaunchOptions sc = new Script.LaunchOptions(); 191 sc.setX(9, 77); 192 193 AllocationAdapter a = makeAdapter(mAout, 68, 0, 0, 9, 0, 0); 194 mScript.forEach_write1d(a); 195 mAout.copyTo(mOut); 196 197 verifyRange(sc, mOut); 198 } 199 200 201 public void testWrite2D() { 202 setup(false, true, 256, 256, 0, false, false, 0, 0, 0, 0); 203 Script.LaunchOptions sc = new Script.LaunchOptions(); 204 sc.setX(9, 77); 205 sc.setY(17, 177); 206 207 mScript.forEach_write2d(mAout, sc); 208 mAout.copyTo(mOut); 209 210 verifyRange(sc, mOut); 211 } 212 213 public void testWrite2DAdapter1D() { 214 setup(false, true, 256, 256, 0, false, false, 0, 0, 0, 0); 215 Script.LaunchOptions sc = new Script.LaunchOptions(); 216 sc.setX(9, 77); 217 sc.setY(17, 18); 218 219 AllocationAdapter a = makeAdapter(mAout, 68, 0, 0, 9, 17, 0); 220 mScript.forEach_write1d(a); 221 mAout.copyTo(mOut); 222 223 verifyRange(sc, mOut); 224 } 225 226 public void testWrite2DAdapter2D() { 227 setup(false, true, 256, 256, 0, false, false, 0, 0, 0, 0); 228 Script.LaunchOptions sc = new Script.LaunchOptions(); 229 sc.setX(9, 77); 230 sc.setY(17, 177); 231 232 AllocationAdapter a = makeAdapter(mAout, 68, 160, 0, 9, 17, 0); 233 mScript.forEach_write2d(a); 234 mAout.copyTo(mOut); 235 236 verifyRange(sc, mOut); 237 } 238 239 public void testWrite3D() { 240 setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0); 241 242 Script.LaunchOptions sc = new Script.LaunchOptions(); 243 sc.setX(9, 37); 244 sc.setY(17, 27); 245 sc.setZ(7, 21); 246 mScript.forEach_write3d(mAout, sc); 247 mAout.copyTo(mOut); 248 249 verifyRange(sc, mOut); 250 } 251 252 public void testWrite3DAdapter1D() { 253 setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0); 254 255 Script.LaunchOptions sc = new Script.LaunchOptions(); 256 sc.setX(9, 37); 257 sc.setY(17, 18); 258 sc.setZ(7, 8); 259 260 AllocationAdapter a = makeAdapter(mAout, 28, 0, 0, 9, 17, 7); 261 mScript.forEach_write1d(a); 262 mAout.copyTo(mOut); 263 264 verifyRange(sc, mOut); 265 } 266 267 public void testWrite3DAdapter2D() { 268 setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0); 269 270 Script.LaunchOptions sc = new Script.LaunchOptions(); 271 sc.setX(9, 37); 272 sc.setY(17, 27); 273 sc.setZ(7, 8); 274 275 AllocationAdapter a = makeAdapter(mAout, 28, 10, 0, 9, 17, 7); 276 mScript.forEach_write2d(a); 277 mAout.copyTo(mOut); 278 279 verifyRange(sc, mOut); 280 } 281 282 public void testWrite3DAdapter3D() { 283 setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0); 284 285 Script.LaunchOptions sc = new Script.LaunchOptions(); 286 sc.setX(9, 37); 287 sc.setY(17, 27); 288 sc.setZ(7, 21); 289 290 AllocationAdapter a = makeAdapter(mAout, 28, 10, 14, 9, 17, 7); 291 mScript.forEach_write3d(a); 292 mAout.copyTo(mOut); 293 294 verifyRange(sc, mOut); 295 } 296 297 } 298