rsCpuScript.h revision 44bef6fba6244292b751387f3d6c31cca96c28ad
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_BCC_H
18#define RSD_BCC_H
19
20#include <rs_hal.h>
21#include <rsRuntime.h>
22
23#ifndef RS_COMPATIBILITY_LIB
24#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
25#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
26#include <vector>
27#include <utility>
28#endif
29
30#include "rsCpuCore.h"
31
32namespace bcc {
33    class BCCContext;
34    class RSCompilerDriver;
35    class RSExecutable;
36}
37
38namespace bcinfo {
39    class MetadataExtractor;
40}
41
42namespace android {
43namespace renderscript {
44
45
46
47class RsdCpuScriptImpl : public RsdCpuReferenceImpl::CpuScript {
48public:
49    typedef void (*outer_foreach_t)(
50        const RsExpandKernelParams *,
51        uint32_t x1, uint32_t x2,
52        uint32_t outstep);
53#ifdef RS_COMPATIBILITY_LIB
54    typedef void (* InvokeFunc_t)(void);
55    typedef void (* ForEachFunc_t)(void);
56    typedef int (* RootFunc_t)(void);
57    typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
58#endif
59
60    bool init(char const *resName, char const *cacheDir,
61              uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags,
62              char const *bccPluginName = nullptr);
63    virtual void populateScript(Script *);
64
65    virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength);
66    virtual int invokeRoot();
67    virtual void preLaunch(uint32_t slot, const Allocation ** ains,
68                           uint32_t inLen, Allocation * aout, const void * usr,
69                           uint32_t usrLen, const RsScriptCall *sc);
70    virtual void postLaunch(uint32_t slot, const Allocation ** ains,
71                            uint32_t inLen, Allocation * aout,
72                            const void * usr, uint32_t usrLen,
73                            const RsScriptCall *sc);
74
75    virtual void invokeForEach(uint32_t slot,
76                               const Allocation ** ains,
77                               uint32_t inLen,
78                               Allocation* aout,
79                               const void* usr,
80                               uint32_t usrLen,
81                               const RsScriptCall* sc);
82
83    virtual void invokeInit();
84    virtual void invokeFreeChildren();
85
86    virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
87    virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength);
88    virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
89                                  const Element *e, const uint32_t *dims, size_t dimLength);
90    virtual void setGlobalBind(uint32_t slot, Allocation *data);
91    virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
92
93
94    virtual ~RsdCpuScriptImpl();
95    RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s);
96
97    const Script * getScript() {return mScript;}
98
99    void forEachMtlsSetup(const Allocation ** ains, uint32_t inLen,
100                          Allocation * aout, const void * usr, uint32_t usrLen,
101                          const RsScriptCall *sc, MTLaunchStruct *mtls);
102
103    virtual void forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls);
104
105
106    const RsdCpuReference::CpuSymbol * lookupSymbolMath(const char *sym);
107    static void * lookupRuntimeStub(void* pContext, char const* name);
108
109    virtual Allocation * getAllocationForPointer(const void *ptr) const;
110
111#ifndef RS_COMPATIBILITY_LIB
112    virtual  void * getRSExecutable() { return mExecutable; }
113#endif
114
115protected:
116    RsdCpuReferenceImpl *mCtx;
117    const Script *mScript;
118
119#ifndef RS_COMPATIBILITY_LIB
120    // Returns the path to the core library we'll use.
121    const char* findCoreLib(const bcinfo::MetadataExtractor& bitCodeMetaData, const char* bitcode,
122                            size_t bitcodeSize);
123    int (*mRoot)();
124    int (*mRootExpand)();
125    void (*mInit)();
126    void (*mFreeChildren)();
127
128    std::vector<std::pair<const char *, uint32_t> > mExportedForEachFuncList;
129
130    bcc::BCCContext *mCompilerContext;
131    bcc::RSCompilerDriver *mCompilerDriver;
132    bcc::CompilerRTSymbolResolver mCompilerRuntime;
133    bcc::LookupFunctionSymbolResolver<void *> mRSRuntime;
134    bcc::SymbolResolverProxy mResolver;
135    bcc::RSExecutable *mExecutable;
136#else
137    void *mScriptSO;
138    RootFunc_t mRoot;
139    RootFunc_t mRootExpand;
140    InvokeFunc_t mInit;
141    InvokeFunc_t mFreeChildren;
142    InvokeFunc_t *mInvokeFunctions;
143    ForEachFunc_t *mForEachFunctions;
144
145    void **mFieldAddress;
146    bool *mFieldIsObject;
147    uint32_t *mForEachSignatures;
148
149    // for populate script
150    //int mVersionMajor;
151    //int mVersionMinor;
152    size_t mExportedVariableCount;
153    size_t mExportedFunctionCount;
154#endif
155
156    Allocation **mBoundAllocs;
157    void * mIntrinsicData;
158    bool mIsThreadable;
159};
160
161Allocation * rsdScriptGetAllocationForPointer(
162                        const Context *dc,
163                        const Script *script,
164                        const void *);
165
166
167
168}
169}
170
171#endif
172