blob: b4d501476f1b843ca9f212af8b0e5ee1f114421e [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>
Jason Sams8d957fa2010-09-28 14:41:22 -070036#include <sys/syscall.h>
37#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080038
Jason Sams326e0dd2009-05-22 14:03:28 -070039using namespace android;
40using namespace android::renderscript;
41
Jason Samse5769102009-06-19 16:03:18 -070042pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070043uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070044uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070045pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070046
Jason Sams33b6e3b2009-10-27 14:44:31 -070047static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
48 if (returnVal != EGL_TRUE) {
49 fprintf(stderr, "%s() returned %d\n", op, returnVal);
50 }
51
52 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
53 = eglGetError()) {
54 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
55 error);
56 }
57}
58
Jason Samsc460e552009-11-25 13:22:07 -080059void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070060{
Jason Samsafcb25c2009-08-25 11:34:49 -070061 mEGL.mNumConfigs = -1;
62 EGLint configAttribs[128];
63 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080064 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsafcb25c2009-08-25 11:34:49 -070066 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070067
Jason Samsafcb25c2009-08-25 11:34:49 -070068 configAttribsPtr[0] = EGL_SURFACE_TYPE;
69 configAttribsPtr[1] = EGL_WINDOW_BIT;
70 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070071
Jason Samsc460e552009-11-25 13:22:07 -080072 if (useGL2) {
73 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
74 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
75 configAttribsPtr += 2;
76 }
77
Jason Samsafcb25c2009-08-25 11:34:49 -070078 if (mUseDepth) {
79 configAttribsPtr[0] = EGL_DEPTH_SIZE;
80 configAttribsPtr[1] = 16;
81 configAttribsPtr += 2;
82 }
Jason Sams9397e302009-08-27 20:23:34 -070083
Jason Sams5fd09d82009-09-23 13:57:02 -070084 if (mDev->mForceSW) {
85 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
86 configAttribsPtr[1] = EGL_SLOW_CONFIG;
87 configAttribsPtr += 2;
88 }
89
Jason Samsafcb25c2009-08-25 11:34:49 -070090 configAttribsPtr[0] = EGL_NONE;
91 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Jason Sams4c5f99e2010-09-14 14:59:03 -070093 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -070094 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070095 checkEglError("eglGetDisplay");
96
Jason Samsafcb25c2009-08-25 11:34:49 -070097 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070098 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070099
100 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
101 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700102 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700103 }
104 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
105
Jason Samsafcb25c2009-08-25 11:34:49 -0700106
Jason Samsc460e552009-11-25 13:22:07 -0800107 if (useGL2) {
108 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
109 } else {
110 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
111 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700112 checkEglError("eglCreateContext");
113 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700114 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700115 }
116 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700117}
118
Jason Sams33b6e3b2009-10-27 14:44:31 -0700119void Context::deinitEGL()
120{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700121 LOGV("%p, deinitEGL", this);
Jason Sams613cad12009-11-12 15:10:25 -0800122 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700123 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
124 checkEglError("eglDestroyContext");
125
126 gGLContextCount--;
127 if (!gGLContextCount) {
128 eglTerminate(mEGL.mDisplay);
129 }
130}
131
132
Jason Samsc61346b2010-05-28 18:23:22 -0700133uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700134{
135 ObjectBaseRef<ProgramFragment> frag(mFragment);
136 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700137 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700138 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700139 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700140
Jason Samsc61346b2010-05-28 18:23:22 -0700141 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700142
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700143 mFragment.set(frag);
144 mVertex.set(vtx);
145 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700146 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700147 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700148 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700149}
150
Jason Samsd01d9702009-12-23 14:35:29 -0800151void Context::checkError(const char *msg) const
152{
153 GLenum err = glGetError();
154 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700155 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800156 }
157}
Jason Sams10308932009-06-09 12:15:30 -0700158
Jason Sams2dca84d2009-12-09 11:05:45 -0800159uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700160{
Jason Sams771565f2010-05-14 15:30:29 -0700161 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700162
Jason Sams2dca84d2009-12-09 11:05:45 -0800163 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700164 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700165 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700166
Jason Samsd01d9702009-12-23 14:35:29 -0800167 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700168 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700169}
170
Jason Sams24371d92009-08-19 12:17:14 -0700171uint64_t Context::getTime() const
172{
173 struct timespec t;
174 clock_gettime(CLOCK_MONOTONIC, &t);
175 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
176}
177
178void Context::timerReset()
179{
180 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
181 mTimers[ct] = 0;
182 }
183}
184
185void Context::timerInit()
186{
187 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700188 mTimeFrame = mTimeLast;
189 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700190 mTimerActive = RS_TIMER_INTERNAL;
191 timerReset();
192}
193
Jason Sams1d54f102009-09-03 15:43:13 -0700194void Context::timerFrame()
195{
196 mTimeLastFrame = mTimeFrame;
197 mTimeFrame = getTime();
198}
199
Jason Sams24371d92009-08-19 12:17:14 -0700200void Context::timerSet(Timers tm)
201{
202 uint64_t last = mTimeLast;
203 mTimeLast = getTime();
204 mTimers[mTimerActive] += mTimeLast - last;
205 mTimerActive = tm;
206}
207
208void Context::timerPrint()
209{
210 double total = 0;
211 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
212 total += mTimers[ct];
213 }
Jason Sams1d54f102009-09-03 15:43:13 -0700214 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800215 mTimeMSLastFrame = frame / 1000000;
216 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
217 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700218
Jason Sams2dca84d2009-12-09 11:05:45 -0800219
220 if (props.mLogTimes) {
221 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
222 mTimeMSLastFrame,
223 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
224 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
225 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
226 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
227 }
Jason Sams24371d92009-08-19 12:17:14 -0700228}
229
Jason Samsa2cf7552010-03-03 13:03:18 -0800230bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700231{
Jason Sams900f1612010-09-16 18:18:29 -0700232 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
233 LOGE("Context::setupCheck() 1 fail");
234 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800235 }
Jason Sams900f1612010-09-16 18:18:29 -0700236
237 mFragmentStore->setupGL2(this, &mStateFragmentStore);
238 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
239 mRaster->setupGL2(this, &mStateRaster);
240 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800241 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700242}
243
Jason Sams1fddd902009-09-25 15:25:00 -0700244static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700245{
246 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700247 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700248 return 0 != strcmp(buf, "0");
249}
Jason Sams326e0dd2009-05-22 14:03:28 -0700250
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700251void Context::displayDebugStats()
252{
253 char buffer[128];
254 sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700255 float oldR, oldG, oldB, oldA;
256 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700257
258 float shadowCol = 0.2f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700259 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700260 mStateFont.renderText(buffer, 5, getHeight() - 5);
261
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700262 float textCol = 0.9f;
263 mStateFont.setFontColor(textCol, textCol, textCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700264 mStateFont.renderText(buffer, 4, getHeight() - 6);
265
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700266 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700267}
268
Jason Sams326e0dd2009-05-22 14:03:28 -0700269void * Context::threadProc(void *vrsc)
270{
271 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800272 rsc->mNativeThreadId = gettid();
273
274 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800275 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700276
Jason Sams1fddd902009-09-25 15:25:00 -0700277 rsc->props.mLogTimes = getProp("debug.rs.profile");
278 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800279 rsc->props.mLogObjects = getProp("debug.rs.object");
280 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700281 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
282 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700283 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700284
Jason Samse5769102009-06-19 16:03:18 -0700285 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
286 if (!tlsStruct) {
287 LOGE("Error allocating tls storage");
288 return NULL;
289 }
290 tlsStruct->mContext = rsc;
291 tlsStruct->mScript = NULL;
292 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
293 if (status) {
294 LOGE("pthread_setspecific %i", status);
295 }
296
Jason Sams4820e8b2010-02-09 16:05:07 -0800297 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700298 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800299 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700300 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800301 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700302 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800303 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700304 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800305 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700306 rsc->mStateFont.init(rsc);
307 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800308 rsc->mStateVertexArray.init(rsc);
309 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700310
Jason Sams326e0dd2009-05-22 14:03:28 -0700311 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700312 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700313 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700314 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700315 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800316 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700317
Jason Sams2dca84d2009-12-09 11:05:45 -0800318 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800319 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800320 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700321
322 if(rsc->props.mLogVisual) {
323 rsc->displayDebugStats();
324 }
325
Jason Sams2dca84d2009-12-09 11:05:45 -0800326 mDraw = targetTime && !rsc->mPaused;
327 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700328 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800329 rsc->timerFrame();
330 rsc->timerSet(RS_TIMER_INTERNAL);
331 rsc->timerPrint();
332 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700333 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800334 if (rsc->mThreadPriority > 0 && targetTime) {
335 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
336 if (t > 0) {
337 usleep(t);
338 }
339 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700340 }
341
Jason Sams4c5f99e2010-09-14 14:59:03 -0700342 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800343 if (rsc->mIsGraphicsContext) {
344 rsc->mRaster.clear();
345 rsc->mFragment.clear();
346 rsc->mVertex.clear();
347 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700348 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800349 rsc->mRootScript.clear();
350 rsc->mStateRaster.deinit(rsc);
351 rsc->mStateVertex.deinit(rsc);
352 rsc->mStateFragment.deinit(rsc);
353 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700354 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800355 }
Jason Samse514b452009-09-25 14:51:22 -0700356 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700357
Jason Sams4820e8b2010-02-09 16:05:07 -0800358 if (rsc->mIsGraphicsContext) {
359 pthread_mutex_lock(&gInitMutex);
360 rsc->deinitEGL();
361 pthread_mutex_unlock(&gInitMutex);
362 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700363
Jason Sams4c5f99e2010-09-14 14:59:03 -0700364 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700365 return NULL;
366}
367
Jason Sams7bf29dd2010-07-19 15:38:19 -0700368void * Context::helperThreadProc(void *vrsc)
369{
370 Context *rsc = static_cast<Context *>(vrsc);
371 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
372
Jason Sams18133402010-07-20 15:09:00 -0700373 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700374
375 rsc->mWorkers.mLaunchSignals[idx].init();
376 rsc->mWorkers.mNativeThreadId[idx] = gettid();
377
Jason Sams8d957fa2010-09-28 14:41:22 -0700378#if 0
379 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
380 cpu_set_t cpuset;
381 memset(&cpuset, 0, sizeof(cpuset));
382 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
383 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700384 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700385 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
386#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700387
388 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
389 while(rsc->mRunning) {
390 rsc->mWorkers.mLaunchSignals[idx].wait();
391 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700392 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
393 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700394 android_atomic_dec(&rsc->mWorkers.mRunningCount);
395 rsc->mWorkers.mCompleteSignal.set();
396 }
Jason Sams18133402010-07-20 15:09:00 -0700397
398 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700399 return NULL;
400}
401
402void Context::launchThreads(WorkerCallback_t cbk, void *data)
403{
404 mWorkers.mLaunchData = data;
405 mWorkers.mLaunchCallback = cbk;
406 mWorkers.mRunningCount = (int)mWorkers.mCount;
407 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
408 mWorkers.mLaunchSignals[ct].set();
409 }
410 while(mWorkers.mRunningCount) {
411 mWorkers.mCompleteSignal.wait();
412 }
413}
414
Jason Sams15832442009-11-15 12:14:26 -0800415void Context::setPriority(int32_t p)
416{
417 // Note: If we put this in the proper "background" policy
418 // the wallpapers can become completly unresponsive at times.
419 // This is probably not what we want for something the user is actively
420 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800421 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800422#if 0
423 SchedPolicy pol = SP_FOREGROUND;
424 if (p > 0) {
425 pol = SP_BACKGROUND;
426 }
427 if (!set_sched_policy(mNativeThreadId, pol)) {
428 // success; reset the priority as well
429 }
430#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700431 setpriority(PRIO_PROCESS, mNativeThreadId, p);
432 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
433 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
434 }
Jason Sams15832442009-11-15 12:14:26 -0800435#endif
436}
437
Jason Sams4820e8b2010-02-09 16:05:07 -0800438Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700439{
Jason Samsfb03a222009-10-15 16:47:31 -0700440 pthread_mutex_lock(&gInitMutex);
441
Jason Sams326e0dd2009-05-22 14:03:28 -0700442 dev->addContext(this);
443 mDev = dev;
444 mRunning = false;
445 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700446 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700447 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700448 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800449 mError = RS_ERROR_NONE;
450 mErrorMsg = NULL;
451
Jason Sams613cad12009-11-12 15:10:25 -0800452 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800453 memset(&mGL, 0, sizeof(mGL));
454 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700455
Jason Samsa658e902009-06-04 14:35:01 -0700456 int status;
457 pthread_attr_t threadAttr;
458
Jason Samsfb03a222009-10-15 16:47:31 -0700459 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700460 status = pthread_key_create(&gThreadTLSKey, NULL);
461 if (status) {
462 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700463 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700464 return;
465 }
Jason Samse5769102009-06-19 16:03:18 -0700466 }
Jason Samsfb03a222009-10-15 16:47:31 -0700467 gThreadTLSKeyCount++;
468 pthread_mutex_unlock(&gInitMutex);
469
470 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700471
Jason Samsa658e902009-06-04 14:35:01 -0700472 status = pthread_attr_init(&threadAttr);
473 if (status) {
474 LOGE("Failed to init thread attribute.");
475 return;
476 }
477
Jason Sams613cad12009-11-12 15:10:25 -0800478 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700479
Jason Sams24371d92009-08-19 12:17:14 -0700480 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700481 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700482
Jason Sams7c7c78a2010-07-26 17:12:55 -0700483 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
484 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
485 if (cpu < 2) cpu = 0;
486
487 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700488 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
489 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
490 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
491 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700492 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700493 if (status) {
494 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700495 return;
496 }
Jason Sams18133402010-07-20 15:09:00 -0700497 while(!mRunning) {
498 usleep(100);
499 }
500
Jason Sams8d957fa2010-09-28 14:41:22 -0700501 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700502 mWorkers.mRunningCount = 0;
503 mWorkers.mLaunchCount = 0;
504 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
505 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
506 if (status) {
507 mWorkers.mCount = ct;
508 LOGE("Created fewer than expected number of RS threads.");
509 break;
510 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700511 }
512
Jason Sams326e0dd2009-05-22 14:03:28 -0700513
Jason Samsa658e902009-06-04 14:35:01 -0700514 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700515}
516
517Context::~Context()
518{
Jason Sams8c0ee652009-08-25 14:49:07 -0700519 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700520 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700521 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700522 void *res;
523
Jason Sams8c0ee652009-08-25 14:49:07 -0700524 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700525 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700526
Jason Samsfb03a222009-10-15 16:47:31 -0700527 // Global structure cleanup.
528 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700529 if (mDev) {
530 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700531 --gThreadTLSKeyCount;
532 if (!gThreadTLSKeyCount) {
533 pthread_key_delete(gThreadTLSKey);
534 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800535 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700536 }
Jason Samsfb03a222009-10-15 16:47:31 -0700537 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700538}
539
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700540void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800541{
Jason Sams4820e8b2010-02-09 16:05:07 -0800542 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800543
Jason Sams458f2dc2009-11-03 13:58:36 -0800544 EGLBoolean ret;
545 if (mEGL.mSurface != NULL) {
546 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
547 checkEglError("eglMakeCurrent", ret);
548
549 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
550 checkEglError("eglDestroySurface", ret);
551
552 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800553 mWidth = 0;
554 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800555 }
556
557 mWndSurface = sur;
558 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700559 mWidth = w;
560 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800561 bool first = false;
562 if (!mEGL.mContext) {
563 first = true;
564 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800565 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800566 pthread_mutex_unlock(&gInitMutex);
567 }
568
Jason Sams458f2dc2009-11-03 13:58:36 -0800569 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
570 checkEglError("eglCreateWindowSurface");
571 if (mEGL.mSurface == EGL_NO_SURFACE) {
572 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
573 }
574
575 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
576 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800577
Jason Sams771565f2010-05-14 15:30:29 -0700578 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800579
Jason Sams613cad12009-11-12 15:10:25 -0800580 if (first) {
581 mGL.mVersion = glGetString(GL_VERSION);
582 mGL.mVendor = glGetString(GL_VENDOR);
583 mGL.mRenderer = glGetString(GL_RENDERER);
584 mGL.mExtensions = glGetString(GL_EXTENSIONS);
585
586 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
587 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800588 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800589 LOGV("GL Renderer %s", mGL.mRenderer);
590 //LOGV("GL Extensions %s", mGL.mExtensions);
591
Jason Samsc460e552009-11-25 13:22:07 -0800592 const char *verptr = NULL;
593 if (strlen((const char *)mGL.mVersion) > 9) {
594 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
595 verptr = (const char *)mGL.mVersion + 12;
596 }
597 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
598 verptr = (const char *)mGL.mVersion + 9;
599 }
600 }
601
602 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800603 LOGE("Error, OpenGL ES Lite not supported");
604 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800605 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800606 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800607
608 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
609 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
610 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
611
612 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
613 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
614
615 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
616 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800617
618 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700619 mGL.EXT_texture_max_aniso = 1.0f;
620 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
621 if(hasAniso) {
622 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
623 }
Jason Sams613cad12009-11-12 15:10:25 -0800624 }
625
Jason Sams458f2dc2009-11-03 13:58:36 -0800626 }
627}
628
Jason Sams86f1b232009-09-24 17:38:20 -0700629void Context::pause()
630{
Jason Sams4820e8b2010-02-09 16:05:07 -0800631 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700632 mPaused = true;
633}
634
635void Context::resume()
636{
Jason Sams4820e8b2010-02-09 16:05:07 -0800637 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700638 mPaused = false;
639}
640
Jason Sams326e0dd2009-05-22 14:03:28 -0700641void Context::setRootScript(Script *s)
642{
Jason Sams4820e8b2010-02-09 16:05:07 -0800643 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700644 mRootScript.set(s);
645}
646
Jason Samsccc010b2010-05-13 18:30:11 -0700647void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700648{
Jason Sams4820e8b2010-02-09 16:05:07 -0800649 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700650 if (pfs == NULL) {
651 mFragmentStore.set(mStateFragmentStore.mDefault);
652 } else {
653 mFragmentStore.set(pfs);
654 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700655}
656
657void Context::setFragment(ProgramFragment *pf)
658{
Jason Sams4820e8b2010-02-09 16:05:07 -0800659 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700660 if (pf == NULL) {
661 mFragment.set(mStateFragment.mDefault);
662 } else {
663 mFragment.set(pf);
664 }
Jason Samscfb1d112009-08-05 13:57:03 -0700665}
666
Jason Sams5fd09d82009-09-23 13:57:02 -0700667void Context::setRaster(ProgramRaster *pr)
668{
Jason Sams4820e8b2010-02-09 16:05:07 -0800669 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700670 if (pr == NULL) {
671 mRaster.set(mStateRaster.mDefault);
672 } else {
673 mRaster.set(pr);
674 }
675}
676
Jason Sams326e0dd2009-05-22 14:03:28 -0700677void Context::setVertex(ProgramVertex *pv)
678{
Jason Sams4820e8b2010-02-09 16:05:07 -0800679 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700680 if (pv == NULL) {
681 mVertex.set(mStateVertex.mDefault);
682 } else {
683 mVertex.set(pv);
684 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700685}
686
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700687void Context::setFont(Font *f)
688{
689 rsAssert(mIsGraphicsContext);
690 if (f == NULL) {
691 mFont.set(mStateFont.mDefault);
692 } else {
693 mFont.set(f);
694 }
695}
696
Jason Samsa4a54e42009-06-10 18:39:40 -0700697void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700698{
699 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700700 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700701 mNames.add(obj);
702}
703
704void Context::removeName(ObjectBase *obj)
705{
706 for(size_t ct=0; ct < mNames.size(); ct++) {
707 if (obj == mNames[ct]) {
708 mNames.removeAt(ct);
709 return;
710 }
711 }
712}
713
Jason Sams8c401ef2009-10-06 13:58:47 -0700714uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
715{
716 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700717 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700718 if (!wait) {
719 if (mIO.mToClient.isEmpty()) {
720 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700721 return 0;
722 }
723 }
724
725 //LOGE("getMessageToClient 2 con=%p", this);
726 uint32_t bytesData = 0;
727 uint32_t commandID = 0;
728 const void *d = mIO.mToClient.get(&commandID, &bytesData);
729 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
730
731 *receiveLen = bytesData;
732 if (bufferLen >= bytesData) {
733 memcpy(data, d, bytesData);
734 mIO.mToClient.next();
735 return commandID;
736 }
737 return 0;
738}
739
740bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
741{
742 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
743 if (cmdID == 0) {
744 LOGE("Attempting to send invalid command 0 to client.");
745 return false;
746 }
747 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700748 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700749 // Not enough room, and not waiting.
750 return false;
751 }
752 }
753 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700754 if (len > 0) {
755 void *p = mIO.mToClient.reserve(len);
756 memcpy(p, data, len);
757 mIO.mToClient.commit(cmdID, len);
758 } else {
759 mIO.mToClient.commit(cmdID, 0);
760 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700761 //LOGE("sendMessageToClient 3");
762 return true;
763}
764
765void Context::initToClient()
766{
767 while(!mRunning) {
768 usleep(100);
769 }
770}
771
772void Context::deinitToClient()
773{
774 mIO.mToClient.shutdown();
775}
Jason Sams50869382009-08-18 17:07:09 -0700776
Jason Samsa2cf7552010-03-03 13:03:18 -0800777const char * Context::getError(RsError *err)
778{
779 *err = mError;
780 mError = RS_ERROR_NONE;
781 if (*err != RS_ERROR_NONE) {
782 return mErrorMsg;
783 }
784 return NULL;
785}
786
787void Context::setError(RsError e, const char *msg)
788{
789 mError = e;
790 mErrorMsg = msg;
791}
792
793
Jason Sams13e26342009-11-24 12:26:35 -0800794void Context::dumpDebug() const
795{
796 LOGE("RS Context debug %p", this);
797 LOGE("RS Context debug");
798
799 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700800 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800801 LOGE(" GL vendor: %s", mGL.mVendor);
802 LOGE(" GL renderer: %s", mGL.mRenderer);
803 LOGE(" GL Version: %s", mGL.mVersion);
804 LOGE(" GL Extensions: %s", mGL.mExtensions);
805 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
806 LOGE(" RS width %i, height %i", mWidth, mHeight);
807 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
808 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
809
Jason Sams4815c0d2009-12-15 12:58:36 -0800810 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
811 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
812 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
813 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800814}
Jason Samsa4a54e42009-06-10 18:39:40 -0700815
Jason Sams326e0dd2009-05-22 14:03:28 -0700816///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700817//
Jason Sams326e0dd2009-05-22 14:03:28 -0700818
819namespace android {
820namespace renderscript {
821
Jason Sams8c880902010-06-15 12:15:57 -0700822void rsi_ContextFinish(Context *rsc)
823{
824}
Jason Sams326e0dd2009-05-22 14:03:28 -0700825
826void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
827{
828 Script *s = static_cast<Script *>(vs);
829 rsc->setRootScript(s);
830}
831
832void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
833{
834 Sampler *s = static_cast<Sampler *>(vs);
835
836 if (slot > RS_MAX_SAMPLER_SLOT) {
837 LOGE("Invalid sampler slot");
838 return;
839 }
840
841 s->bindToContext(&rsc->mStateSampler, slot);
842}
843
Jason Samsccc010b2010-05-13 18:30:11 -0700844void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700845{
Jason Samsccc010b2010-05-13 18:30:11 -0700846 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700847 rsc->setFragmentStore(pfs);
848}
849
850void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
851{
852 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
853 rsc->setFragment(pf);
854}
855
Jason Sams5fd09d82009-09-23 13:57:02 -0700856void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
857{
858 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
859 rsc->setRaster(pr);
860}
861
Jason Sams326e0dd2009-05-22 14:03:28 -0700862void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
863{
864 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
865 rsc->setVertex(pv);
866}
867
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700868void rsi_ContextBindFont(Context *rsc, RsFont vfont)
869{
870 Font *font = static_cast<Font *>(vfont);
871 rsc->setFont(font);
872}
873
Jason Samsa4a54e42009-06-10 18:39:40 -0700874void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700875{
876 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700877 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700878}
Jason Sams326e0dd2009-05-22 14:03:28 -0700879
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700880void rsi_GetName(Context *rsc, void * obj, const char **name)
881{
882 ObjectBase *ob = static_cast<ObjectBase *>(obj);
883 (*name) = ob->getName();
884}
885
Jason Sams707aaf32009-08-18 14:14:24 -0700886void rsi_ObjDestroy(Context *rsc, void *obj)
887{
888 ObjectBase *ob = static_cast<ObjectBase *>(obj);
889 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700890 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700891}
892
Jason Sams86f1b232009-09-24 17:38:20 -0700893void rsi_ContextPause(Context *rsc)
894{
895 rsc->pause();
896}
897
898void rsi_ContextResume(Context *rsc)
899{
900 rsc->resume();
901}
902
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700903void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800904{
Mathias Agopianfa402862010-02-12 14:04:35 -0800905 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800906}
907
Jason Sams15832442009-11-15 12:14:26 -0800908void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800909{
Jason Sams15832442009-11-15 12:14:26 -0800910 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800911}
912
Jason Samsc21cf402009-11-17 17:26:46 -0800913void rsi_ContextDump(Context *rsc, int32_t bits)
914{
915 ObjectBase::dumpAll(rsc);
916}
917
Jason Samsa2cf7552010-03-03 13:03:18 -0800918const char * rsi_ContextGetError(Context *rsc, RsError *e)
919{
920 const char *msg = rsc->getError(e);
921 if (*e != RS_ERROR_NONE) {
922 LOGE("RS Error %i %s", *e, msg);
923 }
924 return msg;
925}
926
Jason Sams326e0dd2009-05-22 14:03:28 -0700927}
928}
929
930
Jason Sams4820e8b2010-02-09 16:05:07 -0800931RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700932{
Jason Sams4820e8b2010-02-09 16:05:07 -0800933 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700934 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800935 Context *rsc = new Context(dev, false, false);
936 return rsc;
937}
938
939RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
940{
941 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
942 Device * dev = static_cast<Device *>(vdev);
943 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700944 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-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 Sams8c401ef2009-10-06 13:58:47 -0700954uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
955{
956 Context * rsc = static_cast<Context *>(vrsc);
957 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
958}
959
960void rsContextInitToClient(RsContext vrsc)
961{
962 Context * rsc = static_cast<Context *>(vrsc);
963 rsc->initToClient();
964}
965
966void rsContextDeinitToClient(RsContext vrsc)
967{
968 Context * rsc = static_cast<Context *>(vrsc);
969 rsc->deinitToClient();
970}
971