rsContext.h revision a04e30dbb5ab11592b03666bb3d102070759c58e
1/*
2 * Copyright (C) 2011 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 "rsFBOCache.h"
41
42#include "rsgApiStructs.h"
43#include "rsLocklessFifo.h"
44
45#include <ui/egl/android_natives.h>
46#endif // ANDROID_RS_SERIALIZE
47
48// ---------------------------------------------------------------------------
49namespace android {
50
51namespace renderscript {
52
53#if 0
54#define CHECK_OBJ(o) { \
55    GET_TLS(); \
56    if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
57        LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
58    } \
59}
60#define CHECK_OBJ_OR_NULL(o) { \
61    GET_TLS(); \
62    if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
63        LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
64    } \
65}
66#else
67#define CHECK_OBJ(o)
68#define CHECK_OBJ_OR_NULL(o)
69#endif
70
71#ifndef ANDROID_RS_SERIALIZE
72
73class Context {
74public:
75    struct Hal {
76        void * drv;
77
78        RsdHalFunctions funcs;
79    };
80    Hal mHal;
81
82    static Context * createContext(Device *, const RsSurfaceConfig *sc);
83    ~Context();
84
85    static pthread_mutex_t gInitMutex;
86    // Library mutex (for providing thread-safe calls from the runtime)
87    static pthread_mutex_t gLibMutex;
88
89    class PushState {
90    public:
91        PushState(Context *);
92        ~PushState();
93
94    private:
95        ObjectBaseRef<ProgramFragment> mFragment;
96        ObjectBaseRef<ProgramVertex> mVertex;
97        ObjectBaseRef<ProgramStore> mStore;
98        ObjectBaseRef<ProgramRaster> mRaster;
99        ObjectBaseRef<Font> mFont;
100        Context *mRsc;
101    };
102
103    RsSurfaceConfig mUserSurfaceConfig;
104
105    ElementState mStateElement;
106    TypeState mStateType;
107    SamplerState mStateSampler;
108    ProgramFragmentState mStateFragment;
109    ProgramStoreState mStateFragmentStore;
110    ProgramRasterState mStateRaster;
111    ProgramVertexState mStateVertex;
112    FontState mStateFont;
113
114    ScriptCState mScriptC;
115    FBOCache mFBOCache;
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
136    void pause();
137    void resume();
138    void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
139    void setPriority(int32_t p);
140    void destroyWorkerThreadResources();
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_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
207    bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
208    float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
209    uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
210    uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
211    uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
212    uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
213
214    uint32_t getDPI() const {return mDPI;}
215    void setDPI(uint32_t dpi) {mDPI = dpi;}
216
217    Device *mDev;
218protected:
219
220    struct {
221        int32_t mMaxVaryingVectors;
222        int32_t mMaxTextureImageUnits;
223
224        int32_t mMaxFragmentTextureImageUnits;
225        int32_t mMaxFragmentUniformVectors;
226
227        int32_t mMaxVertexAttribs;
228        int32_t mMaxVertexUniformVectors;
229        int32_t mMaxVertexTextureUnits;
230
231        bool OES_texture_npot;
232        bool GL_IMG_texture_npot;
233        bool GL_NV_texture_npot_2D_mipmap;
234        float EXT_texture_max_aniso;
235    } mGL;
236
237    uint32_t mDPI;
238    uint32_t mWidth;
239    uint32_t mHeight;
240    int32_t mThreadPriority;
241    bool mIsGraphicsContext;
242
243    bool mRunning;
244    bool mExit;
245    bool mPaused;
246    mutable RsError mError;
247
248    pthread_t mThreadId;
249    pid_t mNativeThreadId;
250
251    ObjectBaseRef<Script> mRootScript;
252    ObjectBaseRef<ProgramFragment> mFragment;
253    ObjectBaseRef<ProgramVertex> mVertex;
254    ObjectBaseRef<ProgramStore> mFragmentStore;
255    ObjectBaseRef<ProgramRaster> mRaster;
256    ObjectBaseRef<Font> mFont;
257
258    void displayDebugStats();
259
260private:
261    Context();
262    bool initContext(Device *, const RsSurfaceConfig *sc);
263
264
265    bool initGLThread();
266    void deinitEGL();
267
268    uint32_t runRootScript();
269
270    static void * threadProc(void *);
271    static void * helperThreadProc(void *);
272
273    ANativeWindow *mWndSurface;
274
275    Vector<ObjectBase *> mNames;
276
277    uint64_t mTimers[_RS_TIMER_TOTAL];
278    Timers mTimerActive;
279    uint64_t mTimeLast;
280    uint64_t mTimeFrame;
281    uint64_t mTimeLastFrame;
282    uint32_t mTimeMSLastFrame;
283    uint32_t mTimeMSLastScript;
284    uint32_t mTimeMSLastSwap;
285    uint32_t mAverageFPSFrameCount;
286    uint64_t mAverageFPSStartTime;
287    uint32_t mAverageFPS;
288};
289
290#else
291
292class Context {
293public:
294    Context() {
295        mObjHead = NULL;
296    }
297    ~Context() {
298        ObjectBase::zeroAllUserRef(this);
299    }
300
301    ElementState mStateElement;
302    TypeState mStateType;
303
304    struct {
305        bool mLogTimes;
306        bool mLogScripts;
307        bool mLogObjects;
308        bool mLogShaders;
309        bool mLogShadersAttr;
310        bool mLogShadersUniforms;
311        bool mLogVisual;
312    } props;
313
314    void setError(RsError e, const char *msg = NULL) {  }
315
316    mutable const ObjectBase * mObjHead;
317
318protected:
319
320};
321#endif //ANDROID_RS_SERIALIZE
322
323} // renderscript
324} // android
325#endif
326