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