blob: 90a2c79267ae21ebc20e9867091f0ec3e21bd01b [file] [log] [blame]
Jason Samsd19f10d2009-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 Agopian3142f4f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsb13ada52009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Mathias Agopian5cbb9402010-02-12 12:00:38 -080022#include <ui/egl/android_natives.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070023
Jason Sams7d787b42009-11-15 12:14:26 -080024#include <sys/types.h>
25#include <sys/resource.h>
26
Joe Onorato9ac2c662009-09-23 16:37:36 -070027#include <cutils/properties.h>
28
Mathias Agopian2db76162010-10-11 17:56:14 -070029#include <EGL/eglext.h>
Jason Sams4b962e52009-06-22 17:15:15 -070030#include <GLES/gl.h>
31#include <GLES/glext.h>
Jason Sams0011bcf2009-12-15 12:58:36 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
Jason Sams4b962e52009-06-22 17:15:15 -070034
Jason Sams7d787b42009-11-15 12:14:26 -080035#include <cutils/sched_policy.h>
36
Jason Samsd19f10d2009-05-22 14:03:28 -070037using namespace android;
38using namespace android::renderscript;
39
Jason Sams462d11b2009-06-19 16:03:18 -070040pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams41c19db92009-10-15 16:47:31 -070041uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams71362202009-10-27 14:44:31 -070042uint32_t Context::gGLContextCount = 0;
Jason Sams41c19db92009-10-15 16:47:31 -070043pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Samsd19f10d2009-05-22 14:03:28 -070044
Jason Sams71362202009-10-27 14:44:31 -070045static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
46 if (returnVal != EGL_TRUE) {
47 fprintf(stderr, "%s() returned %d\n", op, returnVal);
48 }
49
50 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
51 = eglGetError()) {
52 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
53 error);
54 }
55}
56
Jason Samsbb51c402009-11-25 13:22:07 -080057void Context::initEGL(bool useGL2)
Jason Samsd19f10d2009-05-22 14:03:28 -070058{
Jason Samsb13ada52009-08-25 11:34:49 -070059 mEGL.mNumConfigs = -1;
60 EGLint configAttribs[128];
61 EGLint *configAttribsPtr = configAttribs;
Mathias Agopian2db76162010-10-11 17:56:14 -070062 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2,
63 EGL_NONE, GL_NONE, EGL_NONE };
64
65#ifdef HAS_CONTEXT_PRIORITY
66#ifdef EGL_IMG_context_priority
67#warning "using EGL_IMG_context_priority"
68 if (mThreadPriority > 0) {
69 context_attribs2[2] = EGL_CONTEXT_PRIORITY_LEVEL_IMG;
70 context_attribs2[3] = EGL_CONTEXT_PRIORITY_LOW_IMG;
71 }
72#endif
73#endif
Jason Samsd19f10d2009-05-22 14:03:28 -070074
Jason Samsb13ada52009-08-25 11:34:49 -070075 memset(configAttribs, 0, sizeof(configAttribs));
Jason Samsd19f10d2009-05-22 14:03:28 -070076
Jason Samsb13ada52009-08-25 11:34:49 -070077 configAttribsPtr[0] = EGL_SURFACE_TYPE;
78 configAttribsPtr[1] = EGL_WINDOW_BIT;
79 configAttribsPtr += 2;
Jason Samsd19f10d2009-05-22 14:03:28 -070080
Jason Samsbb51c402009-11-25 13:22:07 -080081 if (useGL2) {
82 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
83 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
84 configAttribsPtr += 2;
85 }
86
Jason Samsb13ada52009-08-25 11:34:49 -070087 if (mUseDepth) {
88 configAttribsPtr[0] = EGL_DEPTH_SIZE;
89 configAttribsPtr[1] = 16;
90 configAttribsPtr += 2;
91 }
Jason Sams07ae4062009-08-27 20:23:34 -070092
Jason Samsebfb4362009-09-23 13:57:02 -070093 if (mDev->mForceSW) {
94 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
95 configAttribsPtr[1] = EGL_SLOW_CONFIG;
96 configAttribsPtr += 2;
97 }
98
Jason Samsb13ada52009-08-25 11:34:49 -070099 configAttribsPtr[0] = EGL_NONE;
100 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Samsd19f10d2009-05-22 14:03:28 -0700101
Jason Sams6a17e162009-10-08 12:55:06 -0700102 LOGV("initEGL start");
Jason Samsb13ada52009-08-25 11:34:49 -0700103 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams71362202009-10-27 14:44:31 -0700104 checkEglError("eglGetDisplay");
105
Jason Samsb13ada52009-08-25 11:34:49 -0700106 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams71362202009-10-27 14:44:31 -0700107 checkEglError("eglInitialize");
Jason Samsb13ada52009-08-25 11:34:49 -0700108
109 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
110 if (err) {
Jason Sams07ae4062009-08-27 20:23:34 -0700111 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsb13ada52009-08-25 11:34:49 -0700112 }
113 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
114
Jason Samsb13ada52009-08-25 11:34:49 -0700115
Jason Samsbb51c402009-11-25 13:22:07 -0800116 if (useGL2) {
117 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
118 } else {
119 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
120 }
Jason Sams71362202009-10-27 14:44:31 -0700121 checkEglError("eglCreateContext");
122 if (mEGL.mContext == EGL_NO_CONTEXT) {
123 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
124 }
125 gGLContextCount++;
Jason Samsd19f10d2009-05-22 14:03:28 -0700126}
127
Jason Sams71362202009-10-27 14:44:31 -0700128void Context::deinitEGL()
129{
Jason Sams3bc47d42009-11-12 15:10:25 -0800130 LOGV("deinitEGL");
131 setSurface(0, 0, NULL);
Jason Sams71362202009-10-27 14:44:31 -0700132 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
133 checkEglError("eglDestroyContext");
134
135 gGLContextCount--;
136 if (!gGLContextCount) {
137 eglTerminate(mEGL.mDisplay);
138 }
139}
140
141
Jason Samsb9d5c572009-12-09 11:05:45 -0800142uint32_t Context::runScript(Script *s, uint32_t launchID)
Jason Samsda423d82009-06-09 12:15:30 -0700143{
144 ObjectBaseRef<ProgramFragment> frag(mFragment);
145 ObjectBaseRef<ProgramVertex> vtx(mVertex);
146 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
Jason Sams5235cf32009-09-28 18:12:56 -0700147 ObjectBaseRef<ProgramRaster> raster(mRaster);
Jason Samsda423d82009-06-09 12:15:30 -0700148
Jason Samsb9d5c572009-12-09 11:05:45 -0800149 uint32_t ret = s->run(this, launchID);
Jason Samsda423d82009-06-09 12:15:30 -0700150
Jason Sams3eaa3382009-06-10 15:04:38 -0700151 mFragment.set(frag);
152 mVertex.set(vtx);
153 mFragmentStore.set(store);
Jason Sams5235cf32009-09-28 18:12:56 -0700154 mRaster.set(raster);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700155 return ret;
Jason Samsda423d82009-06-09 12:15:30 -0700156}
157
Jason Sams718cd1f2009-12-23 14:35:29 -0800158void Context::checkError(const char *msg) const
159{
160 GLenum err = glGetError();
161 if (err != GL_NO_ERROR) {
162 LOGE("GL Error, 0x%x, from %s", err, msg);
163 }
164}
Jason Samsda423d82009-06-09 12:15:30 -0700165
Jason Samsb9d5c572009-12-09 11:05:45 -0800166uint32_t Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -0700167{
Jason Samsb9d5c572009-12-09 11:05:45 -0800168 timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsda423d82009-06-09 12:15:30 -0700169 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -0700170
Jason Sams59038ca2009-09-22 12:26:53 -0700171 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
172 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
Jason Samsb13ada52009-08-25 11:34:49 -0700173 glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -0700174 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
175
Jason Sams928f5cf2009-06-08 18:50:13 -0700176 glClearColor(mRootScript->mEnviroment.mClearColor[0],
177 mRootScript->mEnviroment.mClearColor[1],
178 mRootScript->mEnviroment.mClearColor[2],
179 mRootScript->mEnviroment.mClearColor[3]);
Jason Samsb13ada52009-08-25 11:34:49 -0700180 if (mUseDepth) {
181 glDepthMask(GL_TRUE);
182 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
184 } else {
185 glClear(GL_COLOR_BUFFER_BIT);
186 }
Jason Sams67c68442009-08-25 17:09:59 -0700187
Jason Samsb9d5c572009-12-09 11:05:45 -0800188 timerSet(RS_TIMER_SCRIPT);
Jason Sams516c3192009-10-06 13:58:47 -0700189 mStateFragmentStore.mLast.clear();
Jason Samsb9d5c572009-12-09 11:05:45 -0800190 uint32_t ret = runScript(mRootScript.get(), 0);
Jason Samsc7412b32009-10-14 15:43:53 -0700191
Jason Sams718cd1f2009-12-23 14:35:29 -0800192 checkError("runRootScript");
Jason Sams156cce62010-03-03 13:03:18 -0800193 if (mError != RS_ERROR_NONE) {
194 // If we have an error condition we stop rendering until
195 // somthing changes that might fix it.
196 ret = 0;
197 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700198 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700199}
200
Jason Samsf4d16062009-08-19 12:17:14 -0700201uint64_t Context::getTime() const
202{
203 struct timespec t;
204 clock_gettime(CLOCK_MONOTONIC, &t);
205 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
206}
207
208void Context::timerReset()
209{
210 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
211 mTimers[ct] = 0;
212 }
213}
214
215void Context::timerInit()
216{
217 mTimeLast = getTime();
Jason Sams2525a812009-09-03 15:43:13 -0700218 mTimeFrame = mTimeLast;
219 mTimeLastFrame = mTimeLast;
Jason Samsf4d16062009-08-19 12:17:14 -0700220 mTimerActive = RS_TIMER_INTERNAL;
221 timerReset();
222}
223
Jason Sams2525a812009-09-03 15:43:13 -0700224void Context::timerFrame()
225{
226 mTimeLastFrame = mTimeFrame;
227 mTimeFrame = getTime();
228}
229
Jason Samsf4d16062009-08-19 12:17:14 -0700230void Context::timerSet(Timers tm)
231{
232 uint64_t last = mTimeLast;
233 mTimeLast = getTime();
234 mTimers[mTimerActive] += mTimeLast - last;
235 mTimerActive = tm;
236}
237
238void Context::timerPrint()
239{
240 double total = 0;
241 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
242 total += mTimers[ct];
243 }
Jason Sams2525a812009-09-03 15:43:13 -0700244 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800245 mTimeMSLastFrame = frame / 1000000;
246 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
247 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Samsf4d16062009-08-19 12:17:14 -0700248
Jason Samsb9d5c572009-12-09 11:05:45 -0800249
250 if (props.mLogTimes) {
251 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
252 mTimeMSLastFrame,
253 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
254 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
255 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
256 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
257 }
Jason Samsf4d16062009-08-19 12:17:14 -0700258}
259
Jason Sams156cce62010-03-03 13:03:18 -0800260bool Context::setupCheck()
Jason Samsd19f10d2009-05-22 14:03:28 -0700261{
Jason Samsbb51c402009-11-25 13:22:07 -0800262 if (checkVersion2_0()) {
Jason Sams156cce62010-03-03 13:03:18 -0800263 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
264 LOGE("Context::setupCheck() 1 fail");
265 return false;
266 }
Jason Samsbb51c402009-11-25 13:22:07 -0800267
268 mFragmentStore->setupGL2(this, &mStateFragmentStore);
269 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
270 mRaster->setupGL2(this, &mStateRaster);
271 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
272
273 } else {
274 mFragmentStore->setupGL(this, &mStateFragmentStore);
275 mFragment->setupGL(this, &mStateFragment);
276 mRaster->setupGL(this, &mStateRaster);
277 mVertex->setupGL(this, &mStateVertex);
278 }
Jason Sams156cce62010-03-03 13:03:18 -0800279 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700280}
281
Jason Sams66b27712009-09-25 15:25:00 -0700282static bool getProp(const char *str)
Joe Onorato9ac2c662009-09-23 16:37:36 -0700283{
284 char buf[PROPERTY_VALUE_MAX];
Jason Sams66b27712009-09-25 15:25:00 -0700285 property_get(str, buf, "0");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700286 return 0 != strcmp(buf, "0");
287}
Jason Samsd19f10d2009-05-22 14:03:28 -0700288
289void * Context::threadProc(void *vrsc)
290{
291 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams7d787b42009-11-15 12:14:26 -0800292 rsc->mNativeThreadId = gettid();
293
294 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Samsb9d5c572009-12-09 11:05:45 -0800295 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Samsd19f10d2009-05-22 14:03:28 -0700296
Jason Sams66b27712009-09-25 15:25:00 -0700297 rsc->props.mLogTimes = getProp("debug.rs.profile");
298 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Samsa09a6e12010-01-06 11:57:52 -0800299 rsc->props.mLogObjects = getProp("debug.rs.object");
300 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700301
Jason Sams462d11b2009-06-19 16:03:18 -0700302 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
303 if (!tlsStruct) {
304 LOGE("Error allocating tls storage");
305 return NULL;
306 }
307 tlsStruct->mContext = rsc;
308 tlsStruct->mScript = NULL;
309 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
310 if (status) {
311 LOGE("pthread_setspecific %i", status);
312 }
313
Jason Sams704ff642010-02-09 16:05:07 -0800314 if (rsc->mIsGraphicsContext) {
315 rsc->mStateRaster.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
316 rsc->setRaster(NULL);
317 rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
318 rsc->setVertex(NULL);
319 rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
320 rsc->setFragment(NULL);
321 rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
322 rsc->setFragmentStore(NULL);
323 rsc->mStateVertexArray.init(rsc);
324 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700325
Jason Samsd19f10d2009-05-22 14:03:28 -0700326 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700327 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700328 while (!rsc->mExit) {
Jason Samsbc948de2009-08-17 18:35:48 -0700329 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams5f7fc272009-06-18 16:58:42 -0700330 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800331 mDraw &= (rsc->mWndSurface != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700332
Jason Samsb9d5c572009-12-09 11:05:45 -0800333 uint32_t targetTime = 0;
Jason Sams704ff642010-02-09 16:05:07 -0800334 if (mDraw && rsc->mIsGraphicsContext) {
Jason Samsb9d5c572009-12-09 11:05:45 -0800335 targetTime = rsc->runRootScript();
336 mDraw = targetTime && !rsc->mPaused;
337 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsb13ada52009-08-25 11:34:49 -0700338 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Samsb9d5c572009-12-09 11:05:45 -0800339 rsc->timerFrame();
340 rsc->timerSet(RS_TIMER_INTERNAL);
341 rsc->timerPrint();
342 rsc->timerReset();
Jason Samsd19f10d2009-05-22 14:03:28 -0700343 }
Jason Samsf4d16062009-08-19 12:17:14 -0700344 if (rsc->mObjDestroy.mNeedToEmpty) {
345 rsc->objDestroyOOBRun();
346 }
Jason Samsb9d5c572009-12-09 11:05:45 -0800347 if (rsc->mThreadPriority > 0 && targetTime) {
348 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
349 if (t > 0) {
350 usleep(t);
351 }
352 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700353 }
354
Jason Samsf5b45962009-08-25 14:49:07 -0700355 LOGV("RS Thread exiting");
Jason Sams704ff642010-02-09 16:05:07 -0800356 if (rsc->mIsGraphicsContext) {
357 rsc->mRaster.clear();
358 rsc->mFragment.clear();
359 rsc->mVertex.clear();
360 rsc->mFragmentStore.clear();
361 rsc->mRootScript.clear();
362 rsc->mStateRaster.deinit(rsc);
363 rsc->mStateVertex.deinit(rsc);
364 rsc->mStateFragment.deinit(rsc);
365 rsc->mStateFragmentStore.deinit(rsc);
366 }
Jason Samsa9e7a052009-09-25 14:51:22 -0700367 ObjectBase::zeroAllUserRef(rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700368
Jason Sams9d5e03d2009-11-03 11:25:42 -0800369 rsc->mObjDestroy.mNeedToEmpty = true;
370 rsc->objDestroyOOBRun();
371
Jason Sams704ff642010-02-09 16:05:07 -0800372 if (rsc->mIsGraphicsContext) {
373 pthread_mutex_lock(&gInitMutex);
374 rsc->deinitEGL();
375 pthread_mutex_unlock(&gInitMutex);
376 }
Jason Sams71362202009-10-27 14:44:31 -0700377
Jason Samsf5b45962009-08-25 14:49:07 -0700378 LOGV("RS Thread exited");
Jason Samsd19f10d2009-05-22 14:03:28 -0700379 return NULL;
380}
381
Jason Sams7d787b42009-11-15 12:14:26 -0800382void Context::setPriority(int32_t p)
383{
384 // Note: If we put this in the proper "background" policy
385 // the wallpapers can become completly unresponsive at times.
386 // This is probably not what we want for something the user is actively
387 // looking at.
Jason Samsb9d5c572009-12-09 11:05:45 -0800388 mThreadPriority = p;
Jason Sams7d787b42009-11-15 12:14:26 -0800389#if 0
390 SchedPolicy pol = SP_FOREGROUND;
391 if (p > 0) {
392 pol = SP_BACKGROUND;
393 }
394 if (!set_sched_policy(mNativeThreadId, pol)) {
395 // success; reset the priority as well
396 }
397#else
398 setpriority(PRIO_PROCESS, mNativeThreadId, p);
399#endif
400}
401
Jason Sams704ff642010-02-09 16:05:07 -0800402Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Samsd19f10d2009-05-22 14:03:28 -0700403{
Jason Sams41c19db92009-10-15 16:47:31 -0700404 pthread_mutex_lock(&gInitMutex);
405
Jason Samsd19f10d2009-05-22 14:03:28 -0700406 dev->addContext(this);
407 mDev = dev;
408 mRunning = false;
409 mExit = false;
Jason Samsb13ada52009-08-25 11:34:49 -0700410 mUseDepth = useDepth;
Jason Sams65e7aa52009-09-24 17:38:20 -0700411 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700412 mObjHead = NULL;
Jason Sams156cce62010-03-03 13:03:18 -0800413 mError = RS_ERROR_NONE;
414 mErrorMsg = NULL;
415
Jason Sams3bc47d42009-11-12 15:10:25 -0800416 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams704ff642010-02-09 16:05:07 -0800417 memset(&mGL, 0, sizeof(mGL));
418 mIsGraphicsContext = isGraphics;
Jason Samsd19f10d2009-05-22 14:03:28 -0700419
Jason Sams8ad00102009-06-04 14:35:01 -0700420 int status;
421 pthread_attr_t threadAttr;
422
Jason Sams41c19db92009-10-15 16:47:31 -0700423 if (!gThreadTLSKeyCount) {
Jason Sams996db8d2009-10-06 17:16:55 -0700424 status = pthread_key_create(&gThreadTLSKey, NULL);
425 if (status) {
426 LOGE("Failed to init thread tls key.");
Jason Sams41c19db92009-10-15 16:47:31 -0700427 pthread_mutex_unlock(&gInitMutex);
Jason Sams996db8d2009-10-06 17:16:55 -0700428 return;
429 }
Jason Sams462d11b2009-06-19 16:03:18 -0700430 }
Jason Sams41c19db92009-10-15 16:47:31 -0700431 gThreadTLSKeyCount++;
432 pthread_mutex_unlock(&gInitMutex);
433
434 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700435
Jason Sams8ad00102009-06-04 14:35:01 -0700436 status = pthread_attr_init(&threadAttr);
437 if (status) {
438 LOGE("Failed to init thread attribute.");
439 return;
440 }
441
Jason Sams3bc47d42009-11-12 15:10:25 -0800442 mWndSurface = NULL;
Jason Samsf29ca502009-06-23 12:22:47 -0700443
Jason Sams730ee652009-08-18 17:07:09 -0700444 objDestroyOOBInit();
Jason Samsf4d16062009-08-19 12:17:14 -0700445 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700446 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700447
Jason Samsf29ca502009-06-23 12:22:47 -0700448 LOGV("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700449 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700450 if (status) {
451 LOGE("Failed to start rs context thread.");
452 }
453
Jason Samsd19f10d2009-05-22 14:03:28 -0700454 while(!mRunning) {
Jason Samse60446b2009-09-24 14:55:38 -0700455 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700456 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700457
Jason Sams8ad00102009-06-04 14:35:01 -0700458 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700459}
460
461Context::~Context()
462{
Jason Samsf5b45962009-08-25 14:49:07 -0700463 LOGV("Context::~Context");
Jason Samsd19f10d2009-05-22 14:03:28 -0700464 mExit = true;
Jason Sams65e7aa52009-09-24 17:38:20 -0700465 mPaused = false;
Jason Samsd19f10d2009-05-22 14:03:28 -0700466 void *res;
467
Jason Samsf5b45962009-08-25 14:49:07 -0700468 mIO.shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -0700469 int status = pthread_join(mThreadId, &res);
Jason Samsb7a6c432009-11-02 14:25:10 -0800470 mObjDestroy.mNeedToEmpty = true;
Jason Sams730ee652009-08-18 17:07:09 -0700471 objDestroyOOBRun();
Jason Samsd19f10d2009-05-22 14:03:28 -0700472
Jason Sams41c19db92009-10-15 16:47:31 -0700473 // Global structure cleanup.
474 pthread_mutex_lock(&gInitMutex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700475 if (mDev) {
476 mDev->removeContext(this);
Jason Sams41c19db92009-10-15 16:47:31 -0700477 --gThreadTLSKeyCount;
478 if (!gThreadTLSKeyCount) {
479 pthread_key_delete(gThreadTLSKey);
480 }
Jason Samsb7a6c432009-11-02 14:25:10 -0800481 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700482 }
Jason Sams41c19db92009-10-15 16:47:31 -0700483 pthread_mutex_unlock(&gInitMutex);
Jason Sams730ee652009-08-18 17:07:09 -0700484
485 objDestroyOOBDestroy();
Jason Samsd19f10d2009-05-22 14:03:28 -0700486}
487
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700488void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800489{
Jason Sams704ff642010-02-09 16:05:07 -0800490 rsAssert(mIsGraphicsContext);
Jason Sams3bc47d42009-11-12 15:10:25 -0800491
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800492 EGLBoolean ret;
493 if (mEGL.mSurface != NULL) {
494 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
495 checkEglError("eglMakeCurrent", ret);
496
497 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
498 checkEglError("eglDestroySurface", ret);
499
500 mEGL.mSurface = NULL;
Jason Sams3bc47d42009-11-12 15:10:25 -0800501 mEGL.mWidth = 0;
502 mEGL.mHeight = 0;
503 mWidth = 0;
504 mHeight = 0;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800505 }
506
507 mWndSurface = sur;
508 if (mWndSurface != NULL) {
Jason Sams3bc47d42009-11-12 15:10:25 -0800509 bool first = false;
510 if (!mEGL.mContext) {
511 first = true;
512 pthread_mutex_lock(&gInitMutex);
Jason Sams54c0ec12009-11-30 14:49:55 -0800513 initEGL(true);
Jason Sams3bc47d42009-11-12 15:10:25 -0800514 pthread_mutex_unlock(&gInitMutex);
515 }
516
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800517 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
518 checkEglError("eglCreateWindowSurface");
519 if (mEGL.mSurface == EGL_NO_SURFACE) {
520 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
521 }
522
523 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
524 checkEglError("eglMakeCurrent", ret);
Jason Sams3bc47d42009-11-12 15:10:25 -0800525
526 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
527 eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
528 mWidth = w;
529 mHeight = h;
Jason Samseb4b0312009-11-12 16:09:45 -0800530 mStateVertex.updateSize(this, w, h);
Jason Sams3bc47d42009-11-12 15:10:25 -0800531
532 if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
533 LOGE("EGL/Surface mismatch EGL (%i x %i) SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
534 }
535
536 if (first) {
537 mGL.mVersion = glGetString(GL_VERSION);
538 mGL.mVendor = glGetString(GL_VENDOR);
539 mGL.mRenderer = glGetString(GL_RENDERER);
540 mGL.mExtensions = glGetString(GL_EXTENSIONS);
541
542 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
543 LOGV("GL Version %s", mGL.mVersion);
Jason Samsbb51c402009-11-25 13:22:07 -0800544 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams3bc47d42009-11-12 15:10:25 -0800545 LOGV("GL Renderer %s", mGL.mRenderer);
546 //LOGV("GL Extensions %s", mGL.mExtensions);
547
Jason Samsbb51c402009-11-25 13:22:07 -0800548 const char *verptr = NULL;
549 if (strlen((const char *)mGL.mVersion) > 9) {
550 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
551 verptr = (const char *)mGL.mVersion + 12;
552 }
553 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
554 verptr = (const char *)mGL.mVersion + 9;
555 }
556 }
557
558 if (!verptr) {
Jason Sams3bc47d42009-11-12 15:10:25 -0800559 LOGE("Error, OpenGL ES Lite not supported");
560 } else {
Jason Samsbb51c402009-11-25 13:22:07 -0800561 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams3bc47d42009-11-12 15:10:25 -0800562 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800563
564 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
565 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
566 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
567
568 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
569 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
570
571 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
572 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Sams2978bfc2010-02-22 15:37:51 -0800573
574 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Mathias Agopianf9954c72010-12-15 16:59:55 -0800575 mGL.GL_IMG_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_IMG_texture_npot");
Jason Sams3bc47d42009-11-12 15:10:25 -0800576 }
577
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800578 }
579}
580
Jason Sams65e7aa52009-09-24 17:38:20 -0700581void Context::pause()
582{
Jason Sams704ff642010-02-09 16:05:07 -0800583 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700584 mPaused = true;
585}
586
587void Context::resume()
588{
Jason Sams704ff642010-02-09 16:05:07 -0800589 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700590 mPaused = false;
591}
592
Jason Samsd19f10d2009-05-22 14:03:28 -0700593void Context::setRootScript(Script *s)
594{
Jason Sams704ff642010-02-09 16:05:07 -0800595 rsAssert(mIsGraphicsContext);
Jason Samsd19f10d2009-05-22 14:03:28 -0700596 mRootScript.set(s);
597}
598
599void Context::setFragmentStore(ProgramFragmentStore *pfs)
600{
Jason Sams704ff642010-02-09 16:05:07 -0800601 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700602 if (pfs == NULL) {
603 mFragmentStore.set(mStateFragmentStore.mDefault);
604 } else {
605 mFragmentStore.set(pfs);
606 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700607}
608
609void Context::setFragment(ProgramFragment *pf)
610{
Jason Sams704ff642010-02-09 16:05:07 -0800611 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700612 if (pf == NULL) {
613 mFragment.set(mStateFragment.mDefault);
614 } else {
615 mFragment.set(pf);
616 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700617}
618
Jason Samsebfb4362009-09-23 13:57:02 -0700619void Context::setRaster(ProgramRaster *pr)
620{
Jason Sams704ff642010-02-09 16:05:07 -0800621 rsAssert(mIsGraphicsContext);
Jason Samsebfb4362009-09-23 13:57:02 -0700622 if (pr == NULL) {
623 mRaster.set(mStateRaster.mDefault);
624 } else {
625 mRaster.set(pr);
626 }
627}
628
Jason Samsd19f10d2009-05-22 14:03:28 -0700629void Context::setVertex(ProgramVertex *pv)
630{
Jason Sams704ff642010-02-09 16:05:07 -0800631 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700632 if (pv == NULL) {
633 mVertex.set(mStateVertex.mDefault);
634 } else {
635 mVertex.set(pv);
636 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700637}
638
Jason Samsd5680f92009-06-10 18:39:40 -0700639void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700640{
641 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700642 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700643 mNames.add(obj);
644}
645
646void Context::removeName(ObjectBase *obj)
647{
648 for(size_t ct=0; ct < mNames.size(); ct++) {
649 if (obj == mNames[ct]) {
650 mNames.removeAt(ct);
651 return;
652 }
653 }
654}
655
656ObjectBase * Context::lookupName(const char *name) const
657{
658 for(size_t ct=0; ct < mNames.size(); ct++) {
659 if (!strcmp(name, mNames[ct]->getName())) {
660 return mNames[ct];
661 }
662 }
663 return NULL;
664}
665
Jason Samsd5680f92009-06-10 18:39:40 -0700666void Context::appendNameDefines(String8 *str) const
667{
668 char buf[256];
669 for (size_t ct=0; ct < mNames.size(); ct++) {
670 str->append("#define NAMED_");
671 str->append(mNames[ct]->getName());
672 str->append(" ");
673 sprintf(buf, "%i\n", (int)mNames[ct]);
674 str->append(buf);
675 }
676}
677
Jason Sams730ee652009-08-18 17:07:09 -0700678bool Context::objDestroyOOBInit()
679{
680 int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL);
681 if (status) {
682 LOGE("Context::ObjDestroyOOBInit mutex init failure");
683 return false;
684 }
685 return true;
686}
687
688void Context::objDestroyOOBRun()
689{
690 if (mObjDestroy.mNeedToEmpty) {
691 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
692 if (status) {
693 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
694 return;
695 }
696
697 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams07ae4062009-08-27 20:23:34 -0700698 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams730ee652009-08-18 17:07:09 -0700699 }
700 mObjDestroy.mDestroyList.clear();
701 mObjDestroy.mNeedToEmpty = false;
702
703 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
704 if (status) {
705 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
706 }
707 }
708}
709
710void Context::objDestroyOOBDestroy()
711{
712 rsAssert(!mObjDestroy.mNeedToEmpty);
713 pthread_mutex_destroy(&mObjDestroy.mMutex);
714}
715
716void Context::objDestroyAdd(ObjectBase *obj)
717{
718 int status = pthread_mutex_lock(&mObjDestroy.mMutex);
719 if (status) {
720 LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status);
721 return;
722 }
723
724 mObjDestroy.mNeedToEmpty = true;
725 mObjDestroy.mDestroyList.add(obj);
726
727 status = pthread_mutex_unlock(&mObjDestroy.mMutex);
728 if (status) {
729 LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status);
730 }
731}
732
Jason Sams516c3192009-10-06 13:58:47 -0700733uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
734{
735 //LOGE("getMessageToClient %i %i", bufferLen, wait);
736 if (!wait) {
737 if (mIO.mToClient.isEmpty()) {
738 // No message to get and not going to wait for one.
739 receiveLen = 0;
740 return 0;
741 }
742 }
743
744 //LOGE("getMessageToClient 2 con=%p", this);
745 uint32_t bytesData = 0;
746 uint32_t commandID = 0;
747 const void *d = mIO.mToClient.get(&commandID, &bytesData);
748 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
749
750 *receiveLen = bytesData;
751 if (bufferLen >= bytesData) {
752 memcpy(data, d, bytesData);
753 mIO.mToClient.next();
754 return commandID;
755 }
756 return 0;
757}
758
759bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
760{
761 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
762 if (cmdID == 0) {
763 LOGE("Attempting to send invalid command 0 to client.");
764 return false;
765 }
766 if (!waitForSpace) {
767 if (mIO.mToClient.getFreeSpace() < len) {
768 // Not enough room, and not waiting.
769 return false;
770 }
771 }
772 //LOGE("sendMessageToClient 2");
773 void *p = mIO.mToClient.reserve(len);
774 memcpy(p, data, len);
775 mIO.mToClient.commit(cmdID, len);
776 //LOGE("sendMessageToClient 3");
777 return true;
778}
779
780void Context::initToClient()
781{
782 while(!mRunning) {
783 usleep(100);
784 }
785}
786
787void Context::deinitToClient()
788{
789 mIO.mToClient.shutdown();
790}
Jason Sams730ee652009-08-18 17:07:09 -0700791
Jason Sams156cce62010-03-03 13:03:18 -0800792const char * Context::getError(RsError *err)
793{
794 *err = mError;
795 mError = RS_ERROR_NONE;
796 if (*err != RS_ERROR_NONE) {
797 return mErrorMsg;
798 }
799 return NULL;
800}
801
802void Context::setError(RsError e, const char *msg)
803{
804 mError = e;
805 mErrorMsg = msg;
806}
807
808
Jason Sams9dab6672009-11-24 12:26:35 -0800809void Context::dumpDebug() const
810{
811 LOGE("RS Context debug %p", this);
812 LOGE("RS Context debug");
813
814 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
815 LOGE(" EGL context %p surface %p, w=%i h=%i Display=%p", mEGL.mContext,
816 mEGL.mSurface, mEGL.mWidth, mEGL.mHeight, mEGL.mDisplay);
817 LOGE(" GL vendor: %s", mGL.mVendor);
818 LOGE(" GL renderer: %s", mGL.mRenderer);
819 LOGE(" GL Version: %s", mGL.mVersion);
820 LOGE(" GL Extensions: %s", mGL.mExtensions);
821 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
822 LOGE(" RS width %i, height %i", mWidth, mHeight);
823 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
824 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
825
Jason Sams0011bcf2009-12-15 12:58:36 -0800826 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
827 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
828 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
829 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams9dab6672009-11-24 12:26:35 -0800830}
Jason Samsd5680f92009-06-10 18:39:40 -0700831
Jason Samsd19f10d2009-05-22 14:03:28 -0700832///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700833//
Jason Samsd19f10d2009-05-22 14:03:28 -0700834
835namespace android {
836namespace renderscript {
837
838
839void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
840{
841 Script *s = static_cast<Script *>(vs);
842 rsc->setRootScript(s);
843}
844
845void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
846{
847 Sampler *s = static_cast<Sampler *>(vs);
848
849 if (slot > RS_MAX_SAMPLER_SLOT) {
850 LOGE("Invalid sampler slot");
851 return;
852 }
853
854 s->bindToContext(&rsc->mStateSampler, slot);
855}
856
857void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
858{
859 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
860 rsc->setFragmentStore(pfs);
861}
862
863void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
864{
865 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
866 rsc->setFragment(pf);
867}
868
Jason Samsebfb4362009-09-23 13:57:02 -0700869void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
870{
871 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
872 rsc->setRaster(pr);
873}
874
Jason Samsd19f10d2009-05-22 14:03:28 -0700875void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
876{
877 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
878 rsc->setVertex(pv);
879}
880
Jason Samsd5680f92009-06-10 18:39:40 -0700881void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700882{
883 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsd5680f92009-06-10 18:39:40 -0700884 rsc->assignName(ob, name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700885}
Jason Samsd19f10d2009-05-22 14:03:28 -0700886
Jason Sams7ce033d2009-08-18 14:14:24 -0700887void rsi_ObjDestroy(Context *rsc, void *obj)
888{
889 ObjectBase *ob = static_cast<ObjectBase *>(obj);
890 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700891 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700892}
893
Jason Sams65e7aa52009-09-24 17:38:20 -0700894void rsi_ContextPause(Context *rsc)
895{
896 rsc->pause();
897}
898
899void rsi_ContextResume(Context *rsc)
900{
901 rsc->resume();
902}
903
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700904void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800905{
Mathias Agopian128ce4b2010-02-12 14:04:35 -0800906 rsc->setSurface(w, h, sur);
Jason Sams3bc47d42009-11-12 15:10:25 -0800907}
908
Jason Sams7d787b42009-11-15 12:14:26 -0800909void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams3bc47d42009-11-12 15:10:25 -0800910{
Jason Sams7d787b42009-11-15 12:14:26 -0800911 rsc->setPriority(p);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800912}
913
Jason Sams715333b2009-11-17 17:26:46 -0800914void rsi_ContextDump(Context *rsc, int32_t bits)
915{
916 ObjectBase::dumpAll(rsc);
917}
918
Jason Sams156cce62010-03-03 13:03:18 -0800919const char * rsi_ContextGetError(Context *rsc, RsError *e)
920{
921 const char *msg = rsc->getError(e);
922 if (*e != RS_ERROR_NONE) {
923 LOGE("RS Error %i %s", *e, msg);
924 }
925 return msg;
926}
927
Jason Samsd19f10d2009-05-22 14:03:28 -0700928}
929}
930
931
Jason Sams704ff642010-02-09 16:05:07 -0800932RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Samsd19f10d2009-05-22 14:03:28 -0700933{
Jason Sams704ff642010-02-09 16:05:07 -0800934 LOGV("rsContextCreate %p", vdev);
Jason Samsd19f10d2009-05-22 14:03:28 -0700935 Device * dev = static_cast<Device *>(vdev);
Jason Sams704ff642010-02-09 16:05:07 -0800936 Context *rsc = new Context(dev, false, false);
937 return rsc;
938}
939
940RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
941{
942 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
943 Device * dev = static_cast<Device *>(vdev);
944 Context *rsc = new Context(dev, true, useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -0700945 return rsc;
946}
947
948void rsContextDestroy(RsContext vrsc)
949{
950 Context * rsc = static_cast<Context *>(vrsc);
951 delete rsc;
952}
953
Jason Sams730ee652009-08-18 17:07:09 -0700954void rsObjDestroyOOB(RsContext vrsc, void *obj)
955{
956 Context * rsc = static_cast<Context *>(vrsc);
957 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
958}
959
Jason Sams516c3192009-10-06 13:58:47 -0700960uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
961{
962 Context * rsc = static_cast<Context *>(vrsc);
963 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
964}
965
966void rsContextInitToClient(RsContext vrsc)
967{
968 Context * rsc = static_cast<Context *>(vrsc);
969 rsc->initToClient();
970}
971
972void rsContextDeinitToClient(RsContext vrsc)
973{
974 Context * rsc = static_cast<Context *>(vrsc);
975 rsc->deinitToClient();
976}
977