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