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" |
| 28 | #include "rsTriangleMesh.h" |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 29 | #include "rsSimpleMesh.h" |
Jason Sams | 7c878f3 | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 30 | #include "rsMesh.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | #include "rsDevice.h" |
| 32 | #include "rsScriptC.h" |
| 33 | #include "rsAllocation.h" |
| 34 | #include "rsAdapter.h" |
| 35 | #include "rsSampler.h" |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 36 | #include "rsLight.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | #include "rsProgramFragment.h" |
| 38 | #include "rsProgramFragmentStore.h" |
| 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: |
| 52 | Context(Device *, Surface *); |
| 53 | ~Context(); |
| 54 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 55 | static pthread_key_t gThreadTLSKey; |
| 56 | struct ScriptTLSStruct { |
| 57 | Context * mContext; |
| 58 | Script * mScript; |
| 59 | }; |
| 60 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 61 | |
| 62 | //StructuredAllocationContext mStateAllocation; |
| 63 | ElementState mStateElement; |
| 64 | TypeState mStateType; |
| 65 | SamplerState mStateSampler; |
| 66 | ProgramFragmentState mStateFragment; |
| 67 | ProgramFragmentStoreState mStateFragmentStore; |
| 68 | ProgramVertexState mStateVertex; |
Jason Sams | bba134c | 2009-06-22 15:49:21 -0700 | [diff] [blame] | 69 | LightState mStateLight; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 70 | |
| 71 | TriangleMeshContext mStateTriangleMesh; |
| 72 | |
| 73 | ScriptCState mScriptC; |
| 74 | |
| 75 | static Context * getContext() {return gCon;} |
| 76 | |
| 77 | void swapBuffers(); |
| 78 | void setRootScript(Script *); |
| 79 | void setVertex(ProgramVertex *); |
| 80 | void setFragment(ProgramFragment *); |
| 81 | void setFragmentStore(ProgramFragmentStore *); |
| 82 | |
| 83 | void updateSurface(void *sur); |
| 84 | |
| 85 | const ProgramFragment * getFragment() {return mFragment.get();} |
| 86 | const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();} |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 87 | const ProgramVertex * getVertex() {return mVertex.get();} |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | |
| 89 | void setupCheck(); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 90 | void allocationCheck(const Allocation *); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 92 | void assignName(ObjectBase *obj, const char *name, uint32_t len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 93 | void removeName(ObjectBase *obj); |
| 94 | ObjectBase * lookupName(const char *name) const; |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 95 | void appendNameDefines(String8 *str) const; |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 96 | void appendVarDefines(String8 *str) const; |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 97 | |
| 98 | ProgramFragment * getDefaultProgramFragment() const { |
| 99 | return mStateFragment.mDefault.get(); |
| 100 | } |
| 101 | ProgramVertex * getDefaultProgramVertex() const { |
| 102 | return mStateVertex.mDefault.get(); |
| 103 | } |
| 104 | ProgramFragmentStore * getDefaultProgramFragmentStore() const { |
| 105 | return mStateFragmentStore.mDefault.get(); |
| 106 | } |
| 107 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 108 | void addInt32Define(const char* name, int32_t value) { |
| 109 | mInt32Defines.add(String8(name), value); |
| 110 | } |
| 111 | |
| 112 | void addFloatDefine(const char* name, float value) { |
| 113 | mFloatDefines.add(String8(name), value); |
| 114 | } |
| 115 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 116 | uint32_t getWidth() const {return mWidth;} |
| 117 | uint32_t getHeight() const {return mHeight;} |
| 118 | |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 119 | |
| 120 | ThreadIO mIO; |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame^] | 121 | void objDestroyAdd(ObjectBase *); |
Jason Sams | bc948de | 2009-08-17 18:35:48 -0700 | [diff] [blame] | 122 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 123 | protected: |
| 124 | Device *mDev; |
| 125 | |
| 126 | EGLint mNumConfigs; |
| 127 | EGLint mMajorVersion; |
| 128 | EGLint mMinorVersion; |
| 129 | EGLConfig mConfig; |
| 130 | EGLContext mContext; |
| 131 | EGLSurface mSurface; |
| 132 | EGLint mWidth; |
| 133 | EGLint mHeight; |
| 134 | EGLDisplay mDisplay; |
| 135 | |
| 136 | bool mRunning; |
| 137 | bool mExit; |
| 138 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | pthread_t mThreadId; |
| 140 | |
| 141 | ObjectBaseRef<Script> mRootScript; |
| 142 | ObjectBaseRef<ProgramFragment> mFragment; |
| 143 | ObjectBaseRef<ProgramVertex> mVertex; |
| 144 | ObjectBaseRef<ProgramFragmentStore> mFragmentStore; |
| 145 | |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame^] | 146 | |
| 147 | struct ObjDestroyOOB { |
| 148 | pthread_mutex_t mMutex; |
| 149 | Vector<ObjectBase *> mDestroyList; |
| 150 | bool mNeedToEmpty; |
| 151 | }; |
| 152 | ObjDestroyOOB mObjDestroy; |
| 153 | bool objDestroyOOBInit(); |
| 154 | void objDestroyOOBRun(); |
| 155 | void objDestroyOOBDestroy(); |
| 156 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 157 | private: |
| 158 | Context(); |
| 159 | |
| 160 | void initEGL(); |
| 161 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 162 | bool runScript(Script *s, uint32_t launchID); |
Jason Sams | a09f11d | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 163 | bool runRootScript(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 164 | |
| 165 | static void * threadProc(void *); |
| 166 | |
| 167 | // todo: put in TLS |
| 168 | static Context *gCon; |
| 169 | Surface *mWndSurface; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 170 | |
| 171 | Vector<ObjectBase *> mNames; |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 172 | KeyedVector<String8,int> mInt32Defines; |
| 173 | KeyedVector<String8,float> mFloatDefines; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | |
| 177 | } |
| 178 | } |
| 179 | #endif |