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