Jason Sams | 326e0dd | 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 | 5ae678f | 2009-06-22 18:01:09 -0700 | [diff] [blame] | 20 | #include <ui/FramebufferNativeWindow.h> |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 21 | #include <ui/EGLUtils.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 22 | |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
| 24 | |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 25 | #include <GLES/gl.h> |
| 26 | #include <GLES/glext.h> |
| 27 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 28 | using namespace android; |
| 29 | using namespace android::renderscript; |
| 30 | |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 31 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 32 | uint32_t Context::gThreadTLSKeyCount = 0; |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 33 | uint32_t Context::gGLContextCount = 0; |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 34 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | |
Jason Sams | 33b6e3b | 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 | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 | void Context::initEGL() |
| 49 | { |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 50 | mEGL.mNumConfigs = -1; |
| 51 | EGLint configAttribs[128]; |
| 52 | EGLint *configAttribsPtr = configAttribs; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 53 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 54 | memset(configAttribs, 0, sizeof(configAttribs)); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 55 | |
Jason Sams | afcb25c | 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 | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | |
Jason Sams | afcb25c | 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 | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 65 | |
Jason Sams | 5fd09d8 | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 72 | configAttribsPtr[0] = EGL_NONE; |
| 73 | rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint)))); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 74 | |
Jason Sams | 6d751ef | 2009-10-08 12:55:06 -0700 | [diff] [blame] | 75 | LOGV("initEGL start"); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 76 | mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 77 | checkEglError("eglGetDisplay"); |
| 78 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 79 | eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 80 | checkEglError("eglInitialize"); |
Jason Sams | afcb25c | 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 | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 84 | LOGE("couldn't find an EGLConfig matching the screen format\n"); |
Jason Sams | afcb25c | 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 | 33b6e3b | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 99 | |
Jason Sams | 33b6e3b | 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 | afcb25c | 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 | 9397e30 | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 127 | |
Jason Sams | 306fb23 | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 129 | LOGE("Error, OpenGL ES Lite not supported"); |
Jason Sams | 306fb23 | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 132 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jason Sams | 33b6e3b | 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 | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 153 | bool Context::runScript(Script *s, uint32_t launchID) |
Jason Sams | 1030893 | 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 | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 158 | ObjectBaseRef<ProgramRaster> raster(mRaster); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 159 | |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 160 | bool ret = s->run(this, launchID); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 161 | |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 162 | mFragment.set(frag); |
| 163 | mVertex.set(vtx); |
| 164 | mFragmentStore.set(store); |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 165 | mRaster.set(raster); |
Jason Sams | c9d43db | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 166 | return ret; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 170 | bool Context::runRootScript() |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 171 | { |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 172 | if (props.mLogTimes) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 173 | timerSet(RS_TIMER_CLEAR_SWAP); |
| 174 | } |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 175 | rsAssert(mRootScript->mEnviroment.mIsRoot); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 176 | |
Jason Sams | 8c9534b | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 179 | glViewport(0, 0, mEGL.mWidth, mEGL.mHeight); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 180 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 181 | |
Jason Sams | 928b734 | 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 | afcb25c | 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 | 306fb23 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 193 | |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 194 | if (this->props.mLogTimes) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 195 | timerSet(RS_TIMER_SCRIPT); |
| 196 | } |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 197 | mStateFragmentStore.mLast.clear(); |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 198 | bool ret = runScript(mRootScript.get(), 0); |
Jason Sams | 8cfdd24 | 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 | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 205 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jason Sams | 24371d9 | 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 | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 225 | mTimeFrame = mTimeLast; |
| 226 | mTimeLastFrame = mTimeLast; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 227 | mTimerActive = RS_TIMER_INTERNAL; |
| 228 | timerReset(); |
| 229 | } |
| 230 | |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 231 | void Context::timerFrame() |
| 232 | { |
| 233 | mTimeLastFrame = mTimeFrame; |
| 234 | mTimeFrame = getTime(); |
| 235 | } |
| 236 | |
Jason Sams | 24371d9 | 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 | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 251 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 252 | |
Jason Sams | 1d54f10 | 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 | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 255 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000, |
Jason Sams | a57c0a7 | 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 | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 261 | void Context::setupCheck() |
| 262 | { |
Jason Sams | 5fd09d8 | 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 | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 269 | static bool getProp(const char *str) |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 270 | { |
| 271 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 272 | property_get(str, buf, "0"); |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 273 | return 0 != strcmp(buf, "0"); |
| 274 | } |
Jason Sams | 326e0dd | 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 | 1fddd90 | 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 | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 283 | |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 284 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 285 | rsc->initEGL(); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 286 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 287 | |
Jason Sams | e576910 | 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 | 5fd09d8 | 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 | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 302 | rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 303 | rsc->setVertex(NULL); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 304 | rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 305 | rsc->setFragment(NULL); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 306 | rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 307 | rsc->setFragmentStore(NULL); |
| 308 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 309 | rsc->mRunning = true; |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 310 | bool mDraw = true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 311 | while (!rsc->mExit) { |
Jason Sams | fcd3192 | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 312 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
Jason Sams | 732f1c0 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 313 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 314 | |
Jason Sams | 732f1c0 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 315 | if (mDraw) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 316 | mDraw = rsc->runRootScript() && !rsc->mPaused; |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 317 | if (rsc->props.mLogTimes) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 318 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
| 319 | } |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 320 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 321 | if (rsc->props.mLogTimes) { |
Joe Onorato | 76371ff | 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 | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 327 | } |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 328 | if (rsc->mObjDestroy.mNeedToEmpty) { |
| 329 | rsc->objDestroyOOBRun(); |
| 330 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 333 | LOGV("RS Thread exiting"); |
Jason Sams | f2649a9 | 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 | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 343 | ObjectBase::zeroAllUserRef(rsc); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 344 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 345 | glClearColor(0,0,0,0); |
| 346 | glClear(GL_COLOR_BUFFER_BIT); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 347 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 348 | |
| 349 | pthread_mutex_lock(&gInitMutex); |
| 350 | rsc->deinitEGL(); |
| 351 | pthread_mutex_unlock(&gInitMutex); |
| 352 | |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 353 | rsc->objDestroyOOBRun(); |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 354 | LOGV("RS Thread exited"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 358 | Context::Context(Device *dev, Surface *sur, bool useDepth) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 359 | { |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 360 | pthread_mutex_lock(&gInitMutex); |
| 361 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 362 | dev->addContext(this); |
| 363 | mDev = dev; |
| 364 | mRunning = false; |
| 365 | mExit = false; |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 366 | mUseDepth = useDepth; |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 367 | mPaused = false; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 368 | mObjHead = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 369 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 370 | int status; |
| 371 | pthread_attr_t threadAttr; |
| 372 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 373 | if (!gThreadTLSKeyCount) { |
Jason Sams | 9e4e13d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 374 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 375 | if (status) { |
| 376 | LOGE("Failed to init thread tls key."); |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 377 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 9e4e13d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 378 | return; |
| 379 | } |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 380 | } |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 381 | gThreadTLSKeyCount++; |
| 382 | pthread_mutex_unlock(&gInitMutex); |
| 383 | |
| 384 | // Global init done at this point. |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 385 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 386 | status = pthread_attr_init(&threadAttr); |
| 387 | if (status) { |
| 388 | LOGE("Failed to init thread attribute."); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | sched_param sparam; |
| 393 | sparam.sched_priority = ANDROID_PRIORITY_DISPLAY; |
| 394 | pthread_attr_setschedparam(&threadAttr, &sparam); |
| 395 | |
Jason Sams | 992a0b7 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 396 | mWndSurface = sur; |
| 397 | |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 398 | objDestroyOOBInit(); |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 399 | timerInit(); |
Jason Sams | a891933 | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 400 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 401 | |
Jason Sams | 992a0b7 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 402 | LOGV("RS Launching thread"); |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 403 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 404 | if (status) { |
| 405 | LOGE("Failed to start rs context thread."); |
| 406 | } |
| 407 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 408 | while(!mRunning) { |
Jason Sams | ada7f27 | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 409 | usleep(100); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 410 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 411 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 412 | pthread_attr_destroy(&threadAttr); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | Context::~Context() |
| 416 | { |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 417 | LOGV("Context::~Context"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 418 | mExit = true; |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 419 | mPaused = false; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 420 | void *res; |
| 421 | |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 422 | mIO.shutdown(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 423 | int status = pthread_join(mThreadId, &res); |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 424 | objDestroyOOBRun(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 425 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 426 | // Global structure cleanup. |
| 427 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 428 | if (mDev) { |
| 429 | mDev->removeContext(this); |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 430 | --gThreadTLSKeyCount; |
| 431 | if (!gThreadTLSKeyCount) { |
| 432 | pthread_key_delete(gThreadTLSKey); |
| 433 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 434 | } |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 435 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 436 | |
| 437 | objDestroyOOBDestroy(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 440 | void Context::pause() |
| 441 | { |
| 442 | mPaused = true; |
| 443 | } |
| 444 | |
| 445 | void Context::resume() |
| 446 | { |
| 447 | mPaused = false; |
| 448 | } |
| 449 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 450 | void Context::setRootScript(Script *s) |
| 451 | { |
| 452 | mRootScript.set(s); |
| 453 | } |
| 454 | |
| 455 | void Context::setFragmentStore(ProgramFragmentStore *pfs) |
| 456 | { |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 457 | if (pfs == NULL) { |
| 458 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 459 | } else { |
| 460 | mFragmentStore.set(pfs); |
| 461 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void Context::setFragment(ProgramFragment *pf) |
| 465 | { |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 466 | if (pf == NULL) { |
| 467 | mFragment.set(mStateFragment.mDefault); |
| 468 | } else { |
| 469 | mFragment.set(pf); |
| 470 | } |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 473 | void Context::setRaster(ProgramRaster *pr) |
| 474 | { |
| 475 | if (pr == NULL) { |
| 476 | mRaster.set(mStateRaster.mDefault); |
| 477 | } else { |
| 478 | mRaster.set(pr); |
| 479 | } |
| 480 | } |
| 481 | |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 482 | void Context::allocationCheck(const Allocation *a) |
| 483 | { |
| 484 | mVertex->checkUpdatedAllocation(a); |
| 485 | mFragment->checkUpdatedAllocation(a); |
| 486 | mFragmentStore->checkUpdatedAllocation(a); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void Context::setVertex(ProgramVertex *pv) |
| 490 | { |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 491 | if (pv == NULL) { |
| 492 | mVertex.set(mStateVertex.mDefault); |
| 493 | } else { |
| 494 | mVertex.set(pv); |
| 495 | } |
Jason Sams | c2f9490 | 2009-10-15 18:45:45 -0700 | [diff] [blame] | 496 | mVertex->forceDirty(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 499 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 500 | { |
| 501 | rsAssert(!obj->getName()); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 502 | obj->setName(name, len); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 503 | mNames.add(obj); |
| 504 | } |
| 505 | |
| 506 | void Context::removeName(ObjectBase *obj) |
| 507 | { |
| 508 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 509 | if (obj == mNames[ct]) { |
| 510 | mNames.removeAt(ct); |
| 511 | return; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | ObjectBase * Context::lookupName(const char *name) const |
| 517 | { |
| 518 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 519 | if (!strcmp(name, mNames[ct]->getName())) { |
| 520 | return mNames[ct]; |
| 521 | } |
| 522 | } |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 526 | void Context::appendNameDefines(String8 *str) const |
| 527 | { |
| 528 | char buf[256]; |
| 529 | for (size_t ct=0; ct < mNames.size(); ct++) { |
| 530 | str->append("#define NAMED_"); |
| 531 | str->append(mNames[ct]->getName()); |
| 532 | str->append(" "); |
| 533 | sprintf(buf, "%i\n", (int)mNames[ct]); |
| 534 | str->append(buf); |
| 535 | } |
| 536 | } |
| 537 | |
Joe Onorato | 57b79ce | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 538 | void Context::appendVarDefines(String8 *str) const |
| 539 | { |
| 540 | char buf[256]; |
| 541 | for (size_t ct=0; ct < mInt32Defines.size(); ct++) { |
| 542 | str->append("#define "); |
| 543 | str->append(mInt32Defines.keyAt(ct)); |
| 544 | str->append(" "); |
| 545 | sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); |
| 546 | str->append(buf); |
| 547 | |
| 548 | } |
| 549 | for (size_t ct=0; ct < mFloatDefines.size(); ct++) { |
| 550 | str->append("#define "); |
| 551 | str->append(mFloatDefines.keyAt(ct)); |
| 552 | str->append(" "); |
| 553 | sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); |
| 554 | str->append(buf); |
| 555 | } |
| 556 | } |
| 557 | |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 558 | bool Context::objDestroyOOBInit() |
| 559 | { |
| 560 | int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL); |
| 561 | if (status) { |
| 562 | LOGE("Context::ObjDestroyOOBInit mutex init failure"); |
| 563 | return false; |
| 564 | } |
| 565 | return true; |
| 566 | } |
| 567 | |
| 568 | void Context::objDestroyOOBRun() |
| 569 | { |
| 570 | if (mObjDestroy.mNeedToEmpty) { |
| 571 | int status = pthread_mutex_lock(&mObjDestroy.mMutex); |
| 572 | if (status) { |
| 573 | LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) { |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 578 | mObjDestroy.mDestroyList[ct]->decUserRef(); |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 579 | } |
| 580 | mObjDestroy.mDestroyList.clear(); |
| 581 | mObjDestroy.mNeedToEmpty = false; |
| 582 | |
| 583 | status = pthread_mutex_unlock(&mObjDestroy.mMutex); |
| 584 | if (status) { |
| 585 | LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | void Context::objDestroyOOBDestroy() |
| 591 | { |
| 592 | rsAssert(!mObjDestroy.mNeedToEmpty); |
| 593 | pthread_mutex_destroy(&mObjDestroy.mMutex); |
| 594 | } |
| 595 | |
| 596 | void Context::objDestroyAdd(ObjectBase *obj) |
| 597 | { |
| 598 | int status = pthread_mutex_lock(&mObjDestroy.mMutex); |
| 599 | if (status) { |
| 600 | LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | mObjDestroy.mNeedToEmpty = true; |
| 605 | mObjDestroy.mDestroyList.add(obj); |
| 606 | |
| 607 | status = pthread_mutex_unlock(&mObjDestroy.mMutex); |
| 608 | if (status) { |
| 609 | LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); |
| 610 | } |
| 611 | } |
| 612 | |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 613 | uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait) |
| 614 | { |
| 615 | //LOGE("getMessageToClient %i %i", bufferLen, wait); |
| 616 | if (!wait) { |
| 617 | if (mIO.mToClient.isEmpty()) { |
| 618 | // No message to get and not going to wait for one. |
| 619 | receiveLen = 0; |
| 620 | return 0; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | //LOGE("getMessageToClient 2 con=%p", this); |
| 625 | uint32_t bytesData = 0; |
| 626 | uint32_t commandID = 0; |
| 627 | const void *d = mIO.mToClient.get(&commandID, &bytesData); |
| 628 | //LOGE("getMessageToClient 3 %i %i", commandID, bytesData); |
| 629 | |
| 630 | *receiveLen = bytesData; |
| 631 | if (bufferLen >= bytesData) { |
| 632 | memcpy(data, d, bytesData); |
| 633 | mIO.mToClient.next(); |
| 634 | return commandID; |
| 635 | } |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace) |
| 640 | { |
| 641 | //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace); |
| 642 | if (cmdID == 0) { |
| 643 | LOGE("Attempting to send invalid command 0 to client."); |
| 644 | return false; |
| 645 | } |
| 646 | if (!waitForSpace) { |
| 647 | if (mIO.mToClient.getFreeSpace() < len) { |
| 648 | // Not enough room, and not waiting. |
| 649 | return false; |
| 650 | } |
| 651 | } |
| 652 | //LOGE("sendMessageToClient 2"); |
| 653 | void *p = mIO.mToClient.reserve(len); |
| 654 | memcpy(p, data, len); |
| 655 | mIO.mToClient.commit(cmdID, len); |
| 656 | //LOGE("sendMessageToClient 3"); |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | void Context::initToClient() |
| 661 | { |
| 662 | while(!mRunning) { |
| 663 | usleep(100); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void Context::deinitToClient() |
| 668 | { |
| 669 | mIO.mToClient.shutdown(); |
| 670 | } |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 671 | |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 672 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 673 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 674 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 675 | |
| 676 | namespace android { |
| 677 | namespace renderscript { |
| 678 | |
| 679 | |
| 680 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) |
| 681 | { |
| 682 | Script *s = static_cast<Script *>(vs); |
| 683 | rsc->setRootScript(s); |
| 684 | } |
| 685 | |
| 686 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) |
| 687 | { |
| 688 | Sampler *s = static_cast<Sampler *>(vs); |
| 689 | |
| 690 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 691 | LOGE("Invalid sampler slot"); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | s->bindToContext(&rsc->mStateSampler, slot); |
| 696 | } |
| 697 | |
| 698 | void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs) |
| 699 | { |
| 700 | ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs); |
| 701 | rsc->setFragmentStore(pfs); |
| 702 | } |
| 703 | |
| 704 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) |
| 705 | { |
| 706 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
| 707 | rsc->setFragment(pf); |
| 708 | } |
| 709 | |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 710 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) |
| 711 | { |
| 712 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
| 713 | rsc->setRaster(pr); |
| 714 | } |
| 715 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 716 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) |
| 717 | { |
| 718 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
| 719 | rsc->setVertex(pv); |
| 720 | } |
| 721 | |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 722 | void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 723 | { |
| 724 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 725 | rsc->assignName(ob, name, len); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 726 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 727 | |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 728 | void rsi_ObjDestroy(Context *rsc, void *obj) |
| 729 | { |
| 730 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 731 | rsc->removeName(ob); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 732 | ob->decUserRef(); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Joe Onorato | 57b79ce | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 735 | void rsi_ContextSetDefineF(Context *rsc, const char* name, float value) |
| 736 | { |
| 737 | rsc->addInt32Define(name, value); |
| 738 | } |
| 739 | |
| 740 | void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value) |
| 741 | { |
| 742 | rsc->addFloatDefine(name, value); |
| 743 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 744 | |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 745 | void rsi_ContextPause(Context *rsc) |
| 746 | { |
| 747 | rsc->pause(); |
| 748 | } |
| 749 | |
| 750 | void rsi_ContextResume(Context *rsc) |
| 751 | { |
| 752 | rsc->resume(); |
| 753 | } |
| 754 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 759 | RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 760 | { |
| 761 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 762 | Context *rsc = new Context(dev, (Surface *)sur, useDepth); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 763 | return rsc; |
| 764 | } |
| 765 | |
| 766 | void rsContextDestroy(RsContext vrsc) |
| 767 | { |
| 768 | Context * rsc = static_cast<Context *>(vrsc); |
| 769 | delete rsc; |
| 770 | } |
| 771 | |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 772 | void rsObjDestroyOOB(RsContext vrsc, void *obj) |
| 773 | { |
| 774 | Context * rsc = static_cast<Context *>(vrsc); |
| 775 | rsc->objDestroyAdd(static_cast<ObjectBase *>(obj)); |
| 776 | } |
| 777 | |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 778 | uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait) |
| 779 | { |
| 780 | Context * rsc = static_cast<Context *>(vrsc); |
| 781 | return rsc->getMessageToClient(data, receiveLen, bufferLen, wait); |
| 782 | } |
| 783 | |
| 784 | void rsContextInitToClient(RsContext vrsc) |
| 785 | { |
| 786 | Context * rsc = static_cast<Context *>(vrsc); |
| 787 | rsc->initToClient(); |
| 788 | } |
| 789 | |
| 790 | void rsContextDeinitToClient(RsContext vrsc) |
| 791 | { |
| 792 | Context * rsc = static_cast<Context *>(vrsc); |
| 793 | rsc->deinitToClient(); |
| 794 | } |
| 795 | |