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.Matrix4f;
24import android.renderscript.RenderScript;
25import android.renderscript.Script;
26import android.renderscript.ScriptC;
27import android.renderscript.ScriptIntrinsicConvolve3x3;
28import android.renderscript.Type;
29import android.util.Log;
30
31public class Convolve3x3 extends TestBase {
32    private ScriptC_convolve3x3 mScript;
33    private ScriptIntrinsicConvolve3x3 mIntrinsic;
34
35    private int mWidth;
36    private int mHeight;
37    private boolean mUseIntrinsic;
38
39    public Convolve3x3(boolean useIntrinsic) {
40        mUseIntrinsic = useIntrinsic;
41    }
42
43    private float blend(float v1, float v2, float p) {
44        return (v2 * p) + (v1 * (1.f-p));
45    }
46
47    private float[] updateMatrix(float str) {
48        float f[] = new float[9];
49        float cf1 = blend(1.f / 9.f, 0.f, str);
50        float cf2 = blend(1.f / 9.f, -1.f, str);
51        float cf3 = blend(1.f / 9.f, 5.f, str);
52        f[0] =  cf1;  f[1] = cf2;   f[2] = cf1;
53        f[3] =  cf2;  f[4] = cf3;   f[5] = cf2;
54        f[6] =  cf1;  f[7] = cf2;   f[8] = cf1;
55        return f;
56    }
57
58    public void createTest(android.content.res.Resources res) {
59        mWidth = mInPixelsAllocation.getType().getX();
60        mHeight = mInPixelsAllocation.getType().getY();
61
62        float f[] = updateMatrix(1.f);
63        if (mUseIntrinsic) {
64            mIntrinsic = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
65            mIntrinsic.setCoefficients(f);
66            mIntrinsic.setInput(mInPixelsAllocation);
67        } else {
68            mScript = new ScriptC_convolve3x3(mRS);
69            mScript.set_gCoeffs(f);
70            mScript.set_gIn(mInPixelsAllocation);
71            mScript.set_gWidth(mWidth);
72            mScript.set_gHeight(mHeight);
73        }
74    }
75
76    public void animateBars(float time) {
77        float f[] = updateMatrix(time % 1.f);
78        if (mUseIntrinsic) {
79            mIntrinsic.setCoefficients(f);
80        } else {
81            mScript.set_gCoeffs(f);
82        }
83    }
84
85    public void runTest() {
86        if (mUseIntrinsic) {
87            mIntrinsic.forEach(mOutPixelsAllocation);
88        } else {
89            mScript.forEach_root(mOutPixelsAllocation);
90        }
91    }
92
93}
94