rsContext.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #ifndef ANDROID_RS_CONTEXT_H
  17. #define ANDROID_RS_CONTEXT_H
  18. #include "rsUtils.h"
  19. #include "rs_hal.h"
  20. #include <string.h>
  21. #include "rsThreadIO.h"
  22. #include "rsScriptC.h"
  23. #include "rsScriptGroup.h"
  24. #include "rsSampler.h"
  25. #define ATRACE_ENABLED(...) false
  26. #define ATRACE_NAME(...)
  27. #define ATRACE_CALL(...)
  28. #ifndef RS_COMPATIBILITY_LIB
  29. #include "rsFont.h"
  30. #include "rsProgramFragment.h"
  31. #include "rsProgramStore.h"
  32. #include "rsProgramRaster.h"
  33. #include "rsProgramVertex.h"
  34. #include "rsFBOCache.h"
  35. #endif
  36. #include <vector>
  37. // ---------------------------------------------------------------------------
  38. namespace android {
  39. namespace renderscript {
  40. class Device;
  41. #if 0
  42. #define CHECK_OBJ(o) { \
  43. GET_TLS(); \
  44. if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
  45. ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
  46. } \
  47. }
  48. #define CHECK_OBJ_OR_NULL(o) { \
  49. GET_TLS(); \
  50. if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
  51. ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
  52. } \
  53. }
  54. #else
  55. #define CHECK_OBJ(o)
  56. #define CHECK_OBJ_OR_NULL(o)
  57. #endif
  58. class Context {
  59. public:
  60. struct Hal {
  61. void * drv;
  62. RsdHalFunctions funcs;
  63. uint32_t flags;
  64. };
  65. Hal mHal;
  66. static Context * createContext(Device *, const RsSurfaceConfig *sc,
  67. RsContextType ct = RS_CONTEXT_TYPE_NORMAL,
  68. uint32_t flags = 0,
  69. const char* vendorDriverName = nullptr);
  70. static Context * createContextLite();
  71. ~Context();
  72. static pthread_mutex_t gMessageMutex;
  73. static pthread_mutex_t gInitMutex;
  74. // Library mutex (for providing thread-safe calls from the runtime)
  75. static pthread_mutex_t gLibMutex;
  76. class PushState {
  77. public:
  78. explicit PushState(Context *);
  79. ~PushState();
  80. private:
  81. #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
  82. ObjectBaseRef<ProgramFragment> mFragment;
  83. ObjectBaseRef<ProgramVertex> mVertex;
  84. ObjectBaseRef<ProgramStore> mStore;
  85. ObjectBaseRef<ProgramRaster> mRaster;
  86. ObjectBaseRef<Font> mFont;
  87. #endif
  88. Context *mRsc;
  89. };
  90. RsSurfaceConfig mUserSurfaceConfig;
  91. ElementState mStateElement;
  92. TypeState mStateType;
  93. SamplerState mStateSampler;
  94. bool isSynchronous() {return mSynchronous;}
  95. bool setupCheck();
  96. #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
  97. FBOCache mFBOCache;
  98. ProgramFragmentState mStateFragment;
  99. ProgramStoreState mStateFragmentStore;
  100. ProgramRasterState mStateRaster;
  101. ProgramVertexState mStateVertex;
  102. FontState mStateFont;
  103. void swapBuffers();
  104. void setRootScript(Script *);
  105. void setProgramRaster(ProgramRaster *);
  106. void setProgramVertex(ProgramVertex *);
  107. void setProgramFragment(ProgramFragment *);
  108. void setProgramStore(ProgramStore *);
  109. void setFont(Font *);
  110. void updateSurface(void *sur);
  111. ProgramFragment * getProgramFragment() {return mFragment.get();}
  112. ProgramStore * getProgramStore() {return mFragmentStore.get();}
  113. ProgramRaster * getProgramRaster() {return mRaster.get();}
  114. ProgramVertex * getProgramVertex() {return mVertex.get();}
  115. Font * getFont() {return mFont.get();}
  116. void setupProgramStore();
  117. void pause();
  118. void resume();
  119. void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
  120. #endif
  121. void finish();
  122. void setPriority(int32_t p);
  123. void destroyWorkerThreadResources();
  124. void assignName(ObjectBase *obj, const char *name, uint32_t len);
  125. void removeName(ObjectBase *obj);
  126. RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
  127. RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
  128. bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
  129. uint32_t runScript(Script *s);
  130. void initToClient();
  131. void deinitToClient();
  132. #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
  133. ProgramFragment * getDefaultProgramFragment() const {
  134. return mStateFragment.mDefault.get();
  135. }
  136. ProgramVertex * getDefaultProgramVertex() const {
  137. return mStateVertex.mDefault.get();
  138. }
  139. ProgramStore * getDefaultProgramStore() const {
  140. return mStateFragmentStore.mDefault.get();
  141. }
  142. ProgramRaster * getDefaultProgramRaster() const {
  143. return mStateRaster.mDefault.get();
  144. }
  145. Font* getDefaultFont() const {
  146. return mStateFont.mDefault.get();
  147. }
  148. uint32_t getWidth() const {return mWidth;}
  149. uint32_t getHeight() const {return mHeight;}
  150. uint32_t getCurrentSurfaceWidth() const;
  151. uint32_t getCurrentSurfaceHeight() const;
  152. void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
  153. watchdog.command = cmd;
  154. watchdog.file = file;
  155. watchdog.line = line;
  156. }
  157. #endif
  158. mutable ThreadIO mIO;
  159. // Timers
  160. enum Timers {
  161. RS_TIMER_IDLE,
  162. RS_TIMER_INTERNAL,
  163. RS_TIMER_SCRIPT,
  164. RS_TIMER_CLEAR_SWAP,
  165. _RS_TIMER_TOTAL
  166. };
  167. uint64_t getTime() const;
  168. void timerInit();
  169. void timerReset();
  170. void timerSet(Timers);
  171. void timerPrint();
  172. void timerFrame();
  173. struct {
  174. bool mLogTimes;
  175. bool mLogScripts;
  176. bool mLogShaders;
  177. bool mLogShadersAttr;
  178. bool mLogShadersUniforms;
  179. bool mLogVisual;
  180. uint32_t mLogReduce;
  181. bool mDebugReduceSplitAccum;
  182. uint32_t mDebugMaxThreads;
  183. } props;
  184. mutable struct {
  185. bool inRoot;
  186. const char *command;
  187. const char *file;
  188. uint32_t line;
  189. } watchdog;
  190. static void printWatchdogInfo(void *ctx);
  191. void dumpDebug() const;
  192. void setError(RsError e, const char *msg = nullptr) const;
  193. mutable const ObjectBase * mObjHead;
  194. uint32_t getDPI() const {return mDPI;}
  195. void setDPI(uint32_t dpi) {mDPI = dpi;}
  196. uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
  197. void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
  198. RsContextType getContextType() const { return mContextType; }
  199. void setContextType(RsContextType ct) { mContextType = ct; }
  200. // Check for Fatal errors
  201. // Should be used to prevent work from being launched
  202. // which could take the process down. Maximizes the chance
  203. // the process lives long enough to get the error to the developer
  204. bool hadFatalError() {return mFatalErrorOccured;}
  205. uint32_t getOptLevel() const { return mOptLevel; }
  206. void setOptLevel(uint32_t optLevel) { mOptLevel = optLevel; }
  207. Device *mDev;
  208. #ifdef RS_COMPATIBILITY_LIB
  209. void setNativeLibDir(const char * libDir, uint32_t length) {
  210. if (!hasSetNativeLibDir) {
  211. if (length <= PATH_MAX) {
  212. memcpy(nativeLibDir, libDir, length);
  213. nativeLibDir[length] = 0;
  214. hasSetNativeLibDir = true;
  215. } else {
  216. setError(RS_ERROR_BAD_VALUE, "Invalid path");
  217. }
  218. }
  219. }
  220. const char * getNativeLibDir() {
  221. return nativeLibDir;
  222. }
  223. #endif
  224. void setCacheDir(const char * cacheDir_arg, uint32_t length);
  225. const char * getCacheDir() {
  226. if (hasSetCacheDir) {
  227. return mCacheDir;
  228. }
  229. return nullptr;
  230. }
  231. // Returns the actual loaded driver's name (like "libRSDriver.so").
  232. const char * getDriverName() {
  233. return mDriverName;
  234. }
  235. // Set a new driver name, should be called from within
  236. // rsdHalInit in order to alter default behaviour.
  237. void setDriverName(const char * name) {
  238. if (!mDriverName) {
  239. mDriverName = name;
  240. }
  241. }
  242. protected:
  243. uint32_t mTargetSdkVersion;
  244. uint32_t mDPI;
  245. uint32_t mWidth;
  246. uint32_t mHeight;
  247. int32_t mThreadPriority;
  248. bool mIsGraphicsContext;
  249. bool mForceRSoV;
  250. bool mForceCpu;
  251. RsContextType mContextType;
  252. uint32_t mOptLevel;
  253. bool mRunning;
  254. bool mExit;
  255. bool mPaused;
  256. mutable bool mFatalErrorOccured;
  257. mutable RsError mError;
  258. pthread_t mThreadId;
  259. pid_t mNativeThreadId;
  260. ObjectBaseRef<Script> mRootScript;
  261. #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
  262. ObjectBaseRef<ProgramFragment> mFragment;
  263. ObjectBaseRef<ProgramVertex> mVertex;
  264. ObjectBaseRef<ProgramStore> mFragmentStore;
  265. ObjectBaseRef<ProgramRaster> mRaster;
  266. ObjectBaseRef<Font> mFont;
  267. #endif
  268. void displayDebugStats();
  269. private:
  270. Context();
  271. bool initContext(Device *, const RsSurfaceConfig *sc);
  272. bool mSynchronous;
  273. bool initGLThread();
  274. void deinitEGL();
  275. uint32_t runRootScript();
  276. bool loadRuntime(const char* filename);
  277. // Loads the driver.
  278. // forceDefault: If true, loads the default CPU driver.
  279. // forceRSoV: If true, overrides forceDefault and loads the RSoV driver.
  280. bool loadDriver(bool forceDefault, bool forceRSoV);
  281. static void * threadProc(void *);
  282. static void * helperThreadProc(void *);
  283. bool mHasSurface;
  284. bool mIsContextLite;
  285. // This holds the name of the driver (like "libRSDriver.so").
  286. // Since this is always just a static string, we don't have to
  287. // allocate, copy, or free any memory here.
  288. const char* mDriverName;
  289. const char* mVendorDriverName;
  290. std::vector<ObjectBase *> mNames;
  291. // Sync fence id for Graphic API, default value -1.
  292. int32_t mSyncFd = -1;
  293. uint64_t mTimers[_RS_TIMER_TOTAL];
  294. Timers mTimerActive;
  295. uint64_t mTimeLast;
  296. uint64_t mTimeFrame;
  297. uint64_t mTimeLastFrame;
  298. uint32_t mTimeMSLastFrame;
  299. uint32_t mTimeMSLastScript;
  300. uint32_t mTimeMSLastSwap;
  301. uint32_t mAverageFPSFrameCount;
  302. uint64_t mAverageFPSStartTime;
  303. uint32_t mAverageFPS;
  304. #ifdef RS_COMPATIBILITY_LIB
  305. bool hasSetNativeLibDir = false;
  306. char nativeLibDir[PATH_MAX+1];
  307. #endif
  308. bool hasSetCacheDir = false;
  309. char mCacheDir[PATH_MAX+1];
  310. };
  311. void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr);
  312. } // namespace renderscript
  313. } // namespace android
  314. #endif