Type.java revision 1b52aae4d908fcf749e9a8d86bb0a33c70728c56
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, String scriptName) {
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        t.setName(scriptName);
95        return t;
96    }
97
98    public static class Builder {
99        RenderScript mRS;
100        Entry[] mEntries;
101        int mEntryCount;
102        Element mElement;
103
104        class Entry {
105            Dimension mDim;
106            int mValue;
107        }
108
109        public Builder(RenderScript rs, Element e) {
110            mRS = rs;
111            mEntries = new Entry[4];
112            mElement = e;
113        }
114
115        public void add(Dimension d, int value) {
116            if(mEntries.length >= mEntryCount) {
117                Entry[] en = new Entry[mEntryCount + 8];
118                for(int ct=0; ct < mEntries.length; ct++) {
119                    en[ct] = mEntries[ct];
120                }
121                mEntries = en;
122            }
123            mEntries[mEntryCount] = new Entry();
124            mEntries[mEntryCount].mDim = d;
125            mEntries[mEntryCount].mValue = value;
126            mEntryCount++;
127        }
128
129        static synchronized Type internalCreate(RenderScript rs, Builder b) {
130            rs.nTypeBegin(b.mElement.mID);
131            for (int ct=0; ct < b.mEntryCount; ct++) {
132                Entry en = b.mEntries[ct];
133                rs.nTypeAdd(en.mDim.mID, en.mValue);
134            }
135            int id = rs.nTypeCreate();
136            return new Type(id, rs);
137        }
138
139        public Type create() {
140            Type t = internalCreate(mRS, this);
141            t.mElement = mElement;
142            t.mDimensions = new Dimension[mEntryCount];
143            t.mValues = new int[mEntryCount];
144            for(int ct=0; ct < mEntryCount; ct++) {
145                t.mDimensions[ct] = mEntries[ct].mDim;
146                t.mValues[ct] = mEntries[ct].mValue;
147            }
148            return t;
149        }
150    }
151
152}
153