Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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" |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 20 | #include "utils/String8.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 22 | #include <GLES/gl.h> |
| 23 | #include <GLES/glext.h> |
| 24 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | using namespace android; |
| 26 | using namespace android::renderscript; |
| 27 | |
| 28 | Context * Context::gCon = NULL; |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 29 | pthread_key_t Context::gThreadTLSKey = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | |
| 31 | void Context::initEGL() |
| 32 | { |
| 33 | mNumConfigs = -1; |
| 34 | |
| 35 | EGLint s_configAttribs[] = { |
| 36 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 37 | EGL_RED_SIZE, 5, |
| 38 | EGL_GREEN_SIZE, 6, |
| 39 | EGL_BLUE_SIZE, 5, |
| 40 | EGL_DEPTH_SIZE, 16, |
| 41 | EGL_NONE |
| 42 | }; |
| 43 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 45 | eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 46 | eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 47 | |
| 48 | if (mWndSurface) { |
| 49 | mSurface = eglCreateWindowSurface(mDisplay, mConfig, |
| 50 | new EGLNativeWindowSurface(mWndSurface), |
| 51 | NULL); |
| 52 | } else { |
| 53 | mSurface = eglCreateWindowSurface(mDisplay, mConfig, |
| 54 | android_createDisplaySurface(), |
| 55 | NULL); |
| 56 | } |
| 57 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL); |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 59 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 60 | eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth); |
| 61 | eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 64 | bool Context::runScript(Script *s, uint32_t launchID) |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 65 | { |
| 66 | ObjectBaseRef<ProgramFragment> frag(mFragment); |
| 67 | ObjectBaseRef<ProgramVertex> vtx(mVertex); |
| 68 | ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore); |
| 69 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 70 | bool ret = s->run(this, launchID); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 71 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 72 | mFragment.set(frag); |
| 73 | mVertex.set(vtx); |
| 74 | mFragmentStore.set(store); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 75 | return true; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 80 | bool Context::runRootScript() |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | { |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 82 | rsAssert(mRootScript->mEnviroment.mIsRoot); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 83 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 84 | glColor4f(1,1,1,1); |
| 85 | glEnable(GL_LIGHT0); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 86 | glViewport(0, 0, mWidth, mHeight); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 87 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | glDepthMask(GL_TRUE); |
| 89 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 90 | |
Jason Sams | 928f5cf | 2009-06-08 18:50:13 -0700 | [diff] [blame] | 91 | glClearColor(mRootScript->mEnviroment.mClearColor[0], |
| 92 | mRootScript->mEnviroment.mClearColor[1], |
| 93 | mRootScript->mEnviroment.mClearColor[2], |
| 94 | mRootScript->mEnviroment.mClearColor[3]); |
| 95 | glClearDepthf(mRootScript->mEnviroment.mClearDepth); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 96 | glClear(GL_COLOR_BUFFER_BIT); |
| 97 | glClear(GL_DEPTH_BUFFER_BIT); |
| 98 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 99 | return runScript(mRootScript.get(), 0); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void Context::setupCheck() |
| 103 | { |
| 104 | if (mFragmentStore.get()) { |
| 105 | mFragmentStore->setupGL(); |
| 106 | } |
| 107 | if (mFragment.get()) { |
| 108 | mFragment->setupGL(); |
| 109 | } |
| 110 | if (mVertex.get()) { |
| 111 | mVertex->setupGL(); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void * Context::threadProc(void *vrsc) |
| 118 | { |
| 119 | Context *rsc = static_cast<Context *>(vrsc); |
| 120 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 121 | gIO = new ThreadIO(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 122 | rsc->initEGL(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 123 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 124 | ScriptTLSStruct *tlsStruct = new ScriptTLSStruct; |
| 125 | if (!tlsStruct) { |
| 126 | LOGE("Error allocating tls storage"); |
| 127 | return NULL; |
| 128 | } |
| 129 | tlsStruct->mContext = rsc; |
| 130 | tlsStruct->mScript = NULL; |
| 131 | int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct); |
| 132 | if (status) { |
| 133 | LOGE("pthread_setspecific %i", status); |
| 134 | } |
| 135 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 136 | rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight); |
| 137 | rsc->setVertex(NULL); |
| 138 | rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight); |
| 139 | rsc->setFragment(NULL); |
| 140 | rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight); |
| 141 | rsc->setFragmentStore(NULL); |
| 142 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 143 | rsc->mRunning = true; |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 144 | bool mDraw = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 145 | while (!rsc->mExit) { |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 146 | mDraw |= gIO->playCoreCommands(rsc, !mDraw); |
| 147 | mDraw &= (rsc->mRootScript.get() != NULL); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 148 | |
Jason Sams | 5f7fc27 | 2009-06-18 16:58:42 -0700 | [diff] [blame] | 149 | if (mDraw) { |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 150 | mDraw = rsc->runRootScript(); |
| 151 | eglSwapBuffers(rsc->mDisplay, rsc->mSurface); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 152 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 155 | glClearColor(0,0,0,0); |
| 156 | glClear(GL_COLOR_BUFFER_BIT); |
| 157 | eglSwapBuffers(rsc->mDisplay, rsc->mSurface); |
| 158 | eglTerminate(rsc->mDisplay); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | Context::Context(Device *dev, Surface *sur) |
| 163 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 164 | dev->addContext(this); |
| 165 | mDev = dev; |
| 166 | mRunning = false; |
| 167 | mExit = false; |
| 168 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 169 | // see comment in header |
| 170 | gCon = this; |
| 171 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 172 | int status; |
| 173 | pthread_attr_t threadAttr; |
| 174 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 175 | status = pthread_key_create(&gThreadTLSKey, NULL); |
| 176 | if (status) { |
| 177 | LOGE("Failed to init thread tls key."); |
| 178 | return; |
| 179 | } |
| 180 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 181 | status = pthread_attr_init(&threadAttr); |
| 182 | if (status) { |
| 183 | LOGE("Failed to init thread attribute."); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | sched_param sparam; |
| 188 | sparam.sched_priority = ANDROID_PRIORITY_DISPLAY; |
| 189 | pthread_attr_setschedparam(&threadAttr, &sparam); |
| 190 | |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 191 | mWndSurface = sur; |
| 192 | |
| 193 | LOGV("RS Launching thread"); |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 194 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 195 | if (status) { |
| 196 | LOGE("Failed to start rs context thread."); |
| 197 | } |
| 198 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 199 | while(!mRunning) { |
| 200 | sleep(1); |
| 201 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 202 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 203 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | Context::~Context() |
| 207 | { |
| 208 | mExit = true; |
| 209 | void *res; |
| 210 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 211 | int status = pthread_join(mThreadId, &res); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 212 | |
| 213 | if (mDev) { |
| 214 | mDev->removeContext(this); |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 215 | pthread_key_delete(gThreadTLSKey); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void Context::swapBuffers() |
| 220 | { |
| 221 | eglSwapBuffers(mDisplay, mSurface); |
| 222 | } |
| 223 | |
| 224 | void rsContextSwap(RsContext vrsc) |
| 225 | { |
| 226 | Context *rsc = static_cast<Context *>(vrsc); |
| 227 | rsc->swapBuffers(); |
| 228 | } |
| 229 | |
| 230 | void Context::setRootScript(Script *s) |
| 231 | { |
| 232 | mRootScript.set(s); |
| 233 | } |
| 234 | |
| 235 | void Context::setFragmentStore(ProgramFragmentStore *pfs) |
| 236 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 237 | if (pfs == NULL) { |
| 238 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 239 | } else { |
| 240 | mFragmentStore.set(pfs); |
| 241 | } |
| 242 | mFragmentStore->setupGL(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void Context::setFragment(ProgramFragment *pf) |
| 246 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 247 | if (pf == NULL) { |
| 248 | mFragment.set(mStateFragment.mDefault); |
| 249 | } else { |
| 250 | mFragment.set(pf); |
| 251 | } |
| 252 | mFragment->setupGL(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void Context::setVertex(ProgramVertex *pv) |
| 256 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 257 | if (pv == NULL) { |
| 258 | mVertex.set(mStateVertex.mDefault); |
| 259 | } else { |
| 260 | mVertex.set(pv); |
| 261 | } |
| 262 | mVertex->setupGL(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 265 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 266 | { |
| 267 | rsAssert(!obj->getName()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 268 | obj->setName(name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 269 | mNames.add(obj); |
| 270 | } |
| 271 | |
| 272 | void Context::removeName(ObjectBase *obj) |
| 273 | { |
| 274 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 275 | if (obj == mNames[ct]) { |
| 276 | mNames.removeAt(ct); |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | ObjectBase * Context::lookupName(const char *name) const |
| 283 | { |
| 284 | for(size_t ct=0; ct < mNames.size(); ct++) { |
| 285 | if (!strcmp(name, mNames[ct]->getName())) { |
| 286 | return mNames[ct]; |
| 287 | } |
| 288 | } |
| 289 | return NULL; |
| 290 | } |
| 291 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 292 | void Context::appendNameDefines(String8 *str) const |
| 293 | { |
| 294 | char buf[256]; |
| 295 | for (size_t ct=0; ct < mNames.size(); ct++) { |
| 296 | str->append("#define NAMED_"); |
| 297 | str->append(mNames[ct]->getName()); |
| 298 | str->append(" "); |
| 299 | sprintf(buf, "%i\n", (int)mNames[ct]); |
| 300 | str->append(buf); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 305 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 306 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 307 | |
| 308 | namespace android { |
| 309 | namespace renderscript { |
| 310 | |
| 311 | |
| 312 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) |
| 313 | { |
| 314 | Script *s = static_cast<Script *>(vs); |
| 315 | rsc->setRootScript(s); |
| 316 | } |
| 317 | |
| 318 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) |
| 319 | { |
| 320 | Sampler *s = static_cast<Sampler *>(vs); |
| 321 | |
| 322 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 323 | LOGE("Invalid sampler slot"); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | s->bindToContext(&rsc->mStateSampler, slot); |
| 328 | } |
| 329 | |
| 330 | void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs) |
| 331 | { |
| 332 | ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs); |
| 333 | rsc->setFragmentStore(pfs); |
| 334 | } |
| 335 | |
| 336 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) |
| 337 | { |
| 338 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
| 339 | rsc->setFragment(pf); |
| 340 | } |
| 341 | |
| 342 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) |
| 343 | { |
| 344 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
| 345 | rsc->setVertex(pv); |
| 346 | } |
| 347 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 348 | void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len) |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 349 | { |
| 350 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 351 | rsc->assignName(ob, name, len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 352 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 353 | |
| 354 | |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | |
| 359 | RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version) |
| 360 | { |
| 361 | Device * dev = static_cast<Device *>(vdev); |
| 362 | Context *rsc = new Context(dev, (Surface *)sur); |
| 363 | return rsc; |
| 364 | } |
| 365 | |
| 366 | void rsContextDestroy(RsContext vrsc) |
| 367 | { |
| 368 | Context * rsc = static_cast<Context *>(vrsc); |
| 369 | delete rsc; |
| 370 | } |
| 371 | |