blob: c3e5fafff9f110f70ad5b0df572e357a6064d8cb [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/**
31 * @hide
32 *
33 **/
34public class FileA3D extends BaseObj {
35
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080036 // This will go away in the clean up pass,
37 // trying to avoid multiproject submits
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070038 public enum ClassID {
39
40 UNKNOWN,
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080041 MESH;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070042
43 public static ClassID toClassID(int intID) {
44 return ClassID.values()[intID];
45 }
46 }
47
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080048 public enum EntryType {
49
50 UNKNOWN (0),
51 MESH (1);
52
53 int mID;
54 EntryType(int id) {
55 mID = id;
56 }
57
58 static EntryType toEntryType(int intID) {
59 return EntryType.values()[intID];
60 }
61 }
62
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070063 // Read only class with index entries
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070064 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070065 RenderScript mRS;
66 int mIndex;
67 int mID;
68 String mName;
69 ClassID mClassID;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080070 EntryType mEntryType;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070071 BaseObj mLoadedObj;
72
73 public String getName() {
74 return mName;
75 }
76
77 public ClassID getClassID() {
78 return mClassID;
79 }
80
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080081 public EntryType getEntryType() {
82 return mEntryType;
83 }
84
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070085 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070086 mRS.validate();
87 BaseObj obj = internalCreate(mRS, this);
88 return obj;
89 }
90
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080091 public Mesh getMesh() {
92 return (Mesh)getObject();
93 }
94
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070095 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
96 if(entry.mLoadedObj != null) {
97 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070098 }
99
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800100 // to be purged on cleanup
101 if(entry.mEntryType == EntryType.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700102 return null;
103 }
104
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700105 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700106 if(objectID == 0) {
107 return null;
108 }
109
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800110 switch (entry.mEntryType) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700111 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700112 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700113 break;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700114 }
115
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700116 entry.mLoadedObj.updateFromNative();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700117 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700118 }
119
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800120 IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700121 mRS = rs;
122 mIndex = index;
123 mID = id;
124 mName = name;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800125 mEntryType = type;
126 mClassID = mEntryType == EntryType.MESH ? ClassID.MESH : ClassID.UNKNOWN;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700127 mLoadedObj = null;
128 }
129 }
130
131 IndexEntry[] mFileEntries;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700132 InputStream mInputStream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700133
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700134 FileA3D(int id, RenderScript rs, InputStream stream) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700135 super(id, rs);
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700136 mInputStream = stream;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700137 }
138
139 private void initEntries() {
Jason Sams06d69de2010-11-09 17:11:40 -0800140 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700141 if(numFileEntries <= 0) {
142 return;
143 }
144
145 mFileEntries = new IndexEntry[numFileEntries];
146 int[] ids = new int[numFileEntries];
147 String[] names = new String[numFileEntries];
148
Jason Sams06d69de2010-11-09 17:11:40 -0800149 mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700150
151 for(int i = 0; i < numFileEntries; i ++) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800152 mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], EntryType.toEntryType(ids[i]));
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700153 }
154 }
155
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800156 public int getIndexEntryCount() {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700157 if(mFileEntries == null) {
158 return 0;
159 }
160 return mFileEntries.length;
161 }
162
163 public IndexEntry getIndexEntry(int index) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800164 if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700165 return null;
166 }
167 return mFileEntries[index];
168 }
169
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800170 // API cleanup stand-ins
171 // TODO: implement ermaining loading mechanisms
172 static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path)
173 throws IllegalArgumentException {
174 return null;
175 }
176
177 static public FileA3D createFromFile(RenderScript rs, String path)
178 throws IllegalArgumentException {
179 return null;
180 }
181
182 static public FileA3D createFromFile(RenderScript rs, File path)
183 throws IllegalArgumentException {
184 return createFromFile(rs, path.getAbsolutePath());
185 }
186
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700187 static public FileA3D createFromResource(RenderScript rs, Resources res, int id)
188 throws IllegalArgumentException {
189
190 rs.validate();
191 InputStream is = null;
192 try {
193 final TypedValue value = new TypedValue();
194 is = res.openRawResource(id, value);
195
196 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
197
198 int fileId = rs.nFileA3DCreateFromAssetStream(asset);
199
200 if(fileId == 0) {
201 throw new IllegalStateException("Load failed.");
202 }
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700203 FileA3D fa3d = new FileA3D(fileId, rs, is);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700204 fa3d.initEntries();
205 return fa3d;
206
207 } catch (Exception e) {
208 // Ignore
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700209 }
210
211 return null;
212 }
213}