Home | History | Annotate | Download | only in rs
      1 /*
      2  * Copyright (C) 2009 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 
     22 #include "rsThreadIO.h"
     23 #include "rsType.h"
     24 #include "rsMatrix.h"
     25 #include "rsAllocation.h"
     26 #include "rsSimpleMesh.h"
     27 #include "rsMesh.h"
     28 #include "rsDevice.h"
     29 #include "rsScriptC.h"
     30 #include "rsAllocation.h"
     31 #include "rsAdapter.h"
     32 #include "rsSampler.h"
     33 #include "rsLight.h"
     34 #include "rsProgramFragment.h"
     35 #include "rsProgramFragmentStore.h"
     36 #include "rsProgramRaster.h"
     37 #include "rsProgramVertex.h"
     38 #include "rsShaderCache.h"
     39 #include "rsVertexArray.h"
     40 
     41 #include "rsgApiStructs.h"
     42 #include "rsLocklessFifo.h"
     43 
     44 #include <ui/egl/android_natives.h>
     45 
     46 // ---------------------------------------------------------------------------
     47 namespace android {
     48 
     49 namespace renderscript {
     50 
     51 class Context
     52 {
     53 public:
     54     Context(Device *, bool isGraphics, bool useDepth);
     55     ~Context();
     56 
     57     static pthread_key_t gThreadTLSKey;
     58     static uint32_t gThreadTLSKeyCount;
     59     static uint32_t gGLContextCount;
     60     static pthread_mutex_t gInitMutex;
     61 
     62     struct ScriptTLSStruct {
     63         Context * mContext;
     64         Script * mScript;
     65     };
     66 
     67 
     68     //StructuredAllocationContext mStateAllocation;
     69     ElementState mStateElement;
     70     TypeState mStateType;
     71     SamplerState mStateSampler;
     72     ProgramFragmentState mStateFragment;
     73     ProgramFragmentStoreState mStateFragmentStore;
     74     ProgramRasterState mStateRaster;
     75     ProgramVertexState mStateVertex;
     76     LightState mStateLight;
     77     VertexArrayState mStateVertexArray;
     78 
     79     ScriptCState mScriptC;
     80     ShaderCache mShaderCache;
     81 
     82     void swapBuffers();
     83     void setRootScript(Script *);
     84     void setRaster(ProgramRaster *);
     85     void setVertex(ProgramVertex *);
     86     void setFragment(ProgramFragment *);
     87     void setFragmentStore(ProgramFragmentStore *);
     88 
     89     void updateSurface(void *sur);
     90 
     91     const ProgramFragment * getFragment() {return mFragment.get();}
     92     const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
     93     const ProgramRaster * getRaster() {return mRaster.get();}
     94     const ProgramVertex * getVertex() {return mVertex.get();}
     95 
     96     bool setupCheck();
     97     bool checkDriver() const {return mEGL.mSurface != 0;}
     98 
     99     void pause();
    100     void resume();
    101     void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
    102     void setPriority(int32_t p);
    103 
    104     void assignName(ObjectBase *obj, const char *name, uint32_t len);
    105     void removeName(ObjectBase *obj);
    106     ObjectBase * lookupName(const char *name) const;
    107     void appendNameDefines(String8 *str) const;
    108 
    109     uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
    110     bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
    111     uint32_t runScript(Script *s, uint32_t launchID);
    112 
    113     void initToClient();
    114     void deinitToClient();
    115 
    116     ProgramFragment * getDefaultProgramFragment() const {
    117         return mStateFragment.mDefault.get();
    118     }
    119     ProgramVertex * getDefaultProgramVertex() const {
    120         return mStateVertex.mDefault.get();
    121     }
    122     ProgramFragmentStore * getDefaultProgramFragmentStore() const {
    123         return mStateFragmentStore.mDefault.get();
    124     }
    125     ProgramRaster * getDefaultProgramRaster() const {
    126         return mStateRaster.mDefault.get();
    127     }
    128 
    129     uint32_t getWidth() const {return mEGL.mWidth;}
    130     uint32_t getHeight() const {return mEGL.mHeight;}
    131 
    132 
    133     ThreadIO mIO;
    134     void objDestroyAdd(ObjectBase *);
    135 
    136     // Timers
    137     enum Timers {
    138         RS_TIMER_IDLE,
    139         RS_TIMER_INTERNAL,
    140         RS_TIMER_SCRIPT,
    141         RS_TIMER_CLEAR_SWAP,
    142         _RS_TIMER_TOTAL
    143     };
    144     uint64_t getTime() const;
    145     void timerInit();
    146     void timerReset();
    147     void timerSet(Timers);
    148     void timerPrint();
    149     void timerFrame();
    150 
    151     bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
    152     bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
    153 
    154     struct {
    155         bool mLogTimes;
    156         bool mLogScripts;
    157         bool mLogObjects;
    158         bool mLogShaders;
    159     } props;
    160 
    161     void dumpDebug() const;
    162     void checkError(const char *) const;
    163     const char * getError(RsError *);
    164     void setError(RsError e, const char *msg);
    165 
    166     mutable const ObjectBase * mObjHead;
    167 
    168     bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
    169 
    170 protected:
    171     Device *mDev;
    172 
    173     struct {
    174         EGLint mNumConfigs;
    175         EGLint mMajorVersion;
    176         EGLint mMinorVersion;
    177         EGLConfig mConfig;
    178         EGLContext mContext;
    179         EGLSurface mSurface;
    180         EGLint mWidth;
    181         EGLint mHeight;
    182         EGLDisplay mDisplay;
    183     } mEGL;
    184 
    185     struct {
    186         const uint8_t * mVendor;
    187         const uint8_t * mRenderer;
    188         const uint8_t * mVersion;
    189         const uint8_t * mExtensions;
    190 
    191         uint32_t mMajorVersion;
    192         uint32_t mMinorVersion;
    193 
    194         int32_t mMaxVaryingVectors;
    195         int32_t mMaxTextureImageUnits;
    196 
    197         int32_t mMaxFragmentTextureImageUnits;
    198         int32_t mMaxFragmentUniformVectors;
    199 
    200         int32_t mMaxVertexAttribs;
    201         int32_t mMaxVertexUniformVectors;
    202         int32_t mMaxVertexTextureUnits;
    203 
    204         bool OES_texture_npot;
    205     } mGL;
    206 
    207     uint32_t mWidth;
    208     uint32_t mHeight;
    209     int32_t mThreadPriority;
    210     bool mIsGraphicsContext;
    211 
    212     bool mRunning;
    213     bool mExit;
    214     bool mUseDepth;
    215     bool mPaused;
    216     RsError mError;
    217     const char *mErrorMsg;
    218 
    219     pthread_t mThreadId;
    220     pid_t mNativeThreadId;
    221 
    222     ObjectBaseRef<Script> mRootScript;
    223     ObjectBaseRef<ProgramFragment> mFragment;
    224     ObjectBaseRef<ProgramVertex> mVertex;
    225     ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
    226     ObjectBaseRef<ProgramRaster> mRaster;
    227 
    228 
    229     struct ObjDestroyOOB {
    230         pthread_mutex_t mMutex;
    231         Vector<ObjectBase *> mDestroyList;
    232         bool mNeedToEmpty;
    233     };
    234     ObjDestroyOOB mObjDestroy;
    235     bool objDestroyOOBInit();
    236     void objDestroyOOBRun();
    237     void objDestroyOOBDestroy();
    238 
    239 private:
    240     Context();
    241 
    242     void initEGL(bool useGL2);
    243     void deinitEGL();
    244 
    245     uint32_t runRootScript();
    246 
    247     static void * threadProc(void *);
    248 
    249     ANativeWindow *mWndSurface;
    250 
    251     Vector<ObjectBase *> mNames;
    252 
    253     uint64_t mTimers[_RS_TIMER_TOTAL];
    254     Timers mTimerActive;
    255     uint64_t mTimeLast;
    256     uint64_t mTimeFrame;
    257     uint64_t mTimeLastFrame;
    258     uint32_t mTimeMSLastFrame;
    259     uint32_t mTimeMSLastScript;
    260     uint32_t mTimeMSLastSwap;
    261 };
    262 
    263 }
    264 }
    265 #endif
    266