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_CPU_SCRIPT_H
18 #define RSD_CPU_SCRIPT_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 namespace bcc {
30     class BCCContext;
31     class RSCompilerDriver;
32 }
33 
34 namespace bcinfo {
35     class MetadataExtractor;
36 }
37 
38 namespace android {
39 namespace renderscript {
40 
41 class ScriptExecutable;
42 
43 class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript {
44 public:
45     typedef void (*outer_foreach_t)(
46         const RsExpandKernelDriverInfo *,
47         uint32_t x1, uint32_t x2,
48         uint32_t outstep);
49 
50     typedef void (* InvokeFunc_t)(void);
51     typedef void (* ForEachFunc_t)(void);
52     typedef int (* RootFunc_t)(void);
53 #ifdef RS_COMPATIBILITY_LIB
54     typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
55 #endif
56 
57     bool init(char const *resName, char const *cacheDir,
58               uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags,
59               char const *bccPluginName = nullptr);
60     void populateScript(Script *) override;
61 
62     void invokeFunction(uint32_t slot, const void *params, size_t paramLength) override;
63     int invokeRoot() override;
64     virtual void preLaunch(uint32_t slot, const Allocation ** ains,
65                            uint32_t inLen, Allocation * aout, const void * usr,
66                            uint32_t usrLen, const RsScriptCall *sc);
67     virtual void postLaunch(uint32_t slot, const Allocation ** ains,
68                             uint32_t inLen, Allocation * aout,
69                             const void * usr, uint32_t usrLen,
70                             const RsScriptCall *sc);
71 
72     void invokeForEach(uint32_t slot,
73                        const Allocation ** ains,
74                        uint32_t inLen,
75                        Allocation* aout,
76                        const void* usr,
77                        uint32_t usrLen,
78                        const RsScriptCall* sc) override;
79 
80     void invokeInit() override;
81     void invokeFreeChildren() override;
82 
83     void setGlobalVar(uint32_t slot, const void *data, size_t dataLength) override;
84     void getGlobalVar(uint32_t slot, void *data, size_t dataLength) override;
85     void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
86                                   const Element *e, const uint32_t *dims,
87                                   size_t dimLength) override;
88     void setGlobalBind(uint32_t slot, Allocation *data) override;
89     void setGlobalObj(uint32_t slot, ObjectBase *data) override;
90 
91     const char* getFieldName(uint32_t slot) const;
92 
93     ~RsdCpuScriptImpl() override;
94     RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s);
95 
getScript()96     const Script * getScript() {return mScript;}
97 
98     bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen,
99                           Allocation * aout, const void * usr, uint32_t usrLen,
100                           const RsScriptCall *sc, MTLaunchStruct *mtls);
101 
102     virtual void forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls);
103 
104 
105     const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym);
106     static void * lookupRuntimeStub(void* pContext, char const* name);
107 
108     Allocation * getAllocationForPointer(const void *ptr) const override;
109     bool storeRSInfoFromSO();
110 
111     int getGlobalEntries() const override;
112     const char * getGlobalName(int i) const override;
113     const void * getGlobalAddress(int i) const override;
114     size_t getGlobalSize(int i) const override;
115     uint32_t getGlobalProperties(int i) const override;
116 
117 protected:
118     RsdCpuReferenceImpl *mCtx;
119     const Script *mScript;
120     void *mScriptSO;
121 
122 #ifndef RS_COMPATIBILITY_LIB
123     // Returns the path to the core library we'll use.
124     const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode,
125                             size_t bitcodeSize);
126 
127     bcc::RSCompilerDriver *mCompilerDriver;
128 #endif
129 
130     RootFunc_t mRoot;
131     RootFunc_t mRootExpand;
132     InvokeFunc_t mInit;
133     InvokeFunc_t mFreeChildren;
134     ScriptExecutable* mScriptExec;
135 
136     Allocation **mBoundAllocs;
137     void * mIntrinsicData;
138     bool mIsThreadable;
139 
140 public:
141     static const char* BCC_EXE_PATH;
getBitcodeFilePath()142     const char* getBitcodeFilePath() const { return mBitcodeFilePath.string(); }
143 
144 private:
145     String8 mBitcodeFilePath;
146     uint32_t mBuildChecksum;
147     bool mChecksumNeeded;
148 };
149 
150 Allocation * rsdScriptGetAllocationForPointer(
151                         const Context *dc,
152                         const Script *script,
153                         const void *);
154 
155 uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
156                                 const char *commandLine,
157                                 const char ** bccFiles, size_t numFiles);
158 
159 }
160 
161 #ifdef __LP64__
162 #define SYSLIBPATH "/system/lib64"
163 #define SYSLIBPATH_VENDOR "/system/vendor/lib64"
164 #else
165 #define SYSLIBPATH "/system/lib"
166 #define SYSLIBPATH_VENDOR "/system/vendor/lib"
167 #endif
168 
169 }
170 
171 #endif  // RSD_CPU_SCRIPT_H
172