rsScriptC.cpp revision 15849d9a926fbd83587b9da30b8888574998a0fd
1/* 2 * Copyright (C) 2009 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 "rsContext.h" 18#include "rsScriptC.h" 19#include "rsMatrix.h" 20#include "../../compile/libbcc/include/bcc/bcc.h" 21#include "utils/Timers.h" 22#include "utils/StopWatch.h" 23 24#include <GLES/gl.h> 25#include <GLES/glext.h> 26 27using namespace android; 28using namespace android::renderscript; 29 30#define GET_TLS() Context::ScriptTLSStruct * tls = \ 31 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ 32 Context * rsc = tls->mContext; \ 33 ScriptC * sc = (ScriptC *) tls->mScript 34 35 36ScriptC::ScriptC(Context *rsc) : Script(rsc) { 37 mBccScript = NULL; 38 memset(&mProgram, 0, sizeof(mProgram)); 39} 40 41ScriptC::~ScriptC() { 42 if (mBccScript) { 43 bccDeleteScript(mBccScript); 44 } 45 free(mEnviroment.mScriptText); 46 mEnviroment.mScriptText = NULL; 47} 48 49void ScriptC::setupScript(Context *rsc) { 50 mEnviroment.mStartTimeMillis 51 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); 52 53 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { 54 if (mSlots[ct].get() && !mTypes[ct].get()) { 55 mTypes[ct].set(mSlots[ct]->getType()); 56 } 57 58 if (!mTypes[ct].get()) 59 continue; 60 void *ptr = NULL; 61 if (mSlots[ct].get()) { 62 ptr = mSlots[ct]->getPtr(); 63 } 64 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct]; 65 66 if (rsc->props.mLogScripts) { 67 if (mSlots[ct].get() != NULL) { 68 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType()); 69 } else { 70 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=null", rsc, ct, dest, ptr); 71 } 72 } 73 74 if (dest) { 75 *dest = ptr; 76 } 77 } 78} 79 80const Allocation *ScriptC::ptrToAllocation(const void *ptr) const { 81 if (!ptr) { 82 return NULL; 83 } 84 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { 85 if (!mSlots[ct].get()) 86 continue; 87 if (mSlots[ct]->getPtr() == ptr) { 88 return mSlots[ct].get(); 89 } 90 } 91 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr); 92 return NULL; 93} 94 95Script * ScriptC::setTLS(Script *sc) { 96 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *) 97 pthread_getspecific(Context::gThreadTLSKey); 98 rsAssert(tls); 99 Script *old = tls->mScript; 100 tls->mScript = sc; 101 return old; 102} 103 104void ScriptC::setupGLState(Context *rsc) { 105 if (mEnviroment.mFragmentStore.get()) { 106 rsc->setProgramStore(mEnviroment.mFragmentStore.get()); 107 } 108 if (mEnviroment.mFragment.get()) { 109 rsc->setProgramFragment(mEnviroment.mFragment.get()); 110 } 111 if (mEnviroment.mVertex.get()) { 112 rsc->setProgramVertex(mEnviroment.mVertex.get()); 113 } 114 if (mEnviroment.mRaster.get()) { 115 rsc->setProgramRaster(mEnviroment.mRaster.get()); 116 } 117} 118 119uint32_t ScriptC::run(Context *rsc) { 120 if (mProgram.mRoot == NULL) { 121 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); 122 return 0; 123 } 124 125 setupGLState(rsc); 126 setupScript(rsc); 127 128 uint32_t ret = 0; 129 Script * oldTLS = setTLS(this); 130 131 if (rsc->props.mLogScripts) { 132 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot); 133 } 134 135 ret = mProgram.mRoot(); 136 137 if (rsc->props.mLogScripts) { 138 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret); 139 } 140 141 setTLS(oldTLS); 142 return ret; 143} 144 145typedef struct { 146 Context *rsc; 147 ScriptC *script; 148 const Allocation * ain; 149 Allocation * aout; 150 const void * usr; 151 152 uint32_t mSliceSize; 153 volatile int mSliceNum; 154 155 const uint8_t *ptrIn; 156 uint32_t eStrideIn; 157 uint8_t *ptrOut; 158 uint32_t eStrideOut; 159 160 uint32_t xStart; 161 uint32_t xEnd; 162 uint32_t yStart; 163 uint32_t yEnd; 164 uint32_t zStart; 165 uint32_t zEnd; 166 uint32_t arrayStart; 167 uint32_t arrayEnd; 168 169 uint32_t dimX; 170 uint32_t dimY; 171 uint32_t dimZ; 172 uint32_t dimArray; 173} MTLaunchStruct; 174typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); 175 176static void wc_xy(void *usr, uint32_t idx) { 177 MTLaunchStruct *mtls = (MTLaunchStruct *)usr; 178 179 while (1) { 180 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); 181 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize; 182 uint32_t yEnd = yStart + mtls->mSliceSize; 183 yEnd = rsMin(yEnd, mtls->yEnd); 184 if (yEnd <= yStart) { 185 return; 186 } 187 188 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); 189 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut); 190 for (uint32_t y = yStart; y < yEnd; y++) { 191 uint32_t offset = mtls->dimX * y; 192 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset); 193 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset); 194 195 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) { 196 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0); 197 xPtrIn += mtls->eStrideIn; 198 xPtrOut += mtls->eStrideOut; 199 } 200 } 201 } 202} 203 204static void wc_x(void *usr, uint32_t idx) { 205 MTLaunchStruct *mtls = (MTLaunchStruct *)usr; 206 207 while (1) { 208 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); 209 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize; 210 uint32_t xEnd = xStart + mtls->mSliceSize; 211 xEnd = rsMin(xEnd, mtls->xEnd); 212 if (xEnd <= xStart) { 213 return; 214 } 215 216 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); 217 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut); 218 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart); 219 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart); 220 for (uint32_t x = xStart; x < xEnd; x++) { 221 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0); 222 xPtrIn += mtls->eStrideIn; 223 xPtrOut += mtls->eStrideOut; 224 } 225 } 226} 227 228void ScriptC::runForEach(Context *rsc, 229 const Allocation * ain, 230 Allocation * aout, 231 const void * usr, 232 const RsScriptCall *sc) { 233 MTLaunchStruct mtls; 234 memset(&mtls, 0, sizeof(mtls)); 235 Context::PushState ps(rsc); 236 237 if (ain) { 238 mtls.dimX = ain->getType()->getDimX(); 239 mtls.dimY = ain->getType()->getDimY(); 240 mtls.dimZ = ain->getType()->getDimZ(); 241 //mtls.dimArray = ain->getType()->getDimArray(); 242 } else if (aout) { 243 mtls.dimX = aout->getType()->getDimX(); 244 mtls.dimY = aout->getType()->getDimY(); 245 mtls.dimZ = aout->getType()->getDimZ(); 246 //mtls.dimArray = aout->getType()->getDimArray(); 247 } else { 248 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); 249 return; 250 } 251 252 if (!sc || (sc->xEnd == 0)) { 253 mtls.xEnd = mtls.dimX; 254 } else { 255 rsAssert(sc->xStart < mtls.dimX); 256 rsAssert(sc->xEnd <= mtls.dimX); 257 rsAssert(sc->xStart < sc->xEnd); 258 mtls.xStart = rsMin(mtls.dimX, sc->xStart); 259 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd); 260 if (mtls.xStart >= mtls.xEnd) return; 261 } 262 263 if (!sc || (sc->yEnd == 0)) { 264 mtls.yEnd = mtls.dimY; 265 } else { 266 rsAssert(sc->yStart < mtls.dimY); 267 rsAssert(sc->yEnd <= mtls.dimY); 268 rsAssert(sc->yStart < sc->yEnd); 269 mtls.yStart = rsMin(mtls.dimY, sc->yStart); 270 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd); 271 if (mtls.yStart >= mtls.yEnd) return; 272 } 273 274 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd); 275 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd); 276 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd); 277 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd); 278 279 rsAssert(ain->getType()->getDimZ() == 0); 280 281 setupGLState(rsc); 282 setupScript(rsc); 283 Script * oldTLS = setTLS(this); 284 285 mtls.rsc = rsc; 286 mtls.ain = ain; 287 mtls.aout = aout; 288 mtls.script = this; 289 mtls.usr = usr; 290 mtls.mSliceSize = 10; 291 mtls.mSliceNum = 0; 292 293 mtls.ptrIn = NULL; 294 mtls.eStrideIn = 0; 295 if (ain) { 296 mtls.ptrIn = (const uint8_t *)ain->getPtr(); 297 mtls.eStrideIn = ain->getType()->getElementSizeBytes(); 298 } 299 300 mtls.ptrOut = NULL; 301 mtls.eStrideOut = 0; 302 if (aout) { 303 mtls.ptrOut = (uint8_t *)aout->getPtr(); 304 mtls.eStrideOut = aout->getType()->getElementSizeBytes(); 305 } 306 307 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) { 308 if (mtls.dimY > 1) { 309 rsc->launchThreads(wc_xy, &mtls); 310 } else { 311 rsc->launchThreads(wc_x, &mtls); 312 } 313 314 //LOGE("launch 1"); 315 } else { 316 //LOGE("launch 3"); 317 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) { 318 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) { 319 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) { 320 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar + 321 mtls.dimX * mtls.dimY * z + 322 mtls.dimX * y; 323 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset); 324 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset); 325 326 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) { 327 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar); 328 xPtrIn += mtls.eStrideIn; 329 xPtrOut += mtls.eStrideOut; 330 } 331 } 332 } 333 } 334 } 335 336 setTLS(oldTLS); 337} 338 339void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) { 340 if ((slot >= mEnviroment.mInvokeFunctionCount) || 341 (mEnviroment.mInvokeFunctions[slot] == NULL)) { 342 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); 343 return; 344 } 345 setupScript(rsc); 346 Script * oldTLS = setTLS(this); 347 348 if (rsc->props.mLogScripts) { 349 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]); 350 } 351 ((void (*)(const void *, uint32_t)) 352 mEnviroment.mInvokeFunctions[slot])(data, len); 353 if (rsc->props.mLogScripts) { 354 LOGV("%p ScriptC::Invoke complete", rsc); 355 } 356 357 setTLS(oldTLS); 358} 359 360ScriptCState::ScriptCState() { 361 mScript.clear(); 362} 363 364ScriptCState::~ScriptCState() { 365 mScript.clear(); 366} 367 368void ScriptCState::init(Context *rsc) { 369 clear(rsc); 370} 371 372void ScriptCState::clear(Context *rsc) { 373 rsAssert(rsc); 374 mScript.clear(); 375 mScript.set(new ScriptC(rsc)); 376} 377 378static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) { 379 const ScriptCState::SymbolTable_t *sym; 380 ScriptC *s = (ScriptC *)pContext; 381 if (!strcmp(name, "__isThreadable")) { 382 return (BCCvoid*) s->mEnviroment.mIsThreadable; 383 } else if (!strcmp(name, "__clearThreadable")) { 384 s->mEnviroment.mIsThreadable = false; 385 return NULL; 386 } 387 sym = ScriptCState::lookupSymbol(name); 388 if (!sym) { 389 sym = ScriptCState::lookupSymbolCL(name); 390 } 391 if (!sym) { 392 sym = ScriptCState::lookupSymbolGL(name); 393 } 394 if (sym) { 395 s->mEnviroment.mIsThreadable &= sym->threadable; 396 return sym->mPtr; 397 } 398 LOGE("ScriptC sym lookup failed for %s", name); 399 return NULL; 400} 401 402extern const char rs_runtime_lib_bc[]; 403extern unsigned rs_runtime_lib_bc_size; 404 405void ScriptCState::runCompiler(Context *rsc, ScriptC *s, const char *resName, const char *cacheDir) { 406 { 407 s->mBccScript = bccCreateScript(); 408 s->mEnviroment.mIsThreadable = true; 409 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s); 410 // bccReadBC() reads in the BitCode, if no cache file corresponding to 411 // the resName is found. Otherwise, bccReadBC() returns a negative value 412 // and the "else" branch will be taken. 413 if (bccReadBC(s->mBccScript, 414 s->mEnviroment.mScriptText, 415 s->mEnviroment.mScriptTextLength, 416 resName, 417 cacheDir) >= 0) { 418 //bccLinkBC(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size); 419 bccCompileBC(s->mBccScript); 420 } else { 421 // bccReadBC returns a neagative value: Didn't read any script, 422 // So, use cached binary instead 423 bccLoadBinary(s->mBccScript); 424 } 425 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot); 426 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit); 427 } 428 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit); 429 430 if (s->mProgram.mInit) { 431 s->mProgram.mInit(); 432 } 433 434 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL); 435 if (s->mEnviroment.mInvokeFunctionCount <= 0) 436 s->mEnviroment.mInvokeFunctions = NULL; 437 else { 438 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t)); 439 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions); 440 } 441 442 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL); 443 if (s->mEnviroment.mFieldCount <= 0) 444 s->mEnviroment.mFieldAddress = NULL; 445 else { 446 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *)); 447 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress); 448 s->initSlots(); 449 } 450 451 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); 452 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); 453 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); 454 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); 455 456 if (s->mProgram.mRoot) { 457 const static int pragmaMax = 16; 458 BCCsizei pragmaCount; 459 BCCchar * str[pragmaMax]; 460 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]); 461 462 for (int ct=0; ct < pragmaCount; ct+=2) { 463 //LOGE("pragme %s %s", str[ct], str[ct+1]); 464 if (!strcmp(str[ct], "version")) { 465 continue; 466 } 467 468 if (!strcmp(str[ct], "stateVertex")) { 469 if (!strcmp(str[ct+1], "default")) { 470 continue; 471 } 472 if (!strcmp(str[ct+1], "parent")) { 473 s->mEnviroment.mVertex.clear(); 474 continue; 475 } 476 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); 477 } 478 479 if (!strcmp(str[ct], "stateRaster")) { 480 if (!strcmp(str[ct+1], "default")) { 481 continue; 482 } 483 if (!strcmp(str[ct+1], "parent")) { 484 s->mEnviroment.mRaster.clear(); 485 continue; 486 } 487 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); 488 } 489 490 if (!strcmp(str[ct], "stateFragment")) { 491 if (!strcmp(str[ct+1], "default")) { 492 continue; 493 } 494 if (!strcmp(str[ct+1], "parent")) { 495 s->mEnviroment.mFragment.clear(); 496 continue; 497 } 498 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); 499 } 500 501 if (!strcmp(str[ct], "stateStore")) { 502 if (!strcmp(str[ct+1], "default")) { 503 continue; 504 } 505 if (!strcmp(str[ct+1], "parent")) { 506 s->mEnviroment.mFragmentStore.clear(); 507 continue; 508 } 509 LOGE("Unreconized value %s passed to stateStore", str[ct+1]); 510 } 511 512 } 513 514 515 } else { 516 // Deal with an error. 517 } 518} 519 520namespace android { 521namespace renderscript { 522 523void rsi_ScriptCBegin(Context * rsc) { 524 ScriptCState *ss = &rsc->mScriptC; 525 ss->clear(rsc); 526} 527 528void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) { 529 ScriptCState *ss = &rsc->mScriptC; 530 531 char *t = (char *)malloc(len + 1); 532 memcpy(t, text, len); 533 t[len] = 0; 534 ss->mScript->mEnviroment.mScriptText = t; 535 ss->mScript->mEnviroment.mScriptTextLength = len; 536} 537 538RsScript rsi_ScriptCCreate(Context * rsc, const char *resName, const char *cacheDir) 539{ 540 ScriptCState *ss = &rsc->mScriptC; 541 542 ObjectBaseRef<ScriptC> s(ss->mScript); 543 ss->mScript.clear(); 544 s->incUserRef(); 545 546 ss->runCompiler(rsc, s.get(), resName, cacheDir); 547 ss->clear(rsc); 548 return s.get(); 549} 550 551} 552} 553