Element.java revision 0de9444aa6c25d2c586e8204a6168d10e67376e0
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(int id, RenderScript rs, Element[] e, String[] n) {
290        super(id, rs);
291        mSize = 0;
292        mElements = e;
293        mElementNames = n;
294        for (int ct = 0; ct < mElements.length; ct++ ) {
295            mSize += mElements[ct].mSize;
296        }
297    }
298
299    Element(int id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
300        super(id, rs);
301        mSize = dt.mSize * size;
302        mType = dt;
303        mKind = dk;
304        mNormalized = norm;
305        mVectorSize = size;
306    }
307
308    Element(int id, RenderScript rs) {
309        super(id, rs);
310    }
311
312    @Override
313    void updateFromNative() {
314
315        // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
316        int[] dataBuffer = new int[5];
317        mRS.nElementGetNativeData(mID, dataBuffer);
318
319        mNormalized = dataBuffer[2] == 1 ? true : false;
320        mVectorSize = dataBuffer[3];
321        mSize = 0;
322        for (DataType dt: DataType.values()) {
323            if(dt.mID == dataBuffer[0]){
324                mType = dt;
325                mSize = mType.mSize * mVectorSize;
326            }
327        }
328        for (DataKind dk: DataKind.values()) {
329            if(dk.mID == dataBuffer[1]){
330                mKind = dk;
331            }
332        }
333
334        int numSubElements = dataBuffer[4];
335        if(numSubElements > 0) {
336            mElements = new Element[numSubElements];
337            mElementNames = new String[numSubElements];
338
339            int[] subElementIds = new int[numSubElements];
340            mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
341            for(int i = 0; i < numSubElements; i ++) {
342                mElements[i] = new Element(subElementIds[i], mRS);
343                mElements[i].updateFromNative();
344                mSize += mElements[i].mSize;
345            }
346        }
347
348    }
349
350    public void destroy() throws IllegalStateException {
351        super.destroy();
352    }
353
354    /////////////////////////////////////////
355    public static Element createUser(RenderScript rs, DataType dt) {
356        DataKind dk = DataKind.USER;
357        boolean norm = false;
358        int vecSize = 1;
359        int id = rs.nElementCreate(dt.mID, dk.mID, norm, vecSize);
360        return new Element(id, rs, dt, dk, norm, vecSize);
361    }
362
363    public static Element createVector(RenderScript rs, DataType dt, int size) {
364        if (size < 2 || size > 4) {
365            throw new IllegalArgumentException("Bad size");
366        }
367        DataKind dk = DataKind.USER;
368        boolean norm = false;
369        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
370        return new Element(id, rs, dt, dk, norm, size);
371    }
372
373    public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
374        if (!(dk == DataKind.PIXEL_L ||
375              dk == DataKind.PIXEL_A ||
376              dk == DataKind.PIXEL_LA ||
377              dk == DataKind.PIXEL_RGB ||
378              dk == DataKind.PIXEL_RGBA)) {
379            throw new IllegalArgumentException("Unsupported DataKind");
380        }
381        if (!(dt == DataType.UNSIGNED_8 ||
382              dt == DataType.UNSIGNED_5_6_5 ||
383              dt == DataType.UNSIGNED_4_4_4_4 ||
384              dt == DataType.UNSIGNED_5_5_5_1)) {
385            throw new IllegalArgumentException("Unsupported DataType");
386        }
387        if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
388            throw new IllegalArgumentException("Bad kind and type combo");
389        }
390        if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
391            throw new IllegalArgumentException("Bad kind and type combo");
392        }
393        if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
394            throw new IllegalArgumentException("Bad kind and type combo");
395        }
396
397        int size = 1;
398        if (dk == DataKind.PIXEL_LA) {
399            size = 2;
400        }
401        if (dk == DataKind.PIXEL_RGB) {
402            size = 3;
403        }
404        if (dk == DataKind.PIXEL_RGBA) {
405            size = 4;
406        }
407
408        boolean norm = true;
409        int id = rs.nElementCreate(dt.mID, dk.mID, norm, size);
410        return new Element(id, rs, dt, dk, norm, size);
411    }
412
413    public static class Builder {
414        RenderScript mRS;
415        Element[] mElements;
416        String[] mElementNames;
417        int mCount;
418
419        public Builder(RenderScript rs) {
420            mRS = rs;
421            mCount = 0;
422            mElements = new Element[8];
423            mElementNames = new String[8];
424        }
425
426        public void add(Element element, String name) {
427            if(mCount == mElements.length) {
428                Element[] e = new Element[mCount + 8];
429                String[] s = new String[mCount + 8];
430                System.arraycopy(mElements, 0, e, 0, mCount);
431                System.arraycopy(mElementNames, 0, s, 0, mCount);
432                mElements = e;
433                mElementNames = s;
434            }
435            mElements[mCount] = element;
436            mElementNames[mCount] = name;
437            mCount++;
438        }
439
440        public Element create() {
441            mRS.validate();
442            Element[] ein = new Element[mCount];
443            String[] sin = new String[mCount];
444            java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
445            java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
446
447            int[] ids = new int[ein.length];
448            for (int ct = 0; ct < ein.length; ct++ ) {
449                ids[ct] = ein[ct].mID;
450            }
451            int id = mRS.nElementCreate2(ids, sin);
452            return new Element(id, mRS, ein, sin);
453        }
454    }
455
456    static void initPredefined(RenderScript rs) {
457        int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
458                                   DataKind.PIXEL_A.mID, true, 1);
459        int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
460                                         DataKind.PIXEL_RGBA.mID, true, 4);
461        int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
462                                         DataKind.PIXEL_RGBA.mID, true, 4);
463        int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
464                                       DataKind.PIXEL_RGB.mID, true, 3);
465        rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
466    }
467}
468
469