blob: 449e5dcd6b1bb3ccde593a4ad89c9780042efaae [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 Sams326e0dd2009-05-22 14:03:28 -0700114}
115
Jason Sams33b6e3b2009-10-27 14:44:31 -0700116void Context::deinitEGL()
117{
Jason Sams613cad12009-11-12 15:10:25 -0800118 LOGV("deinitEGL");
119 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700120 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
121 checkEglError("eglDestroyContext");
122
123 gGLContextCount--;
124 if (!gGLContextCount) {
125 eglTerminate(mEGL.mDisplay);
126 }
127}
128
129
Jason Samsc61346b2010-05-28 18:23:22 -0700130uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700131{
132 ObjectBaseRef<ProgramFragment> frag(mFragment);
133 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700134 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700135 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700136
Jason Samsc61346b2010-05-28 18:23:22 -0700137 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700138
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700139 mFragment.set(frag);
140 mVertex.set(vtx);
141 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700142 mRaster.set(raster);
Jason Samsc9d43db2009-07-28 12:02:16 -0700143 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700144}
145
Jason Samsd01d9702009-12-23 14:35:29 -0800146void Context::checkError(const char *msg) const
147{
148 GLenum err = glGetError();
149 if (err != GL_NO_ERROR) {
150 LOGE("GL Error, 0x%x, from %s", err, msg);
151 }
152}
Jason Sams10308932009-06-09 12:15:30 -0700153
Jason Sams2dca84d2009-12-09 11:05:45 -0800154uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700155{
Jason Sams771565f2010-05-14 15:30:29 -0700156 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700157
Jason Sams2dca84d2009-12-09 11:05:45 -0800158 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700159 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700160 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700161
Jason Samsd01d9702009-12-23 14:35:29 -0800162 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800163 if (mError != RS_ERROR_NONE) {
164 // If we have an error condition we stop rendering until
165 // somthing changes that might fix it.
166 ret = 0;
167 }
Jason Samscfb1d112009-08-05 13:57:03 -0700168 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700169}
170
Jason Sams24371d92009-08-19 12:17:14 -0700171uint64_t Context::getTime() const
172{
173 struct timespec t;
174 clock_gettime(CLOCK_MONOTONIC, &t);
175 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
176}
177
178void Context::timerReset()
179{
180 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
181 mTimers[ct] = 0;
182 }
183}
184
185void Context::timerInit()
186{
187 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700188 mTimeFrame = mTimeLast;
189 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700190 mTimerActive = RS_TIMER_INTERNAL;
191 timerReset();
192}
193
Jason Sams1d54f102009-09-03 15:43:13 -0700194void Context::timerFrame()
195{
196 mTimeLastFrame = mTimeFrame;
197 mTimeFrame = getTime();
198}
199
Jason Sams24371d92009-08-19 12:17:14 -0700200void Context::timerSet(Timers tm)
201{
202 uint64_t last = mTimeLast;
203 mTimeLast = getTime();
204 mTimers[mTimerActive] += mTimeLast - last;
205 mTimerActive = tm;
206}
207
208void Context::timerPrint()
209{
210 double total = 0;
211 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
212 total += mTimers[ct];
213 }
Jason Sams1d54f102009-09-03 15:43:13 -0700214 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800215 mTimeMSLastFrame = frame / 1000000;
216 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
217 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700218
Jason Sams2dca84d2009-12-09 11:05:45 -0800219
220 if (props.mLogTimes) {
221 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
222 mTimeMSLastFrame,
223 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
224 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
225 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
226 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
227 }
Jason Sams24371d92009-08-19 12:17:14 -0700228}
229
Jason Samsa2cf7552010-03-03 13:03:18 -0800230bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700231{
Jason Samsc460e552009-11-25 13:22:07 -0800232 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800233 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
234 LOGE("Context::setupCheck() 1 fail");
235 return false;
236 }
Jason Samsc460e552009-11-25 13:22:07 -0800237
238 mFragmentStore->setupGL2(this, &mStateFragmentStore);
239 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
240 mRaster->setupGL2(this, &mStateRaster);
241 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
242
243 } else {
244 mFragmentStore->setupGL(this, &mStateFragmentStore);
245 mFragment->setupGL(this, &mStateFragment);
246 mRaster->setupGL(this, &mStateRaster);
247 mVertex->setupGL(this, &mStateVertex);
248 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800249 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700250}
251
Jason Sams1fddd902009-09-25 15:25:00 -0700252static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700253{
254 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700255 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700256 return 0 != strcmp(buf, "0");
257}
Jason Sams326e0dd2009-05-22 14:03:28 -0700258
259void * Context::threadProc(void *vrsc)
260{
261 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800262 rsc->mNativeThreadId = gettid();
263
264 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800265 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700266
Jason Sams1fddd902009-09-25 15:25:00 -0700267 rsc->props.mLogTimes = getProp("debug.rs.profile");
268 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800269 rsc->props.mLogObjects = getProp("debug.rs.object");
270 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700271
Jason Samse5769102009-06-19 16:03:18 -0700272 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
273 if (!tlsStruct) {
274 LOGE("Error allocating tls storage");
275 return NULL;
276 }
277 tlsStruct->mContext = rsc;
278 tlsStruct->mScript = NULL;
279 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
280 if (status) {
281 LOGE("pthread_setspecific %i", status);
282 }
283
Jason Sams4820e8b2010-02-09 16:05:07 -0800284 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700285 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800286 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700287 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800288 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700289 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800290 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700291 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800292 rsc->setFragmentStore(NULL);
293 rsc->mStateVertexArray.init(rsc);
294 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700295
Jason Sams326e0dd2009-05-22 14:03:28 -0700296 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700297 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700298 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700299 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700300 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800301 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700302
Jason Sams2dca84d2009-12-09 11:05:45 -0800303 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800304 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800305 targetTime = rsc->runRootScript();
306 mDraw = targetTime && !rsc->mPaused;
307 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700308 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800309 rsc->timerFrame();
310 rsc->timerSet(RS_TIMER_INTERNAL);
311 rsc->timerPrint();
312 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700313 }
Jason Sams24371d92009-08-19 12:17:14 -0700314 if (rsc->mObjDestroy.mNeedToEmpty) {
315 rsc->objDestroyOOBRun();
316 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800317 if (rsc->mThreadPriority > 0 && targetTime) {
318 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
319 if (t > 0) {
320 usleep(t);
321 }
322 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700323 }
324
Jason Sams8c0ee652009-08-25 14:49:07 -0700325 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800326 if (rsc->mIsGraphicsContext) {
327 rsc->mRaster.clear();
328 rsc->mFragment.clear();
329 rsc->mVertex.clear();
330 rsc->mFragmentStore.clear();
331 rsc->mRootScript.clear();
332 rsc->mStateRaster.deinit(rsc);
333 rsc->mStateVertex.deinit(rsc);
334 rsc->mStateFragment.deinit(rsc);
335 rsc->mStateFragmentStore.deinit(rsc);
336 }
Jason Samse514b452009-09-25 14:51:22 -0700337 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700338
Jason Samse402ed32009-11-03 11:25:42 -0800339 rsc->mObjDestroy.mNeedToEmpty = true;
340 rsc->objDestroyOOBRun();
341
Jason Sams4820e8b2010-02-09 16:05:07 -0800342 if (rsc->mIsGraphicsContext) {
343 pthread_mutex_lock(&gInitMutex);
344 rsc->deinitEGL();
345 pthread_mutex_unlock(&gInitMutex);
346 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700347
Jason Sams8c0ee652009-08-25 14:49:07 -0700348 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700349 return NULL;
350}
351
Jason Sams15832442009-11-15 12:14:26 -0800352void Context::setPriority(int32_t p)
353{
354 // Note: If we put this in the proper "background" policy
355 // the wallpapers can become completly unresponsive at times.
356 // This is probably not what we want for something the user is actively
357 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800358 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800359#if 0
360 SchedPolicy pol = SP_FOREGROUND;
361 if (p > 0) {
362 pol = SP_BACKGROUND;
363 }
364 if (!set_sched_policy(mNativeThreadId, pol)) {
365 // success; reset the priority as well
366 }
367#else
368 setpriority(PRIO_PROCESS, mNativeThreadId, p);
369#endif
370}
371
Jason Sams4820e8b2010-02-09 16:05:07 -0800372Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700373{
Jason Samsfb03a222009-10-15 16:47:31 -0700374 pthread_mutex_lock(&gInitMutex);
375
Jason Sams326e0dd2009-05-22 14:03:28 -0700376 dev->addContext(this);
377 mDev = dev;
378 mRunning = false;
379 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700380 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700381 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700382 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800383 mError = RS_ERROR_NONE;
384 mErrorMsg = NULL;
385
Jason Sams613cad12009-11-12 15:10:25 -0800386 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800387 memset(&mGL, 0, sizeof(mGL));
388 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700389
Jason Samsa658e902009-06-04 14:35:01 -0700390 int status;
391 pthread_attr_t threadAttr;
392
Jason Samsfb03a222009-10-15 16:47:31 -0700393 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700394 status = pthread_key_create(&gThreadTLSKey, NULL);
395 if (status) {
396 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700397 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700398 return;
399 }
Jason Samse5769102009-06-19 16:03:18 -0700400 }
Jason Samsfb03a222009-10-15 16:47:31 -0700401 gThreadTLSKeyCount++;
402 pthread_mutex_unlock(&gInitMutex);
403
404 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700405
Jason Samsa658e902009-06-04 14:35:01 -0700406 status = pthread_attr_init(&threadAttr);
407 if (status) {
408 LOGE("Failed to init thread attribute.");
409 return;
410 }
411
Jason Sams613cad12009-11-12 15:10:25 -0800412 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700413
Jason Sams50869382009-08-18 17:07:09 -0700414 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700415 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700416 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700417
Jason Sams992a0b72009-06-23 12:22:47 -0700418 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700419 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700420 if (status) {
421 LOGE("Failed to start rs context thread.");
422 }
423
Jason Sams326e0dd2009-05-22 14:03:28 -0700424 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700425 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700426 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700427
Jason Samsa658e902009-06-04 14:35:01 -0700428 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700429}
430
431Context::~Context()
432{
Jason Sams8c0ee652009-08-25 14:49:07 -0700433 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700434 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700435 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700436 void *res;
437
Jason Sams8c0ee652009-08-25 14:49:07 -0700438 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700439 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800440 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700441 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700442
Jason Samsfb03a222009-10-15 16:47:31 -0700443 // Global structure cleanup.
444 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700445 if (mDev) {
446 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700447 --gThreadTLSKeyCount;
448 if (!gThreadTLSKeyCount) {
449 pthread_key_delete(gThreadTLSKey);
450 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800451 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700452 }
Jason Samsfb03a222009-10-15 16:47:31 -0700453 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700454
455 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700456}
457
Mathias Agopian9b97c292010-02-12 12:00:38 -0800458void Context::setSurface(uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800459{
Jason Sams4820e8b2010-02-09 16:05:07 -0800460 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800461
Jason Sams458f2dc2009-11-03 13:58:36 -0800462 EGLBoolean ret;
463 if (mEGL.mSurface != NULL) {
464 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
465 checkEglError("eglMakeCurrent", ret);
466
467 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
468 checkEglError("eglDestroySurface", ret);
469
470 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800471 mWidth = 0;
472 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800473 }
474
475 mWndSurface = sur;
476 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700477 mWidth = w;
478 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800479 bool first = false;
480 if (!mEGL.mContext) {
481 first = true;
482 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800483 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800484 pthread_mutex_unlock(&gInitMutex);
485 }
486
Jason Sams458f2dc2009-11-03 13:58:36 -0800487 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
488 checkEglError("eglCreateWindowSurface");
489 if (mEGL.mSurface == EGL_NO_SURFACE) {
490 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
491 }
492
493 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
494 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800495
Jason Sams771565f2010-05-14 15:30:29 -0700496 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800497
Jason Sams613cad12009-11-12 15:10:25 -0800498 if (first) {
499 mGL.mVersion = glGetString(GL_VERSION);
500 mGL.mVendor = glGetString(GL_VENDOR);
501 mGL.mRenderer = glGetString(GL_RENDERER);
502 mGL.mExtensions = glGetString(GL_EXTENSIONS);
503
504 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
505 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800506 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800507 LOGV("GL Renderer %s", mGL.mRenderer);
508 //LOGV("GL Extensions %s", mGL.mExtensions);
509
Jason Samsc460e552009-11-25 13:22:07 -0800510 const char *verptr = NULL;
511 if (strlen((const char *)mGL.mVersion) > 9) {
512 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
513 verptr = (const char *)mGL.mVersion + 12;
514 }
515 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
516 verptr = (const char *)mGL.mVersion + 9;
517 }
518 }
519
520 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800521 LOGE("Error, OpenGL ES Lite not supported");
522 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800523 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800524 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800525
526 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
527 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
528 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
529
530 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
531 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
532
533 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
534 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800535
536 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800537 }
538
Jason Sams458f2dc2009-11-03 13:58:36 -0800539 }
540}
541
Jason Sams86f1b232009-09-24 17:38:20 -0700542void Context::pause()
543{
Jason Sams4820e8b2010-02-09 16:05:07 -0800544 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700545 mPaused = true;
546}
547
548void Context::resume()
549{
Jason Sams4820e8b2010-02-09 16:05:07 -0800550 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700551 mPaused = false;
552}
553
Jason Sams326e0dd2009-05-22 14:03:28 -0700554void Context::setRootScript(Script *s)
555{
Jason Sams4820e8b2010-02-09 16:05:07 -0800556 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700557 mRootScript.set(s);
558}
559
Jason Samsccc010b2010-05-13 18:30:11 -0700560void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700561{
Jason Sams4820e8b2010-02-09 16:05:07 -0800562 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700563 if (pfs == NULL) {
564 mFragmentStore.set(mStateFragmentStore.mDefault);
565 } else {
566 mFragmentStore.set(pfs);
567 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700568}
569
570void Context::setFragment(ProgramFragment *pf)
571{
Jason Sams4820e8b2010-02-09 16:05:07 -0800572 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700573 if (pf == NULL) {
574 mFragment.set(mStateFragment.mDefault);
575 } else {
576 mFragment.set(pf);
577 }
Jason Samscfb1d112009-08-05 13:57:03 -0700578}
579
Jason Sams5fd09d82009-09-23 13:57:02 -0700580void Context::setRaster(ProgramRaster *pr)
581{
Jason Sams4820e8b2010-02-09 16:05:07 -0800582 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700583 if (pr == NULL) {
584 mRaster.set(mStateRaster.mDefault);
585 } else {
586 mRaster.set(pr);
587 }
588}
589
Jason Sams326e0dd2009-05-22 14:03:28 -0700590void Context::setVertex(ProgramVertex *pv)
591{
Jason Sams4820e8b2010-02-09 16:05:07 -0800592 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700593 if (pv == NULL) {
594 mVertex.set(mStateVertex.mDefault);
595 } else {
596 mVertex.set(pv);
597 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700598}
599
Jason Samsa4a54e42009-06-10 18:39:40 -0700600void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700601{
602 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700603 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700604 mNames.add(obj);
605}
606
607void Context::removeName(ObjectBase *obj)
608{
609 for(size_t ct=0; ct < mNames.size(); ct++) {
610 if (obj == mNames[ct]) {
611 mNames.removeAt(ct);
612 return;
613 }
614 }
615}
616
Jason Sams50869382009-08-18 17:07:09 -0700617bool Context::objDestroyOOBInit()
618{
Jason Sams12b14ae2010-03-18 11:39:44 -0700619 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700620 LOGE("Context::ObjDestroyOOBInit mutex init failure");
621 return false;
622 }
623 return true;
624}
625
626void Context::objDestroyOOBRun()
627{
628 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700629 if (!mObjDestroy.mMutex.lock()) {
630 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700631 return;
632 }
633
634 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700635 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700636 }
637 mObjDestroy.mDestroyList.clear();
638 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700639 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700640 }
641}
642
643void Context::objDestroyOOBDestroy()
644{
645 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700646}
647
648void Context::objDestroyAdd(ObjectBase *obj)
649{
Jason Sams12b14ae2010-03-18 11:39:44 -0700650 if (!mObjDestroy.mMutex.lock()) {
651 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700652 return;
653 }
654
655 mObjDestroy.mNeedToEmpty = true;
656 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700657 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700658}
659
Jason Sams8c401ef2009-10-06 13:58:47 -0700660uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
661{
662 //LOGE("getMessageToClient %i %i", bufferLen, wait);
663 if (!wait) {
664 if (mIO.mToClient.isEmpty()) {
665 // No message to get and not going to wait for one.
666 receiveLen = 0;
667 return 0;
668 }
669 }
670
671 //LOGE("getMessageToClient 2 con=%p", this);
672 uint32_t bytesData = 0;
673 uint32_t commandID = 0;
674 const void *d = mIO.mToClient.get(&commandID, &bytesData);
675 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
676
677 *receiveLen = bytesData;
678 if (bufferLen >= bytesData) {
679 memcpy(data, d, bytesData);
680 mIO.mToClient.next();
681 return commandID;
682 }
683 return 0;
684}
685
686bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
687{
688 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
689 if (cmdID == 0) {
690 LOGE("Attempting to send invalid command 0 to client.");
691 return false;
692 }
693 if (!waitForSpace) {
694 if (mIO.mToClient.getFreeSpace() < len) {
695 // Not enough room, and not waiting.
696 return false;
697 }
698 }
699 //LOGE("sendMessageToClient 2");
700 void *p = mIO.mToClient.reserve(len);
701 memcpy(p, data, len);
702 mIO.mToClient.commit(cmdID, len);
703 //LOGE("sendMessageToClient 3");
704 return true;
705}
706
707void Context::initToClient()
708{
709 while(!mRunning) {
710 usleep(100);
711 }
712}
713
714void Context::deinitToClient()
715{
716 mIO.mToClient.shutdown();
717}
Jason Sams50869382009-08-18 17:07:09 -0700718
Jason Samsa2cf7552010-03-03 13:03:18 -0800719const char * Context::getError(RsError *err)
720{
721 *err = mError;
722 mError = RS_ERROR_NONE;
723 if (*err != RS_ERROR_NONE) {
724 return mErrorMsg;
725 }
726 return NULL;
727}
728
729void Context::setError(RsError e, const char *msg)
730{
731 mError = e;
732 mErrorMsg = msg;
733}
734
735
Jason Sams13e26342009-11-24 12:26:35 -0800736void Context::dumpDebug() const
737{
738 LOGE("RS Context debug %p", this);
739 LOGE("RS Context debug");
740
741 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700742 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800743 LOGE(" GL vendor: %s", mGL.mVendor);
744 LOGE(" GL renderer: %s", mGL.mRenderer);
745 LOGE(" GL Version: %s", mGL.mVersion);
746 LOGE(" GL Extensions: %s", mGL.mExtensions);
747 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
748 LOGE(" RS width %i, height %i", mWidth, mHeight);
749 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
750 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
751
Jason Sams4815c0d2009-12-15 12:58:36 -0800752 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
753 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
754 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
755 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800756}
Jason Samsa4a54e42009-06-10 18:39:40 -0700757
Jason Sams326e0dd2009-05-22 14:03:28 -0700758///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700759//
Jason Sams326e0dd2009-05-22 14:03:28 -0700760
761namespace android {
762namespace renderscript {
763
764
765void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
766{
767 Script *s = static_cast<Script *>(vs);
768 rsc->setRootScript(s);
769}
770
771void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
772{
773 Sampler *s = static_cast<Sampler *>(vs);
774
775 if (slot > RS_MAX_SAMPLER_SLOT) {
776 LOGE("Invalid sampler slot");
777 return;
778 }
779
780 s->bindToContext(&rsc->mStateSampler, slot);
781}
782
Jason Samsccc010b2010-05-13 18:30:11 -0700783void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700784{
Jason Samsccc010b2010-05-13 18:30:11 -0700785 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700786 rsc->setFragmentStore(pfs);
787}
788
789void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
790{
791 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
792 rsc->setFragment(pf);
793}
794
Jason Sams5fd09d82009-09-23 13:57:02 -0700795void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
796{
797 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
798 rsc->setRaster(pr);
799}
800
Jason Sams326e0dd2009-05-22 14:03:28 -0700801void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
802{
803 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
804 rsc->setVertex(pv);
805}
806
Jason Samsa4a54e42009-06-10 18:39:40 -0700807void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700808{
809 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700810 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700811}
Jason Sams326e0dd2009-05-22 14:03:28 -0700812
Jason Sams707aaf32009-08-18 14:14:24 -0700813void rsi_ObjDestroy(Context *rsc, void *obj)
814{
815 ObjectBase *ob = static_cast<ObjectBase *>(obj);
816 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700817 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700818}
819
Jason Sams86f1b232009-09-24 17:38:20 -0700820void rsi_ContextPause(Context *rsc)
821{
822 rsc->pause();
823}
824
825void rsi_ContextResume(Context *rsc)
826{
827 rsc->resume();
828}
829
Mathias Agopianfa402862010-02-12 14:04:35 -0800830void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800831{
Mathias Agopianfa402862010-02-12 14:04:35 -0800832 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800833}
834
Jason Sams15832442009-11-15 12:14:26 -0800835void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800836{
Jason Sams15832442009-11-15 12:14:26 -0800837 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800838}
839
Jason Samsc21cf402009-11-17 17:26:46 -0800840void rsi_ContextDump(Context *rsc, int32_t bits)
841{
842 ObjectBase::dumpAll(rsc);
843}
844
Jason Samsa2cf7552010-03-03 13:03:18 -0800845const char * rsi_ContextGetError(Context *rsc, RsError *e)
846{
847 const char *msg = rsc->getError(e);
848 if (*e != RS_ERROR_NONE) {
849 LOGE("RS Error %i %s", *e, msg);
850 }
851 return msg;
852}
853
Jason Sams326e0dd2009-05-22 14:03:28 -0700854}
855}
856
857
Jason Sams4820e8b2010-02-09 16:05:07 -0800858RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700859{
Jason Sams4820e8b2010-02-09 16:05:07 -0800860 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700861 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800862 Context *rsc = new Context(dev, false, false);
863 return rsc;
864}
865
866RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
867{
868 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
869 Device * dev = static_cast<Device *>(vdev);
870 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700871 return rsc;
872}
873
874void rsContextDestroy(RsContext vrsc)
875{
876 Context * rsc = static_cast<Context *>(vrsc);
877 delete rsc;
878}
879
Jason Sams50869382009-08-18 17:07:09 -0700880void rsObjDestroyOOB(RsContext vrsc, void *obj)
881{
882 Context * rsc = static_cast<Context *>(vrsc);
883 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
884}
885
Jason Sams8c401ef2009-10-06 13:58:47 -0700886uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
887{
888 Context * rsc = static_cast<Context *>(vrsc);
889 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
890}
891
892void rsContextInitToClient(RsContext vrsc)
893{
894 Context * rsc = static_cast<Context *>(vrsc);
895 rsc->initToClient();
896}
897
898void rsContextDeinitToClient(RsContext vrsc)
899{
900 Context * rsc = static_cast<Context *>(vrsc);
901 rsc->deinitToClient();
902}
903