rsContext.h revision 5613b91c4f3b9a931ee9282317ea887007e05b86
1/*
2 * Copyright (C) 2009 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
22#include <ui/Surface.h>
23
24#include "rsThreadIO.h"
25#include "rsType.h"
26#include "rsMatrix.h"
27#include "rsAllocation.h"
28#include "rsSimpleMesh.h"
29#include "rsMesh.h"
30#include "rsDevice.h"
31#include "rsScriptC.h"
32#include "rsAllocation.h"
33#include "rsAdapter.h"
34#include "rsSampler.h"
35#include "rsLight.h"
36#include "rsProgramFragment.h"
37#include "rsProgramFragmentStore.h"
38#include "rsProgramRaster.h"
39#include "rsProgramVertex.h"
40#include "rsShaderCache.h"
41#include "rsVertexArray.h"
42
43#include "rsgApiStructs.h"
44#include "rsLocklessFifo.h"
45
46
47// ---------------------------------------------------------------------------
48namespace android {
49namespace renderscript {
50
51class Context
52{
53public:
54    Context(Device *, bool useDepth);
55    ~Context();
56
57    static pthread_key_t gThreadTLSKey;
58    static uint32_t gThreadTLSKeyCount;
59    static uint32_t gGLContextCount;
60    static pthread_mutex_t gInitMutex;
61
62    struct ScriptTLSStruct {
63        Context * mContext;
64        Script * mScript;
65    };
66
67
68    //StructuredAllocationContext mStateAllocation;
69    ElementState mStateElement;
70    TypeState mStateType;
71    SamplerState mStateSampler;
72    ProgramFragmentState mStateFragment;
73    ProgramFragmentStoreState mStateFragmentStore;
74    ProgramRasterState mStateRaster;
75    ProgramVertexState mStateVertex;
76    LightState mStateLight;
77    VertexArrayState mStateVertexArray;
78
79    ScriptCState mScriptC;
80    ShaderCache mShaderCache;
81
82    void swapBuffers();
83    void setRootScript(Script *);
84    void setRaster(ProgramRaster *);
85    void setVertex(ProgramVertex *);
86    void setFragment(ProgramFragment *);
87    void setFragmentStore(ProgramFragmentStore *);
88
89    void updateSurface(void *sur);
90
91    const ProgramFragment * getFragment() {return mFragment.get();}
92    const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
93    const ProgramRaster * getRaster() {return mRaster.get();}
94    const ProgramVertex * getVertex() {return mVertex.get();}
95
96    void setupCheck();
97
98    void pause();
99    void resume();
100    void setSurface(uint32_t w, uint32_t h, Surface *sur);
101    void setPriority(int32_t p);
102
103    void assignName(ObjectBase *obj, const char *name, uint32_t len);
104    void removeName(ObjectBase *obj);
105    ObjectBase * lookupName(const char *name) const;
106    void appendNameDefines(String8 *str) const;
107    void appendVarDefines(String8 *str) const;
108
109    uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
110    bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
111    uint32_t runScript(Script *s, uint32_t launchID);
112
113    void initToClient();
114    void deinitToClient();
115
116    ProgramFragment * getDefaultProgramFragment() const {
117        return mStateFragment.mDefault.get();
118    }
119    ProgramVertex * getDefaultProgramVertex() const {
120        return mStateVertex.mDefault.get();
121    }
122    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
123        return mStateFragmentStore.mDefault.get();
124    }
125    ProgramRaster * getDefaultProgramRaster() const {
126        return mStateRaster.mDefault.get();
127    }
128
129    void addInt32Define(const char* name, int32_t value) {
130        mInt32Defines.add(String8(name), value);
131    }
132
133    void addFloatDefine(const char* name, float value) {
134        mFloatDefines.add(String8(name), value);
135    }
136
137    uint32_t getWidth() const {return mEGL.mWidth;}
138    uint32_t getHeight() const {return mEGL.mHeight;}
139
140
141    ThreadIO mIO;
142    void objDestroyAdd(ObjectBase *);
143
144    // Timers
145    enum Timers {
146        RS_TIMER_IDLE,
147        RS_TIMER_INTERNAL,
148        RS_TIMER_SCRIPT,
149        RS_TIMER_CLEAR_SWAP,
150        _RS_TIMER_TOTAL
151    };
152    uint64_t getTime() const;
153    void timerInit();
154    void timerReset();
155    void timerSet(Timers);
156    void timerPrint();
157    void timerFrame();
158
159    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
160    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
161
162    struct {
163        bool mLogTimes;
164        bool mLogScripts;
165        bool mLogObjects;
166    } props;
167
168    void dumpDebug() const;
169
170    mutable const ObjectBase * mObjHead;
171
172protected:
173    Device *mDev;
174
175    struct {
176        EGLint mNumConfigs;
177        EGLint mMajorVersion;
178        EGLint mMinorVersion;
179        EGLConfig mConfig;
180        EGLContext mContext;
181        EGLSurface mSurface;
182        EGLint mWidth;
183        EGLint mHeight;
184        EGLDisplay mDisplay;
185    } mEGL;
186
187    struct {
188        const uint8_t * mVendor;
189        const uint8_t * mRenderer;
190        const uint8_t * mVersion;
191        const uint8_t * mExtensions;
192
193        uint32_t mMajorVersion;
194        uint32_t mMinorVersion;
195
196    } mGL;
197
198    uint32_t mWidth;
199    uint32_t mHeight;
200    int32_t mThreadPriority;
201
202    bool mRunning;
203    bool mExit;
204    bool mUseDepth;
205    bool mPaused;
206
207    pthread_t mThreadId;
208    pid_t mNativeThreadId;
209
210    ObjectBaseRef<Script> mRootScript;
211    ObjectBaseRef<ProgramFragment> mFragment;
212    ObjectBaseRef<ProgramVertex> mVertex;
213    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
214    ObjectBaseRef<ProgramRaster> mRaster;
215
216
217    struct ObjDestroyOOB {
218        pthread_mutex_t mMutex;
219        Vector<ObjectBase *> mDestroyList;
220        bool mNeedToEmpty;
221    };
222    ObjDestroyOOB mObjDestroy;
223    bool objDestroyOOBInit();
224    void objDestroyOOBRun();
225    void objDestroyOOBDestroy();
226
227private:
228    Context();
229
230    void initEGL(bool useGL2);
231    void deinitEGL();
232
233    uint32_t runRootScript();
234
235    static void * threadProc(void *);
236
237    Surface *mWndSurface;
238
239    Vector<ObjectBase *> mNames;
240    KeyedVector<String8,int> mInt32Defines;
241    KeyedVector<String8,float> mFloatDefines;
242
243    uint64_t mTimers[_RS_TIMER_TOTAL];
244    Timers mTimerActive;
245    uint64_t mTimeLast;
246    uint64_t mTimeFrame;
247    uint64_t mTimeLastFrame;
248    uint32_t mTimeMSLastFrame;
249    uint32_t mTimeMSLastScript;
250    uint32_t mTimeMSLastSwap;
251};
252
253}
254}
255#endif
256