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