blob: 3f750690bd938ed212b6c989af503aed91f5fc75 [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
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
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
Jason Sams36e612a2009-07-31 16:26:13 -070020
21/**
22 * @hide
23 *
24 **/
25public class Element extends BaseObj {
26 final int mPredefinedID;
27 final boolean mIsPredefined;
28
29 public static final Element USER_U8 = new Element(0);
30 public static final Element USER_I8 = new Element(1);
31 public static final Element USER_U16 = new Element(2);
32 public static final Element USER_I16 = new Element(3);
33 public static final Element USER_U32 = new Element(4);
34 public static final Element USER_I32 = new Element(5);
35 public static final Element USER_FLOAT = new Element(6);
36
37 public static final Element A_8 = new Element(7);
38 public static final Element RGB_565 = new Element(8);
39 public static final Element RGB_888 = new Element(11);
40 public static final Element RGBA_5551 = new Element(9);
41 public static final Element RGBA_4444 = new Element(10);
42 public static final Element RGBA_8888 = new Element(12);
43
44 public static final Element INDEX_16 = new Element(13);
45 public static final Element INDEX_32 = new Element(14);
46 public static final Element XY_F32 = new Element(15);
47 public static final Element XYZ_F32 = new Element(16);
48 public static final Element ST_XY_F32 = new Element(17);
49 public static final Element ST_XYZ_F32 = new Element(18);
50 public static final Element NORM_XYZ_F32 = new Element(19);
51 public static final Element NORM_ST_XYZ_F32 = new Element(20);
52
53 void initPredef(RenderScript rs) {
54 mID = rs.nElementGetPredefined(mPredefinedID);
55 }
56
57 static void init(RenderScript rs) {
58 USER_U8.initPredef(rs);
59 USER_I8.initPredef(rs);
60 USER_U16.initPredef(rs);
61 USER_I16.initPredef(rs);
62 USER_U32.initPredef(rs);
63 USER_I32.initPredef(rs);
64 USER_FLOAT.initPredef(rs);
65
66 A_8.initPredef(rs);
67 RGB_565.initPredef(rs);
68 RGB_888.initPredef(rs);
69 RGBA_5551.initPredef(rs);
70 RGBA_4444.initPredef(rs);
71 RGBA_8888.initPredef(rs);
72
73 INDEX_16.initPredef(rs);
74 INDEX_32.initPredef(rs);
75 XY_F32.initPredef(rs);
76 XYZ_F32.initPredef(rs);
77 ST_XY_F32.initPredef(rs);
78 ST_XYZ_F32.initPredef(rs);
79 NORM_XYZ_F32.initPredef(rs);
80 NORM_ST_XYZ_F32.initPredef(rs);
81 }
82
83
84 public enum DataType {
85 FLOAT (0),
86 UNSIGNED (1),
87 SIGNED (2);
88
89 int mID;
90 DataType(int id) {
91 mID = id;
92 }
93 }
94
95 public enum DataKind {
96 USER (0),
97 RED (1),
98 GREEN (2),
99 BLUE (3),
100 ALPHA (4),
101 LUMINANCE (5),
102 INTENSITY (6),
103 X (7),
104 Y (8),
105 Z (9),
106 W (10),
107 S (11),
108 T (12),
109 Q (13),
110 R (14),
111 NX (15),
112 NY (16),
113 NZ (17),
114 INDEX (18);
115
116 int mID;
117 DataKind(int id) {
118 mID = id;
119 }
120 }
121
122
123 Element(int predef) {
124 super(null);
125 mID = 0;
126 mPredefinedID = predef;
127 mIsPredefined = true;
128 }
129
130 Element(int id, RenderScript rs) {
131 super(rs);
132 mID = id;
133 mPredefinedID = 0;
134 mIsPredefined = false;
135 }
136
137 public void destroy() throws IllegalStateException {
138 if(mIsPredefined) {
139 throw new IllegalStateException("Attempting to destroy a predefined Element.");
140 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700141 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700142 }
143
Jason Sams43ee06852009-08-12 17:54:11 -0700144 public static Element createFromClass(RenderScript rs, Class c) {
145 Field[] fields = c.getFields();
146 Builder b = new Builder(rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700147
Jason Sams43ee06852009-08-12 17:54:11 -0700148 for(Field f: fields) {
149 Class fc = f.getType();
150 if(fc == int.class) {
151 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName());
152 } else if(fc == short.class) {
153 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName());
154 } else if(fc == byte.class) {
155 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName());
156 } else if(fc == float.class) {
157 b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName());
158 } else {
159 throw new IllegalArgumentException("Unkown field type");
160 }
161 }
162 return b.create();
163 }
Jason Sams36e612a2009-07-31 16:26:13 -0700164
165
166 public static class Builder {
167 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700168 Entry[] mEntries;
169 int mEntryCount;
Jason Sams36e612a2009-07-31 16:26:13 -0700170
Jason Sams22534172009-08-04 16:58:20 -0700171 private class Entry {
172 Element mElement;
173 Element.DataType mType;
174 Element.DataKind mKind;
175 boolean mIsNormalized;
176 int mBits;
Jason Sams43ee06852009-08-12 17:54:11 -0700177 String mName;
Jason Sams22534172009-08-04 16:58:20 -0700178 }
179
180 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700181 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700182 mEntryCount = 0;
183 mEntries = new Entry[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700184 }
185
Jason Sams22534172009-08-04 16:58:20 -0700186 void addEntry(Entry e) {
187 if(mEntries.length >= mEntryCount) {
188 Entry[] en = new Entry[mEntryCount + 8];
Romain Guy81e46402009-08-14 18:58:33 -0700189 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
Jason Sams22534172009-08-04 16:58:20 -0700190 mEntries = en;
Jason Sams36e612a2009-07-31 16:26:13 -0700191 }
Jason Sams22534172009-08-04 16:58:20 -0700192 mEntries[mEntryCount] = e;
193 mEntryCount++;
Jason Sams36e612a2009-07-31 16:26:13 -0700194 }
195
Jason Sams22534172009-08-04 16:58:20 -0700196 public Builder add(Element e) throws IllegalArgumentException {
Jason Sams36e612a2009-07-31 16:26:13 -0700197 if(!e.mIsPredefined) {
198 throw new IllegalArgumentException("add requires a predefined Element.");
199 }
Jason Sams22534172009-08-04 16:58:20 -0700200 Entry en = new Entry();
201 en.mElement = e;
202 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700203 return this;
204 }
205
Jason Sams43ee06852009-08-12 17:54:11 -0700206 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
Jason Sams22534172009-08-04 16:58:20 -0700207 Entry en = new Entry();
208 en.mType = dt;
209 en.mKind = dk;
210 en.mIsNormalized = isNormalized;
211 en.mBits = bits;
Jason Sams43ee06852009-08-12 17:54:11 -0700212 en.mName = name;
Jason Sams22534172009-08-04 16:58:20 -0700213 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700214 return this;
215 }
216
Jason Sams43ee06852009-08-12 17:54:11 -0700217 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
218 add(dt, dk, isNormalized, bits, null);
219 return this;
220 }
221
Jason Sams334ea0c2009-08-17 13:56:09 -0700222 public Builder addFloat(Element.DataKind dk) {
223 add(DataType.FLOAT, dk, false, 32, null);
224 return this;
225 }
226
227 public Builder addFloat(Element.DataKind dk, String name) {
228 add(DataType.FLOAT, dk, false, 32, name);
229 return this;
230 }
231
232 public Builder addFloatXY() {
233 add(DataType.FLOAT, DataKind.X, false, 32, null);
234 add(DataType.FLOAT, DataKind.Y, false, 32, null);
235 return this;
236 }
237
238 public Builder addFloatXYZ() {
239 add(DataType.FLOAT, DataKind.X, false, 32, null);
240 add(DataType.FLOAT, DataKind.Y, false, 32, null);
241 add(DataType.FLOAT, DataKind.Z, false, 32, null);
242 return this;
243 }
Romain Guy4f7136c2009-08-17 19:59:27 -0700244
245 public Builder addFloatST() {
246 add(DataType.FLOAT, DataKind.S, false, 32, null);
247 add(DataType.FLOAT, DataKind.T, false, 32, null);
248 return this;
249 }
Jason Sams334ea0c2009-08-17 13:56:09 -0700250
251 public Builder addFloatRGB() {
252 add(DataType.FLOAT, DataKind.RED, false, 32, null);
253 add(DataType.FLOAT, DataKind.GREEN, false, 32, null);
254 add(DataType.FLOAT, DataKind.BLUE, false, 32, null);
255 return this;
256 }
257
258 public Builder addFloatRGBA() {
259 add(DataType.FLOAT, DataKind.RED, false, 32, null);
260 add(DataType.FLOAT, DataKind.GREEN, false, 32, null);
261 add(DataType.FLOAT, DataKind.BLUE, false, 32, null);
262 add(DataType.FLOAT, DataKind.ALPHA, false, 32, null);
263 return this;
264 }
265
266 public Builder addUNorm8RGBA() {
267 add(DataType.UNSIGNED, DataKind.RED, true, 8, null);
268 add(DataType.UNSIGNED, DataKind.GREEN, true, 8, null);
269 add(DataType.UNSIGNED, DataKind.BLUE, true, 8, null);
270 add(DataType.UNSIGNED, DataKind.ALPHA, true, 8, null);
271 return this;
272 }
273
Jason Sams22534172009-08-04 16:58:20 -0700274 static synchronized Element internalCreate(RenderScript rs, Builder b) {
275 rs.nElementBegin();
276 for (int ct=0; ct < b.mEntryCount; ct++) {
277 Entry en = b.mEntries[ct];
278 if(en.mElement != null) {
279 rs.nElementAddPredefined(en.mElement.mPredefinedID);
280 } else {
281 int norm = 0;
282 if (en.mIsNormalized) {
283 norm = 1;
284 }
Jason Sams43ee06852009-08-12 17:54:11 -0700285 rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName);
Jason Sams22534172009-08-04 16:58:20 -0700286 }
Jason Sams36e612a2009-07-31 16:26:13 -0700287 }
Jason Sams22534172009-08-04 16:58:20 -0700288 int id = rs.nElementCreate();
289 return new Element(id, rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700290 }
291
Jason Sams22534172009-08-04 16:58:20 -0700292 public Element create() {
293 return internalCreate(mRS, this);
Jason Sams36e612a2009-07-31 16:26:13 -0700294 }
295 }
296
297}
298