rsContext.h revision 4ab16b53f6636198280a545ee6402bde57f1f84b
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#include "rsMutex.h"
22
23#include "rsThreadIO.h"
24#include "rsType.h"
25#include "rsMatrix.h"
26#include "rsAllocation.h"
27#include "rsSimpleMesh.h"
28#include "rsMesh.h"
29#include "rsDevice.h"
30#include "rsScriptC.h"
31#include "rsAllocation.h"
32#include "rsAdapter.h"
33#include "rsSampler.h"
34#include "rsLight.h"
35#include "rsFont.h"
36#include "rsProgramFragment.h"
37#include "rsProgramStore.h"
38#include "rsProgramRaster.h"
39#include "rsProgramVertex.h"
40#include "rsShaderCache.h"
41#include "rsVertexArray.h"
42
43#include "rsgApiStructs.h"
44#include "rsLocklessFifo.h"
45
46#include <ui/egl/android_natives.h>
47
48// ---------------------------------------------------------------------------
49namespace android {
50
51namespace renderscript {
52
53class Context
54{
55public:
56    Context(Device *, bool isGraphics, bool useDepth);
57    ~Context();
58
59    static pthread_key_t gThreadTLSKey;
60    static uint32_t gThreadTLSKeyCount;
61    static uint32_t gGLContextCount;
62    static pthread_mutex_t gInitMutex;
63
64    struct ScriptTLSStruct {
65        Context * mContext;
66        Script * mScript;
67    };
68
69
70    //StructuredAllocationContext mStateAllocation;
71    ElementState mStateElement;
72    TypeState mStateType;
73    SamplerState mStateSampler;
74    ProgramFragmentState mStateFragment;
75    ProgramStoreState mStateFragmentStore;
76    ProgramRasterState mStateRaster;
77    ProgramVertexState mStateVertex;
78    LightState mStateLight;
79    VertexArrayState mStateVertexArray;
80    FontState mStateFont;
81
82    ScriptCState mScriptC;
83    ShaderCache mShaderCache;
84
85    void swapBuffers();
86    void setRootScript(Script *);
87    void setRaster(ProgramRaster *);
88    void setVertex(ProgramVertex *);
89    void setFragment(ProgramFragment *);
90    void setFragmentStore(ProgramStore *);
91    void setFont(Font *);
92
93    void updateSurface(void *sur);
94
95    const ProgramFragment * getFragment() {return mFragment.get();}
96    const ProgramStore * getFragmentStore() {return mFragmentStore.get();}
97    const ProgramRaster * getRaster() {return mRaster.get();}
98    const ProgramVertex * getVertex() {return mVertex.get();}
99    Font * getFont() {return mFont.get();}
100
101    bool setupCheck();
102    bool checkDriver() const {return mEGL.mSurface != 0;}
103
104    void pause();
105    void resume();
106    void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
107    void setPriority(int32_t p);
108
109    void assignName(ObjectBase *obj, const char *name, uint32_t len);
110    void removeName(ObjectBase *obj);
111
112    uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
113    bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
114    uint32_t runScript(Script *s);
115
116    void initToClient();
117    void deinitToClient();
118
119    ProgramFragment * getDefaultProgramFragment() const {
120        return mStateFragment.mDefault.get();
121    }
122    ProgramVertex * getDefaultProgramVertex() const {
123        return mStateVertex.mDefault.get();
124    }
125    ProgramStore * getDefaultProgramStore() const {
126        return mStateFragmentStore.mDefault.get();
127    }
128    ProgramRaster * getDefaultProgramRaster() const {
129        return mStateRaster.mDefault.get();
130    }
131    Font* getDefaultFont() const {
132        return mStateFont.mDefault.get();
133    }
134
135    uint32_t getWidth() const {return mWidth;}
136    uint32_t getHeight() const {return mHeight;}
137
138
139    ThreadIO mIO;
140    void objDestroyAdd(ObjectBase *);
141
142    // Timers
143    enum Timers {
144        RS_TIMER_IDLE,
145        RS_TIMER_INTERNAL,
146        RS_TIMER_SCRIPT,
147        RS_TIMER_CLEAR_SWAP,
148        _RS_TIMER_TOTAL
149    };
150    uint64_t getTime() const;
151    void timerInit();
152    void timerReset();
153    void timerSet(Timers);
154    void timerPrint();
155    void timerFrame();
156
157    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
158    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
159
160    struct {
161        bool mLogTimes;
162        bool mLogScripts;
163        bool mLogObjects;
164        bool mLogShaders;
165    } props;
166
167    void dumpDebug() const;
168    void checkError(const char *) const;
169    const char * getError(RsError *);
170    void setError(RsError e, const char *msg = NULL);
171
172    mutable const ObjectBase * mObjHead;
173
174    bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
175
176protected:
177    Device *mDev;
178
179    struct {
180        EGLint mNumConfigs;
181        EGLint mMajorVersion;
182        EGLint mMinorVersion;
183        EGLConfig mConfig;
184        EGLContext mContext;
185        EGLSurface mSurface;
186        EGLDisplay mDisplay;
187    } mEGL;
188
189    struct {
190        const uint8_t * mVendor;
191        const uint8_t * mRenderer;
192        const uint8_t * mVersion;
193        const uint8_t * mExtensions;
194
195        uint32_t mMajorVersion;
196        uint32_t mMinorVersion;
197
198        int32_t mMaxVaryingVectors;
199        int32_t mMaxTextureImageUnits;
200
201        int32_t mMaxFragmentTextureImageUnits;
202        int32_t mMaxFragmentUniformVectors;
203
204        int32_t mMaxVertexAttribs;
205        int32_t mMaxVertexUniformVectors;
206        int32_t mMaxVertexTextureUnits;
207
208        bool OES_texture_npot;
209    } mGL;
210
211    uint32_t mWidth;
212    uint32_t mHeight;
213    int32_t mThreadPriority;
214    bool mIsGraphicsContext;
215
216    bool mRunning;
217    bool mExit;
218    bool mUseDepth;
219    bool mPaused;
220    RsError mError;
221    const char *mErrorMsg;
222
223    pthread_t mThreadId;
224    pid_t mNativeThreadId;
225
226    ObjectBaseRef<Script> mRootScript;
227    ObjectBaseRef<ProgramFragment> mFragment;
228    ObjectBaseRef<ProgramVertex> mVertex;
229    ObjectBaseRef<ProgramStore> mFragmentStore;
230    ObjectBaseRef<ProgramRaster> mRaster;
231    ObjectBaseRef<Font> mFont;
232
233    struct ObjDestroyOOB {
234        Mutex mMutex;
235        Vector<ObjectBase *> mDestroyList;
236        bool mNeedToEmpty;
237    };
238    ObjDestroyOOB mObjDestroy;
239    bool objDestroyOOBInit();
240    void objDestroyOOBRun();
241    void objDestroyOOBDestroy();
242
243private:
244    Context();
245
246    void initEGL(bool useGL2);
247    void deinitEGL();
248
249    uint32_t runRootScript();
250
251    static void * threadProc(void *);
252
253    ANativeWindow *mWndSurface;
254
255    Vector<ObjectBase *> mNames;
256
257    uint64_t mTimers[_RS_TIMER_TOTAL];
258    Timers mTimerActive;
259    uint64_t mTimeLast;
260    uint64_t mTimeFrame;
261    uint64_t mTimeLastFrame;
262    uint32_t mTimeMSLastFrame;
263    uint32_t mTimeMSLastScript;
264    uint32_t mTimeMSLastSwap;
265};
266
267}
268}
269#endif
270