Element.java revision c89ad4606ae29e103b85bdfc40c57e36c8877dba
1/*
2 * Copyright (C) 2013 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.support.v8.renderscript;
18
19import java.lang.reflect.Field;
20
21import android.util.Log;
22
23/**
24 * <p>The most basic data type. An element represents one cell of a memory allocation.
25 * Element is the basic data type of Renderscript. An element can be of two forms: Basic elements or Complex forms.
26 * Examples of basic elements are:</p>
27 * <ul>
28 *  <li>Single float value</li>
29 *  <li>4 element float vector</li>
30 *  <li>single RGB-565 color</li>
31 *  <li>single unsigned int 16</li>
32 * </ul>
33 * <p>Complex elements contain a list of sub-elements and names that
34 * represents a structure of data. The fields can be accessed by name
35 * from a script or shader. The memory layout is defined and ordered. Data
36 * alignment is determined by the most basic primitive type. i.e. a float4
37 * vector will be aligned to sizeof(float) and not sizeof(float4). The
38 * ordering of elements in memory will be the order in which they were added
39 * with each component aligned as necessary. No re-ordering will be done.</p>
40 *
41 * <p>The primary source of elements are from scripts. A script that exports a
42 * bind point for a data structure generates a Renderscript element to represent the
43 * data exported by the script. The other common source of elements is from bitmap formats.</p>
44 *
45 * <div class="special reference">
46 * <h3>Developer Guides</h3>
47 * <p>For more information about creating an application that uses Renderscript, read the
48 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
49 * </div>
50 **/
51public class Element extends BaseObj {
52    int mSize;
53    Element[] mElements;
54    String[] mElementNames;
55    int[] mArraySizes;
56    int[] mOffsetInBytes;
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() {
91        return mSize;
92    }
93
94    /**
95    * Returns the number of vector components. 2 for float2, 4 for
96    * float4, etc.
97    * @return element vector size
98    */
99    public int getVectorSize() {
100        return mVectorSize;
101    }
102
103
104    /**
105     * DataType represents the basic type information for a basic element.  The
106     * naming convention follows.  For numeric types it is FLOAT,
107     * SIGNED, or UNSIGNED followed by the _BITS where BITS is the
108     * size of the data.  BOOLEAN is a true / false (1,0)
109     * represented in an 8 bit container.  The UNSIGNED variants
110     * with multiple bit definitions are for packed graphical data
111     * formats and represent vectors with per vector member sizes
112     * which are treated as a single unit for packing and alignment
113     * purposes.
114     *
115     * MATRIX the three matrix types contain FLOAT_32 elements and are treated
116     * as 32 bits for alignment purposes.
117     *
118     * RS_* objects.  32 bit opaque handles.
119     */
120    public enum DataType {
121        NONE (0, 0),
122        //FLOAT_16 (1, 2),
123        FLOAT_32 (2, 4),
124        FLOAT_64 (3, 8),
125        SIGNED_8 (4, 1),
126        SIGNED_16 (5, 2),
127        SIGNED_32 (6, 4),
128        SIGNED_64 (7, 8),
129        UNSIGNED_8 (8, 1),
130        UNSIGNED_16 (9, 2),
131        UNSIGNED_32 (10, 4),
132        UNSIGNED_64 (11, 8),
133
134        BOOLEAN(12, 1),
135
136        UNSIGNED_5_6_5 (13, 2),
137        UNSIGNED_5_5_5_1 (14, 2),
138        UNSIGNED_4_4_4_4 (15, 2),
139
140        MATRIX_4X4 (16, 64),
141        MATRIX_3X3 (17, 36),
142        MATRIX_2X2 (18, 16),
143
144        RS_ELEMENT (1000, 4),
145        RS_TYPE (1001, 4),
146        RS_ALLOCATION (1002, 4),
147        RS_SAMPLER (1003, 4),
148        RS_SCRIPT (1004, 4);
149
150        int mID;
151        int mSize;
152        DataType(int id, int size) {
153            mID = id;
154            mSize = size;
155        }
156    }
157
158    /**
159     * The special interpretation of the data if required.  This is primarly
160     * useful for graphical data.  USER indicates no special interpretation is
161     * expected.  PIXEL is used in conjunction with the standard data types for
162     * representing texture formats.
163     */
164    public enum DataKind {
165        USER (0),
166
167        PIXEL_L (7),
168        PIXEL_A (8),
169        PIXEL_LA (9),
170        PIXEL_RGB (10),
171        PIXEL_RGBA (11),
172        PIXEL_DEPTH (12),
173        PIXEL_YUV(13);
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
426    public static Element A_8(RenderScript rs) {
427        if(rs.mElement_A_8 == null) {
428            rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
429        }
430        return rs.mElement_A_8;
431    }
432
433    public static Element RGB_565(RenderScript rs) {
434        if(rs.mElement_RGB_565 == null) {
435            rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
436        }
437        return rs.mElement_RGB_565;
438    }
439
440    public static Element RGB_888(RenderScript rs) {
441        if(rs.mElement_RGB_888 == null) {
442            rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
443        }
444        return rs.mElement_RGB_888;
445    }
446
447    public static Element RGBA_5551(RenderScript rs) {
448        if(rs.mElement_RGBA_5551 == null) {
449            rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
450        }
451        return rs.mElement_RGBA_5551;
452    }
453
454    public static Element RGBA_4444(RenderScript rs) {
455        if(rs.mElement_RGBA_4444 == null) {
456            rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
457        }
458        return rs.mElement_RGBA_4444;
459    }
460
461    public static Element RGBA_8888(RenderScript rs) {
462        if(rs.mElement_RGBA_8888 == null) {
463            rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
464        }
465        return rs.mElement_RGBA_8888;
466    }
467
468    public static Element F32_2(RenderScript rs) {
469        if(rs.mElement_FLOAT_2 == null) {
470            rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
471        }
472        return rs.mElement_FLOAT_2;
473    }
474
475    public static Element F32_3(RenderScript rs) {
476        if(rs.mElement_FLOAT_3 == null) {
477            rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
478        }
479        return rs.mElement_FLOAT_3;
480    }
481
482    public static Element F32_4(RenderScript rs) {
483        if(rs.mElement_FLOAT_4 == null) {
484            rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
485        }
486        return rs.mElement_FLOAT_4;
487    }
488
489    public static Element F64_2(RenderScript rs) {
490        if(rs.mElement_DOUBLE_2 == null) {
491            rs.mElement_DOUBLE_2 = createVector(rs, DataType.FLOAT_64, 2);
492        }
493        return rs.mElement_DOUBLE_2;
494    }
495
496    public static Element F64_3(RenderScript rs) {
497        if(rs.mElement_DOUBLE_3 == null) {
498            rs.mElement_DOUBLE_3 = createVector(rs, DataType.FLOAT_64, 3);
499        }
500        return rs.mElement_DOUBLE_3;
501    }
502
503    public static Element F64_4(RenderScript rs) {
504        if(rs.mElement_DOUBLE_4 == null) {
505            rs.mElement_DOUBLE_4 = createVector(rs, DataType.FLOAT_64, 4);
506        }
507        return rs.mElement_DOUBLE_4;
508    }
509
510    public static Element U8_2(RenderScript rs) {
511        if(rs.mElement_UCHAR_2 == null) {
512            rs.mElement_UCHAR_2 = createVector(rs, DataType.UNSIGNED_8, 2);
513        }
514        return rs.mElement_UCHAR_2;
515    }
516
517    public static Element U8_3(RenderScript rs) {
518        if(rs.mElement_UCHAR_3 == null) {
519            rs.mElement_UCHAR_3 = createVector(rs, DataType.UNSIGNED_8, 3);
520        }
521        return rs.mElement_UCHAR_3;
522    }
523
524    public static Element U8_4(RenderScript rs) {
525        if(rs.mElement_UCHAR_4 == null) {
526            rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
527        }
528        return rs.mElement_UCHAR_4;
529    }
530
531    public static Element I8_2(RenderScript rs) {
532        if(rs.mElement_CHAR_2 == null) {
533            rs.mElement_CHAR_2 = createVector(rs, DataType.SIGNED_8, 2);
534        }
535        return rs.mElement_CHAR_2;
536    }
537
538    public static Element I8_3(RenderScript rs) {
539        if(rs.mElement_CHAR_3 == null) {
540            rs.mElement_CHAR_3 = createVector(rs, DataType.SIGNED_8, 3);
541        }
542        return rs.mElement_CHAR_3;
543    }
544
545    public static Element I8_4(RenderScript rs) {
546        if(rs.mElement_CHAR_4 == null) {
547            rs.mElement_CHAR_4 = createVector(rs, DataType.SIGNED_8, 4);
548        }
549        return rs.mElement_CHAR_4;
550    }
551
552    public static Element U16_2(RenderScript rs) {
553        if(rs.mElement_USHORT_2 == null) {
554            rs.mElement_USHORT_2 = createVector(rs, DataType.UNSIGNED_16, 2);
555        }
556        return rs.mElement_USHORT_2;
557    }
558
559    public static Element U16_3(RenderScript rs) {
560        if(rs.mElement_USHORT_3 == null) {
561            rs.mElement_USHORT_3 = createVector(rs, DataType.UNSIGNED_16, 3);
562        }
563        return rs.mElement_USHORT_3;
564    }
565
566    public static Element U16_4(RenderScript rs) {
567        if(rs.mElement_USHORT_4 == null) {
568            rs.mElement_USHORT_4 = createVector(rs, DataType.UNSIGNED_16, 4);
569        }
570        return rs.mElement_USHORT_4;
571    }
572
573    public static Element I16_2(RenderScript rs) {
574        if(rs.mElement_SHORT_2 == null) {
575            rs.mElement_SHORT_2 = createVector(rs, DataType.SIGNED_16, 2);
576        }
577        return rs.mElement_SHORT_2;
578    }
579
580    public static Element I16_3(RenderScript rs) {
581        if(rs.mElement_SHORT_3 == null) {
582            rs.mElement_SHORT_3 = createVector(rs, DataType.SIGNED_16, 3);
583        }
584        return rs.mElement_SHORT_3;
585    }
586
587    public static Element I16_4(RenderScript rs) {
588        if(rs.mElement_SHORT_4 == null) {
589            rs.mElement_SHORT_4 = createVector(rs, DataType.SIGNED_16, 4);
590        }
591        return rs.mElement_SHORT_4;
592    }
593
594    public static Element U32_2(RenderScript rs) {
595        if(rs.mElement_UINT_2 == null) {
596            rs.mElement_UINT_2 = createVector(rs, DataType.UNSIGNED_32, 2);
597        }
598        return rs.mElement_UINT_2;
599    }
600
601    public static Element U32_3(RenderScript rs) {
602        if(rs.mElement_UINT_3 == null) {
603            rs.mElement_UINT_3 = createVector(rs, DataType.UNSIGNED_32, 3);
604        }
605        return rs.mElement_UINT_3;
606    }
607
608    public static Element U32_4(RenderScript rs) {
609        if(rs.mElement_UINT_4 == null) {
610            rs.mElement_UINT_4 = createVector(rs, DataType.UNSIGNED_32, 4);
611        }
612        return rs.mElement_UINT_4;
613    }
614
615    public static Element I32_2(RenderScript rs) {
616        if(rs.mElement_INT_2 == null) {
617            rs.mElement_INT_2 = createVector(rs, DataType.SIGNED_32, 2);
618        }
619        return rs.mElement_INT_2;
620    }
621
622    public static Element I32_3(RenderScript rs) {
623        if(rs.mElement_INT_3 == null) {
624            rs.mElement_INT_3 = createVector(rs, DataType.SIGNED_32, 3);
625        }
626        return rs.mElement_INT_3;
627    }
628
629    public static Element I32_4(RenderScript rs) {
630        if(rs.mElement_INT_4 == null) {
631            rs.mElement_INT_4 = createVector(rs, DataType.SIGNED_32, 4);
632        }
633        return rs.mElement_INT_4;
634    }
635
636    public static Element U64_2(RenderScript rs) {
637        if(rs.mElement_ULONG_2 == null) {
638            rs.mElement_ULONG_2 = createVector(rs, DataType.UNSIGNED_64, 2);
639        }
640        return rs.mElement_ULONG_2;
641    }
642
643    public static Element U64_3(RenderScript rs) {
644        if(rs.mElement_ULONG_3 == null) {
645            rs.mElement_ULONG_3 = createVector(rs, DataType.UNSIGNED_64, 3);
646        }
647        return rs.mElement_ULONG_3;
648    }
649
650    public static Element U64_4(RenderScript rs) {
651        if(rs.mElement_ULONG_4 == null) {
652            rs.mElement_ULONG_4 = createVector(rs, DataType.UNSIGNED_64, 4);
653        }
654        return rs.mElement_ULONG_4;
655    }
656
657    public static Element I64_2(RenderScript rs) {
658        if(rs.mElement_LONG_2 == null) {
659            rs.mElement_LONG_2 = createVector(rs, DataType.SIGNED_64, 2);
660        }
661        return rs.mElement_LONG_2;
662    }
663
664    public static Element I64_3(RenderScript rs) {
665        if(rs.mElement_LONG_3 == null) {
666            rs.mElement_LONG_3 = createVector(rs, DataType.SIGNED_64, 3);
667        }
668        return rs.mElement_LONG_3;
669    }
670
671    public static Element I64_4(RenderScript rs) {
672        if(rs.mElement_LONG_4 == null) {
673            rs.mElement_LONG_4 = createVector(rs, DataType.SIGNED_64, 4);
674        }
675        return rs.mElement_LONG_4;
676    }
677
678    public static Element MATRIX_4X4(RenderScript rs) {
679        if(rs.mElement_MATRIX_4X4 == null) {
680            rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
681        }
682        return rs.mElement_MATRIX_4X4;
683    }
684
685    public static Element MATRIX_3X3(RenderScript rs) {
686        if(rs.mElement_MATRIX_3X3 == null) {
687            rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
688        }
689        return rs.mElement_MATRIX_3X3;
690    }
691
692    public static Element MATRIX_2X2(RenderScript rs) {
693        if(rs.mElement_MATRIX_2X2 == null) {
694            rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
695        }
696        return rs.mElement_MATRIX_2X2;
697    }
698
699    Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
700        super(id, rs);
701        mSize = 0;
702        mVectorSize = 1;
703        mElements = e;
704        mElementNames = n;
705        mArraySizes = as;
706        mType = DataType.NONE;
707        mKind = DataKind.USER;
708        mOffsetInBytes = new int[mElements.length];
709        for (int ct = 0; ct < mElements.length; ct++ ) {
710            mOffsetInBytes[ct] = mSize;
711            mSize += mElements[ct].mSize * mArraySizes[ct];
712        }
713        updateVisibleSubElements();
714    }
715
716    Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
717        super(id, rs);
718        if ((dt != DataType.UNSIGNED_5_6_5) &&
719            (dt != DataType.UNSIGNED_4_4_4_4) &&
720            (dt != DataType.UNSIGNED_5_5_5_1)) {
721            if (size == 3) {
722                mSize = dt.mSize * 4;
723            } else {
724                mSize = dt.mSize * size;
725            }
726        } else {
727            mSize = dt.mSize;
728        }
729        mType = dt;
730        mKind = dk;
731        mNormalized = norm;
732        mVectorSize = size;
733    }
734
735    Element(int id, RenderScript rs) {
736        super(id, rs);
737    }
738
739    /**
740     * Create a custom Element of the specified DataType.  The DataKind will be
741     * set to USER and the vector size to 1 indicating non-vector.
742     *
743     * @param rs The context associated with the new Element.
744     * @param dt The DataType for the new element.
745     * @return Element
746     */
747    static Element createUser(RenderScript rs, DataType dt) {
748        if (rs.isNative) {
749            RenderScriptThunker rst = (RenderScriptThunker)rs;
750            return ElementThunker.create(rst, dt);
751        }
752        DataKind dk = DataKind.USER;
753        boolean norm = false;
754        int vecSize = 1;
755        int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
756        return new Element(id, rs, dt, dk, norm, vecSize);
757    }
758
759    /**
760     * Create a custom vector element of the specified DataType and vector size.
761     * DataKind will be set to USER. Only primitive types (FLOAT_32, FLOAT_64,
762     * SIGNED_8, SIGNED_16, SIGNED_32, SIGNED_64, UNSIGNED_8, UNSIGNED_16,
763     * UNSIGNED_32, UNSIGNED_64, BOOLEAN) are supported.
764     *
765     * @param rs The context associated with the new Element.
766     * @param dt The DataType for the new Element.
767     * @param size Vector size for the new Element.  Range 2-4 inclusive
768     *             supported.
769     *
770     * @return Element
771     */
772    public static Element createVector(RenderScript rs, DataType dt, int size) {
773        if (rs.isNative) {
774            RenderScriptThunker rst = (RenderScriptThunker)rs;
775            return ElementThunker.createVector(rst, dt, size);
776        }
777        if (size < 2 || size > 4) {
778            throw new RSIllegalArgumentException("Vector size out of range 2-4.");
779        }
780
781        switch (dt) {
782        // Support only primitive integer/float/boolean types as vectors.
783        case FLOAT_32:
784        case FLOAT_64:
785        case SIGNED_8:
786        case SIGNED_16:
787        case SIGNED_32:
788        case SIGNED_64:
789        case UNSIGNED_8:
790        case UNSIGNED_16:
791        case UNSIGNED_32:
792        case UNSIGNED_64:
793        case BOOLEAN: {
794            DataKind dk = DataKind.USER;
795            boolean norm = false;
796            int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
797            return new Element(id, rs, dt, dk, norm, size);
798        }
799
800        default: {
801            throw new RSIllegalArgumentException("Cannot create vector of " +
802                "non-primitive type.");
803        }
804        }
805    }
806
807    /**
808     * Create a new pixel Element type.  A matching DataType and DataKind must
809     * be provided.  The DataType and DataKind must contain the same number of
810     * components.  Vector size will be set to 1.
811     *
812     * @param rs The context associated with the new Element.
813     * @param dt The DataType for the new element.
814     * @param dk The DataKind to specify the mapping of each component in the
815     *           DataType.
816     *
817     * @return Element
818     */
819    public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
820        if (rs.isNative) {
821            RenderScriptThunker rst = (RenderScriptThunker)rs;
822            return ElementThunker.createPixel(rst, dt, dk);
823        }
824
825        if (!(dk == DataKind.PIXEL_L ||
826              dk == DataKind.PIXEL_A ||
827              dk == DataKind.PIXEL_LA ||
828              dk == DataKind.PIXEL_RGB ||
829              dk == DataKind.PIXEL_RGBA ||
830              dk == DataKind.PIXEL_DEPTH ||
831              dk == DataKind.PIXEL_YUV)) {
832            throw new RSIllegalArgumentException("Unsupported DataKind");
833        }
834        if (!(dt == DataType.UNSIGNED_8 ||
835              dt == DataType.UNSIGNED_16 ||
836              dt == DataType.UNSIGNED_5_6_5 ||
837              dt == DataType.UNSIGNED_4_4_4_4 ||
838              dt == DataType.UNSIGNED_5_5_5_1)) {
839            throw new RSIllegalArgumentException("Unsupported DataType");
840        }
841        if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
842            throw new RSIllegalArgumentException("Bad kind and type combo");
843        }
844        if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
845            throw new RSIllegalArgumentException("Bad kind and type combo");
846        }
847        if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
848            throw new RSIllegalArgumentException("Bad kind and type combo");
849        }
850        if (dt == DataType.UNSIGNED_16 &&
851            dk != DataKind.PIXEL_DEPTH) {
852            throw new RSIllegalArgumentException("Bad kind and type combo");
853        }
854
855        int size = 1;
856        switch (dk) {
857        case PIXEL_LA:
858            size = 2;
859            break;
860        case PIXEL_RGB:
861            size = 3;
862            break;
863        case PIXEL_RGBA:
864            size = 4;
865            break;
866        }
867
868        boolean norm = true;
869        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
870        return new Element(id, rs, dt, dk, norm, size);
871    }
872
873    /**
874     * Check if the current Element is compatible with another Element.
875     * Primitive Elements are compatible if they share the same underlying
876     * size and type (i.e. U8 is compatible with A_8). User-defined Elements
877     * must be equal in order to be compatible. This requires strict name
878     * equivalence for all sub-Elements (in addition to structural equivalence).
879     *
880     * @param e The Element to check compatibility with.
881     *
882     * @return boolean true if the Elements are compatible, otherwise false.
883     */
884    public boolean isCompatible(Element e) {
885        // Try strict BaseObj equality to start with.
886        if (this.equals(e)) {
887            return true;
888        }
889
890        // Ignore mKind because it is allowed to be different (user vs. pixel).
891        // We also ignore mNormalized because it can be different. The mType
892        // field must not be NONE since we require name equivalence for
893        // all user-created Elements.
894        return ((mSize == e.mSize) &&
895                (mType != DataType.NONE) &&
896                (mType == e.mType) &&
897                (mVectorSize == e.mVectorSize));
898    }
899
900    /**
901     * Builder class for producing complex elements with matching field and name
902     * pairs.  The builder starts empty.  The order in which elements are added
903     * is retained for the layout in memory.
904     *
905     */
906    public static class Builder {
907        ElementThunker.BuilderThunker mT;
908
909        RenderScript mRS;
910        Element[] mElements;
911        String[] mElementNames;
912        int[] mArraySizes;
913        int mCount;
914        int mSkipPadding;
915
916        /**
917         * Create a builder object.
918         *
919         * @param rs
920         */
921        public Builder(RenderScript rs) {
922            if (rs.isNative) {
923                RenderScriptThunker rst = (RenderScriptThunker)rs;
924                mT = new ElementThunker.BuilderThunker(rs);
925            }
926            mRS = rs;
927            mCount = 0;
928            mElements = new Element[8];
929            mElementNames = new String[8];
930            mArraySizes = new int[8];
931        }
932
933        /**
934         * Add an array of elements to this element.
935         *
936         * @param element
937         * @param name
938         * @param arraySize
939         */
940        public Builder add(Element element, String name, int arraySize) {
941            if (mT != null) {
942                mT.add(element, name, arraySize);
943                return this;
944            }
945
946            if (arraySize < 1) {
947                throw new RSIllegalArgumentException("Array size cannot be less than 1.");
948            }
949
950            // Skip padding fields after a vector 3 type.
951            if (mSkipPadding != 0) {
952                if (name.startsWith("#padding_")) {
953                    mSkipPadding = 0;
954                    return this;
955                }
956            }
957
958            if (element.mVectorSize == 3) {
959                mSkipPadding = 1;
960            } else {
961                mSkipPadding = 0;
962            }
963
964            if(mCount == mElements.length) {
965                Element[] e = new Element[mCount + 8];
966                String[] s = new String[mCount + 8];
967                int[] as = new int[mCount + 8];
968                System.arraycopy(mElements, 0, e, 0, mCount);
969                System.arraycopy(mElementNames, 0, s, 0, mCount);
970                System.arraycopy(mArraySizes, 0, as, 0, mCount);
971                mElements = e;
972                mElementNames = s;
973                mArraySizes = as;
974            }
975            mElements[mCount] = element;
976            mElementNames[mCount] = name;
977            mArraySizes[mCount] = arraySize;
978            mCount++;
979
980            return this;
981        }
982
983        /**
984         * Add a single element to this Element.
985         *
986         * @param element
987         * @param name
988         */
989        public Builder add(Element element, String name) {
990            return add(element, name, 1);
991        }
992
993        /**
994         * Create the element from this builder.
995         *
996         *
997         * @return Element
998         */
999        public Element create() {
1000            if (mT != null) {
1001                return mT.create(mRS);
1002            }
1003
1004            mRS.validate();
1005            Element[] ein = new Element[mCount];
1006            String[] sin = new String[mCount];
1007            int[] asin = new int[mCount];
1008            java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
1009            java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
1010            java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
1011
1012            int[] ids = new int[ein.length];
1013            for (int ct = 0; ct < ein.length; ct++ ) {
1014                ids[ct] = ein[ct].getID(mRS);
1015            }
1016
1017            int id = mRS.nElementCreate2(ids, sin, asin);
1018            return new Element(id, mRS, ein, sin, asin);
1019        }
1020    }
1021}
1022
1023