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