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