blob: 24140620b15090a0e10a0281f5a66668d249e52d [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
19import java.io.IOException;
20import java.io.InputStream;
21
22import android.content.res.Resources;
23import android.content.res.AssetManager;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.util.Log;
27import android.util.TypedValue;
28
29/**
30 * @hide
31 *
32 **/
33public class FileA3D extends BaseObj {
34
35 public enum ClassID {
36
37 UNKNOWN,
38 MESH,
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070039 TYPE,
40 ELEMENT,
41 ALLOCATION,
42 PROGRAM_VERTEX,
43 PROGRAM_RASTER,
44 PROGRAM_FRAGMENT,
45 PROGRAM_STORE,
46 SAMPLER,
47 ANIMATION,
48 LIGHT,
49 ADAPTER_1D,
50 ADAPTER_2D,
51 SCRIPT_C;
52
53 public static ClassID toClassID(int intID) {
54 return ClassID.values()[intID];
55 }
56 }
57
58 // Read only class with index entries
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070059 public static class IndexEntry {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070060 RenderScript mRS;
61 int mIndex;
62 int mID;
63 String mName;
64 ClassID mClassID;
65 BaseObj mLoadedObj;
66
67 public String getName() {
68 return mName;
69 }
70
71 public ClassID getClassID() {
72 return mClassID;
73 }
74
75 public BaseObj getObject() {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070076 mRS.validate();
77 BaseObj obj = internalCreate(mRS, this);
78 return obj;
79 }
80
81 static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
82 if(entry.mLoadedObj != null) {
83 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070084 }
85
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070086 if(entry.mClassID == ClassID.UNKNOWN) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070087 return null;
88 }
89
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070090 int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070091 if(objectID == 0) {
92 return null;
93 }
94
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070095 switch (entry.mClassID) {
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070096 case MESH:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070097 entry.mLoadedObj = new Mesh(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070098 break;
99 case TYPE:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700100 entry.mLoadedObj = new Type(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700101 break;
102 case ELEMENT:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700103 entry.mLoadedObj = null;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700104 break;
105 case ALLOCATION:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700106 entry.mLoadedObj = null;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700107 break;
108 case PROGRAM_VERTEX:
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700109 entry.mLoadedObj = new ProgramVertex(objectID, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700110 break;
111 case PROGRAM_RASTER:
112 break;
113 case PROGRAM_FRAGMENT:
114 break;
115 case PROGRAM_STORE:
116 break;
117 case SAMPLER:
118 break;
119 case ANIMATION:
120 break;
121 case LIGHT:
122 break;
123 case ADAPTER_1D:
124 break;
125 case ADAPTER_2D:
126 break;
127 case SCRIPT_C:
128 break;
129 }
130
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700131 entry.mLoadedObj.updateFromNative();
132
133 return entry.mLoadedObj;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700134 }
135
136 IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) {
137 mRS = rs;
138 mIndex = index;
139 mID = id;
140 mName = name;
141 mClassID = classID;
142 mLoadedObj = null;
143 }
144 }
145
146 IndexEntry[] mFileEntries;
147
148 FileA3D(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700149 super(id, rs);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700150 }
151
152 private void initEntries() {
153 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID);
154 if(numFileEntries <= 0) {
155 return;
156 }
157
158 mFileEntries = new IndexEntry[numFileEntries];
159 int[] ids = new int[numFileEntries];
160 String[] names = new String[numFileEntries];
161
162 mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names);
163
164 for(int i = 0; i < numFileEntries; i ++) {
165 mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i]));
166 }
167 }
168
169 public int getNumIndexEntries() {
170 if(mFileEntries == null) {
171 return 0;
172 }
173 return mFileEntries.length;
174 }
175
176 public IndexEntry getIndexEntry(int index) {
177 if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) {
178 return null;
179 }
180 return mFileEntries[index];
181 }
182
183 static public FileA3D createFromResource(RenderScript rs, Resources res, int id)
184 throws IllegalArgumentException {
185
186 rs.validate();
187 InputStream is = null;
188 try {
189 final TypedValue value = new TypedValue();
190 is = res.openRawResource(id, value);
191
192 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
193
194 int fileId = rs.nFileA3DCreateFromAssetStream(asset);
195
196 if(fileId == 0) {
197 throw new IllegalStateException("Load failed.");
198 }
199 FileA3D fa3d = new FileA3D(fileId, rs);
200 fa3d.initEntries();
201 return fa3d;
202
203 } catch (Exception e) {
204 // Ignore
205 } finally {
206 if (is != null) {
207 try {
208 is.close();
209 } catch (IOException e) {
210 // Ignore
211 }
212 }
213 }
214
215 return null;
216 }
217}