blob: 266c455289800261358172879cd454181ffecfd0 [file] [log] [blame]
Jason Sams326e0dd2009-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"
Jason Samsa4a54e42009-06-10 18:39:40 -070020#include "utils/String8.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070021
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 Sams326e0dd2009-05-22 14:03:28 -070040 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -070041 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Sams326e0dd2009-05-22 14:03:28 -070042 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Sams326e0dd2009-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 Sams326e0dd2009-05-22 14:03:28 -070054 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa44cb292009-06-04 17:58:03 -070055 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Sams326e0dd2009-05-22 14:03:28 -070056 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
57 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070058}
59
Jason Samsa0a1b6f2009-06-10 15:04:38 -070060bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070061{
62 ObjectBaseRef<ProgramFragment> frag(mFragment);
63 ObjectBaseRef<ProgramVertex> vtx(mVertex);
64 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
65
Jason Samsa0a1b6f2009-06-10 15:04:38 -070066 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -070067
Jason Samsa0a1b6f2009-06-10 15:04:38 -070068 mFragment.set(frag);
69 mVertex.set(vtx);
70 mFragmentStore.set(store);
Jason Sams10308932009-06-09 12:15:30 -070071 return true;
72
73}
74
75
Jason Samsa44cb292009-06-04 17:58:03 -070076bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070077{
Jason Sams10308932009-06-09 12:15:30 -070078 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070079
Jason Samsa44cb292009-06-04 17:58:03 -070080 glColor4f(1,1,1,1);
81 glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070082 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070083
Jason Sams10308932009-06-09 12:15:30 -070084 if(mRootScript->mEnviroment.mIsOrtho) {
Jason Sams326e0dd2009-05-22 14:03:28 -070085 glMatrixMode(GL_PROJECTION);
86 glLoadIdentity();
Jason Sams10308932009-06-09 12:15:30 -070087 glOrthof(0, mWidth, mHeight, 0, 0, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -070088 glMatrixMode(GL_MODELVIEW);
89 } else {
Jason Sams10308932009-06-09 12:15:30 -070090 float aspectH = ((float)mWidth) / mHeight;
Jason Sams326e0dd2009-05-22 14:03:28 -070091 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 Sams928b7342009-06-08 18:50:13 -0700105 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 Sams326e0dd2009-05-22 14:03:28 -0700110 glClear(GL_COLOR_BUFFER_BIT);
111 glClear(GL_DEPTH_BUFFER_BIT);
112
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700113 return runScript(mRootScript.get(), 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700114}
115
116void 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
131void * Context::threadProc(void *vrsc)
132{
133 Context *rsc = static_cast<Context *>(vrsc);
134
Jason Sams326e0dd2009-05-22 14:03:28 -0700135 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700136 rsc->mServerCommands.init(128);
137 rsc->mServerReturns.init(128);
138
139 rsc->initEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700140 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700141 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700142 while (!rsc->mExit) {
Jason Samsa44cb292009-06-04 17:58:03 -0700143 mDraw |= gIO->playCoreCommands(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700144
Jason Samsa44cb292009-06-04 17:58:03 -0700145 if (!mDraw || !rsc->mRootScript.get()) {
146 usleep(10000);
Jason Sams326e0dd2009-05-22 14:03:28 -0700147 continue;
148 }
149
Jason Sams326e0dd2009-05-22 14:03:28 -0700150 if (rsc->mRootScript.get()) {
Jason Samsa44cb292009-06-04 17:58:03 -0700151 mDraw = rsc->runRootScript();
152 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700153 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700154 }
155
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 glClearColor(0,0,0,0);
157 glClear(GL_COLOR_BUFFER_BIT);
158 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
159 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700160 return NULL;
161}
162
163Context::Context(Device *dev, Surface *sur)
164{
Jason Sams326e0dd2009-05-22 14:03:28 -0700165 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 Samsa658e902009-06-04 14:35:01 -0700176 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 Samsefb8de12009-06-08 15:20:31 -0700189 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700190 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700191 if (status) {
192 LOGE("Failed to start rs context thread.");
193 }
194
Jason Sams326e0dd2009-05-22 14:03:28 -0700195 mWndSurface = sur;
196 while(!mRunning) {
197 sleep(1);
198 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700199
Jason Samsa658e902009-06-04 14:35:01 -0700200 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700201}
202
203Context::~Context()
204{
205 mExit = true;
206 void *res;
207
Jason Sams326e0dd2009-05-22 14:03:28 -0700208 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700209
210 if (mDev) {
211 mDev->removeContext(this);
212 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700213}
214
215void Context::swapBuffers()
216{
217 eglSwapBuffers(mDisplay, mSurface);
218}
219
220void rsContextSwap(RsContext vrsc)
221{
222 Context *rsc = static_cast<Context *>(vrsc);
223 rsc->swapBuffers();
224}
225
226void Context::setRootScript(Script *s)
227{
228 mRootScript.set(s);
229}
230
231void Context::setFragmentStore(ProgramFragmentStore *pfs)
232{
233 mFragmentStore.set(pfs);
234 pfs->setupGL();
235}
236
237void Context::setFragment(ProgramFragment *pf)
238{
239 mFragment.set(pf);
240 pf->setupGL();
241}
242
243void Context::setVertex(ProgramVertex *pv)
244{
245 mVertex.set(pv);
246 pv->setupGL();
247}
248
Jason Samsa4a54e42009-06-10 18:39:40 -0700249void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700250{
251 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700252 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700253 mNames.add(obj);
254}
255
256void 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
266ObjectBase * 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 Samsa4a54e42009-06-10 18:39:40 -0700276void 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 Sams326e0dd2009-05-22 14:03:28 -0700289///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700290//
Jason Sams326e0dd2009-05-22 14:03:28 -0700291
292namespace android {
293namespace renderscript {
294
295
296void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
297{
298 Script *s = static_cast<Script *>(vs);
299 rsc->setRootScript(s);
300}
301
302void 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
314void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
315{
316 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
317 rsc->setFragmentStore(pfs);
318}
319
320void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
321{
322 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
323 rsc->setFragment(pf);
324}
325
326void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
327{
328 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
329 rsc->setVertex(pv);
330}
331
Jason Samsa4a54e42009-06-10 18:39:40 -0700332void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700333{
334 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700335 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700336}
Jason Sams326e0dd2009-05-22 14:03:28 -0700337
338
339}
340}
341
342
343RsContext 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
350void rsContextDestroy(RsContext vrsc)
351{
352 Context * rsc = static_cast<Context *>(vrsc);
353 delete rsc;
354}
355