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