blob: cc39dacc72a7050b07a0d3f7d76c3bb72617fdde [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;
33pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070034
35void Context::initEGL()
36{
Jason Samsafcb25c2009-08-25 11:34:49 -070037 mEGL.mNumConfigs = -1;
38 EGLint configAttribs[128];
39 EGLint *configAttribsPtr = configAttribs;
Jason Sams326e0dd2009-05-22 14:03:28 -070040
Jason Samsafcb25c2009-08-25 11:34:49 -070041 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070042
Jason Samsafcb25c2009-08-25 11:34:49 -070043 configAttribsPtr[0] = EGL_SURFACE_TYPE;
44 configAttribsPtr[1] = EGL_WINDOW_BIT;
45 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070046
Jason Samsafcb25c2009-08-25 11:34:49 -070047 if (mUseDepth) {
48 configAttribsPtr[0] = EGL_DEPTH_SIZE;
49 configAttribsPtr[1] = 16;
50 configAttribsPtr += 2;
51 }
Jason Sams9397e302009-08-27 20:23:34 -070052
Jason Sams5fd09d82009-09-23 13:57:02 -070053 if (mDev->mForceSW) {
54 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
55 configAttribsPtr[1] = EGL_SLOW_CONFIG;
56 configAttribsPtr += 2;
57 }
58
Jason Samsafcb25c2009-08-25 11:34:49 -070059 configAttribsPtr[0] = EGL_NONE;
60 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Jason Sams6d751ef2009-10-08 12:55:06 -070062 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070063 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
64 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
65
66 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
67 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070068 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070069 }
70 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
71
72 if (mWndSurface) {
73 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
74 } else {
75 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
76 android_createDisplaySurface(),
77 NULL);
78 }
79
80 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
81 eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
82 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
83 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
84
85
86 mGL.mVersion = glGetString(GL_VERSION);
87 mGL.mVendor = glGetString(GL_VENDOR);
88 mGL.mRenderer = glGetString(GL_RENDERER);
89 mGL.mExtensions = glGetString(GL_EXTENSIONS);
90
Jason Sams9397e302009-08-27 20:23:34 -070091 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
92 LOGV("GL Version %s", mGL.mVersion);
93 LOGV("GL Vendor %s", mGL.mVendor);
94 LOGV("GL Renderer %s", mGL.mRenderer);
95 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -070096
Jason Sams306fb232009-08-25 17:09:59 -070097 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -070098 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -070099 } else {
100 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -0700101 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700102}
103
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700104bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700105{
106 ObjectBaseRef<ProgramFragment> frag(mFragment);
107 ObjectBaseRef<ProgramVertex> vtx(mVertex);
108 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700109 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700110
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700111 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700112
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700113 mFragment.set(frag);
114 mVertex.set(vtx);
115 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700116 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700117 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700118}
119
120
Jason Samsa44cb292009-06-04 17:58:03 -0700121bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700122{
Jason Sams1fddd902009-09-25 15:25:00 -0700123 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700124 timerSet(RS_TIMER_CLEAR_SWAP);
125 }
Jason Sams10308932009-06-09 12:15:30 -0700126 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700127
Jason Sams8c9534b2009-09-22 12:26:53 -0700128 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
129 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700130 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700131 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
132
Jason Sams928b7342009-06-08 18:50:13 -0700133 glClearColor(mRootScript->mEnviroment.mClearColor[0],
134 mRootScript->mEnviroment.mClearColor[1],
135 mRootScript->mEnviroment.mClearColor[2],
136 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700137 if (mUseDepth) {
138 glDepthMask(GL_TRUE);
139 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
140 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141 } else {
142 glClear(GL_COLOR_BUFFER_BIT);
143 }
Jason Sams306fb232009-08-25 17:09:59 -0700144
Jason Sams1fddd902009-09-25 15:25:00 -0700145 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700146 timerSet(RS_TIMER_SCRIPT);
147 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700148 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700149 bool ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700150
151 GLenum err = glGetError();
152 if (err != GL_NO_ERROR) {
153 LOGE("Pending GL Error, 0x%x", err);
154 }
155
Jason Samscfb1d112009-08-05 13:57:03 -0700156 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700157}
158
Jason Sams24371d92009-08-19 12:17:14 -0700159uint64_t Context::getTime() const
160{
161 struct timespec t;
162 clock_gettime(CLOCK_MONOTONIC, &t);
163 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
164}
165
166void Context::timerReset()
167{
168 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
169 mTimers[ct] = 0;
170 }
171}
172
173void Context::timerInit()
174{
175 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700176 mTimeFrame = mTimeLast;
177 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700178 mTimerActive = RS_TIMER_INTERNAL;
179 timerReset();
180}
181
Jason Sams1d54f102009-09-03 15:43:13 -0700182void Context::timerFrame()
183{
184 mTimeLastFrame = mTimeFrame;
185 mTimeFrame = getTime();
186}
187
Jason Sams24371d92009-08-19 12:17:14 -0700188void Context::timerSet(Timers tm)
189{
190 uint64_t last = mTimeLast;
191 mTimeLast = getTime();
192 mTimers[mTimerActive] += mTimeLast - last;
193 mTimerActive = tm;
194}
195
196void Context::timerPrint()
197{
198 double total = 0;
199 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
200 total += mTimers[ct];
201 }
Jason Sams1d54f102009-09-03 15:43:13 -0700202 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700203
Jason Sams1d54f102009-09-03 15:43:13 -0700204 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
205 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700206 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700207 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
208 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
209 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700210}
211
Jason Sams326e0dd2009-05-22 14:03:28 -0700212void Context::setupCheck()
213{
Jason Sams5fd09d82009-09-23 13:57:02 -0700214 mFragmentStore->setupGL(this, &mStateFragmentStore);
215 mFragment->setupGL(this, &mStateFragment);
216 mRaster->setupGL(this, &mStateRaster);
217 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700218}
219
Jason Sams1fddd902009-09-25 15:25:00 -0700220static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700221{
222 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700223 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700224 return 0 != strcmp(buf, "0");
225}
Jason Sams326e0dd2009-05-22 14:03:28 -0700226
227void * Context::threadProc(void *vrsc)
228{
229 Context *rsc = static_cast<Context *>(vrsc);
230
Jason Sams1fddd902009-09-25 15:25:00 -0700231 rsc->props.mLogTimes = getProp("debug.rs.profile");
232 rsc->props.mLogScripts = getProp("debug.rs.script");
233 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700234
Jason Sams326e0dd2009-05-22 14:03:28 -0700235 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700236
Jason Samse5769102009-06-19 16:03:18 -0700237 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
238 if (!tlsStruct) {
239 LOGE("Error allocating tls storage");
240 return NULL;
241 }
242 tlsStruct->mContext = rsc;
243 tlsStruct->mScript = NULL;
244 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
245 if (status) {
246 LOGE("pthread_setspecific %i", status);
247 }
248
Jason Sams5fd09d82009-09-23 13:57:02 -0700249 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
250 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700251 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700252 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700253 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700254 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700255 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700256 rsc->setFragmentStore(NULL);
257
Jason Sams326e0dd2009-05-22 14:03:28 -0700258 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700259 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700260 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700261 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700262 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700263
Jason Sams732f1c02009-06-18 16:58:42 -0700264 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700265 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700266 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700267 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
268 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700269 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700270 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700271 rsc->timerFrame();
272 rsc->timerSet(RS_TIMER_INTERNAL);
273 rsc->timerPrint();
274 rsc->timerReset();
275 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700276 }
Jason Sams24371d92009-08-19 12:17:14 -0700277 if (rsc->mObjDestroy.mNeedToEmpty) {
278 rsc->objDestroyOOBRun();
279 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700280 }
281
Jason Sams8c0ee652009-08-25 14:49:07 -0700282 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700283 rsc->mRaster.clear();
284 rsc->mFragment.clear();
285 rsc->mVertex.clear();
286 rsc->mFragmentStore.clear();
287 rsc->mRootScript.clear();
288 rsc->mStateRaster.deinit(rsc);
289 rsc->mStateVertex.deinit(rsc);
290 rsc->mStateFragment.deinit(rsc);
291 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700292 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700293
Jason Sams326e0dd2009-05-22 14:03:28 -0700294 glClearColor(0,0,0,0);
295 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700296 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
297 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700298 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700299 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700300 return NULL;
301}
302
Jason Samsafcb25c2009-08-25 11:34:49 -0700303Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700304{
Jason Samsfb03a222009-10-15 16:47:31 -0700305 pthread_mutex_lock(&gInitMutex);
306
Jason Sams326e0dd2009-05-22 14:03:28 -0700307 dev->addContext(this);
308 mDev = dev;
309 mRunning = false;
310 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700311 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700312 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700313 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700314
Jason Samsa658e902009-06-04 14:35:01 -0700315 int status;
316 pthread_attr_t threadAttr;
317
Jason Samsfb03a222009-10-15 16:47:31 -0700318 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700319 status = pthread_key_create(&gThreadTLSKey, NULL);
320 if (status) {
321 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700322 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700323 return;
324 }
Jason Samse5769102009-06-19 16:03:18 -0700325 }
Jason Samsfb03a222009-10-15 16:47:31 -0700326 gThreadTLSKeyCount++;
327 pthread_mutex_unlock(&gInitMutex);
328
329 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700330
Jason Samsa658e902009-06-04 14:35:01 -0700331 status = pthread_attr_init(&threadAttr);
332 if (status) {
333 LOGE("Failed to init thread attribute.");
334 return;
335 }
336
337 sched_param sparam;
338 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
339 pthread_attr_setschedparam(&threadAttr, &sparam);
340
Jason Sams992a0b72009-06-23 12:22:47 -0700341 mWndSurface = sur;
342
Jason Sams50869382009-08-18 17:07:09 -0700343 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700344 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700345 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700346
Jason Sams992a0b72009-06-23 12:22:47 -0700347 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700348 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 if (status) {
350 LOGE("Failed to start rs context thread.");
351 }
352
Jason Sams326e0dd2009-05-22 14:03:28 -0700353 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700354 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700355 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700356
Jason Samsa658e902009-06-04 14:35:01 -0700357 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700358}
359
360Context::~Context()
361{
Jason Sams8c0ee652009-08-25 14:49:07 -0700362 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700363 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700364 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700365 void *res;
366
Jason Sams8c0ee652009-08-25 14:49:07 -0700367 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700368 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700369 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700370
Jason Samsfb03a222009-10-15 16:47:31 -0700371 // Global structure cleanup.
372 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700373 if (mDev) {
374 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700375 --gThreadTLSKeyCount;
376 if (!gThreadTLSKeyCount) {
377 pthread_key_delete(gThreadTLSKey);
378 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700379 }
Jason Samsfb03a222009-10-15 16:47:31 -0700380 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700381
382 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700383}
384
Jason Sams86f1b232009-09-24 17:38:20 -0700385void Context::pause()
386{
387 mPaused = true;
388}
389
390void Context::resume()
391{
392 mPaused = false;
393}
394
Jason Sams326e0dd2009-05-22 14:03:28 -0700395void Context::setRootScript(Script *s)
396{
397 mRootScript.set(s);
398}
399
400void Context::setFragmentStore(ProgramFragmentStore *pfs)
401{
Jason Sams8ce125b2009-06-17 16:52:59 -0700402 if (pfs == NULL) {
403 mFragmentStore.set(mStateFragmentStore.mDefault);
404 } else {
405 mFragmentStore.set(pfs);
406 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700407}
408
409void Context::setFragment(ProgramFragment *pf)
410{
Jason Sams8ce125b2009-06-17 16:52:59 -0700411 if (pf == NULL) {
412 mFragment.set(mStateFragment.mDefault);
413 } else {
414 mFragment.set(pf);
415 }
Jason Samscfb1d112009-08-05 13:57:03 -0700416}
417
Jason Sams5fd09d82009-09-23 13:57:02 -0700418void Context::setRaster(ProgramRaster *pr)
419{
420 if (pr == NULL) {
421 mRaster.set(mStateRaster.mDefault);
422 } else {
423 mRaster.set(pr);
424 }
425}
426
Jason Samscfb1d112009-08-05 13:57:03 -0700427void Context::allocationCheck(const Allocation *a)
428{
429 mVertex->checkUpdatedAllocation(a);
430 mFragment->checkUpdatedAllocation(a);
431 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700432}
433
434void Context::setVertex(ProgramVertex *pv)
435{
Jason Sams8ce125b2009-06-17 16:52:59 -0700436 if (pv == NULL) {
437 mVertex.set(mStateVertex.mDefault);
438 } else {
439 mVertex.set(pv);
440 }
Jason Samsc2f94902009-10-15 18:45:45 -0700441 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700442}
443
Jason Samsa4a54e42009-06-10 18:39:40 -0700444void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700445{
446 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700447 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700448 mNames.add(obj);
449}
450
451void Context::removeName(ObjectBase *obj)
452{
453 for(size_t ct=0; ct < mNames.size(); ct++) {
454 if (obj == mNames[ct]) {
455 mNames.removeAt(ct);
456 return;
457 }
458 }
459}
460
461ObjectBase * Context::lookupName(const char *name) const
462{
463 for(size_t ct=0; ct < mNames.size(); ct++) {
464 if (!strcmp(name, mNames[ct]->getName())) {
465 return mNames[ct];
466 }
467 }
468 return NULL;
469}
470
Jason Samsa4a54e42009-06-10 18:39:40 -0700471void Context::appendNameDefines(String8 *str) const
472{
473 char buf[256];
474 for (size_t ct=0; ct < mNames.size(); ct++) {
475 str->append("#define NAMED_");
476 str->append(mNames[ct]->getName());
477 str->append(" ");
478 sprintf(buf, "%i\n", (int)mNames[ct]);
479 str->append(buf);
480 }
481}
482
Joe Onorato57b79ce2009-08-09 22:57:44 -0700483void Context::appendVarDefines(String8 *str) const
484{
485 char buf[256];
486 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
487 str->append("#define ");
488 str->append(mInt32Defines.keyAt(ct));
489 str->append(" ");
490 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
491 str->append(buf);
492
493 }
494 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
495 str->append("#define ");
496 str->append(mFloatDefines.keyAt(ct));
497 str->append(" ");
498 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
499 str->append(buf);
500 }
501}
502
Jason Sams50869382009-08-18 17:07:09 -0700503bool Context::objDestroyOOBInit()
504{
505 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
506 if (status) {
507 LOGE("Context::ObjDestroyOOBInit mutex init failure");
508 return false;
509 }
510 return true;
511}
512
513void Context::objDestroyOOBRun()
514{
515 if (mObjDestroy.mNeedToEmpty) {
516 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
517 if (status) {
518 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
519 return;
520 }
521
522 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700523 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700524 }
525 mObjDestroy.mDestroyList.clear();
526 mObjDestroy.mNeedToEmpty = false;
527
528 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
529 if (status) {
530 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
531 }
532 }
533}
534
535void Context::objDestroyOOBDestroy()
536{
537 rsAssert(!mObjDestroy.mNeedToEmpty);
538 pthread_mutex_destroy(&mObjDestroy.mMutex);
539}
540
541void Context::objDestroyAdd(ObjectBase *obj)
542{
543 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
544 if (status) {
545 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
546 return;
547 }
548
549 mObjDestroy.mNeedToEmpty = true;
550 mObjDestroy.mDestroyList.add(obj);
551
552 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
553 if (status) {
554 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
555 }
556}
557
Jason Sams8c401ef2009-10-06 13:58:47 -0700558uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
559{
560 //LOGE("getMessageToClient %i %i", bufferLen, wait);
561 if (!wait) {
562 if (mIO.mToClient.isEmpty()) {
563 // No message to get and not going to wait for one.
564 receiveLen = 0;
565 return 0;
566 }
567 }
568
569 //LOGE("getMessageToClient 2 con=%p", this);
570 uint32_t bytesData = 0;
571 uint32_t commandID = 0;
572 const void *d = mIO.mToClient.get(&commandID, &bytesData);
573 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
574
575 *receiveLen = bytesData;
576 if (bufferLen >= bytesData) {
577 memcpy(data, d, bytesData);
578 mIO.mToClient.next();
579 return commandID;
580 }
581 return 0;
582}
583
584bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
585{
586 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
587 if (cmdID == 0) {
588 LOGE("Attempting to send invalid command 0 to client.");
589 return false;
590 }
591 if (!waitForSpace) {
592 if (mIO.mToClient.getFreeSpace() < len) {
593 // Not enough room, and not waiting.
594 return false;
595 }
596 }
597 //LOGE("sendMessageToClient 2");
598 void *p = mIO.mToClient.reserve(len);
599 memcpy(p, data, len);
600 mIO.mToClient.commit(cmdID, len);
601 //LOGE("sendMessageToClient 3");
602 return true;
603}
604
605void Context::initToClient()
606{
607 while(!mRunning) {
608 usleep(100);
609 }
610}
611
612void Context::deinitToClient()
613{
614 mIO.mToClient.shutdown();
615}
Jason Sams50869382009-08-18 17:07:09 -0700616
Jason Samsa4a54e42009-06-10 18:39:40 -0700617
Jason Sams326e0dd2009-05-22 14:03:28 -0700618///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700619//
Jason Sams326e0dd2009-05-22 14:03:28 -0700620
621namespace android {
622namespace renderscript {
623
624
625void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
626{
627 Script *s = static_cast<Script *>(vs);
628 rsc->setRootScript(s);
629}
630
631void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
632{
633 Sampler *s = static_cast<Sampler *>(vs);
634
635 if (slot > RS_MAX_SAMPLER_SLOT) {
636 LOGE("Invalid sampler slot");
637 return;
638 }
639
640 s->bindToContext(&rsc->mStateSampler, slot);
641}
642
643void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
644{
645 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
646 rsc->setFragmentStore(pfs);
647}
648
649void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
650{
651 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
652 rsc->setFragment(pf);
653}
654
Jason Sams5fd09d82009-09-23 13:57:02 -0700655void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
656{
657 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
658 rsc->setRaster(pr);
659}
660
Jason Sams326e0dd2009-05-22 14:03:28 -0700661void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
662{
663 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
664 rsc->setVertex(pv);
665}
666
Jason Samsa4a54e42009-06-10 18:39:40 -0700667void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700668{
669 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700670 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700671}
Jason Sams326e0dd2009-05-22 14:03:28 -0700672
Jason Sams707aaf32009-08-18 14:14:24 -0700673void rsi_ObjDestroy(Context *rsc, void *obj)
674{
675 ObjectBase *ob = static_cast<ObjectBase *>(obj);
676 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700677 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700678}
679
Joe Onorato57b79ce2009-08-09 22:57:44 -0700680void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
681{
682 rsc->addInt32Define(name, value);
683}
684
685void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
686{
687 rsc->addFloatDefine(name, value);
688}
Jason Sams326e0dd2009-05-22 14:03:28 -0700689
Jason Sams86f1b232009-09-24 17:38:20 -0700690void rsi_ContextPause(Context *rsc)
691{
692 rsc->pause();
693}
694
695void rsi_ContextResume(Context *rsc)
696{
697 rsc->resume();
698}
699
Jason Sams326e0dd2009-05-22 14:03:28 -0700700}
701}
702
703
Jason Samsafcb25c2009-08-25 11:34:49 -0700704RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700705{
706 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700707 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700708 return rsc;
709}
710
711void rsContextDestroy(RsContext vrsc)
712{
713 Context * rsc = static_cast<Context *>(vrsc);
714 delete rsc;
715}
716
Jason Sams50869382009-08-18 17:07:09 -0700717void rsObjDestroyOOB(RsContext vrsc, void *obj)
718{
719 Context * rsc = static_cast<Context *>(vrsc);
720 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
721}
722
Jason Sams8c401ef2009-10-06 13:58:47 -0700723uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
724{
725 Context * rsc = static_cast<Context *>(vrsc);
726 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
727}
728
729void rsContextInitToClient(RsContext vrsc)
730{
731 Context * rsc = static_cast<Context *>(vrsc);
732 rsc->initToClient();
733}
734
735void rsContextDeinitToClient(RsContext vrsc)
736{
737 Context * rsc = static_cast<Context *>(vrsc);
738 rsc->deinitToClient();
739}
740