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