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