blob: 52389eadfbf518119496bdce6e5d229a3f69b685 [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 Sams10308932009-06-09 12:15:30 -070087 rsAssert(mRootScript->mEnviroment.mIsRoot);
Jason Sams326e0dd2009-05-22 14:03:28 -070088
Jason Samscfb1d112009-08-05 13:57:03 -070089 //glColor4f(1,1,1,1);
90 //glEnable(GL_LIGHT0);
Jason Sams10308932009-06-09 12:15:30 -070091 glViewport(0, 0, mWidth, mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070092
Jason Sams326e0dd2009-05-22 14:03:28 -070093 glDepthMask(GL_TRUE);
94 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
95
Jason Sams928b7342009-06-08 18:50:13 -070096 glClearColor(mRootScript->mEnviroment.mClearColor[0],
97 mRootScript->mEnviroment.mClearColor[1],
98 mRootScript->mEnviroment.mClearColor[2],
99 mRootScript->mEnviroment.mClearColor[3]);
100 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -0700101 glClear(GL_COLOR_BUFFER_BIT);
102 glClear(GL_DEPTH_BUFFER_BIT);
103
Jason Samscfb1d112009-08-05 13:57:03 -0700104#if RS_LOG_TIMES
105 struct timespec startTime;
106 clock_gettime(CLOCK_MONOTONIC, &startTime);
107#endif
108 bool ret = runScript(mRootScript.get(), 0);
109
110#if RS_LOG_TIMES
111 struct timespec endTime;
112 clock_gettime(CLOCK_MONOTONIC, &endTime);
113
Jason Samsfcd31922009-08-17 18:35:48 -0700114 uint64_t t1 = endTime.tv_nsec + ((uint64_t)endTime.tv_sec * 1000 * 1000 * 1000);
115 uint64_t t2 = startTime.tv_nsec + ((uint64_t)startTime.tv_sec * 1000 * 1000 * 1000);
116 int t3 = (int)((t1 - t2) / 1000 / 1000);
117 LOGE("times %i", t3);
Jason Samscfb1d112009-08-05 13:57:03 -0700118#endif
119
120 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700121}
122
123void Context::setupCheck()
124{
125 if (mFragmentStore.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700126 mFragmentStore->setupGL(&mStateFragmentStore);
Jason Sams326e0dd2009-05-22 14:03:28 -0700127 }
128 if (mFragment.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700129 mFragment->setupGL(&mStateFragment);
Jason Sams326e0dd2009-05-22 14:03:28 -0700130 }
131 if (mVertex.get()) {
Jason Samscfb1d112009-08-05 13:57:03 -0700132 mVertex->setupGL(&mStateVertex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700133 }
134
135}
136
137
138void * Context::threadProc(void *vrsc)
139{
140 Context *rsc = static_cast<Context *>(vrsc);
141
Jason Sams326e0dd2009-05-22 14:03:28 -0700142 rsc->initEGL();
Jason Sams8ce125b2009-06-17 16:52:59 -0700143
Jason Samse5769102009-06-19 16:03:18 -0700144 ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
145 if (!tlsStruct) {
146 LOGE("Error allocating tls storage");
147 return NULL;
148 }
149 tlsStruct->mContext = rsc;
150 tlsStruct->mScript = NULL;
151 int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
152 if (status) {
153 LOGE("pthread_setspecific %i", status);
154 }
155
Jason Sams8ce125b2009-06-17 16:52:59 -0700156 rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
157 rsc->setVertex(NULL);
158 rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
159 rsc->setFragment(NULL);
160 rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
161 rsc->setFragmentStore(NULL);
162
Jason Sams326e0dd2009-05-22 14:03:28 -0700163 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700164 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700165 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700166 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700167 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700168
Jason Sams732f1c02009-06-18 16:58:42 -0700169 if (mDraw) {
Jason Samsa44cb292009-06-04 17:58:03 -0700170 mDraw = rsc->runRootScript();
171 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700172 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700173 }
174
Jason Sams326e0dd2009-05-22 14:03:28 -0700175 glClearColor(0,0,0,0);
176 glClear(GL_COLOR_BUFFER_BIT);
177 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
178 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700179 return NULL;
180}
181
182Context::Context(Device *dev, Surface *sur)
183{
Jason Sams326e0dd2009-05-22 14:03:28 -0700184 dev->addContext(this);
185 mDev = dev;
186 mRunning = false;
187 mExit = false;
188
Jason Sams326e0dd2009-05-22 14:03:28 -0700189 // see comment in header
190 gCon = this;
191
Jason Samsa658e902009-06-04 14:35:01 -0700192 int status;
193 pthread_attr_t threadAttr;
194
Jason Samse5769102009-06-19 16:03:18 -0700195 status = pthread_key_create(&gThreadTLSKey, NULL);
196 if (status) {
197 LOGE("Failed to init thread tls key.");
198 return;
199 }
200
Jason Samsa658e902009-06-04 14:35:01 -0700201 status = pthread_attr_init(&threadAttr);
202 if (status) {
203 LOGE("Failed to init thread attribute.");
204 return;
205 }
206
207 sched_param sparam;
208 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
209 pthread_attr_setschedparam(&threadAttr, &sparam);
210
Jason Sams992a0b72009-06-23 12:22:47 -0700211 mWndSurface = sur;
212
213 LOGV("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700214 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700215 if (status) {
216 LOGE("Failed to start rs context thread.");
217 }
218
Jason Sams326e0dd2009-05-22 14:03:28 -0700219 while(!mRunning) {
220 sleep(1);
221 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700222
Jason Samsa658e902009-06-04 14:35:01 -0700223 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700224}
225
226Context::~Context()
227{
228 mExit = true;
229 void *res;
230
Jason Sams326e0dd2009-05-22 14:03:28 -0700231 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700232
233 if (mDev) {
234 mDev->removeContext(this);
Jason Samse5769102009-06-19 16:03:18 -0700235 pthread_key_delete(gThreadTLSKey);
Jason Sams326e0dd2009-05-22 14:03:28 -0700236 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700237}
238
239void Context::swapBuffers()
240{
241 eglSwapBuffers(mDisplay, mSurface);
242}
243
244void rsContextSwap(RsContext vrsc)
245{
246 Context *rsc = static_cast<Context *>(vrsc);
247 rsc->swapBuffers();
248}
249
250void Context::setRootScript(Script *s)
251{
252 mRootScript.set(s);
253}
254
255void Context::setFragmentStore(ProgramFragmentStore *pfs)
256{
Jason Sams8ce125b2009-06-17 16:52:59 -0700257 if (pfs == NULL) {
258 mFragmentStore.set(mStateFragmentStore.mDefault);
259 } else {
260 mFragmentStore.set(pfs);
261 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700262}
263
264void Context::setFragment(ProgramFragment *pf)
265{
Jason Sams8ce125b2009-06-17 16:52:59 -0700266 if (pf == NULL) {
267 mFragment.set(mStateFragment.mDefault);
268 } else {
269 mFragment.set(pf);
270 }
Jason Samscfb1d112009-08-05 13:57:03 -0700271}
272
273void Context::allocationCheck(const Allocation *a)
274{
275 mVertex->checkUpdatedAllocation(a);
276 mFragment->checkUpdatedAllocation(a);
277 mFragmentStore->checkUpdatedAllocation(a);
Jason Sams326e0dd2009-05-22 14:03:28 -0700278}
279
280void Context::setVertex(ProgramVertex *pv)
281{
Jason Sams8ce125b2009-06-17 16:52:59 -0700282 if (pv == NULL) {
283 mVertex.set(mStateVertex.mDefault);
284 } else {
285 mVertex.set(pv);
286 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700287}
288
Jason Samsa4a54e42009-06-10 18:39:40 -0700289void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700290{
291 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700292 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700293 mNames.add(obj);
294}
295
296void Context::removeName(ObjectBase *obj)
297{
298 for(size_t ct=0; ct < mNames.size(); ct++) {
299 if (obj == mNames[ct]) {
300 mNames.removeAt(ct);
301 return;
302 }
303 }
304}
305
306ObjectBase * Context::lookupName(const char *name) const
307{
308 for(size_t ct=0; ct < mNames.size(); ct++) {
309 if (!strcmp(name, mNames[ct]->getName())) {
310 return mNames[ct];
311 }
312 }
313 return NULL;
314}
315
Jason Samsa4a54e42009-06-10 18:39:40 -0700316void Context::appendNameDefines(String8 *str) const
317{
318 char buf[256];
319 for (size_t ct=0; ct < mNames.size(); ct++) {
320 str->append("#define NAMED_");
321 str->append(mNames[ct]->getName());
322 str->append(" ");
323 sprintf(buf, "%i\n", (int)mNames[ct]);
324 str->append(buf);
325 }
326}
327
Joe Onorato57b79ce2009-08-09 22:57:44 -0700328void Context::appendVarDefines(String8 *str) const
329{
330 char buf[256];
331 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
332 str->append("#define ");
333 str->append(mInt32Defines.keyAt(ct));
334 str->append(" ");
335 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
336 str->append(buf);
337
338 }
339 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
340 str->append("#define ");
341 str->append(mFloatDefines.keyAt(ct));
342 str->append(" ");
343 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
344 str->append(buf);
345 }
346}
347
Jason Samsa4a54e42009-06-10 18:39:40 -0700348
Jason Sams326e0dd2009-05-22 14:03:28 -0700349///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700350//
Jason Sams326e0dd2009-05-22 14:03:28 -0700351
352namespace android {
353namespace renderscript {
354
355
356void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
357{
358 Script *s = static_cast<Script *>(vs);
359 rsc->setRootScript(s);
360}
361
362void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
363{
364 Sampler *s = static_cast<Sampler *>(vs);
365
366 if (slot > RS_MAX_SAMPLER_SLOT) {
367 LOGE("Invalid sampler slot");
368 return;
369 }
370
371 s->bindToContext(&rsc->mStateSampler, slot);
372}
373
374void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
375{
376 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
377 rsc->setFragmentStore(pfs);
378}
379
380void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
381{
382 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
383 rsc->setFragment(pf);
384}
385
386void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
387{
388 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
389 rsc->setVertex(pv);
390}
391
Jason Samsa4a54e42009-06-10 18:39:40 -0700392void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700393{
394 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700395 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700396}
Jason Sams326e0dd2009-05-22 14:03:28 -0700397
Joe Onorato57b79ce2009-08-09 22:57:44 -0700398void rsi_ContextSetDefineF(Context *rsc, const char* name, float value)
399{
400 rsc->addInt32Define(name, value);
401}
402
403void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
404{
405 rsc->addFloatDefine(name, value);
406}
Jason Sams326e0dd2009-05-22 14:03:28 -0700407
408}
409}
410
411
412RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
413{
414 Device * dev = static_cast<Device *>(vdev);
415 Context *rsc = new Context(dev, (Surface *)sur);
416 return rsc;
417}
418
419void rsContextDestroy(RsContext vrsc)
420{
421 Context * rsc = static_cast<Context *>(vrsc);
422 delete rsc;
423}
424