blob: 653d42734ca303faf110fe483d70adb8da016633 [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);
106
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700107 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700108
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700109 mFragment.set(frag);
110 mVertex.set(vtx);
111 mFragmentStore.set(store);
Jason Samsc9d43db2009-07-28 12:02:16 -0700112 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700113}
114
115
Jason Samsa44cb292009-06-04 17:58:03 -0700116bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700117{
Jason Sams1fddd902009-09-25 15:25:00 -0700118 if (props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700119 timerSet(RS_TIMER_CLEAR_SWAP);
120 }
Jason Sams10308932009-06-09 12:15:30 -0700121 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700122
Jason Sams8c9534b2009-09-22 12:26:53 -0700123 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
124 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700125 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700126 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Jason Sams5fd09d82009-09-23 13:57:02 -0700127 //glEnable(GL_POINT_SMOOTH);
Jason Sams326e0dd2009-05-22 14:03:28 -0700128
Jason Sams928b7342009-06-08 18:50:13 -0700129 glClearColor(mRootScript->mEnviroment.mClearColor[0],
130 mRootScript->mEnviroment.mClearColor[1],
131 mRootScript->mEnviroment.mClearColor[2],
132 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700133 if (mUseDepth) {
134 glDepthMask(GL_TRUE);
135 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
136 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137 } else {
138 glClear(GL_COLOR_BUFFER_BIT);
139 }
Jason Sams306fb232009-08-25 17:09:59 -0700140
Jason Sams1fddd902009-09-25 15:25:00 -0700141 if (this->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700142 timerSet(RS_TIMER_SCRIPT);
143 }
Jason Samscfb1d112009-08-05 13:57:03 -0700144 bool ret = runScript(mRootScript.get(), 0);
Jason Samscfb1d112009-08-05 13:57:03 -0700145 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700146}
147
Jason Sams24371d92009-08-19 12:17:14 -0700148uint64_t Context::getTime() const
149{
150 struct timespec t;
151 clock_gettime(CLOCK_MONOTONIC, &t);
152 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
153}
154
155void Context::timerReset()
156{
157 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
158 mTimers[ct] = 0;
159 }
160}
161
162void Context::timerInit()
163{
164 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700165 mTimeFrame = mTimeLast;
166 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700167 mTimerActive = RS_TIMER_INTERNAL;
168 timerReset();
169}
170
Jason Sams1d54f102009-09-03 15:43:13 -0700171void Context::timerFrame()
172{
173 mTimeLastFrame = mTimeFrame;
174 mTimeFrame = getTime();
175}
176
Jason Sams24371d92009-08-19 12:17:14 -0700177void Context::timerSet(Timers tm)
178{
179 uint64_t last = mTimeLast;
180 mTimeLast = getTime();
181 mTimers[mTimerActive] += mTimeLast - last;
182 mTimerActive = tm;
183}
184
185void Context::timerPrint()
186{
187 double total = 0;
188 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
189 total += mTimers[ct];
190 }
Jason Sams1d54f102009-09-03 15:43:13 -0700191 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700192
Jason Sams1d54f102009-09-03 15:43:13 -0700193 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
194 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700195 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700196 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
197 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
198 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700199}
200
Jason Sams326e0dd2009-05-22 14:03:28 -0700201void Context::setupCheck()
202{
Jason Sams5fd09d82009-09-23 13:57:02 -0700203 mFragmentStore->setupGL(this, &mStateFragmentStore);
204 mFragment->setupGL(this, &mStateFragment);
205 mRaster->setupGL(this, &mStateRaster);
206 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700207}
208
Jason Sams1fddd902009-09-25 15:25:00 -0700209static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700210{
211 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700212 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700213 return 0 != strcmp(buf, "0");
214}
Jason Sams326e0dd2009-05-22 14:03:28 -0700215
216void * Context::threadProc(void *vrsc)
217{
218 Context *rsc = static_cast<Context *>(vrsc);
219
Jason Sams1fddd902009-09-25 15:25:00 -0700220 rsc->props.mLogTimes = getProp("debug.rs.profile");
221 rsc->props.mLogScripts = getProp("debug.rs.script");
222 rsc->props.mLogObjects = getProp("debug.rs.objects");
Joe Onorato76371ff2009-09-23 16:37:36 -0700223
Jason Sams326e0dd2009-05-22 14:03:28 -0700224 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700225
Jason Samse5769102009-06-19 16:03:18 -0700226 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
227 if (!tlsStruct) {
228 LOGE("Error allocating tls storage");
229 return NULL;
230 }
231 tlsStruct->mContext = rsc;
232 tlsStruct->mScript = NULL;
233 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
234 if (status) {
235 LOGE("pthread_setspecific %i", status);
236 }
237
Jason Sams5fd09d82009-09-23 13:57:02 -0700238 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
239 rsc->setRaster(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700240 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700241 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700242 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700243 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700244 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700245 rsc->setFragmentStore(NULL);
246
Jason Sams326e0dd2009-05-22 14:03:28 -0700247 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700248 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700249 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700250 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700251 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700252
Jason Sams732f1c02009-06-18 16:58:42 -0700253 if (mDraw) {
Jason Sams86f1b232009-09-24 17:38:20 -0700254 mDraw = rsc->runRootScript() && !rsc->mPaused;
Jason Sams1fddd902009-09-25 15:25:00 -0700255 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700256 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
257 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700258 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams1fddd902009-09-25 15:25:00 -0700259 if (rsc->props.mLogTimes) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700260 rsc->timerFrame();
261 rsc->timerSet(RS_TIMER_INTERNAL);
262 rsc->timerPrint();
263 rsc->timerReset();
264 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700265 }
Jason Sams24371d92009-08-19 12:17:14 -0700266 if (rsc->mObjDestroy.mNeedToEmpty) {
267 rsc->objDestroyOOBRun();
268 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700269 }
270
Jason Sams8c0ee652009-08-25 14:49:07 -0700271 LOGV("RS Thread exiting");
Jason Samse514b452009-09-25 14:51:22 -0700272 ObjectBase::zeroAllUserRef(rsc);
273 rsc->mRaster.set(NULL);
274 rsc->mFragment.set(NULL);
275 rsc->mVertex.set(NULL);
276 rsc->mFragmentStore.set(NULL);
277
Jason Sams326e0dd2009-05-22 14:03:28 -0700278 glClearColor(0,0,0,0);
279 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700280 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
281 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700282 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700283 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700284 return NULL;
285}
286
Jason Samsafcb25c2009-08-25 11:34:49 -0700287Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700288{
Jason Sams326e0dd2009-05-22 14:03:28 -0700289 dev->addContext(this);
290 mDev = dev;
291 mRunning = false;
292 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700293 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700294 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700295 mObjHead = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700296
Jason Samsa658e902009-06-04 14:35:01 -0700297 int status;
298 pthread_attr_t threadAttr;
299
Jason Samse5769102009-06-19 16:03:18 -0700300 status = pthread_key_create(&gThreadTLSKey, NULL);
301 if (status) {
302 LOGE("Failed to init thread tls key.");
303 return;
304 }
305
Jason Samsa658e902009-06-04 14:35:01 -0700306 status = pthread_attr_init(&threadAttr);
307 if (status) {
308 LOGE("Failed to init thread attribute.");
309 return;
310 }
311
312 sched_param sparam;
313 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
314 pthread_attr_setschedparam(&threadAttr, &sparam);
315
Jason Sams992a0b72009-06-23 12:22:47 -0700316 mWndSurface = sur;
317
Jason Sams50869382009-08-18 17:07:09 -0700318 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700319 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700320 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700321
Jason Sams992a0b72009-06-23 12:22:47 -0700322 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700323 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700324 if (status) {
325 LOGE("Failed to start rs context thread.");
326 }
327
Jason Sams326e0dd2009-05-22 14:03:28 -0700328 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700329 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700330 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700331
Jason Samsa658e902009-06-04 14:35:01 -0700332 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700333}
334
335Context::~Context()
336{
Jason Sams8c0ee652009-08-25 14:49:07 -0700337 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700338 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700339 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700340 void *res;
341
Jason Sams8c0ee652009-08-25 14:49:07 -0700342 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700343 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700344 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700345
346 if (mDev) {
347 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700348 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 }
Jason Sams50869382009-08-18 17:07:09 -0700350
351 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700352}
353
Jason Sams86f1b232009-09-24 17:38:20 -0700354void Context::pause()
355{
356 mPaused = true;
357}
358
359void Context::resume()
360{
361 mPaused = false;
362}
363
Jason Sams326e0dd2009-05-22 14:03:28 -0700364void Context::setRootScript(Script *s)
365{
366 mRootScript.set(s);
367}
368
369void Context::setFragmentStore(ProgramFragmentStore *pfs)
370{
Jason Sams8ce125b2009-06-17 16:52:59 -0700371 if (pfs == NULL) {
372 mFragmentStore.set(mStateFragmentStore.mDefault);
373 } else {
374 mFragmentStore.set(pfs);
375 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700376}
377
378void Context::setFragment(ProgramFragment *pf)
379{
Jason Sams8ce125b2009-06-17 16:52:59 -0700380 if (pf == NULL) {
381 mFragment.set(mStateFragment.mDefault);
382 } else {
383 mFragment.set(pf);
384 }
Jason Samscfb1d112009-08-05 13:57:03 -0700385}
386
Jason Sams5fd09d82009-09-23 13:57:02 -0700387void Context::setRaster(ProgramRaster *pr)
388{
389 if (pr == NULL) {
390 mRaster.set(mStateRaster.mDefault);
391 } else {
392 mRaster.set(pr);
393 }
394}
395
Jason Samscfb1d112009-08-05 13:57:03 -0700396void Context::allocationCheck(const Allocation *a)
397{
398 mVertex->checkUpdatedAllocation(a);
399 mFragment->checkUpdatedAllocation(a);
400 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700401}
402
403void Context::setVertex(ProgramVertex *pv)
404{
Jason Sams8ce125b2009-06-17 16:52:59 -0700405 if (pv == NULL) {
406 mVertex.set(mStateVertex.mDefault);
407 } else {
408 mVertex.set(pv);
409 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700410}
411
Jason Samsa4a54e42009-06-10 18:39:40 -0700412void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700413{
414 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700415 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700416 mNames.add(obj);
417}
418
419void Context::removeName(ObjectBase *obj)
420{
421 for(size_t ct=0; ct < mNames.size(); ct++) {
422 if (obj == mNames[ct]) {
423 mNames.removeAt(ct);
424 return;
425 }
426 }
427}
428
429ObjectBase * Context::lookupName(const char *name) const
430{
431 for(size_t ct=0; ct < mNames.size(); ct++) {
432 if (!strcmp(name, mNames[ct]->getName())) {
433 return mNames[ct];
434 }
435 }
436 return NULL;
437}
438
Jason Samsa4a54e42009-06-10 18:39:40 -0700439void Context::appendNameDefines(String8 *str) const
440{
441 char buf[256];
442 for (size_t ct=0; ct < mNames.size(); ct++) {
443 str->append("#define NAMED_");
444 str->append(mNames[ct]->getName());
445 str->append(" ");
446 sprintf(buf, "%i\n", (int)mNames[ct]);
447 str->append(buf);
448 }
449}
450
Joe Onorato57b79ce2009-08-09 22:57:44 -0700451void Context::appendVarDefines(String8 *str) const
452{
453 char buf[256];
454 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
455 str->append("#define ");
456 str->append(mInt32Defines.keyAt(ct));
457 str->append(" ");
458 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
459 str->append(buf);
460
461 }
462 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
463 str->append("#define ");
464 str->append(mFloatDefines.keyAt(ct));
465 str->append(" ");
466 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
467 str->append(buf);
468 }
469}
470
Jason Sams50869382009-08-18 17:07:09 -0700471bool Context::objDestroyOOBInit()
472{
473 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
474 if (status) {
475 LOGE("Context::ObjDestroyOOBInit mutex init failure");
476 return false;
477 }
478 return true;
479}
480
481void Context::objDestroyOOBRun()
482{
483 if (mObjDestroy.mNeedToEmpty) {
484 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
485 if (status) {
486 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
487 return;
488 }
489
490 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700491 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700492 }
493 mObjDestroy.mDestroyList.clear();
494 mObjDestroy.mNeedToEmpty = false;
495
496 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
497 if (status) {
498 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
499 }
500 }
501}
502
503void Context::objDestroyOOBDestroy()
504{
505 rsAssert(!mObjDestroy.mNeedToEmpty);
506 pthread_mutex_destroy(&mObjDestroy.mMutex);
507}
508
509void Context::objDestroyAdd(ObjectBase *obj)
510{
511 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
512 if (status) {
513 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
514 return;
515 }
516
517 mObjDestroy.mNeedToEmpty = true;
518 mObjDestroy.mDestroyList.add(obj);
519
520 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
521 if (status) {
522 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
523 }
524}
525
526
Jason Samsa4a54e42009-06-10 18:39:40 -0700527
Jason Sams326e0dd2009-05-22 14:03:28 -0700528///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700529//
Jason Sams326e0dd2009-05-22 14:03:28 -0700530
531namespace android {
532namespace renderscript {
533
534
535void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
536{
537 Script *s = static_cast<Script *>(vs);
538 rsc->setRootScript(s);
539}
540
541void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
542{
543 Sampler *s = static_cast<Sampler *>(vs);
544
545 if (slot > RS_MAX_SAMPLER_SLOT) {
546 LOGE("Invalid sampler slot");
547 return;
548 }
549
550 s->bindToContext(&rsc->mStateSampler, slot);
551}
552
553void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
554{
555 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
556 rsc->setFragmentStore(pfs);
557}
558
559void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
560{
561 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
562 rsc->setFragment(pf);
563}
564
Jason Sams5fd09d82009-09-23 13:57:02 -0700565void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
566{
567 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
568 rsc->setRaster(pr);
569}
570
Jason Sams326e0dd2009-05-22 14:03:28 -0700571void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
572{
573 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
574 rsc->setVertex(pv);
575}
576
Jason Samsa4a54e42009-06-10 18:39:40 -0700577void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700578{
579 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700580 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700581}
Jason Sams326e0dd2009-05-22 14:03:28 -0700582
Jason Sams707aaf32009-08-18 14:14:24 -0700583void rsi_ObjDestroy(Context *rsc, void *obj)
584{
585 ObjectBase *ob = static_cast<ObjectBase *>(obj);
586 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700587 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700588}
589
Joe Onorato57b79ce2009-08-09 22:57:44 -0700590void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
591{
592 rsc->addInt32Define(name, value);
593}
594
595void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
596{
597 rsc->addFloatDefine(name, value);
598}
Jason Sams326e0dd2009-05-22 14:03:28 -0700599
Jason Sams86f1b232009-09-24 17:38:20 -0700600void rsi_ContextPause(Context *rsc)
601{
602 rsc->pause();
603}
604
605void rsi_ContextResume(Context *rsc)
606{
607 rsc->resume();
608}
609
Jason Sams326e0dd2009-05-22 14:03:28 -0700610}
611}
612
613
Jason Samsafcb25c2009-08-25 11:34:49 -0700614RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700615{
616 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700617 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700618 return rsc;
619}
620
621void rsContextDestroy(RsContext vrsc)
622{
623 Context * rsc = static_cast<Context *>(vrsc);
624 delete rsc;
625}
626
Jason Sams50869382009-08-18 17:07:09 -0700627void rsObjDestroyOOB(RsContext vrsc, void *obj)
628{
629 Context * rsc = static_cast<Context *>(vrsc);
630 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
631}
632