| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 ANDROID_RENDERSCRIPT_EXECUTABLE_H |
| 18 | #define ANDROID_RENDERSCRIPT_EXECUTABLE_H |
| 19 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 20 | |
| 21 | #include "rsCpuScript.h" |
| 22 | |
| Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string> |
| 25 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace renderscript { |
| 28 | |
| 29 | class Context; |
| 30 | |
| 31 | class SharedLibraryUtils { |
| 32 | public: |
| 33 | #ifndef RS_COMPATIBILITY_LIB |
| Stephen Hines | 4c368af | 2015-05-06 00:43:02 -0700 | [diff] [blame] | 34 | static bool createSharedLibrary(const char* driverName, |
| 35 | const char* cacheDir, |
| 36 | const char* resName); |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | // Load the shared library referred to by cacheDir and resName. If we have |
| 40 | // already loaded this library, we instead create a new copy (in the |
| 41 | // cache dir) and then load that. We then immediately destroy the copy. |
| 42 | // This is required behavior to implement script instancing for the support |
| 43 | // library, since shared objects are loaded and de-duped by name only. |
| 44 | |
| 45 | // For 64bit RS Support Lib, the shared lib path cannot be constructed from |
| 46 | // cacheDir, so nativeLibDir is needed to load shared libs. |
| 47 | static void* loadSharedLibrary(const char *cacheDir, const char *resName, |
| Yang Ni | 1efae29 | 2015-06-27 15:45:18 -0700 | [diff] [blame] | 48 | const char *nativeLibDir = nullptr, |
| 49 | bool *alreadyLoaded = nullptr); |
| 50 | |
| 51 | // Create a len length string containing random characters from [A-Za-z0-9]. |
| Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 52 | static std::string getRandomString(size_t len); |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | // Attempt to load the shared library from origName, but then fall back to |
| 56 | // creating a copy of the shared library if necessary (to ensure instancing). |
| 57 | // This function returns the dlopen()-ed handle if successful. |
| 58 | static void *loadSOHelper(const char *origName, const char *cacheDir, |
| Yang Ni | 1efae29 | 2015-06-27 15:45:18 -0700 | [diff] [blame] | 59 | const char *resName, bool* alreadyLoaded = nullptr); |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 60 | |
| 61 | static const char* LD_EXE_PATH; |
| 62 | static const char* RS_CACHE_DIR; |
| 63 | }; |
| 64 | |
| 65 | class ScriptExecutable { |
| 66 | public: |
| Yang Ni | 5e48002 | 2016-04-06 09:34:34 -0700 | [diff] [blame] | 67 | ScriptExecutable(void** fieldAddress, bool* fieldIsObject, |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 68 | const char* const * fieldName, size_t varCount, |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 69 | InvokeFunc_t* invokeFunctions, size_t funcCount, |
| 70 | ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures, |
| 71 | size_t forEachCount, |
| David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 72 | ReduceDescription *reduceDescriptions, size_t reduceCount, |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 73 | const char** pragmaKeys, const char** pragmaValues, |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 74 | size_t pragmaCount, |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 75 | const char **globalNames, const void **globalAddresses, |
| Stephen Hines | 5aa018c | 2015-05-20 18:09:57 -0700 | [diff] [blame] | 76 | const size_t *globalSizes, |
| 77 | const uint32_t *globalProperties, size_t globalEntries, |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 78 | bool isThreadable, uint32_t buildChecksum) : |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 79 | mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject), |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 80 | mFieldName(fieldName), mExportedVarCount(varCount), |
| 81 | mInvokeFunctions(invokeFunctions), mFuncCount(funcCount), |
| 82 | mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures), |
| 83 | mForEachCount(forEachCount), |
| David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 84 | mReduceDescriptions(reduceDescriptions), mReduceCount(reduceCount), |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 85 | mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues), |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 86 | mPragmaCount(pragmaCount), mGlobalNames(globalNames), |
| 87 | mGlobalAddresses(globalAddresses), mGlobalSizes(globalSizes), |
| Stephen Hines | 5aa018c | 2015-05-20 18:09:57 -0700 | [diff] [blame] | 88 | mGlobalProperties(globalProperties), mGlobalEntries(globalEntries), |
| Yang Ni | 5e48002 | 2016-04-06 09:34:34 -0700 | [diff] [blame] | 89 | mIsThreadable(isThreadable), mBuildChecksum(buildChecksum) { |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | ~ScriptExecutable() { |
| 93 | for (size_t i = 0; i < mExportedVarCount; ++i) { |
| 94 | if (mFieldIsObject[i]) { |
| 95 | if (mFieldAddress[i] != nullptr) { |
| 96 | rs_object_base *obj_addr = |
| 97 | reinterpret_cast<rs_object_base *>(mFieldAddress[i]); |
| Yang Ni | 5e48002 | 2016-04-06 09:34:34 -0700 | [diff] [blame] | 98 | rsrClearObject(obj_addr); |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | for (size_t i = 0; i < mPragmaCount; ++i) { |
| 104 | delete [] mPragmaKeys[i]; |
| 105 | delete [] mPragmaValues[i]; |
| 106 | } |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 107 | delete[] mPragmaValues; |
| 108 | delete[] mPragmaKeys; |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 109 | |
| David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 110 | delete[] mReduceDescriptions; |
| David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 111 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 112 | delete[] mForEachSignatures; |
| 113 | delete[] mForEachFunctions; |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 114 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 115 | delete[] mInvokeFunctions; |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 116 | |
| 117 | for (size_t i = 0; i < mExportedVarCount; i++) { |
| 118 | delete[] mFieldName[i]; |
| 119 | } |
| 120 | delete[] mFieldName; |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 121 | delete[] mFieldIsObject; |
| 122 | delete[] mFieldAddress; |
| 123 | } |
| 124 | |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 125 | // Create an ScriptExecutable object from a shared object. |
| 126 | // If expectedChecksum is not zero, it will be compared to the checksum |
| 127 | // embedded in the shared object. A mismatch will cause a failure. |
| 128 | // If succeeded, returns the new object. Otherwise, returns nullptr. |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 129 | static ScriptExecutable* |
| Yang Ni | 5e48002 | 2016-04-06 09:34:34 -0700 | [diff] [blame] | 130 | createFromSharedObject(void* sharedObj, |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 131 | uint32_t expectedChecksum = 0); |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 132 | |
| 133 | size_t getExportedVariableCount() const { return mExportedVarCount; } |
| 134 | size_t getExportedFunctionCount() const { return mFuncCount; } |
| 135 | size_t getExportedForEachCount() const { return mForEachCount; } |
| Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 136 | size_t getExportedReduceCount() const { return mReduceCount; } |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 137 | size_t getPragmaCount() const { return mPragmaCount; } |
| 138 | |
| 139 | void* getFieldAddress(int slot) const { return mFieldAddress[slot]; } |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 140 | void* getFieldAddress(const char* name) const; |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 141 | bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; } |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 142 | const char* getFieldName(int slot) const { return mFieldName[slot]; } |
| 143 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 144 | InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; } |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 145 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 146 | ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; } |
| 147 | uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; } |
| 148 | |
| David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 149 | const ReduceDescription* getReduceDescription(int slot) const { |
| 150 | return &mReduceDescriptions[slot]; |
| David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 153 | const char ** getPragmaKeys() const { return mPragmaKeys; } |
| 154 | const char ** getPragmaValues() const { return mPragmaValues; } |
| 155 | |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 156 | const char* getGlobalName(int i) const { |
| 157 | if (i < mGlobalEntries) { |
| 158 | return mGlobalNames[i]; |
| 159 | } else { |
| 160 | return nullptr; |
| 161 | } |
| 162 | } |
| 163 | const void* getGlobalAddress(int i) const { |
| 164 | if (i < mGlobalEntries) { |
| 165 | return mGlobalAddresses[i]; |
| 166 | } else { |
| 167 | return nullptr; |
| 168 | } |
| 169 | } |
| 170 | size_t getGlobalSize(int i) const { |
| 171 | if (i < mGlobalEntries) { |
| 172 | return mGlobalSizes[i]; |
| 173 | } else { |
| 174 | return 0; |
| 175 | } |
| 176 | } |
| Stephen Hines | 5aa018c | 2015-05-20 18:09:57 -0700 | [diff] [blame] | 177 | uint32_t getGlobalProperties(int i) const { |
| 178 | if (i < mGlobalEntries) { |
| 179 | return mGlobalProperties[i]; |
| 180 | } else { |
| 181 | return 0; |
| 182 | } |
| 183 | } |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 184 | int getGlobalEntries() const { return mGlobalEntries; } |
| 185 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 186 | bool getThreadable() const { return mIsThreadable; } |
| 187 | |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 188 | uint32_t getBuildChecksum() const { return mBuildChecksum; } |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 189 | |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 190 | bool dumpGlobalInfo() const; |
| 191 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 192 | private: |
| 193 | void** mFieldAddress; |
| 194 | bool* mFieldIsObject; |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 195 | const char* const * mFieldName; |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 196 | size_t mExportedVarCount; |
| 197 | |
| 198 | InvokeFunc_t* mInvokeFunctions; |
| 199 | size_t mFuncCount; |
| 200 | |
| 201 | ForEachFunc_t* mForEachFunctions; |
| 202 | uint32_t* mForEachSignatures; |
| 203 | size_t mForEachCount; |
| 204 | |
| David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 205 | ReduceDescription* mReduceDescriptions; |
| Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 206 | size_t mReduceCount; |
| 207 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 208 | const char ** mPragmaKeys; |
| 209 | const char ** mPragmaValues; |
| 210 | size_t mPragmaCount; |
| 211 | |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 212 | const char ** mGlobalNames; |
| 213 | const void ** mGlobalAddresses; |
| 214 | const size_t * mGlobalSizes; |
| Stephen Hines | 5aa018c | 2015-05-20 18:09:57 -0700 | [diff] [blame] | 215 | const uint32_t * mGlobalProperties; |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 216 | int mGlobalEntries; |
| 217 | |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 218 | bool mIsThreadable; |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 219 | uint32_t mBuildChecksum; |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | } // namespace renderscript |
| 223 | } // namespace android |
| 224 | |
| 225 | #endif // ANDROID_RENDERSCRIPT_EXECUTABLE_H |