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