blob: 82d64992efcdbb5daf207c2d2db47d8ef10be159 [file] [log] [blame]
Jason Samsd19f10d2009-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"
20
21
22using namespace android;
23using namespace android::renderscript;
24
25Context * Context::gCon = NULL;
26
27void 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 Samsd19f10d2009-05-22 14:03:28 -070040 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Samsd19f10d2009-05-22 14:03:28 -070041 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Samsd19f10d2009-05-22 14:03:28 -070042 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Samsd19f10d2009-05-22 14:03:28 -070043
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 Samsd19f10d2009-05-22 14:03:28 -070054 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa09f11d2009-06-04 17:58:03 -070055 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Samsd19f10d2009-05-22 14:03:28 -070056 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
57 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -070058}
59
Jason Samsda423d82009-06-09 12:15:30 -070060bool Context::runScript(Script *s)
61{
62 ObjectBaseRef<ProgramFragment> frag(mFragment);
63 ObjectBaseRef<ProgramVertex> vtx(mVertex);
64 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
65
66
67
68 return true;
69
70}
71
72
Jason Samsa09f11d2009-06-04 17:58:03 -070073bool Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -070074{
Jason Samsda423d82009-06-09 12:15:30 -070075 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -070076
Jason Samsa09f11d2009-06-04 17:58:03 -070077 glColor4f(1,1,1,1);
78 glEnable(GL_LIGHT0);
Jason Samsda423d82009-06-09 12:15:30 -070079 glViewport(0, 0, mWidth, mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -070080
Jason Samsda423d82009-06-09 12:15:30 -070081 if(mRootScript->mEnviroment.mIsOrtho) {
Jason Samsd19f10d2009-05-22 14:03:28 -070082 glMatrixMode(GL_PROJECTION);
83 glLoadIdentity();
Jason Samsda423d82009-06-09 12:15:30 -070084 glOrthof(0, mWidth, mHeight, 0, 0, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -070085 glMatrixMode(GL_MODELVIEW);
86 } else {
Jason Samsda423d82009-06-09 12:15:30 -070087 float aspectH = ((float)mWidth) / mHeight;
Jason Samsd19f10d2009-05-22 14:03:28 -070088 glMatrixMode(GL_PROJECTION);
89 glLoadIdentity();
90 glFrustumf(-1, 1, -aspectH, aspectH, 1, 100);
91 glRotatef(-90, 0,0,1);
92 glTranslatef(0, 0, -3);
93 glMatrixMode(GL_MODELVIEW);
94 }
95
96 glMatrixMode(GL_MODELVIEW);
97 glLoadIdentity();
98
99 glDepthMask(GL_TRUE);
100 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
101
Jason Sams928f5cf2009-06-08 18:50:13 -0700102 glClearColor(mRootScript->mEnviroment.mClearColor[0],
103 mRootScript->mEnviroment.mClearColor[1],
104 mRootScript->mEnviroment.mClearColor[2],
105 mRootScript->mEnviroment.mClearColor[3]);
106 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -0700107 glClear(GL_COLOR_BUFFER_BIT);
108 glClear(GL_DEPTH_BUFFER_BIT);
109
Jason Samsa09f11d2009-06-04 17:58:03 -0700110 return mRootScript->run(this, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -0700111}
112
113void Context::setupCheck()
114{
115 if (mFragmentStore.get()) {
116 mFragmentStore->setupGL();
117 }
118 if (mFragment.get()) {
119 mFragment->setupGL();
120 }
121 if (mVertex.get()) {
122 mVertex->setupGL();
123 }
124
125}
126
127
128void * Context::threadProc(void *vrsc)
129{
130 Context *rsc = static_cast<Context *>(vrsc);
131
Jason Samsd19f10d2009-05-22 14:03:28 -0700132 gIO = new ThreadIO();
Jason Samsd19f10d2009-05-22 14:03:28 -0700133 rsc->mServerCommands.init(128);
134 rsc->mServerReturns.init(128);
135
136 rsc->initEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700137 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700138 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700139 while (!rsc->mExit) {
Jason Samsa09f11d2009-06-04 17:58:03 -0700140 mDraw |= gIO->playCoreCommands(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700141
Jason Samsa09f11d2009-06-04 17:58:03 -0700142 if (!mDraw || !rsc->mRootScript.get()) {
143 usleep(10000);
Jason Samsd19f10d2009-05-22 14:03:28 -0700144 continue;
145 }
146
Jason Samsd19f10d2009-05-22 14:03:28 -0700147 if (rsc->mRootScript.get()) {
Jason Samsa09f11d2009-06-04 17:58:03 -0700148 mDraw = rsc->runRootScript();
149 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Samsd19f10d2009-05-22 14:03:28 -0700150 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700151 }
152
Jason Samsd19f10d2009-05-22 14:03:28 -0700153 glClearColor(0,0,0,0);
154 glClear(GL_COLOR_BUFFER_BIT);
155 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
156 eglTerminate(rsc->mDisplay);
Jason Samsd19f10d2009-05-22 14:03:28 -0700157 return NULL;
158}
159
160Context::Context(Device *dev, Surface *sur)
161{
Jason Samsd19f10d2009-05-22 14:03:28 -0700162 dev->addContext(this);
163 mDev = dev;
164 mRunning = false;
165 mExit = false;
166
167 mServerCommands.init(256);
168 mServerReturns.init(256);
169
170 // see comment in header
171 gCon = this;
172
Jason Sams8ad00102009-06-04 14:35:01 -0700173 int status;
174 pthread_attr_t threadAttr;
175
176 status = pthread_attr_init(&threadAttr);
177 if (status) {
178 LOGE("Failed to init thread attribute.");
179 return;
180 }
181
182 sched_param sparam;
183 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
184 pthread_attr_setschedparam(&threadAttr, &sparam);
185
Jason Sams3a833d82009-06-08 15:20:31 -0700186 LOGE("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700187 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700188 if (status) {
189 LOGE("Failed to start rs context thread.");
190 }
191
Jason Samsd19f10d2009-05-22 14:03:28 -0700192 mWndSurface = sur;
193 while(!mRunning) {
194 sleep(1);
195 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700196
Jason Sams8ad00102009-06-04 14:35:01 -0700197 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700198}
199
200Context::~Context()
201{
202 mExit = true;
203 void *res;
204
Jason Samsd19f10d2009-05-22 14:03:28 -0700205 int status = pthread_join(mThreadId, &res);
Jason Samsd19f10d2009-05-22 14:03:28 -0700206
207 if (mDev) {
208 mDev->removeContext(this);
209 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700210}
211
212void Context::swapBuffers()
213{
214 eglSwapBuffers(mDisplay, mSurface);
215}
216
217void rsContextSwap(RsContext vrsc)
218{
219 Context *rsc = static_cast<Context *>(vrsc);
220 rsc->swapBuffers();
221}
222
223void Context::setRootScript(Script *s)
224{
225 mRootScript.set(s);
226}
227
228void Context::setFragmentStore(ProgramFragmentStore *pfs)
229{
230 mFragmentStore.set(pfs);
231 pfs->setupGL();
232}
233
234void Context::setFragment(ProgramFragment *pf)
235{
236 mFragment.set(pf);
237 pf->setupGL();
238}
239
240void Context::setVertex(ProgramVertex *pv)
241{
242 mVertex.set(pv);
243 pv->setupGL();
244}
245
246///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700247//
Jason Samsd19f10d2009-05-22 14:03:28 -0700248
249namespace android {
250namespace renderscript {
251
252
253void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
254{
255 Script *s = static_cast<Script *>(vs);
256 rsc->setRootScript(s);
257}
258
259void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
260{
261 Sampler *s = static_cast<Sampler *>(vs);
262
263 if (slot > RS_MAX_SAMPLER_SLOT) {
264 LOGE("Invalid sampler slot");
265 return;
266 }
267
268 s->bindToContext(&rsc->mStateSampler, slot);
269}
270
271void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
272{
273 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
274 rsc->setFragmentStore(pfs);
275}
276
277void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
278{
279 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
280 rsc->setFragment(pf);
281}
282
283void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
284{
285 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
286 rsc->setVertex(pv);
287}
288
289
290
291}
292}
293
294
295RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
296{
297 Device * dev = static_cast<Device *>(vdev);
298 Context *rsc = new Context(dev, (Surface *)sur);
299 return rsc;
300}
301
302void rsContextDestroy(RsContext vrsc)
303{
304 Context * rsc = static_cast<Context *>(vrsc);
305 delete rsc;
306}
307