FileA3D.java revision 06d69de78845659e6904ae4964e606a7f1a6a4a8
1/* 2 * Copyright (C) 2008 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 android.renderscript; 18 19import java.io.IOException; 20import java.io.InputStream; 21 22import android.content.res.Resources; 23import android.content.res.AssetManager; 24import android.graphics.Bitmap; 25import android.graphics.BitmapFactory; 26import android.util.Log; 27import android.util.TypedValue; 28 29/** 30 * @hide 31 * 32 **/ 33public class FileA3D extends BaseObj { 34 35 public enum ClassID { 36 37 UNKNOWN, 38 MESH, 39 TYPE, 40 ELEMENT, 41 ALLOCATION, 42 PROGRAM_VERTEX, 43 PROGRAM_RASTER, 44 PROGRAM_FRAGMENT, 45 PROGRAM_STORE, 46 SAMPLER, 47 ANIMATION, 48 ADAPTER_1D, 49 ADAPTER_2D, 50 SCRIPT_C; 51 52 public static ClassID toClassID(int intID) { 53 return ClassID.values()[intID]; 54 } 55 } 56 57 // Read only class with index entries 58 public static class IndexEntry { 59 RenderScript mRS; 60 int mIndex; 61 int mID; 62 String mName; 63 ClassID mClassID; 64 BaseObj mLoadedObj; 65 66 public String getName() { 67 return mName; 68 } 69 70 public ClassID getClassID() { 71 return mClassID; 72 } 73 74 public BaseObj getObject() { 75 mRS.validate(); 76 BaseObj obj = internalCreate(mRS, this); 77 return obj; 78 } 79 80 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) { 81 if(entry.mLoadedObj != null) { 82 return entry.mLoadedObj; 83 } 84 85 if(entry.mClassID == ClassID.UNKNOWN) { 86 return null; 87 } 88 89 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex); 90 if(objectID == 0) { 91 return null; 92 } 93 94 switch (entry.mClassID) { 95 case MESH: 96 entry.mLoadedObj = new Mesh(objectID, rs); 97 break; 98 case TYPE: 99 entry.mLoadedObj = new Type(objectID, rs); 100 break; 101 case ELEMENT: 102 entry.mLoadedObj = null; 103 break; 104 case ALLOCATION: 105 entry.mLoadedObj = null; 106 break; 107 case PROGRAM_VERTEX: 108 entry.mLoadedObj = new ProgramVertex(objectID, rs); 109 break; 110 case PROGRAM_RASTER: 111 break; 112 case PROGRAM_FRAGMENT: 113 break; 114 case PROGRAM_STORE: 115 break; 116 case SAMPLER: 117 break; 118 case ANIMATION: 119 break; 120 case ADAPTER_1D: 121 break; 122 case ADAPTER_2D: 123 break; 124 case SCRIPT_C: 125 break; 126 } 127 128 entry.mLoadedObj.updateFromNative(); 129 130 return entry.mLoadedObj; 131 } 132 133 IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) { 134 mRS = rs; 135 mIndex = index; 136 mID = id; 137 mName = name; 138 mClassID = classID; 139 mLoadedObj = null; 140 } 141 } 142 143 IndexEntry[] mFileEntries; 144 InputStream mInputStream; 145 146 FileA3D(int id, RenderScript rs, InputStream stream) { 147 super(id, rs); 148 mInputStream = stream; 149 } 150 151 private void initEntries() { 152 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID()); 153 if(numFileEntries <= 0) { 154 return; 155 } 156 157 mFileEntries = new IndexEntry[numFileEntries]; 158 int[] ids = new int[numFileEntries]; 159 String[] names = new String[numFileEntries]; 160 161 mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names); 162 163 for(int i = 0; i < numFileEntries; i ++) { 164 mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], ClassID.toClassID(ids[i])); 165 } 166 } 167 168 public int getNumIndexEntries() { 169 if(mFileEntries == null) { 170 return 0; 171 } 172 return mFileEntries.length; 173 } 174 175 public IndexEntry getIndexEntry(int index) { 176 if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) { 177 return null; 178 } 179 return mFileEntries[index]; 180 } 181 182 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) 183 throws IllegalArgumentException { 184 185 rs.validate(); 186 InputStream is = null; 187 try { 188 final TypedValue value = new TypedValue(); 189 is = res.openRawResource(id, value); 190 191 int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); 192 193 int fileId = rs.nFileA3DCreateFromAssetStream(asset); 194 195 if(fileId == 0) { 196 throw new IllegalStateException("Load failed."); 197 } 198 FileA3D fa3d = new FileA3D(fileId, rs, is); 199 fa3d.initEntries(); 200 return fa3d; 201 202 } catch (Exception e) { 203 // Ignore 204 } 205 206 return null; 207 } 208} 209