slang_rs_reflection.cpp revision f1bd510334a62034dcf3b4787f2d6634d05ec80b
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 "Mesh", /* RSExportPrimitiveType::DataTypeRSMesh */ 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::DataTypeRSMesh */ 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::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 }; 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::DataTypeRSMesh */ 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::genInitBoolExportVariable(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 assert((Val.getKind() == APValue::Int) && "Bool type has wrong initial APValue"); 444 445 if (Val.getInt().getSExtValue() == 0) { 446 C.out() << "false"; 447 } else { 448 C.out() << "true"; 449 } 450 C.out() << ";" << endl; 451 452 return; 453} 454 455void RSReflection::genInitPrimitiveExportVariable(Context& C, const std::string& VarName, const APValue& Val) { 456 assert(!Val.isUninit() && "Not a valid initializer"); 457 458 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 459 switch(Val.getKind()) { 460 case APValue::Int: C.out() << Val.getInt().getSExtValue(); break; 461 case APValue::Float: C.out() << Val.getFloat().convertToDouble(); break; 462 463 case APValue::ComplexInt: 464 case APValue::ComplexFloat: 465 case APValue::LValue: 466 case APValue::Vector: 467 assert(false && "Primitive type cannot have such kind of initializer"); 468 break; 469 470 default: 471 assert(false && "Unknown kind of initializer"); 472 break; 473 } 474 C.out() << ";" << endl; 475 476 return; 477} 478 479void RSReflection::genInitExportVariable(Context& C, const RSExportType* ET, const std::string& VarName, const APValue& Val) { 480 assert(!Val.isUninit() && "Not a valid initializer"); 481 482 switch(ET->getClass()) { 483 case RSExportType::ExportClassPrimitive: { 484 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET); 485 if (EPT->getType() == RSExportPrimitiveType::DataTypeBool) { 486 genInitBoolExportVariable(C, VarName, Val); 487 } else { 488 genInitPrimitiveExportVariable(C, VarName, Val); 489 } 490 break; 491 } 492 493 case RSExportType::ExportClassPointer: 494 if(!Val.isInt() || Val.getInt().getSExtValue() != 0) 495 std::cout << "Initializer which is non-NULL to pointer type variable will be ignored" << endl; 496 break; 497 498 case RSExportType::ExportClassVector: 499 { 500 const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(ET); 501 switch(Val.getKind()) { 502 case APValue::Int: 503 case APValue::Float: 504 for(int i=0;i<EVT->getNumElement();i++) { 505 std::string Name = VarName + "." + GetVectorAccessor(i); 506 genInitPrimitiveExportVariable(C, Name, Val); 507 } 508 break; 509 510 case APValue::Vector: 511 { 512 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " << GetVectorTypeName(EVT) << "();" << endl; 513 514 unsigned NumElements = std::min(static_cast<unsigned>(EVT->getNumElement()), Val.getVectorLength()); 515 for(unsigned i=0;i<NumElements;i++) { 516 const APValue& ElementVal = Val.getVectorElt(i); 517 std::string Name = VarName + "." + GetVectorAccessor(i); 518 genInitPrimitiveExportVariable(C, Name, ElementVal); 519 } 520 } 521 break; 522 } 523 } 524 break; 525 526 /* TODO: Resolving initializer of a record type variable is complex. It cannot obtain by just simply evaluating the initializer expression. */ 527 case RSExportType::ExportClassRecord: 528 { 529 /* 530 unsigned InitIndex = 0; 531 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 532 533 assert((Val.getKind() == APValue::Vector) && "Unexpected type of initializer for record type variable"); 534 535 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() << "."RS_TYPE_ITEM_CLASS_NAME"();" << endl; 536 537 for(RSExportRecordType::const_field_iterator I = ERT->fields_begin(); 538 I != ERT->fields_end(); 539 I++) 540 { 541 const RSExportRecordType::Field* F = *I; 542 std::string FieldName = VarName + "." + F->getName(); 543 544 if(InitIndex > Val.getVectorLength()) 545 break; 546 547 genInitPrimitiveExportVariable(C, FieldName, Val.getVectorElt(InitIndex++)); 548 } 549 */ 550 assert(false && "Unsupported initializer for record type variable currently"); 551 } 552 break; 553 554 default: 555 assert(false && "Unknown class of type"); 556 break; 557 } 558 return; 559} 560 561void RSReflection::genExportVariable(Context& C, const RSExportVar* EV) { 562 const RSExportType* ET = EV->getType(); 563 564 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << " = " << C.getNextExportVarSlot() << ";" << endl; 565 566 switch(ET->getClass()) { 567 case RSExportType::ExportClassPrimitive: 568 genPrimitiveTypeExportVariable(C, EV); 569 break; 570 571 case RSExportType::ExportClassPointer: 572 genPointerTypeExportVariable(C, EV); 573 break; 574 575 case RSExportType::ExportClassVector: 576 genVectorTypeExportVariable(C, EV); 577 break; 578 579 case RSExportType::ExportClassRecord: 580 genRecordTypeExportVariable(C, EV); 581 break; 582 583 default: 584 assert(false && "Unknown class of type"); 585 break; 586 } 587 588 return; 589} 590 591void RSReflection::genExportFunction(Context& C, const RSExportFunc* EF) { 592 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" << endl; 593 594 /* invoke_*() */ 595 Context::ArgTy Args; 596 597 for(RSExportFunc::const_param_iterator I = EF->params_begin(); 598 I != EF->params_end(); 599 I++) 600 { 601 const RSExportFunc::Parameter* P = *I; 602 Args.push_back( make_pair(GetTypeName(P->getType()), P->getName()) ); 603 } 604 605 C.startFunction(Context::AM_Public, false, "void", "invoke_" + EF->getName(), Args); 606 607 if(!EF->hasParam()) { 608 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" << endl; 609 } else { 610 const RSExportRecordType* ERT = EF->getParamPacketType(); 611 std::string FieldPackerName = EF->getName() + "_fp"; 612 613 if(genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 614 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 615 616 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " << FieldPackerName << ");" << endl; 617 } 618 619 C.endFunction(); 620 return; 621} 622 623void RSReflection::genPrimitiveTypeExportVariable(Context& C, const RSExportVar* EV) { 624 assert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive) && "Variable should be type of primitive here"); 625 626 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(EV->getType()); 627 const char* TypeName = GetPrimitiveTypeName(EPT); 628 629 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 630 631 /* set_*() */ 632 if(!EV->isConst()) { 633 C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName, "v"); 634 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 635 636 if(EPT->isRSObjectType()) 637 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", (v == null) ? 0 : v.getID());" << endl; 638 else 639 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", v);" << endl; 640 641 C.endFunction(); 642 } 643 644 genGetExportVariable(C, TypeName, EV->getName()); 645 646 return; 647} 648 649void RSReflection::genPointerTypeExportVariable(Context& C, const RSExportVar* EV) { 650 const RSExportType* ET = EV->getType(); 651 const RSExportType* PointeeType; 652 std::string TypeName; 653 654 assert((ET->getClass() == RSExportType::ExportClassPointer) && "Variable should be type of pointer here"); 655 656 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 657 TypeName = GetTypeName(ET); 658 659 /* bind_*() */ 660 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 661 662 C.startFunction(Context::AM_Public, false, "void", "bind_" + EV->getName(), 1, TypeName.c_str(), "v"); 663 664 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 665 C.indent() << "if(v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl; 666 667 if(PointeeType->getClass() == RSExportType::ExportClassRecord) 668 C.indent() << "else bindAllocation(v.getAllocation(), "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl; 669 else 670 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" << endl; 671 672 C.endFunction(); 673 674 genGetExportVariable(C, TypeName, EV->getName()); 675 676 return; 677} 678 679void RSReflection::genVectorTypeExportVariable(Context& C, const RSExportVar* EV) { 680 assert((EV->getType()->getClass() == RSExportType::ExportClassVector) && "Variable should be type of vector here"); 681 682 const RSExportVectorType* EVT = static_cast<const RSExportVectorType*>(EV->getType()); 683 const char* TypeName = GetVectorTypeName(EVT); 684 const char* FieldPackerName = "fp"; 685 686 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 687 688 /* set_*() */ 689 if(!EV->isConst()) { 690 C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName, "v"); 691 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 692 693 if(genCreateFieldPacker(C, EVT, FieldPackerName)) 694 genPackVarOfType(C, EVT, "v", FieldPackerName); 695 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " << FieldPackerName << ");" << endl; 696 697 C.endFunction(); 698 } 699 700 genGetExportVariable(C, TypeName, EV->getName()); 701 return; 702} 703 704void RSReflection::genRecordTypeExportVariable(Context& C, const RSExportVar* EV) { 705 assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && "Variable should be type of struct here"); 706 707 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(EV->getType()); 708 std::string TypeName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 709 const char* FieldPackerName = "fp"; 710 711 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX << EV->getName() << ";" << endl; 712 713 /* set_*() */ 714 if(!EV->isConst()) { 715 C.startFunction(Context::AM_Public, false, "void", "set_" + EV->getName(), 1, TypeName.c_str(), "v"); 716 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << endl; 717 718 if(genCreateFieldPacker(C, ERT, FieldPackerName)) 719 genPackVarOfType(C, ERT, "v", FieldPackerName); 720 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " << FieldPackerName << ");" << endl; 721 722 C.endFunction(); 723 } 724 725 genGetExportVariable(C, TypeName.c_str(), EV->getName()); 726 return; 727} 728 729void RSReflection::genGetExportVariable(Context& C, const std::string& TypeName, const std::string& VarName) { 730 C.startFunction(Context::AM_Public, false, TypeName.c_str(), "get_" + VarName, 0); 731 732 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << endl; 733 734 C.endFunction(); 735 return; 736} 737 738/****************************** Methods to generate script class /end ******************************/ 739 740bool RSReflection::genCreateFieldPacker(Context& C, const RSExportType* ET, const char* FieldPackerName) { 741 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 742 if(AllocSize > 0) 743 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" << AllocSize << ");" << endl; 744 else 745 return false; 746 return true; 747} 748 749void RSReflection::genPackVarOfType(Context& C, const RSExportType* ET, const char* VarName, const char* FieldPackerName) { 750 switch(ET->getClass()) { 751 case RSExportType::ExportClassPrimitive: 752 case RSExportType::ExportClassVector: 753 C.indent() << FieldPackerName << "." << GetPackerAPIName(static_cast<const RSExportPrimitiveType*>(ET)) << "(" << VarName << ");" << endl; 754 break; 755 756 case RSExportType::ExportClassPointer: 757 { 758 /* Must reflect as type Allocation in Java */ 759 const RSExportType* PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 760 761 if(PointeeType->getClass() != RSExportType::ExportClassRecord) 762 C.indent() << FieldPackerName << ".addI32(" << VarName << ".getPtr());" << endl; 763 else 764 C.indent() << FieldPackerName << ".addI32(" << VarName << ".getAllocation().getPtr());" << endl; 765 } 766 break; 767 768 case RSExportType::ExportClassRecord: 769 { 770 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 771 int Pos = 0; /* relative pos from now on in field packer */ 772 773 for(RSExportRecordType::const_field_iterator I = ERT->fields_begin(); 774 I != ERT->fields_end(); 775 I++) 776 { 777 const RSExportRecordType::Field* F = *I; 778 std::string FieldName; 779 size_t FieldOffset = F->getOffsetInParent(); 780 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 781 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 782 783 if(VarName != NULL) 784 FieldName = VarName + ("." + F->getName()); 785 else 786 FieldName = F->getName(); 787 788 if(FieldOffset > Pos) 789 C.indent() << FieldPackerName << ".skip(" << (FieldOffset - Pos) << ");" << endl; 790 791 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 792 793 if(FieldAllocSize > FieldStoreSize) /* there's padding in the field type */ 794 C.indent() << FieldPackerName << ".skip(" << (FieldAllocSize - FieldStoreSize) << ");" << endl; 795 796 Pos = FieldOffset + FieldAllocSize; 797 } 798 799 /* There maybe some padding after the struct */ 800 size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos; 801 if(Padding > 0) 802 C.indent() << FieldPackerName << ".skip(" << Padding << ");" << endl; 803 } 804 break; 805 806 default: 807 assert(false && "Unknown class of type"); 808 break; 809 } 810 811 return; 812} 813 814/****************************** Methods to generate type class ******************************/ 815bool RSReflection::genTypeClass(Context& C, const RSExportRecordType* ERT, std::string& ErrorMsg) { 816 std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName(); 817 818 if(!C.startClass(Context::AM_Public, false, ClassName, RS_TYPE_CLASS_SUPER_CLASS_NAME, ErrorMsg)) 819 return false; 820 821 if(!genTypeItemClass(C, ERT, ErrorMsg)) 822 return false; 823 824 /* Declare item buffer and item buffer packer */ 825 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[];" << endl; 826 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" << endl; 827 828 genTypeClassConstructor(C, ERT); 829 genTypeClassCopyToArray(C, ERT); 830 genTypeClasSet(C, ERT); 831 genTypeClasCopyAll(C, ERT); 832 833 C.endClass(); 834 835 return true; 836} 837 838bool RSReflection::genTypeItemClass(Context& C, const RSExportRecordType* ERT, std::string& ErrorMsg) { 839 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 840 C.startBlock(); 841 842 C.indent() << "public static final int sizeof = " << RSExportType::GetTypeAllocSize(ERT) << ";" << endl; 843 844 /* Member elements */ 845 C.out() << endl; 846 for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin(); 847 FI != ERT->fields_end(); 848 FI++) 849 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() << ";" << endl; 850 851 /* Constructor */ 852 C.out() << endl; 853 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 854 C.startBlock(); 855 856 for(RSExportRecordType::const_field_iterator FI = ERT->fields_begin(); 857 FI != ERT->fields_end(); 858 FI++) 859 { 860 const RSExportRecordType::Field* F = *FI; 861 if((F->getType()->getClass() == RSExportType::ExportClassVector) || (F->getType()->getClass() == RSExportType::ExportClassRecord)) 862 C.indent() << F->getName() << " = new " << GetTypeName(F->getType()) << "();" << endl; 863 } 864 865 C.endBlock(); /* end Constructor */ 866 867 C.endBlock(); /* end Item class */ 868 869 870 return true; 871} 872 873void RSReflection::genTypeClassConstructor(Context& C, const RSExportRecordType* ERT) { 874 const char* RenderScriptVar = "rs"; 875 876 C.startFunction(Context::AM_Public, true, "Element", "createElement", 1, "RenderScript", RenderScriptVar); 877 genBuildElement(C, ERT, RenderScriptVar); 878 C.endFunction(); 879 880 C.startFunction(Context::AM_Public, false, NULL, C.getClassName(), 2, "RenderScript", RenderScriptVar, 881 "int", "count"); 882 883 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << endl; 884 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << endl; 885 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" << endl; 886 /* Call init() in super class */ 887 C.indent() << "init(" << RenderScriptVar << ", count);" << endl; 888 C.endFunction(); 889 890 return; 891} 892 893void RSReflection::genTypeClassCopyToArray(Context& C, const RSExportRecordType* ERT) { 894 C.startFunction(Context::AM_Private, false, "void", "copyToArray", 2, RS_TYPE_ITEM_CLASS_NAME, "i", 895 "int", "index"); 896 897 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; 898 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" << endl; 899 900 genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 901 902 C.endFunction(); 903 return; 904} 905 906void RSReflection::genTypeClasSet(Context& C, const RSExportRecordType* ERT) { 907 C.startFunction(Context::AM_Public, false, "void", "set", 3, RS_TYPE_ITEM_CLASS_NAME, "i", 908 "int", "index", 909 "boolean", "copyNow"); 910 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) "RS_TYPE_ITEM_BUFFER_NAME" = new "RS_TYPE_ITEM_CLASS_NAME"[mType.getX() /* count */];" << endl; 911 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << endl; 912 913 C.indent() << "if (copyNow) "; 914 C.startBlock(); 915 916 C.indent() << "copyToArray(i, index);" << endl; 917 //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; 918 C.indent() << "mAllocation.subData1D(index, 1, "RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << endl; 919 920 C.endBlock(); /* end if (copyNow) */ 921 922 C.endFunction(); 923 return; 924} 925 926void RSReflection::genTypeClasCopyAll(Context& C, const RSExportRecordType* ERT) { 927 C.startFunction(Context::AM_Public, false, "void", "copyAll", 0); 928 929 C.indent() << "for (int ct=0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++) copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" << endl; 930 C.indent() << "mAllocation.data("RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << endl; 931 932 C.endFunction(); 933 return; 934} 935 936/****************************** Methods to generate type class /end ******************************/ 937 938/******************** Methods to create Element in Java of given record type ********************/ 939void RSReflection::genBuildElement(Context& C, const RSExportRecordType* ERT, const char* RenderScriptVar) { 940 const char* ElementBuilderName = "eb"; 941 942 /* Create element builder */ 943 // C.startBlock(true); 944 945 C.indent() << "Element.Builder " << ElementBuilderName << " = new Element.Builder(" << RenderScriptVar << ");" << endl; 946 947 /* eb.add(...) */ 948 genAddElementToElementBuilder(C, ERT, "", ElementBuilderName, RenderScriptVar); 949 950 C.indent() << "return " << ElementBuilderName << ".create();" << endl; 951 952 // C.endBlock(); 953 return; 954} 955 956#define EB_ADD(x, ...) \ 957 C.indent() << ElementBuilderName << ".add(Element." << x ##__VA_ARGS__ ", \"" << VarName << "\");" << endl 958 959void RSReflection::genAddElementToElementBuilder(Context& C, const RSExportType* ET, const std::string& VarName, const char* ElementBuilderName, const char* RenderScriptVar) { 960 const char* ElementConstruct = GetBuiltinElementConstruct(ET); 961 962 if(ElementConstruct != NULL) { 963 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")"); 964 } else { 965 if((ET->getClass() == RSExportType::ExportClassPrimitive) || (ET->getClass() == RSExportType::ExportClassVector)) { 966 const RSExportPrimitiveType* EPT = static_cast<const RSExportPrimitiveType*>(ET); 967 const char* DataKindName = GetElementDataKindName(EPT->getKind()); 968 const char* DataTypeName = GetElementDataTypeName(EPT->getType()); 969 int Size = (ET->getClass() == RSExportType::ExportClassVector) ? static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1; 970 971 switch(EPT->getKind()) { 972 case RSExportPrimitiveType::DataKindColor: 973 case RSExportPrimitiveType::DataKindPosition: 974 case RSExportPrimitiveType::DataKindTexture: 975 case RSExportPrimitiveType::DataKindNormal: 976 case RSExportPrimitiveType::DataKindPointSize: 977 /* Element.createAttrib() */ 978 EB_ADD("createAttrib(" << RenderScriptVar << ", " << DataTypeName << ", " << DataKindName << ", " << Size << ")"); 979 break; 980 981 case RSExportPrimitiveType::DataKindIndex: 982 /* Element.createIndex() */ 983 EB_ADD("createAttrib(" << RenderScriptVar << ")"); 984 break; 985 986 case RSExportPrimitiveType::DataKindPixelL: 987 case RSExportPrimitiveType::DataKindPixelA: 988 case RSExportPrimitiveType::DataKindPixelLA: 989 case RSExportPrimitiveType::DataKindPixelRGB: 990 case RSExportPrimitiveType::DataKindPixelRGBA: 991 /* Element.createPixel() */ 992 EB_ADD("createVector(" << RenderScriptVar << ", " << DataTypeName << ", " << DataKindName << ")"); 993 break; 994 995 case RSExportPrimitiveType::DataKindUser: 996 default: 997 if(EPT->getClass() == RSExportType::ExportClassPrimitive) 998 /* Element.createUser() */ 999 EB_ADD("createUser(" << RenderScriptVar << ", " << DataTypeName << ")"); 1000 else /* (ET->getClass() == RSExportType::ExportClassVector) must hold here */ 1001 /* Element.createVector() */ 1002 EB_ADD("createVector(" << RenderScriptVar << ", " << DataTypeName << ", " << Size << ")"); 1003 break; 1004 } 1005 } else if(ET->getClass() == RSExportType::ExportClassPointer) { 1006 /* Pointer type variable should be resolved in GetBuiltinElementConstruct() */ 1007 assert(false && "??"); 1008 } else if(ET->getClass() == RSExportType::ExportClassRecord) { 1009 /* 1010 * Simalar to genPackVarOfType. 1011 * 1012 * TODO: Generalize these two function such that there's no duplicated codes. 1013 */ 1014 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 1015 int Pos = 0; /* relative pos from now on */ 1016 1017 for(RSExportRecordType::const_field_iterator I = ERT->fields_begin(); 1018 I != ERT->fields_end(); 1019 I++) 1020 { 1021 const RSExportRecordType::Field* F = *I; 1022 std::string FieldName; 1023 size_t FieldOffset = F->getOffsetInParent(); 1024 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1025 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1026 1027 if(!VarName.empty()) 1028 FieldName = VarName + "." + F->getName(); 1029 else 1030 FieldName = F->getName(); 1031 1032 /* alignment */ 1033 genAddPaddingToElementBuiler(C, (FieldOffset - Pos), ElementBuilderName, RenderScriptVar); 1034 1035 /* eb.add(...) */ 1036 genAddElementToElementBuilder(C, (*I)->getType(), FieldName, ElementBuilderName, RenderScriptVar); 1037 1038 /* there's padding within the field type */ 1039 genAddPaddingToElementBuiler(C, (FieldAllocSize - FieldStoreSize), ElementBuilderName, RenderScriptVar); 1040 1041 Pos = FieldOffset + FieldAllocSize; 1042 } 1043 1044 /* There maybe some padding after the struct */ 1045 genAddPaddingToElementBuiler(C, (RSExportType::GetTypeAllocSize(ERT) - Pos), ElementBuilderName, RenderScriptVar); 1046 } else { 1047 assert(false && "Unknown class of type"); 1048 } 1049 } 1050} 1051 1052void RSReflection::genAddPaddingToElementBuiler(Context& C, size_t PaddingSize, const char* ElementBuilderName, const char* RenderScriptVar) { 1053 while(PaddingSize > 0) { 1054 const std::string& VarName = C.createPaddingField(); 1055 if(PaddingSize >= 4) { 1056 EB_ADD("U32(" << RenderScriptVar << ")"); 1057 PaddingSize -= 4; 1058 } else if(PaddingSize >= 2) { 1059 EB_ADD("U16(" << RenderScriptVar << ")"); 1060 PaddingSize -= 2; 1061 } else if(PaddingSize >= 1) { 1062 EB_ADD("U8(" << RenderScriptVar << ")"); 1063 PaddingSize -= 1; 1064 } 1065 } 1066 return; 1067} 1068 1069#undef EB_ADD 1070/******************** Methods to create Element in Java of given record type /end ********************/ 1071 1072bool RSReflection::reflect(const char* OutputPackageName, const std::string& InputFileName, const std::string& OutputBCFileName) { 1073 Context *C = NULL; 1074 std::string ResourceId = ""; 1075 1076 if(!GetFileNameWithoutExtension(OutputBCFileName, ResourceId)) 1077 return false; 1078 1079 if(ResourceId.empty()) 1080 ResourceId = "<Resource ID>"; 1081 1082 if((OutputPackageName == NULL) || (*OutputPackageName == '\0') || strcmp(OutputPackageName, "-") == 0) 1083 C = new Context("<Package Name>", ResourceId, true); 1084 else 1085 C = new Context(OutputPackageName, ResourceId, false); 1086 1087 if(C != NULL) { 1088 std::string ErrorMsg, ScriptClassName; 1089 /* class ScriptC_<ScriptName> */ 1090 if(!GetFileNameWithoutExtension(InputFileName, ScriptClassName)) 1091 return false; 1092 1093 if(ScriptClassName.empty()) 1094 ScriptClassName = "<Input Script Name>"; 1095 1096 ScriptClassName.at(0) = toupper(ScriptClassName.at(0)); 1097 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX); 1098 1099 if (mRSContext->getLicenseNote() != NULL) 1100 C->setLicenseNote(*mRSContext->getLicenseNote()); 1101 1102 if(!genScriptClass(*C, ScriptClassName, ErrorMsg)) { 1103 std::cerr << "Failed to generate class " << ScriptClassName << " (" << ErrorMsg << ")" << endl; 1104 return false; 1105 } 1106 1107 /* class ScriptField_<TypeName> */ 1108 for(RSContext::const_export_type_iterator TI = mRSContext->export_types_begin(); 1109 TI != mRSContext->export_types_end(); 1110 TI++) 1111 { 1112 const RSExportType* ET = TI->getValue(); 1113 1114 if(ET->getClass() == RSExportType::ExportClassRecord) { 1115 const RSExportRecordType* ERT = static_cast<const RSExportRecordType*>(ET); 1116 1117 if(!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1118 std::cerr << "Failed to generate type class for struct '" << ERT->getName() << "' (" << ErrorMsg << ")" << endl; 1119 return false; 1120 } 1121 } 1122 } 1123 } 1124 1125 return true; 1126} 1127 1128/****************************** RSReflection::Context ******************************/ 1129const char* const RSReflection::Context::ApacheLicenseNote = 1130 "/*\n" 1131 " * Copyright (C) 2010 The Android Open Source Project\n" 1132 " *\n" 1133 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 1134 " * you may not use this file except in compliance with the License.\n" 1135 " * You may obtain a copy of the License at\n" 1136 " *\n" 1137 " * http://www.apache.org/licenses/LICENSE-2.0\n" 1138 " *\n" 1139 " * Unless required by applicable law or agreed to in writing, software\n" 1140 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 1141 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" 1142 " * See the License for the specific language governing permissions and\n" 1143 " * limitations under the License.\n" 1144 " */\n" 1145 "\n"; 1146 1147const char* const RSReflection::Context::Import[] = { 1148 /* RenderScript java class */ 1149 "android.renderscript.*", 1150 /* Import R */ 1151 "android.content.res.Resources", 1152 /* Import for debugging */ 1153 "android.util.Log", 1154}; 1155 1156const char* RSReflection::Context::AccessModifierStr(AccessModifier AM) { 1157 switch(AM) { 1158 case AM_Public: return "public"; break; 1159 case AM_Protected: return "protected"; break; 1160 case AM_Private: return "private"; break; 1161 default: return ""; break; 1162 } 1163} 1164 1165bool RSReflection::Context::startClass(AccessModifier AM, bool IsStatic, const std::string& ClassName, const char* SuperClassName, std::string& ErrorMsg) { 1166 if(mVerbose) 1167 std::cout << "Generating " << ClassName << ".java ..." << endl; 1168 1169 /* Open the file */ 1170 if(!mUseStdout) { 1171 mOF.clear(); 1172 mOF.open((ClassName + ".java").c_str()); 1173 if(!mOF.good()) { 1174 ErrorMsg = "failed to open file '" + ClassName + ".java' for write"; 1175 return false; 1176 } 1177 } 1178 1179 /* License */ 1180 out() << mLicenseNote; 1181 1182 /* Package */ 1183 if(!mPackageName.empty()) 1184 out() << "package " << mPackageName << ";" << endl; 1185 out() << endl; 1186 1187 /* Imports */ 1188 for(int i=0;i<(sizeof(Import)/sizeof(const char*));i++) 1189 out() << "import " << Import[i] << ";" << endl; 1190 out() << endl; 1191 1192 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " << ClassName; 1193 if(SuperClassName != NULL) 1194 out() << " extends " << SuperClassName; 1195 1196 startBlock(); 1197 1198 mClassName = ClassName; 1199 1200 return true; 1201} 1202 1203void RSReflection::Context::endClass() { 1204 endBlock(); 1205 if(!mUseStdout) 1206 mOF.close(); 1207 clear(); 1208 return; 1209} 1210 1211void RSReflection::Context::startBlock(bool ShouldIndent) { 1212 if(ShouldIndent) 1213 indent() << "{" << endl; 1214 else 1215 out() << " {" << endl; 1216 incIndentLevel(); 1217 return; 1218} 1219 1220void RSReflection::Context::endBlock() { 1221 decIndentLevel(); 1222 indent() << "}" << endl << endl; 1223 return; 1224} 1225 1226void RSReflection::Context::startTypeClass(const std::string& ClassName) { 1227 indent() << "public static class " << ClassName; 1228 startBlock(); 1229 return; 1230} 1231 1232void RSReflection::Context::endTypeClass() { 1233 endBlock(); 1234 return; 1235} 1236 1237void RSReflection::Context::startFunction(AccessModifier AM, bool IsStatic, const char* ReturnType, const std::string& FunctionName, int Argc, ...) { 1238 ArgTy Args; 1239 va_list vl; 1240 va_start(vl, Argc); 1241 1242 for(int i=0;i<Argc;i++) { 1243 const char* ArgType = va_arg(vl, const char*); 1244 const char* ArgName = va_arg(vl, const char*); 1245 1246 Args.push_back( make_pair(ArgType, ArgName) ); 1247 } 1248 va_end(vl); 1249 1250 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 1251 1252 return; 1253} 1254 1255void RSReflection::Context::startFunction(AccessModifier AM, 1256 bool IsStatic, 1257 const char* ReturnType, 1258 const std::string& FunctionName, 1259 const ArgTy& Args) 1260{ 1261 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 1262 1263 bool FirstArg = true; 1264 for(ArgTy::const_iterator I = Args.begin(); 1265 I != Args.end(); 1266 I++) 1267 { 1268 if(!FirstArg) 1269 out() << ", "; 1270 else 1271 FirstArg = false; 1272 1273 out() << I->first << " " << I->second; 1274 } 1275 1276 out() << ")"; 1277 startBlock(); 1278 1279 return; 1280} 1281 1282void RSReflection::Context::endFunction() { 1283 endBlock(); 1284 return; 1285} 1286 1287} /* namespace slang */ 1288