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