1 /*
2  * Copyright (C) 2009-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 ANDROID_RS_SCRIPT_H
18 #define ANDROID_RS_SCRIPT_H
19 
20 #include "rsAllocation.h"
21 #include "rsMap.h"
22 
23 #include <utility>
24 
25 // ---------------------------------------------------------------------------
26 namespace android {
27 namespace renderscript {
28 
29 #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
30 class ProgramVertex;
31 class ProgramFragment;
32 class ProgramRaster;
33 class ProgramStore;
34 #endif
35 
36 class IDBase : public ObjectBase {
37 public:
IDBase(Context * rsc,Script * s,int slot)38     IDBase(Context *rsc, Script *s, int slot) :
39         ObjectBase(rsc), mScript(s), mSlot(slot) {}
~IDBase()40     virtual ~IDBase() {}
41 
serialize(Context * rsc,OStream * stream)42     virtual void serialize(Context *rsc, OStream *stream) const {}
43     virtual RsA3DClassID getClassId() const = 0;
44 
45     Script *mScript;
46     int mSlot;
47 };
48 
49 class ScriptKernelID : public IDBase {
50 public:
51     ScriptKernelID(Context *rsc, Script *s, int slot, int sig);
~ScriptKernelID()52     virtual ~ScriptKernelID() {}
53 
54     virtual RsA3DClassID getClassId() const;
55 
56     bool mHasKernelInput;
57     bool mHasKernelOutput;
58 };
59 
60 class ScriptInvokeID : public IDBase {
61 public:
62     ScriptInvokeID(Context *rsc, Script *s, int slot);
~ScriptInvokeID()63     virtual ~ScriptInvokeID() {}
64 
65     virtual RsA3DClassID getClassId() const;
66 };
67 
68 class ScriptFieldID : public IDBase {
69 public:
70     ScriptFieldID(Context *rsc, Script *s, int slot);
~ScriptFieldID()71     virtual ~ScriptFieldID() {}
72 
73     virtual RsA3DClassID getClassId() const;
74 };
75 
76 class Script : public ObjectBase {
77 public:
78 
79     struct Hal {
80         void * drv;
81 
82         struct DriverInfo {
83             int mVersionMajor;
84             int mVersionMinor;
85 
86             size_t exportedVariableCount;
87             size_t exportedForEachCount;
88             size_t exportedReduceCount;
89             size_t exportedFunctionCount;
90             size_t exportedPragmaCount;
91             char const **exportedPragmaKeyList;
92             char const **exportedPragmaValueList;
93             const Pair<const char *, uint32_t> *exportedForeachFuncList;
94 
95             int (* root)();
96         };
97         DriverInfo info;
98     };
99     Hal mHal;
100 
101     explicit Script(Context *);
102     virtual ~Script();
103 
104     struct Enviroment_t {
105         int64_t mStartTimeMillis;
106         mutable int64_t mLastDtTime;
107 
108 #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
109         ObjectBaseRef<ProgramVertex> mVertex;
110         ObjectBaseRef<ProgramFragment> mFragment;
111         ObjectBaseRef<ProgramRaster> mRaster;
112         ObjectBaseRef<ProgramStore> mFragmentStore;
113 #endif
114     };
115     Enviroment_t mEnviroment;
116 
117     void setSlot(uint32_t slot, Allocation *a);
118     void setVar(uint32_t slot, const void *val, size_t len);
119     void getVar(uint32_t slot, const void *val, size_t len);
120     void setVar(uint32_t slot, const void *val, size_t len, Element *e,
121                 const uint32_t *dims, size_t dimLen);
122     void setVarObj(uint32_t slot, ObjectBase *val);
123 
124     virtual bool freeChildren();
125 
126     virtual void runForEach(Context* rsc,
127                             uint32_t slot,
128                             const Allocation ** ains,
129                             size_t inLen,
130                             Allocation* aout,
131                             const void* usr,
132                             size_t usrBytes,
133                             const RsScriptCall *sc = nullptr) = 0;
134 
135     virtual void runReduce(Context *rsc, uint32_t slot,
136                            const Allocation **ains, size_t inLen,
137                            Allocation *aout, const RsScriptCall *sc) = 0;
138 
139     virtual void Invoke(Context *rsc, uint32_t slot, const void *data, size_t len) = 0;
140     virtual void setupScript(Context *rsc) = 0;
141     virtual uint32_t run(Context *) = 0;
isIntrinsic()142     virtual bool isIntrinsic() const { return false; }
143 
hasObjectSlots()144     bool hasObjectSlots() const {
145         return mHasObjectSlots;
146     }
147     virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
148 
getApiLevel()149     uint32_t getApiLevel() const {
150         return mApiLevel;
151     }
152 
153 protected:
154     bool mInitialized;
155     bool mHasObjectSlots;
156     uint32_t mApiLevel;
157     ObjectBaseRef<Allocation> *mSlots;
158     ObjectBaseRef<const Type> *mTypes;
159 
160 };
161 
162 
163 } // namespace renderscript
164 } // namespace android
165 #endif
166