blob: 3e4cc36bc36a749bfd1f40ea2c07b61dddddb70e [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
Jason Samsafcb25c2009-08-25 11:34:49 -070088
Jason Sams33b6e3b2009-10-27 14:44:31 -070089 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
90 checkEglError("eglCreateContext");
91 if (mEGL.mContext == EGL_NO_CONTEXT) {
92 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
93 }
94 gGLContextCount++;
95
Jason Sams458f2dc2009-11-03 13:58:36 -080096 if (mWndSurface) {
97 setSurface(mWndSurface);
98 } else {
99 setSurface((Surface *)android_createDisplaySurface());
Jason Sams33b6e3b2009-10-27 14:44:31 -0700100 }
101
Jason Samsafcb25c2009-08-25 11:34:49 -0700102 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
103 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
104
105
106 mGL.mVersion = glGetString(GL_VERSION);
107 mGL.mVendor = glGetString(GL_VENDOR);
108 mGL.mRenderer = glGetString(GL_RENDERER);
109 mGL.mExtensions = glGetString(GL_EXTENSIONS);
110
Jason Sams9397e302009-08-27 20:23:34 -0700111 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
112 LOGV("GL Version %s", mGL.mVersion);
113 LOGV("GL Vendor %s", mGL.mVendor);
114 LOGV("GL Renderer %s", mGL.mRenderer);
115 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -0700116
Jason Sams306fb232009-08-25 17:09:59 -0700117 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700118 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -0700119 } else {
120 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -0700121 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700122}
123
Jason Sams33b6e3b2009-10-27 14:44:31 -0700124void Context::deinitEGL()
125{
Jason Sams458f2dc2009-11-03 13:58:36 -0800126 setSurface(NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700127 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
128 checkEglError("eglDestroyContext");
129
130 gGLContextCount--;
131 if (!gGLContextCount) {
132 eglTerminate(mEGL.mDisplay);
133 }
134}
135
136
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700137bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700138{
139 ObjectBaseRef<ProgramFragment> frag(mFragment);
140 ObjectBaseRef<ProgramVertex> vtx(mVertex);
141 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700142 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700143
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700144 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700145
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700146 mFragment.set(frag);
147 mVertex.set(vtx);
148 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700149 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700150 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700151}
152
153
Jason Samsa44cb292009-06-04 17:58:03 -0700154bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700155{
Jason Sams1fddd902009-09-25 15:25:00 -0700156 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700157 timerSet(RS_TIMER_CLEAR_SWAP);
158 }
Jason Sams10308932009-06-09 12:15:30 -0700159 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700160
Jason Sams8c9534b2009-09-22 12:26:53 -0700161 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
162 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700163 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700164 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
165
Jason Sams928b7342009-06-08 18:50:13 -0700166 glClearColor(mRootScript->mEnviroment.mClearColor[0],
167 mRootScript->mEnviroment.mClearColor[1],
168 mRootScript->mEnviroment.mClearColor[2],
169 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700170 if (mUseDepth) {
171 glDepthMask(GL_TRUE);
172 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
173 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174 } else {
175 glClear(GL_COLOR_BUFFER_BIT);
176 }
Jason Sams306fb232009-08-25 17:09:59 -0700177
Jason Sams1fddd902009-09-25 15:25:00 -0700178 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700179 timerSet(RS_TIMER_SCRIPT);
180 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700181 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700182 bool ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700183
184 GLenum err = glGetError();
185 if (err != GL_NO_ERROR) {
186 LOGE("Pending GL Error, 0x%x", err);
187 }
188
Jason Samscfb1d112009-08-05 13:57:03 -0700189 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700190}
191
Jason Sams24371d92009-08-19 12:17:14 -0700192uint64_t Context::getTime() const
193{
194 struct timespec t;
195 clock_gettime(CLOCK_MONOTONIC, &t);
196 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
197}
198
199void Context::timerReset()
200{
201 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
202 mTimers[ct] = 0;
203 }
204}
205
206void Context::timerInit()
207{
208 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700209 mTimeFrame = mTimeLast;
210 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700211 mTimerActive = RS_TIMER_INTERNAL;
212 timerReset();
213}
214
Jason Sams1d54f102009-09-03 15:43:13 -0700215void Context::timerFrame()
216{
217 mTimeLastFrame = mTimeFrame;
218 mTimeFrame = getTime();
219}
220
Jason Sams24371d92009-08-19 12:17:14 -0700221void Context::timerSet(Timers tm)
222{
223 uint64_t last = mTimeLast;
224 mTimeLast = getTime();
225 mTimers[mTimerActive] += mTimeLast - last;
226 mTimerActive = tm;
227}
228
229void Context::timerPrint()
230{
231 double total = 0;
232 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
233 total += mTimers[ct];
234 }
Jason Sams1d54f102009-09-03 15:43:13 -0700235 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700236
Jason Sams1d54f102009-09-03 15:43:13 -0700237 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
238 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700239 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700240 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
241 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
242 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700243}
244
Jason Sams326e0dd2009-05-22 14:03:28 -0700245void Context::setupCheck()
246{
Jason Sams5fd09d82009-09-23 13:57:02 -0700247 mFragmentStore->setupGL(this, &mStateFragmentStore);
248 mFragment->setupGL(this, &mStateFragment);
249 mRaster->setupGL(this, &mStateRaster);
250 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700251}
252
Jason Sams1fddd902009-09-25 15:25:00 -0700253static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700254{
255 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700256 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700257 return 0 != strcmp(buf, "0");
258}
Jason Sams326e0dd2009-05-22 14:03:28 -0700259
260void * Context::threadProc(void *vrsc)
261{
262 Context *rsc = static_cast<Context *>(vrsc);
263
Jason Sams1fddd902009-09-25 15:25:00 -0700264 rsc->props.mLogTimes = getProp("debug.rs.profile");
265 rsc->props.mLogScripts = getProp("debug.rs.script");
266 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700267
Jason Sams33b6e3b2009-10-27 14:44:31 -0700268 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700269 rsc->initEGL();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700270 pthread_mutex_unlock(&gInitMutex);
Jason Sams8ce125b2009-06-17 16:52:59 -0700271
Jason Samse5769102009-06-19 16:03:18 -0700272 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
273 if (!tlsStruct) {
274 LOGE("Error allocating tls storage");
275 return NULL;
276 }
277 tlsStruct->mContext = rsc;
278 tlsStruct->mScript = NULL;
279 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
280 if (status) {
281 LOGE("pthread_setspecific %i", status);
282 }
283
Jason Sams5fd09d82009-09-23 13:57:02 -0700284 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
285 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700286 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700287 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700288 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700289 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700290 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700291 rsc->setFragmentStore(NULL);
292
Jason Sams326e0dd2009-05-22 14:03:28 -0700293 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700294 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700295 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700296 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700297 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800298 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700299
Jason Sams732f1c02009-06-18 16:58:42 -0700300 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700301 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700302 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700303 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
304 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700305 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700306 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700307 rsc->timerFrame();
308 rsc->timerSet(RS_TIMER_INTERNAL);
309 rsc->timerPrint();
310 rsc->timerReset();
311 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700312 }
Jason Sams24371d92009-08-19 12:17:14 -0700313 if (rsc->mObjDestroy.mNeedToEmpty) {
314 rsc->objDestroyOOBRun();
315 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700316 }
317
Jason Sams8c0ee652009-08-25 14:49:07 -0700318 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700319 rsc->mRaster.clear();
320 rsc->mFragment.clear();
321 rsc->mVertex.clear();
322 rsc->mFragmentStore.clear();
323 rsc->mRootScript.clear();
324 rsc->mStateRaster.deinit(rsc);
325 rsc->mStateVertex.deinit(rsc);
326 rsc->mStateFragment.deinit(rsc);
327 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700328 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700329
Jason Samse402ed32009-11-03 11:25:42 -0800330 rsc->mObjDestroy.mNeedToEmpty = true;
331 rsc->objDestroyOOBRun();
332
Jason Sams326e0dd2009-05-22 14:03:28 -0700333 glClearColor(0,0,0,0);
334 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700335 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700336
337 pthread_mutex_lock(&gInitMutex);
338 rsc->deinitEGL();
339 pthread_mutex_unlock(&gInitMutex);
340
Jason Sams8c0ee652009-08-25 14:49:07 -0700341 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700342 return NULL;
343}
344
Jason Samsafcb25c2009-08-25 11:34:49 -0700345Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700346{
Jason Samsfb03a222009-10-15 16:47:31 -0700347 pthread_mutex_lock(&gInitMutex);
348
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 dev->addContext(this);
350 mDev = dev;
351 mRunning = false;
352 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700353 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700354 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700355 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700356
Jason Samsa658e902009-06-04 14:35:01 -0700357 int status;
358 pthread_attr_t threadAttr;
359
Jason Samsfb03a222009-10-15 16:47:31 -0700360 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700361 status = pthread_key_create(&gThreadTLSKey, NULL);
362 if (status) {
363 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700364 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700365 return;
366 }
Jason Samse5769102009-06-19 16:03:18 -0700367 }
Jason Samsfb03a222009-10-15 16:47:31 -0700368 gThreadTLSKeyCount++;
369 pthread_mutex_unlock(&gInitMutex);
370
371 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700372
Jason Samsa658e902009-06-04 14:35:01 -0700373 status = pthread_attr_init(&threadAttr);
374 if (status) {
375 LOGE("Failed to init thread attribute.");
376 return;
377 }
378
379 sched_param sparam;
380 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
381 pthread_attr_setschedparam(&threadAttr, &sparam);
382
Jason Sams992a0b72009-06-23 12:22:47 -0700383 mWndSurface = sur;
384
Jason Sams50869382009-08-18 17:07:09 -0700385 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700386 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700387 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700388
Jason Sams992a0b72009-06-23 12:22:47 -0700389 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700390 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700391 if (status) {
392 LOGE("Failed to start rs context thread.");
393 }
394
Jason Sams326e0dd2009-05-22 14:03:28 -0700395 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700396 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700397 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700398
Jason Samsa658e902009-06-04 14:35:01 -0700399 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700400}
401
402Context::~Context()
403{
Jason Sams8c0ee652009-08-25 14:49:07 -0700404 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700405 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700406 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700407 void *res;
408
Jason Sams8c0ee652009-08-25 14:49:07 -0700409 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700410 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800411 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700412 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700413
Jason Samsfb03a222009-10-15 16:47:31 -0700414 // Global structure cleanup.
415 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700416 if (mDev) {
417 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700418 --gThreadTLSKeyCount;
419 if (!gThreadTLSKeyCount) {
420 pthread_key_delete(gThreadTLSKey);
421 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800422 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700423 }
Jason Samsfb03a222009-10-15 16:47:31 -0700424 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700425
426 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700427}
428
Jason Sams458f2dc2009-11-03 13:58:36 -0800429void Context::setSurface(Surface *sur)
430{
431 EGLBoolean ret;
432 if (mEGL.mSurface != NULL) {
433 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
434 checkEglError("eglMakeCurrent", ret);
435
436 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
437 checkEglError("eglDestroySurface", ret);
438
439 mEGL.mSurface = NULL;
440 }
441
442 mWndSurface = sur;
443 if (mWndSurface != NULL) {
444 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
445 checkEglError("eglCreateWindowSurface");
446 if (mEGL.mSurface == EGL_NO_SURFACE) {
447 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
448 }
449
450 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
451 checkEglError("eglMakeCurrent", ret);
452 }
453}
454
Jason Sams86f1b232009-09-24 17:38:20 -0700455void Context::pause()
456{
457 mPaused = true;
458}
459
460void Context::resume()
461{
462 mPaused = false;
463}
464
Jason Sams326e0dd2009-05-22 14:03:28 -0700465void Context::setRootScript(Script *s)
466{
467 mRootScript.set(s);
468}
469
470void Context::setFragmentStore(ProgramFragmentStore *pfs)
471{
Jason Sams8ce125b2009-06-17 16:52:59 -0700472 if (pfs == NULL) {
473 mFragmentStore.set(mStateFragmentStore.mDefault);
474 } else {
475 mFragmentStore.set(pfs);
476 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700477}
478
479void Context::setFragment(ProgramFragment *pf)
480{
Jason Sams8ce125b2009-06-17 16:52:59 -0700481 if (pf == NULL) {
482 mFragment.set(mStateFragment.mDefault);
483 } else {
484 mFragment.set(pf);
485 }
Jason Samscfb1d112009-08-05 13:57:03 -0700486}
487
Jason Sams5fd09d82009-09-23 13:57:02 -0700488void Context::setRaster(ProgramRaster *pr)
489{
490 if (pr == NULL) {
491 mRaster.set(mStateRaster.mDefault);
492 } else {
493 mRaster.set(pr);
494 }
495}
496
Jason Samscfb1d112009-08-05 13:57:03 -0700497void Context::allocationCheck(const Allocation *a)
498{
499 mVertex->checkUpdatedAllocation(a);
500 mFragment->checkUpdatedAllocation(a);
501 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700502}
503
504void Context::setVertex(ProgramVertex *pv)
505{
Jason Sams8ce125b2009-06-17 16:52:59 -0700506 if (pv == NULL) {
507 mVertex.set(mStateVertex.mDefault);
508 } else {
509 mVertex.set(pv);
510 }
Jason Samsc2f94902009-10-15 18:45:45 -0700511 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700512}
513
Jason Samsa4a54e42009-06-10 18:39:40 -0700514void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700515{
516 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700517 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700518 mNames.add(obj);
519}
520
521void Context::removeName(ObjectBase *obj)
522{
523 for(size_t ct=0; ct < mNames.size(); ct++) {
524 if (obj == mNames[ct]) {
525 mNames.removeAt(ct);
526 return;
527 }
528 }
529}
530
531ObjectBase * Context::lookupName(const char *name) const
532{
533 for(size_t ct=0; ct < mNames.size(); ct++) {
534 if (!strcmp(name, mNames[ct]->getName())) {
535 return mNames[ct];
536 }
537 }
538 return NULL;
539}
540
Jason Samsa4a54e42009-06-10 18:39:40 -0700541void Context::appendNameDefines(String8 *str) const
542{
543 char buf[256];
544 for (size_t ct=0; ct < mNames.size(); ct++) {
545 str->append("#define NAMED_");
546 str->append(mNames[ct]->getName());
547 str->append(" ");
548 sprintf(buf, "%i\n", (int)mNames[ct]);
549 str->append(buf);
550 }
551}
552
Joe Onorato57b79ce2009-08-09 22:57:44 -0700553void Context::appendVarDefines(String8 *str) const
554{
555 char buf[256];
556 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
557 str->append("#define ");
558 str->append(mInt32Defines.keyAt(ct));
559 str->append(" ");
560 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
561 str->append(buf);
562
563 }
564 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
565 str->append("#define ");
566 str->append(mFloatDefines.keyAt(ct));
567 str->append(" ");
568 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
569 str->append(buf);
570 }
571}
572
Jason Sams50869382009-08-18 17:07:09 -0700573bool Context::objDestroyOOBInit()
574{
575 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
576 if (status) {
577 LOGE("Context::ObjDestroyOOBInit mutex init failure");
578 return false;
579 }
580 return true;
581}
582
583void Context::objDestroyOOBRun()
584{
585 if (mObjDestroy.mNeedToEmpty) {
586 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
587 if (status) {
588 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
589 return;
590 }
591
592 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700593 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700594 }
595 mObjDestroy.mDestroyList.clear();
596 mObjDestroy.mNeedToEmpty = false;
597
598 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
599 if (status) {
600 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
601 }
602 }
603}
604
605void Context::objDestroyOOBDestroy()
606{
607 rsAssert(!mObjDestroy.mNeedToEmpty);
608 pthread_mutex_destroy(&mObjDestroy.mMutex);
609}
610
611void Context::objDestroyAdd(ObjectBase *obj)
612{
613 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
614 if (status) {
615 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
616 return;
617 }
618
619 mObjDestroy.mNeedToEmpty = true;
620 mObjDestroy.mDestroyList.add(obj);
621
622 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
623 if (status) {
624 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
625 }
626}
627
Jason Sams8c401ef2009-10-06 13:58:47 -0700628uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
629{
630 //LOGE("getMessageToClient %i %i", bufferLen, wait);
631 if (!wait) {
632 if (mIO.mToClient.isEmpty()) {
633 // No message to get and not going to wait for one.
634 receiveLen = 0;
635 return 0;
636 }
637 }
638
639 //LOGE("getMessageToClient 2 con=%p", this);
640 uint32_t bytesData = 0;
641 uint32_t commandID = 0;
642 const void *d = mIO.mToClient.get(&commandID, &bytesData);
643 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
644
645 *receiveLen = bytesData;
646 if (bufferLen >= bytesData) {
647 memcpy(data, d, bytesData);
648 mIO.mToClient.next();
649 return commandID;
650 }
651 return 0;
652}
653
654bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
655{
656 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
657 if (cmdID == 0) {
658 LOGE("Attempting to send invalid command 0 to client.");
659 return false;
660 }
661 if (!waitForSpace) {
662 if (mIO.mToClient.getFreeSpace() < len) {
663 // Not enough room, and not waiting.
664 return false;
665 }
666 }
667 //LOGE("sendMessageToClient 2");
668 void *p = mIO.mToClient.reserve(len);
669 memcpy(p, data, len);
670 mIO.mToClient.commit(cmdID, len);
671 //LOGE("sendMessageToClient 3");
672 return true;
673}
674
675void Context::initToClient()
676{
677 while(!mRunning) {
678 usleep(100);
679 }
680}
681
682void Context::deinitToClient()
683{
684 mIO.mToClient.shutdown();
685}
Jason Sams50869382009-08-18 17:07:09 -0700686
Jason Samsa4a54e42009-06-10 18:39:40 -0700687
Jason Sams326e0dd2009-05-22 14:03:28 -0700688///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700689//
Jason Sams326e0dd2009-05-22 14:03:28 -0700690
691namespace android {
692namespace renderscript {
693
694
695void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
696{
697 Script *s = static_cast<Script *>(vs);
698 rsc->setRootScript(s);
699}
700
701void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
702{
703 Sampler *s = static_cast<Sampler *>(vs);
704
705 if (slot > RS_MAX_SAMPLER_SLOT) {
706 LOGE("Invalid sampler slot");
707 return;
708 }
709
710 s->bindToContext(&rsc->mStateSampler, slot);
711}
712
713void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
714{
715 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
716 rsc->setFragmentStore(pfs);
717}
718
719void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
720{
721 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
722 rsc->setFragment(pf);
723}
724
Jason Sams5fd09d82009-09-23 13:57:02 -0700725void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
726{
727 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
728 rsc->setRaster(pr);
729}
730
Jason Sams326e0dd2009-05-22 14:03:28 -0700731void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
732{
733 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
734 rsc->setVertex(pv);
735}
736
Jason Samsa4a54e42009-06-10 18:39:40 -0700737void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700738{
739 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700740 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700741}
Jason Sams326e0dd2009-05-22 14:03:28 -0700742
Jason Sams707aaf32009-08-18 14:14:24 -0700743void rsi_ObjDestroy(Context *rsc, void *obj)
744{
745 ObjectBase *ob = static_cast<ObjectBase *>(obj);
746 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700747 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700748}
749
Joe Onorato57b79ce2009-08-09 22:57:44 -0700750void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
751{
752 rsc->addInt32Define(name, value);
753}
754
755void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
756{
757 rsc->addFloatDefine(name, value);
758}
Jason Sams326e0dd2009-05-22 14:03:28 -0700759
Jason Sams86f1b232009-09-24 17:38:20 -0700760void rsi_ContextPause(Context *rsc)
761{
762 rsc->pause();
763}
764
765void rsi_ContextResume(Context *rsc)
766{
767 rsc->resume();
768}
769
Jason Sams458f2dc2009-11-03 13:58:36 -0800770void rsi_ContextSetSurface(Context *rsc, void *sur)
771{
772 rsc->setSurface((Surface *)sur);
773}
774
Jason Sams326e0dd2009-05-22 14:03:28 -0700775}
776}
777
778
Jason Samsafcb25c2009-08-25 11:34:49 -0700779RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700780{
781 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700782 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700783 return rsc;
784}
785
786void rsContextDestroy(RsContext vrsc)
787{
788 Context * rsc = static_cast<Context *>(vrsc);
789 delete rsc;
790}
791
Jason Sams50869382009-08-18 17:07:09 -0700792void rsObjDestroyOOB(RsContext vrsc, void *obj)
793{
794 Context * rsc = static_cast<Context *>(vrsc);
795 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
796}
797
Jason Sams8c401ef2009-10-06 13:58:47 -0700798uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
799{
800 Context * rsc = static_cast<Context *>(vrsc);
801 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
802}
803
804void rsContextInitToClient(RsContext vrsc)
805{
806 Context * rsc = static_cast<Context *>(vrsc);
807 rsc->initToClient();
808}
809
810void rsContextDeinitToClient(RsContext vrsc)
811{
812 Context * rsc = static_cast<Context *>(vrsc);
813 rsc->deinitToClient();
814}
815