| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 19 | import java.io.File; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
| 22 | |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 23 | import android.content.res.AssetManager; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 24 | import android.content.res.Resources; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.BitmapFactory; |
| 27 | import android.util.Log; |
| 28 | import android.util.TypedValue; |
| 29 | |
| 30 | /** |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 31 | * FileA3D allows users to load Renderscript objects from files |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 32 | * or resources stored on disk. It could be used to load items |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 33 | * such as 3D geometry data converted to a Renderscript format from |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 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 Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 39 | * |
| 40 | **/ |
| 41 | public class FileA3D extends BaseObj { |
| 42 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Specifies what renderscript object type is contained within |
| 45 | * the FileA3D IndexEntry |
| 46 | **/ |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 47 | public enum EntryType { |
| 48 | |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Unknown or or invalid object, nothing will be loaded |
| 51 | **/ |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 52 | UNKNOWN (0), |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Renderscript Mesh object |
| 55 | **/ |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 56 | 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 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 68 | /** |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 69 | * IndexEntry contains information about one of the Renderscript |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 70 | * objects inside the file's index. It could be used to query the |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 71 | * object's type and also name and load the object itself if |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 72 | * necessary. |
| 73 | */ |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 74 | public static class IndexEntry { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 75 | RenderScript mRS; |
| 76 | int mIndex; |
| 77 | int mID; |
| 78 | String mName; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 79 | EntryType mEntryType; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 80 | BaseObj mLoadedObj; |
| 81 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 82 | /** |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 83 | * Returns the name of a renderscript object the index entry |
| 84 | * describes |
| 85 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 86 | * @return name of a renderscript object the index entry |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 87 | * describes |
| 88 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 89 | */ |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 90 | public String getName() { |
| 91 | return mName; |
| 92 | } |
| 93 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 94 | /** |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 95 | * Returns the type of a renderscript object the index entry |
| 96 | * describes |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 97 | * @return type of a renderscript object the index entry |
| 98 | * describes |
| 99 | */ |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 100 | public EntryType getEntryType() { |
| 101 | return mEntryType; |
| 102 | } |
| 103 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 104 | /** |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 105 | * Used to load the object described by the index entry |
| 106 | * @return base renderscript object described by the entry |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 107 | */ |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 108 | public BaseObj getObject() { |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 109 | mRS.validate(); |
| 110 | BaseObj obj = internalCreate(mRS, this); |
| 111 | return obj; |
| 112 | } |
| 113 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 114 | /** |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 115 | * Used to load the mesh described by the index entry, object |
| 116 | * described by the index entry must be a renderscript mesh |
| 117 | * |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 118 | * @return renderscript mesh object described by the entry |
| 119 | */ |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 120 | public Mesh getMesh() { |
| 121 | return (Mesh)getObject(); |
| 122 | } |
| 123 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 124 | static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) { |
| 125 | if(entry.mLoadedObj != null) { |
| 126 | return entry.mLoadedObj; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 129 | // to be purged on cleanup |
| 130 | if(entry.mEntryType == EntryType.UNKNOWN) { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 131 | return null; |
| 132 | } |
| 133 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 134 | int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 135 | if(objectID == 0) { |
| 136 | return null; |
| 137 | } |
| 138 | |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 139 | switch (entry.mEntryType) { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 140 | case MESH: |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 141 | entry.mLoadedObj = new Mesh(objectID, rs); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 142 | break; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 145 | entry.mLoadedObj.updateFromNative(); |
| Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 146 | return entry.mLoadedObj; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 149 | IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 150 | mRS = rs; |
| 151 | mIndex = index; |
| 152 | mID = id; |
| 153 | mName = name; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 154 | mEntryType = type; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 155 | mLoadedObj = null; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | IndexEntry[] mFileEntries; |
| Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 160 | InputStream mInputStream; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 161 | |
| Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 162 | FileA3D(int id, RenderScript rs, InputStream stream) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 163 | super(id, rs); |
| Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 164 | mInputStream = stream; |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private void initEntries() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 168 | int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID()); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 169 | if(numFileEntries <= 0) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | mFileEntries = new IndexEntry[numFileEntries]; |
| 174 | int[] ids = new int[numFileEntries]; |
| 175 | String[] names = new String[numFileEntries]; |
| 176 | |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 177 | mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 178 | |
| 179 | for(int i = 0; i < numFileEntries; i ++) { |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 180 | mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], EntryType.toEntryType(ids[i])); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 184 | /** |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 185 | * Returns the number of objects stored inside the a3d file |
| 186 | * |
| 187 | * @return the number of objects stored inside the a3d file |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 188 | */ |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 189 | public int getIndexEntryCount() { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 190 | if(mFileEntries == null) { |
| 191 | return 0; |
| 192 | } |
| 193 | return mFileEntries.length; |
| 194 | } |
| 195 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Returns an index entry from the list of all objects inside |
| 198 | * FileA3D |
| 199 | * |
| 200 | * @param index number of the entry from the list to return |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 201 | * |
| 202 | * @return entry in the a3d file described by the index |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 203 | */ |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 204 | public IndexEntry getIndexEntry(int index) { |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 205 | if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 206 | return null; |
| 207 | } |
| 208 | return mFileEntries[index]; |
| 209 | } |
| 210 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 211 | /** |
| 212 | * Creates a FileA3D object from an asset stored on disk |
| 213 | * |
| 214 | * @param rs Context to which the object will belong. |
| 215 | * @param mgr asset manager used to load asset |
| 216 | * @param path location of the file to load |
| 217 | * |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 218 | * @return a3d file containing renderscript objects |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 219 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 220 | static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) { |
| 221 | rs.validate(); |
| 222 | int fileId = rs.nFileA3DCreateFromAsset(mgr, path); |
| 223 | |
| 224 | if(fileId == 0) { |
| 225 | throw new RSRuntimeException("Unable to create a3d file from asset " + path); |
| 226 | } |
| 227 | FileA3D fa3d = new FileA3D(fileId, rs, null); |
| 228 | fa3d.initEntries(); |
| 229 | return fa3d; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 232 | /** |
| 233 | * Creates a FileA3D object from a file stored on disk |
| 234 | * |
| 235 | * @param rs Context to which the object will belong. |
| 236 | * @param path location of the file to load |
| 237 | * |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 238 | * @return a3d file containing renderscript objects |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 239 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 240 | static public FileA3D createFromFile(RenderScript rs, String path) { |
| 241 | int fileId = rs.nFileA3DCreateFromFile(path); |
| 242 | |
| 243 | if(fileId == 0) { |
| 244 | throw new RSRuntimeException("Unable to create a3d file from " + path); |
| 245 | } |
| 246 | FileA3D fa3d = new FileA3D(fileId, rs, null); |
| 247 | fa3d.initEntries(); |
| 248 | return fa3d; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 251 | /** |
| 252 | * 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 Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 257 | * @return a3d file containing renderscript objects |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 258 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 259 | static public FileA3D createFromFile(RenderScript rs, File path) { |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 260 | return createFromFile(rs, path.getAbsolutePath()); |
| 261 | } |
| 262 | |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 263 | /** |
| 264 | * Creates a FileA3D object from an application resource |
| 265 | * |
| 266 | * @param rs Context to which the object will belong. |
| 267 | * @param res resource manager used for loading |
| 268 | * @param id resource to create FileA3D from |
| 269 | * |
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 270 | * @return a3d file containing renderscript objects |
| Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 271 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 272 | static public FileA3D createFromResource(RenderScript rs, Resources res, int id) { |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 273 | |
| 274 | rs.validate(); |
| 275 | InputStream is = null; |
| 276 | try { |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 277 | is = res.openRawResource(id); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 278 | } catch (Exception e) { |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 279 | throw new RSRuntimeException("Unable to open resource " + id); |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 282 | int fileId = 0; |
| 283 | if (is instanceof AssetManager.AssetInputStream) { |
| 284 | int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); |
| 285 | fileId = rs.nFileA3DCreateFromAssetStream(asset); |
| 286 | } else { |
| 287 | throw new RSRuntimeException("Unsupported asset stream"); |
| 288 | } |
| 289 | |
| 290 | if(fileId == 0) { |
| 291 | throw new RSRuntimeException("Unable to create a3d file from resource " + id); |
| 292 | } |
| 293 | FileA3D fa3d = new FileA3D(fileId, rs, is); |
| 294 | fa3d.initEntries(); |
| 295 | return fa3d; |
| 296 | |
| Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 297 | } |
| 298 | } |