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