blob: 873a79a4e085ce22cefbbd70b62004042b64fe88 [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
Yang Nid9bae682015-01-20 15:31:15 -080029#include <vector>
30
Jason Sams709a0972012-11-15 18:18:04 -080031namespace bcc {
32 class BCCContext;
33 class RSCompilerDriver;
Jason Sams709a0972012-11-15 18:18:04 -080034}
35
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -070036namespace bcinfo {
37 class MetadataExtractor;
38}
39
Jason Sams709a0972012-11-15 18:18:04 -080040namespace android {
41namespace renderscript {
42
Yang Ni1c44cb62015-01-22 12:02:27 -080043class 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
Yang Nid9bae682015-01-20 15:31:15 -080067class 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,
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -080074 std::vector<uint32_t>& forEachSignatures,
75 std::vector<const char *> &pragmaKeys,
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -080076 std::vector<const char *> &pragmaValues,
77 bool isThreadable)
78 : mIsThreadable(isThreadable), mRS(RSContext) {
Yang Nid9bae682015-01-20 15:31:15 -080079 mFieldAddress.swap(fieldAddress);
80 mFieldIsObject.swap(fieldIsObject);
81 mInvokeFunctions.swap(invokeFunctions);
82 mForEachFunctions.swap(forEachFunctions);
83 mForEachSignatures.swap(forEachSignatures);
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -080084 mPragmaKeys.swap(pragmaKeys);
85 mPragmaValues.swap(pragmaValues);
Yang Nid9bae682015-01-20 15:31:15 -080086 }
Jason Sams709a0972012-11-15 18:18:04 -080087
Yang Nid9bae682015-01-20 15:31:15 -080088 ~ScriptExecutable() {
89 for (size_t i = 0; i < mFieldAddress.size(); ++i) {
90 if (mFieldIsObject[i]) {
91 if (mFieldAddress[i] != nullptr) {
92 rs_object_base *obj_addr =
93 reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
94 rsrClearObject(mRS, obj_addr);
95 }
96 }
97 }
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -080098
99 for (size_t i = 0; i < mPragmaKeys.size(); ++i) {
100 delete [] mPragmaKeys[i];
101 delete [] mPragmaValues[i];
102 }
Yang Nid9bae682015-01-20 15:31:15 -0800103 }
104
105 static ScriptExecutable*
106 createFromSharedObject(Context* RSContext, void* sharedObj);
107
108 size_t getExportedVariableCount() const { return mFieldAddress.size(); }
109 size_t getExportedFunctionCount() const { return mInvokeFunctions.size(); }
110 size_t getExportedForEachCount() const { return mForEachFunctions.size(); }
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800111 size_t getPragmaCount() const { return mPragmaKeys.size(); }
Yang Nid9bae682015-01-20 15:31:15 -0800112
113 void* getFieldAddress(int slot) const { return mFieldAddress[slot]; }
114 bool getFieldIsObject(int slot) const { return mFieldIsObject[slot]; }
115 InvokeFunc_t getInvokeFunction(int slot) const { return mInvokeFunctions[slot]; }
116 ForEachFunc_t getForEachFunction(int slot) const { return mForEachFunctions[slot]; }
117 uint32_t getForEachSignature(int slot) const { return mForEachSignatures[slot]; }
118
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800119 const std::vector<const char *> & getPragmaKeys() const { return mPragmaKeys; }
120 const std::vector<const char *> & getPragmaValues() const { return mPragmaValues; }
121
122 bool getThreadable() const { return mIsThreadable; }
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800123
Yang Nid9bae682015-01-20 15:31:15 -0800124 private:
125 std::vector<void*> mFieldAddress;
126 std::vector<bool> mFieldIsObject;
127 std::vector<InvokeFunc_t> mInvokeFunctions;
128 std::vector<ForEachFunc_t> mForEachFunctions;
129 std::vector<uint32_t> mForEachSignatures;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800130 std::vector<const char *> mPragmaKeys;
131 std::vector<const char *> mPragmaValues;
Yang Nid9bae682015-01-20 15:31:15 -0800132
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800133 bool mIsThreadable;
134
Yang Nid9bae682015-01-20 15:31:15 -0800135 Context* mRS;
136};
Jason Sams709a0972012-11-15 18:18:04 -0800137
138class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript {
139public:
140 typedef void (*outer_foreach_t)(
Chris Wailes80ef6932014-07-08 11:22:18 -0700141 const RsExpandKernelParams *,
Jason Sams709a0972012-11-15 18:18:04 -0800142 uint32_t x1, uint32_t x2,
Chris Wailes9ed79102014-07-25 15:53:28 -0700143 uint32_t outstep);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800144
Jason Sams110f1812013-03-14 16:02:18 -0700145 typedef void (* InvokeFunc_t)(void);
146 typedef void (* ForEachFunc_t)(void);
147 typedef int (* RootFunc_t)(void);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800148#ifdef RS_COMPATIBILITY_LIB
Jason Sams110f1812013-03-14 16:02:18 -0700149 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
150#endif
Jason Sams709a0972012-11-15 18:18:04 -0800151
152 bool init(char const *resName, char const *cacheDir,
Stephen Hines00511322014-01-31 11:20:23 -0800153 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags,
Chris Wailes44bef6f2014-08-12 13:51:10 -0700154 char const *bccPluginName = nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800155 virtual void populateScript(Script *);
156
157 virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength);
158 virtual int invokeRoot();
Chris Wailesf3712132014-07-16 15:18:30 -0700159 virtual void preLaunch(uint32_t slot, const Allocation ** ains,
160 uint32_t inLen, Allocation * aout, const void * usr,
Jason Sams17e3cdc2013-09-09 17:32:16 -0700161 uint32_t usrLen, const RsScriptCall *sc);
Chris Wailesf3712132014-07-16 15:18:30 -0700162 virtual void postLaunch(uint32_t slot, const Allocation ** ains,
163 uint32_t inLen, Allocation * aout,
164 const void * usr, uint32_t usrLen,
165 const RsScriptCall *sc);
Chris Wailes818cfa02014-07-16 15:18:30 -0700166
Chris Wailesf3712132014-07-16 15:18:30 -0700167 virtual void invokeForEach(uint32_t slot,
168 const Allocation ** ains,
169 uint32_t inLen,
170 Allocation* aout,
171 const void* usr,
172 uint32_t usrLen,
173 const RsScriptCall* sc);
174
Jason Sams709a0972012-11-15 18:18:04 -0800175 virtual void invokeInit();
176 virtual void invokeFreeChildren();
177
178 virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700179 virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800180 virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700181 const Element *e, const uint32_t *dims, size_t dimLength);
Jason Sams709a0972012-11-15 18:18:04 -0800182 virtual void setGlobalBind(uint32_t slot, Allocation *data);
183 virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
184
185
186 virtual ~RsdCpuScriptImpl();
187 RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s);
188
189 const Script * getScript() {return mScript;}
190
Jason Samsbf2111d2015-01-26 18:13:41 -0800191 bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700192 Allocation * aout, const void * usr, uint32_t usrLen,
193 const RsScriptCall *sc, MTLaunchStruct *mtls);
194
Jason Sams709a0972012-11-15 18:18:04 -0800195 virtual void forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls);
196
197
198 const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym);
199 static void * lookupRuntimeStub(void* pContext, char const* name);
200
201 virtual Allocation * getAllocationForPointer(const void *ptr) const;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800202 bool storeRSInfoFromSO();
Jason Sams709a0972012-11-15 18:18:04 -0800203
Jason Sams709a0972012-11-15 18:18:04 -0800204protected:
205 RsdCpuReferenceImpl *mCtx;
206 const Script *mScript;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800207 void *mScriptSO;
Jason Sams709a0972012-11-15 18:18:04 -0800208
Jason Sams110f1812013-03-14 16:02:18 -0700209#ifndef RS_COMPATIBILITY_LIB
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700210 // Returns the path to the core library we'll use.
211 const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode,
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700212 size_t bitcodeSize);
Tim Murray29809d12014-05-28 12:04:19 -0700213
Jason Sams709a0972012-11-15 18:18:04 -0800214 bcc::RSCompilerDriver *mCompilerDriver;
Stephen Hines45e753a2015-01-19 20:58:44 -0800215#endif
216
Jason Sams110f1812013-03-14 16:02:18 -0700217 RootFunc_t mRoot;
218 RootFunc_t mRootExpand;
219 InvokeFunc_t mInit;
220 InvokeFunc_t mFreeChildren;
Yang Nid9bae682015-01-20 15:31:15 -0800221 ScriptExecutable* mScriptExec;
Jason Sams709a0972012-11-15 18:18:04 -0800222
223 Allocation **mBoundAllocs;
224 void * mIntrinsicData;
225 bool mIsThreadable;
Jason Sams709a0972012-11-15 18:18:04 -0800226};
227
Jason Sams709a0972012-11-15 18:18:04 -0800228Allocation * rsdScriptGetAllocationForPointer(
229 const Context *dc,
230 const Script *script,
231 const void *);
232
233
234
235}
Yang Nid9bae682015-01-20 15:31:15 -0800236
Jason Sams709a0972012-11-15 18:18:04 -0800237}
238
239#endif