Type.java revision fbf0b9ecda03fbdbd4ebabfd18da09a789686249
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.lang.reflect.Field;
20
21import android.renderscript.Element;
22import android.util.Config;
23import android.util.Log;
24
25/**
26 * @hide
27 *
28 **/
29public class Type extends BaseObj {
30    Dimension[] mDimensions;
31    int[] mValues;
32    Element mElement;
33    private int mNativeCache;
34    Class mJavaClass;
35
36
37    Type(int id, RenderScript rs) {
38        super(rs);
39        mID = id;
40        mNativeCache = 0;
41    }
42
43    protected void finalize() throws Throwable {
44        if(mNativeCache != 0) {
45            mRS.nTypeFinalDestroy(this);
46            mNativeCache = 0;
47        }
48        super.finalize();
49    }
50
51    public void destroy() {
52        if(mDestroyed) {
53            throw new IllegalStateException("Object already destroyed.");
54        }
55        mDestroyed = true;
56        mRS.nTypeDestroy(mID);
57    }
58
59    public static Type createFromClass(RenderScript rs, Class c, int size) {
60        Element e = Element.createFromClass(rs, c);
61        Builder b = new Builder(rs, e);
62        b.add(Dimension.X, size);
63        Type t = b.create();
64        e.destroy();
65
66        // native fields
67        {
68            Field[] fields = c.getFields();
69            int[] arTypes = new int[fields.length];
70            int[] arBits = new int[fields.length];
71
72            for(int ct=0; ct < fields.length; ct++) {
73                Field f = fields[ct];
74                Class fc = f.getType();
75                if(fc == int.class) {
76                    arTypes[ct] = Element.DataType.SIGNED.mID;
77                    arBits[ct] = 32;
78                } else if(fc == short.class) {
79                    arTypes[ct] = Element.DataType.SIGNED.mID;
80                    arBits[ct] = 16;
81                } else if(fc == byte.class) {
82                    arTypes[ct] = Element.DataType.SIGNED.mID;
83                    arBits[ct] = 8;
84                } else if(fc == float.class) {
85                    arTypes[ct] = Element.DataType.FLOAT.mID;
86                    arBits[ct] = 32;
87                } else {
88                    throw new IllegalArgumentException("Unkown field type");
89                }
90            }
91            rs.nTypeSetupFields(t, arTypes, arBits, fields);
92        }
93        t.mJavaClass = c;
94        return t;
95    }
96
97    public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
98        Type t = createFromClass(rs, c, size);
99        t.setName(scriptName);
100        return t;
101    }
102
103
104    public static class Builder {
105        RenderScript mRS;
106        Entry[] mEntries;
107        int mEntryCount;
108        Element mElement;
109
110        class Entry {
111            Dimension mDim;
112            int mValue;
113        }
114
115        public Builder(RenderScript rs, Element e) {
116            mRS = rs;
117            mEntries = new Entry[4];
118            mElement = e;
119        }
120
121        public void add(Dimension d, int value) {
122            if(mEntries.length >= mEntryCount) {
123                Entry[] en = new Entry[mEntryCount + 8];
124                for(int ct=0; ct < mEntries.length; ct++) {
125                    en[ct] = mEntries[ct];
126                }
127                mEntries = en;
128            }
129            mEntries[mEntryCount] = new Entry();
130            mEntries[mEntryCount].mDim = d;
131            mEntries[mEntryCount].mValue = value;
132            mEntryCount++;
133        }
134
135        static synchronized Type internalCreate(RenderScript rs, Builder b) {
136            rs.nTypeBegin(b.mElement.mID);
137            for (int ct=0; ct < b.mEntryCount; ct++) {
138                Entry en = b.mEntries[ct];
139                rs.nTypeAdd(en.mDim.mID, en.mValue);
140            }
141            int id = rs.nTypeCreate();
142            return new Type(id, rs);
143        }
144
145        public Type create() {
146            Type t = internalCreate(mRS, this);
147            t.mElement = mElement;
148            t.mDimensions = new Dimension[mEntryCount];
149            t.mValues = new int[mEntryCount];
150            for(int ct=0; ct < mEntryCount; ct++) {
151                t.mDimensions[ct] = mEntries[ct].mDim;
152                t.mValues[ct] = mEntries[ct].mValue;
153            }
154            return t;
155        }
156    }
157
158}
159