blob: 7dc26d23a6a35d87f39172f0537746a38a364ddd [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;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080047pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070048
Jason Sams33b6e3b2009-10-27 14:44:31 -070049static 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 Sams6b8552a2010-10-13 15:31:10 -070061void 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 Sakhartchoukafb743a2010-11-09 17:00:54 -0800111bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -0700112 pthread_mutex_lock(&gInitMutex);
113 LOGV("initGLThread start %p", this);
114
Jason Samsafcb25c2009-08-25 11:34:49 -0700115 mEGL.mNumConfigs = -1;
116 EGLint configAttribs[128];
117 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -0800118 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -0700119
Jason Samsafcb25c2009-08-25 11:34:49 -0700120 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -0700121
Jason Samsafcb25c2009-08-25 11:34:49 -0700122 configAttribsPtr[0] = EGL_SURFACE_TYPE;
123 configAttribsPtr[1] = EGL_WINDOW_BIT;
124 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125
Jason Sams6b8552a2010-10-13 15:31:10 -0700126 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
127 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
128 configAttribsPtr += 2;
Jason Samsc460e552009-11-25 13:22:07 -0800129
Jason Sams6b8552a2010-10-13 15:31:10 -0700130 if (mUserSurfaceConfig.depthMin > 0) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700131 configAttribsPtr[0] = EGL_DEPTH_SIZE;
Jason Sams6b8552a2010-10-13 15:31:10 -0700132 configAttribsPtr[1] = mUserSurfaceConfig.depthMin;
Jason Samsafcb25c2009-08-25 11:34:49 -0700133 configAttribsPtr += 2;
134 }
Jason Sams9397e302009-08-27 20:23:34 -0700135
Jason Sams5fd09d82009-09-23 13:57:02 -0700136 if (mDev->mForceSW) {
137 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
138 configAttribsPtr[1] = EGL_SLOW_CONFIG;
139 configAttribsPtr += 2;
140 }
141
Jason Samsafcb25c2009-08-25 11:34:49 -0700142 configAttribsPtr[0] = EGL_NONE;
143 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -0700144
Jason Sams4c5f99e2010-09-14 14:59:03 -0700145 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700146 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700147 checkEglError("eglGetDisplay");
148
Jason Samsafcb25c2009-08-25 11:34:49 -0700149 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700150 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -0700151
Jason Sams6b8552a2010-10-13 15:31:10 -0700152#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 Samsafcb25c2009-08-25 11:34:49 -0700159 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700160 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700161 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700162 if (props.mLogVisual) {
163 printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig);
Jason Samsc460e552009-11-25 13:22:07 -0800164 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700165#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 Sams33b6e3b2009-10-27 14:44:31 -0700170 checkEglError("eglCreateContext");
171 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700172 pthread_mutex_unlock(&gInitMutex);
Jason Sams4c5f99e2010-09-14 14:59:03 -0700173 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700174 return false;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700175 }
176 gGLContextCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700177
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 Sams5c1c79a2010-11-03 14:27:11 -0700184 pthread_mutex_unlock(&gInitMutex);
185 deinitEGL();
186 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700187 }
188
189 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700190 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 Sams6b8552a2010-10-13 15:31:10 -0700197
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 Sams249d4532011-01-23 17:48:45 -0800204 //LOGV("GL Version %s", mGL.mVersion);
Jason Sams6b8552a2010-10-13 15:31:10 -0700205 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams249d4532011-01-23 17:48:45 -0800206 //LOGV("GL Renderer %s", mGL.mRenderer);
Jason Sams6b8552a2010-10-13 15:31:10 -0700207 //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 Sams5c1c79a2010-11-03 14:27:11 -0700221 pthread_mutex_unlock(&gInitMutex);
222 deinitEGL();
223 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700224 } 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 Sams0f7785c2011-01-13 17:02:35 -0800239 mGL.GL_IMG_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_IMG_texture_npot");
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700240 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 -0700241 mGL.EXT_texture_max_aniso = 1.0f;
242 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800243 if (hasAniso) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700244 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 Sams5c1c79a2010-11-03 14:27:11 -0700249 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700250}
251
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800252void Context::deinitEGL() {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700253 LOGV("%p, deinitEGL", this);
Jason Sams6b8552a2010-10-13 15:31:10 -0700254
Jason Sams5c1c79a2010-11-03 14:27:11 -0700255 if (mEGL.mContext != EGL_NO_CONTEXT) {
Mathias Agopianb7ee74d2011-01-19 16:42:02 -0800256 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 Sams5c1c79a2010-11-03 14:27:11 -0700261 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
262 checkEglError("eglDestroyContext");
263 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700264
265 gGLContextCount--;
266 if (!gGLContextCount) {
267 eglTerminate(mEGL.mDisplay);
268 }
269}
270
Jason Sams60709252010-11-17 15:29:32 -0800271Context::PushState::PushState(Context *con) {
272 mRsc = con;
Jason Samsc946b612011-02-23 14:47:17 -0800273 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 Sams60709252010-11-17 15:29:32 -0800280}
281
282Context::PushState::~PushState() {
Jason Samsc946b612011-02-23 14:47:17 -0800283 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 Sams60709252010-11-17 15:29:32 -0800290}
291
Jason Sams33b6e3b2009-10-27 14:44:31 -0700292
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800293uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -0800294 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -0700295
Jason Samsc61346b2010-05-28 18:23:22 -0700296 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700297 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700298}
299
Jason Sams87319de2010-11-22 16:20:16 -0800300void Context::checkError(const char *msg, bool isFatal) const {
301
Jason Samsd01d9702009-12-23 14:35:29 -0800302 GLenum err = glGetError();
303 if (err != GL_NO_ERROR) {
Jason Sams87319de2010-11-22 16:20:16 -0800304 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 Samsd01d9702009-12-23 14:35:29 -0800321 }
322}
Jason Sams10308932009-06-09 12:15:30 -0700323
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800324uint32_t Context::runRootScript() {
Jason Sams771565f2010-05-14 15:30:29 -0700325 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700326
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700328 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700329 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700330
Jason Samsd01d9702009-12-23 14:35:29 -0800331 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700332 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700333}
334
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800335uint64_t Context::getTime() const {
Jason Sams24371d92009-08-19 12:17:14 -0700336 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 Sakhartchoukafb743a2010-11-09 17:00:54 -0800341void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700342 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
343 mTimers[ct] = 0;
344 }
345}
346
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800347void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700348 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700349 mTimeFrame = mTimeLast;
350 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700351 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700352 mAverageFPSFrameCount = 0;
353 mAverageFPSStartTime = mTimeLast;
354 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700355 timerReset();
356}
357
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800358void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700359 mTimeLastFrame = mTimeFrame;
360 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700361 // Update average fps
362 const uint64_t averageFramerateInterval = 1000 * 1000000;
363 mAverageFPSFrameCount ++;
364 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800365 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700366 inverval = inverval / 1000000;
367 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
368 mAverageFPSFrameCount = 0;
369 mAverageFPSStartTime = mTimeFrame;
370 }
Jason Sams1d54f102009-09-03 15:43:13 -0700371}
372
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800373void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700374 uint64_t last = mTimeLast;
375 mTimeLast = getTime();
376 mTimers[mTimerActive] += mTimeLast - last;
377 mTimerActive = tm;
378}
379
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800380void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700381 double total = 0;
382 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
383 total += mTimers[ct];
384 }
Jason Sams1d54f102009-09-03 15:43:13 -0700385 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800386 mTimeMSLastFrame = frame / 1000000;
387 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
388 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700389
Jason Sams2dca84d2009-12-09 11:05:45 -0800390
391 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700392 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 -0800393 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 Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700397 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
398 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800399 }
Jason Sams24371d92009-08-19 12:17:14 -0700400}
401
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800402bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700403 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
404 LOGE("Context::setupCheck() 1 fail");
405 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800406 }
Jason Sams900f1612010-09-16 18:18:29 -0700407
408 mFragmentStore->setupGL2(this, &mStateFragmentStore);
409 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
410 mRaster->setupGL2(this, &mStateRaster);
411 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800412 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700413}
414
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700415void Context::setupProgramStore() {
416 mFragmentStore->setupGL2(this, &mStateFragmentStore);
417}
418
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800419static bool getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700420 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700421 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700422 return 0 != strcmp(buf, "0");
423}
Jason Sams326e0dd2009-05-22 14:03:28 -0700424
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800425void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700426 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700427 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700428 float oldR, oldG, oldB, oldA;
429 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700430 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700431
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700432 ObjectBaseRef<Font> lastFont(getFont());
433 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700434 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700435 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700436 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700437
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700438 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700439 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700440
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700441 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700442 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700443}
444
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800445void * Context::threadProc(void *vrsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700446 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800447 rsc->mNativeThreadId = gettid();
448
449 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800450 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700451
Jason Sams1fddd902009-09-25 15:25:00 -0700452 rsc->props.mLogTimes = getProp("debug.rs.profile");
453 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800454 rsc->props.mLogObjects = getProp("debug.rs.object");
455 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700456 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
457 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700458 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700459
Jason Sams605048a2010-09-30 18:15:52 -0700460 rsc->mTlsStruct = new ScriptTLSStruct;
461 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700462 LOGE("Error allocating tls storage");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700463 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
Jason Samse5769102009-06-19 16:03:18 -0700464 return NULL;
465 }
Jason Sams605048a2010-09-30 18:15:52 -0700466 rsc->mTlsStruct->mContext = rsc;
467 rsc->mTlsStruct->mScript = NULL;
468 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700469 if (status) {
470 LOGE("pthread_setspecific %i", status);
471 }
472
Jason Sams5c1c79a2010-11-03 14:27:11 -0700473 if (!rsc->initGLThread()) {
474 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
475 return NULL;
476 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700477
Jason Sams4820e8b2010-02-09 16:05:07 -0800478 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700479 rsc->mStateRaster.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800480 rsc->setProgramRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700481 rsc->mStateVertex.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800482 rsc->setProgramVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700483 rsc->mStateFragment.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800484 rsc->setProgramFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700485 rsc->mStateFragmentStore.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800486 rsc->setProgramStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700487 rsc->mStateFont.init(rsc);
488 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800489 rsc->mStateVertexArray.init(rsc);
490 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700491
Jason Sams326e0dd2009-05-22 14:03:28 -0700492 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700493 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700494 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700495 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700496 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800497 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700498
Jason Sams2dca84d2009-12-09 11:05:45 -0800499 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800500 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800501 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700502
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800503 if (rsc->props.mLogVisual) {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700504 rsc->displayDebugStats();
505 }
506
Jason Sams2dca84d2009-12-09 11:05:45 -0800507 mDraw = targetTime && !rsc->mPaused;
508 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700509 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800510 rsc->timerFrame();
511 rsc->timerSet(RS_TIMER_INTERNAL);
512 rsc->timerPrint();
513 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700514 }
Jason Sams177f8442010-10-29 10:19:21 -0700515 if (targetTime > 1) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800516 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
517 if (t > 0) {
518 usleep(t);
519 }
520 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700521 }
522
Jason Sams4c5f99e2010-09-14 14:59:03 -0700523 LOGV("%p, RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700524
Jason Sams4820e8b2010-02-09 16:05:07 -0800525 if (rsc->mIsGraphicsContext) {
526 pthread_mutex_lock(&gInitMutex);
527 rsc->deinitEGL();
528 pthread_mutex_unlock(&gInitMutex);
529 }
Jason Sams605048a2010-09-30 18:15:52 -0700530 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700531
Jason Sams4c5f99e2010-09-14 14:59:03 -0700532 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700533 return NULL;
534}
535
Jason Sams741aac92010-12-24 14:38:39 -0800536void Context::destroyWorkerThreadResources() {
Jason Sams249d4532011-01-23 17:48:45 -0800537 //LOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800538 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800539 if (mIsGraphicsContext) {
540 mRaster.clear();
541 mFragment.clear();
542 mVertex.clear();
543 mFragmentStore.clear();
544 mFont.clear();
545 mRootScript.clear();
546 mStateRaster.deinit(this);
547 mStateVertex.deinit(this);
548 mStateFragment.deinit(this);
549 mStateFragmentStore.deinit(this);
550 mStateFont.deinit(this);
Jason Sams2cbfc4c2011-01-05 03:37:48 -0800551 mShaderCache.cleanupAll();
Jason Sams741aac92010-12-24 14:38:39 -0800552 }
Jason Sams249d4532011-01-23 17:48:45 -0800553 //LOGV("destroyWorkerThreadResources 2");
Jason Samscf912de2011-01-09 16:09:51 -0800554 mExit = true;
Jason Sams741aac92010-12-24 14:38:39 -0800555}
556
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800557void * Context::helperThreadProc(void *vrsc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700558 Context *rsc = static_cast<Context *>(vrsc);
559 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
560
Jason Sams249d4532011-01-23 17:48:45 -0800561 //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700562
563 rsc->mWorkers.mLaunchSignals[idx].init();
564 rsc->mWorkers.mNativeThreadId[idx] = gettid();
565
Jason Sams8d957fa2010-09-28 14:41:22 -0700566#if 0
567 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
568 cpu_set_t cpuset;
569 memset(&cpuset, 0, sizeof(cpuset));
570 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
571 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700572 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700573 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
574#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700575
576 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700577 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
578 if (status) {
579 LOGE("pthread_setspecific %i", status);
580 }
581
Jason Sams249d4532011-01-23 17:48:45 -0800582 while (!rsc->mExit) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700583 rsc->mWorkers.mLaunchSignals[idx].wait();
584 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700585 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
586 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700587 android_atomic_dec(&rsc->mWorkers.mRunningCount);
588 rsc->mWorkers.mCompleteSignal.set();
589 }
Jason Sams18133402010-07-20 15:09:00 -0700590
Jason Sams249d4532011-01-23 17:48:45 -0800591 //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700592 return NULL;
593}
594
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800595void Context::launchThreads(WorkerCallback_t cbk, void *data) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700596 mWorkers.mLaunchData = data;
597 mWorkers.mLaunchCallback = cbk;
Stephen Hines4f947d72011-03-08 16:49:28 -0800598 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700599 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
600 mWorkers.mLaunchSignals[ct].set();
601 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800602 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700603 mWorkers.mCompleteSignal.wait();
604 }
605}
606
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800607void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800608 // Note: If we put this in the proper "background" policy
609 // the wallpapers can become completly unresponsive at times.
610 // This is probably not what we want for something the user is actively
611 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800612 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800613#if 0
614 SchedPolicy pol = SP_FOREGROUND;
615 if (p > 0) {
616 pol = SP_BACKGROUND;
617 }
618 if (!set_sched_policy(mNativeThreadId, pol)) {
619 // success; reset the priority as well
620 }
621#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700622 setpriority(PRIO_PROCESS, mNativeThreadId, p);
623 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
624 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
625 }
Jason Sams15832442009-11-15 12:14:26 -0800626#endif
627}
628
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800629Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700630 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700631 mRunning = false;
632 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700633 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700634 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800635 mError = RS_ERROR_NONE;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700636 mDPI = 96;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700637}
638
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800639Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700640 Context * rsc = new Context();
641 if (!rsc->initContext(dev, sc)) {
642 delete rsc;
643 return NULL;
644 }
645 return rsc;
646}
647
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800648bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700649 pthread_mutex_lock(&gInitMutex);
650
651 dev->addContext(this);
652 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700653 if (sc) {
654 mUserSurfaceConfig = *sc;
655 } else {
656 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
657 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800658
Jason Sams613cad12009-11-12 15:10:25 -0800659 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700661 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700662
Jason Samsa658e902009-06-04 14:35:01 -0700663 int status;
664 pthread_attr_t threadAttr;
665
Jason Samsfb03a222009-10-15 16:47:31 -0700666 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700667 status = pthread_key_create(&gThreadTLSKey, NULL);
668 if (status) {
669 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700670 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700671 return false;
Jason Sams9e4e13d2009-10-06 17:16:55 -0700672 }
Jason Samse5769102009-06-19 16:03:18 -0700673 }
Jason Samsfb03a222009-10-15 16:47:31 -0700674 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700675
Jason Samsfb03a222009-10-15 16:47:31 -0700676 pthread_mutex_unlock(&gInitMutex);
677
678 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700679
Jason Samsa658e902009-06-04 14:35:01 -0700680 status = pthread_attr_init(&threadAttr);
681 if (status) {
682 LOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700683 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700684 }
685
Jason Sams613cad12009-11-12 15:10:25 -0800686 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700687
Jason Sams24371d92009-08-19 12:17:14 -0700688 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700689 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700690
Jason Samsbad80742011-03-16 16:29:28 -0700691 if (!rsdHalInit(this, 0, 0)) {
692 return false;
693 }
694
Jason Sams7c7c78a2010-07-26 17:12:55 -0700695 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
696 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
697 if (cpu < 2) cpu = 0;
698
699 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700700 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
701 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
702 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
703 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700704 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700705 if (status) {
706 LOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700707 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700708 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800709 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700710 usleep(100);
711 }
712
Jason Sams5c1c79a2010-11-03 14:27:11 -0700713 if (mError != RS_ERROR_NONE) {
714 return false;
715 }
716
Jason Sams8d957fa2010-09-28 14:41:22 -0700717 mWorkers.mCompleteSignal.init();
Stephen Hines4f947d72011-03-08 16:49:28 -0800718 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
719 android_atomic_release_store(0, &mWorkers.mLaunchCount);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700720 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
721 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
722 if (status) {
723 mWorkers.mCount = ct;
724 LOGE("Created fewer than expected number of RS threads.");
725 break;
726 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700727 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800728 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) {
729 usleep(100);
730 }
Jason Samsa658e902009-06-04 14:35:01 -0700731 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700732 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700733}
734
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800735Context::~Context() {
Jason Sams8c0ee652009-08-25 14:49:07 -0700736 LOGV("Context::~Context");
Jason Samscf912de2011-01-09 16:09:51 -0800737
738 mIO.mToCore.flush();
739 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700740 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700741 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700742 void *res;
743
Jason Sams8c0ee652009-08-25 14:49:07 -0700744 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700745 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700746
Jason Sams249d4532011-01-23 17:48:45 -0800747 // Cleanup compute threads.
748 mWorkers.mLaunchData = NULL;
749 mWorkers.mLaunchCallback = NULL;
Stephen Hines4f947d72011-03-08 16:49:28 -0800750 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
Jason Sams249d4532011-01-23 17:48:45 -0800751 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
752 mWorkers.mLaunchSignals[ct].set();
Jason Sams51462c52011-01-25 00:26:25 -0800753 }
754 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
Stephen Hines196c1112011-03-01 17:34:59 -0800755 status = pthread_join(mWorkers.mThreadId[ct], &res);
Jason Sams249d4532011-01-23 17:48:45 -0800756 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800757 rsAssert(android_atomic_acquire_load(&mWorkers.mRunningCount) == 0);
Jason Sams249d4532011-01-23 17:48:45 -0800758
Jason Samsfb03a222009-10-15 16:47:31 -0700759 // Global structure cleanup.
760 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700761 if (mDev) {
762 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700763 --gThreadTLSKeyCount;
764 if (!gThreadTLSKeyCount) {
765 pthread_key_delete(gThreadTLSKey);
766 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800767 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700768 }
Jason Samsfb03a222009-10-15 16:47:31 -0700769 pthread_mutex_unlock(&gInitMutex);
Jason Sams741aac92010-12-24 14:38:39 -0800770 LOGV("Context::~Context done");
Jason Sams326e0dd2009-05-22 14:03:28 -0700771}
772
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800773void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800774 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800775
Jason Sams458f2dc2009-11-03 13:58:36 -0800776 EGLBoolean ret;
Jason Sams52a67b22011-01-12 15:26:25 -0800777 // WAR: Some drivers fail to handle 0 size surfaces correcntly.
778 // Use the pbuffer to avoid this pitfall.
779 if ((mEGL.mSurface != NULL) || (w == 0) || (h == 0)) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700780 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800781 checkEglError("eglMakeCurrent", ret);
782
783 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
784 checkEglError("eglDestroySurface", ret);
785
786 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700787 mWidth = 1;
788 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800789 }
790
791 mWndSurface = sur;
792 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700793 mWidth = w;
794 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800795
Jason Sams458f2dc2009-11-03 13:58:36 -0800796 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
797 checkEglError("eglCreateWindowSurface");
798 if (mEGL.mSurface == EGL_NO_SURFACE) {
799 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
800 }
801
802 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
803 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800804
Jason Sams771565f2010-05-14 15:30:29 -0700805 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800806 }
807}
808
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800809void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800810 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700811 mPaused = true;
812}
813
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800814void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800815 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700816 mPaused = false;
817}
818
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800819void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800820 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700821 mRootScript.set(s);
822}
823
Jason Sams60709252010-11-17 15:29:32 -0800824void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800825 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700826 if (pfs == NULL) {
827 mFragmentStore.set(mStateFragmentStore.mDefault);
828 } else {
829 mFragmentStore.set(pfs);
830 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700831}
832
Jason Sams60709252010-11-17 15:29:32 -0800833void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800834 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700835 if (pf == NULL) {
836 mFragment.set(mStateFragment.mDefault);
837 } else {
838 mFragment.set(pf);
839 }
Jason Samscfb1d112009-08-05 13:57:03 -0700840}
841
Jason Sams60709252010-11-17 15:29:32 -0800842void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800843 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700844 if (pr == NULL) {
845 mRaster.set(mStateRaster.mDefault);
846 } else {
847 mRaster.set(pr);
848 }
849}
850
Jason Sams60709252010-11-17 15:29:32 -0800851void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800852 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700853 if (pv == NULL) {
854 mVertex.set(mStateVertex.mDefault);
855 } else {
856 mVertex.set(pv);
857 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700858}
859
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800860void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700861 rsAssert(mIsGraphicsContext);
862 if (f == NULL) {
863 mFont.set(mStateFont.mDefault);
864 } else {
865 mFont.set(f);
866 }
867}
868
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800869void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700870 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700871 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700872 mNames.add(obj);
873}
874
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800875void Context::removeName(ObjectBase *obj) {
876 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700877 if (obj == mNames[ct]) {
878 mNames.removeAt(ct);
879 return;
880 }
881 }
882}
883
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800884RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800885 *receiveLen = 0;
886 if (!wait && mIO.mToClient.isEmpty()) {
887 return RS_MESSAGE_TO_CLIENT_NONE;
888 }
889
890 uint32_t bytesData = 0;
891 uint32_t commandID = 0;
892 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
893 *receiveLen = bytesData - sizeof(uint32_t);
894 if (bytesData) {
895 *subID = d[0];
896 }
897 return (RsMessageToClientType)commandID;
898}
899
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800900RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700901 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700902 *receiveLen = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800903 if (!wait && mIO.mToClient.isEmpty()) {
904 return RS_MESSAGE_TO_CLIENT_NONE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700905 }
906
907 //LOGE("getMessageToClient 2 con=%p", this);
908 uint32_t bytesData = 0;
909 uint32_t commandID = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800910 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
Jason Sams8c401ef2009-10-06 13:58:47 -0700911 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
912
Jason Samsaad4bc52010-11-08 17:06:46 -0800913 *receiveLen = bytesData - sizeof(uint32_t);
914 *subID = d[0];
915
916 //LOGE("getMessageToClient %i %i", commandID, *subID);
Jason Sams8c401ef2009-10-06 13:58:47 -0700917 if (bufferLen >= bytesData) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800918 memcpy(data, d+1, *receiveLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700919 mIO.mToClient.next();
Jason Samsaad4bc52010-11-08 17:06:46 -0800920 return (RsMessageToClientType)commandID;
Jason Sams8c401ef2009-10-06 13:58:47 -0700921 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800922 return RS_MESSAGE_TO_CLIENT_RESIZE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700923}
924
Jason Sams87319de2010-11-22 16:20:16 -0800925bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
926 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsaad4bc52010-11-08 17:06:46 -0800927 //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700928 if (cmdID == 0) {
929 LOGE("Attempting to send invalid command 0 to client.");
930 return false;
931 }
932 if (!waitForSpace) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800933 if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700934 // Not enough room, and not waiting.
935 return false;
936 }
937 }
938 //LOGE("sendMessageToClient 2");
Jason Samsaad4bc52010-11-08 17:06:46 -0800939 uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
940 p[0] = subID;
Jason Samsef5867a2010-07-28 11:17:53 -0700941 if (len > 0) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800942 memcpy(p+1, data, len);
Jason Samsef5867a2010-07-28 11:17:53 -0700943 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800944 mIO.mToClient.commit(cmdID, len + sizeof(subID));
Jason Sams8c401ef2009-10-06 13:58:47 -0700945 //LOGE("sendMessageToClient 3");
946 return true;
947}
948
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800949void Context::initToClient() {
950 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700951 usleep(100);
952 }
953}
954
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800955void Context::deinitToClient() {
Jason Sams8c401ef2009-10-06 13:58:47 -0700956 mIO.mToClient.shutdown();
957}
Jason Sams50869382009-08-18 17:07:09 -0700958
Jason Sams87319de2010-11-22 16:20:16 -0800959void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800960 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800961 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800962}
963
964
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800965void Context::dumpDebug() const {
Jason Sams13e26342009-11-24 12:26:35 -0800966 LOGE("RS Context debug %p", this);
967 LOGE("RS Context debug");
968
969 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700970 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800971 LOGE(" GL vendor: %s", mGL.mVendor);
972 LOGE(" GL renderer: %s", mGL.mRenderer);
973 LOGE(" GL Version: %s", mGL.mVersion);
974 LOGE(" GL Extensions: %s", mGL.mExtensions);
975 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
976 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700977 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800978 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
979
Jason Sams4815c0d2009-12-15 12:58:36 -0800980 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
981 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
982 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
983 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800984}
Jason Samsa4a54e42009-06-10 18:39:40 -0700985
Jason Sams326e0dd2009-05-22 14:03:28 -0700986///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700987//
Jason Sams326e0dd2009-05-22 14:03:28 -0700988
989namespace android {
990namespace renderscript {
991
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800992void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700993}
Jason Sams326e0dd2009-05-22 14:03:28 -0700994
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800995void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700996 Script *s = static_cast<Script *>(vs);
997 rsc->setRootScript(s);
998}
999
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001000void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001001 Sampler *s = static_cast<Sampler *>(vs);
1002
1003 if (slot > RS_MAX_SAMPLER_SLOT) {
1004 LOGE("Invalid sampler slot");
1005 return;
1006 }
1007
1008 s->bindToContext(&rsc->mStateSampler, slot);
1009}
1010
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001011void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -07001012 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -08001013 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -07001014}
1015
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001016void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001017 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -08001018 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -07001019}
1020
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001021void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -07001022 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -08001023 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -07001024}
1025
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001026void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001027 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -08001028 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -07001029}
1030
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001031void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001032 Font *font = static_cast<Font *>(vfont);
1033 rsc->setFont(font);
1034}
1035
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001036void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001037 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -07001038 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001039}
Jason Sams326e0dd2009-05-22 14:03:28 -07001040
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001041void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -07001042 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -07001043 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -07001044 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -07001045}
1046
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001047void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001048 rsc->pause();
1049}
1050
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001051void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001052 rsc->resume();
1053}
1054
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001055void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -08001056 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -08001057}
1058
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001059void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -08001060 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001061}
1062
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001063void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -08001064 ObjectBase::dumpAll(rsc);
1065}
1066
Jason Sams741aac92010-12-24 14:38:39 -08001067void rsi_ContextDestroyWorker(Context *rsc) {
Jason Sams741aac92010-12-24 14:38:39 -08001068 rsc->destroyWorkerThreadResources();;
Jason Sams741aac92010-12-24 14:38:39 -08001069}
1070
1071}
1072}
1073
1074void rsContextDestroy(RsContext vcon) {
1075 LOGV("rsContextDestroy %p", vcon);
1076 Context *rsc = static_cast<Context *>(vcon);
1077 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -08001078 delete rsc;
Jason Sams741aac92010-12-24 14:38:39 -08001079 LOGV("rsContextDestroy 2 %p", vcon);
Jason Sams1dcefab2010-12-09 12:19:46 -08001080}
1081
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001082RsContext rsContextCreate(RsDevice vdev, uint32_t version) {
Jason Sams4820e8b2010-02-09 16:05:07 -08001083 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001084 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001085 Context *rsc = Context::createContext(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001086 return rsc;
1087}
1088
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -07001089RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
1090 RsSurfaceConfig sc, uint32_t dpi) {
Jason Sams6b8552a2010-10-13 15:31:10 -07001091 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001092 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001093 Context *rsc = Context::createContext(dev, &sc);
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -07001094 rsc->setDPI(dpi);
Jason Sams900f1612010-09-16 18:18:29 -07001095 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001096 return rsc;
1097}
1098
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001099RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001100 Context * rsc = static_cast<Context *>(vrsc);
Jason Samsaad4bc52010-11-08 17:06:46 -08001101 return rsc->peekMessageToClient(receiveLen, subID, wait);
1102}
1103
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001104RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -08001105 Context * rsc = static_cast<Context *>(vrsc);
1106 return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait);
Jason Sams8c401ef2009-10-06 13:58:47 -07001107}
1108
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001109void rsContextInitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001110 Context * rsc = static_cast<Context *>(vrsc);
1111 rsc->initToClient();
1112}
1113
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001114void rsContextDeinitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001115 Context * rsc = static_cast<Context *>(vrsc);
1116 rsc->deinitToClient();
1117}
1118
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001119// Only to be called at a3d load time, before object is visible to user
1120// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001121void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001122 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1123 (*name) = ob->getName();
1124}