blob: cfdb29a650827fa9675a644375c4ee2588602c02 [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"
Yang Nic31585b2015-02-15 11:43:50 -080023#include "rsCppUtils.h"
Jason Sams709a0972012-11-15 18:18:04 -080024#include "rsElement.h"
25#include "rsScriptC.h"
David Grossdced5c92015-03-11 16:12:42 -070026#include "rsCpuCoreRuntime.h"
Jason Sams709a0972012-11-15 18:18:04 -080027
Jason Sams709a0972012-11-15 18:18:04 -080028namespace android {
29namespace renderscript {
30
Matt Wala11fd9ec2015-07-10 16:40:12 -070031// Whether the CPU we're running on supports SIMD instructions
Jason Samsf5ef8df2013-08-06 13:49:25 -070032extern bool gArchUseSIMD;
Jason Sams709a0972012-11-15 18:18:04 -080033
Matt Wala14ce0072015-07-30 17:30:25 -070034// Function types found in RenderScript code
35typedef void (*ReduceFunc_t)(const uint8_t *inBuf, uint8_t *outBuf, uint32_t len);
36typedef void (*ForEachFunc_t)(const RsExpandKernelDriverInfo *info, uint32_t x1, uint32_t x2, uint32_t outStride);
37typedef void (*InvokeFunc_t)(void *params);
38typedef void (*InitOrDtorFunc_t)(void);
39typedef int (*RootFunc_t)(void);
40
41// Internal driver callback used to execute a kernel
Jason Sams709a0972012-11-15 18:18:04 -080042typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
43
44class RsdCpuScriptImpl;
45class RsdCpuReferenceImpl;
46
Chris Wailesf3712132014-07-16 15:18:30 -070047struct ScriptTLSStruct {
Jason Sams709a0972012-11-15 18:18:04 -080048 android::renderscript::Context * mContext;
49 const android::renderscript::Script * mScript;
50 RsdCpuScriptImpl *mImpl;
Chris Wailesf3712132014-07-16 15:18:30 -070051};
Jason Sams709a0972012-11-15 18:18:04 -080052
Matt Wala14ce0072015-07-30 17:30:25 -070053// MTLaunchStruct passes information about a multithreaded kernel launch.
54struct MTLaunchStructCommon {
55 RsdCpuReferenceImpl *rs;
Jason Sams709a0972012-11-15 18:18:04 -080056 RsdCpuScriptImpl *script;
57
Jason Sams709a0972012-11-15 18:18:04 -080058 uint32_t mSliceSize;
59 volatile int mSliceNum;
60 bool isThreadable;
61
Matt Wala14ce0072015-07-30 17:30:25 -070062 // Boundary information about the launch
Jason Samsbf2111d2015-01-26 18:13:41 -080063 RsLaunchDimensions start;
64 RsLaunchDimensions end;
Matt Wala14ce0072015-07-30 17:30:25 -070065 // Points to MTLaunchStructForEach::fep::dim or
66 // MTLaunchStructReduce::inputDim.
67 RsLaunchDimensions *dimPtr;
68};
69
70struct MTLaunchStructForEach : public MTLaunchStructCommon {
71 // Driver info structure
72 RsExpandKernelDriverInfo fep;
73
74 ForEachFunc_t kernel;
75 uint32_t sig;
76 const Allocation *ains[RS_KERNEL_INPUT_LIMIT];
77 Allocation *aout[RS_KERNEL_INPUT_LIMIT];
78};
79
80struct MTLaunchStructReduce : public MTLaunchStructCommon {
81 ReduceFunc_t kernel;
82 const uint8_t *inBuf;
83 uint8_t *outBuf;
84 RsLaunchDimensions inputDim;
Chris Wailesf3712132014-07-16 15:18:30 -070085};
Jason Sams709a0972012-11-15 18:18:04 -080086
87class RsdCpuReferenceImpl : public RsdCpuReference {
88public:
Stephen Hinesc060f142015-05-13 19:26:09 -070089 ~RsdCpuReferenceImpl() override;
Jason Sams709a0972012-11-15 18:18:04 -080090 RsdCpuReferenceImpl(Context *);
91
92 void lockMutex();
93 void unlockMutex();
94
95 bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
Stephen Hinesc060f142015-05-13 19:26:09 -070096 void setPriority(int32_t priority) override;
Jason Sams709a0972012-11-15 18:18:04 -080097 virtual void launchThreads(WorkerCallback_t cbk, void *data);
98 static void * helperThreadProc(void *vrsc);
99 RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
100
101 Context * getContext() {return mRSC;}
Jason Samsc44d6702012-11-28 18:37:52 -0800102 uint32_t getThreadCount() const {
103 return mWorkers.mCount + 1;
104 }
Jason Sams709a0972012-11-15 18:18:04 -0800105
Matt Wala14ce0072015-07-30 17:30:25 -0700106 // Launch foreach kernel
107 void launchForEach(const Allocation **ains, uint32_t inLen, Allocation *aout,
108 const RsScriptCall *sc, MTLaunchStructForEach *mtls);
109
110 // Launch a reduce kernel
111 void launchReduce(const Allocation *ain, Allocation *aout,
112 MTLaunchStructReduce *mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700113
Stephen Hinesc060f142015-05-13 19:26:09 -0700114 CpuScript * createScript(const ScriptC *s, char const *resName, char const *cacheDir,
115 uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags) override;
116 CpuScript * createIntrinsic(const Script *s, RsScriptIntrinsicID iid, Element *e) override;
117 void* createScriptGroup(const ScriptGroupBase *sg) override;
Jason Sams709a0972012-11-15 18:18:04 -0800118
119 const RsdCpuReference::CpuSymbol *symLookup(const char *);
120
Matt Wala14ce0072015-07-30 17:30:25 -0700121 RsdCpuReference::CpuScript *lookupScript(const Script *s) {
Jason Sams709a0972012-11-15 18:18:04 -0800122 return mScriptLookupFn(mRSC, s);
123 }
124
Stephen Hines1d476622013-03-29 22:08:49 -0700125 void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
126 mSelectRTCallback = pSelectRTCallback;
127 }
128 RSSelectRTCallback getSelectRTCallback() {
129 return mSelectRTCallback;
130 }
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700131
Stephen Hines00511322014-01-31 11:20:23 -0800132 virtual void setBccPluginName(const char *name) {
Yang Nic31585b2015-02-15 11:43:50 -0800133 mBccPluginName.setTo(name);
Stephen Hines00511322014-01-31 11:20:23 -0800134 }
135 virtual const char *getBccPluginName() const {
Yang Nic31585b2015-02-15 11:43:50 -0800136 return mBccPluginName.string();
Stephen Hines00511322014-01-31 11:20:23 -0800137 }
Stephen Hinesc060f142015-05-13 19:26:09 -0700138 bool getInForEach() override { return mInForEach; }
Jason Sams709a0972012-11-15 18:18:04 -0800139
Stephen Hines8409d642015-04-28 18:49:56 -0700140 // Set to true if we should embed global variable information in the code.
Stephen Hinesc060f142015-05-13 19:26:09 -0700141 void setEmbedGlobalInfo(bool v) override {
Stephen Hines8409d642015-04-28 18:49:56 -0700142 mEmbedGlobalInfo = v;
143 }
144
145 // Returns true if we should embed global variable information in the code.
Stephen Hinesc060f142015-05-13 19:26:09 -0700146 bool getEmbedGlobalInfo() const override {
Stephen Hines8409d642015-04-28 18:49:56 -0700147 return mEmbedGlobalInfo;
148 }
149
150 // Set to true if we should skip constant (immutable) global variables when
151 // potentially embedding information about globals.
Stephen Hinesc060f142015-05-13 19:26:09 -0700152 void setEmbedGlobalInfoSkipConstant(bool v) override {
Stephen Hines8409d642015-04-28 18:49:56 -0700153 mEmbedGlobalInfoSkipConstant = v;
154 }
155
156 // Returns true if we should skip constant (immutable) global variables when
157 // potentially embedding information about globals.
Stephen Hinesc060f142015-05-13 19:26:09 -0700158 bool getEmbedGlobalInfoSkipConstant() const override {
Stephen Hines8409d642015-04-28 18:49:56 -0700159 return mEmbedGlobalInfoSkipConstant;
160 }
161
Jason Sams709a0972012-11-15 18:18:04 -0800162protected:
163 Context *mRSC;
164 uint32_t version_major;
165 uint32_t version_minor;
166 //bool mHasGraphics;
167 bool mInForEach;
168
169 struct Workers {
170 volatile int mRunningCount;
171 volatile int mLaunchCount;
172 uint32_t mCount;
173 pthread_t *mThreadId;
174 pid_t *mNativeThreadId;
175 Signal mCompleteSignal;
176 Signal *mLaunchSignals;
177 WorkerCallback_t mLaunchCallback;
178 void *mLaunchData;
179 };
180 Workers mWorkers;
181 bool mExit;
182 sym_lookup_t mSymLookupFn;
183 script_lookup_t mScriptLookupFn;
184
185 ScriptTLSStruct mTlsStruct;
Stephen Hinesf218bf12013-02-12 19:32:38 -0800186
Stephen Hines1d476622013-03-29 22:08:49 -0700187 RSSelectRTCallback mSelectRTCallback;
Yang Nic31585b2015-02-15 11:43:50 -0800188 String8 mBccPluginName;
Stephen Hines8409d642015-04-28 18:49:56 -0700189
190 // Specifies whether we should embed global variable information in the
191 // code via special RS variables that can be examined later by the driver.
192 // Defaults to true.
193 bool mEmbedGlobalInfo;
194
195 // Specifies whether we should skip constant (immutable) global variables
196 // when potentially embedding information about globals.
197 // Defaults to true.
198 bool mEmbedGlobalInfoSkipConstant;
Jason Sams709a0972012-11-15 18:18:04 -0800199};
200
201
202}
203}
204
205#endif