blob: 427a6ccacd2c33c1a62d97f9b6518c5d31ce4f8e [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
Jason Sams15832442009-11-15 12:14:26 -080023#include <sys/types.h>
24#include <sys/resource.h>
25
Joe Onorato76371ff2009-09-23 16:37:36 -070026#include <cutils/properties.h>
27
Jason Sams1aa5a4e2009-06-22 17:15:15 -070028#include <GLES/gl.h>
29#include <GLES/glext.h>
30
Jason Sams15832442009-11-15 12:14:26 -080031#include <cutils/sched_policy.h>
32
Jason Sams326e0dd2009-05-22 14:03:28 -070033using namespace android;
34using namespace android::renderscript;
35
Jason Samse5769102009-06-19 16:03:18 -070036pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070037uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070038uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070039pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070040
Jason Sams33b6e3b2009-10-27 14:44:31 -070041static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
42 if (returnVal != EGL_TRUE) {
43 fprintf(stderr, "%s() returned %d\n", op, returnVal);
44 }
45
46 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
47 = eglGetError()) {
48 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
49 error);
50 }
51}
52
Jason Samsc460e552009-11-25 13:22:07 -080053void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070054{
Jason Samsafcb25c2009-08-25 11:34:49 -070055 mEGL.mNumConfigs = -1;
56 EGLint configAttribs[128];
57 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080058 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070059
Jason Samsafcb25c2009-08-25 11:34:49 -070060 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Jason Samsafcb25c2009-08-25 11:34:49 -070062 configAttribsPtr[0] = EGL_SURFACE_TYPE;
63 configAttribsPtr[1] = EGL_WINDOW_BIT;
64 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsc460e552009-11-25 13:22:07 -080066 if (useGL2) {
67 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
68 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
69 configAttribsPtr += 2;
70 }
71
Jason Samsafcb25c2009-08-25 11:34:49 -070072 if (mUseDepth) {
73 configAttribsPtr[0] = EGL_DEPTH_SIZE;
74 configAttribsPtr[1] = 16;
75 configAttribsPtr += 2;
76 }
Jason Sams9397e302009-08-27 20:23:34 -070077
Jason Sams5fd09d82009-09-23 13:57:02 -070078 if (mDev->mForceSW) {
79 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
80 configAttribsPtr[1] = EGL_SLOW_CONFIG;
81 configAttribsPtr += 2;
82 }
83
Jason Samsafcb25c2009-08-25 11:34:49 -070084 configAttribsPtr[0] = EGL_NONE;
85 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070086
Jason Sams6d751ef2009-10-08 12:55:06 -070087 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070088 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070089 checkEglError("eglGetDisplay");
90
Jason Samsafcb25c2009-08-25 11:34:49 -070091 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070092 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070093
94 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
95 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070096 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070097 }
98 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
99
Jason Samsafcb25c2009-08-25 11:34:49 -0700100
Jason Samsc460e552009-11-25 13:22:07 -0800101 if (useGL2) {
102 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
103 } else {
104 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
105 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700106 checkEglError("eglCreateContext");
107 if (mEGL.mContext == EGL_NO_CONTEXT) {
108 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
109 }
110 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700111}
112
Jason Sams33b6e3b2009-10-27 14:44:31 -0700113void Context::deinitEGL()
114{
Jason Sams613cad12009-11-12 15:10:25 -0800115 LOGV("deinitEGL");
116 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700117 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
118 checkEglError("eglDestroyContext");
119
120 gGLContextCount--;
121 if (!gGLContextCount) {
122 eglTerminate(mEGL.mDisplay);
123 }
124}
125
126
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700127bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700128{
129 ObjectBaseRef<ProgramFragment> frag(mFragment);
130 ObjectBaseRef<ProgramVertex> vtx(mVertex);
131 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700132 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700133
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700134 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700135
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700136 mFragment.set(frag);
137 mVertex.set(vtx);
138 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700139 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700140 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700141}
142
143
Jason Samsa44cb292009-06-04 17:58:03 -0700144bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700145{
Jason Sams1fddd902009-09-25 15:25:00 -0700146 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700147 timerSet(RS_TIMER_CLEAR_SWAP);
148 }
Jason Sams10308932009-06-09 12:15:30 -0700149 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700150
Jason Sams8c9534b2009-09-22 12:26:53 -0700151 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
152 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700153 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700154 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
155
Jason Sams928b7342009-06-08 18:50:13 -0700156 glClearColor(mRootScript->mEnviroment.mClearColor[0],
157 mRootScript->mEnviroment.mClearColor[1],
158 mRootScript->mEnviroment.mClearColor[2],
159 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700160 if (mUseDepth) {
161 glDepthMask(GL_TRUE);
162 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
163 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
164 } else {
165 glClear(GL_COLOR_BUFFER_BIT);
166 }
Jason Sams306fb232009-08-25 17:09:59 -0700167
Jason Sams1fddd902009-09-25 15:25:00 -0700168 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700169 timerSet(RS_TIMER_SCRIPT);
170 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700171 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700172 bool ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700173
174 GLenum err = glGetError();
175 if (err != GL_NO_ERROR) {
176 LOGE("Pending GL Error, 0x%x", err);
177 }
178
Jason Samscfb1d112009-08-05 13:57:03 -0700179 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700180}
181
Jason Sams24371d92009-08-19 12:17:14 -0700182uint64_t Context::getTime() const
183{
184 struct timespec t;
185 clock_gettime(CLOCK_MONOTONIC, &t);
186 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
187}
188
189void Context::timerReset()
190{
191 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
192 mTimers[ct] = 0;
193 }
194}
195
196void Context::timerInit()
197{
198 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700199 mTimeFrame = mTimeLast;
200 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700201 mTimerActive = RS_TIMER_INTERNAL;
202 timerReset();
203}
204
Jason Sams1d54f102009-09-03 15:43:13 -0700205void Context::timerFrame()
206{
207 mTimeLastFrame = mTimeFrame;
208 mTimeFrame = getTime();
209}
210
Jason Sams24371d92009-08-19 12:17:14 -0700211void Context::timerSet(Timers tm)
212{
213 uint64_t last = mTimeLast;
214 mTimeLast = getTime();
215 mTimers[mTimerActive] += mTimeLast - last;
216 mTimerActive = tm;
217}
218
219void Context::timerPrint()
220{
221 double total = 0;
222 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
223 total += mTimers[ct];
224 }
Jason Sams1d54f102009-09-03 15:43:13 -0700225 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700226
Jason Sams1d54f102009-09-03 15:43:13 -0700227 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
228 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700229 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700230 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
231 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
232 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700233}
234
Jason Sams326e0dd2009-05-22 14:03:28 -0700235void Context::setupCheck()
236{
Jason Samsc460e552009-11-25 13:22:07 -0800237 if (checkVersion2_0()) {
238 mShaderCache.lookup(mVertex.get(), mFragment.get());
239
240 mFragmentStore->setupGL2(this, &mStateFragmentStore);
241 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
242 mRaster->setupGL2(this, &mStateRaster);
243 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
244
245 } else {
246 mFragmentStore->setupGL(this, &mStateFragmentStore);
247 mFragment->setupGL(this, &mStateFragment);
248 mRaster->setupGL(this, &mStateRaster);
249 mVertex->setupGL(this, &mStateVertex);
250 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700251}
252
Jason Sams1fddd902009-09-25 15:25:00 -0700253static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700254{
255 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700256 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700257 return 0 != strcmp(buf, "0");
258}
Jason Sams326e0dd2009-05-22 14:03:28 -0700259
260void * Context::threadProc(void *vrsc)
261{
262 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800263 rsc->mNativeThreadId = gettid();
264
265 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -0700266
Jason Sams1fddd902009-09-25 15:25:00 -0700267 rsc->props.mLogTimes = getProp("debug.rs.profile");
268 rsc->props.mLogScripts = getProp("debug.rs.script");
269 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700270
Jason Samse5769102009-06-19 16:03:18 -0700271 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
272 if (!tlsStruct) {
273 LOGE("Error allocating tls storage");
274 return NULL;
275 }
276 tlsStruct->mContext = rsc;
277 tlsStruct->mScript = NULL;
278 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
279 if (status) {
280 LOGE("pthread_setspecific %i", status);
281 }
282
Jason Sams5fd09d82009-09-23 13:57:02 -0700283 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
284 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700285 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700286 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700287 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700288 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700289 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700290 rsc->setFragmentStore(NULL);
Jason Samsc460e552009-11-25 13:22:07 -0800291 rsc->mStateVertexArray.init(rsc);
Jason Sams8ce125b2009-06-17 16:52:59 -0700292
Jason Sams326e0dd2009-05-22 14:03:28 -0700293 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700294 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700295 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700296 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700297 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800298 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700299
Jason Sams732f1c02009-06-18 16:58:42 -0700300 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700301 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700302 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700303 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
304 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700305 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700306 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700307 rsc->timerFrame();
308 rsc->timerSet(RS_TIMER_INTERNAL);
309 rsc->timerPrint();
310 rsc->timerReset();
311 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700312 }
Jason Sams24371d92009-08-19 12:17:14 -0700313 if (rsc->mObjDestroy.mNeedToEmpty) {
314 rsc->objDestroyOOBRun();
315 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700316 }
317
Jason Sams8c0ee652009-08-25 14:49:07 -0700318 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700319 rsc->mRaster.clear();
320 rsc->mFragment.clear();
321 rsc->mVertex.clear();
322 rsc->mFragmentStore.clear();
323 rsc->mRootScript.clear();
324 rsc->mStateRaster.deinit(rsc);
325 rsc->mStateVertex.deinit(rsc);
326 rsc->mStateFragment.deinit(rsc);
327 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700328 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700329
Jason Samse402ed32009-11-03 11:25:42 -0800330 rsc->mObjDestroy.mNeedToEmpty = true;
331 rsc->objDestroyOOBRun();
332
Jason Sams326e0dd2009-05-22 14:03:28 -0700333 glClearColor(0,0,0,0);
334 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700335 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700336
337 pthread_mutex_lock(&gInitMutex);
338 rsc->deinitEGL();
339 pthread_mutex_unlock(&gInitMutex);
340
Jason Sams8c0ee652009-08-25 14:49:07 -0700341 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700342 return NULL;
343}
344
Jason Sams15832442009-11-15 12:14:26 -0800345void Context::setPriority(int32_t p)
346{
347 // Note: If we put this in the proper "background" policy
348 // the wallpapers can become completly unresponsive at times.
349 // This is probably not what we want for something the user is actively
350 // looking at.
351#if 0
352 SchedPolicy pol = SP_FOREGROUND;
353 if (p > 0) {
354 pol = SP_BACKGROUND;
355 }
356 if (!set_sched_policy(mNativeThreadId, pol)) {
357 // success; reset the priority as well
358 }
359#else
360 setpriority(PRIO_PROCESS, mNativeThreadId, p);
361#endif
362}
363
Jason Sams613cad12009-11-12 15:10:25 -0800364Context::Context(Device *dev, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700365{
Jason Samsfb03a222009-10-15 16:47:31 -0700366 pthread_mutex_lock(&gInitMutex);
367
Jason Sams326e0dd2009-05-22 14:03:28 -0700368 dev->addContext(this);
369 mDev = dev;
370 mRunning = false;
371 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700372 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700373 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700374 mObjHead = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800375 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams326e0dd2009-05-22 14:03:28 -0700376
Jason Samsa658e902009-06-04 14:35:01 -0700377 int status;
378 pthread_attr_t threadAttr;
379
Jason Samsfb03a222009-10-15 16:47:31 -0700380 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700381 status = pthread_key_create(&gThreadTLSKey, NULL);
382 if (status) {
383 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700384 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700385 return;
386 }
Jason Samse5769102009-06-19 16:03:18 -0700387 }
Jason Samsfb03a222009-10-15 16:47:31 -0700388 gThreadTLSKeyCount++;
389 pthread_mutex_unlock(&gInitMutex);
390
391 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700392
Jason Samsa658e902009-06-04 14:35:01 -0700393 status = pthread_attr_init(&threadAttr);
394 if (status) {
395 LOGE("Failed to init thread attribute.");
396 return;
397 }
398
Jason Sams613cad12009-11-12 15:10:25 -0800399 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700400
Jason Sams50869382009-08-18 17:07:09 -0700401 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700402 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700403 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700404
Jason Sams992a0b72009-06-23 12:22:47 -0700405 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700406 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700407 if (status) {
408 LOGE("Failed to start rs context thread.");
409 }
410
Jason Sams326e0dd2009-05-22 14:03:28 -0700411 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700412 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700413 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700414
Jason Samsa658e902009-06-04 14:35:01 -0700415 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700416}
417
418Context::~Context()
419{
Jason Sams8c0ee652009-08-25 14:49:07 -0700420 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700421 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700422 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700423 void *res;
424
Jason Sams8c0ee652009-08-25 14:49:07 -0700425 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700426 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800427 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700428 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700429
Jason Samsfb03a222009-10-15 16:47:31 -0700430 // Global structure cleanup.
431 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700432 if (mDev) {
433 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700434 --gThreadTLSKeyCount;
435 if (!gThreadTLSKeyCount) {
436 pthread_key_delete(gThreadTLSKey);
437 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800438 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700439 }
Jason Samsfb03a222009-10-15 16:47:31 -0700440 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700441
442 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700443}
444
Jason Sams613cad12009-11-12 15:10:25 -0800445void Context::setSurface(uint32_t w, uint32_t h, Surface *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800446{
Jason Sams613cad12009-11-12 15:10:25 -0800447 LOGV("setSurface %i %i %p", w, h, sur);
448
Jason Sams458f2dc2009-11-03 13:58:36 -0800449 EGLBoolean ret;
450 if (mEGL.mSurface != NULL) {
451 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
452 checkEglError("eglMakeCurrent", ret);
453
454 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
455 checkEglError("eglDestroySurface", ret);
456
457 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800458 mEGL.mWidth = 0;
459 mEGL.mHeight = 0;
460 mWidth = 0;
461 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800462 }
463
464 mWndSurface = sur;
465 if (mWndSurface != NULL) {
Jason Sams613cad12009-11-12 15:10:25 -0800466 bool first = false;
467 if (!mEGL.mContext) {
468 first = true;
469 pthread_mutex_lock(&gInitMutex);
Jason Samsc460e552009-11-25 13:22:07 -0800470 initEGL(false);
Jason Sams613cad12009-11-12 15:10:25 -0800471 pthread_mutex_unlock(&gInitMutex);
472 }
473
Jason Sams458f2dc2009-11-03 13:58:36 -0800474 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
475 checkEglError("eglCreateWindowSurface");
476 if (mEGL.mSurface == EGL_NO_SURFACE) {
477 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
478 }
479
480 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
481 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800482
483 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
484 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
485 mWidth = w;
486 mHeight = h;
Jason Samse18844a2009-11-12 16:09:45 -0800487 mStateVertex.updateSize(this, w, h);
Jason Sams613cad12009-11-12 15:10:25 -0800488
489 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
490 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
491 }
492
493 if (first) {
494 mGL.mVersion = glGetString(GL_VERSION);
495 mGL.mVendor = glGetString(GL_VENDOR);
496 mGL.mRenderer = glGetString(GL_RENDERER);
497 mGL.mExtensions = glGetString(GL_EXTENSIONS);
498
499 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
500 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800501 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800502 LOGV("GL Renderer %s", mGL.mRenderer);
503 //LOGV("GL Extensions %s", mGL.mExtensions);
504
Jason Samsc460e552009-11-25 13:22:07 -0800505 const char *verptr = NULL;
506 if (strlen((const char *)mGL.mVersion) > 9) {
507 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
508 verptr = (const char *)mGL.mVersion + 12;
509 }
510 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
511 verptr = (const char *)mGL.mVersion + 9;
512 }
513 }
514
515 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800516 LOGE("Error, OpenGL ES Lite not supported");
517 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800518 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800519 }
520 }
521
Jason Sams458f2dc2009-11-03 13:58:36 -0800522 }
523}
524
Jason Sams86f1b232009-09-24 17:38:20 -0700525void Context::pause()
526{
527 mPaused = true;
528}
529
530void Context::resume()
531{
532 mPaused = false;
533}
534
Jason Sams326e0dd2009-05-22 14:03:28 -0700535void Context::setRootScript(Script *s)
536{
537 mRootScript.set(s);
538}
539
540void Context::setFragmentStore(ProgramFragmentStore *pfs)
541{
Jason Sams8ce125b2009-06-17 16:52:59 -0700542 if (pfs == NULL) {
543 mFragmentStore.set(mStateFragmentStore.mDefault);
544 } else {
545 mFragmentStore.set(pfs);
546 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700547}
548
549void Context::setFragment(ProgramFragment *pf)
550{
Jason Sams8ce125b2009-06-17 16:52:59 -0700551 if (pf == NULL) {
552 mFragment.set(mStateFragment.mDefault);
553 } else {
554 mFragment.set(pf);
555 }
Jason Samscfb1d112009-08-05 13:57:03 -0700556}
557
Jason Sams5fd09d82009-09-23 13:57:02 -0700558void Context::setRaster(ProgramRaster *pr)
559{
560 if (pr == NULL) {
561 mRaster.set(mStateRaster.mDefault);
562 } else {
563 mRaster.set(pr);
564 }
565}
566
Jason Sams326e0dd2009-05-22 14:03:28 -0700567void Context::setVertex(ProgramVertex *pv)
568{
Jason Sams8ce125b2009-06-17 16:52:59 -0700569 if (pv == NULL) {
570 mVertex.set(mStateVertex.mDefault);
571 } else {
572 mVertex.set(pv);
573 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700574}
575
Jason Samsa4a54e42009-06-10 18:39:40 -0700576void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700577{
578 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700579 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700580 mNames.add(obj);
581}
582
583void Context::removeName(ObjectBase *obj)
584{
585 for(size_t ct=0; ct < mNames.size(); ct++) {
586 if (obj == mNames[ct]) {
587 mNames.removeAt(ct);
588 return;
589 }
590 }
591}
592
593ObjectBase * Context::lookupName(const char *name) const
594{
595 for(size_t ct=0; ct < mNames.size(); ct++) {
596 if (!strcmp(name, mNames[ct]->getName())) {
597 return mNames[ct];
598 }
599 }
600 return NULL;
601}
602
Jason Samsa4a54e42009-06-10 18:39:40 -0700603void Context::appendNameDefines(String8 *str) const
604{
605 char buf[256];
606 for (size_t ct=0; ct < mNames.size(); ct++) {
607 str->append("#define NAMED_");
608 str->append(mNames[ct]->getName());
609 str->append(" ");
610 sprintf(buf, "%i\n", (int)mNames[ct]);
611 str->append(buf);
612 }
613}
614
Joe Onorato57b79ce2009-08-09 22:57:44 -0700615void Context::appendVarDefines(String8 *str) const
616{
617 char buf[256];
618 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
619 str->append("#define ");
620 str->append(mInt32Defines.keyAt(ct));
621 str->append(" ");
622 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
623 str->append(buf);
624
625 }
626 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
627 str->append("#define ");
628 str->append(mFloatDefines.keyAt(ct));
629 str->append(" ");
630 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
631 str->append(buf);
632 }
633}
634
Jason Sams50869382009-08-18 17:07:09 -0700635bool Context::objDestroyOOBInit()
636{
637 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
638 if (status) {
639 LOGE("Context::ObjDestroyOOBInit mutex init failure");
640 return false;
641 }
642 return true;
643}
644
645void Context::objDestroyOOBRun()
646{
647 if (mObjDestroy.mNeedToEmpty) {
648 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
649 if (status) {
650 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
651 return;
652 }
653
654 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700655 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700656 }
657 mObjDestroy.mDestroyList.clear();
658 mObjDestroy.mNeedToEmpty = false;
659
660 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
661 if (status) {
662 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
663 }
664 }
665}
666
667void Context::objDestroyOOBDestroy()
668{
669 rsAssert(!mObjDestroy.mNeedToEmpty);
670 pthread_mutex_destroy(&mObjDestroy.mMutex);
671}
672
673void Context::objDestroyAdd(ObjectBase *obj)
674{
675 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
676 if (status) {
677 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
678 return;
679 }
680
681 mObjDestroy.mNeedToEmpty = true;
682 mObjDestroy.mDestroyList.add(obj);
683
684 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
685 if (status) {
686 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
687 }
688}
689
Jason Sams8c401ef2009-10-06 13:58:47 -0700690uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
691{
692 //LOGE("getMessageToClient %i %i", bufferLen, wait);
693 if (!wait) {
694 if (mIO.mToClient.isEmpty()) {
695 // No message to get and not going to wait for one.
696 receiveLen = 0;
697 return 0;
698 }
699 }
700
701 //LOGE("getMessageToClient 2 con=%p", this);
702 uint32_t bytesData = 0;
703 uint32_t commandID = 0;
704 const void *d = mIO.mToClient.get(&commandID, &bytesData);
705 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
706
707 *receiveLen = bytesData;
708 if (bufferLen >= bytesData) {
709 memcpy(data, d, bytesData);
710 mIO.mToClient.next();
711 return commandID;
712 }
713 return 0;
714}
715
716bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
717{
718 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
719 if (cmdID == 0) {
720 LOGE("Attempting to send invalid command 0 to client.");
721 return false;
722 }
723 if (!waitForSpace) {
724 if (mIO.mToClient.getFreeSpace() < len) {
725 // Not enough room, and not waiting.
726 return false;
727 }
728 }
729 //LOGE("sendMessageToClient 2");
730 void *p = mIO.mToClient.reserve(len);
731 memcpy(p, data, len);
732 mIO.mToClient.commit(cmdID, len);
733 //LOGE("sendMessageToClient 3");
734 return true;
735}
736
737void Context::initToClient()
738{
739 while(!mRunning) {
740 usleep(100);
741 }
742}
743
744void Context::deinitToClient()
745{
746 mIO.mToClient.shutdown();
747}
Jason Sams50869382009-08-18 17:07:09 -0700748
Jason Sams13e26342009-11-24 12:26:35 -0800749void Context::dumpDebug() const
750{
751 LOGE("RS Context debug %p", this);
752 LOGE("RS Context debug");
753
754 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
755 LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext,
756 mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay);
757 LOGE(" GL vendor: %s", mGL.mVendor);
758 LOGE(" GL renderer: %s", mGL.mRenderer);
759 LOGE(" GL Version: %s", mGL.mVersion);
760 LOGE(" GL Extensions: %s", mGL.mExtensions);
761 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
762 LOGE(" RS width %i, height %i", mWidth, mHeight);
763 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
764 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
765
766}
Jason Samsa4a54e42009-06-10 18:39:40 -0700767
Jason Sams326e0dd2009-05-22 14:03:28 -0700768///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700769//
Jason Sams326e0dd2009-05-22 14:03:28 -0700770
771namespace android {
772namespace renderscript {
773
774
775void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
776{
777 Script *s = static_cast<Script *>(vs);
778 rsc->setRootScript(s);
779}
780
781void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
782{
783 Sampler *s = static_cast<Sampler *>(vs);
784
785 if (slot > RS_MAX_SAMPLER_SLOT) {
786 LOGE("Invalid sampler slot");
787 return;
788 }
789
790 s->bindToContext(&rsc->mStateSampler, slot);
791}
792
793void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
794{
795 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
796 rsc->setFragmentStore(pfs);
797}
798
799void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
800{
801 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
802 rsc->setFragment(pf);
803}
804
Jason Sams5fd09d82009-09-23 13:57:02 -0700805void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
806{
807 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
808 rsc->setRaster(pr);
809}
810
Jason Sams326e0dd2009-05-22 14:03:28 -0700811void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
812{
813 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
814 rsc->setVertex(pv);
815}
816
Jason Samsa4a54e42009-06-10 18:39:40 -0700817void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700818{
819 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700820 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700821}
Jason Sams326e0dd2009-05-22 14:03:28 -0700822
Jason Sams707aaf32009-08-18 14:14:24 -0700823void rsi_ObjDestroy(Context *rsc, void *obj)
824{
825 ObjectBase *ob = static_cast<ObjectBase *>(obj);
826 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700827 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700828}
829
Joe Onorato57b79ce2009-08-09 22:57:44 -0700830void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
831{
832 rsc->addInt32Define(name, value);
833}
834
835void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
836{
837 rsc->addFloatDefine(name, value);
838}
Jason Sams326e0dd2009-05-22 14:03:28 -0700839
Jason Sams86f1b232009-09-24 17:38:20 -0700840void rsi_ContextPause(Context *rsc)
841{
842 rsc->pause();
843}
844
845void rsi_ContextResume(Context *rsc)
846{
847 rsc->resume();
848}
849
Jason Sams613cad12009-11-12 15:10:25 -0800850void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800851{
Jason Sams613cad12009-11-12 15:10:25 -0800852 rsc->setSurface(w, h, (Surface *)sur);
853}
854
Jason Sams15832442009-11-15 12:14:26 -0800855void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800856{
Jason Sams15832442009-11-15 12:14:26 -0800857 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800858}
859
Jason Samsc21cf402009-11-17 17:26:46 -0800860void rsi_ContextDump(Context *rsc, int32_t bits)
861{
862 ObjectBase::dumpAll(rsc);
863}
864
Jason Sams326e0dd2009-05-22 14:03:28 -0700865}
866}
867
868
Jason Sams613cad12009-11-12 15:10:25 -0800869RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700870{
871 Device * dev = static_cast<Device *>(vdev);
Jason Sams613cad12009-11-12 15:10:25 -0800872 Context *rsc = new Context(dev, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700873 return rsc;
874}
875
876void rsContextDestroy(RsContext vrsc)
877{
878 Context * rsc = static_cast<Context *>(vrsc);
879 delete rsc;
880}
881
Jason Sams50869382009-08-18 17:07:09 -0700882void rsObjDestroyOOB(RsContext vrsc, void *obj)
883{
884 Context * rsc = static_cast<Context *>(vrsc);
885 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
886}
887
Jason Sams8c401ef2009-10-06 13:58:47 -0700888uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
889{
890 Context * rsc = static_cast<Context *>(vrsc);
891 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
892}
893
894void rsContextInitToClient(RsContext vrsc)
895{
896 Context * rsc = static_cast<Context *>(vrsc);
897 rsc->initToClient();
898}
899
900void rsContextDeinitToClient(RsContext vrsc)
901{
902 Context * rsc = static_cast<Context *>(vrsc);
903 rsc->deinitToClient();
904}
905