blob: f0acb56b84adb0cefbad7e708bd77c54f0df33f2 [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.InputStream;
21
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070022import android.content.res.AssetManager;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080023import android.content.res.Resources;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070024
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070025/**
Tim Murraya9084222013-04-05 22:06:43 +000026 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070027 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070028 * FileA3D allows users to load RenderScript objects from files
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080029 * or resources stored on disk. It could be used to load items
Tim Murrayc11e25c2013-04-09 11:01:01 -070030 * such as 3D geometry data converted to a RenderScript format from
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080031 * content creation tools. Currently only meshes are supported
32 * in FileA3D.
33 *
34 * When successfully loaded, FileA3D will contain a list of
35 * index entries for all the objects stored inside it.
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070036 *
37 **/
38public class FileA3D extends BaseObj {
39
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070040 /**
Jason Sams65c80f82012-05-08 17:30:26 -070041 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080042 * Specifies what renderscript object type is contained within
43 * the FileA3D IndexEntry
44 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080045 public enum EntryType {
46
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070047 /**
Jason Sams65c80f82012-05-08 17:30:26 -070048 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080049 * Unknown or or invalid object, nothing will be loaded
50 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080051 UNKNOWN (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070052 /**
Jason Sams65c80f82012-05-08 17:30:26 -070053 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070054 * RenderScript Mesh object
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080055 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080056 MESH (1);
57
58 int mID;
59 EntryType(int id) {
60 mID = id;
61 }
62
63 static EntryType toEntryType(int intID) {
64 return EntryType.values()[intID];
65 }
66 }
67
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Jason Sams65c80f82012-05-08 17:30:26 -070069 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070070 * IndexEntry contains information about one of the RenderScript
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080071 * objects inside the file's index. It could be used to query the
Robert Ly11518ac2011-02-09 13:57:06 -080072 * object's type and also name and load the object itself if
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080073 * necessary.
74 */
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070075 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070076 RenderScript mRS;
77 int mIndex;
Tim Murray460a0492013-11-19 12:45:54 -080078 long mID;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070079 String mName;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080080 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070081 BaseObj mLoadedObj;
82
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070083 /**
Jason Sams65c80f82012-05-08 17:30:26 -070084 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080085 * Returns the name of a renderscript object the index entry
86 * describes
87 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080088 * @return name of a renderscript object the index entry
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080089 * describes
90 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080091 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070092 public String getName() {
93 return mName;
94 }
95
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070096 /**
Jason Sams65c80f82012-05-08 17:30:26 -070097 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080098 * Returns the type of a renderscript object the index entry
99 * describes
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800100 * @return type of a renderscript object the index entry
101 * describes
102 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800103 public EntryType getEntryType() {
104 return mEntryType;
105 }
106
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700107 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700108 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800109 * Used to load the object described by the index entry
110 * @return base renderscript object described by the entry
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800111 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700112 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700113 mRS.validate();
114 BaseObj obj = internalCreate(mRS, this);
115 return obj;
116 }
117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700119 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800120 * Used to load the mesh described by the index entry, object
121 * described by the index entry must be a renderscript mesh
122 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800123 * @return renderscript mesh object described by the entry
124 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800125 public Mesh getMesh() {
126 return (Mesh)getObject();
127 }
128
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700129 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
130 if(entry.mLoadedObj != null) {
131 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700132 }
133
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800134 // to be purged on cleanup
135 if(entry.mEntryType == EntryType.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700136 return null;
137 }
138
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700139 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700140 if(objectID == 0) {
141 return null;
142 }
143
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800144 switch (entry.mEntryType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700145 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700146 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700147 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700148 }
149
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700150 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700151 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700152 }
153
Tim Murray460a0492013-11-19 12:45:54 -0800154 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700155 mRS = rs;
156 mIndex = index;
157 mID = id;
158 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800159 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700160 mLoadedObj = null;
161 }
162 }
163
164 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700165 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700166
Tim Murray460a0492013-11-19 12:45:54 -0800167 FileA3D(long id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700168 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700169 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700170 }
171
172 private void initEntries() {
Jason Samse07694b2012-04-03 15:36:36 -0700173 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700174 if(numFileEntries <= 0) {
175 return;
176 }
177
178 mFileEntries = new IndexEntry[numFileEntries];
179 int[] ids = new int[numFileEntries];
180 String[] names = new String[numFileEntries];
181
Jason Samse07694b2012-04-03 15:36:36 -0700182 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700183
184 for(int i = 0; i < numFileEntries; i ++) {
Jason Samse07694b2012-04-03 15:36:36 -0700185 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700186 }
187 }
188
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700189 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700190 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800191 * Returns the number of objects stored inside the a3d file
192 *
193 * @return the number of objects stored inside the a3d file
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800194 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800195 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700196 if(mFileEntries == null) {
197 return 0;
198 }
199 return mFileEntries.length;
200 }
201
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700202 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700203 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800204 * Returns an index entry from the list of all objects inside
205 * FileA3D
206 *
207 * @param index number of the entry from the list to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800208 *
209 * @return entry in the a3d file described by the index
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800210 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700211 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800212 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700213 return null;
214 }
215 return mFileEntries[index];
216 }
217
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700219 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800220 * Creates a FileA3D object from an asset stored on disk
221 *
222 * @param rs Context to which the object will belong.
223 * @param mgr asset manager used to load asset
224 * @param path location of the file to load
225 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800226 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800227 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800228 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
229 rs.validate();
Tim Murray460a0492013-11-19 12:45:54 -0800230 long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800231
232 if(fileId == 0) {
233 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
234 }
235 FileA3D fa3d = new FileA3D(fileId, rs, null);
236 fa3d.initEntries();
237 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800238 }
239
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700240 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700241 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800242 * Creates a FileA3D object from a file stored on disk
243 *
244 * @param rs Context to which the object will belong.
245 * @param path location of the file to load
246 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800247 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800248 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800249 static public FileA3D createFromFile(RenderScript rs, String path) {
Tim Murray460a0492013-11-19 12:45:54 -0800250 long fileId = rs.nFileA3DCreateFromFile(path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800251
252 if(fileId == 0) {
253 throw new RSRuntimeException("Unable to create a3d file from " + path);
254 }
255 FileA3D fa3d = new FileA3D(fileId, rs, null);
256 fa3d.initEntries();
257 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800258 }
259
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700260 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700261 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800262 * Creates a FileA3D object from a file stored on disk
263 *
264 * @param rs Context to which the object will belong.
265 * @param path location of the file to load
266 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800267 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800268 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800269 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800270 return createFromFile(rs, path.getAbsolutePath());
271 }
272
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700273 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700274 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800275 * Creates a FileA3D object from an application resource
276 *
277 * @param rs Context to which the object will belong.
278 * @param res resource manager used for loading
279 * @param id resource to create FileA3D from
280 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800281 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800282 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800283 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700284
285 rs.validate();
286 InputStream is = null;
287 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800288 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700289 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800290 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700291 }
292
Tim Murray460a0492013-11-19 12:45:54 -0800293 long fileId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800294 if (is instanceof AssetManager.AssetInputStream) {
295 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
296 fileId = rs.nFileA3DCreateFromAssetStream(asset);
297 } else {
298 throw new RSRuntimeException("Unsupported asset stream");
299 }
300
301 if(fileId == 0) {
302 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
303 }
304 FileA3D fa3d = new FileA3D(fileId, rs, is);
305 fa3d.initEntries();
306 return fa3d;
307
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700308 }
309}