blob: 2b3d0765bea88b089397489694d7e9558328be15 [file] [log] [blame]
Jason Samsd19f10d2009-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 Agopian3142f4f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsb13ada52009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070022
Joe Onorato9ac2c662009-09-23 16:37:36 -070023#include <cutils/properties.h>
24
Jason Sams4b962e52009-06-22 17:15:15 -070025#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Jason Samsd19f10d2009-05-22 14:03:28 -070028using namespace android;
29using namespace android::renderscript;
30
Jason Sams462d11b2009-06-19 16:03:18 -070031pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams41c19db92009-10-15 16:47:31 -070032uint32_t Context::gThreadTLSKeyCount = 0;
33pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Samsd19f10d2009-05-22 14:03:28 -070034
35void Context::initEGL()
36{
Jason Samsb13ada52009-08-25 11:34:49 -070037 mEGL.mNumConfigs = -1;
38 EGLint configAttribs[128];
39 EGLint *configAttribsPtr = configAttribs;
Jason Samsd19f10d2009-05-22 14:03:28 -070040
Jason Samsb13ada52009-08-25 11:34:49 -070041 memset(configAttribs, 0, sizeof(configAttribs));
Jason Samsd19f10d2009-05-22 14:03:28 -070042
Jason Samsb13ada52009-08-25 11:34:49 -070043 configAttribsPtr[0] = EGL_SURFACE_TYPE;
44 configAttribsPtr[1] = EGL_WINDOW_BIT;
45 configAttribsPtr += 2;
Jason Samsd19f10d2009-05-22 14:03:28 -070046
Jason Samsb13ada52009-08-25 11:34:49 -070047 if (mUseDepth) {
48 configAttribsPtr[0] = EGL_DEPTH_SIZE;
49 configAttribsPtr[1] = 16;
50 configAttribsPtr += 2;
51 }
Jason Sams07ae4062009-08-27 20:23:34 -070052
Jason Samsebfb4362009-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 Samsb13ada52009-08-25 11:34:49 -070059 configAttribsPtr[0] = EGL_NONE;
60 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Samsd19f10d2009-05-22 14:03:28 -070061
Jason Sams6a17e162009-10-08 12:55:06 -070062 LOGV("initEGL start");
Jason Samsb13ada52009-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 Sams07ae4062009-08-27 20:23:34 -070068 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsb13ada52009-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 Sams07ae4062009-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 Samsb13ada52009-08-25 11:34:49 -070096
Jason Sams67c68442009-08-25 17:09:59 -070097 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsb13ada52009-08-25 11:34:49 -070098 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams67c68442009-08-25 17:09:59 -070099 } else {
100 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsb13ada52009-08-25 11:34:49 -0700101 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700102}
103
Jason Sams3eaa3382009-06-10 15:04:38 -0700104bool Context::runScript(Script *s, uint32_t launchID)
Jason Samsda423d82009-06-09 12:15:30 -0700105{
106 ObjectBaseRef<ProgramFragment> frag(mFragment);
107 ObjectBaseRef<ProgramVertex> vtx(mVertex);
108 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Sams5235cf32009-09-28 18:12:56 -0700109 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Samsda423d82009-06-09 12:15:30 -0700110
Jason Sams3eaa3382009-06-10 15:04:38 -0700111 bool ret = s->run(this, launchID);
Jason Samsda423d82009-06-09 12:15:30 -0700112
Jason Sams3eaa3382009-06-10 15:04:38 -0700113 mFragment.set(frag);
114 mVertex.set(vtx);
115 mFragmentStore.set(store);
Jason Sams5235cf32009-09-28 18:12:56 -0700116 mRaster.set(raster);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700117 return ret;
Jason Samsda423d82009-06-09 12:15:30 -0700118}
119
120
Jason Samsa09f11d2009-06-04 17:58:03 -0700121bool Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -0700122{
Jason Sams66b27712009-09-25 15:25:00 -0700123 if (props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700124 timerSet(RS_TIMER_CLEAR_SWAP);
125 }
Jason Samsda423d82009-06-09 12:15:30 -0700126 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -0700127
Jason Sams59038ca2009-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 Samsb13ada52009-08-25 11:34:49 -0700130 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -0700131 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
132
Jason Sams928f5cf2009-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 Samsb13ada52009-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 Sams67c68442009-08-25 17:09:59 -0700144
Jason Sams66b27712009-09-25 15:25:00 -0700145 if (this->props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700146 timerSet(RS_TIMER_SCRIPT);
147 }
Jason Sams516c3192009-10-06 13:58:47 -0700148 mStateFragmentStore.mLast.clear();
Jason Sams9bee51c2009-08-05 13:57:03 -0700149 bool ret = runScript(mRootScript.get(), 0);
Jason Samsc7412b32009-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 Sams9bee51c2009-08-05 13:57:03 -0700156 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700157}
158
Jason Samsf4d16062009-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 Sams2525a812009-09-03 15:43:13 -0700176 mTimeFrame = mTimeLast;
177 mTimeLastFrame = mTimeLast;
Jason Samsf4d16062009-08-19 12:17:14 -0700178 mTimerActive = RS_TIMER_INTERNAL;
179 timerReset();
180}
181
Jason Sams2525a812009-09-03 15:43:13 -0700182void Context::timerFrame()
183{
184 mTimeLastFrame = mTimeFrame;
185 mTimeFrame = getTime();
186}
187
Jason Samsf4d16062009-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 Sams2525a812009-09-03 15:43:13 -0700202 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Samsf4d16062009-08-19 12:17:14 -0700203
Jason Sams2525a812009-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 Samsf4d16062009-08-19 12:17:14 -0700206 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsea84a7c2009-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 Samsf4d16062009-08-19 12:17:14 -0700210}
211
Jason Samsd19f10d2009-05-22 14:03:28 -0700212void Context::setupCheck()
213{
Jason Samsebfb4362009-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 Samsd19f10d2009-05-22 14:03:28 -0700218}
219
Jason Sams66b27712009-09-25 15:25:00 -0700220static bool getProp(const char *str)
Joe Onorato9ac2c662009-09-23 16:37:36 -0700221{
222 char buf[PROPERTY_VALUE_MAX];
Jason Sams66b27712009-09-25 15:25:00 -0700223 property_get(str, buf, "0");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700224 return 0 != strcmp(buf, "0");
225}
Jason Samsd19f10d2009-05-22 14:03:28 -0700226
227void * Context::threadProc(void *vrsc)
228{
229 Context *rsc = static_cast<Context *>(vrsc);
230
Jason Sams66b27712009-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 Onorato9ac2c662009-09-23 16:37:36 -0700234
Jason Samsd19f10d2009-05-22 14:03:28 -0700235 rsc->initEGL();
Jason Sams9c54bdb2009-06-17 16:52:59 -0700236
Jason Sams462d11b2009-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 Samsebfb4362009-09-23 13:57:02 -0700249 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
250 rsc->setRaster(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700251 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700252 rsc->setVertex(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700253 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700254 rsc->setFragment(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700255 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700256 rsc->setFragmentStore(NULL);
257
Jason Samsd19f10d2009-05-22 14:03:28 -0700258 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700259 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700260 while (!rsc->mExit) {
Jason Samsbc948de2009-08-17 18:35:48 -0700261 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams5f7fc272009-06-18 16:58:42 -0700262 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700263
Jason Sams5f7fc272009-06-18 16:58:42 -0700264 if (mDraw) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700265 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams66b27712009-09-25 15:25:00 -0700266 if (rsc->props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700267 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
268 }
Jason Samsb13ada52009-08-25 11:34:49 -0700269 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams66b27712009-09-25 15:25:00 -0700270 if (rsc->props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700271 rsc->timerFrame();
272 rsc->timerSet(RS_TIMER_INTERNAL);
273 rsc->timerPrint();
274 rsc->timerReset();
275 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700276 }
Jason Samsf4d16062009-08-19 12:17:14 -0700277 if (rsc->mObjDestroy.mNeedToEmpty) {
278 rsc->objDestroyOOBRun();
279 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700280 }
281
Jason Samsf5b45962009-08-25 14:49:07 -0700282 LOGV("RS Thread exiting");
Jason Sams61f08d62009-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 Samsa9e7a052009-09-25 14:51:22 -0700292 ObjectBase::zeroAllUserRef(rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700293
Jason Samsd19f10d2009-05-22 14:03:28 -0700294 glClearColor(0,0,0,0);
295 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsb13ada52009-08-25 11:34:49 -0700296 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
297 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams730ee652009-08-18 17:07:09 -0700298 rsc->objDestroyOOBRun();
Jason Samsf5b45962009-08-25 14:49:07 -0700299 LOGV("RS Thread exited");
Jason Samsd19f10d2009-05-22 14:03:28 -0700300 return NULL;
301}
302
Jason Samsb13ada52009-08-25 11:34:49 -0700303Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700304{
Jason Sams41c19db92009-10-15 16:47:31 -0700305 pthread_mutex_lock(&gInitMutex);
306
Jason Samsd19f10d2009-05-22 14:03:28 -0700307 dev->addContext(this);
308 mDev = dev;
309 mRunning = false;
310 mExit = false;
Jason Samsb13ada52009-08-25 11:34:49 -0700311 mUseDepth = useDepth;
Jason Sams65e7aa52009-09-24 17:38:20 -0700312 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700313 mObjHead = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700314
Jason Sams8ad00102009-06-04 14:35:01 -0700315 int status;
316 pthread_attr_t threadAttr;
317
Jason Sams41c19db92009-10-15 16:47:31 -0700318 if (!gThreadTLSKeyCount) {
Jason Sams996db8d2009-10-06 17:16:55 -0700319 status = pthread_key_create(&gThreadTLSKey, NULL);
320 if (status) {
321 LOGE("Failed to init thread tls key.");
Jason Sams41c19db92009-10-15 16:47:31 -0700322 pthread_mutex_unlock(&gInitMutex);
Jason Sams996db8d2009-10-06 17:16:55 -0700323 return;
324 }
Jason Sams462d11b2009-06-19 16:03:18 -0700325 }
Jason Sams41c19db92009-10-15 16:47:31 -0700326 gThreadTLSKeyCount++;
327 pthread_mutex_unlock(&gInitMutex);
328
329 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700330
Jason Sams8ad00102009-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 Samsf29ca502009-06-23 12:22:47 -0700341 mWndSurface = sur;
342
Jason Sams730ee652009-08-18 17:07:09 -0700343 objDestroyOOBInit();
Jason Samsf4d16062009-08-19 12:17:14 -0700344 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700345 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700346
Jason Samsf29ca502009-06-23 12:22:47 -0700347 LOGV("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700348 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700349 if (status) {
350 LOGE("Failed to start rs context thread.");
351 }
352
Jason Samsd19f10d2009-05-22 14:03:28 -0700353 while(!mRunning) {
Jason Samse60446b2009-09-24 14:55:38 -0700354 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700355 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700356
Jason Sams8ad00102009-06-04 14:35:01 -0700357 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700358}
359
360Context::~Context()
361{
Jason Samsf5b45962009-08-25 14:49:07 -0700362 LOGV("Context::~Context");
Jason Samsd19f10d2009-05-22 14:03:28 -0700363 mExit = true;
Jason Sams65e7aa52009-09-24 17:38:20 -0700364 mPaused = false;
Jason Samsd19f10d2009-05-22 14:03:28 -0700365 void *res;
366
Jason Samsf5b45962009-08-25 14:49:07 -0700367 mIO.shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -0700368 int status = pthread_join(mThreadId, &res);
Jason Sams730ee652009-08-18 17:07:09 -0700369 objDestroyOOBRun();
Jason Samsd19f10d2009-05-22 14:03:28 -0700370
Jason Sams41c19db92009-10-15 16:47:31 -0700371 // Global structure cleanup.
372 pthread_mutex_lock(&gInitMutex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700373 if (mDev) {
374 mDev->removeContext(this);
Jason Sams41c19db92009-10-15 16:47:31 -0700375 --gThreadTLSKeyCount;
376 if (!gThreadTLSKeyCount) {
377 pthread_key_delete(gThreadTLSKey);
378 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700379 }
Jason Sams41c19db92009-10-15 16:47:31 -0700380 pthread_mutex_unlock(&gInitMutex);
Jason Sams730ee652009-08-18 17:07:09 -0700381
382 objDestroyOOBDestroy();
Jason Samsd19f10d2009-05-22 14:03:28 -0700383}
384
Jason Sams65e7aa52009-09-24 17:38:20 -0700385void Context::pause()
386{
387 mPaused = true;
388}
389
390void Context::resume()
391{
392 mPaused = false;
393}
394
Jason Samsd19f10d2009-05-22 14:03:28 -0700395void Context::setRootScript(Script *s)
396{
397 mRootScript.set(s);
398}
399
400void Context::setFragmentStore(ProgramFragmentStore *pfs)
401{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700402 if (pfs == NULL) {
403 mFragmentStore.set(mStateFragmentStore.mDefault);
404 } else {
405 mFragmentStore.set(pfs);
406 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700407}
408
409void Context::setFragment(ProgramFragment *pf)
410{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700411 if (pf == NULL) {
412 mFragment.set(mStateFragment.mDefault);
413 } else {
414 mFragment.set(pf);
415 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700416}
417
Jason Samsebfb4362009-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 Sams9bee51c2009-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 Samsd19f10d2009-05-22 14:03:28 -0700432}
433
434void Context::setVertex(ProgramVertex *pv)
435{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700436 if (pv == NULL) {
437 mVertex.set(mStateVertex.mDefault);
438 } else {
439 mVertex.set(pv);
440 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700441}
442
Jason Samsd5680f92009-06-10 18:39:40 -0700443void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700444{
445 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700446 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700447 mNames.add(obj);
448}
449
450void Context::removeName(ObjectBase *obj)
451{
452 for(size_t ct=0; ct < mNames.size(); ct++) {
453 if (obj == mNames[ct]) {
454 mNames.removeAt(ct);
455 return;
456 }
457 }
458}
459
460ObjectBase * Context::lookupName(const char *name) const
461{
462 for(size_t ct=0; ct < mNames.size(); ct++) {
463 if (!strcmp(name, mNames[ct]->getName())) {
464 return mNames[ct];
465 }
466 }
467 return NULL;
468}
469
Jason Samsd5680f92009-06-10 18:39:40 -0700470void Context::appendNameDefines(String8 *str) const
471{
472 char buf[256];
473 for (size_t ct=0; ct < mNames.size(); ct++) {
474 str->append("#define NAMED_");
475 str->append(mNames[ct]->getName());
476 str->append(" ");
477 sprintf(buf, "%i\n", (int)mNames[ct]);
478 str->append(buf);
479 }
480}
481
Joe Onoratod7b37742009-08-09 22:57:44 -0700482void Context::appendVarDefines(String8 *str) const
483{
484 char buf[256];
485 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
486 str->append("#define ");
487 str->append(mInt32Defines.keyAt(ct));
488 str->append(" ");
489 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
490 str->append(buf);
491
492 }
493 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
494 str->append("#define ");
495 str->append(mFloatDefines.keyAt(ct));
496 str->append(" ");
497 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
498 str->append(buf);
499 }
500}
501
Jason Sams730ee652009-08-18 17:07:09 -0700502bool Context::objDestroyOOBInit()
503{
504 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
505 if (status) {
506 LOGE("Context::ObjDestroyOOBInit mutex init failure");
507 return false;
508 }
509 return true;
510}
511
512void Context::objDestroyOOBRun()
513{
514 if (mObjDestroy.mNeedToEmpty) {
515 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
516 if (status) {
517 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
518 return;
519 }
520
521 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams07ae4062009-08-27 20:23:34 -0700522 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams730ee652009-08-18 17:07:09 -0700523 }
524 mObjDestroy.mDestroyList.clear();
525 mObjDestroy.mNeedToEmpty = false;
526
527 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
528 if (status) {
529 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
530 }
531 }
532}
533
534void Context::objDestroyOOBDestroy()
535{
536 rsAssert(!mObjDestroy.mNeedToEmpty);
537 pthread_mutex_destroy(&mObjDestroy.mMutex);
538}
539
540void Context::objDestroyAdd(ObjectBase *obj)
541{
542 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
543 if (status) {
544 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
545 return;
546 }
547
548 mObjDestroy.mNeedToEmpty = true;
549 mObjDestroy.mDestroyList.add(obj);
550
551 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
552 if (status) {
553 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
554 }
555}
556
Jason Sams516c3192009-10-06 13:58:47 -0700557uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
558{
559 //LOGE("getMessageToClient %i %i", bufferLen, wait);
560 if (!wait) {
561 if (mIO.mToClient.isEmpty()) {
562 // No message to get and not going to wait for one.
563 receiveLen = 0;
564 return 0;
565 }
566 }
567
568 //LOGE("getMessageToClient 2 con=%p", this);
569 uint32_t bytesData = 0;
570 uint32_t commandID = 0;
571 const void *d = mIO.mToClient.get(&commandID, &bytesData);
572 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
573
574 *receiveLen = bytesData;
575 if (bufferLen >= bytesData) {
576 memcpy(data, d, bytesData);
577 mIO.mToClient.next();
578 return commandID;
579 }
580 return 0;
581}
582
583bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
584{
585 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
586 if (cmdID == 0) {
587 LOGE("Attempting to send invalid command 0 to client.");
588 return false;
589 }
590 if (!waitForSpace) {
591 if (mIO.mToClient.getFreeSpace() < len) {
592 // Not enough room, and not waiting.
593 return false;
594 }
595 }
596 //LOGE("sendMessageToClient 2");
597 void *p = mIO.mToClient.reserve(len);
598 memcpy(p, data, len);
599 mIO.mToClient.commit(cmdID, len);
600 //LOGE("sendMessageToClient 3");
601 return true;
602}
603
604void Context::initToClient()
605{
606 while(!mRunning) {
607 usleep(100);
608 }
609}
610
611void Context::deinitToClient()
612{
613 mIO.mToClient.shutdown();
614}
Jason Sams730ee652009-08-18 17:07:09 -0700615
Jason Samsd5680f92009-06-10 18:39:40 -0700616
Jason Samsd19f10d2009-05-22 14:03:28 -0700617///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700618//
Jason Samsd19f10d2009-05-22 14:03:28 -0700619
620namespace android {
621namespace renderscript {
622
623
624void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
625{
626 Script *s = static_cast<Script *>(vs);
627 rsc->setRootScript(s);
628}
629
630void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
631{
632 Sampler *s = static_cast<Sampler *>(vs);
633
634 if (slot > RS_MAX_SAMPLER_SLOT) {
635 LOGE("Invalid sampler slot");
636 return;
637 }
638
639 s->bindToContext(&rsc->mStateSampler, slot);
640}
641
642void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
643{
644 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
645 rsc->setFragmentStore(pfs);
646}
647
648void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
649{
650 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
651 rsc->setFragment(pf);
652}
653
Jason Samsebfb4362009-09-23 13:57:02 -0700654void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
655{
656 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
657 rsc->setRaster(pr);
658}
659
Jason Samsd19f10d2009-05-22 14:03:28 -0700660void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
661{
662 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
663 rsc->setVertex(pv);
664}
665
Jason Samsd5680f92009-06-10 18:39:40 -0700666void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700667{
668 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsd5680f92009-06-10 18:39:40 -0700669 rsc->assignName(ob, name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700670}
Jason Samsd19f10d2009-05-22 14:03:28 -0700671
Jason Sams7ce033d2009-08-18 14:14:24 -0700672void rsi_ObjDestroy(Context *rsc, void *obj)
673{
674 ObjectBase *ob = static_cast<ObjectBase *>(obj);
675 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700676 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700677}
678
Joe Onoratod7b37742009-08-09 22:57:44 -0700679void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
680{
681 rsc->addInt32Define(name, value);
682}
683
684void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
685{
686 rsc->addFloatDefine(name, value);
687}
Jason Samsd19f10d2009-05-22 14:03:28 -0700688
Jason Sams65e7aa52009-09-24 17:38:20 -0700689void rsi_ContextPause(Context *rsc)
690{
691 rsc->pause();
692}
693
694void rsi_ContextResume(Context *rsc)
695{
696 rsc->resume();
697}
698
Jason Samsd19f10d2009-05-22 14:03:28 -0700699}
700}
701
702
Jason Samsb13ada52009-08-25 11:34:49 -0700703RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700704{
705 Device * dev = static_cast<Device *>(vdev);
Jason Samsb13ada52009-08-25 11:34:49 -0700706 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -0700707 return rsc;
708}
709
710void rsContextDestroy(RsContext vrsc)
711{
712 Context * rsc = static_cast<Context *>(vrsc);
713 delete rsc;
714}
715
Jason Sams730ee652009-08-18 17:07:09 -0700716void rsObjDestroyOOB(RsContext vrsc, void *obj)
717{
718 Context * rsc = static_cast<Context *>(vrsc);
719 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
720}
721
Jason Sams516c3192009-10-06 13:58:47 -0700722uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
723{
724 Context * rsc = static_cast<Context *>(vrsc);
725 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
726}
727
728void rsContextInitToClient(RsContext vrsc)
729{
730 Context * rsc = static_cast<Context *>(vrsc);
731 rsc->initToClient();
732}
733
734void rsContextDeinitToClient(RsContext vrsc)
735{
736 Context * rsc = static_cast<Context *>(vrsc);
737 rsc->deinitToClient();
738}
739