Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "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 | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 21 | #include <ui/EGLUtils.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 22 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
| 24 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 25 | #include <GLES/gl.h> |
| 26 | #include <GLES/glext.h> |
| 27 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 28 | using namespace android; |
| 29 | using namespace android::renderscript; |
| 30 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 31 | bool g_logTimes = -1; |
| 32 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 33 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | |
| 35 | void Context::initEGL() |
| 36 | { |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 37 | mEGL.mNumConfigs = -1; |
| 38 | EGLint configAttribs[128]; |
| 39 | EGLint *configAttribsPtr = configAttribs; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 41 | memset(configAttribs, 0, sizeof(configAttribs)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 43 | configAttribsPtr[0] = EGL_SURFACE_TYPE; |
| 44 | configAttribsPtr[1] = EGL_WINDOW_BIT; |
| 45 | configAttribsPtr += 2; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 46 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 47 | if (mUseDepth) { |
| 48 | configAttribsPtr[0] = EGL_DEPTH_SIZE; |
| 49 | configAttribsPtr[1] = 16; |
| 50 | configAttribsPtr += 2; |
| 51 | } |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 52 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 53 | if (mDev->mForceSW) { |
| 54 | configAttribsPtr[0] = EGL_CONFIG_CAVEAT; |
| 55 | configAttribsPtr[1] = EGL_SLOW_CONFIG; |
| 56 | configAttribsPtr += 2; |
| 57 | } |
| 58 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 59 | configAttribsPtr[0] = EGL_NONE; |
| 60 | rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint)))); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 61 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 62 | mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 63 | eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion); |
| 64 | |
| 65 | status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig); |
| 66 | if (err) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 67 | LOGE("couldn't find an EGLConfig matching the screen format\n"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 68 | } |
| 69 | //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs); |
| 70 | |
| 71 | if (mWndSurface) { |
| 72 | mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL); |
| 73 | } else { |
| 74 | mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, |
| 75 | android_createDisplaySurface(), |
| 76 | NULL); |
| 77 | } |
| 78 | |
| 79 | mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL); |
| 80 | eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext); |
| 81 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); |
| 82 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); |
| 83 | |
| 84 | |
| 85 | mGL.mVersion = glGetString(GL_VERSION); |
| 86 | mGL.mVendor = glGetString(GL_VENDOR); |
| 87 | mGL.mRenderer = glGetString(GL_RENDERER); |
| 88 | mGL.mExtensions = glGetString(GL_EXTENSIONS); |
| 89 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 90 | LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
| 91 | LOGV("GL Version %s", mGL.mVersion); |
| 92 | LOGV("GL Vendor %s", mGL.mVendor); |
| 93 | LOGV("GL Renderer %s", mGL.mRenderer); |
| 94 | LOGV("GL Extensions %s", mGL.mExtensions); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 95 | |
Jason Sams | 67c6844 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 96 | if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) { |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 97 | LOGE("Error, OpenGL ES Lite not supported"); |
Jason Sams | 67c6844 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 98 | } else { |
| 99 | sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 100 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 103 | bool Context::runScript(Script *s, uint32_t launchID) |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 104 | { |
| 105 | ObjectBaseRef<ProgramFragment> frag(mFragment); |
| 106 | ObjectBaseRef<ProgramVertex> vtx(mVertex); |
| 107 | ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore); |
| 108 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 109 | bool ret = s->run(this, launchID); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 110 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 111 | mFragment.set(frag); |
| 112 | mVertex.set(vtx); |
| 113 | mFragmentStore.set(store); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 114 | return ret; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 118 | bool Context::runRootScript() |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | { |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 120 | if (this->logTimes) { |
| 121 | timerSet(RS_TIMER_CLEAR_SWAP); |
| 122 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 123 | rsAssert(mRootScript->mEnviroment.mIsRoot); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 124 | |
Jason Sams | 59038ca | 2009-09-22 12:26:53 -0700 | [diff] [blame] | 125 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); |
| 126 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 127 | glViewport(0, 0, mEGL.mWidth, mEGL.mHeight); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 129 | //glEnable(GL_POINT_SMOOTH); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 130 | |
Jason Sams | 928f5cf | 2009-06-08 18:50:13 -0700 | [diff] [blame] | 131 | glClearColor(mRootScript->mEnviroment.mClearColor[0], |
| 132 | mRootScript->mEnviroment.mClearColor[1], |
| 133 | mRootScript->mEnviroment.mClearColor[2], |
| 134 | mRootScript->mEnviroment.mClearColor[3]); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 135 | if (mUseDepth) { |
| 136 | glDepthMask(GL_TRUE); |
| 137 | glClearDepthf(mRootScript->mEnviroment.mClearDepth); |
| 138 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 139 | } else { |
| 140 | glClear(GL_COLOR_BUFFER_BIT); |
| 141 | } |
Jason Sams | 67c6844 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 142 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 143 | if (this->logTimes) { |
| 144 | timerSet(RS_TIMER_SCRIPT); |
| 145 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 146 | bool ret = runScript(mRootScript.get(), 0); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 147 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 150 | uint64_t Context::getTime() const |
| 151 | { |
| 152 | struct timespec t; |
| 153 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 154 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
| 155 | } |
| 156 | |
| 157 | void Context::timerReset() |
| 158 | { |
| 159 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 160 | mTimers[ct] = 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void Context::timerInit() |
| 165 | { |
| 166 | mTimeLast = getTime(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 167 | mTimeFrame = mTimeLast; |
| 168 | mTimeLastFrame = mTimeLast; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 169 | mTimerActive = RS_TIMER_INTERNAL; |
| 170 | timerReset(); |
| 171 | } |
| 172 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 173 | void Context::timerFrame() |
| 174 | { |
| 175 | mTimeLastFrame = mTimeFrame; |
| 176 | mTimeFrame = getTime(); |
| 177 | } |
| 178 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 179 | void Context::timerSet(Timers tm) |
| 180 | { |
| 181 | uint64_t last = mTimeLast; |
| 182 | mTimeLast = getTime(); |
| 183 | mTimers[mTimerActive] += mTimeLast - last; |
| 184 | mTimerActive = tm; |
| 185 | } |
| 186 | |
| 187 | void Context::timerPrint() |
| 188 | { |
| 189 | double total = 0; |
| 190 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 191 | total += mTimers[ct]; |
| 192 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 193 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 194 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 195 | LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)", |
| 196 | frame / 1000000, |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 197 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000, |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 198 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000, |
| 199 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
| 200 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000); |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 203 | void Context::setupCheck() |
| 204 | { |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 205 | mFragmentStore->setupGL(this, &mStateFragmentStore); |
| 206 | mFragment->setupGL(this, &mStateFragment); |
| 207 | mRaster->setupGL(this, &mStateRaster); |
| 208 | mVertex->setupGL(this, &mStateVertex); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 211 | static bool get_log_times() |
| 212 | { |
| 213 | char buf[PROPERTY_VALUE_MAX]; |
| 214 | property_get("debug.rs.profile", buf, "0"); |
| 215 | return 0 != strcmp(buf, "0"); |
| 216 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 217 | |
| 218 | void * Context::threadProc(void *vrsc) |
| 219 | { |
| 220 | Context *rsc = static_cast<Context *>(vrsc); |
| 221 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 222 | rsc->logTimes = get_log_times(); |
| 223 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 224 | rsc->initEGL(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 225 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 226 | ScriptTLSStruct *tlsStruct = new ScriptTLSStruct; |
| 227 | if (!tlsStruct) { |
| 228 | LOGE("Error allocating tls storage"); |
| 229 | return NULL; |
| 230 | } |
| 231 | tlsStruct->mContext = rsc; |
| 232 | tlsStruct->mScript = NULL; |
| 233 | int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct); |
| 234 | if (status) { |
| 235 | LOGE("pthread_setspecific %i", status); |
| 236 | } |
| 237 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 238 | rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
| 239 | rsc->setRaster(NULL); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 240 | rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 241 | rsc->setVertex(NULL); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 242 | rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 243 | rsc->setFragment(NULL); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 244 | rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 245 | rsc->setFragmentStore(NULL); |
| 246 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 247 | rsc->mRunning = true; |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 248 | bool mDraw = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 249 | while (!rsc->mExit) { |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 250 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 251 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 252 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 253 | if (mDraw) { |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 254 | mDraw = rsc->runRootScript(); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 255 | if (rsc->logTimes) { |
| 256 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
| 257 | } |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 258 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 259 | if (rsc->logTimes) { |
| 260 | rsc->timerFrame(); |
| 261 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 262 | rsc->timerPrint(); |
| 263 | rsc->timerReset(); |
| 264 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 265 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 266 | if (rsc->mObjDestroy.mNeedToEmpty) { |
| 267 | rsc->objDestroyOOBRun(); |
| 268 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 271 | LOGV("RS Thread exiting"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 272 | glClearColor(0,0,0,0); |
| 273 | glClear(GL_COLOR_BUFFER_BIT); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 274 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
| 275 | eglTerminate(rsc->mEGL.mDisplay); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 276 | rsc->objDestroyOOBRun(); |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 277 | LOGV("RS Thread exited"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 278 | return NULL; |
| 279 | } |
| 280 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 281 | Context::Context(Device *dev, Surface *sur, bool useDepth) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 282 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 283 | dev->addContext(this); |
| 284 | mDev = dev; |
| 285 | mRunning = false; |
| 286 | mExit = false; |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 287 | mUseDepth = useDepth; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 288 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 289 | int status; |
| 290 | pthread_attr_t threadAttr; |
| 291 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 292 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 293 | if (status) { |
| 294 | LOGE("Failed to init thread tls key."); |
| 295 | return; |
| 296 | } |
| 297 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 298 | status = pthread_attr_init(&threadAttr); |
| 299 | if (status) { |
| 300 | LOGE("Failed to init thread attribute."); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | sched_param sparam; |
| 305 | sparam.sched_priority = ANDROID_PRIORITY_DISPLAY; |
| 306 | pthread_attr_setschedparam(&threadAttr, &sparam); |
| 307 | |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 308 | mWndSurface = sur; |
| 309 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 310 | objDestroyOOBInit(); |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 311 | timerInit(); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 312 | |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 313 | LOGV("RS Launching thread"); |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 314 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 315 | if (status) { |
| 316 | LOGE("Failed to start rs context thread."); |
| 317 | } |
| 318 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 319 | while(!mRunning) { |
| 320 | sleep(1); |
| 321 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 322 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 323 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | Context::~Context() |
| 327 | { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 328 | LOGV("Context::~Context"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 329 | mExit = true; |
| 330 | void *res; |
| 331 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 332 | mIO.shutdown(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 333 | int status = pthread_join(mThreadId, &res); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 334 | objDestroyOOBRun(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 335 | |
| 336 | if (mDev) { |
| 337 | mDev->removeContext(this); |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 338 | pthread_key_delete(gThreadTLSKey); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 339 | } |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 340 | |
| 341 | objDestroyOOBDestroy(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 344 | void Context::setRootScript(Script *s) |
| 345 | { |
| 346 | mRootScript.set(s); |
| 347 | } |
| 348 | |
| 349 | void Context::setFragmentStore(ProgramFragmentStore *pfs) |
| 350 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 351 | if (pfs == NULL) { |
| 352 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 353 | } else { |
| 354 | mFragmentStore.set(pfs); |
| 355 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void Context::setFragment(ProgramFragment *pf) |
| 359 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 360 | if (pf == NULL) { |
| 361 | mFragment.set(mStateFragment.mDefault); |
| 362 | } else { |
| 363 | mFragment.set(pf); |
| 364 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 367 | void Context::setRaster(ProgramRaster *pr) |
| 368 | { |
| 369 | if (pr == NULL) { |
| 370 | mRaster.set(mStateRaster.mDefault); |
| 371 | } else { |
| 372 | mRaster.set(pr); |
| 373 | } |
| 374 | } |
| 375 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 376 | void Context::allocationCheck(const Allocation *a) |
| 377 | { |
| 378 | mVertex->checkUpdatedAllocation(a); |
| 379 | mFragment->checkUpdatedAllocation(a); |
| 380 | mFragmentStore->checkUpdatedAllocation(a); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void Context::setVertex(ProgramVertex *pv) |
| 384 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 385 | if (pv == NULL) { |
| 386 | mVertex.set(mStateVertex.mDefault); |
| 387 | } else { |
| 388 | mVertex.set(pv); |
| 389 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 392 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 393 | { |
| 394 | rsAssert(!obj->getName()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 395 | obj->setName(name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 396 | mNames.add(obj); |
| 397 | } |
| 398 | |
| 399 | void Context::removeName(ObjectBase *obj) |
| 400 | { |
| 401 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 402 | if (obj == mNames[ct]) { |
| 403 | mNames.removeAt(ct); |
| 404 | return; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | ObjectBase * Context::lookupName(const char *name) const |
| 410 | { |
| 411 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 412 | if (!strcmp(name, mNames[ct]->getName())) { |
| 413 | return mNames[ct]; |
| 414 | } |
| 415 | } |
| 416 | return NULL; |
| 417 | } |
| 418 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 419 | void Context::appendNameDefines(String8 *str) const |
| 420 | { |
| 421 | char buf[256]; |
| 422 | for (size_t ct=0; ct < mNames.size(); ct++) { |
| 423 | str->append("#define NAMED_"); |
| 424 | str->append(mNames[ct]->getName()); |
| 425 | str->append(" "); |
| 426 | sprintf(buf, "%i\n", (int)mNames[ct]); |
| 427 | str->append(buf); |
| 428 | } |
| 429 | } |
| 430 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 431 | void Context::appendVarDefines(String8 *str) const |
| 432 | { |
| 433 | char buf[256]; |
| 434 | for (size_t ct=0; ct < mInt32Defines.size(); ct++) { |
| 435 | str->append("#define "); |
| 436 | str->append(mInt32Defines.keyAt(ct)); |
| 437 | str->append(" "); |
| 438 | sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); |
| 439 | str->append(buf); |
| 440 | |
| 441 | } |
| 442 | for (size_t ct=0; ct < mFloatDefines.size(); ct++) { |
| 443 | str->append("#define "); |
| 444 | str->append(mFloatDefines.keyAt(ct)); |
| 445 | str->append(" "); |
| 446 | sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); |
| 447 | str->append(buf); |
| 448 | } |
| 449 | } |
| 450 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 451 | bool Context::objDestroyOOBInit() |
| 452 | { |
| 453 | int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL); |
| 454 | if (status) { |
| 455 | LOGE("Context::ObjDestroyOOBInit mutex init failure"); |
| 456 | return false; |
| 457 | } |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | void Context::objDestroyOOBRun() |
| 462 | { |
| 463 | if (mObjDestroy.mNeedToEmpty) { |
| 464 | int status = pthread_mutex_lock(&mObjDestroy.mMutex); |
| 465 | if (status) { |
| 466 | LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 471 | mObjDestroy.mDestroyList[ct]->decUserRef(); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 472 | } |
| 473 | mObjDestroy.mDestroyList.clear(); |
| 474 | mObjDestroy.mNeedToEmpty = false; |
| 475 | |
| 476 | status = pthread_mutex_unlock(&mObjDestroy.mMutex); |
| 477 | if (status) { |
| 478 | LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void Context::objDestroyOOBDestroy() |
| 484 | { |
| 485 | rsAssert(!mObjDestroy.mNeedToEmpty); |
| 486 | pthread_mutex_destroy(&mObjDestroy.mMutex); |
| 487 | } |
| 488 | |
| 489 | void Context::objDestroyAdd(ObjectBase *obj) |
| 490 | { |
| 491 | int status = pthread_mutex_lock(&mObjDestroy.mMutex); |
| 492 | if (status) { |
| 493 | LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | mObjDestroy.mNeedToEmpty = true; |
| 498 | mObjDestroy.mDestroyList.add(obj); |
| 499 | |
| 500 | status = pthread_mutex_unlock(&mObjDestroy.mMutex); |
| 501 | if (status) { |
| 502 | LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 507 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 508 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 509 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 510 | |
| 511 | namespace android { |
| 512 | namespace renderscript { |
| 513 | |
| 514 | |
| 515 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) |
| 516 | { |
| 517 | Script *s = static_cast<Script *>(vs); |
| 518 | rsc->setRootScript(s); |
| 519 | } |
| 520 | |
| 521 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) |
| 522 | { |
| 523 | Sampler *s = static_cast<Sampler *>(vs); |
| 524 | |
| 525 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 526 | LOGE("Invalid sampler slot"); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | s->bindToContext(&rsc->mStateSampler, slot); |
| 531 | } |
| 532 | |
| 533 | void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs) |
| 534 | { |
| 535 | ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs); |
| 536 | rsc->setFragmentStore(pfs); |
| 537 | } |
| 538 | |
| 539 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) |
| 540 | { |
| 541 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
| 542 | rsc->setFragment(pf); |
| 543 | } |
| 544 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 545 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) |
| 546 | { |
| 547 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
| 548 | rsc->setRaster(pr); |
| 549 | } |
| 550 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 551 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) |
| 552 | { |
| 553 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
| 554 | rsc->setVertex(pv); |
| 555 | } |
| 556 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 557 | void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 558 | { |
| 559 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 560 | rsc->assignName(ob, name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 561 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 562 | |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 563 | void rsi_ObjDestroy(Context *rsc, void *obj) |
| 564 | { |
| 565 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 566 | rsc->removeName(ob); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 567 | ob->decUserRef(); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 570 | void rsi_ContextSetDefineF(Context *rsc, const char* name, float value) |
| 571 | { |
| 572 | rsc->addInt32Define(name, value); |
| 573 | } |
| 574 | |
| 575 | void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value) |
| 576 | { |
| 577 | rsc->addFloatDefine(name, value); |
| 578 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 579 | |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 584 | RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 585 | { |
| 586 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 587 | Context *rsc = new Context(dev, (Surface *)sur, useDepth); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 588 | return rsc; |
| 589 | } |
| 590 | |
| 591 | void rsContextDestroy(RsContext vrsc) |
| 592 | { |
| 593 | Context * rsc = static_cast<Context *>(vrsc); |
| 594 | delete rsc; |
| 595 | } |
| 596 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 597 | void rsObjDestroyOOB(RsContext vrsc, void *obj) |
| 598 | { |
| 599 | Context * rsc = static_cast<Context *>(vrsc); |
| 600 | rsc->objDestroyAdd(static_cast<ObjectBase *>(obj)); |
| 601 | } |
| 602 | |