blob: a7f380f94f52f9d6d7784caec25b7b69b192f358 [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 Sakhartchouk0cae59f2010-08-03 12:03:16 -0700281 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700282
Jason Samse5769102009-06-19 16:03:18 -0700283 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
284 if (!tlsStruct) {
285 LOGE("Error allocating tls storage");
286 return NULL;
287 }
288 tlsStruct->mContext = rsc;
289 tlsStruct->mScript = NULL;
290 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
291 if (status) {
292 LOGE("pthread_setspecific %i", status);
293 }
294
Jason Sams4820e8b2010-02-09 16:05:07 -0800295 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700296 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800297 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700298 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800299 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700300 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800301 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700302 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800303 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700304 rsc->mStateFont.init(rsc);
305 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800306 rsc->mStateVertexArray.init(rsc);
307 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700308
Jason Sams326e0dd2009-05-22 14:03:28 -0700309 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700310 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700311 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700312 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700313 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800314 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700315
Jason Sams2dca84d2009-12-09 11:05:45 -0800316 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800317 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800318 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700319
320 if(rsc->props.mLogVisual) {
321 rsc->displayDebugStats();
322 }
323
Jason Sams2dca84d2009-12-09 11:05:45 -0800324 mDraw = targetTime && !rsc->mPaused;
325 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700326 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 rsc->timerFrame();
328 rsc->timerSet(RS_TIMER_INTERNAL);
329 rsc->timerPrint();
330 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700331 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800332 if (rsc->mThreadPriority > 0 && targetTime) {
333 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
334 if (t > 0) {
335 usleep(t);
336 }
337 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700338 }
339
Jason Sams4c5f99e2010-09-14 14:59:03 -0700340 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800341 if (rsc->mIsGraphicsContext) {
342 rsc->mRaster.clear();
343 rsc->mFragment.clear();
344 rsc->mVertex.clear();
345 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700346 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800347 rsc->mRootScript.clear();
348 rsc->mStateRaster.deinit(rsc);
349 rsc->mStateVertex.deinit(rsc);
350 rsc->mStateFragment.deinit(rsc);
351 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700352 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800353 }
Jason Samse514b452009-09-25 14:51:22 -0700354 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700355
Jason Sams4820e8b2010-02-09 16:05:07 -0800356 if (rsc->mIsGraphicsContext) {
357 pthread_mutex_lock(&gInitMutex);
358 rsc->deinitEGL();
359 pthread_mutex_unlock(&gInitMutex);
360 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700361
Jason Sams4c5f99e2010-09-14 14:59:03 -0700362 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700363 return NULL;
364}
365
Jason Sams7bf29dd2010-07-19 15:38:19 -0700366void * Context::helperThreadProc(void *vrsc)
367{
368 Context *rsc = static_cast<Context *>(vrsc);
369 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
370
Jason Sams18133402010-07-20 15:09:00 -0700371 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700372
373 rsc->mWorkers.mLaunchSignals[idx].init();
374 rsc->mWorkers.mNativeThreadId[idx] = gettid();
375
Jason Sams8d957fa2010-09-28 14:41:22 -0700376#if 0
377 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
378 cpu_set_t cpuset;
379 memset(&cpuset, 0, sizeof(cpuset));
380 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
381 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
382 sizeof(cpuset), &cpuset);
383 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
384#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700385
386 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
387 while(rsc->mRunning) {
388 rsc->mWorkers.mLaunchSignals[idx].wait();
389 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700390 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
391 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700392 android_atomic_dec(&rsc->mWorkers.mRunningCount);
393 rsc->mWorkers.mCompleteSignal.set();
394 }
Jason Sams18133402010-07-20 15:09:00 -0700395
396 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700397 return NULL;
398}
399
400void Context::launchThreads(WorkerCallback_t cbk, void *data)
401{
402 mWorkers.mLaunchData = data;
403 mWorkers.mLaunchCallback = cbk;
404 mWorkers.mRunningCount = (int)mWorkers.mCount;
405 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
406 mWorkers.mLaunchSignals[ct].set();
407 }
408 while(mWorkers.mRunningCount) {
409 mWorkers.mCompleteSignal.wait();
410 }
411}
412
Jason Sams15832442009-11-15 12:14:26 -0800413void Context::setPriority(int32_t p)
414{
415 // Note: If we put this in the proper "background" policy
416 // the wallpapers can become completly unresponsive at times.
417 // This is probably not what we want for something the user is actively
418 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800419 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800420#if 0
421 SchedPolicy pol = SP_FOREGROUND;
422 if (p > 0) {
423 pol = SP_BACKGROUND;
424 }
425 if (!set_sched_policy(mNativeThreadId, pol)) {
426 // success; reset the priority as well
427 }
428#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700429 setpriority(PRIO_PROCESS, mNativeThreadId, p);
430 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
431 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
432 }
Jason Sams15832442009-11-15 12:14:26 -0800433#endif
434}
435
Jason Sams4820e8b2010-02-09 16:05:07 -0800436Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700437{
Jason Samsfb03a222009-10-15 16:47:31 -0700438 pthread_mutex_lock(&gInitMutex);
439
Jason Sams326e0dd2009-05-22 14:03:28 -0700440 dev->addContext(this);
441 mDev = dev;
442 mRunning = false;
443 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700444 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700445 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700446 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800447 mError = RS_ERROR_NONE;
448 mErrorMsg = NULL;
449
Jason Sams613cad12009-11-12 15:10:25 -0800450 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800451 memset(&mGL, 0, sizeof(mGL));
452 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700453
Jason Samsa658e902009-06-04 14:35:01 -0700454 int status;
455 pthread_attr_t threadAttr;
456
Jason Samsfb03a222009-10-15 16:47:31 -0700457 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700458 status = pthread_key_create(&gThreadTLSKey, NULL);
459 if (status) {
460 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700461 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700462 return;
463 }
Jason Samse5769102009-06-19 16:03:18 -0700464 }
Jason Samsfb03a222009-10-15 16:47:31 -0700465 gThreadTLSKeyCount++;
466 pthread_mutex_unlock(&gInitMutex);
467
468 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700469
Jason Samsa658e902009-06-04 14:35:01 -0700470 status = pthread_attr_init(&threadAttr);
471 if (status) {
472 LOGE("Failed to init thread attribute.");
473 return;
474 }
475
Jason Sams613cad12009-11-12 15:10:25 -0800476 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700477
Jason Sams24371d92009-08-19 12:17:14 -0700478 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700479 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700480
Jason Sams7c7c78a2010-07-26 17:12:55 -0700481 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
482 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
483 if (cpu < 2) cpu = 0;
484
485 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700486 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
487 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
488 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
489 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700490 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700491 if (status) {
492 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700493 return;
494 }
Jason Sams18133402010-07-20 15:09:00 -0700495 while(!mRunning) {
496 usleep(100);
497 }
498
Jason Sams8d957fa2010-09-28 14:41:22 -0700499 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700500 mWorkers.mRunningCount = 0;
501 mWorkers.mLaunchCount = 0;
502 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
503 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
504 if (status) {
505 mWorkers.mCount = ct;
506 LOGE("Created fewer than expected number of RS threads.");
507 break;
508 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700509 }
510
Jason Sams326e0dd2009-05-22 14:03:28 -0700511
Jason Samsa658e902009-06-04 14:35:01 -0700512 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700513}
514
515Context::~Context()
516{
Jason Sams8c0ee652009-08-25 14:49:07 -0700517 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700518 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700519 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700520 void *res;
521
Jason Sams8c0ee652009-08-25 14:49:07 -0700522 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700523 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700524
Jason Samsfb03a222009-10-15 16:47:31 -0700525 // Global structure cleanup.
526 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700527 if (mDev) {
528 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700529 --gThreadTLSKeyCount;
530 if (!gThreadTLSKeyCount) {
531 pthread_key_delete(gThreadTLSKey);
532 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800533 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700534 }
Jason Samsfb03a222009-10-15 16:47:31 -0700535 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700536}
537
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700538void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800539{
Jason Sams4820e8b2010-02-09 16:05:07 -0800540 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800541
Jason Sams458f2dc2009-11-03 13:58:36 -0800542 EGLBoolean ret;
543 if (mEGL.mSurface != NULL) {
544 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
545 checkEglError("eglMakeCurrent", ret);
546
547 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
548 checkEglError("eglDestroySurface", ret);
549
550 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800551 mWidth = 0;
552 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800553 }
554
555 mWndSurface = sur;
556 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700557 mWidth = w;
558 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800559 bool first = false;
560 if (!mEGL.mContext) {
561 first = true;
562 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800563 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800564 pthread_mutex_unlock(&gInitMutex);
565 }
566
Jason Sams458f2dc2009-11-03 13:58:36 -0800567 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
568 checkEglError("eglCreateWindowSurface");
569 if (mEGL.mSurface == EGL_NO_SURFACE) {
570 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
571 }
572
573 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
574 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800575
Jason Sams771565f2010-05-14 15:30:29 -0700576 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800577
Jason Sams613cad12009-11-12 15:10:25 -0800578 if (first) {
579 mGL.mVersion = glGetString(GL_VERSION);
580 mGL.mVendor = glGetString(GL_VENDOR);
581 mGL.mRenderer = glGetString(GL_RENDERER);
582 mGL.mExtensions = glGetString(GL_EXTENSIONS);
583
584 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
585 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800586 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800587 LOGV("GL Renderer %s", mGL.mRenderer);
588 //LOGV("GL Extensions %s", mGL.mExtensions);
589
Jason Samsc460e552009-11-25 13:22:07 -0800590 const char *verptr = NULL;
591 if (strlen((const char *)mGL.mVersion) > 9) {
592 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
593 verptr = (const char *)mGL.mVersion + 12;
594 }
595 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
596 verptr = (const char *)mGL.mVersion + 9;
597 }
598 }
599
600 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800601 LOGE("Error, OpenGL ES Lite not supported");
602 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800603 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800604 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800605
606 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
607 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
608 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
609
610 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
611 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
612
613 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
614 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800615
616 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800617 }
618
Jason Sams458f2dc2009-11-03 13:58:36 -0800619 }
620}
621
Jason Sams86f1b232009-09-24 17:38:20 -0700622void Context::pause()
623{
Jason Sams4820e8b2010-02-09 16:05:07 -0800624 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700625 mPaused = true;
626}
627
628void Context::resume()
629{
Jason Sams4820e8b2010-02-09 16:05:07 -0800630 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700631 mPaused = false;
632}
633
Jason Sams326e0dd2009-05-22 14:03:28 -0700634void Context::setRootScript(Script *s)
635{
Jason Sams4820e8b2010-02-09 16:05:07 -0800636 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700637 mRootScript.set(s);
638}
639
Jason Samsccc010b2010-05-13 18:30:11 -0700640void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700641{
Jason Sams4820e8b2010-02-09 16:05:07 -0800642 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700643 if (pfs == NULL) {
644 mFragmentStore.set(mStateFragmentStore.mDefault);
645 } else {
646 mFragmentStore.set(pfs);
647 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700648}
649
650void Context::setFragment(ProgramFragment *pf)
651{
Jason Sams4820e8b2010-02-09 16:05:07 -0800652 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700653 if (pf == NULL) {
654 mFragment.set(mStateFragment.mDefault);
655 } else {
656 mFragment.set(pf);
657 }
Jason Samscfb1d112009-08-05 13:57:03 -0700658}
659
Jason Sams5fd09d82009-09-23 13:57:02 -0700660void Context::setRaster(ProgramRaster *pr)
661{
Jason Sams4820e8b2010-02-09 16:05:07 -0800662 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700663 if (pr == NULL) {
664 mRaster.set(mStateRaster.mDefault);
665 } else {
666 mRaster.set(pr);
667 }
668}
669
Jason Sams326e0dd2009-05-22 14:03:28 -0700670void Context::setVertex(ProgramVertex *pv)
671{
Jason Sams4820e8b2010-02-09 16:05:07 -0800672 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700673 if (pv == NULL) {
674 mVertex.set(mStateVertex.mDefault);
675 } else {
676 mVertex.set(pv);
677 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700678}
679
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700680void Context::setFont(Font *f)
681{
682 rsAssert(mIsGraphicsContext);
683 if (f == NULL) {
684 mFont.set(mStateFont.mDefault);
685 } else {
686 mFont.set(f);
687 }
688}
689
Jason Samsa4a54e42009-06-10 18:39:40 -0700690void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700691{
692 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700693 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700694 mNames.add(obj);
695}
696
697void Context::removeName(ObjectBase *obj)
698{
699 for(size_t ct=0; ct < mNames.size(); ct++) {
700 if (obj == mNames[ct]) {
701 mNames.removeAt(ct);
702 return;
703 }
704 }
705}
706
Jason Sams8c401ef2009-10-06 13:58:47 -0700707uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
708{
709 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700710 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700711 if (!wait) {
712 if (mIO.mToClient.isEmpty()) {
713 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700714 return 0;
715 }
716 }
717
718 //LOGE("getMessageToClient 2 con=%p", this);
719 uint32_t bytesData = 0;
720 uint32_t commandID = 0;
721 const void *d = mIO.mToClient.get(&commandID, &bytesData);
722 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
723
724 *receiveLen = bytesData;
725 if (bufferLen >= bytesData) {
726 memcpy(data, d, bytesData);
727 mIO.mToClient.next();
728 return commandID;
729 }
730 return 0;
731}
732
733bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
734{
735 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
736 if (cmdID == 0) {
737 LOGE("Attempting to send invalid command 0 to client.");
738 return false;
739 }
740 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700741 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700742 // Not enough room, and not waiting.
743 return false;
744 }
745 }
746 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700747 if (len > 0) {
748 void *p = mIO.mToClient.reserve(len);
749 memcpy(p, data, len);
750 mIO.mToClient.commit(cmdID, len);
751 } else {
752 mIO.mToClient.commit(cmdID, 0);
753 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700754 //LOGE("sendMessageToClient 3");
755 return true;
756}
757
758void Context::initToClient()
759{
760 while(!mRunning) {
761 usleep(100);
762 }
763}
764
765void Context::deinitToClient()
766{
767 mIO.mToClient.shutdown();
768}
Jason Sams50869382009-08-18 17:07:09 -0700769
Jason Samsa2cf7552010-03-03 13:03:18 -0800770const char * Context::getError(RsError *err)
771{
772 *err = mError;
773 mError = RS_ERROR_NONE;
774 if (*err != RS_ERROR_NONE) {
775 return mErrorMsg;
776 }
777 return NULL;
778}
779
780void Context::setError(RsError e, const char *msg)
781{
782 mError = e;
783 mErrorMsg = msg;
784}
785
786
Jason Sams13e26342009-11-24 12:26:35 -0800787void Context::dumpDebug() const
788{
789 LOGE("RS Context debug %p", this);
790 LOGE("RS Context debug");
791
792 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700793 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800794 LOGE(" GL vendor: %s", mGL.mVendor);
795 LOGE(" GL renderer: %s", mGL.mRenderer);
796 LOGE(" GL Version: %s", mGL.mVersion);
797 LOGE(" GL Extensions: %s", mGL.mExtensions);
798 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
799 LOGE(" RS width %i, height %i", mWidth, mHeight);
800 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
801 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
802
Jason Sams4815c0d2009-12-15 12:58:36 -0800803 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
804 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
805 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
806 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800807}
Jason Samsa4a54e42009-06-10 18:39:40 -0700808
Jason Sams326e0dd2009-05-22 14:03:28 -0700809///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700810//
Jason Sams326e0dd2009-05-22 14:03:28 -0700811
812namespace android {
813namespace renderscript {
814
Jason Sams8c880902010-06-15 12:15:57 -0700815void rsi_ContextFinish(Context *rsc)
816{
817}
Jason Sams326e0dd2009-05-22 14:03:28 -0700818
819void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
820{
821 Script *s = static_cast<Script *>(vs);
822 rsc->setRootScript(s);
823}
824
825void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
826{
827 Sampler *s = static_cast<Sampler *>(vs);
828
829 if (slot > RS_MAX_SAMPLER_SLOT) {
830 LOGE("Invalid sampler slot");
831 return;
832 }
833
834 s->bindToContext(&rsc->mStateSampler, slot);
835}
836
Jason Samsccc010b2010-05-13 18:30:11 -0700837void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700838{
Jason Samsccc010b2010-05-13 18:30:11 -0700839 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700840 rsc->setFragmentStore(pfs);
841}
842
843void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
844{
845 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
846 rsc->setFragment(pf);
847}
848
Jason Sams5fd09d82009-09-23 13:57:02 -0700849void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
850{
851 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
852 rsc->setRaster(pr);
853}
854
Jason Sams326e0dd2009-05-22 14:03:28 -0700855void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
856{
857 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
858 rsc->setVertex(pv);
859}
860
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700861void rsi_ContextBindFont(Context *rsc, RsFont vfont)
862{
863 Font *font = static_cast<Font *>(vfont);
864 rsc->setFont(font);
865}
866
Jason Samsa4a54e42009-06-10 18:39:40 -0700867void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700868{
869 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700870 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700871}
Jason Sams326e0dd2009-05-22 14:03:28 -0700872
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700873void rsi_GetName(Context *rsc, void * obj, const char **name)
874{
875 ObjectBase *ob = static_cast<ObjectBase *>(obj);
876 (*name) = ob->getName();
877}
878
Jason Sams707aaf32009-08-18 14:14:24 -0700879void rsi_ObjDestroy(Context *rsc, void *obj)
880{
881 ObjectBase *ob = static_cast<ObjectBase *>(obj);
882 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700883 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700884}
885
Jason Sams86f1b232009-09-24 17:38:20 -0700886void rsi_ContextPause(Context *rsc)
887{
888 rsc->pause();
889}
890
891void rsi_ContextResume(Context *rsc)
892{
893 rsc->resume();
894}
895
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700896void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800897{
Mathias Agopianfa402862010-02-12 14:04:35 -0800898 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800899}
900
Jason Sams15832442009-11-15 12:14:26 -0800901void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800902{
Jason Sams15832442009-11-15 12:14:26 -0800903 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800904}
905
Jason Samsc21cf402009-11-17 17:26:46 -0800906void rsi_ContextDump(Context *rsc, int32_t bits)
907{
908 ObjectBase::dumpAll(rsc);
909}
910
Jason Samsa2cf7552010-03-03 13:03:18 -0800911const char * rsi_ContextGetError(Context *rsc, RsError *e)
912{
913 const char *msg = rsc->getError(e);
914 if (*e != RS_ERROR_NONE) {
915 LOGE("RS Error %i %s", *e, msg);
916 }
917 return msg;
918}
919
Jason Sams326e0dd2009-05-22 14:03:28 -0700920}
921}
922
923
Jason Sams4820e8b2010-02-09 16:05:07 -0800924RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700925{
Jason Sams4820e8b2010-02-09 16:05:07 -0800926 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700927 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800928 Context *rsc = new Context(dev, false, false);
929 return rsc;
930}
931
932RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
933{
934 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
935 Device * dev = static_cast<Device *>(vdev);
936 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700937 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700938 return rsc;
939}
940
941void rsContextDestroy(RsContext vrsc)
942{
943 Context * rsc = static_cast<Context *>(vrsc);
944 delete rsc;
945}
946
Jason Sams8c401ef2009-10-06 13:58:47 -0700947uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
948{
949 Context * rsc = static_cast<Context *>(vrsc);
950 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
951}
952
953void rsContextInitToClient(RsContext vrsc)
954{
955 Context * rsc = static_cast<Context *>(vrsc);
956 rsc->initToClient();
957}
958
959void rsContextDeinitToClient(RsContext vrsc)
960{
961 Context * rsc = static_cast<Context *>(vrsc);
962 rsc->deinitToClient();
963}
964