blob: 42b508b721955c383f3a37b13a3c79db4d51f70a [file] [log] [blame]
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001/*
Jason Sams65c80f82012-05-08 17:30:26 -07002 * Copyright (C) 2008-2012 The Android Open Source Project
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07003 *
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
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080019import java.io.File;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070020import java.io.IOException;
21import java.io.InputStream;
22
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070023import android.content.res.AssetManager;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080024import android.content.res.Resources;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070025import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.util.Log;
28import android.util.TypedValue;
29
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Jason Sams65c80f82012-05-08 17:30:26 -070031 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080032 * FileA3D allows users to load Renderscript objects from files
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080033 * or resources stored on disk. It could be used to load items
Robert Ly11518ac2011-02-09 13:57:06 -080034 * such as 3D geometry data converted to a Renderscript format from
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080035 * content creation tools. Currently only meshes are supported
36 * in FileA3D.
37 *
38 * When successfully loaded, FileA3D will contain a list of
39 * index entries for all the objects stored inside it.
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070040 *
41 **/
42public class FileA3D extends BaseObj {
43
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070044 /**
Jason Sams65c80f82012-05-08 17:30:26 -070045 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080046 * Specifies what renderscript object type is contained within
47 * the FileA3D IndexEntry
48 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080049 public enum EntryType {
50
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070051 /**
Jason Sams65c80f82012-05-08 17:30:26 -070052 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080053 * Unknown or or invalid object, nothing will be loaded
54 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080055 UNKNOWN (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070056 /**
Jason Sams65c80f82012-05-08 17:30:26 -070057 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080058 * Renderscript Mesh object
59 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080060 MESH (1);
61
62 int mID;
63 EntryType(int id) {
64 mID = id;
65 }
66
67 static EntryType toEntryType(int intID) {
68 return EntryType.values()[intID];
69 }
70 }
71
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070072 /**
Jason Sams65c80f82012-05-08 17:30:26 -070073 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080074 * IndexEntry contains information about one of the Renderscript
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080075 * objects inside the file's index. It could be used to query the
Robert Ly11518ac2011-02-09 13:57:06 -080076 * object's type and also name and load the object itself if
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080077 * necessary.
78 */
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070079 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070080 RenderScript mRS;
81 int mIndex;
82 int mID;
83 String mName;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080084 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070085 BaseObj mLoadedObj;
86
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070087 /**
Jason Sams65c80f82012-05-08 17:30:26 -070088 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080089 * Returns the name of a renderscript object the index entry
90 * describes
91 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080092 * @return name of a renderscript object the index entry
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080093 * describes
94 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080095 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070096 public String getName() {
97 return mName;
98 }
99
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700100 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700101 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800102 * Returns the type of a renderscript object the index entry
103 * describes
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800104 * @return type of a renderscript object the index entry
105 * describes
106 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800107 public EntryType getEntryType() {
108 return mEntryType;
109 }
110
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700111 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700112 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800113 * Used to load the object described by the index entry
114 * @return base renderscript object described by the entry
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800115 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700116 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700117 mRS.validate();
118 BaseObj obj = internalCreate(mRS, this);
119 return obj;
120 }
121
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700122 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700123 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800124 * Used to load the mesh described by the index entry, object
125 * described by the index entry must be a renderscript mesh
126 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800127 * @return renderscript mesh object described by the entry
128 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800129 public Mesh getMesh() {
130 return (Mesh)getObject();
131 }
132
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700133 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
134 if(entry.mLoadedObj != null) {
135 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700136 }
137
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800138 // to be purged on cleanup
139 if(entry.mEntryType == EntryType.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700140 return null;
141 }
142
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700143 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700144 if(objectID == 0) {
145 return null;
146 }
147
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800148 switch (entry.mEntryType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700149 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700150 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700151 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700152 }
153
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700154 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700155 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700156 }
157
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800158 IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700159 mRS = rs;
160 mIndex = index;
161 mID = id;
162 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800163 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700164 mLoadedObj = null;
165 }
166 }
167
168 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700169 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700170
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700171 FileA3D(int id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700172 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700173 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700174 }
175
176 private void initEntries() {
Jason Samse07694b2012-04-03 15:36:36 -0700177 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700178 if(numFileEntries <= 0) {
179 return;
180 }
181
182 mFileEntries = new IndexEntry[numFileEntries];
183 int[] ids = new int[numFileEntries];
184 String[] names = new String[numFileEntries];
185
Jason Samse07694b2012-04-03 15:36:36 -0700186 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700187
188 for(int i = 0; i < numFileEntries; i ++) {
Jason Samse07694b2012-04-03 15:36:36 -0700189 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700190 }
191 }
192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700194 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800195 * Returns the number of objects stored inside the a3d file
196 *
197 * @return the number of objects stored inside the a3d file
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800198 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800199 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700200 if(mFileEntries == null) {
201 return 0;
202 }
203 return mFileEntries.length;
204 }
205
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700206 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700207 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800208 * Returns an index entry from the list of all objects inside
209 * FileA3D
210 *
211 * @param index number of the entry from the list to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800212 *
213 * @return entry in the a3d file described by the index
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800214 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700215 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800216 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700217 return null;
218 }
219 return mFileEntries[index];
220 }
221
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700222 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700223 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800224 * Creates a FileA3D object from an asset stored on disk
225 *
226 * @param rs Context to which the object will belong.
227 * @param mgr asset manager used to load asset
228 * @param path location of the file to load
229 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800230 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800231 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800232 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
233 rs.validate();
234 int fileId = rs.nFileA3DCreateFromAsset(mgr, path);
235
236 if(fileId == 0) {
237 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
238 }
239 FileA3D fa3d = new FileA3D(fileId, rs, null);
240 fa3d.initEntries();
241 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800242 }
243
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700244 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700245 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800246 * Creates a FileA3D object from a file stored on disk
247 *
248 * @param rs Context to which the object will belong.
249 * @param path location of the file to load
250 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800251 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800252 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800253 static public FileA3D createFromFile(RenderScript rs, String path) {
254 int fileId = rs.nFileA3DCreateFromFile(path);
255
256 if(fileId == 0) {
257 throw new RSRuntimeException("Unable to create a3d file from " + path);
258 }
259 FileA3D fa3d = new FileA3D(fileId, rs, null);
260 fa3d.initEntries();
261 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800262 }
263
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700264 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700265 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800266 * Creates a FileA3D object from a file stored on disk
267 *
268 * @param rs Context to which the object will belong.
269 * @param path location of the file to load
270 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800271 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800272 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800273 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800274 return createFromFile(rs, path.getAbsolutePath());
275 }
276
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700277 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700278 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800279 * Creates a FileA3D object from an application resource
280 *
281 * @param rs Context to which the object will belong.
282 * @param res resource manager used for loading
283 * @param id resource to create FileA3D from
284 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800285 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800286 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800287 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700288
289 rs.validate();
290 InputStream is = null;
291 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800292 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700293 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800294 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700295 }
296
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800297 int fileId = 0;
298 if (is instanceof AssetManager.AssetInputStream) {
299 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
300 fileId = rs.nFileA3DCreateFromAssetStream(asset);
301 } else {
302 throw new RSRuntimeException("Unsupported asset stream");
303 }
304
305 if(fileId == 0) {
306 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
307 }
308 FileA3D fa3d = new FileA3D(fileId, rs, is);
309 fa3d.initEntries();
310 return fa3d;
311
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700312 }
313}