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" |
Yang Ni | c31585b | 2015-02-15 11:43:50 -0800 | [diff] [blame] | 23 | #include "rsCppUtils.h" |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 24 | #include "rsElement.h" |
| 25 | #include "rsScriptC.h" |
David Gross | dced5c9 | 2015-03-11 16:12:42 -0700 | [diff] [blame] | 26 | #include "rsCpuCoreRuntime.h" |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 27 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace renderscript { |
| 30 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 31 | // Whether the CPU we're running on supports SIMD instructions |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 32 | extern bool gArchUseSIMD; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 33 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 34 | // Function types found in RenderScript code |
| 35 | typedef void (*ReduceFunc_t)(const uint8_t *inBuf, uint8_t *outBuf, uint32_t len); |
| 36 | typedef void (*ForEachFunc_t)(const RsExpandKernelDriverInfo *info, uint32_t x1, uint32_t x2, uint32_t outStride); |
| 37 | typedef void (*InvokeFunc_t)(void *params); |
| 38 | typedef void (*InitOrDtorFunc_t)(void); |
| 39 | typedef int (*RootFunc_t)(void); |
| 40 | |
| 41 | // Internal driver callback used to execute a kernel |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 42 | typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); |
| 43 | |
| 44 | class RsdCpuScriptImpl; |
| 45 | class RsdCpuReferenceImpl; |
| 46 | |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 47 | struct ScriptTLSStruct { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 48 | android::renderscript::Context * mContext; |
| 49 | const android::renderscript::Script * mScript; |
| 50 | RsdCpuScriptImpl *mImpl; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 51 | }; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 52 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 53 | // MTLaunchStruct passes information about a multithreaded kernel launch. |
| 54 | struct MTLaunchStructCommon { |
| 55 | RsdCpuReferenceImpl *rs; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 56 | RsdCpuScriptImpl *script; |
| 57 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 58 | uint32_t mSliceSize; |
| 59 | volatile int mSliceNum; |
| 60 | bool isThreadable; |
| 61 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 62 | // Boundary information about the launch |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 63 | RsLaunchDimensions start; |
| 64 | RsLaunchDimensions end; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 65 | // Points to MTLaunchStructForEach::fep::dim or |
| 66 | // MTLaunchStructReduce::inputDim. |
| 67 | RsLaunchDimensions *dimPtr; |
| 68 | }; |
| 69 | |
| 70 | struct MTLaunchStructForEach : public MTLaunchStructCommon { |
| 71 | // Driver info structure |
| 72 | RsExpandKernelDriverInfo fep; |
| 73 | |
| 74 | ForEachFunc_t kernel; |
| 75 | uint32_t sig; |
| 76 | const Allocation *ains[RS_KERNEL_INPUT_LIMIT]; |
| 77 | Allocation *aout[RS_KERNEL_INPUT_LIMIT]; |
| 78 | }; |
| 79 | |
| 80 | struct MTLaunchStructReduce : public MTLaunchStructCommon { |
| 81 | ReduceFunc_t kernel; |
| 82 | const uint8_t *inBuf; |
| 83 | uint8_t *outBuf; |
| 84 | RsLaunchDimensions inputDim; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 85 | }; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 86 | |
| 87 | class RsdCpuReferenceImpl : public RsdCpuReference { |
| 88 | public: |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 89 | ~RsdCpuReferenceImpl() override; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 90 | RsdCpuReferenceImpl(Context *); |
| 91 | |
| 92 | void lockMutex(); |
| 93 | void unlockMutex(); |
| 94 | |
| 95 | bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t); |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 96 | void setPriority(int32_t priority) override; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 97 | virtual void launchThreads(WorkerCallback_t cbk, void *data); |
| 98 | static void * helperThreadProc(void *vrsc); |
| 99 | RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc); |
| 100 | |
| 101 | Context * getContext() {return mRSC;} |
Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 102 | uint32_t getThreadCount() const { |
| 103 | return mWorkers.mCount + 1; |
| 104 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 105 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 106 | // Launch foreach kernel |
| 107 | void launchForEach(const Allocation **ains, uint32_t inLen, Allocation *aout, |
| 108 | const RsScriptCall *sc, MTLaunchStructForEach *mtls); |
| 109 | |
| 110 | // Launch a reduce kernel |
| 111 | void launchReduce(const Allocation *ain, Allocation *aout, |
| 112 | MTLaunchStructReduce *mtls); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 113 | |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 114 | CpuScript * createScript(const ScriptC *s, char const *resName, char const *cacheDir, |
| 115 | uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags) override; |
| 116 | CpuScript * createIntrinsic(const Script *s, RsScriptIntrinsicID iid, Element *e) override; |
| 117 | void* createScriptGroup(const ScriptGroupBase *sg) override; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 118 | |
| 119 | const RsdCpuReference::CpuSymbol *symLookup(const char *); |
| 120 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 121 | RsdCpuReference::CpuScript *lookupScript(const Script *s) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 122 | return mScriptLookupFn(mRSC, s); |
| 123 | } |
| 124 | |
Stephen Hines | 1d47662 | 2013-03-29 22:08:49 -0700 | [diff] [blame] | 125 | void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) { |
| 126 | mSelectRTCallback = pSelectRTCallback; |
| 127 | } |
| 128 | RSSelectRTCallback getSelectRTCallback() { |
| 129 | return mSelectRTCallback; |
| 130 | } |
Stephen Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 131 | |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 132 | virtual void setBccPluginName(const char *name) { |
Yang Ni | c31585b | 2015-02-15 11:43:50 -0800 | [diff] [blame] | 133 | mBccPluginName.setTo(name); |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 134 | } |
| 135 | virtual const char *getBccPluginName() const { |
Yang Ni | c31585b | 2015-02-15 11:43:50 -0800 | [diff] [blame] | 136 | return mBccPluginName.string(); |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 137 | } |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 138 | bool getInForEach() override { return mInForEach; } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 139 | |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 140 | // Set to true if we should embed global variable information in the code. |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 141 | void setEmbedGlobalInfo(bool v) override { |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 142 | mEmbedGlobalInfo = v; |
| 143 | } |
| 144 | |
| 145 | // Returns true if we should embed global variable information in the code. |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 146 | bool getEmbedGlobalInfo() const override { |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 147 | return mEmbedGlobalInfo; |
| 148 | } |
| 149 | |
| 150 | // Set to true if we should skip constant (immutable) global variables when |
| 151 | // potentially embedding information about globals. |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 152 | void setEmbedGlobalInfoSkipConstant(bool v) override { |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 153 | mEmbedGlobalInfoSkipConstant = v; |
| 154 | } |
| 155 | |
| 156 | // Returns true if we should skip constant (immutable) global variables when |
| 157 | // potentially embedding information about globals. |
Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 158 | bool getEmbedGlobalInfoSkipConstant() const override { |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 159 | return mEmbedGlobalInfoSkipConstant; |
| 160 | } |
| 161 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 162 | protected: |
| 163 | Context *mRSC; |
| 164 | uint32_t version_major; |
| 165 | uint32_t version_minor; |
| 166 | //bool mHasGraphics; |
| 167 | bool mInForEach; |
| 168 | |
| 169 | struct Workers { |
| 170 | volatile int mRunningCount; |
| 171 | volatile int mLaunchCount; |
| 172 | uint32_t mCount; |
| 173 | pthread_t *mThreadId; |
| 174 | pid_t *mNativeThreadId; |
| 175 | Signal mCompleteSignal; |
| 176 | Signal *mLaunchSignals; |
| 177 | WorkerCallback_t mLaunchCallback; |
| 178 | void *mLaunchData; |
| 179 | }; |
| 180 | Workers mWorkers; |
| 181 | bool mExit; |
| 182 | sym_lookup_t mSymLookupFn; |
| 183 | script_lookup_t mScriptLookupFn; |
| 184 | |
| 185 | ScriptTLSStruct mTlsStruct; |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 186 | |
Stephen Hines | 1d47662 | 2013-03-29 22:08:49 -0700 | [diff] [blame] | 187 | RSSelectRTCallback mSelectRTCallback; |
Yang Ni | c31585b | 2015-02-15 11:43:50 -0800 | [diff] [blame] | 188 | String8 mBccPluginName; |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 189 | |
| 190 | // Specifies whether we should embed global variable information in the |
| 191 | // code via special RS variables that can be examined later by the driver. |
| 192 | // Defaults to true. |
| 193 | bool mEmbedGlobalInfo; |
| 194 | |
| 195 | // Specifies whether we should skip constant (immutable) global variables |
| 196 | // when potentially embedding information about globals. |
| 197 | // Defaults to true. |
| 198 | bool mEmbedGlobalInfoSkipConstant; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | #endif |