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 "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         uint32_t flags;
     84     };
     85     Hal mHal;
     86 
     87     static Context * createContext(Device *, const RsSurfaceConfig *sc,
     88             RsContextType ct = RS_CONTEXT_TYPE_NORMAL,
     89             uint32_t flags = 0);
     90     static Context * createContextLite();
     91     ~Context();
     92 
     93     static pthread_mutex_t gMessageMutex;
     94     static pthread_mutex_t gInitMutex;
     95     // Library mutex (for providing thread-safe calls from the runtime)
     96     static pthread_mutex_t gLibMutex;
     97 
     98     class PushState {
     99     public:
    100         PushState(Context *);
    101         ~PushState();
    102 
    103     private:
    104 #ifndef RS_COMPATIBILITY_LIB
    105         ObjectBaseRef<ProgramFragment> mFragment;
    106         ObjectBaseRef<ProgramVertex> mVertex;
    107         ObjectBaseRef<ProgramStore> mStore;
    108         ObjectBaseRef<ProgramRaster> mRaster;
    109         ObjectBaseRef<Font> mFont;
    110 #endif
    111         Context *mRsc;
    112     };
    113 
    114     RsSurfaceConfig mUserSurfaceConfig;
    115 
    116     ElementState mStateElement;
    117     TypeState mStateType;
    118     SamplerState mStateSampler;
    119 
    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;
    251 protected:
    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 
    283 private:
    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 void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr);
    316 
    317 } // renderscript
    318 } // android
    319 #endif
    320