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