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