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