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