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