FileA3D.java revision 164aaedf7f24827c3da84acc733325ae985930d6
1/*
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,
39        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
59    public class IndexEntry {
60        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() {
76            if(mLoadedObj != null) {
77                return mLoadedObj;
78            }
79
80            if(mClassID == ClassID.UNKNOWN) {
81                return null;
82            }
83
84            int objectID = mRS.nFileA3DGetEntryByIndex(mID, mIndex);
85            if(objectID == 0) {
86                return null;
87            }
88
89            switch (mClassID) {
90            case MESH:
91                mLoadedObj = new Mesh(objectID, mRS);
92                break;
93            case TYPE:
94                mLoadedObj = new Type(objectID, mRS);
95                break;
96            case ELEMENT:
97                mLoadedObj = null;
98                break;
99            case ALLOCATION:
100                mLoadedObj = null;
101                break;
102            case PROGRAM_VERTEX:
103                mLoadedObj = new ProgramVertex(objectID, mRS);
104                break;
105            case PROGRAM_RASTER:
106                break;
107            case PROGRAM_FRAGMENT:
108                break;
109            case PROGRAM_STORE:
110                break;
111            case SAMPLER:
112                break;
113            case ANIMATION:
114                break;
115            case LIGHT:
116                break;
117            case ADAPTER_1D:
118                break;
119            case ADAPTER_2D:
120                break;
121            case SCRIPT_C:
122                break;
123            }
124
125            return mLoadedObj;
126        }
127
128        IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) {
129            mRS = rs;
130            mIndex = index;
131            mID = id;
132            mName = name;
133            mClassID = classID;
134            mLoadedObj = null;
135        }
136    }
137
138    IndexEntry[] mFileEntries;
139
140    FileA3D(int id, RenderScript rs) {
141        super(rs);
142        mID = id;
143    }
144
145    private void initEntries() {
146        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID);
147        if(numFileEntries <= 0) {
148            return;
149        }
150
151        mFileEntries = new IndexEntry[numFileEntries];
152        int[] ids = new int[numFileEntries];
153        String[] names = new String[numFileEntries];
154
155        mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names);
156
157        for(int i = 0; i < numFileEntries; i ++) {
158            mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i]));
159        }
160    }
161
162    public int getNumIndexEntries() {
163        if(mFileEntries == null) {
164            return 0;
165        }
166        return mFileEntries.length;
167    }
168
169    public IndexEntry getIndexEntry(int index) {
170        if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) {
171            return null;
172        }
173        return mFileEntries[index];
174    }
175
176    static public FileA3D createFromResource(RenderScript rs, Resources res, int id)
177        throws IllegalArgumentException {
178
179        rs.validate();
180        InputStream is = null;
181        try {
182            final TypedValue value = new TypedValue();
183            is = res.openRawResource(id, value);
184
185            int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
186
187            int fileId = rs.nFileA3DCreateFromAssetStream(asset);
188
189            if(fileId == 0) {
190                throw new IllegalStateException("Load failed.");
191            }
192            FileA3D fa3d = new FileA3D(fileId, rs);
193            fa3d.initEntries();
194            return fa3d;
195
196        } catch (Exception e) {
197            // Ignore
198        } finally {
199            if (is != null) {
200                try {
201                    is.close();
202                } catch (IOException e) {
203                    // Ignore
204                }
205            }
206        }
207
208        return null;
209    }
210}
211