blob: 3681bc29bacf8163f540a42e73222b2335e27959 [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 Samscfb1d112009-08-05 13:57:03 -0700166 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700167}
168
Jason Sams24371d92009-08-19 12:17:14 -0700169uint64_t Context::getTime() const
170{
171 struct timespec t;
172 clock_gettime(CLOCK_MONOTONIC, &t);
173 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
174}
175
176void Context::timerReset()
177{
178 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
179 mTimers[ct] = 0;
180 }
181}
182
183void Context::timerInit()
184{
185 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700186 mTimeFrame = mTimeLast;
187 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700188 mTimerActive = RS_TIMER_INTERNAL;
189 timerReset();
190}
191
Jason Sams1d54f102009-09-03 15:43:13 -0700192void Context::timerFrame()
193{
194 mTimeLastFrame = mTimeFrame;
195 mTimeFrame = getTime();
196}
197
Jason Sams24371d92009-08-19 12:17:14 -0700198void Context::timerSet(Timers tm)
199{
200 uint64_t last = mTimeLast;
201 mTimeLast = getTime();
202 mTimers[mTimerActive] += mTimeLast - last;
203 mTimerActive = tm;
204}
205
206void Context::timerPrint()
207{
208 double total = 0;
209 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
210 total += mTimers[ct];
211 }
Jason Sams1d54f102009-09-03 15:43:13 -0700212 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800213 mTimeMSLastFrame = frame / 1000000;
214 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
215 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700216
Jason Sams2dca84d2009-12-09 11:05:45 -0800217
218 if (props.mLogTimes) {
219 LOGV("RS: Frame (%i), Script %2.1f (%i), Clear & Swap %2.1f (%i), Idle %2.1f (%lli), Internal %2.1f (%lli)",
220 mTimeMSLastFrame,
221 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
222 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
223 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
224 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000);
225 }
Jason Sams24371d92009-08-19 12:17:14 -0700226}
227
Jason Samsa2cf7552010-03-03 13:03:18 -0800228bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700229{
Jason Sams900f1612010-09-16 18:18:29 -0700230 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
231 LOGE("Context::setupCheck() 1 fail");
232 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800233 }
Jason Sams900f1612010-09-16 18:18:29 -0700234
235 mFragmentStore->setupGL2(this, &mStateFragmentStore);
236 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
237 mRaster->setupGL2(this, &mStateRaster);
238 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800239 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700240}
241
Jason Sams1fddd902009-09-25 15:25:00 -0700242static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700243{
244 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700245 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700246 return 0 != strcmp(buf, "0");
247}
Jason Sams326e0dd2009-05-22 14:03:28 -0700248
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700249void Context::displayDebugStats()
250{
251 char buffer[128];
252 sprintf(buffer, "Frame %i ms, Script %i ms", mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700253 float oldR, oldG, oldB, oldA;
254 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700255
256 float shadowCol = 0.2f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700257 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700258 mStateFont.renderText(buffer, 5, getHeight() - 5);
259
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700260 float textCol = 0.9f;
261 mStateFont.setFontColor(textCol, textCol, textCol, 1.0f);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700262 mStateFont.renderText(buffer, 4, getHeight() - 6);
263
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700264 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700265}
266
Jason Sams326e0dd2009-05-22 14:03:28 -0700267void * Context::threadProc(void *vrsc)
268{
269 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800270 rsc->mNativeThreadId = gettid();
271
272 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800273 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700274
Jason Sams1fddd902009-09-25 15:25:00 -0700275 rsc->props.mLogTimes = getProp("debug.rs.profile");
276 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800277 rsc->props.mLogObjects = getProp("debug.rs.object");
278 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700279 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700280
Jason Samse5769102009-06-19 16:03:18 -0700281 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
282 if (!tlsStruct) {
283 LOGE("Error allocating tls storage");
284 return NULL;
285 }
286 tlsStruct->mContext = rsc;
287 tlsStruct->mScript = NULL;
288 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
289 if (status) {
290 LOGE("pthread_setspecific %i", status);
291 }
292
Jason Sams4820e8b2010-02-09 16:05:07 -0800293 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700294 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800295 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700296 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800297 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700298 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800299 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700300 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800301 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700302 rsc->mStateFont.init(rsc);
303 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800304 rsc->mStateVertexArray.init(rsc);
305 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700306
Jason Sams326e0dd2009-05-22 14:03:28 -0700307 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700308 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700309 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700310 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700311 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800312 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700313
Jason Sams2dca84d2009-12-09 11:05:45 -0800314 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800315 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800316 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700317
318 if(rsc->props.mLogVisual) {
319 rsc->displayDebugStats();
320 }
321
Jason Sams2dca84d2009-12-09 11:05:45 -0800322 mDraw = targetTime && !rsc->mPaused;
323 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700324 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800325 rsc->timerFrame();
326 rsc->timerSet(RS_TIMER_INTERNAL);
327 rsc->timerPrint();
328 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700329 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800330 if (rsc->mThreadPriority > 0 && targetTime) {
331 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
332 if (t > 0) {
333 usleep(t);
334 }
335 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700336 }
337
Jason Sams4c5f99e2010-09-14 14:59:03 -0700338 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800339 if (rsc->mIsGraphicsContext) {
340 rsc->mRaster.clear();
341 rsc->mFragment.clear();
342 rsc->mVertex.clear();
343 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700344 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800345 rsc->mRootScript.clear();
346 rsc->mStateRaster.deinit(rsc);
347 rsc->mStateVertex.deinit(rsc);
348 rsc->mStateFragment.deinit(rsc);
349 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700350 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800351 }
Jason Samse514b452009-09-25 14:51:22 -0700352 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700353
Jason Sams4820e8b2010-02-09 16:05:07 -0800354 if (rsc->mIsGraphicsContext) {
355 pthread_mutex_lock(&gInitMutex);
356 rsc->deinitEGL();
357 pthread_mutex_unlock(&gInitMutex);
358 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700359
Jason Sams4c5f99e2010-09-14 14:59:03 -0700360 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700361 return NULL;
362}
363
Jason Sams7bf29dd2010-07-19 15:38:19 -0700364void * Context::helperThreadProc(void *vrsc)
365{
366 Context *rsc = static_cast<Context *>(vrsc);
367 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
368
Jason Sams18133402010-07-20 15:09:00 -0700369 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700370
371 rsc->mWorkers.mLaunchSignals[idx].init();
372 rsc->mWorkers.mNativeThreadId[idx] = gettid();
373
374 //cpu_set_t cpset[16];
375 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
376 //LOGE("ret = %i", ret);
377
378//sched_setaffinity
379
380 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
381 while(rsc->mRunning) {
382 rsc->mWorkers.mLaunchSignals[idx].wait();
383 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700384 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
385 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700386 android_atomic_dec(&rsc->mWorkers.mRunningCount);
387 rsc->mWorkers.mCompleteSignal.set();
388 }
Jason Sams18133402010-07-20 15:09:00 -0700389
390 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700391 return NULL;
392}
393
394void Context::launchThreads(WorkerCallback_t cbk, void *data)
395{
396 mWorkers.mLaunchData = data;
397 mWorkers.mLaunchCallback = cbk;
398 mWorkers.mRunningCount = (int)mWorkers.mCount;
399 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
400 mWorkers.mLaunchSignals[ct].set();
401 }
402 while(mWorkers.mRunningCount) {
403 mWorkers.mCompleteSignal.wait();
404 }
405}
406
Jason Sams15832442009-11-15 12:14:26 -0800407void Context::setPriority(int32_t p)
408{
409 // Note: If we put this in the proper "background" policy
410 // the wallpapers can become completly unresponsive at times.
411 // This is probably not what we want for something the user is actively
412 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800413 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800414#if 0
415 SchedPolicy pol = SP_FOREGROUND;
416 if (p > 0) {
417 pol = SP_BACKGROUND;
418 }
419 if (!set_sched_policy(mNativeThreadId, pol)) {
420 // success; reset the priority as well
421 }
422#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700423 setpriority(PRIO_PROCESS, mNativeThreadId, p);
424 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
425 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
426 }
Jason Sams15832442009-11-15 12:14:26 -0800427#endif
428}
429
Jason Sams4820e8b2010-02-09 16:05:07 -0800430Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700431{
Jason Samsfb03a222009-10-15 16:47:31 -0700432 pthread_mutex_lock(&gInitMutex);
433
Jason Sams326e0dd2009-05-22 14:03:28 -0700434 dev->addContext(this);
435 mDev = dev;
436 mRunning = false;
437 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700438 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700439 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700440 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800441 mError = RS_ERROR_NONE;
442 mErrorMsg = NULL;
443
Jason Sams613cad12009-11-12 15:10:25 -0800444 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800445 memset(&mGL, 0, sizeof(mGL));
446 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700447
Jason Samsa658e902009-06-04 14:35:01 -0700448 int status;
449 pthread_attr_t threadAttr;
450
Jason Samsfb03a222009-10-15 16:47:31 -0700451 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700452 status = pthread_key_create(&gThreadTLSKey, NULL);
453 if (status) {
454 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700455 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700456 return;
457 }
Jason Samse5769102009-06-19 16:03:18 -0700458 }
Jason Samsfb03a222009-10-15 16:47:31 -0700459 gThreadTLSKeyCount++;
460 pthread_mutex_unlock(&gInitMutex);
461
462 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700463
Jason Samsa658e902009-06-04 14:35:01 -0700464 status = pthread_attr_init(&threadAttr);
465 if (status) {
466 LOGE("Failed to init thread attribute.");
467 return;
468 }
469
Jason Sams613cad12009-11-12 15:10:25 -0800470 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700471
Jason Sams24371d92009-08-19 12:17:14 -0700472 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700473 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700474
Jason Sams7c7c78a2010-07-26 17:12:55 -0700475 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
476 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
477 if (cpu < 2) cpu = 0;
478
479 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700480 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
481 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
482 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
483 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700484 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700485 if (status) {
486 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700487 return;
488 }
Jason Sams18133402010-07-20 15:09:00 -0700489 while(!mRunning) {
490 usleep(100);
491 }
492
Jason Sams7bf29dd2010-07-19 15:38:19 -0700493 mWorkers.mRunningCount = 0;
494 mWorkers.mLaunchCount = 0;
495 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
496 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
497 if (status) {
498 mWorkers.mCount = ct;
499 LOGE("Created fewer than expected number of RS threads.");
500 break;
501 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700502 }
503
Jason Sams326e0dd2009-05-22 14:03:28 -0700504
Jason Samsa658e902009-06-04 14:35:01 -0700505 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700506}
507
508Context::~Context()
509{
Jason Sams8c0ee652009-08-25 14:49:07 -0700510 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700511 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700512 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700513 void *res;
514
Jason Sams8c0ee652009-08-25 14:49:07 -0700515 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700516 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700517
Jason Samsfb03a222009-10-15 16:47:31 -0700518 // Global structure cleanup.
519 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700520 if (mDev) {
521 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700522 --gThreadTLSKeyCount;
523 if (!gThreadTLSKeyCount) {
524 pthread_key_delete(gThreadTLSKey);
525 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800526 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700527 }
Jason Samsfb03a222009-10-15 16:47:31 -0700528 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700529}
530
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700531void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800532{
Jason Sams4820e8b2010-02-09 16:05:07 -0800533 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800534
Jason Sams458f2dc2009-11-03 13:58:36 -0800535 EGLBoolean ret;
536 if (mEGL.mSurface != NULL) {
537 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
538 checkEglError("eglMakeCurrent", ret);
539
540 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
541 checkEglError("eglDestroySurface", ret);
542
543 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800544 mWidth = 0;
545 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800546 }
547
548 mWndSurface = sur;
549 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700550 mWidth = w;
551 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800552 bool first = false;
553 if (!mEGL.mContext) {
554 first = true;
555 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800556 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800557 pthread_mutex_unlock(&gInitMutex);
558 }
559
Jason Sams458f2dc2009-11-03 13:58:36 -0800560 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
561 checkEglError("eglCreateWindowSurface");
562 if (mEGL.mSurface == EGL_NO_SURFACE) {
563 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
564 }
565
566 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
567 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800568
Jason Sams771565f2010-05-14 15:30:29 -0700569 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800570
Jason Sams613cad12009-11-12 15:10:25 -0800571 if (first) {
572 mGL.mVersion = glGetString(GL_VERSION);
573 mGL.mVendor = glGetString(GL_VENDOR);
574 mGL.mRenderer = glGetString(GL_RENDERER);
575 mGL.mExtensions = glGetString(GL_EXTENSIONS);
576
577 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
578 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800579 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800580 LOGV("GL Renderer %s", mGL.mRenderer);
581 //LOGV("GL Extensions %s", mGL.mExtensions);
582
Jason Samsc460e552009-11-25 13:22:07 -0800583 const char *verptr = NULL;
584 if (strlen((const char *)mGL.mVersion) > 9) {
585 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
586 verptr = (const char *)mGL.mVersion + 12;
587 }
588 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
589 verptr = (const char *)mGL.mVersion + 9;
590 }
591 }
592
593 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800594 LOGE("Error, OpenGL ES Lite not supported");
595 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800596 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800597 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800598
599 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
600 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
601 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
602
603 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
604 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
605
606 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
607 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800608
609 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800610 }
611
Jason Sams458f2dc2009-11-03 13:58:36 -0800612 }
613}
614
Jason Sams86f1b232009-09-24 17:38:20 -0700615void Context::pause()
616{
Jason Sams4820e8b2010-02-09 16:05:07 -0800617 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700618 mPaused = true;
619}
620
621void Context::resume()
622{
Jason Sams4820e8b2010-02-09 16:05:07 -0800623 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700624 mPaused = false;
625}
626
Jason Sams326e0dd2009-05-22 14:03:28 -0700627void Context::setRootScript(Script *s)
628{
Jason Sams4820e8b2010-02-09 16:05:07 -0800629 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700630 mRootScript.set(s);
631}
632
Jason Samsccc010b2010-05-13 18:30:11 -0700633void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700634{
Jason Sams4820e8b2010-02-09 16:05:07 -0800635 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700636 if (pfs == NULL) {
637 mFragmentStore.set(mStateFragmentStore.mDefault);
638 } else {
639 mFragmentStore.set(pfs);
640 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700641}
642
643void Context::setFragment(ProgramFragment *pf)
644{
Jason Sams4820e8b2010-02-09 16:05:07 -0800645 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700646 if (pf == NULL) {
647 mFragment.set(mStateFragment.mDefault);
648 } else {
649 mFragment.set(pf);
650 }
Jason Samscfb1d112009-08-05 13:57:03 -0700651}
652
Jason Sams5fd09d82009-09-23 13:57:02 -0700653void Context::setRaster(ProgramRaster *pr)
654{
Jason Sams4820e8b2010-02-09 16:05:07 -0800655 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700656 if (pr == NULL) {
657 mRaster.set(mStateRaster.mDefault);
658 } else {
659 mRaster.set(pr);
660 }
661}
662
Jason Sams326e0dd2009-05-22 14:03:28 -0700663void Context::setVertex(ProgramVertex *pv)
664{
Jason Sams4820e8b2010-02-09 16:05:07 -0800665 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700666 if (pv == NULL) {
667 mVertex.set(mStateVertex.mDefault);
668 } else {
669 mVertex.set(pv);
670 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700671}
672
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700673void Context::setFont(Font *f)
674{
675 rsAssert(mIsGraphicsContext);
676 if (f == NULL) {
677 mFont.set(mStateFont.mDefault);
678 } else {
679 mFont.set(f);
680 }
681}
682
Jason Samsa4a54e42009-06-10 18:39:40 -0700683void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700684{
685 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700686 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700687 mNames.add(obj);
688}
689
690void Context::removeName(ObjectBase *obj)
691{
692 for(size_t ct=0; ct < mNames.size(); ct++) {
693 if (obj == mNames[ct]) {
694 mNames.removeAt(ct);
695 return;
696 }
697 }
698}
699
Jason Sams8c401ef2009-10-06 13:58:47 -0700700uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
701{
702 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700703 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700704 if (!wait) {
705 if (mIO.mToClient.isEmpty()) {
706 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700707 return 0;
708 }
709 }
710
711 //LOGE("getMessageToClient 2 con=%p", this);
712 uint32_t bytesData = 0;
713 uint32_t commandID = 0;
714 const void *d = mIO.mToClient.get(&commandID, &bytesData);
715 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
716
717 *receiveLen = bytesData;
718 if (bufferLen >= bytesData) {
719 memcpy(data, d, bytesData);
720 mIO.mToClient.next();
721 return commandID;
722 }
723 return 0;
724}
725
726bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
727{
728 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
729 if (cmdID == 0) {
730 LOGE("Attempting to send invalid command 0 to client.");
731 return false;
732 }
733 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700734 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700735 // Not enough room, and not waiting.
736 return false;
737 }
738 }
739 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700740 if (len > 0) {
741 void *p = mIO.mToClient.reserve(len);
742 memcpy(p, data, len);
743 mIO.mToClient.commit(cmdID, len);
744 } else {
745 mIO.mToClient.commit(cmdID, 0);
746 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700747 //LOGE("sendMessageToClient 3");
748 return true;
749}
750
751void Context::initToClient()
752{
753 while(!mRunning) {
754 usleep(100);
755 }
756}
757
758void Context::deinitToClient()
759{
760 mIO.mToClient.shutdown();
761}
Jason Sams50869382009-08-18 17:07:09 -0700762
Jason Samsa2cf7552010-03-03 13:03:18 -0800763const char * Context::getError(RsError *err)
764{
765 *err = mError;
766 mError = RS_ERROR_NONE;
767 if (*err != RS_ERROR_NONE) {
768 return mErrorMsg;
769 }
770 return NULL;
771}
772
773void Context::setError(RsError e, const char *msg)
774{
775 mError = e;
776 mErrorMsg = msg;
777}
778
779
Jason Sams13e26342009-11-24 12:26:35 -0800780void Context::dumpDebug() const
781{
782 LOGE("RS Context debug %p", this);
783 LOGE("RS Context debug");
784
785 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700786 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800787 LOGE(" GL vendor: %s", mGL.mVendor);
788 LOGE(" GL renderer: %s", mGL.mRenderer);
789 LOGE(" GL Version: %s", mGL.mVersion);
790 LOGE(" GL Extensions: %s", mGL.mExtensions);
791 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
792 LOGE(" RS width %i, height %i", mWidth, mHeight);
793 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
794 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
795
Jason Sams4815c0d2009-12-15 12:58:36 -0800796 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
797 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
798 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
799 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800800}
Jason Samsa4a54e42009-06-10 18:39:40 -0700801
Jason Sams326e0dd2009-05-22 14:03:28 -0700802///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700803//
Jason Sams326e0dd2009-05-22 14:03:28 -0700804
805namespace android {
806namespace renderscript {
807
Jason Sams8c880902010-06-15 12:15:57 -0700808void rsi_ContextFinish(Context *rsc)
809{
810}
Jason Sams326e0dd2009-05-22 14:03:28 -0700811
812void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
813{
814 Script *s = static_cast<Script *>(vs);
815 rsc->setRootScript(s);
816}
817
818void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
819{
820 Sampler *s = static_cast<Sampler *>(vs);
821
822 if (slot > RS_MAX_SAMPLER_SLOT) {
823 LOGE("Invalid sampler slot");
824 return;
825 }
826
827 s->bindToContext(&rsc->mStateSampler, slot);
828}
829
Jason Samsccc010b2010-05-13 18:30:11 -0700830void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700831{
Jason Samsccc010b2010-05-13 18:30:11 -0700832 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700833 rsc->setFragmentStore(pfs);
834}
835
836void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
837{
838 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
839 rsc->setFragment(pf);
840}
841
Jason Sams5fd09d82009-09-23 13:57:02 -0700842void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
843{
844 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
845 rsc->setRaster(pr);
846}
847
Jason Sams326e0dd2009-05-22 14:03:28 -0700848void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
849{
850 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
851 rsc->setVertex(pv);
852}
853
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700854void rsi_ContextBindFont(Context *rsc, RsFont vfont)
855{
856 Font *font = static_cast<Font *>(vfont);
857 rsc->setFont(font);
858}
859
Jason Samsa4a54e42009-06-10 18:39:40 -0700860void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700861{
862 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700863 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700864}
Jason Sams326e0dd2009-05-22 14:03:28 -0700865
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700866void rsi_GetName(Context *rsc, void * obj, const char **name)
867{
868 ObjectBase *ob = static_cast<ObjectBase *>(obj);
869 (*name) = ob->getName();
870}
871
Jason Sams707aaf32009-08-18 14:14:24 -0700872void rsi_ObjDestroy(Context *rsc, void *obj)
873{
874 ObjectBase *ob = static_cast<ObjectBase *>(obj);
875 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700876 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700877}
878
Jason Sams86f1b232009-09-24 17:38:20 -0700879void rsi_ContextPause(Context *rsc)
880{
881 rsc->pause();
882}
883
884void rsi_ContextResume(Context *rsc)
885{
886 rsc->resume();
887}
888
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700889void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800890{
Mathias Agopianfa402862010-02-12 14:04:35 -0800891 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800892}
893
Jason Sams15832442009-11-15 12:14:26 -0800894void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800895{
Jason Sams15832442009-11-15 12:14:26 -0800896 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800897}
898
Jason Samsc21cf402009-11-17 17:26:46 -0800899void rsi_ContextDump(Context *rsc, int32_t bits)
900{
901 ObjectBase::dumpAll(rsc);
902}
903
Jason Samsa2cf7552010-03-03 13:03:18 -0800904const char * rsi_ContextGetError(Context *rsc, RsError *e)
905{
906 const char *msg = rsc->getError(e);
907 if (*e != RS_ERROR_NONE) {
908 LOGE("RS Error %i %s", *e, msg);
909 }
910 return msg;
911}
912
Jason Sams326e0dd2009-05-22 14:03:28 -0700913}
914}
915
916
Jason Sams4820e8b2010-02-09 16:05:07 -0800917RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700918{
Jason Sams4820e8b2010-02-09 16:05:07 -0800919 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700920 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800921 Context *rsc = new Context(dev, false, false);
922 return rsc;
923}
924
925RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
926{
927 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
928 Device * dev = static_cast<Device *>(vdev);
929 Context *rsc = new Context(dev, true, useDepth);
Jason Sams900f1612010-09-16 18:18:29 -0700930 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700931 return rsc;
932}
933
934void rsContextDestroy(RsContext vrsc)
935{
936 Context * rsc = static_cast<Context *>(vrsc);
937 delete rsc;
938}
939
Jason Sams8c401ef2009-10-06 13:58:47 -0700940uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
941{
942 Context * rsc = static_cast<Context *>(vrsc);
943 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
944}
945
946void rsContextInitToClient(RsContext vrsc)
947{
948 Context * rsc = static_cast<Context *>(vrsc);
949 rsc->initToClient();
950}
951
952void rsContextDeinitToClient(RsContext vrsc)
953{
954 Context * rsc = static_cast<Context *>(vrsc);
955 rsc->deinitToClient();
956}
957