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