blob: 278d309c74f7c0145eea38e21d0dc81de36d813b [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;
Yang Ni6484b6b2016-03-24 09:40:32 -0700173 guard.open("destroy");
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();
Tim Murray460a0492013-11-19 12:45:54 -0800234 long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800235
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) {
Tim Murray460a0492013-11-19 12:45:54 -0800254 long fileId = rs.nFileA3DCreateFromFile(path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800255
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
Tim Murray460a0492013-11-19 12:45:54 -0800297 long fileId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800298 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000299 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800300 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}