• 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     Entry[] mEntries;
28 
getSizeBytes()29     int getSizeBytes() {
30         return mSize;
31     }
getComponentCount()32     int getComponentCount() {
33         return mEntries.length;
34     }
getComponentDataType(int num)35     Element.DataType getComponentDataType(int num) {
36         return mEntries[num].mType;
37     }
getComponentDataKind(int num)38     Element.DataKind getComponentDataKind(int num) {
39         return mEntries[num].mKind;
40     }
getComponentIsNormalized(int num)41     boolean getComponentIsNormalized(int num) {
42         return mEntries[num].mIsNormalized;
43     }
getComponentBits(int num)44     int getComponentBits(int num) {
45         return mEntries[num].mBits;
46     }
getComponentName(int num)47     String getComponentName(int num) {
48         return mEntries[num].mName;
49     }
50 
51     static class Entry {
52         //Element mElement;
53         Element.DataType mType;
54         Element.DataKind mKind;
55         boolean mIsNormalized;
56         int mBits;
57         String mName;
58 
59         //Entry(Element e, int bits) {
60             //mElement = e;
61             //int mBits = bits;
62         //}
63 
Entry(DataType dt, DataKind dk, boolean isNorm, int bits, String name)64         Entry(DataType dt, DataKind dk, boolean isNorm, int bits, String name) {
65             mType = dt;
66             mKind = dk;
67             mIsNormalized = isNorm;
68             mBits = bits;
69             mName = name;
70         }
71     }
72 
USER_U8(RenderScript rs)73     public static Element USER_U8(RenderScript rs) {
74         if(rs.mElement_USER_U8 == null) {
75             rs.mElement_USER_U8 = new Element(rs, 1);
76             rs.mElement_USER_U8.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.USER, false, 8, null);
77             rs.mElement_USER_U8.init();
78         }
79         return rs.mElement_USER_U8;
80     }
81 
USER_I8(RenderScript rs)82     public static Element USER_I8(RenderScript rs) {
83         if(rs.mElement_USER_I8 == null) {
84             rs.mElement_USER_I8 = new Element(rs, 1);
85             rs.mElement_USER_I8.mEntries[0] = new Entry(DataType.SIGNED, DataKind.USER, false, 8, null);
86             rs.mElement_USER_I8.init();
87         }
88         return rs.mElement_USER_I8;
89     }
90 
USER_U16(RenderScript rs)91     public static Element USER_U16(RenderScript rs) {
92         if(rs.mElement_USER_U16 == null) {
93             rs.mElement_USER_U16 = new Element(rs, 1);
94             rs.mElement_USER_U16.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.USER, false, 16, null);
95             rs.mElement_USER_U16.init();
96         }
97         return rs.mElement_USER_U16;
98     }
99 
USER_I16(RenderScript rs)100     public static Element USER_I16(RenderScript rs) {
101         if(rs.mElement_USER_I16 == null) {
102             rs.mElement_USER_I16 = new Element(rs, 1);
103             rs.mElement_USER_I16.mEntries[0] = new Entry(DataType.SIGNED, DataKind.USER, false, 16, null);
104             rs.mElement_USER_I16.init();
105         }
106         return rs.mElement_USER_I16;
107     }
108 
USER_U32(RenderScript rs)109     public static Element USER_U32(RenderScript rs) {
110         if(rs.mElement_USER_U32 == null) {
111             rs.mElement_USER_U32 = new Element(rs, 1);
112             rs.mElement_USER_U32.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.USER, false, 32, null);
113             rs.mElement_USER_U32.init();
114         }
115         return rs.mElement_USER_U32;
116     }
117 
USER_I32(RenderScript rs)118     public static Element USER_I32(RenderScript rs) {
119         if(rs.mElement_USER_I32 == null) {
120             rs.mElement_USER_I32 = new Element(rs, 1);
121             rs.mElement_USER_I32.mEntries[0] = new Entry(DataType.SIGNED, DataKind.USER, false, 32, null);
122             rs.mElement_USER_I32.init();
123         }
124         return rs.mElement_USER_I32;
125     }
126 
USER_F32(RenderScript rs)127     public static Element USER_F32(RenderScript rs) {
128         if(rs.mElement_USER_FLOAT == null) {
129             rs.mElement_USER_FLOAT = new Element(rs, 1);
130             rs.mElement_USER_FLOAT.mEntries[0] = new Entry(DataType.FLOAT, DataKind.USER, false, 32, null);
131             rs.mElement_USER_FLOAT.init();
132         }
133         return rs.mElement_USER_FLOAT;
134     }
135 
A_8(RenderScript rs)136     public static Element A_8(RenderScript rs) {
137         if(rs.mElement_A_8 == null) {
138             rs.mElement_A_8 = new Element(rs, 1);
139             rs.mElement_A_8.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.ALPHA, true, 8, "a");
140             rs.mElement_A_8.init();
141         }
142         return rs.mElement_A_8;
143     }
144 
RGB_565(RenderScript rs)145     public static Element RGB_565(RenderScript rs) {
146         if(rs.mElement_RGB_565 == null) {
147             rs.mElement_RGB_565 = new Element(rs, 3);
148             rs.mElement_RGB_565.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.RED, true, 5, "r");
149             rs.mElement_RGB_565.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.GREEN, true, 6, "g");
150             rs.mElement_RGB_565.mEntries[2] = new Entry(DataType.UNSIGNED, DataKind.BLUE, true, 5, "b");
151             rs.mElement_RGB_565.init();
152         }
153         return rs.mElement_RGB_565;
154     }
155 
RGB_888(RenderScript rs)156     public static Element RGB_888(RenderScript rs) {
157         if(rs.mElement_RGB_888 == null) {
158             rs.mElement_RGB_888 = new Element(rs, 3);
159             rs.mElement_RGB_888.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.RED, true, 8, "r");
160             rs.mElement_RGB_888.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.GREEN, true, 8, "g");
161             rs.mElement_RGB_888.mEntries[2] = new Entry(DataType.UNSIGNED, DataKind.BLUE, true, 8, "b");
162             rs.mElement_RGB_888.init();
163         }
164         return rs.mElement_RGB_888;
165     }
166 
RGBA_5551(RenderScript rs)167     public static Element RGBA_5551(RenderScript rs) {
168         if(rs.mElement_RGBA_5551 == null) {
169             rs.mElement_RGBA_5551 = new Element(rs, 4);
170             rs.mElement_RGBA_5551.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.RED, true, 5, "r");
171             rs.mElement_RGBA_5551.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.GREEN, true, 5, "g");
172             rs.mElement_RGBA_5551.mEntries[2] = new Entry(DataType.UNSIGNED, DataKind.BLUE, true, 5, "b");
173             rs.mElement_RGBA_5551.mEntries[3] = new Entry(DataType.UNSIGNED, DataKind.ALPHA, true, 1, "a");
174             rs.mElement_RGBA_5551.init();
175         }
176         return rs.mElement_RGBA_5551;
177     }
178 
RGBA_4444(RenderScript rs)179     public static Element RGBA_4444(RenderScript rs) {
180         if(rs.mElement_RGBA_4444 == null) {
181             rs.mElement_RGBA_4444 = new Element(rs, 4);
182             rs.mElement_RGBA_4444.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.RED, true, 4, "r");
183             rs.mElement_RGBA_4444.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.GREEN, true, 4, "g");
184             rs.mElement_RGBA_4444.mEntries[2] = new Entry(DataType.UNSIGNED, DataKind.BLUE, true, 4, "b");
185             rs.mElement_RGBA_4444.mEntries[3] = new Entry(DataType.UNSIGNED, DataKind.ALPHA, true, 4, "a");
186             rs.mElement_RGBA_4444.init();
187         }
188         return rs.mElement_RGBA_4444;
189     }
190 
RGBA_8888(RenderScript rs)191     public static Element RGBA_8888(RenderScript rs) {
192         if(rs.mElement_RGBA_8888 == null) {
193             rs.mElement_RGBA_8888 = new Element(rs, 4);
194             rs.mElement_RGBA_8888.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.RED, true, 8, "r");
195             rs.mElement_RGBA_8888.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.GREEN, true, 8, "g");
196             rs.mElement_RGBA_8888.mEntries[2] = new Entry(DataType.UNSIGNED, DataKind.BLUE, true, 8, "b");
197             rs.mElement_RGBA_8888.mEntries[3] = new Entry(DataType.UNSIGNED, DataKind.ALPHA, true, 8, "a");
198             rs.mElement_RGBA_8888.init();
199         }
200         return rs.mElement_RGBA_8888;
201     }
202 
INDEX_16(RenderScript rs)203     public static Element INDEX_16(RenderScript rs) {
204         if(rs.mElement_INDEX_16 == null) {
205             rs.mElement_INDEX_16 = new Element(rs, 1);
206             rs.mElement_INDEX_16.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.INDEX, false, 16, "index");
207             rs.mElement_INDEX_16.init();
208         }
209         return rs.mElement_INDEX_16;
210     }
211 
XY_F32(RenderScript rs)212     public static Element XY_F32(RenderScript rs) {
213         if(rs.mElement_XY_F32 == null) {
214             rs.mElement_XY_F32 = new Element(rs, 2);
215             rs.mElement_XY_F32.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.X, false, 32, "x");
216             rs.mElement_XY_F32.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.Y, false, 32, "y");
217             rs.mElement_XY_F32.init();
218         }
219         return rs.mElement_XY_F32;
220     }
221 
XYZ_F32(RenderScript rs)222     public static Element XYZ_F32(RenderScript rs) {
223         if(rs.mElement_XYZ_F32 == null) {
224             rs.mElement_XYZ_F32 = new Element(rs, 3);
225             rs.mElement_XYZ_F32.mEntries[0] = new Entry(DataType.UNSIGNED, DataKind.X, false, 32, "x");
226             rs.mElement_XYZ_F32.mEntries[1] = new Entry(DataType.UNSIGNED, DataKind.Y, false, 32, "y");
227             rs.mElement_XYZ_F32.mEntries[2] = new Entry(DataType.UNSIGNED, DataKind.Z, false, 32, "z");
228             rs.mElement_XYZ_F32.init();
229         }
230         return rs.mElement_XYZ_F32;
231     }
232 
initPredefined(RenderScript rs)233     static void initPredefined(RenderScript rs) {
234         rs.nInitElements(A_8(rs).mID, RGBA_4444(rs).mID, RGBA_8888(rs).mID, RGB_565(rs).mID);
235     }
236 
237     public enum DataType {
238         FLOAT (0),
239         UNSIGNED (1),
240         SIGNED (2);
241 
242         int mID;
DataType(int id)243         DataType(int id) {
244             mID = id;
245         }
246     }
247 
248     public enum DataKind {
249         USER (0),
250         RED (1),
251         GREEN (2),
252         BLUE (3),
253         ALPHA (4),
254         LUMINANCE (5),
255         INTENSITY (6),
256         X (7),
257         Y (8),
258         Z (9),
259         W (10),
260         S (11),
261         T (12),
262         Q (13),
263         R (14),
264         NX (15),
265         NY (16),
266         NZ (17),
267         INDEX (18),
268         POINT_SIZE(19);
269 
270         int mID;
DataKind(int id)271         DataKind(int id) {
272             mID = id;
273         }
274     }
275 
Element(RenderScript rs, int count)276     Element(RenderScript rs, int count) {
277         super(rs);
278         mSize = 0;
279         mEntries = new Entry[count];
280     }
281 
destroy()282     public void destroy() throws IllegalStateException {
283         super.destroy();
284     }
285 
createFromClass(RenderScript rs, Class c)286     public static Element createFromClass(RenderScript rs, Class c) {
287         Field[] fields = c.getFields();
288         Builder b = new Builder(rs);
289 
290         for(Field f: fields) {
291             Class fc = f.getType();
292             if(fc == int.class) {
293                 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName());
294             } else if(fc == short.class) {
295                 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName());
296             } else if(fc == byte.class) {
297                 b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName());
298             } else if(fc == float.class) {
299                 b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName());
300             } else {
301                 throw new IllegalArgumentException("Unkown field type");
302             }
303         }
304         return b.create();
305     }
306 
internalCreate(RenderScript rs, Element e)307     static synchronized void internalCreate(RenderScript rs, Element e) {
308         rs.nElementBegin();
309         int bits = 0;
310         for (int ct=0; ct < e.mEntries.length; ct++) {
311             Entry en = e.mEntries[ct];
312             //if(en.mElement !=  null) {
313                 //rs.nElementAdd(en.mElement.mID);
314             //} else
315             {
316                 rs.nElementAdd(en.mKind.mID, en.mType.mID, en.mIsNormalized, en.mBits, en.mName);
317                 bits += en.mBits;
318             }
319         }
320         e.mID = rs.nElementCreate();
321         e.mSize = (bits + 7) >> 3;
322     }
323 
init()324     void init() {
325         internalCreate(mRS, this);
326     }
327 
328 
329     public static class Builder {
330         RenderScript mRS;
331         Entry[] mEntries;
332         int mEntryCount;
333 
Builder(RenderScript rs)334         public Builder(RenderScript rs) {
335             mRS = rs;
336             mEntryCount = 0;
337             mEntries = new Entry[8];
338         }
339 
addEntry(Entry e)340         void addEntry(Entry e) {
341             if(mEntries.length >= mEntryCount) {
342                 Entry[] en = new Entry[mEntryCount + 8];
343                 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
344                 mEntries = en;
345             }
346             mEntries[mEntryCount] = e;
347             mEntryCount++;
348         }
349 
350         //public Builder add(Element e) throws IllegalArgumentException {
351             //Entry en = new Entry(e, e.mSize * 8);
352             //addEntry(en);
353             //return this;
354         //}
355 
add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name)356         public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
357             Entry en = new Entry(dt, dk, isNormalized, bits, name);
358             addEntry(en);
359             return this;
360         }
361 
add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits)362         public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
363             add(dt, dk, isNormalized, bits, null);
364             return this;
365         }
366 
addFloat(Element.DataKind dk)367         public Builder addFloat(Element.DataKind dk) {
368             add(DataType.FLOAT, dk, false, 32, null);
369             return this;
370         }
371 
addFloat(Element.DataKind dk, String name)372         public Builder addFloat(Element.DataKind dk, String name) {
373             add(DataType.FLOAT, dk, false, 32, name);
374             return this;
375         }
376 
addFloatXY()377         public Builder addFloatXY() {
378             add(DataType.FLOAT, DataKind.X, false, 32, null);
379             add(DataType.FLOAT, DataKind.Y, false, 32, null);
380             return this;
381         }
382 
addFloatXY(String prefix)383         public Builder addFloatXY(String prefix) {
384             add(DataType.FLOAT, DataKind.X, false, 32, prefix + "x");
385             add(DataType.FLOAT, DataKind.Y, false, 32, prefix + "y");
386             return this;
387         }
388 
addFloatXYZ()389         public Builder addFloatXYZ() {
390             add(DataType.FLOAT, DataKind.X, false, 32, null);
391             add(DataType.FLOAT, DataKind.Y, false, 32, null);
392             add(DataType.FLOAT, DataKind.Z, false, 32, null);
393             return this;
394         }
395 
addFloatXYZ(String prefix)396         public Builder addFloatXYZ(String prefix) {
397             add(DataType.FLOAT, DataKind.X, false, 32, prefix + "x");
398             add(DataType.FLOAT, DataKind.Y, false, 32, prefix + "y");
399             add(DataType.FLOAT, DataKind.Z, false, 32, prefix + "z");
400             return this;
401         }
402 
addFloatST()403         public Builder addFloatST() {
404             add(DataType.FLOAT, DataKind.S, false, 32, null);
405             add(DataType.FLOAT, DataKind.T, false, 32, null);
406             return this;
407         }
408 
addFloatST(String prefix)409         public Builder addFloatST(String prefix) {
410             add(DataType.FLOAT, DataKind.S, false, 32, prefix + "s");
411             add(DataType.FLOAT, DataKind.T, false, 32, prefix + "t");
412             return this;
413         }
414 
addFloatNorm()415         public Builder addFloatNorm() {
416             add(DataType.FLOAT, DataKind.NX, false, 32, null);
417             add(DataType.FLOAT, DataKind.NY, false, 32, null);
418             add(DataType.FLOAT, DataKind.NZ, false, 32, null);
419             return this;
420         }
421 
addFloatNorm(String prefix)422         public Builder addFloatNorm(String prefix) {
423             add(DataType.FLOAT, DataKind.NX, false, 32, prefix + "nx");
424             add(DataType.FLOAT, DataKind.NY, false, 32, prefix + "ny");
425             add(DataType.FLOAT, DataKind.NZ, false, 32, prefix + "nz");
426             return this;
427         }
428 
addFloatPointSize()429         public Builder addFloatPointSize() {
430             add(DataType.FLOAT, DataKind.POINT_SIZE, false, 32, null);
431             return this;
432         }
433 
addFloatPointSize(String prefix)434         public Builder addFloatPointSize(String prefix) {
435             add(DataType.FLOAT, DataKind.POINT_SIZE, false, 32, prefix + "pointSize");
436             return this;
437         }
438 
addFloatRGB()439         public Builder addFloatRGB() {
440             add(DataType.FLOAT, DataKind.RED, false, 32, null);
441             add(DataType.FLOAT, DataKind.GREEN, false, 32, null);
442             add(DataType.FLOAT, DataKind.BLUE, false, 32, null);
443             return this;
444         }
445 
addFloatRGB(String prefix)446         public Builder addFloatRGB(String prefix) {
447             add(DataType.FLOAT, DataKind.RED, false, 32, prefix + "r");
448             add(DataType.FLOAT, DataKind.GREEN, false, 32, prefix + "g");
449             add(DataType.FLOAT, DataKind.BLUE, false, 32, prefix + "b");
450             return this;
451         }
452 
addFloatRGBA()453         public Builder addFloatRGBA() {
454             add(DataType.FLOAT, DataKind.RED, false, 32, null);
455             add(DataType.FLOAT, DataKind.GREEN, false, 32, null);
456             add(DataType.FLOAT, DataKind.BLUE, false, 32, null);
457             add(DataType.FLOAT, DataKind.ALPHA, false, 32, null);
458             return this;
459         }
460 
addFloatRGBA(String prefix)461         public Builder addFloatRGBA(String prefix) {
462             add(DataType.FLOAT, DataKind.RED, false, 32, prefix + "r");
463             add(DataType.FLOAT, DataKind.GREEN, false, 32, prefix + "g");
464             add(DataType.FLOAT, DataKind.BLUE, false, 32, prefix + "b");
465             add(DataType.FLOAT, DataKind.ALPHA, false, 32, prefix + "a");
466             return this;
467         }
468 
addUNorm8RGBA()469         public Builder addUNorm8RGBA() {
470             add(DataType.UNSIGNED, DataKind.RED, true, 8, null);
471             add(DataType.UNSIGNED, DataKind.GREEN, true, 8, null);
472             add(DataType.UNSIGNED, DataKind.BLUE, true, 8, null);
473             add(DataType.UNSIGNED, DataKind.ALPHA, true, 8, null);
474             return this;
475         }
476 
addUNorm8RGBA(String prefix)477         public Builder addUNorm8RGBA(String prefix) {
478             add(DataType.UNSIGNED, DataKind.RED, true, 8, prefix + "r");
479             add(DataType.UNSIGNED, DataKind.GREEN, true, 8, prefix + "g");
480             add(DataType.UNSIGNED, DataKind.BLUE, true, 8, prefix + "b");
481             add(DataType.UNSIGNED, DataKind.ALPHA, true, 8, prefix + "a");
482             return this;
483         }
484 
create()485         public Element create() {
486             Element e = new Element(mRS, mEntryCount);
487             java.lang.System.arraycopy(mEntries, 0, e.mEntries, 0, mEntryCount);
488             e.init();
489             return e;
490         }
491     }
492 
493 }
494 
495