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