blob: 9d8f1624a0519cfbd6b557e0ae794c9271f37a29 [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
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000139 long 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;
Jason Samsbca8e6d2015-03-26 15:29:56 -0700148
149 default:
150 throw new RSRuntimeException("Unrecognized object type in file.");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700151 }
152
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700153 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700154 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700155 }
156
Tim Murray460a0492013-11-19 12:45:54 -0800157 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700158 mRS = rs;
159 mIndex = index;
160 mID = id;
161 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800162 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700163 mLoadedObj = null;
164 }
165 }
166
167 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700168 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700169
Tim Murray460a0492013-11-19 12:45:54 -0800170 FileA3D(long id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700171 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700172 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700173 }
174
175 private void initEntries() {
Jason Samse07694b2012-04-03 15:36:36 -0700176 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700177 if(numFileEntries <= 0) {
178 return;
179 }
180
181 mFileEntries = new IndexEntry[numFileEntries];
182 int[] ids = new int[numFileEntries];
183 String[] names = new String[numFileEntries];
184
Jason Samse07694b2012-04-03 15:36:36 -0700185 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700186
187 for(int i = 0; i < numFileEntries; i ++) {
Jason Samse07694b2012-04-03 15:36:36 -0700188 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700189 }
190 }
191
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700192 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700193 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800194 * Returns the number of objects stored inside the a3d file
195 *
196 * @return the number of objects stored inside the a3d file
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800197 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800198 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700199 if(mFileEntries == null) {
200 return 0;
201 }
202 return mFileEntries.length;
203 }
204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700205 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700206 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800207 * Returns an index entry from the list of all objects inside
208 * FileA3D
209 *
210 * @param index number of the entry from the list to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800211 *
212 * @return entry in the a3d file described by the index
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800213 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700214 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800215 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700216 return null;
217 }
218 return mFileEntries[index];
219 }
220
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700221 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700222 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800223 * Creates a FileA3D object from an asset stored on disk
224 *
225 * @param rs Context to which the object will belong.
226 * @param mgr asset manager used to load asset
227 * @param path location of the file to load
228 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800229 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800230 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800231 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
232 rs.validate();
Tim Murray460a0492013-11-19 12:45:54 -0800233 long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800234
235 if(fileId == 0) {
236 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
237 }
238 FileA3D fa3d = new FileA3D(fileId, rs, null);
239 fa3d.initEntries();
240 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800241 }
242
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700243 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700244 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800245 * Creates a FileA3D object from a file stored on disk
246 *
247 * @param rs Context to which the object will belong.
248 * @param path location of the file to load
249 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800250 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800251 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800252 static public FileA3D createFromFile(RenderScript rs, String path) {
Tim Murray460a0492013-11-19 12:45:54 -0800253 long fileId = rs.nFileA3DCreateFromFile(path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800254
255 if(fileId == 0) {
256 throw new RSRuntimeException("Unable to create a3d file from " + path);
257 }
258 FileA3D fa3d = new FileA3D(fileId, rs, null);
259 fa3d.initEntries();
260 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800261 }
262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700264 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800265 * Creates a FileA3D object from a file stored on disk
266 *
267 * @param rs Context to which the object will belong.
268 * @param path location of the file to load
269 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800270 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800271 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800272 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800273 return createFromFile(rs, path.getAbsolutePath());
274 }
275
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700276 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700277 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800278 * Creates a FileA3D object from an application resource
279 *
280 * @param rs Context to which the object will belong.
281 * @param res resource manager used for loading
282 * @param id resource to create FileA3D from
283 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800284 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800285 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800286 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700287
288 rs.validate();
289 InputStream is = null;
290 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800291 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700292 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800293 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700294 }
295
Tim Murray460a0492013-11-19 12:45:54 -0800296 long fileId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800297 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000298 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800299 fileId = rs.nFileA3DCreateFromAssetStream(asset);
300 } else {
301 throw new RSRuntimeException("Unsupported asset stream");
302 }
303
304 if(fileId == 0) {
305 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
306 }
307 FileA3D fa3d = new FileA3D(fileId, rs, is);
308 fa3d.initEntries();
309 return fa3d;
310
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700311 }
312}