| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.renderscript; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.res.AssetManager; |
| 21 |
import android.content.res.Resources; |
| 22 |
|
| 23 |
import java.io.File; |
| 24 |
import java.io.InputStream; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
@Deprecated |
| 40 |
public class FileA3D extends BaseObj { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public enum EntryType { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
UNKNOWN (0), |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@UnsupportedAppUsage |
| 59 |
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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static class IndexEntry { |
| 79 |
RenderScript mRS; |
| 80 |
int mIndex; |
| 81 |
long mID; |
| 82 |
String mName; |
| 83 |
EntryType mEntryType; |
| 84 |
BaseObj mLoadedObj; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public String getName() { |
| 96 |
return mName; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@UnsupportedAppUsage |
| 107 |
public EntryType getEntryType() { |
| 108 |
return mEntryType; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@UnsupportedAppUsage |
| 117 |
public BaseObj getObject() { |
| 118 |
mRS.validate(); |
| 119 |
BaseObj obj = internalCreate(mRS, this); |
| 120 |
return obj; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public Mesh getMesh() { |
| 131 |
return (Mesh)getObject(); |
| 132 |
} |
| 133 |
|
| 134 |
static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) { |
| 135 |
if(entry.mLoadedObj != null) { |
| 136 |
return entry.mLoadedObj; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
if(entry.mEntryType == EntryType.UNKNOWN) { |
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
long objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex); |
| 145 |
if(objectID == 0) { |
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
switch (entry.mEntryType) { |
| 150 |
case MESH: |
| 151 |
entry.mLoadedObj = new Mesh(objectID, rs); |
| 152 |
break; |
| 153 |
|
| 154 |
default: |
| 155 |
throw new RSRuntimeException("Unrecognized object type in file."); |
| 156 |
} |
| 157 |
|
| 158 |
entry.mLoadedObj.updateFromNative(); |
| 159 |
return entry.mLoadedObj; |
| 160 |
} |
| 161 |
|
| 162 |
IndexEntry(RenderScript rs, int index, long id, String name, EntryType type) { |
| 163 |
mRS = rs; |
| 164 |
mIndex = index; |
| 165 |
mID = id; |
| 166 |
mName = name; |
| 167 |
mEntryType = type; |
| 168 |
mLoadedObj = null; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
IndexEntry[] mFileEntries; |
| 173 |
InputStream mInputStream; |
| 174 |
|
| 175 |
FileA3D(long id, RenderScript rs, InputStream stream) { |
| 176 |
super(id, rs); |
| 177 |
mInputStream = stream; |
| 178 |
guard.open("destroy"); |
| 179 |
} |
| 180 |
|
| 181 |
private void initEntries() { |
| 182 |
int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS)); |
| 183 |
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 |
|
| 191 |
mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names); |
| 192 |
|
| 193 |
for(int i = 0; i < numFileEntries; i ++) { |
| 194 |
mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i])); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public int getIndexEntryCount() { |
| 205 |
if(mFileEntries == null) { |
| 206 |
return 0; |
| 207 |
} |
| 208 |
return mFileEntries.length; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
@UnsupportedAppUsage |
| 221 |
public IndexEntry getIndexEntry(int index) { |
| 222 |
if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) { |
| 223 |
return null; |
| 224 |
} |
| 225 |
return mFileEntries[index]; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) { |
| 239 |
rs.validate(); |
| 240 |
long fileId = rs.nFileA3DCreateFromAsset(mgr, path); |
| 241 |
|
| 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; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static public FileA3D createFromFile(RenderScript rs, String path) { |
| 260 |
long fileId = rs.nFileA3DCreateFromFile(path); |
| 261 |
|
| 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; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
static public FileA3D createFromFile(RenderScript rs, File path) { |
| 280 |
return createFromFile(rs, path.getAbsolutePath()); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
@UnsupportedAppUsage |
| 294 |
static public FileA3D createFromResource(RenderScript rs, Resources res, int id) { |
| 295 |
|
| 296 |
rs.validate(); |
| 297 |
InputStream is = null; |
| 298 |
try { |
| 299 |
is = res.openRawResource(id); |
| 300 |
} catch (Exception e) { |
| 301 |
throw new RSRuntimeException("Unable to open resource " + id); |
| 302 |
} |
| 303 |
|
| 304 |
long fileId = 0; |
| 305 |
if (is instanceof AssetManager.AssetInputStream) { |
| 306 |
long asset = ((AssetManager.AssetInputStream) is).getNativeAsset(); |
| 307 |
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 |
|
| 319 |
} |
| 320 |
} |