rsCpuExecutable.h revision cb17015fed6b11a5028f31cc804a3847e379945d
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* cacheDir, const char* resName); 33#endif 34 35 // Load the shared library referred to by cacheDir and resName. If we have 36 // already loaded this library, we instead create a new copy (in the 37 // cache dir) and then load that. We then immediately destroy the copy. 38 // This is required behavior to implement script instancing for the support 39 // library, since shared objects are loaded and de-duped by name only. 40 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 46private: 47 // Attempt to load the shared library from origName, but then fall back to 48 // creating a copy of the shared library if necessary (to ensure instancing). 49 // This function returns the dlopen()-ed handle if successful. 50 static void *loadSOHelper(const char *origName, const char *cacheDir, 51 const char *resName); 52 53 static const char* LD_EXE_PATH; 54 static const char* RS_CACHE_DIR; 55}; 56 57class ScriptExecutable { 58public: 59 ScriptExecutable(Context* RSContext, 60 void** fieldAddress, bool* fieldIsObject, 61 const char* const * fieldName, size_t varCount, 62 InvokeFunc_t* invokeFunctions, size_t funcCount, 63 ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures, 64 size_t forEachCount, 65 const char** pragmaKeys, const char** pragmaValues, 66 size_t pragmaCount, 67 bool isThreadable, uint32_t buildChecksum) : 68 mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject), 69 mFieldName(fieldName), mExportedVarCount(varCount), 70 mInvokeFunctions(invokeFunctions), mFuncCount(funcCount), 71 mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures), 72 mForEachCount(forEachCount), 73 mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues), 74 mPragmaCount(pragmaCount), mIsThreadable(isThreadable), 75 mBuildChecksum(buildChecksum), mRS(RSContext) { 76 } 77 78 ~ScriptExecutable() { 79 for (size_t i = 0; i < mExportedVarCount; ++i) { 80 if (mFieldIsObject[i]) { 81 if (mFieldAddress[i] != nullptr) { 82 rs_object_base *obj_addr = 83 reinterpret_cast<rs_object_base *>(mFieldAddress[i]); 84 rsrClearObject(mRS, obj_addr); 85 } 86 } 87 } 88 89 for (size_t i = 0; i < mPragmaCount; ++i) { 90 delete [] mPragmaKeys[i]; 91 delete [] mPragmaValues[i]; 92 } 93 delete[] mPragmaValues; 94 delete[] mPragmaKeys; 95 96 delete[] mForEachSignatures; 97 delete[] mForEachFunctions; 98 99 delete[] mInvokeFunctions; 100 101 for (size_t i = 0; i < mExportedVarCount; i++) { 102 delete[] mFieldName[i]; 103 } 104 delete[] mFieldName; 105 delete[] mFieldIsObject; 106 delete[] mFieldAddress; 107 } 108 109 // Create an ScriptExecutable object from a shared object. 110 // If expectedChecksum is not zero, it will be compared to the checksum 111 // embedded in the shared object. A mismatch will cause a failure. 112 // If succeeded, returns the new object. Otherwise, returns nullptr. 113 static ScriptExecutable* 114 createFromSharedObject(Context* RSContext, void* sharedObj, 115 uint32_t expectedChecksum = 0); 116 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; } 121 122 void* getFieldAddress(int slot) const { return mFieldAddress[slot]; } 123 void* getFieldAddress(const char* name) const; 124 bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; } 125 const char* getFieldName(int slot) const { return mFieldName[slot]; } 126 127 InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; } 128 129 ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; } 130 uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; } 131 132 const char ** getPragmaKeys() const { return mPragmaKeys; } 133 const char ** getPragmaValues() const { return mPragmaValues; } 134 135 bool getThreadable() const { return mIsThreadable; } 136 137 uint32_t getBuildChecksum() const { return mBuildChecksum; } 138 139private: 140 void** mFieldAddress; 141 bool* mFieldIsObject; 142 const char* const * mFieldName; 143 size_t mExportedVarCount; 144 145 InvokeFunc_t* mInvokeFunctions; 146 size_t mFuncCount; 147 148 ForEachFunc_t* mForEachFunctions; 149 uint32_t* mForEachSignatures; 150 size_t mForEachCount; 151 152 const char ** mPragmaKeys; 153 const char ** mPragmaValues; 154 size_t mPragmaCount; 155 156 bool mIsThreadable; 157 uint32_t mBuildChecksum; 158 159 Context* mRS; 160}; 161 162} // namespace renderscript 163} // namespace android 164 165#endif // ANDROID_RENDERSCRIPT_EXECUTABLE_H 166