blob: 629b481142893f0f34bc0684eaba38442b0c4aba [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 Sams6d751ef2009-10-08 12:55:06 -070091 LOGV("initEGL start");
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 Sams9397e302009-08-27 20:23:34 -0700100 LOGE("couldn't find an EGLConfig matching the screen format\n");
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) {
112 LOGE("eglCreateContext returned EGL_NO_CONTEXT");
113 }
114 gGLContextCount++;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams33b6e3b2009-10-27 14:44:31 -0700117void Context::deinitEGL()
118{
Jason Sams613cad12009-11-12 15:10:25 -0800119 LOGV("deinitEGL");
120 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) {
153 LOGE("GL Error, 0x%x, from %s", err, msg);
154 }
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 Samsc460e552009-11-25 13:22:07 -0800235 if (checkVersion2_0()) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800236 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
237 LOGE("Context::setupCheck() 1 fail");
238 return false;
239 }
Jason Samsc460e552009-11-25 13:22:07 -0800240
241 mFragmentStore->setupGL2(this, &mStateFragmentStore);
242 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
243 mRaster->setupGL2(this, &mStateRaster);
244 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
245
246 } else {
247 mFragmentStore->setupGL(this, &mStateFragmentStore);
248 mFragment->setupGL(this, &mStateFragment);
249 mRaster->setupGL(this, &mStateRaster);
250 mVertex->setupGL(this, &mStateVertex);
251 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800252 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700253}
254
Jason Sams1fddd902009-09-25 15:25:00 -0700255static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700256{
257 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700258 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700259 return 0 != strcmp(buf, "0");
260}
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
262void * Context::threadProc(void *vrsc)
263{
264 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800265 rsc->mNativeThreadId = gettid();
266
267 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800268 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700269
Jason Sams1fddd902009-09-25 15:25:00 -0700270 rsc->props.mLogTimes = getProp("debug.rs.profile");
271 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800272 rsc->props.mLogObjects = getProp("debug.rs.object");
273 rsc->props.mLogShaders = getProp("debug.rs.shader");
Joe Onorato76371ff2009-09-23 16:37:36 -0700274
Jason Samse5769102009-06-19 16:03:18 -0700275 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
276 if (!tlsStruct) {
277 LOGE("Error allocating tls storage");
278 return NULL;
279 }
280 tlsStruct->mContext = rsc;
281 tlsStruct->mScript = NULL;
282 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
283 if (status) {
284 LOGE("pthread_setspecific %i", status);
285 }
286
Jason Sams4820e8b2010-02-09 16:05:07 -0800287 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700288 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800289 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700290 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800291 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700292 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800293 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700294 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800295 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700296 rsc->mStateFont.init(rsc);
297 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800298 rsc->mStateVertexArray.init(rsc);
299 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700300
Jason Sams326e0dd2009-05-22 14:03:28 -0700301 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700302 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700303 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700304 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700305 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800306 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700307
Jason Sams2dca84d2009-12-09 11:05:45 -0800308 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800309 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800310 targetTime = rsc->runRootScript();
311 mDraw = targetTime && !rsc->mPaused;
312 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700313 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800314 rsc->timerFrame();
315 rsc->timerSet(RS_TIMER_INTERNAL);
316 rsc->timerPrint();
317 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700318 }
Jason Sams24371d92009-08-19 12:17:14 -0700319 if (rsc->mObjDestroy.mNeedToEmpty) {
320 rsc->objDestroyOOBRun();
321 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800322 if (rsc->mThreadPriority > 0 && targetTime) {
323 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
324 if (t > 0) {
325 usleep(t);
326 }
327 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700328 }
329
Jason Sams8c0ee652009-08-25 14:49:07 -0700330 LOGV("RS Thread exiting");
Jason Sams4820e8b2010-02-09 16:05:07 -0800331 if (rsc->mIsGraphicsContext) {
332 rsc->mRaster.clear();
333 rsc->mFragment.clear();
334 rsc->mVertex.clear();
335 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700336 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800337 rsc->mRootScript.clear();
338 rsc->mStateRaster.deinit(rsc);
339 rsc->mStateVertex.deinit(rsc);
340 rsc->mStateFragment.deinit(rsc);
341 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700342 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800343 }
Jason Samse514b452009-09-25 14:51:22 -0700344 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700345
Jason Samse402ed32009-11-03 11:25:42 -0800346 rsc->mObjDestroy.mNeedToEmpty = true;
347 rsc->objDestroyOOBRun();
348
Jason Sams4820e8b2010-02-09 16:05:07 -0800349 if (rsc->mIsGraphicsContext) {
350 pthread_mutex_lock(&gInitMutex);
351 rsc->deinitEGL();
352 pthread_mutex_unlock(&gInitMutex);
353 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700354
Jason Sams8c0ee652009-08-25 14:49:07 -0700355 LOGV("RS Thread exited");
Jason Sams326e0dd2009-05-22 14:03:28 -0700356 return NULL;
357}
358
Jason Sams7bf29dd2010-07-19 15:38:19 -0700359void * Context::helperThreadProc(void *vrsc)
360{
361 Context *rsc = static_cast<Context *>(vrsc);
362 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
363
364 LOGE("helperThreadProc 1 %p idx=%i", rsc, idx);
365
366 rsc->mWorkers.mLaunchSignals[idx].init();
367 rsc->mWorkers.mNativeThreadId[idx] = gettid();
368
369 //cpu_set_t cpset[16];
370 //int ret = sched_getaffinity(rsc->mWorkers.mNativeThreadId[idx], sizeof(cpset), &cpset);
371 //LOGE("ret = %i", ret);
372
373//sched_setaffinity
374
375 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
376 while(rsc->mRunning) {
377 rsc->mWorkers.mLaunchSignals[idx].wait();
378 if (rsc->mWorkers.mLaunchCallback) {
379 LOGE("helperThreadProc 4");
380 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
381 }
382 LOGE("helperThreadProc 5");
383 android_atomic_dec(&rsc->mWorkers.mRunningCount);
384 rsc->mWorkers.mCompleteSignal.set();
385 }
386 return NULL;
387}
388
389void Context::launchThreads(WorkerCallback_t cbk, void *data)
390{
391 mWorkers.mLaunchData = data;
392 mWorkers.mLaunchCallback = cbk;
393 mWorkers.mRunningCount = (int)mWorkers.mCount;
394 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
395 mWorkers.mLaunchSignals[ct].set();
396 }
397 while(mWorkers.mRunningCount) {
398 mWorkers.mCompleteSignal.wait();
399 }
400}
401
Jason Sams15832442009-11-15 12:14:26 -0800402void Context::setPriority(int32_t p)
403{
404 // Note: If we put this in the proper "background" policy
405 // the wallpapers can become completly unresponsive at times.
406 // This is probably not what we want for something the user is actively
407 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800408 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800409#if 0
410 SchedPolicy pol = SP_FOREGROUND;
411 if (p > 0) {
412 pol = SP_BACKGROUND;
413 }
414 if (!set_sched_policy(mNativeThreadId, pol)) {
415 // success; reset the priority as well
416 }
417#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700418 setpriority(PRIO_PROCESS, mNativeThreadId, p);
419 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
420 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
421 }
Jason Sams15832442009-11-15 12:14:26 -0800422#endif
423}
424
Jason Sams4820e8b2010-02-09 16:05:07 -0800425Context::Context(Device *dev, bool isGraphics, bool useDepth)
Jason Sams326e0dd2009-05-22 14:03:28 -0700426{
Jason Samsfb03a222009-10-15 16:47:31 -0700427 pthread_mutex_lock(&gInitMutex);
428
Jason Sams326e0dd2009-05-22 14:03:28 -0700429 dev->addContext(this);
430 mDev = dev;
431 mRunning = false;
432 mExit = false;
Jason Samsafcb25c2009-08-25 11:34:49 -0700433 mUseDepth = useDepth;
Jason Sams86f1b232009-09-24 17:38:20 -0700434 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700435 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800436 mError = RS_ERROR_NONE;
437 mErrorMsg = NULL;
438
Jason Sams613cad12009-11-12 15:10:25 -0800439 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800440 memset(&mGL, 0, sizeof(mGL));
441 mIsGraphicsContext = isGraphics;
Jason Sams326e0dd2009-05-22 14:03:28 -0700442
Jason Samsa658e902009-06-04 14:35:01 -0700443 int status;
444 pthread_attr_t threadAttr;
445
Jason Samsfb03a222009-10-15 16:47:31 -0700446 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700447 status = pthread_key_create(&gThreadTLSKey, NULL);
448 if (status) {
449 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700450 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700451 return;
452 }
Jason Samse5769102009-06-19 16:03:18 -0700453 }
Jason Samsfb03a222009-10-15 16:47:31 -0700454 gThreadTLSKeyCount++;
455 pthread_mutex_unlock(&gInitMutex);
456
457 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700458
Jason Samsa658e902009-06-04 14:35:01 -0700459 status = pthread_attr_init(&threadAttr);
460 if (status) {
461 LOGE("Failed to init thread attribute.");
462 return;
463 }
464
Jason Sams613cad12009-11-12 15:10:25 -0800465 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700466
Jason Sams50869382009-08-18 17:07:09 -0700467 objDestroyOOBInit();
Jason Sams24371d92009-08-19 12:17:14 -0700468 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700469 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700470
Jason Sams7bf29dd2010-07-19 15:38:19 -0700471 LOGV("RS Launching thread(s)");
472 mWorkers.mCount = 2;
473 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
474 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
475 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
476 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700477 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700478 if (status) {
479 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700480 return;
481 }
482 mWorkers.mRunningCount = 0;
483 mWorkers.mLaunchCount = 0;
484 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
485 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
486 if (status) {
487 mWorkers.mCount = ct;
488 LOGE("Created fewer than expected number of RS threads.");
489 break;
490 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700491 }
492
Jason Sams326e0dd2009-05-22 14:03:28 -0700493 while(!mRunning) {
Jason Samsada7f272009-09-24 14:55:38 -0700494 usleep(100);
Jason Sams326e0dd2009-05-22 14:03:28 -0700495 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700496
Jason Samsa658e902009-06-04 14:35:01 -0700497 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700498}
499
500Context::~Context()
501{
Jason Sams8c0ee652009-08-25 14:49:07 -0700502 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700503 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700504 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700505 void *res;
506
Jason Sams8c0ee652009-08-25 14:49:07 -0700507 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700508 int status = pthread_join(mThreadId, &res);
Jason Samsbf3c14e2009-11-02 14:25:10 -0800509 mObjDestroy.mNeedToEmpty = true;
Jason Sams50869382009-08-18 17:07:09 -0700510 objDestroyOOBRun();
Jason Sams326e0dd2009-05-22 14:03:28 -0700511
Jason Samsfb03a222009-10-15 16:47:31 -0700512 // Global structure cleanup.
513 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700514 if (mDev) {
515 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700516 --gThreadTLSKeyCount;
517 if (!gThreadTLSKeyCount) {
518 pthread_key_delete(gThreadTLSKey);
519 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800520 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700521 }
Jason Samsfb03a222009-10-15 16:47:31 -0700522 pthread_mutex_unlock(&gInitMutex);
Jason Sams50869382009-08-18 17:07:09 -0700523
524 objDestroyOOBDestroy();
Jason Sams326e0dd2009-05-22 14:03:28 -0700525}
526
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700527void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800528{
Jason Sams4820e8b2010-02-09 16:05:07 -0800529 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800530
Jason Sams458f2dc2009-11-03 13:58:36 -0800531 EGLBoolean ret;
532 if (mEGL.mSurface != NULL) {
533 ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
534 checkEglError("eglMakeCurrent", ret);
535
536 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
537 checkEglError("eglDestroySurface", ret);
538
539 mEGL.mSurface = NULL;
Jason Sams613cad12009-11-12 15:10:25 -0800540 mWidth = 0;
541 mHeight = 0;
Jason Sams458f2dc2009-11-03 13:58:36 -0800542 }
543
544 mWndSurface = sur;
545 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700546 mWidth = w;
547 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800548 bool first = false;
549 if (!mEGL.mContext) {
550 first = true;
551 pthread_mutex_lock(&gInitMutex);
Jason Samsf2a5d732009-11-30 14:49:55 -0800552 initEGL(true);
Jason Sams613cad12009-11-12 15:10:25 -0800553 pthread_mutex_unlock(&gInitMutex);
554 }
555
Jason Sams458f2dc2009-11-03 13:58:36 -0800556 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
557 checkEglError("eglCreateWindowSurface");
558 if (mEGL.mSurface == EGL_NO_SURFACE) {
559 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
560 }
561
562 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
563 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800564
Jason Sams771565f2010-05-14 15:30:29 -0700565 mStateVertex.updateSize(this);
Jason Sams613cad12009-11-12 15:10:25 -0800566
Jason Sams613cad12009-11-12 15:10:25 -0800567 if (first) {
568 mGL.mVersion = glGetString(GL_VERSION);
569 mGL.mVendor = glGetString(GL_VENDOR);
570 mGL.mRenderer = glGetString(GL_RENDERER);
571 mGL.mExtensions = glGetString(GL_EXTENSIONS);
572
573 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
574 LOGV("GL Version %s", mGL.mVersion);
Jason Samsc460e552009-11-25 13:22:07 -0800575 //LOGV("GL Vendor %s", mGL.mVendor);
Jason Sams613cad12009-11-12 15:10:25 -0800576 LOGV("GL Renderer %s", mGL.mRenderer);
577 //LOGV("GL Extensions %s", mGL.mExtensions);
578
Jason Samsc460e552009-11-25 13:22:07 -0800579 const char *verptr = NULL;
580 if (strlen((const char *)mGL.mVersion) > 9) {
581 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
582 verptr = (const char *)mGL.mVersion + 12;
583 }
584 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
585 verptr = (const char *)mGL.mVersion + 9;
586 }
587 }
588
589 if (!verptr) {
Jason Sams613cad12009-11-12 15:10:25 -0800590 LOGE("Error, OpenGL ES Lite not supported");
591 } else {
Jason Samsc460e552009-11-25 13:22:07 -0800592 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
Jason Sams613cad12009-11-12 15:10:25 -0800593 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800594
595 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
596 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
597 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
598
599 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
600 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
601
602 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
603 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
Jason Samsef21edc2010-02-22 15:37:51 -0800604
605 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Jason Sams613cad12009-11-12 15:10:25 -0800606 }
607
Jason Sams458f2dc2009-11-03 13:58:36 -0800608 }
609}
610
Jason Sams86f1b232009-09-24 17:38:20 -0700611void Context::pause()
612{
Jason Sams4820e8b2010-02-09 16:05:07 -0800613 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700614 mPaused = true;
615}
616
617void Context::resume()
618{
Jason Sams4820e8b2010-02-09 16:05:07 -0800619 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700620 mPaused = false;
621}
622
Jason Sams326e0dd2009-05-22 14:03:28 -0700623void Context::setRootScript(Script *s)
624{
Jason Sams4820e8b2010-02-09 16:05:07 -0800625 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700626 mRootScript.set(s);
627}
628
Jason Samsccc010b2010-05-13 18:30:11 -0700629void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700630{
Jason Sams4820e8b2010-02-09 16:05:07 -0800631 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700632 if (pfs == NULL) {
633 mFragmentStore.set(mStateFragmentStore.mDefault);
634 } else {
635 mFragmentStore.set(pfs);
636 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700637}
638
639void Context::setFragment(ProgramFragment *pf)
640{
Jason Sams4820e8b2010-02-09 16:05:07 -0800641 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700642 if (pf == NULL) {
643 mFragment.set(mStateFragment.mDefault);
644 } else {
645 mFragment.set(pf);
646 }
Jason Samscfb1d112009-08-05 13:57:03 -0700647}
648
Jason Sams5fd09d82009-09-23 13:57:02 -0700649void Context::setRaster(ProgramRaster *pr)
650{
Jason Sams4820e8b2010-02-09 16:05:07 -0800651 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700652 if (pr == NULL) {
653 mRaster.set(mStateRaster.mDefault);
654 } else {
655 mRaster.set(pr);
656 }
657}
658
Jason Sams326e0dd2009-05-22 14:03:28 -0700659void Context::setVertex(ProgramVertex *pv)
660{
Jason Sams4820e8b2010-02-09 16:05:07 -0800661 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700662 if (pv == NULL) {
663 mVertex.set(mStateVertex.mDefault);
664 } else {
665 mVertex.set(pv);
666 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700667}
668
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700669void Context::setFont(Font *f)
670{
671 rsAssert(mIsGraphicsContext);
672 if (f == NULL) {
673 mFont.set(mStateFont.mDefault);
674 } else {
675 mFont.set(f);
676 }
677}
678
Jason Samsa4a54e42009-06-10 18:39:40 -0700679void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700680{
681 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700682 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700683 mNames.add(obj);
684}
685
686void Context::removeName(ObjectBase *obj)
687{
688 for(size_t ct=0; ct < mNames.size(); ct++) {
689 if (obj == mNames[ct]) {
690 mNames.removeAt(ct);
691 return;
692 }
693 }
694}
695
Jason Sams50869382009-08-18 17:07:09 -0700696bool Context::objDestroyOOBInit()
697{
Jason Sams12b14ae2010-03-18 11:39:44 -0700698 if (!mObjDestroy.mMutex.init()) {
Jason Sams50869382009-08-18 17:07:09 -0700699 LOGE("Context::ObjDestroyOOBInit mutex init failure");
700 return false;
701 }
702 return true;
703}
704
705void Context::objDestroyOOBRun()
706{
707 if (mObjDestroy.mNeedToEmpty) {
Jason Sams12b14ae2010-03-18 11:39:44 -0700708 if (!mObjDestroy.mMutex.lock()) {
709 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700710 return;
711 }
712
713 for (size_t ct = 0; ct < mObjDestroy.mDestroyList.size(); ct++) {
Jason Sams9397e302009-08-27 20:23:34 -0700714 mObjDestroy.mDestroyList[ct]->decUserRef();
Jason Sams50869382009-08-18 17:07:09 -0700715 }
716 mObjDestroy.mDestroyList.clear();
717 mObjDestroy.mNeedToEmpty = false;
Jason Sams12b14ae2010-03-18 11:39:44 -0700718 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700719 }
720}
721
722void Context::objDestroyOOBDestroy()
723{
724 rsAssert(!mObjDestroy.mNeedToEmpty);
Jason Sams50869382009-08-18 17:07:09 -0700725}
726
727void Context::objDestroyAdd(ObjectBase *obj)
728{
Jason Sams12b14ae2010-03-18 11:39:44 -0700729 if (!mObjDestroy.mMutex.lock()) {
730 LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun.");
Jason Sams50869382009-08-18 17:07:09 -0700731 return;
732 }
733
734 mObjDestroy.mNeedToEmpty = true;
735 mObjDestroy.mDestroyList.add(obj);
Jason Sams12b14ae2010-03-18 11:39:44 -0700736 mObjDestroy.mMutex.unlock();
Jason Sams50869382009-08-18 17:07:09 -0700737}
738
Jason Sams8c401ef2009-10-06 13:58:47 -0700739uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
740{
741 //LOGE("getMessageToClient %i %i", bufferLen, wait);
742 if (!wait) {
743 if (mIO.mToClient.isEmpty()) {
744 // No message to get and not going to wait for one.
745 receiveLen = 0;
746 return 0;
747 }
748 }
749
750 //LOGE("getMessageToClient 2 con=%p", this);
751 uint32_t bytesData = 0;
752 uint32_t commandID = 0;
753 const void *d = mIO.mToClient.get(&commandID, &bytesData);
754 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
755
756 *receiveLen = bytesData;
757 if (bufferLen >= bytesData) {
758 memcpy(data, d, bytesData);
759 mIO.mToClient.next();
760 return commandID;
761 }
762 return 0;
763}
764
765bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
766{
767 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
768 if (cmdID == 0) {
769 LOGE("Attempting to send invalid command 0 to client.");
770 return false;
771 }
772 if (!waitForSpace) {
773 if (mIO.mToClient.getFreeSpace() < len) {
774 // Not enough room, and not waiting.
775 return false;
776 }
777 }
778 //LOGE("sendMessageToClient 2");
779 void *p = mIO.mToClient.reserve(len);
780 memcpy(p, data, len);
781 mIO.mToClient.commit(cmdID, len);
782 //LOGE("sendMessageToClient 3");
783 return true;
784}
785
786void Context::initToClient()
787{
788 while(!mRunning) {
789 usleep(100);
790 }
791}
792
793void Context::deinitToClient()
794{
795 mIO.mToClient.shutdown();
796}
Jason Sams50869382009-08-18 17:07:09 -0700797
Jason Samsa2cf7552010-03-03 13:03:18 -0800798const char * Context::getError(RsError *err)
799{
800 *err = mError;
801 mError = RS_ERROR_NONE;
802 if (*err != RS_ERROR_NONE) {
803 return mErrorMsg;
804 }
805 return NULL;
806}
807
808void Context::setError(RsError e, const char *msg)
809{
810 mError = e;
811 mErrorMsg = msg;
812}
813
814
Jason Sams13e26342009-11-24 12:26:35 -0800815void Context::dumpDebug() const
816{
817 LOGE("RS Context debug %p", this);
818 LOGE("RS Context debug");
819
820 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700821 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800822 LOGE(" GL vendor: %s", mGL.mVendor);
823 LOGE(" GL renderer: %s", mGL.mRenderer);
824 LOGE(" GL Version: %s", mGL.mVersion);
825 LOGE(" GL Extensions: %s", mGL.mExtensions);
826 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
827 LOGE(" RS width %i, height %i", mWidth, mHeight);
828 LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
829 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
830
Jason Sams4815c0d2009-12-15 12:58:36 -0800831 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
832 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
833 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
834 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800835}
Jason Samsa4a54e42009-06-10 18:39:40 -0700836
Jason Sams326e0dd2009-05-22 14:03:28 -0700837///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700838//
Jason Sams326e0dd2009-05-22 14:03:28 -0700839
840namespace android {
841namespace renderscript {
842
Jason Sams8c880902010-06-15 12:15:57 -0700843void rsi_ContextFinish(Context *rsc)
844{
845}
Jason Sams326e0dd2009-05-22 14:03:28 -0700846
847void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
848{
849 Script *s = static_cast<Script *>(vs);
850 rsc->setRootScript(s);
851}
852
853void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
854{
855 Sampler *s = static_cast<Sampler *>(vs);
856
857 if (slot > RS_MAX_SAMPLER_SLOT) {
858 LOGE("Invalid sampler slot");
859 return;
860 }
861
862 s->bindToContext(&rsc->mStateSampler, slot);
863}
864
Jason Samsccc010b2010-05-13 18:30:11 -0700865void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700866{
Jason Samsccc010b2010-05-13 18:30:11 -0700867 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700868 rsc->setFragmentStore(pfs);
869}
870
871void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
872{
873 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
874 rsc->setFragment(pf);
875}
876
Jason Sams5fd09d82009-09-23 13:57:02 -0700877void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
878{
879 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
880 rsc->setRaster(pr);
881}
882
Jason Sams326e0dd2009-05-22 14:03:28 -0700883void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
884{
885 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
886 rsc->setVertex(pv);
887}
888
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700889void rsi_ContextBindFont(Context *rsc, RsFont vfont)
890{
891 Font *font = static_cast<Font *>(vfont);
892 rsc->setFont(font);
893}
894
895
Jason Samsa4a54e42009-06-10 18:39:40 -0700896void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700897{
898 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700899 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700900}
Jason Sams326e0dd2009-05-22 14:03:28 -0700901
Jason Sams707aaf32009-08-18 14:14:24 -0700902void rsi_ObjDestroy(Context *rsc, void *obj)
903{
904 ObjectBase *ob = static_cast<ObjectBase *>(obj);
905 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700906 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700907}
908
Jason Sams86f1b232009-09-24 17:38:20 -0700909void rsi_ContextPause(Context *rsc)
910{
911 rsc->pause();
912}
913
914void rsi_ContextResume(Context *rsc)
915{
916 rsc->resume();
917}
918
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700919void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800920{
Mathias Agopianfa402862010-02-12 14:04:35 -0800921 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800922}
923
Jason Sams15832442009-11-15 12:14:26 -0800924void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800925{
Jason Sams15832442009-11-15 12:14:26 -0800926 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800927}
928
Jason Samsc21cf402009-11-17 17:26:46 -0800929void rsi_ContextDump(Context *rsc, int32_t bits)
930{
931 ObjectBase::dumpAll(rsc);
932}
933
Jason Samsa2cf7552010-03-03 13:03:18 -0800934const char * rsi_ContextGetError(Context *rsc, RsError *e)
935{
936 const char *msg = rsc->getError(e);
937 if (*e != RS_ERROR_NONE) {
938 LOGE("RS Error %i %s", *e, msg);
939 }
940 return msg;
941}
942
Jason Sams326e0dd2009-05-22 14:03:28 -0700943}
944}
945
946
Jason Sams4820e8b2010-02-09 16:05:07 -0800947RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -0700948{
Jason Sams4820e8b2010-02-09 16:05:07 -0800949 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -0700950 Device * dev = static_cast<Device *>(vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -0800951 Context *rsc = new Context(dev, false, false);
952 return rsc;
953}
954
955RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
956{
957 LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
958 Device * dev = static_cast<Device *>(vdev);
959 Context *rsc = new Context(dev, true, useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700960 return rsc;
961}
962
963void rsContextDestroy(RsContext vrsc)
964{
965 Context * rsc = static_cast<Context *>(vrsc);
966 delete rsc;
967}
968
Jason Sams50869382009-08-18 17:07:09 -0700969void rsObjDestroyOOB(RsContext vrsc, void *obj)
970{
971 Context * rsc = static_cast<Context *>(vrsc);
972 rsc->objDestroyAdd(static_cast<ObjectBase *>(obj));
973}
974
Jason Sams8c401ef2009-10-06 13:58:47 -0700975uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
976{
977 Context * rsc = static_cast<Context *>(vrsc);
978 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
979}
980
981void rsContextInitToClient(RsContext vrsc)
982{
983 Context * rsc = static_cast<Context *>(vrsc);
984 rsc->initToClient();
985}
986
987void rsContextDeinitToClient(RsContext vrsc)
988{
989 Context * rsc = static_cast<Context *>(vrsc);
990 rsc->deinitToClient();
991}
992