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