Element.java revision 252c07802f7039f15f723751162e64a6621e6998
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19import java.lang.reflect.Field;
20import android.util.Log;
21
22/**
23 * Element is the basic data type of RenderScript.  An element can be of 2
24 * forms.  Basic elements contain a single component of data.  This can be of
25 * any of the legal RS types.  Examples of basic element types.
26 * Single float value
27 * 4 element float vector
28 * single RGB-565 color
29 * single unsigned int 16
30 *
31 * Complex elements will contain a list of sub-elements and names.  This in
32 * effect represents a structure of data.  The fields can be accessed by name
33 * from a script or shader.  The memory layout is defined and ordered.  Data
34 * alignment is determinied by the most basic primitive type.  i.e. a float4
35 * vector will be alligned to sizeof(float) and not sizeof(float4).  The
36 * ordering of elements in memory will be the order in which they were added
37 * with each component aligned as necessary. No re-ordering will be done.
38 *
39 * The primary source of elements will be from scripts.  A script that exports a
40 * bind point for a data structure will generate a RS element to represent the
41 * data exported by the script.
42 **/
43public class Element extends BaseObj {
44    int mSize;
45    Element[] mElements;
46    String[] mElementNames;
47    int[] mArraySizes;
48
49    DataType mType;
50    DataKind mKind;
51    boolean mNormalized;
52    int mVectorSize;
53
54    int getSizeBytes() {return mSize;}
55
56
57    /**
58     * DataType represents the basic type information for a basic element.  The
59     * naming convention follows.  For numeric types its FLOAT, SIGNED, UNSIGNED
60     * followed by the _BITS where BITS is the size of the data.  BOOLEAN is a
61     * true / false (1,0) represented in an 8 bit container.  The UNSIGNED
62     * variants with multiple bit definitions are for packed graphical data
63     * formats and represents vectors with per vector member sizes which are
64     * treated as a single unit for packing and alignment purposes.
65     *
66     * MATRIX the three matrix types contain FLOAT_32 elements and are treated
67     * as 32 bits for alignment purposes.
68     *
69     * RS_* objects.  32 bit opaque handles.
70     */
71    public enum DataType {
72        //FLOAT_16 (1, 2),
73        FLOAT_32 (2, 4),
74        FLOAT_64 (3, 8),
75        SIGNED_8 (4, 1),
76        SIGNED_16 (5, 2),
77        SIGNED_32 (6, 4),
78        SIGNED_64 (7, 8),
79        UNSIGNED_8 (8, 1),
80        UNSIGNED_16 (9, 2),
81        UNSIGNED_32 (10, 4),
82        UNSIGNED_64 (11, 8),
83
84        BOOLEAN(12, 1),
85
86        UNSIGNED_5_6_5 (13, 2),
87        UNSIGNED_5_5_5_1 (14, 2),
88        UNSIGNED_4_4_4_4 (15, 2),
89
90        MATRIX_4X4 (16, 64),
91        MATRIX_3X3 (17, 36),
92        MATRIX_2X2 (18, 16),
93
94        RS_ELEMENT (1000, 4),
95        RS_TYPE (1001, 4),
96        RS_ALLOCATION (1002, 4),
97        RS_SAMPLER (1003, 4),
98        RS_SCRIPT (1004, 4),
99        RS_MESH (1005, 4),
100        RS_PROGRAM_FRAGMENT (1006, 4),
101        RS_PROGRAM_VERTEX (1007, 4),
102        RS_PROGRAM_RASTER (1008, 4),
103        RS_PROGRAM_STORE (1009, 4);
104
105        int mID;
106        int mSize;
107        DataType(int id, int size) {
108            mID = id;
109            mSize = size;
110        }
111    }
112
113    /**
114     * The special interpretation of the data if required.  This is primarly
115     * useful for graphical data.  USER indicates no special interpretation is
116     * expected.  PIXEL is used in conjunction with the standard data types for
117     * representing texture formats.
118     */
119    public enum DataKind {
120        USER (0),
121
122        PIXEL_L (7),
123        PIXEL_A (8),
124        PIXEL_LA (9),
125        PIXEL_RGB (10),
126        PIXEL_RGBA (11);
127
128        int mID;
129        DataKind(int id) {
130            mID = id;
131        }
132    }
133
134    /**
135     * Return if a element is too complex for use as a data source for a Mesh or
136     * a Program.
137     *
138     * @return boolean
139     */
140    public boolean isComplex() {
141        if (mElements == null) {
142            return false;
143        }
144        for (int ct=0; ct < mElements.length; ct++) {
145            if (mElements[ct].mElements != null) {
146                return true;
147            }
148        }
149        return false;
150    }
151
152    /**
153     * Utility function for returning an Element containing a single Boolean.
154     *
155     * @param rs Context to which the element will belong.
156     *
157     * @return Element
158     */
159    public static Element BOOLEAN(RenderScript rs) {
160        if(rs.mElement_BOOLEAN == null) {
161            rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
162        }
163        return rs.mElement_BOOLEAN;
164    }
165
166    /**
167     * Utility function for returning an Element containing a single UNSIGNED_8.
168     *
169     * @param rs Context to which the element will belong.
170     *
171     * @return Element
172     */
173    public static Element U8(RenderScript rs) {
174        if(rs.mElement_U8 == null) {
175            rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
176        }
177        return rs.mElement_U8;
178    }
179
180    /**
181     * Utility function for returning an Element containing a single SIGNED_8.
182     *
183     * @param rs Context to which the element will belong.
184     *
185     * @return Element
186     */
187    public static Element I8(RenderScript rs) {
188        if(rs.mElement_I8 == null) {
189            rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
190        }
191        return rs.mElement_I8;
192    }
193
194    public static Element U16(RenderScript rs) {
195        if(rs.mElement_U16 == null) {
196            rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
197        }
198        return rs.mElement_U16;
199    }
200
201    public static Element I16(RenderScript rs) {
202        if(rs.mElement_I16 == null) {
203            rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
204        }
205        return rs.mElement_I16;
206    }
207
208    public static Element U32(RenderScript rs) {
209        if(rs.mElement_U32 == null) {
210            rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
211        }
212        return rs.mElement_U32;
213    }
214
215    public static Element I32(RenderScript rs) {
216        if(rs.mElement_I32 == null) {
217            rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
218        }
219        return rs.mElement_I32;
220    }
221
222    public static Element U64(RenderScript rs) {
223        if(rs.mElement_U64 == null) {
224            rs.mElement_U64 = createUser(rs, DataType.UNSIGNED_64);
225        }
226        return rs.mElement_U64;
227    }
228
229    public static Element I64(RenderScript rs) {
230        if(rs.mElement_I64 == null) {
231            rs.mElement_I64 = createUser(rs, DataType.SIGNED_64);
232        }
233        return rs.mElement_I64;
234    }
235
236    public static Element F32(RenderScript rs) {
237        if(rs.mElement_F32 == null) {
238            rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
239        }
240        return rs.mElement_F32;
241    }
242
243    public static Element F64(RenderScript rs) {
244        if(rs.mElement_F64 == null) {
245            rs.mElement_F64 = createUser(rs, DataType.FLOAT_64);
246        }
247        return rs.mElement_F64;
248    }
249
250    public static Element ELEMENT(RenderScript rs) {
251        if(rs.mElement_ELEMENT == null) {
252            rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
253        }
254        return rs.mElement_ELEMENT;
255    }
256
257    public static Element TYPE(RenderScript rs) {
258        if(rs.mElement_TYPE == null) {
259            rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
260        }
261        return rs.mElement_TYPE;
262    }
263
264    public static Element ALLOCATION(RenderScript rs) {
265        if(rs.mElement_ALLOCATION == null) {
266            rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
267        }
268        return rs.mElement_ALLOCATION;
269    }
270
271    public static Element SAMPLER(RenderScript rs) {
272        if(rs.mElement_SAMPLER == null) {
273            rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
274        }
275        return rs.mElement_SAMPLER;
276    }
277
278    public static Element SCRIPT(RenderScript rs) {
279        if(rs.mElement_SCRIPT == null) {
280            rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
281        }
282        return rs.mElement_SCRIPT;
283    }
284
285    public static Element MESH(RenderScript rs) {
286        if(rs.mElement_MESH == null) {
287            rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
288        }
289        return rs.mElement_MESH;
290    }
291
292    public static Element PROGRAM_FRAGMENT(RenderScript rs) {
293        if(rs.mElement_PROGRAM_FRAGMENT == null) {
294            rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
295        }
296        return rs.mElement_PROGRAM_FRAGMENT;
297    }
298
299    public static Element PROGRAM_VERTEX(RenderScript rs) {
300        if(rs.mElement_PROGRAM_VERTEX == null) {
301            rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
302        }
303        return rs.mElement_PROGRAM_VERTEX;
304    }
305
306    public static Element PROGRAM_RASTER(RenderScript rs) {
307        if(rs.mElement_PROGRAM_RASTER == null) {
308            rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
309        }
310        return rs.mElement_PROGRAM_RASTER;
311    }
312
313    public static Element PROGRAM_STORE(RenderScript rs) {
314        if(rs.mElement_PROGRAM_STORE == null) {
315            rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
316        }
317        return rs.mElement_PROGRAM_STORE;
318    }
319
320
321    public static Element A_8(RenderScript rs) {
322        if(rs.mElement_A_8 == null) {
323            rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
324        }
325        return rs.mElement_A_8;
326    }
327
328    public static Element RGB_565(RenderScript rs) {
329        if(rs.mElement_RGB_565 == null) {
330            rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
331        }
332        return rs.mElement_RGB_565;
333    }
334
335    public static Element RGB_888(RenderScript rs) {
336        if(rs.mElement_RGB_888 == null) {
337            rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
338        }
339        return rs.mElement_RGB_888;
340    }
341
342    public static Element RGBA_5551(RenderScript rs) {
343        if(rs.mElement_RGBA_5551 == null) {
344            rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
345        }
346        return rs.mElement_RGBA_5551;
347    }
348
349    public static Element RGBA_4444(RenderScript rs) {
350        if(rs.mElement_RGBA_4444 == null) {
351            rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
352        }
353        return rs.mElement_RGBA_4444;
354    }
355
356    public static Element RGBA_8888(RenderScript rs) {
357        if(rs.mElement_RGBA_8888 == null) {
358            rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
359        }
360        return rs.mElement_RGBA_8888;
361    }
362
363    public static Element F32_2(RenderScript rs) {
364        if(rs.mElement_FLOAT_2 == null) {
365            rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
366        }
367        return rs.mElement_FLOAT_2;
368    }
369
370    public static Element F32_3(RenderScript rs) {
371        if(rs.mElement_FLOAT_3 == null) {
372            rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
373        }
374        return rs.mElement_FLOAT_3;
375    }
376
377    public static Element F32_4(RenderScript rs) {
378        if(rs.mElement_FLOAT_4 == null) {
379            rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
380        }
381        return rs.mElement_FLOAT_4;
382    }
383
384    public static Element U8_4(RenderScript rs) {
385        if(rs.mElement_UCHAR_4 == null) {
386            rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
387        }
388        return rs.mElement_UCHAR_4;
389    }
390
391    public static Element MATRIX_4X4(RenderScript rs) {
392        if(rs.mElement_MATRIX_4X4 == null) {
393            rs.mElement_MATRIX_4X4 = createUser(rs, DataType.MATRIX_4X4);
394        }
395        return rs.mElement_MATRIX_4X4;
396    }
397    public static Element MATRIX4X4(RenderScript rs) {
398        return MATRIX_4X4(rs);
399    }
400
401    public static Element MATRIX_3X3(RenderScript rs) {
402        if(rs.mElement_MATRIX_3X3 == null) {
403            rs.mElement_MATRIX_3X3 = createUser(rs, DataType.MATRIX_3X3);
404        }
405        return rs.mElement_MATRIX_4X4;
406    }
407
408    public static Element MATRIX_2X2(RenderScript rs) {
409        if(rs.mElement_MATRIX_2X2 == null) {
410            rs.mElement_MATRIX_2X2 = createUser(rs, DataType.MATRIX_2X2);
411        }
412        return rs.mElement_MATRIX_2X2;
413    }
414
415    Element(int id, RenderScript rs, Element[] e, String[] n, int[] as) {
416        super(id, rs);
417        mSize = 0;
418        mElements = e;
419        mElementNames = n;
420        mArraySizes = as;
421        for (int ct = 0; ct < mElements.length; ct++ ) {
422            mSize += mElements[ct].mSize * mArraySizes[ct];
423        }
424    }
425
426    Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
427        super(id, rs);
428        if ((dt != DataType.UNSIGNED_5_6_5) &&
429            (dt != DataType.UNSIGNED_4_4_4_4) &&
430            (dt != DataType.UNSIGNED_5_5_5_1)) {
431            mSize = dt.mSize * size;
432        } else {
433            mSize = dt.mSize;
434        }
435        mType = dt;
436        mKind = dk;
437        mNormalized = norm;
438        mVectorSize = size;
439    }
440
441    Element(int id, RenderScript rs) {
442        super(id, rs);
443    }
444
445    @Override
446    void updateFromNative() {
447        super.updateFromNative();
448
449        // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
450        int[] dataBuffer = new int[5];
451        mRS.nElementGetNativeData(getID(), dataBuffer);
452
453        mNormalized = dataBuffer[2] == 1 ? true : false;
454        mVectorSize = dataBuffer[3];
455        mSize = 0;
456        for (DataType dt: DataType.values()) {
457            if(dt.mID == dataBuffer[0]){
458                mType = dt;
459                mSize = mType.mSize * mVectorSize;
460            }
461        }
462        for (DataKind dk: DataKind.values()) {
463            if(dk.mID == dataBuffer[1]){
464                mKind = dk;
465            }
466        }
467
468        int numSubElements = dataBuffer[4];
469        if(numSubElements > 0) {
470            mElements = new Element[numSubElements];
471            mElementNames = new String[numSubElements];
472
473            int[] subElementIds = new int[numSubElements];
474            mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
475            for(int i = 0; i < numSubElements; i ++) {
476                mElements[i] = new Element(subElementIds[i], mRS);
477                mElements[i].updateFromNative();
478                mSize += mElements[i].mSize;
479            }
480        }
481
482    }
483
484    /**
485     * Create a custom Element of the specified DataType.  The DataKind will be
486     * set to USER and the vector size to 1 indicating non-vector.
487     *
488     * @param rs The context associated with the new Element.
489     * @param dt The DataType for the new element.
490     * @return Element
491     */
492    static Element createUser(RenderScript rs, DataType dt) {
493        DataKind dk = DataKind.USER;
494        boolean norm = false;
495        int vecSize = 1;
496        int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
497        return new Element(id, rs, dt, dk, norm, vecSize);
498    }
499
500    /**
501     * Create a custom vector element of the specified DataType and vector size.
502     *  DataKind will be set to USER.
503     *
504     * @param rs The context associated with the new Element.
505     * @param dt The DataType for the new element.
506     * @param size Vector size for the new Element.  Range 2-4 inclusive
507     *             supported.
508     *
509     * @return Element
510     */
511    public static Element createVector(RenderScript rs, DataType dt, int size) {
512        if (size < 2 || size > 4) {
513            throw new RSIllegalArgumentException("Vector size out of range 2-4.");
514        }
515        DataKind dk = DataKind.USER;
516        boolean norm = false;
517        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
518        return new Element(id, rs, dt, dk, norm, size);
519    }
520
521    /**
522     * Create a new pixel Element type.  A matching DataType and DataKind must
523     * be provided.  The DataType and DataKind must contain the same number of
524     * components.  Vector size will be set to 1.
525     *
526     * @param rs The context associated with the new Element.
527     * @param dt The DataType for the new element.
528     * @param dk The DataKind to specify the mapping of each component in the
529     *           DataType.
530     *
531     * @return Element
532     */
533    public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
534        if (!(dk == DataKind.PIXEL_L ||
535              dk == DataKind.PIXEL_A ||
536              dk == DataKind.PIXEL_LA ||
537              dk == DataKind.PIXEL_RGB ||
538              dk == DataKind.PIXEL_RGBA)) {
539            throw new RSIllegalArgumentException("Unsupported DataKind");
540        }
541        if (!(dt == DataType.UNSIGNED_8 ||
542              dt == DataType.UNSIGNED_5_6_5 ||
543              dt == DataType.UNSIGNED_4_4_4_4 ||
544              dt == DataType.UNSIGNED_5_5_5_1)) {
545            throw new RSIllegalArgumentException("Unsupported DataType");
546        }
547        if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
548            throw new RSIllegalArgumentException("Bad kind and type combo");
549        }
550        if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
551            throw new RSIllegalArgumentException("Bad kind and type combo");
552        }
553        if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
554            throw new RSIllegalArgumentException("Bad kind and type combo");
555        }
556
557        int size = 1;
558        if (dk == DataKind.PIXEL_LA) {
559            size = 2;
560        }
561        if (dk == DataKind.PIXEL_RGB) {
562            size = 3;
563        }
564        if (dk == DataKind.PIXEL_RGBA) {
565            size = 4;
566        }
567
568        boolean norm = true;
569        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
570        return new Element(id, rs, dt, dk, norm, size);
571    }
572
573    /**
574     * Builder class for producing complex elements with matching field and name
575     * pairs.  The builder starts empty.  The order in which elements are added
576     * is retained for the layout in memory.
577     *
578     */
579    public static class Builder {
580        RenderScript mRS;
581        Element[] mElements;
582        String[] mElementNames;
583        int[] mArraySizes;
584        int mCount;
585
586        /**
587         * Create a builder object.
588         *
589         * @param rs
590         */
591        public Builder(RenderScript rs) {
592            mRS = rs;
593            mCount = 0;
594            mElements = new Element[8];
595            mElementNames = new String[8];
596            mArraySizes = new int[8];
597        }
598
599        /**
600         * Add an array of elements to this element.
601         *
602         * @param element
603         * @param name
604         * @param arraySize
605         */
606        public Builder add(Element element, String name, int arraySize) {
607            if (arraySize < 1) {
608                throw new RSIllegalArgumentException("Array size cannot be less than 1.");
609            }
610            if(mCount == mElements.length) {
611                Element[] e = new Element[mCount + 8];
612                String[] s = new String[mCount + 8];
613                int[] as = new int[mCount + 8];
614                System.arraycopy(mElements, 0, e, 0, mCount);
615                System.arraycopy(mElementNames, 0, s, 0, mCount);
616                System.arraycopy(mArraySizes, 0, as, 0, mCount);
617                mElements = e;
618                mElementNames = s;
619                mArraySizes = as;
620            }
621            mElements[mCount] = element;
622            mElementNames[mCount] = name;
623            mArraySizes[mCount] = arraySize;
624            mCount++;
625            return this;
626        }
627
628        /**
629         * Add a single element to this Element.
630         *
631         * @param element
632         * @param name
633         */
634        public Builder add(Element element, String name) {
635            return add(element, name, 1);
636        }
637
638        /**
639         * Create the element from this builder.
640         *
641         *
642         * @return Element
643         */
644        public Element create() {
645            mRS.validate();
646            Element[] ein = new Element[mCount];
647            String[] sin = new String[mCount];
648            int[] asin = new int[mCount];
649            java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
650            java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
651            java.lang.System.arraycopy(mArraySizes, 0, asin, 0, mCount);
652
653            int[] ids = new int[ein.length];
654            for (int ct = 0; ct < ein.length; ct++ ) {
655                ids[ct] = ein[ct].getID();
656            }
657            int id = mRS.nElementCreate2(ids, sin, asin);
658            return new Element(id, mRS, ein, sin, asin);
659        }
660    }
661}
662
663