rsContext.h revision 86f1b23aaaf9b8822a009d8c3e585e46768abb6a
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
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    ProgramRasterState mStateRaster;
69    ProgramVertexState mStateVertex;
70    LightState mStateLight;
71
72    ScriptCState mScriptC;
73
74    void swapBuffers();
75    void setRootScript(Script *);
76    void setRaster(ProgramRaster *);
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 ProgramRaster * getRaster() {return mRaster.get();}
86    const ProgramVertex * getVertex() {return mVertex.get();}
87
88    void setupCheck();
89    void allocationCheck(const Allocation *);
90
91    void pause();
92    void resume();
93
94    void assignName(ObjectBase *obj, const char *name, uint32_t len);
95    void removeName(ObjectBase *obj);
96    ObjectBase * lookupName(const char *name) const;
97    void appendNameDefines(String8 *str) const;
98    void appendVarDefines(String8 *str) const;
99
100    ProgramFragment * getDefaultProgramFragment() const {
101        return mStateFragment.mDefault.get();
102    }
103    ProgramVertex * getDefaultProgramVertex() const {
104        return mStateVertex.mDefault.get();
105    }
106    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
107        return mStateFragmentStore.mDefault.get();
108    }
109    ProgramRaster * getDefaultProgramRaster() const {
110        return mStateRaster.mDefault.get();
111    }
112
113    void addInt32Define(const char* name, int32_t value) {
114        mInt32Defines.add(String8(name), value);
115    }
116
117    void addFloatDefine(const char* name, float value) {
118        mFloatDefines.add(String8(name), value);
119    }
120
121    uint32_t getWidth() const {return mEGL.mWidth;}
122    uint32_t getHeight() const {return mEGL.mHeight;}
123
124
125    ThreadIO mIO;
126    void objDestroyAdd(ObjectBase *);
127
128    // Timers
129    enum Timers {
130        RS_TIMER_IDLE,
131        RS_TIMER_INTERNAL,
132        RS_TIMER_SCRIPT,
133        RS_TIMER_CLEAR_SWAP,
134        _RS_TIMER_TOTAL
135    };
136    uint64_t getTime() const;
137    void timerInit();
138    void timerReset();
139    void timerSet(Timers);
140    void timerPrint();
141    void timerFrame();
142
143    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
144    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
145
146    bool logTimes;
147
148protected:
149    Device *mDev;
150
151    struct {
152        EGLint mNumConfigs;
153        EGLint mMajorVersion;
154        EGLint mMinorVersion;
155        EGLConfig mConfig;
156        EGLContext mContext;
157        EGLSurface mSurface;
158        EGLint mWidth;
159        EGLint mHeight;
160        EGLDisplay mDisplay;
161    } mEGL;
162
163    struct {
164        const uint8_t * mVendor;
165        const uint8_t * mRenderer;
166        const uint8_t * mVersion;
167        const uint8_t * mExtensions;
168
169        uint32_t mMajorVersion;
170        uint32_t mMinorVersion;
171
172    } mGL;
173
174    bool mRunning;
175    bool mExit;
176    bool mUseDepth;
177    bool mPaused;
178
179    pthread_t mThreadId;
180
181    ObjectBaseRef<Script> mRootScript;
182    ObjectBaseRef<ProgramFragment> mFragment;
183    ObjectBaseRef<ProgramVertex> mVertex;
184    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
185    ObjectBaseRef<ProgramRaster> mRaster;
186
187
188    struct ObjDestroyOOB {
189        pthread_mutex_t mMutex;
190        Vector<ObjectBase *> mDestroyList;
191        bool mNeedToEmpty;
192    };
193    ObjDestroyOOB mObjDestroy;
194    bool objDestroyOOBInit();
195    void objDestroyOOBRun();
196    void objDestroyOOBDestroy();
197
198private:
199    Context();
200
201    void initEGL();
202
203    bool runScript(Script *s, uint32_t launchID);
204    bool runRootScript();
205
206    static void * threadProc(void *);
207
208    Surface *mWndSurface;
209
210    Vector<ObjectBase *> mNames;
211    KeyedVector<String8,int> mInt32Defines;
212    KeyedVector<String8,float> mFloatDefines;
213
214    uint64_t mTimers[_RS_TIMER_TOTAL];
215    Timers mTimerActive;
216    uint64_t mTimeLast;
217    uint64_t mTimeFrame;
218    uint64_t mTimeLastFrame;
219};
220
221}
222}
223#endif
224