1 /* 2 * Copyright (C) 2012 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 RSD_CPU_CORE_H 18 #define RSD_CPU_CORE_H 19 20 #include "rsd_cpu.h" 21 #include "rsSignal.h" 22 #include "rsContext.h" 23 #include "rsElement.h" 24 #include "rsScriptC.h" 25 26 namespace bcc { 27 class BCCContext; 28 class RSCompilerDriver; 29 class RSExecutable; 30 } 31 32 namespace android { 33 namespace renderscript { 34 35 extern bool gArchUseSIMD; 36 37 typedef void (* InvokeFunc_t)(void); 38 typedef void (* ForEachFunc_t)(void); 39 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); 40 41 class RsdCpuScriptImpl; 42 class RsdCpuReferenceImpl; 43 44 typedef struct ScriptTLSStructRec { 45 android::renderscript::Context * mContext; 46 const android::renderscript::Script * mScript; 47 RsdCpuScriptImpl *mImpl; 48 } ScriptTLSStruct; 49 50 typedef struct { 51 RsForEachStubParamStruct fep; 52 53 RsdCpuReferenceImpl *rsc; 54 RsdCpuScriptImpl *script; 55 56 ForEachFunc_t kernel; 57 uint32_t sig; 58 const Allocation * ain; 59 Allocation * aout; 60 61 uint32_t mSliceSize; 62 volatile int mSliceNum; 63 bool isThreadable; 64 65 uint32_t xStart; 66 uint32_t xEnd; 67 uint32_t yStart; 68 uint32_t yEnd; 69 uint32_t zStart; 70 uint32_t zEnd; 71 uint32_t arrayStart; 72 uint32_t arrayEnd; 73 } MTLaunchStruct; 74 75 76 77 78 class RsdCpuReferenceImpl : public RsdCpuReference { 79 public: 80 virtual ~RsdCpuReferenceImpl(); 81 RsdCpuReferenceImpl(Context *); 82 83 void lockMutex(); 84 void unlockMutex(); 85 86 bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t); 87 virtual void setPriority(int32_t priority); 88 virtual void launchThreads(WorkerCallback_t cbk, void *data); 89 static void * helperThreadProc(void *vrsc); 90 RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc); 91 92 Context * getContext() {return mRSC;} 93 uint32_t getThreadCount() const { 94 return mWorkers.mCount + 1; 95 } 96 97 void launchThreads(const Allocation * ain, Allocation * aout, 98 const RsScriptCall *sc, MTLaunchStruct *mtls); 99 100 virtual CpuScript * createScript(const ScriptC *s, 101 char const *resName, char const *cacheDir, 102 uint8_t const *bitcode, size_t bitcodeSize, 103 uint32_t flags); 104 virtual CpuScript * createIntrinsic(const Script *s, 105 RsScriptIntrinsicID iid, Element *e); 106 virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg); 107 108 const RsdCpuReference::CpuSymbol *symLookup(const char *); 109 110 RsdCpuReference::CpuScript * lookupScript(const Script *s) { 111 return mScriptLookupFn(mRSC, s); 112 } 113 114 #ifndef RS_COMPATIBILITY_LIB 115 void setLinkRuntimeCallback( 116 bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) { 117 mLinkRuntimeCallback = pLinkRuntimeCallback; 118 } 119 bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() { 120 return mLinkRuntimeCallback; 121 } 122 123 void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) { 124 mSelectRTCallback = pSelectRTCallback; 125 } 126 RSSelectRTCallback getSelectRTCallback() { 127 return mSelectRTCallback; 128 } 129 130 virtual void setSetupCompilerCallback( 131 RSSetupCompilerCallback pSetupCompilerCallback) { 132 mSetupCompilerCallback = pSetupCompilerCallback; 133 } 134 virtual RSSetupCompilerCallback getSetupCompilerCallback() const { 135 return mSetupCompilerCallback; 136 } 137 #endif 138 virtual bool getInForEach() { return mInForEach; } 139 140 protected: 141 Context *mRSC; 142 uint32_t version_major; 143 uint32_t version_minor; 144 //bool mHasGraphics; 145 bool mInForEach; 146 147 struct Workers { 148 volatile int mRunningCount; 149 volatile int mLaunchCount; 150 uint32_t mCount; 151 pthread_t *mThreadId; 152 pid_t *mNativeThreadId; 153 Signal mCompleteSignal; 154 Signal *mLaunchSignals; 155 WorkerCallback_t mLaunchCallback; 156 void *mLaunchData; 157 }; 158 Workers mWorkers; 159 bool mExit; 160 sym_lookup_t mSymLookupFn; 161 script_lookup_t mScriptLookupFn; 162 163 ScriptTLSStruct mTlsStruct; 164 165 #ifndef RS_COMPATIBILITY_LIB 166 bcc::RSLinkRuntimeCallback mLinkRuntimeCallback; 167 RSSelectRTCallback mSelectRTCallback; 168 RSSetupCompilerCallback mSetupCompilerCallback; 169 #endif 170 }; 171 172 173 } 174 } 175 176 #endif 177