Element.java revision dfac814c18f73dd7289f9927edca3e3b6ec6bc00
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 * @hide
24 *
25 **/
26public class Element extends BaseObj {
27    int mSize;
28    Element[] mElements;
29    String[] mElementNames;
30
31    DataType mType;
32    DataKind mKind;
33    boolean mNormalized;
34    int mVectorSize;
35
36    int getSizeBytes() {return mSize;}
37
38    public enum DataType {
39        //FLOAT_16 (1, 2),
40        FLOAT_32 (2, 4),
41        //FLOAT_64 (3, 8),
42        SIGNED_8 (4, 1),
43        SIGNED_16 (5, 2),
44        SIGNED_32 (6, 4),
45        //SIGNED_64 (7, 8),
46        UNSIGNED_8 (8, 1),
47        UNSIGNED_16 (9, 2),
48        UNSIGNED_32 (10, 4),
49        //UNSIGNED_64 (11, 8),
50
51        BOOLEAN(12, 1),
52
53        UNSIGNED_5_6_5 (13, 2),
54        UNSIGNED_5_5_5_1 (14, 2),
55        UNSIGNED_4_4_4_4 (15, 2),
56
57        RS_ELEMENT (16, 4),
58        RS_TYPE (17, 4),
59        RS_ALLOCATION (18, 4),
60        RS_SAMPLER (19, 4),
61        RS_SCRIPT (20, 4),
62        RS_MESH (21, 4),
63        RS_PROGRAM_FRAGMENT (22, 4),
64        RS_PROGRAM_VERTEX (23, 4),
65        RS_PROGRAM_RASTER (24, 4),
66        RS_PROGRAM_STORE (25, 4);
67
68        int mID;
69        int mSize;
70        DataType(int id, int size) {
71            mID = id;
72            mSize = size;
73        }
74    }
75
76    public enum DataKind {
77        USER (0),
78
79        PIXEL_L (7),
80        PIXEL_A (8),
81        PIXEL_LA (9),
82        PIXEL_RGB (10),
83        PIXEL_RGBA (11);
84
85        int mID;
86        DataKind(int id) {
87            mID = id;
88        }
89    }
90
91    public static Element BOOLEAN(RenderScript rs) {
92        if(rs.mElement_BOOLEAN == null) {
93            rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
94        }
95        return rs.mElement_BOOLEAN;
96    }
97
98    public static Element U8(RenderScript rs) {
99        if(rs.mElement_U8 == null) {
100            rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8);
101        }
102        return rs.mElement_U8;
103    }
104
105    public static Element I8(RenderScript rs) {
106        if(rs.mElement_I8 == null) {
107            rs.mElement_I8 = createUser(rs, DataType.SIGNED_8);
108        }
109        return rs.mElement_I8;
110    }
111
112    public static Element U16(RenderScript rs) {
113        if(rs.mElement_U16 == null) {
114            rs.mElement_U16 = createUser(rs, DataType.UNSIGNED_16);
115        }
116        return rs.mElement_U16;
117    }
118
119    public static Element I16(RenderScript rs) {
120        if(rs.mElement_I16 == null) {
121            rs.mElement_I16 = createUser(rs, DataType.SIGNED_16);
122        }
123        return rs.mElement_I16;
124    }
125
126    public static Element U32(RenderScript rs) {
127        if(rs.mElement_U32 == null) {
128            rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32);
129        }
130        return rs.mElement_U32;
131    }
132
133    public static Element I32(RenderScript rs) {
134        if(rs.mElement_I32 == null) {
135            rs.mElement_I32 = createUser(rs, DataType.SIGNED_32);
136        }
137        return rs.mElement_I32;
138    }
139
140    public static Element F32(RenderScript rs) {
141        if(rs.mElement_F32 == null) {
142            rs.mElement_F32 = createUser(rs, DataType.FLOAT_32);
143        }
144        return rs.mElement_F32;
145    }
146
147    public static Element ELEMENT(RenderScript rs) {
148        if(rs.mElement_ELEMENT == null) {
149            rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT);
150        }
151        return rs.mElement_ELEMENT;
152    }
153
154    public static Element TYPE(RenderScript rs) {
155        if(rs.mElement_TYPE == null) {
156            rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE);
157        }
158        return rs.mElement_TYPE;
159    }
160
161    public static Element ALLOCATION(RenderScript rs) {
162        if(rs.mElement_ALLOCATION == null) {
163            rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION);
164        }
165        return rs.mElement_ALLOCATION;
166    }
167
168    public static Element SAMPLER(RenderScript rs) {
169        if(rs.mElement_SAMPLER == null) {
170            rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER);
171        }
172        return rs.mElement_SAMPLER;
173    }
174
175    public static Element SCRIPT(RenderScript rs) {
176        if(rs.mElement_SCRIPT == null) {
177            rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT);
178        }
179        return rs.mElement_SCRIPT;
180    }
181
182    public static Element MESH(RenderScript rs) {
183        if(rs.mElement_MESH == null) {
184            rs.mElement_MESH = createUser(rs, DataType.RS_MESH);
185        }
186        return rs.mElement_MESH;
187    }
188
189    public static Element PROGRAM_FRAGMENT(RenderScript rs) {
190        if(rs.mElement_PROGRAM_FRAGMENT == null) {
191            rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT);
192        }
193        return rs.mElement_PROGRAM_FRAGMENT;
194    }
195
196    public static Element PROGRAM_VERTEX(RenderScript rs) {
197        if(rs.mElement_PROGRAM_VERTEX == null) {
198            rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX);
199        }
200        return rs.mElement_PROGRAM_VERTEX;
201    }
202
203    public static Element PROGRAM_RASTER(RenderScript rs) {
204        if(rs.mElement_PROGRAM_RASTER == null) {
205            rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER);
206        }
207        return rs.mElement_PROGRAM_RASTER;
208    }
209
210    public static Element PROGRAM_STORE(RenderScript rs) {
211        if(rs.mElement_PROGRAM_STORE == null) {
212            rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE);
213        }
214        return rs.mElement_PROGRAM_STORE;
215    }
216
217
218    public static Element A_8(RenderScript rs) {
219        if(rs.mElement_A_8 == null) {
220            rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
221        }
222        return rs.mElement_A_8;
223    }
224
225    public static Element RGB_565(RenderScript rs) {
226        if(rs.mElement_RGB_565 == null) {
227            rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
228        }
229        return rs.mElement_RGB_565;
230    }
231
232    public static Element RGB_888(RenderScript rs) {
233        if(rs.mElement_RGB_888 == null) {
234            rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
235        }
236        return rs.mElement_RGB_888;
237    }
238
239    public static Element RGBA_5551(RenderScript rs) {
240        if(rs.mElement_RGBA_5551 == null) {
241            rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
242        }
243        return rs.mElement_RGBA_5551;
244    }
245
246    public static Element RGBA_4444(RenderScript rs) {
247        if(rs.mElement_RGBA_4444 == null) {
248            rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
249        }
250        return rs.mElement_RGBA_4444;
251    }
252
253    public static Element RGBA_8888(RenderScript rs) {
254        if(rs.mElement_RGBA_8888 == null) {
255            rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
256        }
257        return rs.mElement_RGBA_8888;
258    }
259
260    public static Element F32_2(RenderScript rs) {
261        if(rs.mElement_FLOAT_2 == null) {
262            rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2);
263        }
264        return rs.mElement_FLOAT_2;
265    }
266
267    public static Element F32_3(RenderScript rs) {
268        if(rs.mElement_FLOAT_3 == null) {
269            rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3);
270        }
271        return rs.mElement_FLOAT_3;
272    }
273
274    public static Element F32_4(RenderScript rs) {
275        if(rs.mElement_FLOAT_4 == null) {
276            rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4);
277        }
278        return rs.mElement_FLOAT_4;
279    }
280
281    public static Element U8_4(RenderScript rs) {
282        if(rs.mElement_UCHAR_4 == null) {
283            rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4);
284        }
285        return rs.mElement_UCHAR_4;
286    }
287
288
289    Element(RenderScript rs, Element[] e, String[] n) {
290        super(rs);
291        mSize = 0;
292        mElements = e;
293        mElementNames = n;
294        int[] ids = new int[mElements.length];
295        for (int ct = 0; ct < mElements.length; ct++ ) {
296            mSize += mElements[ct].mSize;
297            ids[ct] = mElements[ct].mID;
298        }
299        mID = rs.nElementCreate2(ids, mElementNames);
300    }
301
302    Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
303        super(rs);
304        mSize = dt.mSize * size;
305        mType = dt;
306        mKind = dk;
307        mNormalized = norm;
308        mVectorSize = size;
309        mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
310    }
311
312    Element(RenderScript rs, int id) {
313        super(rs);
314        mID = id;
315    }
316
317    @Override
318    void updateFromNative() {
319
320        // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
321        int[] dataBuffer = new int[5];
322        mRS.nElementGetNativeData(mID, dataBuffer);
323        for (DataType dt: DataType.values()) {
324            if(dt.mID == dataBuffer[0]){
325                mType = dt;
326            }
327        }
328        for (DataKind dk: DataKind.values()) {
329            if(dk.mID == dataBuffer[1]){
330                mKind = dk;
331            }
332        }
333
334        mNormalized = dataBuffer[2] == 1 ? true : false;
335        mVectorSize = dataBuffer[3];
336        int numSubElements = dataBuffer[4];
337        if(numSubElements > 0) {
338            mElements = new Element[numSubElements];
339            mElementNames = new String[numSubElements];
340
341            int[] subElementIds = new int[numSubElements];
342            mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
343            for(int i = 0; i < numSubElements; i ++) {
344                mElements[i] = new Element(mRS, subElementIds[i]);
345                mElements[i].updateFromNative();
346            }
347        }
348
349    }
350
351    public void destroy() throws IllegalStateException {
352        super.destroy();
353    }
354
355    /////////////////////////////////////////
356    public static Element createUser(RenderScript rs, DataType dt) {
357        return new Element(rs, dt, DataKind.USER, false, 1);
358    }
359
360    public static Element createVector(RenderScript rs, DataType dt, int size) {
361        if (size < 2 || size > 4) {
362            throw new IllegalArgumentException("Bad size");
363        }
364        return new Element(rs, dt, DataKind.USER, false, size);
365    }
366
367    public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
368        if (!(dk == DataKind.PIXEL_L ||
369              dk == DataKind.PIXEL_A ||
370              dk == DataKind.PIXEL_LA ||
371              dk == DataKind.PIXEL_RGB ||
372              dk == DataKind.PIXEL_RGBA)) {
373            throw new IllegalArgumentException("Unsupported DataKind");
374        }
375        if (!(dt == DataType.UNSIGNED_8 ||
376              dt == DataType.UNSIGNED_5_6_5 ||
377              dt == DataType.UNSIGNED_4_4_4_4 ||
378              dt == DataType.UNSIGNED_5_5_5_1)) {
379            throw new IllegalArgumentException("Unsupported DataType");
380        }
381        if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
382            throw new IllegalArgumentException("Bad kind and type combo");
383        }
384        if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
385            throw new IllegalArgumentException("Bad kind and type combo");
386        }
387        if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
388            throw new IllegalArgumentException("Bad kind and type combo");
389        }
390
391        int size = 1;
392        if (dk == DataKind.PIXEL_LA) {
393            size = 2;
394        }
395        if (dk == DataKind.PIXEL_RGB) {
396            size = 3;
397        }
398        if (dk == DataKind.PIXEL_RGBA) {
399            size = 4;
400        }
401
402        return new Element(rs, dt, dk, true, size);
403    }
404
405    public static class Builder {
406        RenderScript mRS;
407        Element[] mElements;
408        String[] mElementNames;
409        int mCount;
410
411        public Builder(RenderScript rs) {
412            mRS = rs;
413            mCount = 0;
414            mElements = new Element[8];
415            mElementNames = new String[8];
416        }
417
418        public void add(Element element, String name) {
419            if(mCount == mElements.length) {
420                Element[] e = new Element[mCount + 8];
421                String[] s = new String[mCount + 8];
422                System.arraycopy(mElements, 0, e, 0, mCount);
423                System.arraycopy(mElementNames, 0, s, 0, mCount);
424                mElements = e;
425                mElementNames = s;
426            }
427            mElements[mCount] = element;
428            mElementNames[mCount] = name;
429            mCount++;
430        }
431
432        public Element create() {
433            mRS.validate();
434            Element[] ein = new Element[mCount];
435            String[] sin = new String[mCount];
436            java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
437            java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
438            return new Element(mRS, ein, sin);
439        }
440    }
441
442    static void initPredefined(RenderScript rs) {
443        int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
444                                   DataKind.PIXEL_A.mID, true, 1);
445        int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
446                                         DataKind.PIXEL_RGBA.mID, true, 4);
447        int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
448                                         DataKind.PIXEL_RGBA.mID, true, 4);
449        int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
450                                       DataKind.PIXEL_RGB.mID, true, 3);
451        rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
452    }
453}
454
455