1 /* 2 * Copyright (C) 2008-2012 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 android.renderscript; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.content.res.AssetManager; 21 import android.content.res.Resources; 22 23 import java.io.File; 24 import java.io.InputStream; 25 26 /** 27 * @hide 28 * @deprecated in API 16 29 * FileA3D allows users to load RenderScript objects from files 30 * or resources stored on disk. It could be used to load items 31 * such as 3D geometry data converted to a RenderScript format from 32 * content creation tools. Currently only meshes are supported 33 * in FileA3D. 34 * 35 * When successfully loaded, FileA3D will contain a list of 36 * index entries for all the objects stored inside it. 37 * 38 **/ 39 @Deprecated 40 public class FileA3D extends BaseObj { 41 42 /** 43 * @deprecated in API 16 44 * Specifies what renderscript object type is contained within 45 * the FileA3D IndexEntry 46 **/ 47 public enum EntryType { 48 49 /** 50 * @deprecated in API 16 51 * Unknown or or invalid object, nothing will be loaded 52 **/ 53 UNKNOWN (0), 54 /** 55 * @deprecated in API 16 56 * RenderScript Mesh object 57 **/ 58 @UnsupportedAppUsage 59 MESH (1); 60 61 int mID; EntryType(int id)62 EntryType(int id) { 63 mID = id; 64 } 65 toEntryType(int intID)66 static EntryType toEntryType(int intID) { 67 return EntryType.values()[intID]; 68 } 69 } 70 71 /** 72 * @deprecated in API 16 73 * IndexEntry contains information about one of the RenderScript 74 * objects inside the file's index. It could be used to query the 75 * object's type and also name and load the object itself if 76 * necessary. 77 */ 78 public static class IndexEntry { 79 RenderScript mRS; 80 int mIndex; 81 long mID; 82 String mName; 83 EntryType mEntryType; 84 BaseObj mLoadedObj; 85 86 /** 87 * @deprecated in API 16 88 * Returns the name of a renderscript object the index entry 89 * describes 90 * 91 * @return name of a renderscript object the index entry 92 * describes 93 * 94 */ getName()95 public String getName() { 96 return mName; 97 } 98 99 /** 100 * @deprecated in API 16 101 * Returns the type of a renderscript object the index entry 102 * describes 103 * @return type of a renderscript object the index entry 104 * describes 105 */ 106 @UnsupportedAppUsage getEntryType()107 public EntryType getEntryType() { 108 return mEntryType; 109 } 110 111 /** 112 * @deprecated in API 16 113 * Used to load the object described by the index entry 114 * @return base renderscript object described by the entry 115 */ 116 @UnsupportedAppUsage getObject()117 public BaseObj getObject() { 118 mRS.validate(); 119 BaseObj obj = internalCreate(mRS, this); 120 return obj; 121 } 122 123 /** 124 * @deprecated in API 16 125 * Used to load the mesh described by the index entry, object 126 * described by the index entry must be a renderscript mesh 127 * 128 * @return renderscript mesh object described by the entry 129 */ getMesh()130 public Mesh getMesh() { 131 return (Mesh)getObject(); 132 } 133 internalCreate(RenderScript rs, IndexEntry entry)134 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) { 135 if(entry.mLoadedObj != null) { 136 return entry.mLoadedObj; 137 } 138 139 // to be purged on cleanup 140 if(entry.mEntryType == EntryType.UNKNOWN) { 141 return null; 142 } 143 144 long objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex); 145 if(objectID == 0) { 146 return null; 147 } 148 149 switch (entry.mEntryType) { 150 case MESH: 151 entry.mLoadedObj = new Mesh(objectID, rs); 152 break; 153 154 default: 155 throw new RSRuntimeException("Unrecognized object type in file."); 156 } 157 158 entry.mLoadedObj.updateFromNative(); 159 return entry.mLoadedObj; 160 } 161 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type)162 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) { 163 mRS = rs; 164 mIndex = index; 165 mID = id; 166 mName = name; 167 mEntryType = type; 168 mLoadedObj = null; 169 } 170 } 171 172 IndexEntry[] mFileEntries; 173 InputStream mInputStream; 174 FileA3D(long id, RenderScript rs, InputStream stream)175 FileA3D(long id, RenderScript rs, InputStream stream) { 176 super(id, rs); 177 mInputStream = stream; 178 guard.open("destroy"); 179 } 180 initEntries()181 private void initEntries() { 182 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS)); 183 if(numFileEntries <= 0) { 184 return; 185 } 186 187 mFileEntries = new IndexEntry[numFileEntries]; 188 int[] ids = new int[numFileEntries]; 189 String[] names = new String[numFileEntries]; 190 191 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names); 192 193 for(int i = 0; i < numFileEntries; i ++) { 194 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i])); 195 } 196 } 197 198 /** 199 * @deprecated in API 16 200 * Returns the number of objects stored inside the a3d file 201 * 202 * @return the number of objects stored inside the a3d file 203 */ getIndexEntryCount()204 public int getIndexEntryCount() { 205 if(mFileEntries == null) { 206 return 0; 207 } 208 return mFileEntries.length; 209 } 210 211 /** 212 * @deprecated in API 16 213 * Returns an index entry from the list of all objects inside 214 * FileA3D 215 * 216 * @param index number of the entry from the list to return 217 * 218 * @return entry in the a3d file described by the index 219 */ 220 @UnsupportedAppUsage getIndexEntry(int index)221 public IndexEntry getIndexEntry(int index) { 222 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) { 223 return null; 224 } 225 return mFileEntries[index]; 226 } 227 228 /** 229 * @deprecated in API 16 230 * Creates a FileA3D object from an asset stored on disk 231 * 232 * @param rs Context to which the object will belong. 233 * @param mgr asset manager used to load asset 234 * @param path location of the file to load 235 * 236 * @return a3d file containing renderscript objects 237 */ createFromAsset(RenderScript rs, AssetManager mgr, String path)238 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) { 239 rs.validate(); 240 long fileId = rs.nFileA3DCreateFromAsset(mgr, path); 241 242 if(fileId == 0) { 243 throw new RSRuntimeException("Unable to create a3d file from asset " + path); 244 } 245 FileA3D fa3d = new FileA3D(fileId, rs, null); 246 fa3d.initEntries(); 247 return fa3d; 248 } 249 250 /** 251 * @deprecated in API 16 252 * Creates a FileA3D object from a file stored on disk 253 * 254 * @param rs Context to which the object will belong. 255 * @param path location of the file to load 256 * 257 * @return a3d file containing renderscript objects 258 */ createFromFile(RenderScript rs, String path)259 static public FileA3D createFromFile(RenderScript rs, String path) { 260 long fileId = rs.nFileA3DCreateFromFile(path); 261 262 if(fileId == 0) { 263 throw new RSRuntimeException("Unable to create a3d file from " + path); 264 } 265 FileA3D fa3d = new FileA3D(fileId, rs, null); 266 fa3d.initEntries(); 267 return fa3d; 268 } 269 270 /** 271 * @deprecated in API 16 272 * Creates a FileA3D object from a file stored on disk 273 * 274 * @param rs Context to which the object will belong. 275 * @param path location of the file to load 276 * 277 * @return a3d file containing renderscript objects 278 */ createFromFile(RenderScript rs, File path)279 static public FileA3D createFromFile(RenderScript rs, File path) { 280 return createFromFile(rs, path.getAbsolutePath()); 281 } 282 283 /** 284 * @deprecated in API 16 285 * Creates a FileA3D object from an application resource 286 * 287 * @param rs Context to which the object will belong. 288 * @param res resource manager used for loading 289 * @param id resource to create FileA3D from 290 * 291 * @return a3d file containing renderscript objects 292 */ 293 @UnsupportedAppUsage createFromResource(RenderScript rs, Resources res, int id)294 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) { 295 296 rs.validate(); 297 InputStream is = null; 298 try { 299 is = res.openRawResource(id); 300 } catch (Exception e) { 301 throw new RSRuntimeException("Unable to open resource " + id); 302 } 303 304 long fileId = 0; 305 if (is instanceof AssetManager.AssetInputStream) { 306 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset(); 307 fileId = rs.nFileA3DCreateFromAssetStream(asset); 308 } else { 309 throw new RSRuntimeException("Unsupported asset stream"); 310 } 311 312 if(fileId == 0) { 313 throw new RSRuntimeException("Unable to create a3d file from resource " + id); 314 } 315 FileA3D fa3d = new FileA3D(fileId, rs, is); 316 fa3d.initEntries(); 317 return fa3d; 318 319 } 320 } 321