Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "rsDevice.h" |
| 18 | #include "rsContext.h" |
| 19 | #include "rsThreadIO.h" |
Mathias Agopian | 3142f4f | 2009-06-22 18:01:09 -0700 | [diff] [blame] | 20 | #include <ui/FramebufferNativeWindow.h> |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 21 | #include <ui/EGLUtils.h> |
Mathias Agopian | 5cbb940 | 2010-02-12 12:00:38 -0800 | [diff] [blame] | 22 | #include <ui/egl/android_natives.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/resource.h> |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 26 | #include <sched.h> |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 27 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> |
| 29 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 30 | #include <GLES/gl.h> |
| 31 | #include <GLES/glext.h> |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 32 | #include <GLES2/gl2.h> |
| 33 | #include <GLES2/gl2ext.h> |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 34 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 35 | #include <cutils/sched_policy.h> |
| 36 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | using namespace android; |
| 38 | using namespace android::renderscript; |
| 39 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 40 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 41 | uint32_t Context::gThreadTLSKeyCount = 0; |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 42 | uint32_t Context::gGLContextCount = 0; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 43 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 45 | static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { |
| 46 | if (returnVal != EGL_TRUE) { |
| 47 | fprintf(stderr, "%s() returned %d\n", op, returnVal); |
| 48 | } |
| 49 | |
| 50 | for (EGLint error = eglGetError(); error != EGL_SUCCESS; error |
| 51 | = eglGetError()) { |
| 52 | fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error), |
| 53 | error); |
| 54 | } |
| 55 | } |
| 56 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 57 | void Context::initEGL(bool useGL2) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | { |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 59 | mEGL.mNumConfigs = -1; |
| 60 | EGLint configAttribs[128]; |
| 61 | EGLint *configAttribsPtr = configAttribs; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 62 | EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 64 | memset(configAttribs, 0, sizeof(configAttribs)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 65 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 66 | configAttribsPtr[0] = EGL_SURFACE_TYPE; |
| 67 | configAttribsPtr[1] = EGL_WINDOW_BIT; |
| 68 | configAttribsPtr += 2; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 69 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 70 | if (useGL2) { |
| 71 | configAttribsPtr[0] = EGL_RENDERABLE_TYPE; |
| 72 | configAttribsPtr[1] = EGL_OPENGL_ES2_BIT; |
| 73 | configAttribsPtr += 2; |
| 74 | } |
| 75 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 76 | if (mUseDepth) { |
| 77 | configAttribsPtr[0] = EGL_DEPTH_SIZE; |
| 78 | configAttribsPtr[1] = 16; |
| 79 | configAttribsPtr += 2; |
| 80 | } |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 81 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 82 | if (mDev->mForceSW) { |
| 83 | configAttribsPtr[0] = EGL_CONFIG_CAVEAT; |
| 84 | configAttribsPtr[1] = EGL_SLOW_CONFIG; |
| 85 | configAttribsPtr += 2; |
| 86 | } |
| 87 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 88 | configAttribsPtr[0] = EGL_NONE; |
| 89 | rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint)))); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 90 | |
Jason Sams | 6a17e16 | 2009-10-08 12:55:06 -0700 | [diff] [blame] | 91 | LOGV("initEGL start"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 92 | mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 93 | checkEglError("eglGetDisplay"); |
| 94 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 95 | eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 96 | checkEglError("eglInitialize"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 97 | |
| 98 | status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig); |
| 99 | if (err) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 100 | LOGE("couldn't find an EGLConfig matching the screen format\n"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 101 | } |
| 102 | //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs); |
| 103 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 104 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 105 | if (useGL2) { |
| 106 | mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2); |
| 107 | } else { |
| 108 | mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL); |
| 109 | } |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 110 | checkEglError("eglCreateContext"); |
| 111 | if (mEGL.mContext == EGL_NO_CONTEXT) { |
| 112 | LOGE("eglCreateContext returned EGL_NO_CONTEXT"); |
| 113 | } |
| 114 | gGLContextCount++; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 117 | void Context::deinitEGL() |
| 118 | { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 119 | LOGV("deinitEGL"); |
| 120 | setSurface(0, 0, NULL); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 121 | eglDestroyContext(mEGL.mDisplay, mEGL.mContext); |
| 122 | checkEglError("eglDestroyContext"); |
| 123 | |
| 124 | gGLContextCount--; |
| 125 | if (!gGLContextCount) { |
| 126 | eglTerminate(mEGL.mDisplay); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 131 | uint32_t Context::runScript(Script *s) |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 132 | { |
| 133 | ObjectBaseRef<ProgramFragment> frag(mFragment); |
| 134 | ObjectBaseRef<ProgramVertex> vtx(mVertex); |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 135 | ObjectBaseRef<ProgramStore> store(mFragmentStore); |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 136 | ObjectBaseRef<ProgramRaster> raster(mRaster); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 137 | ObjectBaseRef<Font> font(mFont); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 138 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 139 | uint32_t ret = s->run(this); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 140 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 141 | mFragment.set(frag); |
| 142 | mVertex.set(vtx); |
| 143 | mFragmentStore.set(store); |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 144 | mRaster.set(raster); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 145 | mFont.set(font); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 146 | return ret; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jason Sams | 718cd1f | 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 | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 156 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 157 | uint32_t Context::runRootScript() |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 158 | { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 159 | glViewport(0, 0, mWidth, mHeight); |
Jason Sams | 67c6844 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 160 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 161 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 162 | mStateFragmentStore.mLast.clear(); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 163 | uint32_t ret = runScript(mRootScript.get()); |
Jason Sams | c7412b3 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 164 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 165 | checkError("runRootScript"); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 166 | if (mError != RS_ERROR_NONE) { |
| 167 | // If we have an error condition we stop rendering until |
| 168 | // somthing changes that might fix it. |
| 169 | ret = 0; |
| 170 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 171 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 174 | uint64_t Context::getTime() const |
| 175 | { |
| 176 | struct timespec t; |
| 177 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 178 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
| 179 | } |
| 180 | |
| 181 | void Context::timerReset() |
| 182 | { |
| 183 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 184 | mTimers[ct] = 0; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void Context::timerInit() |
| 189 | { |
| 190 | mTimeLast = getTime(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 191 | mTimeFrame = mTimeLast; |
| 192 | mTimeLastFrame = mTimeLast; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 193 | mTimerActive = RS_TIMER_INTERNAL; |
| 194 | timerReset(); |
| 195 | } |
| 196 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 197 | void Context::timerFrame() |
| 198 | { |
| 199 | mTimeLastFrame = mTimeFrame; |
| 200 | mTimeFrame = getTime(); |
| 201 | } |
| 202 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 203 | void Context::timerSet(Timers tm) |
| 204 | { |
| 205 | uint64_t last = mTimeLast; |
| 206 | mTimeLast = getTime(); |
| 207 | mTimers[mTimerActive] += mTimeLast - last; |
| 208 | mTimerActive = tm; |
| 209 | } |
| 210 | |
| 211 | void Context::timerPrint() |
| 212 | { |
| 213 | double total = 0; |
| 214 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 215 | total += mTimers[ct]; |
| 216 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 217 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 218 | mTimeMSLastFrame = frame / 1000000; |
| 219 | mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; |
| 220 | mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 221 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 222 | |
| 223 | if (props.mLogTimes) { |
| 224 | LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)", |
| 225 | mTimeMSLastFrame, |
| 226 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, |
| 227 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, |
| 228 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
| 229 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000); |
| 230 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 233 | bool Context::setupCheck() |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 234 | { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 235 | if (checkVersion2_0()) { |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 236 | if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) { |
| 237 | LOGE("Context::setupCheck() 1 fail"); |
| 238 | return false; |
| 239 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 240 | |
| 241 | mFragmentStore->setupGL2(this, &mStateFragmentStore); |
| 242 | mFragment->setupGL2(this, &mStateFragment, &mShaderCache); |
| 243 | mRaster->setupGL2(this, &mStateRaster); |
| 244 | mVertex->setupGL2(this, &mStateVertex, &mShaderCache); |
| 245 | |
| 246 | } else { |
| 247 | mFragmentStore->setupGL(this, &mStateFragmentStore); |
| 248 | mFragment->setupGL(this, &mStateFragment); |
| 249 | mRaster->setupGL(this, &mStateRaster); |
| 250 | mVertex->setupGL(this, &mStateVertex); |
| 251 | } |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 252 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 255 | static bool getProp(const char *str) |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 256 | { |
| 257 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 258 | property_get(str, buf, "0"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 259 | return 0 != strcmp(buf, "0"); |
| 260 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 261 | |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 262 | void Context::displayDebugStats() |
| 263 | { |
| 264 | char buffer[128]; |
| 265 | sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript); |
| 266 | float oldR = mStateVertex.color[0]; |
| 267 | float oldG = mStateVertex.color[1]; |
| 268 | float oldB = mStateVertex.color[2]; |
| 269 | float oldA = mStateVertex.color[3]; |
| 270 | |
| 271 | float shadowCol = 0.2f; |
| 272 | mStateVertex.color[0] = shadowCol; |
| 273 | mStateVertex.color[1] = shadowCol; |
| 274 | mStateVertex.color[2] = shadowCol; |
| 275 | mStateVertex.color[3] = 1.0f; |
| 276 | if (!checkVersion2_0()) { |
| 277 | glColor4f(shadowCol, shadowCol, shadowCol, 1.0f); |
| 278 | } |
| 279 | mStateFont.renderText(buffer, 5, getHeight() - 5); |
| 280 | |
| 281 | float textCol = 0.9f; |
| 282 | mStateVertex.color[0] = textCol; |
| 283 | mStateVertex.color[1] = textCol; |
| 284 | mStateVertex.color[2] = textCol; |
| 285 | mStateVertex.color[3] = 1.0f; |
| 286 | if (!checkVersion2_0()) { |
| 287 | glColor4f(textCol, textCol, textCol, 1.0f); |
| 288 | } |
| 289 | mStateFont.renderText(buffer, 4, getHeight() - 6); |
| 290 | |
| 291 | mStateVertex.color[0] = oldR; |
| 292 | mStateVertex.color[1] = oldG; |
| 293 | mStateVertex.color[2] = oldB; |
| 294 | mStateVertex.color[3] = oldA; |
| 295 | if (!checkVersion2_0()) { |
| 296 | glColor4f(oldR, oldG, oldB, oldA); |
| 297 | } |
| 298 | } |
| 299 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 300 | void * Context::threadProc(void *vrsc) |
| 301 | { |
| 302 | Context *rsc = static_cast<Context *>(vrsc); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 303 | rsc->mNativeThreadId = gettid(); |
| 304 | |
| 305 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 306 | rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 307 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 308 | rsc->props.mLogTimes = getProp("debug.rs.profile"); |
| 309 | rsc->props.mLogScripts = getProp("debug.rs.script"); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 310 | rsc->props.mLogObjects = getProp("debug.rs.object"); |
| 311 | rsc->props.mLogShaders = getProp("debug.rs.shader"); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 312 | rsc->props.mLogVisual = getProp("debug.rs.visual"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 313 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 314 | ScriptTLSStruct *tlsStruct = new ScriptTLSStruct; |
| 315 | if (!tlsStruct) { |
| 316 | LOGE("Error allocating tls storage"); |
| 317 | return NULL; |
| 318 | } |
| 319 | tlsStruct->mContext = rsc; |
| 320 | tlsStruct->mScript = NULL; |
| 321 | int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct); |
| 322 | if (status) { |
| 323 | LOGE("pthread_setspecific %i", status); |
| 324 | } |
| 325 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 326 | if (rsc->mIsGraphicsContext) { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 327 | rsc->mStateRaster.init(rsc); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 328 | rsc->setRaster(NULL); |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 329 | rsc->mStateVertex.init(rsc); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 330 | rsc->setVertex(NULL); |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 331 | rsc->mStateFragment.init(rsc); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 332 | rsc->setFragment(NULL); |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 333 | rsc->mStateFragmentStore.init(rsc); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 334 | rsc->setFragmentStore(NULL); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 335 | rsc->mStateFont.init(rsc); |
| 336 | rsc->setFont(NULL); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 337 | rsc->mStateVertexArray.init(rsc); |
| 338 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 339 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 340 | rsc->mRunning = true; |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 341 | bool mDraw = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 342 | while (!rsc->mExit) { |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 343 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 344 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 345 | mDraw &= (rsc->mWndSurface != NULL); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 346 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 347 | uint32_t targetTime = 0; |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 348 | if (mDraw && rsc->mIsGraphicsContext) { |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 349 | targetTime = rsc->runRootScript(); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 350 | |
| 351 | if(rsc->props.mLogVisual) { |
| 352 | rsc->displayDebugStats(); |
| 353 | } |
| 354 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 355 | mDraw = targetTime && !rsc->mPaused; |
| 356 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 357 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 358 | rsc->timerFrame(); |
| 359 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 360 | rsc->timerPrint(); |
| 361 | rsc->timerReset(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 362 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 363 | if (rsc->mObjDestroy.mNeedToEmpty) { |
| 364 | rsc->objDestroyOOBRun(); |
| 365 | } |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 366 | if (rsc->mThreadPriority > 0 && targetTime) { |
| 367 | int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000; |
| 368 | if (t > 0) { |
| 369 | usleep(t); |
| 370 | } |
| 371 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 374 | LOGV("RS Thread exiting"); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 375 | if (rsc->mIsGraphicsContext) { |
| 376 | rsc->mRaster.clear(); |
| 377 | rsc->mFragment.clear(); |
| 378 | rsc->mVertex.clear(); |
| 379 | rsc->mFragmentStore.clear(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 380 | rsc->mFont.clear(); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 381 | rsc->mRootScript.clear(); |
| 382 | rsc->mStateRaster.deinit(rsc); |
| 383 | rsc->mStateVertex.deinit(rsc); |
| 384 | rsc->mStateFragment.deinit(rsc); |
| 385 | rsc->mStateFragmentStore.deinit(rsc); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 386 | rsc->mStateFont.deinit(rsc); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 387 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 388 | ObjectBase::zeroAllUserRef(rsc); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 389 | |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 390 | rsc->mObjDestroy.mNeedToEmpty = true; |
| 391 | rsc->objDestroyOOBRun(); |
| 392 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 393 | if (rsc->mIsGraphicsContext) { |
| 394 | pthread_mutex_lock(&gInitMutex); |
| 395 | rsc->deinitEGL(); |
| 396 | pthread_mutex_unlock(&gInitMutex); |
| 397 | } |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 398 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 399 | LOGV("RS Thread exited"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 400 | return NULL; |
| 401 | } |
| 402 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 403 | void * Context::helperThreadProc(void *vrsc) |
| 404 | { |
| 405 | Context *rsc = static_cast<Context *>(vrsc); |
| 406 | uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount); |
| 407 | |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 408 | LOGV("RS helperThread starting %p idx=%i", rsc, idx); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 409 | |
| 410 | rsc->mWorkers.mLaunchSignals[idx].init(); |
| 411 | rsc->mWorkers.mNativeThreadId[idx] = gettid(); |
| 412 | |
| 413 | //cpu_set_t cpset[16]; |
| 414 | //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset); |
| 415 | //LOGE("ret = %i", ret); |
| 416 | |
| 417 | //sched_setaffinity |
| 418 | |
| 419 | setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority); |
| 420 | while(rsc->mRunning) { |
| 421 | rsc->mWorkers.mLaunchSignals[idx].wait(); |
| 422 | if (rsc->mWorkers.mLaunchCallback) { |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 423 | rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx); |
| 424 | } |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 425 | android_atomic_dec(&rsc->mWorkers.mRunningCount); |
| 426 | rsc->mWorkers.mCompleteSignal.set(); |
| 427 | } |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 428 | |
| 429 | LOGV("RS helperThread exiting %p idx=%i", rsc, idx); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | void Context::launchThreads(WorkerCallback_t cbk, void *data) |
| 434 | { |
| 435 | mWorkers.mLaunchData = data; |
| 436 | mWorkers.mLaunchCallback = cbk; |
| 437 | mWorkers.mRunningCount = (int)mWorkers.mCount; |
| 438 | for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { |
| 439 | mWorkers.mLaunchSignals[ct].set(); |
| 440 | } |
| 441 | while(mWorkers.mRunningCount) { |
| 442 | mWorkers.mCompleteSignal.wait(); |
| 443 | } |
| 444 | } |
| 445 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 446 | void Context::setPriority(int32_t p) |
| 447 | { |
| 448 | // Note: If we put this in the proper "background" policy |
| 449 | // the wallpapers can become completly unresponsive at times. |
| 450 | // This is probably not what we want for something the user is actively |
| 451 | // looking at. |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 452 | mThreadPriority = p; |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 453 | #if 0 |
| 454 | SchedPolicy pol = SP_FOREGROUND; |
| 455 | if (p > 0) { |
| 456 | pol = SP_BACKGROUND; |
| 457 | } |
| 458 | if (!set_sched_policy(mNativeThreadId, pol)) { |
| 459 | // success; reset the priority as well |
| 460 | } |
| 461 | #else |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 462 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
| 463 | for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { |
| 464 | setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p); |
| 465 | } |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 466 | #endif |
| 467 | } |
| 468 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 469 | Context::Context(Device *dev, bool isGraphics, bool useDepth) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 470 | { |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 471 | pthread_mutex_lock(&gInitMutex); |
| 472 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 473 | dev->addContext(this); |
| 474 | mDev = dev; |
| 475 | mRunning = false; |
| 476 | mExit = false; |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 477 | mUseDepth = useDepth; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 478 | mPaused = false; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 479 | mObjHead = NULL; |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 480 | mError = RS_ERROR_NONE; |
| 481 | mErrorMsg = NULL; |
| 482 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 483 | memset(&mEGL, 0, sizeof(mEGL)); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 484 | memset(&mGL, 0, sizeof(mGL)); |
| 485 | mIsGraphicsContext = isGraphics; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 486 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 487 | int status; |
| 488 | pthread_attr_t threadAttr; |
| 489 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 490 | if (!gThreadTLSKeyCount) { |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 491 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 492 | if (status) { |
| 493 | LOGE("Failed to init thread tls key."); |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 494 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 495 | return; |
| 496 | } |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 497 | } |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 498 | gThreadTLSKeyCount++; |
| 499 | pthread_mutex_unlock(&gInitMutex); |
| 500 | |
| 501 | // Global init done at this point. |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 502 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 503 | status = pthread_attr_init(&threadAttr); |
| 504 | if (status) { |
| 505 | LOGE("Failed to init thread attribute."); |
| 506 | return; |
| 507 | } |
| 508 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 509 | mWndSurface = NULL; |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 510 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 511 | objDestroyOOBInit(); |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 512 | timerInit(); |
Jason Sams | d3f2eaf | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 513 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 514 | |
Jason Sams | 7f4d0c9 | 2010-07-26 17:12:55 -0700 | [diff] [blame] | 515 | int cpu = sysconf(_SC_NPROCESSORS_ONLN); |
| 516 | LOGV("RS Launching thread(s), reported CPU count %i", cpu); |
| 517 | if (cpu < 2) cpu = 0; |
| 518 | |
| 519 | mWorkers.mCount = (uint32_t)cpu; |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 520 | mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t)); |
| 521 | mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t)); |
| 522 | mWorkers.mLaunchSignals = new Signal[mWorkers.mCount]; |
| 523 | mWorkers.mLaunchCallback = NULL; |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 524 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 525 | if (status) { |
| 526 | LOGE("Failed to start rs context thread."); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 527 | return; |
| 528 | } |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 529 | while(!mRunning) { |
| 530 | usleep(100); |
| 531 | } |
| 532 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 533 | mWorkers.mRunningCount = 0; |
| 534 | mWorkers.mLaunchCount = 0; |
| 535 | for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { |
| 536 | status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this); |
| 537 | if (status) { |
| 538 | mWorkers.mCount = ct; |
| 539 | LOGE("Created fewer than expected number of RS threads."); |
| 540 | break; |
| 541 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 544 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 545 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | Context::~Context() |
| 549 | { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 550 | LOGV("Context::~Context"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 551 | mExit = true; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 552 | mPaused = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 553 | void *res; |
| 554 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 555 | mIO.shutdown(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 556 | int status = pthread_join(mThreadId, &res); |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 557 | mObjDestroy.mNeedToEmpty = true; |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 558 | objDestroyOOBRun(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 559 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 560 | // Global structure cleanup. |
| 561 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 562 | if (mDev) { |
| 563 | mDev->removeContext(this); |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 564 | --gThreadTLSKeyCount; |
| 565 | if (!gThreadTLSKeyCount) { |
| 566 | pthread_key_delete(gThreadTLSKey); |
| 567 | } |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 568 | mDev = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 569 | } |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 570 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 571 | |
| 572 | objDestroyOOBDestroy(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 575 | void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 576 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 577 | rsAssert(mIsGraphicsContext); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 578 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 579 | EGLBoolean ret; |
| 580 | if (mEGL.mSurface != NULL) { |
| 581 | ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 582 | checkEglError("eglMakeCurrent", ret); |
| 583 | |
| 584 | ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface); |
| 585 | checkEglError("eglDestroySurface", ret); |
| 586 | |
| 587 | mEGL.mSurface = NULL; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 588 | mWidth = 0; |
| 589 | mHeight = 0; |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | mWndSurface = sur; |
| 593 | if (mWndSurface != NULL) { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 594 | mWidth = w; |
| 595 | mHeight = h; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 596 | bool first = false; |
| 597 | if (!mEGL.mContext) { |
| 598 | first = true; |
| 599 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 600 | initEGL(true); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 601 | pthread_mutex_unlock(&gInitMutex); |
| 602 | } |
| 603 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 604 | mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL); |
| 605 | checkEglError("eglCreateWindowSurface"); |
| 606 | if (mEGL.mSurface == EGL_NO_SURFACE) { |
| 607 | LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE"); |
| 608 | } |
| 609 | |
| 610 | ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext); |
| 611 | checkEglError("eglMakeCurrent", ret); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 612 | |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 613 | mStateVertex.updateSize(this); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 614 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 615 | if (first) { |
| 616 | mGL.mVersion = glGetString(GL_VERSION); |
| 617 | mGL.mVendor = glGetString(GL_VENDOR); |
| 618 | mGL.mRenderer = glGetString(GL_RENDERER); |
| 619 | mGL.mExtensions = glGetString(GL_EXTENSIONS); |
| 620 | |
| 621 | //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
| 622 | LOGV("GL Version %s", mGL.mVersion); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 623 | //LOGV("GL Vendor %s", mGL.mVendor); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 624 | LOGV("GL Renderer %s", mGL.mRenderer); |
| 625 | //LOGV("GL Extensions %s", mGL.mExtensions); |
| 626 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 627 | const char *verptr = NULL; |
| 628 | if (strlen((const char *)mGL.mVersion) > 9) { |
| 629 | if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) { |
| 630 | verptr = (const char *)mGL.mVersion + 12; |
| 631 | } |
| 632 | if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) { |
| 633 | verptr = (const char *)mGL.mVersion + 9; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (!verptr) { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 638 | LOGE("Error, OpenGL ES Lite not supported"); |
| 639 | } else { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 640 | sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 641 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 642 | |
| 643 | glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs); |
| 644 | glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors); |
| 645 | glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits); |
| 646 | |
| 647 | glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors); |
| 648 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits); |
| 649 | |
| 650 | glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits); |
| 651 | glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors); |
Jason Sams | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 652 | |
| 653 | mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot"); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 659 | void Context::pause() |
| 660 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 661 | rsAssert(mIsGraphicsContext); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 662 | mPaused = true; |
| 663 | } |
| 664 | |
| 665 | void Context::resume() |
| 666 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 667 | rsAssert(mIsGraphicsContext); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 668 | mPaused = false; |
| 669 | } |
| 670 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 671 | void Context::setRootScript(Script *s) |
| 672 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 673 | rsAssert(mIsGraphicsContext); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 674 | mRootScript.set(s); |
| 675 | } |
| 676 | |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 677 | void Context::setFragmentStore(ProgramStore *pfs) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 678 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 679 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 680 | if (pfs == NULL) { |
| 681 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 682 | } else { |
| 683 | mFragmentStore.set(pfs); |
| 684 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void Context::setFragment(ProgramFragment *pf) |
| 688 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 689 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 690 | if (pf == NULL) { |
| 691 | mFragment.set(mStateFragment.mDefault); |
| 692 | } else { |
| 693 | mFragment.set(pf); |
| 694 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 697 | void Context::setRaster(ProgramRaster *pr) |
| 698 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 699 | rsAssert(mIsGraphicsContext); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 700 | if (pr == NULL) { |
| 701 | mRaster.set(mStateRaster.mDefault); |
| 702 | } else { |
| 703 | mRaster.set(pr); |
| 704 | } |
| 705 | } |
| 706 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 707 | void Context::setVertex(ProgramVertex *pv) |
| 708 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 709 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 710 | if (pv == NULL) { |
| 711 | mVertex.set(mStateVertex.mDefault); |
| 712 | } else { |
| 713 | mVertex.set(pv); |
| 714 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 717 | void Context::setFont(Font *f) |
| 718 | { |
| 719 | rsAssert(mIsGraphicsContext); |
| 720 | if (f == NULL) { |
| 721 | mFont.set(mStateFont.mDefault); |
| 722 | } else { |
| 723 | mFont.set(f); |
| 724 | } |
| 725 | } |
| 726 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 727 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 728 | { |
| 729 | rsAssert(!obj->getName()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 730 | obj->setName(name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 731 | mNames.add(obj); |
| 732 | } |
| 733 | |
| 734 | void Context::removeName(ObjectBase *obj) |
| 735 | { |
| 736 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 737 | if (obj == mNames[ct]) { |
| 738 | mNames.removeAt(ct); |
| 739 | return; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 744 | bool Context::objDestroyOOBInit() |
| 745 | { |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 746 | if (!mObjDestroy.mMutex.init()) { |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 747 | LOGE("Context::ObjDestroyOOBInit mutex init failure"); |
| 748 | return false; |
| 749 | } |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | void Context::objDestroyOOBRun() |
| 754 | { |
| 755 | if (mObjDestroy.mNeedToEmpty) { |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 756 | if (!mObjDestroy.mMutex.lock()) { |
| 757 | LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun."); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | |
| 761 | for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 762 | mObjDestroy.mDestroyList[ct]->decUserRef(); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 763 | } |
| 764 | mObjDestroy.mDestroyList.clear(); |
| 765 | mObjDestroy.mNeedToEmpty = false; |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 766 | mObjDestroy.mMutex.unlock(); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
| 770 | void Context::objDestroyOOBDestroy() |
| 771 | { |
| 772 | rsAssert(!mObjDestroy.mNeedToEmpty); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | void Context::objDestroyAdd(ObjectBase *obj) |
| 776 | { |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 777 | if (!mObjDestroy.mMutex.lock()) { |
| 778 | LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun."); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 779 | return; |
| 780 | } |
| 781 | |
| 782 | mObjDestroy.mNeedToEmpty = true; |
| 783 | mObjDestroy.mDestroyList.add(obj); |
Jason Sams | c1d726c | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 784 | mObjDestroy.mMutex.unlock(); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 787 | uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait) |
| 788 | { |
| 789 | //LOGE("getMessageToClient %i %i", bufferLen, wait); |
| 790 | if (!wait) { |
| 791 | if (mIO.mToClient.isEmpty()) { |
| 792 | // No message to get and not going to wait for one. |
| 793 | receiveLen = 0; |
| 794 | return 0; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | //LOGE("getMessageToClient 2 con=%p", this); |
| 799 | uint32_t bytesData = 0; |
| 800 | uint32_t commandID = 0; |
| 801 | const void *d = mIO.mToClient.get(&commandID, &bytesData); |
| 802 | //LOGE("getMessageToClient 3 %i %i", commandID, bytesData); |
| 803 | |
| 804 | *receiveLen = bytesData; |
| 805 | if (bufferLen >= bytesData) { |
| 806 | memcpy(data, d, bytesData); |
| 807 | mIO.mToClient.next(); |
| 808 | return commandID; |
| 809 | } |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace) |
| 814 | { |
| 815 | //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace); |
| 816 | if (cmdID == 0) { |
| 817 | LOGE("Attempting to send invalid command 0 to client."); |
| 818 | return false; |
| 819 | } |
| 820 | if (!waitForSpace) { |
| 821 | if (mIO.mToClient.getFreeSpace() < len) { |
| 822 | // Not enough room, and not waiting. |
| 823 | return false; |
| 824 | } |
| 825 | } |
| 826 | //LOGE("sendMessageToClient 2"); |
Jason Sams | 1796651 | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 827 | if (len > 0) { |
| 828 | void *p = mIO.mToClient.reserve(len); |
| 829 | memcpy(p, data, len); |
| 830 | mIO.mToClient.commit(cmdID, len); |
| 831 | } else { |
| 832 | mIO.mToClient.commit(cmdID, 0); |
| 833 | } |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 834 | //LOGE("sendMessageToClient 3"); |
| 835 | return true; |
| 836 | } |
| 837 | |
| 838 | void Context::initToClient() |
| 839 | { |
| 840 | while(!mRunning) { |
| 841 | usleep(100); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void Context::deinitToClient() |
| 846 | { |
| 847 | mIO.mToClient.shutdown(); |
| 848 | } |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 849 | |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 850 | const char * Context::getError(RsError *err) |
| 851 | { |
| 852 | *err = mError; |
| 853 | mError = RS_ERROR_NONE; |
| 854 | if (*err != RS_ERROR_NONE) { |
| 855 | return mErrorMsg; |
| 856 | } |
| 857 | return NULL; |
| 858 | } |
| 859 | |
| 860 | void Context::setError(RsError e, const char *msg) |
| 861 | { |
| 862 | mError = e; |
| 863 | mErrorMsg = msg; |
| 864 | } |
| 865 | |
| 866 | |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 867 | void Context::dumpDebug() const |
| 868 | { |
| 869 | LOGE("RS Context debug %p", this); |
| 870 | LOGE("RS Context debug"); |
| 871 | |
| 872 | LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 873 | LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 874 | LOGE(" GL vendor: %s", mGL.mVendor); |
| 875 | LOGE(" GL renderer: %s", mGL.mRenderer); |
| 876 | LOGE(" GL Version: %s", mGL.mVersion); |
| 877 | LOGE(" GL Extensions: %s", mGL.mExtensions); |
| 878 | LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion); |
| 879 | LOGE(" RS width %i, height %i", mWidth, mHeight); |
| 880 | LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused); |
| 881 | LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId); |
| 882 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 883 | LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits); |
| 884 | LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs); |
| 885 | LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors); |
| 886 | LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 887 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 888 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 889 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 890 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 891 | |
| 892 | namespace android { |
| 893 | namespace renderscript { |
| 894 | |
Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 895 | void rsi_ContextFinish(Context *rsc) |
| 896 | { |
| 897 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 898 | |
| 899 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) |
| 900 | { |
| 901 | Script *s = static_cast<Script *>(vs); |
| 902 | rsc->setRootScript(s); |
| 903 | } |
| 904 | |
| 905 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) |
| 906 | { |
| 907 | Sampler *s = static_cast<Sampler *>(vs); |
| 908 | |
| 909 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 910 | LOGE("Invalid sampler slot"); |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | s->bindToContext(&rsc->mStateSampler, slot); |
| 915 | } |
| 916 | |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 917 | void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 918 | { |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 919 | ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 920 | rsc->setFragmentStore(pfs); |
| 921 | } |
| 922 | |
| 923 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) |
| 924 | { |
| 925 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
| 926 | rsc->setFragment(pf); |
| 927 | } |
| 928 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 929 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) |
| 930 | { |
| 931 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
| 932 | rsc->setRaster(pr); |
| 933 | } |
| 934 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 935 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) |
| 936 | { |
| 937 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
| 938 | rsc->setVertex(pv); |
| 939 | } |
| 940 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 941 | void rsi_ContextBindFont(Context *rsc, RsFont vfont) |
| 942 | { |
| 943 | Font *font = static_cast<Font *>(vfont); |
| 944 | rsc->setFont(font); |
| 945 | } |
| 946 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 947 | void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 948 | { |
| 949 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 950 | rsc->assignName(ob, name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 951 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 952 | |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame^] | 953 | void rsi_GetName(Context *rsc, void * obj, const char **name) |
| 954 | { |
| 955 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 956 | (*name) = ob->getName(); |
| 957 | } |
| 958 | |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 959 | void rsi_ObjDestroy(Context *rsc, void *obj) |
| 960 | { |
| 961 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 962 | rsc->removeName(ob); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 963 | ob->decUserRef(); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 966 | void rsi_ContextPause(Context *rsc) |
| 967 | { |
| 968 | rsc->pause(); |
| 969 | } |
| 970 | |
| 971 | void rsi_ContextResume(Context *rsc) |
| 972 | { |
| 973 | rsc->resume(); |
| 974 | } |
| 975 | |
Dianne Hackborn | 8b49bd1 | 2010-06-30 13:56:17 -0700 | [diff] [blame] | 976 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 977 | { |
Mathias Agopian | 128ce4b | 2010-02-12 14:04:35 -0800 | [diff] [blame] | 978 | rsc->setSurface(w, h, sur); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 981 | void rsi_ContextSetPriority(Context *rsc, int32_t p) |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 982 | { |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 983 | rsc->setPriority(p); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 984 | } |
| 985 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 986 | void rsi_ContextDump(Context *rsc, int32_t bits) |
| 987 | { |
| 988 | ObjectBase::dumpAll(rsc); |
| 989 | } |
| 990 | |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 991 | const char * rsi_ContextGetError(Context *rsc, RsError *e) |
| 992 | { |
| 993 | const char *msg = rsc->getError(e); |
| 994 | if (*e != RS_ERROR_NONE) { |
| 995 | LOGE("RS Error %i %s", *e, msg); |
| 996 | } |
| 997 | return msg; |
| 998 | } |
| 999 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1004 | RsContext rsContextCreate(RsDevice vdev, uint32_t version) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1005 | { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1006 | LOGV("rsContextCreate %p", vdev); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1007 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1008 | Context *rsc = new Context(dev, false, false); |
| 1009 | return rsc; |
| 1010 | } |
| 1011 | |
| 1012 | RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth) |
| 1013 | { |
| 1014 | LOGV("rsContextCreateGL %p, %i", vdev, useDepth); |
| 1015 | Device * dev = static_cast<Device *>(vdev); |
| 1016 | Context *rsc = new Context(dev, true, useDepth); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1017 | return rsc; |
| 1018 | } |
| 1019 | |
| 1020 | void rsContextDestroy(RsContext vrsc) |
| 1021 | { |
| 1022 | Context * rsc = static_cast<Context *>(vrsc); |
| 1023 | delete rsc; |
| 1024 | } |
| 1025 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 1026 | void rsObjDestroyOOB(RsContext vrsc, void *obj) |
| 1027 | { |
| 1028 | Context * rsc = static_cast<Context *>(vrsc); |
| 1029 | rsc->objDestroyAdd(static_cast<ObjectBase *>(obj)); |
| 1030 | } |
| 1031 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1032 | uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait) |
| 1033 | { |
| 1034 | Context * rsc = static_cast<Context *>(vrsc); |
| 1035 | return rsc->getMessageToClient(data, receiveLen, bufferLen, wait); |
| 1036 | } |
| 1037 | |
| 1038 | void rsContextInitToClient(RsContext vrsc) |
| 1039 | { |
| 1040 | Context * rsc = static_cast<Context *>(vrsc); |
| 1041 | rsc->initToClient(); |
| 1042 | } |
| 1043 | |
| 1044 | void rsContextDeinitToClient(RsContext vrsc) |
| 1045 | { |
| 1046 | Context * rsc = static_cast<Context *>(vrsc); |
| 1047 | rsc->deinitToClient(); |
| 1048 | } |
| 1049 | |