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 | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/resource.h> |
| 25 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| 27 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 28 | #include <GLES/gl.h> |
| 29 | #include <GLES/glext.h> |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 30 | #include <GLES2/gl2.h> |
| 31 | #include <GLES2/gl2ext.h> |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 32 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 33 | #include <cutils/sched_policy.h> |
| 34 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | using namespace android; |
| 36 | using namespace android::renderscript; |
| 37 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 38 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 39 | uint32_t Context::gThreadTLSKeyCount = 0; |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 40 | uint32_t Context::gGLContextCount = 0; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 41 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 43 | static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { |
| 44 | if (returnVal != EGL_TRUE) { |
| 45 | fprintf(stderr, "%s() returned %d\n", op, returnVal); |
| 46 | } |
| 47 | |
| 48 | for (EGLint error = eglGetError(); error != EGL_SUCCESS; error |
| 49 | = eglGetError()) { |
| 50 | fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error), |
| 51 | error); |
| 52 | } |
| 53 | } |
| 54 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 55 | void Context::initEGL(bool useGL2) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 56 | { |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 57 | mEGL.mNumConfigs = -1; |
| 58 | EGLint configAttribs[128]; |
| 59 | EGLint *configAttribsPtr = configAttribs; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 60 | EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 61 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 62 | memset(configAttribs, 0, sizeof(configAttribs)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 64 | configAttribsPtr[0] = EGL_SURFACE_TYPE; |
| 65 | configAttribsPtr[1] = EGL_WINDOW_BIT; |
| 66 | configAttribsPtr += 2; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 67 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 68 | if (useGL2) { |
| 69 | configAttribsPtr[0] = EGL_RENDERABLE_TYPE; |
| 70 | configAttribsPtr[1] = EGL_OPENGL_ES2_BIT; |
| 71 | configAttribsPtr += 2; |
| 72 | } |
| 73 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 74 | if (mUseDepth) { |
| 75 | configAttribsPtr[0] = EGL_DEPTH_SIZE; |
| 76 | configAttribsPtr[1] = 16; |
| 77 | configAttribsPtr += 2; |
| 78 | } |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 79 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 80 | if (mDev->mForceSW) { |
| 81 | configAttribsPtr[0] = EGL_CONFIG_CAVEAT; |
| 82 | configAttribsPtr[1] = EGL_SLOW_CONFIG; |
| 83 | configAttribsPtr += 2; |
| 84 | } |
| 85 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 86 | configAttribsPtr[0] = EGL_NONE; |
| 87 | rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint)))); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | |
Jason Sams | 6a17e16 | 2009-10-08 12:55:06 -0700 | [diff] [blame] | 89 | LOGV("initEGL start"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 90 | mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 91 | checkEglError("eglGetDisplay"); |
| 92 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 93 | eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 94 | checkEglError("eglInitialize"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 95 | |
| 96 | status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig); |
| 97 | if (err) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 98 | LOGE("couldn't find an EGLConfig matching the screen format\n"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 99 | } |
| 100 | //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs); |
| 101 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 102 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 103 | if (useGL2) { |
| 104 | mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2); |
| 105 | } else { |
| 106 | mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL); |
| 107 | } |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 108 | checkEglError("eglCreateContext"); |
| 109 | if (mEGL.mContext == EGL_NO_CONTEXT) { |
| 110 | LOGE("eglCreateContext returned EGL_NO_CONTEXT"); |
| 111 | } |
| 112 | gGLContextCount++; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 115 | void Context::deinitEGL() |
| 116 | { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 117 | LOGV("deinitEGL"); |
| 118 | setSurface(0, 0, NULL); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 119 | eglDestroyContext(mEGL.mDisplay, mEGL.mContext); |
| 120 | checkEglError("eglDestroyContext"); |
| 121 | |
| 122 | gGLContextCount--; |
| 123 | if (!gGLContextCount) { |
| 124 | eglTerminate(mEGL.mDisplay); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 129 | uint32_t Context::runScript(Script *s, uint32_t launchID) |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 130 | { |
| 131 | ObjectBaseRef<ProgramFragment> frag(mFragment); |
| 132 | ObjectBaseRef<ProgramVertex> vtx(mVertex); |
| 133 | ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore); |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 134 | ObjectBaseRef<ProgramRaster> raster(mRaster); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 135 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 136 | uint32_t ret = s->run(this, launchID); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 137 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 138 | mFragment.set(frag); |
| 139 | mVertex.set(vtx); |
| 140 | mFragmentStore.set(store); |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 141 | mRaster.set(raster); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 142 | return ret; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 145 | void Context::checkError(const char *msg) const |
| 146 | { |
| 147 | GLenum err = glGetError(); |
| 148 | if (err != GL_NO_ERROR) { |
| 149 | LOGE("GL Error, 0x%x, from %s", err, msg); |
| 150 | } |
| 151 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 152 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 153 | uint32_t Context::runRootScript() |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 154 | { |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 155 | timerSet(RS_TIMER_CLEAR_SWAP); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 156 | rsAssert(mRootScript->mEnviroment.mIsRoot); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 157 | |
Jason Sams | 59038ca | 2009-09-22 12:26:53 -0700 | [diff] [blame] | 158 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); |
| 159 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 160 | glViewport(0, 0, mEGL.mWidth, mEGL.mHeight); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 161 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 162 | |
Jason Sams | 928f5cf | 2009-06-08 18:50:13 -0700 | [diff] [blame] | 163 | glClearColor(mRootScript->mEnviroment.mClearColor[0], |
| 164 | mRootScript->mEnviroment.mClearColor[1], |
| 165 | mRootScript->mEnviroment.mClearColor[2], |
| 166 | mRootScript->mEnviroment.mClearColor[3]); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 167 | if (mUseDepth) { |
| 168 | glDepthMask(GL_TRUE); |
| 169 | glClearDepthf(mRootScript->mEnviroment.mClearDepth); |
| 170 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 171 | } else { |
| 172 | glClear(GL_COLOR_BUFFER_BIT); |
| 173 | } |
Jason Sams | 67c6844 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 174 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 175 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 176 | mStateFragmentStore.mLast.clear(); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 177 | uint32_t ret = runScript(mRootScript.get(), 0); |
Jason Sams | c7412b3 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 178 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 179 | checkError("runRootScript"); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 180 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 183 | uint64_t Context::getTime() const |
| 184 | { |
| 185 | struct timespec t; |
| 186 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 187 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
| 188 | } |
| 189 | |
| 190 | void Context::timerReset() |
| 191 | { |
| 192 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 193 | mTimers[ct] = 0; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void Context::timerInit() |
| 198 | { |
| 199 | mTimeLast = getTime(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 200 | mTimeFrame = mTimeLast; |
| 201 | mTimeLastFrame = mTimeLast; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 202 | mTimerActive = RS_TIMER_INTERNAL; |
| 203 | timerReset(); |
| 204 | } |
| 205 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 206 | void Context::timerFrame() |
| 207 | { |
| 208 | mTimeLastFrame = mTimeFrame; |
| 209 | mTimeFrame = getTime(); |
| 210 | } |
| 211 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 212 | void Context::timerSet(Timers tm) |
| 213 | { |
| 214 | uint64_t last = mTimeLast; |
| 215 | mTimeLast = getTime(); |
| 216 | mTimers[mTimerActive] += mTimeLast - last; |
| 217 | mTimerActive = tm; |
| 218 | } |
| 219 | |
| 220 | void Context::timerPrint() |
| 221 | { |
| 222 | double total = 0; |
| 223 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 224 | total += mTimers[ct]; |
| 225 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 226 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 227 | mTimeMSLastFrame = frame / 1000000; |
| 228 | mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; |
| 229 | mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 230 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 231 | |
| 232 | if (props.mLogTimes) { |
| 233 | LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)", |
| 234 | mTimeMSLastFrame, |
| 235 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, |
| 236 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, |
| 237 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
| 238 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000); |
| 239 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 242 | void Context::setupCheck() |
| 243 | { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 244 | if (checkVersion2_0()) { |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 245 | mShaderCache.lookup(this, mVertex.get(), mFragment.get()); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 246 | |
| 247 | mFragmentStore->setupGL2(this, &mStateFragmentStore); |
| 248 | mFragment->setupGL2(this, &mStateFragment, &mShaderCache); |
| 249 | mRaster->setupGL2(this, &mStateRaster); |
| 250 | mVertex->setupGL2(this, &mStateVertex, &mShaderCache); |
| 251 | |
| 252 | } else { |
| 253 | mFragmentStore->setupGL(this, &mStateFragmentStore); |
| 254 | mFragment->setupGL(this, &mStateFragment); |
| 255 | mRaster->setupGL(this, &mStateRaster); |
| 256 | mVertex->setupGL(this, &mStateVertex); |
| 257 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 260 | static bool getProp(const char *str) |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 261 | { |
| 262 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 263 | property_get(str, buf, "0"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 264 | return 0 != strcmp(buf, "0"); |
| 265 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 266 | |
| 267 | void * Context::threadProc(void *vrsc) |
| 268 | { |
| 269 | Context *rsc = static_cast<Context *>(vrsc); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 270 | rsc->mNativeThreadId = gettid(); |
| 271 | |
| 272 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 273 | rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 274 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 275 | rsc->props.mLogTimes = getProp("debug.rs.profile"); |
| 276 | rsc->props.mLogScripts = getProp("debug.rs.script"); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 277 | rsc->props.mLogObjects = getProp("debug.rs.object"); |
| 278 | rsc->props.mLogShaders = getProp("debug.rs.shader"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 279 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 280 | ScriptTLSStruct *tlsStruct = new ScriptTLSStruct; |
| 281 | if (!tlsStruct) { |
| 282 | LOGE("Error allocating tls storage"); |
| 283 | return NULL; |
| 284 | } |
| 285 | tlsStruct->mContext = rsc; |
| 286 | tlsStruct->mScript = NULL; |
| 287 | int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct); |
| 288 | if (status) { |
| 289 | LOGE("pthread_setspecific %i", status); |
| 290 | } |
| 291 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 292 | rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
| 293 | rsc->setRaster(NULL); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 294 | rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 295 | rsc->setVertex(NULL); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 296 | rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 297 | rsc->setFragment(NULL); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 298 | rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 299 | rsc->setFragmentStore(NULL); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 300 | rsc->mStateVertexArray.init(rsc); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 301 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 302 | rsc->mRunning = true; |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 303 | bool mDraw = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 304 | while (!rsc->mExit) { |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 305 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 306 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 307 | mDraw &= (rsc->mWndSurface != NULL); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 308 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 309 | uint32_t targetTime = 0; |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 310 | if (mDraw) { |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 311 | targetTime = rsc->runRootScript(); |
| 312 | mDraw = targetTime && !rsc->mPaused; |
| 313 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 314 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 315 | rsc->timerFrame(); |
| 316 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 317 | rsc->timerPrint(); |
| 318 | rsc->timerReset(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 319 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 320 | if (rsc->mObjDestroy.mNeedToEmpty) { |
| 321 | rsc->objDestroyOOBRun(); |
| 322 | } |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 323 | if (rsc->mThreadPriority > 0 && targetTime) { |
| 324 | int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000; |
| 325 | if (t > 0) { |
| 326 | usleep(t); |
| 327 | } |
| 328 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 331 | LOGV("RS Thread exiting"); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 332 | rsc->mRaster.clear(); |
| 333 | rsc->mFragment.clear(); |
| 334 | rsc->mVertex.clear(); |
| 335 | rsc->mFragmentStore.clear(); |
| 336 | rsc->mRootScript.clear(); |
| 337 | rsc->mStateRaster.deinit(rsc); |
| 338 | rsc->mStateVertex.deinit(rsc); |
| 339 | rsc->mStateFragment.deinit(rsc); |
| 340 | rsc->mStateFragmentStore.deinit(rsc); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 341 | ObjectBase::zeroAllUserRef(rsc); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 342 | |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 343 | rsc->mObjDestroy.mNeedToEmpty = true; |
| 344 | rsc->objDestroyOOBRun(); |
| 345 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 346 | pthread_mutex_lock(&gInitMutex); |
| 347 | rsc->deinitEGL(); |
| 348 | pthread_mutex_unlock(&gInitMutex); |
| 349 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 350 | LOGV("RS Thread exited"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 351 | return NULL; |
| 352 | } |
| 353 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 354 | void Context::setPriority(int32_t p) |
| 355 | { |
| 356 | // Note: If we put this in the proper "background" policy |
| 357 | // the wallpapers can become completly unresponsive at times. |
| 358 | // This is probably not what we want for something the user is actively |
| 359 | // looking at. |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 360 | mThreadPriority = p; |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 361 | #if 0 |
| 362 | SchedPolicy pol = SP_FOREGROUND; |
| 363 | if (p > 0) { |
| 364 | pol = SP_BACKGROUND; |
| 365 | } |
| 366 | if (!set_sched_policy(mNativeThreadId, pol)) { |
| 367 | // success; reset the priority as well |
| 368 | } |
| 369 | #else |
| 370 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
| 371 | #endif |
| 372 | } |
| 373 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 374 | Context::Context(Device *dev, bool useDepth) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 375 | { |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 376 | pthread_mutex_lock(&gInitMutex); |
| 377 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 378 | dev->addContext(this); |
| 379 | mDev = dev; |
| 380 | mRunning = false; |
| 381 | mExit = false; |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 382 | mUseDepth = useDepth; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 383 | mPaused = false; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 384 | mObjHead = NULL; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 385 | memset(&mEGL, 0, sizeof(mEGL)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 386 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 387 | int status; |
| 388 | pthread_attr_t threadAttr; |
| 389 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 390 | if (!gThreadTLSKeyCount) { |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 391 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 392 | if (status) { |
| 393 | LOGE("Failed to init thread tls key."); |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 394 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 397 | } |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 398 | gThreadTLSKeyCount++; |
| 399 | pthread_mutex_unlock(&gInitMutex); |
| 400 | |
| 401 | // Global init done at this point. |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 402 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 403 | status = pthread_attr_init(&threadAttr); |
| 404 | if (status) { |
| 405 | LOGE("Failed to init thread attribute."); |
| 406 | return; |
| 407 | } |
| 408 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 409 | mWndSurface = NULL; |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 410 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 411 | objDestroyOOBInit(); |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 412 | timerInit(); |
Jason Sams | d3f2eaf | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 413 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 414 | |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 415 | LOGV("RS Launching thread"); |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 416 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 417 | if (status) { |
| 418 | LOGE("Failed to start rs context thread."); |
| 419 | } |
| 420 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 421 | while(!mRunning) { |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 422 | usleep(100); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 423 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 424 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 425 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | Context::~Context() |
| 429 | { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 430 | LOGV("Context::~Context"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 431 | mExit = true; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 432 | mPaused = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 433 | void *res; |
| 434 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 435 | mIO.shutdown(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 436 | int status = pthread_join(mThreadId, &res); |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 437 | mObjDestroy.mNeedToEmpty = true; |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 438 | objDestroyOOBRun(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 439 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 440 | // Global structure cleanup. |
| 441 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 442 | if (mDev) { |
| 443 | mDev->removeContext(this); |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 444 | --gThreadTLSKeyCount; |
| 445 | if (!gThreadTLSKeyCount) { |
| 446 | pthread_key_delete(gThreadTLSKey); |
| 447 | } |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 448 | mDev = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 449 | } |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 450 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 451 | |
| 452 | objDestroyOOBDestroy(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 455 | void Context::setSurface(uint32_t w, uint32_t h, Surface *sur) |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 456 | { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 457 | LOGV("setSurface %i %i %p", w, h, sur); |
| 458 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 459 | EGLBoolean ret; |
| 460 | if (mEGL.mSurface != NULL) { |
| 461 | ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 462 | checkEglError("eglMakeCurrent", ret); |
| 463 | |
| 464 | ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface); |
| 465 | checkEglError("eglDestroySurface", ret); |
| 466 | |
| 467 | mEGL.mSurface = NULL; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 468 | mEGL.mWidth = 0; |
| 469 | mEGL.mHeight = 0; |
| 470 | mWidth = 0; |
| 471 | mHeight = 0; |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | mWndSurface = sur; |
| 475 | if (mWndSurface != NULL) { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 476 | bool first = false; |
| 477 | if (!mEGL.mContext) { |
| 478 | first = true; |
| 479 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 480 | initEGL(true); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 481 | pthread_mutex_unlock(&gInitMutex); |
| 482 | } |
| 483 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 484 | mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL); |
| 485 | checkEglError("eglCreateWindowSurface"); |
| 486 | if (mEGL.mSurface == EGL_NO_SURFACE) { |
| 487 | LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE"); |
| 488 | } |
| 489 | |
| 490 | ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext); |
| 491 | checkEglError("eglMakeCurrent", ret); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 492 | |
| 493 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth); |
| 494 | eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight); |
| 495 | mWidth = w; |
| 496 | mHeight = h; |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 497 | mStateVertex.updateSize(this, w, h); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 498 | |
| 499 | if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) { |
| 500 | LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight); |
| 501 | } |
| 502 | |
| 503 | if (first) { |
| 504 | mGL.mVersion = glGetString(GL_VERSION); |
| 505 | mGL.mVendor = glGetString(GL_VENDOR); |
| 506 | mGL.mRenderer = glGetString(GL_RENDERER); |
| 507 | mGL.mExtensions = glGetString(GL_EXTENSIONS); |
| 508 | |
| 509 | //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
| 510 | LOGV("GL Version %s", mGL.mVersion); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 511 | //LOGV("GL Vendor %s", mGL.mVendor); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 512 | LOGV("GL Renderer %s", mGL.mRenderer); |
| 513 | //LOGV("GL Extensions %s", mGL.mExtensions); |
| 514 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 515 | const char *verptr = NULL; |
| 516 | if (strlen((const char *)mGL.mVersion) > 9) { |
| 517 | if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) { |
| 518 | verptr = (const char *)mGL.mVersion + 12; |
| 519 | } |
| 520 | if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) { |
| 521 | verptr = (const char *)mGL.mVersion + 9; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (!verptr) { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 526 | LOGE("Error, OpenGL ES Lite not supported"); |
| 527 | } else { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 528 | sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 529 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 530 | |
| 531 | glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs); |
| 532 | glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors); |
| 533 | glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits); |
| 534 | |
| 535 | glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors); |
| 536 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits); |
| 537 | |
| 538 | glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits); |
| 539 | glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 545 | void Context::pause() |
| 546 | { |
| 547 | mPaused = true; |
| 548 | } |
| 549 | |
| 550 | void Context::resume() |
| 551 | { |
| 552 | mPaused = false; |
| 553 | } |
| 554 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 555 | void Context::setRootScript(Script *s) |
| 556 | { |
| 557 | mRootScript.set(s); |
| 558 | } |
| 559 | |
| 560 | void Context::setFragmentStore(ProgramFragmentStore *pfs) |
| 561 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 562 | if (pfs == NULL) { |
| 563 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 564 | } else { |
| 565 | mFragmentStore.set(pfs); |
| 566 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void Context::setFragment(ProgramFragment *pf) |
| 570 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 571 | if (pf == NULL) { |
| 572 | mFragment.set(mStateFragment.mDefault); |
| 573 | } else { |
| 574 | mFragment.set(pf); |
| 575 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 578 | void Context::setRaster(ProgramRaster *pr) |
| 579 | { |
| 580 | if (pr == NULL) { |
| 581 | mRaster.set(mStateRaster.mDefault); |
| 582 | } else { |
| 583 | mRaster.set(pr); |
| 584 | } |
| 585 | } |
| 586 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 587 | void Context::setVertex(ProgramVertex *pv) |
| 588 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 589 | if (pv == NULL) { |
| 590 | mVertex.set(mStateVertex.mDefault); |
| 591 | } else { |
| 592 | mVertex.set(pv); |
| 593 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 596 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 597 | { |
| 598 | rsAssert(!obj->getName()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 599 | obj->setName(name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 600 | mNames.add(obj); |
| 601 | } |
| 602 | |
| 603 | void Context::removeName(ObjectBase *obj) |
| 604 | { |
| 605 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 606 | if (obj == mNames[ct]) { |
| 607 | mNames.removeAt(ct); |
| 608 | return; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | ObjectBase * Context::lookupName(const char *name) const |
| 614 | { |
| 615 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 616 | if (!strcmp(name, mNames[ct]->getName())) { |
| 617 | return mNames[ct]; |
| 618 | } |
| 619 | } |
| 620 | return NULL; |
| 621 | } |
| 622 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 623 | void Context::appendNameDefines(String8 *str) const |
| 624 | { |
| 625 | char buf[256]; |
| 626 | for (size_t ct=0; ct < mNames.size(); ct++) { |
| 627 | str->append("#define NAMED_"); |
| 628 | str->append(mNames[ct]->getName()); |
| 629 | str->append(" "); |
| 630 | sprintf(buf, "%i\n", (int)mNames[ct]); |
| 631 | str->append(buf); |
| 632 | } |
| 633 | } |
| 634 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 635 | bool Context::objDestroyOOBInit() |
| 636 | { |
| 637 | int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL); |
| 638 | if (status) { |
| 639 | LOGE("Context::ObjDestroyOOBInit mutex init failure"); |
| 640 | return false; |
| 641 | } |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | void Context::objDestroyOOBRun() |
| 646 | { |
| 647 | if (mObjDestroy.mNeedToEmpty) { |
| 648 | int status = pthread_mutex_lock(&mObjDestroy.mMutex); |
| 649 | if (status) { |
| 650 | LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 655 | mObjDestroy.mDestroyList[ct]->decUserRef(); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 656 | } |
| 657 | mObjDestroy.mDestroyList.clear(); |
| 658 | mObjDestroy.mNeedToEmpty = false; |
| 659 | |
| 660 | status = pthread_mutex_unlock(&mObjDestroy.mMutex); |
| 661 | if (status) { |
| 662 | LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void Context::objDestroyOOBDestroy() |
| 668 | { |
| 669 | rsAssert(!mObjDestroy.mNeedToEmpty); |
| 670 | pthread_mutex_destroy(&mObjDestroy.mMutex); |
| 671 | } |
| 672 | |
| 673 | void Context::objDestroyAdd(ObjectBase *obj) |
| 674 | { |
| 675 | int status = pthread_mutex_lock(&mObjDestroy.mMutex); |
| 676 | if (status) { |
| 677 | LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | mObjDestroy.mNeedToEmpty = true; |
| 682 | mObjDestroy.mDestroyList.add(obj); |
| 683 | |
| 684 | status = pthread_mutex_unlock(&mObjDestroy.mMutex); |
| 685 | if (status) { |
| 686 | LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); |
| 687 | } |
| 688 | } |
| 689 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 690 | uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait) |
| 691 | { |
| 692 | //LOGE("getMessageToClient %i %i", bufferLen, wait); |
| 693 | if (!wait) { |
| 694 | if (mIO.mToClient.isEmpty()) { |
| 695 | // No message to get and not going to wait for one. |
| 696 | receiveLen = 0; |
| 697 | return 0; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | //LOGE("getMessageToClient 2 con=%p", this); |
| 702 | uint32_t bytesData = 0; |
| 703 | uint32_t commandID = 0; |
| 704 | const void *d = mIO.mToClient.get(&commandID, &bytesData); |
| 705 | //LOGE("getMessageToClient 3 %i %i", commandID, bytesData); |
| 706 | |
| 707 | *receiveLen = bytesData; |
| 708 | if (bufferLen >= bytesData) { |
| 709 | memcpy(data, d, bytesData); |
| 710 | mIO.mToClient.next(); |
| 711 | return commandID; |
| 712 | } |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace) |
| 717 | { |
| 718 | //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace); |
| 719 | if (cmdID == 0) { |
| 720 | LOGE("Attempting to send invalid command 0 to client."); |
| 721 | return false; |
| 722 | } |
| 723 | if (!waitForSpace) { |
| 724 | if (mIO.mToClient.getFreeSpace() < len) { |
| 725 | // Not enough room, and not waiting. |
| 726 | return false; |
| 727 | } |
| 728 | } |
| 729 | //LOGE("sendMessageToClient 2"); |
| 730 | void *p = mIO.mToClient.reserve(len); |
| 731 | memcpy(p, data, len); |
| 732 | mIO.mToClient.commit(cmdID, len); |
| 733 | //LOGE("sendMessageToClient 3"); |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | void Context::initToClient() |
| 738 | { |
| 739 | while(!mRunning) { |
| 740 | usleep(100); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | void Context::deinitToClient() |
| 745 | { |
| 746 | mIO.mToClient.shutdown(); |
| 747 | } |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 748 | |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 749 | void Context::dumpDebug() const |
| 750 | { |
| 751 | LOGE("RS Context debug %p", this); |
| 752 | LOGE("RS Context debug"); |
| 753 | |
| 754 | LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
| 755 | LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext, |
| 756 | mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay); |
| 757 | LOGE(" GL vendor: %s", mGL.mVendor); |
| 758 | LOGE(" GL renderer: %s", mGL.mRenderer); |
| 759 | LOGE(" GL Version: %s", mGL.mVersion); |
| 760 | LOGE(" GL Extensions: %s", mGL.mExtensions); |
| 761 | LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion); |
| 762 | LOGE(" RS width %i, height %i", mWidth, mHeight); |
| 763 | LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused); |
| 764 | LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId); |
| 765 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 766 | LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits); |
| 767 | LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs); |
| 768 | LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors); |
| 769 | LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 770 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 771 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 772 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 773 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 774 | |
| 775 | namespace android { |
| 776 | namespace renderscript { |
| 777 | |
| 778 | |
| 779 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) |
| 780 | { |
| 781 | Script *s = static_cast<Script *>(vs); |
| 782 | rsc->setRootScript(s); |
| 783 | } |
| 784 | |
| 785 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) |
| 786 | { |
| 787 | Sampler *s = static_cast<Sampler *>(vs); |
| 788 | |
| 789 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 790 | LOGE("Invalid sampler slot"); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | s->bindToContext(&rsc->mStateSampler, slot); |
| 795 | } |
| 796 | |
| 797 | void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs) |
| 798 | { |
| 799 | ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs); |
| 800 | rsc->setFragmentStore(pfs); |
| 801 | } |
| 802 | |
| 803 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) |
| 804 | { |
| 805 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
| 806 | rsc->setFragment(pf); |
| 807 | } |
| 808 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 809 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) |
| 810 | { |
| 811 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
| 812 | rsc->setRaster(pr); |
| 813 | } |
| 814 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 815 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) |
| 816 | { |
| 817 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
| 818 | rsc->setVertex(pv); |
| 819 | } |
| 820 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 821 | 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] | 822 | { |
| 823 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 824 | rsc->assignName(ob, name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 825 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 826 | |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 827 | void rsi_ObjDestroy(Context *rsc, void *obj) |
| 828 | { |
| 829 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 830 | rsc->removeName(ob); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 831 | ob->decUserRef(); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 834 | void rsi_ContextPause(Context *rsc) |
| 835 | { |
| 836 | rsc->pause(); |
| 837 | } |
| 838 | |
| 839 | void rsi_ContextResume(Context *rsc) |
| 840 | { |
| 841 | rsc->resume(); |
| 842 | } |
| 843 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 844 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur) |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 845 | { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 846 | rsc->setSurface(w, h, (Surface *)sur); |
| 847 | } |
| 848 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 849 | void rsi_ContextSetPriority(Context *rsc, int32_t p) |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 850 | { |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 851 | rsc->setPriority(p); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 854 | void rsi_ContextDump(Context *rsc, int32_t bits) |
| 855 | { |
| 856 | ObjectBase::dumpAll(rsc); |
| 857 | } |
| 858 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 863 | RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 864 | { |
| 865 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 866 | Context *rsc = new Context(dev, useDepth); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 867 | return rsc; |
| 868 | } |
| 869 | |
| 870 | void rsContextDestroy(RsContext vrsc) |
| 871 | { |
| 872 | Context * rsc = static_cast<Context *>(vrsc); |
| 873 | delete rsc; |
| 874 | } |
| 875 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 876 | void rsObjDestroyOOB(RsContext vrsc, void *obj) |
| 877 | { |
| 878 | Context * rsc = static_cast<Context *>(vrsc); |
| 879 | rsc->objDestroyAdd(static_cast<ObjectBase *>(obj)); |
| 880 | } |
| 881 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 882 | uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait) |
| 883 | { |
| 884 | Context * rsc = static_cast<Context *>(vrsc); |
| 885 | return rsc->getMessageToClient(data, receiveLen, bufferLen, wait); |
| 886 | } |
| 887 | |
| 888 | void rsContextInitToClient(RsContext vrsc) |
| 889 | { |
| 890 | Context * rsc = static_cast<Context *>(vrsc); |
| 891 | rsc->initToClient(); |
| 892 | } |
| 893 | |
| 894 | void rsContextDeinitToClient(RsContext vrsc) |
| 895 | { |
| 896 | Context * rsc = static_cast<Context *>(vrsc); |
| 897 | rsc->deinitToClient(); |
| 898 | } |
| 899 | |