blob: 11d0bdce8784816015858f3d2fd77e3aac66d197 [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;
273 mFragment.set(con->getProgramFragment());
274 mVertex.set(con->getProgramVertex());
275 mStore.set(con->getProgramStore());
276 mRaster.set(con->getProgramRaster());
Alex Sakhartchouk5c0912b2010-11-18 16:36:47 -0800277 mFont.set(con->getFont());
Jason Sams60709252010-11-17 15:29:32 -0800278}
279
280Context::PushState::~PushState() {
281 mRsc->setProgramFragment(mFragment.get());
282 mRsc->setProgramVertex(mVertex.get());
283 mRsc->setProgramStore(mStore.get());
284 mRsc->setProgramRaster(mRaster.get());
285 mRsc->setFont(mFont.get());
286}
287
Jason Sams33b6e3b2009-10-27 14:44:31 -0700288
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800289uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -0800290 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -0700291
Jason Samsc61346b2010-05-28 18:23:22 -0700292 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700293 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700294}
295
Jason Sams87319de2010-11-22 16:20:16 -0800296void Context::checkError(const char *msg, bool isFatal) const {
297
Jason Samsd01d9702009-12-23 14:35:29 -0800298 GLenum err = glGetError();
299 if (err != GL_NO_ERROR) {
Jason Sams87319de2010-11-22 16:20:16 -0800300 char buf[1024];
301 snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
302
303 if (isFatal) {
304 setError(RS_ERROR_FATAL_DRIVER, buf);
305 } else {
306 switch (err) {
307 case GL_OUT_OF_MEMORY:
308 setError(RS_ERROR_OUT_OF_MEMORY, buf);
309 break;
310 default:
311 setError(RS_ERROR_DRIVER, buf);
312 break;
313 }
314 }
315
316 LOGE("%p, %s", this, buf);
Jason Samsd01d9702009-12-23 14:35:29 -0800317 }
318}
Jason Sams10308932009-06-09 12:15:30 -0700319
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800320uint32_t Context::runRootScript() {
Jason Sams771565f2010-05-14 15:30:29 -0700321 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700322
Jason Sams2dca84d2009-12-09 11:05:45 -0800323 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700324 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700325 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700326
Jason Samsd01d9702009-12-23 14:35:29 -0800327 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700328 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700329}
330
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800331uint64_t Context::getTime() const {
Jason Sams24371d92009-08-19 12:17:14 -0700332 struct timespec t;
333 clock_gettime(CLOCK_MONOTONIC, &t);
334 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
335}
336
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800337void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700338 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
339 mTimers[ct] = 0;
340 }
341}
342
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800343void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700344 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700345 mTimeFrame = mTimeLast;
346 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700347 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700348 mAverageFPSFrameCount = 0;
349 mAverageFPSStartTime = mTimeLast;
350 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700351 timerReset();
352}
353
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800354void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700355 mTimeLastFrame = mTimeFrame;
356 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700357 // Update average fps
358 const uint64_t averageFramerateInterval = 1000 * 1000000;
359 mAverageFPSFrameCount ++;
360 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800361 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700362 inverval = inverval / 1000000;
363 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
364 mAverageFPSFrameCount = 0;
365 mAverageFPSStartTime = mTimeFrame;
366 }
Jason Sams1d54f102009-09-03 15:43:13 -0700367}
368
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800369void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700370 uint64_t last = mTimeLast;
371 mTimeLast = getTime();
372 mTimers[mTimerActive] += mTimeLast - last;
373 mTimerActive = tm;
374}
375
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800376void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700377 double total = 0;
378 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
379 total += mTimers[ct];
380 }
Jason Sams1d54f102009-09-03 15:43:13 -0700381 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800382 mTimeMSLastFrame = frame / 1000000;
383 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
384 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700385
Jason Sams2dca84d2009-12-09 11:05:45 -0800386
387 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700388 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 -0800389 mTimeMSLastFrame,
390 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
391 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
392 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700393 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
394 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800395 }
Jason Sams24371d92009-08-19 12:17:14 -0700396}
397
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800398bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700399 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
400 LOGE("Context::setupCheck() 1 fail");
401 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800402 }
Jason Sams900f1612010-09-16 18:18:29 -0700403
404 mFragmentStore->setupGL2(this, &mStateFragmentStore);
405 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
406 mRaster->setupGL2(this, &mStateRaster);
407 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800408 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700409}
410
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700411void Context::setupProgramStore() {
412 mFragmentStore->setupGL2(this, &mStateFragmentStore);
413}
414
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800415static bool getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700416 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700417 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700418 return 0 != strcmp(buf, "0");
419}
Jason Sams326e0dd2009-05-22 14:03:28 -0700420
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800421void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700422 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700423 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700424 float oldR, oldG, oldB, oldA;
425 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700426 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700427
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700428 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700429 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700430 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700431
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700432 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700433 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700434
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700435 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700436}
437
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800438void * Context::threadProc(void *vrsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700439 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800440 rsc->mNativeThreadId = gettid();
441
442 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800443 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700444
Jason Sams1fddd902009-09-25 15:25:00 -0700445 rsc->props.mLogTimes = getProp("debug.rs.profile");
446 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800447 rsc->props.mLogObjects = getProp("debug.rs.object");
448 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700449 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
450 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700451 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700452
Jason Sams605048a2010-09-30 18:15:52 -0700453 rsc->mTlsStruct = new ScriptTLSStruct;
454 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700455 LOGE("Error allocating tls storage");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700456 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
Jason Samse5769102009-06-19 16:03:18 -0700457 return NULL;
458 }
Jason Sams605048a2010-09-30 18:15:52 -0700459 rsc->mTlsStruct->mContext = rsc;
460 rsc->mTlsStruct->mScript = NULL;
461 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700462 if (status) {
463 LOGE("pthread_setspecific %i", status);
464 }
465
Jason Sams5c1c79a2010-11-03 14:27:11 -0700466 if (!rsc->initGLThread()) {
467 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
468 return NULL;
469 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700470
Jason Sams4820e8b2010-02-09 16:05:07 -0800471 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700472 rsc->mStateRaster.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800473 rsc->setProgramRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700474 rsc->mStateVertex.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800475 rsc->setProgramVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700476 rsc->mStateFragment.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800477 rsc->setProgramFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700478 rsc->mStateFragmentStore.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800479 rsc->setProgramStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700480 rsc->mStateFont.init(rsc);
481 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800482 rsc->mStateVertexArray.init(rsc);
483 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700484
Jason Sams326e0dd2009-05-22 14:03:28 -0700485 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700486 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700487 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700488 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700489 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800490 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700491
Jason Sams2dca84d2009-12-09 11:05:45 -0800492 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800493 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800494 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700495
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800496 if (rsc->props.mLogVisual) {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700497 rsc->displayDebugStats();
498 }
499
Jason Sams2dca84d2009-12-09 11:05:45 -0800500 mDraw = targetTime && !rsc->mPaused;
501 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700502 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800503 rsc->timerFrame();
504 rsc->timerSet(RS_TIMER_INTERNAL);
505 rsc->timerPrint();
506 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700507 }
Jason Sams177f8442010-10-29 10:19:21 -0700508 if (targetTime > 1) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800509 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
510 if (t > 0) {
511 usleep(t);
512 }
513 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700514 }
515
Jason Sams4c5f99e2010-09-14 14:59:03 -0700516 LOGV("%p, RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700517
Jason Sams4820e8b2010-02-09 16:05:07 -0800518 if (rsc->mIsGraphicsContext) {
519 pthread_mutex_lock(&gInitMutex);
520 rsc->deinitEGL();
521 pthread_mutex_unlock(&gInitMutex);
522 }
Jason Sams605048a2010-09-30 18:15:52 -0700523 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700524
Jason Sams4c5f99e2010-09-14 14:59:03 -0700525 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700526 return NULL;
527}
528
Jason Sams741aac92010-12-24 14:38:39 -0800529void Context::destroyWorkerThreadResources() {
Jason Sams249d4532011-01-23 17:48:45 -0800530 //LOGV("destroyWorkerThreadResources 1");
Jason Sams741aac92010-12-24 14:38:39 -0800531 if (mIsGraphicsContext) {
532 mRaster.clear();
533 mFragment.clear();
534 mVertex.clear();
535 mFragmentStore.clear();
536 mFont.clear();
537 mRootScript.clear();
538 mStateRaster.deinit(this);
539 mStateVertex.deinit(this);
540 mStateFragment.deinit(this);
541 mStateFragmentStore.deinit(this);
542 mStateFont.deinit(this);
Jason Sams2cbfc4c2011-01-05 03:37:48 -0800543 mShaderCache.cleanupAll();
Jason Sams741aac92010-12-24 14:38:39 -0800544 }
545 ObjectBase::zeroAllUserRef(this);
Jason Sams249d4532011-01-23 17:48:45 -0800546 //LOGV("destroyWorkerThreadResources 2");
Jason Samscf912de2011-01-09 16:09:51 -0800547 mExit = true;
Jason Sams741aac92010-12-24 14:38:39 -0800548}
549
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800550void * Context::helperThreadProc(void *vrsc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700551 Context *rsc = static_cast<Context *>(vrsc);
552 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
553
Jason Sams249d4532011-01-23 17:48:45 -0800554 //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700555
556 rsc->mWorkers.mLaunchSignals[idx].init();
557 rsc->mWorkers.mNativeThreadId[idx] = gettid();
558
Jason Sams8d957fa2010-09-28 14:41:22 -0700559#if 0
560 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
561 cpu_set_t cpuset;
562 memset(&cpuset, 0, sizeof(cpuset));
563 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
564 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700565 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700566 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
567#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700568
569 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700570 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
571 if (status) {
572 LOGE("pthread_setspecific %i", status);
573 }
574
Jason Sams249d4532011-01-23 17:48:45 -0800575 while (!rsc->mExit) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700576 rsc->mWorkers.mLaunchSignals[idx].wait();
577 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700578 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
579 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700580 android_atomic_dec(&rsc->mWorkers.mRunningCount);
581 rsc->mWorkers.mCompleteSignal.set();
582 }
Jason Sams18133402010-07-20 15:09:00 -0700583
Jason Sams249d4532011-01-23 17:48:45 -0800584 //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700585 return NULL;
586}
587
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800588void Context::launchThreads(WorkerCallback_t cbk, void *data) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700589 mWorkers.mLaunchData = data;
590 mWorkers.mLaunchCallback = cbk;
591 mWorkers.mRunningCount = (int)mWorkers.mCount;
592 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
593 mWorkers.mLaunchSignals[ct].set();
594 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800595 while (mWorkers.mRunningCount) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700596 mWorkers.mCompleteSignal.wait();
597 }
598}
599
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800600void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800601 // Note: If we put this in the proper "background" policy
602 // the wallpapers can become completly unresponsive at times.
603 // This is probably not what we want for something the user is actively
604 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800605 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800606#if 0
607 SchedPolicy pol = SP_FOREGROUND;
608 if (p > 0) {
609 pol = SP_BACKGROUND;
610 }
611 if (!set_sched_policy(mNativeThreadId, pol)) {
612 // success; reset the priority as well
613 }
614#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700615 setpriority(PRIO_PROCESS, mNativeThreadId, p);
616 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
617 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
618 }
Jason Sams15832442009-11-15 12:14:26 -0800619#endif
620}
621
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800622Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700623 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700624 mRunning = false;
625 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700626 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700627 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800628 mError = RS_ERROR_NONE;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700629}
630
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800631Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700632 Context * rsc = new Context();
633 if (!rsc->initContext(dev, sc)) {
634 delete rsc;
635 return NULL;
636 }
637 return rsc;
638}
639
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800640bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700641 pthread_mutex_lock(&gInitMutex);
642
643 dev->addContext(this);
644 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700645 if (sc) {
646 mUserSurfaceConfig = *sc;
647 } else {
648 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
649 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800650
Jason Sams613cad12009-11-12 15:10:25 -0800651 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800652 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700653 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700654
Jason Samsa658e902009-06-04 14:35:01 -0700655 int status;
656 pthread_attr_t threadAttr;
657
Jason Samsfb03a222009-10-15 16:47:31 -0700658 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700659 status = pthread_key_create(&gThreadTLSKey, NULL);
660 if (status) {
661 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700662 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700663 return false;
Jason Sams9e4e13d2009-10-06 17:16:55 -0700664 }
Jason Samse5769102009-06-19 16:03:18 -0700665 }
Jason Samsfb03a222009-10-15 16:47:31 -0700666 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700667
Jason Samsfb03a222009-10-15 16:47:31 -0700668 pthread_mutex_unlock(&gInitMutex);
669
670 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700671
Jason Samsa658e902009-06-04 14:35:01 -0700672 status = pthread_attr_init(&threadAttr);
673 if (status) {
674 LOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700675 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700676 }
677
Jason Sams613cad12009-11-12 15:10:25 -0800678 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700679
Jason Sams24371d92009-08-19 12:17:14 -0700680 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700681 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700682
Jason Sams7c7c78a2010-07-26 17:12:55 -0700683 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
684 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
685 if (cpu < 2) cpu = 0;
686
687 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700688 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
689 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
690 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
691 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700692 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700693 if (status) {
694 LOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700695 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700696 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800697 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700698 usleep(100);
699 }
700
Jason Sams5c1c79a2010-11-03 14:27:11 -0700701 if (mError != RS_ERROR_NONE) {
702 return false;
703 }
704
Jason Sams8d957fa2010-09-28 14:41:22 -0700705 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700706 mWorkers.mRunningCount = 0;
707 mWorkers.mLaunchCount = 0;
708 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
709 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
710 if (status) {
711 mWorkers.mCount = ct;
712 LOGE("Created fewer than expected number of RS threads.");
713 break;
714 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700715 }
Jason Samsa658e902009-06-04 14:35:01 -0700716 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700717 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700718}
719
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800720Context::~Context() {
Jason Sams8c0ee652009-08-25 14:49:07 -0700721 LOGV("Context::~Context");
Jason Samscf912de2011-01-09 16:09:51 -0800722
723 mIO.mToCore.flush();
724 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700725 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700726 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700727 void *res;
728
Jason Sams8c0ee652009-08-25 14:49:07 -0700729 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700730 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700731
Jason Sams249d4532011-01-23 17:48:45 -0800732 // Cleanup compute threads.
733 mWorkers.mLaunchData = NULL;
734 mWorkers.mLaunchCallback = NULL;
735 mWorkers.mRunningCount = (int)mWorkers.mCount;
736 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
737 mWorkers.mLaunchSignals[ct].set();
738 int status = pthread_join(mWorkers.mThreadId[ct], &res);
739 }
740 rsAssert(!mWorkers.mRunningCount);
741
Jason Samsfb03a222009-10-15 16:47:31 -0700742 // Global structure cleanup.
743 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700744 if (mDev) {
745 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700746 --gThreadTLSKeyCount;
747 if (!gThreadTLSKeyCount) {
748 pthread_key_delete(gThreadTLSKey);
749 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800750 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700751 }
Jason Samsfb03a222009-10-15 16:47:31 -0700752 pthread_mutex_unlock(&gInitMutex);
Jason Sams741aac92010-12-24 14:38:39 -0800753 LOGV("Context::~Context done");
Jason Sams326e0dd2009-05-22 14:03:28 -0700754}
755
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800756void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800757 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800758
Jason Sams458f2dc2009-11-03 13:58:36 -0800759 EGLBoolean ret;
Jason Sams52a67b22011-01-12 15:26:25 -0800760 // WAR: Some drivers fail to handle 0 size surfaces correcntly.
761 // Use the pbuffer to avoid this pitfall.
762 if ((mEGL.mSurface != NULL) || (w == 0) || (h == 0)) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700763 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800764 checkEglError("eglMakeCurrent", ret);
765
766 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
767 checkEglError("eglDestroySurface", ret);
768
769 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700770 mWidth = 1;
771 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800772 }
773
774 mWndSurface = sur;
775 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700776 mWidth = w;
777 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800778
Jason Sams458f2dc2009-11-03 13:58:36 -0800779 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
780 checkEglError("eglCreateWindowSurface");
781 if (mEGL.mSurface == EGL_NO_SURFACE) {
782 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
783 }
784
785 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
786 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800787
Jason Sams771565f2010-05-14 15:30:29 -0700788 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800789 }
790}
791
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800792void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800793 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700794 mPaused = true;
795}
796
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800797void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800798 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700799 mPaused = false;
800}
801
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800802void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800803 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700804 mRootScript.set(s);
805}
806
Jason Sams60709252010-11-17 15:29:32 -0800807void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800808 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700809 if (pfs == NULL) {
810 mFragmentStore.set(mStateFragmentStore.mDefault);
811 } else {
812 mFragmentStore.set(pfs);
813 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700814}
815
Jason Sams60709252010-11-17 15:29:32 -0800816void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800817 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700818 if (pf == NULL) {
819 mFragment.set(mStateFragment.mDefault);
820 } else {
821 mFragment.set(pf);
822 }
Jason Samscfb1d112009-08-05 13:57:03 -0700823}
824
Jason Sams60709252010-11-17 15:29:32 -0800825void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800826 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700827 if (pr == NULL) {
828 mRaster.set(mStateRaster.mDefault);
829 } else {
830 mRaster.set(pr);
831 }
832}
833
Jason Sams60709252010-11-17 15:29:32 -0800834void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800835 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700836 if (pv == NULL) {
837 mVertex.set(mStateVertex.mDefault);
838 } else {
839 mVertex.set(pv);
840 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700841}
842
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800843void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700844 rsAssert(mIsGraphicsContext);
845 if (f == NULL) {
846 mFont.set(mStateFont.mDefault);
847 } else {
848 mFont.set(f);
849 }
850}
851
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800852void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700853 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700854 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700855 mNames.add(obj);
856}
857
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800858void Context::removeName(ObjectBase *obj) {
859 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700860 if (obj == mNames[ct]) {
861 mNames.removeAt(ct);
862 return;
863 }
864 }
865}
866
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800867RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800868 *receiveLen = 0;
869 if (!wait && mIO.mToClient.isEmpty()) {
870 return RS_MESSAGE_TO_CLIENT_NONE;
871 }
872
873 uint32_t bytesData = 0;
874 uint32_t commandID = 0;
875 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
876 *receiveLen = bytesData - sizeof(uint32_t);
877 if (bytesData) {
878 *subID = d[0];
879 }
880 return (RsMessageToClientType)commandID;
881}
882
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800883RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700884 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700885 *receiveLen = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800886 if (!wait && mIO.mToClient.isEmpty()) {
887 return RS_MESSAGE_TO_CLIENT_NONE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700888 }
889
890 //LOGE("getMessageToClient 2 con=%p", this);
891 uint32_t bytesData = 0;
892 uint32_t commandID = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800893 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
Jason Sams8c401ef2009-10-06 13:58:47 -0700894 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
895
Jason Samsaad4bc52010-11-08 17:06:46 -0800896 *receiveLen = bytesData - sizeof(uint32_t);
897 *subID = d[0];
898
899 //LOGE("getMessageToClient %i %i", commandID, *subID);
Jason Sams8c401ef2009-10-06 13:58:47 -0700900 if (bufferLen >= bytesData) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800901 memcpy(data, d+1, *receiveLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700902 mIO.mToClient.next();
Jason Samsaad4bc52010-11-08 17:06:46 -0800903 return (RsMessageToClientType)commandID;
Jason Sams8c401ef2009-10-06 13:58:47 -0700904 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800905 return RS_MESSAGE_TO_CLIENT_RESIZE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700906}
907
Jason Sams87319de2010-11-22 16:20:16 -0800908bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
909 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsaad4bc52010-11-08 17:06:46 -0800910 //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700911 if (cmdID == 0) {
912 LOGE("Attempting to send invalid command 0 to client.");
913 return false;
914 }
915 if (!waitForSpace) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800916 if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700917 // Not enough room, and not waiting.
918 return false;
919 }
920 }
921 //LOGE("sendMessageToClient 2");
Jason Samsaad4bc52010-11-08 17:06:46 -0800922 uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
923 p[0] = subID;
Jason Samsef5867a2010-07-28 11:17:53 -0700924 if (len > 0) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800925 memcpy(p+1, data, len);
Jason Samsef5867a2010-07-28 11:17:53 -0700926 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800927 mIO.mToClient.commit(cmdID, len + sizeof(subID));
Jason Sams8c401ef2009-10-06 13:58:47 -0700928 //LOGE("sendMessageToClient 3");
929 return true;
930}
931
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800932void Context::initToClient() {
933 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700934 usleep(100);
935 }
936}
937
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800938void Context::deinitToClient() {
Jason Sams8c401ef2009-10-06 13:58:47 -0700939 mIO.mToClient.shutdown();
940}
Jason Sams50869382009-08-18 17:07:09 -0700941
Jason Sams87319de2010-11-22 16:20:16 -0800942void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800943 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800944 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800945}
946
947
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800948void Context::dumpDebug() const {
Jason Sams13e26342009-11-24 12:26:35 -0800949 LOGE("RS Context debug %p", this);
950 LOGE("RS Context debug");
951
952 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700953 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800954 LOGE(" GL vendor: %s", mGL.mVendor);
955 LOGE(" GL renderer: %s", mGL.mRenderer);
956 LOGE(" GL Version: %s", mGL.mVersion);
957 LOGE(" GL Extensions: %s", mGL.mExtensions);
958 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
959 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700960 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800961 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
962
Jason Sams4815c0d2009-12-15 12:58:36 -0800963 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
964 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
965 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
966 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800967}
Jason Samsa4a54e42009-06-10 18:39:40 -0700968
Jason Sams326e0dd2009-05-22 14:03:28 -0700969///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700970//
Jason Sams326e0dd2009-05-22 14:03:28 -0700971
972namespace android {
973namespace renderscript {
974
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800975void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700976}
Jason Sams326e0dd2009-05-22 14:03:28 -0700977
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800978void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700979 Script *s = static_cast<Script *>(vs);
980 rsc->setRootScript(s);
981}
982
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800983void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700984 Sampler *s = static_cast<Sampler *>(vs);
985
986 if (slot > RS_MAX_SAMPLER_SLOT) {
987 LOGE("Invalid sampler slot");
988 return;
989 }
990
991 s->bindToContext(&rsc->mStateSampler, slot);
992}
993
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800994void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700995 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800996 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700997}
998
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800999void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001000 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -08001001 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -07001002}
1003
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001004void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -07001005 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -08001006 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -07001007}
1008
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001009void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001010 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -08001011 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -07001012}
1013
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001014void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001015 Font *font = static_cast<Font *>(vfont);
1016 rsc->setFont(font);
1017}
1018
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001019void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001020 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -07001021 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001022}
Jason Sams326e0dd2009-05-22 14:03:28 -07001023
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001024void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -07001025 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -07001026 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -07001027 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -07001028}
1029
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001030void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001031 rsc->pause();
1032}
1033
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001034void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001035 rsc->resume();
1036}
1037
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001038void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -08001039 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -08001040}
1041
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001042void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -08001043 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001044}
1045
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001046void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -08001047 ObjectBase::dumpAll(rsc);
1048}
1049
Jason Sams741aac92010-12-24 14:38:39 -08001050void rsi_ContextDestroyWorker(Context *rsc) {
Jason Sams741aac92010-12-24 14:38:39 -08001051 rsc->destroyWorkerThreadResources();;
Jason Sams741aac92010-12-24 14:38:39 -08001052}
1053
1054}
1055}
1056
1057void rsContextDestroy(RsContext vcon) {
1058 LOGV("rsContextDestroy %p", vcon);
1059 Context *rsc = static_cast<Context *>(vcon);
1060 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -08001061 delete rsc;
Jason Sams741aac92010-12-24 14:38:39 -08001062 LOGV("rsContextDestroy 2 %p", vcon);
Jason Sams1dcefab2010-12-09 12:19:46 -08001063}
1064
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001065RsContext rsContextCreate(RsDevice vdev, uint32_t version) {
Jason Sams4820e8b2010-02-09 16:05:07 -08001066 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001067 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001068 Context *rsc = Context::createContext(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001069 return rsc;
1070}
1071
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001072RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc) {
Jason Sams6b8552a2010-10-13 15:31:10 -07001073 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001074 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001075 Context *rsc = Context::createContext(dev, &sc);
Jason Sams900f1612010-09-16 18:18:29 -07001076 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001077 return rsc;
1078}
1079
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001080RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001081 Context * rsc = static_cast<Context *>(vrsc);
Jason Samsaad4bc52010-11-08 17:06:46 -08001082 return rsc->peekMessageToClient(receiveLen, subID, wait);
1083}
1084
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001085RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -08001086 Context * rsc = static_cast<Context *>(vrsc);
1087 return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait);
Jason Sams8c401ef2009-10-06 13:58:47 -07001088}
1089
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001090void rsContextInitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001091 Context * rsc = static_cast<Context *>(vrsc);
1092 rsc->initToClient();
1093}
1094
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001095void rsContextDeinitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001096 Context * rsc = static_cast<Context *>(vrsc);
1097 rsc->deinitToClient();
1098}
1099
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001100// Only to be called at a3d load time, before object is visible to user
1101// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001102void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001103 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1104 (*name) = ob->getName();
1105}