rsContext.h revision 4820e8bb83b1f78e6232ebe853221f737da2a1ea
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 isGraphics, 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    bool checkDriver() const {return mEGL.mSurface != 0;}
98
99    void pause();
100    void resume();
101    void setSurface(uint32_t w, uint32_t h, Surface *sur);
102    void setPriority(int32_t p);
103
104    void assignName(ObjectBase *obj, const char *name, uint32_t len);
105    void removeName(ObjectBase *obj);
106    ObjectBase * lookupName(const char *name) const;
107    void appendNameDefines(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    uint32_t getWidth() const {return mEGL.mWidth;}
130    uint32_t getHeight() const {return mEGL.mHeight;}
131
132
133    ThreadIO mIO;
134    void objDestroyAdd(ObjectBase *);
135
136    // Timers
137    enum Timers {
138        RS_TIMER_IDLE,
139        RS_TIMER_INTERNAL,
140        RS_TIMER_SCRIPT,
141        RS_TIMER_CLEAR_SWAP,
142        _RS_TIMER_TOTAL
143    };
144    uint64_t getTime() const;
145    void timerInit();
146    void timerReset();
147    void timerSet(Timers);
148    void timerPrint();
149    void timerFrame();
150
151    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
152    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
153
154    struct {
155        bool mLogTimes;
156        bool mLogScripts;
157        bool mLogObjects;
158        bool mLogShaders;
159    } props;
160
161    void dumpDebug() const;
162    void checkError(const char *) const;
163
164    mutable const ObjectBase * mObjHead;
165
166protected:
167    Device *mDev;
168
169    struct {
170        EGLint mNumConfigs;
171        EGLint mMajorVersion;
172        EGLint mMinorVersion;
173        EGLConfig mConfig;
174        EGLContext mContext;
175        EGLSurface mSurface;
176        EGLint mWidth;
177        EGLint mHeight;
178        EGLDisplay mDisplay;
179    } mEGL;
180
181    struct {
182        const uint8_t * mVendor;
183        const uint8_t * mRenderer;
184        const uint8_t * mVersion;
185        const uint8_t * mExtensions;
186
187        uint32_t mMajorVersion;
188        uint32_t mMinorVersion;
189
190        int32_t mMaxVaryingVectors;
191        int32_t mMaxTextureImageUnits;
192
193        int32_t mMaxFragmentTextureImageUnits;
194        int32_t mMaxFragmentUniformVectors;
195
196        int32_t mMaxVertexAttribs;
197        int32_t mMaxVertexUniformVectors;
198        int32_t mMaxVertexTextureUnits;
199    } mGL;
200
201    uint32_t mWidth;
202    uint32_t mHeight;
203    int32_t mThreadPriority;
204    bool mIsGraphicsContext;
205
206    bool mRunning;
207    bool mExit;
208    bool mUseDepth;
209    bool mPaused;
210
211    pthread_t mThreadId;
212    pid_t mNativeThreadId;
213
214    ObjectBaseRef<Script> mRootScript;
215    ObjectBaseRef<ProgramFragment> mFragment;
216    ObjectBaseRef<ProgramVertex> mVertex;
217    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
218    ObjectBaseRef<ProgramRaster> mRaster;
219
220
221    struct ObjDestroyOOB {
222        pthread_mutex_t mMutex;
223        Vector<ObjectBase *> mDestroyList;
224        bool mNeedToEmpty;
225    };
226    ObjDestroyOOB mObjDestroy;
227    bool objDestroyOOBInit();
228    void objDestroyOOBRun();
229    void objDestroyOOBDestroy();
230
231private:
232    Context();
233
234    void initEGL(bool useGL2);
235    void deinitEGL();
236
237    uint32_t runRootScript();
238
239    static void * threadProc(void *);
240
241    Surface *mWndSurface;
242
243    Vector<ObjectBase *> mNames;
244
245    uint64_t mTimers[_RS_TIMER_TOTAL];
246    Timers mTimerActive;
247    uint64_t mTimeLast;
248    uint64_t mTimeFrame;
249    uint64_t mTimeLastFrame;
250    uint32_t mTimeMSLastFrame;
251    uint32_t mTimeMSLastScript;
252    uint32_t mTimeMSLastSwap;
253};
254
255}
256}
257#endif
258