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 | /** |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 31 | * 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 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 | |
| 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 Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 62 | /** |
| 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 Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 68 | public static class IndexEntry { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 69 | RenderScript mRS; |
| 70 | int mIndex; |
| 71 | int mID; |
| 72 | String mName; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 73 | EntryType mEntryType; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 74 | BaseObj mLoadedObj; |
| 75 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 76 | /** |
| 77 | * @return name of a renderscript object the index entry |
| 78 | * describes |
| 79 | */ |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 80 | public String getName() { |
| 81 | return mName; |
| 82 | } |
| 83 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 84 | /** |
| 85 | * @return type of a renderscript object the index entry |
| 86 | * describes |
| 87 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 88 | public EntryType getEntryType() { |
| 89 | return mEntryType; |
| 90 | } |
| 91 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 92 | /** |
| 93 | * @return renderscript object described by the entry |
| 94 | */ |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 95 | public BaseObj getObject() { |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 96 | mRS.validate(); |
| 97 | BaseObj obj = internalCreate(mRS, this); |
| 98 | return obj; |
| 99 | } |
| 100 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 101 | /** |
| 102 | * @return renderscript mesh object described by the entry |
| 103 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 104 | public Mesh getMesh() { |
| 105 | return (Mesh)getObject(); |
| 106 | } |
| 107 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 108 | static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) { |
| 109 | if(entry.mLoadedObj != null) { |
| 110 | return entry.mLoadedObj; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 113 | // to be purged on cleanup |
| 114 | if(entry.mEntryType == EntryType.UNKNOWN) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 115 | return null; |
| 116 | } |
| 117 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 118 | int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 119 | if(objectID == 0) { |
| 120 | return null; |
| 121 | } |
| 122 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 123 | switch (entry.mEntryType) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 124 | case MESH: |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 125 | entry.mLoadedObj = new Mesh(objectID, rs); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 126 | break; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 129 | entry.mLoadedObj.updateFromNative(); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 130 | return entry.mLoadedObj; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 133 | IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 134 | mRS = rs; |
| 135 | mIndex = index; |
| 136 | mID = id; |
| 137 | mName = name; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 138 | mEntryType = type; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 139 | mLoadedObj = null; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | IndexEntry[] mFileEntries; |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 144 | InputStream mInputStream; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 145 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 146 | FileA3D(int id, RenderScript rs, InputStream stream) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 147 | super(id, rs); |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 148 | mInputStream = stream; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | private void initEntries() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 152 | int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID()); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 153 | 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 Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 161 | mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 162 | |
| 163 | for(int i = 0; i < numFileEntries; i ++) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 164 | 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] | 165 | } |
| 166 | } |
| 167 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 168 | /** |
| 169 | * @return the numberof objects stored inside a FileA3D |
| 170 | */ |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 171 | public int getIndexEntryCount() { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 172 | if(mFileEntries == null) { |
| 173 | return 0; |
| 174 | } |
| 175 | return mFileEntries.length; |
| 176 | } |
| 177 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 178 | /** |
| 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 Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 184 | public IndexEntry getIndexEntry(int index) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 185 | if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 186 | return null; |
| 187 | } |
| 188 | return mFileEntries[index]; |
| 189 | } |
| 190 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 191 | /** |
| 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 Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 199 | 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 Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 211 | /** |
| 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 Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 218 | 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 Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 229 | /** |
| 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 Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 236 | static public FileA3D createFromFile(RenderScript rs, File path) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 237 | return createFromFile(rs, path.getAbsolutePath()); |
| 238 | } |
| 239 | |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame^] | 240 | /** |
| 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 Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 248 | static public FileA3D createFromResource(RenderScript rs, Resources res, int id) { |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 249 | |
| 250 | rs.validate(); |
| 251 | InputStream is = null; |
| 252 | try { |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 253 | is = res.openRawResource(id); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 254 | } catch (Exception e) { |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 255 | throw new RSRuntimeException("Unable to open resource " + id); |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 258 | 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 Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 273 | } |
| 274 | } |