rsCpuScript.cpp revision bee48d79ba974e3dfbb782f9cce5c8d554f488e7
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 20#ifdef RS_COMPATIBILITY_LIB 21 #include <set> 22 #include <string> 23 #include <dlfcn.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <sys/stat.h> 28 #include <unistd.h> 29#else 30 #include <bcc/BCCContext.h> 31 #include <bcc/Config/Config.h> 32 #include <bcc/Renderscript/RSCompilerDriver.h> 33 #include <bcc/Renderscript/RSExecutable.h> 34 #include <bcc/Renderscript/RSInfo.h> 35 #include <bcinfo/MetadataExtractor.h> 36 #include <cutils/properties.h> 37 38 #include <sys/types.h> 39 #include <sys/wait.h> 40 #include <unistd.h> 41 42 #include <string> 43 #include <vector> 44#endif 45 46namespace { 47#ifdef RS_COMPATIBILITY_LIB 48 49// Create a len length string containing random characters from [A-Za-z0-9]. 50static std::string getRandomString(size_t len) { 51 char buf[len + 1]; 52 for (size_t i = 0; i < len; i++) { 53 uint32_t r = arc4random() & 0xffff; 54 r %= 62; 55 if (r < 26) { 56 // lowercase 57 buf[i] = 'a' + r; 58 } else if (r < 52) { 59 // uppercase 60 buf[i] = 'A' + (r - 26); 61 } else { 62 // Use a number 63 buf[i] = '0' + (r - 52); 64 } 65 } 66 buf[len] = '\0'; 67 return std::string(buf); 68} 69 70// Check if a path exists and attempt to create it if it doesn't. 71static bool ensureCacheDirExists(const char *path) { 72 if (access(path, R_OK | W_OK | X_OK) == 0) { 73 // Done if we can rwx the directory 74 return true; 75 } 76 if (mkdir(path, 0700) == 0) { 77 return true; 78 } 79 return false; 80} 81 82// Attempt to load the shared library from origName, but then fall back to 83// creating the symlinked shared library if necessary (to ensure instancing). 84// This function returns the dlopen()-ed handle if successful. 85static void *loadSOHelper(const char *origName, const char *cacheDir, 86 const char *resName) { 87 // Keep track of which .so libraries have been loaded. Once a library is 88 // in the set (per-process granularity), we must instead make a symlink to 89 // the original shared object (randomly named .so file) and load that one 90 // instead. If we don't do this, we end up aliasing global data between 91 // the various Script instances (which are supposed to be completely 92 // independent). 93 static std::set<std::string> LoadedLibraries; 94 95 void *loaded = NULL; 96 97 // Skip everything if we don't even have the original library available. 98 if (access(origName, F_OK) != 0) { 99 return NULL; 100 } 101 102 // Common path is that we have not loaded this Script/library before. 103 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) { 104 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL); 105 if (loaded) { 106 LoadedLibraries.insert(origName); 107 } 108 return loaded; 109 } 110 111 std::string newName(cacheDir); 112 newName.append("/com.android.renderscript.cache/"); 113 114 if (!ensureCacheDirExists(newName.c_str())) { 115 ALOGE("Could not verify or create cache dir: %s", cacheDir); 116 return NULL; 117 } 118 119 // Construct an appropriately randomized filename for the symlink. 120 newName.append("librs."); 121 newName.append(resName); 122 newName.append("#"); 123 newName.append(getRandomString(6)); // 62^6 potential filename variants. 124 newName.append(".so"); 125 126 int r = symlink(origName, newName.c_str()); 127 if (r != 0) { 128 ALOGE("Could not create symlink %s -> %s", newName.c_str(), origName); 129 return NULL; 130 } 131 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL); 132 r = unlink(newName.c_str()); 133 if (r != 0) { 134 ALOGE("Could not unlink symlink %s", newName.c_str()); 135 } 136 if (loaded) { 137 LoadedLibraries.insert(newName.c_str()); 138 } 139 140 return loaded; 141} 142 143// Load the shared library referred to by cacheDir and resName. If we have 144// already loaded this library, we instead create a new symlink (in the 145// cache dir) and then load that. We then immediately destroy the symlink. 146// This is required behavior to implement script instancing for the support 147// library, since shared objects are loaded and de-duped by name only. 148static void *loadSharedLibrary(const char *cacheDir, const char *resName) { 149 void *loaded = NULL; 150 //arc4random_stir(); 151#ifndef RS_SERVER 152 std::string scriptSOName(cacheDir); 153 size_t cutPos = scriptSOName.rfind("cache"); 154 if (cutPos != std::string::npos) { 155 scriptSOName.erase(cutPos); 156 } else { 157 ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir); 158 } 159 scriptSOName.append("/lib/librs."); 160#else 161 std::string scriptSOName("lib"); 162#endif 163 scriptSOName.append(resName); 164 scriptSOName.append(".so"); 165 166 // We should check if we can load the library from the standard app 167 // location for shared libraries first. 168 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName); 169 170 if (loaded == NULL) { 171 ALOGE("Unable to open shared library (%s): %s", 172 scriptSOName.c_str(), dlerror()); 173 174 // One final attempt to find the library in "/system/lib". 175 // We do this to allow bundled applications to use the compatibility 176 // library fallback path. Those applications don't have a private 177 // library path, so they need to install to the system directly. 178 // Note that this is really just a testing path. 179 android::String8 scriptSONameSystem("/system/lib/librs."); 180 scriptSONameSystem.append(resName); 181 scriptSONameSystem.append(".so"); 182 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir, 183 resName); 184 if (loaded == NULL) { 185 ALOGE("Unable to open system shared library (%s): %s", 186 scriptSONameSystem.c_str(), dlerror()); 187 } 188 } 189 190 return loaded; 191} 192 193 194#else // RS_COMPATIBILITY_LIB is defined 195static bool is_force_recompile() { 196#ifdef RS_SERVER 197 return false; 198#else 199 char buf[PROPERTY_VALUE_MAX]; 200 201 // Re-compile if floating point precision has been overridden. 202 property_get("debug.rs.precision", buf, ""); 203 if (buf[0] != '\0') { 204 return true; 205 } 206 207 // Re-compile if debug.rs.forcerecompile is set. 208 property_get("debug.rs.forcerecompile", buf, "0"); 209 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) { 210 return true; 211 } else { 212 return false; 213 } 214#endif // RS_SERVER 215} 216 217const static char *BCC_EXE_PATH = "/system/bin/bcc"; 218 219static bool compileBitcode(const char *cacheDir, 220 const char *resName, 221 const char *bitcode, 222 size_t bitcodeSize, 223 const char *core_lib, 224 bool useRSDebugContext, 225 const char *bccPluginName) { 226 rsAssert(cacheDir && resName && bitcode && bitcodeSize && core_lib); 227 228 android::String8 bcFilename(cacheDir); 229 bcFilename.append("/"); 230 bcFilename.append(resName); 231 bcFilename.append(".bc"); 232 FILE *bcfile = fopen(bcFilename.string(), "w"); 233 if (!bcfile) { 234 ALOGE("Could not write to %s", bcFilename.string()); 235 return false; 236 } 237 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile); 238 fclose(bcfile); 239 if (nwritten != bitcodeSize) { 240 ALOGE("Could not write %zu bytes to %s", bitcodeSize, 241 bcFilename.string()); 242 return false; 243 } 244 245 pid_t pid = fork(); 246 247 switch (pid) { 248 case -1: { // Error occurred (we attempt no recovery) 249 ALOGE("Couldn't fork for bcc compiler execution"); 250 return false; 251 } 252 case 0: { // Child process 253 std::vector<std::string> args; 254 args.push_back(BCC_EXE_PATH); 255 args.push_back("-o"); 256 args.push_back(resName); 257 args.push_back("-output_path"); 258 args.push_back(cacheDir); 259 args.push_back("-bclib"); 260 args.push_back(core_lib); 261 args.push_back("-mtriple"); 262 args.push_back(DEFAULT_TARGET_TRIPLE_STRING); 263 264 // Execute the bcc compiler. 265 if (useRSDebugContext) { 266 args.push_back("-rs-debug-ctx"); 267 } else { 268 // Only load additional libraries for compiles that don't use 269 // the debug context. 270 if (bccPluginName && strlen(bccPluginName) > 0) { 271 args.push_back("-load"); 272 args.push_back(bccPluginName); 273 } 274 } 275 276 args.push_back(bcFilename.string()); 277 278 const char **cargs = new const char *[args.size() + 1]; 279 for (uint32_t i = 0; i < args.size(); i++) { 280 cargs[i] = args[i].c_str(); 281 } 282 cargs[args.size()] = NULL; 283 284 execv(BCC_EXE_PATH, (char *const *)cargs); 285 286 delete [] cargs; 287 ALOGE("execv() failed: %s", strerror(errno)); 288 abort(); 289 return false; 290 } 291 default: { // Parent process (actual driver) 292 // Wait on child process to finish compiling the source. 293 int status = 0; 294 pid_t w = waitpid(pid, &status, 0); 295 if (w == -1) { 296 ALOGE("Could not wait for bcc compiler"); 297 return false; 298 } 299 300 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 301 return true; 302 } 303 304 ALOGE("bcc compiler terminated unexpectedly"); 305 return false; 306 } 307 } 308} 309 310#endif // !defined(RS_COMPATIBILITY_LIB) 311} // namespace 312 313namespace android { 314namespace renderscript { 315 316#ifdef RS_COMPATIBILITY_LIB 317#define MAXLINE 500 318#define MAKE_STR_HELPER(S) #S 319#define MAKE_STR(S) MAKE_STR_HELPER(S) 320#define EXPORT_VAR_STR "exportVarCount: " 321#define EXPORT_VAR_STR_LEN strlen(EXPORT_VAR_STR) 322#define EXPORT_FUNC_STR "exportFuncCount: " 323#define EXPORT_FUNC_STR_LEN strlen(EXPORT_FUNC_STR) 324#define EXPORT_FOREACH_STR "exportForEachCount: " 325#define EXPORT_FOREACH_STR_LEN strlen(EXPORT_FOREACH_STR) 326#define OBJECT_SLOT_STR "objectSlotCount: " 327#define OBJECT_SLOT_STR_LEN strlen(OBJECT_SLOT_STR) 328 329// Copy up to a newline or size chars from str -> s, updating str 330// Returns s when successful and NULL when '\0' is finally reached. 331static char* strgets(char *s, int size, const char **ppstr) { 332 if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) { 333 return NULL; 334 } 335 336 int i; 337 for (i = 0; i < (size - 1); i++) { 338 s[i] = **ppstr; 339 (*ppstr)++; 340 if (s[i] == '\0') { 341 return s; 342 } else if (s[i] == '\n') { 343 s[i+1] = '\0'; 344 return s; 345 } 346 } 347 348 // size has been exceeded. 349 s[i] = '\0'; 350 351 return s; 352} 353#endif 354 355RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) { 356 mCtx = ctx; 357 mScript = s; 358 359#ifdef RS_COMPATIBILITY_LIB 360 mScriptSO = NULL; 361 mInvokeFunctions = NULL; 362 mForEachFunctions = NULL; 363 mFieldAddress = NULL; 364 mFieldIsObject = NULL; 365 mForEachSignatures = NULL; 366#else 367 mCompilerContext = NULL; 368 mCompilerDriver = NULL; 369 mExecutable = NULL; 370#endif 371 372 373 mRoot = NULL; 374 mRootExpand = NULL; 375 mInit = NULL; 376 mFreeChildren = NULL; 377 378 379 mBoundAllocs = NULL; 380 mIntrinsicData = NULL; 381 mIsThreadable = true; 382} 383 384 385bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir, 386 uint8_t const *bitcode, size_t bitcodeSize, 387 uint32_t flags, char const *bccPluginName) { 388 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc); 389 //ALOGE("rsdScriptInit %p %p", rsc, script); 390 391 mCtx->lockMutex(); 392#ifndef RS_COMPATIBILITY_LIB 393 bcc::RSExecutable *exec = NULL; 394 bool useRSDebugContext = false; 395 396 mCompilerContext = NULL; 397 mCompilerDriver = NULL; 398 mExecutable = NULL; 399 400 mCompilerContext = new bcc::BCCContext(); 401 if (mCompilerContext == NULL) { 402 ALOGE("bcc: FAILS to create compiler context (out of memory)"); 403 mCtx->unlockMutex(); 404 return false; 405 } 406 407 mCompilerDriver = new bcc::RSCompilerDriver(); 408 if (mCompilerDriver == NULL) { 409 ALOGE("bcc: FAILS to create compiler driver (out of memory)"); 410 mCtx->unlockMutex(); 411 return false; 412 } 413 414 // Configure symbol resolvers (via compiler-rt and the RS runtime). 415 mRSRuntime.setLookupFunction(lookupRuntimeStub); 416 mRSRuntime.setContext(this); 417 mResolver.chainResolver(mCompilerRuntime); 418 mResolver.chainResolver(mRSRuntime); 419 420 // Run any compiler setup functions we have been provided with. 421 RSSetupCompilerCallback setupCompilerCallback = 422 mCtx->getSetupCompilerCallback(); 423 if (setupCompilerCallback != NULL) { 424 setupCompilerCallback(mCompilerDriver); 425 } 426 427 const char *core_lib = bcc::RSInfo::LibCLCorePath; 428 429 bcinfo::MetadataExtractor ME((const char *) bitcode, bitcodeSize); 430 if (!ME.extract()) { 431 ALOGE("Could not extract metadata from bitcode"); 432 return false; 433 } 434 435 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision(); 436 switch (prec) { 437 case bcinfo::RS_FP_Imprecise: 438 case bcinfo::RS_FP_Relaxed: 439#if defined(ARCH_ARM_HAVE_NEON) && !defined(ARCH_ARM64_HAVE_NEON) 440 // NEON-capable ARMv7a devices can use an accelerated math library 441 // for all reduced precision scripts. 442 // ARMv8 does not use NEON, as ASIMD can be used with all precision 443 // levels. 444 core_lib = bcc::RSInfo::LibCLCoreNEONPath; 445#endif 446 break; 447 case bcinfo::RS_FP_Full: 448 break; 449 default: 450 ALOGE("Unknown precision for bitcode"); 451 return false; 452 } 453 454#if defined(__i386__) 455 // x86 devices will use an optimized library. 456 core_lib = bcc::RSInfo::LibCLCoreX86Path; 457#endif 458 459 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback(); 460 if (selectRTCallback != NULL) { 461 core_lib = selectRTCallback((const char *)bitcode, bitcodeSize); 462 } 463 464 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { 465 // Use the libclcore_debug.bc instead of the default library. 466 core_lib = bcc::RSInfo::LibCLCoreDebugPath; 467 mCompilerDriver->setDebugContext(true); 468 useRSDebugContext = true; 469 // Skip the cache lookup 470 } else if (!is_force_recompile()) { 471 // New cache infrastructure goes here 472 473 } 474 475 if (exec == NULL) { 476 bool built = compileBitcode(cacheDir, resName, (const char *)bitcode, 477 bitcodeSize, core_lib, useRSDebugContext, 478 bccPluginName); 479 if (built) { 480 exec = bcc::RSCompilerDriver::loadScript(cacheDir, resName, 481 (const char *)bitcode, bitcodeSize, mResolver); 482 } 483 } 484 485 if (exec == NULL) { 486 ALOGE("bcc: FAILS to prepare executable for '%s'", resName); 487 mCtx->unlockMutex(); 488 return false; 489 } 490 491 mExecutable = exec; 492 493 exec->setThreadable(mIsThreadable); 494 if (!exec->syncInfo()) { 495 ALOGW("bcc: FAILS to synchronize the RS info file to the disk"); 496 } 497 498 mRoot = reinterpret_cast<int (*)()>(exec->getSymbolAddress("root")); 499 mRootExpand = 500 reinterpret_cast<int (*)()>(exec->getSymbolAddress("root.expand")); 501 mInit = reinterpret_cast<void (*)()>(exec->getSymbolAddress("init")); 502 mFreeChildren = 503 reinterpret_cast<void (*)()>(exec->getSymbolAddress(".rs.dtor")); 504 505 506 if (ME.getExportVarCount()) { 507 mBoundAllocs = new Allocation *[ME.getExportVarCount()]; 508 memset(mBoundAllocs, 0, sizeof(void *) * ME.getExportVarCount()); 509 } 510 511 for (size_t i = 0; i < ME.getExportForEachSignatureCount(); i++) { 512 char* name = new char[strlen(ME.getExportForEachNameList()[i]) + 1]; 513 mExportedForEachFuncList.push_back(std::make_pair(name, 514 ME.getExportForEachSignatureList()[i])); 515 } 516 517#else // RS_COMPATIBILITY_LIB is defined 518 519 mScriptSO = loadSharedLibrary(cacheDir, resName); 520 521 if (mScriptSO) { 522 char line[MAXLINE]; 523 mRoot = (RootFunc_t) dlsym(mScriptSO, "root"); 524 if (mRoot) { 525 //ALOGE("Found root(): %p", mRoot); 526 } 527 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand"); 528 if (mRootExpand) { 529 //ALOGE("Found root.expand(): %p", mRootExpand); 530 } 531 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init"); 532 if (mInit) { 533 //ALOGE("Found init(): %p", mInit); 534 } 535 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor"); 536 if (mFreeChildren) { 537 //ALOGE("Found .rs.dtor(): %p", mFreeChildren); 538 } 539 540 const char *rsInfo = (const char *) dlsym(mScriptSO, ".rs.info"); 541 if (rsInfo) { 542 //ALOGE("Found .rs.info(): %p - %s", rsInfo, rsInfo); 543 } 544 545 size_t varCount = 0; 546 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 547 goto error; 548 } 549 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) { 550 ALOGE("Invalid export var count!: %s", line); 551 goto error; 552 } 553 554 mExportedVariableCount = varCount; 555 //ALOGE("varCount: %zu", varCount); 556 if (varCount > 0) { 557 // Start by creating/zeroing this member, since we don't want to 558 // accidentally clean up invalid pointers later (if we error out). 559 mFieldIsObject = new bool[varCount]; 560 if (mFieldIsObject == NULL) { 561 goto error; 562 } 563 memset(mFieldIsObject, 0, varCount * sizeof(*mFieldIsObject)); 564 mFieldAddress = new void*[varCount]; 565 if (mFieldAddress == NULL) { 566 goto error; 567 } 568 for (size_t i = 0; i < varCount; ++i) { 569 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 570 goto error; 571 } 572 char *c = strrchr(line, '\n'); 573 if (c) { 574 *c = '\0'; 575 } 576 mFieldAddress[i] = dlsym(mScriptSO, line); 577 if (mFieldAddress[i] == NULL) { 578 ALOGE("Failed to find variable address for %s: %s", 579 line, dlerror()); 580 // Not a critical error if we don't find a global variable. 581 } 582 else { 583 //ALOGE("Found variable %s at %p", line, 584 //mFieldAddress[i]); 585 } 586 } 587 } 588 589 size_t funcCount = 0; 590 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 591 goto error; 592 } 593 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) { 594 ALOGE("Invalid export func count!: %s", line); 595 goto error; 596 } 597 598 mExportedFunctionCount = funcCount; 599 //ALOGE("funcCount: %zu", funcCount); 600 601 if (funcCount > 0) { 602 mInvokeFunctions = new InvokeFunc_t[funcCount]; 603 if (mInvokeFunctions == NULL) { 604 goto error; 605 } 606 for (size_t i = 0; i < funcCount; ++i) { 607 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 608 goto error; 609 } 610 char *c = strrchr(line, '\n'); 611 if (c) { 612 *c = '\0'; 613 } 614 615 mInvokeFunctions[i] = (InvokeFunc_t) dlsym(mScriptSO, line); 616 if (mInvokeFunctions[i] == NULL) { 617 ALOGE("Failed to get function address for %s(): %s", 618 line, dlerror()); 619 goto error; 620 } 621 else { 622 //ALOGE("Found InvokeFunc_t %s at %p", line, mInvokeFunctions[i]); 623 } 624 } 625 } 626 627 size_t forEachCount = 0; 628 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 629 goto error; 630 } 631 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) { 632 ALOGE("Invalid export forEach count!: %s", line); 633 goto error; 634 } 635 636 if (forEachCount > 0) { 637 638 mForEachSignatures = new uint32_t[forEachCount]; 639 if (mForEachSignatures == NULL) { 640 goto error; 641 } 642 mForEachFunctions = new ForEachFunc_t[forEachCount]; 643 if (mForEachFunctions == NULL) { 644 goto error; 645 } 646 for (size_t i = 0; i < forEachCount; ++i) { 647 unsigned int tmpSig = 0; 648 char tmpName[MAXLINE]; 649 650 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 651 goto error; 652 } 653 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s", 654 &tmpSig, tmpName) != 2) { 655 ALOGE("Invalid export forEach!: %s", line); 656 goto error; 657 } 658 659 // Lookup the expanded ForEach kernel. 660 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName)); 661 mForEachSignatures[i] = tmpSig; 662 mForEachFunctions[i] = 663 (ForEachFunc_t) dlsym(mScriptSO, tmpName); 664 if (i != 0 && mForEachFunctions[i] == NULL) { 665 // Ignore missing root.expand functions. 666 // root() is always specified at location 0. 667 ALOGE("Failed to find forEach function address for %s: %s", 668 tmpName, dlerror()); 669 goto error; 670 } 671 else { 672 //ALOGE("Found forEach %s at %p", tmpName, mForEachFunctions[i]); 673 } 674 } 675 } 676 677 size_t objectSlotCount = 0; 678 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 679 goto error; 680 } 681 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) { 682 ALOGE("Invalid object slot count!: %s", line); 683 goto error; 684 } 685 686 if (objectSlotCount > 0) { 687 rsAssert(varCount > 0); 688 for (size_t i = 0; i < objectSlotCount; ++i) { 689 uint32_t varNum = 0; 690 if (strgets(line, MAXLINE, &rsInfo) == NULL) { 691 goto error; 692 } 693 if (sscanf(line, "%u", &varNum) != 1) { 694 ALOGE("Invalid object slot!: %s", line); 695 goto error; 696 } 697 698 if (varNum < varCount) { 699 mFieldIsObject[varNum] = true; 700 } 701 } 702 } 703 704 if (varCount > 0) { 705 mBoundAllocs = new Allocation *[varCount]; 706 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs)); 707 } 708 709 if (mScriptSO == (void*)1) { 710 //rsdLookupRuntimeStub(script, "acos"); 711 } 712 } else { 713 goto error; 714 } 715#endif 716 mCtx->unlockMutex(); 717 return true; 718 719#ifdef RS_COMPATIBILITY_LIB 720error: 721 722 mCtx->unlockMutex(); 723 delete[] mInvokeFunctions; 724 delete[] mForEachFunctions; 725 delete[] mFieldAddress; 726 delete[] mFieldIsObject; 727 delete[] mForEachSignatures; 728 delete[] mBoundAllocs; 729 if (mScriptSO) { 730 dlclose(mScriptSO); 731 } 732 return false; 733#endif 734} 735 736void RsdCpuScriptImpl::populateScript(Script *script) { 737#ifndef RS_COMPATIBILITY_LIB 738 // Copy info over to runtime 739 script->mHal.info.exportedFunctionCount = mExecutable->getExportFuncAddrs().size(); 740 script->mHal.info.exportedVariableCount = mExecutable->getExportVarAddrs().size(); 741 script->mHal.info.exportedForeachFuncList = &mExportedForEachFuncList[0]; 742 script->mHal.info.exportedPragmaCount = mExecutable->getPragmaKeys().size(); 743 script->mHal.info.exportedPragmaKeyList = 744 const_cast<const char**>(mExecutable->getPragmaKeys().array()); 745 script->mHal.info.exportedPragmaValueList = 746 const_cast<const char**>(mExecutable->getPragmaValues().array()); 747 748 if (mRootExpand) { 749 script->mHal.info.root = mRootExpand; 750 } else { 751 script->mHal.info.root = mRoot; 752 } 753#else 754 // Copy info over to runtime 755 script->mHal.info.exportedFunctionCount = mExportedFunctionCount; 756 script->mHal.info.exportedVariableCount = mExportedVariableCount; 757 script->mHal.info.exportedPragmaCount = 0; 758 script->mHal.info.exportedPragmaKeyList = 0; 759 script->mHal.info.exportedPragmaValueList = 0; 760 761 // Bug, need to stash in metadata 762 if (mRootExpand) { 763 script->mHal.info.root = mRootExpand; 764 } else { 765 script->mHal.info.root = mRoot; 766 } 767#endif 768} 769 770 771typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); 772 773void RsdCpuScriptImpl::forEachMtlsSetup(const Allocation * ain, Allocation * aout, 774 const void * usr, uint32_t usrLen, 775 const RsScriptCall *sc, 776 MTLaunchStruct *mtls) { 777 778 memset(mtls, 0, sizeof(MTLaunchStruct)); 779 780 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface 781 if (ain && (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == NULL) { 782 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null in allocations"); 783 return; 784 } 785 if (aout && (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == NULL) { 786 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null out allocations"); 787 return; 788 } 789 790 if (ain) { 791 mtls->fep.dimX = ain->getType()->getDimX(); 792 mtls->fep.dimY = ain->getType()->getDimY(); 793 mtls->fep.dimZ = ain->getType()->getDimZ(); 794 //mtls->dimArray = ain->getType()->getDimArray(); 795 } else if (aout) { 796 mtls->fep.dimX = aout->getType()->getDimX(); 797 mtls->fep.dimY = aout->getType()->getDimY(); 798 mtls->fep.dimZ = aout->getType()->getDimZ(); 799 //mtls->dimArray = aout->getType()->getDimArray(); 800 } else { 801 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); 802 return; 803 } 804 805 if (!sc || (sc->xEnd == 0)) { 806 mtls->xEnd = mtls->fep.dimX; 807 } else { 808 rsAssert(sc->xStart < mtls->fep.dimX); 809 rsAssert(sc->xEnd <= mtls->fep.dimX); 810 rsAssert(sc->xStart < sc->xEnd); 811 mtls->xStart = rsMin(mtls->fep.dimX, sc->xStart); 812 mtls->xEnd = rsMin(mtls->fep.dimX, sc->xEnd); 813 if (mtls->xStart >= mtls->xEnd) return; 814 } 815 816 if (!sc || (sc->yEnd == 0)) { 817 mtls->yEnd = mtls->fep.dimY; 818 } else { 819 rsAssert(sc->yStart < mtls->fep.dimY); 820 rsAssert(sc->yEnd <= mtls->fep.dimY); 821 rsAssert(sc->yStart < sc->yEnd); 822 mtls->yStart = rsMin(mtls->fep.dimY, sc->yStart); 823 mtls->yEnd = rsMin(mtls->fep.dimY, sc->yEnd); 824 if (mtls->yStart >= mtls->yEnd) return; 825 } 826 827 if (!sc || (sc->zEnd == 0)) { 828 mtls->zEnd = mtls->fep.dimZ; 829 } else { 830 rsAssert(sc->zStart < mtls->fep.dimZ); 831 rsAssert(sc->zEnd <= mtls->fep.dimZ); 832 rsAssert(sc->zStart < sc->zEnd); 833 mtls->zStart = rsMin(mtls->fep.dimZ, sc->zStart); 834 mtls->zEnd = rsMin(mtls->fep.dimZ, sc->zEnd); 835 if (mtls->zStart >= mtls->zEnd) return; 836 } 837 838 mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd); 839 mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd); 840 mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd); 841 mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd); 842 843 rsAssert(!ain || (ain->getType()->getDimZ() == 0)); 844 845 mtls->rsc = mCtx; 846 mtls->ain = ain; 847 mtls->aout = aout; 848 mtls->fep.usr = usr; 849 mtls->fep.usrLen = usrLen; 850 mtls->mSliceSize = 1; 851 mtls->mSliceNum = 0; 852 853 mtls->fep.ptrIn = NULL; 854 mtls->fep.eStrideIn = 0; 855 mtls->isThreadable = mIsThreadable; 856 857 if (ain) { 858 mtls->fep.ptrIn = (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr; 859 mtls->fep.eStrideIn = ain->getType()->getElementSizeBytes(); 860 mtls->fep.yStrideIn = ain->mHal.drvState.lod[0].stride; 861 } 862 863 mtls->fep.ptrOut = NULL; 864 mtls->fep.eStrideOut = 0; 865 if (aout) { 866 mtls->fep.ptrOut = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr; 867 mtls->fep.eStrideOut = aout->getType()->getElementSizeBytes(); 868 mtls->fep.yStrideOut = aout->mHal.drvState.lod[0].stride; 869 } 870} 871 872 873void RsdCpuScriptImpl::invokeForEach(uint32_t slot, 874 const Allocation * ain, 875 Allocation * aout, 876 const void * usr, 877 uint32_t usrLen, 878 const RsScriptCall *sc) { 879 880 MTLaunchStruct mtls; 881 forEachMtlsSetup(ain, aout, usr, usrLen, sc, &mtls); 882 forEachKernelSetup(slot, &mtls); 883 884 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); 885 mCtx->launchThreads(ain, aout, sc, &mtls); 886 mCtx->setTLS(oldTLS); 887} 888 889void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) { 890 mtls->script = this; 891 mtls->fep.slot = slot; 892#ifndef RS_COMPATIBILITY_LIB 893 rsAssert(slot < mExecutable->getExportForeachFuncAddrs().size()); 894 mtls->kernel = reinterpret_cast<ForEachFunc_t>( 895 mExecutable->getExportForeachFuncAddrs()[slot]); 896 rsAssert(mtls->kernel != NULL); 897 mtls->sig = mExecutable->getInfo().getExportForeachFuncs()[slot].second; 898#else 899 mtls->kernel = reinterpret_cast<ForEachFunc_t>(mForEachFunctions[slot]); 900 rsAssert(mtls->kernel != NULL); 901 mtls->sig = mForEachSignatures[slot]; 902#endif 903} 904 905int RsdCpuScriptImpl::invokeRoot() { 906 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); 907 int ret = mRoot(); 908 mCtx->setTLS(oldTLS); 909 return ret; 910} 911 912void RsdCpuScriptImpl::invokeInit() { 913 if (mInit) { 914 mInit(); 915 } 916} 917 918void RsdCpuScriptImpl::invokeFreeChildren() { 919 if (mFreeChildren) { 920 mFreeChildren(); 921 } 922} 923 924void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params, 925 size_t paramLength) { 926 //ALOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength); 927 928 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); 929 reinterpret_cast<void (*)(const void *, uint32_t)>( 930#ifndef RS_COMPATIBILITY_LIB 931 mExecutable->getExportFuncAddrs()[slot])(params, paramLength); 932#else 933 mInvokeFunctions[slot])(params, paramLength); 934#endif 935 mCtx->setTLS(oldTLS); 936} 937 938void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { 939 //rsAssert(!script->mFieldIsObject[slot]); 940 //ALOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength); 941 942 //if (mIntrinsicID) { 943 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength); 944 //return; 945 //} 946 947#ifndef RS_COMPATIBILITY_LIB 948 int32_t *destPtr = reinterpret_cast<int32_t *>( 949 mExecutable->getExportVarAddrs()[slot]); 950#else 951 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]); 952#endif 953 if (!destPtr) { 954 //ALOGV("Calling setVar on slot = %i which is null", slot); 955 return; 956 } 957 958 memcpy(destPtr, data, dataLength); 959} 960 961void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) { 962 //rsAssert(!script->mFieldIsObject[slot]); 963 //ALOGE("getGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength); 964 965#ifndef RS_COMPATIBILITY_LIB 966 int32_t *srcPtr = reinterpret_cast<int32_t *>( 967 mExecutable->getExportVarAddrs()[slot]); 968#else 969 int32_t *srcPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]); 970#endif 971 if (!srcPtr) { 972 //ALOGV("Calling setVar on slot = %i which is null", slot); 973 return; 974 } 975 memcpy(data, srcPtr, dataLength); 976} 977 978 979void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, 980 const Element *elem, 981 const size_t *dims, size_t dimLength) { 982 983#ifndef RS_COMPATIBILITY_LIB 984 int32_t *destPtr = reinterpret_cast<int32_t *>( 985 mExecutable->getExportVarAddrs()[slot]); 986#else 987 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]); 988#endif 989 if (!destPtr) { 990 //ALOGV("Calling setVar on slot = %i which is null", slot); 991 return; 992 } 993 994 // We want to look at dimension in terms of integer components, 995 // but dimLength is given in terms of bytes. 996 dimLength /= sizeof(int); 997 998 // Only a single dimension is currently supported. 999 rsAssert(dimLength == 1); 1000 if (dimLength == 1) { 1001 // First do the increment loop. 1002 size_t stride = elem->getSizeBytes(); 1003 const char *cVal = reinterpret_cast<const char *>(data); 1004 for (size_t i = 0; i < dims[0]; i++) { 1005 elem->incRefs(cVal); 1006 cVal += stride; 1007 } 1008 1009 // Decrement loop comes after (to prevent race conditions). 1010 char *oldVal = reinterpret_cast<char *>(destPtr); 1011 for (size_t i = 0; i < dims[0]; i++) { 1012 elem->decRefs(oldVal); 1013 oldVal += stride; 1014 } 1015 } 1016 1017 memcpy(destPtr, data, dataLength); 1018} 1019 1020void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) { 1021 1022 //rsAssert(!script->mFieldIsObject[slot]); 1023 //ALOGE("setGlobalBind %p %p %i %p", dc, script, slot, data); 1024 1025#ifndef RS_COMPATIBILITY_LIB 1026 int32_t *destPtr = reinterpret_cast<int32_t *>( 1027 mExecutable->getExportVarAddrs()[slot]); 1028#else 1029 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]); 1030#endif 1031 if (!destPtr) { 1032 //ALOGV("Calling setVar on slot = %i which is null", slot); 1033 return; 1034 } 1035 1036 void *ptr = NULL; 1037 mBoundAllocs[slot] = data; 1038 if(data) { 1039 ptr = data->mHal.drvState.lod[0].mallocPtr; 1040 } 1041 memcpy(destPtr, &ptr, sizeof(void *)); 1042} 1043 1044void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) { 1045 1046 //rsAssert(script->mFieldIsObject[slot]); 1047 //ALOGE("setGlobalObj %p %p %i %p", dc, script, slot, data); 1048 1049 //if (mIntrinsicID) { 1050 //mIntrinsicFuncs.setVarObj(dc, script, drv->mIntrinsicData, slot, alloc); 1051 //return; 1052 //} 1053 1054#ifndef RS_COMPATIBILITY_LIB 1055 int32_t *destPtr = reinterpret_cast<int32_t *>( 1056 mExecutable->getExportVarAddrs()[slot]); 1057#else 1058 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]); 1059#endif 1060 1061 if (!destPtr) { 1062 //ALOGV("Calling setVar on slot = %i which is null", slot); 1063 return; 1064 } 1065 1066 rsrSetObject(mCtx->getContext(), (ObjectBase **)destPtr, data); 1067} 1068 1069RsdCpuScriptImpl::~RsdCpuScriptImpl() { 1070#ifndef RS_COMPATIBILITY_LIB 1071 if (mExecutable) { 1072 Vector<void *>::const_iterator var_addr_iter = 1073 mExecutable->getExportVarAddrs().begin(); 1074 Vector<void *>::const_iterator var_addr_end = 1075 mExecutable->getExportVarAddrs().end(); 1076 1077 bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_iter = 1078 mExecutable->getInfo().getObjectSlots().begin(); 1079 bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_end = 1080 mExecutable->getInfo().getObjectSlots().end(); 1081 1082 while ((var_addr_iter != var_addr_end) && 1083 (is_object_iter != is_object_end)) { 1084 // The field address can be NULL if the script-side has optimized 1085 // the corresponding global variable away. 1086 ObjectBase **obj_addr = 1087 reinterpret_cast<ObjectBase **>(*var_addr_iter); 1088 if (*is_object_iter) { 1089 if (*var_addr_iter != NULL) { 1090 rsrClearObject(mCtx->getContext(), obj_addr); 1091 } 1092 } 1093 var_addr_iter++; 1094 is_object_iter++; 1095 } 1096 } 1097 1098 if (mCompilerContext) { 1099 delete mCompilerContext; 1100 } 1101 if (mCompilerDriver) { 1102 delete mCompilerDriver; 1103 } 1104 if (mExecutable) { 1105 delete mExecutable; 1106 } 1107 if (mBoundAllocs) { 1108 delete[] mBoundAllocs; 1109 } 1110 1111 for (size_t i = 0; i < mExportedForEachFuncList.size(); i++) { 1112 delete[] mExportedForEachFuncList[i].first; 1113 } 1114#else 1115 if (mFieldIsObject) { 1116 for (size_t i = 0; i < mExportedVariableCount; ++i) { 1117 if (mFieldIsObject[i]) { 1118 if (mFieldAddress[i] != NULL) { 1119 ObjectBase **obj_addr = 1120 reinterpret_cast<ObjectBase **>(mFieldAddress[i]); 1121 rsrClearObject(mCtx->getContext(), obj_addr); 1122 } 1123 } 1124 } 1125 } 1126 1127 if (mInvokeFunctions) delete[] mInvokeFunctions; 1128 if (mForEachFunctions) delete[] mForEachFunctions; 1129 if (mFieldAddress) delete[] mFieldAddress; 1130 if (mFieldIsObject) delete[] mFieldIsObject; 1131 if (mForEachSignatures) delete[] mForEachSignatures; 1132 if (mBoundAllocs) delete[] mBoundAllocs; 1133 if (mScriptSO) { 1134 dlclose(mScriptSO); 1135 } 1136#endif 1137} 1138 1139Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const { 1140 if (!ptr) { 1141 return NULL; 1142 } 1143 1144 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) { 1145 Allocation *a = mBoundAllocs[ct]; 1146 if (!a) continue; 1147 if (a->mHal.drvState.lod[0].mallocPtr == ptr) { 1148 return a; 1149 } 1150 } 1151 ALOGE("rsGetAllocation, failed to find %p", ptr); 1152 return NULL; 1153} 1154 1155void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation * ain, 1156 Allocation * aout, const void * usr, 1157 uint32_t usrLen, const RsScriptCall *sc) 1158{ 1159} 1160 1161void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation * ain, 1162 Allocation * aout, const void * usr, 1163 uint32_t usrLen, const RsScriptCall *sc) 1164{ 1165} 1166 1167 1168} 1169} 1170