rsContext.h revision afcb25c65e8145d15aaf50a0ca38333954a97000
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 "rsTriangleMesh.h"
29#include "rsSimpleMesh.h"
30#include "rsMesh.h"
31#include "rsDevice.h"
32#include "rsScriptC.h"
33#include "rsAllocation.h"
34#include "rsAdapter.h"
35#include "rsSampler.h"
36#include "rsLight.h"
37#include "rsProgramFragment.h"
38#include "rsProgramFragmentStore.h"
39#include "rsProgramVertex.h"
40
41#include "rsgApiStructs.h"
42#include "rsLocklessFifo.h"
43
44
45// ---------------------------------------------------------------------------
46namespace android {
47namespace renderscript {
48
49class Context
50{
51public:
52    Context(Device *, Surface *, bool useDepth);
53    ~Context();
54
55    static pthread_key_t gThreadTLSKey;
56    struct ScriptTLSStruct {
57        Context * mContext;
58        Script * mScript;
59    };
60
61
62    //StructuredAllocationContext mStateAllocation;
63    ElementState mStateElement;
64    TypeState mStateType;
65    SamplerState mStateSampler;
66    ProgramFragmentState mStateFragment;
67    ProgramFragmentStoreState mStateFragmentStore;
68    ProgramVertexState mStateVertex;
69    LightState mStateLight;
70
71    TriangleMeshContext mStateTriangleMesh;
72
73    ScriptCState mScriptC;
74
75    void swapBuffers();
76    void setRootScript(Script *);
77    void setVertex(ProgramVertex *);
78    void setFragment(ProgramFragment *);
79    void setFragmentStore(ProgramFragmentStore *);
80
81    void updateSurface(void *sur);
82
83    const ProgramFragment * getFragment() {return mFragment.get();}
84    const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
85    const ProgramVertex * getVertex() {return mVertex.get();}
86
87    void setupCheck();
88    void allocationCheck(const Allocation *);
89
90    void assignName(ObjectBase *obj, const char *name, uint32_t len);
91    void removeName(ObjectBase *obj);
92    ObjectBase * lookupName(const char *name) const;
93    void appendNameDefines(String8 *str) const;
94    void appendVarDefines(String8 *str) const;
95
96    ProgramFragment * getDefaultProgramFragment() const {
97        return mStateFragment.mDefault.get();
98    }
99    ProgramVertex * getDefaultProgramVertex() const {
100        return mStateVertex.mDefault.get();
101    }
102    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
103        return mStateFragmentStore.mDefault.get();
104    }
105
106    void addInt32Define(const char* name, int32_t value) {
107        mInt32Defines.add(String8(name), value);
108    }
109
110    void addFloatDefine(const char* name, float value) {
111        mFloatDefines.add(String8(name), value);
112    }
113
114    uint32_t getWidth() const {return mEGL.mWidth;}
115    uint32_t getHeight() const {return mEGL.mHeight;}
116
117
118    ThreadIO mIO;
119    void objDestroyAdd(ObjectBase *);
120
121    // Timers
122    enum Timers {
123        RS_TIMER_IDLE,
124        RS_TIMER_INTERNAL,
125        RS_TIMER_SCRIPT,
126        RS_TIMER_CLEAR_SWAP,
127        _RS_TIMER_TOTAL
128    };
129    uint64_t getTime() const;
130    void timerInit();
131    void timerReset();
132    void timerSet(Timers);
133    void timerPrint();
134
135    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
136    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
137
138protected:
139    Device *mDev;
140
141    struct {
142        EGLint mNumConfigs;
143        EGLint mMajorVersion;
144        EGLint mMinorVersion;
145        EGLConfig mConfig;
146        EGLContext mContext;
147        EGLSurface mSurface;
148        EGLint mWidth;
149        EGLint mHeight;
150        EGLDisplay mDisplay;
151    } mEGL;
152
153    struct {
154        const uint8_t * mVendor;
155        const uint8_t * mRenderer;
156        const uint8_t * mVersion;
157        const uint8_t * mExtensions;
158
159        uint32_t mMajorVersion;
160        uint32_t mMinorVersion;
161
162    } mGL;
163
164    bool mRunning;
165    bool mExit;
166    bool mUseDepth;
167
168    pthread_t mThreadId;
169
170    ObjectBaseRef<Script> mRootScript;
171    ObjectBaseRef<ProgramFragment> mFragment;
172    ObjectBaseRef<ProgramVertex> mVertex;
173    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
174
175
176    struct ObjDestroyOOB {
177        pthread_mutex_t mMutex;
178        Vector<ObjectBase *> mDestroyList;
179        bool mNeedToEmpty;
180    };
181    ObjDestroyOOB mObjDestroy;
182    bool objDestroyOOBInit();
183    void objDestroyOOBRun();
184    void objDestroyOOBDestroy();
185
186private:
187    Context();
188
189    void initEGL();
190
191    bool runScript(Script *s, uint32_t launchID);
192    bool runRootScript();
193
194    static void * threadProc(void *);
195
196    Surface *mWndSurface;
197
198    Vector<ObjectBase *> mNames;
199    KeyedVector<String8,int> mInt32Defines;
200    KeyedVector<String8,float> mFloatDefines;
201
202    uint64_t mTimers[_RS_TIMER_TOTAL];
203    Timers mTimerActive;
204    uint64_t mTimeLast;
205};
206
207
208}
209}
210#endif
211