blob: 302a5f48938ad8f82e611e8e6306e2f44dae961b [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) {
149 super(rs);
150 mID = id;
151 }
152
153 private void initEntries() {
154 int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID);
155 if(numFileEntries <= 0) {
156 return;
157 }
158
159 mFileEntries = new IndexEntry[numFileEntries];
160 int[] ids = new int[numFileEntries];
161 String[] names = new String[numFileEntries];
162
163 mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names);
164
165 for(int i = 0; i < numFileEntries; i ++) {
166 mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i]));
167 }
168 }
169
170 public int getNumIndexEntries() {
171 if(mFileEntries == null) {
172 return 0;
173 }
174 return mFileEntries.length;
175 }
176
177 public IndexEntry getIndexEntry(int index) {
178 if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) {
179 return null;
180 }
181 return mFileEntries[index];
182 }
183
184 static public FileA3D createFromResource(RenderScript rs, Resources res, int id)
185 throws IllegalArgumentException {
186
187 rs.validate();
188 InputStream is = null;
189 try {
190 final TypedValue value = new TypedValue();
191 is = res.openRawResource(id, value);
192
193 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
194
195 int fileId = rs.nFileA3DCreateFromAssetStream(asset);
196
197 if(fileId == 0) {
198 throw new IllegalStateException("Load failed.");
199 }
200 FileA3D fa3d = new FileA3D(fileId, rs);
201 fa3d.initEntries();
202 return fa3d;
203
204 } catch (Exception e) {
205 // Ignore
206 } finally {
207 if (is != null) {
208 try {
209 is.close();
210 } catch (IOException e) {
211 // Ignore
212 }
213 }
214 }
215
216 return null;
217 }
218}