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 #include <string>
30
31 namespace bcinfo {
32 class MetadataExtractor;
33 } // namespace bcinfo
34
35 namespace android {
36 namespace renderscript {
37
38 class ScriptExecutable;
39
40 class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript {
41 public:
42 bool init(char const *resName, char const *cacheDir,
43 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags,
44 char const *bccPluginName = nullptr);
45 void populateScript(Script *) override;
46
47 void invokeFunction(uint32_t slot, const void *params, size_t paramLength) override;
48 int invokeRoot() override;
49 virtual void preLaunch(uint32_t slot, const Allocation ** ains,
50 uint32_t inLen, Allocation * aout, const void * usr,
51 uint32_t usrLen, const RsScriptCall *sc);
52 virtual void postLaunch(uint32_t slot, const Allocation ** ains,
53 uint32_t inLen, Allocation * aout,
54 const void * usr, uint32_t usrLen,
55 const RsScriptCall *sc);
56
57 void invokeForEach(uint32_t slot,
58 const Allocation ** ains,
59 uint32_t inLen,
60 Allocation* aout,
61 const void* usr,
62 uint32_t usrLen,
63 const RsScriptCall* sc) override;
64
65 void invokeReduce(uint32_t slot,
66 const Allocation ** ains, uint32_t inLen,
67 Allocation* aout,
68 const RsScriptCall* sc) override;
69
70 void invokeInit() override;
71 void invokeFreeChildren() override;
72
73 void setGlobalVar(uint32_t slot, const void *data, size_t dataLength) override;
74 void getGlobalVar(uint32_t slot, void *data, size_t dataLength) override;
75 void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
76 const Element *e, const uint32_t *dims,
77 size_t dimLength) override;
78 void setGlobalBind(uint32_t slot, Allocation *data) override;
79 void setGlobalObj(uint32_t slot, ObjectBase *data) override;
80
81 const char* getFieldName(uint32_t slot) const;
82
83 ~RsdCpuScriptImpl() override;
84 RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s);
85
getScript()86 const Script * getScript() {return mScript;}
87
88 bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen,
89 Allocation * aout, const void * usr, uint32_t usrLen,
90 const RsScriptCall *sc, MTLaunchStructForEach *mtls);
91
92 virtual void forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls);
93
94 // Build an MTLaunchStruct suitable for launching a general reduce-style kernel.
95 bool reduceMtlsSetup(const Allocation ** ains, uint32_t inLen, const Allocation *aout,
96 const RsScriptCall *sc, MTLaunchStructReduce *mtls);
97 // Finalize an MTLaunchStruct for launching a general reduce-style kernel.
98 virtual void reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls);
99
100 const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym);
101 static void * lookupRuntimeStub(void* pContext, char const* name);
102
103 Allocation * getAllocationForPointer(const void *ptr) const override;
104 bool storeRSInfoFromSO();
105
106 int getGlobalEntries() const override;
107 const char * getGlobalName(int i) const override;
108 const void * getGlobalAddress(int i) const override;
109 size_t getGlobalSize(int i) const override;
110 uint32_t getGlobalProperties(int i) const override;
111
112 protected:
113 RsdCpuReferenceImpl *mCtx;
114 const Script *mScript;
115 void *mScriptSO;
116
117 #ifndef RS_COMPATIBILITY_LIB
118 // Returns the path to the core library we'll use.
119 const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode,
120 size_t bitcodeSize);
121 #endif
122
123 RootFunc_t mRoot;
124 RootFunc_t mRootExpand;
125 InitOrDtorFunc_t mInit;
126 InitOrDtorFunc_t mFreeChildren;
127 ScriptExecutable* mScriptExec;
128
129 Allocation **mBoundAllocs;
130 void * mIntrinsicData;
131 bool mIsThreadable;
132
133 public:
134 static const char* BCC_EXE_PATH;
getBitcodeFilePath()135 const char* getBitcodeFilePath() const { return mBitcodeFilePath.c_str(); }
136
137 private:
138 bool setUpMtlsDimensions(MTLaunchStructCommon *mtls,
139 const RsLaunchDimensions &baseDim,
140 const RsScriptCall *sc);
141
142 std::string mBitcodeFilePath;
143 uint32_t mBuildChecksum;
144 bool mChecksumNeeded;
145 };
146
147 Allocation * rsdScriptGetAllocationForPointer(
148 const Context *dc,
149 const Script *script,
150 const void *);
151
152 uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
153 const char *commandLine,
154 const char ** bccFiles, size_t numFiles);
155
156 } // namespace renderscript
157
158 #ifndef __ANDROID_NATIVE_BRIDGE__
159
160 #ifdef __LP64__
161 #define SYSLIBPATH "/system/lib64"
162 #define SYSLIBPATH_BC "/system/lib64"
163 #define SYSLIBPATH_VENDOR "/system/vendor/lib64"
164 #else
165 #define SYSLIBPATH "/system/lib"
166 #define SYSLIBPATH_BC "/system/lib"
167 #define SYSLIBPATH_VENDOR "/system/vendor/lib"
168 #endif
169
170 #else
171
172 #if defined(__arm__)
173 #define SYSLIBPATH "/system/lib/arm"
174 #define SYSLIBPATH_BC "/system/lib"
175 #define SYSLIBPATH_VENDOR "/system/vendor/lib/arm"
176 #elif defined(__aarch64__)
177 #define SYSLIBPATH "/system/lib64/arm64"
178 #define SYSLIBPATH_BC "/system/lib64"
179 #define SYSLIBPATH_VENDOR "/system/vendor/lib64/arm64"
180 #else
181 #error Unknown architecture
182 #endif
183
184 #endif
185
186 } // namespace android
187
188 namespace {
189
is_force_recompile()190 inline bool is_force_recompile() {
191 char buf[PROP_VALUE_MAX];
192
193 // Re-compile if floating point precision has been overridden.
194 android::renderscript::property_get("debug.rs.precision", buf, "");
195 if (buf[0] != '\0') {
196 return true;
197 }
198
199 // Re-compile if debug.rs.forcerecompile is set.
200 android::renderscript::property_get("debug.rs.forcerecompile", buf, "0");
201 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
202 return true;
203 } else {
204 return false;
205 }
206 }
207
getVndkSysLibPath()208 inline std::string getVndkSysLibPath() {
209 char buf[PROP_VALUE_MAX];
210 android::renderscript::property_get("ro.vndk.version", buf, "");
211 std::string vndk_path = "/apex/com.android.vndk.v" + std::string(buf) + "/lib";
212 #ifdef __LP64__
213 vndk_path += "64";
214 #endif
215 return vndk_path;
216 }
217
218 } // anonymous namespace
219
220 #endif // RSD_CPU_SCRIPT_H
221