blob: 9de23b32f03d5563f1110b2ae75bf70b5985b08c [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 Samscfb1d112009-08-05 13:57:03 -070088#if RS_LOG_TIMES
89 struct timespec beginTime;
90 clock_gettime(CLOCK_MONOTONIC, &beginTime);
91#endif
92
Jason Sams10308932009-06-09 12:15:30 -070093 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070094
Jason Samscfb1d112009-08-05 13:57:03 -070095 //glColor4f(1,1,1,1);
96 //glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070097 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070098
Jason Sams326e0dd2009-05-22 14:03:28 -070099 glDepthMask(GL_TRUE);
100 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
101
Jason Sams928b7342009-06-08 18:50:13 -0700102 glClearColor(mRootScript->mEnviroment.mClearColor[0],
103 mRootScript->mEnviroment.mClearColor[1],
104 mRootScript->mEnviroment.mClearColor[2],
105 mRootScript->mEnviroment.mClearColor[3]);
106 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700107 glClear(GL_COLOR_BUFFER_BIT);
108 glClear(GL_DEPTH_BUFFER_BIT);
109
Jason Samscfb1d112009-08-05 13:57:03 -0700110#if RS_LOG_TIMES
111 struct timespec startTime;
112 clock_gettime(CLOCK_MONOTONIC, &startTime);
113#endif
114 bool ret = runScript(mRootScript.get(), 0);
115
116#if RS_LOG_TIMES
117 struct timespec endTime;
118 clock_gettime(CLOCK_MONOTONIC, &endTime);
119
120 int t1 = ((unsigned long)startTime.tv_nsec - (unsigned long)beginTime.tv_nsec) / 1000 / 1000;
121 int t2 = ((unsigned long)endTime.tv_nsec - (unsigned long)startTime.tv_nsec) / 1000 / 1000;
122 LOGE("times %i, %i", t1, t2);
123#endif
124
125 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700126}
127
128void Context::setupCheck()
129{
130 if (mFragmentStore.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700131 mFragmentStore->setupGL(&mStateFragmentStore);
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 }
133 if (mFragment.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700134 mFragment->setupGL(&mStateFragment);
Jason Sams326e0dd2009-05-22 14:03:28 -0700135 }
136 if (mVertex.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700137 mVertex->setupGL(&mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700138 }
139
140}
141
142
143void * Context::threadProc(void *vrsc)
144{
145 Context *rsc = static_cast<Context *>(vrsc);
146
Jason Sams326e0dd2009-05-22 14:03:28 -0700147 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700148 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700149
Jason Samse5769102009-06-19 16:03:18 -0700150 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
151 if (!tlsStruct) {
152 LOGE("Error allocating tls storage");
153 return NULL;
154 }
155 tlsStruct->mContext = rsc;
156 tlsStruct->mScript = NULL;
157 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
158 if (status) {
159 LOGE("pthread_setspecific %i", status);
160 }
161
Jason Sams8ce125b2009-06-17 16:52:59 -0700162 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
163 rsc->setVertex(NULL);
164 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
165 rsc->setFragment(NULL);
166 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
167 rsc->setFragmentStore(NULL);
168
Jason Sams326e0dd2009-05-22 14:03:28 -0700169 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700170 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700171 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700172 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
173 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700174
Jason Sams732f1c02009-06-18 16:58:42 -0700175 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700176 mDraw = rsc->runRootScript();
177 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700178 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700179 }
180
Jason Sams326e0dd2009-05-22 14:03:28 -0700181 glClearColor(0,0,0,0);
182 glClear(GL_COLOR_BUFFER_BIT);
183 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
184 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 return NULL;
186}
187
188Context::Context(Device *dev, Surface *sur)
189{
Jason Sams326e0dd2009-05-22 14:03:28 -0700190 dev->addContext(this);
191 mDev = dev;
192 mRunning = false;
193 mExit = false;
194
Jason Sams326e0dd2009-05-22 14:03:28 -0700195 // see comment in header
196 gCon = this;
197
Jason Samsa658e902009-06-04 14:35:01 -0700198 int status;
199 pthread_attr_t threadAttr;
200
Jason Samse5769102009-06-19 16:03:18 -0700201 status = pthread_key_create(&gThreadTLSKey, NULL);
202 if (status) {
203 LOGE("Failed to init thread tls key.");
204 return;
205 }
206
Jason Samsa658e902009-06-04 14:35:01 -0700207 status = pthread_attr_init(&threadAttr);
208 if (status) {
209 LOGE("Failed to init thread attribute.");
210 return;
211 }
212
213 sched_param sparam;
214 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
215 pthread_attr_setschedparam(&threadAttr, &sparam);
216
Jason Sams992a0b72009-06-23 12:22:47 -0700217 mWndSurface = sur;
218
219 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700220 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700221 if (status) {
222 LOGE("Failed to start rs context thread.");
223 }
224
Jason Sams326e0dd2009-05-22 14:03:28 -0700225 while(!mRunning) {
226 sleep(1);
227 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700228
Jason Samsa658e902009-06-04 14:35:01 -0700229 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700230}
231
232Context::~Context()
233{
234 mExit = true;
235 void *res;
236
Jason Sams326e0dd2009-05-22 14:03:28 -0700237 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700238
239 if (mDev) {
240 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700241 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700242 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700243}
244
245void Context::swapBuffers()
246{
247 eglSwapBuffers(mDisplay, mSurface);
248}
249
250void rsContextSwap(RsContext vrsc)
251{
252 Context *rsc = static_cast<Context *>(vrsc);
253 rsc->swapBuffers();
254}
255
256void Context::setRootScript(Script *s)
257{
258 mRootScript.set(s);
259}
260
261void Context::setFragmentStore(ProgramFragmentStore *pfs)
262{
Jason Sams8ce125b2009-06-17 16:52:59 -0700263 if (pfs == NULL) {
264 mFragmentStore.set(mStateFragmentStore.mDefault);
265 } else {
266 mFragmentStore.set(pfs);
267 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700268}
269
270void Context::setFragment(ProgramFragment *pf)
271{
Jason Sams8ce125b2009-06-17 16:52:59 -0700272 if (pf == NULL) {
273 mFragment.set(mStateFragment.mDefault);
274 } else {
275 mFragment.set(pf);
276 }
Jason Samscfb1d112009-08-05 13:57:03 -0700277}
278
279void Context::allocationCheck(const Allocation *a)
280{
281 mVertex->checkUpdatedAllocation(a);
282 mFragment->checkUpdatedAllocation(a);
283 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700284}
285
286void Context::setVertex(ProgramVertex *pv)
287{
Jason Sams8ce125b2009-06-17 16:52:59 -0700288 if (pv == NULL) {
289 mVertex.set(mStateVertex.mDefault);
290 } else {
291 mVertex.set(pv);
292 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700293}
294
Jason Samsa4a54e42009-06-10 18:39:40 -0700295void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700296{
297 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700298 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700299 mNames.add(obj);
300}
301
302void Context::removeName(ObjectBase *obj)
303{
304 for(size_t ct=0; ct < mNames.size(); ct++) {
305 if (obj == mNames[ct]) {
306 mNames.removeAt(ct);
307 return;
308 }
309 }
310}
311
312ObjectBase * Context::lookupName(const char *name) const
313{
314 for(size_t ct=0; ct < mNames.size(); ct++) {
315 if (!strcmp(name, mNames[ct]->getName())) {
316 return mNames[ct];
317 }
318 }
319 return NULL;
320}
321
Jason Samsa4a54e42009-06-10 18:39:40 -0700322void Context::appendNameDefines(String8 *str) const
323{
324 char buf[256];
325 for (size_t ct=0; ct < mNames.size(); ct++) {
326 str->append("#define NAMED_");
327 str->append(mNames[ct]->getName());
328 str->append(" ");
329 sprintf(buf, "%i\n", (int)mNames[ct]);
330 str->append(buf);
331 }
332}
333
Joe Onorato57b79ce2009-08-09 22:57:44 -0700334void Context::appendVarDefines(String8 *str) const
335{
336 char buf[256];
337 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
338 str->append("#define ");
339 str->append(mInt32Defines.keyAt(ct));
340 str->append(" ");
341 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
342 str->append(buf);
343
344 }
345 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
346 str->append("#define ");
347 str->append(mFloatDefines.keyAt(ct));
348 str->append(" ");
349 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
350 str->append(buf);
351 }
352}
353
Jason Samsa4a54e42009-06-10 18:39:40 -0700354
Jason Sams326e0dd2009-05-22 14:03:28 -0700355///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700356//
Jason Sams326e0dd2009-05-22 14:03:28 -0700357
358namespace android {
359namespace renderscript {
360
361
362void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
363{
364 Script *s = static_cast<Script *>(vs);
365 rsc->setRootScript(s);
366}
367
368void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
369{
370 Sampler *s = static_cast<Sampler *>(vs);
371
372 if (slot > RS_MAX_SAMPLER_SLOT) {
373 LOGE("Invalid sampler slot");
374 return;
375 }
376
377 s->bindToContext(&rsc->mStateSampler, slot);
378}
379
380void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
381{
382 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
383 rsc->setFragmentStore(pfs);
384}
385
386void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
387{
388 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
389 rsc->setFragment(pf);
390}
391
392void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
393{
394 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
395 rsc->setVertex(pv);
396}
397
Jason Samsa4a54e42009-06-10 18:39:40 -0700398void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700399{
400 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700401 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700402}
Jason Sams326e0dd2009-05-22 14:03:28 -0700403
Joe Onorato57b79ce2009-08-09 22:57:44 -0700404void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
405{
406 rsc->addInt32Define(name, value);
407}
408
409void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
410{
411 rsc->addFloatDefine(name, value);
412}
Jason Sams326e0dd2009-05-22 14:03:28 -0700413
414}
415}
416
417
418RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
419{
420 Device * dev = static_cast<Device *>(vdev);
421 Context *rsc = new Context(dev, (Surface *)sur);
422 return rsc;
423}
424
425void rsContextDestroy(RsContext vrsc)
426{
427 Context * rsc = static_cast<Context *>(vrsc);
428 delete rsc;
429}
430