blob: a775c73716074cdd1a7916e4555f88d2b66c25b8 [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
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
Stephen Hines25e3af52014-05-21 21:23:08 -070023#ifndef RS_COMPATIBILITY_LIB
Tim Murray29809d12014-05-28 12:04:19 -070024#include <utility>
Stephen Hines25e3af52014-05-21 21:23:08 -070025#endif
26
Jason Sams709a0972012-11-15 18:18:04 -080027#include "rsCpuCore.h"
28
29namespace bcc {
30 class BCCContext;
31 class RSCompilerDriver;
Jason Sams709a0972012-11-15 18:18:04 -080032}
33
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -070034namespace bcinfo {
35 class MetadataExtractor;
36}
37
Jason Sams709a0972012-11-15 18:18:04 -080038namespace android {
39namespace renderscript {
40
Yang Ni1c44cb62015-01-22 12:02:27 -080041class 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.
Miao Wangf3213d72015-01-14 10:03:07 -080052
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);
Yang Ni1c44cb62015-01-22 12:02:27 -080057
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
Yang Nid9bae682015-01-20 15:31:15 -080069class ScriptExecutable {
70 public:
71 ScriptExecutable(Context* RSContext,
Yang Nie8f9fba2015-01-30 08:55:10 -080072 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) {
Yang Nid9bae682015-01-20 15:31:15 -080087 }
Jason Sams709a0972012-11-15 18:18:04 -080088
Yang Nid9bae682015-01-20 15:31:15 -080089 ~ScriptExecutable() {
Yang Nie8f9fba2015-01-30 08:55:10 -080090 for (size_t i = 0; i < mExportedVarCount; ++i) {
Yang Nid9bae682015-01-20 15:31:15 -080091 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 }
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -080099
Yang Nie8f9fba2015-01-30 08:55:10 -0800100 for (size_t i = 0; i < mPragmaCount; ++i) {
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800101 delete [] mPragmaKeys[i];
102 delete [] mPragmaValues[i];
103 }
Yang Nie8f9fba2015-01-30 08:55:10 -0800104
105 delete[] mPragmaValues;
106 delete[] mPragmaKeys;
107 delete[] mForEachSignatures;
108 delete[] mForEachFunctions;
109 delete[] mInvokeFunctions;
110 delete[] mFieldIsObject;
111 delete[] mFieldAddress;
Yang Nid9bae682015-01-20 15:31:15 -0800112 }
113
114 static ScriptExecutable*
115 createFromSharedObject(Context* RSContext, void* sharedObj);
116
Yang Nie8f9fba2015-01-30 08:55:10 -0800117 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; }
Yang Nid9bae682015-01-20 15:31:15 -0800121
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
Yang Nie8f9fba2015-01-30 08:55:10 -0800128 const char ** getPragmaKeys() const { return mPragmaKeys; }
129 const char ** getPragmaValues() const { return mPragmaValues; }
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800130
131 bool getThreadable() const { return mIsThreadable; }
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800132
Yang Nid9bae682015-01-20 15:31:15 -0800133 private:
Yang Nie8f9fba2015-01-30 08:55:10 -0800134 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;
Yang Nid9bae682015-01-20 15:31:15 -0800148
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800149 bool mIsThreadable;
150
Yang Nid9bae682015-01-20 15:31:15 -0800151 Context* mRS;
152};
Jason Sams709a0972012-11-15 18:18:04 -0800153
154class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript {
155public:
156 typedef void (*outer_foreach_t)(
Chris Wailes80ef6932014-07-08 11:22:18 -0700157 const RsExpandKernelParams *,
Jason Sams709a0972012-11-15 18:18:04 -0800158 uint32_t x1, uint32_t x2,
Chris Wailes9ed79102014-07-25 15:53:28 -0700159 uint32_t outstep);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800160
Jason Sams110f1812013-03-14 16:02:18 -0700161 typedef void (* InvokeFunc_t)(void);
162 typedef void (* ForEachFunc_t)(void);
163 typedef int (* RootFunc_t)(void);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800164#ifdef RS_COMPATIBILITY_LIB
Jason Sams110f1812013-03-14 16:02:18 -0700165 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
166#endif
Jason Sams709a0972012-11-15 18:18:04 -0800167
168 bool init(char const *resName, char const *cacheDir,
Stephen Hines00511322014-01-31 11:20:23 -0800169 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags,
Chris Wailes44bef6f2014-08-12 13:51:10 -0700170 char const *bccPluginName = nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800171 virtual void populateScript(Script *);
172
173 virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength);
174 virtual int invokeRoot();
Chris Wailesf3712132014-07-16 15:18:30 -0700175 virtual void preLaunch(uint32_t slot, const Allocation ** ains,
176 uint32_t inLen, Allocation * aout, const void * usr,
Jason Sams17e3cdc2013-09-09 17:32:16 -0700177 uint32_t usrLen, const RsScriptCall *sc);
Chris Wailesf3712132014-07-16 15:18:30 -0700178 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);
Chris Wailes818cfa02014-07-16 15:18:30 -0700182
Chris Wailesf3712132014-07-16 15:18:30 -0700183 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
Jason Sams709a0972012-11-15 18:18:04 -0800191 virtual void invokeInit();
192 virtual void invokeFreeChildren();
193
194 virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700195 virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800196 virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700197 const Element *e, const uint32_t *dims, size_t dimLength);
Jason Sams709a0972012-11-15 18:18:04 -0800198 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
Jason Samsbf2111d2015-01-26 18:13:41 -0800207 bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700208 Allocation * aout, const void * usr, uint32_t usrLen,
209 const RsScriptCall *sc, MTLaunchStruct *mtls);
210
Jason Sams709a0972012-11-15 18:18:04 -0800211 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;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800218 bool storeRSInfoFromSO();
Jason Sams709a0972012-11-15 18:18:04 -0800219
Jason Sams709a0972012-11-15 18:18:04 -0800220protected:
221 RsdCpuReferenceImpl *mCtx;
222 const Script *mScript;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800223 void *mScriptSO;
Jason Sams709a0972012-11-15 18:18:04 -0800224
Jason Sams110f1812013-03-14 16:02:18 -0700225#ifndef RS_COMPATIBILITY_LIB
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700226 // Returns the path to the core library we'll use.
227 const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode,
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700228 size_t bitcodeSize);
Tim Murray29809d12014-05-28 12:04:19 -0700229
Jason Sams709a0972012-11-15 18:18:04 -0800230 bcc::RSCompilerDriver *mCompilerDriver;
Stephen Hines45e753a2015-01-19 20:58:44 -0800231#endif
232
Jason Sams110f1812013-03-14 16:02:18 -0700233 RootFunc_t mRoot;
234 RootFunc_t mRootExpand;
235 InvokeFunc_t mInit;
236 InvokeFunc_t mFreeChildren;
Yang Nid9bae682015-01-20 15:31:15 -0800237 ScriptExecutable* mScriptExec;
Jason Sams709a0972012-11-15 18:18:04 -0800238
239 Allocation **mBoundAllocs;
240 void * mIntrinsicData;
241 bool mIsThreadable;
Yang Nida0f0692015-01-12 13:03:40 -0800242
243 public:
244 static const char* BCC_EXE_PATH;
Yang Nic31585b2015-02-15 11:43:50 -0800245 const char* getBitcodeFilePath() const { return mBitcodeFilePath.string(); }
Yang Nida0f0692015-01-12 13:03:40 -0800246
247 private:
Yang Nic31585b2015-02-15 11:43:50 -0800248 String8 mBitcodeFilePath;
Jason Sams709a0972012-11-15 18:18:04 -0800249};
250
Jason Sams709a0972012-11-15 18:18:04 -0800251Allocation * rsdScriptGetAllocationForPointer(
252 const Context *dc,
253 const Script *script,
254 const void *);
255
Jason Sams709a0972012-11-15 18:18:04 -0800256}
Yang Nid9bae682015-01-20 15:31:15 -0800257
Yang Nida0f0692015-01-12 13:03:40 -0800258#ifdef __LP64__
259#define SYSLIBPATH "/system/lib64"
260#else
261#define SYSLIBPATH "/system/lib"
262#endif
263
Jason Sams709a0972012-11-15 18:18:04 -0800264}
265
266#endif