blob: 455c18dd940c514136ee1144eaf3e6cf9850ea1a [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"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070021#include <ui/FramebufferNativeWindow.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070022
23using namespace android;
24using namespace android::renderscript;
25
26Context * Context::gCon = NULL;
Jason Samse5769102009-06-19 16:03:18 -070027pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070028
29void Context::initEGL()
30{
31 mNumConfigs = -1;
32
33 EGLint s_configAttribs[] = {
34 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
35 EGL_RED_SIZE, 5,
36 EGL_GREEN_SIZE, 6,
37 EGL_BLUE_SIZE, 5,
38 EGL_DEPTH_SIZE, 16,
39 EGL_NONE
40 };
41
Jason Sams326e0dd2009-05-22 14:03:28 -070042 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -070043 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Sams326e0dd2009-05-22 14:03:28 -070044 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Sams326e0dd2009-05-22 14:03:28 -070045
46 if (mWndSurface) {
Mathias Agopian5ae678f2009-06-22 18:01:09 -070047 mSurface = eglCreateWindowSurface(mDisplay, mConfig, mWndSurface,
Jason Sams326e0dd2009-05-22 14:03:28 -070048 NULL);
49 } else {
50 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
51 android_createDisplaySurface(),
52 NULL);
53 }
54
Jason Sams326e0dd2009-05-22 14:03:28 -070055 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa44cb292009-06-04 17:58:03 -070056 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Sams326e0dd2009-05-22 14:03:28 -070057 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
58 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070059}
60
Jason Samsa0a1b6f2009-06-10 15:04:38 -070061bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070062{
63 ObjectBaseRef<ProgramFragment> frag(mFragment);
64 ObjectBaseRef<ProgramVertex> vtx(mVertex);
65 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
66
Jason Samsa0a1b6f2009-06-10 15:04:38 -070067 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -070068
Jason Samsa0a1b6f2009-06-10 15:04:38 -070069 mFragment.set(frag);
70 mVertex.set(vtx);
71 mFragmentStore.set(store);
Jason Sams10308932009-06-09 12:15:30 -070072 return true;
73
74}
75
76
Jason Samsa44cb292009-06-04 17:58:03 -070077bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070078{
Jason Sams10308932009-06-09 12:15:30 -070079 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070080
Jason Samsa44cb292009-06-04 17:58:03 -070081 glColor4f(1,1,1,1);
82 glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070083 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070084
Jason Sams326e0dd2009-05-22 14:03:28 -070085 glDepthMask(GL_TRUE);
86 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
87
Jason Sams928b7342009-06-08 18:50:13 -070088 glClearColor(mRootScript->mEnviroment.mClearColor[0],
89 mRootScript->mEnviroment.mClearColor[1],
90 mRootScript->mEnviroment.mClearColor[2],
91 mRootScript->mEnviroment.mClearColor[3]);
92 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -070093 glClear(GL_COLOR_BUFFER_BIT);
94 glClear(GL_DEPTH_BUFFER_BIT);
95
Jason Samsa0a1b6f2009-06-10 15:04:38 -070096 return runScript(mRootScript.get(), 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070097}
98
99void Context::setupCheck()
100{
101 if (mFragmentStore.get()) {
102 mFragmentStore->setupGL();
103 }
104 if (mFragment.get()) {
105 mFragment->setupGL();
106 }
107 if (mVertex.get()) {
108 mVertex->setupGL();
109 }
110
111}
112
113
114void * Context::threadProc(void *vrsc)
115{
116 Context *rsc = static_cast<Context *>(vrsc);
117
Jason Sams326e0dd2009-05-22 14:03:28 -0700118 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700119 rsc->mServerCommands.init(128);
120 rsc->mServerReturns.init(128);
121
122 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700123
Jason Samse5769102009-06-19 16:03:18 -0700124 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
125 if (!tlsStruct) {
126 LOGE("Error allocating tls storage");
127 return NULL;
128 }
129 tlsStruct->mContext = rsc;
130 tlsStruct->mScript = NULL;
131 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
132 if (status) {
133 LOGE("pthread_setspecific %i", status);
134 }
135
Jason Sams8ce125b2009-06-17 16:52:59 -0700136 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
137 rsc->setVertex(NULL);
138 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
139 rsc->setFragment(NULL);
140 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
141 rsc->setFragmentStore(NULL);
142
Jason Sams326e0dd2009-05-22 14:03:28 -0700143 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700144 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700145 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700146 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
147 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700148
Jason Sams732f1c02009-06-18 16:58:42 -0700149 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700150 mDraw = rsc->runRootScript();
151 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700152 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700153 }
154
Jason Sams326e0dd2009-05-22 14:03:28 -0700155 glClearColor(0,0,0,0);
156 glClear(GL_COLOR_BUFFER_BIT);
157 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
158 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 return NULL;
160}
161
162Context::Context(Device *dev, Surface *sur)
163{
Jason Sams326e0dd2009-05-22 14:03:28 -0700164 dev->addContext(this);
165 mDev = dev;
166 mRunning = false;
167 mExit = false;
168
169 mServerCommands.init(256);
170 mServerReturns.init(256);
171
172 // see comment in header
173 gCon = this;
174
Jason Samsa658e902009-06-04 14:35:01 -0700175 int status;
176 pthread_attr_t threadAttr;
177
Jason Samse5769102009-06-19 16:03:18 -0700178 status = pthread_key_create(&gThreadTLSKey, NULL);
179 if (status) {
180 LOGE("Failed to init thread tls key.");
181 return;
182 }
183
Jason Samsa658e902009-06-04 14:35:01 -0700184 status = pthread_attr_init(&threadAttr);
185 if (status) {
186 LOGE("Failed to init thread attribute.");
187 return;
188 }
189
190 sched_param sparam;
191 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
192 pthread_attr_setschedparam(&threadAttr, &sparam);
193
Jason Samsefb8de12009-06-08 15:20:31 -0700194 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700195 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700196 if (status) {
197 LOGE("Failed to start rs context thread.");
198 }
199
Jason Sams326e0dd2009-05-22 14:03:28 -0700200 mWndSurface = sur;
201 while(!mRunning) {
202 sleep(1);
203 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700204
Jason Samsa658e902009-06-04 14:35:01 -0700205 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700206}
207
208Context::~Context()
209{
210 mExit = true;
211 void *res;
212
Jason Sams326e0dd2009-05-22 14:03:28 -0700213 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700214
215 if (mDev) {
216 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700217 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700218 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700219}
220
221void Context::swapBuffers()
222{
223 eglSwapBuffers(mDisplay, mSurface);
224}
225
226void rsContextSwap(RsContext vrsc)
227{
228 Context *rsc = static_cast<Context *>(vrsc);
229 rsc->swapBuffers();
230}
231
232void Context::setRootScript(Script *s)
233{
234 mRootScript.set(s);
235}
236
237void Context::setFragmentStore(ProgramFragmentStore *pfs)
238{
Jason Sams8ce125b2009-06-17 16:52:59 -0700239 if (pfs == NULL) {
240 mFragmentStore.set(mStateFragmentStore.mDefault);
241 } else {
242 mFragmentStore.set(pfs);
243 }
244 mFragmentStore->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700245}
246
247void Context::setFragment(ProgramFragment *pf)
248{
Jason Sams8ce125b2009-06-17 16:52:59 -0700249 if (pf == NULL) {
250 mFragment.set(mStateFragment.mDefault);
251 } else {
252 mFragment.set(pf);
253 }
254 mFragment->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700255}
256
257void Context::setVertex(ProgramVertex *pv)
258{
Jason Sams8ce125b2009-06-17 16:52:59 -0700259 if (pv == NULL) {
260 mVertex.set(mStateVertex.mDefault);
261 } else {
262 mVertex.set(pv);
263 }
264 mVertex->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700265}
266
Jason Samsa4a54e42009-06-10 18:39:40 -0700267void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700268{
269 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700270 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700271 mNames.add(obj);
272}
273
274void Context::removeName(ObjectBase *obj)
275{
276 for(size_t ct=0; ct < mNames.size(); ct++) {
277 if (obj == mNames[ct]) {
278 mNames.removeAt(ct);
279 return;
280 }
281 }
282}
283
284ObjectBase * Context::lookupName(const char *name) const
285{
286 for(size_t ct=0; ct < mNames.size(); ct++) {
287 if (!strcmp(name, mNames[ct]->getName())) {
288 return mNames[ct];
289 }
290 }
291 return NULL;
292}
293
Jason Samsa4a54e42009-06-10 18:39:40 -0700294void Context::appendNameDefines(String8 *str) const
295{
296 char buf[256];
297 for (size_t ct=0; ct < mNames.size(); ct++) {
298 str->append("#define NAMED_");
299 str->append(mNames[ct]->getName());
300 str->append(" ");
301 sprintf(buf, "%i\n", (int)mNames[ct]);
302 str->append(buf);
303 }
304}
305
306
Jason Sams326e0dd2009-05-22 14:03:28 -0700307///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700308//
Jason Sams326e0dd2009-05-22 14:03:28 -0700309
310namespace android {
311namespace renderscript {
312
313
314void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
315{
316 Script *s = static_cast<Script *>(vs);
317 rsc->setRootScript(s);
318}
319
320void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
321{
322 Sampler *s = static_cast<Sampler *>(vs);
323
324 if (slot > RS_MAX_SAMPLER_SLOT) {
325 LOGE("Invalid sampler slot");
326 return;
327 }
328
329 s->bindToContext(&rsc->mStateSampler, slot);
330}
331
332void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
333{
334 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
335 rsc->setFragmentStore(pfs);
336}
337
338void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
339{
340 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
341 rsc->setFragment(pf);
342}
343
344void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
345{
346 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
347 rsc->setVertex(pv);
348}
349
Jason Samsa4a54e42009-06-10 18:39:40 -0700350void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700351{
352 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700353 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700354}
Jason Sams326e0dd2009-05-22 14:03:28 -0700355
356
357}
358}
359
360
361RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
362{
363 Device * dev = static_cast<Device *>(vdev);
364 Context *rsc = new Context(dev, (Surface *)sur);
365 return rsc;
366}
367
368void rsContextDestroy(RsContext vrsc)
369{
370 Context * rsc = static_cast<Context *>(vrsc);
371 delete rsc;
372}
373