Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "rsDevice.h" |
| 18 | #include "rsContext.h" |
| 19 | #include "rsThreadIO.h" |
Mathias Agopian | 5ae678f | 2009-06-22 18:01:09 -0700 | [diff] [blame] | 20 | #include <ui/FramebufferNativeWindow.h> |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 21 | #include <ui/PixelFormat.h> |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 22 | #include <ui/EGLUtils.h> |
Mathias Agopian | 9b97c29 | 2010-02-12 12:00:38 -0800 | [diff] [blame] | 23 | #include <ui/egl/android_natives.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/resource.h> |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 27 | #include <sched.h> |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 28 | |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
| 30 | |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 31 | #include <GLES/gl.h> |
| 32 | #include <GLES/glext.h> |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 33 | #include <GLES2/gl2.h> |
| 34 | #include <GLES2/gl2ext.h> |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 35 | |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 36 | #include <cutils/sched_policy.h> |
Jason Sams | 8d957fa | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 37 | #include <sys/syscall.h> |
| 38 | #include <string.h> |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 39 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | using namespace android; |
| 41 | using namespace android::renderscript; |
| 42 | |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 43 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 44 | uint32_t Context::gThreadTLSKeyCount = 0; |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 45 | uint32_t Context::gGLContextCount = 0; |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 46 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 47 | pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 | |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 49 | static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { |
| 50 | if (returnVal != EGL_TRUE) { |
| 51 | fprintf(stderr, "%s() returned %d\n", op, returnVal); |
| 52 | } |
| 53 | |
| 54 | for (EGLint error = eglGetError(); error != EGL_SUCCESS; error |
| 55 | = eglGetError()) { |
| 56 | fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error), |
| 57 | error); |
| 58 | } |
| 59 | } |
| 60 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 61 | void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) { |
| 62 | |
| 63 | #define X(VAL) {VAL, #VAL} |
| 64 | struct {EGLint attribute; const char* name;} names[] = { |
| 65 | X(EGL_BUFFER_SIZE), |
| 66 | X(EGL_ALPHA_SIZE), |
| 67 | X(EGL_BLUE_SIZE), |
| 68 | X(EGL_GREEN_SIZE), |
| 69 | X(EGL_RED_SIZE), |
| 70 | X(EGL_DEPTH_SIZE), |
| 71 | X(EGL_STENCIL_SIZE), |
| 72 | X(EGL_CONFIG_CAVEAT), |
| 73 | X(EGL_CONFIG_ID), |
| 74 | X(EGL_LEVEL), |
| 75 | X(EGL_MAX_PBUFFER_HEIGHT), |
| 76 | X(EGL_MAX_PBUFFER_PIXELS), |
| 77 | X(EGL_MAX_PBUFFER_WIDTH), |
| 78 | X(EGL_NATIVE_RENDERABLE), |
| 79 | X(EGL_NATIVE_VISUAL_ID), |
| 80 | X(EGL_NATIVE_VISUAL_TYPE), |
| 81 | X(EGL_SAMPLES), |
| 82 | X(EGL_SAMPLE_BUFFERS), |
| 83 | X(EGL_SURFACE_TYPE), |
| 84 | X(EGL_TRANSPARENT_TYPE), |
| 85 | X(EGL_TRANSPARENT_RED_VALUE), |
| 86 | X(EGL_TRANSPARENT_GREEN_VALUE), |
| 87 | X(EGL_TRANSPARENT_BLUE_VALUE), |
| 88 | X(EGL_BIND_TO_TEXTURE_RGB), |
| 89 | X(EGL_BIND_TO_TEXTURE_RGBA), |
| 90 | X(EGL_MIN_SWAP_INTERVAL), |
| 91 | X(EGL_MAX_SWAP_INTERVAL), |
| 92 | X(EGL_LUMINANCE_SIZE), |
| 93 | X(EGL_ALPHA_MASK_SIZE), |
| 94 | X(EGL_COLOR_BUFFER_TYPE), |
| 95 | X(EGL_RENDERABLE_TYPE), |
| 96 | X(EGL_CONFORMANT), |
| 97 | }; |
| 98 | #undef X |
| 99 | |
| 100 | for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) { |
| 101 | EGLint value = -1; |
| 102 | EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value); |
| 103 | EGLint error = eglGetError(); |
| 104 | if (returnVal && error == EGL_SUCCESS) { |
| 105 | LOGV(" %s: %d (0x%x)", names[j].name, value, value); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 111 | bool Context::initGLThread() { |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 112 | pthread_mutex_lock(&gInitMutex); |
| 113 | LOGV("initGLThread start %p", this); |
| 114 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 115 | mEGL.mNumConfigs = -1; |
| 116 | EGLint configAttribs[128]; |
| 117 | EGLint *configAttribsPtr = configAttribs; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 118 | EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 120 | memset(configAttribs, 0, sizeof(configAttribs)); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 121 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 122 | configAttribsPtr[0] = EGL_SURFACE_TYPE; |
| 123 | configAttribsPtr[1] = EGL_WINDOW_BIT; |
| 124 | configAttribsPtr += 2; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 126 | configAttribsPtr[0] = EGL_RENDERABLE_TYPE; |
| 127 | configAttribsPtr[1] = EGL_OPENGL_ES2_BIT; |
| 128 | configAttribsPtr += 2; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 129 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 130 | if (mUserSurfaceConfig.depthMin > 0) { |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 131 | configAttribsPtr[0] = EGL_DEPTH_SIZE; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 132 | configAttribsPtr[1] = mUserSurfaceConfig.depthMin; |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 133 | configAttribsPtr += 2; |
| 134 | } |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 135 | |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 136 | if (mDev->mForceSW) { |
| 137 | configAttribsPtr[0] = EGL_CONFIG_CAVEAT; |
| 138 | configAttribsPtr[1] = EGL_SLOW_CONFIG; |
| 139 | configAttribsPtr += 2; |
| 140 | } |
| 141 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 142 | configAttribsPtr[0] = EGL_NONE; |
| 143 | rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint)))); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 144 | |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 145 | LOGV("%p initEGL start", this); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 146 | mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 147 | checkEglError("eglGetDisplay"); |
| 148 | |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 149 | eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 150 | checkEglError("eglInitialize"); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 151 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 152 | #if 1 |
| 153 | PixelFormat pf = PIXEL_FORMAT_RGBA_8888; |
| 154 | if (mUserSurfaceConfig.alphaMin == 0) { |
| 155 | pf = PIXEL_FORMAT_RGBX_8888; |
| 156 | } |
| 157 | |
| 158 | status_t err = EGLUtils::selectConfigForPixelFormat(mEGL.mDisplay, configAttribs, pf, &mEGL.mConfig); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 159 | if (err) { |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 160 | LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 161 | } |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 162 | if (props.mLogVisual) { |
| 163 | printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 164 | } |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 165 | #else |
| 166 | eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs); |
| 167 | #endif |
| 168 | |
| 169 | mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2); |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 170 | checkEglError("eglCreateContext"); |
| 171 | if (mEGL.mContext == EGL_NO_CONTEXT) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 172 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 173 | LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 174 | return false; |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 175 | } |
| 176 | gGLContextCount++; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 177 | |
| 178 | |
| 179 | EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 180 | mEGL.mSurfaceDefault = eglCreatePbufferSurface(mEGL.mDisplay, mEGL.mConfig, pbuffer_attribs); |
| 181 | checkEglError("eglCreatePbufferSurface"); |
| 182 | if (mEGL.mSurfaceDefault == EGL_NO_SURFACE) { |
| 183 | LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE"); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 184 | pthread_mutex_unlock(&gInitMutex); |
| 185 | deinitEGL(); |
| 186 | return false; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 190 | if (ret == EGL_FALSE) { |
| 191 | LOGE("eglMakeCurrent returned EGL_FALSE"); |
| 192 | checkEglError("eglMakeCurrent", ret); |
| 193 | pthread_mutex_unlock(&gInitMutex); |
| 194 | deinitEGL(); |
| 195 | return false; |
| 196 | } |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 197 | |
| 198 | mGL.mVersion = glGetString(GL_VERSION); |
| 199 | mGL.mVendor = glGetString(GL_VENDOR); |
| 200 | mGL.mRenderer = glGetString(GL_RENDERER); |
| 201 | mGL.mExtensions = glGetString(GL_EXTENSIONS); |
| 202 | |
| 203 | //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 204 | //LOGV("GL Version %s", mGL.mVersion); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 205 | //LOGV("GL Vendor %s", mGL.mVendor); |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 206 | //LOGV("GL Renderer %s", mGL.mRenderer); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 207 | //LOGV("GL Extensions %s", mGL.mExtensions); |
| 208 | |
| 209 | const char *verptr = NULL; |
| 210 | if (strlen((const char *)mGL.mVersion) > 9) { |
| 211 | if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) { |
| 212 | verptr = (const char *)mGL.mVersion + 12; |
| 213 | } |
| 214 | if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) { |
| 215 | verptr = (const char *)mGL.mVersion + 9; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (!verptr) { |
| 220 | LOGE("Error, OpenGL ES Lite not supported"); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 221 | pthread_mutex_unlock(&gInitMutex); |
| 222 | deinitEGL(); |
| 223 | return false; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 224 | } else { |
| 225 | sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion); |
| 226 | } |
| 227 | |
| 228 | glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs); |
| 229 | glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors); |
| 230 | glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits); |
| 231 | |
| 232 | glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors); |
| 233 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits); |
| 234 | |
| 235 | glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits); |
| 236 | glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors); |
| 237 | |
| 238 | mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot"); |
Jason Sams | 0f7785c | 2011-01-13 17:02:35 -0800 | [diff] [blame] | 239 | mGL.GL_IMG_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_IMG_texture_npot"); |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 240 | mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)mGL.mExtensions, "GL_NV_texture_npot_2D_mipmap"); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 241 | mGL.EXT_texture_max_aniso = 1.0f; |
| 242 | bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic"); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 243 | if (hasAniso) { |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 244 | glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso); |
| 245 | } |
| 246 | |
| 247 | LOGV("initGLThread end %p", this); |
| 248 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 249 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 252 | void Context::deinitEGL() { |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 253 | LOGV("%p, deinitEGL", this); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 254 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 255 | if (mEGL.mContext != EGL_NO_CONTEXT) { |
Mathias Agopian | b7ee74d | 2011-01-19 16:42:02 -0800 | [diff] [blame] | 256 | eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 257 | eglDestroySurface(mEGL.mDisplay, mEGL.mSurfaceDefault); |
| 258 | if (mEGL.mSurface != EGL_NO_SURFACE) { |
| 259 | eglDestroySurface(mEGL.mDisplay, mEGL.mSurface); |
| 260 | } |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 261 | eglDestroyContext(mEGL.mDisplay, mEGL.mContext); |
| 262 | checkEglError("eglDestroyContext"); |
| 263 | } |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 264 | |
| 265 | gGLContextCount--; |
| 266 | if (!gGLContextCount) { |
| 267 | eglTerminate(mEGL.mDisplay); |
| 268 | } |
| 269 | } |
| 270 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 271 | Context::PushState::PushState(Context *con) { |
| 272 | mRsc = con; |
| 273 | mFragment.set(con->getProgramFragment()); |
| 274 | mVertex.set(con->getProgramVertex()); |
| 275 | mStore.set(con->getProgramStore()); |
| 276 | mRaster.set(con->getProgramRaster()); |
Alex Sakhartchouk | 5c0912b | 2010-11-18 16:36:47 -0800 | [diff] [blame] | 277 | mFont.set(con->getFont()); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | Context::PushState::~PushState() { |
| 281 | mRsc->setProgramFragment(mFragment.get()); |
| 282 | mRsc->setProgramVertex(mVertex.get()); |
| 283 | mRsc->setProgramStore(mStore.get()); |
| 284 | mRsc->setProgramRaster(mRaster.get()); |
| 285 | mRsc->setFont(mFont.get()); |
| 286 | } |
| 287 | |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 288 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 289 | uint32_t Context::runScript(Script *s) { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 290 | PushState(this); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 291 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 292 | uint32_t ret = s->run(this); |
Jason Sams | c9d43db | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 293 | return ret; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 296 | void Context::checkError(const char *msg, bool isFatal) const { |
| 297 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 298 | GLenum err = glGetError(); |
| 299 | if (err != GL_NO_ERROR) { |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 300 | char buf[1024]; |
| 301 | snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg); |
| 302 | |
| 303 | if (isFatal) { |
| 304 | setError(RS_ERROR_FATAL_DRIVER, buf); |
| 305 | } else { |
| 306 | switch (err) { |
| 307 | case GL_OUT_OF_MEMORY: |
| 308 | setError(RS_ERROR_OUT_OF_MEMORY, buf); |
| 309 | break; |
| 310 | default: |
| 311 | setError(RS_ERROR_DRIVER, buf); |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | LOGE("%p, %s", this, buf); |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 319 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 320 | uint32_t Context::runRootScript() { |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 321 | glViewport(0, 0, mWidth, mHeight); |
Jason Sams | 306fb23 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 322 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 323 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 324 | mStateFragmentStore.mLast.clear(); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 325 | uint32_t ret = runScript(mRootScript.get()); |
Jason Sams | 8cfdd24 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 326 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 327 | checkError("runRootScript"); |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 328 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 331 | uint64_t Context::getTime() const { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 332 | struct timespec t; |
| 333 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 334 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
| 335 | } |
| 336 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 337 | void Context::timerReset() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 338 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 339 | mTimers[ct] = 0; |
| 340 | } |
| 341 | } |
| 342 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 343 | void Context::timerInit() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 344 | mTimeLast = getTime(); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 345 | mTimeFrame = mTimeLast; |
| 346 | mTimeLastFrame = mTimeLast; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 347 | mTimerActive = RS_TIMER_INTERNAL; |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 348 | mAverageFPSFrameCount = 0; |
| 349 | mAverageFPSStartTime = mTimeLast; |
| 350 | mAverageFPS = 0; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 351 | timerReset(); |
| 352 | } |
| 353 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 354 | void Context::timerFrame() { |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 355 | mTimeLastFrame = mTimeFrame; |
| 356 | mTimeFrame = getTime(); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 357 | // Update average fps |
| 358 | const uint64_t averageFramerateInterval = 1000 * 1000000; |
| 359 | mAverageFPSFrameCount ++; |
| 360 | uint64_t inverval = mTimeFrame - mAverageFPSStartTime; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 361 | if (inverval >= averageFramerateInterval) { |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 362 | inverval = inverval / 1000000; |
| 363 | mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval; |
| 364 | mAverageFPSFrameCount = 0; |
| 365 | mAverageFPSStartTime = mTimeFrame; |
| 366 | } |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 369 | void Context::timerSet(Timers tm) { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 370 | uint64_t last = mTimeLast; |
| 371 | mTimeLast = getTime(); |
| 372 | mTimers[mTimerActive] += mTimeLast - last; |
| 373 | mTimerActive = tm; |
| 374 | } |
| 375 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 376 | void Context::timerPrint() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 377 | double total = 0; |
| 378 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 379 | total += mTimers[ct]; |
| 380 | } |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 381 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 382 | mTimeMSLastFrame = frame / 1000000; |
| 383 | mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; |
| 384 | mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 385 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 386 | |
| 387 | if (props.mLogTimes) { |
Alex Sakhartchouk | 64cd98e | 2010-10-18 17:18:50 -0700 | [diff] [blame] | 388 | LOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u", |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 389 | mTimeMSLastFrame, |
| 390 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, |
| 391 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, |
| 392 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 393 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000, |
| 394 | mAverageFPS); |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 395 | } |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 398 | bool Context::setupCheck() { |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 399 | if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) { |
| 400 | LOGE("Context::setupCheck() 1 fail"); |
| 401 | return false; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 402 | } |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 403 | |
| 404 | mFragmentStore->setupGL2(this, &mStateFragmentStore); |
| 405 | mFragment->setupGL2(this, &mStateFragment, &mShaderCache); |
| 406 | mRaster->setupGL2(this, &mStateRaster); |
| 407 | mVertex->setupGL2(this, &mStateVertex, &mShaderCache); |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 408 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 411 | void Context::setupProgramStore() { |
| 412 | mFragmentStore->setupGL2(this, &mStateFragmentStore); |
| 413 | } |
| 414 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 415 | static bool getProp(const char *str) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 416 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 417 | property_get(str, buf, "0"); |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 418 | return 0 != strcmp(buf, "0"); |
| 419 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 420 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 421 | void Context::displayDebugStats() { |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 422 | char buffer[128]; |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 423 | sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript); |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 424 | float oldR, oldG, oldB, oldA; |
| 425 | mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 426 | uint32_t bufferLen = strlen(buffer); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 427 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 428 | float shadowCol = 0.1f; |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 429 | mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 430 | mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 431 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 432 | mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 433 | mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 434 | |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 435 | mStateFont.setFontColor(oldR, oldG, oldB, oldA); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 438 | void * Context::threadProc(void *vrsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 439 | Context *rsc = static_cast<Context *>(vrsc); |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 440 | rsc->mNativeThreadId = gettid(); |
| 441 | |
| 442 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 443 | rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 444 | |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 445 | rsc->props.mLogTimes = getProp("debug.rs.profile"); |
| 446 | rsc->props.mLogScripts = getProp("debug.rs.script"); |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 447 | rsc->props.mLogObjects = getProp("debug.rs.object"); |
| 448 | rsc->props.mLogShaders = getProp("debug.rs.shader"); |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 449 | rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes"); |
| 450 | rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms"); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 451 | rsc->props.mLogVisual = getProp("debug.rs.visual"); |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 452 | |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 453 | rsc->mTlsStruct = new ScriptTLSStruct; |
| 454 | if (!rsc->mTlsStruct) { |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 455 | LOGE("Error allocating tls storage"); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 456 | rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS"); |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 457 | return NULL; |
| 458 | } |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 459 | rsc->mTlsStruct->mContext = rsc; |
| 460 | rsc->mTlsStruct->mScript = NULL; |
| 461 | int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct); |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 462 | if (status) { |
| 463 | LOGE("pthread_setspecific %i", status); |
| 464 | } |
| 465 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 466 | if (!rsc->initGLThread()) { |
| 467 | rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL"); |
| 468 | return NULL; |
| 469 | } |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 470 | |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 471 | if (rsc->mIsGraphicsContext) { |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 472 | rsc->mStateRaster.init(rsc); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 473 | rsc->setProgramRaster(NULL); |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 474 | rsc->mStateVertex.init(rsc); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 475 | rsc->setProgramVertex(NULL); |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 476 | rsc->mStateFragment.init(rsc); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 477 | rsc->setProgramFragment(NULL); |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 478 | rsc->mStateFragmentStore.init(rsc); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 479 | rsc->setProgramStore(NULL); |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 480 | rsc->mStateFont.init(rsc); |
| 481 | rsc->setFont(NULL); |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 482 | rsc->mStateVertexArray.init(rsc); |
| 483 | } |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 484 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 485 | rsc->mRunning = true; |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 486 | bool mDraw = true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 487 | while (!rsc->mExit) { |
Jason Sams | fcd3192 | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 488 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
Jason Sams | 732f1c0 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 489 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 490 | mDraw &= (rsc->mWndSurface != NULL); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 491 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 492 | uint32_t targetTime = 0; |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 493 | if (mDraw && rsc->mIsGraphicsContext) { |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 494 | targetTime = rsc->runRootScript(); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 495 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 496 | if (rsc->props.mLogVisual) { |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 497 | rsc->displayDebugStats(); |
| 498 | } |
| 499 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 500 | mDraw = targetTime && !rsc->mPaused; |
| 501 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
Jason Sams | afcb25c | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 502 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 503 | rsc->timerFrame(); |
| 504 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 505 | rsc->timerPrint(); |
| 506 | rsc->timerReset(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 507 | } |
Jason Sams | 177f844 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 508 | if (targetTime > 1) { |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 509 | int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000; |
| 510 | if (t > 0) { |
| 511 | usleep(t); |
| 512 | } |
| 513 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 516 | LOGV("%p, RS Thread exiting", rsc); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 517 | |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 518 | if (rsc->mIsGraphicsContext) { |
| 519 | pthread_mutex_lock(&gInitMutex); |
| 520 | rsc->deinitEGL(); |
| 521 | pthread_mutex_unlock(&gInitMutex); |
| 522 | } |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 523 | delete rsc->mTlsStruct; |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 524 | |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 525 | LOGV("%p, RS Thread exited", rsc); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 526 | return NULL; |
| 527 | } |
| 528 | |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 529 | void Context::destroyWorkerThreadResources() { |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 530 | //LOGV("destroyWorkerThreadResources 1"); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 531 | if (mIsGraphicsContext) { |
| 532 | mRaster.clear(); |
| 533 | mFragment.clear(); |
| 534 | mVertex.clear(); |
| 535 | mFragmentStore.clear(); |
| 536 | mFont.clear(); |
| 537 | mRootScript.clear(); |
| 538 | mStateRaster.deinit(this); |
| 539 | mStateVertex.deinit(this); |
| 540 | mStateFragment.deinit(this); |
| 541 | mStateFragmentStore.deinit(this); |
| 542 | mStateFont.deinit(this); |
Jason Sams | 2cbfc4c | 2011-01-05 03:37:48 -0800 | [diff] [blame] | 543 | mShaderCache.cleanupAll(); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 544 | } |
| 545 | ObjectBase::zeroAllUserRef(this); |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 546 | //LOGV("destroyWorkerThreadResources 2"); |
Jason Sams | cf912de | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 547 | mExit = true; |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 550 | void * Context::helperThreadProc(void *vrsc) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 551 | Context *rsc = static_cast<Context *>(vrsc); |
| 552 | uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount); |
| 553 | |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 554 | //LOGV("RS helperThread starting %p idx=%i", rsc, idx); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 555 | |
| 556 | rsc->mWorkers.mLaunchSignals[idx].init(); |
| 557 | rsc->mWorkers.mNativeThreadId[idx] = gettid(); |
| 558 | |
Jason Sams | 8d957fa | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 559 | #if 0 |
| 560 | typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t; |
| 561 | cpu_set_t cpuset; |
| 562 | memset(&cpuset, 0, sizeof(cpuset)); |
| 563 | cpuset.bits[idx / 64] |= 1ULL << (idx % 64); |
| 564 | int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx], |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 565 | sizeof(cpuset), &cpuset); |
Jason Sams | 8d957fa | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 566 | LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret)); |
| 567 | #endif |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 568 | |
| 569 | setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority); |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 570 | int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct); |
| 571 | if (status) { |
| 572 | LOGE("pthread_setspecific %i", status); |
| 573 | } |
| 574 | |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 575 | while (!rsc->mExit) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 576 | rsc->mWorkers.mLaunchSignals[idx].wait(); |
| 577 | if (rsc->mWorkers.mLaunchCallback) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 578 | rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx); |
| 579 | } |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 580 | android_atomic_dec(&rsc->mWorkers.mRunningCount); |
| 581 | rsc->mWorkers.mCompleteSignal.set(); |
| 582 | } |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 583 | |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 584 | //LOGV("RS helperThread exited %p idx=%i", rsc, idx); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 585 | return NULL; |
| 586 | } |
| 587 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 588 | void Context::launchThreads(WorkerCallback_t cbk, void *data) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 589 | mWorkers.mLaunchData = data; |
| 590 | mWorkers.mLaunchCallback = cbk; |
| 591 | mWorkers.mRunningCount = (int)mWorkers.mCount; |
| 592 | for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { |
| 593 | mWorkers.mLaunchSignals[ct].set(); |
| 594 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 595 | while (mWorkers.mRunningCount) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 596 | mWorkers.mCompleteSignal.wait(); |
| 597 | } |
| 598 | } |
| 599 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 600 | void Context::setPriority(int32_t p) { |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 601 | // Note: If we put this in the proper "background" policy |
| 602 | // the wallpapers can become completly unresponsive at times. |
| 603 | // This is probably not what we want for something the user is actively |
| 604 | // looking at. |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 605 | mThreadPriority = p; |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 606 | #if 0 |
| 607 | SchedPolicy pol = SP_FOREGROUND; |
| 608 | if (p > 0) { |
| 609 | pol = SP_BACKGROUND; |
| 610 | } |
| 611 | if (!set_sched_policy(mNativeThreadId, pol)) { |
| 612 | // success; reset the priority as well |
| 613 | } |
| 614 | #else |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 615 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
| 616 | for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { |
| 617 | setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p); |
| 618 | } |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 619 | #endif |
| 620 | } |
| 621 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 622 | Context::Context() { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 623 | mDev = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 624 | mRunning = false; |
| 625 | mExit = false; |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 626 | mPaused = false; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 627 | mObjHead = NULL; |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 628 | mError = RS_ERROR_NONE; |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 631 | Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 632 | Context * rsc = new Context(); |
| 633 | if (!rsc->initContext(dev, sc)) { |
| 634 | delete rsc; |
| 635 | return NULL; |
| 636 | } |
| 637 | return rsc; |
| 638 | } |
| 639 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 640 | bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 641 | pthread_mutex_lock(&gInitMutex); |
| 642 | |
| 643 | dev->addContext(this); |
| 644 | mDev = dev; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 645 | if (sc) { |
| 646 | mUserSurfaceConfig = *sc; |
| 647 | } else { |
| 648 | memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); |
| 649 | } |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 650 | |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 651 | memset(&mEGL, 0, sizeof(mEGL)); |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 652 | memset(&mGL, 0, sizeof(mGL)); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 653 | mIsGraphicsContext = sc != NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 654 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 655 | int status; |
| 656 | pthread_attr_t threadAttr; |
| 657 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 658 | if (!gThreadTLSKeyCount) { |
Jason Sams | 9e4e13d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 659 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 660 | if (status) { |
| 661 | LOGE("Failed to init thread tls key."); |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 662 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 663 | return false; |
Jason Sams | 9e4e13d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 664 | } |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 665 | } |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 666 | gThreadTLSKeyCount++; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 667 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 668 | pthread_mutex_unlock(&gInitMutex); |
| 669 | |
| 670 | // Global init done at this point. |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 671 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 672 | status = pthread_attr_init(&threadAttr); |
| 673 | if (status) { |
| 674 | LOGE("Failed to init thread attribute."); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 675 | return false; |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 678 | mWndSurface = NULL; |
Jason Sams | 992a0b7 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 679 | |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 680 | timerInit(); |
Jason Sams | a891933 | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 681 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 682 | |
Jason Sams | 7c7c78a | 2010-07-26 17:12:55 -0700 | [diff] [blame] | 683 | int cpu = sysconf(_SC_NPROCESSORS_ONLN); |
| 684 | LOGV("RS Launching thread(s), reported CPU count %i", cpu); |
| 685 | if (cpu < 2) cpu = 0; |
| 686 | |
| 687 | mWorkers.mCount = (uint32_t)cpu; |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 688 | mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t)); |
| 689 | mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t)); |
| 690 | mWorkers.mLaunchSignals = new Signal[mWorkers.mCount]; |
| 691 | mWorkers.mLaunchCallback = NULL; |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 692 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 693 | if (status) { |
| 694 | LOGE("Failed to start rs context thread."); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 695 | return false; |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 696 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 697 | while (!mRunning && (mError == RS_ERROR_NONE)) { |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 698 | usleep(100); |
| 699 | } |
| 700 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 701 | if (mError != RS_ERROR_NONE) { |
| 702 | return false; |
| 703 | } |
| 704 | |
Jason Sams | 8d957fa | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 705 | mWorkers.mCompleteSignal.init(); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 706 | mWorkers.mRunningCount = 0; |
| 707 | mWorkers.mLaunchCount = 0; |
| 708 | for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { |
| 709 | status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this); |
| 710 | if (status) { |
| 711 | mWorkers.mCount = ct; |
| 712 | LOGE("Created fewer than expected number of RS threads."); |
| 713 | break; |
| 714 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 715 | } |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 716 | pthread_attr_destroy(&threadAttr); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 717 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 720 | Context::~Context() { |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 721 | LOGV("Context::~Context"); |
Jason Sams | cf912de | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 722 | |
| 723 | mIO.mToCore.flush(); |
| 724 | rsAssert(mExit); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 725 | mExit = true; |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 726 | mPaused = false; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 727 | void *res; |
| 728 | |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 729 | mIO.shutdown(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 730 | int status = pthread_join(mThreadId, &res); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 731 | |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame^] | 732 | // Cleanup compute threads. |
| 733 | mWorkers.mLaunchData = NULL; |
| 734 | mWorkers.mLaunchCallback = NULL; |
| 735 | mWorkers.mRunningCount = (int)mWorkers.mCount; |
| 736 | for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { |
| 737 | mWorkers.mLaunchSignals[ct].set(); |
| 738 | int status = pthread_join(mWorkers.mThreadId[ct], &res); |
| 739 | } |
| 740 | rsAssert(!mWorkers.mRunningCount); |
| 741 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 742 | // Global structure cleanup. |
| 743 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 744 | if (mDev) { |
| 745 | mDev->removeContext(this); |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 746 | --gThreadTLSKeyCount; |
| 747 | if (!gThreadTLSKeyCount) { |
| 748 | pthread_key_delete(gThreadTLSKey); |
| 749 | } |
Jason Sams | bf3c14e | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 750 | mDev = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 751 | } |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 752 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 753 | LOGV("Context::~Context done"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 756 | void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 757 | rsAssert(mIsGraphicsContext); |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 758 | |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 759 | EGLBoolean ret; |
Jason Sams | 52a67b2 | 2011-01-12 15:26:25 -0800 | [diff] [blame] | 760 | // WAR: Some drivers fail to handle 0 size surfaces correcntly. |
| 761 | // Use the pbuffer to avoid this pitfall. |
| 762 | if ((mEGL.mSurface != NULL) || (w == 0) || (h == 0)) { |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 763 | ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 764 | checkEglError("eglMakeCurrent", ret); |
| 765 | |
| 766 | ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface); |
| 767 | checkEglError("eglDestroySurface", ret); |
| 768 | |
| 769 | mEGL.mSurface = NULL; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 770 | mWidth = 1; |
| 771 | mHeight = 1; |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | mWndSurface = sur; |
| 775 | if (mWndSurface != NULL) { |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 776 | mWidth = w; |
| 777 | mHeight = h; |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 778 | |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 779 | mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL); |
| 780 | checkEglError("eglCreateWindowSurface"); |
| 781 | if (mEGL.mSurface == EGL_NO_SURFACE) { |
| 782 | LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE"); |
| 783 | } |
| 784 | |
| 785 | ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext); |
| 786 | checkEglError("eglMakeCurrent", ret); |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 787 | |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 788 | mStateVertex.updateSize(this); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 792 | void Context::pause() { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 793 | rsAssert(mIsGraphicsContext); |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 794 | mPaused = true; |
| 795 | } |
| 796 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 797 | void Context::resume() { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 798 | rsAssert(mIsGraphicsContext); |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 799 | mPaused = false; |
| 800 | } |
| 801 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 802 | void Context::setRootScript(Script *s) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 803 | rsAssert(mIsGraphicsContext); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 804 | mRootScript.set(s); |
| 805 | } |
| 806 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 807 | void Context::setProgramStore(ProgramStore *pfs) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 808 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 809 | if (pfs == NULL) { |
| 810 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 811 | } else { |
| 812 | mFragmentStore.set(pfs); |
| 813 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 816 | void Context::setProgramFragment(ProgramFragment *pf) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 817 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 818 | if (pf == NULL) { |
| 819 | mFragment.set(mStateFragment.mDefault); |
| 820 | } else { |
| 821 | mFragment.set(pf); |
| 822 | } |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 825 | void Context::setProgramRaster(ProgramRaster *pr) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 826 | rsAssert(mIsGraphicsContext); |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 827 | if (pr == NULL) { |
| 828 | mRaster.set(mStateRaster.mDefault); |
| 829 | } else { |
| 830 | mRaster.set(pr); |
| 831 | } |
| 832 | } |
| 833 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 834 | void Context::setProgramVertex(ProgramVertex *pv) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 835 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 836 | if (pv == NULL) { |
| 837 | mVertex.set(mStateVertex.mDefault); |
| 838 | } else { |
| 839 | mVertex.set(pv); |
| 840 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 843 | void Context::setFont(Font *f) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 844 | rsAssert(mIsGraphicsContext); |
| 845 | if (f == NULL) { |
| 846 | mFont.set(mStateFont.mDefault); |
| 847 | } else { |
| 848 | mFont.set(f); |
| 849 | } |
| 850 | } |
| 851 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 852 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 853 | rsAssert(!obj->getName()); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 854 | obj->setName(name, len); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 855 | mNames.add(obj); |
| 856 | } |
| 857 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 858 | void Context::removeName(ObjectBase *obj) { |
| 859 | for (size_t ct=0; ct < mNames.size(); ct++) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 860 | if (obj == mNames[ct]) { |
| 861 | mNames.removeAt(ct); |
| 862 | return; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 867 | RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) { |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 868 | *receiveLen = 0; |
| 869 | if (!wait && mIO.mToClient.isEmpty()) { |
| 870 | return RS_MESSAGE_TO_CLIENT_NONE; |
| 871 | } |
| 872 | |
| 873 | uint32_t bytesData = 0; |
| 874 | uint32_t commandID = 0; |
| 875 | const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData); |
| 876 | *receiveLen = bytesData - sizeof(uint32_t); |
| 877 | if (bytesData) { |
| 878 | *subID = d[0]; |
| 879 | } |
| 880 | return (RsMessageToClientType)commandID; |
| 881 | } |
| 882 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 883 | RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 884 | //LOGE("getMessageToClient %i %i", bufferLen, wait); |
Jason Sams | 39cd317 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 885 | *receiveLen = 0; |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 886 | if (!wait && mIO.mToClient.isEmpty()) { |
| 887 | return RS_MESSAGE_TO_CLIENT_NONE; |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | //LOGE("getMessageToClient 2 con=%p", this); |
| 891 | uint32_t bytesData = 0; |
| 892 | uint32_t commandID = 0; |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 893 | const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 894 | //LOGE("getMessageToClient 3 %i %i", commandID, bytesData); |
| 895 | |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 896 | *receiveLen = bytesData - sizeof(uint32_t); |
| 897 | *subID = d[0]; |
| 898 | |
| 899 | //LOGE("getMessageToClient %i %i", commandID, *subID); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 900 | if (bufferLen >= bytesData) { |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 901 | memcpy(data, d+1, *receiveLen); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 902 | mIO.mToClient.next(); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 903 | return (RsMessageToClientType)commandID; |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 904 | } |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 905 | return RS_MESSAGE_TO_CLIENT_RESIZE; |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 908 | bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, |
| 909 | uint32_t subID, size_t len, bool waitForSpace) const { |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 910 | //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 911 | if (cmdID == 0) { |
| 912 | LOGE("Attempting to send invalid command 0 to client."); |
| 913 | return false; |
| 914 | } |
| 915 | if (!waitForSpace) { |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 916 | if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 917 | // Not enough room, and not waiting. |
| 918 | return false; |
| 919 | } |
| 920 | } |
| 921 | //LOGE("sendMessageToClient 2"); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 922 | uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID)); |
| 923 | p[0] = subID; |
Jason Sams | ef5867a | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 924 | if (len > 0) { |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 925 | memcpy(p+1, data, len); |
Jason Sams | ef5867a | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 926 | } |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 927 | mIO.mToClient.commit(cmdID, len + sizeof(subID)); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 928 | //LOGE("sendMessageToClient 3"); |
| 929 | return true; |
| 930 | } |
| 931 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 932 | void Context::initToClient() { |
| 933 | while (!mRunning) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 934 | usleep(100); |
| 935 | } |
| 936 | } |
| 937 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 938 | void Context::deinitToClient() { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 939 | mIO.mToClient.shutdown(); |
| 940 | } |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 941 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 942 | void Context::setError(RsError e, const char *msg) const { |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 943 | mError = e; |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 944 | sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 948 | void Context::dumpDebug() const { |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 949 | LOGE("RS Context debug %p", this); |
| 950 | LOGE("RS Context debug"); |
| 951 | |
| 952 | LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 953 | LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay); |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 954 | LOGE(" GL vendor: %s", mGL.mVendor); |
| 955 | LOGE(" GL renderer: %s", mGL.mRenderer); |
| 956 | LOGE(" GL Version: %s", mGL.mVersion); |
| 957 | LOGE(" GL Extensions: %s", mGL.mExtensions); |
| 958 | LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion); |
| 959 | LOGE(" RS width %i, height %i", mWidth, mHeight); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 960 | LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 961 | LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId); |
| 962 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 963 | LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits); |
| 964 | LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs); |
| 965 | LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors); |
| 966 | LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors); |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 967 | } |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 968 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 969 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 970 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 971 | |
| 972 | namespace android { |
| 973 | namespace renderscript { |
| 974 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 975 | void rsi_ContextFinish(Context *rsc) { |
Jason Sams | 8c88090 | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 976 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 977 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 978 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 979 | Script *s = static_cast<Script *>(vs); |
| 980 | rsc->setRootScript(s); |
| 981 | } |
| 982 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 983 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 984 | Sampler *s = static_cast<Sampler *>(vs); |
| 985 | |
| 986 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 987 | LOGE("Invalid sampler slot"); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | s->bindToContext(&rsc->mStateSampler, slot); |
| 992 | } |
| 993 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 994 | void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { |
Jason Sams | ccc010b | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 995 | ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 996 | rsc->setProgramStore(pfs); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 999 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1000 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 1001 | rsc->setProgramFragment(pf); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1004 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 1005 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 1006 | rsc->setProgramRaster(pr); |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1009 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1010 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 1011 | rsc->setProgramVertex(pv); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1014 | void rsi_ContextBindFont(Context *rsc, RsFont vfont) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1015 | Font *font = static_cast<Font *>(vfont); |
| 1016 | rsc->setFont(font); |
| 1017 | } |
| 1018 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1019 | void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 1020 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 1021 | rsc->assignName(ob, name, len); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 1022 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1023 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1024 | void rsi_ObjDestroy(Context *rsc, void *optr) { |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 1025 | ObjectBase *ob = static_cast<ObjectBase *>(optr); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 1026 | rsc->removeName(ob); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 1027 | ob->decUserRef(); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1030 | void rsi_ContextPause(Context *rsc) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 1031 | rsc->pause(); |
| 1032 | } |
| 1033 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1034 | void rsi_ContextResume(Context *rsc) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 1035 | rsc->resume(); |
| 1036 | } |
| 1037 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1038 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) { |
Mathias Agopian | fa40286 | 2010-02-12 14:04:35 -0800 | [diff] [blame] | 1039 | rsc->setSurface(w, h, sur); |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1042 | void rsi_ContextSetPriority(Context *rsc, int32_t p) { |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 1043 | rsc->setPriority(p); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1046 | void rsi_ContextDump(Context *rsc, int32_t bits) { |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 1047 | ObjectBase::dumpAll(rsc); |
| 1048 | } |
| 1049 | |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 1050 | void rsi_ContextDestroyWorker(Context *rsc) { |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 1051 | rsc->destroyWorkerThreadResources();; |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | void rsContextDestroy(RsContext vcon) { |
| 1058 | LOGV("rsContextDestroy %p", vcon); |
| 1059 | Context *rsc = static_cast<Context *>(vcon); |
| 1060 | rsContextDestroyWorker(rsc); |
Jason Sams | 1dcefab | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 1061 | delete rsc; |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 1062 | LOGV("rsContextDestroy 2 %p", vcon); |
Jason Sams | 1dcefab | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1065 | RsContext rsContextCreate(RsDevice vdev, uint32_t version) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1066 | LOGV("rsContextCreate %p", vdev); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1067 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 1068 | Context *rsc = Context::createContext(dev, NULL); |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1069 | return rsc; |
| 1070 | } |
| 1071 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1072 | RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc) { |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 1073 | LOGV("rsContextCreateGL %p", vdev); |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1074 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 1075 | Context *rsc = Context::createContext(dev, &sc); |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 1076 | LOGV("rsContextCreateGL ret %p ", rsc); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1077 | return rsc; |
| 1078 | } |
| 1079 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1080 | RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1081 | Context * rsc = static_cast<Context *>(vrsc); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 1082 | return rsc->peekMessageToClient(receiveLen, subID, wait); |
| 1083 | } |
| 1084 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1085 | RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) { |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 1086 | Context * rsc = static_cast<Context *>(vrsc); |
| 1087 | return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1090 | void rsContextInitToClient(RsContext vrsc) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1091 | Context * rsc = static_cast<Context *>(vrsc); |
| 1092 | rsc->initToClient(); |
| 1093 | } |
| 1094 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1095 | void rsContextDeinitToClient(RsContext vrsc) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1096 | Context * rsc = static_cast<Context *>(vrsc); |
| 1097 | rsc->deinitToClient(); |
| 1098 | } |
| 1099 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 1100 | // Only to be called at a3d load time, before object is visible to user |
| 1101 | // not thread safe |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1102 | void rsaGetName(RsContext con, void * obj, const char **name) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 1103 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 1104 | (*name) = ob->getName(); |
| 1105 | } |