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" |
| 20 | |
| 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 | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs); |
Jason Sams | d19f10d | 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 | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 54 | mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL); |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 55 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
Jason Sams | d19f10d | 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 | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 60 | bool Context::runRootScript() |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 61 | { |
| 62 | rsAssert(mRootScript->mIsRoot); |
| 63 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 64 | glColor4f(1,1,1,1); |
| 65 | glEnable(GL_LIGHT0); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | glViewport(0, 0, 320, 480); |
| 67 | float aspectH = 480.f / 320.f; |
| 68 | |
| 69 | if(mRootScript->mIsOrtho) { |
| 70 | glMatrixMode(GL_PROJECTION); |
| 71 | glLoadIdentity(); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 72 | glOrthof(0, 320, 480, 0, 0, 1); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 73 | glMatrixMode(GL_MODELVIEW); |
| 74 | } else { |
| 75 | glMatrixMode(GL_PROJECTION); |
| 76 | glLoadIdentity(); |
| 77 | glFrustumf(-1, 1, -aspectH, aspectH, 1, 100); |
| 78 | glRotatef(-90, 0,0,1); |
| 79 | glTranslatef(0, 0, -3); |
| 80 | glMatrixMode(GL_MODELVIEW); |
| 81 | } |
| 82 | |
| 83 | glMatrixMode(GL_MODELVIEW); |
| 84 | glLoadIdentity(); |
| 85 | |
| 86 | glDepthMask(GL_TRUE); |
| 87 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 88 | |
Jason Sams | 928f5cf | 2009-06-08 18:50:13 -0700 | [diff] [blame^] | 89 | glClearColor(mRootScript->mEnviroment.mClearColor[0], |
| 90 | mRootScript->mEnviroment.mClearColor[1], |
| 91 | mRootScript->mEnviroment.mClearColor[2], |
| 92 | mRootScript->mEnviroment.mClearColor[3]); |
| 93 | glClearDepthf(mRootScript->mEnviroment.mClearDepth); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 94 | glClear(GL_COLOR_BUFFER_BIT); |
| 95 | glClear(GL_DEPTH_BUFFER_BIT); |
| 96 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 97 | return mRootScript->run(this, 0); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void Context::setupCheck() |
| 101 | { |
| 102 | if (mFragmentStore.get()) { |
| 103 | mFragmentStore->setupGL(); |
| 104 | } |
| 105 | if (mFragment.get()) { |
| 106 | mFragment->setupGL(); |
| 107 | } |
| 108 | if (mVertex.get()) { |
| 109 | mVertex->setupGL(); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void * Context::threadProc(void *vrsc) |
| 116 | { |
| 117 | Context *rsc = static_cast<Context *>(vrsc); |
| 118 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | gIO = new ThreadIO(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 120 | rsc->mServerCommands.init(128); |
| 121 | rsc->mServerReturns.init(128); |
| 122 | |
| 123 | rsc->initEGL(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 124 | rsc->mRunning = true; |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 125 | bool mDraw = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 126 | while (!rsc->mExit) { |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 127 | mDraw |= gIO->playCoreCommands(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 129 | if (!mDraw || !rsc->mRootScript.get()) { |
| 130 | usleep(10000); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 134 | if (rsc->mRootScript.get()) { |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 135 | mDraw = rsc->runRootScript(); |
| 136 | eglSwapBuffers(rsc->mDisplay, rsc->mSurface); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 137 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 140 | glClearColor(0,0,0,0); |
| 141 | glClear(GL_COLOR_BUFFER_BIT); |
| 142 | eglSwapBuffers(rsc->mDisplay, rsc->mSurface); |
| 143 | eglTerminate(rsc->mDisplay); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | Context::Context(Device *dev, Surface *sur) |
| 148 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 149 | dev->addContext(this); |
| 150 | mDev = dev; |
| 151 | mRunning = false; |
| 152 | mExit = false; |
| 153 | |
| 154 | mServerCommands.init(256); |
| 155 | mServerReturns.init(256); |
| 156 | |
| 157 | // see comment in header |
| 158 | gCon = this; |
| 159 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 160 | int status; |
| 161 | pthread_attr_t threadAttr; |
| 162 | |
| 163 | status = pthread_attr_init(&threadAttr); |
| 164 | if (status) { |
| 165 | LOGE("Failed to init thread attribute."); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | sched_param sparam; |
| 170 | sparam.sched_priority = ANDROID_PRIORITY_DISPLAY; |
| 171 | pthread_attr_setschedparam(&threadAttr, &sparam); |
| 172 | |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 173 | LOGE("RS Launching thread"); |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 174 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 175 | if (status) { |
| 176 | LOGE("Failed to start rs context thread."); |
| 177 | } |
| 178 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 179 | mWndSurface = sur; |
| 180 | while(!mRunning) { |
| 181 | sleep(1); |
| 182 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 183 | |
Jason Sams | 8ad0010 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 184 | pthread_attr_destroy(&threadAttr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | Context::~Context() |
| 188 | { |
| 189 | mExit = true; |
| 190 | void *res; |
| 191 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 192 | int status = pthread_join(mThreadId, &res); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 193 | |
| 194 | if (mDev) { |
| 195 | mDev->removeContext(this); |
| 196 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void Context::swapBuffers() |
| 200 | { |
| 201 | eglSwapBuffers(mDisplay, mSurface); |
| 202 | } |
| 203 | |
| 204 | void rsContextSwap(RsContext vrsc) |
| 205 | { |
| 206 | Context *rsc = static_cast<Context *>(vrsc); |
| 207 | rsc->swapBuffers(); |
| 208 | } |
| 209 | |
| 210 | void Context::setRootScript(Script *s) |
| 211 | { |
| 212 | mRootScript.set(s); |
| 213 | } |
| 214 | |
| 215 | void Context::setFragmentStore(ProgramFragmentStore *pfs) |
| 216 | { |
| 217 | mFragmentStore.set(pfs); |
| 218 | pfs->setupGL(); |
| 219 | } |
| 220 | |
| 221 | void Context::setFragment(ProgramFragment *pf) |
| 222 | { |
| 223 | mFragment.set(pf); |
| 224 | pf->setupGL(); |
| 225 | } |
| 226 | |
| 227 | void Context::setVertex(ProgramVertex *pv) |
| 228 | { |
| 229 | mVertex.set(pv); |
| 230 | pv->setupGL(); |
| 231 | } |
| 232 | |
| 233 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 234 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 235 | |
| 236 | namespace android { |
| 237 | namespace renderscript { |
| 238 | |
| 239 | |
| 240 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) |
| 241 | { |
| 242 | Script *s = static_cast<Script *>(vs); |
| 243 | rsc->setRootScript(s); |
| 244 | } |
| 245 | |
| 246 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) |
| 247 | { |
| 248 | Sampler *s = static_cast<Sampler *>(vs); |
| 249 | |
| 250 | if (slot > RS_MAX_SAMPLER_SLOT) { |
| 251 | LOGE("Invalid sampler slot"); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | s->bindToContext(&rsc->mStateSampler, slot); |
| 256 | } |
| 257 | |
| 258 | void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs) |
| 259 | { |
| 260 | ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs); |
| 261 | rsc->setFragmentStore(pfs); |
| 262 | } |
| 263 | |
| 264 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) |
| 265 | { |
| 266 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
| 267 | rsc->setFragment(pf); |
| 268 | } |
| 269 | |
| 270 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) |
| 271 | { |
| 272 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
| 273 | rsc->setVertex(pv); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
| 282 | RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version) |
| 283 | { |
| 284 | Device * dev = static_cast<Device *>(vdev); |
| 285 | Context *rsc = new Context(dev, (Surface *)sur); |
| 286 | return rsc; |
| 287 | } |
| 288 | |
| 289 | void rsContextDestroy(RsContext vrsc) |
| 290 | { |
| 291 | Context * rsc = static_cast<Context *>(vrsc); |
| 292 | delete rsc; |
| 293 | } |
| 294 | |