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 "rsPath.h" 41 #include "rsProgramFragment.h" 42 #include "rsProgramStore.h" 43 #include "rsProgramRaster.h" 44 #include "rsProgramVertex.h" 45 #include "rsFBOCache.h" 46 47 #endif 48 49 50 // --------------------------------------------------------------------------- 51 namespace android { 52 53 namespace renderscript { 54 55 class Device; 56 57 #if 0 58 #define CHECK_OBJ(o) { \ 59 GET_TLS(); \ 60 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \ 61 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \ 62 } \ 63 } 64 #define CHECK_OBJ_OR_NULL(o) { \ 65 GET_TLS(); \ 66 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \ 67 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \ 68 } \ 69 } 70 #else 71 #define CHECK_OBJ(o) 72 #define CHECK_OBJ_OR_NULL(o) 73 #endif 74 75 76 77 class Context { 78 public: 79 struct Hal { 80 void * drv; 81 82 RsdHalFunctions funcs; 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 ScriptCState mScriptC; isSynchronous()120 bool isSynchronous() {return mSynchronous;} 121 bool setupCheck(); 122 123 #ifndef RS_COMPATIBILITY_LIB 124 FBOCache mFBOCache; 125 ProgramFragmentState mStateFragment; 126 ProgramStoreState mStateFragmentStore; 127 ProgramRasterState mStateRaster; 128 ProgramVertexState mStateVertex; 129 FontState mStateFont; 130 131 132 void swapBuffers(); 133 void setRootScript(Script *); 134 void setProgramRaster(ProgramRaster *); 135 void setProgramVertex(ProgramVertex *); 136 void setProgramFragment(ProgramFragment *); 137 void setProgramStore(ProgramStore *); 138 void setFont(Font *); 139 140 void updateSurface(void *sur); 141 getProgramFragment()142 ProgramFragment * getProgramFragment() {return mFragment.get();} getProgramStore()143 ProgramStore * getProgramStore() {return mFragmentStore.get();} getProgramRaster()144 ProgramRaster * getProgramRaster() {return mRaster.get();} getProgramVertex()145 ProgramVertex * getProgramVertex() {return mVertex.get();} getFont()146 Font * getFont() {return mFont.get();} 147 148 void setupProgramStore(); 149 150 void pause(); 151 void resume(); 152 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur); 153 #endif 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 getDefaultProgramFragment()170 ProgramFragment * getDefaultProgramFragment() const { 171 return mStateFragment.mDefault.get(); 172 } getDefaultProgramVertex()173 ProgramVertex * getDefaultProgramVertex() const { 174 return mStateVertex.mDefault.get(); 175 } getDefaultProgramStore()176 ProgramStore * getDefaultProgramStore() const { 177 return mStateFragmentStore.mDefault.get(); 178 } getDefaultProgramRaster()179 ProgramRaster * getDefaultProgramRaster() const { 180 return mStateRaster.mDefault.get(); 181 } getDefaultFont()182 Font* getDefaultFont() const { 183 return mStateFont.mDefault.get(); 184 } 185 getWidth()186 uint32_t getWidth() const {return mWidth;} getHeight()187 uint32_t getHeight() const {return mHeight;} 188 189 uint32_t getCurrentSurfaceWidth() const; 190 uint32_t getCurrentSurfaceHeight() const; 191 setWatchdogGL(const char * cmd,uint32_t line,const char * file)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 mLogObjects; 220 bool mLogShaders; 221 bool mLogShadersAttr; 222 bool mLogShadersUniforms; 223 bool mLogVisual; 224 uint32_t mDebugMaxThreads; 225 } props; 226 227 mutable struct { 228 bool inRoot; 229 const char *command; 230 const char *file; 231 uint32_t line; 232 } watchdog; 233 static void printWatchdogInfo(void *ctx); 234 235 void dumpDebug() const; 236 void setError(RsError e, const char *msg = NULL) const; 237 238 mutable const ObjectBase * mObjHead; 239 getDPI()240 uint32_t getDPI() const {return mDPI;} setDPI(uint32_t dpi)241 void setDPI(uint32_t dpi) {mDPI = dpi;} 242 getTargetSdkVersion()243 uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;} setTargetSdkVersion(uint32_t sdkVer)244 void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;} 245 getContextType()246 RsContextType getContextType() const { return mContextType; } setContextType(RsContextType ct)247 void setContextType(RsContextType ct) { mContextType = ct; } 248 249 Device *mDev; 250 protected: 251 252 uint32_t mTargetSdkVersion; 253 uint32_t mDPI; 254 uint32_t mWidth; 255 uint32_t mHeight; 256 int32_t mThreadPriority; 257 bool mIsGraphicsContext; 258 259 bool mForceCpu; 260 261 RsContextType mContextType; 262 263 bool mRunning; 264 bool mExit; 265 bool mPaused; 266 mutable RsError mError; 267 268 pthread_t mThreadId; 269 pid_t mNativeThreadId; 270 271 ObjectBaseRef<Script> mRootScript; 272 #ifndef RS_COMPATIBILITY_LIB 273 ObjectBaseRef<ProgramFragment> mFragment; 274 ObjectBaseRef<ProgramVertex> mVertex; 275 ObjectBaseRef<ProgramStore> mFragmentStore; 276 ObjectBaseRef<ProgramRaster> mRaster; 277 ObjectBaseRef<Font> mFont; 278 #endif 279 280 void displayDebugStats(); 281 282 private: 283 Context(); 284 bool initContext(Device *, const RsSurfaceConfig *sc); 285 286 bool mSynchronous; 287 bool initGLThread(); 288 void deinitEGL(); 289 290 uint32_t runRootScript(); 291 292 static bool loadRuntime(const char* filename, Context* rsc); 293 static void * threadProc(void *); 294 static void * helperThreadProc(void *); 295 296 bool mHasSurface; 297 bool mIsContextLite; 298 299 Vector<ObjectBase *> mNames; 300 301 uint64_t mTimers[_RS_TIMER_TOTAL]; 302 Timers mTimerActive; 303 uint64_t mTimeLast; 304 uint64_t mTimeFrame; 305 uint64_t mTimeLastFrame; 306 uint32_t mTimeMSLastFrame; 307 uint32_t mTimeMSLastScript; 308 uint32_t mTimeMSLastSwap; 309 uint32_t mAverageFPSFrameCount; 310 uint64_t mAverageFPSStartTime; 311 uint32_t mAverageFPS; 312 }; 313 314 void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr); 315 316 } // renderscript 317 } // android 318 #endif 319