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