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 
setup(boolean makeIn, boolean makeOut, int x, int y, int z, boolean face, boolean lods, int a0, int a1, int a2, int a3)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     @Override
tearDown()98     protected void tearDown() throws Exception {
99         if (mAPassFail != null) {
100             mAPassFail.destroy();
101         }
102         if (mAin != null) {
103             mAin.destroy();
104         }
105         if (mAout != null) {
106             mAout.destroy();
107         }
108         if (mScript != null) {
109             mScript.destroy();
110         }
111         super.tearDown();
112     }
113 
verifyCell(int x, int y, int z, int[] a, Script.LaunchOptions sc)114     private void verifyCell(int x, int y, int z, int[] a, Script.LaunchOptions sc) {
115         int expected = 0x80000000;
116         boolean inRange = true;
117 
118         if (mDimX != 0) {
119             if (x >= sc.getXStart() && x < sc.getXEnd()) {
120                 expected |= x;
121             } else {
122                 inRange = false;
123             }
124         }
125 
126         if (mDimY != 0) {
127             if (y >= sc.getYStart() && y < sc.getYEnd()) {
128                 expected |= y << 8;
129             } else {
130                 inRange = false;
131             }
132         }
133 
134         if (mDimZ != 0) {
135             if (z >= sc.getZStart() && z < sc.getZEnd()) {
136                 expected |= z << 16;
137             } else {
138                 inRange = false;
139             }
140         }
141 
142         if (!inRange) {
143             expected = 0;
144         }
145 
146         int val = a[x + y * mDimX + z * mDimX * mDimY];
147         if (val != expected) {
148             String s = new String("verify error @ " + x + ", " + y + ", " + z +
149                                   ", expected " + expected + ", got " + val);
150             ///android.util.Log.e("rs", s);
151             throw new IllegalStateException(s);
152         }
153     }
154 
verifyRange(Script.LaunchOptions sc, int[] a)155     void verifyRange(Script.LaunchOptions sc, int[] a) {
156         int itY = (mDimY > 0) ? mDimY : 1;
157         int itZ = (mDimZ > 0) ? mDimZ : 1;
158 
159         for (int x = 0; x < mDimX; x++) {
160             for (int y = 0; y < itY; y++) {
161                 for (int z = 0; z < itZ; z++) {
162                     verifyCell(x, y, z, a, sc);
163                 }
164             }
165         }
166     }
167 
makeAdapter(Allocation base, int ax, int ay, int az, int ox, int oy, int oz)168     AllocationAdapter makeAdapter(Allocation base, int ax, int ay, int az, int ox, int oy, int oz) {
169         Type.Builder tb = new Type.Builder(mRS, base.getType().getElement());
170         tb.setX(ax);
171         if (ay > 0) {
172             tb.setY(ay);
173         }
174         if (az > 0) {
175             tb.setZ(az);
176         }
177         Type t = tb.create();
178 
179         AllocationAdapter a = AllocationAdapter.createTyped(mRS, base, t);
180         a.setX(ox);
181         if (base.getType().getY() > 0) {
182             a.setY(oy);
183         }
184         if (base.getType().getZ() > 0) {
185             a.setZ(oz);
186         }
187 
188         mScript.set_biasX(ox);
189         mScript.set_biasY(oy);
190         mScript.set_biasZ(oz);
191         return a;
192     }
193 
testWrite1D()194     public void testWrite1D() {
195         setup(false, true, 256, 0, 0, false, false, 0, 0, 0, 0);
196         Script.LaunchOptions sc = new Script.LaunchOptions();
197         sc.setX(9, 77);
198 
199         mScript.forEach_write1d(mAout, sc);
200         mAout.copyTo(mOut);
201 
202         verifyRange(sc, mOut);
203     }
204 
testWrite1DAdapter1D()205     public void testWrite1DAdapter1D() {
206         setup(false, true, 256, 0, 0, false, false, 0, 0, 0, 0);
207         Script.LaunchOptions sc = new Script.LaunchOptions();
208         sc.setX(9, 77);
209 
210         AllocationAdapter a = makeAdapter(mAout, 68, 0, 0,  9, 0, 0);
211         mScript.forEach_write1d(a);
212         mAout.copyTo(mOut);
213 
214         verifyRange(sc, mOut);
215     }
216 
217 
testWrite2D()218     public void testWrite2D() {
219         setup(false, true, 256, 256, 0, false, false, 0, 0, 0, 0);
220         Script.LaunchOptions sc = new Script.LaunchOptions();
221         sc.setX(9, 77);
222         sc.setY(17, 177);
223 
224         mScript.forEach_write2d(mAout, sc);
225         mAout.copyTo(mOut);
226 
227         verifyRange(sc, mOut);
228     }
229 
testWrite2DAdapter1D()230     public void testWrite2DAdapter1D() {
231         setup(false, true, 256, 256, 0, false, false, 0, 0, 0, 0);
232         Script.LaunchOptions sc = new Script.LaunchOptions();
233         sc.setX(9, 77);
234         sc.setY(17, 18);
235 
236         AllocationAdapter a = makeAdapter(mAout, 68, 0, 0,  9, 17, 0);
237         mScript.forEach_write1d(a);
238         mAout.copyTo(mOut);
239 
240         verifyRange(sc, mOut);
241     }
242 
testWrite2DAdapter2D()243     public void testWrite2DAdapter2D() {
244         setup(false, true, 256, 256, 0, false, false, 0, 0, 0, 0);
245         Script.LaunchOptions sc = new Script.LaunchOptions();
246         sc.setX(9, 77);
247         sc.setY(17, 177);
248 
249         AllocationAdapter a = makeAdapter(mAout, 68, 160, 0,  9, 17, 0);
250         mScript.forEach_write2d(a);
251         mAout.copyTo(mOut);
252 
253         verifyRange(sc, mOut);
254     }
255 
testWrite3D()256     public void testWrite3D() {
257         setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0);
258 
259         Script.LaunchOptions sc = new Script.LaunchOptions();
260         sc.setX(9, 37);
261         sc.setY(17, 27);
262         sc.setZ(7, 21);
263         mScript.forEach_write3d(mAout, sc);
264         mAout.copyTo(mOut);
265 
266         verifyRange(sc, mOut);
267     }
268 
testWrite3DAdapter1D()269     public void testWrite3DAdapter1D() {
270         setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0);
271 
272         Script.LaunchOptions sc = new Script.LaunchOptions();
273         sc.setX(9, 37);
274         sc.setY(17, 18);
275         sc.setZ(7, 8);
276 
277         AllocationAdapter a = makeAdapter(mAout, 28, 0, 0,  9, 17, 7);
278         mScript.forEach_write1d(a);
279         mAout.copyTo(mOut);
280 
281         verifyRange(sc, mOut);
282     }
283 
testWrite3DAdapter2D()284     public void testWrite3DAdapter2D() {
285         setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0);
286 
287         Script.LaunchOptions sc = new Script.LaunchOptions();
288         sc.setX(9, 37);
289         sc.setY(17, 27);
290         sc.setZ(7, 8);
291 
292         AllocationAdapter a = makeAdapter(mAout, 28, 10, 0,  9, 17, 7);
293         mScript.forEach_write2d(a);
294         mAout.copyTo(mOut);
295 
296         verifyRange(sc, mOut);
297     }
298 
testWrite3DAdapter3D()299     public void testWrite3DAdapter3D() {
300         setup(false, true, 64, 64, 64, false, false, 0, 0, 0, 0);
301 
302         Script.LaunchOptions sc = new Script.LaunchOptions();
303         sc.setX(9, 37);
304         sc.setY(17, 27);
305         sc.setZ(7, 21);
306 
307         AllocationAdapter a = makeAdapter(mAout, 28, 10, 14,  9, 17, 7);
308         mScript.forEach_write3d(a);
309         mAout.copyTo(mOut);
310 
311         verifyRange(sc, mOut);
312     }
313 
314 }
315