blob: 5327aacbfc973245714c0089252484782d0f2764 [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>
36
Jason Sams326e0dd2009-05-22 14:03:28 -070037using namespace android;
38using namespace android::renderscript;
39
Jason Samse5769102009-06-19 16:03:18 -070040pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070041uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070042uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070043pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070044
Jason Sams33b6e3b2009-10-27 14:44:31 -070045static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
46 if (returnVal != EGL_TRUE) {
47 fprintf(stderr, "%s() returned %d\n", op, returnVal);
48 }
49
50 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
51 = eglGetError()) {
52 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
53 error);
54 }
55}
56
Jason Samsc460e552009-11-25 13:22:07 -080057void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070058{
Jason Samsafcb25c2009-08-25 11:34:49 -070059 mEGL.mNumConfigs = -1;
60 EGLint configAttribs[128];
61 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080062 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070063
Jason Samsafcb25c2009-08-25 11:34:49 -070064 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsafcb25c2009-08-25 11:34:49 -070066 configAttribsPtr[0] = EGL_SURFACE_TYPE;
67 configAttribsPtr[1] = EGL_WINDOW_BIT;
68 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070069
Jason Samsc460e552009-11-25 13:22:07 -080070 if (useGL2) {
71 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
72 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
73 configAttribsPtr += 2;
74 }
75
Jason Samsafcb25c2009-08-25 11:34:49 -070076 if (mUseDepth) {
77 configAttribsPtr[0] = EGL_DEPTH_SIZE;
78 configAttribsPtr[1] = 16;
79 configAttribsPtr += 2;
80 }
Jason Sams9397e302009-08-27 20:23:34 -070081
Jason Sams5fd09d82009-09-23 13:57:02 -070082 if (mDev->mForceSW) {
83 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
84 configAttribsPtr[1] = EGL_SLOW_CONFIG;
85 configAttribsPtr += 2;
86 }
87
Jason Samsafcb25c2009-08-25 11:34:49 -070088 configAttribsPtr[0] = EGL_NONE;
89 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070090
Jason Sams4c5f99e2010-09-14 14:59:03 -070091 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -070092 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070093 checkEglError("eglGetDisplay");
94
Jason Samsafcb25c2009-08-25 11:34:49 -070095 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070096 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070097
98 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
99 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700100 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700101 }
102 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
103
Jason Samsafcb25c2009-08-25 11:34:49 -0700104
Jason Samsc460e552009-11-25 13:22:07 -0800105 if (useGL2) {
106 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
107 } else {
108 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
109 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700110 checkEglError("eglCreateContext");
111 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700112 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700113 }
114 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams33b6e3b2009-10-27 14:44:31 -0700117void Context::deinitEGL()
118{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700119 LOGV("%p, deinitEGL", this);
Jason Sams613cad12009-11-12 15:10:25 -0800120 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700121 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
122 checkEglError("eglDestroyContext");
123
124 gGLContextCount--;
125 if (!gGLContextCount) {
126 eglTerminate(mEGL.mDisplay);
127 }
128}
129
130
Jason Samsc61346b2010-05-28 18:23:22 -0700131uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700132{
133 ObjectBaseRef<ProgramFragment> frag(mFragment);
134 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700135 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700136 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700137 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700138
Jason Samsc61346b2010-05-28 18:23:22 -0700139 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700140
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700141 mFragment.set(frag);
142 mVertex.set(vtx);
143 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700144 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700145 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700146 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700147}
148
Jason Samsd01d9702009-12-23 14:35:29 -0800149void Context::checkError(const char *msg) const
150{
151 GLenum err = glGetError();
152 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700153 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800154 }
155}
Jason Sams10308932009-06-09 12:15:30 -0700156
Jason Sams2dca84d2009-12-09 11:05:45 -0800157uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700158{
Jason Sams771565f2010-05-14 15:30:29 -0700159 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700160
Jason Sams2dca84d2009-12-09 11:05:45 -0800161 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700162 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700163 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700164
Jason Samsd01d9702009-12-23 14:35:29 -0800165 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800166 if (mError != RS_ERROR_NONE) {
167 // If we have an error condition we stop rendering until
168 // somthing changes that might fix it.
169 ret = 0;
170 }
Jason Samscfb1d112009-08-05 13:57:03 -0700171 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700172}
173
Jason Sams24371d92009-08-19 12:17:14 -0700174uint64_t Context::getTime() const
175{
176 struct timespec t;
177 clock_gettime(CLOCK_MONOTONIC, &t);
178 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
179}
180
181void Context::timerReset()
182{
183 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
184 mTimers[ct] = 0;
185 }
186}
187
188void Context::timerInit()
189{
190 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700191 mTimeFrame = mTimeLast;
192 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700193 mTimerActive = RS_TIMER_INTERNAL;
194 timerReset();
195}
196
Jason Sams1d54f102009-09-03 15:43:13 -0700197void Context::timerFrame()
198{
199 mTimeLastFrame = mTimeFrame;
200 mTimeFrame = getTime();
201}
202
Jason Sams24371d92009-08-19 12:17:14 -0700203void Context::timerSet(Timers tm)
204{
205 uint64_t last = mTimeLast;
206 mTimeLast = getTime();
207 mTimers[mTimerActive] += mTimeLast - last;
208 mTimerActive = tm;
209}
210
211void Context::timerPrint()
212{
213 double total = 0;
214 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
215 total += mTimers[ct];
216 }
Jason Sams1d54f102009-09-03 15:43:13 -0700217 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800218 mTimeMSLastFrame = frame / 1000000;
219 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
220 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700221
Jason Sams2dca84d2009-12-09 11:05:45 -0800222
223 if (props.mLogTimes) {
224 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
225 mTimeMSLastFrame,
226 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
227 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
228 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
229 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
230 }
Jason Sams24371d92009-08-19 12:17:14 -0700231}
232
Jason Samsa2cf7552010-03-03 13:03:18 -0800233bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700234{
Jason Samsc460e552009-11-25 13:22:07 -0800235 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800236 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
237 LOGE("Context::setupCheck() 1 fail");
238 return false;
239 }
Jason Samsc460e552009-11-25 13:22:07 -0800240
241 mFragmentStore->setupGL2(this, &mStateFragmentStore);
242 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
243 mRaster->setupGL2(this, &mStateRaster);
244 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
245
246 } else {
247 mFragmentStore->setupGL(this, &mStateFragmentStore);
248 mFragment->setupGL(this, &mStateFragment);
249 mRaster->setupGL(this, &mStateRaster);
250 mVertex->setupGL(this, &mStateVertex);
251 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800252 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700253}
254
Jason Sams1fddd902009-09-25 15:25:00 -0700255static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700256{
257 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700258 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 return 0 != strcmp(buf, "0");
260}
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700262void Context::displayDebugStats()
263{
264 char buffer[128];
265 sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700266 float oldR, oldG, oldB, oldA;
267 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700268
269 float shadowCol = 0.2f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700270 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700271 mStateFont.renderText(buffer, 5, getHeight() - 5);
272
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700273 float textCol = 0.9f;
274 mStateFont.setFontColor(textCol, textCol, textCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700275 mStateFont.renderText(buffer, 4, getHeight() - 6);
276
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700277 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700278}
279
Jason Sams326e0dd2009-05-22 14:03:28 -0700280void * Context::threadProc(void *vrsc)
281{
282 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800283 rsc->mNativeThreadId = gettid();
284
285 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800286 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700287
Jason Sams1fddd902009-09-25 15:25:00 -0700288 rsc->props.mLogTimes = getProp("debug.rs.profile");
289 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800290 rsc->props.mLogObjects = getProp("debug.rs.object");
291 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700292 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700293
Jason Samse5769102009-06-19 16:03:18 -0700294 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
295 if (!tlsStruct) {
296 LOGE("Error allocating tls storage");
297 return NULL;
298 }
299 tlsStruct->mContext = rsc;
300 tlsStruct->mScript = NULL;
301 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
302 if (status) {
303 LOGE("pthread_setspecific %i", status);
304 }
305
Jason Sams4820e8b2010-02-09 16:05:07 -0800306 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700307 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800308 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700309 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800310 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700311 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800312 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700313 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800314 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700315 rsc->mStateFont.init(rsc);
316 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800317 rsc->mStateVertexArray.init(rsc);
318 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700319
Jason Sams326e0dd2009-05-22 14:03:28 -0700320 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700321 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700322 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700323 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700324 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800325 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700326
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800328 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800329 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700330
331 if(rsc->props.mLogVisual) {
332 rsc->displayDebugStats();
333 }
334
Jason Sams2dca84d2009-12-09 11:05:45 -0800335 mDraw = targetTime && !rsc->mPaused;
336 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700337 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800338 rsc->timerFrame();
339 rsc->timerSet(RS_TIMER_INTERNAL);
340 rsc->timerPrint();
341 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700342 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800343 if (rsc->mThreadPriority > 0 && targetTime) {
344 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
345 if (t > 0) {
346 usleep(t);
347 }
348 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 }
350
Jason Sams4c5f99e2010-09-14 14:59:03 -0700351 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800352 if (rsc->mIsGraphicsContext) {
353 rsc->mRaster.clear();
354 rsc->mFragment.clear();
355 rsc->mVertex.clear();
356 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700357 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800358 rsc->mRootScript.clear();
359 rsc->mStateRaster.deinit(rsc);
360 rsc->mStateVertex.deinit(rsc);
361 rsc->mStateFragment.deinit(rsc);
362 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700363 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800364 }
Jason Samse514b452009-09-25 14:51:22 -0700365 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700366
Jason Sams4820e8b2010-02-09 16:05:07 -0800367 if (rsc->mIsGraphicsContext) {
368 pthread_mutex_lock(&gInitMutex);
369 rsc->deinitEGL();
370 pthread_mutex_unlock(&gInitMutex);
371 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700372
Jason Sams4c5f99e2010-09-14 14:59:03 -0700373 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700374 return NULL;
375}
376
Jason Sams7bf29dd2010-07-19 15:38:19 -0700377void * Context::helperThreadProc(void *vrsc)
378{
379 Context *rsc = static_cast<Context *>(vrsc);
380 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
381
Jason Sams18133402010-07-20 15:09:00 -0700382 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700383
384 rsc->mWorkers.mLaunchSignals[idx].init();
385 rsc->mWorkers.mNativeThreadId[idx] = gettid();
386
387 //cpu_set_t cpset[16];
388 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
389 //LOGE("ret = %i", ret);
390
391//sched_setaffinity
392
393 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
394 while(rsc->mRunning) {
395 rsc->mWorkers.mLaunchSignals[idx].wait();
396 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700397 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
398 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700399 android_atomic_dec(&rsc->mWorkers.mRunningCount);
400 rsc->mWorkers.mCompleteSignal.set();
401 }
Jason Sams18133402010-07-20 15:09:00 -0700402
403 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700404 return NULL;
405}
406
407void Context::launchThreads(WorkerCallback_t cbk, void *data)
408{
409 mWorkers.mLaunchData = data;
410 mWorkers.mLaunchCallback = cbk;
411 mWorkers.mRunningCount = (int)mWorkers.mCount;
412 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
413 mWorkers.mLaunchSignals[ct].set();
414 }
415 while(mWorkers.mRunningCount) {
416 mWorkers.mCompleteSignal.wait();
417 }
418}
419
Jason Sams15832442009-11-15 12:14:26 -0800420void Context::setPriority(int32_t p)
421{
422 // Note: If we put this in the proper "background" policy
423 // the wallpapers can become completly unresponsive at times.
424 // This is probably not what we want for something the user is actively
425 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800426 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800427#if 0
428 SchedPolicy pol = SP_FOREGROUND;
429 if (p > 0) {
430 pol = SP_BACKGROUND;
431 }
432 if (!set_sched_policy(mNativeThreadId, pol)) {
433 // success; reset the priority as well
434 }
435#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700436 setpriority(PRIO_PROCESS, mNativeThreadId, p);
437 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
438 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
439 }
Jason Sams15832442009-11-15 12:14:26 -0800440#endif
441}
442
Jason Sams4820e8b2010-02-09 16:05:07 -0800443Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700444{
Jason Samsfb03a222009-10-15 16:47:31 -0700445 pthread_mutex_lock(&gInitMutex);
446
Jason Sams326e0dd2009-05-22 14:03:28 -0700447 dev->addContext(this);
448 mDev = dev;
449 mRunning = false;
450 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700451 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700452 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700453 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800454 mError = RS_ERROR_NONE;
455 mErrorMsg = NULL;
456
Jason Sams613cad12009-11-12 15:10:25 -0800457 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800458 memset(&mGL, 0, sizeof(mGL));
459 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700460
Jason Samsa658e902009-06-04 14:35:01 -0700461 int status;
462 pthread_attr_t threadAttr;
463
Jason Samsfb03a222009-10-15 16:47:31 -0700464 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700465 status = pthread_key_create(&gThreadTLSKey, NULL);
466 if (status) {
467 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700468 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700469 return;
470 }
Jason Samse5769102009-06-19 16:03:18 -0700471 }
Jason Samsfb03a222009-10-15 16:47:31 -0700472 gThreadTLSKeyCount++;
473 pthread_mutex_unlock(&gInitMutex);
474
475 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700476
Jason Samsa658e902009-06-04 14:35:01 -0700477 status = pthread_attr_init(&threadAttr);
478 if (status) {
479 LOGE("Failed to init thread attribute.");
480 return;
481 }
482
Jason Sams613cad12009-11-12 15:10:25 -0800483 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700484
Jason Sams24371d92009-08-19 12:17:14 -0700485 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700486 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700487
Jason Sams7c7c78a2010-07-26 17:12:55 -0700488 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
489 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
490 if (cpu < 2) cpu = 0;
491
492 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700493 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
494 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
495 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
496 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700497 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700498 if (status) {
499 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700500 return;
501 }
Jason Sams18133402010-07-20 15:09:00 -0700502 while(!mRunning) {
503 usleep(100);
504 }
505
Jason Sams7bf29dd2010-07-19 15:38:19 -0700506 mWorkers.mRunningCount = 0;
507 mWorkers.mLaunchCount = 0;
508 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
509 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
510 if (status) {
511 mWorkers.mCount = ct;
512 LOGE("Created fewer than expected number of RS threads.");
513 break;
514 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700515 }
516
Jason Sams326e0dd2009-05-22 14:03:28 -0700517
Jason Samsa658e902009-06-04 14:35:01 -0700518 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700519}
520
521Context::~Context()
522{
Jason Sams8c0ee652009-08-25 14:49:07 -0700523 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700524 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700525 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700526 void *res;
527
Jason Sams8c0ee652009-08-25 14:49:07 -0700528 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700529 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700530
Jason Samsfb03a222009-10-15 16:47:31 -0700531 // Global structure cleanup.
532 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700533 if (mDev) {
534 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700535 --gThreadTLSKeyCount;
536 if (!gThreadTLSKeyCount) {
537 pthread_key_delete(gThreadTLSKey);
538 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800539 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700540 }
Jason Samsfb03a222009-10-15 16:47:31 -0700541 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700542}
543
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700544void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800545{
Jason Sams4820e8b2010-02-09 16:05:07 -0800546 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800547
Jason Sams458f2dc2009-11-03 13:58:36 -0800548 EGLBoolean ret;
549 if (mEGL.mSurface != NULL) {
550 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
551 checkEglError("eglMakeCurrent", ret);
552
553 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
554 checkEglError("eglDestroySurface", ret);
555
556 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800557 mWidth = 0;
558 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800559 }
560
561 mWndSurface = sur;
562 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700563 mWidth = w;
564 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800565 bool first = false;
566 if (!mEGL.mContext) {
567 first = true;
568 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800569 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800570 pthread_mutex_unlock(&gInitMutex);
571 }
572
Jason Sams458f2dc2009-11-03 13:58:36 -0800573 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
574 checkEglError("eglCreateWindowSurface");
575 if (mEGL.mSurface == EGL_NO_SURFACE) {
576 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
577 }
578
579 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
580 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800581
Jason Sams771565f2010-05-14 15:30:29 -0700582 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800583
Jason Sams613cad12009-11-12 15:10:25 -0800584 if (first) {
585 mGL.mVersion = glGetString(GL_VERSION);
586 mGL.mVendor = glGetString(GL_VENDOR);
587 mGL.mRenderer = glGetString(GL_RENDERER);
588 mGL.mExtensions = glGetString(GL_EXTENSIONS);
589
590 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
591 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800592 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800593 LOGV("GL Renderer %s", mGL.mRenderer);
594 //LOGV("GL Extensions %s", mGL.mExtensions);
595
Jason Samsc460e552009-11-25 13:22:07 -0800596 const char *verptr = NULL;
597 if (strlen((const char *)mGL.mVersion) > 9) {
598 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
599 verptr = (const char *)mGL.mVersion + 12;
600 }
601 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
602 verptr = (const char *)mGL.mVersion + 9;
603 }
604 }
605
606 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800607 LOGE("Error, OpenGL ES Lite not supported");
608 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800609 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800610 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800611
612 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
613 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
614 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
615
616 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
617 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
618
619 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
620 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800621
622 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800623 }
624
Jason Sams458f2dc2009-11-03 13:58:36 -0800625 }
626}
627
Jason Sams86f1b232009-09-24 17:38:20 -0700628void Context::pause()
629{
Jason Sams4820e8b2010-02-09 16:05:07 -0800630 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700631 mPaused = true;
632}
633
634void Context::resume()
635{
Jason Sams4820e8b2010-02-09 16:05:07 -0800636 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700637 mPaused = false;
638}
639
Jason Sams326e0dd2009-05-22 14:03:28 -0700640void Context::setRootScript(Script *s)
641{
Jason Sams4820e8b2010-02-09 16:05:07 -0800642 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700643 mRootScript.set(s);
644}
645
Jason Samsccc010b2010-05-13 18:30:11 -0700646void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700647{
Jason Sams4820e8b2010-02-09 16:05:07 -0800648 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700649 if (pfs == NULL) {
650 mFragmentStore.set(mStateFragmentStore.mDefault);
651 } else {
652 mFragmentStore.set(pfs);
653 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700654}
655
656void Context::setFragment(ProgramFragment *pf)
657{
Jason Sams4820e8b2010-02-09 16:05:07 -0800658 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700659 if (pf == NULL) {
660 mFragment.set(mStateFragment.mDefault);
661 } else {
662 mFragment.set(pf);
663 }
Jason Samscfb1d112009-08-05 13:57:03 -0700664}
665
Jason Sams5fd09d82009-09-23 13:57:02 -0700666void Context::setRaster(ProgramRaster *pr)
667{
Jason Sams4820e8b2010-02-09 16:05:07 -0800668 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700669 if (pr == NULL) {
670 mRaster.set(mStateRaster.mDefault);
671 } else {
672 mRaster.set(pr);
673 }
674}
675
Jason Sams326e0dd2009-05-22 14:03:28 -0700676void Context::setVertex(ProgramVertex *pv)
677{
Jason Sams4820e8b2010-02-09 16:05:07 -0800678 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700679 if (pv == NULL) {
680 mVertex.set(mStateVertex.mDefault);
681 } else {
682 mVertex.set(pv);
683 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700684}
685
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700686void Context::setFont(Font *f)
687{
688 rsAssert(mIsGraphicsContext);
689 if (f == NULL) {
690 mFont.set(mStateFont.mDefault);
691 } else {
692 mFont.set(f);
693 }
694}
695
Jason Samsa4a54e42009-06-10 18:39:40 -0700696void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700697{
698 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700699 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700700 mNames.add(obj);
701}
702
703void Context::removeName(ObjectBase *obj)
704{
705 for(size_t ct=0; ct < mNames.size(); ct++) {
706 if (obj == mNames[ct]) {
707 mNames.removeAt(ct);
708 return;
709 }
710 }
711}
712
Jason Sams8c401ef2009-10-06 13:58:47 -0700713uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
714{
715 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700716 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700717 if (!wait) {
718 if (mIO.mToClient.isEmpty()) {
719 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700720 return 0;
721 }
722 }
723
724 //LOGE("getMessageToClient 2 con=%p", this);
725 uint32_t bytesData = 0;
726 uint32_t commandID = 0;
727 const void *d = mIO.mToClient.get(&commandID, &bytesData);
728 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
729
730 *receiveLen = bytesData;
731 if (bufferLen >= bytesData) {
732 memcpy(data, d, bytesData);
733 mIO.mToClient.next();
734 return commandID;
735 }
736 return 0;
737}
738
739bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
740{
741 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
742 if (cmdID == 0) {
743 LOGE("Attempting to send invalid command 0 to client.");
744 return false;
745 }
746 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700747 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700748 // Not enough room, and not waiting.
749 return false;
750 }
751 }
752 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700753 if (len > 0) {
754 void *p = mIO.mToClient.reserve(len);
755 memcpy(p, data, len);
756 mIO.mToClient.commit(cmdID, len);
757 } else {
758 mIO.mToClient.commit(cmdID, 0);
759 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700760 //LOGE("sendMessageToClient 3");
761 return true;
762}
763
764void Context::initToClient()
765{
766 while(!mRunning) {
767 usleep(100);
768 }
769}
770
771void Context::deinitToClient()
772{
773 mIO.mToClient.shutdown();
774}
Jason Sams50869382009-08-18 17:07:09 -0700775
Jason Samsa2cf7552010-03-03 13:03:18 -0800776const char * Context::getError(RsError *err)
777{
778 *err = mError;
779 mError = RS_ERROR_NONE;
780 if (*err != RS_ERROR_NONE) {
781 return mErrorMsg;
782 }
783 return NULL;
784}
785
786void Context::setError(RsError e, const char *msg)
787{
788 mError = e;
789 mErrorMsg = msg;
790}
791
792
Jason Sams13e26342009-11-24 12:26:35 -0800793void Context::dumpDebug() const
794{
795 LOGE("RS Context debug %p", this);
796 LOGE("RS Context debug");
797
798 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700799 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800800 LOGE(" GL vendor: %s", mGL.mVendor);
801 LOGE(" GL renderer: %s", mGL.mRenderer);
802 LOGE(" GL Version: %s", mGL.mVersion);
803 LOGE(" GL Extensions: %s", mGL.mExtensions);
804 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
805 LOGE(" RS width %i, height %i", mWidth, mHeight);
806 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
807 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
808
Jason Sams4815c0d2009-12-15 12:58:36 -0800809 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
810 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
811 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
812 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800813}
Jason Samsa4a54e42009-06-10 18:39:40 -0700814
Jason Sams326e0dd2009-05-22 14:03:28 -0700815///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700816//
Jason Sams326e0dd2009-05-22 14:03:28 -0700817
818namespace android {
819namespace renderscript {
820
Jason Sams8c880902010-06-15 12:15:57 -0700821void rsi_ContextFinish(Context *rsc)
822{
823}
Jason Sams326e0dd2009-05-22 14:03:28 -0700824
825void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
826{
827 Script *s = static_cast<Script *>(vs);
828 rsc->setRootScript(s);
829}
830
831void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
832{
833 Sampler *s = static_cast<Sampler *>(vs);
834
835 if (slot > RS_MAX_SAMPLER_SLOT) {
836 LOGE("Invalid sampler slot");
837 return;
838 }
839
840 s->bindToContext(&rsc->mStateSampler, slot);
841}
842
Jason Samsccc010b2010-05-13 18:30:11 -0700843void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700844{
Jason Samsccc010b2010-05-13 18:30:11 -0700845 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700846 rsc->setFragmentStore(pfs);
847}
848
849void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
850{
851 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
852 rsc->setFragment(pf);
853}
854
Jason Sams5fd09d82009-09-23 13:57:02 -0700855void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
856{
857 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
858 rsc->setRaster(pr);
859}
860
Jason Sams326e0dd2009-05-22 14:03:28 -0700861void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
862{
863 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
864 rsc->setVertex(pv);
865}
866
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700867void rsi_ContextBindFont(Context *rsc, RsFont vfont)
868{
869 Font *font = static_cast<Font *>(vfont);
870 rsc->setFont(font);
871}
872
Jason Samsa4a54e42009-06-10 18:39:40 -0700873void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700874{
875 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700876 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700877}
Jason Sams326e0dd2009-05-22 14:03:28 -0700878
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700879void rsi_GetName(Context *rsc, void * obj, const char **name)
880{
881 ObjectBase *ob = static_cast<ObjectBase *>(obj);
882 (*name) = ob->getName();
883}
884
Jason Sams707aaf32009-08-18 14:14:24 -0700885void rsi_ObjDestroy(Context *rsc, void *obj)
886{
887 ObjectBase *ob = static_cast<ObjectBase *>(obj);
888 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700889 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700890}
891
Jason Sams86f1b232009-09-24 17:38:20 -0700892void rsi_ContextPause(Context *rsc)
893{
894 rsc->pause();
895}
896
897void rsi_ContextResume(Context *rsc)
898{
899 rsc->resume();
900}
901
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700902void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800903{
Mathias Agopianfa402862010-02-12 14:04:35 -0800904 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800905}
906
Jason Sams15832442009-11-15 12:14:26 -0800907void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800908{
Jason Sams15832442009-11-15 12:14:26 -0800909 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800910}
911
Jason Samsc21cf402009-11-17 17:26:46 -0800912void rsi_ContextDump(Context *rsc, int32_t bits)
913{
914 ObjectBase::dumpAll(rsc);
915}
916
Jason Samsa2cf7552010-03-03 13:03:18 -0800917const char * rsi_ContextGetError(Context *rsc, RsError *e)
918{
919 const char *msg = rsc->getError(e);
920 if (*e != RS_ERROR_NONE) {
921 LOGE("RS Error %i %s", *e, msg);
922 }
923 return msg;
924}
925
Jason Sams326e0dd2009-05-22 14:03:28 -0700926}
927}
928
929
Jason Sams4820e8b2010-02-09 16:05:07 -0800930RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700931{
Jason Sams4820e8b2010-02-09 16:05:07 -0800932 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700933 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800934 Context *rsc = new Context(dev, false, false);
935 return rsc;
936}
937
938RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
939{
940 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
941 Device * dev = static_cast<Device *>(vdev);
942 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700943 return rsc;
944}
945
946void rsContextDestroy(RsContext vrsc)
947{
948 Context * rsc = static_cast<Context *>(vrsc);
949 delete rsc;
950}
951
Jason Sams8c401ef2009-10-06 13:58:47 -0700952uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
953{
954 Context * rsc = static_cast<Context *>(vrsc);
955 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
956}
957
958void rsContextInitToClient(RsContext vrsc)
959{
960 Context * rsc = static_cast<Context *>(vrsc);
961 rsc->initToClient();
962}
963
964void rsContextDeinitToClient(RsContext vrsc)
965{
966 Context * rsc = static_cast<Context *>(vrsc);
967 rsc->deinitToClient();
968}
969