slang_rs_reflection.cpp revision 2f1451cf567125d8ebd2cacfa83a10fdf3a7ab5a
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 "slang_rs_reflection.h" 18 19#include <utility> 20#include <cstdarg> 21#include <cctype> 22#include <sys/stat.h> 23 24#include "llvm/ADT/APFloat.h" 25 26#include "slang_utils.h" 27#include "slang_rs_context.h" 28#include "slang_rs_export_var.h" 29#include "slang_rs_export_func.h" 30#include "slang_rs_reflect_utils.h" 31 32#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_" 33#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC" 34 35#define RS_TYPE_CLASS_NAME_PREFIX "ScriptField_" 36#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.Script.FieldBase" 37 38#define RS_TYPE_ITEM_CLASS_NAME "Item" 39 40#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray" 41#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer" 42 43#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_" 44#define RS_EXPORT_VAR_PREFIX "mExportVar_" 45 46#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_" 47 48#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_" 49#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_" 50 51using namespace slang; 52 53// Some utility function using internal in RSReflection 54static bool GetClassNameFromFileName(const std::string &FileName, 55 std::string &ClassName) { 56 ClassName.clear(); 57 58 if (FileName.empty() || (FileName == "-")) 59 return true; 60 61 ClassName = 62 RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str()); 63 64 return true; 65} 66 67static const char *GetPrimitiveTypeName(const RSExportPrimitiveType *EPT) { 68 static const char *PrimitiveTypeJavaNameMap[] = { 69 "", // RSExportPrimitiveType::DataTypeFloat16 70 "float", // RSExportPrimitiveType::DataTypeFloat32 71 "double", // RSExportPrimitiveType::DataTypeFloat64 72 "byte", // RSExportPrimitiveType::DataTypeSigned8 73 "short", // RSExportPrimitiveType::DataTypeSigned16 74 "int", // RSExportPrimitiveType::DataTypeSigned32 75 "long", // RSExportPrimitiveType::DataTypeSigned64 76 "short", // RSExportPrimitiveType::DataTypeUnsigned8 77 "int", // RSExportPrimitiveType::DataTypeUnsigned16 78 "long", // RSExportPrimitiveType::DataTypeUnsigned32 79 "long", // RSExportPrimitiveType::DataTypeUnsigned64 80 "boolean", // RSExportPrimitiveType::DataTypeBoolean 81 82 "int", // RSExportPrimitiveType::DataTypeUnsigned565 83 "int", // RSExportPrimitiveType::DataTypeUnsigned5551 84 "int", // RSExportPrimitiveType::DataTypeUnsigned4444 85 86 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 87 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 88 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 89 90 "Element", // RSExportPrimitiveType::DataTypeRSElement 91 "Type", // RSExportPrimitiveType::DataTypeRSType 92 "Allocation", // RSExportPrimitiveType::DataTypeRSAllocation 93 "Sampler", // RSExportPrimitiveType::DataTypeRSSampler 94 "Script", // RSExportPrimitiveType::DataTypeRSScript 95 "Mesh", // RSExportPrimitiveType::DataTypeRSMesh 96 "ProgramFragment", // RSExportPrimitiveType::DataTypeRSProgramFragment 97 "ProgramVertex", // RSExportPrimitiveType::DataTypeRSProgramVertex 98 "ProgramRaster", // RSExportPrimitiveType::DataTypeRSProgramRaster 99 "ProgramStore", // RSExportPrimitiveType::DataTypeRSProgramStore 100 "Font", // RSExportPrimitiveType::DataTypeRSFont 101 }; 102 unsigned TypeId = EPT->getType(); 103 104 if (TypeId < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*))) { 105 return PrimitiveTypeJavaNameMap[ EPT->getType() ]; 106 } 107 108 assert(false && "GetPrimitiveTypeName : Unknown primitive data type"); 109 return NULL; 110} 111 112static const char *GetVectorTypeName(const RSExportVectorType *EVT) { 113 static const char *VectorTypeJavaNameMap[][3] = { 114 /* 0 */ { "Byte2", "Byte3", "Byte4" }, 115 /* 1 */ { "Short2", "Short3", "Short4" }, 116 /* 2 */ { "Int2", "Int3", "Int4" }, 117 /* 3 */ { "Long2", "Long3", "Long4" }, 118 /* 4 */ { "Float2", "Float3", "Float4" }, 119 }; 120 121 const char **BaseElement = NULL; 122 123 switch (EVT->getType()) { 124 case RSExportPrimitiveType::DataTypeSigned8: 125 case RSExportPrimitiveType::DataTypeBoolean: { 126 BaseElement = VectorTypeJavaNameMap[0]; 127 break; 128 } 129 case RSExportPrimitiveType::DataTypeSigned16: 130 case RSExportPrimitiveType::DataTypeUnsigned8: { 131 BaseElement = VectorTypeJavaNameMap[1]; 132 break; 133 } 134 case RSExportPrimitiveType::DataTypeSigned32: 135 case RSExportPrimitiveType::DataTypeUnsigned16: { 136 BaseElement = VectorTypeJavaNameMap[2]; 137 break; 138 } 139 case RSExportPrimitiveType::DataTypeSigned64: 140 case RSExportPrimitiveType::DataTypeUnsigned64: 141 case RSExportPrimitiveType::DataTypeUnsigned32: { 142 BaseElement = VectorTypeJavaNameMap[3]; 143 break; 144 } 145 case RSExportPrimitiveType::DataTypeFloat32: { 146 BaseElement = VectorTypeJavaNameMap[4]; 147 break; 148 } 149 default: { 150 assert(false && "RSReflection::genElementTypeName : Unsupported vector " 151 "element data type"); 152 break; 153 } 154 } 155 156 assert((EVT->getNumElement() > 1) && 157 (EVT->getNumElement() <= 4) && 158 "Number of element in vector type is invalid"); 159 160 return BaseElement[EVT->getNumElement() - 2]; 161} 162 163static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) { 164 static const char *MatrixTypeJavaNameMap[] = { 165 /* 2x2 */ "Matrix2f", 166 /* 3x3 */ "Matrix3f", 167 /* 4x4 */ "Matrix4f", 168 }; 169 unsigned Dim = EMT->getDim(); 170 171 if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char*))) 172 return MatrixTypeJavaNameMap[ EMT->getDim() - 2 ]; 173 174 assert(false && "GetMatrixTypeName : Unsupported matrix dimension"); 175 return NULL; 176} 177 178static const char *GetVectorAccessor(int Index) { 179 static const char *VectorAccessorMap[] = { 180 /* 0 */ "x", 181 /* 1 */ "y", 182 /* 2 */ "z", 183 /* 3 */ "w", 184 }; 185 186 assert((Index >= 0) && 187 (Index < (sizeof(VectorAccessorMap) / sizeof(const char*))) && 188 "Out-of-bound index to access vector member"); 189 190 return VectorAccessorMap[Index]; 191} 192 193static const char *GetPackerAPIName(const RSExportPrimitiveType *EPT) { 194 static const char *PrimitiveTypePackerAPINameMap[] = { 195 "", // RSExportPrimitiveType::DataTypeFloat16 196 "addF32", // RSExportPrimitiveType::DataTypeFloat32 197 "addF64", // RSExportPrimitiveType::DataTypeFloat64 198 "addI8", // RSExportPrimitiveType::DataTypeSigned8 199 "addI16", // RSExportPrimitiveType::DataTypeSigned16 200 "addI32", // RSExportPrimitiveType::DataTypeSigned32 201 "addI64", // RSExportPrimitiveType::DataTypeSigned64 202 "addU8", // RSExportPrimitiveType::DataTypeUnsigned8 203 "addU16", // RSExportPrimitiveType::DataTypeUnsigned16 204 "addU32", // RSExportPrimitiveType::DataTypeUnsigned32 205 "addU64", // RSExportPrimitiveType::DataTypeUnsigned64 206 "addBoolean", // RSExportPrimitiveType::DataTypeBoolean 207 208 "addU16", // RSExportPrimitiveType::DataTypeUnsigned565 209 "addU16", // RSExportPrimitiveType::DataTypeUnsigned5551 210 "addU16", // RSExportPrimitiveType::DataTypeUnsigned4444 211 212 "addObj", // RSExportPrimitiveType::DataTypeRSMatrix2x2 213 "addObj", // RSExportPrimitiveType::DataTypeRSMatrix3x3 214 "addObj", // RSExportPrimitiveType::DataTypeRSMatrix4x4 215 216 "addObj", // RSExportPrimitiveType::DataTypeRSElement 217 "addObj", // RSExportPrimitiveType::DataTypeRSType 218 "addObj", // RSExportPrimitiveType::DataTypeRSAllocation 219 "addObj", // RSExportPrimitiveType::DataTypeRSSampler 220 "addObj", // RSExportPrimitiveType::DataTypeRSScript 221 "addObj", // RSExportPrimitiveType::DataTypeRSMesh 222 "addObj", // RSExportPrimitiveType::DataTypeRSProgramFragment 223 "addObj", // RSExportPrimitiveType::DataTypeRSProgramVertex 224 "addObj", // RSExportPrimitiveType::DataTypeRSProgramRaster 225 "addObj", // RSExportPrimitiveType::DataTypeRSProgramStore 226 "addObj", // RSExportPrimitiveType::DataTypeRSFont 227 }; 228 unsigned TypeId = EPT->getType(); 229 230 if (TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*))) 231 return PrimitiveTypePackerAPINameMap[ EPT->getType() ]; 232 233 assert(false && "GetPackerAPIName : Unknown primitive data type"); 234 return NULL; 235} 236 237static std::string GetTypeName(const RSExportType *ET) { 238 switch (ET->getClass()) { 239 case RSExportType::ExportClassPrimitive: { 240 return GetPrimitiveTypeName( 241 static_cast<const RSExportPrimitiveType*>(ET)); 242 } 243 case RSExportType::ExportClassPointer: { 244 const RSExportType *PointeeType = 245 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 246 247 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 248 return "Allocation"; 249 else 250 return RS_TYPE_CLASS_NAME_PREFIX + PointeeType->getName(); 251 } 252 case RSExportType::ExportClassVector: { 253 return GetVectorTypeName(static_cast<const RSExportVectorType*>(ET)); 254 } 255 case RSExportType::ExportClassMatrix: { 256 return GetMatrixTypeName(static_cast<const RSExportMatrixType*>(ET)); 257 } 258 case RSExportType::ExportClassConstantArray: { 259 const RSExportConstantArrayType* CAT = 260 static_cast<const RSExportConstantArrayType*>(ET); 261 std::string ElementTypeName = GetTypeName(CAT->getElementType()); 262 ElementTypeName.append("[]"); 263 return ElementTypeName; 264 } 265 case RSExportType::ExportClassRecord: { 266 return RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + 267 "."RS_TYPE_ITEM_CLASS_NAME; 268 } 269 default: { 270 assert(false && "Unknown class of type"); 271 } 272 } 273 274 return ""; 275} 276 277static const char *GetTypeNullValue(const RSExportType *ET) { 278 switch (ET->getClass()) { 279 case RSExportType::ExportClassPrimitive: { 280 const RSExportPrimitiveType *EPT = 281 static_cast<const RSExportPrimitiveType*>(ET); 282 if (EPT->isRSObjectType()) 283 return "null"; 284 else if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) 285 return "false"; 286 else 287 return "0"; 288 break; 289 } 290 case RSExportType::ExportClassPointer: 291 case RSExportType::ExportClassVector: 292 case RSExportType::ExportClassMatrix: 293 case RSExportType::ExportClassConstantArray: 294 case RSExportType::ExportClassRecord: { 295 return "null"; 296 break; 297 } 298 default: { 299 assert(false && "Unknown class of type"); 300 } 301 } 302 return ""; 303} 304 305static const char *GetBuiltinElementConstruct(const RSExportType *ET) { 306 if (ET->getClass() == RSExportType::ExportClassPrimitive) { 307 const RSExportPrimitiveType *EPT = 308 static_cast<const RSExportPrimitiveType*>(ET); 309 if (EPT->getKind() == RSExportPrimitiveType::DataKindUser) { 310 static const char *PrimitiveBuiltinElementConstructMap[] = { 311 NULL, // RSExportPrimitiveType::DataTypeFloat16 312 "F32", // RSExportPrimitiveType::DataTypeFloat32 313 "F64", // RSExportPrimitiveType::DataTypeFloat64 314 "I8", // RSExportPrimitiveType::DataTypeSigned8 315 NULL, // RSExportPrimitiveType::DataTypeSigned16 316 "I32", // RSExportPrimitiveType::DataTypeSigned32 317 "I64", // RSExportPrimitiveType::DataTypeSigned64 318 "U8", // RSExportPrimitiveType::DataTypeUnsigned8 319 NULL, // RSExportPrimitiveType::DataTypeUnsigned16 320 "U32", // RSExportPrimitiveType::DataTypeUnsigned32 321 "U64", // RSExportPrimitiveType::DataTypeUnsigned64 322 "BOOLEAN", // RSExportPrimitiveType::DataTypeBoolean 323 324 NULL, // RSExportPrimitiveType::DataTypeUnsigned565 325 NULL, // RSExportPrimitiveType::DataTypeUnsigned5551 326 NULL, // RSExportPrimitiveType::DataTypeUnsigned4444 327 328 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 329 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 330 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 331 332 "ELEMENT", // RSExportPrimitiveType::DataTypeRSElement 333 "TYPE", // RSExportPrimitiveType::DataTypeRSType 334 "ALLOCATION", // RSExportPrimitiveType::DataTypeRSAllocation 335 "SAMPLER", // RSExportPrimitiveType::DataTypeRSSampler 336 "SCRIPT", // RSExportPrimitiveType::DataTypeRSScript 337 "MESH", // RSExportPrimitiveType::DataTypeRSMesh 338 "PROGRAM_FRAGMENT", // RSExportPrimitiveType::DataTypeRSProgramFragment 339 "PROGRAM_VERTEX", // RSExportPrimitiveType::DataTypeRSProgramVertex 340 "PROGRAM_RASTER", // RSExportPrimitiveType::DataTypeRSProgramRaster 341 "PROGRAM_STORE", // RSExportPrimitiveType::DataTypeRSProgramStore 342 "FONT", // RSExportPrimitiveType::DataTypeRSFont 343 }; 344 unsigned TypeId = EPT->getType(); 345 346 if (TypeId < 347 (sizeof(PrimitiveBuiltinElementConstructMap) / sizeof(const char*))) 348 return PrimitiveBuiltinElementConstructMap[ EPT->getType() ]; 349 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelA) { 350 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 351 return "A_8"; 352 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGB) { 353 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned565) 354 return "RGB_565"; 355 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 356 return "RGB_888"; 357 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGBA) { 358 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned5551) 359 return "RGBA_5551"; 360 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned4444) 361 return "RGBA_4444"; 362 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 363 return "RGBA_8888"; 364 } 365 } else if (ET->getClass() == RSExportType::ExportClassVector) { 366 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(ET); 367 if (EVT->getKind() == RSExportPrimitiveType::DataKindUser) { 368 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 369 if (EVT->getNumElement() == 2) 370 return "F32_2"; 371 else if (EVT->getNumElement() == 3) 372 return "F32_3"; 373 else if (EVT->getNumElement() == 4) 374 return "F32_4"; 375 } else if (EVT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) { 376 if (EVT->getNumElement() == 4) 377 return "U8_4"; 378 } 379 } 380 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 381 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType *>(ET); 382 switch (EMT->getDim()) { 383 case 2: { 384 return "MATRIX_2X2"; 385 break; 386 } 387 case 3: { 388 return "MATRIX_3X3"; 389 break; 390 } 391 case 4: { 392 return "MATRIX_4X4"; 393 break; 394 } 395 default: { 396 assert(false && "Unsupported dimension of matrix"); 397 } 398 } 399 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 400 // Treat pointer type variable as unsigned int 401 // TODO(zonr): this is target dependent 402 return "USER_I32"; 403 } 404 405 return NULL; 406} 407 408static const char *GetElementDataKindName(RSExportPrimitiveType::DataKind DK) { 409 static const char *ElementDataKindNameMap[] = { 410 "Element.DataKind.USER", // RSExportPrimitiveType::DataKindUser 411 "Element.DataKind.PIXEL_L", // RSExportPrimitiveType::DataKindPixelL 412 "Element.DataKind.PIXEL_A", // RSExportPrimitiveType::DataKindPixelA 413 "Element.DataKind.PIXEL_LA", // RSExportPrimitiveType::DataKindPixelLA 414 "Element.DataKind.PIXEL_RGB", // RSExportPrimitiveType::DataKindPixelRGB 415 "Element.DataKind.PIXEL_RGBA", // RSExportPrimitiveType::DataKindPixelRGBA 416 }; 417 418 if (static_cast<unsigned>(DK) < 419 (sizeof(ElementDataKindNameMap) / sizeof(const char*))) 420 return ElementDataKindNameMap[ DK ]; 421 else 422 return NULL; 423} 424 425static const char *GetElementDataTypeName(RSExportPrimitiveType::DataType DT) { 426 static const char *ElementDataTypeNameMap[] = { 427 NULL, // RSExportPrimitiveType::DataTypeFloat16 428 "Element.DataType.FLOAT_32", // RSExportPrimitiveType::DataTypeFloat32 429 "Element.DataType.FLOAT_64", // RSExportPrimitiveType::DataTypeFloat64 430 "Element.DataType.SIGNED_8", // RSExportPrimitiveType::DataTypeSigned8 431 "Element.DataType.SIGNED_16", // RSExportPrimitiveType::DataTypeSigned16 432 "Element.DataType.SIGNED_32", // RSExportPrimitiveType::DataTypeSigned32 433 "Element.DataType.SIGNED_64", // RSExportPrimitiveType::DataTypeSigned64 434 "Element.DataType.UNSIGNED_8", // RSExportPrimitiveType::DataTypeUnsigned8 435 "Element.DataType.UNSIGNED_16", // RSExportPrimitiveType::DataTypeUnsigned16 436 "Element.DataType.UNSIGNED_32", // RSExportPrimitiveType::DataTypeUnsigned32 437 "Element.DataType.UNSIGNED_64", // RSExportPrimitiveType::DataTypeUnsigned64 438 "Element.DataType.BOOLEAN", // RSExportPrimitiveType::DataTypeBoolean 439 440 // RSExportPrimitiveType::DataTypeUnsigned565 441 "Element.DataType.UNSIGNED_5_6_5", 442 // RSExportPrimitiveType::DataTypeUnsigned5551 443 "Element.DataType.UNSIGNED_5_5_5_1", 444 // RSExportPrimitiveType::DataTypeUnsigned4444 445 "Element.DataType.UNSIGNED_4_4_4_4", 446 447 // DataTypeRSMatrix* must have been resolved in GetBuiltinElementConstruct() 448 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 449 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 450 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 451 452 "Element.DataType.RS_ELEMENT", // RSExportPrimitiveType::DataTypeRSElement 453 "Element.DataType.RS_TYPE", // RSExportPrimitiveType::DataTypeRSType 454 // RSExportPrimitiveType::DataTypeRSAllocation 455 "Element.DataType.RS_ALLOCATION", 456 // RSExportPrimitiveType::DataTypeRSSampler 457 "Element.DataType.RS_SAMPLER", 458 // RSExportPrimitiveType::DataTypeRSScript 459 "Element.DataType.RS_SCRIPT", 460 // RSExportPrimitiveType::DataTypeRSMesh 461 "Element.DataType.RS_MESH", 462 // RSExportPrimitiveType::DataTypeRSProgramFragment 463 "Element.DataType.RS_PROGRAM_FRAGMENT", 464 // RSExportPrimitiveType::DataTypeRSProgramVertex 465 "Element.DataType.RS_PROGRAM_VERTEX", 466 // RSExportPrimitiveType::DataTypeRSProgramRaster 467 "Element.DataType.RS_PROGRAM_RASTER", 468 // RSExportPrimitiveType::DataTypeRSProgramStore 469 "Element.DataType.RS_PROGRAM_STORE", 470 // RSExportPrimitiveType::DataTypeRSFont 471 "Element.DataType.RS_FONT", 472 }; 473 474 if (static_cast<unsigned>(DT) < 475 (sizeof(ElementDataTypeNameMap) / sizeof(const char*))) 476 return ElementDataTypeNameMap[ DT ]; 477 else 478 return NULL; 479} 480 481/********************** Methods to generate script class **********************/ 482bool RSReflection::genScriptClass(Context &C, 483 const std::string &ClassName, 484 std::string &ErrorMsg) { 485 if (!C.startClass(Context::AM_Public, 486 false, 487 ClassName, 488 RS_SCRIPT_CLASS_SUPER_CLASS_NAME, 489 ErrorMsg)) 490 return false; 491 492 genScriptClassConstructor(C); 493 494 // Reflect export variable 495 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 496 E = mRSContext->export_vars_end(); 497 I != E; 498 I++) 499 genExportVariable(C, *I); 500 501 // Reflect export function 502 for (RSContext::const_export_func_iterator 503 I = mRSContext->export_funcs_begin(), 504 E = mRSContext->export_funcs_end(); 505 I != E; I++) 506 genExportFunction(C, *I); 507 508 C.endClass(); 509 510 return true; 511} 512 513void RSReflection::genScriptClassConstructor(Context &C) { 514 C.indent() << "// Constructor" << std::endl; 515 C.startFunction(Context::AM_Public, 516 false, 517 NULL, 518 C.getClassName(), 519 4, 520 "RenderScript", 521 "rs", 522 "Resources", 523 "resources", 524 "int", 525 "id", 526 "boolean", 527 "isRoot"); 528 // Call constructor of super class 529 C.indent() << "super(rs, resources, id, isRoot);" << std::endl; 530 531 // If an exported variable has initial value, reflect it 532 533 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 534 E = mRSContext->export_vars_end(); 535 I != E; 536 I++) { 537 const RSExportVar *EV = *I; 538 if (!EV->getInit().isUninit()) 539 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit()); 540 } 541 542 C.endFunction(); 543 return; 544} 545 546void RSReflection::genInitBoolExportVariable(Context &C, 547 const std::string &VarName, 548 const clang::APValue &Val) { 549 assert(!Val.isUninit() && "Not a valid initializer"); 550 551 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 552 assert((Val.getKind() == clang::APValue::Int) && 553 "Bool type has wrong initial APValue"); 554 555 C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true") 556 << ";" << std::endl; 557 558 return; 559} 560 561void RSReflection::genInitPrimitiveExportVariable(Context &C, 562 const std::string &VarName, 563 const clang::APValue &Val) { 564 assert(!Val.isUninit() && "Not a valid initializer"); 565 566 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 567 switch (Val.getKind()) { 568 case clang::APValue::Int: { 569 llvm::APInt api = Val.getInt(); 570 C.out() << api.getSExtValue(); 571 if (api.getBitWidth() > 32) { 572 C.out() << "L"; 573 } 574 break; 575 } 576 case clang::APValue::Float: { 577 llvm::APFloat apf = Val.getFloat(); 578 if (&apf.getSemantics() == &llvm::APFloat::IEEEsingle) { 579 C.out() << apf.convertToFloat() << "f"; 580 } else { 581 C.out() << apf.convertToDouble(); 582 } 583 break; 584 } 585 586 case clang::APValue::ComplexInt: 587 case clang::APValue::ComplexFloat: 588 case clang::APValue::LValue: 589 case clang::APValue::Vector: { 590 assert(false && "Primitive type cannot have such kind of initializer"); 591 break; 592 } 593 default: { 594 assert(false && "Unknown kind of initializer"); 595 } 596 } 597 C.out() << ";" << std::endl; 598 599 return; 600} 601 602void RSReflection::genInitExportVariable(Context &C, 603 const RSExportType *ET, 604 const std::string &VarName, 605 const clang::APValue &Val) { 606 assert(!Val.isUninit() && "Not a valid initializer"); 607 608 switch (ET->getClass()) { 609 case RSExportType::ExportClassPrimitive: { 610 const RSExportPrimitiveType *EPT = 611 static_cast<const RSExportPrimitiveType*>(ET); 612 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) { 613 genInitBoolExportVariable(C, VarName, Val); 614 } else { 615 genInitPrimitiveExportVariable(C, VarName, Val); 616 } 617 break; 618 } 619 case RSExportType::ExportClassPointer: { 620 if (!Val.isInt() || Val.getInt().getSExtValue() != 0) 621 std::cout << "Initializer which is non-NULL to pointer type variable " 622 "will be ignored" << std::endl; 623 break; 624 } 625 case RSExportType::ExportClassVector: { 626 const RSExportVectorType *EVT = 627 static_cast<const RSExportVectorType*>(ET); 628 switch (Val.getKind()) { 629 case clang::APValue::Int: 630 case clang::APValue::Float: { 631 for (unsigned i = 0; i < EVT->getNumElement(); i++) { 632 std::string Name = VarName + "." + GetVectorAccessor(i); 633 genInitPrimitiveExportVariable(C, Name, Val); 634 } 635 break; 636 } 637 case clang::APValue::Vector: { 638 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " 639 << GetVectorTypeName(EVT) << "();" << std::endl; 640 641 unsigned NumElements = 642 std::min(static_cast<unsigned>(EVT->getNumElement()), 643 Val.getVectorLength()); 644 for (unsigned i = 0; i < NumElements; i++) { 645 const clang::APValue &ElementVal = Val.getVectorElt(i); 646 std::string Name = VarName + "." + GetVectorAccessor(i); 647 genInitPrimitiveExportVariable(C, Name, ElementVal); 648 } 649 break; 650 } 651 case clang::APValue::Uninitialized: 652 case clang::APValue::ComplexInt: 653 case clang::APValue::ComplexFloat: 654 case clang::APValue::LValue: { 655 assert(false && "Unexpected type of value of initializer."); 656 } 657 } 658 break; 659 } 660 // TODO(zonr): Resolving initializer of a record (and matrix) type variable 661 // is complex. It cannot obtain by just simply evaluating the initializer 662 // expression. 663 case RSExportType::ExportClassMatrix: 664 case RSExportType::ExportClassConstantArray: 665 case RSExportType::ExportClassRecord: { 666#if 0 667 unsigned InitIndex = 0; 668 const RSExportRecordType *ERT = 669 static_cast<const RSExportRecordType*>(ET); 670 671 assert((Val.getKind() == clang::APValue::Vector) && "Unexpected type of " 672 "initializer for record type variable"); 673 674 C.indent() << RS_EXPORT_VAR_PREFIX << VarName 675 << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() 676 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 677 678 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 679 E = ERT->fields_end(); 680 I != E; 681 I++) { 682 const RSExportRecordType::Field *F = *I; 683 std::string FieldName = VarName + "." + F->getName(); 684 685 if (InitIndex > Val.getVectorLength()) 686 break; 687 688 genInitPrimitiveExportVariable(C, 689 FieldName, 690 Val.getVectorElt(InitIndex++)); 691 } 692#endif 693 assert(false && "Unsupported initializer for record/matrix/constant " 694 "array type variable currently"); 695 break; 696 } 697 default: { 698 assert(false && "Unknown class of type"); 699 } 700 } 701 return; 702} 703 704void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) { 705 const RSExportType *ET = EV->getType(); 706 707 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX 708 << EV->getName() << " = " << C.getNextExportVarSlot() << ";" 709 << std::endl; 710 711 switch (ET->getClass()) { 712 case RSExportType::ExportClassPrimitive: { 713 genPrimitiveTypeExportVariable(C, EV); 714 break; 715 } 716 case RSExportType::ExportClassPointer: { 717 genPointerTypeExportVariable(C, EV); 718 break; 719 } 720 case RSExportType::ExportClassVector: { 721 genVectorTypeExportVariable(C, EV); 722 break; 723 } 724 case RSExportType::ExportClassMatrix: { 725 genMatrixTypeExportVariable(C, EV); 726 break; 727 } 728 case RSExportType::ExportClassConstantArray: { 729 genConstantArrayTypeExportVariable(C, EV); 730 break; 731 } 732 case RSExportType::ExportClassRecord: { 733 genRecordTypeExportVariable(C, EV); 734 break; 735 } 736 default: { 737 assert(false && "Unknown class of type"); 738 } 739 } 740 741 return; 742} 743 744void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) { 745 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX 746 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" 747 << std::endl; 748 749 // invoke_*() 750 Context::ArgTy Args; 751 752 if (EF->hasParam()) { 753 for (RSExportFunc::const_param_iterator I = EF->params_begin(), 754 E = EF->params_end(); 755 I != E; 756 I++) { 757 Args.push_back(std::make_pair(GetTypeName((*I)->getType()), 758 (*I)->getName())); 759 } 760 } 761 762 C.startFunction(Context::AM_Public, 763 false, 764 "void", 765 "invoke_" + EF->getName(), 766 Args); 767 768 if (!EF->hasParam()) { 769 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" 770 << std::endl; 771 } else { 772 const RSExportRecordType *ERT = EF->getParamPacketType(); 773 std::string FieldPackerName = EF->getName() + "_fp"; 774 775 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 776 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 777 778 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " 779 << FieldPackerName << ");" << std::endl; 780 } 781 782 C.endFunction(); 783 return; 784} 785 786void RSReflection::genPrimitiveTypeExportVariable(Context &C, 787 const RSExportVar *EV) { 788 assert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive) && 789 "Variable should be type of primitive here"); 790 791 const RSExportPrimitiveType *EPT = 792 static_cast<const RSExportPrimitiveType*>(EV->getType()); 793 const char *TypeName = GetPrimitiveTypeName(EPT); 794 795 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 796 << EV->getName() << ";" << std::endl; 797 798 // set_*() 799 if (!EV->isConst()) { 800 C.startFunction(Context::AM_Public, 801 false, 802 "void", 803 "set_" + EV->getName(), 804 1, 805 TypeName, 806 "v"); 807 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 808 809 if (EPT->isRSObjectType()) 810 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 811 << ", (v == null) ? 0 : v.getID());" << std::endl; 812 else 813 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 814 << ", v);" << std::endl; 815 816 C.endFunction(); 817 } 818 819 genGetExportVariable(C, TypeName, EV->getName()); 820 821 return; 822} 823 824void RSReflection::genPointerTypeExportVariable(Context &C, 825 const RSExportVar *EV) { 826 const RSExportType *ET = EV->getType(); 827 const RSExportType *PointeeType; 828 std::string TypeName; 829 830 assert((ET->getClass() == RSExportType::ExportClassPointer) && 831 "Variable should be type of pointer here"); 832 833 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 834 TypeName = GetTypeName(ET); 835 836 // bind_*() 837 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 838 << EV->getName() << ";" << std::endl; 839 840 C.startFunction(Context::AM_Public, 841 false, 842 "void", 843 "bind_" + EV->getName(), 844 1, 845 TypeName.c_str(), 846 "v"); 847 848 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 849 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX 850 << EV->getName() << ");" << std::endl; 851 852 if (PointeeType->getClass() == RSExportType::ExportClassRecord) 853 C.indent() << "else bindAllocation(v.getAllocation(), " 854 RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" 855 << std::endl; 856 else 857 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX 858 << EV->getName() << ");" << std::endl; 859 860 C.endFunction(); 861 862 genGetExportVariable(C, TypeName, EV->getName()); 863 864 return; 865} 866 867void RSReflection::genVectorTypeExportVariable(Context &C, 868 const RSExportVar *EV) { 869 assert((EV->getType()->getClass() == RSExportType::ExportClassVector) && 870 "Variable should be type of vector here"); 871 872 const RSExportVectorType *EVT = 873 static_cast<const RSExportVectorType*>(EV->getType()); 874 const char *TypeName = GetVectorTypeName(EVT); 875 const char *FieldPackerName = "fp"; 876 877 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 878 << EV->getName() << ";" << std::endl; 879 880 // set_*() 881 if (!EV->isConst()) { 882 C.startFunction(Context::AM_Public, 883 false, 884 "void", 885 "set_" + EV->getName(), 886 1, 887 TypeName, 888 "v"); 889 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 890 891 if (genCreateFieldPacker(C, EVT, FieldPackerName)) 892 genPackVarOfType(C, EVT, "v", FieldPackerName); 893 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 894 << FieldPackerName << ");" << std::endl; 895 896 C.endFunction(); 897 } 898 899 genGetExportVariable(C, TypeName, EV->getName()); 900 return; 901} 902 903void RSReflection::genMatrixTypeExportVariable(Context &C, 904 const RSExportVar *EV) { 905 assert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) && 906 "Variable should be type of matrix here"); 907 908 const RSExportMatrixType *EMT = 909 static_cast<const RSExportMatrixType*>(EV->getType()); 910 const char *TypeName = GetMatrixTypeName(EMT); 911 const char *FieldPackerName = "fp"; 912 913 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 914 << EV->getName() << ";" << std::endl; 915 916 // set_*() 917 if (!EV->isConst()) { 918 C.startFunction(Context::AM_Public, 919 false, 920 "void", 921 "set_" + EV->getName(), 922 1, 923 TypeName, "v"); 924 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 925 926 if (genCreateFieldPacker(C, EMT, FieldPackerName)) 927 genPackVarOfType(C, EMT, "v", FieldPackerName); 928 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 929 << FieldPackerName << ");" << std::endl; 930 931 C.endFunction(); 932 } 933 934 genGetExportVariable(C, TypeName, EV->getName()); 935 return; 936} 937 938void RSReflection::genConstantArrayTypeExportVariable(Context &C, 939 const RSExportVar *EV) { 940 assert((EV->getType()->getClass() == RSExportType::ExportClassConstantArray)&& 941 "Variable should be type of constant array here"); 942 943 const RSExportConstantArrayType *ECAT = 944 static_cast<const RSExportConstantArrayType*>(EV->getType()); 945 std::string TypeName = GetTypeName(ECAT); 946 const char *FieldPackerName = "fp"; 947 948 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 949 << EV->getName() << ";" << std::endl; 950 951 // set_*() 952 if (!EV->isConst()) { 953 C.startFunction(Context::AM_Public, 954 false, 955 "void", 956 "set_" + EV->getName(), 957 1, 958 TypeName.c_str(), "v"); 959 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 960 961 if (genCreateFieldPacker(C, ECAT, FieldPackerName)) 962 genPackVarOfType(C, ECAT, "v", FieldPackerName); 963 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 964 << FieldPackerName << ");" << std::endl; 965 966 C.endFunction(); 967 } 968 969 genGetExportVariable(C, TypeName, EV->getName()); 970 return; 971} 972 973void RSReflection::genRecordTypeExportVariable(Context &C, 974 const RSExportVar *EV) { 975 assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && 976 "Variable should be type of struct here"); 977 978 const RSExportRecordType *ERT = 979 static_cast<const RSExportRecordType*>(EV->getType()); 980 std::string TypeName = 981 RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 982 const char *FieldPackerName = "fp"; 983 984 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 985 << EV->getName() << ";" << std::endl; 986 987 // set_*() 988 if (!EV->isConst()) { 989 C.startFunction(Context::AM_Public, 990 false, 991 "void", 992 "set_" + EV->getName(), 993 1, 994 TypeName.c_str(), 995 "v"); 996 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 997 998 if (genCreateFieldPacker(C, ERT, FieldPackerName)) 999 genPackVarOfType(C, ERT, "v", FieldPackerName); 1000 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 1001 << ", " << FieldPackerName << ");" << std::endl; 1002 1003 C.endFunction(); 1004 } 1005 1006 genGetExportVariable(C, TypeName.c_str(), EV->getName()); 1007 return; 1008} 1009 1010void RSReflection::genGetExportVariable(Context &C, 1011 const std::string &TypeName, 1012 const std::string &VarName) { 1013 C.startFunction(Context::AM_Public, 1014 false, 1015 TypeName.c_str(), 1016 "get_" + VarName, 1017 0); 1018 1019 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl; 1020 1021 C.endFunction(); 1022 return; 1023} 1024 1025/******************* Methods to generate script class /end *******************/ 1026 1027bool RSReflection::genCreateFieldPacker(Context &C, 1028 const RSExportType *ET, 1029 const char *FieldPackerName) { 1030 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 1031 if (AllocSize > 0) 1032 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" 1033 << AllocSize << ");" << std::endl; 1034 else 1035 return false; 1036 return true; 1037} 1038 1039void RSReflection::genPackVarOfType(Context &C, 1040 const RSExportType *ET, 1041 const char *VarName, 1042 const char *FieldPackerName) { 1043 switch (ET->getClass()) { 1044 case RSExportType::ExportClassPrimitive: 1045 case RSExportType::ExportClassVector: { 1046 C.indent() << FieldPackerName << "." 1047 << GetPackerAPIName( 1048 static_cast<const RSExportPrimitiveType*>(ET)) 1049 << "(" << VarName << ");" << std::endl; 1050 break; 1051 } 1052 case RSExportType::ExportClassPointer: { 1053 // Must reflect as type Allocation in Java 1054 const RSExportType *PointeeType = 1055 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 1056 1057 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 1058 C.indent() << FieldPackerName << ".addI32(" << VarName 1059 << ".getPtr());" << std::endl; 1060 else 1061 C.indent() << FieldPackerName << ".addI32(" << VarName 1062 << ".getAllocation().getPtr());" << std::endl; 1063 break; 1064 } 1065 case RSExportType::ExportClassMatrix: { 1066 C.indent() << FieldPackerName << ".addObj(" << VarName << ");" 1067 << std::endl; 1068 break; 1069 } 1070 case RSExportType::ExportClassConstantArray: { 1071 const RSExportConstantArrayType *ECAT = 1072 static_cast<const RSExportConstantArrayType *>(ET); 1073 C.indent() << "for (int ct = 0; ct < " << ECAT->getSize() << "; ct++)"; 1074 C.startBlock(); 1075 1076 std::string ElementVarName(VarName); 1077 ElementVarName.append("[ct]"); 1078 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(), 1079 FieldPackerName); 1080 1081 C.endBlock(); 1082 break; 1083 } 1084 case RSExportType::ExportClassRecord: { 1085 const RSExportRecordType *ERT = 1086 static_cast<const RSExportRecordType*>(ET); 1087 // Relative pos from now on in field packer 1088 unsigned Pos = 0; 1089 1090 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1091 E = ERT->fields_end(); 1092 I != E; 1093 I++) { 1094 const RSExportRecordType::Field *F = *I; 1095 std::string FieldName; 1096 size_t FieldOffset = F->getOffsetInParent(); 1097 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1098 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1099 1100 if (VarName != NULL) 1101 FieldName = VarName + ("." + F->getName()); 1102 else 1103 FieldName = F->getName(); 1104 1105 if (FieldOffset > Pos) 1106 C.indent() << FieldPackerName << ".skip(" 1107 << (FieldOffset - Pos) << ");" << std::endl; 1108 1109 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 1110 1111 // There is padding in the field type 1112 if (FieldAllocSize > FieldStoreSize) 1113 C.indent() << FieldPackerName << ".skip(" 1114 << (FieldAllocSize - FieldStoreSize) 1115 << ");" << std::endl; 1116 1117 Pos = FieldOffset + FieldAllocSize; 1118 } 1119 1120 // There maybe some padding after the struct 1121 size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos; 1122 if (Padding > 0) 1123 C.indent() << FieldPackerName << ".skip(" << Padding << ");" 1124 << std::endl; 1125 break; 1126 } 1127 default: { 1128 assert(false && "Unknown class of type"); 1129 } 1130 } 1131 1132 return; 1133} 1134 1135void RSReflection::genAllocateVarOfType(Context &C, 1136 const RSExportType *T, 1137 const std::string &VarName) { 1138 switch (T->getClass()) { 1139 case RSExportType::ExportClassPrimitive: { 1140 // Primitive type like int in Java has its own storage once it's declared. 1141 // 1142 // FIXME: Should we allocate storage for RS object? 1143 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType()) 1144 // C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1145 // << std::endl; 1146 break; 1147 } 1148 case RSExportType::ExportClassPointer: { 1149 // Pointer type is an instance of Allocation or a TypeClass whose value is 1150 // expected to be assigned by programmer later in Java program. Therefore 1151 // we don't reflect things like [VarName] = new Allocation(); 1152 C.indent() << VarName << " = null;" << std::endl; 1153 break; 1154 } 1155 case RSExportType::ExportClassConstantArray: { 1156 const RSExportConstantArrayType *ECAT = 1157 static_cast<const RSExportConstantArrayType *>(T); 1158 const RSExportType *ElementType = ECAT->getElementType(); 1159 1160 C.indent() << VarName << " = new " << GetTypeName(ElementType) 1161 << "[" << ECAT->getSize() << "];" << std::endl; 1162 1163 // Primitive type element doesn't need allocation code. 1164 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) { 1165 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; " 1166 "$ct++)"; 1167 C.startBlock(); 1168 1169 std::string ElementVarName(VarName); 1170 ElementVarName.append("[$ct]"); 1171 genAllocateVarOfType(C, ElementType, ElementVarName); 1172 1173 C.endBlock(); 1174 } 1175 break; 1176 } 1177 case RSExportType::ExportClassVector: 1178 case RSExportType::ExportClassMatrix: 1179 case RSExportType::ExportClassRecord: { 1180 C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1181 << std::endl; 1182 break; 1183 } 1184 } 1185 return; 1186} 1187 1188void RSReflection::genNewItemBufferIfNull(Context &C, const char *Index) { 1189 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) " 1190 RS_TYPE_ITEM_BUFFER_NAME" = " 1191 "new "RS_TYPE_ITEM_CLASS_NAME"[mType.getX() /* count */];" 1192 << std::endl; 1193 if (Index != NULL) 1194 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) " 1195 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = " 1196 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 1197 return; 1198} 1199 1200void RSReflection::genNewItemBufferPackerIfNull(Context &C) { 1201 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_PACKER_NAME" == null) " 1202 RS_TYPE_ITEM_BUFFER_PACKER_NAME" = " 1203 "new FieldPacker(" 1204 RS_TYPE_ITEM_CLASS_NAME".sizeof * mType.getX()/* count */" 1205 ");" << std::endl; 1206 return; 1207} 1208 1209/********************** Methods to generate type class **********************/ 1210bool RSReflection::genTypeClass(Context &C, 1211 const RSExportRecordType *ERT, 1212 std::string &ErrorMsg) { 1213 std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName(); 1214 1215 if (!C.startClass(Context::AM_Public, 1216 false, 1217 ClassName, 1218 RS_TYPE_CLASS_SUPER_CLASS_NAME, 1219 ErrorMsg)) 1220 return false; 1221 1222 genTypeItemClass(C, ERT); 1223 1224 // Declare item buffer and item buffer packer 1225 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]" 1226 ";" << std::endl; 1227 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" 1228 << std::endl; 1229 1230 genTypeClassConstructor(C, ERT); 1231 genTypeClassCopyToArray(C, ERT); 1232 genTypeClassItemSetter(C, ERT); 1233 genTypeClassItemGetter(C, ERT); 1234 genTypeClassComponentSetter(C, ERT); 1235 genTypeClassComponentGetter(C, ERT); 1236 genTypeClassCopyAll(C, ERT); 1237 1238 C.endClass(); 1239 1240 C.resetFieldIndex(); 1241 C.clearFieldIndexMap(); 1242 1243 return true; 1244} 1245 1246void RSReflection::genTypeItemClass(Context &C, const RSExportRecordType *ERT) { 1247 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 1248 C.startBlock(); 1249 1250 C.indent() << "public static final int sizeof = " 1251 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl; 1252 1253 // Member elements 1254 C.out() << std::endl; 1255 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1256 FE = ERT->fields_end(); 1257 FI != FE; 1258 FI++) { 1259 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() 1260 << ";" << std::endl; 1261 } 1262 1263 // Constructor 1264 C.out() << std::endl; 1265 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 1266 C.startBlock(); 1267 1268 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1269 FE = ERT->fields_end(); 1270 FI != FE; 1271 FI++) { 1272 const RSExportRecordType::Field *F = *FI; 1273 genAllocateVarOfType(C, F->getType(), F->getName()); 1274 } 1275 1276 // end Constructor 1277 C.endBlock(); 1278 1279 // end Item class 1280 C.endBlock(); 1281 1282 return; 1283} 1284 1285void RSReflection::genTypeClassConstructor(Context &C, 1286 const RSExportRecordType *ERT) { 1287 const char *RenderScriptVar = "rs"; 1288 1289 C.startFunction(Context::AM_Public, 1290 true, 1291 "Element", 1292 "createElement", 1293 1, 1294 "RenderScript", 1295 RenderScriptVar); 1296 genBuildElement(C, ERT, RenderScriptVar); 1297 C.endFunction(); 1298 1299 C.startFunction(Context::AM_Public, 1300 false, 1301 NULL, 1302 C.getClassName(), 1303 2, 1304 "RenderScript", 1305 RenderScriptVar, 1306 "int", 1307 "count"); 1308 1309 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1310 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1311 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1312 << std::endl; 1313 // Call init() in super class 1314 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl; 1315 C.endFunction(); 1316 1317 return; 1318} 1319 1320void RSReflection::genTypeClassCopyToArray(Context &C, 1321 const RSExportRecordType *ERT) { 1322 C.startFunction(Context::AM_Private, 1323 false, 1324 "void", 1325 "copyToArray", 1326 2, 1327 RS_TYPE_ITEM_CLASS_NAME, 1328 "i", 1329 "int", 1330 "index"); 1331 1332 genNewItemBufferPackerIfNull(C); 1333 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1334 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1335 << std::endl; 1336 1337 genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1338 1339 C.endFunction(); 1340 return; 1341} 1342 1343void RSReflection::genTypeClassItemSetter(Context &C, 1344 const RSExportRecordType *ERT) { 1345 C.startFunction(Context::AM_Public, 1346 false, 1347 "void", 1348 "set", 1349 3, 1350 RS_TYPE_ITEM_CLASS_NAME, 1351 "i", 1352 "int", 1353 "index", 1354 "boolean", 1355 "copyNow"); 1356 genNewItemBufferIfNull(C, NULL); 1357 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl; 1358 1359 C.indent() << "if (copyNow) "; 1360 C.startBlock(); 1361 1362 C.indent() << "copyToArray(i, index);" << std::endl; 1363 C.indent() << "mAllocation.subData1D(index, 1, " 1364 RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << std::endl; 1365 1366 // End of if (copyNow) 1367 C.endBlock(); 1368 1369 C.endFunction(); 1370 return; 1371} 1372 1373void RSReflection::genTypeClassItemGetter(Context &C, 1374 const RSExportRecordType *ERT) { 1375 C.startFunction(Context::AM_Public, 1376 false, 1377 RS_TYPE_ITEM_CLASS_NAME, 1378 "get", 1379 1, 1380 "int", 1381 "index"); 1382 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" 1383 << std::endl; 1384 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl; 1385 C.endFunction(); 1386 return; 1387} 1388 1389void RSReflection::genTypeClassComponentSetter(Context &C, 1390 const RSExportRecordType *ERT) { 1391 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1392 FE = ERT->fields_end(); 1393 FI != FE; 1394 FI++) { 1395 const RSExportRecordType::Field *F = *FI; 1396 size_t FieldOffset = F->getOffsetInParent(); 1397 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1398 unsigned FieldIndex = C.getFieldIndex(F); 1399 1400 C.startFunction(Context::AM_Public, 1401 false, 1402 "void", 1403 "set_" + F->getName(), 3, 1404 "int", 1405 "index", 1406 GetTypeName(F->getType()).c_str(), 1407 "v", 1408 "boolean", 1409 "copyNow"); 1410 genNewItemBufferPackerIfNull(C); 1411 genNewItemBufferIfNull(C, "index"); 1412 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1413 << " = v;" << std::endl; 1414 1415 C.indent() << "if (copyNow) "; 1416 C.startBlock(); 1417 1418 if (FieldOffset > 0) 1419 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1420 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " 1421 << FieldOffset << ");" << std::endl; 1422 else 1423 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1424 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1425 << std::endl; 1426 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1427 1428 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" 1429 << std::endl; 1430 genPackVarOfType(C, F->getType(), "v", "fp"); 1431 C.indent() << "mAllocation.subElementData(index, " << FieldIndex << ", fp);" 1432 << std::endl; 1433 1434 // End of if (copyNow) 1435 C.endBlock(); 1436 1437 C.endFunction(); 1438 } 1439 return; 1440} 1441 1442void RSReflection::genTypeClassComponentGetter(Context &C, 1443 const RSExportRecordType *ERT) { 1444 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1445 FE = ERT->fields_end(); 1446 FI != FE; 1447 FI++) { 1448 const RSExportRecordType::Field *F = *FI; 1449 C.startFunction(Context::AM_Public, 1450 false, 1451 GetTypeName(F->getType()).c_str(), 1452 "get_" + F->getName(), 1453 1, 1454 "int", 1455 "index"); 1456 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return " 1457 << GetTypeNullValue(F->getType()) << ";" << std::endl; 1458 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1459 << ";" << std::endl; 1460 C.endFunction(); 1461 } 1462 return; 1463} 1464 1465void RSReflection::genTypeClassCopyAll(Context &C, 1466 const RSExportRecordType *ERT) { 1467 C.startFunction(Context::AM_Public, false, "void", "copyAll", 0); 1468 1469 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)" 1470 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" 1471 << std::endl; 1472 C.indent() << "mAllocation.data("RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" 1473 << std::endl; 1474 1475 C.endFunction(); 1476 return; 1477} 1478 1479/******************** Methods to generate type class /end ********************/ 1480 1481/********** Methods to create Element in Java of given record type ***********/ 1482void RSReflection::genBuildElement(Context &C, const RSExportRecordType *ERT, 1483 const char *RenderScriptVar) { 1484 const char *ElementBuilderName = "eb"; 1485 1486 // Create element builder 1487 // C.startBlock(true); 1488 1489 C.indent() << "Element.Builder " << ElementBuilderName << " = " 1490 "new Element.Builder(" << RenderScriptVar << ");" << std::endl; 1491 1492 // eb.add(...) 1493 genAddElementToElementBuilder(C, 1494 ERT, 1495 "", 1496 ElementBuilderName, 1497 RenderScriptVar); 1498 1499 C.indent() << "return " << ElementBuilderName << ".create();" << std::endl; 1500 1501 // C.endBlock(); 1502 return; 1503} 1504 1505#define EB_ADD(x) \ 1506 C.indent() << ElementBuilderName \ 1507 << ".add(Element." << x << ", \"" << VarName << "\");" \ 1508 << std::endl; \ 1509 C.incFieldIndex() 1510 1511void RSReflection::genAddElementToElementBuilder(Context &C, 1512 const RSExportType *ET, 1513 const std::string &VarName, 1514 const char *ElementBuilderName, 1515 const char *RenderScriptVar) { 1516 const char *ElementConstruct = GetBuiltinElementConstruct(ET); 1517 1518 if (ElementConstruct != NULL) { 1519 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")"); 1520 } else { 1521 if ((ET->getClass() == RSExportType::ExportClassPrimitive) || 1522 (ET->getClass() == RSExportType::ExportClassVector)) { 1523 const RSExportPrimitiveType *EPT = 1524 static_cast<const RSExportPrimitiveType*>(ET); 1525 const char *DataKindName = GetElementDataKindName(EPT->getKind()); 1526 const char *DataTypeName = GetElementDataTypeName(EPT->getType()); 1527 int Size = (ET->getClass() == RSExportType::ExportClassVector) ? 1528 static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1529 1; 1530 1531 switch (EPT->getKind()) { 1532 case RSExportPrimitiveType::DataKindPixelL: 1533 case RSExportPrimitiveType::DataKindPixelA: 1534 case RSExportPrimitiveType::DataKindPixelLA: 1535 case RSExportPrimitiveType::DataKindPixelRGB: 1536 case RSExportPrimitiveType::DataKindPixelRGBA: { 1537 // Element.createPixel() 1538 EB_ADD("createPixel(" << RenderScriptVar << ", " 1539 << DataTypeName << ", " 1540 << DataKindName << ")"); 1541 break; 1542 } 1543 case RSExportPrimitiveType::DataKindUser: 1544 default: { 1545 if (EPT->getClass() == RSExportType::ExportClassPrimitive) { 1546 // Element.createUser() 1547 EB_ADD("createUser(" << RenderScriptVar << ", " 1548 << DataTypeName << ")"); 1549 } else { 1550 assert((ET->getClass() == RSExportType::ExportClassVector) && 1551 "Unexpected type."); 1552 EB_ADD("createVector(" << RenderScriptVar << ", " 1553 << DataTypeName << ", " 1554 << Size << ")"); 1555 } 1556 break; 1557 } 1558 } 1559#ifndef NDEBUG 1560 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 1561 // Pointer type variable should be resolved in 1562 // GetBuiltinElementConstruct() 1563 assert(false && "??"); 1564 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 1565 // Matrix type variable should be resolved 1566 // in GetBuiltinElementConstruct() 1567 assert(false && "??"); 1568#endif 1569 } else if (ET->getClass() == RSExportType::ExportClassConstantArray) { 1570 const RSExportConstantArrayType *ECAT = 1571 static_cast<const RSExportConstantArrayType *>(ET); 1572 C.indent() << "for (int ct = 0; ct < " << ECAT->getSize() << "; ct++)"; 1573 C.startBlock(); 1574 1575 std::string ElementVarName(VarName); 1576 ElementVarName.append("[\" + ct + \"]"); 1577 genAddElementToElementBuilder(C, 1578 ECAT->getElementType(), 1579 ElementVarName, 1580 ElementBuilderName, 1581 RenderScriptVar); 1582 1583 C.endBlock(); 1584 } else if (ET->getClass() == RSExportType::ExportClassRecord) { 1585 // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType. 1586 // 1587 // TODO(zonr): Generalize these two function such that there's no 1588 // duplicated codes. 1589 const RSExportRecordType *ERT = 1590 static_cast<const RSExportRecordType*>(ET); 1591 int Pos = 0; // relative pos from now on 1592 1593 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1594 E = ERT->fields_end(); 1595 I != E; 1596 I++) { 1597 const RSExportRecordType::Field *F = *I; 1598 std::string FieldName; 1599 int FieldOffset = F->getOffsetInParent(); 1600 int FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1601 int FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1602 1603 if (!VarName.empty()) 1604 FieldName = VarName + "." + F->getName(); 1605 else 1606 FieldName = F->getName(); 1607 1608 // Alignment 1609 genAddPaddingToElementBuiler(C, 1610 (FieldOffset - Pos), 1611 ElementBuilderName, 1612 RenderScriptVar); 1613 1614 // eb.add(...) 1615 C.addFieldIndexMapping(F); 1616 genAddElementToElementBuilder(C, 1617 F->getType(), 1618 FieldName, 1619 ElementBuilderName, 1620 RenderScriptVar); 1621 1622 // There is padding within the field type 1623 genAddPaddingToElementBuiler(C, 1624 (FieldAllocSize - FieldStoreSize), 1625 ElementBuilderName, 1626 RenderScriptVar); 1627 1628 Pos = FieldOffset + FieldAllocSize; 1629 } 1630 1631 // There maybe some padding after the struct 1632 //unsigned char align = RSExportType::GetTypeAlignment(ERT); 1633 //size_t siz = RSExportType::GetTypeAllocSize(ERT); 1634 size_t siz1 = RSExportType::GetTypeStoreSize(ERT); 1635 1636 genAddPaddingToElementBuiler(C, 1637 siz1 - Pos, 1638 ElementBuilderName, 1639 RenderScriptVar); 1640 } else { 1641 assert(false && "Unknown class of type"); 1642 } 1643 } 1644} 1645 1646void RSReflection::genAddPaddingToElementBuiler(Context &C, 1647 int PaddingSize, 1648 const char *ElementBuilderName, 1649 const char *RenderScriptVar) { 1650 while (PaddingSize > 0) { 1651 const std::string &VarName = C.createPaddingField(); 1652 if (PaddingSize >= 4) { 1653 EB_ADD("U32(" << RenderScriptVar << ")"); 1654 PaddingSize -= 4; 1655 } else if (PaddingSize >= 2) { 1656 EB_ADD("U16(" << RenderScriptVar << ")"); 1657 PaddingSize -= 2; 1658 } else if (PaddingSize >= 1) { 1659 EB_ADD("U8(" << RenderScriptVar << ")"); 1660 PaddingSize -= 1; 1661 } 1662 } 1663 return; 1664} 1665 1666#undef EB_ADD 1667/******** Methods to create Element in Java of given record type /end ********/ 1668 1669bool RSReflection::reflect(const std::string &OutputPathBase, 1670 const std::string &OutputPackageName, 1671 const std::string &InputFileName, 1672 const std::string &OutputBCFileName) { 1673 Context *C = NULL; 1674 std::string ResourceId = ""; 1675 1676 if (!GetClassNameFromFileName(OutputBCFileName, ResourceId)) 1677 return false; 1678 1679 if (ResourceId.empty()) 1680 ResourceId = "<Resource ID>"; 1681 1682 if (OutputPackageName.empty() || OutputPackageName == "-") 1683 C = new Context(OutputPathBase, InputFileName, "<Package Name>", 1684 ResourceId, true); 1685 else 1686 C = new Context(OutputPathBase, InputFileName, OutputPackageName, 1687 ResourceId, false); 1688 1689 if (C != NULL) { 1690 std::string ErrorMsg, ScriptClassName; 1691 // class ScriptC_<ScriptName> 1692 if (!GetClassNameFromFileName(InputFileName, ScriptClassName)) 1693 return false; 1694 1695 if (ScriptClassName.empty()) 1696 ScriptClassName = "<Input Script Name>"; 1697 1698 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX); 1699 1700 if (mRSContext->getLicenseNote() != NULL) { 1701 C->setLicenseNote(*(mRSContext->getLicenseNote())); 1702 } 1703 1704 if (!genScriptClass(*C, ScriptClassName, ErrorMsg)) { 1705 std::cerr << "Failed to generate class " << ScriptClassName << " (" 1706 << ErrorMsg << ")" << std::endl; 1707 return false; 1708 } 1709 1710 // class ScriptField_<TypeName> 1711 for (RSContext::const_export_type_iterator TI = 1712 mRSContext->export_types_begin(), 1713 TE = mRSContext->export_types_end(); 1714 TI != TE; 1715 TI++) { 1716 const RSExportType *ET = TI->getValue(); 1717 1718 if (ET->getClass() == RSExportType::ExportClassRecord) { 1719 const RSExportRecordType *ERT = 1720 static_cast<const RSExportRecordType*>(ET); 1721 1722 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1723 std::cerr << "Failed to generate type class for struct '" 1724 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl; 1725 return false; 1726 } 1727 } 1728 } 1729 } 1730 1731 return true; 1732} 1733 1734/************************** RSReflection::Context **************************/ 1735const char *const RSReflection::Context::ApacheLicenseNote = 1736 "/*\n" 1737 " * Copyright (C) 2010 The Android Open Source Project\n" 1738 " *\n" 1739 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 1740 " * you may not use this file except in compliance with the License.\n" 1741 " * You may obtain a copy of the License at\n" 1742 " *\n" 1743 " * http://www.apache.org/licenses/LICENSE-2.0\n" 1744 " *\n" 1745 " * Unless required by applicable law or agreed to in writing, software\n" 1746 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 1747 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or " 1748 "implied.\n" 1749 " * See the License for the specific language governing permissions and\n" 1750 " * limitations under the License.\n" 1751 " */\n" 1752 "\n"; 1753 1754const char *const RSReflection::Context::Import[] = { 1755 // RenderScript java class 1756 "android.renderscript.*", 1757 // Import R 1758 "android.content.res.Resources", 1759 // Import for debugging 1760 "android.util.Log", 1761}; 1762 1763bool RSReflection::Context::openClassFile(const std::string &ClassName, 1764 std::string &ErrorMsg) { 1765 if (!mUseStdout) { 1766 mOF.clear(); 1767 std::string Path = 1768 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(), 1769 mPackageName.c_str()); 1770 1771 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg)) 1772 return false; 1773 1774 std::string ClassFile = Path + "/" + ClassName + ".java"; 1775 1776 mOF.open(ClassFile.c_str()); 1777 if (!mOF.good()) { 1778 ErrorMsg = "failed to open file '" + ClassFile + "' for write"; 1779 return false; 1780 } 1781 } 1782 return true; 1783} 1784 1785const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) { 1786 switch (AM) { 1787 case AM_Public: return "public"; break; 1788 case AM_Protected: return "protected"; break; 1789 case AM_Private: return "private"; break; 1790 default: return ""; break; 1791 } 1792} 1793 1794bool RSReflection::Context::startClass(AccessModifier AM, 1795 bool IsStatic, 1796 const std::string &ClassName, 1797 const char *SuperClassName, 1798 std::string &ErrorMsg) { 1799 if (mVerbose) 1800 std::cout << "Generating " << ClassName << ".java ..." << std::endl; 1801 1802 // Open file for class 1803 if (!openClassFile(ClassName, ErrorMsg)) 1804 return false; 1805 1806 // License 1807 out() << mLicenseNote; 1808 1809 // Notice of generated file 1810 out() << "/*" << std::endl; 1811 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl; 1812 out() << " * The source RenderScript file: " << mInputRSFile << std::endl; 1813 out() << " */" << std::endl; 1814 1815 // Package 1816 if (!mPackageName.empty()) 1817 out() << "package " << mPackageName << ";" << std::endl; 1818 out() << std::endl; 1819 1820 // Imports 1821 for (unsigned i = 0;i < (sizeof(Import) / sizeof(const char*)); i++) 1822 out() << "import " << Import[i] << ";" << std::endl; 1823 out() << std::endl; 1824 1825 // All reflected classes should be annotated as hidden, so that they won't 1826 // be exposed in SDK. 1827 out() << "/**" << std::endl; 1828 out() << " * @hide" << std::endl; 1829 out() << " */" << std::endl; 1830 1831 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " 1832 << ClassName; 1833 if (SuperClassName != NULL) 1834 out() << " extends " << SuperClassName; 1835 1836 startBlock(); 1837 1838 mClassName = ClassName; 1839 1840 return true; 1841} 1842 1843void RSReflection::Context::endClass() { 1844 endBlock(); 1845 if (!mUseStdout) 1846 mOF.close(); 1847 clear(); 1848 return; 1849} 1850 1851void RSReflection::Context::startBlock(bool ShouldIndent) { 1852 if (ShouldIndent) 1853 indent() << "{" << std::endl; 1854 else 1855 out() << " {" << std::endl; 1856 incIndentLevel(); 1857 return; 1858} 1859 1860void RSReflection::Context::endBlock() { 1861 decIndentLevel(); 1862 indent() << "}" << std::endl << std::endl; 1863 return; 1864} 1865 1866void RSReflection::Context::startTypeClass(const std::string &ClassName) { 1867 indent() << "public static class " << ClassName; 1868 startBlock(); 1869 return; 1870} 1871 1872void RSReflection::Context::endTypeClass() { 1873 endBlock(); 1874 return; 1875} 1876 1877void RSReflection::Context::startFunction(AccessModifier AM, 1878 bool IsStatic, 1879 const char *ReturnType, 1880 const std::string &FunctionName, 1881 int Argc, ...) { 1882 ArgTy Args; 1883 va_list vl; 1884 va_start(vl, Argc); 1885 1886 for (int i = 0; i < Argc; i++) { 1887 const char *ArgType = va_arg(vl, const char*); 1888 const char *ArgName = va_arg(vl, const char*); 1889 1890 Args.push_back(std::make_pair(ArgType, ArgName)); 1891 } 1892 va_end(vl); 1893 1894 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 1895 1896 return; 1897} 1898 1899void RSReflection::Context::startFunction(AccessModifier AM, 1900 bool IsStatic, 1901 const char *ReturnType, 1902 const std::string &FunctionName, 1903 const ArgTy &Args) { 1904 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") 1905 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 1906 1907 bool FirstArg = true; 1908 for (ArgTy::const_iterator I = Args.begin(), E = Args.end(); 1909 I != E; 1910 I++) { 1911 if (!FirstArg) 1912 out() << ", "; 1913 else 1914 FirstArg = false; 1915 1916 out() << I->first << " " << I->second; 1917 } 1918 1919 out() << ")"; 1920 startBlock(); 1921 1922 return; 1923} 1924 1925void RSReflection::Context::endFunction() { 1926 endBlock(); 1927 return; 1928} 1929