blob: 33ed0ca0a386232e8298b371c086ed81b56be3fe [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 Samscfb1d112009-08-05 13:57:03 -0700148 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700149}
150
Jason Sams24371d92009-08-19 12:17:14 -0700151uint64_t Context::getTime() const
152{
153 struct timespec t;
154 clock_gettime(CLOCK_MONOTONIC, &t);
155 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
156}
157
158void Context::timerReset()
159{
160 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
161 mTimers[ct] = 0;
162 }
163}
164
165void Context::timerInit()
166{
167 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700168 mTimeFrame = mTimeLast;
169 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700170 mTimerActive = RS_TIMER_INTERNAL;
171 timerReset();
172}
173
Jason Sams1d54f102009-09-03 15:43:13 -0700174void Context::timerFrame()
175{
176 mTimeLastFrame = mTimeFrame;
177 mTimeFrame = getTime();
178}
179
Jason Sams24371d92009-08-19 12:17:14 -0700180void Context::timerSet(Timers tm)
181{
182 uint64_t last = mTimeLast;
183 mTimeLast = getTime();
184 mTimers[mTimerActive] += mTimeLast - last;
185 mTimerActive = tm;
186}
187
188void Context::timerPrint()
189{
190 double total = 0;
191 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
192 total += mTimers[ct];
193 }
Jason Sams1d54f102009-09-03 15:43:13 -0700194 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700195
Jason Sams1d54f102009-09-03 15:43:13 -0700196 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
197 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700198 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700199 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
200 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
201 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700202}
203
Jason Sams326e0dd2009-05-22 14:03:28 -0700204void Context::setupCheck()
205{
Jason Sams5fd09d82009-09-23 13:57:02 -0700206 mFragmentStore->setupGL(this, &mStateFragmentStore);
207 mFragment->setupGL(this, &mStateFragment);
208 mRaster->setupGL(this, &mStateRaster);
209 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700210}
211
Jason Sams1fddd902009-09-25 15:25:00 -0700212static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700213{
214 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700215 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700216 return 0 != strcmp(buf, "0");
217}
Jason Sams326e0dd2009-05-22 14:03:28 -0700218
219void * Context::threadProc(void *vrsc)
220{
221 Context *rsc = static_cast<Context *>(vrsc);
222
Jason Sams1fddd902009-09-25 15:25:00 -0700223 rsc->props.mLogTimes = getProp("debug.rs.profile");
224 rsc->props.mLogScripts = getProp("debug.rs.script");
225 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700226
Jason Sams326e0dd2009-05-22 14:03:28 -0700227 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700228
Jason Samse5769102009-06-19 16:03:18 -0700229 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
230 if (!tlsStruct) {
231 LOGE("Error allocating tls storage");
232 return NULL;
233 }
234 tlsStruct->mContext = rsc;
235 tlsStruct->mScript = NULL;
236 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
237 if (status) {
238 LOGE("pthread_setspecific %i", status);
239 }
240
Jason Sams5fd09d82009-09-23 13:57:02 -0700241 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
242 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700243 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700244 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700245 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700246 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700247 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700248 rsc->setFragmentStore(NULL);
249
Jason Sams326e0dd2009-05-22 14:03:28 -0700250 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700251 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700252 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700253 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700254 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700255
Jason Sams732f1c02009-06-18 16:58:42 -0700256 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700257 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700258 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
260 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700261 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700262 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700263 rsc->timerFrame();
264 rsc->timerSet(RS_TIMER_INTERNAL);
265 rsc->timerPrint();
266 rsc->timerReset();
267 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700268 }
Jason Sams24371d92009-08-19 12:17:14 -0700269 if (rsc->mObjDestroy.mNeedToEmpty) {
270 rsc->objDestroyOOBRun();
271 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700272 }
273
Jason Sams8c0ee652009-08-25 14:49:07 -0700274 LOGV("RS Thread exiting");
Jason Samsf2649a92009-09-25 16:37:33 -0700275 rsc->mRaster.clear();
276 rsc->mFragment.clear();
277 rsc->mVertex.clear();
278 rsc->mFragmentStore.clear();
279 rsc->mRootScript.clear();
280 rsc->mStateRaster.deinit(rsc);
281 rsc->mStateVertex.deinit(rsc);
282 rsc->mStateFragment.deinit(rsc);
283 rsc->mStateFragmentStore.deinit(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700284 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700285
Jason Sams326e0dd2009-05-22 14:03:28 -0700286 glClearColor(0,0,0,0);
287 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700288 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
289 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700290 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700291 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700292 return NULL;
293}
294
Jason Samsafcb25c2009-08-25 11:34:49 -0700295Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700296{
Jason Sams326e0dd2009-05-22 14:03:28 -0700297 dev->addContext(this);
298 mDev = dev;
299 mRunning = false;
300 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700301 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700302 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700303 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700304
Jason Samsa658e902009-06-04 14:35:01 -0700305 int status;
306 pthread_attr_t threadAttr;
307
Jason Sams9e4e13d2009-10-06 17:16:55 -0700308 if (!gThreadTLSKey) {
309 status = pthread_key_create(&gThreadTLSKey, NULL);
310 if (status) {
311 LOGE("Failed to init thread tls key.");
312 return;
313 }
314 } else {
315 // HACK: workaround gl hang on start
316 exit(-1);
Jason Samse5769102009-06-19 16:03:18 -0700317 }
318
Jason Samsa658e902009-06-04 14:35:01 -0700319 status = pthread_attr_init(&threadAttr);
320 if (status) {
321 LOGE("Failed to init thread attribute.");
322 return;
323 }
324
325 sched_param sparam;
326 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
327 pthread_attr_setschedparam(&threadAttr, &sparam);
328
Jason Sams992a0b72009-06-23 12:22:47 -0700329 mWndSurface = sur;
330
Jason Sams50869382009-08-18 17:07:09 -0700331 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700332 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700333 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700334
Jason Sams992a0b72009-06-23 12:22:47 -0700335 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700336 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700337 if (status) {
338 LOGE("Failed to start rs context thread.");
339 }
340
Jason Sams326e0dd2009-05-22 14:03:28 -0700341 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700342 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700343 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700344
Jason Samsa658e902009-06-04 14:35:01 -0700345 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700346}
347
348Context::~Context()
349{
Jason Sams8c0ee652009-08-25 14:49:07 -0700350 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700351 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700352 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700353 void *res;
354
Jason Sams8c0ee652009-08-25 14:49:07 -0700355 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700356 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700357 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700358
359 if (mDev) {
360 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700361 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700362 }
Jason Sams50869382009-08-18 17:07:09 -0700363
364 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700365}
366
Jason Sams86f1b232009-09-24 17:38:20 -0700367void Context::pause()
368{
369 mPaused = true;
370}
371
372void Context::resume()
373{
374 mPaused = false;
375}
376
Jason Sams326e0dd2009-05-22 14:03:28 -0700377void Context::setRootScript(Script *s)
378{
379 mRootScript.set(s);
380}
381
382void Context::setFragmentStore(ProgramFragmentStore *pfs)
383{
Jason Sams8ce125b2009-06-17 16:52:59 -0700384 if (pfs == NULL) {
385 mFragmentStore.set(mStateFragmentStore.mDefault);
386 } else {
387 mFragmentStore.set(pfs);
388 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700389}
390
391void Context::setFragment(ProgramFragment *pf)
392{
Jason Sams8ce125b2009-06-17 16:52:59 -0700393 if (pf == NULL) {
394 mFragment.set(mStateFragment.mDefault);
395 } else {
396 mFragment.set(pf);
397 }
Jason Samscfb1d112009-08-05 13:57:03 -0700398}
399
Jason Sams5fd09d82009-09-23 13:57:02 -0700400void Context::setRaster(ProgramRaster *pr)
401{
402 if (pr == NULL) {
403 mRaster.set(mStateRaster.mDefault);
404 } else {
405 mRaster.set(pr);
406 }
407}
408
Jason Samscfb1d112009-08-05 13:57:03 -0700409void Context::allocationCheck(const Allocation *a)
410{
411 mVertex->checkUpdatedAllocation(a);
412 mFragment->checkUpdatedAllocation(a);
413 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700414}
415
416void Context::setVertex(ProgramVertex *pv)
417{
Jason Sams8ce125b2009-06-17 16:52:59 -0700418 if (pv == NULL) {
419 mVertex.set(mStateVertex.mDefault);
420 } else {
421 mVertex.set(pv);
422 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700423}
424
Jason Samsa4a54e42009-06-10 18:39:40 -0700425void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700426{
427 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700428 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700429 mNames.add(obj);
430}
431
432void Context::removeName(ObjectBase *obj)
433{
434 for(size_t ct=0; ct < mNames.size(); ct++) {
435 if (obj == mNames[ct]) {
436 mNames.removeAt(ct);
437 return;
438 }
439 }
440}
441
442ObjectBase * Context::lookupName(const char *name) const
443{
444 for(size_t ct=0; ct < mNames.size(); ct++) {
445 if (!strcmp(name, mNames[ct]->getName())) {
446 return mNames[ct];
447 }
448 }
449 return NULL;
450}
451
Jason Samsa4a54e42009-06-10 18:39:40 -0700452void Context::appendNameDefines(String8 *str) const
453{
454 char buf[256];
455 for (size_t ct=0; ct < mNames.size(); ct++) {
456 str->append("#define NAMED_");
457 str->append(mNames[ct]->getName());
458 str->append(" ");
459 sprintf(buf, "%i\n", (int)mNames[ct]);
460 str->append(buf);
461 }
462}
463
Joe Onorato57b79ce2009-08-09 22:57:44 -0700464void Context::appendVarDefines(String8 *str) const
465{
466 char buf[256];
467 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
468 str->append("#define ");
469 str->append(mInt32Defines.keyAt(ct));
470 str->append(" ");
471 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
472 str->append(buf);
473
474 }
475 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
476 str->append("#define ");
477 str->append(mFloatDefines.keyAt(ct));
478 str->append(" ");
479 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
480 str->append(buf);
481 }
482}
483
Jason Sams50869382009-08-18 17:07:09 -0700484bool Context::objDestroyOOBInit()
485{
486 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
487 if (status) {
488 LOGE("Context::ObjDestroyOOBInit mutex init failure");
489 return false;
490 }
491 return true;
492}
493
494void Context::objDestroyOOBRun()
495{
496 if (mObjDestroy.mNeedToEmpty) {
497 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
498 if (status) {
499 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
500 return;
501 }
502
503 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700504 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700505 }
506 mObjDestroy.mDestroyList.clear();
507 mObjDestroy.mNeedToEmpty = false;
508
509 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
510 if (status) {
511 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
512 }
513 }
514}
515
516void Context::objDestroyOOBDestroy()
517{
518 rsAssert(!mObjDestroy.mNeedToEmpty);
519 pthread_mutex_destroy(&mObjDestroy.mMutex);
520}
521
522void Context::objDestroyAdd(ObjectBase *obj)
523{
524 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
525 if (status) {
526 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
527 return;
528 }
529
530 mObjDestroy.mNeedToEmpty = true;
531 mObjDestroy.mDestroyList.add(obj);
532
533 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
534 if (status) {
535 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
536 }
537}
538
Jason Sams8c401ef2009-10-06 13:58:47 -0700539uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
540{
541 //LOGE("getMessageToClient %i %i", bufferLen, wait);
542 if (!wait) {
543 if (mIO.mToClient.isEmpty()) {
544 // No message to get and not going to wait for one.
545 receiveLen = 0;
546 return 0;
547 }
548 }
549
550 //LOGE("getMessageToClient 2 con=%p", this);
551 uint32_t bytesData = 0;
552 uint32_t commandID = 0;
553 const void *d = mIO.mToClient.get(&commandID, &bytesData);
554 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
555
556 *receiveLen = bytesData;
557 if (bufferLen >= bytesData) {
558 memcpy(data, d, bytesData);
559 mIO.mToClient.next();
560 return commandID;
561 }
562 return 0;
563}
564
565bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
566{
567 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
568 if (cmdID == 0) {
569 LOGE("Attempting to send invalid command 0 to client.");
570 return false;
571 }
572 if (!waitForSpace) {
573 if (mIO.mToClient.getFreeSpace() < len) {
574 // Not enough room, and not waiting.
575 return false;
576 }
577 }
578 //LOGE("sendMessageToClient 2");
579 void *p = mIO.mToClient.reserve(len);
580 memcpy(p, data, len);
581 mIO.mToClient.commit(cmdID, len);
582 //LOGE("sendMessageToClient 3");
583 return true;
584}
585
586void Context::initToClient()
587{
588 while(!mRunning) {
589 usleep(100);
590 }
591}
592
593void Context::deinitToClient()
594{
595 mIO.mToClient.shutdown();
596}
Jason Sams50869382009-08-18 17:07:09 -0700597
Jason Samsa4a54e42009-06-10 18:39:40 -0700598
Jason Sams326e0dd2009-05-22 14:03:28 -0700599///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700600//
Jason Sams326e0dd2009-05-22 14:03:28 -0700601
602namespace android {
603namespace renderscript {
604
605
606void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
607{
608 Script *s = static_cast<Script *>(vs);
609 rsc->setRootScript(s);
610}
611
612void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
613{
614 Sampler *s = static_cast<Sampler *>(vs);
615
616 if (slot > RS_MAX_SAMPLER_SLOT) {
617 LOGE("Invalid sampler slot");
618 return;
619 }
620
621 s->bindToContext(&rsc->mStateSampler, slot);
622}
623
624void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
625{
626 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
627 rsc->setFragmentStore(pfs);
628}
629
630void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
631{
632 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
633 rsc->setFragment(pf);
634}
635
Jason Sams5fd09d82009-09-23 13:57:02 -0700636void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
637{
638 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
639 rsc->setRaster(pr);
640}
641
Jason Sams326e0dd2009-05-22 14:03:28 -0700642void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
643{
644 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
645 rsc->setVertex(pv);
646}
647
Jason Samsa4a54e42009-06-10 18:39:40 -0700648void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700649{
650 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700651 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700652}
Jason Sams326e0dd2009-05-22 14:03:28 -0700653
Jason Sams707aaf32009-08-18 14:14:24 -0700654void rsi_ObjDestroy(Context *rsc, void *obj)
655{
656 ObjectBase *ob = static_cast<ObjectBase *>(obj);
657 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700658 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700659}
660
Joe Onorato57b79ce2009-08-09 22:57:44 -0700661void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
662{
663 rsc->addInt32Define(name, value);
664}
665
666void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
667{
668 rsc->addFloatDefine(name, value);
669}
Jason Sams326e0dd2009-05-22 14:03:28 -0700670
Jason Sams86f1b232009-09-24 17:38:20 -0700671void rsi_ContextPause(Context *rsc)
672{
673 rsc->pause();
674}
675
676void rsi_ContextResume(Context *rsc)
677{
678 rsc->resume();
679}
680
Jason Sams326e0dd2009-05-22 14:03:28 -0700681}
682}
683
684
Jason Samsafcb25c2009-08-25 11:34:49 -0700685RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700686{
687 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700688 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700689 return rsc;
690}
691
692void rsContextDestroy(RsContext vrsc)
693{
694 Context * rsc = static_cast<Context *>(vrsc);
695 delete rsc;
696}
697
Jason Sams50869382009-08-18 17:07:09 -0700698void rsObjDestroyOOB(RsContext vrsc, void *obj)
699{
700 Context * rsc = static_cast<Context *>(vrsc);
701 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
702}
703
Jason Sams8c401ef2009-10-06 13:58:47 -0700704uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
705{
706 Context * rsc = static_cast<Context *>(vrsc);
707 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
708}
709
710void rsContextInitToClient(RsContext vrsc)
711{
712 Context * rsc = static_cast<Context *>(vrsc);
713 rsc->initToClient();
714}
715
716void rsContextDeinitToClient(RsContext vrsc)
717{
718 Context * rsc = static_cast<Context *>(vrsc);
719 rsc->deinitToClient();
720}
721