• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.renderscript;
18 
19 import java.lang.reflect.Field;
20 
21 /**
22  * @hide
23  *
24  **/
25 public class Element extends BaseObj {
26     int mSize;
27     Element[] mElements;
28     String[] mElementNames;
29 
30     DataType mType;
31     DataKind mKind;
32     boolean mNormalized;
33     int mVectorSize;
34 
getSizeBytes()35     int getSizeBytes() {return mSize;}
36 
37     public enum DataType {
38         //FLOAT_16 (1, 2),
39         FLOAT_32 (2, 4),
40         //FLOAT_64 (3, 8),
41         SIGNED_8 (4, 1),
42         SIGNED_16 (5, 2),
43         SIGNED_32 (6, 4),
44         //SIGNED_64 (7, 8),
45         UNSIGNED_8 (8, 1),
46         UNSIGNED_16 (9, 2),
47         UNSIGNED_32 (10, 4),
48         //UNSIGNED_64 (11, 8),
49 
50         UNSIGNED_5_6_5 (12, 2),
51         UNSIGNED_5_5_5_1 (13, 2),
52         UNSIGNED_4_4_4_4 (14, 2),
53 
54         RS_ELEMENT (15, 4),
55         RS_TYPE (16, 4),
56         RS_ALLOCATION (17, 4),
57         RS_SAMPLER (18, 4),
58         RS_SCRIPT (19, 4),
59         RS_MESH (20, 4),
60         RS_PROGRAM_FRAGMENT (21, 4),
61         RS_PROGRAM_VERTEX (22, 4),
62         RS_PROGRAM_RASTER (23, 4),
63         RS_PROGRAM_STORE (24, 4);
64 
65         int mID;
66         int mSize;
DataType(int id, int size)67         DataType(int id, int size) {
68             mID = id;
69             mSize = size;
70         }
71     }
72 
73     public enum DataKind {
74         USER (0),
75         COLOR (1),
76         POSITION (2),
77         TEXTURE (3),
78         NORMAL (4),
79         INDEX (5),
80         POINT_SIZE(6),
81 
82         PIXEL_L (7),
83         PIXEL_A (8),
84         PIXEL_LA (9),
85         PIXEL_RGB (10),
86         PIXEL_RGBA (11);
87 
88         int mID;
DataKind(int id)89         DataKind(int id) {
90             mID = id;
91         }
92     }
93 
USER_U8(RenderScript rs)94     public static Element USER_U8(RenderScript rs) {
95         if(rs.mElement_USER_U8 == null) {
96             rs.mElement_USER_U8 = createUser(rs, DataType.UNSIGNED_8);
97         }
98         return rs.mElement_USER_U8;
99     }
100 
USER_I8(RenderScript rs)101     public static Element USER_I8(RenderScript rs) {
102         if(rs.mElement_USER_I8 == null) {
103             rs.mElement_USER_I8 = createUser(rs, DataType.SIGNED_8);
104         }
105         return rs.mElement_USER_I8;
106     }
107 
USER_U32(RenderScript rs)108     public static Element USER_U32(RenderScript rs) {
109         if(rs.mElement_USER_U32 == null) {
110             rs.mElement_USER_U32 = createUser(rs, DataType.UNSIGNED_32);
111         }
112         return rs.mElement_USER_U32;
113     }
114 
USER_I32(RenderScript rs)115     public static Element USER_I32(RenderScript rs) {
116         if(rs.mElement_USER_I32 == null) {
117             rs.mElement_USER_I32 = createUser(rs, DataType.SIGNED_32);
118         }
119         return rs.mElement_USER_I32;
120     }
121 
USER_F32(RenderScript rs)122     public static Element USER_F32(RenderScript rs) {
123         if(rs.mElement_USER_F32 == null) {
124             rs.mElement_USER_F32 = createUser(rs, DataType.FLOAT_32);
125         }
126         return rs.mElement_USER_F32;
127     }
128 
A_8(RenderScript rs)129     public static Element A_8(RenderScript rs) {
130         if(rs.mElement_A_8 == null) {
131             rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
132         }
133         return rs.mElement_A_8;
134     }
135 
RGB_565(RenderScript rs)136     public static Element RGB_565(RenderScript rs) {
137         if(rs.mElement_RGB_565 == null) {
138             rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
139         }
140         return rs.mElement_RGB_565;
141     }
142 
RGB_888(RenderScript rs)143     public static Element RGB_888(RenderScript rs) {
144         if(rs.mElement_RGB_888 == null) {
145             rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
146         }
147         return rs.mElement_RGB_888;
148     }
149 
RGBA_5551(RenderScript rs)150     public static Element RGBA_5551(RenderScript rs) {
151         if(rs.mElement_RGBA_5551 == null) {
152             rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
153         }
154         return rs.mElement_RGBA_5551;
155     }
156 
RGBA_4444(RenderScript rs)157     public static Element RGBA_4444(RenderScript rs) {
158         if(rs.mElement_RGBA_4444 == null) {
159             rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
160         }
161         return rs.mElement_RGBA_4444;
162     }
163 
RGBA_8888(RenderScript rs)164     public static Element RGBA_8888(RenderScript rs) {
165         if(rs.mElement_RGBA_8888 == null) {
166             rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
167         }
168         return rs.mElement_RGBA_8888;
169     }
170 
INDEX_16(RenderScript rs)171     public static Element INDEX_16(RenderScript rs) {
172         if(rs.mElement_INDEX_16 == null) {
173             rs.mElement_INDEX_16 = createIndex(rs);
174         }
175         return rs.mElement_INDEX_16;
176     }
177 
ATTRIB_POSITION_2(RenderScript rs)178     public static Element ATTRIB_POSITION_2(RenderScript rs) {
179         if(rs.mElement_POSITION_2 == null) {
180             rs.mElement_POSITION_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 2);
181         }
182         return rs.mElement_POSITION_2;
183     }
184 
ATTRIB_POSITION_3(RenderScript rs)185     public static Element ATTRIB_POSITION_3(RenderScript rs) {
186         if(rs.mElement_POSITION_3 == null) {
187             rs.mElement_POSITION_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 3);
188         }
189         return rs.mElement_POSITION_3;
190     }
191 
ATTRIB_TEXTURE_2(RenderScript rs)192     public static Element ATTRIB_TEXTURE_2(RenderScript rs) {
193         if(rs.mElement_TEXTURE_2 == null) {
194             rs.mElement_TEXTURE_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.TEXTURE, 2);
195         }
196         return rs.mElement_TEXTURE_2;
197     }
198 
ATTRIB_NORMAL_3(RenderScript rs)199     public static Element ATTRIB_NORMAL_3(RenderScript rs) {
200         if(rs.mElement_NORMAL_3 == null) {
201             rs.mElement_NORMAL_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.NORMAL, 3);
202         }
203         return rs.mElement_NORMAL_3;
204     }
205 
ATTRIB_COLOR_U8_4(RenderScript rs)206     public static Element ATTRIB_COLOR_U8_4(RenderScript rs) {
207         if(rs.mElement_COLOR_U8_4 == null) {
208             rs.mElement_COLOR_U8_4 = createAttrib(rs, DataType.UNSIGNED_8, DataKind.COLOR, 4);
209         }
210         return rs.mElement_COLOR_U8_4;
211     }
212 
ATTRIB_COLOR_F32_4(RenderScript rs)213     public static Element ATTRIB_COLOR_F32_4(RenderScript rs) {
214         if(rs.mElement_COLOR_F32_4 == null) {
215             rs.mElement_COLOR_F32_4 = createAttrib(rs, DataType.FLOAT_32, DataKind.COLOR, 4);
216         }
217         return rs.mElement_COLOR_F32_4;
218     }
219 
Element(RenderScript rs, Element[] e, String[] n)220     Element(RenderScript rs, Element[] e, String[] n) {
221         super(rs);
222         mSize = 0;
223         mElements = e;
224         mElementNames = n;
225         int[] ids = new int[mElements.length];
226         for (int ct = 0; ct < mElements.length; ct++ ) {
227             mSize += mElements[ct].mSize;
228             ids[ct] = mElements[ct].mID;
229         }
230         mID = rs.nElementCreate2(ids, mElementNames);
231     }
232 
Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size)233     Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
234         super(rs);
235         mSize = dt.mSize * size;
236         mType = dt;
237         mKind = dk;
238         mNormalized = norm;
239         mVectorSize = size;
240         mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
241     }
242 
destroy()243     public void destroy() throws IllegalStateException {
244         super.destroy();
245     }
246 
createFromClass(RenderScript rs, Class c)247     public static Element createFromClass(RenderScript rs, Class c) {
248         rs.validate();
249         Field[] fields = c.getFields();
250         Builder b = new Builder(rs);
251 
252         for(Field f: fields) {
253             Class fc = f.getType();
254             if(fc == int.class) {
255                 b.add(createUser(rs, DataType.SIGNED_32), f.getName());
256             } else if(fc == short.class) {
257                 b.add(createUser(rs, DataType.SIGNED_16), f.getName());
258             } else if(fc == byte.class) {
259                 b.add(createUser(rs, DataType.SIGNED_8), f.getName());
260             } else if(fc == float.class) {
261                 b.add(createUser(rs, DataType.FLOAT_32), f.getName());
262             } else {
263                 throw new IllegalArgumentException("Unkown field type");
264             }
265         }
266         return b.create();
267     }
268 
269 
270     /////////////////////////////////////////
createUser(RenderScript rs, DataType dt)271     public static Element createUser(RenderScript rs, DataType dt) {
272         return new Element(rs, dt, DataKind.USER, false, 1);
273     }
274 
createVector(RenderScript rs, DataType dt, int size)275     public static Element createVector(RenderScript rs, DataType dt, int size) {
276         if (size < 2 || size > 4) {
277             throw new IllegalArgumentException("Bad size");
278         }
279         return new Element(rs, dt, DataKind.USER, false, size);
280     }
281 
createIndex(RenderScript rs)282     public static Element createIndex(RenderScript rs) {
283         return new Element(rs, DataType.UNSIGNED_16, DataKind.INDEX, false, 1);
284     }
285 
createAttrib(RenderScript rs, DataType dt, DataKind dk, int size)286     public static Element createAttrib(RenderScript rs, DataType dt, DataKind dk, int size) {
287         if (!(dt == DataType.FLOAT_32 ||
288               dt == DataType.UNSIGNED_8 ||
289               dt == DataType.UNSIGNED_16 ||
290               dt == DataType.UNSIGNED_32 ||
291               dt == DataType.SIGNED_8 ||
292               dt == DataType.SIGNED_16 ||
293               dt == DataType.SIGNED_32)) {
294             throw new IllegalArgumentException("Unsupported DataType");
295         }
296 
297         if (!(dk == DataKind.COLOR ||
298               dk == DataKind.POSITION ||
299               dk == DataKind.TEXTURE ||
300               dk == DataKind.NORMAL ||
301               dk == DataKind.POINT_SIZE ||
302               dk == DataKind.USER)) {
303             throw new IllegalArgumentException("Unsupported DataKind");
304         }
305 
306         if (dk == DataKind.COLOR &&
307             ((dt != DataType.FLOAT_32 && dt != DataType.UNSIGNED_8) ||
308              size < 3 || size > 4)) {
309             throw new IllegalArgumentException("Bad combo");
310         }
311         if (dk == DataKind.POSITION && (size < 1 || size > 4)) {
312             throw new IllegalArgumentException("Bad combo");
313         }
314         if (dk == DataKind.TEXTURE &&
315             (dt != DataType.FLOAT_32 || size < 1 || size > 4)) {
316             throw new IllegalArgumentException("Bad combo");
317         }
318         if (dk == DataKind.NORMAL &&
319             (dt != DataType.FLOAT_32 || size != 3)) {
320             throw new IllegalArgumentException("Bad combo");
321         }
322         if (dk == DataKind.POINT_SIZE &&
323             (dt != DataType.FLOAT_32 || size != 1)) {
324             throw new IllegalArgumentException("Bad combo");
325         }
326 
327         boolean norm = false;
328         if (dk == DataKind.COLOR && dt == DataType.UNSIGNED_8) {
329             norm = true;
330         }
331 
332         return new Element(rs, dt, dk, norm, size);
333     }
334 
createPixel(RenderScript rs, DataType dt, DataKind dk)335     public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
336         if (!(dk == DataKind.PIXEL_L ||
337               dk == DataKind.PIXEL_A ||
338               dk == DataKind.PIXEL_LA ||
339               dk == DataKind.PIXEL_RGB ||
340               dk == DataKind.PIXEL_RGBA)) {
341             throw new IllegalArgumentException("Unsupported DataKind");
342         }
343         if (!(dt == DataType.UNSIGNED_8 ||
344               dt == DataType.UNSIGNED_5_6_5 ||
345               dt == DataType.UNSIGNED_4_4_4_4 ||
346               dt == DataType.UNSIGNED_5_5_5_1)) {
347             throw new IllegalArgumentException("Unsupported DataType");
348         }
349         if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
350             throw new IllegalArgumentException("Bad kind and type combo");
351         }
352         if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
353             throw new IllegalArgumentException("Bad kind and type combo");
354         }
355         if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
356             throw new IllegalArgumentException("Bad kind and type combo");
357         }
358 
359         int size = 1;
360         if (dk == DataKind.PIXEL_LA) {
361             size = 2;
362         }
363         if (dk == DataKind.PIXEL_RGB) {
364             size = 3;
365         }
366         if (dk == DataKind.PIXEL_RGBA) {
367             size = 4;
368         }
369 
370         return new Element(rs, dt, dk, true, size);
371     }
372 
373     public static class Builder {
374         RenderScript mRS;
375         Element[] mElements;
376         String[] mElementNames;
377         int mCount;
378 
Builder(RenderScript rs)379         public Builder(RenderScript rs) {
380             mRS = rs;
381             mCount = 0;
382             mElements = new Element[8];
383             mElementNames = new String[8];
384         }
385 
add(Element element, String name)386         public void add(Element element, String name) {
387             if(mCount == mElements.length) {
388                 Element[] e = new Element[mCount + 8];
389                 String[] s = new String[mCount + 8];
390                 System.arraycopy(mElements, 0, e, 0, mCount);
391                 System.arraycopy(mElementNames, 0, s, 0, mCount);
392                 mElements = e;
393                 mElementNames = s;
394             }
395             mElements[mCount] = element;
396             mElementNames[mCount] = name;
397             mCount++;
398         }
399 
create()400         public Element create() {
401             mRS.validate();
402             Element[] ein = new Element[mCount];
403             String[] sin = new String[mCount];
404             java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
405             java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
406             return new Element(mRS, ein, sin);
407         }
408     }
409 
initPredefined(RenderScript rs)410     static void initPredefined(RenderScript rs) {
411         int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
412                                    DataKind.PIXEL_A.mID, true, 1);
413         int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
414                                          DataKind.PIXEL_RGBA.mID, true, 4);
415         int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
416                                          DataKind.PIXEL_RGBA.mID, true, 4);
417         int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
418                                        DataKind.PIXEL_RGB.mID, true, 3);
419         rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
420     }
421 }
422 
423