rsContext.h revision da3b58d804b59595f1c618ff271fecd8a9ce59ed
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 "rs_hal.h" 22 23#include "rsThreadIO.h" 24#include "rsScriptC.h" 25#include "rsSampler.h" 26#include "rsFont.h" 27#include "rsPath.h" 28#include "rsProgramFragment.h" 29#include "rsProgramStore.h" 30#include "rsProgramRaster.h" 31#include "rsProgramVertex.h" 32#include "rsFBOCache.h" 33#include <string.h> 34 35// --------------------------------------------------------------------------- 36namespace android { 37 38namespace renderscript { 39 40class Device; 41 42#if 0 43#define CHECK_OBJ(o) { \ 44 GET_TLS(); \ 45 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \ 46 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \ 47 } \ 48} 49#define CHECK_OBJ_OR_NULL(o) { \ 50 GET_TLS(); \ 51 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \ 52 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \ 53 } \ 54} 55#else 56#define CHECK_OBJ(o) 57#define CHECK_OBJ_OR_NULL(o) 58#endif 59 60class Context { 61public: 62 struct Hal { 63 void * drv; 64 65 RsdHalFunctions funcs; 66 }; 67 Hal mHal; 68 69 static Context * createContext(Device *, const RsSurfaceConfig *sc); 70 static Context * createContextLite(); 71 ~Context(); 72 73 static pthread_mutex_t gInitMutex; 74 // Library mutex (for providing thread-safe calls from the runtime) 75 static pthread_mutex_t gLibMutex; 76 77 class PushState { 78 public: 79 PushState(Context *); 80 ~PushState(); 81 82 private: 83 ObjectBaseRef<ProgramFragment> mFragment; 84 ObjectBaseRef<ProgramVertex> mVertex; 85 ObjectBaseRef<ProgramStore> mStore; 86 ObjectBaseRef<ProgramRaster> mRaster; 87 ObjectBaseRef<Font> mFont; 88 Context *mRsc; 89 }; 90 91 RsSurfaceConfig mUserSurfaceConfig; 92 93 ElementState mStateElement; 94 TypeState mStateType; 95 SamplerState mStateSampler; 96 ProgramFragmentState mStateFragment; 97 ProgramStoreState mStateFragmentStore; 98 ProgramRasterState mStateRaster; 99 ProgramVertexState mStateVertex; 100 FontState mStateFont; 101 102 ScriptCState mScriptC; 103 FBOCache mFBOCache; 104 105 void swapBuffers(); 106 void setRootScript(Script *); 107 void setProgramRaster(ProgramRaster *); 108 void setProgramVertex(ProgramVertex *); 109 void setProgramFragment(ProgramFragment *); 110 void setProgramStore(ProgramStore *); 111 void setFont(Font *); 112 113 void updateSurface(void *sur); 114 115 ProgramFragment * getProgramFragment() {return mFragment.get();} 116 ProgramStore * getProgramStore() {return mFragmentStore.get();} 117 ProgramRaster * getProgramRaster() {return mRaster.get();} 118 ProgramVertex * getProgramVertex() {return mVertex.get();} 119 Font * getFont() {return mFont.get();} 120 121 bool setupCheck(); 122 void setupProgramStore(); 123 124 void pause(); 125 void resume(); 126 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur); 127 void setPriority(int32_t p); 128 void destroyWorkerThreadResources(); 129 130 void assignName(ObjectBase *obj, const char *name, uint32_t len); 131 void removeName(ObjectBase *obj); 132 133 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID); 134 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen); 135 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const; 136 uint32_t runScript(Script *s); 137 138 void initToClient(); 139 void deinitToClient(); 140 141 ProgramFragment * getDefaultProgramFragment() const { 142 return mStateFragment.mDefault.get(); 143 } 144 ProgramVertex * getDefaultProgramVertex() const { 145 return mStateVertex.mDefault.get(); 146 } 147 ProgramStore * getDefaultProgramStore() const { 148 return mStateFragmentStore.mDefault.get(); 149 } 150 ProgramRaster * getDefaultProgramRaster() const { 151 return mStateRaster.mDefault.get(); 152 } 153 Font* getDefaultFont() const { 154 return mStateFont.mDefault.get(); 155 } 156 157 uint32_t getWidth() const {return mWidth;} 158 uint32_t getHeight() const {return mHeight;} 159 160 uint32_t getCurrentSurfaceWidth() const; 161 uint32_t getCurrentSurfaceHeight() const; 162 163 mutable ThreadIO mIO; 164 165 // Timers 166 enum Timers { 167 RS_TIMER_IDLE, 168 RS_TIMER_INTERNAL, 169 RS_TIMER_SCRIPT, 170 RS_TIMER_CLEAR_SWAP, 171 _RS_TIMER_TOTAL 172 }; 173 uint64_t getTime() const; 174 void timerInit(); 175 void timerReset(); 176 void timerSet(Timers); 177 void timerPrint(); 178 void timerFrame(); 179 180 struct { 181 bool mLogTimes; 182 bool mLogScripts; 183 bool mLogObjects; 184 bool mLogShaders; 185 bool mLogShadersAttr; 186 bool mLogShadersUniforms; 187 bool mLogVisual; 188 uint32_t mDebugMaxThreads; 189 } props; 190 191 mutable struct { 192 bool inRoot; 193 const char *command; 194 const char *file; 195 uint32_t line; 196 } watchdog; 197 static void printWatchdogInfo(void *ctx); 198 void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const { 199 watchdog.command = cmd; 200 watchdog.file = file; 201 watchdog.line = line; 202 } 203 204 void dumpDebug() const; 205 void setError(RsError e, const char *msg = NULL) const; 206 207 mutable const ObjectBase * mObjHead; 208 209 uint32_t getDPI() const {return mDPI;} 210 void setDPI(uint32_t dpi) {mDPI = dpi;} 211 212 uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;} 213 void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;} 214 215 Device *mDev; 216protected: 217 218 uint32_t mTargetSdkVersion; 219 uint32_t mDPI; 220 uint32_t mWidth; 221 uint32_t mHeight; 222 int32_t mThreadPriority; 223 bool mIsGraphicsContext; 224 225 bool mRunning; 226 bool mExit; 227 bool mPaused; 228 mutable RsError mError; 229 230 pthread_t mThreadId; 231 pid_t mNativeThreadId; 232 233 ObjectBaseRef<Script> mRootScript; 234 ObjectBaseRef<ProgramFragment> mFragment; 235 ObjectBaseRef<ProgramVertex> mVertex; 236 ObjectBaseRef<ProgramStore> mFragmentStore; 237 ObjectBaseRef<ProgramRaster> mRaster; 238 ObjectBaseRef<Font> mFont; 239 240 void displayDebugStats(); 241 242private: 243 Context(); 244 bool initContext(Device *, const RsSurfaceConfig *sc); 245 246 247 bool initGLThread(); 248 void deinitEGL(); 249 250 uint32_t runRootScript(); 251 252 static void * threadProc(void *); 253 static void * helperThreadProc(void *); 254 255 bool mHasSurface; 256 bool mIsContextLite; 257 258 Vector<ObjectBase *> mNames; 259 260 uint64_t mTimers[_RS_TIMER_TOTAL]; 261 Timers mTimerActive; 262 uint64_t mTimeLast; 263 uint64_t mTimeFrame; 264 uint64_t mTimeLastFrame; 265 uint32_t mTimeMSLastFrame; 266 uint32_t mTimeMSLastScript; 267 uint32_t mTimeMSLastSwap; 268 uint32_t mAverageFPSFrameCount; 269 uint64_t mAverageFPSStartTime; 270 uint32_t mAverageFPS; 271}; 272 273} // renderscript 274} // android 275#endif 276