slang_rs_reflection.cpp revision d2936939ec10879e25746322db60071f79f28c1b
1/* 2 * Copyright 2010-2012, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "slang_rs_reflection.h" 18 19#include <sys/stat.h> 20 21#include <cstdarg> 22#include <cctype> 23 24#include <algorithm> 25#include <sstream> 26#include <string> 27#include <utility> 28 29#include "llvm/ADT/APFloat.h" 30#include "llvm/ADT/StringExtras.h" 31 32#include "os_sep.h" 33#include "slang_rs_context.h" 34#include "slang_rs_export_var.h" 35#include "slang_rs_export_foreach.h" 36#include "slang_rs_export_func.h" 37#include "slang_rs_reflect_utils.h" 38#include "slang_version.h" 39#include "slang_utils.h" 40 41#include "slang_rs_reflection_base.h" 42 43#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_" 44#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC" 45 46#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.Script.FieldBase" 47 48#define RS_TYPE_ITEM_CLASS_NAME "Item" 49 50#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray" 51#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer" 52#define RS_TYPE_ELEMENT_REF_NAME "mElementCache" 53 54#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_" 55#define RS_EXPORT_VAR_PREFIX "mExportVar_" 56#define RS_EXPORT_VAR_ELEM_PREFIX "mExportVarElem_" 57#define RS_EXPORT_VAR_DIM_PREFIX "mExportVarDim_" 58#define RS_EXPORT_VAR_CONST_PREFIX "const_" 59 60#define RS_ELEM_PREFIX "__" 61 62#define RS_FP_PREFIX "__rs_fp_" 63 64#define RS_RESOURCE_NAME "__rs_resource_name" 65 66#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_" 67#define RS_EXPORT_FOREACH_INDEX_PREFIX "mExportForEachIdx_" 68 69#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_" 70#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_" 71 72namespace slang { 73 74// Some utility function using internal in RSReflection 75static bool GetClassNameFromFileName(const std::string &FileName, 76 std::string &ClassName) { 77 ClassName.clear(); 78 79 if (FileName.empty() || (FileName == "-")) 80 return true; 81 82 ClassName = 83 RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str()); 84 85 return true; 86} 87 88static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) { 89 static const char *MatrixTypeJavaNameMap[] = { 90 /* 2x2 */ "Matrix2f", 91 /* 3x3 */ "Matrix3f", 92 /* 4x4 */ "Matrix4f", 93 }; 94 unsigned Dim = EMT->getDim(); 95 96 if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char*))) 97 return MatrixTypeJavaNameMap[ EMT->getDim() - 2 ]; 98 99 slangAssert(false && "GetMatrixTypeName : Unsupported matrix dimension"); 100 return NULL; 101} 102 103static const char *GetVectorAccessor(unsigned Index) { 104 static const char *VectorAccessorMap[] = { 105 /* 0 */ "x", 106 /* 1 */ "y", 107 /* 2 */ "z", 108 /* 3 */ "w", 109 }; 110 111 slangAssert((Index < (sizeof(VectorAccessorMap) / sizeof(const char*))) && 112 "Out-of-bound index to access vector member"); 113 114 return VectorAccessorMap[Index]; 115} 116 117static const char *GetPackerAPIName(const RSExportPrimitiveType *EPT) { 118 static const char *PrimitiveTypePackerAPINameMap[] = { 119 "", // RSExportPrimitiveType::DataTypeFloat16 120 "addF32", // RSExportPrimitiveType::DataTypeFloat32 121 "addF64", // RSExportPrimitiveType::DataTypeFloat64 122 "addI8", // RSExportPrimitiveType::DataTypeSigned8 123 "addI16", // RSExportPrimitiveType::DataTypeSigned16 124 "addI32", // RSExportPrimitiveType::DataTypeSigned32 125 "addI64", // RSExportPrimitiveType::DataTypeSigned64 126 "addU8", // RSExportPrimitiveType::DataTypeUnsigned8 127 "addU16", // RSExportPrimitiveType::DataTypeUnsigned16 128 "addU32", // RSExportPrimitiveType::DataTypeUnsigned32 129 "addU64", // RSExportPrimitiveType::DataTypeUnsigned64 130 "addBoolean", // RSExportPrimitiveType::DataTypeBoolean 131 132 "addU16", // RSExportPrimitiveType::DataTypeUnsigned565 133 "addU16", // RSExportPrimitiveType::DataTypeUnsigned5551 134 "addU16", // RSExportPrimitiveType::DataTypeUnsigned4444 135 136 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix2x2 137 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix3x3 138 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix4x4 139 140 "addObj", // RSExportPrimitiveType::DataTypeRSElement 141 "addObj", // RSExportPrimitiveType::DataTypeRSType 142 "addObj", // RSExportPrimitiveType::DataTypeRSAllocation 143 "addObj", // RSExportPrimitiveType::DataTypeRSSampler 144 "addObj", // RSExportPrimitiveType::DataTypeRSScript 145 "addObj", // RSExportPrimitiveType::DataTypeRSMesh 146 "addObj", // RSExportPrimitiveType::DataTypeRSPath 147 "addObj", // RSExportPrimitiveType::DataTypeRSProgramFragment 148 "addObj", // RSExportPrimitiveType::DataTypeRSProgramVertex 149 "addObj", // RSExportPrimitiveType::DataTypeRSProgramRaster 150 "addObj", // RSExportPrimitiveType::DataTypeRSProgramStore 151 "addObj", // RSExportPrimitiveType::DataTypeRSFont 152 }; 153 unsigned TypeId = EPT->getType(); 154 155 if (TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*))) 156 return PrimitiveTypePackerAPINameMap[ EPT->getType() ]; 157 158 slangAssert(false && "GetPackerAPIName : Unknown primitive data type"); 159 return NULL; 160} 161 162static std::string GetTypeName(const RSExportType *ET, bool Brackets = true) { 163 switch (ET->getClass()) { 164 case RSExportType::ExportClassPrimitive: { 165 return RSExportPrimitiveType::getRSReflectionType( 166 static_cast<const RSExportPrimitiveType*>(ET))->java_name; 167 } 168 case RSExportType::ExportClassPointer: { 169 const RSExportType *PointeeType = 170 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 171 172 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 173 return "Allocation"; 174 else 175 return PointeeType->getElementName(); 176 } 177 case RSExportType::ExportClassVector: { 178 const RSExportVectorType *EVT = 179 static_cast<const RSExportVectorType*>(ET); 180 std::stringstream VecName; 181 VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix 182 << EVT->getNumElement(); 183 return VecName.str(); 184 } 185 case RSExportType::ExportClassMatrix: { 186 return GetMatrixTypeName(static_cast<const RSExportMatrixType*>(ET)); 187 } 188 case RSExportType::ExportClassConstantArray: { 189 const RSExportConstantArrayType* CAT = 190 static_cast<const RSExportConstantArrayType*>(ET); 191 std::string ElementTypeName = GetTypeName(CAT->getElementType()); 192 if (Brackets) { 193 ElementTypeName.append("[]"); 194 } 195 return ElementTypeName; 196 } 197 case RSExportType::ExportClassRecord: { 198 return ET->getElementName() + "."RS_TYPE_ITEM_CLASS_NAME; 199 } 200 default: { 201 slangAssert(false && "Unknown class of type"); 202 } 203 } 204 205 return ""; 206} 207 208static const char *GetTypeNullValue(const RSExportType *ET) { 209 switch (ET->getClass()) { 210 case RSExportType::ExportClassPrimitive: { 211 const RSExportPrimitiveType *EPT = 212 static_cast<const RSExportPrimitiveType*>(ET); 213 if (EPT->isRSObjectType()) 214 return "null"; 215 else if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) 216 return "false"; 217 else 218 return "0"; 219 break; 220 } 221 case RSExportType::ExportClassPointer: 222 case RSExportType::ExportClassVector: 223 case RSExportType::ExportClassMatrix: 224 case RSExportType::ExportClassConstantArray: 225 case RSExportType::ExportClassRecord: { 226 return "null"; 227 break; 228 } 229 default: { 230 slangAssert(false && "Unknown class of type"); 231 } 232 } 233 return ""; 234} 235 236static std::string GetBuiltinElementConstruct(const RSExportType *ET) { 237 if (ET->getClass() == RSExportType::ExportClassPrimitive) { 238 return std::string("Element.") + ET->getElementName(); 239 } else if (ET->getClass() == RSExportType::ExportClassVector) { 240 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(ET); 241 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 242 if (EVT->getNumElement() == 2) 243 return "Element.F32_2"; 244 else if (EVT->getNumElement() == 3) 245 return "Element.F32_3"; 246 else if (EVT->getNumElement() == 4) 247 return "Element.F32_4"; 248 } else if (EVT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) { 249 if (EVT->getNumElement() == 4) 250 return "Element.U8_4"; 251 } 252 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 253 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType *>(ET); 254 switch (EMT->getDim()) { 255 case 2: return "Element.MATRIX_2X2"; 256 case 3: return "Element.MATRIX_3X3"; 257 case 4: return "Element.MATRIX_4X4"; 258 default: slangAssert(false && "Unsupported dimension of matrix"); 259 } 260 } 261 // RSExportType::ExportClassPointer can't be generated in a struct. 262 263 return ""; 264} 265 266 267/********************** Methods to generate script class **********************/ 268bool RSReflection::genScriptClass(Context &C, 269 const std::string &ClassName, 270 std::string &ErrorMsg) { 271 if (!C.startClass(Context::AM_Public, 272 false, 273 ClassName, 274 RS_SCRIPT_CLASS_SUPER_CLASS_NAME, 275 ErrorMsg)) 276 return false; 277 278 genScriptClassConstructor(C); 279 280 // Reflect export variable 281 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 282 E = mRSContext->export_vars_end(); 283 I != E; 284 I++) 285 genExportVariable(C, *I); 286 287 // Reflect export for each functions (only available on ICS+) 288 if (mRSContext->getTargetAPI() >= SLANG_ICS_TARGET_API) { 289 for (RSContext::const_export_foreach_iterator 290 I = mRSContext->export_foreach_begin(), 291 E = mRSContext->export_foreach_end(); 292 I != E; I++) 293 genExportForEach(C, *I); 294 } 295 296 // Reflect export function 297 for (RSContext::const_export_func_iterator 298 I = mRSContext->export_funcs_begin(), 299 E = mRSContext->export_funcs_end(); 300 I != E; I++) 301 genExportFunction(C, *I); 302 303 C.endClass(); 304 305 return true; 306} 307 308void RSReflection::genScriptClassConstructor(Context &C) { 309 // Provide a simple way to reference this object. 310 C.indent() << "private static final String " RS_RESOURCE_NAME " = \"" 311 << C.getResourceId() 312 << "\";" << std::endl; 313 314 // Generate a simple constructor with only a single parameter (the rest 315 // can be inferred from information we already have). 316 C.indent() << "// Constructor" << std::endl; 317 C.startFunction(Context::AM_Public, 318 false, 319 NULL, 320 C.getClassName(), 321 1, 322 "RenderScript", "rs"); 323 // Call alternate constructor with required parameters. 324 // Look up the proper raw bitcode resource id via the context. 325 C.indent() << "this(rs," << std::endl; 326 C.indent() << " rs.getApplicationContext().getResources()," << std::endl; 327 C.indent() << " rs.getApplicationContext().getResources()." 328 "getIdentifier(" << std::endl; 329 C.indent() << " " RS_RESOURCE_NAME ", \"raw\"," << std::endl; 330 C.indent() << " rs.getApplicationContext().getPackageName()));" 331 << std::endl; 332 C.endFunction(); 333 334 // Alternate constructor (legacy) with 3 original parameters. 335 C.startFunction(Context::AM_Public, 336 false, 337 NULL, 338 C.getClassName(), 339 3, 340 "RenderScript", "rs", 341 "Resources", "resources", 342 "int", "id"); 343 // Call constructor of super class 344 C.indent() << "super(rs, resources, id);" << std::endl; 345 346 // If an exported variable has initial value, reflect it 347 348 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 349 E = mRSContext->export_vars_end(); 350 I != E; 351 I++) { 352 const RSExportVar *EV = *I; 353 if (!EV->getInit().isUninit()) { 354 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit()); 355 } else if (EV->getArraySize()) { 356 // Always create an initial zero-init array object. 357 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = new " 358 << GetTypeName(EV->getType(), false) << "[" 359 << EV->getArraySize() << "];" << std::endl; 360 size_t NumInits = EV->getNumInits(); 361 const RSExportConstantArrayType *ECAT = 362 static_cast<const RSExportConstantArrayType*>(EV->getType()); 363 const RSExportType *ET = ECAT->getElementType(); 364 for (size_t i = 0; i < NumInits; i++) { 365 std::stringstream Name; 366 Name << EV->getName() << "[" << i << "]"; 367 genInitExportVariable(C, ET, Name.str(), EV->getInitArray(i)); 368 } 369 } 370 if (mRSContext->getTargetAPI() >= SLANG_JB_TARGET_API) { 371 genTypeInstance(C, EV->getType()); 372 } 373 genFieldPackerInstance(C, EV->getType()); 374 } 375 376 for (RSContext::const_export_foreach_iterator 377 I = mRSContext->export_foreach_begin(), 378 E = mRSContext->export_foreach_end(); 379 I != E; 380 I++) { 381 const RSExportForEach *EF = *I; 382 383 const RSExportType *IET = EF->getInType(); 384 if (IET) { 385 genTypeInstanceFromPointer(C, IET); 386 } 387 const RSExportType *OET = EF->getOutType(); 388 if (OET) { 389 genTypeInstanceFromPointer(C, OET); 390 } 391 } 392 393 C.endFunction(); 394 395 for (std::set<std::string>::iterator I = C.mTypesToCheck.begin(), 396 E = C.mTypesToCheck.end(); 397 I != E; 398 I++) { 399 C.indent() << "private Element " RS_ELEM_PREFIX << *I << ";" << std::endl; 400 } 401 402 for (std::set<std::string>::iterator I = C.mFieldPackerTypes.begin(), 403 E = C.mFieldPackerTypes.end(); 404 I != E; 405 I++) { 406 C.indent() << "private FieldPacker " RS_FP_PREFIX << *I << ";" << std::endl; 407 } 408 409 return; 410} 411 412void RSReflection::genInitBoolExportVariable(Context &C, 413 const std::string &VarName, 414 const clang::APValue &Val) { 415 slangAssert(!Val.isUninit() && "Not a valid initializer"); 416 417 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 418 slangAssert((Val.getKind() == clang::APValue::Int) && 419 "Bool type has wrong initial APValue"); 420 421 C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true") 422 << ";" << std::endl; 423 424 return; 425} 426 427void RSReflection::genInitPrimitiveExportVariable( 428 Context &C, 429 const std::string &VarName, 430 const clang::APValue &Val) { 431 slangAssert(!Val.isUninit() && "Not a valid initializer"); 432 433 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 434 C.out() << RSReflectionBase::genInitValue(Val); 435 C.out() << ";" << std::endl; 436 437 return; 438} 439 440void RSReflection::genInitExportVariable(Context &C, 441 const RSExportType *ET, 442 const std::string &VarName, 443 const clang::APValue &Val) { 444 slangAssert(!Val.isUninit() && "Not a valid initializer"); 445 446 switch (ET->getClass()) { 447 case RSExportType::ExportClassPrimitive: { 448 const RSExportPrimitiveType *EPT = 449 static_cast<const RSExportPrimitiveType*>(ET); 450 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) { 451 genInitBoolExportVariable(C, VarName, Val); 452 } else { 453 genInitPrimitiveExportVariable(C, VarName, Val); 454 } 455 break; 456 } 457 case RSExportType::ExportClassPointer: { 458 if (!Val.isInt() || Val.getInt().getSExtValue() != 0) 459 std::cout << "Initializer which is non-NULL to pointer type variable " 460 "will be ignored" << std::endl; 461 break; 462 } 463 case RSExportType::ExportClassVector: { 464 const RSExportVectorType *EVT = 465 static_cast<const RSExportVectorType*>(ET); 466 switch (Val.getKind()) { 467 case clang::APValue::Int: 468 case clang::APValue::Float: { 469 for (unsigned i = 0; i < EVT->getNumElement(); i++) { 470 std::string Name = VarName + "." + GetVectorAccessor(i); 471 genInitPrimitiveExportVariable(C, Name, Val); 472 } 473 break; 474 } 475 case clang::APValue::Vector: { 476 std::stringstream VecName; 477 VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix 478 << EVT->getNumElement(); 479 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " 480 << VecName.str() << "();" << std::endl; 481 482 unsigned NumElements = 483 std::min(static_cast<unsigned>(EVT->getNumElement()), 484 Val.getVectorLength()); 485 for (unsigned i = 0; i < NumElements; i++) { 486 const clang::APValue &ElementVal = Val.getVectorElt(i); 487 std::string Name = VarName + "." + GetVectorAccessor(i); 488 genInitPrimitiveExportVariable(C, Name, ElementVal); 489 } 490 break; 491 } 492 case clang::APValue::MemberPointer: 493 case clang::APValue::Uninitialized: 494 case clang::APValue::ComplexInt: 495 case clang::APValue::ComplexFloat: 496 case clang::APValue::LValue: 497 case clang::APValue::Array: 498 case clang::APValue::Struct: 499 case clang::APValue::Union: 500 case clang::APValue::AddrLabelDiff: { 501 slangAssert(false && "Unexpected type of value of initializer."); 502 } 503 } 504 break; 505 } 506 // TODO(zonr): Resolving initializer of a record (and matrix) type variable 507 // is complex. It cannot obtain by just simply evaluating the initializer 508 // expression. 509 case RSExportType::ExportClassMatrix: 510 case RSExportType::ExportClassConstantArray: 511 case RSExportType::ExportClassRecord: { 512#if 0 513 unsigned InitIndex = 0; 514 const RSExportRecordType *ERT = 515 static_cast<const RSExportRecordType*>(ET); 516 517 slangAssert((Val.getKind() == clang::APValue::Vector) && 518 "Unexpected type of initializer for record type variable"); 519 520 C.indent() << RS_EXPORT_VAR_PREFIX << VarName 521 << " = new " << ERT->getElementName() 522 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 523 524 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 525 E = ERT->fields_end(); 526 I != E; 527 I++) { 528 const RSExportRecordType::Field *F = *I; 529 std::string FieldName = VarName + "." + F->getName(); 530 531 if (InitIndex > Val.getVectorLength()) 532 break; 533 534 genInitPrimitiveExportVariable(C, 535 FieldName, 536 Val.getVectorElt(InitIndex++)); 537 } 538#endif 539 slangAssert(false && "Unsupported initializer for record/matrix/constant " 540 "array type variable currently"); 541 break; 542 } 543 default: { 544 slangAssert(false && "Unknown class of type"); 545 } 546 } 547 return; 548} 549 550void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) { 551 const RSExportType *ET = EV->getType(); 552 553 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX 554 << EV->getName() << " = " << C.getNextExportVarSlot() << ";" 555 << std::endl; 556 557 switch (ET->getClass()) { 558 case RSExportType::ExportClassPrimitive: { 559 genPrimitiveTypeExportVariable(C, EV); 560 break; 561 } 562 case RSExportType::ExportClassPointer: { 563 genPointerTypeExportVariable(C, EV); 564 break; 565 } 566 case RSExportType::ExportClassVector: { 567 genVectorTypeExportVariable(C, EV); 568 break; 569 } 570 case RSExportType::ExportClassMatrix: { 571 genMatrixTypeExportVariable(C, EV); 572 break; 573 } 574 case RSExportType::ExportClassConstantArray: { 575 genConstantArrayTypeExportVariable(C, EV); 576 break; 577 } 578 case RSExportType::ExportClassRecord: { 579 genRecordTypeExportVariable(C, EV); 580 break; 581 } 582 default: { 583 slangAssert(false && "Unknown class of type"); 584 } 585 } 586 587 return; 588} 589 590void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) { 591 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX 592 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" 593 << std::endl; 594 595 // invoke_*() 596 Context::ArgTy Args; 597 598 if (EF->hasParam()) { 599 for (RSExportFunc::const_param_iterator I = EF->params_begin(), 600 E = EF->params_end(); 601 I != E; 602 I++) { 603 Args.push_back(std::make_pair(GetTypeName((*I)->getType()), 604 (*I)->getName())); 605 } 606 } 607 608 C.startFunction(Context::AM_Public, 609 false, 610 "void", 611 "invoke_" + EF->getName(/*Mangle=*/ false), 612 // We are using un-mangled name since Java 613 // supports method overloading. 614 Args); 615 616 if (!EF->hasParam()) { 617 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" 618 << std::endl; 619 } else { 620 const RSExportRecordType *ERT = EF->getParamPacketType(); 621 std::string FieldPackerName = EF->getName() + "_fp"; 622 623 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 624 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 625 626 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " 627 << FieldPackerName << ");" << std::endl; 628 } 629 630 C.endFunction(); 631 return; 632} 633 634void RSReflection::genExportForEach(Context &C, const RSExportForEach *EF) { 635 if (EF->isDummyRoot()) { 636 // Skip reflection for dummy root() kernels. Note that we have to 637 // advance the next slot number for ForEach, however. 638 C.indent() << "//private final static int "RS_EXPORT_FOREACH_INDEX_PREFIX 639 << EF->getName() << " = " << C.getNextExportForEachSlot() << ";" 640 << std::endl; 641 return; 642 } 643 644 C.indent() << "private final static int "RS_EXPORT_FOREACH_INDEX_PREFIX 645 << EF->getName() << " = " << C.getNextExportForEachSlot() << ";" 646 << std::endl; 647 648 // forEach_*() 649 Context::ArgTy Args; 650 651 slangAssert(EF->getNumParameters() > 0); 652 653 if (EF->hasIn()) 654 Args.push_back(std::make_pair("Allocation", "ain")); 655 if (EF->hasOut()) 656 Args.push_back(std::make_pair("Allocation", "aout")); 657 658 const RSExportRecordType *ERT = EF->getParamPacketType(); 659 if (ERT) { 660 for (RSExportForEach::const_param_iterator I = EF->params_begin(), 661 E = EF->params_end(); 662 I != E; 663 I++) { 664 Args.push_back(std::make_pair(GetTypeName((*I)->getType()), 665 (*I)->getName())); 666 } 667 } 668 669 C.startFunction(Context::AM_Public, 670 false, 671 "void", 672 "forEach_" + EF->getName(), 673 Args); 674 675 const RSExportType *IET = EF->getInType(); 676 if (IET) { 677 genTypeCheck(C, IET, "ain"); 678 } 679 680 const RSExportType *OET = EF->getOutType(); 681 if (OET) { 682 genTypeCheck(C, OET, "aout"); 683 } 684 685 if (EF->hasIn() && EF->hasOut()) { 686 C.indent() << "// Verify dimensions" << std::endl; 687 C.indent() << "Type tIn = ain.getType();" << std::endl; 688 C.indent() << "Type tOut = aout.getType();" << std::endl; 689 C.indent() << "if ((tIn.getCount() != tOut.getCount()) ||" << std::endl; 690 C.indent() << " (tIn.getX() != tOut.getX()) ||" << std::endl; 691 C.indent() << " (tIn.getY() != tOut.getY()) ||" << std::endl; 692 C.indent() << " (tIn.getZ() != tOut.getZ()) ||" << std::endl; 693 C.indent() << " (tIn.hasFaces() != tOut.hasFaces()) ||" << std::endl; 694 C.indent() << " (tIn.hasMipmaps() != tOut.hasMipmaps())) {" << std::endl; 695 C.indent() << " throw new RSRuntimeException(\"Dimension mismatch " 696 << "between input and output parameters!\");"; 697 C.out() << std::endl; 698 C.indent() << "}" << std::endl; 699 } 700 701 std::string FieldPackerName = EF->getName() + "_fp"; 702 if (ERT) { 703 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) { 704 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 705 } 706 } 707 C.indent() << "forEach("RS_EXPORT_FOREACH_INDEX_PREFIX << EF->getName(); 708 709 if (EF->hasIn()) 710 C.out() << ", ain"; 711 else 712 C.out() << ", null"; 713 714 if (EF->hasOut()) 715 C.out() << ", aout"; 716 else 717 C.out() << ", null"; 718 719 if (EF->hasUsrData()) 720 C.out() << ", " << FieldPackerName; 721 else 722 C.out() << ", null"; 723 724 C.out() << ");" << std::endl; 725 726 C.endFunction(); 727 return; 728} 729 730void RSReflection::genTypeInstanceFromPointer(Context &C, 731 const RSExportType *ET) { 732 if (ET->getClass() == RSExportType::ExportClassPointer) { 733 const RSExportPointerType *EPT = 734 static_cast<const RSExportPointerType*>(ET); 735 genTypeInstance(C, EPT->getPointeeType()); 736 } 737} 738 739void RSReflection::genTypeInstance(Context &C, 740 const RSExportType *ET) { 741 switch (ET->getClass()) { 742 case RSExportType::ExportClassPrimitive: 743 case RSExportType::ExportClassVector: 744 case RSExportType::ExportClassConstantArray: { 745 std::string TypeName = ET->getElementName(); 746 if (C.addTypeNameForElement(TypeName)) { 747 C.indent() << RS_ELEM_PREFIX << TypeName << " = Element." << TypeName 748 << "(rs);" << std::endl; 749 } 750 break; 751 } 752 753 case RSExportType::ExportClassRecord: { 754 std::string ClassName = ET->getElementName(); 755 if (C.addTypeNameForElement(ClassName)) { 756 C.indent() << RS_ELEM_PREFIX << ClassName << " = " << ClassName << 757 ".createElement(rs);" << std::endl; 758 } 759 break; 760 } 761 762 default: 763 break; 764 } 765} 766 767void RSReflection::genFieldPackerInstance(Context &C, 768 const RSExportType *ET) { 769 switch (ET->getClass()) { 770 case RSExportType::ExportClassPrimitive: 771 case RSExportType::ExportClassVector: 772 case RSExportType::ExportClassConstantArray: 773 case RSExportType::ExportClassRecord: { 774 std::string TypeName = ET->getElementName(); 775 C.addTypeNameForFieldPacker(TypeName); 776 break; 777 } 778 779 default: 780 break; 781 } 782} 783 784void RSReflection::genTypeCheck(Context &C, 785 const RSExportType *ET, 786 const char *VarName) { 787 C.indent() << "// check " << VarName << std::endl; 788 789 if (ET->getClass() == RSExportType::ExportClassPointer) { 790 const RSExportPointerType *EPT = 791 static_cast<const RSExportPointerType*>(ET); 792 ET = EPT->getPointeeType(); 793 } 794 795 std::string TypeName; 796 797 switch (ET->getClass()) { 798 case RSExportType::ExportClassPrimitive: 799 case RSExportType::ExportClassVector: 800 case RSExportType::ExportClassRecord: { 801 TypeName = ET->getElementName(); 802 break; 803 } 804 805 default: 806 break; 807 } 808 809 if (!TypeName.empty()) { 810 C.indent() << "if (!" << VarName 811 << ".getType().getElement().isCompatible(" RS_ELEM_PREFIX 812 << TypeName << ")) {" << std::endl; 813 C.indent() << " throw new RSRuntimeException(\"Type mismatch with " 814 << TypeName << "!\");" << std::endl; 815 C.indent() << "}" << std::endl; 816 } 817 818 return; 819} 820 821 822void RSReflection::genPrimitiveTypeExportVariable( 823 Context &C, 824 const RSExportVar *EV) { 825 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive) 826 && "Variable should be type of primitive here"); 827 828 const RSExportPrimitiveType *EPT = 829 static_cast<const RSExportPrimitiveType*>(EV->getType()); 830 std::string TypeName = GetTypeName(EPT); 831 std::string VarName = EV->getName(); 832 833 genPrivateExportVariable(C, TypeName, EV->getName()); 834 835 if (EV->isConst()) { 836 C.indent() << "public final static " << TypeName 837 << " " RS_EXPORT_VAR_CONST_PREFIX << VarName << " = "; 838 const clang::APValue &Val = EV->getInit(); 839 C.out() << RSReflectionBase::genInitValue(Val, EPT->getType() == 840 RSExportPrimitiveType::DataTypeBoolean) << ";" << std::endl; 841 } else { 842 // set_*() 843 // This must remain synchronized, since multiple Dalvik threads may 844 // be calling setters. 845 C.startFunction(Context::AM_PublicSynchronized, 846 false, 847 "void", 848 "set_" + VarName, 849 1, 850 TypeName.c_str(), "v"); 851 if (EV->isUnsigned()) { 852 // We create/cache a per-type FieldPacker. This allows us to reuse the 853 // validation logic (for catching negative inputs from Dalvik, as well 854 // as inputs that are too large to be represented in the unsigned type). 855 std::string ElemName = EPT->getElementName(); 856 std::string FPName; 857 FPName = RS_FP_PREFIX + ElemName; 858 C.indent() << "if (" << FPName << "!= null) {" 859 << std::endl; 860 C.incIndentLevel(); 861 C.indent() << FPName << ".reset();" << std::endl; 862 C.decIndentLevel(); 863 C.indent() << "} else {" << std::endl; 864 C.incIndentLevel(); 865 C.indent() << FPName << " = new FieldPacker(" 866 << EPT->getSize() << ");" << std::endl; 867 C.decIndentLevel(); 868 C.indent() << "}" << std::endl; 869 870 genPackVarOfType(C, EPT, "v", FPName.c_str()); 871 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName 872 << ", " << FPName << ");" << std::endl; 873 } else { 874 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName 875 << ", v);" << std::endl; 876 } 877 878 // Dalvik update comes last, since the input may be invalid (and hence 879 // throw an exception). 880 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl; 881 882 C.endFunction(); 883 } 884 885 genGetExportVariable(C, TypeName, VarName); 886 return; 887} 888 889void RSReflection::genPointerTypeExportVariable(Context &C, 890 const RSExportVar *EV) { 891 const RSExportType *ET = EV->getType(); 892 const RSExportType *PointeeType; 893 894 slangAssert((ET->getClass() == RSExportType::ExportClassPointer) && 895 "Variable should be type of pointer here"); 896 897 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 898 std::string TypeName = GetTypeName(ET); 899 std::string VarName = EV->getName(); 900 901 genPrivateExportVariable(C, TypeName, VarName); 902 903 // bind_*() 904 C.startFunction(Context::AM_Public, 905 false, 906 "void", 907 "bind_" + VarName, 908 1, 909 TypeName.c_str(), "v"); 910 911 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl; 912 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX 913 << VarName << ");" << std::endl; 914 915 if (PointeeType->getClass() == RSExportType::ExportClassRecord) 916 C.indent() << "else bindAllocation(v.getAllocation(), " 917 RS_EXPORT_VAR_INDEX_PREFIX << VarName << ");" 918 << std::endl; 919 else 920 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX 921 << VarName << ");" << std::endl; 922 923 C.endFunction(); 924 925 genGetExportVariable(C, TypeName, VarName); 926 return; 927} 928 929void RSReflection::genVectorTypeExportVariable(Context &C, 930 const RSExportVar *EV) { 931 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassVector) && 932 "Variable should be type of vector here"); 933 934 std::string TypeName = GetTypeName(EV->getType()); 935 std::string VarName = EV->getName(); 936 937 genPrivateExportVariable(C, TypeName, VarName); 938 genSetExportVariable(C, TypeName, EV); 939 genGetExportVariable(C, TypeName, VarName); 940 return; 941} 942 943void RSReflection::genMatrixTypeExportVariable(Context &C, 944 const RSExportVar *EV) { 945 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) && 946 "Variable should be type of matrix here"); 947 948 const RSExportType *ET = EV->getType(); 949 std::string TypeName = GetTypeName(ET); 950 std::string VarName = EV->getName(); 951 952 genPrivateExportVariable(C, TypeName, VarName); 953 954 // set_*() 955 if (!EV->isConst()) { 956 const char *FieldPackerName = "fp"; 957 C.startFunction(Context::AM_PublicSynchronized, 958 false, 959 "void", 960 "set_" + VarName, 961 1, 962 TypeName.c_str(), "v"); 963 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl; 964 965 if (genCreateFieldPacker(C, ET, FieldPackerName)) 966 genPackVarOfType(C, ET, "v", FieldPackerName); 967 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", " 968 << FieldPackerName << ");" << std::endl; 969 970 C.endFunction(); 971 } 972 973 genGetExportVariable(C, TypeName, VarName); 974 return; 975} 976 977void RSReflection::genConstantArrayTypeExportVariable( 978 Context &C, 979 const RSExportVar *EV) { 980 slangAssert((EV->getType()->getClass() == 981 RSExportType::ExportClassConstantArray) && 982 "Variable should be type of constant array here"); 983 984 std::string TypeName = GetTypeName(EV->getType()); 985 std::string VarName = EV->getName(); 986 987 genPrivateExportVariable(C, TypeName, VarName); 988 genSetExportVariable(C, TypeName, EV); 989 genGetExportVariable(C, TypeName, VarName); 990 return; 991} 992 993void RSReflection::genRecordTypeExportVariable(Context &C, 994 const RSExportVar *EV) { 995 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && 996 "Variable should be type of struct here"); 997 998 std::string TypeName = GetTypeName(EV->getType()); 999 std::string VarName = EV->getName(); 1000 1001 genPrivateExportVariable(C, TypeName, VarName); 1002 genSetExportVariable(C, TypeName, EV); 1003 genGetExportVariable(C, TypeName, VarName); 1004 return; 1005} 1006 1007void RSReflection::genPrivateExportVariable(Context &C, 1008 const std::string &TypeName, 1009 const std::string &VarName) { 1010 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 1011 << VarName << ";" << std::endl; 1012 return; 1013} 1014 1015void RSReflection::genSetExportVariable(Context &C, 1016 const std::string &TypeName, 1017 const RSExportVar *EV) { 1018 if (!EV->isConst()) { 1019 const char *FieldPackerName = "fp"; 1020 std::string VarName = EV->getName(); 1021 const RSExportType *ET = EV->getType(); 1022 C.startFunction(Context::AM_PublicSynchronized, 1023 false, 1024 "void", 1025 "set_" + VarName, 1026 1, 1027 TypeName.c_str(), "v"); 1028 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;" << std::endl; 1029 1030 if (genCreateFieldPacker(C, ET, FieldPackerName)) 1031 genPackVarOfType(C, ET, "v", FieldPackerName); 1032 1033 if (mRSContext->getTargetAPI() < SLANG_JB_TARGET_API) { 1034 // Legacy apps must use the old setVar() without Element/dim components. 1035 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName 1036 << ", " << FieldPackerName << ");" << std::endl; 1037 } else { 1038 // We only have support for one-dimensional array reflection today, 1039 // but the entry point (i.e. setVar()) takes an array of dimensions. 1040 C.indent() << "int []__dimArr = new int[1];" << std::endl; 1041 C.indent() << "__dimArr[0] = " << ET->getSize() << ";" << std::endl; 1042 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", " 1043 << FieldPackerName << ", " RS_ELEM_PREFIX 1044 << ET->getElementName() << ", __dimArr);" << std::endl; 1045 } 1046 1047 C.endFunction(); 1048 } 1049 return; 1050} 1051 1052void RSReflection::genGetExportVariable(Context &C, 1053 const std::string &TypeName, 1054 const std::string &VarName) { 1055 C.startFunction(Context::AM_Public, 1056 false, 1057 TypeName.c_str(), 1058 "get_" + VarName, 1059 0); 1060 1061 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl; 1062 1063 C.endFunction(); 1064 return; 1065} 1066 1067/******************* Methods to generate script class /end *******************/ 1068 1069bool RSReflection::genCreateFieldPacker(Context &C, 1070 const RSExportType *ET, 1071 const char *FieldPackerName) { 1072 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 1073 if (AllocSize > 0) 1074 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" 1075 << AllocSize << ");" << std::endl; 1076 else 1077 return false; 1078 return true; 1079} 1080 1081void RSReflection::genPackVarOfType(Context &C, 1082 const RSExportType *ET, 1083 const char *VarName, 1084 const char *FieldPackerName) { 1085 switch (ET->getClass()) { 1086 case RSExportType::ExportClassPrimitive: 1087 case RSExportType::ExportClassVector: { 1088 C.indent() << FieldPackerName << "." 1089 << GetPackerAPIName( 1090 static_cast<const RSExportPrimitiveType*>(ET)) 1091 << "(" << VarName << ");" << std::endl; 1092 break; 1093 } 1094 case RSExportType::ExportClassPointer: { 1095 // Must reflect as type Allocation in Java 1096 const RSExportType *PointeeType = 1097 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 1098 1099 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 1100 C.indent() << FieldPackerName << ".addI32(" << VarName 1101 << ".getPtr());" << std::endl; 1102 else 1103 C.indent() << FieldPackerName << ".addI32(" << VarName 1104 << ".getAllocation().getPtr());" << std::endl; 1105 break; 1106 } 1107 case RSExportType::ExportClassMatrix: { 1108 C.indent() << FieldPackerName << ".addMatrix(" << VarName << ");" 1109 << std::endl; 1110 break; 1111 } 1112 case RSExportType::ExportClassConstantArray: { 1113 const RSExportConstantArrayType *ECAT = 1114 static_cast<const RSExportConstantArrayType *>(ET); 1115 1116 // TODO(zonr): more elegant way. Currently, we obtain the unique index 1117 // variable (this method involves recursive call which means 1118 // we may have more than one level loop, therefore we can't 1119 // always use the same index variable name here) name given 1120 // in the for-loop from counting the '.' in @VarName. 1121 unsigned Level = 0; 1122 size_t LastDotPos = 0; 1123 std::string ElementVarName(VarName); 1124 1125 while (LastDotPos != std::string::npos) { 1126 LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1); 1127 Level++; 1128 } 1129 std::string IndexVarName("ct"); 1130 IndexVarName.append(llvm::utostr_32(Level)); 1131 1132 C.indent() << "for (int " << IndexVarName << " = 0; " << 1133 IndexVarName << " < " << ECAT->getSize() << "; " << 1134 IndexVarName << "++)"; 1135 C.startBlock(); 1136 1137 ElementVarName.append("[" + IndexVarName + "]"); 1138 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(), 1139 FieldPackerName); 1140 1141 C.endBlock(); 1142 break; 1143 } 1144 case RSExportType::ExportClassRecord: { 1145 const RSExportRecordType *ERT = 1146 static_cast<const RSExportRecordType*>(ET); 1147 // Relative pos from now on in field packer 1148 unsigned Pos = 0; 1149 1150 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1151 E = ERT->fields_end(); 1152 I != E; 1153 I++) { 1154 const RSExportRecordType::Field *F = *I; 1155 std::string FieldName; 1156 size_t FieldOffset = F->getOffsetInParent(); 1157 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1158 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1159 1160 if (VarName != NULL) 1161 FieldName = VarName + ("." + F->getName()); 1162 else 1163 FieldName = F->getName(); 1164 1165 if (FieldOffset > Pos) 1166 C.indent() << FieldPackerName << ".skip(" 1167 << (FieldOffset - Pos) << ");" << std::endl; 1168 1169 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 1170 1171 // There is padding in the field type 1172 if (FieldAllocSize > FieldStoreSize) 1173 C.indent() << FieldPackerName << ".skip(" 1174 << (FieldAllocSize - FieldStoreSize) 1175 << ");" << std::endl; 1176 1177 Pos = FieldOffset + FieldAllocSize; 1178 } 1179 1180 // There maybe some padding after the struct 1181 if (RSExportType::GetTypeAllocSize(ERT) > Pos) 1182 C.indent() << FieldPackerName << ".skip(" 1183 << RSExportType::GetTypeAllocSize(ERT) - Pos << ");" 1184 << std::endl; 1185 break; 1186 } 1187 default: { 1188 slangAssert(false && "Unknown class of type"); 1189 } 1190 } 1191 1192 return; 1193} 1194 1195void RSReflection::genAllocateVarOfType(Context &C, 1196 const RSExportType *T, 1197 const std::string &VarName) { 1198 switch (T->getClass()) { 1199 case RSExportType::ExportClassPrimitive: { 1200 // Primitive type like int in Java has its own storage once it's declared. 1201 // 1202 // FIXME: Should we allocate storage for RS object? 1203 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType()) 1204 // C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1205 // << std::endl; 1206 break; 1207 } 1208 case RSExportType::ExportClassPointer: { 1209 // Pointer type is an instance of Allocation or a TypeClass whose value is 1210 // expected to be assigned by programmer later in Java program. Therefore 1211 // we don't reflect things like [VarName] = new Allocation(); 1212 C.indent() << VarName << " = null;" << std::endl; 1213 break; 1214 } 1215 case RSExportType::ExportClassConstantArray: { 1216 const RSExportConstantArrayType *ECAT = 1217 static_cast<const RSExportConstantArrayType *>(T); 1218 const RSExportType *ElementType = ECAT->getElementType(); 1219 1220 C.indent() << VarName << " = new " << GetTypeName(ElementType) 1221 << "[" << ECAT->getSize() << "];" << std::endl; 1222 1223 // Primitive type element doesn't need allocation code. 1224 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) { 1225 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; " 1226 "$ct++)"; 1227 C.startBlock(); 1228 1229 std::string ElementVarName(VarName); 1230 ElementVarName.append("[$ct]"); 1231 genAllocateVarOfType(C, ElementType, ElementVarName); 1232 1233 C.endBlock(); 1234 } 1235 break; 1236 } 1237 case RSExportType::ExportClassVector: 1238 case RSExportType::ExportClassMatrix: 1239 case RSExportType::ExportClassRecord: { 1240 C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1241 << std::endl; 1242 break; 1243 } 1244 } 1245 return; 1246} 1247 1248void RSReflection::genNewItemBufferIfNull(Context &C, 1249 const char *Index) { 1250 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME " == null) " 1251 RS_TYPE_ITEM_BUFFER_NAME " = " 1252 "new " RS_TYPE_ITEM_CLASS_NAME 1253 "[getType().getX() /* count */];" 1254 << std::endl; 1255 if (Index != NULL) 1256 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) " 1257 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = " 1258 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 1259 return; 1260} 1261 1262void RSReflection::genNewItemBufferPackerIfNull(Context &C) { 1263 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " == null) " 1264 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1265 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1266 ".sizeof * getType().getX()/* count */" 1267 ");" << std::endl; 1268 return; 1269} 1270 1271/********************** Methods to generate type class **********************/ 1272bool RSReflection::genTypeClass(Context &C, 1273 const RSExportRecordType *ERT, 1274 std::string &ErrorMsg) { 1275 std::string ClassName = ERT->getElementName(); 1276 1277 if (!C.startClass(Context::AM_Public, 1278 false, 1279 ClassName, 1280 RS_TYPE_CLASS_SUPER_CLASS_NAME, 1281 ErrorMsg)) 1282 return false; 1283 1284 mGeneratedFileNames->push_back(ClassName); 1285 1286 genTypeItemClass(C, ERT); 1287 1288 // Declare item buffer and item buffer packer 1289 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]" 1290 ";" << std::endl; 1291 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" 1292 << std::endl; 1293 C.indent() << "private static java.lang.ref.WeakReference<Element> " 1294 RS_TYPE_ELEMENT_REF_NAME 1295 " = new java.lang.ref.WeakReference<Element>(null);" << std::endl; 1296 1297 genTypeClassConstructor(C, ERT); 1298 genTypeClassCopyToArrayLocal(C, ERT); 1299 genTypeClassCopyToArray(C, ERT); 1300 genTypeClassItemSetter(C, ERT); 1301 genTypeClassItemGetter(C, ERT); 1302 genTypeClassComponentSetter(C, ERT); 1303 genTypeClassComponentGetter(C, ERT); 1304 genTypeClassCopyAll(C, ERT); 1305 genTypeClassResize(C); 1306 1307 C.endClass(); 1308 1309 C.resetFieldIndex(); 1310 C.clearFieldIndexMap(); 1311 1312 return true; 1313} 1314 1315void RSReflection::genTypeItemClass(Context &C, 1316 const RSExportRecordType *ERT) { 1317 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 1318 C.startBlock(); 1319 1320 C.indent() << "public static final int sizeof = " 1321 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl; 1322 1323 // Member elements 1324 C.out() << std::endl; 1325 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1326 FE = ERT->fields_end(); 1327 FI != FE; 1328 FI++) { 1329 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() 1330 << ";" << std::endl; 1331 } 1332 1333 // Constructor 1334 C.out() << std::endl; 1335 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 1336 C.startBlock(); 1337 1338 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1339 FE = ERT->fields_end(); 1340 FI != FE; 1341 FI++) { 1342 const RSExportRecordType::Field *F = *FI; 1343 genAllocateVarOfType(C, F->getType(), F->getName()); 1344 } 1345 1346 // end Constructor 1347 C.endBlock(); 1348 1349 // end Item class 1350 C.endBlock(); 1351 1352 return; 1353} 1354 1355void RSReflection::genTypeClassConstructor(Context &C, 1356 const RSExportRecordType *ERT) { 1357 const char *RenderScriptVar = "rs"; 1358 1359 C.startFunction(Context::AM_Public, 1360 true, 1361 "Element", 1362 "createElement", 1363 1, 1364 "RenderScript", RenderScriptVar); 1365 1366 // TODO(all): Fix weak-refs + multi-context issue. 1367 // C.indent() << "Element e = " << RS_TYPE_ELEMENT_REF_NAME 1368 // << ".get();" << std::endl; 1369 // C.indent() << "if (e != null) return e;" << std::endl; 1370 genBuildElement(C, "eb", ERT, RenderScriptVar, /* IsInline = */true); 1371 C.indent() << "return eb.create();" << std::endl; 1372 // C.indent() << "e = eb.create();" << std::endl; 1373 // C.indent() << RS_TYPE_ELEMENT_REF_NAME 1374 // << " = new java.lang.ref.WeakReference<Element>(e);" 1375 // << std::endl; 1376 // C.indent() << "return e;" << std::endl; 1377 C.endFunction(); 1378 1379 1380 // private with element 1381 C.startFunction(Context::AM_Private, 1382 false, 1383 NULL, 1384 C.getClassName(), 1385 1, 1386 "RenderScript", RenderScriptVar); 1387 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1388 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1389 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1390 << std::endl; 1391 C.endFunction(); 1392 1393 // 1D without usage 1394 C.startFunction(Context::AM_Public, 1395 false, 1396 NULL, 1397 C.getClassName(), 1398 2, 1399 "RenderScript", RenderScriptVar, 1400 "int", "count"); 1401 1402 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1403 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1404 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1405 << std::endl; 1406 // Call init() in super class 1407 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl; 1408 C.endFunction(); 1409 1410 // 1D with usage 1411 C.startFunction(Context::AM_Public, 1412 false, 1413 NULL, 1414 C.getClassName(), 1415 3, 1416 "RenderScript", RenderScriptVar, 1417 "int", "count", 1418 "int", "usages"); 1419 1420 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1421 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1422 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1423 << std::endl; 1424 // Call init() in super class 1425 C.indent() << "init(" << RenderScriptVar << ", count, usages);" << std::endl; 1426 C.endFunction(); 1427 1428 1429 // create1D with usage 1430 C.startFunction(Context::AM_Public, 1431 true, 1432 C.getClassName().c_str(), 1433 "create1D", 1434 3, 1435 "RenderScript", RenderScriptVar, 1436 "int", "dimX", 1437 "int", "usages"); 1438 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "(" 1439 << RenderScriptVar << ");" << std::endl; 1440 C.indent() << "obj.mAllocation = Allocation.createSized(" 1441 "rs, obj.mElement, dimX, usages);" << std::endl; 1442 C.indent() << "return obj;" << std::endl; 1443 C.endFunction(); 1444 1445 // create1D without usage 1446 C.startFunction(Context::AM_Public, 1447 true, 1448 C.getClassName().c_str(), 1449 "create1D", 1450 2, 1451 "RenderScript", RenderScriptVar, 1452 "int", "dimX"); 1453 C.indent() << "return create1D(" << RenderScriptVar 1454 << ", dimX, Allocation.USAGE_SCRIPT);" << std::endl; 1455 C.endFunction(); 1456 1457 1458 // create2D without usage 1459 C.startFunction(Context::AM_Public, 1460 true, 1461 C.getClassName().c_str(), 1462 "create2D", 1463 3, 1464 "RenderScript", RenderScriptVar, 1465 "int", "dimX", 1466 "int", "dimY"); 1467 C.indent() << "return create2D(" << RenderScriptVar 1468 << ", dimX, dimY, Allocation.USAGE_SCRIPT);" << std::endl; 1469 C.endFunction(); 1470 1471 // create2D with usage 1472 C.startFunction(Context::AM_Public, 1473 true, 1474 C.getClassName().c_str(), 1475 "create2D", 1476 4, 1477 "RenderScript", RenderScriptVar, 1478 "int", "dimX", 1479 "int", "dimY", 1480 "int", "usages"); 1481 1482 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "(" 1483 << RenderScriptVar << ");" << std::endl; 1484 C.indent() << "Type.Builder b = new Type.Builder(rs, obj.mElement);" 1485 << std::endl; 1486 C.indent() << "b.setX(dimX);" << std::endl; 1487 C.indent() << "b.setY(dimY);" << std::endl; 1488 C.indent() << "Type t = b.create();" << std::endl; 1489 C.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);" 1490 << std::endl; 1491 C.indent() << "return obj;" << std::endl; 1492 C.endFunction(); 1493 1494 1495 // createTypeBuilder 1496 C.startFunction(Context::AM_Public, 1497 true, 1498 "Type.Builder", 1499 "createTypeBuilder", 1500 1, 1501 "RenderScript", RenderScriptVar); 1502 C.indent() << "Element e = createElement(" << RenderScriptVar << ");" 1503 << std::endl; 1504 C.indent() << "return new Type.Builder(rs, e);" << std::endl; 1505 C.endFunction(); 1506 1507 // createCustom with usage 1508 C.startFunction(Context::AM_Public, 1509 true, 1510 C.getClassName().c_str(), 1511 "createCustom", 1512 3, 1513 "RenderScript", RenderScriptVar, 1514 "Type.Builder", "tb", 1515 "int", "usages"); 1516 C.indent() << C.getClassName() << " obj = new " << C.getClassName() << "(" 1517 << RenderScriptVar << ");" << std::endl; 1518 C.indent() << "Type t = tb.create();" << std::endl; 1519 C.indent() << "if (t.getElement() != obj.mElement) {" << std::endl; 1520 C.indent() << " throw new RSIllegalArgumentException(" 1521 "\"Type.Builder did not match expected element type.\");" 1522 << std::endl; 1523 C.indent() << "}" << std::endl; 1524 C.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);" 1525 << std::endl; 1526 C.indent() << "return obj;" << std::endl; 1527 C.endFunction(); 1528} 1529 1530 1531void RSReflection::genTypeClassCopyToArray(Context &C, 1532 const RSExportRecordType *ERT) { 1533 C.startFunction(Context::AM_Private, 1534 false, 1535 "void", 1536 "copyToArray", 1537 2, 1538 RS_TYPE_ITEM_CLASS_NAME, "i", 1539 "int", "index"); 1540 1541 genNewItemBufferPackerIfNull(C); 1542 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1543 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1544 << std::endl; 1545 1546 C.indent() << "copyToArrayLocal(i, " RS_TYPE_ITEM_BUFFER_PACKER_NAME 1547 ");" << std::endl; 1548 1549 C.endFunction(); 1550 return; 1551} 1552 1553void RSReflection::genTypeClassCopyToArrayLocal(Context &C, 1554 const RSExportRecordType *ERT) { 1555 C.startFunction(Context::AM_Private, 1556 false, 1557 "void", 1558 "copyToArrayLocal", 1559 2, 1560 RS_TYPE_ITEM_CLASS_NAME, "i", 1561 "FieldPacker", "fp"); 1562 1563 genPackVarOfType(C, ERT, "i", "fp"); 1564 1565 C.endFunction(); 1566 return; 1567} 1568 1569void RSReflection::genTypeClassItemSetter(Context &C, 1570 const RSExportRecordType *ERT) { 1571 C.startFunction(Context::AM_PublicSynchronized, 1572 false, 1573 "void", 1574 "set", 1575 3, 1576 RS_TYPE_ITEM_CLASS_NAME, "i", 1577 "int", "index", 1578 "boolean", "copyNow"); 1579 genNewItemBufferIfNull(C, NULL); 1580 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl; 1581 1582 C.indent() << "if (copyNow) "; 1583 C.startBlock(); 1584 1585 C.indent() << "copyToArray(i, index);" << std::endl; 1586 C.indent() << "FieldPacker fp = new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1587 ".sizeof);" << std::endl; 1588 C.indent() << "copyToArrayLocal(i, fp);" << std::endl; 1589 C.indent() << "mAllocation.setFromFieldPacker(index, fp);" << std::endl; 1590 1591 // End of if (copyNow) 1592 C.endBlock(); 1593 1594 C.endFunction(); 1595 return; 1596} 1597 1598void RSReflection::genTypeClassItemGetter(Context &C, 1599 const RSExportRecordType *ERT) { 1600 C.startFunction(Context::AM_PublicSynchronized, 1601 false, 1602 RS_TYPE_ITEM_CLASS_NAME, 1603 "get", 1604 1, 1605 "int", "index"); 1606 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" 1607 << std::endl; 1608 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl; 1609 C.endFunction(); 1610 return; 1611} 1612 1613void RSReflection::genTypeClassComponentSetter(Context &C, 1614 const RSExportRecordType *ERT) { 1615 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1616 FE = ERT->fields_end(); 1617 FI != FE; 1618 FI++) { 1619 const RSExportRecordType::Field *F = *FI; 1620 size_t FieldOffset = F->getOffsetInParent(); 1621 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1622 unsigned FieldIndex = C.getFieldIndex(F); 1623 1624 C.startFunction(Context::AM_PublicSynchronized, 1625 false, 1626 "void", 1627 "set_" + F->getName(), 3, 1628 "int", "index", 1629 GetTypeName(F->getType()).c_str(), "v", 1630 "boolean", "copyNow"); 1631 genNewItemBufferPackerIfNull(C); 1632 genNewItemBufferIfNull(C, "index"); 1633 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1634 << " = v;" << std::endl; 1635 1636 C.indent() << "if (copyNow) "; 1637 C.startBlock(); 1638 1639 if (FieldOffset > 0) 1640 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1641 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " 1642 << FieldOffset << ");" << std::endl; 1643 else 1644 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1645 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1646 << std::endl; 1647 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1648 1649 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" 1650 << std::endl; 1651 genPackVarOfType(C, F->getType(), "v", "fp"); 1652 C.indent() << "mAllocation.setFromFieldPacker(index, " << FieldIndex 1653 << ", fp);" 1654 << std::endl; 1655 1656 // End of if (copyNow) 1657 C.endBlock(); 1658 1659 C.endFunction(); 1660 } 1661 return; 1662} 1663 1664void RSReflection::genTypeClassComponentGetter(Context &C, 1665 const RSExportRecordType *ERT) { 1666 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1667 FE = ERT->fields_end(); 1668 FI != FE; 1669 FI++) { 1670 const RSExportRecordType::Field *F = *FI; 1671 C.startFunction(Context::AM_PublicSynchronized, 1672 false, 1673 GetTypeName(F->getType()).c_str(), 1674 "get_" + F->getName(), 1675 1, 1676 "int", "index"); 1677 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return " 1678 << GetTypeNullValue(F->getType()) << ";" << std::endl; 1679 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1680 << ";" << std::endl; 1681 C.endFunction(); 1682 } 1683 return; 1684} 1685 1686void RSReflection::genTypeClassCopyAll(Context &C, 1687 const RSExportRecordType *ERT) { 1688 C.startFunction(Context::AM_PublicSynchronized, false, "void", "copyAll", 0); 1689 1690 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)" 1691 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" 1692 << std::endl; 1693 C.indent() << "mAllocation.setFromFieldPacker(0, " 1694 RS_TYPE_ITEM_BUFFER_PACKER_NAME");" 1695 << std::endl; 1696 1697 C.endFunction(); 1698 return; 1699} 1700 1701void RSReflection::genTypeClassResize(Context &C) { 1702 C.startFunction(Context::AM_PublicSynchronized, 1703 false, 1704 "void", 1705 "resize", 1706 1, 1707 "int", "newSize"); 1708 1709 C.indent() << "if (mItemArray != null) "; 1710 C.startBlock(); 1711 C.indent() << "int oldSize = mItemArray.length;" << std::endl; 1712 C.indent() << "int copySize = Math.min(oldSize, newSize);" << std::endl; 1713 C.indent() << "if (newSize == oldSize) return;" << std::endl; 1714 C.indent() << "Item ni[] = new Item[newSize];" << std::endl; 1715 C.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);" 1716 << std::endl; 1717 C.indent() << "mItemArray = ni;" << std::endl; 1718 C.endBlock(); 1719 C.indent() << "mAllocation.resize(newSize);" << std::endl; 1720 1721 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " != null) " 1722 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1723 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1724 ".sizeof * getType().getX()/* count */" 1725 ");" << std::endl; 1726 1727 C.endFunction(); 1728 return; 1729} 1730 1731/******************** Methods to generate type class /end ********************/ 1732 1733/********** Methods to create Element in Java of given record type ***********/ 1734void RSReflection::genBuildElement(Context &C, 1735 const char *ElementBuilderName, 1736 const RSExportRecordType *ERT, 1737 const char *RenderScriptVar, 1738 bool IsInline) { 1739 C.indent() << "Element.Builder " << ElementBuilderName << " = " 1740 "new Element.Builder(" << RenderScriptVar << ");" << std::endl; 1741 1742 // eb.add(...) 1743 genAddElementToElementBuilder(C, 1744 ERT, 1745 "", 1746 ElementBuilderName, 1747 RenderScriptVar, 1748 /* ArraySize = */0); 1749 1750 if (!IsInline) 1751 C.indent() << "return " << ElementBuilderName << ".create();" << std::endl; 1752 return; 1753} 1754 1755#define EB_ADD(x) do { \ 1756 C.indent() << ElementBuilderName \ 1757 << ".add(" << x << ", \"" << VarName << "\""; \ 1758 if (ArraySize > 0) \ 1759 C.out() << ", " << ArraySize; \ 1760 C.out() << ");" << std::endl; \ 1761 C.incFieldIndex(); \ 1762} while (false) 1763 1764void RSReflection::genAddElementToElementBuilder(Context &C, 1765 const RSExportType *ET, 1766 const std::string &VarName, 1767 const char *ElementBuilderName, 1768 const char *RenderScriptVar, 1769 unsigned ArraySize) { 1770 std::string ElementConstruct = GetBuiltinElementConstruct(ET); 1771 1772 if (ElementConstruct != "") { 1773 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")"); 1774 } else { 1775 if ((ET->getClass() == RSExportType::ExportClassPrimitive) || 1776 (ET->getClass() == RSExportType::ExportClassVector)) { 1777 const RSExportPrimitiveType *EPT = 1778 static_cast<const RSExportPrimitiveType*>(ET); 1779 const char *DataTypeName = 1780 RSExportPrimitiveType::getRSReflectionType(EPT)->rs_type; 1781 int Size = (ET->getClass() == RSExportType::ExportClassVector) ? 1782 static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1783 1; 1784 1785 if (EPT->getClass() == RSExportType::ExportClassPrimitive) { 1786 // Element.createUser() 1787 EB_ADD("Element.createUser(" << RenderScriptVar 1788 << ", Element.DataType." 1789 << DataTypeName << ")"); 1790 } else { 1791 slangAssert((ET->getClass() == RSExportType::ExportClassVector) && 1792 "Unexpected type."); 1793 EB_ADD("Element.createVector(" << RenderScriptVar 1794 << ", Element.DataType." 1795 << DataTypeName << ", " 1796 << Size << ")"); 1797 } 1798#ifndef NDEBUG 1799 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 1800 // Pointer type variable should be resolved in 1801 // GetBuiltinElementConstruct() 1802 slangAssert(false && "??"); 1803 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 1804 // Matrix type variable should be resolved 1805 // in GetBuiltinElementConstruct() 1806 slangAssert(false && "??"); 1807#endif 1808 } else if (ET->getClass() == RSExportType::ExportClassConstantArray) { 1809 const RSExportConstantArrayType *ECAT = 1810 static_cast<const RSExportConstantArrayType *>(ET); 1811 1812 const RSExportType *ElementType = ECAT->getElementType(); 1813 if (ElementType->getClass() != RSExportType::ExportClassRecord) { 1814 genAddElementToElementBuilder(C, 1815 ECAT->getElementType(), 1816 VarName, 1817 ElementBuilderName, 1818 RenderScriptVar, 1819 ECAT->getSize()); 1820 } else { 1821 std::string NewElementBuilderName(ElementBuilderName); 1822 NewElementBuilderName.append(1, '_'); 1823 1824 genBuildElement(C, 1825 NewElementBuilderName.c_str(), 1826 static_cast<const RSExportRecordType*>(ElementType), 1827 RenderScriptVar, 1828 /* IsInline = */true); 1829 ArraySize = ECAT->getSize(); 1830 EB_ADD(NewElementBuilderName << ".create()"); 1831 } 1832 } else if (ET->getClass() == RSExportType::ExportClassRecord) { 1833 // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType. 1834 // 1835 // TODO(zonr): Generalize these two function such that there's no 1836 // duplicated codes. 1837 const RSExportRecordType *ERT = 1838 static_cast<const RSExportRecordType*>(ET); 1839 int Pos = 0; // relative pos from now on 1840 1841 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1842 E = ERT->fields_end(); 1843 I != E; 1844 I++) { 1845 const RSExportRecordType::Field *F = *I; 1846 std::string FieldName; 1847 int FieldOffset = F->getOffsetInParent(); 1848 int FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1849 int FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1850 1851 if (!VarName.empty()) 1852 FieldName = VarName + "." + F->getName(); 1853 else 1854 FieldName = F->getName(); 1855 1856 // Alignment 1857 genAddPaddingToElementBuiler(C, 1858 (FieldOffset - Pos), 1859 ElementBuilderName, 1860 RenderScriptVar); 1861 1862 // eb.add(...) 1863 C.addFieldIndexMapping(F); 1864 if (F->getType()->getClass() != RSExportType::ExportClassRecord) { 1865 genAddElementToElementBuilder(C, 1866 F->getType(), 1867 FieldName, 1868 ElementBuilderName, 1869 RenderScriptVar, 1870 0); 1871 } else { 1872 std::string NewElementBuilderName(ElementBuilderName); 1873 NewElementBuilderName.append(1, '_'); 1874 1875 genBuildElement(C, 1876 NewElementBuilderName.c_str(), 1877 static_cast<const RSExportRecordType*>(F->getType()), 1878 RenderScriptVar, 1879 /* IsInline = */true); 1880 1881 const std::string &VarName = FieldName; // Hack for EB_ADD macro 1882 EB_ADD(NewElementBuilderName << ".create()"); 1883 } 1884 1885 if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) { 1886 // There is padding within the field type. This is only necessary 1887 // for HC-targeted APIs. 1888 genAddPaddingToElementBuiler(C, 1889 (FieldAllocSize - FieldStoreSize), 1890 ElementBuilderName, 1891 RenderScriptVar); 1892 } 1893 1894 Pos = FieldOffset + FieldAllocSize; 1895 } 1896 1897 // There maybe some padding after the struct 1898 size_t RecordAllocSize = RSExportType::GetTypeAllocSize(ERT); 1899 1900 genAddPaddingToElementBuiler(C, 1901 RecordAllocSize - Pos, 1902 ElementBuilderName, 1903 RenderScriptVar); 1904 } else { 1905 slangAssert(false && "Unknown class of type"); 1906 } 1907 } 1908} 1909 1910void RSReflection::genAddPaddingToElementBuiler(Context &C, 1911 int PaddingSize, 1912 const char *ElementBuilderName, 1913 const char *RenderScriptVar) { 1914 unsigned ArraySize = 0; // Hack the EB_ADD macro 1915 while (PaddingSize > 0) { 1916 const std::string &VarName = C.createPaddingField(); 1917 if (PaddingSize >= 4) { 1918 EB_ADD("Element.U32(" << RenderScriptVar << ")"); 1919 PaddingSize -= 4; 1920 } else if (PaddingSize >= 2) { 1921 EB_ADD("Element.U16(" << RenderScriptVar << ")"); 1922 PaddingSize -= 2; 1923 } else if (PaddingSize >= 1) { 1924 EB_ADD("Element.U8(" << RenderScriptVar << ")"); 1925 PaddingSize -= 1; 1926 } 1927 } 1928 return; 1929} 1930 1931#undef EB_ADD 1932/******** Methods to create Element in Java of given record type /end ********/ 1933 1934bool RSReflection::reflect(const std::string &OutputPathBase, 1935 const std::string &OutputPackageName, 1936 const std::string &RSPackageName, 1937 const std::string &InputFileName, 1938 const std::string &OutputBCFileName) { 1939 Context *C = NULL; 1940 std::string ResourceId = ""; 1941 std::string PaddingPrefix = ""; 1942 1943 if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) { 1944 PaddingPrefix = "#padding_"; 1945 } else { 1946 PaddingPrefix = "#rs_padding_"; 1947 } 1948 1949 if (!GetClassNameFromFileName(OutputBCFileName, ResourceId)) 1950 return false; 1951 1952 if (ResourceId.empty()) 1953 ResourceId = "<Resource ID>"; 1954 1955 if (OutputPackageName.empty() || OutputPackageName == "-") 1956 C = new Context(OutputPathBase, InputFileName, "<Package Name>", 1957 RSPackageName, ResourceId, PaddingPrefix, true); 1958 else 1959 C = new Context(OutputPathBase, InputFileName, OutputPackageName, 1960 RSPackageName, ResourceId, PaddingPrefix, false); 1961 1962 if (C != NULL) { 1963 std::string ErrorMsg, ScriptClassName; 1964 // class ScriptC_<ScriptName> 1965 if (!GetClassNameFromFileName(InputFileName, ScriptClassName)) 1966 return false; 1967 1968 if (ScriptClassName.empty()) 1969 ScriptClassName = "<Input Script Name>"; 1970 1971 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX); 1972 1973 if (mRSContext->getLicenseNote() != NULL) { 1974 C->setLicenseNote(*(mRSContext->getLicenseNote())); 1975 } 1976 1977 if (!genScriptClass(*C, ScriptClassName, ErrorMsg)) { 1978 std::cerr << "Failed to generate class " << ScriptClassName << " (" 1979 << ErrorMsg << ")" << std::endl; 1980 return false; 1981 } 1982 1983 mGeneratedFileNames->push_back(ScriptClassName); 1984 1985 // class ScriptField_<TypeName> 1986 for (RSContext::const_export_type_iterator TI = 1987 mRSContext->export_types_begin(), 1988 TE = mRSContext->export_types_end(); 1989 TI != TE; 1990 TI++) { 1991 const RSExportType *ET = TI->getValue(); 1992 1993 if (ET->getClass() == RSExportType::ExportClassRecord) { 1994 const RSExportRecordType *ERT = 1995 static_cast<const RSExportRecordType*>(ET); 1996 1997 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1998 std::cerr << "Failed to generate type class for struct '" 1999 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl; 2000 return false; 2001 } 2002 } 2003 } 2004 } 2005 2006 return true; 2007} 2008 2009/************************** RSReflection::Context **************************/ 2010const char *const RSReflection::Context::ApacheLicenseNote = 2011 "/*\n" 2012 " * Copyright (C) 2011-2012 The Android Open Source Project\n" 2013 " *\n" 2014 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 2015 " * you may not use this file except in compliance with the License.\n" 2016 " * You may obtain a copy of the License at\n" 2017 " *\n" 2018 " * http://www.apache.org/licenses/LICENSE-2.0\n" 2019 " *\n" 2020 " * Unless required by applicable law or agreed to in writing, software\n" 2021 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 2022 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or " 2023 "implied.\n" 2024 " * See the License for the specific language governing permissions and\n" 2025 " * limitations under the License.\n" 2026 " */\n" 2027 "\n"; 2028 2029bool RSReflection::Context::openClassFile(const std::string &ClassName, 2030 std::string &ErrorMsg) { 2031 if (!mUseStdout) { 2032 mOF.clear(); 2033 std::string Path = 2034 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(), 2035 mPackageName.c_str()); 2036 2037 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg)) 2038 return false; 2039 2040 std::string ClassFile = Path + OS_PATH_SEPARATOR_STR + ClassName + ".java"; 2041 2042 mOF.open(ClassFile.c_str()); 2043 if (!mOF.good()) { 2044 ErrorMsg = "failed to open file '" + ClassFile + "' for write"; 2045 return false; 2046 } 2047 } 2048 return true; 2049} 2050 2051const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) { 2052 switch (AM) { 2053 case AM_Public: return "public"; break; 2054 case AM_Protected: return "protected"; break; 2055 case AM_Private: return "private"; break; 2056 case AM_PublicSynchronized: return "public synchronized"; break; 2057 default: return ""; break; 2058 } 2059} 2060 2061bool RSReflection::Context::startClass(AccessModifier AM, 2062 bool IsStatic, 2063 const std::string &ClassName, 2064 const char *SuperClassName, 2065 std::string &ErrorMsg) { 2066 if (mVerbose) 2067 std::cout << "Generating " << ClassName << ".java ..." << std::endl; 2068 2069 // Open file for class 2070 if (!openClassFile(ClassName, ErrorMsg)) 2071 return false; 2072 2073 // License 2074 out() << mLicenseNote; 2075 2076 // Notice of generated file 2077 out() << "/*" << std::endl; 2078 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl; 2079 out() << " * The source Renderscript file: " << mInputRSFile << std::endl; 2080 out() << " */" << std::endl; 2081 2082 // Package 2083 if (!mPackageName.empty()) 2084 out() << "package " << mPackageName << ";" << std::endl; 2085 out() << std::endl; 2086 2087 // Imports 2088 out() << "import " << mRSPackageName << ".*;" << std::endl; 2089 out() << "import android.content.res.Resources;" << std::endl; 2090 out() << std::endl; 2091 2092 // All reflected classes should be annotated as hidden, so that they won't 2093 // be exposed in SDK. 2094 out() << "/**" << std::endl; 2095 out() << " * @hide" << std::endl; 2096 out() << " */" << std::endl; 2097 2098 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " 2099 << ClassName; 2100 if (SuperClassName != NULL) 2101 out() << " extends " << SuperClassName; 2102 2103 startBlock(); 2104 2105 mClassName = ClassName; 2106 2107 return true; 2108} 2109 2110void RSReflection::Context::endClass() { 2111 endBlock(); 2112 if (!mUseStdout) 2113 mOF.close(); 2114 clear(); 2115 return; 2116} 2117 2118void RSReflection::Context::startBlock(bool ShouldIndent) { 2119 if (ShouldIndent) 2120 indent() << "{" << std::endl; 2121 else 2122 out() << " {" << std::endl; 2123 incIndentLevel(); 2124 return; 2125} 2126 2127void RSReflection::Context::endBlock() { 2128 decIndentLevel(); 2129 indent() << "}" << std::endl << std::endl; 2130 return; 2131} 2132 2133void RSReflection::Context::startTypeClass(const std::string &ClassName) { 2134 indent() << "public static class " << ClassName; 2135 startBlock(); 2136 return; 2137} 2138 2139void RSReflection::Context::endTypeClass() { 2140 endBlock(); 2141 return; 2142} 2143 2144void RSReflection::Context::startFunction(AccessModifier AM, 2145 bool IsStatic, 2146 const char *ReturnType, 2147 const std::string &FunctionName, 2148 int Argc, ...) { 2149 ArgTy Args; 2150 va_list vl; 2151 va_start(vl, Argc); 2152 2153 for (int i = 0; i < Argc; i++) { 2154 const char *ArgType = va_arg(vl, const char*); 2155 const char *ArgName = va_arg(vl, const char*); 2156 2157 Args.push_back(std::make_pair(ArgType, ArgName)); 2158 } 2159 va_end(vl); 2160 2161 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 2162 2163 return; 2164} 2165 2166void RSReflection::Context::startFunction(AccessModifier AM, 2167 bool IsStatic, 2168 const char *ReturnType, 2169 const std::string &FunctionName, 2170 const ArgTy &Args) { 2171 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") 2172 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 2173 2174 bool FirstArg = true; 2175 for (ArgTy::const_iterator I = Args.begin(), E = Args.end(); 2176 I != E; 2177 I++) { 2178 if (!FirstArg) 2179 out() << ", "; 2180 else 2181 FirstArg = false; 2182 2183 out() << I->first << " " << I->second; 2184 } 2185 2186 out() << ")"; 2187 startBlock(); 2188 2189 return; 2190} 2191 2192void RSReflection::Context::endFunction() { 2193 endBlock(); 2194 return; 2195} 2196 2197bool RSReflection::Context::addTypeNameForElement( 2198 const std::string &TypeName) { 2199 if (mTypesToCheck.find(TypeName) == mTypesToCheck.end()) { 2200 mTypesToCheck.insert(TypeName); 2201 return true; 2202 } else { 2203 return false; 2204 } 2205} 2206 2207bool RSReflection::Context::addTypeNameForFieldPacker( 2208 const std::string &TypeName) { 2209 if (mFieldPackerTypes.find(TypeName) == mFieldPackerTypes.end()) { 2210 mFieldPackerTypes.insert(TypeName); 2211 return true; 2212 } else { 2213 return false; 2214 } 2215} 2216 2217} // namespace slang 2218