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