blob: c731db00afcc4a7075484aae9fc3fe4be515c89b [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsd5680f92009-06-10 18:39:40 -070020#include "utils/String8.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070021
Jason Sams4b962e52009-06-22 17:15:15 -070022#include <GLES/gl.h>
23#include <GLES/glext.h>
24
Jason Samsd19f10d2009-05-22 14:03:28 -070025using namespace android;
26using namespace android::renderscript;
27
28Context * Context::gCon = NULL;
Jason Sams462d11b2009-06-19 16:03:18 -070029pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070044 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Samsd19f10d2009-05-22 14:03:28 -070045 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Samsd19f10d2009-05-22 14:03:28 -070046 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070058 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa09f11d2009-06-04 17:58:03 -070059 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Samsd19f10d2009-05-22 14:03:28 -070060 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
61 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -070062}
63
Jason Sams3eaa3382009-06-10 15:04:38 -070064bool Context::runScript(Script *s, uint32_t launchID)
Jason Samsda423d82009-06-09 12:15:30 -070065{
66 ObjectBaseRef<ProgramFragment> frag(mFragment);
67 ObjectBaseRef<ProgramVertex> vtx(mVertex);
68 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
69
Jason Sams3eaa3382009-06-10 15:04:38 -070070 bool ret = s->run(this, launchID);
Jason Samsda423d82009-06-09 12:15:30 -070071
Jason Sams3eaa3382009-06-10 15:04:38 -070072 mFragment.set(frag);
73 mVertex.set(vtx);
74 mFragmentStore.set(store);
Jason Samsda423d82009-06-09 12:15:30 -070075 return true;
76
77}
78
79
Jason Samsa09f11d2009-06-04 17:58:03 -070080bool Context::runRootScript()
Jason Samsd19f10d2009-05-22 14:03:28 -070081{
Jason Samsda423d82009-06-09 12:15:30 -070082 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Samsd19f10d2009-05-22 14:03:28 -070083
Jason Samsa09f11d2009-06-04 17:58:03 -070084 glColor4f(1,1,1,1);
85 glEnable(GL_LIGHT0);
Jason Samsda423d82009-06-09 12:15:30 -070086 glViewport(0, 0, mWidth, mHeight);
Jason Samsd19f10d2009-05-22 14:03:28 -070087
Jason Samsd19f10d2009-05-22 14:03:28 -070088 glDepthMask(GL_TRUE);
89 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
90
Jason Sams928f5cf2009-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 Samsd19f10d2009-05-22 14:03:28 -070096 glClear(GL_COLOR_BUFFER_BIT);
97 glClear(GL_DEPTH_BUFFER_BIT);
98
Jason Sams3eaa3382009-06-10 15:04:38 -070099 return runScript(mRootScript.get(), 0);
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -0700121 gIO = new ThreadIO();
Jason Samsd19f10d2009-05-22 14:03:28 -0700122 rsc->initEGL();
Jason Sams9c54bdb2009-06-17 16:52:59 -0700123
Jason Sams462d11b2009-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 Sams9c54bdb2009-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 Samsd19f10d2009-05-22 14:03:28 -0700143 rsc->mRunning = true;
Jason Samsa09f11d2009-06-04 17:58:03 -0700144 bool mDraw = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700145 while (!rsc->mExit) {
Jason Sams5f7fc272009-06-18 16:58:42 -0700146 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
147 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700148
Jason Sams5f7fc272009-06-18 16:58:42 -0700149 if (mDraw) {
Jason Samsa09f11d2009-06-04 17:58:03 -0700150 mDraw = rsc->runRootScript();
151 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Samsd19f10d2009-05-22 14:03:28 -0700152 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700153 }
154
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -0700159 return NULL;
160}
161
162Context::Context(Device *dev, Surface *sur)
163{
Jason Samsd19f10d2009-05-22 14:03:28 -0700164 dev->addContext(this);
165 mDev = dev;
166 mRunning = false;
167 mExit = false;
168
Jason Samsd19f10d2009-05-22 14:03:28 -0700169 // see comment in header
170 gCon = this;
171
Jason Sams8ad00102009-06-04 14:35:01 -0700172 int status;
173 pthread_attr_t threadAttr;
174
Jason Sams462d11b2009-06-19 16:03:18 -0700175 status = pthread_key_create(&gThreadTLSKey, NULL);
176 if (status) {
177 LOGE("Failed to init thread tls key.");
178 return;
179 }
180
Jason Sams8ad00102009-06-04 14:35:01 -0700181 status = pthread_attr_init(&threadAttr);
182 if (status) {
183 LOGE("Failed to init thread attribute.");
184 return;
185 }
186
187 sched_param sparam;
188 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
189 pthread_attr_setschedparam(&threadAttr, &sparam);
190
Jason Samsf29ca502009-06-23 12:22:47 -0700191 mWndSurface = sur;
192
193 LOGV("RS Launching thread");
Jason Sams8ad00102009-06-04 14:35:01 -0700194 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700195 if (status) {
196 LOGE("Failed to start rs context thread.");
197 }
198
Jason Samsd19f10d2009-05-22 14:03:28 -0700199 while(!mRunning) {
200 sleep(1);
201 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700202
Jason Sams8ad00102009-06-04 14:35:01 -0700203 pthread_attr_destroy(&threadAttr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700204}
205
206Context::~Context()
207{
208 mExit = true;
209 void *res;
210
Jason Samsd19f10d2009-05-22 14:03:28 -0700211 int status = pthread_join(mThreadId, &res);
Jason Samsd19f10d2009-05-22 14:03:28 -0700212
213 if (mDev) {
214 mDev->removeContext(this);
Jason Sams462d11b2009-06-19 16:03:18 -0700215 pthread_key_delete(gThreadTLSKey);
Jason Samsd19f10d2009-05-22 14:03:28 -0700216 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700217}
218
219void Context::swapBuffers()
220{
221 eglSwapBuffers(mDisplay, mSurface);
222}
223
224void rsContextSwap(RsContext vrsc)
225{
226 Context *rsc = static_cast<Context *>(vrsc);
227 rsc->swapBuffers();
228}
229
230void Context::setRootScript(Script *s)
231{
232 mRootScript.set(s);
233}
234
235void Context::setFragmentStore(ProgramFragmentStore *pfs)
236{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700237 if (pfs == NULL) {
238 mFragmentStore.set(mStateFragmentStore.mDefault);
239 } else {
240 mFragmentStore.set(pfs);
241 }
242 mFragmentStore->setupGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700243}
244
245void Context::setFragment(ProgramFragment *pf)
246{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700247 if (pf == NULL) {
248 mFragment.set(mStateFragment.mDefault);
249 } else {
250 mFragment.set(pf);
251 }
252 mFragment->setupGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700253}
254
255void Context::setVertex(ProgramVertex *pv)
256{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700257 if (pv == NULL) {
258 mVertex.set(mStateVertex.mDefault);
259 } else {
260 mVertex.set(pv);
261 }
262 mVertex->setupGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700263}
264
Jason Samsd5680f92009-06-10 18:39:40 -0700265void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700266{
267 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700268 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700269 mNames.add(obj);
270}
271
272void Context::removeName(ObjectBase *obj)
273{
274 for(size_t ct=0; ct < mNames.size(); ct++) {
275 if (obj == mNames[ct]) {
276 mNames.removeAt(ct);
277 return;
278 }
279 }
280}
281
282ObjectBase * Context::lookupName(const char *name) const
283{
284 for(size_t ct=0; ct < mNames.size(); ct++) {
285 if (!strcmp(name, mNames[ct]->getName())) {
286 return mNames[ct];
287 }
288 }
289 return NULL;
290}
291
Jason Samsd5680f92009-06-10 18:39:40 -0700292void Context::appendNameDefines(String8 *str) const
293{
294 char buf[256];
295 for (size_t ct=0; ct < mNames.size(); ct++) {
296 str->append("#define NAMED_");
297 str->append(mNames[ct]->getName());
298 str->append(" ");
299 sprintf(buf, "%i\n", (int)mNames[ct]);
300 str->append(buf);
301 }
302}
303
304
Jason Samsd19f10d2009-05-22 14:03:28 -0700305///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700306//
Jason Samsd19f10d2009-05-22 14:03:28 -0700307
308namespace android {
309namespace renderscript {
310
311
312void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
313{
314 Script *s = static_cast<Script *>(vs);
315 rsc->setRootScript(s);
316}
317
318void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
319{
320 Sampler *s = static_cast<Sampler *>(vs);
321
322 if (slot > RS_MAX_SAMPLER_SLOT) {
323 LOGE("Invalid sampler slot");
324 return;
325 }
326
327 s->bindToContext(&rsc->mStateSampler, slot);
328}
329
330void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
331{
332 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
333 rsc->setFragmentStore(pfs);
334}
335
336void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
337{
338 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
339 rsc->setFragment(pf);
340}
341
342void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
343{
344 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
345 rsc->setVertex(pv);
346}
347
Jason Samsd5680f92009-06-10 18:39:40 -0700348void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Sams3eaa3382009-06-10 15:04:38 -0700349{
350 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsd5680f92009-06-10 18:39:40 -0700351 rsc->assignName(ob, name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700352}
Jason Samsd19f10d2009-05-22 14:03:28 -0700353
354
355}
356}
357
358
359RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
360{
361 Device * dev = static_cast<Device *>(vdev);
362 Context *rsc = new Context(dev, (Surface *)sur);
363 return rsc;
364}
365
366void rsContextDestroy(RsContext vrsc)
367{
368 Context * rsc = static_cast<Context *>(vrsc);
369 delete rsc;
370}
371