blob: e897d00b2fb1fd7113bcc335636d5adb2446e1f4 [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>
Mathias Agopian9b97c292010-02-12 12:00:38 -080022#include <ui/egl/android_natives.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070023
Jason Sams15832442009-11-15 12:14:26 -080024#include <sys/types.h>
25#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070026#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080027
Joe Onorato76371ff2009-09-23 16:37:36 -070028#include <cutils/properties.h>
29
Jason Sams1aa5a4e2009-06-22 17:15:15 -070030#include <GLES/gl.h>
31#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070034
Jason Sams15832442009-11-15 12:14:26 -080035#include <cutils/sched_policy.h>
36
Jason Sams326e0dd2009-05-22 14:03:28 -070037using namespace android;
38using namespace android::renderscript;
39
Jason Samse5769102009-06-19 16:03:18 -070040pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070041uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070042uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070043pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070044
Jason Sams33b6e3b2009-10-27 14:44:31 -070045static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
46 if (returnVal != EGL_TRUE) {
47 fprintf(stderr, "%s() returned %d\n", op, returnVal);
48 }
49
50 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
51 = eglGetError()) {
52 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
53 error);
54 }
55}
56
Jason Samsc460e552009-11-25 13:22:07 -080057void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070058{
Jason Samsafcb25c2009-08-25 11:34:49 -070059 mEGL.mNumConfigs = -1;
60 EGLint configAttribs[128];
61 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080062 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070063
Jason Samsafcb25c2009-08-25 11:34:49 -070064 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsafcb25c2009-08-25 11:34:49 -070066 configAttribsPtr[0] = EGL_SURFACE_TYPE;
67 configAttribsPtr[1] = EGL_WINDOW_BIT;
68 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070069
Jason Samsc460e552009-11-25 13:22:07 -080070 if (useGL2) {
71 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
72 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
73 configAttribsPtr += 2;
74 }
75
Jason Samsafcb25c2009-08-25 11:34:49 -070076 if (mUseDepth) {
77 configAttribsPtr[0] = EGL_DEPTH_SIZE;
78 configAttribsPtr[1] = 16;
79 configAttribsPtr += 2;
80 }
Jason Sams9397e302009-08-27 20:23:34 -070081
Jason Sams5fd09d82009-09-23 13:57:02 -070082 if (mDev->mForceSW) {
83 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
84 configAttribsPtr[1] = EGL_SLOW_CONFIG;
85 configAttribsPtr += 2;
86 }
87
Jason Samsafcb25c2009-08-25 11:34:49 -070088 configAttribsPtr[0] = EGL_NONE;
89 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070090
Jason Sams6d751ef2009-10-08 12:55:06 -070091 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070092 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070093 checkEglError("eglGetDisplay");
94
Jason Samsafcb25c2009-08-25 11:34:49 -070095 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070096 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070097
98 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
99 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -0700100 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -0700101 }
102 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
103
Jason Samsafcb25c2009-08-25 11:34:49 -0700104
Jason Samsc460e552009-11-25 13:22:07 -0800105 if (useGL2) {
106 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
107 } else {
108 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
109 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700110 checkEglError("eglCreateContext");
111 if (mEGL.mContext == EGL_NO_CONTEXT) {
112 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
113 }
114 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams33b6e3b2009-10-27 14:44:31 -0700117void Context::deinitEGL()
118{
Jason Sams613cad12009-11-12 15:10:25 -0800119 LOGV("deinitEGL");
120 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700121 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
122 checkEglError("eglDestroyContext");
123
124 gGLContextCount--;
125 if (!gGLContextCount) {
126 eglTerminate(mEGL.mDisplay);
127 }
128}
129
130
Jason Samsc61346b2010-05-28 18:23:22 -0700131uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700132{
133 ObjectBaseRef<ProgramFragment> frag(mFragment);
134 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700135 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700136 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700137 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700138
Jason Samsc61346b2010-05-28 18:23:22 -0700139 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700140
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700141 mFragment.set(frag);
142 mVertex.set(vtx);
143 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700144 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700145 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700146 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700147}
148
Jason Samsd01d9702009-12-23 14:35:29 -0800149void Context::checkError(const char *msg) const
150{
151 GLenum err = glGetError();
152 if (err != GL_NO_ERROR) {
153 LOGE("GL Error, 0x%x, from %s", err, msg);
154 }
155}
Jason Sams10308932009-06-09 12:15:30 -0700156
Jason Sams2dca84d2009-12-09 11:05:45 -0800157uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700158{
Jason Sams771565f2010-05-14 15:30:29 -0700159 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700160
Jason Sams2dca84d2009-12-09 11:05:45 -0800161 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700162 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700163 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700164
Jason Samsd01d9702009-12-23 14:35:29 -0800165 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800166 if (mError != RS_ERROR_NONE) {
167 // If we have an error condition we stop rendering until
168 // somthing changes that might fix it.
169 ret = 0;
170 }
Jason Samscfb1d112009-08-05 13:57:03 -0700171 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700172}
173
Jason Sams24371d92009-08-19 12:17:14 -0700174uint64_t Context::getTime() const
175{
176 struct timespec t;
177 clock_gettime(CLOCK_MONOTONIC, &t);
178 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
179}
180
181void Context::timerReset()
182{
183 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
184 mTimers[ct] = 0;
185 }
186}
187
188void Context::timerInit()
189{
190 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700191 mTimeFrame = mTimeLast;
192 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700193 mTimerActive = RS_TIMER_INTERNAL;
194 timerReset();
195}
196
Jason Sams1d54f102009-09-03 15:43:13 -0700197void Context::timerFrame()
198{
199 mTimeLastFrame = mTimeFrame;
200 mTimeFrame = getTime();
201}
202
Jason Sams24371d92009-08-19 12:17:14 -0700203void Context::timerSet(Timers tm)
204{
205 uint64_t last = mTimeLast;
206 mTimeLast = getTime();
207 mTimers[mTimerActive] += mTimeLast - last;
208 mTimerActive = tm;
209}
210
211void Context::timerPrint()
212{
213 double total = 0;
214 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
215 total += mTimers[ct];
216 }
Jason Sams1d54f102009-09-03 15:43:13 -0700217 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800218 mTimeMSLastFrame = frame / 1000000;
219 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
220 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700221
Jason Sams2dca84d2009-12-09 11:05:45 -0800222
223 if (props.mLogTimes) {
224 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
225 mTimeMSLastFrame,
226 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
227 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
228 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
229 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
230 }
Jason Sams24371d92009-08-19 12:17:14 -0700231}
232
Jason Samsa2cf7552010-03-03 13:03:18 -0800233bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700234{
Jason Samsc460e552009-11-25 13:22:07 -0800235 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800236 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
237 LOGE("Context::setupCheck() 1 fail");
238 return false;
239 }
Jason Samsc460e552009-11-25 13:22:07 -0800240
241 mFragmentStore->setupGL2(this, &mStateFragmentStore);
242 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
243 mRaster->setupGL2(this, &mStateRaster);
244 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
245
246 } else {
247 mFragmentStore->setupGL(this, &mStateFragmentStore);
248 mFragment->setupGL(this, &mStateFragment);
249 mRaster->setupGL(this, &mStateRaster);
250 mVertex->setupGL(this, &mStateVertex);
251 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800252 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700253}
254
Jason Sams1fddd902009-09-25 15:25:00 -0700255static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700256{
257 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700258 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 return 0 != strcmp(buf, "0");
260}
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
262void * Context::threadProc(void *vrsc)
263{
264 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800265 rsc->mNativeThreadId = gettid();
266
267 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800268 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700269
Jason Sams1fddd902009-09-25 15:25:00 -0700270 rsc->props.mLogTimes = getProp("debug.rs.profile");
271 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800272 rsc->props.mLogObjects = getProp("debug.rs.object");
273 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700274
Jason Samse5769102009-06-19 16:03:18 -0700275 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
276 if (!tlsStruct) {
277 LOGE("Error allocating tls storage");
278 return NULL;
279 }
280 tlsStruct->mContext = rsc;
281 tlsStruct->mScript = NULL;
282 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
283 if (status) {
284 LOGE("pthread_setspecific %i", status);
285 }
286
Jason Sams4820e8b2010-02-09 16:05:07 -0800287 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700288 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800289 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700290 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800291 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700292 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800293 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700294 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800295 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700296 rsc->mStateFont.init(rsc);
297 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800298 rsc->mStateVertexArray.init(rsc);
299 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700300
Jason Sams326e0dd2009-05-22 14:03:28 -0700301 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700302 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700303 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700304 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700305 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800306 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700307
Jason Sams2dca84d2009-12-09 11:05:45 -0800308 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800309 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800310 targetTime = rsc->runRootScript();
311 mDraw = targetTime && !rsc->mPaused;
312 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700313 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800314 rsc->timerFrame();
315 rsc->timerSet(RS_TIMER_INTERNAL);
316 rsc->timerPrint();
317 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700318 }
Jason Sams24371d92009-08-19 12:17:14 -0700319 if (rsc->mObjDestroy.mNeedToEmpty) {
320 rsc->objDestroyOOBRun();
321 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800322 if (rsc->mThreadPriority > 0 && targetTime) {
323 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
324 if (t > 0) {
325 usleep(t);
326 }
327 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700328 }
329
Jason Sams8c0ee652009-08-25 14:49:07 -0700330 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800331 if (rsc->mIsGraphicsContext) {
332 rsc->mRaster.clear();
333 rsc->mFragment.clear();
334 rsc->mVertex.clear();
335 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700336 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800337 rsc->mRootScript.clear();
338 rsc->mStateRaster.deinit(rsc);
339 rsc->mStateVertex.deinit(rsc);
340 rsc->mStateFragment.deinit(rsc);
341 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700342 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800343 }
Jason Samse514b452009-09-25 14:51:22 -0700344 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700345
Jason Samse402ed32009-11-03 11:25:42 -0800346 rsc->mObjDestroy.mNeedToEmpty = true;
347 rsc->objDestroyOOBRun();
348
Jason Sams4820e8b2010-02-09 16:05:07 -0800349 if (rsc->mIsGraphicsContext) {
350 pthread_mutex_lock(&gInitMutex);
351 rsc->deinitEGL();
352 pthread_mutex_unlock(&gInitMutex);
353 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700354
Jason Sams8c0ee652009-08-25 14:49:07 -0700355 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700356 return NULL;
357}
358
Jason Sams7bf29dd2010-07-19 15:38:19 -0700359void * Context::helperThreadProc(void *vrsc)
360{
361 Context *rsc = static_cast<Context *>(vrsc);
362 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
363
Jason Sams18133402010-07-20 15:09:00 -0700364 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700365
366 rsc->mWorkers.mLaunchSignals[idx].init();
367 rsc->mWorkers.mNativeThreadId[idx] = gettid();
368
369 //cpu_set_t cpset[16];
370 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
371 //LOGE("ret = %i", ret);
372
373//sched_setaffinity
374
375 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
376 while(rsc->mRunning) {
377 rsc->mWorkers.mLaunchSignals[idx].wait();
378 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700379 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
380 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700381 android_atomic_dec(&rsc->mWorkers.mRunningCount);
382 rsc->mWorkers.mCompleteSignal.set();
383 }
Jason Sams18133402010-07-20 15:09:00 -0700384
385 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700386 return NULL;
387}
388
389void Context::launchThreads(WorkerCallback_t cbk, void *data)
390{
391 mWorkers.mLaunchData = data;
392 mWorkers.mLaunchCallback = cbk;
393 mWorkers.mRunningCount = (int)mWorkers.mCount;
394 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
395 mWorkers.mLaunchSignals[ct].set();
396 }
397 while(mWorkers.mRunningCount) {
398 mWorkers.mCompleteSignal.wait();
399 }
400}
401
Jason Sams15832442009-11-15 12:14:26 -0800402void Context::setPriority(int32_t p)
403{
404 // Note: If we put this in the proper "background" policy
405 // the wallpapers can become completly unresponsive at times.
406 // This is probably not what we want for something the user is actively
407 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800408 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800409#if 0
410 SchedPolicy pol = SP_FOREGROUND;
411 if (p > 0) {
412 pol = SP_BACKGROUND;
413 }
414 if (!set_sched_policy(mNativeThreadId, pol)) {
415 // success; reset the priority as well
416 }
417#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700418 setpriority(PRIO_PROCESS, mNativeThreadId, p);
419 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
420 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
421 }
Jason Sams15832442009-11-15 12:14:26 -0800422#endif
423}
424
Jason Sams4820e8b2010-02-09 16:05:07 -0800425Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700426{
Jason Samsfb03a222009-10-15 16:47:31 -0700427 pthread_mutex_lock(&gInitMutex);
428
Jason Sams326e0dd2009-05-22 14:03:28 -0700429 dev->addContext(this);
430 mDev = dev;
431 mRunning = false;
432 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700433 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700434 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700435 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800436 mError = RS_ERROR_NONE;
437 mErrorMsg = NULL;
438
Jason Sams613cad12009-11-12 15:10:25 -0800439 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800440 memset(&mGL, 0, sizeof(mGL));
441 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700442
Jason Samsa658e902009-06-04 14:35:01 -0700443 int status;
444 pthread_attr_t threadAttr;
445
Jason Samsfb03a222009-10-15 16:47:31 -0700446 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700447 status = pthread_key_create(&gThreadTLSKey, NULL);
448 if (status) {
449 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700450 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700451 return;
452 }
Jason Samse5769102009-06-19 16:03:18 -0700453 }
Jason Samsfb03a222009-10-15 16:47:31 -0700454 gThreadTLSKeyCount++;
455 pthread_mutex_unlock(&gInitMutex);
456
457 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700458
Jason Samsa658e902009-06-04 14:35:01 -0700459 status = pthread_attr_init(&threadAttr);
460 if (status) {
461 LOGE("Failed to init thread attribute.");
462 return;
463 }
464
Jason Sams613cad12009-11-12 15:10:25 -0800465 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700466
Jason Sams50869382009-08-18 17:07:09 -0700467 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700468 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700469 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700470
Jason Sams7bf29dd2010-07-19 15:38:19 -0700471 LOGV("RS Launching thread(s)");
472 mWorkers.mCount = 2;
473 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
474 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
475 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
476 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700477 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700478 if (status) {
479 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700480 return;
481 }
Jason Sams18133402010-07-20 15:09:00 -0700482 while(!mRunning) {
483 usleep(100);
484 }
485
Jason Sams7bf29dd2010-07-19 15:38:19 -0700486 mWorkers.mRunningCount = 0;
487 mWorkers.mLaunchCount = 0;
488 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
489 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
490 if (status) {
491 mWorkers.mCount = ct;
492 LOGE("Created fewer than expected number of RS threads.");
493 break;
494 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700495 }
496
Jason Sams326e0dd2009-05-22 14:03:28 -0700497
Jason Samsa658e902009-06-04 14:35:01 -0700498 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700499}
500
501Context::~Context()
502{
Jason Sams8c0ee652009-08-25 14:49:07 -0700503 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700504 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700505 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700506 void *res;
507
Jason Sams8c0ee652009-08-25 14:49:07 -0700508 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700509 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800510 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700511 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700512
Jason Samsfb03a222009-10-15 16:47:31 -0700513 // Global structure cleanup.
514 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700515 if (mDev) {
516 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700517 --gThreadTLSKeyCount;
518 if (!gThreadTLSKeyCount) {
519 pthread_key_delete(gThreadTLSKey);
520 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800521 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700522 }
Jason Samsfb03a222009-10-15 16:47:31 -0700523 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700524
525 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700526}
527
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700528void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800529{
Jason Sams4820e8b2010-02-09 16:05:07 -0800530 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800531
Jason Sams458f2dc2009-11-03 13:58:36 -0800532 EGLBoolean ret;
533 if (mEGL.mSurface != NULL) {
534 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
535 checkEglError("eglMakeCurrent", ret);
536
537 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
538 checkEglError("eglDestroySurface", ret);
539
540 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800541 mWidth = 0;
542 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800543 }
544
545 mWndSurface = sur;
546 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700547 mWidth = w;
548 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800549 bool first = false;
550 if (!mEGL.mContext) {
551 first = true;
552 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800553 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800554 pthread_mutex_unlock(&gInitMutex);
555 }
556
Jason Sams458f2dc2009-11-03 13:58:36 -0800557 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
558 checkEglError("eglCreateWindowSurface");
559 if (mEGL.mSurface == EGL_NO_SURFACE) {
560 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
561 }
562
563 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
564 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800565
Jason Sams771565f2010-05-14 15:30:29 -0700566 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800567
Jason Sams613cad12009-11-12 15:10:25 -0800568 if (first) {
569 mGL.mVersion = glGetString(GL_VERSION);
570 mGL.mVendor = glGetString(GL_VENDOR);
571 mGL.mRenderer = glGetString(GL_RENDERER);
572 mGL.mExtensions = glGetString(GL_EXTENSIONS);
573
574 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
575 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800576 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800577 LOGV("GL Renderer %s", mGL.mRenderer);
578 //LOGV("GL Extensions %s", mGL.mExtensions);
579
Jason Samsc460e552009-11-25 13:22:07 -0800580 const char *verptr = NULL;
581 if (strlen((const char *)mGL.mVersion) > 9) {
582 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
583 verptr = (const char *)mGL.mVersion + 12;
584 }
585 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
586 verptr = (const char *)mGL.mVersion + 9;
587 }
588 }
589
590 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800591 LOGE("Error, OpenGL ES Lite not supported");
592 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800593 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800594 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800595
596 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
597 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
598 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
599
600 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
601 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
602
603 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
604 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800605
606 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800607 }
608
Jason Sams458f2dc2009-11-03 13:58:36 -0800609 }
610}
611
Jason Sams86f1b232009-09-24 17:38:20 -0700612void Context::pause()
613{
Jason Sams4820e8b2010-02-09 16:05:07 -0800614 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700615 mPaused = true;
616}
617
618void Context::resume()
619{
Jason Sams4820e8b2010-02-09 16:05:07 -0800620 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700621 mPaused = false;
622}
623
Jason Sams326e0dd2009-05-22 14:03:28 -0700624void Context::setRootScript(Script *s)
625{
Jason Sams4820e8b2010-02-09 16:05:07 -0800626 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700627 mRootScript.set(s);
628}
629
Jason Samsccc010b2010-05-13 18:30:11 -0700630void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700631{
Jason Sams4820e8b2010-02-09 16:05:07 -0800632 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700633 if (pfs == NULL) {
634 mFragmentStore.set(mStateFragmentStore.mDefault);
635 } else {
636 mFragmentStore.set(pfs);
637 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700638}
639
640void Context::setFragment(ProgramFragment *pf)
641{
Jason Sams4820e8b2010-02-09 16:05:07 -0800642 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700643 if (pf == NULL) {
644 mFragment.set(mStateFragment.mDefault);
645 } else {
646 mFragment.set(pf);
647 }
Jason Samscfb1d112009-08-05 13:57:03 -0700648}
649
Jason Sams5fd09d82009-09-23 13:57:02 -0700650void Context::setRaster(ProgramRaster *pr)
651{
Jason Sams4820e8b2010-02-09 16:05:07 -0800652 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700653 if (pr == NULL) {
654 mRaster.set(mStateRaster.mDefault);
655 } else {
656 mRaster.set(pr);
657 }
658}
659
Jason Sams326e0dd2009-05-22 14:03:28 -0700660void Context::setVertex(ProgramVertex *pv)
661{
Jason Sams4820e8b2010-02-09 16:05:07 -0800662 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700663 if (pv == NULL) {
664 mVertex.set(mStateVertex.mDefault);
665 } else {
666 mVertex.set(pv);
667 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700668}
669
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700670void Context::setFont(Font *f)
671{
672 rsAssert(mIsGraphicsContext);
673 if (f == NULL) {
674 mFont.set(mStateFont.mDefault);
675 } else {
676 mFont.set(f);
677 }
678}
679
Jason Samsa4a54e42009-06-10 18:39:40 -0700680void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700681{
682 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700683 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700684 mNames.add(obj);
685}
686
687void Context::removeName(ObjectBase *obj)
688{
689 for(size_t ct=0; ct < mNames.size(); ct++) {
690 if (obj == mNames[ct]) {
691 mNames.removeAt(ct);
692 return;
693 }
694 }
695}
696
Jason Sams50869382009-08-18 17:07:09 -0700697bool Context::objDestroyOOBInit()
698{
Jason Sams12b14ae2010-03-18 11:39:44 -0700699 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700700 LOGE("Context::ObjDestroyOOBInit mutex init failure");
701 return false;
702 }
703 return true;
704}
705
706void Context::objDestroyOOBRun()
707{
708 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700709 if (!mObjDestroy.mMutex.lock()) {
710 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700711 return;
712 }
713
714 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700715 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700716 }
717 mObjDestroy.mDestroyList.clear();
718 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700719 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700720 }
721}
722
723void Context::objDestroyOOBDestroy()
724{
725 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700726}
727
728void Context::objDestroyAdd(ObjectBase *obj)
729{
Jason Sams12b14ae2010-03-18 11:39:44 -0700730 if (!mObjDestroy.mMutex.lock()) {
731 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700732 return;
733 }
734
735 mObjDestroy.mNeedToEmpty = true;
736 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700737 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700738}
739
Jason Sams8c401ef2009-10-06 13:58:47 -0700740uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
741{
742 //LOGE("getMessageToClient %i %i", bufferLen, wait);
743 if (!wait) {
744 if (mIO.mToClient.isEmpty()) {
745 // No message to get and not going to wait for one.
746 receiveLen = 0;
747 return 0;
748 }
749 }
750
751 //LOGE("getMessageToClient 2 con=%p", this);
752 uint32_t bytesData = 0;
753 uint32_t commandID = 0;
754 const void *d = mIO.mToClient.get(&commandID, &bytesData);
755 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
756
757 *receiveLen = bytesData;
758 if (bufferLen >= bytesData) {
759 memcpy(data, d, bytesData);
760 mIO.mToClient.next();
761 return commandID;
762 }
763 return 0;
764}
765
766bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
767{
768 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
769 if (cmdID == 0) {
770 LOGE("Attempting to send invalid command 0 to client.");
771 return false;
772 }
773 if (!waitForSpace) {
774 if (mIO.mToClient.getFreeSpace() < len) {
775 // Not enough room, and not waiting.
776 return false;
777 }
778 }
779 //LOGE("sendMessageToClient 2");
780 void *p = mIO.mToClient.reserve(len);
781 memcpy(p, data, len);
782 mIO.mToClient.commit(cmdID, len);
783 //LOGE("sendMessageToClient 3");
784 return true;
785}
786
787void Context::initToClient()
788{
789 while(!mRunning) {
790 usleep(100);
791 }
792}
793
794void Context::deinitToClient()
795{
796 mIO.mToClient.shutdown();
797}
Jason Sams50869382009-08-18 17:07:09 -0700798
Jason Samsa2cf7552010-03-03 13:03:18 -0800799const char * Context::getError(RsError *err)
800{
801 *err = mError;
802 mError = RS_ERROR_NONE;
803 if (*err != RS_ERROR_NONE) {
804 return mErrorMsg;
805 }
806 return NULL;
807}
808
809void Context::setError(RsError e, const char *msg)
810{
811 mError = e;
812 mErrorMsg = msg;
813}
814
815
Jason Sams13e26342009-11-24 12:26:35 -0800816void Context::dumpDebug() const
817{
818 LOGE("RS Context debug %p", this);
819 LOGE("RS Context debug");
820
821 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700822 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800823 LOGE(" GL vendor: %s", mGL.mVendor);
824 LOGE(" GL renderer: %s", mGL.mRenderer);
825 LOGE(" GL Version: %s", mGL.mVersion);
826 LOGE(" GL Extensions: %s", mGL.mExtensions);
827 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
828 LOGE(" RS width %i, height %i", mWidth, mHeight);
829 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
830 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
831
Jason Sams4815c0d2009-12-15 12:58:36 -0800832 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
833 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
834 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
835 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800836}
Jason Samsa4a54e42009-06-10 18:39:40 -0700837
Jason Sams326e0dd2009-05-22 14:03:28 -0700838///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700839//
Jason Sams326e0dd2009-05-22 14:03:28 -0700840
841namespace android {
842namespace renderscript {
843
Jason Sams8c880902010-06-15 12:15:57 -0700844void rsi_ContextFinish(Context *rsc)
845{
846}
Jason Sams326e0dd2009-05-22 14:03:28 -0700847
848void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
849{
850 Script *s = static_cast<Script *>(vs);
851 rsc->setRootScript(s);
852}
853
854void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
855{
856 Sampler *s = static_cast<Sampler *>(vs);
857
858 if (slot > RS_MAX_SAMPLER_SLOT) {
859 LOGE("Invalid sampler slot");
860 return;
861 }
862
863 s->bindToContext(&rsc->mStateSampler, slot);
864}
865
Jason Samsccc010b2010-05-13 18:30:11 -0700866void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700867{
Jason Samsccc010b2010-05-13 18:30:11 -0700868 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700869 rsc->setFragmentStore(pfs);
870}
871
872void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
873{
874 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
875 rsc->setFragment(pf);
876}
877
Jason Sams5fd09d82009-09-23 13:57:02 -0700878void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
879{
880 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
881 rsc->setRaster(pr);
882}
883
Jason Sams326e0dd2009-05-22 14:03:28 -0700884void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
885{
886 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
887 rsc->setVertex(pv);
888}
889
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700890void rsi_ContextBindFont(Context *rsc, RsFont vfont)
891{
892 Font *font = static_cast<Font *>(vfont);
893 rsc->setFont(font);
894}
895
896
Jason Samsa4a54e42009-06-10 18:39:40 -0700897void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700898{
899 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700900 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700901}
Jason Sams326e0dd2009-05-22 14:03:28 -0700902
Jason Sams707aaf32009-08-18 14:14:24 -0700903void rsi_ObjDestroy(Context *rsc, void *obj)
904{
905 ObjectBase *ob = static_cast<ObjectBase *>(obj);
906 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700907 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700908}
909
Jason Sams86f1b232009-09-24 17:38:20 -0700910void rsi_ContextPause(Context *rsc)
911{
912 rsc->pause();
913}
914
915void rsi_ContextResume(Context *rsc)
916{
917 rsc->resume();
918}
919
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700920void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800921{
Mathias Agopianfa402862010-02-12 14:04:35 -0800922 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800923}
924
Jason Sams15832442009-11-15 12:14:26 -0800925void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800926{
Jason Sams15832442009-11-15 12:14:26 -0800927 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800928}
929
Jason Samsc21cf402009-11-17 17:26:46 -0800930void rsi_ContextDump(Context *rsc, int32_t bits)
931{
932 ObjectBase::dumpAll(rsc);
933}
934
Jason Samsa2cf7552010-03-03 13:03:18 -0800935const char * rsi_ContextGetError(Context *rsc, RsError *e)
936{
937 const char *msg = rsc->getError(e);
938 if (*e != RS_ERROR_NONE) {
939 LOGE("RS Error %i %s", *e, msg);
940 }
941 return msg;
942}
943
Jason Sams326e0dd2009-05-22 14:03:28 -0700944}
945}
946
947
Jason Sams4820e8b2010-02-09 16:05:07 -0800948RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700949{
Jason Sams4820e8b2010-02-09 16:05:07 -0800950 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700951 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800952 Context *rsc = new Context(dev, false, false);
953 return rsc;
954}
955
956RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
957{
958 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
959 Device * dev = static_cast<Device *>(vdev);
960 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700961 return rsc;
962}
963
964void rsContextDestroy(RsContext vrsc)
965{
966 Context * rsc = static_cast<Context *>(vrsc);
967 delete rsc;
968}
969
Jason Sams50869382009-08-18 17:07:09 -0700970void rsObjDestroyOOB(RsContext vrsc, void *obj)
971{
972 Context * rsc = static_cast<Context *>(vrsc);
973 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
974}
975
Jason Sams8c401ef2009-10-06 13:58:47 -0700976uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
977{
978 Context * rsc = static_cast<Context *>(vrsc);
979 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
980}
981
982void rsContextInitToClient(RsContext vrsc)
983{
984 Context * rsc = static_cast<Context *>(vrsc);
985 rsc->initToClient();
986}
987
988void rsContextDeinitToClient(RsContext vrsc)
989{
990 Context * rsc = static_cast<Context *>(vrsc);
991 rsc->deinitToClient();
992}
993