blob: 30add62dbfb5241d2ef96dddecaafa35507f8773 [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;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700191 mAverageFPSFrameCount = 0;
192 mAverageFPSStartTime = mTimeLast;
193 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700194 timerReset();
195}
196
Jason Sams1d54f102009-09-03 15:43:13 -0700197void Context::timerFrame()
198{
199 mTimeLastFrame = mTimeFrame;
200 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700201 // Update average fps
202 const uint64_t averageFramerateInterval = 1000 * 1000000;
203 mAverageFPSFrameCount ++;
204 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
205 if(inverval >= averageFramerateInterval) {
206 inverval = inverval / 1000000;
207 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
208 mAverageFPSFrameCount = 0;
209 mAverageFPSStartTime = mTimeFrame;
210 }
Jason Sams1d54f102009-09-03 15:43:13 -0700211}
212
Jason Sams24371d92009-08-19 12:17:14 -0700213void Context::timerSet(Timers tm)
214{
215 uint64_t last = mTimeLast;
216 mTimeLast = getTime();
217 mTimers[mTimerActive] += mTimeLast - last;
218 mTimerActive = tm;
219}
220
221void Context::timerPrint()
222{
223 double total = 0;
224 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
225 total += mTimers[ct];
226 }
Jason Sams1d54f102009-09-03 15:43:13 -0700227 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800228 mTimeMSLastFrame = frame / 1000000;
229 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
230 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700231
Jason Sams2dca84d2009-12-09 11:05:45 -0800232
233 if (props.mLogTimes) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700234 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800235 mTimeMSLastFrame,
236 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
237 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
238 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700239 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
240 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800241 }
Jason Sams24371d92009-08-19 12:17:14 -0700242}
243
Jason Samsa2cf7552010-03-03 13:03:18 -0800244bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700245{
Jason Sams900f1612010-09-16 18:18:29 -0700246 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
247 LOGE("Context::setupCheck() 1 fail");
248 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800249 }
Jason Sams900f1612010-09-16 18:18:29 -0700250
251 mFragmentStore->setupGL2(this, &mStateFragmentStore);
252 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
253 mRaster->setupGL2(this, &mStateRaster);
254 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800255 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700256}
257
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700258void Context::setupProgramStore() {
259 mFragmentStore->setupGL2(this, &mStateFragmentStore);
260}
261
Jason Sams1fddd902009-09-25 15:25:00 -0700262static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700263{
264 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700265 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700266 return 0 != strcmp(buf, "0");
267}
Jason Sams326e0dd2009-05-22 14:03:28 -0700268
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700269void Context::displayDebugStats()
270{
271 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700272 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700273 float oldR, oldG, oldB, oldA;
274 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700275 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700276
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700277 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700278 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700279 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700280
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700281 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700282 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700283
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700284 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700285}
286
Jason Sams326e0dd2009-05-22 14:03:28 -0700287void * Context::threadProc(void *vrsc)
288{
289 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800290 rsc->mNativeThreadId = gettid();
291
292 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800293 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700294
Jason Sams1fddd902009-09-25 15:25:00 -0700295 rsc->props.mLogTimes = getProp("debug.rs.profile");
296 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800297 rsc->props.mLogObjects = getProp("debug.rs.object");
298 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700299 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
300 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700301 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700302
Jason Sams605048a2010-09-30 18:15:52 -0700303 rsc->mTlsStruct = new ScriptTLSStruct;
304 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700305 LOGE("Error allocating tls storage");
306 return NULL;
307 }
Jason Sams605048a2010-09-30 18:15:52 -0700308 rsc->mTlsStruct->mContext = rsc;
309 rsc->mTlsStruct->mScript = NULL;
310 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700311 if (status) {
312 LOGE("pthread_setspecific %i", status);
313 }
314
Stephen Hines01b7d292010-09-28 15:45:45 -0700315 rsc->mScriptC.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800316 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700317 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800318 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700319 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800320 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700321 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800322 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700323 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800324 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700325 rsc->mStateFont.init(rsc);
326 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800327 rsc->mStateVertexArray.init(rsc);
328 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700329
Jason Sams326e0dd2009-05-22 14:03:28 -0700330 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700331 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700332 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700333 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700334 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800335 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700336
Jason Sams2dca84d2009-12-09 11:05:45 -0800337 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800338 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800339 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700340
341 if(rsc->props.mLogVisual) {
342 rsc->displayDebugStats();
343 }
344
Jason Sams2dca84d2009-12-09 11:05:45 -0800345 mDraw = targetTime && !rsc->mPaused;
346 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700347 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800348 rsc->timerFrame();
349 rsc->timerSet(RS_TIMER_INTERNAL);
350 rsc->timerPrint();
351 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700352 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800353 if (rsc->mThreadPriority > 0 && targetTime) {
354 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
355 if (t > 0) {
356 usleep(t);
357 }
358 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700359 }
360
Jason Sams4c5f99e2010-09-14 14:59:03 -0700361 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800362 if (rsc->mIsGraphicsContext) {
363 rsc->mRaster.clear();
364 rsc->mFragment.clear();
365 rsc->mVertex.clear();
366 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700367 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800368 rsc->mRootScript.clear();
369 rsc->mStateRaster.deinit(rsc);
370 rsc->mStateVertex.deinit(rsc);
371 rsc->mStateFragment.deinit(rsc);
372 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700373 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800374 }
Jason Samse514b452009-09-25 14:51:22 -0700375 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700376
Jason Sams4820e8b2010-02-09 16:05:07 -0800377 if (rsc->mIsGraphicsContext) {
378 pthread_mutex_lock(&gInitMutex);
379 rsc->deinitEGL();
380 pthread_mutex_unlock(&gInitMutex);
381 }
Jason Sams605048a2010-09-30 18:15:52 -0700382 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700383
Jason Sams4c5f99e2010-09-14 14:59:03 -0700384 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700385 return NULL;
386}
387
Jason Sams7bf29dd2010-07-19 15:38:19 -0700388void * Context::helperThreadProc(void *vrsc)
389{
390 Context *rsc = static_cast<Context *>(vrsc);
391 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
392
Jason Sams18133402010-07-20 15:09:00 -0700393 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700394
395 rsc->mWorkers.mLaunchSignals[idx].init();
396 rsc->mWorkers.mNativeThreadId[idx] = gettid();
397
Jason Sams8d957fa2010-09-28 14:41:22 -0700398#if 0
399 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
400 cpu_set_t cpuset;
401 memset(&cpuset, 0, sizeof(cpuset));
402 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
403 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700404 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700405 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
406#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700407
408 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700409 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
410 if (status) {
411 LOGE("pthread_setspecific %i", status);
412 }
413
Jason Sams7bf29dd2010-07-19 15:38:19 -0700414 while(rsc->mRunning) {
415 rsc->mWorkers.mLaunchSignals[idx].wait();
416 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700417 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
418 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700419 android_atomic_dec(&rsc->mWorkers.mRunningCount);
420 rsc->mWorkers.mCompleteSignal.set();
421 }
Jason Sams18133402010-07-20 15:09:00 -0700422
423 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700424 return NULL;
425}
426
427void Context::launchThreads(WorkerCallback_t cbk, void *data)
428{
429 mWorkers.mLaunchData = data;
430 mWorkers.mLaunchCallback = cbk;
431 mWorkers.mRunningCount = (int)mWorkers.mCount;
432 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
433 mWorkers.mLaunchSignals[ct].set();
434 }
435 while(mWorkers.mRunningCount) {
436 mWorkers.mCompleteSignal.wait();
437 }
438}
439
Jason Sams15832442009-11-15 12:14:26 -0800440void Context::setPriority(int32_t p)
441{
442 // Note: If we put this in the proper "background" policy
443 // the wallpapers can become completly unresponsive at times.
444 // This is probably not what we want for something the user is actively
445 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800446 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800447#if 0
448 SchedPolicy pol = SP_FOREGROUND;
449 if (p > 0) {
450 pol = SP_BACKGROUND;
451 }
452 if (!set_sched_policy(mNativeThreadId, pol)) {
453 // success; reset the priority as well
454 }
455#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700456 setpriority(PRIO_PROCESS, mNativeThreadId, p);
457 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
458 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
459 }
Jason Sams15832442009-11-15 12:14:26 -0800460#endif
461}
462
Jason Sams4820e8b2010-02-09 16:05:07 -0800463Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700464{
Jason Samsfb03a222009-10-15 16:47:31 -0700465 pthread_mutex_lock(&gInitMutex);
466
Jason Sams326e0dd2009-05-22 14:03:28 -0700467 dev->addContext(this);
468 mDev = dev;
469 mRunning = false;
470 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700471 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700472 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700473 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800474 mError = RS_ERROR_NONE;
475 mErrorMsg = NULL;
476
Jason Sams613cad12009-11-12 15:10:25 -0800477 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800478 memset(&mGL, 0, sizeof(mGL));
479 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700480
Jason Samsa658e902009-06-04 14:35:01 -0700481 int status;
482 pthread_attr_t threadAttr;
483
Jason Samsfb03a222009-10-15 16:47:31 -0700484 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700485 status = pthread_key_create(&gThreadTLSKey, NULL);
486 if (status) {
487 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700488 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700489 return;
490 }
Jason Samse5769102009-06-19 16:03:18 -0700491 }
Jason Samsfb03a222009-10-15 16:47:31 -0700492 gThreadTLSKeyCount++;
493 pthread_mutex_unlock(&gInitMutex);
494
495 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700496
Jason Samsa658e902009-06-04 14:35:01 -0700497 status = pthread_attr_init(&threadAttr);
498 if (status) {
499 LOGE("Failed to init thread attribute.");
500 return;
501 }
502
Jason Sams613cad12009-11-12 15:10:25 -0800503 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700504
Jason Sams24371d92009-08-19 12:17:14 -0700505 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700506 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700507
Jason Sams7c7c78a2010-07-26 17:12:55 -0700508 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
509 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
510 if (cpu < 2) cpu = 0;
511
512 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700513 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
514 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
515 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
516 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700517 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700518 if (status) {
519 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700520 return;
521 }
Jason Sams18133402010-07-20 15:09:00 -0700522 while(!mRunning) {
523 usleep(100);
524 }
525
Jason Sams8d957fa2010-09-28 14:41:22 -0700526 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700527 mWorkers.mRunningCount = 0;
528 mWorkers.mLaunchCount = 0;
529 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
530 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
531 if (status) {
532 mWorkers.mCount = ct;
533 LOGE("Created fewer than expected number of RS threads.");
534 break;
535 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700536 }
537
Jason Sams326e0dd2009-05-22 14:03:28 -0700538
Jason Samsa658e902009-06-04 14:35:01 -0700539 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700540}
541
542Context::~Context()
543{
Jason Sams8c0ee652009-08-25 14:49:07 -0700544 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700545 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700546 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700547 void *res;
548
Jason Sams8c0ee652009-08-25 14:49:07 -0700549 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700550 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700551
Jason Samsfb03a222009-10-15 16:47:31 -0700552 // Global structure cleanup.
553 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700554 if (mDev) {
555 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700556 --gThreadTLSKeyCount;
557 if (!gThreadTLSKeyCount) {
558 pthread_key_delete(gThreadTLSKey);
559 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800560 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700561 }
Jason Samsfb03a222009-10-15 16:47:31 -0700562 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700563}
564
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700565void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800566{
Jason Sams4820e8b2010-02-09 16:05:07 -0800567 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800568
Jason Sams458f2dc2009-11-03 13:58:36 -0800569 EGLBoolean ret;
570 if (mEGL.mSurface != NULL) {
571 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
572 checkEglError("eglMakeCurrent", ret);
573
574 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
575 checkEglError("eglDestroySurface", ret);
576
577 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800578 mWidth = 0;
579 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800580 }
581
582 mWndSurface = sur;
583 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700584 mWidth = w;
585 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800586 bool first = false;
587 if (!mEGL.mContext) {
588 first = true;
589 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800590 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800591 pthread_mutex_unlock(&gInitMutex);
592 }
593
Jason Sams458f2dc2009-11-03 13:58:36 -0800594 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
595 checkEglError("eglCreateWindowSurface");
596 if (mEGL.mSurface == EGL_NO_SURFACE) {
597 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
598 }
599
600 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
601 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800602
Jason Sams771565f2010-05-14 15:30:29 -0700603 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800604
Jason Sams613cad12009-11-12 15:10:25 -0800605 if (first) {
606 mGL.mVersion = glGetString(GL_VERSION);
607 mGL.mVendor = glGetString(GL_VENDOR);
608 mGL.mRenderer = glGetString(GL_RENDERER);
609 mGL.mExtensions = glGetString(GL_EXTENSIONS);
610
611 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
612 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800613 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800614 LOGV("GL Renderer %s", mGL.mRenderer);
615 //LOGV("GL Extensions %s", mGL.mExtensions);
616
Jason Samsc460e552009-11-25 13:22:07 -0800617 const char *verptr = NULL;
618 if (strlen((const char *)mGL.mVersion) > 9) {
619 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
620 verptr = (const char *)mGL.mVersion + 12;
621 }
622 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
623 verptr = (const char *)mGL.mVersion + 9;
624 }
625 }
626
627 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800628 LOGE("Error, OpenGL ES Lite not supported");
629 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800630 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800631 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800632
633 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
634 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
635 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
636
637 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
638 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
639
640 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
641 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800642
643 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700644 mGL.EXT_texture_max_aniso = 1.0f;
645 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
646 if(hasAniso) {
647 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
648 }
Jason Sams613cad12009-11-12 15:10:25 -0800649 }
650
Jason Sams458f2dc2009-11-03 13:58:36 -0800651 }
652}
653
Jason Sams86f1b232009-09-24 17:38:20 -0700654void Context::pause()
655{
Jason Sams4820e8b2010-02-09 16:05:07 -0800656 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700657 mPaused = true;
658}
659
660void Context::resume()
661{
Jason Sams4820e8b2010-02-09 16:05:07 -0800662 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700663 mPaused = false;
664}
665
Jason Sams326e0dd2009-05-22 14:03:28 -0700666void Context::setRootScript(Script *s)
667{
Jason Sams4820e8b2010-02-09 16:05:07 -0800668 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700669 mRootScript.set(s);
670}
671
Jason Samsccc010b2010-05-13 18:30:11 -0700672void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700673{
Jason Sams4820e8b2010-02-09 16:05:07 -0800674 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700675 if (pfs == NULL) {
676 mFragmentStore.set(mStateFragmentStore.mDefault);
677 } else {
678 mFragmentStore.set(pfs);
679 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700680}
681
682void Context::setFragment(ProgramFragment *pf)
683{
Jason Sams4820e8b2010-02-09 16:05:07 -0800684 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700685 if (pf == NULL) {
686 mFragment.set(mStateFragment.mDefault);
687 } else {
688 mFragment.set(pf);
689 }
Jason Samscfb1d112009-08-05 13:57:03 -0700690}
691
Jason Sams5fd09d82009-09-23 13:57:02 -0700692void Context::setRaster(ProgramRaster *pr)
693{
Jason Sams4820e8b2010-02-09 16:05:07 -0800694 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700695 if (pr == NULL) {
696 mRaster.set(mStateRaster.mDefault);
697 } else {
698 mRaster.set(pr);
699 }
700}
701
Jason Sams326e0dd2009-05-22 14:03:28 -0700702void Context::setVertex(ProgramVertex *pv)
703{
Jason Sams4820e8b2010-02-09 16:05:07 -0800704 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700705 if (pv == NULL) {
706 mVertex.set(mStateVertex.mDefault);
707 } else {
708 mVertex.set(pv);
709 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700710}
711
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700712void Context::setFont(Font *f)
713{
714 rsAssert(mIsGraphicsContext);
715 if (f == NULL) {
716 mFont.set(mStateFont.mDefault);
717 } else {
718 mFont.set(f);
719 }
720}
721
Jason Samsa4a54e42009-06-10 18:39:40 -0700722void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700723{
724 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700725 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700726 mNames.add(obj);
727}
728
729void Context::removeName(ObjectBase *obj)
730{
731 for(size_t ct=0; ct < mNames.size(); ct++) {
732 if (obj == mNames[ct]) {
733 mNames.removeAt(ct);
734 return;
735 }
736 }
737}
738
Jason Sams8c401ef2009-10-06 13:58:47 -0700739uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
740{
741 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700742 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700743 if (!wait) {
744 if (mIO.mToClient.isEmpty()) {
745 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700746 return 0;
747 }
748 }
749
750 //LOGE("getMessageToClient 2 con=%p", this);
751 uint32_t bytesData = 0;
752 uint32_t commandID = 0;
753 const void *d = mIO.mToClient.get(&commandID, &bytesData);
754 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
755
756 *receiveLen = bytesData;
757 if (bufferLen >= bytesData) {
758 memcpy(data, d, bytesData);
759 mIO.mToClient.next();
760 return commandID;
761 }
762 return 0;
763}
764
765bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
766{
767 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
768 if (cmdID == 0) {
769 LOGE("Attempting to send invalid command 0 to client.");
770 return false;
771 }
772 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700773 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700774 // Not enough room, and not waiting.
775 return false;
776 }
777 }
778 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700779 if (len > 0) {
780 void *p = mIO.mToClient.reserve(len);
781 memcpy(p, data, len);
782 mIO.mToClient.commit(cmdID, len);
783 } else {
784 mIO.mToClient.commit(cmdID, 0);
785 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700786 //LOGE("sendMessageToClient 3");
787 return true;
788}
789
790void Context::initToClient()
791{
792 while(!mRunning) {
793 usleep(100);
794 }
795}
796
797void Context::deinitToClient()
798{
799 mIO.mToClient.shutdown();
800}
Jason Sams50869382009-08-18 17:07:09 -0700801
Jason Samsa2cf7552010-03-03 13:03:18 -0800802const char * Context::getError(RsError *err)
803{
804 *err = mError;
805 mError = RS_ERROR_NONE;
806 if (*err != RS_ERROR_NONE) {
807 return mErrorMsg;
808 }
809 return NULL;
810}
811
812void Context::setError(RsError e, const char *msg)
813{
814 mError = e;
815 mErrorMsg = msg;
816}
817
818
Jason Sams13e26342009-11-24 12:26:35 -0800819void Context::dumpDebug() const
820{
821 LOGE("RS Context debug %p", this);
822 LOGE("RS Context debug");
823
824 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700825 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800826 LOGE(" GL vendor: %s", mGL.mVendor);
827 LOGE(" GL renderer: %s", mGL.mRenderer);
828 LOGE(" GL Version: %s", mGL.mVersion);
829 LOGE(" GL Extensions: %s", mGL.mExtensions);
830 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
831 LOGE(" RS width %i, height %i", mWidth, mHeight);
832 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
833 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
834
Jason Sams4815c0d2009-12-15 12:58:36 -0800835 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
836 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
837 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
838 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800839}
Jason Samsa4a54e42009-06-10 18:39:40 -0700840
Jason Sams326e0dd2009-05-22 14:03:28 -0700841///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700842//
Jason Sams326e0dd2009-05-22 14:03:28 -0700843
844namespace android {
845namespace renderscript {
846
Jason Sams8c880902010-06-15 12:15:57 -0700847void rsi_ContextFinish(Context *rsc)
848{
849}
Jason Sams326e0dd2009-05-22 14:03:28 -0700850
851void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
852{
853 Script *s = static_cast<Script *>(vs);
854 rsc->setRootScript(s);
855}
856
857void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
858{
859 Sampler *s = static_cast<Sampler *>(vs);
860
861 if (slot > RS_MAX_SAMPLER_SLOT) {
862 LOGE("Invalid sampler slot");
863 return;
864 }
865
866 s->bindToContext(&rsc->mStateSampler, slot);
867}
868
Jason Samsccc010b2010-05-13 18:30:11 -0700869void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700870{
Jason Samsccc010b2010-05-13 18:30:11 -0700871 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700872 rsc->setFragmentStore(pfs);
873}
874
875void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
876{
877 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
878 rsc->setFragment(pf);
879}
880
Jason Sams5fd09d82009-09-23 13:57:02 -0700881void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
882{
883 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
884 rsc->setRaster(pr);
885}
886
Jason Sams326e0dd2009-05-22 14:03:28 -0700887void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
888{
889 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
890 rsc->setVertex(pv);
891}
892
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700893void rsi_ContextBindFont(Context *rsc, RsFont vfont)
894{
895 Font *font = static_cast<Font *>(vfont);
896 rsc->setFont(font);
897}
898
Jason Samsa4a54e42009-06-10 18:39:40 -0700899void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700900{
901 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700902 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700903}
Jason Sams326e0dd2009-05-22 14:03:28 -0700904
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700905void rsi_GetName(Context *rsc, void * obj, const char **name)
906{
907 ObjectBase *ob = static_cast<ObjectBase *>(obj);
908 (*name) = ob->getName();
909}
910
Jason Sams707aaf32009-08-18 14:14:24 -0700911void rsi_ObjDestroy(Context *rsc, void *obj)
912{
913 ObjectBase *ob = static_cast<ObjectBase *>(obj);
914 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700915 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700916}
917
Jason Sams86f1b232009-09-24 17:38:20 -0700918void rsi_ContextPause(Context *rsc)
919{
920 rsc->pause();
921}
922
923void rsi_ContextResume(Context *rsc)
924{
925 rsc->resume();
926}
927
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700928void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800929{
Mathias Agopianfa402862010-02-12 14:04:35 -0800930 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800931}
932
Jason Sams15832442009-11-15 12:14:26 -0800933void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800934{
Jason Sams15832442009-11-15 12:14:26 -0800935 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800936}
937
Jason Samsc21cf402009-11-17 17:26:46 -0800938void rsi_ContextDump(Context *rsc, int32_t bits)
939{
940 ObjectBase::dumpAll(rsc);
941}
942
Jason Samsa2cf7552010-03-03 13:03:18 -0800943const char * rsi_ContextGetError(Context *rsc, RsError *e)
944{
945 const char *msg = rsc->getError(e);
946 if (*e != RS_ERROR_NONE) {
947 LOGE("RS Error %i %s", *e, msg);
948 }
949 return msg;
950}
951
Jason Sams326e0dd2009-05-22 14:03:28 -0700952}
953}
954
955
Jason Sams4820e8b2010-02-09 16:05:07 -0800956RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700957{
Jason Sams4820e8b2010-02-09 16:05:07 -0800958 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700959 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800960 Context *rsc = new Context(dev, false, false);
961 return rsc;
962}
963
964RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
965{
966 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
967 Device * dev = static_cast<Device *>(vdev);
968 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700969 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700970 return rsc;
971}
972
973void rsContextDestroy(RsContext vrsc)
974{
975 Context * rsc = static_cast<Context *>(vrsc);
976 delete rsc;
977}
978
Jason Sams8c401ef2009-10-06 13:58:47 -0700979uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
980{
981 Context * rsc = static_cast<Context *>(vrsc);
982 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
983}
984
985void rsContextInitToClient(RsContext vrsc)
986{
987 Context * rsc = static_cast<Context *>(vrsc);
988 rsc->initToClient();
989}
990
991void rsContextDeinitToClient(RsContext vrsc)
992{
993 Context * rsc = static_cast<Context *>(vrsc);
994 rsc->deinitToClient();
995}
996