1/*
2 * Copyright (C) 2014 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.ScriptIntrinsicResize;
28import android.renderscript.Type;
29import android.util.Log;
30
31public class Resize extends TestBase {
32    private ScriptC_resize mScript;
33    private ScriptIntrinsicResize mIntrinsic;
34
35    private Allocation mScratchAllocation;
36    private int mWidth;
37    private int mHeight;
38    private boolean mUseIntrinsic;
39
40    public Resize(boolean useIntrinsic) {
41        mUseIntrinsic = useIntrinsic;
42    }
43
44    public void createTest(android.content.res.Resources res) {
45        mWidth = mInPixelsAllocation.getType().getX();
46        mHeight = mInPixelsAllocation.getType().getY();
47        float scale = 1.f / 32.f;
48
49        Type t = Type.createXY(mRS, mInPixelsAllocation.getElement(),
50                               (int)(mWidth * scale), (int)(mHeight * scale));
51        mScratchAllocation = Allocation.createTyped(mRS, t);
52
53        // make small buffer
54        mScript = new ScriptC_resize(mRS);
55        mScript.set_gIn(mInPixelsAllocation);
56        mScript.set_gWidthIn(mWidth);
57        mScript.set_gHeightIn(mHeight);
58        mScript.set_scale(1.f / scale);
59        mScript.forEach_nearest(mScratchAllocation);
60
61        // setup normal ops
62        mScript.set_gIn(mScratchAllocation);
63        mScript.set_gWidthIn(t.getX());
64        mScript.set_gHeightIn(t.getY());
65        mScript.set_scale(scale);
66        //mScript.forEach_nearest(mScratchAllocation);
67
68        mIntrinsic = ScriptIntrinsicResize.create(mRS);
69        mIntrinsic.setInput(mScratchAllocation);
70    }
71
72    public void runTest() {
73        if (mUseIntrinsic) {
74            mIntrinsic.forEach_bicubic(mOutPixelsAllocation);
75        } else {
76            mScript.forEach_bicubic(mOutPixelsAllocation);
77            //mScript.forEach_nearest(mOutPixelsAllocation);
78        }
79    }
80
81}
82