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 17 package com.android.perftest; 18 19 import android.os.Environment; 20 import android.content.res.Resources; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.renderscript.*; 24 import android.renderscript.Element.DataKind; 25 import android.renderscript.Element.DataType; 26 import android.renderscript.Allocation.MipmapControl; 27 import android.renderscript.Program.TextureType; 28 import android.renderscript.RenderScript.RSMessageHandler; 29 import android.renderscript.Mesh.Primitive; 30 import android.renderscript.Matrix4f; 31 import android.renderscript.ProgramVertexFixedFunction; 32 33 import android.util.Log; 34 35 36 public class MeshTest implements RsBenchBaseTest{ 37 38 private static final String TAG = "MeshTest"; 39 private RenderScriptGL mRS; 40 private Resources mRes; 41 42 int mBenchmarkDimX; 43 int mBenchmarkDimY; 44 45 private Mesh m10by10Mesh; 46 private Mesh m100by100Mesh; 47 private Mesh mWbyHMesh; 48 49 private ScriptC_mesh_test mGeoScript; 50 51 private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options(); 52 53 ScriptField_TestScripts_s.Item[] mTests; 54 55 private final String[] mNames = { 56 "Full screen mesh 10 by 10", 57 "Full screen mesh 100 by 100", 58 "Full screen mesh W / 4 by H / 4" 59 }; 60 61 public MeshTest() { 62 mBenchmarkDimX = 1280; 63 mBenchmarkDimY = 720; 64 } 65 66 void addTest(int index, int meshNum) { 67 mTests[index] = new ScriptField_TestScripts_s.Item(); 68 mTests[index].testScript = mGeoScript; 69 mTests[index].testName = Allocation.createFromString(mRS, 70 mNames[index], 71 Allocation.USAGE_SCRIPT); 72 mTests[index].debugName = RsBenchRS.createZeroTerminatedAlloc(mRS, 73 mNames[index], 74 Allocation.USAGE_SCRIPT); 75 76 ScriptField_MeshTestData_s.Item dataItem = new ScriptField_MeshTestData_s.Item(); 77 dataItem.meshNum = meshNum; 78 ScriptField_MeshTestData_s testData = new ScriptField_MeshTestData_s(mRS, 1); 79 testData.set(dataItem, 0, true); 80 mTests[index].testData = testData.getAllocation(); 81 } 82 83 public boolean init(RenderScriptGL rs, Resources res) { 84 mRS = rs; 85 mRes = res; 86 initGeoScript(); 87 mTests = new ScriptField_TestScripts_s.Item[mNames.length]; 88 89 int index = 0; 90 addTest(index++, 0 /*meshNum*/); 91 addTest(index++, 1 /*meshNum*/); 92 addTest(index++, 2 /*meshNum*/); 93 94 return true; 95 } 96 97 public ScriptField_TestScripts_s.Item[] getTests() { 98 return mTests; 99 } 100 101 public String[] getTestNames() { 102 return mNames; 103 } 104 105 private Mesh getMbyNMesh(float width, float height, int wResolution, int hResolution) { 106 107 Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS, 108 2, Mesh.TriangleMeshBuilder.TEXTURE_0); 109 110 for (int y = 0; y <= hResolution; y++) { 111 final float normalizedY = (float)y / hResolution; 112 final float yOffset = (normalizedY - 0.5f) * height; 113 for (int x = 0; x <= wResolution; x++) { 114 float normalizedX = (float)x / wResolution; 115 float xOffset = (normalizedX - 0.5f) * width; 116 tmb.setTexture((float)x % 2, (float)y % 2); 117 tmb.addVertex(xOffset, yOffset); 118 } 119 } 120 121 for (int y = 0; y < hResolution; y++) { 122 final int curY = y * (wResolution + 1); 123 final int belowY = (y + 1) * (wResolution + 1); 124 for (int x = 0; x < wResolution; x++) { 125 int curV = curY + x; 126 int belowV = belowY + x; 127 tmb.addTriangle(curV, belowV, curV + 1); 128 tmb.addTriangle(belowV, belowV + 1, curV + 1); 129 } 130 } 131 132 return tmb.create(true); 133 } 134 135 private Allocation loadTextureRGB(int id) { 136 return Allocation.createFromBitmapResource(mRS, mRes, id, 137 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 138 Allocation.USAGE_GRAPHICS_TEXTURE); 139 } 140 141 void initGeoScript() { 142 mGeoScript = new ScriptC_mesh_test(mRS, mRes, R.raw.mesh_test); 143 144 ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS); 145 ProgramVertexFixedFunction progVertex = pvb.create(); 146 ProgramVertexFixedFunction.Constants PVA = new ProgramVertexFixedFunction.Constants(mRS); 147 ((ProgramVertexFixedFunction)progVertex).bindConstants(PVA); 148 Matrix4f proj = new Matrix4f(); 149 proj.loadOrthoWindow(mBenchmarkDimX, mBenchmarkDimY); 150 PVA.setProjection(proj); 151 152 mGeoScript.set_gProgVertex(progVertex); 153 ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS); 154 texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE, 155 ProgramFragmentFixedFunction.Builder.Format.RGBA, 0); 156 mGeoScript.set_gProgFragmentTexture(texBuilder.create()); 157 mGeoScript.set_gProgStoreBlendNone(ProgramStore.BLEND_NONE_DEPTH_NONE(mRS)); 158 159 mGeoScript.set_gLinearClamp(Sampler.CLAMP_LINEAR(mRS)); 160 mGeoScript.set_gTexOpaque(loadTextureRGB(R.drawable.data)); 161 162 m10by10Mesh = getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, 10, 10); 163 m100by100Mesh = getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, 100, 100); 164 mWbyHMesh= getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, mBenchmarkDimX/4, mBenchmarkDimY/4); 165 166 mGeoScript.set_g10by10Mesh(m10by10Mesh); 167 mGeoScript.set_g100by100Mesh(m100by100Mesh); 168 mGeoScript.set_gWbyHMesh(mWbyHMesh); 169 } 170 } 171