1/*
2 * Copyright (C) 2008-2012 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.File;
20import java.io.IOException;
21import java.io.InputStream;
22
23import android.content.res.AssetManager;
24import android.content.res.Resources;
25import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.util.Log;
28import android.util.TypedValue;
29
30/**
31 * @hide
32 * @deprecated in API 16
33 * FileA3D allows users to load RenderScript objects from files
34 * or resources stored on disk. It could be used to load items
35 * such as 3D geometry data converted to a RenderScript format from
36 * content creation tools. Currently only meshes are supported
37 * in FileA3D.
38 *
39 * When successfully loaded, FileA3D will contain a list of
40 * index entries for all the objects stored inside it.
41 *
42 **/
43public class FileA3D extends BaseObj {
44
45    /**
46    * @deprecated in API 16
47    * Specifies what renderscript object type is contained within
48    * the FileA3D IndexEntry
49    **/
50    public enum EntryType {
51
52        /**
53        * @deprecated in API 16
54        * Unknown or or invalid object, nothing will be loaded
55        **/
56        UNKNOWN (0),
57        /**
58        * @deprecated in API 16
59        * RenderScript Mesh object
60        **/
61        MESH (1);
62
63        int mID;
64        EntryType(int id) {
65            mID = id;
66        }
67
68        static EntryType toEntryType(int intID) {
69            return EntryType.values()[intID];
70        }
71    }
72
73    /**
74    * @deprecated in API 16
75    * IndexEntry contains information about one of the RenderScript
76    * objects inside the file's index. It could be used to query the
77    * object's type and also name and load the object itself if
78    * necessary.
79    */
80    public static class IndexEntry {
81        RenderScript mRS;
82        int mIndex;
83        int mID;
84        String mName;
85        EntryType mEntryType;
86        BaseObj mLoadedObj;
87
88        /**
89        * @deprecated in API 16
90        * Returns the name of a renderscript object the index entry
91        * describes
92        *
93        * @return name of a renderscript object the index entry
94        * describes
95        *
96        */
97        public String getName() {
98            return mName;
99        }
100
101        /**
102        * @deprecated in API 16
103        * Returns the type of a renderscript object the index entry
104        * describes
105        * @return type of a renderscript object the index entry
106        *         describes
107        */
108        public EntryType getEntryType() {
109            return mEntryType;
110        }
111
112        /**
113        * @deprecated in API 16
114        * Used to load the object described by the index entry
115        * @return base renderscript object described by the entry
116        */
117        public BaseObj getObject() {
118            mRS.validate();
119            BaseObj obj = internalCreate(mRS, this);
120            return obj;
121        }
122
123        /**
124        * @deprecated in API 16
125        * Used to load the mesh described by the index entry, object
126        * described by the index entry must be a renderscript mesh
127        *
128        * @return renderscript mesh object described by the entry
129        */
130        public Mesh getMesh() {
131            return (Mesh)getObject();
132        }
133
134        static synchronized BaseObj internalCreate(RenderScript rs, IndexEntry entry) {
135            if(entry.mLoadedObj != null) {
136                return entry.mLoadedObj;
137            }
138
139            // to be purged on cleanup
140            if(entry.mEntryType == EntryType.UNKNOWN) {
141                return null;
142            }
143
144            int objectID = rs.nFileA3DGetEntryByIndex(entry.mID, entry.mIndex);
145            if(objectID == 0) {
146                return null;
147            }
148
149            switch (entry.mEntryType) {
150            case MESH:
151                entry.mLoadedObj = new Mesh(objectID, rs);
152                break;
153            }
154
155            entry.mLoadedObj.updateFromNative();
156            return entry.mLoadedObj;
157        }
158
159        IndexEntry(RenderScript rs, int index, int id, String name, EntryType type) {
160            mRS = rs;
161            mIndex = index;
162            mID = id;
163            mName = name;
164            mEntryType = type;
165            mLoadedObj = null;
166        }
167    }
168
169    IndexEntry[] mFileEntries;
170    InputStream mInputStream;
171
172    FileA3D(int id, RenderScript rs, InputStream stream) {
173        super(id, rs);
174        mInputStream = stream;
175    }
176
177    private void initEntries() {
178        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID(mRS));
179        if(numFileEntries <= 0) {
180            return;
181        }
182
183        mFileEntries = new IndexEntry[numFileEntries];
184        int[] ids = new int[numFileEntries];
185        String[] names = new String[numFileEntries];
186
187        mRS.nFileA3DGetIndexEntries(getID(mRS), numFileEntries, ids, names);
188
189        for(int i = 0; i < numFileEntries; i ++) {
190            mFileEntries[i] = new IndexEntry(mRS, i, getID(mRS), names[i], EntryType.toEntryType(ids[i]));
191        }
192    }
193
194    /**
195    * @deprecated in API 16
196    * Returns the number of objects stored inside the a3d file
197    *
198    * @return the number of objects stored inside the a3d file
199    */
200    public int getIndexEntryCount() {
201        if(mFileEntries == null) {
202            return 0;
203        }
204        return mFileEntries.length;
205    }
206
207    /**
208    * @deprecated in API 16
209    * Returns an index entry from the list of all objects inside
210    * FileA3D
211    *
212    * @param index number of the entry from the list to return
213    *
214    * @return entry in the a3d file described by the index
215    */
216    public IndexEntry getIndexEntry(int index) {
217        if(getIndexEntryCount() == 0 || index < 0 || index >= mFileEntries.length) {
218            return null;
219        }
220        return mFileEntries[index];
221    }
222
223    /**
224    * @deprecated in API 16
225    * Creates a FileA3D object from an asset stored on disk
226    *
227    * @param rs Context to which the object will belong.
228    * @param mgr asset manager used to load asset
229    * @param path location of the file to load
230    *
231    * @return a3d file containing renderscript objects
232    */
233    static public FileA3D createFromAsset(RenderScript rs, AssetManager mgr, String path) {
234        rs.validate();
235        int fileId = rs.nFileA3DCreateFromAsset(mgr, path);
236
237        if(fileId == 0) {
238            throw new RSRuntimeException("Unable to create a3d file from asset " + path);
239        }
240        FileA3D fa3d = new FileA3D(fileId, rs, null);
241        fa3d.initEntries();
242        return fa3d;
243    }
244
245    /**
246    * @deprecated in API 16
247    * Creates a FileA3D object from a file stored on disk
248    *
249    * @param rs Context to which the object will belong.
250    * @param path location of the file to load
251    *
252    * @return a3d file containing renderscript objects
253    */
254    static public FileA3D createFromFile(RenderScript rs, String path) {
255        int fileId = rs.nFileA3DCreateFromFile(path);
256
257        if(fileId == 0) {
258            throw new RSRuntimeException("Unable to create a3d file from " + path);
259        }
260        FileA3D fa3d = new FileA3D(fileId, rs, null);
261        fa3d.initEntries();
262        return fa3d;
263    }
264
265    /**
266    * @deprecated in API 16
267    * Creates a FileA3D object from a file stored on disk
268    *
269    * @param rs Context to which the object will belong.
270    * @param path location of the file to load
271    *
272    * @return a3d file containing renderscript objects
273    */
274    static public FileA3D createFromFile(RenderScript rs, File path) {
275        return createFromFile(rs, path.getAbsolutePath());
276    }
277
278    /**
279    * @deprecated in API 16
280    * Creates a FileA3D object from an application resource
281    *
282    * @param rs Context to which the object will belong.
283    * @param res resource manager used for loading
284    * @param id resource to create FileA3D from
285    *
286    * @return a3d file containing renderscript objects
287    */
288    static public FileA3D createFromResource(RenderScript rs, Resources res, int id) {
289
290        rs.validate();
291        InputStream is = null;
292        try {
293            is = res.openRawResource(id);
294        } catch (Exception e) {
295            throw new RSRuntimeException("Unable to open resource " + id);
296        }
297
298        int fileId = 0;
299        if (is instanceof AssetManager.AssetInputStream) {
300            int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
301            fileId = rs.nFileA3DCreateFromAssetStream(asset);
302        } else {
303            throw new RSRuntimeException("Unsupported asset stream");
304        }
305
306        if(fileId == 0) {
307            throw new RSRuntimeException("Unable to create a3d file from resource " + id);
308        }
309        FileA3D fa3d = new FileA3D(fileId, rs, is);
310        fa3d.initEntries();
311        return fa3d;
312
313    }
314}
315