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