blob: d1784f3e3af4f683899908ca8e1d86b84724f8cb [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>
Jason Sams7bf29dd2010-07-19 15:38:19 -070026#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080027
Joe Onorato76371ff2009-09-23 16:37:36 -070028#include <cutils/properties.h>
29
Jason Sams1aa5a4e2009-06-22 17:15:15 -070030#include <GLES/gl.h>
31#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070034
Jason Sams15832442009-11-15 12:14:26 -080035#include <cutils/sched_policy.h>
36
Jason Sams326e0dd2009-05-22 14:03:28 -070037using namespace android;
38using namespace android::renderscript;
39
Jason Samse5769102009-06-19 16:03:18 -070040pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070041uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070042uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070043pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070044
Jason Sams33b6e3b2009-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 Samsc460e552009-11-25 13:22:07 -080057void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070058{
Jason Samsafcb25c2009-08-25 11:34:49 -070059 mEGL.mNumConfigs = -1;
60 EGLint configAttribs[128];
61 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080062 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070063
Jason Samsafcb25c2009-08-25 11:34:49 -070064 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsafcb25c2009-08-25 11:34:49 -070066 configAttribsPtr[0] = EGL_SURFACE_TYPE;
67 configAttribsPtr[1] = EGL_WINDOW_BIT;
68 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070069
Jason Samsc460e552009-11-25 13:22:07 -080070 if (useGL2) {
71 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
72 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
73 configAttribsPtr += 2;
74 }
75
Jason Samsafcb25c2009-08-25 11:34:49 -070076 if (mUseDepth) {
77 configAttribsPtr[0] = EGL_DEPTH_SIZE;
78 configAttribsPtr[1] = 16;
79 configAttribsPtr += 2;
80 }
Jason Sams9397e302009-08-27 20:23:34 -070081
Jason Sams5fd09d82009-09-23 13:57:02 -070082 if (mDev->mForceSW) {
83 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
84 configAttribsPtr[1] = EGL_SLOW_CONFIG;
85 configAttribsPtr += 2;
86 }
87
Jason Samsafcb25c2009-08-25 11:34:49 -070088 configAttribsPtr[0] = EGL_NONE;
89 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070090
Jason Sams6d751ef2009-10-08 12:55:06 -070091 LOGV("initEGL start");
Jason Samsafcb25c2009-08-25 11:34:49 -070092 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070093 checkEglError("eglGetDisplay");
94
Jason Samsafcb25c2009-08-25 11:34:49 -070095 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070096 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070097
98 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
99 if (err) {
Jason Sams9397e302009-08-27 20:23:34 -0700100 LOGE("couldn't find an EGLConfig matching the screen format\n");
Jason Samsafcb25c2009-08-25 11:34:49 -0700101 }
102 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
103
Jason Samsafcb25c2009-08-25 11:34:49 -0700104
Jason Samsc460e552009-11-25 13:22:07 -0800105 if (useGL2) {
106 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
107 } else {
108 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
109 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700110 checkEglError("eglCreateContext");
111 if (mEGL.mContext == EGL_NO_CONTEXT) {
112 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
113 }
114 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams33b6e3b2009-10-27 14:44:31 -0700117void Context::deinitEGL()
118{
Jason Sams613cad12009-11-12 15:10:25 -0800119 LOGV("deinitEGL");
120 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700121 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
122 checkEglError("eglDestroyContext");
123
124 gGLContextCount--;
125 if (!gGLContextCount) {
126 eglTerminate(mEGL.mDisplay);
127 }
128}
129
130
Jason Samsc61346b2010-05-28 18:23:22 -0700131uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700132{
133 ObjectBaseRef<ProgramFragment> frag(mFragment);
134 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700135 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700136 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700137 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700138
Jason Samsc61346b2010-05-28 18:23:22 -0700139 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700140
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700141 mFragment.set(frag);
142 mVertex.set(vtx);
143 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700144 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700145 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700146 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700147}
148
Jason Samsd01d9702009-12-23 14:35:29 -0800149void Context::checkError(const char *msg) const
150{
151 GLenum err = glGetError();
152 if (err != GL_NO_ERROR) {
153 LOGE("GL Error, 0x%x, from %s", err, msg);
154 }
155}
Jason Sams10308932009-06-09 12:15:30 -0700156
Jason Sams2dca84d2009-12-09 11:05:45 -0800157uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700158{
Jason Sams771565f2010-05-14 15:30:29 -0700159 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700160
Jason Sams2dca84d2009-12-09 11:05:45 -0800161 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700162 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700163 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700164
Jason Samsd01d9702009-12-23 14:35:29 -0800165 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800166 if (mError != RS_ERROR_NONE) {
167 // If we have an error condition we stop rendering until
168 // somthing changes that might fix it.
169 ret = 0;
170 }
Jason Samscfb1d112009-08-05 13:57:03 -0700171 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700172}
173
Jason Sams24371d92009-08-19 12:17:14 -0700174uint64_t Context::getTime() const
175{
176 struct timespec t;
177 clock_gettime(CLOCK_MONOTONIC, &t);
178 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
179}
180
181void Context::timerReset()
182{
183 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
184 mTimers[ct] = 0;
185 }
186}
187
188void Context::timerInit()
189{
190 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700191 mTimeFrame = mTimeLast;
192 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700193 mTimerActive = RS_TIMER_INTERNAL;
194 timerReset();
195}
196
Jason Sams1d54f102009-09-03 15:43:13 -0700197void Context::timerFrame()
198{
199 mTimeLastFrame = mTimeFrame;
200 mTimeFrame = getTime();
201}
202
Jason Sams24371d92009-08-19 12:17:14 -0700203void Context::timerSet(Timers tm)
204{
205 uint64_t last = mTimeLast;
206 mTimeLast = getTime();
207 mTimers[mTimerActive] += mTimeLast - last;
208 mTimerActive = tm;
209}
210
211void Context::timerPrint()
212{
213 double total = 0;
214 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
215 total += mTimers[ct];
216 }
Jason Sams1d54f102009-09-03 15:43:13 -0700217 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800218 mTimeMSLastFrame = frame / 1000000;
219 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
220 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700221
Jason Sams2dca84d2009-12-09 11:05:45 -0800222
223 if (props.mLogTimes) {
224 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
225 mTimeMSLastFrame,
226 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
227 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
228 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
229 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
230 }
Jason Sams24371d92009-08-19 12:17:14 -0700231}
232
Jason Samsa2cf7552010-03-03 13:03:18 -0800233bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700234{
Jason Samsc460e552009-11-25 13:22:07 -0800235 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800236 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
237 LOGE("Context::setupCheck() 1 fail");
238 return false;
239 }
Jason Samsc460e552009-11-25 13:22:07 -0800240
241 mFragmentStore->setupGL2(this, &mStateFragmentStore);
242 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
243 mRaster->setupGL2(this, &mStateRaster);
244 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
245
246 } else {
247 mFragmentStore->setupGL(this, &mStateFragmentStore);
248 mFragment->setupGL(this, &mStateFragment);
249 mRaster->setupGL(this, &mStateRaster);
250 mVertex->setupGL(this, &mStateVertex);
251 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800252 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700253}
254
Jason Sams1fddd902009-09-25 15:25:00 -0700255static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700256{
257 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700258 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 return 0 != strcmp(buf, "0");
260}
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
262void * Context::threadProc(void *vrsc)
263{
264 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800265 rsc->mNativeThreadId = gettid();
266
267 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800268 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700269
Jason Sams1fddd902009-09-25 15:25:00 -0700270 rsc->props.mLogTimes = getProp("debug.rs.profile");
271 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800272 rsc->props.mLogObjects = getProp("debug.rs.object");
273 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700274
Jason Samse5769102009-06-19 16:03:18 -0700275 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
276 if (!tlsStruct) {
277 LOGE("Error allocating tls storage");
278 return NULL;
279 }
280 tlsStruct->mContext = rsc;
281 tlsStruct->mScript = NULL;
282 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
283 if (status) {
284 LOGE("pthread_setspecific %i", status);
285 }
286
Jason Sams4820e8b2010-02-09 16:05:07 -0800287 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700288 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800289 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700290 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800291 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700292 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800293 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700294 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800295 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700296 rsc->mStateFont.init(rsc);
297 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800298 rsc->mStateVertexArray.init(rsc);
299 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700300
Jason Sams326e0dd2009-05-22 14:03:28 -0700301 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700302 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700303 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700304 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700305 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800306 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700307
Jason Sams2dca84d2009-12-09 11:05:45 -0800308 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800309 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800310 targetTime = rsc->runRootScript();
311 mDraw = targetTime && !rsc->mPaused;
312 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700313 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800314 rsc->timerFrame();
315 rsc->timerSet(RS_TIMER_INTERNAL);
316 rsc->timerPrint();
317 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700318 }
Jason Sams24371d92009-08-19 12:17:14 -0700319 if (rsc->mObjDestroy.mNeedToEmpty) {
320 rsc->objDestroyOOBRun();
321 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800322 if (rsc->mThreadPriority > 0 && targetTime) {
323 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
324 if (t > 0) {
325 usleep(t);
326 }
327 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700328 }
329
Jason Sams8c0ee652009-08-25 14:49:07 -0700330 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800331 if (rsc->mIsGraphicsContext) {
332 rsc->mRaster.clear();
333 rsc->mFragment.clear();
334 rsc->mVertex.clear();
335 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700336 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800337 rsc->mRootScript.clear();
338 rsc->mStateRaster.deinit(rsc);
339 rsc->mStateVertex.deinit(rsc);
340 rsc->mStateFragment.deinit(rsc);
341 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700342 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800343 }
Jason Samse514b452009-09-25 14:51:22 -0700344 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700345
Jason Samse402ed32009-11-03 11:25:42 -0800346 rsc->mObjDestroy.mNeedToEmpty = true;
347 rsc->objDestroyOOBRun();
348
Jason Sams4820e8b2010-02-09 16:05:07 -0800349 if (rsc->mIsGraphicsContext) {
350 pthread_mutex_lock(&gInitMutex);
351 rsc->deinitEGL();
352 pthread_mutex_unlock(&gInitMutex);
353 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700354
Jason Sams8c0ee652009-08-25 14:49:07 -0700355 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700356 return NULL;
357}
358
Jason Sams7bf29dd2010-07-19 15:38:19 -0700359void * Context::helperThreadProc(void *vrsc)
360{
361 Context *rsc = static_cast<Context *>(vrsc);
362 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
363
Jason Sams18133402010-07-20 15:09:00 -0700364 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700365
366 rsc->mWorkers.mLaunchSignals[idx].init();
367 rsc->mWorkers.mNativeThreadId[idx] = gettid();
368
369 //cpu_set_t cpset[16];
370 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
371 //LOGE("ret = %i", ret);
372
373//sched_setaffinity
374
375 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
376 while(rsc->mRunning) {
377 rsc->mWorkers.mLaunchSignals[idx].wait();
378 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700379 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
380 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700381 android_atomic_dec(&rsc->mWorkers.mRunningCount);
382 rsc->mWorkers.mCompleteSignal.set();
383 }
Jason Sams18133402010-07-20 15:09:00 -0700384
385 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700386 return NULL;
387}
388
389void Context::launchThreads(WorkerCallback_t cbk, void *data)
390{
391 mWorkers.mLaunchData = data;
392 mWorkers.mLaunchCallback = cbk;
393 mWorkers.mRunningCount = (int)mWorkers.mCount;
394 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
395 mWorkers.mLaunchSignals[ct].set();
396 }
397 while(mWorkers.mRunningCount) {
398 mWorkers.mCompleteSignal.wait();
399 }
400}
401
Jason Sams15832442009-11-15 12:14:26 -0800402void Context::setPriority(int32_t p)
403{
404 // Note: If we put this in the proper "background" policy
405 // the wallpapers can become completly unresponsive at times.
406 // This is probably not what we want for something the user is actively
407 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800408 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800409#if 0
410 SchedPolicy pol = SP_FOREGROUND;
411 if (p > 0) {
412 pol = SP_BACKGROUND;
413 }
414 if (!set_sched_policy(mNativeThreadId, pol)) {
415 // success; reset the priority as well
416 }
417#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700418 setpriority(PRIO_PROCESS, mNativeThreadId, p);
419 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
420 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
421 }
Jason Sams15832442009-11-15 12:14:26 -0800422#endif
423}
424
Jason Sams4820e8b2010-02-09 16:05:07 -0800425Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700426{
Jason Samsfb03a222009-10-15 16:47:31 -0700427 pthread_mutex_lock(&gInitMutex);
428
Jason Sams326e0dd2009-05-22 14:03:28 -0700429 dev->addContext(this);
430 mDev = dev;
431 mRunning = false;
432 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700433 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700434 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700435 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800436 mError = RS_ERROR_NONE;
437 mErrorMsg = NULL;
438
Jason Sams613cad12009-11-12 15:10:25 -0800439 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800440 memset(&mGL, 0, sizeof(mGL));
441 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700442
Jason Samsa658e902009-06-04 14:35:01 -0700443 int status;
444 pthread_attr_t threadAttr;
445
Jason Samsfb03a222009-10-15 16:47:31 -0700446 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700447 status = pthread_key_create(&gThreadTLSKey, NULL);
448 if (status) {
449 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700450 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700451 return;
452 }
Jason Samse5769102009-06-19 16:03:18 -0700453 }
Jason Samsfb03a222009-10-15 16:47:31 -0700454 gThreadTLSKeyCount++;
455 pthread_mutex_unlock(&gInitMutex);
456
457 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700458
Jason Samsa658e902009-06-04 14:35:01 -0700459 status = pthread_attr_init(&threadAttr);
460 if (status) {
461 LOGE("Failed to init thread attribute.");
462 return;
463 }
464
Jason Sams613cad12009-11-12 15:10:25 -0800465 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700466
Jason Sams50869382009-08-18 17:07:09 -0700467 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700468 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700469 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700470
Jason Sams7c7c78a2010-07-26 17:12:55 -0700471 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
472 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
473 if (cpu < 2) cpu = 0;
474
475 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700476 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
477 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
478 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
479 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700480 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700481 if (status) {
482 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700483 return;
484 }
Jason Sams18133402010-07-20 15:09:00 -0700485 while(!mRunning) {
486 usleep(100);
487 }
488
Jason Sams7bf29dd2010-07-19 15:38:19 -0700489 mWorkers.mRunningCount = 0;
490 mWorkers.mLaunchCount = 0;
491 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
492 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
493 if (status) {
494 mWorkers.mCount = ct;
495 LOGE("Created fewer than expected number of RS threads.");
496 break;
497 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700498 }
499
Jason Sams326e0dd2009-05-22 14:03:28 -0700500
Jason Samsa658e902009-06-04 14:35:01 -0700501 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700502}
503
504Context::~Context()
505{
Jason Sams8c0ee652009-08-25 14:49:07 -0700506 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700507 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700508 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700509 void *res;
510
Jason Sams8c0ee652009-08-25 14:49:07 -0700511 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700512 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800513 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700514 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700515
Jason Samsfb03a222009-10-15 16:47:31 -0700516 // Global structure cleanup.
517 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700518 if (mDev) {
519 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700520 --gThreadTLSKeyCount;
521 if (!gThreadTLSKeyCount) {
522 pthread_key_delete(gThreadTLSKey);
523 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800524 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700525 }
Jason Samsfb03a222009-10-15 16:47:31 -0700526 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700527
528 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700529}
530
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700531void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800532{
Jason Sams4820e8b2010-02-09 16:05:07 -0800533 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800534
Jason Sams458f2dc2009-11-03 13:58:36 -0800535 EGLBoolean ret;
536 if (mEGL.mSurface != NULL) {
537 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
538 checkEglError("eglMakeCurrent", ret);
539
540 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
541 checkEglError("eglDestroySurface", ret);
542
543 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800544 mWidth = 0;
545 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800546 }
547
548 mWndSurface = sur;
549 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700550 mWidth = w;
551 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800552 bool first = false;
553 if (!mEGL.mContext) {
554 first = true;
555 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800556 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800557 pthread_mutex_unlock(&gInitMutex);
558 }
559
Jason Sams458f2dc2009-11-03 13:58:36 -0800560 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
561 checkEglError("eglCreateWindowSurface");
562 if (mEGL.mSurface == EGL_NO_SURFACE) {
563 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
564 }
565
566 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
567 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800568
Jason Sams771565f2010-05-14 15:30:29 -0700569 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800570
Jason Sams613cad12009-11-12 15:10:25 -0800571 if (first) {
572 mGL.mVersion = glGetString(GL_VERSION);
573 mGL.mVendor = glGetString(GL_VENDOR);
574 mGL.mRenderer = glGetString(GL_RENDERER);
575 mGL.mExtensions = glGetString(GL_EXTENSIONS);
576
577 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
578 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800579 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800580 LOGV("GL Renderer %s", mGL.mRenderer);
581 //LOGV("GL Extensions %s", mGL.mExtensions);
582
Jason Samsc460e552009-11-25 13:22:07 -0800583 const char *verptr = NULL;
584 if (strlen((const char *)mGL.mVersion) > 9) {
585 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
586 verptr = (const char *)mGL.mVersion + 12;
587 }
588 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
589 verptr = (const char *)mGL.mVersion + 9;
590 }
591 }
592
593 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800594 LOGE("Error, OpenGL ES Lite not supported");
595 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800596 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800597 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800598
599 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
600 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
601 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
602
603 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
604 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
605
606 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
607 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800608
609 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800610 }
611
Jason Sams458f2dc2009-11-03 13:58:36 -0800612 }
613}
614
Jason Sams86f1b232009-09-24 17:38:20 -0700615void Context::pause()
616{
Jason Sams4820e8b2010-02-09 16:05:07 -0800617 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700618 mPaused = true;
619}
620
621void Context::resume()
622{
Jason Sams4820e8b2010-02-09 16:05:07 -0800623 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700624 mPaused = false;
625}
626
Jason Sams326e0dd2009-05-22 14:03:28 -0700627void Context::setRootScript(Script *s)
628{
Jason Sams4820e8b2010-02-09 16:05:07 -0800629 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700630 mRootScript.set(s);
631}
632
Jason Samsccc010b2010-05-13 18:30:11 -0700633void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700634{
Jason Sams4820e8b2010-02-09 16:05:07 -0800635 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700636 if (pfs == NULL) {
637 mFragmentStore.set(mStateFragmentStore.mDefault);
638 } else {
639 mFragmentStore.set(pfs);
640 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700641}
642
643void Context::setFragment(ProgramFragment *pf)
644{
Jason Sams4820e8b2010-02-09 16:05:07 -0800645 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700646 if (pf == NULL) {
647 mFragment.set(mStateFragment.mDefault);
648 } else {
649 mFragment.set(pf);
650 }
Jason Samscfb1d112009-08-05 13:57:03 -0700651}
652
Jason Sams5fd09d82009-09-23 13:57:02 -0700653void Context::setRaster(ProgramRaster *pr)
654{
Jason Sams4820e8b2010-02-09 16:05:07 -0800655 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700656 if (pr == NULL) {
657 mRaster.set(mStateRaster.mDefault);
658 } else {
659 mRaster.set(pr);
660 }
661}
662
Jason Sams326e0dd2009-05-22 14:03:28 -0700663void Context::setVertex(ProgramVertex *pv)
664{
Jason Sams4820e8b2010-02-09 16:05:07 -0800665 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700666 if (pv == NULL) {
667 mVertex.set(mStateVertex.mDefault);
668 } else {
669 mVertex.set(pv);
670 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700671}
672
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700673void Context::setFont(Font *f)
674{
675 rsAssert(mIsGraphicsContext);
676 if (f == NULL) {
677 mFont.set(mStateFont.mDefault);
678 } else {
679 mFont.set(f);
680 }
681}
682
Jason Samsa4a54e42009-06-10 18:39:40 -0700683void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700684{
685 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700686 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700687 mNames.add(obj);
688}
689
690void Context::removeName(ObjectBase *obj)
691{
692 for(size_t ct=0; ct < mNames.size(); ct++) {
693 if (obj == mNames[ct]) {
694 mNames.removeAt(ct);
695 return;
696 }
697 }
698}
699
Jason Sams50869382009-08-18 17:07:09 -0700700bool Context::objDestroyOOBInit()
701{
Jason Sams12b14ae2010-03-18 11:39:44 -0700702 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700703 LOGE("Context::ObjDestroyOOBInit mutex init failure");
704 return false;
705 }
706 return true;
707}
708
709void Context::objDestroyOOBRun()
710{
711 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700712 if (!mObjDestroy.mMutex.lock()) {
713 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700714 return;
715 }
716
717 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700718 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700719 }
720 mObjDestroy.mDestroyList.clear();
721 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700722 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700723 }
724}
725
726void Context::objDestroyOOBDestroy()
727{
728 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700729}
730
731void Context::objDestroyAdd(ObjectBase *obj)
732{
Jason Sams12b14ae2010-03-18 11:39:44 -0700733 if (!mObjDestroy.mMutex.lock()) {
734 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700735 return;
736 }
737
738 mObjDestroy.mNeedToEmpty = true;
739 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700740 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700741}
742
Jason Sams8c401ef2009-10-06 13:58:47 -0700743uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
744{
745 //LOGE("getMessageToClient %i %i", bufferLen, wait);
746 if (!wait) {
747 if (mIO.mToClient.isEmpty()) {
748 // No message to get and not going to wait for one.
749 receiveLen = 0;
750 return 0;
751 }
752 }
753
754 //LOGE("getMessageToClient 2 con=%p", this);
755 uint32_t bytesData = 0;
756 uint32_t commandID = 0;
757 const void *d = mIO.mToClient.get(&commandID, &bytesData);
758 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
759
760 *receiveLen = bytesData;
761 if (bufferLen >= bytesData) {
762 memcpy(data, d, bytesData);
763 mIO.mToClient.next();
764 return commandID;
765 }
766 return 0;
767}
768
769bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
770{
771 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
772 if (cmdID == 0) {
773 LOGE("Attempting to send invalid command 0 to client.");
774 return false;
775 }
776 if (!waitForSpace) {
777 if (mIO.mToClient.getFreeSpace() < len) {
778 // Not enough room, and not waiting.
779 return false;
780 }
781 }
782 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700783 if (len > 0) {
784 void *p = mIO.mToClient.reserve(len);
785 memcpy(p, data, len);
786 mIO.mToClient.commit(cmdID, len);
787 } else {
788 mIO.mToClient.commit(cmdID, 0);
789 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700790 //LOGE("sendMessageToClient 3");
791 return true;
792}
793
794void Context::initToClient()
795{
796 while(!mRunning) {
797 usleep(100);
798 }
799}
800
801void Context::deinitToClient()
802{
803 mIO.mToClient.shutdown();
804}
Jason Sams50869382009-08-18 17:07:09 -0700805
Jason Samsa2cf7552010-03-03 13:03:18 -0800806const char * Context::getError(RsError *err)
807{
808 *err = mError;
809 mError = RS_ERROR_NONE;
810 if (*err != RS_ERROR_NONE) {
811 return mErrorMsg;
812 }
813 return NULL;
814}
815
816void Context::setError(RsError e, const char *msg)
817{
818 mError = e;
819 mErrorMsg = msg;
820}
821
822
Jason Sams13e26342009-11-24 12:26:35 -0800823void Context::dumpDebug() const
824{
825 LOGE("RS Context debug %p", this);
826 LOGE("RS Context debug");
827
828 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700829 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800830 LOGE(" GL vendor: %s", mGL.mVendor);
831 LOGE(" GL renderer: %s", mGL.mRenderer);
832 LOGE(" GL Version: %s", mGL.mVersion);
833 LOGE(" GL Extensions: %s", mGL.mExtensions);
834 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
835 LOGE(" RS width %i, height %i", mWidth, mHeight);
836 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
837 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
838
Jason Sams4815c0d2009-12-15 12:58:36 -0800839 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
840 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
841 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
842 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800843}
Jason Samsa4a54e42009-06-10 18:39:40 -0700844
Jason Sams326e0dd2009-05-22 14:03:28 -0700845///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700846//
Jason Sams326e0dd2009-05-22 14:03:28 -0700847
848namespace android {
849namespace renderscript {
850
Jason Sams8c880902010-06-15 12:15:57 -0700851void rsi_ContextFinish(Context *rsc)
852{
853}
Jason Sams326e0dd2009-05-22 14:03:28 -0700854
855void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
856{
857 Script *s = static_cast<Script *>(vs);
858 rsc->setRootScript(s);
859}
860
861void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
862{
863 Sampler *s = static_cast<Sampler *>(vs);
864
865 if (slot > RS_MAX_SAMPLER_SLOT) {
866 LOGE("Invalid sampler slot");
867 return;
868 }
869
870 s->bindToContext(&rsc->mStateSampler, slot);
871}
872
Jason Samsccc010b2010-05-13 18:30:11 -0700873void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700874{
Jason Samsccc010b2010-05-13 18:30:11 -0700875 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700876 rsc->setFragmentStore(pfs);
877}
878
879void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
880{
881 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
882 rsc->setFragment(pf);
883}
884
Jason Sams5fd09d82009-09-23 13:57:02 -0700885void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
886{
887 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
888 rsc->setRaster(pr);
889}
890
Jason Sams326e0dd2009-05-22 14:03:28 -0700891void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
892{
893 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
894 rsc->setVertex(pv);
895}
896
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700897void rsi_ContextBindFont(Context *rsc, RsFont vfont)
898{
899 Font *font = static_cast<Font *>(vfont);
900 rsc->setFont(font);
901}
902
903
Jason Samsa4a54e42009-06-10 18:39:40 -0700904void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700905{
906 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700907 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700908}
Jason Sams326e0dd2009-05-22 14:03:28 -0700909
Jason Sams707aaf32009-08-18 14:14:24 -0700910void rsi_ObjDestroy(Context *rsc, void *obj)
911{
912 ObjectBase *ob = static_cast<ObjectBase *>(obj);
913 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700914 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700915}
916
Jason Sams86f1b232009-09-24 17:38:20 -0700917void rsi_ContextPause(Context *rsc)
918{
919 rsc->pause();
920}
921
922void rsi_ContextResume(Context *rsc)
923{
924 rsc->resume();
925}
926
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700927void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800928{
Mathias Agopianfa402862010-02-12 14:04:35 -0800929 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800930}
931
Jason Sams15832442009-11-15 12:14:26 -0800932void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800933{
Jason Sams15832442009-11-15 12:14:26 -0800934 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800935}
936
Jason Samsc21cf402009-11-17 17:26:46 -0800937void rsi_ContextDump(Context *rsc, int32_t bits)
938{
939 ObjectBase::dumpAll(rsc);
940}
941
Jason Samsa2cf7552010-03-03 13:03:18 -0800942const char * rsi_ContextGetError(Context *rsc, RsError *e)
943{
944 const char *msg = rsc->getError(e);
945 if (*e != RS_ERROR_NONE) {
946 LOGE("RS Error %i %s", *e, msg);
947 }
948 return msg;
949}
950
Jason Sams326e0dd2009-05-22 14:03:28 -0700951}
952}
953
954
Jason Sams4820e8b2010-02-09 16:05:07 -0800955RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700956{
Jason Sams4820e8b2010-02-09 16:05:07 -0800957 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700958 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800959 Context *rsc = new Context(dev, false, false);
960 return rsc;
961}
962
963RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
964{
965 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
966 Device * dev = static_cast<Device *>(vdev);
967 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700968 return rsc;
969}
970
971void rsContextDestroy(RsContext vrsc)
972{
973 Context * rsc = static_cast<Context *>(vrsc);
974 delete rsc;
975}
976
Jason Sams50869382009-08-18 17:07:09 -0700977void rsObjDestroyOOB(RsContext vrsc, void *obj)
978{
979 Context * rsc = static_cast<Context *>(vrsc);
980 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
981}
982
Jason Sams8c401ef2009-10-06 13:58:47 -0700983uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
984{
985 Context * rsc = static_cast<Context *>(vrsc);
986 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
987}
988
989void rsContextInitToClient(RsContext vrsc)
990{
991 Context * rsc = static_cast<Context *>(vrsc);
992 rsc->initToClient();
993}
994
995void rsContextDeinitToClient(RsContext vrsc)
996{
997 Context * rsc = static_cast<Context *>(vrsc);
998 rsc->deinitToClient();
999}
1000