blob: 8060826345db7a676a8754d05b1483ab56f1932e [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
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_CORE_H
18#define RSD_CPU_CORE_H
19
20#include "rsd_cpu.h"
21#include "rsSignal.h"
22#include "rsContext.h"
23#include "rsElement.h"
24#include "rsScriptC.h"
25
Stephen Hines00511322014-01-31 11:20:23 -080026#include <string>
27
Chris Wailesf3712132014-07-16 15:18:30 -070028#define RS_KERNEL_INPUT_THRESHOLD 32
29
Jason Sams709a0972012-11-15 18:18:04 -080030namespace bcc {
31 class BCCContext;
32 class RSCompilerDriver;
33 class RSExecutable;
34}
35
36namespace android {
37namespace renderscript {
38
Chris Wailes80ef6932014-07-08 11:22:18 -070039struct StridePair {
Chris Wailes4b3c34e2014-06-11 12:00:29 -070040 uint32_t eStride;
41 uint32_t yStride;
Chris Wailes80ef6932014-07-08 11:22:18 -070042};
Chris Wailes4b3c34e2014-06-11 12:00:29 -070043
Chris Wailes80ef6932014-07-08 11:22:18 -070044struct RsExpandKernelDriverInfo {
Chris Wailesf3712132014-07-16 15:18:30 -070045 const uint8_t **inPtrs;
46 uint32_t inLen;
47
48 uint8_t *outPtr;
49
50 StridePair *inStrides;
51 StridePair outStride;
Chris Wailescb517982014-07-07 16:27:14 -070052
53 uint32_t dimX;
54 uint32_t dimY;
55 uint32_t dimZ;
Chris Wailescb517982014-07-07 16:27:14 -070056
Chris Wailescb517982014-07-07 16:27:14 -070057 uint32_t slot;
Chris Wailes4b3c34e2014-06-11 12:00:29 -070058
Chris Wailesf3712132014-07-16 15:18:30 -070059 const void *usr;
60 uint32_t usrLen;
61
62 bool heapAllocatedArrays;
63
64 RsExpandKernelDriverInfo() : heapAllocatedArrays(false) {}
Chris Wailes80ef6932014-07-08 11:22:18 -070065
66 ~RsExpandKernelDriverInfo() {
Chris Wailesf3712132014-07-16 15:18:30 -070067 if (heapAllocatedArrays) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070068 if (inPtrs != nullptr) {
Chris Wailesf3712132014-07-16 15:18:30 -070069 delete[] inPtrs;
70 }
Chris Wailes80ef6932014-07-08 11:22:18 -070071
Chris Wailes44bef6f2014-08-12 13:51:10 -070072 if (inStrides != nullptr) {
Chris Wailesf3712132014-07-16 15:18:30 -070073 delete[] inStrides;
74 }
Chris Wailes80ef6932014-07-08 11:22:18 -070075 }
76 }
77};
78
79struct RsExpandKernelParams {
80
81 // Used by kernels
Chris Wailesf3712132014-07-16 15:18:30 -070082 const void **ins;
83 uint32_t *inEStrides;
Chris Wailes80ef6932014-07-08 11:22:18 -070084 void *out;
85 uint32_t y;
86 uint32_t z;
87 uint32_t lid;
88
Chris Wailes80ef6932014-07-08 11:22:18 -070089 // Used by ScriptGroup and user kernels.
90 const void *usr;
91
92 // Used by intrinsics
93 uint32_t dimX;
94 uint32_t dimY;
95 uint32_t dimZ;
96
97 /*
98 * FIXME: This is only used by the blend intrinsic. If possible, we should
99 * modify blur to not need it.
100 */
101 uint32_t slot;
102
103 /// Copy fields needed by a kernel from a driver struct.
104 void takeFields(const RsExpandKernelDriverInfo &dstruct) {
105 this->usr = dstruct.usr;
106 this->slot = dstruct.slot;
107
108 this->dimX = dstruct.dimX;
109 this->dimY = dstruct.dimY;
110 this->dimZ = dstruct.dimZ;
111 }
112};
Chris Wailescb517982014-07-07 16:27:14 -0700113
Jason Samsf5ef8df2013-08-06 13:49:25 -0700114extern bool gArchUseSIMD;
Jason Sams709a0972012-11-15 18:18:04 -0800115
116typedef void (* InvokeFunc_t)(void);
117typedef void (* ForEachFunc_t)(void);
118typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
119
120class RsdCpuScriptImpl;
121class RsdCpuReferenceImpl;
122
Chris Wailesf3712132014-07-16 15:18:30 -0700123struct ScriptTLSStruct {
Jason Sams709a0972012-11-15 18:18:04 -0800124 android::renderscript::Context * mContext;
125 const android::renderscript::Script * mScript;
126 RsdCpuScriptImpl *mImpl;
Chris Wailesf3712132014-07-16 15:18:30 -0700127};
Jason Sams709a0972012-11-15 18:18:04 -0800128
Chris Wailesf3712132014-07-16 15:18:30 -0700129struct MTLaunchStruct {
Chris Wailes80ef6932014-07-08 11:22:18 -0700130 RsExpandKernelDriverInfo fep;
Jason Sams709a0972012-11-15 18:18:04 -0800131
132 RsdCpuReferenceImpl *rsc;
133 RsdCpuScriptImpl *script;
134
135 ForEachFunc_t kernel;
136 uint32_t sig;
Chris Wailesf3712132014-07-16 15:18:30 -0700137 const Allocation ** ains;
Jason Sams709a0972012-11-15 18:18:04 -0800138 Allocation * aout;
139
140 uint32_t mSliceSize;
141 volatile int mSliceNum;
142 bool isThreadable;
143
144 uint32_t xStart;
145 uint32_t xEnd;
146 uint32_t yStart;
147 uint32_t yEnd;
148 uint32_t zStart;
149 uint32_t zEnd;
150 uint32_t arrayStart;
151 uint32_t arrayEnd;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700152
Chris Wailesf3712132014-07-16 15:18:30 -0700153 const uint8_t *inPtrsBuff[RS_KERNEL_INPUT_THRESHOLD];
154 StridePair inStridesBuff[RS_KERNEL_INPUT_THRESHOLD];
155};
Jason Sams709a0972012-11-15 18:18:04 -0800156
157class RsdCpuReferenceImpl : public RsdCpuReference {
158public:
159 virtual ~RsdCpuReferenceImpl();
160 RsdCpuReferenceImpl(Context *);
161
162 void lockMutex();
163 void unlockMutex();
164
165 bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
166 virtual void setPriority(int32_t priority);
167 virtual void launchThreads(WorkerCallback_t cbk, void *data);
168 static void * helperThreadProc(void *vrsc);
169 RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
170
171 Context * getContext() {return mRSC;}
Jason Samsc44d6702012-11-28 18:37:52 -0800172 uint32_t getThreadCount() const {
173 return mWorkers.mCount + 1;
174 }
Jason Sams709a0972012-11-15 18:18:04 -0800175
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700176 void launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout,
177 const RsScriptCall* sc, MTLaunchStruct* mtls);
178
Jason Sams709a0972012-11-15 18:18:04 -0800179 virtual CpuScript * createScript(const ScriptC *s,
180 char const *resName, char const *cacheDir,
181 uint8_t const *bitcode, size_t bitcodeSize,
182 uint32_t flags);
183 virtual CpuScript * createIntrinsic(const Script *s,
184 RsScriptIntrinsicID iid, Element *e);
Yang Ni1ffd86b2015-01-07 09:16:40 -0800185 virtual void* createScriptGroup(const ScriptGroupBase *sg);
Jason Sams709a0972012-11-15 18:18:04 -0800186
187 const RsdCpuReference::CpuSymbol *symLookup(const char *);
188
189 RsdCpuReference::CpuScript * lookupScript(const Script *s) {
190 return mScriptLookupFn(mRSC, s);
191 }
192
Stephen Hinesf218bf12013-02-12 19:32:38 -0800193 void setLinkRuntimeCallback(
194 bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
195 mLinkRuntimeCallback = pLinkRuntimeCallback;
196 }
197 bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
198 return mLinkRuntimeCallback;
199 }
Stephen Hines1d476622013-03-29 22:08:49 -0700200
201 void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
202 mSelectRTCallback = pSelectRTCallback;
203 }
204 RSSelectRTCallback getSelectRTCallback() {
205 return mSelectRTCallback;
206 }
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700207
208 virtual void setSetupCompilerCallback(
209 RSSetupCompilerCallback pSetupCompilerCallback) {
210 mSetupCompilerCallback = pSetupCompilerCallback;
211 }
212 virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
213 return mSetupCompilerCallback;
214 }
Stephen Hines00511322014-01-31 11:20:23 -0800215
216 virtual void setBccPluginName(const char *name) {
217 mBccPluginName.assign(name);
218 }
219 virtual const char *getBccPluginName() const {
220 return mBccPluginName.c_str();
221 }
Stephen Hinesf218bf12013-02-12 19:32:38 -0800222 virtual bool getInForEach() { return mInForEach; }
Jason Sams709a0972012-11-15 18:18:04 -0800223
224protected:
225 Context *mRSC;
226 uint32_t version_major;
227 uint32_t version_minor;
228 //bool mHasGraphics;
229 bool mInForEach;
230
231 struct Workers {
232 volatile int mRunningCount;
233 volatile int mLaunchCount;
234 uint32_t mCount;
235 pthread_t *mThreadId;
236 pid_t *mNativeThreadId;
237 Signal mCompleteSignal;
238 Signal *mLaunchSignals;
239 WorkerCallback_t mLaunchCallback;
240 void *mLaunchData;
241 };
242 Workers mWorkers;
243 bool mExit;
244 sym_lookup_t mSymLookupFn;
245 script_lookup_t mScriptLookupFn;
246
247 ScriptTLSStruct mTlsStruct;
Stephen Hinesf218bf12013-02-12 19:32:38 -0800248
249 bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
Stephen Hines1d476622013-03-29 22:08:49 -0700250 RSSelectRTCallback mSelectRTCallback;
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700251 RSSetupCompilerCallback mSetupCompilerCallback;
Stephen Hines00511322014-01-31 11:20:23 -0800252 std::string mBccPluginName;
Jason Sams709a0972012-11-15 18:18:04 -0800253};
254
255
256}
257}
258
259#endif