• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rsThreadIO.h"
23 #include "rsType.h"
24 #include "rsMatrix.h"
25 #include "rsAllocation.h"
26 #include "rsSimpleMesh.h"
27 #include "rsMesh.h"
28 #include "rsDevice.h"
29 #include "rsScriptC.h"
30 #include "rsAllocation.h"
31 #include "rsAdapter.h"
32 #include "rsSampler.h"
33 #include "rsLight.h"
34 #include "rsProgramFragment.h"
35 #include "rsProgramFragmentStore.h"
36 #include "rsProgramRaster.h"
37 #include "rsProgramVertex.h"
38 #include "rsShaderCache.h"
39 #include "rsVertexArray.h"
40 
41 #include "rsgApiStructs.h"
42 #include "rsLocklessFifo.h"
43 
44 #include <ui/egl/android_natives.h>
45 
46 // ---------------------------------------------------------------------------
47 namespace android {
48 
49 namespace renderscript {
50 
51 class Context
52 {
53 public:
54     Context(Device *, bool isGraphics, bool useDepth);
55     ~Context();
56 
57     static pthread_key_t gThreadTLSKey;
58     static uint32_t gThreadTLSKeyCount;
59     static uint32_t gGLContextCount;
60     static pthread_mutex_t gInitMutex;
61 
62     struct ScriptTLSStruct {
63         Context * mContext;
64         Script * mScript;
65     };
66 
67 
68     //StructuredAllocationContext mStateAllocation;
69     ElementState mStateElement;
70     TypeState mStateType;
71     SamplerState mStateSampler;
72     ProgramFragmentState mStateFragment;
73     ProgramFragmentStoreState mStateFragmentStore;
74     ProgramRasterState mStateRaster;
75     ProgramVertexState mStateVertex;
76     LightState mStateLight;
77     VertexArrayState mStateVertexArray;
78 
79     ScriptCState mScriptC;
80     ShaderCache mShaderCache;
81 
82     void swapBuffers();
83     void setRootScript(Script *);
84     void setRaster(ProgramRaster *);
85     void setVertex(ProgramVertex *);
86     void setFragment(ProgramFragment *);
87     void setFragmentStore(ProgramFragmentStore *);
88 
89     void updateSurface(void *sur);
90 
getFragment()91     const ProgramFragment * getFragment() {return mFragment.get();}
getFragmentStore()92     const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
getRaster()93     const ProgramRaster * getRaster() {return mRaster.get();}
getVertex()94     const ProgramVertex * getVertex() {return mVertex.get();}
95 
96     bool setupCheck();
checkDriver()97     bool checkDriver() const {return mEGL.mSurface != 0;}
98 
99     void pause();
100     void resume();
101     void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
102     void setPriority(int32_t p);
103 
104     void assignName(ObjectBase *obj, const char *name, uint32_t len);
105     void removeName(ObjectBase *obj);
106     ObjectBase * lookupName(const char *name) const;
107     void appendNameDefines(String8 *str) const;
108 
109     uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
110     bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
111     uint32_t runScript(Script *s, uint32_t launchID);
112 
113     void initToClient();
114     void deinitToClient();
115 
getDefaultProgramFragment()116     ProgramFragment * getDefaultProgramFragment() const {
117         return mStateFragment.mDefault.get();
118     }
getDefaultProgramVertex()119     ProgramVertex * getDefaultProgramVertex() const {
120         return mStateVertex.mDefault.get();
121     }
getDefaultProgramFragmentStore()122     ProgramFragmentStore * getDefaultProgramFragmentStore() const {
123         return mStateFragmentStore.mDefault.get();
124     }
getDefaultProgramRaster()125     ProgramRaster * getDefaultProgramRaster() const {
126         return mStateRaster.mDefault.get();
127     }
128 
getWidth()129     uint32_t getWidth() const {return mEGL.mWidth;}
getHeight()130     uint32_t getHeight() const {return mEGL.mHeight;}
131 
132 
133     ThreadIO mIO;
134     void objDestroyAdd(ObjectBase *);
135 
136     // Timers
137     enum Timers {
138         RS_TIMER_IDLE,
139         RS_TIMER_INTERNAL,
140         RS_TIMER_SCRIPT,
141         RS_TIMER_CLEAR_SWAP,
142         _RS_TIMER_TOTAL
143     };
144     uint64_t getTime() const;
145     void timerInit();
146     void timerReset();
147     void timerSet(Timers);
148     void timerPrint();
149     void timerFrame();
150 
checkVersion1_1()151     bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
checkVersion2_0()152     bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
153 
154     struct {
155         bool mLogTimes;
156         bool mLogScripts;
157         bool mLogObjects;
158         bool mLogShaders;
159     } props;
160 
161     void dumpDebug() const;
162     void checkError(const char *) const;
163     const char * getError(RsError *);
164     void setError(RsError e, const char *msg);
165 
166     mutable const ObjectBase * mObjHead;
167 
ext_OES_texture_npot()168     bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
ext_GL_IMG_texture_npot()169     bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
170 
171 protected:
172     Device *mDev;
173 
174     struct {
175         EGLint mNumConfigs;
176         EGLint mMajorVersion;
177         EGLint mMinorVersion;
178         EGLConfig mConfig;
179         EGLContext mContext;
180         EGLSurface mSurface;
181         EGLint mWidth;
182         EGLint mHeight;
183         EGLDisplay mDisplay;
184     } mEGL;
185 
186     struct {
187         const uint8_t * mVendor;
188         const uint8_t * mRenderer;
189         const uint8_t * mVersion;
190         const uint8_t * mExtensions;
191 
192         uint32_t mMajorVersion;
193         uint32_t mMinorVersion;
194 
195         int32_t mMaxVaryingVectors;
196         int32_t mMaxTextureImageUnits;
197 
198         int32_t mMaxFragmentTextureImageUnits;
199         int32_t mMaxFragmentUniformVectors;
200 
201         int32_t mMaxVertexAttribs;
202         int32_t mMaxVertexUniformVectors;
203         int32_t mMaxVertexTextureUnits;
204 
205         bool OES_texture_npot;
206         bool GL_IMG_texture_npot;
207     } mGL;
208 
209     uint32_t mWidth;
210     uint32_t mHeight;
211     int32_t mThreadPriority;
212     bool mIsGraphicsContext;
213 
214     bool mRunning;
215     bool mExit;
216     bool mUseDepth;
217     bool mPaused;
218     RsError mError;
219     const char *mErrorMsg;
220 
221     pthread_t mThreadId;
222     pid_t mNativeThreadId;
223 
224     ObjectBaseRef<Script> mRootScript;
225     ObjectBaseRef<ProgramFragment> mFragment;
226     ObjectBaseRef<ProgramVertex> mVertex;
227     ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
228     ObjectBaseRef<ProgramRaster> mRaster;
229 
230 
231     struct ObjDestroyOOB {
232         pthread_mutex_t mMutex;
233         Vector<ObjectBase *> mDestroyList;
234         bool mNeedToEmpty;
235     };
236     ObjDestroyOOB mObjDestroy;
237     bool objDestroyOOBInit();
238     void objDestroyOOBRun();
239     void objDestroyOOBDestroy();
240 
241 private:
242     Context();
243 
244     void initEGL(bool useGL2);
245     void deinitEGL();
246 
247     uint32_t runRootScript();
248 
249     static void * threadProc(void *);
250 
251     ANativeWindow *mWndSurface;
252 
253     Vector<ObjectBase *> mNames;
254 
255     uint64_t mTimers[_RS_TIMER_TOTAL];
256     Timers mTimerActive;
257     uint64_t mTimeLast;
258     uint64_t mTimeFrame;
259     uint64_t mTimeLastFrame;
260     uint32_t mTimeMSLastFrame;
261     uint32_t mTimeMSLastScript;
262     uint32_t mTimeMSLastSwap;
263 };
264 
265 }
266 }
267 #endif
268