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