rsCpuCore.h revision 4b2bea3dc20865f3a198797702e19912a6a2171c
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#include <string> 27 28namespace bcc { 29 class BCCContext; 30 class RSCompilerDriver; 31 class RSExecutable; 32} 33 34namespace android { 35namespace renderscript { 36 37struct StridePair { 38 uint32_t eStride; 39 uint32_t yStride; 40}; 41 42struct RsExpandKernelDriverInfo { 43 const void *usr; 44 uint32_t usrLen; 45 46 uint32_t dimX; 47 uint32_t dimY; 48 uint32_t dimZ; 49 50 const uint8_t *ptrIn; 51 uint8_t *ptrOut; 52 uint32_t eStrideIn; 53 uint32_t eStrideOut; 54 uint32_t yStrideIn; 55 uint32_t yStrideOut; 56 uint32_t slot; 57 58 const uint8_t** ptrIns; 59 StridePair* inStrides; 60 61 ~RsExpandKernelDriverInfo() { 62 if (ptrIns != NULL) { 63 delete[] ptrIns; 64 } 65 66 if (inStrides != NULL) { 67 delete[] inStrides; 68 } 69 } 70}; 71 72struct RsExpandKernelParams { 73 74 // Used by kernels 75 const void *in; 76 void *out; 77 uint32_t y; 78 uint32_t z; 79 uint32_t lid; 80 81 const void **ins; 82 uint32_t *eStrideIns; 83 84 // Used by ScriptGroup and user kernels. 85 const void *usr; 86 87 // Used by intrinsics 88 uint32_t dimX; 89 uint32_t dimY; 90 uint32_t dimZ; 91 92 /* 93 * FIXME: This is only used by the blend intrinsic. If possible, we should 94 * modify blur to not need it. 95 */ 96 uint32_t slot; 97 98 /// Copy fields needed by a kernel from a driver struct. 99 void takeFields(const RsExpandKernelDriverInfo &dstruct) { 100 this->usr = dstruct.usr; 101 this->slot = dstruct.slot; 102 103 this->dimX = dstruct.dimX; 104 this->dimY = dstruct.dimY; 105 this->dimZ = dstruct.dimZ; 106 } 107}; 108 109extern bool gArchUseSIMD; 110 111typedef void (* InvokeFunc_t)(void); 112typedef void (* ForEachFunc_t)(void); 113typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); 114 115class RsdCpuScriptImpl; 116class RsdCpuReferenceImpl; 117 118typedef struct ScriptTLSStructRec { 119 android::renderscript::Context * mContext; 120 const android::renderscript::Script * mScript; 121 RsdCpuScriptImpl *mImpl; 122} ScriptTLSStruct; 123 124typedef struct { 125 RsExpandKernelDriverInfo fep; 126 127 RsdCpuReferenceImpl *rsc; 128 RsdCpuScriptImpl *script; 129 130 ForEachFunc_t kernel; 131 uint32_t sig; 132 const Allocation * ain; 133 Allocation * aout; 134 135 uint32_t mSliceSize; 136 volatile int mSliceNum; 137 bool isThreadable; 138 139 uint32_t xStart; 140 uint32_t xEnd; 141 uint32_t yStart; 142 uint32_t yEnd; 143 uint32_t zStart; 144 uint32_t zEnd; 145 uint32_t arrayStart; 146 uint32_t arrayEnd; 147 148 // Multi-input data. 149 const Allocation ** ains; 150} MTLaunchStruct; 151 152 153 154 155class RsdCpuReferenceImpl : public RsdCpuReference { 156public: 157 virtual ~RsdCpuReferenceImpl(); 158 RsdCpuReferenceImpl(Context *); 159 160 void lockMutex(); 161 void unlockMutex(); 162 163 bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t); 164 virtual void setPriority(int32_t priority); 165 virtual void launchThreads(WorkerCallback_t cbk, void *data); 166 static void * helperThreadProc(void *vrsc); 167 RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc); 168 169 Context * getContext() {return mRSC;} 170 uint32_t getThreadCount() const { 171 return mWorkers.mCount + 1; 172 } 173 174 void launchThreads(const Allocation * ain, Allocation * aout, 175 const RsScriptCall *sc, MTLaunchStruct *mtls); 176 177 void launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout, 178 const RsScriptCall* sc, MTLaunchStruct* mtls); 179 180 virtual CpuScript * createScript(const ScriptC *s, 181 char const *resName, char const *cacheDir, 182 uint8_t const *bitcode, size_t bitcodeSize, 183 uint32_t flags); 184 virtual CpuScript * createIntrinsic(const Script *s, 185 RsScriptIntrinsicID iid, Element *e); 186 virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg); 187 188 const RsdCpuReference::CpuSymbol *symLookup(const char *); 189 190 RsdCpuReference::CpuScript * lookupScript(const Script *s) { 191 return mScriptLookupFn(mRSC, s); 192 } 193 194#ifndef RS_COMPATIBILITY_LIB 195 void setLinkRuntimeCallback( 196 bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) { 197 mLinkRuntimeCallback = pLinkRuntimeCallback; 198 } 199 bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() { 200 return mLinkRuntimeCallback; 201 } 202 203 void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) { 204 mSelectRTCallback = pSelectRTCallback; 205 } 206 RSSelectRTCallback getSelectRTCallback() { 207 return mSelectRTCallback; 208 } 209 210 virtual void setSetupCompilerCallback( 211 RSSetupCompilerCallback pSetupCompilerCallback) { 212 mSetupCompilerCallback = pSetupCompilerCallback; 213 } 214 virtual RSSetupCompilerCallback getSetupCompilerCallback() const { 215 return mSetupCompilerCallback; 216 } 217 218 virtual void setBccPluginName(const char *name) { 219 mBccPluginName.assign(name); 220 } 221 virtual const char *getBccPluginName() const { 222 return mBccPluginName.c_str(); 223 } 224#endif 225 virtual bool getInForEach() { return mInForEach; } 226 227protected: 228 Context *mRSC; 229 uint32_t version_major; 230 uint32_t version_minor; 231 //bool mHasGraphics; 232 bool mInForEach; 233 234 struct Workers { 235 volatile int mRunningCount; 236 volatile int mLaunchCount; 237 uint32_t mCount; 238 pthread_t *mThreadId; 239 pid_t *mNativeThreadId; 240 Signal mCompleteSignal; 241 Signal *mLaunchSignals; 242 WorkerCallback_t mLaunchCallback; 243 void *mLaunchData; 244 }; 245 Workers mWorkers; 246 bool mExit; 247 sym_lookup_t mSymLookupFn; 248 script_lookup_t mScriptLookupFn; 249 250 ScriptTLSStruct mTlsStruct; 251 252#ifndef RS_COMPATIBILITY_LIB 253 bcc::RSLinkRuntimeCallback mLinkRuntimeCallback; 254 RSSelectRTCallback mSelectRTCallback; 255 RSSetupCompilerCallback mSetupCompilerCallback; 256 std::string mBccPluginName; 257#endif 258}; 259 260 261} 262} 263 264#endif 265