2010-06-04 10:06:50 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package android.renderscript;
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
import java.io.File;
|
2010-06-04 10:06:50 -07:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import android.content.res.AssetManager;
|
2010-12-17 11:41:08 -08:00
|
|
|
import android.content.res.Resources;
|
2010-06-04 10:06:50 -07:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public class FileA3D extends BaseObj {
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
// This will go away in the clean up pass,
|
|
|
|
// trying to avoid multiproject submits
|
2010-06-04 10:06:50 -07:00
|
|
|
public enum ClassID {
|
|
|
|
|
|
|
|
UNKNOWN,
|
2010-12-17 11:41:08 -08:00
|
|
|
MESH;
|
2010-06-04 10:06:50 -07:00
|
|
|
|
|
|
|
public static ClassID toClassID(int intID) {
|
|
|
|
return ClassID.values()[intID];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
public enum EntryType {
|
|
|
|
|
|
|
|
UNKNOWN (0),
|
|
|
|
MESH (1);
|
|
|
|
|
|
|
|
int mID;
|
|
|
|
EntryType(int id) {
|
|
|
|
mID = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EntryType toEntryType(int intID) {
|
|
|
|
return EntryType.values()[intID];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-04 10:06:50 -07:00
|
|
|
// Read only class with index entries
|
2010-07-15 11:33:03 -07:00
|
|
|
public static class IndexEntry {
|
2010-06-04 10:06:50 -07:00
|
|
|
RenderScript mRS;
|
|
|
|
int mIndex;
|
|
|
|
int mID;
|
|
|
|
String mName;
|
|
|
|
ClassID mClassID;
|
2010-12-17 11:41:08 -08:00
|
|
|
EntryType mEntryType;
|
2010-06-04 10:06:50 -07:00
|
|
|
BaseObj mLoadedObj;
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ClassID getClassID() {
|
|
|
|
return mClassID;
|
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
public EntryType getEntryType() {
|
|
|
|
return mEntryType;
|
|
|
|
}
|
|
|
|
|
2010-06-04 10:06:50 -07:00
|
|
|
public BaseObj getObject() {
|
2010-07-15 11:33:03 -07:00
|
|
|
mRS.validate();
|
|
|
|
BaseObj obj = internalCreate(mRS, this);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
public Mesh getMesh() {
|
|
|
|
return (Mesh)getObject();
|
|
|
|
}
|
|
|
|
|
2010-07-15 11:33:03 -07:00
|
|
|
static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
|
|
|
|
if(entry.mLoadedObj != null) {
|
|
|
|
return entry.mLoadedObj;
|
2010-06-04 10:06:50 -07:00
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
// to be purged on cleanup
|
|
|
|
if(entry.mEntryType == EntryType.UNKNOWN) {
|
2010-06-04 10:06:50 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-07-15 11:33:03 -07:00
|
|
|
int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
|
2010-06-04 10:06:50 -07:00
|
|
|
if(objectID == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
switch (entry.mEntryType) {
|
2010-06-04 10:06:50 -07:00
|
|
|
case MESH:
|
2010-07-15 11:33:03 -07:00
|
|
|
entry.mLoadedObj = new Mesh(objectID, rs);
|
2010-06-04 10:06:50 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-15 11:33:03 -07:00
|
|
|
entry.mLoadedObj.updateFromNative();
|
|
|
|
return entry.mLoadedObj;
|
2010-06-04 10:06:50 -07:00
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
|
2010-06-04 10:06:50 -07:00
|
|
|
mRS = rs;
|
|
|
|
mIndex = index;
|
|
|
|
mID = id;
|
|
|
|
mName = name;
|
2010-12-17 11:41:08 -08:00
|
|
|
mEntryType = type;
|
|
|
|
mClassID = mEntryType == EntryType.MESH ? ClassID.MESH : ClassID.UNKNOWN;
|
2010-06-04 10:06:50 -07:00
|
|
|
mLoadedObj = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexEntry[] mFileEntries;
|
2010-10-27 14:10:07 -07:00
|
|
|
InputStream mInputStream;
|
2010-06-04 10:06:50 -07:00
|
|
|
|
2010-10-27 14:10:07 -07:00
|
|
|
FileA3D(int id, RenderScript rs, InputStream stream) {
|
2010-08-11 14:41:28 -07:00
|
|
|
super(id, rs);
|
2010-10-27 14:10:07 -07:00
|
|
|
mInputStream = stream;
|
2010-06-04 10:06:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initEntries() {
|
2010-11-09 17:11:40 -08:00
|
|
|
int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
|
2010-06-04 10:06:50 -07:00
|
|
|
if(numFileEntries <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFileEntries = new IndexEntry[numFileEntries];
|
|
|
|
int[] ids = new int[numFileEntries];
|
|
|
|
String[] names = new String[numFileEntries];
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);
|
2010-06-04 10:06:50 -07:00
|
|
|
|
|
|
|
for(int i = 0; i < numFileEntries; i ++) {
|
2010-12-17 11:41:08 -08:00
|
|
|
mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], EntryType.toEntryType(ids[i]));
|
2010-06-04 10:06:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
public int getIndexEntryCount() {
|
2010-06-04 10:06:50 -07:00
|
|
|
if(mFileEntries == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mFileEntries.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IndexEntry getIndexEntry(int index) {
|
2010-12-17 11:41:08 -08:00
|
|
|
if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
|
2010-06-04 10:06:50 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return mFileEntries[index];
|
|
|
|
}
|
|
|
|
|
2010-12-17 11:41:08 -08:00
|
|
|
// API cleanup stand-ins
|
|
|
|
// TODO: implement ermaining loading mechanisms
|
|
|
|
static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public FileA3D createFromFile(RenderScript rs, String path)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public FileA3D createFromFile(RenderScript rs, File path)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
return createFromFile(rs, path.getAbsolutePath());
|
|
|
|
}
|
|
|
|
|
2010-06-04 10:06:50 -07:00
|
|
|
static public FileA3D createFromResource(RenderScript rs, Resources res, int id)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
|
|
|
rs.validate();
|
|
|
|
InputStream is = null;
|
|
|
|
try {
|
|
|
|
final TypedValue value = new TypedValue();
|
|
|
|
is = res.openRawResource(id, value);
|
|
|
|
|
|
|
|
int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
|
|
|
|
|
|
|
|
int fileId = rs.nFileA3DCreateFromAssetStream(asset);
|
|
|
|
|
|
|
|
if(fileId == 0) {
|
|
|
|
throw new IllegalStateException("Load failed.");
|
|
|
|
}
|
2010-10-27 14:10:07 -07:00
|
|
|
FileA3D fa3d = new FileA3D(fileId, rs, is);
|
2010-06-04 10:06:50 -07:00
|
|
|
fa3d.initEntries();
|
|
|
|
return fa3d;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|