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