blob: 2fe762c3df30c90614da2201d7dffb22bc7f6946 [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
Joe Onorato76371ff2009-09-23 16:37:36 -070031bool g_logTimes = -1;
32
Jason Samse5769102009-06-19 16:03:18 -070033pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070034
35void Context::initEGL()
36{
Jason Samsafcb25c2009-08-25 11:34:49 -070037 mEGL.mNumConfigs = -1;
38 EGLint configAttribs[128];
39 EGLint *configAttribsPtr = configAttribs;
Jason Sams326e0dd2009-05-22 14:03:28 -070040
Jason Samsafcb25c2009-08-25 11:34:49 -070041 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070042
Jason Samsafcb25c2009-08-25 11:34:49 -070043 configAttribsPtr[0] = EGL_SURFACE_TYPE;
44 configAttribsPtr[1] = EGL_WINDOW_BIT;
45 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070046
Jason Samsafcb25c2009-08-25 11:34:49 -070047 if (mUseDepth) {
48 configAttribsPtr[0] = EGL_DEPTH_SIZE;
49 configAttribsPtr[1] = 16;
50 configAttribsPtr += 2;
51 }
Jason Sams9397e302009-08-27 20:23:34 -070052
Jason Sams5fd09d82009-09-23 13:57:02 -070053 if (mDev->mForceSW) {
54 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
55 configAttribsPtr[1] = EGL_SLOW_CONFIG;
56 configAttribsPtr += 2;
57 }
58
Jason Samsafcb25c2009-08-25 11:34:49 -070059 configAttribsPtr[0] = EGL_NONE;
60 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Jason Samsafcb25c2009-08-25 11:34:49 -070062 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
63 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
64
65 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
66 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070067 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -070068 }
69 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
70
71 if (mWndSurface) {
72 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
73 } else {
74 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
75 android_createDisplaySurface(),
76 NULL);
77 }
78
79 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
80 eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
81 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
82 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
83
84
85 mGL.mVersion = glGetString(GL_VERSION);
86 mGL.mVendor = glGetString(GL_VENDOR);
87 mGL.mRenderer = glGetString(GL_RENDERER);
88 mGL.mExtensions = glGetString(GL_EXTENSIONS);
89
Jason Sams9397e302009-08-27 20:23:34 -070090 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
91 LOGV("GL Version %s", mGL.mVersion);
92 LOGV("GL Vendor %s", mGL.mVendor);
93 LOGV("GL Renderer %s", mGL.mRenderer);
94 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -070095
Jason Sams306fb232009-08-25 17:09:59 -070096 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -070097 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -070098 } else {
99 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -0700100 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700101}
102
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700103bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700104{
105 ObjectBaseRef<ProgramFragment> frag(mFragment);
106 ObjectBaseRef<ProgramVertex> vtx(mVertex);
107 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
108
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 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{
Joe Onorato76371ff2009-09-23 16:37:36 -0700120 if (this->logTimes) {
121 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);
Jason Sams5fd09d82009-09-23 13:57:02 -0700129 //glEnable(GL_POINT_SMOOTH);
Jason Sams326e0dd2009-05-22 14:03:28 -0700130
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
Joe Onorato76371ff2009-09-23 16:37:36 -0700143 if (this->logTimes) {
144 timerSet(RS_TIMER_SCRIPT);
145 }
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
Joe Onorato76371ff2009-09-23 16:37:36 -0700211static bool get_log_times()
212{
213 char buf[PROPERTY_VALUE_MAX];
214 property_get("debug.rs.profile", buf, "0");
215 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
Joe Onorato76371ff2009-09-23 16:37:36 -0700222 rsc->logTimes = get_log_times();
223
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;
Joe Onorato76371ff2009-09-23 16:37:36 -0700255 if (rsc->logTimes) {
256 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
257 }
Jason Samsafcb25c2009-08-25 11:34:49 -0700258 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 if (rsc->logTimes) {
260 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 Sams326e0dd2009-05-22 14:03:28 -0700272 glClearColor(0,0,0,0);
273 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700274 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
275 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700276 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700277 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700278 return NULL;
279}
280
Jason Samsafcb25c2009-08-25 11:34:49 -0700281Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700282{
Jason Sams326e0dd2009-05-22 14:03:28 -0700283 dev->addContext(this);
284 mDev = dev;
285 mRunning = false;
286 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700287 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700288 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700289
Jason Samsa658e902009-06-04 14:35:01 -0700290 int status;
291 pthread_attr_t threadAttr;
292
Jason Samse5769102009-06-19 16:03:18 -0700293 status = pthread_key_create(&gThreadTLSKey, NULL);
294 if (status) {
295 LOGE("Failed to init thread tls key.");
296 return;
297 }
298
Jason Samsa658e902009-06-04 14:35:01 -0700299 status = pthread_attr_init(&threadAttr);
300 if (status) {
301 LOGE("Failed to init thread attribute.");
302 return;
303 }
304
305 sched_param sparam;
306 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
307 pthread_attr_setschedparam(&threadAttr, &sparam);
308
Jason Sams992a0b72009-06-23 12:22:47 -0700309 mWndSurface = sur;
310
Jason Sams50869382009-08-18 17:07:09 -0700311 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700312 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700313 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700314
Jason Sams992a0b72009-06-23 12:22:47 -0700315 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700316 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700317 if (status) {
318 LOGE("Failed to start rs context thread.");
319 }
320
Jason Sams326e0dd2009-05-22 14:03:28 -0700321 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700322 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700323 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700324
Jason Samsa658e902009-06-04 14:35:01 -0700325 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700326}
327
328Context::~Context()
329{
Jason Sams8c0ee652009-08-25 14:49:07 -0700330 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700331 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700332 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700333 void *res;
334
Jason Sams8c0ee652009-08-25 14:49:07 -0700335 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700336 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700337 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700338
339 if (mDev) {
340 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700341 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700342 }
Jason Sams50869382009-08-18 17:07:09 -0700343
344 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700345}
346
Jason Sams86f1b232009-09-24 17:38:20 -0700347void Context::pause()
348{
349 mPaused = true;
350}
351
352void Context::resume()
353{
354 mPaused = false;
355}
356
Jason Sams326e0dd2009-05-22 14:03:28 -0700357void Context::setRootScript(Script *s)
358{
359 mRootScript.set(s);
360}
361
362void Context::setFragmentStore(ProgramFragmentStore *pfs)
363{
Jason Sams8ce125b2009-06-17 16:52:59 -0700364 if (pfs == NULL) {
365 mFragmentStore.set(mStateFragmentStore.mDefault);
366 } else {
367 mFragmentStore.set(pfs);
368 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700369}
370
371void Context::setFragment(ProgramFragment *pf)
372{
Jason Sams8ce125b2009-06-17 16:52:59 -0700373 if (pf == NULL) {
374 mFragment.set(mStateFragment.mDefault);
375 } else {
376 mFragment.set(pf);
377 }
Jason Samscfb1d112009-08-05 13:57:03 -0700378}
379
Jason Sams5fd09d82009-09-23 13:57:02 -0700380void Context::setRaster(ProgramRaster *pr)
381{
382 if (pr == NULL) {
383 mRaster.set(mStateRaster.mDefault);
384 } else {
385 mRaster.set(pr);
386 }
387}
388
Jason Samscfb1d112009-08-05 13:57:03 -0700389void Context::allocationCheck(const Allocation *a)
390{
391 mVertex->checkUpdatedAllocation(a);
392 mFragment->checkUpdatedAllocation(a);
393 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700394}
395
396void Context::setVertex(ProgramVertex *pv)
397{
Jason Sams8ce125b2009-06-17 16:52:59 -0700398 if (pv == NULL) {
399 mVertex.set(mStateVertex.mDefault);
400 } else {
401 mVertex.set(pv);
402 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700403}
404
Jason Samsa4a54e42009-06-10 18:39:40 -0700405void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700406{
407 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700408 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700409 mNames.add(obj);
410}
411
412void Context::removeName(ObjectBase *obj)
413{
414 for(size_t ct=0; ct < mNames.size(); ct++) {
415 if (obj == mNames[ct]) {
416 mNames.removeAt(ct);
417 return;
418 }
419 }
420}
421
422ObjectBase * Context::lookupName(const char *name) const
423{
424 for(size_t ct=0; ct < mNames.size(); ct++) {
425 if (!strcmp(name, mNames[ct]->getName())) {
426 return mNames[ct];
427 }
428 }
429 return NULL;
430}
431
Jason Samsa4a54e42009-06-10 18:39:40 -0700432void Context::appendNameDefines(String8 *str) const
433{
434 char buf[256];
435 for (size_t ct=0; ct < mNames.size(); ct++) {
436 str->append("#define NAMED_");
437 str->append(mNames[ct]->getName());
438 str->append(" ");
439 sprintf(buf, "%i\n", (int)mNames[ct]);
440 str->append(buf);
441 }
442}
443
Joe Onorato57b79ce2009-08-09 22:57:44 -0700444void Context::appendVarDefines(String8 *str) const
445{
446 char buf[256];
447 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
448 str->append("#define ");
449 str->append(mInt32Defines.keyAt(ct));
450 str->append(" ");
451 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
452 str->append(buf);
453
454 }
455 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
456 str->append("#define ");
457 str->append(mFloatDefines.keyAt(ct));
458 str->append(" ");
459 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
460 str->append(buf);
461 }
462}
463
Jason Sams50869382009-08-18 17:07:09 -0700464bool Context::objDestroyOOBInit()
465{
466 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
467 if (status) {
468 LOGE("Context::ObjDestroyOOBInit mutex init failure");
469 return false;
470 }
471 return true;
472}
473
474void Context::objDestroyOOBRun()
475{
476 if (mObjDestroy.mNeedToEmpty) {
477 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
478 if (status) {
479 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
480 return;
481 }
482
483 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700484 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700485 }
486 mObjDestroy.mDestroyList.clear();
487 mObjDestroy.mNeedToEmpty = false;
488
489 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
490 if (status) {
491 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
492 }
493 }
494}
495
496void Context::objDestroyOOBDestroy()
497{
498 rsAssert(!mObjDestroy.mNeedToEmpty);
499 pthread_mutex_destroy(&mObjDestroy.mMutex);
500}
501
502void Context::objDestroyAdd(ObjectBase *obj)
503{
504 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
505 if (status) {
506 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
507 return;
508 }
509
510 mObjDestroy.mNeedToEmpty = true;
511 mObjDestroy.mDestroyList.add(obj);
512
513 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
514 if (status) {
515 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
516 }
517}
518
519
Jason Samsa4a54e42009-06-10 18:39:40 -0700520
Jason Sams326e0dd2009-05-22 14:03:28 -0700521///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700522//
Jason Sams326e0dd2009-05-22 14:03:28 -0700523
524namespace android {
525namespace renderscript {
526
527
528void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
529{
530 Script *s = static_cast<Script *>(vs);
531 rsc->setRootScript(s);
532}
533
534void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
535{
536 Sampler *s = static_cast<Sampler *>(vs);
537
538 if (slot > RS_MAX_SAMPLER_SLOT) {
539 LOGE("Invalid sampler slot");
540 return;
541 }
542
543 s->bindToContext(&rsc->mStateSampler, slot);
544}
545
546void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
547{
548 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
549 rsc->setFragmentStore(pfs);
550}
551
552void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
553{
554 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
555 rsc->setFragment(pf);
556}
557
Jason Sams5fd09d82009-09-23 13:57:02 -0700558void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
559{
560 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
561 rsc->setRaster(pr);
562}
563
Jason Sams326e0dd2009-05-22 14:03:28 -0700564void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
565{
566 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
567 rsc->setVertex(pv);
568}
569
Jason Samsa4a54e42009-06-10 18:39:40 -0700570void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700571{
572 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700573 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700574}
Jason Sams326e0dd2009-05-22 14:03:28 -0700575
Jason Sams707aaf32009-08-18 14:14:24 -0700576void rsi_ObjDestroy(Context *rsc, void *obj)
577{
578 ObjectBase *ob = static_cast<ObjectBase *>(obj);
579 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700580 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700581}
582
Joe Onorato57b79ce2009-08-09 22:57:44 -0700583void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
584{
585 rsc->addInt32Define(name, value);
586}
587
588void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
589{
590 rsc->addFloatDefine(name, value);
591}
Jason Sams326e0dd2009-05-22 14:03:28 -0700592
Jason Sams86f1b232009-09-24 17:38:20 -0700593void rsi_ContextPause(Context *rsc)
594{
595 rsc->pause();
596}
597
598void rsi_ContextResume(Context *rsc)
599{
600 rsc->resume();
601}
602
Jason Sams326e0dd2009-05-22 14:03:28 -0700603}
604}
605
606
Jason Samsafcb25c2009-08-25 11:34:49 -0700607RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700608{
609 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700610 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700611 return rsc;
612}
613
614void rsContextDestroy(RsContext vrsc)
615{
616 Context * rsc = static_cast<Context *>(vrsc);
617 delete rsc;
618}
619
Jason Sams50869382009-08-18 17:07:09 -0700620void rsObjDestroyOOB(RsContext vrsc, void *obj)
621{
622 Context * rsc = static_cast<Context *>(vrsc);
623 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
624}
625