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