blob: c28bd02c0efe4fc4cc48afa2304d2c4691362c09 [file] [log] [blame]
Jason Samsd19f10d2009-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 Agopian3142f4f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsb13ada52009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070022
Jason Sams4b962e52009-06-22 17:15:15 -070023#include <GLES/gl.h>
24#include <GLES/glext.h>
25
Jason Samsd19f10d2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Sams462d11b2009-06-19 16:03:18 -070029pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070030
31void Context::initEGL()
32{
Jason Samsb13ada52009-08-25 11:34:49 -070033 mEGL.mNumConfigs = -1;
34 EGLint configAttribs[128];
35 EGLint *configAttribsPtr = configAttribs;
Jason Samsd19f10d2009-05-22 14:03:28 -070036
Jason Samsb13ada52009-08-25 11:34:49 -070037 memset(configAttribs, 0, sizeof(configAttribs));
Jason Samsd19f10d2009-05-22 14:03:28 -070038
Jason Samsb13ada52009-08-25 11:34:49 -070039 configAttribsPtr[0] = EGL_SURFACE_TYPE;
40 configAttribsPtr[1] = EGL_WINDOW_BIT;
41 configAttribsPtr += 2;
Jason Samsd19f10d2009-05-22 14:03:28 -070042
Jason Samsb13ada52009-08-25 11:34:49 -070043 if (mUseDepth) {
44 configAttribsPtr[0] = EGL_DEPTH_SIZE;
45 configAttribsPtr[1] = 16;
46 configAttribsPtr += 2;
47 }
Jason Sams07ae4062009-08-27 20:23:34 -070048
Jason Samsb13ada52009-08-25 11:34:49 -070049 configAttribsPtr[0] = EGL_NONE;
50 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Samsd19f10d2009-05-22 14:03:28 -070051
Jason Samsb13ada52009-08-25 11:34:49 -070052 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
53 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
54
55 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
56 if (err) {
Jason Sams07ae4062009-08-27 20:23:34 -070057 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsb13ada52009-08-25 11:34:49 -070058 }
59 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
60
61 if (mWndSurface) {
62 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
63 } else {
64 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
65 android_createDisplaySurface(),
66 NULL);
67 }
68
69 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
70 eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
71 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
72 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
73
74
75 mGL.mVersion = glGetString(GL_VERSION);
76 mGL.mVendor = glGetString(GL_VENDOR);
77 mGL.mRenderer = glGetString(GL_RENDERER);
78 mGL.mExtensions = glGetString(GL_EXTENSIONS);
79
Jason Sams07ae4062009-08-27 20:23:34 -070080 LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
81 LOGV("GL Version %s", mGL.mVersion);
82 LOGV("GL Vendor %s", mGL.mVendor);
83 LOGV("GL Renderer %s", mGL.mRenderer);
84 LOGV("GL Extensions %s", mGL.mExtensions);
Jason Samsb13ada52009-08-25 11:34:49 -070085
Jason Sams67c68442009-08-25 17:09:59 -070086 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsb13ada52009-08-25 11:34:49 -070087 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams67c68442009-08-25 17:09:59 -070088 } else {
89 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsb13ada52009-08-25 11:34:49 -070090 }
Jason Samsd19f10d2009-05-22 14:03:28 -070091}
92
Jason Sams3eaa3382009-06-10 15:04:38 -070093bool Context::runScript(Script *s, uint32_t launchID)
Jason Samsda423d82009-06-09 12:15:30 -070094{
95 ObjectBaseRef<ProgramFragment> frag(mFragment);
96 ObjectBaseRef<ProgramVertex> vtx(mVertex);
97 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
98
Jason Sams3eaa3382009-06-10 15:04:38 -070099 bool ret = s->run(this, launchID);
Jason Samsda423d82009-06-09 12:15:30 -0700100
Jason Sams3eaa3382009-06-10 15:04:38 -0700101 mFragment.set(frag);
102 mVertex.set(vtx);
103 mFragmentStore.set(store);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700104 return ret;
Jason Samsda423d82009-06-09 12:15:30 -0700105}
106
107
Jason Samsa09f11d2009-06-04 17:58:03 -0700108bool Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -0700109{
Jason Samsf4d16062009-08-19 12:17:14 -0700110#if RS_LOG_TIMES
111 timerSet(RS_TIMER_CLEAR_SWAP);
112#endif
Jason Samsda423d82009-06-09 12:15:30 -0700113 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -0700114
Jason Sams9bee51c2009-08-05 13:57:03 -0700115 //glColor4f(1,1,1,1);
116 //glEnable(GL_LIGHT0);
Jason Samsb13ada52009-08-25 11:34:49 -0700117 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -0700118 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
119
Jason Sams928f5cf2009-06-08 18:50:13 -0700120 glClearColor(mRootScript->mEnviroment.mClearColor[0],
121 mRootScript->mEnviroment.mClearColor[1],
122 mRootScript->mEnviroment.mClearColor[2],
123 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsb13ada52009-08-25 11:34:49 -0700124 if (mUseDepth) {
125 glDepthMask(GL_TRUE);
126 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
127 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
128 } else {
129 glClear(GL_COLOR_BUFFER_BIT);
130 }
Jason Sams67c68442009-08-25 17:09:59 -0700131
Jason Sams9bee51c2009-08-05 13:57:03 -0700132#if RS_LOG_TIMES
Jason Samsf4d16062009-08-19 12:17:14 -0700133 timerSet(RS_TIMER_SCRIPT);
Jason Sams9bee51c2009-08-05 13:57:03 -0700134#endif
135 bool ret = runScript(mRootScript.get(), 0);
Jason Sams9bee51c2009-08-05 13:57:03 -0700136 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700137}
138
Jason Samsf4d16062009-08-19 12:17:14 -0700139uint64_t Context::getTime() const
140{
141 struct timespec t;
142 clock_gettime(CLOCK_MONOTONIC, &t);
143 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
144}
145
146void Context::timerReset()
147{
148 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
149 mTimers[ct] = 0;
150 }
151}
152
153void Context::timerInit()
154{
155 mTimeLast = getTime();
156 mTimerActive = RS_TIMER_INTERNAL;
157 timerReset();
158}
159
160void Context::timerSet(Timers tm)
161{
162 uint64_t last = mTimeLast;
163 mTimeLast = getTime();
164 mTimers[mTimerActive] += mTimeLast - last;
165 mTimerActive = tm;
166}
167
168void Context::timerPrint()
169{
170 double total = 0;
171 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
172 total += mTimers[ct];
173 }
174
175 LOGV("RS Time Data: Idle %2.1f (%lli), Internal %2.1f (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli)",
176 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
177 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
178 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
179 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000);
180}
181
Jason Samsd19f10d2009-05-22 14:03:28 -0700182void Context::setupCheck()
183{
184 if (mFragmentStore.get()) {
Jason Samsb13ada52009-08-25 11:34:49 -0700185 mFragmentStore->setupGL(this, &mStateFragmentStore);
Jason Samsd19f10d2009-05-22 14:03:28 -0700186 }
187 if (mFragment.get()) {
Jason Samsb13ada52009-08-25 11:34:49 -0700188 mFragment->setupGL(this, &mStateFragment);
Jason Samsd19f10d2009-05-22 14:03:28 -0700189 }
190 if (mVertex.get()) {
Jason Samsb13ada52009-08-25 11:34:49 -0700191 mVertex->setupGL(this, &mStateVertex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700192 }
193
194}
195
196
197void * Context::threadProc(void *vrsc)
198{
199 Context *rsc = static_cast<Context *>(vrsc);
200
Jason Samsd19f10d2009-05-22 14:03:28 -0700201 rsc->initEGL();
Jason Sams9c54bdb2009-06-17 16:52:59 -0700202
Jason Sams462d11b2009-06-19 16:03:18 -0700203 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
204 if (!tlsStruct) {
205 LOGE("Error allocating tls storage");
206 return NULL;
207 }
208 tlsStruct->mContext = rsc;
209 tlsStruct->mScript = NULL;
210 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
211 if (status) {
212 LOGE("pthread_setspecific %i", status);
213 }
214
Jason Samsb13ada52009-08-25 11:34:49 -0700215 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700216 rsc->setVertex(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700217 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700218 rsc->setFragment(NULL);
Jason Samsb13ada52009-08-25 11:34:49 -0700219 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700220 rsc->setFragmentStore(NULL);
221
Jason Samsd19f10d2009-05-22 14:03:28 -0700222 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700223 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700224 while (!rsc->mExit) {
Jason Samsbc948de2009-08-17 18:35:48 -0700225 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams5f7fc272009-06-18 16:58:42 -0700226 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700227
Jason Sams5f7fc272009-06-18 16:58:42 -0700228 if (mDraw) {
Jason Samsa09f11d2009-06-04 17:58:03 -0700229 mDraw = rsc->runRootScript();
Jason Samsf4d16062009-08-19 12:17:14 -0700230#if RS_LOG_TIMES
231 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
232#endif
Jason Samsb13ada52009-08-25 11:34:49 -0700233 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Samsf4d16062009-08-19 12:17:14 -0700234#if RS_LOG_TIMES
235 rsc->timerSet(RS_TIMER_INTERNAL);
236 rsc->timerPrint();
237 rsc->timerReset();
238#endif
Jason Samsd19f10d2009-05-22 14:03:28 -0700239 }
Jason Samsf4d16062009-08-19 12:17:14 -0700240 if (rsc->mObjDestroy.mNeedToEmpty) {
241 rsc->objDestroyOOBRun();
242 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700243 }
244
Jason Samsf5b45962009-08-25 14:49:07 -0700245 LOGV("RS Thread exiting");
Jason Samsd19f10d2009-05-22 14:03:28 -0700246 glClearColor(0,0,0,0);
247 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsb13ada52009-08-25 11:34:49 -0700248 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
249 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams730ee652009-08-18 17:07:09 -0700250 rsc->objDestroyOOBRun();
Jason Samsf5b45962009-08-25 14:49:07 -0700251 LOGV("RS Thread exited");
Jason Samsd19f10d2009-05-22 14:03:28 -0700252 return NULL;
253}
254
Jason Samsb13ada52009-08-25 11:34:49 -0700255Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700256{
Jason Samsd19f10d2009-05-22 14:03:28 -0700257 dev->addContext(this);
258 mDev = dev;
259 mRunning = false;
260 mExit = false;
Jason Samsb13ada52009-08-25 11:34:49 -0700261 mUseDepth = useDepth;
Jason Samsd19f10d2009-05-22 14:03:28 -0700262
Jason Sams8ad00102009-06-04 14:35:01 -0700263 int status;
264 pthread_attr_t threadAttr;
265
Jason Sams462d11b2009-06-19 16:03:18 -0700266 status = pthread_key_create(&gThreadTLSKey, NULL);
267 if (status) {
268 LOGE("Failed to init thread tls key.");
269 return;
270 }
271
Jason Sams8ad00102009-06-04 14:35:01 -0700272 status = pthread_attr_init(&threadAttr);
273 if (status) {
274 LOGE("Failed to init thread attribute.");
275 return;
276 }
277
278 sched_param sparam;
279 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
280 pthread_attr_setschedparam(&threadAttr, &sparam);
281
Jason Samsf29ca502009-06-23 12:22:47 -0700282 mWndSurface = sur;
283
Jason Sams730ee652009-08-18 17:07:09 -0700284 objDestroyOOBInit();
Jason Samsf4d16062009-08-19 12:17:14 -0700285 timerInit();
Jason Sams730ee652009-08-18 17:07:09 -0700286
Jason Samsf29ca502009-06-23 12:22:47 -0700287 LOGV("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700288 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700289 if (status) {
290 LOGE("Failed to start rs context thread.");
291 }
292
Jason Samsd19f10d2009-05-22 14:03:28 -0700293 while(!mRunning) {
294 sleep(1);
295 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700296
Jason Sams8ad00102009-06-04 14:35:01 -0700297 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700298}
299
300Context::~Context()
301{
Jason Samsf5b45962009-08-25 14:49:07 -0700302 LOGV("Context::~Context");
Jason Samsd19f10d2009-05-22 14:03:28 -0700303 mExit = true;
304 void *res;
305
Jason Samsf5b45962009-08-25 14:49:07 -0700306 mIO.shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -0700307 int status = pthread_join(mThreadId, &res);
Jason Sams730ee652009-08-18 17:07:09 -0700308 objDestroyOOBRun();
Jason Samsd19f10d2009-05-22 14:03:28 -0700309
310 if (mDev) {
311 mDev->removeContext(this);
Jason Sams462d11b2009-06-19 16:03:18 -0700312 pthread_key_delete(gThreadTLSKey);
Jason Samsd19f10d2009-05-22 14:03:28 -0700313 }
Jason Sams730ee652009-08-18 17:07:09 -0700314
315 objDestroyOOBDestroy();
Jason Samsd19f10d2009-05-22 14:03:28 -0700316}
317
Jason Samsd19f10d2009-05-22 14:03:28 -0700318void Context::setRootScript(Script *s)
319{
320 mRootScript.set(s);
321}
322
323void Context::setFragmentStore(ProgramFragmentStore *pfs)
324{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700325 if (pfs == NULL) {
326 mFragmentStore.set(mStateFragmentStore.mDefault);
327 } else {
328 mFragmentStore.set(pfs);
329 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700330}
331
332void Context::setFragment(ProgramFragment *pf)
333{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700334 if (pf == NULL) {
335 mFragment.set(mStateFragment.mDefault);
336 } else {
337 mFragment.set(pf);
338 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700339}
340
341void Context::allocationCheck(const Allocation *a)
342{
343 mVertex->checkUpdatedAllocation(a);
344 mFragment->checkUpdatedAllocation(a);
345 mFragmentStore->checkUpdatedAllocation(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700346}
347
348void Context::setVertex(ProgramVertex *pv)
349{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700350 if (pv == NULL) {
351 mVertex.set(mStateVertex.mDefault);
352 } else {
353 mVertex.set(pv);
354 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700355}
356
Jason Samsd5680f92009-06-10 18:39:40 -0700357void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700358{
359 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700360 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700361 mNames.add(obj);
362}
363
364void Context::removeName(ObjectBase *obj)
365{
366 for(size_t ct=0; ct < mNames.size(); ct++) {
367 if (obj == mNames[ct]) {
368 mNames.removeAt(ct);
369 return;
370 }
371 }
372}
373
374ObjectBase * Context::lookupName(const char *name) const
375{
376 for(size_t ct=0; ct < mNames.size(); ct++) {
377 if (!strcmp(name, mNames[ct]->getName())) {
378 return mNames[ct];
379 }
380 }
381 return NULL;
382}
383
Jason Samsd5680f92009-06-10 18:39:40 -0700384void Context::appendNameDefines(String8 *str) const
385{
386 char buf[256];
387 for (size_t ct=0; ct < mNames.size(); ct++) {
388 str->append("#define NAMED_");
389 str->append(mNames[ct]->getName());
390 str->append(" ");
391 sprintf(buf, "%i\n", (int)mNames[ct]);
392 str->append(buf);
393 }
394}
395
Joe Onoratod7b37742009-08-09 22:57:44 -0700396void Context::appendVarDefines(String8 *str) const
397{
398 char buf[256];
399 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
400 str->append("#define ");
401 str->append(mInt32Defines.keyAt(ct));
402 str->append(" ");
403 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
404 str->append(buf);
405
406 }
407 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
408 str->append("#define ");
409 str->append(mFloatDefines.keyAt(ct));
410 str->append(" ");
411 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
412 str->append(buf);
413 }
414}
415
Jason Sams730ee652009-08-18 17:07:09 -0700416bool Context::objDestroyOOBInit()
417{
418 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
419 if (status) {
420 LOGE("Context::ObjDestroyOOBInit mutex init failure");
421 return false;
422 }
423 return true;
424}
425
426void Context::objDestroyOOBRun()
427{
428 if (mObjDestroy.mNeedToEmpty) {
429 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
430 if (status) {
431 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
432 return;
433 }
434
435 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams07ae4062009-08-27 20:23:34 -0700436 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams730ee652009-08-18 17:07:09 -0700437 }
438 mObjDestroy.mDestroyList.clear();
439 mObjDestroy.mNeedToEmpty = false;
440
441 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
442 if (status) {
443 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
444 }
445 }
446}
447
448void Context::objDestroyOOBDestroy()
449{
450 rsAssert(!mObjDestroy.mNeedToEmpty);
451 pthread_mutex_destroy(&mObjDestroy.mMutex);
452}
453
454void Context::objDestroyAdd(ObjectBase *obj)
455{
456 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
457 if (status) {
458 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
459 return;
460 }
461
462 mObjDestroy.mNeedToEmpty = true;
463 mObjDestroy.mDestroyList.add(obj);
464
465 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
466 if (status) {
467 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
468 }
469}
470
471
Jason Samsd5680f92009-06-10 18:39:40 -0700472
Jason Samsd19f10d2009-05-22 14:03:28 -0700473///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700474//
Jason Samsd19f10d2009-05-22 14:03:28 -0700475
476namespace android {
477namespace renderscript {
478
479
480void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
481{
482 Script *s = static_cast<Script *>(vs);
483 rsc->setRootScript(s);
484}
485
486void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
487{
488 Sampler *s = static_cast<Sampler *>(vs);
489
490 if (slot > RS_MAX_SAMPLER_SLOT) {
491 LOGE("Invalid sampler slot");
492 return;
493 }
494
495 s->bindToContext(&rsc->mStateSampler, slot);
496}
497
498void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
499{
500 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
501 rsc->setFragmentStore(pfs);
502}
503
504void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
505{
506 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
507 rsc->setFragment(pf);
508}
509
510void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
511{
512 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
513 rsc->setVertex(pv);
514}
515
Jason Samsd5680f92009-06-10 18:39:40 -0700516void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700517{
518 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsd5680f92009-06-10 18:39:40 -0700519 rsc->assignName(ob, name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700520}
Jason Samsd19f10d2009-05-22 14:03:28 -0700521
Jason Sams7ce033d2009-08-18 14:14:24 -0700522void rsi_ObjDestroy(Context *rsc, void *obj)
523{
524 ObjectBase *ob = static_cast<ObjectBase *>(obj);
525 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700526 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700527}
528
Joe Onoratod7b37742009-08-09 22:57:44 -0700529void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
530{
531 rsc->addInt32Define(name, value);
532}
533
534void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
535{
536 rsc->addFloatDefine(name, value);
537}
Jason Samsd19f10d2009-05-22 14:03:28 -0700538
539}
540}
541
542
Jason Samsb13ada52009-08-25 11:34:49 -0700543RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700544{
545 Device * dev = static_cast<Device *>(vdev);
Jason Samsb13ada52009-08-25 11:34:49 -0700546 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -0700547 return rsc;
548}
549
550void rsContextDestroy(RsContext vrsc)
551{
552 Context * rsc = static_cast<Context *>(vrsc);
553 delete rsc;
554}
555
Jason Sams730ee652009-08-18 17:07:09 -0700556void rsObjDestroyOOB(RsContext vrsc, void *obj)
557{
558 Context * rsc = static_cast<Context *>(vrsc);
559 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
560}
561