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