1 /*
2  * Copyright (C) 2013 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.util.Log;
20 import android.renderscript.RenderScript;
21 import android.renderscript.Allocation;
22 import android.renderscript.Element;
23 import android.renderscript.Type;
24 import android.renderscript.Script;
25 
26 public class IntrinsicBase extends RSBaseCompute {
27     protected final String TAG = "Img";
28 
29     protected Allocation mAllocSrc;
30     protected Allocation mAllocRef;
31     protected Allocation mAllocDst;
32     protected ScriptC_verify mVerify;
33 
34     @Override
setUp()35     protected void setUp() throws Exception {
36         super.setUp();
37         mVerify = new ScriptC_verify(mRS);
38     }
39 
40     @Override
tearDown()41     protected void tearDown() throws Exception {
42         if (mVerify != null) {
43             mVerify.destroy();
44         }
45 
46         if (mAllocSrc != null) {
47             mAllocSrc.destroy();
48         }
49 
50         if (mAllocRef != null) {
51             mAllocRef.destroy();
52         }
53 
54         if (mAllocDst != null) {
55             mAllocDst.destroy();
56         }
57 
58         super.tearDown();
59     }
60 
makeElement(Element.DataType dt, int vecSize)61     protected Element makeElement(Element.DataType dt, int vecSize) {
62         Element e;
63         if (vecSize > 1) {
64             e = Element.createVector(mRS, dt, vecSize);
65         } else {
66             if (dt == Element.DataType.UNSIGNED_8) {
67                 e = Element.U8(mRS);
68             } else {
69                 e = Element.F32(mRS);
70             }
71         }
72         return e;
73     }
74 
makeAllocation(int w, int h, Element e, boolean clear)75     protected Allocation makeAllocation(int w, int h, Element e, boolean clear) {
76         Type.Builder tb = new Type.Builder(mRS, e);
77         tb.setX(w);
78         tb.setY(h);
79         Type t = tb.create();
80         Allocation a = Allocation.createTyped(mRS, t);
81 
82         if (clear) {
83             final int s = a.getBytesSize();
84             byte[] b = new byte[s];
85             a.copyFromUnchecked(b);
86         }
87 
88         return a;
89     }
90 
makeAllocation(int w, int h, Element e)91     protected Allocation makeAllocation(int w, int h, Element e) {
92         return makeAllocation(w, h, e, true);
93     }
94 
makeSource(int w, int h, Element e)95     protected void makeSource(int w, int h, Element e) {
96         mAllocSrc = makeAllocation(w, h, e);
97 
98         java.util.Random r = new java.util.Random(100);
99 
100         int vs = e.getVectorSize();
101         if (vs == 3) vs = 4;
102         if (e.getDataType() == Element.DataType.FLOAT_32) {
103             float f[] = new float[w * h * vs];
104             for (int y=0; y < h; y++) {
105                 for (int x = 0; x < w; x++) {
106                     for (int v = 0; v < vs; v++) {
107                         f[(y * w + x) * vs + v] = r.nextFloat();
108                     }
109                 }
110             }
111             mAllocSrc.copyFromUnchecked(f);
112         }
113 
114         if (e.getDataType() == Element.DataType.UNSIGNED_8) {
115             byte f[] = new byte[w * h * vs];
116             for (int y=0; y < h; y++) {
117                 for (int x = 0; x < w; x++) {
118                     for (int v = 0; v < vs; v++) {
119                         f[(y * w + x) * vs + v] = (byte)r.nextInt(256);
120                     }
121                 }
122             }
123             mAllocSrc.copyFromUnchecked(f);
124         }
125     }
126 
makeBuffers(int w, int h, Element e)127     protected void makeBuffers(int w, int h, Element e) {
128         makeSource(w, h, e);
129 
130         mAllocRef = makeAllocation(w, h, e);
131         mAllocDst = makeAllocation(w, h, e);
132     }
133 
134 
checkError()135     protected void checkError() {
136         mRS.finish();
137         mVerify.invoke_checkError();
138         waitForMessage();
139         checkForErrors();
140     }
141 
makeClipper(int x, int y, int x2, int y2)142     protected Script.LaunchOptions makeClipper(int x, int y, int x2, int y2) {
143         Script.LaunchOptions lo = new Script.LaunchOptions();
144         lo.setX(x, x2);
145         lo.setY(y, y2);
146         return lo;
147     }
148 
149 }
150