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