blob: 78b8bf89e9e8d31b92be452a7838359534f4d13b [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
Jason Sams1aa5a4e2009-06-22 17:15:15 -070023#include <GLES/gl.h>
24#include <GLES/glext.h>
25
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
29Context * Context::gCon = NULL;
Jason Samse5769102009-06-19 16:03:18 -070030pthread_key_t Context::gThreadTLSKey = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070031
32void Context::initEGL()
33{
34 mNumConfigs = -1;
35
36 EGLint s_configAttribs[] = {
37 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Jason Samsb80dfa72009-07-10 17:32:40 -070038#if 1
39 EGL_RED_SIZE, 8,
40 EGL_GREEN_SIZE, 8,
41 EGL_BLUE_SIZE, 8,
42 EGL_ALPHA_SIZE, 8,
43#else
Jason Sams326e0dd2009-05-22 14:03:28 -070044 EGL_RED_SIZE, 5,
45 EGL_GREEN_SIZE, 6,
46 EGL_BLUE_SIZE, 5,
Jason Samsb80dfa72009-07-10 17:32:40 -070047#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070048 EGL_DEPTH_SIZE, 16,
49 EGL_NONE
50 };
51
Jason Sams326e0dd2009-05-22 14:03:28 -070052 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -070053 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Sams326e0dd2009-05-22 14:03:28 -070054 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Sams326e0dd2009-05-22 14:03:28 -070055
56 if (mWndSurface) {
Mathias Agopian5ae678f2009-06-22 18:01:09 -070057 mSurface = eglCreateWindowSurface(mDisplay, mConfig, mWndSurface,
Jason Sams326e0dd2009-05-22 14:03:28 -070058 NULL);
59 } else {
60 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
61 android_createDisplaySurface(),
62 NULL);
63 }
64
Jason Sams326e0dd2009-05-22 14:03:28 -070065 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa44cb292009-06-04 17:58:03 -070066 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Sams326e0dd2009-05-22 14:03:28 -070067 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
68 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070069}
70
Jason Samsa0a1b6f2009-06-10 15:04:38 -070071bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070072{
73 ObjectBaseRef<ProgramFragment> frag(mFragment);
74 ObjectBaseRef<ProgramVertex> vtx(mVertex);
75 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
76
Jason Samsa0a1b6f2009-06-10 15:04:38 -070077 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -070078
Jason Samsa0a1b6f2009-06-10 15:04:38 -070079 mFragment.set(frag);
80 mVertex.set(vtx);
81 mFragmentStore.set(store);
Jason Sams10308932009-06-09 12:15:30 -070082 return true;
83
84}
85
86
Jason Samsa44cb292009-06-04 17:58:03 -070087bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070088{
Jason Sams10308932009-06-09 12:15:30 -070089 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070090
Jason Samsa44cb292009-06-04 17:58:03 -070091 glColor4f(1,1,1,1);
92 glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070093 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070094
Jason Sams326e0dd2009-05-22 14:03:28 -070095 glDepthMask(GL_TRUE);
96 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
97
Jason Sams928b7342009-06-08 18:50:13 -070098 glClearColor(mRootScript->mEnviroment.mClearColor[0],
99 mRootScript->mEnviroment.mClearColor[1],
100 mRootScript->mEnviroment.mClearColor[2],
101 mRootScript->mEnviroment.mClearColor[3]);
102 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700103 glClear(GL_COLOR_BUFFER_BIT);
104 glClear(GL_DEPTH_BUFFER_BIT);
105
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700106 return runScript(mRootScript.get(), 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700107}
108
109void Context::setupCheck()
110{
111 if (mFragmentStore.get()) {
112 mFragmentStore->setupGL();
113 }
114 if (mFragment.get()) {
115 mFragment->setupGL();
116 }
117 if (mVertex.get()) {
118 mVertex->setupGL();
119 }
120
121}
122
123
124void * Context::threadProc(void *vrsc)
125{
126 Context *rsc = static_cast<Context *>(vrsc);
127
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700130
Jason Samse5769102009-06-19 16:03:18 -0700131 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
132 if (!tlsStruct) {
133 LOGE("Error allocating tls storage");
134 return NULL;
135 }
136 tlsStruct->mContext = rsc;
137 tlsStruct->mScript = NULL;
138 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
139 if (status) {
140 LOGE("pthread_setspecific %i", status);
141 }
142
Jason Sams8ce125b2009-06-17 16:52:59 -0700143 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
144 rsc->setVertex(NULL);
145 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
146 rsc->setFragment(NULL);
147 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
148 rsc->setFragmentStore(NULL);
149
Jason Sams326e0dd2009-05-22 14:03:28 -0700150 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700151 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700152 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700153 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
154 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700155
Jason Sams732f1c02009-06-18 16:58:42 -0700156 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700157 mDraw = rsc->runRootScript();
158 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700160 }
161
Jason Sams326e0dd2009-05-22 14:03:28 -0700162 glClearColor(0,0,0,0);
163 glClear(GL_COLOR_BUFFER_BIT);
164 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
165 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700166 return NULL;
167}
168
169Context::Context(Device *dev, Surface *sur)
170{
Jason Sams326e0dd2009-05-22 14:03:28 -0700171 dev->addContext(this);
172 mDev = dev;
173 mRunning = false;
174 mExit = false;
175
Jason Sams326e0dd2009-05-22 14:03:28 -0700176 // see comment in header
177 gCon = this;
178
Jason Samsa658e902009-06-04 14:35:01 -0700179 int status;
180 pthread_attr_t threadAttr;
181
Jason Samse5769102009-06-19 16:03:18 -0700182 status = pthread_key_create(&gThreadTLSKey, NULL);
183 if (status) {
184 LOGE("Failed to init thread tls key.");
185 return;
186 }
187
Jason Samsa658e902009-06-04 14:35:01 -0700188 status = pthread_attr_init(&threadAttr);
189 if (status) {
190 LOGE("Failed to init thread attribute.");
191 return;
192 }
193
194 sched_param sparam;
195 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
196 pthread_attr_setschedparam(&threadAttr, &sparam);
197
Jason Sams992a0b72009-06-23 12:22:47 -0700198 mWndSurface = sur;
199
200 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700201 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700202 if (status) {
203 LOGE("Failed to start rs context thread.");
204 }
205
Jason Sams326e0dd2009-05-22 14:03:28 -0700206 while(!mRunning) {
207 sleep(1);
208 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700209
Jason Samsa658e902009-06-04 14:35:01 -0700210 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700211}
212
213Context::~Context()
214{
215 mExit = true;
216 void *res;
217
Jason Sams326e0dd2009-05-22 14:03:28 -0700218 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700219
220 if (mDev) {
221 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700222 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700223 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700224}
225
226void Context::swapBuffers()
227{
228 eglSwapBuffers(mDisplay, mSurface);
229}
230
231void rsContextSwap(RsContext vrsc)
232{
233 Context *rsc = static_cast<Context *>(vrsc);
234 rsc->swapBuffers();
235}
236
237void Context::setRootScript(Script *s)
238{
239 mRootScript.set(s);
240}
241
242void Context::setFragmentStore(ProgramFragmentStore *pfs)
243{
Jason Sams8ce125b2009-06-17 16:52:59 -0700244 if (pfs == NULL) {
245 mFragmentStore.set(mStateFragmentStore.mDefault);
246 } else {
247 mFragmentStore.set(pfs);
248 }
249 mFragmentStore->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700250}
251
252void Context::setFragment(ProgramFragment *pf)
253{
Jason Sams8ce125b2009-06-17 16:52:59 -0700254 if (pf == NULL) {
255 mFragment.set(mStateFragment.mDefault);
256 } else {
257 mFragment.set(pf);
258 }
259 mFragment->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700260}
261
262void Context::setVertex(ProgramVertex *pv)
263{
Jason Sams8ce125b2009-06-17 16:52:59 -0700264 if (pv == NULL) {
265 mVertex.set(mStateVertex.mDefault);
266 } else {
267 mVertex.set(pv);
268 }
269 mVertex->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700270}
271
Jason Samsa4a54e42009-06-10 18:39:40 -0700272void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700273{
274 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700275 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700276 mNames.add(obj);
277}
278
279void Context::removeName(ObjectBase *obj)
280{
281 for(size_t ct=0; ct < mNames.size(); ct++) {
282 if (obj == mNames[ct]) {
283 mNames.removeAt(ct);
284 return;
285 }
286 }
287}
288
289ObjectBase * Context::lookupName(const char *name) const
290{
291 for(size_t ct=0; ct < mNames.size(); ct++) {
292 if (!strcmp(name, mNames[ct]->getName())) {
293 return mNames[ct];
294 }
295 }
296 return NULL;
297}
298
Jason Samsa4a54e42009-06-10 18:39:40 -0700299void Context::appendNameDefines(String8 *str) const
300{
301 char buf[256];
302 for (size_t ct=0; ct < mNames.size(); ct++) {
303 str->append("#define NAMED_");
304 str->append(mNames[ct]->getName());
305 str->append(" ");
306 sprintf(buf, "%i\n", (int)mNames[ct]);
307 str->append(buf);
308 }
309}
310
311
Jason Sams326e0dd2009-05-22 14:03:28 -0700312///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700313//
Jason Sams326e0dd2009-05-22 14:03:28 -0700314
315namespace android {
316namespace renderscript {
317
318
319void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
320{
321 Script *s = static_cast<Script *>(vs);
322 rsc->setRootScript(s);
323}
324
325void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
326{
327 Sampler *s = static_cast<Sampler *>(vs);
328
329 if (slot > RS_MAX_SAMPLER_SLOT) {
330 LOGE("Invalid sampler slot");
331 return;
332 }
333
334 s->bindToContext(&rsc->mStateSampler, slot);
335}
336
337void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
338{
339 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
340 rsc->setFragmentStore(pfs);
341}
342
343void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
344{
345 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
346 rsc->setFragment(pf);
347}
348
349void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
350{
351 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
352 rsc->setVertex(pv);
353}
354
Jason Samsa4a54e42009-06-10 18:39:40 -0700355void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700356{
357 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700358 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700359}
Jason Sams326e0dd2009-05-22 14:03:28 -0700360
361
362}
363}
364
365
366RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
367{
368 Device * dev = static_cast<Device *>(vdev);
369 Context *rsc = new Context(dev, (Surface *)sur);
370 return rsc;
371}
372
373void rsContextDestroy(RsContext vrsc)
374{
375 Context * rsc = static_cast<Context *>(vrsc);
376 delete rsc;
377}
378