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