blob: 35db33229c3c5de2cfe1686c6dc78430802d4edf [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Sams6b8552a2010-10-13 15:31:10 -070021#include <ui/PixelFormat.h>
Jason Samsafcb25c2009-08-25 11:34:49 -070022#include <ui/EGLUtils.h>
Mathias Agopian9b97c292010-02-12 12:00:38 -080023#include <ui/egl/android_natives.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Jason Sams15832442009-11-15 12:14:26 -080025#include <sys/types.h>
26#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070027#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080028
Joe Onorato76371ff2009-09-23 16:37:36 -070029#include <cutils/properties.h>
30
Jason Sams1aa5a4e2009-06-22 17:15:15 -070031#include <GLES/gl.h>
32#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080033#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070035
Jason Sams15832442009-11-15 12:14:26 -080036#include <cutils/sched_policy.h>
Jason Sams8d957fa2010-09-28 14:41:22 -070037#include <sys/syscall.h>
38#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080039
Jason Sams326e0dd2009-05-22 14:03:28 -070040using namespace android;
41using namespace android::renderscript;
42
Jason Samse5769102009-06-19 16:03:18 -070043pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070044uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070045uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070046pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070047
Jason Sams33b6e3b2009-10-27 14:44:31 -070048static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
49 if (returnVal != EGL_TRUE) {
50 fprintf(stderr, "%s() returned %d\n", op, returnVal);
51 }
52
53 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
54 = eglGetError()) {
55 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
56 error);
57 }
58}
59
Jason Sams6b8552a2010-10-13 15:31:10 -070060void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
61
62#define X(VAL) {VAL, #VAL}
63 struct {EGLint attribute; const char* name;} names[] = {
64 X(EGL_BUFFER_SIZE),
65 X(EGL_ALPHA_SIZE),
66 X(EGL_BLUE_SIZE),
67 X(EGL_GREEN_SIZE),
68 X(EGL_RED_SIZE),
69 X(EGL_DEPTH_SIZE),
70 X(EGL_STENCIL_SIZE),
71 X(EGL_CONFIG_CAVEAT),
72 X(EGL_CONFIG_ID),
73 X(EGL_LEVEL),
74 X(EGL_MAX_PBUFFER_HEIGHT),
75 X(EGL_MAX_PBUFFER_PIXELS),
76 X(EGL_MAX_PBUFFER_WIDTH),
77 X(EGL_NATIVE_RENDERABLE),
78 X(EGL_NATIVE_VISUAL_ID),
79 X(EGL_NATIVE_VISUAL_TYPE),
80 X(EGL_SAMPLES),
81 X(EGL_SAMPLE_BUFFERS),
82 X(EGL_SURFACE_TYPE),
83 X(EGL_TRANSPARENT_TYPE),
84 X(EGL_TRANSPARENT_RED_VALUE),
85 X(EGL_TRANSPARENT_GREEN_VALUE),
86 X(EGL_TRANSPARENT_BLUE_VALUE),
87 X(EGL_BIND_TO_TEXTURE_RGB),
88 X(EGL_BIND_TO_TEXTURE_RGBA),
89 X(EGL_MIN_SWAP_INTERVAL),
90 X(EGL_MAX_SWAP_INTERVAL),
91 X(EGL_LUMINANCE_SIZE),
92 X(EGL_ALPHA_MASK_SIZE),
93 X(EGL_COLOR_BUFFER_TYPE),
94 X(EGL_RENDERABLE_TYPE),
95 X(EGL_CONFORMANT),
96 };
97#undef X
98
99 for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
100 EGLint value = -1;
101 EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
102 EGLint error = eglGetError();
103 if (returnVal && error == EGL_SUCCESS) {
104 LOGV(" %s: %d (0x%x)", names[j].name, value, value);
105 }
106 }
107}
108
109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -0700111 pthread_mutex_lock(&gInitMutex);
112 LOGV("initGLThread start %p", this);
113
Jason Samsafcb25c2009-08-25 11:34:49 -0700114 mEGL.mNumConfigs = -1;
115 EGLint configAttribs[128];
116 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -0800117 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -0700118
Jason Samsafcb25c2009-08-25 11:34:49 -0700119 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -0700120
Jason Samsafcb25c2009-08-25 11:34:49 -0700121 configAttribsPtr[0] = EGL_SURFACE_TYPE;
122 configAttribsPtr[1] = EGL_WINDOW_BIT;
123 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -0700124
Jason Sams6b8552a2010-10-13 15:31:10 -0700125 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
126 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
127 configAttribsPtr += 2;
Jason Samsc460e552009-11-25 13:22:07 -0800128
Jason Sams6b8552a2010-10-13 15:31:10 -0700129 if (mUserSurfaceConfig.depthMin > 0) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700130 configAttribsPtr[0] = EGL_DEPTH_SIZE;
Jason Sams6b8552a2010-10-13 15:31:10 -0700131 configAttribsPtr[1] = mUserSurfaceConfig.depthMin;
Jason Samsafcb25c2009-08-25 11:34:49 -0700132 configAttribsPtr += 2;
133 }
Jason Sams9397e302009-08-27 20:23:34 -0700134
Jason Sams5fd09d82009-09-23 13:57:02 -0700135 if (mDev->mForceSW) {
136 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
137 configAttribsPtr[1] = EGL_SLOW_CONFIG;
138 configAttribsPtr += 2;
139 }
140
Jason Samsafcb25c2009-08-25 11:34:49 -0700141 configAttribsPtr[0] = EGL_NONE;
142 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -0700143
Jason Sams4c5f99e2010-09-14 14:59:03 -0700144 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700145 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700146 checkEglError("eglGetDisplay");
147
Jason Samsafcb25c2009-08-25 11:34:49 -0700148 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700149 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -0700150
Jason Sams6b8552a2010-10-13 15:31:10 -0700151#if 1
152 PixelFormat pf = PIXEL_FORMAT_RGBA_8888;
153 if (mUserSurfaceConfig.alphaMin == 0) {
154 pf = PIXEL_FORMAT_RGBX_8888;
155 }
156
157 status_t err = EGLUtils::selectConfigForPixelFormat(mEGL.mDisplay, configAttribs, pf, &mEGL.mConfig);
Jason Samsafcb25c2009-08-25 11:34:49 -0700158 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700159 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700160 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700161 if (props.mLogVisual) {
162 printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig);
Jason Samsc460e552009-11-25 13:22:07 -0800163 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700164#else
165 eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
166#endif
167
168 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700169 checkEglError("eglCreateContext");
170 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700171 pthread_mutex_unlock(&gInitMutex);
Jason Sams4c5f99e2010-09-14 14:59:03 -0700172 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700173 return false;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700174 }
175 gGLContextCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700176
177
178 EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
179 mEGL.mSurfaceDefault = eglCreatePbufferSurface(mEGL.mDisplay, mEGL.mConfig, pbuffer_attribs);
180 checkEglError("eglCreatePbufferSurface");
181 if (mEGL.mSurfaceDefault == EGL_NO_SURFACE) {
182 LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700183 pthread_mutex_unlock(&gInitMutex);
184 deinitEGL();
185 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700186 }
187
188 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700189 if (ret == EGL_FALSE) {
190 LOGE("eglMakeCurrent returned EGL_FALSE");
191 checkEglError("eglMakeCurrent", ret);
192 pthread_mutex_unlock(&gInitMutex);
193 deinitEGL();
194 return false;
195 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700196
197 mGL.mVersion = glGetString(GL_VERSION);
198 mGL.mVendor = glGetString(GL_VENDOR);
199 mGL.mRenderer = glGetString(GL_RENDERER);
200 mGL.mExtensions = glGetString(GL_EXTENSIONS);
201
202 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
203 LOGV("GL Version %s", mGL.mVersion);
204 //LOGV("GL Vendor %s", mGL.mVendor);
205 LOGV("GL Renderer %s", mGL.mRenderer);
206 //LOGV("GL Extensions %s", mGL.mExtensions);
207
208 const char *verptr = NULL;
209 if (strlen((const char *)mGL.mVersion) > 9) {
210 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
211 verptr = (const char *)mGL.mVersion + 12;
212 }
213 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
214 verptr = (const char *)mGL.mVersion + 9;
215 }
216 }
217
218 if (!verptr) {
219 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700220 pthread_mutex_unlock(&gInitMutex);
221 deinitEGL();
222 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700223 } else {
224 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
225 }
226
227 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
228 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
229 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
230
231 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
232 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
233
234 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
235 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
236
237 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700238 mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)mGL.mExtensions, "GL_NV_texture_npot_2D_mipmap");
Jason Sams6b8552a2010-10-13 15:31:10 -0700239 mGL.EXT_texture_max_aniso = 1.0f;
240 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800241 if (hasAniso) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700242 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
243 }
244
245 LOGV("initGLThread end %p", this);
246 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700247 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700248}
249
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800250void Context::deinitEGL() {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700251 LOGV("%p, deinitEGL", this);
Jason Sams6b8552a2010-10-13 15:31:10 -0700252
Jason Sams5c1c79a2010-11-03 14:27:11 -0700253 if (mEGL.mContext != EGL_NO_CONTEXT) {
254 eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEGL.mContext);
255 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
256 checkEglError("eglDestroyContext");
257 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700258
259 gGLContextCount--;
260 if (!gGLContextCount) {
261 eglTerminate(mEGL.mDisplay);
262 }
263}
264
Jason Sams60709252010-11-17 15:29:32 -0800265Context::PushState::PushState(Context *con) {
266 mRsc = con;
267 mFragment.set(con->getProgramFragment());
268 mVertex.set(con->getProgramVertex());
269 mStore.set(con->getProgramStore());
270 mRaster.set(con->getProgramRaster());
Alex Sakhartchouk5c0912b2010-11-18 16:36:47 -0800271 mFont.set(con->getFont());
Jason Sams60709252010-11-17 15:29:32 -0800272}
273
274Context::PushState::~PushState() {
275 mRsc->setProgramFragment(mFragment.get());
276 mRsc->setProgramVertex(mVertex.get());
277 mRsc->setProgramStore(mStore.get());
278 mRsc->setProgramRaster(mRaster.get());
279 mRsc->setFont(mFont.get());
280}
281
Jason Sams33b6e3b2009-10-27 14:44:31 -0700282
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800283uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -0800284 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -0700285
Jason Samsc61346b2010-05-28 18:23:22 -0700286 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700287 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700288}
289
Jason Sams87319de2010-11-22 16:20:16 -0800290void Context::checkError(const char *msg, bool isFatal) const {
291
Jason Samsd01d9702009-12-23 14:35:29 -0800292 GLenum err = glGetError();
293 if (err != GL_NO_ERROR) {
Jason Sams87319de2010-11-22 16:20:16 -0800294 char buf[1024];
295 snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
296
297 if (isFatal) {
298 setError(RS_ERROR_FATAL_DRIVER, buf);
299 } else {
300 switch (err) {
301 case GL_OUT_OF_MEMORY:
302 setError(RS_ERROR_OUT_OF_MEMORY, buf);
303 break;
304 default:
305 setError(RS_ERROR_DRIVER, buf);
306 break;
307 }
308 }
309
310 LOGE("%p, %s", this, buf);
Jason Samsd01d9702009-12-23 14:35:29 -0800311 }
312}
Jason Sams10308932009-06-09 12:15:30 -0700313
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800314uint32_t Context::runRootScript() {
Jason Sams771565f2010-05-14 15:30:29 -0700315 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700316
Jason Sams2dca84d2009-12-09 11:05:45 -0800317 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700318 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700319 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700320
Jason Samsd01d9702009-12-23 14:35:29 -0800321 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700322 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700323}
324
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800325uint64_t Context::getTime() const {
Jason Sams24371d92009-08-19 12:17:14 -0700326 struct timespec t;
327 clock_gettime(CLOCK_MONOTONIC, &t);
328 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
329}
330
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800331void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700332 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
333 mTimers[ct] = 0;
334 }
335}
336
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800337void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700338 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700339 mTimeFrame = mTimeLast;
340 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700341 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700342 mAverageFPSFrameCount = 0;
343 mAverageFPSStartTime = mTimeLast;
344 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700345 timerReset();
346}
347
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800348void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700349 mTimeLastFrame = mTimeFrame;
350 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700351 // Update average fps
352 const uint64_t averageFramerateInterval = 1000 * 1000000;
353 mAverageFPSFrameCount ++;
354 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800355 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700356 inverval = inverval / 1000000;
357 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
358 mAverageFPSFrameCount = 0;
359 mAverageFPSStartTime = mTimeFrame;
360 }
Jason Sams1d54f102009-09-03 15:43:13 -0700361}
362
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800363void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700364 uint64_t last = mTimeLast;
365 mTimeLast = getTime();
366 mTimers[mTimerActive] += mTimeLast - last;
367 mTimerActive = tm;
368}
369
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800370void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700371 double total = 0;
372 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
373 total += mTimers[ct];
374 }
Jason Sams1d54f102009-09-03 15:43:13 -0700375 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800376 mTimeMSLastFrame = frame / 1000000;
377 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
378 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700379
Jason Sams2dca84d2009-12-09 11:05:45 -0800380
381 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700382 LOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800383 mTimeMSLastFrame,
384 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
385 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
386 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700387 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
388 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800389 }
Jason Sams24371d92009-08-19 12:17:14 -0700390}
391
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800392bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700393 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
394 LOGE("Context::setupCheck() 1 fail");
395 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800396 }
Jason Sams900f1612010-09-16 18:18:29 -0700397
398 mFragmentStore->setupGL2(this, &mStateFragmentStore);
399 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
400 mRaster->setupGL2(this, &mStateRaster);
401 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800402 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700403}
404
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700405void Context::setupProgramStore() {
406 mFragmentStore->setupGL2(this, &mStateFragmentStore);
407}
408
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800409static bool getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700410 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700411 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700412 return 0 != strcmp(buf, "0");
413}
Jason Sams326e0dd2009-05-22 14:03:28 -0700414
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800415void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700416 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700417 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700418 float oldR, oldG, oldB, oldA;
419 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700420 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700421
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700422 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700423 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700424 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700425
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700426 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700427 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700428
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700429 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700430}
431
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800432void * Context::threadProc(void *vrsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700433 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800434 rsc->mNativeThreadId = gettid();
435
436 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800437 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700438
Jason Sams1fddd902009-09-25 15:25:00 -0700439 rsc->props.mLogTimes = getProp("debug.rs.profile");
440 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800441 rsc->props.mLogObjects = getProp("debug.rs.object");
442 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700443 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
444 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700445 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700446
Jason Sams605048a2010-09-30 18:15:52 -0700447 rsc->mTlsStruct = new ScriptTLSStruct;
448 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700449 LOGE("Error allocating tls storage");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700450 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
Jason Samse5769102009-06-19 16:03:18 -0700451 return NULL;
452 }
Jason Sams605048a2010-09-30 18:15:52 -0700453 rsc->mTlsStruct->mContext = rsc;
454 rsc->mTlsStruct->mScript = NULL;
455 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700456 if (status) {
457 LOGE("pthread_setspecific %i", status);
458 }
459
Jason Sams5c1c79a2010-11-03 14:27:11 -0700460 if (!rsc->initGLThread()) {
461 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
462 return NULL;
463 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700464
Stephen Hines01b7d292010-09-28 15:45:45 -0700465 rsc->mScriptC.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800466 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700467 rsc->mStateRaster.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800468 rsc->setProgramRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700469 rsc->mStateVertex.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800470 rsc->setProgramVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700471 rsc->mStateFragment.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800472 rsc->setProgramFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700473 rsc->mStateFragmentStore.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800474 rsc->setProgramStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700475 rsc->mStateFont.init(rsc);
476 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800477 rsc->mStateVertexArray.init(rsc);
478 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700479
Jason Sams326e0dd2009-05-22 14:03:28 -0700480 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700481 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700482 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700483 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700484 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800485 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700486
Jason Sams2dca84d2009-12-09 11:05:45 -0800487 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800488 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800489 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700490
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800491 if (rsc->props.mLogVisual) {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700492 rsc->displayDebugStats();
493 }
494
Jason Sams2dca84d2009-12-09 11:05:45 -0800495 mDraw = targetTime && !rsc->mPaused;
496 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700497 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800498 rsc->timerFrame();
499 rsc->timerSet(RS_TIMER_INTERNAL);
500 rsc->timerPrint();
501 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700502 }
Jason Sams177f8442010-10-29 10:19:21 -0700503 if (targetTime > 1) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800504 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
505 if (t > 0) {
506 usleep(t);
507 }
508 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700509 }
510
Jason Sams4c5f99e2010-09-14 14:59:03 -0700511 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800512 if (rsc->mIsGraphicsContext) {
513 rsc->mRaster.clear();
514 rsc->mFragment.clear();
515 rsc->mVertex.clear();
516 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700517 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800518 rsc->mRootScript.clear();
519 rsc->mStateRaster.deinit(rsc);
520 rsc->mStateVertex.deinit(rsc);
521 rsc->mStateFragment.deinit(rsc);
522 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700523 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800524 }
Jason Samse514b452009-09-25 14:51:22 -0700525 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700526
Jason Sams4820e8b2010-02-09 16:05:07 -0800527 if (rsc->mIsGraphicsContext) {
528 pthread_mutex_lock(&gInitMutex);
529 rsc->deinitEGL();
530 pthread_mutex_unlock(&gInitMutex);
531 }
Jason Sams605048a2010-09-30 18:15:52 -0700532 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700533
Jason Sams4c5f99e2010-09-14 14:59:03 -0700534 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700535 return NULL;
536}
537
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800538void * Context::helperThreadProc(void *vrsc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700539 Context *rsc = static_cast<Context *>(vrsc);
540 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
541
Jason Sams18133402010-07-20 15:09:00 -0700542 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700543
544 rsc->mWorkers.mLaunchSignals[idx].init();
545 rsc->mWorkers.mNativeThreadId[idx] = gettid();
546
Jason Sams8d957fa2010-09-28 14:41:22 -0700547#if 0
548 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
549 cpu_set_t cpuset;
550 memset(&cpuset, 0, sizeof(cpuset));
551 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
552 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700553 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700554 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
555#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700556
557 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700558 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
559 if (status) {
560 LOGE("pthread_setspecific %i", status);
561 }
562
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800563 while (rsc->mRunning) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700564 rsc->mWorkers.mLaunchSignals[idx].wait();
565 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700566 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
567 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700568 android_atomic_dec(&rsc->mWorkers.mRunningCount);
569 rsc->mWorkers.mCompleteSignal.set();
570 }
Jason Sams18133402010-07-20 15:09:00 -0700571
572 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700573 return NULL;
574}
575
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800576void Context::launchThreads(WorkerCallback_t cbk, void *data) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700577 mWorkers.mLaunchData = data;
578 mWorkers.mLaunchCallback = cbk;
579 mWorkers.mRunningCount = (int)mWorkers.mCount;
580 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
581 mWorkers.mLaunchSignals[ct].set();
582 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800583 while (mWorkers.mRunningCount) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700584 mWorkers.mCompleteSignal.wait();
585 }
586}
587
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800588void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800589 // Note: If we put this in the proper "background" policy
590 // the wallpapers can become completly unresponsive at times.
591 // This is probably not what we want for something the user is actively
592 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800593 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800594#if 0
595 SchedPolicy pol = SP_FOREGROUND;
596 if (p > 0) {
597 pol = SP_BACKGROUND;
598 }
599 if (!set_sched_policy(mNativeThreadId, pol)) {
600 // success; reset the priority as well
601 }
602#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700603 setpriority(PRIO_PROCESS, mNativeThreadId, p);
604 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
605 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
606 }
Jason Sams15832442009-11-15 12:14:26 -0800607#endif
608}
609
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800610Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700611 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700612 mRunning = false;
613 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700614 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700615 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800616 mError = RS_ERROR_NONE;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700617}
618
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800619Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700620 Context * rsc = new Context();
621 if (!rsc->initContext(dev, sc)) {
622 delete rsc;
623 return NULL;
624 }
625 return rsc;
626}
627
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800628bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700629 pthread_mutex_lock(&gInitMutex);
630
631 dev->addContext(this);
632 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700633 if (sc) {
634 mUserSurfaceConfig = *sc;
635 } else {
636 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
637 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800638
Jason Sams613cad12009-11-12 15:10:25 -0800639 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800640 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700641 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700642
Jason Samsa658e902009-06-04 14:35:01 -0700643 int status;
644 pthread_attr_t threadAttr;
645
Jason Samsfb03a222009-10-15 16:47:31 -0700646 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700647 status = pthread_key_create(&gThreadTLSKey, NULL);
648 if (status) {
649 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700650 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700651 return false;
Jason Sams9e4e13d2009-10-06 17:16:55 -0700652 }
Jason Samse5769102009-06-19 16:03:18 -0700653 }
Jason Samsfb03a222009-10-15 16:47:31 -0700654 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700655
Jason Samsfb03a222009-10-15 16:47:31 -0700656 pthread_mutex_unlock(&gInitMutex);
657
658 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700659
Jason Samsa658e902009-06-04 14:35:01 -0700660 status = pthread_attr_init(&threadAttr);
661 if (status) {
662 LOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700663 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700664 }
665
Jason Sams613cad12009-11-12 15:10:25 -0800666 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700667
Jason Sams24371d92009-08-19 12:17:14 -0700668 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700669 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700670
Jason Sams7c7c78a2010-07-26 17:12:55 -0700671 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
672 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
673 if (cpu < 2) cpu = 0;
674
675 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700676 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
677 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
678 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
679 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700680 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700681 if (status) {
682 LOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700683 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700684 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800685 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700686 usleep(100);
687 }
688
Jason Sams5c1c79a2010-11-03 14:27:11 -0700689 if (mError != RS_ERROR_NONE) {
690 return false;
691 }
692
Jason Sams8d957fa2010-09-28 14:41:22 -0700693 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700694 mWorkers.mRunningCount = 0;
695 mWorkers.mLaunchCount = 0;
696 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
697 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
698 if (status) {
699 mWorkers.mCount = ct;
700 LOGE("Created fewer than expected number of RS threads.");
701 break;
702 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700703 }
Jason Samsa658e902009-06-04 14:35:01 -0700704 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700705 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700706}
707
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800708Context::~Context() {
Jason Sams8c0ee652009-08-25 14:49:07 -0700709 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700710 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700711 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700712 void *res;
713
Jason Sams8c0ee652009-08-25 14:49:07 -0700714 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700715 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700716
Jason Samsfb03a222009-10-15 16:47:31 -0700717 // Global structure cleanup.
718 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700719 if (mDev) {
720 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700721 --gThreadTLSKeyCount;
722 if (!gThreadTLSKeyCount) {
723 pthread_key_delete(gThreadTLSKey);
724 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800725 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700726 }
Jason Samsfb03a222009-10-15 16:47:31 -0700727 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700728}
729
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800730void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800731 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800732
Jason Sams458f2dc2009-11-03 13:58:36 -0800733 EGLBoolean ret;
734 if (mEGL.mSurface != NULL) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700735 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800736 checkEglError("eglMakeCurrent", ret);
737
738 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
739 checkEglError("eglDestroySurface", ret);
740
741 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700742 mWidth = 1;
743 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800744 }
745
746 mWndSurface = sur;
747 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700748 mWidth = w;
749 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800750
Jason Sams458f2dc2009-11-03 13:58:36 -0800751 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
752 checkEglError("eglCreateWindowSurface");
753 if (mEGL.mSurface == EGL_NO_SURFACE) {
754 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
755 }
756
757 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
758 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800759
Jason Sams771565f2010-05-14 15:30:29 -0700760 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800761 }
762}
763
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800764void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800765 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700766 mPaused = true;
767}
768
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800769void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800770 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700771 mPaused = false;
772}
773
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800774void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800775 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700776 mRootScript.set(s);
777}
778
Jason Sams60709252010-11-17 15:29:32 -0800779void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800780 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700781 if (pfs == NULL) {
782 mFragmentStore.set(mStateFragmentStore.mDefault);
783 } else {
784 mFragmentStore.set(pfs);
785 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700786}
787
Jason Sams60709252010-11-17 15:29:32 -0800788void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800789 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700790 if (pf == NULL) {
791 mFragment.set(mStateFragment.mDefault);
792 } else {
793 mFragment.set(pf);
794 }
Jason Samscfb1d112009-08-05 13:57:03 -0700795}
796
Jason Sams60709252010-11-17 15:29:32 -0800797void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800798 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700799 if (pr == NULL) {
800 mRaster.set(mStateRaster.mDefault);
801 } else {
802 mRaster.set(pr);
803 }
804}
805
Jason Sams60709252010-11-17 15:29:32 -0800806void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800807 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700808 if (pv == NULL) {
809 mVertex.set(mStateVertex.mDefault);
810 } else {
811 mVertex.set(pv);
812 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700813}
814
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800815void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700816 rsAssert(mIsGraphicsContext);
817 if (f == NULL) {
818 mFont.set(mStateFont.mDefault);
819 } else {
820 mFont.set(f);
821 }
822}
823
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800824void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700825 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700826 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700827 mNames.add(obj);
828}
829
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800830void Context::removeName(ObjectBase *obj) {
831 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700832 if (obj == mNames[ct]) {
833 mNames.removeAt(ct);
834 return;
835 }
836 }
837}
838
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800839RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800840 *receiveLen = 0;
841 if (!wait && mIO.mToClient.isEmpty()) {
842 return RS_MESSAGE_TO_CLIENT_NONE;
843 }
844
845 uint32_t bytesData = 0;
846 uint32_t commandID = 0;
847 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
848 *receiveLen = bytesData - sizeof(uint32_t);
849 if (bytesData) {
850 *subID = d[0];
851 }
852 return (RsMessageToClientType)commandID;
853}
854
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800855RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700856 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700857 *receiveLen = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800858 if (!wait && mIO.mToClient.isEmpty()) {
859 return RS_MESSAGE_TO_CLIENT_NONE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700860 }
861
862 //LOGE("getMessageToClient 2 con=%p", this);
863 uint32_t bytesData = 0;
864 uint32_t commandID = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800865 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
Jason Sams8c401ef2009-10-06 13:58:47 -0700866 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
867
Jason Samsaad4bc52010-11-08 17:06:46 -0800868 *receiveLen = bytesData - sizeof(uint32_t);
869 *subID = d[0];
870
871 //LOGE("getMessageToClient %i %i", commandID, *subID);
Jason Sams8c401ef2009-10-06 13:58:47 -0700872 if (bufferLen >= bytesData) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800873 memcpy(data, d+1, *receiveLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700874 mIO.mToClient.next();
Jason Samsaad4bc52010-11-08 17:06:46 -0800875 return (RsMessageToClientType)commandID;
Jason Sams8c401ef2009-10-06 13:58:47 -0700876 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800877 return RS_MESSAGE_TO_CLIENT_RESIZE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700878}
879
Jason Sams87319de2010-11-22 16:20:16 -0800880bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
881 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsaad4bc52010-11-08 17:06:46 -0800882 //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700883 if (cmdID == 0) {
884 LOGE("Attempting to send invalid command 0 to client.");
885 return false;
886 }
887 if (!waitForSpace) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800888 if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700889 // Not enough room, and not waiting.
890 return false;
891 }
892 }
893 //LOGE("sendMessageToClient 2");
Jason Samsaad4bc52010-11-08 17:06:46 -0800894 uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
895 p[0] = subID;
Jason Samsef5867a2010-07-28 11:17:53 -0700896 if (len > 0) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800897 memcpy(p+1, data, len);
Jason Samsef5867a2010-07-28 11:17:53 -0700898 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800899 mIO.mToClient.commit(cmdID, len + sizeof(subID));
Jason Sams8c401ef2009-10-06 13:58:47 -0700900 //LOGE("sendMessageToClient 3");
901 return true;
902}
903
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800904void Context::initToClient() {
905 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700906 usleep(100);
907 }
908}
909
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800910void Context::deinitToClient() {
Jason Sams8c401ef2009-10-06 13:58:47 -0700911 mIO.mToClient.shutdown();
912}
Jason Sams50869382009-08-18 17:07:09 -0700913
Jason Sams87319de2010-11-22 16:20:16 -0800914void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800915 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800916 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800917}
918
919
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800920void Context::dumpDebug() const {
Jason Sams13e26342009-11-24 12:26:35 -0800921 LOGE("RS Context debug %p", this);
922 LOGE("RS Context debug");
923
924 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700925 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800926 LOGE(" GL vendor: %s", mGL.mVendor);
927 LOGE(" GL renderer: %s", mGL.mRenderer);
928 LOGE(" GL Version: %s", mGL.mVersion);
929 LOGE(" GL Extensions: %s", mGL.mExtensions);
930 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
931 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700932 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800933 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
934
Jason Sams4815c0d2009-12-15 12:58:36 -0800935 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
936 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
937 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
938 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800939}
Jason Samsa4a54e42009-06-10 18:39:40 -0700940
Jason Sams326e0dd2009-05-22 14:03:28 -0700941///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700942//
Jason Sams326e0dd2009-05-22 14:03:28 -0700943
944namespace android {
945namespace renderscript {
946
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800947void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700948}
Jason Sams326e0dd2009-05-22 14:03:28 -0700949
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800950void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700951 Script *s = static_cast<Script *>(vs);
952 rsc->setRootScript(s);
953}
954
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800955void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700956 Sampler *s = static_cast<Sampler *>(vs);
957
958 if (slot > RS_MAX_SAMPLER_SLOT) {
959 LOGE("Invalid sampler slot");
960 return;
961 }
962
963 s->bindToContext(&rsc->mStateSampler, slot);
964}
965
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800966void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700967 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800968 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700969}
970
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800971void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700972 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800973 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700974}
975
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800976void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700977 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800978 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700979}
980
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800981void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700982 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800983 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700984}
985
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800986void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700987 Font *font = static_cast<Font *>(vfont);
988 rsc->setFont(font);
989}
990
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800991void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700992 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700993 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700994}
Jason Sams326e0dd2009-05-22 14:03:28 -0700995
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800996void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700997 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700998 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700999 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -07001000}
1001
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001002void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001003 rsc->pause();
1004}
1005
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001006void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001007 rsc->resume();
1008}
1009
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001010void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -08001011 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -08001012}
1013
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001014void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -08001015 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001016}
1017
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001018void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -08001019 ObjectBase::dumpAll(rsc);
1020}
1021
Jason Sams326e0dd2009-05-22 14:03:28 -07001022}
1023}
1024
1025
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001026RsContext rsContextCreate(RsDevice vdev, uint32_t version) {
Jason Sams4820e8b2010-02-09 16:05:07 -08001027 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001028 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001029 Context *rsc = Context::createContext(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001030 return rsc;
1031}
1032
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001033RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc) {
Jason Sams6b8552a2010-10-13 15:31:10 -07001034 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001035 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001036 Context *rsc = Context::createContext(dev, &sc);
Jason Sams900f1612010-09-16 18:18:29 -07001037 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001038 return rsc;
1039}
1040
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001041void rsContextDestroy(RsContext vrsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001042 Context * rsc = static_cast<Context *>(vrsc);
1043 delete rsc;
1044}
1045
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001046RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001047 Context * rsc = static_cast<Context *>(vrsc);
Jason Samsaad4bc52010-11-08 17:06:46 -08001048 return rsc->peekMessageToClient(receiveLen, subID, wait);
1049}
1050
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001051RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -08001052 Context * rsc = static_cast<Context *>(vrsc);
1053 return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait);
Jason Sams8c401ef2009-10-06 13:58:47 -07001054}
1055
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001056void rsContextInitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001057 Context * rsc = static_cast<Context *>(vrsc);
1058 rsc->initToClient();
1059}
1060
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001061void rsContextDeinitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001062 Context * rsc = static_cast<Context *>(vrsc);
1063 rsc->deinitToClient();
1064}
1065
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001066// Only to be called at a3d load time, before object is visible to user
1067// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001068void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001069 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1070 (*name) = ob->getName();
1071}