blob: 195ea6f1d7d5ca4cfcafc6915a3e045e579daa01 [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 Samsafcb25c2009-08-25 11:34:49 -070060 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
61 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
62
63 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
64 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070065 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070066 }
67 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
68
69 if (mWndSurface) {
70 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
71 } else {
72 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
73 android_createDisplaySurface(),
74 NULL);
75 }
76
77 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
78 eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
79 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
80 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
81
82
83 mGL.mVersion = glGetString(GL_VERSION);
84 mGL.mVendor = glGetString(GL_VENDOR);
85 mGL.mRenderer = glGetString(GL_RENDERER);
86 mGL.mExtensions = glGetString(GL_EXTENSIONS);
87
Jason Sams9397e302009-08-27 20:23:34 -070088 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
89 LOGV("GL Version %s", mGL.mVersion);
90 LOGV("GL Vendor %s", mGL.mVendor);
91 LOGV("GL Renderer %s", mGL.mRenderer);
92 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -070093
Jason Sams306fb232009-08-25 17:09:59 -070094 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -070095 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -070096 } else {
97 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -070098 }
Jason Sams326e0dd2009-05-22 14:03:28 -070099}
100
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700101bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700102{
103 ObjectBaseRef<ProgramFragment> frag(mFragment);
104 ObjectBaseRef<ProgramVertex> vtx(mVertex);
105 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700106 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700107
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700108 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700109
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700110 mFragment.set(frag);
111 mVertex.set(vtx);
112 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700113 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700114 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700115}
116
117
Jason Samsa44cb292009-06-04 17:58:03 -0700118bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700119{
Jason Sams1fddd902009-09-25 15:25:00 -0700120 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700121 timerSet(RS_TIMER_CLEAR_SWAP);
122 }
Jason Sams10308932009-06-09 12:15:30 -0700123 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700124
Jason Sams8c9534b2009-09-22 12:26:53 -0700125 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
126 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700127 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
129
Jason Sams928b7342009-06-08 18:50:13 -0700130 glClearColor(mRootScript->mEnviroment.mClearColor[0],
131 mRootScript->mEnviroment.mClearColor[1],
132 mRootScript->mEnviroment.mClearColor[2],
133 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700134 if (mUseDepth) {
135 glDepthMask(GL_TRUE);
136 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
137 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
138 } else {
139 glClear(GL_COLOR_BUFFER_BIT);
140 }
Jason Sams306fb232009-08-25 17:09:59 -0700141
Jason Sams1fddd902009-09-25 15:25:00 -0700142 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700143 timerSet(RS_TIMER_SCRIPT);
144 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700145 mStateFragmentStore.mLast.clear();
Jason Samscfb1d112009-08-05 13:57:03 -0700146 bool ret = runScript(mRootScript.get(), 0);
Jason Samscfb1d112009-08-05 13:57:03 -0700147 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700148}
149
Jason Sams24371d92009-08-19 12:17:14 -0700150uint64_t Context::getTime() const
151{
152 struct timespec t;
153 clock_gettime(CLOCK_MONOTONIC, &t);
154 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
155}
156
157void Context::timerReset()
158{
159 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
160 mTimers[ct] = 0;
161 }
162}
163
164void Context::timerInit()
165{
166 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700167 mTimeFrame = mTimeLast;
168 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700169 mTimerActive = RS_TIMER_INTERNAL;
170 timerReset();
171}
172
Jason Sams1d54f102009-09-03 15:43:13 -0700173void Context::timerFrame()
174{
175 mTimeLastFrame = mTimeFrame;
176 mTimeFrame = getTime();
177}
178
Jason Sams24371d92009-08-19 12:17:14 -0700179void Context::timerSet(Timers tm)
180{
181 uint64_t last = mTimeLast;
182 mTimeLast = getTime();
183 mTimers[mTimerActive] += mTimeLast - last;
184 mTimerActive = tm;
185}
186
187void Context::timerPrint()
188{
189 double total = 0;
190 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
191 total += mTimers[ct];
192 }
Jason Sams1d54f102009-09-03 15:43:13 -0700193 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700194
Jason Sams1d54f102009-09-03 15:43:13 -0700195 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
196 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700197 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700198 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
199 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
200 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700201}
202
Jason Sams326e0dd2009-05-22 14:03:28 -0700203void Context::setupCheck()
204{
Jason Sams5fd09d82009-09-23 13:57:02 -0700205 mFragmentStore->setupGL(this, &mStateFragmentStore);
206 mFragment->setupGL(this, &mStateFragment);
207 mRaster->setupGL(this, &mStateRaster);
208 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700209}
210
Jason Sams1fddd902009-09-25 15:25:00 -0700211static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700212{
213 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700214 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700215 return 0 != strcmp(buf, "0");
216}
Jason Sams326e0dd2009-05-22 14:03:28 -0700217
218void * Context::threadProc(void *vrsc)
219{
220 Context *rsc = static_cast<Context *>(vrsc);
221
Jason Sams1fddd902009-09-25 15:25:00 -0700222 rsc->props.mLogTimes = getProp("debug.rs.profile");
223 rsc->props.mLogScripts = getProp("debug.rs.script");
224 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700225
Jason Sams326e0dd2009-05-22 14:03:28 -0700226 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700227
Jason Samse5769102009-06-19 16:03:18 -0700228 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
229 if (!tlsStruct) {
230 LOGE("Error allocating tls storage");
231 return NULL;
232 }
233 tlsStruct->mContext = rsc;
234 tlsStruct->mScript = NULL;
235 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
236 if (status) {
237 LOGE("pthread_setspecific %i", status);
238 }
239
Jason Sams5fd09d82009-09-23 13:57:02 -0700240 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
241 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700242 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700243 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700244 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700245 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700246 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700247 rsc->setFragmentStore(NULL);
248
Jason Sams326e0dd2009-05-22 14:03:28 -0700249 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700250 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700251 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700252 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700253 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700254
Jason Sams732f1c02009-06-18 16:58:42 -0700255 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700256 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700257 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700258 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
259 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700260 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700261 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700262 rsc->timerFrame();
263 rsc->timerSet(RS_TIMER_INTERNAL);
264 rsc->timerPrint();
265 rsc->timerReset();
266 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700267 }
Jason Sams24371d92009-08-19 12:17:14 -0700268 if (rsc->mObjDestroy.mNeedToEmpty) {
269 rsc->objDestroyOOBRun();
270 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700271 }
272
Jason Sams8c0ee652009-08-25 14:49:07 -0700273 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700274 rsc->mRaster.clear();
275 rsc->mFragment.clear();
276 rsc->mVertex.clear();
277 rsc->mFragmentStore.clear();
278 rsc->mRootScript.clear();
279 rsc->mStateRaster.deinit(rsc);
280 rsc->mStateVertex.deinit(rsc);
281 rsc->mStateFragment.deinit(rsc);
282 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700283 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700284
Jason Sams326e0dd2009-05-22 14:03:28 -0700285 glClearColor(0,0,0,0);
286 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700287 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
288 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700289 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700290 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700291 return NULL;
292}
293
Jason Samsafcb25c2009-08-25 11:34:49 -0700294Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700295{
Jason Sams326e0dd2009-05-22 14:03:28 -0700296 dev->addContext(this);
297 mDev = dev;
298 mRunning = false;
299 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700300 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700301 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700302 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700303
Jason Samsa658e902009-06-04 14:35:01 -0700304 int status;
305 pthread_attr_t threadAttr;
306
Jason Sams9e4e13d2009-10-06 17:16:55 -0700307 if (!gThreadTLSKey) {
308 status = pthread_key_create(&gThreadTLSKey, NULL);
309 if (status) {
310 LOGE("Failed to init thread tls key.");
311 return;
312 }
313 } else {
314 // HACK: workaround gl hang on start
315 exit(-1);
Jason Samse5769102009-06-19 16:03:18 -0700316 }
317
Jason Samsa658e902009-06-04 14:35:01 -0700318 status = pthread_attr_init(&threadAttr);
319 if (status) {
320 LOGE("Failed to init thread attribute.");
321 return;
322 }
323
324 sched_param sparam;
325 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
326 pthread_attr_setschedparam(&threadAttr, &sparam);
327
Jason Sams992a0b72009-06-23 12:22:47 -0700328 mWndSurface = sur;
329
Jason Sams50869382009-08-18 17:07:09 -0700330 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700331 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700332 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700333
Jason Sams992a0b72009-06-23 12:22:47 -0700334 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700335 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700336 if (status) {
337 LOGE("Failed to start rs context thread.");
338 }
339
Jason Sams326e0dd2009-05-22 14:03:28 -0700340 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700341 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700342 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700343
Jason Samsa658e902009-06-04 14:35:01 -0700344 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700345}
346
347Context::~Context()
348{
Jason Sams8c0ee652009-08-25 14:49:07 -0700349 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700350 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700351 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700352 void *res;
353
Jason Sams8c0ee652009-08-25 14:49:07 -0700354 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700355 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700356 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700357
358 if (mDev) {
359 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700360 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700361 }
Jason Sams50869382009-08-18 17:07:09 -0700362
363 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700364}
365
Jason Sams86f1b232009-09-24 17:38:20 -0700366void Context::pause()
367{
368 mPaused = true;
369}
370
371void Context::resume()
372{
373 mPaused = false;
374}
375
Jason Sams326e0dd2009-05-22 14:03:28 -0700376void Context::setRootScript(Script *s)
377{
378 mRootScript.set(s);
379}
380
381void Context::setFragmentStore(ProgramFragmentStore *pfs)
382{
Jason Sams8ce125b2009-06-17 16:52:59 -0700383 if (pfs == NULL) {
384 mFragmentStore.set(mStateFragmentStore.mDefault);
385 } else {
386 mFragmentStore.set(pfs);
387 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700388}
389
390void Context::setFragment(ProgramFragment *pf)
391{
Jason Sams8ce125b2009-06-17 16:52:59 -0700392 if (pf == NULL) {
393 mFragment.set(mStateFragment.mDefault);
394 } else {
395 mFragment.set(pf);
396 }
Jason Samscfb1d112009-08-05 13:57:03 -0700397}
398
Jason Sams5fd09d82009-09-23 13:57:02 -0700399void Context::setRaster(ProgramRaster *pr)
400{
401 if (pr == NULL) {
402 mRaster.set(mStateRaster.mDefault);
403 } else {
404 mRaster.set(pr);
405 }
406}
407
Jason Samscfb1d112009-08-05 13:57:03 -0700408void Context::allocationCheck(const Allocation *a)
409{
410 mVertex->checkUpdatedAllocation(a);
411 mFragment->checkUpdatedAllocation(a);
412 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700413}
414
415void Context::setVertex(ProgramVertex *pv)
416{
Jason Sams8ce125b2009-06-17 16:52:59 -0700417 if (pv == NULL) {
418 mVertex.set(mStateVertex.mDefault);
419 } else {
420 mVertex.set(pv);
421 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700422}
423
Jason Samsa4a54e42009-06-10 18:39:40 -0700424void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700425{
426 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700427 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700428 mNames.add(obj);
429}
430
431void Context::removeName(ObjectBase *obj)
432{
433 for(size_t ct=0; ct < mNames.size(); ct++) {
434 if (obj == mNames[ct]) {
435 mNames.removeAt(ct);
436 return;
437 }
438 }
439}
440
441ObjectBase * Context::lookupName(const char *name) const
442{
443 for(size_t ct=0; ct < mNames.size(); ct++) {
444 if (!strcmp(name, mNames[ct]->getName())) {
445 return mNames[ct];
446 }
447 }
448 return NULL;
449}
450
Jason Samsa4a54e42009-06-10 18:39:40 -0700451void Context::appendNameDefines(String8 *str) const
452{
453 char buf[256];
454 for (size_t ct=0; ct < mNames.size(); ct++) {
455 str->append("#define NAMED_");
456 str->append(mNames[ct]->getName());
457 str->append(" ");
458 sprintf(buf, "%i\n", (int)mNames[ct]);
459 str->append(buf);
460 }
461}
462
Joe Onorato57b79ce2009-08-09 22:57:44 -0700463void Context::appendVarDefines(String8 *str) const
464{
465 char buf[256];
466 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
467 str->append("#define ");
468 str->append(mInt32Defines.keyAt(ct));
469 str->append(" ");
470 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
471 str->append(buf);
472
473 }
474 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
475 str->append("#define ");
476 str->append(mFloatDefines.keyAt(ct));
477 str->append(" ");
478 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
479 str->append(buf);
480 }
481}
482
Jason Sams50869382009-08-18 17:07:09 -0700483bool Context::objDestroyOOBInit()
484{
485 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
486 if (status) {
487 LOGE("Context::ObjDestroyOOBInit mutex init failure");
488 return false;
489 }
490 return true;
491}
492
493void Context::objDestroyOOBRun()
494{
495 if (mObjDestroy.mNeedToEmpty) {
496 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
497 if (status) {
498 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
499 return;
500 }
501
502 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700503 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700504 }
505 mObjDestroy.mDestroyList.clear();
506 mObjDestroy.mNeedToEmpty = false;
507
508 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
509 if (status) {
510 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
511 }
512 }
513}
514
515void Context::objDestroyOOBDestroy()
516{
517 rsAssert(!mObjDestroy.mNeedToEmpty);
518 pthread_mutex_destroy(&mObjDestroy.mMutex);
519}
520
521void Context::objDestroyAdd(ObjectBase *obj)
522{
523 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
524 if (status) {
525 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
526 return;
527 }
528
529 mObjDestroy.mNeedToEmpty = true;
530 mObjDestroy.mDestroyList.add(obj);
531
532 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
533 if (status) {
534 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
535 }
536}
537
Jason Sams8c401ef2009-10-06 13:58:47 -0700538uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
539{
540 //LOGE("getMessageToClient %i %i", bufferLen, wait);
541 if (!wait) {
542 if (mIO.mToClient.isEmpty()) {
543 // No message to get and not going to wait for one.
544 receiveLen = 0;
545 return 0;
546 }
547 }
548
549 //LOGE("getMessageToClient 2 con=%p", this);
550 uint32_t bytesData = 0;
551 uint32_t commandID = 0;
552 const void *d = mIO.mToClient.get(&commandID, &bytesData);
553 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
554
555 *receiveLen = bytesData;
556 if (bufferLen >= bytesData) {
557 memcpy(data, d, bytesData);
558 mIO.mToClient.next();
559 return commandID;
560 }
561 return 0;
562}
563
564bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
565{
566 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
567 if (cmdID == 0) {
568 LOGE("Attempting to send invalid command 0 to client.");
569 return false;
570 }
571 if (!waitForSpace) {
572 if (mIO.mToClient.getFreeSpace() < len) {
573 // Not enough room, and not waiting.
574 return false;
575 }
576 }
577 //LOGE("sendMessageToClient 2");
578 void *p = mIO.mToClient.reserve(len);
579 memcpy(p, data, len);
580 mIO.mToClient.commit(cmdID, len);
581 //LOGE("sendMessageToClient 3");
582 return true;
583}
584
585void Context::initToClient()
586{
587 while(!mRunning) {
588 usleep(100);
589 }
590}
591
592void Context::deinitToClient()
593{
594 mIO.mToClient.shutdown();
595}
Jason Sams50869382009-08-18 17:07:09 -0700596
Jason Samsa4a54e42009-06-10 18:39:40 -0700597
Jason Sams326e0dd2009-05-22 14:03:28 -0700598///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700599//
Jason Sams326e0dd2009-05-22 14:03:28 -0700600
601namespace android {
602namespace renderscript {
603
604
605void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
606{
607 Script *s = static_cast<Script *>(vs);
608 rsc->setRootScript(s);
609}
610
611void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
612{
613 Sampler *s = static_cast<Sampler *>(vs);
614
615 if (slot > RS_MAX_SAMPLER_SLOT) {
616 LOGE("Invalid sampler slot");
617 return;
618 }
619
620 s->bindToContext(&rsc->mStateSampler, slot);
621}
622
623void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
624{
625 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
626 rsc->setFragmentStore(pfs);
627}
628
629void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
630{
631 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
632 rsc->setFragment(pf);
633}
634
Jason Sams5fd09d82009-09-23 13:57:02 -0700635void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
636{
637 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
638 rsc->setRaster(pr);
639}
640
Jason Sams326e0dd2009-05-22 14:03:28 -0700641void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
642{
643 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
644 rsc->setVertex(pv);
645}
646
Jason Samsa4a54e42009-06-10 18:39:40 -0700647void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700648{
649 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700650 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700651}
Jason Sams326e0dd2009-05-22 14:03:28 -0700652
Jason Sams707aaf32009-08-18 14:14:24 -0700653void rsi_ObjDestroy(Context *rsc, void *obj)
654{
655 ObjectBase *ob = static_cast<ObjectBase *>(obj);
656 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700657 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700658}
659
Joe Onorato57b79ce2009-08-09 22:57:44 -0700660void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
661{
662 rsc->addInt32Define(name, value);
663}
664
665void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
666{
667 rsc->addFloatDefine(name, value);
668}
Jason Sams326e0dd2009-05-22 14:03:28 -0700669
Jason Sams86f1b232009-09-24 17:38:20 -0700670void rsi_ContextPause(Context *rsc)
671{
672 rsc->pause();
673}
674
675void rsi_ContextResume(Context *rsc)
676{
677 rsc->resume();
678}
679
Jason Sams326e0dd2009-05-22 14:03:28 -0700680}
681}
682
683
Jason Samsafcb25c2009-08-25 11:34:49 -0700684RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700685{
686 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700687 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700688 return rsc;
689}
690
691void rsContextDestroy(RsContext vrsc)
692{
693 Context * rsc = static_cast<Context *>(vrsc);
694 delete rsc;
695}
696
Jason Sams50869382009-08-18 17:07:09 -0700697void rsObjDestroyOOB(RsContext vrsc, void *obj)
698{
699 Context * rsc = static_cast<Context *>(vrsc);
700 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
701}
702
Jason Sams8c401ef2009-10-06 13:58:47 -0700703uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
704{
705 Context * rsc = static_cast<Context *>(vrsc);
706 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
707}
708
709void rsContextInitToClient(RsContext vrsc)
710{
711 Context * rsc = static_cast<Context *>(vrsc);
712 rsc->initToClient();
713}
714
715void rsContextDeinitToClient(RsContext vrsc)
716{
717 Context * rsc = static_cast<Context *>(vrsc);
718 rsc->deinitToClient();
719}
720