rsCpuScript.h revision fe4f02f81d7c461835be97eebbde744885846e99
1/* 2 * Copyright (C) 2011-2012 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 RSD_BCC_H 18#define RSD_BCC_H 19 20#include <rs_hal.h> 21#include <rsRuntime.h> 22 23#ifndef RS_COMPATIBILITY_LIB 24#include <utility> 25#endif 26 27#include "rsCpuCore.h" 28 29#include <vector> 30 31namespace bcc { 32 class BCCContext; 33 class RSCompilerDriver; 34} 35 36namespace bcinfo { 37 class MetadataExtractor; 38} 39 40namespace android { 41namespace renderscript { 42 43class SharedLibraryUtils { 44 public: 45#ifndef RS_COMPATIBILITY_LIB 46 static bool createSharedLibrary(const char* cacheDir, const char* resName); 47#endif 48 49 // Load the shared library referred to by cacheDir and resName. If we have 50 // already loaded this library, we instead create a new copy (in the 51 // cache dir) and then load that. We then immediately destroy the copy. 52 // This is required behavior to implement script instancing for the support 53 // library, since shared objects are loaded and de-duped by name only. 54 55 // For 64bit RS Support Lib, the shared lib path cannot be constructed from 56 // cacheDir, so nativeLibDir is needed to load shared libs. 57 static void* loadSharedLibrary(const char *cacheDir, const char *resName, 58 const char *nativeLibDir = nullptr); 59 60 private: 61 // Attempt to load the shared library from origName, but then fall back to 62 // creating a copy of the shared library if necessary (to ensure instancing). 63 // This function returns the dlopen()-ed handle if successful. 64 static void *loadSOHelper(const char *origName, const char *cacheDir, 65 const char *resName); 66 67 static const char* LD_EXE_PATH; 68 static const char* RS_CACHE_DIR; 69}; 70 71class ScriptExecutable { 72 public: 73 ScriptExecutable(Context* RSContext, 74 void** fieldAddress, bool* fieldIsObject, size_t varCount, 75 InvokeFunc_t* invokeFunctions, size_t funcCount, 76 ForEachFunc_t* forEachFunctions, uint32_t* forEachSignatures, 77 size_t forEachCount, 78 const char ** pragmaKeys, const char ** pragmaValues, 79 size_t pragmaCount, 80 bool isThreadable) : 81 mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject), 82 mExportedVarCount(varCount), 83 mInvokeFunctions(invokeFunctions), mFuncCount(funcCount), 84 mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures), 85 mForEachCount(forEachCount), 86 mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues), 87 mPragmaCount(pragmaCount), 88 mIsThreadable(isThreadable), mRS(RSContext) { 89 } 90 91 ~ScriptExecutable() { 92 for (size_t i = 0; i < mExportedVarCount; ++i) { 93 if (mFieldIsObject[i]) { 94 if (mFieldAddress[i] != nullptr) { 95 rs_object_base *obj_addr = 96 reinterpret_cast<rs_object_base *>(mFieldAddress[i]); 97 rsrClearObject(mRS, obj_addr); 98 } 99 } 100 } 101 102 for (size_t i = 0; i < mPragmaCount; ++i) { 103 delete [] mPragmaKeys[i]; 104 delete [] mPragmaValues[i]; 105 } 106 107 delete[] mPragmaValues; 108 delete[] mPragmaKeys; 109 delete[] mForEachSignatures; 110 delete[] mForEachFunctions; 111 delete[] mInvokeFunctions; 112 delete[] mFieldIsObject; 113 delete[] mFieldAddress; 114 } 115 116 static ScriptExecutable* 117 createFromSharedObject(Context* RSContext, void* sharedObj); 118 119 size_t getExportedVariableCount() const { return mExportedVarCount; } 120 size_t getExportedFunctionCount() const { return mFuncCount; } 121 size_t getExportedForEachCount() const { return mForEachCount; } 122 size_t getPragmaCount() const { return mPragmaCount; } 123 124 void* getFieldAddress(int slot) const { return mFieldAddress[slot]; } 125 bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; } 126 InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; } 127 ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; } 128 uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; } 129 130 const char ** getPragmaKeys() const { return mPragmaKeys; } 131 const char ** getPragmaValues() const { return mPragmaValues; } 132 133 bool getThreadable() const { return mIsThreadable; } 134 135 private: 136 void** mFieldAddress; 137 bool* mFieldIsObject; 138 size_t mExportedVarCount; 139 140 InvokeFunc_t* mInvokeFunctions; 141 size_t mFuncCount; 142 143 ForEachFunc_t* mForEachFunctions; 144 uint32_t* mForEachSignatures; 145 size_t mForEachCount; 146 147 const char ** mPragmaKeys; 148 const char ** mPragmaValues; 149 size_t mPragmaCount; 150 151 bool mIsThreadable; 152 153 Context* mRS; 154}; 155 156class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript { 157public: 158 typedef void (*outer_foreach_t)( 159 const RsExpandKernelParams *, 160 uint32_t x1, uint32_t x2, 161 uint32_t outstep); 162 163 typedef void (* InvokeFunc_t)(void); 164 typedef void (* ForEachFunc_t)(void); 165 typedef int (* RootFunc_t)(void); 166#ifdef RS_COMPATIBILITY_LIB 167 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); 168#endif 169 170 bool init(char const *resName, char const *cacheDir, 171 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags, 172 char const *bccPluginName = nullptr); 173 virtual void populateScript(Script *); 174 175 virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength); 176 virtual int invokeRoot(); 177 virtual void preLaunch(uint32_t slot, const Allocation ** ains, 178 uint32_t inLen, Allocation * aout, const void * usr, 179 uint32_t usrLen, const RsScriptCall *sc); 180 virtual void postLaunch(uint32_t slot, const Allocation ** ains, 181 uint32_t inLen, Allocation * aout, 182 const void * usr, uint32_t usrLen, 183 const RsScriptCall *sc); 184 185 virtual void invokeForEach(uint32_t slot, 186 const Allocation ** ains, 187 uint32_t inLen, 188 Allocation* aout, 189 const void* usr, 190 uint32_t usrLen, 191 const RsScriptCall* sc); 192 193 virtual void invokeInit(); 194 virtual void invokeFreeChildren(); 195 196 virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength); 197 virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength); 198 virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, 199 const Element *e, const uint32_t *dims, size_t dimLength); 200 virtual void setGlobalBind(uint32_t slot, Allocation *data); 201 virtual void setGlobalObj(uint32_t slot, ObjectBase *data); 202 203 204 virtual ~RsdCpuScriptImpl(); 205 RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s); 206 207 const Script * getScript() {return mScript;} 208 209 bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen, 210 Allocation * aout, const void * usr, uint32_t usrLen, 211 const RsScriptCall *sc, MTLaunchStruct *mtls); 212 213 virtual void forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls); 214 215 216 const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym); 217 static void * lookupRuntimeStub(void* pContext, char const* name); 218 219 virtual Allocation * getAllocationForPointer(const void *ptr) const; 220 bool storeRSInfoFromSO(); 221 222protected: 223 RsdCpuReferenceImpl *mCtx; 224 const Script *mScript; 225 void *mScriptSO; 226 227#ifndef RS_COMPATIBILITY_LIB 228 // Returns the path to the core library we'll use. 229 const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode, 230 size_t bitcodeSize); 231 232 bcc::RSCompilerDriver *mCompilerDriver; 233#endif 234 235 RootFunc_t mRoot; 236 RootFunc_t mRootExpand; 237 InvokeFunc_t mInit; 238 InvokeFunc_t mFreeChildren; 239 ScriptExecutable* mScriptExec; 240 241 Allocation **mBoundAllocs; 242 void * mIntrinsicData; 243 bool mIsThreadable; 244 245 public: 246 static const char* BCC_EXE_PATH; 247 const std::string& getBitcodeFilePath() const { return mBitcodeFilePath; } 248 249 private: 250 std::string mBitcodeFilePath; 251}; 252 253Allocation * rsdScriptGetAllocationForPointer( 254 const Context *dc, 255 const Script *script, 256 const void *); 257 258} 259 260#ifdef __LP64__ 261#define SYSLIBPATH "/system/lib64" 262#else 263#define SYSLIBPATH "/system/lib" 264#endif 265 266} 267 268#endif 269