rsContext.h revision 93eacc7ce0aad4314b4cb41a281f59ce54bb3286
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 gInitMutex;
79    // Library mutex (for providing thread-safe calls from the runtime)
80    static pthread_mutex_t gLibMutex;
81
82    class PushState {
83    public:
84        PushState(Context *);
85        ~PushState();
86
87    private:
88#ifndef RS_COMPATIBILITY_LIB
89        ObjectBaseRef<ProgramFragment> mFragment;
90        ObjectBaseRef<ProgramVertex> mVertex;
91        ObjectBaseRef<ProgramStore> mStore;
92        ObjectBaseRef<ProgramRaster> mRaster;
93        ObjectBaseRef<Font> mFont;
94#endif
95        Context *mRsc;
96    };
97
98    RsSurfaceConfig mUserSurfaceConfig;
99
100    ElementState mStateElement;
101    TypeState mStateType;
102    SamplerState mStateSampler;
103
104    ScriptCState mScriptC;
105    bool isSynchronous() {return mSynchronous;}
106    bool setupCheck();
107
108#ifndef RS_COMPATIBILITY_LIB
109    FBOCache mFBOCache;
110    ProgramFragmentState mStateFragment;
111    ProgramStoreState mStateFragmentStore;
112    ProgramRasterState mStateRaster;
113    ProgramVertexState mStateVertex;
114    FontState mStateFont;
115
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    void setupProgramStore();
134
135    void pause();
136    void resume();
137    void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
138#endif
139
140    void setPriority(int32_t p);
141    void destroyWorkerThreadResources();
142
143    void assignName(ObjectBase *obj, const char *name, uint32_t len);
144    void removeName(ObjectBase *obj);
145
146    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
147    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
148    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
149    uint32_t runScript(Script *s);
150
151    void initToClient();
152    void deinitToClient();
153
154#ifndef RS_COMPATIBILITY_LIB
155    ProgramFragment * getDefaultProgramFragment() const {
156        return mStateFragment.mDefault.get();
157    }
158    ProgramVertex * getDefaultProgramVertex() const {
159        return mStateVertex.mDefault.get();
160    }
161    ProgramStore * getDefaultProgramStore() const {
162        return mStateFragmentStore.mDefault.get();
163    }
164    ProgramRaster * getDefaultProgramRaster() const {
165        return mStateRaster.mDefault.get();
166    }
167    Font* getDefaultFont() const {
168        return mStateFont.mDefault.get();
169    }
170
171    uint32_t getWidth() const {return mWidth;}
172    uint32_t getHeight() const {return mHeight;}
173
174    uint32_t getCurrentSurfaceWidth() const;
175    uint32_t getCurrentSurfaceHeight() const;
176
177    void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
178        watchdog.command = cmd;
179        watchdog.file = file;
180        watchdog.line = line;
181    }
182#endif
183
184    mutable ThreadIO mIO;
185
186    // Timers
187    enum Timers {
188        RS_TIMER_IDLE,
189        RS_TIMER_INTERNAL,
190        RS_TIMER_SCRIPT,
191        RS_TIMER_CLEAR_SWAP,
192        _RS_TIMER_TOTAL
193    };
194    uint64_t getTime() const;
195    void timerInit();
196    void timerReset();
197    void timerSet(Timers);
198    void timerPrint();
199    void timerFrame();
200
201    struct {
202        bool mLogTimes;
203        bool mLogScripts;
204        bool mLogObjects;
205        bool mLogShaders;
206        bool mLogShadersAttr;
207        bool mLogShadersUniforms;
208        bool mLogVisual;
209        uint32_t mDebugMaxThreads;
210    } props;
211
212    mutable struct {
213        bool inRoot;
214        const char *command;
215        const char *file;
216        uint32_t line;
217    } watchdog;
218    static void printWatchdogInfo(void *ctx);
219
220    void dumpDebug() const;
221    void setError(RsError e, const char *msg = NULL) const;
222
223    mutable const ObjectBase * mObjHead;
224
225    uint32_t getDPI() const {return mDPI;}
226    void setDPI(uint32_t dpi) {mDPI = dpi;}
227
228    uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
229    void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
230
231    Device *mDev;
232protected:
233
234    uint32_t mTargetSdkVersion;
235    uint32_t mDPI;
236    uint32_t mWidth;
237    uint32_t mHeight;
238    int32_t mThreadPriority;
239    bool mIsGraphicsContext;
240
241    bool mForceCpu;
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#ifndef RS_COMPATIBILITY_LIB
253    ObjectBaseRef<ProgramFragment> mFragment;
254    ObjectBaseRef<ProgramVertex> mVertex;
255    ObjectBaseRef<ProgramStore> mFragmentStore;
256    ObjectBaseRef<ProgramRaster> mRaster;
257    ObjectBaseRef<Font> mFont;
258#endif
259
260    void displayDebugStats();
261
262private:
263    Context();
264    bool initContext(Device *, const RsSurfaceConfig *sc);
265
266    bool mSynchronous;
267    bool initGLThread();
268    void deinitEGL();
269
270    uint32_t runRootScript();
271
272    static bool loadRuntime(const char* filename, Context* rsc);
273    static void * threadProc(void *);
274    static void * helperThreadProc(void *);
275
276    bool mHasSurface;
277    bool mIsContextLite;
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} // renderscript
295} // android
296#endif
297