blob: c63d183c7b59927ea9cd0a78ac8c0aea717d9a57 [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 Sakhartchouk09c67352010-10-05 11:33:27 -0700432 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700433 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700434 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700435
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700436 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700437 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700438
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700439 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700440}
441
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800442void * Context::threadProc(void *vrsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700443 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800444 rsc->mNativeThreadId = gettid();
445
446 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800447 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700448
Jason Sams1fddd902009-09-25 15:25:00 -0700449 rsc->props.mLogTimes = getProp("debug.rs.profile");
450 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800451 rsc->props.mLogObjects = getProp("debug.rs.object");
452 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700453 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
454 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700455 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700456
Jason Sams605048a2010-09-30 18:15:52 -0700457 rsc->mTlsStruct = new ScriptTLSStruct;
458 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700459 LOGE("Error allocating tls storage");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700460 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
Jason Samse5769102009-06-19 16:03:18 -0700461 return NULL;
462 }
Jason Sams605048a2010-09-30 18:15:52 -0700463 rsc->mTlsStruct->mContext = rsc;
464 rsc->mTlsStruct->mScript = NULL;
465 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700466 if (status) {
467 LOGE("pthread_setspecific %i", status);
468 }
469
Jason Sams5c1c79a2010-11-03 14:27:11 -0700470 if (!rsc->initGLThread()) {
471 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
472 return NULL;
473 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700474
Jason Sams4820e8b2010-02-09 16:05:07 -0800475 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700476 rsc->mStateRaster.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800477 rsc->setProgramRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700478 rsc->mStateVertex.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800479 rsc->setProgramVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700480 rsc->mStateFragment.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800481 rsc->setProgramFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700482 rsc->mStateFragmentStore.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800483 rsc->setProgramStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700484 rsc->mStateFont.init(rsc);
485 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800486 rsc->mStateVertexArray.init(rsc);
487 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700488
Jason Sams326e0dd2009-05-22 14:03:28 -0700489 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700490 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700491 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700492 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700493 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800494 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700495
Jason Sams2dca84d2009-12-09 11:05:45 -0800496 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800497 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800498 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700499
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800500 if (rsc->props.mLogVisual) {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700501 rsc->displayDebugStats();
502 }
503
Jason Sams2dca84d2009-12-09 11:05:45 -0800504 mDraw = targetTime && !rsc->mPaused;
505 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700506 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800507 rsc->timerFrame();
508 rsc->timerSet(RS_TIMER_INTERNAL);
509 rsc->timerPrint();
510 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700511 }
Jason Sams177f8442010-10-29 10:19:21 -0700512 if (targetTime > 1) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800513 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
514 if (t > 0) {
515 usleep(t);
516 }
517 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700518 }
519
Jason Sams4c5f99e2010-09-14 14:59:03 -0700520 LOGV("%p, RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700521
Jason Sams4820e8b2010-02-09 16:05:07 -0800522 if (rsc->mIsGraphicsContext) {
523 pthread_mutex_lock(&gInitMutex);
524 rsc->deinitEGL();
525 pthread_mutex_unlock(&gInitMutex);
526 }
Jason Sams605048a2010-09-30 18:15:52 -0700527 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700528
Jason Sams4c5f99e2010-09-14 14:59:03 -0700529 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700530 return NULL;
531}
532
Jason Sams741aac92010-12-24 14:38:39 -0800533void Context::destroyWorkerThreadResources() {
Jason Sams249d4532011-01-23 17:48:45 -0800534 //LOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800535 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800536 if (mIsGraphicsContext) {
537 mRaster.clear();
538 mFragment.clear();
539 mVertex.clear();
540 mFragmentStore.clear();
541 mFont.clear();
542 mRootScript.clear();
543 mStateRaster.deinit(this);
544 mStateVertex.deinit(this);
545 mStateFragment.deinit(this);
546 mStateFragmentStore.deinit(this);
547 mStateFont.deinit(this);
Jason Sams2cbfc4c2011-01-05 03:37:48 -0800548 mShaderCache.cleanupAll();
Jason Sams741aac92010-12-24 14:38:39 -0800549 }
Jason Sams249d4532011-01-23 17:48:45 -0800550 //LOGV("destroyWorkerThreadResources 2");
Jason Samscf912de2011-01-09 16:09:51 -0800551 mExit = true;
Jason Sams741aac92010-12-24 14:38:39 -0800552}
553
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800554void * Context::helperThreadProc(void *vrsc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700555 Context *rsc = static_cast<Context *>(vrsc);
556 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
557
Jason Sams249d4532011-01-23 17:48:45 -0800558 //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700559
560 rsc->mWorkers.mLaunchSignals[idx].init();
561 rsc->mWorkers.mNativeThreadId[idx] = gettid();
562
Jason Sams8d957fa2010-09-28 14:41:22 -0700563#if 0
564 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
565 cpu_set_t cpuset;
566 memset(&cpuset, 0, sizeof(cpuset));
567 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
568 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700569 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700570 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
571#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700572
573 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700574 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
575 if (status) {
576 LOGE("pthread_setspecific %i", status);
577 }
578
Jason Sams249d4532011-01-23 17:48:45 -0800579 while (!rsc->mExit) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700580 rsc->mWorkers.mLaunchSignals[idx].wait();
581 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700582 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
583 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700584 android_atomic_dec(&rsc->mWorkers.mRunningCount);
585 rsc->mWorkers.mCompleteSignal.set();
586 }
Jason Sams18133402010-07-20 15:09:00 -0700587
Jason Sams249d4532011-01-23 17:48:45 -0800588 //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700589 return NULL;
590}
591
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800592void Context::launchThreads(WorkerCallback_t cbk, void *data) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700593 mWorkers.mLaunchData = data;
594 mWorkers.mLaunchCallback = cbk;
Stephen Hines4f947d72011-03-08 16:49:28 -0800595 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700596 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
597 mWorkers.mLaunchSignals[ct].set();
598 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800599 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700600 mWorkers.mCompleteSignal.wait();
601 }
602}
603
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800604void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800605 // Note: If we put this in the proper "background" policy
606 // the wallpapers can become completly unresponsive at times.
607 // This is probably not what we want for something the user is actively
608 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800609 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800610#if 0
611 SchedPolicy pol = SP_FOREGROUND;
612 if (p > 0) {
613 pol = SP_BACKGROUND;
614 }
615 if (!set_sched_policy(mNativeThreadId, pol)) {
616 // success; reset the priority as well
617 }
618#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700619 setpriority(PRIO_PROCESS, mNativeThreadId, p);
620 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
621 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
622 }
Jason Sams15832442009-11-15 12:14:26 -0800623#endif
624}
625
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800626Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700627 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700628 mRunning = false;
629 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700630 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700631 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800632 mError = RS_ERROR_NONE;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700633 mDPI = 96;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700634}
635
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800636Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700637 Context * rsc = new Context();
638 if (!rsc->initContext(dev, sc)) {
639 delete rsc;
640 return NULL;
641 }
642 return rsc;
643}
644
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800645bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700646 pthread_mutex_lock(&gInitMutex);
647
648 dev->addContext(this);
649 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700650 if (sc) {
651 mUserSurfaceConfig = *sc;
652 } else {
653 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
654 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800655
Jason Sams613cad12009-11-12 15:10:25 -0800656 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800657 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700658 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700659
Jason Samsa658e902009-06-04 14:35:01 -0700660 int status;
661 pthread_attr_t threadAttr;
662
Jason Samsfb03a222009-10-15 16:47:31 -0700663 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700664 status = pthread_key_create(&gThreadTLSKey, NULL);
665 if (status) {
666 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700667 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700668 return false;
Jason Sams9e4e13d2009-10-06 17:16:55 -0700669 }
Jason Samse5769102009-06-19 16:03:18 -0700670 }
Jason Samsfb03a222009-10-15 16:47:31 -0700671 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700672
Jason Samsfb03a222009-10-15 16:47:31 -0700673 pthread_mutex_unlock(&gInitMutex);
674
675 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700676
Jason Samsa658e902009-06-04 14:35:01 -0700677 status = pthread_attr_init(&threadAttr);
678 if (status) {
679 LOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700680 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700681 }
682
Jason Sams613cad12009-11-12 15:10:25 -0800683 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700684
Jason Sams24371d92009-08-19 12:17:14 -0700685 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700686 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700687
Jason Sams7c7c78a2010-07-26 17:12:55 -0700688 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
689 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
690 if (cpu < 2) cpu = 0;
691
692 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700693 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
694 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
695 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
696 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700697 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700698 if (status) {
699 LOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700700 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700701 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800702 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700703 usleep(100);
704 }
705
Jason Sams5c1c79a2010-11-03 14:27:11 -0700706 if (mError != RS_ERROR_NONE) {
707 return false;
708 }
709
Jason Sams8d957fa2010-09-28 14:41:22 -0700710 mWorkers.mCompleteSignal.init();
Stephen Hines4f947d72011-03-08 16:49:28 -0800711 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
712 android_atomic_release_store(0, &mWorkers.mLaunchCount);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700713 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
714 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
715 if (status) {
716 mWorkers.mCount = ct;
717 LOGE("Created fewer than expected number of RS threads.");
718 break;
719 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700720 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800721 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) {
722 usleep(100);
723 }
Jason Samsa658e902009-06-04 14:35:01 -0700724 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700725 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700726}
727
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800728Context::~Context() {
Jason Sams8c0ee652009-08-25 14:49:07 -0700729 LOGV("Context::~Context");
Jason Samscf912de2011-01-09 16:09:51 -0800730
731 mIO.mToCore.flush();
732 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700733 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700734 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700735 void *res;
736
Jason Sams8c0ee652009-08-25 14:49:07 -0700737 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700738 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700739
Jason Sams249d4532011-01-23 17:48:45 -0800740 // Cleanup compute threads.
741 mWorkers.mLaunchData = NULL;
742 mWorkers.mLaunchCallback = NULL;
Stephen Hines4f947d72011-03-08 16:49:28 -0800743 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
Jason Sams249d4532011-01-23 17:48:45 -0800744 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
745 mWorkers.mLaunchSignals[ct].set();
Jason Sams51462c52011-01-25 00:26:25 -0800746 }
747 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
Stephen Hines196c1112011-03-01 17:34:59 -0800748 status = pthread_join(mWorkers.mThreadId[ct], &res);
Jason Sams249d4532011-01-23 17:48:45 -0800749 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800750 rsAssert(android_atomic_acquire_load(&mWorkers.mRunningCount) == 0);
Jason Sams249d4532011-01-23 17:48:45 -0800751
Jason Samsfb03a222009-10-15 16:47:31 -0700752 // Global structure cleanup.
753 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700754 if (mDev) {
755 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700756 --gThreadTLSKeyCount;
757 if (!gThreadTLSKeyCount) {
758 pthread_key_delete(gThreadTLSKey);
759 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800760 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700761 }
Jason Samsfb03a222009-10-15 16:47:31 -0700762 pthread_mutex_unlock(&gInitMutex);
Jason Sams741aac92010-12-24 14:38:39 -0800763 LOGV("Context::~Context done");
Jason Sams326e0dd2009-05-22 14:03:28 -0700764}
765
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800766void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800767 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800768
Jason Sams458f2dc2009-11-03 13:58:36 -0800769 EGLBoolean ret;
Jason Sams52a67b22011-01-12 15:26:25 -0800770 // WAR: Some drivers fail to handle 0 size surfaces correcntly.
771 // Use the pbuffer to avoid this pitfall.
772 if ((mEGL.mSurface != NULL) || (w == 0) || (h == 0)) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700773 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800774 checkEglError("eglMakeCurrent", ret);
775
776 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
777 checkEglError("eglDestroySurface", ret);
778
779 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700780 mWidth = 1;
781 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800782 }
783
784 mWndSurface = sur;
785 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700786 mWidth = w;
787 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800788
Jason Sams458f2dc2009-11-03 13:58:36 -0800789 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
790 checkEglError("eglCreateWindowSurface");
791 if (mEGL.mSurface == EGL_NO_SURFACE) {
792 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
793 }
794
795 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
796 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800797
Jason Sams771565f2010-05-14 15:30:29 -0700798 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800799 }
800}
801
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800802void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800803 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700804 mPaused = true;
805}
806
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800807void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800808 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700809 mPaused = false;
810}
811
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800812void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800813 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700814 mRootScript.set(s);
815}
816
Jason Sams60709252010-11-17 15:29:32 -0800817void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800818 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700819 if (pfs == NULL) {
820 mFragmentStore.set(mStateFragmentStore.mDefault);
821 } else {
822 mFragmentStore.set(pfs);
823 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700824}
825
Jason Sams60709252010-11-17 15:29:32 -0800826void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800827 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700828 if (pf == NULL) {
829 mFragment.set(mStateFragment.mDefault);
830 } else {
831 mFragment.set(pf);
832 }
Jason Samscfb1d112009-08-05 13:57:03 -0700833}
834
Jason Sams60709252010-11-17 15:29:32 -0800835void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800836 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700837 if (pr == NULL) {
838 mRaster.set(mStateRaster.mDefault);
839 } else {
840 mRaster.set(pr);
841 }
842}
843
Jason Sams60709252010-11-17 15:29:32 -0800844void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800845 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700846 if (pv == NULL) {
847 mVertex.set(mStateVertex.mDefault);
848 } else {
849 mVertex.set(pv);
850 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700851}
852
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800853void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700854 rsAssert(mIsGraphicsContext);
855 if (f == NULL) {
856 mFont.set(mStateFont.mDefault);
857 } else {
858 mFont.set(f);
859 }
860}
861
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800862void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700863 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700864 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700865 mNames.add(obj);
866}
867
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800868void Context::removeName(ObjectBase *obj) {
869 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700870 if (obj == mNames[ct]) {
871 mNames.removeAt(ct);
872 return;
873 }
874 }
875}
876
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800877RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800878 *receiveLen = 0;
879 if (!wait && mIO.mToClient.isEmpty()) {
880 return RS_MESSAGE_TO_CLIENT_NONE;
881 }
882
883 uint32_t bytesData = 0;
884 uint32_t commandID = 0;
885 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
886 *receiveLen = bytesData - sizeof(uint32_t);
887 if (bytesData) {
888 *subID = d[0];
889 }
890 return (RsMessageToClientType)commandID;
891}
892
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800893RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700894 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700895 *receiveLen = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800896 if (!wait && mIO.mToClient.isEmpty()) {
897 return RS_MESSAGE_TO_CLIENT_NONE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700898 }
899
900 //LOGE("getMessageToClient 2 con=%p", this);
901 uint32_t bytesData = 0;
902 uint32_t commandID = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800903 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
Jason Sams8c401ef2009-10-06 13:58:47 -0700904 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
905
Jason Samsaad4bc52010-11-08 17:06:46 -0800906 *receiveLen = bytesData - sizeof(uint32_t);
907 *subID = d[0];
908
909 //LOGE("getMessageToClient %i %i", commandID, *subID);
Jason Sams8c401ef2009-10-06 13:58:47 -0700910 if (bufferLen >= bytesData) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800911 memcpy(data, d+1, *receiveLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700912 mIO.mToClient.next();
Jason Samsaad4bc52010-11-08 17:06:46 -0800913 return (RsMessageToClientType)commandID;
Jason Sams8c401ef2009-10-06 13:58:47 -0700914 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800915 return RS_MESSAGE_TO_CLIENT_RESIZE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700916}
917
Jason Sams87319de2010-11-22 16:20:16 -0800918bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
919 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsaad4bc52010-11-08 17:06:46 -0800920 //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700921 if (cmdID == 0) {
922 LOGE("Attempting to send invalid command 0 to client.");
923 return false;
924 }
925 if (!waitForSpace) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800926 if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700927 // Not enough room, and not waiting.
928 return false;
929 }
930 }
931 //LOGE("sendMessageToClient 2");
Jason Samsaad4bc52010-11-08 17:06:46 -0800932 uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
933 p[0] = subID;
Jason Samsef5867a2010-07-28 11:17:53 -0700934 if (len > 0) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800935 memcpy(p+1, data, len);
Jason Samsef5867a2010-07-28 11:17:53 -0700936 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800937 mIO.mToClient.commit(cmdID, len + sizeof(subID));
Jason Sams8c401ef2009-10-06 13:58:47 -0700938 //LOGE("sendMessageToClient 3");
939 return true;
940}
941
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800942void Context::initToClient() {
943 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700944 usleep(100);
945 }
946}
947
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800948void Context::deinitToClient() {
Jason Sams8c401ef2009-10-06 13:58:47 -0700949 mIO.mToClient.shutdown();
950}
Jason Sams50869382009-08-18 17:07:09 -0700951
Jason Sams87319de2010-11-22 16:20:16 -0800952void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800953 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800954 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800955}
956
957
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800958void Context::dumpDebug() const {
Jason Sams13e26342009-11-24 12:26:35 -0800959 LOGE("RS Context debug %p", this);
960 LOGE("RS Context debug");
961
962 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700963 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800964 LOGE(" GL vendor: %s", mGL.mVendor);
965 LOGE(" GL renderer: %s", mGL.mRenderer);
966 LOGE(" GL Version: %s", mGL.mVersion);
967 LOGE(" GL Extensions: %s", mGL.mExtensions);
968 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
969 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700970 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800971 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
972
Jason Sams4815c0d2009-12-15 12:58:36 -0800973 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
974 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
975 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
976 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800977}
Jason Samsa4a54e42009-06-10 18:39:40 -0700978
Jason Sams326e0dd2009-05-22 14:03:28 -0700979///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700980//
Jason Sams326e0dd2009-05-22 14:03:28 -0700981
982namespace android {
983namespace renderscript {
984
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800985void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700986}
Jason Sams326e0dd2009-05-22 14:03:28 -0700987
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800988void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700989 Script *s = static_cast<Script *>(vs);
990 rsc->setRootScript(s);
991}
992
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800993void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700994 Sampler *s = static_cast<Sampler *>(vs);
995
996 if (slot > RS_MAX_SAMPLER_SLOT) {
997 LOGE("Invalid sampler slot");
998 return;
999 }
1000
1001 s->bindToContext(&rsc->mStateSampler, slot);
1002}
1003
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001004void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -07001005 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -08001006 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -07001007}
1008
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001009void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001010 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -08001011 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -07001012}
1013
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001014void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -07001015 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -08001016 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -07001017}
1018
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001019void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001020 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -08001021 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -07001022}
1023
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001024void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001025 Font *font = static_cast<Font *>(vfont);
1026 rsc->setFont(font);
1027}
1028
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001029void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001030 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -07001031 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001032}
Jason Sams326e0dd2009-05-22 14:03:28 -07001033
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001034void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -07001035 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -07001036 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -07001037 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -07001038}
1039
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001040void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001041 rsc->pause();
1042}
1043
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001044void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001045 rsc->resume();
1046}
1047
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001048void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -08001049 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -08001050}
1051
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001052void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -08001053 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001054}
1055
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001056void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -08001057 ObjectBase::dumpAll(rsc);
1058}
1059
Jason Sams741aac92010-12-24 14:38:39 -08001060void rsi_ContextDestroyWorker(Context *rsc) {
Jason Sams741aac92010-12-24 14:38:39 -08001061 rsc->destroyWorkerThreadResources();;
Jason Sams741aac92010-12-24 14:38:39 -08001062}
1063
1064}
1065}
1066
1067void rsContextDestroy(RsContext vcon) {
1068 LOGV("rsContextDestroy %p", vcon);
1069 Context *rsc = static_cast<Context *>(vcon);
1070 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -08001071 delete rsc;
Jason Sams741aac92010-12-24 14:38:39 -08001072 LOGV("rsContextDestroy 2 %p", vcon);
Jason Sams1dcefab2010-12-09 12:19:46 -08001073}
1074
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001075RsContext rsContextCreate(RsDevice vdev, uint32_t version) {
Jason Sams4820e8b2010-02-09 16:05:07 -08001076 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001077 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001078 Context *rsc = Context::createContext(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001079 return rsc;
1080}
1081
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -07001082RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
1083 RsSurfaceConfig sc, uint32_t dpi) {
Jason Sams6b8552a2010-10-13 15:31:10 -07001084 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001085 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001086 Context *rsc = Context::createContext(dev, &sc);
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -07001087 rsc->setDPI(dpi);
Jason Sams900f1612010-09-16 18:18:29 -07001088 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001089 return rsc;
1090}
1091
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001092RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001093 Context * rsc = static_cast<Context *>(vrsc);
Jason Samsaad4bc52010-11-08 17:06:46 -08001094 return rsc->peekMessageToClient(receiveLen, subID, wait);
1095}
1096
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001097RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -08001098 Context * rsc = static_cast<Context *>(vrsc);
1099 return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait);
Jason Sams8c401ef2009-10-06 13:58:47 -07001100}
1101
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001102void rsContextInitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001103 Context * rsc = static_cast<Context *>(vrsc);
1104 rsc->initToClient();
1105}
1106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001107void rsContextDeinitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001108 Context * rsc = static_cast<Context *>(vrsc);
1109 rsc->deinitToClient();
1110}
1111
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001112// Only to be called at a3d load time, before object is visible to user
1113// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001114void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001115 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1116 (*name) = ob->getName();
1117}