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.fbotest; 18 19 import java.io.Writer; 20 21 import android.content.res.Resources; 22 import android.renderscript.*; 23 import android.renderscript.Element.DataType; 24 import android.renderscript.Element.DataKind; 25 import android.renderscript.ProgramStore.DepthFunc; 26 import android.renderscript.Type.Builder; 27 import android.util.Log; 28 29 30 public class FBOSyncRS { 31 FBOSyncRS()32 public FBOSyncRS() { 33 } 34 init(RenderScriptGL rs, Resources res)35 public void init(RenderScriptGL rs, Resources res) { 36 mRS = rs; 37 mRes = res; 38 initRS(); 39 } 40 surfaceChanged()41 public void surfaceChanged() { 42 mRS.getWidth(); 43 mRS.getHeight(); 44 } 45 46 private Resources mRes; 47 private RenderScriptGL mRS; 48 private Sampler mSampler; 49 private ProgramStore mPSBackground; 50 private ProgramFragment mPFBackground; 51 private ProgramVertex mPVBackground; 52 private ProgramVertexFixedFunction.Constants mPVA; 53 54 private Allocation mGridImage; 55 private Allocation mOffscreen; 56 private Allocation mOffscreenDepth; 57 private Allocation mAllocPV; 58 private Allocation mReadBackTest; 59 60 private Font mItalic; 61 private Allocation mTextAlloc; 62 63 private ScriptField_MeshInfo mMeshes; 64 private ScriptC_fbosync mScript; 65 66 onActionDown(float x, float y)67 public void onActionDown(float x, float y) { 68 mScript.invoke_onActionDown(x, y); 69 } 70 onActionScale(float scale)71 public void onActionScale(float scale) { 72 mScript.invoke_onActionScale(scale); 73 } 74 onActionMove(float x, float y)75 public void onActionMove(float x, float y) { 76 mScript.invoke_onActionMove(x, y); 77 } 78 initPFS()79 private void initPFS() { 80 ProgramStore.Builder b = new ProgramStore.Builder(mRS); 81 82 b.setDepthFunc(ProgramStore.DepthFunc.LESS); 83 b.setDitherEnabled(false); 84 b.setDepthMaskEnabled(true); 85 mPSBackground = b.create(); 86 87 mScript.set_gPFSBackground(mPSBackground); 88 } 89 initPF()90 private void initPF() { 91 Sampler.Builder bs = new Sampler.Builder(mRS); 92 bs.setMinification(Sampler.Value.LINEAR); 93 bs.setMagnification(Sampler.Value.LINEAR); 94 bs.setWrapS(Sampler.Value.CLAMP); 95 bs.setWrapT(Sampler.Value.CLAMP); 96 mSampler = bs.create(); 97 98 ProgramFragmentFixedFunction.Builder b = new ProgramFragmentFixedFunction.Builder(mRS); 99 b.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE, 100 ProgramFragmentFixedFunction.Builder.Format.RGBA, 0); 101 mPFBackground = b.create(); 102 mPFBackground.bindSampler(mSampler, 0); 103 104 mScript.set_gPFBackground(mPFBackground); 105 } 106 initPV()107 private void initPV() { 108 ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS); 109 mPVBackground = pvb.create(); 110 111 mPVA = new ProgramVertexFixedFunction.Constants(mRS); 112 ((ProgramVertexFixedFunction)mPVBackground).bindConstants(mPVA); 113 114 mScript.set_gPVBackground(mPVBackground); 115 } 116 loadImage()117 private void loadImage() { 118 mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot, 119 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, 120 Allocation.USAGE_GRAPHICS_TEXTURE); 121 mScript.set_gTGrid(mGridImage); 122 } 123 initTextAllocation(String fileName)124 private void initTextAllocation(String fileName) { 125 String allocString = "Displaying file: " + fileName; 126 mTextAlloc = Allocation.createFromString(mRS, allocString, Allocation.USAGE_SCRIPT); 127 mScript.set_gTextAlloc(mTextAlloc); 128 } 129 initMeshes(FileA3D model)130 private void initMeshes(FileA3D model) { 131 int numEntries = model.getIndexEntryCount(); 132 int numMeshes = 0; 133 for (int i = 0; i < numEntries; i ++) { 134 FileA3D.IndexEntry entry = model.getIndexEntry(i); 135 if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) { 136 numMeshes ++; 137 } 138 } 139 140 if (numMeshes > 0) { 141 mMeshes = new ScriptField_MeshInfo(mRS, numMeshes); 142 143 for (int i = 0; i < numEntries; i ++) { 144 FileA3D.IndexEntry entry = model.getIndexEntry(i); 145 if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) { 146 Mesh mesh = entry.getMesh(); 147 mMeshes.set_mMesh(i, mesh, false); 148 mMeshes.set_mNumIndexSets(i, mesh.getPrimitiveCount(), false); 149 } 150 } 151 mMeshes.copyAll(); 152 } else { 153 throw new RSRuntimeException("No valid meshes in file"); 154 } 155 156 mScript.bind_gMeshes(mMeshes); 157 mScript.invoke_updateMeshInfo(); 158 } 159 loadA3DFile(String path)160 public void loadA3DFile(String path) { 161 FileA3D model = FileA3D.createFromFile(mRS, path); 162 initMeshes(model); 163 initTextAllocation(path); 164 } 165 initRS()166 private void initRS() { 167 168 mScript = new ScriptC_fbosync(mRS, mRes, R.raw.fbosync); 169 170 initPFS(); 171 initPF(); 172 initPV(); 173 174 loadImage(); 175 176 Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS)); 177 b.setX(512).setY(512); 178 mOffscreen = Allocation.createTyped(mRS, 179 b.create(), 180 Allocation.USAGE_SCRIPT | 181 Allocation.USAGE_GRAPHICS_TEXTURE | 182 Allocation.USAGE_GRAPHICS_RENDER_TARGET); 183 mScript.set_gOffscreen(mOffscreen); 184 185 mReadBackTest = Allocation.createTyped(mRS, 186 b.create(), 187 Allocation.USAGE_SCRIPT | 188 Allocation.USAGE_GRAPHICS_TEXTURE); 189 mScript.set_gReadBackTest(mReadBackTest); 190 191 b = new Type.Builder(mRS, 192 Element.createPixel(mRS, DataType.UNSIGNED_16, 193 DataKind.PIXEL_DEPTH)); 194 b.setX(512).setY(512); 195 mOffscreenDepth = Allocation.createTyped(mRS, 196 b.create(), 197 Allocation.USAGE_GRAPHICS_RENDER_TARGET); 198 mScript.set_gOffscreenDepth(mOffscreenDepth); 199 200 FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot); 201 initMeshes(model); 202 203 mItalic = Font.create(mRS, mRes, "serif", Font.Style.ITALIC, 8); 204 mScript.set_gItalic(mItalic); 205 206 initTextAllocation("R.raw.robot"); 207 208 mRS.bindRootScript(mScript); 209 } 210 } 211 212 213 214