rsContext.h revision 3a27c952c013ad0a8e0c91bea76d895a07f7a56d
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    uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
101    bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
102    bool runScript(Script *s, uint32_t launchID);
103
104    void initToClient();
105    void deinitToClient();
106
107    ProgramFragment * getDefaultProgramFragment() const {
108        return mStateFragment.mDefault.get();
109    }
110    ProgramVertex * getDefaultProgramVertex() const {
111        return mStateVertex.mDefault.get();
112    }
113    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
114        return mStateFragmentStore.mDefault.get();
115    }
116    ProgramRaster * getDefaultProgramRaster() const {
117        return mStateRaster.mDefault.get();
118    }
119
120    void addInt32Define(const char* name, int32_t value) {
121        mInt32Defines.add(String8(name), value);
122    }
123
124    void addFloatDefine(const char* name, float value) {
125        mFloatDefines.add(String8(name), value);
126    }
127
128    uint32_t getWidth() const {return mEGL.mWidth;}
129    uint32_t getHeight() const {return mEGL.mHeight;}
130
131
132    ThreadIO mIO;
133    void objDestroyAdd(ObjectBase *);
134
135    // Timers
136    enum Timers {
137        RS_TIMER_IDLE,
138        RS_TIMER_INTERNAL,
139        RS_TIMER_SCRIPT,
140        RS_TIMER_CLEAR_SWAP,
141        _RS_TIMER_TOTAL
142    };
143    uint64_t getTime() const;
144    void timerInit();
145    void timerReset();
146    void timerSet(Timers);
147    void timerPrint();
148    void timerFrame();
149
150    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
151    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
152
153    struct {
154        bool mLogTimes;
155        bool mLogScripts;
156        bool mLogObjects;
157    } props;
158
159    mutable const ObjectBase * mObjHead;
160
161protected:
162    Device *mDev;
163
164    struct {
165        EGLint mNumConfigs;
166        EGLint mMajorVersion;
167        EGLint mMinorVersion;
168        EGLConfig mConfig;
169        EGLContext mContext;
170        EGLSurface mSurface;
171        EGLint mWidth;
172        EGLint mHeight;
173        EGLDisplay mDisplay;
174    } mEGL;
175
176    struct {
177        const uint8_t * mVendor;
178        const uint8_t * mRenderer;
179        const uint8_t * mVersion;
180        const uint8_t * mExtensions;
181
182        uint32_t mMajorVersion;
183        uint32_t mMinorVersion;
184
185    } mGL;
186
187    bool mRunning;
188    bool mExit;
189    bool mUseDepth;
190    bool mPaused;
191
192    pthread_t mThreadId;
193
194    ObjectBaseRef<Script> mRootScript;
195    ObjectBaseRef<ProgramFragment> mFragment;
196    ObjectBaseRef<ProgramVertex> mVertex;
197    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
198    ObjectBaseRef<ProgramRaster> mRaster;
199
200
201    struct ObjDestroyOOB {
202        pthread_mutex_t mMutex;
203        Vector<ObjectBase *> mDestroyList;
204        bool mNeedToEmpty;
205    };
206    ObjDestroyOOB mObjDestroy;
207    bool objDestroyOOBInit();
208    void objDestroyOOBRun();
209    void objDestroyOOBDestroy();
210
211private:
212    Context();
213
214    void initEGL();
215
216    bool runRootScript();
217
218    static void * threadProc(void *);
219
220    Surface *mWndSurface;
221
222    Vector<ObjectBase *> mNames;
223    KeyedVector<String8,int> mInt32Defines;
224    KeyedVector<String8,float> mFloatDefines;
225
226    uint64_t mTimers[_RS_TIMER_TOTAL];
227    Timers mTimerActive;
228    uint64_t mTimeLast;
229    uint64_t mTimeFrame;
230    uint64_t mTimeLastFrame;
231};
232
233}
234}
235#endif
236