blob: 91824e63faadd6da504690a487d24301583eb66d [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;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070020import android.util.Log;
Jason Sams36e612a2009-07-31 16:26:13 -070021
22/**
23 * @hide
24 *
25 **/
26public class Element extends BaseObj {
Jason Samsea84a7c2009-09-04 14:42:41 -070027 int mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -080028 Element[] mElements;
29 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -070030 int[] mArraySizes;
Jason Sams36e612a2009-07-31 16:26:13 -070031
Jason Sams718cd1f2009-12-23 14:35:29 -080032 DataType mType;
33 DataKind mKind;
34 boolean mNormalized;
35 int mVectorSize;
Jason Sams768bc022009-09-21 19:41:04 -070036
Jason Sams718cd1f2009-12-23 14:35:29 -080037 int getSizeBytes() {return mSize;}
Jason Sams36e612a2009-07-31 16:26:13 -070038
39 public enum DataType {
Jason Sams718cd1f2009-12-23 14:35:29 -080040 //FLOAT_16 (1, 2),
41 FLOAT_32 (2, 4),
Stephen Hines02f417052010-09-30 15:19:22 -070042 FLOAT_64 (3, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080043 SIGNED_8 (4, 1),
44 SIGNED_16 (5, 2),
45 SIGNED_32 (6, 4),
Stephen Hinesef1dac22010-10-01 15:39:33 -070046 SIGNED_64 (7, 8),
Jason Sams718cd1f2009-12-23 14:35:29 -080047 UNSIGNED_8 (8, 1),
48 UNSIGNED_16 (9, 2),
49 UNSIGNED_32 (10, 4),
50 //UNSIGNED_64 (11, 8),
51
Jason Samsf110d4b2010-06-21 17:42:41 -070052 BOOLEAN(12, 1),
Jason Sams718cd1f2009-12-23 14:35:29 -080053
Jason Samsf110d4b2010-06-21 17:42:41 -070054 UNSIGNED_5_6_5 (13, 2),
55 UNSIGNED_5_5_5_1 (14, 2),
56 UNSIGNED_4_4_4_4 (15, 2),
57
Jason Sams1d45c472010-08-25 14:31:48 -070058 MATRIX_4X4 (16, 64),
59 MATRIX_3X3 (17, 36),
60 MATRIX_2X2 (18, 16),
61
62 RS_ELEMENT (1000, 4),
63 RS_TYPE (1001, 4),
64 RS_ALLOCATION (1002, 4),
65 RS_SAMPLER (1003, 4),
66 RS_SCRIPT (1004, 4),
67 RS_MESH (1005, 4),
68 RS_PROGRAM_FRAGMENT (1006, 4),
69 RS_PROGRAM_VERTEX (1007, 4),
70 RS_PROGRAM_RASTER (1008, 4),
71 RS_PROGRAM_STORE (1009, 4);
Jason Sams36e612a2009-07-31 16:26:13 -070072
73 int mID;
Jason Sams718cd1f2009-12-23 14:35:29 -080074 int mSize;
75 DataType(int id, int size) {
Jason Sams36e612a2009-07-31 16:26:13 -070076 mID = id;
Jason Sams718cd1f2009-12-23 14:35:29 -080077 mSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -070078 }
79 }
80
81 public enum DataKind {
82 USER (0),
Jason Sams718cd1f2009-12-23 14:35:29 -080083
84 PIXEL_L (7),
85 PIXEL_A (8),
86 PIXEL_LA (9),
87 PIXEL_RGB (10),
88 PIXEL_RGBA (11);
Jason Sams36e612a2009-07-31 16:26:13 -070089
90 int mID;
91 DataKind(int id) {
92 mID = id;
93 }
94 }
95
Jason Samsf110d4b2010-06-21 17:42:41 -070096 public static Element BOOLEAN(RenderScript rs) {
97 if(rs.mElement_BOOLEAN == null) {
98 rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
99 }
100 return rs.mElement_BOOLEAN;
101 }
102
Jason Sams8cb39de2010-06-01 15:47:01 -0700103 public static Element U8(RenderScript rs) {
104 if(rs.mElement_U8 == null) {
105 rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800106 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700107 return rs.mElement_U8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800108 }
109
Jason Sams8cb39de2010-06-01 15:47:01 -0700110 public static Element I8(RenderScript rs) {
111 if(rs.mElement_I8 == null) {
112 rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
Jason Sams718cd1f2009-12-23 14:35:29 -0800113 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700114 return rs.mElement_I8;
Jason Sams718cd1f2009-12-23 14:35:29 -0800115 }
116
Jason Samse29f3e72010-06-08 15:40:48 -0700117 public static Element U16(RenderScript rs) {
118 if(rs.mElement_U16 == null) {
119 rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
120 }
121 return rs.mElement_U16;
122 }
123
124 public static Element I16(RenderScript rs) {
125 if(rs.mElement_I16 == null) {
126 rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
127 }
128 return rs.mElement_I16;
129 }
130
Jason Sams8cb39de2010-06-01 15:47:01 -0700131 public static Element U32(RenderScript rs) {
132 if(rs.mElement_U32 == null) {
133 rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800134 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700135 return rs.mElement_U32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800136 }
137
Jason Sams8cb39de2010-06-01 15:47:01 -0700138 public static Element I32(RenderScript rs) {
139 if(rs.mElement_I32 == null) {
140 rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800141 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700142 return rs.mElement_I32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800143 }
144
Stephen Hinesef1dac22010-10-01 15:39:33 -0700145 public static Element I64(RenderScript rs) {
146 if(rs.mElement_I64 == null) {
147 rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
148 }
149 return rs.mElement_I64;
150 }
151
Jason Sams8cb39de2010-06-01 15:47:01 -0700152 public static Element F32(RenderScript rs) {
153 if(rs.mElement_F32 == null) {
154 rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
Jason Sams718cd1f2009-12-23 14:35:29 -0800155 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700156 return rs.mElement_F32;
Jason Sams718cd1f2009-12-23 14:35:29 -0800157 }
158
Stephen Hines02f417052010-09-30 15:19:22 -0700159 public static Element F64(RenderScript rs) {
160 if(rs.mElement_F64 == null) {
161 rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
162 }
163 return rs.mElement_F64;
164 }
165
Jason Sams8cb39de2010-06-01 15:47:01 -0700166 public static Element ELEMENT(RenderScript rs) {
167 if(rs.mElement_ELEMENT == null) {
168 rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700169 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700170 return rs.mElement_ELEMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700171 }
172
Jason Sams8cb39de2010-06-01 15:47:01 -0700173 public static Element TYPE(RenderScript rs) {
174 if(rs.mElement_TYPE == null) {
175 rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
Jason Samsa70f4162010-03-26 15:33:42 -0700176 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700177 return rs.mElement_TYPE;
Jason Samsa70f4162010-03-26 15:33:42 -0700178 }
179
Jason Sams8cb39de2010-06-01 15:47:01 -0700180 public static Element ALLOCATION(RenderScript rs) {
181 if(rs.mElement_ALLOCATION == null) {
182 rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
Jason Samsa70f4162010-03-26 15:33:42 -0700183 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700184 return rs.mElement_ALLOCATION;
Jason Samsa70f4162010-03-26 15:33:42 -0700185 }
186
Jason Sams8cb39de2010-06-01 15:47:01 -0700187 public static Element SAMPLER(RenderScript rs) {
188 if(rs.mElement_SAMPLER == null) {
189 rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
Jason Samsa70f4162010-03-26 15:33:42 -0700190 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700191 return rs.mElement_SAMPLER;
Jason Samsa70f4162010-03-26 15:33:42 -0700192 }
193
Jason Sams8cb39de2010-06-01 15:47:01 -0700194 public static Element SCRIPT(RenderScript rs) {
195 if(rs.mElement_SCRIPT == null) {
196 rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
Jason Samsa70f4162010-03-26 15:33:42 -0700197 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700198 return rs.mElement_SCRIPT;
Jason Samsa70f4162010-03-26 15:33:42 -0700199 }
200
Jason Sams8cb39de2010-06-01 15:47:01 -0700201 public static Element MESH(RenderScript rs) {
202 if(rs.mElement_MESH == null) {
203 rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
Jason Samsa70f4162010-03-26 15:33:42 -0700204 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700205 return rs.mElement_MESH;
Jason Samsa70f4162010-03-26 15:33:42 -0700206 }
207
Jason Sams8cb39de2010-06-01 15:47:01 -0700208 public static Element PROGRAM_FRAGMENT(RenderScript rs) {
209 if(rs.mElement_PROGRAM_FRAGMENT == null) {
210 rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
Jason Samsa70f4162010-03-26 15:33:42 -0700211 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700212 return rs.mElement_PROGRAM_FRAGMENT;
Jason Samsa70f4162010-03-26 15:33:42 -0700213 }
214
Jason Sams8cb39de2010-06-01 15:47:01 -0700215 public static Element PROGRAM_VERTEX(RenderScript rs) {
216 if(rs.mElement_PROGRAM_VERTEX == null) {
217 rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
Jason Samsa70f4162010-03-26 15:33:42 -0700218 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700219 return rs.mElement_PROGRAM_VERTEX;
Jason Samsa70f4162010-03-26 15:33:42 -0700220 }
221
Jason Sams8cb39de2010-06-01 15:47:01 -0700222 public static Element PROGRAM_RASTER(RenderScript rs) {
223 if(rs.mElement_PROGRAM_RASTER == null) {
224 rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
Jason Samsa70f4162010-03-26 15:33:42 -0700225 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700226 return rs.mElement_PROGRAM_RASTER;
Jason Samsa70f4162010-03-26 15:33:42 -0700227 }
228
Jason Sams8cb39de2010-06-01 15:47:01 -0700229 public static Element PROGRAM_STORE(RenderScript rs) {
230 if(rs.mElement_PROGRAM_STORE == null) {
231 rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
Jason Samsa70f4162010-03-26 15:33:42 -0700232 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700233 return rs.mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700234 }
235
236
Jason Sams718cd1f2009-12-23 14:35:29 -0800237 public static Element A_8(RenderScript rs) {
238 if(rs.mElement_A_8 == null) {
239 rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
240 }
241 return rs.mElement_A_8;
242 }
243
244 public static Element RGB_565(RenderScript rs) {
245 if(rs.mElement_RGB_565 == null) {
246 rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
247 }
248 return rs.mElement_RGB_565;
249 }
250
251 public static Element RGB_888(RenderScript rs) {
252 if(rs.mElement_RGB_888 == null) {
253 rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
254 }
255 return rs.mElement_RGB_888;
256 }
257
258 public static Element RGBA_5551(RenderScript rs) {
259 if(rs.mElement_RGBA_5551 == null) {
260 rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
261 }
262 return rs.mElement_RGBA_5551;
263 }
264
265 public static Element RGBA_4444(RenderScript rs) {
266 if(rs.mElement_RGBA_4444 == null) {
267 rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
268 }
269 return rs.mElement_RGBA_4444;
270 }
271
272 public static Element RGBA_8888(RenderScript rs) {
273 if(rs.mElement_RGBA_8888 == null) {
274 rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
275 }
276 return rs.mElement_RGBA_8888;
277 }
278
Jason Sams8cb39de2010-06-01 15:47:01 -0700279 public static Element F32_2(RenderScript rs) {
280 if(rs.mElement_FLOAT_2 == null) {
281 rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
Jason Sams718cd1f2009-12-23 14:35:29 -0800282 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700283 return rs.mElement_FLOAT_2;
Jason Sams718cd1f2009-12-23 14:35:29 -0800284 }
285
Jason Sams8cb39de2010-06-01 15:47:01 -0700286 public static Element F32_3(RenderScript rs) {
287 if(rs.mElement_FLOAT_3 == null) {
288 rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
Jason Sams718cd1f2009-12-23 14:35:29 -0800289 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700290 return rs.mElement_FLOAT_3;
Jason Sams718cd1f2009-12-23 14:35:29 -0800291 }
292
Jason Sams8cb39de2010-06-01 15:47:01 -0700293 public static Element F32_4(RenderScript rs) {
294 if(rs.mElement_FLOAT_4 == null) {
295 rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800296 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700297 return rs.mElement_FLOAT_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800298 }
299
Jason Sams8cb39de2010-06-01 15:47:01 -0700300 public static Element U8_4(RenderScript rs) {
301 if(rs.mElement_UCHAR_4 == null) {
302 rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
Jason Sams718cd1f2009-12-23 14:35:29 -0800303 }
Jason Sams8cb39de2010-06-01 15:47:01 -0700304 return rs.mElement_UCHAR_4;
Jason Sams718cd1f2009-12-23 14:35:29 -0800305 }
306
Jason Sams1d45c472010-08-25 14:31:48 -0700307 public static Element MATRIX_4X4(RenderScript rs) {
308 if(rs.mElement_MATRIX_4X4 == null) {
309 rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
310 }
311 return rs.mElement_MATRIX_4X4;
312 }
313 public static Element MATRIX4X4(RenderScript rs) {
314 return MATRIX_4X4(rs);
315 }
316
317 public static Element MATRIX_3X3(RenderScript rs) {
318 if(rs.mElement_MATRIX_3X3 == null) {
319 rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
320 }
321 return rs.mElement_MATRIX_4X4;
322 }
323
324 public static Element MATRIX_2X2(RenderScript rs) {
325 if(rs.mElement_MATRIX_2X2 == null) {
326 rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
327 }
328 return rs.mElement_MATRIX_2X2;
329 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800330
Jason Sams70d4e502010-09-02 17:35:23 -0700331 Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700332 super(id, rs);
Jason Samsea84a7c2009-09-04 14:42:41 -0700333 mSize = 0;
Jason Sams718cd1f2009-12-23 14:35:29 -0800334 mElements = e;
335 mElementNames = n;
Jason Sams70d4e502010-09-02 17:35:23 -0700336 mArraySizes = as;
Jason Sams718cd1f2009-12-23 14:35:29 -0800337 for (int ct = 0; ct < mElements.length; ct++ ) {
338 mSize += mElements[ct].mSize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800339 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800340 }
341
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700342 Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
343 super(id, rs);
Jason Sams718cd1f2009-12-23 14:35:29 -0800344 mSize = dt.mSize * size;
345 mType = dt;
346 mKind = dk;
347 mNormalized = norm;
348 mVectorSize = size;
Jason Sams36e612a2009-07-31 16:26:13 -0700349 }
350
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700351 Element(int id, RenderScript rs) {
352 super(id, rs);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700353 }
354
355 @Override
356 void updateFromNative() {
357
358 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
359 int[] dataBuffer = new int[5];
360 mRS.nElementGetNativeData(mID, dataBuffer);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700361
362 mNormalized = dataBuffer[2] == 1 ? true : false;
363 mVectorSize = dataBuffer[3];
364 mSize = 0;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700365 for (DataType dt: DataType.values()) {
366 if(dt.mID == dataBuffer[0]){
367 mType = dt;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700368 mSize = mType.mSize * mVectorSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700369 }
370 }
371 for (DataKind dk: DataKind.values()) {
372 if(dk.mID == dataBuffer[1]){
373 mKind = dk;
374 }
375 }
376
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700377 int numSubElements = dataBuffer[4];
378 if(numSubElements > 0) {
379 mElements = new Element[numSubElements];
380 mElementNames = new String[numSubElements];
381
382 int[] subElementIds = new int[numSubElements];
383 mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
384 for(int i = 0; i < numSubElements; i ++) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700385 mElements[i] = new Element(subElementIds[i], mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700386 mElements[i].updateFromNative();
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700387 mSize += mElements[i].mSize;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700388 }
389 }
390
391 }
392
Jason Sams36e612a2009-07-31 16:26:13 -0700393 public void destroy() throws IllegalStateException {
Jason Sams7ce033d2009-08-18 14:14:24 -0700394 super.destroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700395 }
396
Jason Sams718cd1f2009-12-23 14:35:29 -0800397 /////////////////////////////////////////
398 public static Element createUser(RenderScript rs, DataType dt) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700399 DataKind dk = DataKind.USER;
400 boolean norm = false;
401 int vecSize = 1;
402 int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
403 return new Element(id, rs, dt, dk, norm, vecSize);
Jason Sams718cd1f2009-12-23 14:35:29 -0800404 }
405
406 public static Element createVector(RenderScript rs, DataType dt, int size) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800407 if (size < 2 || size > 4) {
408 throw new IllegalArgumentException("Bad size");
Jason Samsea84a7c2009-09-04 14:42:41 -0700409 }
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700410 DataKind dk = DataKind.USER;
411 boolean norm = false;
412 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
413 return new Element(id, rs, dt, dk, norm, size);
Jason Samsea84a7c2009-09-04 14:42:41 -0700414 }
415
Jason Sams718cd1f2009-12-23 14:35:29 -0800416 public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800417 if (!(dk == DataKind.PIXEL_L ||
418 dk == DataKind.PIXEL_A ||
419 dk == DataKind.PIXEL_LA ||
420 dk == DataKind.PIXEL_RGB ||
421 dk == DataKind.PIXEL_RGBA)) {
422 throw new IllegalArgumentException("Unsupported DataKind");
423 }
424 if (!(dt == DataType.UNSIGNED_8 ||
425 dt == DataType.UNSIGNED_5_6_5 ||
426 dt == DataType.UNSIGNED_4_4_4_4 ||
427 dt == DataType.UNSIGNED_5_5_5_1)) {
428 throw new IllegalArgumentException("Unsupported DataType");
429 }
430 if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
431 throw new IllegalArgumentException("Bad kind and type combo");
432 }
433 if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
434 throw new IllegalArgumentException("Bad kind and type combo");
435 }
436 if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
437 throw new IllegalArgumentException("Bad kind and type combo");
438 }
439
440 int size = 1;
441 if (dk == DataKind.PIXEL_LA) {
442 size = 2;
443 }
444 if (dk == DataKind.PIXEL_RGB) {
445 size = 3;
446 }
447 if (dk == DataKind.PIXEL_RGBA) {
448 size = 4;
449 }
450
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700451 boolean norm = true;
452 int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
453 return new Element(id, rs, dt, dk, norm, size);
Jason Sams718cd1f2009-12-23 14:35:29 -0800454 }
Jason Sams36e612a2009-07-31 16:26:13 -0700455
456 public static class Builder {
457 RenderScript mRS;
Jason Sams718cd1f2009-12-23 14:35:29 -0800458 Element[] mElements;
459 String[] mElementNames;
Jason Sams70d4e502010-09-02 17:35:23 -0700460 int[] mArraySizes;
Jason Sams718cd1f2009-12-23 14:35:29 -0800461 int mCount;
Jason Sams22534172009-08-04 16:58:20 -0700462
463 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700464 mRS = rs;
Jason Sams718cd1f2009-12-23 14:35:29 -0800465 mCount = 0;
466 mElements = new Element[8];
467 mElementNames = new String[8];
Jason Sams70d4e502010-09-02 17:35:23 -0700468 mArraySizes = new int[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700469 }
470
Jason Sams70d4e502010-09-02 17:35:23 -0700471 public void add(Element element, String name, int arraySize) {
472 if (arraySize < 1) {
473 throw new IllegalArgumentException("Array size cannot be less than 1.");
474 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800475 if(mCount == mElements.length) {
476 Element[] e = new Element[mCount + 8];
477 String[] s = new String[mCount + 8];
Jason Sams70d4e502010-09-02 17:35:23 -0700478 int[] as = new int[mCount + 8];
Jason Sams718cd1f2009-12-23 14:35:29 -0800479 System.arraycopy(mElements, 0, e, 0, mCount);
480 System.arraycopy(mElementNames, 0, s, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700481 System.arraycopy(mArraySizes, 0, as, 0, mCount);
Jason Sams718cd1f2009-12-23 14:35:29 -0800482 mElements = e;
483 mElementNames = s;
Jason Sams70d4e502010-09-02 17:35:23 -0700484 mArraySizes = as;
Jason Sams36e612a2009-07-31 16:26:13 -0700485 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800486 mElements[mCount] = element;
487 mElementNames[mCount] = name;
Jason Sams70d4e502010-09-02 17:35:23 -0700488 mArraySizes[mCount] = arraySize;
Jason Sams718cd1f2009-12-23 14:35:29 -0800489 mCount++;
Jason Sams07ae4062009-08-27 20:23:34 -0700490 }
491
Jason Sams70d4e502010-09-02 17:35:23 -0700492 public void add(Element element, String name) {
493 add(element, name, 1);
494 }
495
Jason Sams22534172009-08-04 16:58:20 -0700496 public Element create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800497 mRS.validate();
Jason Sams718cd1f2009-12-23 14:35:29 -0800498 Element[] ein = new Element[mCount];
499 String[] sin = new String[mCount];
Jason Sams70d4e502010-09-02 17:35:23 -0700500 int[] asin = new int[mCount];
Jason Sams718cd1f2009-12-23 14:35:29 -0800501 java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
502 java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
Jason Sams70d4e502010-09-02 17:35:23 -0700503 java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700504
505 int[] ids = new int[ein.length];
506 for (int ct = 0; ct < ein.length; ct++ ) {
507 ids[ct] = ein[ct].mID;
508 }
Jason Sams70d4e502010-09-02 17:35:23 -0700509 int id = mRS.nElementCreate2(ids, sin, asin);
510 return new Element(id, mRS, ein, sin, asin);
Jason Sams36e612a2009-07-31 16:26:13 -0700511 }
512 }
513
Jason Sams718cd1f2009-12-23 14:35:29 -0800514 static void initPredefined(RenderScript rs) {
515 int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
516 DataKind.PIXEL_A.mID, true, 1);
517 int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
518 DataKind.PIXEL_RGBA.mID, true, 4);
519 int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
520 DataKind.PIXEL_RGBA.mID, true, 4);
521 int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
522 DataKind.PIXEL_RGB.mID, true, 3);
523 rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
524 }
Jason Sams36e612a2009-07-31 16:26:13 -0700525}
526