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