Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011-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_BCC_H |
| 18 | #define RSD_BCC_H |
| 19 | |
| 20 | #include <rs_hal.h> |
| 21 | #include <rsRuntime.h> |
| 22 | |
Stephen Hines | 25e3af5 | 2014-05-21 21:23:08 -0700 | [diff] [blame] | 23 | #ifndef RS_COMPATIBILITY_LIB |
Tim Murray | 29809d1 | 2014-05-28 12:04:19 -0700 | [diff] [blame] | 24 | #include <utility> |
Stephen Hines | 25e3af5 | 2014-05-21 21:23:08 -0700 | [diff] [blame] | 25 | #endif |
| 26 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 27 | #include "rsCpuCore.h" |
| 28 | |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 31 | namespace bcc { |
| 32 | class BCCContext; |
| 33 | class RSCompilerDriver; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 36 | namespace bcinfo { |
| 37 | class MetadataExtractor; |
| 38 | } |
| 39 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace renderscript { |
| 42 | |
Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 43 | class SharedLibraryUtils { |
| 44 | public: |
| 45 | #ifndef RS_COMPATIBILITY_LIB |
| 46 | static bool createSharedLibrary(const char* cacheDir, const char* resName); |
| 47 | #endif |
| 48 | |
| 49 | // Load the shared library referred to by cacheDir and resName. If we have |
| 50 | // already loaded this library, we instead create a new copy (in the |
| 51 | // cache dir) and then load that. We then immediately destroy the copy. |
| 52 | // This is required behavior to implement script instancing for the support |
| 53 | // library, since shared objects are loaded and de-duped by name only. |
| 54 | static void* loadSharedLibrary(const char* cacheDir, const char* resName); |
| 55 | |
| 56 | private: |
| 57 | // Attempt to load the shared library from origName, but then fall back to |
| 58 | // creating a copy of the shared library if necessary (to ensure instancing). |
| 59 | // This function returns the dlopen()-ed handle if successful. |
| 60 | static void *loadSOHelper(const char *origName, const char *cacheDir, |
| 61 | const char *resName); |
| 62 | |
| 63 | static const char* LD_EXE_PATH; |
| 64 | static const char* RS_CACHE_DIR; |
| 65 | }; |
| 66 | |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 67 | class ScriptExecutable { |
| 68 | public: |
| 69 | ScriptExecutable(Context* RSContext, |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 70 | void** fieldAddress, bool* fieldIsObject, size_t varCount, |
| 71 | InvokeFunc_t* invokeFunctions, size_t funcCount, |
| 72 | ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures, |
| 73 | size_t forEachCount, |
| 74 | const char ** pragmaKeys, const char ** pragmaValues, |
| 75 | size_t pragmaCount, |
| 76 | bool isThreadable) : |
| 77 | mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject), |
| 78 | mExportedVarCount(varCount), |
| 79 | mInvokeFunctions(invokeFunctions), mFuncCount(funcCount), |
| 80 | mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures), |
| 81 | mForEachCount(forEachCount), |
| 82 | mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues), |
| 83 | mPragmaCount(pragmaCount), |
| 84 | mIsThreadable(isThreadable), mRS(RSContext) { |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 85 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 86 | |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 87 | ~ScriptExecutable() { |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 88 | for (size_t i = 0; i < mExportedVarCount; ++i) { |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 89 | if (mFieldIsObject[i]) { |
| 90 | if (mFieldAddress[i] != nullptr) { |
| 91 | rs_object_base *obj_addr = |
| 92 | reinterpret_cast<rs_object_base *>(mFieldAddress[i]); |
| 93 | rsrClearObject(mRS, obj_addr); |
| 94 | } |
| 95 | } |
| 96 | } |
Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 97 | |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 98 | for (size_t i = 0; i < mPragmaCount; ++i) { |
Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 99 | delete [] mPragmaKeys[i]; |
| 100 | delete [] mPragmaValues[i]; |
| 101 | } |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 102 | |
| 103 | delete[] mPragmaValues; |
| 104 | delete[] mPragmaKeys; |
| 105 | delete[] mForEachSignatures; |
| 106 | delete[] mForEachFunctions; |
| 107 | delete[] mInvokeFunctions; |
| 108 | delete[] mFieldIsObject; |
| 109 | delete[] mFieldAddress; |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static ScriptExecutable* |
| 113 | createFromSharedObject(Context* RSContext, void* sharedObj); |
| 114 | |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 115 | size_t getExportedVariableCount() const { return mExportedVarCount; } |
| 116 | size_t getExportedFunctionCount() const { return mFuncCount; } |
| 117 | size_t getExportedForEachCount() const { return mForEachCount; } |
| 118 | size_t getPragmaCount() const { return mPragmaCount; } |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 119 | |
| 120 | void* getFieldAddress(int slot) const { return mFieldAddress[slot]; } |
| 121 | bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; } |
| 122 | InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; } |
| 123 | ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; } |
| 124 | uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; } |
| 125 | |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 126 | const char ** getPragmaKeys() const { return mPragmaKeys; } |
| 127 | const char ** getPragmaValues() const { return mPragmaValues; } |
Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 128 | |
| 129 | bool getThreadable() const { return mIsThreadable; } |
Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 130 | |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 131 | private: |
Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 132 | void** mFieldAddress; |
| 133 | bool* mFieldIsObject; |
| 134 | size_t mExportedVarCount; |
| 135 | |
| 136 | InvokeFunc_t* mInvokeFunctions; |
| 137 | size_t mFuncCount; |
| 138 | |
| 139 | ForEachFunc_t* mForEachFunctions; |
| 140 | uint32_t* mForEachSignatures; |
| 141 | size_t mForEachCount; |
| 142 | |
| 143 | const char ** mPragmaKeys; |
| 144 | const char ** mPragmaValues; |
| 145 | size_t mPragmaCount; |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 146 | |
Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 147 | bool mIsThreadable; |
| 148 | |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 149 | Context* mRS; |
| 150 | }; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 151 | |
| 152 | class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript { |
| 153 | public: |
| 154 | typedef void (*outer_foreach_t)( |
Chris Wailes | 80ef693 | 2014-07-08 11:22:18 -0700 | [diff] [blame] | 155 | const RsExpandKernelParams *, |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 156 | uint32_t x1, uint32_t x2, |
Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 157 | uint32_t outstep); |
Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 158 | |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 159 | typedef void (* InvokeFunc_t)(void); |
| 160 | typedef void (* ForEachFunc_t)(void); |
| 161 | typedef int (* RootFunc_t)(void); |
Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 162 | #ifdef RS_COMPATIBILITY_LIB |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 163 | typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); |
| 164 | #endif |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 165 | |
| 166 | bool init(char const *resName, char const *cacheDir, |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 167 | uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags, |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 168 | char const *bccPluginName = nullptr); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 169 | virtual void populateScript(Script *); |
| 170 | |
| 171 | virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength); |
| 172 | virtual int invokeRoot(); |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 173 | virtual void preLaunch(uint32_t slot, const Allocation ** ains, |
| 174 | uint32_t inLen, Allocation * aout, const void * usr, |
Jason Sams | 17e3cdc | 2013-09-09 17:32:16 -0700 | [diff] [blame] | 175 | uint32_t usrLen, const RsScriptCall *sc); |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 176 | virtual void postLaunch(uint32_t slot, const Allocation ** ains, |
| 177 | uint32_t inLen, Allocation * aout, |
| 178 | const void * usr, uint32_t usrLen, |
| 179 | const RsScriptCall *sc); |
Chris Wailes | 818cfa0 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 180 | |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 181 | virtual void invokeForEach(uint32_t slot, |
| 182 | const Allocation ** ains, |
| 183 | uint32_t inLen, |
| 184 | Allocation* aout, |
| 185 | const void* usr, |
| 186 | uint32_t usrLen, |
| 187 | const RsScriptCall* sc); |
| 188 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 189 | virtual void invokeInit(); |
| 190 | virtual void invokeFreeChildren(); |
| 191 | |
| 192 | virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength); |
Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 193 | virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 194 | virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, |
Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 195 | const Element *e, const uint32_t *dims, size_t dimLength); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 196 | virtual void setGlobalBind(uint32_t slot, Allocation *data); |
| 197 | virtual void setGlobalObj(uint32_t slot, ObjectBase *data); |
| 198 | |
| 199 | |
| 200 | virtual ~RsdCpuScriptImpl(); |
| 201 | RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s); |
| 202 | |
| 203 | const Script * getScript() {return mScript;} |
| 204 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 205 | bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen, |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 206 | Allocation * aout, const void * usr, uint32_t usrLen, |
| 207 | const RsScriptCall *sc, MTLaunchStruct *mtls); |
| 208 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 209 | virtual void forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls); |
| 210 | |
| 211 | |
| 212 | const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym); |
| 213 | static void * lookupRuntimeStub(void* pContext, char const* name); |
| 214 | |
| 215 | virtual Allocation * getAllocationForPointer(const void *ptr) const; |
Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 216 | bool storeRSInfoFromSO(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 217 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 218 | protected: |
| 219 | RsdCpuReferenceImpl *mCtx; |
| 220 | const Script *mScript; |
Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 221 | void *mScriptSO; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 222 | |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 223 | #ifndef RS_COMPATIBILITY_LIB |
Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 224 | // Returns the path to the core library we'll use. |
| 225 | const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode, |
Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 226 | size_t bitcodeSize); |
Tim Murray | 29809d1 | 2014-05-28 12:04:19 -0700 | [diff] [blame] | 227 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 228 | bcc::RSCompilerDriver *mCompilerDriver; |
Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 229 | #endif |
| 230 | |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 231 | RootFunc_t mRoot; |
| 232 | RootFunc_t mRootExpand; |
| 233 | InvokeFunc_t mInit; |
| 234 | InvokeFunc_t mFreeChildren; |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 235 | ScriptExecutable* mScriptExec; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 236 | |
| 237 | Allocation **mBoundAllocs; |
| 238 | void * mIntrinsicData; |
| 239 | bool mIsThreadable; |
Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 240 | |
| 241 | public: |
| 242 | static const char* BCC_EXE_PATH; |
| 243 | const std::string& getBitcodeFilePath() const { return mBitcodeFilePath; } |
| 244 | |
| 245 | private: |
| 246 | std::string mBitcodeFilePath; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 247 | }; |
| 248 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 249 | Allocation * rsdScriptGetAllocationForPointer( |
| 250 | const Context *dc, |
| 251 | const Script *script, |
| 252 | const void *); |
| 253 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 254 | } |
Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 255 | |
Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 256 | #ifdef __LP64__ |
| 257 | #define SYSLIBPATH "/system/lib64" |
| 258 | #else |
| 259 | #define SYSLIBPATH "/system/lib" |
| 260 | #endif |
| 261 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | #endif |