blob: 9ba65c78a582d17acbbc98e4e7a052ec9cd38563 [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
Stephen Hines6a236ad2015-06-22 14:51:30 -070017#ifndef RSD_CPU_SCRIPT_H
18#define RSD_CPU_SCRIPT_H
Jason Sams709a0972012-11-15 18:18:04 -080019
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
Miao Wang82e135c2017-02-27 23:35:35 -080029#include <string>
30
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -070031namespace bcinfo {
32 class MetadataExtractor;
Rahul Chaudhry7974fc02017-02-09 12:33:28 -080033} // namespace bcinfo
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -070034
Jason Sams709a0972012-11-15 18:18:04 -080035namespace android {
36namespace renderscript {
37
Yang Ni2abfcc62015-02-17 16:05:19 -080038class ScriptExecutable;
Jason Sams709a0972012-11-15 18:18:04 -080039
40class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript {
41public:
Jason Sams709a0972012-11-15 18:18:04 -080042 bool init(char const *resName, char const *cacheDir,
Stephen Hines00511322014-01-31 11:20:23 -080043 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags,
Chris Wailes44bef6f2014-08-12 13:51:10 -070044 char const *bccPluginName = nullptr);
Stephen Hinesc060f142015-05-13 19:26:09 -070045 void populateScript(Script *) override;
Jason Sams709a0972012-11-15 18:18:04 -080046
Stephen Hinesc060f142015-05-13 19:26:09 -070047 void invokeFunction(uint32_t slot, const void *params, size_t paramLength) override;
48 int invokeRoot() override;
Chris Wailesf3712132014-07-16 15:18:30 -070049 virtual void preLaunch(uint32_t slot, const Allocation ** ains,
50 uint32_t inLen, Allocation * aout, const void * usr,
Jason Sams17e3cdc2013-09-09 17:32:16 -070051 uint32_t usrLen, const RsScriptCall *sc);
Chris Wailesf3712132014-07-16 15:18:30 -070052 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);
Chris Wailes818cfa02014-07-16 15:18:30 -070056
Stephen Hinesc060f142015-05-13 19:26:09 -070057 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;
Chris Wailesf3712132014-07-16 15:18:30 -070064
Matt Wala14ce0072015-07-30 17:30:25 -070065 void invokeReduce(uint32_t slot,
David Grossae2ec3f2016-06-01 14:45:47 -070066 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -070067 Allocation* aout,
68 const RsScriptCall* sc) override;
69
Stephen Hinesc060f142015-05-13 19:26:09 -070070 void invokeInit() override;
71 void invokeFreeChildren() override;
Jason Sams709a0972012-11-15 18:18:04 -080072
Stephen Hinesc060f142015-05-13 19:26:09 -070073 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;
Jason Sams709a0972012-11-15 18:18:04 -080080
Yang Ni062c2872015-02-20 15:20:00 -080081 const char* getFieldName(uint32_t slot) const;
Jason Sams709a0972012-11-15 18:18:04 -080082
Stephen Hinesc060f142015-05-13 19:26:09 -070083 ~RsdCpuScriptImpl() override;
Jason Sams709a0972012-11-15 18:18:04 -080084 RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s);
85
86 const Script * getScript() {return mScript;}
87
Jason Samsbf2111d2015-01-26 18:13:41 -080088 bool forEachMtlsSetup(const Allocation ** ains, uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -070089 Allocation * aout, const void * usr, uint32_t usrLen,
Matt Wala14ce0072015-07-30 17:30:25 -070090 const RsScriptCall *sc, MTLaunchStructForEach *mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -070091
Matt Wala14ce0072015-07-30 17:30:25 -070092 virtual void forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls);
Jason Sams709a0972012-11-15 18:18:04 -080093
David Gross6c1876b2016-01-15 11:52:14 -080094 // Build an MTLaunchStruct suitable for launching a general reduce-style kernel.
David Grossae2ec3f2016-06-01 14:45:47 -070095 bool reduceMtlsSetup(const Allocation ** ains, uint32_t inLen, const Allocation *aout,
96 const RsScriptCall *sc, MTLaunchStructReduce *mtls);
David Gross6c1876b2016-01-15 11:52:14 -080097 // Finalize an MTLaunchStruct for launching a general reduce-style kernel.
David Grossae2ec3f2016-06-01 14:45:47 -070098 virtual void reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls);
David Gross6c1876b2016-01-15 11:52:14 -080099
Jason Sams709a0972012-11-15 18:18:04 -0800100 const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym);
101 static void * lookupRuntimeStub(void* pContext, char const* name);
102
Stephen Hinesc060f142015-05-13 19:26:09 -0700103 Allocation * getAllocationForPointer(const void *ptr) const override;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800104 bool storeRSInfoFromSO();
Jason Sams709a0972012-11-15 18:18:04 -0800105
Stephen Hines8409d642015-04-28 18:49:56 -0700106 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;
Stephen Hines5aa018c2015-05-20 18:09:57 -0700110 uint32_t getGlobalProperties(int i) const override;
Stephen Hines8409d642015-04-28 18:49:56 -0700111
Jason Sams709a0972012-11-15 18:18:04 -0800112protected:
113 RsdCpuReferenceImpl *mCtx;
114 const Script *mScript;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800115 void *mScriptSO;
Jason Sams709a0972012-11-15 18:18:04 -0800116
Jason Sams110f1812013-03-14 16:02:18 -0700117#ifndef RS_COMPATIBILITY_LIB
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700118 // Returns the path to the core library we'll use.
119 const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode,
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700120 size_t bitcodeSize);
Stephen Hines45e753a2015-01-19 20:58:44 -0800121#endif
122
Jason Sams110f1812013-03-14 16:02:18 -0700123 RootFunc_t mRoot;
124 RootFunc_t mRootExpand;
Matt Wala14ce0072015-07-30 17:30:25 -0700125 InitOrDtorFunc_t mInit;
126 InitOrDtorFunc_t mFreeChildren;
Yang Nid9bae682015-01-20 15:31:15 -0800127 ScriptExecutable* mScriptExec;
Jason Sams709a0972012-11-15 18:18:04 -0800128
129 Allocation **mBoundAllocs;
130 void * mIntrinsicData;
131 bool mIsThreadable;
Yang Nida0f0692015-01-12 13:03:40 -0800132
Yang Ni2abfcc62015-02-17 16:05:19 -0800133public:
134 static const char* BCC_EXE_PATH;
Miao Wang82e135c2017-02-27 23:35:35 -0800135 const char* getBitcodeFilePath() const { return mBitcodeFilePath.c_str(); }
Yang Nida0f0692015-01-12 13:03:40 -0800136
Yang Ni2abfcc62015-02-17 16:05:19 -0800137private:
Matt Wala14ce0072015-07-30 17:30:25 -0700138 bool setUpMtlsDimensions(MTLaunchStructCommon *mtls,
139 const RsLaunchDimensions &baseDim,
140 const RsScriptCall *sc);
141
Miao Wang82e135c2017-02-27 23:35:35 -0800142 std::string mBitcodeFilePath;
Yang Nicb170152015-04-16 10:27:02 -0700143 uint32_t mBuildChecksum;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800144 bool mChecksumNeeded;
Jason Sams709a0972012-11-15 18:18:04 -0800145};
146
Jason Sams709a0972012-11-15 18:18:04 -0800147Allocation * rsdScriptGetAllocationForPointer(
148 const Context *dc,
149 const Script *script,
150 const void *);
151
Yang Nicb170152015-04-16 10:27:02 -0700152uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
153 const char *commandLine,
154 const char ** bccFiles, size_t numFiles);
Yang Nif02a2b02015-04-07 16:00:31 -0700155
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800156} // namespace renderscript
Yang Nid9bae682015-01-20 15:31:15 -0800157
Victor Khimenkobd7a52b2020-09-11 23:15:44 +0200158#ifndef __ANDROID_NATIVE_BRIDGE__
159
Yang Nida0f0692015-01-12 13:03:40 -0800160#ifdef __LP64__
161#define SYSLIBPATH "/system/lib64"
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200162#define SYSLIBPATH_BC "/system/lib64"
Pirama Arumuga Nainar682672e2015-05-07 15:52:48 -0700163#define SYSLIBPATH_VENDOR "/system/vendor/lib64"
Yang Nida0f0692015-01-12 13:03:40 -0800164#else
165#define SYSLIBPATH "/system/lib"
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200166#define SYSLIBPATH_BC "/system/lib"
Pirama Arumuga Nainar682672e2015-05-07 15:52:48 -0700167#define SYSLIBPATH_VENDOR "/system/vendor/lib"
Yang Nida0f0692015-01-12 13:03:40 -0800168#endif
169
Victor Khimenkobd7a52b2020-09-11 23:15:44 +0200170#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
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800186} // namespace android
Jason Sams709a0972012-11-15 18:18:04 -0800187
Yang Nia845c352017-05-01 15:53:23 -0700188namespace {
189
190inline 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
Justin Yune0f1e2e2017-12-05 18:42:57 +0900208inline std::string getVndkSysLibPath() {
209 char buf[PROP_VALUE_MAX];
210 android::renderscript::property_get("ro.vndk.version", buf, "");
Jooyung Han5b809072020-03-25 21:46:54 +0900211 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;
Justin Yune0f1e2e2017-12-05 18:42:57 +0900216}
217
Yang Nia845c352017-05-01 15:53:23 -0700218} // anonymous namespace
219
Stephen Hines6a236ad2015-06-22 14:51:30 -0700220#endif // RSD_CPU_SCRIPT_H