rsScriptC.cpp revision 7a21ee61484e7c55f8642231362821058104a49d
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 setupGLState(rsc); 51 mEnviroment.mStartTimeMillis 52 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); 53 54 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { 55 if (mSlots[ct].get() && !mTypes[ct].get()) { 56 mTypes[ct].set(mSlots[ct]->getType()); 57 } 58 59 if (!mTypes[ct].get()) 60 continue; 61 void *ptr = NULL; 62 if (mSlots[ct].get()) { 63 ptr = mSlots[ct]->getPtr(); 64 } 65 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct]; 66 67 if (rsc->props.mLogScripts) { 68 if (mSlots[ct].get() != NULL) { 69 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType()); 70 } else { 71 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=null", rsc, ct, dest, ptr); 72 } 73 } 74 75 if (dest) { 76 *dest = ptr; 77 } 78 } 79} 80 81const Allocation *ScriptC::ptrToAllocation(const void *ptr) const { 82 if (!ptr) { 83 return NULL; 84 } 85 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { 86 if (!mSlots[ct].get()) 87 continue; 88 if (mSlots[ct]->getPtr() == ptr) { 89 return mSlots[ct].get(); 90 } 91 } 92 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr); 93 return NULL; 94} 95 96Script * ScriptC::setTLS(Script *sc) { 97 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *) 98 pthread_getspecific(Context::gThreadTLSKey); 99 rsAssert(tls); 100 Script *old = tls->mScript; 101 tls->mScript = sc; 102 return old; 103} 104 105void ScriptC::setupGLState(Context *rsc) { 106 if (mEnviroment.mFragmentStore.get()) { 107 rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); 108 } 109 if (mEnviroment.mFragment.get()) { 110 rsc->setFragment(mEnviroment.mFragment.get()); 111 } 112 if (mEnviroment.mVertex.get()) { 113 rsc->setVertex(mEnviroment.mVertex.get()); 114 } 115 if (mEnviroment.mRaster.get()) { 116 rsc->setRaster(mEnviroment.mRaster.get()); 117 } 118} 119 120uint32_t ScriptC::run(Context *rsc) { 121 if (mProgram.mRoot == NULL) { 122 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); 123 return 0; 124 } 125 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 236 if (ain) { 237 mtls.dimX = ain->getType()->getDimX(); 238 mtls.dimY = ain->getType()->getDimY(); 239 mtls.dimZ = ain->getType()->getDimZ(); 240 //mtls.dimArray = ain->getType()->getDimArray(); 241 } else if (aout) { 242 mtls.dimX = aout->getType()->getDimX(); 243 mtls.dimY = aout->getType()->getDimY(); 244 mtls.dimZ = aout->getType()->getDimZ(); 245 //mtls.dimArray = aout->getType()->getDimArray(); 246 } else { 247 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); 248 return; 249 } 250 251 if (!sc || (sc->xEnd == 0)) { 252 mtls.xEnd = mtls.dimX; 253 } else { 254 rsAssert(sc->xStart < mtls.dimX); 255 rsAssert(sc->xEnd <= mtls.dimX); 256 rsAssert(sc->xStart < sc->xEnd); 257 mtls.xStart = rsMin(mtls.dimX, sc->xStart); 258 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd); 259 if (mtls.xStart >= mtls.xEnd) return; 260 } 261 262 if (!sc || (sc->yEnd == 0)) { 263 mtls.yEnd = mtls.dimY; 264 } else { 265 rsAssert(sc->yStart < mtls.dimY); 266 rsAssert(sc->yEnd <= mtls.dimY); 267 rsAssert(sc->yStart < sc->yEnd); 268 mtls.yStart = rsMin(mtls.dimY, sc->yStart); 269 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd); 270 if (mtls.yStart >= mtls.yEnd) return; 271 } 272 273 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd); 274 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd); 275 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd); 276 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd); 277 278 rsAssert(ain->getType()->getDimZ() == 0); 279 280 setupScript(rsc); 281 Script * oldTLS = setTLS(this); 282 283 mtls.rsc = rsc; 284 mtls.ain = ain; 285 mtls.aout = aout; 286 mtls.script = this; 287 mtls.usr = usr; 288 mtls.mSliceSize = 10; 289 mtls.mSliceNum = 0; 290 291 mtls.ptrIn = NULL; 292 mtls.eStrideIn = 0; 293 if (ain) { 294 mtls.ptrIn = (const uint8_t *)ain->getPtr(); 295 mtls.eStrideIn = ain->getType()->getElementSizeBytes(); 296 } 297 298 mtls.ptrOut = NULL; 299 mtls.eStrideOut = 0; 300 if (aout) { 301 mtls.ptrOut = (uint8_t *)aout->getPtr(); 302 mtls.eStrideOut = aout->getType()->getElementSizeBytes(); 303 } 304 305 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) { 306 if (mtls.dimY > 1) { 307 rsc->launchThreads(wc_xy, &mtls); 308 } else { 309 rsc->launchThreads(wc_x, &mtls); 310 } 311 312 //LOGE("launch 1"); 313 } else { 314 //LOGE("launch 3"); 315 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) { 316 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) { 317 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) { 318 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar + 319 mtls.dimX * mtls.dimY * z + 320 mtls.dimX * y; 321 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset); 322 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset); 323 324 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) { 325 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar); 326 xPtrIn += mtls.eStrideIn; 327 xPtrOut += mtls.eStrideOut; 328 } 329 } 330 } 331 } 332 } 333 334 setTLS(oldTLS); 335} 336 337void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) { 338 //LOGE("rsi_ScriptInvoke %i", slot); 339 if ((slot >= mEnviroment.mInvokeFunctionCount) || 340 (mEnviroment.mInvokeFunctions[slot] == NULL)) { 341 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); 342 return; 343 } 344 setupScript(rsc); 345 Script * oldTLS = setTLS(this); 346 347 if (rsc->props.mLogScripts) { 348 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]); 349 } 350 ((void (*)(const void *, uint32_t)) 351 mEnviroment.mInvokeFunctions[slot])(data, len); 352 if (rsc->props.mLogScripts) { 353 LOGV("%p ScriptC::Invoke complete", rsc); 354 } 355 356 setTLS(oldTLS); 357} 358 359ScriptCState::ScriptCState() { 360 mScript.clear(); 361} 362 363ScriptCState::~ScriptCState() { 364 mScript.clear(); 365} 366 367void ScriptCState::init(Context *rsc) { 368 clear(rsc); 369} 370 371void ScriptCState::clear(Context *rsc) { 372 rsAssert(rsc); 373 mScript.clear(); 374 mScript.set(new ScriptC(rsc)); 375} 376 377static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) { 378 const ScriptCState::SymbolTable_t *sym; 379 ScriptC *s = (ScriptC *)pContext; 380 sym = ScriptCState::lookupSymbol(name); 381 if (!sym) { 382 sym = ScriptCState::lookupSymbolCL(name); 383 } 384 if (!sym) { 385 sym = ScriptCState::lookupSymbolGL(name); 386 } 387 if (sym) { 388 s->mEnviroment.mIsThreadable &= sym->threadable; 389 return sym->mPtr; 390 } 391 LOGE("ScriptC sym lookup failed for %s", name); 392 return NULL; 393} 394 395extern const char rs_runtime_lib_bc[]; 396extern unsigned rs_runtime_lib_bc_size; 397 398void ScriptCState::runCompiler(Context *rsc, ScriptC *s) { 399 { 400 StopWatch compileTimer("RenderScript compile time"); 401 s->mBccScript = bccCreateScript(); 402 s->mEnviroment.mIsThreadable = true; 403 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength); 404 //bccLinkBitcode(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size); 405 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s); 406 bccCompileScript(s->mBccScript); 407 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot); 408 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit); 409 } 410 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit); 411 412 if (s->mProgram.mInit) { 413 s->mProgram.mInit(); 414 } 415 416 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL); 417 if (s->mEnviroment.mInvokeFunctionCount <= 0) 418 s->mEnviroment.mInvokeFunctions = NULL; 419 else { 420 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t)); 421 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions); 422 } 423 424 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL); 425 if (s->mEnviroment.mFieldCount <= 0) 426 s->mEnviroment.mFieldAddress = NULL; 427 else { 428 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *)); 429 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress); 430 s->initSlots(); 431 } 432 433 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); 434 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); 435 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); 436 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); 437 438 if (s->mProgram.mRoot) { 439 const static int pragmaMax = 16; 440 BCCsizei pragmaCount; 441 BCCchar * str[pragmaMax]; 442 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]); 443 444 for (int ct=0; ct < pragmaCount; ct+=2) { 445 //LOGE("pragme %s %s", str[ct], str[ct+1]); 446 if (!strcmp(str[ct], "version")) { 447 continue; 448 } 449 450 if (!strcmp(str[ct], "stateVertex")) { 451 if (!strcmp(str[ct+1], "default")) { 452 continue; 453 } 454 if (!strcmp(str[ct+1], "parent")) { 455 s->mEnviroment.mVertex.clear(); 456 continue; 457 } 458 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); 459 } 460 461 if (!strcmp(str[ct], "stateRaster")) { 462 if (!strcmp(str[ct+1], "default")) { 463 continue; 464 } 465 if (!strcmp(str[ct+1], "parent")) { 466 s->mEnviroment.mRaster.clear(); 467 continue; 468 } 469 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); 470 } 471 472 if (!strcmp(str[ct], "stateFragment")) { 473 if (!strcmp(str[ct+1], "default")) { 474 continue; 475 } 476 if (!strcmp(str[ct+1], "parent")) { 477 s->mEnviroment.mFragment.clear(); 478 continue; 479 } 480 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); 481 } 482 483 if (!strcmp(str[ct], "stateStore")) { 484 if (!strcmp(str[ct+1], "default")) { 485 continue; 486 } 487 if (!strcmp(str[ct+1], "parent")) { 488 s->mEnviroment.mFragmentStore.clear(); 489 continue; 490 } 491 LOGE("Unreconized value %s passed to stateStore", str[ct+1]); 492 } 493 494 } 495 496 497 } else { 498 // Deal with an error. 499 } 500} 501 502namespace android { 503namespace renderscript { 504 505void rsi_ScriptCBegin(Context * rsc) { 506 ScriptCState *ss = &rsc->mScriptC; 507 ss->clear(rsc); 508} 509 510void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) { 511 ScriptCState *ss = &rsc->mScriptC; 512 513 char *t = (char *)malloc(len + 1); 514 memcpy(t, text, len); 515 t[len] = 0; 516 ss->mScript->mEnviroment.mScriptText = t; 517 ss->mScript->mEnviroment.mScriptTextLength = len; 518} 519 520RsScript rsi_ScriptCCreate(Context * rsc) { 521 ScriptCState *ss = &rsc->mScriptC; 522 523 ObjectBaseRef<ScriptC> s(ss->mScript); 524 ss->mScript.clear(); 525 s->incUserRef(); 526 527 ss->runCompiler(rsc, s.get()); 528 ss->clear(rsc); 529 return s.get(); 530} 531 532} 533} 534