blob: c466f46a9fba00548d8dce0c6d1ede9c8f6ac3ef [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 Sams326e0dd2009-05-22 14:03:28 -0700122 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700123 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700124 while (!rsc->mExit) {
Jason Samsa44cb292009-06-04 17:58:03 -0700125 mDraw |= gIO->playCoreCommands(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700126
Jason Samsa44cb292009-06-04 17:58:03 -0700127 if (!mDraw || !rsc->mRootScript.get()) {
128 usleep(10000);
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 continue;
130 }
131
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 if (rsc->mRootScript.get()) {
Jason Samsa44cb292009-06-04 17:58:03 -0700133 mDraw = rsc->runRootScript();
134 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700135 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700136 }
137
Jason Sams326e0dd2009-05-22 14:03:28 -0700138 glClearColor(0,0,0,0);
139 glClear(GL_COLOR_BUFFER_BIT);
140 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
141 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700142 return NULL;
143}
144
145Context::Context(Device *dev, Surface *sur)
146{
Jason Sams326e0dd2009-05-22 14:03:28 -0700147 dev->addContext(this);
148 mDev = dev;
149 mRunning = false;
150 mExit = false;
151
152 mServerCommands.init(256);
153 mServerReturns.init(256);
154
155 // see comment in header
156 gCon = this;
157
Jason Samsa658e902009-06-04 14:35:01 -0700158 int status;
159 pthread_attr_t threadAttr;
160
161 status = pthread_attr_init(&threadAttr);
162 if (status) {
163 LOGE("Failed to init thread attribute.");
164 return;
165 }
166
167 sched_param sparam;
168 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
169 pthread_attr_setschedparam(&threadAttr, &sparam);
170
Jason Samsefb8de12009-06-08 15:20:31 -0700171 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700172 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700173 if (status) {
174 LOGE("Failed to start rs context thread.");
175 }
176
Jason Sams326e0dd2009-05-22 14:03:28 -0700177 mWndSurface = sur;
178 while(!mRunning) {
179 sleep(1);
180 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700181
Jason Samsa658e902009-06-04 14:35:01 -0700182 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700183}
184
185Context::~Context()
186{
187 mExit = true;
188 void *res;
189
Jason Sams326e0dd2009-05-22 14:03:28 -0700190 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700191
192 if (mDev) {
193 mDev->removeContext(this);
194 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700195}
196
197void Context::swapBuffers()
198{
199 eglSwapBuffers(mDisplay, mSurface);
200}
201
202void rsContextSwap(RsContext vrsc)
203{
204 Context *rsc = static_cast<Context *>(vrsc);
205 rsc->swapBuffers();
206}
207
208void Context::setRootScript(Script *s)
209{
210 mRootScript.set(s);
211}
212
213void Context::setFragmentStore(ProgramFragmentStore *pfs)
214{
215 mFragmentStore.set(pfs);
216 pfs->setupGL();
217}
218
219void Context::setFragment(ProgramFragment *pf)
220{
221 mFragment.set(pf);
222 pf->setupGL();
223}
224
225void Context::setVertex(ProgramVertex *pv)
226{
227 mVertex.set(pv);
228 pv->setupGL();
229}
230
Jason Samsa4a54e42009-06-10 18:39:40 -0700231void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700232{
233 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700234 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700235 mNames.add(obj);
236}
237
238void Context::removeName(ObjectBase *obj)
239{
240 for(size_t ct=0; ct < mNames.size(); ct++) {
241 if (obj == mNames[ct]) {
242 mNames.removeAt(ct);
243 return;
244 }
245 }
246}
247
248ObjectBase * Context::lookupName(const char *name) const
249{
250 for(size_t ct=0; ct < mNames.size(); ct++) {
251 if (!strcmp(name, mNames[ct]->getName())) {
252 return mNames[ct];
253 }
254 }
255 return NULL;
256}
257
Jason Samsa4a54e42009-06-10 18:39:40 -0700258void Context::appendNameDefines(String8 *str) const
259{
260 char buf[256];
261 for (size_t ct=0; ct < mNames.size(); ct++) {
262 str->append("#define NAMED_");
263 str->append(mNames[ct]->getName());
264 str->append(" ");
265 sprintf(buf, "%i\n", (int)mNames[ct]);
266 str->append(buf);
267 }
268}
269
270
Jason Sams326e0dd2009-05-22 14:03:28 -0700271///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700272//
Jason Sams326e0dd2009-05-22 14:03:28 -0700273
274namespace android {
275namespace renderscript {
276
277
278void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
279{
280 Script *s = static_cast<Script *>(vs);
281 rsc->setRootScript(s);
282}
283
284void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
285{
286 Sampler *s = static_cast<Sampler *>(vs);
287
288 if (slot > RS_MAX_SAMPLER_SLOT) {
289 LOGE("Invalid sampler slot");
290 return;
291 }
292
293 s->bindToContext(&rsc->mStateSampler, slot);
294}
295
296void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
297{
298 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
299 rsc->setFragmentStore(pfs);
300}
301
302void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
303{
304 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
305 rsc->setFragment(pf);
306}
307
308void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
309{
310 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
311 rsc->setVertex(pv);
312}
313
Jason Samsa4a54e42009-06-10 18:39:40 -0700314void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700315{
316 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700317 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700318}
Jason Sams326e0dd2009-05-22 14:03:28 -0700319
320
321}
322}
323
324
325RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
326{
327 Device * dev = static_cast<Device *>(vdev);
328 Context *rsc = new Context(dev, (Surface *)sur);
329 return rsc;
330}
331
332void rsContextDestroy(RsContext vrsc)
333{
334 Context * rsc = static_cast<Context *>(vrsc);
335 delete rsc;
336}
337