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