rsContext.h revision 12b14ae9fa34f4fd0bf21a2a4ac95a4864248fe9
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#include "rsMutex.h"
22
23#include "rsThreadIO.h"
24#include "rsType.h"
25#include "rsMatrix.h"
26#include "rsAllocation.h"
27#include "rsSimpleMesh.h"
28#include "rsMesh.h"
29#include "rsDevice.h"
30#include "rsScriptC.h"
31#include "rsAllocation.h"
32#include "rsAdapter.h"
33#include "rsSampler.h"
34#include "rsLight.h"
35#include "rsProgramFragment.h"
36#include "rsProgramFragmentStore.h"
37#include "rsProgramRaster.h"
38#include "rsProgramVertex.h"
39#include "rsShaderCache.h"
40#include "rsVertexArray.h"
41
42#include "rsgApiStructs.h"
43#include "rsLocklessFifo.h"
44
45#include <ui/egl/android_natives.h>
46
47// ---------------------------------------------------------------------------
48namespace android {
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    bool 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, android_native_window_t *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    const char * getError(RsError *);
165    void setError(RsError e, const char *msg = NULL);
166
167    mutable const ObjectBase * mObjHead;
168
169    bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
170
171protected:
172    Device *mDev;
173
174    struct {
175        EGLint mNumConfigs;
176        EGLint mMajorVersion;
177        EGLint mMinorVersion;
178        EGLConfig mConfig;
179        EGLContext mContext;
180        EGLSurface mSurface;
181        EGLint mWidth;
182        EGLint mHeight;
183        EGLDisplay mDisplay;
184    } mEGL;
185
186    struct {
187        const uint8_t * mVendor;
188        const uint8_t * mRenderer;
189        const uint8_t * mVersion;
190        const uint8_t * mExtensions;
191
192        uint32_t mMajorVersion;
193        uint32_t mMinorVersion;
194
195        int32_t mMaxVaryingVectors;
196        int32_t mMaxTextureImageUnits;
197
198        int32_t mMaxFragmentTextureImageUnits;
199        int32_t mMaxFragmentUniformVectors;
200
201        int32_t mMaxVertexAttribs;
202        int32_t mMaxVertexUniformVectors;
203        int32_t mMaxVertexTextureUnits;
204
205        bool OES_texture_npot;
206    } mGL;
207
208    uint32_t mWidth;
209    uint32_t mHeight;
210    int32_t mThreadPriority;
211    bool mIsGraphicsContext;
212
213    bool mRunning;
214    bool mExit;
215    bool mUseDepth;
216    bool mPaused;
217    RsError mError;
218    const char *mErrorMsg;
219
220    pthread_t mThreadId;
221    pid_t mNativeThreadId;
222
223    ObjectBaseRef<Script> mRootScript;
224    ObjectBaseRef<ProgramFragment> mFragment;
225    ObjectBaseRef<ProgramVertex> mVertex;
226    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
227    ObjectBaseRef<ProgramRaster> mRaster;
228
229
230    struct ObjDestroyOOB {
231        Mutex mMutex;
232        Vector<ObjectBase *> mDestroyList;
233        bool mNeedToEmpty;
234    };
235    ObjDestroyOOB mObjDestroy;
236    bool objDestroyOOBInit();
237    void objDestroyOOBRun();
238    void objDestroyOOBDestroy();
239
240private:
241    Context();
242
243    void initEGL(bool useGL2);
244    void deinitEGL();
245
246    uint32_t runRootScript();
247
248    static void * threadProc(void *);
249
250    android_native_window_t *mWndSurface;
251
252    Vector<ObjectBase *> mNames;
253
254    uint64_t mTimers[_RS_TIMER_TOTAL];
255    Timers mTimerActive;
256    uint64_t mTimeLast;
257    uint64_t mTimeFrame;
258    uint64_t mTimeLastFrame;
259    uint32_t mTimeMSLastFrame;
260    uint32_t mTimeMSLastScript;
261    uint32_t mTimeMSLastSwap;
262};
263
264}
265}
266#endif
267