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