blob: c835dda7601183fce975f59d226b2beb3c660753 [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;
Jason Samse18844a2009-11-12 16:09:45 -0800446 mStateVertex.updateSize(this, w, h);
Jason Sams613cad12009-11-12 15:10:25 -0800447
448 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
449 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
450 }
451
452 if (first) {
453 mGL.mVersion = glGetString(GL_VERSION);
454 mGL.mVendor = glGetString(GL_VENDOR);
455 mGL.mRenderer = glGetString(GL_RENDERER);
456 mGL.mExtensions = glGetString(GL_EXTENSIONS);
457
458 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
459 LOGV("GL Version %s", mGL.mVersion);
460 LOGV("GL Vendor %s", mGL.mVendor);
461 LOGV("GL Renderer %s", mGL.mRenderer);
462 //LOGV("GL Extensions %s", mGL.mExtensions);
463
464 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
465 LOGE("Error, OpenGL ES Lite not supported");
466 } else {
467 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
468 }
469 }
470
Jason Sams458f2dc2009-11-03 13:58:36 -0800471 }
472}
473
Jason Sams86f1b232009-09-24 17:38:20 -0700474void Context::pause()
475{
476 mPaused = true;
477}
478
479void Context::resume()
480{
481 mPaused = false;
482}
483
Jason Sams326e0dd2009-05-22 14:03:28 -0700484void Context::setRootScript(Script *s)
485{
486 mRootScript.set(s);
487}
488
489void Context::setFragmentStore(ProgramFragmentStore *pfs)
490{
Jason Sams8ce125b2009-06-17 16:52:59 -0700491 if (pfs == NULL) {
492 mFragmentStore.set(mStateFragmentStore.mDefault);
493 } else {
494 mFragmentStore.set(pfs);
495 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700496}
497
498void Context::setFragment(ProgramFragment *pf)
499{
Jason Sams8ce125b2009-06-17 16:52:59 -0700500 if (pf == NULL) {
501 mFragment.set(mStateFragment.mDefault);
502 } else {
503 mFragment.set(pf);
504 }
Jason Samscfb1d112009-08-05 13:57:03 -0700505}
506
Jason Sams5fd09d82009-09-23 13:57:02 -0700507void Context::setRaster(ProgramRaster *pr)
508{
509 if (pr == NULL) {
510 mRaster.set(mStateRaster.mDefault);
511 } else {
512 mRaster.set(pr);
513 }
514}
515
Jason Samscfb1d112009-08-05 13:57:03 -0700516void Context::allocationCheck(const Allocation *a)
517{
518 mVertex->checkUpdatedAllocation(a);
519 mFragment->checkUpdatedAllocation(a);
520 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700521}
522
523void Context::setVertex(ProgramVertex *pv)
524{
Jason Sams8ce125b2009-06-17 16:52:59 -0700525 if (pv == NULL) {
526 mVertex.set(mStateVertex.mDefault);
527 } else {
528 mVertex.set(pv);
529 }
Jason Samsc2f94902009-10-15 18:45:45 -0700530 mVertex->forceDirty();
Jason Sams326e0dd2009-05-22 14:03:28 -0700531}
532
Jason Samsa4a54e42009-06-10 18:39:40 -0700533void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700534{
535 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700536 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700537 mNames.add(obj);
538}
539
540void Context::removeName(ObjectBase *obj)
541{
542 for(size_t ct=0; ct < mNames.size(); ct++) {
543 if (obj == mNames[ct]) {
544 mNames.removeAt(ct);
545 return;
546 }
547 }
548}
549
550ObjectBase * Context::lookupName(const char *name) const
551{
552 for(size_t ct=0; ct < mNames.size(); ct++) {
553 if (!strcmp(name, mNames[ct]->getName())) {
554 return mNames[ct];
555 }
556 }
557 return NULL;
558}
559
Jason Samsa4a54e42009-06-10 18:39:40 -0700560void Context::appendNameDefines(String8 *str) const
561{
562 char buf[256];
563 for (size_t ct=0; ct < mNames.size(); ct++) {
564 str->append("#define NAMED_");
565 str->append(mNames[ct]->getName());
566 str->append(" ");
567 sprintf(buf, "%i\n", (int)mNames[ct]);
568 str->append(buf);
569 }
570}
571
Joe Onorato57b79ce2009-08-09 22:57:44 -0700572void Context::appendVarDefines(String8 *str) const
573{
574 char buf[256];
575 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
576 str->append("#define ");
577 str->append(mInt32Defines.keyAt(ct));
578 str->append(" ");
579 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
580 str->append(buf);
581
582 }
583 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
584 str->append("#define ");
585 str->append(mFloatDefines.keyAt(ct));
586 str->append(" ");
587 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
588 str->append(buf);
589 }
590}
591
Jason Sams50869382009-08-18 17:07:09 -0700592bool Context::objDestroyOOBInit()
593{
594 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
595 if (status) {
596 LOGE("Context::ObjDestroyOOBInit mutex init failure");
597 return false;
598 }
599 return true;
600}
601
602void Context::objDestroyOOBRun()
603{
604 if (mObjDestroy.mNeedToEmpty) {
605 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
606 if (status) {
607 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
608 return;
609 }
610
611 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700612 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700613 }
614 mObjDestroy.mDestroyList.clear();
615 mObjDestroy.mNeedToEmpty = false;
616
617 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
618 if (status) {
619 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
620 }
621 }
622}
623
624void Context::objDestroyOOBDestroy()
625{
626 rsAssert(!mObjDestroy.mNeedToEmpty);
627 pthread_mutex_destroy(&mObjDestroy.mMutex);
628}
629
630void Context::objDestroyAdd(ObjectBase *obj)
631{
632 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
633 if (status) {
634 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
635 return;
636 }
637
638 mObjDestroy.mNeedToEmpty = true;
639 mObjDestroy.mDestroyList.add(obj);
640
641 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
642 if (status) {
643 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
644 }
645}
646
Jason Sams8c401ef2009-10-06 13:58:47 -0700647uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
648{
649 //LOGE("getMessageToClient %i %i", bufferLen, wait);
650 if (!wait) {
651 if (mIO.mToClient.isEmpty()) {
652 // No message to get and not going to wait for one.
653 receiveLen = 0;
654 return 0;
655 }
656 }
657
658 //LOGE("getMessageToClient 2 con=%p", this);
659 uint32_t bytesData = 0;
660 uint32_t commandID = 0;
661 const void *d = mIO.mToClient.get(&commandID, &bytesData);
662 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
663
664 *receiveLen = bytesData;
665 if (bufferLen >= bytesData) {
666 memcpy(data, d, bytesData);
667 mIO.mToClient.next();
668 return commandID;
669 }
670 return 0;
671}
672
673bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
674{
675 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
676 if (cmdID == 0) {
677 LOGE("Attempting to send invalid command 0 to client.");
678 return false;
679 }
680 if (!waitForSpace) {
681 if (mIO.mToClient.getFreeSpace() < len) {
682 // Not enough room, and not waiting.
683 return false;
684 }
685 }
686 //LOGE("sendMessageToClient 2");
687 void *p = mIO.mToClient.reserve(len);
688 memcpy(p, data, len);
689 mIO.mToClient.commit(cmdID, len);
690 //LOGE("sendMessageToClient 3");
691 return true;
692}
693
694void Context::initToClient()
695{
696 while(!mRunning) {
697 usleep(100);
698 }
699}
700
701void Context::deinitToClient()
702{
703 mIO.mToClient.shutdown();
704}
Jason Sams50869382009-08-18 17:07:09 -0700705
Jason Samsa4a54e42009-06-10 18:39:40 -0700706
Jason Sams326e0dd2009-05-22 14:03:28 -0700707///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700708//
Jason Sams326e0dd2009-05-22 14:03:28 -0700709
710namespace android {
711namespace renderscript {
712
713
714void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
715{
716 Script *s = static_cast<Script *>(vs);
717 rsc->setRootScript(s);
718}
719
720void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
721{
722 Sampler *s = static_cast<Sampler *>(vs);
723
724 if (slot > RS_MAX_SAMPLER_SLOT) {
725 LOGE("Invalid sampler slot");
726 return;
727 }
728
729 s->bindToContext(&rsc->mStateSampler, slot);
730}
731
732void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
733{
734 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
735 rsc->setFragmentStore(pfs);
736}
737
738void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
739{
740 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
741 rsc->setFragment(pf);
742}
743
Jason Sams5fd09d82009-09-23 13:57:02 -0700744void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
745{
746 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
747 rsc->setRaster(pr);
748}
749
Jason Sams326e0dd2009-05-22 14:03:28 -0700750void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
751{
752 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
753 rsc->setVertex(pv);
754}
755
Jason Samsa4a54e42009-06-10 18:39:40 -0700756void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700757{
758 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700759 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700760}
Jason Sams326e0dd2009-05-22 14:03:28 -0700761
Jason Sams707aaf32009-08-18 14:14:24 -0700762void rsi_ObjDestroy(Context *rsc, void *obj)
763{
764 ObjectBase *ob = static_cast<ObjectBase *>(obj);
765 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700766 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700767}
768
Joe Onorato57b79ce2009-08-09 22:57:44 -0700769void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
770{
771 rsc->addInt32Define(name, value);
772}
773
774void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
775{
776 rsc->addFloatDefine(name, value);
777}
Jason Sams326e0dd2009-05-22 14:03:28 -0700778
Jason Sams86f1b232009-09-24 17:38:20 -0700779void rsi_ContextPause(Context *rsc)
780{
781 rsc->pause();
782}
783
784void rsi_ContextResume(Context *rsc)
785{
786 rsc->resume();
787}
788
Jason Sams613cad12009-11-12 15:10:25 -0800789void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800790{
Jason Sams613cad12009-11-12 15:10:25 -0800791 rsc->setSurface(w, h, (Surface *)sur);
792}
793
794void rsi_ContextSetPriority(Context *rsc, uint32_t p)
795{
Jason Sams458f2dc2009-11-03 13:58:36 -0800796}
797
Jason Sams326e0dd2009-05-22 14:03:28 -0700798}
799}
800
801
Jason Sams613cad12009-11-12 15:10:25 -0800802RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700803{
804 Device * dev = static_cast<Device *>(vdev);
Jason Sams613cad12009-11-12 15:10:25 -0800805 Context *rsc = new Context(dev, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700806 return rsc;
807}
808
809void rsContextDestroy(RsContext vrsc)
810{
811 Context * rsc = static_cast<Context *>(vrsc);
812 delete rsc;
813}
814
Jason Sams50869382009-08-18 17:07:09 -0700815void rsObjDestroyOOB(RsContext vrsc, void *obj)
816{
817 Context * rsc = static_cast<Context *>(vrsc);
818 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
819}
820
Jason Sams8c401ef2009-10-06 13:58:47 -0700821uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
822{
823 Context * rsc = static_cast<Context *>(vrsc);
824 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
825}
826
827void rsContextInitToClient(RsContext vrsc)
828{
829 Context * rsc = static_cast<Context *>(vrsc);
830 rsc->initToClient();
831}
832
833void rsContextDeinitToClient(RsContext vrsc)
834{
835 Context * rsc = static_cast<Context *>(vrsc);
836 rsc->deinitToClient();
837}
838