blob: 961ec0b2709cb71fdca4c476f1f7be619765fd93 [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 Samsbf3c14e2009-11-02 14:25:10 -0800353 rsc->mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700354 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700355 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700356 return NULL;
357}
358
Jason Samsafcb25c2009-08-25 11:34:49 -0700359Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700360{
Jason Samsfb03a222009-10-15 16:47:31 -0700361 pthread_mutex_lock(&gInitMutex);
362
Jason Sams326e0dd2009-05-22 14:03:28 -0700363 dev->addContext(this);
364 mDev = dev;
365 mRunning = false;
366 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700367 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700368 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700369 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700370
Jason Samsa658e902009-06-04 14:35:01 -0700371 int status;
372 pthread_attr_t threadAttr;
373
Jason Samsfb03a222009-10-15 16:47:31 -0700374 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700375 status = pthread_key_create(&gThreadTLSKey, NULL);
376 if (status) {
377 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700378 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700379 return;
380 }
Jason Samse5769102009-06-19 16:03:18 -0700381 }
Jason Samsfb03a222009-10-15 16:47:31 -0700382 gThreadTLSKeyCount++;
383 pthread_mutex_unlock(&gInitMutex);
384
385 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700386
Jason Samsa658e902009-06-04 14:35:01 -0700387 status = pthread_attr_init(&threadAttr);
388 if (status) {
389 LOGE("Failed to init thread attribute.");
390 return;
391 }
392
393 sched_param sparam;
394 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
395 pthread_attr_setschedparam(&threadAttr, &sparam);
396
Jason Sams992a0b72009-06-23 12:22:47 -0700397 mWndSurface = sur;
398
Jason Sams50869382009-08-18 17:07:09 -0700399 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700400 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700401 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700402
Jason Sams992a0b72009-06-23 12:22:47 -0700403 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700404 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700405 if (status) {
406 LOGE("Failed to start rs context thread.");
407 }
408
Jason Sams326e0dd2009-05-22 14:03:28 -0700409 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700410 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700411 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700412
Jason Samsa658e902009-06-04 14:35:01 -0700413 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700414}
415
416Context::~Context()
417{
Jason Sams8c0ee652009-08-25 14:49:07 -0700418 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700419 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700420 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700421 void *res;
422
Jason Sams8c0ee652009-08-25 14:49:07 -0700423 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700424 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800425 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700426 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700427
Jason Samsfb03a222009-10-15 16:47:31 -0700428 // Global structure cleanup.
429 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700430 if (mDev) {
431 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700432 --gThreadTLSKeyCount;
433 if (!gThreadTLSKeyCount) {
434 pthread_key_delete(gThreadTLSKey);
435 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800436 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700437 }
Jason Samsfb03a222009-10-15 16:47:31 -0700438 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700439
440 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700441}
442
Jason Sams86f1b232009-09-24 17:38:20 -0700443void Context::pause()
444{
445 mPaused = true;
446}
447
448void Context::resume()
449{
450 mPaused = false;
451}
452
Jason Sams326e0dd2009-05-22 14:03:28 -0700453void Context::setRootScript(Script *s)
454{
455 mRootScript.set(s);
456}
457
458void Context::setFragmentStore(ProgramFragmentStore *pfs)
459{
Jason Sams8ce125b2009-06-17 16:52:59 -0700460 if (pfs == NULL) {
461 mFragmentStore.set(mStateFragmentStore.mDefault);
462 } else {
463 mFragmentStore.set(pfs);
464 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700465}
466
467void Context::setFragment(ProgramFragment *pf)
468{
Jason Sams8ce125b2009-06-17 16:52:59 -0700469 if (pf == NULL) {
470 mFragment.set(mStateFragment.mDefault);
471 } else {
472 mFragment.set(pf);
473 }
Jason Samscfb1d112009-08-05 13:57:03 -0700474}
475
Jason Sams5fd09d82009-09-23 13:57:02 -0700476void Context::setRaster(ProgramRaster *pr)
477{
478 if (pr == NULL) {
479 mRaster.set(mStateRaster.mDefault);
480 } else {
481 mRaster.set(pr);
482 }
483}
484
Jason Samscfb1d112009-08-05 13:57:03 -0700485void Context::allocationCheck(const Allocation *a)
486{
487 mVertex->checkUpdatedAllocation(a);
488 mFragment->checkUpdatedAllocation(a);
489 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700490}
491
492void Context::setVertex(ProgramVertex *pv)
493{
Jason Sams8ce125b2009-06-17 16:52:59 -0700494 if (pv == NULL) {
495 mVertex.set(mStateVertex.mDefault);
496 } else {
497 mVertex.set(pv);
498 }
Jason Samsc2f94902009-10-15 18:45:45 -0700499 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700500}
501
Jason Samsa4a54e42009-06-10 18:39:40 -0700502void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700503{
504 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700505 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700506 mNames.add(obj);
507}
508
509void Context::removeName(ObjectBase *obj)
510{
511 for(size_t ct=0; ct < mNames.size(); ct++) {
512 if (obj == mNames[ct]) {
513 mNames.removeAt(ct);
514 return;
515 }
516 }
517}
518
519ObjectBase * Context::lookupName(const char *name) const
520{
521 for(size_t ct=0; ct < mNames.size(); ct++) {
522 if (!strcmp(name, mNames[ct]->getName())) {
523 return mNames[ct];
524 }
525 }
526 return NULL;
527}
528
Jason Samsa4a54e42009-06-10 18:39:40 -0700529void Context::appendNameDefines(String8 *str) const
530{
531 char buf[256];
532 for (size_t ct=0; ct < mNames.size(); ct++) {
533 str->append("#define NAMED_");
534 str->append(mNames[ct]->getName());
535 str->append(" ");
536 sprintf(buf, "%i\n", (int)mNames[ct]);
537 str->append(buf);
538 }
539}
540
Joe Onorato57b79ce2009-08-09 22:57:44 -0700541void Context::appendVarDefines(String8 *str) const
542{
543 char buf[256];
544 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
545 str->append("#define ");
546 str->append(mInt32Defines.keyAt(ct));
547 str->append(" ");
548 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
549 str->append(buf);
550
551 }
552 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
553 str->append("#define ");
554 str->append(mFloatDefines.keyAt(ct));
555 str->append(" ");
556 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
557 str->append(buf);
558 }
559}
560
Jason Sams50869382009-08-18 17:07:09 -0700561bool Context::objDestroyOOBInit()
562{
563 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
564 if (status) {
565 LOGE("Context::ObjDestroyOOBInit mutex init failure");
566 return false;
567 }
568 return true;
569}
570
571void Context::objDestroyOOBRun()
572{
573 if (mObjDestroy.mNeedToEmpty) {
574 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
575 if (status) {
576 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
577 return;
578 }
579
580 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700581 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700582 }
583 mObjDestroy.mDestroyList.clear();
584 mObjDestroy.mNeedToEmpty = false;
585
586 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
587 if (status) {
588 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
589 }
590 }
591}
592
593void Context::objDestroyOOBDestroy()
594{
595 rsAssert(!mObjDestroy.mNeedToEmpty);
596 pthread_mutex_destroy(&mObjDestroy.mMutex);
597}
598
599void Context::objDestroyAdd(ObjectBase *obj)
600{
601 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
602 if (status) {
603 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
604 return;
605 }
606
607 mObjDestroy.mNeedToEmpty = true;
608 mObjDestroy.mDestroyList.add(obj);
609
610 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
611 if (status) {
612 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
613 }
614}
615
Jason Sams8c401ef2009-10-06 13:58:47 -0700616uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
617{
618 //LOGE("getMessageToClient %i %i", bufferLen, wait);
619 if (!wait) {
620 if (mIO.mToClient.isEmpty()) {
621 // No message to get and not going to wait for one.
622 receiveLen = 0;
623 return 0;
624 }
625 }
626
627 //LOGE("getMessageToClient 2 con=%p", this);
628 uint32_t bytesData = 0;
629 uint32_t commandID = 0;
630 const void *d = mIO.mToClient.get(&commandID, &bytesData);
631 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
632
633 *receiveLen = bytesData;
634 if (bufferLen >= bytesData) {
635 memcpy(data, d, bytesData);
636 mIO.mToClient.next();
637 return commandID;
638 }
639 return 0;
640}
641
642bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
643{
644 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
645 if (cmdID == 0) {
646 LOGE("Attempting to send invalid command 0 to client.");
647 return false;
648 }
649 if (!waitForSpace) {
650 if (mIO.mToClient.getFreeSpace() < len) {
651 // Not enough room, and not waiting.
652 return false;
653 }
654 }
655 //LOGE("sendMessageToClient 2");
656 void *p = mIO.mToClient.reserve(len);
657 memcpy(p, data, len);
658 mIO.mToClient.commit(cmdID, len);
659 //LOGE("sendMessageToClient 3");
660 return true;
661}
662
663void Context::initToClient()
664{
665 while(!mRunning) {
666 usleep(100);
667 }
668}
669
670void Context::deinitToClient()
671{
672 mIO.mToClient.shutdown();
673}
Jason Sams50869382009-08-18 17:07:09 -0700674
Jason Samsa4a54e42009-06-10 18:39:40 -0700675
Jason Sams326e0dd2009-05-22 14:03:28 -0700676///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700677//
Jason Sams326e0dd2009-05-22 14:03:28 -0700678
679namespace android {
680namespace renderscript {
681
682
683void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
684{
685 Script *s = static_cast<Script *>(vs);
686 rsc->setRootScript(s);
687}
688
689void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
690{
691 Sampler *s = static_cast<Sampler *>(vs);
692
693 if (slot > RS_MAX_SAMPLER_SLOT) {
694 LOGE("Invalid sampler slot");
695 return;
696 }
697
698 s->bindToContext(&rsc->mStateSampler, slot);
699}
700
701void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
702{
703 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
704 rsc->setFragmentStore(pfs);
705}
706
707void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
708{
709 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
710 rsc->setFragment(pf);
711}
712
Jason Sams5fd09d82009-09-23 13:57:02 -0700713void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
714{
715 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
716 rsc->setRaster(pr);
717}
718
Jason Sams326e0dd2009-05-22 14:03:28 -0700719void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
720{
721 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
722 rsc->setVertex(pv);
723}
724
Jason Samsa4a54e42009-06-10 18:39:40 -0700725void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700726{
727 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700728 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700729}
Jason Sams326e0dd2009-05-22 14:03:28 -0700730
Jason Sams707aaf32009-08-18 14:14:24 -0700731void rsi_ObjDestroy(Context *rsc, void *obj)
732{
733 ObjectBase *ob = static_cast<ObjectBase *>(obj);
734 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700735 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700736}
737
Joe Onorato57b79ce2009-08-09 22:57:44 -0700738void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
739{
740 rsc->addInt32Define(name, value);
741}
742
743void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
744{
745 rsc->addFloatDefine(name, value);
746}
Jason Sams326e0dd2009-05-22 14:03:28 -0700747
Jason Sams86f1b232009-09-24 17:38:20 -0700748void rsi_ContextPause(Context *rsc)
749{
750 rsc->pause();
751}
752
753void rsi_ContextResume(Context *rsc)
754{
755 rsc->resume();
756}
757
Jason Sams326e0dd2009-05-22 14:03:28 -0700758}
759}
760
761
Jason Samsafcb25c2009-08-25 11:34:49 -0700762RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700763{
764 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700765 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700766 return rsc;
767}
768
769void rsContextDestroy(RsContext vrsc)
770{
771 Context * rsc = static_cast<Context *>(vrsc);
772 delete rsc;
773}
774
Jason Sams50869382009-08-18 17:07:09 -0700775void rsObjDestroyOOB(RsContext vrsc, void *obj)
776{
777 Context * rsc = static_cast<Context *>(vrsc);
778 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
779}
780
Jason Sams8c401ef2009-10-06 13:58:47 -0700781uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
782{
783 Context * rsc = static_cast<Context *>(vrsc);
784 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
785}
786
787void rsContextInitToClient(RsContext vrsc)
788{
789 Context * rsc = static_cast<Context *>(vrsc);
790 rsc->initToClient();
791}
792
793void rsContextDeinitToClient(RsContext vrsc)
794{
795 Context * rsc = static_cast<Context *>(vrsc);
796 rsc->deinitToClient();
797}
798