blob: 04bc7c66689028781000604bd5a885bb90576629 [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.IOException;
21import java.io.InputStream;
22
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070023import android.content.res.AssetManager;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080024import android.content.res.Resources;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070025import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.util.Log;
28import android.util.TypedValue;
29
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Tim Murraya9084222013-04-05 22:06:43 +000031 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070032 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * FileA3D allows users to load RenderScript objects from files
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080034 * or resources stored on disk. It could be used to load items
Tim Murrayc11e25c2013-04-09 11:01:01 -070035 * such as 3D geometry data converted to a RenderScript format from
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080036 * content creation tools. Currently only meshes are supported
37 * in FileA3D.
38 *
39 * When successfully loaded, FileA3D will contain a list of
40 * index entries for all the objects stored inside it.
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070041 *
42 **/
43public class FileA3D extends BaseObj {
44
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070045 /**
Jason Sams65c80f82012-05-08 17:30:26 -070046 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080047 * Specifies what renderscript object type is contained within
48 * the FileA3D IndexEntry
49 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080050 public enum EntryType {
51
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070052 /**
Jason Sams65c80f82012-05-08 17:30:26 -070053 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080054 * Unknown or or invalid object, nothing will be loaded
55 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080056 UNKNOWN (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070057 /**
Jason Sams65c80f82012-05-08 17:30:26 -070058 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070059 * RenderScript Mesh object
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080060 **/
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080061 MESH (1);
62
63 int mID;
64 EntryType(int id) {
65 mID = id;
66 }
67
68 static EntryType toEntryType(int intID) {
69 return EntryType.values()[intID];
70 }
71 }
72
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070073 /**
Jason Sams65c80f82012-05-08 17:30:26 -070074 * @deprecated in API 16
Tim Murrayc11e25c2013-04-09 11:01:01 -070075 * IndexEntry contains information about one of the RenderScript
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080076 * objects inside the file's index. It could be used to query the
Robert Ly11518ac2011-02-09 13:57:06 -080077 * object's type and also name and load the object itself if
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080078 * necessary.
79 */
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070080 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070081 RenderScript mRS;
82 int mIndex;
Tim Murray7a629fa2013-11-19 12:45:54 -080083 long mID;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070084 String mName;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080085 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070086 BaseObj mLoadedObj;
87
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070088 /**
Jason Sams65c80f82012-05-08 17:30:26 -070089 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080090 * Returns the name of a renderscript object the index entry
91 * describes
92 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080093 * @return name of a renderscript object the index entry
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080094 * describes
95 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080096 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070097 public String getName() {
98 return mName;
99 }
100
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700101 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700102 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800103 * Returns the type of a renderscript object the index entry
104 * describes
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800105 * @return type of a renderscript object the index entry
106 * describes
107 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800108 public EntryType getEntryType() {
109 return mEntryType;
110 }
111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700113 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800114 * Used to load the object described by the index entry
115 * @return base renderscript object described by the entry
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800116 */
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;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700153 }
154
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700155 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700156 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700157 }
158
Tim Murray7a629fa2013-11-19 12:45:54 -0800159 IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700160 mRS = rs;
161 mIndex = index;
162 mID = id;
163 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800164 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700165 mLoadedObj = null;
166 }
167 }
168
169 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700170 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700171
Tim Murray7a629fa2013-11-19 12:45:54 -0800172 FileA3D(long id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700173 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700174 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700175 }
176
177 private void initEntries() {
Jason Samse07694b2012-04-03 15:36:36 -0700178 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700179 if(numFileEntries <= 0) {
180 return;
181 }
182
183 mFileEntries = new IndexEntry[numFileEntries];
184 int[] ids = new int[numFileEntries];
185 String[] names = new String[numFileEntries];
186
Jason Samse07694b2012-04-03 15:36:36 -0700187 mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700188
189 for(int i = 0; i < numFileEntries; i ++) {
Jason Samse07694b2012-04-03 15:36:36 -0700190 mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700191 }
192 }
193
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700194 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700195 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800196 * Returns the number of objects stored inside the a3d file
197 *
198 * @return the number of objects stored inside the a3d file
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800199 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800200 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700201 if(mFileEntries == null) {
202 return 0;
203 }
204 return mFileEntries.length;
205 }
206
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700207 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700208 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800209 * Returns an index entry from the list of all objects inside
210 * FileA3D
211 *
212 * @param index number of the entry from the list to return
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800213 *
214 * @return entry in the a3d file described by the index
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800215 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700216 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800217 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700218 return null;
219 }
220 return mFileEntries[index];
221 }
222
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700223 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700224 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800225 * Creates a FileA3D object from an asset stored on disk
226 *
227 * @param rs Context to which the object will belong.
228 * @param mgr asset manager used to load asset
229 * @param path location of the file to load
230 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800231 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800232 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800233 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
234 rs.validate();
Tim Murray7a629fa2013-11-19 12:45:54 -0800235 long fileId = rs.nFileA3DCreateFromAsset(mgr, path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800236
237 if(fileId == 0) {
238 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
239 }
240 FileA3D fa3d = new FileA3D(fileId, rs, null);
241 fa3d.initEntries();
242 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800243 }
244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700245 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700246 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800247 * Creates a FileA3D object from a file stored on disk
248 *
249 * @param rs Context to which the object will belong.
250 * @param path location of the file to load
251 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800252 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800253 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800254 static public FileA3D createFromFile(RenderScript rs, String path) {
Tim Murray7a629fa2013-11-19 12:45:54 -0800255 long fileId = rs.nFileA3DCreateFromFile(path);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800256
257 if(fileId == 0) {
258 throw new RSRuntimeException("Unable to create a3d file from " + path);
259 }
260 FileA3D fa3d = new FileA3D(fileId, rs, null);
261 fa3d.initEntries();
262 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800263 }
264
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700265 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700266 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800267 * Creates a FileA3D object from a file stored on disk
268 *
269 * @param rs Context to which the object will belong.
270 * @param path location of the file to load
271 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800272 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800273 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800274 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800275 return createFromFile(rs, path.getAbsolutePath());
276 }
277
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700278 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700279 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800280 * Creates a FileA3D object from an application resource
281 *
282 * @param rs Context to which the object will belong.
283 * @param res resource manager used for loading
284 * @param id resource to create FileA3D from
285 *
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -0800286 * @return a3d file containing renderscript objects
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800287 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800288 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700289
290 rs.validate();
291 InputStream is = null;
292 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800293 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700294 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800295 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700296 }
297
Tim Murray7a629fa2013-11-19 12:45:54 -0800298 long fileId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800299 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000300 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800301 fileId = rs.nFileA3DCreateFromAssetStream(asset);
302 } else {
303 throw new RSRuntimeException("Unsupported asset stream");
304 }
305
306 if(fileId == 0) {
307 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
308 }
309 FileA3D fa3d = new FileA3D(fileId, rs, is);
310 fa3d.initEntries();
311 return fa3d;
312
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700313 }
314}