blob: e52b0e00b203f8ccabd73998f75ee42d35d95a05 [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 Samsc9d43db2009-07-28 12:02:16 -070082 return ret;
Jason Sams10308932009-06-09 12:15:30 -070083}
84
85
Jason Samsa44cb292009-06-04 17:58:03 -070086bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070087{
Jason Sams10308932009-06-09 12:15:30 -070088 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070089
Jason Samsa44cb292009-06-04 17:58:03 -070090 glColor4f(1,1,1,1);
91 glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070092 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Jason Sams326e0dd2009-05-22 14:03:28 -070094 glDepthMask(GL_TRUE);
95 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
96
Jason Sams928b7342009-06-08 18:50:13 -070097 glClearColor(mRootScript->mEnviroment.mClearColor[0],
98 mRootScript->mEnviroment.mClearColor[1],
99 mRootScript->mEnviroment.mClearColor[2],
100 mRootScript->mEnviroment.mClearColor[3]);
101 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700102 glClear(GL_COLOR_BUFFER_BIT);
103 glClear(GL_DEPTH_BUFFER_BIT);
104
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700105 return runScript(mRootScript.get(), 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700106}
107
108void Context::setupCheck()
109{
110 if (mFragmentStore.get()) {
111 mFragmentStore->setupGL();
112 }
113 if (mFragment.get()) {
114 mFragment->setupGL();
115 }
116 if (mVertex.get()) {
117 mVertex->setupGL();
118 }
119
120}
121
122
123void * Context::threadProc(void *vrsc)
124{
125 Context *rsc = static_cast<Context *>(vrsc);
126
Jason Sams326e0dd2009-05-22 14:03:28 -0700127 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700129
Jason Samse5769102009-06-19 16:03:18 -0700130 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
131 if (!tlsStruct) {
132 LOGE("Error allocating tls storage");
133 return NULL;
134 }
135 tlsStruct->mContext = rsc;
136 tlsStruct->mScript = NULL;
137 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
138 if (status) {
139 LOGE("pthread_setspecific %i", status);
140 }
141
Jason Sams8ce125b2009-06-17 16:52:59 -0700142 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
143 rsc->setVertex(NULL);
144 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
145 rsc->setFragment(NULL);
146 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
147 rsc->setFragmentStore(NULL);
148
Jason Sams326e0dd2009-05-22 14:03:28 -0700149 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700150 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700151 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700152 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
153 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700154
Jason Sams732f1c02009-06-18 16:58:42 -0700155 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700156 mDraw = rsc->runRootScript();
157 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700158 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 }
160
Jason Sams326e0dd2009-05-22 14:03:28 -0700161 glClearColor(0,0,0,0);
162 glClear(GL_COLOR_BUFFER_BIT);
163 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
164 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700165 return NULL;
166}
167
168Context::Context(Device *dev, Surface *sur)
169{
Jason Sams326e0dd2009-05-22 14:03:28 -0700170 dev->addContext(this);
171 mDev = dev;
172 mRunning = false;
173 mExit = false;
174
Jason Sams326e0dd2009-05-22 14:03:28 -0700175 // 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 Sams992a0b72009-06-23 12:22:47 -0700197 mWndSurface = sur;
198
199 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700200 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700201 if (status) {
202 LOGE("Failed to start rs context thread.");
203 }
204
Jason Sams326e0dd2009-05-22 14:03:28 -0700205 while(!mRunning) {
206 sleep(1);
207 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700208
Jason Samsa658e902009-06-04 14:35:01 -0700209 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700210}
211
212Context::~Context()
213{
214 mExit = true;
215 void *res;
216
Jason Sams326e0dd2009-05-22 14:03:28 -0700217 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700218
219 if (mDev) {
220 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700221 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700222 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700223}
224
225void Context::swapBuffers()
226{
227 eglSwapBuffers(mDisplay, mSurface);
228}
229
230void rsContextSwap(RsContext vrsc)
231{
232 Context *rsc = static_cast<Context *>(vrsc);
233 rsc->swapBuffers();
234}
235
236void Context::setRootScript(Script *s)
237{
238 mRootScript.set(s);
239}
240
241void Context::setFragmentStore(ProgramFragmentStore *pfs)
242{
Jason Sams8ce125b2009-06-17 16:52:59 -0700243 if (pfs == NULL) {
244 mFragmentStore.set(mStateFragmentStore.mDefault);
245 } else {
246 mFragmentStore.set(pfs);
247 }
248 mFragmentStore->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700249}
250
251void Context::setFragment(ProgramFragment *pf)
252{
Jason Sams8ce125b2009-06-17 16:52:59 -0700253 if (pf == NULL) {
254 mFragment.set(mStateFragment.mDefault);
255 } else {
256 mFragment.set(pf);
257 }
258 mFragment->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700259}
260
261void Context::setVertex(ProgramVertex *pv)
262{
Jason Sams8ce125b2009-06-17 16:52:59 -0700263 if (pv == NULL) {
264 mVertex.set(mStateVertex.mDefault);
265 } else {
266 mVertex.set(pv);
267 }
268 mVertex->setupGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700269}
270
Jason Samsa4a54e42009-06-10 18:39:40 -0700271void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700272{
273 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700274 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700275 mNames.add(obj);
276}
277
278void Context::removeName(ObjectBase *obj)
279{
280 for(size_t ct=0; ct < mNames.size(); ct++) {
281 if (obj == mNames[ct]) {
282 mNames.removeAt(ct);
283 return;
284 }
285 }
286}
287
288ObjectBase * Context::lookupName(const char *name) const
289{
290 for(size_t ct=0; ct < mNames.size(); ct++) {
291 if (!strcmp(name, mNames[ct]->getName())) {
292 return mNames[ct];
293 }
294 }
295 return NULL;
296}
297
Jason Samsa4a54e42009-06-10 18:39:40 -0700298void Context::appendNameDefines(String8 *str) const
299{
300 char buf[256];
301 for (size_t ct=0; ct < mNames.size(); ct++) {
302 str->append("#define NAMED_");
303 str->append(mNames[ct]->getName());
304 str->append(" ");
305 sprintf(buf, "%i\n", (int)mNames[ct]);
306 str->append(buf);
307 }
308}
309
310
Jason Sams326e0dd2009-05-22 14:03:28 -0700311///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700312//
Jason Sams326e0dd2009-05-22 14:03:28 -0700313
314namespace android {
315namespace renderscript {
316
317
318void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
319{
320 Script *s = static_cast<Script *>(vs);
321 rsc->setRootScript(s);
322}
323
324void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
325{
326 Sampler *s = static_cast<Sampler *>(vs);
327
328 if (slot > RS_MAX_SAMPLER_SLOT) {
329 LOGE("Invalid sampler slot");
330 return;
331 }
332
333 s->bindToContext(&rsc->mStateSampler, slot);
334}
335
336void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
337{
338 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
339 rsc->setFragmentStore(pfs);
340}
341
342void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
343{
344 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
345 rsc->setFragment(pf);
346}
347
348void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
349{
350 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
351 rsc->setVertex(pv);
352}
353
Jason Samsa4a54e42009-06-10 18:39:40 -0700354void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700355{
356 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700357 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700358}
Jason Sams326e0dd2009-05-22 14:03:28 -0700359
360
361}
362}
363
364
365RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
366{
367 Device * dev = static_cast<Device *>(vdev);
368 Context *rsc = new Context(dev, (Surface *)sur);
369 return rsc;
370}
371
372void rsContextDestroy(RsContext vrsc)
373{
374 Context * rsc = static_cast<Context *>(vrsc);
375 delete rsc;
376}
377