Element.java revision 3aac0abe7965ce9e2078c7d5796805d83e39df7c
1/*
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
19import java.lang.reflect.Field;
20import android.util.Log;
21
22/**
23 * <p>The most basic data type. An element represents one cell of a memory allocation.
24 * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms.
25 * Examples of basic elements are:</p>
26 * <ul>
27 *  <li>Single float value</li>
28 *  <li>4 element float vector</li>
29 *  <li>single RGB-565 color</li>
30 *  <li>single unsigned int 16</li>
31 * </ul>
32 * <p>Complex elements contain a list of sub-elements and names that
33 * represents a structure of data. The fields can be accessed by name
34 * from a script or shader. The memory layout is defined and ordered. Data
35 * alignment is determined by the most basic primitive type. i.e. a float4
36 * vector will be aligned to sizeof(float) and not sizeof(float4). The
37 * ordering of elements in memory will be the order in which they were added
38 * with each component aligned as necessary. No re-ordering will be done.</p>
39 *
40 * <p>The primary source of elements are from scripts. A script that exports a
41 * bind point for a data structure generates a Renderscript element to represent the
42 * data exported by the script. The other common source of elements is from bitmap formats.</p>
43 **/
44public class Element extends BaseObj {
45    int mSize;
46    Element[] mElements;
47    String[] mElementNames;
48    int[] mArraySizes;
49    int[] mOffsetInBytes;
50
51    int[] mVisibleElementMap;
52
53    DataType mType;
54    DataKind mKind;
55    boolean mNormalized;
56    int mVectorSize;
57
58    private void updateVisibleSubElements() {
59        if (mElements == null) {
60            return;
61        }
62
63        int noPaddingFieldCount = 0;
64        int fieldCount = mElementNames.length;
65        // Find out how many elements are not padding
66        for (int ct = 0; ct < fieldCount; ct ++) {
67            if (mElementNames[ct].charAt(0) != '#') {
68                noPaddingFieldCount ++;
69            }
70        }
71        mVisibleElementMap = new int[noPaddingFieldCount];
72
73        // Make a map that points us at non-padding elements
74        for (int ct = 0, ctNoPadding = 0; ct < fieldCount; ct ++) {
75            if (mElementNames[ct].charAt(0) != '#') {
76                mVisibleElementMap[ctNoPadding ++] = ct;
77            }
78        }
79    }
80
81    /**
82    * @hide
83    * @return element size in bytes
84    */
85    public int getSizeBytes() {return mSize;}
86
87
88    /**
89     * DataType represents the basic type information for a basic element.  The
90     * naming convention follows.  For numeric types it is FLOAT,
91     * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
92     * size of the data.  BOOLEAN is a true / false (1,0)
93     * represented in an 8 bit container.  The UNSIGNED variants
94     * with multiple bit definitions are for packed graphical data
95     * formats and represent vectors with per vector member sizes
96     * which are treated as a single unit for packing and alignment
97     * purposes.
98     *
99     * MATRIX the three matrix types contain FLOAT_32 elements and are treated
100     * as 32 bits for alignment purposes.
101     *
102     * RS_* objects.  32 bit opaque handles.
103     */
104    public enum DataType {
105        /**
106        * @hide
107        * new enum
108        */
109        NONE (0, 0),
110        //FLOAT_16 (1, 2),
111        FLOAT_32 (2, 4),
112        FLOAT_64 (3, 8),
113        SIGNED_8 (4, 1),
114        SIGNED_16 (5, 2),
115        SIGNED_32 (6, 4),
116        SIGNED_64 (7, 8),
117        UNSIGNED_8 (8, 1),
118        UNSIGNED_16 (9, 2),
119        UNSIGNED_32 (10, 4),
120        UNSIGNED_64 (11, 8),
121
122        BOOLEAN(12, 1),
123
124        UNSIGNED_5_6_5 (13, 2),
125        UNSIGNED_5_5_5_1 (14, 2),
126        UNSIGNED_4_4_4_4 (15, 2),
127
128        MATRIX_4X4 (16, 64),
129        MATRIX_3X3 (17, 36),
130        MATRIX_2X2 (18, 16),
131
132        RS_ELEMENT (1000, 4),
133        RS_TYPE (1001, 4),
134        RS_ALLOCATION (1002, 4),
135        RS_SAMPLER (1003, 4),
136        RS_SCRIPT (1004, 4),
137        RS_MESH (1005, 4),
138        RS_PROGRAM_FRAGMENT (1006, 4),
139        RS_PROGRAM_VERTEX (1007, 4),
140        RS_PROGRAM_RASTER (1008, 4),
141        RS_PROGRAM_STORE (1009, 4);
142
143        int mID;
144        int mSize;
145        DataType(int id, int size) {
146            mID = id;
147            mSize = size;
148        }
149    }
150
151    /**
152     * The special interpretation of the data if required.  This is primarly
153     * useful for graphical data.  USER indicates no special interpretation is
154     * expected.  PIXEL is used in conjunction with the standard data types for
155     * representing texture formats.
156     */
157    public enum DataKind {
158        USER (0),
159
160        PIXEL_L (7),
161        PIXEL_A (8),
162        PIXEL_LA (9),
163        PIXEL_RGB (10),
164        PIXEL_RGBA (11),
165        PIXEL_DEPTH (12);
166
167        int mID;
168        DataKind(int id) {
169            mID = id;
170        }
171    }
172
173    /**
174     * Return if a element is too complex for use as a data source for a Mesh or
175     * a Program.
176     *
177     * @return boolean
178     */
179    public boolean isComplex() {
180        if (mElements == null) {
181            return false;
182        }
183        for (int ct=0; ct < mElements.length; ct++) {
184            if (mElements[ct].mElements != null) {
185                return true;
186            }
187        }
188        return false;
189    }
190
191    /**
192    * @hide
193    * @return number of sub-elements in this element
194    */
195    public int getSubElementCount() {
196        if (mVisibleElementMap == null) {
197            return 0;
198        }
199        return mVisibleElementMap.length;
200    }
201
202    /**
203    * @hide
204    * @param index index of the sub-element to return
205    * @return sub-element in this element at given index
206    */
207    public Element getSubElement(int index) {
208        if (mVisibleElementMap == null) {
209            throw new RSIllegalArgumentException("Element contains no sub-elements");
210        }
211        if (index < 0 || index >= mVisibleElementMap.length) {
212            throw new RSIllegalArgumentException("Illegal sub-element index");
213        }
214        return mElements[mVisibleElementMap[index]];
215    }
216
217    /**
218    * @hide
219    * @param index index of the sub-element
220    * @return sub-element in this element at given index
221    */
222    public String getSubElementName(int index) {
223        if (mVisibleElementMap == null) {
224            throw new RSIllegalArgumentException("Element contains no sub-elements");
225        }
226        if (index < 0 || index >= mVisibleElementMap.length) {
227            throw new RSIllegalArgumentException("Illegal sub-element index");
228        }
229        return mElementNames[mVisibleElementMap[index]];
230    }
231
232    /**
233    * @hide
234    * @param index index of the sub-element
235    * @return array size of sub-element in this element at given index
236    */
237    public int getSubElementArraySize(int index) {
238        if (mVisibleElementMap == null) {
239            throw new RSIllegalArgumentException("Element contains no sub-elements");
240        }
241        if (index < 0 || index >= mVisibleElementMap.length) {
242            throw new RSIllegalArgumentException("Illegal sub-element index");
243        }
244        return mArraySizes[mVisibleElementMap[index]];
245    }
246
247    /**
248    * @hide
249    * @param index index of the sub-element
250    * @return offset in bytes of sub-element in this element at given index
251    */
252    public int getSubElementOffsetBytes(int index) {
253        if (mVisibleElementMap == null) {
254            throw new RSIllegalArgumentException("Element contains no sub-elements");
255        }
256        if (index < 0 || index >= mVisibleElementMap.length) {
257            throw new RSIllegalArgumentException("Illegal sub-element index");
258        }
259        return mOffsetInBytes[mVisibleElementMap[index]];
260    }
261
262    /**
263    * @hide
264    * @return element data type
265    */
266    public DataType getDataType() {
267        return mType;
268    }
269
270    /**
271    * @hide
272    * @return element data kind
273    */
274    public DataKind getDataKind() {
275        return mKind;
276    }
277
278    /**
279     * Utility function for returning an Element containing a single Boolean.
280     *
281     * @param rs Context to which the element will belong.
282     *
283     * @return Element
284     */
285    public static Element BOOLEAN(RenderScript rs) {
286        if(rs.mElement_BOOLEAN == null) {
287            rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
288        }
289        return rs.mElement_BOOLEAN;
290    }
291
292    /**
293     * Utility function for returning an Element containing a single UNSIGNED_8.
294     *
295     * @param rs Context to which the element will belong.
296     *
297     * @return Element
298     */
299    public static Element U8(RenderScript rs) {
300        if(rs.mElement_U8 == null) {
301            rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
302        }
303        return rs.mElement_U8;
304    }
305
306    /**
307     * Utility function for returning an Element containing a single SIGNED_8.
308     *
309     * @param rs Context to which the element will belong.
310     *
311     * @return Element
312     */
313    public static Element I8(RenderScript rs) {
314        if(rs.mElement_I8 == null) {
315            rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
316        }
317        return rs.mElement_I8;
318    }
319
320    public static Element U16(RenderScript rs) {
321        if(rs.mElement_U16 == null) {
322            rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
323        }
324        return rs.mElement_U16;
325    }
326
327    public static Element I16(RenderScript rs) {
328        if(rs.mElement_I16 == null) {
329            rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
330        }
331        return rs.mElement_I16;
332    }
333
334    public static Element U32(RenderScript rs) {
335        if(rs.mElement_U32 == null) {
336            rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
337        }
338        return rs.mElement_U32;
339    }
340
341    public static Element I32(RenderScript rs) {
342        if(rs.mElement_I32 == null) {
343            rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
344        }
345        return rs.mElement_I32;
346    }
347
348    public static Element U64(RenderScript rs) {
349        if(rs.mElement_U64 == null) {
350            rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
351        }
352        return rs.mElement_U64;
353    }
354
355    public static Element I64(RenderScript rs) {
356        if(rs.mElement_I64 == null) {
357            rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
358        }
359        return rs.mElement_I64;
360    }
361
362    public static Element F32(RenderScript rs) {
363        if(rs.mElement_F32 == null) {
364            rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
365        }
366        return rs.mElement_F32;
367    }
368
369    public static Element F64(RenderScript rs) {
370        if(rs.mElement_F64 == null) {
371            rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
372        }
373        return rs.mElement_F64;
374    }
375
376    public static Element ELEMENT(RenderScript rs) {
377        if(rs.mElement_ELEMENT == null) {
378            rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
379        }
380        return rs.mElement_ELEMENT;
381    }
382
383    public static Element TYPE(RenderScript rs) {
384        if(rs.mElement_TYPE == null) {
385            rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
386        }
387        return rs.mElement_TYPE;
388    }
389
390    public static Element ALLOCATION(RenderScript rs) {
391        if(rs.mElement_ALLOCATION == null) {
392            rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
393        }
394        return rs.mElement_ALLOCATION;
395    }
396
397    public static Element SAMPLER(RenderScript rs) {
398        if(rs.mElement_SAMPLER == null) {
399            rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
400        }
401        return rs.mElement_SAMPLER;
402    }
403
404    public static Element SCRIPT(RenderScript rs) {
405        if(rs.mElement_SCRIPT == null) {
406            rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
407        }
408        return rs.mElement_SCRIPT;
409    }
410
411    public static Element MESH(RenderScript rs) {
412        if(rs.mElement_MESH == null) {
413            rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
414        }
415        return rs.mElement_MESH;
416    }
417
418    public static Element PROGRAM_FRAGMENT(RenderScript rs) {
419        if(rs.mElement_PROGRAM_FRAGMENT == null) {
420            rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
421        }
422        return rs.mElement_PROGRAM_FRAGMENT;
423    }
424
425    public static Element PROGRAM_VERTEX(RenderScript rs) {
426        if(rs.mElement_PROGRAM_VERTEX == null) {
427            rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
428        }
429        return rs.mElement_PROGRAM_VERTEX;
430    }
431
432    public static Element PROGRAM_RASTER(RenderScript rs) {
433        if(rs.mElement_PROGRAM_RASTER == null) {
434            rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
435        }
436        return rs.mElement_PROGRAM_RASTER;
437    }
438
439    public static Element PROGRAM_STORE(RenderScript rs) {
440        if(rs.mElement_PROGRAM_STORE == null) {
441            rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
442        }
443        return rs.mElement_PROGRAM_STORE;
444    }
445
446
447    public static Element A_8(RenderScript rs) {
448        if(rs.mElement_A_8 == null) {
449            rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
450        }
451        return rs.mElement_A_8;
452    }
453
454    public static Element RGB_565(RenderScript rs) {
455        if(rs.mElement_RGB_565 == null) {
456            rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
457        }
458        return rs.mElement_RGB_565;
459    }
460
461    public static Element RGB_888(RenderScript rs) {
462        if(rs.mElement_RGB_888 == null) {
463            rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
464        }
465        return rs.mElement_RGB_888;
466    }
467
468    public static Element RGBA_5551(RenderScript rs) {
469        if(rs.mElement_RGBA_5551 == null) {
470            rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
471        }
472        return rs.mElement_RGBA_5551;
473    }
474
475    public static Element RGBA_4444(RenderScript rs) {
476        if(rs.mElement_RGBA_4444 == null) {
477            rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
478        }
479        return rs.mElement_RGBA_4444;
480    }
481
482    public static Element RGBA_8888(RenderScript rs) {
483        if(rs.mElement_RGBA_8888 == null) {
484            rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
485        }
486        return rs.mElement_RGBA_8888;
487    }
488
489    public static Element F32_2(RenderScript rs) {
490        if(rs.mElement_FLOAT_2 == null) {
491            rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
492        }
493        return rs.mElement_FLOAT_2;
494    }
495
496    public static Element F32_3(RenderScript rs) {
497        if(rs.mElement_FLOAT_3 == null) {
498            rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
499        }
500        return rs.mElement_FLOAT_3;
501    }
502
503    public static Element F32_4(RenderScript rs) {
504        if(rs.mElement_FLOAT_4 == null) {
505            rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
506        }
507        return rs.mElement_FLOAT_4;
508    }
509
510    public static Element F64_2(RenderScript rs) {
511        if(rs.mElement_DOUBLE_2 == null) {
512            rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
513        }
514        return rs.mElement_DOUBLE_2;
515    }
516
517    public static Element F64_3(RenderScript rs) {
518        if(rs.mElement_DOUBLE_3 == null) {
519            rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
520        }
521        return rs.mElement_DOUBLE_3;
522    }
523
524    public static Element F64_4(RenderScript rs) {
525        if(rs.mElement_DOUBLE_4 == null) {
526            rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
527        }
528        return rs.mElement_DOUBLE_4;
529    }
530
531    public static Element U8_2(RenderScript rs) {
532        if(rs.mElement_UCHAR_2 == null) {
533            rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
534        }
535        return rs.mElement_UCHAR_2;
536    }
537
538    public static Element U8_3(RenderScript rs) {
539        if(rs.mElement_UCHAR_3 == null) {
540            rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
541        }
542        return rs.mElement_UCHAR_3;
543    }
544
545    public static Element U8_4(RenderScript rs) {
546        if(rs.mElement_UCHAR_4 == null) {
547            rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
548        }
549        return rs.mElement_UCHAR_4;
550    }
551
552    public static Element I8_2(RenderScript rs) {
553        if(rs.mElement_CHAR_2 == null) {
554            rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
555        }
556        return rs.mElement_CHAR_2;
557    }
558
559    public static Element I8_3(RenderScript rs) {
560        if(rs.mElement_CHAR_3 == null) {
561            rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
562        }
563        return rs.mElement_CHAR_3;
564    }
565
566    public static Element I8_4(RenderScript rs) {
567        if(rs.mElement_CHAR_4 == null) {
568            rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
569        }
570        return rs.mElement_CHAR_4;
571    }
572
573    public static Element U16_2(RenderScript rs) {
574        if(rs.mElement_USHORT_2 == null) {
575            rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
576        }
577        return rs.mElement_USHORT_2;
578    }
579
580    public static Element U16_3(RenderScript rs) {
581        if(rs.mElement_USHORT_3 == null) {
582            rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
583        }
584        return rs.mElement_USHORT_3;
585    }
586
587    public static Element U16_4(RenderScript rs) {
588        if(rs.mElement_USHORT_4 == null) {
589            rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
590        }
591        return rs.mElement_USHORT_4;
592    }
593
594    public static Element I16_2(RenderScript rs) {
595        if(rs.mElement_SHORT_2 == null) {
596            rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
597        }
598        return rs.mElement_SHORT_2;
599    }
600
601    public static Element I16_3(RenderScript rs) {
602        if(rs.mElement_SHORT_3 == null) {
603            rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
604        }
605        return rs.mElement_SHORT_3;
606    }
607
608    public static Element I16_4(RenderScript rs) {
609        if(rs.mElement_SHORT_4 == null) {
610            rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
611        }
612        return rs.mElement_SHORT_4;
613    }
614
615    public static Element U32_2(RenderScript rs) {
616        if(rs.mElement_UINT_2 == null) {
617            rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
618        }
619        return rs.mElement_UINT_2;
620    }
621
622    public static Element U32_3(RenderScript rs) {
623        if(rs.mElement_UINT_3 == null) {
624            rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
625        }
626        return rs.mElement_UINT_3;
627    }
628
629    public static Element U32_4(RenderScript rs) {
630        if(rs.mElement_UINT_4 == null) {
631            rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
632        }
633        return rs.mElement_UINT_4;
634    }
635
636    public static Element I32_2(RenderScript rs) {
637        if(rs.mElement_INT_2 == null) {
638            rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
639        }
640        return rs.mElement_INT_2;
641    }
642
643    public static Element I32_3(RenderScript rs) {
644        if(rs.mElement_INT_3 == null) {
645            rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
646        }
647        return rs.mElement_INT_3;
648    }
649
650    public static Element I32_4(RenderScript rs) {
651        if(rs.mElement_INT_4 == null) {
652            rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
653        }
654        return rs.mElement_INT_4;
655    }
656
657    public static Element U64_2(RenderScript rs) {
658        if(rs.mElement_ULONG_2 == null) {
659            rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
660        }
661        return rs.mElement_ULONG_2;
662    }
663
664    public static Element U64_3(RenderScript rs) {
665        if(rs.mElement_ULONG_3 == null) {
666            rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
667        }
668        return rs.mElement_ULONG_3;
669    }
670
671    public static Element U64_4(RenderScript rs) {
672        if(rs.mElement_ULONG_4 == null) {
673            rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
674        }
675        return rs.mElement_ULONG_4;
676    }
677
678    public static Element I64_2(RenderScript rs) {
679        if(rs.mElement_LONG_2 == null) {
680            rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
681        }
682        return rs.mElement_LONG_2;
683    }
684
685    public static Element I64_3(RenderScript rs) {
686        if(rs.mElement_LONG_3 == null) {
687            rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
688        }
689        return rs.mElement_LONG_3;
690    }
691
692    public static Element I64_4(RenderScript rs) {
693        if(rs.mElement_LONG_4 == null) {
694            rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
695        }
696        return rs.mElement_LONG_4;
697    }
698
699    public static Element MATRIX_4X4(RenderScript rs) {
700        if(rs.mElement_MATRIX_4X4 == null) {
701            rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
702        }
703        return rs.mElement_MATRIX_4X4;
704    }
705    public static Element MATRIX4X4(RenderScript rs) {
706        return MATRIX_4X4(rs);
707    }
708
709    public static Element MATRIX_3X3(RenderScript rs) {
710        if(rs.mElement_MATRIX_3X3 == null) {
711            rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
712        }
713        return rs.mElement_MATRIX_3X3;
714    }
715
716    public static Element MATRIX_2X2(RenderScript rs) {
717        if(rs.mElement_MATRIX_2X2 == null) {
718            rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
719        }
720        return rs.mElement_MATRIX_2X2;
721    }
722
723    Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
724        super(id, rs);
725        mSize = 0;
726        mElements = e;
727        mElementNames = n;
728        mArraySizes = as;
729        mType = DataType.NONE;
730        mKind = DataKind.USER;
731        mOffsetInBytes = new int[mElements.length];
732        for (int ct = 0; ct < mElements.length; ct++ ) {
733            mOffsetInBytes[ct] = mSize;
734            mSize += mElements[ct].mSize * mArraySizes[ct];
735        }
736        updateVisibleSubElements();
737    }
738
739    Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
740        super(id, rs);
741        if ((dt != DataType.UNSIGNED_5_6_5) &&
742            (dt != DataType.UNSIGNED_4_4_4_4) &&
743            (dt != DataType.UNSIGNED_5_5_5_1)) {
744            if (size == 3) {
745                mSize = dt.mSize * 4;
746            } else {
747                mSize = dt.mSize * size;
748            }
749        } else {
750            mSize = dt.mSize;
751        }
752        mType = dt;
753        mKind = dk;
754        mNormalized = norm;
755        mVectorSize = size;
756    }
757
758    Element(int id, RenderScript rs) {
759        super(id, rs);
760    }
761
762    @Override
763    void updateFromNative() {
764        super.updateFromNative();
765
766        // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
767        int[] dataBuffer = new int[5];
768        mRS.nElementGetNativeData(getID(), dataBuffer);
769
770        mNormalized = dataBuffer[2] == 1 ? true : false;
771        mVectorSize = dataBuffer[3];
772        mSize = 0;
773        for (DataType dt: DataType.values()) {
774            if(dt.mID == dataBuffer[0]){
775                mType = dt;
776                mSize = mType.mSize * mVectorSize;
777            }
778        }
779        for (DataKind dk: DataKind.values()) {
780            if(dk.mID == dataBuffer[1]){
781                mKind = dk;
782            }
783        }
784
785        int numSubElements = dataBuffer[4];
786        if(numSubElements > 0) {
787            mElements = new Element[numSubElements];
788            mElementNames = new String[numSubElements];
789            mArraySizes = new int[numSubElements];
790            mOffsetInBytes = new int[numSubElements];
791
792            int[] subElementIds = new int[numSubElements];
793            mRS.nElementGetSubElements(getID(), subElementIds, mElementNames, mArraySizes);
794            for(int i = 0; i < numSubElements; i ++) {
795                mElements[i] = new Element(subElementIds[i], mRS);
796                mElements[i].updateFromNative();
797                mOffsetInBytes[i] = mSize;
798                mSize += mElements[i].mSize * mArraySizes[i];
799            }
800        }
801        updateVisibleSubElements();
802    }
803
804    /**
805     * Create a custom Element of the specified DataType.  The DataKind will be
806     * set to USER and the vector size to 1 indicating non-vector.
807     *
808     * @param rs The context associated with the new Element.
809     * @param dt The DataType for the new element.
810     * @return Element
811     */
812    static Element createUser(RenderScript rs, DataType dt) {
813        DataKind dk = DataKind.USER;
814        boolean norm = false;
815        int vecSize = 1;
816        int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
817        return new Element(id, rs, dt, dk, norm, vecSize);
818    }
819
820    /**
821     * Create a custom vector element of the specified DataType and vector size.
822     *  DataKind will be set to USER.
823     *
824     * @param rs The context associated with the new Element.
825     * @param dt The DataType for the new element.
826     * @param size Vector size for the new Element.  Range 2-4 inclusive
827     *             supported.
828     *
829     * @return Element
830     */
831    public static Element createVector(RenderScript rs, DataType dt, int size) {
832        if (size < 2 || size > 4) {
833            throw new RSIllegalArgumentException("Vector size out of range 2-4.");
834        }
835        DataKind dk = DataKind.USER;
836        boolean norm = false;
837        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
838        return new Element(id, rs, dt, dk, norm, size);
839    }
840
841    /**
842     * Create a new pixel Element type.  A matching DataType and DataKind must
843     * be provided.  The DataType and DataKind must contain the same number of
844     * components.  Vector size will be set to 1.
845     *
846     * @param rs The context associated with the new Element.
847     * @param dt The DataType for the new element.
848     * @param dk The DataKind to specify the mapping of each component in the
849     *           DataType.
850     *
851     * @return Element
852     */
853    public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
854        if (!(dk == DataKind.PIXEL_L ||
855              dk == DataKind.PIXEL_A ||
856              dk == DataKind.PIXEL_LA ||
857              dk == DataKind.PIXEL_RGB ||
858              dk == DataKind.PIXEL_RGBA ||
859              dk == DataKind.PIXEL_DEPTH)) {
860            throw new RSIllegalArgumentException("Unsupported DataKind");
861        }
862        if (!(dt == DataType.UNSIGNED_8 ||
863              dt == DataType.UNSIGNED_16 ||
864              dt == DataType.UNSIGNED_5_6_5 ||
865              dt == DataType.UNSIGNED_4_4_4_4 ||
866              dt == DataType.UNSIGNED_5_5_5_1)) {
867            throw new RSIllegalArgumentException("Unsupported DataType");
868        }
869        if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
870            throw new RSIllegalArgumentException("Bad kind and type combo");
871        }
872        if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
873            throw new RSIllegalArgumentException("Bad kind and type combo");
874        }
875        if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
876            throw new RSIllegalArgumentException("Bad kind and type combo");
877        }
878        if (dt == DataType.UNSIGNED_16 &&
879            dk != DataKind.PIXEL_DEPTH) {
880            throw new RSIllegalArgumentException("Bad kind and type combo");
881        }
882
883        int size = 1;
884        switch (dk) {
885        case PIXEL_LA:
886            size = 2;
887            break;
888        case PIXEL_RGB:
889            size = 3;
890            break;
891        case PIXEL_RGBA:
892            size = 4;
893            break;
894        case PIXEL_DEPTH:
895            size = 2;
896            break;
897        }
898
899        boolean norm = true;
900        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
901        return new Element(id, rs, dt, dk, norm, size);
902    }
903
904    /**
905     * Check if the current Element is compatible with another Element.
906     * Primitive Elements are compatible if they share the same underlying
907     * size and type (i.e. U8 is compatible with A_8). User-defined Elements
908     * must be equal in order to be compatible. This requires strict name
909     * equivalence for all sub-Elements (in addition to structural equivalence).
910     *
911     * @param e The Element to check compatibility with.
912     *
913     * @return boolean true if the Elements are compatible, otherwise false.
914     */
915    public boolean isCompatible(Element e) {
916        // Try strict BaseObj equality to start with.
917        if (this.equals(e)) {
918            return true;
919        }
920
921        // Ignore mKind because it is allowed to be different (user vs. pixel).
922        // We also ignore mNormalized because it can be different. The mType
923        // field must be non-null since we require name equivalence for
924        // user-created Elements.
925        return ((mSize == e.mSize) &&
926                (mType != null) &&
927                (mType == e.mType) &&
928                (mVectorSize == e.mVectorSize));
929    }
930
931    /**
932     * Builder class for producing complex elements with matching field and name
933     * pairs.  The builder starts empty.  The order in which elements are added
934     * is retained for the layout in memory.
935     *
936     */
937    public static class Builder {
938        RenderScript mRS;
939        Element[] mElements;
940        String[] mElementNames;
941        int[] mArraySizes;
942        int mCount;
943        int mSkipPadding;
944
945        /**
946         * Create a builder object.
947         *
948         * @param rs
949         */
950        public Builder(RenderScript rs) {
951            mRS = rs;
952            mCount = 0;
953            mElements = new Element[8];
954            mElementNames = new String[8];
955            mArraySizes = new int[8];
956        }
957
958        /**
959         * Add an array of elements to this element.
960         *
961         * @param element
962         * @param name
963         * @param arraySize
964         */
965        public Builder add(Element element, String name, int arraySize) {
966            if (arraySize < 1) {
967                throw new RSIllegalArgumentException("Array size cannot be less than 1.");
968            }
969
970            // Skip padding fields after a vector 3 type.
971            if (mSkipPadding != 0) {
972                if (name.startsWith("#padding_")) {
973                    mSkipPadding = 0;
974                    return this;
975                }
976            }
977
978            if (element.mVectorSize == 3) {
979                mSkipPadding = 1;
980            } else {
981                mSkipPadding = 0;
982            }
983
984            if(mCount == mElements.length) {
985                Element[] e = new Element[mCount + 8];
986                String[] s = new String[mCount + 8];
987                int[] as = new int[mCount + 8];
988                System.arraycopy(mElements, 0, e, 0, mCount);
989                System.arraycopy(mElementNames, 0, s, 0, mCount);
990                System.arraycopy(mArraySizes, 0, as, 0, mCount);
991                mElements = e;
992                mElementNames = s;
993                mArraySizes = as;
994            }
995            mElements[mCount] = element;
996            mElementNames[mCount] = name;
997            mArraySizes[mCount] = arraySize;
998            mCount++;
999            return this;
1000        }
1001
1002        /**
1003         * Add a single element to this Element.
1004         *
1005         * @param element
1006         * @param name
1007         */
1008        public Builder add(Element element, String name) {
1009            return add(element, name, 1);
1010        }
1011
1012        /**
1013         * Create the element from this builder.
1014         *
1015         *
1016         * @return Element
1017         */
1018        public Element create() {
1019            mRS.validate();
1020            Element[] ein = new Element[mCount];
1021            String[] sin = new String[mCount];
1022            int[] asin = new int[mCount];
1023            java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1024            java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
1025            java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
1026
1027            int[] ids = new int[ein.length];
1028            for (int ct = 0; ct < ein.length; ct++ ) {
1029                ids[ct] = ein[ct].getID();
1030            }
1031            int id = mRS.nElementCreate2(ids, sin, asin);
1032            return new Element(id, mRS, ein, sin, asin);
1033        }
1034    }
1035}
1036
1037