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// --------------------------------------------------------------------------- 51namespace android { 52 53namespace renderscript { 54 55class 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 77class Context { 78public: 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; 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 142 ProgramFragment * getProgramFragment() {return mFragment.get();} 143 ProgramStore * getProgramStore() {return mFragmentStore.get();} 144 ProgramRaster * getProgramRaster() {return mRaster.get();} 145 ProgramVertex * getProgramVertex() {return mVertex.get();} 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 void finish(); 155 156 void setPriority(int32_t p); 157 void destroyWorkerThreadResources(); 158 159 void assignName(ObjectBase *obj, const char *name, uint32_t len); 160 void removeName(ObjectBase *obj); 161 162 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID); 163 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen); 164 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const; 165 uint32_t runScript(Script *s); 166 167 void initToClient(); 168 void deinitToClient(); 169 170#ifndef RS_COMPATIBILITY_LIB 171 ProgramFragment * getDefaultProgramFragment() const { 172 return mStateFragment.mDefault.get(); 173 } 174 ProgramVertex * getDefaultProgramVertex() const { 175 return mStateVertex.mDefault.get(); 176 } 177 ProgramStore * getDefaultProgramStore() const { 178 return mStateFragmentStore.mDefault.get(); 179 } 180 ProgramRaster * getDefaultProgramRaster() const { 181 return mStateRaster.mDefault.get(); 182 } 183 Font* getDefaultFont() const { 184 return mStateFont.mDefault.get(); 185 } 186 187 uint32_t getWidth() const {return mWidth;} 188 uint32_t getHeight() const {return mHeight;} 189 190 uint32_t getCurrentSurfaceWidth() const; 191 uint32_t getCurrentSurfaceHeight() const; 192 193 void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const { 194 watchdog.command = cmd; 195 watchdog.file = file; 196 watchdog.line = line; 197 } 198#endif 199 200 mutable ThreadIO mIO; 201 202 // Timers 203 enum Timers { 204 RS_TIMER_IDLE, 205 RS_TIMER_INTERNAL, 206 RS_TIMER_SCRIPT, 207 RS_TIMER_CLEAR_SWAP, 208 _RS_TIMER_TOTAL 209 }; 210 uint64_t getTime() const; 211 void timerInit(); 212 void timerReset(); 213 void timerSet(Timers); 214 void timerPrint(); 215 void timerFrame(); 216 217 struct { 218 bool mLogTimes; 219 bool mLogScripts; 220 bool mLogObjects; 221 bool mLogShaders; 222 bool mLogShadersAttr; 223 bool mLogShadersUniforms; 224 bool mLogVisual; 225 uint32_t mDebugMaxThreads; 226 } props; 227 228 mutable struct { 229 bool inRoot; 230 const char *command; 231 const char *file; 232 uint32_t line; 233 } watchdog; 234 static void printWatchdogInfo(void *ctx); 235 236 void dumpDebug() const; 237 void setError(RsError e, const char *msg = NULL) const; 238 239 mutable const ObjectBase * mObjHead; 240 241 uint32_t getDPI() const {return mDPI;} 242 void setDPI(uint32_t dpi) {mDPI = dpi;} 243 244 uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;} 245 void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;} 246 247 RsContextType getContextType() const { return mContextType; } 248 void setContextType(RsContextType ct) { mContextType = ct; } 249 250 Device *mDev; 251protected: 252 253 uint32_t mTargetSdkVersion; 254 uint32_t mDPI; 255 uint32_t mWidth; 256 uint32_t mHeight; 257 int32_t mThreadPriority; 258 bool mIsGraphicsContext; 259 260 bool mForceCpu; 261 262 RsContextType mContextType; 263 264 bool mRunning; 265 bool mExit; 266 bool mPaused; 267 mutable RsError mError; 268 269 pthread_t mThreadId; 270 pid_t mNativeThreadId; 271 272 ObjectBaseRef<Script> mRootScript; 273#ifndef RS_COMPATIBILITY_LIB 274 ObjectBaseRef<ProgramFragment> mFragment; 275 ObjectBaseRef<ProgramVertex> mVertex; 276 ObjectBaseRef<ProgramStore> mFragmentStore; 277 ObjectBaseRef<ProgramRaster> mRaster; 278 ObjectBaseRef<Font> mFont; 279#endif 280 281 void displayDebugStats(); 282 283private: 284 Context(); 285 bool initContext(Device *, const RsSurfaceConfig *sc); 286 287 bool mSynchronous; 288 bool initGLThread(); 289 void deinitEGL(); 290 291 uint32_t runRootScript(); 292 293 static bool loadRuntime(const char* filename, Context* rsc); 294 static void * threadProc(void *); 295 static void * helperThreadProc(void *); 296 297 bool mHasSurface; 298 bool mIsContextLite; 299 300 Vector<ObjectBase *> mNames; 301 302 uint64_t mTimers[_RS_TIMER_TOTAL]; 303 Timers mTimerActive; 304 uint64_t mTimeLast; 305 uint64_t mTimeFrame; 306 uint64_t mTimeLastFrame; 307 uint32_t mTimeMSLastFrame; 308 uint32_t mTimeMSLastScript; 309 uint32_t mTimeMSLastSwap; 310 uint32_t mAverageFPSFrameCount; 311 uint64_t mAverageFPSStartTime; 312 uint32_t mAverageFPS; 313}; 314 315} // renderscript 316} // android 317#endif 318