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