1/*
2 * Copyright (C) 2016 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
17package com.android.rs.rsov.test;
18
19import android.content.Context;
20import android.renderscript.Allocation;
21import android.renderscript.Element;
22import android.renderscript.RenderScript;
23import android.renderscript.Type;
24import android.util.Log;
25
26public class UT_multi_kernel extends UnitTest {
27    protected UT_multi_kernel(RSoVTestCore rstc, Context ctx) {
28        super(rstc, "multi_kernel", ctx);
29    }
30
31    private boolean Test(int width, int height, int depth) {
32        RenderScript pRS = RenderScript.create(mCtx);
33        ScriptC_multi_kernel s = new ScriptC_multi_kernel(pRS);
34
35        Type.Builder typeBuilder = new Type.Builder(pRS, Element.F32_4(pRS));
36        typeBuilder.setX(width);
37        if (height > 0) {
38            typeBuilder.setY(height);
39            if (depth > 0) {
40                typeBuilder.setZ(depth);
41            }
42        }
43
44        if (depth < 1) {
45            depth = 1;
46        }
47
48        if (height < 1) {
49            height = 1;
50        }
51
52        Allocation A = Allocation.createTyped(pRS, typeBuilder.create());
53        Allocation B = Allocation.createTyped(pRS, typeBuilder.create());
54
55        float a[] = new float[width * height * depth * 4];
56        float b[] = new float[width * height * depth * 4];
57
58        java.util.Random rand = new java.util.Random(0xcafebabe);
59
60        for (int i = 0; i < width * height * depth * 4; i++) {
61            a[i] = rand.nextFloat();
62        }
63
64        A.copyFrom(a);
65
66        s.forEach_invert(A, B);
67        s.forEach_scalebytwo(B, A);
68        s.forEach_scalebythree(A, B);
69
70        B.copyTo(b);
71
72        B.destroy();
73        A.destroy();
74
75        pRS.finish();
76        pRS.destroy();
77
78        boolean failed = false;
79        for (int i = 0; i < width * height * depth * 4; i++) {
80            if (b[i] != 3.0f * 2.0f * (1.0f - a[i])) {
81                Log.e(name, "expects " + 3.0f * 2.0f * (1.0f - a[i]) + " for element "
82                      + i + ". got " + b[i]);
83                failed = true;
84                break;
85            }
86        }
87
88        return !failed;
89    }
90
91    public void run() {
92        final int X = 96;
93        final int Y = 64;
94        final int Z = 32;
95
96        if (Test(X, 0, 0) && Test(X, Y, 0)) {
97            passTest();
98            return;
99        }
100
101        failTest();
102    }
103}
104