blob: 22291e61c6e577ca3166fd1c42c082a3bbc97773 [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 {
Jason Samsea84a7c2009-09-04 14:42:41 -070026 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080027 Element[] mElements;
28 String[] mElementNames;
Jason Sams36e612a2009-07-31 16:26:13 -070029
Jason Sams718cd1f2009-12-23 14:35:29 -080030 DataType mType;
31 DataKind mKind;
32 boolean mNormalized;
33 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070034
Jason Sams718cd1f2009-12-23 14:35:29 -080035 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070036
37 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080038 //FLOAT_16 (1, 2),
39 FLOAT_32 (2, 4),
40 //FLOAT_64 (3, 8),
41 SIGNED_8 (4, 1),
42 SIGNED_16 (5, 2),
43 SIGNED_32 (6, 4),
44 //SIGNED_64 (7, 8),
45 UNSIGNED_8 (8, 1),
46 UNSIGNED_16 (9, 2),
47 UNSIGNED_32 (10, 4),
48 //UNSIGNED_64 (11, 8),
49
50 UNSIGNED_5_6_5 (12, 2),
51 UNSIGNED_5_5_5_1 (13, 2),
52 UNSIGNED_4_4_4_4 (14, 2),
53
54 RS_ELEMENT (15, 4),
55 RS_TYPE (16, 4),
56 RS_ALLOCATION (17, 4),
57 RS_SAMPLER (18, 4),
58 RS_SCRIPT (19, 4),
59 RS_MESH (20, 4),
60 RS_PROGRAM_FRAGMENT (21, 4),
61 RS_PROGRAM_VERTEX (22, 4),
62 RS_PROGRAM_RASTER (23, 4),
63 RS_PROGRAM_STORE (24, 4);
Jason Sams36e612a2009-07-31 16:26:13 -070064
65 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -080066 int mSize;
67 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -070068 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -080069 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -070070 }
71 }
72
73 public enum DataKind {
74 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -080075
76 PIXEL_L (7),
77 PIXEL_A (8),
78 PIXEL_LA (9),
79 PIXEL_RGB (10),
80 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -070081
82 int mID;
83 DataKind(int id) {
84 mID = id;
85 }
86 }
87
Jason Sams8cb39de2010-06-01 15:47:01 -070088 public static Element U8(RenderScript rs) {
89 if(rs.mElement_U8 == null) {
90 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -080091 }
Jason Sams8cb39de2010-06-01 15:47:01 -070092 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -080093 }
94
Jason Sams8cb39de2010-06-01 15:47:01 -070095 public static Element I8(RenderScript rs) {
96 if(rs.mElement_I8 == null) {
97 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -080098 }
Jason Sams8cb39de2010-06-01 15:47:01 -070099 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800100 }
101
Jason Samse29f3e72010-06-08 15:40:48 -0700102 public static Element U16(RenderScript rs) {
103 if(rs.mElement_U16 == null) {
104 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
105 }
106 return rs.mElement_U16;
107 }
108
109 public static Element I16(RenderScript rs) {
110 if(rs.mElement_I16 == null) {
111 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
112 }
113 return rs.mElement_I16;
114 }
115
Jason Sams8cb39de2010-06-01 15:47:01 -0700116 public static Element U32(RenderScript rs) {
117 if(rs.mElement_U32 == null) {
118 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800119 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700120 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800121 }
122
Jason Sams8cb39de2010-06-01 15:47:01 -0700123 public static Element I32(RenderScript rs) {
124 if(rs.mElement_I32 == null) {
125 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800126 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700127 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800128 }
129
Jason Sams8cb39de2010-06-01 15:47:01 -0700130 public static Element F32(RenderScript rs) {
131 if(rs.mElement_F32 == null) {
132 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800133 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700134 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800135 }
136
Jason Sams8cb39de2010-06-01 15:47:01 -0700137 public static Element ELEMENT(RenderScript rs) {
138 if(rs.mElement_ELEMENT == null) {
139 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700140 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700141 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700142 }
143
Jason Sams8cb39de2010-06-01 15:47:01 -0700144 public static Element TYPE(RenderScript rs) {
145 if(rs.mElement_TYPE == null) {
146 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700147 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700148 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700149 }
150
Jason Sams8cb39de2010-06-01 15:47:01 -0700151 public static Element ALLOCATION(RenderScript rs) {
152 if(rs.mElement_ALLOCATION == null) {
153 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700154 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700155 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700156 }
157
Jason Sams8cb39de2010-06-01 15:47:01 -0700158 public static Element SAMPLER(RenderScript rs) {
159 if(rs.mElement_SAMPLER == null) {
160 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700161 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700162 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700163 }
164
Jason Sams8cb39de2010-06-01 15:47:01 -0700165 public static Element SCRIPT(RenderScript rs) {
166 if(rs.mElement_SCRIPT == null) {
167 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700168 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700169 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700170 }
171
Jason Sams8cb39de2010-06-01 15:47:01 -0700172 public static Element MESH(RenderScript rs) {
173 if(rs.mElement_MESH == null) {
174 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700175 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700176 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700177 }
178
Jason Sams8cb39de2010-06-01 15:47:01 -0700179 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
180 if(rs.mElement_PROGRAM_FRAGMENT == null) {
181 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700182 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700183 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700184 }
185
Jason Sams8cb39de2010-06-01 15:47:01 -0700186 public static Element PROGRAM_VERTEX(RenderScript rs) {
187 if(rs.mElement_PROGRAM_VERTEX == null) {
188 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700189 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700190 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700191 }
192
Jason Sams8cb39de2010-06-01 15:47:01 -0700193 public static Element PROGRAM_RASTER(RenderScript rs) {
194 if(rs.mElement_PROGRAM_RASTER == null) {
195 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700196 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700197 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700198 }
199
Jason Sams8cb39de2010-06-01 15:47:01 -0700200 public static Element PROGRAM_STORE(RenderScript rs) {
201 if(rs.mElement_PROGRAM_STORE == null) {
202 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700203 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700204 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700205 }
206
207
Jason Sams718cd1f2009-12-23 14:35:29 -0800208 public static Element A_8(RenderScript rs) {
209 if(rs.mElement_A_8 == null) {
210 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
211 }
212 return rs.mElement_A_8;
213 }
214
215 public static Element RGB_565(RenderScript rs) {
216 if(rs.mElement_RGB_565 == null) {
217 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
218 }
219 return rs.mElement_RGB_565;
220 }
221
222 public static Element RGB_888(RenderScript rs) {
223 if(rs.mElement_RGB_888 == null) {
224 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
225 }
226 return rs.mElement_RGB_888;
227 }
228
229 public static Element RGBA_5551(RenderScript rs) {
230 if(rs.mElement_RGBA_5551 == null) {
231 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
232 }
233 return rs.mElement_RGBA_5551;
234 }
235
236 public static Element RGBA_4444(RenderScript rs) {
237 if(rs.mElement_RGBA_4444 == null) {
238 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
239 }
240 return rs.mElement_RGBA_4444;
241 }
242
243 public static Element RGBA_8888(RenderScript rs) {
244 if(rs.mElement_RGBA_8888 == null) {
245 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
246 }
247 return rs.mElement_RGBA_8888;
248 }
249
Jason Sams8cb39de2010-06-01 15:47:01 -0700250 public static Element F32_2(RenderScript rs) {
251 if(rs.mElement_FLOAT_2 == null) {
252 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800253 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700254 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800255 }
256
Jason Sams8cb39de2010-06-01 15:47:01 -0700257 public static Element F32_3(RenderScript rs) {
258 if(rs.mElement_FLOAT_3 == null) {
259 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800260 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700261 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800262 }
263
Jason Sams8cb39de2010-06-01 15:47:01 -0700264 public static Element F32_4(RenderScript rs) {
265 if(rs.mElement_FLOAT_4 == null) {
266 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800267 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700268 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800269 }
270
Jason Sams8cb39de2010-06-01 15:47:01 -0700271 public static Element U8_4(RenderScript rs) {
272 if(rs.mElement_UCHAR_4 == null) {
273 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800274 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700275 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800276 }
277
Jason Sams718cd1f2009-12-23 14:35:29 -0800278
279 Element(RenderScript rs, Element[] e, String[] n) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700280 super(rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700281 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800282 mElements = e;
283 mElementNames = n;
284 int[] ids = new int[mElements.length];
285 for (int ct = 0; ct < mElements.length; ct++ ) {
286 mSize += mElements[ct].mSize;
287 ids[ct] = mElements[ct].mID;
288 }
289 mID = rs.nElementCreate2(ids, mElementNames);
290 }
291
292 Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
293 super(rs);
294 mSize = dt.mSize * size;
295 mType = dt;
296 mKind = dk;
297 mNormalized = norm;
298 mVectorSize = size;
299 mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Jason Sams36e612a2009-07-31 16:26:13 -0700300 }
301
302 public void destroy() throws IllegalStateException {
Jason Sams7ce033d2009-08-18 14:14:24 -0700303 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700304 }
305
Jason Sams718cd1f2009-12-23 14:35:29 -0800306 /////////////////////////////////////////
307 public static Element createUser(RenderScript rs, DataType dt) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800308 return new Element(rs, dt, DataKind.USER, false, 1);
309 }
310
311 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800312 if (size < 2 || size > 4) {
313 throw new IllegalArgumentException("Bad size");
Jason Samsea84a7c2009-09-04 14:42:41 -0700314 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800315 return new Element(rs, dt, DataKind.USER, false, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700316 }
317
Jason Sams718cd1f2009-12-23 14:35:29 -0800318 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800319 if (!(dk == DataKind.PIXEL_L ||
320 dk == DataKind.PIXEL_A ||
321 dk == DataKind.PIXEL_LA ||
322 dk == DataKind.PIXEL_RGB ||
323 dk == DataKind.PIXEL_RGBA)) {
324 throw new IllegalArgumentException("Unsupported DataKind");
325 }
326 if (!(dt == DataType.UNSIGNED_8 ||
327 dt == DataType.UNSIGNED_5_6_5 ||
328 dt == DataType.UNSIGNED_4_4_4_4 ||
329 dt == DataType.UNSIGNED_5_5_5_1)) {
330 throw new IllegalArgumentException("Unsupported DataType");
331 }
332 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
333 throw new IllegalArgumentException("Bad kind and type combo");
334 }
335 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
336 throw new IllegalArgumentException("Bad kind and type combo");
337 }
338 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
339 throw new IllegalArgumentException("Bad kind and type combo");
340 }
341
342 int size = 1;
343 if (dk == DataKind.PIXEL_LA) {
344 size = 2;
345 }
346 if (dk == DataKind.PIXEL_RGB) {
347 size = 3;
348 }
349 if (dk == DataKind.PIXEL_RGBA) {
350 size = 4;
351 }
352
353 return new Element(rs, dt, dk, true, size);
354 }
Jason Sams36e612a2009-07-31 16:26:13 -0700355
356 public static class Builder {
357 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800358 Element[] mElements;
359 String[] mElementNames;
360 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700361
362 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700363 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800364 mCount = 0;
365 mElements = new Element[8];
366 mElementNames = new String[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700367 }
368
Jason Sams718cd1f2009-12-23 14:35:29 -0800369 public void add(Element element, String name) {
370 if(mCount == mElements.length) {
371 Element[] e = new Element[mCount + 8];
372 String[] s = new String[mCount + 8];
373 System.arraycopy(mElements, 0, e, 0, mCount);
374 System.arraycopy(mElementNames, 0, s, 0, mCount);
375 mElements = e;
376 mElementNames = s;
Jason Sams36e612a2009-07-31 16:26:13 -0700377 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800378 mElements[mCount] = element;
379 mElementNames[mCount] = name;
380 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700381 }
382
Jason Sams22534172009-08-04 16:58:20 -0700383 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800384 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800385 Element[] ein = new Element[mCount];
386 String[] sin = new String[mCount];
387 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
388 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
389 return new Element(mRS, ein, sin);
Jason Sams36e612a2009-07-31 16:26:13 -0700390 }
391 }
392
Jason Sams718cd1f2009-12-23 14:35:29 -0800393 static void initPredefined(RenderScript rs) {
394 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
395 DataKind.PIXEL_A.mID, true, 1);
396 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
397 DataKind.PIXEL_RGBA.mID, true, 4);
398 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
399 DataKind.PIXEL_RGBA.mID, true, 4);
400 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
401 DataKind.PIXEL_RGB.mID, true, 3);
402 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
403 }
Jason Sams36e612a2009-07-31 16:26:13 -0700404}
405