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