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