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