blob: 9155da8414afda29c338107f1a180edb730876da [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 android.util.Config;
20import android.util.Log;
21
22import java.lang.reflect.Field;
Jason Sams36e612a2009-07-31 16:26:13 -070023
24/**
25 * @hide
26 *
27 **/
28public class Element extends BaseObj {
29 final int mPredefinedID;
30 final boolean mIsPredefined;
31
32 public static final Element USER_U8 = new Element(0);
33 public static final Element USER_I8 = new Element(1);
34 public static final Element USER_U16 = new Element(2);
35 public static final Element USER_I16 = new Element(3);
36 public static final Element USER_U32 = new Element(4);
37 public static final Element USER_I32 = new Element(5);
38 public static final Element USER_FLOAT = new Element(6);
39
40 public static final Element A_8 = new Element(7);
41 public static final Element RGB_565 = new Element(8);
42 public static final Element RGB_888 = new Element(11);
43 public static final Element RGBA_5551 = new Element(9);
44 public static final Element RGBA_4444 = new Element(10);
45 public static final Element RGBA_8888 = new Element(12);
46
47 public static final Element INDEX_16 = new Element(13);
48 public static final Element INDEX_32 = new Element(14);
49 public static final Element XY_F32 = new Element(15);
50 public static final Element XYZ_F32 = new Element(16);
51 public static final Element ST_XY_F32 = new Element(17);
52 public static final Element ST_XYZ_F32 = new Element(18);
53 public static final Element NORM_XYZ_F32 = new Element(19);
54 public static final Element NORM_ST_XYZ_F32 = new Element(20);
55
56 void initPredef(RenderScript rs) {
57 mID = rs.nElementGetPredefined(mPredefinedID);
58 }
59
60 static void init(RenderScript rs) {
61 USER_U8.initPredef(rs);
62 USER_I8.initPredef(rs);
63 USER_U16.initPredef(rs);
64 USER_I16.initPredef(rs);
65 USER_U32.initPredef(rs);
66 USER_I32.initPredef(rs);
67 USER_FLOAT.initPredef(rs);
68
69 A_8.initPredef(rs);
70 RGB_565.initPredef(rs);
71 RGB_888.initPredef(rs);
72 RGBA_5551.initPredef(rs);
73 RGBA_4444.initPredef(rs);
74 RGBA_8888.initPredef(rs);
75
76 INDEX_16.initPredef(rs);
77 INDEX_32.initPredef(rs);
78 XY_F32.initPredef(rs);
79 XYZ_F32.initPredef(rs);
80 ST_XY_F32.initPredef(rs);
81 ST_XYZ_F32.initPredef(rs);
82 NORM_XYZ_F32.initPredef(rs);
83 NORM_ST_XYZ_F32.initPredef(rs);
84 }
85
86
87 public enum DataType {
88 FLOAT (0),
89 UNSIGNED (1),
90 SIGNED (2);
91
92 int mID;
93 DataType(int id) {
94 mID = id;
95 }
96 }
97
98 public enum DataKind {
99 USER (0),
100 RED (1),
101 GREEN (2),
102 BLUE (3),
103 ALPHA (4),
104 LUMINANCE (5),
105 INTENSITY (6),
106 X (7),
107 Y (8),
108 Z (9),
109 W (10),
110 S (11),
111 T (12),
112 Q (13),
113 R (14),
114 NX (15),
115 NY (16),
116 NZ (17),
117 INDEX (18);
118
119 int mID;
120 DataKind(int id) {
121 mID = id;
122 }
123 }
124
125
126 Element(int predef) {
127 super(null);
128 mID = 0;
129 mPredefinedID = predef;
130 mIsPredefined = true;
131 }
132
133 Element(int id, RenderScript rs) {
134 super(rs);
135 mID = id;
136 mPredefinedID = 0;
137 mIsPredefined = false;
138 }
139
140 public void destroy() throws IllegalStateException {
141 if(mIsPredefined) {
142 throw new IllegalStateException("Attempting to destroy a predefined Element.");
143 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700144 if(mDestroyed) {
145 throw new IllegalStateException("Object already destroyed.");
146 }
147 mDestroyed = true;
Jason Sams36e612a2009-07-31 16:26:13 -0700148 mRS.nElementDestroy(mID);
Jason Sams36e612a2009-07-31 16:26:13 -0700149 }
150
Jason Sams43ee06852009-08-12 17:54:11 -0700151 public static Element createFromClass(RenderScript rs, Class c) {
152 Field[] fields = c.getFields();
153 Builder b = new Builder(rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700154
Jason Sams43ee06852009-08-12 17:54:11 -0700155 for(Field f: fields) {
156 Class fc = f.getType();
157 if(fc == int.class) {
158 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName());
159 } else if(fc == short.class) {
160 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName());
161 } else if(fc == byte.class) {
162 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName());
163 } else if(fc == float.class) {
164 b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName());
165 } else {
166 throw new IllegalArgumentException("Unkown field type");
167 }
168 }
169 return b.create();
170 }
Jason Sams36e612a2009-07-31 16:26:13 -0700171
172
173 public static class Builder {
174 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700175 Entry[] mEntries;
176 int mEntryCount;
Jason Sams36e612a2009-07-31 16:26:13 -0700177
Jason Sams22534172009-08-04 16:58:20 -0700178 private class Entry {
179 Element mElement;
180 Element.DataType mType;
181 Element.DataKind mKind;
182 boolean mIsNormalized;
183 int mBits;
Jason Sams43ee06852009-08-12 17:54:11 -0700184 String mName;
Jason Sams22534172009-08-04 16:58:20 -0700185 }
186
187 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700188 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700189 mEntryCount = 0;
190 mEntries = new Entry[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700191 }
192
Jason Sams22534172009-08-04 16:58:20 -0700193 void addEntry(Entry e) {
194 if(mEntries.length >= mEntryCount) {
195 Entry[] en = new Entry[mEntryCount + 8];
196 for(int ct=0; ct < mEntries.length; ct++) {
197 en[ct] = mEntries[ct];
198 }
199 mEntries = en;
Jason Sams36e612a2009-07-31 16:26:13 -0700200 }
Jason Sams22534172009-08-04 16:58:20 -0700201 mEntries[mEntryCount] = e;
202 mEntryCount++;
Jason Sams36e612a2009-07-31 16:26:13 -0700203 }
204
Jason Sams22534172009-08-04 16:58:20 -0700205 public Builder add(Element e) throws IllegalArgumentException {
Jason Sams36e612a2009-07-31 16:26:13 -0700206 if(!e.mIsPredefined) {
207 throw new IllegalArgumentException("add requires a predefined Element.");
208 }
Jason Sams22534172009-08-04 16:58:20 -0700209 Entry en = new Entry();
210 en.mElement = e;
211 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700212 return this;
213 }
214
Jason Sams43ee06852009-08-12 17:54:11 -0700215 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
Jason Sams22534172009-08-04 16:58:20 -0700216 Entry en = new Entry();
217 en.mType = dt;
218 en.mKind = dk;
219 en.mIsNormalized = isNormalized;
220 en.mBits = bits;
Jason Sams43ee06852009-08-12 17:54:11 -0700221 en.mName = name;
Jason Sams22534172009-08-04 16:58:20 -0700222 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700223 return this;
224 }
225
Jason Sams43ee06852009-08-12 17:54:11 -0700226 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
227 add(dt, dk, isNormalized, bits, null);
228 return this;
229 }
230
Jason Sams22534172009-08-04 16:58:20 -0700231 static synchronized Element internalCreate(RenderScript rs, Builder b) {
232 rs.nElementBegin();
233 for (int ct=0; ct < b.mEntryCount; ct++) {
234 Entry en = b.mEntries[ct];
235 if(en.mElement != null) {
236 rs.nElementAddPredefined(en.mElement.mPredefinedID);
237 } else {
238 int norm = 0;
239 if (en.mIsNormalized) {
240 norm = 1;
241 }
Jason Sams43ee06852009-08-12 17:54:11 -0700242 rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName);
Jason Sams22534172009-08-04 16:58:20 -0700243 }
Jason Sams36e612a2009-07-31 16:26:13 -0700244 }
Jason Sams22534172009-08-04 16:58:20 -0700245 int id = rs.nElementCreate();
246 return new Element(id, rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700247 }
248
Jason Sams22534172009-08-04 16:58:20 -0700249 public Element create() {
250 return internalCreate(mRS, this);
Jason Sams36e612a2009-07-31 16:26:13 -0700251 }
252 }
253
254}
255