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