blob: 3652f0acdc5a3bcc73ca0cd1e0b1727eb5782a88 [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 Samsb9d5c572009-12-09 11:05:45 -0800116uint32_t 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 Samsb9d5c572009-12-09 11:05:45 -0800123 uint32_t 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 Samsb9d5c572009-12-09 11:05:45 -0800133uint32_t Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -0700134{
Jason Samsb9d5c572009-12-09 11:05:45 -0800135 timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsda423d82009-06-09 12:15:30 -0700136 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -0700137
Jason Sams59038ca2009-09-22 12:26:53 -0700138 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
139 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsb13ada52009-08-25 11:34:49 -0700140 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -0700141 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
142
Jason Sams928f5cf2009-06-08 18:50:13 -0700143 glClearColor(mRootScript->mEnviroment.mClearColor[0],
144 mRootScript->mEnviroment.mClearColor[1],
145 mRootScript->mEnviroment.mClearColor[2],
146 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsb13ada52009-08-25 11:34:49 -0700147 if (mUseDepth) {
148 glDepthMask(GL_TRUE);
149 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
151 } else {
152 glClear(GL_COLOR_BUFFER_BIT);
153 }
Jason Sams67c68442009-08-25 17:09:59 -0700154
Jason Samsb9d5c572009-12-09 11:05:45 -0800155 timerSet(RS_TIMER_SCRIPT);
Jason Sams516c3192009-10-06 13:58:47 -0700156 mStateFragmentStore.mLast.clear();
Jason Samsb9d5c572009-12-09 11:05:45 -0800157 uint32_t ret = runScript(mRootScript.get(), 0);
Jason Samsc7412b32009-10-14 15:43:53 -0700158
159 GLenum err = glGetError();
160 if (err != GL_NO_ERROR) {
161 LOGE("Pending GL Error, 0x%x", err);
162 }
163
Jason Sams9bee51c2009-08-05 13:57:03 -0700164 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700165}
166
Jason Samsf4d16062009-08-19 12:17:14 -0700167uint64_t Context::getTime() const
168{
169 struct timespec t;
170 clock_gettime(CLOCK_MONOTONIC, &t);
171 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
172}
173
174void Context::timerReset()
175{
176 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
177 mTimers[ct] = 0;
178 }
179}
180
181void Context::timerInit()
182{
183 mTimeLast = getTime();
Jason Sams2525a812009-09-03 15:43:13 -0700184 mTimeFrame = mTimeLast;
185 mTimeLastFrame = mTimeLast;
Jason Samsf4d16062009-08-19 12:17:14 -0700186 mTimerActive = RS_TIMER_INTERNAL;
187 timerReset();
188}
189
Jason Sams2525a812009-09-03 15:43:13 -0700190void Context::timerFrame()
191{
192 mTimeLastFrame = mTimeFrame;
193 mTimeFrame = getTime();
194}
195
Jason Samsf4d16062009-08-19 12:17:14 -0700196void Context::timerSet(Timers tm)
197{
198 uint64_t last = mTimeLast;
199 mTimeLast = getTime();
200 mTimers[mTimerActive] += mTimeLast - last;
201 mTimerActive = tm;
202}
203
204void Context::timerPrint()
205{
206 double total = 0;
207 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
208 total += mTimers[ct];
209 }
Jason Sams2525a812009-09-03 15:43:13 -0700210 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800211 mTimeMSLastFrame = frame / 1000000;
212 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
213 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Samsf4d16062009-08-19 12:17:14 -0700214
Jason Samsb9d5c572009-12-09 11:05:45 -0800215
216 if (props.mLogTimes) {
217 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
218 mTimeMSLastFrame,
219 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
220 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
221 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
222 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
223 }
Jason Samsf4d16062009-08-19 12:17:14 -0700224}
225
Jason Samsd19f10d2009-05-22 14:03:28 -0700226void Context::setupCheck()
227{
Jason Samsebfb4362009-09-23 13:57:02 -0700228 mFragmentStore->setupGL(this, &mStateFragmentStore);
229 mFragment->setupGL(this, &mStateFragment);
230 mRaster->setupGL(this, &mStateRaster);
231 mVertex->setupGL(this, &mStateVertex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700232}
233
Jason Sams66b27712009-09-25 15:25:00 -0700234static bool getProp(const char *str)
Joe Onorato9ac2c662009-09-23 16:37:36 -0700235{
236 char buf[PROPERTY_VALUE_MAX];
Jason Sams66b27712009-09-25 15:25:00 -0700237 property_get(str, buf, "0");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700238 return 0 != strcmp(buf, "0");
239}
Jason Samsd19f10d2009-05-22 14:03:28 -0700240
241void * Context::threadProc(void *vrsc)
242{
243 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams7d787b42009-11-15 12:14:26 -0800244 rsc->mNativeThreadId = gettid();
245
246 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Samsb9d5c572009-12-09 11:05:45 -0800247 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Samsd19f10d2009-05-22 14:03:28 -0700248
Jason Sams66b27712009-09-25 15:25:00 -0700249 rsc->props.mLogTimes = getProp("debug.rs.profile");
250 rsc->props.mLogScripts = getProp("debug.rs.script");
251 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700252
Jason Sams3bc47d42009-11-12 15:10:25 -0800253 //pthread_mutex_lock(&gInitMutex);
254 //rsc->initEGL();
255 //pthread_mutex_unlock(&gInitMutex);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700256
Jason Sams462d11b2009-06-19 16:03:18 -0700257 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
258 if (!tlsStruct) {
259 LOGE("Error allocating tls storage");
260 return NULL;
261 }
262 tlsStruct->mContext = rsc;
263 tlsStruct->mScript = NULL;
264 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
265 if (status) {
266 LOGE("pthread_setspecific %i", status);
267 }
268
Jason Samsebfb4362009-09-23 13:57:02 -0700269 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
270 rsc->setRaster(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700271 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700272 rsc->setVertex(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700273 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700274 rsc->setFragment(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700275 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700276 rsc->setFragmentStore(NULL);
277
Jason Samsd19f10d2009-05-22 14:03:28 -0700278 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700279 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700280 while (!rsc->mExit) {
Jason Samsbc948de2009-08-17 18:35:48 -0700281 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams5f7fc272009-06-18 16:58:42 -0700282 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800283 mDraw &= (rsc->mWndSurface != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700284
Jason Samsb9d5c572009-12-09 11:05:45 -0800285 uint32_t targetTime = 0;
Jason Sams5f7fc272009-06-18 16:58:42 -0700286 if (mDraw) {
Jason Samsb9d5c572009-12-09 11:05:45 -0800287 targetTime = rsc->runRootScript();
288 mDraw = targetTime && !rsc->mPaused;
289 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsb13ada52009-08-25 11:34:49 -0700290 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Samsb9d5c572009-12-09 11:05:45 -0800291 rsc->timerFrame();
292 rsc->timerSet(RS_TIMER_INTERNAL);
293 rsc->timerPrint();
294 rsc->timerReset();
Jason Samsd19f10d2009-05-22 14:03:28 -0700295 }
Jason Samsf4d16062009-08-19 12:17:14 -0700296 if (rsc->mObjDestroy.mNeedToEmpty) {
297 rsc->objDestroyOOBRun();
298 }
Jason Samsb9d5c572009-12-09 11:05:45 -0800299 if (rsc->mThreadPriority > 0 && targetTime) {
300 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
301 if (t > 0) {
302 usleep(t);
303 }
304 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700305 }
306
Jason Samsf5b45962009-08-25 14:49:07 -0700307 LOGV("RS Thread exiting");
Jason Sams61f08d62009-09-25 16:37:33 -0700308 rsc->mRaster.clear();
309 rsc->mFragment.clear();
310 rsc->mVertex.clear();
311 rsc->mFragmentStore.clear();
312 rsc->mRootScript.clear();
313 rsc->mStateRaster.deinit(rsc);
314 rsc->mStateVertex.deinit(rsc);
315 rsc->mStateFragment.deinit(rsc);
316 rsc->mStateFragmentStore.deinit(rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700317 ObjectBase::zeroAllUserRef(rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700318
Jason Sams9d5e03d2009-11-03 11:25:42 -0800319 rsc->mObjDestroy.mNeedToEmpty = true;
320 rsc->objDestroyOOBRun();
321
Jason Samsd19f10d2009-05-22 14:03:28 -0700322 glClearColor(0,0,0,0);
323 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsb13ada52009-08-25 11:34:49 -0700324 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams71362202009-10-27 14:44:31 -0700325
326 pthread_mutex_lock(&gInitMutex);
327 rsc->deinitEGL();
328 pthread_mutex_unlock(&gInitMutex);
329
Jason Samsf5b45962009-08-25 14:49:07 -0700330 LOGV("RS Thread exited");
Jason Samsd19f10d2009-05-22 14:03:28 -0700331 return NULL;
332}
333
Jason Sams7d787b42009-11-15 12:14:26 -0800334void Context::setPriority(int32_t p)
335{
336 // Note: If we put this in the proper "background" policy
337 // the wallpapers can become completly unresponsive at times.
338 // This is probably not what we want for something the user is actively
339 // looking at.
Jason Samsb9d5c572009-12-09 11:05:45 -0800340 mThreadPriority = p;
Jason Sams7d787b42009-11-15 12:14:26 -0800341#if 0
342 SchedPolicy pol = SP_FOREGROUND;
343 if (p > 0) {
344 pol = SP_BACKGROUND;
345 }
346 if (!set_sched_policy(mNativeThreadId, pol)) {
347 // success; reset the priority as well
348 }
349#else
350 setpriority(PRIO_PROCESS, mNativeThreadId, p);
351#endif
352}
353
Jason Sams3bc47d42009-11-12 15:10:25 -0800354Context::Context(Device *dev, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700355{
Jason Sams41c19db92009-10-15 16:47:31 -0700356 pthread_mutex_lock(&gInitMutex);
357
Jason Samsd19f10d2009-05-22 14:03:28 -0700358 dev->addContext(this);
359 mDev = dev;
360 mRunning = false;
361 mExit = false;
Jason Samsb13ada52009-08-25 11:34:49 -0700362 mUseDepth = useDepth;
Jason Sams65e7aa52009-09-24 17:38:20 -0700363 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700364 mObjHead = NULL;
Jason Sams3bc47d42009-11-12 15:10:25 -0800365 memset(&mEGL, 0, sizeof(mEGL));
Jason Samsd19f10d2009-05-22 14:03:28 -0700366
Jason Sams8ad00102009-06-04 14:35:01 -0700367 int status;
368 pthread_attr_t threadAttr;
369
Jason Sams41c19db92009-10-15 16:47:31 -0700370 if (!gThreadTLSKeyCount) {
Jason Sams996db8d2009-10-06 17:16:55 -0700371 status = pthread_key_create(&gThreadTLSKey, NULL);
372 if (status) {
373 LOGE("Failed to init thread tls key.");
Jason Sams41c19db92009-10-15 16:47:31 -0700374 pthread_mutex_unlock(&gInitMutex);
Jason Sams996db8d2009-10-06 17:16:55 -0700375 return;
376 }
Jason Sams462d11b2009-06-19 16:03:18 -0700377 }
Jason Sams41c19db92009-10-15 16:47:31 -0700378 gThreadTLSKeyCount++;
379 pthread_mutex_unlock(&gInitMutex);
380
381 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700382
Jason Sams8ad00102009-06-04 14:35:01 -0700383 status = pthread_attr_init(&threadAttr);
384 if (status) {
385 LOGE("Failed to init thread attribute.");
386 return;
387 }
388
Jason Sams3bc47d42009-11-12 15:10:25 -0800389 mWndSurface = NULL;
Jason Samsf29ca502009-06-23 12:22:47 -0700390
Jason Sams730ee652009-08-18 17:07:09 -0700391 objDestroyOOBInit();
Jason Samsf4d16062009-08-19 12:17:14 -0700392 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700393 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700394
Jason Samsf29ca502009-06-23 12:22:47 -0700395 LOGV("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700396 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700397 if (status) {
398 LOGE("Failed to start rs context thread.");
399 }
400
Jason Samsd19f10d2009-05-22 14:03:28 -0700401 while(!mRunning) {
Jason Samse60446b2009-09-24 14:55:38 -0700402 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700403 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700404
Jason Sams8ad00102009-06-04 14:35:01 -0700405 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700406}
407
408Context::~Context()
409{
Jason Samsf5b45962009-08-25 14:49:07 -0700410 LOGV("Context::~Context");
Jason Samsd19f10d2009-05-22 14:03:28 -0700411 mExit = true;
Jason Sams65e7aa52009-09-24 17:38:20 -0700412 mPaused = false;
Jason Samsd19f10d2009-05-22 14:03:28 -0700413 void *res;
414
Jason Samsf5b45962009-08-25 14:49:07 -0700415 mIO.shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -0700416 int status = pthread_join(mThreadId, &res);
Jason Samsb7a6c432009-11-02 14:25:10 -0800417 mObjDestroy.mNeedToEmpty = true;
Jason Sams730ee652009-08-18 17:07:09 -0700418 objDestroyOOBRun();
Jason Samsd19f10d2009-05-22 14:03:28 -0700419
Jason Sams41c19db92009-10-15 16:47:31 -0700420 // Global structure cleanup.
421 pthread_mutex_lock(&gInitMutex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700422 if (mDev) {
423 mDev->removeContext(this);
Jason Sams41c19db92009-10-15 16:47:31 -0700424 --gThreadTLSKeyCount;
425 if (!gThreadTLSKeyCount) {
426 pthread_key_delete(gThreadTLSKey);
427 }
Jason Samsb7a6c432009-11-02 14:25:10 -0800428 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700429 }
Jason Sams41c19db92009-10-15 16:47:31 -0700430 pthread_mutex_unlock(&gInitMutex);
Jason Sams730ee652009-08-18 17:07:09 -0700431
432 objDestroyOOBDestroy();
Jason Samsd19f10d2009-05-22 14:03:28 -0700433}
434
Jason Sams3bc47d42009-11-12 15:10:25 -0800435void Context::setSurface(uint32_t w, uint32_t h, Surface *sur)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800436{
Jason Sams3bc47d42009-11-12 15:10:25 -0800437 LOGV("setSurface %i %i %p", w, h, sur);
438
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800439 EGLBoolean ret;
440 if (mEGL.mSurface != NULL) {
441 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
442 checkEglError("eglMakeCurrent", ret);
443
444 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
445 checkEglError("eglDestroySurface", ret);
446
447 mEGL.mSurface = NULL;
Jason Sams3bc47d42009-11-12 15:10:25 -0800448 mEGL.mWidth = 0;
449 mEGL.mHeight = 0;
450 mWidth = 0;
451 mHeight = 0;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800452 }
453
454 mWndSurface = sur;
455 if (mWndSurface != NULL) {
Jason Sams3bc47d42009-11-12 15:10:25 -0800456 bool first = false;
457 if (!mEGL.mContext) {
458 first = true;
459 pthread_mutex_lock(&gInitMutex);
460 initEGL();
461 pthread_mutex_unlock(&gInitMutex);
462 }
463
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800464 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
465 checkEglError("eglCreateWindowSurface");
466 if (mEGL.mSurface == EGL_NO_SURFACE) {
467 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
468 }
469
470 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
471 checkEglError("eglMakeCurrent", ret);
Jason Sams3bc47d42009-11-12 15:10:25 -0800472
473 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
474 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
475 mWidth = w;
476 mHeight = h;
Jason Samseb4b0312009-11-12 16:09:45 -0800477 mStateVertex.updateSize(this, w, h);
Jason Sams3bc47d42009-11-12 15:10:25 -0800478
479 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
480 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
481 }
482
483 if (first) {
484 mGL.mVersion = glGetString(GL_VERSION);
485 mGL.mVendor = glGetString(GL_VENDOR);
486 mGL.mRenderer = glGetString(GL_RENDERER);
487 mGL.mExtensions = glGetString(GL_EXTENSIONS);
488
489 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
490 LOGV("GL Version %s", mGL.mVersion);
491 LOGV("GL Vendor %s", mGL.mVendor);
492 LOGV("GL Renderer %s", mGL.mRenderer);
493 //LOGV("GL Extensions %s", mGL.mExtensions);
494
495 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
496 LOGE("Error, OpenGL ES Lite not supported");
497 } else {
498 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
499 }
500 }
501
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800502 }
503}
504
Jason Sams65e7aa52009-09-24 17:38:20 -0700505void Context::pause()
506{
507 mPaused = true;
508}
509
510void Context::resume()
511{
512 mPaused = false;
513}
514
Jason Samsd19f10d2009-05-22 14:03:28 -0700515void Context::setRootScript(Script *s)
516{
517 mRootScript.set(s);
518}
519
520void Context::setFragmentStore(ProgramFragmentStore *pfs)
521{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700522 if (pfs == NULL) {
523 mFragmentStore.set(mStateFragmentStore.mDefault);
524 } else {
525 mFragmentStore.set(pfs);
526 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700527}
528
529void Context::setFragment(ProgramFragment *pf)
530{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700531 if (pf == NULL) {
532 mFragment.set(mStateFragment.mDefault);
533 } else {
534 mFragment.set(pf);
535 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700536}
537
Jason Samsebfb4362009-09-23 13:57:02 -0700538void Context::setRaster(ProgramRaster *pr)
539{
540 if (pr == NULL) {
541 mRaster.set(mStateRaster.mDefault);
542 } else {
543 mRaster.set(pr);
544 }
545}
546
Jason Sams9bee51c2009-08-05 13:57:03 -0700547void Context::allocationCheck(const Allocation *a)
548{
549 mVertex->checkUpdatedAllocation(a);
550 mFragment->checkUpdatedAllocation(a);
551 mFragmentStore->checkUpdatedAllocation(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700552}
553
554void Context::setVertex(ProgramVertex *pv)
555{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700556 if (pv == NULL) {
557 mVertex.set(mStateVertex.mDefault);
558 } else {
559 mVertex.set(pv);
560 }
Jason Sams741a6102009-10-15 18:45:45 -0700561 mVertex->forceDirty();
Jason Samsd19f10d2009-05-22 14:03:28 -0700562}
563
Jason Samsd5680f92009-06-10 18:39:40 -0700564void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700565{
566 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700567 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700568 mNames.add(obj);
569}
570
571void Context::removeName(ObjectBase *obj)
572{
573 for(size_t ct=0; ct < mNames.size(); ct++) {
574 if (obj == mNames[ct]) {
575 mNames.removeAt(ct);
576 return;
577 }
578 }
579}
580
581ObjectBase * Context::lookupName(const char *name) const
582{
583 for(size_t ct=0; ct < mNames.size(); ct++) {
584 if (!strcmp(name, mNames[ct]->getName())) {
585 return mNames[ct];
586 }
587 }
588 return NULL;
589}
590
Jason Samsd5680f92009-06-10 18:39:40 -0700591void Context::appendNameDefines(String8 *str) const
592{
593 char buf[256];
594 for (size_t ct=0; ct < mNames.size(); ct++) {
595 str->append("#define NAMED_");
596 str->append(mNames[ct]->getName());
597 str->append(" ");
598 sprintf(buf, "%i\n", (int)mNames[ct]);
599 str->append(buf);
600 }
601}
602
Joe Onoratod7b37742009-08-09 22:57:44 -0700603void Context::appendVarDefines(String8 *str) const
604{
605 char buf[256];
606 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
607 str->append("#define ");
608 str->append(mInt32Defines.keyAt(ct));
609 str->append(" ");
610 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
611 str->append(buf);
612
613 }
614 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
615 str->append("#define ");
616 str->append(mFloatDefines.keyAt(ct));
617 str->append(" ");
618 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
619 str->append(buf);
620 }
621}
622
Jason Sams730ee652009-08-18 17:07:09 -0700623bool Context::objDestroyOOBInit()
624{
625 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
626 if (status) {
627 LOGE("Context::ObjDestroyOOBInit mutex init failure");
628 return false;
629 }
630 return true;
631}
632
633void Context::objDestroyOOBRun()
634{
635 if (mObjDestroy.mNeedToEmpty) {
636 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
637 if (status) {
638 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
639 return;
640 }
641
642 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams07ae4062009-08-27 20:23:34 -0700643 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams730ee652009-08-18 17:07:09 -0700644 }
645 mObjDestroy.mDestroyList.clear();
646 mObjDestroy.mNeedToEmpty = false;
647
648 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
649 if (status) {
650 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
651 }
652 }
653}
654
655void Context::objDestroyOOBDestroy()
656{
657 rsAssert(!mObjDestroy.mNeedToEmpty);
658 pthread_mutex_destroy(&mObjDestroy.mMutex);
659}
660
661void Context::objDestroyAdd(ObjectBase *obj)
662{
663 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
664 if (status) {
665 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
666 return;
667 }
668
669 mObjDestroy.mNeedToEmpty = true;
670 mObjDestroy.mDestroyList.add(obj);
671
672 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
673 if (status) {
674 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
675 }
676}
677
Jason Sams516c3192009-10-06 13:58:47 -0700678uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
679{
680 //LOGE("getMessageToClient %i %i", bufferLen, wait);
681 if (!wait) {
682 if (mIO.mToClient.isEmpty()) {
683 // No message to get and not going to wait for one.
684 receiveLen = 0;
685 return 0;
686 }
687 }
688
689 //LOGE("getMessageToClient 2 con=%p", this);
690 uint32_t bytesData = 0;
691 uint32_t commandID = 0;
692 const void *d = mIO.mToClient.get(&commandID, &bytesData);
693 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
694
695 *receiveLen = bytesData;
696 if (bufferLen >= bytesData) {
697 memcpy(data, d, bytesData);
698 mIO.mToClient.next();
699 return commandID;
700 }
701 return 0;
702}
703
704bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
705{
706 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
707 if (cmdID == 0) {
708 LOGE("Attempting to send invalid command 0 to client.");
709 return false;
710 }
711 if (!waitForSpace) {
712 if (mIO.mToClient.getFreeSpace() < len) {
713 // Not enough room, and not waiting.
714 return false;
715 }
716 }
717 //LOGE("sendMessageToClient 2");
718 void *p = mIO.mToClient.reserve(len);
719 memcpy(p, data, len);
720 mIO.mToClient.commit(cmdID, len);
721 //LOGE("sendMessageToClient 3");
722 return true;
723}
724
725void Context::initToClient()
726{
727 while(!mRunning) {
728 usleep(100);
729 }
730}
731
732void Context::deinitToClient()
733{
734 mIO.mToClient.shutdown();
735}
Jason Sams730ee652009-08-18 17:07:09 -0700736
Jason Sams9dab6672009-11-24 12:26:35 -0800737void Context::dumpDebug() const
738{
739 LOGE("RS Context debug %p", this);
740 LOGE("RS Context debug");
741
742 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
743 LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext,
744 mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay);
745 LOGE(" GL vendor: %s", mGL.mVendor);
746 LOGE(" GL renderer: %s", mGL.mRenderer);
747 LOGE(" GL Version: %s", mGL.mVersion);
748 LOGE(" GL Extensions: %s", mGL.mExtensions);
749 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
750 LOGE(" RS width %i, height %i", mWidth, mHeight);
751 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
752 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
753
754}
Jason Samsd5680f92009-06-10 18:39:40 -0700755
Jason Samsd19f10d2009-05-22 14:03:28 -0700756///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700757//
Jason Samsd19f10d2009-05-22 14:03:28 -0700758
759namespace android {
760namespace renderscript {
761
762
763void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
764{
765 Script *s = static_cast<Script *>(vs);
766 rsc->setRootScript(s);
767}
768
769void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
770{
771 Sampler *s = static_cast<Sampler *>(vs);
772
773 if (slot > RS_MAX_SAMPLER_SLOT) {
774 LOGE("Invalid sampler slot");
775 return;
776 }
777
778 s->bindToContext(&rsc->mStateSampler, slot);
779}
780
781void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
782{
783 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
784 rsc->setFragmentStore(pfs);
785}
786
787void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
788{
789 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
790 rsc->setFragment(pf);
791}
792
Jason Samsebfb4362009-09-23 13:57:02 -0700793void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
794{
795 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
796 rsc->setRaster(pr);
797}
798
Jason Samsd19f10d2009-05-22 14:03:28 -0700799void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
800{
801 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
802 rsc->setVertex(pv);
803}
804
Jason Samsd5680f92009-06-10 18:39:40 -0700805void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700806{
807 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsd5680f92009-06-10 18:39:40 -0700808 rsc->assignName(ob, name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700809}
Jason Samsd19f10d2009-05-22 14:03:28 -0700810
Jason Sams7ce033d2009-08-18 14:14:24 -0700811void rsi_ObjDestroy(Context *rsc, void *obj)
812{
813 ObjectBase *ob = static_cast<ObjectBase *>(obj);
814 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700815 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700816}
817
Joe Onoratod7b37742009-08-09 22:57:44 -0700818void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
819{
820 rsc->addInt32Define(name, value);
821}
822
823void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
824{
825 rsc->addFloatDefine(name, value);
826}
Jason Samsd19f10d2009-05-22 14:03:28 -0700827
Jason Sams65e7aa52009-09-24 17:38:20 -0700828void rsi_ContextPause(Context *rsc)
829{
830 rsc->pause();
831}
832
833void rsi_ContextResume(Context *rsc)
834{
835 rsc->resume();
836}
837
Jason Sams3bc47d42009-11-12 15:10:25 -0800838void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800839{
Jason Sams3bc47d42009-11-12 15:10:25 -0800840 rsc->setSurface(w, h, (Surface *)sur);
841}
842
Jason Sams7d787b42009-11-15 12:14:26 -0800843void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams3bc47d42009-11-12 15:10:25 -0800844{
Jason Sams7d787b42009-11-15 12:14:26 -0800845 rsc->setPriority(p);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800846}
847
Jason Sams715333b2009-11-17 17:26:46 -0800848void rsi_ContextDump(Context *rsc, int32_t bits)
849{
850 ObjectBase::dumpAll(rsc);
851}
852
Jason Samsd19f10d2009-05-22 14:03:28 -0700853}
854}
855
856
Jason Sams3bc47d42009-11-12 15:10:25 -0800857RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700858{
859 Device * dev = static_cast<Device *>(vdev);
Jason Sams3bc47d42009-11-12 15:10:25 -0800860 Context *rsc = new Context(dev, useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -0700861 return rsc;
862}
863
864void rsContextDestroy(RsContext vrsc)
865{
866 Context * rsc = static_cast<Context *>(vrsc);
867 delete rsc;
868}
869
Jason Sams730ee652009-08-18 17:07:09 -0700870void rsObjDestroyOOB(RsContext vrsc, void *obj)
871{
872 Context * rsc = static_cast<Context *>(vrsc);
873 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
874}
875
Jason Sams516c3192009-10-06 13:58:47 -0700876uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
877{
878 Context * rsc = static_cast<Context *>(vrsc);
879 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
880}
881
882void rsContextInitToClient(RsContext vrsc)
883{
884 Context * rsc = static_cast<Context *>(vrsc);
885 rsc->initToClient();
886}
887
888void rsContextDeinitToClient(RsContext vrsc)
889{
890 Context * rsc = static_cast<Context *>(vrsc);
891 rsc->deinitToClient();
892}
893