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