blob: 2a5f65d7fc879518e7a62e422285f920b808dd8c [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 Sams326e0dd2009-05-22 14:03:28 -070084 glDepthMask(GL_TRUE);
85 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
86
Jason Sams928b7342009-06-08 18:50:13 -070087 glClearColor(mRootScript->mEnviroment.mClearColor[0],
88 mRootScript->mEnviroment.mClearColor[1],
89 mRootScript->mEnviroment.mClearColor[2],
90 mRootScript->mEnviroment.mClearColor[3]);
91 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -070092 glClear(GL_COLOR_BUFFER_BIT);
93 glClear(GL_DEPTH_BUFFER_BIT);
94
Jason Samsa0a1b6f2009-06-10 15:04:38 -070095 return runScript(mRootScript.get(), 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070096}
97
98void Context::setupCheck()
99{
100 if (mFragmentStore.get()) {
101 mFragmentStore->setupGL();
102 }
103 if (mFragment.get()) {
104 mFragment->setupGL();
105 }
106 if (mVertex.get()) {
107 mVertex->setupGL();
108 }
109
110}
111
112
113void * Context::threadProc(void *vrsc)
114{
115 Context *rsc = static_cast<Context *>(vrsc);
116
Jason Sams326e0dd2009-05-22 14:03:28 -0700117 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700118 rsc->mServerCommands.init(128);
119 rsc->mServerReturns.init(128);
120
121 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700122
123 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
124 rsc->setVertex(NULL);
125 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
126 rsc->setFragment(NULL);
127 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
128 rsc->setFragmentStore(NULL);
129
Jason Sams326e0dd2009-05-22 14:03:28 -0700130 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700131 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700133 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
134 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700135
Jason Sams732f1c02009-06-18 16:58:42 -0700136 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700137 mDraw = rsc->runRootScript();
138 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700139 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700140 }
141
Jason Sams326e0dd2009-05-22 14:03:28 -0700142 glClearColor(0,0,0,0);
143 glClear(GL_COLOR_BUFFER_BIT);
144 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
145 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 return NULL;
147}
148
149Context::Context(Device *dev, Surface *sur)
150{
Jason Sams326e0dd2009-05-22 14:03:28 -0700151 dev->addContext(this);
152 mDev = dev;
153 mRunning = false;
154 mExit = false;
155
156 mServerCommands.init(256);
157 mServerReturns.init(256);
158
159 // see comment in header
160 gCon = this;
161
Jason Samsa658e902009-06-04 14:35:01 -0700162 int status;
163 pthread_attr_t threadAttr;
164
165 status = pthread_attr_init(&threadAttr);
166 if (status) {
167 LOGE("Failed to init thread attribute.");
168 return;
169 }
170
171 sched_param sparam;
172 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
173 pthread_attr_setschedparam(&threadAttr, &sparam);
174
Jason Samsefb8de12009-06-08 15:20:31 -0700175 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700176 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700177 if (status) {
178 LOGE("Failed to start rs context thread.");
179 }
180
Jason Sams326e0dd2009-05-22 14:03:28 -0700181 mWndSurface = sur;
182 while(!mRunning) {
183 sleep(1);
184 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700185
Jason Samsa658e902009-06-04 14:35:01 -0700186 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700187}
188
189Context::~Context()
190{
191 mExit = true;
192 void *res;
193
Jason Sams326e0dd2009-05-22 14:03:28 -0700194 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700195
196 if (mDev) {
197 mDev->removeContext(this);
198 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700199}
200
201void Context::swapBuffers()
202{
203 eglSwapBuffers(mDisplay, mSurface);
204}
205
206void rsContextSwap(RsContext vrsc)
207{
208 Context *rsc = static_cast<Context *>(vrsc);
209 rsc->swapBuffers();
210}
211
212void Context::setRootScript(Script *s)
213{
214 mRootScript.set(s);
215}
216
217void Context::setFragmentStore(ProgramFragmentStore *pfs)
218{
Jason Sams8ce125b2009-06-17 16:52:59 -0700219 if (pfs == NULL) {
220 mFragmentStore.set(mStateFragmentStore.mDefault);
221 } else {
222 mFragmentStore.set(pfs);
223 }
224 mFragmentStore->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700225}
226
227void Context::setFragment(ProgramFragment *pf)
228{
Jason Sams8ce125b2009-06-17 16:52:59 -0700229 if (pf == NULL) {
230 mFragment.set(mStateFragment.mDefault);
231 } else {
232 mFragment.set(pf);
233 }
234 mFragment->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700235}
236
237void Context::setVertex(ProgramVertex *pv)
238{
Jason Sams8ce125b2009-06-17 16:52:59 -0700239 if (pv == NULL) {
240 mVertex.set(mStateVertex.mDefault);
241 } else {
242 mVertex.set(pv);
243 }
244 mVertex->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700245}
246
Jason Samsa4a54e42009-06-10 18:39:40 -0700247void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700248{
249 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700250 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700251 mNames.add(obj);
252}
253
254void Context::removeName(ObjectBase *obj)
255{
256 for(size_t ct=0; ct < mNames.size(); ct++) {
257 if (obj == mNames[ct]) {
258 mNames.removeAt(ct);
259 return;
260 }
261 }
262}
263
264ObjectBase * Context::lookupName(const char *name) const
265{
266 for(size_t ct=0; ct < mNames.size(); ct++) {
267 if (!strcmp(name, mNames[ct]->getName())) {
268 return mNames[ct];
269 }
270 }
271 return NULL;
272}
273
Jason Samsa4a54e42009-06-10 18:39:40 -0700274void Context::appendNameDefines(String8 *str) const
275{
276 char buf[256];
277 for (size_t ct=0; ct < mNames.size(); ct++) {
278 str->append("#define NAMED_");
279 str->append(mNames[ct]->getName());
280 str->append(" ");
281 sprintf(buf, "%i\n", (int)mNames[ct]);
282 str->append(buf);
283 }
284}
285
286
Jason Sams326e0dd2009-05-22 14:03:28 -0700287///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700288//
Jason Sams326e0dd2009-05-22 14:03:28 -0700289
290namespace android {
291namespace renderscript {
292
293
294void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
295{
296 Script *s = static_cast<Script *>(vs);
297 rsc->setRootScript(s);
298}
299
300void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
301{
302 Sampler *s = static_cast<Sampler *>(vs);
303
304 if (slot > RS_MAX_SAMPLER_SLOT) {
305 LOGE("Invalid sampler slot");
306 return;
307 }
308
309 s->bindToContext(&rsc->mStateSampler, slot);
310}
311
312void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
313{
314 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
315 rsc->setFragmentStore(pfs);
316}
317
318void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
319{
320 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
321 rsc->setFragment(pf);
322}
323
324void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
325{
326 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
327 rsc->setVertex(pv);
328}
329
Jason Samsa4a54e42009-06-10 18:39:40 -0700330void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700331{
332 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700333 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700334}
Jason Sams326e0dd2009-05-22 14:03:28 -0700335
336
337}
338}
339
340
341RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
342{
343 Device * dev = static_cast<Device *>(vdev);
344 Context *rsc = new Context(dev, (Surface *)sur);
345 return rsc;
346}
347
348void rsContextDestroy(RsContext vrsc)
349{
350 Context * rsc = static_cast<Context *>(vrsc);
351 delete rsc;
352}
353