rsCpuScript.cpp revision 80afd424b02291e6f088cab2961537c7ba1ee2a5
1/* 2 * Copyright (C) 2011-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 "rsCpuCore.h" 18#include "rsCpuScript.h" 19#include "rsCpuExecutable.h" 20 21#ifdef RS_COMPATIBILITY_LIB 22 #include <stdio.h> 23 #include <sys/stat.h> 24 #include <unistd.h> 25#else 26 #include "rsCppUtils.h" 27 28 #include <bcc/BCCContext.h> 29 #include <bcc/Config/Config.h> 30 #include <bcc/Renderscript/RSCompilerDriver.h> 31 #include <bcinfo/MetadataExtractor.h> 32 #include <cutils/properties.h> 33 34 #include <zlib.h> 35 #include <sys/file.h> 36 #include <sys/types.h> 37 #include <unistd.h> 38 39 #include <string> 40 #include <vector> 41#endif 42 43#include <set> 44#include <string> 45#include <dlfcn.h> 46#include <stdlib.h> 47#include <string.h> 48#include <iostream> 49#include <sstream> 50 51#ifdef __LP64__ 52#define SYSLIBPATH "/system/lib64" 53#else 54#define SYSLIBPATH "/system/lib" 55#endif 56 57namespace { 58#ifndef RS_COMPATIBILITY_LIB 59 60static bool is_force_recompile() { 61#ifdef RS_SERVER 62 return false; 63#else 64 char buf[PROPERTY_VALUE_MAX]; 65 66 // Re-compile if floating point precision has been overridden. 67 property_get("debug.rs.precision", buf, ""); 68 if (buf[0] != '\0') { 69 return true; 70 } 71 72 // Re-compile if debug.rs.forcerecompile is set. 73 property_get("debug.rs.forcerecompile", buf, "0"); 74 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) { 75 return true; 76 } else { 77 return false; 78 } 79#endif // RS_SERVER 80} 81 82static void setCompileArguments(std::vector<const char*>* args, 83 const std::string& bcFileName, 84 const char* cacheDir, const char* resName, 85 const char* core_lib, bool useRSDebugContext, 86 const char* bccPluginName) { 87 rsAssert(cacheDir && resName && core_lib); 88 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH); 89 args->push_back("-unroll-runtime"); 90 args->push_back("-scalarize-load-store"); 91 args->push_back("-o"); 92 args->push_back(resName); 93 args->push_back("-output_path"); 94 args->push_back(cacheDir); 95 args->push_back("-bclib"); 96 args->push_back(core_lib); 97 args->push_back("-mtriple"); 98 args->push_back(DEFAULT_TARGET_TRIPLE_STRING); 99 100 // Enable workaround for A53 codegen by default. 101#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND) 102 args->push_back("-aarch64-fix-cortex-a53-835769"); 103#endif 104 105 // Execute the bcc compiler. 106 if (useRSDebugContext) { 107 args->push_back("-rs-debug-ctx"); 108 } else { 109 // Only load additional libraries for compiles that don't use 110 // the debug context. 111 if (bccPluginName && strlen(bccPluginName) > 0) { 112 args->push_back("-load"); 113 args->push_back(bccPluginName); 114 } 115 } 116 117 args->push_back("-fPIC"); 118 args->push_back("-embedRSInfo"); 119 120 args->push_back(bcFileName.c_str()); 121 args->push_back(nullptr); 122} 123 124static bool compileBitcode(const std::string &bcFileName, 125 const char *bitcode, 126 size_t bitcodeSize, 127 std::vector<const char *> &compileArguments) { 128 rsAssert(bitcode && bitcodeSize); 129 130 FILE *bcfile = fopen(bcFileName.c_str(), "w"); 131 if (!bcfile) { 132 ALOGE("Could not write to %s", bcFileName.c_str()); 133 return false; 134 } 135 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile); 136 fclose(bcfile); 137 if (nwritten != bitcodeSize) { 138 ALOGE("Could not write %zu bytes to %s", bitcodeSize, 139 bcFileName.c_str()); 140 return false; 141 } 142 143 return android::renderscript::rsuExecuteCommand( 144 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH, 145 compileArguments.size()-1, compileArguments.data()); 146} 147 148bool isChecksumNeeded() { 149 char buf[PROPERTY_VALUE_MAX]; 150 property_get("ro.debuggable", buf, ""); 151 return (buf[0] == '1'); 152} 153 154bool addFileToChecksum(const char *fileName, uint32_t &checksum) { 155 int FD = open(fileName, O_RDONLY); 156 if (FD == -1) { 157 ALOGE("Cannot open file \'%s\' to compute checksum", fileName); 158 return false; 159 } 160 161 char buf[256]; 162 while (true) { 163 ssize_t nread = read(FD, buf, sizeof(buf)); 164 if (nread < 0) { // bail out on failed read 165 ALOGE("Error while computing checksum for file \'%s\'", fileName); 166 return false; 167 } 168 169 checksum = adler32(checksum, (const unsigned char *) buf, nread); 170 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF 171 break; 172 } 173 174 if (TEMP_FAILURE_RETRY(close(FD)) != 0) { 175 ALOGE("Cannot close file \'%s\' after computing checksum", fileName); 176 return false; 177 } 178 return true; 179} 180 181#endif // !defined(RS_COMPATIBILITY_LIB) 182} // namespace 183 184namespace android { 185namespace renderscript { 186 187#ifndef RS_COMPATIBILITY_LIB 188 189uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize, 190 const char *commandLine, 191 const char** bccFiles, size_t numFiles) { 192 uint32_t checksum = adler32(0L, Z_NULL, 0); 193 194 // include checksum of bitcode 195 if (bitcode != nullptr && bitcodeSize > 0) { 196 checksum = adler32(checksum, bitcode, bitcodeSize); 197 } 198 199 // include checksum of command line arguments 200 checksum = adler32(checksum, (const unsigned char *) commandLine, 201 strlen(commandLine)); 202 203 // include checksum of bccFiles 204 for (size_t i = 0; i < numFiles; i++) { 205 const char* bccFile = bccFiles[i]; 206 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) { 207 // return empty checksum instead of something partial/corrupt 208 return 0; 209 } 210 } 211 212 return checksum; 213} 214 215#endif // !RS_COMPATIBILITY_LIB 216 217RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) { 218 mCtx = ctx; 219 mScript = s; 220 221 mScriptSO = nullptr; 222 223#ifndef RS_COMPATIBILITY_LIB 224 mCompilerDriver = nullptr; 225#endif 226 227 228 mRoot = nullptr; 229 mRootExpand = nullptr; 230 mInit = nullptr; 231 mFreeChildren = nullptr; 232 mScriptExec = nullptr; 233 234 mBoundAllocs = nullptr; 235 mIntrinsicData = nullptr; 236 mIsThreadable = true; 237 238 mBuildChecksum = 0; 239 mChecksumNeeded = false; 240} 241 242bool RsdCpuScriptImpl::storeRSInfoFromSO() { 243 // The shared object may have an invalid build checksum. 244 // Validate and fail early. 245 mScriptExec = ScriptExecutable::createFromSharedObject( 246 mCtx->getContext(), mScriptSO, 247 mChecksumNeeded ? mBuildChecksum : 0); 248 249 if (mScriptExec == nullptr) { 250 return false; 251 } 252 253 mRoot = (RootFunc_t) dlsym(mScriptSO, "root"); 254 if (mRoot) { 255 //ALOGE("Found root(): %p", mRoot); 256 } 257 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand"); 258 if (mRootExpand) { 259 //ALOGE("Found root.expand(): %p", mRootExpand); 260 } 261 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init"); 262 if (mInit) { 263 //ALOGE("Found init(): %p", mInit); 264 } 265 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor"); 266 if (mFreeChildren) { 267 //ALOGE("Found .rs.dtor(): %p", mFreeChildren); 268 } 269 270 size_t varCount = mScriptExec->getExportedVariableCount(); 271 if (varCount > 0) { 272 mBoundAllocs = new Allocation *[varCount]; 273 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs)); 274 } 275 276 mIsThreadable = mScriptExec->getThreadable(); 277 //ALOGE("Script isThreadable? %d", mIsThreadable); 278 279 return true; 280} 281 282bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir, 283 uint8_t const *bitcode, size_t bitcodeSize, 284 uint32_t flags, char const *bccPluginName) { 285 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, 286 // bitcode, bitcodeSize, flags, lookupFunc); 287 //ALOGE("rsdScriptInit %p %p", rsc, script); 288 289 mCtx->lockMutex(); 290#ifndef RS_COMPATIBILITY_LIB 291 bool useRSDebugContext = false; 292 293 mCompilerDriver = nullptr; 294 295 mCompilerDriver = new bcc::RSCompilerDriver(); 296 if (mCompilerDriver == nullptr) { 297 ALOGE("bcc: FAILS to create compiler driver (out of memory)"); 298 mCtx->unlockMutex(); 299 return false; 300 } 301 302 // Run any compiler setup functions we have been provided with. 303 RSSetupCompilerCallback setupCompilerCallback = 304 mCtx->getSetupCompilerCallback(); 305 if (setupCompilerCallback != nullptr) { 306 setupCompilerCallback(mCompilerDriver); 307 } 308 309 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize); 310 if (!bitcodeMetadata.extract()) { 311 ALOGE("Could not extract metadata from bitcode"); 312 mCtx->unlockMutex(); 313 return false; 314 } 315 316 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize); 317 318 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { 319 mCompilerDriver->setDebugContext(true); 320 useRSDebugContext = true; 321 } 322 323 std::string bcFileName(cacheDir); 324 bcFileName.append("/"); 325 bcFileName.append(resName); 326 bcFileName.append(".bc"); 327 328 std::vector<const char*> compileArguments; 329 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib, 330 useRSDebugContext, bccPluginName); 331 332 mChecksumNeeded = isChecksumNeeded(); 333 if (mChecksumNeeded) { 334 std::vector<const char *> bccFiles = { BCC_EXE_PATH, 335 core_lib, 336 }; 337 338 // The last argument of compileArguments is a nullptr, so remove 1 from 339 // the size. 340 std::unique_ptr<const char> compileCommandLine( 341 rsuJoinStrings(compileArguments.size()-1, compileArguments.data())); 342 343 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize, 344 compileCommandLine.get(), 345 bccFiles.data(), bccFiles.size()); 346 347 if (mBuildChecksum == 0) { 348 // cannot compute checksum but verification is enabled 349 mCtx->unlockMutex(); 350 return false; 351 } 352 } 353 else { 354 // add a dummy/constant as a checksum if verification is disabled 355 mBuildChecksum = 0xabadcafe; 356 } 357 358 // Append build checksum to commandline 359 // Handle the terminal nullptr in compileArguments 360 compileArguments.pop_back(); 361 compileArguments.push_back("-build-checksum"); 362 std::stringstream ss; 363 ss << std::hex << mBuildChecksum; 364 compileArguments.push_back(ss.str().c_str()); 365 compileArguments.push_back(nullptr); 366 367 if (!is_force_recompile() && !useRSDebugContext) { 368 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); 369 370 // Read RS info from the shared object to detect checksum mismatch 371 if (mScriptSO != nullptr && !storeRSInfoFromSO()) { 372 dlclose(mScriptSO); 373 mScriptSO = nullptr; 374 } 375 } 376 377 // If we can't, it's either not there or out of date. We compile the bit code and try loading 378 // again. 379 if (mScriptSO == nullptr) { 380 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize, 381 compileArguments)) 382 { 383 ALOGE("bcc: FAILS to compile '%s'", resName); 384 mCtx->unlockMutex(); 385 return false; 386 } 387 388 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) { 389 ALOGE("Linker: Failed to link object file '%s'", resName); 390 mCtx->unlockMutex(); 391 return false; 392 } 393 394 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); 395 if (mScriptSO == nullptr) { 396 ALOGE("Unable to load '%s'", resName); 397 mCtx->unlockMutex(); 398 return false; 399 } 400 401 // Read RS symbol information from the .so. 402 if (!storeRSInfoFromSO()) { 403 goto error; 404 } 405 } 406 407 mBitcodeFilePath.setTo(bcFileName.c_str()); 408 409#else // RS_COMPATIBILITY_LIB is defined 410 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir(); 411 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir); 412 413 if (!mScriptSO) { 414 goto error; 415 } 416 417 if (!storeRSInfoFromSO()) { 418 goto error; 419 } 420#endif 421 mCtx->unlockMutex(); 422 return true; 423 424error: 425 426 mCtx->unlockMutex(); 427 if (mScriptSO) { 428 dlclose(mScriptSO); 429 mScriptSO = nullptr; 430 } 431 return false; 432} 433 434#ifndef RS_COMPATIBILITY_LIB 435 436const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode, 437 size_t bitcodeSize) { 438 const char* defaultLib = SYSLIBPATH"/libclcore.bc"; 439 440 // If we're debugging, use the debug library. 441 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { 442 return SYSLIBPATH"/libclcore_debug.bc"; 443 } 444 445 // If a callback has been registered to specify a library, use that. 446 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback(); 447 if (selectRTCallback != nullptr) { 448 return selectRTCallback((const char*)bitcode, bitcodeSize); 449 } 450 451 // Check for a platform specific library 452#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON) 453 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision(); 454 if (prec == bcinfo::RS_FP_Relaxed) { 455 // NEON-capable ARMv7a devices can use an accelerated math library 456 // for all reduced precision scripts. 457 // ARMv8 does not use NEON, as ASIMD can be used with all precision 458 // levels. 459 return SYSLIBPATH"/libclcore_neon.bc"; 460 } else { 461 return defaultLib; 462 } 463#elif defined(__i386__) || defined(__x86_64__) 464 // x86 devices will use an optimized library. 465 return SYSLIBPATH"/libclcore_x86.bc"; 466#else 467 return defaultLib; 468#endif 469} 470 471#endif 472 473void RsdCpuScriptImpl::populateScript(Script *script) { 474 // Copy info over to runtime 475 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount(); 476 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount(); 477 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();; 478 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys(); 479 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues(); 480 481 // Bug, need to stash in metadata 482 if (mRootExpand) { 483 script->mHal.info.root = mRootExpand; 484 } else { 485 script->mHal.info.root = mRoot; 486 } 487} 488 489 490bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains, 491 uint32_t inLen, 492 Allocation * aout, 493 const void * usr, uint32_t usrLen, 494 const RsScriptCall *sc, 495 MTLaunchStruct *mtls) { 496 497 memset(mtls, 0, sizeof(MTLaunchStruct)); 498 499 for (int index = inLen; --index >= 0;) { 500 const Allocation* ain = ains[index]; 501 502 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface 503 if (ain != nullptr && 504 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) { 505 506 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 507 "rsForEach called with null in allocations"); 508 return false; 509 } 510 } 511 512 if (aout && 513 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) { 514 515 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 516 "rsForEach called with null out allocations"); 517 return false; 518 } 519 520 if (inLen > 0) { 521 const Allocation *ain0 = ains[0]; 522 const Type *inType = ain0->getType(); 523 524 mtls->fep.dim.x = inType->getDimX(); 525 mtls->fep.dim.y = inType->getDimY(); 526 mtls->fep.dim.z = inType->getDimZ(); 527 528 for (int Index = inLen; --Index >= 1;) { 529 if (!ain0->hasSameDims(ains[Index])) { 530 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 531 "Failed to launch kernel; dimensions of input and output" 532 "allocations do not match."); 533 534 return false; 535 } 536 } 537 538 } else if (aout != nullptr) { 539 const Type *outType = aout->getType(); 540 541 mtls->fep.dim.x = outType->getDimX(); 542 mtls->fep.dim.y = outType->getDimY(); 543 mtls->fep.dim.z = outType->getDimZ(); 544 545 } else if (sc != nullptr) { 546 mtls->fep.dim.x = sc->xEnd; 547 mtls->fep.dim.y = sc->yEnd; 548 mtls->fep.dim.z = 0; 549 } else { 550 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 551 "rsForEach called with null allocations"); 552 return false; 553 } 554 555 if (inLen > 0 && aout != nullptr) { 556 if (!ains[0]->hasSameDims(aout)) { 557 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 558 "Failed to launch kernel; dimensions of input and output allocations do not match."); 559 560 return false; 561 } 562 } 563 564 if (!sc || (sc->xEnd == 0)) { 565 mtls->end.x = mtls->fep.dim.x; 566 } else { 567 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart); 568 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd); 569 if (mtls->start.x >= mtls->end.x) { 570 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 571 "Failed to launch kernel; Invalid xStart or xEnd."); 572 return false; 573 } 574 } 575 576 if (!sc || (sc->yEnd == 0)) { 577 mtls->end.y = mtls->fep.dim.y; 578 } else { 579 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart); 580 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd); 581 if (mtls->start.y >= mtls->end.y) { 582 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 583 "Failed to launch kernel; Invalid yStart or yEnd."); 584 return false; 585 } 586 } 587 588 if (!sc || (sc->zEnd == 0)) { 589 mtls->end.z = mtls->fep.dim.z; 590 } else { 591 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart); 592 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd); 593 if (mtls->start.z >= mtls->end.z) { 594 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 595 "Failed to launch kernel; Invalid zStart or zEnd."); 596 return false; 597 } 598 } 599 600 if (!sc || (sc->arrayEnd == 0)) { 601 mtls->end.array[0] = mtls->fep.dim.array[0]; 602 } else { 603 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart); 604 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd); 605 if (mtls->start.array[0] >= mtls->end.array[0]) { 606 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 607 "Failed to launch kernel; Invalid arrayStart or arrayEnd."); 608 return false; 609 } 610 } 611 612 if (!sc || (sc->array2End == 0)) { 613 mtls->end.array[1] = mtls->fep.dim.array[1]; 614 } else { 615 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start); 616 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End); 617 if (mtls->start.array[1] >= mtls->end.array[1]) { 618 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 619 "Failed to launch kernel; Invalid array2Start or array2End."); 620 return false; 621 } 622 } 623 624 if (!sc || (sc->array3End == 0)) { 625 mtls->end.array[2] = mtls->fep.dim.array[2]; 626 } else { 627 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start); 628 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End); 629 if (mtls->start.array[2] >= mtls->end.array[2]) { 630 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 631 "Failed to launch kernel; Invalid array3Start or array3End."); 632 return false; 633 } 634 } 635 636 if (!sc || (sc->array4End == 0)) { 637 mtls->end.array[3] = mtls->fep.dim.array[3]; 638 } else { 639 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start); 640 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End); 641 if (mtls->start.array[3] >= mtls->end.array[3]) { 642 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, 643 "Failed to launch kernel; Invalid array4Start or array4End."); 644 return false; 645 } 646 } 647 648 649 // The X & Y walkers always want 0-1 min even if dim is not present 650 mtls->end.x = rsMax((uint32_t)1, mtls->end.x); 651 mtls->end.y = rsMax((uint32_t)1, mtls->end.y); 652 653 mtls->rsc = mCtx; 654 if (ains) { 655 memcpy(mtls->ains, ains, inLen * sizeof(ains[0])); 656 } 657 mtls->aout[0] = aout; 658 mtls->fep.usr = usr; 659 mtls->fep.usrLen = usrLen; 660 mtls->mSliceSize = 1; 661 mtls->mSliceNum = 0; 662 663 mtls->isThreadable = mIsThreadable; 664 665 if (inLen > 0) { 666 mtls->fep.inLen = inLen; 667 for (int index = inLen; --index >= 0;) { 668 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr; 669 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes(); 670 } 671 } 672 673 if (aout != nullptr) { 674 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr; 675 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes(); 676 } 677 678 // All validation passed, ok to launch threads 679 return true; 680} 681 682 683void RsdCpuScriptImpl::invokeForEach(uint32_t slot, 684 const Allocation ** ains, 685 uint32_t inLen, 686 Allocation * aout, 687 const void * usr, 688 uint32_t usrLen, 689 const RsScriptCall *sc) { 690 691 MTLaunchStruct mtls; 692 693 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) { 694 forEachKernelSetup(slot, &mtls); 695 696 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); 697 mCtx->launchThreads(ains, inLen, aout, sc, &mtls); 698 mCtx->setTLS(oldTLS); 699 } 700} 701 702void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) { 703 mtls->script = this; 704 mtls->fep.slot = slot; 705 mtls->kernel = mScriptExec->getForEachFunction(slot); 706 rsAssert(mtls->kernel != nullptr); 707 mtls->sig = mScriptExec->getForEachSignature(slot); 708} 709 710int RsdCpuScriptImpl::invokeRoot() { 711 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); 712 int ret = mRoot(); 713 mCtx->setTLS(oldTLS); 714 return ret; 715} 716 717void RsdCpuScriptImpl::invokeInit() { 718 if (mInit) { 719 mInit(); 720 } 721} 722 723void RsdCpuScriptImpl::invokeFreeChildren() { 724 if (mFreeChildren) { 725 mFreeChildren(); 726 } 727} 728 729void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params, 730 size_t paramLength) { 731 //ALOGE("invoke %i %p %zu", slot, params, paramLength); 732 void * ap = nullptr; 733 734#if defined(__x86_64__) 735 // The invoked function could have input parameter of vector type for example float4 which 736 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform. 737 // So try to align void* params before passing them into RS exported function. 738 739 if ((uint8_t)(uint64_t)params & 0x0F) { 740 if ((ap = (void*)memalign(16, paramLength)) != nullptr) { 741 memcpy(ap, params, paramLength); 742 } else { 743 ALOGE("x86_64: invokeFunction memalign error, still use params which" 744 " is not 16 bytes aligned."); 745 } 746 } 747#endif 748 749 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); 750 reinterpret_cast<void (*)(const void *, uint32_t)>( 751 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength); 752 753 mCtx->setTLS(oldTLS); 754} 755 756void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { 757 //rsAssert(!script->mFieldIsObject[slot]); 758 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength); 759 760 //if (mIntrinsicID) { 761 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength); 762 //return; 763 //} 764 765 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); 766 if (!destPtr) { 767 //ALOGV("Calling setVar on slot = %i which is null", slot); 768 return; 769 } 770 771 memcpy(destPtr, data, dataLength); 772} 773 774void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) { 775 //rsAssert(!script->mFieldIsObject[slot]); 776 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength); 777 778 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); 779 if (!srcPtr) { 780 //ALOGV("Calling setVar on slot = %i which is null", slot); 781 return; 782 } 783 memcpy(data, srcPtr, dataLength); 784} 785 786 787void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, 788 const Element *elem, 789 const uint32_t *dims, size_t dimLength) { 790 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); 791 if (!destPtr) { 792 //ALOGV("Calling setVar on slot = %i which is null", slot); 793 return; 794 } 795 796 // We want to look at dimension in terms of integer components, 797 // but dimLength is given in terms of bytes. 798 dimLength /= sizeof(int); 799 800 // Only a single dimension is currently supported. 801 rsAssert(dimLength == 1); 802 if (dimLength == 1) { 803 // First do the increment loop. 804 size_t stride = elem->getSizeBytes(); 805 const char *cVal = reinterpret_cast<const char *>(data); 806 for (uint32_t i = 0; i < dims[0]; i++) { 807 elem->incRefs(cVal); 808 cVal += stride; 809 } 810 811 // Decrement loop comes after (to prevent race conditions). 812 char *oldVal = reinterpret_cast<char *>(destPtr); 813 for (uint32_t i = 0; i < dims[0]; i++) { 814 elem->decRefs(oldVal); 815 oldVal += stride; 816 } 817 } 818 819 memcpy(destPtr, data, dataLength); 820} 821 822void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) { 823 824 //rsAssert(!script->mFieldIsObject[slot]); 825 //ALOGE("setGlobalBind %i %p", slot, data); 826 827 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); 828 if (!destPtr) { 829 //ALOGV("Calling setVar on slot = %i which is null", slot); 830 return; 831 } 832 833 void *ptr = nullptr; 834 mBoundAllocs[slot] = data; 835 if (data) { 836 ptr = data->mHal.drvState.lod[0].mallocPtr; 837 } 838 memcpy(destPtr, &ptr, sizeof(void *)); 839} 840 841void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) { 842 843 //rsAssert(script->mFieldIsObject[slot]); 844 //ALOGE("setGlobalObj %i %p", slot, data); 845 846 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); 847 if (!destPtr) { 848 //ALOGV("Calling setVar on slot = %i which is null", slot); 849 return; 850 } 851 852 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data); 853} 854 855const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const { 856 return mScriptExec->getFieldName(slot); 857} 858 859RsdCpuScriptImpl::~RsdCpuScriptImpl() { 860#ifndef RS_COMPATIBILITY_LIB 861 if (mCompilerDriver) { 862 delete mCompilerDriver; 863 } 864#endif 865 866 if (mScriptExec != nullptr) { 867 delete mScriptExec; 868 } 869 if (mBoundAllocs) delete[] mBoundAllocs; 870 if (mScriptSO) { 871 dlclose(mScriptSO); 872 } 873} 874 875Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const { 876 if (!ptr) { 877 return nullptr; 878 } 879 880 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) { 881 Allocation *a = mBoundAllocs[ct]; 882 if (!a) continue; 883 if (a->mHal.drvState.lod[0].mallocPtr == ptr) { 884 return a; 885 } 886 } 887 ALOGE("rsGetAllocation, failed to find %p", ptr); 888 return nullptr; 889} 890 891void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains, 892 uint32_t inLen, Allocation * aout, 893 const void * usr, uint32_t usrLen, 894 const RsScriptCall *sc) {} 895 896void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains, 897 uint32_t inLen, Allocation * aout, 898 const void * usr, uint32_t usrLen, 899 const RsScriptCall *sc) {} 900 901 902} 903} 904