blob: 409d267624c9ba2706752cc5b368f8f1a5938f04 [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
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
19
20/**
21 * @hide
22 *
23 **/
24public class Element extends BaseObj {
25 final int mPredefinedID;
26 final boolean mIsPredefined;
27
28 public static final Element USER_U8 = new Element(0);
29 public static final Element USER_I8 = new Element(1);
30 public static final Element USER_U16 = new Element(2);
31 public static final Element USER_I16 = new Element(3);
32 public static final Element USER_U32 = new Element(4);
33 public static final Element USER_I32 = new Element(5);
34 public static final Element USER_FLOAT = new Element(6);
35
36 public static final Element A_8 = new Element(7);
37 public static final Element RGB_565 = new Element(8);
38 public static final Element RGB_888 = new Element(11);
39 public static final Element RGBA_5551 = new Element(9);
40 public static final Element RGBA_4444 = new Element(10);
41 public static final Element RGBA_8888 = new Element(12);
42
43 public static final Element INDEX_16 = new Element(13);
44 public static final Element INDEX_32 = new Element(14);
45 public static final Element XY_F32 = new Element(15);
46 public static final Element XYZ_F32 = new Element(16);
47 public static final Element ST_XY_F32 = new Element(17);
48 public static final Element ST_XYZ_F32 = new Element(18);
49 public static final Element NORM_XYZ_F32 = new Element(19);
50 public static final Element NORM_ST_XYZ_F32 = new Element(20);
51
52 void initPredef(RenderScript rs) {
53 mID = rs.nElementGetPredefined(mPredefinedID);
54 }
55
56 static void init(RenderScript rs) {
57 USER_U8.initPredef(rs);
58 USER_I8.initPredef(rs);
59 USER_U16.initPredef(rs);
60 USER_I16.initPredef(rs);
61 USER_U32.initPredef(rs);
62 USER_I32.initPredef(rs);
63 USER_FLOAT.initPredef(rs);
64
65 A_8.initPredef(rs);
66 RGB_565.initPredef(rs);
67 RGB_888.initPredef(rs);
68 RGBA_5551.initPredef(rs);
69 RGBA_4444.initPredef(rs);
70 RGBA_8888.initPredef(rs);
71
72 INDEX_16.initPredef(rs);
73 INDEX_32.initPredef(rs);
74 XY_F32.initPredef(rs);
75 XYZ_F32.initPredef(rs);
76 ST_XY_F32.initPredef(rs);
77 ST_XYZ_F32.initPredef(rs);
78 NORM_XYZ_F32.initPredef(rs);
79 NORM_ST_XYZ_F32.initPredef(rs);
80 }
81
82
83 public enum DataType {
84 FLOAT (0),
85 UNSIGNED (1),
86 SIGNED (2);
87
88 int mID;
89 DataType(int id) {
90 mID = id;
91 }
92 }
93
94 public enum DataKind {
95 USER (0),
96 RED (1),
97 GREEN (2),
98 BLUE (3),
99 ALPHA (4),
100 LUMINANCE (5),
101 INTENSITY (6),
102 X (7),
103 Y (8),
104 Z (9),
105 W (10),
106 S (11),
107 T (12),
108 Q (13),
109 R (14),
110 NX (15),
111 NY (16),
112 NZ (17),
113 INDEX (18);
114
115 int mID;
116 DataKind(int id) {
117 mID = id;
118 }
119 }
120
121
122 Element(int predef) {
123 super(null);
124 mID = 0;
125 mPredefinedID = predef;
126 mIsPredefined = true;
127 }
128
129 Element(int id, RenderScript rs) {
130 super(rs);
131 mID = id;
132 mPredefinedID = 0;
133 mIsPredefined = false;
134 }
135
136 public void destroy() throws IllegalStateException {
137 if(mIsPredefined) {
138 throw new IllegalStateException("Attempting to destroy a predefined Element.");
139 }
140 mRS.nElementDestroy(mID);
141 mID = 0;
142 }
143
144
145
146
147 public static class Builder {
148 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700149 Entry[] mEntries;
150 int mEntryCount;
Jason Sams36e612a2009-07-31 16:26:13 -0700151
Jason Sams22534172009-08-04 16:58:20 -0700152 private class Entry {
153 Element mElement;
154 Element.DataType mType;
155 Element.DataKind mKind;
156 boolean mIsNormalized;
157 int mBits;
158 }
159
160 public Builder(RenderScript rs) {
Jason Sams36e612a2009-07-31 16:26:13 -0700161 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700162 mEntryCount = 0;
163 mEntries = new Entry[8];
Jason Sams36e612a2009-07-31 16:26:13 -0700164 }
165
Jason Sams22534172009-08-04 16:58:20 -0700166 void addEntry(Entry e) {
167 if(mEntries.length >= mEntryCount) {
168 Entry[] en = new Entry[mEntryCount + 8];
169 for(int ct=0; ct < mEntries.length; ct++) {
170 en[ct] = mEntries[ct];
171 }
172 mEntries = en;
Jason Sams36e612a2009-07-31 16:26:13 -0700173 }
Jason Sams22534172009-08-04 16:58:20 -0700174 mEntries[mEntryCount] = e;
175 mEntryCount++;
Jason Sams36e612a2009-07-31 16:26:13 -0700176 }
177
Jason Sams22534172009-08-04 16:58:20 -0700178 public Builder add(Element e) throws IllegalArgumentException {
Jason Sams36e612a2009-07-31 16:26:13 -0700179 if(!e.mIsPredefined) {
180 throw new IllegalArgumentException("add requires a predefined Element.");
181 }
Jason Sams22534172009-08-04 16:58:20 -0700182 Entry en = new Entry();
183 en.mElement = e;
184 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700185 return this;
186 }
187
Jason Sams22534172009-08-04 16:58:20 -0700188 public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
189 Entry en = new Entry();
190 en.mType = dt;
191 en.mKind = dk;
192 en.mIsNormalized = isNormalized;
193 en.mBits = bits;
194 addEntry(en);
Jason Sams36e612a2009-07-31 16:26:13 -0700195 return this;
196 }
197
Jason Sams22534172009-08-04 16:58:20 -0700198 static synchronized Element internalCreate(RenderScript rs, Builder b) {
199 rs.nElementBegin();
200 for (int ct=0; ct < b.mEntryCount; ct++) {
201 Entry en = b.mEntries[ct];
202 if(en.mElement != null) {
203 rs.nElementAddPredefined(en.mElement.mPredefinedID);
204 } else {
205 int norm = 0;
206 if (en.mIsNormalized) {
207 norm = 1;
208 }
209 rs.nElementAdd(en.mType.mID, en.mKind.mID, norm, en.mBits);
210 }
Jason Sams36e612a2009-07-31 16:26:13 -0700211 }
Jason Sams22534172009-08-04 16:58:20 -0700212 int id = rs.nElementCreate();
213 return new Element(id, rs);
Jason Sams36e612a2009-07-31 16:26:13 -0700214 }
215
Jason Sams22534172009-08-04 16:58:20 -0700216 public Element create() {
217 return internalCreate(mRS, this);
Jason Sams36e612a2009-07-31 16:26:13 -0700218 }
219 }
220
221}
222