rsContext.h revision 771565f47fc44608444c00aa8fa3bda769ceaece
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 "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
80    ScriptCState mScriptC;
81    ShaderCache mShaderCache;
82
83    void swapBuffers();
84    void setRootScript(Script *);
85    void setRaster(ProgramRaster *);
86    void setVertex(ProgramVertex *);
87    void setFragment(ProgramFragment *);
88    void setFragmentStore(ProgramStore *);
89
90    void updateSurface(void *sur);
91
92    const ProgramFragment * getFragment() {return mFragment.get();}
93    const ProgramStore * getFragmentStore() {return mFragmentStore.get();}
94    const ProgramRaster * getRaster() {return mRaster.get();}
95    const ProgramVertex * getVertex() {return mVertex.get();}
96
97    bool setupCheck();
98    bool checkDriver() const {return mEGL.mSurface != 0;}
99
100    void pause();
101    void resume();
102    void setSurface(uint32_t w, uint32_t h, android_native_window_t *sur);
103    void setPriority(int32_t p);
104
105    void assignName(ObjectBase *obj, const char *name, uint32_t len);
106    void removeName(ObjectBase *obj);
107
108    uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
109    bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
110    uint32_t runScript(Script *s, uint32_t launchID);
111
112    void initToClient();
113    void deinitToClient();
114
115    ProgramFragment * getDefaultProgramFragment() const {
116        return mStateFragment.mDefault.get();
117    }
118    ProgramVertex * getDefaultProgramVertex() const {
119        return mStateVertex.mDefault.get();
120    }
121    ProgramStore * getDefaultProgramStore() const {
122        return mStateFragmentStore.mDefault.get();
123    }
124    ProgramRaster * getDefaultProgramRaster() const {
125        return mStateRaster.mDefault.get();
126    }
127
128    uint32_t getWidth() const {return mWidth;}
129    uint32_t getHeight() const {return mHeight;}
130
131
132    ThreadIO mIO;
133    void objDestroyAdd(ObjectBase *);
134
135    // Timers
136    enum Timers {
137        RS_TIMER_IDLE,
138        RS_TIMER_INTERNAL,
139        RS_TIMER_SCRIPT,
140        RS_TIMER_CLEAR_SWAP,
141        _RS_TIMER_TOTAL
142    };
143    uint64_t getTime() const;
144    void timerInit();
145    void timerReset();
146    void timerSet(Timers);
147    void timerPrint();
148    void timerFrame();
149
150    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
151    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
152
153    struct {
154        bool mLogTimes;
155        bool mLogScripts;
156        bool mLogObjects;
157        bool mLogShaders;
158    } props;
159
160    void dumpDebug() const;
161    void checkError(const char *) const;
162    const char * getError(RsError *);
163    void setError(RsError e, const char *msg = NULL);
164
165    mutable const ObjectBase * mObjHead;
166
167    bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
168
169protected:
170    Device *mDev;
171
172    struct {
173        EGLint mNumConfigs;
174        EGLint mMajorVersion;
175        EGLint mMinorVersion;
176        EGLConfig mConfig;
177        EGLContext mContext;
178        EGLSurface mSurface;
179        EGLint mWidth;
180        EGLint mHeight;
181        EGLDisplay mDisplay;
182    } mEGL;
183
184    struct {
185        const uint8_t * mVendor;
186        const uint8_t * mRenderer;
187        const uint8_t * mVersion;
188        const uint8_t * mExtensions;
189
190        uint32_t mMajorVersion;
191        uint32_t mMinorVersion;
192
193        int32_t mMaxVaryingVectors;
194        int32_t mMaxTextureImageUnits;
195
196        int32_t mMaxFragmentTextureImageUnits;
197        int32_t mMaxFragmentUniformVectors;
198
199        int32_t mMaxVertexAttribs;
200        int32_t mMaxVertexUniformVectors;
201        int32_t mMaxVertexTextureUnits;
202
203        bool OES_texture_npot;
204    } mGL;
205
206    uint32_t mWidth;
207    uint32_t mHeight;
208    int32_t mThreadPriority;
209    bool mIsGraphicsContext;
210
211    bool mRunning;
212    bool mExit;
213    bool mUseDepth;
214    bool mPaused;
215    RsError mError;
216    const char *mErrorMsg;
217
218    pthread_t mThreadId;
219    pid_t mNativeThreadId;
220
221    ObjectBaseRef<Script> mRootScript;
222    ObjectBaseRef<ProgramFragment> mFragment;
223    ObjectBaseRef<ProgramVertex> mVertex;
224    ObjectBaseRef<ProgramStore> mFragmentStore;
225    ObjectBaseRef<ProgramRaster> mRaster;
226
227
228    struct ObjDestroyOOB {
229        Mutex mMutex;
230        Vector<ObjectBase *> mDestroyList;
231        bool mNeedToEmpty;
232    };
233    ObjDestroyOOB mObjDestroy;
234    bool objDestroyOOBInit();
235    void objDestroyOOBRun();
236    void objDestroyOOBDestroy();
237
238private:
239    Context();
240
241    void initEGL(bool useGL2);
242    void deinitEGL();
243
244    uint32_t runRootScript();
245
246    static void * threadProc(void *);
247
248    android_native_window_t *mWndSurface;
249
250    Vector<ObjectBase *> mNames;
251
252    uint64_t mTimers[_RS_TIMER_TOTAL];
253    Timers mTimerActive;
254    uint64_t mTimeLast;
255    uint64_t mTimeFrame;
256    uint64_t mTimeLastFrame;
257    uint32_t mTimeMSLastFrame;
258    uint32_t mTimeMSLastScript;
259    uint32_t mTimeMSLastSwap;
260};
261
262}
263}
264#endif
265