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