blob: 90d102cff9280fdfad93533eac892d6940632fc8 [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 Sakhartchoukaae74ad2010-06-04 10:06:50 -070031 *
32 **/
33public class FileA3D extends BaseObj {
34
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080035 // This will go away in the clean up pass,
36 // trying to avoid multiproject submits
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070037 public enum ClassID {
38
39 UNKNOWN,
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080040 MESH;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070041
42 public static ClassID toClassID(int intID) {
43 return ClassID.values()[intID];
44 }
45 }
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 Sakhartchoukaae74ad2010-06-04 10:06:50 -070062 // Read only class with index entries
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070063 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070064 RenderScript mRS;
65 int mIndex;
66 int mID;
67 String mName;
68 ClassID mClassID;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080069 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070070 BaseObj mLoadedObj;
71
72 public String getName() {
73 return mName;
74 }
75
76 public ClassID getClassID() {
77 return mClassID;
78 }
79
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080080 public EntryType getEntryType() {
81 return mEntryType;
82 }
83
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070084 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070085 mRS.validate();
86 BaseObj obj = internalCreate(mRS, this);
87 return obj;
88 }
89
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080090 public Mesh getMesh() {
91 return (Mesh)getObject();
92 }
93
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070094 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
95 if(entry.mLoadedObj != null) {
96 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070097 }
98
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080099 // to be purged on cleanup
100 if(entry.mEntryType == EntryType.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700101 return null;
102 }
103
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700104 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700105 if(objectID == 0) {
106 return null;
107 }
108
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800109 switch (entry.mEntryType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700110 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700111 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700112 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700113 }
114
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700115 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700116 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700117 }
118
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800119 IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700120 mRS = rs;
121 mIndex = index;
122 mID = id;
123 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800124 mEntryType = type;
125 mClassID = mEntryType == EntryType.MESH ? ClassID.MESH : ClassID.UNKNOWN;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700126 mLoadedObj = null;
127 }
128 }
129
130 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700131 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700132
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700133 FileA3D(int id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700134 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700135 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700136 }
137
138 private void initEntries() {
Jason Sams06d69de2010-11-09 17:11:40 -0800139 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700140 if(numFileEntries <= 0) {
141 return;
142 }
143
144 mFileEntries = new IndexEntry[numFileEntries];
145 int[] ids = new int[numFileEntries];
146 String[] names = new String[numFileEntries];
147
Jason Sams06d69de2010-11-09 17:11:40 -0800148 mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700149
150 for(int i = 0; i < numFileEntries; i ++) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800151 mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700152 }
153 }
154
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800155 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700156 if(mFileEntries == null) {
157 return 0;
158 }
159 return mFileEntries.length;
160 }
161
162 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800163 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700164 return null;
165 }
166 return mFileEntries[index];
167 }
168
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800169 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
170 rs.validate();
171 int fileId = rs.nFileA3DCreateFromAsset(mgr, path);
172
173 if(fileId == 0) {
174 throw new RSRuntimeException("Unable to create a3d file from asset " + path);
175 }
176 FileA3D fa3d = new FileA3D(fileId, rs, null);
177 fa3d.initEntries();
178 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800179 }
180
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800181 static public FileA3D createFromFile(RenderScript rs, String path) {
182 int fileId = rs.nFileA3DCreateFromFile(path);
183
184 if(fileId == 0) {
185 throw new RSRuntimeException("Unable to create a3d file from " + path);
186 }
187 FileA3D fa3d = new FileA3D(fileId, rs, null);
188 fa3d.initEntries();
189 return fa3d;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800190 }
191
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800192 static public FileA3D createFromFile(RenderScript rs, File path) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800193 return createFromFile(rs, path.getAbsolutePath());
194 }
195
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800196 static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700197
198 rs.validate();
199 InputStream is = null;
200 try {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800201 is = res.openRawResource(id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700202 } catch (Exception e) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800203 throw new RSRuntimeException("Unable to open resource " + id);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700204 }
205
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800206 int fileId = 0;
207 if (is instanceof AssetManager.AssetInputStream) {
208 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
209 fileId = rs.nFileA3DCreateFromAssetStream(asset);
210 } else {
211 throw new RSRuntimeException("Unsupported asset stream");
212 }
213
214 if(fileId == 0) {
215 throw new RSRuntimeException("Unable to create a3d file from resource " + id);
216 }
217 FileA3D fa3d = new FileA3D(fileId, rs, is);
218 fa3d.initEntries();
219 return fa3d;
220
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700221 }
222}