Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jason Sams | 326e0dd | 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 | 5ae678f | 2009-06-22 18:01:09 -0700 | [diff] [blame] | 20 | #include <ui/FramebufferNativeWindow.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/resource.h> |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 24 | #include <sched.h> |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 25 | |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| 27 | |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 28 | #include <cutils/sched_policy.h> |
Jason Sams | 8d957fa | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 29 | #include <sys/syscall.h> |
| 30 | #include <string.h> |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 31 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | using namespace android; |
| 33 | using namespace android::renderscript; |
| 34 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 35 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 36 | pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 38 | bool Context::initGLThread() { |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 39 | pthread_mutex_lock(&gInitMutex); |
| 40 | LOGV("initGLThread start %p", this); |
| 41 | |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 42 | if (!mHal.funcs.initGraphics(this)) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 43 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 44 | LOGE("%p, initGraphics failed", this); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 45 | return false; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 48 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 49 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 52 | void Context::deinitEGL() { |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 53 | LOGV("%p, deinitEGL", this); |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 54 | mHal.funcs.shutdownGraphics(this); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 57 | Context::PushState::PushState(Context *con) { |
| 58 | mRsc = con; |
Jason Sams | c946b61 | 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 | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | Context::PushState::~PushState() { |
Jason Sams | c946b61 | 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 | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 78 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 79 | uint32_t Context::runScript(Script *s) { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 80 | PushState(this); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 81 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 82 | uint32_t ret = s->run(this); |
Jason Sams | c9d43db | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 83 | return ret; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | uint32_t Context::runRootScript() { |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 87 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 88 | mStateFragmentStore.mLast.clear(); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 89 | uint32_t ret = runScript(mRootScript.get()); |
Jason Sams | 8cfdd24 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 90 | |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 91 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 94 | uint64_t Context::getTime() const { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 95 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | 24371d9 | 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 | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 99 | #else |
| 100 | return 0; |
| 101 | #endif //ANDROID_RS_SERIALIZE |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 104 | void Context::timerReset() { |
Jason Sams | 24371d9 | 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 | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 110 | void Context::timerInit() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 111 | mTimeLast = getTime(); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 112 | mTimeFrame = mTimeLast; |
| 113 | mTimeLastFrame = mTimeLast; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 114 | mTimerActive = RS_TIMER_INTERNAL; |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 115 | mAverageFPSFrameCount = 0; |
| 116 | mAverageFPSStartTime = mTimeLast; |
| 117 | mAverageFPS = 0; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 118 | timerReset(); |
| 119 | } |
| 120 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 121 | void Context::timerFrame() { |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 122 | mTimeLastFrame = mTimeFrame; |
| 123 | mTimeFrame = getTime(); |
Alex Sakhartchouk | c8fb69e | 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 | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 128 | if (inverval >= averageFramerateInterval) { |
Alex Sakhartchouk | c8fb69e | 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 | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 136 | void Context::timerSet(Timers tm) { |
Jason Sams | 24371d9 | 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 | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 143 | void Context::timerPrint() { |
Jason Sams | 24371d9 | 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 | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 148 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | 2dca84d | 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 | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 152 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 153 | |
| 154 | if (props.mLogTimes) { |
Alex Sakhartchouk | 64cd98e | 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 | 2dca84d | 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 | c8fb69e | 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 | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 162 | } |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 165 | bool Context::setupCheck() { |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 166 | |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 167 | mFragmentStore->setup(this, &mStateFragmentStore); |
Alex Sakhartchouk | c19ff01 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 168 | mFragment->setup(this, &mStateFragment); |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 169 | mRaster->setup(this, &mStateRaster); |
Alex Sakhartchouk | c19ff01 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 170 | mVertex->setup(this, &mStateVertex); |
| 171 | mFBOCache.setup(this); |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 172 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 175 | void Context::setupProgramStore() { |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 176 | mFragmentStore->setup(this, &mStateFragmentStore); |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 179 | static bool getProp(const char *str) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 180 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 181 | property_get(str, buf, "0"); |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 182 | return 0 != strcmp(buf, "0"); |
| 183 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 184 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 185 | void Context::displayDebugStats() { |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 186 | char buffer[128]; |
Alex Sakhartchouk | c8fb69e | 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 | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 188 | float oldR, oldG, oldB, oldA; |
| 189 | mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 190 | uint32_t bufferLen = strlen(buffer); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 191 | |
Alex Sakhartchouk | 1809bde | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 192 | ObjectBaseRef<Font> lastFont(getFont()); |
| 193 | setFont(NULL); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 194 | float shadowCol = 0.1f; |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 195 | mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 196 | mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 197 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 198 | mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 199 | mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 200 | |
Alex Sakhartchouk | 1809bde | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 201 | setFont(lastFont.get()); |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 202 | mStateFont.setFontColor(oldR, oldG, oldB, oldA); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 205 | void * Context::threadProc(void *vrsc) { |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 206 | Context *rsc = static_cast<Context *>(vrsc); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 207 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 208 | rsc->mNativeThreadId = gettid(); |
Jason Sams | 83c451a | 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 | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 211 | #endif //ANDROID_RS_SERIALIZE |
Jason Sams | 83c451a | 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 | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 219 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 220 | if (!rsdHalInit(rsc, 0, 0)) { |
Jason Sams | d3e7107 | 2011-05-03 15:01:58 -0700 | [diff] [blame] | 221 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL"); |
Jason Sams | 83c451a | 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 | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 226 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 227 | if (rsc->mIsGraphicsContext) { |
Jason Sams | d3e7107 | 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 | 83c451a | 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 | da6d34a | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 243 | rsc->mFBOCache.init(rsc); |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 244 | } |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 245 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 246 | rsc->mRunning = true; |
| 247 | bool mDraw = true; |
| 248 | while (!rsc->mExit) { |
| 249 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
| 250 | mDraw &= (rsc->mRootScript.get() != NULL); |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 251 | mDraw &= rsc->mHasSurface; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 252 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 253 | uint32_t targetTime = 0; |
| 254 | if (mDraw && rsc->mIsGraphicsContext) { |
| 255 | targetTime = rsc->runRootScript(); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 256 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 257 | if (rsc->props.mLogVisual) { |
| 258 | rsc->displayDebugStats(); |
| 259 | } |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 260 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 261 | mDraw = targetTime && !rsc->mPaused; |
| 262 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
| 263 | rsc->mHal.funcs.swap(rsc); |
| 264 | rsc->timerFrame(); |
| 265 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 266 | rsc->timerPrint(); |
| 267 | rsc->timerReset(); |
| 268 | } |
| 269 | if (targetTime > 1) { |
| 270 | int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000; |
| 271 | if (t > 0) { |
| 272 | usleep(t); |
| 273 | } |
| 274 | } |
| 275 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 276 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 277 | LOGV("%p, RS Thread exiting", rsc); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 278 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 279 | if (rsc->mIsGraphicsContext) { |
| 280 | pthread_mutex_lock(&gInitMutex); |
| 281 | rsc->deinitEGL(); |
| 282 | pthread_mutex_unlock(&gInitMutex); |
| 283 | } |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 284 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 285 | LOGV("%p, RS Thread exited", rsc); |
| 286 | return NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 289 | void Context::destroyWorkerThreadResources() { |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 290 | //LOGV("destroyWorkerThreadResources 1"); |
Jason Sams | 2e8665d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 291 | ObjectBase::zeroAllUserRef(this); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 292 | if (mIsGraphicsContext) { |
| 293 | mRaster.clear(); |
| 294 | mFragment.clear(); |
| 295 | mVertex.clear(); |
| 296 | mFragmentStore.clear(); |
| 297 | mFont.clear(); |
| 298 | mRootScript.clear(); |
| 299 | mStateRaster.deinit(this); |
| 300 | mStateVertex.deinit(this); |
| 301 | mStateFragment.deinit(this); |
| 302 | mStateFragmentStore.deinit(this); |
| 303 | mStateFont.deinit(this); |
Alex Sakhartchouk | da6d34a | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 304 | mFBOCache.deinit(this); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 305 | } |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 306 | //LOGV("destroyWorkerThreadResources 2"); |
Jason Sams | cf912de | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 307 | mExit = true; |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 310 | void Context::setPriority(int32_t p) { |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 311 | // Note: If we put this in the proper "background" policy |
| 312 | // the wallpapers can become completly unresponsive at times. |
| 313 | // This is probably not what we want for something the user is actively |
| 314 | // looking at. |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 315 | mThreadPriority = p; |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 316 | #if 0 |
| 317 | SchedPolicy pol = SP_FOREGROUND; |
| 318 | if (p > 0) { |
| 319 | pol = SP_BACKGROUND; |
| 320 | } |
| 321 | if (!set_sched_policy(mNativeThreadId, pol)) { |
| 322 | // success; reset the priority as well |
| 323 | } |
| 324 | #else |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 325 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 326 | #endif |
| 327 | } |
| 328 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 329 | Context::Context() { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 330 | mDev = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 331 | mRunning = false; |
| 332 | mExit = false; |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 333 | mPaused = false; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 334 | mObjHead = NULL; |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 335 | mError = RS_ERROR_NONE; |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame^] | 336 | mTargetSdkVersion = 14; |
Alex Sakhartchouk | 7b3e9bd | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 337 | mDPI = 96; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 338 | mIsContextLite = false; |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 341 | Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 342 | Context * rsc = new Context(); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 343 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 344 | if (!rsc->initContext(dev, sc)) { |
| 345 | delete rsc; |
| 346 | return NULL; |
| 347 | } |
| 348 | return rsc; |
| 349 | } |
| 350 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 351 | Context * Context::createContextLite() { |
| 352 | Context * rsc = new Context(); |
| 353 | rsc->mIsContextLite = true; |
| 354 | return rsc; |
| 355 | } |
| 356 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 357 | bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 358 | pthread_mutex_lock(&gInitMutex); |
| 359 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 360 | mIO.init(); |
| 361 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 362 | dev->addContext(this); |
| 363 | mDev = dev; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 364 | if (sc) { |
| 365 | mUserSurfaceConfig = *sc; |
| 366 | } else { |
| 367 | memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); |
| 368 | } |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 369 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 370 | mIsGraphicsContext = sc != NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 371 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 372 | int status; |
| 373 | pthread_attr_t threadAttr; |
| 374 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 375 | pthread_mutex_unlock(&gInitMutex); |
| 376 | |
| 377 | // Global init done at this point. |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 378 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 379 | status = pthread_attr_init(&threadAttr); |
| 380 | if (status) { |
| 381 | LOGE("Failed to init thread attribute."); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 382 | return false; |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 385 | mHasSurface = false; |
Jason Sams | 992a0b7 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 386 | |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 387 | timerInit(); |
Jason Sams | a891933 | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 388 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 389 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 390 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 391 | if (status) { |
| 392 | LOGE("Failed to start rs context thread."); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 393 | return false; |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 394 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 395 | while (!mRunning && (mError == RS_ERROR_NONE)) { |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 396 | usleep(100); |
| 397 | } |
| 398 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 399 | if (mError != RS_ERROR_NONE) { |
Jason Sams | 2cfe51e | 2011-03-18 17:08:54 -0700 | [diff] [blame] | 400 | LOGE("Errors during thread init"); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 401 | return false; |
| 402 | } |
| 403 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 404 | pthread_attr_destroy(&threadAttr); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 405 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 408 | Context::~Context() { |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 409 | LOGV("Context::~Context"); |
Jason Sams | cf912de | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 410 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 411 | if (!mIsContextLite) { |
| 412 | mIO.coreFlush(); |
| 413 | rsAssert(mExit); |
| 414 | mExit = true; |
| 415 | mPaused = false; |
| 416 | void *res; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 417 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 418 | mIO.shutdown(); |
| 419 | int status = pthread_join(mThreadId, &res); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 420 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 421 | if (mHal.funcs.shutdownDriver) { |
| 422 | mHal.funcs.shutdownDriver(this); |
| 423 | } |
| 424 | |
| 425 | // Global structure cleanup. |
| 426 | pthread_mutex_lock(&gInitMutex); |
| 427 | if (mDev) { |
| 428 | mDev->removeContext(this); |
| 429 | mDev = NULL; |
| 430 | } |
| 431 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 51462c5 | 2011-01-25 00:26:25 -0800 | [diff] [blame] | 432 | } |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 433 | LOGV("Context::~Context done"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 436 | void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 437 | rsAssert(mIsGraphicsContext); |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 438 | mHal.funcs.setSurface(this, w, h, sur); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 439 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 440 | mHasSurface = sur != NULL; |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 441 | mWidth = w; |
| 442 | mHeight = h; |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 443 | |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 444 | if (mWidth && mHeight) { |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 445 | mStateVertex.updateSize(this); |
Alex Sakhartchouk | a544b63 | 2011-07-19 17:50:29 -0700 | [diff] [blame] | 446 | mFBOCache.updateSize(); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 450 | void Context::pause() { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 451 | rsAssert(mIsGraphicsContext); |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 452 | mPaused = true; |
| 453 | } |
| 454 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 455 | void Context::resume() { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 456 | rsAssert(mIsGraphicsContext); |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 457 | mPaused = false; |
| 458 | } |
| 459 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 460 | void Context::setRootScript(Script *s) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 461 | rsAssert(mIsGraphicsContext); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 462 | mRootScript.set(s); |
| 463 | } |
| 464 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 465 | void Context::setProgramStore(ProgramStore *pfs) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 466 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 467 | if (pfs == NULL) { |
| 468 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 469 | } else { |
| 470 | mFragmentStore.set(pfs); |
| 471 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 474 | void Context::setProgramFragment(ProgramFragment *pf) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 475 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 476 | if (pf == NULL) { |
| 477 | mFragment.set(mStateFragment.mDefault); |
| 478 | } else { |
| 479 | mFragment.set(pf); |
| 480 | } |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 483 | void Context::setProgramRaster(ProgramRaster *pr) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 484 | rsAssert(mIsGraphicsContext); |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 485 | if (pr == NULL) { |
| 486 | mRaster.set(mStateRaster.mDefault); |
| 487 | } else { |
| 488 | mRaster.set(pr); |
| 489 | } |
| 490 | } |
| 491 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 492 | void Context::setProgramVertex(ProgramVertex *pv) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 493 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 494 | if (pv == NULL) { |
| 495 | mVertex.set(mStateVertex.mDefault); |
| 496 | } else { |
| 497 | mVertex.set(pv); |
| 498 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 501 | void Context::setFont(Font *f) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 502 | rsAssert(mIsGraphicsContext); |
| 503 | if (f == NULL) { |
| 504 | mFont.set(mStateFont.mDefault); |
| 505 | } else { |
| 506 | mFont.set(f); |
| 507 | } |
| 508 | } |
| 509 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 510 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 511 | rsAssert(!obj->getName()); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 512 | obj->setName(name, len); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 513 | mNames.add(obj); |
| 514 | } |
| 515 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 516 | void Context::removeName(ObjectBase *obj) { |
| 517 | for (size_t ct=0; ct < mNames.size(); ct++) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 518 | if (obj == mNames[ct]) { |
| 519 | mNames.removeAt(ct); |
| 520 | return; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 525 | RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) { |
| 526 | return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 529 | RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) { |
| 530 | return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 533 | bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, |
| 534 | uint32_t subID, size_t len, bool waitForSpace) const { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 535 | |
| 536 | return mIO.sendToClient(cmdID, subID, data, len, waitForSpace); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 539 | void Context::initToClient() { |
| 540 | while (!mRunning) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 541 | usleep(100); |
| 542 | } |
| 543 | } |
| 544 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 545 | void Context::deinitToClient() { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 546 | mIO.clientShutdown(); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 547 | } |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 548 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 549 | void Context::setError(RsError e, const char *msg) const { |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 550 | mError = e; |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 551 | sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 555 | void Context::dumpDebug() const { |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 556 | LOGE("RS Context debug %p", this); |
| 557 | LOGE("RS Context debug"); |
| 558 | |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 559 | LOGE(" RS width %i, height %i", mWidth, mHeight); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 560 | LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 561 | LOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId); |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 562 | } |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 563 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 564 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 565 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 566 | |
| 567 | namespace android { |
| 568 | namespace renderscript { |
| 569 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 570 | void rsi_ContextFinish(Context *rsc) { |
Jason Sams | 8c88090 | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 571 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 572 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 573 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 574 | Script *s = static_cast<Script *>(vs); |
| 575 | rsc->setRootScript(s); |
| 576 | } |
| 577 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 578 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 579 | Sampler *s = static_cast<Sampler *>(vs); |
| 580 | |
| 581 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 582 | LOGE("Invalid sampler slot"); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | s->bindToContext(&rsc->mStateSampler, slot); |
| 587 | } |
| 588 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 589 | void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { |
Jason Sams | ccc010b | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 590 | ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 591 | rsc->setProgramStore(pfs); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 594 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 595 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 596 | rsc->setProgramFragment(pf); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 599 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 600 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 601 | rsc->setProgramRaster(pr); |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 604 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 605 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 606 | rsc->setProgramVertex(pv); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 609 | void rsi_ContextBindFont(Context *rsc, RsFont vfont) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 610 | Font *font = static_cast<Font *>(vfont); |
| 611 | rsc->setFont(font); |
| 612 | } |
| 613 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 614 | void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 615 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 616 | rsc->assignName(ob, name, name_length); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 617 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 618 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 619 | void rsi_ObjDestroy(Context *rsc, void *optr) { |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 620 | ObjectBase *ob = static_cast<ObjectBase *>(optr); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 621 | rsc->removeName(ob); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 622 | ob->decUserRef(); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 625 | void rsi_ContextPause(Context *rsc) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 626 | rsc->pause(); |
| 627 | } |
| 628 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 629 | void rsi_ContextResume(Context *rsc) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 630 | rsc->resume(); |
| 631 | } |
| 632 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 633 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) { |
Mathias Agopian | fa40286 | 2010-02-12 14:04:35 -0800 | [diff] [blame] | 634 | rsc->setSurface(w, h, sur); |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 637 | void rsi_ContextSetPriority(Context *rsc, int32_t p) { |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 638 | rsc->setPriority(p); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 641 | void rsi_ContextDump(Context *rsc, int32_t bits) { |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 642 | ObjectBase::dumpAll(rsc); |
| 643 | } |
| 644 | |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 645 | void rsi_ContextDestroyWorker(Context *rsc) { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 646 | rsc->destroyWorkerThreadResources(); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 649 | void rsi_ContextDestroy(Context *rsc) { |
| 650 | LOGV("rsContextDestroy %p", rsc); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 651 | rsContextDestroyWorker(rsc); |
Jason Sams | 1dcefab | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 652 | delete rsc; |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 653 | LOGV("rsContextDestroy 2 %p", rsc); |
Jason Sams | 1dcefab | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 656 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 657 | RsMessageToClientType rsi_ContextPeekMessage(Context *rsc, |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 658 | size_t * receiveLen, size_t receiveLen_length, |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 659 | uint32_t * subID, size_t subID_length) { |
| 660 | return rsc->peekMessageToClient(receiveLen, subID); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 663 | RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length, |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 664 | size_t * receiveLen, size_t receiveLen_length, |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 665 | uint32_t * subID, size_t subID_length) { |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 666 | rsAssert(subID_length == sizeof(uint32_t)); |
| 667 | rsAssert(receiveLen_length == sizeof(size_t)); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 668 | return rsc->getMessageToClient(data, receiveLen, subID, data_length); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 671 | void rsi_ContextInitToClient(Context *rsc) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 672 | rsc->initToClient(); |
| 673 | } |
| 674 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 675 | void rsi_ContextDeinitToClient(Context *rsc) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 676 | rsc->deinitToClient(); |
| 677 | } |
| 678 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame^] | 682 | RsContext rsContextCreate(RsDevice vdev, uint32_t version, |
| 683 | uint32_t sdkVersion) { |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 684 | LOGV("rsContextCreate %p", vdev); |
| 685 | Device * dev = static_cast<Device *>(vdev); |
| 686 | Context *rsc = Context::createContext(dev, NULL); |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame^] | 687 | if (rsc) { |
| 688 | rsc->setTargetSdkVersion(sdkVersion); |
| 689 | } |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 690 | return rsc; |
| 691 | } |
| 692 | |
| 693 | RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame^] | 694 | uint32_t sdkVersion, RsSurfaceConfig sc, |
| 695 | uint32_t dpi) { |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 696 | LOGV("rsContextCreateGL %p", vdev); |
| 697 | Device * dev = static_cast<Device *>(vdev); |
| 698 | Context *rsc = Context::createContext(dev, &sc); |
Jason Sams | 9544f76 | 2011-07-13 16:09:42 -0700 | [diff] [blame] | 699 | if (rsc) { |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame^] | 700 | rsc->setTargetSdkVersion(sdkVersion); |
Jason Sams | 9544f76 | 2011-07-13 16:09:42 -0700 | [diff] [blame] | 701 | rsc->setDPI(dpi); |
| 702 | } |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 703 | LOGV("rsContextCreateGL ret %p ", rsc); |
| 704 | return rsc; |
| 705 | } |
| 706 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 707 | // Only to be called at a3d load time, before object is visible to user |
| 708 | // not thread safe |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 709 | void rsaGetName(RsContext con, void * obj, const char **name) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 710 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 711 | (*name) = ob->getName(); |
| 712 | } |