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