rsContext.h revision 83c451a4ef4388e002482e383d488ca9b7b7600d
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 "rsType.h"
22#include "rsAllocation.h"
23#include "rsMesh.h"
24
25#include "rs_hal.h"
26
27#ifndef ANDROID_RS_SERIALIZE
28#include "rsMutex.h"
29#include "rsThreadIO.h"
30#include "rsMatrix4x4.h"
31#include "rsDevice.h"
32#include "rsScriptC.h"
33#include "rsAdapter.h"
34#include "rsSampler.h"
35#include "rsFont.h"
36#include "rsProgramFragment.h"
37#include "rsProgramStore.h"
38#include "rsProgramRaster.h"
39#include "rsProgramVertex.h"
40#include "rsShaderCache.h"
41#include "rsFBOCache.h"
42#include "rsVertexArray.h"
43
44#include "rsgApiStructs.h"
45#include "rsLocklessFifo.h"
46
47#include <ui/egl/android_natives.h>
48#endif // ANDROID_RS_SERIALIZE
49
50// ---------------------------------------------------------------------------
51namespace android {
52
53namespace renderscript {
54
55#if 0
56#define CHECK_OBJ(o) { \
57    GET_TLS(); \
58    if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
59        LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
60    } \
61}
62#define CHECK_OBJ_OR_NULL(o) { \
63    GET_TLS(); \
64    if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
65        LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
66    } \
67}
68#else
69#define CHECK_OBJ(o)
70#define CHECK_OBJ_OR_NULL(o)
71#endif
72
73#ifndef ANDROID_RS_SERIALIZE
74
75class Context {
76public:
77    struct Hal {
78        void * drv;
79
80        RsdHalFunctions funcs;
81    };
82    Hal mHal;
83
84    static Context * createContext(Device *, const RsSurfaceConfig *sc);
85    ~Context();
86
87    static pthread_mutex_t gInitMutex;
88    // Library mutex (for providing thread-safe calls from the runtime)
89    static pthread_mutex_t gLibMutex;
90
91    class PushState {
92    public:
93        PushState(Context *);
94        ~PushState();
95
96    private:
97        ObjectBaseRef<ProgramFragment> mFragment;
98        ObjectBaseRef<ProgramVertex> mVertex;
99        ObjectBaseRef<ProgramStore> mStore;
100        ObjectBaseRef<ProgramRaster> mRaster;
101        ObjectBaseRef<Font> mFont;
102        Context *mRsc;
103    };
104
105    RsSurfaceConfig mUserSurfaceConfig;
106
107    ElementState mStateElement;
108    TypeState mStateType;
109    SamplerState mStateSampler;
110    ProgramFragmentState mStateFragment;
111    ProgramStoreState mStateFragmentStore;
112    ProgramRasterState mStateRaster;
113    ProgramVertexState mStateVertex;
114    VertexArrayState mStateVertexArray;
115    FontState mStateFont;
116
117    ScriptCState mScriptC;
118    ShaderCache mShaderCache;
119    FBOCache mFBOCache;
120
121    void swapBuffers();
122    void setRootScript(Script *);
123    void setProgramRaster(ProgramRaster *);
124    void setProgramVertex(ProgramVertex *);
125    void setProgramFragment(ProgramFragment *);
126    void setProgramStore(ProgramStore *);
127    void setFont(Font *);
128
129    void updateSurface(void *sur);
130
131    ProgramFragment * getProgramFragment() {return mFragment.get();}
132    ProgramStore * getProgramStore() {return mFragmentStore.get();}
133    ProgramRaster * getProgramRaster() {return mRaster.get();}
134    ProgramVertex * getProgramVertex() {return mVertex.get();}
135    Font * getFont() {return mFont.get();}
136
137    bool setupCheck();
138    void setupProgramStore();
139
140    void pause();
141    void resume();
142    void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
143    void setPriority(int32_t p);
144    void destroyWorkerThreadResources();
145
146    void assignName(ObjectBase *obj, const char *name, uint32_t len);
147    void removeName(ObjectBase *obj);
148
149    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
150    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
151    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
152    uint32_t runScript(Script *s);
153
154    void initToClient();
155    void deinitToClient();
156
157    ProgramFragment * getDefaultProgramFragment() const {
158        return mStateFragment.mDefault.get();
159    }
160    ProgramVertex * getDefaultProgramVertex() const {
161        return mStateVertex.mDefault.get();
162    }
163    ProgramStore * getDefaultProgramStore() const {
164        return mStateFragmentStore.mDefault.get();
165    }
166    ProgramRaster * getDefaultProgramRaster() const {
167        return mStateRaster.mDefault.get();
168    }
169    Font* getDefaultFont() const {
170        return mStateFont.mDefault.get();
171    }
172
173    uint32_t getWidth() const {return mWidth;}
174    uint32_t getHeight() const {return mHeight;}
175
176    mutable ThreadIO mIO;
177
178    // Timers
179    enum Timers {
180        RS_TIMER_IDLE,
181        RS_TIMER_INTERNAL,
182        RS_TIMER_SCRIPT,
183        RS_TIMER_CLEAR_SWAP,
184        _RS_TIMER_TOTAL
185    };
186    uint64_t getTime() const;
187    void timerInit();
188    void timerReset();
189    void timerSet(Timers);
190    void timerPrint();
191    void timerFrame();
192
193    struct {
194        bool mLogTimes;
195        bool mLogScripts;
196        bool mLogObjects;
197        bool mLogShaders;
198        bool mLogShadersAttr;
199        bool mLogShadersUniforms;
200        bool mLogVisual;
201    } props;
202
203    void dumpDebug() const;
204    void checkError(const char *, bool isFatal = false) const;
205    void setError(RsError e, const char *msg = NULL) const;
206
207    mutable const ObjectBase * mObjHead;
208
209    bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
210    bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
211    bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
212    float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
213    uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
214    uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
215    uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
216    uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
217
218    uint32_t getDPI() const {return mDPI;}
219    void setDPI(uint32_t dpi) {mDPI = dpi;}
220
221    Device *mDev;
222protected:
223
224    struct {
225        int32_t mMaxVaryingVectors;
226        int32_t mMaxTextureImageUnits;
227
228        int32_t mMaxFragmentTextureImageUnits;
229        int32_t mMaxFragmentUniformVectors;
230
231        int32_t mMaxVertexAttribs;
232        int32_t mMaxVertexUniformVectors;
233        int32_t mMaxVertexTextureUnits;
234
235        bool OES_texture_npot;
236        bool GL_IMG_texture_npot;
237        bool GL_NV_texture_npot_2D_mipmap;
238        float EXT_texture_max_aniso;
239    } mGL;
240
241    uint32_t mDPI;
242    uint32_t mWidth;
243    uint32_t mHeight;
244    int32_t mThreadPriority;
245    bool mIsGraphicsContext;
246
247    bool mRunning;
248    bool mExit;
249    bool mPaused;
250    mutable RsError mError;
251
252    pthread_t mThreadId;
253    pid_t mNativeThreadId;
254
255    ObjectBaseRef<Script> mRootScript;
256    ObjectBaseRef<ProgramFragment> mFragment;
257    ObjectBaseRef<ProgramVertex> mVertex;
258    ObjectBaseRef<ProgramStore> mFragmentStore;
259    ObjectBaseRef<ProgramRaster> mRaster;
260    ObjectBaseRef<Font> mFont;
261
262    void displayDebugStats();
263
264private:
265    Context();
266    bool initContext(Device *, const RsSurfaceConfig *sc);
267
268
269    bool initGLThread();
270    void deinitEGL();
271
272    uint32_t runRootScript();
273
274    static void * threadProc(void *);
275    static void * helperThreadProc(void *);
276
277    ANativeWindow *mWndSurface;
278
279    Vector<ObjectBase *> mNames;
280
281    uint64_t mTimers[_RS_TIMER_TOTAL];
282    Timers mTimerActive;
283    uint64_t mTimeLast;
284    uint64_t mTimeFrame;
285    uint64_t mTimeLastFrame;
286    uint32_t mTimeMSLastFrame;
287    uint32_t mTimeMSLastScript;
288    uint32_t mTimeMSLastSwap;
289    uint32_t mAverageFPSFrameCount;
290    uint64_t mAverageFPSStartTime;
291    uint32_t mAverageFPS;
292};
293
294#else
295
296class Context {
297public:
298    Context() {
299        mObjHead = NULL;
300    }
301    ~Context() {
302        ObjectBase::zeroAllUserRef(this);
303    }
304
305    ElementState mStateElement;
306    TypeState mStateType;
307
308    struct {
309        bool mLogTimes;
310        bool mLogScripts;
311        bool mLogObjects;
312        bool mLogShaders;
313        bool mLogShadersAttr;
314        bool mLogShadersUniforms;
315        bool mLogVisual;
316    } props;
317
318    void setError(RsError e, const char *msg = NULL) {  }
319
320    mutable const ObjectBase * mObjHead;
321
322protected:
323
324};
325#endif //ANDROID_RS_SERIALIZE
326
327} // renderscript
328} // android
329#endif
330