rsContext.cpp revision 4961cceab2b71bf0ab59e1b66a7559f67ed28781
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#include "rs.h" 18#include "rsDevice.h" 19#include "rsContext.h" 20#include "rsThreadIO.h" 21 22#ifndef RS_COMPATIBILITY_LIB 23#include "rsMesh.h" 24#include <ui/FramebufferNativeWindow.h> 25#include <gui/DisplayEventReceiver.h> 26#endif 27 28#include <sys/types.h> 29#include <sys/resource.h> 30#include <sched.h> 31 32#include <sys/syscall.h> 33#include <string.h> 34#include <dlfcn.h> 35 36#ifndef RS_SERVER 37#include <cutils/properties.h> 38#endif 39 40#ifdef RS_SERVER 41// Android exposes gettid(), standard Linux does not 42static pid_t gettid() { 43 return syscall(SYS_gettid); 44} 45#endif 46 47using namespace android; 48using namespace android::renderscript; 49 50pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; 51pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER; 52pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER; 53 54bool Context::initGLThread() { 55 pthread_mutex_lock(&gInitMutex); 56 57 if (!mHal.funcs.initGraphics(this)) { 58 pthread_mutex_unlock(&gInitMutex); 59 ALOGE("%p initGraphics failed", this); 60 return false; 61 } 62 63 pthread_mutex_unlock(&gInitMutex); 64 return true; 65} 66 67void Context::deinitEGL() { 68#ifndef RS_COMPATIBILITY_LIB 69 mHal.funcs.shutdownGraphics(this); 70#endif 71} 72 73Context::PushState::PushState(Context *con) { 74 mRsc = con; 75#ifndef RS_COMPATIBILITY_LIB 76 if (con->mIsGraphicsContext) { 77 mFragment.set(con->getProgramFragment()); 78 mVertex.set(con->getProgramVertex()); 79 mStore.set(con->getProgramStore()); 80 mRaster.set(con->getProgramRaster()); 81 mFont.set(con->getFont()); 82 } 83#endif 84} 85 86Context::PushState::~PushState() { 87#ifndef RS_COMPATIBILITY_LIB 88 if (mRsc->mIsGraphicsContext) { 89 mRsc->setProgramFragment(mFragment.get()); 90 mRsc->setProgramVertex(mVertex.get()); 91 mRsc->setProgramStore(mStore.get()); 92 mRsc->setProgramRaster(mRaster.get()); 93 mRsc->setFont(mFont.get()); 94 } 95#endif 96} 97 98 99uint32_t Context::runScript(Script *s) { 100 PushState ps(this); 101 102 uint32_t ret = s->run(this); 103 return ret; 104} 105 106uint32_t Context::runRootScript() { 107 timerSet(RS_TIMER_SCRIPT); 108#ifndef RS_COMPATIBILITY_LIB 109 mStateFragmentStore.mLast.clear(); 110#endif 111 watchdog.inRoot = true; 112 uint32_t ret = runScript(mRootScript.get()); 113 watchdog.inRoot = false; 114 115 return ret; 116} 117 118uint64_t Context::getTime() const { 119#ifndef ANDROID_RS_SERIALIZE 120 struct timespec t; 121 clock_gettime(CLOCK_MONOTONIC, &t); 122 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); 123#else 124 return 0; 125#endif //ANDROID_RS_SERIALIZE 126} 127 128void Context::timerReset() { 129 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { 130 mTimers[ct] = 0; 131 } 132} 133 134void Context::timerInit() { 135 mTimeLast = getTime(); 136 mTimeFrame = mTimeLast; 137 mTimeLastFrame = mTimeLast; 138 mTimerActive = RS_TIMER_INTERNAL; 139 mAverageFPSFrameCount = 0; 140 mAverageFPSStartTime = mTimeLast; 141 mAverageFPS = 0; 142 timerReset(); 143} 144 145void Context::timerFrame() { 146 mTimeLastFrame = mTimeFrame; 147 mTimeFrame = getTime(); 148 // Update average fps 149 const uint64_t averageFramerateInterval = 1000 * 1000000; 150 mAverageFPSFrameCount ++; 151 uint64_t inverval = mTimeFrame - mAverageFPSStartTime; 152 if (inverval >= averageFramerateInterval) { 153 inverval = inverval / 1000000; 154 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval; 155 mAverageFPSFrameCount = 0; 156 mAverageFPSStartTime = mTimeFrame; 157 } 158} 159 160void Context::timerSet(Timers tm) { 161 uint64_t last = mTimeLast; 162 mTimeLast = getTime(); 163 mTimers[mTimerActive] += mTimeLast - last; 164 mTimerActive = tm; 165} 166 167void Context::timerPrint() { 168 double total = 0; 169 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { 170 total += mTimers[ct]; 171 } 172 uint64_t frame = mTimeFrame - mTimeLastFrame; 173 mTimeMSLastFrame = frame / 1000000; 174 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; 175 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; 176 177 178 if (props.mLogTimes) { 179 ALOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u", 180 mTimeMSLastFrame, 181 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, 182 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, 183 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, 184 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000, 185 mAverageFPS); 186 } 187} 188 189bool Context::setupCheck() { 190#ifndef RS_COMPATIBILITY_LIB 191 mFragmentStore->setup(this, &mStateFragmentStore); 192 mFragment->setup(this, &mStateFragment); 193 mRaster->setup(this, &mStateRaster); 194 mVertex->setup(this, &mStateVertex); 195 mFBOCache.setup(this); 196#endif 197 return true; 198} 199 200#ifndef RS_COMPATIBILITY_LIB 201void Context::setupProgramStore() { 202 mFragmentStore->setup(this, &mStateFragmentStore); 203} 204#endif 205 206static uint32_t getProp(const char *str) { 207#ifndef RS_SERVER 208 char buf[PROPERTY_VALUE_MAX]; 209 property_get(str, buf, "0"); 210 return atoi(buf); 211#else 212 return 0; 213#endif 214} 215 216void Context::displayDebugStats() { 217#ifndef RS_COMPATIBILITY_LIB 218 char buffer[128]; 219 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript); 220 float oldR, oldG, oldB, oldA; 221 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); 222 uint32_t bufferLen = strlen(buffer); 223 224 ObjectBaseRef<Font> lastFont(getFont()); 225 setFont(NULL); 226 float shadowCol = 0.1f; 227 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); 228 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6); 229 230 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f); 231 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7); 232 233 setFont(lastFont.get()); 234 mStateFont.setFontColor(oldR, oldG, oldB, oldA); 235#endif 236} 237 238bool Context::loadRuntime(const char* filename, Context* rsc) { 239 240 // TODO: store the driverSO somewhere so we can dlclose later 241 void *driverSO = NULL; 242 243 driverSO = dlopen(filename, RTLD_LAZY); 244 if (driverSO == NULL) { 245 ALOGE("Failed loading RS driver: %s", dlerror()); 246 return false; 247 } 248 249 // Need to call dlerror() to clear buffer before using it for dlsym(). 250 (void) dlerror(); 251 typedef bool (*HalSig)(Context*, uint32_t, uint32_t); 252 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit"); 253 254 // If we can't find the C variant, we go looking for the C++ version. 255 if (halInit == NULL) { 256 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror()); 257 halInit = (HalSig) dlsym(driverSO, 258 "_Z10rsdHalInitPN7android12renderscript7ContextEjj"); 259 } 260 261 if (halInit == NULL) { 262 dlclose(driverSO); 263 ALOGE("Failed to find rsdHalInit: %s", dlerror()); 264 return false; 265 } 266 267 if (!(*halInit)(rsc, 0, 0)) { 268 dlclose(driverSO); 269 ALOGE("Hal init failed"); 270 return false; 271 } 272 273 //validate HAL struct 274 275 276 return true; 277} 278 279extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor); 280 281void * Context::threadProc(void *vrsc) { 282 Context *rsc = static_cast<Context *>(vrsc); 283#ifndef ANDROID_RS_SERIALIZE 284 rsc->mNativeThreadId = gettid(); 285#ifndef RS_COMPATIBILITY_LIB 286 if (!rsc->isSynchronous()) { 287 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); 288 } 289 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; 290#else 291 if (!rsc->isSynchronous()) { 292 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4); 293 } 294 rsc->mThreadPriority = -4; 295#endif 296#endif //ANDROID_RS_SERIALIZE 297 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0; 298 rsc->props.mLogScripts = getProp("debug.rs.script") != 0; 299 rsc->props.mLogObjects = getProp("debug.rs.object") != 0; 300 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0; 301 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0; 302 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0; 303 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0; 304 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads"); 305 306 bool loadDefault = true; 307 308 // Provide a mechanism for dropping in a different RS driver. 309#ifndef RS_COMPATIBILITY_LIB 310#ifdef OVERRIDE_RS_DRIVER 311#define XSTR(S) #S 312#define STR(S) XSTR(S) 313#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER) 314 315 if (getProp("debug.rs.default-CPU-driver") != 0) { 316 ALOGE("Skipping override driver and loading default CPU driver"); 317 } else if (rsc->mForceCpu) { 318 ALOGV("Application requested CPU execution"); 319 } else { 320 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) { 321 ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING); 322 loadDefault = false; 323 } else { 324 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING); 325 } 326 } 327 328#undef XSTR 329#undef STR 330#endif // OVERRIDE_RS_DRIVER 331 332 if (loadDefault) { 333 if (!loadRuntime("libRSDriver.so", rsc)) { 334 ALOGE("Failed to load default runtime!"); 335 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver"); 336 return NULL; 337 } 338 } 339#else // RS_COMPATIBILITY_LIB 340 if (rsdHalInit(rsc, 0, 0) != true) { 341 return NULL; 342 } 343#endif 344 345 346 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority); 347 348#ifndef RS_COMPATIBILITY_LIB 349 if (rsc->mIsGraphicsContext) { 350 if (!rsc->initGLThread()) { 351 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL"); 352 return NULL; 353 } 354 355 rsc->mStateRaster.init(rsc); 356 rsc->setProgramRaster(NULL); 357 rsc->mStateVertex.init(rsc); 358 rsc->setProgramVertex(NULL); 359 rsc->mStateFragment.init(rsc); 360 rsc->setProgramFragment(NULL); 361 rsc->mStateFragmentStore.init(rsc); 362 rsc->setProgramStore(NULL); 363 rsc->mStateFont.init(rsc); 364 rsc->setFont(NULL); 365 rsc->mStateSampler.init(rsc); 366 rsc->mFBOCache.init(rsc); 367 } 368#endif 369 370 rsc->mRunning = true; 371 372 if (rsc->isSynchronous()) { 373 return NULL; 374 } 375 376 if (!rsc->mIsGraphicsContext) { 377 while (!rsc->mExit) { 378 rsc->mIO.playCoreCommands(rsc, -1); 379 } 380#ifndef RS_COMPATIBILITY_LIB 381 } else { 382#ifndef ANDROID_RS_SERIALIZE 383 DisplayEventReceiver displayEvent; 384 DisplayEventReceiver::Event eventBuffer[1]; 385#endif 386 int vsyncRate = 0; 387 int targetRate = 0; 388 389 bool drawOnce = false; 390 while (!rsc->mExit) { 391 rsc->timerSet(RS_TIMER_IDLE); 392 393#ifndef ANDROID_RS_SERIALIZE 394 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) { 395 targetRate = 0; 396 } 397 398 if (vsyncRate != targetRate) { 399 displayEvent.setVsyncRate(targetRate); 400 vsyncRate = targetRate; 401 } 402 if (targetRate) { 403 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd()); 404 while (displayEvent.getEvents(eventBuffer, 1) != 0) { 405 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000); 406 } 407 } else 408#endif 409 { 410 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1); 411 } 412 413 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface && 414 (targetRate || drawOnce) && !rsc->mPaused) { 415 416 drawOnce = false; 417 targetRate = ((rsc->runRootScript() + 15) / 16); 418 419 if (rsc->props.mLogVisual) { 420 rsc->displayDebugStats(); 421 } 422 423 rsc->timerSet(RS_TIMER_CLEAR_SWAP); 424 rsc->mHal.funcs.swap(rsc); 425 rsc->timerFrame(); 426 rsc->timerSet(RS_TIMER_INTERNAL); 427 rsc->timerPrint(); 428 rsc->timerReset(); 429 } 430 } 431#endif 432 } 433 434 ALOGV("%p RS Thread exiting", rsc); 435 436#ifndef RS_COMPATIBILITY_LIB 437 if (rsc->mIsGraphicsContext) { 438 pthread_mutex_lock(&gInitMutex); 439 rsc->deinitEGL(); 440 pthread_mutex_unlock(&gInitMutex); 441 } 442#endif 443 444 ALOGV("%p RS Thread exited", rsc); 445 return NULL; 446} 447 448void Context::destroyWorkerThreadResources() { 449 //ALOGV("destroyWorkerThreadResources 1"); 450 ObjectBase::zeroAllUserRef(this); 451#ifndef RS_COMPATIBILITY_LIB 452 if (mIsGraphicsContext) { 453 mRaster.clear(); 454 mFragment.clear(); 455 mVertex.clear(); 456 mFragmentStore.clear(); 457 mFont.clear(); 458 mRootScript.clear(); 459 mStateRaster.deinit(this); 460 mStateVertex.deinit(this); 461 mStateFragment.deinit(this); 462 mStateFragmentStore.deinit(this); 463 mStateFont.deinit(this); 464 mStateSampler.deinit(this); 465 mFBOCache.deinit(this); 466 } 467#endif 468 ObjectBase::freeAllChildren(this); 469 mExit = true; 470 //ALOGV("destroyWorkerThreadResources 2"); 471} 472 473void Context::printWatchdogInfo(void *ctx) { 474 Context *rsc = (Context *)ctx; 475 if (rsc->watchdog.command && rsc->watchdog.file) { 476 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot, 477 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file); 478 } else { 479 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot); 480 } 481} 482 483 484void Context::setPriority(int32_t p) { 485 // Note: If we put this in the proper "background" policy 486 // the wallpapers can become completly unresponsive at times. 487 // This is probably not what we want for something the user is actively 488 // looking at. 489 mThreadPriority = p; 490 setpriority(PRIO_PROCESS, mNativeThreadId, p); 491 mHal.funcs.setPriority(this, mThreadPriority); 492} 493 494Context::Context() { 495 mDev = NULL; 496 mRunning = false; 497 mExit = false; 498 mPaused = false; 499 mObjHead = NULL; 500 mError = RS_ERROR_NONE; 501 mTargetSdkVersion = 14; 502 mDPI = 96; 503 mIsContextLite = false; 504 memset(&watchdog, 0, sizeof(watchdog)); 505 mForceCpu = false; 506 mSynchronous = false; 507} 508 509Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc, 510 bool forceCpu, bool synchronous) { 511 Context * rsc = new Context(); 512 513 rsc->mForceCpu = forceCpu; 514 rsc->mSynchronous = synchronous; 515 516 if (!rsc->initContext(dev, sc)) { 517 delete rsc; 518 return NULL; 519 } 520 return rsc; 521} 522 523Context * Context::createContextLite() { 524 Context * rsc = new Context(); 525 rsc->mIsContextLite = true; 526 return rsc; 527} 528 529bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { 530 pthread_mutex_lock(&gInitMutex); 531 532 mIO.init(); 533 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9); 534 535 dev->addContext(this); 536 mDev = dev; 537 if (sc) { 538 mUserSurfaceConfig = *sc; 539 } else { 540 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); 541 } 542 543 mIsGraphicsContext = sc != NULL; 544 545 int status; 546 pthread_attr_t threadAttr; 547 548 pthread_mutex_unlock(&gInitMutex); 549 550 // Global init done at this point. 551 552 status = pthread_attr_init(&threadAttr); 553 if (status) { 554 ALOGE("Failed to init thread attribute."); 555 return false; 556 } 557 558 mHasSurface = false; 559 560 timerInit(); 561 timerSet(RS_TIMER_INTERNAL); 562 if (mSynchronous) { 563 threadProc(this); 564 } else { 565 status = pthread_create(&mThreadId, &threadAttr, threadProc, this); 566 if (status) { 567 ALOGE("Failed to start rs context thread."); 568 return false; 569 } 570 while (!mRunning && (mError == RS_ERROR_NONE)) { 571 usleep(100); 572 } 573 574 if (mError != RS_ERROR_NONE) { 575 ALOGE("Errors during thread init"); 576 return false; 577 } 578 579 pthread_attr_destroy(&threadAttr); 580 } 581 return true; 582} 583 584Context::~Context() { 585 ALOGV("%p Context::~Context", this); 586 587 if (!mIsContextLite) { 588 mPaused = false; 589 void *res; 590 591 mIO.shutdown(); 592 int status = pthread_join(mThreadId, &res); 593 rsAssert(mExit); 594 595 if (mHal.funcs.shutdownDriver) { 596 mHal.funcs.shutdownDriver(this); 597 } 598 599 // Global structure cleanup. 600 pthread_mutex_lock(&gInitMutex); 601 if (mDev) { 602 mDev->removeContext(this); 603 mDev = NULL; 604 } 605 pthread_mutex_unlock(&gInitMutex); 606 } 607 ALOGV("%p Context::~Context done", this); 608} 609 610#ifndef RS_COMPATIBILITY_LIB 611void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) { 612 rsAssert(mIsGraphicsContext); 613 mHal.funcs.setSurface(this, w, h, sur); 614 615 mHasSurface = sur != NULL; 616 mWidth = w; 617 mHeight = h; 618 619 if (mWidth && mHeight) { 620 mStateVertex.updateSize(this); 621 mFBOCache.updateSize(); 622 } 623} 624 625uint32_t Context::getCurrentSurfaceWidth() const { 626 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) { 627 if (mFBOCache.mHal.state.colorTargets[i] != NULL) { 628 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX(); 629 } 630 } 631 if (mFBOCache.mHal.state.depthTarget != NULL) { 632 return mFBOCache.mHal.state.depthTarget->getType()->getDimX(); 633 } 634 return mWidth; 635} 636 637uint32_t Context::getCurrentSurfaceHeight() const { 638 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) { 639 if (mFBOCache.mHal.state.colorTargets[i] != NULL) { 640 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY(); 641 } 642 } 643 if (mFBOCache.mHal.state.depthTarget != NULL) { 644 return mFBOCache.mHal.state.depthTarget->getType()->getDimY(); 645 } 646 return mHeight; 647} 648 649void Context::pause() { 650 rsAssert(mIsGraphicsContext); 651 mPaused = true; 652} 653 654void Context::resume() { 655 rsAssert(mIsGraphicsContext); 656 mPaused = false; 657} 658 659void Context::setRootScript(Script *s) { 660 rsAssert(mIsGraphicsContext); 661 mRootScript.set(s); 662} 663 664void Context::setProgramStore(ProgramStore *pfs) { 665 rsAssert(mIsGraphicsContext); 666 if (pfs == NULL) { 667 mFragmentStore.set(mStateFragmentStore.mDefault); 668 } else { 669 mFragmentStore.set(pfs); 670 } 671} 672 673void Context::setProgramFragment(ProgramFragment *pf) { 674 rsAssert(mIsGraphicsContext); 675 if (pf == NULL) { 676 mFragment.set(mStateFragment.mDefault); 677 } else { 678 mFragment.set(pf); 679 } 680} 681 682void Context::setProgramRaster(ProgramRaster *pr) { 683 rsAssert(mIsGraphicsContext); 684 if (pr == NULL) { 685 mRaster.set(mStateRaster.mDefault); 686 } else { 687 mRaster.set(pr); 688 } 689} 690 691void Context::setProgramVertex(ProgramVertex *pv) { 692 rsAssert(mIsGraphicsContext); 693 if (pv == NULL) { 694 mVertex.set(mStateVertex.mDefault); 695 } else { 696 mVertex.set(pv); 697 } 698} 699 700void Context::setFont(Font *f) { 701 rsAssert(mIsGraphicsContext); 702 if (f == NULL) { 703 mFont.set(mStateFont.mDefault); 704 } else { 705 mFont.set(f); 706 } 707} 708#endif 709 710void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { 711 rsAssert(!obj->getName()); 712 obj->setName(name, len); 713 mNames.add(obj); 714} 715 716void Context::removeName(ObjectBase *obj) { 717 for (size_t ct=0; ct < mNames.size(); ct++) { 718 if (obj == mNames[ct]) { 719 mNames.removeAt(ct); 720 return; 721 } 722 } 723} 724 725RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) { 726 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID); 727} 728 729RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) { 730 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen); 731} 732 733bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, 734 uint32_t subID, size_t len, bool waitForSpace) const { 735 736 pthread_mutex_lock(&gMessageMutex); 737 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace); 738 pthread_mutex_unlock(&gMessageMutex); 739 return ret; 740} 741 742void Context::initToClient() { 743 while (!mRunning) { 744 usleep(100); 745 } 746} 747 748void Context::deinitToClient() { 749 mIO.clientShutdown(); 750} 751 752void Context::setError(RsError e, const char *msg) const { 753 mError = e; 754 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); 755} 756 757 758void Context::dumpDebug() const { 759 ALOGE("RS Context debug %p", this); 760 ALOGE("RS Context debug"); 761 762 ALOGE(" RS width %i, height %i", mWidth, mHeight); 763 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); 764 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId); 765} 766 767/////////////////////////////////////////////////////////////////////////////////////////// 768// 769 770namespace android { 771namespace renderscript { 772 773void rsi_ContextFinish(Context *rsc) { 774} 775 776void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { 777#ifndef RS_COMPATIBILITY_LIB 778 Script *s = static_cast<Script *>(vs); 779 rsc->setRootScript(s); 780#endif 781} 782 783void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { 784 Sampler *s = static_cast<Sampler *>(vs); 785 786 if (slot > RS_MAX_SAMPLER_SLOT) { 787 ALOGE("Invalid sampler slot"); 788 return; 789 } 790 791 s->bindToContext(&rsc->mStateSampler, slot); 792} 793 794#ifndef RS_COMPATIBILITY_LIB 795void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { 796 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); 797 rsc->setProgramStore(pfs); 798} 799 800void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { 801 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); 802 rsc->setProgramFragment(pf); 803} 804 805void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { 806 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); 807 rsc->setProgramRaster(pr); 808} 809 810void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { 811 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); 812 rsc->setProgramVertex(pv); 813} 814 815void rsi_ContextBindFont(Context *rsc, RsFont vfont) { 816 Font *font = static_cast<Font *>(vfont); 817 rsc->setFont(font); 818} 819#endif 820 821void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) { 822 ObjectBase *ob = static_cast<ObjectBase *>(obj); 823 rsc->assignName(ob, name, name_length); 824} 825 826void rsi_ObjDestroy(Context *rsc, void *optr) { 827 ObjectBase *ob = static_cast<ObjectBase *>(optr); 828 rsc->removeName(ob); 829 ob->decUserRef(); 830} 831 832#ifndef RS_COMPATIBILITY_LIB 833void rsi_ContextPause(Context *rsc) { 834 rsc->pause(); 835} 836 837void rsi_ContextResume(Context *rsc) { 838 rsc->resume(); 839} 840 841void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) { 842 rsc->setSurface(w, h, sur); 843} 844#endif 845 846void rsi_ContextSetPriority(Context *rsc, int32_t p) { 847 rsc->setPriority(p); 848} 849 850void rsi_ContextDump(Context *rsc, int32_t bits) { 851 ObjectBase::dumpAll(rsc); 852} 853 854void rsi_ContextDestroyWorker(Context *rsc) { 855 rsc->destroyWorkerThreadResources(); 856} 857 858void rsi_ContextDestroy(Context *rsc) { 859 ALOGV("%p rsContextDestroy", rsc); 860 rsContextDestroyWorker(rsc); 861 delete rsc; 862 ALOGV("%p rsContextDestroy done", rsc); 863} 864 865 866RsMessageToClientType rsi_ContextPeekMessage(Context *rsc, 867 size_t * receiveLen, size_t receiveLen_length, 868 uint32_t * subID, size_t subID_length) { 869 return rsc->peekMessageToClient(receiveLen, subID); 870} 871 872RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length, 873 size_t * receiveLen, size_t receiveLen_length, 874 uint32_t * subID, size_t subID_length) { 875 rsAssert(subID_length == sizeof(uint32_t)); 876 rsAssert(receiveLen_length == sizeof(size_t)); 877 return rsc->getMessageToClient(data, receiveLen, subID, data_length); 878} 879 880void rsi_ContextInitToClient(Context *rsc) { 881 rsc->initToClient(); 882} 883 884void rsi_ContextDeinitToClient(Context *rsc) { 885 rsc->deinitToClient(); 886} 887 888void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) { 889 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true); 890} 891 892} 893} 894 895RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion, 896 RsContextType ct, bool forceCpu, bool synchronous) { 897 ALOGV("rsContextCreate dev=%p", vdev); 898 Device * dev = static_cast<Device *>(vdev); 899 Context *rsc = Context::createContext(dev, NULL, forceCpu, synchronous); 900 if (rsc) { 901 rsc->setTargetSdkVersion(sdkVersion); 902 } 903 return rsc; 904} 905 906#ifndef RS_COMPATIBILITY_LIB 907RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, 908 uint32_t sdkVersion, RsSurfaceConfig sc, 909 uint32_t dpi) { 910 ALOGV("rsContextCreateGL dev=%p", vdev); 911 Device * dev = static_cast<Device *>(vdev); 912 Context *rsc = Context::createContext(dev, &sc); 913 if (rsc) { 914 rsc->setTargetSdkVersion(sdkVersion); 915 rsc->setDPI(dpi); 916 } 917 ALOGV("%p rsContextCreateGL ret", rsc); 918 return rsc; 919} 920#endif 921 922// Only to be called at a3d load time, before object is visible to user 923// not thread safe 924void rsaGetName(RsContext con, void * obj, const char **name) { 925 ObjectBase *ob = static_cast<ObjectBase *>(obj); 926 (*name) = ob->getName(); 927} 928