• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             mVerify = null;
45         }
46         super.tearDown();
47     }
48 
makeElement(Element.DataType dt, int vecSize)49     protected Element makeElement(Element.DataType dt, int vecSize) {
50         Element e;
51         if (vecSize > 1) {
52             e = Element.createVector(mRS, dt, vecSize);
53         } else {
54             if (dt == Element.DataType.UNSIGNED_8) {
55                 e = Element.U8(mRS);
56             } else {
57                 e = Element.F32(mRS);
58             }
59         }
60         return e;
61     }
62 
makeAllocation(int w, int h, Element e, boolean clear)63     protected Allocation makeAllocation(int w, int h, Element e, boolean clear) {
64         Type.Builder tb = new Type.Builder(mRS, e);
65         tb.setX(w);
66         tb.setY(h);
67         Type t = tb.create();
68         Allocation a = Allocation.createTyped(mRS, t);
69 
70         if (clear) {
71             final int s = a.getBytesSize();
72             byte[] b = new byte[s];
73             a.copyFromUnchecked(b);
74         }
75 
76         return a;
77     }
78 
makeAllocation(int w, int h, Element e)79     protected Allocation makeAllocation(int w, int h, Element e) {
80         return makeAllocation(w, h, e, true);
81     }
82 
makeSource(int w, int h, Element e)83     protected void makeSource(int w, int h, Element e) {
84         if (mAllocSrc != null) {
85             mAllocSrc.destroy();
86         }
87         mAllocSrc = makeAllocation(w, h, e);
88 
89         java.util.Random r = new java.util.Random(100);
90 
91         int vs = e.getVectorSize();
92         if (vs == 3) vs = 4;
93         if (e.getDataType() == Element.DataType.FLOAT_32) {
94             float f[] = new float[w * h * vs];
95             for (int y=0; y < h; y++) {
96                 for (int x = 0; x < w; x++) {
97                     for (int v = 0; v < vs; v++) {
98                         f[(y * w + x) * vs + v] = r.nextFloat();
99                     }
100                 }
101             }
102             mAllocSrc.copyFromUnchecked(f);
103         }
104 
105         if (e.getDataType() == Element.DataType.UNSIGNED_8) {
106             byte f[] = new byte[w * h * vs];
107             for (int y=0; y < h; y++) {
108                 for (int x = 0; x < w; x++) {
109                     for (int v = 0; v < vs; v++) {
110                         f[(y * w + x) * vs + v] = (byte)r.nextInt(256);
111                     }
112                 }
113             }
114             mAllocSrc.copyFromUnchecked(f);
115         }
116     }
117 
makeBuffers(int w, int h, Element e)118     protected void makeBuffers(int w, int h, Element e) {
119         makeSource(w, h, e);
120 
121         if (mAllocRef != null) {
122             mAllocRef.destroy();
123         }
124         if (mAllocDst != null) {
125             mAllocDst.destroy();
126         }
127         mAllocRef = makeAllocation(w, h, e);
128         mAllocDst = makeAllocation(w, h, e);
129     }
130 
131 
checkError()132     protected void checkError() {
133         mRS.finish();
134         mVerify.invoke_checkError();
135         waitForMessage();
136         checkForErrors();
137     }
138 
makeClipper(int x, int y, int x2, int y2)139     protected Script.LaunchOptions makeClipper(int x, int y, int x2, int y2) {
140         Script.LaunchOptions lo = new Script.LaunchOptions();
141         lo.setX(x, x2);
142         lo.setY(y, y2);
143         return lo;
144     }
145 
146 }
147