rsContext.cpp revision c9d43db4d216b01b13aebfdb31d5615909591b33
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#include "utils/String8.h"
21#include <ui/FramebufferNativeWindow.h>
22
23#include <GLES/gl.h>
24#include <GLES/glext.h>
25
26using namespace android;
27using namespace android::renderscript;
28
29Context * Context::gCon = NULL;
30pthread_key_t Context::gThreadTLSKey = 0;
31
32void Context::initEGL()
33{
34    mNumConfigs = -1;
35
36    EGLint s_configAttribs[] = {
37         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
38#if 1
39         EGL_RED_SIZE,       8,
40         EGL_GREEN_SIZE,     8,
41         EGL_BLUE_SIZE,      8,
42         EGL_ALPHA_SIZE,     8,
43#else
44         EGL_RED_SIZE,       5,
45         EGL_GREEN_SIZE,     6,
46         EGL_BLUE_SIZE,      5,
47#endif
48         EGL_DEPTH_SIZE,     16,
49         EGL_NONE
50     };
51
52     mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
53     eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
54     eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
55
56     if (mWndSurface) {
57         mSurface = eglCreateWindowSurface(mDisplay, mConfig, mWndSurface,
58                 NULL);
59     } else {
60         mSurface = eglCreateWindowSurface(mDisplay, mConfig,
61                 android_createDisplaySurface(),
62                 NULL);
63     }
64
65     mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
66     eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
67     eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
68     eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
69}
70
71bool Context::runScript(Script *s, uint32_t launchID)
72{
73    ObjectBaseRef<ProgramFragment> frag(mFragment);
74    ObjectBaseRef<ProgramVertex> vtx(mVertex);
75    ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
76
77    bool ret = s->run(this, launchID);
78
79    mFragment.set(frag);
80    mVertex.set(vtx);
81    mFragmentStore.set(store);
82    return ret;
83}
84
85
86bool Context::runRootScript()
87{
88    rsAssert(mRootScript->mEnviroment.mIsRoot);
89
90    glColor4f(1,1,1,1);
91    glEnable(GL_LIGHT0);
92    glViewport(0, 0, mWidth, mHeight);
93
94    glDepthMask(GL_TRUE);
95    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
96
97    glClearColor(mRootScript->mEnviroment.mClearColor[0],
98                 mRootScript->mEnviroment.mClearColor[1],
99                 mRootScript->mEnviroment.mClearColor[2],
100                 mRootScript->mEnviroment.mClearColor[3]);
101    glClearDepthf(mRootScript->mEnviroment.mClearDepth);
102    glClear(GL_COLOR_BUFFER_BIT);
103    glClear(GL_DEPTH_BUFFER_BIT);
104
105    return runScript(mRootScript.get(), 0);
106}
107
108void Context::setupCheck()
109{
110    if (mFragmentStore.get()) {
111        mFragmentStore->setupGL();
112    }
113    if (mFragment.get()) {
114        mFragment->setupGL();
115    }
116    if (mVertex.get()) {
117        mVertex->setupGL();
118    }
119
120}
121
122
123void * Context::threadProc(void *vrsc)
124{
125     Context *rsc = static_cast<Context *>(vrsc);
126
127     gIO = new ThreadIO();
128     rsc->initEGL();
129
130     ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
131     if (!tlsStruct) {
132         LOGE("Error allocating tls storage");
133         return NULL;
134     }
135     tlsStruct->mContext = rsc;
136     tlsStruct->mScript = NULL;
137     int status = pthread_setspecific(rsc->gThreadTLSKey, tlsStruct);
138     if (status) {
139         LOGE("pthread_setspecific %i", status);
140     }
141
142     rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
143     rsc->setVertex(NULL);
144     rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
145     rsc->setFragment(NULL);
146     rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
147     rsc->setFragmentStore(NULL);
148
149     rsc->mRunning = true;
150     bool mDraw = true;
151     while (!rsc->mExit) {
152         mDraw |= gIO->playCoreCommands(rsc, !mDraw);
153         mDraw &= (rsc->mRootScript.get() != NULL);
154
155         if (mDraw) {
156             mDraw = rsc->runRootScript();
157             eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
158         }
159     }
160
161     glClearColor(0,0,0,0);
162     glClear(GL_COLOR_BUFFER_BIT);
163     eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
164     eglTerminate(rsc->mDisplay);
165     return NULL;
166}
167
168Context::Context(Device *dev, Surface *sur)
169{
170    dev->addContext(this);
171    mDev = dev;
172    mRunning = false;
173    mExit = false;
174
175    // see comment in header
176    gCon = this;
177
178    int status;
179    pthread_attr_t threadAttr;
180
181    status = pthread_key_create(&gThreadTLSKey, NULL);
182    if (status) {
183        LOGE("Failed to init thread tls key.");
184        return;
185    }
186
187    status = pthread_attr_init(&threadAttr);
188    if (status) {
189        LOGE("Failed to init thread attribute.");
190        return;
191    }
192
193    sched_param sparam;
194    sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
195    pthread_attr_setschedparam(&threadAttr, &sparam);
196
197    mWndSurface = sur;
198
199    LOGV("RS Launching thread");
200    status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
201    if (status) {
202        LOGE("Failed to start rs context thread.");
203    }
204
205    while(!mRunning) {
206        sleep(1);
207    }
208
209    pthread_attr_destroy(&threadAttr);
210}
211
212Context::~Context()
213{
214    mExit = true;
215    void *res;
216
217    int status = pthread_join(mThreadId, &res);
218
219    if (mDev) {
220        mDev->removeContext(this);
221        pthread_key_delete(gThreadTLSKey);
222    }
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    if (pfs == NULL) {
244        mFragmentStore.set(mStateFragmentStore.mDefault);
245    } else {
246        mFragmentStore.set(pfs);
247    }
248    mFragmentStore->setupGL();
249}
250
251void Context::setFragment(ProgramFragment *pf)
252{
253    if (pf == NULL) {
254        mFragment.set(mStateFragment.mDefault);
255    } else {
256        mFragment.set(pf);
257    }
258    mFragment->setupGL();
259}
260
261void Context::setVertex(ProgramVertex *pv)
262{
263    if (pv == NULL) {
264        mVertex.set(mStateVertex.mDefault);
265    } else {
266        mVertex.set(pv);
267    }
268    mVertex->setupGL();
269}
270
271void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
272{
273    rsAssert(!obj->getName());
274    obj->setName(name, len);
275    mNames.add(obj);
276}
277
278void Context::removeName(ObjectBase *obj)
279{
280    for(size_t ct=0; ct < mNames.size(); ct++) {
281        if (obj == mNames[ct]) {
282            mNames.removeAt(ct);
283            return;
284        }
285    }
286}
287
288ObjectBase * Context::lookupName(const char *name) const
289{
290    for(size_t ct=0; ct < mNames.size(); ct++) {
291        if (!strcmp(name, mNames[ct]->getName())) {
292            return mNames[ct];
293        }
294    }
295    return NULL;
296}
297
298void Context::appendNameDefines(String8 *str) const
299{
300    char buf[256];
301    for (size_t ct=0; ct < mNames.size(); ct++) {
302        str->append("#define NAMED_");
303        str->append(mNames[ct]->getName());
304        str->append(" ");
305        sprintf(buf, "%i\n", (int)mNames[ct]);
306        str->append(buf);
307    }
308}
309
310
311///////////////////////////////////////////////////////////////////////////////////////////
312//
313
314namespace android {
315namespace renderscript {
316
317
318void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
319{
320    Script *s = static_cast<Script *>(vs);
321    rsc->setRootScript(s);
322}
323
324void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
325{
326    Sampler *s = static_cast<Sampler *>(vs);
327
328    if (slot > RS_MAX_SAMPLER_SLOT) {
329        LOGE("Invalid sampler slot");
330        return;
331    }
332
333    s->bindToContext(&rsc->mStateSampler, slot);
334}
335
336void rsi_ContextBindProgramFragmentStore(Context *rsc, RsProgramFragmentStore vpfs)
337{
338    ProgramFragmentStore *pfs = static_cast<ProgramFragmentStore *>(vpfs);
339    rsc->setFragmentStore(pfs);
340}
341
342void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
343{
344    ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
345    rsc->setFragment(pf);
346}
347
348void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
349{
350    ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
351    rsc->setVertex(pv);
352}
353
354void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
355{
356    ObjectBase *ob = static_cast<ObjectBase *>(obj);
357    rsc->assignName(ob, name, len);
358}
359
360
361}
362}
363
364
365RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
366{
367    Device * dev = static_cast<Device *>(vdev);
368    Context *rsc = new Context(dev, (Surface *)sur);
369    return rsc;
370}
371
372void rsContextDestroy(RsContext vrsc)
373{
374    Context * rsc = static_cast<Context *>(vrsc);
375    delete rsc;
376}
377
378