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