rsCpuScript.h revision 577194ac9c2bf10f31e564de91371764b265929a
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 static void* loadSharedLibrary(const char* cacheDir, const char* resName); 55 56 private: 57 // Attempt to load the shared library from origName, but then fall back to 58 // creating a copy of the shared library if necessary (to ensure instancing). 59 // This function returns the dlopen()-ed handle if successful. 60 static void *loadSOHelper(const char *origName, const char *cacheDir, 61 const char *resName); 62 63 static const char* LD_EXE_PATH; 64 static const char* RS_CACHE_DIR; 65}; 66 67class ScriptExecutable { 68 public: 69 ScriptExecutable(Context* RSContext, 70 std::vector<void*>& fieldAddress, 71 std::vector<bool>& fieldIsObject, 72 std::vector<InvokeFunc_t>& invokeFunctions, 73 std::vector<ForEachFunc_t>& forEachFunctions, 74 std::vector<uint32_t>& forEachSignatures, 75 std::vector<const char *> &pragmaKeys, 76 std::vector<const char *> &pragmaValues) : mRS(RSContext) { 77 mFieldAddress.swap(fieldAddress); 78 mFieldIsObject.swap(fieldIsObject); 79 mInvokeFunctions.swap(invokeFunctions); 80 mForEachFunctions.swap(forEachFunctions); 81 mForEachSignatures.swap(forEachSignatures); 82 mPragmaKeys.swap(pragmaKeys); 83 mPragmaValues.swap(pragmaValues); 84 } 85 86 ~ScriptExecutable() { 87 for (size_t i = 0; i < mFieldAddress.size(); ++i) { 88 if (mFieldIsObject[i]) { 89 if (mFieldAddress[i] != nullptr) { 90 rs_object_base *obj_addr = 91 reinterpret_cast<rs_object_base *>(mFieldAddress[i]); 92 rsrClearObject(mRS, obj_addr); 93 } 94 } 95 } 96 97 for (size_t i = 0; i < mPragmaKeys.size(); ++i) { 98 delete [] mPragmaKeys[i]; 99 delete [] mPragmaValues[i]; 100 } 101 } 102 103 static ScriptExecutable* 104 createFromSharedObject(Context* RSContext, void* sharedObj); 105 106 size_t getExportedVariableCount() const { return mFieldAddress.size(); } 107 size_t getExportedFunctionCount() const { return mInvokeFunctions.size(); } 108 size_t getExportedForEachCount() const { return mForEachFunctions.size(); } 109 size_t getPragmaCount() const { return mPragmaKeys.size(); } 110 111 void* getFieldAddress(int slot) const { return mFieldAddress[slot]; } 112 bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; } 113 InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; } 114 ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; } 115 uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; } 116 117 std::vector<const char *> & getPragmaKeys() { return mPragmaKeys; } 118 std::vector<const char *> & getPragmaValues() { return mPragmaValues; } 119 120 private: 121 std::vector<void*> mFieldAddress; 122 std::vector<bool> mFieldIsObject; 123 std::vector<InvokeFunc_t> mInvokeFunctions; 124 std::vector<ForEachFunc_t> mForEachFunctions; 125 std::vector<uint32_t> mForEachSignatures; 126 std::vector<const char *> mPragmaKeys; 127 std::vector<const char *> mPragmaValues; 128 129 Context* mRS; 130}; 131 132class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript { 133public: 134 typedef void (*outer_foreach_t)( 135 const RsExpandKernelParams *, 136 uint32_t x1, uint32_t x2, 137 uint32_t outstep); 138 139 typedef void (* InvokeFunc_t)(void); 140 typedef void (* ForEachFunc_t)(void); 141 typedef int (* RootFunc_t)(void); 142#ifdef RS_COMPATIBILITY_LIB 143 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); 144#endif 145 146 bool init(char const *resName, char const *cacheDir, 147 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags, 148 char const *bccPluginName = nullptr); 149 virtual void populateScript(Script *); 150 151 virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength); 152 virtual int invokeRoot(); 153 virtual void preLaunch(uint32_t slot, const Allocation ** ains, 154 uint32_t inLen, Allocation * aout, const void * usr, 155 uint32_t usrLen, const RsScriptCall *sc); 156 virtual void postLaunch(uint32_t slot, const Allocation ** ains, 157 uint32_t inLen, Allocation * aout, 158 const void * usr, uint32_t usrLen, 159 const RsScriptCall *sc); 160 161 virtual void invokeForEach(uint32_t slot, 162 const Allocation ** ains, 163 uint32_t inLen, 164 Allocation* aout, 165 const void* usr, 166 uint32_t usrLen, 167 const RsScriptCall* sc); 168 169 virtual void invokeInit(); 170 virtual void invokeFreeChildren(); 171 172 virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength); 173 virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength); 174 virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, 175 const Element *e, const uint32_t *dims, size_t dimLength); 176 virtual void setGlobalBind(uint32_t slot, Allocation *data); 177 virtual void setGlobalObj(uint32_t slot, ObjectBase *data); 178 179 180 virtual ~RsdCpuScriptImpl(); 181 RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s); 182 183 const Script * getScript() {return mScript;} 184 185 void forEachMtlsSetup(const Allocation ** ains, uint32_t inLen, 186 Allocation * aout, const void * usr, uint32_t usrLen, 187 const RsScriptCall *sc, MTLaunchStruct *mtls); 188 189 virtual void forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls); 190 191 192 const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym); 193 static void * lookupRuntimeStub(void* pContext, char const* name); 194 195 virtual Allocation * getAllocationForPointer(const void *ptr) const; 196 bool storeRSInfoFromSO(); 197 198protected: 199 RsdCpuReferenceImpl *mCtx; 200 const Script *mScript; 201 void *mScriptSO; 202 203#ifndef RS_COMPATIBILITY_LIB 204 // Returns the path to the core library we'll use. 205 const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode, 206 size_t bitcodeSize); 207 208 bcc::RSCompilerDriver *mCompilerDriver; 209#endif 210 211 RootFunc_t mRoot; 212 RootFunc_t mRootExpand; 213 InvokeFunc_t mInit; 214 InvokeFunc_t mFreeChildren; 215 ScriptExecutable* mScriptExec; 216 217 Allocation **mBoundAllocs; 218 void * mIntrinsicData; 219 bool mIsThreadable; 220}; 221 222Allocation * rsdScriptGetAllocationForPointer( 223 const Context *dc, 224 const Script *script, 225 const void *); 226 227 228 229} 230 231} 232 233#endif 234