blob: d8c3861da60eec33dd078f8e415fc21ce56c07a3 [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);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700136 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700137
Jason Samsc61346b2010-05-28 18:23:22 -0700138 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700139
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700140 mFragment.set(frag);
141 mVertex.set(vtx);
142 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700143 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700144 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700145 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700146}
147
Jason Samsd01d9702009-12-23 14:35:29 -0800148void Context::checkError(const char *msg) const
149{
150 GLenum err = glGetError();
151 if (err != GL_NO_ERROR) {
152 LOGE("GL Error, 0x%x, from %s", err, msg);
153 }
154}
Jason Sams10308932009-06-09 12:15:30 -0700155
Jason Sams2dca84d2009-12-09 11:05:45 -0800156uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700157{
Jason Sams771565f2010-05-14 15:30:29 -0700158 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700159
Jason Sams2dca84d2009-12-09 11:05:45 -0800160 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700161 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700162 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700163
Jason Samsd01d9702009-12-23 14:35:29 -0800164 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800165 if (mError != RS_ERROR_NONE) {
166 // If we have an error condition we stop rendering until
167 // somthing changes that might fix it.
168 ret = 0;
169 }
Jason Samscfb1d112009-08-05 13:57:03 -0700170 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700171}
172
Jason Sams24371d92009-08-19 12:17:14 -0700173uint64_t Context::getTime() const
174{
175 struct timespec t;
176 clock_gettime(CLOCK_MONOTONIC, &t);
177 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
178}
179
180void Context::timerReset()
181{
182 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
183 mTimers[ct] = 0;
184 }
185}
186
187void Context::timerInit()
188{
189 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700190 mTimeFrame = mTimeLast;
191 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700192 mTimerActive = RS_TIMER_INTERNAL;
193 timerReset();
194}
195
Jason Sams1d54f102009-09-03 15:43:13 -0700196void Context::timerFrame()
197{
198 mTimeLastFrame = mTimeFrame;
199 mTimeFrame = getTime();
200}
201
Jason Sams24371d92009-08-19 12:17:14 -0700202void Context::timerSet(Timers tm)
203{
204 uint64_t last = mTimeLast;
205 mTimeLast = getTime();
206 mTimers[mTimerActive] += mTimeLast - last;
207 mTimerActive = tm;
208}
209
210void Context::timerPrint()
211{
212 double total = 0;
213 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
214 total += mTimers[ct];
215 }
Jason Sams1d54f102009-09-03 15:43:13 -0700216 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800217 mTimeMSLastFrame = frame / 1000000;
218 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
219 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700220
Jason Sams2dca84d2009-12-09 11:05:45 -0800221
222 if (props.mLogTimes) {
223 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
224 mTimeMSLastFrame,
225 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
226 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
227 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
228 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
229 }
Jason Sams24371d92009-08-19 12:17:14 -0700230}
231
Jason Samsa2cf7552010-03-03 13:03:18 -0800232bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700233{
Jason Samsc460e552009-11-25 13:22:07 -0800234 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800235 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
236 LOGE("Context::setupCheck() 1 fail");
237 return false;
238 }
Jason Samsc460e552009-11-25 13:22:07 -0800239
240 mFragmentStore->setupGL2(this, &mStateFragmentStore);
241 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
242 mRaster->setupGL2(this, &mStateRaster);
243 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
244
245 } else {
246 mFragmentStore->setupGL(this, &mStateFragmentStore);
247 mFragment->setupGL(this, &mStateFragment);
248 mRaster->setupGL(this, &mStateRaster);
249 mVertex->setupGL(this, &mStateVertex);
250 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800251 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700252}
253
Jason Sams1fddd902009-09-25 15:25:00 -0700254static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700255{
256 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700257 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700258 return 0 != strcmp(buf, "0");
259}
Jason Sams326e0dd2009-05-22 14:03:28 -0700260
261void * Context::threadProc(void *vrsc)
262{
263 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800264 rsc->mNativeThreadId = gettid();
265
266 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800267 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700268
Jason Sams1fddd902009-09-25 15:25:00 -0700269 rsc->props.mLogTimes = getProp("debug.rs.profile");
270 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800271 rsc->props.mLogObjects = getProp("debug.rs.object");
272 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700273
Jason Samse5769102009-06-19 16:03:18 -0700274 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
275 if (!tlsStruct) {
276 LOGE("Error allocating tls storage");
277 return NULL;
278 }
279 tlsStruct->mContext = rsc;
280 tlsStruct->mScript = NULL;
281 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
282 if (status) {
283 LOGE("pthread_setspecific %i", status);
284 }
285
Jason Sams4820e8b2010-02-09 16:05:07 -0800286 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700287 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800288 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700289 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800290 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700291 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800292 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700293 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800294 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700295 rsc->mStateFont.init(rsc);
296 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800297 rsc->mStateVertexArray.init(rsc);
298 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700299
Jason Sams326e0dd2009-05-22 14:03:28 -0700300 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700301 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700302 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700303 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700304 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800305 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700306
Jason Sams2dca84d2009-12-09 11:05:45 -0800307 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800308 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800309 targetTime = rsc->runRootScript();
310 mDraw = targetTime && !rsc->mPaused;
311 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700312 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800313 rsc->timerFrame();
314 rsc->timerSet(RS_TIMER_INTERNAL);
315 rsc->timerPrint();
316 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700317 }
Jason Sams24371d92009-08-19 12:17:14 -0700318 if (rsc->mObjDestroy.mNeedToEmpty) {
319 rsc->objDestroyOOBRun();
320 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800321 if (rsc->mThreadPriority > 0 && targetTime) {
322 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
323 if (t > 0) {
324 usleep(t);
325 }
326 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700327 }
328
Jason Sams8c0ee652009-08-25 14:49:07 -0700329 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800330 if (rsc->mIsGraphicsContext) {
331 rsc->mRaster.clear();
332 rsc->mFragment.clear();
333 rsc->mVertex.clear();
334 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700335 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800336 rsc->mRootScript.clear();
337 rsc->mStateRaster.deinit(rsc);
338 rsc->mStateVertex.deinit(rsc);
339 rsc->mStateFragment.deinit(rsc);
340 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700341 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800342 }
Jason Samse514b452009-09-25 14:51:22 -0700343 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700344
Jason Samse402ed32009-11-03 11:25:42 -0800345 rsc->mObjDestroy.mNeedToEmpty = true;
346 rsc->objDestroyOOBRun();
347
Jason Sams4820e8b2010-02-09 16:05:07 -0800348 if (rsc->mIsGraphicsContext) {
349 pthread_mutex_lock(&gInitMutex);
350 rsc->deinitEGL();
351 pthread_mutex_unlock(&gInitMutex);
352 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700353
Jason Sams8c0ee652009-08-25 14:49:07 -0700354 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700355 return NULL;
356}
357
Jason Sams15832442009-11-15 12:14:26 -0800358void Context::setPriority(int32_t p)
359{
360 // Note: If we put this in the proper "background" policy
361 // the wallpapers can become completly unresponsive at times.
362 // This is probably not what we want for something the user is actively
363 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800364 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800365#if 0
366 SchedPolicy pol = SP_FOREGROUND;
367 if (p > 0) {
368 pol = SP_BACKGROUND;
369 }
370 if (!set_sched_policy(mNativeThreadId, pol)) {
371 // success; reset the priority as well
372 }
373#else
374 setpriority(PRIO_PROCESS, mNativeThreadId, p);
375#endif
376}
377
Jason Sams4820e8b2010-02-09 16:05:07 -0800378Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700379{
Jason Samsfb03a222009-10-15 16:47:31 -0700380 pthread_mutex_lock(&gInitMutex);
381
Jason Sams326e0dd2009-05-22 14:03:28 -0700382 dev->addContext(this);
383 mDev = dev;
384 mRunning = false;
385 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700386 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700387 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700388 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800389 mError = RS_ERROR_NONE;
390 mErrorMsg = NULL;
391
Jason Sams613cad12009-11-12 15:10:25 -0800392 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800393 memset(&mGL, 0, sizeof(mGL));
394 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700395
Jason Samsa658e902009-06-04 14:35:01 -0700396 int status;
397 pthread_attr_t threadAttr;
398
Jason Samsfb03a222009-10-15 16:47:31 -0700399 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700400 status = pthread_key_create(&gThreadTLSKey, NULL);
401 if (status) {
402 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700403 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700404 return;
405 }
Jason Samse5769102009-06-19 16:03:18 -0700406 }
Jason Samsfb03a222009-10-15 16:47:31 -0700407 gThreadTLSKeyCount++;
408 pthread_mutex_unlock(&gInitMutex);
409
410 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700411
Jason Samsa658e902009-06-04 14:35:01 -0700412 status = pthread_attr_init(&threadAttr);
413 if (status) {
414 LOGE("Failed to init thread attribute.");
415 return;
416 }
417
Jason Sams613cad12009-11-12 15:10:25 -0800418 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700419
Jason Sams50869382009-08-18 17:07:09 -0700420 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700421 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700422 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700423
Jason Sams992a0b72009-06-23 12:22:47 -0700424 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700425 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700426 if (status) {
427 LOGE("Failed to start rs context thread.");
428 }
429
Jason Sams326e0dd2009-05-22 14:03:28 -0700430 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700431 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700432 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700433
Jason Samsa658e902009-06-04 14:35:01 -0700434 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700435}
436
437Context::~Context()
438{
Jason Sams8c0ee652009-08-25 14:49:07 -0700439 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700440 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700441 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700442 void *res;
443
Jason Sams8c0ee652009-08-25 14:49:07 -0700444 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700445 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800446 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700447 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700448
Jason Samsfb03a222009-10-15 16:47:31 -0700449 // Global structure cleanup.
450 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700451 if (mDev) {
452 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700453 --gThreadTLSKeyCount;
454 if (!gThreadTLSKeyCount) {
455 pthread_key_delete(gThreadTLSKey);
456 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800457 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700458 }
Jason Samsfb03a222009-10-15 16:47:31 -0700459 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700460
461 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700462}
463
Mathias Agopian9b97c292010-02-12 12:00:38 -0800464void Context::setSurface(uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800465{
Jason Sams4820e8b2010-02-09 16:05:07 -0800466 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800467
Jason Sams458f2dc2009-11-03 13:58:36 -0800468 EGLBoolean ret;
469 if (mEGL.mSurface != NULL) {
470 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
471 checkEglError("eglMakeCurrent", ret);
472
473 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
474 checkEglError("eglDestroySurface", ret);
475
476 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800477 mWidth = 0;
478 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800479 }
480
481 mWndSurface = sur;
482 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700483 mWidth = w;
484 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800485 bool first = false;
486 if (!mEGL.mContext) {
487 first = true;
488 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800489 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800490 pthread_mutex_unlock(&gInitMutex);
491 }
492
Jason Sams458f2dc2009-11-03 13:58:36 -0800493 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
494 checkEglError("eglCreateWindowSurface");
495 if (mEGL.mSurface == EGL_NO_SURFACE) {
496 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
497 }
498
499 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
500 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800501
Jason Sams771565f2010-05-14 15:30:29 -0700502 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800503
Jason Sams613cad12009-11-12 15:10:25 -0800504 if (first) {
505 mGL.mVersion = glGetString(GL_VERSION);
506 mGL.mVendor = glGetString(GL_VENDOR);
507 mGL.mRenderer = glGetString(GL_RENDERER);
508 mGL.mExtensions = glGetString(GL_EXTENSIONS);
509
510 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
511 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800512 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800513 LOGV("GL Renderer %s", mGL.mRenderer);
514 //LOGV("GL Extensions %s", mGL.mExtensions);
515
Jason Samsc460e552009-11-25 13:22:07 -0800516 const char *verptr = NULL;
517 if (strlen((const char *)mGL.mVersion) > 9) {
518 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
519 verptr = (const char *)mGL.mVersion + 12;
520 }
521 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
522 verptr = (const char *)mGL.mVersion + 9;
523 }
524 }
525
526 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800527 LOGE("Error, OpenGL ES Lite not supported");
528 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800529 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800530 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800531
532 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
533 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
534 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
535
536 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
537 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
538
539 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
540 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800541
542 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800543 }
544
Jason Sams458f2dc2009-11-03 13:58:36 -0800545 }
546}
547
Jason Sams86f1b232009-09-24 17:38:20 -0700548void Context::pause()
549{
Jason Sams4820e8b2010-02-09 16:05:07 -0800550 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700551 mPaused = true;
552}
553
554void Context::resume()
555{
Jason Sams4820e8b2010-02-09 16:05:07 -0800556 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700557 mPaused = false;
558}
559
Jason Sams326e0dd2009-05-22 14:03:28 -0700560void Context::setRootScript(Script *s)
561{
Jason Sams4820e8b2010-02-09 16:05:07 -0800562 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700563 mRootScript.set(s);
564}
565
Jason Samsccc010b2010-05-13 18:30:11 -0700566void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700567{
Jason Sams4820e8b2010-02-09 16:05:07 -0800568 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700569 if (pfs == NULL) {
570 mFragmentStore.set(mStateFragmentStore.mDefault);
571 } else {
572 mFragmentStore.set(pfs);
573 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700574}
575
576void Context::setFragment(ProgramFragment *pf)
577{
Jason Sams4820e8b2010-02-09 16:05:07 -0800578 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700579 if (pf == NULL) {
580 mFragment.set(mStateFragment.mDefault);
581 } else {
582 mFragment.set(pf);
583 }
Jason Samscfb1d112009-08-05 13:57:03 -0700584}
585
Jason Sams5fd09d82009-09-23 13:57:02 -0700586void Context::setRaster(ProgramRaster *pr)
587{
Jason Sams4820e8b2010-02-09 16:05:07 -0800588 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700589 if (pr == NULL) {
590 mRaster.set(mStateRaster.mDefault);
591 } else {
592 mRaster.set(pr);
593 }
594}
595
Jason Sams326e0dd2009-05-22 14:03:28 -0700596void Context::setVertex(ProgramVertex *pv)
597{
Jason Sams4820e8b2010-02-09 16:05:07 -0800598 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700599 if (pv == NULL) {
600 mVertex.set(mStateVertex.mDefault);
601 } else {
602 mVertex.set(pv);
603 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700604}
605
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700606void Context::setFont(Font *f)
607{
608 rsAssert(mIsGraphicsContext);
609 if (f == NULL) {
610 mFont.set(mStateFont.mDefault);
611 } else {
612 mFont.set(f);
613 }
614}
615
Jason Samsa4a54e42009-06-10 18:39:40 -0700616void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700617{
618 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700619 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700620 mNames.add(obj);
621}
622
623void Context::removeName(ObjectBase *obj)
624{
625 for(size_t ct=0; ct < mNames.size(); ct++) {
626 if (obj == mNames[ct]) {
627 mNames.removeAt(ct);
628 return;
629 }
630 }
631}
632
Jason Sams50869382009-08-18 17:07:09 -0700633bool Context::objDestroyOOBInit()
634{
Jason Sams12b14ae2010-03-18 11:39:44 -0700635 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700636 LOGE("Context::ObjDestroyOOBInit mutex init failure");
637 return false;
638 }
639 return true;
640}
641
642void Context::objDestroyOOBRun()
643{
644 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700645 if (!mObjDestroy.mMutex.lock()) {
646 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700647 return;
648 }
649
650 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700651 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700652 }
653 mObjDestroy.mDestroyList.clear();
654 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700655 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700656 }
657}
658
659void Context::objDestroyOOBDestroy()
660{
661 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700662}
663
664void Context::objDestroyAdd(ObjectBase *obj)
665{
Jason Sams12b14ae2010-03-18 11:39:44 -0700666 if (!mObjDestroy.mMutex.lock()) {
667 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700668 return;
669 }
670
671 mObjDestroy.mNeedToEmpty = true;
672 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700673 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700674}
675
Jason Sams8c401ef2009-10-06 13:58:47 -0700676uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
677{
678 //LOGE("getMessageToClient %i %i", bufferLen, wait);
679 if (!wait) {
680 if (mIO.mToClient.isEmpty()) {
681 // No message to get and not going to wait for one.
682 receiveLen = 0;
683 return 0;
684 }
685 }
686
687 //LOGE("getMessageToClient 2 con=%p", this);
688 uint32_t bytesData = 0;
689 uint32_t commandID = 0;
690 const void *d = mIO.mToClient.get(&commandID, &bytesData);
691 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
692
693 *receiveLen = bytesData;
694 if (bufferLen >= bytesData) {
695 memcpy(data, d, bytesData);
696 mIO.mToClient.next();
697 return commandID;
698 }
699 return 0;
700}
701
702bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
703{
704 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
705 if (cmdID == 0) {
706 LOGE("Attempting to send invalid command 0 to client.");
707 return false;
708 }
709 if (!waitForSpace) {
710 if (mIO.mToClient.getFreeSpace() < len) {
711 // Not enough room, and not waiting.
712 return false;
713 }
714 }
715 //LOGE("sendMessageToClient 2");
716 void *p = mIO.mToClient.reserve(len);
717 memcpy(p, data, len);
718 mIO.mToClient.commit(cmdID, len);
719 //LOGE("sendMessageToClient 3");
720 return true;
721}
722
723void Context::initToClient()
724{
725 while(!mRunning) {
726 usleep(100);
727 }
728}
729
730void Context::deinitToClient()
731{
732 mIO.mToClient.shutdown();
733}
Jason Sams50869382009-08-18 17:07:09 -0700734
Jason Samsa2cf7552010-03-03 13:03:18 -0800735const char * Context::getError(RsError *err)
736{
737 *err = mError;
738 mError = RS_ERROR_NONE;
739 if (*err != RS_ERROR_NONE) {
740 return mErrorMsg;
741 }
742 return NULL;
743}
744
745void Context::setError(RsError e, const char *msg)
746{
747 mError = e;
748 mErrorMsg = msg;
749}
750
751
Jason Sams13e26342009-11-24 12:26:35 -0800752void Context::dumpDebug() const
753{
754 LOGE("RS Context debug %p", this);
755 LOGE("RS Context debug");
756
757 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700758 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800759 LOGE(" GL vendor: %s", mGL.mVendor);
760 LOGE(" GL renderer: %s", mGL.mRenderer);
761 LOGE(" GL Version: %s", mGL.mVersion);
762 LOGE(" GL Extensions: %s", mGL.mExtensions);
763 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
764 LOGE(" RS width %i, height %i", mWidth, mHeight);
765 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
766 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
767
Jason Sams4815c0d2009-12-15 12:58:36 -0800768 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
769 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
770 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
771 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800772}
Jason Samsa4a54e42009-06-10 18:39:40 -0700773
Jason Sams326e0dd2009-05-22 14:03:28 -0700774///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700775//
Jason Sams326e0dd2009-05-22 14:03:28 -0700776
777namespace android {
778namespace renderscript {
779
Jason Sams8c880902010-06-15 12:15:57 -0700780void rsi_ContextFinish(Context *rsc)
781{
782}
Jason Sams326e0dd2009-05-22 14:03:28 -0700783
784void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
785{
786 Script *s = static_cast<Script *>(vs);
787 rsc->setRootScript(s);
788}
789
790void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
791{
792 Sampler *s = static_cast<Sampler *>(vs);
793
794 if (slot > RS_MAX_SAMPLER_SLOT) {
795 LOGE("Invalid sampler slot");
796 return;
797 }
798
799 s->bindToContext(&rsc->mStateSampler, slot);
800}
801
Jason Samsccc010b2010-05-13 18:30:11 -0700802void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700803{
Jason Samsccc010b2010-05-13 18:30:11 -0700804 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700805 rsc->setFragmentStore(pfs);
806}
807
808void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
809{
810 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
811 rsc->setFragment(pf);
812}
813
Jason Sams5fd09d82009-09-23 13:57:02 -0700814void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
815{
816 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
817 rsc->setRaster(pr);
818}
819
Jason Sams326e0dd2009-05-22 14:03:28 -0700820void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
821{
822 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
823 rsc->setVertex(pv);
824}
825
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700826void rsi_ContextBindFont(Context *rsc, RsFont vfont)
827{
828 Font *font = static_cast<Font *>(vfont);
829 rsc->setFont(font);
830}
831
832
Jason Samsa4a54e42009-06-10 18:39:40 -0700833void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700834{
835 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700836 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700837}
Jason Sams326e0dd2009-05-22 14:03:28 -0700838
Jason Sams707aaf32009-08-18 14:14:24 -0700839void rsi_ObjDestroy(Context *rsc, void *obj)
840{
841 ObjectBase *ob = static_cast<ObjectBase *>(obj);
842 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700843 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700844}
845
Jason Sams86f1b232009-09-24 17:38:20 -0700846void rsi_ContextPause(Context *rsc)
847{
848 rsc->pause();
849}
850
851void rsi_ContextResume(Context *rsc)
852{
853 rsc->resume();
854}
855
Mathias Agopianfa402862010-02-12 14:04:35 -0800856void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, android_native_window_t *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800857{
Mathias Agopianfa402862010-02-12 14:04:35 -0800858 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800859}
860
Jason Sams15832442009-11-15 12:14:26 -0800861void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800862{
Jason Sams15832442009-11-15 12:14:26 -0800863 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800864}
865
Jason Samsc21cf402009-11-17 17:26:46 -0800866void rsi_ContextDump(Context *rsc, int32_t bits)
867{
868 ObjectBase::dumpAll(rsc);
869}
870
Jason Samsa2cf7552010-03-03 13:03:18 -0800871const char * rsi_ContextGetError(Context *rsc, RsError *e)
872{
873 const char *msg = rsc->getError(e);
874 if (*e != RS_ERROR_NONE) {
875 LOGE("RS Error %i %s", *e, msg);
876 }
877 return msg;
878}
879
Jason Sams326e0dd2009-05-22 14:03:28 -0700880}
881}
882
883
Jason Sams4820e8b2010-02-09 16:05:07 -0800884RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700885{
Jason Sams4820e8b2010-02-09 16:05:07 -0800886 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700887 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800888 Context *rsc = new Context(dev, false, false);
889 return rsc;
890}
891
892RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
893{
894 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
895 Device * dev = static_cast<Device *>(vdev);
896 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700897 return rsc;
898}
899
900void rsContextDestroy(RsContext vrsc)
901{
902 Context * rsc = static_cast<Context *>(vrsc);
903 delete rsc;
904}
905
Jason Sams50869382009-08-18 17:07:09 -0700906void rsObjDestroyOOB(RsContext vrsc, void *obj)
907{
908 Context * rsc = static_cast<Context *>(vrsc);
909 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
910}
911
Jason Sams8c401ef2009-10-06 13:58:47 -0700912uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
913{
914 Context * rsc = static_cast<Context *>(vrsc);
915 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
916}
917
918void rsContextInitToClient(RsContext vrsc)
919{
920 Context * rsc = static_cast<Context *>(vrsc);
921 rsc->initToClient();
922}
923
924void rsContextDeinitToClient(RsContext vrsc)
925{
926 Context * rsc = static_cast<Context *>(vrsc);
927 rsc->deinitToClient();
928}
929