1/*
2 * Copyright (C) 2011 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.perftest;
18
19import android.os.Environment;
20import android.content.res.Resources;
21import android.renderscript.*;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24
25
26import android.util.Log;
27
28
29public class FillTest implements RsBenchBaseTest{
30
31    private static final String TAG = "FillTest";
32    private RenderScriptGL mRS;
33    private Resources mRes;
34
35    // Custom shaders
36    private ProgramFragment mProgFragmentMultitex;
37    private ProgramFragment mProgFragmentSingletex;
38    private ProgramFragment mProgFragmentSingletexModulate;
39    private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
40    int mBenchmarkDimX;
41    int mBenchmarkDimY;
42
43    private ScriptC_fill_test mFillScript;
44    ScriptField_TestScripts_s.Item[] mTests;
45    ScriptField_FillTestFragData_s mFragData;
46
47    private final String[] mNames = {
48        "Fill screen 10x singletexture",
49        "Fill screen 10x 3tex multitexture",
50        "Fill screen 10x blended singletexture",
51        "Fill screen 10x blended 3tex multitexture",
52        "Fill screen 3x modulate blended singletexture",
53        "Fill screen 1x modulate blended singletexture",
54    };
55
56    public FillTest() {
57        mOptionsARGB.inScaled = false;
58        mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;
59        mBenchmarkDimX = 1280;
60        mBenchmarkDimY = 720;
61    }
62
63    void addTest(int index, int testId, int blend, int quadCount) {
64        mTests[index] = new ScriptField_TestScripts_s.Item();
65        mTests[index].testScript = mFillScript;
66        mTests[index].testName = Allocation.createFromString(mRS,
67                                                             mNames[index],
68                                                             Allocation.USAGE_SCRIPT);
69        mTests[index].debugName = RsBenchRS.createZeroTerminatedAlloc(mRS,
70                                                                      mNames[index],
71                                                                      Allocation.USAGE_SCRIPT);
72
73        ScriptField_FillTestData_s.Item dataItem = new ScriptField_FillTestData_s.Item();
74        dataItem.testId = testId;
75        dataItem.blend = blend;
76        dataItem.quadCount = quadCount;
77        ScriptField_FillTestData_s testData = new ScriptField_FillTestData_s(mRS, 1);
78        testData.set(dataItem, 0, true);
79        mTests[index].testData = testData.getAllocation();
80    }
81
82    public boolean init(RenderScriptGL rs, Resources res) {
83        mRS = rs;
84        mRes = res;
85        initCustomShaders();
86        initFillScript();
87        mTests = new ScriptField_TestScripts_s.Item[mNames.length];
88
89        int index = 0;
90
91        addTest(index++, 1 /*testId*/, 0 /*blend*/, 10 /*quadCount*/);
92        addTest(index++, 0 /*testId*/, 0 /*blend*/, 10 /*quadCount*/);
93        addTest(index++, 1 /*testId*/, 1 /*blend*/, 10 /*quadCount*/);
94        addTest(index++, 0 /*testId*/, 1 /*blend*/, 10 /*quadCount*/);
95        addTest(index++, 2 /*testId*/, 1 /*blend*/, 3 /*quadCount*/);
96        addTest(index++, 2 /*testId*/, 1 /*blend*/, 1 /*quadCount*/);
97
98        return true;
99    }
100
101    public ScriptField_TestScripts_s.Item[] getTests() {
102        return mTests;
103    }
104
105    public String[] getTestNames() {
106        return mNames;
107    }
108
109    private void initCustomShaders() {
110        ProgramFragment.Builder pfbCustom = new ProgramFragment.Builder(mRS);
111        pfbCustom.setShader(mRes, R.raw.multitexf);
112        for (int texCount = 0; texCount < 3; texCount ++) {
113            pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
114        }
115        mProgFragmentMultitex = pfbCustom.create();
116
117        pfbCustom = new ProgramFragment.Builder(mRS);
118        pfbCustom.setShader(mRes, R.raw.singletexf);
119        pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
120        mProgFragmentSingletex = pfbCustom.create();
121
122        pfbCustom = new ProgramFragment.Builder(mRS);
123        pfbCustom.setShader(mRes, R.raw.singletexfm);
124        pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
125        mFragData = new ScriptField_FillTestFragData_s(mRS, 1);
126        pfbCustom.addConstant(mFragData.getType());
127        mProgFragmentSingletexModulate = pfbCustom.create();
128        mProgFragmentSingletexModulate.bindConstants(mFragData.getAllocation(), 0);
129    }
130
131    private Allocation loadTextureARGB(int id) {
132        Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
133        return Allocation.createFromBitmap(mRS, b,
134                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
135                Allocation.USAGE_GRAPHICS_TEXTURE);
136    }
137
138    private Allocation loadTextureRGB(int id) {
139        return Allocation.createFromBitmapResource(mRS, mRes, id,
140                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
141                Allocation.USAGE_GRAPHICS_TEXTURE);
142    }
143
144    void initFillScript() {
145        mFillScript = new ScriptC_fill_test(mRS, mRes, R.raw.fill_test);
146
147        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
148        ProgramVertexFixedFunction progVertex = pvb.create();
149        ProgramVertexFixedFunction.Constants PVA = new ProgramVertexFixedFunction.Constants(mRS);
150        ((ProgramVertexFixedFunction)progVertex).bindConstants(PVA);
151        Matrix4f proj = new Matrix4f();
152        proj.loadOrthoWindow(mBenchmarkDimX, mBenchmarkDimY);
153        PVA.setProjection(proj);
154        mFillScript.set_gProgVertex(progVertex);
155
156        mFillScript.set_gProgFragmentTexture(mProgFragmentSingletex);
157        mFillScript.set_gProgFragmentTextureModulate(mProgFragmentSingletexModulate);
158        mFillScript.set_gProgFragmentMultitex(mProgFragmentMultitex);
159        mFillScript.set_gProgStoreBlendNone(ProgramStore.BLEND_NONE_DEPTH_NONE(mRS));
160        mFillScript.set_gProgStoreBlendAlpha(ProgramStore.BLEND_ALPHA_DEPTH_NONE(mRS));
161
162        mFillScript.set_gLinearClamp(Sampler.CLAMP_LINEAR(mRS));
163        mFillScript.set_gLinearWrap(Sampler.WRAP_LINEAR(mRS));
164        mFillScript.set_gTexTorus(loadTextureRGB(R.drawable.torusmap));
165        mFillScript.set_gTexOpaque(loadTextureRGB(R.drawable.data));
166        mFillScript.set_gTexTransparent(loadTextureARGB(R.drawable.leaf));
167        mFillScript.set_gTexChecker(loadTextureRGB(R.drawable.checker));
168
169        mFillScript.bind_gFragData(mFragData);
170    }
171}
172