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