rsCpuExecutable.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifndef ANDROID_RENDERSCRIPT_EXECUTABLE_H
  17. #define ANDROID_RENDERSCRIPT_EXECUTABLE_H
  18. #include "rsCpuScript.h"
  19. #include <stdlib.h>
  20. #include <string>
  21. namespace android {
  22. namespace renderscript {
  23. class Context;
  24. class SharedLibraryUtils {
  25. public:
  26. #ifndef RS_COMPATIBILITY_LIB
  27. // Creates a shared library in cacheDir for the bitcode named resName.
  28. // If reuse is false and SOPath is not nullptr, saves the filename
  29. // used for the shared library in SOPath.
  30. static bool createSharedLibrary(const char* driverName,
  31. const char* cacheDir,
  32. const char* resName,
  33. const bool reuse = true,
  34. std::string *SOPath = nullptr);
  35. #endif
  36. // Load the shared library referred to by cacheDir and resName. If we have
  37. // already loaded this library, we instead create a new copy (in the
  38. // cache dir) and then load that. We then immediately destroy the copy.
  39. // This is required behavior to implement script instancing for the support
  40. // library, since shared objects are loaded and de-duped by name only.
  41. // For 64bit RS Support Lib, the shared lib path cannot be constructed from
  42. // cacheDir, so nativeLibDir is needed to load shared libs.
  43. static void* loadSharedLibrary(const char *cacheDir, const char *resName,
  44. const char *nativeLibDir = nullptr,
  45. bool *alreadyLoaded = nullptr);
  46. // Load the shared library referred to by fullPath, and delete it right
  47. // after loading it. Files loaded by this function are only used once, e.g.,
  48. // shared libraries generated for scripts in a debug context. Deleting them
  49. // is OK in this case since the shared libraries have already been dlopened.
  50. // Deletion is also required because such files are not intended for reuse.
  51. static void* loadAndDeleteSharedLibrary(const char *fullPath);
  52. // Create a len length string containing random characters from [A-Za-z0-9].
  53. static std::string getRandomString(size_t len);
  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,
  59. const char *resName, bool* alreadyLoaded = nullptr);
  60. static const char* LD_EXE_PATH;
  61. static const char* RS_CACHE_DIR;
  62. };
  63. class ScriptExecutable {
  64. public:
  65. ScriptExecutable(void** fieldAddress, bool* fieldIsObject,
  66. const char* const * fieldName, size_t varCount,
  67. InvokeFunc_t* invokeFunctions, size_t funcCount,
  68. ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures,
  69. size_t forEachCount,
  70. ReduceDescription *reduceDescriptions, size_t reduceCount,
  71. const char** pragmaKeys, const char** pragmaValues,
  72. size_t pragmaCount,
  73. const char **globalNames, const void **globalAddresses,
  74. const size_t *globalSizes,
  75. const uint32_t *globalProperties, size_t globalEntries,
  76. bool isThreadable, uint32_t buildChecksum) :
  77. mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject),
  78. mFieldName(fieldName), mExportedVarCount(varCount),
  79. mInvokeFunctions(invokeFunctions), mFuncCount(funcCount),
  80. mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures),
  81. mForEachCount(forEachCount),
  82. mReduceDescriptions(reduceDescriptions), mReduceCount(reduceCount),
  83. mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues),
  84. mPragmaCount(pragmaCount), mGlobalNames(globalNames),
  85. mGlobalAddresses(globalAddresses), mGlobalSizes(globalSizes),
  86. mGlobalProperties(globalProperties), mGlobalEntries(globalEntries),
  87. mIsThreadable(isThreadable), mBuildChecksum(buildChecksum) {
  88. }
  89. ~ScriptExecutable() {
  90. for (size_t i = 0; i < mExportedVarCount; ++i) {
  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(obj_addr);
  96. }
  97. }
  98. }
  99. for (size_t i = 0; i < mPragmaCount; ++i) {
  100. delete [] mPragmaKeys[i];
  101. delete [] mPragmaValues[i];
  102. }
  103. delete[] mPragmaValues;
  104. delete[] mPragmaKeys;
  105. delete[] mReduceDescriptions;
  106. delete[] mForEachSignatures;
  107. delete[] mForEachFunctions;
  108. delete[] mInvokeFunctions;
  109. for (size_t i = 0; i < mExportedVarCount; i++) {
  110. delete[] mFieldName[i];
  111. }
  112. delete[] mFieldName;
  113. delete[] mFieldIsObject;
  114. delete[] mFieldAddress;
  115. }
  116. // Create an ScriptExecutable object from a shared object.
  117. // If expectedChecksum is not zero, it will be compared to the checksum
  118. // embedded in the shared object. A mismatch will cause a failure.
  119. // If succeeded, returns the new object. Otherwise, returns nullptr.
  120. static ScriptExecutable*
  121. createFromSharedObject(void* sharedObj,
  122. uint32_t expectedChecksum = 0);
  123. size_t getExportedVariableCount() const { return mExportedVarCount; }
  124. size_t getExportedFunctionCount() const { return mFuncCount; }
  125. size_t getExportedForEachCount() const { return mForEachCount; }
  126. size_t getExportedReduceCount() const { return mReduceCount; }
  127. size_t getPragmaCount() const { return mPragmaCount; }
  128. void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
  129. void* getFieldAddress(const char* name) const;
  130. bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
  131. const char* getFieldName(int slot) const { return mFieldName[slot]; }
  132. InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
  133. ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
  134. uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
  135. const ReduceDescription* getReduceDescription(int slot) const {
  136. return &mReduceDescriptions[slot];
  137. }
  138. const char ** getPragmaKeys() const { return mPragmaKeys; }
  139. const char ** getPragmaValues() const { return mPragmaValues; }
  140. const char* getGlobalName(int i) const {
  141. if (i < mGlobalEntries) {
  142. return mGlobalNames[i];
  143. } else {
  144. return nullptr;
  145. }
  146. }
  147. const void* getGlobalAddress(int i) const {
  148. if (i < mGlobalEntries) {
  149. return mGlobalAddresses[i];
  150. } else {
  151. return nullptr;
  152. }
  153. }
  154. size_t getGlobalSize(int i) const {
  155. if (i < mGlobalEntries) {
  156. return mGlobalSizes[i];
  157. } else {
  158. return 0;
  159. }
  160. }
  161. uint32_t getGlobalProperties(int i) const {
  162. if (i < mGlobalEntries) {
  163. return mGlobalProperties[i];
  164. } else {
  165. return 0;
  166. }
  167. }
  168. int getGlobalEntries() const { return mGlobalEntries; }
  169. bool getThreadable() const { return mIsThreadable; }
  170. uint32_t getBuildChecksum() const { return mBuildChecksum; }
  171. bool dumpGlobalInfo() const;
  172. private:
  173. void** mFieldAddress;
  174. bool* mFieldIsObject;
  175. const char* const * mFieldName;
  176. size_t mExportedVarCount;
  177. InvokeFunc_t* mInvokeFunctions;
  178. size_t mFuncCount;
  179. ForEachFunc_t* mForEachFunctions;
  180. uint32_t* mForEachSignatures;
  181. size_t mForEachCount;
  182. ReduceDescription* mReduceDescriptions;
  183. size_t mReduceCount;
  184. const char ** mPragmaKeys;
  185. const char ** mPragmaValues;
  186. size_t mPragmaCount;
  187. const char ** mGlobalNames;
  188. const void ** mGlobalAddresses;
  189. const size_t * mGlobalSizes;
  190. const uint32_t * mGlobalProperties;
  191. int mGlobalEntries;
  192. bool mIsThreadable;
  193. uint32_t mBuildChecksum;
  194. };
  195. } // namespace renderscript
  196. } // namespace android
  197. #endif // ANDROID_RENDERSCRIPT_EXECUTABLE_H