blob: 51277172a0f922212453e3d615929863adb5d8b5 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsafcb25c2009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070022
Joe Onorato76371ff2009-09-23 16:37:36 -070023#include <cutils/properties.h>
24
Jason Sams1aa5a4e2009-06-22 17:15:15 -070025#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028using namespace android;
29using namespace android::renderscript;
30
Jason Samse5769102009-06-19 16:03:18 -070031pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070032
33void Context::initEGL()
34{
Jason Samsafcb25c2009-08-25 11:34:49 -070035 mEGL.mNumConfigs = -1;
36 EGLint configAttribs[128];
37 EGLint *configAttribsPtr = configAttribs;
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Jason Samsafcb25c2009-08-25 11:34:49 -070039 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070040
Jason Samsafcb25c2009-08-25 11:34:49 -070041 configAttribsPtr[0] = EGL_SURFACE_TYPE;
42 configAttribsPtr[1] = EGL_WINDOW_BIT;
43 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070044
Jason Samsafcb25c2009-08-25 11:34:49 -070045 if (mUseDepth) {
46 configAttribsPtr[0] = EGL_DEPTH_SIZE;
47 configAttribsPtr[1] = 16;
48 configAttribsPtr += 2;
49 }
Jason Sams9397e302009-08-27 20:23:34 -070050
Jason Sams5fd09d82009-09-23 13:57:02 -070051 if (mDev->mForceSW) {
52 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
53 configAttribsPtr[1] = EGL_SLOW_CONFIG;
54 configAttribsPtr += 2;
55 }
56
Jason Samsafcb25c2009-08-25 11:34:49 -070057 configAttribsPtr[0] = EGL_NONE;
58 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070059
Jason Sams6d751ef2009-10-08 12:55:06 -070060 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070061 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
62 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
63
64 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
65 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070066 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070067 }
68 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
69
70 if (mWndSurface) {
71 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
72 } else {
73 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
74 android_createDisplaySurface(),
75 NULL);
76 }
77
78 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
79 eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
80 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
81 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
82
83
84 mGL.mVersion = glGetString(GL_VERSION);
85 mGL.mVendor = glGetString(GL_VENDOR);
86 mGL.mRenderer = glGetString(GL_RENDERER);
87 mGL.mExtensions = glGetString(GL_EXTENSIONS);
88
Jason Sams9397e302009-08-27 20:23:34 -070089 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
90 LOGV("GL Version %s", mGL.mVersion);
91 LOGV("GL Vendor %s", mGL.mVendor);
92 LOGV("GL Renderer %s", mGL.mRenderer);
93 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -070094
Jason Sams306fb232009-08-25 17:09:59 -070095 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -070096 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -070097 } else {
98 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -070099 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700100}
101
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700102bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700103{
104 ObjectBaseRef<ProgramFragment> frag(mFragment);
105 ObjectBaseRef<ProgramVertex> vtx(mVertex);
106 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700107 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700108
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700109 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700110
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700111 mFragment.set(frag);
112 mVertex.set(vtx);
113 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700114 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700115 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700116}
117
118
Jason Samsa44cb292009-06-04 17:58:03 -0700119bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700120{
Jason Sams1fddd902009-09-25 15:25:00 -0700121 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700122 timerSet(RS_TIMER_CLEAR_SWAP);
123 }
Jason Sams10308932009-06-09 12:15:30 -0700124 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700125
Jason Sams8c9534b2009-09-22 12:26:53 -0700126 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
127 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700128 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
130
Jason Sams928b7342009-06-08 18:50:13 -0700131 glClearColor(mRootScript->mEnviroment.mClearColor[0],
132 mRootScript->mEnviroment.mClearColor[1],
133 mRootScript->mEnviroment.mClearColor[2],
134 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700135 if (mUseDepth) {
136 glDepthMask(GL_TRUE);
137 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
138 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
139 } else {
140 glClear(GL_COLOR_BUFFER_BIT);
141 }
Jason Sams306fb232009-08-25 17:09:59 -0700142
Jason Sams1fddd902009-09-25 15:25:00 -0700143 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700144 timerSet(RS_TIMER_SCRIPT);
145 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700146 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700147 bool ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700148
149 GLenum err = glGetError();
150 if (err != GL_NO_ERROR) {
151 LOGE("Pending GL Error, 0x%x", err);
152 }
153
Jason Samscfb1d112009-08-05 13:57:03 -0700154 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700155}
156
Jason Sams24371d92009-08-19 12:17:14 -0700157uint64_t Context::getTime() const
158{
159 struct timespec t;
160 clock_gettime(CLOCK_MONOTONIC, &t);
161 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
162}
163
164void Context::timerReset()
165{
166 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
167 mTimers[ct] = 0;
168 }
169}
170
171void Context::timerInit()
172{
173 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700174 mTimeFrame = mTimeLast;
175 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700176 mTimerActive = RS_TIMER_INTERNAL;
177 timerReset();
178}
179
Jason Sams1d54f102009-09-03 15:43:13 -0700180void Context::timerFrame()
181{
182 mTimeLastFrame = mTimeFrame;
183 mTimeFrame = getTime();
184}
185
Jason Sams24371d92009-08-19 12:17:14 -0700186void Context::timerSet(Timers tm)
187{
188 uint64_t last = mTimeLast;
189 mTimeLast = getTime();
190 mTimers[mTimerActive] += mTimeLast - last;
191 mTimerActive = tm;
192}
193
194void Context::timerPrint()
195{
196 double total = 0;
197 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
198 total += mTimers[ct];
199 }
Jason Sams1d54f102009-09-03 15:43:13 -0700200 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700201
Jason Sams1d54f102009-09-03 15:43:13 -0700202 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
203 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700204 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700205 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
206 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
207 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700208}
209
Jason Sams326e0dd2009-05-22 14:03:28 -0700210void Context::setupCheck()
211{
Jason Sams5fd09d82009-09-23 13:57:02 -0700212 mFragmentStore->setupGL(this, &mStateFragmentStore);
213 mFragment->setupGL(this, &mStateFragment);
214 mRaster->setupGL(this, &mStateRaster);
215 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700216}
217
Jason Sams1fddd902009-09-25 15:25:00 -0700218static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700219{
220 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700221 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700222 return 0 != strcmp(buf, "0");
223}
Jason Sams326e0dd2009-05-22 14:03:28 -0700224
225void * Context::threadProc(void *vrsc)
226{
227 Context *rsc = static_cast<Context *>(vrsc);
228
Jason Sams1fddd902009-09-25 15:25:00 -0700229 rsc->props.mLogTimes = getProp("debug.rs.profile");
230 rsc->props.mLogScripts = getProp("debug.rs.script");
231 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700232
Jason Sams326e0dd2009-05-22 14:03:28 -0700233 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700234
Jason Samse5769102009-06-19 16:03:18 -0700235 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
236 if (!tlsStruct) {
237 LOGE("Error allocating tls storage");
238 return NULL;
239 }
240 tlsStruct->mContext = rsc;
241 tlsStruct->mScript = NULL;
242 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
243 if (status) {
244 LOGE("pthread_setspecific %i", status);
245 }
246
Jason Sams5fd09d82009-09-23 13:57:02 -0700247 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
248 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700249 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700250 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700251 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700252 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700253 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700254 rsc->setFragmentStore(NULL);
255
Jason Sams326e0dd2009-05-22 14:03:28 -0700256 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700257 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700258 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700259 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700260 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
Jason Sams732f1c02009-06-18 16:58:42 -0700262 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700263 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700264 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700265 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
266 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700267 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700268 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700269 rsc->timerFrame();
270 rsc->timerSet(RS_TIMER_INTERNAL);
271 rsc->timerPrint();
272 rsc->timerReset();
273 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700274 }
Jason Sams24371d92009-08-19 12:17:14 -0700275 if (rsc->mObjDestroy.mNeedToEmpty) {
276 rsc->objDestroyOOBRun();
277 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700278 }
279
Jason Sams8c0ee652009-08-25 14:49:07 -0700280 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700281 rsc->mRaster.clear();
282 rsc->mFragment.clear();
283 rsc->mVertex.clear();
284 rsc->mFragmentStore.clear();
285 rsc->mRootScript.clear();
286 rsc->mStateRaster.deinit(rsc);
287 rsc->mStateVertex.deinit(rsc);
288 rsc->mStateFragment.deinit(rsc);
289 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700290 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700291
Jason Sams326e0dd2009-05-22 14:03:28 -0700292 glClearColor(0,0,0,0);
293 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700294 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
295 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700296 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700297 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700298 return NULL;
299}
300
Jason Samsafcb25c2009-08-25 11:34:49 -0700301Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700302{
Jason Sams326e0dd2009-05-22 14:03:28 -0700303 dev->addContext(this);
304 mDev = dev;
305 mRunning = false;
306 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700307 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700308 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700309 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700310
Jason Samsa658e902009-06-04 14:35:01 -0700311 int status;
312 pthread_attr_t threadAttr;
313
Jason Sams9e4e13d2009-10-06 17:16:55 -0700314 if (!gThreadTLSKey) {
315 status = pthread_key_create(&gThreadTLSKey, NULL);
316 if (status) {
317 LOGE("Failed to init thread tls key.");
318 return;
319 }
320 } else {
321 // HACK: workaround gl hang on start
322 exit(-1);
Jason Samse5769102009-06-19 16:03:18 -0700323 }
324
Jason Samsa658e902009-06-04 14:35:01 -0700325 status = pthread_attr_init(&threadAttr);
326 if (status) {
327 LOGE("Failed to init thread attribute.");
328 return;
329 }
330
331 sched_param sparam;
332 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
333 pthread_attr_setschedparam(&threadAttr, &sparam);
334
Jason Sams992a0b72009-06-23 12:22:47 -0700335 mWndSurface = sur;
336
Jason Sams50869382009-08-18 17:07:09 -0700337 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700338 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700339 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700340
Jason Sams992a0b72009-06-23 12:22:47 -0700341 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700342 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700343 if (status) {
344 LOGE("Failed to start rs context thread.");
345 }
346
Jason Sams326e0dd2009-05-22 14:03:28 -0700347 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700348 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700350
Jason Samsa658e902009-06-04 14:35:01 -0700351 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700352}
353
354Context::~Context()
355{
Jason Sams8c0ee652009-08-25 14:49:07 -0700356 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700357 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700358 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700359 void *res;
360
Jason Sams8c0ee652009-08-25 14:49:07 -0700361 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700362 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700363 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700364
365 if (mDev) {
366 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700367 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700368 }
Jason Sams50869382009-08-18 17:07:09 -0700369
370 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700371}
372
Jason Sams86f1b232009-09-24 17:38:20 -0700373void Context::pause()
374{
375 mPaused = true;
376}
377
378void Context::resume()
379{
380 mPaused = false;
381}
382
Jason Sams326e0dd2009-05-22 14:03:28 -0700383void Context::setRootScript(Script *s)
384{
385 mRootScript.set(s);
386}
387
388void Context::setFragmentStore(ProgramFragmentStore *pfs)
389{
Jason Sams8ce125b2009-06-17 16:52:59 -0700390 if (pfs == NULL) {
391 mFragmentStore.set(mStateFragmentStore.mDefault);
392 } else {
393 mFragmentStore.set(pfs);
394 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700395}
396
397void Context::setFragment(ProgramFragment *pf)
398{
Jason Sams8ce125b2009-06-17 16:52:59 -0700399 if (pf == NULL) {
400 mFragment.set(mStateFragment.mDefault);
401 } else {
402 mFragment.set(pf);
403 }
Jason Samscfb1d112009-08-05 13:57:03 -0700404}
405
Jason Sams5fd09d82009-09-23 13:57:02 -0700406void Context::setRaster(ProgramRaster *pr)
407{
408 if (pr == NULL) {
409 mRaster.set(mStateRaster.mDefault);
410 } else {
411 mRaster.set(pr);
412 }
413}
414
Jason Samscfb1d112009-08-05 13:57:03 -0700415void Context::allocationCheck(const Allocation *a)
416{
417 mVertex->checkUpdatedAllocation(a);
418 mFragment->checkUpdatedAllocation(a);
419 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700420}
421
422void Context::setVertex(ProgramVertex *pv)
423{
Jason Sams8ce125b2009-06-17 16:52:59 -0700424 if (pv == NULL) {
425 mVertex.set(mStateVertex.mDefault);
426 } else {
427 mVertex.set(pv);
428 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700429}
430
Jason Samsa4a54e42009-06-10 18:39:40 -0700431void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700432{
433 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700434 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700435 mNames.add(obj);
436}
437
438void Context::removeName(ObjectBase *obj)
439{
440 for(size_t ct=0; ct < mNames.size(); ct++) {
441 if (obj == mNames[ct]) {
442 mNames.removeAt(ct);
443 return;
444 }
445 }
446}
447
448ObjectBase * Context::lookupName(const char *name) const
449{
450 for(size_t ct=0; ct < mNames.size(); ct++) {
451 if (!strcmp(name, mNames[ct]->getName())) {
452 return mNames[ct];
453 }
454 }
455 return NULL;
456}
457
Jason Samsa4a54e42009-06-10 18:39:40 -0700458void Context::appendNameDefines(String8 *str) const
459{
460 char buf[256];
461 for (size_t ct=0; ct < mNames.size(); ct++) {
462 str->append("#define NAMED_");
463 str->append(mNames[ct]->getName());
464 str->append(" ");
465 sprintf(buf, "%i\n", (int)mNames[ct]);
466 str->append(buf);
467 }
468}
469
Joe Onorato57b79ce2009-08-09 22:57:44 -0700470void Context::appendVarDefines(String8 *str) const
471{
472 char buf[256];
473 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
474 str->append("#define ");
475 str->append(mInt32Defines.keyAt(ct));
476 str->append(" ");
477 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
478 str->append(buf);
479
480 }
481 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
482 str->append("#define ");
483 str->append(mFloatDefines.keyAt(ct));
484 str->append(" ");
485 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
486 str->append(buf);
487 }
488}
489
Jason Sams50869382009-08-18 17:07:09 -0700490bool Context::objDestroyOOBInit()
491{
492 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
493 if (status) {
494 LOGE("Context::ObjDestroyOOBInit mutex init failure");
495 return false;
496 }
497 return true;
498}
499
500void Context::objDestroyOOBRun()
501{
502 if (mObjDestroy.mNeedToEmpty) {
503 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
504 if (status) {
505 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
506 return;
507 }
508
509 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700510 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700511 }
512 mObjDestroy.mDestroyList.clear();
513 mObjDestroy.mNeedToEmpty = false;
514
515 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
516 if (status) {
517 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
518 }
519 }
520}
521
522void Context::objDestroyOOBDestroy()
523{
524 rsAssert(!mObjDestroy.mNeedToEmpty);
525 pthread_mutex_destroy(&mObjDestroy.mMutex);
526}
527
528void Context::objDestroyAdd(ObjectBase *obj)
529{
530 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
531 if (status) {
532 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
533 return;
534 }
535
536 mObjDestroy.mNeedToEmpty = true;
537 mObjDestroy.mDestroyList.add(obj);
538
539 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
540 if (status) {
541 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
542 }
543}
544
Jason Sams8c401ef2009-10-06 13:58:47 -0700545uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
546{
547 //LOGE("getMessageToClient %i %i", bufferLen, wait);
548 if (!wait) {
549 if (mIO.mToClient.isEmpty()) {
550 // No message to get and not going to wait for one.
551 receiveLen = 0;
552 return 0;
553 }
554 }
555
556 //LOGE("getMessageToClient 2 con=%p", this);
557 uint32_t bytesData = 0;
558 uint32_t commandID = 0;
559 const void *d = mIO.mToClient.get(&commandID, &bytesData);
560 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
561
562 *receiveLen = bytesData;
563 if (bufferLen >= bytesData) {
564 memcpy(data, d, bytesData);
565 mIO.mToClient.next();
566 return commandID;
567 }
568 return 0;
569}
570
571bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
572{
573 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
574 if (cmdID == 0) {
575 LOGE("Attempting to send invalid command 0 to client.");
576 return false;
577 }
578 if (!waitForSpace) {
579 if (mIO.mToClient.getFreeSpace() < len) {
580 // Not enough room, and not waiting.
581 return false;
582 }
583 }
584 //LOGE("sendMessageToClient 2");
585 void *p = mIO.mToClient.reserve(len);
586 memcpy(p, data, len);
587 mIO.mToClient.commit(cmdID, len);
588 //LOGE("sendMessageToClient 3");
589 return true;
590}
591
592void Context::initToClient()
593{
594 while(!mRunning) {
595 usleep(100);
596 }
597}
598
599void Context::deinitToClient()
600{
601 mIO.mToClient.shutdown();
602}
Jason Sams50869382009-08-18 17:07:09 -0700603
Jason Samsa4a54e42009-06-10 18:39:40 -0700604
Jason Sams326e0dd2009-05-22 14:03:28 -0700605///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700606//
Jason Sams326e0dd2009-05-22 14:03:28 -0700607
608namespace android {
609namespace renderscript {
610
611
612void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
613{
614 Script *s = static_cast<Script *>(vs);
615 rsc->setRootScript(s);
616}
617
618void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
619{
620 Sampler *s = static_cast<Sampler *>(vs);
621
622 if (slot > RS_MAX_SAMPLER_SLOT) {
623 LOGE("Invalid sampler slot");
624 return;
625 }
626
627 s->bindToContext(&rsc->mStateSampler, slot);
628}
629
630void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
631{
632 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
633 rsc->setFragmentStore(pfs);
634}
635
636void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
637{
638 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
639 rsc->setFragment(pf);
640}
641
Jason Sams5fd09d82009-09-23 13:57:02 -0700642void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
643{
644 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
645 rsc->setRaster(pr);
646}
647
Jason Sams326e0dd2009-05-22 14:03:28 -0700648void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
649{
650 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
651 rsc->setVertex(pv);
652}
653
Jason Samsa4a54e42009-06-10 18:39:40 -0700654void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700655{
656 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700657 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700658}
Jason Sams326e0dd2009-05-22 14:03:28 -0700659
Jason Sams707aaf32009-08-18 14:14:24 -0700660void rsi_ObjDestroy(Context *rsc, void *obj)
661{
662 ObjectBase *ob = static_cast<ObjectBase *>(obj);
663 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700664 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700665}
666
Joe Onorato57b79ce2009-08-09 22:57:44 -0700667void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
668{
669 rsc->addInt32Define(name, value);
670}
671
672void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
673{
674 rsc->addFloatDefine(name, value);
675}
Jason Sams326e0dd2009-05-22 14:03:28 -0700676
Jason Sams86f1b232009-09-24 17:38:20 -0700677void rsi_ContextPause(Context *rsc)
678{
679 rsc->pause();
680}
681
682void rsi_ContextResume(Context *rsc)
683{
684 rsc->resume();
685}
686
Jason Sams326e0dd2009-05-22 14:03:28 -0700687}
688}
689
690
Jason Samsafcb25c2009-08-25 11:34:49 -0700691RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700692{
693 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700694 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700695 return rsc;
696}
697
698void rsContextDestroy(RsContext vrsc)
699{
700 Context * rsc = static_cast<Context *>(vrsc);
701 delete rsc;
702}
703
Jason Sams50869382009-08-18 17:07:09 -0700704void rsObjDestroyOOB(RsContext vrsc, void *obj)
705{
706 Context * rsc = static_cast<Context *>(vrsc);
707 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
708}
709
Jason Sams8c401ef2009-10-06 13:58:47 -0700710uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
711{
712 Context * rsc = static_cast<Context *>(vrsc);
713 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
714}
715
716void rsContextInitToClient(RsContext vrsc)
717{
718 Context * rsc = static_cast<Context *>(vrsc);
719 rsc->initToClient();
720}
721
722void rsContextDeinitToClient(RsContext vrsc)
723{
724 Context * rsc = static_cast<Context *>(vrsc);
725 rsc->deinitToClient();
726}
727