blob: 7248ecc1a4e4ce361f847a9cd91e4a391ba58101 [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 Samsa44cb292009-06-04 17:58:03 -0700133 mDraw |= gIO->playCoreCommands(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700134
Jason Samsa44cb292009-06-04 17:58:03 -0700135 if (!mDraw || !rsc->mRootScript.get()) {
136 usleep(10000);
Jason Sams326e0dd2009-05-22 14:03:28 -0700137 continue;
138 }
139
Jason Sams326e0dd2009-05-22 14:03:28 -0700140 if (rsc->mRootScript.get()) {
Jason Samsa44cb292009-06-04 17:58:03 -0700141 mDraw = rsc->runRootScript();
142 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700143 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700144 }
145
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 glClearColor(0,0,0,0);
147 glClear(GL_COLOR_BUFFER_BIT);
148 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
149 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700150 return NULL;
151}
152
153Context::Context(Device *dev, Surface *sur)
154{
Jason Sams326e0dd2009-05-22 14:03:28 -0700155 dev->addContext(this);
156 mDev = dev;
157 mRunning = false;
158 mExit = false;
159
160 mServerCommands.init(256);
161 mServerReturns.init(256);
162
163 // see comment in header
164 gCon = this;
165
Jason Samsa658e902009-06-04 14:35:01 -0700166 int status;
167 pthread_attr_t threadAttr;
168
169 status = pthread_attr_init(&threadAttr);
170 if (status) {
171 LOGE("Failed to init thread attribute.");
172 return;
173 }
174
175 sched_param sparam;
176 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
177 pthread_attr_setschedparam(&threadAttr, &sparam);
178
Jason Samsefb8de12009-06-08 15:20:31 -0700179 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700180 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700181 if (status) {
182 LOGE("Failed to start rs context thread.");
183 }
184
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 mWndSurface = sur;
186 while(!mRunning) {
187 sleep(1);
188 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700189
Jason Samsa658e902009-06-04 14:35:01 -0700190 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700191}
192
193Context::~Context()
194{
195 mExit = true;
196 void *res;
197
Jason Sams326e0dd2009-05-22 14:03:28 -0700198 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700199
200 if (mDev) {
201 mDev->removeContext(this);
202 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700203}
204
205void Context::swapBuffers()
206{
207 eglSwapBuffers(mDisplay, mSurface);
208}
209
210void rsContextSwap(RsContext vrsc)
211{
212 Context *rsc = static_cast<Context *>(vrsc);
213 rsc->swapBuffers();
214}
215
216void Context::setRootScript(Script *s)
217{
218 mRootScript.set(s);
219}
220
221void Context::setFragmentStore(ProgramFragmentStore *pfs)
222{
Jason Sams8ce125b2009-06-17 16:52:59 -0700223 if (pfs == NULL) {
224 mFragmentStore.set(mStateFragmentStore.mDefault);
225 } else {
226 mFragmentStore.set(pfs);
227 }
228 mFragmentStore->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700229}
230
231void Context::setFragment(ProgramFragment *pf)
232{
Jason Sams8ce125b2009-06-17 16:52:59 -0700233 if (pf == NULL) {
234 mFragment.set(mStateFragment.mDefault);
235 } else {
236 mFragment.set(pf);
237 }
238 mFragment->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700239}
240
241void Context::setVertex(ProgramVertex *pv)
242{
Jason Sams8ce125b2009-06-17 16:52:59 -0700243 if (pv == NULL) {
244 mVertex.set(mStateVertex.mDefault);
245 } else {
246 mVertex.set(pv);
247 }
248 mVertex->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700249}
250
Jason Samsa4a54e42009-06-10 18:39:40 -0700251void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700252{
253 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700254 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700255 mNames.add(obj);
256}
257
258void Context::removeName(ObjectBase *obj)
259{
260 for(size_t ct=0; ct < mNames.size(); ct++) {
261 if (obj == mNames[ct]) {
262 mNames.removeAt(ct);
263 return;
264 }
265 }
266}
267
268ObjectBase * Context::lookupName(const char *name) const
269{
270 for(size_t ct=0; ct < mNames.size(); ct++) {
271 if (!strcmp(name, mNames[ct]->getName())) {
272 return mNames[ct];
273 }
274 }
275 return NULL;
276}
277
Jason Samsa4a54e42009-06-10 18:39:40 -0700278void Context::appendNameDefines(String8 *str) const
279{
280 char buf[256];
281 for (size_t ct=0; ct < mNames.size(); ct++) {
282 str->append("#define NAMED_");
283 str->append(mNames[ct]->getName());
284 str->append(" ");
285 sprintf(buf, "%i\n", (int)mNames[ct]);
286 str->append(buf);
287 }
288}
289
290
Jason Sams326e0dd2009-05-22 14:03:28 -0700291///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700292//
Jason Sams326e0dd2009-05-22 14:03:28 -0700293
294namespace android {
295namespace renderscript {
296
297
298void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
299{
300 Script *s = static_cast<Script *>(vs);
301 rsc->setRootScript(s);
302}
303
304void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
305{
306 Sampler *s = static_cast<Sampler *>(vs);
307
308 if (slot > RS_MAX_SAMPLER_SLOT) {
309 LOGE("Invalid sampler slot");
310 return;
311 }
312
313 s->bindToContext(&rsc->mStateSampler, slot);
314}
315
316void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
317{
318 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
319 rsc->setFragmentStore(pfs);
320}
321
322void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
323{
324 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
325 rsc->setFragment(pf);
326}
327
328void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
329{
330 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
331 rsc->setVertex(pv);
332}
333
Jason Samsa4a54e42009-06-10 18:39:40 -0700334void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700335{
336 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700337 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700338}
Jason Sams326e0dd2009-05-22 14:03:28 -0700339
340
341}
342}
343
344
345RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
346{
347 Device * dev = static_cast<Device *>(vdev);
348 Context *rsc = new Context(dev, (Surface *)sur);
349 return rsc;
350}
351
352void rsContextDestroy(RsContext vrsc)
353{
354 Context * rsc = static_cast<Context *>(vrsc);
355 delete rsc;
356}
357