rsContext.h revision cdfdb8f2cdf4668c476cac842212892b2505ff3f
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 "rsType.h" 22#include "rsAllocation.h" 23#include "rsMesh.h" 24 25#include "rs_hal.h" 26 27#ifndef ANDROID_RS_SERIALIZE 28#include "rsMutex.h" 29#include "rsThreadIO.h" 30#include "rsMatrix.h" 31#include "rsDevice.h" 32#include "rsScriptC.h" 33#include "rsAdapter.h" 34#include "rsSampler.h" 35#include "rsFont.h" 36#include "rsProgramFragment.h" 37#include "rsProgramStore.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#include <ui/egl/android_natives.h> 47#endif // ANDROID_RS_SERIALIZE 48 49// --------------------------------------------------------------------------- 50namespace android { 51 52namespace renderscript { 53 54#if 0 55#define CHECK_OBJ(o) { \ 56 GET_TLS(); \ 57 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \ 58 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \ 59 } \ 60} 61#define CHECK_OBJ_OR_NULL(o) { \ 62 GET_TLS(); \ 63 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \ 64 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \ 65 } \ 66} 67#else 68#define CHECK_OBJ(o) 69#define CHECK_OBJ_OR_NULL(o) 70#endif 71 72#ifndef ANDROID_RS_SERIALIZE 73 74class Context { 75public: 76 struct Hal { 77 void * drv; 78 79 RsdHalFunctions funcs; 80 }; 81 Hal mHal; 82 83 static Context * createContext(Device *, const RsSurfaceConfig *sc); 84 ~Context(); 85 86 static pthread_key_t gThreadTLSKey; 87 static uint32_t gThreadTLSKeyCount; 88 static uint32_t gGLContextCount; 89 static pthread_mutex_t gInitMutex; 90 // Library mutex (for providing thread-safe calls from the runtime) 91 static pthread_mutex_t gLibMutex; 92 93 class PushState { 94 public: 95 PushState(Context *); 96 ~PushState(); 97 98 private: 99 ObjectBaseRef<ProgramFragment> mFragment; 100 ObjectBaseRef<ProgramVertex> mVertex; 101 ObjectBaseRef<ProgramStore> mStore; 102 ObjectBaseRef<ProgramRaster> mRaster; 103 ObjectBaseRef<Font> mFont; 104 Context *mRsc; 105 }; 106 107 ScriptTLSStruct *mTlsStruct; 108 RsSurfaceConfig mUserSurfaceConfig; 109 110 ElementState mStateElement; 111 TypeState mStateType; 112 SamplerState mStateSampler; 113 ProgramFragmentState mStateFragment; 114 ProgramStoreState mStateFragmentStore; 115 ProgramRasterState mStateRaster; 116 ProgramVertexState mStateVertex; 117 VertexArrayState mStateVertexArray; 118 FontState mStateFont; 119 120 ScriptCState mScriptC; 121 ShaderCache mShaderCache; 122 123 void swapBuffers(); 124 void setRootScript(Script *); 125 void setProgramRaster(ProgramRaster *); 126 void setProgramVertex(ProgramVertex *); 127 void setProgramFragment(ProgramFragment *); 128 void setProgramStore(ProgramStore *); 129 void setFont(Font *); 130 131 void updateSurface(void *sur); 132 133 ProgramFragment * getProgramFragment() {return mFragment.get();} 134 ProgramStore * getProgramStore() {return mFragmentStore.get();} 135 ProgramRaster * getProgramRaster() {return mRaster.get();} 136 ProgramVertex * getProgramVertex() {return mVertex.get();} 137 Font * getFont() {return mFont.get();} 138 139 bool setupCheck(); 140 void setupProgramStore(); 141 142 void pause(); 143 void resume(); 144 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur); 145 void setPriority(int32_t p); 146 void destroyWorkerThreadResources(); 147 148 void assignName(ObjectBase *obj, const char *name, uint32_t len); 149 void removeName(ObjectBase *obj); 150 151 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait); 152 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait); 153 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const; 154 uint32_t runScript(Script *s); 155 156 void initToClient(); 157 void deinitToClient(); 158 159 ProgramFragment * getDefaultProgramFragment() const { 160 return mStateFragment.mDefault.get(); 161 } 162 ProgramVertex * getDefaultProgramVertex() const { 163 return mStateVertex.mDefault.get(); 164 } 165 ProgramStore * getDefaultProgramStore() const { 166 return mStateFragmentStore.mDefault.get(); 167 } 168 ProgramRaster * getDefaultProgramRaster() const { 169 return mStateRaster.mDefault.get(); 170 } 171 Font* getDefaultFont() const { 172 return mStateFont.mDefault.get(); 173 } 174 175 uint32_t getWidth() const {return mWidth;} 176 uint32_t getHeight() const {return mHeight;} 177 178 mutable ThreadIO mIO; 179 180 // Timers 181 enum Timers { 182 RS_TIMER_IDLE, 183 RS_TIMER_INTERNAL, 184 RS_TIMER_SCRIPT, 185 RS_TIMER_CLEAR_SWAP, 186 _RS_TIMER_TOTAL 187 }; 188 uint64_t getTime() const; 189 void timerInit(); 190 void timerReset(); 191 void timerSet(Timers); 192 void timerPrint(); 193 void timerFrame(); 194 195 struct { 196 bool mLogTimes; 197 bool mLogScripts; 198 bool mLogObjects; 199 bool mLogShaders; 200 bool mLogShadersAttr; 201 bool mLogShadersUniforms; 202 bool mLogVisual; 203 } props; 204 205 void dumpDebug() const; 206 void checkError(const char *, bool isFatal = false) const; 207 void setError(RsError e, const char *msg = NULL) const; 208 209 mutable const ObjectBase * mObjHead; 210 211 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;} 212 bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;} 213 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;} 214 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; } 215 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;} 216 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;} 217 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;} 218 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;} 219 220 uint32_t getDPI() const {return mDPI;} 221 void setDPI(uint32_t dpi) {mDPI = dpi;} 222 223protected: 224 Device *mDev; 225 226 struct { 227 EGLint mNumConfigs; 228 EGLint mMajorVersion; 229 EGLint mMinorVersion; 230 EGLConfig mConfig; 231 EGLContext mContext; 232 EGLSurface mSurface; 233 EGLSurface mSurfaceDefault; 234 EGLDisplay mDisplay; 235 } mEGL; 236 237 struct { 238 const uint8_t * mVendor; 239 const uint8_t * mRenderer; 240 const uint8_t * mVersion; 241 const uint8_t * mExtensions; 242 243 uint32_t mMajorVersion; 244 uint32_t mMinorVersion; 245 246 int32_t mMaxVaryingVectors; 247 int32_t mMaxTextureImageUnits; 248 249 int32_t mMaxFragmentTextureImageUnits; 250 int32_t mMaxFragmentUniformVectors; 251 252 int32_t mMaxVertexAttribs; 253 int32_t mMaxVertexUniformVectors; 254 int32_t mMaxVertexTextureUnits; 255 256 bool OES_texture_npot; 257 bool GL_IMG_texture_npot; 258 bool GL_NV_texture_npot_2D_mipmap; 259 float EXT_texture_max_aniso; 260 } mGL; 261 262 uint32_t mDPI; 263 uint32_t mWidth; 264 uint32_t mHeight; 265 int32_t mThreadPriority; 266 bool mIsGraphicsContext; 267 268 bool mRunning; 269 bool mExit; 270 bool mPaused; 271 mutable RsError mError; 272 273 pthread_t mThreadId; 274 pid_t mNativeThreadId; 275 276 ObjectBaseRef<Script> mRootScript; 277 ObjectBaseRef<ProgramFragment> mFragment; 278 ObjectBaseRef<ProgramVertex> mVertex; 279 ObjectBaseRef<ProgramStore> mFragmentStore; 280 ObjectBaseRef<ProgramRaster> mRaster; 281 ObjectBaseRef<Font> mFont; 282 283 void displayDebugStats(); 284 285private: 286 Context(); 287 bool initContext(Device *, const RsSurfaceConfig *sc); 288 289 290 bool initGLThread(); 291 void deinitEGL(); 292 293 uint32_t runRootScript(); 294 295 static void * threadProc(void *); 296 static void * helperThreadProc(void *); 297 298 ANativeWindow *mWndSurface; 299 300 Vector<ObjectBase *> mNames; 301 302 uint64_t mTimers[_RS_TIMER_TOTAL]; 303 Timers mTimerActive; 304 uint64_t mTimeLast; 305 uint64_t mTimeFrame; 306 uint64_t mTimeLastFrame; 307 uint32_t mTimeMSLastFrame; 308 uint32_t mTimeMSLastScript; 309 uint32_t mTimeMSLastSwap; 310 uint32_t mAverageFPSFrameCount; 311 uint64_t mAverageFPSStartTime; 312 uint32_t mAverageFPS; 313}; 314 315#else 316 317class Context { 318public: 319 Context() { 320 mObjHead = NULL; 321 } 322 ~Context() { 323 ObjectBase::zeroAllUserRef(this); 324 } 325 326 ElementState mStateElement; 327 TypeState mStateType; 328 329 struct { 330 bool mLogTimes; 331 bool mLogScripts; 332 bool mLogObjects; 333 bool mLogShaders; 334 bool mLogShadersAttr; 335 bool mLogShadersUniforms; 336 bool mLogVisual; 337 } props; 338 339 void setError(RsError e, const char *msg = NULL) { } 340 341 mutable const ObjectBase * mObjHead; 342 343protected: 344 345}; 346#endif //ANDROID_RS_SERIALIZE 347 348} // renderscript 349} // android 350#endif 351