rsContext.h revision f4d160653fe405eba9d6f55448ac4599c6cadd77
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 "rsTriangleMesh.h"
29#include "rsSimpleMesh.h"
30#include "rsMesh.h"
31#include "rsDevice.h"
32#include "rsScriptC.h"
33#include "rsAllocation.h"
34#include "rsAdapter.h"
35#include "rsSampler.h"
36#include "rsLight.h"
37#include "rsProgramFragment.h"
38#include "rsProgramFragmentStore.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 *);
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    ProgramVertexState mStateVertex;
69    LightState mStateLight;
70
71    TriangleMeshContext mStateTriangleMesh;
72
73    ScriptCState mScriptC;
74
75    void swapBuffers();
76    void setRootScript(Script *);
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 ProgramVertex * getVertex() {return mVertex.get();}
86
87    void setupCheck();
88    void allocationCheck(const Allocation *);
89
90    void assignName(ObjectBase *obj, const char *name, uint32_t len);
91    void removeName(ObjectBase *obj);
92    ObjectBase * lookupName(const char *name) const;
93    void appendNameDefines(String8 *str) const;
94    void appendVarDefines(String8 *str) const;
95
96    ProgramFragment * getDefaultProgramFragment() const {
97        return mStateFragment.mDefault.get();
98    }
99    ProgramVertex * getDefaultProgramVertex() const {
100        return mStateVertex.mDefault.get();
101    }
102    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
103        return mStateFragmentStore.mDefault.get();
104    }
105
106    void addInt32Define(const char* name, int32_t value) {
107        mInt32Defines.add(String8(name), value);
108    }
109
110    void addFloatDefine(const char* name, float value) {
111        mFloatDefines.add(String8(name), value);
112    }
113
114    uint32_t getWidth() const {return mWidth;}
115    uint32_t getHeight() const {return mHeight;}
116
117
118    ThreadIO mIO;
119    void objDestroyAdd(ObjectBase *);
120
121    // Timers
122    enum Timers {
123        RS_TIMER_IDLE,
124        RS_TIMER_INTERNAL,
125        RS_TIMER_SCRIPT,
126        RS_TIMER_CLEAR_SWAP,
127        _RS_TIMER_TOTAL
128    };
129    uint64_t getTime() const;
130    void timerInit();
131    void timerReset();
132    void timerSet(Timers);
133    void timerPrint();
134
135protected:
136    Device *mDev;
137
138    EGLint mNumConfigs;
139    EGLint mMajorVersion;
140    EGLint mMinorVersion;
141    EGLConfig mConfig;
142    EGLContext mContext;
143    EGLSurface mSurface;
144    EGLint mWidth;
145    EGLint mHeight;
146    EGLDisplay mDisplay;
147
148    bool mRunning;
149    bool mExit;
150
151    pthread_t mThreadId;
152
153    ObjectBaseRef<Script> mRootScript;
154    ObjectBaseRef<ProgramFragment> mFragment;
155    ObjectBaseRef<ProgramVertex> mVertex;
156    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
157
158
159    struct ObjDestroyOOB {
160        pthread_mutex_t mMutex;
161        Vector<ObjectBase *> mDestroyList;
162        bool mNeedToEmpty;
163    };
164    ObjDestroyOOB mObjDestroy;
165    bool objDestroyOOBInit();
166    void objDestroyOOBRun();
167    void objDestroyOOBDestroy();
168
169private:
170    Context();
171
172    void initEGL();
173
174    bool runScript(Script *s, uint32_t launchID);
175    bool runRootScript();
176
177    static void * threadProc(void *);
178
179    Surface *mWndSurface;
180
181    Vector<ObjectBase *> mNames;
182    KeyedVector<String8,int> mInt32Defines;
183    KeyedVector<String8,float> mFloatDefines;
184
185    uint64_t mTimers[_RS_TIMER_TOTAL];
186    Timers mTimerActive;
187    uint64_t mTimeLast;
188};
189
190
191}
192}
193#endif
194