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