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