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