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