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.image;
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.Builder2 b = new ScriptGroup.Builder2(mRS);
73            ScriptGroup.Input in = b.addInput();
74            ScriptGroup.Closure c;
75
76            c = b.addKernel(mConvolve.getKernelID(),
77                            connect,
78                            new ScriptGroup.Binding(mConvolve.getFieldID_Input(), in));
79
80            c = b.addKernel(mMatrix.getKernelID(),
81                            mOutPixelsAllocation.getType(),
82                            c.getReturn());
83            mGroup = b.create("group", c.getReturn());
84        } else {
85            mScratchPixelsAllocation1 = Allocation.createTyped(mRS, connect);
86        }
87    }
88
89    public void runTest() {
90        mConvolve.setInput(mInPixelsAllocation);
91        if (mUseNative) {
92            mOutPixelsAllocation = (Allocation)mGroup.execute(mInPixelsAllocation)[0];
93        } else {
94            mConvolve.forEach(mScratchPixelsAllocation1);
95            mMatrix.forEach(mScratchPixelsAllocation1, mOutPixelsAllocation);
96        }
97    }
98
99}
100