blob: eb388af500344c9df299b394f50c062397c64556 [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 Sams326e0dd2009-05-22 14:03:28 -0700345 glClearColor(0,0,0,0);
346 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700347 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700348
349 pthread_mutex_lock(&gInitMutex);
350 rsc->deinitEGL();
351 pthread_mutex_unlock(&gInitMutex);
352
Jason Sams50869382009-08-18 17:07:09 -0700353 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700354 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700355 return NULL;
356}
357
Jason Samsafcb25c2009-08-25 11:34:49 -0700358Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700359{
Jason Samsfb03a222009-10-15 16:47:31 -0700360 pthread_mutex_lock(&gInitMutex);
361
Jason Sams326e0dd2009-05-22 14:03:28 -0700362 dev->addContext(this);
363 mDev = dev;
364 mRunning = false;
365 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700366 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700367 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700368 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700369
Jason Samsa658e902009-06-04 14:35:01 -0700370 int status;
371 pthread_attr_t threadAttr;
372
Jason Samsfb03a222009-10-15 16:47:31 -0700373 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700374 status = pthread_key_create(&gThreadTLSKey, NULL);
375 if (status) {
376 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700377 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700378 return;
379 }
Jason Samse5769102009-06-19 16:03:18 -0700380 }
Jason Samsfb03a222009-10-15 16:47:31 -0700381 gThreadTLSKeyCount++;
382 pthread_mutex_unlock(&gInitMutex);
383
384 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700385
Jason Samsa658e902009-06-04 14:35:01 -0700386 status = pthread_attr_init(&threadAttr);
387 if (status) {
388 LOGE("Failed to init thread attribute.");
389 return;
390 }
391
392 sched_param sparam;
393 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
394 pthread_attr_setschedparam(&threadAttr, &sparam);
395
Jason Sams992a0b72009-06-23 12:22:47 -0700396 mWndSurface = sur;
397
Jason Sams50869382009-08-18 17:07:09 -0700398 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700399 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700400 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700401
Jason Sams992a0b72009-06-23 12:22:47 -0700402 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700403 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700404 if (status) {
405 LOGE("Failed to start rs context thread.");
406 }
407
Jason Sams326e0dd2009-05-22 14:03:28 -0700408 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700409 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700410 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700411
Jason Samsa658e902009-06-04 14:35:01 -0700412 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700413}
414
415Context::~Context()
416{
Jason Sams8c0ee652009-08-25 14:49:07 -0700417 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700418 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700419 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700420 void *res;
421
Jason Sams8c0ee652009-08-25 14:49:07 -0700422 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700423 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700424 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700425
Jason Samsfb03a222009-10-15 16:47:31 -0700426 // Global structure cleanup.
427 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700428 if (mDev) {
429 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700430 --gThreadTLSKeyCount;
431 if (!gThreadTLSKeyCount) {
432 pthread_key_delete(gThreadTLSKey);
433 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700434 }
Jason Samsfb03a222009-10-15 16:47:31 -0700435 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700436
437 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700438}
439
Jason Sams86f1b232009-09-24 17:38:20 -0700440void Context::pause()
441{
442 mPaused = true;
443}
444
445void Context::resume()
446{
447 mPaused = false;
448}
449
Jason Sams326e0dd2009-05-22 14:03:28 -0700450void Context::setRootScript(Script *s)
451{
452 mRootScript.set(s);
453}
454
455void Context::setFragmentStore(ProgramFragmentStore *pfs)
456{
Jason Sams8ce125b2009-06-17 16:52:59 -0700457 if (pfs == NULL) {
458 mFragmentStore.set(mStateFragmentStore.mDefault);
459 } else {
460 mFragmentStore.set(pfs);
461 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700462}
463
464void Context::setFragment(ProgramFragment *pf)
465{
Jason Sams8ce125b2009-06-17 16:52:59 -0700466 if (pf == NULL) {
467 mFragment.set(mStateFragment.mDefault);
468 } else {
469 mFragment.set(pf);
470 }
Jason Samscfb1d112009-08-05 13:57:03 -0700471}
472
Jason Sams5fd09d82009-09-23 13:57:02 -0700473void Context::setRaster(ProgramRaster *pr)
474{
475 if (pr == NULL) {
476 mRaster.set(mStateRaster.mDefault);
477 } else {
478 mRaster.set(pr);
479 }
480}
481
Jason Samscfb1d112009-08-05 13:57:03 -0700482void Context::allocationCheck(const Allocation *a)
483{
484 mVertex->checkUpdatedAllocation(a);
485 mFragment->checkUpdatedAllocation(a);
486 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700487}
488
489void Context::setVertex(ProgramVertex *pv)
490{
Jason Sams8ce125b2009-06-17 16:52:59 -0700491 if (pv == NULL) {
492 mVertex.set(mStateVertex.mDefault);
493 } else {
494 mVertex.set(pv);
495 }
Jason Samsc2f94902009-10-15 18:45:45 -0700496 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700497}
498
Jason Samsa4a54e42009-06-10 18:39:40 -0700499void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700500{
501 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700502 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700503 mNames.add(obj);
504}
505
506void Context::removeName(ObjectBase *obj)
507{
508 for(size_t ct=0; ct < mNames.size(); ct++) {
509 if (obj == mNames[ct]) {
510 mNames.removeAt(ct);
511 return;
512 }
513 }
514}
515
516ObjectBase * Context::lookupName(const char *name) const
517{
518 for(size_t ct=0; ct < mNames.size(); ct++) {
519 if (!strcmp(name, mNames[ct]->getName())) {
520 return mNames[ct];
521 }
522 }
523 return NULL;
524}
525
Jason Samsa4a54e42009-06-10 18:39:40 -0700526void Context::appendNameDefines(String8 *str) const
527{
528 char buf[256];
529 for (size_t ct=0; ct < mNames.size(); ct++) {
530 str->append("#define NAMED_");
531 str->append(mNames[ct]->getName());
532 str->append(" ");
533 sprintf(buf, "%i\n", (int)mNames[ct]);
534 str->append(buf);
535 }
536}
537
Joe Onorato57b79ce2009-08-09 22:57:44 -0700538void Context::appendVarDefines(String8 *str) const
539{
540 char buf[256];
541 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
542 str->append("#define ");
543 str->append(mInt32Defines.keyAt(ct));
544 str->append(" ");
545 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
546 str->append(buf);
547
548 }
549 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
550 str->append("#define ");
551 str->append(mFloatDefines.keyAt(ct));
552 str->append(" ");
553 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
554 str->append(buf);
555 }
556}
557
Jason Sams50869382009-08-18 17:07:09 -0700558bool Context::objDestroyOOBInit()
559{
560 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
561 if (status) {
562 LOGE("Context::ObjDestroyOOBInit mutex init failure");
563 return false;
564 }
565 return true;
566}
567
568void Context::objDestroyOOBRun()
569{
570 if (mObjDestroy.mNeedToEmpty) {
571 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
572 if (status) {
573 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
574 return;
575 }
576
577 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700578 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700579 }
580 mObjDestroy.mDestroyList.clear();
581 mObjDestroy.mNeedToEmpty = false;
582
583 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
584 if (status) {
585 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
586 }
587 }
588}
589
590void Context::objDestroyOOBDestroy()
591{
592 rsAssert(!mObjDestroy.mNeedToEmpty);
593 pthread_mutex_destroy(&mObjDestroy.mMutex);
594}
595
596void Context::objDestroyAdd(ObjectBase *obj)
597{
598 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
599 if (status) {
600 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
601 return;
602 }
603
604 mObjDestroy.mNeedToEmpty = true;
605 mObjDestroy.mDestroyList.add(obj);
606
607 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
608 if (status) {
609 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
610 }
611}
612
Jason Sams8c401ef2009-10-06 13:58:47 -0700613uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
614{
615 //LOGE("getMessageToClient %i %i", bufferLen, wait);
616 if (!wait) {
617 if (mIO.mToClient.isEmpty()) {
618 // No message to get and not going to wait for one.
619 receiveLen = 0;
620 return 0;
621 }
622 }
623
624 //LOGE("getMessageToClient 2 con=%p", this);
625 uint32_t bytesData = 0;
626 uint32_t commandID = 0;
627 const void *d = mIO.mToClient.get(&commandID, &bytesData);
628 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
629
630 *receiveLen = bytesData;
631 if (bufferLen >= bytesData) {
632 memcpy(data, d, bytesData);
633 mIO.mToClient.next();
634 return commandID;
635 }
636 return 0;
637}
638
639bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
640{
641 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
642 if (cmdID == 0) {
643 LOGE("Attempting to send invalid command 0 to client.");
644 return false;
645 }
646 if (!waitForSpace) {
647 if (mIO.mToClient.getFreeSpace() < len) {
648 // Not enough room, and not waiting.
649 return false;
650 }
651 }
652 //LOGE("sendMessageToClient 2");
653 void *p = mIO.mToClient.reserve(len);
654 memcpy(p, data, len);
655 mIO.mToClient.commit(cmdID, len);
656 //LOGE("sendMessageToClient 3");
657 return true;
658}
659
660void Context::initToClient()
661{
662 while(!mRunning) {
663 usleep(100);
664 }
665}
666
667void Context::deinitToClient()
668{
669 mIO.mToClient.shutdown();
670}
Jason Sams50869382009-08-18 17:07:09 -0700671
Jason Samsa4a54e42009-06-10 18:39:40 -0700672
Jason Sams326e0dd2009-05-22 14:03:28 -0700673///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700674//
Jason Sams326e0dd2009-05-22 14:03:28 -0700675
676namespace android {
677namespace renderscript {
678
679
680void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
681{
682 Script *s = static_cast<Script *>(vs);
683 rsc->setRootScript(s);
684}
685
686void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
687{
688 Sampler *s = static_cast<Sampler *>(vs);
689
690 if (slot > RS_MAX_SAMPLER_SLOT) {
691 LOGE("Invalid sampler slot");
692 return;
693 }
694
695 s->bindToContext(&rsc->mStateSampler, slot);
696}
697
698void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
699{
700 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
701 rsc->setFragmentStore(pfs);
702}
703
704void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
705{
706 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
707 rsc->setFragment(pf);
708}
709
Jason Sams5fd09d82009-09-23 13:57:02 -0700710void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
711{
712 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
713 rsc->setRaster(pr);
714}
715
Jason Sams326e0dd2009-05-22 14:03:28 -0700716void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
717{
718 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
719 rsc->setVertex(pv);
720}
721
Jason Samsa4a54e42009-06-10 18:39:40 -0700722void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700723{
724 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700725 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700726}
Jason Sams326e0dd2009-05-22 14:03:28 -0700727
Jason Sams707aaf32009-08-18 14:14:24 -0700728void rsi_ObjDestroy(Context *rsc, void *obj)
729{
730 ObjectBase *ob = static_cast<ObjectBase *>(obj);
731 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700732 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700733}
734
Joe Onorato57b79ce2009-08-09 22:57:44 -0700735void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
736{
737 rsc->addInt32Define(name, value);
738}
739
740void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
741{
742 rsc->addFloatDefine(name, value);
743}
Jason Sams326e0dd2009-05-22 14:03:28 -0700744
Jason Sams86f1b232009-09-24 17:38:20 -0700745void rsi_ContextPause(Context *rsc)
746{
747 rsc->pause();
748}
749
750void rsi_ContextResume(Context *rsc)
751{
752 rsc->resume();
753}
754
Jason Sams326e0dd2009-05-22 14:03:28 -0700755}
756}
757
758
Jason Samsafcb25c2009-08-25 11:34:49 -0700759RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700760{
761 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700762 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700763 return rsc;
764}
765
766void rsContextDestroy(RsContext vrsc)
767{
768 Context * rsc = static_cast<Context *>(vrsc);
769 delete rsc;
770}
771
Jason Sams50869382009-08-18 17:07:09 -0700772void rsObjDestroyOOB(RsContext vrsc, void *obj)
773{
774 Context * rsc = static_cast<Context *>(vrsc);
775 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
776}
777
Jason Sams8c401ef2009-10-06 13:58:47 -0700778uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
779{
780 Context * rsc = static_cast<Context *>(vrsc);
781 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
782}
783
784void rsContextInitToClient(RsContext vrsc)
785{
786 Context * rsc = static_cast<Context *>(vrsc);
787 rsc->initToClient();
788}
789
790void rsContextDeinitToClient(RsContext vrsc)
791{
792 Context * rsc = static_cast<Context *>(vrsc);
793 rsc->deinitToClient();
794}
795