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