rsContext.h revision 7bf29ddc35450d8064541c42c99a1f48be6cf0dd
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    typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
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
176    void launchThreads(WorkerCallback_t cbk, void *data);
177
178protected:
179    Device *mDev;
180
181    struct {
182        EGLint mNumConfigs;
183        EGLint mMajorVersion;
184        EGLint mMinorVersion;
185        EGLConfig mConfig;
186        EGLContext mContext;
187        EGLSurface mSurface;
188        EGLDisplay mDisplay;
189    } mEGL;
190
191    struct {
192        const uint8_t * mVendor;
193        const uint8_t * mRenderer;
194        const uint8_t * mVersion;
195        const uint8_t * mExtensions;
196
197        uint32_t mMajorVersion;
198        uint32_t mMinorVersion;
199
200        int32_t mMaxVaryingVectors;
201        int32_t mMaxTextureImageUnits;
202
203        int32_t mMaxFragmentTextureImageUnits;
204        int32_t mMaxFragmentUniformVectors;
205
206        int32_t mMaxVertexAttribs;
207        int32_t mMaxVertexUniformVectors;
208        int32_t mMaxVertexTextureUnits;
209
210        bool OES_texture_npot;
211    } mGL;
212
213    uint32_t mWidth;
214    uint32_t mHeight;
215    int32_t mThreadPriority;
216    bool mIsGraphicsContext;
217
218    bool mRunning;
219    bool mExit;
220    bool mUseDepth;
221    bool mPaused;
222    RsError mError;
223    const char *mErrorMsg;
224
225    pthread_t mThreadId;
226    pid_t mNativeThreadId;
227
228    struct Workers {
229        volatile int mRunningCount;
230        volatile int mLaunchCount;
231        uint32_t mCount;
232        pthread_t *mThreadId;
233        pid_t *mNativeThreadId;
234        Signal mCompleteSignal;
235
236        Signal *mLaunchSignals;
237        WorkerCallback_t mLaunchCallback;
238        void *mLaunchData;
239    };
240    Workers mWorkers;
241
242    ObjectBaseRef<Script> mRootScript;
243    ObjectBaseRef<ProgramFragment> mFragment;
244    ObjectBaseRef<ProgramVertex> mVertex;
245    ObjectBaseRef<ProgramStore> mFragmentStore;
246    ObjectBaseRef<ProgramRaster> mRaster;
247    ObjectBaseRef<Font> mFont;
248
249    struct ObjDestroyOOB {
250        Mutex mMutex;
251        Vector<ObjectBase *> mDestroyList;
252        bool mNeedToEmpty;
253    };
254    ObjDestroyOOB mObjDestroy;
255    bool objDestroyOOBInit();
256    void objDestroyOOBRun();
257    void objDestroyOOBDestroy();
258
259private:
260    Context();
261
262    void initEGL(bool useGL2);
263    void deinitEGL();
264
265    uint32_t runRootScript();
266
267    static void * threadProc(void *);
268    static void * helperThreadProc(void *);
269
270    ANativeWindow *mWndSurface;
271
272    Vector<ObjectBase *> mNames;
273
274    uint64_t mTimers[_RS_TIMER_TOTAL];
275    Timers mTimerActive;
276    uint64_t mTimeLast;
277    uint64_t mTimeFrame;
278    uint64_t mTimeLastFrame;
279    uint32_t mTimeMSLastFrame;
280    uint32_t mTimeMSLastScript;
281    uint32_t mTimeMSLastSwap;
282};
283
284}
285}
286#endif
287