blob: 6a47de132f99af757025c37bab69c9654025d115 [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"
20
21
22using namespace android;
23using namespace android::renderscript;
24
25Context * Context::gCon = NULL;
26
27void Context::initEGL()
28{
29 mNumConfigs = -1;
30
31 EGLint s_configAttribs[] = {
32 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
33 EGL_RED_SIZE, 5,
34 EGL_GREEN_SIZE, 6,
35 EGL_BLUE_SIZE, 5,
36 EGL_DEPTH_SIZE, 16,
37 EGL_NONE
38 };
39
Jason Sams326e0dd2009-05-22 14:03:28 -070040 mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams326e0dd2009-05-22 14:03:28 -070041 eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
Jason Sams326e0dd2009-05-22 14:03:28 -070042 eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
Jason Sams326e0dd2009-05-22 14:03:28 -070043
44 if (mWndSurface) {
45 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
46 new EGLNativeWindowSurface(mWndSurface),
47 NULL);
48 } else {
49 mSurface = eglCreateWindowSurface(mDisplay, mConfig,
50 android_createDisplaySurface(),
51 NULL);
52 }
53
Jason Sams326e0dd2009-05-22 14:03:28 -070054 mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
Jason Samsa44cb292009-06-04 17:58:03 -070055 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
Jason Sams326e0dd2009-05-22 14:03:28 -070056 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
57 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
Jason Sams326e0dd2009-05-22 14:03:28 -070058}
59
Jason Samsa44cb292009-06-04 17:58:03 -070060bool Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -070061{
62 rsAssert(mRootScript->mIsRoot);
63
Jason Samsa44cb292009-06-04 17:58:03 -070064 glColor4f(1,1,1,1);
65 glEnable(GL_LIGHT0);
Jason Sams326e0dd2009-05-22 14:03:28 -070066 glViewport(0, 0, 320, 480);
67 float aspectH = 480.f / 320.f;
68
69 if(mRootScript->mIsOrtho) {
70 glMatrixMode(GL_PROJECTION);
71 glLoadIdentity();
Jason Sams6678e9b2009-05-27 14:45:32 -070072 glOrthof(0, 320, 480, 0, 0, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -070073 glMatrixMode(GL_MODELVIEW);
74 } else {
75 glMatrixMode(GL_PROJECTION);
76 glLoadIdentity();
77 glFrustumf(-1, 1, -aspectH, aspectH, 1, 100);
78 glRotatef(-90, 0,0,1);
79 glTranslatef(0, 0, -3);
80 glMatrixMode(GL_MODELVIEW);
81 }
82
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
85
86 glDepthMask(GL_TRUE);
87 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
88
Jason Sams928b7342009-06-08 18:50:13 -070089 glClearColor(mRootScript->mEnviroment.mClearColor[0],
90 mRootScript->mEnviroment.mClearColor[1],
91 mRootScript->mEnviroment.mClearColor[2],
92 mRootScript->mEnviroment.mClearColor[3]);
93 glClearDepthf(mRootScript->mEnviroment.mClearDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -070094 glClear(GL_COLOR_BUFFER_BIT);
95 glClear(GL_DEPTH_BUFFER_BIT);
96
Jason Samsa44cb292009-06-04 17:58:03 -070097 return mRootScript->run(this, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -070098}
99
100void Context::setupCheck()
101{
102 if (mFragmentStore.get()) {
103 mFragmentStore->setupGL();
104 }
105 if (mFragment.get()) {
106 mFragment->setupGL();
107 }
108 if (mVertex.get()) {
109 mVertex->setupGL();
110 }
111
112}
113
114
115void * Context::threadProc(void *vrsc)
116{
117 Context *rsc = static_cast<Context *>(vrsc);
118
Jason Sams326e0dd2009-05-22 14:03:28 -0700119 gIO = new ThreadIO();
Jason Sams326e0dd2009-05-22 14:03:28 -0700120 rsc->mServerCommands.init(128);
121 rsc->mServerReturns.init(128);
122
123 rsc->initEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700124 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700125 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700126 while (!rsc->mExit) {
Jason Samsa44cb292009-06-04 17:58:03 -0700127 mDraw |= gIO->playCoreCommands(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700128
Jason Samsa44cb292009-06-04 17:58:03 -0700129 if (!mDraw || !rsc->mRootScript.get()) {
130 usleep(10000);
Jason Sams326e0dd2009-05-22 14:03:28 -0700131 continue;
132 }
133
Jason Sams326e0dd2009-05-22 14:03:28 -0700134 if (rsc->mRootScript.get()) {
Jason Samsa44cb292009-06-04 17:58:03 -0700135 mDraw = rsc->runRootScript();
136 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
Jason Sams326e0dd2009-05-22 14:03:28 -0700137 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700138 }
139
Jason Sams326e0dd2009-05-22 14:03:28 -0700140 glClearColor(0,0,0,0);
141 glClear(GL_COLOR_BUFFER_BIT);
142 eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
143 eglTerminate(rsc->mDisplay);
Jason Sams326e0dd2009-05-22 14:03:28 -0700144 return NULL;
145}
146
147Context::Context(Device *dev, Surface *sur)
148{
Jason Sams326e0dd2009-05-22 14:03:28 -0700149 dev->addContext(this);
150 mDev = dev;
151 mRunning = false;
152 mExit = false;
153
154 mServerCommands.init(256);
155 mServerReturns.init(256);
156
157 // see comment in header
158 gCon = this;
159
Jason Samsa658e902009-06-04 14:35:01 -0700160 int status;
161 pthread_attr_t threadAttr;
162
163 status = pthread_attr_init(&threadAttr);
164 if (status) {
165 LOGE("Failed to init thread attribute.");
166 return;
167 }
168
169 sched_param sparam;
170 sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
171 pthread_attr_setschedparam(&threadAttr, &sparam);
172
Jason Samsefb8de12009-06-08 15:20:31 -0700173 LOGE("RS Launching thread");
Jason Samsa658e902009-06-04 14:35:01 -0700174 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700175 if (status) {
176 LOGE("Failed to start rs context thread.");
177 }
178
Jason Sams326e0dd2009-05-22 14:03:28 -0700179 mWndSurface = sur;
180 while(!mRunning) {
181 sleep(1);
182 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700183
Jason Samsa658e902009-06-04 14:35:01 -0700184 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700185}
186
187Context::~Context()
188{
189 mExit = true;
190 void *res;
191
Jason Sams326e0dd2009-05-22 14:03:28 -0700192 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700193
194 if (mDev) {
195 mDev->removeContext(this);
196 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700197}
198
199void Context::swapBuffers()
200{
201 eglSwapBuffers(mDisplay, mSurface);
202}
203
204void rsContextSwap(RsContext vrsc)
205{
206 Context *rsc = static_cast<Context *>(vrsc);
207 rsc->swapBuffers();
208}
209
210void Context::setRootScript(Script *s)
211{
212 mRootScript.set(s);
213}
214
215void Context::setFragmentStore(ProgramFragmentStore *pfs)
216{
217 mFragmentStore.set(pfs);
218 pfs->setupGL();
219}
220
221void Context::setFragment(ProgramFragment *pf)
222{
223 mFragment.set(pf);
224 pf->setupGL();
225}
226
227void Context::setVertex(ProgramVertex *pv)
228{
229 mVertex.set(pv);
230 pv->setupGL();
231}
232
233///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700234//
Jason Sams326e0dd2009-05-22 14:03:28 -0700235
236namespace android {
237namespace renderscript {
238
239
240void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
241{
242 Script *s = static_cast<Script *>(vs);
243 rsc->setRootScript(s);
244}
245
246void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
247{
248 Sampler *s = static_cast<Sampler *>(vs);
249
250 if (slot > RS_MAX_SAMPLER_SLOT) {
251 LOGE("Invalid sampler slot");
252 return;
253 }
254
255 s->bindToContext(&rsc->mStateSampler, slot);
256}
257
258void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
259{
260 ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
261 rsc->setFragmentStore(pfs);
262}
263
264void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
265{
266 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
267 rsc->setFragment(pf);
268}
269
270void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
271{
272 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
273 rsc->setVertex(pv);
274}
275
276
277
278}
279}
280
281
282RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
283{
284 Device * dev = static_cast<Device *>(vdev);
285 Context *rsc = new Context(dev, (Surface *)sur);
286 return rsc;
287}
288
289void rsContextDestroy(RsContext vrsc)
290{
291 Context * rsc = static_cast<Context *>(vrsc);
292 delete rsc;
293}
294