| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 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 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Field; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * @hide |
| 23 | * |
| 24 | **/ |
| 25 | public class Element extends BaseObj { |
| 26 | final int mPredefinedID; |
| 27 | final boolean mIsPredefined; |
| 28 | |
| 29 | public static final Element USER_U8 = new Element(0); |
| 30 | public static final Element USER_I8 = new Element(1); |
| 31 | public static final Element USER_U16 = new Element(2); |
| 32 | public static final Element USER_I16 = new Element(3); |
| 33 | public static final Element USER_U32 = new Element(4); |
| 34 | public static final Element USER_I32 = new Element(5); |
| 35 | public static final Element USER_FLOAT = new Element(6); |
| 36 | |
| 37 | public static final Element A_8 = new Element(7); |
| 38 | public static final Element RGB_565 = new Element(8); |
| 39 | public static final Element RGB_888 = new Element(11); |
| 40 | public static final Element RGBA_5551 = new Element(9); |
| 41 | public static final Element RGBA_4444 = new Element(10); |
| 42 | public static final Element RGBA_8888 = new Element(12); |
| 43 | |
| 44 | public static final Element INDEX_16 = new Element(13); |
| 45 | public static final Element INDEX_32 = new Element(14); |
| 46 | public static final Element XY_F32 = new Element(15); |
| 47 | public static final Element XYZ_F32 = new Element(16); |
| 48 | public static final Element ST_XY_F32 = new Element(17); |
| 49 | public static final Element ST_XYZ_F32 = new Element(18); |
| 50 | public static final Element NORM_XYZ_F32 = new Element(19); |
| 51 | public static final Element NORM_ST_XYZ_F32 = new Element(20); |
| 52 | |
| 53 | void initPredef(RenderScript rs) { |
| 54 | mID = rs.nElementGetPredefined(mPredefinedID); |
| 55 | } |
| 56 | |
| 57 | static void init(RenderScript rs) { |
| 58 | USER_U8.initPredef(rs); |
| 59 | USER_I8.initPredef(rs); |
| 60 | USER_U16.initPredef(rs); |
| 61 | USER_I16.initPredef(rs); |
| 62 | USER_U32.initPredef(rs); |
| 63 | USER_I32.initPredef(rs); |
| 64 | USER_FLOAT.initPredef(rs); |
| 65 | |
| 66 | A_8.initPredef(rs); |
| 67 | RGB_565.initPredef(rs); |
| 68 | RGB_888.initPredef(rs); |
| 69 | RGBA_5551.initPredef(rs); |
| 70 | RGBA_4444.initPredef(rs); |
| 71 | RGBA_8888.initPredef(rs); |
| 72 | |
| 73 | INDEX_16.initPredef(rs); |
| 74 | INDEX_32.initPredef(rs); |
| 75 | XY_F32.initPredef(rs); |
| 76 | XYZ_F32.initPredef(rs); |
| 77 | ST_XY_F32.initPredef(rs); |
| 78 | ST_XYZ_F32.initPredef(rs); |
| 79 | NORM_XYZ_F32.initPredef(rs); |
| 80 | NORM_ST_XYZ_F32.initPredef(rs); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | public enum DataType { |
| 85 | FLOAT (0), |
| 86 | UNSIGNED (1), |
| 87 | SIGNED (2); |
| 88 | |
| 89 | int mID; |
| 90 | DataType(int id) { |
| 91 | mID = id; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public enum DataKind { |
| 96 | USER (0), |
| 97 | RED (1), |
| 98 | GREEN (2), |
| 99 | BLUE (3), |
| 100 | ALPHA (4), |
| 101 | LUMINANCE (5), |
| 102 | INTENSITY (6), |
| 103 | X (7), |
| 104 | Y (8), |
| 105 | Z (9), |
| 106 | W (10), |
| 107 | S (11), |
| 108 | T (12), |
| 109 | Q (13), |
| 110 | R (14), |
| 111 | NX (15), |
| 112 | NY (16), |
| 113 | NZ (17), |
| 114 | INDEX (18); |
| 115 | |
| 116 | int mID; |
| 117 | DataKind(int id) { |
| 118 | mID = id; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | Element(int predef) { |
| 124 | super(null); |
| 125 | mID = 0; |
| 126 | mPredefinedID = predef; |
| 127 | mIsPredefined = true; |
| 128 | } |
| 129 | |
| 130 | Element(int id, RenderScript rs) { |
| 131 | super(rs); |
| 132 | mID = id; |
| 133 | mPredefinedID = 0; |
| 134 | mIsPredefined = false; |
| 135 | } |
| 136 | |
| 137 | public void destroy() throws IllegalStateException { |
| 138 | if(mIsPredefined) { |
| 139 | throw new IllegalStateException("Attempting to destroy a predefined Element."); |
| 140 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 141 | if(mDestroyed) { |
| 142 | throw new IllegalStateException("Object already destroyed."); |
| 143 | } |
| 144 | mDestroyed = true; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 145 | mRS.nElementDestroy(mID); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 148 | public static Element createFromClass(RenderScript rs, Class c) { |
| 149 | Field[] fields = c.getFields(); |
| 150 | Builder b = new Builder(rs); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 151 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 152 | for(Field f: fields) { |
| 153 | Class fc = f.getType(); |
| 154 | if(fc == int.class) { |
| 155 | b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName()); |
| 156 | } else if(fc == short.class) { |
| 157 | b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName()); |
| 158 | } else if(fc == byte.class) { |
| 159 | b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName()); |
| 160 | } else if(fc == float.class) { |
| 161 | b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName()); |
| 162 | } else { |
| 163 | throw new IllegalArgumentException("Unkown field type"); |
| 164 | } |
| 165 | } |
| 166 | return b.create(); |
| 167 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 168 | |
| 169 | |
| 170 | public static class Builder { |
| 171 | RenderScript mRS; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 172 | Entry[] mEntries; |
| 173 | int mEntryCount; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 174 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 175 | private class Entry { |
| 176 | Element mElement; |
| 177 | Element.DataType mType; |
| 178 | Element.DataKind mKind; |
| 179 | boolean mIsNormalized; |
| 180 | int mBits; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 181 | String mName; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public Builder(RenderScript rs) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 185 | mRS = rs; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 186 | mEntryCount = 0; |
| 187 | mEntries = new Entry[8]; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 190 | void addEntry(Entry e) { |
| 191 | if(mEntries.length >= mEntryCount) { |
| 192 | Entry[] en = new Entry[mEntryCount + 8]; |
| Romain Guy | 81e4640 | 2009-08-14 18:58:33 -0700 | [diff] [blame] | 193 | System.arraycopy(mEntries, 0, en, 0, mEntries.length); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 194 | mEntries = en; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 195 | } |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 196 | mEntries[mEntryCount] = e; |
| 197 | mEntryCount++; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 200 | public Builder add(Element e) throws IllegalArgumentException { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 201 | if(!e.mIsPredefined) { |
| 202 | throw new IllegalArgumentException("add requires a predefined Element."); |
| 203 | } |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 204 | Entry en = new Entry(); |
| 205 | en.mElement = e; |
| 206 | addEntry(en); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 207 | return this; |
| 208 | } |
| 209 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 210 | public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 211 | Entry en = new Entry(); |
| 212 | en.mType = dt; |
| 213 | en.mKind = dk; |
| 214 | en.mIsNormalized = isNormalized; |
| 215 | en.mBits = bits; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 216 | en.mName = name; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 217 | addEntry(en); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 218 | return this; |
| 219 | } |
| 220 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 221 | public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) { |
| 222 | add(dt, dk, isNormalized, bits, null); |
| 223 | return this; |
| 224 | } |
| 225 | |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame^] | 226 | public Builder addFloat(Element.DataKind dk) { |
| 227 | add(DataType.FLOAT, dk, false, 32, null); |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | public Builder addFloat(Element.DataKind dk, String name) { |
| 232 | add(DataType.FLOAT, dk, false, 32, name); |
| 233 | return this; |
| 234 | } |
| 235 | |
| 236 | public Builder addFloatXY() { |
| 237 | add(DataType.FLOAT, DataKind.X, false, 32, null); |
| 238 | add(DataType.FLOAT, DataKind.Y, false, 32, null); |
| 239 | return this; |
| 240 | } |
| 241 | |
| 242 | public Builder addFloatXYZ() { |
| 243 | add(DataType.FLOAT, DataKind.X, false, 32, null); |
| 244 | add(DataType.FLOAT, DataKind.Y, false, 32, null); |
| 245 | add(DataType.FLOAT, DataKind.Z, false, 32, null); |
| 246 | return this; |
| 247 | } |
| 248 | |
| 249 | public Builder addFloatRGB() { |
| 250 | add(DataType.FLOAT, DataKind.RED, false, 32, null); |
| 251 | add(DataType.FLOAT, DataKind.GREEN, false, 32, null); |
| 252 | add(DataType.FLOAT, DataKind.BLUE, false, 32, null); |
| 253 | return this; |
| 254 | } |
| 255 | |
| 256 | public Builder addFloatRGBA() { |
| 257 | add(DataType.FLOAT, DataKind.RED, false, 32, null); |
| 258 | add(DataType.FLOAT, DataKind.GREEN, false, 32, null); |
| 259 | add(DataType.FLOAT, DataKind.BLUE, false, 32, null); |
| 260 | add(DataType.FLOAT, DataKind.ALPHA, false, 32, null); |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | public Builder addUNorm8RGBA() { |
| 265 | add(DataType.UNSIGNED, DataKind.RED, true, 8, null); |
| 266 | add(DataType.UNSIGNED, DataKind.GREEN, true, 8, null); |
| 267 | add(DataType.UNSIGNED, DataKind.BLUE, true, 8, null); |
| 268 | add(DataType.UNSIGNED, DataKind.ALPHA, true, 8, null); |
| 269 | return this; |
| 270 | } |
| 271 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 272 | static synchronized Element internalCreate(RenderScript rs, Builder b) { |
| 273 | rs.nElementBegin(); |
| 274 | for (int ct=0; ct < b.mEntryCount; ct++) { |
| 275 | Entry en = b.mEntries[ct]; |
| 276 | if(en.mElement != null) { |
| 277 | rs.nElementAddPredefined(en.mElement.mPredefinedID); |
| 278 | } else { |
| 279 | int norm = 0; |
| 280 | if (en.mIsNormalized) { |
| 281 | norm = 1; |
| 282 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 283 | rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 284 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 285 | } |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 286 | int id = rs.nElementCreate(); |
| 287 | return new Element(id, rs); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 290 | public Element create() { |
| 291 | return internalCreate(mRS, this); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | } |
| 296 | |