rsContext.h revision c19ff0177a7a0dadfc01b1990f822354fdc95991
1/* 2 * Copyright (C) 2011 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 "rsMatrix4x4.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 "rsFBOCache.h" 41 42#include "rsgApiStructs.h" 43#include "rsLocklessFifo.h" 44 45#endif // ANDROID_RS_SERIALIZE 46 47class ANativeWindow; 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_mutex_t gInitMutex; 87 // Library mutex (for providing thread-safe calls from the runtime) 88 static pthread_mutex_t gLibMutex; 89 90 class PushState { 91 public: 92 PushState(Context *); 93 ~PushState(); 94 95 private: 96 ObjectBaseRef<ProgramFragment> mFragment; 97 ObjectBaseRef<ProgramVertex> mVertex; 98 ObjectBaseRef<ProgramStore> mStore; 99 ObjectBaseRef<ProgramRaster> mRaster; 100 ObjectBaseRef<Font> mFont; 101 Context *mRsc; 102 }; 103 104 RsSurfaceConfig mUserSurfaceConfig; 105 106 ElementState mStateElement; 107 TypeState mStateType; 108 SamplerState mStateSampler; 109 ProgramFragmentState mStateFragment; 110 ProgramStoreState mStateFragmentStore; 111 ProgramRasterState mStateRaster; 112 ProgramVertexState mStateVertex; 113 FontState mStateFont; 114 115 ScriptCState mScriptC; 116 FBOCache mFBOCache; 117 118 void swapBuffers(); 119 void setRootScript(Script *); 120 void setProgramRaster(ProgramRaster *); 121 void setProgramVertex(ProgramVertex *); 122 void setProgramFragment(ProgramFragment *); 123 void setProgramStore(ProgramStore *); 124 void setFont(Font *); 125 126 void updateSurface(void *sur); 127 128 ProgramFragment * getProgramFragment() {return mFragment.get();} 129 ProgramStore * getProgramStore() {return mFragmentStore.get();} 130 ProgramRaster * getProgramRaster() {return mRaster.get();} 131 ProgramVertex * getProgramVertex() {return mVertex.get();} 132 Font * getFont() {return mFont.get();} 133 134 bool setupCheck(); 135 void setupProgramStore(); 136 137 void pause(); 138 void resume(); 139 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur); 140 void setPriority(int32_t p); 141 void destroyWorkerThreadResources(); 142 143 void assignName(ObjectBase *obj, const char *name, uint32_t len); 144 void removeName(ObjectBase *obj); 145 146 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait); 147 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait); 148 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const; 149 uint32_t runScript(Script *s); 150 151 void initToClient(); 152 void deinitToClient(); 153 154 ProgramFragment * getDefaultProgramFragment() const { 155 return mStateFragment.mDefault.get(); 156 } 157 ProgramVertex * getDefaultProgramVertex() const { 158 return mStateVertex.mDefault.get(); 159 } 160 ProgramStore * getDefaultProgramStore() const { 161 return mStateFragmentStore.mDefault.get(); 162 } 163 ProgramRaster * getDefaultProgramRaster() const { 164 return mStateRaster.mDefault.get(); 165 } 166 Font* getDefaultFont() const { 167 return mStateFont.mDefault.get(); 168 } 169 170 uint32_t getWidth() const {return mWidth;} 171 uint32_t getHeight() const {return mHeight;} 172 173 mutable ThreadIO mIO; 174 175 // Timers 176 enum Timers { 177 RS_TIMER_IDLE, 178 RS_TIMER_INTERNAL, 179 RS_TIMER_SCRIPT, 180 RS_TIMER_CLEAR_SWAP, 181 _RS_TIMER_TOTAL 182 }; 183 uint64_t getTime() const; 184 void timerInit(); 185 void timerReset(); 186 void timerSet(Timers); 187 void timerPrint(); 188 void timerFrame(); 189 190 struct { 191 bool mLogTimes; 192 bool mLogScripts; 193 bool mLogObjects; 194 bool mLogShaders; 195 bool mLogShadersAttr; 196 bool mLogShadersUniforms; 197 bool mLogVisual; 198 } props; 199 200 void dumpDebug() const; 201 void setError(RsError e, const char *msg = NULL) const; 202 203 mutable const ObjectBase * mObjHead; 204 205 uint32_t getDPI() const {return mDPI;} 206 void setDPI(uint32_t dpi) {mDPI = dpi;} 207 208 Device *mDev; 209protected: 210 211 uint32_t mDPI; 212 uint32_t mWidth; 213 uint32_t mHeight; 214 int32_t mThreadPriority; 215 bool mIsGraphicsContext; 216 217 bool mRunning; 218 bool mExit; 219 bool mPaused; 220 mutable RsError mError; 221 222 pthread_t mThreadId; 223 pid_t mNativeThreadId; 224 225 ObjectBaseRef<Script> mRootScript; 226 ObjectBaseRef<ProgramFragment> mFragment; 227 ObjectBaseRef<ProgramVertex> mVertex; 228 ObjectBaseRef<ProgramStore> mFragmentStore; 229 ObjectBaseRef<ProgramRaster> mRaster; 230 ObjectBaseRef<Font> mFont; 231 232 void displayDebugStats(); 233 234private: 235 Context(); 236 bool initContext(Device *, const RsSurfaceConfig *sc); 237 238 239 bool initGLThread(); 240 void deinitEGL(); 241 242 uint32_t runRootScript(); 243 244 static void * threadProc(void *); 245 static void * helperThreadProc(void *); 246 247 ANativeWindow *mWndSurface; 248 249 Vector<ObjectBase *> mNames; 250 251 uint64_t mTimers[_RS_TIMER_TOTAL]; 252 Timers mTimerActive; 253 uint64_t mTimeLast; 254 uint64_t mTimeFrame; 255 uint64_t mTimeLastFrame; 256 uint32_t mTimeMSLastFrame; 257 uint32_t mTimeMSLastScript; 258 uint32_t mTimeMSLastSwap; 259 uint32_t mAverageFPSFrameCount; 260 uint64_t mAverageFPSStartTime; 261 uint32_t mAverageFPS; 262}; 263 264#else 265 266class Context { 267public: 268 Context() { 269 mObjHead = NULL; 270 } 271 ~Context() { 272 ObjectBase::zeroAllUserRef(this); 273 } 274 275 ElementState mStateElement; 276 TypeState mStateType; 277 278 struct { 279 bool mLogTimes; 280 bool mLogScripts; 281 bool mLogObjects; 282 bool mLogShaders; 283 bool mLogShadersAttr; 284 bool mLogShadersUniforms; 285 bool mLogVisual; 286 } props; 287 288 void setError(RsError e, const char *msg = NULL) { } 289 290 mutable const ObjectBase * mObjHead; 291 292protected: 293 294}; 295#endif //ANDROID_RS_SERIALIZE 296 297} // renderscript 298} // android 299#endif 300