Element.java revision 1bada8cd6e4f340de93cff4a2439835fc3b1456c
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
19
20/**
21 * @hide
22 *
23 **/
24public class Element extends BaseObj {
25    final int mPredefinedID;
26    final boolean mIsPredefined;
27
28    public static final Element USER_U8 = new Element(0);
29    public static final Element USER_I8 = new Element(1);
30    public static final Element USER_U16 = new Element(2);
31    public static final Element USER_I16 = new Element(3);
32    public static final Element USER_U32 = new Element(4);
33    public static final Element USER_I32 = new Element(5);
34    public static final Element USER_FLOAT = new Element(6);
35
36    public static final Element A_8 = new Element(7);
37    public static final Element RGB_565 = new Element(8);
38    public static final Element RGB_888 = new Element(11);
39    public static final Element RGBA_5551 = new Element(9);
40    public static final Element RGBA_4444 = new Element(10);
41    public static final Element RGBA_8888 = new Element(12);
42
43    public static final Element INDEX_16 = new Element(13);
44    public static final Element INDEX_32 = new Element(14);
45    public static final Element XY_F32 = new Element(15);
46    public static final Element XYZ_F32 = new Element(16);
47    public static final Element ST_XY_F32 = new Element(17);
48    public static final Element ST_XYZ_F32 = new Element(18);
49    public static final Element NORM_XYZ_F32 = new Element(19);
50    public static final Element NORM_ST_XYZ_F32 = new Element(20);
51
52    void initPredef(RenderScript rs) {
53        mID = rs.nElementGetPredefined(mPredefinedID);
54    }
55
56    static void init(RenderScript rs) {
57        USER_U8.initPredef(rs);
58        USER_I8.initPredef(rs);
59        USER_U16.initPredef(rs);
60        USER_I16.initPredef(rs);
61        USER_U32.initPredef(rs);
62        USER_I32.initPredef(rs);
63        USER_FLOAT.initPredef(rs);
64
65        A_8.initPredef(rs);
66        RGB_565.initPredef(rs);
67        RGB_888.initPredef(rs);
68        RGBA_5551.initPredef(rs);
69        RGBA_4444.initPredef(rs);
70        RGBA_8888.initPredef(rs);
71
72        INDEX_16.initPredef(rs);
73        INDEX_32.initPredef(rs);
74        XY_F32.initPredef(rs);
75        XYZ_F32.initPredef(rs);
76        ST_XY_F32.initPredef(rs);
77        ST_XYZ_F32.initPredef(rs);
78        NORM_XYZ_F32.initPredef(rs);
79        NORM_ST_XYZ_F32.initPredef(rs);
80    }
81
82
83    public enum DataType {
84        FLOAT (0),
85        UNSIGNED (1),
86        SIGNED (2);
87
88        int mID;
89        DataType(int id) {
90            mID = id;
91        }
92    }
93
94    public enum DataKind {
95        USER (0),
96        RED (1),
97        GREEN (2),
98        BLUE (3),
99        ALPHA (4),
100        LUMINANCE (5),
101        INTENSITY (6),
102        X (7),
103        Y (8),
104        Z (9),
105        W (10),
106        S (11),
107        T (12),
108        Q (13),
109        R (14),
110        NX (15),
111        NY (16),
112        NZ (17),
113        INDEX (18);
114
115        int mID;
116        DataKind(int id) {
117            mID = id;
118        }
119    }
120
121
122    Element(int predef) {
123        super(null);
124        mID = 0;
125        mPredefinedID = predef;
126        mIsPredefined = true;
127    }
128
129    Element(int id, RenderScript rs) {
130        super(rs);
131        mID = id;
132        mPredefinedID = 0;
133        mIsPredefined = false;
134    }
135
136    public void destroy() throws IllegalStateException {
137        if(mIsPredefined) {
138            throw new IllegalStateException("Attempting to destroy a predefined Element.");
139        }
140        if(mDestroyed) {
141            throw new IllegalStateException("Object already destroyed.");
142        }
143        mDestroyed = true;
144        mRS.nElementDestroy(mID);
145    }
146
147
148
149
150    public static class Builder {
151        RenderScript mRS;
152        Entry[] mEntries;
153        int mEntryCount;
154
155        private class Entry {
156            Element mElement;
157            Element.DataType mType;
158            Element.DataKind mKind;
159            boolean mIsNormalized;
160            int mBits;
161        }
162
163        public Builder(RenderScript rs) {
164            mRS = rs;
165            mEntryCount = 0;
166            mEntries = new Entry[8];
167        }
168
169        void addEntry(Entry e) {
170            if(mEntries.length >= mEntryCount) {
171                Entry[] en = new Entry[mEntryCount + 8];
172                for(int ct=0; ct < mEntries.length; ct++) {
173                    en[ct] = mEntries[ct];
174                }
175                mEntries = en;
176            }
177            mEntries[mEntryCount] = e;
178            mEntryCount++;
179        }
180
181        public Builder add(Element e) throws IllegalArgumentException {
182            if(!e.mIsPredefined) {
183                throw new IllegalArgumentException("add requires a predefined Element.");
184            }
185            Entry en = new Entry();
186            en.mElement = e;
187            addEntry(en);
188            return this;
189        }
190
191        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
192            Entry en = new Entry();
193            en.mType = dt;
194            en.mKind = dk;
195            en.mIsNormalized = isNormalized;
196            en.mBits = bits;
197            addEntry(en);
198            return this;
199        }
200
201        static synchronized Element internalCreate(RenderScript rs, Builder b) {
202            rs.nElementBegin();
203            for (int ct=0; ct < b.mEntryCount; ct++) {
204                Entry en = b.mEntries[ct];
205                if(en.mElement !=  null) {
206                    rs.nElementAddPredefined(en.mElement.mPredefinedID);
207                } else {
208                    int norm = 0;
209                    if (en.mIsNormalized) {
210                        norm = 1;
211                    }
212                    rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits);
213                }
214            }
215            int id = rs.nElementCreate();
216            return new Element(id, rs);
217        }
218
219        public Element create() {
220            return internalCreate(mRS, this);
221        }
222    }
223
224}
225
226