1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.android.rs.unittest;
16
17import android.content.Context;
18import android.renderscript.Allocation;
19import android.renderscript.Element;
20import android.renderscript.RenderScript;
21import android.renderscript.ScriptGroup;
22import android.renderscript.Type;
23import android.util.Log;
24
25public class UT_script_group2_nochain extends UnitTest {
26    private static final int ARRAY_SIZE = 256;
27
28    private static final String TAG = "ScriptGroup2 (nochain)";
29
30    public UT_script_group2_nochain(Context ctx) {
31        super(TAG, ctx);
32    }
33
34    public void run() {
35        RenderScript pRS = createRenderScript(false);
36        ScriptC_increment s_inc = new ScriptC_increment(pRS);
37        ScriptC_increment2 s_inc2 = new ScriptC_increment2(pRS);
38        ScriptC_double s_double = new ScriptC_double(pRS);
39
40        int[] array = new int[ARRAY_SIZE * 4];
41
42        for (int i = 0; i < ARRAY_SIZE * 4; i++) {
43            array[i] = i;
44        }
45
46        Allocation input = Allocation.createSized(pRS, Element.I32_4(pRS), ARRAY_SIZE);
47        input.copyFrom(array);
48
49        ScriptGroup.Builder2 builder = new ScriptGroup.Builder2(pRS);
50
51        ScriptGroup.Input unbound = builder.addInput();
52
53        Type T = Type.createX(pRS, Element.I32_4(pRS), ARRAY_SIZE);
54
55        ScriptGroup.Closure c0 =
56                builder.addKernel(s_inc.getKernelID_increment(),
57                                  T,
58                                  unbound);
59
60        ScriptGroup.Closure c1 =
61                builder.addKernel(s_inc2.getKernelID_increment2(),
62                                  T,
63                                  unbound,
64                                  new ScriptGroup.Binding(s_inc2.getFieldID_a(), unbound));
65
66        ScriptGroup.Closure c2 =
67                builder.addKernel(s_double.getKernelID_doubleKernel(),
68                                  T,
69                                  unbound);
70
71        ScriptGroup group = builder.create("AddDouble2", c2.getReturn());
72
73        int[] a = new int[ARRAY_SIZE * 4];
74        ((Allocation) group.execute(input)[0]).copyTo(a);
75
76        pRS.finish();
77        group.destroy();
78        T.destroy();
79        input.destroy();
80        s_inc.destroy();
81        s_inc2.destroy();
82        s_double.destroy();
83        pRS.destroy();
84
85        boolean failed = false;
86        for (int i = 0; i < ARRAY_SIZE * 4; i++) {
87            if (a[i] != (i + 1) * 2) {
88                Log.e(TAG, "a[" + i + "]=" + a[i] + ", should be " + ((i + 1) * 2));
89                failed = true;
90            }
91        }
92        if (failed) {
93            failTest();
94            return;
95        }
96        passTest();
97    }
98}
99