Home | History | Annotate | Download | only in cpu_ref
      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 
     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;
    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 #ifdef __LP64__
    159 #define SYSLIBPATH "/system/lib64"
    160 #define SYSLIBPATH_VNDK "/system/lib64/vndk-sp"
    161 #define SYSLIBPATH_BC "/system/lib64"
    162 #define SYSLIBPATH_VENDOR "/system/vendor/lib64"
    163 #elif defined(BUILD_ARM_FOR_X86) && defined(__arm__)
    164 #define SYSLIBPATH "/system/lib/arm"
    165 #define SYSLIBPATH_VNDK "/system/lib/arm/vndk-sp"
    166 #define SYSLIBPATH_BC "/system/lib"
    167 #define SYSLIBPATH_VENDOR "/system/vendor/lib/arm"
    168 #else
    169 #define SYSLIBPATH "/system/lib"
    170 #define SYSLIBPATH_VNDK "/system/lib/vndk-sp"
    171 #define SYSLIBPATH_BC "/system/lib"
    172 #define SYSLIBPATH_VENDOR "/system/vendor/lib"
    173 #endif
    174 
    175 } // namespace android
    176 
    177 namespace {
    178 
    179 inline bool is_force_recompile() {
    180   char buf[PROP_VALUE_MAX];
    181 
    182   // Re-compile if floating point precision has been overridden.
    183   android::renderscript::property_get("debug.rs.precision", buf, "");
    184   if (buf[0] != '\0') {
    185     return true;
    186   }
    187 
    188   // Re-compile if debug.rs.forcerecompile is set.
    189   android::renderscript::property_get("debug.rs.forcerecompile", buf, "0");
    190   if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
    191     return true;
    192   } else {
    193     return false;
    194   }
    195 }
    196 
    197 inline std::string getVndkSysLibPath() {
    198   char buf[PROP_VALUE_MAX];
    199   android::renderscript::property_get("ro.vndk.version", buf, "");
    200   std::string versionStr = buf;
    201   if (versionStr != "" && versionStr != "current") {
    202     return SYSLIBPATH_VNDK "-" + versionStr;
    203   }
    204   return SYSLIBPATH_VNDK;
    205 }
    206 
    207 }  // anonymous namespace
    208 
    209 #endif  // RSD_CPU_SCRIPT_H
    210