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