blob: f0a9fa718a6f7c29a80f4f2d1108856c4ef58332 [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 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080039@Deprecated
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070040public class FileA3D extends BaseObj {
41
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070042 /**
Jason Sams65c80f82012-05-08 17:30:26 -070043 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080044 * Specifies what renderscript object type is contained within
45 * the FileA3D IndexEntry
46 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080047 public enum EntryType {
48
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070049 /**
Jason Sams65c80f82012-05-08 17:30:26 -070050 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080051 * Unknown or or invalid object, nothing will be loaded
52 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080053 UNKNOWN (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070054 /**
Jason Sams65c80f82012-05-08 17:30:26 -070055 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070056 * RenderScript Mesh object
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080057 **/
Mathew Inwood15324472018-08-06 11:18:49 +010058 @UnsupportedAppUsage
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080059 MESH (1);
60
61 int mID;
62 EntryType(int id) {
63 mID = id;
64 }
65
66 static EntryType toEntryType(int intID) {
67 return EntryType.values()[intID];
68 }
69 }
70
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070071 /**
Jason Sams65c80f82012-05-08 17:30:26 -070072 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070073 * IndexEntry contains information about one of the RenderScript
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080074 * objects inside the file's index. It could be used to query the
Robert Ly11518ac2011-02-09 13:57:06 -080075 * object's type and also name and load the object itself if
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080076 * necessary.
77 */
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070078 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070079 RenderScript mRS;
80 int mIndex;
Tim Murray460a0492013-11-19 12:45:54 -080081 long mID;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070082 String mName;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080083 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070084 BaseObj mLoadedObj;
85
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070086 /**
Jason Sams65c80f82012-05-08 17:30:26 -070087 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080088 * Returns the name of a renderscript object the index entry
89 * describes
90 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080091 * @return name of a renderscript object the index entry
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080092 * describes
93 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080094 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070095 public String getName() {
96 return mName;
97 }
98
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070099 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700100 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800101 * Returns the type of a renderscript object the index entry
102 * describes
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800103 * @return type of a renderscript object the index entry
104 * describes
105 */
Mathew Inwood15324472018-08-06 11:18:49 +0100106 @UnsupportedAppUsage
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 */
Mathew Inwood15324472018-08-06 11:18:49 +0100116 @UnsupportedAppUsage
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700117 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700118 mRS.validate();
119 BaseObj obj = internalCreate(mRS, this);
120 return obj;
121 }
122
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700123 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700124 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800125 * Used to load the mesh described by the index entry, object
126 * described by the index entry must be a renderscript mesh
127 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800128 * @return renderscript mesh object described by the entry
129 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800130 public Mesh getMesh() {
131 return (Mesh)getObject();
132 }
133
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700134 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
135 if(entry.mLoadedObj != null) {
136 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700137 }
138
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800139 // to be purged on cleanup
140 if(entry.mEntryType == EntryType.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700141 return null;
142 }
143
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000144 long objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700145 if(objectID == 0) {
146 return null;
147 }
148
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800149 switch (entry.mEntryType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700150 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700151 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700152 break;
Jason Samsbca8e6d2015-03-26 15:29:56 -0700153
154 default:
155 throw new RSRuntimeException("Unrecognized object type in file.");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700156 }
157
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700158 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700159 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700160 }
161
Tim Murray460a0492013-11-19 12:45:54 -0800162 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700163 mRS = rs;
164 mIndex = index;
165 mID = id;
166 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800167 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700168 mLoadedObj = null;
169 }
170 }
171
172 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700173 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700174
Tim Murray460a0492013-11-19 12:45:54 -0800175 FileA3D(long id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700176 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700177 mInputStream = stream;
Yang Nieb4dd082016-03-24 09:40:32 -0700178 guard.open("destroy");
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700179 }
180
181 private void initEntries() {
Jason Samse07694b2012-04-03 15:36:36 -0700182 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700183 if(numFileEntries <= 0) {
184 return;
185 }
186
187 mFileEntries = new IndexEntry[numFileEntries];
188 int[] ids = new int[numFileEntries];
189 String[] names = new String[numFileEntries];
190
Jason Samse07694b2012-04-03 15:36:36 -0700191 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700192
193 for(int i = 0; i < numFileEntries; i ++) {
Jason Samse07694b2012-04-03 15:36:36 -0700194 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700195 }
196 }
197
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700198 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700199 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800200 * Returns the number of objects stored inside the a3d file
201 *
202 * @return the number of objects stored inside the a3d file
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800203 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800204 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700205 if(mFileEntries == null) {
206 return 0;
207 }
208 return mFileEntries.length;
209 }
210
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700211 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700212 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800213 * Returns an index entry from the list of all objects inside
214 * FileA3D
215 *
216 * @param index number of the entry from the list to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800217 *
218 * @return entry in the a3d file described by the index
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800219 */
Mathew Inwood15324472018-08-06 11:18:49 +0100220 @UnsupportedAppUsage
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700221 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800222 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700223 return null;
224 }
225 return mFileEntries[index];
226 }
227
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700229 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800230 * Creates a FileA3D object from an asset stored on disk
231 *
232 * @param rs Context to which the object will belong.
233 * @param mgr asset manager used to load asset
234 * @param path location of the file to load
235 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800236 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800237 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800238 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
239 rs.validate();
Tim Murray460a0492013-11-19 12:45:54 -0800240 long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800241
242 if(fileId == 0) {
243 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
244 }
245 FileA3D fa3d = new FileA3D(fileId, rs, null);
246 fa3d.initEntries();
247 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800248 }
249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700250 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700251 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800252 * Creates a FileA3D object from a file stored on disk
253 *
254 * @param rs Context to which the object will belong.
255 * @param path location of the file to load
256 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800257 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800258 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800259 static public FileA3D createFromFile(RenderScript rs, String path) {
Tim Murray460a0492013-11-19 12:45:54 -0800260 long fileId = rs.nFileA3DCreateFromFile(path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800261
262 if(fileId == 0) {
263 throw new RSRuntimeException("Unable to create a3d file from " + path);
264 }
265 FileA3D fa3d = new FileA3D(fileId, rs, null);
266 fa3d.initEntries();
267 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800268 }
269
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700270 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700271 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800272 * Creates a FileA3D object from a file stored on disk
273 *
274 * @param rs Context to which the object will belong.
275 * @param path location of the file to load
276 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800277 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800278 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800279 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800280 return createFromFile(rs, path.getAbsolutePath());
281 }
282
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700283 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700284 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800285 * Creates a FileA3D object from an application resource
286 *
287 * @param rs Context to which the object will belong.
288 * @param res resource manager used for loading
289 * @param id resource to create FileA3D from
290 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800291 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800292 */
Mathew Inwood15324472018-08-06 11:18:49 +0100293 @UnsupportedAppUsage
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800294 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700295
296 rs.validate();
297 InputStream is = null;
298 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800299 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700300 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800301 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700302 }
303
Tim Murray460a0492013-11-19 12:45:54 -0800304 long fileId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800305 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000306 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800307 fileId = rs.nFileA3DCreateFromAssetStream(asset);
308 } else {
309 throw new RSRuntimeException("Unsupported asset stream");
310 }
311
312 if(fileId == 0) {
313 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
314 }
315 FileA3D fa3d = new FileA3D(fileId, rs, is);
316 fa3d.initEntries();
317 return fa3d;
318
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700319 }
320}