blob: da85f83ac60ec7a1f18090486037718387fa95a7 [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>
26
Joe Onorato76371ff2009-09-23 16:37:36 -070027#include <cutils/properties.h>
28
Jason Sams1aa5a4e2009-06-22 17:15:15 -070029#include <GLES/gl.h>
30#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080031#include <GLES2/gl2.h>
32#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070033
Jason Sams15832442009-11-15 12:14:26 -080034#include <cutils/sched_policy.h>
35
Jason Sams326e0dd2009-05-22 14:03:28 -070036using namespace android;
37using namespace android::renderscript;
38
Jason Samse5769102009-06-19 16:03:18 -070039pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070040uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070041uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070042pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070043
Jason Sams33b6e3b2009-10-27 14:44:31 -070044static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
45 if (returnVal != EGL_TRUE) {
46 fprintf(stderr, "%s() returned %d\n", op, returnVal);
47 }
48
49 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
50 = eglGetError()) {
51 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
52 error);
53 }
54}
55
Jason Samsc460e552009-11-25 13:22:07 -080056void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070057{
Jason Samsafcb25c2009-08-25 11:34:49 -070058 mEGL.mNumConfigs = -1;
59 EGLint configAttribs[128];
60 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080061 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070062
Jason Samsafcb25c2009-08-25 11:34:49 -070063 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070064
Jason Samsafcb25c2009-08-25 11:34:49 -070065 configAttribsPtr[0] = EGL_SURFACE_TYPE;
66 configAttribsPtr[1] = EGL_WINDOW_BIT;
67 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070068
Jason Samsc460e552009-11-25 13:22:07 -080069 if (useGL2) {
70 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
71 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
72 configAttribsPtr += 2;
73 }
74
Jason Samsafcb25c2009-08-25 11:34:49 -070075 if (mUseDepth) {
76 configAttribsPtr[0] = EGL_DEPTH_SIZE;
77 configAttribsPtr[1] = 16;
78 configAttribsPtr += 2;
79 }
Jason Sams9397e302009-08-27 20:23:34 -070080
Jason Sams5fd09d82009-09-23 13:57:02 -070081 if (mDev->mForceSW) {
82 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
83 configAttribsPtr[1] = EGL_SLOW_CONFIG;
84 configAttribsPtr += 2;
85 }
86
Jason Samsafcb25c2009-08-25 11:34:49 -070087 configAttribsPtr[0] = EGL_NONE;
88 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070089
Jason Sams6d751ef2009-10-08 12:55:06 -070090 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070091 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070092 checkEglError("eglGetDisplay");
93
Jason Samsafcb25c2009-08-25 11:34:49 -070094 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070095 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070096
97 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
98 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -070099 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -0700100 }
101 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
102
Jason Samsafcb25c2009-08-25 11:34:49 -0700103
Jason Samsc460e552009-11-25 13:22:07 -0800104 if (useGL2) {
105 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
106 } else {
107 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
108 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700109 checkEglError("eglCreateContext");
110 if (mEGL.mContext == EGL_NO_CONTEXT) {
111 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
112 }
113 gGLContextCount++;
Jason Sams771565f2010-05-14 15:30:29 -0700114
115 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
116 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700117}
118
Jason Sams33b6e3b2009-10-27 14:44:31 -0700119void Context::deinitEGL()
120{
Jason Sams613cad12009-11-12 15:10:25 -0800121 LOGV("deinitEGL");
122 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700123 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
124 checkEglError("eglDestroyContext");
125
126 gGLContextCount--;
127 if (!gGLContextCount) {
128 eglTerminate(mEGL.mDisplay);
129 }
130}
131
132
Jason Sams2dca84d2009-12-09 11:05:45 -0800133uint32_t Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700134{
135 ObjectBaseRef<ProgramFragment> frag(mFragment);
136 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700137 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700138 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700139
Jason Sams2dca84d2009-12-09 11:05:45 -0800140 uint32_t ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -0700141
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700142 mFragment.set(frag);
143 mVertex.set(vtx);
144 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700145 mRaster.set(raster);
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 Sams2dca84d2009-12-09 11:05:45 -0800159 timerSet(RS_TIMER_CLEAR_SWAP);
Jason Sams326e0dd2009-05-22 14:03:28 -0700160
Jason Sams771565f2010-05-14 15:30:29 -0700161 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700162 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
Jason Sams928b7342009-06-08 18:50:13 -0700163 glClearColor(mRootScript->mEnviroment.mClearColor[0],
164 mRootScript->mEnviroment.mClearColor[1],
165 mRootScript->mEnviroment.mClearColor[2],
166 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700167 if (mUseDepth) {
168 glDepthMask(GL_TRUE);
169 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
170 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
171 } else {
172 glClear(GL_COLOR_BUFFER_BIT);
173 }
Jason Sams306fb232009-08-25 17:09:59 -0700174
Jason Sams2dca84d2009-12-09 11:05:45 -0800175 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700176 mStateFragmentStore.mLast.clear();
Jason Sams2dca84d2009-12-09 11:05:45 -0800177 uint32_t ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700178
Jason Samsd01d9702009-12-23 14:35:29 -0800179 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800180 if (mError != RS_ERROR_NONE) {
181 // If we have an error condition we stop rendering until
182 // somthing changes that might fix it.
183 ret = 0;
184 }
Jason Samscfb1d112009-08-05 13:57:03 -0700185 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700186}
187
Jason Sams24371d92009-08-19 12:17:14 -0700188uint64_t Context::getTime() const
189{
190 struct timespec t;
191 clock_gettime(CLOCK_MONOTONIC, &t);
192 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
193}
194
195void Context::timerReset()
196{
197 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
198 mTimers[ct] = 0;
199 }
200}
201
202void Context::timerInit()
203{
204 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700205 mTimeFrame = mTimeLast;
206 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700207 mTimerActive = RS_TIMER_INTERNAL;
208 timerReset();
209}
210
Jason Sams1d54f102009-09-03 15:43:13 -0700211void Context::timerFrame()
212{
213 mTimeLastFrame = mTimeFrame;
214 mTimeFrame = getTime();
215}
216
Jason Sams24371d92009-08-19 12:17:14 -0700217void Context::timerSet(Timers tm)
218{
219 uint64_t last = mTimeLast;
220 mTimeLast = getTime();
221 mTimers[mTimerActive] += mTimeLast - last;
222 mTimerActive = tm;
223}
224
225void Context::timerPrint()
226{
227 double total = 0;
228 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
229 total += mTimers[ct];
230 }
Jason Sams1d54f102009-09-03 15:43:13 -0700231 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800232 mTimeMSLastFrame = frame / 1000000;
233 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
234 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700235
Jason Sams2dca84d2009-12-09 11:05:45 -0800236
237 if (props.mLogTimes) {
238 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
239 mTimeMSLastFrame,
240 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
241 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
242 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
243 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
244 }
Jason Sams24371d92009-08-19 12:17:14 -0700245}
246
Jason Samsa2cf7552010-03-03 13:03:18 -0800247bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700248{
Jason Samsc460e552009-11-25 13:22:07 -0800249 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800250 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
251 LOGE("Context::setupCheck() 1 fail");
252 return false;
253 }
Jason Samsc460e552009-11-25 13:22:07 -0800254
255 mFragmentStore->setupGL2(this, &mStateFragmentStore);
256 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
257 mRaster->setupGL2(this, &mStateRaster);
258 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
259
260 } else {
261 mFragmentStore->setupGL(this, &mStateFragmentStore);
262 mFragment->setupGL(this, &mStateFragment);
263 mRaster->setupGL(this, &mStateRaster);
264 mVertex->setupGL(this, &mStateVertex);
265 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800266 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700267}
268
Jason Sams1fddd902009-09-25 15:25:00 -0700269static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700270{
271 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700272 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700273 return 0 != strcmp(buf, "0");
274}
Jason Sams326e0dd2009-05-22 14:03:28 -0700275
276void * Context::threadProc(void *vrsc)
277{
278 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800279 rsc->mNativeThreadId = gettid();
280
281 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800282 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700283
Jason Sams1fddd902009-09-25 15:25:00 -0700284 rsc->props.mLogTimes = getProp("debug.rs.profile");
285 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800286 rsc->props.mLogObjects = getProp("debug.rs.object");
287 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700288
Jason Samse5769102009-06-19 16:03:18 -0700289 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
290 if (!tlsStruct) {
291 LOGE("Error allocating tls storage");
292 return NULL;
293 }
294 tlsStruct->mContext = rsc;
295 tlsStruct->mScript = NULL;
296 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
297 if (status) {
298 LOGE("pthread_setspecific %i", status);
299 }
300
Jason Sams4820e8b2010-02-09 16:05:07 -0800301 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700302 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800303 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700304 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800305 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700306 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800307 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700308 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800309 rsc->setFragmentStore(NULL);
310 rsc->mStateVertexArray.init(rsc);
311 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700312
Jason Sams326e0dd2009-05-22 14:03:28 -0700313 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700314 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700315 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700316 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700317 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800318 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700319
Jason Sams2dca84d2009-12-09 11:05:45 -0800320 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800321 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800322 targetTime = rsc->runRootScript();
323 mDraw = targetTime && !rsc->mPaused;
324 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700325 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800326 rsc->timerFrame();
327 rsc->timerSet(RS_TIMER_INTERNAL);
328 rsc->timerPrint();
329 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700330 }
Jason Sams24371d92009-08-19 12:17:14 -0700331 if (rsc->mObjDestroy.mNeedToEmpty) {
332 rsc->objDestroyOOBRun();
333 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800334 if (rsc->mThreadPriority > 0 && targetTime) {
335 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
336 if (t > 0) {
337 usleep(t);
338 }
339 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700340 }
341
Jason Sams8c0ee652009-08-25 14:49:07 -0700342 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800343 if (rsc->mIsGraphicsContext) {
344 rsc->mRaster.clear();
345 rsc->mFragment.clear();
346 rsc->mVertex.clear();
347 rsc->mFragmentStore.clear();
348 rsc->mRootScript.clear();
349 rsc->mStateRaster.deinit(rsc);
350 rsc->mStateVertex.deinit(rsc);
351 rsc->mStateFragment.deinit(rsc);
352 rsc->mStateFragmentStore.deinit(rsc);
353 }
Jason Samse514b452009-09-25 14:51:22 -0700354 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700355
Jason Samse402ed32009-11-03 11:25:42 -0800356 rsc->mObjDestroy.mNeedToEmpty = true;
357 rsc->objDestroyOOBRun();
358
Jason Sams4820e8b2010-02-09 16:05:07 -0800359 if (rsc->mIsGraphicsContext) {
360 pthread_mutex_lock(&gInitMutex);
361 rsc->deinitEGL();
362 pthread_mutex_unlock(&gInitMutex);
363 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700364
Jason Sams8c0ee652009-08-25 14:49:07 -0700365 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700366 return NULL;
367}
368
Jason Sams15832442009-11-15 12:14:26 -0800369void Context::setPriority(int32_t p)
370{
371 // Note: If we put this in the proper "background" policy
372 // the wallpapers can become completly unresponsive at times.
373 // This is probably not what we want for something the user is actively
374 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800375 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800376#if 0
377 SchedPolicy pol = SP_FOREGROUND;
378 if (p > 0) {
379 pol = SP_BACKGROUND;
380 }
381 if (!set_sched_policy(mNativeThreadId, pol)) {
382 // success; reset the priority as well
383 }
384#else
385 setpriority(PRIO_PROCESS, mNativeThreadId, p);
386#endif
387}
388
Jason Sams4820e8b2010-02-09 16:05:07 -0800389Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700390{
Jason Samsfb03a222009-10-15 16:47:31 -0700391 pthread_mutex_lock(&gInitMutex);
392
Jason Sams326e0dd2009-05-22 14:03:28 -0700393 dev->addContext(this);
394 mDev = dev;
395 mRunning = false;
396 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700397 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700398 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700399 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800400 mError = RS_ERROR_NONE;
401 mErrorMsg = NULL;
402
Jason Sams613cad12009-11-12 15:10:25 -0800403 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800404 memset(&mGL, 0, sizeof(mGL));
405 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700406
Jason Samsa658e902009-06-04 14:35:01 -0700407 int status;
408 pthread_attr_t threadAttr;
409
Jason Samsfb03a222009-10-15 16:47:31 -0700410 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700411 status = pthread_key_create(&gThreadTLSKey, NULL);
412 if (status) {
413 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700414 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700415 return;
416 }
Jason Samse5769102009-06-19 16:03:18 -0700417 }
Jason Samsfb03a222009-10-15 16:47:31 -0700418 gThreadTLSKeyCount++;
419 pthread_mutex_unlock(&gInitMutex);
420
421 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700422
Jason Samsa658e902009-06-04 14:35:01 -0700423 status = pthread_attr_init(&threadAttr);
424 if (status) {
425 LOGE("Failed to init thread attribute.");
426 return;
427 }
428
Jason Sams613cad12009-11-12 15:10:25 -0800429 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700430
Jason Sams50869382009-08-18 17:07:09 -0700431 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700432 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700433 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700434
Jason Sams992a0b72009-06-23 12:22:47 -0700435 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700436 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700437 if (status) {
438 LOGE("Failed to start rs context thread.");
439 }
440
Jason Sams326e0dd2009-05-22 14:03:28 -0700441 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700442 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700443 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700444
Jason Samsa658e902009-06-04 14:35:01 -0700445 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700446}
447
448Context::~Context()
449{
Jason Sams8c0ee652009-08-25 14:49:07 -0700450 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700451 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700452 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700453 void *res;
454
Jason Sams8c0ee652009-08-25 14:49:07 -0700455 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700456 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800457 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700458 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700459
Jason Samsfb03a222009-10-15 16:47:31 -0700460 // Global structure cleanup.
461 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700462 if (mDev) {
463 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700464 --gThreadTLSKeyCount;
465 if (!gThreadTLSKeyCount) {
466 pthread_key_delete(gThreadTLSKey);
467 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800468 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700469 }
Jason Samsfb03a222009-10-15 16:47:31 -0700470 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700471
472 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700473}
474
Mathias Agopian9b97c292010-02-12 12:00:38 -0800475void Context::setSurface(uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800476{
Jason Sams4820e8b2010-02-09 16:05:07 -0800477 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800478
Jason Sams458f2dc2009-11-03 13:58:36 -0800479 EGLBoolean ret;
480 if (mEGL.mSurface != NULL) {
481 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
482 checkEglError("eglMakeCurrent", ret);
483
484 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
485 checkEglError("eglDestroySurface", ret);
486
487 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800488 mEGL.mWidth = 0;
489 mEGL.mHeight = 0;
490 mWidth = 0;
491 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800492 }
493
494 mWndSurface = sur;
495 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700496 mWidth = w;
497 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800498 bool first = false;
499 if (!mEGL.mContext) {
500 first = true;
501 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800502 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800503 pthread_mutex_unlock(&gInitMutex);
504 }
505
Jason Sams458f2dc2009-11-03 13:58:36 -0800506 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
507 checkEglError("eglCreateWindowSurface");
508 if (mEGL.mSurface == EGL_NO_SURFACE) {
509 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
510 }
511
512 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
513 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800514
Jason Sams771565f2010-05-14 15:30:29 -0700515 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800516
517 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
518 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
519 }
520
521 if (first) {
522 mGL.mVersion = glGetString(GL_VERSION);
523 mGL.mVendor = glGetString(GL_VENDOR);
524 mGL.mRenderer = glGetString(GL_RENDERER);
525 mGL.mExtensions = glGetString(GL_EXTENSIONS);
526
527 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
528 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800529 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800530 LOGV("GL Renderer %s", mGL.mRenderer);
531 //LOGV("GL Extensions %s", mGL.mExtensions);
532
Jason Samsc460e552009-11-25 13:22:07 -0800533 const char *verptr = NULL;
534 if (strlen((const char *)mGL.mVersion) > 9) {
535 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
536 verptr = (const char *)mGL.mVersion + 12;
537 }
538 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
539 verptr = (const char *)mGL.mVersion + 9;
540 }
541 }
542
543 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800544 LOGE("Error, OpenGL ES Lite not supported");
545 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800546 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800547 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800548
549 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
550 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
551 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
552
553 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
554 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
555
556 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
557 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800558
559 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800560 }
561
Jason Sams458f2dc2009-11-03 13:58:36 -0800562 }
563}
564
Jason Sams86f1b232009-09-24 17:38:20 -0700565void Context::pause()
566{
Jason Sams4820e8b2010-02-09 16:05:07 -0800567 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700568 mPaused = true;
569}
570
571void Context::resume()
572{
Jason Sams4820e8b2010-02-09 16:05:07 -0800573 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700574 mPaused = false;
575}
576
Jason Sams326e0dd2009-05-22 14:03:28 -0700577void Context::setRootScript(Script *s)
578{
Jason Sams4820e8b2010-02-09 16:05:07 -0800579 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700580 mRootScript.set(s);
581}
582
Jason Samsccc010b2010-05-13 18:30:11 -0700583void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700584{
Jason Sams4820e8b2010-02-09 16:05:07 -0800585 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700586 if (pfs == NULL) {
587 mFragmentStore.set(mStateFragmentStore.mDefault);
588 } else {
589 mFragmentStore.set(pfs);
590 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700591}
592
593void Context::setFragment(ProgramFragment *pf)
594{
Jason Sams4820e8b2010-02-09 16:05:07 -0800595 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700596 if (pf == NULL) {
597 mFragment.set(mStateFragment.mDefault);
598 } else {
599 mFragment.set(pf);
600 }
Jason Samscfb1d112009-08-05 13:57:03 -0700601}
602
Jason Sams5fd09d82009-09-23 13:57:02 -0700603void Context::setRaster(ProgramRaster *pr)
604{
Jason Sams4820e8b2010-02-09 16:05:07 -0800605 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700606 if (pr == NULL) {
607 mRaster.set(mStateRaster.mDefault);
608 } else {
609 mRaster.set(pr);
610 }
611}
612
Jason Sams326e0dd2009-05-22 14:03:28 -0700613void Context::setVertex(ProgramVertex *pv)
614{
Jason Sams4820e8b2010-02-09 16:05:07 -0800615 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700616 if (pv == NULL) {
617 mVertex.set(mStateVertex.mDefault);
618 } else {
619 mVertex.set(pv);
620 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700621}
622
Jason Samsa4a54e42009-06-10 18:39:40 -0700623void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700624{
625 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700626 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700627 mNames.add(obj);
628}
629
630void Context::removeName(ObjectBase *obj)
631{
632 for(size_t ct=0; ct < mNames.size(); ct++) {
633 if (obj == mNames[ct]) {
634 mNames.removeAt(ct);
635 return;
636 }
637 }
638}
639
Jason Sams50869382009-08-18 17:07:09 -0700640bool Context::objDestroyOOBInit()
641{
Jason Sams12b14ae2010-03-18 11:39:44 -0700642 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700643 LOGE("Context::ObjDestroyOOBInit mutex init failure");
644 return false;
645 }
646 return true;
647}
648
649void Context::objDestroyOOBRun()
650{
651 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700652 if (!mObjDestroy.mMutex.lock()) {
653 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700654 return;
655 }
656
657 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700658 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700659 }
660 mObjDestroy.mDestroyList.clear();
661 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700662 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700663 }
664}
665
666void Context::objDestroyOOBDestroy()
667{
668 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700669}
670
671void Context::objDestroyAdd(ObjectBase *obj)
672{
Jason Sams12b14ae2010-03-18 11:39:44 -0700673 if (!mObjDestroy.mMutex.lock()) {
674 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700675 return;
676 }
677
678 mObjDestroy.mNeedToEmpty = true;
679 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700680 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700681}
682
Jason Sams8c401ef2009-10-06 13:58:47 -0700683uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
684{
685 //LOGE("getMessageToClient %i %i", bufferLen, wait);
686 if (!wait) {
687 if (mIO.mToClient.isEmpty()) {
688 // No message to get and not going to wait for one.
689 receiveLen = 0;
690 return 0;
691 }
692 }
693
694 //LOGE("getMessageToClient 2 con=%p", this);
695 uint32_t bytesData = 0;
696 uint32_t commandID = 0;
697 const void *d = mIO.mToClient.get(&commandID, &bytesData);
698 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
699
700 *receiveLen = bytesData;
701 if (bufferLen >= bytesData) {
702 memcpy(data, d, bytesData);
703 mIO.mToClient.next();
704 return commandID;
705 }
706 return 0;
707}
708
709bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
710{
711 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
712 if (cmdID == 0) {
713 LOGE("Attempting to send invalid command 0 to client.");
714 return false;
715 }
716 if (!waitForSpace) {
717 if (mIO.mToClient.getFreeSpace() < len) {
718 // Not enough room, and not waiting.
719 return false;
720 }
721 }
722 //LOGE("sendMessageToClient 2");
723 void *p = mIO.mToClient.reserve(len);
724 memcpy(p, data, len);
725 mIO.mToClient.commit(cmdID, len);
726 //LOGE("sendMessageToClient 3");
727 return true;
728}
729
730void Context::initToClient()
731{
732 while(!mRunning) {
733 usleep(100);
734 }
735}
736
737void Context::deinitToClient()
738{
739 mIO.mToClient.shutdown();
740}
Jason Sams50869382009-08-18 17:07:09 -0700741
Jason Samsa2cf7552010-03-03 13:03:18 -0800742const char * Context::getError(RsError *err)
743{
744 *err = mError;
745 mError = RS_ERROR_NONE;
746 if (*err != RS_ERROR_NONE) {
747 return mErrorMsg;
748 }
749 return NULL;
750}
751
752void Context::setError(RsError e, const char *msg)
753{
754 mError = e;
755 mErrorMsg = msg;
756}
757
758
Jason Sams13e26342009-11-24 12:26:35 -0800759void Context::dumpDebug() const
760{
761 LOGE("RS Context debug %p", this);
762 LOGE("RS Context debug");
763
764 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
765 LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext,
766 mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay);
767 LOGE(" GL vendor: %s", mGL.mVendor);
768 LOGE(" GL renderer: %s", mGL.mRenderer);
769 LOGE(" GL Version: %s", mGL.mVersion);
770 LOGE(" GL Extensions: %s", mGL.mExtensions);
771 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
772 LOGE(" RS width %i, height %i", mWidth, mHeight);
773 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
774 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
775
Jason Sams4815c0d2009-12-15 12:58:36 -0800776 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
777 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
778 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
779 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800780}
Jason Samsa4a54e42009-06-10 18:39:40 -0700781
Jason Sams326e0dd2009-05-22 14:03:28 -0700782///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700783//
Jason Sams326e0dd2009-05-22 14:03:28 -0700784
785namespace android {
786namespace renderscript {
787
788
789void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
790{
791 Script *s = static_cast<Script *>(vs);
792 rsc->setRootScript(s);
793}
794
795void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
796{
797 Sampler *s = static_cast<Sampler *>(vs);
798
799 if (slot > RS_MAX_SAMPLER_SLOT) {
800 LOGE("Invalid sampler slot");
801 return;
802 }
803
804 s->bindToContext(&rsc->mStateSampler, slot);
805}
806
Jason Samsccc010b2010-05-13 18:30:11 -0700807void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700808{
Jason Samsccc010b2010-05-13 18:30:11 -0700809 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700810 rsc->setFragmentStore(pfs);
811}
812
813void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
814{
815 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
816 rsc->setFragment(pf);
817}
818
Jason Sams5fd09d82009-09-23 13:57:02 -0700819void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
820{
821 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
822 rsc->setRaster(pr);
823}
824
Jason Sams326e0dd2009-05-22 14:03:28 -0700825void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
826{
827 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
828 rsc->setVertex(pv);
829}
830
Jason Samsa4a54e42009-06-10 18:39:40 -0700831void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700832{
833 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700834 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700835}
Jason Sams326e0dd2009-05-22 14:03:28 -0700836
Jason Sams707aaf32009-08-18 14:14:24 -0700837void rsi_ObjDestroy(Context *rsc, void *obj)
838{
839 ObjectBase *ob = static_cast<ObjectBase *>(obj);
840 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700841 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700842}
843
Jason Sams86f1b232009-09-24 17:38:20 -0700844void rsi_ContextPause(Context *rsc)
845{
846 rsc->pause();
847}
848
849void rsi_ContextResume(Context *rsc)
850{
851 rsc->resume();
852}
853
Mathias Agopianfa402862010-02-12 14:04:35 -0800854void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800855{
Mathias Agopianfa402862010-02-12 14:04:35 -0800856 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800857}
858
Jason Sams15832442009-11-15 12:14:26 -0800859void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800860{
Jason Sams15832442009-11-15 12:14:26 -0800861 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800862}
863
Jason Samsc21cf402009-11-17 17:26:46 -0800864void rsi_ContextDump(Context *rsc, int32_t bits)
865{
866 ObjectBase::dumpAll(rsc);
867}
868
Jason Samsa2cf7552010-03-03 13:03:18 -0800869const char * rsi_ContextGetError(Context *rsc, RsError *e)
870{
871 const char *msg = rsc->getError(e);
872 if (*e != RS_ERROR_NONE) {
873 LOGE("RS Error %i %s", *e, msg);
874 }
875 return msg;
876}
877
Jason Sams326e0dd2009-05-22 14:03:28 -0700878}
879}
880
881
Jason Sams4820e8b2010-02-09 16:05:07 -0800882RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700883{
Jason Sams4820e8b2010-02-09 16:05:07 -0800884 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700885 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800886 Context *rsc = new Context(dev, false, false);
887 return rsc;
888}
889
890RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
891{
892 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
893 Device * dev = static_cast<Device *>(vdev);
894 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700895 return rsc;
896}
897
898void rsContextDestroy(RsContext vrsc)
899{
900 Context * rsc = static_cast<Context *>(vrsc);
901 delete rsc;
902}
903
Jason Sams50869382009-08-18 17:07:09 -0700904void rsObjDestroyOOB(RsContext vrsc, void *obj)
905{
906 Context * rsc = static_cast<Context *>(vrsc);
907 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
908}
909
Jason Sams8c401ef2009-10-06 13:58:47 -0700910uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
911{
912 Context * rsc = static_cast<Context *>(vrsc);
913 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
914}
915
916void rsContextInitToClient(RsContext vrsc)
917{
918 Context * rsc = static_cast<Context *>(vrsc);
919 rsc->initToClient();
920}
921
922void rsContextDeinitToClient(RsContext vrsc)
923{
924 Context * rsc = static_cast<Context *>(vrsc);
925 rsc->deinitToClient();
926}
927