rsContext.h revision 900f1616bf33c7ba13cf2a737832a95bcd176388
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 141 // Timers 142 enum Timers { 143 RS_TIMER_IDLE, 144 RS_TIMER_INTERNAL, 145 RS_TIMER_SCRIPT, 146 RS_TIMER_CLEAR_SWAP, 147 _RS_TIMER_TOTAL 148 }; 149 uint64_t getTime() const; 150 void timerInit(); 151 void timerReset(); 152 void timerSet(Timers); 153 void timerPrint(); 154 void timerFrame(); 155 156 struct { 157 bool mLogTimes; 158 bool mLogScripts; 159 bool mLogObjects; 160 bool mLogShaders; 161 bool mLogVisual; 162 } props; 163 164 void dumpDebug() const; 165 void checkError(const char *) const; 166 const char * getError(RsError *); 167 void setError(RsError e, const char *msg = NULL); 168 169 mutable const ObjectBase * mObjHead; 170 171 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;} 172 173 void launchThreads(WorkerCallback_t cbk, void *data); 174 uint32_t getWorkerPoolSize() const {return (uint32_t)mWorkers.mRunningCount;} 175 176protected: 177 Device *mDev; 178 179 struct { 180 EGLint mNumConfigs; 181 EGLint mMajorVersion; 182 EGLint mMinorVersion; 183 EGLConfig mConfig; 184 EGLContext mContext; 185 EGLSurface mSurface; 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 208 bool OES_texture_npot; 209 } mGL; 210 211 uint32_t mWidth; 212 uint32_t mHeight; 213 int32_t mThreadPriority; 214 bool mIsGraphicsContext; 215 216 bool mRunning; 217 bool mExit; 218 bool mUseDepth; 219 bool mPaused; 220 RsError mError; 221 const char *mErrorMsg; 222 223 pthread_t mThreadId; 224 pid_t mNativeThreadId; 225 226 struct Workers { 227 volatile int mRunningCount; 228 volatile int mLaunchCount; 229 uint32_t mCount; 230 pthread_t *mThreadId; 231 pid_t *mNativeThreadId; 232 Signal mCompleteSignal; 233 234 Signal *mLaunchSignals; 235 WorkerCallback_t mLaunchCallback; 236 void *mLaunchData; 237 }; 238 Workers mWorkers; 239 240 ObjectBaseRef<Script> mRootScript; 241 ObjectBaseRef<ProgramFragment> mFragment; 242 ObjectBaseRef<ProgramVertex> mVertex; 243 ObjectBaseRef<ProgramStore> mFragmentStore; 244 ObjectBaseRef<ProgramRaster> mRaster; 245 ObjectBaseRef<Font> mFont; 246 247 void displayDebugStats(); 248 249private: 250 Context(); 251 252 void initEGL(bool useGL2); 253 void deinitEGL(); 254 255 uint32_t runRootScript(); 256 257 static void * threadProc(void *); 258 static void * helperThreadProc(void *); 259 260 ANativeWindow *mWndSurface; 261 262 Vector<ObjectBase *> mNames; 263 264 uint64_t mTimers[_RS_TIMER_TOTAL]; 265 Timers mTimerActive; 266 uint64_t mTimeLast; 267 uint64_t mTimeFrame; 268 uint64_t mTimeLastFrame; 269 uint32_t mTimeMSLastFrame; 270 uint32_t mTimeMSLastScript; 271 uint32_t mTimeMSLastSwap; 272}; 273 274} 275} 276#endif 277