blob: 52c2b78bbb4b26fed578920a14d78e7796854b7b [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
Jason Sams1aa5a4e2009-06-22 17:15:15 -070023#include <GLES/gl.h>
24#include <GLES/glext.h>
25
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Samse5769102009-06-19 16:03:18 -070029pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070030
31void Context::initEGL()
32{
Jason Samsafcb25c2009-08-25 11:34:49 -070033 mEGL.mNumConfigs = -1;
34 EGLint configAttribs[128];
35 EGLint *configAttribsPtr = configAttribs;
Jason Sams326e0dd2009-05-22 14:03:28 -070036
Jason Samsafcb25c2009-08-25 11:34:49 -070037 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Jason Samsafcb25c2009-08-25 11:34:49 -070039 configAttribsPtr[0] = EGL_SURFACE_TYPE;
40 configAttribsPtr[1] = EGL_WINDOW_BIT;
41 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070042
Jason Samsafcb25c2009-08-25 11:34:49 -070043 if (mUseDepth) {
44 configAttribsPtr[0] = EGL_DEPTH_SIZE;
45 configAttribsPtr[1] = 16;
46 configAttribsPtr += 2;
47 }
48 configAttribsPtr[0] = EGL_NONE;
49 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070050
Jason Samsafcb25c2009-08-25 11:34:49 -070051 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
52 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
53
54 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
55 if (err) {
56 LOGE("couldn't find an EGLConfig matching the screen format\n");
57 }
58 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
59
60 if (mWndSurface) {
61 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
62 } else {
63 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
64 android_createDisplaySurface(),
65 NULL);
66 }
67
68 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
69 eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
70 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
71 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
72
73
74 mGL.mVersion = glGetString(GL_VERSION);
75 mGL.mVendor = glGetString(GL_VENDOR);
76 mGL.mRenderer = glGetString(GL_RENDERER);
77 mGL.mExtensions = glGetString(GL_EXTENSIONS);
78
Jason Sams306fb232009-08-25 17:09:59 -070079 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
80 //LOGV("GL Version %s", mGL.mVersion);
81 //LOGV("GL Vendor %s", mGL.mVendor);
82 //LOGV("GL Renderer %s", mGL.mRenderer);
83 //LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsafcb25c2009-08-25 11:34:49 -070084
Jason Sams306fb232009-08-25 17:09:59 -070085 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -070086 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -070087 } else {
88 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -070089 }
Jason Sams326e0dd2009-05-22 14:03:28 -070090}
91
Jason Samsa0a1b6f2009-06-10 15:04:38 -070092bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070093{
94 ObjectBaseRef<ProgramFragment> frag(mFragment);
95 ObjectBaseRef<ProgramVertex> vtx(mVertex);
96 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
97
Jason Samsa0a1b6f2009-06-10 15:04:38 -070098 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -070099
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700100 mFragment.set(frag);
101 mVertex.set(vtx);
102 mFragmentStore.set(store);
Jason Samsc9d43db2009-07-28 12:02:16 -0700103 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700104}
105
106
Jason Samsa44cb292009-06-04 17:58:03 -0700107bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700108{
Jason Sams24371d92009-08-19 12:17:14 -0700109#if RS_LOG_TIMES
110 timerSet(RS_TIMER_CLEAR_SWAP);
111#endif
Jason Sams10308932009-06-09 12:15:30 -0700112 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700113
Jason Samscfb1d112009-08-05 13:57:03 -0700114 //glColor4f(1,1,1,1);
115 //glEnable(GL_LIGHT0);
Jason Samsafcb25c2009-08-25 11:34:49 -0700116 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700117 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
118
Jason Sams928b7342009-06-08 18:50:13 -0700119 glClearColor(mRootScript->mEnviroment.mClearColor[0],
120 mRootScript->mEnviroment.mClearColor[1],
121 mRootScript->mEnviroment.mClearColor[2],
122 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700123 if (mUseDepth) {
124 glDepthMask(GL_TRUE);
125 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127 } else {
128 glClear(GL_COLOR_BUFFER_BIT);
129 }
Jason Sams306fb232009-08-25 17:09:59 -0700130
Jason Samscfb1d112009-08-05 13:57:03 -0700131#if RS_LOG_TIMES
Jason Sams24371d92009-08-19 12:17:14 -0700132 timerSet(RS_TIMER_SCRIPT);
Jason Samscfb1d112009-08-05 13:57:03 -0700133#endif
134 bool ret = runScript(mRootScript.get(), 0);
Jason Samscfb1d112009-08-05 13:57:03 -0700135 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700136}
137
Jason Sams24371d92009-08-19 12:17:14 -0700138uint64_t Context::getTime() const
139{
140 struct timespec t;
141 clock_gettime(CLOCK_MONOTONIC, &t);
142 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
143}
144
145void Context::timerReset()
146{
147 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
148 mTimers[ct] = 0;
149 }
150}
151
152void Context::timerInit()
153{
154 mTimeLast = getTime();
155 mTimerActive = RS_TIMER_INTERNAL;
156 timerReset();
157}
158
159void Context::timerSet(Timers tm)
160{
161 uint64_t last = mTimeLast;
162 mTimeLast = getTime();
163 mTimers[mTimerActive] += mTimeLast - last;
164 mTimerActive = tm;
165}
166
167void Context::timerPrint()
168{
169 double total = 0;
170 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
171 total += mTimers[ct];
172 }
173
174 LOGV("RS Time Data: Idle %2.1f (%lli), Internal %2.1f (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli)",
175 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
176 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
177 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
178 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000);
179}
180
Jason Sams326e0dd2009-05-22 14:03:28 -0700181void Context::setupCheck()
182{
183 if (mFragmentStore.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700184 mFragmentStore->setupGL(this, &mStateFragmentStore);
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 }
186 if (mFragment.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700187 mFragment->setupGL(this, &mStateFragment);
Jason Sams326e0dd2009-05-22 14:03:28 -0700188 }
189 if (mVertex.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700190 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700191 }
192
193}
194
195
196void * Context::threadProc(void *vrsc)
197{
198 Context *rsc = static_cast<Context *>(vrsc);
199
Jason Sams326e0dd2009-05-22 14:03:28 -0700200 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700201
Jason Samse5769102009-06-19 16:03:18 -0700202 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
203 if (!tlsStruct) {
204 LOGE("Error allocating tls storage");
205 return NULL;
206 }
207 tlsStruct->mContext = rsc;
208 tlsStruct->mScript = NULL;
209 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
210 if (status) {
211 LOGE("pthread_setspecific %i", status);
212 }
213
Jason Samsafcb25c2009-08-25 11:34:49 -0700214 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700215 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700216 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700217 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700218 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700219 rsc->setFragmentStore(NULL);
220
Jason Sams326e0dd2009-05-22 14:03:28 -0700221 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700222 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700223 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700224 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700225 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700226
Jason Sams732f1c02009-06-18 16:58:42 -0700227 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700228 mDraw = rsc->runRootScript();
Jason Sams24371d92009-08-19 12:17:14 -0700229#if RS_LOG_TIMES
230 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
231#endif
Jason Samsafcb25c2009-08-25 11:34:49 -0700232 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams24371d92009-08-19 12:17:14 -0700233#if RS_LOG_TIMES
234 rsc->timerSet(RS_TIMER_INTERNAL);
235 rsc->timerPrint();
236 rsc->timerReset();
237#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700238 }
Jason Sams24371d92009-08-19 12:17:14 -0700239 if (rsc->mObjDestroy.mNeedToEmpty) {
240 rsc->objDestroyOOBRun();
241 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700242 }
243
Jason Sams8c0ee652009-08-25 14:49:07 -0700244 LOGV("RS Thread exiting");
Jason Sams326e0dd2009-05-22 14:03:28 -0700245 glClearColor(0,0,0,0);
246 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700247 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
248 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700249 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700250 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700251 return NULL;
252}
253
Jason Samsafcb25c2009-08-25 11:34:49 -0700254Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700255{
Jason Sams326e0dd2009-05-22 14:03:28 -0700256 dev->addContext(this);
257 mDev = dev;
258 mRunning = false;
259 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700260 mUseDepth = useDepth;
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
Jason Samsa658e902009-06-04 14:35:01 -0700262 int status;
263 pthread_attr_t threadAttr;
264
Jason Samse5769102009-06-19 16:03:18 -0700265 status = pthread_key_create(&gThreadTLSKey, NULL);
266 if (status) {
267 LOGE("Failed to init thread tls key.");
268 return;
269 }
270
Jason Samsa658e902009-06-04 14:35:01 -0700271 status = pthread_attr_init(&threadAttr);
272 if (status) {
273 LOGE("Failed to init thread attribute.");
274 return;
275 }
276
277 sched_param sparam;
278 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
279 pthread_attr_setschedparam(&threadAttr, &sparam);
280
Jason Sams992a0b72009-06-23 12:22:47 -0700281 mWndSurface = sur;
282
Jason Sams50869382009-08-18 17:07:09 -0700283 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700284 timerInit();
Jason Sams50869382009-08-18 17:07:09 -0700285
Jason Sams992a0b72009-06-23 12:22:47 -0700286 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700287 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700288 if (status) {
289 LOGE("Failed to start rs context thread.");
290 }
291
Jason Sams326e0dd2009-05-22 14:03:28 -0700292 while(!mRunning) {
293 sleep(1);
294 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700295
Jason Samsa658e902009-06-04 14:35:01 -0700296 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700297}
298
299Context::~Context()
300{
Jason Sams8c0ee652009-08-25 14:49:07 -0700301 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700302 mExit = true;
303 void *res;
304
Jason Sams8c0ee652009-08-25 14:49:07 -0700305 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700306 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700307 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700308
309 if (mDev) {
310 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700311 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700312 }
Jason Sams50869382009-08-18 17:07:09 -0700313
314 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700315}
316
Jason Sams326e0dd2009-05-22 14:03:28 -0700317void Context::setRootScript(Script *s)
318{
319 mRootScript.set(s);
320}
321
322void Context::setFragmentStore(ProgramFragmentStore *pfs)
323{
Jason Sams8ce125b2009-06-17 16:52:59 -0700324 if (pfs == NULL) {
325 mFragmentStore.set(mStateFragmentStore.mDefault);
326 } else {
327 mFragmentStore.set(pfs);
328 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700329}
330
331void Context::setFragment(ProgramFragment *pf)
332{
Jason Sams8ce125b2009-06-17 16:52:59 -0700333 if (pf == NULL) {
334 mFragment.set(mStateFragment.mDefault);
335 } else {
336 mFragment.set(pf);
337 }
Jason Samscfb1d112009-08-05 13:57:03 -0700338}
339
340void Context::allocationCheck(const Allocation *a)
341{
342 mVertex->checkUpdatedAllocation(a);
343 mFragment->checkUpdatedAllocation(a);
344 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700345}
346
347void Context::setVertex(ProgramVertex *pv)
348{
Jason Sams8ce125b2009-06-17 16:52:59 -0700349 if (pv == NULL) {
350 mVertex.set(mStateVertex.mDefault);
351 } else {
352 mVertex.set(pv);
353 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700354}
355
Jason Samsa4a54e42009-06-10 18:39:40 -0700356void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700357{
358 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700359 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700360 mNames.add(obj);
361}
362
363void Context::removeName(ObjectBase *obj)
364{
365 for(size_t ct=0; ct < mNames.size(); ct++) {
366 if (obj == mNames[ct]) {
367 mNames.removeAt(ct);
368 return;
369 }
370 }
371}
372
373ObjectBase * Context::lookupName(const char *name) const
374{
375 for(size_t ct=0; ct < mNames.size(); ct++) {
376 if (!strcmp(name, mNames[ct]->getName())) {
377 return mNames[ct];
378 }
379 }
380 return NULL;
381}
382
Jason Samsa4a54e42009-06-10 18:39:40 -0700383void Context::appendNameDefines(String8 *str) const
384{
385 char buf[256];
386 for (size_t ct=0; ct < mNames.size(); ct++) {
387 str->append("#define NAMED_");
388 str->append(mNames[ct]->getName());
389 str->append(" ");
390 sprintf(buf, "%i\n", (int)mNames[ct]);
391 str->append(buf);
392 }
393}
394
Joe Onorato57b79ce2009-08-09 22:57:44 -0700395void Context::appendVarDefines(String8 *str) const
396{
397 char buf[256];
398 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
399 str->append("#define ");
400 str->append(mInt32Defines.keyAt(ct));
401 str->append(" ");
402 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
403 str->append(buf);
404
405 }
406 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
407 str->append("#define ");
408 str->append(mFloatDefines.keyAt(ct));
409 str->append(" ");
410 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
411 str->append(buf);
412 }
413}
414
Jason Sams50869382009-08-18 17:07:09 -0700415bool Context::objDestroyOOBInit()
416{
417 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
418 if (status) {
419 LOGE("Context::ObjDestroyOOBInit mutex init failure");
420 return false;
421 }
422 return true;
423}
424
425void Context::objDestroyOOBRun()
426{
427 if (mObjDestroy.mNeedToEmpty) {
428 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
429 if (status) {
430 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
431 return;
432 }
433
434 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
435 mObjDestroy.mDestroyList[ct]->decRef();
436 }
437 mObjDestroy.mDestroyList.clear();
438 mObjDestroy.mNeedToEmpty = false;
439
440 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
441 if (status) {
442 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
443 }
444 }
445}
446
447void Context::objDestroyOOBDestroy()
448{
449 rsAssert(!mObjDestroy.mNeedToEmpty);
450 pthread_mutex_destroy(&mObjDestroy.mMutex);
451}
452
453void Context::objDestroyAdd(ObjectBase *obj)
454{
455 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
456 if (status) {
457 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
458 return;
459 }
460
461 mObjDestroy.mNeedToEmpty = true;
462 mObjDestroy.mDestroyList.add(obj);
463
464 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
465 if (status) {
466 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
467 }
468}
469
470
Jason Samsa4a54e42009-06-10 18:39:40 -0700471
Jason Sams326e0dd2009-05-22 14:03:28 -0700472///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700473//
Jason Sams326e0dd2009-05-22 14:03:28 -0700474
475namespace android {
476namespace renderscript {
477
478
479void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
480{
481 Script *s = static_cast<Script *>(vs);
482 rsc->setRootScript(s);
483}
484
485void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
486{
487 Sampler *s = static_cast<Sampler *>(vs);
488
489 if (slot > RS_MAX_SAMPLER_SLOT) {
490 LOGE("Invalid sampler slot");
491 return;
492 }
493
494 s->bindToContext(&rsc->mStateSampler, slot);
495}
496
497void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
498{
499 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
500 rsc->setFragmentStore(pfs);
501}
502
503void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
504{
505 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
506 rsc->setFragment(pf);
507}
508
509void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
510{
511 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
512 rsc->setVertex(pv);
513}
514
Jason Samsa4a54e42009-06-10 18:39:40 -0700515void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700516{
517 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700518 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700519}
Jason Sams326e0dd2009-05-22 14:03:28 -0700520
Jason Sams707aaf32009-08-18 14:14:24 -0700521void rsi_ObjDestroy(Context *rsc, void *obj)
522{
523 ObjectBase *ob = static_cast<ObjectBase *>(obj);
524 rsc->removeName(ob);
525 ob->decRef();
526}
527
Joe Onorato57b79ce2009-08-09 22:57:44 -0700528void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
529{
530 rsc->addInt32Define(name, value);
531}
532
533void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
534{
535 rsc->addFloatDefine(name, value);
536}
Jason Sams326e0dd2009-05-22 14:03:28 -0700537
538}
539}
540
541
Jason Samsafcb25c2009-08-25 11:34:49 -0700542RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700543{
544 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700545 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700546 return rsc;
547}
548
549void rsContextDestroy(RsContext vrsc)
550{
551 Context * rsc = static_cast<Context *>(vrsc);
552 delete rsc;
553}
554
Jason Sams50869382009-08-18 17:07:09 -0700555void rsObjDestroyOOB(RsContext vrsc, void *obj)
556{
557 Context * rsc = static_cast<Context *>(vrsc);
558 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
559}
560