blob: 4752b35fd7ea23f3852d866ba29b54331d71ae7e [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 Sams605048a2010-09-30 18:15:52 -0700285 rsc->mTlsStruct = new ScriptTLSStruct;
286 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700287 LOGE("Error allocating tls storage");
288 return NULL;
289 }
Jason Sams605048a2010-09-30 18:15:52 -0700290 rsc->mTlsStruct->mContext = rsc;
291 rsc->mTlsStruct->mScript = NULL;
292 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700293 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 Sams605048a2010-09-30 18:15:52 -0700364 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700365
Jason Sams4c5f99e2010-09-14 14:59:03 -0700366 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700367 return NULL;
368}
369
Jason Sams7bf29dd2010-07-19 15:38:19 -0700370void * Context::helperThreadProc(void *vrsc)
371{
372 Context *rsc = static_cast<Context *>(vrsc);
373 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
374
Jason Sams18133402010-07-20 15:09:00 -0700375 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700376
377 rsc->mWorkers.mLaunchSignals[idx].init();
378 rsc->mWorkers.mNativeThreadId[idx] = gettid();
379
Jason Sams8d957fa2010-09-28 14:41:22 -0700380#if 0
381 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
382 cpu_set_t cpuset;
383 memset(&cpuset, 0, sizeof(cpuset));
384 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
385 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700386 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700387 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
388#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700389
390 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700391 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
392 if (status) {
393 LOGE("pthread_setspecific %i", status);
394 }
395
Jason Sams7bf29dd2010-07-19 15:38:19 -0700396 while(rsc->mRunning) {
397 rsc->mWorkers.mLaunchSignals[idx].wait();
398 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700399 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
400 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700401 android_atomic_dec(&rsc->mWorkers.mRunningCount);
402 rsc->mWorkers.mCompleteSignal.set();
403 }
Jason Sams18133402010-07-20 15:09:00 -0700404
405 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700406 return NULL;
407}
408
409void Context::launchThreads(WorkerCallback_t cbk, void *data)
410{
411 mWorkers.mLaunchData = data;
412 mWorkers.mLaunchCallback = cbk;
413 mWorkers.mRunningCount = (int)mWorkers.mCount;
414 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
415 mWorkers.mLaunchSignals[ct].set();
416 }
417 while(mWorkers.mRunningCount) {
418 mWorkers.mCompleteSignal.wait();
419 }
420}
421
Jason Sams15832442009-11-15 12:14:26 -0800422void Context::setPriority(int32_t p)
423{
424 // Note: If we put this in the proper "background" policy
425 // the wallpapers can become completly unresponsive at times.
426 // This is probably not what we want for something the user is actively
427 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800428 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800429#if 0
430 SchedPolicy pol = SP_FOREGROUND;
431 if (p > 0) {
432 pol = SP_BACKGROUND;
433 }
434 if (!set_sched_policy(mNativeThreadId, pol)) {
435 // success; reset the priority as well
436 }
437#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700438 setpriority(PRIO_PROCESS, mNativeThreadId, p);
439 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
440 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
441 }
Jason Sams15832442009-11-15 12:14:26 -0800442#endif
443}
444
Jason Sams4820e8b2010-02-09 16:05:07 -0800445Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700446{
Jason Samsfb03a222009-10-15 16:47:31 -0700447 pthread_mutex_lock(&gInitMutex);
448
Jason Sams326e0dd2009-05-22 14:03:28 -0700449 dev->addContext(this);
450 mDev = dev;
451 mRunning = false;
452 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700453 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700454 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700455 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800456 mError = RS_ERROR_NONE;
457 mErrorMsg = NULL;
458
Jason Sams613cad12009-11-12 15:10:25 -0800459 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800460 memset(&mGL, 0, sizeof(mGL));
461 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700462
Jason Samsa658e902009-06-04 14:35:01 -0700463 int status;
464 pthread_attr_t threadAttr;
465
Jason Samsfb03a222009-10-15 16:47:31 -0700466 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700467 status = pthread_key_create(&gThreadTLSKey, NULL);
468 if (status) {
469 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700470 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700471 return;
472 }
Jason Samse5769102009-06-19 16:03:18 -0700473 }
Jason Samsfb03a222009-10-15 16:47:31 -0700474 gThreadTLSKeyCount++;
475 pthread_mutex_unlock(&gInitMutex);
476
477 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700478
Jason Samsa658e902009-06-04 14:35:01 -0700479 status = pthread_attr_init(&threadAttr);
480 if (status) {
481 LOGE("Failed to init thread attribute.");
482 return;
483 }
484
Jason Sams613cad12009-11-12 15:10:25 -0800485 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700486
Jason Sams24371d92009-08-19 12:17:14 -0700487 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700488 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700489
Jason Sams7c7c78a2010-07-26 17:12:55 -0700490 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
491 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
492 if (cpu < 2) cpu = 0;
493
494 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700495 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
496 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
497 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
498 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700499 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700500 if (status) {
501 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700502 return;
503 }
Jason Sams18133402010-07-20 15:09:00 -0700504 while(!mRunning) {
505 usleep(100);
506 }
507
Jason Sams8d957fa2010-09-28 14:41:22 -0700508 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700509 mWorkers.mRunningCount = 0;
510 mWorkers.mLaunchCount = 0;
511 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
512 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
513 if (status) {
514 mWorkers.mCount = ct;
515 LOGE("Created fewer than expected number of RS threads.");
516 break;
517 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700518 }
519
Jason Sams326e0dd2009-05-22 14:03:28 -0700520
Jason Samsa658e902009-06-04 14:35:01 -0700521 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700522}
523
524Context::~Context()
525{
Jason Sams8c0ee652009-08-25 14:49:07 -0700526 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700527 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700528 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700529 void *res;
530
Jason Sams8c0ee652009-08-25 14:49:07 -0700531 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700532 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700533
Jason Samsfb03a222009-10-15 16:47:31 -0700534 // Global structure cleanup.
535 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700536 if (mDev) {
537 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700538 --gThreadTLSKeyCount;
539 if (!gThreadTLSKeyCount) {
540 pthread_key_delete(gThreadTLSKey);
541 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800542 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700543 }
Jason Samsfb03a222009-10-15 16:47:31 -0700544 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700545}
546
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700547void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800548{
Jason Sams4820e8b2010-02-09 16:05:07 -0800549 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800550
Jason Sams458f2dc2009-11-03 13:58:36 -0800551 EGLBoolean ret;
552 if (mEGL.mSurface != NULL) {
553 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
554 checkEglError("eglMakeCurrent", ret);
555
556 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
557 checkEglError("eglDestroySurface", ret);
558
559 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800560 mWidth = 0;
561 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800562 }
563
564 mWndSurface = sur;
565 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700566 mWidth = w;
567 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800568 bool first = false;
569 if (!mEGL.mContext) {
570 first = true;
571 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800572 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800573 pthread_mutex_unlock(&gInitMutex);
574 }
575
Jason Sams458f2dc2009-11-03 13:58:36 -0800576 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
577 checkEglError("eglCreateWindowSurface");
578 if (mEGL.mSurface == EGL_NO_SURFACE) {
579 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
580 }
581
582 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
583 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800584
Jason Sams771565f2010-05-14 15:30:29 -0700585 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800586
Jason Sams613cad12009-11-12 15:10:25 -0800587 if (first) {
588 mGL.mVersion = glGetString(GL_VERSION);
589 mGL.mVendor = glGetString(GL_VENDOR);
590 mGL.mRenderer = glGetString(GL_RENDERER);
591 mGL.mExtensions = glGetString(GL_EXTENSIONS);
592
593 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
594 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800595 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800596 LOGV("GL Renderer %s", mGL.mRenderer);
597 //LOGV("GL Extensions %s", mGL.mExtensions);
598
Jason Samsc460e552009-11-25 13:22:07 -0800599 const char *verptr = NULL;
600 if (strlen((const char *)mGL.mVersion) > 9) {
601 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
602 verptr = (const char *)mGL.mVersion + 12;
603 }
604 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
605 verptr = (const char *)mGL.mVersion + 9;
606 }
607 }
608
609 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800610 LOGE("Error, OpenGL ES Lite not supported");
611 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800612 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800613 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800614
615 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
616 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
617 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
618
619 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
620 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
621
622 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
623 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800624
625 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700626 mGL.EXT_texture_max_aniso = 1.0f;
627 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
628 if(hasAniso) {
629 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
630 }
Jason Sams613cad12009-11-12 15:10:25 -0800631 }
632
Jason Sams458f2dc2009-11-03 13:58:36 -0800633 }
634}
635
Jason Sams86f1b232009-09-24 17:38:20 -0700636void Context::pause()
637{
Jason Sams4820e8b2010-02-09 16:05:07 -0800638 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700639 mPaused = true;
640}
641
642void Context::resume()
643{
Jason Sams4820e8b2010-02-09 16:05:07 -0800644 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700645 mPaused = false;
646}
647
Jason Sams326e0dd2009-05-22 14:03:28 -0700648void Context::setRootScript(Script *s)
649{
Jason Sams4820e8b2010-02-09 16:05:07 -0800650 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700651 mRootScript.set(s);
652}
653
Jason Samsccc010b2010-05-13 18:30:11 -0700654void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700655{
Jason Sams4820e8b2010-02-09 16:05:07 -0800656 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700657 if (pfs == NULL) {
658 mFragmentStore.set(mStateFragmentStore.mDefault);
659 } else {
660 mFragmentStore.set(pfs);
661 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700662}
663
664void Context::setFragment(ProgramFragment *pf)
665{
Jason Sams4820e8b2010-02-09 16:05:07 -0800666 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700667 if (pf == NULL) {
668 mFragment.set(mStateFragment.mDefault);
669 } else {
670 mFragment.set(pf);
671 }
Jason Samscfb1d112009-08-05 13:57:03 -0700672}
673
Jason Sams5fd09d82009-09-23 13:57:02 -0700674void Context::setRaster(ProgramRaster *pr)
675{
Jason Sams4820e8b2010-02-09 16:05:07 -0800676 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700677 if (pr == NULL) {
678 mRaster.set(mStateRaster.mDefault);
679 } else {
680 mRaster.set(pr);
681 }
682}
683
Jason Sams326e0dd2009-05-22 14:03:28 -0700684void Context::setVertex(ProgramVertex *pv)
685{
Jason Sams4820e8b2010-02-09 16:05:07 -0800686 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700687 if (pv == NULL) {
688 mVertex.set(mStateVertex.mDefault);
689 } else {
690 mVertex.set(pv);
691 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700692}
693
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700694void Context::setFont(Font *f)
695{
696 rsAssert(mIsGraphicsContext);
697 if (f == NULL) {
698 mFont.set(mStateFont.mDefault);
699 } else {
700 mFont.set(f);
701 }
702}
703
Jason Samsa4a54e42009-06-10 18:39:40 -0700704void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700705{
706 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700707 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700708 mNames.add(obj);
709}
710
711void Context::removeName(ObjectBase *obj)
712{
713 for(size_t ct=0; ct < mNames.size(); ct++) {
714 if (obj == mNames[ct]) {
715 mNames.removeAt(ct);
716 return;
717 }
718 }
719}
720
Jason Sams8c401ef2009-10-06 13:58:47 -0700721uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
722{
723 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700724 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700725 if (!wait) {
726 if (mIO.mToClient.isEmpty()) {
727 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700728 return 0;
729 }
730 }
731
732 //LOGE("getMessageToClient 2 con=%p", this);
733 uint32_t bytesData = 0;
734 uint32_t commandID = 0;
735 const void *d = mIO.mToClient.get(&commandID, &bytesData);
736 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
737
738 *receiveLen = bytesData;
739 if (bufferLen >= bytesData) {
740 memcpy(data, d, bytesData);
741 mIO.mToClient.next();
742 return commandID;
743 }
744 return 0;
745}
746
747bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
748{
749 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
750 if (cmdID == 0) {
751 LOGE("Attempting to send invalid command 0 to client.");
752 return false;
753 }
754 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700755 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700756 // Not enough room, and not waiting.
757 return false;
758 }
759 }
760 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700761 if (len > 0) {
762 void *p = mIO.mToClient.reserve(len);
763 memcpy(p, data, len);
764 mIO.mToClient.commit(cmdID, len);
765 } else {
766 mIO.mToClient.commit(cmdID, 0);
767 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700768 //LOGE("sendMessageToClient 3");
769 return true;
770}
771
772void Context::initToClient()
773{
774 while(!mRunning) {
775 usleep(100);
776 }
777}
778
779void Context::deinitToClient()
780{
781 mIO.mToClient.shutdown();
782}
Jason Sams50869382009-08-18 17:07:09 -0700783
Jason Samsa2cf7552010-03-03 13:03:18 -0800784const char * Context::getError(RsError *err)
785{
786 *err = mError;
787 mError = RS_ERROR_NONE;
788 if (*err != RS_ERROR_NONE) {
789 return mErrorMsg;
790 }
791 return NULL;
792}
793
794void Context::setError(RsError e, const char *msg)
795{
796 mError = e;
797 mErrorMsg = msg;
798}
799
800
Jason Sams13e26342009-11-24 12:26:35 -0800801void Context::dumpDebug() const
802{
803 LOGE("RS Context debug %p", this);
804 LOGE("RS Context debug");
805
806 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700807 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800808 LOGE(" GL vendor: %s", mGL.mVendor);
809 LOGE(" GL renderer: %s", mGL.mRenderer);
810 LOGE(" GL Version: %s", mGL.mVersion);
811 LOGE(" GL Extensions: %s", mGL.mExtensions);
812 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
813 LOGE(" RS width %i, height %i", mWidth, mHeight);
814 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
815 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
816
Jason Sams4815c0d2009-12-15 12:58:36 -0800817 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
818 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
819 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
820 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800821}
Jason Samsa4a54e42009-06-10 18:39:40 -0700822
Jason Sams326e0dd2009-05-22 14:03:28 -0700823///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700824//
Jason Sams326e0dd2009-05-22 14:03:28 -0700825
826namespace android {
827namespace renderscript {
828
Jason Sams8c880902010-06-15 12:15:57 -0700829void rsi_ContextFinish(Context *rsc)
830{
831}
Jason Sams326e0dd2009-05-22 14:03:28 -0700832
833void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
834{
835 Script *s = static_cast<Script *>(vs);
836 rsc->setRootScript(s);
837}
838
839void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
840{
841 Sampler *s = static_cast<Sampler *>(vs);
842
843 if (slot > RS_MAX_SAMPLER_SLOT) {
844 LOGE("Invalid sampler slot");
845 return;
846 }
847
848 s->bindToContext(&rsc->mStateSampler, slot);
849}
850
Jason Samsccc010b2010-05-13 18:30:11 -0700851void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700852{
Jason Samsccc010b2010-05-13 18:30:11 -0700853 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700854 rsc->setFragmentStore(pfs);
855}
856
857void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
858{
859 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
860 rsc->setFragment(pf);
861}
862
Jason Sams5fd09d82009-09-23 13:57:02 -0700863void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
864{
865 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
866 rsc->setRaster(pr);
867}
868
Jason Sams326e0dd2009-05-22 14:03:28 -0700869void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
870{
871 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
872 rsc->setVertex(pv);
873}
874
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700875void rsi_ContextBindFont(Context *rsc, RsFont vfont)
876{
877 Font *font = static_cast<Font *>(vfont);
878 rsc->setFont(font);
879}
880
Jason Samsa4a54e42009-06-10 18:39:40 -0700881void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700882{
883 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700884 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700885}
Jason Sams326e0dd2009-05-22 14:03:28 -0700886
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700887void rsi_GetName(Context *rsc, void * obj, const char **name)
888{
889 ObjectBase *ob = static_cast<ObjectBase *>(obj);
890 (*name) = ob->getName();
891}
892
Jason Sams707aaf32009-08-18 14:14:24 -0700893void rsi_ObjDestroy(Context *rsc, void *obj)
894{
895 ObjectBase *ob = static_cast<ObjectBase *>(obj);
896 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700897 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700898}
899
Jason Sams86f1b232009-09-24 17:38:20 -0700900void rsi_ContextPause(Context *rsc)
901{
902 rsc->pause();
903}
904
905void rsi_ContextResume(Context *rsc)
906{
907 rsc->resume();
908}
909
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700910void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800911{
Mathias Agopianfa402862010-02-12 14:04:35 -0800912 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800913}
914
Jason Sams15832442009-11-15 12:14:26 -0800915void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800916{
Jason Sams15832442009-11-15 12:14:26 -0800917 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800918}
919
Jason Samsc21cf402009-11-17 17:26:46 -0800920void rsi_ContextDump(Context *rsc, int32_t bits)
921{
922 ObjectBase::dumpAll(rsc);
923}
924
Jason Samsa2cf7552010-03-03 13:03:18 -0800925const char * rsi_ContextGetError(Context *rsc, RsError *e)
926{
927 const char *msg = rsc->getError(e);
928 if (*e != RS_ERROR_NONE) {
929 LOGE("RS Error %i %s", *e, msg);
930 }
931 return msg;
932}
933
Jason Sams326e0dd2009-05-22 14:03:28 -0700934}
935}
936
937
Jason Sams4820e8b2010-02-09 16:05:07 -0800938RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700939{
Jason Sams4820e8b2010-02-09 16:05:07 -0800940 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700941 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800942 Context *rsc = new Context(dev, false, false);
943 return rsc;
944}
945
946RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
947{
948 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
949 Device * dev = static_cast<Device *>(vdev);
950 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700951 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700952 return rsc;
953}
954
955void rsContextDestroy(RsContext vrsc)
956{
957 Context * rsc = static_cast<Context *>(vrsc);
958 delete rsc;
959}
960
Jason Sams8c401ef2009-10-06 13:58:47 -0700961uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
962{
963 Context * rsc = static_cast<Context *>(vrsc);
964 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
965}
966
967void rsContextInitToClient(RsContext vrsc)
968{
969 Context * rsc = static_cast<Context *>(vrsc);
970 rsc->initToClient();
971}
972
973void rsContextDeinitToClient(RsContext vrsc)
974{
975 Context * rsc = static_cast<Context *>(vrsc);
976 rsc->deinitToClient();
977}
978