blob: ab3809de655ce1ac06592d2797ddebbbecab09ea [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsafcb25c2009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070022
Joe Onorato76371ff2009-09-23 16:37:36 -070023#include <cutils/properties.h>
24
Jason Sams1aa5a4e2009-06-22 17:15:15 -070025#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028using namespace android;
29using namespace android::renderscript;
30
Jason Samse5769102009-06-19 16:03:18 -070031pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070032uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070033uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070034pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070035
Jason Sams33b6e3b2009-10-27 14:44:31 -070036static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
37 if (returnVal != EGL_TRUE) {
38 fprintf(stderr, "%s() returned %d\n", op, returnVal);
39 }
40
41 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
42 = eglGetError()) {
43 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
44 error);
45 }
46}
47
Jason Sams326e0dd2009-05-22 14:03:28 -070048void Context::initEGL()
49{
Jason Samsafcb25c2009-08-25 11:34:49 -070050 mEGL.mNumConfigs = -1;
51 EGLint configAttribs[128];
52 EGLint *configAttribsPtr = configAttribs;
Jason Sams326e0dd2009-05-22 14:03:28 -070053
Jason Samsafcb25c2009-08-25 11:34:49 -070054 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070055
Jason Samsafcb25c2009-08-25 11:34:49 -070056 configAttribsPtr[0] = EGL_SURFACE_TYPE;
57 configAttribsPtr[1] = EGL_WINDOW_BIT;
58 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070059
Jason Samsafcb25c2009-08-25 11:34:49 -070060 if (mUseDepth) {
61 configAttribsPtr[0] = EGL_DEPTH_SIZE;
62 configAttribsPtr[1] = 16;
63 configAttribsPtr += 2;
64 }
Jason Sams9397e302009-08-27 20:23:34 -070065
Jason Sams5fd09d82009-09-23 13:57:02 -070066 if (mDev->mForceSW) {
67 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
68 configAttribsPtr[1] = EGL_SLOW_CONFIG;
69 configAttribsPtr += 2;
70 }
71
Jason Samsafcb25c2009-08-25 11:34:49 -070072 configAttribsPtr[0] = EGL_NONE;
73 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070074
Jason Sams6d751ef2009-10-08 12:55:06 -070075 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070076 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070077 checkEglError("eglGetDisplay");
78
Jason Samsafcb25c2009-08-25 11:34:49 -070079 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070080 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070081
82 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
83 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070084 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070085 }
86 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
87
Jason Samsafcb25c2009-08-25 11:34:49 -070088
Jason Sams33b6e3b2009-10-27 14:44:31 -070089 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
90 checkEglError("eglCreateContext");
91 if (mEGL.mContext == EGL_NO_CONTEXT) {
92 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
93 }
94 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -070095}
96
Jason Sams33b6e3b2009-10-27 14:44:31 -070097void Context::deinitEGL()
98{
Jason Sams613cad12009-11-12 15:10:25 -080099 LOGV("deinitEGL");
100 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700101 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
102 checkEglError("eglDestroyContext");
103
104 gGLContextCount--;
105 if (!gGLContextCount) {
106 eglTerminate(mEGL.mDisplay);
107 }
108}
109
110
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700111bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700112{
113 ObjectBaseRef<ProgramFragment> frag(mFragment);
114 ObjectBaseRef<ProgramVertex> vtx(mVertex);
115 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700116 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700117
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700118 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700119
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700120 mFragment.set(frag);
121 mVertex.set(vtx);
122 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700123 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700124 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700125}
126
127
Jason Samsa44cb292009-06-04 17:58:03 -0700128bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700129{
Jason Sams1fddd902009-09-25 15:25:00 -0700130 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700131 timerSet(RS_TIMER_CLEAR_SWAP);
132 }
Jason Sams10308932009-06-09 12:15:30 -0700133 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700134
Jason Sams8c9534b2009-09-22 12:26:53 -0700135 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
136 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700137 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700138 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
139
Jason Sams928b7342009-06-08 18:50:13 -0700140 glClearColor(mRootScript->mEnviroment.mClearColor[0],
141 mRootScript->mEnviroment.mClearColor[1],
142 mRootScript->mEnviroment.mClearColor[2],
143 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700144 if (mUseDepth) {
145 glDepthMask(GL_TRUE);
146 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
147 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
148 } else {
149 glClear(GL_COLOR_BUFFER_BIT);
150 }
Jason Sams306fb232009-08-25 17:09:59 -0700151
Jason Sams1fddd902009-09-25 15:25:00 -0700152 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700153 timerSet(RS_TIMER_SCRIPT);
154 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700155 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700156 bool ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700157
158 GLenum err = glGetError();
159 if (err != GL_NO_ERROR) {
160 LOGE("Pending GL Error, 0x%x", err);
161 }
162
Jason Samscfb1d112009-08-05 13:57:03 -0700163 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700164}
165
Jason Sams24371d92009-08-19 12:17:14 -0700166uint64_t Context::getTime() const
167{
168 struct timespec t;
169 clock_gettime(CLOCK_MONOTONIC, &t);
170 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
171}
172
173void Context::timerReset()
174{
175 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
176 mTimers[ct] = 0;
177 }
178}
179
180void Context::timerInit()
181{
182 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700183 mTimeFrame = mTimeLast;
184 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700185 mTimerActive = RS_TIMER_INTERNAL;
186 timerReset();
187}
188
Jason Sams1d54f102009-09-03 15:43:13 -0700189void Context::timerFrame()
190{
191 mTimeLastFrame = mTimeFrame;
192 mTimeFrame = getTime();
193}
194
Jason Sams24371d92009-08-19 12:17:14 -0700195void Context::timerSet(Timers tm)
196{
197 uint64_t last = mTimeLast;
198 mTimeLast = getTime();
199 mTimers[mTimerActive] += mTimeLast - last;
200 mTimerActive = tm;
201}
202
203void Context::timerPrint()
204{
205 double total = 0;
206 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
207 total += mTimers[ct];
208 }
Jason Sams1d54f102009-09-03 15:43:13 -0700209 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700210
Jason Sams1d54f102009-09-03 15:43:13 -0700211 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
212 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700213 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700214 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
215 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
216 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700217}
218
Jason Sams326e0dd2009-05-22 14:03:28 -0700219void Context::setupCheck()
220{
Jason Sams5fd09d82009-09-23 13:57:02 -0700221 mFragmentStore->setupGL(this, &mStateFragmentStore);
222 mFragment->setupGL(this, &mStateFragment);
223 mRaster->setupGL(this, &mStateRaster);
224 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700225}
226
Jason Sams1fddd902009-09-25 15:25:00 -0700227static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700228{
229 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700230 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700231 return 0 != strcmp(buf, "0");
232}
Jason Sams326e0dd2009-05-22 14:03:28 -0700233
234void * Context::threadProc(void *vrsc)
235{
236 Context *rsc = static_cast<Context *>(vrsc);
237
Jason Sams1fddd902009-09-25 15:25:00 -0700238 rsc->props.mLogTimes = getProp("debug.rs.profile");
239 rsc->props.mLogScripts = getProp("debug.rs.script");
240 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700241
Jason Sams613cad12009-11-12 15:10:25 -0800242 //pthread_mutex_lock(&gInitMutex);
243 //rsc->initEGL();
244 //pthread_mutex_unlock(&gInitMutex);
Jason Sams8ce125b2009-06-17 16:52:59 -0700245
Jason Samse5769102009-06-19 16:03:18 -0700246 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
247 if (!tlsStruct) {
248 LOGE("Error allocating tls storage");
249 return NULL;
250 }
251 tlsStruct->mContext = rsc;
252 tlsStruct->mScript = NULL;
253 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
254 if (status) {
255 LOGE("pthread_setspecific %i", status);
256 }
257
Jason Sams5fd09d82009-09-23 13:57:02 -0700258 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
259 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700260 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700261 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700262 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700263 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700264 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700265 rsc->setFragmentStore(NULL);
266
Jason Sams326e0dd2009-05-22 14:03:28 -0700267 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700268 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700269 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700270 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700271 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800272 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700273
Jason Sams732f1c02009-06-18 16:58:42 -0700274 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700275 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700276 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700277 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
278 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700279 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700280 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700281 rsc->timerFrame();
282 rsc->timerSet(RS_TIMER_INTERNAL);
283 rsc->timerPrint();
284 rsc->timerReset();
285 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700286 }
Jason Sams24371d92009-08-19 12:17:14 -0700287 if (rsc->mObjDestroy.mNeedToEmpty) {
288 rsc->objDestroyOOBRun();
289 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700290 }
291
Jason Sams8c0ee652009-08-25 14:49:07 -0700292 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700293 rsc->mRaster.clear();
294 rsc->mFragment.clear();
295 rsc->mVertex.clear();
296 rsc->mFragmentStore.clear();
297 rsc->mRootScript.clear();
298 rsc->mStateRaster.deinit(rsc);
299 rsc->mStateVertex.deinit(rsc);
300 rsc->mStateFragment.deinit(rsc);
301 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700302 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700303
Jason Samse402ed32009-11-03 11:25:42 -0800304 rsc->mObjDestroy.mNeedToEmpty = true;
305 rsc->objDestroyOOBRun();
306
Jason Sams326e0dd2009-05-22 14:03:28 -0700307 glClearColor(0,0,0,0);
308 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700309 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700310
311 pthread_mutex_lock(&gInitMutex);
312 rsc->deinitEGL();
313 pthread_mutex_unlock(&gInitMutex);
314
Jason Sams8c0ee652009-08-25 14:49:07 -0700315 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700316 return NULL;
317}
318
Jason Sams613cad12009-11-12 15:10:25 -0800319Context::Context(Device *dev, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700320{
Jason Samsfb03a222009-10-15 16:47:31 -0700321 pthread_mutex_lock(&gInitMutex);
322
Jason Sams326e0dd2009-05-22 14:03:28 -0700323 dev->addContext(this);
324 mDev = dev;
325 mRunning = false;
326 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700327 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700328 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700329 mObjHead = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800330 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams326e0dd2009-05-22 14:03:28 -0700331
Jason Samsa658e902009-06-04 14:35:01 -0700332 int status;
333 pthread_attr_t threadAttr;
334
Jason Samsfb03a222009-10-15 16:47:31 -0700335 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700336 status = pthread_key_create(&gThreadTLSKey, NULL);
337 if (status) {
338 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700339 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700340 return;
341 }
Jason Samse5769102009-06-19 16:03:18 -0700342 }
Jason Samsfb03a222009-10-15 16:47:31 -0700343 gThreadTLSKeyCount++;
344 pthread_mutex_unlock(&gInitMutex);
345
346 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700347
Jason Samsa658e902009-06-04 14:35:01 -0700348 status = pthread_attr_init(&threadAttr);
349 if (status) {
350 LOGE("Failed to init thread attribute.");
351 return;
352 }
353
354 sched_param sparam;
355 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
356 pthread_attr_setschedparam(&threadAttr, &sparam);
357
Jason Sams613cad12009-11-12 15:10:25 -0800358 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700359
Jason Sams50869382009-08-18 17:07:09 -0700360 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700361 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700362 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700363
Jason Sams992a0b72009-06-23 12:22:47 -0700364 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700365 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700366 if (status) {
367 LOGE("Failed to start rs context thread.");
368 }
369
Jason Sams326e0dd2009-05-22 14:03:28 -0700370 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700371 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700372 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700373
Jason Samsa658e902009-06-04 14:35:01 -0700374 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700375}
376
377Context::~Context()
378{
Jason Sams8c0ee652009-08-25 14:49:07 -0700379 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700380 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700381 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700382 void *res;
383
Jason Sams8c0ee652009-08-25 14:49:07 -0700384 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700385 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800386 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700387 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700388
Jason Samsfb03a222009-10-15 16:47:31 -0700389 // Global structure cleanup.
390 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700391 if (mDev) {
392 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700393 --gThreadTLSKeyCount;
394 if (!gThreadTLSKeyCount) {
395 pthread_key_delete(gThreadTLSKey);
396 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800397 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700398 }
Jason Samsfb03a222009-10-15 16:47:31 -0700399 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700400
401 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700402}
403
Jason Sams613cad12009-11-12 15:10:25 -0800404void Context::setSurface(uint32_t w, uint32_t h, Surface *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800405{
Jason Sams613cad12009-11-12 15:10:25 -0800406 LOGV("setSurface %i %i %p", w, h, sur);
407
Jason Sams458f2dc2009-11-03 13:58:36 -0800408 EGLBoolean ret;
409 if (mEGL.mSurface != NULL) {
410 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
411 checkEglError("eglMakeCurrent", ret);
412
413 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
414 checkEglError("eglDestroySurface", ret);
415
416 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800417 mEGL.mWidth = 0;
418 mEGL.mHeight = 0;
419 mWidth = 0;
420 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800421 }
422
423 mWndSurface = sur;
424 if (mWndSurface != NULL) {
Jason Sams613cad12009-11-12 15:10:25 -0800425 bool first = false;
426 if (!mEGL.mContext) {
427 first = true;
428 pthread_mutex_lock(&gInitMutex);
429 initEGL();
430 pthread_mutex_unlock(&gInitMutex);
431 }
432
Jason Sams458f2dc2009-11-03 13:58:36 -0800433 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
434 checkEglError("eglCreateWindowSurface");
435 if (mEGL.mSurface == EGL_NO_SURFACE) {
436 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
437 }
438
439 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
440 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800441
442 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
443 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
444 mWidth = w;
445 mHeight = h;
446
447 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
448 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
449 }
450
451 if (first) {
452 mGL.mVersion = glGetString(GL_VERSION);
453 mGL.mVendor = glGetString(GL_VENDOR);
454 mGL.mRenderer = glGetString(GL_RENDERER);
455 mGL.mExtensions = glGetString(GL_EXTENSIONS);
456
457 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
458 LOGV("GL Version %s", mGL.mVersion);
459 LOGV("GL Vendor %s", mGL.mVendor);
460 LOGV("GL Renderer %s", mGL.mRenderer);
461 //LOGV("GL Extensions %s", mGL.mExtensions);
462
463 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
464 LOGE("Error, OpenGL ES Lite not supported");
465 } else {
466 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
467 }
468 }
469
Jason Sams458f2dc2009-11-03 13:58:36 -0800470 }
471}
472
Jason Sams86f1b232009-09-24 17:38:20 -0700473void Context::pause()
474{
475 mPaused = true;
476}
477
478void Context::resume()
479{
480 mPaused = false;
481}
482
Jason Sams326e0dd2009-05-22 14:03:28 -0700483void Context::setRootScript(Script *s)
484{
485 mRootScript.set(s);
486}
487
488void Context::setFragmentStore(ProgramFragmentStore *pfs)
489{
Jason Sams8ce125b2009-06-17 16:52:59 -0700490 if (pfs == NULL) {
491 mFragmentStore.set(mStateFragmentStore.mDefault);
492 } else {
493 mFragmentStore.set(pfs);
494 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700495}
496
497void Context::setFragment(ProgramFragment *pf)
498{
Jason Sams8ce125b2009-06-17 16:52:59 -0700499 if (pf == NULL) {
500 mFragment.set(mStateFragment.mDefault);
501 } else {
502 mFragment.set(pf);
503 }
Jason Samscfb1d112009-08-05 13:57:03 -0700504}
505
Jason Sams5fd09d82009-09-23 13:57:02 -0700506void Context::setRaster(ProgramRaster *pr)
507{
508 if (pr == NULL) {
509 mRaster.set(mStateRaster.mDefault);
510 } else {
511 mRaster.set(pr);
512 }
513}
514
Jason Samscfb1d112009-08-05 13:57:03 -0700515void Context::allocationCheck(const Allocation *a)
516{
517 mVertex->checkUpdatedAllocation(a);
518 mFragment->checkUpdatedAllocation(a);
519 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700520}
521
522void Context::setVertex(ProgramVertex *pv)
523{
Jason Sams8ce125b2009-06-17 16:52:59 -0700524 if (pv == NULL) {
525 mVertex.set(mStateVertex.mDefault);
526 } else {
527 mVertex.set(pv);
528 }
Jason Samsc2f94902009-10-15 18:45:45 -0700529 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700530}
531
Jason Samsa4a54e42009-06-10 18:39:40 -0700532void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700533{
534 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700535 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700536 mNames.add(obj);
537}
538
539void Context::removeName(ObjectBase *obj)
540{
541 for(size_t ct=0; ct < mNames.size(); ct++) {
542 if (obj == mNames[ct]) {
543 mNames.removeAt(ct);
544 return;
545 }
546 }
547}
548
549ObjectBase * Context::lookupName(const char *name) const
550{
551 for(size_t ct=0; ct < mNames.size(); ct++) {
552 if (!strcmp(name, mNames[ct]->getName())) {
553 return mNames[ct];
554 }
555 }
556 return NULL;
557}
558
Jason Samsa4a54e42009-06-10 18:39:40 -0700559void Context::appendNameDefines(String8 *str) const
560{
561 char buf[256];
562 for (size_t ct=0; ct < mNames.size(); ct++) {
563 str->append("#define NAMED_");
564 str->append(mNames[ct]->getName());
565 str->append(" ");
566 sprintf(buf, "%i\n", (int)mNames[ct]);
567 str->append(buf);
568 }
569}
570
Joe Onorato57b79ce2009-08-09 22:57:44 -0700571void Context::appendVarDefines(String8 *str) const
572{
573 char buf[256];
574 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
575 str->append("#define ");
576 str->append(mInt32Defines.keyAt(ct));
577 str->append(" ");
578 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
579 str->append(buf);
580
581 }
582 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
583 str->append("#define ");
584 str->append(mFloatDefines.keyAt(ct));
585 str->append(" ");
586 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
587 str->append(buf);
588 }
589}
590
Jason Sams50869382009-08-18 17:07:09 -0700591bool Context::objDestroyOOBInit()
592{
593 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
594 if (status) {
595 LOGE("Context::ObjDestroyOOBInit mutex init failure");
596 return false;
597 }
598 return true;
599}
600
601void Context::objDestroyOOBRun()
602{
603 if (mObjDestroy.mNeedToEmpty) {
604 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
605 if (status) {
606 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
607 return;
608 }
609
610 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700611 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700612 }
613 mObjDestroy.mDestroyList.clear();
614 mObjDestroy.mNeedToEmpty = false;
615
616 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
617 if (status) {
618 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
619 }
620 }
621}
622
623void Context::objDestroyOOBDestroy()
624{
625 rsAssert(!mObjDestroy.mNeedToEmpty);
626 pthread_mutex_destroy(&mObjDestroy.mMutex);
627}
628
629void Context::objDestroyAdd(ObjectBase *obj)
630{
631 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
632 if (status) {
633 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
634 return;
635 }
636
637 mObjDestroy.mNeedToEmpty = true;
638 mObjDestroy.mDestroyList.add(obj);
639
640 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
641 if (status) {
642 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
643 }
644}
645
Jason Sams8c401ef2009-10-06 13:58:47 -0700646uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
647{
648 //LOGE("getMessageToClient %i %i", bufferLen, wait);
649 if (!wait) {
650 if (mIO.mToClient.isEmpty()) {
651 // No message to get and not going to wait for one.
652 receiveLen = 0;
653 return 0;
654 }
655 }
656
657 //LOGE("getMessageToClient 2 con=%p", this);
658 uint32_t bytesData = 0;
659 uint32_t commandID = 0;
660 const void *d = mIO.mToClient.get(&commandID, &bytesData);
661 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
662
663 *receiveLen = bytesData;
664 if (bufferLen >= bytesData) {
665 memcpy(data, d, bytesData);
666 mIO.mToClient.next();
667 return commandID;
668 }
669 return 0;
670}
671
672bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
673{
674 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
675 if (cmdID == 0) {
676 LOGE("Attempting to send invalid command 0 to client.");
677 return false;
678 }
679 if (!waitForSpace) {
680 if (mIO.mToClient.getFreeSpace() < len) {
681 // Not enough room, and not waiting.
682 return false;
683 }
684 }
685 //LOGE("sendMessageToClient 2");
686 void *p = mIO.mToClient.reserve(len);
687 memcpy(p, data, len);
688 mIO.mToClient.commit(cmdID, len);
689 //LOGE("sendMessageToClient 3");
690 return true;
691}
692
693void Context::initToClient()
694{
695 while(!mRunning) {
696 usleep(100);
697 }
698}
699
700void Context::deinitToClient()
701{
702 mIO.mToClient.shutdown();
703}
Jason Sams50869382009-08-18 17:07:09 -0700704
Jason Samsa4a54e42009-06-10 18:39:40 -0700705
Jason Sams326e0dd2009-05-22 14:03:28 -0700706///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700707//
Jason Sams326e0dd2009-05-22 14:03:28 -0700708
709namespace android {
710namespace renderscript {
711
712
713void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
714{
715 Script *s = static_cast<Script *>(vs);
716 rsc->setRootScript(s);
717}
718
719void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
720{
721 Sampler *s = static_cast<Sampler *>(vs);
722
723 if (slot > RS_MAX_SAMPLER_SLOT) {
724 LOGE("Invalid sampler slot");
725 return;
726 }
727
728 s->bindToContext(&rsc->mStateSampler, slot);
729}
730
731void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
732{
733 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
734 rsc->setFragmentStore(pfs);
735}
736
737void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
738{
739 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
740 rsc->setFragment(pf);
741}
742
Jason Sams5fd09d82009-09-23 13:57:02 -0700743void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
744{
745 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
746 rsc->setRaster(pr);
747}
748
Jason Sams326e0dd2009-05-22 14:03:28 -0700749void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
750{
751 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
752 rsc->setVertex(pv);
753}
754
Jason Samsa4a54e42009-06-10 18:39:40 -0700755void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700756{
757 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700758 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700759}
Jason Sams326e0dd2009-05-22 14:03:28 -0700760
Jason Sams707aaf32009-08-18 14:14:24 -0700761void rsi_ObjDestroy(Context *rsc, void *obj)
762{
763 ObjectBase *ob = static_cast<ObjectBase *>(obj);
764 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700765 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700766}
767
Joe Onorato57b79ce2009-08-09 22:57:44 -0700768void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
769{
770 rsc->addInt32Define(name, value);
771}
772
773void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
774{
775 rsc->addFloatDefine(name, value);
776}
Jason Sams326e0dd2009-05-22 14:03:28 -0700777
Jason Sams86f1b232009-09-24 17:38:20 -0700778void rsi_ContextPause(Context *rsc)
779{
780 rsc->pause();
781}
782
783void rsi_ContextResume(Context *rsc)
784{
785 rsc->resume();
786}
787
Jason Sams613cad12009-11-12 15:10:25 -0800788void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800789{
Jason Sams613cad12009-11-12 15:10:25 -0800790 rsc->setSurface(w, h, (Surface *)sur);
791}
792
793void rsi_ContextSetPriority(Context *rsc, uint32_t p)
794{
Jason Sams458f2dc2009-11-03 13:58:36 -0800795}
796
Jason Sams326e0dd2009-05-22 14:03:28 -0700797}
798}
799
800
Jason Sams613cad12009-11-12 15:10:25 -0800801RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700802{
803 Device * dev = static_cast<Device *>(vdev);
Jason Sams613cad12009-11-12 15:10:25 -0800804 Context *rsc = new Context(dev, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700805 return rsc;
806}
807
808void rsContextDestroy(RsContext vrsc)
809{
810 Context * rsc = static_cast<Context *>(vrsc);
811 delete rsc;
812}
813
Jason Sams50869382009-08-18 17:07:09 -0700814void rsObjDestroyOOB(RsContext vrsc, void *obj)
815{
816 Context * rsc = static_cast<Context *>(vrsc);
817 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
818}
819
Jason Sams8c401ef2009-10-06 13:58:47 -0700820uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
821{
822 Context * rsc = static_cast<Context *>(vrsc);
823 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
824}
825
826void rsContextInitToClient(RsContext vrsc)
827{
828 Context * rsc = static_cast<Context *>(vrsc);
829 rsc->initToClient();
830}
831
832void rsContextDeinitToClient(RsContext vrsc)
833{
834 Context * rsc = static_cast<Context *>(vrsc);
835 rsc->deinitToClient();
836}
837