blob: f815badbbcc625649913107a5ef736ad94c83c18 [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 Sams2dca84d2009-12-09 11:05:45 -0800127uint32_t 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 Sams2dca84d2009-12-09 11:05:45 -0800134 uint32_t 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 Sams2dca84d2009-12-09 11:05:45 -0800144uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700145{
Jason Sams2dca84d2009-12-09 11:05:45 -0800146 timerSet(RS_TIMER_CLEAR_SWAP);
Jason Sams10308932009-06-09 12:15:30 -0700147 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700148
Jason Sams8c9534b2009-09-22 12:26:53 -0700149 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
150 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700151 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700152 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
153
Jason Sams928b7342009-06-08 18:50:13 -0700154 glClearColor(mRootScript->mEnviroment.mClearColor[0],
155 mRootScript->mEnviroment.mClearColor[1],
156 mRootScript->mEnviroment.mClearColor[2],
157 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700158 if (mUseDepth) {
159 glDepthMask(GL_TRUE);
160 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162 } else {
163 glClear(GL_COLOR_BUFFER_BIT);
164 }
Jason Sams306fb232009-08-25 17:09:59 -0700165
Jason Sams2dca84d2009-12-09 11:05:45 -0800166 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700167 mStateFragmentStore.mLast.clear();
Jason Sams2dca84d2009-12-09 11:05:45 -0800168 uint32_t ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700169
170 GLenum err = glGetError();
171 if (err != GL_NO_ERROR) {
172 LOGE("Pending GL Error, 0x%x", err);
173 }
174
Jason Samscfb1d112009-08-05 13:57:03 -0700175 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700176}
177
Jason Sams24371d92009-08-19 12:17:14 -0700178uint64_t Context::getTime() const
179{
180 struct timespec t;
181 clock_gettime(CLOCK_MONOTONIC, &t);
182 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
183}
184
185void Context::timerReset()
186{
187 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
188 mTimers[ct] = 0;
189 }
190}
191
192void Context::timerInit()
193{
194 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700195 mTimeFrame = mTimeLast;
196 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700197 mTimerActive = RS_TIMER_INTERNAL;
198 timerReset();
199}
200
Jason Sams1d54f102009-09-03 15:43:13 -0700201void Context::timerFrame()
202{
203 mTimeLastFrame = mTimeFrame;
204 mTimeFrame = getTime();
205}
206
Jason Sams24371d92009-08-19 12:17:14 -0700207void Context::timerSet(Timers tm)
208{
209 uint64_t last = mTimeLast;
210 mTimeLast = getTime();
211 mTimers[mTimerActive] += mTimeLast - last;
212 mTimerActive = tm;
213}
214
215void Context::timerPrint()
216{
217 double total = 0;
218 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
219 total += mTimers[ct];
220 }
Jason Sams1d54f102009-09-03 15:43:13 -0700221 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800222 mTimeMSLastFrame = frame / 1000000;
223 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
224 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700225
Jason Sams2dca84d2009-12-09 11:05:45 -0800226
227 if (props.mLogTimes) {
228 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
229 mTimeMSLastFrame,
230 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
231 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
232 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
233 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
234 }
Jason Sams24371d92009-08-19 12:17:14 -0700235}
236
Jason Sams326e0dd2009-05-22 14:03:28 -0700237void Context::setupCheck()
238{
Jason Samsc460e552009-11-25 13:22:07 -0800239 if (checkVersion2_0()) {
240 mShaderCache.lookup(mVertex.get(), mFragment.get());
241
242 mFragmentStore->setupGL2(this, &mStateFragmentStore);
243 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
244 mRaster->setupGL2(this, &mStateRaster);
245 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
246
247 } else {
248 mFragmentStore->setupGL(this, &mStateFragmentStore);
249 mFragment->setupGL(this, &mStateFragment);
250 mRaster->setupGL(this, &mStateRaster);
251 mVertex->setupGL(this, &mStateVertex);
252 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700253}
254
Jason Sams1fddd902009-09-25 15:25:00 -0700255static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700256{
257 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700258 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 return 0 != strcmp(buf, "0");
260}
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
262void * Context::threadProc(void *vrsc)
263{
264 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800265 rsc->mNativeThreadId = gettid();
266
267 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800268 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700269
Jason Sams1fddd902009-09-25 15:25:00 -0700270 rsc->props.mLogTimes = getProp("debug.rs.profile");
271 rsc->props.mLogScripts = getProp("debug.rs.script");
272 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700273
Jason Samse5769102009-06-19 16:03:18 -0700274 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
275 if (!tlsStruct) {
276 LOGE("Error allocating tls storage");
277 return NULL;
278 }
279 tlsStruct->mContext = rsc;
280 tlsStruct->mScript = NULL;
281 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
282 if (status) {
283 LOGE("pthread_setspecific %i", status);
284 }
285
Jason Sams5fd09d82009-09-23 13:57:02 -0700286 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
287 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700288 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700289 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700290 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700291 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700292 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700293 rsc->setFragmentStore(NULL);
Jason Samsc460e552009-11-25 13:22:07 -0800294 rsc->mStateVertexArray.init(rsc);
Jason Sams8ce125b2009-06-17 16:52:59 -0700295
Jason Sams326e0dd2009-05-22 14:03:28 -0700296 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700297 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700298 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700299 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700300 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800301 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700302
Jason Sams2dca84d2009-12-09 11:05:45 -0800303 uint32_t targetTime = 0;
Jason Sams732f1c02009-06-18 16:58:42 -0700304 if (mDraw) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800305 targetTime = rsc->runRootScript();
306 mDraw = targetTime && !rsc->mPaused;
307 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700308 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800309 rsc->timerFrame();
310 rsc->timerSet(RS_TIMER_INTERNAL);
311 rsc->timerPrint();
312 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700313 }
Jason Sams24371d92009-08-19 12:17:14 -0700314 if (rsc->mObjDestroy.mNeedToEmpty) {
315 rsc->objDestroyOOBRun();
316 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800317 if (rsc->mThreadPriority > 0 && targetTime) {
318 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
319 if (t > 0) {
320 usleep(t);
321 }
322 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700323 }
324
Jason Sams8c0ee652009-08-25 14:49:07 -0700325 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700326 rsc->mRaster.clear();
327 rsc->mFragment.clear();
328 rsc->mVertex.clear();
329 rsc->mFragmentStore.clear();
330 rsc->mRootScript.clear();
331 rsc->mStateRaster.deinit(rsc);
332 rsc->mStateVertex.deinit(rsc);
333 rsc->mStateFragment.deinit(rsc);
334 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700335 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700336
Jason Samse402ed32009-11-03 11:25:42 -0800337 rsc->mObjDestroy.mNeedToEmpty = true;
338 rsc->objDestroyOOBRun();
339
Jason Sams326e0dd2009-05-22 14:03:28 -0700340 glClearColor(0,0,0,0);
341 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700342 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700343
344 pthread_mutex_lock(&gInitMutex);
345 rsc->deinitEGL();
346 pthread_mutex_unlock(&gInitMutex);
347
Jason Sams8c0ee652009-08-25 14:49:07 -0700348 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 return NULL;
350}
351
Jason Sams15832442009-11-15 12:14:26 -0800352void Context::setPriority(int32_t p)
353{
354 // Note: If we put this in the proper "background" policy
355 // the wallpapers can become completly unresponsive at times.
356 // This is probably not what we want for something the user is actively
357 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800358 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800359#if 0
360 SchedPolicy pol = SP_FOREGROUND;
361 if (p > 0) {
362 pol = SP_BACKGROUND;
363 }
364 if (!set_sched_policy(mNativeThreadId, pol)) {
365 // success; reset the priority as well
366 }
367#else
368 setpriority(PRIO_PROCESS, mNativeThreadId, p);
369#endif
370}
371
Jason Sams613cad12009-11-12 15:10:25 -0800372Context::Context(Device *dev, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700373{
Jason Samsfb03a222009-10-15 16:47:31 -0700374 pthread_mutex_lock(&gInitMutex);
375
Jason Sams326e0dd2009-05-22 14:03:28 -0700376 dev->addContext(this);
377 mDev = dev;
378 mRunning = false;
379 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700380 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700381 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700382 mObjHead = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800383 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams326e0dd2009-05-22 14:03:28 -0700384
Jason Samsa658e902009-06-04 14:35:01 -0700385 int status;
386 pthread_attr_t threadAttr;
387
Jason Samsfb03a222009-10-15 16:47:31 -0700388 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700389 status = pthread_key_create(&gThreadTLSKey, NULL);
390 if (status) {
391 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700392 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700393 return;
394 }
Jason Samse5769102009-06-19 16:03:18 -0700395 }
Jason Samsfb03a222009-10-15 16:47:31 -0700396 gThreadTLSKeyCount++;
397 pthread_mutex_unlock(&gInitMutex);
398
399 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700400
Jason Samsa658e902009-06-04 14:35:01 -0700401 status = pthread_attr_init(&threadAttr);
402 if (status) {
403 LOGE("Failed to init thread attribute.");
404 return;
405 }
406
Jason Sams613cad12009-11-12 15:10:25 -0800407 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700408
Jason Sams50869382009-08-18 17:07:09 -0700409 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700410 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700411 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700412
Jason Sams992a0b72009-06-23 12:22:47 -0700413 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700414 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700415 if (status) {
416 LOGE("Failed to start rs context thread.");
417 }
418
Jason Sams326e0dd2009-05-22 14:03:28 -0700419 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700420 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700421 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700422
Jason Samsa658e902009-06-04 14:35:01 -0700423 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700424}
425
426Context::~Context()
427{
Jason Sams8c0ee652009-08-25 14:49:07 -0700428 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700429 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700430 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700431 void *res;
432
Jason Sams8c0ee652009-08-25 14:49:07 -0700433 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700434 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800435 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700436 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700437
Jason Samsfb03a222009-10-15 16:47:31 -0700438 // Global structure cleanup.
439 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700440 if (mDev) {
441 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700442 --gThreadTLSKeyCount;
443 if (!gThreadTLSKeyCount) {
444 pthread_key_delete(gThreadTLSKey);
445 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800446 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700447 }
Jason Samsfb03a222009-10-15 16:47:31 -0700448 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700449
450 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700451}
452
Jason Sams613cad12009-11-12 15:10:25 -0800453void Context::setSurface(uint32_t w, uint32_t h, Surface *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800454{
Jason Sams613cad12009-11-12 15:10:25 -0800455 LOGV("setSurface %i %i %p", w, h, sur);
456
Jason Sams458f2dc2009-11-03 13:58:36 -0800457 EGLBoolean ret;
458 if (mEGL.mSurface != NULL) {
459 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
460 checkEglError("eglMakeCurrent", ret);
461
462 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
463 checkEglError("eglDestroySurface", ret);
464
465 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800466 mEGL.mWidth = 0;
467 mEGL.mHeight = 0;
468 mWidth = 0;
469 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800470 }
471
472 mWndSurface = sur;
473 if (mWndSurface != NULL) {
Jason Sams613cad12009-11-12 15:10:25 -0800474 bool first = false;
475 if (!mEGL.mContext) {
476 first = true;
477 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800478 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800479 pthread_mutex_unlock(&gInitMutex);
480 }
481
Jason Sams458f2dc2009-11-03 13:58:36 -0800482 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
483 checkEglError("eglCreateWindowSurface");
484 if (mEGL.mSurface == EGL_NO_SURFACE) {
485 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
486 }
487
488 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
489 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800490
491 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
492 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
493 mWidth = w;
494 mHeight = h;
Jason Samse18844a2009-11-12 16:09:45 -0800495 mStateVertex.updateSize(this, w, h);
Jason Sams613cad12009-11-12 15:10:25 -0800496
497 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
498 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
499 }
500
501 if (first) {
502 mGL.mVersion = glGetString(GL_VERSION);
503 mGL.mVendor = glGetString(GL_VENDOR);
504 mGL.mRenderer = glGetString(GL_RENDERER);
505 mGL.mExtensions = glGetString(GL_EXTENSIONS);
506
507 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
508 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800509 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800510 LOGV("GL Renderer %s", mGL.mRenderer);
511 //LOGV("GL Extensions %s", mGL.mExtensions);
512
Jason Samsc460e552009-11-25 13:22:07 -0800513 const char *verptr = NULL;
514 if (strlen((const char *)mGL.mVersion) > 9) {
515 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
516 verptr = (const char *)mGL.mVersion + 12;
517 }
518 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
519 verptr = (const char *)mGL.mVersion + 9;
520 }
521 }
522
523 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800524 LOGE("Error, OpenGL ES Lite not supported");
525 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800526 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800527 }
528 }
529
Jason Sams458f2dc2009-11-03 13:58:36 -0800530 }
531}
532
Jason Sams86f1b232009-09-24 17:38:20 -0700533void Context::pause()
534{
535 mPaused = true;
536}
537
538void Context::resume()
539{
540 mPaused = false;
541}
542
Jason Sams326e0dd2009-05-22 14:03:28 -0700543void Context::setRootScript(Script *s)
544{
545 mRootScript.set(s);
546}
547
548void Context::setFragmentStore(ProgramFragmentStore *pfs)
549{
Jason Sams8ce125b2009-06-17 16:52:59 -0700550 if (pfs == NULL) {
551 mFragmentStore.set(mStateFragmentStore.mDefault);
552 } else {
553 mFragmentStore.set(pfs);
554 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700555}
556
557void Context::setFragment(ProgramFragment *pf)
558{
Jason Sams8ce125b2009-06-17 16:52:59 -0700559 if (pf == NULL) {
560 mFragment.set(mStateFragment.mDefault);
561 } else {
562 mFragment.set(pf);
563 }
Jason Samscfb1d112009-08-05 13:57:03 -0700564}
565
Jason Sams5fd09d82009-09-23 13:57:02 -0700566void Context::setRaster(ProgramRaster *pr)
567{
568 if (pr == NULL) {
569 mRaster.set(mStateRaster.mDefault);
570 } else {
571 mRaster.set(pr);
572 }
573}
574
Jason Sams326e0dd2009-05-22 14:03:28 -0700575void Context::setVertex(ProgramVertex *pv)
576{
Jason Sams8ce125b2009-06-17 16:52:59 -0700577 if (pv == NULL) {
578 mVertex.set(mStateVertex.mDefault);
579 } else {
580 mVertex.set(pv);
581 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700582}
583
Jason Samsa4a54e42009-06-10 18:39:40 -0700584void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700585{
586 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700587 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700588 mNames.add(obj);
589}
590
591void Context::removeName(ObjectBase *obj)
592{
593 for(size_t ct=0; ct < mNames.size(); ct++) {
594 if (obj == mNames[ct]) {
595 mNames.removeAt(ct);
596 return;
597 }
598 }
599}
600
601ObjectBase * Context::lookupName(const char *name) const
602{
603 for(size_t ct=0; ct < mNames.size(); ct++) {
604 if (!strcmp(name, mNames[ct]->getName())) {
605 return mNames[ct];
606 }
607 }
608 return NULL;
609}
610
Jason Samsa4a54e42009-06-10 18:39:40 -0700611void Context::appendNameDefines(String8 *str) const
612{
613 char buf[256];
614 for (size_t ct=0; ct < mNames.size(); ct++) {
615 str->append("#define NAMED_");
616 str->append(mNames[ct]->getName());
617 str->append(" ");
618 sprintf(buf, "%i\n", (int)mNames[ct]);
619 str->append(buf);
620 }
621}
622
Joe Onorato57b79ce2009-08-09 22:57:44 -0700623void Context::appendVarDefines(String8 *str) const
624{
625 char buf[256];
626 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
627 str->append("#define ");
628 str->append(mInt32Defines.keyAt(ct));
629 str->append(" ");
630 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
631 str->append(buf);
632
633 }
634 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
635 str->append("#define ");
636 str->append(mFloatDefines.keyAt(ct));
637 str->append(" ");
638 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
639 str->append(buf);
640 }
641}
642
Jason Sams50869382009-08-18 17:07:09 -0700643bool Context::objDestroyOOBInit()
644{
645 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
646 if (status) {
647 LOGE("Context::ObjDestroyOOBInit mutex init failure");
648 return false;
649 }
650 return true;
651}
652
653void Context::objDestroyOOBRun()
654{
655 if (mObjDestroy.mNeedToEmpty) {
656 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
657 if (status) {
658 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
659 return;
660 }
661
662 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700663 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700664 }
665 mObjDestroy.mDestroyList.clear();
666 mObjDestroy.mNeedToEmpty = false;
667
668 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
669 if (status) {
670 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
671 }
672 }
673}
674
675void Context::objDestroyOOBDestroy()
676{
677 rsAssert(!mObjDestroy.mNeedToEmpty);
678 pthread_mutex_destroy(&mObjDestroy.mMutex);
679}
680
681void Context::objDestroyAdd(ObjectBase *obj)
682{
683 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
684 if (status) {
685 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
686 return;
687 }
688
689 mObjDestroy.mNeedToEmpty = true;
690 mObjDestroy.mDestroyList.add(obj);
691
692 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
693 if (status) {
694 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
695 }
696}
697
Jason Sams8c401ef2009-10-06 13:58:47 -0700698uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
699{
700 //LOGE("getMessageToClient %i %i", bufferLen, wait);
701 if (!wait) {
702 if (mIO.mToClient.isEmpty()) {
703 // No message to get and not going to wait for one.
704 receiveLen = 0;
705 return 0;
706 }
707 }
708
709 //LOGE("getMessageToClient 2 con=%p", this);
710 uint32_t bytesData = 0;
711 uint32_t commandID = 0;
712 const void *d = mIO.mToClient.get(&commandID, &bytesData);
713 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
714
715 *receiveLen = bytesData;
716 if (bufferLen >= bytesData) {
717 memcpy(data, d, bytesData);
718 mIO.mToClient.next();
719 return commandID;
720 }
721 return 0;
722}
723
724bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
725{
726 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
727 if (cmdID == 0) {
728 LOGE("Attempting to send invalid command 0 to client.");
729 return false;
730 }
731 if (!waitForSpace) {
732 if (mIO.mToClient.getFreeSpace() < len) {
733 // Not enough room, and not waiting.
734 return false;
735 }
736 }
737 //LOGE("sendMessageToClient 2");
738 void *p = mIO.mToClient.reserve(len);
739 memcpy(p, data, len);
740 mIO.mToClient.commit(cmdID, len);
741 //LOGE("sendMessageToClient 3");
742 return true;
743}
744
745void Context::initToClient()
746{
747 while(!mRunning) {
748 usleep(100);
749 }
750}
751
752void Context::deinitToClient()
753{
754 mIO.mToClient.shutdown();
755}
Jason Sams50869382009-08-18 17:07:09 -0700756
Jason Sams13e26342009-11-24 12:26:35 -0800757void Context::dumpDebug() const
758{
759 LOGE("RS Context debug %p", this);
760 LOGE("RS Context debug");
761
762 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
763 LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext,
764 mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay);
765 LOGE(" GL vendor: %s", mGL.mVendor);
766 LOGE(" GL renderer: %s", mGL.mRenderer);
767 LOGE(" GL Version: %s", mGL.mVersion);
768 LOGE(" GL Extensions: %s", mGL.mExtensions);
769 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
770 LOGE(" RS width %i, height %i", mWidth, mHeight);
771 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
772 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
773
774}
Jason Samsa4a54e42009-06-10 18:39:40 -0700775
Jason Sams326e0dd2009-05-22 14:03:28 -0700776///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700777//
Jason Sams326e0dd2009-05-22 14:03:28 -0700778
779namespace android {
780namespace renderscript {
781
782
783void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
784{
785 Script *s = static_cast<Script *>(vs);
786 rsc->setRootScript(s);
787}
788
789void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
790{
791 Sampler *s = static_cast<Sampler *>(vs);
792
793 if (slot > RS_MAX_SAMPLER_SLOT) {
794 LOGE("Invalid sampler slot");
795 return;
796 }
797
798 s->bindToContext(&rsc->mStateSampler, slot);
799}
800
801void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
802{
803 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
804 rsc->setFragmentStore(pfs);
805}
806
807void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
808{
809 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
810 rsc->setFragment(pf);
811}
812
Jason Sams5fd09d82009-09-23 13:57:02 -0700813void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
814{
815 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
816 rsc->setRaster(pr);
817}
818
Jason Sams326e0dd2009-05-22 14:03:28 -0700819void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
820{
821 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
822 rsc->setVertex(pv);
823}
824
Jason Samsa4a54e42009-06-10 18:39:40 -0700825void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700826{
827 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700828 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700829}
Jason Sams326e0dd2009-05-22 14:03:28 -0700830
Jason Sams707aaf32009-08-18 14:14:24 -0700831void rsi_ObjDestroy(Context *rsc, void *obj)
832{
833 ObjectBase *ob = static_cast<ObjectBase *>(obj);
834 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700835 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700836}
837
Joe Onorato57b79ce2009-08-09 22:57:44 -0700838void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
839{
840 rsc->addInt32Define(name, value);
841}
842
843void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
844{
845 rsc->addFloatDefine(name, value);
846}
Jason Sams326e0dd2009-05-22 14:03:28 -0700847
Jason Sams86f1b232009-09-24 17:38:20 -0700848void rsi_ContextPause(Context *rsc)
849{
850 rsc->pause();
851}
852
853void rsi_ContextResume(Context *rsc)
854{
855 rsc->resume();
856}
857
Jason Sams613cad12009-11-12 15:10:25 -0800858void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800859{
Jason Sams613cad12009-11-12 15:10:25 -0800860 rsc->setSurface(w, h, (Surface *)sur);
861}
862
Jason Sams15832442009-11-15 12:14:26 -0800863void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800864{
Jason Sams15832442009-11-15 12:14:26 -0800865 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800866}
867
Jason Samsc21cf402009-11-17 17:26:46 -0800868void rsi_ContextDump(Context *rsc, int32_t bits)
869{
870 ObjectBase::dumpAll(rsc);
871}
872
Jason Sams326e0dd2009-05-22 14:03:28 -0700873}
874}
875
876
Jason Sams613cad12009-11-12 15:10:25 -0800877RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700878{
879 Device * dev = static_cast<Device *>(vdev);
Jason Sams613cad12009-11-12 15:10:25 -0800880 Context *rsc = new Context(dev, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700881 return rsc;
882}
883
884void rsContextDestroy(RsContext vrsc)
885{
886 Context * rsc = static_cast<Context *>(vrsc);
887 delete rsc;
888}
889
Jason Sams50869382009-08-18 17:07:09 -0700890void rsObjDestroyOOB(RsContext vrsc, void *obj)
891{
892 Context * rsc = static_cast<Context *>(vrsc);
893 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
894}
895
Jason Sams8c401ef2009-10-06 13:58:47 -0700896uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
897{
898 Context * rsc = static_cast<Context *>(vrsc);
899 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
900}
901
902void rsContextInitToClient(RsContext vrsc)
903{
904 Context * rsc = static_cast<Context *>(vrsc);
905 rsc->initToClient();
906}
907
908void rsContextDeinitToClient(RsContext vrsc)
909{
910 Context * rsc = static_cast<Context *>(vrsc);
911 rsc->deinitToClient();
912}
913