blob: 7cc2825ae56539dbc9b81f1827a89d63e95ccfcd [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
Artur Satayev2ebb31c2020-01-08 12:24:36 +000019import android.compat.annotation.UnsupportedAppUsage;
Austin Wanga63a2c02019-12-19 06:38:19 +000020import android.content.res.AssetManager;
21import android.content.res.Resources;
22
Artur Satayev2ebb31c2020-01-08 12:24:36 +000023import java.io.File;
24import java.io.InputStream;
25
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070026/**
Tim Murraya9084222013-04-05 22:06:43 +000027 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070028 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070029 * FileA3D allows users to load RenderScript objects from files
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080030 * or resources stored on disk. It could be used to load items
Tim Murrayc11e25c2013-04-09 11:01:01 -070031 * such as 3D geometry data converted to a RenderScript format from
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080032 * content creation tools. Currently only meshes are supported
33 * in FileA3D.
34 *
35 * When successfully loaded, FileA3D will contain a list of
36 * index entries for all the objects stored inside it.
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070037 *
38 **/
39public class FileA3D extends BaseObj {
40
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070041 /**
Jason Sams65c80f82012-05-08 17:30:26 -070042 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080043 * Specifies what renderscript object type is contained within
44 * the FileA3D IndexEntry
45 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080046 public enum EntryType {
47
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070048 /**
Jason Sams65c80f82012-05-08 17:30:26 -070049 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080050 * Unknown or or invalid object, nothing will be loaded
51 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080052 UNKNOWN (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070053 /**
Jason Sams65c80f82012-05-08 17:30:26 -070054 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070055 * RenderScript Mesh object
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080056 **/
Mathew Inwood15324472018-08-06 11:18:49 +010057 @UnsupportedAppUsage
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080058 MESH (1);
59
60 int mID;
61 EntryType(int id) {
62 mID = id;
63 }
64
65 static EntryType toEntryType(int intID) {
66 return EntryType.values()[intID];
67 }
68 }
69
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070070 /**
Jason Sams65c80f82012-05-08 17:30:26 -070071 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070072 * IndexEntry contains information about one of the RenderScript
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080073 * objects inside the file's index. It could be used to query the
Robert Ly11518ac2011-02-09 13:57:06 -080074 * object's type and also name and load the object itself if
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080075 * necessary.
76 */
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070077 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070078 RenderScript mRS;
79 int mIndex;
Tim Murray460a0492013-11-19 12:45:54 -080080 long mID;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070081 String mName;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080082 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070083 BaseObj mLoadedObj;
84
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070085 /**
Jason Sams65c80f82012-05-08 17:30:26 -070086 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080087 * Returns the name of a renderscript object the index entry
88 * describes
89 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080090 * @return name of a renderscript object the index entry
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080091 * describes
92 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080093 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070094 public String getName() {
95 return mName;
96 }
97
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Jason Sams65c80f82012-05-08 17:30:26 -070099 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800100 * Returns the type of a renderscript object the index entry
101 * describes
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800102 * @return type of a renderscript object the index entry
103 * describes
104 */
Mathew Inwood15324472018-08-06 11:18:49 +0100105 @UnsupportedAppUsage
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800106 public EntryType getEntryType() {
107 return mEntryType;
108 }
109
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700110 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700111 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800112 * Used to load the object described by the index entry
113 * @return base renderscript object described by the entry
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800114 */
Mathew Inwood15324472018-08-06 11:18:49 +0100115 @UnsupportedAppUsage
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
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000143 long 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;
Jason Samsbca8e6d2015-03-26 15:29:56 -0700152
153 default:
154 throw new RSRuntimeException("Unrecognized object type in file.");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700155 }
156
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700157 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700158 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700159 }
160
Tim Murray460a0492013-11-19 12:45:54 -0800161 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700162 mRS = rs;
163 mIndex = index;
164 mID = id;
165 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800166 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700167 mLoadedObj = null;
168 }
169 }
170
171 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700172 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700173
Tim Murray460a0492013-11-19 12:45:54 -0800174 FileA3D(long id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700175 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700176 mInputStream = stream;
Yang Nieb4dd082016-03-24 09:40:32 -0700177 guard.open("destroy");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700178 }
179
180 private void initEntries() {
Jason Samse07694b2012-04-03 15:36:36 -0700181 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700182 if(numFileEntries <= 0) {
183 return;
184 }
185
186 mFileEntries = new IndexEntry[numFileEntries];
187 int[] ids = new int[numFileEntries];
188 String[] names = new String[numFileEntries];
189
Jason Samse07694b2012-04-03 15:36:36 -0700190 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700191
192 for(int i = 0; i < numFileEntries; i ++) {
Jason Samse07694b2012-04-03 15:36:36 -0700193 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700194 }
195 }
196
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700197 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700198 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800199 * Returns the number of objects stored inside the a3d file
200 *
201 * @return the number of objects stored inside the a3d file
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800202 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800203 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700204 if(mFileEntries == null) {
205 return 0;
206 }
207 return mFileEntries.length;
208 }
209
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700210 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700211 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800212 * Returns an index entry from the list of all objects inside
213 * FileA3D
214 *
215 * @param index number of the entry from the list to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800216 *
217 * @return entry in the a3d file described by the index
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800218 */
Mathew Inwood15324472018-08-06 11:18:49 +0100219 @UnsupportedAppUsage
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700220 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800221 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700222 return null;
223 }
224 return mFileEntries[index];
225 }
226
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700227 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700228 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800229 * Creates a FileA3D object from an asset stored on disk
230 *
231 * @param rs Context to which the object will belong.
232 * @param mgr asset manager used to load asset
233 * @param path location of the file to load
234 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800235 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800236 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800237 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
238 rs.validate();
Tim Murray460a0492013-11-19 12:45:54 -0800239 long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800240
241 if(fileId == 0) {
242 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
243 }
244 FileA3D fa3d = new FileA3D(fileId, rs, null);
245 fa3d.initEntries();
246 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800247 }
248
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700249 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700250 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800251 * Creates a FileA3D object from a file stored on disk
252 *
253 * @param rs Context to which the object will belong.
254 * @param path location of the file to load
255 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800256 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800257 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800258 static public FileA3D createFromFile(RenderScript rs, String path) {
Tim Murray460a0492013-11-19 12:45:54 -0800259 long fileId = rs.nFileA3DCreateFromFile(path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800260
261 if(fileId == 0) {
262 throw new RSRuntimeException("Unable to create a3d file from " + path);
263 }
264 FileA3D fa3d = new FileA3D(fileId, rs, null);
265 fa3d.initEntries();
266 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800267 }
268
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700269 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700270 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800271 * Creates a FileA3D object from a file stored on disk
272 *
273 * @param rs Context to which the object will belong.
274 * @param path location of the file to load
275 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800276 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800277 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800278 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800279 return createFromFile(rs, path.getAbsolutePath());
280 }
281
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700282 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700283 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800284 * Creates a FileA3D object from an application resource
285 *
286 * @param rs Context to which the object will belong.
287 * @param res resource manager used for loading
288 * @param id resource to create FileA3D from
289 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800290 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800291 */
Mathew Inwood15324472018-08-06 11:18:49 +0100292 @UnsupportedAppUsage
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800293 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700294
295 rs.validate();
296 InputStream is = null;
297 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800298 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700299 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800300 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700301 }
302
Tim Murray460a0492013-11-19 12:45:54 -0800303 long fileId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800304 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000305 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800306 fileId = rs.nFileA3DCreateFromAssetStream(asset);
307 } else {
308 throw new RSRuntimeException("Unsupported asset stream");
309 }
310
311 if(fileId == 0) {
312 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
313 }
314 FileA3D fa3d = new FileA3D(fileId, rs, is);
315 fa3d.initEntries();
316 return fa3d;
317
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700318 }
319}