rsCpuExecutable.h revision 6c1876bbef1b2c89975dce91230a168bd2d2ce4c
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 24namespace android { 25namespace renderscript { 26 27class Context; 28 29class SharedLibraryUtils { 30public: 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 52private: 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 63class ScriptExecutable { 64public: 65 ScriptExecutable(Context* RSContext, 66 void** fieldAddress, bool* fieldIsObject, 67 const char* const * fieldName, size_t varCount, 68 InvokeFunc_t* invokeFunctions, size_t funcCount, 69 ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures, 70 size_t forEachCount, 71 ReduceFunc_t* reduceFunctions, size_t reduceCount, 72 ReduceNewDescription *reduceNewDescriptions, size_t reduceNewCount, 73 const char** pragmaKeys, const char** pragmaValues, 74 size_t pragmaCount, 75 const char **globalNames, const void **globalAddresses, 76 const size_t *globalSizes, 77 const uint32_t *globalProperties, size_t globalEntries, 78 bool isThreadable, uint32_t buildChecksum) : 79 mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject), 80 mFieldName(fieldName), mExportedVarCount(varCount), 81 mInvokeFunctions(invokeFunctions), mFuncCount(funcCount), 82 mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures), 83 mForEachCount(forEachCount), 84 mReduceFunctions(reduceFunctions), mReduceCount(reduceCount), 85 mReduceNewDescriptions(reduceNewDescriptions), mReduceNewCount(reduceNewCount), 86 mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues), 87 mPragmaCount(pragmaCount), mGlobalNames(globalNames), 88 mGlobalAddresses(globalAddresses), mGlobalSizes(globalSizes), 89 mGlobalProperties(globalProperties), mGlobalEntries(globalEntries), 90 mIsThreadable(isThreadable), mBuildChecksum(buildChecksum), 91 mRS(RSContext) { 92 } 93 94 ~ScriptExecutable() { 95 for (size_t i = 0; i < mExportedVarCount; ++i) { 96 if (mFieldIsObject[i]) { 97 if (mFieldAddress[i] != nullptr) { 98 rs_object_base *obj_addr = 99 reinterpret_cast<rs_object_base *>(mFieldAddress[i]); 100 rsrClearObject(mRS, obj_addr); 101 } 102 } 103 } 104 105 for (size_t i = 0; i < mPragmaCount; ++i) { 106 delete [] mPragmaKeys[i]; 107 delete [] mPragmaValues[i]; 108 } 109 delete[] mPragmaValues; 110 delete[] mPragmaKeys; 111 112 delete[] mReduceFunctions; 113 114 delete[] mReduceNewDescriptions; 115 116 delete[] mForEachSignatures; 117 delete[] mForEachFunctions; 118 119 delete[] mInvokeFunctions; 120 121 for (size_t i = 0; i < mExportedVarCount; i++) { 122 delete[] mFieldName[i]; 123 } 124 delete[] mFieldName; 125 delete[] mFieldIsObject; 126 delete[] mFieldAddress; 127 } 128 129 // Create an ScriptExecutable object from a shared object. 130 // If expectedChecksum is not zero, it will be compared to the checksum 131 // embedded in the shared object. A mismatch will cause a failure. 132 // If succeeded, returns the new object. Otherwise, returns nullptr. 133 static ScriptExecutable* 134 createFromSharedObject(Context* RSContext, void* sharedObj, 135 uint32_t expectedChecksum = 0); 136 137 size_t getExportedVariableCount() const { return mExportedVarCount; } 138 size_t getExportedFunctionCount() const { return mFuncCount; } 139 size_t getExportedForEachCount() const { return mForEachCount; } 140 size_t getExportedReduceCount() const { return mReduceCount; } 141 size_t getExportedReduceNewCount() const { return mReduceNewCount; } 142 size_t getPragmaCount() const { return mPragmaCount; } 143 144 void* getFieldAddress(int slot) const { return mFieldAddress[slot]; } 145 void* getFieldAddress(const char* name) const; 146 bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; } 147 const char* getFieldName(int slot) const { return mFieldName[slot]; } 148 149 InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; } 150 151 ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; } 152 uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; } 153 154 ReduceFunc_t getReduceFunction(int slot) const { return mReduceFunctions[slot]; } 155 156 const ReduceNewDescription* getReduceNewDescription(int slot) const { 157 return &mReduceNewDescriptions[slot]; 158 } 159 160 const char ** getPragmaKeys() const { return mPragmaKeys; } 161 const char ** getPragmaValues() const { return mPragmaValues; } 162 163 const char* getGlobalName(int i) const { 164 if (i < mGlobalEntries) { 165 return mGlobalNames[i]; 166 } else { 167 return nullptr; 168 } 169 } 170 const void* getGlobalAddress(int i) const { 171 if (i < mGlobalEntries) { 172 return mGlobalAddresses[i]; 173 } else { 174 return nullptr; 175 } 176 } 177 size_t getGlobalSize(int i) const { 178 if (i < mGlobalEntries) { 179 return mGlobalSizes[i]; 180 } else { 181 return 0; 182 } 183 } 184 uint32_t getGlobalProperties(int i) const { 185 if (i < mGlobalEntries) { 186 return mGlobalProperties[i]; 187 } else { 188 return 0; 189 } 190 } 191 int getGlobalEntries() const { return mGlobalEntries; } 192 193 bool getThreadable() const { return mIsThreadable; } 194 195 uint32_t getBuildChecksum() const { return mBuildChecksum; } 196 197 bool dumpGlobalInfo() const; 198 199private: 200 void** mFieldAddress; 201 bool* mFieldIsObject; 202 const char* const * mFieldName; 203 size_t mExportedVarCount; 204 205 InvokeFunc_t* mInvokeFunctions; 206 size_t mFuncCount; 207 208 ForEachFunc_t* mForEachFunctions; 209 uint32_t* mForEachSignatures; 210 size_t mForEachCount; 211 212 ReduceFunc_t* mReduceFunctions; 213 size_t mReduceCount; 214 215 ReduceNewDescription* mReduceNewDescriptions; 216 size_t mReduceNewCount; 217 218 const char ** mPragmaKeys; 219 const char ** mPragmaValues; 220 size_t mPragmaCount; 221 222 const char ** mGlobalNames; 223 const void ** mGlobalAddresses; 224 const size_t * mGlobalSizes; 225 const uint32_t * mGlobalProperties; 226 int mGlobalEntries; 227 228 bool mIsThreadable; 229 uint32_t mBuildChecksum; 230 231 Context* mRS; 232}; 233 234} // namespace renderscript 235} // namespace android 236 237#endif // ANDROID_RENDERSCRIPT_EXECUTABLE_H 238