blob: fe3971a5e116b5257c68e9a42d3ee182018c6843 [file] [log] [blame]
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
30/**
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080031 * FileA3D allows users to load renderscript objects from files
32 * or resources stored on disk. It could be used to load items
33 * such as 3d geometry data converted a renderscript format from
34 * content creation tools. Currently only meshes are supported
35 * in FileA3D.
36 *
37 * When successfully loaded, FileA3D will contain a list of
38 * index entries for all the objects stored inside it.
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070039 *
40 **/
41public class FileA3D extends BaseObj {
42
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080043 /**
44 * 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
49 UNKNOWN (0),
50 MESH (1);
51
52 int mID;
53 EntryType(int id) {
54 mID = id;
55 }
56
57 static EntryType toEntryType(int intID) {
58 return EntryType.values()[intID];
59 }
60 }
61
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080062 /**
63 * IndexEntry contains information about one of the renderscript
64 * objects inside the file's index. It could be used to query the
65 * object's type and name and load the object itself if
66 * necessary.
67 */
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070068 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070069 RenderScript mRS;
70 int mIndex;
71 int mID;
72 String mName;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080073 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070074 BaseObj mLoadedObj;
75
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080076 /**
77 * @return name of a renderscript object the index entry
78 * describes
79 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070080 public String getName() {
81 return mName;
82 }
83
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080084 /**
85 * @return type of a renderscript object the index entry
86 * describes
87 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080088 public EntryType getEntryType() {
89 return mEntryType;
90 }
91
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080092 /**
93 * @return renderscript object described by the entry
94 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070095 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070096 mRS.validate();
97 BaseObj obj = internalCreate(mRS, this);
98 return obj;
99 }
100
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800101 /**
102 * @return renderscript mesh object described by the entry
103 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800104 public Mesh getMesh() {
105 return (Mesh)getObject();
106 }
107
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700108 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
109 if(entry.mLoadedObj != null) {
110 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700111 }
112
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800113 // to be purged on cleanup
114 if(entry.mEntryType == EntryType.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700115 return null;
116 }
117
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700118 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700119 if(objectID == 0) {
120 return null;
121 }
122
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800123 switch (entry.mEntryType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700124 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700125 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700126 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700127 }
128
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700129 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700130 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700131 }
132
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800133 IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700134 mRS = rs;
135 mIndex = index;
136 mID = id;
137 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800138 mEntryType = type;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700139 mLoadedObj = null;
140 }
141 }
142
143 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700144 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700145
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700146 FileA3D(int id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700147 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700148 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700149 }
150
151 private void initEntries() {
Jason Sams06d69de2010-11-09 17:11:40 -0800152 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700153 if(numFileEntries <= 0) {
154 return;
155 }
156
157 mFileEntries = new IndexEntry[numFileEntries];
158 int[] ids = new int[numFileEntries];
159 String[] names = new String[numFileEntries];
160
Jason Sams06d69de2010-11-09 17:11:40 -0800161 mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700162
163 for(int i = 0; i < numFileEntries; i ++) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800164 mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700165 }
166 }
167
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800168 /**
169 * @return the numberof objects stored inside a FileA3D
170 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800171 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700172 if(mFileEntries == null) {
173 return 0;
174 }
175 return mFileEntries.length;
176 }
177
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800178 /**
179 * Returns an index entry from the list of all objects inside
180 * FileA3D
181 *
182 * @param index number of the entry from the list to return
183 */
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700184 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800185 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700186 return null;
187 }
188 return mFileEntries[index];
189 }
190
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800191 /**
192 * Creates a FileA3D object from an asset stored on disk
193 *
194 * @param rs Context to which the object will belong.
195 * @param mgr asset manager used to load asset
196 * @param path location of the file to load
197 *
198 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800199 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
200 rs.validate();
201 int fileId = rs.nFileA3DCreateFromAsset(mgr, path);
202
203 if(fileId == 0) {
204 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
205 }
206 FileA3D fa3d = new FileA3D(fileId, rs, null);
207 fa3d.initEntries();
208 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800209 }
210
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800211 /**
212 * Creates a FileA3D object from a file stored on disk
213 *
214 * @param rs Context to which the object will belong.
215 * @param path location of the file to load
216 *
217 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800218 static public FileA3D createFromFile(RenderScript rs, String path) {
219 int fileId = rs.nFileA3DCreateFromFile(path);
220
221 if(fileId == 0) {
222 throw new RSRuntimeException("Unable to create a3d file from " + path);
223 }
224 FileA3D fa3d = new FileA3D(fileId, rs, null);
225 fa3d.initEntries();
226 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800227 }
228
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800229 /**
230 * Creates a FileA3D object from a file stored on disk
231 *
232 * @param rs Context to which the object will belong.
233 * @param path location of the file to load
234 *
235 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800236 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800237 return createFromFile(rs, path.getAbsolutePath());
238 }
239
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800240 /**
241 * Creates a FileA3D object from an application resource
242 *
243 * @param rs Context to which the object will belong.
244 * @param res resource manager used for loading
245 * @param id resource to create FileA3D from
246 *
247 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800248 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700249
250 rs.validate();
251 InputStream is = null;
252 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800253 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700254 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800255 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700256 }
257
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800258 int fileId = 0;
259 if (is instanceof AssetManager.AssetInputStream) {
260 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
261 fileId = rs.nFileA3DCreateFromAssetStream(asset);
262 } else {
263 throw new RSRuntimeException("Unsupported asset stream");
264 }
265
266 if(fileId == 0) {
267 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
268 }
269 FileA3D fa3d = new FileA3D(fileId, rs, is);
270 fa3d.initEntries();
271 return fa3d;
272
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700273 }
274}