rsdBcc.cpp revision 1415ca46b289604fd727310e4f6ae3c8c68276c9
1/* 2 * Copyright (C) 2011 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 18#include "rsdCore.h" 19#include "rsdBcc.h" 20#include "rsdRuntime.h" 21 22#include <bcinfo/MetadataExtractor.h> 23 24#include "rsContext.h" 25#include "rsScriptC.h" 26 27#include "utils/Timers.h" 28#include "utils/StopWatch.h" 29extern "C" { 30#include "libdex/ZipArchive.h" 31} 32 33 34using namespace android; 35using namespace android::renderscript; 36 37struct DrvScript { 38 int (*mRoot)(); 39 void (*mInit)(); 40 void (*mFreeChildren)(); 41 42 BCCScriptRef mBccScript; 43 44 bcinfo::MetadataExtractor *ME; 45 46 InvokeFunc_t *mInvokeFunctions; 47 void ** mFieldAddress; 48 bool * mFieldIsObject; 49 const uint32_t *mExportForEachSignatureList; 50 51 const uint8_t * mScriptText; 52 uint32_t mScriptTextLength; 53}; 54 55 56static Script * setTLS(Script *sc) { 57 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(rsdgThreadTLSKey); 58 rsAssert(tls); 59 Script *old = tls->mScript; 60 tls->mScript = sc; 61 return old; 62} 63 64 65bool rsdScriptInit(const Context *rsc, 66 ScriptC *script, 67 char const *resName, 68 char const *cacheDir, 69 uint8_t const *bitcode, 70 size_t bitcodeSize, 71 uint32_t flags) { 72 //LOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc); 73 74 pthread_mutex_lock(&rsdgInitMutex); 75 76 size_t exportFuncCount = 0; 77 size_t exportVarCount = 0; 78 size_t objectSlotCount = 0; 79 size_t exportForEachSignatureCount = 0; 80 81 DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript)); 82 if (drv == NULL) { 83 goto error; 84 } 85 script->mHal.drv = drv; 86 87 drv->mBccScript = bccCreateScript(); 88 script->mHal.info.isThreadable = true; 89 drv->mScriptText = bitcode; 90 drv->mScriptTextLength = bitcodeSize; 91 92 93 drv->ME = new bcinfo::MetadataExtractor((const char*)drv->mScriptText, 94 drv->mScriptTextLength); 95 if (!drv->ME->extract()) { 96 LOGE("bcinfo: failed to read script metadata"); 97 goto error; 98 } 99 100 //LOGE("mBccScript %p", script->mBccScript); 101 102 if (bccRegisterSymbolCallback(drv->mBccScript, &rsdLookupRuntimeStub, script) != 0) { 103 LOGE("bcc: FAILS to register symbol callback"); 104 goto error; 105 } 106 107 if (bccReadBC(drv->mBccScript, 108 resName, 109 (char const *)drv->mScriptText, 110 drv->mScriptTextLength, 0) != 0) { 111 LOGE("bcc: FAILS to read bitcode"); 112 goto error; 113 } 114 115 if (bccLinkFile(drv->mBccScript, "/system/lib/libclcore.bc", 0) != 0) { 116 LOGE("bcc: FAILS to link bitcode"); 117 goto error; 118 } 119 120 if (bccPrepareExecutable(drv->mBccScript, cacheDir, resName, 0) != 0) { 121 LOGE("bcc: FAILS to prepare executable"); 122 goto error; 123 } 124 125 drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root")); 126 drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init")); 127 drv->mFreeChildren = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, ".rs.dtor")); 128 129 exportFuncCount = drv->ME->getExportFuncCount(); 130 if (exportFuncCount > 0) { 131 drv->mInvokeFunctions = (InvokeFunc_t*) calloc(exportFuncCount, 132 sizeof(InvokeFunc_t)); 133 bccGetExportFuncList(drv->mBccScript, exportFuncCount, 134 (void **) drv->mInvokeFunctions); 135 } else { 136 drv->mInvokeFunctions = NULL; 137 } 138 139 exportVarCount = drv->ME->getExportVarCount(); 140 if (exportVarCount > 0) { 141 drv->mFieldAddress = (void **) calloc(exportVarCount, sizeof(void*)); 142 drv->mFieldIsObject = (bool *) calloc(exportVarCount, sizeof(bool)); 143 bccGetExportVarList(drv->mBccScript, exportVarCount, 144 (void **) drv->mFieldAddress); 145 } else { 146 drv->mFieldAddress = NULL; 147 drv->mFieldIsObject = NULL; 148 } 149 150 objectSlotCount = drv->ME->getObjectSlotCount(); 151 if (objectSlotCount > 0) { 152 const uint32_t *objectSlotList = drv->ME->getObjectSlotList(); 153 for (uint32_t ct=0; ct < objectSlotCount; ct++) { 154 drv->mFieldIsObject[objectSlotList[ct]] = true; 155 } 156 } 157 158 exportForEachSignatureCount = drv->ME->getExportForEachSignatureCount(); 159 rsAssert(exportForEachSignatureCount <= 1); 160 drv->mExportForEachSignatureList = drv->ME->getExportForEachSignatureList(); 161 162 // Copy info over to runtime 163 script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount(); 164 script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount(); 165 script->mHal.info.exportedPragmaCount = drv->ME->getPragmaCount(); 166 script->mHal.info.exportedPragmaKeyList = drv->ME->getPragmaKeyList(); 167 script->mHal.info.exportedPragmaValueList = drv->ME->getPragmaValueList(); 168 script->mHal.info.root = drv->mRoot; 169 170 pthread_mutex_unlock(&rsdgInitMutex); 171 return true; 172 173error: 174 175 pthread_mutex_unlock(&rsdgInitMutex); 176 if (drv->ME) { 177 delete drv->ME; 178 drv->ME = NULL; 179 } 180 free(drv); 181 return false; 182 183} 184 185typedef struct { 186 Context *rsc; 187 Script *script; 188 uint32_t sig; 189 const Allocation * ain; 190 Allocation * aout; 191 const void * usr; 192 size_t usrLen; 193 194 uint32_t mSliceSize; 195 volatile int mSliceNum; 196 197 const uint8_t *ptrIn; 198 uint32_t eStrideIn; 199 uint8_t *ptrOut; 200 uint32_t eStrideOut; 201 202 uint32_t xStart; 203 uint32_t xEnd; 204 uint32_t yStart; 205 uint32_t yEnd; 206 uint32_t zStart; 207 uint32_t zEnd; 208 uint32_t arrayStart; 209 uint32_t arrayEnd; 210 211 uint32_t dimX; 212 uint32_t dimY; 213 uint32_t dimZ; 214 uint32_t dimArray; 215} MTLaunchStruct; 216typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); 217 218static void wc_xy(void *usr, uint32_t idx) { 219 MTLaunchStruct *mtls = (MTLaunchStruct *)usr; 220 RsForEachStubParamStruct p; 221 memset(&p, 0, sizeof(p)); 222 p.usr = mtls->usr; 223 p.usr_len = mtls->usrLen; 224 RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv; 225 uint32_t sig = mtls->sig; 226 227 outer_foreach_t fn = dc->mForEachLaunch[sig]; 228 while (1) { 229 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); 230 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize; 231 uint32_t yEnd = yStart + mtls->mSliceSize; 232 yEnd = rsMin(yEnd, mtls->yEnd); 233 if (yEnd <= yStart) { 234 return; 235 } 236 237 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); 238 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut); 239 for (p.y = yStart; p.y < yEnd; p.y++) { 240 uint32_t offset = mtls->dimX * p.y; 241 p.out = mtls->ptrOut + (mtls->eStrideOut * offset); 242 p.in = mtls->ptrIn + (mtls->eStrideIn * offset); 243 fn(&mtls->script->mHal.info.root, &p, mtls->xStart, mtls->xEnd, 244 mtls->eStrideIn, mtls->eStrideOut); 245 } 246 } 247} 248 249static void wc_x(void *usr, uint32_t idx) { 250 MTLaunchStruct *mtls = (MTLaunchStruct *)usr; 251 RsForEachStubParamStruct p; 252 memset(&p, 0, sizeof(p)); 253 p.usr = mtls->usr; 254 p.usr_len = mtls->usrLen; 255 RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv; 256 uint32_t sig = mtls->sig; 257 258 outer_foreach_t fn = dc->mForEachLaunch[sig]; 259 while (1) { 260 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); 261 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize; 262 uint32_t xEnd = xStart + mtls->mSliceSize; 263 xEnd = rsMin(xEnd, mtls->xEnd); 264 if (xEnd <= xStart) { 265 return; 266 } 267 268 //LOGE("usr slice %i idx %i, x %i,%i", slice, idx, xStart, xEnd); 269 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut); 270 271 p.out = mtls->ptrOut + (mtls->eStrideOut * xStart); 272 p.in = mtls->ptrIn + (mtls->eStrideIn * xStart); 273 fn(&mtls->script->mHal.info.root, &p, xStart, xEnd, mtls->eStrideIn, mtls->eStrideOut); 274 } 275} 276 277void rsdScriptInvokeForEach(const Context *rsc, 278 Script *s, 279 uint32_t slot, 280 const Allocation * ain, 281 Allocation * aout, 282 const void * usr, 283 uint32_t usrLen, 284 const RsScriptCall *sc) { 285 286 RsdHal * dc = (RsdHal *)rsc->mHal.drv; 287 288 MTLaunchStruct mtls; 289 memset(&mtls, 0, sizeof(mtls)); 290 291 DrvScript *drv = (DrvScript *)s->mHal.drv; 292 // We only support slot 0 (root) at this point in time. 293 rsAssert(slot == 0); 294 mtls.sig = 0x1f; // temp fix for old apps, full table in slang_rs_export_foreach.cpp 295 if (drv->mExportForEachSignatureList) { 296 mtls.sig = drv->mExportForEachSignatureList[slot]; 297 } 298 if (ain) { 299 mtls.dimX = ain->getType()->getDimX(); 300 mtls.dimY = ain->getType()->getDimY(); 301 mtls.dimZ = ain->getType()->getDimZ(); 302 //mtls.dimArray = ain->getType()->getDimArray(); 303 } else if (aout) { 304 mtls.dimX = aout->getType()->getDimX(); 305 mtls.dimY = aout->getType()->getDimY(); 306 mtls.dimZ = aout->getType()->getDimZ(); 307 //mtls.dimArray = aout->getType()->getDimArray(); 308 } else { 309 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); 310 return; 311 } 312 313 if (!sc || (sc->xEnd == 0)) { 314 mtls.xEnd = mtls.dimX; 315 } else { 316 rsAssert(sc->xStart < mtls.dimX); 317 rsAssert(sc->xEnd <= mtls.dimX); 318 rsAssert(sc->xStart < sc->xEnd); 319 mtls.xStart = rsMin(mtls.dimX, sc->xStart); 320 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd); 321 if (mtls.xStart >= mtls.xEnd) return; 322 } 323 324 if (!sc || (sc->yEnd == 0)) { 325 mtls.yEnd = mtls.dimY; 326 } else { 327 rsAssert(sc->yStart < mtls.dimY); 328 rsAssert(sc->yEnd <= mtls.dimY); 329 rsAssert(sc->yStart < sc->yEnd); 330 mtls.yStart = rsMin(mtls.dimY, sc->yStart); 331 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd); 332 if (mtls.yStart >= mtls.yEnd) return; 333 } 334 335 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd); 336 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd); 337 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd); 338 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd); 339 340 rsAssert(!ain || (ain->getType()->getDimZ() == 0)); 341 342 Context *mrsc = (Context *)rsc; 343 Script * oldTLS = setTLS(s); 344 345 mtls.rsc = mrsc; 346 mtls.ain = ain; 347 mtls.aout = aout; 348 mtls.script = s; 349 mtls.usr = usr; 350 mtls.usrLen = usrLen; 351 mtls.mSliceSize = 10; 352 mtls.mSliceNum = 0; 353 354 mtls.ptrIn = NULL; 355 mtls.eStrideIn = 0; 356 if (ain) { 357 mtls.ptrIn = (const uint8_t *)ain->getPtr(); 358 mtls.eStrideIn = ain->getType()->getElementSizeBytes(); 359 } 360 361 mtls.ptrOut = NULL; 362 mtls.eStrideOut = 0; 363 if (aout) { 364 mtls.ptrOut = (uint8_t *)aout->getPtr(); 365 mtls.eStrideOut = aout->getType()->getElementSizeBytes(); 366 } 367 368 if ((dc->mWorkers.mCount > 1) && s->mHal.info.isThreadable) { 369 if (mtls.dimY > 1) { 370 rsdLaunchThreads(mrsc, wc_xy, &mtls); 371 } else { 372 rsdLaunchThreads(mrsc, wc_x, &mtls); 373 } 374 375 //LOGE("launch 1"); 376 } else { 377 RsForEachStubParamStruct p; 378 memset(&p, 0, sizeof(p)); 379 p.usr = mtls.usr; 380 p.usr_len = mtls.usrLen; 381 uint32_t sig = mtls.sig; 382 383 //LOGE("launch 3"); 384 outer_foreach_t fn = dc->mForEachLaunch[sig]; 385 for (p.ar[0] = mtls.arrayStart; p.ar[0] < mtls.arrayEnd; p.ar[0]++) { 386 for (p.z = mtls.zStart; p.z < mtls.zEnd; p.z++) { 387 for (p.y = mtls.yStart; p.y < mtls.yEnd; p.y++) { 388 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * p.ar[0] + 389 mtls.dimX * mtls.dimY * p.z + 390 mtls.dimX * p.y; 391 p.out = mtls.ptrOut + (mtls.eStrideOut * offset); 392 p.in = mtls.ptrIn + (mtls.eStrideIn * offset); 393 fn(&mtls.script->mHal.info.root, &p, mtls.xStart, mtls.xEnd, 394 mtls.eStrideIn, mtls.eStrideOut); 395 } 396 } 397 } 398 } 399 400 setTLS(oldTLS); 401} 402 403 404int rsdScriptInvokeRoot(const Context *dc, Script *script) { 405 DrvScript *drv = (DrvScript *)script->mHal.drv; 406 407 Script * oldTLS = setTLS(script); 408 int ret = drv->mRoot(); 409 setTLS(oldTLS); 410 411 return ret; 412} 413 414void rsdScriptInvokeInit(const Context *dc, Script *script) { 415 DrvScript *drv = (DrvScript *)script->mHal.drv; 416 417 if (drv->mInit) { 418 drv->mInit(); 419 } 420} 421 422void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) { 423 DrvScript *drv = (DrvScript *)script->mHal.drv; 424 425 if (drv->mFreeChildren) { 426 drv->mFreeChildren(); 427 } 428} 429 430void rsdScriptInvokeFunction(const Context *dc, Script *script, 431 uint32_t slot, 432 const void *params, 433 size_t paramLength) { 434 DrvScript *drv = (DrvScript *)script->mHal.drv; 435 //LOGE("invoke %p %p %i %p %i", dc, script, slot, params, paramLength); 436 437 Script * oldTLS = setTLS(script); 438 ((void (*)(const void *, uint32_t)) 439 drv->mInvokeFunctions[slot])(params, paramLength); 440 setTLS(oldTLS); 441} 442 443void rsdScriptSetGlobalVar(const Context *dc, const Script *script, 444 uint32_t slot, void *data, size_t dataLength) { 445 DrvScript *drv = (DrvScript *)script->mHal.drv; 446 //rsAssert(!script->mFieldIsObject[slot]); 447 //LOGE("setGlobalVar %p %p %i %p %i", dc, script, slot, data, dataLength); 448 449 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot]; 450 if (!destPtr) { 451 //ALOGV("Calling setVar on slot = %i which is null", slot); 452 return; 453 } 454 455 memcpy(destPtr, data, dataLength); 456} 457 458void rsdScriptSetGlobalBind(const Context *dc, const Script *script, uint32_t slot, void *data) { 459 DrvScript *drv = (DrvScript *)script->mHal.drv; 460 //rsAssert(!script->mFieldIsObject[slot]); 461 //LOGE("setGlobalBind %p %p %i %p", dc, script, slot, data); 462 463 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot]; 464 if (!destPtr) { 465 //ALOGV("Calling setVar on slot = %i which is null", slot); 466 return; 467 } 468 469 memcpy(destPtr, &data, sizeof(void *)); 470} 471 472void rsdScriptSetGlobalObj(const Context *dc, const Script *script, uint32_t slot, ObjectBase *data) { 473 DrvScript *drv = (DrvScript *)script->mHal.drv; 474 //rsAssert(script->mFieldIsObject[slot]); 475 //LOGE("setGlobalObj %p %p %i %p", dc, script, slot, data); 476 477 int32_t *destPtr = ((int32_t **)drv->mFieldAddress)[slot]; 478 if (!destPtr) { 479 //ALOGV("Calling setVar on slot = %i which is null", slot); 480 return; 481 } 482 483 rsrSetObject(dc, script, (ObjectBase **)destPtr, data); 484} 485 486void rsdScriptDestroy(const Context *dc, Script *script) { 487 DrvScript *drv = (DrvScript *)script->mHal.drv; 488 489 if (drv->mFieldAddress) { 490 size_t exportVarCount = drv->ME->getExportVarCount(); 491 for (size_t ct = 0; ct < exportVarCount; ct++) { 492 if (drv->mFieldIsObject[ct]) { 493 // The field address can be NULL if the script-side has 494 // optimized the corresponding global variable away. 495 if (drv->mFieldAddress[ct]) { 496 rsrClearObject(dc, script, (ObjectBase **)drv->mFieldAddress[ct]); 497 } 498 } 499 } 500 free(drv->mFieldAddress); 501 drv->mFieldAddress = NULL; 502 free(drv->mFieldIsObject); 503 drv->mFieldIsObject = NULL; } 504 505 if (drv->mInvokeFunctions) { 506 free(drv->mInvokeFunctions); 507 drv->mInvokeFunctions = NULL; 508 } 509 510 delete drv->ME; 511 drv->ME = NULL; 512 513 free(drv); 514 script->mHal.drv = NULL; 515 516} 517 518 519