1 /*
2  * Copyright (C) 2017 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 com.android.rs.unittest;
18 
19 import android.content.Context;
20 import android.renderscript.Allocation;
21 import android.renderscript.Element;
22 import android.renderscript.RenderScript;
23 import android.renderscript.Type;
24 
25 public class UT_fp16 extends UnitTest {
26     private int dimX = 7;
27     private int dimY = 5;
28     private int dimZ = 3;
29     private Type type;
30     private Allocation alloc;
31 
UT_fp16(Context ctx)32     public UT_fp16(Context ctx) {
33         super("Fp16", ctx);
34     }
35 
initializeGlobals(RenderScript RS, ScriptC_fp16 s, Element e, int nDims)36     private void initializeGlobals(RenderScript RS, ScriptC_fp16 s, Element e, int nDims) {
37         Type.Builder typeBuilder = new Type.Builder(RS, e);
38         switch (nDims) {
39             case 1:
40                 typeBuilder.setX(dimX);
41                 break;
42             case 2:
43                 typeBuilder.setX(dimX).setY(dimY);
44                 break;
45             case 3:
46                 typeBuilder.setX(dimX).setY(dimY).setZ(dimZ);
47                 break;
48         }
49         type = typeBuilder.create();
50         alloc = Allocation.createTyped(RS, type);
51 
52         s.set_gDimX(dimX);
53         s.set_gDimY(nDims > 1 ? dimY : 0);
54         s.set_gDimZ(nDims > 2 ? dimZ : 0);
55         s.set_gAlloc(alloc);
56     }
57 
TestHalf(RenderScript RS, ScriptC_fp16 s, int nDims)58     private void TestHalf(RenderScript RS, ScriptC_fp16 s, int nDims) {
59         initializeGlobals(RS, s, Element.F16(RS), nDims);
60 
61         // set in kernel and test rsGetElementAt in invoke
62         s.forEach_set_kernel_half(alloc);
63         s.invoke_verify_half();
64 
65         RS.finish();
66         alloc.destroy();
67         type.destroy();
68 
69         initializeGlobals(RS, s, Element.F16(RS), nDims);
70         // rsSetElementAt in invoke and verify in kernel
71         s.invoke_set_half();
72         s.forEach_verify_kernel_half(alloc);
73 
74         RS.finish();
75         alloc.destroy();
76         type.destroy();
77     }
78 
TestHalf2(RenderScript RS, ScriptC_fp16 s, int nDims)79     private void TestHalf2(RenderScript RS, ScriptC_fp16 s, int nDims) {
80         initializeGlobals(RS, s, Element.F16_2(RS), nDims);
81 
82         // set in kernel and test rsGetElementAt in invoke
83         s.forEach_set_kernel_half2(alloc);
84         s.invoke_verify_half2();
85 
86         RS.finish();
87         alloc.destroy();
88         type.destroy();
89 
90         initializeGlobals(RS, s, Element.F16_2(RS), nDims);
91         // rsSetElementAt in invoke and verify in kernel
92         s.invoke_set_half2();
93         s.forEach_verify_kernel_half2(alloc);
94 
95         RS.finish();
96         alloc.destroy();
97         type.destroy();
98     }
99 
TestHalf3(RenderScript RS, ScriptC_fp16 s, int nDims)100     private void TestHalf3(RenderScript RS, ScriptC_fp16 s, int nDims) {
101         initializeGlobals(RS, s, Element.F16_3(RS), nDims);
102 
103         // set in kernel and test rsGetElementAt in invoke
104         s.forEach_set_kernel_half3(alloc);
105         s.invoke_verify_half3();
106 
107         RS.finish();
108         alloc.destroy();
109         type.destroy();
110 
111         initializeGlobals(RS, s, Element.F16_3(RS), nDims);
112         // rsSetElementAt in invoke and verify in kernel
113         s.invoke_set_half3();
114         s.forEach_verify_kernel_half3(alloc);
115 
116         RS.finish();
117         alloc.destroy();
118         type.destroy();
119     }
120 
TestHalf4(RenderScript RS, ScriptC_fp16 s, int nDims)121     private void TestHalf4(RenderScript RS, ScriptC_fp16 s, int nDims) {
122         initializeGlobals(RS, s, Element.F16_4(RS), nDims);
123 
124         // set in kernel and test rsGetElementAt in invoke
125         s.forEach_set_kernel_half4(alloc);
126         s.invoke_verify_half4();
127 
128         RS.finish();
129         alloc.destroy();
130         type.destroy();
131 
132         initializeGlobals(RS, s, Element.F16_4(RS), nDims);
133         // rsSetElementAt in invoke and verify in kernel
134         s.invoke_set_half4();
135         s.forEach_verify_kernel_half4(alloc);
136 
137         RS.finish();
138         alloc.destroy();
139         type.destroy();
140     }
141 
run()142     public void run() {
143         Allocation A;
144         RenderScript pRS = createRenderScript(true);
145         ScriptC_fp16 s = new ScriptC_fp16(pRS);
146 
147         for (int nDims = 1; nDims <= 3; nDims++) {
148             TestHalf(pRS, s, nDims);
149             TestHalf2(pRS, s, nDims);
150             TestHalf3(pRS, s, nDims);
151             TestHalf4(pRS, s, nDims);
152         }
153 
154         s.invoke_fp16_test();
155         pRS.finish();
156         s.destroy();
157         pRS.destroy();
158     }
159 }
160