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