blob: 3dbdbfb511b2c8dbc9726ab830635e955b17ff12 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Samsafcb25c2009-08-25 11:34:49 -070021#include <ui/EGLUtils.h>
Mathias Agopian9b97c292010-02-12 12:00:38 -080022#include <ui/egl/android_natives.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070023
Jason Sams15832442009-11-15 12:14:26 -080024#include <sys/types.h>
25#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070026#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080027
Joe Onorato76371ff2009-09-23 16:37:36 -070028#include <cutils/properties.h>
29
Jason Sams1aa5a4e2009-06-22 17:15:15 -070030#include <GLES/gl.h>
31#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070034
Jason Sams15832442009-11-15 12:14:26 -080035#include <cutils/sched_policy.h>
36
Jason Sams326e0dd2009-05-22 14:03:28 -070037using namespace android;
38using namespace android::renderscript;
39
Jason Samse5769102009-06-19 16:03:18 -070040pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070041uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070042uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070043pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070044
Jason Sams33b6e3b2009-10-27 14:44:31 -070045static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
46 if (returnVal != EGL_TRUE) {
47 fprintf(stderr, "%s() returned %d\n", op, returnVal);
48 }
49
50 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
51 = eglGetError()) {
52 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
53 error);
54 }
55}
56
Jason Samsc460e552009-11-25 13:22:07 -080057void Context::initEGL(bool useGL2)
Jason Sams326e0dd2009-05-22 14:03:28 -070058{
Jason Samsafcb25c2009-08-25 11:34:49 -070059 mEGL.mNumConfigs = -1;
60 EGLint configAttribs[128];
61 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080062 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -070063
Jason Samsafcb25c2009-08-25 11:34:49 -070064 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsafcb25c2009-08-25 11:34:49 -070066 configAttribsPtr[0] = EGL_SURFACE_TYPE;
67 configAttribsPtr[1] = EGL_WINDOW_BIT;
68 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -070069
Jason Samsc460e552009-11-25 13:22:07 -080070 if (useGL2) {
71 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
72 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
73 configAttribsPtr += 2;
74 }
75
Jason Samsafcb25c2009-08-25 11:34:49 -070076 if (mUseDepth) {
77 configAttribsPtr[0] = EGL_DEPTH_SIZE;
78 configAttribsPtr[1] = 16;
79 configAttribsPtr += 2;
80 }
Jason Sams9397e302009-08-27 20:23:34 -070081
Jason Sams5fd09d82009-09-23 13:57:02 -070082 if (mDev->mForceSW) {
83 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
84 configAttribsPtr[1] = EGL_SLOW_CONFIG;
85 configAttribsPtr += 2;
86 }
87
Jason Samsafcb25c2009-08-25 11:34:49 -070088 configAttribsPtr[0] = EGL_NONE;
89 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -070090
Jason Sams4c5f99e2010-09-14 14:59:03 -070091 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -070092 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -070093 checkEglError("eglGetDisplay");
94
Jason Samsafcb25c2009-08-25 11:34:49 -070095 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -070096 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -070097
98 status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
99 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700100 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700101 }
102 //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
103
Jason Samsafcb25c2009-08-25 11:34:49 -0700104
Jason Samsc460e552009-11-25 13:22:07 -0800105 if (useGL2) {
106 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
107 } else {
108 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
109 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700110 checkEglError("eglCreateContext");
111 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700112 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700113 }
114 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams33b6e3b2009-10-27 14:44:31 -0700117void Context::deinitEGL()
118{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700119 LOGV("%p, deinitEGL", this);
Jason Sams613cad12009-11-12 15:10:25 -0800120 setSurface(0, 0, NULL);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700121 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
122 checkEglError("eglDestroyContext");
123
124 gGLContextCount--;
125 if (!gGLContextCount) {
126 eglTerminate(mEGL.mDisplay);
127 }
128}
129
130
Jason Samsc61346b2010-05-28 18:23:22 -0700131uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700132{
133 ObjectBaseRef<ProgramFragment> frag(mFragment);
134 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700135 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700136 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700137 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700138
Jason Samsc61346b2010-05-28 18:23:22 -0700139 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700140
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700141 mFragment.set(frag);
142 mVertex.set(vtx);
143 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700144 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700145 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700146 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700147}
148
Jason Samsd01d9702009-12-23 14:35:29 -0800149void Context::checkError(const char *msg) const
150{
151 GLenum err = glGetError();
152 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700153 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800154 }
155}
Jason Sams10308932009-06-09 12:15:30 -0700156
Jason Sams2dca84d2009-12-09 11:05:45 -0800157uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700158{
Jason Sams771565f2010-05-14 15:30:29 -0700159 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700160
Jason Sams2dca84d2009-12-09 11:05:45 -0800161 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700162 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700163 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700164
Jason Samsd01d9702009-12-23 14:35:29 -0800165 checkError("runRootScript");
Jason Samsa2cf7552010-03-03 13:03:18 -0800166 if (mError != RS_ERROR_NONE) {
167 // If we have an error condition we stop rendering until
168 // somthing changes that might fix it.
169 ret = 0;
170 }
Jason Samscfb1d112009-08-05 13:57:03 -0700171 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700172}
173
Jason Sams24371d92009-08-19 12:17:14 -0700174uint64_t Context::getTime() const
175{
176 struct timespec t;
177 clock_gettime(CLOCK_MONOTONIC, &t);
178 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
179}
180
181void Context::timerReset()
182{
183 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
184 mTimers[ct] = 0;
185 }
186}
187
188void Context::timerInit()
189{
190 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700191 mTimeFrame = mTimeLast;
192 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700193 mTimerActive = RS_TIMER_INTERNAL;
194 timerReset();
195}
196
Jason Sams1d54f102009-09-03 15:43:13 -0700197void Context::timerFrame()
198{
199 mTimeLastFrame = mTimeFrame;
200 mTimeFrame = getTime();
201}
202
Jason Sams24371d92009-08-19 12:17:14 -0700203void Context::timerSet(Timers tm)
204{
205 uint64_t last = mTimeLast;
206 mTimeLast = getTime();
207 mTimers[mTimerActive] += mTimeLast - last;
208 mTimerActive = tm;
209}
210
211void Context::timerPrint()
212{
213 double total = 0;
214 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
215 total += mTimers[ct];
216 }
Jason Sams1d54f102009-09-03 15:43:13 -0700217 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800218 mTimeMSLastFrame = frame / 1000000;
219 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
220 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700221
Jason Sams2dca84d2009-12-09 11:05:45 -0800222
223 if (props.mLogTimes) {
224 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
225 mTimeMSLastFrame,
226 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
227 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
228 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
229 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
230 }
Jason Sams24371d92009-08-19 12:17:14 -0700231}
232
Jason Samsa2cf7552010-03-03 13:03:18 -0800233bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700234{
Jason Sams900f1612010-09-16 18:18:29 -0700235 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
236 LOGE("Context::setupCheck() 1 fail");
237 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800238 }
Jason Sams900f1612010-09-16 18:18:29 -0700239
240 mFragmentStore->setupGL2(this, &mStateFragmentStore);
241 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
242 mRaster->setupGL2(this, &mStateRaster);
243 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800244 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700245}
246
Jason Sams1fddd902009-09-25 15:25:00 -0700247static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700248{
249 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700250 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700251 return 0 != strcmp(buf, "0");
252}
Jason Sams326e0dd2009-05-22 14:03:28 -0700253
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700254void Context::displayDebugStats()
255{
256 char buffer[128];
257 sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700258 float oldR, oldG, oldB, oldA;
259 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700260
261 float shadowCol = 0.2f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700262 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700263 mStateFont.renderText(buffer, 5, getHeight() - 5);
264
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700265 float textCol = 0.9f;
266 mStateFont.setFontColor(textCol, textCol, textCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700267 mStateFont.renderText(buffer, 4, getHeight() - 6);
268
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700269 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700270}
271
Jason Sams326e0dd2009-05-22 14:03:28 -0700272void * Context::threadProc(void *vrsc)
273{
274 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800275 rsc->mNativeThreadId = gettid();
276
277 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800278 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700279
Jason Sams1fddd902009-09-25 15:25:00 -0700280 rsc->props.mLogTimes = getProp("debug.rs.profile");
281 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800282 rsc->props.mLogObjects = getProp("debug.rs.object");
283 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700284 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700285
Jason Samse5769102009-06-19 16:03:18 -0700286 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
287 if (!tlsStruct) {
288 LOGE("Error allocating tls storage");
289 return NULL;
290 }
291 tlsStruct->mContext = rsc;
292 tlsStruct->mScript = NULL;
293 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
294 if (status) {
295 LOGE("pthread_setspecific %i", status);
296 }
297
Jason Sams4820e8b2010-02-09 16:05:07 -0800298 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700299 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800300 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700301 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800302 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700303 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800304 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700305 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800306 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700307 rsc->mStateFont.init(rsc);
308 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800309 rsc->mStateVertexArray.init(rsc);
310 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700311
Jason Sams326e0dd2009-05-22 14:03:28 -0700312 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700313 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700314 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700315 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700316 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800317 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700318
Jason Sams2dca84d2009-12-09 11:05:45 -0800319 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800320 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800321 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700322
323 if(rsc->props.mLogVisual) {
324 rsc->displayDebugStats();
325 }
326
Jason Sams2dca84d2009-12-09 11:05:45 -0800327 mDraw = targetTime && !rsc->mPaused;
328 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700329 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800330 rsc->timerFrame();
331 rsc->timerSet(RS_TIMER_INTERNAL);
332 rsc->timerPrint();
333 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700334 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800335 if (rsc->mThreadPriority > 0 && targetTime) {
336 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
337 if (t > 0) {
338 usleep(t);
339 }
340 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700341 }
342
Jason Sams4c5f99e2010-09-14 14:59:03 -0700343 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800344 if (rsc->mIsGraphicsContext) {
345 rsc->mRaster.clear();
346 rsc->mFragment.clear();
347 rsc->mVertex.clear();
348 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700349 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800350 rsc->mRootScript.clear();
351 rsc->mStateRaster.deinit(rsc);
352 rsc->mStateVertex.deinit(rsc);
353 rsc->mStateFragment.deinit(rsc);
354 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700355 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800356 }
Jason Samse514b452009-09-25 14:51:22 -0700357 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700358
Jason Sams4820e8b2010-02-09 16:05:07 -0800359 if (rsc->mIsGraphicsContext) {
360 pthread_mutex_lock(&gInitMutex);
361 rsc->deinitEGL();
362 pthread_mutex_unlock(&gInitMutex);
363 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700364
Jason Sams4c5f99e2010-09-14 14:59:03 -0700365 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700366 return NULL;
367}
368
Jason Sams7bf29dd2010-07-19 15:38:19 -0700369void * Context::helperThreadProc(void *vrsc)
370{
371 Context *rsc = static_cast<Context *>(vrsc);
372 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
373
Jason Sams18133402010-07-20 15:09:00 -0700374 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700375
376 rsc->mWorkers.mLaunchSignals[idx].init();
377 rsc->mWorkers.mNativeThreadId[idx] = gettid();
378
379 //cpu_set_t cpset[16];
380 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
381 //LOGE("ret = %i", ret);
382
383//sched_setaffinity
384
385 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
386 while(rsc->mRunning) {
387 rsc->mWorkers.mLaunchSignals[idx].wait();
388 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700389 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
390 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700391 android_atomic_dec(&rsc->mWorkers.mRunningCount);
392 rsc->mWorkers.mCompleteSignal.set();
393 }
Jason Sams18133402010-07-20 15:09:00 -0700394
395 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700396 return NULL;
397}
398
399void Context::launchThreads(WorkerCallback_t cbk, void *data)
400{
401 mWorkers.mLaunchData = data;
402 mWorkers.mLaunchCallback = cbk;
403 mWorkers.mRunningCount = (int)mWorkers.mCount;
404 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
405 mWorkers.mLaunchSignals[ct].set();
406 }
407 while(mWorkers.mRunningCount) {
408 mWorkers.mCompleteSignal.wait();
409 }
410}
411
Jason Sams15832442009-11-15 12:14:26 -0800412void Context::setPriority(int32_t p)
413{
414 // Note: If we put this in the proper "background" policy
415 // the wallpapers can become completly unresponsive at times.
416 // This is probably not what we want for something the user is actively
417 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800418 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800419#if 0
420 SchedPolicy pol = SP_FOREGROUND;
421 if (p > 0) {
422 pol = SP_BACKGROUND;
423 }
424 if (!set_sched_policy(mNativeThreadId, pol)) {
425 // success; reset the priority as well
426 }
427#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700428 setpriority(PRIO_PROCESS, mNativeThreadId, p);
429 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
430 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
431 }
Jason Sams15832442009-11-15 12:14:26 -0800432#endif
433}
434
Jason Sams4820e8b2010-02-09 16:05:07 -0800435Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700436{
Jason Samsfb03a222009-10-15 16:47:31 -0700437 pthread_mutex_lock(&gInitMutex);
438
Jason Sams326e0dd2009-05-22 14:03:28 -0700439 dev->addContext(this);
440 mDev = dev;
441 mRunning = false;
442 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700443 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700444 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700445 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800446 mError = RS_ERROR_NONE;
447 mErrorMsg = NULL;
448
Jason Sams613cad12009-11-12 15:10:25 -0800449 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800450 memset(&mGL, 0, sizeof(mGL));
451 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700452
Jason Samsa658e902009-06-04 14:35:01 -0700453 int status;
454 pthread_attr_t threadAttr;
455
Jason Samsfb03a222009-10-15 16:47:31 -0700456 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700457 status = pthread_key_create(&gThreadTLSKey, NULL);
458 if (status) {
459 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700460 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700461 return;
462 }
Jason Samse5769102009-06-19 16:03:18 -0700463 }
Jason Samsfb03a222009-10-15 16:47:31 -0700464 gThreadTLSKeyCount++;
465 pthread_mutex_unlock(&gInitMutex);
466
467 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700468
Jason Samsa658e902009-06-04 14:35:01 -0700469 status = pthread_attr_init(&threadAttr);
470 if (status) {
471 LOGE("Failed to init thread attribute.");
472 return;
473 }
474
Jason Sams613cad12009-11-12 15:10:25 -0800475 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700476
Jason Sams24371d92009-08-19 12:17:14 -0700477 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700478 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700479
Jason Sams7c7c78a2010-07-26 17:12:55 -0700480 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
481 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
482 if (cpu < 2) cpu = 0;
483
484 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700485 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
486 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
487 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
488 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700489 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700490 if (status) {
491 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700492 return;
493 }
Jason Sams18133402010-07-20 15:09:00 -0700494 while(!mRunning) {
495 usleep(100);
496 }
497
Jason Sams7bf29dd2010-07-19 15:38:19 -0700498 mWorkers.mRunningCount = 0;
499 mWorkers.mLaunchCount = 0;
500 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
501 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
502 if (status) {
503 mWorkers.mCount = ct;
504 LOGE("Created fewer than expected number of RS threads.");
505 break;
506 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700507 }
508
Jason Sams326e0dd2009-05-22 14:03:28 -0700509
Jason Samsa658e902009-06-04 14:35:01 -0700510 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700511}
512
513Context::~Context()
514{
Jason Sams8c0ee652009-08-25 14:49:07 -0700515 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700516 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700517 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700518 void *res;
519
Jason Sams8c0ee652009-08-25 14:49:07 -0700520 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700521 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700522
Jason Samsfb03a222009-10-15 16:47:31 -0700523 // Global structure cleanup.
524 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700525 if (mDev) {
526 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700527 --gThreadTLSKeyCount;
528 if (!gThreadTLSKeyCount) {
529 pthread_key_delete(gThreadTLSKey);
530 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800531 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700532 }
Jason Samsfb03a222009-10-15 16:47:31 -0700533 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700534}
535
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700536void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800537{
Jason Sams4820e8b2010-02-09 16:05:07 -0800538 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800539
Jason Sams458f2dc2009-11-03 13:58:36 -0800540 EGLBoolean ret;
541 if (mEGL.mSurface != NULL) {
542 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
543 checkEglError("eglMakeCurrent", ret);
544
545 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
546 checkEglError("eglDestroySurface", ret);
547
548 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800549 mWidth = 0;
550 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800551 }
552
553 mWndSurface = sur;
554 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700555 mWidth = w;
556 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800557 bool first = false;
558 if (!mEGL.mContext) {
559 first = true;
560 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800561 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800562 pthread_mutex_unlock(&gInitMutex);
563 }
564
Jason Sams458f2dc2009-11-03 13:58:36 -0800565 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
566 checkEglError("eglCreateWindowSurface");
567 if (mEGL.mSurface == EGL_NO_SURFACE) {
568 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
569 }
570
571 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
572 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800573
Jason Sams771565f2010-05-14 15:30:29 -0700574 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800575
Jason Sams613cad12009-11-12 15:10:25 -0800576 if (first) {
577 mGL.mVersion = glGetString(GL_VERSION);
578 mGL.mVendor = glGetString(GL_VENDOR);
579 mGL.mRenderer = glGetString(GL_RENDERER);
580 mGL.mExtensions = glGetString(GL_EXTENSIONS);
581
582 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
583 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800584 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800585 LOGV("GL Renderer %s", mGL.mRenderer);
586 //LOGV("GL Extensions %s", mGL.mExtensions);
587
Jason Samsc460e552009-11-25 13:22:07 -0800588 const char *verptr = NULL;
589 if (strlen((const char *)mGL.mVersion) > 9) {
590 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
591 verptr = (const char *)mGL.mVersion + 12;
592 }
593 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
594 verptr = (const char *)mGL.mVersion + 9;
595 }
596 }
597
598 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800599 LOGE("Error, OpenGL ES Lite not supported");
600 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800601 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800602 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800603
604 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
605 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
606 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
607
608 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
609 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
610
611 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
612 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800613
614 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800615 }
616
Jason Sams458f2dc2009-11-03 13:58:36 -0800617 }
618}
619
Jason Sams86f1b232009-09-24 17:38:20 -0700620void Context::pause()
621{
Jason Sams4820e8b2010-02-09 16:05:07 -0800622 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700623 mPaused = true;
624}
625
626void Context::resume()
627{
Jason Sams4820e8b2010-02-09 16:05:07 -0800628 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700629 mPaused = false;
630}
631
Jason Sams326e0dd2009-05-22 14:03:28 -0700632void Context::setRootScript(Script *s)
633{
Jason Sams4820e8b2010-02-09 16:05:07 -0800634 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700635 mRootScript.set(s);
636}
637
Jason Samsccc010b2010-05-13 18:30:11 -0700638void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700639{
Jason Sams4820e8b2010-02-09 16:05:07 -0800640 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700641 if (pfs == NULL) {
642 mFragmentStore.set(mStateFragmentStore.mDefault);
643 } else {
644 mFragmentStore.set(pfs);
645 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700646}
647
648void Context::setFragment(ProgramFragment *pf)
649{
Jason Sams4820e8b2010-02-09 16:05:07 -0800650 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700651 if (pf == NULL) {
652 mFragment.set(mStateFragment.mDefault);
653 } else {
654 mFragment.set(pf);
655 }
Jason Samscfb1d112009-08-05 13:57:03 -0700656}
657
Jason Sams5fd09d82009-09-23 13:57:02 -0700658void Context::setRaster(ProgramRaster *pr)
659{
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700661 if (pr == NULL) {
662 mRaster.set(mStateRaster.mDefault);
663 } else {
664 mRaster.set(pr);
665 }
666}
667
Jason Sams326e0dd2009-05-22 14:03:28 -0700668void Context::setVertex(ProgramVertex *pv)
669{
Jason Sams4820e8b2010-02-09 16:05:07 -0800670 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700671 if (pv == NULL) {
672 mVertex.set(mStateVertex.mDefault);
673 } else {
674 mVertex.set(pv);
675 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700676}
677
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700678void Context::setFont(Font *f)
679{
680 rsAssert(mIsGraphicsContext);
681 if (f == NULL) {
682 mFont.set(mStateFont.mDefault);
683 } else {
684 mFont.set(f);
685 }
686}
687
Jason Samsa4a54e42009-06-10 18:39:40 -0700688void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700689{
690 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700691 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700692 mNames.add(obj);
693}
694
695void Context::removeName(ObjectBase *obj)
696{
697 for(size_t ct=0; ct < mNames.size(); ct++) {
698 if (obj == mNames[ct]) {
699 mNames.removeAt(ct);
700 return;
701 }
702 }
703}
704
Jason Sams8c401ef2009-10-06 13:58:47 -0700705uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
706{
707 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700708 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700709 if (!wait) {
710 if (mIO.mToClient.isEmpty()) {
711 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700712 return 0;
713 }
714 }
715
716 //LOGE("getMessageToClient 2 con=%p", this);
717 uint32_t bytesData = 0;
718 uint32_t commandID = 0;
719 const void *d = mIO.mToClient.get(&commandID, &bytesData);
720 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
721
722 *receiveLen = bytesData;
723 if (bufferLen >= bytesData) {
724 memcpy(data, d, bytesData);
725 mIO.mToClient.next();
726 return commandID;
727 }
728 return 0;
729}
730
731bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
732{
733 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
734 if (cmdID == 0) {
735 LOGE("Attempting to send invalid command 0 to client.");
736 return false;
737 }
738 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700739 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700740 // Not enough room, and not waiting.
741 return false;
742 }
743 }
744 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700745 if (len > 0) {
746 void *p = mIO.mToClient.reserve(len);
747 memcpy(p, data, len);
748 mIO.mToClient.commit(cmdID, len);
749 } else {
750 mIO.mToClient.commit(cmdID, 0);
751 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700752 //LOGE("sendMessageToClient 3");
753 return true;
754}
755
756void Context::initToClient()
757{
758 while(!mRunning) {
759 usleep(100);
760 }
761}
762
763void Context::deinitToClient()
764{
765 mIO.mToClient.shutdown();
766}
Jason Sams50869382009-08-18 17:07:09 -0700767
Jason Samsa2cf7552010-03-03 13:03:18 -0800768const char * Context::getError(RsError *err)
769{
770 *err = mError;
771 mError = RS_ERROR_NONE;
772 if (*err != RS_ERROR_NONE) {
773 return mErrorMsg;
774 }
775 return NULL;
776}
777
778void Context::setError(RsError e, const char *msg)
779{
780 mError = e;
781 mErrorMsg = msg;
782}
783
784
Jason Sams13e26342009-11-24 12:26:35 -0800785void Context::dumpDebug() const
786{
787 LOGE("RS Context debug %p", this);
788 LOGE("RS Context debug");
789
790 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700791 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800792 LOGE(" GL vendor: %s", mGL.mVendor);
793 LOGE(" GL renderer: %s", mGL.mRenderer);
794 LOGE(" GL Version: %s", mGL.mVersion);
795 LOGE(" GL Extensions: %s", mGL.mExtensions);
796 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
797 LOGE(" RS width %i, height %i", mWidth, mHeight);
798 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
799 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
800
Jason Sams4815c0d2009-12-15 12:58:36 -0800801 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
802 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
803 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
804 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800805}
Jason Samsa4a54e42009-06-10 18:39:40 -0700806
Jason Sams326e0dd2009-05-22 14:03:28 -0700807///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700808//
Jason Sams326e0dd2009-05-22 14:03:28 -0700809
810namespace android {
811namespace renderscript {
812
Jason Sams8c880902010-06-15 12:15:57 -0700813void rsi_ContextFinish(Context *rsc)
814{
815}
Jason Sams326e0dd2009-05-22 14:03:28 -0700816
817void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
818{
819 Script *s = static_cast<Script *>(vs);
820 rsc->setRootScript(s);
821}
822
823void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
824{
825 Sampler *s = static_cast<Sampler *>(vs);
826
827 if (slot > RS_MAX_SAMPLER_SLOT) {
828 LOGE("Invalid sampler slot");
829 return;
830 }
831
832 s->bindToContext(&rsc->mStateSampler, slot);
833}
834
Jason Samsccc010b2010-05-13 18:30:11 -0700835void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700836{
Jason Samsccc010b2010-05-13 18:30:11 -0700837 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700838 rsc->setFragmentStore(pfs);
839}
840
841void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
842{
843 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
844 rsc->setFragment(pf);
845}
846
Jason Sams5fd09d82009-09-23 13:57:02 -0700847void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
848{
849 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
850 rsc->setRaster(pr);
851}
852
Jason Sams326e0dd2009-05-22 14:03:28 -0700853void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
854{
855 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
856 rsc->setVertex(pv);
857}
858
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700859void rsi_ContextBindFont(Context *rsc, RsFont vfont)
860{
861 Font *font = static_cast<Font *>(vfont);
862 rsc->setFont(font);
863}
864
Jason Samsa4a54e42009-06-10 18:39:40 -0700865void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700866{
867 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700868 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700869}
Jason Sams326e0dd2009-05-22 14:03:28 -0700870
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700871void rsi_GetName(Context *rsc, void * obj, const char **name)
872{
873 ObjectBase *ob = static_cast<ObjectBase *>(obj);
874 (*name) = ob->getName();
875}
876
Jason Sams707aaf32009-08-18 14:14:24 -0700877void rsi_ObjDestroy(Context *rsc, void *obj)
878{
879 ObjectBase *ob = static_cast<ObjectBase *>(obj);
880 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700881 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700882}
883
Jason Sams86f1b232009-09-24 17:38:20 -0700884void rsi_ContextPause(Context *rsc)
885{
886 rsc->pause();
887}
888
889void rsi_ContextResume(Context *rsc)
890{
891 rsc->resume();
892}
893
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700894void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800895{
Mathias Agopianfa402862010-02-12 14:04:35 -0800896 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800897}
898
Jason Sams15832442009-11-15 12:14:26 -0800899void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800900{
Jason Sams15832442009-11-15 12:14:26 -0800901 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800902}
903
Jason Samsc21cf402009-11-17 17:26:46 -0800904void rsi_ContextDump(Context *rsc, int32_t bits)
905{
906 ObjectBase::dumpAll(rsc);
907}
908
Jason Samsa2cf7552010-03-03 13:03:18 -0800909const char * rsi_ContextGetError(Context *rsc, RsError *e)
910{
911 const char *msg = rsc->getError(e);
912 if (*e != RS_ERROR_NONE) {
913 LOGE("RS Error %i %s", *e, msg);
914 }
915 return msg;
916}
917
Jason Sams326e0dd2009-05-22 14:03:28 -0700918}
919}
920
921
Jason Sams4820e8b2010-02-09 16:05:07 -0800922RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700923{
Jason Sams4820e8b2010-02-09 16:05:07 -0800924 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700925 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800926 Context *rsc = new Context(dev, false, false);
927 return rsc;
928}
929
930RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
931{
932 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
933 Device * dev = static_cast<Device *>(vdev);
934 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700935 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700936 return rsc;
937}
938
939void rsContextDestroy(RsContext vrsc)
940{
941 Context * rsc = static_cast<Context *>(vrsc);
942 delete rsc;
943}
944
Jason Sams8c401ef2009-10-06 13:58:47 -0700945uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
946{
947 Context * rsc = static_cast<Context *>(vrsc);
948 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
949}
950
951void rsContextInitToClient(RsContext vrsc)
952{
953 Context * rsc = static_cast<Context *>(vrsc);
954 rsc->initToClient();
955}
956
957void rsContextDeinitToClient(RsContext vrsc)
958{
959 Context * rsc = static_cast<Context *>(vrsc);
960 rsc->deinitToClient();
961}
962