rsContext.h revision e57691037aea219562ac686429b4b98202aab7bc
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#ifndef ANDROID_RS_CONTEXT_H
18#define ANDROID_RS_CONTEXT_H
19
20#include <utils/Vector.h>
21#include <ui/EGLNativeWindowSurface.h>
22#include <ui/Surface.h>
23
24#include "rsType.h"
25#include "rsMatrix.h"
26#include "rsAllocation.h"
27#include "rsTriangleMesh.h"
28#include "rsDevice.h"
29#include "rsScriptC.h"
30#include "rsAllocation.h"
31#include "rsAdapter.h"
32#include "rsSampler.h"
33#include "rsProgramFragment.h"
34#include "rsProgramFragmentStore.h"
35#include "rsProgramVertex.h"
36
37#include "rsgApiStructs.h"
38#include "rsLocklessFifo.h"
39
40
41// ---------------------------------------------------------------------------
42namespace android {
43namespace renderscript {
44
45class Context
46{
47public:
48    Context(Device *, Surface *);
49    ~Context();
50
51    static pthread_key_t gThreadTLSKey;
52    struct ScriptTLSStruct {
53        Context * mContext;
54        Script * mScript;
55    };
56
57
58    //StructuredAllocationContext mStateAllocation;
59    ElementState mStateElement;
60    TypeState mStateType;
61    SamplerState mStateSampler;
62    ProgramFragmentState mStateFragment;
63    ProgramFragmentStoreState mStateFragmentStore;
64    ProgramVertexState mStateVertex;
65
66    TriangleMeshContext mStateTriangleMesh;
67
68    ScriptCState mScriptC;
69
70    static Context * getContext() {return gCon;}
71
72    void swapBuffers();
73    void setRootScript(Script *);
74    void setVertex(ProgramVertex *);
75    void setFragment(ProgramFragment *);
76    void setFragmentStore(ProgramFragmentStore *);
77
78    void updateSurface(void *sur);
79
80    const ProgramFragment * getFragment() {return mFragment.get();}
81    const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
82
83    void setupCheck();
84
85    void assignName(ObjectBase *obj, const char *name, uint32_t len);
86    void removeName(ObjectBase *obj);
87    ObjectBase * lookupName(const char *name) const;
88    void appendNameDefines(String8 *str) const;
89
90
91    ProgramFragment * getDefaultProgramFragment() const {
92        return mStateFragment.mDefault.get();
93    }
94    ProgramVertex * getDefaultProgramVertex() const {
95        return mStateVertex.mDefault.get();
96    }
97    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
98        return mStateFragmentStore.mDefault.get();
99    }
100
101protected:
102    Device *mDev;
103
104    EGLint mNumConfigs;
105    EGLint mMajorVersion;
106    EGLint mMinorVersion;
107    EGLConfig mConfig;
108    EGLContext mContext;
109    EGLSurface mSurface;
110    EGLint mWidth;
111    EGLint mHeight;
112    EGLDisplay mDisplay;
113
114    bool mRunning;
115    bool mExit;
116
117    LocklessCommandFifo mServerCommands;
118    LocklessCommandFifo mServerReturns;
119
120    pthread_t mThreadId;
121
122    ObjectBaseRef<Script> mRootScript;
123    ObjectBaseRef<ProgramFragment> mFragment;
124    ObjectBaseRef<ProgramVertex> mVertex;
125    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
126
127private:
128    Context();
129
130    void initEGL();
131
132    bool runScript(Script *s, uint32_t launchID);
133    bool runRootScript();
134
135    static void * threadProc(void *);
136
137    // todo: put in TLS
138    static Context *gCon;
139    Surface *mWndSurface;
140
141    Vector<ObjectBase *> mNames;
142};
143
144
145}
146}
147#endif
148