Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | 399bfce | 2009-07-13 12:20:31 -0700 | [diff] [blame] | 20 | #include "rsUtils.h" |
| 21 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 22 | #include <ui/Surface.h> |
| 23 | |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 24 | #include "rsThreadIO.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | #include "rsType.h" |
| 26 | #include "rsMatrix.h" |
| 27 | #include "rsAllocation.h" |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 28 | #include "rsSimpleMesh.h" |
Jason Sams | 7c878f3 | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 29 | #include "rsMesh.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | #include "rsDevice.h" |
| 31 | #include "rsScriptC.h" |
| 32 | #include "rsAllocation.h" |
| 33 | #include "rsAdapter.h" |
| 34 | #include "rsSampler.h" |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 35 | #include "rsLight.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 | #include "rsProgramFragment.h" |
| 37 | #include "rsProgramFragmentStore.h" |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 38 | #include "rsProgramRaster.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | #include "rsProgramVertex.h" |
| 40 | |
| 41 | #include "rsgApiStructs.h" |
| 42 | #include "rsLocklessFifo.h" |
| 43 | |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | namespace android { |
| 47 | namespace renderscript { |
| 48 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 49 | class Context |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | { |
| 51 | public: |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 52 | Context(Device *, Surface *, bool useDepth); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 53 | ~Context(); |
| 54 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 55 | static pthread_key_t gThreadTLSKey; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 56 | static uint32_t gThreadTLSKeyCount; |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 57 | static uint32_t gGLContextCount; |
Jason Sams | 41c19db9 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 58 | static pthread_mutex_t gInitMutex; |
| 59 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 60 | struct ScriptTLSStruct { |
| 61 | Context * mContext; |
| 62 | Script * mScript; |
| 63 | }; |
| 64 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 65 | |
| 66 | //StructuredAllocationContext mStateAllocation; |
| 67 | ElementState mStateElement; |
| 68 | TypeState mStateType; |
| 69 | SamplerState mStateSampler; |
| 70 | ProgramFragmentState mStateFragment; |
| 71 | ProgramFragmentStoreState mStateFragmentStore; |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 72 | ProgramRasterState mStateRaster; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 73 | ProgramVertexState mStateVertex; |
Jason Sams | bba134c | 2009-06-22 15:49:21 -0700 | [diff] [blame] | 74 | LightState mStateLight; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 75 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 76 | ScriptCState mScriptC; |
| 77 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 78 | void swapBuffers(); |
| 79 | void setRootScript(Script *); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 80 | void setRaster(ProgramRaster *); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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();} |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 89 | const ProgramRaster * getRaster() {return mRaster.get();} |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 90 | const ProgramVertex * getVertex() {return mVertex.get();} |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | |
| 92 | void setupCheck(); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 93 | void allocationCheck(const Allocation *); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 94 | |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 95 | void pause(); |
| 96 | void resume(); |
| 97 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 98 | void assignName(ObjectBase *obj, const char *name, uint32_t len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 99 | void removeName(ObjectBase *obj); |
| 100 | ObjectBase * lookupName(const char *name) const; |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 101 | void appendNameDefines(String8 *str) const; |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 102 | void appendVarDefines(String8 *str) const; |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 103 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 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); |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 106 | bool runScript(Script *s, uint32_t launchID); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 107 | |
| 108 | void initToClient(); |
| 109 | void deinitToClient(); |
| 110 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 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 | } |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 120 | ProgramRaster * getDefaultProgramRaster() const { |
| 121 | return mStateRaster.mDefault.get(); |
| 122 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 123 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 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 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 132 | uint32_t getWidth() const {return mEGL.mWidth;} |
| 133 | uint32_t getHeight() const {return mEGL.mHeight;} |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 134 | |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 135 | |
| 136 | ThreadIO mIO; |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 137 | void objDestroyAdd(ObjectBase *); |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 138 | |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 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(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 152 | void timerFrame(); |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 153 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 154 | bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); } |
| 155 | bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; } |
| 156 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 157 | struct { |
| 158 | bool mLogTimes; |
| 159 | bool mLogScripts; |
| 160 | bool mLogObjects; |
| 161 | } props; |
Joe Onorato | 9ac2c66 | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 162 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 163 | mutable const ObjectBase * mObjHead; |
| 164 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | protected: |
| 166 | Device *mDev; |
| 167 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 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; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 190 | |
| 191 | bool mRunning; |
| 192 | bool mExit; |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 193 | bool mUseDepth; |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 194 | bool mPaused; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 195 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 196 | pthread_t mThreadId; |
| 197 | |
| 198 | ObjectBaseRef<Script> mRootScript; |
| 199 | ObjectBaseRef<ProgramFragment> mFragment; |
| 200 | ObjectBaseRef<ProgramVertex> mVertex; |
| 201 | ObjectBaseRef<ProgramFragmentStore> mFragmentStore; |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 202 | ObjectBaseRef<ProgramRaster> mRaster; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 203 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 204 | |
| 205 | struct ObjDestroyOOB { |
| 206 | pthread_mutex_t mMutex; |
| 207 | Vector<ObjectBase *> mDestroyList; |
| 208 | bool mNeedToEmpty; |
| 209 | }; |
| 210 | ObjDestroyOOB mObjDestroy; |
| 211 | bool objDestroyOOBInit(); |
| 212 | void objDestroyOOBRun(); |
| 213 | void objDestroyOOBDestroy(); |
| 214 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 215 | private: |
| 216 | Context(); |
| 217 | |
| 218 | void initEGL(); |
Jason Sams | 7136220 | 2009-10-27 14:44:31 -0700 | [diff] [blame^] | 219 | void deinitEGL(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 220 | |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 221 | bool runRootScript(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 222 | |
| 223 | static void * threadProc(void *); |
| 224 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 225 | Surface *mWndSurface; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 226 | |
| 227 | Vector<ObjectBase *> mNames; |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 228 | KeyedVector<String8,int> mInt32Defines; |
| 229 | KeyedVector<String8,float> mFloatDefines; |
Jason Sams | f4d1606 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 230 | |
| 231 | uint64_t mTimers[_RS_TIMER_TOTAL]; |
| 232 | Timers mTimerActive; |
| 233 | uint64_t mTimeLast; |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 234 | uint64_t mTimeFrame; |
| 235 | uint64_t mTimeLastFrame; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | #endif |