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