1 /*
2  * Copyright (C) 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_H
18 #define RSD_CPU_H
19 
20 #include "rsAllocation.h"
21 
22 namespace llvm {
23 
24 class Module;
25 
26 }  // end namespace llvm
27 
28 namespace bcc {
29 
30 class RSCompilerDriver;
31 class RSScript;
32 typedef llvm::Module* (*RSLinkRuntimeCallback)
33         (bcc::RSScript *, llvm::Module *, llvm::Module *);
34 
35 }  // end namespace bcc;
36 
37 typedef const char* (*RSSelectRTCallback) (const char*, size_t);
38 
39 typedef void (*RSSetupCompilerCallback) (bcc::RSCompilerDriver *);
40 
41 namespace android {
42 namespace renderscript {
43 
44 class ScriptC;
45 class Script;
46 class ScriptGroupBase;
47 class ScriptKernelID;
48 
49 
50 class RsdCpuReference {
51 public:
52     struct CpuSymbol {
53         const char * name;
54         void * fnPtr;
55         bool threadable;
56     };
57 
58     typedef const CpuSymbol * (* sym_lookup_t)(Context *, const char *name);
59 
60     struct CpuTls {
61         Context *rsc;
62         const ScriptC * sc;
63     };
64 
65     class CpuScript {
66     public:
67         virtual void populateScript(Script *) = 0;
68         virtual void invokeFunction(uint32_t slot, const void *params, size_t paramLength) = 0;
69         virtual int invokeRoot() = 0;
70 
71         virtual void invokeForEach(uint32_t slot,
72                                    const Allocation ** ains,
73                                    uint32_t inLen,
74                                    Allocation * aout,
75                                    const void * usr,
76                                    uint32_t usrLen,
77                                    const RsScriptCall *sc) = 0;
78 
79         virtual void invokeInit() = 0;
80         virtual void invokeFreeChildren() = 0;
81 
82         virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength) = 0;
83         virtual void getGlobalVar(uint32_t slot, void *data, size_t dataLength) = 0;
84         virtual void setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
85                                       const Element *e, const uint32_t *dims, size_t dimLength) = 0;
86         virtual void setGlobalBind(uint32_t slot, Allocation *data) = 0;
87         virtual void setGlobalObj(uint32_t slot, ObjectBase *obj) = 0;
88 
89         virtual Allocation * getAllocationForPointer(const void *ptr) const = 0;
90 
91         // Returns number of global variables in this Script (may be 0 if
92         // compiler is not configured to emit this information).
93         virtual int getGlobalEntries() const = 0;
94         // Returns the name of the global variable at index i.
95         virtual const char * getGlobalName(int i) const = 0;
96         // Returns the CPU address of the global variable at index i.
97         virtual const void * getGlobalAddress(int i) const = 0;
98         // Returns the size (in bytes) of the global variable at index i.
99         virtual size_t getGlobalSize(int i) const = 0;
100         // Returns the properties of the global variable at index i.
101         virtual uint32_t getGlobalProperties(int i) const = 0;
102 
~CpuScript()103         virtual ~CpuScript() {}
104     };
105     typedef CpuScript * (* script_lookup_t)(Context *, const Script *s);
106 
107     class CpuScriptGroupBase {
108      public:
109       virtual void execute() = 0;
~CpuScriptGroupBase()110       virtual ~CpuScriptGroupBase() {}
111     };
112 
113     class CpuScriptGroup : public CpuScriptGroupBase {
114     public:
115         virtual void setInput(const ScriptKernelID *kid, Allocation *) = 0;
116         virtual void setOutput(const ScriptKernelID *kid, Allocation *) = 0;
~CpuScriptGroup()117         ~CpuScriptGroup() override {};
118     };
119 
120     class CpuScriptGroup2 : public CpuScriptGroupBase {
121      public:
~CpuScriptGroup2()122       ~CpuScriptGroup2() override {}
123     };
124 
125     static Context * getTlsContext();
126     static const Script * getTlsScript();
127     static pthread_key_t getThreadTLSKey();
128 
129     static RsdCpuReference * create(Context *c, uint32_t version_major,
130                                     uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn
131                                     , bcc::RSLinkRuntimeCallback pLinkRuntimeCallback = nullptr,
132                                     RSSelectRTCallback pSelectRTCallback = nullptr,
133                                     const char *pBccPluginName = nullptr
134                                     );
135     virtual ~RsdCpuReference();
136     virtual void setPriority(int32_t priority) = 0;
137 
138     virtual CpuScript * createScript(const ScriptC *s, char const *resName, char const *cacheDir,
139                                      uint8_t const *bitcode, size_t bitcodeSize,
140                                      uint32_t flags) = 0;
141     virtual CpuScript * createIntrinsic(const Script *s, RsScriptIntrinsicID iid, Element *e) = 0;
142     virtual void* createScriptGroup(const ScriptGroupBase *sg) = 0;
143     virtual bool getInForEach() = 0;
144 
145 #ifndef RS_COMPATIBILITY_LIB
146     virtual void setSetupCompilerCallback(
147             RSSetupCompilerCallback pSetupCompilerCallback) = 0;
148     virtual RSSetupCompilerCallback getSetupCompilerCallback() const = 0;
149 #endif
150 
151     // Set to true if we should embed global variable information in the code.
152     virtual void setEmbedGlobalInfo(bool v) = 0;
153 
154     // Returns true if we should embed global variable information in the code.
155     virtual bool getEmbedGlobalInfo() const = 0;
156 
157     // Set to true if we should skip constant (immutable) global variables when
158     // potentially embedding information about globals.
159     virtual void setEmbedGlobalInfoSkipConstant(bool v) = 0;
160 
161     // Returns true if we should skip constant (immutable) global variables when
162     // potentially embedding information about globals.
163     virtual bool getEmbedGlobalInfoSkipConstant() const = 0;
164 };
165 
166 
167 }
168 }
169 
170 #endif
171