blob: ec4a30928d527a413e80d781d985ef1a83aeb8e2 [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
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);
Jason Samsa09f11d2009-06-04 17:58:03 -070060 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Samsd19f10d2009-05-22 14:03:28 -070061 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
62 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
63 LOGE("EGL 9");
64
65}
66
Jason Samsa09f11d2009-06-04 17:58:03 -070067bool Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -070068{
69 rsAssert(mRootScript->mIsRoot);
70
Jason Samsa09f11d2009-06-04 17:58:03 -070071 glColor4f(1,1,1,1);
72 glEnable(GL_LIGHT0);
Jason Samsd19f10d2009-05-22 14:03:28 -070073 glViewport(0, 0, 320, 480);
74 float aspectH = 480.f / 320.f;
75
76 if(mRootScript->mIsOrtho) {
77 glMatrixMode(GL_PROJECTION);
78 glLoadIdentity();
Jason Samsfe08d992009-05-27 14:45:32 -070079 glOrthof(0, 320, 480, 0, 0, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -070080 glMatrixMode(GL_MODELVIEW);
81 } else {
82 glMatrixMode(GL_PROJECTION);
83 glLoadIdentity();
84 glFrustumf(-1, 1, -aspectH, aspectH, 1, 100);
85 glRotatef(-90, 0,0,1);
86 glTranslatef(0, 0, -3);
87 glMatrixMode(GL_MODELVIEW);
88 }
89
90 glMatrixMode(GL_MODELVIEW);
91 glLoadIdentity();
92
93 glDepthMask(GL_TRUE);
94 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
95
Jason Samsa09f11d2009-06-04 17:58:03 -070096 glClearColor(mRootScript->mClearColor[0],
Jason Samsd19f10d2009-05-22 14:03:28 -070097 mRootScript->mClearColor[1],
98 mRootScript->mClearColor[2],
99 mRootScript->mClearColor[3]);
100 glClearDepthf(mRootScript->mClearDepth);
101 glClear(GL_COLOR_BUFFER_BIT);
102 glClear(GL_DEPTH_BUFFER_BIT);
103
Jason Samsa09f11d2009-06-04 17:58:03 -0700104 return mRootScript->run(this, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -0700105}
106
107void Context::setupCheck()
108{
109 if (mFragmentStore.get()) {
110 mFragmentStore->setupGL();
111 }
112 if (mFragment.get()) {
113 mFragment->setupGL();
114 }
115 if (mVertex.get()) {
116 mVertex->setupGL();
117 }
118
119}
120
121
122void * Context::threadProc(void *vrsc)
123{
124 Context *rsc = static_cast<Context *>(vrsc);
125
126 LOGE("TP 1");
127 gIO = new ThreadIO();
128
129 rsc->mServerCommands.init(128);
130 rsc->mServerReturns.init(128);
131
132 rsc->initEGL();
133
134 LOGE("TP 2");
135
136 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700137 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700138 while (!rsc->mExit) {
Jason Samsa09f11d2009-06-04 17:58:03 -0700139 mDraw |= gIO->playCoreCommands(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700140
Jason Samsa09f11d2009-06-04 17:58:03 -0700141 if (!mDraw || !rsc->mRootScript.get()) {
142 usleep(10000);
Jason Samsd19f10d2009-05-22 14:03:28 -0700143 continue;
144 }
145
Jason Samsd19f10d2009-05-22 14:03:28 -0700146 if (rsc->mRootScript.get()) {
Jason Samsa09f11d2009-06-04 17:58:03 -0700147 mDraw = rsc->runRootScript();
148 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Samsd19f10d2009-05-22 14:03:28 -0700149 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700150 }
151
152 LOGE("TP 6");
153 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{
162 LOGE("CC 1");
163 dev->addContext(this);
164 mDev = dev;
165 mRunning = false;
166 mExit = false;
167
168 mServerCommands.init(256);
169 mServerReturns.init(256);
170
171 // see comment in header
172 gCon = this;
173
174 LOGE("CC 2");
Jason Sams8ad00102009-06-04 14:35:01 -0700175 int status;
176 pthread_attr_t threadAttr;
177
178 status = pthread_attr_init(&threadAttr);
179 if (status) {
180 LOGE("Failed to init thread attribute.");
181 return;
182 }
183
184 sched_param sparam;
185 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
186 pthread_attr_setschedparam(&threadAttr, &sparam);
187
188 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700189 if (status) {
190 LOGE("Failed to start rs context thread.");
191 }
192
193 LOGE("CC 3");
194 mWndSurface = sur;
195 while(!mRunning) {
196 sleep(1);
197 }
198 LOGE("CC 4");
199
Jason Sams8ad00102009-06-04 14:35:01 -0700200 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700201}
202
203Context::~Context()
204{
205 mExit = true;
206 void *res;
207
208 LOGE("DES 1");
209 int status = pthread_join(mThreadId, &res);
210 LOGE("DES 2");
211
212 if (mDev) {
213 mDev->removeContext(this);
214 }
215 LOGE("DES 3");
216}
217
218void Context::swapBuffers()
219{
220 eglSwapBuffers(mDisplay, mSurface);
221}
222
223void rsContextSwap(RsContext vrsc)
224{
225 Context *rsc = static_cast<Context *>(vrsc);
226 rsc->swapBuffers();
227}
228
229void Context::setRootScript(Script *s)
230{
231 mRootScript.set(s);
232}
233
234void Context::setFragmentStore(ProgramFragmentStore *pfs)
235{
236 mFragmentStore.set(pfs);
237 pfs->setupGL();
238}
239
240void Context::setFragment(ProgramFragment *pf)
241{
242 mFragment.set(pf);
243 pf->setupGL();
244}
245
246void Context::setVertex(ProgramVertex *pv)
247{
248 mVertex.set(pv);
249 pv->setupGL();
250}
251
252///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700253//
Jason Samsd19f10d2009-05-22 14:03:28 -0700254
255namespace android {
256namespace renderscript {
257
258
259void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
260{
261 Script *s = static_cast<Script *>(vs);
262 rsc->setRootScript(s);
263}
264
265void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
266{
267 Sampler *s = static_cast<Sampler *>(vs);
268
269 if (slot > RS_MAX_SAMPLER_SLOT) {
270 LOGE("Invalid sampler slot");
271 return;
272 }
273
274 s->bindToContext(&rsc->mStateSampler, slot);
275}
276
277void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
278{
279 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
280 rsc->setFragmentStore(pfs);
281}
282
283void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
284{
285 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
286 rsc->setFragment(pf);
287}
288
289void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
290{
291 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
292 rsc->setVertex(pv);
293}
294
295
296
297}
298}
299
300
301RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
302{
303 Device * dev = static_cast<Device *>(vdev);
304 Context *rsc = new Context(dev, (Surface *)sur);
305 return rsc;
306}
307
308void rsContextDestroy(RsContext vrsc)
309{
310 Context * rsc = static_cast<Context *>(vrsc);
311 delete rsc;
312}
313