blob: 308d66347ac9ec51866e071ba50484fea6133c6f [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
Jason Samsf110d4b2010-06-21 17:42:41 -070050 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080051
Jason Samsf110d4b2010-06-21 17:42:41 -070052 UNSIGNED_5_6_5 (13, 2),
53 UNSIGNED_5_5_5_1 (14, 2),
54 UNSIGNED_4_4_4_4 (15, 2),
55
56 RS_ELEMENT (16, 4),
57 RS_TYPE (17, 4),
58 RS_ALLOCATION (18, 4),
59 RS_SAMPLER (19, 4),
60 RS_SCRIPT (20, 4),
61 RS_MESH (21, 4),
62 RS_PROGRAM_FRAGMENT (22, 4),
63 RS_PROGRAM_VERTEX (23, 4),
64 RS_PROGRAM_RASTER (24, 4),
65 RS_PROGRAM_STORE (25, 4);
Jason Sams36e612a2009-07-31 16:26:13 -070066
67 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -080068 int mSize;
69 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -070070 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -080071 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -070072 }
73 }
74
75 public enum DataKind {
76 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -080077
78 PIXEL_L (7),
79 PIXEL_A (8),
80 PIXEL_LA (9),
81 PIXEL_RGB (10),
82 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -070083
84 int mID;
85 DataKind(int id) {
86 mID = id;
87 }
88 }
89
Jason Samsf110d4b2010-06-21 17:42:41 -070090 public static Element BOOLEAN(RenderScript rs) {
91 if(rs.mElement_BOOLEAN == null) {
92 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
93 }
94 return rs.mElement_BOOLEAN;
95 }
96
Jason Sams8cb39de2010-06-01 15:47:01 -070097 public static Element U8(RenderScript rs) {
98 if(rs.mElement_U8 == null) {
99 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800100 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700101 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800102 }
103
Jason Sams8cb39de2010-06-01 15:47:01 -0700104 public static Element I8(RenderScript rs) {
105 if(rs.mElement_I8 == null) {
106 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800107 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700108 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800109 }
110
Jason Samse29f3e72010-06-08 15:40:48 -0700111 public static Element U16(RenderScript rs) {
112 if(rs.mElement_U16 == null) {
113 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
114 }
115 return rs.mElement_U16;
116 }
117
118 public static Element I16(RenderScript rs) {
119 if(rs.mElement_I16 == null) {
120 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
121 }
122 return rs.mElement_I16;
123 }
124
Jason Sams8cb39de2010-06-01 15:47:01 -0700125 public static Element U32(RenderScript rs) {
126 if(rs.mElement_U32 == null) {
127 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800128 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700129 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800130 }
131
Jason Sams8cb39de2010-06-01 15:47:01 -0700132 public static Element I32(RenderScript rs) {
133 if(rs.mElement_I32 == null) {
134 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800135 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700136 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800137 }
138
Jason Sams8cb39de2010-06-01 15:47:01 -0700139 public static Element F32(RenderScript rs) {
140 if(rs.mElement_F32 == null) {
141 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800142 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700143 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800144 }
145
Jason Sams8cb39de2010-06-01 15:47:01 -0700146 public static Element ELEMENT(RenderScript rs) {
147 if(rs.mElement_ELEMENT == null) {
148 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700149 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700150 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700151 }
152
Jason Sams8cb39de2010-06-01 15:47:01 -0700153 public static Element TYPE(RenderScript rs) {
154 if(rs.mElement_TYPE == null) {
155 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700156 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700157 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700158 }
159
Jason Sams8cb39de2010-06-01 15:47:01 -0700160 public static Element ALLOCATION(RenderScript rs) {
161 if(rs.mElement_ALLOCATION == null) {
162 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700163 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700164 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700165 }
166
Jason Sams8cb39de2010-06-01 15:47:01 -0700167 public static Element SAMPLER(RenderScript rs) {
168 if(rs.mElement_SAMPLER == null) {
169 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700170 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700171 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700172 }
173
Jason Sams8cb39de2010-06-01 15:47:01 -0700174 public static Element SCRIPT(RenderScript rs) {
175 if(rs.mElement_SCRIPT == null) {
176 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700177 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700178 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700179 }
180
Jason Sams8cb39de2010-06-01 15:47:01 -0700181 public static Element MESH(RenderScript rs) {
182 if(rs.mElement_MESH == null) {
183 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700184 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700185 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700186 }
187
Jason Sams8cb39de2010-06-01 15:47:01 -0700188 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
189 if(rs.mElement_PROGRAM_FRAGMENT == null) {
190 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700191 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700192 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700193 }
194
Jason Sams8cb39de2010-06-01 15:47:01 -0700195 public static Element PROGRAM_VERTEX(RenderScript rs) {
196 if(rs.mElement_PROGRAM_VERTEX == null) {
197 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700198 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700199 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700200 }
201
Jason Sams8cb39de2010-06-01 15:47:01 -0700202 public static Element PROGRAM_RASTER(RenderScript rs) {
203 if(rs.mElement_PROGRAM_RASTER == null) {
204 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700205 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700206 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700207 }
208
Jason Sams8cb39de2010-06-01 15:47:01 -0700209 public static Element PROGRAM_STORE(RenderScript rs) {
210 if(rs.mElement_PROGRAM_STORE == null) {
211 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700212 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700213 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700214 }
215
216
Jason Sams718cd1f2009-12-23 14:35:29 -0800217 public static Element A_8(RenderScript rs) {
218 if(rs.mElement_A_8 == null) {
219 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
220 }
221 return rs.mElement_A_8;
222 }
223
224 public static Element RGB_565(RenderScript rs) {
225 if(rs.mElement_RGB_565 == null) {
226 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
227 }
228 return rs.mElement_RGB_565;
229 }
230
231 public static Element RGB_888(RenderScript rs) {
232 if(rs.mElement_RGB_888 == null) {
233 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
234 }
235 return rs.mElement_RGB_888;
236 }
237
238 public static Element RGBA_5551(RenderScript rs) {
239 if(rs.mElement_RGBA_5551 == null) {
240 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
241 }
242 return rs.mElement_RGBA_5551;
243 }
244
245 public static Element RGBA_4444(RenderScript rs) {
246 if(rs.mElement_RGBA_4444 == null) {
247 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
248 }
249 return rs.mElement_RGBA_4444;
250 }
251
252 public static Element RGBA_8888(RenderScript rs) {
253 if(rs.mElement_RGBA_8888 == null) {
254 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
255 }
256 return rs.mElement_RGBA_8888;
257 }
258
Jason Sams8cb39de2010-06-01 15:47:01 -0700259 public static Element F32_2(RenderScript rs) {
260 if(rs.mElement_FLOAT_2 == null) {
261 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800262 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700263 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800264 }
265
Jason Sams8cb39de2010-06-01 15:47:01 -0700266 public static Element F32_3(RenderScript rs) {
267 if(rs.mElement_FLOAT_3 == null) {
268 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800269 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700270 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800271 }
272
Jason Sams8cb39de2010-06-01 15:47:01 -0700273 public static Element F32_4(RenderScript rs) {
274 if(rs.mElement_FLOAT_4 == null) {
275 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800276 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700277 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800278 }
279
Jason Sams8cb39de2010-06-01 15:47:01 -0700280 public static Element U8_4(RenderScript rs) {
281 if(rs.mElement_UCHAR_4 == null) {
282 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800283 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700284 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800285 }
286
Jason Sams718cd1f2009-12-23 14:35:29 -0800287
288 Element(RenderScript rs, Element[] e, String[] n) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700289 super(rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700290 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800291 mElements = e;
292 mElementNames = n;
293 int[] ids = new int[mElements.length];
294 for (int ct = 0; ct < mElements.length; ct++ ) {
295 mSize += mElements[ct].mSize;
296 ids[ct] = mElements[ct].mID;
297 }
298 mID = rs.nElementCreate2(ids, mElementNames);
299 }
300
301 Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
302 super(rs);
303 mSize = dt.mSize * size;
304 mType = dt;
305 mKind = dk;
306 mNormalized = norm;
307 mVectorSize = size;
308 mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
Jason Sams36e612a2009-07-31 16:26:13 -0700309 }
310
311 public void destroy() throws IllegalStateException {
Jason Sams7ce033d2009-08-18 14:14:24 -0700312 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700313 }
314
Jason Sams718cd1f2009-12-23 14:35:29 -0800315 /////////////////////////////////////////
316 public static Element createUser(RenderScript rs, DataType dt) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800317 return new Element(rs, dt, DataKind.USER, false, 1);
318 }
319
320 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800321 if (size < 2 || size > 4) {
322 throw new IllegalArgumentException("Bad size");
Jason Samsea84a7c2009-09-04 14:42:41 -0700323 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800324 return new Element(rs, dt, DataKind.USER, false, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700325 }
326
Jason Sams718cd1f2009-12-23 14:35:29 -0800327 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800328 if (!(dk == DataKind.PIXEL_L ||
329 dk == DataKind.PIXEL_A ||
330 dk == DataKind.PIXEL_LA ||
331 dk == DataKind.PIXEL_RGB ||
332 dk == DataKind.PIXEL_RGBA)) {
333 throw new IllegalArgumentException("Unsupported DataKind");
334 }
335 if (!(dt == DataType.UNSIGNED_8 ||
336 dt == DataType.UNSIGNED_5_6_5 ||
337 dt == DataType.UNSIGNED_4_4_4_4 ||
338 dt == DataType.UNSIGNED_5_5_5_1)) {
339 throw new IllegalArgumentException("Unsupported DataType");
340 }
341 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
342 throw new IllegalArgumentException("Bad kind and type combo");
343 }
344 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
345 throw new IllegalArgumentException("Bad kind and type combo");
346 }
347 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
348 throw new IllegalArgumentException("Bad kind and type combo");
349 }
350
351 int size = 1;
352 if (dk == DataKind.PIXEL_LA) {
353 size = 2;
354 }
355 if (dk == DataKind.PIXEL_RGB) {
356 size = 3;
357 }
358 if (dk == DataKind.PIXEL_RGBA) {
359 size = 4;
360 }
361
362 return new Element(rs, dt, dk, true, size);
363 }
Jason Sams36e612a2009-07-31 16:26:13 -0700364
365 public static class Builder {
366 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800367 Element[] mElements;
368 String[] mElementNames;
369 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700370
371 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700372 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800373 mCount = 0;
374 mElements = new Element[8];
375 mElementNames = new String[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700376 }
377
Jason Sams718cd1f2009-12-23 14:35:29 -0800378 public void add(Element element, String name) {
379 if(mCount == mElements.length) {
380 Element[] e = new Element[mCount + 8];
381 String[] s = new String[mCount + 8];
382 System.arraycopy(mElements, 0, e, 0, mCount);
383 System.arraycopy(mElementNames, 0, s, 0, mCount);
384 mElements = e;
385 mElementNames = s;
Jason Sams36e612a2009-07-31 16:26:13 -0700386 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800387 mElements[mCount] = element;
388 mElementNames[mCount] = name;
389 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700390 }
391
Jason Sams22534172009-08-04 16:58:20 -0700392 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800393 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800394 Element[] ein = new Element[mCount];
395 String[] sin = new String[mCount];
396 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
397 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
398 return new Element(mRS, ein, sin);
Jason Sams36e612a2009-07-31 16:26:13 -0700399 }
400 }
401
Jason Sams718cd1f2009-12-23 14:35:29 -0800402 static void initPredefined(RenderScript rs) {
403 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
404 DataKind.PIXEL_A.mID, true, 1);
405 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
406 DataKind.PIXEL_RGBA.mID, true, 4);
407 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
408 DataKind.PIXEL_RGBA.mID, true, 4);
409 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
410 DataKind.PIXEL_RGB.mID, true, 3);
411 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
412 }
Jason Sams36e612a2009-07-31 16:26:13 -0700413}
414