rsContext.h revision 2525a815220652b37e2e390fe8c62394a6d0e574
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    void timerFrame();
135
136    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
137    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
138
139protected:
140    Device *mDev;
141
142    struct {
143        EGLint mNumConfigs;
144        EGLint mMajorVersion;
145        EGLint mMinorVersion;
146        EGLConfig mConfig;
147        EGLContext mContext;
148        EGLSurface mSurface;
149        EGLint mWidth;
150        EGLint mHeight;
151        EGLDisplay mDisplay;
152    } mEGL;
153
154    struct {
155        const uint8_t * mVendor;
156        const uint8_t * mRenderer;
157        const uint8_t * mVersion;
158        const uint8_t * mExtensions;
159
160        uint32_t mMajorVersion;
161        uint32_t mMinorVersion;
162
163    } mGL;
164
165    bool mRunning;
166    bool mExit;
167    bool mUseDepth;
168
169    pthread_t mThreadId;
170
171    ObjectBaseRef<Script> mRootScript;
172    ObjectBaseRef<ProgramFragment> mFragment;
173    ObjectBaseRef<ProgramVertex> mVertex;
174    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
175
176
177    struct ObjDestroyOOB {
178        pthread_mutex_t mMutex;
179        Vector<ObjectBase *> mDestroyList;
180        bool mNeedToEmpty;
181    };
182    ObjDestroyOOB mObjDestroy;
183    bool objDestroyOOBInit();
184    void objDestroyOOBRun();
185    void objDestroyOOBDestroy();
186
187private:
188    Context();
189
190    void initEGL();
191
192    bool runScript(Script *s, uint32_t launchID);
193    bool runRootScript();
194
195    static void * threadProc(void *);
196
197    Surface *mWndSurface;
198
199    Vector<ObjectBase *> mNames;
200    KeyedVector<String8,int> mInt32Defines;
201    KeyedVector<String8,float> mFloatDefines;
202
203    uint64_t mTimers[_RS_TIMER_TOTAL];
204    Timers mTimerActive;
205    uint64_t mTimeLast;
206    uint64_t mTimeFrame;
207    uint64_t mTimeLastFrame;
208};
209
210
211}
212}
213#endif
214