blob: 4107229ddf17b130e70d1fe9aba261bdcec9811b [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 Sams2dca84d2009-12-09 11:05:45 -0800130uint32_t Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -0700131{
132 ObjectBaseRef<ProgramFragment> frag(mFragment);
133 ObjectBaseRef<ProgramVertex> vtx(mVertex);
134 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700135 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Sams10308932009-06-09 12:15:30 -0700136
Jason Sams2dca84d2009-12-09 11:05:45 -0800137 uint32_t ret = s->run(this, launchID);
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 Sams2dca84d2009-12-09 11:05:45 -0800156 timerSet(RS_TIMER_CLEAR_SWAP);
Jason Sams10308932009-06-09 12:15:30 -0700157 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -0700158
Jason Sams8c9534b2009-09-22 12:26:53 -0700159 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
160 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsafcb25c2009-08-25 11:34:49 -0700161 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -0700162 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
163
Jason Sams928b7342009-06-08 18:50:13 -0700164 glClearColor(mRootScript->mEnviroment.mClearColor[0],
165 mRootScript->mEnviroment.mClearColor[1],
166 mRootScript->mEnviroment.mClearColor[2],
167 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsafcb25c2009-08-25 11:34:49 -0700168 if (mUseDepth) {
169 glDepthMask(GL_TRUE);
170 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
171 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
172 } else {
173 glClear(GL_COLOR_BUFFER_BIT);
174 }
Jason Sams306fb232009-08-25 17:09:59 -0700175
Jason Sams2dca84d2009-12-09 11:05:45 -0800176 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700177 mStateFragmentStore.mLast.clear();
Jason Sams2dca84d2009-12-09 11:05:45 -0800178 uint32_t ret = runScript(mRootScript.get(), 0);
Jason Sams8cfdd242009-10-14 15:43:53 -0700179
Jason Samsd01d9702009-12-23 14:35:29 -0800180 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800181 if (mError != RS_ERROR_NONE) {
182 // If we have an error condition we stop rendering until
183 // somthing changes that might fix it.
184 ret = 0;
185 }
Jason Samscfb1d112009-08-05 13:57:03 -0700186 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700187}
188
Jason Sams24371d92009-08-19 12:17:14 -0700189uint64_t Context::getTime() const
190{
191 struct timespec t;
192 clock_gettime(CLOCK_MONOTONIC, &t);
193 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
194}
195
196void Context::timerReset()
197{
198 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
199 mTimers[ct] = 0;
200 }
201}
202
203void Context::timerInit()
204{
205 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700206 mTimeFrame = mTimeLast;
207 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700208 mTimerActive = RS_TIMER_INTERNAL;
209 timerReset();
210}
211
Jason Sams1d54f102009-09-03 15:43:13 -0700212void Context::timerFrame()
213{
214 mTimeLastFrame = mTimeFrame;
215 mTimeFrame = getTime();
216}
217
Jason Sams24371d92009-08-19 12:17:14 -0700218void Context::timerSet(Timers tm)
219{
220 uint64_t last = mTimeLast;
221 mTimeLast = getTime();
222 mTimers[mTimerActive] += mTimeLast - last;
223 mTimerActive = tm;
224}
225
226void Context::timerPrint()
227{
228 double total = 0;
229 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
230 total += mTimers[ct];
231 }
Jason Sams1d54f102009-09-03 15:43:13 -0700232 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800233 mTimeMSLastFrame = frame / 1000000;
234 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
235 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700236
Jason Sams2dca84d2009-12-09 11:05:45 -0800237
238 if (props.mLogTimes) {
239 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
240 mTimeMSLastFrame,
241 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
242 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
243 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
244 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
245 }
Jason Sams24371d92009-08-19 12:17:14 -0700246}
247
Jason Samsa2cf7552010-03-03 13:03:18 -0800248bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700249{
Jason Samsc460e552009-11-25 13:22:07 -0800250 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800251 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
252 LOGE("Context::setupCheck() 1 fail");
253 return false;
254 }
Jason Samsc460e552009-11-25 13:22:07 -0800255
256 mFragmentStore->setupGL2(this, &mStateFragmentStore);
257 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
258 mRaster->setupGL2(this, &mStateRaster);
259 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
260
261 } else {
262 mFragmentStore->setupGL(this, &mStateFragmentStore);
263 mFragment->setupGL(this, &mStateFragment);
264 mRaster->setupGL(this, &mStateRaster);
265 mVertex->setupGL(this, &mStateVertex);
266 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800267 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700268}
269
Jason Sams1fddd902009-09-25 15:25:00 -0700270static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700271{
272 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700273 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700274 return 0 != strcmp(buf, "0");
275}
Jason Sams326e0dd2009-05-22 14:03:28 -0700276
277void * Context::threadProc(void *vrsc)
278{
279 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800280 rsc->mNativeThreadId = gettid();
281
282 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800283 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700284
Jason Sams1fddd902009-09-25 15:25:00 -0700285 rsc->props.mLogTimes = getProp("debug.rs.profile");
286 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800287 rsc->props.mLogObjects = getProp("debug.rs.object");
288 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700289
Jason Samse5769102009-06-19 16:03:18 -0700290 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
291 if (!tlsStruct) {
292 LOGE("Error allocating tls storage");
293 return NULL;
294 }
295 tlsStruct->mContext = rsc;
296 tlsStruct->mScript = NULL;
297 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
298 if (status) {
299 LOGE("pthread_setspecific %i", status);
300 }
301
Jason Sams4820e8b2010-02-09 16:05:07 -0800302 if (rsc->mIsGraphicsContext) {
303 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
304 rsc->setRaster(NULL);
305 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
306 rsc->setVertex(NULL);
307 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
308 rsc->setFragment(NULL);
309 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
310 rsc->setFragmentStore(NULL);
311 rsc->mStateVertexArray.init(rsc);
312 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700313
Jason Sams326e0dd2009-05-22 14:03:28 -0700314 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700315 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700316 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700317 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700318 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800319 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700320
Jason Sams2dca84d2009-12-09 11:05:45 -0800321 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800322 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800323 targetTime = rsc->runRootScript();
324 mDraw = targetTime && !rsc->mPaused;
325 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700326 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 rsc->timerFrame();
328 rsc->timerSet(RS_TIMER_INTERNAL);
329 rsc->timerPrint();
330 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700331 }
Jason Sams24371d92009-08-19 12:17:14 -0700332 if (rsc->mObjDestroy.mNeedToEmpty) {
333 rsc->objDestroyOOBRun();
334 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800335 if (rsc->mThreadPriority > 0 && targetTime) {
336 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
337 if (t > 0) {
338 usleep(t);
339 }
340 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700341 }
342
Jason Sams8c0ee652009-08-25 14:49:07 -0700343 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800344 if (rsc->mIsGraphicsContext) {
345 rsc->mRaster.clear();
346 rsc->mFragment.clear();
347 rsc->mVertex.clear();
348 rsc->mFragmentStore.clear();
349 rsc->mRootScript.clear();
350 rsc->mStateRaster.deinit(rsc);
351 rsc->mStateVertex.deinit(rsc);
352 rsc->mStateFragment.deinit(rsc);
353 rsc->mStateFragmentStore.deinit(rsc);
354 }
Jason Samse514b452009-09-25 14:51:22 -0700355 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700356
Jason Samse402ed32009-11-03 11:25:42 -0800357 rsc->mObjDestroy.mNeedToEmpty = true;
358 rsc->objDestroyOOBRun();
359
Jason Sams4820e8b2010-02-09 16:05:07 -0800360 if (rsc->mIsGraphicsContext) {
361 pthread_mutex_lock(&gInitMutex);
362 rsc->deinitEGL();
363 pthread_mutex_unlock(&gInitMutex);
364 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700365
Jason Sams8c0ee652009-08-25 14:49:07 -0700366 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700367 return NULL;
368}
369
Jason Sams15832442009-11-15 12:14:26 -0800370void Context::setPriority(int32_t p)
371{
372 // Note: If we put this in the proper "background" policy
373 // the wallpapers can become completly unresponsive at times.
374 // This is probably not what we want for something the user is actively
375 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800376 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800377#if 0
378 SchedPolicy pol = SP_FOREGROUND;
379 if (p > 0) {
380 pol = SP_BACKGROUND;
381 }
382 if (!set_sched_policy(mNativeThreadId, pol)) {
383 // success; reset the priority as well
384 }
385#else
386 setpriority(PRIO_PROCESS, mNativeThreadId, p);
387#endif
388}
389
Jason Sams4820e8b2010-02-09 16:05:07 -0800390Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700391{
Jason Samsfb03a222009-10-15 16:47:31 -0700392 pthread_mutex_lock(&gInitMutex);
393
Jason Sams326e0dd2009-05-22 14:03:28 -0700394 dev->addContext(this);
395 mDev = dev;
396 mRunning = false;
397 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700398 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700399 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700400 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800401 mError = RS_ERROR_NONE;
402 mErrorMsg = NULL;
403
Jason Sams613cad12009-11-12 15:10:25 -0800404 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800405 memset(&mGL, 0, sizeof(mGL));
406 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700407
Jason Samsa658e902009-06-04 14:35:01 -0700408 int status;
409 pthread_attr_t threadAttr;
410
Jason Samsfb03a222009-10-15 16:47:31 -0700411 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700412 status = pthread_key_create(&gThreadTLSKey, NULL);
413 if (status) {
414 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700415 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700416 return;
417 }
Jason Samse5769102009-06-19 16:03:18 -0700418 }
Jason Samsfb03a222009-10-15 16:47:31 -0700419 gThreadTLSKeyCount++;
420 pthread_mutex_unlock(&gInitMutex);
421
422 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700423
Jason Samsa658e902009-06-04 14:35:01 -0700424 status = pthread_attr_init(&threadAttr);
425 if (status) {
426 LOGE("Failed to init thread attribute.");
427 return;
428 }
429
Jason Sams613cad12009-11-12 15:10:25 -0800430 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700431
Jason Sams50869382009-08-18 17:07:09 -0700432 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700433 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700434 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700435
Jason Sams992a0b72009-06-23 12:22:47 -0700436 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700437 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700438 if (status) {
439 LOGE("Failed to start rs context thread.");
440 }
441
Jason Sams326e0dd2009-05-22 14:03:28 -0700442 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700443 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700444 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700445
Jason Samsa658e902009-06-04 14:35:01 -0700446 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700447}
448
449Context::~Context()
450{
Jason Sams8c0ee652009-08-25 14:49:07 -0700451 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700452 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700453 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700454 void *res;
455
Jason Sams8c0ee652009-08-25 14:49:07 -0700456 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700457 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800458 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700459 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700460
Jason Samsfb03a222009-10-15 16:47:31 -0700461 // Global structure cleanup.
462 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700463 if (mDev) {
464 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700465 --gThreadTLSKeyCount;
466 if (!gThreadTLSKeyCount) {
467 pthread_key_delete(gThreadTLSKey);
468 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800469 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700470 }
Jason Samsfb03a222009-10-15 16:47:31 -0700471 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700472
473 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700474}
475
Mathias Agopian9b97c292010-02-12 12:00:38 -0800476void Context::setSurface(uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800477{
Jason Sams4820e8b2010-02-09 16:05:07 -0800478 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800479
Jason Sams458f2dc2009-11-03 13:58:36 -0800480 EGLBoolean ret;
481 if (mEGL.mSurface != NULL) {
482 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
483 checkEglError("eglMakeCurrent", ret);
484
485 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
486 checkEglError("eglDestroySurface", ret);
487
488 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800489 mEGL.mWidth = 0;
490 mEGL.mHeight = 0;
491 mWidth = 0;
492 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800493 }
494
495 mWndSurface = sur;
496 if (mWndSurface != NULL) {
Jason Sams613cad12009-11-12 15:10:25 -0800497 bool first = false;
498 if (!mEGL.mContext) {
499 first = true;
500 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800501 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800502 pthread_mutex_unlock(&gInitMutex);
503 }
504
Jason Sams458f2dc2009-11-03 13:58:36 -0800505 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
506 checkEglError("eglCreateWindowSurface");
507 if (mEGL.mSurface == EGL_NO_SURFACE) {
508 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
509 }
510
511 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
512 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800513
514 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
515 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
516 mWidth = w;
517 mHeight = h;
Jason Samse18844a2009-11-12 16:09:45 -0800518 mStateVertex.updateSize(this, w, h);
Jason Sams613cad12009-11-12 15:10:25 -0800519
520 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
521 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
522 }
523
524 if (first) {
525 mGL.mVersion = glGetString(GL_VERSION);
526 mGL.mVendor = glGetString(GL_VENDOR);
527 mGL.mRenderer = glGetString(GL_RENDERER);
528 mGL.mExtensions = glGetString(GL_EXTENSIONS);
529
530 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
531 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800532 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800533 LOGV("GL Renderer %s", mGL.mRenderer);
534 //LOGV("GL Extensions %s", mGL.mExtensions);
535
Jason Samsc460e552009-11-25 13:22:07 -0800536 const char *verptr = NULL;
537 if (strlen((const char *)mGL.mVersion) > 9) {
538 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
539 verptr = (const char *)mGL.mVersion + 12;
540 }
541 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
542 verptr = (const char *)mGL.mVersion + 9;
543 }
544 }
545
546 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800547 LOGE("Error, OpenGL ES Lite not supported");
548 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800549 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800550 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800551
552 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
553 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
554 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
555
556 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
557 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
558
559 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
560 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800561
562 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800563 }
564
Jason Sams458f2dc2009-11-03 13:58:36 -0800565 }
566}
567
Jason Sams86f1b232009-09-24 17:38:20 -0700568void Context::pause()
569{
Jason Sams4820e8b2010-02-09 16:05:07 -0800570 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700571 mPaused = true;
572}
573
574void Context::resume()
575{
Jason Sams4820e8b2010-02-09 16:05:07 -0800576 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700577 mPaused = false;
578}
579
Jason Sams326e0dd2009-05-22 14:03:28 -0700580void Context::setRootScript(Script *s)
581{
Jason Sams4820e8b2010-02-09 16:05:07 -0800582 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700583 mRootScript.set(s);
584}
585
586void Context::setFragmentStore(ProgramFragmentStore *pfs)
587{
Jason Sams4820e8b2010-02-09 16:05:07 -0800588 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700589 if (pfs == NULL) {
590 mFragmentStore.set(mStateFragmentStore.mDefault);
591 } else {
592 mFragmentStore.set(pfs);
593 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700594}
595
596void Context::setFragment(ProgramFragment *pf)
597{
Jason Sams4820e8b2010-02-09 16:05:07 -0800598 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700599 if (pf == NULL) {
600 mFragment.set(mStateFragment.mDefault);
601 } else {
602 mFragment.set(pf);
603 }
Jason Samscfb1d112009-08-05 13:57:03 -0700604}
605
Jason Sams5fd09d82009-09-23 13:57:02 -0700606void Context::setRaster(ProgramRaster *pr)
607{
Jason Sams4820e8b2010-02-09 16:05:07 -0800608 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700609 if (pr == NULL) {
610 mRaster.set(mStateRaster.mDefault);
611 } else {
612 mRaster.set(pr);
613 }
614}
615
Jason Sams326e0dd2009-05-22 14:03:28 -0700616void Context::setVertex(ProgramVertex *pv)
617{
Jason Sams4820e8b2010-02-09 16:05:07 -0800618 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700619 if (pv == NULL) {
620 mVertex.set(mStateVertex.mDefault);
621 } else {
622 mVertex.set(pv);
623 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700624}
625
Jason Samsa4a54e42009-06-10 18:39:40 -0700626void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700627{
628 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700629 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700630 mNames.add(obj);
631}
632
633void Context::removeName(ObjectBase *obj)
634{
635 for(size_t ct=0; ct < mNames.size(); ct++) {
636 if (obj == mNames[ct]) {
637 mNames.removeAt(ct);
638 return;
639 }
640 }
641}
642
643ObjectBase * Context::lookupName(const char *name) const
644{
645 for(size_t ct=0; ct < mNames.size(); ct++) {
646 if (!strcmp(name, mNames[ct]->getName())) {
647 return mNames[ct];
648 }
649 }
650 return NULL;
651}
652
Jason Samsa4a54e42009-06-10 18:39:40 -0700653void Context::appendNameDefines(String8 *str) const
654{
655 char buf[256];
656 for (size_t ct=0; ct < mNames.size(); ct++) {
657 str->append("#define NAMED_");
658 str->append(mNames[ct]->getName());
659 str->append(" ");
660 sprintf(buf, "%i\n", (int)mNames[ct]);
661 str->append(buf);
662 }
663}
664
Jason Sams50869382009-08-18 17:07:09 -0700665bool Context::objDestroyOOBInit()
666{
Jason Sams12b14ae2010-03-18 11:39:44 -0700667 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700668 LOGE("Context::ObjDestroyOOBInit mutex init failure");
669 return false;
670 }
671 return true;
672}
673
674void Context::objDestroyOOBRun()
675{
676 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700677 if (!mObjDestroy.mMutex.lock()) {
678 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700679 return;
680 }
681
682 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700683 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700684 }
685 mObjDestroy.mDestroyList.clear();
686 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700687 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700688 }
689}
690
691void Context::objDestroyOOBDestroy()
692{
693 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700694}
695
696void Context::objDestroyAdd(ObjectBase *obj)
697{
Jason Sams12b14ae2010-03-18 11:39:44 -0700698 if (!mObjDestroy.mMutex.lock()) {
699 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700700 return;
701 }
702
703 mObjDestroy.mNeedToEmpty = true;
704 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700705 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700706}
707
Jason Sams8c401ef2009-10-06 13:58:47 -0700708uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
709{
710 //LOGE("getMessageToClient %i %i", bufferLen, wait);
711 if (!wait) {
712 if (mIO.mToClient.isEmpty()) {
713 // No message to get and not going to wait for one.
714 receiveLen = 0;
715 return 0;
716 }
717 }
718
719 //LOGE("getMessageToClient 2 con=%p", this);
720 uint32_t bytesData = 0;
721 uint32_t commandID = 0;
722 const void *d = mIO.mToClient.get(&commandID, &bytesData);
723 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
724
725 *receiveLen = bytesData;
726 if (bufferLen >= bytesData) {
727 memcpy(data, d, bytesData);
728 mIO.mToClient.next();
729 return commandID;
730 }
731 return 0;
732}
733
734bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
735{
736 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
737 if (cmdID == 0) {
738 LOGE("Attempting to send invalid command 0 to client.");
739 return false;
740 }
741 if (!waitForSpace) {
742 if (mIO.mToClient.getFreeSpace() < len) {
743 // Not enough room, and not waiting.
744 return false;
745 }
746 }
747 //LOGE("sendMessageToClient 2");
748 void *p = mIO.mToClient.reserve(len);
749 memcpy(p, data, len);
750 mIO.mToClient.commit(cmdID, len);
751 //LOGE("sendMessageToClient 3");
752 return true;
753}
754
755void Context::initToClient()
756{
757 while(!mRunning) {
758 usleep(100);
759 }
760}
761
762void Context::deinitToClient()
763{
764 mIO.mToClient.shutdown();
765}
Jason Sams50869382009-08-18 17:07:09 -0700766
Jason Samsa2cf7552010-03-03 13:03:18 -0800767const char * Context::getError(RsError *err)
768{
769 *err = mError;
770 mError = RS_ERROR_NONE;
771 if (*err != RS_ERROR_NONE) {
772 return mErrorMsg;
773 }
774 return NULL;
775}
776
777void Context::setError(RsError e, const char *msg)
778{
779 mError = e;
780 mErrorMsg = msg;
781}
782
783
Jason Sams13e26342009-11-24 12:26:35 -0800784void Context::dumpDebug() const
785{
786 LOGE("RS Context debug %p", this);
787 LOGE("RS Context debug");
788
789 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
790 LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext,
791 mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay);
792 LOGE(" GL vendor: %s", mGL.mVendor);
793 LOGE(" GL renderer: %s", mGL.mRenderer);
794 LOGE(" GL Version: %s", mGL.mVersion);
795 LOGE(" GL Extensions: %s", mGL.mExtensions);
796 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
797 LOGE(" RS width %i, height %i", mWidth, mHeight);
798 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
799 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
800
Jason Sams4815c0d2009-12-15 12:58:36 -0800801 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
802 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
803 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
804 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800805}
Jason Samsa4a54e42009-06-10 18:39:40 -0700806
Jason Sams326e0dd2009-05-22 14:03:28 -0700807///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700808//
Jason Sams326e0dd2009-05-22 14:03:28 -0700809
810namespace android {
811namespace renderscript {
812
813
814void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
815{
816 Script *s = static_cast<Script *>(vs);
817 rsc->setRootScript(s);
818}
819
820void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
821{
822 Sampler *s = static_cast<Sampler *>(vs);
823
824 if (slot > RS_MAX_SAMPLER_SLOT) {
825 LOGE("Invalid sampler slot");
826 return;
827 }
828
829 s->bindToContext(&rsc->mStateSampler, slot);
830}
831
832void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
833{
834 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
835 rsc->setFragmentStore(pfs);
836}
837
838void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
839{
840 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
841 rsc->setFragment(pf);
842}
843
Jason Sams5fd09d82009-09-23 13:57:02 -0700844void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
845{
846 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
847 rsc->setRaster(pr);
848}
849
Jason Sams326e0dd2009-05-22 14:03:28 -0700850void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
851{
852 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
853 rsc->setVertex(pv);
854}
855
Jason Samsa4a54e42009-06-10 18:39:40 -0700856void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700857{
858 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700859 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700860}
Jason Sams326e0dd2009-05-22 14:03:28 -0700861
Jason Sams707aaf32009-08-18 14:14:24 -0700862void rsi_ObjDestroy(Context *rsc, void *obj)
863{
864 ObjectBase *ob = static_cast<ObjectBase *>(obj);
865 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700866 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700867}
868
Jason Sams86f1b232009-09-24 17:38:20 -0700869void rsi_ContextPause(Context *rsc)
870{
871 rsc->pause();
872}
873
874void rsi_ContextResume(Context *rsc)
875{
876 rsc->resume();
877}
878
Mathias Agopianfa402862010-02-12 14:04:35 -0800879void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800880{
Mathias Agopianfa402862010-02-12 14:04:35 -0800881 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800882}
883
Jason Sams15832442009-11-15 12:14:26 -0800884void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800885{
Jason Sams15832442009-11-15 12:14:26 -0800886 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800887}
888
Jason Samsc21cf402009-11-17 17:26:46 -0800889void rsi_ContextDump(Context *rsc, int32_t bits)
890{
891 ObjectBase::dumpAll(rsc);
892}
893
Jason Samsa2cf7552010-03-03 13:03:18 -0800894const char * rsi_ContextGetError(Context *rsc, RsError *e)
895{
896 const char *msg = rsc->getError(e);
897 if (*e != RS_ERROR_NONE) {
898 LOGE("RS Error %i %s", *e, msg);
899 }
900 return msg;
901}
902
Jason Sams326e0dd2009-05-22 14:03:28 -0700903}
904}
905
906
Jason Sams4820e8b2010-02-09 16:05:07 -0800907RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700908{
Jason Sams4820e8b2010-02-09 16:05:07 -0800909 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700910 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800911 Context *rsc = new Context(dev, false, false);
912 return rsc;
913}
914
915RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
916{
917 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
918 Device * dev = static_cast<Device *>(vdev);
919 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700920 return rsc;
921}
922
923void rsContextDestroy(RsContext vrsc)
924{
925 Context * rsc = static_cast<Context *>(vrsc);
926 delete rsc;
927}
928
Jason Sams50869382009-08-18 17:07:09 -0700929void rsObjDestroyOOB(RsContext vrsc, void *obj)
930{
931 Context * rsc = static_cast<Context *>(vrsc);
932 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
933}
934
Jason Sams8c401ef2009-10-06 13:58:47 -0700935uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
936{
937 Context * rsc = static_cast<Context *>(vrsc);
938 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
939}
940
941void rsContextInitToClient(RsContext vrsc)
942{
943 Context * rsc = static_cast<Context *>(vrsc);
944 rsc->initToClient();
945}
946
947void rsContextDeinitToClient(RsContext vrsc)
948{
949 Context * rsc = static_cast<Context *>(vrsc);
950 rsc->deinitToClient();
951}
952