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