rsContext.cpp revision 61db21cef81cbc8439b34a8f1d32952339ffc190
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(nullptr); 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 = nullptr; 251 252 driverSO = dlopen(filename, RTLD_LAZY); 253 if (driverSO == nullptr) { 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 == nullptr) { 265 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror()); 266 halInit = (HalSig) dlsym(driverSO, 267 "_Z10rsdHalInitPN7android12renderscript7ContextEjj"); 268 } 269 270 if (halInit == nullptr) { 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 if (getProp("debug.rs.debug") != 0) { 316 ALOGD("Forcing debug context due to debug.rs.debug."); 317 rsc->mContextType = RS_CONTEXT_TYPE_DEBUG; 318 } 319 320 bool loadDefault = true; 321 322 // Provide a mechanism for dropping in a different RS driver. 323#ifndef RS_COMPATIBILITY_LIB 324#ifdef OVERRIDE_RS_DRIVER 325#define XSTR(S) #S 326#define STR(S) XSTR(S) 327#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER) 328 329 if (getProp("debug.rs.default-CPU-driver") != 0) { 330 ALOGD("Skipping override driver and loading default CPU driver"); 331 } else if (rsc->mForceCpu || rsc->mIsGraphicsContext) { 332 ALOGV("Application requested CPU execution"); 333 } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) { 334 ALOGV("Application requested debug context"); 335 } else { 336 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) { 337 ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING); 338 loadDefault = false; 339 } else { 340 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING); 341 } 342 } 343 344#undef XSTR 345#undef STR 346#endif // OVERRIDE_RS_DRIVER 347 348 if (loadDefault) { 349 if (!loadRuntime("libRSDriver.so", rsc)) { 350 ALOGE("Failed to load default runtime!"); 351 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver"); 352 return nullptr; 353 } 354 } 355#else // RS_COMPATIBILITY_LIB 356 if (rsdHalInit(rsc, 0, 0) != true) { 357 return nullptr; 358 } 359#endif 360 361 362 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority); 363 364#ifndef RS_COMPATIBILITY_LIB 365 if (rsc->mIsGraphicsContext) { 366 if (!rsc->initGLThread()) { 367 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL"); 368 return nullptr; 369 } 370 371 rsc->mStateRaster.init(rsc); 372 rsc->setProgramRaster(nullptr); 373 rsc->mStateVertex.init(rsc); 374 rsc->setProgramVertex(nullptr); 375 rsc->mStateFragment.init(rsc); 376 rsc->setProgramFragment(nullptr); 377 rsc->mStateFragmentStore.init(rsc); 378 rsc->setProgramStore(nullptr); 379 rsc->mStateFont.init(rsc); 380 rsc->setFont(nullptr); 381 rsc->mStateSampler.init(rsc); 382 rsc->mFBOCache.init(rsc); 383 } 384#endif 385 386 rsc->mRunning = true; 387 388 if (rsc->isSynchronous()) { 389 return nullptr; 390 } 391 392 if (!rsc->mIsGraphicsContext) { 393 while (!rsc->mExit) { 394 rsc->mIO.playCoreCommands(rsc, -1); 395 } 396#ifndef RS_COMPATIBILITY_LIB 397 } else { 398#ifndef ANDROID_RS_SERIALIZE 399 DisplayEventReceiver displayEvent; 400 DisplayEventReceiver::Event eventBuffer[1]; 401#endif 402 int vsyncRate = 0; 403 int targetRate = 0; 404 405 bool drawOnce = false; 406 while (!rsc->mExit) { 407 rsc->timerSet(RS_TIMER_IDLE); 408 409#ifndef ANDROID_RS_SERIALIZE 410 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) { 411 targetRate = 0; 412 } 413 414 if (vsyncRate != targetRate) { 415 displayEvent.setVsyncRate(targetRate); 416 vsyncRate = targetRate; 417 } 418 if (targetRate) { 419 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd()); 420 while (displayEvent.getEvents(eventBuffer, 1) != 0) { 421 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000); 422 } 423 } else 424#endif 425 { 426 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1); 427 } 428 429 if ((rsc->mRootScript.get() != nullptr) && rsc->mHasSurface && 430 (targetRate || drawOnce) && !rsc->mPaused) { 431 432 drawOnce = false; 433 targetRate = ((rsc->runRootScript() + 15) / 16); 434 435 if (rsc->props.mLogVisual) { 436 rsc->displayDebugStats(); 437 } 438 439 rsc->timerSet(RS_TIMER_CLEAR_SWAP); 440 rsc->mHal.funcs.swap(rsc); 441 rsc->timerFrame(); 442 rsc->timerSet(RS_TIMER_INTERNAL); 443 rsc->timerPrint(); 444 rsc->timerReset(); 445 } 446 } 447#endif 448 } 449 450 //ALOGV("%p RS Thread exiting", rsc); 451 452#ifndef RS_COMPATIBILITY_LIB 453 if (rsc->mIsGraphicsContext) { 454 pthread_mutex_lock(&gInitMutex); 455 rsc->deinitEGL(); 456 pthread_mutex_unlock(&gInitMutex); 457 } 458#endif 459 460 //ALOGV("%p RS Thread exited", rsc); 461 return nullptr; 462} 463 464void Context::destroyWorkerThreadResources() { 465 //ALOGV("destroyWorkerThreadResources 1"); 466 ObjectBase::zeroAllUserRef(this); 467#ifndef RS_COMPATIBILITY_LIB 468 if (mIsGraphicsContext) { 469 mRaster.clear(); 470 mFragment.clear(); 471 mVertex.clear(); 472 mFragmentStore.clear(); 473 mFont.clear(); 474 mRootScript.clear(); 475 mStateRaster.deinit(this); 476 mStateVertex.deinit(this); 477 mStateFragment.deinit(this); 478 mStateFragmentStore.deinit(this); 479 mStateFont.deinit(this); 480 mStateSampler.deinit(this); 481 mFBOCache.deinit(this); 482 } 483#endif 484 ObjectBase::freeAllChildren(this); 485 mExit = true; 486 //ALOGV("destroyWorkerThreadResources 2"); 487} 488 489void Context::printWatchdogInfo(void *ctx) { 490 Context *rsc = (Context *)ctx; 491 if (rsc->watchdog.command && rsc->watchdog.file) { 492 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot, 493 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file); 494 } else { 495 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot); 496 } 497} 498 499 500void Context::setPriority(int32_t p) { 501 // Note: If we put this in the proper "background" policy 502 // the wallpapers can become completly unresponsive at times. 503 // This is probably not what we want for something the user is actively 504 // looking at. 505 mThreadPriority = p; 506 setpriority(PRIO_PROCESS, mNativeThreadId, p); 507 mHal.funcs.setPriority(this, mThreadPriority); 508} 509 510Context::Context() { 511 mDev = nullptr; 512 mRunning = false; 513 mExit = false; 514 mPaused = false; 515 mObjHead = nullptr; 516 mError = RS_ERROR_NONE; 517 mTargetSdkVersion = 14; 518 mDPI = 96; 519 mIsContextLite = false; 520 memset(&watchdog, 0, sizeof(watchdog)); 521 memset(&mHal, 0, sizeof(mHal)); 522 mForceCpu = false; 523 mContextType = RS_CONTEXT_TYPE_NORMAL; 524 mSynchronous = false; 525} 526 527Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc, 528 RsContextType ct, uint32_t flags) { 529 Context * rsc = new Context(); 530 531 if (flags & RS_CONTEXT_LOW_LATENCY) { 532 rsc->mForceCpu = true; 533 } 534 if (flags & RS_CONTEXT_SYNCHRONOUS) { 535 rsc->mSynchronous = true; 536 } 537 rsc->mContextType = ct; 538 rsc->mHal.flags = flags; 539 540 if (!rsc->initContext(dev, sc)) { 541 delete rsc; 542 return nullptr; 543 } 544 return rsc; 545} 546 547Context * Context::createContextLite() { 548 Context * rsc = new Context(); 549 rsc->mIsContextLite = true; 550 return rsc; 551} 552 553bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { 554 pthread_mutex_lock(&gInitMutex); 555 556 mIO.init(); 557 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9); 558 559 dev->addContext(this); 560 mDev = dev; 561 if (sc) { 562 mUserSurfaceConfig = *sc; 563 } else { 564 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); 565 } 566 567 mIsGraphicsContext = sc != nullptr; 568 569 int status; 570 pthread_attr_t threadAttr; 571 572 pthread_mutex_unlock(&gInitMutex); 573 574 // Global init done at this point. 575 576 status = pthread_attr_init(&threadAttr); 577 if (status) { 578 ALOGE("Failed to init thread attribute."); 579 return false; 580 } 581 582 mHasSurface = false; 583 584 timerInit(); 585 timerSet(RS_TIMER_INTERNAL); 586 if (mSynchronous) { 587 threadProc(this); 588 589 if (mError != RS_ERROR_NONE) { 590 ALOGE("Errors during thread init (sync mode)"); 591 return false; 592 } 593 } else { 594 status = pthread_create(&mThreadId, &threadAttr, threadProc, this); 595 if (status) { 596 ALOGE("Failed to start rs context thread."); 597 return false; 598 } 599 while (!mRunning && (mError == RS_ERROR_NONE)) { 600 usleep(100); 601 } 602 603 if (mError != RS_ERROR_NONE) { 604 ALOGE("Errors during thread init"); 605 return false; 606 } 607 608 pthread_attr_destroy(&threadAttr); 609 } 610 return true; 611} 612 613Context::~Context() { 614 //ALOGV("%p Context::~Context", this); 615 616 if (!mIsContextLite) { 617 mPaused = false; 618 void *res; 619 620 mIO.shutdown(); 621 if (!mSynchronous) { 622 pthread_join(mThreadId, &res); 623 } 624 rsAssert(mExit); 625 626 if (mHal.funcs.shutdownDriver && mHal.drv) { 627 mHal.funcs.shutdownDriver(this); 628 } 629 630 // Global structure cleanup. 631 pthread_mutex_lock(&gInitMutex); 632 if (mDev) { 633 mDev->removeContext(this); 634 mDev = nullptr; 635 } 636 pthread_mutex_unlock(&gInitMutex); 637 } 638 //ALOGV("%p Context::~Context done", this); 639} 640 641#ifndef RS_COMPATIBILITY_LIB 642void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) { 643 rsAssert(mIsGraphicsContext); 644 mHal.funcs.setSurface(this, w, h, sur); 645 646 mHasSurface = sur != nullptr; 647 mWidth = w; 648 mHeight = h; 649 650 if (mWidth && mHeight) { 651 mStateVertex.updateSize(this); 652 mFBOCache.updateSize(); 653 } 654} 655 656uint32_t Context::getCurrentSurfaceWidth() const { 657 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) { 658 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) { 659 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX(); 660 } 661 } 662 if (mFBOCache.mHal.state.depthTarget != nullptr) { 663 return mFBOCache.mHal.state.depthTarget->getType()->getDimX(); 664 } 665 return mWidth; 666} 667 668uint32_t Context::getCurrentSurfaceHeight() const { 669 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) { 670 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) { 671 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY(); 672 } 673 } 674 if (mFBOCache.mHal.state.depthTarget != nullptr) { 675 return mFBOCache.mHal.state.depthTarget->getType()->getDimY(); 676 } 677 return mHeight; 678} 679 680void Context::pause() { 681 rsAssert(mIsGraphicsContext); 682 mPaused = true; 683} 684 685void Context::resume() { 686 rsAssert(mIsGraphicsContext); 687 mPaused = false; 688} 689 690void Context::setRootScript(Script *s) { 691 rsAssert(mIsGraphicsContext); 692 mRootScript.set(s); 693} 694 695void Context::setProgramStore(ProgramStore *pfs) { 696 rsAssert(mIsGraphicsContext); 697 if (pfs == nullptr) { 698 mFragmentStore.set(mStateFragmentStore.mDefault); 699 } else { 700 mFragmentStore.set(pfs); 701 } 702} 703 704void Context::setProgramFragment(ProgramFragment *pf) { 705 rsAssert(mIsGraphicsContext); 706 if (pf == nullptr) { 707 mFragment.set(mStateFragment.mDefault); 708 } else { 709 mFragment.set(pf); 710 } 711} 712 713void Context::setProgramRaster(ProgramRaster *pr) { 714 rsAssert(mIsGraphicsContext); 715 if (pr == nullptr) { 716 mRaster.set(mStateRaster.mDefault); 717 } else { 718 mRaster.set(pr); 719 } 720} 721 722void Context::setProgramVertex(ProgramVertex *pv) { 723 rsAssert(mIsGraphicsContext); 724 if (pv == nullptr) { 725 mVertex.set(mStateVertex.mDefault); 726 } else { 727 mVertex.set(pv); 728 } 729} 730 731void Context::setFont(Font *f) { 732 rsAssert(mIsGraphicsContext); 733 if (f == nullptr) { 734 mFont.set(mStateFont.mDefault); 735 } else { 736 mFont.set(f); 737 } 738} 739#endif 740 741void Context::finish() { 742 if (mHal.funcs.finish) { 743 mHal.funcs.finish(this); 744 } 745} 746 747void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { 748 rsAssert(!obj->getName()); 749 obj->setName(name, len); 750 mNames.push_back(obj); 751} 752 753void Context::removeName(ObjectBase *obj) { 754 for (auto nameIter = mNames.begin(), endIter = mNames.end(); 755 nameIter != endIter; nameIter++) { 756 757 if (obj == *nameIter) { 758 mNames.erase(nameIter); 759 return; 760 } 761 } 762} 763 764RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) { 765 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID); 766} 767 768RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) { 769 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen); 770} 771 772bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, 773 uint32_t subID, size_t len, bool waitForSpace) const { 774 775 pthread_mutex_lock(&gMessageMutex); 776 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace); 777 pthread_mutex_unlock(&gMessageMutex); 778 return ret; 779} 780 781void Context::initToClient() { 782 while (!mRunning) { 783 usleep(100); 784 } 785} 786 787void Context::deinitToClient() { 788 mIO.clientShutdown(); 789} 790 791void Context::setError(RsError e, const char *msg) const { 792 mError = e; 793 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); 794} 795 796 797void Context::dumpDebug() const { 798 ALOGE("RS Context debug %p", this); 799 ALOGE("RS Context debug"); 800 801 ALOGE(" RS width %i, height %i", mWidth, mHeight); 802 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); 803 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId); 804} 805 806/////////////////////////////////////////////////////////////////////////////////////////// 807// 808 809namespace android { 810namespace renderscript { 811 812void rsi_ContextFinish(Context *rsc) { 813 rsc->finish(); 814} 815 816void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { 817#ifndef RS_COMPATIBILITY_LIB 818 Script *s = static_cast<Script *>(vs); 819 rsc->setRootScript(s); 820#endif 821} 822 823void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { 824 Sampler *s = static_cast<Sampler *>(vs); 825 826 if (slot > RS_MAX_SAMPLER_SLOT) { 827 ALOGE("Invalid sampler slot"); 828 return; 829 } 830 831 s->bindToContext(&rsc->mStateSampler, slot); 832} 833 834#ifndef RS_COMPATIBILITY_LIB 835void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { 836 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); 837 rsc->setProgramStore(pfs); 838} 839 840void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { 841 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); 842 rsc->setProgramFragment(pf); 843} 844 845void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { 846 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); 847 rsc->setProgramRaster(pr); 848} 849 850void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { 851 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); 852 rsc->setProgramVertex(pv); 853} 854 855void rsi_ContextBindFont(Context *rsc, RsFont vfont) { 856 Font *font = static_cast<Font *>(vfont); 857 rsc->setFont(font); 858} 859#endif 860 861void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) { 862 ObjectBase *ob = static_cast<ObjectBase *>(obj); 863 rsc->assignName(ob, name, name_length); 864} 865 866void rsi_ObjDestroy(Context *rsc, void *optr) { 867 ObjectBase *ob = static_cast<ObjectBase *>(optr); 868 rsc->removeName(ob); 869 ob->decUserRef(); 870} 871 872#ifndef RS_COMPATIBILITY_LIB 873void rsi_ContextPause(Context *rsc) { 874 rsc->pause(); 875} 876 877void rsi_ContextResume(Context *rsc) { 878 rsc->resume(); 879} 880 881void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) { 882 rsc->setSurface(w, h, sur); 883} 884#endif 885 886void rsi_ContextSetPriority(Context *rsc, int32_t p) { 887 rsc->setPriority(p); 888} 889 890void rsi_ContextDump(Context *rsc, int32_t bits) { 891 ObjectBase::dumpAll(rsc); 892} 893 894void rsi_ContextDestroyWorker(Context *rsc) { 895 rsc->destroyWorkerThreadResources(); 896} 897 898void rsi_ContextDestroy(Context *rsc) { 899 //ALOGE("%p rsContextDestroy", rsc); 900 rsContextDestroyWorker(rsc); 901 delete rsc; 902 //ALOGV("%p rsContextDestroy done", rsc); 903} 904 905RsMessageToClientType rsi_ContextPeekMessage(Context *rsc, 906 size_t * receiveLen, size_t receiveLen_length, 907 uint32_t * subID, size_t subID_length) { 908 return rsc->peekMessageToClient(receiveLen, subID); 909} 910 911RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length, 912 size_t * receiveLen, size_t receiveLen_length, 913 uint32_t * subID, size_t subID_length) { 914 rsAssert(subID_length == sizeof(uint32_t)); 915 rsAssert(receiveLen_length == sizeof(size_t)); 916 return rsc->getMessageToClient(data, receiveLen, subID, data_length); 917} 918 919void rsi_ContextInitToClient(Context *rsc) { 920 rsc->initToClient(); 921} 922 923void rsi_ContextDeinitToClient(Context *rsc) { 924 rsc->deinitToClient(); 925} 926 927void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) { 928 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true); 929} 930 931// implementation of handcode LF_ObjDestroy 932// required so nObjDestroy can be run from finalizer without blocking 933void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) { 934 if (((Context *)rsc)->isSynchronous()) { 935 rsi_ObjDestroy((Context *)rsc, objPtr); 936 return; 937 } 938 939 // struct has two parts: 940 // RsPlaybackRemoteHeader (cmdID and bytes) 941 // RS_CMD_ObjDestroy (ptr) 942 struct destroyCmd { 943 uint32_t cmdID; 944 uint32_t bytes; 945 RsAsyncVoidPtr ptr; 946 }; 947 948 destroyCmd cmd; 949 cmd.cmdID = RS_CMD_ID_ObjDestroy; 950 cmd.bytes = sizeof(RsAsyncVoidPtr); 951 cmd.ptr = objPtr; 952 ThreadIO *io = &((Context *)rsc)->mIO; 953 io->coreWrite((void*)&cmd, sizeof(destroyCmd)); 954 955} 956 957} 958} 959 960extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion, 961 RsContextType ct, uint32_t flags) { 962 //ALOGV("rsContextCreate dev=%p", vdev); 963 Device * dev = static_cast<Device *>(vdev); 964 Context *rsc = Context::createContext(dev, nullptr, ct, flags); 965 if (rsc) { 966 rsc->setTargetSdkVersion(sdkVersion); 967 } 968 return rsc; 969} 970 971#ifndef RS_COMPATIBILITY_LIB 972RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, 973 uint32_t sdkVersion, RsSurfaceConfig sc, 974 uint32_t dpi) { 975 //ALOGV("rsContextCreateGL dev=%p", vdev); 976 Device * dev = static_cast<Device *>(vdev); 977 Context *rsc = Context::createContext(dev, &sc); 978 if (rsc) { 979 rsc->setTargetSdkVersion(sdkVersion); 980 rsc->setDPI(dpi); 981 } 982 //ALOGV("%p rsContextCreateGL ret", rsc); 983 return rsc; 984} 985#endif 986 987// Only to be called at a3d load time, before object is visible to user 988// not thread safe 989void rsaGetName(RsContext con, void * obj, const char **name) { 990 ObjectBase *ob = static_cast<ObjectBase *>(obj); 991 (*name) = ob->getName(); 992} 993