slang_rs_reflection.cpp revision 9c631ff2e65a8fa766981c9683c3b255ce0a2388
1#include "slang_rs_context.hpp" 2#include "slang_rs_export_var.hpp" 3#include "slang_rs_reflection.hpp" 4#include "slang_rs_export_func.hpp" 5#include "slang_rs_reflect_utils.hpp" 6 7#include <ctype.h> 8 9#include <utility> 10#include <cstdarg> 11 12#include "llvm/ADT/APFloat.h" 13 14using std::make_pair; 15using std::endl; 16 17 18#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_" 19#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC" 20 21#define RS_TYPE_CLASS_NAME_PREFIX "ScriptField_" 22#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.Script.FieldBase" 23 24#define RS_TYPE_ITEM_CLASS_NAME "Item" 25 26#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray" 27#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer" 28 29#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_" 30#define RS_EXPORT_VAR_PREFIX "mExportVar_" 31 32#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_" 33 34#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_" 35#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_" 36 37namespace slang { 38 39/* Some utility function using internal in RSReflection */ 40static bool GetClassNameFromFileName(const std::string& FileName, std::string& ClassName) { 41 ClassName.clear(); 42 43 if(FileName.empty() || (FileName == "-")) 44 return true; 45 46 ClassName = RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str()); 47 48 return true; 49} 50 51static const char* GetPrimitiveTypeName(const RSExportPrimitiveType* EPT) { 52 static const char* PrimitiveTypeJavaNameMap[] = { 53 "", 54 "", 55 "float", /* RSExportPrimitiveType::DataTypeFloat32 */ 56 "double", /* RSExportPrimitiveType::DataTypeFloat64 */ 57 "byte", /* RSExportPrimitiveType::DataTypeSigned8 */ 58 "short", /* RSExportPrimitiveType::DataTypeSigned16 */ 59 "int", /* RSExportPrimitiveType::DataTypeSigned32 */ 60 "long", /* RSExportPrimitiveType::DataTypeSigned64 */ 61 "short", /* RSExportPrimitiveType::DataTypeUnsigned8 */ 62 "int", /* RSExportPrimitiveType::DataTypeUnsigned16 */ 63 "long", /* RSExportPrimitiveType::DataTypeUnsigned32 */ 64 "long", /* RSExportPrimitiveType::DataTypeUnsigned64 */ 65 66 "int", /* RSExportPrimitiveType::DataTypeUnsigned565 */ 67 "int", /* RSExportPrimitiveType::DataTypeUnsigned5551 */ 68 "int", /* RSExportPrimitiveType::DataTypeUnsigned4444 */ 69 70 "boolean", /* RSExportPrimitiveType::DataTypeBool */ 71 72 "Element", /* RSExportPrimitiveType::DataTypeRSElement */ 73 "Type", /* RSExportPrimitiveType::DataTypeRSType */ 74 "Allocation", /* RSExportPrimitiveType::DataTypeRSAllocation */ 75 "Sampler", /* RSExportPrimitiveType::DataTypeRSSampler */ 76 "Script", /* RSExportPrimitiveType::DataTypeRSScript */ 77 "Mesh", /* RSExportPrimitiveType::DataTypeRSMesh */ 78 "ProgramFragment", /* RSExportPrimitiveType::DataTypeRSProgramFragment */ 79 "ProgramVertex", /* RSExportPrimitiveType::DataTypeRSProgramVertex */ 80 "ProgramRaster", /* RSExportPrimitiveType::DataTypeRSProgramRaster */ 81 "ProgramStore", /* RSExportPrimitiveType::DataTypeRSProgramStore */ 82 "Font", /* RSExportPrimitiveType::DataTypeRSFont */ 83 "Matrix2f", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */ 84 "Matrix3f", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */ 85 "Matrix4f", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */ 86 }; 87 88 if((EPT->getType() >= 0) && (EPT->getType() < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*)))) { 89 // printf("Type %d\n", EPT->getType()); 90 return PrimitiveTypeJavaNameMap[ EPT->getType() ]; 91 } 92 93 assert(false && "GetPrimitiveTypeName : Unknown primitive data type"); 94 return NULL; 95} 96 97static const char* GetVectorTypeName(const RSExportVectorType* EVT) { 98 static const char* VectorTypeJavaNameMap[][3] = { 99 /* 0 */ { "Byte2", "Byte3", "Byte4" }, 100 /* 1 */ { "Short2", "Short3", "Short4" }, 101 /* 2 */ { "Int2", "Int3", "Int4" }, 102 /* 3 */ { "Long2", "Long3", "Long4" }, 103 /* 4 */ { "Float2", "Float3", "Float4" }, 104 }; 105 106 const char** BaseElement; 107 108 switch(EVT->getType()) { 109 case RSExportPrimitiveType::DataTypeSigned8: 110 BaseElement = VectorTypeJavaNameMap[0]; 111 break; 112 113 case RSExportPrimitiveType::DataTypeSigned16: 114 case RSExportPrimitiveType::DataTypeUnsigned8: 115 BaseElement = VectorTypeJavaNameMap[1]; 116 break; 117 118 case RSExportPrimitiveType::DataTypeSigned32: 119 case RSExportPrimitiveType::DataTypeUnsigned16: 120 BaseElement = VectorTypeJavaNameMap[2]; 121 break; 122 123 case RSExportPrimitiveType::DataTypeUnsigned32: 124 BaseElement = VectorTypeJavaNameMap[3]; 125 break; 126 127 case RSExportPrimitiveType::DataTypeFloat32: 128 BaseElement = VectorTypeJavaNameMap[4]; 129 break; 130 131 case RSExportPrimitiveType::DataTypeBool: 132 BaseElement = VectorTypeJavaNameMap[0]; 133 break; 134 135 default: 136 assert(false && "RSReflection::genElementTypeName : Unsupported vector element data type"); 137 break; 138 } 139 140 assert((EVT->getNumElement() > 1) && (EVT->getNumElement() <= 4) && "Number of element in vector type is invalid"); 141 142 return BaseElement[EVT->getNumElement() - 2]; 143} 144 145static const char* GetVectorAccessor(int Index) { 146 static const char* VectorAccessorMap[] = { 147 /* 0 */ "x", 148 /* 1 */ "y", 149 /* 2 */ "z", 150 /* 3 */ "w", 151 }; 152 153 assert((Index >= 0) && (Index < (sizeof(VectorAccessorMap) / sizeof(const char*))) && "Out-of-bound index to access vector member"); 154 155 return VectorAccessorMap[Index]; 156} 157 158static const char* GetPackerAPIName(const RSExportPrimitiveType* EPT) { 159 static const char* PrimitiveTypePackerAPINameMap[] = { 160 "", 161 "", 162 "addF32", /* RSExportPrimitiveType::DataTypeFloat32 */ 163 "addF64", /* RSExportPrimitiveType::DataTypeFloat64 */ 164 "addI8", /* RSExportPrimitiveType::DataTypeSigned8 */ 165 "addI16", /* RSExportPrimitiveType::DataTypeSigned16 */ 166 "addI32", /* RSExportPrimitiveType::DataTypeSigned32 */ 167 "addI64", /* RSExportPrimitiveType::DataTypeSigned64 */ 168 "addU8", /* RSExportPrimitiveType::DataTypeUnsigned8 */ 169 "addU16", /* RSExportPrimitiveType::DataTypeUnsigned16 */ 170 "addU32", /* RSExportPrimitiveType::DataTypeUnsigned32 */ 171 "addU64", /* RSExportPrimitiveType::DataTypeUnsigned64 */ 172 173 "addU16", /* RSExportPrimitiveType::DataTypeUnsigned565 */ 174 "addU16", /* RSExportPrimitiveType::DataTypeUnsigned5551 */ 175 "addU16", /* RSExportPrimitiveType::DataTypeUnsigned4444 */ 176 177 "addBoolean", /* RSExportPrimitiveType::DataTypeBool */ 178 179 "addObj", /* RSExportPrimitiveType::DataTypeRSElement */ 180 "addObj", /* RSExportPrimitiveType::DataTypeRSType */ 181 "addObj", /* RSExportPrimitiveType::DataTypeRSAllocation */ 182 "addObj", /* RSExportPrimitiveType::DataTypeRSSampler */ 183 "addObj", /* RSExportPrimitiveType::DataTypeRSScript */ 184 "addObj", /* RSExportPrimitiveType::DataTypeRSMesh */ 185 "addObj", /* RSExportPrimitiveType::DataTypeRSProgramFragment */ 186 "addObj", /* RSExportPrimitiveType::DataTypeRSProgramVertex */ 187 "addObj", /* RSExportPrimitiveType::DataTypeRSProgramRaster */ 188 "addObj", /* RSExportPrimitiveType::DataTypeRSProgramStore */ 189 "addObj", /* RSExportPrimitiveType::DataTypeRSFont */ 190 "addObj", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */ 191 "addObj", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */ 192 "addObj", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */ 193 }; 194 195 if((EPT->getType() >= 0) && (EPT->getType() < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*)))) 196 return PrimitiveTypePackerAPINameMap[ EPT->getType() ]; 197 198 assert(false && "GetPackerAPIName : Unknown primitive data type"); 199 return NULL; 200} 201 202static std::string GetTypeName(const RSExportType* ET) { 203 switch(ET->getClass()) { 204 case RSExportType::ExportClassPrimitive: 205 case RSExportType::ExportClassConstantArray: 206 return GetPrimitiveTypeName(static_cast<const RSExportPrimitiveType*>(ET)); 207 break; 208 209 case RSExportType::ExportClassPointer: 210 { 211 const RSExportType* PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 212 213 if(PointeeType->getClass() != RSExportType::ExportClassRecord) 214 return "Allocation"; 215 else 216 return RS_TYPE_CLASS_NAME_PREFIX + PointeeType->getName(); 217 } 218 break; 219 220 case RSExportType::ExportClassVector: 221 return GetVectorTypeName(static_cast<const RSExportVectorType*>(ET)); 222 break; 223 224 case RSExportType::ExportClassRecord: 225 return RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 226 break; 227 228 default: 229 assert(false && "Unknown class of type"); 230 break; 231 } 232 233 return ""; 234} 235 236static const char* GetBuiltinElementConstruct(const RSExportType* ET) { 237 if (ET->getClass() == RSExportType::ExportClassPrimitive || ET->getClass() == RSExportType::ExportClassConstantArray) { 238 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET); 239 if (EPT->getKind() == RSExportPrimitiveType::DataKindUser) { 240 static const char* PrimitiveBuiltinElementConstructMap[] = { 241 NULL, 242 NULL, 243 "F32", /* RSExportPrimitiveType::DataTypeFloat32 */ 244 NULL, /* RSExportPrimitiveType::DataTypeFloat64 */ 245 "I8", /* RSExportPrimitiveType::DataTypeSigned8 */ 246 NULL, /* RSExportPrimitiveType::DataTypeSigned16 */ 247 "I32", /* RSExportPrimitiveType::DataTypeSigned32 */ 248 NULL, /* RSExportPrimitiveType::DataTypeSigned64 */ 249 "U8", /* RSExportPrimitiveType::DataTypeUnsigned8 */ 250 NULL, /* RSExportPrimitiveType::DataTypeUnsigned16 */ 251 "U32", /* RSExportPrimitiveType::DataTypeUnsigned32 */ 252 NULL, /* RSExportPrimitiveType::DataTypeUnsigned64 */ 253 254 NULL, /* RSExportPrimitiveType::DataTypeUnsigned565 */ 255 NULL, /* RSExportPrimitiveType::DataTypeUnsigned5551 */ 256 NULL, /* RSExportPrimitiveType::DataTypeUnsigned4444 */ 257 258 "BOOLEAN", /* RSExportPrimitiveType::DataTypeBool */ 259 260 "ELEMENT", /* RSExportPrimitiveType::DataTypeRSElement */ 261 "TYPE", /* RSExportPrimitiveType::DataTypeRSType */ 262 "ALLOCATION", /* RSExportPrimitiveType::DataTypeRSAllocation */ 263 "SAMPLER", /* RSExportPrimitiveType::DataTypeRSSampler */ 264 "SCRIPT", /* RSExportPrimitiveType::DataTypeRSScript */ 265 "MESH", /* RSExportPrimitiveType::DataTypeRSMesh */ 266 "PROGRAM_FRAGMENT", /* RSExportPrimitiveType::DataTypeRSProgramFragment */ 267 "PROGRAM_VERTEX", /* RSExportPrimitiveType::DataTypeRSProgramVertex */ 268 "PROGRAM_RASTER", /* RSExportPrimitiveType::DataTypeRSProgramRaster */ 269 "PROGRAM_STORE", /* RSExportPrimitiveType::DataTypeRSProgramStore */ 270 "FONT", /* RSExportPrimitiveType::DataTypeRSFont */ 271 "MATRIX_2X2", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */ 272 "MATRIX_3X3", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */ 273 "MATRIX_4X4", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */ 274 }; 275 276 if ((EPT->getType() >= 0) && (EPT->getType() < (sizeof(PrimitiveBuiltinElementConstructMap) / sizeof(const char*)))) 277 return PrimitiveBuiltinElementConstructMap[ EPT->getType() ]; 278 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelA) { 279 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 280 return "A_8"; 281 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGB) { 282 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned565) 283 return "RGB_565"; 284 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 285 return "RGB_888"; 286 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGBA) { 287 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned5551) 288 return "RGB_5551"; 289 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned4444) 290 return "RGB_4444"; 291 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 292 return "RGB_8888"; 293 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindIndex) { 294 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned16) 295 return "INDEX_16"; 296 } 297 } else if (ET->getClass() == RSExportType::ExportClassVector) { 298 const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(ET); 299 if (EVT->getKind() == RSExportPrimitiveType::DataKindPosition) { 300 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 301 if (EVT->getNumElement() == 2) 302 return "ATTRIB_POSITION_2"; 303 else if (EVT->getNumElement() == 3) 304 return "ATTRIB_POSITION_3"; 305 } 306 } else if (EVT->getKind() == RSExportPrimitiveType::DataKindTexture) { 307 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 308 if (EVT->getNumElement() == 2) 309 return "ATTRIB_TEXTURE_2"; 310 } 311 } else if (EVT->getKind() == RSExportPrimitiveType::DataKindNormal) { 312 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 313 if (EVT->getNumElement() == 3) 314 return "ATTRIB_NORMAL_3"; 315 } 316 } else if (EVT->getKind() == RSExportPrimitiveType::DataKindColor) { 317 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 318 if (EVT->getNumElement() == 4) 319 return "ATTRIB_COLOR_F32_4"; 320 } else if (EVT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) { 321 if (EVT->getNumElement() == 4) 322 return "ATTRIB_COLOR_U8_4"; 323 } 324 } 325 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 326 return "USER_I32"; /* tread pointer type variable as unsigned int (TODO: this is target dependent) */ 327 } 328 329 return NULL; 330} 331 332static const char* GetElementDataKindName(RSExportPrimitiveType::DataKind DK) { 333 static const char* ElementDataKindNameMap[] = { 334 "Element.DataKind.USER", /* RSExportPrimitiveType::DataKindUser */ 335 "Element.DataKind.COLOR", /* RSExportPrimitiveType::DataKindColor */ 336 "Element.DataKind.POSITION", /* RSExportPrimitiveType::DataKindPosition */ 337 "Element.DataKind.TEXTURE", /* RSExportPrimitiveType::DataKindTexture */ 338 "Element.DataKind.NORMAL", /* RSExportPrimitiveType::DataKindNormal */ 339 "Element.DataKind.INDEX", /* RSExportPrimitiveType::DataKindIndex */ 340 "Element.DataKind.POINT_SIZE", /* RSExportPrimitiveType::DataKindPointSize */ 341 "Element.DataKind.PIXEL_L", /* RSExportPrimitiveType::DataKindPixelL */ 342 "Element.DataKind.PIXEL_A", /* RSExportPrimitiveType::DataKindPixelA */ 343 "Element.DataKind.PIXEL_LA", /* RSExportPrimitiveType::DataKindPixelLA */ 344 "Element.DataKind.PIXEL_RGB", /* RSExportPrimitiveType::DataKindPixelRGB */ 345 "Element.DataKind.PIXEL_RGBA", /* RSExportPrimitiveType::DataKindPixelRGBA */ 346 }; 347 348 if((DK >= 0) && (DK < (sizeof(ElementDataKindNameMap) / sizeof(const char*)))) 349 return ElementDataKindNameMap[ DK ]; 350 else 351 return NULL; 352} 353 354static const char* GetElementDataTypeName(RSExportPrimitiveType::DataType DT) { 355 static const char* ElementDataTypeNameMap[] = { 356 NULL, 357 NULL, 358 "Element.DataType.FLOAT_32", /* RSExportPrimitiveType::DataTypeFloat32 */ 359 NULL, /* RSExportPrimitiveType::DataTypeFloat64 */ 360 "Element.DataType.SIGNED_8", /* RSExportPrimitiveType::DataTypeSigned8 */ 361 "Element.DataType.SIGNED_16", /* RSExportPrimitiveType::DataTypeSigned16 */ 362 "Element.DataType.SIGNED_32", /* RSExportPrimitiveType::DataTypeSigned32 */ 363 NULL, /* RSExportPrimitiveType::DataTypeSigned64 */ 364 "Element.DataType.UNSIGNED_8", /* RSExportPrimitiveType::DataTypeUnsigned8 */ 365 "Element.DataType.UNSIGNED_16", /* RSExportPrimitiveType::DataTypeUnsigned16 */ 366 "Element.DataType.UNSIGNED_32", /* RSExportPrimitiveType::DataTypeUnsigned32 */ 367 NULL, /* RSExportPrimitiveType::DataTypeUnsigned64 */ 368 369 "Element.DataType.UNSIGNED_5_6_5", /* RSExportPrimitiveType::DataTypeUnsigned565 */ 370 "Element.DataType.UNSIGNED_5_5_5_1", /* RSExportPrimitiveType::DataTypeUnsigned5551 */ 371 "Element.DataType.UNSIGNED_4_4_4_4", /* RSExportPrimitiveType::DataTypeUnsigned4444 */ 372 373 "Element.DataType.BOOLEAN", /* RSExportPrimitiveType::DataTypeBool */ 374 375 "Element.DataType.RS_ELEMENT", /* RSExportPrimitiveType::DataTypeRSElement */ 376 "Element.DataType.RS_TYPE", /* RSExportPrimitiveType::DataTypeRSType */ 377 "Element.DataType.RS_ALLOCATION", /* RSExportPrimitiveType::DataTypeRSAllocation */ 378 "Element.DataType.RS_SAMPLER", /* RSExportPrimitiveType::DataTypeRSSampler */ 379 "Element.DataType.RS_SCRIPT", /* RSExportPrimitiveType::DataTypeRSScript */ 380 "Element.DataType.RS_MESH", /* RSExportPrimitiveType::DataTypeRSMesh */ 381 "Element.DataType.RS_PROGRAM_FRAGMENT", /* RSExportPrimitiveType::DataTypeRSProgramFragment */ 382 "Element.DataType.RS_PROGRAM_VERTEX", /* RSExportPrimitiveType::DataTypeRSProgramVertex */ 383 "Element.DataType.RS_PROGRAM_RASTER", /* RSExportPrimitiveType::DataTypeRSProgramRaster */ 384 "Element.DataType.RS_PROGRAM_STORE", /* RSExportPrimitiveType::DataTypeRSProgramStore */ 385 "Element.DataType.RS_FONT", /* RSExportPrimitiveType::DataTypeRSFont */ 386 "Element.DataType.RS_MATRIX_2X2", /* RSExportPrimitiveType::DataTypeRSMatrix2x2 */ 387 "Element.DataType.RS_MATRIX_3X3", /* RSExportPrimitiveType::DataTypeRSMatrix3x3 */ 388 "Element.DataType.RS_MATRIX_4X4", /* RSExportPrimitiveType::DataTypeRSMatrix4x4 */ 389 }; 390 391 if((DT >= 0) && (DT < (sizeof(ElementDataTypeNameMap) / sizeof(const char*)))) 392 return ElementDataTypeNameMap[ DT ]; 393 else 394 return NULL; 395} 396 397bool RSReflection::openScriptFile(Context&C, const std::string& ClassName, std::string& ErrorMsg) { 398 if(!C.mUseStdout) { 399 C.mOF.clear(); 400 std::string _path = RSSlangReflectUtils::ComputePackagedPath( 401 mRSContext->getReflectJavaPathName().c_str(), C.getPackageName().c_str()); 402 403 RSSlangReflectUtils::mkdir_p(_path.c_str()); 404 C.mOF.open(( _path + "/" + ClassName + ".java" ).c_str()); 405 if(!C.mOF.good()) { 406 ErrorMsg = "failed to open file '" + _path + "/" + ClassName + ".java' for write"; 407 408 return false; 409 } 410 } 411 return true; 412} 413 414/****************************** Methods to generate script class ******************************/ 415bool RSReflection::genScriptClass(Context& C, const std::string& ClassName, std::string& ErrorMsg) { 416 /* Open the file */ 417 if (!openScriptFile(C, ClassName, ErrorMsg)) { 418 return false; 419 } 420 421 if(!C.startClass(Context::AM_Public, false, ClassName, RS_SCRIPT_CLASS_SUPER_CLASS_NAME, ErrorMsg)) 422 return false; 423 424 genScriptClassConstructor(C); 425 426 /* Reflect export variable */ 427 for(RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(); 428 I != mRSContext->export_vars_end(); 429 I++) 430 genExportVariable(C, *I); 431 432 /* Reflect export function */ 433 for(RSContext::const_export_func_iterator I = mRSContext->export_funcs_begin(); 434 I != mRSContext->export_funcs_end(); 435 I++) 436 genExportFunction(C, *I); 437 438 C.endClass(); 439 440 return true; 441} 442 443void RSReflection::genScriptClassConstructor(Context& C) { 444 C.indent() << "// Constructor" << endl; 445 C.startFunction(Context::AM_Public, false, NULL, C.getClassName(), 4, "RenderScript", "rs", 446 "Resources", "resources", 447 "int", "id", 448 "boolean", "isRoot"); 449 /* Call constructor of super class */ 450 C.indent() << "super(rs, resources, id, isRoot);" << endl; 451 452 /* If an exported variable has initial value, reflect it */ 453 454 for(RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(); 455 I != mRSContext->export_vars_end(); 456 I++) 457 { 458 const RSExportVar* EV = *I; 459 if(!EV->getInit().isUninit()) 460 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit()); 461 } 462 463 C.endFunction(); 464 return; 465} 466 467void RSReflection::genInitBoolExportVariable(Context& C, const std::string& VarName, const APValue& Val) { 468 assert(!Val.isUninit() && "Not a valid initializer"); 469 470 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 471 assert((Val.getKind() == APValue::Int) && "Bool type has wrong initial APValue"); 472 473 if (Val.getInt().getSExtValue() == 0) { 474 C.out() << "false"; 475 } else { 476 C.out() << "true"; 477 } 478 C.out() << ";" << endl; 479 480 return; 481} 482 483void RSReflection::genInitPrimitiveExportVariable(Context& C, const std::string& VarName, const APValue& Val) { 484 assert(!Val.isUninit() && "Not a valid initializer"); 485 486 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 487 switch(Val.getKind()) { 488 case APValue::Int: C.out() << Val.getInt().getSExtValue(); break; 489 490 case APValue::Float: { 491 llvm::APFloat apf = Val.getFloat(); 492 if (apf.semanticsPrecision(apf.getSemantics()) == 24 /*llvm::APFloat::IEEEsingle*/) { 493 C.out() << apf.convertToFloat(); 494 } else { 495 C.out() << apf.convertToDouble(); 496 } 497 break; 498 } 499 500 case APValue::ComplexInt: 501 case APValue::ComplexFloat: 502 case APValue::LValue: 503 case APValue::Vector: 504 assert(false && "Primitive type cannot have such kind of initializer"); 505 break; 506 507 default: 508 assert(false && "Unknown kind of initializer"); 509 break; 510 } 511 C.out() << ";" << endl; 512 513 return; 514} 515 516void RSReflection::genInitExportVariable(Context& C, const RSExportType* ET, const std::string& VarName, const APValue& Val) { 517 assert(!Val.isUninit() && "Not a valid initializer"); 518 519 switch(ET->getClass()) { 520 case RSExportType::ExportClassPrimitive: 521 case RSExportType::ExportClassConstantArray: 522 { 523 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET); 524 if (EPT->getType() == RSExportPrimitiveType::DataTypeBool) { 525 genInitBoolExportVariable(C, VarName, Val); 526 } else { 527 genInitPrimitiveExportVariable(C, VarName, Val); 528 } 529 break; 530 } 531 532 case RSExportType::ExportClassPointer: 533 if(!Val.isInt() || Val.getInt().getSExtValue() != 0) 534 std::cout << "Initializer which is non-NULL to pointer type variable will be ignored" << endl; 535 break; 536 537 case RSExportType::ExportClassVector: 538 { 539 const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(ET); 540 switch(Val.getKind()) { 541 case APValue::Int: 542 case APValue::Float: 543 for(int i=0;i<EVT->getNumElement();i++) { 544 std::string Name = VarName + "." + GetVectorAccessor(i); 545 genInitPrimitiveExportVariable(C, Name, Val); 546 } 547 break; 548 549 case APValue::Vector: 550 { 551 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " << GetVectorTypeName(EVT) << "();" << endl; 552 553 unsigned NumElements = std::min(static_cast<unsigned>(EVT->getNumElement()), Val.getVectorLength()); 554 for(unsigned i=0;i<NumElements;i++) { 555 const APValue& ElementVal = Val.getVectorElt(i); 556 std::string Name = VarName + "." + GetVectorAccessor(i); 557 genInitPrimitiveExportVariable(C, Name, ElementVal); 558 } 559 } 560 break; 561 } 562 } 563 break; 564 565 /* TODO: Resolving initializer of a record type variable is complex. It cannot obtain by just simply evaluating the initializer expression. */ 566 case RSExportType::ExportClassRecord: 567 { 568 /* 569 unsigned InitIndex = 0; 570 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 571 572 assert((Val.getKind() == APValue::Vector) && "Unexpected type of initializer for record type variable"); 573 574 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() << "."RS_TYPE_ITEM_CLASS_NAME"();" << endl; 575 576 for(RSExportRecordType::const_field_iterator I = ERT->fields_begin(); 577 I != ERT->fields_end(); 578 I++) 579 { 580 const RSExportRecordType::Field* F = *I; 581 std::string FieldName = VarName + "." + F->getName(); 582 583 if(InitIndex > Val.getVectorLength()) 584 break; 585 586 genInitPrimitiveExportVariable(C, FieldName, Val.getVectorElt(InitIndex++)); 587 } 588 */ 589 assert(false && "Unsupported initializer for record type variable currently"); 590 } 591 break; 592 593 default: 594 assert(false && "Unknown class of type"); 595 break; 596 } 597 return; 598} 599 600void RSReflection::genExportVariable(Context& C, const RSExportVar* EV) { 601 const RSExportType* ET = EV->getType(); 602 603 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << " = " << C.getNextExportVarSlot() << ";" << endl; 604 605 switch(ET->getClass()) { 606 case RSExportType::ExportClassPrimitive: 607 case RSExportType::ExportClassConstantArray: 608 genPrimitiveTypeExportVariable(C, EV); 609 break; 610 611 case RSExportType::ExportClassPointer: 612 genPointerTypeExportVariable(C, EV); 613 break; 614 615 case RSExportType::ExportClassVector: 616 genVectorTypeExportVariable(C, EV); 617 break; 618 619 case RSExportType::ExportClassRecord: 620 genRecordTypeExportVariable(C, EV); 621 break; 622 623 default: 624 assert(false && "Unknown class of type"); 625 break; 626 } 627 628 return; 629} 630 631void RSReflection::genExportFunction(Context& C, const RSExportFunc* EF) { 632 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" << endl; 633 634 /* invoke_*() */ 635 Context::ArgTy Args; 636 637 for(RSExportFunc::const_param_iterator I = EF->params_begin(); 638 I != EF->params_end(); 639 I++) 640 { 641 const RSExportFunc::Parameter* P = *I; 642 Args.push_back( make_pair(GetTypeName(P->getType()), P->getName()) ); 643 } 644 645 C.startFunction(Context::AM_Public, false, "void", "invoke_" + EF->getName(), Args); 646 647 if(!EF->hasParam()) { 648 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" << endl; 649 } else { 650 const RSExportRecordType* ERT = EF->getParamPacketType(); 651 std::string FieldPackerName = EF->getName() + "_fp"; 652 653 if(genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 654 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 655 656 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " << FieldPackerName << ");" << endl; 657 } 658 659 C.endFunction(); 660 return; 661} 662 663void RSReflection::genPrimitiveTypeExportVariable(Context& C, const RSExportVar* EV) { 664 assert(( EV->getType()->getClass() == RSExportType::ExportClassPrimitive || 665 EV->getType()->getClass() == RSExportType::ExportClassConstantArray 666 ) && "Variable should be type of primitive here"); 667 668 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(EV->getType()); 669 const char* TypeName = GetPrimitiveTypeName(EPT); 670 671 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 672 673 /* set_*() */ 674 if(!EV->isConst()) { 675 C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName, "v"); 676 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 677 678 if(EPT->isRSObjectType()) 679 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", (v == null) ? 0 : v.getID());" << endl; 680 else 681 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", v);" << endl; 682 683 C.endFunction(); 684 } 685 686 genGetExportVariable(C, TypeName, EV->getName()); 687 688 return; 689} 690 691void RSReflection::genPointerTypeExportVariable(Context& C, const RSExportVar* EV) { 692 const RSExportType* ET = EV->getType(); 693 const RSExportType* PointeeType; 694 std::string TypeName; 695 696 assert((ET->getClass() == RSExportType::ExportClassPointer) && "Variable should be type of pointer here"); 697 698 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 699 TypeName = GetTypeName(ET); 700 701 /* bind_*() */ 702 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 703 704 C.startFunction(Context::AM_Public, false, "void", "bind_" + EV->getName(), 1, TypeName.c_str(), "v"); 705 706 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 707 C.indent() << "if(v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl; 708 709 if(PointeeType->getClass() == RSExportType::ExportClassRecord) 710 C.indent() << "else bindAllocation(v.getAllocation(), "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl; 711 else 712 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl; 713 714 C.endFunction(); 715 716 genGetExportVariable(C, TypeName, EV->getName()); 717 718 return; 719} 720 721void RSReflection::genVectorTypeExportVariable(Context& C, const RSExportVar* EV) { 722 assert((EV->getType()->getClass() == RSExportType::ExportClassVector) && "Variable should be type of vector here"); 723 724 const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(EV->getType()); 725 const char* TypeName = GetVectorTypeName(EVT); 726 const char* FieldPackerName = "fp"; 727 728 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 729 730 /* set_*() */ 731 if(!EV->isConst()) { 732 C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName, "v"); 733 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 734 735 if(genCreateFieldPacker(C, EVT, FieldPackerName)) 736 genPackVarOfType(C, EVT, "v", FieldPackerName); 737 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " << FieldPackerName << ");" << endl; 738 739 C.endFunction(); 740 } 741 742 genGetExportVariable(C, TypeName, EV->getName()); 743 return; 744} 745 746void RSReflection::genRecordTypeExportVariable(Context& C, const RSExportVar* EV) { 747 assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && "Variable should be type of struct here"); 748 749 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(EV->getType()); 750 std::string TypeName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 751 const char* FieldPackerName = "fp"; 752 753 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 754 755 /* set_*() */ 756 if(!EV->isConst()) { 757 C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName.c_str(), "v"); 758 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 759 760 if(genCreateFieldPacker(C, ERT, FieldPackerName)) 761 genPackVarOfType(C, ERT, "v", FieldPackerName); 762 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " << FieldPackerName << ");" << endl; 763 764 C.endFunction(); 765 } 766 767 genGetExportVariable(C, TypeName.c_str(), EV->getName()); 768 return; 769} 770 771void RSReflection::genGetExportVariable(Context& C, const std::string& TypeName, const std::string& VarName) { 772 C.startFunction(Context::AM_Public, false, TypeName.c_str(), "get_" + VarName, 0); 773 774 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << endl; 775 776 C.endFunction(); 777 return; 778} 779 780/****************************** Methods to generate script class /end ******************************/ 781 782bool RSReflection::genCreateFieldPacker(Context& C, const RSExportType* ET, const char* FieldPackerName) { 783 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 784 if(AllocSize > 0) 785 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" << AllocSize << ");" << endl; 786 else 787 return false; 788 return true; 789} 790 791void RSReflection::genPackVarOfType(Context& C, const RSExportType* ET, const char* VarName, const char* FieldPackerName) { 792 switch(ET->getClass()) { 793 case RSExportType::ExportClassPrimitive: 794 case RSExportType::ExportClassVector: 795 C.indent() << FieldPackerName << "." << GetPackerAPIName(static_cast<const RSExportPrimitiveType*>(ET)) << "(" << VarName << ");" << endl; 796 break; 797 798 case RSExportType::ExportClassConstantArray: { 799 if (ET->getName().compare("addObj") == 0) { 800 C.indent() << FieldPackerName << "." << "addObj" << "(" << VarName << ");" << endl; 801 } else { 802 C.indent() << FieldPackerName << "." << GetPackerAPIName(static_cast<const RSExportPrimitiveType*>(ET)) << "(" << VarName << ");" << endl; 803 } 804 break; 805 } 806 807 case RSExportType::ExportClassPointer: 808 { 809 /* Must reflect as type Allocation in Java */ 810 const RSExportType* PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 811 812 if(PointeeType->getClass() != RSExportType::ExportClassRecord) 813 C.indent() << FieldPackerName << ".addI32(" << VarName << ".getPtr());" << endl; 814 else 815 C.indent() << FieldPackerName << ".addI32(" << VarName << ".getAllocation().getPtr());" << endl; 816 } 817 break; 818 819 case RSExportType::ExportClassRecord: 820 { 821 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 822 int Pos = 0; /* relative pos from now on in field packer */ 823 824 for(RSExportRecordType::const_field_iterator I = ERT->fields_begin(); 825 I != ERT->fields_end(); 826 I++) 827 { 828 const RSExportRecordType::Field* F = *I; 829 std::string FieldName; 830 size_t FieldOffset = F->getOffsetInParent(); 831 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 832 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 833 834 if(VarName != NULL) 835 FieldName = VarName + ("." + F->getName()); 836 else 837 FieldName = F->getName(); 838 839 if(FieldOffset > Pos) 840 C.indent() << FieldPackerName << ".skip(" << (FieldOffset - Pos) << ");" << endl; 841 842 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 843 844 if(FieldAllocSize > FieldStoreSize) /* there's padding in the field type */ 845 C.indent() << FieldPackerName << ".skip(" << (FieldAllocSize - FieldStoreSize) << ");" << endl; 846 847 Pos = FieldOffset + FieldAllocSize; 848 } 849 850 /* There maybe some padding after the struct */ 851 size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos; 852 if(Padding > 0) 853 C.indent() << FieldPackerName << ".skip(" << Padding << ");" << endl; 854 } 855 break; 856 857 default: 858 assert(false && "Unknown class of type"); 859 break; 860 } 861 862 return; 863} 864 865void RSReflection::genNewItemBufferIfNull(Context& C, const char* Index) { 866 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) " 867 RS_TYPE_ITEM_BUFFER_NAME" = new "RS_TYPE_ITEM_CLASS_NAME"[mType.getX() /* count */];" << endl; 868 if (Index != NULL) 869 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) " 870 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = new "RS_TYPE_ITEM_CLASS_NAME"();" << endl; 871 return; 872} 873 874void RSReflection::genNewItemBufferPackerIfNull(Context& C) { 875 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_PACKER_NAME" == null) " 876 RS_TYPE_ITEM_BUFFER_PACKER_NAME" = new FieldPacker("RS_TYPE_ITEM_CLASS_NAME".sizeof * mType.getX() /* count */);" << endl; 877 return; 878} 879 880/****************************** Methods to generate type class ******************************/ 881bool RSReflection::genTypeClass(Context& C, const RSExportRecordType* ERT, std::string& ErrorMsg) { 882 std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName(); 883 884 /* Open the file */ 885 if (!openScriptFile(C, ClassName, ErrorMsg)) { 886 return false; 887 } 888 889 if(!C.startClass(Context::AM_Public, false, ClassName, RS_TYPE_CLASS_SUPER_CLASS_NAME, ErrorMsg)) 890 return false; 891 892 if(!genTypeItemClass(C, ERT, ErrorMsg)) 893 return false; 894 895 /* Declare item buffer and item buffer packer */ 896 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[];" << endl; 897 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" << endl; 898 899 genTypeClassConstructor(C, ERT); 900 genTypeClassCopyToArray(C, ERT); 901 genTypeClassItemSetter(C, ERT); 902 genTypeClassItemGetter(C, ERT); 903 genTypeClassComponentSetter(C, ERT); 904 genTypeClassComponentGetter(C, ERT); 905 genTypeClassCopyAll(C, ERT); 906 907 C.endClass(); 908 909 return true; 910} 911 912bool RSReflection::genTypeItemClass(Context& C, const RSExportRecordType* ERT, std::string& ErrorMsg) { 913 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 914 C.startBlock(); 915 916 C.indent() << "public static final int sizeof = " << RSExportType::GetTypeAllocSize(ERT) << ";" << endl; 917 918 /* Member elements */ 919 C.out() << endl; 920 for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin(); 921 FI != ERT->fields_end(); 922 FI++) { 923 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() << ";" << endl; 924 } 925 926 /* Constructor */ 927 C.out() << endl; 928 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 929 C.startBlock(); 930 931 for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin(); 932 FI != ERT->fields_end(); 933 FI++) 934 { 935 const RSExportRecordType::Field* F = *FI; 936 if( (F->getType()->getClass() == RSExportType::ExportClassVector) || 937 (F->getType()->getClass() == RSExportType::ExportClassRecord) || 938 (F->getType()->getClass() == RSExportType::ExportClassConstantArray) 939 ) { 940 C.indent() << F->getName() << " = new " << GetTypeName(F->getType()) << "();" << endl; 941 } 942 } 943 944 C.endBlock(); /* end Constructor */ 945 946 C.endBlock(); /* end Item class */ 947 948 949 return true; 950} 951 952void RSReflection::genTypeClassConstructor(Context& C, const RSExportRecordType* ERT) { 953 const char* RenderScriptVar = "rs"; 954 955 C.startFunction(Context::AM_Public, true, "Element", "createElement", 1, "RenderScript", RenderScriptVar); 956 genBuildElement(C, ERT, RenderScriptVar); 957 C.endFunction(); 958 959 C.startFunction(Context::AM_Public, false, NULL, C.getClassName(), 2, "RenderScript", RenderScriptVar, 960 "int", "count"); 961 962 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << endl; 963 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << endl; 964 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" << endl; 965 /* Call init() in super class */ 966 C.indent() << "init(" << RenderScriptVar << ", count);" << endl; 967 C.endFunction(); 968 969 return; 970} 971 972void RSReflection::genTypeClassCopyToArray(Context& C, const RSExportRecordType* ERT) { 973 C.startFunction(Context::AM_Private, false, "void", "copyToArray", 2, RS_TYPE_ITEM_CLASS_NAME, "i", 974 "int", "index"); 975 976 genNewItemBufferPackerIfNull(C); 977 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" << endl; 978 979 genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 980 981 C.endFunction(); 982 return; 983} 984 985void RSReflection::genTypeClassItemSetter(Context& C, const RSExportRecordType* ERT) { 986 C.startFunction(Context::AM_Public, false, "void", "set", 3, RS_TYPE_ITEM_CLASS_NAME, "i", 987 "int", "index", 988 "boolean", "copyNow"); 989 genNewItemBufferIfNull(C, NULL); 990 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << endl; 991 992 C.indent() << "if (copyNow) "; 993 C.startBlock(); 994 995 C.indent() << "copyToArray(i, index);" << endl; 996 C.indent() << "mAllocation.subData1D(index, 1, "RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << endl; 997 998 C.endBlock(); /* end if (copyNow) */ 999 1000 C.endFunction(); 1001 return; 1002} 1003 1004void RSReflection::genTypeClassItemGetter(Context& C, const RSExportRecordType* ERT) { 1005 C.startFunction(Context::AM_Public, false, RS_TYPE_ITEM_CLASS_NAME, "get", 1, "int", "index"); 1006 //C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" << endl; 1007 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << endl; 1008 C.endFunction(); 1009 return; 1010} 1011void RSReflection::genTypeClassComponentSetter(Context& C, const RSExportRecordType* ERT) { 1012 for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin(); 1013 FI != ERT->fields_end(); 1014 FI++) 1015 { 1016 const RSExportRecordType::Field* F = *FI; 1017 size_t FieldOffset = F->getOffsetInParent(); 1018 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1019 unsigned FieldIndex = C.getFieldIndex(F); 1020 1021 C.startFunction(Context::AM_Public, false, "void", "set_" + F->getName(), 3, "int", "index", 1022 GetTypeName(F->getType()).c_str(), "v", 1023 "boolean", "copyNow"); 1024 genNewItemBufferPackerIfNull(C); 1025 genNewItemBufferIfNull(C, "index"); 1026 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() << " = v;" << endl; 1027 1028 C.indent() << "if (copyNow) "; 1029 C.startBlock(); 1030 1031 if(FieldOffset > 0) 1032 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " << FieldOffset << ");" << endl; 1033 else 1034 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" << endl; 1035 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1036 1037 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" << endl; 1038 genPackVarOfType(C, F->getType(), "v", "fp"); 1039 C.indent() << "mAllocation.subElementData(index, " << FieldIndex << ", fp);" << endl; 1040 1041 C.endBlock(); /* end if (copyNow) */ 1042 1043 C.endFunction(); 1044 } 1045 return; 1046} 1047 1048void RSReflection::genTypeClassComponentGetter(Context& C, const RSExportRecordType* ERT) { 1049 for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin(); 1050 FI != ERT->fields_end(); 1051 FI++) 1052 { 1053 const RSExportRecordType::Field* F = *FI; 1054 C.startFunction(Context::AM_Public, false, GetTypeName(F->getType()).c_str(), "get_" + F->getName(), 1, "int", "index"); 1055 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" << endl; 1056 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() << ";" << endl; 1057 C.endFunction(); 1058 } 1059 return; 1060} 1061 1062void RSReflection::genTypeClassCopyAll(Context& C, const RSExportRecordType* ERT) { 1063 C.startFunction(Context::AM_Public, false, "void", "copyAll", 0); 1064 1065 C.indent() << "for (int ct=0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++) copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" << endl; 1066 C.indent() << "mAllocation.data("RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << endl; 1067 1068 C.endFunction(); 1069 return; 1070} 1071 1072/****************************** Methods to generate type class /end ******************************/ 1073 1074/******************** Methods to create Element in Java of given record type ********************/ 1075void RSReflection::genBuildElement(Context& C, const RSExportRecordType* ERT, const char* RenderScriptVar) { 1076 const char* ElementBuilderName = "eb"; 1077 1078 /* Create element builder */ 1079 // C.startBlock(true); 1080 1081 C.indent() << "Element.Builder " << ElementBuilderName << " = new Element.Builder(" << RenderScriptVar << ");" << endl; 1082 1083 /* eb.add(...) */ 1084 genAddElementToElementBuilder(C, ERT, "", ElementBuilderName, RenderScriptVar); 1085 1086 C.indent() << "return " << ElementBuilderName << ".create();" << endl; 1087 1088 // C.endBlock(); 1089 return; 1090} 1091 1092#define EB_ADD(x, ...) \ 1093 C.indent() << ElementBuilderName << ".add(Element." << x ##__VA_ARGS__ ", \"" << VarName << "\");" << endl; \ 1094 C.incFieldIndex() 1095 1096void RSReflection::genAddElementToElementBuilder(Context& C, const RSExportType* ET, const std::string& VarName, const char* ElementBuilderName, const char* RenderScriptVar) { 1097 const char* ElementConstruct = GetBuiltinElementConstruct(ET); 1098 1099 if(ElementConstruct != NULL) { 1100 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")"); 1101 } else { 1102 if ((ET->getClass() == RSExportType::ExportClassPrimitive) || 1103 (ET->getClass() == RSExportType::ExportClassVector) || 1104 (ET->getClass() == RSExportType::ExportClassConstantArray) 1105 ) { 1106 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET); 1107 const char* DataKindName = GetElementDataKindName(EPT->getKind()); 1108 const char* DataTypeName = GetElementDataTypeName(EPT->getType()); 1109 int Size = (ET->getClass() == RSExportType::ExportClassVector) ? static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1; 1110 1111 switch(EPT->getKind()) { 1112 case RSExportPrimitiveType::DataKindColor: 1113 case RSExportPrimitiveType::DataKindPosition: 1114 case RSExportPrimitiveType::DataKindTexture: 1115 case RSExportPrimitiveType::DataKindNormal: 1116 case RSExportPrimitiveType::DataKindPointSize: 1117 /* Element.createAttrib() */ 1118 EB_ADD("createAttrib(" << RenderScriptVar << ", " << DataTypeName << ", " << DataKindName << ", " << Size << ")"); 1119 break; 1120 1121 case RSExportPrimitiveType::DataKindIndex: 1122 /* Element.createIndex() */ 1123 EB_ADD("createAttrib(" << RenderScriptVar << ")"); 1124 break; 1125 1126 case RSExportPrimitiveType::DataKindPixelL: 1127 case RSExportPrimitiveType::DataKindPixelA: 1128 case RSExportPrimitiveType::DataKindPixelLA: 1129 case RSExportPrimitiveType::DataKindPixelRGB: 1130 case RSExportPrimitiveType::DataKindPixelRGBA: 1131 /* Element.createPixel() */ 1132 EB_ADD("createVector(" << RenderScriptVar << ", " << DataTypeName << ", " << DataKindName << ")"); 1133 break; 1134 1135 case RSExportPrimitiveType::DataKindUser: 1136 default: 1137 if (EPT->getClass() == RSExportType::ExportClassPrimitive || EPT->getClass() == RSExportType::ExportClassConstantArray) { 1138 /* Element.createUser() */ 1139 EB_ADD("createUser(" << RenderScriptVar << ", " << DataTypeName << ")"); 1140 } else { /* (ET->getClass() == RSExportType::ExportClassVector) must hold here */ 1141 /* Element.createVector() */ 1142 EB_ADD("createVector(" << RenderScriptVar << ", " << DataTypeName << ", " << Size << ")"); 1143 } 1144 break; 1145 } 1146 } else if(ET->getClass() == RSExportType::ExportClassPointer) { 1147 /* Pointer type variable should be resolved in GetBuiltinElementConstruct() */ 1148 assert(false && "??"); 1149 } else if(ET->getClass() == RSExportType::ExportClassRecord) { 1150 /* 1151 * Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType. 1152 * TODO: Generalize these two function such that there's no duplicated codes. 1153 */ 1154 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 1155 int Pos = 0; /* relative pos from now on */ 1156 1157 for(RSExportRecordType::const_field_iterator I = ERT->fields_begin(); 1158 I != ERT->fields_end(); 1159 I++) 1160 { 1161 const RSExportRecordType::Field* F = *I; 1162 std::string FieldName; 1163 size_t FieldOffset = F->getOffsetInParent(); 1164 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1165 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1166 1167 if(!VarName.empty()) 1168 FieldName = VarName + "." + F->getName(); 1169 else 1170 FieldName = F->getName(); 1171 1172 /* alignment */ 1173 genAddPaddingToElementBuiler(C, (FieldOffset - Pos), ElementBuilderName, RenderScriptVar); 1174 1175 /* eb.add(...) */ 1176 C.addFieldIndexMapping(F); 1177 genAddElementToElementBuilder(C, F->getType(), FieldName, ElementBuilderName, RenderScriptVar); 1178 1179 /* There is padding within the field type */ 1180 genAddPaddingToElementBuiler(C, (FieldAllocSize - FieldStoreSize), ElementBuilderName, RenderScriptVar); 1181 1182 Pos = FieldOffset + FieldAllocSize; 1183 } 1184 1185 /* There maybe some padding after the struct */ 1186 //unsigned char align = RSExportType::GetTypeAlignment(ERT); 1187 //size_t siz = RSExportType::GetTypeAllocSize(ERT); 1188 size_t siz1 = RSExportType::GetTypeStoreSize(ERT); 1189 1190 genAddPaddingToElementBuiler(C, siz1 - Pos, ElementBuilderName, RenderScriptVar); 1191 } else { 1192 assert(false && "Unknown class of type"); 1193 } 1194 } 1195} 1196 1197void RSReflection::genAddPaddingToElementBuiler(Context& C, size_t PaddingSize, const char* ElementBuilderName, const char* RenderScriptVar) { 1198 while(PaddingSize > 0) { 1199 const std::string& VarName = C.createPaddingField(); 1200 if(PaddingSize >= 4) { 1201 EB_ADD("U32(" << RenderScriptVar << ")"); 1202 PaddingSize -= 4; 1203 } else if(PaddingSize >= 2) { 1204 EB_ADD("U16(" << RenderScriptVar << ")"); 1205 PaddingSize -= 2; 1206 } else if(PaddingSize >= 1) { 1207 EB_ADD("U8(" << RenderScriptVar << ")"); 1208 PaddingSize -= 1; 1209 } 1210 } 1211 return; 1212} 1213 1214#undef EB_ADD 1215/******************** Methods to create Element in Java of given record type /end ********************/ 1216 1217bool RSReflection::reflect(const char* OutputPackageName, const std::string& InputFileName, const std::string& OutputBCFileName) { 1218 Context *C = NULL; 1219 std::string ResourceId = ""; 1220 1221 if(!GetClassNameFromFileName(OutputBCFileName, ResourceId)) 1222 return false; 1223 1224 if(ResourceId.empty()) 1225 ResourceId = "<Resource ID>"; 1226 1227 if((OutputPackageName == NULL) || (*OutputPackageName == '\0') || strcmp(OutputPackageName, "-") == 0) 1228 C = new Context(InputFileName, "<Package Name>", ResourceId, true); 1229 else 1230 C = new Context(InputFileName, OutputPackageName, ResourceId, false); 1231 1232 if(C != NULL) { 1233 std::string ErrorMsg, ScriptClassName; 1234 /* class ScriptC_<ScriptName> */ 1235 if(!GetClassNameFromFileName(InputFileName, ScriptClassName)) 1236 return false; 1237 1238 if(ScriptClassName.empty()) 1239 ScriptClassName = "<Input Script Name>"; 1240 1241 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX); 1242 1243 if (mRSContext->getLicenseNote() != NULL) { 1244 C->setLicenseNote(*(mRSContext->getLicenseNote())); 1245 } 1246 1247 if(!genScriptClass(*C, ScriptClassName, ErrorMsg)) { 1248 std::cerr << "Failed to generate class " << ScriptClassName << " (" << ErrorMsg << ")" << endl; 1249 return false; 1250 } 1251 1252 /* class ScriptField_<TypeName> */ 1253 for(RSContext::const_export_type_iterator TI = mRSContext->export_types_begin(); 1254 TI != mRSContext->export_types_end(); 1255 TI++) 1256 { 1257 const RSExportType* ET = TI->getValue(); 1258 1259 if(ET->getClass() == RSExportType::ExportClassRecord) { 1260 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 1261 1262 if(!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1263 std::cerr << "Failed to generate type class for struct '" << ERT->getName() << "' (" << ErrorMsg << ")" << endl; 1264 return false; 1265 } 1266 } 1267 } 1268 } 1269 1270 return true; 1271} 1272 1273/****************************** RSReflection::Context ******************************/ 1274const char* const RSReflection::Context::ApacheLicenseNote = 1275 "/*\n" 1276 " * Copyright (C) 2010 The Android Open Source Project\n" 1277 " *\n" 1278 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 1279 " * you may not use this file except in compliance with the License.\n" 1280 " * You may obtain a copy of the License at\n" 1281 " *\n" 1282 " * http://www.apache.org/licenses/LICENSE-2.0\n" 1283 " *\n" 1284 " * Unless required by applicable law or agreed to in writing, software\n" 1285 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 1286 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" 1287 " * See the License for the specific language governing permissions and\n" 1288 " * limitations under the License.\n" 1289 " */\n" 1290 "\n"; 1291 1292const char* const RSReflection::Context::Import[] = { 1293 /* RenderScript java class */ 1294 "android.renderscript.*", 1295 /* Import R */ 1296 "android.content.res.Resources", 1297 /* Import for debugging */ 1298 "android.util.Log", 1299}; 1300 1301const char* RSReflection::Context::AccessModifierStr(AccessModifier AM) { 1302 switch(AM) { 1303 case AM_Public: return "public"; break; 1304 case AM_Protected: return "protected"; break; 1305 case AM_Private: return "private"; break; 1306 default: return ""; break; 1307 } 1308} 1309 1310bool RSReflection::Context::startClass(AccessModifier AM, bool IsStatic, const std::string& ClassName, const char* SuperClassName, std::string& ErrorMsg) { 1311 if(mVerbose) 1312 std::cout << "Generating " << ClassName << ".java ..." << endl; 1313 1314 /* License */ 1315 out() << mLicenseNote; 1316 1317 /* Notice of generated file */ 1318 out() << "/*" << endl; 1319 out() << " * This file is auto-generated. DO NOT MODIFY!" << endl; 1320 out() << " * The source RenderScript file: " << mInputRSFile << endl; 1321 out() << " */" << endl; 1322 1323 /* Package */ 1324 if(!mPackageName.empty()) 1325 out() << "package " << mPackageName << ";" << endl; 1326 out() << endl; 1327 1328 /* Imports */ 1329 for(int i=0;i<(sizeof(Import)/sizeof(const char*));i++) 1330 out() << "import " << Import[i] << ";" << endl; 1331 out() << endl; 1332 1333 /* All reflected classes should be annotated as hidden, so that they won't be exposed in SDK. */ 1334 out() << "/**" << endl; 1335 out() << " * @hide" << endl; 1336 out() << " */" << endl; 1337 1338 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " << ClassName; 1339 if(SuperClassName != NULL) 1340 out() << " extends " << SuperClassName; 1341 1342 startBlock(); 1343 1344 mClassName = ClassName; 1345 1346 return true; 1347} 1348 1349void RSReflection::Context::endClass() { 1350 endBlock(); 1351 if(!mUseStdout) 1352 mOF.close(); 1353 clear(); 1354 return; 1355} 1356 1357void RSReflection::Context::startBlock(bool ShouldIndent) { 1358 if(ShouldIndent) 1359 indent() << "{" << endl; 1360 else 1361 out() << " {" << endl; 1362 incIndentLevel(); 1363 return; 1364} 1365 1366void RSReflection::Context::endBlock() { 1367 decIndentLevel(); 1368 indent() << "}" << endl << endl; 1369 return; 1370} 1371 1372void RSReflection::Context::startTypeClass(const std::string& ClassName) { 1373 indent() << "public static class " << ClassName; 1374 startBlock(); 1375 return; 1376} 1377 1378void RSReflection::Context::endTypeClass() { 1379 endBlock(); 1380 return; 1381} 1382 1383void RSReflection::Context::startFunction(AccessModifier AM, bool IsStatic, const char* ReturnType, const std::string& FunctionName, int Argc, ...) { 1384 ArgTy Args; 1385 va_list vl; 1386 va_start(vl, Argc); 1387 1388 for(int i=0;i<Argc;i++) { 1389 const char* ArgType = va_arg(vl, const char*); 1390 const char* ArgName = va_arg(vl, const char*); 1391 1392 Args.push_back( make_pair(ArgType, ArgName) ); 1393 } 1394 va_end(vl); 1395 1396 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 1397 1398 return; 1399} 1400 1401void RSReflection::Context::startFunction(AccessModifier AM, 1402 bool IsStatic, 1403 const char* ReturnType, 1404 const std::string& FunctionName, 1405 const ArgTy& Args) 1406{ 1407 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 1408 1409 bool FirstArg = true; 1410 for(ArgTy::const_iterator I = Args.begin(); 1411 I != Args.end(); 1412 I++) 1413 { 1414 if(!FirstArg) 1415 out() << ", "; 1416 else 1417 FirstArg = false; 1418 1419 out() << I->first << " " << I->second; 1420 } 1421 1422 out() << ")"; 1423 startBlock(); 1424 1425 return; 1426} 1427 1428void RSReflection::Context::endFunction() { 1429 endBlock(); 1430 return; 1431} 1432 1433} /* namespace slang */ 1434