rsContext.h revision cd50653f99c960e1a47c2c30e53b369b8805344a
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#include "rsShaderCache.h" 41#include "rsVertexArray.h" 42 43#include "rsgApiStructs.h" 44#include "rsLocklessFifo.h" 45 46 47// --------------------------------------------------------------------------- 48namespace android { 49namespace renderscript { 50 51class Context 52{ 53public: 54 Context(Device *, 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 91 const ProgramFragment * getFragment() {return mFragment.get();} 92 const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();} 93 const ProgramRaster * getRaster() {return mRaster.get();} 94 const ProgramVertex * getVertex() {return mVertex.get();} 95 96 void setupCheck(); 97 bool checkDriver() const {return mEGL.mSurface != 0;} 98 99 void pause(); 100 void resume(); 101 void setSurface(uint32_t w, uint32_t h, Surface *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 void appendVarDefines(String8 *str) const; 109 110 uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait); 111 bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace); 112 uint32_t runScript(Script *s, uint32_t launchID); 113 114 void initToClient(); 115 void deinitToClient(); 116 117 ProgramFragment * getDefaultProgramFragment() const { 118 return mStateFragment.mDefault.get(); 119 } 120 ProgramVertex * getDefaultProgramVertex() const { 121 return mStateVertex.mDefault.get(); 122 } 123 ProgramFragmentStore * getDefaultProgramFragmentStore() const { 124 return mStateFragmentStore.mDefault.get(); 125 } 126 ProgramRaster * getDefaultProgramRaster() const { 127 return mStateRaster.mDefault.get(); 128 } 129 130 void addInt32Define(const char* name, int32_t value) { 131 mInt32Defines.add(String8(name), value); 132 } 133 134 void addFloatDefine(const char* name, float value) { 135 mFloatDefines.add(String8(name), value); 136 } 137 138 uint32_t getWidth() const {return mEGL.mWidth;} 139 uint32_t getHeight() const {return mEGL.mHeight;} 140 141 142 ThreadIO mIO; 143 void objDestroyAdd(ObjectBase *); 144 145 // Timers 146 enum Timers { 147 RS_TIMER_IDLE, 148 RS_TIMER_INTERNAL, 149 RS_TIMER_SCRIPT, 150 RS_TIMER_CLEAR_SWAP, 151 _RS_TIMER_TOTAL 152 }; 153 uint64_t getTime() const; 154 void timerInit(); 155 void timerReset(); 156 void timerSet(Timers); 157 void timerPrint(); 158 void timerFrame(); 159 160 bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); } 161 bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; } 162 163 struct { 164 bool mLogTimes; 165 bool mLogScripts; 166 bool mLogObjects; 167 bool mLogShaders; 168 } props; 169 170 void dumpDebug() const; 171 172 mutable const ObjectBase * mObjHead; 173 174protected: 175 Device *mDev; 176 177 struct { 178 EGLint mNumConfigs; 179 EGLint mMajorVersion; 180 EGLint mMinorVersion; 181 EGLConfig mConfig; 182 EGLContext mContext; 183 EGLSurface mSurface; 184 EGLint mWidth; 185 EGLint mHeight; 186 EGLDisplay mDisplay; 187 } mEGL; 188 189 struct { 190 const uint8_t * mVendor; 191 const uint8_t * mRenderer; 192 const uint8_t * mVersion; 193 const uint8_t * mExtensions; 194 195 uint32_t mMajorVersion; 196 uint32_t mMinorVersion; 197 198 int32_t mMaxVaryingVectors; 199 int32_t mMaxTextureImageUnits; 200 201 int32_t mMaxFragmentTextureImageUnits; 202 int32_t mMaxFragmentUniformVectors; 203 204 int32_t mMaxVertexAttribs; 205 int32_t mMaxVertexUniformVectors; 206 int32_t mMaxVertexTextureUnits; 207 } mGL; 208 209 uint32_t mWidth; 210 uint32_t mHeight; 211 int32_t mThreadPriority; 212 213 bool mRunning; 214 bool mExit; 215 bool mUseDepth; 216 bool mPaused; 217 218 pthread_t mThreadId; 219 pid_t mNativeThreadId; 220 221 ObjectBaseRef<Script> mRootScript; 222 ObjectBaseRef<ProgramFragment> mFragment; 223 ObjectBaseRef<ProgramVertex> mVertex; 224 ObjectBaseRef<ProgramFragmentStore> mFragmentStore; 225 ObjectBaseRef<ProgramRaster> mRaster; 226 227 228 struct ObjDestroyOOB { 229 pthread_mutex_t mMutex; 230 Vector<ObjectBase *> mDestroyList; 231 bool mNeedToEmpty; 232 }; 233 ObjDestroyOOB mObjDestroy; 234 bool objDestroyOOBInit(); 235 void objDestroyOOBRun(); 236 void objDestroyOOBDestroy(); 237 238private: 239 Context(); 240 241 void initEGL(bool useGL2); 242 void deinitEGL(); 243 244 uint32_t runRootScript(); 245 246 static void * threadProc(void *); 247 248 Surface *mWndSurface; 249 250 Vector<ObjectBase *> mNames; 251 KeyedVector<String8,int> mInt32Defines; 252 KeyedVector<String8,float> mFloatDefines; 253 254 uint64_t mTimers[_RS_TIMER_TOTAL]; 255 Timers mTimerActive; 256 uint64_t mTimeLast; 257 uint64_t mTimeFrame; 258 uint64_t mTimeLastFrame; 259 uint32_t mTimeMSLastFrame; 260 uint32_t mTimeMSLastScript; 261 uint32_t mTimeMSLastSwap; 262}; 263 264} 265} 266#endif 267