rsContext.h revision ebfb436a49673693b98469683451bd9ede797557
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 <ui/Surface.h>
23
24#include "rsThreadIO.h"
25#include "rsType.h"
26#include "rsMatrix.h"
27#include "rsAllocation.h"
28#include "rsTriangleMesh.h"
29#include "rsSimpleMesh.h"
30#include "rsMesh.h"
31#include "rsDevice.h"
32#include "rsScriptC.h"
33#include "rsAllocation.h"
34#include "rsAdapter.h"
35#include "rsSampler.h"
36#include "rsLight.h"
37#include "rsProgramFragment.h"
38#include "rsProgramFragmentStore.h"
39#include "rsProgramRaster.h"
40#include "rsProgramVertex.h"
41
42#include "rsgApiStructs.h"
43#include "rsLocklessFifo.h"
44
45
46// ---------------------------------------------------------------------------
47namespace android {
48namespace renderscript {
49
50class Context
51{
52public:
53    Context(Device *, Surface *, bool useDepth);
54    ~Context();
55
56    static pthread_key_t gThreadTLSKey;
57    struct ScriptTLSStruct {
58        Context * mContext;
59        Script * mScript;
60    };
61
62
63    //StructuredAllocationContext mStateAllocation;
64    ElementState mStateElement;
65    TypeState mStateType;
66    SamplerState mStateSampler;
67    ProgramFragmentState mStateFragment;
68    ProgramFragmentStoreState mStateFragmentStore;
69    ProgramRasterState mStateRaster;
70    ProgramVertexState mStateVertex;
71    LightState mStateLight;
72
73    TriangleMeshContext mStateTriangleMesh;
74
75    ScriptCState mScriptC;
76
77    void swapBuffers();
78    void setRootScript(Script *);
79    void setRaster(ProgramRaster *);
80    void setVertex(ProgramVertex *);
81    void setFragment(ProgramFragment *);
82    void setFragmentStore(ProgramFragmentStore *);
83
84    void updateSurface(void *sur);
85
86    const ProgramFragment * getFragment() {return mFragment.get();}
87    const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
88    const ProgramRaster * getRaster() {return mRaster.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    ProgramRaster * getDefaultProgramRaster() const {
110        return mStateRaster.mDefault.get();
111    }
112
113    void addInt32Define(const char* name, int32_t value) {
114        mInt32Defines.add(String8(name), value);
115    }
116
117    void addFloatDefine(const char* name, float value) {
118        mFloatDefines.add(String8(name), value);
119    }
120
121    uint32_t getWidth() const {return mEGL.mWidth;}
122    uint32_t getHeight() const {return mEGL.mHeight;}
123
124
125    ThreadIO mIO;
126    void objDestroyAdd(ObjectBase *);
127
128    // Timers
129    enum Timers {
130        RS_TIMER_IDLE,
131        RS_TIMER_INTERNAL,
132        RS_TIMER_SCRIPT,
133        RS_TIMER_CLEAR_SWAP,
134        _RS_TIMER_TOTAL
135    };
136    uint64_t getTime() const;
137    void timerInit();
138    void timerReset();
139    void timerSet(Timers);
140    void timerPrint();
141    void timerFrame();
142
143    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
144    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
145
146protected:
147    Device *mDev;
148
149    struct {
150        EGLint mNumConfigs;
151        EGLint mMajorVersion;
152        EGLint mMinorVersion;
153        EGLConfig mConfig;
154        EGLContext mContext;
155        EGLSurface mSurface;
156        EGLint mWidth;
157        EGLint mHeight;
158        EGLDisplay mDisplay;
159    } mEGL;
160
161    struct {
162        const uint8_t * mVendor;
163        const uint8_t * mRenderer;
164        const uint8_t * mVersion;
165        const uint8_t * mExtensions;
166
167        uint32_t mMajorVersion;
168        uint32_t mMinorVersion;
169
170    } mGL;
171
172    bool mRunning;
173    bool mExit;
174    bool mUseDepth;
175
176    pthread_t mThreadId;
177
178    ObjectBaseRef<Script> mRootScript;
179    ObjectBaseRef<ProgramFragment> mFragment;
180    ObjectBaseRef<ProgramVertex> mVertex;
181    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
182    ObjectBaseRef<ProgramRaster> mRaster;
183
184
185    struct ObjDestroyOOB {
186        pthread_mutex_t mMutex;
187        Vector<ObjectBase *> mDestroyList;
188        bool mNeedToEmpty;
189    };
190    ObjDestroyOOB mObjDestroy;
191    bool objDestroyOOBInit();
192    void objDestroyOOBRun();
193    void objDestroyOOBDestroy();
194
195private:
196    Context();
197
198    void initEGL();
199
200    bool runScript(Script *s, uint32_t launchID);
201    bool runRootScript();
202
203    static void * threadProc(void *);
204
205    Surface *mWndSurface;
206
207    Vector<ObjectBase *> mNames;
208    KeyedVector<String8,int> mInt32Defines;
209    KeyedVector<String8,float> mFloatDefines;
210
211    uint64_t mTimers[_RS_TIMER_TOTAL];
212    Timers mTimerActive;
213    uint64_t mTimeLast;
214    uint64_t mTimeFrame;
215    uint64_t mTimeLastFrame;
216};
217
218
219}
220}
221#endif
222