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