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