Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, 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 | #include <string> |
| 18 | #include <cstring> |
Stephen Hines | d30158d | 2010-10-18 16:01:44 -0700 | [diff] [blame^] | 19 | #include <cstdio> |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 20 | |
| 21 | #define PRIMITIVE_DATA_TYPE_ENUMS \ |
| 22 | ENUM_PRIMITIVE_DATA_TYPE(Float16, NULL, 128) \ |
| 23 | ENUM_PRIMITIVE_DATA_TYPE(Float32, "float", 256) \ |
| 24 | ENUM_PRIMITIVE_DATA_TYPE(Float64, "double", 512) \ |
| 25 | ENUM_PRIMITIVE_DATA_TYPE(Signed8, "char", 64) \ |
| 26 | ENUM_PRIMITIVE_DATA_TYPE(Signed16, "short", 128) \ |
| 27 | ENUM_PRIMITIVE_DATA_TYPE(Signed32, "int", 256) \ |
| 28 | ENUM_PRIMITIVE_DATA_TYPE(Signed64, "long", 512) \ |
| 29 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned8, "uchar", 64) \ |
| 30 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned16, "ushort", 128) \ |
| 31 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned32, "uint", 256) \ |
| 32 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned64, "ulong", 512) \ |
| 33 | ENUM_PRIMITIVE_DATA_TYPE(Boolean, "bool", 8) \ |
| 34 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned565, "u565", 128) \ |
| 35 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned5551, "u5551", 128) \ |
| 36 | ENUM_PRIMITIVE_DATA_TYPE(Unsigned4444, "u4444", 128) |
| 37 | |
| 38 | #define RS_OBJECT_DATA_TYPE_ENUMS \ |
| 39 | ENUM_RS_OBJECT_DATA_TYPE(RSMatrix2x2, "rs_matrix2x2") \ |
| 40 | ENUM_RS_OBJECT_DATA_TYPE(RSMatrix3x3, "rs_matrix3x3") \ |
| 41 | ENUM_RS_OBJECT_DATA_TYPE(RSMatrix4x4, "rs_matrix4x4") \ |
| 42 | ENUM_RS_OBJECT_DATA_TYPE(RSElement, "rs_element") \ |
| 43 | ENUM_RS_OBJECT_DATA_TYPE(RSType, "rs_type") \ |
| 44 | ENUM_RS_OBJECT_DATA_TYPE(RSAllocation, "rs_allocation") \ |
| 45 | ENUM_RS_OBJECT_DATA_TYPE(RSSampler, "rs_sampler") \ |
| 46 | ENUM_RS_OBJECT_DATA_TYPE(RSScript, "rs_script") \ |
| 47 | ENUM_RS_OBJECT_DATA_TYPE(RSMesh, "rs_mesh") \ |
| 48 | ENUM_RS_OBJECT_DATA_TYPE(RSProgramFragment, "rs_program_fragment") \ |
| 49 | ENUM_RS_OBJECT_DATA_TYPE(RSProgramVertex, "rs_program_vertex") \ |
| 50 | ENUM_RS_OBJECT_DATA_TYPE(RSProgramRaster, "rs_program_raster") \ |
| 51 | ENUM_RS_OBJECT_DATA_TYPE(RSProgramStore, "rs_program_store") \ |
| 52 | ENUM_RS_OBJECT_DATA_TYPE(RSFont, "rs_font") |
| 53 | |
| 54 | #define RS_DATA_KIND_ENUMS \ |
| 55 | ENUM_RS_DATA_KIND(User) \ |
| 56 | ENUM_RS_DATA_KIND(PixelL) \ |
| 57 | ENUM_RS_DATA_KIND(PixelA) \ |
| 58 | ENUM_RS_DATA_KIND(PixelLA) \ |
| 59 | ENUM_RS_DATA_KIND(PixelRGB) \ |
| 60 | ENUM_RS_DATA_KIND(PixelRGBA) |
| 61 | |
| 62 | enum { |
| 63 | #define ENUM_PRIMITIVE_DATA_TYPE(x, name, bits) x, |
| 64 | PRIMITIVE_DATA_TYPE_ENUMS |
| 65 | #undef ENUM_PRIMITIVE_DATA_TYPE |
| 66 | #define ENUM_RS_OBJECT_DATA_TYPE(x, name) x, |
| 67 | RS_OBJECT_DATA_TYPE_ENUMS |
| 68 | #undef ENUM_RS_OBJECT_DATA_TYPE |
| 69 | }; |
| 70 | |
| 71 | enum { |
| 72 | #define ENUM_RS_DATA_KIND(x) x, |
| 73 | RS_DATA_KIND_ENUMS |
| 74 | #undef ENUM_RS_DATA_KIND |
| 75 | }; |
| 76 | |
| 77 | class RSDataTypeSpec { |
| 78 | private: |
| 79 | const char *mTypeName; // e.g. Float32 |
| 80 | // FIXME: better name |
| 81 | const char *mTypePragmaName; // e.g. float |
| 82 | size_t mBits; |
| 83 | |
| 84 | protected: |
| 85 | bool mIsRSObject; |
| 86 | |
| 87 | public: |
| 88 | RSDataTypeSpec(const char *TypeName, |
| 89 | const char *TypePragmaName, |
| 90 | size_t Bits) |
| 91 | : mTypeName(TypeName), |
| 92 | mTypePragmaName(TypePragmaName), |
| 93 | mBits(Bits), |
| 94 | mIsRSObject(false) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | inline const char *getTypeName() const { return mTypeName; } |
| 99 | inline const char *getTypePragmaName() const { return mTypePragmaName; } |
| 100 | inline size_t getSizeInBit() const { return mBits; } |
| 101 | inline bool isRSObject() const { return mIsRSObject; } |
| 102 | }; |
| 103 | |
| 104 | class RSObjectDataTypeSpec : public RSDataTypeSpec { |
| 105 | public: |
| 106 | RSObjectDataTypeSpec(const char *TypeName, |
| 107 | const char *TypePragmaName) |
| 108 | : RSDataTypeSpec(TypeName, TypePragmaName, 32 /* opaque pointer */) { |
| 109 | mIsRSObject = true; |
| 110 | return; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | ///////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | // clang::BuiltinType::Kind -> RSDataTypeSpec |
| 117 | class ClangBuiltinTypeMap { |
| 118 | const char *mBuiltinTypeKind; |
| 119 | const RSDataTypeSpec *mDataType; |
| 120 | |
| 121 | public: |
| 122 | ClangBuiltinTypeMap(const char *BuiltinTypeKind, |
| 123 | const RSDataTypeSpec *DataType) |
| 124 | : mBuiltinTypeKind(BuiltinTypeKind), |
| 125 | mDataType(DataType) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | inline const char *getBuiltinTypeKind() const { return mBuiltinTypeKind; } |
| 130 | inline const RSDataTypeSpec *getDataType() const { return mDataType; } |
| 131 | }; |
| 132 | |
| 133 | ///////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | class RSDataKindSpec { |
| 136 | private: |
| 137 | const char *mKindName; |
| 138 | |
| 139 | public: |
| 140 | explicit RSDataKindSpec(const char *KindName) : mKindName(KindName) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | inline const char *getKindName() const { return mKindName; } |
| 145 | }; |
| 146 | |
| 147 | ///////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | class RSDataElementSpec { |
| 150 | private: |
| 151 | const char *mElementName; |
| 152 | const RSDataKindSpec *mDataKind; |
| 153 | const RSDataTypeSpec *mDataType; |
| 154 | bool mIsNormal; |
| 155 | unsigned mVectorSize; |
| 156 | |
| 157 | public: |
| 158 | RSDataElementSpec(const char *ElementName, |
| 159 | const RSDataKindSpec *DataKind, |
| 160 | const RSDataTypeSpec *DataType, |
| 161 | bool IsNormal, |
| 162 | unsigned VectorSize) |
| 163 | : mElementName(ElementName), |
| 164 | mDataKind(DataKind), |
| 165 | mDataType(DataType), |
| 166 | mIsNormal(IsNormal), |
| 167 | mVectorSize(VectorSize) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | inline const char *getElementName() const { return mElementName; } |
| 172 | inline const RSDataKindSpec *getDataKind() const { return mDataKind; } |
| 173 | inline const RSDataTypeSpec *getDataType() const { return mDataType; } |
| 174 | inline bool isNormal() const { return mIsNormal; } |
| 175 | inline unsigned getVectorSize() const { return mVectorSize; } |
| 176 | }; |
| 177 | |
| 178 | ///////////////////////////////////////////////////////////////////////////// |
| 179 | |
| 180 | // -gen-rs-data-type-enums |
| 181 | // |
| 182 | // ENUM_RS_DATA_TYPE(type, cname, bits) |
| 183 | // e.g., ENUM_RS_DATA_TYPE(Float32, "float", 256) |
| 184 | static int GenRSDataTypeEnums(const RSDataTypeSpec *const DataTypes[], |
| 185 | unsigned NumDataTypes) { |
| 186 | for (unsigned i = 0; i < NumDataTypes; i++) |
| 187 | printf("ENUM_RS_DATA_TYPE(%s, \"%s\", %u)\n", |
| 188 | DataTypes[i]->getTypeName(), |
| 189 | DataTypes[i]->getTypePragmaName(), |
| 190 | DataTypes[i]->getSizeInBit()); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | // -gen-clang-builtin-cnames |
| 195 | // |
| 196 | // ENUM_SUPPORT_BUILTIN_TYPE(builtin_type, type, cname) |
| 197 | // e.g., ENUM_SUPPORT_BUILTIN_TYPE(clang::BuiltinType::Float, Float32, "float") |
| 198 | static int GenClangBuiltinEnum( |
| 199 | const ClangBuiltinTypeMap *const ClangBuilitinsMap[], |
| 200 | unsigned NumClangBuilitins) { |
| 201 | for (unsigned i = 0; i < NumClangBuilitins; i++) |
| 202 | printf("ENUM_SUPPORT_BUILTIN_TYPE(%s, %s, \"%s\")\n", |
| 203 | ClangBuilitinsMap[i]->getBuiltinTypeKind(), |
| 204 | ClangBuilitinsMap[i]->getDataType()->getTypeName(), |
| 205 | ClangBuilitinsMap[i]->getDataType()->getTypePragmaName()); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | // -gen-rs-object-type-enums |
| 210 | // |
| 211 | // ENUM_RS_OBJECT_TYPE(type, cname) |
| 212 | // e.g., ENUM_RS_OBJECT_TYPE(RSMatrix2x2, "rs_matrix2x2") |
| 213 | static int GenRSObjectTypeEnums(const RSDataTypeSpec *const DataTypes[], |
| 214 | unsigned NumDataTypes) { |
| 215 | for (unsigned i = 0; i < NumDataTypes; i++) |
| 216 | if (DataTypes[i]->isRSObject()) |
| 217 | printf("ENUM_RS_OBJECT_TYPE(%s, \"%s\")\n", |
| 218 | DataTypes[i]->getTypeName(), |
| 219 | DataTypes[i]->getTypePragmaName()); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | // -gen-rs-data-kind-enums |
| 224 | // |
| 225 | // ENUM_RS_DATA_KIND(kind) |
| 226 | // e.g., ENUM_RS_DATA_KIND(PixelRGB) |
| 227 | int GenRSDataKindEnums(const RSDataKindSpec *const DataKinds[], |
| 228 | unsigned NumDataKinds) { |
| 229 | for (unsigned i = 0; i < NumDataKinds; i++) |
| 230 | printf("ENUM_RS_DATA_KIND(%s)\n", DataKinds[i]->getKindName()); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | // -gen-rs-data-element-enums |
| 235 | // |
| 236 | // ENUM_RS_DATA_ELEMENT(name, dt, dk, normailized, vsize) |
| 237 | // e.g., ENUM_RS_DATA_ELEMENT("rs_pixel_rgba", PixelRGB, Unsigned8, true, 4) |
| 238 | int GenRSDataElementEnums(const RSDataElementSpec *const DataElements[], |
| 239 | unsigned NumDataElements) { |
| 240 | for (unsigned i = 0; i < NumDataElements; i++) |
| 241 | printf("ENUM_RS_DATA_ELEMENT(\"%s\", %s, %s, %s, %d)\n", |
| 242 | DataElements[i]->getElementName(), |
| 243 | DataElements[i]->getDataKind()->getKindName(), |
| 244 | DataElements[i]->getDataType()->getTypeName(), |
| 245 | ((DataElements[i]->isNormal()) ? "true" : "false"), |
| 246 | DataElements[i]->getVectorSize()); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int main(int argc, char **argv) { |
| 251 | if (argc < 2) { |
| 252 | fprintf(stderr, "usage: %s [gen type]\n", argv[0]); |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | RSDataTypeSpec *DataTypes[] = { |
| 257 | #define ENUM_PRIMITIVE_DATA_TYPE(x, name, bits) \ |
| 258 | new RSDataTypeSpec(#x , name, bits), |
| 259 | PRIMITIVE_DATA_TYPE_ENUMS |
| 260 | #undef ENUM_PRIMITIVE_DATA_TYPE |
| 261 | #define ENUM_RS_OBJECT_DATA_TYPE(x, name) \ |
| 262 | new RSObjectDataTypeSpec(#x, name), |
| 263 | RS_OBJECT_DATA_TYPE_ENUMS |
| 264 | #undef ENUM_RS_OBJECT_DATA_TYPE |
| 265 | }; |
| 266 | |
| 267 | unsigned NumDataTypes = sizeof(DataTypes) / sizeof(DataTypes[0]); |
| 268 | ///////////////////////////////////////////////////////////////////////////// |
| 269 | |
| 270 | ClangBuiltinTypeMap *ClangBuilitinsMap[] = { |
| 271 | new ClangBuiltinTypeMap("clang::BuiltinType::Bool", DataTypes[Boolean]), |
| 272 | new ClangBuiltinTypeMap("clang::BuiltinType::Char_U", DataTypes[Unsigned8]), |
| 273 | new ClangBuiltinTypeMap("clang::BuiltinType::UChar", DataTypes[Unsigned8]), |
| 274 | new ClangBuiltinTypeMap("clang::BuiltinType::Char16", DataTypes[Signed16]), |
| 275 | new ClangBuiltinTypeMap("clang::BuiltinType::Char32", DataTypes[Signed32]), |
| 276 | new ClangBuiltinTypeMap("clang::BuiltinType::UShort", DataTypes[Unsigned16]), |
| 277 | new ClangBuiltinTypeMap("clang::BuiltinType::UInt", DataTypes[Unsigned32]), |
| 278 | new ClangBuiltinTypeMap("clang::BuiltinType::ULong", DataTypes[Unsigned32]), |
| 279 | new ClangBuiltinTypeMap("clang::BuiltinType::ULongLong", DataTypes[Unsigned64]), |
| 280 | |
| 281 | new ClangBuiltinTypeMap("clang::BuiltinType::Char_S", DataTypes[Signed8]), |
| 282 | new ClangBuiltinTypeMap("clang::BuiltinType::SChar", DataTypes[Signed8]), |
| 283 | new ClangBuiltinTypeMap("clang::BuiltinType::Short", DataTypes[Signed16]), |
| 284 | new ClangBuiltinTypeMap("clang::BuiltinType::Int", DataTypes[Signed32]), |
| 285 | new ClangBuiltinTypeMap("clang::BuiltinType::Long", DataTypes[Signed64]), |
| 286 | new ClangBuiltinTypeMap("clang::BuiltinType::LongLong", DataTypes[Signed64]), |
| 287 | |
| 288 | new ClangBuiltinTypeMap("clang::BuiltinType::Float", DataTypes[Float32]), |
| 289 | new ClangBuiltinTypeMap("clang::BuiltinType::Double", DataTypes[Float64]), |
| 290 | }; |
| 291 | |
| 292 | unsigned NumClangBuilitins = |
| 293 | sizeof(ClangBuilitinsMap) / sizeof(ClangBuilitinsMap[0]); |
| 294 | |
| 295 | ///////////////////////////////////////////////////////////////////////////// |
| 296 | |
| 297 | RSDataKindSpec *DataKinds[] = { |
| 298 | #define ENUM_RS_DATA_KIND(x) \ |
| 299 | new RSDataKindSpec(#x), |
| 300 | RS_DATA_KIND_ENUMS |
| 301 | #undef ENUM_RS_DATA_KIND |
| 302 | }; |
| 303 | |
| 304 | unsigned NumDataKinds = sizeof(DataKinds) / sizeof(DataKinds[0]); |
| 305 | ///////////////////////////////////////////////////////////////////////////// |
| 306 | |
| 307 | RSDataElementSpec *DataElements[] = { |
| 308 | new RSDataElementSpec("rs_pixel_l", |
| 309 | DataKinds[PixelL], |
| 310 | DataTypes[Unsigned8], |
| 311 | /* IsNormal = */true, /* VectorSize = */1), |
| 312 | new RSDataElementSpec("rs_pixel_a", |
| 313 | DataKinds[PixelA], |
| 314 | DataTypes[Unsigned8], |
| 315 | true, 1), |
| 316 | new RSDataElementSpec("rs_pixel_la", |
| 317 | DataKinds[PixelLA], |
| 318 | DataTypes[Unsigned8], |
| 319 | true, 2), |
| 320 | new RSDataElementSpec("rs_pixel_rgb", |
| 321 | DataKinds[PixelRGB], |
| 322 | DataTypes[Unsigned8], |
| 323 | true, 3), |
| 324 | new RSDataElementSpec("rs_pixel_rgba", |
| 325 | DataKinds[PixelRGB], |
| 326 | DataTypes[Unsigned8], |
| 327 | true, 4), |
| 328 | new RSDataElementSpec("rs_pixel_rgb565", |
| 329 | DataKinds[PixelRGB], |
| 330 | DataTypes[Unsigned8], |
| 331 | true, 3), |
| 332 | new RSDataElementSpec("rs_pixel_rgb5551", |
| 333 | DataKinds[PixelRGBA], |
| 334 | DataTypes[Unsigned8], |
| 335 | true, 4), |
| 336 | new RSDataElementSpec("rs_pixel_rgb4444", |
| 337 | DataKinds[PixelRGBA], |
| 338 | DataTypes[Unsigned8], |
| 339 | true, 4), |
| 340 | }; |
| 341 | |
| 342 | unsigned NumDataElements = sizeof(DataElements) / sizeof(DataElements[0]); |
| 343 | ///////////////////////////////////////////////////////////////////////////// |
| 344 | int Result = 1; |
| 345 | |
| 346 | if (::strcmp(argv[1], "-gen-rs-data-type-enums") == 0) |
| 347 | Result = GenRSDataTypeEnums(DataTypes, NumDataTypes); |
| 348 | else if (::strcmp(argv[1], "-gen-clang-builtin-enums") == 0) |
| 349 | Result = GenClangBuiltinEnum(ClangBuilitinsMap, NumClangBuilitins); |
| 350 | else if (::strcmp(argv[1], "-gen-rs-object-type-enums") == 0) |
| 351 | Result = GenRSObjectTypeEnums(DataTypes, NumDataTypes); |
| 352 | else if (::strcmp(argv[1], "-gen-rs-data-kind-enums") == 0) |
| 353 | Result = GenRSDataKindEnums(DataKinds, NumDataKinds); |
| 354 | else if (::strcmp(argv[1], "-gen-rs-data-element-enums") == 0) |
| 355 | Result = GenRSDataElementEnums(DataElements, NumDataElements); |
| 356 | else |
| 357 | fprintf(stderr, "%s: Unknown table generation type '%s'\n", |
| 358 | argv[0], argv[1]); |
| 359 | |
| 360 | |
| 361 | ///////////////////////////////////////////////////////////////////////////// |
| 362 | for (unsigned i = 0; i < NumDataTypes; i++) |
| 363 | delete DataTypes[i]; |
| 364 | for (unsigned i = 0; i < NumClangBuilitins; i++) |
| 365 | delete ClangBuilitinsMap[i]; |
| 366 | for (unsigned i = 0; i < NumDataKinds; i++) |
| 367 | delete DataKinds[i]; |
| 368 | for (unsigned i = 0; i < NumDataElements; i++) |
| 369 | delete DataElements[i]; |
| 370 | |
| 371 | return Result; |
| 372 | } |