blob: 1c0395446aa325f7cfea101cd93bf087f43cce3a [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
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700262void Context::displayDebugStats()
263{
264 char buffer[128];
265 sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript);
266 float oldR = mStateVertex.color[0];
267 float oldG = mStateVertex.color[1];
268 float oldB = mStateVertex.color[2];
269 float oldA = mStateVertex.color[3];
270
271 float shadowCol = 0.2f;
272 mStateVertex.color[0] = shadowCol;
273 mStateVertex.color[1] = shadowCol;
274 mStateVertex.color[2] = shadowCol;
275 mStateVertex.color[3] = 1.0f;
276 if (!checkVersion2_0()) {
277 glColor4f(shadowCol, shadowCol, shadowCol, 1.0f);
278 }
279 mStateFont.renderText(buffer, 5, getHeight() - 5);
280
281 float textCol = 0.9f;
282 mStateVertex.color[0] = textCol;
283 mStateVertex.color[1] = textCol;
284 mStateVertex.color[2] = textCol;
285 mStateVertex.color[3] = 1.0f;
286 if (!checkVersion2_0()) {
287 glColor4f(textCol, textCol, textCol, 1.0f);
288 }
289 mStateFont.renderText(buffer, 4, getHeight() - 6);
290
291 mStateVertex.color[0] = oldR;
292 mStateVertex.color[1] = oldG;
293 mStateVertex.color[2] = oldB;
294 mStateVertex.color[3] = oldA;
295 if (!checkVersion2_0()) {
296 glColor4f(oldR, oldG, oldB, oldA);
297 }
298}
299
Jason Sams326e0dd2009-05-22 14:03:28 -0700300void * Context::threadProc(void *vrsc)
301{
302 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800303 rsc->mNativeThreadId = gettid();
304
305 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800306 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700307
Jason Sams1fddd902009-09-25 15:25:00 -0700308 rsc->props.mLogTimes = getProp("debug.rs.profile");
309 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800310 rsc->props.mLogObjects = getProp("debug.rs.object");
311 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700312 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700313
Jason Samse5769102009-06-19 16:03:18 -0700314 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
315 if (!tlsStruct) {
316 LOGE("Error allocating tls storage");
317 return NULL;
318 }
319 tlsStruct->mContext = rsc;
320 tlsStruct->mScript = NULL;
321 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
322 if (status) {
323 LOGE("pthread_setspecific %i", status);
324 }
325
Jason Sams4820e8b2010-02-09 16:05:07 -0800326 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700327 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800328 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700329 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800330 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700331 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800332 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700333 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800334 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700335 rsc->mStateFont.init(rsc);
336 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800337 rsc->mStateVertexArray.init(rsc);
338 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700339
Jason Sams326e0dd2009-05-22 14:03:28 -0700340 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700341 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700342 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700343 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700344 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800345 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700346
Jason Sams2dca84d2009-12-09 11:05:45 -0800347 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800348 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800349 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700350
351 if(rsc->props.mLogVisual) {
352 rsc->displayDebugStats();
353 }
354
Jason Sams2dca84d2009-12-09 11:05:45 -0800355 mDraw = targetTime && !rsc->mPaused;
356 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700357 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800358 rsc->timerFrame();
359 rsc->timerSet(RS_TIMER_INTERNAL);
360 rsc->timerPrint();
361 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700362 }
Jason Sams24371d92009-08-19 12:17:14 -0700363 if (rsc->mObjDestroy.mNeedToEmpty) {
364 rsc->objDestroyOOBRun();
365 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800366 if (rsc->mThreadPriority > 0 && targetTime) {
367 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
368 if (t > 0) {
369 usleep(t);
370 }
371 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700372 }
373
Jason Sams8c0ee652009-08-25 14:49:07 -0700374 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800375 if (rsc->mIsGraphicsContext) {
376 rsc->mRaster.clear();
377 rsc->mFragment.clear();
378 rsc->mVertex.clear();
379 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700380 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800381 rsc->mRootScript.clear();
382 rsc->mStateRaster.deinit(rsc);
383 rsc->mStateVertex.deinit(rsc);
384 rsc->mStateFragment.deinit(rsc);
385 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700386 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800387 }
Jason Samse514b452009-09-25 14:51:22 -0700388 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700389
Jason Samse402ed32009-11-03 11:25:42 -0800390 rsc->mObjDestroy.mNeedToEmpty = true;
391 rsc->objDestroyOOBRun();
392
Jason Sams4820e8b2010-02-09 16:05:07 -0800393 if (rsc->mIsGraphicsContext) {
394 pthread_mutex_lock(&gInitMutex);
395 rsc->deinitEGL();
396 pthread_mutex_unlock(&gInitMutex);
397 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700398
Jason Sams8c0ee652009-08-25 14:49:07 -0700399 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700400 return NULL;
401}
402
Jason Sams7bf29dd2010-07-19 15:38:19 -0700403void * Context::helperThreadProc(void *vrsc)
404{
405 Context *rsc = static_cast<Context *>(vrsc);
406 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
407
Jason Sams18133402010-07-20 15:09:00 -0700408 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700409
410 rsc->mWorkers.mLaunchSignals[idx].init();
411 rsc->mWorkers.mNativeThreadId[idx] = gettid();
412
413 //cpu_set_t cpset[16];
414 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
415 //LOGE("ret = %i", ret);
416
417//sched_setaffinity
418
419 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
420 while(rsc->mRunning) {
421 rsc->mWorkers.mLaunchSignals[idx].wait();
422 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700423 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
424 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700425 android_atomic_dec(&rsc->mWorkers.mRunningCount);
426 rsc->mWorkers.mCompleteSignal.set();
427 }
Jason Sams18133402010-07-20 15:09:00 -0700428
429 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700430 return NULL;
431}
432
433void Context::launchThreads(WorkerCallback_t cbk, void *data)
434{
435 mWorkers.mLaunchData = data;
436 mWorkers.mLaunchCallback = cbk;
437 mWorkers.mRunningCount = (int)mWorkers.mCount;
438 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
439 mWorkers.mLaunchSignals[ct].set();
440 }
441 while(mWorkers.mRunningCount) {
442 mWorkers.mCompleteSignal.wait();
443 }
444}
445
Jason Sams15832442009-11-15 12:14:26 -0800446void Context::setPriority(int32_t p)
447{
448 // Note: If we put this in the proper "background" policy
449 // the wallpapers can become completly unresponsive at times.
450 // This is probably not what we want for something the user is actively
451 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800452 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800453#if 0
454 SchedPolicy pol = SP_FOREGROUND;
455 if (p > 0) {
456 pol = SP_BACKGROUND;
457 }
458 if (!set_sched_policy(mNativeThreadId, pol)) {
459 // success; reset the priority as well
460 }
461#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700462 setpriority(PRIO_PROCESS, mNativeThreadId, p);
463 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
464 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
465 }
Jason Sams15832442009-11-15 12:14:26 -0800466#endif
467}
468
Jason Sams4820e8b2010-02-09 16:05:07 -0800469Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700470{
Jason Samsfb03a222009-10-15 16:47:31 -0700471 pthread_mutex_lock(&gInitMutex);
472
Jason Sams326e0dd2009-05-22 14:03:28 -0700473 dev->addContext(this);
474 mDev = dev;
475 mRunning = false;
476 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700477 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700478 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700479 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800480 mError = RS_ERROR_NONE;
481 mErrorMsg = NULL;
482
Jason Sams613cad12009-11-12 15:10:25 -0800483 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800484 memset(&mGL, 0, sizeof(mGL));
485 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700486
Jason Samsa658e902009-06-04 14:35:01 -0700487 int status;
488 pthread_attr_t threadAttr;
489
Jason Samsfb03a222009-10-15 16:47:31 -0700490 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700491 status = pthread_key_create(&gThreadTLSKey, NULL);
492 if (status) {
493 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700494 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700495 return;
496 }
Jason Samse5769102009-06-19 16:03:18 -0700497 }
Jason Samsfb03a222009-10-15 16:47:31 -0700498 gThreadTLSKeyCount++;
499 pthread_mutex_unlock(&gInitMutex);
500
501 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700502
Jason Samsa658e902009-06-04 14:35:01 -0700503 status = pthread_attr_init(&threadAttr);
504 if (status) {
505 LOGE("Failed to init thread attribute.");
506 return;
507 }
508
Jason Sams613cad12009-11-12 15:10:25 -0800509 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700510
Jason Sams50869382009-08-18 17:07:09 -0700511 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700512 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700513 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700514
Jason Sams7c7c78a2010-07-26 17:12:55 -0700515 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
516 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
517 if (cpu < 2) cpu = 0;
518
519 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700520 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
521 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
522 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
523 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700524 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700525 if (status) {
526 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700527 return;
528 }
Jason Sams18133402010-07-20 15:09:00 -0700529 while(!mRunning) {
530 usleep(100);
531 }
532
Jason Sams7bf29dd2010-07-19 15:38:19 -0700533 mWorkers.mRunningCount = 0;
534 mWorkers.mLaunchCount = 0;
535 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
536 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
537 if (status) {
538 mWorkers.mCount = ct;
539 LOGE("Created fewer than expected number of RS threads.");
540 break;
541 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700542 }
543
Jason Sams326e0dd2009-05-22 14:03:28 -0700544
Jason Samsa658e902009-06-04 14:35:01 -0700545 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700546}
547
548Context::~Context()
549{
Jason Sams8c0ee652009-08-25 14:49:07 -0700550 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700551 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700552 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700553 void *res;
554
Jason Sams8c0ee652009-08-25 14:49:07 -0700555 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700556 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800557 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700558 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700559
Jason Samsfb03a222009-10-15 16:47:31 -0700560 // Global structure cleanup.
561 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700562 if (mDev) {
563 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700564 --gThreadTLSKeyCount;
565 if (!gThreadTLSKeyCount) {
566 pthread_key_delete(gThreadTLSKey);
567 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800568 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700569 }
Jason Samsfb03a222009-10-15 16:47:31 -0700570 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700571
572 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700573}
574
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700575void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800576{
Jason Sams4820e8b2010-02-09 16:05:07 -0800577 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800578
Jason Sams458f2dc2009-11-03 13:58:36 -0800579 EGLBoolean ret;
580 if (mEGL.mSurface != NULL) {
581 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
582 checkEglError("eglMakeCurrent", ret);
583
584 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
585 checkEglError("eglDestroySurface", ret);
586
587 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800588 mWidth = 0;
589 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800590 }
591
592 mWndSurface = sur;
593 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700594 mWidth = w;
595 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800596 bool first = false;
597 if (!mEGL.mContext) {
598 first = true;
599 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800600 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800601 pthread_mutex_unlock(&gInitMutex);
602 }
603
Jason Sams458f2dc2009-11-03 13:58:36 -0800604 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
605 checkEglError("eglCreateWindowSurface");
606 if (mEGL.mSurface == EGL_NO_SURFACE) {
607 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
608 }
609
610 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
611 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800612
Jason Sams771565f2010-05-14 15:30:29 -0700613 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800614
Jason Sams613cad12009-11-12 15:10:25 -0800615 if (first) {
616 mGL.mVersion = glGetString(GL_VERSION);
617 mGL.mVendor = glGetString(GL_VENDOR);
618 mGL.mRenderer = glGetString(GL_RENDERER);
619 mGL.mExtensions = glGetString(GL_EXTENSIONS);
620
621 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
622 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800623 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800624 LOGV("GL Renderer %s", mGL.mRenderer);
625 //LOGV("GL Extensions %s", mGL.mExtensions);
626
Jason Samsc460e552009-11-25 13:22:07 -0800627 const char *verptr = NULL;
628 if (strlen((const char *)mGL.mVersion) > 9) {
629 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
630 verptr = (const char *)mGL.mVersion + 12;
631 }
632 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
633 verptr = (const char *)mGL.mVersion + 9;
634 }
635 }
636
637 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800638 LOGE("Error, OpenGL ES Lite not supported");
639 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800640 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800641 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800642
643 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
644 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
645 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
646
647 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
648 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
649
650 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
651 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800652
653 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800654 }
655
Jason Sams458f2dc2009-11-03 13:58:36 -0800656 }
657}
658
Jason Sams86f1b232009-09-24 17:38:20 -0700659void Context::pause()
660{
Jason Sams4820e8b2010-02-09 16:05:07 -0800661 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700662 mPaused = true;
663}
664
665void Context::resume()
666{
Jason Sams4820e8b2010-02-09 16:05:07 -0800667 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700668 mPaused = false;
669}
670
Jason Sams326e0dd2009-05-22 14:03:28 -0700671void Context::setRootScript(Script *s)
672{
Jason Sams4820e8b2010-02-09 16:05:07 -0800673 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700674 mRootScript.set(s);
675}
676
Jason Samsccc010b2010-05-13 18:30:11 -0700677void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700678{
Jason Sams4820e8b2010-02-09 16:05:07 -0800679 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700680 if (pfs == NULL) {
681 mFragmentStore.set(mStateFragmentStore.mDefault);
682 } else {
683 mFragmentStore.set(pfs);
684 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700685}
686
687void Context::setFragment(ProgramFragment *pf)
688{
Jason Sams4820e8b2010-02-09 16:05:07 -0800689 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700690 if (pf == NULL) {
691 mFragment.set(mStateFragment.mDefault);
692 } else {
693 mFragment.set(pf);
694 }
Jason Samscfb1d112009-08-05 13:57:03 -0700695}
696
Jason Sams5fd09d82009-09-23 13:57:02 -0700697void Context::setRaster(ProgramRaster *pr)
698{
Jason Sams4820e8b2010-02-09 16:05:07 -0800699 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700700 if (pr == NULL) {
701 mRaster.set(mStateRaster.mDefault);
702 } else {
703 mRaster.set(pr);
704 }
705}
706
Jason Sams326e0dd2009-05-22 14:03:28 -0700707void Context::setVertex(ProgramVertex *pv)
708{
Jason Sams4820e8b2010-02-09 16:05:07 -0800709 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700710 if (pv == NULL) {
711 mVertex.set(mStateVertex.mDefault);
712 } else {
713 mVertex.set(pv);
714 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700715}
716
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700717void Context::setFont(Font *f)
718{
719 rsAssert(mIsGraphicsContext);
720 if (f == NULL) {
721 mFont.set(mStateFont.mDefault);
722 } else {
723 mFont.set(f);
724 }
725}
726
Jason Samsa4a54e42009-06-10 18:39:40 -0700727void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700728{
729 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700730 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700731 mNames.add(obj);
732}
733
734void Context::removeName(ObjectBase *obj)
735{
736 for(size_t ct=0; ct < mNames.size(); ct++) {
737 if (obj == mNames[ct]) {
738 mNames.removeAt(ct);
739 return;
740 }
741 }
742}
743
Jason Sams50869382009-08-18 17:07:09 -0700744bool Context::objDestroyOOBInit()
745{
Jason Sams12b14ae2010-03-18 11:39:44 -0700746 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700747 LOGE("Context::ObjDestroyOOBInit mutex init failure");
748 return false;
749 }
750 return true;
751}
752
753void Context::objDestroyOOBRun()
754{
755 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700756 if (!mObjDestroy.mMutex.lock()) {
757 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700758 return;
759 }
760
761 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700762 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700763 }
764 mObjDestroy.mDestroyList.clear();
765 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700766 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700767 }
768}
769
770void Context::objDestroyOOBDestroy()
771{
772 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700773}
774
775void Context::objDestroyAdd(ObjectBase *obj)
776{
Jason Sams12b14ae2010-03-18 11:39:44 -0700777 if (!mObjDestroy.mMutex.lock()) {
778 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700779 return;
780 }
781
782 mObjDestroy.mNeedToEmpty = true;
783 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700784 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700785}
786
Jason Sams8c401ef2009-10-06 13:58:47 -0700787uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
788{
789 //LOGE("getMessageToClient %i %i", bufferLen, wait);
790 if (!wait) {
791 if (mIO.mToClient.isEmpty()) {
792 // No message to get and not going to wait for one.
793 receiveLen = 0;
794 return 0;
795 }
796 }
797
798 //LOGE("getMessageToClient 2 con=%p", this);
799 uint32_t bytesData = 0;
800 uint32_t commandID = 0;
801 const void *d = mIO.mToClient.get(&commandID, &bytesData);
802 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
803
804 *receiveLen = bytesData;
805 if (bufferLen >= bytesData) {
806 memcpy(data, d, bytesData);
807 mIO.mToClient.next();
808 return commandID;
809 }
810 return 0;
811}
812
813bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
814{
815 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
816 if (cmdID == 0) {
817 LOGE("Attempting to send invalid command 0 to client.");
818 return false;
819 }
820 if (!waitForSpace) {
821 if (mIO.mToClient.getFreeSpace() < len) {
822 // Not enough room, and not waiting.
823 return false;
824 }
825 }
826 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700827 if (len > 0) {
828 void *p = mIO.mToClient.reserve(len);
829 memcpy(p, data, len);
830 mIO.mToClient.commit(cmdID, len);
831 } else {
832 mIO.mToClient.commit(cmdID, 0);
833 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700834 //LOGE("sendMessageToClient 3");
835 return true;
836}
837
838void Context::initToClient()
839{
840 while(!mRunning) {
841 usleep(100);
842 }
843}
844
845void Context::deinitToClient()
846{
847 mIO.mToClient.shutdown();
848}
Jason Sams50869382009-08-18 17:07:09 -0700849
Jason Samsa2cf7552010-03-03 13:03:18 -0800850const char * Context::getError(RsError *err)
851{
852 *err = mError;
853 mError = RS_ERROR_NONE;
854 if (*err != RS_ERROR_NONE) {
855 return mErrorMsg;
856 }
857 return NULL;
858}
859
860void Context::setError(RsError e, const char *msg)
861{
862 mError = e;
863 mErrorMsg = msg;
864}
865
866
Jason Sams13e26342009-11-24 12:26:35 -0800867void Context::dumpDebug() const
868{
869 LOGE("RS Context debug %p", this);
870 LOGE("RS Context debug");
871
872 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700873 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800874 LOGE(" GL vendor: %s", mGL.mVendor);
875 LOGE(" GL renderer: %s", mGL.mRenderer);
876 LOGE(" GL Version: %s", mGL.mVersion);
877 LOGE(" GL Extensions: %s", mGL.mExtensions);
878 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
879 LOGE(" RS width %i, height %i", mWidth, mHeight);
880 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
881 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
882
Jason Sams4815c0d2009-12-15 12:58:36 -0800883 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
884 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
885 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
886 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800887}
Jason Samsa4a54e42009-06-10 18:39:40 -0700888
Jason Sams326e0dd2009-05-22 14:03:28 -0700889///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700890//
Jason Sams326e0dd2009-05-22 14:03:28 -0700891
892namespace android {
893namespace renderscript {
894
Jason Sams8c880902010-06-15 12:15:57 -0700895void rsi_ContextFinish(Context *rsc)
896{
897}
Jason Sams326e0dd2009-05-22 14:03:28 -0700898
899void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
900{
901 Script *s = static_cast<Script *>(vs);
902 rsc->setRootScript(s);
903}
904
905void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
906{
907 Sampler *s = static_cast<Sampler *>(vs);
908
909 if (slot > RS_MAX_SAMPLER_SLOT) {
910 LOGE("Invalid sampler slot");
911 return;
912 }
913
914 s->bindToContext(&rsc->mStateSampler, slot);
915}
916
Jason Samsccc010b2010-05-13 18:30:11 -0700917void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700918{
Jason Samsccc010b2010-05-13 18:30:11 -0700919 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700920 rsc->setFragmentStore(pfs);
921}
922
923void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
924{
925 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
926 rsc->setFragment(pf);
927}
928
Jason Sams5fd09d82009-09-23 13:57:02 -0700929void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
930{
931 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
932 rsc->setRaster(pr);
933}
934
Jason Sams326e0dd2009-05-22 14:03:28 -0700935void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
936{
937 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
938 rsc->setVertex(pv);
939}
940
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700941void rsi_ContextBindFont(Context *rsc, RsFont vfont)
942{
943 Font *font = static_cast<Font *>(vfont);
944 rsc->setFont(font);
945}
946
Jason Samsa4a54e42009-06-10 18:39:40 -0700947void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700948{
949 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700950 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700951}
Jason Sams326e0dd2009-05-22 14:03:28 -0700952
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700953void rsi_GetName(Context *rsc, void * obj, const char **name)
954{
955 ObjectBase *ob = static_cast<ObjectBase *>(obj);
956 (*name) = ob->getName();
957}
958
Jason Sams707aaf32009-08-18 14:14:24 -0700959void rsi_ObjDestroy(Context *rsc, void *obj)
960{
961 ObjectBase *ob = static_cast<ObjectBase *>(obj);
962 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700963 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700964}
965
Jason Sams86f1b232009-09-24 17:38:20 -0700966void rsi_ContextPause(Context *rsc)
967{
968 rsc->pause();
969}
970
971void rsi_ContextResume(Context *rsc)
972{
973 rsc->resume();
974}
975
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700976void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800977{
Mathias Agopianfa402862010-02-12 14:04:35 -0800978 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800979}
980
Jason Sams15832442009-11-15 12:14:26 -0800981void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800982{
Jason Sams15832442009-11-15 12:14:26 -0800983 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800984}
985
Jason Samsc21cf402009-11-17 17:26:46 -0800986void rsi_ContextDump(Context *rsc, int32_t bits)
987{
988 ObjectBase::dumpAll(rsc);
989}
990
Jason Samsa2cf7552010-03-03 13:03:18 -0800991const char * rsi_ContextGetError(Context *rsc, RsError *e)
992{
993 const char *msg = rsc->getError(e);
994 if (*e != RS_ERROR_NONE) {
995 LOGE("RS Error %i %s", *e, msg);
996 }
997 return msg;
998}
999
Jason Sams326e0dd2009-05-22 14:03:28 -07001000}
1001}
1002
1003
Jason Sams4820e8b2010-02-09 16:05:07 -08001004RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -07001005{
Jason Sams4820e8b2010-02-09 16:05:07 -08001006 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001007 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001008 Context *rsc = new Context(dev, false, false);
1009 return rsc;
1010}
1011
1012RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
1013{
1014 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
1015 Device * dev = static_cast<Device *>(vdev);
1016 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -07001017 return rsc;
1018}
1019
1020void rsContextDestroy(RsContext vrsc)
1021{
1022 Context * rsc = static_cast<Context *>(vrsc);
1023 delete rsc;
1024}
1025
Jason Sams50869382009-08-18 17:07:09 -07001026void rsObjDestroyOOB(RsContext vrsc, void *obj)
1027{
1028 Context * rsc = static_cast<Context *>(vrsc);
1029 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
1030}
1031
Jason Sams8c401ef2009-10-06 13:58:47 -07001032uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
1033{
1034 Context * rsc = static_cast<Context *>(vrsc);
1035 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
1036}
1037
1038void rsContextInitToClient(RsContext vrsc)
1039{
1040 Context * rsc = static_cast<Context *>(vrsc);
1041 rsc->initToClient();
1042}
1043
1044void rsContextDeinitToClient(RsContext vrsc)
1045{
1046 Context * rsc = static_cast<Context *>(vrsc);
1047 rsc->deinitToClient();
1048}
1049