blob: 04f6e070af333565a9d191cba488543e5dab3705 [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 }
Jason Sams9397e302009-08-27 20:23:34 -070048
Jason Samsafcb25c2009-08-25 11:34:49 -070049 configAttribsPtr[0] = EGL_NONE;
50 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070051
Jason Samsafcb25c2009-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 Sams9397e302009-08-27 20:23:34 -070057 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-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 Sams9397e302009-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 Samsafcb25c2009-08-25 11:34:49 -070085
Jason Sams306fb232009-08-25 17:09:59 -070086 if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
Jason Samsafcb25c2009-08-25 11:34:49 -070087 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams306fb232009-08-25 17:09:59 -070088 } else {
89 sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Samsafcb25c2009-08-25 11:34:49 -070090 }
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);
Jason Sams326e0dd2009-05-22 14:03:28 -0700118 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Jason Sams1d54f102009-09-03 15:43:13 -0700119 glEnable(GL_POINT_SMOOTH);
Jason Sams326e0dd2009-05-22 14:03:28 -0700120
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 }
Jason Sams306fb232009-08-25 17:09:59 -0700132
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();
Jason Sams1d54f102009-09-03 15:43:13 -0700157 mTimeFrame = mTimeLast;
158 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700159 mTimerActive = RS_TIMER_INTERNAL;
160 timerReset();
161}
162
Jason Sams1d54f102009-09-03 15:43:13 -0700163void Context::timerFrame()
164{
165 mTimeLastFrame = mTimeFrame;
166 mTimeFrame = getTime();
167}
168
Jason Sams24371d92009-08-19 12:17:14 -0700169void Context::timerSet(Timers tm)
170{
171 uint64_t last = mTimeLast;
172 mTimeLast = getTime();
173 mTimers[mTimerActive] += mTimeLast - last;
174 mTimerActive = tm;
175}
176
177void Context::timerPrint()
178{
179 double total = 0;
180 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
181 total += mTimers[ct];
182 }
Jason Sams1d54f102009-09-03 15:43:13 -0700183 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams24371d92009-08-19 12:17:14 -0700184
Jason Sams1d54f102009-09-03 15:43:13 -0700185 LOGV("RS: Frame (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli), Idle %2.1f (%lli), Internal %2.1f (%lli)",
186 frame / 1000000,
Jason Sams24371d92009-08-19 12:17:14 -0700187 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000,
Jason Samsa57c0a72009-09-04 14:42:41 -0700188 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000,
189 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
190 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
Jason Sams24371d92009-08-19 12:17:14 -0700191}
192
Jason Sams326e0dd2009-05-22 14:03:28 -0700193void Context::setupCheck()
194{
195 if (mFragmentStore.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700196 mFragmentStore->setupGL(this, &mStateFragmentStore);
Jason Sams326e0dd2009-05-22 14:03:28 -0700197 }
198 if (mFragment.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700199 mFragment->setupGL(this, &mStateFragment);
Jason Sams326e0dd2009-05-22 14:03:28 -0700200 }
201 if (mVertex.get()) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700202 mVertex->setupGL(this, &mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700203 }
204
205}
206
207
208void * Context::threadProc(void *vrsc)
209{
210 Context *rsc = static_cast<Context *>(vrsc);
211
Jason Sams326e0dd2009-05-22 14:03:28 -0700212 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700213
Jason Samse5769102009-06-19 16:03:18 -0700214 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
215 if (!tlsStruct) {
216 LOGE("Error allocating tls storage");
217 return NULL;
218 }
219 tlsStruct->mContext = rsc;
220 tlsStruct->mScript = NULL;
221 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
222 if (status) {
223 LOGE("pthread_setspecific %i", status);
224 }
225
Jason Samsafcb25c2009-08-25 11:34:49 -0700226 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700227 rsc->setVertex(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700228 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700229 rsc->setFragment(NULL);
Jason Samsafcb25c2009-08-25 11:34:49 -0700230 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
Jason Sams8ce125b2009-06-17 16:52:59 -0700231 rsc->setFragmentStore(NULL);
232
Jason Sams326e0dd2009-05-22 14:03:28 -0700233 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700234 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700235 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700236 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700237 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700238
Jason Sams732f1c02009-06-18 16:58:42 -0700239 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700240 mDraw = rsc->runRootScript();
Jason Sams24371d92009-08-19 12:17:14 -0700241#if RS_LOG_TIMES
242 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
243#endif
Jason Samsafcb25c2009-08-25 11:34:49 -0700244 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams24371d92009-08-19 12:17:14 -0700245#if RS_LOG_TIMES
Jason Sams1d54f102009-09-03 15:43:13 -0700246 rsc->timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700247 rsc->timerSet(RS_TIMER_INTERNAL);
248 rsc->timerPrint();
249 rsc->timerReset();
250#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700251 }
Jason Sams24371d92009-08-19 12:17:14 -0700252 if (rsc->mObjDestroy.mNeedToEmpty) {
253 rsc->objDestroyOOBRun();
254 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700255 }
256
Jason Sams8c0ee652009-08-25 14:49:07 -0700257 LOGV("RS Thread exiting");
Jason Sams326e0dd2009-05-22 14:03:28 -0700258 glClearColor(0,0,0,0);
259 glClear(GL_COLOR_BUFFER_BIT);
Jason Samsafcb25c2009-08-25 11:34:49 -0700260 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
261 eglTerminate(rsc->mEGL.mDisplay);
Jason Sams50869382009-08-18 17:07:09 -0700262 rsc->objDestroyOOBRun();
Jason Sams8c0ee652009-08-25 14:49:07 -0700263 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700264 return NULL;
265}
266
Jason Samsafcb25c2009-08-25 11:34:49 -0700267Context::Context(Device *dev, Surface *sur, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700268{
Jason Sams326e0dd2009-05-22 14:03:28 -0700269 dev->addContext(this);
270 mDev = dev;
271 mRunning = false;
272 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700273 mUseDepth = useDepth;
Jason Sams326e0dd2009-05-22 14:03:28 -0700274
Jason Samsa658e902009-06-04 14:35:01 -0700275 int status;
276 pthread_attr_t threadAttr;
277
Jason Samse5769102009-06-19 16:03:18 -0700278 status = pthread_key_create(&gThreadTLSKey, NULL);
279 if (status) {
280 LOGE("Failed to init thread tls key.");
281 return;
282 }
283
Jason Samsa658e902009-06-04 14:35:01 -0700284 status = pthread_attr_init(&threadAttr);
285 if (status) {
286 LOGE("Failed to init thread attribute.");
287 return;
288 }
289
290 sched_param sparam;
291 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
292 pthread_attr_setschedparam(&threadAttr, &sparam);
293
Jason Sams992a0b72009-06-23 12:22:47 -0700294 mWndSurface = sur;
295
Jason Sams50869382009-08-18 17:07:09 -0700296 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700297 timerInit();
Jason Sams50869382009-08-18 17:07:09 -0700298
Jason Sams992a0b72009-06-23 12:22:47 -0700299 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700300 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700301 if (status) {
302 LOGE("Failed to start rs context thread.");
303 }
304
Jason Sams326e0dd2009-05-22 14:03:28 -0700305 while(!mRunning) {
306 sleep(1);
307 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700308
Jason Samsa658e902009-06-04 14:35:01 -0700309 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700310}
311
312Context::~Context()
313{
Jason Sams8c0ee652009-08-25 14:49:07 -0700314 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700315 mExit = true;
316 void *res;
317
Jason Sams8c0ee652009-08-25 14:49:07 -0700318 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700319 int status = pthread_join(mThreadId, &res);
Jason Sams50869382009-08-18 17:07:09 -0700320 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700321
322 if (mDev) {
323 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700324 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700325 }
Jason Sams50869382009-08-18 17:07:09 -0700326
327 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700328}
329
Jason Sams326e0dd2009-05-22 14:03:28 -0700330void Context::setRootScript(Script *s)
331{
332 mRootScript.set(s);
333}
334
335void Context::setFragmentStore(ProgramFragmentStore *pfs)
336{
Jason Sams8ce125b2009-06-17 16:52:59 -0700337 if (pfs == NULL) {
338 mFragmentStore.set(mStateFragmentStore.mDefault);
339 } else {
340 mFragmentStore.set(pfs);
341 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700342}
343
344void Context::setFragment(ProgramFragment *pf)
345{
Jason Sams8ce125b2009-06-17 16:52:59 -0700346 if (pf == NULL) {
347 mFragment.set(mStateFragment.mDefault);
348 } else {
349 mFragment.set(pf);
350 }
Jason Samscfb1d112009-08-05 13:57:03 -0700351}
352
353void Context::allocationCheck(const Allocation *a)
354{
355 mVertex->checkUpdatedAllocation(a);
356 mFragment->checkUpdatedAllocation(a);
357 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700358}
359
360void Context::setVertex(ProgramVertex *pv)
361{
Jason Sams8ce125b2009-06-17 16:52:59 -0700362 if (pv == NULL) {
363 mVertex.set(mStateVertex.mDefault);
364 } else {
365 mVertex.set(pv);
366 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700367}
368
Jason Samsa4a54e42009-06-10 18:39:40 -0700369void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700370{
371 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700372 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700373 mNames.add(obj);
374}
375
376void Context::removeName(ObjectBase *obj)
377{
378 for(size_t ct=0; ct < mNames.size(); ct++) {
379 if (obj == mNames[ct]) {
380 mNames.removeAt(ct);
381 return;
382 }
383 }
384}
385
386ObjectBase * Context::lookupName(const char *name) const
387{
388 for(size_t ct=0; ct < mNames.size(); ct++) {
389 if (!strcmp(name, mNames[ct]->getName())) {
390 return mNames[ct];
391 }
392 }
393 return NULL;
394}
395
Jason Samsa4a54e42009-06-10 18:39:40 -0700396void Context::appendNameDefines(String8 *str) const
397{
398 char buf[256];
399 for (size_t ct=0; ct < mNames.size(); ct++) {
400 str->append("#define NAMED_");
401 str->append(mNames[ct]->getName());
402 str->append(" ");
403 sprintf(buf, "%i\n", (int)mNames[ct]);
404 str->append(buf);
405 }
406}
407
Joe Onorato57b79ce2009-08-09 22:57:44 -0700408void Context::appendVarDefines(String8 *str) const
409{
410 char buf[256];
411 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
412 str->append("#define ");
413 str->append(mInt32Defines.keyAt(ct));
414 str->append(" ");
415 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
416 str->append(buf);
417
418 }
419 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
420 str->append("#define ");
421 str->append(mFloatDefines.keyAt(ct));
422 str->append(" ");
423 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
424 str->append(buf);
425 }
426}
427
Jason Sams50869382009-08-18 17:07:09 -0700428bool Context::objDestroyOOBInit()
429{
430 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
431 if (status) {
432 LOGE("Context::ObjDestroyOOBInit mutex init failure");
433 return false;
434 }
435 return true;
436}
437
438void Context::objDestroyOOBRun()
439{
440 if (mObjDestroy.mNeedToEmpty) {
441 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
442 if (status) {
443 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
444 return;
445 }
446
447 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700448 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700449 }
450 mObjDestroy.mDestroyList.clear();
451 mObjDestroy.mNeedToEmpty = false;
452
453 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
454 if (status) {
455 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
456 }
457 }
458}
459
460void Context::objDestroyOOBDestroy()
461{
462 rsAssert(!mObjDestroy.mNeedToEmpty);
463 pthread_mutex_destroy(&mObjDestroy.mMutex);
464}
465
466void Context::objDestroyAdd(ObjectBase *obj)
467{
468 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
469 if (status) {
470 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
471 return;
472 }
473
474 mObjDestroy.mNeedToEmpty = true;
475 mObjDestroy.mDestroyList.add(obj);
476
477 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
478 if (status) {
479 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
480 }
481}
482
483
Jason Samsa4a54e42009-06-10 18:39:40 -0700484
Jason Sams326e0dd2009-05-22 14:03:28 -0700485///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700486//
Jason Sams326e0dd2009-05-22 14:03:28 -0700487
488namespace android {
489namespace renderscript {
490
491
492void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
493{
494 Script *s = static_cast<Script *>(vs);
495 rsc->setRootScript(s);
496}
497
498void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
499{
500 Sampler *s = static_cast<Sampler *>(vs);
501
502 if (slot > RS_MAX_SAMPLER_SLOT) {
503 LOGE("Invalid sampler slot");
504 return;
505 }
506
507 s->bindToContext(&rsc->mStateSampler, slot);
508}
509
510void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
511{
512 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
513 rsc->setFragmentStore(pfs);
514}
515
516void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
517{
518 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
519 rsc->setFragment(pf);
520}
521
522void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
523{
524 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
525 rsc->setVertex(pv);
526}
527
Jason Samsa4a54e42009-06-10 18:39:40 -0700528void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700529{
530 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700531 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700532}
Jason Sams326e0dd2009-05-22 14:03:28 -0700533
Jason Sams707aaf32009-08-18 14:14:24 -0700534void rsi_ObjDestroy(Context *rsc, void *obj)
535{
536 ObjectBase *ob = static_cast<ObjectBase *>(obj);
537 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700538 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700539}
540
Joe Onorato57b79ce2009-08-09 22:57:44 -0700541void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
542{
543 rsc->addInt32Define(name, value);
544}
545
546void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
547{
548 rsc->addFloatDefine(name, value);
549}
Jason Sams326e0dd2009-05-22 14:03:28 -0700550
551}
552}
553
554
Jason Samsafcb25c2009-08-25 11:34:49 -0700555RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700556{
557 Device * dev = static_cast<Device *>(vdev);
Jason Samsafcb25c2009-08-25 11:34:49 -0700558 Context *rsc = new Context(dev, (Surface *)sur, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700559 return rsc;
560}
561
562void rsContextDestroy(RsContext vrsc)
563{
564 Context * rsc = static_cast<Context *>(vrsc);
565 delete rsc;
566}
567
Jason Sams50869382009-08-18 17:07:09 -0700568void rsObjDestroyOOB(RsContext vrsc, void *obj)
569{
570 Context * rsc = static_cast<Context *>(vrsc);
571 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
572}
573