rsContext.cpp revision a658e905f14fe500bfee3812b213a73cb37ef6f2
1/*
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
40     LOGE("EGL 1");
41     mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
42     LOGE("EGL 2  %p", mDisplay);
43     eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
44     LOGE("EGL 3  %i  %i", mMajorVersion, mMinorVersion);
45     eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
46     LOGE("EGL 4  %p", mConfig);
47
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
58     LOGE("EGL 5");
59     mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
60     eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
61     eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
62     eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
63     LOGE("EGL 9");
64
65}
66
67void Context::runRootScript()
68{
69    rsAssert(mRootScript->mIsRoot);
70
71    glViewport(0, 0, 320, 480);
72    float aspectH = 480.f / 320.f;
73
74    if(mRootScript->mIsOrtho) {
75        glMatrixMode(GL_PROJECTION);
76        glLoadIdentity();
77        glOrthof(0, 320,  480, 0,  0, 1);
78        glMatrixMode(GL_MODELVIEW);
79    } else {
80        glMatrixMode(GL_PROJECTION);
81        glLoadIdentity();
82        glFrustumf(-1, 1,  -aspectH, aspectH,  1, 100);
83        glRotatef(-90, 0,0,1);
84        glTranslatef(0,  0,  -3);
85        glMatrixMode(GL_MODELVIEW);
86    }
87
88    glMatrixMode(GL_MODELVIEW);
89    glLoadIdentity();
90
91    glDepthMask(GL_TRUE);
92    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
93
94    glClearColor(mRootScript->mClearColor[0],
95                 mRootScript->mClearColor[1],
96                 mRootScript->mClearColor[2],
97                 mRootScript->mClearColor[3]);
98    glClearDepthf(mRootScript->mClearDepth);
99    glClear(GL_COLOR_BUFFER_BIT);
100    glClear(GL_DEPTH_BUFFER_BIT);
101
102    mRootScript->run(this, 0);
103
104}
105
106void Context::setupCheck()
107{
108    if (mFragmentStore.get()) {
109        mFragmentStore->setupGL();
110    }
111    if (mFragment.get()) {
112        mFragment->setupGL();
113    }
114    if (mVertex.get()) {
115        mVertex->setupGL();
116    }
117
118}
119
120
121void * Context::threadProc(void *vrsc)
122{
123     Context *rsc = static_cast<Context *>(vrsc);
124
125     LOGE("TP 1");
126     gIO = new ThreadIO();
127
128     rsc->mServerCommands.init(128);
129     rsc->mServerReturns.init(128);
130
131     rsc->initEGL();
132
133     LOGE("TP 2");
134
135     rsc->mRunning = true;
136     while (!rsc->mExit) {
137         gIO->playCoreCommands(rsc);
138
139         if (!rsc->mRootScript.get()) {
140             continue;
141         }
142
143
144         glColor4f(1,1,1,1);
145         glEnable(GL_LIGHT0);
146
147         if (rsc->mRootScript.get()) {
148             rsc->runRootScript();
149         }
150
151         eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
152
153         usleep(10000);
154     }
155
156     LOGE("TP 6");
157     glClearColor(0,0,0,0);
158     glClear(GL_COLOR_BUFFER_BIT);
159     eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
160     eglTerminate(rsc->mDisplay);
161
162     LOGE("TP 7");
163
164     return NULL;
165}
166
167Context::Context(Device *dev, Surface *sur)
168{
169    LOGE("CC 1");
170    dev->addContext(this);
171    mDev = dev;
172    mRunning = false;
173    mExit = false;
174
175    mServerCommands.init(256);
176    mServerReturns.init(256);
177
178    // see comment in header
179    gCon = this;
180
181    LOGE("CC 2");
182    int status;
183    pthread_attr_t threadAttr;
184
185    status = pthread_attr_init(&threadAttr);
186    if (status) {
187        LOGE("Failed to init thread attribute.");
188        return;
189    }
190
191    sched_param sparam;
192    sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
193    pthread_attr_setschedparam(&threadAttr, &sparam);
194
195    status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
196    if (status) {
197        LOGE("Failed to start rs context thread.");
198    }
199
200    LOGE("CC 3");
201    mWndSurface = sur;
202    while(!mRunning) {
203        sleep(1);
204    }
205    LOGE("CC 4");
206
207    pthread_attr_destroy(&threadAttr);
208}
209
210Context::~Context()
211{
212    mExit = true;
213    void *res;
214
215    LOGE("DES 1");
216    int status = pthread_join(mThreadId, &res);
217    LOGE("DES 2");
218
219    if (mDev) {
220        mDev->removeContext(this);
221    }
222    LOGE("DES 3");
223}
224
225void Context::swapBuffers()
226{
227    eglSwapBuffers(mDisplay, mSurface);
228}
229
230void rsContextSwap(RsContext vrsc)
231{
232    Context *rsc = static_cast<Context *>(vrsc);
233    rsc->swapBuffers();
234}
235
236void Context::setRootScript(Script *s)
237{
238    mRootScript.set(s);
239}
240
241void Context::setFragmentStore(ProgramFragmentStore *pfs)
242{
243    mFragmentStore.set(pfs);
244    pfs->setupGL();
245}
246
247void Context::setFragment(ProgramFragment *pf)
248{
249    mFragment.set(pf);
250    pf->setupGL();
251}
252
253void Context::setVertex(ProgramVertex *pv)
254{
255    mVertex.set(pv);
256    pv->setupGL();
257}
258
259///////////////////////////////////////////////////////////////////////////////////////////
260//
261
262namespace android {
263namespace renderscript {
264
265
266void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
267{
268    Script *s = static_cast<Script *>(vs);
269    rsc->setRootScript(s);
270}
271
272void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
273{
274    Sampler *s = static_cast<Sampler *>(vs);
275
276    if (slot > RS_MAX_SAMPLER_SLOT) {
277        LOGE("Invalid sampler slot");
278        return;
279    }
280
281    s->bindToContext(&rsc->mStateSampler, slot);
282}
283
284void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
285{
286    ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
287    rsc->setFragmentStore(pfs);
288}
289
290void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
291{
292    ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
293    rsc->setFragment(pf);
294}
295
296void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
297{
298    ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
299    rsc->setVertex(pv);
300}
301
302
303
304}
305}
306
307
308RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
309{
310    Device * dev = static_cast<Device *>(vdev);
311    Context *rsc = new Context(dev, (Surface *)sur);
312    return rsc;
313}
314
315void rsContextDestroy(RsContext vrsc)
316{
317    Context * rsc = static_cast<Context *>(vrsc);
318    delete rsc;
319}
320
321