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