blob: 08ed7252e8ee12bc5f94116a6dc9c3d8d1813523 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian3142f4f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsb13ada52009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070022
Jason Sams7d787b42009-11-15 12:14:26 -080023#include <sys/types.h>
24#include <sys/resource.h>
25
Joe Onorato9ac2c662009-09-23 16:37:36 -070026#include <cutils/properties.h>
27
Jason Sams4b962e52009-06-22 17:15:15 -070028#include <GLES/gl.h>
29#include <GLES/glext.h>
30
Jason Sams7d787b42009-11-15 12:14:26 -080031#include <cutils/sched_policy.h>
32
Jason Samsd19f10d2009-05-22 14:03:28 -070033using namespace android;
34using namespace android::renderscript;
35
Jason Sams462d11b2009-06-19 16:03:18 -070036pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams41c19db92009-10-15 16:47:31 -070037uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams71362202009-10-27 14:44:31 -070038uint32_t Context::gGLContextCount = 0;
Jason Sams41c19db92009-10-15 16:47:31 -070039pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Samsd19f10d2009-05-22 14:03:28 -070040
Jason Sams71362202009-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 Samsd19f10d2009-05-22 14:03:28 -070053void Context::initEGL()
54{
Jason Samsb13ada52009-08-25 11:34:49 -070055 mEGL.mNumConfigs = -1;
56 EGLint configAttribs[128];
57 EGLint *configAttribsPtr = configAttribs;
Jason Samsd19f10d2009-05-22 14:03:28 -070058
Jason Samsb13ada52009-08-25 11:34:49 -070059 memset(configAttribs, 0, sizeof(configAttribs));
Jason Samsd19f10d2009-05-22 14:03:28 -070060
Jason Samsb13ada52009-08-25 11:34:49 -070061 configAttribsPtr[0] = EGL_SURFACE_TYPE;
62 configAttribsPtr[1] = EGL_WINDOW_BIT;
63 configAttribsPtr += 2;
Jason Samsd19f10d2009-05-22 14:03:28 -070064
Jason Samsb13ada52009-08-25 11:34:49 -070065 if (mUseDepth) {
66 configAttribsPtr[0] = EGL_DEPTH_SIZE;
67 configAttribsPtr[1] = 16;
68 configAttribsPtr += 2;
69 }
Jason Sams07ae4062009-08-27 20:23:34 -070070
Jason Samsebfb4362009-09-23 13:57:02 -070071 if (mDev->mForceSW) {
72 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
73 configAttribsPtr[1] = EGL_SLOW_CONFIG;
74 configAttribsPtr += 2;
75 }
76
Jason Samsb13ada52009-08-25 11:34:49 -070077 configAttribsPtr[0] = EGL_NONE;
78 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Samsd19f10d2009-05-22 14:03:28 -070079
Jason Sams6a17e162009-10-08 12:55:06 -070080 LOGV("initEGL start");
Jason Samsb13ada52009-08-25 11:34:49 -070081 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams71362202009-10-27 14:44:31 -070082 checkEglError("eglGetDisplay");
83
Jason Samsb13ada52009-08-25 11:34:49 -070084 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams71362202009-10-27 14:44:31 -070085 checkEglError("eglInitialize");
Jason Samsb13ada52009-08-25 11:34:49 -070086
87 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
88 if (err) {
Jason Sams07ae4062009-08-27 20:23:34 -070089 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsb13ada52009-08-25 11:34:49 -070090 }
91 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
92
Jason Samsb13ada52009-08-25 11:34:49 -070093
Jason Sams71362202009-10-27 14:44:31 -070094 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
95 checkEglError("eglCreateContext");
96 if (mEGL.mContext == EGL_NO_CONTEXT) {
97 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
98 }
99 gGLContextCount++;
Jason Samsd19f10d2009-05-22 14:03:28 -0700100}
101
Jason Sams71362202009-10-27 14:44:31 -0700102void Context::deinitEGL()
103{
Jason Sams3bc47d42009-11-12 15:10:25 -0800104 LOGV("deinitEGL");
105 setSurface(0, 0, NULL);
Jason Sams71362202009-10-27 14:44:31 -0700106 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
107 checkEglError("eglDestroyContext");
108
109 gGLContextCount--;
110 if (!gGLContextCount) {
111 eglTerminate(mEGL.mDisplay);
112 }
113}
114
115
Jason Sams3eaa3382009-06-10 15:04:38 -0700116bool Context::runScript(Script *s, uint32_t launchID)
Jason Samsda423d82009-06-09 12:15:30 -0700117{
118 ObjectBaseRef<ProgramFragment> frag(mFragment);
119 ObjectBaseRef<ProgramVertex> vtx(mVertex);
120 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Sams5235cf32009-09-28 18:12:56 -0700121 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Samsda423d82009-06-09 12:15:30 -0700122
Jason Sams3eaa3382009-06-10 15:04:38 -0700123 bool ret = s->run(this, launchID);
Jason Samsda423d82009-06-09 12:15:30 -0700124
Jason Sams3eaa3382009-06-10 15:04:38 -0700125 mFragment.set(frag);
126 mVertex.set(vtx);
127 mFragmentStore.set(store);
Jason Sams5235cf32009-09-28 18:12:56 -0700128 mRaster.set(raster);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700129 return ret;
Jason Samsda423d82009-06-09 12:15:30 -0700130}
131
132
Jason Samsa09f11d2009-06-04 17:58:03 -0700133bool Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -0700134{
Jason Sams66b27712009-09-25 15:25:00 -0700135 if (props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700136 timerSet(RS_TIMER_CLEAR_SWAP);
137 }
Jason Samsda423d82009-06-09 12:15:30 -0700138 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -0700139
Jason Sams59038ca2009-09-22 12:26:53 -0700140 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
141 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsb13ada52009-08-25 11:34:49 -0700142 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -0700143 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
144
Jason Sams928f5cf2009-06-08 18:50:13 -0700145 glClearColor(mRootScript->mEnviroment.mClearColor[0],
146 mRootScript->mEnviroment.mClearColor[1],
147 mRootScript->mEnviroment.mClearColor[2],
148 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsb13ada52009-08-25 11:34:49 -0700149 if (mUseDepth) {
150 glDepthMask(GL_TRUE);
151 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
152 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
153 } else {
154 glClear(GL_COLOR_BUFFER_BIT);
155 }
Jason Sams67c68442009-08-25 17:09:59 -0700156
Jason Sams66b27712009-09-25 15:25:00 -0700157 if (this->props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700158 timerSet(RS_TIMER_SCRIPT);
159 }
Jason Sams516c3192009-10-06 13:58:47 -0700160 mStateFragmentStore.mLast.clear();
Jason Sams9bee51c2009-08-05 13:57:03 -0700161 bool ret = runScript(mRootScript.get(), 0);
Jason Samsc7412b32009-10-14 15:43:53 -0700162
163 GLenum err = glGetError();
164 if (err != GL_NO_ERROR) {
165 LOGE("Pending GL Error, 0x%x", err);
166 }
167
Jason Sams9bee51c2009-08-05 13:57:03 -0700168 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700169}
170
Jason Samsf4d16062009-08-19 12:17:14 -0700171uint64_t Context::getTime() const
172{
173 struct timespec t;
174 clock_gettime(CLOCK_MONOTONIC, &t);
175 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
176}
177
178void Context::timerReset()
179{
180 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
181 mTimers[ct] = 0;
182 }
183}
184
185void Context::timerInit()
186{
187 mTimeLast = getTime();
Jason Sams2525a812009-09-03 15:43:13 -0700188 mTimeFrame = mTimeLast;
189 mTimeLastFrame = mTimeLast;
Jason Samsf4d16062009-08-19 12:17:14 -0700190 mTimerActive = RS_TIMER_INTERNAL;
191 timerReset();
192}
193
Jason Sams2525a812009-09-03 15:43:13 -0700194void Context::timerFrame()
195{
196 mTimeLastFrame = mTimeFrame;
197 mTimeFrame = getTime();
198}
199
Jason Samsf4d16062009-08-19 12:17:14 -0700200void Context::timerSet(Timers tm)
201{
202 uint64_t last = mTimeLast;
203 mTimeLast = getTime();
204 mTimers[mTimerActive] += mTimeLast - last;
205 mTimerActive = tm;
206}
207
208void Context::timerPrint()
209{
210 double total = 0;
211 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
212 total += mTimers[ct];
213 }
Jason Sams2525a812009-09-03 15:43:13 -0700214 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Samsf4d16062009-08-19 12:17:14 -0700215
Jason Sams2525a812009-09-03 15:43:13 -0700216 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
217 frame / 1000000,
Jason Samsf4d16062009-08-19 12:17:14 -0700218 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsea84a7c2009-09-04 14:42:41 -0700219 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
220 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
221 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Samsf4d16062009-08-19 12:17:14 -0700222}
223
Jason Samsd19f10d2009-05-22 14:03:28 -0700224void Context::setupCheck()
225{
Jason Samsebfb4362009-09-23 13:57:02 -0700226 mFragmentStore->setupGL(this, &mStateFragmentStore);
227 mFragment->setupGL(this, &mStateFragment);
228 mRaster->setupGL(this, &mStateRaster);
229 mVertex->setupGL(this, &mStateVertex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700230}
231
Jason Sams66b27712009-09-25 15:25:00 -0700232static bool getProp(const char *str)
Joe Onorato9ac2c662009-09-23 16:37:36 -0700233{
234 char buf[PROPERTY_VALUE_MAX];
Jason Sams66b27712009-09-25 15:25:00 -0700235 property_get(str, buf, "0");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700236 return 0 != strcmp(buf, "0");
237}
Jason Samsd19f10d2009-05-22 14:03:28 -0700238
239void * Context::threadProc(void *vrsc)
240{
241 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams7d787b42009-11-15 12:14:26 -0800242 rsc->mNativeThreadId = gettid();
243
244 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Samsd19f10d2009-05-22 14:03:28 -0700245
Jason Sams66b27712009-09-25 15:25:00 -0700246 rsc->props.mLogTimes = getProp("debug.rs.profile");
247 rsc->props.mLogScripts = getProp("debug.rs.script");
248 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700249
Jason Sams3bc47d42009-11-12 15:10:25 -0800250 //pthread_mutex_lock(&gInitMutex);
251 //rsc->initEGL();
252 //pthread_mutex_unlock(&gInitMutex);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700253
Jason Sams462d11b2009-06-19 16:03:18 -0700254 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
255 if (!tlsStruct) {
256 LOGE("Error allocating tls storage");
257 return NULL;
258 }
259 tlsStruct->mContext = rsc;
260 tlsStruct->mScript = NULL;
261 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
262 if (status) {
263 LOGE("pthread_setspecific %i", status);
264 }
265
Jason Samsebfb4362009-09-23 13:57:02 -0700266 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
267 rsc->setRaster(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700268 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700269 rsc->setVertex(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700270 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700271 rsc->setFragment(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700272 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700273 rsc->setFragmentStore(NULL);
274
Jason Samsd19f10d2009-05-22 14:03:28 -0700275 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700276 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700277 while (!rsc->mExit) {
Jason Samsbc948de2009-08-17 18:35:48 -0700278 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams5f7fc272009-06-18 16:58:42 -0700279 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800280 mDraw &= (rsc->mWndSurface != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700281
Jason Sams5f7fc272009-06-18 16:58:42 -0700282 if (mDraw) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700283 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams66b27712009-09-25 15:25:00 -0700284 if (rsc->props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700285 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
286 }
Jason Samsb13ada52009-08-25 11:34:49 -0700287 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams66b27712009-09-25 15:25:00 -0700288 if (rsc->props.mLogTimes) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700289 rsc->timerFrame();
290 rsc->timerSet(RS_TIMER_INTERNAL);
291 rsc->timerPrint();
292 rsc->timerReset();
293 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700294 }
Jason Samsf4d16062009-08-19 12:17:14 -0700295 if (rsc->mObjDestroy.mNeedToEmpty) {
296 rsc->objDestroyOOBRun();
297 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700298 }
299
Jason Samsf5b45962009-08-25 14:49:07 -0700300 LOGV("RS Thread exiting");
Jason Sams61f08d62009-09-25 16:37:33 -0700301 rsc->mRaster.clear();
302 rsc->mFragment.clear();
303 rsc->mVertex.clear();
304 rsc->mFragmentStore.clear();
305 rsc->mRootScript.clear();
306 rsc->mStateRaster.deinit(rsc);
307 rsc->mStateVertex.deinit(rsc);
308 rsc->mStateFragment.deinit(rsc);
309 rsc->mStateFragmentStore.deinit(rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700310 ObjectBase::zeroAllUserRef(rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700311
Jason Sams9d5e03d2009-11-03 11:25:42 -0800312 rsc->mObjDestroy.mNeedToEmpty = true;
313 rsc->objDestroyOOBRun();
314
Jason Samsd19f10d2009-05-22 14:03:28 -0700315 glClearColor(0,0,0,0);
316 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsb13ada52009-08-25 11:34:49 -0700317 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams71362202009-10-27 14:44:31 -0700318
319 pthread_mutex_lock(&gInitMutex);
320 rsc->deinitEGL();
321 pthread_mutex_unlock(&gInitMutex);
322
Jason Samsf5b45962009-08-25 14:49:07 -0700323 LOGV("RS Thread exited");
Jason Samsd19f10d2009-05-22 14:03:28 -0700324 return NULL;
325}
326
Jason Sams7d787b42009-11-15 12:14:26 -0800327void Context::setPriority(int32_t p)
328{
329 // Note: If we put this in the proper "background" policy
330 // the wallpapers can become completly unresponsive at times.
331 // This is probably not what we want for something the user is actively
332 // looking at.
333#if 0
334 SchedPolicy pol = SP_FOREGROUND;
335 if (p > 0) {
336 pol = SP_BACKGROUND;
337 }
338 if (!set_sched_policy(mNativeThreadId, pol)) {
339 // success; reset the priority as well
340 }
341#else
342 setpriority(PRIO_PROCESS, mNativeThreadId, p);
343#endif
344}
345
Jason Sams3bc47d42009-11-12 15:10:25 -0800346Context::Context(Device *dev, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700347{
Jason Sams41c19db92009-10-15 16:47:31 -0700348 pthread_mutex_lock(&gInitMutex);
349
Jason Samsd19f10d2009-05-22 14:03:28 -0700350 dev->addContext(this);
351 mDev = dev;
352 mRunning = false;
353 mExit = false;
Jason Samsb13ada52009-08-25 11:34:49 -0700354 mUseDepth = useDepth;
Jason Sams65e7aa52009-09-24 17:38:20 -0700355 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700356 mObjHead = NULL;
Jason Sams3bc47d42009-11-12 15:10:25 -0800357 memset(&mEGL, 0, sizeof(mEGL));
Jason Samsd19f10d2009-05-22 14:03:28 -0700358
Jason Sams8ad00102009-06-04 14:35:01 -0700359 int status;
360 pthread_attr_t threadAttr;
361
Jason Sams41c19db92009-10-15 16:47:31 -0700362 if (!gThreadTLSKeyCount) {
Jason Sams996db8d2009-10-06 17:16:55 -0700363 status = pthread_key_create(&gThreadTLSKey, NULL);
364 if (status) {
365 LOGE("Failed to init thread tls key.");
Jason Sams41c19db92009-10-15 16:47:31 -0700366 pthread_mutex_unlock(&gInitMutex);
Jason Sams996db8d2009-10-06 17:16:55 -0700367 return;
368 }
Jason Sams462d11b2009-06-19 16:03:18 -0700369 }
Jason Sams41c19db92009-10-15 16:47:31 -0700370 gThreadTLSKeyCount++;
371 pthread_mutex_unlock(&gInitMutex);
372
373 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700374
Jason Sams8ad00102009-06-04 14:35:01 -0700375 status = pthread_attr_init(&threadAttr);
376 if (status) {
377 LOGE("Failed to init thread attribute.");
378 return;
379 }
380
Jason Sams3bc47d42009-11-12 15:10:25 -0800381 mWndSurface = NULL;
Jason Samsf29ca502009-06-23 12:22:47 -0700382
Jason Sams730ee652009-08-18 17:07:09 -0700383 objDestroyOOBInit();
Jason Samsf4d16062009-08-19 12:17:14 -0700384 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700385 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700386
Jason Samsf29ca502009-06-23 12:22:47 -0700387 LOGV("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700388 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700389 if (status) {
390 LOGE("Failed to start rs context thread.");
391 }
392
Jason Samsd19f10d2009-05-22 14:03:28 -0700393 while(!mRunning) {
Jason Samse60446b2009-09-24 14:55:38 -0700394 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700395 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700396
Jason Sams8ad00102009-06-04 14:35:01 -0700397 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700398}
399
400Context::~Context()
401{
Jason Samsf5b45962009-08-25 14:49:07 -0700402 LOGV("Context::~Context");
Jason Samsd19f10d2009-05-22 14:03:28 -0700403 mExit = true;
Jason Sams65e7aa52009-09-24 17:38:20 -0700404 mPaused = false;
Jason Samsd19f10d2009-05-22 14:03:28 -0700405 void *res;
406
Jason Samsf5b45962009-08-25 14:49:07 -0700407 mIO.shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -0700408 int status = pthread_join(mThreadId, &res);
Jason Samsb7a6c432009-11-02 14:25:10 -0800409 mObjDestroy.mNeedToEmpty = true;
Jason Sams730ee652009-08-18 17:07:09 -0700410 objDestroyOOBRun();
Jason Samsd19f10d2009-05-22 14:03:28 -0700411
Jason Sams41c19db92009-10-15 16:47:31 -0700412 // Global structure cleanup.
413 pthread_mutex_lock(&gInitMutex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700414 if (mDev) {
415 mDev->removeContext(this);
Jason Sams41c19db92009-10-15 16:47:31 -0700416 --gThreadTLSKeyCount;
417 if (!gThreadTLSKeyCount) {
418 pthread_key_delete(gThreadTLSKey);
419 }
Jason Samsb7a6c432009-11-02 14:25:10 -0800420 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700421 }
Jason Sams41c19db92009-10-15 16:47:31 -0700422 pthread_mutex_unlock(&gInitMutex);
Jason Sams730ee652009-08-18 17:07:09 -0700423
424 objDestroyOOBDestroy();
Jason Samsd19f10d2009-05-22 14:03:28 -0700425}
426
Jason Sams3bc47d42009-11-12 15:10:25 -0800427void Context::setSurface(uint32_t w, uint32_t h, Surface *sur)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800428{
Jason Sams3bc47d42009-11-12 15:10:25 -0800429 LOGV("setSurface %i %i %p", w, h, sur);
430
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800431 EGLBoolean ret;
432 if (mEGL.mSurface != NULL) {
433 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
434 checkEglError("eglMakeCurrent", ret);
435
436 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
437 checkEglError("eglDestroySurface", ret);
438
439 mEGL.mSurface = NULL;
Jason Sams3bc47d42009-11-12 15:10:25 -0800440 mEGL.mWidth = 0;
441 mEGL.mHeight = 0;
442 mWidth = 0;
443 mHeight = 0;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800444 }
445
446 mWndSurface = sur;
447 if (mWndSurface != NULL) {
Jason Sams3bc47d42009-11-12 15:10:25 -0800448 bool first = false;
449 if (!mEGL.mContext) {
450 first = true;
451 pthread_mutex_lock(&gInitMutex);
452 initEGL();
453 pthread_mutex_unlock(&gInitMutex);
454 }
455
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800456 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
457 checkEglError("eglCreateWindowSurface");
458 if (mEGL.mSurface == EGL_NO_SURFACE) {
459 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
460 }
461
462 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
463 checkEglError("eglMakeCurrent", ret);
Jason Sams3bc47d42009-11-12 15:10:25 -0800464
465 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
466 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
467 mWidth = w;
468 mHeight = h;
Jason Samseb4b0312009-11-12 16:09:45 -0800469 mStateVertex.updateSize(this, w, h);
Jason Sams3bc47d42009-11-12 15:10:25 -0800470
471 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
472 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
473 }
474
475 if (first) {
476 mGL.mVersion = glGetString(GL_VERSION);
477 mGL.mVendor = glGetString(GL_VENDOR);
478 mGL.mRenderer = glGetString(GL_RENDERER);
479 mGL.mExtensions = glGetString(GL_EXTENSIONS);
480
481 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
482 LOGV("GL Version %s", mGL.mVersion);
483 LOGV("GL Vendor %s", mGL.mVendor);
484 LOGV("GL Renderer %s", mGL.mRenderer);
485 //LOGV("GL Extensions %s", mGL.mExtensions);
486
487 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
488 LOGE("Error, OpenGL ES Lite not supported");
489 } else {
490 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
491 }
492 }
493
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800494 }
495}
496
Jason Sams65e7aa52009-09-24 17:38:20 -0700497void Context::pause()
498{
499 mPaused = true;
500}
501
502void Context::resume()
503{
504 mPaused = false;
505}
506
Jason Samsd19f10d2009-05-22 14:03:28 -0700507void Context::setRootScript(Script *s)
508{
509 mRootScript.set(s);
510}
511
512void Context::setFragmentStore(ProgramFragmentStore *pfs)
513{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700514 if (pfs == NULL) {
515 mFragmentStore.set(mStateFragmentStore.mDefault);
516 } else {
517 mFragmentStore.set(pfs);
518 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700519}
520
521void Context::setFragment(ProgramFragment *pf)
522{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700523 if (pf == NULL) {
524 mFragment.set(mStateFragment.mDefault);
525 } else {
526 mFragment.set(pf);
527 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700528}
529
Jason Samsebfb4362009-09-23 13:57:02 -0700530void Context::setRaster(ProgramRaster *pr)
531{
532 if (pr == NULL) {
533 mRaster.set(mStateRaster.mDefault);
534 } else {
535 mRaster.set(pr);
536 }
537}
538
Jason Sams9bee51c2009-08-05 13:57:03 -0700539void Context::allocationCheck(const Allocation *a)
540{
541 mVertex->checkUpdatedAllocation(a);
542 mFragment->checkUpdatedAllocation(a);
543 mFragmentStore->checkUpdatedAllocation(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700544}
545
546void Context::setVertex(ProgramVertex *pv)
547{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700548 if (pv == NULL) {
549 mVertex.set(mStateVertex.mDefault);
550 } else {
551 mVertex.set(pv);
552 }
Jason Sams741a6102009-10-15 18:45:45 -0700553 mVertex->forceDirty();
Jason Samsd19f10d2009-05-22 14:03:28 -0700554}
555
Jason Samsd5680f92009-06-10 18:39:40 -0700556void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700557{
558 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700559 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700560 mNames.add(obj);
561}
562
563void Context::removeName(ObjectBase *obj)
564{
565 for(size_t ct=0; ct < mNames.size(); ct++) {
566 if (obj == mNames[ct]) {
567 mNames.removeAt(ct);
568 return;
569 }
570 }
571}
572
573ObjectBase * Context::lookupName(const char *name) const
574{
575 for(size_t ct=0; ct < mNames.size(); ct++) {
576 if (!strcmp(name, mNames[ct]->getName())) {
577 return mNames[ct];
578 }
579 }
580 return NULL;
581}
582
Jason Samsd5680f92009-06-10 18:39:40 -0700583void Context::appendNameDefines(String8 *str) const
584{
585 char buf[256];
586 for (size_t ct=0; ct < mNames.size(); ct++) {
587 str->append("#define NAMED_");
588 str->append(mNames[ct]->getName());
589 str->append(" ");
590 sprintf(buf, "%i\n", (int)mNames[ct]);
591 str->append(buf);
592 }
593}
594
Joe Onoratod7b37742009-08-09 22:57:44 -0700595void Context::appendVarDefines(String8 *str) const
596{
597 char buf[256];
598 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
599 str->append("#define ");
600 str->append(mInt32Defines.keyAt(ct));
601 str->append(" ");
602 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
603 str->append(buf);
604
605 }
606 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
607 str->append("#define ");
608 str->append(mFloatDefines.keyAt(ct));
609 str->append(" ");
610 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
611 str->append(buf);
612 }
613}
614
Jason Sams730ee652009-08-18 17:07:09 -0700615bool Context::objDestroyOOBInit()
616{
617 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
618 if (status) {
619 LOGE("Context::ObjDestroyOOBInit mutex init failure");
620 return false;
621 }
622 return true;
623}
624
625void Context::objDestroyOOBRun()
626{
627 if (mObjDestroy.mNeedToEmpty) {
628 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
629 if (status) {
630 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
631 return;
632 }
633
634 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams07ae4062009-08-27 20:23:34 -0700635 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams730ee652009-08-18 17:07:09 -0700636 }
637 mObjDestroy.mDestroyList.clear();
638 mObjDestroy.mNeedToEmpty = false;
639
640 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
641 if (status) {
642 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
643 }
644 }
645}
646
647void Context::objDestroyOOBDestroy()
648{
649 rsAssert(!mObjDestroy.mNeedToEmpty);
650 pthread_mutex_destroy(&mObjDestroy.mMutex);
651}
652
653void Context::objDestroyAdd(ObjectBase *obj)
654{
655 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
656 if (status) {
657 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
658 return;
659 }
660
661 mObjDestroy.mNeedToEmpty = true;
662 mObjDestroy.mDestroyList.add(obj);
663
664 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
665 if (status) {
666 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
667 }
668}
669
Jason Sams516c3192009-10-06 13:58:47 -0700670uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
671{
672 //LOGE("getMessageToClient %i %i", bufferLen, wait);
673 if (!wait) {
674 if (mIO.mToClient.isEmpty()) {
675 // No message to get and not going to wait for one.
676 receiveLen = 0;
677 return 0;
678 }
679 }
680
681 //LOGE("getMessageToClient 2 con=%p", this);
682 uint32_t bytesData = 0;
683 uint32_t commandID = 0;
684 const void *d = mIO.mToClient.get(&commandID, &bytesData);
685 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
686
687 *receiveLen = bytesData;
688 if (bufferLen >= bytesData) {
689 memcpy(data, d, bytesData);
690 mIO.mToClient.next();
691 return commandID;
692 }
693 return 0;
694}
695
696bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
697{
698 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
699 if (cmdID == 0) {
700 LOGE("Attempting to send invalid command 0 to client.");
701 return false;
702 }
703 if (!waitForSpace) {
704 if (mIO.mToClient.getFreeSpace() < len) {
705 // Not enough room, and not waiting.
706 return false;
707 }
708 }
709 //LOGE("sendMessageToClient 2");
710 void *p = mIO.mToClient.reserve(len);
711 memcpy(p, data, len);
712 mIO.mToClient.commit(cmdID, len);
713 //LOGE("sendMessageToClient 3");
714 return true;
715}
716
717void Context::initToClient()
718{
719 while(!mRunning) {
720 usleep(100);
721 }
722}
723
724void Context::deinitToClient()
725{
726 mIO.mToClient.shutdown();
727}
Jason Sams730ee652009-08-18 17:07:09 -0700728
Jason Samsd5680f92009-06-10 18:39:40 -0700729
Jason Samsd19f10d2009-05-22 14:03:28 -0700730///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700731//
Jason Samsd19f10d2009-05-22 14:03:28 -0700732
733namespace android {
734namespace renderscript {
735
736
737void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
738{
739 Script *s = static_cast<Script *>(vs);
740 rsc->setRootScript(s);
741}
742
743void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
744{
745 Sampler *s = static_cast<Sampler *>(vs);
746
747 if (slot > RS_MAX_SAMPLER_SLOT) {
748 LOGE("Invalid sampler slot");
749 return;
750 }
751
752 s->bindToContext(&rsc->mStateSampler, slot);
753}
754
755void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
756{
757 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
758 rsc->setFragmentStore(pfs);
759}
760
761void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
762{
763 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
764 rsc->setFragment(pf);
765}
766
Jason Samsebfb4362009-09-23 13:57:02 -0700767void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
768{
769 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
770 rsc->setRaster(pr);
771}
772
Jason Samsd19f10d2009-05-22 14:03:28 -0700773void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
774{
775 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
776 rsc->setVertex(pv);
777}
778
Jason Samsd5680f92009-06-10 18:39:40 -0700779void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700780{
781 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsd5680f92009-06-10 18:39:40 -0700782 rsc->assignName(ob, name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700783}
Jason Samsd19f10d2009-05-22 14:03:28 -0700784
Jason Sams7ce033d2009-08-18 14:14:24 -0700785void rsi_ObjDestroy(Context *rsc, void *obj)
786{
787 ObjectBase *ob = static_cast<ObjectBase *>(obj);
788 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700789 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700790}
791
Joe Onoratod7b37742009-08-09 22:57:44 -0700792void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
793{
794 rsc->addInt32Define(name, value);
795}
796
797void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
798{
799 rsc->addFloatDefine(name, value);
800}
Jason Samsd19f10d2009-05-22 14:03:28 -0700801
Jason Sams65e7aa52009-09-24 17:38:20 -0700802void rsi_ContextPause(Context *rsc)
803{
804 rsc->pause();
805}
806
807void rsi_ContextResume(Context *rsc)
808{
809 rsc->resume();
810}
811
Jason Sams3bc47d42009-11-12 15:10:25 -0800812void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800813{
Jason Sams3bc47d42009-11-12 15:10:25 -0800814 rsc->setSurface(w, h, (Surface *)sur);
815}
816
Jason Sams7d787b42009-11-15 12:14:26 -0800817void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams3bc47d42009-11-12 15:10:25 -0800818{
Jason Sams7d787b42009-11-15 12:14:26 -0800819 rsc->setPriority(p);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800820}
821
Jason Sams715333b2009-11-17 17:26:46 -0800822void rsi_ContextDump(Context *rsc, int32_t bits)
823{
824 ObjectBase::dumpAll(rsc);
825}
826
Jason Samsd19f10d2009-05-22 14:03:28 -0700827}
828}
829
830
Jason Sams3bc47d42009-11-12 15:10:25 -0800831RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700832{
833 Device * dev = static_cast<Device *>(vdev);
Jason Sams3bc47d42009-11-12 15:10:25 -0800834 Context *rsc = new Context(dev, useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -0700835 return rsc;
836}
837
838void rsContextDestroy(RsContext vrsc)
839{
840 Context * rsc = static_cast<Context *>(vrsc);
841 delete rsc;
842}
843
Jason Sams730ee652009-08-18 17:07:09 -0700844void rsObjDestroyOOB(RsContext vrsc, void *obj)
845{
846 Context * rsc = static_cast<Context *>(vrsc);
847 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
848}
849
Jason Sams516c3192009-10-06 13:58:47 -0700850uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
851{
852 Context * rsc = static_cast<Context *>(vrsc);
853 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
854}
855
856void rsContextInitToClient(RsContext vrsc)
857{
858 Context * rsc = static_cast<Context *>(vrsc);
859 rsc->initToClient();
860}
861
862void rsContextDeinitToClient(RsContext vrsc)
863{
864 Context * rsc = static_cast<Context *>(vrsc);
865 rsc->deinitToClient();
866}
867