blob: a7c0180336a29642db3e2236aba977e23628ecef [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Sams6b8552a2010-10-13 15:31:10 -070021#include <ui/PixelFormat.h>
Jason Samsafcb25c2009-08-25 11:34:49 -070022#include <ui/EGLUtils.h>
Mathias Agopian9b97c292010-02-12 12:00:38 -080023#include <ui/egl/android_natives.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Jason Sams15832442009-11-15 12:14:26 -080025#include <sys/types.h>
26#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070027#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080028
Joe Onorato76371ff2009-09-23 16:37:36 -070029#include <cutils/properties.h>
30
Jason Sams1aa5a4e2009-06-22 17:15:15 -070031#include <GLES/gl.h>
32#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080033#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070035
Jason Sams15832442009-11-15 12:14:26 -080036#include <cutils/sched_policy.h>
Jason Sams8d957fa2010-09-28 14:41:22 -070037#include <sys/syscall.h>
38#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080039
Jason Sams326e0dd2009-05-22 14:03:28 -070040using namespace android;
41using namespace android::renderscript;
42
Jason Samse5769102009-06-19 16:03:18 -070043pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070044uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070045uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070046pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080047pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070048
Jason Sams33b6e3b2009-10-27 14:44:31 -070049static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
50 if (returnVal != EGL_TRUE) {
51 fprintf(stderr, "%s() returned %d\n", op, returnVal);
52 }
53
54 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
55 = eglGetError()) {
56 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
57 error);
58 }
59}
60
Jason Sams6b8552a2010-10-13 15:31:10 -070061void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
62
63#define X(VAL) {VAL, #VAL}
64 struct {EGLint attribute; const char* name;} names[] = {
65 X(EGL_BUFFER_SIZE),
66 X(EGL_ALPHA_SIZE),
67 X(EGL_BLUE_SIZE),
68 X(EGL_GREEN_SIZE),
69 X(EGL_RED_SIZE),
70 X(EGL_DEPTH_SIZE),
71 X(EGL_STENCIL_SIZE),
72 X(EGL_CONFIG_CAVEAT),
73 X(EGL_CONFIG_ID),
74 X(EGL_LEVEL),
75 X(EGL_MAX_PBUFFER_HEIGHT),
76 X(EGL_MAX_PBUFFER_PIXELS),
77 X(EGL_MAX_PBUFFER_WIDTH),
78 X(EGL_NATIVE_RENDERABLE),
79 X(EGL_NATIVE_VISUAL_ID),
80 X(EGL_NATIVE_VISUAL_TYPE),
81 X(EGL_SAMPLES),
82 X(EGL_SAMPLE_BUFFERS),
83 X(EGL_SURFACE_TYPE),
84 X(EGL_TRANSPARENT_TYPE),
85 X(EGL_TRANSPARENT_RED_VALUE),
86 X(EGL_TRANSPARENT_GREEN_VALUE),
87 X(EGL_TRANSPARENT_BLUE_VALUE),
88 X(EGL_BIND_TO_TEXTURE_RGB),
89 X(EGL_BIND_TO_TEXTURE_RGBA),
90 X(EGL_MIN_SWAP_INTERVAL),
91 X(EGL_MAX_SWAP_INTERVAL),
92 X(EGL_LUMINANCE_SIZE),
93 X(EGL_ALPHA_MASK_SIZE),
94 X(EGL_COLOR_BUFFER_TYPE),
95 X(EGL_RENDERABLE_TYPE),
96 X(EGL_CONFORMANT),
97 };
98#undef X
99
100 for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
101 EGLint value = -1;
102 EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
103 EGLint error = eglGetError();
104 if (returnVal && error == EGL_SUCCESS) {
105 LOGV(" %s: %d (0x%x)", names[j].name, value, value);
106 }
107 }
108}
109
110
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800111bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -0700112 pthread_mutex_lock(&gInitMutex);
113 LOGV("initGLThread start %p", this);
114
Jason Samsafcb25c2009-08-25 11:34:49 -0700115 mEGL.mNumConfigs = -1;
116 EGLint configAttribs[128];
117 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -0800118 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -0700119
Jason Samsafcb25c2009-08-25 11:34:49 -0700120 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -0700121
Jason Samsafcb25c2009-08-25 11:34:49 -0700122 configAttribsPtr[0] = EGL_SURFACE_TYPE;
123 configAttribsPtr[1] = EGL_WINDOW_BIT;
124 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125
Jason Sams6b8552a2010-10-13 15:31:10 -0700126 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
127 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
128 configAttribsPtr += 2;
Jason Samsc460e552009-11-25 13:22:07 -0800129
Jason Sams6b8552a2010-10-13 15:31:10 -0700130 if (mUserSurfaceConfig.depthMin > 0) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700131 configAttribsPtr[0] = EGL_DEPTH_SIZE;
Jason Sams6b8552a2010-10-13 15:31:10 -0700132 configAttribsPtr[1] = mUserSurfaceConfig.depthMin;
Jason Samsafcb25c2009-08-25 11:34:49 -0700133 configAttribsPtr += 2;
134 }
Jason Sams9397e302009-08-27 20:23:34 -0700135
Jason Sams5fd09d82009-09-23 13:57:02 -0700136 if (mDev->mForceSW) {
137 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
138 configAttribsPtr[1] = EGL_SLOW_CONFIG;
139 configAttribsPtr += 2;
140 }
141
Jason Samsafcb25c2009-08-25 11:34:49 -0700142 configAttribsPtr[0] = EGL_NONE;
143 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -0700144
Jason Sams4c5f99e2010-09-14 14:59:03 -0700145 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700146 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700147 checkEglError("eglGetDisplay");
148
Jason Samsafcb25c2009-08-25 11:34:49 -0700149 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700150 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -0700151
Jason Sams6b8552a2010-10-13 15:31:10 -0700152#if 1
153 PixelFormat pf = PIXEL_FORMAT_RGBA_8888;
154 if (mUserSurfaceConfig.alphaMin == 0) {
155 pf = PIXEL_FORMAT_RGBX_8888;
156 }
157
158 status_t err = EGLUtils::selectConfigForPixelFormat(mEGL.mDisplay, configAttribs, pf, &mEGL.mConfig);
Jason Samsafcb25c2009-08-25 11:34:49 -0700159 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700160 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700161 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700162 if (props.mLogVisual) {
163 printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig);
Jason Samsc460e552009-11-25 13:22:07 -0800164 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700165#else
166 eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
167#endif
168
169 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700170 checkEglError("eglCreateContext");
171 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700172 pthread_mutex_unlock(&gInitMutex);
Jason Sams4c5f99e2010-09-14 14:59:03 -0700173 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700174 return false;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700175 }
176 gGLContextCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700177
178
179 EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
180 mEGL.mSurfaceDefault = eglCreatePbufferSurface(mEGL.mDisplay, mEGL.mConfig, pbuffer_attribs);
181 checkEglError("eglCreatePbufferSurface");
182 if (mEGL.mSurfaceDefault == EGL_NO_SURFACE) {
183 LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700184 pthread_mutex_unlock(&gInitMutex);
185 deinitEGL();
186 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700187 }
188
189 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700190 if (ret == EGL_FALSE) {
191 LOGE("eglMakeCurrent returned EGL_FALSE");
192 checkEglError("eglMakeCurrent", ret);
193 pthread_mutex_unlock(&gInitMutex);
194 deinitEGL();
195 return false;
196 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700197
198 mGL.mVersion = glGetString(GL_VERSION);
199 mGL.mVendor = glGetString(GL_VENDOR);
200 mGL.mRenderer = glGetString(GL_RENDERER);
201 mGL.mExtensions = glGetString(GL_EXTENSIONS);
202
203 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams249d4532011-01-23 17:48:45 -0800204 //LOGV("GL Version %s", mGL.mVersion);
Jason Sams6b8552a2010-10-13 15:31:10 -0700205 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams249d4532011-01-23 17:48:45 -0800206 //LOGV("GL Renderer %s", mGL.mRenderer);
Jason Sams6b8552a2010-10-13 15:31:10 -0700207 //LOGV("GL Extensions %s", mGL.mExtensions);
208
209 const char *verptr = NULL;
210 if (strlen((const char *)mGL.mVersion) > 9) {
211 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
212 verptr = (const char *)mGL.mVersion + 12;
213 }
214 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
215 verptr = (const char *)mGL.mVersion + 9;
216 }
217 }
218
219 if (!verptr) {
220 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700221 pthread_mutex_unlock(&gInitMutex);
222 deinitEGL();
223 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700224 } else {
225 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
226 }
227
228 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
229 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
230 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
231
232 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
233 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
234
235 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
236 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
237
238 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams0f7785c2011-01-13 17:02:35 -0800239 mGL.GL_IMG_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_IMG_texture_npot");
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700240 mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)mGL.mExtensions, "GL_NV_texture_npot_2D_mipmap");
Jason Sams6b8552a2010-10-13 15:31:10 -0700241 mGL.EXT_texture_max_aniso = 1.0f;
242 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800243 if (hasAniso) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700244 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
245 }
246
247 LOGV("initGLThread end %p", this);
248 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700249 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700250}
251
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800252void Context::deinitEGL() {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700253 LOGV("%p, deinitEGL", this);
Jason Sams6b8552a2010-10-13 15:31:10 -0700254
Jason Sams5c1c79a2010-11-03 14:27:11 -0700255 if (mEGL.mContext != EGL_NO_CONTEXT) {
Mathias Agopianb7ee74d2011-01-19 16:42:02 -0800256 eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
257 eglDestroySurface(mEGL.mDisplay, mEGL.mSurfaceDefault);
258 if (mEGL.mSurface != EGL_NO_SURFACE) {
259 eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
260 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700261 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
262 checkEglError("eglDestroyContext");
263 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700264
265 gGLContextCount--;
266 if (!gGLContextCount) {
267 eglTerminate(mEGL.mDisplay);
268 }
269}
270
Jason Sams60709252010-11-17 15:29:32 -0800271Context::PushState::PushState(Context *con) {
272 mRsc = con;
Jason Samsc946b612011-02-23 14:47:17 -0800273 if (con->mIsGraphicsContext) {
274 mFragment.set(con->getProgramFragment());
275 mVertex.set(con->getProgramVertex());
276 mStore.set(con->getProgramStore());
277 mRaster.set(con->getProgramRaster());
278 mFont.set(con->getFont());
279 }
Jason Sams60709252010-11-17 15:29:32 -0800280}
281
282Context::PushState::~PushState() {
Jason Samsc946b612011-02-23 14:47:17 -0800283 if (mRsc->mIsGraphicsContext) {
284 mRsc->setProgramFragment(mFragment.get());
285 mRsc->setProgramVertex(mVertex.get());
286 mRsc->setProgramStore(mStore.get());
287 mRsc->setProgramRaster(mRaster.get());
288 mRsc->setFont(mFont.get());
289 }
Jason Sams60709252010-11-17 15:29:32 -0800290}
291
Jason Sams33b6e3b2009-10-27 14:44:31 -0700292
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800293uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -0800294 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -0700295
Jason Samsc61346b2010-05-28 18:23:22 -0700296 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700297 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700298}
299
Jason Sams87319de2010-11-22 16:20:16 -0800300void Context::checkError(const char *msg, bool isFatal) const {
301
Jason Samsd01d9702009-12-23 14:35:29 -0800302 GLenum err = glGetError();
303 if (err != GL_NO_ERROR) {
Jason Sams87319de2010-11-22 16:20:16 -0800304 char buf[1024];
305 snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
306
307 if (isFatal) {
308 setError(RS_ERROR_FATAL_DRIVER, buf);
309 } else {
310 switch (err) {
311 case GL_OUT_OF_MEMORY:
312 setError(RS_ERROR_OUT_OF_MEMORY, buf);
313 break;
314 default:
315 setError(RS_ERROR_DRIVER, buf);
316 break;
317 }
318 }
319
320 LOGE("%p, %s", this, buf);
Jason Samsd01d9702009-12-23 14:35:29 -0800321 }
322}
Jason Sams10308932009-06-09 12:15:30 -0700323
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800324uint32_t Context::runRootScript() {
Jason Sams771565f2010-05-14 15:30:29 -0700325 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700326
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700328 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700329 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700330
Jason Samsd01d9702009-12-23 14:35:29 -0800331 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700332 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700333}
334
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800335uint64_t Context::getTime() const {
Jason Sams24371d92009-08-19 12:17:14 -0700336 struct timespec t;
337 clock_gettime(CLOCK_MONOTONIC, &t);
338 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
339}
340
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800341void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700342 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
343 mTimers[ct] = 0;
344 }
345}
346
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800347void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700348 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700349 mTimeFrame = mTimeLast;
350 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700351 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700352 mAverageFPSFrameCount = 0;
353 mAverageFPSStartTime = mTimeLast;
354 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700355 timerReset();
356}
357
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800358void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700359 mTimeLastFrame = mTimeFrame;
360 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700361 // Update average fps
362 const uint64_t averageFramerateInterval = 1000 * 1000000;
363 mAverageFPSFrameCount ++;
364 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800365 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700366 inverval = inverval / 1000000;
367 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
368 mAverageFPSFrameCount = 0;
369 mAverageFPSStartTime = mTimeFrame;
370 }
Jason Sams1d54f102009-09-03 15:43:13 -0700371}
372
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800373void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700374 uint64_t last = mTimeLast;
375 mTimeLast = getTime();
376 mTimers[mTimerActive] += mTimeLast - last;
377 mTimerActive = tm;
378}
379
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800380void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700381 double total = 0;
382 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
383 total += mTimers[ct];
384 }
Jason Sams1d54f102009-09-03 15:43:13 -0700385 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800386 mTimeMSLastFrame = frame / 1000000;
387 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
388 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700389
Jason Sams2dca84d2009-12-09 11:05:45 -0800390
391 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700392 LOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800393 mTimeMSLastFrame,
394 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
395 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
396 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700397 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
398 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800399 }
Jason Sams24371d92009-08-19 12:17:14 -0700400}
401
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800402bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700403 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
404 LOGE("Context::setupCheck() 1 fail");
405 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800406 }
Jason Sams900f1612010-09-16 18:18:29 -0700407
408 mFragmentStore->setupGL2(this, &mStateFragmentStore);
409 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
410 mRaster->setupGL2(this, &mStateRaster);
411 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800412 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700413}
414
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700415void Context::setupProgramStore() {
416 mFragmentStore->setupGL2(this, &mStateFragmentStore);
417}
418
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800419static bool getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700420 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700421 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700422 return 0 != strcmp(buf, "0");
423}
Jason Sams326e0dd2009-05-22 14:03:28 -0700424
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800425void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700426 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700427 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700428 float oldR, oldG, oldB, oldA;
429 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700430 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700431
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700432 ObjectBaseRef<Font> lastFont(getFont());
433 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700434 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700435 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700436 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700437
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700438 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700439 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700440
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700441 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700442 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700443}
444
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800445void * Context::threadProc(void *vrsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700446 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800447 rsc->mNativeThreadId = gettid();
448
449 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800450 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700451
Jason Sams1fddd902009-09-25 15:25:00 -0700452 rsc->props.mLogTimes = getProp("debug.rs.profile");
453 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800454 rsc->props.mLogObjects = getProp("debug.rs.object");
455 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700456 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
457 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700458 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700459
Jason Sams605048a2010-09-30 18:15:52 -0700460 rsc->mTlsStruct = new ScriptTLSStruct;
461 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700462 LOGE("Error allocating tls storage");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700463 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
Jason Samse5769102009-06-19 16:03:18 -0700464 return NULL;
465 }
Jason Sams605048a2010-09-30 18:15:52 -0700466 rsc->mTlsStruct->mContext = rsc;
467 rsc->mTlsStruct->mScript = NULL;
468 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700469 if (status) {
470 LOGE("pthread_setspecific %i", status);
471 }
472
Jason Sams5c1c79a2010-11-03 14:27:11 -0700473 if (!rsc->initGLThread()) {
474 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
475 return NULL;
476 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700477
Jason Sams4820e8b2010-02-09 16:05:07 -0800478 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700479 rsc->mStateRaster.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800480 rsc->setProgramRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700481 rsc->mStateVertex.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800482 rsc->setProgramVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700483 rsc->mStateFragment.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800484 rsc->setProgramFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700485 rsc->mStateFragmentStore.init(rsc);
Jason Sams60709252010-11-17 15:29:32 -0800486 rsc->setProgramStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700487 rsc->mStateFont.init(rsc);
488 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800489 rsc->mStateVertexArray.init(rsc);
490 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700491
Jason Sams326e0dd2009-05-22 14:03:28 -0700492 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700493 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700494 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700495 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700496 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800497 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700498
Jason Sams2dca84d2009-12-09 11:05:45 -0800499 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800500 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800501 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700502
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800503 if (rsc->props.mLogVisual) {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700504 rsc->displayDebugStats();
505 }
506
Jason Sams2dca84d2009-12-09 11:05:45 -0800507 mDraw = targetTime && !rsc->mPaused;
508 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700509 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800510 rsc->timerFrame();
511 rsc->timerSet(RS_TIMER_INTERNAL);
512 rsc->timerPrint();
513 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700514 }
Jason Sams177f8442010-10-29 10:19:21 -0700515 if (targetTime > 1) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800516 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
517 if (t > 0) {
518 usleep(t);
519 }
520 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700521 }
522
Jason Sams4c5f99e2010-09-14 14:59:03 -0700523 LOGV("%p, RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700524
Jason Sams4820e8b2010-02-09 16:05:07 -0800525 if (rsc->mIsGraphicsContext) {
526 pthread_mutex_lock(&gInitMutex);
527 rsc->deinitEGL();
528 pthread_mutex_unlock(&gInitMutex);
529 }
Jason Sams605048a2010-09-30 18:15:52 -0700530 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700531
Jason Sams4c5f99e2010-09-14 14:59:03 -0700532 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700533 return NULL;
534}
535
Jason Sams741aac92010-12-24 14:38:39 -0800536void Context::destroyWorkerThreadResources() {
Jason Sams249d4532011-01-23 17:48:45 -0800537 //LOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800538 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800539 if (mIsGraphicsContext) {
540 mRaster.clear();
541 mFragment.clear();
542 mVertex.clear();
543 mFragmentStore.clear();
544 mFont.clear();
545 mRootScript.clear();
546 mStateRaster.deinit(this);
547 mStateVertex.deinit(this);
548 mStateFragment.deinit(this);
549 mStateFragmentStore.deinit(this);
550 mStateFont.deinit(this);
Jason Sams2cbfc4c2011-01-05 03:37:48 -0800551 mShaderCache.cleanupAll();
Jason Sams741aac92010-12-24 14:38:39 -0800552 }
Jason Sams249d4532011-01-23 17:48:45 -0800553 //LOGV("destroyWorkerThreadResources 2");
Jason Samscf912de2011-01-09 16:09:51 -0800554 mExit = true;
Jason Sams741aac92010-12-24 14:38:39 -0800555}
556
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800557void * Context::helperThreadProc(void *vrsc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700558 Context *rsc = static_cast<Context *>(vrsc);
559 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
560
Jason Sams249d4532011-01-23 17:48:45 -0800561 //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700562
563 rsc->mWorkers.mLaunchSignals[idx].init();
564 rsc->mWorkers.mNativeThreadId[idx] = gettid();
565
Jason Sams8d957fa2010-09-28 14:41:22 -0700566#if 0
567 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
568 cpu_set_t cpuset;
569 memset(&cpuset, 0, sizeof(cpuset));
570 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
571 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700572 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700573 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
574#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700575
576 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700577 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
578 if (status) {
579 LOGE("pthread_setspecific %i", status);
580 }
581
Jason Sams249d4532011-01-23 17:48:45 -0800582 while (!rsc->mExit) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700583 rsc->mWorkers.mLaunchSignals[idx].wait();
584 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700585 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
586 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700587 android_atomic_dec(&rsc->mWorkers.mRunningCount);
588 rsc->mWorkers.mCompleteSignal.set();
589 }
Jason Sams18133402010-07-20 15:09:00 -0700590
Jason Sams249d4532011-01-23 17:48:45 -0800591 //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700592 return NULL;
593}
594
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800595void Context::launchThreads(WorkerCallback_t cbk, void *data) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700596 mWorkers.mLaunchData = data;
597 mWorkers.mLaunchCallback = cbk;
Stephen Hines4f947d72011-03-08 16:49:28 -0800598 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700599 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
600 mWorkers.mLaunchSignals[ct].set();
601 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800602 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700603 mWorkers.mCompleteSignal.wait();
604 }
605}
606
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800607void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800608 // Note: If we put this in the proper "background" policy
609 // the wallpapers can become completly unresponsive at times.
610 // This is probably not what we want for something the user is actively
611 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800612 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800613#if 0
614 SchedPolicy pol = SP_FOREGROUND;
615 if (p > 0) {
616 pol = SP_BACKGROUND;
617 }
618 if (!set_sched_policy(mNativeThreadId, pol)) {
619 // success; reset the priority as well
620 }
621#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700622 setpriority(PRIO_PROCESS, mNativeThreadId, p);
623 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
624 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
625 }
Jason Sams15832442009-11-15 12:14:26 -0800626#endif
627}
628
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800629Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700630 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700631 mRunning = false;
632 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700633 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700634 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800635 mError = RS_ERROR_NONE;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700636 mDPI = 96;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700637}
638
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800639Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700640 Context * rsc = new Context();
641 if (!rsc->initContext(dev, sc)) {
642 delete rsc;
643 return NULL;
644 }
645 return rsc;
646}
647
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800648bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700649 pthread_mutex_lock(&gInitMutex);
650
651 dev->addContext(this);
652 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700653 if (sc) {
654 mUserSurfaceConfig = *sc;
655 } else {
656 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
657 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800658
Jason Sams613cad12009-11-12 15:10:25 -0800659 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700661 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700662
Jason Samsa658e902009-06-04 14:35:01 -0700663 int status;
664 pthread_attr_t threadAttr;
665
Jason Samsfb03a222009-10-15 16:47:31 -0700666 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700667 status = pthread_key_create(&gThreadTLSKey, NULL);
668 if (status) {
669 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700670 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700671 return false;
Jason Sams9e4e13d2009-10-06 17:16:55 -0700672 }
Jason Samse5769102009-06-19 16:03:18 -0700673 }
Jason Samsfb03a222009-10-15 16:47:31 -0700674 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700675
Jason Samsfb03a222009-10-15 16:47:31 -0700676 pthread_mutex_unlock(&gInitMutex);
677
678 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700679
Jason Samsa658e902009-06-04 14:35:01 -0700680 status = pthread_attr_init(&threadAttr);
681 if (status) {
682 LOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700683 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700684 }
685
Jason Sams613cad12009-11-12 15:10:25 -0800686 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700687
Jason Sams24371d92009-08-19 12:17:14 -0700688 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700689 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700690
Jason Sams7c7c78a2010-07-26 17:12:55 -0700691 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
692 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
693 if (cpu < 2) cpu = 0;
694
695 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700696 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
697 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
698 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
699 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700700 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700701 if (status) {
702 LOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700703 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700704 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800705 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700706 usleep(100);
707 }
708
Jason Sams5c1c79a2010-11-03 14:27:11 -0700709 if (mError != RS_ERROR_NONE) {
710 return false;
711 }
712
Jason Sams8d957fa2010-09-28 14:41:22 -0700713 mWorkers.mCompleteSignal.init();
Stephen Hines4f947d72011-03-08 16:49:28 -0800714 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
715 android_atomic_release_store(0, &mWorkers.mLaunchCount);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700716 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
717 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
718 if (status) {
719 mWorkers.mCount = ct;
720 LOGE("Created fewer than expected number of RS threads.");
721 break;
722 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700723 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800724 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) {
725 usleep(100);
726 }
Jason Samsa658e902009-06-04 14:35:01 -0700727 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700728 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700729}
730
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800731Context::~Context() {
Jason Sams8c0ee652009-08-25 14:49:07 -0700732 LOGV("Context::~Context");
Jason Samscf912de2011-01-09 16:09:51 -0800733
734 mIO.mToCore.flush();
735 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700736 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700737 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700738 void *res;
739
Jason Sams8c0ee652009-08-25 14:49:07 -0700740 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700741 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700742
Jason Sams249d4532011-01-23 17:48:45 -0800743 // Cleanup compute threads.
744 mWorkers.mLaunchData = NULL;
745 mWorkers.mLaunchCallback = NULL;
Stephen Hines4f947d72011-03-08 16:49:28 -0800746 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount);
Jason Sams249d4532011-01-23 17:48:45 -0800747 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
748 mWorkers.mLaunchSignals[ct].set();
Jason Sams51462c52011-01-25 00:26:25 -0800749 }
750 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
Stephen Hines196c1112011-03-01 17:34:59 -0800751 status = pthread_join(mWorkers.mThreadId[ct], &res);
Jason Sams249d4532011-01-23 17:48:45 -0800752 }
Stephen Hines4f947d72011-03-08 16:49:28 -0800753 rsAssert(android_atomic_acquire_load(&mWorkers.mRunningCount) == 0);
Jason Sams249d4532011-01-23 17:48:45 -0800754
Jason Samsfb03a222009-10-15 16:47:31 -0700755 // Global structure cleanup.
756 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700757 if (mDev) {
758 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700759 --gThreadTLSKeyCount;
760 if (!gThreadTLSKeyCount) {
761 pthread_key_delete(gThreadTLSKey);
762 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800763 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700764 }
Jason Samsfb03a222009-10-15 16:47:31 -0700765 pthread_mutex_unlock(&gInitMutex);
Jason Sams741aac92010-12-24 14:38:39 -0800766 LOGV("Context::~Context done");
Jason Sams326e0dd2009-05-22 14:03:28 -0700767}
768
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800769void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800770 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800771
Jason Sams458f2dc2009-11-03 13:58:36 -0800772 EGLBoolean ret;
Jason Sams52a67b22011-01-12 15:26:25 -0800773 // WAR: Some drivers fail to handle 0 size surfaces correcntly.
774 // Use the pbuffer to avoid this pitfall.
775 if ((mEGL.mSurface != NULL) || (w == 0) || (h == 0)) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700776 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800777 checkEglError("eglMakeCurrent", ret);
778
779 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
780 checkEglError("eglDestroySurface", ret);
781
782 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700783 mWidth = 1;
784 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800785 }
786
787 mWndSurface = sur;
788 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700789 mWidth = w;
790 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800791
Jason Sams458f2dc2009-11-03 13:58:36 -0800792 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
793 checkEglError("eglCreateWindowSurface");
794 if (mEGL.mSurface == EGL_NO_SURFACE) {
795 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
796 }
797
798 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
799 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800800
Jason Sams771565f2010-05-14 15:30:29 -0700801 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800802 }
803}
804
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800805void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800806 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700807 mPaused = true;
808}
809
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800810void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800811 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700812 mPaused = false;
813}
814
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800815void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800816 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700817 mRootScript.set(s);
818}
819
Jason Sams60709252010-11-17 15:29:32 -0800820void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800821 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700822 if (pfs == NULL) {
823 mFragmentStore.set(mStateFragmentStore.mDefault);
824 } else {
825 mFragmentStore.set(pfs);
826 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700827}
828
Jason Sams60709252010-11-17 15:29:32 -0800829void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800830 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700831 if (pf == NULL) {
832 mFragment.set(mStateFragment.mDefault);
833 } else {
834 mFragment.set(pf);
835 }
Jason Samscfb1d112009-08-05 13:57:03 -0700836}
837
Jason Sams60709252010-11-17 15:29:32 -0800838void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800839 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700840 if (pr == NULL) {
841 mRaster.set(mStateRaster.mDefault);
842 } else {
843 mRaster.set(pr);
844 }
845}
846
Jason Sams60709252010-11-17 15:29:32 -0800847void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800848 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700849 if (pv == NULL) {
850 mVertex.set(mStateVertex.mDefault);
851 } else {
852 mVertex.set(pv);
853 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700854}
855
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800856void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700857 rsAssert(mIsGraphicsContext);
858 if (f == NULL) {
859 mFont.set(mStateFont.mDefault);
860 } else {
861 mFont.set(f);
862 }
863}
864
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800865void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700866 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700867 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700868 mNames.add(obj);
869}
870
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800871void Context::removeName(ObjectBase *obj) {
872 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700873 if (obj == mNames[ct]) {
874 mNames.removeAt(ct);
875 return;
876 }
877 }
878}
879
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800880RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800881 *receiveLen = 0;
882 if (!wait && mIO.mToClient.isEmpty()) {
883 return RS_MESSAGE_TO_CLIENT_NONE;
884 }
885
886 uint32_t bytesData = 0;
887 uint32_t commandID = 0;
888 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
889 *receiveLen = bytesData - sizeof(uint32_t);
890 if (bytesData) {
891 *subID = d[0];
892 }
893 return (RsMessageToClientType)commandID;
894}
895
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800896RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700897 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700898 *receiveLen = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800899 if (!wait && mIO.mToClient.isEmpty()) {
900 return RS_MESSAGE_TO_CLIENT_NONE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700901 }
902
903 //LOGE("getMessageToClient 2 con=%p", this);
904 uint32_t bytesData = 0;
905 uint32_t commandID = 0;
Jason Samsaad4bc52010-11-08 17:06:46 -0800906 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
Jason Sams8c401ef2009-10-06 13:58:47 -0700907 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
908
Jason Samsaad4bc52010-11-08 17:06:46 -0800909 *receiveLen = bytesData - sizeof(uint32_t);
910 *subID = d[0];
911
912 //LOGE("getMessageToClient %i %i", commandID, *subID);
Jason Sams8c401ef2009-10-06 13:58:47 -0700913 if (bufferLen >= bytesData) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800914 memcpy(data, d+1, *receiveLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700915 mIO.mToClient.next();
Jason Samsaad4bc52010-11-08 17:06:46 -0800916 return (RsMessageToClientType)commandID;
Jason Sams8c401ef2009-10-06 13:58:47 -0700917 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800918 return RS_MESSAGE_TO_CLIENT_RESIZE;
Jason Sams8c401ef2009-10-06 13:58:47 -0700919}
920
Jason Sams87319de2010-11-22 16:20:16 -0800921bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
922 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsaad4bc52010-11-08 17:06:46 -0800923 //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700924 if (cmdID == 0) {
925 LOGE("Attempting to send invalid command 0 to client.");
926 return false;
927 }
928 if (!waitForSpace) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800929 if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700930 // Not enough room, and not waiting.
931 return false;
932 }
933 }
934 //LOGE("sendMessageToClient 2");
Jason Samsaad4bc52010-11-08 17:06:46 -0800935 uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
936 p[0] = subID;
Jason Samsef5867a2010-07-28 11:17:53 -0700937 if (len > 0) {
Jason Samsaad4bc52010-11-08 17:06:46 -0800938 memcpy(p+1, data, len);
Jason Samsef5867a2010-07-28 11:17:53 -0700939 }
Jason Samsaad4bc52010-11-08 17:06:46 -0800940 mIO.mToClient.commit(cmdID, len + sizeof(subID));
Jason Sams8c401ef2009-10-06 13:58:47 -0700941 //LOGE("sendMessageToClient 3");
942 return true;
943}
944
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800945void Context::initToClient() {
946 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700947 usleep(100);
948 }
949}
950
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800951void Context::deinitToClient() {
Jason Sams8c401ef2009-10-06 13:58:47 -0700952 mIO.mToClient.shutdown();
953}
Jason Sams50869382009-08-18 17:07:09 -0700954
Jason Sams87319de2010-11-22 16:20:16 -0800955void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800956 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800957 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800958}
959
960
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800961void Context::dumpDebug() const {
Jason Sams13e26342009-11-24 12:26:35 -0800962 LOGE("RS Context debug %p", this);
963 LOGE("RS Context debug");
964
965 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700966 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800967 LOGE(" GL vendor: %s", mGL.mVendor);
968 LOGE(" GL renderer: %s", mGL.mRenderer);
969 LOGE(" GL Version: %s", mGL.mVersion);
970 LOGE(" GL Extensions: %s", mGL.mExtensions);
971 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
972 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700973 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800974 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
975
Jason Sams4815c0d2009-12-15 12:58:36 -0800976 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
977 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
978 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
979 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800980}
Jason Samsa4a54e42009-06-10 18:39:40 -0700981
Jason Sams326e0dd2009-05-22 14:03:28 -0700982///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700983//
Jason Sams326e0dd2009-05-22 14:03:28 -0700984
985namespace android {
986namespace renderscript {
987
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800988void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700989}
Jason Sams326e0dd2009-05-22 14:03:28 -0700990
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800991void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700992 Script *s = static_cast<Script *>(vs);
993 rsc->setRootScript(s);
994}
995
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800996void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700997 Sampler *s = static_cast<Sampler *>(vs);
998
999 if (slot > RS_MAX_SAMPLER_SLOT) {
1000 LOGE("Invalid sampler slot");
1001 return;
1002 }
1003
1004 s->bindToContext(&rsc->mStateSampler, slot);
1005}
1006
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001007void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -07001008 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -08001009 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -07001010}
1011
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001012void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001013 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -08001014 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -07001015}
1016
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001017void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -07001018 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -08001019 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -07001020}
1021
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001022void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -07001023 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -08001024 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -07001025}
1026
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001027void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001028 Font *font = static_cast<Font *>(vfont);
1029 rsc->setFont(font);
1030}
1031
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001032void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001033 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -07001034 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001035}
Jason Sams326e0dd2009-05-22 14:03:28 -07001036
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001037void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -07001038 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -07001039 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -07001040 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -07001041}
1042
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001043void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001044 rsc->pause();
1045}
1046
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001047void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -07001048 rsc->resume();
1049}
1050
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001051void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -08001052 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -08001053}
1054
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001055void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -08001056 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001057}
1058
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001059void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -08001060 ObjectBase::dumpAll(rsc);
1061}
1062
Jason Sams741aac92010-12-24 14:38:39 -08001063void rsi_ContextDestroyWorker(Context *rsc) {
Jason Sams741aac92010-12-24 14:38:39 -08001064 rsc->destroyWorkerThreadResources();;
Jason Sams741aac92010-12-24 14:38:39 -08001065}
1066
1067}
1068}
1069
1070void rsContextDestroy(RsContext vcon) {
1071 LOGV("rsContextDestroy %p", vcon);
1072 Context *rsc = static_cast<Context *>(vcon);
1073 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -08001074 delete rsc;
Jason Sams741aac92010-12-24 14:38:39 -08001075 LOGV("rsContextDestroy 2 %p", vcon);
Jason Sams1dcefab2010-12-09 12:19:46 -08001076}
1077
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001078RsContext rsContextCreate(RsDevice vdev, uint32_t version) {
Jason Sams4820e8b2010-02-09 16:05:07 -08001079 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001080 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001081 Context *rsc = Context::createContext(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001082 return rsc;
1083}
1084
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -07001085RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
1086 RsSurfaceConfig sc, uint32_t dpi) {
Jason Sams6b8552a2010-10-13 15:31:10 -07001087 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001088 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001089 Context *rsc = Context::createContext(dev, &sc);
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -07001090 rsc->setDPI(dpi);
Jason Sams900f1612010-09-16 18:18:29 -07001091 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001092 return rsc;
1093}
1094
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001095RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001096 Context * rsc = static_cast<Context *>(vrsc);
Jason Samsaad4bc52010-11-08 17:06:46 -08001097 return rsc->peekMessageToClient(receiveLen, subID, wait);
1098}
1099
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001100RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Samsaad4bc52010-11-08 17:06:46 -08001101 Context * rsc = static_cast<Context *>(vrsc);
1102 return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait);
Jason Sams8c401ef2009-10-06 13:58:47 -07001103}
1104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001105void rsContextInitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001106 Context * rsc = static_cast<Context *>(vrsc);
1107 rsc->initToClient();
1108}
1109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001110void rsContextDeinitToClient(RsContext vrsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -07001111 Context * rsc = static_cast<Context *>(vrsc);
1112 rsc->deinitToClient();
1113}
1114
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001115// Only to be called at a3d load time, before object is visible to user
1116// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001117void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001118 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1119 (*name) = ob->getName();
1120}