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