1/*
2 * Copyright (C) 2012 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.imagejb;
18
19import java.lang.Math;
20
21import android.renderscript.Allocation;
22import android.renderscript.Element;
23import android.renderscript.RenderScript;
24import android.renderscript.ScriptIntrinsicConvolve3x3;
25import android.renderscript.ScriptIntrinsicColorMatrix;
26import android.renderscript.Type;
27import android.renderscript.Matrix4f;
28import android.renderscript.ScriptGroup;
29import android.util.Log;
30
31public class GroupTest extends TestBase {
32    private ScriptIntrinsicConvolve3x3 mConvolve;
33    private ScriptIntrinsicColorMatrix mMatrix;
34
35    private Allocation mScratchPixelsAllocation1;
36    private ScriptGroup mGroup;
37
38    private int mWidth;
39    private int mHeight;
40    private boolean mUseNative;
41
42
43    public GroupTest(boolean useNative) {
44        mUseNative = useNative;
45    }
46
47    public void createTest(android.content.res.Resources res) {
48        mWidth = mInPixelsAllocation.getType().getX();
49        mHeight = mInPixelsAllocation.getType().getY();
50
51        mConvolve = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
52        mMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
53
54        float f[] = new float[9];
55        f[0] =  0.f;    f[1] = -1.f;    f[2] =  0.f;
56        f[3] = -1.f;    f[4] =  5.f;    f[5] = -1.f;
57        f[6] =  0.f;    f[7] = -1.f;    f[8] =  0.f;
58        mConvolve.setCoefficients(f);
59
60        Matrix4f m = new Matrix4f();
61        m.set(1, 0, 0.2f);
62        m.set(1, 1, 0.9f);
63        m.set(1, 2, 0.2f);
64        mMatrix.setColorMatrix(m);
65
66        Type.Builder tb = new Type.Builder(mRS, Element.U8_4(mRS));
67        tb.setX(mWidth);
68        tb.setY(mHeight);
69        Type connect = tb.create();
70
71        if (mUseNative) {
72            ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
73            b.addKernel(mConvolve.getKernelID());
74            b.addKernel(mMatrix.getKernelID());
75            b.addConnection(connect, mConvolve.getKernelID(), mMatrix.getKernelID());
76            mGroup = b.create();
77        } else {
78            mScratchPixelsAllocation1 = Allocation.createTyped(mRS, connect);
79        }
80    }
81
82    public void animateBars(float time) {
83        Matrix4f m = new Matrix4f();
84        m.set(1, 0, (time + 0.2f) % 1.0f);
85        m.set(1, 1, (time + 0.9f) % 1.0f);
86        m.set(1, 2, (time + 0.4f) % 1.0f);
87        mMatrix.setColorMatrix(m);
88    }
89
90    public void runTest() {
91        mConvolve.setInput(mInPixelsAllocation);
92        if (mUseNative) {
93            mGroup.setOutput(mMatrix.getKernelID(), mOutPixelsAllocation);
94            mGroup.execute();
95        } else {
96            mConvolve.forEach(mScratchPixelsAllocation1);
97            mMatrix.forEach(mScratchPixelsAllocation1, mOutPixelsAllocation);
98        }
99    }
100
101}
102