rsContext.h revision afb743aca56c18beb7ab924e75cb6e070ef3e55a
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 "rsFont.h"
34#include "rsProgramFragment.h"
35#include "rsProgramStore.h"
36#include "rsProgramRaster.h"
37#include "rsProgramVertex.h"
38#include "rsShaderCache.h"
39#include "rsVertexArray.h"
40
41#include "rsgApiStructs.h"
42#include "rsLocklessFifo.h"
43
44#include <ui/egl/android_natives.h>
45
46// ---------------------------------------------------------------------------
47namespace android {
48
49namespace renderscript {
50
51#if 0
52#define CHECK_OBJ(o) { \
53    GET_TLS(); \
54    if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
55        LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
56    } \
57}
58#define CHECK_OBJ_OR_NULL(o) { \
59    GET_TLS(); \
60    if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
61        LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
62    } \
63}
64#else
65#define CHECK_OBJ(o)
66#define CHECK_OBJ_OR_NULL(o)
67#endif
68
69class Context {
70public:
71    static Context * createContext(Device *, const RsSurfaceConfig *sc);
72    ~Context();
73
74    static pthread_key_t gThreadTLSKey;
75    static uint32_t gThreadTLSKeyCount;
76    static uint32_t gGLContextCount;
77    static pthread_mutex_t gInitMutex;
78
79    struct ScriptTLSStruct {
80        Context * mContext;
81        Script * mScript;
82    };
83    ScriptTLSStruct *mTlsStruct;
84    RsSurfaceConfig mUserSurfaceConfig;
85
86    typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
87
88    //StructuredAllocationContext mStateAllocation;
89    ElementState mStateElement;
90    TypeState mStateType;
91    SamplerState mStateSampler;
92    ProgramFragmentState mStateFragment;
93    ProgramStoreState mStateFragmentStore;
94    ProgramRasterState mStateRaster;
95    ProgramVertexState mStateVertex;
96    VertexArrayState mStateVertexArray;
97    FontState mStateFont;
98
99    ScriptCState mScriptC;
100    ShaderCache mShaderCache;
101
102    void swapBuffers();
103    void setRootScript(Script *);
104    void setRaster(ProgramRaster *);
105    void setVertex(ProgramVertex *);
106    void setFragment(ProgramFragment *);
107    void setFragmentStore(ProgramStore *);
108    void setFont(Font *);
109
110    void updateSurface(void *sur);
111
112    const ProgramFragment * getFragment() {return mFragment.get();}
113    const ProgramStore * getFragmentStore() {return mFragmentStore.get();}
114    const ProgramRaster * getRaster() {return mRaster.get();}
115    const ProgramVertex * getVertex() {return mVertex.get();}
116    Font * getFont() {return mFont.get();}
117
118    bool setupCheck();
119    void setupProgramStore();
120    bool checkDriver() const {return mEGL.mSurface != 0;}
121
122    void pause();
123    void resume();
124    void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
125    void setPriority(int32_t p);
126
127    void assignName(ObjectBase *obj, const char *name, uint32_t len);
128    void removeName(ObjectBase *obj);
129
130    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
131    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
132    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace);
133    uint32_t runScript(Script *s);
134
135    void initToClient();
136    void deinitToClient();
137
138    ProgramFragment * getDefaultProgramFragment() const {
139        return mStateFragment.mDefault.get();
140    }
141    ProgramVertex * getDefaultProgramVertex() const {
142        return mStateVertex.mDefault.get();
143    }
144    ProgramStore * getDefaultProgramStore() const {
145        return mStateFragmentStore.mDefault.get();
146    }
147    ProgramRaster * getDefaultProgramRaster() const {
148        return mStateRaster.mDefault.get();
149    }
150    Font* getDefaultFont() const {
151        return mStateFont.mDefault.get();
152    }
153
154    uint32_t getWidth() const {return mWidth;}
155    uint32_t getHeight() const {return mHeight;}
156
157    ThreadIO mIO;
158
159    // Timers
160    enum Timers {
161        RS_TIMER_IDLE,
162        RS_TIMER_INTERNAL,
163        RS_TIMER_SCRIPT,
164        RS_TIMER_CLEAR_SWAP,
165        _RS_TIMER_TOTAL
166    };
167    uint64_t getTime() const;
168    void timerInit();
169    void timerReset();
170    void timerSet(Timers);
171    void timerPrint();
172    void timerFrame();
173
174    struct {
175        bool mLogTimes;
176        bool mLogScripts;
177        bool mLogObjects;
178        bool mLogShaders;
179        bool mLogShadersAttr;
180        bool mLogShadersUniforms;
181        bool mLogVisual;
182    } props;
183
184    void dumpDebug() const;
185    void checkError(const char *) const;
186    const char * getError(RsError *);
187    void setError(RsError e, const char *msg = NULL);
188
189    mutable const ObjectBase * mObjHead;
190
191    bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
192    bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
193    float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
194    uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
195    uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
196    uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
197
198    void launchThreads(WorkerCallback_t cbk, void *data);
199    uint32_t getWorkerPoolSize() const {return (uint32_t)mWorkers.mCount;}
200
201protected:
202    Device *mDev;
203
204    struct {
205        EGLint mNumConfigs;
206        EGLint mMajorVersion;
207        EGLint mMinorVersion;
208        EGLConfig mConfig;
209        EGLContext mContext;
210        EGLSurface mSurface;
211        EGLSurface mSurfaceDefault;
212        EGLDisplay mDisplay;
213    } mEGL;
214
215    struct {
216        const uint8_t * mVendor;
217        const uint8_t * mRenderer;
218        const uint8_t * mVersion;
219        const uint8_t * mExtensions;
220
221        uint32_t mMajorVersion;
222        uint32_t mMinorVersion;
223
224        int32_t mMaxVaryingVectors;
225        int32_t mMaxTextureImageUnits;
226
227        int32_t mMaxFragmentTextureImageUnits;
228        int32_t mMaxFragmentUniformVectors;
229
230        int32_t mMaxVertexAttribs;
231        int32_t mMaxVertexUniformVectors;
232        int32_t mMaxVertexTextureUnits;
233
234        bool OES_texture_npot;
235        bool GL_NV_texture_npot_2D_mipmap;
236        float EXT_texture_max_aniso;
237    } mGL;
238
239    uint32_t mWidth;
240    uint32_t mHeight;
241    int32_t mThreadPriority;
242    bool mIsGraphicsContext;
243
244    bool mRunning;
245    bool mExit;
246    bool mPaused;
247    RsError mError;
248    const char *mErrorMsg;
249
250    pthread_t mThreadId;
251    pid_t mNativeThreadId;
252
253    struct Workers {
254        volatile int mRunningCount;
255        volatile int mLaunchCount;
256        uint32_t mCount;
257        pthread_t *mThreadId;
258        pid_t *mNativeThreadId;
259        Signal mCompleteSignal;
260
261        Signal *mLaunchSignals;
262        WorkerCallback_t mLaunchCallback;
263        void *mLaunchData;
264    };
265    Workers mWorkers;
266
267    ObjectBaseRef<Script> mRootScript;
268    ObjectBaseRef<ProgramFragment> mFragment;
269    ObjectBaseRef<ProgramVertex> mVertex;
270    ObjectBaseRef<ProgramStore> mFragmentStore;
271    ObjectBaseRef<ProgramRaster> mRaster;
272    ObjectBaseRef<Font> mFont;
273
274    void displayDebugStats();
275
276private:
277    Context();
278    bool initContext(Device *, const RsSurfaceConfig *sc);
279
280
281    bool initGLThread();
282    void deinitEGL();
283
284    uint32_t runRootScript();
285
286    static void * threadProc(void *);
287    static void * helperThreadProc(void *);
288
289    ANativeWindow *mWndSurface;
290
291    Vector<ObjectBase *> mNames;
292
293    uint64_t mTimers[_RS_TIMER_TOTAL];
294    Timers mTimerActive;
295    uint64_t mTimeLast;
296    uint64_t mTimeFrame;
297    uint64_t mTimeLastFrame;
298    uint32_t mTimeMSLastFrame;
299    uint32_t mTimeMSLastScript;
300    uint32_t mTimeMSLastSwap;
301    uint32_t mAverageFPSFrameCount;
302    uint64_t mAverageFPSStartTime;
303    uint32_t mAverageFPS;
304};
305
306}
307}
308#endif
309