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