blob: 2e6d7b0c09433c7d0bfdbcf25715ab69f8282390 [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>
Jason Sams326e0dd2009-05-22 14:03:28 -070022
Joe Onorato76371ff2009-09-23 16:37:36 -070023#include <cutils/properties.h>
24
Jason Sams1aa5a4e2009-06-22 17:15:15 -070025#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028using namespace android;
29using namespace android::renderscript;
30
Jason Samse5769102009-06-19 16:03:18 -070031pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070032uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070033uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070034pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070035
Jason Sams33b6e3b2009-10-27 14:44:31 -070036static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
37 if (returnVal != EGL_TRUE) {
38 fprintf(stderr, "%s() returned %d\n", op, returnVal);
39 }
40
41 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
42 = eglGetError()) {
43 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
44 error);
45 }
46}
47
Jason Sams326e0dd2009-05-22 14:03:28 -070048void Context::initEGL()
49{
Jason Samsafcb25c2009-08-25 11:34:49 -070050 mEGL.mNumConfigs = -1;
51 EGLint configAttribs[128];
52 EGLint *configAttribsPtr = configAttribs;
Jason Sams326e0dd2009-05-22 14:03:28 -070053
Jason Samsafcb25c2009-08-25 11:34:49 -070054 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070055
Jason Samsafcb25c2009-08-25 11:34:49 -070056 configAttribsPtr[0] = EGL_SURFACE_TYPE;
57 configAttribsPtr[1] = EGL_WINDOW_BIT;
58 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070059
Jason Samsafcb25c2009-08-25 11:34:49 -070060 if (mUseDepth) {
61 configAttribsPtr[0] = EGL_DEPTH_SIZE;
62 configAttribsPtr[1] = 16;
63 configAttribsPtr += 2;
64 }
Jason Sams9397e302009-08-27 20:23:34 -070065
Jason Sams5fd09d82009-09-23 13:57:02 -070066 if (mDev->mForceSW) {
67 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
68 configAttribsPtr[1] = EGL_SLOW_CONFIG;
69 configAttribsPtr += 2;
70 }
71
Jason Samsafcb25c2009-08-25 11:34:49 -070072 configAttribsPtr[0] = EGL_NONE;
73 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070074
Jason Sams6d751ef2009-10-08 12:55:06 -070075 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070076 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070077 checkEglError("eglGetDisplay");
78
Jason Samsafcb25c2009-08-25 11:34:49 -070079 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070080 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070081
82 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
83 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070084 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070085 }
86 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
87
88 if (mWndSurface) {
89 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
90 } else {
91 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
92 android_createDisplaySurface(),
93 NULL);
94 }
Jason Sams33b6e3b2009-10-27 14:44:31 -070095 checkEglError("eglCreateWindowSurface");
96 if (mEGL.mSurface == EGL_NO_SURFACE) {
97 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
98 }
Jason Samsafcb25c2009-08-25 11:34:49 -070099
Jason Sams33b6e3b2009-10-27 14:44:31 -0700100 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
101 checkEglError("eglCreateContext");
102 if (mEGL.mContext == EGL_NO_CONTEXT) {
103 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
104 }
105 gGLContextCount++;
106
107 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
108 checkEglError("eglCreateContext", ret);
109 if (mEGL.mContext == EGL_NO_CONTEXT) {
110 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
111 }
112
Jason Samsafcb25c2009-08-25 11:34:49 -0700113 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
114 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
115
116
117 mGL.mVersion = glGetString(GL_VERSION);
118 mGL.mVendor = glGetString(GL_VENDOR);
119 mGL.mRenderer = glGetString(GL_RENDERER);
120 mGL.mExtensions = glGetString(GL_EXTENSIONS);
121
Jason Sams9397e302009-08-27 20:23:34 -0700122 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
123 LOGV("GL Version %s", mGL.mVersion);
124 LOGV("GL Vendor %s", mGL.mVendor);
125 LOGV("GL Renderer %s", mGL.mRenderer);
126 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -0700127
Jason Sams306fb232009-08-25 17:09:59 -0700128 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700129 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -0700130 } else {
131 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -0700132 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700133}
134
Jason Sams33b6e3b2009-10-27 14:44:31 -0700135void Context::deinitEGL()
136{
137 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 checkEglError("eglCreateContext", ret);
139 if (mEGL.mContext == EGL_NO_CONTEXT) {
140 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
141 }
142
143 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
144 checkEglError("eglDestroyContext");
145
146 gGLContextCount--;
147 if (!gGLContextCount) {
148 eglTerminate(mEGL.mDisplay);
149 }
150}
151
152
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700153bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700154{
155 ObjectBaseRef<ProgramFragment> frag(mFragment);
156 ObjectBaseRef<ProgramVertex> vtx(mVertex);
157 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700158 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700159
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700160 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700161
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700162 mFragment.set(frag);
163 mVertex.set(vtx);
164 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700165 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700166 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700167}
168
169
Jason Samsa44cb292009-06-04 17:58:03 -0700170bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700171{
Jason Sams1fddd902009-09-25 15:25:00 -0700172 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700173 timerSet(RS_TIMER_CLEAR_SWAP);
174 }
Jason Sams10308932009-06-09 12:15:30 -0700175 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700176
Jason Sams8c9534b2009-09-22 12:26:53 -0700177 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
178 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700179 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700180 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
181
Jason Sams928b7342009-06-08 18:50:13 -0700182 glClearColor(mRootScript->mEnviroment.mClearColor[0],
183 mRootScript->mEnviroment.mClearColor[1],
184 mRootScript->mEnviroment.mClearColor[2],
185 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700186 if (mUseDepth) {
187 glDepthMask(GL_TRUE);
188 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
189 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
190 } else {
191 glClear(GL_COLOR_BUFFER_BIT);
192 }
Jason Sams306fb232009-08-25 17:09:59 -0700193
Jason Sams1fddd902009-09-25 15:25:00 -0700194 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700195 timerSet(RS_TIMER_SCRIPT);
196 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700197 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700198 bool ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700199
200 GLenum err = glGetError();
201 if (err != GL_NO_ERROR) {
202 LOGE("Pending GL Error, 0x%x", err);
203 }
204
Jason Samscfb1d112009-08-05 13:57:03 -0700205 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700206}
207
Jason Sams24371d92009-08-19 12:17:14 -0700208uint64_t Context::getTime() const
209{
210 struct timespec t;
211 clock_gettime(CLOCK_MONOTONIC, &t);
212 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
213}
214
215void Context::timerReset()
216{
217 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
218 mTimers[ct] = 0;
219 }
220}
221
222void Context::timerInit()
223{
224 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700225 mTimeFrame = mTimeLast;
226 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700227 mTimerActive = RS_TIMER_INTERNAL;
228 timerReset();
229}
230
Jason Sams1d54f102009-09-03 15:43:13 -0700231void Context::timerFrame()
232{
233 mTimeLastFrame = mTimeFrame;
234 mTimeFrame = getTime();
235}
236
Jason Sams24371d92009-08-19 12:17:14 -0700237void Context::timerSet(Timers tm)
238{
239 uint64_t last = mTimeLast;
240 mTimeLast = getTime();
241 mTimers[mTimerActive] += mTimeLast - last;
242 mTimerActive = tm;
243}
244
245void Context::timerPrint()
246{
247 double total = 0;
248 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
249 total += mTimers[ct];
250 }
Jason Sams1d54f102009-09-03 15:43:13 -0700251 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700252
Jason Sams1d54f102009-09-03 15:43:13 -0700253 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
254 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700255 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700256 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
257 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
258 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700259}
260
Jason Sams326e0dd2009-05-22 14:03:28 -0700261void Context::setupCheck()
262{
Jason Sams5fd09d82009-09-23 13:57:02 -0700263 mFragmentStore->setupGL(this, &mStateFragmentStore);
264 mFragment->setupGL(this, &mStateFragment);
265 mRaster->setupGL(this, &mStateRaster);
266 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700267}
268
Jason Sams1fddd902009-09-25 15:25:00 -0700269static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700270{
271 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700272 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700273 return 0 != strcmp(buf, "0");
274}
Jason Sams326e0dd2009-05-22 14:03:28 -0700275
276void * Context::threadProc(void *vrsc)
277{
278 Context *rsc = static_cast<Context *>(vrsc);
279
Jason Sams1fddd902009-09-25 15:25:00 -0700280 rsc->props.mLogTimes = getProp("debug.rs.profile");
281 rsc->props.mLogScripts = getProp("debug.rs.script");
282 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700283
Jason Sams33b6e3b2009-10-27 14:44:31 -0700284 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700285 rsc->initEGL();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700286 pthread_mutex_unlock(&gInitMutex);
Jason Sams8ce125b2009-06-17 16:52:59 -0700287
Jason Samse5769102009-06-19 16:03:18 -0700288 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
289 if (!tlsStruct) {
290 LOGE("Error allocating tls storage");
291 return NULL;
292 }
293 tlsStruct->mContext = rsc;
294 tlsStruct->mScript = NULL;
295 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
296 if (status) {
297 LOGE("pthread_setspecific %i", status);
298 }
299
Jason Sams5fd09d82009-09-23 13:57:02 -0700300 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
301 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700302 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700303 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700304 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700305 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700306 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700307 rsc->setFragmentStore(NULL);
308
Jason Sams326e0dd2009-05-22 14:03:28 -0700309 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700310 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700311 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700312 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700313 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700314
Jason Sams732f1c02009-06-18 16:58:42 -0700315 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700316 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700317 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700318 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
319 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700320 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700321 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700322 rsc->timerFrame();
323 rsc->timerSet(RS_TIMER_INTERNAL);
324 rsc->timerPrint();
325 rsc->timerReset();
326 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700327 }
Jason Sams24371d92009-08-19 12:17:14 -0700328 if (rsc->mObjDestroy.mNeedToEmpty) {
329 rsc->objDestroyOOBRun();
330 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700331 }
332
Jason Sams8c0ee652009-08-25 14:49:07 -0700333 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700334 rsc->mRaster.clear();
335 rsc->mFragment.clear();
336 rsc->mVertex.clear();
337 rsc->mFragmentStore.clear();
338 rsc->mRootScript.clear();
339 rsc->mStateRaster.deinit(rsc);
340 rsc->mStateVertex.deinit(rsc);
341 rsc->mStateFragment.deinit(rsc);
342 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700343 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700344
Jason Samse402ed32009-11-03 11:25:42 -0800345 rsc->mObjDestroy.mNeedToEmpty = true;
346 rsc->objDestroyOOBRun();
347
Jason Sams326e0dd2009-05-22 14:03:28 -0700348 glClearColor(0,0,0,0);
349 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700350 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700351
352 pthread_mutex_lock(&gInitMutex);
353 rsc->deinitEGL();
354 pthread_mutex_unlock(&gInitMutex);
355
Jason Sams8c0ee652009-08-25 14:49:07 -0700356 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700357 return NULL;
358}
359
Jason Samsafcb25c2009-08-25 11:34:49 -0700360Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700361{
Jason Samsfb03a222009-10-15 16:47:31 -0700362 pthread_mutex_lock(&gInitMutex);
363
Jason Sams326e0dd2009-05-22 14:03:28 -0700364 dev->addContext(this);
365 mDev = dev;
366 mRunning = false;
367 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700368 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700369 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700370 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700371
Jason Samsa658e902009-06-04 14:35:01 -0700372 int status;
373 pthread_attr_t threadAttr;
374
Jason Samsfb03a222009-10-15 16:47:31 -0700375 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700376 status = pthread_key_create(&gThreadTLSKey, NULL);
377 if (status) {
378 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700379 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700380 return;
381 }
Jason Samse5769102009-06-19 16:03:18 -0700382 }
Jason Samsfb03a222009-10-15 16:47:31 -0700383 gThreadTLSKeyCount++;
384 pthread_mutex_unlock(&gInitMutex);
385
386 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700387
Jason Samsa658e902009-06-04 14:35:01 -0700388 status = pthread_attr_init(&threadAttr);
389 if (status) {
390 LOGE("Failed to init thread attribute.");
391 return;
392 }
393
394 sched_param sparam;
395 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
396 pthread_attr_setschedparam(&threadAttr, &sparam);
397
Jason Sams992a0b72009-06-23 12:22:47 -0700398 mWndSurface = sur;
399
Jason Sams50869382009-08-18 17:07:09 -0700400 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700401 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700402 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700403
Jason Sams992a0b72009-06-23 12:22:47 -0700404 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700405 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700406 if (status) {
407 LOGE("Failed to start rs context thread.");
408 }
409
Jason Sams326e0dd2009-05-22 14:03:28 -0700410 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700411 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700412 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700413
Jason Samsa658e902009-06-04 14:35:01 -0700414 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700415}
416
417Context::~Context()
418{
Jason Sams8c0ee652009-08-25 14:49:07 -0700419 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700420 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700421 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700422 void *res;
423
Jason Sams8c0ee652009-08-25 14:49:07 -0700424 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700425 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800426 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700427 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700428
Jason Samsfb03a222009-10-15 16:47:31 -0700429 // Global structure cleanup.
430 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700431 if (mDev) {
432 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700433 --gThreadTLSKeyCount;
434 if (!gThreadTLSKeyCount) {
435 pthread_key_delete(gThreadTLSKey);
436 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800437 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700438 }
Jason Samsfb03a222009-10-15 16:47:31 -0700439 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700440
441 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700442}
443
Jason Sams86f1b232009-09-24 17:38:20 -0700444void Context::pause()
445{
446 mPaused = true;
447}
448
449void Context::resume()
450{
451 mPaused = false;
452}
453
Jason Sams326e0dd2009-05-22 14:03:28 -0700454void Context::setRootScript(Script *s)
455{
456 mRootScript.set(s);
457}
458
459void Context::setFragmentStore(ProgramFragmentStore *pfs)
460{
Jason Sams8ce125b2009-06-17 16:52:59 -0700461 if (pfs == NULL) {
462 mFragmentStore.set(mStateFragmentStore.mDefault);
463 } else {
464 mFragmentStore.set(pfs);
465 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700466}
467
468void Context::setFragment(ProgramFragment *pf)
469{
Jason Sams8ce125b2009-06-17 16:52:59 -0700470 if (pf == NULL) {
471 mFragment.set(mStateFragment.mDefault);
472 } else {
473 mFragment.set(pf);
474 }
Jason Samscfb1d112009-08-05 13:57:03 -0700475}
476
Jason Sams5fd09d82009-09-23 13:57:02 -0700477void Context::setRaster(ProgramRaster *pr)
478{
479 if (pr == NULL) {
480 mRaster.set(mStateRaster.mDefault);
481 } else {
482 mRaster.set(pr);
483 }
484}
485
Jason Samscfb1d112009-08-05 13:57:03 -0700486void Context::allocationCheck(const Allocation *a)
487{
488 mVertex->checkUpdatedAllocation(a);
489 mFragment->checkUpdatedAllocation(a);
490 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700491}
492
493void Context::setVertex(ProgramVertex *pv)
494{
Jason Sams8ce125b2009-06-17 16:52:59 -0700495 if (pv == NULL) {
496 mVertex.set(mStateVertex.mDefault);
497 } else {
498 mVertex.set(pv);
499 }
Jason Samsc2f94902009-10-15 18:45:45 -0700500 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700501}
502
Jason Samsa4a54e42009-06-10 18:39:40 -0700503void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700504{
505 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700506 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700507 mNames.add(obj);
508}
509
510void Context::removeName(ObjectBase *obj)
511{
512 for(size_t ct=0; ct < mNames.size(); ct++) {
513 if (obj == mNames[ct]) {
514 mNames.removeAt(ct);
515 return;
516 }
517 }
518}
519
520ObjectBase * Context::lookupName(const char *name) const
521{
522 for(size_t ct=0; ct < mNames.size(); ct++) {
523 if (!strcmp(name, mNames[ct]->getName())) {
524 return mNames[ct];
525 }
526 }
527 return NULL;
528}
529
Jason Samsa4a54e42009-06-10 18:39:40 -0700530void Context::appendNameDefines(String8 *str) const
531{
532 char buf[256];
533 for (size_t ct=0; ct < mNames.size(); ct++) {
534 str->append("#define NAMED_");
535 str->append(mNames[ct]->getName());
536 str->append(" ");
537 sprintf(buf, "%i\n", (int)mNames[ct]);
538 str->append(buf);
539 }
540}
541
Joe Onorato57b79ce2009-08-09 22:57:44 -0700542void Context::appendVarDefines(String8 *str) const
543{
544 char buf[256];
545 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
546 str->append("#define ");
547 str->append(mInt32Defines.keyAt(ct));
548 str->append(" ");
549 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
550 str->append(buf);
551
552 }
553 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
554 str->append("#define ");
555 str->append(mFloatDefines.keyAt(ct));
556 str->append(" ");
557 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
558 str->append(buf);
559 }
560}
561
Jason Sams50869382009-08-18 17:07:09 -0700562bool Context::objDestroyOOBInit()
563{
564 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
565 if (status) {
566 LOGE("Context::ObjDestroyOOBInit mutex init failure");
567 return false;
568 }
569 return true;
570}
571
572void Context::objDestroyOOBRun()
573{
574 if (mObjDestroy.mNeedToEmpty) {
575 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
576 if (status) {
577 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
578 return;
579 }
580
581 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700582 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700583 }
584 mObjDestroy.mDestroyList.clear();
585 mObjDestroy.mNeedToEmpty = false;
586
587 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
588 if (status) {
589 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
590 }
591 }
592}
593
594void Context::objDestroyOOBDestroy()
595{
596 rsAssert(!mObjDestroy.mNeedToEmpty);
597 pthread_mutex_destroy(&mObjDestroy.mMutex);
598}
599
600void Context::objDestroyAdd(ObjectBase *obj)
601{
602 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
603 if (status) {
604 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
605 return;
606 }
607
608 mObjDestroy.mNeedToEmpty = true;
609 mObjDestroy.mDestroyList.add(obj);
610
611 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
612 if (status) {
613 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
614 }
615}
616
Jason Sams8c401ef2009-10-06 13:58:47 -0700617uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
618{
619 //LOGE("getMessageToClient %i %i", bufferLen, wait);
620 if (!wait) {
621 if (mIO.mToClient.isEmpty()) {
622 // No message to get and not going to wait for one.
623 receiveLen = 0;
624 return 0;
625 }
626 }
627
628 //LOGE("getMessageToClient 2 con=%p", this);
629 uint32_t bytesData = 0;
630 uint32_t commandID = 0;
631 const void *d = mIO.mToClient.get(&commandID, &bytesData);
632 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
633
634 *receiveLen = bytesData;
635 if (bufferLen >= bytesData) {
636 memcpy(data, d, bytesData);
637 mIO.mToClient.next();
638 return commandID;
639 }
640 return 0;
641}
642
643bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
644{
645 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
646 if (cmdID == 0) {
647 LOGE("Attempting to send invalid command 0 to client.");
648 return false;
649 }
650 if (!waitForSpace) {
651 if (mIO.mToClient.getFreeSpace() < len) {
652 // Not enough room, and not waiting.
653 return false;
654 }
655 }
656 //LOGE("sendMessageToClient 2");
657 void *p = mIO.mToClient.reserve(len);
658 memcpy(p, data, len);
659 mIO.mToClient.commit(cmdID, len);
660 //LOGE("sendMessageToClient 3");
661 return true;
662}
663
664void Context::initToClient()
665{
666 while(!mRunning) {
667 usleep(100);
668 }
669}
670
671void Context::deinitToClient()
672{
673 mIO.mToClient.shutdown();
674}
Jason Sams50869382009-08-18 17:07:09 -0700675
Jason Samsa4a54e42009-06-10 18:39:40 -0700676
Jason Sams326e0dd2009-05-22 14:03:28 -0700677///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700678//
Jason Sams326e0dd2009-05-22 14:03:28 -0700679
680namespace android {
681namespace renderscript {
682
683
684void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
685{
686 Script *s = static_cast<Script *>(vs);
687 rsc->setRootScript(s);
688}
689
690void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
691{
692 Sampler *s = static_cast<Sampler *>(vs);
693
694 if (slot > RS_MAX_SAMPLER_SLOT) {
695 LOGE("Invalid sampler slot");
696 return;
697 }
698
699 s->bindToContext(&rsc->mStateSampler, slot);
700}
701
702void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
703{
704 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
705 rsc->setFragmentStore(pfs);
706}
707
708void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
709{
710 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
711 rsc->setFragment(pf);
712}
713
Jason Sams5fd09d82009-09-23 13:57:02 -0700714void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
715{
716 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
717 rsc->setRaster(pr);
718}
719
Jason Sams326e0dd2009-05-22 14:03:28 -0700720void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
721{
722 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
723 rsc->setVertex(pv);
724}
725
Jason Samsa4a54e42009-06-10 18:39:40 -0700726void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700727{
728 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700729 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700730}
Jason Sams326e0dd2009-05-22 14:03:28 -0700731
Jason Sams707aaf32009-08-18 14:14:24 -0700732void rsi_ObjDestroy(Context *rsc, void *obj)
733{
734 ObjectBase *ob = static_cast<ObjectBase *>(obj);
735 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700736 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700737}
738
Joe Onorato57b79ce2009-08-09 22:57:44 -0700739void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
740{
741 rsc->addInt32Define(name, value);
742}
743
744void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
745{
746 rsc->addFloatDefine(name, value);
747}
Jason Sams326e0dd2009-05-22 14:03:28 -0700748
Jason Sams86f1b232009-09-24 17:38:20 -0700749void rsi_ContextPause(Context *rsc)
750{
751 rsc->pause();
752}
753
754void rsi_ContextResume(Context *rsc)
755{
756 rsc->resume();
757}
758
Jason Sams326e0dd2009-05-22 14:03:28 -0700759}
760}
761
762
Jason Samsafcb25c2009-08-25 11:34:49 -0700763RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700764{
765 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700766 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700767 return rsc;
768}
769
770void rsContextDestroy(RsContext vrsc)
771{
772 Context * rsc = static_cast<Context *>(vrsc);
773 delete rsc;
774}
775
Jason Sams50869382009-08-18 17:07:09 -0700776void rsObjDestroyOOB(RsContext vrsc, void *obj)
777{
778 Context * rsc = static_cast<Context *>(vrsc);
779 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
780}
781
Jason Sams8c401ef2009-10-06 13:58:47 -0700782uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
783{
784 Context * rsc = static_cast<Context *>(vrsc);
785 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
786}
787
788void rsContextInitToClient(RsContext vrsc)
789{
790 Context * rsc = static_cast<Context *>(vrsc);
791 rsc->initToClient();
792}
793
794void rsContextDeinitToClient(RsContext vrsc)
795{
796 Context * rsc = static_cast<Context *>(vrsc);
797 rsc->deinitToClient();
798}
799