blob: 286045280b8efaddd90549912c90a1be0c55ea95 [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"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.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,
Jason Samsb80dfa72009-07-10 17:32:40 -070037#if 1
38 EGL_RED_SIZE, 8,
39 EGL_GREEN_SIZE, 8,
40 EGL_BLUE_SIZE, 8,
41 EGL_ALPHA_SIZE, 8,
42#else
Jason Sams326e0dd2009-05-22 14:03:28 -070043 EGL_RED_SIZE, 5,
44 EGL_GREEN_SIZE, 6,
45 EGL_BLUE_SIZE, 5,
Jason Samsb80dfa72009-07-10 17:32:40 -070046#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070047 EGL_DEPTH_SIZE, 16,
48 EGL_NONE
49 };
50
Jason Sams326e0dd2009-05-22 14:03:28 -070051 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -070052 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Sams326e0dd2009-05-22 14:03:28 -070053 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Sams326e0dd2009-05-22 14:03:28 -070054
55 if (mWndSurface) {
Mathias Agopian5ae678f2009-06-22 18:01:09 -070056 mSurface = eglCreateWindowSurface(mDisplay, mConfig, mWndSurface,
Jason Sams326e0dd2009-05-22 14:03:28 -070057 NULL);
58 } else {
59 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
60 android_createDisplaySurface(),
61 NULL);
62 }
63
Jason Sams326e0dd2009-05-22 14:03:28 -070064 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa44cb292009-06-04 17:58:03 -070065 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Sams326e0dd2009-05-22 14:03:28 -070066 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
67 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070068}
69
Jason Samsa0a1b6f2009-06-10 15:04:38 -070070bool Context::runScript(Script *s, uint32_t launchID)
Jason Sams10308932009-06-09 12:15:30 -070071{
72 ObjectBaseRef<ProgramFragment> frag(mFragment);
73 ObjectBaseRef<ProgramVertex> vtx(mVertex);
74 ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
75
Jason Samsa0a1b6f2009-06-10 15:04:38 -070076 bool ret = s->run(this, launchID);
Jason Sams10308932009-06-09 12:15:30 -070077
Jason Samsa0a1b6f2009-06-10 15:04:38 -070078 mFragment.set(frag);
79 mVertex.set(vtx);
80 mFragmentStore.set(store);
Jason Samsc9d43db2009-07-28 12:02:16 -070081 return ret;
Jason Sams10308932009-06-09 12:15:30 -070082}
83
84
Jason Samsa44cb292009-06-04 17:58:03 -070085bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070086{
Jason Samscfb1d112009-08-05 13:57:03 -070087#if RS_LOG_TIMES
88 struct timespec beginTime;
89 clock_gettime(CLOCK_MONOTONIC, &beginTime);
90#endif
91
Jason Sams10308932009-06-09 12:15:30 -070092 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Jason Samscfb1d112009-08-05 13:57:03 -070094 //glColor4f(1,1,1,1);
95 //glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070096 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070097
Jason Sams326e0dd2009-05-22 14:03:28 -070098 glDepthMask(GL_TRUE);
99 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
100
Jason Sams928b7342009-06-08 18:50:13 -0700101 glClearColor(mRootScript->mEnviroment.mClearColor[0],
102 mRootScript->mEnviroment.mClearColor[1],
103 mRootScript->mEnviroment.mClearColor[2],
104 mRootScript->mEnviroment.mClearColor[3]);
105 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700106 glClear(GL_COLOR_BUFFER_BIT);
107 glClear(GL_DEPTH_BUFFER_BIT);
108
Jason Samscfb1d112009-08-05 13:57:03 -0700109#if RS_LOG_TIMES
110 struct timespec startTime;
111 clock_gettime(CLOCK_MONOTONIC, &startTime);
112#endif
113 bool ret = runScript(mRootScript.get(), 0);
114
115#if RS_LOG_TIMES
116 struct timespec endTime;
117 clock_gettime(CLOCK_MONOTONIC, &endTime);
118
119 int t1 = ((unsigned long)startTime.tv_nsec - (unsigned long)beginTime.tv_nsec) / 1000 / 1000;
120 int t2 = ((unsigned long)endTime.tv_nsec - (unsigned long)startTime.tv_nsec) / 1000 / 1000;
121 LOGE("times %i, %i", t1, t2);
122#endif
123
124 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125}
126
127void Context::setupCheck()
128{
129 if (mFragmentStore.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700130 mFragmentStore->setupGL(&mStateFragmentStore);
Jason Sams326e0dd2009-05-22 14:03:28 -0700131 }
132 if (mFragment.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700133 mFragment->setupGL(&mStateFragment);
Jason Sams326e0dd2009-05-22 14:03:28 -0700134 }
135 if (mVertex.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700136 mVertex->setupGL(&mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700137 }
138
139}
140
141
142void * Context::threadProc(void *vrsc)
143{
144 Context *rsc = static_cast<Context *>(vrsc);
145
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700147 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700148
Jason Samse5769102009-06-19 16:03:18 -0700149 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
150 if (!tlsStruct) {
151 LOGE("Error allocating tls storage");
152 return NULL;
153 }
154 tlsStruct->mContext = rsc;
155 tlsStruct->mScript = NULL;
156 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
157 if (status) {
158 LOGE("pthread_setspecific %i", status);
159 }
160
Jason Sams8ce125b2009-06-17 16:52:59 -0700161 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
162 rsc->setVertex(NULL);
163 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
164 rsc->setFragment(NULL);
165 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
166 rsc->setFragmentStore(NULL);
167
Jason Sams326e0dd2009-05-22 14:03:28 -0700168 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700169 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700170 while (!rsc->mExit) {
Jason Sams732f1c02009-06-18 16:58:42 -0700171 mDraw |= gIO->playCoreCommands(rsc, !mDraw);
172 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700173
Jason Sams732f1c02009-06-18 16:58:42 -0700174 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700175 mDraw = rsc->runRootScript();
176 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700177 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700178 }
179
Jason Sams326e0dd2009-05-22 14:03:28 -0700180 glClearColor(0,0,0,0);
181 glClear(GL_COLOR_BUFFER_BIT);
182 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
183 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700184 return NULL;
185}
186
187Context::Context(Device *dev, Surface *sur)
188{
Jason Sams326e0dd2009-05-22 14:03:28 -0700189 dev->addContext(this);
190 mDev = dev;
191 mRunning = false;
192 mExit = false;
193
Jason Sams326e0dd2009-05-22 14:03:28 -0700194 // see comment in header
195 gCon = this;
196
Jason Samsa658e902009-06-04 14:35:01 -0700197 int status;
198 pthread_attr_t threadAttr;
199
Jason Samse5769102009-06-19 16:03:18 -0700200 status = pthread_key_create(&gThreadTLSKey, NULL);
201 if (status) {
202 LOGE("Failed to init thread tls key.");
203 return;
204 }
205
Jason Samsa658e902009-06-04 14:35:01 -0700206 status = pthread_attr_init(&threadAttr);
207 if (status) {
208 LOGE("Failed to init thread attribute.");
209 return;
210 }
211
212 sched_param sparam;
213 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
214 pthread_attr_setschedparam(&threadAttr, &sparam);
215
Jason Sams992a0b72009-06-23 12:22:47 -0700216 mWndSurface = sur;
217
218 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700219 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700220 if (status) {
221 LOGE("Failed to start rs context thread.");
222 }
223
Jason Sams326e0dd2009-05-22 14:03:28 -0700224 while(!mRunning) {
225 sleep(1);
226 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700227
Jason Samsa658e902009-06-04 14:35:01 -0700228 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700229}
230
231Context::~Context()
232{
233 mExit = true;
234 void *res;
235
Jason Sams326e0dd2009-05-22 14:03:28 -0700236 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700237
238 if (mDev) {
239 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700240 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700241 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700242}
243
244void Context::swapBuffers()
245{
246 eglSwapBuffers(mDisplay, mSurface);
247}
248
249void rsContextSwap(RsContext vrsc)
250{
251 Context *rsc = static_cast<Context *>(vrsc);
252 rsc->swapBuffers();
253}
254
255void Context::setRootScript(Script *s)
256{
257 mRootScript.set(s);
258}
259
260void Context::setFragmentStore(ProgramFragmentStore *pfs)
261{
Jason Sams8ce125b2009-06-17 16:52:59 -0700262 if (pfs == NULL) {
263 mFragmentStore.set(mStateFragmentStore.mDefault);
264 } else {
265 mFragmentStore.set(pfs);
266 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700267}
268
269void Context::setFragment(ProgramFragment *pf)
270{
Jason Sams8ce125b2009-06-17 16:52:59 -0700271 if (pf == NULL) {
272 mFragment.set(mStateFragment.mDefault);
273 } else {
274 mFragment.set(pf);
275 }
Jason Samscfb1d112009-08-05 13:57:03 -0700276}
277
278void Context::allocationCheck(const Allocation *a)
279{
280 mVertex->checkUpdatedAllocation(a);
281 mFragment->checkUpdatedAllocation(a);
282 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700283}
284
285void Context::setVertex(ProgramVertex *pv)
286{
Jason Sams8ce125b2009-06-17 16:52:59 -0700287 if (pv == NULL) {
288 mVertex.set(mStateVertex.mDefault);
289 } else {
290 mVertex.set(pv);
291 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700292}
293
Jason Samsa4a54e42009-06-10 18:39:40 -0700294void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700295{
296 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700297 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700298 mNames.add(obj);
299}
300
301void Context::removeName(ObjectBase *obj)
302{
303 for(size_t ct=0; ct < mNames.size(); ct++) {
304 if (obj == mNames[ct]) {
305 mNames.removeAt(ct);
306 return;
307 }
308 }
309}
310
311ObjectBase * Context::lookupName(const char *name) const
312{
313 for(size_t ct=0; ct < mNames.size(); ct++) {
314 if (!strcmp(name, mNames[ct]->getName())) {
315 return mNames[ct];
316 }
317 }
318 return NULL;
319}
320
Jason Samsa4a54e42009-06-10 18:39:40 -0700321void Context::appendNameDefines(String8 *str) const
322{
323 char buf[256];
324 for (size_t ct=0; ct < mNames.size(); ct++) {
325 str->append("#define NAMED_");
326 str->append(mNames[ct]->getName());
327 str->append(" ");
328 sprintf(buf, "%i\n", (int)mNames[ct]);
329 str->append(buf);
330 }
331}
332
Joe Onorato57b79ce2009-08-09 22:57:44 -0700333void Context::appendVarDefines(String8 *str) const
334{
335 char buf[256];
336 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
337 str->append("#define ");
338 str->append(mInt32Defines.keyAt(ct));
339 str->append(" ");
340 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
341 str->append(buf);
342
343 }
344 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
345 str->append("#define ");
346 str->append(mFloatDefines.keyAt(ct));
347 str->append(" ");
348 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
349 str->append(buf);
350 }
351}
352
Jason Samsa4a54e42009-06-10 18:39:40 -0700353
Jason Sams326e0dd2009-05-22 14:03:28 -0700354///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700355//
Jason Sams326e0dd2009-05-22 14:03:28 -0700356
357namespace android {
358namespace renderscript {
359
360
361void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
362{
363 Script *s = static_cast<Script *>(vs);
364 rsc->setRootScript(s);
365}
366
367void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
368{
369 Sampler *s = static_cast<Sampler *>(vs);
370
371 if (slot > RS_MAX_SAMPLER_SLOT) {
372 LOGE("Invalid sampler slot");
373 return;
374 }
375
376 s->bindToContext(&rsc->mStateSampler, slot);
377}
378
379void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
380{
381 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
382 rsc->setFragmentStore(pfs);
383}
384
385void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
386{
387 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
388 rsc->setFragment(pf);
389}
390
391void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
392{
393 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
394 rsc->setVertex(pv);
395}
396
Jason Samsa4a54e42009-06-10 18:39:40 -0700397void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700398{
399 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700400 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700401}
Jason Sams326e0dd2009-05-22 14:03:28 -0700402
Joe Onorato57b79ce2009-08-09 22:57:44 -0700403void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
404{
405 rsc->addInt32Define(name, value);
406}
407
408void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
409{
410 rsc->addFloatDefine(name, value);
411}
Jason Sams326e0dd2009-05-22 14:03:28 -0700412
413}
414}
415
416
417RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
418{
419 Device * dev = static_cast<Device *>(vdev);
420 Context *rsc = new Context(dev, (Surface *)sur);
421 return rsc;
422}
423
424void rsContextDestroy(RsContext vrsc)
425{
426 Context * rsc = static_cast<Context *>(vrsc);
427 delete rsc;
428}
429