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