blob: 3ebfdce5eac4569f0ae57572a5512fda050c1420 [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
79 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);
84
85 if (memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
86 LOGE("Error, OpenGL ES Lite not supported");
87 }
88 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
89
90
Jason Sams326e0dd2009-05-22 14:03:28 -070091}
92
Jason Samsa0a1b6f2009-06-10 15:04:38 -070093bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070094{
95 ObjectBaseRef<ProgramFragment> frag(mFragment);
96 ObjectBaseRef<ProgramVertex> vtx(mVertex);
97 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
98
Jason Samsa0a1b6f2009-06-10 15:04:38 -070099 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700100
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700101 mFragment.set(frag);
102 mVertex.set(vtx);
103 mFragmentStore.set(store);
Jason Samsc9d43db2009-07-28 12:02:16 -0700104 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700105}
106
107
Jason Samsa44cb292009-06-04 17:58:03 -0700108bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700109{
Jason Sams24371d92009-08-19 12:17:14 -0700110#if RS_LOG_TIMES
111 timerSet(RS_TIMER_CLEAR_SWAP);
112#endif
Jason Sams10308932009-06-09 12:15:30 -0700113 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700114
Jason Samscfb1d112009-08-05 13:57:03 -0700115 //glColor4f(1,1,1,1);
116 //glEnable(GL_LIGHT0);
Jason Samsafcb25c2009-08-25 11:34:49 -0700117 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
118#if 1
Jason Sams326e0dd2009-05-22 14:03:28 -0700119 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
120
Jason Sams928b7342009-06-08 18:50:13 -0700121 glClearColor(mRootScript->mEnviroment.mClearColor[0],
122 mRootScript->mEnviroment.mClearColor[1],
123 mRootScript->mEnviroment.mClearColor[2],
124 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700125 if (mUseDepth) {
126 glDepthMask(GL_TRUE);
127 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129 } else {
130 glClear(GL_COLOR_BUFFER_BIT);
131 }
132#endif
Jason Samscfb1d112009-08-05 13:57:03 -0700133#if RS_LOG_TIMES
Jason Sams24371d92009-08-19 12:17:14 -0700134 timerSet(RS_TIMER_SCRIPT);
Jason Samscfb1d112009-08-05 13:57:03 -0700135#endif
136 bool ret = runScript(mRootScript.get(), 0);
Jason Samscfb1d112009-08-05 13:57:03 -0700137 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700138}
139
Jason Sams24371d92009-08-19 12:17:14 -0700140uint64_t Context::getTime() const
141{
142 struct timespec t;
143 clock_gettime(CLOCK_MONOTONIC, &t);
144 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
145}
146
147void Context::timerReset()
148{
149 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
150 mTimers[ct] = 0;
151 }
152}
153
154void Context::timerInit()
155{
156 mTimeLast = getTime();
157 mTimerActive = RS_TIMER_INTERNAL;
158 timerReset();
159}
160
161void Context::timerSet(Timers tm)
162{
163 uint64_t last = mTimeLast;
164 mTimeLast = getTime();
165 mTimers[mTimerActive] += mTimeLast - last;
166 mTimerActive = tm;
167}
168
169void Context::timerPrint()
170{
171 double total = 0;
172 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
173 total += mTimers[ct];
174 }
175
176 LOGV("RS Time Data: Idle %2.1f (%lli), Internal %2.1f (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli)",
177 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
178 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
179 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
180 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000);
181}
182
Jason Sams326e0dd2009-05-22 14:03:28 -0700183void Context::setupCheck()
184{
185 if (mFragmentStore.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700186 mFragmentStore->setupGL(this, &mStateFragmentStore);
Jason Sams326e0dd2009-05-22 14:03:28 -0700187 }
188 if (mFragment.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700189 mFragment->setupGL(this, &mStateFragment);
Jason Sams326e0dd2009-05-22 14:03:28 -0700190 }
191 if (mVertex.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700192 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700193 }
194
195}
196
197
198void * Context::threadProc(void *vrsc)
199{
200 Context *rsc = static_cast<Context *>(vrsc);
201
Jason Sams326e0dd2009-05-22 14:03:28 -0700202 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700203
Jason Samse5769102009-06-19 16:03:18 -0700204 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
205 if (!tlsStruct) {
206 LOGE("Error allocating tls storage");
207 return NULL;
208 }
209 tlsStruct->mContext = rsc;
210 tlsStruct->mScript = NULL;
211 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
212 if (status) {
213 LOGE("pthread_setspecific %i", status);
214 }
215
Jason Samsafcb25c2009-08-25 11:34:49 -0700216 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700217 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700218 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700219 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700220 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700221 rsc->setFragmentStore(NULL);
222
Jason Sams326e0dd2009-05-22 14:03:28 -0700223 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700224 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700225 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700226 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700227 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700228
Jason Sams732f1c02009-06-18 16:58:42 -0700229 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700230 mDraw = rsc->runRootScript();
Jason Sams24371d92009-08-19 12:17:14 -0700231#if RS_LOG_TIMES
232 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
233#endif
Jason Samsafcb25c2009-08-25 11:34:49 -0700234 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams24371d92009-08-19 12:17:14 -0700235#if RS_LOG_TIMES
236 rsc->timerSet(RS_TIMER_INTERNAL);
237 rsc->timerPrint();
238 rsc->timerReset();
239#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700240 }
Jason Sams24371d92009-08-19 12:17:14 -0700241 if (rsc->mObjDestroy.mNeedToEmpty) {
242 rsc->objDestroyOOBRun();
243 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700244 }
245
Jason Sams8c0ee652009-08-25 14:49:07 -0700246 LOGV("RS Thread exiting");
Jason Sams326e0dd2009-05-22 14:03:28 -0700247 glClearColor(0,0,0,0);
248 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700249 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
250 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700251 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700252 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700253 return NULL;
254}
255
Jason Samsafcb25c2009-08-25 11:34:49 -0700256Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700257{
Jason Sams326e0dd2009-05-22 14:03:28 -0700258 dev->addContext(this);
259 mDev = dev;
260 mRunning = false;
261 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700262 mUseDepth = useDepth;
Jason Sams326e0dd2009-05-22 14:03:28 -0700263
Jason Samsa658e902009-06-04 14:35:01 -0700264 int status;
265 pthread_attr_t threadAttr;
266
Jason Samse5769102009-06-19 16:03:18 -0700267 status = pthread_key_create(&gThreadTLSKey, NULL);
268 if (status) {
269 LOGE("Failed to init thread tls key.");
270 return;
271 }
272
Jason Samsa658e902009-06-04 14:35:01 -0700273 status = pthread_attr_init(&threadAttr);
274 if (status) {
275 LOGE("Failed to init thread attribute.");
276 return;
277 }
278
279 sched_param sparam;
280 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
281 pthread_attr_setschedparam(&threadAttr, &sparam);
282
Jason Sams992a0b72009-06-23 12:22:47 -0700283 mWndSurface = sur;
284
Jason Sams50869382009-08-18 17:07:09 -0700285 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700286 timerInit();
Jason Sams50869382009-08-18 17:07:09 -0700287
Jason Sams992a0b72009-06-23 12:22:47 -0700288 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700289 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700290 if (status) {
291 LOGE("Failed to start rs context thread.");
292 }
293
Jason Sams326e0dd2009-05-22 14:03:28 -0700294 while(!mRunning) {
295 sleep(1);
296 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700297
Jason Samsa658e902009-06-04 14:35:01 -0700298 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700299}
300
301Context::~Context()
302{
Jason Sams8c0ee652009-08-25 14:49:07 -0700303 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700304 mExit = true;
305 void *res;
306
Jason Sams8c0ee652009-08-25 14:49:07 -0700307 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700308 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700309 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700310
311 if (mDev) {
312 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700313 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700314 }
Jason Sams50869382009-08-18 17:07:09 -0700315
316 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700317}
318
Jason Sams326e0dd2009-05-22 14:03:28 -0700319void Context::setRootScript(Script *s)
320{
321 mRootScript.set(s);
322}
323
324void Context::setFragmentStore(ProgramFragmentStore *pfs)
325{
Jason Sams8ce125b2009-06-17 16:52:59 -0700326 if (pfs == NULL) {
327 mFragmentStore.set(mStateFragmentStore.mDefault);
328 } else {
329 mFragmentStore.set(pfs);
330 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700331}
332
333void Context::setFragment(ProgramFragment *pf)
334{
Jason Sams8ce125b2009-06-17 16:52:59 -0700335 if (pf == NULL) {
336 mFragment.set(mStateFragment.mDefault);
337 } else {
338 mFragment.set(pf);
339 }
Jason Samscfb1d112009-08-05 13:57:03 -0700340}
341
342void Context::allocationCheck(const Allocation *a)
343{
344 mVertex->checkUpdatedAllocation(a);
345 mFragment->checkUpdatedAllocation(a);
346 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700347}
348
349void Context::setVertex(ProgramVertex *pv)
350{
Jason Sams8ce125b2009-06-17 16:52:59 -0700351 if (pv == NULL) {
352 mVertex.set(mStateVertex.mDefault);
353 } else {
354 mVertex.set(pv);
355 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700356}
357
Jason Samsa4a54e42009-06-10 18:39:40 -0700358void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700359{
360 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700361 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700362 mNames.add(obj);
363}
364
365void Context::removeName(ObjectBase *obj)
366{
367 for(size_t ct=0; ct < mNames.size(); ct++) {
368 if (obj == mNames[ct]) {
369 mNames.removeAt(ct);
370 return;
371 }
372 }
373}
374
375ObjectBase * Context::lookupName(const char *name) const
376{
377 for(size_t ct=0; ct < mNames.size(); ct++) {
378 if (!strcmp(name, mNames[ct]->getName())) {
379 return mNames[ct];
380 }
381 }
382 return NULL;
383}
384
Jason Samsa4a54e42009-06-10 18:39:40 -0700385void Context::appendNameDefines(String8 *str) const
386{
387 char buf[256];
388 for (size_t ct=0; ct < mNames.size(); ct++) {
389 str->append("#define NAMED_");
390 str->append(mNames[ct]->getName());
391 str->append(" ");
392 sprintf(buf, "%i\n", (int)mNames[ct]);
393 str->append(buf);
394 }
395}
396
Joe Onorato57b79ce2009-08-09 22:57:44 -0700397void Context::appendVarDefines(String8 *str) const
398{
399 char buf[256];
400 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
401 str->append("#define ");
402 str->append(mInt32Defines.keyAt(ct));
403 str->append(" ");
404 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
405 str->append(buf);
406
407 }
408 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
409 str->append("#define ");
410 str->append(mFloatDefines.keyAt(ct));
411 str->append(" ");
412 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
413 str->append(buf);
414 }
415}
416
Jason Sams50869382009-08-18 17:07:09 -0700417bool Context::objDestroyOOBInit()
418{
419 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
420 if (status) {
421 LOGE("Context::ObjDestroyOOBInit mutex init failure");
422 return false;
423 }
424 return true;
425}
426
427void Context::objDestroyOOBRun()
428{
429 if (mObjDestroy.mNeedToEmpty) {
430 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
431 if (status) {
432 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
433 return;
434 }
435
436 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
437 mObjDestroy.mDestroyList[ct]->decRef();
438 }
439 mObjDestroy.mDestroyList.clear();
440 mObjDestroy.mNeedToEmpty = false;
441
442 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
443 if (status) {
444 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
445 }
446 }
447}
448
449void Context::objDestroyOOBDestroy()
450{
451 rsAssert(!mObjDestroy.mNeedToEmpty);
452 pthread_mutex_destroy(&mObjDestroy.mMutex);
453}
454
455void Context::objDestroyAdd(ObjectBase *obj)
456{
457 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
458 if (status) {
459 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
460 return;
461 }
462
463 mObjDestroy.mNeedToEmpty = true;
464 mObjDestroy.mDestroyList.add(obj);
465
466 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
467 if (status) {
468 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
469 }
470}
471
472
Jason Samsa4a54e42009-06-10 18:39:40 -0700473
Jason Sams326e0dd2009-05-22 14:03:28 -0700474///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700475//
Jason Sams326e0dd2009-05-22 14:03:28 -0700476
477namespace android {
478namespace renderscript {
479
480
481void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
482{
483 Script *s = static_cast<Script *>(vs);
484 rsc->setRootScript(s);
485}
486
487void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
488{
489 Sampler *s = static_cast<Sampler *>(vs);
490
491 if (slot > RS_MAX_SAMPLER_SLOT) {
492 LOGE("Invalid sampler slot");
493 return;
494 }
495
496 s->bindToContext(&rsc->mStateSampler, slot);
497}
498
499void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
500{
501 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
502 rsc->setFragmentStore(pfs);
503}
504
505void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
506{
507 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
508 rsc->setFragment(pf);
509}
510
511void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
512{
513 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
514 rsc->setVertex(pv);
515}
516
Jason Samsa4a54e42009-06-10 18:39:40 -0700517void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700518{
519 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700520 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700521}
Jason Sams326e0dd2009-05-22 14:03:28 -0700522
Jason Sams707aaf32009-08-18 14:14:24 -0700523void rsi_ObjDestroy(Context *rsc, void *obj)
524{
525 ObjectBase *ob = static_cast<ObjectBase *>(obj);
526 rsc->removeName(ob);
527 ob->decRef();
528}
529
Joe Onorato57b79ce2009-08-09 22:57:44 -0700530void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
531{
532 rsc->addInt32Define(name, value);
533}
534
535void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
536{
537 rsc->addFloatDefine(name, value);
538}
Jason Sams326e0dd2009-05-22 14:03:28 -0700539
540}
541}
542
543
Jason Samsafcb25c2009-08-25 11:34:49 -0700544RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700545{
546 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700547 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700548 return rsc;
549}
550
551void rsContextDestroy(RsContext vrsc)
552{
553 Context * rsc = static_cast<Context *>(vrsc);
554 delete rsc;
555}
556
Jason Sams50869382009-08-18 17:07:09 -0700557void rsObjDestroyOOB(RsContext vrsc, void *obj)
558{
559 Context * rsc = static_cast<Context *>(vrsc);
560 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
561}
562