2 * Copyright (C) 2008-2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.renderscript;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import android.content.res.AssetManager;
24 import android.content.res.Resources;
25 import android.graphics.Bitmap;
26 import android.graphics.BitmapFactory;
27 import android.util.Log;
28 import android.util.TypedValue;
32 * @deprecated in API 16
33 * FileA3D allows users to load Renderscript objects from files
34 * or resources stored on disk. It could be used to load items
35 * such as 3D geometry data converted to a Renderscript format from
36 * content creation tools. Currently only meshes are supported
39 * When successfully loaded, FileA3D will contain a list of
40 * index entries for all the objects stored inside it.
43 public class FileA3D extends BaseObj {
46 * @deprecated in API 16
47 * Specifies what renderscript object type is contained within
48 * the FileA3D IndexEntry
50 public enum EntryType {
53 * @deprecated in API 16
54 * Unknown or or invalid object, nothing will be loaded
58 * @deprecated in API 16
59 * Renderscript Mesh object
68 static EntryType toEntryType(int intID) {
69 return EntryType.values()[intID];
74 * @deprecated in API 16
75 * IndexEntry contains information about one of the Renderscript
76 * objects inside the file's index. It could be used to query the
77 * object's type and also name and load the object itself if
80 public static class IndexEntry {
89 * @deprecated in API 16
90 * Returns the name of a renderscript object the index entry
93 * @return name of a renderscript object the index entry
97 public String getName() {
102 * @deprecated in API 16
103 * Returns the type of a renderscript object the index entry
105 * @return type of a renderscript object the index entry
108 public EntryType getEntryType() {
113 * @deprecated in API 16
114 * Used to load the object described by the index entry
115 * @return base renderscript object described by the entry
117 public BaseObj getObject() {
119 BaseObj obj = internalCreate(mRS, this);
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
128 * @return renderscript mesh object described by the entry
130 public Mesh getMesh() {
131 return (Mesh)getObject();
134 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
135 if(entry.mLoadedObj != null) {
136 return entry.mLoadedObj;
139 // to be purged on cleanup
140 if(entry.mEntryType == EntryType.UNKNOWN) {
144 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
149 switch (entry.mEntryType) {
151 entry.mLoadedObj = new Mesh(objectID, rs);
155 entry.mLoadedObj.updateFromNative();
156 return entry.mLoadedObj;
159 IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
169 IndexEntry[] mFileEntries;
170 InputStream mInputStream;
172 FileA3D(int id, RenderScript rs, InputStream stream) {
174 mInputStream = stream;
177 private void initEntries() {
178 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
179 if(numFileEntries <= 0) {
183 mFileEntries = new IndexEntry[numFileEntries];
184 int[] ids = new int[numFileEntries];
185 String[] names = new String[numFileEntries];
187 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
189 for(int i = 0; i < numFileEntries; i ++) {
190 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
195 * @deprecated in API 16
196 * Returns the number of objects stored inside the a3d file
198 * @return the number of objects stored inside the a3d file
200 public int getIndexEntryCount() {
201 if(mFileEntries == null) {
204 return mFileEntries.length;
208 * @deprecated in API 16
209 * Returns an index entry from the list of all objects inside
212 * @param index number of the entry from the list to return
214 * @return entry in the a3d file described by the index
216 public IndexEntry getIndexEntry(int index) {
217 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
220 return mFileEntries[index];
224 * @deprecated in API 16
225 * Creates a FileA3D object from an asset stored on disk
227 * @param rs Context to which the object will belong.
228 * @param mgr asset manager used to load asset
229 * @param path location of the file to load
231 * @return a3d file containing renderscript objects
233 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
235 int fileId = rs.nFileA3DCreateFromAsset(mgr, path);
238 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
240 FileA3D fa3d = new FileA3D(fileId, rs, null);
246 * @deprecated in API 16
247 * Creates a FileA3D object from a file stored on disk
249 * @param rs Context to which the object will belong.
250 * @param path location of the file to load
252 * @return a3d file containing renderscript objects
254 static public FileA3D createFromFile(RenderScript rs, String path) {
255 int fileId = rs.nFileA3DCreateFromFile(path);
258 throw new RSRuntimeException("Unable to create a3d file from " + path);
260 FileA3D fa3d = new FileA3D(fileId, rs, null);
266 * @deprecated in API 16
267 * Creates a FileA3D object from a file stored on disk
269 * @param rs Context to which the object will belong.
270 * @param path location of the file to load
272 * @return a3d file containing renderscript objects
274 static public FileA3D createFromFile(RenderScript rs, File path) {
275 return createFromFile(rs, path.getAbsolutePath());
279 * @deprecated in API 16
280 * Creates a FileA3D object from an application resource
282 * @param rs Context to which the object will belong.
283 * @param res resource manager used for loading
284 * @param id resource to create FileA3D from
286 * @return a3d file containing renderscript objects
288 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
291 InputStream is = null;
293 is = res.openRawResource(id);
294 } catch (Exception e) {
295 throw new RSRuntimeException("Unable to open resource " + id);
299 if (is instanceof AssetManager.AssetInputStream) {
300 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
301 fileId = rs.nFileA3DCreateFromAssetStream(asset);
303 throw new RSRuntimeException("Unsupported asset stream");
307 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
309 FileA3D fa3d = new FileA3D(fileId, rs, is);