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 | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 21 | #include <ui/PixelFormat.h> |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 22 | #include <ui/EGLUtils.h> |
Mathias Agopian | 5cbb940 | 2010-02-12 12:00:38 -0800 | [diff] [blame] | 23 | #include <ui/egl/android_natives.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/resource.h> |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 27 | #include <sched.h> |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 28 | |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
| 30 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 31 | #include <GLES/gl.h> |
| 32 | #include <GLES/glext.h> |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 33 | #include <GLES2/gl2.h> |
| 34 | #include <GLES2/gl2ext.h> |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 35 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 36 | #include <cutils/sched_policy.h> |
Jason Sams | f3470ed | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 37 | #include <sys/syscall.h> |
| 38 | #include <string.h> |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 39 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | using namespace android; |
| 41 | using namespace android::renderscript; |
| 42 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 43 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 44 | uint32_t Context::gThreadTLSKeyCount = 0; |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 45 | uint32_t Context::gGLContextCount = 0; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 46 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Stephen Hines | 1ac9da6 | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 47 | pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 | |
Jason Sams | 7136220 | 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 | 11c8af9 | 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 | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 111 | bool Context::initGLThread() { |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 112 | pthread_mutex_lock(&gInitMutex); |
| 113 | LOGV("initGLThread start %p", this); |
| 114 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 115 | mEGL.mNumConfigs = -1; |
| 116 | EGLint configAttribs[128]; |
| 117 | EGLint *configAttribsPtr = configAttribs; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 118 | EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 120 | memset(configAttribs, 0, sizeof(configAttribs)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 121 | |
Jason Sams | b13ada5 | 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 | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | |
Jason Sams | 11c8af9 | 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 | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 129 | |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 130 | if (mUserSurfaceConfig.depthMin > 0) { |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 131 | configAttribsPtr[0] = EGL_DEPTH_SIZE; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 132 | configAttribsPtr[1] = mUserSurfaceConfig.depthMin; |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 133 | configAttribsPtr += 2; |
| 134 | } |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 135 | |
Jason Sams | ebfb436 | 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 | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 142 | configAttribsPtr[0] = EGL_NONE; |
| 143 | rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint)))); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 144 | |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 145 | LOGV("%p initEGL start", this); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 146 | mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 147 | checkEglError("eglGetDisplay"); |
| 148 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 149 | eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 150 | checkEglError("eglInitialize"); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 151 | |
Jason Sams | 11c8af9 | 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 | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 159 | if (err) { |
Jason Sams | cfc0436 | 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 | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 161 | } |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 162 | if (props.mLogVisual) { |
| 163 | printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 164 | } |
Jason Sams | 11c8af9 | 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 | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 170 | checkEglError("eglCreateContext"); |
| 171 | if (mEGL.mContext == EGL_NO_CONTEXT) { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 172 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 173 | LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 174 | return false; |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 175 | } |
| 176 | gGLContextCount++; |
Jason Sams | 11c8af9 | 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 | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 184 | pthread_mutex_unlock(&gInitMutex); |
| 185 | deinitEGL(); |
| 186 | return false; |
Jason Sams | 11c8af9 | 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 | d5f0630 | 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 | 11c8af9 | 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 | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 204 | //LOGV("GL Version %s", mGL.mVersion); |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 205 | //LOGV("GL Vendor %s", mGL.mVendor); |
Jason Sams | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 206 | //LOGV("GL Renderer %s", mGL.mRenderer); |
Jason Sams | 11c8af9 | 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 | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 221 | pthread_mutex_unlock(&gInitMutex); |
| 222 | deinitEGL(); |
| 223 | return false; |
Jason Sams | 11c8af9 | 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 | 9333e64 | 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 | 581cc64 | 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 | 11c8af9 | 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 | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 243 | if (hasAniso) { |
Jason Sams | 11c8af9 | 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 | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 249 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 252 | void Context::deinitEGL() { |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 253 | LOGV("%p, deinitEGL", this); |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 254 | |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 255 | if (mEGL.mContext != EGL_NO_CONTEXT) { |
Mathias Agopian | 578b7a2 | 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 | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 261 | eglDestroyContext(mEGL.mDisplay, mEGL.mContext); |
| 262 | checkEglError("eglDestroyContext"); |
| 263 | } |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 264 | |
| 265 | gGLContextCount--; |
| 266 | if (!gGLContextCount) { |
| 267 | eglTerminate(mEGL.mDisplay); |
| 268 | } |
| 269 | } |
| 270 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 271 | Context::PushState::PushState(Context *con) { |
| 272 | mRsc = con; |
Jason Sams | 07078e3 | 2011-02-23 14:47:17 -0800 | [diff] [blame] | 273 | if (con->mIsGraphicsContext) { |
| 274 | mFragment.set(con->getProgramFragment()); |
| 275 | mVertex.set(con->getProgramVertex()); |
| 276 | mStore.set(con->getProgramStore()); |
| 277 | mRaster.set(con->getProgramRaster()); |
| 278 | mFont.set(con->getFont()); |
| 279 | } |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | Context::PushState::~PushState() { |
Jason Sams | 07078e3 | 2011-02-23 14:47:17 -0800 | [diff] [blame] | 283 | if (mRsc->mIsGraphicsContext) { |
| 284 | mRsc->setProgramFragment(mFragment.get()); |
| 285 | mRsc->setProgramVertex(mVertex.get()); |
| 286 | mRsc->setProgramStore(mStore.get()); |
| 287 | mRsc->setProgramRaster(mRaster.get()); |
| 288 | mRsc->setFont(mFont.get()); |
| 289 | } |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 292 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 293 | uint32_t Context::runScript(Script *s) { |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 294 | PushState(this); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 295 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 296 | uint32_t ret = s->run(this); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 297 | return ret; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 300 | void Context::checkError(const char *msg, bool isFatal) const { |
| 301 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 302 | GLenum err = glGetError(); |
| 303 | if (err != GL_NO_ERROR) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 304 | char buf[1024]; |
| 305 | snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg); |
| 306 | |
| 307 | if (isFatal) { |
| 308 | setError(RS_ERROR_FATAL_DRIVER, buf); |
| 309 | } else { |
| 310 | switch (err) { |
| 311 | case GL_OUT_OF_MEMORY: |
| 312 | setError(RS_ERROR_OUT_OF_MEMORY, buf); |
| 313 | break; |
| 314 | default: |
| 315 | setError(RS_ERROR_DRIVER, buf); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | LOGE("%p, %s", this, buf); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 323 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 324 | uint32_t Context::runRootScript() { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 325 | glViewport(0, 0, mWidth, mHeight); |
Jason Sams | 67c6844 | 2009-08-25 17:09:59 -0700 | [diff] [blame] | 326 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 327 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 328 | mStateFragmentStore.mLast.clear(); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 329 | uint32_t ret = runScript(mRootScript.get()); |
Jason Sams | c7412b3 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 330 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 331 | checkError("runRootScript"); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 332 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 335 | uint64_t Context::getTime() const { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 336 | struct timespec t; |
| 337 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 338 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
| 339 | } |
| 340 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 341 | void Context::timerReset() { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 342 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 343 | mTimers[ct] = 0; |
| 344 | } |
| 345 | } |
| 346 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 347 | void Context::timerInit() { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 348 | mTimeLast = getTime(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 349 | mTimeFrame = mTimeLast; |
| 350 | mTimeLastFrame = mTimeLast; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 351 | mTimerActive = RS_TIMER_INTERNAL; |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 352 | mAverageFPSFrameCount = 0; |
| 353 | mAverageFPSStartTime = mTimeLast; |
| 354 | mAverageFPS = 0; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 355 | timerReset(); |
| 356 | } |
| 357 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 358 | void Context::timerFrame() { |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 359 | mTimeLastFrame = mTimeFrame; |
| 360 | mTimeFrame = getTime(); |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 361 | // Update average fps |
| 362 | const uint64_t averageFramerateInterval = 1000 * 1000000; |
| 363 | mAverageFPSFrameCount ++; |
| 364 | uint64_t inverval = mTimeFrame - mAverageFPSStartTime; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 365 | if (inverval >= averageFramerateInterval) { |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 366 | inverval = inverval / 1000000; |
| 367 | mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval; |
| 368 | mAverageFPSFrameCount = 0; |
| 369 | mAverageFPSStartTime = mTimeFrame; |
| 370 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 373 | void Context::timerSet(Timers tm) { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 374 | uint64_t last = mTimeLast; |
| 375 | mTimeLast = getTime(); |
| 376 | mTimers[mTimerActive] += mTimeLast - last; |
| 377 | mTimerActive = tm; |
| 378 | } |
| 379 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 380 | void Context::timerPrint() { |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 381 | double total = 0; |
| 382 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 383 | total += mTimers[ct]; |
| 384 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 385 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 386 | mTimeMSLastFrame = frame / 1000000; |
| 387 | mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; |
| 388 | mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 389 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 390 | |
| 391 | if (props.mLogTimes) { |
Alex Sakhartchouk | 98bfe5d | 2010-10-18 17:18:50 -0700 | [diff] [blame] | 392 | 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 | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 393 | mTimeMSLastFrame, |
| 394 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, |
| 395 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, |
| 396 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 397 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000, |
| 398 | mAverageFPS); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 399 | } |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 402 | bool Context::setupCheck() { |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 403 | if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) { |
| 404 | LOGE("Context::setupCheck() 1 fail"); |
| 405 | return false; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 406 | } |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 407 | |
| 408 | mFragmentStore->setupGL2(this, &mStateFragmentStore); |
| 409 | mFragment->setupGL2(this, &mStateFragment, &mShaderCache); |
| 410 | mRaster->setupGL2(this, &mStateRaster); |
| 411 | mVertex->setupGL2(this, &mStateVertex, &mShaderCache); |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame^] | 412 | mFBOCache.setupGL2(this); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 413 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 416 | void Context::setupProgramStore() { |
| 417 | mFragmentStore->setupGL2(this, &mStateFragmentStore); |
| 418 | } |
| 419 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 420 | static bool getProp(const char *str) { |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 421 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 422 | property_get(str, buf, "0"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 423 | return 0 != strcmp(buf, "0"); |
| 424 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 425 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 426 | void Context::displayDebugStats() { |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 427 | char buffer[128]; |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 428 | sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript); |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 429 | float oldR, oldG, oldB, oldA; |
| 430 | mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 431 | uint32_t bufferLen = strlen(buffer); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 432 | |
Alex Sakhartchouk | 20a9354 | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 433 | ObjectBaseRef<Font> lastFont(getFont()); |
| 434 | setFont(NULL); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 435 | float shadowCol = 0.1f; |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 436 | mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 437 | mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 438 | |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 439 | mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f); |
Alex Sakhartchouk | 76322af | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 440 | mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 441 | |
Alex Sakhartchouk | 20a9354 | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 442 | setFont(lastFont.get()); |
Alex Sakhartchouk | 55e8198 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 443 | mStateFont.setFontColor(oldR, oldG, oldB, oldA); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 446 | void * Context::threadProc(void *vrsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 447 | Context *rsc = static_cast<Context *>(vrsc); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 448 | rsc->mNativeThreadId = gettid(); |
| 449 | |
| 450 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 451 | rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 452 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 453 | rsc->props.mLogTimes = getProp("debug.rs.profile"); |
| 454 | rsc->props.mLogScripts = getProp("debug.rs.script"); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 455 | rsc->props.mLogObjects = getProp("debug.rs.object"); |
| 456 | rsc->props.mLogShaders = getProp("debug.rs.shader"); |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 457 | rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes"); |
| 458 | rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms"); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 459 | rsc->props.mLogVisual = getProp("debug.rs.visual"); |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 460 | |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 461 | rsc->mTlsStruct = new ScriptTLSStruct; |
| 462 | if (!rsc->mTlsStruct) { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 463 | LOGE("Error allocating tls storage"); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 464 | rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS"); |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 467 | rsc->mTlsStruct->mContext = rsc; |
| 468 | rsc->mTlsStruct->mScript = NULL; |
| 469 | int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct); |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 470 | if (status) { |
| 471 | LOGE("pthread_setspecific %i", status); |
| 472 | } |
| 473 | |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 474 | if (!rsc->initGLThread()) { |
| 475 | rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL"); |
| 476 | return NULL; |
| 477 | } |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 478 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 479 | if (rsc->mIsGraphicsContext) { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 480 | rsc->mStateRaster.init(rsc); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 481 | rsc->setProgramRaster(NULL); |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 482 | rsc->mStateVertex.init(rsc); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 483 | rsc->setProgramVertex(NULL); |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 484 | rsc->mStateFragment.init(rsc); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 485 | rsc->setProgramFragment(NULL); |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 486 | rsc->mStateFragmentStore.init(rsc); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 487 | rsc->setProgramStore(NULL); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 488 | rsc->mStateFont.init(rsc); |
| 489 | rsc->setFont(NULL); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 490 | rsc->mStateVertexArray.init(rsc); |
| 491 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 492 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 493 | rsc->mRunning = true; |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 494 | bool mDraw = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 495 | while (!rsc->mExit) { |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 496 | mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw); |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 497 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 498 | mDraw &= (rsc->mWndSurface != NULL); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 499 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 500 | uint32_t targetTime = 0; |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 501 | if (mDraw && rsc->mIsGraphicsContext) { |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 502 | targetTime = rsc->runRootScript(); |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 503 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 504 | if (rsc->props.mLogVisual) { |
Alex Sakhartchouk | 6de5550 | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 505 | rsc->displayDebugStats(); |
| 506 | } |
| 507 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 508 | mDraw = targetTime && !rsc->mPaused; |
| 509 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 510 | eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface); |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 511 | rsc->timerFrame(); |
| 512 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 513 | rsc->timerPrint(); |
| 514 | rsc->timerReset(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 515 | } |
Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 516 | if (targetTime > 1) { |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 517 | int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000; |
| 518 | if (t > 0) { |
| 519 | usleep(t); |
| 520 | } |
| 521 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 524 | LOGV("%p, RS Thread exiting", rsc); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 525 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 526 | if (rsc->mIsGraphicsContext) { |
| 527 | pthread_mutex_lock(&gInitMutex); |
| 528 | rsc->deinitEGL(); |
| 529 | pthread_mutex_unlock(&gInitMutex); |
| 530 | } |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 531 | delete rsc->mTlsStruct; |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 532 | |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 533 | LOGV("%p, RS Thread exited", rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
| 536 | |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 537 | void Context::destroyWorkerThreadResources() { |
Jason Sams | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 538 | //LOGV("destroyWorkerThreadResources 1"); |
Jason Sams | 38f8d9d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 539 | ObjectBase::zeroAllUserRef(this); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 540 | if (mIsGraphicsContext) { |
| 541 | mRaster.clear(); |
| 542 | mFragment.clear(); |
| 543 | mVertex.clear(); |
| 544 | mFragmentStore.clear(); |
| 545 | mFont.clear(); |
| 546 | mRootScript.clear(); |
| 547 | mStateRaster.deinit(this); |
| 548 | mStateVertex.deinit(this); |
| 549 | mStateFragment.deinit(this); |
| 550 | mStateFragmentStore.deinit(this); |
| 551 | mStateFont.deinit(this); |
Jason Sams | 49945c0 | 2011-01-05 03:37:48 -0800 | [diff] [blame] | 552 | mShaderCache.cleanupAll(); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 553 | } |
Jason Sams | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 554 | //LOGV("destroyWorkerThreadResources 2"); |
Jason Sams | 84035ff | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 555 | mExit = true; |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 558 | void Context::setPriority(int32_t p) { |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 559 | // Note: If we put this in the proper "background" policy |
| 560 | // the wallpapers can become completly unresponsive at times. |
| 561 | // This is probably not what we want for something the user is actively |
| 562 | // looking at. |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 563 | mThreadPriority = p; |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 564 | #if 0 |
| 565 | SchedPolicy pol = SP_FOREGROUND; |
| 566 | if (p > 0) { |
| 567 | pol = SP_BACKGROUND; |
| 568 | } |
| 569 | if (!set_sched_policy(mNativeThreadId, pol)) { |
| 570 | // success; reset the priority as well |
| 571 | } |
| 572 | #else |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 573 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 574 | #endif |
| 575 | } |
| 576 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 577 | Context::Context() { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 578 | mDev = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 579 | mRunning = false; |
| 580 | mExit = false; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 581 | mPaused = false; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 582 | mObjHead = NULL; |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 583 | mError = RS_ERROR_NONE; |
Alex Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 584 | mDPI = 96; |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 587 | Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 588 | Context * rsc = new Context(); |
| 589 | if (!rsc->initContext(dev, sc)) { |
| 590 | delete rsc; |
| 591 | return NULL; |
| 592 | } |
| 593 | return rsc; |
| 594 | } |
| 595 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 596 | bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 597 | pthread_mutex_lock(&gInitMutex); |
| 598 | |
| 599 | dev->addContext(this); |
| 600 | mDev = dev; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 601 | if (sc) { |
| 602 | mUserSurfaceConfig = *sc; |
| 603 | } else { |
| 604 | memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); |
| 605 | } |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 606 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 607 | memset(&mEGL, 0, sizeof(mEGL)); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 608 | memset(&mGL, 0, sizeof(mGL)); |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 609 | mIsGraphicsContext = sc != NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 610 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 611 | int status; |
| 612 | pthread_attr_t threadAttr; |
| 613 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 614 | if (!gThreadTLSKeyCount) { |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 615 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 616 | if (status) { |
| 617 | LOGE("Failed to init thread tls key."); |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 618 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 619 | return false; |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 620 | } |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 621 | } |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 622 | gThreadTLSKeyCount++; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 623 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 624 | pthread_mutex_unlock(&gInitMutex); |
| 625 | |
| 626 | // Global init done at this point. |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 627 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 628 | status = pthread_attr_init(&threadAttr); |
| 629 | if (status) { |
| 630 | LOGE("Failed to init thread attribute."); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 631 | return false; |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 634 | mWndSurface = NULL; |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 635 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 636 | timerInit(); |
Jason Sams | d3f2eaf | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 637 | timerSet(RS_TIMER_INTERNAL); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 638 | |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 639 | if (!rsdHalInit(this, 0, 0)) { |
Jason Sams | 80e29cf | 2011-03-18 17:08:54 -0700 | [diff] [blame] | 640 | LOGE("Hal init failed"); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 641 | return false; |
| 642 | } |
Jason Sams | 55d2a25 | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 643 | mHal.funcs.setPriority(this, mThreadPriority); |
Jason Sams | e4a06c5 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 644 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 645 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 646 | if (status) { |
| 647 | LOGE("Failed to start rs context thread."); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 648 | return false; |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 649 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 650 | while (!mRunning && (mError == RS_ERROR_NONE)) { |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 651 | usleep(100); |
| 652 | } |
| 653 | |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 654 | if (mError != RS_ERROR_NONE) { |
Jason Sams | 80e29cf | 2011-03-18 17:08:54 -0700 | [diff] [blame] | 655 | LOGE("Errors during thread init"); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 656 | return false; |
| 657 | } |
| 658 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 659 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 660 | return true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 663 | Context::~Context() { |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 664 | LOGV("Context::~Context"); |
Jason Sams | 84035ff | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 665 | |
| 666 | mIO.mToCore.flush(); |
| 667 | rsAssert(mExit); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 668 | mExit = true; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 669 | mPaused = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 670 | void *res; |
| 671 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 672 | mIO.shutdown(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 673 | int status = pthread_join(mThreadId, &res); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 674 | |
Jason Sams | 55d2a25 | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 675 | |
| 676 | if (mHal.funcs.shutdownDriver) { |
| 677 | mHal.funcs.shutdownDriver(this); |
Jason Sams | 03855bb | 2011-01-25 00:26:25 -0800 | [diff] [blame] | 678 | } |
Jason Sams | c55de66 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 679 | |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 680 | // Global structure cleanup. |
| 681 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 682 | if (mDev) { |
| 683 | mDev->removeContext(this); |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 684 | --gThreadTLSKeyCount; |
| 685 | if (!gThreadTLSKeyCount) { |
| 686 | pthread_key_delete(gThreadTLSKey); |
| 687 | } |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 688 | mDev = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 689 | } |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 690 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 691 | LOGV("Context::~Context done"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 694 | void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 695 | rsAssert(mIsGraphicsContext); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 696 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 697 | EGLBoolean ret; |
Jason Sams | ec30933 | 2011-01-12 15:26:25 -0800 | [diff] [blame] | 698 | // WAR: Some drivers fail to handle 0 size surfaces correcntly. |
| 699 | // Use the pbuffer to avoid this pitfall. |
| 700 | if ((mEGL.mSurface != NULL) || (w == 0) || (h == 0)) { |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 701 | ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 702 | checkEglError("eglMakeCurrent", ret); |
| 703 | |
| 704 | ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface); |
| 705 | checkEglError("eglDestroySurface", ret); |
| 706 | |
| 707 | mEGL.mSurface = NULL; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 708 | mWidth = 1; |
| 709 | mHeight = 1; |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | mWndSurface = sur; |
| 713 | if (mWndSurface != NULL) { |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 714 | mWidth = w; |
| 715 | mHeight = h; |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 716 | |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 717 | mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL); |
| 718 | checkEglError("eglCreateWindowSurface"); |
| 719 | if (mEGL.mSurface == EGL_NO_SURFACE) { |
| 720 | LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE"); |
| 721 | } |
| 722 | |
| 723 | ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext); |
| 724 | checkEglError("eglMakeCurrent", ret); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 725 | |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 726 | mStateVertex.updateSize(this); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 730 | void Context::pause() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 731 | rsAssert(mIsGraphicsContext); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 732 | mPaused = true; |
| 733 | } |
| 734 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 735 | void Context::resume() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 736 | rsAssert(mIsGraphicsContext); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 737 | mPaused = false; |
| 738 | } |
| 739 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 740 | void Context::setRootScript(Script *s) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 741 | rsAssert(mIsGraphicsContext); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 742 | mRootScript.set(s); |
| 743 | } |
| 744 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 745 | void Context::setProgramStore(ProgramStore *pfs) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 746 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 747 | if (pfs == NULL) { |
| 748 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 749 | } else { |
| 750 | mFragmentStore.set(pfs); |
| 751 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 754 | void Context::setProgramFragment(ProgramFragment *pf) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 755 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 756 | if (pf == NULL) { |
| 757 | mFragment.set(mStateFragment.mDefault); |
| 758 | } else { |
| 759 | mFragment.set(pf); |
| 760 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 763 | void Context::setProgramRaster(ProgramRaster *pr) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 764 | rsAssert(mIsGraphicsContext); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 765 | if (pr == NULL) { |
| 766 | mRaster.set(mStateRaster.mDefault); |
| 767 | } else { |
| 768 | mRaster.set(pr); |
| 769 | } |
| 770 | } |
| 771 | |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 772 | void Context::setProgramVertex(ProgramVertex *pv) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 773 | rsAssert(mIsGraphicsContext); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 774 | if (pv == NULL) { |
| 775 | mVertex.set(mStateVertex.mDefault); |
| 776 | } else { |
| 777 | mVertex.set(pv); |
| 778 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 781 | void Context::setFont(Font *f) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 782 | rsAssert(mIsGraphicsContext); |
| 783 | if (f == NULL) { |
| 784 | mFont.set(mStateFont.mDefault); |
| 785 | } else { |
| 786 | mFont.set(f); |
| 787 | } |
| 788 | } |
| 789 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 790 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 791 | rsAssert(!obj->getName()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 792 | obj->setName(name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 793 | mNames.add(obj); |
| 794 | } |
| 795 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 796 | void Context::removeName(ObjectBase *obj) { |
| 797 | for (size_t ct=0; ct < mNames.size(); ct++) { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 798 | if (obj == mNames[ct]) { |
| 799 | mNames.removeAt(ct); |
| 800 | return; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 805 | RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) { |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 806 | *receiveLen = 0; |
| 807 | if (!wait && mIO.mToClient.isEmpty()) { |
| 808 | return RS_MESSAGE_TO_CLIENT_NONE; |
| 809 | } |
| 810 | |
| 811 | uint32_t bytesData = 0; |
| 812 | uint32_t commandID = 0; |
| 813 | const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData); |
| 814 | *receiveLen = bytesData - sizeof(uint32_t); |
| 815 | if (bytesData) { |
| 816 | *subID = d[0]; |
| 817 | } |
| 818 | return (RsMessageToClientType)commandID; |
| 819 | } |
| 820 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 821 | RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 822 | //LOGE("getMessageToClient %i %i", bufferLen, wait); |
Jason Sams | 1d45c47 | 2010-08-25 14:31:48 -0700 | [diff] [blame] | 823 | *receiveLen = 0; |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 824 | if (!wait && mIO.mToClient.isEmpty()) { |
| 825 | return RS_MESSAGE_TO_CLIENT_NONE; |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | //LOGE("getMessageToClient 2 con=%p", this); |
| 829 | uint32_t bytesData = 0; |
| 830 | uint32_t commandID = 0; |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 831 | const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 832 | //LOGE("getMessageToClient 3 %i %i", commandID, bytesData); |
| 833 | |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 834 | *receiveLen = bytesData - sizeof(uint32_t); |
| 835 | *subID = d[0]; |
| 836 | |
| 837 | //LOGE("getMessageToClient %i %i", commandID, *subID); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 838 | if (bufferLen >= bytesData) { |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 839 | memcpy(data, d+1, *receiveLen); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 840 | mIO.mToClient.next(); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 841 | return (RsMessageToClientType)commandID; |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 842 | } |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 843 | return RS_MESSAGE_TO_CLIENT_RESIZE; |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 846 | bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, |
| 847 | uint32_t subID, size_t len, bool waitForSpace) const { |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 848 | //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 849 | if (cmdID == 0) { |
| 850 | LOGE("Attempting to send invalid command 0 to client."); |
| 851 | return false; |
| 852 | } |
| 853 | if (!waitForSpace) { |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 854 | if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 855 | // Not enough room, and not waiting. |
| 856 | return false; |
| 857 | } |
| 858 | } |
| 859 | //LOGE("sendMessageToClient 2"); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 860 | uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID)); |
| 861 | p[0] = subID; |
Jason Sams | 1796651 | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 862 | if (len > 0) { |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 863 | memcpy(p+1, data, len); |
Jason Sams | 1796651 | 2010-07-28 11:17:53 -0700 | [diff] [blame] | 864 | } |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 865 | mIO.mToClient.commit(cmdID, len + sizeof(subID)); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 866 | //LOGE("sendMessageToClient 3"); |
| 867 | return true; |
| 868 | } |
| 869 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 870 | void Context::initToClient() { |
| 871 | while (!mRunning) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 872 | usleep(100); |
| 873 | } |
| 874 | } |
| 875 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 876 | void Context::deinitToClient() { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 877 | mIO.mToClient.shutdown(); |
| 878 | } |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 879 | |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 880 | void Context::setError(RsError e, const char *msg) const { |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 881 | mError = e; |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 882 | sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 886 | void Context::dumpDebug() const { |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 887 | LOGE("RS Context debug %p", this); |
| 888 | LOGE("RS Context debug"); |
| 889 | |
| 890 | LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 891 | 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] | 892 | LOGE(" GL vendor: %s", mGL.mVendor); |
| 893 | LOGE(" GL renderer: %s", mGL.mRenderer); |
| 894 | LOGE(" GL Version: %s", mGL.mVersion); |
| 895 | LOGE(" GL Extensions: %s", mGL.mExtensions); |
| 896 | LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion); |
| 897 | LOGE(" RS width %i, height %i", mWidth, mHeight); |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 898 | LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 899 | LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId); |
| 900 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 901 | LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits); |
| 902 | LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs); |
| 903 | LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors); |
| 904 | LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 905 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 906 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 907 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 908 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 909 | |
| 910 | namespace android { |
| 911 | namespace renderscript { |
| 912 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 913 | void rsi_ContextFinish(Context *rsc) { |
Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 914 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 915 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 916 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 917 | Script *s = static_cast<Script *>(vs); |
| 918 | rsc->setRootScript(s); |
| 919 | } |
| 920 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 921 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 922 | Sampler *s = static_cast<Sampler *>(vs); |
| 923 | |
| 924 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 925 | LOGE("Invalid sampler slot"); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | s->bindToContext(&rsc->mStateSampler, slot); |
| 930 | } |
| 931 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 932 | void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 933 | ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 934 | rsc->setProgramStore(pfs); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 937 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 938 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 939 | rsc->setProgramFragment(pf); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 942 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 943 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 944 | rsc->setProgramRaster(pr); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 947 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 948 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
Jason Sams | a17af04 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 949 | rsc->setProgramVertex(pv); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 952 | void rsi_ContextBindFont(Context *rsc, RsFont vfont) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 953 | Font *font = static_cast<Font *>(vfont); |
| 954 | rsc->setFont(font); |
| 955 | } |
| 956 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 957 | 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] | 958 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 959 | rsc->assignName(ob, name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 960 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 961 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 962 | void rsi_ObjDestroy(Context *rsc, void *optr) { |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 963 | ObjectBase *ob = static_cast<ObjectBase *>(optr); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 964 | rsc->removeName(ob); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 965 | ob->decUserRef(); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 968 | void rsi_ContextPause(Context *rsc) { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 969 | rsc->pause(); |
| 970 | } |
| 971 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 972 | void rsi_ContextResume(Context *rsc) { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 973 | rsc->resume(); |
| 974 | } |
| 975 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 976 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) { |
Mathias Agopian | 128ce4b | 2010-02-12 14:04:35 -0800 | [diff] [blame] | 977 | rsc->setSurface(w, h, sur); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 980 | void rsi_ContextSetPriority(Context *rsc, int32_t p) { |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 981 | rsc->setPriority(p); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 984 | void rsi_ContextDump(Context *rsc, int32_t bits) { |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 985 | ObjectBase::dumpAll(rsc); |
| 986 | } |
| 987 | |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 988 | void rsi_ContextDestroyWorker(Context *rsc) { |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 989 | rsc->destroyWorkerThreadResources();; |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | void rsContextDestroy(RsContext vcon) { |
| 996 | LOGV("rsContextDestroy %p", vcon); |
| 997 | Context *rsc = static_cast<Context *>(vcon); |
| 998 | rsContextDestroyWorker(rsc); |
Jason Sams | 546f01b | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 999 | delete rsc; |
Jason Sams | 5c68a71 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 1000 | LOGV("rsContextDestroy 2 %p", vcon); |
Jason Sams | 546f01b | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1003 | RsContext rsContextCreate(RsDevice vdev, uint32_t version) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1004 | LOGV("rsContextCreate %p", vdev); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1005 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 1006 | Context *rsc = Context::createContext(dev, NULL); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1007 | return rsc; |
| 1008 | } |
| 1009 | |
Alex Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 1010 | RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, |
| 1011 | RsSurfaceConfig sc, uint32_t dpi) { |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 1012 | LOGV("rsContextCreateGL %p", vdev); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1013 | Device * dev = static_cast<Device *>(vdev); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 1014 | Context *rsc = Context::createContext(dev, &sc); |
Alex Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 1015 | rsc->setDPI(dpi); |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 1016 | LOGV("rsContextCreateGL ret %p ", rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1017 | return rsc; |
| 1018 | } |
| 1019 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1020 | RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1021 | Context * rsc = static_cast<Context *>(vrsc); |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 1022 | return rsc->peekMessageToClient(receiveLen, subID, wait); |
| 1023 | } |
| 1024 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1025 | RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) { |
Jason Sams | 1c41517 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 1026 | Context * rsc = static_cast<Context *>(vrsc); |
| 1027 | return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1030 | void rsContextInitToClient(RsContext vrsc) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1031 | Context * rsc = static_cast<Context *>(vrsc); |
| 1032 | rsc->initToClient(); |
| 1033 | } |
| 1034 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1035 | void rsContextDeinitToClient(RsContext vrsc) { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 1036 | Context * rsc = static_cast<Context *>(vrsc); |
| 1037 | rsc->deinitToClient(); |
| 1038 | } |
| 1039 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 1040 | // Only to be called at a3d load time, before object is visible to user |
| 1041 | // not thread safe |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 1042 | void rsaGetName(RsContext con, void * obj, const char **name) { |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 1043 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 1044 | (*name) = ob->getName(); |
| 1045 | } |