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