blob: cfd64796f093de0510488c214635cc904fa7ed68 [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 Samsafcb25c2009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Mathias Agopian9b97c292010-02-12 12:00:38 -080022#include <ui/egl/android_natives.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070023
Jason Sams15832442009-11-15 12:14:26 -080024#include <sys/types.h>
25#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070026#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080027
Joe Onorato76371ff2009-09-23 16:37:36 -070028#include <cutils/properties.h>
29
Jason Sams1aa5a4e2009-06-22 17:15:15 -070030#include <GLES/gl.h>
31#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070034
Jason Sams15832442009-11-15 12:14:26 -080035#include <cutils/sched_policy.h>
Jason Sams8d957fa2010-09-28 14:41:22 -070036#include <sys/syscall.h>
37#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080038
Jason Sams326e0dd2009-05-22 14:03:28 -070039using namespace android;
40using namespace android::renderscript;
41
Jason Samse5769102009-06-19 16:03:18 -070042pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070043uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070044uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070045pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070046
Jason Sams33b6e3b2009-10-27 14:44:31 -070047static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
48 if (returnVal != EGL_TRUE) {
49 fprintf(stderr, "%s() returned %d\n", op, returnVal);
50 }
51
52 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
53 = eglGetError()) {
54 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
55 error);
56 }
57}
58
Jason Samsc460e552009-11-25 13:22:07 -080059void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070060{
Jason Samsafcb25c2009-08-25 11:34:49 -070061 mEGL.mNumConfigs = -1;
62 EGLint configAttribs[128];
63 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080064 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsafcb25c2009-08-25 11:34:49 -070066 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070067
Jason Samsafcb25c2009-08-25 11:34:49 -070068 configAttribsPtr[0] = EGL_SURFACE_TYPE;
69 configAttribsPtr[1] = EGL_WINDOW_BIT;
70 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070071
Jason Samsc460e552009-11-25 13:22:07 -080072 if (useGL2) {
73 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
74 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
75 configAttribsPtr += 2;
76 }
77
Jason Samsafcb25c2009-08-25 11:34:49 -070078 if (mUseDepth) {
79 configAttribsPtr[0] = EGL_DEPTH_SIZE;
80 configAttribsPtr[1] = 16;
81 configAttribsPtr += 2;
82 }
Jason Sams9397e302009-08-27 20:23:34 -070083
Jason Sams5fd09d82009-09-23 13:57:02 -070084 if (mDev->mForceSW) {
85 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
86 configAttribsPtr[1] = EGL_SLOW_CONFIG;
87 configAttribsPtr += 2;
88 }
89
Jason Samsafcb25c2009-08-25 11:34:49 -070090 configAttribsPtr[0] = EGL_NONE;
91 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Jason Sams4c5f99e2010-09-14 14:59:03 -070093 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -070094 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070095 checkEglError("eglGetDisplay");
96
Jason Samsafcb25c2009-08-25 11:34:49 -070097 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070098 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070099
100 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
101 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700102 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700103 }
104 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
105
Jason Samsafcb25c2009-08-25 11:34:49 -0700106
Jason Samsc460e552009-11-25 13:22:07 -0800107 if (useGL2) {
108 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
109 } else {
110 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
111 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700112 checkEglError("eglCreateContext");
113 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700114 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700115 }
116 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700117}
118
Jason Sams33b6e3b2009-10-27 14:44:31 -0700119void Context::deinitEGL()
120{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700121 LOGV("%p, deinitEGL", this);
Jason Sams613cad12009-11-12 15:10:25 -0800122 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700123 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
124 checkEglError("eglDestroyContext");
125
126 gGLContextCount--;
127 if (!gGLContextCount) {
128 eglTerminate(mEGL.mDisplay);
129 }
130}
131
132
Jason Samsc61346b2010-05-28 18:23:22 -0700133uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700134{
135 ObjectBaseRef<ProgramFragment> frag(mFragment);
136 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700137 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700138 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700139 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700140
Jason Samsc61346b2010-05-28 18:23:22 -0700141 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700142
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700143 mFragment.set(frag);
144 mVertex.set(vtx);
145 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700146 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700147 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700148 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700149}
150
Jason Samsd01d9702009-12-23 14:35:29 -0800151void Context::checkError(const char *msg) const
152{
153 GLenum err = glGetError();
154 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700155 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800156 }
157}
Jason Sams10308932009-06-09 12:15:30 -0700158
Jason Sams2dca84d2009-12-09 11:05:45 -0800159uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700160{
Jason Sams771565f2010-05-14 15:30:29 -0700161 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700162
Jason Sams2dca84d2009-12-09 11:05:45 -0800163 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700164 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700165 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700166
Jason Samsd01d9702009-12-23 14:35:29 -0800167 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700168 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700169}
170
Jason Sams24371d92009-08-19 12:17:14 -0700171uint64_t Context::getTime() const
172{
173 struct timespec t;
174 clock_gettime(CLOCK_MONOTONIC, &t);
175 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
176}
177
178void Context::timerReset()
179{
180 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
181 mTimers[ct] = 0;
182 }
183}
184
185void Context::timerInit()
186{
187 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700188 mTimeFrame = mTimeLast;
189 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700190 mTimerActive = RS_TIMER_INTERNAL;
191 timerReset();
192}
193
Jason Sams1d54f102009-09-03 15:43:13 -0700194void Context::timerFrame()
195{
196 mTimeLastFrame = mTimeFrame;
197 mTimeFrame = getTime();
198}
199
Jason Sams24371d92009-08-19 12:17:14 -0700200void Context::timerSet(Timers tm)
201{
202 uint64_t last = mTimeLast;
203 mTimeLast = getTime();
204 mTimers[mTimerActive] += mTimeLast - last;
205 mTimerActive = tm;
206}
207
208void Context::timerPrint()
209{
210 double total = 0;
211 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
212 total += mTimers[ct];
213 }
Jason Sams1d54f102009-09-03 15:43:13 -0700214 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800215 mTimeMSLastFrame = frame / 1000000;
216 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
217 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700218
Jason Sams2dca84d2009-12-09 11:05:45 -0800219
220 if (props.mLogTimes) {
221 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
222 mTimeMSLastFrame,
223 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
224 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
225 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
226 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
227 }
Jason Sams24371d92009-08-19 12:17:14 -0700228}
229
Jason Samsa2cf7552010-03-03 13:03:18 -0800230bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700231{
Jason Sams900f1612010-09-16 18:18:29 -0700232 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
233 LOGE("Context::setupCheck() 1 fail");
234 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800235 }
Jason Sams900f1612010-09-16 18:18:29 -0700236
237 mFragmentStore->setupGL2(this, &mStateFragmentStore);
238 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
239 mRaster->setupGL2(this, &mStateRaster);
240 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800241 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700242}
243
Jason Sams1fddd902009-09-25 15:25:00 -0700244static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700245{
246 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700247 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700248 return 0 != strcmp(buf, "0");
249}
Jason Sams326e0dd2009-05-22 14:03:28 -0700250
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700251void Context::displayDebugStats()
252{
253 char buffer[128];
254 sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700255 float oldR, oldG, oldB, oldA;
256 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700257
258 float shadowCol = 0.2f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700259 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700260 mStateFont.renderText(buffer, 5, getHeight() - 5);
261
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700262 float textCol = 0.9f;
263 mStateFont.setFontColor(textCol, textCol, textCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700264 mStateFont.renderText(buffer, 4, getHeight() - 6);
265
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700266 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700267}
268
Jason Sams326e0dd2009-05-22 14:03:28 -0700269void * Context::threadProc(void *vrsc)
270{
271 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800272 rsc->mNativeThreadId = gettid();
273
274 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800275 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700276
Jason Sams1fddd902009-09-25 15:25:00 -0700277 rsc->props.mLogTimes = getProp("debug.rs.profile");
278 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800279 rsc->props.mLogObjects = getProp("debug.rs.object");
280 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700281 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
282 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700283 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700284
Jason Samse5769102009-06-19 16:03:18 -0700285 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
286 if (!tlsStruct) {
287 LOGE("Error allocating tls storage");
288 return NULL;
289 }
290 tlsStruct->mContext = rsc;
291 tlsStruct->mScript = NULL;
292 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
293 if (status) {
294 LOGE("pthread_setspecific %i", status);
295 }
296
Stephen Hines01b7d292010-09-28 15:45:45 -0700297 rsc->mScriptC.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800298 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700299 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800300 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700301 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800302 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700303 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800304 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700305 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800306 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700307 rsc->mStateFont.init(rsc);
308 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800309 rsc->mStateVertexArray.init(rsc);
310 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700311
Jason Sams326e0dd2009-05-22 14:03:28 -0700312 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700313 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700314 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700315 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700316 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800317 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700318
Jason Sams2dca84d2009-12-09 11:05:45 -0800319 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800320 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800321 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700322
323 if(rsc->props.mLogVisual) {
324 rsc->displayDebugStats();
325 }
326
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 mDraw = targetTime && !rsc->mPaused;
328 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700329 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800330 rsc->timerFrame();
331 rsc->timerSet(RS_TIMER_INTERNAL);
332 rsc->timerPrint();
333 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700334 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800335 if (rsc->mThreadPriority > 0 && targetTime) {
336 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
337 if (t > 0) {
338 usleep(t);
339 }
340 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700341 }
342
Jason Sams4c5f99e2010-09-14 14:59:03 -0700343 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800344 if (rsc->mIsGraphicsContext) {
345 rsc->mRaster.clear();
346 rsc->mFragment.clear();
347 rsc->mVertex.clear();
348 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700349 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800350 rsc->mRootScript.clear();
351 rsc->mStateRaster.deinit(rsc);
352 rsc->mStateVertex.deinit(rsc);
353 rsc->mStateFragment.deinit(rsc);
354 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700355 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800356 }
Jason Samse514b452009-09-25 14:51:22 -0700357 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700358
Jason Sams4820e8b2010-02-09 16:05:07 -0800359 if (rsc->mIsGraphicsContext) {
360 pthread_mutex_lock(&gInitMutex);
361 rsc->deinitEGL();
362 pthread_mutex_unlock(&gInitMutex);
363 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700364
Jason Sams4c5f99e2010-09-14 14:59:03 -0700365 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700366 return NULL;
367}
368
Jason Sams7bf29dd2010-07-19 15:38:19 -0700369void * Context::helperThreadProc(void *vrsc)
370{
371 Context *rsc = static_cast<Context *>(vrsc);
372 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
373
Jason Sams18133402010-07-20 15:09:00 -0700374 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700375
376 rsc->mWorkers.mLaunchSignals[idx].init();
377 rsc->mWorkers.mNativeThreadId[idx] = gettid();
378
Jason Sams8d957fa2010-09-28 14:41:22 -0700379#if 0
380 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
381 cpu_set_t cpuset;
382 memset(&cpuset, 0, sizeof(cpuset));
383 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
384 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700385 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700386 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
387#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700388
389 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
390 while(rsc->mRunning) {
391 rsc->mWorkers.mLaunchSignals[idx].wait();
392 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700393 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
394 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700395 android_atomic_dec(&rsc->mWorkers.mRunningCount);
396 rsc->mWorkers.mCompleteSignal.set();
397 }
Jason Sams18133402010-07-20 15:09:00 -0700398
399 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700400 return NULL;
401}
402
403void Context::launchThreads(WorkerCallback_t cbk, void *data)
404{
405 mWorkers.mLaunchData = data;
406 mWorkers.mLaunchCallback = cbk;
407 mWorkers.mRunningCount = (int)mWorkers.mCount;
408 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
409 mWorkers.mLaunchSignals[ct].set();
410 }
411 while(mWorkers.mRunningCount) {
412 mWorkers.mCompleteSignal.wait();
413 }
414}
415
Jason Sams15832442009-11-15 12:14:26 -0800416void Context::setPriority(int32_t p)
417{
418 // Note: If we put this in the proper "background" policy
419 // the wallpapers can become completly unresponsive at times.
420 // This is probably not what we want for something the user is actively
421 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800422 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800423#if 0
424 SchedPolicy pol = SP_FOREGROUND;
425 if (p > 0) {
426 pol = SP_BACKGROUND;
427 }
428 if (!set_sched_policy(mNativeThreadId, pol)) {
429 // success; reset the priority as well
430 }
431#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700432 setpriority(PRIO_PROCESS, mNativeThreadId, p);
433 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
434 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
435 }
Jason Sams15832442009-11-15 12:14:26 -0800436#endif
437}
438
Jason Sams4820e8b2010-02-09 16:05:07 -0800439Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700440{
Jason Samsfb03a222009-10-15 16:47:31 -0700441 pthread_mutex_lock(&gInitMutex);
442
Jason Sams326e0dd2009-05-22 14:03:28 -0700443 dev->addContext(this);
444 mDev = dev;
445 mRunning = false;
446 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700447 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700448 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700449 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800450 mError = RS_ERROR_NONE;
451 mErrorMsg = NULL;
452
Jason Sams613cad12009-11-12 15:10:25 -0800453 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800454 memset(&mGL, 0, sizeof(mGL));
455 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700456
Jason Samsa658e902009-06-04 14:35:01 -0700457 int status;
458 pthread_attr_t threadAttr;
459
Jason Samsfb03a222009-10-15 16:47:31 -0700460 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700461 status = pthread_key_create(&gThreadTLSKey, NULL);
462 if (status) {
463 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700464 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700465 return;
466 }
Jason Samse5769102009-06-19 16:03:18 -0700467 }
Jason Samsfb03a222009-10-15 16:47:31 -0700468 gThreadTLSKeyCount++;
469 pthread_mutex_unlock(&gInitMutex);
470
471 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700472
Jason Samsa658e902009-06-04 14:35:01 -0700473 status = pthread_attr_init(&threadAttr);
474 if (status) {
475 LOGE("Failed to init thread attribute.");
476 return;
477 }
478
Jason Sams613cad12009-11-12 15:10:25 -0800479 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700480
Jason Sams24371d92009-08-19 12:17:14 -0700481 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700482 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700483
Jason Sams7c7c78a2010-07-26 17:12:55 -0700484 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
485 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
486 if (cpu < 2) cpu = 0;
487
488 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700489 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
490 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
491 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
492 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700493 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700494 if (status) {
495 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700496 return;
497 }
Jason Sams18133402010-07-20 15:09:00 -0700498 while(!mRunning) {
499 usleep(100);
500 }
501
Jason Sams8d957fa2010-09-28 14:41:22 -0700502 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700503 mWorkers.mRunningCount = 0;
504 mWorkers.mLaunchCount = 0;
505 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
506 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
507 if (status) {
508 mWorkers.mCount = ct;
509 LOGE("Created fewer than expected number of RS threads.");
510 break;
511 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700512 }
513
Jason Sams326e0dd2009-05-22 14:03:28 -0700514
Jason Samsa658e902009-06-04 14:35:01 -0700515 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700516}
517
518Context::~Context()
519{
Jason Sams8c0ee652009-08-25 14:49:07 -0700520 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700521 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700522 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700523 void *res;
524
Jason Sams8c0ee652009-08-25 14:49:07 -0700525 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700526 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700527
Jason Samsfb03a222009-10-15 16:47:31 -0700528 // Global structure cleanup.
529 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700530 if (mDev) {
531 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700532 --gThreadTLSKeyCount;
533 if (!gThreadTLSKeyCount) {
534 pthread_key_delete(gThreadTLSKey);
535 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800536 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700537 }
Jason Samsfb03a222009-10-15 16:47:31 -0700538 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700539}
540
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700541void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800542{
Jason Sams4820e8b2010-02-09 16:05:07 -0800543 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800544
Jason Sams458f2dc2009-11-03 13:58:36 -0800545 EGLBoolean ret;
546 if (mEGL.mSurface != NULL) {
547 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
548 checkEglError("eglMakeCurrent", ret);
549
550 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
551 checkEglError("eglDestroySurface", ret);
552
553 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800554 mWidth = 0;
555 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800556 }
557
558 mWndSurface = sur;
559 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700560 mWidth = w;
561 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800562 bool first = false;
563 if (!mEGL.mContext) {
564 first = true;
565 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800566 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800567 pthread_mutex_unlock(&gInitMutex);
568 }
569
Jason Sams458f2dc2009-11-03 13:58:36 -0800570 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
571 checkEglError("eglCreateWindowSurface");
572 if (mEGL.mSurface == EGL_NO_SURFACE) {
573 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
574 }
575
576 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
577 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800578
Jason Sams771565f2010-05-14 15:30:29 -0700579 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800580
Jason Sams613cad12009-11-12 15:10:25 -0800581 if (first) {
582 mGL.mVersion = glGetString(GL_VERSION);
583 mGL.mVendor = glGetString(GL_VENDOR);
584 mGL.mRenderer = glGetString(GL_RENDERER);
585 mGL.mExtensions = glGetString(GL_EXTENSIONS);
586
587 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
588 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800589 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800590 LOGV("GL Renderer %s", mGL.mRenderer);
591 //LOGV("GL Extensions %s", mGL.mExtensions);
592
Jason Samsc460e552009-11-25 13:22:07 -0800593 const char *verptr = NULL;
594 if (strlen((const char *)mGL.mVersion) > 9) {
595 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
596 verptr = (const char *)mGL.mVersion + 12;
597 }
598 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
599 verptr = (const char *)mGL.mVersion + 9;
600 }
601 }
602
603 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800604 LOGE("Error, OpenGL ES Lite not supported");
605 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800606 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800607 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800608
609 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
610 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
611 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
612
613 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
614 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
615
616 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
617 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800618
619 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700620 mGL.EXT_texture_max_aniso = 1.0f;
621 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
622 if(hasAniso) {
623 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
624 }
Jason Sams613cad12009-11-12 15:10:25 -0800625 }
626
Jason Sams458f2dc2009-11-03 13:58:36 -0800627 }
628}
629
Jason Sams86f1b232009-09-24 17:38:20 -0700630void Context::pause()
631{
Jason Sams4820e8b2010-02-09 16:05:07 -0800632 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700633 mPaused = true;
634}
635
636void Context::resume()
637{
Jason Sams4820e8b2010-02-09 16:05:07 -0800638 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700639 mPaused = false;
640}
641
Jason Sams326e0dd2009-05-22 14:03:28 -0700642void Context::setRootScript(Script *s)
643{
Jason Sams4820e8b2010-02-09 16:05:07 -0800644 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700645 mRootScript.set(s);
646}
647
Jason Samsccc010b2010-05-13 18:30:11 -0700648void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700649{
Jason Sams4820e8b2010-02-09 16:05:07 -0800650 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700651 if (pfs == NULL) {
652 mFragmentStore.set(mStateFragmentStore.mDefault);
653 } else {
654 mFragmentStore.set(pfs);
655 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700656}
657
658void Context::setFragment(ProgramFragment *pf)
659{
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700661 if (pf == NULL) {
662 mFragment.set(mStateFragment.mDefault);
663 } else {
664 mFragment.set(pf);
665 }
Jason Samscfb1d112009-08-05 13:57:03 -0700666}
667
Jason Sams5fd09d82009-09-23 13:57:02 -0700668void Context::setRaster(ProgramRaster *pr)
669{
Jason Sams4820e8b2010-02-09 16:05:07 -0800670 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700671 if (pr == NULL) {
672 mRaster.set(mStateRaster.mDefault);
673 } else {
674 mRaster.set(pr);
675 }
676}
677
Jason Sams326e0dd2009-05-22 14:03:28 -0700678void Context::setVertex(ProgramVertex *pv)
679{
Jason Sams4820e8b2010-02-09 16:05:07 -0800680 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700681 if (pv == NULL) {
682 mVertex.set(mStateVertex.mDefault);
683 } else {
684 mVertex.set(pv);
685 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700686}
687
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700688void Context::setFont(Font *f)
689{
690 rsAssert(mIsGraphicsContext);
691 if (f == NULL) {
692 mFont.set(mStateFont.mDefault);
693 } else {
694 mFont.set(f);
695 }
696}
697
Jason Samsa4a54e42009-06-10 18:39:40 -0700698void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700699{
700 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700701 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700702 mNames.add(obj);
703}
704
705void Context::removeName(ObjectBase *obj)
706{
707 for(size_t ct=0; ct < mNames.size(); ct++) {
708 if (obj == mNames[ct]) {
709 mNames.removeAt(ct);
710 return;
711 }
712 }
713}
714
Jason Sams8c401ef2009-10-06 13:58:47 -0700715uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
716{
717 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700718 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700719 if (!wait) {
720 if (mIO.mToClient.isEmpty()) {
721 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700722 return 0;
723 }
724 }
725
726 //LOGE("getMessageToClient 2 con=%p", this);
727 uint32_t bytesData = 0;
728 uint32_t commandID = 0;
729 const void *d = mIO.mToClient.get(&commandID, &bytesData);
730 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
731
732 *receiveLen = bytesData;
733 if (bufferLen >= bytesData) {
734 memcpy(data, d, bytesData);
735 mIO.mToClient.next();
736 return commandID;
737 }
738 return 0;
739}
740
741bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
742{
743 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
744 if (cmdID == 0) {
745 LOGE("Attempting to send invalid command 0 to client.");
746 return false;
747 }
748 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700749 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700750 // Not enough room, and not waiting.
751 return false;
752 }
753 }
754 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700755 if (len > 0) {
756 void *p = mIO.mToClient.reserve(len);
757 memcpy(p, data, len);
758 mIO.mToClient.commit(cmdID, len);
759 } else {
760 mIO.mToClient.commit(cmdID, 0);
761 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700762 //LOGE("sendMessageToClient 3");
763 return true;
764}
765
766void Context::initToClient()
767{
768 while(!mRunning) {
769 usleep(100);
770 }
771}
772
773void Context::deinitToClient()
774{
775 mIO.mToClient.shutdown();
776}
Jason Sams50869382009-08-18 17:07:09 -0700777
Jason Samsa2cf7552010-03-03 13:03:18 -0800778const char * Context::getError(RsError *err)
779{
780 *err = mError;
781 mError = RS_ERROR_NONE;
782 if (*err != RS_ERROR_NONE) {
783 return mErrorMsg;
784 }
785 return NULL;
786}
787
788void Context::setError(RsError e, const char *msg)
789{
790 mError = e;
791 mErrorMsg = msg;
792}
793
794
Jason Sams13e26342009-11-24 12:26:35 -0800795void Context::dumpDebug() const
796{
797 LOGE("RS Context debug %p", this);
798 LOGE("RS Context debug");
799
800 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700801 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800802 LOGE(" GL vendor: %s", mGL.mVendor);
803 LOGE(" GL renderer: %s", mGL.mRenderer);
804 LOGE(" GL Version: %s", mGL.mVersion);
805 LOGE(" GL Extensions: %s", mGL.mExtensions);
806 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
807 LOGE(" RS width %i, height %i", mWidth, mHeight);
808 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
809 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
810
Jason Sams4815c0d2009-12-15 12:58:36 -0800811 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
812 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
813 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
814 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800815}
Jason Samsa4a54e42009-06-10 18:39:40 -0700816
Jason Sams326e0dd2009-05-22 14:03:28 -0700817///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700818//
Jason Sams326e0dd2009-05-22 14:03:28 -0700819
820namespace android {
821namespace renderscript {
822
Jason Sams8c880902010-06-15 12:15:57 -0700823void rsi_ContextFinish(Context *rsc)
824{
825}
Jason Sams326e0dd2009-05-22 14:03:28 -0700826
827void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
828{
829 Script *s = static_cast<Script *>(vs);
830 rsc->setRootScript(s);
831}
832
833void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
834{
835 Sampler *s = static_cast<Sampler *>(vs);
836
837 if (slot > RS_MAX_SAMPLER_SLOT) {
838 LOGE("Invalid sampler slot");
839 return;
840 }
841
842 s->bindToContext(&rsc->mStateSampler, slot);
843}
844
Jason Samsccc010b2010-05-13 18:30:11 -0700845void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700846{
Jason Samsccc010b2010-05-13 18:30:11 -0700847 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700848 rsc->setFragmentStore(pfs);
849}
850
851void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
852{
853 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
854 rsc->setFragment(pf);
855}
856
Jason Sams5fd09d82009-09-23 13:57:02 -0700857void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
858{
859 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
860 rsc->setRaster(pr);
861}
862
Jason Sams326e0dd2009-05-22 14:03:28 -0700863void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
864{
865 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
866 rsc->setVertex(pv);
867}
868
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700869void rsi_ContextBindFont(Context *rsc, RsFont vfont)
870{
871 Font *font = static_cast<Font *>(vfont);
872 rsc->setFont(font);
873}
874
Jason Samsa4a54e42009-06-10 18:39:40 -0700875void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700876{
877 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700878 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700879}
Jason Sams326e0dd2009-05-22 14:03:28 -0700880
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700881void rsi_GetName(Context *rsc, void * obj, const char **name)
882{
883 ObjectBase *ob = static_cast<ObjectBase *>(obj);
884 (*name) = ob->getName();
885}
886
Jason Sams707aaf32009-08-18 14:14:24 -0700887void rsi_ObjDestroy(Context *rsc, void *obj)
888{
889 ObjectBase *ob = static_cast<ObjectBase *>(obj);
890 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700891 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700892}
893
Jason Sams86f1b232009-09-24 17:38:20 -0700894void rsi_ContextPause(Context *rsc)
895{
896 rsc->pause();
897}
898
899void rsi_ContextResume(Context *rsc)
900{
901 rsc->resume();
902}
903
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700904void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800905{
Mathias Agopianfa402862010-02-12 14:04:35 -0800906 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800907}
908
Jason Sams15832442009-11-15 12:14:26 -0800909void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800910{
Jason Sams15832442009-11-15 12:14:26 -0800911 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800912}
913
Jason Samsc21cf402009-11-17 17:26:46 -0800914void rsi_ContextDump(Context *rsc, int32_t bits)
915{
916 ObjectBase::dumpAll(rsc);
917}
918
Jason Samsa2cf7552010-03-03 13:03:18 -0800919const char * rsi_ContextGetError(Context *rsc, RsError *e)
920{
921 const char *msg = rsc->getError(e);
922 if (*e != RS_ERROR_NONE) {
923 LOGE("RS Error %i %s", *e, msg);
924 }
925 return msg;
926}
927
Jason Sams326e0dd2009-05-22 14:03:28 -0700928}
929}
930
931
Jason Sams4820e8b2010-02-09 16:05:07 -0800932RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700933{
Jason Sams4820e8b2010-02-09 16:05:07 -0800934 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700935 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800936 Context *rsc = new Context(dev, false, false);
937 return rsc;
938}
939
940RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
941{
942 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
943 Device * dev = static_cast<Device *>(vdev);
944 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700945 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700946 return rsc;
947}
948
949void rsContextDestroy(RsContext vrsc)
950{
951 Context * rsc = static_cast<Context *>(vrsc);
952 delete rsc;
953}
954
Jason Sams8c401ef2009-10-06 13:58:47 -0700955uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
956{
957 Context * rsc = static_cast<Context *>(vrsc);
958 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
959}
960
961void rsContextInitToClient(RsContext vrsc)
962{
963 Context * rsc = static_cast<Context *>(vrsc);
964 rsc->initToClient();
965}
966
967void rsContextDeinitToClient(RsContext vrsc)
968{
969 Context * rsc = static_cast<Context *>(vrsc);
970 rsc->deinitToClient();
971}
972