blob: 0f78df4e6ebf00b0d0def9d86568dcc9b29108d6 [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
Jason Sams1aa5a4e2009-06-22 17:15:15 -070022#include <GLES/gl.h>
23#include <GLES/glext.h>
24
Jason Sams326e0dd2009-05-22 14:03:28 -070025using namespace android;
26using namespace android::renderscript;
27
28Context * Context::gCon = NULL;
Jason Samse5769102009-06-19 16:03:18 -070029pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070030
31void Context::initEGL()
32{
33 mNumConfigs = -1;
34
35 EGLint s_configAttribs[] = {
36 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
37 EGL_RED_SIZE, 5,
38 EGL_GREEN_SIZE, 6,
39 EGL_BLUE_SIZE, 5,
40 EGL_DEPTH_SIZE, 16,
41 EGL_NONE
42 };
43
Jason Sams326e0dd2009-05-22 14:03:28 -070044 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -070045 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Sams326e0dd2009-05-22 14:03:28 -070046 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Sams326e0dd2009-05-22 14:03:28 -070047
48 if (mWndSurface) {
49 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
50 new EGLNativeWindowSurface(mWndSurface),
51 NULL);
52 } else {
53 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
54 android_createDisplaySurface(),
55 NULL);
56 }
57
Jason Sams326e0dd2009-05-22 14:03:28 -070058 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa44cb292009-06-04 17:58:03 -070059 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Sams326e0dd2009-05-22 14:03:28 -070060 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
61 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070062}
63
Jason Samsa0a1b6f2009-06-10 15:04:38 -070064bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070065{
66 ObjectBaseRef<ProgramFragment> frag(mFragment);
67 ObjectBaseRef<ProgramVertex> vtx(mVertex);
68 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
69
Jason Samsa0a1b6f2009-06-10 15:04:38 -070070 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -070071
Jason Samsa0a1b6f2009-06-10 15:04:38 -070072 mFragment.set(frag);
73 mVertex.set(vtx);
74 mFragmentStore.set(store);
Jason Sams10308932009-06-09 12:15:30 -070075 return true;
76
77}
78
79
Jason Samsa44cb292009-06-04 17:58:03 -070080bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070081{
Jason Sams10308932009-06-09 12:15:30 -070082 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070083
Jason Samsa44cb292009-06-04 17:58:03 -070084 glColor4f(1,1,1,1);
85 glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070086 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070087
Jason Sams326e0dd2009-05-22 14:03:28 -070088 glDepthMask(GL_TRUE);
89 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
90
Jason Sams928b7342009-06-08 18:50:13 -070091 glClearColor(mRootScript->mEnviroment.mClearColor[0],
92 mRootScript->mEnviroment.mClearColor[1],
93 mRootScript->mEnviroment.mClearColor[2],
94 mRootScript->mEnviroment.mClearColor[3]);
95 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -070096 glClear(GL_COLOR_BUFFER_BIT);
97 glClear(GL_DEPTH_BUFFER_BIT);
98
Jason Samsa0a1b6f2009-06-10 15:04:38 -070099 return runScript(mRootScript.get(), 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700100}
101
102void Context::setupCheck()
103{
104 if (mFragmentStore.get()) {
105 mFragmentStore->setupGL();
106 }
107 if (mFragment.get()) {
108 mFragment->setupGL();
109 }
110 if (mVertex.get()) {
111 mVertex->setupGL();
112 }
113
114}
115
116
117void * Context::threadProc(void *vrsc)
118{
119 Context *rsc = static_cast<Context *>(vrsc);
120
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700122 rsc->mServerCommands.init(128);
123 rsc->mServerReturns.init(128);
124
125 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700126
Jason Samse5769102009-06-19 16:03:18 -0700127 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
128 if (!tlsStruct) {
129 LOGE("Error allocating tls storage");
130 return NULL;
131 }
132 tlsStruct->mContext = rsc;
133 tlsStruct->mScript = NULL;
134 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
135 if (status) {
136 LOGE("pthread_setspecific %i", status);
137 }
138
Jason Sams8ce125b2009-06-17 16:52:59 -0700139 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
140 rsc->setVertex(NULL);
141 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
142 rsc->setFragment(NULL);
143 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
144 rsc->setFragmentStore(NULL);
145
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700147 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700148 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700149 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
150 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700151
Jason Sams732f1c02009-06-18 16:58:42 -0700152 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700153 mDraw = rsc->runRootScript();
154 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700155 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 }
157
Jason Sams326e0dd2009-05-22 14:03:28 -0700158 glClearColor(0,0,0,0);
159 glClear(GL_COLOR_BUFFER_BIT);
160 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
161 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700162 return NULL;
163}
164
165Context::Context(Device *dev, Surface *sur)
166{
Jason Sams326e0dd2009-05-22 14:03:28 -0700167 dev->addContext(this);
168 mDev = dev;
169 mRunning = false;
170 mExit = false;
171
172 mServerCommands.init(256);
173 mServerReturns.init(256);
174
175 // see comment in header
176 gCon = this;
177
Jason Samsa658e902009-06-04 14:35:01 -0700178 int status;
179 pthread_attr_t threadAttr;
180
Jason Samse5769102009-06-19 16:03:18 -0700181 status = pthread_key_create(&gThreadTLSKey, NULL);
182 if (status) {
183 LOGE("Failed to init thread tls key.");
184 return;
185 }
186
Jason Samsa658e902009-06-04 14:35:01 -0700187 status = pthread_attr_init(&threadAttr);
188 if (status) {
189 LOGE("Failed to init thread attribute.");
190 return;
191 }
192
193 sched_param sparam;
194 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
195 pthread_attr_setschedparam(&threadAttr, &sparam);
196
Jason Samsefb8de12009-06-08 15:20:31 -0700197 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700198 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700199 if (status) {
200 LOGE("Failed to start rs context thread.");
201 }
202
Jason Sams326e0dd2009-05-22 14:03:28 -0700203 mWndSurface = sur;
204 while(!mRunning) {
205 sleep(1);
206 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700207
Jason Samsa658e902009-06-04 14:35:01 -0700208 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700209}
210
211Context::~Context()
212{
213 mExit = true;
214 void *res;
215
Jason Sams326e0dd2009-05-22 14:03:28 -0700216 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700217
218 if (mDev) {
219 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700220 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700221 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700222}
223
224void Context::swapBuffers()
225{
226 eglSwapBuffers(mDisplay, mSurface);
227}
228
229void rsContextSwap(RsContext vrsc)
230{
231 Context *rsc = static_cast<Context *>(vrsc);
232 rsc->swapBuffers();
233}
234
235void Context::setRootScript(Script *s)
236{
237 mRootScript.set(s);
238}
239
240void Context::setFragmentStore(ProgramFragmentStore *pfs)
241{
Jason Sams8ce125b2009-06-17 16:52:59 -0700242 if (pfs == NULL) {
243 mFragmentStore.set(mStateFragmentStore.mDefault);
244 } else {
245 mFragmentStore.set(pfs);
246 }
247 mFragmentStore->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700248}
249
250void Context::setFragment(ProgramFragment *pf)
251{
Jason Sams8ce125b2009-06-17 16:52:59 -0700252 if (pf == NULL) {
253 mFragment.set(mStateFragment.mDefault);
254 } else {
255 mFragment.set(pf);
256 }
257 mFragment->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700258}
259
260void Context::setVertex(ProgramVertex *pv)
261{
Jason Sams8ce125b2009-06-17 16:52:59 -0700262 if (pv == NULL) {
263 mVertex.set(mStateVertex.mDefault);
264 } else {
265 mVertex.set(pv);
266 }
267 mVertex->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700268}
269
Jason Samsa4a54e42009-06-10 18:39:40 -0700270void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700271{
272 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700273 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700274 mNames.add(obj);
275}
276
277void Context::removeName(ObjectBase *obj)
278{
279 for(size_t ct=0; ct < mNames.size(); ct++) {
280 if (obj == mNames[ct]) {
281 mNames.removeAt(ct);
282 return;
283 }
284 }
285}
286
287ObjectBase * Context::lookupName(const char *name) const
288{
289 for(size_t ct=0; ct < mNames.size(); ct++) {
290 if (!strcmp(name, mNames[ct]->getName())) {
291 return mNames[ct];
292 }
293 }
294 return NULL;
295}
296
Jason Samsa4a54e42009-06-10 18:39:40 -0700297void Context::appendNameDefines(String8 *str) const
298{
299 char buf[256];
300 for (size_t ct=0; ct < mNames.size(); ct++) {
301 str->append("#define NAMED_");
302 str->append(mNames[ct]->getName());
303 str->append(" ");
304 sprintf(buf, "%i\n", (int)mNames[ct]);
305 str->append(buf);
306 }
307}
308
309
Jason Sams326e0dd2009-05-22 14:03:28 -0700310///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700311//
Jason Sams326e0dd2009-05-22 14:03:28 -0700312
313namespace android {
314namespace renderscript {
315
316
317void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
318{
319 Script *s = static_cast<Script *>(vs);
320 rsc->setRootScript(s);
321}
322
323void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
324{
325 Sampler *s = static_cast<Sampler *>(vs);
326
327 if (slot > RS_MAX_SAMPLER_SLOT) {
328 LOGE("Invalid sampler slot");
329 return;
330 }
331
332 s->bindToContext(&rsc->mStateSampler, slot);
333}
334
335void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
336{
337 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
338 rsc->setFragmentStore(pfs);
339}
340
341void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
342{
343 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
344 rsc->setFragment(pf);
345}
346
347void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
348{
349 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
350 rsc->setVertex(pv);
351}
352
Jason Samsa4a54e42009-06-10 18:39:40 -0700353void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700354{
355 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700356 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700357}
Jason Sams326e0dd2009-05-22 14:03:28 -0700358
359
360}
361}
362
363
364RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
365{
366 Device * dev = static_cast<Device *>(vdev);
367 Context *rsc = new Context(dev, (Surface *)sur);
368 return rsc;
369}
370
371void rsContextDestroy(RsContext vrsc)
372{
373 Context * rsc = static_cast<Context *>(vrsc);
374 delete rsc;
375}
376