rsContext.h revision b81a0eb8180791e4eaab1253b59fa8bd562b046b
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    Device *mDev;
203protected:
204
205    uint32_t mDPI;
206    uint32_t mWidth;
207    uint32_t mHeight;
208    int32_t mThreadPriority;
209    bool mIsGraphicsContext;
210
211    bool mRunning;
212    bool mExit;
213    bool mPaused;
214    mutable RsError mError;
215
216    pthread_t mThreadId;
217    pid_t mNativeThreadId;
218
219    ObjectBaseRef<Script> mRootScript;
220    ObjectBaseRef<ProgramFragment> mFragment;
221    ObjectBaseRef<ProgramVertex> mVertex;
222    ObjectBaseRef<ProgramStore> mFragmentStore;
223    ObjectBaseRef<ProgramRaster> mRaster;
224    ObjectBaseRef<Font> mFont;
225
226    void displayDebugStats();
227
228private:
229    Context();
230    bool initContext(Device *, const RsSurfaceConfig *sc);
231
232
233    bool initGLThread();
234    void deinitEGL();
235
236    uint32_t runRootScript();
237
238    static void * threadProc(void *);
239    static void * helperThreadProc(void *);
240
241    bool mHasSurface;
242    bool mIsContextLite;
243
244    Vector<ObjectBase *> mNames;
245
246    uint64_t mTimers[_RS_TIMER_TOTAL];
247    Timers mTimerActive;
248    uint64_t mTimeLast;
249    uint64_t mTimeFrame;
250    uint64_t mTimeLastFrame;
251    uint32_t mTimeMSLastFrame;
252    uint32_t mTimeMSLastScript;
253    uint32_t mTimeMSLastSwap;
254    uint32_t mAverageFPSFrameCount;
255    uint64_t mAverageFPSStartTime;
256    uint32_t mAverageFPS;
257};
258
259} // renderscript
260} // android
261#endif
262