Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 "rsDevice.h" |
| 18 | #include "rsContext.h" |
| 19 | #include "rsThreadIO.h" |
Mathias Agopian | 3142f4f | 2009-06-22 18:01:09 -0700 | [diff] [blame] | 20 | #include <ui/FramebufferNativeWindow.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/resource.h> |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 24 | #include <sched.h> |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 25 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| 27 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 28 | #include <cutils/sched_policy.h> |
Jason Sams | f3470ed | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 29 | #include <sys/syscall.h> |
| 30 | #include <string.h> |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 31 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | using namespace android; |
| 33 | using namespace android::renderscript; |
| 34 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 35 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Stephen Hines | 1ac9da6 | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 36 | pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 38 | bool Context::initGLThread() { |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 39 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 40 | |
Jason Sams | 803626f | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 41 | if (!mHal.funcs.initGraphics(this)) { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 42 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 43 | LOGE("%p initGraphics failed", this); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 44 | return false; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 47 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 48 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 51 | void Context::deinitEGL() { |
Jason Sams | 803626f | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 52 | mHal.funcs.shutdownGraphics(this); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 55 | Context::PushState::PushState(Context *con) { |
| 56 | mRsc = con; |
Jason Sams | 07078e3 | 2011-02-23 14:47:17 -0800 | [diff] [blame] | 57 | if (con->mIsGraphicsContext) { |
| 58 | mFragment.set(con->getProgramFragment()); |
| 59 | mVertex.set(con->getProgramVertex()); |
| 60 | mStore.set(con->getProgramStore()); |
| 61 | mRaster.set(con->getProgramRaster()); |
| 62 | mFont.set(con->getFont()); |
| 63 | } |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Context::PushState::~PushState() { |
Jason Sams | 07078e3 | 2011-02-23 14:47:17 -0800 | [diff] [blame] | 67 | if (mRsc->mIsGraphicsContext) { |
| 68 | mRsc->setProgramFragment(mFragment.get()); |
| 69 | mRsc->setProgramVertex(mVertex.get()); |
| 70 | mRsc->setProgramStore(mStore.get()); |
| 71 | mRsc->setProgramRaster(mRaster.get()); |
| 72 | mRsc->setFont(mFont.get()); |
| 73 | } |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 76 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 77 | uint32_t Context::runScript(Script *s) { |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 78 | PushState(this); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 79 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 80 | uint32_t ret = s->run(this); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 81 | return ret; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 84 | uint32_t Context::runRootScript() { |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 85 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 86 | mStateFragmentStore.mLast.clear(); |
Jason Sams | 5316b9e | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 87 | watchdog.inRoot = true; |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 88 | uint32_t ret = runScript(mRootScript.get()); |
Jason Sams | 5316b9e | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 89 | watchdog.inRoot = false; |
Jason Sams | c7412b3 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 90 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 91 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 94 | uint64_t Context::getTime() const { |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 95 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 96 | struct timespec t; |
| 97 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 98 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 99 | #else |
| 100 | return 0; |
| 101 | #endif //ANDROID_RS_SERIALIZE |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 104 | void Context::timerReset() { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 105 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 106 | mTimers[ct] = 0; |
| 107 | } |
| 108 | } |
| 109 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 110 | void Context::timerInit() { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 111 | mTimeLast = getTime(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 112 | mTimeFrame = mTimeLast; |
| 113 | mTimeLastFrame = mTimeLast; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 114 | mTimerActive = RS_TIMER_INTERNAL; |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 115 | mAverageFPSFrameCount = 0; |
| 116 | mAverageFPSStartTime = mTimeLast; |
| 117 | mAverageFPS = 0; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 118 | timerReset(); |
| 119 | } |
| 120 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 121 | void Context::timerFrame() { |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 122 | mTimeLastFrame = mTimeFrame; |
| 123 | mTimeFrame = getTime(); |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 124 | // Update average fps |
| 125 | const uint64_t averageFramerateInterval = 1000 * 1000000; |
| 126 | mAverageFPSFrameCount ++; |
| 127 | uint64_t inverval = mTimeFrame - mAverageFPSStartTime; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 128 | if (inverval >= averageFramerateInterval) { |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 129 | inverval = inverval / 1000000; |
| 130 | mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval; |
| 131 | mAverageFPSFrameCount = 0; |
| 132 | mAverageFPSStartTime = mTimeFrame; |
| 133 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 136 | void Context::timerSet(Timers tm) { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 137 | uint64_t last = mTimeLast; |
| 138 | mTimeLast = getTime(); |
| 139 | mTimers[mTimerActive] += mTimeLast - last; |
| 140 | mTimerActive = tm; |
| 141 | } |
| 142 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 143 | void Context::timerPrint() { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 144 | double total = 0; |
| 145 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 146 | total += mTimers[ct]; |
| 147 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 148 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 149 | mTimeMSLastFrame = frame / 1000000; |
| 150 | mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; |
| 151 | mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 152 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 153 | |
| 154 | if (props.mLogTimes) { |
Alex Sakhartchouk | 98bfe5d | 2010-10-18 17:18:50 -0700 | [diff] [blame] | 155 | LOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u", |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 156 | mTimeMSLastFrame, |
| 157 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, |
| 158 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, |
| 159 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 160 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000, |
| 161 | mAverageFPS); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 162 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 165 | bool Context::setupCheck() { |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 166 | |
Jason Sams | 331bf9b | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 167 | mFragmentStore->setup(this, &mStateFragmentStore); |
Alex Sakhartchouk | 407cae9 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 168 | mFragment->setup(this, &mStateFragment); |
Jason Sams | 331bf9b | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 169 | mRaster->setup(this, &mStateRaster); |
Alex Sakhartchouk | 407cae9 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 170 | mVertex->setup(this, &mStateVertex); |
| 171 | mFBOCache.setup(this); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 172 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 175 | void Context::setupProgramStore() { |
Jason Sams | 331bf9b | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 176 | mFragmentStore->setup(this, &mStateFragmentStore); |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 179 | static bool getProp(const char *str) { |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 180 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 181 | property_get(str, buf, "0"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 182 | return 0 != strcmp(buf, "0"); |
| 183 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 184 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 185 | void Context::displayDebugStats() { |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 186 | char buffer[128]; |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 187 | sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript); |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 188 | float oldR, oldG, oldB, oldA; |
| 189 | mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 190 | uint32_t bufferLen = strlen(buffer); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 191 | |
Alex Sakhartchouk | 20a9354 | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 192 | ObjectBaseRef<Font> lastFont(getFont()); |
| 193 | setFont(NULL); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 194 | float shadowCol = 0.1f; |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 195 | mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 196 | mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 197 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 198 | mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f); |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 199 | mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 200 | |
Alex Sakhartchouk | 20a9354 | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 201 | setFont(lastFont.get()); |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 202 | mStateFont.setFontColor(oldR, oldG, oldB, oldA); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 205 | void * Context::threadProc(void *vrsc) { |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 206 | Context *rsc = static_cast<Context *>(vrsc); |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 207 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 208 | rsc->mNativeThreadId = gettid(); |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 209 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); |
| 210 | rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 211 | #endif //ANDROID_RS_SERIALIZE |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 212 | rsc->props.mLogTimes = getProp("debug.rs.profile"); |
| 213 | rsc->props.mLogScripts = getProp("debug.rs.script"); |
| 214 | rsc->props.mLogObjects = getProp("debug.rs.object"); |
| 215 | rsc->props.mLogShaders = getProp("debug.rs.shader"); |
| 216 | rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes"); |
| 217 | rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms"); |
| 218 | rsc->props.mLogVisual = getProp("debug.rs.visual"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 219 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 220 | if (!rsdHalInit(rsc, 0, 0)) { |
Jason Sams | 2698536 | 2011-05-03 15:01:58 -0700 | [diff] [blame] | 221 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL"); |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 222 | LOGE("Hal init failed"); |
| 223 | return NULL; |
| 224 | } |
| 225 | rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority); |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 226 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 227 | if (rsc->mIsGraphicsContext) { |
Jason Sams | 2698536 | 2011-05-03 15:01:58 -0700 | [diff] [blame] | 228 | if (!rsc->initGLThread()) { |
| 229 | rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL"); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 233 | rsc->mStateRaster.init(rsc); |
| 234 | rsc->setProgramRaster(NULL); |
| 235 | rsc->mStateVertex.init(rsc); |
| 236 | rsc->setProgramVertex(NULL); |
| 237 | rsc->mStateFragment.init(rsc); |
| 238 | rsc->setProgramFragment(NULL); |
| 239 | rsc->mStateFragmentStore.init(rsc); |
| 240 | rsc->setProgramStore(NULL); |
| 241 | rsc->mStateFont.init(rsc); |
| 242 | rsc->setFont(NULL); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 243 | rsc->mStateSampler.init(rsc); |
Alex Sakhartchouk | 2f6964f | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 244 | rsc->mFBOCache.init(rsc); |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 245 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 246 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 247 | rsc->mRunning = true; |
| 248 | bool mDraw = true; |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 249 | bool doWait = true; |
| 250 | |
| 251 | uint64_t targetTime = rsc->getTime(); |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 252 | while (!rsc->mExit) { |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 253 | uint64_t waitTime = 0; |
| 254 | uint64_t now = rsc->getTime(); |
Jason Sams | 85deb78 | 2011-08-17 17:15:23 -0700 | [diff] [blame] | 255 | if (!doWait) { |
| 256 | if (now < targetTime) { |
| 257 | waitTime = targetTime - now; |
| 258 | doWait = true; |
| 259 | } |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | mDraw |= rsc->mIO.playCoreCommands(rsc, doWait, waitTime); |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 263 | mDraw &= (rsc->mRootScript.get() != NULL); |
Alex Sakhartchouk | 6c72eec | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 264 | mDraw &= rsc->mHasSurface; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 265 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 266 | if (mDraw && rsc->mIsGraphicsContext) { |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 267 | uint64_t delay = rsc->runRootScript() * 1000000; |
| 268 | targetTime = rsc->getTime() + delay; |
Jason Sams | 85deb78 | 2011-08-17 17:15:23 -0700 | [diff] [blame] | 269 | doWait = (delay == 0); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 270 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 271 | if (rsc->props.mLogVisual) { |
| 272 | rsc->displayDebugStats(); |
| 273 | } |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 274 | |
Jason Sams | bfc7891 | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 275 | mDraw = !rsc->mPaused; |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 276 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
| 277 | rsc->mHal.funcs.swap(rsc); |
| 278 | rsc->timerFrame(); |
| 279 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 280 | rsc->timerPrint(); |
| 281 | rsc->timerReset(); |
| 282 | } |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 283 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 284 | |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 285 | LOGV("%p RS Thread exiting", rsc); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 286 | |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 287 | if (rsc->mIsGraphicsContext) { |
| 288 | pthread_mutex_lock(&gInitMutex); |
| 289 | rsc->deinitEGL(); |
| 290 | pthread_mutex_unlock(&gInitMutex); |
| 291 | } |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 292 | |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 293 | LOGV("%p RS Thread exited", rsc); |
Jason Sams | be8ac6a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 294 | return NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 297 | void Context::destroyWorkerThreadResources() { |
Jason Sams | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 298 | //LOGV("destroyWorkerThreadResources 1"); |
Jason Sams | 38f8d9d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 299 | ObjectBase::zeroAllUserRef(this); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 300 | if (mIsGraphicsContext) { |
| 301 | mRaster.clear(); |
| 302 | mFragment.clear(); |
| 303 | mVertex.clear(); |
| 304 | mFragmentStore.clear(); |
| 305 | mFont.clear(); |
| 306 | mRootScript.clear(); |
| 307 | mStateRaster.deinit(this); |
| 308 | mStateVertex.deinit(this); |
| 309 | mStateFragment.deinit(this); |
| 310 | mStateFragmentStore.deinit(this); |
| 311 | mStateFont.deinit(this); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 312 | mStateSampler.deinit(this); |
Alex Sakhartchouk | 2f6964f | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 313 | mFBOCache.deinit(this); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 314 | } |
Jason Sams | 777ec26 | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 315 | ObjectBase::freeAllChildren(this); |
Jason Sams | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 316 | //LOGV("destroyWorkerThreadResources 2"); |
Jason Sams | 84035ff | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 317 | mExit = true; |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Jason Sams | 5316b9e | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 320 | void Context::printWatchdogInfo(void *ctx) { |
| 321 | Context *rsc = (Context *)ctx; |
| 322 | LOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot, |
| 323 | rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file); |
| 324 | } |
| 325 | |
| 326 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 327 | void Context::setPriority(int32_t p) { |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 328 | // Note: If we put this in the proper "background" policy |
| 329 | // the wallpapers can become completly unresponsive at times. |
| 330 | // This is probably not what we want for something the user is actively |
| 331 | // looking at. |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 332 | mThreadPriority = p; |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 333 | #if 0 |
| 334 | SchedPolicy pol = SP_FOREGROUND; |
| 335 | if (p > 0) { |
| 336 | pol = SP_BACKGROUND; |
| 337 | } |
| 338 | if (!set_sched_policy(mNativeThreadId, pol)) { |
| 339 | // success; reset the priority as well |
| 340 | } |
| 341 | #else |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 342 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 343 | #endif |
| 344 | } |
| 345 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 346 | Context::Context() { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 347 | mDev = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 348 | mRunning = false; |
| 349 | mExit = false; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 350 | mPaused = false; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 351 | mObjHead = NULL; |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 352 | mError = RS_ERROR_NONE; |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 353 | mTargetSdkVersion = 14; |
Alex Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 354 | mDPI = 96; |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 355 | mIsContextLite = false; |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 358 | Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 359 | Context * rsc = new Context(); |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 360 | |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 361 | if (!rsc->initContext(dev, sc)) { |
| 362 | delete rsc; |
| 363 | return NULL; |
| 364 | } |
| 365 | return rsc; |
| 366 | } |
| 367 | |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 368 | Context * Context::createContextLite() { |
| 369 | Context * rsc = new Context(); |
| 370 | rsc->mIsContextLite = true; |
| 371 | return rsc; |
| 372 | } |
| 373 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 374 | bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 375 | pthread_mutex_lock(&gInitMutex); |
| 376 | |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 377 | mIO.init(); |
Jason Sams | 5316b9e | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 378 | mIO.setTimoutCallback(printWatchdogInfo, this, 2e9); |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 379 | |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 380 | dev->addContext(this); |
| 381 | mDev = dev; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 382 | if (sc) { |
| 383 | mUserSurfaceConfig = *sc; |
| 384 | } else { |
| 385 | memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); |
| 386 | } |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 387 | |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 388 | mIsGraphicsContext = sc != NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 389 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 390 | int status; |
| 391 | pthread_attr_t threadAttr; |
| 392 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 393 | pthread_mutex_unlock(&gInitMutex); |
| 394 | |
| 395 | // Global init done at this point. |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 396 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 397 | status = pthread_attr_init(&threadAttr); |
| 398 | if (status) { |
| 399 | LOGE("Failed to init thread attribute."); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 400 | return false; |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Alex Sakhartchouk | 6c72eec | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 403 | mHasSurface = false; |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 404 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 405 | timerInit(); |
Jason Sams | d3f2eaf | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 406 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 407 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 408 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 409 | if (status) { |
| 410 | LOGE("Failed to start rs context thread."); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 411 | return false; |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 412 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 413 | while (!mRunning && (mError == RS_ERROR_NONE)) { |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 414 | usleep(100); |
| 415 | } |
| 416 | |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 417 | if (mError != RS_ERROR_NONE) { |
Jason Sams | 80e29cf | 2011-03-18 17:08:54 -0700 | [diff] [blame] | 418 | LOGE("Errors during thread init"); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 422 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 423 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 426 | Context::~Context() { |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 427 | LOGV("%p Context::~Context", this); |
Jason Sams | 84035ff | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 428 | |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 429 | if (!mIsContextLite) { |
| 430 | mIO.coreFlush(); |
| 431 | rsAssert(mExit); |
| 432 | mExit = true; |
| 433 | mPaused = false; |
| 434 | void *res; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 435 | |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 436 | mIO.shutdown(); |
| 437 | int status = pthread_join(mThreadId, &res); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 438 | |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 439 | if (mHal.funcs.shutdownDriver) { |
| 440 | mHal.funcs.shutdownDriver(this); |
| 441 | } |
| 442 | |
| 443 | // Global structure cleanup. |
| 444 | pthread_mutex_lock(&gInitMutex); |
| 445 | if (mDev) { |
| 446 | mDev->removeContext(this); |
| 447 | mDev = NULL; |
| 448 | } |
| 449 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 03855bb | 2011-01-25 00:26:25 -0800 | [diff] [blame] | 450 | } |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 451 | LOGV("%p Context::~Context done", this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Alex Sakhartchouk | 6c72eec | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 454 | void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 455 | rsAssert(mIsGraphicsContext); |
Jason Sams | 803626f | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 456 | mHal.funcs.setSurface(this, w, h, sur); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 457 | |
Alex Sakhartchouk | 6c72eec | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 458 | mHasSurface = sur != NULL; |
Jason Sams | 803626f | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 459 | mWidth = w; |
| 460 | mHeight = h; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 461 | |
Jason Sams | 803626f | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 462 | if (mWidth && mHeight) { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 463 | mStateVertex.updateSize(this); |
Alex Sakhartchouk | 10ed04935 | 2011-07-19 17:50:29 -0700 | [diff] [blame] | 464 | mFBOCache.updateSize(); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 468 | void Context::pause() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 469 | rsAssert(mIsGraphicsContext); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 470 | mPaused = true; |
| 471 | } |
| 472 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 473 | void Context::resume() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 474 | rsAssert(mIsGraphicsContext); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 475 | mPaused = false; |
| 476 | } |
| 477 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 478 | void Context::setRootScript(Script *s) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 479 | rsAssert(mIsGraphicsContext); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 480 | mRootScript.set(s); |
| 481 | } |
| 482 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 483 | void Context::setProgramStore(ProgramStore *pfs) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 484 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 485 | if (pfs == NULL) { |
| 486 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 487 | } else { |
| 488 | mFragmentStore.set(pfs); |
| 489 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 492 | void Context::setProgramFragment(ProgramFragment *pf) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 493 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 494 | if (pf == NULL) { |
| 495 | mFragment.set(mStateFragment.mDefault); |
| 496 | } else { |
| 497 | mFragment.set(pf); |
| 498 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 501 | void Context::setProgramRaster(ProgramRaster *pr) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 502 | rsAssert(mIsGraphicsContext); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 503 | if (pr == NULL) { |
| 504 | mRaster.set(mStateRaster.mDefault); |
| 505 | } else { |
| 506 | mRaster.set(pr); |
| 507 | } |
| 508 | } |
| 509 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 510 | void Context::setProgramVertex(ProgramVertex *pv) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 511 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 512 | if (pv == NULL) { |
| 513 | mVertex.set(mStateVertex.mDefault); |
| 514 | } else { |
| 515 | mVertex.set(pv); |
| 516 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 519 | void Context::setFont(Font *f) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 520 | rsAssert(mIsGraphicsContext); |
| 521 | if (f == NULL) { |
| 522 | mFont.set(mStateFont.mDefault); |
| 523 | } else { |
| 524 | mFont.set(f); |
| 525 | } |
| 526 | } |
| 527 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 528 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 529 | rsAssert(!obj->getName()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 530 | obj->setName(name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 531 | mNames.add(obj); |
| 532 | } |
| 533 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 534 | void Context::removeName(ObjectBase *obj) { |
| 535 | for (size_t ct=0; ct < mNames.size(); ct++) { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 536 | if (obj == mNames[ct]) { |
| 537 | mNames.removeAt(ct); |
| 538 | return; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 543 | RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) { |
| 544 | return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 547 | RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) { |
| 548 | return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 551 | bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, |
| 552 | uint32_t subID, size_t len, bool waitForSpace) const { |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 553 | |
| 554 | return mIO.sendToClient(cmdID, subID, data, len, waitForSpace); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 557 | void Context::initToClient() { |
| 558 | while (!mRunning) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 559 | usleep(100); |
| 560 | } |
| 561 | } |
| 562 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 563 | void Context::deinitToClient() { |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 564 | mIO.clientShutdown(); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 565 | } |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 566 | |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 567 | void Context::setError(RsError e, const char *msg) const { |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 568 | mError = e; |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 569 | sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 573 | void Context::dumpDebug() const { |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 574 | LOGE("RS Context debug %p", this); |
| 575 | LOGE("RS Context debug"); |
| 576 | |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 577 | LOGE(" RS width %i, height %i", mWidth, mHeight); |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 578 | LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 579 | LOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 580 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 581 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 582 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 583 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 584 | |
| 585 | namespace android { |
| 586 | namespace renderscript { |
| 587 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 588 | void rsi_ContextFinish(Context *rsc) { |
Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 589 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 590 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 591 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 592 | Script *s = static_cast<Script *>(vs); |
| 593 | rsc->setRootScript(s); |
| 594 | } |
| 595 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 596 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 597 | Sampler *s = static_cast<Sampler *>(vs); |
| 598 | |
| 599 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 600 | LOGE("Invalid sampler slot"); |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | s->bindToContext(&rsc->mStateSampler, slot); |
| 605 | } |
| 606 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 607 | void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 608 | ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 609 | rsc->setProgramStore(pfs); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 612 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 613 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 614 | rsc->setProgramFragment(pf); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 617 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 618 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 619 | rsc->setProgramRaster(pr); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 622 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 623 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 624 | rsc->setProgramVertex(pv); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 627 | void rsi_ContextBindFont(Context *rsc, RsFont vfont) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 628 | Font *font = static_cast<Font *>(vfont); |
| 629 | rsc->setFont(font); |
| 630 | } |
| 631 | |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 632 | void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 633 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 634 | rsc->assignName(ob, name, name_length); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 635 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 636 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 637 | void rsi_ObjDestroy(Context *rsc, void *optr) { |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 638 | ObjectBase *ob = static_cast<ObjectBase *>(optr); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 639 | rsc->removeName(ob); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 640 | ob->decUserRef(); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 643 | void rsi_ContextPause(Context *rsc) { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 644 | rsc->pause(); |
| 645 | } |
| 646 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 647 | void rsi_ContextResume(Context *rsc) { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 648 | rsc->resume(); |
| 649 | } |
| 650 | |
Alex Sakhartchouk | 6c72eec | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 651 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) { |
Mathias Agopian | 128ce4b | 2010-02-12 14:04:35 -0800 | [diff] [blame] | 652 | rsc->setSurface(w, h, sur); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 655 | void rsi_ContextSetPriority(Context *rsc, int32_t p) { |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 656 | rsc->setPriority(p); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 659 | void rsi_ContextDump(Context *rsc, int32_t bits) { |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 660 | ObjectBase::dumpAll(rsc); |
| 661 | } |
| 662 | |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 663 | void rsi_ContextDestroyWorker(Context *rsc) { |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 664 | rsc->destroyWorkerThreadResources(); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 667 | void rsi_ContextDestroy(Context *rsc) { |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 668 | LOGV("%p rsContextDestroy", rsc); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 669 | rsContextDestroyWorker(rsc); |
Jason Sams | 546f01b | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 670 | delete rsc; |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 671 | LOGV("%p rsContextDestroy done", rsc); |
Jason Sams | 546f01b | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 674 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 675 | RsMessageToClientType rsi_ContextPeekMessage(Context *rsc, |
Jason Sams | 65bdaf1 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 676 | size_t * receiveLen, size_t receiveLen_length, |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 677 | uint32_t * subID, size_t subID_length) { |
| 678 | return rsc->peekMessageToClient(receiveLen, subID); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 681 | RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length, |
Jason Sams | 65bdaf1 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 682 | size_t * receiveLen, size_t receiveLen_length, |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 683 | uint32_t * subID, size_t subID_length) { |
Jason Sams | 65bdaf1 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 684 | rsAssert(subID_length == sizeof(uint32_t)); |
| 685 | rsAssert(receiveLen_length == sizeof(size_t)); |
Jason Sams | edbfabd | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 686 | return rsc->getMessageToClient(data, receiveLen, subID, data_length); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 689 | void rsi_ContextInitToClient(Context *rsc) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 690 | rsc->initToClient(); |
| 691 | } |
| 692 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 693 | void rsi_ContextDeinitToClient(Context *rsc) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 694 | rsc->deinitToClient(); |
| 695 | } |
| 696 | |
Jason Sams | c576537 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 700 | RsContext rsContextCreate(RsDevice vdev, uint32_t version, |
| 701 | uint32_t sdkVersion) { |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 702 | LOGV("rsContextCreate dev=%p", vdev); |
Jason Sams | d9d37cc | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 703 | Device * dev = static_cast<Device *>(vdev); |
| 704 | Context *rsc = Context::createContext(dev, NULL); |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 705 | if (rsc) { |
| 706 | rsc->setTargetSdkVersion(sdkVersion); |
| 707 | } |
Jason Sams | d9d37cc | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 708 | return rsc; |
| 709 | } |
| 710 | |
| 711 | RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 712 | uint32_t sdkVersion, RsSurfaceConfig sc, |
| 713 | uint32_t dpi) { |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 714 | LOGV("rsContextCreateGL dev=%p", vdev); |
Jason Sams | d9d37cc | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 715 | Device * dev = static_cast<Device *>(vdev); |
| 716 | Context *rsc = Context::createContext(dev, &sc); |
Jason Sams | fb06b7a | 2011-07-13 16:09:42 -0700 | [diff] [blame] | 717 | if (rsc) { |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 718 | rsc->setTargetSdkVersion(sdkVersion); |
Jason Sams | fb06b7a | 2011-07-13 16:09:42 -0700 | [diff] [blame] | 719 | rsc->setDPI(dpi); |
| 720 | } |
Jason Sams | 8410b14 | 2011-09-20 16:59:22 -0700 | [diff] [blame] | 721 | LOGV("%p rsContextCreateGL ret", rsc); |
Jason Sams | d9d37cc | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 722 | return rsc; |
| 723 | } |
| 724 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 725 | // Only to be called at a3d load time, before object is visible to user |
| 726 | // not thread safe |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 727 | void rsaGetName(RsContext con, void * obj, const char **name) { |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 728 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 729 | (*name) = ob->getName(); |
| 730 | } |